@0xobelisk/sui-common 0.5.9 → 0.5.10

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.
@@ -1,4 +1,4 @@
1
- import { BaseType, SchemaMapType, BaseValueType, MoveType } from "../../types";
1
+ import { BaseType, SchemaType, BaseValueType, MoveType } from "../../types";
2
2
  import fs from "fs";
3
3
 
4
4
  export function deleteFolderRecursive(path: string) {
@@ -19,68 +19,6 @@ export function capitalizeFirstLetter(input: string): string {
19
19
  return input.charAt(0).toUpperCase() + input.slice(1);
20
20
  }
21
21
 
22
- /**
23
- * Convert snake_case to camelCase
24
- * @param str
25
- */
26
- export function convertToCamelCase(str: string): string {
27
- str = str.charAt(0).toUpperCase() + str.slice(1);
28
- let result = str.replace(/(_\w)/g, (match) => match[1].toUpperCase());
29
- return result + "Data";
30
- }
31
-
32
- /**
33
- *
34
- * @param name
35
- * @param values
36
- * @return [use name::name_schema, use name::info_schema]
37
- */
38
- export function getUseSchema(
39
- name: string,
40
- values: Record<string, SchemaMapType>
41
- ): string[] {
42
- let schema: string[] = [];
43
- Object.entries(values).forEach(([key, value]) => {
44
- if (typeof value === "object" && value.ephemeral) {
45
- } else {
46
- schema.push(`\tuse ${name}::${key}_schema;`);
47
- }
48
- });
49
- return schema;
50
- }
51
-
52
- /**
53
- * @param values
54
- * @return [ name_schema::register(&mut _obelisk_world, ctx) ,info_schema::register(&mut _obelisk_world, ctx) ]
55
- */
56
- export function getRegisterSchema(
57
- values: Record<string, SchemaMapType>
58
- ): string[] {
59
- let registers: string[] = [];
60
- Object.entries(values).forEach(([key, value]) => {
61
- if (typeof value === "object" && value.ephemeral) {
62
- } else {
63
- registers.push(
64
- `\t\t${key}_schema::register(&mut _obelisk_world, &admin_cap, ctx);`
65
- );
66
- }
67
- });
68
- return registers;
69
- }
70
-
71
- /**
72
- *
73
- * @param name
74
- * @param values
75
- * @return [ package name::name_system, package name::info_system ]
76
- */
77
- export function getpackageSystem(name: string, values: string[]): string {
78
- return (
79
- values.map((key) => `\tpackage ${name}::${key};`).join("\n") +
80
- `\n\tpackage ${name}::deploy_hook;`
81
- );
82
- }
83
-
84
22
  /**
85
23
  *
86
24
  * @param values
@@ -89,11 +27,8 @@ export function getpackageSystem(name: string, values: string[]): string {
89
27
  */
90
28
  export function getStructAttrs(
91
29
  values: Record<string, string> | string,
92
- prefixArgs: string
93
- ): string[] {
94
- return typeof values === "string"
95
- ? [`${prefixArgs}value`]
96
- : Object.entries(values).map(([key, _]) => `${prefixArgs}${key}`);
30
+ ): string {
31
+ return Object.entries(values).map(([key, _]) => `${key}`).join(",");
97
32
  }
98
33
 
99
34
  function isAddress(str: string): boolean {
@@ -101,136 +36,12 @@ function isAddress(str: string): boolean {
101
36
  return regex.test(str);
102
37
  }
103
38
 
104
- export function getStructInitValue(
105
- keys: BaseType | Record<string, BaseType>,
106
- values: BaseValueType | Record<string, BaseValueType>
107
- ) {
108
- if (
109
- typeof values === "string" ||
110
- typeof values === "boolean" ||
111
- typeof values === "number"
112
- ) {
113
- if (keys === "string") {
114
- return [`string(b"${values}")`];
115
- }
116
- if (typeof values === "string") {
117
- if (isAddress(values)) {
118
- return [`@${values}`];
119
- }
120
- }
121
- return [`${values}`];
122
- } else if (Array.isArray(values)) {
123
- // Check the array element type
124
- if (values.length > 0) {
125
- if (
126
- typeof values[0] === "string" ||
127
- typeof values[0] === "boolean" ||
128
- typeof values[0] === "number"
129
- ) {
130
- if (keys === "vector<string>") {
131
- return [`vector[${values.map((item) => `string(b"${item}")`)}]`];
132
- }
133
-
134
- if (typeof values[0] === "string") {
135
- if (isAddress(values[0])) {
136
- return [`vector[${values.map((item) => `@${item}`)}]`];
137
- }
138
- }
139
- return [`vector[${values.map((item) => `${item}`)}]`];
140
- } else if (typeof values === "object") {
141
- let res = `vector[${values.map((item: any) => {
142
- return `vector[${item.map((data: any) => {
143
- return `${data}`;
144
- })}]`;
145
- })}]`;
146
-
147
- return [res];
148
- }
149
- } else {
150
- if (keys === "vector<string>") {
151
- return 'vector[string(b"")]';
152
- }
153
- return "vector[]";
154
- }
155
- } else if (typeof values === "object") {
156
- // It's an object, handle accordingly
157
- let res = Object.entries(values).map(([key, value]) => {
158
- if (
159
- typeof value === "string" ||
160
- typeof value === "boolean" ||
161
- typeof value === "number"
162
- ) {
163
- if (typeof keys === "string") {
164
- if (keys === "string") {
165
- return `string(b"${value}")`;
166
- }
167
- } else {
168
- if (keys[key] === "string") {
169
- return `string(b"${value}")`;
170
- }
171
- }
172
-
173
- if (typeof value === "string") {
174
- if (isAddress(value)) {
175
- return `@${value}`;
176
- }
177
- }
178
- return `${value}`;
179
- } else if (Array.isArray(value)) {
180
- // Check the array element type
181
- if (value.length > 0) {
182
- if (
183
- typeof value[0] === "string" ||
184
- typeof value[0] === "boolean" ||
185
- typeof value[0] === "number"
186
- ) {
187
- if (typeof keys === "string") {
188
- if (keys === "vector<string>") {
189
- return `vector[${value.map((item) => `string(b"${item}")`)}]`;
190
- }
191
- } else {
192
- if (keys[key] === "vector<string>") {
193
- return `vector[${value.map((item) => `string(b"${item}")`)}]`;
194
- }
195
- }
196
-
197
- if (typeof value[0] === "string") {
198
- if (isAddress(value[0])) {
199
- return `vector[${value.map((item) => `@${item}`)}]`;
200
- }
201
- }
202
- return `vector[${value.map((item) => `${item}`)}]`;
203
- } else if (typeof value === "object") {
204
- let res = `vector[${value.map((item: any) => {
205
- return `vector[${item.map((data: any) => {
206
- return `${data}`;
207
- })}]`;
208
- })}]`;
209
-
210
- return res;
211
- }
212
- } else {
213
- if (typeof keys !== "string") {
214
- if (keys[key] === "vector<string>") {
215
- return 'vector[string(b"")]';
216
- }
217
- return "vector[]";
218
- }
219
- }
220
- }
221
- });
222
- return res;
223
- }
224
- // Handle other cases or return an empty array if type not recognized
225
- return [];
226
- }
227
-
228
39
  /**
229
40
  *
230
41
  * @param values
231
42
  * @return ( bool , u64 , u64)
232
43
  */
233
- // export function getStructTypes(values: SchemaMapType): string {
44
+ // export function getStructTypes(values: SchemaType): string {
234
45
  export function getStructTypes(
235
46
  values: MoveType | Record<string, MoveType>
236
47
  ): string {
@@ -245,12 +56,9 @@ export function getStructTypes(
245
56
  * @return Attributes and types of the struct. [ name: string, age: u64 ]
246
57
  */
247
58
  export function getStructAttrsWithType(
248
- values: Record<string, string> | string,
249
- prefix: string
59
+ values: Record<string, string>
250
60
  ): string[] {
251
- return typeof values === "string"
252
- ? [`${prefix}value: ${values}`]
253
- : Object.entries(values).map(([key, type]) => `${prefix}${key}: ${type}`);
61
+ return Object.entries(values).map(([key, type]) => `${key}: ${type}`);
254
62
  }
255
63
 
256
64
  /**
@@ -263,229 +71,8 @@ export function getStructAttrsQuery(
263
71
  prefixArgs: string
264
72
  ): string[] {
265
73
  return typeof values === "string"
266
- ? [`${prefixArgs}_obelisk_data.value`]
74
+ ? [`${prefixArgs}self.value`]
267
75
  : Object.entries(values).map(
268
- ([key, _]) => `${prefixArgs}_obelisk_data.${key}`
76
+ ([key, _]) => `${prefixArgs}self.${key}`
269
77
  );
270
- }
271
-
272
- export function renderKeyName(values: Record<string, string> | string): string {
273
- return `\t${getStructAttrs(values, "// ").join("\n\t")}`;
274
- }
275
-
276
- export function renderStruct(
277
- structName: string,
278
- values: Record<string, string> | string,
279
- isEphemeral: boolean = false
280
- ): string {
281
- return `\tpublic struct ${structName} has copy, drop ${
282
- isEphemeral ? "" : ", store"
283
- } {
284
- ${getStructAttrsWithType(values, "\t\t").join(",\n")}
285
- \t}\n`;
286
- }
287
-
288
- export function renderNewStructFunc(
289
- structName: string,
290
- values: Record<string, string> | string
291
- ): string {
292
- return `\tpublic fun new(${getStructAttrsWithType(values, "").join(
293
- ", "
294
- )}): ${structName} {
295
- \t\t${structName} {
296
- ${getStructAttrs(values, "\t\t\t").join(", \n")}
297
- \t\t}
298
- \t}\n`;
299
- }
300
-
301
- export function renderRegisterFunc(structName: string): string {
302
- return `\tpublic fun register(_obelisk_world: &mut World, admin_cap: &AdminCap, ctx: &mut TxContext) {
303
- \t\tschema::add<Table<address,${structName}>>(_obelisk_world, SCHEMA_ID, table::new<address, ${structName}>(ctx), admin_cap);
304
- \t}`;
305
- }
306
-
307
- export function renderSetFunc(
308
- structName: string,
309
- values: Record<string, string> | string
310
- ): string {
311
- return `\tpublic(package) fun set(_obelisk_world: &mut World, _obelisk_entity_key: address, ${getStructAttrsWithType(
312
- values,
313
- " "
314
- )}) {
315
- \t\tlet _obelisk_schema = schema::get_mut<Table<address,${structName}>, AppKey>(app_key::new(), _obelisk_world, SCHEMA_ID);
316
- \t\tlet _obelisk_data = new(${getStructAttrs(values, " ")});
317
- \t\tif(table::contains<address, ${structName}>(_obelisk_schema, _obelisk_entity_key)) {
318
- \t\t\t*table::borrow_mut<address, ${structName}>(_obelisk_schema, _obelisk_entity_key) = _obelisk_data;
319
- \t\t} else {
320
- \t\t\ttable::add(_obelisk_schema, _obelisk_entity_key, _obelisk_data);
321
- \t\t};
322
- \t\tevents::emit_set(SCHEMA_ID, SCHEMA_TYPE, some(_obelisk_entity_key), _obelisk_data)
323
- \t}
324
- `;
325
- }
326
-
327
- export function renderRemoveFunc(structName: string): string {
328
- return `\tpublic(package) fun remove(_obelisk_world: &mut World, _obelisk_entity_key: address) {
329
- \t\tlet _obelisk_schema = schema::get_mut<Table<address,${structName}>, AppKey>(app_key::new(),_obelisk_world, SCHEMA_ID);
330
- \t\tassert!(table::contains<address, ${structName}>(_obelisk_schema, _obelisk_entity_key), EEntityDoesNotExist);
331
- \t\ttable::remove(_obelisk_schema, _obelisk_entity_key);
332
- \t\tevents::emit_remove(SCHEMA_ID, _obelisk_entity_key)
333
- \t}
334
- `;
335
- }
336
-
337
- export function renderSetAttrsFunc(
338
- structName: string,
339
- struct: MoveType | Record<string, MoveType>
340
- ): string {
341
- return typeof struct === "string"
342
- ? ""
343
- : "\n" +
344
- Object.entries(struct)
345
- .map(
346
- ([key, type]) =>
347
- `\tpublic(package) fun set_${key}(_obelisk_world: &mut World, _obelisk_entity_key: address, ${key}: ${type}) {
348
- \t\tlet _obelisk_schema = schema::get_mut<Table<address,${structName}>, AppKey>(app_key::new(),_obelisk_world, SCHEMA_ID);
349
- \t\tassert!(table::contains<address, ${structName}>(_obelisk_schema, _obelisk_entity_key), EEntityDoesNotExist);
350
- \t\tlet _obelisk_data = table::borrow_mut<address, ${structName}>(_obelisk_schema, _obelisk_entity_key);
351
- \t\t_obelisk_data.${key} = ${key};
352
- \t\tevents::emit_set(SCHEMA_ID, SCHEMA_TYPE, some(_obelisk_entity_key), *_obelisk_data)
353
- \t}
354
- `
355
- )
356
- .join("\n");
357
- }
358
-
359
- export function renderGetAllFunc(
360
- structName: string,
361
- struct: MoveType | Record<string, MoveType>
362
- ): string {
363
- return `\tpublic fun get(_obelisk_world: &World, _obelisk_entity_key: address): ${getStructTypes(
364
- struct
365
- )} {
366
- \t\tlet _obelisk_schema = schema::get<Table<address,${structName}>>(_obelisk_world, SCHEMA_ID);
367
- \t\tassert!(table::contains<address, ${structName}>(_obelisk_schema, _obelisk_entity_key), EEntityDoesNotExist);
368
- \t\tlet _obelisk_data = table::borrow<address, ${structName}>(_obelisk_schema, _obelisk_entity_key);
369
- \t\t(
370
- ${getStructAttrsQuery(struct, "\t\t\t").join(",\n")}
371
- \t\t)
372
- \t}
373
- `;
374
- }
375
-
376
- export function renderGetAttrsFunc(
377
- structName: string,
378
- struct: MoveType | Record<string, MoveType>
379
- ): string {
380
- return typeof struct === "string"
381
- ? ""
382
- : "\n" +
383
- Object.entries(struct)
384
- .map(
385
- ([
386
- key,
387
- type,
388
- ]) => `\tpublic fun get_${key}(_obelisk_world: &World, _obelisk_entity_key: address): ${type} {
389
- \t\tlet _obelisk_schema = schema::get<Table<address,${structName}>>(_obelisk_world, SCHEMA_ID);
390
- \t\tassert!(table::contains<address, ${structName}>(_obelisk_schema, _obelisk_entity_key), EEntityDoesNotExist);
391
- \t\tlet _obelisk_data = table::borrow<address, ${structName}>(_obelisk_schema, _obelisk_entity_key);
392
- \t\t_obelisk_data.${key}
393
- \t}
394
- `
395
- )
396
- .join("\n");
397
- }
398
-
399
- export function renderContainFunc(structName: string): string {
400
- return `\tpublic fun contains(_obelisk_world: &World, _obelisk_entity_key: address): bool {
401
- \t\tlet _obelisk_schema = schema::get<Table<address,${structName}>>(_obelisk_world, SCHEMA_ID);
402
- \t\ttable::contains<address, ${structName}>(_obelisk_schema, _obelisk_entity_key)
403
- \t}`;
404
- }
405
-
406
- export function renderRegisterFuncWithInit(
407
- structName: string,
408
- valueType: BaseType | Record<string, BaseType>,
409
- defaultValue: BaseValueType | Record<string, BaseValueType>
410
- ): string {
411
- return `\tpublic fun register(_obelisk_world: &mut World, admin_cap: &AdminCap, _ctx: &mut TxContext) {
412
- \t\tlet _obelisk_schema = new(${getStructInitValue(valueType, defaultValue)});
413
- \t\tschema::add<${structName}>(_obelisk_world, SCHEMA_ID, _obelisk_schema, admin_cap);
414
- \t\tevents::emit_set(SCHEMA_ID, SCHEMA_TYPE, none(), _obelisk_schema);
415
- \t}`;
416
- }
417
-
418
- export function renderSingleSetFunc(
419
- structName: string,
420
- values: Record<string, string> | string
421
- ): string {
422
- return `\tpublic(package) fun set(_obelisk_world: &mut World, ${getStructAttrsWithType(
423
- values,
424
- " "
425
- )}) {
426
- \t\tlet _obelisk_schema = schema::get_mut<${structName}, AppKey>(app_key::new(),_obelisk_world, SCHEMA_ID);
427
- ${
428
- typeof values === "string"
429
- ? `\t\t_obelisk_schema.value = value;
430
- events::emit_set(SCHEMA_ID, SCHEMA_TYPE, none(), _obelisk_schema.value);`
431
- : `\t\tlet _obelisk_data = new(${getStructAttrs(values, " ")});
432
- *_obelisk_schema = _obelisk_data;
433
- events::emit_set(SCHEMA_ID, SCHEMA_TYPE, none(), _obelisk_data);`
434
- }
435
- \t}`;
436
- }
437
-
438
- export function renderSingleSetAttrsFunc(
439
- structName: string,
440
- struct: MoveType | Record<string, MoveType>
441
- ): string {
442
- return typeof struct === "string"
443
- ? ""
444
- : "\n" +
445
- Object.entries(struct)
446
- .map(
447
- ([key, type]) => `
448
- \tpublic(package) fun set_${key}(_obelisk_world: &mut World, ${key}: ${type}) {
449
- \t\tlet _obelisk_schema = schema::get_mut<${structName}, AppKey>(app_key::new(),_obelisk_world, SCHEMA_ID);
450
- \t\t_obelisk_schema.${key} = ${key};
451
- \t\tevents::emit_set(SCHEMA_ID, SCHEMA_TYPE, none(), *_obelisk_schema)
452
- \t}`
453
- )
454
- .join("\n");
455
- }
456
-
457
- export function renderSingleGetAllFunc(
458
- structName: string,
459
- values: MoveType | Record<string, MoveType>
460
- ): string {
461
- return `\tpublic fun get(_obelisk_world: &World): ${getStructTypes(values)} {
462
- \t\tlet _obelisk_schema = schema::get<${structName}>(_obelisk_world, SCHEMA_ID);
463
- \t\t(
464
- ${
465
- typeof values === "string"
466
- ? `\t\t\t_obelisk_schema.value`
467
- : Object.entries(values)
468
- .map(([key, _]) => `\t\t\t_obelisk_schema.${key},`)
469
- .join("\n")
470
- }
471
- \t\t)
472
- \t}`;
473
- }
474
-
475
- export function renderSingleGetAttrsFunc(
476
- structName: string,
477
- struct: MoveType | Record<string, MoveType>
478
- ): string {
479
- return typeof struct === "string"
480
- ? ""
481
- : "\n" +
482
- Object.entries(struct)
483
- .map(
484
- ([key, type]) => `
485
- \tpublic fun get_${key}(_obelisk_world: &World): ${type} {
486
- \t\tlet _obelisk_schema = schema::get<${structName}>(_obelisk_world, SCHEMA_ID);
487
- \t\t_obelisk_schema.${key}
488
- \t}`
489
- )
490
- .join("\n");
491
- }
78
+ }
@@ -0,0 +1,19 @@
1
+ import { ObeliskConfig } from "../../types";
2
+ import { formatAndWriteMove } from "../formatAndWrite";
3
+
4
+ export async function generateDappKey(config: ObeliskConfig, srcPrefix: string) {
5
+ let code = `module ${config.name}::dapp_key {
6
+ /// Authorization token for the app.
7
+ public struct DappKey has drop {}
8
+
9
+ public(package) fun new(): DappKey {
10
+ DappKey { }
11
+ }
12
+ }
13
+ `;
14
+ await formatAndWriteMove(
15
+ code,
16
+ `${srcPrefix}/contracts/${config.name}/sources/codegen/dapp_key.move`,
17
+ "formatAndWriteMove"
18
+ );
19
+ }