@0xobelisk/sui-common 0.5.18 → 0.5.20
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 +6 -4
- package/dist/index.js +191 -87
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/codegen/types/index.ts +8 -1
- package/src/codegen/utils/formatAndWrite.ts +1 -1
- package/src/codegen/utils/renderMove/generateEvent.ts +74 -0
- package/src/codegen/utils/renderMove/generateSchema.ts +82 -37
- package/src/codegen/utils/renderMove/generateSchemaHub.ts +65 -0
- package/src/codegen/utils/renderMove/generateScript.ts +49 -52
- package/src/codegen/utils/renderMove/generateSystem.ts +6 -14
- package/src/codegen/utils/renderMove/generateToml.ts +1 -1
- package/src/codegen/utils/renderMove/schemaGen.ts +13 -6
package/dist/index.d.ts
CHANGED
|
@@ -17,16 +17,17 @@ declare function formatAndWriteTypescript(output: string, fullOutputPath: string
|
|
|
17
17
|
*/
|
|
18
18
|
declare function posixPath(path: string): string;
|
|
19
19
|
|
|
20
|
-
type BaseType = "String" | "vector<String>" | "address" | "bool" | "u8" | "u64" | "u128" | "vector<address>" | "vector<bool>" | "vector<u8>" | "vector<vector<u8>>" | "vector<u64>" | "vector<u128>" | string;
|
|
20
|
+
type BaseType = "String" | "vector<String>" | "address" | "bool" | "u8" | "u32" | "u64" | "u128" | "vector<address>" | "vector<bool>" | "vector<u8>" | "vector<vector<u8>>" | "vector<u32>" | "vector<u64>" | "vector<u128>" | string;
|
|
21
21
|
type StorageDataType = "Struct" | "Enum";
|
|
22
22
|
type StorageMapType = "Map" | "Bag" | "Table";
|
|
23
23
|
type Address = string;
|
|
24
24
|
type Bool = boolean;
|
|
25
25
|
type U8 = number;
|
|
26
|
+
type U32 = number;
|
|
26
27
|
type U64 = number;
|
|
27
28
|
type U128 = number;
|
|
28
29
|
type Vector<T> = T[];
|
|
29
|
-
type BaseValueType = String | Address | Bool | U8 | U64 | U128 | Vector<Address> | Vector<Bool> | Vector<U8> | Vector<Vector<U8>> | Vector<U64> | Vector<U128>;
|
|
30
|
+
type BaseValueType = String | Address | Bool | U8 | U32 | U64 | U128 | Vector<Address> | Vector<Bool> | Vector<U8> | Vector<Vector<U8>> | Vector<U64> | Vector<U128>;
|
|
30
31
|
type SchemaData = {
|
|
31
32
|
name: string;
|
|
32
33
|
type?: StorageDataType;
|
|
@@ -35,14 +36,15 @@ type SchemaData = {
|
|
|
35
36
|
type SchemaType = {
|
|
36
37
|
data?: SchemaData[];
|
|
37
38
|
structure: Record<string, string>;
|
|
39
|
+
events?: SchemaData[];
|
|
38
40
|
};
|
|
39
41
|
type DubheConfig = {
|
|
40
42
|
name: string;
|
|
41
43
|
description: string;
|
|
42
|
-
systems: string[];
|
|
43
44
|
schemas: Record<string, SchemaType>;
|
|
45
|
+
migration_enabled: boolean;
|
|
44
46
|
};
|
|
45
|
-
type MoveType = "string" | "vector<string>" | "String" | "vector<String>" | "address" | "bool" | "u8" | "u64" | "u128" | "vector<address>" | "vector<bool>" | "vector<u8>" | "vector<vector<u8>>" | "vector<u64>" | "vector<u128>";
|
|
47
|
+
type MoveType = "string" | "vector<string>" | "String" | "vector<String>" | "address" | "bool" | "u8" | "u32" | "u64" | "u128" | "vector<address>" | "vector<bool>" | "vector<u8>" | "vector<vector<u8>>" | "vector<u32>" | "vector<u64>" | "vector<u128>";
|
|
46
48
|
|
|
47
49
|
declare function schemaGen(config: DubheConfig, srcPrefix?: string, network?: 'mainnet' | 'testnet' | 'devnet' | 'localnet', frameworkId?: string): Promise<void>;
|
|
48
50
|
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import w from"prettier";import X from"prettier-plugin-move-js";async function T(e,t){let r;t&&(r=await w.resolveConfig(t));try{return w.format(e,{plugins:[X],parser:"move-parse",printWidth:120,semi:!0,tabWidth:2,useTabs:!1,bracketSpacing:!0,...r})}catch(o){let s;return o instanceof Error?s=o.message:s=o,console.log(`Error during output formatting: ${s}`),e}}async function M(e){return w.format(e,{parser:"typescript"})}import S from"node:fs/promises";import W from"node:path";import j from"debug";var _=j("dubhe:common"),Y=j("dubhe:common");_.log=console.debug.bind(console);Y.log=console.error.bind(console);var y=_.extend("codegen"),q=_.extend("codegen");y.log=console.debug.bind(console);q.log=console.error.bind(console);async function m(e,t,r){let o=await T(e),s=` // Copyright (c) Obelisk Labs, Inc.
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
#[allow(unused_use)]
|
|
4
4
|
|
|
@@ -6,12 +6,7 @@ import y from"prettier";import K from"prettier-plugin-move-js";async function w(
|
|
|
6
6
|
|
|
7
7
|
`,n=`#[allow(lint(share_owned))]
|
|
8
8
|
|
|
9
|
-
`,
|
|
10
|
-
\u2699\uFE0F Starting System Generation...`),e.systems.map(async r=>{if(console.log(` \u251C\u2500 Generating system: ${r}`),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/system/${r}.move`),!L(`${t}/contracts/${e.name}/sources/system/${r}.move`)){let o=`module ${e.name}::${r}_system {
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
`;await c(o,`${t}/contracts/${e.name}/sources/system/${r}.move`,"formatAndWriteMove")}}),console.log(`\u2705 System Generation Complete
|
|
14
|
-
`)}async function T(e,t,r){console.log(`
|
|
9
|
+
`,p=s+o;t.includes(".toml")||t.includes("system")||t.includes("migrate")?p=o:t.includes("deploy_hook")&&(p=n+o),await S.mkdir(W.dirname(t),{recursive:!0}),await S.writeFile(t,p),y(`${r}: ${t}`)}async function ve(e,t,r){let o=await M(e);await S.mkdir(W.dirname(t),{recursive:!0}),await S.writeFile(t,o),y(`${r}: ${t}`)}function ke(e){return e.replace(/\\/g,"/")}import{existsSync as A}from"fs";import g from"fs";function k(e){g.existsSync(e)&&(g.readdirSync(e).forEach(t=>{let r=`${e}/${t}`;g.lstatSync(r).isDirectory()?k(r):g.unlinkSync(r)}),g.rmdirSync(e))}function f(e){return Object.entries(e).map(([t,r])=>`${t}`).join(",")}function G(e){return`(${Object.entries(e).map(([t,r])=>`${r}`)})`}function l(e){return Object.entries(e).map(([t,r])=>`${t}: ${r}`)}function R(e){return Object.entries(e).map(([t,r])=>`self.${t}`)}async function H(e,t,r){console.log(`
|
|
15
10
|
\u{1F4C4} Starting Move.toml Generation...`),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/Move.toml`);let o=`[package]
|
|
16
11
|
name = "${e.name}"
|
|
17
12
|
version = "0.0.1"
|
|
@@ -19,148 +14,189 @@ edition = "2024.beta"
|
|
|
19
14
|
|
|
20
15
|
[dependencies]
|
|
21
16
|
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.36.2" }
|
|
22
|
-
Dubhe = { git = "https://github.com/0xobelisk/dubhe.git",
|
|
17
|
+
Dubhe = { git = "https://github.com/0xobelisk/dubhe-framework.git", rev = "release-dubhe-v1.0.0-rc1" }
|
|
23
18
|
|
|
24
19
|
[addresses]
|
|
25
20
|
sui = "0x2"
|
|
26
21
|
dubhe = "${r}"
|
|
27
22
|
${e.name} = "0x0"
|
|
28
|
-
`;await
|
|
29
|
-
`)}function
|
|
23
|
+
`;await m(o,`${t}/contracts/${e.name}/Move.toml`,"formatAndWriteMove"),console.log(`\u2705 Move.toml Generation Complete
|
|
24
|
+
`)}function u(e){return e.split("_").map((t,r)=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join("")}function J(e,t){return Object.entries(t).map(([r,o])=>`public(package) fun set_${r}(self: &mut ${e}, ${r}: ${o}) {
|
|
30
25
|
self.${r} = ${r};
|
|
31
26
|
}`).join(`
|
|
32
|
-
`)}function
|
|
27
|
+
`)}function P(e,t){return`public(package) fun set(self: &mut ${e}, ${l(t)}) {
|
|
33
28
|
${Object.entries(t).map(([r])=>`self.${r} = ${r};`).join(`
|
|
34
29
|
`)}
|
|
35
|
-
}`}function
|
|
36
|
-
(${
|
|
37
|
-
}`}function
|
|
30
|
+
}`}function ee(e,t){return`public fun get(self: &${e}): ${G(t)} {
|
|
31
|
+
(${R(t)})
|
|
32
|
+
}`}function te(e,t){return Object.entries(t).map(([r,o])=>`public fun get_${r}(self: &${e}): ${o} {
|
|
38
33
|
self.${r}
|
|
39
34
|
}`).join(`
|
|
40
|
-
`)}function
|
|
41
|
-
\u{1F4E6} Starting Schema Data Generation...`);for(let o in t){let s=t[o];if(s.data){console.log(` \u251C\u2500 Processing schema: ${o}`);for(let n of s.data){console.log(` \u2514\u2500 Generating ${n.name} ${Array.isArray(n.fields)?"(enum)":"(struct)"}`);let
|
|
35
|
+
`)}function d(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}async function O(e,t,r){console.log(`
|
|
36
|
+
\u{1F4E6} Starting Schema Data Generation...`);for(let o in t){let s=t[o];if(s.data){console.log(` \u251C\u2500 Processing schema: ${o}`);for(let n of s.data){console.log(` \u2514\u2500 Generating ${n.name} ${Array.isArray(n.fields)?"(enum)":"(struct)"}`);let p="",i=s.data.filter(a=>Array.isArray(a.fields)).map(a=>a.name);Array.isArray(n.fields)?p=`module ${e}::${o}_${d(n.name)} {
|
|
42
37
|
public enum ${n.name} has copy, drop , store {
|
|
43
38
|
${n.fields}
|
|
44
39
|
}
|
|
45
40
|
|
|
46
|
-
${n.fields.map(
|
|
47
|
-
${n.name}::${
|
|
48
|
-
}`).join("")}`:
|
|
41
|
+
${n.fields.map(a=>`public fun new_${d(a)}(): ${n.name} {
|
|
42
|
+
${n.name}::${a}
|
|
43
|
+
}`).join("")}`:p=`module ${e}::${o}_${d(n.name)} {
|
|
49
44
|
use std::ascii::String;
|
|
50
|
-
${
|
|
45
|
+
${i.map(a=>`use ${e}::${o}_${d(a)}::${a};`).join(`
|
|
51
46
|
`)}
|
|
52
47
|
|
|
53
48
|
public struct ${n.name} has copy, drop , store {
|
|
54
|
-
${
|
|
49
|
+
${l(n.fields)}
|
|
55
50
|
}
|
|
56
51
|
|
|
57
|
-
public fun new(${
|
|
52
|
+
public fun new(${l(n.fields)}): ${n.name} {
|
|
58
53
|
${n.name} {
|
|
59
|
-
${
|
|
54
|
+
${f(n.fields)}
|
|
60
55
|
}
|
|
61
56
|
}
|
|
62
57
|
|
|
63
|
-
${
|
|
64
|
-
${
|
|
65
|
-
${
|
|
66
|
-
${
|
|
67
|
-
}`,await
|
|
68
|
-
`)}function
|
|
69
|
-
`):""}async function
|
|
70
|
-
\u{1F528} Starting Schema Structure Generation...`);for(let
|
|
58
|
+
${ee(n.name,n.fields)}
|
|
59
|
+
${te(n.name,n.fields)}
|
|
60
|
+
${J(n.name,n.fields)}
|
|
61
|
+
${P(n.name,n.fields)}
|
|
62
|
+
}`,await m(p,`${r}/contracts/${e}/sources/codegen/schemas/${o}_${d(n.name)}.move`,"formatAndWriteMove")}}}console.log(`\u2705 Schema Data Generation Complete
|
|
63
|
+
`)}function re(e,t,r){return r.data?r.data.map(o=>`use ${e}::${t}_${d(o.name)}::${o.name};`).join(`
|
|
64
|
+
`):""}async function z(e,t,r,o){console.log(`
|
|
65
|
+
\u{1F528} Starting Schema Structure Generation...`);for(let s in t){console.log(` \u251C\u2500 Generating schema: ${s}`),console.log(` \u251C\u2500 Output path: ${r}/contracts/${e}/sources/codegen/schemas/${s}.move`),console.log(` \u2514\u2500 Structure fields: ${Object.keys(t[s].structure).length}`);let n=t[s],p=`module ${e}::${s}_schema {
|
|
71
66
|
use std::ascii::String;
|
|
67
|
+
use std::ascii::string;
|
|
68
|
+
use sui::package::UpgradeCap;
|
|
72
69
|
use std::type_name;
|
|
73
70
|
use dubhe::dapps_system;
|
|
71
|
+
use dubhe::storage_migrate;
|
|
74
72
|
use dubhe::dapps_schema::Dapps;
|
|
75
73
|
use dubhe::storage_value::{Self, StorageValue};
|
|
76
74
|
use dubhe::storage_map::{Self, StorageMap};
|
|
77
75
|
use dubhe::storage_double_map::{Self, StorageDoubleMap};
|
|
78
76
|
use ${e}::dapp_key::DappKey;
|
|
79
|
-
|
|
77
|
+
use sui::dynamic_field as df;
|
|
78
|
+
${re(e,s,n)}
|
|
80
79
|
|
|
81
|
-
public struct ${
|
|
82
|
-
id: UID
|
|
83
|
-
|
|
84
|
-
}
|
|
80
|
+
public struct ${u(s)} has key, store {
|
|
81
|
+
id: UID
|
|
82
|
+
}
|
|
85
83
|
|
|
86
|
-
${Object.entries(
|
|
87
|
-
&self
|
|
84
|
+
${Object.entries(n.structure).map(([i,a])=>`public fun borrow_${i}(self: &${u(s)}) : &${a} {
|
|
85
|
+
storage_migrate::borrow_field(&self.id, b"${i}")
|
|
88
86
|
}
|
|
89
87
|
|
|
90
|
-
public(package) fun borrow_mut_${
|
|
91
|
-
&mut self
|
|
88
|
+
public(package) fun borrow_mut_${i}(self: &mut ${u(s)}): &mut ${a} {
|
|
89
|
+
storage_migrate::borrow_mut_field(&mut self.id, b"${i}")
|
|
92
90
|
}
|
|
93
91
|
`).join("")}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
dapps_system::add_schema<${p(o)}>(dapps, package_id, ctx);
|
|
102
|
-
${p(o)} {
|
|
103
|
-
id: object::new(ctx),
|
|
104
|
-
${Object.entries(s.structure).map(([a,m])=>{let i="";return m.includes("StorageValue")?i="storage_value::new()":m.includes("StorageMap")?i="storage_map::new()":m.includes("StorageDoubleMap")&&(i="storage_double_map::new()"),`${a}: ${i},`}).join(" ")}
|
|
105
|
-
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
public(package) fun create(ctx: &mut TxContext): ${u(s)} {
|
|
95
|
+
let mut id = object::new(ctx);
|
|
96
|
+
${Object.entries(n.structure).map(([i,a])=>{let c="";return a.includes("StorageValue")?c="storage_value::new()":a.includes("StorageMap")?c="storage_map::new()":a.includes("StorageDoubleMap")&&(c="storage_double_map::new()"),`storage_migrate::add_field<${a}>(&mut id, b"${i}", ${c});`}).join("")}
|
|
97
|
+
|
|
98
|
+
${u(s)} { id }
|
|
106
99
|
}
|
|
100
|
+
|
|
101
|
+
${o?`public fun migrate(_${s}: &mut ${u(s)}, _cap: &UpgradeCap) { }`:""}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
// ======================================== View Functions ========================================
|
|
105
|
+
${Object.entries(n.structure).map(([i,a])=>{let c=a.match(/<(.+)>/)[1].split(",").map(Z=>Z.trim()),$=[],h="",b="",v="";return a.includes("StorageValue")?($=[],h=`${c[0]}`,b="try_get()"):a.includes("StorageMap")?($=[`key: ${c[0]}`],h=`${c[1]}`,b="try_get(key)",v=`public fun get_${i}_keys(self: &${u(s)}) : vector<${c[0]}> {
|
|
106
|
+
self.borrow_${i}().keys()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public fun get_${i}_values(self: &${u(s)}) : vector<${c[1]}> {
|
|
110
|
+
self.borrow_${i}().values()
|
|
111
|
+
}`):a.includes("StorageDoubleMap")&&($=[`key1: ${c[0]}`,`key2: ${c[1]}`],h=`${c[2]}`,b="try_get(key1, key2)",v=`public fun get_${i}_keys(self: &${u(s)}) : (vector<${c[0]}>, vector<${c[1]}>) {
|
|
112
|
+
self.borrow_${i}().keys()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public fun get_${i}_values(self: &${u(s)}) : vector<${c[2]}> {
|
|
116
|
+
self.borrow_${i}().values()
|
|
117
|
+
}`),`public fun get_${i}(self: &${u(s)}, ${$}) : Option<${h}> {
|
|
118
|
+
self.borrow_${i}().${b}
|
|
119
|
+
}
|
|
120
|
+
`+v}).join("")}
|
|
121
|
+
// =========================================================================================================
|
|
122
|
+
|
|
107
123
|
|
|
108
|
-
}`;await
|
|
109
|
-
`)}import{existsSync as
|
|
110
|
-
\u{1F4DD} Starting Deploy Hook Generation...`),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/script/deploy_hook.move`),!
|
|
124
|
+
}`;await m(p,`${r}/contracts/${e}/sources/codegen/schemas/${s}.move`,"formatAndWriteMove")}console.log(`\u2705 Schema Structure Generation Complete
|
|
125
|
+
`)}import{existsSync as E}from"fs";async function U(e,t){if(console.log(`
|
|
126
|
+
\u{1F4DD} Starting Deploy Hook Generation...`),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/script/deploy_hook.move`),!E(`${t}/contracts/${e.name}/sources/script/deploy_hook.move`)){let r=`module ${e.name}::deploy_hook {
|
|
111
127
|
use dubhe::dapps_schema::Dapps;
|
|
112
128
|
use dubhe::dapps_system;
|
|
113
|
-
use ${e.name}::
|
|
114
|
-
use std::ascii;
|
|
129
|
+
use ${e.name}::schema_hub::SchemaHub;
|
|
130
|
+
use std::ascii::string;
|
|
115
131
|
use sui::clock::Clock;
|
|
132
|
+
use sui::package::UpgradeCap;
|
|
116
133
|
use sui::transfer::public_share_object;
|
|
117
|
-
|
|
118
|
-
|
|
134
|
+
${Object.keys(e.schemas).map(o=>`use ${e.name}::${o}_schema::${u(o)};`).join(`
|
|
135
|
+
`)}
|
|
119
136
|
#[test_only]
|
|
120
137
|
use sui::clock;
|
|
121
138
|
#[test_only]
|
|
122
139
|
use sui::test_scenario;
|
|
123
140
|
#[test_only]
|
|
141
|
+
use sui::package;
|
|
142
|
+
#[test_only]
|
|
143
|
+
use ${e.name}::schema_hub;
|
|
144
|
+
#[test_only]
|
|
145
|
+
use dubhe::dapps_schema;
|
|
146
|
+
#[test_only]
|
|
124
147
|
use sui::test_scenario::Scenario;
|
|
125
148
|
|
|
126
|
-
public entry fun run(dapps: &mut Dapps, clock: &Clock, ctx: &mut TxContext) {
|
|
149
|
+
public entry fun run(schema_hub: &mut SchemaHub, dapps: &mut Dapps, cap: &UpgradeCap, clock: &Clock, ctx: &mut TxContext) {
|
|
127
150
|
// Register the dapp to dubhe.
|
|
128
|
-
dapps_system::register
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
ascii::string(b"${e.description}"),
|
|
132
|
-
clock,
|
|
133
|
-
ctx
|
|
134
|
-
);
|
|
135
|
-
${Object.keys(e.schemas).map(o=>`let ${o} = ${e.name}::${o}_schema::register(dapps, ctx);`).join(`
|
|
151
|
+
dapps_system::register(dapps,cap,string(b"${e.name}"),string(b"${e.description}"),clock,ctx);
|
|
152
|
+
// Create schemas
|
|
153
|
+
${Object.keys(e.schemas).map(o=>`let ${o} = ${e.name}::${o}_schema::create(ctx);`).join(`
|
|
136
154
|
`)}
|
|
137
155
|
// Logic that needs to be automated once the contract is deployed
|
|
156
|
+
|
|
138
157
|
|
|
158
|
+
|
|
139
159
|
|
|
140
|
-
|
|
141
|
-
|
|
160
|
+
|
|
161
|
+
// Authorize schemas and public share objects
|
|
162
|
+
${Object.keys(e.schemas).map(o=>`
|
|
163
|
+
schema_hub.authorize_schema<${u(o)}>();
|
|
164
|
+
public_share_object(${o});
|
|
165
|
+
`).join(`
|
|
142
166
|
`)}
|
|
143
167
|
}
|
|
144
168
|
|
|
145
169
|
#[test_only]
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
170
|
+
public fun deploy_hook_for_testing(): (Scenario, SchemaHub, Dapps) {
|
|
171
|
+
let mut scenario = test_scenario::begin(@0xA);
|
|
172
|
+
{
|
|
149
173
|
let ctx = test_scenario::ctx(&mut scenario);
|
|
150
174
|
dapps_schema::init_dapps_for_testing(ctx);
|
|
175
|
+
schema_hub::init_schema_hub_for_testing(ctx);
|
|
151
176
|
test_scenario::next_tx(&mut scenario,@0xA);
|
|
152
177
|
};
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
178
|
+
let mut dapps = test_scenario::take_shared<Dapps>(&scenario);
|
|
179
|
+
let mut schema_hub = test_scenario::take_shared<SchemaHub>(&scenario);
|
|
180
|
+
let ctx = test_scenario::ctx(&mut scenario);
|
|
181
|
+
let clock = clock::create_for_testing(ctx);
|
|
182
|
+
let upgrade_cap = package::test_publish(@0x42.to_id(), ctx);
|
|
183
|
+
run(&mut schema_hub, &mut dapps, &upgrade_cap, &clock, ctx);
|
|
184
|
+
|
|
185
|
+
clock::destroy_for_testing(clock);
|
|
186
|
+
upgrade_cap.make_immutable();
|
|
187
|
+
test_scenario::next_tx(&mut scenario,@0xA);
|
|
188
|
+
(scenario, schema_hub, dapps)
|
|
160
189
|
}
|
|
161
190
|
}
|
|
162
|
-
`;await
|
|
163
|
-
`)}async function
|
|
191
|
+
`;await m(r,`${t}/contracts/${e.name}/sources/script/deploy_hook.move`,"formatAndWriteMove")}console.log(`\u2705 Deploy Hook Generation Complete
|
|
192
|
+
`)}async function F(e,t){if(!E(`${t}/contracts/${e.name}/sources/script/migrate.move`)){let r=`module ${e.name}::migrate {
|
|
193
|
+
const ON_CHAIN_VERSION: u32 = 0;
|
|
194
|
+
|
|
195
|
+
public fun on_chain_version(): u32 {
|
|
196
|
+
ON_CHAIN_VERSION
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
`;await m(r,`${t}/contracts/${e.name}/sources/script/migrate.move`,"formatAndWriteMove")}}async function L(e,t){console.log(`
|
|
164
200
|
\u{1F511} Starting DappKey Generation...`),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/codegen/dapp_key.move`);let r=`module ${e.name}::dapp_key {
|
|
165
201
|
/// Authorization token for the app.
|
|
166
202
|
public struct DappKey has drop {}
|
|
@@ -169,9 +205,77 @@ ${e.name} = "0x0"
|
|
|
169
205
|
DappKey { }
|
|
170
206
|
}
|
|
171
207
|
}
|
|
172
|
-
`;await
|
|
173
|
-
`)}function
|
|
174
|
-
\u{
|
|
175
|
-
|
|
176
|
-
|
|
208
|
+
`;await m(r,`${t}/contracts/${e.name}/sources/codegen/dapp_key.move`,"formatAndWriteMove"),console.log(`\u2705 DappKey Generation Complete
|
|
209
|
+
`)}function I(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}async function K(e,t,r){console.log(`
|
|
210
|
+
\u{1F4E6} Starting Schema Event Generation...`);for(let o in t){let s=t[o];if(s.events){console.log(` \u251C\u2500 Processing schema: ${o}`);for(let n of s.events){console.log(` \u2514\u2500 Generating ${n.name} ${Array.isArray(n.fields)?"(enum)":"(struct)"}`);let p=`module ${e}::${o}_${I(n.name)}_event {
|
|
211
|
+
use sui::event;
|
|
212
|
+
use std::ascii::String;
|
|
213
|
+
public struct ${n.name}Event has copy, drop {
|
|
214
|
+
${l(n.fields)}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public fun new(${l(n.fields)}): ${n.name}Event {
|
|
218
|
+
${n.name}Event {
|
|
219
|
+
${f(n.fields)}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
public fun emit(${l(n.fields)}) {
|
|
224
|
+
event::emit(${n.name}Event {
|
|
225
|
+
${f(n.fields)}
|
|
226
|
+
});
|
|
227
|
+
}`;await m(p,`${r}/contracts/${e}/sources/codegen/events/${o}_${I(n.name)}_event.move`,"formatAndWriteMove")}}}console.log(`\u2705 Schema Event Generation Complete
|
|
228
|
+
`)}import{existsSync as oe}from"fs";import se from"node:fs/promises";async function B(e,t){console.log(`
|
|
229
|
+
\u2699\uFE0F Starting System Generation...`),console.log(" \u251C\u2500 Generating systems"),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/systems`),oe(`${t}/contracts/${e.name}/sources/systems`)||await se.mkdir(`${t}/contracts/${e.name}/sources/systems`,{recursive:!0}),console.log(`\u2705 System Generation Complete
|
|
230
|
+
`)}async function V(e,t){console.log(`
|
|
231
|
+
\u{1F511} Starting DappKey Generation...`),console.log(` \u2514\u2500 Output path: ${t}/contracts/${e.name}/sources/codegen/schema_hub.move`);let r=`module ${e.name}::schema_hub {
|
|
232
|
+
use sui::transfer::public_share_object;
|
|
233
|
+
use sui::dynamic_field as df;
|
|
234
|
+
|
|
235
|
+
public struct SchemaHub has key, store {
|
|
236
|
+
id: UID,
|
|
237
|
+
admin: address,
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
public struct SchemaTypeWapper<phantom Schema: key + store> has copy, store, drop {}
|
|
241
|
+
|
|
242
|
+
/// Authorize an schema to access protected features of the SchemaHub.
|
|
243
|
+
public(package) fun authorize_schema<Schema: key + store>(self: &mut SchemaHub) {
|
|
244
|
+
df::add(&mut self.id, SchemaTypeWapper<Schema> {}, true);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/// Deauthorize an schema by removing its authorization key.
|
|
248
|
+
public(package) fun deauthorize_schema<Schema: key + store>(self: &mut SchemaHub) {
|
|
249
|
+
df::remove<SchemaTypeWapper<Schema>, bool>(&mut self.id, SchemaTypeWapper<Schema> {});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/// Check if an schema is authorized to access protected features of
|
|
253
|
+
/// the SchemaHub.
|
|
254
|
+
public fun is_schema_authorized<Schema: key + store>(self: &SchemaHub): bool {
|
|
255
|
+
df::exists_(&self.id, SchemaTypeWapper<Schema> {})
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/// Assert that an schema is authorized to access protected features of
|
|
259
|
+
/// the SchemaHub. Aborts with \`EAppNotAuthorized\` if not.
|
|
260
|
+
public fun ensure_schema_authorized<Schema: key + store>(self: &SchemaHub) {
|
|
261
|
+
assert!(self.is_schema_authorized<Schema>(), 0);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
fun init(ctx: &mut TxContext) {
|
|
265
|
+
public_share_object(SchemaHub {
|
|
266
|
+
id: object::new(ctx),
|
|
267
|
+
admin: ctx.sender(),
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#[test_only]
|
|
272
|
+
public fun init_schema_hub_for_testing(ctx: &mut TxContext) {
|
|
273
|
+
init(ctx);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
`;await m(r,`${t}/contracts/${e.name}/sources/codegen/schema_hub.move`,"formatAndWriteMove"),console.log(`\u2705 DappKey Generation Complete
|
|
277
|
+
`)}function N(e){switch(e){case"testnet":return"0x417ad1864a56a29ad0b5aaddd2e11bac1eeab6a68883ef53184a4cc5c293fec6";case"localnet":return"0x417ad1864a56a29ad0b5aaddd2e11bac1eeab6a68883ef53184a4cc5c293fec6";default:return"0x417ad1864a56a29ad0b5aaddd2e11bac1eeab6a68883ef53184a4cc5c293fec6"}}async function at(e,t,r,o){console.log(`
|
|
278
|
+
\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"}`),console.log(` \u2514\u2500 Framework ID: ${o||N(r??"testnet")}
|
|
279
|
+
`);let s=t??process.cwd();o=o||N(r??"testnet"),A(`${s}/contracts/${e.name}`)&&k(`${s}/contracts/${e.name}/sources/codegen`),A(`${s}/contracts/${e.name}/Move.toml`)||await H(e,s,o),A(`${s}/contracts/${e.name}/sources/script/deploy_hook.move`)||await U(e,s),await O(e.name,e.schemas,s),await z(e.name,e.schemas,s,e.migration_enabled),await K(e.name,e.schemas,s),await L(e,s),await V(e,s),await B(e,s),e.migration_enabled&&await F(e,s),console.log(`\u2705 Schema Generation Process Complete!
|
|
280
|
+
`)}import{findUp as ne}from"find-up";import C from"path";import ae from"esbuild";var x=class extends Error{name="NotInsideProjectError";message="You are not inside a Dubhe project"};import{rmSync as ie}from"fs";import{pathToFileURL as ce}from"url";import me from"os";var ue=["dubhe.config.js","dubhe.config.mjs","dubhe.config.ts","dubhe.config.mts"],D="dubhe.config.example.mjs";async function $t(e){e=await Q(e);try{return await ae.build({entryPoints:[e],format:"esm",outfile:D,platform:"node",bundle:!0,packages:"external"}),e=await Q(D,!0),(await import(e+`?update=${Date.now()}`)).dubheConfig}finally{ie(D,{force:!0})}}async function Q(e,t){return e===void 0?e=await pe():C.isAbsolute(e)||(e=C.join(process.cwd(),e),e=C.normalize(e)),t&&me.platform()==="win32"?ce(e).href:e}async function pe(){let e=await ne(ue);if(e===void 0)throw new x;return e}export{m as formatAndWriteMove,ve as formatAndWriteTypescript,T as formatMove,M as formatTypescript,$t as loadConfig,ke as posixPath,Q as resolveConfigPath,at as schemaGen};
|
|
177
281
|
//# sourceMappingURL=index.js.map
|