@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.
Files changed (39) hide show
  1. package/LICENSE +92 -0
  2. package/README.md +670 -1
  3. package/dist/index.d.ts +61 -27
  4. package/dist/index.js +1078 -461
  5. package/dist/index.js.map +1 -1
  6. package/package.json +15 -17
  7. package/src/codegen/debug.ts +0 -4
  8. package/src/codegen/types/index.ts +46 -10
  9. package/src/codegen/utils/config.ts +1 -1
  10. package/src/codegen/utils/format.ts +0 -6
  11. package/src/codegen/utils/formatAndWrite.ts +10 -31
  12. package/src/codegen/utils/generateLock.ts +122 -0
  13. package/src/codegen/utils/index.ts +4 -2
  14. package/src/codegen/utils/renderMove/{schemaGen.ts → codegen.ts} +40 -28
  15. package/src/codegen/utils/renderMove/common.ts +0 -65
  16. package/src/codegen/utils/renderMove/dapp.ts +2 -14
  17. package/src/codegen/utils/renderMove/generateDappKey.ts +33 -18
  18. package/src/codegen/utils/renderMove/generateError.ts +32 -15
  19. package/src/codegen/utils/renderMove/generateGenesis.ts +55 -22
  20. package/src/codegen/utils/renderMove/generateInitTest.ts +26 -14
  21. package/src/codegen/utils/renderMove/generateObjects.ts +377 -0
  22. package/src/codegen/utils/renderMove/generatePermits.ts +151 -0
  23. package/src/codegen/utils/renderMove/generateResources.ts +894 -242
  24. package/src/codegen/utils/renderMove/generateScenes.ts +467 -0
  25. package/src/codegen/utils/renderMove/generateScript.ts +18 -13
  26. package/src/codegen/utils/renderMove/generateSystem.ts +0 -2
  27. package/src/codegen/utils/renderMove/generateUserStorageInit.ts +37 -0
  28. package/src/codegen/utils/validateConfig.ts +237 -0
  29. package/src/index.ts +0 -1
  30. package/src/modules.d.ts +0 -10
  31. package/src/codegen/modules.d.ts +0 -1
  32. package/src/codegen/utils/posixPath.ts +0 -8
  33. package/src/codegen/utils/renderMove/generateComponents.ts +0 -802
  34. package/src/codegen/utils/renderMove/generateDefaultSchema.ts +0 -216
  35. package/src/codegen/utils/renderMove/generateEvent.ts +0 -99
  36. package/src/codegen/utils/renderMove/generateSchema.ts +0 -287
  37. package/src/codegen/utils/renderMove/generateSchemaHub.ts +0 -60
  38. package/src/parseData/index.ts +0 -1
  39. package/src/parseData/parser/index.ts +0 -47
