@0xobelisk/sui-common 0.4.9
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 +3 -0
- package/dist/index.js +370 -0
- package/dist/index.js.map +1 -0
- package/package.json +71 -0
- package/src/codegen/index.ts +3 -0
- package/src/codegen/modules.d.ts +1 -0
- package/src/codegen/types/index.ts +82 -0
- package/src/codegen/utils/config.ts +68 -0
- package/src/codegen/utils/errors.ts +5 -0
- package/src/codegen/utils/format.ts +42 -0
- package/src/codegen/utils/formatAndWrite.ts +31 -0
- package/src/codegen/utils/index.ts +6 -0
- package/src/codegen/utils/posixPath.ts +8 -0
- package/src/codegen/utils/renderMove/common.ts +473 -0
- package/src/codegen/utils/renderMove/generateEntityKey.ts +53 -0
- package/src/codegen/utils/renderMove/generateEps.ts +159 -0
- package/src/codegen/utils/renderMove/generateInit.ts +40 -0
- package/src/codegen/utils/renderMove/generateSchema.ts +219 -0
- package/src/codegen/utils/renderMove/generateScript.ts +37 -0
- package/src/codegen/utils/renderMove/generateSystem.ts +53 -0
- package/src/codegen/utils/renderMove/generateToml.ts +21 -0
- package/src/codegen/utils/renderMove/worldgen.ts +34 -0
- package/src/index.ts +1 -0
- package/src/modules.d.ts +11 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ObeliskConfig } from "../../types";
|
|
2
|
+
import { formatAndWriteMove } from "../formatAndWrite";
|
|
3
|
+
import {
|
|
4
|
+
getRegisterSchema,
|
|
5
|
+
getUseSchema,
|
|
6
|
+
capitalizeFirstLetter,
|
|
7
|
+
} from "./common";
|
|
8
|
+
|
|
9
|
+
export function generateInit(config: ObeliskConfig, srcPrefix: string) {
|
|
10
|
+
let code = `module ${config.name}::init {
|
|
11
|
+
use std::ascii::string;
|
|
12
|
+
use sui::transfer;
|
|
13
|
+
use sui::tx_context::{Self, TxContext};
|
|
14
|
+
use ${config.name}::world;
|
|
15
|
+
${getUseSchema(config.name, config.schemas).join("\n")}
|
|
16
|
+
|
|
17
|
+
fun init(ctx: &mut TxContext) {
|
|
18
|
+
let (_obelisk_world, admin_cap) = world::create(string(b"${capitalizeFirstLetter(
|
|
19
|
+
config.name
|
|
20
|
+
)}"), string(b"${capitalizeFirstLetter(config.description)}"),ctx);
|
|
21
|
+
|
|
22
|
+
// Add Schema
|
|
23
|
+
${getRegisterSchema(config.schemas).join("\n")}
|
|
24
|
+
|
|
25
|
+
transfer::public_share_object(_obelisk_world);
|
|
26
|
+
transfer::public_transfer(admin_cap, tx_context::sender(ctx));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
#[test_only]
|
|
30
|
+
public fun init_world_for_testing(ctx: &mut TxContext){
|
|
31
|
+
init(ctx)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
`;
|
|
35
|
+
formatAndWriteMove(
|
|
36
|
+
code,
|
|
37
|
+
`${srcPrefix}/contracts/${config.name}/sources/codegen/init.move`,
|
|
38
|
+
"formatAndWriteMove"
|
|
39
|
+
);
|
|
40
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ObeliskConfig,
|
|
3
|
+
BaseValueType,
|
|
4
|
+
BaseType,
|
|
5
|
+
RenderSchemaOptions,
|
|
6
|
+
MoveType,
|
|
7
|
+
} from "../../types";
|
|
8
|
+
import { formatAndWriteMove } from "../formatAndWrite";
|
|
9
|
+
import {
|
|
10
|
+
getFriendSystem,
|
|
11
|
+
renderKeyName,
|
|
12
|
+
renderSetFunc,
|
|
13
|
+
renderContainFunc,
|
|
14
|
+
renderRemoveFunc,
|
|
15
|
+
renderStruct,
|
|
16
|
+
renderNewStructFunc,
|
|
17
|
+
convertToCamelCase,
|
|
18
|
+
renderSetAttrsFunc,
|
|
19
|
+
renderRegisterFunc,
|
|
20
|
+
renderGetAllFunc,
|
|
21
|
+
renderGetAttrsFunc,
|
|
22
|
+
getStructAttrsWithType,
|
|
23
|
+
getStructAttrs,
|
|
24
|
+
renderRegisterFuncWithInit,
|
|
25
|
+
renderSingleSetFunc,
|
|
26
|
+
renderSingleSetAttrsFunc,
|
|
27
|
+
renderSingleGetAllFunc,
|
|
28
|
+
renderSingleGetAttrsFunc,
|
|
29
|
+
} from "./common";
|
|
30
|
+
|
|
31
|
+
export function getRenderSchemaOptions(config: ObeliskConfig) {
|
|
32
|
+
const options: RenderSchemaOptions[] = [];
|
|
33
|
+
for (const schemaName of Object.keys(config.schemas)) {
|
|
34
|
+
const schemaData = config.schemas[schemaName];
|
|
35
|
+
let valueType: MoveType | Record<string, MoveType>;
|
|
36
|
+
let realType: BaseType | Record<string, BaseType>;
|
|
37
|
+
let defaultValue: BaseValueType | Record<string, BaseValueType> | undefined;
|
|
38
|
+
let ephemeral = false;
|
|
39
|
+
let singleton = false;
|
|
40
|
+
let needImportString = false;
|
|
41
|
+
if (typeof schemaData === "string") {
|
|
42
|
+
realType = schemaData;
|
|
43
|
+
|
|
44
|
+
if (schemaData === "string") {
|
|
45
|
+
valueType = "String";
|
|
46
|
+
needImportString = true;
|
|
47
|
+
} else if (schemaData === "vector<string>") {
|
|
48
|
+
valueType = "vector<String>";
|
|
49
|
+
needImportString = true;
|
|
50
|
+
} else {
|
|
51
|
+
valueType = schemaData;
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
realType = schemaData.valueType;
|
|
55
|
+
|
|
56
|
+
if (typeof schemaData.valueType === "string") {
|
|
57
|
+
if (schemaData.valueType === "string") {
|
|
58
|
+
valueType = "String";
|
|
59
|
+
needImportString = true;
|
|
60
|
+
} else if (schemaData.valueType === "vector<string>") {
|
|
61
|
+
valueType = "vector<String>";
|
|
62
|
+
needImportString = true;
|
|
63
|
+
} else {
|
|
64
|
+
valueType = schemaData.valueType;
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
67
|
+
valueType = { ...schemaData.valueType };
|
|
68
|
+
for (const key in valueType) {
|
|
69
|
+
if (valueType.hasOwnProperty(key)) {
|
|
70
|
+
if (valueType[key] === "string") {
|
|
71
|
+
valueType[key] = "String";
|
|
72
|
+
needImportString = true;
|
|
73
|
+
} else if (valueType[key] === "vector<string>") {
|
|
74
|
+
valueType[key] = "vector<String>";
|
|
75
|
+
needImportString = true;
|
|
76
|
+
|
|
77
|
+
// needImport = "\tuse std::ascii::{String, string};";
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
defaultValue = schemaData.defaultValue;
|
|
83
|
+
ephemeral =
|
|
84
|
+
schemaData.ephemeral !== undefined ? schemaData.ephemeral : false;
|
|
85
|
+
singleton = schemaData.defaultValue !== undefined ? true : false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
options.push({
|
|
89
|
+
projectName: config.name,
|
|
90
|
+
systems: config.systems,
|
|
91
|
+
schemaName: schemaName,
|
|
92
|
+
structName: convertToCamelCase(schemaName),
|
|
93
|
+
ephemeral,
|
|
94
|
+
singleton,
|
|
95
|
+
valueType,
|
|
96
|
+
realType,
|
|
97
|
+
// structAttrs: [renderKeyName(valueType)],
|
|
98
|
+
// structTypes: [renderStruct(convertToCamelCase(schemaName), valueType)],
|
|
99
|
+
defaultValue,
|
|
100
|
+
needImportString,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return options;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function generateSchema(config: ObeliskConfig, srcPrefix: string) {
|
|
107
|
+
const options = getRenderSchemaOptions(config);
|
|
108
|
+
for (const option of options) {
|
|
109
|
+
let code: string;
|
|
110
|
+
if (option.ephemeral) {
|
|
111
|
+
code = renderEphemeralSchema(option);
|
|
112
|
+
} else if (option.defaultValue !== undefined) {
|
|
113
|
+
code = renderSingleSchema(option);
|
|
114
|
+
} else {
|
|
115
|
+
code = renderSchema(option);
|
|
116
|
+
}
|
|
117
|
+
formatAndWriteMove(
|
|
118
|
+
code,
|
|
119
|
+
`${srcPrefix}/contracts/${option.projectName}/sources/codegen/schemas/${option.schemaName}.move`,
|
|
120
|
+
"formatAndWriteMove"
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function renderEphemeralSchema(option: RenderSchemaOptions): string {
|
|
126
|
+
return `module ${option.projectName}::${option.schemaName}_schema {
|
|
127
|
+
use std::option::none;
|
|
128
|
+
use ${option.projectName}::events;
|
|
129
|
+
|
|
130
|
+
const SCHEMA_ID: vector<u8> = b"${option.schemaName}";
|
|
131
|
+
const SCHEMA_TYPE: u8 = 2;
|
|
132
|
+
|
|
133
|
+
${renderKeyName(option.valueType)}
|
|
134
|
+
${renderStruct(option.structName, option.valueType, option.ephemeral)}
|
|
135
|
+
\tpublic fun emit_${option.schemaName}(${getStructAttrsWithType(
|
|
136
|
+
option.valueType,
|
|
137
|
+
" "
|
|
138
|
+
)}) {
|
|
139
|
+
\t\tevents::emit_set(SCHEMA_ID, SCHEMA_TYPE, none(), ${option.structName} { ${getStructAttrs(
|
|
140
|
+
option.valueType,
|
|
141
|
+
" "
|
|
142
|
+
)} })
|
|
143
|
+
\t}
|
|
144
|
+
}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function renderSingleSchema(option: RenderSchemaOptions): string {
|
|
148
|
+
return `module ${option.projectName}::${option.schemaName}_schema {
|
|
149
|
+
${
|
|
150
|
+
option.needImportString ? "\tuse std::ascii::{String,string};\n\t" : "\t"
|
|
151
|
+
}use std::option::none;
|
|
152
|
+
use sui::tx_context::TxContext;
|
|
153
|
+
use ${option.projectName}::events;
|
|
154
|
+
use ${option.projectName}::world::{Self, World, AdminCap};
|
|
155
|
+
// Systems
|
|
156
|
+
${getFriendSystem(option.projectName, option.systems)}
|
|
157
|
+
|
|
158
|
+
\tconst SCHEMA_ID: vector<u8> = b"${option.schemaName}";
|
|
159
|
+
\tconst SCHEMA_TYPE: u8 = 1;
|
|
160
|
+
|
|
161
|
+
${renderKeyName(option.valueType)}
|
|
162
|
+
${renderStruct(option.structName, option.valueType)}
|
|
163
|
+
${renderNewStructFunc(option.structName, option.valueType)}
|
|
164
|
+
${renderRegisterFuncWithInit(
|
|
165
|
+
option.structName,
|
|
166
|
+
option.realType,
|
|
167
|
+
option.defaultValue!
|
|
168
|
+
)}
|
|
169
|
+
|
|
170
|
+
${renderSingleSetFunc(
|
|
171
|
+
option.structName,
|
|
172
|
+
option.valueType
|
|
173
|
+
)}${renderSingleSetAttrsFunc(option.structName, option.valueType)}
|
|
174
|
+
|
|
175
|
+
${renderSingleGetAllFunc(
|
|
176
|
+
option.structName,
|
|
177
|
+
option.valueType
|
|
178
|
+
)}${renderSingleGetAttrsFunc(option.structName, option.valueType)}
|
|
179
|
+
}
|
|
180
|
+
`;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function renderSchema(option: RenderSchemaOptions) {
|
|
184
|
+
return `module ${option.projectName}::${option.schemaName}_schema {
|
|
185
|
+
${
|
|
186
|
+
option.needImportString ? "\tuse std::ascii::String;\n\t" : "\t"
|
|
187
|
+
}use std::option::some;
|
|
188
|
+
use sui::tx_context::TxContext;
|
|
189
|
+
use sui::table::{Self, Table};
|
|
190
|
+
use ${option.projectName}::events;
|
|
191
|
+
use ${option.projectName}::world::{Self, World, AdminCap};
|
|
192
|
+
|
|
193
|
+
// Systems
|
|
194
|
+
${getFriendSystem(option.projectName, option.systems)}
|
|
195
|
+
|
|
196
|
+
\t/// Entity does not exist
|
|
197
|
+
\tconst EEntityDoesNotExist: u64 = 0;
|
|
198
|
+
|
|
199
|
+
\tconst SCHEMA_ID: vector<u8> = b"${option.schemaName}";
|
|
200
|
+
\tconst SCHEMA_TYPE: u8 = 0;
|
|
201
|
+
|
|
202
|
+
${renderKeyName(option.valueType)}
|
|
203
|
+
${renderStruct(option.structName, option.valueType)}
|
|
204
|
+
${renderNewStructFunc(option.structName, option.valueType)}
|
|
205
|
+
${renderRegisterFunc(option.structName)}
|
|
206
|
+
|
|
207
|
+
${renderSetFunc(option.structName, option.valueType)}${renderSetAttrsFunc(
|
|
208
|
+
option.structName,
|
|
209
|
+
option.valueType
|
|
210
|
+
)}
|
|
211
|
+
${renderGetAllFunc(option.structName, option.valueType)}${renderGetAttrsFunc(
|
|
212
|
+
option.structName,
|
|
213
|
+
option.valueType
|
|
214
|
+
)}
|
|
215
|
+
${renderRemoveFunc(option.structName)}
|
|
216
|
+
${renderContainFunc(option.structName)}
|
|
217
|
+
}
|
|
218
|
+
`;
|
|
219
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ObeliskConfig } from "../../types";
|
|
2
|
+
import { formatAndWriteMove } from "../formatAndWrite";
|
|
3
|
+
import { existsSync } from "fs";
|
|
4
|
+
|
|
5
|
+
export function generateScript(config: ObeliskConfig, srcPrefix: string) {
|
|
6
|
+
if (
|
|
7
|
+
!existsSync(
|
|
8
|
+
`${srcPrefix}/contracts/${config.name}/sources/script/deploy_hook.move`
|
|
9
|
+
)
|
|
10
|
+
) {
|
|
11
|
+
let code = `module ${config.name}::deploy_hook {
|
|
12
|
+
use ${config.name}::world::{World, AdminCap, get_admin};
|
|
13
|
+
use sui::object;
|
|
14
|
+
|
|
15
|
+
/// Not the right admin for this world
|
|
16
|
+
const ENotAdmin: u64 = 0;
|
|
17
|
+
|
|
18
|
+
public entry fun run(world: &mut World, admin_cap: &AdminCap) {
|
|
19
|
+
assert!( get_admin(world) == object::id(admin_cap), ENotAdmin);
|
|
20
|
+
|
|
21
|
+
// Logic that needs to be automated once the contract is deployed
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#[test_only]
|
|
26
|
+
public fun deploy_hook_for_testing(world: &mut World, admin_cap: &AdminCap){
|
|
27
|
+
run(world, admin_cap)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
`;
|
|
31
|
+
formatAndWriteMove(
|
|
32
|
+
code,
|
|
33
|
+
`${srcPrefix}/contracts/${config.name}/sources/script/deploy_hook.move`,
|
|
34
|
+
"formatAndWriteMove"
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ObeliskConfig } from "../../types";
|
|
2
|
+
import { formatAndWriteMove } from "../formatAndWrite";
|
|
3
|
+
import { existsSync } from "fs";
|
|
4
|
+
|
|
5
|
+
export function generateSystem2(config: ObeliskConfig, srcPrefix: string) {
|
|
6
|
+
config.systems.map((systemName) => {
|
|
7
|
+
let code = `module ${config.name}::${systemName} {
|
|
8
|
+
|
|
9
|
+
}
|
|
10
|
+
`;
|
|
11
|
+
formatAndWriteMove(
|
|
12
|
+
code,
|
|
13
|
+
`${srcPrefix}/contracts/${config.name}/sources/system/${systemName}.move`,
|
|
14
|
+
"formatAndWriteMove"
|
|
15
|
+
);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function generateNewSystem1(
|
|
20
|
+
name: string,
|
|
21
|
+
systemName: string,
|
|
22
|
+
srcPrefix: string
|
|
23
|
+
) {
|
|
24
|
+
let code = `module ${name}::${systemName} {
|
|
25
|
+
|
|
26
|
+
}
|
|
27
|
+
`;
|
|
28
|
+
formatAndWriteMove(
|
|
29
|
+
code,
|
|
30
|
+
`${srcPrefix}/contracts/${name}/sources/system/${systemName}.move`,
|
|
31
|
+
"formatAndWriteMove"
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function generateSystem(config: ObeliskConfig, srcPrefix: string) {
|
|
36
|
+
config.systems.map((systemName) => {
|
|
37
|
+
if (
|
|
38
|
+
!existsSync(
|
|
39
|
+
`${srcPrefix}/contracts/${config.name}/sources/system/${systemName}.move`
|
|
40
|
+
)
|
|
41
|
+
) {
|
|
42
|
+
let code = `module ${config.name}::${systemName} {
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
formatAndWriteMove(
|
|
47
|
+
code,
|
|
48
|
+
`${srcPrefix}/contracts/${config.name}/sources/system/${systemName}.move`,
|
|
49
|
+
"formatAndWriteMove"
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ObeliskConfig } from "../../types";
|
|
2
|
+
import { formatAndWriteMove } from "../formatAndWrite";
|
|
3
|
+
|
|
4
|
+
export function generateToml(config: ObeliskConfig, srcPrefix: string) {
|
|
5
|
+
let code = `[package]
|
|
6
|
+
name = "${config.name}"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
|
|
9
|
+
[dependencies]
|
|
10
|
+
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "testnet-v1.8.0" }
|
|
11
|
+
|
|
12
|
+
[addresses]
|
|
13
|
+
sui = "0x2"
|
|
14
|
+
${config.name} = "0x0"
|
|
15
|
+
`;
|
|
16
|
+
formatAndWriteMove(
|
|
17
|
+
code,
|
|
18
|
+
`${srcPrefix}/contracts/${config.name}/Move.toml`,
|
|
19
|
+
"formatAndWriteMove"
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { SchemaMapType, ObeliskConfig } from "../../types";
|
|
2
|
+
import { rmdirSync, existsSync } from "fs";
|
|
3
|
+
import { deleteFolderRecursive } from "./common";
|
|
4
|
+
import { generateSystem } from "./generateSystem";
|
|
5
|
+
import { generateToml } from "./generateToml";
|
|
6
|
+
import { generateEntityKey } from "./generateEntityKey";
|
|
7
|
+
import { generateInit } from "./generateInit";
|
|
8
|
+
import { generateEps } from "./generateEps";
|
|
9
|
+
import { generateSchema } from "./generateSchema";
|
|
10
|
+
import {generateScript} from "./generateScript";
|
|
11
|
+
|
|
12
|
+
export function worldgen(config: ObeliskConfig, srcPrefix?: string) {
|
|
13
|
+
let path = "";
|
|
14
|
+
if (srcPrefix === undefined) {
|
|
15
|
+
path = process.cwd();
|
|
16
|
+
} else {
|
|
17
|
+
path = srcPrefix;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (existsSync(`${path}/contracts/${config.name}`)) {
|
|
21
|
+
deleteFolderRecursive(`${path}/contracts/${config.name}/sources/codegen`);
|
|
22
|
+
} else {
|
|
23
|
+
generateToml(config, path);
|
|
24
|
+
generateEntityKey(config, path);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
generateSystem(config, path);
|
|
28
|
+
generateScript(config, path);
|
|
29
|
+
|
|
30
|
+
// generate codegen
|
|
31
|
+
generateSchema(config, path);
|
|
32
|
+
generateEps(config.name, path);
|
|
33
|
+
generateInit(config, path);
|
|
34
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./codegen";
|
package/src/modules.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// adding .js to minimal would break clients down the line because it probably won't get a synthetic default import
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
declare module "protobufjs/minimal" {
|
|
4
|
+
export const configure: any;
|
|
5
|
+
export const util: any;
|
|
6
|
+
export const Reader: any;
|
|
7
|
+
export type Reader = any;
|
|
8
|
+
export const Writer: any;
|
|
9
|
+
export type Writer = any;
|
|
10
|
+
}
|
|
11
|
+
declare module "rollup-plugin-preserve-shebang";
|