@0xobelisk/sui-common 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +253 -265
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/codegen/types/index.ts +1 -1
- package/src/codegen/utils/renderMove/generateDefaultSchema.ts +168 -161
- package/src/codegen/utils/renderMove/generateError.ts +18 -22
- package/src/codegen/utils/renderMove/generateEvent.ts +27 -6
- package/src/codegen/utils/renderMove/generateSchema.ts +49 -72
- package/src/codegen/utils/renderMove/generateScript.ts +4 -18
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import y from"prettier";import X from"prettier-plugin-move-js";async function T(e,t){let r;t&&(r=await y.resolveConfig(t));try{return y.format(e,{plugins:[X],parser:"move-parse",printWidth:120,semi:!0,tabWidth:2,useTabs:!1,bracketSpacing:!0,...r})}catch(a){let n;return a instanceof Error?n=a.message:n=a,console.log(`Error during output formatting: ${n}`),e}}async function j(e){return y.format(e,{parser:"typescript"})}import h from"node:fs/promises";import E from"node:path";import R from"debug";var b=R("dubhe:common"),Y=R("dubhe:common");b.log=console.debug.bind(console);Y.log=console.error.bind(console);var $=b.extend("codegen"),q=b.extend("codegen");$.log=console.debug.bind(console);q.log=console.error.bind(console);async function c(e,t,r){let a=await T(e),n=` // Copyright (c) Obelisk Labs, Inc.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
#[allow(unused_use)]
|
|
4
4
|
|
|
5
5
|
/* Autogenerated file. Do not edit manually. */
|
|
6
6
|
|
|
7
|
-
`,
|
|
7
|
+
`,o="#[test_only]",s=n+a,i="#[allow(lint(share_owned), unused_let_mut)]";t.includes(".toml")||t.includes("system")||t.includes("migrate")?s=a:t.includes("init")?s=o+a:t.includes("deploy_hook")&&(s=i+a),await h.mkdir(E.dirname(t),{recursive:!0}),await h.writeFile(t,s),$(`${r}: ${t}`)}async function Me(e,t,r){let a=await j(e);await h.mkdir(E.dirname(t),{recursive:!0}),await h.writeFile(t,a),$(`${r}: ${t}`)}function je(e){return e.replace(/\\/g,"/")}import{existsSync as C}from"fs";import g from"fs";function w(e){g.existsSync(e)&&(g.readdirSync(e).forEach(t=>{let r=`${e}/${t}`;g.lstatSync(r).isDirectory()?w(r):g.unlinkSync(r)}),g.rmdirSync(e))}function f(e){return Object.entries(e).map(([t,r])=>`${t}`).join(",")}function W(e){return`(${Object.entries(e).map(([t,r])=>`${r}`)})`}function u(e){return Object.entries(e).map(([t,r])=>`${t}: ${r}`)}function G(e){return Object.entries(e).map(([t,r])=>`self.${t}`)}async function V(e,t){console.log(`
|
|
8
8
|
\u{1F4C4} Starting Move.toml Generation...`),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/Move.toml`);let r=`[package]
|
|
9
9
|
name = "${e.name}"
|
|
10
10
|
version = "1.0.0"
|
|
@@ -17,145 +17,120 @@ Dubhe = { git = "https://github.com/0xobelisk/dubhe-framework.git", rev = "dubhe
|
|
|
17
17
|
[addresses]
|
|
18
18
|
sui = "0x2"
|
|
19
19
|
${e.name} = "0x0"
|
|
20
|
-
`;await
|
|
21
|
-
`)}function
|
|
20
|
+
`;await c(r,`${t}/contracts/${e.name}/Move.toml`,"formatAndWriteMove"),console.log(`\u2705 Move.toml Generation Complete
|
|
21
|
+
`)}function J(e){return e.sort((t,r)=>{let a=t.charAt(0).toLowerCase(),n=r.charAt(0).toLowerCase();return a<n?-1:a>n?1:0})}function v(e){return e.split("_").map((t,r)=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join("")}function N(e,t){return Object.entries(t).map(([r,a])=>`public(package) fun set_${r}(self: &mut ${e}, ${r}: ${a}) {
|
|
22
22
|
self.${r} = ${r};
|
|
23
23
|
}`).join(`
|
|
24
|
-
`)}function
|
|
24
|
+
`)}function P(e,t){return`public(package) fun set(self: &mut ${e}, ${u(t)}) {
|
|
25
25
|
${Object.entries(t).map(([r])=>`self.${r} = ${r};`).join(`
|
|
26
26
|
`)}
|
|
27
|
-
}`}function
|
|
28
|
-
(${
|
|
29
|
-
}`}function
|
|
27
|
+
}`}function ee(e,t){return`public fun get(self: &${e}): ${W(t)} {
|
|
28
|
+
(${G(t)})
|
|
29
|
+
}`}function te(e,t){return Object.entries(t).map(([r,a])=>`public fun get_${r}(self: &${e}): ${a} {
|
|
30
30
|
self.${r}
|
|
31
31
|
}`).join(`
|
|
32
|
-
`)}function
|
|
33
|
-
\u{1F4E6} Starting Schema Data Generation...`);for(let a of Object.keys(t)){let
|
|
34
|
-
public enum ${
|
|
35
|
-
${
|
|
32
|
+
`)}function l(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}async function O(e,t,r){console.log(`
|
|
33
|
+
\u{1F4E6} Starting Schema Data Generation...`);for(let a of Object.keys(t)){let n=a,o=t[a];console.log(` \u2514\u2500 Generating ${n} ${Array.isArray(o)?"(enum)":"(struct)"}`);let s="",i=Object.keys(t).filter(p=>Array.isArray(t[p])).map(p=>p);if(console.log(i),Array.isArray(o)){let p=J(o);s=`module ${e}::${l(n)} {
|
|
34
|
+
public enum ${n} has copy, drop , store {
|
|
35
|
+
${p}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
${
|
|
39
|
-
${
|
|
40
|
-
}`).join("")}
|
|
38
|
+
${p.map(d=>`public fun new_${l(d)}(): ${n} {
|
|
39
|
+
${n}::${d}
|
|
40
|
+
}`).join("")}`}else s=`module ${e}::${l(n)} {
|
|
41
41
|
use std::ascii::String;
|
|
42
|
-
${i.map(
|
|
42
|
+
${i.map(p=>`use ${e}::${l(p)}::${p};`).join(`
|
|
43
43
|
`)}
|
|
44
44
|
|
|
45
|
-
public struct ${
|
|
46
|
-
${
|
|
45
|
+
public struct ${n} has copy, drop , store {
|
|
46
|
+
${u(o)}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
public fun new(${
|
|
50
|
-
${
|
|
51
|
-
${
|
|
49
|
+
public fun new(${u(o)}): ${n} {
|
|
50
|
+
${n} {
|
|
51
|
+
${f(o)}
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
${
|
|
56
|
-
${
|
|
57
|
-
${
|
|
58
|
-
${
|
|
59
|
-
}
|
|
60
|
-
`)}function
|
|
61
|
-
`):""}async function
|
|
62
|
-
\u{1F528} Starting Schema Structure Generation...`)
|
|
55
|
+
${ee(n,o)}
|
|
56
|
+
${te(n,o)}
|
|
57
|
+
${N(n,o)}
|
|
58
|
+
${P(n,o)}
|
|
59
|
+
}`;await c(s,`${r}/contracts/${e}/sources/codegen/data/${l(n)}.move`,"formatAndWriteMove")}console.log(`\u2705 Schema Data Generation Complete
|
|
60
|
+
`)}function re(e,t){return t!=null?Object.keys(t).map(a=>`use ${e}::${l(a)}::${a};`).join(`
|
|
61
|
+
`):""}async function x(e,t,r,a){console.log(`
|
|
62
|
+
\u{1F528} Starting Schema Structure Generation...`),console.log(` \u251C\u2500 Output path: ${a}/contracts/${e}/sources/codegen/schema.move`),console.log(` \u2514\u2500 Structure fields: ${Object.values(r).length}`);let n=`module ${e}::schema {
|
|
63
63
|
use std::ascii::String;
|
|
64
64
|
use std::ascii::string;
|
|
65
65
|
use sui::package::UpgradeCap;
|
|
66
66
|
use std::type_name;
|
|
67
|
-
use dubhe::
|
|
67
|
+
use dubhe::storage;
|
|
68
68
|
use dubhe::storage_value::{Self, StorageValue};
|
|
69
69
|
use dubhe::storage_map::{Self, StorageMap};
|
|
70
70
|
use dubhe::storage_double_map::{Self, StorageDoubleMap};
|
|
71
71
|
use sui::dynamic_field as df;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
public struct ${m(o)} has key, store {
|
|
78
|
-
id: UID
|
|
79
|
-
}
|
|
72
|
+
|
|
73
|
+
${re(e,t)}
|
|
74
|
+
|
|
75
|
+
public struct Schema has key, store { id: UID }
|
|
80
76
|
|
|
81
|
-
${Object.entries(
|
|
82
|
-
|
|
77
|
+
${Object.entries(r).map(([o,s])=>`public fun borrow_${o}(self: &Schema) : &${s} {
|
|
78
|
+
storage::borrow_field(&self.id, b"${o}")
|
|
83
79
|
}
|
|
84
80
|
|
|
85
|
-
public(package) fun ${
|
|
86
|
-
|
|
81
|
+
public(package) fun ${o}(self: &mut Schema): &mut ${s} {
|
|
82
|
+
storage::borrow_mut_field(&mut self.id, b"${o}")
|
|
87
83
|
}
|
|
88
84
|
`).join("")}
|
|
89
85
|
|
|
90
86
|
|
|
91
|
-
public(package) fun create(ctx: &mut TxContext):
|
|
87
|
+
public(package) fun create(ctx: &mut TxContext): Schema {
|
|
92
88
|
let mut id = object::new(ctx);
|
|
93
|
-
${Object.entries(
|
|
89
|
+
${Object.entries(r).map(([o,s])=>{let i="";return s.includes("StorageValue")?i=`storage_value::new(b"${o}", ctx)`:s.includes("StorageMap")?i=`storage_map::new(b"${o}", ctx)`:s.includes("StorageDoubleMap")&&(i=`storage_double_map::new(b"${o}", ctx)`),`storage::add_field<${s}>(&mut id, b"${o}", ${i});`}).join(`
|
|
90
|
+
`)}
|
|
94
91
|
|
|
95
|
-
|
|
92
|
+
Schema { id }
|
|
96
93
|
}
|
|
97
94
|
|
|
98
|
-
public fun migrate(
|
|
95
|
+
public fun migrate(_schema: &mut Schema, _cap: &UpgradeCap, _ctx: &mut TxContext) { }
|
|
99
96
|
|
|
100
|
-
|
|
101
97
|
|
|
102
98
|
// ======================================== View Functions ========================================
|
|
103
|
-
${Object.entries(
|
|
104
|
-
self.borrow_${
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
public fun get_${i}_values(self: &${m(o)}) : vector<${u[1]}> {
|
|
108
|
-
self.borrow_${i}().values()
|
|
109
|
-
}`)):c.includes("StorageDoubleMap")&&(f=[`key1: ${u[0]}`,`key2: ${u[1]}`],b=`${u[2]}`,$="borrow(key1, key2)",!c.includes("Balance")&&!c.includes("Coin")&&(v=`public fun get_${i}_keys(self: &${m(o)}) : (vector<${u[0]}>, vector<${u[1]}>) {
|
|
110
|
-
self.borrow_${i}().keys()
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
public fun get_${i}_values(self: &${m(o)}) : vector<${u[2]}> {
|
|
114
|
-
self.borrow_${i}().values()
|
|
115
|
-
}`)),`public fun get_${i}(self: &${m(o)}, ${f}) : &${b} {
|
|
116
|
-
self.borrow_${i}().${$}
|
|
117
|
-
}
|
|
118
|
-
`+v}).join("")}
|
|
99
|
+
${Object.entries(r).map(([o,s])=>{let i=s.match(/<(.+)>/)[1].split(",").map(Z=>Z.trim()),p=[],d="",_="";return s.includes("StorageValue")?(p=[],d=`${i[0]}`,_="get()"):s.includes("StorageMap")?(p=[`key: ${i[0]}`],d=`${i[1]}`,_="get(key)"):s.includes("StorageDoubleMap")&&(p=[`key1: ${i[0]}`,`key2: ${i[1]}`],d=`${i[2]}`,_="get(key1, key2)"),`public fun get_${o}(self: &Schema, ${p}) : &${d} {
|
|
100
|
+
self.borrow_${o}().${_}
|
|
101
|
+
}`}).join(`
|
|
102
|
+
`)}
|
|
119
103
|
// =========================================================================================================
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
`)
|
|
124
|
-
\u{1F4DD} Starting Deploy Hook Generation...`),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/scripts/deploy_hook.move`);let r=Object.keys(e.schemas).map(s=>`_${s}: &mut ${m(s)}`).join(","),a=Object.keys(e.schemas).map(s=>`use ${e.name}::${s}_schema::${m(s)};`).join(`
|
|
125
|
-
`),o=`${t}/contracts/${e.name}/sources/scripts/deploy_hook.move`,n="";if(!F(o))n=`module ${e.name}::deploy_hook {
|
|
104
|
+
}`;await c(n,`${a}/contracts/${e}/sources/codegen/schema.move`,"formatAndWriteMove"),console.log(`\u2705 Schema Structure Generation Complete
|
|
105
|
+
`)}import{existsSync as L}from"fs";import{readFileSync as ae}from"fs";function oe(e){let t=ae(e,"utf-8"),r=/\/\/ Logic that needs to be automated once the contract is deployed\s*\{([\s\S]*?)\}\s*;\s*\/\/ Authorize schemas and public share objects/,a=t.match(r);if(a)return a[1].trim();throw new Error("Logic and authorization block not found in the file.")}async function F(e,t){console.log(`
|
|
106
|
+
\u{1F4DD} Starting Deploy Hook Generation...`),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/scripts/deploy_hook.move`);let r=Object.keys(e.schemas).map(s=>`_${s}: &mut ${v(s)}`).join(","),a=Object.keys(e.schemas).map(s=>`use ${e.name}::${s}_schema::${v(s)};`).join(`
|
|
107
|
+
`),n=`${t}/contracts/${e.name}/sources/scripts/deploy_hook.move`,o="";if(!L(n))o=`module ${e.name}::deploy_hook {
|
|
126
108
|
use std::ascii::string;
|
|
127
109
|
use sui::clock::Clock;
|
|
128
110
|
use ${e.name}::dapp_system;
|
|
129
|
-
${a}
|
|
130
111
|
public entry fun run(clock: &Clock, ctx: &mut TxContext) {
|
|
131
112
|
// Create a dapp.
|
|
132
113
|
let mut dapp = dapp_system::create(string(b"${e.name}"),string(b"${e.description}"), clock , ctx);
|
|
133
114
|
|
|
134
115
|
// Create schemas
|
|
135
|
-
|
|
136
|
-
`)}
|
|
116
|
+
let mut schema = ${e.name}::schema::create(ctx);
|
|
137
117
|
// Logic that needs to be automated once the contract is deployed
|
|
138
118
|
{
|
|
139
119
|
};
|
|
140
120
|
// Authorize schemas and public share objects
|
|
141
|
-
|
|
142
|
-
dapp.add_schema<${m(s)}>(${s}, ctx);
|
|
143
|
-
`).join(`
|
|
144
|
-
`)}
|
|
121
|
+
dapp.add_schema(schema);
|
|
145
122
|
sui::transfer::public_share_object(dapp);
|
|
146
123
|
}
|
|
147
|
-
}`;else{let s=
|
|
124
|
+
}`;else{let s=oe(n);o=`module ${e.name}::deploy_hook {
|
|
148
125
|
use std::ascii::string;
|
|
149
126
|
use sui::clock::Clock;
|
|
150
127
|
use ${e.name}::dapp_system;
|
|
151
|
-
${a}
|
|
152
128
|
public entry fun run(clock: &Clock, ctx: &mut TxContext) {
|
|
153
129
|
// Create a dapp.
|
|
154
130
|
let mut dapp = dapp_system::create(string(b"${e.name}"),string(b"${e.description}"), clock , ctx);
|
|
155
131
|
|
|
156
132
|
// Create schemas
|
|
157
|
-
|
|
158
|
-
`)}
|
|
133
|
+
let mut schema = ${e.name}::schema::create(ctx);
|
|
159
134
|
// Logic that needs to be automated once the contract is deployed
|
|
160
135
|
|
|
161
136
|
{
|
|
@@ -163,55 +138,61 @@ ${e.name} = "0x0"
|
|
|
163
138
|
};
|
|
164
139
|
|
|
165
140
|
// Authorize schemas and public share objects
|
|
166
|
-
|
|
167
|
-
dapp.add_schema<${m(i)}>(${i}, ctx);
|
|
168
|
-
`).join(`
|
|
169
|
-
`)}
|
|
141
|
+
dapp.add_schema(schema);
|
|
170
142
|
sui::transfer::public_share_object(dapp);
|
|
171
143
|
}
|
|
172
|
-
}`}await
|
|
173
|
-
`)}async function
|
|
144
|
+
}`}await c(o,n,"formatAndWriteMove"),console.log(`\u2705 Deploy Hook Generation Complete
|
|
145
|
+
`)}async function U(e,t){if(!L(`${t}/contracts/${e.name}/sources/scripts/migrate.move`)){let r=`module ${e.name}::migrate {
|
|
174
146
|
const ON_CHAIN_VERSION: u32 = 1;
|
|
175
147
|
|
|
176
148
|
public fun on_chain_version(): u32 {
|
|
177
149
|
ON_CHAIN_VERSION
|
|
178
150
|
}
|
|
179
151
|
}
|
|
180
|
-
`;await
|
|
181
|
-
`):""}async function
|
|
182
|
-
\u{1F4E6} Starting Schema Event Generation...`);for(let o of Object.keys(r)){let
|
|
152
|
+
`;await c(r,`${t}/contracts/${e.name}/sources/scripts/migrate.move`,"formatAndWriteMove")}}function m(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}function I(e,t){return t!=null?Object.keys(t).map(a=>`use ${e}::${m(a)}::${a};`).join(`
|
|
153
|
+
`):""}async function D(e,t,r,a){console.log(`
|
|
154
|
+
\u{1F4E6} Starting Schema Event Generation...`);for(let o of Object.keys(r)){let s=o,i=r[o];console.log(` \u2514\u2500 Generating ${s} event: ${i}`);let p=`module ${e}::${m(s)}_event {
|
|
183
155
|
use sui::event;
|
|
184
156
|
use std::ascii::String;
|
|
185
|
-
${
|
|
157
|
+
${I(e,t)}
|
|
186
158
|
|
|
187
|
-
public struct ${
|
|
188
|
-
${
|
|
159
|
+
public struct ${s}Event has copy, drop {
|
|
160
|
+
${u(i)}
|
|
189
161
|
}
|
|
190
162
|
|
|
191
|
-
public fun new(${
|
|
192
|
-
${
|
|
193
|
-
${
|
|
163
|
+
public fun new(${u(i)}): ${s}Event {
|
|
164
|
+
${s}Event {
|
|
165
|
+
${f(i)}
|
|
194
166
|
}
|
|
195
167
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
168
|
+
}`;await c(p,`${a}/contracts/${e}/sources/codegen/data/${m(s)}_event.move`,"formatAndWriteMove")}let n=`module ${e}::events {
|
|
169
|
+
use std::ascii::{String, string};
|
|
170
|
+
${I(e,t)}
|
|
171
|
+
${Object.entries(r).map(([o,s])=>`
|
|
172
|
+
use ${e}::${m(o)}_event::${o}Event;
|
|
173
|
+
use ${e}::${m(o)}_event;
|
|
174
|
+
public fun ${m(o)}_event(${u(s)}) {
|
|
175
|
+
dubhe::storage_event::emit_set_record<${o}Event, ${o}Event, ${o}Event>(
|
|
176
|
+
string(b"${m(o)}_event"),
|
|
177
|
+
option::none(),
|
|
178
|
+
option::none(),
|
|
179
|
+
option::some(${m(o)}_event::new(${f(s)}))
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
`).join(`
|
|
183
|
+
`)}
|
|
184
|
+
}`;await c(n,`${a}/contracts/${e}/sources/codegen/events.move`,"formatAndWriteMove"),console.log(`\u2705 Schema Event Generation Complete
|
|
185
|
+
`)}import{existsSync as se}from"fs";import ne from"node:fs/promises";async function z(e,t){console.log(`
|
|
186
|
+
\u2699\uFE0F Starting System Generation...`),console.log(" \u251C\u2500 Generating systems"),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/systems`),se(`${t}/contracts/${e.name}/sources/systems`)||await ne.mkdir(`${t}/contracts/${e.name}/sources/systems`,{recursive:!0}),console.log(`\u2705 System Generation Complete
|
|
187
|
+
`)}function ie(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}async function B(e,t,r){console.log(`
|
|
188
|
+
\u{1F4E6} Starting Schema Error Generation...`);let a=`module ${e}::errors {
|
|
189
|
+
${Object.entries(t).map(([n,o])=>(console.log(` \u251C\u2500 Generating Error: ${n}`),console.log(` \u2502 \u2514\u2500 Message: ${o}`),`#[error]
|
|
190
|
+
const ${n}: vector<u8> = b"${o}";
|
|
191
|
+
public fun ${ie(n)}_error(condition: bool) { assert!(condition, ${n}) }
|
|
192
|
+
`)).join(`
|
|
193
|
+
`)}
|
|
194
|
+
}`;await c(a,`${r}/contracts/${e}/sources/codegen/errors.move`,"formatAndWriteMove"),console.log(`\u2705 Schema Error Generation Complete
|
|
195
|
+
`)}import{existsSync as k}from"fs";async function H(e,t){await ce(e,t),await pe(e,t),await ue(e,t)}async function ce(e,t){let r=`${t}/contracts/${e.name}/sources/codegen/dapp/metadata.move`;if(!k(r)){let a=`module ${e.name}::dapp_metadata {
|
|
215
196
|
use std::ascii::String;
|
|
216
197
|
|
|
217
198
|
public struct DappMetadata has drop, copy, store {
|
|
@@ -307,125 +288,125 @@ ${e.name} = "0x0"
|
|
|
307
288
|
}
|
|
308
289
|
|
|
309
290
|
}
|
|
310
|
-
`;await
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
storage_migration::borrow_field(&self.id, b"admin")
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
public(package) fun borrow_mut_admin(self: &mut Dapp): &mut StorageValue<address> {
|
|
325
|
-
storage_migration::borrow_mut_field(&mut self.id, b"admin")
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
public fun borrow_package_id(self: &Dapp): &StorageValue<address> {
|
|
329
|
-
storage_migration::borrow_field(&self.id, b"package_id")
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
public(package) fun borrow_mut_package_id(self: &mut Dapp): &mut StorageValue<address> {
|
|
333
|
-
storage_migration::borrow_mut_field(&mut self.id, b"package_id")
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
public fun borrow_version(self: &Dapp): &StorageValue<u32> {
|
|
337
|
-
storage_migration::borrow_field(&self.id, b"version")
|
|
338
|
-
}
|
|
291
|
+
`;await c(a,r,"formatAndWriteMove")}}async function pe(e,t){let r=`${t}/contracts/${e.name}/sources/codegen/dapp/schema.move`;if(!k(r)){let a=`module ${e.name}::dapp_schema {
|
|
292
|
+
use ${e.name}::dapp_metadata::DappMetadata;
|
|
293
|
+
use dubhe::storage_value;
|
|
294
|
+
use dubhe::storage_value::StorageValue;
|
|
295
|
+
use dubhe::storage;
|
|
296
|
+
use sui::transfer::public_share_object;
|
|
297
|
+
use dubhe::type_info;
|
|
298
|
+
|
|
299
|
+
public struct Dapp has key, store {
|
|
300
|
+
id: UID,
|
|
301
|
+
}
|
|
339
302
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
303
|
+
public fun borrow_admin(self: &Dapp): &StorageValue<address> {
|
|
304
|
+
storage::borrow_field(&self.id, b"admin")
|
|
305
|
+
}
|
|
343
306
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
307
|
+
public(package) fun admin(self: &mut Dapp): &mut StorageValue<address> {
|
|
308
|
+
storage::borrow_mut_field(&mut self.id, b"admin")
|
|
309
|
+
}
|
|
347
310
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
311
|
+
public fun borrow_package_id(self: &Dapp): &StorageValue<address> {
|
|
312
|
+
storage::borrow_field(&self.id, b"package_id")
|
|
313
|
+
}
|
|
351
314
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
315
|
+
public(package) fun package_id(self: &mut Dapp): &mut StorageValue<address> {
|
|
316
|
+
storage::borrow_mut_field(&mut self.id, b"package_id")
|
|
317
|
+
}
|
|
355
318
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
319
|
+
public fun borrow_version(self: &Dapp): &StorageValue<u32> {
|
|
320
|
+
storage::borrow_field(&self.id, b"version")
|
|
321
|
+
}
|
|
359
322
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
323
|
+
public(package) fun version(self: &mut Dapp): &mut StorageValue<u32> {
|
|
324
|
+
storage::borrow_mut_field(&mut self.id, b"version")
|
|
325
|
+
}
|
|
363
326
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
327
|
+
public fun borrow_metadata(self: &Dapp): &StorageValue<DappMetadata> {
|
|
328
|
+
storage::borrow_field(&self.id, b"metadata")
|
|
329
|
+
}
|
|
367
330
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
storage_migration::add_field<StorageValue<address>>(&mut id, b"package_id", storage_value::new());
|
|
372
|
-
storage_migration::add_field<StorageValue<u32>>(&mut id, b"version", storage_value::new());
|
|
373
|
-
storage_migration::add_field<StorageValue<DappMetadata>>(&mut id, b"metadata", storage_value::new());
|
|
374
|
-
storage_migration::add_field<StorageValue<vector<address>>>(&mut id, b"schemas", storage_value::new());
|
|
375
|
-
storage_migration::add_field<StorageValue<bool>>(&mut id, b"safe_mode", storage_value::new());
|
|
376
|
-
Dapp { id }
|
|
377
|
-
}
|
|
331
|
+
public(package) fun metadata(self: &mut Dapp): &mut StorageValue<DappMetadata> {
|
|
332
|
+
storage::borrow_mut_field(&mut self.id, b"metadata")
|
|
333
|
+
}
|
|
378
334
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
335
|
+
public fun borrow_safe_mode(self: &Dapp): &StorageValue<bool> {
|
|
336
|
+
storage::borrow_field(&self.id, b"safe_mode")
|
|
337
|
+
}
|
|
382
338
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
339
|
+
public(package) fun safe_mode(self: &mut Dapp): &mut StorageValue<bool> {
|
|
340
|
+
storage::borrow_mut_field(&mut self.id, b"safe_mode")
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
public(package) fun borrow_schemas(self: &Dapp): &StorageValue<vector<address>> {
|
|
344
|
+
storage::borrow_field(&self.id, b"schemas")
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
public(package) fun schemas(self: &mut Dapp): &mut StorageValue<vector<address>> {
|
|
348
|
+
storage::borrow_mut_field(&mut self.id, b"schemas")
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
public(package) fun create(ctx: &mut TxContext): Dapp {
|
|
353
|
+
let mut id = object::new(ctx);
|
|
354
|
+
storage::add_field<StorageValue<address>>(&mut id, b"admin", storage_value::new(b"admin", ctx));
|
|
355
|
+
storage::add_field<StorageValue<address>>(&mut id, b"package_id", storage_value::new(b"package_id", ctx));
|
|
356
|
+
storage::add_field<StorageValue<u32>>(&mut id, b"version", storage_value::new(b"version", ctx));
|
|
357
|
+
storage::add_field<StorageValue<DappMetadata>>(&mut id, b"metadata", storage_value::new(b"metadata", ctx));
|
|
358
|
+
storage::add_field<StorageValue<bool>>(&mut id, b"safe_mode", storage_value::new(b"safe_mode", ctx));
|
|
359
|
+
storage::add_field<StorageValue<vector<address>>>(&mut id, b"schemas", storage_value::new(b"schemas", ctx));
|
|
360
|
+
Dapp { id }
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
public(package) fun upgrade<DappKey: drop>(dapp: &mut Dapp, ctx: &TxContext) {
|
|
364
|
+
assert!(dapp.borrow_metadata().contains(), 0);
|
|
365
|
+
assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
|
|
366
|
+
let new_package_id = type_info::current_package_id<DappKey>();
|
|
367
|
+
dapp.package_id().set(new_package_id);
|
|
368
|
+
let current_version = dapp.version()[];
|
|
369
|
+
dapp.version().set(current_version + 1);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
public(package) fun add_schema<Schema: key + store>(dapp: &mut Dapp, schema: Schema) {
|
|
373
|
+
let mut schemas = dapp.schemas()[];
|
|
374
|
+
schemas.push_back(object::id_address<Schema>(&schema));
|
|
375
|
+
dapp.schemas().set(schemas);
|
|
376
|
+
public_share_object(schema);
|
|
377
|
+
}
|
|
389
378
|
|
|
390
|
-
|
|
391
|
-
assert!(dapp.borrow_metadata().contains(), 0);
|
|
392
|
-
assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
|
|
379
|
+
#[test_only]
|
|
393
380
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
}
|
|
381
|
+
public fun create_dapp_for_testing(ctx: &mut TxContext): Dapp {
|
|
382
|
+
create(ctx)
|
|
383
|
+
}
|
|
398
384
|
|
|
399
|
-
|
|
400
|
-
public fun create_dapp_for_testing(ctx: &mut TxContext): Dapp {
|
|
401
|
-
create(ctx)
|
|
402
|
-
}
|
|
385
|
+
#[test_only]
|
|
403
386
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
387
|
+
public fun distroy_dapp_for_testing(dapp: Dapp) {
|
|
388
|
+
let Dapp { id } = dapp;
|
|
389
|
+
id.delete();
|
|
390
|
+
}
|
|
409
391
|
}
|
|
410
|
-
`;await
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
dapp.borrow_mut_metadata().set(
|
|
392
|
+
`;await c(a,r,"formatAndWriteMove")}}async function ue(e,t){let r=`${t}/contracts/${e.name}/sources/codegen/dapp/system.move`;if(!k(r)){let a=`module ${e.name}::dapp_system {
|
|
393
|
+
use std::ascii::String;
|
|
394
|
+
use std::ascii;
|
|
395
|
+
use dubhe::type_info;
|
|
396
|
+
use sui::clock::Clock;
|
|
397
|
+
use ${e.name}::dapp_schema;
|
|
398
|
+
use ${e.name}::dapp_metadata;
|
|
399
|
+
use ${e.name}::dapp_schema::Dapp;
|
|
400
|
+
|
|
401
|
+
public struct DappKey has drop {}
|
|
402
|
+
public(package) fun new(): DappKey {
|
|
403
|
+
DappKey { }
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
public(package) fun create(name: String, description: String, clock: &Clock, ctx: &mut TxContext): Dapp {
|
|
407
|
+
let mut dapp = dapp_schema::create(ctx);
|
|
408
|
+
assert!(!dapp.borrow_metadata().contains(), 0);
|
|
409
|
+
dapp.metadata().set(
|
|
429
410
|
dapp_metadata::new(
|
|
430
411
|
name,
|
|
431
412
|
description,
|
|
@@ -435,20 +416,28 @@ ${e.name} = "0x0"
|
|
|
435
416
|
vector[]
|
|
436
417
|
)
|
|
437
418
|
);
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
419
|
+
let package_id = type_info::current_package_id<DappKey>();
|
|
420
|
+
dapp.package_id().set(package_id);
|
|
421
|
+
dapp.admin().set(ctx.sender());
|
|
422
|
+
dapp.version().set(1);
|
|
423
|
+
dapp.safe_mode().set(false);
|
|
424
|
+
dapp.schemas().set(vector[]);
|
|
425
|
+
dapp
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
public entry fun set_metadata(
|
|
429
|
+
dapp: &mut Dapp,
|
|
430
|
+
name: String,
|
|
431
|
+
description: String,
|
|
432
|
+
icon_url: String,
|
|
433
|
+
website_url: String,
|
|
434
|
+
partners: vector<String>,
|
|
435
|
+
ctx: &TxContext,
|
|
436
|
+
) {
|
|
437
|
+
let admin = dapp.admin().try_get();
|
|
438
|
+
assert!(admin == option::some(ctx.sender()), 0);
|
|
439
|
+
let created_at = dapp.metadata().remove().get_created_at();
|
|
440
|
+
dapp.metadata().set(
|
|
452
441
|
dapp_metadata::new(
|
|
453
442
|
name,
|
|
454
443
|
description,
|
|
@@ -458,37 +447,36 @@ ${e.name} = "0x0"
|
|
|
458
447
|
partners
|
|
459
448
|
)
|
|
460
449
|
);
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
assert!(dapp.borrow_schemas().get().contains(&schema_id), 0);
|
|
488
|
-
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
public entry fun transfer_ownership(dapp: &mut Dapp, new_admin: address, ctx: &mut TxContext) {
|
|
453
|
+
let admin = dapp.admin().try_get();
|
|
454
|
+
assert!(admin == option::some(ctx.sender()), 0);
|
|
455
|
+
dapp.admin().set(new_admin);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
public entry fun set_safe_mode(dapp: &mut Dapp, safe_mode: bool, ctx: &TxContext) {
|
|
459
|
+
let admin = dapp.admin().try_get();
|
|
460
|
+
assert!(admin == option::some(ctx.sender()), 0);
|
|
461
|
+
dapp.safe_mode().set(safe_mode);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
public fun ensure_no_safe_mode(dapp: &Dapp) {
|
|
465
|
+
assert!(!dapp.borrow_safe_mode()[], 0);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
public fun ensure_has_authority(dapp: &Dapp, ctx: &TxContext) {
|
|
469
|
+
assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
public fun ensure_has_schema<Schema: key + store>(dapp: &Dapp, schema: &Schema) {
|
|
473
|
+
let schema_id = object::id_address(schema);
|
|
474
|
+
assert!(dapp.borrow_schemas().get().contains(&schema_id), 0);
|
|
475
|
+
}
|
|
489
476
|
}
|
|
490
477
|
|
|
491
|
-
|
|
478
|
+
|
|
479
|
+
`;await c(a,r,"formatAndWriteMove")}}async function K(e,t){console.log(`
|
|
492
480
|
\u{1F4DD} Starting Init Generation...`),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/tests/init.move`);let r=`module ${e.name}::init_test {
|
|
493
481
|
use ${e.name}::dapp_schema::Dapp;
|
|
494
482
|
use sui::clock;
|
|
@@ -506,8 +494,8 @@ ${e.name} = "0x0"
|
|
|
506
494
|
(scenario, dapp)
|
|
507
495
|
}
|
|
508
496
|
}
|
|
509
|
-
`;await
|
|
510
|
-
`)}async function
|
|
511
|
-
\u{1F680} Starting Schema Generation Process...`),console.log("\u{1F4CB} Project Configuration:"),console.log(` \u251C\u2500 Name: ${e.name}`),console.log(` \u251C\u2500 Description: ${e.description||"No description provided"}`),console.log(` \u251C\u2500 Network: ${r||"testnet"}`);let a=t??process.cwd();
|
|
512
|
-
`)}import{findUp as
|
|
497
|
+
`;await c(r,`${t}/contracts/${e.name}/sources/tests/init.move`,"formatAndWriteMove"),console.log(`\u2705 Deploy Hook Generation Complete
|
|
498
|
+
`)}async function bt(e,t,r){console.log(`
|
|
499
|
+
\u{1F680} Starting Schema Generation Process...`),console.log("\u{1F4CB} Project Configuration:"),console.log(` \u251C\u2500 Name: ${e.name}`),console.log(` \u251C\u2500 Description: ${e.description||"No description provided"}`),console.log(` \u251C\u2500 Network: ${r||"testnet"}`);let a=t??process.cwd();C(`${a}/contracts/${e.name}`)&&w(`${a}/contracts/${e.name}/sources/codegen`),C(`${a}/contracts/${e.name}/Move.toml`)||await V(e,a),C(`${a}/contracts/${e.name}/sources/script/deploy_hook.move`)||await F(e,a),e.events&&(e.data?await D(e.name,e.data,e.events,a):await D(e.name,null,e.events,a)),e.data?(await O(e.name,e.data,a),await x(e.name,e.data,e.schemas,a)):await x(e.name,null,e.schemas,a),e.errors&&await B(e.name,e.errors,a),await H(e,a),await K(e,a),await z(e,a),await U(e,a),console.log(`\u2705 Schema Generation Process Complete!
|
|
500
|
+
`)}import{findUp as me}from"find-up";import A from"path";import de from"esbuild";var S=class extends Error{name="NotInsideProjectError";message="You are not inside a Dubhe project"};import{rmSync as le}from"fs";import{pathToFileURL as ge}from"url";import fe from"os";var _e=["dubhe.config.js","dubhe.config.mjs","dubhe.config.ts","dubhe.config.mts"],M="dubhe.config.example.mjs";async function Ct(e){e=await Q(e);try{return await de.build({entryPoints:[e],format:"esm",outfile:M,platform:"node",bundle:!0,packages:"external"}),e=await Q(M,!0),(await import(e+`?update=${Date.now()}`)).dubheConfig}finally{le(M,{force:!0})}}async function Q(e,t){return e===void 0?e=await be():A.isAbsolute(e)||(e=A.join(process.cwd(),e),e=A.normalize(e)),t&&fe.platform()==="win32"?ge(e).href:e}async function be(){let e=await me(_e);if(e===void 0)throw new S;return e}export{c as formatAndWriteMove,Me as formatAndWriteTypescript,T as formatMove,j as formatTypescript,Ct as loadConfig,je as posixPath,Q as resolveConfigPath,bt as schemaGen};
|
|
513
501
|
//# sourceMappingURL=index.js.map
|