@@ -0,0 +1,237 @@
1
+ import chalk from 'chalk';
2
+ import { DubheConfig, Component } from '../types';
3
+
4
+ const warn = (msg: string) =>
5
+ console.warn(chalk.yellow('[dubhe codegen]') + chalk.yellow(' WARNING: ') + msg);
6
+
7
+ /**
8
+ * Validate a DubheConfig for hard semantic errors only (throws, no console.warn).
9
+ * Called by defineConfig() so errors surface immediately at config-load time,
10
+ * without emitting duplicate warnings on every loadConfig() call.
11
+ */
12
+ export function validateConfigErrors(config: DubheConfig): void {
13
+ validateConfig(config, false);
14
+ }
15
+
16
+ /**
17
+ * Validate a DubheConfig for semantic errors and warn on suspicious combinations.
18
+ * Called by codegen before generation begins (once per generate/build run).
19
+ *
20
+ * Hard errors (throw):
21
+ * - A resource listed in objects.accepts / scenes.accepts lacks `transferable: true`
22
+ *
23
+ * Warnings (console.warn):
24
+ * - reactive: true + fungible: true (fungible deltas don't fit reactive pattern)
25
+ * - fungible: true + listable: true (valid but requires special list with amount param)
26
+ */
27
+ export function validateConfig(config: DubheConfig, emitWarnings = true): void {
28
+ const resources = config.resources ?? {};
29
+ const objects = config.objects ?? {};
30
+ const permits = config.permits ?? {};
31
+ const scenes = config.scenes ?? {};
32
+
33
+ // ── Cross-namespace naming conflict check ─────────────────────────────────
34
+ // resources, objects, permits, and scenes all generate Move modules named
35
+ // `module <project>::<key>`. Duplicate keys across these three maps would
36
+ // produce two modules with the same name in the same package, causing a
37
+ // compile-time error.
38
+ const resourceKeys = new Set(Object.keys(resources));
39
+ const objectKeys = new Set(Object.keys(objects));
40
+ const permitKeys = new Set(Object.keys(permits));
41
+ const sceneKeys = new Set(Object.keys(scenes));
42
+
43
+ const duplicates = new Set<string>();
44
+ for (const k of resourceKeys) {
45
+ if (objectKeys.has(k) || permitKeys.has(k) || sceneKeys.has(k)) duplicates.add(k);
46
+ }
47
+ for (const k of objectKeys) {
48
+ if (permitKeys.has(k) || sceneKeys.has(k)) duplicates.add(k);
49
+ }
50
+ for (const k of permitKeys) {
51
+ if (sceneKeys.has(k)) duplicates.add(k);
52
+ }
53
+ if (duplicates.size > 0) {
54
+ throw new Error(
55
+ `Duplicate module names found across resources/objects/permits/scenes: ${[...duplicates]
56
+ .sort()
57
+ .join(', ')}`
58
+ );
59
+ }
60
+
61
+ // ── Collect all resource names that are accepted by objects or scenes ────────
62
+ const acceptedResources = new Set<string>();
63
+
64
+ for (const [objKey, objCfg] of Object.entries(objects)) {
65
+ for (const r of objCfg.accepts ?? []) {
66
+ acceptedResources.add(r);
67
+ const res = resources[r];
68
+ if (!res) {
69
+ throw new Error(
70
+ `objects.${objKey}.accepts references '${r}' which is not defined in resources`
71
+ );
72
+ }
73
+ if (typeof res !== 'string' && !(res as Component).transferable) {
74
+ throw new Error(
75
+ `objects.${objKey}.accepts includes '${r}', but resources.${r} is missing transferable: true. ` +
76
+ `Add transferable: true to resources.${r} to enable cross-storage transfers.`
77
+ );
78
+ }
79
+ }
80
+
81
+ for (const r of objCfg.acceptsFrom ?? []) {
82
+ const isObject = !!objects[r];
83
+ const isScene = !!scenes[r];
84
+ if (!isObject && !isScene) {
85
+ throw new Error(
86
+ `objects.${objKey}.acceptsFrom references '${r}' which is not defined in objects or scenes`
87
+ );
88
+ }
89
+ }
90
+ }
91
+
92
+ for (const [sceneKey, sceneCfg] of Object.entries(scenes)) {
93
+ if (!sceneCfg.authorization) {
94
+ throw new Error(
95
+ `scenes.${sceneKey} is missing authorization. Use { kind: 'system' } or ` +
96
+ `{ kind: 'permit', permit: '<permit_name>' }.`
97
+ );
98
+ }
99
+ if (sceneCfg.authorization.kind === 'permit') {
100
+ if (!permits[sceneCfg.authorization.permit]) {
101
+ throw new Error(
102
+ `scenes.${sceneKey}.authorization references permit '${sceneCfg.authorization.permit}', ` +
103
+ `but permits.${sceneCfg.authorization.permit} is not defined`
104
+ );
105
+ }
106
+ } else if (sceneCfg.authorization.kind !== 'system') {
107
+ throw new Error(`scenes.${sceneKey}.authorization.kind must be 'system' or 'permit'`);
108
+ }
109
+
110
+ for (const r of sceneCfg.accepts ?? []) {
111
+ acceptedResources.add(r);
112
+ const res = resources[r];
113
+ if (!res) {
114
+ throw new Error(
115
+ `scenes.${sceneKey}.accepts references '${r}' which is not defined in resources`
116
+ );
117
+ }
118
+ if (typeof res !== 'string' && !(res as Component).transferable) {
119
+ throw new Error(
120
+ `scenes.${sceneKey}.accepts includes '${r}', but resources.${r} is missing transferable: true. ` +
121
+ `Add transferable: true to resources.${r} to enable cross-storage transfers.`
122
+ );
123
+ }
124
+ }
125
+
126
+ for (const r of sceneCfg.acceptsFrom ?? []) {
127
+ const isObject = !!objects[r];
128
+ const isScene = !!scenes[r];
129
+ if (!isObject && !isScene) {
130
+ throw new Error(
131
+ `scenes.${sceneKey}.acceptsFrom references '${r}' which is not defined in objects or scenes`
132
+ );
133
+ }
134
+ }
135
+ }
136
+
137
+ // ── Per-resource annotation combination checks ───────────────────────────────
138
+ for (const [name, resource] of Object.entries(resources)) {
139
+ if (typeof resource === 'string') continue;
140
+ const comp = resource as Component;
141
+
142
+ // ── offchain incompatibility checks ─────────────────────────────────────
143
+ if (comp.offchain) {
144
+ if (comp.listable) {
145
+ throw new Error(
146
+ `resources.${name} has both offchain: true and listable: true. ` +
147
+ `offchain resources emit events but store no on-chain state, ` +
148
+ `so there is nothing to take out and place into a Listing.`
149
+ );
150
+ }
151
+ if (comp.transferable) {
152
+ throw new Error(
153
+ `resources.${name} has both offchain: true and transferable: true. ` +
154
+ `offchain resources have no on-chain state to transfer between storages.`
155
+ );
156
+ }
157
+ if (comp.reactive) {
158
+ throw new Error(
159
+ `resources.${name} has both offchain: true and reactive: true. ` +
160
+ `reactive writes target on-chain state, which offchain resources do not have.`
161
+ );
162
+ }
163
+ if (comp.fungible) {
164
+ if (emitWarnings)
165
+ warn(
166
+ `resources.${chalk.bold(name)} has both ${chalk.cyan(
167
+ 'offchain: true'
168
+ )} and ${chalk.cyan('fungible: true')}. ` +
169
+ `offchain fungible events are emitted only; no on-chain balance is maintained ` +
170
+ `and no add/sub functions are generated. This is unusual — verify your intent.`
171
+ );
172
+ }
173
+ }
174
+
175
+ if (comp.reactive && comp.fungible) {
176
+ if (emitWarnings)
177
+ warn(
178
+ `resources.${chalk.bold(name)} has both ${chalk.cyan('reactive: true')} and ${chalk.cyan(
179
+ 'fungible: true'
180
+ )}. ` +
181
+ `Fungible quantity changes (add/sub) do not suit reactive cross-user writes. ` +
182
+ `Consider using a transfer function instead.`
183
+ );
184
+ }
185
+
186
+ // ── global incompatibility checks ────────────────────────────────────────
187
+ if (comp.global) {
188
+ if (comp.reactive) {
189
+ throw new Error(
190
+ `resources.${name} has both global: true and reactive: true. ` +
191
+ `global resources live in DappStorage (shared), not in UserStorage. ` +
192
+ `Reactive writes operate between two UserStorage objects and are incompatible with global resources.`
193
+ );
194
+ }
195
+ if (comp.listable) {
196
+ throw new Error(
197
+ `resources.${name} has both global: true and listable: true. ` +
198
+ `global resources live in DappStorage and cannot be individually listed for sale. ` +
199
+ `listable requires per-user ownership in UserStorage.`
200
+ );
201
+ }
202
+ if (comp.transferable) {
203
+ throw new Error(
204
+ `resources.${name} has both global: true and transferable: true. ` +
205
+ `global resources live in DappStorage and are not owned by individual users. ` +
206
+ `transferable requires per-user state in UserStorage or ObjectStorage.`
207
+ );
208
+ }
209
+ }
210
+
211
+ if (comp.fungible && comp.listable) {
212
+ if (emitWarnings)
213
+ warn(
214
+ `resources.${chalk.bold(name)} has both ${chalk.cyan('fungible: true')} and ${chalk.cyan(
215
+ 'listable: true'
216
+ )}. ` +
217
+ `The generated ${chalk.green(
218
+ `list_${name}`
219
+ )} entry function will include an ${chalk.cyan(
220
+ 'amount'
221
+ )} parameter for partial listings.`
222
+ );
223
+ }
224
+
225
+ if (comp.transferable && !acceptedResources.has(name)) {
226
+ if (emitWarnings)
227
+ warn(
228
+ `resources.${chalk.bold(name)} has ${chalk.cyan(
229
+ 'transferable: true'
230
+ )} but is not referenced ` +
231
+ `in any ${chalk.cyan('objects.accepts')} or ${chalk.cyan(
232
+ 'scenes.accepts'
233
+ )}. The cross-layer transfer functions will not be generated.`
234
+ );
235
+ }
236
+ }
237
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from './codegen';
2
- export * from './parseData';
3
2
  export * from './primitives';
package/src/modules.d.ts CHANGED
@@ -1,11 +1 @@
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
1
  declare module 'rollup-plugin-preserve-shebang';
@@ -1 +0,0 @@
1
- declare module 'prettier-plugin-rust';
@@ -1,8 +0,0 @@
1
- /**
2
- * Explicitly normalize a given path to a posix path (using `/` as separator).
3
- * This should be used for generating Solidity files that will be consumed by solc,
4
- * because solc expects `/` as path separator, but path.join produces `\` if the user is on windows.
5
- */
6
- export function posixPath(path: string): string {
7
- return path.replace(/\\/g, '/');
8
- }