@0xobelisk/sui-common 1.0.2 → 1.0.4
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 +239 -260
- 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/generateEvent.ts +3 -2
- package/src/codegen/utils/renderMove/generateSchema.ts +52 -74
- package/src/codegen/utils/renderMove/generateScript.ts +4 -18
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import S from"prettier";import q from"prettier-plugin-move-js";async function T(e,t){let r;t&&(r=await S.resolveConfig(t));try{return S.format(e,{plugins:[q],parser:"move-parse",printWidth:120,semi:!0,tabWidth:2,useTabs:!1,bracketSpacing:!0,...r})}catch(a){let s;return a instanceof Error?s=a.message:s=a,console.log(`Error during output formatting: ${s}`),e}}async function j(e){return S.format(e,{parser:"typescript"})}import h from"node:fs/promises";import G from"node:path";import R from"debug";var _=R("dubhe:common"),X=R("dubhe:common");_.log=console.debug.bind(console);X.log=console.error.bind(console);var b=_.extend("codegen"),Y=_.extend("codegen");b.log=console.debug.bind(console);Y.log=console.error.bind(console);async function c(e,t,r){let a=await T(e),s=` // 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]",n=s+a,i="#[allow(lint(share_owned), unused_let_mut)]";t.includes(".toml")||t.includes("system")||t.includes("migrate")?n=a:t.includes("init")?n=o+a:t.includes("deploy_hook")&&(n=i+a),await h.mkdir(G.dirname(t),{recursive:!0}),await h.writeFile(t,n),b(`${r}: ${t}`)}async function Me(e,t,r){let a=await j(e);await h.mkdir(G.dirname(t),{recursive:!0}),await h.writeFile(t,a),b(`${r}: ${t}`)}function je(e){return e.replace(/\\/g,"/")}import{existsSync as C}from"fs";import l from"fs";function y(e){l.existsSync(e)&&(l.readdirSync(e).forEach(t=>{let r=`${e}/${t}`;l.lstatSync(r).isDirectory()?y(r):l.unlinkSync(r)}),l.rmdirSync(e))}function g(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 E(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,201 +17,173 @@ 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(),s=r.charAt(0).toLowerCase();return a<s?-1:a>s?1:0})}function w(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
|
+
(${E(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 d(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}async function L(e,t,r){console.log(`
|
|
33
|
+
\u{1F4E6} Starting Schema Data Generation...`);for(let a of Object.keys(t)){let s=a,o=t[a];console.log(` \u2514\u2500 Generating ${s} ${Array.isArray(o)?"(enum)":"(struct)"}`);let n="",i=Object.keys(t).filter(p=>Array.isArray(t[p])).map(p=>p);if(console.log(i),Array.isArray(o)){let p=J(o);n=`module ${e}::${d(s)} {
|
|
34
|
+
public enum ${s} has copy, drop , store {
|
|
35
|
+
${p}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
${
|
|
39
|
-
${
|
|
40
|
-
}`).join("")}
|
|
38
|
+
${p.map(m=>`public fun new_${d(m)}(): ${s} {
|
|
39
|
+
${s}::${m}
|
|
40
|
+
}`).join("")}`}else n=`module ${e}::${d(s)} {
|
|
41
41
|
use std::ascii::String;
|
|
42
|
-
${i.map(
|
|
42
|
+
${i.map(p=>`use ${e}::${d(p)}::${p};`).join(`
|
|
43
43
|
`)}
|
|
44
44
|
|
|
45
|
-
public struct ${
|
|
46
|
-
${
|
|
45
|
+
public struct ${s} has copy, drop , store {
|
|
46
|
+
${u(o)}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
public fun new(${
|
|
50
|
-
${
|
|
51
|
-
${
|
|
49
|
+
public fun new(${u(o)}): ${s} {
|
|
50
|
+
${s} {
|
|
51
|
+
${g(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(s,o)}
|
|
56
|
+
${te(s,o)}
|
|
57
|
+
${N(s,o)}
|
|
58
|
+
${P(s,o)}
|
|
59
|
+
}`;await c(n,`${r}/contracts/${e}/sources/codegen/data/${d(s)}.move`,"formatAndWriteMove")}console.log(`\u2705 Schema Data Generation Complete
|
|
60
|
+
`)}function re(e,t){return t!=null?Object.keys(t).map(a=>`use ${e}::${d(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 s=`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,n])=>`public fun borrow_${o}(self: &Schema) : &${n} {
|
|
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 ${n} {
|
|
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,n])=>{let i="";return n.includes("StorageValue")?i=`storage_value::new(b"${o}", ctx)`:n.includes("StorageMap")?i=`storage_map::new(b"${o}", ctx)`:n.includes("StorageDoubleMap")&&(i=`storage_double_map::new(b"${o}", ctx)`),`storage::add_field<${n}>(&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,n])=>{let i=n.match(/<(.+)>/)[1].split(",").map(Z=>Z.trim()),p=[],m="",f="";return n.includes("StorageValue")?(p=[],m=`${i[0]}`,f="get()"):n.includes("StorageMap")?(p=[`key: ${i[0]}`],m=`${i[1]}`,f="get(key)"):n.includes("StorageDoubleMap")&&(p=[`key1: ${i[0]}`,`key2: ${i[1]}`],m=`${i[2]}`,f="get(key1, key2)"),`public fun get_${o}(self: &Schema, ${p}) : &${m} {
|
|
100
|
+
self.borrow_${o}().${f}
|
|
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(s,`${a}/contracts/${e}/sources/codegen/schema.move`,"formatAndWriteMove"),console.log(`\u2705 Schema Structure Generation Complete
|
|
105
|
+
`)}import{existsSync as O}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(n=>`_${n}: &mut ${w(n)}`).join(","),a=Object.keys(e.schemas).map(n=>`use ${e.name}::${n}_schema::${w(n)};`).join(`
|
|
107
|
+
`),s=`${t}/contracts/${e.name}/sources/scripts/deploy_hook.move`,o="";if(!O(s))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
|
|
124
|
+
}`;else{let n=oe(s);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
|
{
|
|
162
|
-
${
|
|
137
|
+
${n}
|
|
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,s,"formatAndWriteMove"),console.log(`\u2705 Deploy Hook Generation Complete
|
|
145
|
+
`)}async function U(e,t){if(!O(`${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
|
|
152
|
+
`;await c(r,`${t}/contracts/${e.name}/sources/scripts/migrate.move`,"formatAndWriteMove")}}function v(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}function se(e,t){return t!=null?Object.keys(t).map(a=>`use ${e}::${v(a)}::${a};`).join(`
|
|
153
|
+
`):""}async function D(e,t,r,a){console.log(`
|
|
154
|
+
\u{1F4E6} Starting Schema Event Generation...`);for(let s of Object.keys(r)){let o=s,n=r[s];console.log(` \u2514\u2500 Generating ${o} event: ${n}`);let i=`module ${e}::${v(o)}_event {
|
|
183
155
|
use sui::event;
|
|
184
156
|
use std::ascii::String;
|
|
185
|
-
${
|
|
157
|
+
${se(e,t)}
|
|
186
158
|
|
|
187
|
-
public struct ${
|
|
188
|
-
${
|
|
159
|
+
public struct ${o}Event has copy, drop {
|
|
160
|
+
${u(n)}
|
|
189
161
|
}
|
|
190
162
|
|
|
191
|
-
public fun new(${
|
|
192
|
-
${
|
|
193
|
-
${
|
|
163
|
+
public fun new(${u(n)}): ${o}Event {
|
|
164
|
+
${o}Event {
|
|
165
|
+
${g(n)}
|
|
194
166
|
}
|
|
195
167
|
}
|
|
196
168
|
|
|
197
|
-
public fun emit(${
|
|
198
|
-
event::emit(${
|
|
199
|
-
${
|
|
169
|
+
public fun emit(${u(n)}) {
|
|
170
|
+
event::emit(${o}Event {
|
|
171
|
+
${g(n)}
|
|
200
172
|
});
|
|
201
|
-
}`;await
|
|
202
|
-
`)}import{existsSync as
|
|
203
|
-
\u2699\uFE0F Starting System Generation...`),console.log(" \u251C\u2500 Generating systems"),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/systems`),
|
|
204
|
-
`)}function
|
|
205
|
-
\u{1F4E6} Starting Schema Error Generation...`);for(let a of Object.keys(t)){let
|
|
173
|
+
}`;await c(i,`${a}/contracts/${e}/sources/codegen/events/${v(o)}_event.move`,"formatAndWriteMove")}console.log(`\u2705 Schema Event Generation Complete
|
|
174
|
+
`)}import{existsSync as ne}from"fs";import ie from"node:fs/promises";async function I(e,t){console.log(`
|
|
175
|
+
\u2699\uFE0F Starting System Generation...`),console.log(" \u251C\u2500 Generating systems"),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/systems`),ne(`${t}/contracts/${e.name}/sources/systems`)||await ie.mkdir(`${t}/contracts/${e.name}/sources/systems`,{recursive:!0}),console.log(`\u2705 System Generation Complete
|
|
176
|
+
`)}function z(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}async function B(e,t,r){console.log(`
|
|
177
|
+
\u{1F4E6} Starting Schema Error Generation...`);for(let a of Object.keys(t)){let s=a,o=t[a];console.log(` \u2514\u2500 Generating ${s} message: ${o}`);let n=`module ${e}::${z(s)}_error {
|
|
206
178
|
#[error]
|
|
207
|
-
const ${
|
|
179
|
+
const ${s}: vector<u8> = b"${o}";
|
|
208
180
|
/// Get the error message.
|
|
209
|
-
public fun message(): vector<u8> { ${
|
|
181
|
+
public fun message(): vector<u8> { ${s} }
|
|
210
182
|
/// Abort execution with the given error code.
|
|
211
|
-
public fun emit() { abort ${
|
|
183
|
+
public fun emit() { abort ${s} }
|
|
212
184
|
/// Require that the given condition is true, otherwise abort with the given error code.
|
|
213
|
-
public fun require(condition: bool) { if (!condition) { emit() } }`;await
|
|
214
|
-
`)}import{existsSync as
|
|
185
|
+
public fun require(condition: bool) { if (!condition) { emit() } }`;await c(n,`${r}/contracts/${e}/sources/codegen/errors/${z(s)}_error.move`,"formatAndWriteMove")}console.log(`\u2705 Schema Error Generation Complete
|
|
186
|
+
`)}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
187
|
use std::ascii::String;
|
|
216
188
|
|
|
217
189
|
public struct DappMetadata has drop, copy, store {
|
|
@@ -307,125 +279,125 @@ ${e.name} = "0x0"
|
|
|
307
279
|
}
|
|
308
280
|
|
|
309
281
|
}
|
|
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
|
-
}
|
|
282
|
+
`;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 {
|
|
283
|
+
use ${e.name}::dapp_metadata::DappMetadata;
|
|
284
|
+
use dubhe::storage_value;
|
|
285
|
+
use dubhe::storage_value::StorageValue;
|
|
286
|
+
use dubhe::storage;
|
|
287
|
+
use sui::transfer::public_share_object;
|
|
288
|
+
use dubhe::type_info;
|
|
289
|
+
|
|
290
|
+
public struct Dapp has key, store {
|
|
291
|
+
id: UID,
|
|
292
|
+
}
|
|
339
293
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
294
|
+
public fun borrow_admin(self: &Dapp): &StorageValue<address> {
|
|
295
|
+
storage::borrow_field(&self.id, b"admin")
|
|
296
|
+
}
|
|
343
297
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
298
|
+
public(package) fun admin(self: &mut Dapp): &mut StorageValue<address> {
|
|
299
|
+
storage::borrow_mut_field(&mut self.id, b"admin")
|
|
300
|
+
}
|
|
347
301
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
302
|
+
public fun borrow_package_id(self: &Dapp): &StorageValue<address> {
|
|
303
|
+
storage::borrow_field(&self.id, b"package_id")
|
|
304
|
+
}
|
|
351
305
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
306
|
+
public(package) fun package_id(self: &mut Dapp): &mut StorageValue<address> {
|
|
307
|
+
storage::borrow_mut_field(&mut self.id, b"package_id")
|
|
308
|
+
}
|
|
355
309
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
310
|
+
public fun borrow_version(self: &Dapp): &StorageValue<u32> {
|
|
311
|
+
storage::borrow_field(&self.id, b"version")
|
|
312
|
+
}
|
|
359
313
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
314
|
+
public(package) fun version(self: &mut Dapp): &mut StorageValue<u32> {
|
|
315
|
+
storage::borrow_mut_field(&mut self.id, b"version")
|
|
316
|
+
}
|
|
363
317
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
318
|
+
public fun borrow_metadata(self: &Dapp): &StorageValue<DappMetadata> {
|
|
319
|
+
storage::borrow_field(&self.id, b"metadata")
|
|
320
|
+
}
|
|
367
321
|
|
|
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
|
-
}
|
|
322
|
+
public(package) fun metadata(self: &mut Dapp): &mut StorageValue<DappMetadata> {
|
|
323
|
+
storage::borrow_mut_field(&mut self.id, b"metadata")
|
|
324
|
+
}
|
|
378
325
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
326
|
+
public fun borrow_safe_mode(self: &Dapp): &StorageValue<bool> {
|
|
327
|
+
storage::borrow_field(&self.id, b"safe_mode")
|
|
328
|
+
}
|
|
382
329
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
330
|
+
public(package) fun safe_mode(self: &mut Dapp): &mut StorageValue<bool> {
|
|
331
|
+
storage::borrow_mut_field(&mut self.id, b"safe_mode")
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
public(package) fun borrow_schemas(self: &Dapp): &StorageValue<vector<address>> {
|
|
335
|
+
storage::borrow_field(&self.id, b"schemas")
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
public(package) fun schemas(self: &mut Dapp): &mut StorageValue<vector<address>> {
|
|
339
|
+
storage::borrow_mut_field(&mut self.id, b"schemas")
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
public(package) fun create(ctx: &mut TxContext): Dapp {
|
|
344
|
+
let mut id = object::new(ctx);
|
|
345
|
+
storage::add_field<StorageValue<address>>(&mut id, b"admin", storage_value::new(b"admin", ctx));
|
|
346
|
+
storage::add_field<StorageValue<address>>(&mut id, b"package_id", storage_value::new(b"package_id", ctx));
|
|
347
|
+
storage::add_field<StorageValue<u32>>(&mut id, b"version", storage_value::new(b"version", ctx));
|
|
348
|
+
storage::add_field<StorageValue<DappMetadata>>(&mut id, b"metadata", storage_value::new(b"metadata", ctx));
|
|
349
|
+
storage::add_field<StorageValue<bool>>(&mut id, b"safe_mode", storage_value::new(b"safe_mode", ctx));
|
|
350
|
+
storage::add_field<StorageValue<vector<address>>>(&mut id, b"schemas", storage_value::new(b"schemas", ctx));
|
|
351
|
+
Dapp { id }
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
public(package) fun upgrade<DappKey: drop>(dapp: &mut Dapp, ctx: &TxContext) {
|
|
355
|
+
assert!(dapp.borrow_metadata().contains(), 0);
|
|
356
|
+
assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
|
|
357
|
+
let new_package_id = type_info::current_package_id<DappKey>();
|
|
358
|
+
dapp.package_id().set(new_package_id);
|
|
359
|
+
let current_version = dapp.version()[];
|
|
360
|
+
dapp.version().set(current_version + 1);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
public(package) fun add_schema<Schema: key + store>(dapp: &mut Dapp, schema: Schema) {
|
|
364
|
+
let mut schemas = dapp.schemas()[];
|
|
365
|
+
schemas.push_back(object::id_address<Schema>(&schema));
|
|
366
|
+
dapp.schemas().set(schemas);
|
|
367
|
+
public_share_object(schema);
|
|
368
|
+
}
|
|
389
369
|
|
|
390
|
-
|
|
391
|
-
assert!(dapp.borrow_metadata().contains(), 0);
|
|
392
|
-
assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
|
|
370
|
+
#[test_only]
|
|
393
371
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
}
|
|
372
|
+
public fun create_dapp_for_testing(ctx: &mut TxContext): Dapp {
|
|
373
|
+
create(ctx)
|
|
374
|
+
}
|
|
398
375
|
|
|
399
|
-
|
|
400
|
-
public fun create_dapp_for_testing(ctx: &mut TxContext): Dapp {
|
|
401
|
-
create(ctx)
|
|
402
|
-
}
|
|
376
|
+
#[test_only]
|
|
403
377
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
378
|
+
public fun distroy_dapp_for_testing(dapp: Dapp) {
|
|
379
|
+
let Dapp { id } = dapp;
|
|
380
|
+
id.delete();
|
|
381
|
+
}
|
|
409
382
|
}
|
|
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(
|
|
383
|
+
`;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 {
|
|
384
|
+
use std::ascii::String;
|
|
385
|
+
use std::ascii;
|
|
386
|
+
use dubhe::type_info;
|
|
387
|
+
use sui::clock::Clock;
|
|
388
|
+
use ${e.name}::dapp_schema;
|
|
389
|
+
use ${e.name}::dapp_metadata;
|
|
390
|
+
use ${e.name}::dapp_schema::Dapp;
|
|
391
|
+
|
|
392
|
+
public struct DappKey has drop {}
|
|
393
|
+
public(package) fun new(): DappKey {
|
|
394
|
+
DappKey { }
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
public(package) fun create(name: String, description: String, clock: &Clock, ctx: &mut TxContext): Dapp {
|
|
398
|
+
let mut dapp = dapp_schema::create(ctx);
|
|
399
|
+
assert!(!dapp.borrow_metadata().contains(), 0);
|
|
400
|
+
dapp.metadata().set(
|
|
429
401
|
dapp_metadata::new(
|
|
430
402
|
name,
|
|
431
403
|
description,
|
|
@@ -435,20 +407,28 @@ ${e.name} = "0x0"
|
|
|
435
407
|
vector[]
|
|
436
408
|
)
|
|
437
409
|
);
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
410
|
+
let package_id = type_info::current_package_id<DappKey>();
|
|
411
|
+
dapp.package_id().set(package_id);
|
|
412
|
+
dapp.admin().set(ctx.sender());
|
|
413
|
+
dapp.version().set(1);
|
|
414
|
+
dapp.safe_mode().set(false);
|
|
415
|
+
dapp.schemas().set(vector[]);
|
|
416
|
+
dapp
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
public entry fun set_metadata(
|
|
420
|
+
dapp: &mut Dapp,
|
|
421
|
+
name: String,
|
|
422
|
+
description: String,
|
|
423
|
+
icon_url: String,
|
|
424
|
+
website_url: String,
|
|
425
|
+
partners: vector<String>,
|
|
426
|
+
ctx: &TxContext,
|
|
427
|
+
) {
|
|
428
|
+
let admin = dapp.admin().try_get();
|
|
429
|
+
assert!(admin == option::some(ctx.sender()), 0);
|
|
430
|
+
let created_at = dapp.metadata().remove().get_created_at();
|
|
431
|
+
dapp.metadata().set(
|
|
452
432
|
dapp_metadata::new(
|
|
453
433
|
name,
|
|
454
434
|
description,
|
|
@@ -458,37 +438,36 @@ ${e.name} = "0x0"
|
|
|
458
438
|
partners
|
|
459
439
|
)
|
|
460
440
|
);
|
|
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
|
-
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
public entry fun transfer_ownership(dapp: &mut Dapp, new_admin: address, ctx: &mut TxContext) {
|
|
444
|
+
let admin = dapp.admin().try_get();
|
|
445
|
+
assert!(admin == option::some(ctx.sender()), 0);
|
|
446
|
+
dapp.admin().set(new_admin);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
public entry fun set_safe_mode(dapp: &mut Dapp, safe_mode: bool, ctx: &TxContext) {
|
|
450
|
+
let admin = dapp.admin().try_get();
|
|
451
|
+
assert!(admin == option::some(ctx.sender()), 0);
|
|
452
|
+
dapp.safe_mode().set(safe_mode);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
public fun ensure_no_safe_mode(dapp: &Dapp) {
|
|
456
|
+
assert!(!dapp.borrow_safe_mode()[], 0);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
public fun ensure_has_authority(dapp: &Dapp, ctx: &TxContext) {
|
|
460
|
+
assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
public fun ensure_has_schema<Schema: key + store>(dapp: &Dapp, schema: &Schema) {
|
|
464
|
+
let schema_id = object::id_address(schema);
|
|
465
|
+
assert!(dapp.borrow_schemas().get().contains(&schema_id), 0);
|
|
466
|
+
}
|
|
489
467
|
}
|
|
490
468
|
|
|
491
|
-
|
|
469
|
+
|
|
470
|
+
`;await c(a,r,"formatAndWriteMove")}}async function K(e,t){console.log(`
|
|
492
471
|
\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
472
|
use ${e.name}::dapp_schema::Dapp;
|
|
494
473
|
use sui::clock;
|
|
@@ -506,8 +485,8 @@ ${e.name} = "0x0"
|
|
|
506
485
|
(scenario, dapp)
|
|
507
486
|
}
|
|
508
487
|
}
|
|
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
|
|
488
|
+
`;await c(r,`${t}/contracts/${e.name}/sources/tests/init.move`,"formatAndWriteMove"),console.log(`\u2705 Deploy Hook Generation Complete
|
|
489
|
+
`)}async function bt(e,t,r){console.log(`
|
|
490
|
+
\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}`)&&y(`${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 L(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 I(e,a),await U(e,a),console.log(`\u2705 Schema Generation Process Complete!
|
|
491
|
+
`)}import{findUp as me}from"find-up";import A from"path";import de from"esbuild";var $=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 $;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
492
|
//# sourceMappingURL=index.js.map
|