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