@0xobelisk/sui-common 1.1.8 → 1.1.9
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.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import v from"prettier";import
|
|
1
|
+
import v from"prettier";import X from"prettier-plugin-move-js";async function T(e,t){let r;t&&(r=await v.resolveConfig(t));try{return v.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 C(e){return v.format(e,{parser:"typescript"})}import $ from"node:fs/promises";import R from"node:path";import V from"debug";var b=V("dubhe:common"),Y=V("dubhe:common");b.log=console.debug.bind(console);Y.log=console.error.bind(console);var h=b.extend("codegen"),q=b.extend("codegen");h.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
|
-
`,s="#[test_only]",n
|
|
7
|
+
`,s="#[test_only]",o=n+a,i="#[allow(lint(share_owned))]";t.includes(".toml")||t.includes("system")||t.includes("migrate")?o=a:t.includes("init")?o=s+a:t.includes("genesis")&&(o=i+a),await $.mkdir(R.dirname(t),{recursive:!0}),await $.writeFile(t,o),h(`${r}: ${t}`)}async function Te(e,t,r){let a=await C(e);await $.mkdir(R.dirname(t),{recursive:!0}),await $.writeFile(t,a),h(`${r}: ${t}`)}function Ve(e){return e.replace(/\\/g,"/")}import{existsSync as k}from"fs";import _ from"fs";function x(e){_.existsSync(e)&&(_.readdirSync(e).forEach(t=>{let r=`${e}/${t}`;_.lstatSync(r).isDirectory()?x(r):_.unlinkSync(r)}),_.rmdirSync(e))}function g(e){return Object.entries(e).map(([t,r])=>`${t}`).join(",")}function E(e){return`(${Object.entries(e).map(([t,r])=>`${r}`)})`}function u(e){return Object.entries(e).map(([t,r])=>`${t}: ${r}`)}function U(e){return Object.entries(e).map(([t,r])=>`self.${t}`)}function W(e,t){for(let r in e)if(e.hasOwnProperty(r)){let a=e[r];if(typeof a=="string"&&a===t||typeof a=="string"&&a.includes(t)&&a.includes(">"))return!0}return!1}async function O(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,49 +17,49 @@ 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 N(e){return e.sort((t,r)=>{let a=t.charAt(0).toLowerCase(),
|
|
20
|
+
`;await c(r,`${t}/contracts/${e.name}/Move.toml`,"formatAndWriteMove"),console.log(`\u2705 Move.toml Generation Complete
|
|
21
|
+
`)}function N(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 ee(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
24
|
`)}function te(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 re(e,t){return`public fun get(self: &${e}): ${
|
|
28
|
-
(${
|
|
27
|
+
}`}function re(e,t){return`public fun get(self: &${e}): ${E(t)} {
|
|
28
|
+
(${U(t)})
|
|
29
29
|
}`}function ae(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 l(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}async function
|
|
33
|
-
\u{1F4E6} Starting Schema Data Generation...`);for(let a of Object.keys(t)){let
|
|
34
|
-
public enum ${
|
|
32
|
+
`)}function l(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}async function j(e,t,r){console.log(`
|
|
33
|
+
\u{1F4E6} Starting Schema Data Generation...`);for(let a of Object.keys(t)){let n=a,s=t[a];console.log(` \u2514\u2500 ${n} ${Array.isArray(s)?"(enum)":"(struct)"}: ${JSON.stringify(s)}`);let o="",i=Object.keys(t).filter(p=>Array.isArray(t[p])).map(p=>p);if(Array.isArray(s)){let p=N(s);o=`module ${e}::${e}_${l(n)} {
|
|
34
|
+
public enum ${n} has copy, drop , store {
|
|
35
35
|
${p}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
${p.map(m=>`public fun new_${l(m)}(): ${
|
|
39
|
-
${
|
|
40
|
-
}`).join("")}`}else
|
|
38
|
+
${p.map(m=>`public fun new_${l(m)}(): ${n} {
|
|
39
|
+
${n}::${m}
|
|
40
|
+
}`).join("")}`}else o=`module ${e}::${e}_${l(n)} {
|
|
41
41
|
use std::ascii::String;
|
|
42
42
|
|
|
43
|
-
${Object.keys(t).map(p=>{if(
|
|
43
|
+
${Object.keys(t).map(p=>{if(W(s,p))return`use ${e}::${e}_${l(p)}::${p};`}).filter(Boolean).join(`
|
|
44
44
|
`)}
|
|
45
45
|
|
|
46
|
-
public struct ${
|
|
46
|
+
public struct ${n} has copy, drop , store {
|
|
47
47
|
${u(s)}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
public fun new(${u(s)}): ${
|
|
51
|
-
${
|
|
52
|
-
${
|
|
50
|
+
public fun new(${u(s)}): ${n} {
|
|
51
|
+
${n} {
|
|
52
|
+
${g(s)}
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
${re(
|
|
57
|
-
${ae(
|
|
58
|
-
${ee(
|
|
59
|
-
${te(
|
|
60
|
-
}`;await
|
|
56
|
+
${re(n,s)}
|
|
57
|
+
${ae(n,s)}
|
|
58
|
+
${ee(n,s)}
|
|
59
|
+
${te(n,s)}
|
|
60
|
+
}`;await c(o,`${r}/contracts/${e}/sources/codegen/data/${l(n)}.move`,"formatAndWriteMove")}}function se(e,t){return t!=null?Object.keys(t).map(a=>`use ${e}::${e}_${l(a)}::${a};`).join(`
|
|
61
61
|
`):""}async function w(e,t,r,a){console.log(`
|
|
62
|
-
\u{1F528} Starting Schema Structure Generation...`),Object.entries(r).forEach(([s,
|
|
62
|
+
\u{1F528} Starting Schema Structure Generation...`),Object.entries(r).forEach(([s,o])=>{console.log(` \u2514\u2500 ${s}: ${o}`)});let n=`module ${e}::${e}_schema {
|
|
63
63
|
use std::ascii::String;
|
|
64
64
|
use std::ascii::string;
|
|
65
65
|
use sui::package::UpgradeCap;
|
|
@@ -69,16 +69,70 @@ ${e.name} = "0x0"
|
|
|
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
|
+
use ${e}::${e}_dapp_metadata::DappMetadata;
|
|
72
73
|
|
|
73
74
|
${se(e,t)}
|
|
74
75
|
|
|
75
76
|
public struct Schema has key, store { id: UID }
|
|
77
|
+
|
|
78
|
+
// Default storage
|
|
79
|
+
public fun borrow_dapp__admin(self: &Schema): &StorageValue<address> {
|
|
80
|
+
storage::borrow_field(&self.id, b"dapp__admin")
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public fun borrow_dapp__package_id(self: &Schema): &StorageValue<address> {
|
|
84
|
+
storage::borrow_field(&self.id, b"dapp__package_id")
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public fun borrow_dapp__version(self: &Schema): &StorageValue<u32> {
|
|
88
|
+
storage::borrow_field(&self.id, b"dapp__version")
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
public fun borrow_dapp__metadata(self: &Schema): &StorageValue<DappMetadata> {
|
|
92
|
+
storage::borrow_field(&self.id, b"dapp__metadata")
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public fun borrow_dapp__safe_mode(self: &Schema): &StorageValue<bool> {
|
|
96
|
+
storage::borrow_field(&self.id, b"dapp__safe_mode")
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public fun borrow_dapp__authorised_schemas(self: &Schema): &StorageValue<vector<address>> {
|
|
100
|
+
storage::borrow_field(&self.id, b"dapp__authorised_schemas")
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public fun borrow_dapp__schemas(self: &Schema): &StorageValue<vector<address>> {
|
|
104
|
+
storage::borrow_field(&self.id, b"dapp__schemas")
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public(package) fun dapp__admin(self: &mut Schema): &mut StorageValue<address> {
|
|
108
|
+
storage::borrow_mut_field(&mut self.id, b"dapp__admin")
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public(package) fun dapp__package_id(self: &mut Schema): &mut StorageValue<address> {
|
|
112
|
+
storage::borrow_mut_field(&mut self.id, b"dapp__package_id")
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public(package) fun dapp__version(self: &mut Schema): &mut StorageValue<u32> {
|
|
116
|
+
storage::borrow_mut_field(&mut self.id, b"dapp__version")
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public(package) fun dapp__metadata(self: &mut Schema): &mut StorageValue<DappMetadata> {
|
|
120
|
+
storage::borrow_mut_field(&mut self.id, b"dapp__metadata")
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
public(package) fun dapp__safe_mode(self: &mut Schema): &mut StorageValue<bool> {
|
|
124
|
+
storage::borrow_mut_field(&mut self.id, b"dapp__safe_mode")
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public(package) fun dapp__authorised_schemas(self: &mut Schema): &mut StorageValue<vector<address>> {
|
|
128
|
+
storage::borrow_mut_field(&mut self.id, b"dapp__authorised_schemas")
|
|
129
|
+
}
|
|
76
130
|
|
|
77
|
-
${Object.entries(r).map(([s,
|
|
131
|
+
${Object.entries(r).map(([s,o])=>`public fun borrow_${s}(self: &Schema) : &${o} {
|
|
78
132
|
storage::borrow_field(&self.id, b"${s}")
|
|
79
133
|
}
|
|
80
134
|
|
|
81
|
-
public(package) fun ${s}(self: &mut Schema): &mut ${
|
|
135
|
+
public(package) fun ${s}(self: &mut Schema): &mut ${o} {
|
|
82
136
|
storage::borrow_mut_field(&mut self.id, b"${s}")
|
|
83
137
|
}
|
|
84
138
|
`).join("")}
|
|
@@ -86,7 +140,7 @@ ${e.name} = "0x0"
|
|
|
86
140
|
|
|
87
141
|
public(package) fun create(ctx: &mut TxContext): Schema {
|
|
88
142
|
let mut id = object::new(ctx);
|
|
89
|
-
${Object.entries(r).map(([s,
|
|
143
|
+
${Object.entries(r).map(([s,o])=>{let i="";return o.includes("StorageValue")?i=`storage_value::new(b"${s}", ctx)`:o.includes("StorageMap")?i=`storage_map::new(b"${s}", ctx)`:o.includes("StorageDoubleMap")&&(i=`storage_double_map::new(b"${s}", ctx)`),`storage::add_field<${o}>(&mut id, b"${s}", ${i});`}).join(`
|
|
90
144
|
`)}
|
|
91
145
|
|
|
92
146
|
Schema { id }
|
|
@@ -104,365 +158,284 @@ ${e.name} = "0x0"
|
|
|
104
158
|
|
|
105
159
|
|
|
106
160
|
// ======================================== View Functions ========================================
|
|
107
|
-
${Object.entries(r).map(([s,
|
|
108
|
-
self.borrow_${s}().${
|
|
161
|
+
${Object.entries(r).map(([s,o])=>{let i=o.match(/<(.+)>/)[1].split(",").map(P=>P.trim()),p=[],m="",f="";return o.includes("StorageValue")?(p=[],m=`${i[0]}`,f="get()"):o.includes("StorageMap")?(p=[`key: ${i[0]}`],m=`${i[1]}`,f="get(key)"):o.includes("StorageDoubleMap")&&(p=[`key1: ${i[0]}`,`key2: ${i[1]}`],m=`${i[2]}`,f="get(key1, key2)"),`public fun get_${s}(self: &Schema, ${p}) : &${m} {
|
|
162
|
+
self.borrow_${s}().${f}
|
|
109
163
|
}`}).join(`
|
|
110
164
|
`)}
|
|
111
165
|
// =========================================================================================================
|
|
112
|
-
}`;await
|
|
166
|
+
}`;await c(n,`${a}/contracts/${e}/sources/codegen/core/schema.move`,"formatAndWriteMove")}import{existsSync as F}from"fs";async function L(e,t){let r=`${t}/contracts/${e.name}/sources/scripts/deploy_hook.move`;if(!F(r)){let a=`module ${e.name}::${e.name}_deploy_hook {
|
|
113
167
|
use ${e.name}::${e.name}_schema::Schema;
|
|
114
168
|
|
|
115
169
|
public(package) fun run(_schema: &mut Schema, _ctx: &mut TxContext) {
|
|
116
170
|
|
|
117
171
|
}
|
|
118
|
-
}`;await
|
|
172
|
+
}`;await c(a,r,"formatAndWriteMove")}}async function B(e,t){if(!F(`${t}/contracts/${e.name}/sources/scripts/migrate.move`)){let r=`module ${e.name}::${e.name}_migrate {
|
|
119
173
|
const ON_CHAIN_VERSION: u32 = 1;
|
|
120
174
|
|
|
121
175
|
public fun on_chain_version(): u32 {
|
|
122
176
|
ON_CHAIN_VERSION
|
|
123
177
|
}
|
|
124
178
|
}
|
|
125
|
-
`;await
|
|
179
|
+
`;await c(r,`${t}/contracts/${e.name}/sources/scripts/migrate.move`,"formatAndWriteMove")}}function d(e){return e.split("_").map(t=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join("")}function oe(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}function I(e,t){return t!=null?Object.keys(t).map(a=>`use ${e}::${e}_${oe(a)}::${a};`).join(`
|
|
126
180
|
`):""}async function D(e,t,r,a){console.log(`
|
|
127
|
-
\u{1F4E6} Starting Schema Event Generation...`);for(let s of Object.keys(r)){let
|
|
181
|
+
\u{1F4E6} Starting Schema Event Generation...`);for(let s of Object.keys(r)){let o=s,i=r[s];console.log(` \u2514\u2500 ${o} event: ${JSON.stringify(i)}`);let p=`module ${e}::${e}_${o}_event {
|
|
128
182
|
use sui::event;
|
|
129
183
|
use std::ascii::String;
|
|
130
|
-
${
|
|
184
|
+
${I(e,t)}
|
|
131
185
|
|
|
132
|
-
public struct ${d(
|
|
133
|
-
${u(
|
|
186
|
+
public struct ${d(o)}Event has copy, drop {
|
|
187
|
+
${u(i)}
|
|
134
188
|
}
|
|
135
189
|
|
|
136
|
-
public fun new(${u(
|
|
137
|
-
${d(
|
|
138
|
-
${
|
|
190
|
+
public fun new(${u(i)}): ${d(o)}Event {
|
|
191
|
+
${d(o)}Event {
|
|
192
|
+
${g(i)}
|
|
139
193
|
}
|
|
140
194
|
}
|
|
141
|
-
}`;await
|
|
195
|
+
}`;await c(p,`${a}/contracts/${e}/sources/codegen/data/${o}_event.move`,"formatAndWriteMove")}let n=`module ${e}::${e}_events {
|
|
142
196
|
use std::ascii::{String, string};
|
|
143
|
-
${
|
|
144
|
-
${Object.entries(r).map(([s,
|
|
197
|
+
${I(e,t)}
|
|
198
|
+
${Object.entries(r).map(([s,o])=>`
|
|
145
199
|
use ${e}::${e}_${s}_event::${d(s)}Event;
|
|
146
200
|
use ${e}::${e}_${s}_event;
|
|
147
|
-
public fun ${s}_event(${u(
|
|
201
|
+
public fun ${s}_event(${u(o)}) {
|
|
148
202
|
dubhe::storage_event::emit_set_record<${d(s)}Event, ${d(s)}Event, ${d(s)}Event>(
|
|
149
203
|
string(b"${s}_event"),
|
|
150
204
|
option::none(),
|
|
151
205
|
option::none(),
|
|
152
|
-
option::some(${e}_${s}_event::new(${
|
|
206
|
+
option::some(${e}_${s}_event::new(${g(o)}))
|
|
153
207
|
)
|
|
154
208
|
}
|
|
155
209
|
`).join(`
|
|
156
210
|
`)}
|
|
157
|
-
}`;await
|
|
211
|
+
}`;await c(n,`${a}/contracts/${e}/sources/codegen/events.move`,"formatAndWriteMove")}import{existsSync as ne}from"fs";import ie from"node:fs/promises";async function K(e,t){ne(`${t}/contracts/${e.name}/sources/systems`)||await ie.mkdir(`${t}/contracts/${e.name}/sources/systems`,{recursive:!0})}async function G(e,t,r){console.log(`
|
|
158
212
|
\u{1F4E6} Starting Schema Error Generation...`);let a=`module ${e}::${e}_errors {
|
|
159
|
-
${Object.entries(t).map(([
|
|
160
|
-
const ${
|
|
161
|
-
public fun ${
|
|
213
|
+
${Object.entries(t).map(([n,s])=>(console.log(` \u2514\u2500 ${n}: ${s}`),`#[error]
|
|
214
|
+
const ${n.toUpperCase()}: vector<u8> = b"${s}";
|
|
215
|
+
public fun ${n}_error(condition: bool) { assert!(condition, ${n.toUpperCase()}) }
|
|
162
216
|
`)).join(`
|
|
163
217
|
`)}
|
|
164
|
-
}`;await
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
218
|
+
}`;await c(a,`${r}/contracts/${e}/sources/codegen/errors.move`,"formatAndWriteMove")}import{existsSync as z}from"fs";async function H(e,t){await ce(e,t),await pe(e,t)}async function ce(e,t){let r=`${t}/contracts/${e.name}/sources/codegen/core/metadata.move`;if(!z(r)){let a=`module ${e.name}::${e.name}_dapp_metadata {
|
|
219
|
+
use std::ascii::String;
|
|
220
|
+
|
|
221
|
+
public struct DappMetadata has drop, copy, store {
|
|
222
|
+
name: String,
|
|
223
|
+
description: String,
|
|
224
|
+
cover_url: vector<String>,
|
|
225
|
+
website_url: String,
|
|
226
|
+
created_at: u64,
|
|
227
|
+
partners: vector<String>,
|
|
228
|
+
}
|
|
175
229
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
230
|
+
public fun new(
|
|
231
|
+
name: String,
|
|
232
|
+
description: String,
|
|
233
|
+
cover_url: vector<String>,
|
|
234
|
+
website_url: String,
|
|
235
|
+
created_at: u64,
|
|
236
|
+
partners: vector<String>,
|
|
237
|
+
): DappMetadata {
|
|
238
|
+
DappMetadata {
|
|
185
239
|
name,
|
|
186
240
|
description,
|
|
187
|
-
|
|
241
|
+
cover_url,
|
|
188
242
|
website_url,
|
|
189
243
|
created_at,
|
|
190
244
|
partners,
|
|
191
245
|
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
public fun set(
|
|
195
|
-
self: &mut DappMetadata,
|
|
196
|
-
name: String,
|
|
197
|
-
description: String,
|
|
198
|
-
icon_url: String,
|
|
199
|
-
website_url: String,
|
|
200
|
-
created_at: u64,
|
|
201
|
-
partners: vector<String>,
|
|
202
|
-
) {
|
|
203
|
-
self.name = name;
|
|
204
|
-
self.description = description;
|
|
205
|
-
self.icon_url = icon_url;
|
|
206
|
-
self.website_url = website_url;
|
|
207
|
-
self.created_at = created_at;
|
|
208
|
-
self.partners = partners;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
public fun set_name(self: &mut DappMetadata, name: String) {
|
|
212
|
-
self.name = name;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
public fun set_description(self: &mut DappMetadata, description: String) {
|
|
216
|
-
self.description = description;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
public fun set_icon_url(self: &mut DappMetadata, icon_url: String) {
|
|
220
|
-
self.icon_url = icon_url;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
public fun set_website_url(self: &mut DappMetadata, website_url: String) {
|
|
224
|
-
self.website_url = website_url;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
public fun set_created_at(self: &mut DappMetadata, created_at: u64) {
|
|
228
|
-
self.created_at = created_at;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
public fun set_partners(self: &mut DappMetadata, partners: vector<String>) {
|
|
232
|
-
self.partners = partners;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
public fun get_name(self: &DappMetadata): String {
|
|
236
|
-
self.name
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
public fun get_description(self: &DappMetadata): String {
|
|
240
|
-
self.description
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
public fun get_icon_url(self: &DappMetadata): String {
|
|
244
|
-
self.icon_url
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
public fun get_website_url(self: &DappMetadata): String {
|
|
248
|
-
self.website_url
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
public fun get_created_at(self: &DappMetadata): u64 {
|
|
252
|
-
self.created_at
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
public fun get_partners(self: &DappMetadata): vector<String> {
|
|
256
|
-
self.partners
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
`;await i(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}::${e.name}_dapp_schema {
|
|
261
|
-
use ${e.name}::${e.name}_dapp_metadata::DappMetadata;
|
|
262
|
-
use dubhe::storage_value;
|
|
263
|
-
use dubhe::storage_value::StorageValue;
|
|
264
|
-
use dubhe::storage;
|
|
265
|
-
use sui::transfer::public_share_object;
|
|
266
|
-
use dubhe::type_info;
|
|
267
|
-
|
|
268
|
-
public struct Dapp has key, store {
|
|
269
|
-
id: UID,
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
public fun borrow_admin(self: &Dapp): &StorageValue<address> {
|
|
273
|
-
storage::borrow_field(&self.id, b"admin")
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
public(package) fun admin(self: &mut Dapp): &mut StorageValue<address> {
|
|
277
|
-
storage::borrow_mut_field(&mut self.id, b"admin")
|
|
278
246
|
}
|
|
279
247
|
|
|
280
|
-
public fun
|
|
281
|
-
|
|
248
|
+
public fun set(
|
|
249
|
+
self: &mut DappMetadata,
|
|
250
|
+
name: String,
|
|
251
|
+
description: String,
|
|
252
|
+
cover_url: vector<String>,
|
|
253
|
+
website_url: String,
|
|
254
|
+
created_at: u64,
|
|
255
|
+
partners: vector<String>,
|
|
256
|
+
) {
|
|
257
|
+
self.name = name;
|
|
258
|
+
self.description = description;
|
|
259
|
+
self.cover_url = cover_url;
|
|
260
|
+
self.website_url = website_url;
|
|
261
|
+
self.created_at = created_at;
|
|
262
|
+
self.partners = partners;
|
|
282
263
|
}
|
|
283
264
|
|
|
284
|
-
public
|
|
285
|
-
|
|
265
|
+
public fun set_name(self: &mut DappMetadata, name: String) {
|
|
266
|
+
self.name = name;
|
|
286
267
|
}
|
|
287
268
|
|
|
288
|
-
public fun
|
|
289
|
-
|
|
269
|
+
public fun set_description(self: &mut DappMetadata, description: String) {
|
|
270
|
+
self.description = description;
|
|
290
271
|
}
|
|
291
272
|
|
|
292
|
-
public
|
|
293
|
-
|
|
273
|
+
public fun set_cover_url(self: &mut DappMetadata, cover_url: vector<String>) {
|
|
274
|
+
self.cover_url = cover_url;
|
|
294
275
|
}
|
|
295
276
|
|
|
296
|
-
public fun
|
|
297
|
-
|
|
277
|
+
public fun set_website_url(self: &mut DappMetadata, website_url: String) {
|
|
278
|
+
self.website_url = website_url;
|
|
298
279
|
}
|
|
299
280
|
|
|
300
|
-
public
|
|
301
|
-
|
|
281
|
+
public fun set_created_at(self: &mut DappMetadata, created_at: u64) {
|
|
282
|
+
self.created_at = created_at;
|
|
302
283
|
}
|
|
303
284
|
|
|
304
|
-
public fun
|
|
305
|
-
|
|
285
|
+
public fun set_partners(self: &mut DappMetadata, partners: vector<String>) {
|
|
286
|
+
self.partners = partners;
|
|
306
287
|
}
|
|
307
288
|
|
|
308
|
-
public
|
|
309
|
-
|
|
289
|
+
public fun get_name(self: &DappMetadata): String {
|
|
290
|
+
self.name
|
|
310
291
|
}
|
|
311
|
-
|
|
312
|
-
public(package) fun borrow_schemas(self: &Dapp): &StorageValue<vector<address>> {
|
|
313
|
-
storage::borrow_field(&self.id, b"schemas")
|
|
314
|
-
}
|
|
315
292
|
|
|
316
|
-
public
|
|
317
|
-
|
|
293
|
+
public fun get_description(self: &DappMetadata): String {
|
|
294
|
+
self.description
|
|
318
295
|
}
|
|
319
296
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
let mut id = object::new(ctx);
|
|
323
|
-
storage::add_field<StorageValue<address>>(&mut id, b"admin", storage_value::new(b"admin", ctx));
|
|
324
|
-
storage::add_field<StorageValue<address>>(&mut id, b"package_id", storage_value::new(b"package_id", ctx));
|
|
325
|
-
storage::add_field<StorageValue<u32>>(&mut id, b"version", storage_value::new(b"version", ctx));
|
|
326
|
-
storage::add_field<StorageValue<DappMetadata>>(&mut id, b"metadata", storage_value::new(b"metadata", ctx));
|
|
327
|
-
storage::add_field<StorageValue<bool>>(&mut id, b"safe_mode", storage_value::new(b"safe_mode", ctx));
|
|
328
|
-
storage::add_field<StorageValue<vector<address>>>(&mut id, b"schemas", storage_value::new(b"schemas", ctx));
|
|
329
|
-
Dapp { id }
|
|
297
|
+
public fun get_cover_url(self: &DappMetadata): vector<String> {
|
|
298
|
+
self.cover_url
|
|
330
299
|
}
|
|
331
300
|
|
|
332
|
-
public
|
|
333
|
-
|
|
334
|
-
assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
|
|
335
|
-
let new_package_id = type_info::current_package_id<DappKey>();
|
|
336
|
-
dapp.package_id().set(new_package_id);
|
|
337
|
-
let current_version = dapp.version()[];
|
|
338
|
-
dapp.version().set(current_version + 1);
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
public(package) fun add_schema<Schema: key + store>(dapp: &mut Dapp, schema: Schema) {
|
|
342
|
-
let mut schemas = dapp.schemas()[];
|
|
343
|
-
schemas.push_back(object::id_address<Schema>(&schema));
|
|
344
|
-
dapp.schemas().set(schemas);
|
|
345
|
-
public_share_object(schema);
|
|
301
|
+
public fun get_website_url(self: &DappMetadata): String {
|
|
302
|
+
self.website_url
|
|
346
303
|
}
|
|
347
304
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
public fun create_dapp_for_testing(ctx: &mut TxContext): Dapp {
|
|
351
|
-
create(ctx)
|
|
305
|
+
public fun get_created_at(self: &DappMetadata): u64 {
|
|
306
|
+
self.created_at
|
|
352
307
|
}
|
|
353
308
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
public fun distroy_dapp_for_testing(dapp: Dapp) {
|
|
357
|
-
let Dapp { id } = dapp;
|
|
358
|
-
id.delete();
|
|
309
|
+
public fun get_partners(self: &DappMetadata): vector<String> {
|
|
310
|
+
self.partners
|
|
359
311
|
}
|
|
360
312
|
}
|
|
361
|
-
`;await
|
|
313
|
+
`;await c(a,r,"formatAndWriteMove")}}async function pe(e,t){let r=`${t}/contracts/${e.name}/sources/codegen/core/system.move`;if(!z(r)){let a=`module ${e.name}::${e.name}_dapp_system {
|
|
362
314
|
use std::ascii::String;
|
|
363
315
|
use std::ascii;
|
|
364
316
|
use dubhe::type_info;
|
|
365
317
|
use sui::clock::Clock;
|
|
366
|
-
use
|
|
318
|
+
use sui::transfer::public_share_object;
|
|
319
|
+
use ${e.name}::${e.name}_schema::Schema;
|
|
367
320
|
use ${e.name}::${e.name}_dapp_metadata;
|
|
368
|
-
use ${e.name}::${e.name}
|
|
321
|
+
use ${e.name}::${e.name}_dapp_metadata::DappMetadata;
|
|
322
|
+
use dubhe::storage::add_field;
|
|
323
|
+
use dubhe::storage_value;
|
|
324
|
+
use dubhe::storage_value::StorageValue;
|
|
369
325
|
|
|
370
326
|
public struct DappKey has drop {}
|
|
371
327
|
public(package) fun new(): DappKey {
|
|
372
328
|
DappKey { }
|
|
373
329
|
}
|
|
374
330
|
|
|
375
|
-
public(package) fun create(name: String, description: String, clock: &Clock, ctx: &mut TxContext): Dapp {
|
|
376
|
-
let mut dapp = ${e.name}_dapp_schema::create(ctx);
|
|
377
|
-
assert!(!dapp.borrow_metadata().contains(), 0);
|
|
378
|
-
dapp.metadata().set(
|
|
379
|
-
${e.name}_dapp_metadata::new(
|
|
380
|
-
name,
|
|
381
|
-
description,
|
|
382
|
-
ascii::string(b""),
|
|
383
|
-
ascii::string(b""),
|
|
384
|
-
clock.timestamp_ms(),
|
|
385
|
-
vector[]
|
|
386
|
-
)
|
|
387
|
-
);
|
|
388
|
-
let package_id = type_info::current_package_id<DappKey>();
|
|
389
|
-
dapp.package_id().set(package_id);
|
|
390
|
-
dapp.admin().set(ctx.sender());
|
|
391
|
-
dapp.version().set(1);
|
|
392
|
-
dapp.safe_mode().set(false);
|
|
393
|
-
dapp.schemas().set(vector[]);
|
|
394
|
-
dapp
|
|
395
|
-
}
|
|
396
|
-
|
|
397
331
|
public entry fun set_metadata(
|
|
398
|
-
|
|
332
|
+
schema: &mut Schema,
|
|
399
333
|
name: String,
|
|
400
334
|
description: String,
|
|
401
|
-
|
|
335
|
+
cover_url: vector<String>,
|
|
402
336
|
website_url: String,
|
|
403
337
|
partners: vector<String>,
|
|
404
338
|
ctx: &TxContext,
|
|
405
339
|
) {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
let created_at =
|
|
409
|
-
|
|
340
|
+
let admin = schema.dapp__admin().try_get();
|
|
341
|
+
assert!(admin == option::some(ctx.sender()), 0);
|
|
342
|
+
let created_at = schema.dapp__metadata().get().get_created_at();
|
|
343
|
+
schema.dapp__metadata().set(
|
|
410
344
|
${e.name}_dapp_metadata::new(
|
|
411
345
|
name,
|
|
412
346
|
description,
|
|
413
|
-
|
|
347
|
+
cover_url,
|
|
414
348
|
website_url,
|
|
415
349
|
created_at,
|
|
416
350
|
partners
|
|
417
351
|
)
|
|
418
352
|
);
|
|
419
|
-
|
|
353
|
+
}
|
|
420
354
|
|
|
421
|
-
public entry fun transfer_ownership(dapp: &mut Dapp, new_admin: address, ctx: &mut TxContext) {
|
|
422
|
-
let admin = dapp.admin().try_get();
|
|
423
|
-
assert!(admin == option::some(ctx.sender()), 0);
|
|
424
|
-
dapp.admin().set(new_admin);
|
|
425
|
-
}
|
|
426
355
|
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
356
|
+
public entry fun transfer_ownership(schema: &mut Schema, new_admin: address, ctx: &mut TxContext) {
|
|
357
|
+
let admin = schema.dapp__admin().try_get();
|
|
358
|
+
assert!(admin == option::some(ctx.sender()), 0);
|
|
359
|
+
schema.dapp__admin().set(new_admin);
|
|
360
|
+
}
|
|
432
361
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
362
|
+
public entry fun set_safe_mode(schema: &mut Schema, safe_mode: bool, ctx: &TxContext) {
|
|
363
|
+
let admin = schema.dapp__admin().try_get();
|
|
364
|
+
assert!(admin == option::some(ctx.sender()), 0);
|
|
365
|
+
schema.dapp__safe_mode().set(safe_mode);
|
|
366
|
+
}
|
|
436
367
|
|
|
437
|
-
|
|
438
|
-
assert!(
|
|
439
|
-
|
|
368
|
+
public fun ensure_no_safe_mode(schema: &mut Schema) {
|
|
369
|
+
assert!(!schema.dapp__safe_mode()[], 0);
|
|
370
|
+
}
|
|
440
371
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
372
|
+
public fun ensure_has_authority(schema: &mut Schema, ctx: &TxContext) {
|
|
373
|
+
assert!(schema.dapp__admin().get() == ctx.sender(), 0);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
public fun ensure_has_schema<S: key + store>(schema: &mut Schema, new_schema: &S) {
|
|
377
|
+
let schema_id = object::id_address(new_schema);
|
|
378
|
+
assert!(schema.dapp__authorised_schemas().get().contains(&schema_id), 0);
|
|
379
|
+
}
|
|
446
380
|
|
|
381
|
+
public(package) fun create(schema: &mut Schema, name: String, description: String, clock: &Clock, ctx: &mut TxContext){
|
|
382
|
+
add_field<StorageValue<address>>(schema.id(), b"dapp__admin", storage_value::new(b"dapp__admin", ctx));
|
|
383
|
+
add_field<StorageValue<address>>(schema.id(), b"dapp__package_id", storage_value::new(b"dapp__package_id", ctx));
|
|
384
|
+
add_field<StorageValue<u32>>(schema.id(), b"dapp__version", storage_value::new(b"dapp__version", ctx));
|
|
385
|
+
add_field<StorageValue<DappMetadata>>(schema.id(), b"dapp__metadata", storage_value::new(b"dapp__metadata", ctx));
|
|
386
|
+
add_field<StorageValue<bool>>(schema.id(), b"dapp__safe_mode", storage_value::new(b"dapp__safe_mode", ctx));
|
|
387
|
+
add_field<StorageValue<vector<address>>>(schema.id(), b"dapp__authorised_schemas", storage_value::new(b"dapp__authorised_schemas", ctx));
|
|
388
|
+
schema.dapp__metadata().set(
|
|
389
|
+
${e.name}_dapp_metadata::new(
|
|
390
|
+
name,
|
|
391
|
+
description,
|
|
392
|
+
vector[],
|
|
393
|
+
ascii::string(b""),
|
|
394
|
+
clock.timestamp_ms(),
|
|
395
|
+
vector[]
|
|
396
|
+
)
|
|
397
|
+
);
|
|
398
|
+
let package_id = type_info::current_package_id<DappKey>();
|
|
399
|
+
schema.dapp__package_id().set(package_id);
|
|
400
|
+
schema.dapp__admin().set(ctx.sender());
|
|
401
|
+
schema.dapp__version().set(1);
|
|
402
|
+
schema.dapp__safe_mode().set(false);
|
|
403
|
+
schema.dapp__authorised_schemas().set(vector[]);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
public(package) fun upgrade<DappKey: drop>(schema: &mut Schema, ctx: &TxContext) {
|
|
407
|
+
assert!(schema.dapp__metadata().contains(), 0);
|
|
408
|
+
assert!(schema.dapp__admin().get() == ctx.sender(), 0);
|
|
409
|
+
let new_package_id = type_info::current_package_id<DappKey>();
|
|
410
|
+
schema.dapp__package_id().set(new_package_id);
|
|
411
|
+
let current_version = schema.dapp__version()[];
|
|
412
|
+
schema.dapp__version().set(current_version + 1);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
public(package) fun add_schema<S: key + store>(schema: &mut Schema, new_schema: S) {
|
|
416
|
+
let mut schemas = schema.dapp__authorised_schemas()[];
|
|
417
|
+
schemas.push_back(object::id_address<S>(&new_schema));
|
|
418
|
+
schema.dapp__authorised_schemas().set(schemas);
|
|
419
|
+
public_share_object(new_schema);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
447
422
|
|
|
448
|
-
`;await
|
|
449
|
-
use ${e.name}::${e.name}_dapp_schema::Dapp;
|
|
423
|
+
`;await c(a,r,"formatAndWriteMove")}}async function Q(e,t){let r=`module ${e.name}::${e.name}_init_test {
|
|
450
424
|
use sui::clock;
|
|
451
425
|
use sui::test_scenario;
|
|
452
426
|
use sui::test_scenario::Scenario;
|
|
453
427
|
|
|
454
|
-
public fun deploy_dapp_for_testing(sender: address):
|
|
428
|
+
public fun deploy_dapp_for_testing(sender: address): Scenario {
|
|
455
429
|
let mut scenario = test_scenario::begin(sender);
|
|
456
430
|
let ctx = test_scenario::ctx(&mut scenario);
|
|
457
431
|
let clock = clock::create_for_testing(ctx);
|
|
458
432
|
${e.name}::${e.name}_genesis::run(&clock, ctx);
|
|
459
433
|
clock::destroy_for_testing(clock);
|
|
460
434
|
test_scenario::next_tx(&mut scenario,sender);
|
|
461
|
-
|
|
462
|
-
(scenario, dapp)
|
|
435
|
+
scenario
|
|
463
436
|
}
|
|
464
437
|
}
|
|
465
|
-
`;await
|
|
438
|
+
`;await c(r,`${t}/contracts/${e.name}/sources/codegen/core/init_test.move`,"formatAndWriteMove");let a=`module ${e.name}::${e.name}_genesis {
|
|
466
439
|
use std::ascii::string;
|
|
467
440
|
|
|
468
441
|
use sui::clock::Clock;
|
|
@@ -470,19 +443,18 @@ use ${e}::${e}_${s}_event;
|
|
|
470
443
|
use ${e.name}::${e.name}_dapp_system;
|
|
471
444
|
|
|
472
445
|
public entry fun run(clock: &Clock, ctx: &mut TxContext) {
|
|
473
|
-
// Create a dapp.
|
|
474
|
-
let mut dapp = ${e.name}_dapp_system::create(string(b"${e.name}"),string(b"${e.description}"), clock , ctx);
|
|
475
446
|
// Create schemas
|
|
476
447
|
let mut schema = ${e.name}::${e.name}_schema::create(ctx);
|
|
448
|
+
// Setup default storage
|
|
449
|
+
${e.name}_dapp_system::create(&mut schema, string(b"${e.name}"),string(b"${e.description}"), clock , ctx);
|
|
477
450
|
// Logic that needs to be automated once the contract is deployed
|
|
478
451
|
${e.name}::${e.name}_deploy_hook::run(&mut schema, ctx);
|
|
479
452
|
// Authorize schemas and public share objects
|
|
480
|
-
|
|
481
|
-
sui::transfer::public_share_object(dapp);
|
|
453
|
+
sui::transfer::public_share_object(schema);
|
|
482
454
|
}
|
|
483
455
|
}
|
|
484
|
-
`;await
|
|
485
|
-
\u{1F680} Starting Schema Generation Process...`),console.log("\u{1F4CB} Project Configuration:"),console.log(` \u2514\u2500 Name: ${e.name}`),console.log(` \u2514\u2500 Description: ${e.description||"No description provided"}`),console.log(` \u2514\u2500 Network: ${r||"testnet"}`);let a=t??process.cwd();
|
|
456
|
+
`;await c(a,`${t}/contracts/${e.name}/sources/codegen/core/genesis.move`,"formatAndWriteMove")}async function gt(e,t,r){console.log(`
|
|
457
|
+
\u{1F680} Starting Schema Generation Process...`),console.log("\u{1F4CB} Project Configuration:"),console.log(` \u2514\u2500 Name: ${e.name}`),console.log(` \u2514\u2500 Description: ${e.description||"No description provided"}`),console.log(` \u2514\u2500 Network: ${r||"testnet"}`);let a=t??process.cwd();k(`${a}/contracts/${e.name}`)&&x(`${a}/contracts/${e.name}/sources/codegen`),k(`${a}/contracts/${e.name}/Move.toml`)||await O(e,a),k(`${a}/contracts/${e.name}/sources/script/deploy_hook.move`)||await L(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 j(e.name,e.data,a),await w(e.name,e.data,e.schemas,a)):await w(e.name,null,e.schemas,a),e.errors&&await G(e.name,e.errors,a),await H(e,a),await Q(e,a),await K(e,a),await B(e,a),console.log(`
|
|
486
458
|
\u2705 Schema Generation Process Complete!
|
|
487
|
-
`)}import{findUp as
|
|
459
|
+
`)}import{findUp as ue}from"find-up";import A from"path";import me from"esbuild";var S=class extends Error{name="NotInsideProjectError";message="You are not inside a Dubhe project"};import{rmSync as de}from"fs";import{pathToFileURL as le}from"url";import _e from"os";var ge=["dubhe.config.js","dubhe.config.mjs","dubhe.config.ts","dubhe.config.mts"],M="dubhe.config.example.mjs";async function Dt(e){e=await Z(e);try{return await me.build({entryPoints:[e],format:"esm",outfile:M,platform:"node",bundle:!0,packages:"external"}),e=await Z(M,!0),(await import(e+`?update=${Date.now()}`)).dubheConfig}finally{de(M,{force:!0})}}async function Z(e,t){return e===void 0?e=await fe():A.isAbsolute(e)||(e=A.join(process.cwd(),e),e=A.normalize(e)),t&&_e.platform()==="win32"?le(e).href:e}async function fe(){let e=await ue(ge);if(e===void 0)throw new S;return e}function Et(...e){if(e.length===1)return`StorageValue<${e[0]}>`;if(e.length===2)return`StorageMap<${e[0]}, ${e[1]}>`;if(e.length===3)return`StorageDoubleMap<${e[0]}, ${e[1]}, ${e[2]}>`;throw new Error("Invalid number of arguments for storage()")}var y=e=>{if(typeof e!="object"||e===null)return e;let t={};for(let r in e)if(e.hasOwnProperty(r)){let a=e[r];typeof a=="object"&&a!==null?Array.isArray(a)?t[r]=J(a):a.hasOwnProperty("variant")?t[r]={[a.variant]:{}}:a.hasOwnProperty("fields")?t[r]=y(a.fields):t[r]=y(a):t[r]=a}return t},J=e=>{let t=[];return e.forEach(r=>{typeof r=="object"&&r!==null?Array.isArray(r)?t.push(J(r)):r.hasOwnProperty("variant")?t.push({[r.variant]:{}}):r.hasOwnProperty("fields")?t.push(y(r.fields)):t.push(y(r)):t.push(r)}),t};var be=(r=>(r.Event="event",r.Schema="schema",r))(be||{});export{be as SubscriptionKind,c as formatAndWriteMove,Te as formatAndWriteTypescript,T as formatMove,C as formatTypescript,Dt as loadConfig,y as parseData,Ve as posixPath,Z as resolveConfigPath,gt as schemaGen,Et as storage};
|
|
488
460
|
//# sourceMappingURL=index.js.map
|