@0xobelisk/sui-common 1.0.3 ā 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +253 -265
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/codegen/types/index.ts +1 -1
- package/src/codegen/utils/renderMove/generateDefaultSchema.ts +168 -161
- package/src/codegen/utils/renderMove/generateError.ts +18 -22
- package/src/codegen/utils/renderMove/generateEvent.ts +27 -6
- package/src/codegen/utils/renderMove/generateSchema.ts +49 -72
- package/src/codegen/utils/renderMove/generateScript.ts +4 -18
|
@@ -7,6 +7,21 @@ import {
|
|
|
7
7
|
getStructAttrsQuery,
|
|
8
8
|
} from './common';
|
|
9
9
|
|
|
10
|
+
function sortByFirstLetter(arr: string[]): string[] {
|
|
11
|
+
return arr.sort((a, b) => {
|
|
12
|
+
const firstLetterA = a.charAt(0).toLowerCase();
|
|
13
|
+
const firstLetterB = b.charAt(0).toLowerCase();
|
|
14
|
+
|
|
15
|
+
if (firstLetterA < firstLetterB) {
|
|
16
|
+
return -1;
|
|
17
|
+
}
|
|
18
|
+
if (firstLetterA > firstLetterB) {
|
|
19
|
+
return 1;
|
|
20
|
+
}
|
|
21
|
+
return 0;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
10
25
|
export function capitalizeAndRemoveUnderscores(input: string): string {
|
|
11
26
|
return input
|
|
12
27
|
.split('_')
|
|
@@ -100,14 +115,15 @@ export async function generateSchemaData(
|
|
|
100
115
|
console.log(enumNames)
|
|
101
116
|
|
|
102
117
|
if (Array.isArray(fields)) {
|
|
118
|
+
const sortByFirstLetterFields = sortByFirstLetter(fields);
|
|
103
119
|
code = `module ${projectName}::${convertToSnakeCase(
|
|
104
120
|
name,
|
|
105
121
|
)} {
|
|
106
122
|
public enum ${name} has copy, drop , store {
|
|
107
|
-
${
|
|
123
|
+
${sortByFirstLetterFields}
|
|
108
124
|
}
|
|
109
125
|
|
|
110
|
-
${
|
|
126
|
+
${sortByFirstLetterFields
|
|
111
127
|
.map((field: string) => {
|
|
112
128
|
return `public fun new_${convertToSnakeCase(
|
|
113
129
|
field,
|
|
@@ -185,138 +201,99 @@ export async function generateSchemaStructure(
|
|
|
185
201
|
path: string,
|
|
186
202
|
) {
|
|
187
203
|
console.log('\nšØ Starting Schema Structure Generation...');
|
|
188
|
-
for (const schemaName in schemas) {
|
|
189
|
-
console.log(` āā Generating schema: ${schemaName}`);
|
|
190
204
|
console.log(
|
|
191
|
-
` āā Output path: ${path}/contracts/${projectName}/sources/codegen/
|
|
205
|
+
` āā Output path: ${path}/contracts/${projectName}/sources/codegen/schema.move`,
|
|
192
206
|
);
|
|
193
207
|
console.log(
|
|
194
208
|
` āā Structure fields: ${
|
|
195
|
-
Object.values(schemas
|
|
209
|
+
Object.values(schemas).length
|
|
196
210
|
}`,
|
|
197
211
|
);
|
|
198
|
-
const
|
|
199
|
-
const schemaMoudle = `module ${projectName}::${schemaName}_schema {
|
|
212
|
+
const schemaMoudle = `module ${projectName}::schema {
|
|
200
213
|
use std::ascii::String;
|
|
201
214
|
use std::ascii::string;
|
|
202
215
|
use sui::package::UpgradeCap;
|
|
203
216
|
use std::type_name;
|
|
204
|
-
use dubhe::
|
|
217
|
+
use dubhe::storage;
|
|
205
218
|
use dubhe::storage_value::{Self, StorageValue};
|
|
206
219
|
use dubhe::storage_map::{Self, StorageMap};
|
|
207
220
|
use dubhe::storage_double_map::{Self, StorageDoubleMap};
|
|
208
221
|
use sui::dynamic_field as df;
|
|
209
|
-
|
|
210
|
-
use sui::coin::Coin;
|
|
211
|
-
use sui::balance::Balance;
|
|
222
|
+
|
|
212
223
|
${generateImport(projectName, data)}
|
|
213
224
|
|
|
214
|
-
public struct
|
|
215
|
-
schemaName,
|
|
216
|
-
)} has key, store {
|
|
217
|
-
id: UID
|
|
218
|
-
}
|
|
225
|
+
public struct Schema has key, store { id: UID }
|
|
219
226
|
|
|
220
|
-
${Object.entries(
|
|
227
|
+
${Object.entries(schemas)
|
|
221
228
|
.map(([key, value]) => {
|
|
222
|
-
return `public fun borrow_${key}(self: &${
|
|
223
|
-
|
|
224
|
-
)}) : &${value} {
|
|
225
|
-
storage_migration::borrow_field(&self.id, b"${key}")
|
|
229
|
+
return `public fun borrow_${key}(self: &Schema) : &${value} {
|
|
230
|
+
storage::borrow_field(&self.id, b"${key}")
|
|
226
231
|
}
|
|
227
232
|
|
|
228
|
-
public(package) fun ${key}(self: &mut ${
|
|
229
|
-
|
|
230
|
-
)}): &mut ${value} {
|
|
231
|
-
storage_migration::borrow_mut_field(&mut self.id, b"${key}")
|
|
233
|
+
public(package) fun ${key}(self: &mut Schema): &mut ${value} {
|
|
234
|
+
storage::borrow_mut_field(&mut self.id, b"${key}")
|
|
232
235
|
}
|
|
233
236
|
`;
|
|
234
237
|
})
|
|
235
238
|
.join('')}
|
|
236
239
|
|
|
237
240
|
|
|
238
|
-
public(package) fun create(ctx: &mut TxContext):
|
|
239
|
-
schemaName,
|
|
240
|
-
)} {
|
|
241
|
+
public(package) fun create(ctx: &mut TxContext): Schema {
|
|
241
242
|
let mut id = object::new(ctx);
|
|
242
|
-
${Object.entries(
|
|
243
|
+
${Object.entries(schemas)
|
|
243
244
|
.map(([key, value]) => {
|
|
244
245
|
let storage_type = '';
|
|
245
246
|
if (value.includes('StorageValue')) {
|
|
246
|
-
storage_type = `storage_value::new()`;
|
|
247
|
+
storage_type = `storage_value::new(b"${key}", ctx)`;
|
|
247
248
|
} else if (value.includes('StorageMap')) {
|
|
248
|
-
storage_type = `storage_map::new()`;
|
|
249
|
+
storage_type = `storage_map::new(b"${key}", ctx)`;
|
|
249
250
|
} else if (
|
|
250
251
|
value.includes('StorageDoubleMap')
|
|
251
252
|
) {
|
|
252
|
-
storage_type = `storage_double_map::new()`;
|
|
253
|
+
storage_type = `storage_double_map::new(b"${key}", ctx)`;
|
|
253
254
|
}
|
|
254
|
-
return `
|
|
255
|
+
return `storage::add_field<${value}>(&mut id, b"${key}", ${storage_type});`;
|
|
255
256
|
})
|
|
256
|
-
.join('')}
|
|
257
|
+
.join('\n')}
|
|
257
258
|
|
|
258
|
-
|
|
259
|
+
Schema { id }
|
|
259
260
|
}
|
|
260
261
|
|
|
261
|
-
public fun migrate(
|
|
262
|
+
public fun migrate(_schema: &mut Schema, _cap: &UpgradeCap, _ctx: &mut TxContext) { }
|
|
262
263
|
|
|
263
|
-
|
|
264
264
|
|
|
265
265
|
// ======================================== View Functions ========================================
|
|
266
|
-
${Object.entries(
|
|
266
|
+
${Object.entries(schemas)
|
|
267
267
|
.map(([key, value]) => {
|
|
268
268
|
// @ts-ignore
|
|
269
269
|
let all_types = value.match(/<(.+)>/)[1].split(',').map(type => type.trim());
|
|
270
270
|
let para_key: string[] = [];
|
|
271
271
|
let para_value = '';
|
|
272
272
|
let borrow_key = '';
|
|
273
|
-
let extra_code = '';
|
|
274
273
|
if (value.includes('StorageValue')) {
|
|
275
274
|
para_key = [];
|
|
276
275
|
para_value = `${all_types[0]}`;
|
|
277
|
-
borrow_key = '
|
|
276
|
+
borrow_key = 'get()';
|
|
278
277
|
} else if (value.includes('StorageMap')) {
|
|
279
278
|
para_key = [`key: ${all_types[0]}`];
|
|
280
279
|
para_value = `${all_types[1]}`;
|
|
281
|
-
borrow_key = '
|
|
282
|
-
if (!value.includes('Balance') && !value.includes('Coin')) {
|
|
283
|
-
extra_code = `public fun get_${key}_keys(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[0]}> {
|
|
284
|
-
self.borrow_${key}().keys()
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
public fun get_${key}_values(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[1]}> {
|
|
288
|
-
self.borrow_${key}().values()
|
|
289
|
-
}`;
|
|
290
|
-
}
|
|
280
|
+
borrow_key = 'get(key)';
|
|
291
281
|
} else if (value.includes('StorageDoubleMap')) {
|
|
292
282
|
para_key = [`key1: ${all_types[0]}`, `key2: ${all_types[1]}`];
|
|
293
283
|
para_value = `${all_types[2]}`;
|
|
294
|
-
borrow_key = '
|
|
295
|
-
if (!value.includes('Balance') && !value.includes('Coin')) {
|
|
296
|
-
extra_code = `public fun get_${key}_keys(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : (vector<${all_types[0]}>, vector<${all_types[1]}>) {
|
|
297
|
-
self.borrow_${key}().keys()
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
public fun get_${key}_values(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[2]}> {
|
|
301
|
-
self.borrow_${key}().values()
|
|
302
|
-
}`;
|
|
303
|
-
}
|
|
284
|
+
borrow_key = 'get(key1, key2)';
|
|
304
285
|
}
|
|
305
|
-
return `public fun get_${key}(self:
|
|
286
|
+
return `public fun get_${key}(self: &Schema, ${para_key}) : &${para_value} {
|
|
306
287
|
self.borrow_${key}().${borrow_key}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
})
|
|
310
|
-
.join('')}
|
|
288
|
+
}`
|
|
289
|
+
}).join('\n')}
|
|
311
290
|
// =========================================================================================================
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
}`;
|
|
291
|
+
}`
|
|
315
292
|
await formatAndWriteMove(
|
|
316
293
|
schemaMoudle,
|
|
317
|
-
`${path}/contracts/${projectName}/sources/codegen/
|
|
294
|
+
`${path}/contracts/${projectName}/sources/codegen/schema.move`,
|
|
318
295
|
'formatAndWriteMove',
|
|
319
296
|
);
|
|
320
|
-
|
|
321
|
-
console.log('ā
Schema Structure Generation Complete\n');
|
|
297
|
+
console.log('ā
Schema Structure Generation Complete\n');
|
|
322
298
|
}
|
|
299
|
+
|
|
@@ -35,24 +35,17 @@ export async function generateDeployHook(
|
|
|
35
35
|
use std::ascii::string;
|
|
36
36
|
use sui::clock::Clock;
|
|
37
37
|
use ${config.name}::dapp_system;
|
|
38
|
-
${importSchemas}
|
|
39
38
|
public entry fun run(clock: &Clock, ctx: &mut TxContext) {
|
|
40
39
|
// Create a dapp.
|
|
41
40
|
let mut dapp = dapp_system::create(string(b"${config.name}"),string(b"${config.description}"), clock , ctx);
|
|
42
41
|
|
|
43
42
|
// Create schemas
|
|
44
|
-
|
|
45
|
-
return `let mut ${schemaName} = ${config.name}::${schemaName}_schema::create(ctx);`;
|
|
46
|
-
}).join('\n')}
|
|
43
|
+
let mut schema = ${config.name}::schema::create(ctx);
|
|
47
44
|
// Logic that needs to be automated once the contract is deployed
|
|
48
45
|
{
|
|
49
46
|
};
|
|
50
47
|
// Authorize schemas and public share objects
|
|
51
|
-
|
|
52
|
-
return `
|
|
53
|
-
dapp.add_schema<${capitalizeAndRemoveUnderscores(schemaName)}>(${schemaName}, ctx);
|
|
54
|
-
`;
|
|
55
|
-
}).join('\n')}
|
|
48
|
+
dapp.add_schema(schema);
|
|
56
49
|
sui::transfer::public_share_object(dapp);
|
|
57
50
|
}
|
|
58
51
|
}` } else {
|
|
@@ -61,15 +54,12 @@ export async function generateDeployHook(
|
|
|
61
54
|
use std::ascii::string;
|
|
62
55
|
use sui::clock::Clock;
|
|
63
56
|
use ${config.name}::dapp_system;
|
|
64
|
-
${importSchemas}
|
|
65
57
|
public entry fun run(clock: &Clock, ctx: &mut TxContext) {
|
|
66
58
|
// Create a dapp.
|
|
67
59
|
let mut dapp = dapp_system::create(string(b"${config.name}"),string(b"${config.description}"), clock , ctx);
|
|
68
60
|
|
|
69
61
|
// Create schemas
|
|
70
|
-
${
|
|
71
|
-
return `let mut ${schemaName} = ${config.name}::${schemaName}_schema::create(ctx);`;
|
|
72
|
-
}).join('\n')}
|
|
62
|
+
let mut schema = ${config.name}::schema::create(ctx);
|
|
73
63
|
// Logic that needs to be automated once the contract is deployed
|
|
74
64
|
|
|
75
65
|
{
|
|
@@ -77,11 +67,7 @@ export async function generateDeployHook(
|
|
|
77
67
|
};
|
|
78
68
|
|
|
79
69
|
// Authorize schemas and public share objects
|
|
80
|
-
|
|
81
|
-
return `
|
|
82
|
-
dapp.add_schema<${capitalizeAndRemoveUnderscores(schemaName)}>(${schemaName}, ctx);
|
|
83
|
-
`;
|
|
84
|
-
}).join('\n')}
|
|
70
|
+
dapp.add_schema(schema);
|
|
85
71
|
sui::transfer::public_share_object(dapp);
|
|
86
72
|
}
|
|
87
73
|
}`
|