@0xobelisk/sui-common 1.2.0-pre.99 ā 2.0.0
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/LICENSE +92 -0
- package/README.md +670 -1
- package/dist/index.d.ts +61 -27
- package/dist/index.js +1078 -461
- package/dist/index.js.map +1 -1
- package/package.json +15 -17
- package/src/codegen/debug.ts +0 -4
- package/src/codegen/types/index.ts +46 -10
- package/src/codegen/utils/config.ts +1 -1
- package/src/codegen/utils/format.ts +0 -6
- package/src/codegen/utils/formatAndWrite.ts +10 -31
- package/src/codegen/utils/generateLock.ts +122 -0
- package/src/codegen/utils/index.ts +4 -2
- package/src/codegen/utils/renderMove/{schemaGen.ts ā codegen.ts} +40 -28
- package/src/codegen/utils/renderMove/common.ts +0 -65
- package/src/codegen/utils/renderMove/dapp.ts +2 -14
- package/src/codegen/utils/renderMove/generateDappKey.ts +33 -18
- package/src/codegen/utils/renderMove/generateError.ts +32 -15
- package/src/codegen/utils/renderMove/generateGenesis.ts +55 -22
- package/src/codegen/utils/renderMove/generateInitTest.ts +26 -14
- package/src/codegen/utils/renderMove/generateObjects.ts +377 -0
- package/src/codegen/utils/renderMove/generatePermits.ts +151 -0
- package/src/codegen/utils/renderMove/generateResources.ts +894 -242
- package/src/codegen/utils/renderMove/generateScenes.ts +467 -0
- package/src/codegen/utils/renderMove/generateScript.ts +18 -13
- package/src/codegen/utils/renderMove/generateSystem.ts +0 -2
- package/src/codegen/utils/renderMove/generateUserStorageInit.ts +37 -0
- package/src/codegen/utils/validateConfig.ts +237 -0
- package/src/index.ts +0 -1
- package/src/modules.d.ts +0 -10
- package/src/codegen/modules.d.ts +0 -1
- package/src/codegen/utils/posixPath.ts +0 -8
- package/src/codegen/utils/renderMove/generateComponents.ts +0 -802
- package/src/codegen/utils/renderMove/generateDefaultSchema.ts +0 -216
- package/src/codegen/utils/renderMove/generateEvent.ts +0 -99
- package/src/codegen/utils/renderMove/generateSchema.ts +0 -287
- package/src/codegen/utils/renderMove/generateSchemaHub.ts +0 -60
- package/src/parseData/index.ts +0 -1
- package/src/parseData/parser/index.ts +0 -47
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import { DubheConfig } from '../../types';
|
|
2
|
-
import { formatAndWriteMove } from '../formatAndWrite';
|
|
3
|
-
import { existsSync } from 'fs';
|
|
4
|
-
// import { capitalizeAndRemoveUnderscores } from './generateSchema'; // Unused
|
|
5
|
-
|
|
6
|
-
export async function generateDefaultSchema(config: DubheConfig, srcPrefix: string) {
|
|
7
|
-
await generateDappSchemaMetadata(config, srcPrefix);
|
|
8
|
-
await generateDappSystem(config, srcPrefix);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
async function generateDappSchemaMetadata(config: DubheConfig, srcPrefix: string) {
|
|
12
|
-
const path = `${srcPrefix}/src/${config.name}/sources/codegen/core/metadata.move`;
|
|
13
|
-
if (!existsSync(path)) {
|
|
14
|
-
let code = `module ${config.name}::${config.name}_dapp_metadata {
|
|
15
|
-
use std::ascii::String;
|
|
16
|
-
|
|
17
|
-
public struct DappMetadata has drop, copy, store {
|
|
18
|
-
name: String,
|
|
19
|
-
description: String,
|
|
20
|
-
cover_url: vector<String>,
|
|
21
|
-
website_url: String,
|
|
22
|
-
created_at: u64,
|
|
23
|
-
partners: vector<String>,
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public fun new(
|
|
27
|
-
name: String,
|
|
28
|
-
description: String,
|
|
29
|
-
cover_url: vector<String>,
|
|
30
|
-
website_url: String,
|
|
31
|
-
created_at: u64,
|
|
32
|
-
partners: vector<String>,
|
|
33
|
-
): DappMetadata {
|
|
34
|
-
DappMetadata {
|
|
35
|
-
name,
|
|
36
|
-
description,
|
|
37
|
-
cover_url,
|
|
38
|
-
website_url,
|
|
39
|
-
created_at,
|
|
40
|
-
partners,
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public fun set(
|
|
45
|
-
self: &mut DappMetadata,
|
|
46
|
-
name: String,
|
|
47
|
-
description: String,
|
|
48
|
-
cover_url: vector<String>,
|
|
49
|
-
website_url: String,
|
|
50
|
-
created_at: u64,
|
|
51
|
-
partners: vector<String>,
|
|
52
|
-
) {
|
|
53
|
-
self.name = name;
|
|
54
|
-
self.description = description;
|
|
55
|
-
self.cover_url = cover_url;
|
|
56
|
-
self.website_url = website_url;
|
|
57
|
-
self.created_at = created_at;
|
|
58
|
-
self.partners = partners;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public fun set_name(self: &mut DappMetadata, name: String) {
|
|
62
|
-
self.name = name;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public fun set_description(self: &mut DappMetadata, description: String) {
|
|
66
|
-
self.description = description;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
public fun set_cover_url(self: &mut DappMetadata, cover_url: vector<String>) {
|
|
70
|
-
self.cover_url = cover_url;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
public fun set_website_url(self: &mut DappMetadata, website_url: String) {
|
|
74
|
-
self.website_url = website_url;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public fun set_created_at(self: &mut DappMetadata, created_at: u64) {
|
|
78
|
-
self.created_at = created_at;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
public fun set_partners(self: &mut DappMetadata, partners: vector<String>) {
|
|
82
|
-
self.partners = partners;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
public fun get_name(self: &DappMetadata): String {
|
|
86
|
-
self.name
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public fun get_description(self: &DappMetadata): String {
|
|
90
|
-
self.description
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public fun get_cover_url(self: &DappMetadata): vector<String> {
|
|
94
|
-
self.cover_url
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public fun get_website_url(self: &DappMetadata): String {
|
|
98
|
-
self.website_url
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public fun get_created_at(self: &DappMetadata): u64 {
|
|
102
|
-
self.created_at
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public fun get_partners(self: &DappMetadata): vector<String> {
|
|
106
|
-
self.partners
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
`;
|
|
110
|
-
await formatAndWriteMove(code, path, 'formatAndWriteMove');
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async function generateDappSystem(config: DubheConfig, srcPrefix: string) {
|
|
115
|
-
const path = `${srcPrefix}/src/${config.name}/sources/codegen/core/system.move`;
|
|
116
|
-
if (!existsSync(path)) {
|
|
117
|
-
let code = `module ${config.name}::${config.name}_dapp_system {
|
|
118
|
-
use std::ascii::String;
|
|
119
|
-
use std::ascii;
|
|
120
|
-
use dubhe::type_info;
|
|
121
|
-
use sui::clock::Clock;
|
|
122
|
-
use sui::transfer::public_share_object;
|
|
123
|
-
use ${config.name}::${config.name}_schema::Schema;
|
|
124
|
-
use ${config.name}::${config.name}_dapp_metadata;
|
|
125
|
-
use ${config.name}::${config.name}_dapp_metadata::DappMetadata;
|
|
126
|
-
use dubhe::storage::add_field;
|
|
127
|
-
use dubhe::storage_value;
|
|
128
|
-
use dubhe::storage_value::StorageValue;
|
|
129
|
-
|
|
130
|
-
public entry fun set_metadata(
|
|
131
|
-
schema: &mut Schema,
|
|
132
|
-
name: String,
|
|
133
|
-
description: String,
|
|
134
|
-
cover_url: vector<String>,
|
|
135
|
-
website_url: String,
|
|
136
|
-
partners: vector<String>,
|
|
137
|
-
ctx: &TxContext,
|
|
138
|
-
) {
|
|
139
|
-
let admin = schema.dapp__admin().try_get();
|
|
140
|
-
assert!(admin == option::some(ctx.sender()), 0);
|
|
141
|
-
let created_at = schema.dapp__metadata().get().get_created_at();
|
|
142
|
-
schema.dapp__metadata().set(
|
|
143
|
-
${config.name}_dapp_metadata::new(
|
|
144
|
-
name,
|
|
145
|
-
description,
|
|
146
|
-
cover_url,
|
|
147
|
-
website_url,
|
|
148
|
-
created_at,
|
|
149
|
-
partners
|
|
150
|
-
)
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
public entry fun transfer_ownership(schema: &mut Schema, new_admin: address, ctx: &mut TxContext) {
|
|
156
|
-
let admin = schema.dapp__admin().try_get();
|
|
157
|
-
assert!(admin == option::some(ctx.sender()), 0);
|
|
158
|
-
schema.dapp__admin().set(new_admin);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
public entry fun set_safe_mode(schema: &mut Schema, safe_mode: bool, ctx: &TxContext) {
|
|
162
|
-
let admin = schema.dapp__admin().try_get();
|
|
163
|
-
assert!(admin == option::some(ctx.sender()), 0);
|
|
164
|
-
schema.dapp__safe_mode().set(safe_mode);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
public fun ensure_no_safe_mode(schema: &mut Schema) {
|
|
168
|
-
assert!(!schema.dapp__safe_mode()[], 0);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
public fun ensure_has_authority(schema: &mut Schema, ctx: &TxContext) {
|
|
172
|
-
assert!(schema.dapp__admin().get() == ctx.sender(), 0);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
public fun ensure_has_schema<S: key + store>(schema: &mut Schema, new_schema: &S) {
|
|
176
|
-
let schema_id = object::id_address(new_schema);
|
|
177
|
-
assert!(schema.dapp__authorised_schemas().get().contains(&schema_id), 0);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
public(package) fun create(schema: &mut Schema, name: String, description: String, clock: &Clock, ctx: &mut TxContext){
|
|
181
|
-
add_field<StorageValue<address>>(schema.id(), b"dapp__admin", storage_value::new(b"dapp__admin", ctx));
|
|
182
|
-
add_field<StorageValue<address>>(schema.id(), b"dapp__package_id", storage_value::new(b"dapp__package_id", ctx));
|
|
183
|
-
add_field<StorageValue<u32>>(schema.id(), b"dapp__version", storage_value::new(b"dapp__version", ctx));
|
|
184
|
-
add_field<StorageValue<DappMetadata>>(schema.id(), b"dapp__metadata", storage_value::new(b"dapp__metadata", ctx));
|
|
185
|
-
add_field<StorageValue<bool>>(schema.id(), b"dapp__safe_mode", storage_value::new(b"dapp__safe_mode", ctx));
|
|
186
|
-
add_field<StorageValue<vector<address>>>(schema.id(), b"dapp__authorised_schemas", storage_value::new(b"dapp__authorised_schemas", ctx));
|
|
187
|
-
schema.dapp__metadata().set(
|
|
188
|
-
${config.name}_dapp_metadata::new(
|
|
189
|
-
name,
|
|
190
|
-
description,
|
|
191
|
-
vector[],
|
|
192
|
-
ascii::string(b""),
|
|
193
|
-
clock.timestamp_ms(),
|
|
194
|
-
vector[]
|
|
195
|
-
)
|
|
196
|
-
);
|
|
197
|
-
let package_id = type_info::current_package_id<Schema>();
|
|
198
|
-
schema.dapp__package_id().set(package_id);
|
|
199
|
-
schema.dapp__admin().set(ctx.sender());
|
|
200
|
-
schema.dapp__version().set(1);
|
|
201
|
-
schema.dapp__safe_mode().set(false);
|
|
202
|
-
schema.dapp__authorised_schemas().set(vector[]);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
public(package) fun add_schema<S: key + store>(schema: &mut Schema, new_schema: S) {
|
|
206
|
-
let mut schemas = schema.dapp__authorised_schemas()[];
|
|
207
|
-
schemas.push_back(object::id_address<S>(&new_schema));
|
|
208
|
-
schema.dapp__authorised_schemas().set(schemas);
|
|
209
|
-
public_share_object(new_schema);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
`;
|
|
214
|
-
await formatAndWriteMove(code, path, 'formatAndWriteMove');
|
|
215
|
-
}
|
|
216
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { EventData, SchemaData } from '../../types';
|
|
2
|
-
import { formatAndWriteMove } from '../formatAndWrite';
|
|
3
|
-
import { getStructAttrsWithType, getStructAttrs } from './common';
|
|
4
|
-
|
|
5
|
-
// account_not_found => AccountNotFound,
|
|
6
|
-
function toPascalCase(str: string): string {
|
|
7
|
-
return str
|
|
8
|
-
.split('_')
|
|
9
|
-
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
|
10
|
-
.join('');
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function convertToSnakeCase(input: string): string {
|
|
14
|
-
return input
|
|
15
|
-
.replace(/([A-Z])/g, '_$1')
|
|
16
|
-
.toLowerCase()
|
|
17
|
-
.replace(/^_/, '');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function generateImport(projectName: string, data: Record<string, SchemaData> | null) {
|
|
21
|
-
if (data != null) {
|
|
22
|
-
const names = Object.keys(data);
|
|
23
|
-
return names
|
|
24
|
-
.map((name) => {
|
|
25
|
-
return `use ${projectName}::${projectName}_${convertToSnakeCase(name)}::${name};`;
|
|
26
|
-
})
|
|
27
|
-
.join('\n');
|
|
28
|
-
} else {
|
|
29
|
-
return '';
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export async function generateSchemaEvent(
|
|
34
|
-
projectName: string,
|
|
35
|
-
data: Record<string, SchemaData> | null,
|
|
36
|
-
events: Record<string, EventData>,
|
|
37
|
-
path: string
|
|
38
|
-
) {
|
|
39
|
-
console.log('\nš¦ Starting Schema Event Generation...');
|
|
40
|
-
for (const key of Object.keys(events)) {
|
|
41
|
-
const name = key;
|
|
42
|
-
const fields = events[key];
|
|
43
|
-
console.log(` āā ${name} event: ${JSON.stringify(fields)}`);
|
|
44
|
-
|
|
45
|
-
let code = `module ${projectName}::${projectName}_${name}_event {
|
|
46
|
-
use sui::event;
|
|
47
|
-
use std::ascii::String;
|
|
48
|
-
${generateImport(projectName, data)}
|
|
49
|
-
|
|
50
|
-
public struct ${toPascalCase(name)}Event has copy, drop {
|
|
51
|
-
${getStructAttrsWithType(fields as Record<string, string>)}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public fun new(${getStructAttrsWithType(
|
|
55
|
-
fields as Record<string, string>
|
|
56
|
-
)}): ${toPascalCase(name)}Event {
|
|
57
|
-
${toPascalCase(name)}Event {
|
|
58
|
-
${getStructAttrs(fields as Record<string, string>)}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}`;
|
|
62
|
-
await formatAndWriteMove(
|
|
63
|
-
code,
|
|
64
|
-
`${path}/src/${projectName}/sources/codegen/data/${name}_event.move`,
|
|
65
|
-
'formatAndWriteMove'
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
let code = `module ${projectName}::${projectName}_events {
|
|
70
|
-
use std::ascii::{String, string};
|
|
71
|
-
${generateImport(projectName, data)}
|
|
72
|
-
${Object.entries(events)
|
|
73
|
-
.map(([name, fields]) => {
|
|
74
|
-
return `
|
|
75
|
-
use ${projectName}::${projectName}_${name}_event::${toPascalCase(name)}Event;
|
|
76
|
-
use ${projectName}::${projectName}_${name}_event;
|
|
77
|
-
public fun ${name}_event(${getStructAttrsWithType(fields as Record<string, string>)}) {
|
|
78
|
-
dubhe::storage_event::emit_set_record<${toPascalCase(name)}Event, ${toPascalCase(
|
|
79
|
-
name
|
|
80
|
-
)}Event, ${toPascalCase(name)}Event>(
|
|
81
|
-
string(b"${name}_event"),
|
|
82
|
-
option::none(),
|
|
83
|
-
option::none(),
|
|
84
|
-
option::some(${projectName}_${name}_event::new(${getStructAttrs(
|
|
85
|
-
fields as Record<string, string>
|
|
86
|
-
)}))
|
|
87
|
-
)
|
|
88
|
-
}
|
|
89
|
-
`;
|
|
90
|
-
})
|
|
91
|
-
.join('\n')}
|
|
92
|
-
}`;
|
|
93
|
-
|
|
94
|
-
await formatAndWriteMove(
|
|
95
|
-
code,
|
|
96
|
-
`${path}/src/${projectName}/sources/codegen/events.move`,
|
|
97
|
-
'formatAndWriteMove'
|
|
98
|
-
);
|
|
99
|
-
}
|
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
import { BaseType, SchemaData, SchemaType } from '../../types';
|
|
2
|
-
import { formatAndWriteMove } from '../formatAndWrite';
|
|
3
|
-
import {
|
|
4
|
-
getStructAttrsWithType,
|
|
5
|
-
getStructAttrs,
|
|
6
|
-
getStructTypes,
|
|
7
|
-
getStructAttrsQuery,
|
|
8
|
-
containsString
|
|
9
|
-
} from './common';
|
|
10
|
-
|
|
11
|
-
function sortByFirstLetter(arr: string[]): string[] {
|
|
12
|
-
return arr.sort((a, b) => {
|
|
13
|
-
const firstLetterA = a.charAt(0).toLowerCase();
|
|
14
|
-
const firstLetterB = b.charAt(0).toLowerCase();
|
|
15
|
-
|
|
16
|
-
if (firstLetterA < firstLetterB) {
|
|
17
|
-
return -1;
|
|
18
|
-
}
|
|
19
|
-
if (firstLetterA > firstLetterB) {
|
|
20
|
-
return 1;
|
|
21
|
-
}
|
|
22
|
-
return 0;
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function capitalizeAndRemoveUnderscores(input: string): string {
|
|
27
|
-
return input
|
|
28
|
-
.split('_')
|
|
29
|
-
.map((word, index) => {
|
|
30
|
-
return index === 0
|
|
31
|
-
? word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
|
|
32
|
-
: word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
|
33
|
-
})
|
|
34
|
-
.join('');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function renderSetAttrsFunc(
|
|
38
|
-
schemaName: string,
|
|
39
|
-
fields: BaseType | Record<string, BaseType>
|
|
40
|
-
): string {
|
|
41
|
-
return Object.entries(fields)
|
|
42
|
-
.map(
|
|
43
|
-
([key, type]) =>
|
|
44
|
-
`public(package) fun set_${key}(self: &mut ${schemaName}, ${key}: ${type}) {
|
|
45
|
-
self.${key} = ${key};
|
|
46
|
-
}`
|
|
47
|
-
)
|
|
48
|
-
.join('\n');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function renderSetFunc(schemaName: string, fields: Record<string, string>): string {
|
|
52
|
-
return `public(package) fun set(self: &mut ${schemaName}, ${getStructAttrsWithType(fields)}) {
|
|
53
|
-
${Object.entries(fields)
|
|
54
|
-
.map(([fieldName]) => `self.${fieldName} = ${fieldName};`)
|
|
55
|
-
.join('\n')}
|
|
56
|
-
}`;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function renderGetAllFunc(schemaName: string, fields: Record<string, string>): string {
|
|
60
|
-
return `public fun get(self: &${schemaName}): ${getStructTypes(fields)} {
|
|
61
|
-
(${getStructAttrsQuery(fields)})
|
|
62
|
-
}`;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function renderGetAttrsFunc(
|
|
66
|
-
schemaName: string,
|
|
67
|
-
fields: BaseType | Record<string, BaseType>
|
|
68
|
-
): string {
|
|
69
|
-
return Object.entries(fields)
|
|
70
|
-
.map(
|
|
71
|
-
([key, type]) => `public fun get_${key}(self: &${schemaName}): ${type} {
|
|
72
|
-
self.${key}
|
|
73
|
-
}`
|
|
74
|
-
)
|
|
75
|
-
.join('\n');
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function convertToSnakeCase(input: string): string {
|
|
79
|
-
return input
|
|
80
|
-
.replace(/([A-Z])/g, '_$1')
|
|
81
|
-
.toLowerCase()
|
|
82
|
-
.replace(/^_/, '');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export async function generateSchemaData(
|
|
86
|
-
projectName: string,
|
|
87
|
-
data: Record<string, SchemaData>,
|
|
88
|
-
path: string
|
|
89
|
-
) {
|
|
90
|
-
console.log('\nš¦ Starting Schema Data Generation...');
|
|
91
|
-
for (const key of Object.keys(data)) {
|
|
92
|
-
const name = key;
|
|
93
|
-
const fields = data[key];
|
|
94
|
-
console.log(
|
|
95
|
-
` āā ${name} ${Array.isArray(fields) ? '(enum)' : '(struct)'}: ${JSON.stringify(fields)}`
|
|
96
|
-
);
|
|
97
|
-
let code = '';
|
|
98
|
-
|
|
99
|
-
const _enumNames = Object.keys(data)
|
|
100
|
-
.filter((item) => Array.isArray(data[item]))
|
|
101
|
-
.map((item) => item);
|
|
102
|
-
|
|
103
|
-
if (Array.isArray(fields)) {
|
|
104
|
-
const sortByFirstLetterFields = sortByFirstLetter(fields);
|
|
105
|
-
code = `module ${projectName}::${projectName}_${convertToSnakeCase(name)} {
|
|
106
|
-
public enum ${name} has copy, drop , store {
|
|
107
|
-
${sortByFirstLetterFields}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
${sortByFirstLetterFields
|
|
111
|
-
.map((field: string) => {
|
|
112
|
-
return `public fun new_${convertToSnakeCase(field)}(): ${name} {
|
|
113
|
-
${name}::${field}
|
|
114
|
-
}`;
|
|
115
|
-
})
|
|
116
|
-
.join('')}`;
|
|
117
|
-
} else {
|
|
118
|
-
code = `module ${projectName}::${projectName}_${convertToSnakeCase(name)} {
|
|
119
|
-
use std::ascii::String;
|
|
120
|
-
|
|
121
|
-
${Object.keys(data)
|
|
122
|
-
.map((name) => {
|
|
123
|
-
if (containsString(fields, name)) {
|
|
124
|
-
return `use ${projectName}::${projectName}_${convertToSnakeCase(name)}::${name};`;
|
|
125
|
-
}
|
|
126
|
-
return undefined;
|
|
127
|
-
})
|
|
128
|
-
.filter(Boolean)
|
|
129
|
-
.join('\n')}
|
|
130
|
-
|
|
131
|
-
public struct ${name} has copy, drop , store {
|
|
132
|
-
${getStructAttrsWithType(fields)}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
public fun new(${getStructAttrsWithType(fields)}): ${name} {
|
|
136
|
-
${name} {
|
|
137
|
-
${getStructAttrs(fields)}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
${renderGetAllFunc(name, fields)}
|
|
142
|
-
${renderGetAttrsFunc(name, fields)}
|
|
143
|
-
${renderSetAttrsFunc(name, fields)}
|
|
144
|
-
${renderSetFunc(name, fields)}
|
|
145
|
-
}`;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
await formatAndWriteMove(
|
|
149
|
-
code,
|
|
150
|
-
`${path}/src/${projectName}/sources/codegen/data/${convertToSnakeCase(name)}.move`,
|
|
151
|
-
'formatAndWriteMove'
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function generateImport(projectName: string, data: Record<string, SchemaData> | null) {
|
|
157
|
-
if (data != null) {
|
|
158
|
-
const names = Object.keys(data);
|
|
159
|
-
return names
|
|
160
|
-
.map((name) => {
|
|
161
|
-
return `use ${projectName}::${projectName}_${convertToSnakeCase(name)}::${name};`;
|
|
162
|
-
})
|
|
163
|
-
.join('\n');
|
|
164
|
-
} else {
|
|
165
|
-
return '';
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export async function generateSchemaStructure(
|
|
170
|
-
projectName: string,
|
|
171
|
-
data: Record<string, SchemaData> | null,
|
|
172
|
-
schemas: Record<string, SchemaType>,
|
|
173
|
-
path: string
|
|
174
|
-
) {
|
|
175
|
-
console.log('\nš¦ Starting Schema Structure Generation...');
|
|
176
|
-
Object.entries(schemas).forEach(([key, value]) => {
|
|
177
|
-
console.log(` āā ${key}: ${value}`);
|
|
178
|
-
});
|
|
179
|
-
const schemaMoudle = `module ${projectName}::${projectName}_schema {
|
|
180
|
-
use std::ascii::String;
|
|
181
|
-
use std::ascii::string;
|
|
182
|
-
use sui::package::UpgradeCap;
|
|
183
|
-
use std::type_name;
|
|
184
|
-
use dubhe::storage;
|
|
185
|
-
use dubhe::${
|
|
186
|
-
projectName == 'dubhe' ? 'storage_value_internal' : 'storage_value'
|
|
187
|
-
}::{Self, StorageValue};
|
|
188
|
-
use dubhe::${
|
|
189
|
-
projectName == 'dubhe' ? 'storage_map_internal' : 'storage_map'
|
|
190
|
-
}::{Self, StorageMap};
|
|
191
|
-
use dubhe::${
|
|
192
|
-
projectName == 'dubhe' ? 'storage_double_map_internal' : 'storage_double_map'
|
|
193
|
-
}::{Self, StorageDoubleMap};
|
|
194
|
-
use sui::dynamic_field as df;
|
|
195
|
-
|
|
196
|
-
${generateImport(projectName, data)}
|
|
197
|
-
|
|
198
|
-
public struct Schema has key, store { id: UID }
|
|
199
|
-
|
|
200
|
-
${Object.entries(schemas)
|
|
201
|
-
.map(([key, value]) => {
|
|
202
|
-
return `public fun borrow_${key}(self: &Schema) : &${value} {
|
|
203
|
-
storage::borrow_field(&self.id, b"${key}")
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
public(package) fun ${key}(self: &mut Schema): &mut ${value} {
|
|
207
|
-
storage::borrow_mut_field(&mut self.id, b"${key}")
|
|
208
|
-
}
|
|
209
|
-
`;
|
|
210
|
-
})
|
|
211
|
-
.join('')}
|
|
212
|
-
|
|
213
|
-
public(package) fun create(ctx: &mut TxContext): Schema {
|
|
214
|
-
let mut id = object::new(ctx);
|
|
215
|
-
${Object.entries(schemas)
|
|
216
|
-
.map(([key, value]) => {
|
|
217
|
-
let storage_type = '';
|
|
218
|
-
if (value.includes('StorageValue')) {
|
|
219
|
-
storage_type = `${
|
|
220
|
-
projectName == 'dubhe' ? 'storage_value_internal' : 'storage_value'
|
|
221
|
-
}::new(b"${key}", ctx)`;
|
|
222
|
-
} else if (value.includes('StorageMap')) {
|
|
223
|
-
storage_type = `${
|
|
224
|
-
projectName == 'dubhe' ? 'storage_map_internal' : 'storage_map'
|
|
225
|
-
}::new(b"${key}", ctx)`;
|
|
226
|
-
} else if (value.includes('StorageDoubleMap')) {
|
|
227
|
-
storage_type = `${
|
|
228
|
-
projectName == 'dubhe'
|
|
229
|
-
? 'storage_double_map_internal'
|
|
230
|
-
: 'storage_double_map'
|
|
231
|
-
}::new(b"${key}", ctx)`;
|
|
232
|
-
}
|
|
233
|
-
return `storage::add_field<${value}>(&mut id, b"${key}", ${storage_type});`;
|
|
234
|
-
})
|
|
235
|
-
.join('\n')}
|
|
236
|
-
|
|
237
|
-
Schema { id }
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
public(package) fun id(self: &mut Schema): &mut UID {
|
|
241
|
-
&mut self.id
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
public(package) fun borrow_id(self: &Schema): &UID {
|
|
245
|
-
&self.id
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
public fun migrate(_schema: &mut Schema, _ctx: &mut TxContext) { }
|
|
249
|
-
|
|
250
|
-
// ======================================== View Functions ========================================
|
|
251
|
-
${Object.entries(schemas)
|
|
252
|
-
.map(([key, value]) => {
|
|
253
|
-
// @ts-ignore
|
|
254
|
-
let all_types = value
|
|
255
|
-
.match(/<(.+)>/)[1]
|
|
256
|
-
.split(',')
|
|
257
|
-
.map((type: string) => type.trim());
|
|
258
|
-
let para_key: string[] = [];
|
|
259
|
-
let para_value = '';
|
|
260
|
-
let borrow_key = '';
|
|
261
|
-
if (value.includes('StorageValue')) {
|
|
262
|
-
para_key = [];
|
|
263
|
-
para_value = `${all_types[0]}`;
|
|
264
|
-
borrow_key = 'get()';
|
|
265
|
-
} else if (value.includes('StorageMap')) {
|
|
266
|
-
para_key = [`key: ${all_types[0]}`];
|
|
267
|
-
para_value = `${all_types[1]}`;
|
|
268
|
-
borrow_key = 'get(key)';
|
|
269
|
-
} else if (value.includes('StorageDoubleMap')) {
|
|
270
|
-
para_key = [`key1: ${all_types[0]}`, `key2: ${all_types[1]}`];
|
|
271
|
-
para_value = `${all_types[2]}`;
|
|
272
|
-
borrow_key = 'get(key1, key2)';
|
|
273
|
-
}
|
|
274
|
-
return `public fun get_${key}(self: &Schema, ${para_key}) : &${para_value} {
|
|
275
|
-
self.borrow_${key}().${borrow_key}
|
|
276
|
-
}`;
|
|
277
|
-
})
|
|
278
|
-
.join('\n')}
|
|
279
|
-
// =========================================================================================================
|
|
280
|
-
}`;
|
|
281
|
-
await formatAndWriteMove(
|
|
282
|
-
schemaMoudle,
|
|
283
|
-
|
|
284
|
-
`${path}/src/${projectName}/sources/codegen/core/schema.move`,
|
|
285
|
-
'formatAndWriteMove'
|
|
286
|
-
);
|
|
287
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { DubheConfig } from '../../types';
|
|
2
|
-
import { formatAndWriteMove } from '../formatAndWrite';
|
|
3
|
-
|
|
4
|
-
export async function generateSchemaHub(config: DubheConfig, srcPrefix: string) {
|
|
5
|
-
console.log('\nš Starting DappKey Generation...');
|
|
6
|
-
console.log(` āā Output path: ${srcPrefix}/src/${config.name}/sources/codegen/schema_hub.move`);
|
|
7
|
-
|
|
8
|
-
let code = `module ${config.name}::${config.name}_schema_hub {
|
|
9
|
-
use sui::transfer::public_share_object;
|
|
10
|
-
use sui::dynamic_field as df;
|
|
11
|
-
|
|
12
|
-
public struct SchemaHub has key, store {
|
|
13
|
-
id: UID,
|
|
14
|
-
admin: address,
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public struct SchemaTypeWapper<phantom Schema: key + store> has copy, store, drop {}
|
|
18
|
-
|
|
19
|
-
/// Authorize an schema to access protected features of the SchemaHub.
|
|
20
|
-
public(package) fun authorize_schema<Schema: key + store>(self: &mut SchemaHub) {
|
|
21
|
-
df::add(&mut self.id, SchemaTypeWapper<Schema> {}, true);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/// Deauthorize an schema by removing its authorization key.
|
|
25
|
-
public(package) fun deauthorize_schema<Schema: key + store>(self: &mut SchemaHub) {
|
|
26
|
-
df::remove<SchemaTypeWapper<Schema>, bool>(&mut self.id, SchemaTypeWapper<Schema> {});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/// Check if an schema is authorized to access protected features of
|
|
30
|
-
/// the SchemaHub.
|
|
31
|
-
public fun is_schema_authorized<Schema: key + store>(self: &SchemaHub): bool {
|
|
32
|
-
df::exists_(&self.id, SchemaTypeWapper<Schema> {})
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/// Assert that an schema is authorized to access protected features of
|
|
36
|
-
/// the SchemaHub. Aborts with \`EAppNotAuthorized\` if not.
|
|
37
|
-
public fun ensure_schema_authorized<Schema: key + store>(self: &SchemaHub) {
|
|
38
|
-
assert!(self.is_schema_authorized<Schema>(), 0);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
fun init(ctx: &mut TxContext) {
|
|
42
|
-
public_share_object(SchemaHub {
|
|
43
|
-
id: object::new(ctx),
|
|
44
|
-
admin: ctx.sender(),
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
#[test_only]
|
|
49
|
-
public fun init_schema_hub_for_testing(ctx: &mut TxContext) {
|
|
50
|
-
init(ctx);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
`;
|
|
54
|
-
await formatAndWriteMove(
|
|
55
|
-
code,
|
|
56
|
-
`${srcPrefix}/src/${config.name}/sources/codegen/schema_hub.move`,
|
|
57
|
-
'formatAndWriteMove'
|
|
58
|
-
);
|
|
59
|
-
console.log('ā
DappKey Generation Complete\n');
|
|
60
|
-
}
|
package/src/parseData/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './parser';
|