@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
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
getStructAttrsQuery,
|
|
8
8
|
} from './common';
|
|
9
9
|
|
|
10
|
-
function capitalizeAndRemoveUnderscores(input: string): string {
|
|
10
|
+
export function capitalizeAndRemoveUnderscores(input: string): string {
|
|
11
11
|
return input
|
|
12
12
|
.split('_')
|
|
13
13
|
.map((word, index) => {
|
|
@@ -20,34 +20,34 @@ function capitalizeAndRemoveUnderscores(input: string): string {
|
|
|
20
20
|
|
|
21
21
|
export function renderSetAttrsFunc(
|
|
22
22
|
schemaName: string,
|
|
23
|
-
fields: BaseType | Record<string, BaseType
|
|
23
|
+
fields: BaseType | Record<string, BaseType>,
|
|
24
24
|
): string {
|
|
25
25
|
return Object.entries(fields)
|
|
26
26
|
.map(
|
|
27
27
|
([key, type]) =>
|
|
28
28
|
`public(package) fun set_${key}(self: &mut ${schemaName}, ${key}: ${type}) {
|
|
29
29
|
self.${key} = ${key};
|
|
30
|
-
}
|
|
30
|
+
}`,
|
|
31
31
|
)
|
|
32
32
|
.join('\n');
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
export function renderSetFunc(
|
|
36
36
|
schemaName: string,
|
|
37
|
-
fields: Record<string, string
|
|
37
|
+
fields: Record<string, string>,
|
|
38
38
|
): string {
|
|
39
39
|
return `public(package) fun set(self: &mut ${schemaName}, ${getStructAttrsWithType(
|
|
40
|
-
fields
|
|
40
|
+
fields,
|
|
41
41
|
)}) {
|
|
42
42
|
${Object.entries(fields)
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
.map(([fieldName]) => `self.${fieldName} = ${fieldName};`)
|
|
44
|
+
.join('\n')}
|
|
45
45
|
}`;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export function renderGetAllFunc(
|
|
49
49
|
schemaName: string,
|
|
50
|
-
fields: Record<string, string
|
|
50
|
+
fields: Record<string, string>,
|
|
51
51
|
): string {
|
|
52
52
|
return `public fun get(self: &${schemaName}): ${getStructTypes(fields)} {
|
|
53
53
|
(${getStructAttrsQuery(fields)})
|
|
@@ -56,16 +56,16 @@ export function renderGetAllFunc(
|
|
|
56
56
|
|
|
57
57
|
export function renderGetAttrsFunc(
|
|
58
58
|
schemaName: string,
|
|
59
|
-
fields: BaseType | Record<string, BaseType
|
|
59
|
+
fields: BaseType | Record<string, BaseType>,
|
|
60
60
|
): string {
|
|
61
61
|
return Object.entries(fields)
|
|
62
62
|
.map(
|
|
63
63
|
([
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
key,
|
|
65
|
+
type,
|
|
66
|
+
]) => `public fun get_${key}(self: &${schemaName}): ${type} {
|
|
67
67
|
self.${key}
|
|
68
|
-
}
|
|
68
|
+
}`,
|
|
69
69
|
)
|
|
70
70
|
.join('\n');
|
|
71
71
|
}
|
|
@@ -80,7 +80,7 @@ function convertToSnakeCase(input: string): string {
|
|
|
80
80
|
export async function generateSchemaData(
|
|
81
81
|
projectName: string,
|
|
82
82
|
schemas: Record<string, SchemaType>,
|
|
83
|
-
path: string
|
|
83
|
+
path: string,
|
|
84
84
|
) {
|
|
85
85
|
console.log('\nš¦ Starting Schema Data Generation...');
|
|
86
86
|
for (const schemaName in schemas) {
|
|
@@ -91,7 +91,7 @@ export async function generateSchemaData(
|
|
|
91
91
|
console.log(
|
|
92
92
|
` āā Generating ${item.name} ${
|
|
93
93
|
Array.isArray(item.fields) ? '(enum)' : '(struct)'
|
|
94
|
-
}
|
|
94
|
+
}`,
|
|
95
95
|
);
|
|
96
96
|
let code = '';
|
|
97
97
|
|
|
@@ -101,42 +101,42 @@ export async function generateSchemaData(
|
|
|
101
101
|
|
|
102
102
|
if (Array.isArray(item.fields)) {
|
|
103
103
|
code = `module ${projectName}::${schemaName}_${convertToSnakeCase(
|
|
104
|
-
item.name
|
|
104
|
+
item.name,
|
|
105
105
|
)} {
|
|
106
106
|
public enum ${item.name} has copy, drop , store {
|
|
107
107
|
${item.fields}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
${item.fields
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
111
|
+
.map((field: string) => {
|
|
112
|
+
return `public fun new_${convertToSnakeCase(
|
|
113
|
+
field,
|
|
114
|
+
)}(): ${item.name} {
|
|
115
115
|
${item.name}::${field}
|
|
116
116
|
}`;
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
})
|
|
118
|
+
.join('')}`;
|
|
119
119
|
} else {
|
|
120
120
|
code = `module ${projectName}::${schemaName}_${convertToSnakeCase(
|
|
121
|
-
item.name
|
|
121
|
+
item.name,
|
|
122
122
|
)} {
|
|
123
123
|
use std::ascii::String;
|
|
124
124
|
${enumNames
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
125
|
+
.map(
|
|
126
|
+
name =>
|
|
127
|
+
`use ${projectName}::${schemaName}_${convertToSnakeCase(
|
|
128
|
+
name,
|
|
129
|
+
)}::${name};`,
|
|
130
|
+
)
|
|
131
|
+
.join('\n')}
|
|
132
132
|
|
|
133
133
|
public struct ${item.name} has copy, drop , store {
|
|
134
134
|
${getStructAttrsWithType(item.fields)}
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
public fun new(${getStructAttrsWithType(
|
|
138
|
-
|
|
139
|
-
|
|
138
|
+
item.fields,
|
|
139
|
+
)}): ${item.name} {
|
|
140
140
|
${item.name} {
|
|
141
141
|
${getStructAttrs(item.fields)}
|
|
142
142
|
}
|
|
@@ -152,9 +152,9 @@ export async function generateSchemaData(
|
|
|
152
152
|
await formatAndWriteMove(
|
|
153
153
|
code,
|
|
154
154
|
`${path}/contracts/${projectName}/sources/codegen/schemas/${schemaName}_${convertToSnakeCase(
|
|
155
|
-
item.name
|
|
155
|
+
item.name,
|
|
156
156
|
)}.move`,
|
|
157
|
-
'formatAndWriteMove'
|
|
157
|
+
'formatAndWriteMove',
|
|
158
158
|
);
|
|
159
159
|
}
|
|
160
160
|
}
|
|
@@ -165,13 +165,13 @@ export async function generateSchemaData(
|
|
|
165
165
|
function generateImport(
|
|
166
166
|
projectName: string,
|
|
167
167
|
schemaName: string,
|
|
168
|
-
schema: SchemaType
|
|
168
|
+
schema: SchemaType,
|
|
169
169
|
) {
|
|
170
170
|
if (schema.data) {
|
|
171
171
|
return schema.data
|
|
172
172
|
.map(item => {
|
|
173
173
|
return `use ${projectName}::${schemaName}_${convertToSnakeCase(
|
|
174
|
-
item.name
|
|
174
|
+
item.name,
|
|
175
175
|
)}::${item.name};`;
|
|
176
176
|
})
|
|
177
177
|
.join('\n');
|
|
@@ -183,128 +183,124 @@ function generateImport(
|
|
|
183
183
|
export async function generateSchemaStructure(
|
|
184
184
|
projectName: string,
|
|
185
185
|
schemas: Record<string, SchemaType>,
|
|
186
|
-
path: string
|
|
186
|
+
path: string,
|
|
187
187
|
) {
|
|
188
188
|
console.log('\nšØ Starting Schema Structure Generation...');
|
|
189
189
|
for (const schemaName in schemas) {
|
|
190
190
|
console.log(` āā Generating schema: ${schemaName}`);
|
|
191
191
|
console.log(
|
|
192
|
-
` āā Output path: ${path}/contracts/${projectName}/sources/codegen/schemas/${schemaName}.move
|
|
192
|
+
` āā Output path: ${path}/contracts/${projectName}/sources/codegen/schemas/${schemaName}.move`,
|
|
193
193
|
);
|
|
194
194
|
console.log(
|
|
195
195
|
` āā Structure fields: ${
|
|
196
196
|
Object.keys(schemas[schemaName].structure).length
|
|
197
|
-
}
|
|
197
|
+
}`,
|
|
198
198
|
);
|
|
199
199
|
const schema = schemas[schemaName];
|
|
200
200
|
const schemaMoudle = `module ${projectName}::${schemaName}_schema {
|
|
201
201
|
use std::ascii::String;
|
|
202
|
+
use std::ascii::string;
|
|
203
|
+
use sui::package::UpgradeCap;
|
|
202
204
|
use std::type_name;
|
|
203
205
|
use dubhe::dapps_system;
|
|
206
|
+
use dubhe::storage_migrate;
|
|
204
207
|
use dubhe::dapps_schema::Dapps;
|
|
205
208
|
use dubhe::storage_value::{Self, StorageValue};
|
|
206
209
|
use dubhe::storage_map::{Self, StorageMap};
|
|
207
210
|
use dubhe::storage_double_map::{Self, StorageDoubleMap};
|
|
208
211
|
use ${projectName}::dapp_key::DappKey;
|
|
212
|
+
use sui::dynamic_field as df;
|
|
209
213
|
${generateImport(projectName, schemaName, schema)}
|
|
210
214
|
|
|
211
215
|
public struct ${capitalizeAndRemoveUnderscores(
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
id: UID
|
|
215
|
-
|
|
216
|
-
}
|
|
216
|
+
schemaName,
|
|
217
|
+
)} has key, store {
|
|
218
|
+
id: UID
|
|
219
|
+
}
|
|
217
220
|
|
|
218
221
|
${Object.entries(schema.structure)
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
&self
|
|
222
|
+
.map(([key, value]) => {
|
|
223
|
+
return `public fun borrow_${key}(self: &${capitalizeAndRemoveUnderscores(
|
|
224
|
+
schemaName,
|
|
225
|
+
)}) : &${value} {
|
|
226
|
+
storage_migrate::borrow_field(&self.id, b"${key}")
|
|
224
227
|
}
|
|
225
228
|
|
|
226
229
|
public(package) fun borrow_mut_${key}(self: &mut ${capitalizeAndRemoveUnderscores(
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
&mut self
|
|
230
|
+
schemaName,
|
|
231
|
+
)}): &mut ${value} {
|
|
232
|
+
storage_migrate::borrow_mut_field(&mut self.id, b"${key}")
|
|
230
233
|
}
|
|
231
234
|
`;
|
|
232
|
-
|
|
233
|
-
|
|
235
|
+
})
|
|
236
|
+
.join('')}
|
|
234
237
|
|
|
235
238
|
|
|
236
|
-
public fun
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
let
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
storage_type = `storage_map::new()`;
|
|
258
|
-
} else if (
|
|
259
|
-
value.includes('StorageDoubleMap')
|
|
260
|
-
) {
|
|
261
|
-
storage_type = `storage_double_map::new()`;
|
|
262
|
-
}
|
|
263
|
-
return `${key}: ${storage_type},`;
|
|
264
|
-
})
|
|
265
|
-
.join(' ')}
|
|
266
|
-
}
|
|
239
|
+
public(package) fun create(ctx: &mut TxContext): ${capitalizeAndRemoveUnderscores(
|
|
240
|
+
schemaName,
|
|
241
|
+
)} {
|
|
242
|
+
let mut id = object::new(ctx);
|
|
243
|
+
${Object.entries(schema.structure)
|
|
244
|
+
.map(([key, value]) => {
|
|
245
|
+
let storage_type = '';
|
|
246
|
+
if (value.includes('StorageValue')) {
|
|
247
|
+
storage_type = `storage_value::new()`;
|
|
248
|
+
} else if (value.includes('StorageMap')) {
|
|
249
|
+
storage_type = `storage_map::new()`;
|
|
250
|
+
} else if (
|
|
251
|
+
value.includes('StorageDoubleMap')
|
|
252
|
+
) {
|
|
253
|
+
storage_type = `storage_double_map::new()`;
|
|
254
|
+
}
|
|
255
|
+
return `storage_migrate::add_field<${value}>(&mut id, b"${key}", ${storage_type});`;
|
|
256
|
+
})
|
|
257
|
+
.join('')}
|
|
258
|
+
|
|
259
|
+
${capitalizeAndRemoveUnderscores(schemaName)} { id }
|
|
267
260
|
}
|
|
268
261
|
|
|
262
|
+
public fun migrate(_${schemaName}: &mut ${capitalizeAndRemoveUnderscores(schemaName)}, _cap: &UpgradeCap) { }
|
|
263
|
+
|
|
269
264
|
|
|
265
|
+
|
|
270
266
|
// ======================================== View Functions ========================================
|
|
271
267
|
${Object.entries(schema.structure)
|
|
272
268
|
.map(([key, value]) => {
|
|
273
269
|
// @ts-ignore
|
|
274
|
-
let all_types = value.match(/<(.+)>/)[1].split(',').map(type => type.trim())
|
|
275
|
-
let para_key: string[] = []
|
|
276
|
-
let para_value =
|
|
277
|
-
let borrow_key =
|
|
278
|
-
let extra_code =
|
|
270
|
+
let all_types = value.match(/<(.+)>/)[1].split(',').map(type => type.trim());
|
|
271
|
+
let para_key: string[] = [];
|
|
272
|
+
let para_value = '';
|
|
273
|
+
let borrow_key = '';
|
|
274
|
+
let extra_code = '';
|
|
279
275
|
if (value.includes('StorageValue')) {
|
|
280
|
-
para_key = []
|
|
281
|
-
para_value = `${all_types[0]}
|
|
282
|
-
borrow_key = 'try_get()'
|
|
276
|
+
para_key = [];
|
|
277
|
+
para_value = `${all_types[0]}`;
|
|
278
|
+
borrow_key = 'try_get()';
|
|
283
279
|
} else if (value.includes('StorageMap')) {
|
|
284
|
-
para_key = [`key: ${all_types[0]}`]
|
|
285
|
-
para_value = `${all_types[1]}
|
|
286
|
-
borrow_key = 'try_get(key)'
|
|
280
|
+
para_key = [`key: ${all_types[0]}`];
|
|
281
|
+
para_value = `${all_types[1]}`;
|
|
282
|
+
borrow_key = 'try_get(key)';
|
|
287
283
|
extra_code = `public fun get_${key}_keys(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[0]}> {
|
|
288
|
-
self
|
|
284
|
+
self.borrow_${key}().keys()
|
|
289
285
|
}
|
|
290
286
|
|
|
291
287
|
public fun get_${key}_values(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[1]}> {
|
|
292
|
-
self
|
|
293
|
-
}
|
|
288
|
+
self.borrow_${key}().values()
|
|
289
|
+
}`;
|
|
294
290
|
} else if (value.includes('StorageDoubleMap')) {
|
|
295
|
-
para_key = [`key1: ${all_types[0]}`, `key2: ${all_types[1]}`]
|
|
296
|
-
para_value = `${all_types[2]}
|
|
297
|
-
borrow_key = 'try_get(key1, key2)'
|
|
291
|
+
para_key = [`key1: ${all_types[0]}`, `key2: ${all_types[1]}`];
|
|
292
|
+
para_value = `${all_types[2]}`;
|
|
293
|
+
borrow_key = 'try_get(key1, key2)';
|
|
298
294
|
extra_code = `public fun get_${key}_keys(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : (vector<${all_types[0]}>, vector<${all_types[1]}>) {
|
|
299
|
-
self
|
|
295
|
+
self.borrow_${key}().keys()
|
|
300
296
|
}
|
|
301
297
|
|
|
302
298
|
public fun get_${key}_values(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[2]}> {
|
|
303
|
-
self
|
|
304
|
-
}
|
|
299
|
+
self.borrow_${key}().values()
|
|
300
|
+
}`;
|
|
305
301
|
}
|
|
306
302
|
return `public fun get_${key}(self: &${capitalizeAndRemoveUnderscores(schemaName)}, ${para_key}) : Option<${para_value}> {
|
|
307
|
-
self
|
|
303
|
+
self.borrow_${key}().${borrow_key}
|
|
308
304
|
}
|
|
309
305
|
` + extra_code;
|
|
310
306
|
})
|
|
@@ -316,7 +312,7 @@ export async function generateSchemaStructure(
|
|
|
316
312
|
await formatAndWriteMove(
|
|
317
313
|
schemaMoudle,
|
|
318
314
|
`${path}/contracts/${projectName}/sources/codegen/schemas/${schemaName}.move`,
|
|
319
|
-
'formatAndWriteMove'
|
|
315
|
+
'formatAndWriteMove',
|
|
320
316
|
);
|
|
321
317
|
}
|
|
322
318
|
console.log('ā
Schema Structure Generation Complete\n');
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { DubheConfig } from '../../types';
|
|
2
|
+
import { formatAndWriteMove } from '../formatAndWrite';
|
|
3
|
+
|
|
4
|
+
export async function generateSchemaHub(
|
|
5
|
+
config: DubheConfig,
|
|
6
|
+
srcPrefix: string
|
|
7
|
+
) {
|
|
8
|
+
console.log('\nš Starting DappKey Generation...');
|
|
9
|
+
console.log(
|
|
10
|
+
` āā Output path: ${srcPrefix}/contracts/${config.name}/sources/codegen/schema_hub.move`
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
let code = `module ${config.name}::schema_hub {
|
|
14
|
+
use sui::transfer::public_share_object;
|
|
15
|
+
use sui::dynamic_field as df;
|
|
16
|
+
|
|
17
|
+
public struct SchemaHub has key, store {
|
|
18
|
+
id: UID,
|
|
19
|
+
admin: address,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public struct SchemaTypeWapper<phantom Schema: key + store> has copy, store, drop {}
|
|
23
|
+
|
|
24
|
+
/// Authorize an schema to access protected features of the SchemaHub.
|
|
25
|
+
public(package) fun authorize_schema<Schema: key + store>(self: &mut SchemaHub) {
|
|
26
|
+
df::add(&mut self.id, SchemaTypeWapper<Schema> {}, true);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// Deauthorize an schema by removing its authorization key.
|
|
30
|
+
public(package) fun deauthorize_schema<Schema: key + store>(self: &mut SchemaHub) {
|
|
31
|
+
df::remove<SchemaTypeWapper<Schema>, bool>(&mut self.id, SchemaTypeWapper<Schema> {});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// Check if an schema is authorized to access protected features of
|
|
35
|
+
/// the SchemaHub.
|
|
36
|
+
public fun is_schema_authorized<Schema: key + store>(self: &SchemaHub): bool {
|
|
37
|
+
df::exists_(&self.id, SchemaTypeWapper<Schema> {})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// Assert that an schema is authorized to access protected features of
|
|
41
|
+
/// the SchemaHub. Aborts with \`EAppNotAuthorized\` if not.
|
|
42
|
+
public fun ensure_schema_authorized<Schema: key + store>(self: &SchemaHub) {
|
|
43
|
+
assert!(self.is_schema_authorized<Schema>(), 0);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fun init(ctx: &mut TxContext) {
|
|
47
|
+
public_share_object(SchemaHub {
|
|
48
|
+
id: object::new(ctx),
|
|
49
|
+
admin: ctx.sender(),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#[test_only]
|
|
54
|
+
public fun init_schema_hub_for_testing(ctx: &mut TxContext) {
|
|
55
|
+
init(ctx);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
`;
|
|
59
|
+
await formatAndWriteMove(
|
|
60
|
+
code,
|
|
61
|
+
`${srcPrefix}/contracts/${config.name}/sources/codegen/schema_hub.move`,
|
|
62
|
+
'formatAndWriteMove'
|
|
63
|
+
);
|
|
64
|
+
console.log('ā
DappKey Generation Complete\n');
|
|
65
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DubheConfig } from '../../types';
|
|
2
2
|
import { formatAndWriteMove } from '../formatAndWrite';
|
|
3
3
|
import { existsSync } from 'fs';
|
|
4
|
+
import { capitalizeAndRemoveUnderscores } from './generateSchema';
|
|
4
5
|
|
|
5
6
|
export async function generateDeployHook(
|
|
6
7
|
config: DubheConfig,
|
|
@@ -19,59 +20,67 @@ export async function generateDeployHook(
|
|
|
19
20
|
let code = `module ${config.name}::deploy_hook {
|
|
20
21
|
use dubhe::dapps_schema::Dapps;
|
|
21
22
|
use dubhe::dapps_system;
|
|
22
|
-
use ${config.name}::
|
|
23
|
-
use std::ascii;
|
|
23
|
+
use ${config.name}::schema_hub::SchemaHub;
|
|
24
|
+
use std::ascii::string;
|
|
24
25
|
use sui::clock::Clock;
|
|
26
|
+
use sui::package::UpgradeCap;
|
|
25
27
|
use sui::transfer::public_share_object;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
${Object.keys(config.schemas).map(schemaName => {
|
|
29
|
+
return `use ${config.name}::${schemaName}_schema::${capitalizeAndRemoveUnderscores(schemaName)};`}).join('\n')
|
|
30
|
+
}
|
|
28
31
|
#[test_only]
|
|
29
32
|
use sui::clock;
|
|
30
33
|
#[test_only]
|
|
31
34
|
use sui::test_scenario;
|
|
32
35
|
#[test_only]
|
|
36
|
+
use sui::package;
|
|
37
|
+
#[test_only]
|
|
38
|
+
use ${config.name}::schema_hub;
|
|
39
|
+
#[test_only]
|
|
40
|
+
use dubhe::dapps_schema;
|
|
41
|
+
#[test_only]
|
|
33
42
|
use sui::test_scenario::Scenario;
|
|
34
43
|
|
|
35
|
-
public entry fun run(dapps: &mut Dapps, clock: &Clock, ctx: &mut TxContext) {
|
|
44
|
+
public entry fun run(schema_hub: &mut SchemaHub, dapps: &mut Dapps, cap: &UpgradeCap, clock: &Clock, ctx: &mut TxContext) {
|
|
36
45
|
// Register the dapp to dubhe.
|
|
37
|
-
dapps_system::register
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
ctx
|
|
43
|
-
);
|
|
44
|
-
${Object.keys(config.schemas)
|
|
45
|
-
.map(schemaName => {
|
|
46
|
-
return `let ${schemaName} = ${config.name}::${schemaName}_schema::register(dapps, ctx);`;
|
|
47
|
-
})
|
|
48
|
-
.join('\n')}
|
|
46
|
+
dapps_system::register(dapps,cap,string(b"${config.name}"),string(b"${config.description}"),clock,ctx);
|
|
47
|
+
// Create schemas
|
|
48
|
+
${Object.keys(config.schemas).map(schemaName => {
|
|
49
|
+
return `let ${schemaName} = ${config.name}::${schemaName}_schema::create(ctx);`;
|
|
50
|
+
}).join('\n')}
|
|
49
51
|
// Logic that needs to be automated once the contract is deployed
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
${`\n`}
|
|
53
|
+
${`\n`}
|
|
54
|
+
|
|
55
|
+
// Authorize schemas and public share objects
|
|
56
|
+
${Object.keys(config.schemas).map(schemaName => {
|
|
57
|
+
return `
|
|
58
|
+
schema_hub.authorize_schema<${capitalizeAndRemoveUnderscores(schemaName)}>();
|
|
59
|
+
public_share_object(${schemaName});
|
|
60
|
+
`;
|
|
61
|
+
}).join('\n')}
|
|
58
62
|
}
|
|
59
63
|
|
|
60
64
|
#[test_only]
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
public fun deploy_hook_for_testing(): (Scenario, SchemaHub, Dapps) {
|
|
66
|
+
let mut scenario = test_scenario::begin(@0xA);
|
|
67
|
+
{
|
|
64
68
|
let ctx = test_scenario::ctx(&mut scenario);
|
|
65
69
|
dapps_schema::init_dapps_for_testing(ctx);
|
|
70
|
+
schema_hub::init_schema_hub_for_testing(ctx);
|
|
66
71
|
test_scenario::next_tx(&mut scenario,@0xA);
|
|
67
72
|
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
let mut dapps = test_scenario::take_shared<Dapps>(&scenario);
|
|
74
|
+
let mut schema_hub = test_scenario::take_shared<SchemaHub>(&scenario);
|
|
75
|
+
let ctx = test_scenario::ctx(&mut scenario);
|
|
76
|
+
let clock = clock::create_for_testing(ctx);
|
|
77
|
+
let upgrade_cap = package::test_publish(@0x42.to_id(), ctx);
|
|
78
|
+
run(&mut schema_hub, &mut dapps, &upgrade_cap, &clock, ctx);
|
|
79
|
+
|
|
80
|
+
clock::destroy_for_testing(clock);
|
|
81
|
+
upgrade_cap.make_immutable();
|
|
82
|
+
test_scenario::next_tx(&mut scenario,@0xA);
|
|
83
|
+
(scenario, schema_hub, dapps)
|
|
75
84
|
}
|
|
76
85
|
}
|
|
77
86
|
`;
|
|
@@ -84,33 +93,21 @@ export async function generateDeployHook(
|
|
|
84
93
|
console.log('ā
Deploy Hook Generation Complete\n');
|
|
85
94
|
}
|
|
86
95
|
|
|
87
|
-
export function generateMigrate(config: DubheConfig, srcPrefix: string) {
|
|
96
|
+
export async function generateMigrate(config: DubheConfig, srcPrefix: string) {
|
|
88
97
|
if (
|
|
89
98
|
!existsSync(
|
|
90
99
|
`${srcPrefix}/contracts/${config.name}/sources/script/migrate.move`
|
|
91
100
|
)
|
|
92
101
|
) {
|
|
93
102
|
let code = `module ${config.name}::migrate {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
/// Not the right admin for this world
|
|
97
|
-
const ENotAdmin: u64 = 0;
|
|
98
|
-
const EWrongVersion: u64 = 1;
|
|
99
|
-
const ENotUpgrade: u64 = 2;
|
|
100
|
-
const VERSION: u64 = 1;
|
|
103
|
+
const ON_CHAIN_VERSION: u32 = 0;
|
|
101
104
|
|
|
102
|
-
public
|
|
103
|
-
|
|
104
|
-
assert!(world.version() < VERSION, ENotUpgrade);
|
|
105
|
-
*dubhe::world::mut_version(world, admin_cap) = VERSION;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
public fun assert_version(world: &World){
|
|
109
|
-
assert!(world.version() == VERSION, EWrongVersion);
|
|
105
|
+
public fun on_chain_version(): u32 {
|
|
106
|
+
ON_CHAIN_VERSION
|
|
110
107
|
}
|
|
111
108
|
}
|
|
112
109
|
`;
|
|
113
|
-
formatAndWriteMove(
|
|
110
|
+
await formatAndWriteMove(
|
|
114
111
|
code,
|
|
115
112
|
`${srcPrefix}/contracts/${config.name}/sources/script/migrate.move`,
|
|
116
113
|
'formatAndWriteMove'
|
|
@@ -1,30 +1,22 @@
|
|
|
1
1
|
import { DubheConfig } from '../../types';
|
|
2
2
|
import { formatAndWriteMove } from '../formatAndWrite';
|
|
3
3
|
import { existsSync } from 'fs';
|
|
4
|
+
import fs from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
4
6
|
|
|
5
7
|
export async function generateSystem(config: DubheConfig, srcPrefix: string) {
|
|
6
8
|
console.log('\nāļø Starting System Generation...');
|
|
7
|
-
|
|
8
|
-
console.log(` āā Generating system: ${systemName}`);
|
|
9
|
+
console.log(` āā Generating systems`);
|
|
9
10
|
console.log(
|
|
10
|
-
` āā Output path: ${srcPrefix}/contracts/${config.name}/sources/
|
|
11
|
+
` āā Output path: ${srcPrefix}/contracts/${config.name}/sources/systems`
|
|
11
12
|
);
|
|
12
13
|
|
|
13
14
|
if (
|
|
14
15
|
!existsSync(
|
|
15
|
-
`${srcPrefix}/contracts/${config.name}/sources/
|
|
16
|
+
`${srcPrefix}/contracts/${config.name}/sources/systems`
|
|
16
17
|
)
|
|
17
18
|
) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
`;
|
|
22
|
-
await formatAndWriteMove(
|
|
23
|
-
code,
|
|
24
|
-
`${srcPrefix}/contracts/${config.name}/sources/system/${systemName}.move`,
|
|
25
|
-
'formatAndWriteMove'
|
|
26
|
-
);
|
|
19
|
+
await fs.mkdir(`${srcPrefix}/contracts/${config.name}/sources/systems`, { recursive: true })
|
|
27
20
|
}
|
|
28
|
-
});
|
|
29
21
|
console.log('ā
System Generation Complete\n');
|
|
30
22
|
}
|
|
@@ -18,7 +18,7 @@ edition = "2024.beta"
|
|
|
18
18
|
|
|
19
19
|
[dependencies]
|
|
20
20
|
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.36.2" }
|
|
21
|
-
Dubhe = { git = "https://github.com/0xobelisk/dubhe.git",
|
|
21
|
+
Dubhe = { git = "https://github.com/0xobelisk/dubhe-framework.git", rev = "release-dubhe-v1.0.0-rc1" }
|
|
22
22
|
|
|
23
23
|
[addresses]
|
|
24
24
|
sui = "0x2"
|