@0xobelisk/sui-common 1.1.6 → 1.1.7

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,110 +1,99 @@
1
- import {BaseType, EventData, SchemaData, SchemaType} from '../../types';
1
+ import { BaseType, EventData, SchemaData, SchemaType } from '../../types';
2
2
  import { formatAndWriteMove } from '../formatAndWrite';
3
3
  import {
4
- getStructAttrsWithType,
5
- getStructAttrs,
6
- getStructTypes,
7
- getStructAttrsQuery,
4
+ getStructAttrsWithType,
5
+ getStructAttrs,
6
+ getStructTypes,
7
+ getStructAttrsQuery
8
8
  } from './common';
9
9
 
10
- function capitalizeAndRemoveUnderscores(input: string): string {
11
- return input
12
- .split('_')
13
- .map((word, index) => {
14
- return index === 0
15
- ? word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
16
- : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
17
- })
18
- .join('');
10
+ // account_not_found => AccountNotFound,
11
+ function toPascalCase(str: string): string {
12
+ return str
13
+ .split('_')
14
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
15
+ .join('');
19
16
  }
20
17
 
21
18
  function convertToSnakeCase(input: string): string {
22
- return input
23
- .replace(/([A-Z])/g, '_$1')
24
- .toLowerCase()
25
- .replace(/^_/, '');
19
+ return input
20
+ .replace(/([A-Z])/g, '_$1')
21
+ .toLowerCase()
22
+ .replace(/^_/, '');
26
23
  }
27
24
 
28
- function generateImport(
29
- projectName: string,
30
- data: Record<string, SchemaData> | null,
31
- ) {
32
- if (data != null) {
33
- const names = Object.keys(data);
34
- return names
35
- .map(name => {
36
- return `use ${projectName}::${projectName}_${convertToSnakeCase(
37
- name,
38
- )}::${name};`;
39
- })
40
- .join('\n');
41
- } else {
42
- return '';
43
- }
25
+ function generateImport(projectName: string, data: Record<string, SchemaData> | null) {
26
+ if (data != null) {
27
+ const names = Object.keys(data);
28
+ return names
29
+ .map((name) => {
30
+ return `use ${projectName}::${projectName}_${convertToSnakeCase(name)}::${name};`;
31
+ })
32
+ .join('\n');
33
+ } else {
34
+ return '';
35
+ }
44
36
  }
45
37
 
46
38
  export async function generateSchemaEvent(
47
- projectName: string,
48
- data: Record<string, SchemaData> | null,
49
- events: Record<string, EventData>,
50
- path: string
39
+ projectName: string,
40
+ data: Record<string, SchemaData> | null,
41
+ events: Record<string, EventData>,
42
+ path: string
51
43
  ) {
52
- console.log('\nšŸ“¦ Starting Schema Event Generation...');
53
- for (const key of Object.keys(events)) {
54
- const name = key;
55
- const fields = events[key];
56
- console.log(
57
- ` └─ Generating ${name} event: ${fields}`
58
- );
44
+ console.log('\nšŸ“¦ Starting Schema Event Generation...');
45
+ for (const key of Object.keys(events)) {
46
+ const name = key;
47
+ const fields = events[key];
48
+ console.log(` └─ Generating ${name} event: ${fields}`);
59
49
 
60
- let code = `module ${projectName}::${projectName}_${convertToSnakeCase(name)}_event {
50
+ let code = `module ${projectName}::${projectName}_${name}_event {
61
51
  use sui::event;
62
52
  use std::ascii::String;
63
53
  ${generateImport(projectName, data)}
64
54
 
65
- public struct ${name}Event has copy, drop {
55
+ public struct ${toPascalCase(name)}Event has copy, drop {
66
56
  ${getStructAttrsWithType(fields as Record<string, string>)}
67
57
  }
68
58
 
69
- public fun new(${getStructAttrsWithType(fields as Record<string, string>)}): ${name}Event {
70
- ${name}Event {
59
+ public fun new(${getStructAttrsWithType(fields as Record<string, string>)}): ${toPascalCase(name)}Event {
60
+ ${toPascalCase(name)}Event {
71
61
  ${getStructAttrs(fields as Record<string, string>)}
72
62
  }
73
63
  }
74
64
  }`;
75
- await formatAndWriteMove(
76
- code,
77
- `${path}/contracts/${projectName}/sources/codegen/data/${convertToSnakeCase(
78
- name
79
- )}_event.move`,
80
- 'formatAndWriteMove'
81
- );
82
- }
65
+ await formatAndWriteMove(
66
+ code,
67
+ `${path}/contracts/${projectName}/sources/codegen/data/${name}_event.move`,
68
+ 'formatAndWriteMove'
69
+ );
70
+ }
83
71
 
84
- let code = `module ${projectName}::${projectName}_events {
72
+ let code = `module ${projectName}::${projectName}_events {
85
73
  use std::ascii::{String, string};
86
74
  ${generateImport(projectName, data)}
87
- ${Object.entries(events).map(([name, fields]) => {
88
- return `
89
- use ${projectName}::${projectName}_${convertToSnakeCase(name)}_event::${name}Event;
90
- use ${projectName}::${projectName}_${convertToSnakeCase(name)}_event;
91
- public fun ${convertToSnakeCase(name)}_event(${getStructAttrsWithType(fields as Record<string, string>)}) {
92
- dubhe::storage_event::emit_set_record<${name}Event, ${name}Event, ${name}Event>(
93
- string(b"${convertToSnakeCase(name)}_event"),
75
+ ${Object.entries(events)
76
+ .map(([name, fields]) => {
77
+ return `
78
+ use ${projectName}::${projectName}_${name}_event::${toPascalCase(name)}Event;
79
+ use ${projectName}::${projectName}_${name}_event;
80
+ public fun ${name}_event(${getStructAttrsWithType(fields as Record<string, string>)}) {
81
+ dubhe::storage_event::emit_set_record<${toPascalCase(name)}Event, ${toPascalCase(name)}Event, ${toPascalCase(name)}Event>(
82
+ string(b"${name}_event"),
94
83
  option::none(),
95
84
  option::none(),
96
- option::some(${projectName}_${convertToSnakeCase(name)}_event::new(${getStructAttrs(fields as Record<string, string>)}))
85
+ option::some(${projectName}_${name}_event::new(${getStructAttrs(fields as Record<string, string>)}))
97
86
  )
98
87
  }
99
- `
100
- }).join('\n')}
101
- }`
88
+ `;
89
+ })
90
+ .join('\n')}
91
+ }`;
102
92
 
103
-
104
- await formatAndWriteMove(
105
- code,
106
- `${path}/contracts/${projectName}/sources/codegen/events.move`,
107
- 'formatAndWriteMove'
108
- );
109
- console.log('āœ… Schema Event Generation Complete\n');
110
- }
93
+ await formatAndWriteMove(
94
+ code,
95
+ `${path}/contracts/${projectName}/sources/codegen/events.move`,
96
+ 'formatAndWriteMove'
97
+ );
98
+ console.log('āœ… Schema Event Generation Complete\n');
99
+ }
@@ -3,16 +3,11 @@ import { formatAndWriteMove } from '../formatAndWrite';
3
3
  import { existsSync } from 'fs';
4
4
  import { capitalizeAndRemoveUnderscores } from './generateSchema';
5
5
 
6
- export async function generateInit(
7
- config: DubheConfig,
8
- srcPrefix: string
9
- ) {
10
- console.log('\nšŸ“ Starting Init Generation...');
11
- console.log(
12
- ` └─ Output path: ${srcPrefix}/contracts/${config.name}/sources/tests/init.move`
13
- );
6
+ export async function generateInit(config: DubheConfig, srcPrefix: string) {
7
+ console.log('\nšŸ“ Starting Init Generation...');
8
+ console.log(` └─ Output path: ${srcPrefix}/contracts/${config.name}/sources/tests/init.move`);
14
9
 
15
- let init_test_code = `module ${config.name}::${config.name}_init_test {
10
+ let init_test_code = `module ${config.name}::${config.name}_init_test {
16
11
  use ${config.name}::${config.name}_dapp_schema::Dapp;
17
12
  use sui::clock;
18
13
  use sui::test_scenario;
@@ -30,13 +25,13 @@ export async function generateInit(
30
25
  }
31
26
  }
32
27
  `;
33
- await formatAndWriteMove(
34
- init_test_code,
35
- `${srcPrefix}/contracts/${config.name}/sources/tests/init.move`,
36
- 'formatAndWriteMove'
37
- );
28
+ await formatAndWriteMove(
29
+ init_test_code,
30
+ `${srcPrefix}/contracts/${config.name}/sources/tests/init.move`,
31
+ 'formatAndWriteMove'
32
+ );
38
33
 
39
- let init_code = `module ${config.name}::${config.name}_genesis {
34
+ let init_code = `module ${config.name}::${config.name}_genesis {
40
35
  use std::ascii::string;
41
36
 
42
37
  use sui::clock::Clock;
@@ -56,11 +51,11 @@ export async function generateInit(
56
51
  }
57
52
  }
58
53
  `;
59
- await formatAndWriteMove(
60
- init_code,
61
- `${srcPrefix}/contracts/${config.name}/sources/codegen/genesis.move`,
62
- 'formatAndWriteMove'
63
- );
54
+ await formatAndWriteMove(
55
+ init_code,
56
+ `${srcPrefix}/contracts/${config.name}/sources/codegen/genesis.move`,
57
+ 'formatAndWriteMove'
58
+ );
64
59
 
65
- console.log('āœ… Init Generation Complete\n');
66
- }
60
+ console.log('āœ… Init Generation Complete\n');
61
+ }