@0xobelisk/sui-common 1.2.0-pre.112 → 1.2.0-pre.115
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/dist/index.d.ts +1 -19
- package/dist/index.js +131 -123
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/codegen/debug.ts +0 -4
- package/src/codegen/utils/config.ts +1 -1
- package/src/codegen/utils/format.ts +0 -6
- package/src/codegen/utils/formatAndWrite.ts +0 -20
- package/src/codegen/utils/index.ts +0 -1
- package/src/codegen/utils/renderMove/common.ts +0 -65
- package/src/codegen/utils/renderMove/generateError.ts +12 -11
- package/src/codegen/utils/renderMove/generateGenesis.ts +8 -8
- package/src/codegen/utils/renderMove/generateScript.ts +0 -8
- package/src/codegen/utils/renderMove/schemaGen.ts +0 -7
- package/src/index.ts +0 -1
- package/src/modules.d.ts +0 -10
- package/src/codegen/utils/posixPath.ts +0 -8
- 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
package/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,6 @@
|
|
|
1
1
|
declare function formatMove(content: string, prettierConfigPath?: string): Promise<string>;
|
|
2
|
-
declare function formatTypescript(content: string): Promise<string>;
|
|
3
2
|
|
|
4
3
|
declare function formatAndWriteMove(output: string, fullOutputPath: string, logPrefix?: string): Promise<void>;
|
|
5
|
-
/**
|
|
6
|
-
* Formats typescript code using prettier and write it to a file
|
|
7
|
-
* @param output typescript code
|
|
8
|
-
* @param fullOutputPath full path to the output file
|
|
9
|
-
* @param logPrefix prefix for debug logs
|
|
10
|
-
*/
|
|
11
|
-
declare function formatAndWriteTypescript(output: string, fullOutputPath: string, logPrefix: string): Promise<void>;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Explicitly normalize a given path to a posix path (using `/` as separator).
|
|
15
|
-
* This should be used for generating Solidity files that will be consumed by solc,
|
|
16
|
-
* because solc expects `/` as path separator, but path.join produces `\` if the user is on windows.
|
|
17
|
-
*/
|
|
18
|
-
declare function posixPath(path: string): string;
|
|
19
4
|
|
|
20
5
|
type ComponentType = 'Onchain' | 'Offchain';
|
|
21
6
|
type MoveType = 'address' | 'bool' | 'u8' | 'u32' | 'u64' | 'u128' | 'u256' | 'String' | 'vector<address>' | 'vector<bool>' | 'vector<u8>' | 'vector<vector<u8>>' | 'vector<u32>' | 'vector<u64>' | 'vector<u128>' | 'vector<u256>' | string;
|
|
@@ -47,9 +32,6 @@ declare function schemaGen(rootDir: string, config: DubheConfig, network?: 'main
|
|
|
47
32
|
declare const defineConfig: (config: DubheConfig) => DubheConfig;
|
|
48
33
|
|
|
49
34
|
declare function loadConfig(configPath?: string): Promise<unknown>;
|
|
50
|
-
declare function resolveConfigPath(configPath: string | undefined, toFileURL?: boolean): Promise<string>;
|
|
51
|
-
|
|
52
|
-
declare const parseData: (data: any) => any;
|
|
53
35
|
|
|
54
36
|
declare enum SubscriptionKind {
|
|
55
37
|
Event = "event",
|
|
@@ -64,4 +46,4 @@ type SubscribableType = {
|
|
|
64
46
|
name?: string;
|
|
65
47
|
};
|
|
66
48
|
|
|
67
|
-
export { BaseType, Component, ComponentType, DubheConfig, DubheMetadata, ErrorData, EventData, MoveType, SchemaData, SchemaType, SubscribableType, SubscriptionKind, defineConfig, formatAndWriteMove,
|
|
49
|
+
export { BaseType, Component, ComponentType, DubheConfig, DubheMetadata, ErrorData, EventData, MoveType, SchemaData, SchemaType, SubscribableType, SubscriptionKind, defineConfig, formatAndWriteMove, formatMove, loadConfig, schemaGen };
|
package/dist/index.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import
|
|
1
|
+
import J from"prettier";import le from"prettier-plugin-move-js";async function Oe(e,t){let s;t&&(s=await J.resolveConfig(t));try{return J.format(e,{plugins:[le],parser:"move-parse",printWidth:120,semi:!0,tabWidth:2,useTabs:!1,bracketSpacing:!0,...s})}catch(n){let u;return n instanceof Error?u=n.message:u=n,console.log(`Error during output formatting: ${u}`),e}}import Z from"node:fs/promises";import be from"node:path";import P from"debug";var L=P("dubhe:common"),de=P("dubhe:common");L.log=console.debug.bind(console);de.log=console.error.bind(console);var B=L.extend("codegen");B.log=console.debug.bind(console);async function f(e,t,s){let n=e,u=` // Copyright (c) Obelisk Labs, Inc.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
#[allow(unused_use)]
|
|
4
4
|
|
|
5
5
|
/* Autogenerated file. Do not edit manually. */
|
|
6
6
|
|
|
7
|
-
`,
|
|
8
|
-
\u{1F4C4} Starting Move.toml Generation...`),console.log(` \u2514\u2500 Output path: ${t}/src/${e.name}/Move.toml`);let
|
|
7
|
+
`,c="#[test_only]",h=u+n,v="#[allow(lint(share_owned))]";t.includes(".toml")||t.includes("system")||t.includes("migrate")?h=n:t.includes("init")?h=c+n:t.includes("genesis")&&(h=v+n),await Z.mkdir(be.dirname(t),{recursive:!0}),await Z.writeFile(t,h),B(`${s}: ${t}`)}import{existsSync as A}from"fs";import j from"fs";function G(e){j.existsSync(e)&&(j.readdirSync(e).forEach(t=>{let s=`${e}/${t}`;j.lstatSync(s).isDirectory()?G(s):j.unlinkSync(s)}),j.rmdirSync(e))}var q={name:"@0xobelisk/sui-common",version:"1.2.0-pre.115",description:"Common low level logic shared between packages",keywords:["sui","obelisk labs","move","blockchain"],homepage:"https://github.com/0xobelisk/dubhe/tree/main/packages/sui-common#readme",bugs:"https://github.com/0xobelisk/dubhe/issues",repository:{type:"git",url:"https://github.com/0xobelisk/dubhe.git"},license:"Apache-2.0",author:"team@obelisk.build",type:"module",exports:{".":{types:"./dist/index.d.ts",default:"./dist/index.js"}},types:"./dist/index.d.ts",typesVersions:{"*":{index:["./src/index.ts"]}},scripts:{build:"pnpm run type-check && pnpm run build:js","build:js":"tsup && chmod +x ./dist/index.js",clean:"pnpm run clean:js","clean:js":"rimraf dist",dev:"tsup --watch",format:"prettier --write .","format:check":"prettier --check .",lint:"eslint . --ext .ts",test:"vitest","type-check":"tsc --noEmit",validate:"pnpm format:check && pnpm type-check"},dependencies:{chalk:"^5.0.1",debug:"^4.3.4",dotenv:"^16.0.3",ejs:"^3.1.8",esbuild:"^0.17.15",execa:"^7.0.0","find-up":"^6.3.0",glob:"^10.5.0",path:"^0.12.7",prettier:"^3.1.1","prettier-plugin-move-js":"^0.0.5",typescript:"^5.8.3",yargs:"^17.7.1",zod:"^3.22.3","zod-validation-error":"^1.3.0"},devDependencies:{"@types/debug":"^4.1.12","@types/ejs":"^3.1.1","@types/glob":"^7.2.0","@types/node":"^22.16.0","@types/yargs":"^17.0.10",eslint:"^9.0.0","eslint-config-prettier":"^9.1.0",prettier:"3.3.3",tsup:"^6.7.0",tsx:"^3.12.6",vitest:"^2.1.3"},engines:{node:">=22.0.0"},publishConfig:{access:"public"}};async function X(e,t){console.log(`
|
|
8
|
+
\u{1F4C4} Starting Move.toml Generation...`),console.log(` \u2514\u2500 Output path: ${t}/src/${e.name}/Move.toml`);let s=`[package]
|
|
9
9
|
name = "${e.name}"
|
|
10
10
|
version = "1.0.0"
|
|
11
11
|
edition = "2024"
|
|
12
12
|
|
|
13
13
|
[dependencies]
|
|
14
14
|
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.46.3" }
|
|
15
|
-
Dubhe = { git = "https://github.com/0xobelisk/dubhe.git", subdir = "framework/src/dubhe", rev = "v${
|
|
15
|
+
Dubhe = { git = "https://github.com/0xobelisk/dubhe.git", subdir = "framework/src/dubhe", rev = "v${q.version}" }
|
|
16
16
|
|
|
17
17
|
[addresses]
|
|
18
18
|
sui = "0x2"
|
|
19
19
|
${e.name} = "0x0"
|
|
20
|
-
`;await
|
|
21
|
-
`)}import{existsSync as
|
|
20
|
+
`;await f(s,`${t}/src/${e.name}/Move.toml`,"formatAndWriteMove"),console.log(`\u2705 Move.toml Generation Complete
|
|
21
|
+
`)}import{existsSync as Y}from"fs";async function Q(e,t){if(!Y(t)){let s=`module ${e.name}::deploy_hook {
|
|
22
22
|
use dubhe::dapp_service::DappHub;
|
|
23
23
|
|
|
24
24
|
public(package) fun run(_dapp_hub: &mut DappHub, _ctx: &mut TxContext) {
|
|
25
25
|
|
|
26
26
|
}
|
|
27
|
-
}`;await
|
|
27
|
+
}`;await f(s,t,"formatAndWriteMove")}}async function N(e,t){if(!Y(`${t}/src/${e.name}/sources/scripts/migrate.move`)){let s=`module ${e.name}::migrate {
|
|
28
28
|
const ON_CHAIN_VERSION: u32 = 1;
|
|
29
29
|
|
|
30
30
|
public fun on_chain_version(): u32 {
|
|
31
31
|
ON_CHAIN_VERSION
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
`;await
|
|
34
|
+
`;await f(s,`${t}/src/${e.name}/sources/scripts/migrate.move`,"formatAndWriteMove")}}async function ee(e,t){let s=`module ${e.name}::dapp_key {
|
|
35
35
|
use std::type_name;
|
|
36
36
|
use sui::address;
|
|
37
37
|
use std::ascii::String;
|
|
@@ -57,14 +57,14 @@ use std::type_name;
|
|
|
57
57
|
type_name::get<DappKey1>() == type_name::get<DappKey2>()
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
|
-
`;await
|
|
61
|
-
\u{1F4E6} Starting Schema Error Generation...`);let n
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
`;await f(s,t,"formatAndWriteMove")}import{existsSync as te}from"fs";import se from"node:fs/promises";async function re(e,t){te(`${t}/src/${e.name}/sources/systems`)||await se.mkdir(`${t}/src/${e.name}/sources/systems`,{recursive:!0}),te(`${t}/src/${e.name}/sources/tests`)||await se.mkdir(`${t}/src/${e.name}/sources/tests`,{recursive:!0})}async function ne(e,t,s){console.log(`
|
|
61
|
+
\u{1F4E6} Starting Schema Error Generation...`);let n=Object.entries(t).map(([c,h])=>(console.log(` \u2514\u2500 ${c}: ${h}`),[" #[error]",` const ${c.toUpperCase()}: vector<u8> = b"${h}";`,` public fun ${c}_error(condition: bool) { assert!(condition, ${c.toUpperCase()}) }`].join(`
|
|
62
|
+
`))).join(`
|
|
63
|
+
|
|
64
|
+
`),u=`module ${e}::errors {
|
|
65
|
+
${n}
|
|
66
|
+
}
|
|
67
|
+
`;await f(u,`${s}/src/${e}/sources/codegen/errors.move`,"formatAndWriteMove")}async function oe(e,t){let s=`module ${e.name}::init_test {
|
|
68
68
|
use sui::clock;
|
|
69
69
|
use sui::test_scenario;
|
|
70
70
|
use sui::test_scenario::Scenario;
|
|
@@ -82,7 +82,7 @@ use std::type_name;
|
|
|
82
82
|
dapp_hub
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
-
`;await
|
|
85
|
+
`;await f(s,t,"formatAndWriteMove")}async function ue(e,t){let s=`module ${e.name}::genesis {
|
|
86
86
|
use sui::clock::Clock;
|
|
87
87
|
use dubhe::dapp_service::DappHub;
|
|
88
88
|
use ${e.name}::dapp_key;
|
|
@@ -97,17 +97,25 @@ use std::type_name;
|
|
|
97
97
|
// Logic that needs to be automated once the contract is deployed
|
|
98
98
|
${e.name}::deploy_hook::run(dapp_hub, ctx);
|
|
99
99
|
}
|
|
100
|
+
|
|
101
|
+
// Called during contract upgrades to register newly added resource tables.
|
|
102
|
+
// The region between the separator comments is rewritten by \`dubhe upgrade\`
|
|
103
|
+
// when new resources are detected, so do not manually edit that block.
|
|
104
|
+
public(package) fun migrate(dapp_hub: &mut DappHub, ctx: &mut TxContext) {
|
|
105
|
+
// ==========================================
|
|
106
|
+
// ==========================================
|
|
107
|
+
}
|
|
100
108
|
}
|
|
101
|
-
`;await
|
|
102
|
-
\u{1F4E6} Starting Enums Generation...`),!!e.enums)for(let[
|
|
109
|
+
`;await f(s,t,"formatAndWriteMove")}function ie(e){return e.replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`).replace(/^_/,"")}async function pe(e,t){if(console.log(`
|
|
110
|
+
\u{1F4E6} Starting Enums Generation...`),!!e.enums)for(let[s,n]of Object.entries(e.enums)){console.log(` \u2514\u2500 ${s}: ${JSON.stringify(n)}`);let u=[...n].sort((h,v)=>h.localeCompare(v)),c=ge(e.name,s,u);await f(c,`${t}/${ie(s)}.move`,"formatAndWriteMove")}}function ge(e,t,s){let n=s.map(u=>u.charAt(0).toUpperCase()+u.slice(1)).join(",");return`module ${e}::${ie(t)} {
|
|
103
111
|
use sui::bcs::{BCS, to_bytes, peel_enum_tag};
|
|
104
112
|
|
|
105
113
|
public enum ${t} has copy, drop, store {
|
|
106
114
|
${n}
|
|
107
115
|
}
|
|
108
116
|
|
|
109
|
-
${
|
|
110
|
-
${t}::${
|
|
117
|
+
${s.map(u=>` public fun new_${u.toLowerCase()}(): ${t} {
|
|
118
|
+
${t}::${u.charAt(0).toUpperCase()+u.slice(1)}
|
|
111
119
|
}`).join(`
|
|
112
120
|
|
|
113
121
|
`)}
|
|
@@ -118,43 +126,43 @@ ${r.map(i=>` public fun new_${i.toLowerCase()}(): ${t} {
|
|
|
118
126
|
|
|
119
127
|
public fun decode(bytes: &mut BCS): ${t} {
|
|
120
128
|
match(peel_enum_tag(bytes)) {
|
|
121
|
-
${
|
|
129
|
+
${s.map((u,c)=>` ${c} => ${t}::${u.charAt(0).toUpperCase()+u.slice(1)},`).join(`
|
|
122
130
|
`)}
|
|
123
131
|
_ => abort,
|
|
124
132
|
}
|
|
125
133
|
}
|
|
126
|
-
}`}function
|
|
127
|
-
\u{1F4E6} Starting Resources Generation...`);for(let[
|
|
134
|
+
}`}function _(e){return e==="dubhe"?"dapp_service":"dapp_system"}async function U(e,t){console.log(`
|
|
135
|
+
\u{1F4E6} Starting Resources Generation...`);for(let[s,n]of Object.entries(e.resources)){if(console.log(` \u2514\u2500 ${s}: ${JSON.stringify(n)}`),typeof n=="string"){let c=$e(e.name,s,n,"Onchain");await f(c,`${t}/${s}.move`,"formatAndWriteMove");continue}if(!n.fields||Object.keys(n.fields).length===0)throw new Error(`Resource '${s}' must have fields defined, but found empty object`);n.keys||(n.keys=[]);let u=me(e.name,s,n);await f(u,`${t}/${s}.move`,"formatAndWriteMove")}}function $e(e,t,s,n="Onchain"){let u=!w(s),c=u?`${R(s)}`:"",v=n==="Offchain"?"":`
|
|
128
136
|
public fun has(dapp_hub: &DappHub, resource_account: String): bool {
|
|
129
137
|
let mut key_tuple = vector::empty();
|
|
130
138
|
key_tuple.push_back(TABLE_NAME);
|
|
131
|
-
${
|
|
139
|
+
${_(e)}::has_record<DappKey>(dapp_hub, resource_account, key_tuple)
|
|
132
140
|
}
|
|
133
141
|
|
|
134
142
|
public fun ensure_has(dapp_hub: &DappHub, resource_account: String) {
|
|
135
143
|
let mut key_tuple = vector::empty();
|
|
136
144
|
key_tuple.push_back(TABLE_NAME);
|
|
137
|
-
${
|
|
145
|
+
${_(e)}::ensure_has_record<DappKey>(dapp_hub, resource_account, key_tuple)
|
|
138
146
|
}
|
|
139
147
|
|
|
140
148
|
public fun ensure_has_not(dapp_hub: &DappHub, resource_account: String) {
|
|
141
149
|
let mut key_tuple = vector::empty();
|
|
142
150
|
key_tuple.push_back(TABLE_NAME);
|
|
143
|
-
${
|
|
151
|
+
${_(e)}::ensure_has_not_record<DappKey>(dapp_hub, resource_account, key_tuple)
|
|
144
152
|
}
|
|
145
153
|
|
|
146
154
|
public(package) fun delete(dapp_hub: &mut DappHub, resource_account: String) {
|
|
147
155
|
let mut key_tuple = vector::empty();
|
|
148
156
|
key_tuple.push_back(TABLE_NAME);
|
|
149
|
-
${
|
|
157
|
+
${_(e)}::delete_record<DappKey>(dapp_hub, dapp_key::new(), key_tuple, resource_account);
|
|
150
158
|
}
|
|
151
159
|
|
|
152
|
-
public fun get(dapp_hub: &DappHub, resource_account: String): (${
|
|
160
|
+
public fun get(dapp_hub: &DappHub, resource_account: String): (${s==="string"||s==="String"?"String":s}) {
|
|
153
161
|
let mut key_tuple = vector::empty();
|
|
154
162
|
key_tuple.push_back(TABLE_NAME);
|
|
155
|
-
let value_tuple = ${
|
|
163
|
+
let value_tuple = ${_(e)}::get_record<DappKey>(dapp_hub, resource_account, key_tuple);
|
|
156
164
|
let mut bsc_type = sui::bcs::new(value_tuple);
|
|
157
|
-
${
|
|
165
|
+
${s==="string"||s==="String"?"let value = dubhe::bcs::peel_string(&mut bsc_type);":s==="vector<String>"?"let value = dubhe::bcs::peel_vec_string(&mut bsc_type);":u?`let value = ${e}::${c}::decode(&mut bsc_type);`:`let value = sui::bcs::peel_${E(s)}(&mut bsc_type);`}
|
|
158
166
|
(value)
|
|
159
167
|
}
|
|
160
168
|
`;return`module ${e}::${t} {
|
|
@@ -165,26 +173,26 @@ ${r.map((i,l)=>` ${l} => ${t}::${i.charAt(0).toUpperCase()+i.slice(1)
|
|
|
165
173
|
use dubhe::dapp_system;
|
|
166
174
|
use ${e}::dapp_key;
|
|
167
175
|
use ${e}::dapp_key::DappKey;
|
|
168
|
-
${
|
|
169
|
-
use ${e}::${
|
|
176
|
+
${u&&s!=="string"&&s!=="String"?` use ${e}::${c};
|
|
177
|
+
use ${e}::${c}::{${s}};`:""}
|
|
170
178
|
|
|
171
179
|
const TABLE_NAME: vector<u8> = b"${t}";
|
|
172
180
|
const OFFCHAIN: bool = ${n==="Offchain"};
|
|
173
181
|
|
|
174
182
|
${v}
|
|
175
|
-
public(package) fun set(dapp_hub: &mut DappHub, resource_account: String, value: ${
|
|
183
|
+
public(package) fun set(dapp_hub: &mut DappHub, resource_account: String, value: ${s==="string"||s==="String"?"String":s}, ctx: &mut TxContext) {
|
|
176
184
|
let mut key_tuple = vector::empty();
|
|
177
185
|
key_tuple.push_back(TABLE_NAME);
|
|
178
186
|
let value_tuple = encode(value);
|
|
179
|
-
${
|
|
187
|
+
${_(e)}::set_record(dapp_hub, dapp_key::new(), key_tuple, value_tuple, resource_account, OFFCHAIN, ctx);
|
|
180
188
|
}
|
|
181
189
|
|
|
182
|
-
public fun encode(value: ${
|
|
190
|
+
public fun encode(value: ${s==="string"||s==="String"?"String":s}): vector<vector<u8>> {
|
|
183
191
|
let mut value_tuple = vector::empty();
|
|
184
|
-
value_tuple.push_back(${
|
|
192
|
+
value_tuple.push_back(${s==="string"||s==="String"?"to_bytes(&into_bytes(value))":s==="vector<String>"?"to_bytes(&value)":u?`${e}::${c}::encode(value)`:"to_bytes(&value)"});
|
|
185
193
|
value_tuple
|
|
186
194
|
}
|
|
187
|
-
}`}function
|
|
195
|
+
}`}function R(e){return e.replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`).replace(/^_/,"")}function me(e,t,s){let n=s.fields,u=s.keys||[],c=s.offchain||!1,h=s.global||!1,v=c?"Offchain":"Onchain",a=h,b=Object.keys(n).every(o=>u.includes(o)),M=Object.entries(n).filter(([o])=>!u.includes(o)),x=M.map(([o])=>o),D=x.length===1,p=M.filter(([o,$])=>!w($)&&$!=="string"&&$!=="String").map(([o,$])=>({type:$,module:`${R($)}`})).filter((o,$,H)=>H.findIndex(F=>F.type===o.type)===$),g=Object.entries(n).filter(([o,$])=>!w($)&&$!=="string"&&$!=="String").map(([o,$])=>({type:$,module:`${R($)}`})).filter((o,$,H)=>H.findIndex(F=>F.type===o.type)===$),i=ye(e,t,n,u,!b&&!D,p,v,a);if(b||D)return`module ${e}::${t} {
|
|
188
196
|
use sui::bcs::{to_bytes};
|
|
189
197
|
use std::ascii::{string, String, into_bytes};
|
|
190
198
|
use dubhe::table_id;
|
|
@@ -192,21 +200,21 @@ ${v}
|
|
|
192
200
|
use dubhe::dapp_system;
|
|
193
201
|
use ${e}::dapp_key;
|
|
194
202
|
use ${e}::dapp_key::DappKey;
|
|
195
|
-
${
|
|
203
|
+
${g.length>0?g.map(o=>` use ${e}::${o.module};
|
|
196
204
|
use ${e}::${o.module}::{${o.type}};`).join(`
|
|
197
205
|
`):""}
|
|
198
206
|
|
|
199
207
|
const TABLE_NAME: vector<u8> = b"${t}";
|
|
200
|
-
const OFFCHAIN: bool = ${
|
|
208
|
+
const OFFCHAIN: bool = ${c};
|
|
201
209
|
|
|
202
|
-
${
|
|
210
|
+
${i}
|
|
203
211
|
}`;let m=x.map(o=>` ${o}: ${n[o]==="string"||n[o]==="String"?"String":n[o]==="vector<String>"?"vector<String>":n[o]},`).join(`
|
|
204
|
-
`),
|
|
205
|
-
`),
|
|
212
|
+
`),y=x.map(o=>`${o}: ${n[o]==="string"||n[o]==="String"?"String":n[o]==="vector<String>"?"vector<String>":n[o]}`).join(", "),T=x.map(o=>` ${o},`).join(`
|
|
213
|
+
`),I=x.map(o=>` public fun ${o}(self: &${k(t)}): ${n[o]==="string"||n[o]==="String"?"String":n[o]==="vector<String>"?"vector<String>":n[o]} {
|
|
206
214
|
self.${o}
|
|
207
215
|
}`).join(`
|
|
208
216
|
|
|
209
|
-
`),
|
|
217
|
+
`),W=x.map(o=>` public fun update_${o}(self: &mut ${k(t)}, ${o}: ${n[o]==="string"||n[o]==="String"?"String":n[o]==="vector<String>"?"vector<String>":n[o]}) {
|
|
210
218
|
self.${o} = ${o}
|
|
211
219
|
}`).join(`
|
|
212
220
|
|
|
@@ -218,150 +226,150 @@ ${u}
|
|
|
218
226
|
use dubhe::dapp_system;
|
|
219
227
|
use ${e}::dapp_key;
|
|
220
228
|
use ${e}::dapp_key::DappKey;
|
|
221
|
-
${
|
|
229
|
+
${g.length>0?g.map(o=>` use ${e}::${o.module};
|
|
222
230
|
use ${e}::${o.module}::{${o.type}};`).join(`
|
|
223
231
|
`):""}
|
|
224
232
|
|
|
225
233
|
const TABLE_NAME: vector<u8> = b"${t}";
|
|
226
|
-
const OFFCHAIN: bool = ${
|
|
234
|
+
const OFFCHAIN: bool = ${c};
|
|
227
235
|
|
|
228
236
|
public struct ${k(t)} has copy, drop, store {
|
|
229
237
|
${m}
|
|
230
238
|
}
|
|
231
239
|
|
|
232
|
-
public fun new(${
|
|
240
|
+
public fun new(${y}): ${k(t)} {
|
|
233
241
|
${k(t)} {
|
|
234
|
-
${
|
|
242
|
+
${T}
|
|
235
243
|
}
|
|
236
244
|
}
|
|
237
245
|
|
|
238
|
-
${
|
|
246
|
+
${I}
|
|
239
247
|
|
|
240
|
-
${
|
|
248
|
+
${W}
|
|
241
249
|
|
|
242
|
-
${
|
|
243
|
-
}`}function
|
|
250
|
+
${i}
|
|
251
|
+
}`}function w(e){return["address","bool","u8","u16","u32","u64","u128","u256","string","String","vector<address>","vector<bool>","vector<u8>","vector<u16>","vector<vector<u8>>","vector<u32>","vector<u64>","vector<u128>","vector<u256>","vector<String>"].includes(e)}function ye(e,t,s,n,u=!0,c=[],h="Onchain",v=!1){let a=Object.entries(s).filter(([r])=>!n.includes(r)).reduce((r,[d,l])=>({...r,[d]:l}),{}),b=Object.keys(a),M=Object.keys(s).every(r=>n.includes(r)),x=b.length===1,D=h==="Offchain",p=n.length>0?n.map(r=>`${r}: ${s[r]}`).join(", "):"",g=n.length>0?`let mut key_tuple = vector::empty();
|
|
244
252
|
key_tuple.push_back(TABLE_NAME);
|
|
245
|
-
${n.map(
|
|
253
|
+
${n.map(r=>`key_tuple.push_back(to_bytes(&${r}));`).join(`
|
|
246
254
|
`)}`:`let mut key_tuple = vector::empty();
|
|
247
|
-
key_tuple.push_back(TABLE_NAME);`,
|
|
248
|
-
${
|
|
249
|
-
${
|
|
255
|
+
key_tuple.push_back(TABLE_NAME);`,i=v?"":"resource_account: String",m=v?"dapp_key::package_id().to_ascii_string()":"resource_account",y=i&&p?", ":"",T=D?"":` public fun has(dapp_hub: &DappHub${i||p?", ":""}${i}${y}${p}): bool {
|
|
256
|
+
${g}
|
|
257
|
+
${_(e)}::has_record<DappKey>(dapp_hub, ${m}, key_tuple)
|
|
250
258
|
}
|
|
251
259
|
|
|
252
|
-
public fun ensure_has(dapp_hub: &DappHub${
|
|
253
|
-
${
|
|
254
|
-
${
|
|
260
|
+
public fun ensure_has(dapp_hub: &DappHub${i||p?", ":""}${i}${y}${p}) {
|
|
261
|
+
${g}
|
|
262
|
+
${_(e)}::ensure_has_record<DappKey>(dapp_hub, ${m}, key_tuple)
|
|
255
263
|
}
|
|
256
264
|
|
|
257
|
-
public fun ensure_has_not(dapp_hub: &DappHub${
|
|
258
|
-
${
|
|
259
|
-
${
|
|
265
|
+
public fun ensure_has_not(dapp_hub: &DappHub${i||p?", ":""}${i}${y}${p}) {
|
|
266
|
+
${g}
|
|
267
|
+
${_(e)}::ensure_has_not_record<DappKey>(dapp_hub, ${m}, key_tuple)
|
|
260
268
|
}
|
|
261
|
-
`,
|
|
262
|
-
${
|
|
263
|
-
${
|
|
264
|
-
}`,
|
|
265
|
-
${
|
|
266
|
-
let value = ${
|
|
269
|
+
`,I=D?"":` public(package) fun delete(dapp_hub: &mut DappHub${i||p?", ":""}${i}${y}${p}) {
|
|
270
|
+
${g}
|
|
271
|
+
${_(e)}::delete_record<DappKey>(dapp_hub, dapp_key::new(), key_tuple, ${m});
|
|
272
|
+
}`,W=!x&&!D?b.map(r=>{let d=b.indexOf(r),l=s[r],S=!w(l),C=S?c.find(ae=>ae.type===l):null;return` public fun get_${r}(dapp_hub: &DappHub${i||p?", ":""}${i}${y}${p}): ${l==="string"||l==="String"?"String":l==="vector<String>"?"vector<String>":l} {
|
|
273
|
+
${g}
|
|
274
|
+
let value = ${_(e)}::get_field<DappKey>(dapp_hub, ${m}, key_tuple, ${d});
|
|
267
275
|
let mut bsc_type = sui::bcs::new(value);
|
|
268
|
-
${
|
|
269
|
-
${
|
|
276
|
+
${l==="string"||l==="String"?`let ${r} = dubhe::bcs::peel_string(&mut bsc_type);`:l==="vector<String>"?`let ${r} = dubhe::bcs::peel_vec_string(&mut bsc_type);`:S?`let ${r} = ${e}::${C?.module}::decode(&mut bsc_type);`:`let ${r} = sui::bcs::peel_${E(l)}(&mut bsc_type);`}
|
|
277
|
+
${r}
|
|
270
278
|
}
|
|
271
279
|
|
|
272
|
-
public(package) fun set_${
|
|
273
|
-
${
|
|
274
|
-
let value = ${
|
|
275
|
-
${
|
|
280
|
+
public(package) fun set_${r}(dapp_hub: &mut DappHub${i||p?", ":""}${i}${y}${p}, ${r}: ${l==="string"||l==="String"?"String":l==="vector<String>"?"vector<String>":l}, ctx: &mut TxContext) {
|
|
281
|
+
${g}
|
|
282
|
+
let value = ${l==="string"||l==="String"?`to_bytes(&into_bytes(${r}))`:l==="vector<String>"?`to_bytes(&${r})`:S?`${e}::${C?.module}::encode(${r})`:`to_bytes(&${r})`};
|
|
283
|
+
${_(e)}::set_field(dapp_hub, dapp_key::new(), ${m}, key_tuple, ${d}, value, ctx);
|
|
276
284
|
}`}).join(`
|
|
277
285
|
|
|
278
|
-
`):"",o=
|
|
279
|
-
${
|
|
286
|
+
`):"",o=M?` public(package) fun set(dapp_hub: &mut DappHub${i||p?", ":""}${i}${y}${p}, ctx: &mut TxContext) {
|
|
287
|
+
${g}
|
|
280
288
|
let value_tuple = vector::empty();
|
|
281
|
-
${
|
|
282
|
-
}`:x?D?` public(package) fun set(dapp_hub: &mut DappHub${
|
|
283
|
-
${
|
|
289
|
+
${_(e)}::set_record(dapp_hub, dapp_key::new(), key_tuple, value_tuple, ${m}, OFFCHAIN, ctx);
|
|
290
|
+
}`:x?D?` public(package) fun set(dapp_hub: &mut DappHub${i||p?", ":""}${i}${y}${p}, value: ${Object.values(a)[0]==="string"||Object.values(a)[0]==="String"?"String":Object.values(a)[0]}, ctx: &mut TxContext) {
|
|
291
|
+
${g}
|
|
284
292
|
let value_tuple = encode(value);
|
|
285
|
-
${
|
|
286
|
-
}`:` public fun get(dapp_hub: &DappHub${
|
|
287
|
-
${
|
|
288
|
-
let value = ${
|
|
293
|
+
${_(e)}::set_record(dapp_hub, dapp_key::new(), key_tuple, value_tuple, ${m}, OFFCHAIN, ctx);
|
|
294
|
+
}`:` public fun get(dapp_hub: &DappHub${i||p?", ":""}${i}${y}${p}): ${Object.values(a)[0]==="string"||Object.values(a)[0]==="String"?"String":Object.values(a)[0]} {
|
|
295
|
+
${g}
|
|
296
|
+
let value = ${_(e)}::get_field<DappKey>(dapp_hub, ${m}, key_tuple, 0);
|
|
289
297
|
let mut bsc_type = sui::bcs::new(value);
|
|
290
|
-
${Object.values(
|
|
298
|
+
${Object.values(a)[0]==="string"||Object.values(a)[0]==="String"?"let value = dubhe::bcs::peel_string(&mut bsc_type);":Object.values(a)[0]==="vector<String>"?"let value = dubhe::bcs::peel_vec_string(&mut bsc_type);":w(Object.values(a)[0])?`let value = sui::bcs::peel_${E(Object.values(a)[0])}(&mut bsc_type);`:`let value = ${e}::${c.find(r=>r.type===Object.values(a)[0])?.module}::decode(&mut bsc_type);`}
|
|
291
299
|
value
|
|
292
300
|
}
|
|
293
301
|
|
|
294
|
-
public(package) fun set(dapp_hub: &mut DappHub${
|
|
295
|
-
${
|
|
302
|
+
public(package) fun set(dapp_hub: &mut DappHub${i||p?", ":""}${i}${y}${p}, value: ${Object.values(a)[0]==="string"||Object.values(a)[0]==="String"?"String":Object.values(a)[0]}, ctx: &mut TxContext) {
|
|
303
|
+
${g}
|
|
296
304
|
let value_tuple = encode(value);
|
|
297
|
-
${
|
|
298
|
-
}`:D?` public(package) fun set(dapp_hub: &mut DappHub${
|
|
299
|
-
${
|
|
300
|
-
let value_tuple = encode(${
|
|
301
|
-
${
|
|
302
|
-
}`:` public fun get(dapp_hub: &DappHub${
|
|
303
|
-
${
|
|
304
|
-
let value_tuple = ${
|
|
305
|
+
${_(e)}::set_record(dapp_hub, dapp_key::new(), key_tuple, value_tuple, ${m}, OFFCHAIN, ctx);
|
|
306
|
+
}`:D?` public(package) fun set(dapp_hub: &mut DappHub${i||p?", ":""}${i}${y}${p}, ${b.map(r=>`${r}: ${s[r]==="string"||s[r]==="String"?"String":s[r]}`).join(", ")}, ctx: &mut TxContext) {
|
|
307
|
+
${g}
|
|
308
|
+
let value_tuple = encode(${b.join(", ")});
|
|
309
|
+
${_(e)}::set_record(dapp_hub, dapp_key::new(), key_tuple, value_tuple, ${m}, OFFCHAIN, ctx);
|
|
310
|
+
}`:` public fun get(dapp_hub: &DappHub${i||p?", ":""}${i}${y}${p}): (${Object.values(a).map(r=>r==="string"||r==="String"?"String":r).join(", ")}) {
|
|
311
|
+
${g}
|
|
312
|
+
let value_tuple = ${_(e)}::get_record<DappKey>(dapp_hub, ${m}, key_tuple);
|
|
305
313
|
let mut bsc_type = sui::bcs::new(value_tuple);
|
|
306
|
-
${
|
|
314
|
+
${b.map(r=>{let d=s[r],l=!w(d),S=l?c.find(C=>C.type===d):null;return`let ${r} = ${d==="string"||d==="String"?"dubhe::bcs::peel_string(&mut bsc_type)":d==="vector<String>"?"dubhe::bcs::peel_vec_string(&mut bsc_type)":l?`${e}::${S?.module}::decode(&mut bsc_type)`:`sui::bcs::peel_${E(d)}(&mut bsc_type)`};`}).join(`
|
|
307
315
|
`)}
|
|
308
|
-
(${
|
|
316
|
+
(${b.join(", ")})
|
|
309
317
|
}
|
|
310
318
|
|
|
311
|
-
public(package) fun set(dapp_hub: &mut DappHub${
|
|
312
|
-
${
|
|
313
|
-
let value_tuple = encode(${
|
|
314
|
-
${
|
|
315
|
-
}`,$=
|
|
316
|
-
${
|
|
319
|
+
public(package) fun set(dapp_hub: &mut DappHub${i||p?", ":""}${i}${y}${p}, ${b.map(r=>`${r}: ${s[r]==="string"||s[r]==="String"?"String":s[r]}`).join(", ")}, ctx: &mut TxContext) {
|
|
320
|
+
${g}
|
|
321
|
+
let value_tuple = encode(${b.join(", ")});
|
|
322
|
+
${_(e)}::set_record(dapp_hub, dapp_key::new(), key_tuple, value_tuple, ${m}, OFFCHAIN, ctx);
|
|
323
|
+
}`,$=u?D?` public(package) fun set_struct(dapp_hub: &mut DappHub${i||p?", ":""}${i}${y}${p}, ${t}: ${k(t)}, ctx: &mut TxContext) {
|
|
324
|
+
${g}
|
|
317
325
|
let value_tuple = encode_struct(${t});
|
|
318
|
-
${
|
|
319
|
-
}`:` public fun get_struct(dapp_hub: &DappHub${
|
|
320
|
-
${
|
|
321
|
-
let value_tuple = ${
|
|
326
|
+
${_(e)}::set_record(dapp_hub, dapp_key::new(), key_tuple, value_tuple, ${m}, OFFCHAIN, ctx);
|
|
327
|
+
}`:` public fun get_struct(dapp_hub: &DappHub${i||p?", ":""}${i}${y}${p}): ${k(t)} {
|
|
328
|
+
${g}
|
|
329
|
+
let value_tuple = ${_(e)}::get_record<DappKey>(dapp_hub, ${m}, key_tuple);
|
|
322
330
|
decode(value_tuple)
|
|
323
331
|
}
|
|
324
332
|
|
|
325
|
-
public(package) fun set_struct(dapp_hub: &mut DappHub${
|
|
326
|
-
${
|
|
333
|
+
public(package) fun set_struct(dapp_hub: &mut DappHub${i||p?", ":""}${i}${y}${p}, ${t}: ${k(t)}, ctx: &mut TxContext) {
|
|
334
|
+
${g}
|
|
327
335
|
let value_tuple = encode_struct(${t});
|
|
328
|
-
${
|
|
329
|
-
}`:"",
|
|
336
|
+
${_(e)}::set_record(dapp_hub, dapp_key::new(), key_tuple, value_tuple, ${m}, OFFCHAIN, ctx);
|
|
337
|
+
}`:"",H=x?` public fun encode(value: ${Object.values(a)[0]==="string"||Object.values(a)[0]==="String"?"String":Object.values(a)[0]}): vector<vector<u8>> {
|
|
330
338
|
let mut value_tuple = vector::empty();
|
|
331
|
-
value_tuple.push_back(${Object.values(
|
|
339
|
+
value_tuple.push_back(${Object.values(a)[0]==="string"||Object.values(a)[0]==="String"?"to_bytes(&into_bytes(value))":Object.values(a)[0]==="vector<String>"||w(Object.values(a)[0])?"to_bytes(&value)":`${e}::${c.find(r=>r.type===Object.values(a)[0])?.module}::encode(value)`});
|
|
332
340
|
value_tuple
|
|
333
|
-
}`:
|
|
341
|
+
}`:u?D?` public fun encode(${b.map(r=>`${r}: ${s[r]==="string"||s[r]==="String"?"String":s[r]}`).join(", ")}): vector<vector<u8>> {
|
|
334
342
|
let mut value_tuple = vector::empty();
|
|
335
|
-
${
|
|
343
|
+
${b.map(r=>{let d=s[r],l=!w(d),S=l?c.find(C=>C.type===d):null;return`value_tuple.push_back(${d==="string"||d==="String"?`to_bytes(&into_bytes(${r}))`:d==="vector<String>"?`to_bytes(&${r})`:l?`${e}::${S?.module}::encode(${r})`:`to_bytes(&${r})`});`}).join(`
|
|
336
344
|
`)}
|
|
337
345
|
value_tuple
|
|
338
346
|
}
|
|
339
347
|
|
|
340
348
|
public fun encode_struct(${t}: ${k(t)}): vector<vector<u8>> {
|
|
341
|
-
encode(${
|
|
342
|
-
}`:` public fun encode(${
|
|
349
|
+
encode(${b.map(r=>`${t}.${r}`).join(", ")})
|
|
350
|
+
}`:` public fun encode(${b.map(r=>`${r}: ${s[r]==="string"||s[r]==="String"?"String":s[r]}`).join(", ")}): vector<vector<u8>> {
|
|
343
351
|
let mut value_tuple = vector::empty();
|
|
344
|
-
${
|
|
352
|
+
${b.map(r=>{let d=s[r],l=!w(d),S=l?c.find(C=>C.type===d):null;return`value_tuple.push_back(${d==="string"||d==="String"?`to_bytes(&into_bytes(${r}))`:d==="vector<String>"?`to_bytes(&${r})`:l?`${e}::${S?.module}::encode(${r})`:`to_bytes(&${r})`});`}).join(`
|
|
345
353
|
`)}
|
|
346
354
|
value_tuple
|
|
347
355
|
}
|
|
348
356
|
|
|
349
357
|
public fun encode_struct(${t}: ${k(t)}): vector<vector<u8>> {
|
|
350
|
-
encode(${
|
|
358
|
+
encode(${b.map(r=>`${t}.${r}`).join(", ")})
|
|
351
359
|
}
|
|
352
360
|
|
|
353
361
|
public fun decode(data: vector<u8>): ${k(t)} {
|
|
354
362
|
let mut bsc_type = sui::bcs::new(data);
|
|
355
|
-
${
|
|
363
|
+
${b.map(r=>{let d=s[r],l=!w(d),S=l?c.find(C=>C.type===d):null;return`let ${r} = ${d==="string"||d==="String"?"string(sui::bcs::peel_vec_u8(&mut bsc_type))":d==="vector<String>"?"dubhe::bcs::peel_vec_string(&mut bsc_type)":l?`${e}::${S?.module}::decode(&mut bsc_type)`:`sui::bcs::peel_${E(d)}(&mut bsc_type)`};`}).join(`
|
|
356
364
|
`)}
|
|
357
365
|
${k(t)} {
|
|
358
|
-
${
|
|
366
|
+
${b.map(r=>`${r},`).join(`
|
|
359
367
|
`)}
|
|
360
368
|
}
|
|
361
|
-
}`:"";return[
|
|
369
|
+
}`:"";return[T,I,W,o,$,H].filter(r=>r.trim().length>0).join(`
|
|
362
370
|
|
|
363
|
-
`)}function k(e){return e.split("_").map((t,
|
|
364
|
-
\u{1F680} Starting Schema Generation Process...`),console.log("\u{1F4CB} Project Configuration:"),console.log(` \u2514\u2500 Name: ${t.name}`),console.log(` \u2514\u2500 Description: ${t.description||"No description provided"}`),console.log(` \u2514\u2500 Network: ${
|
|
371
|
+
`)}function k(e){return e.split("_").map((t,s)=>/^\d+$/.test(t)?t:t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join("")}function E(e){if(e.startsWith("vector<")){let t=e.slice(7,-1);return t==="vector<u8>"?"vec_vec_u8":t==="String"?"vec_string":`vec_${E(t)}`}switch(e){case"u8":return"u8";case"u16":return"u16";case"u32":return"u32";case"u64":return"u64";case"u128":return"u128";case"u256":return"u256";case"bool":return"bool";case"address":return"address";case"String":return"string";default:return e}}import O from"node:path";async function vt(e,t,s){console.log(`
|
|
372
|
+
\u{1F680} Starting Schema Generation Process...`),console.log("\u{1F4CB} Project Configuration:"),console.log(` \u2514\u2500 Name: ${t.name}`),console.log(` \u2514\u2500 Description: ${t.description||"No description provided"}`),console.log(` \u2514\u2500 Network: ${s||"testnet"}`),console.log(e);let n=O.join(e,"src",t.name);A(`${n}`)&&G(`${n}/sources/codegen`),A(`${n}/Move.toml`)||await X(t,e);let u=O.join(n,"sources","codegen","genesis.move");A(u)||await ue(t,u);let c=O.join(n,"sources","codegen","init_test.move");A(c)||await oe(t,c);let h=O.join(n,"sources","codegen","dapp_key.move");A(h)||await ee(t,h);let v=O.join(n,"sources","scripts","deploy_hook.move");A(v)||await Q(t,v);let a=O.join(n,"sources","codegen","resources");A(a)?await U(t,a):await U(t,a);let b=O.join(n,"sources","codegen","enums");A(b)||await pe(t,b),t.errors&&await ne(t.name,t.errors,e),await re(t,e),await N(t,e),console.log(`
|
|
365
373
|
\u2705 Schema Generation Process Complete!
|
|
366
|
-
`)}var
|
|
374
|
+
`)}var St=e=>e;import{findUp as fe}from"find-up";import V from"path";import he from"esbuild";var K=class extends Error{name="NotInsideProjectError";message="You are not inside a Dubhe project"};import{rmSync as ve}from"fs";import{pathToFileURL as ke}from"url";import Se from"os";var xe=["dubhe.config.js","dubhe.config.mjs","dubhe.config.ts","dubhe.config.mts"],z="dubhe.config.example.mjs";async function Mt(e){e=await ce(e);try{return await he.build({entryPoints:[e],format:"esm",outfile:z,platform:"node",bundle:!0,packages:"external"}),e=await ce(z,!0),(await import(e+`?update=${Date.now()}`)).dubheConfig}finally{ve(z,{force:!0})}}async function ce(e,t){return e===void 0?e=await De():V.isAbsolute(e)||(e=V.join(process.cwd(),e),e=V.normalize(e)),t&&Se.platform()==="win32"?ke(e).href:e}async function De(){let e=await fe(xe);if(e===void 0)throw new K;return e}var Ce=(s=>(s.Event="event",s.Schema="schema",s))(Ce||{});export{Ce as SubscriptionKind,St as defineConfig,f as formatAndWriteMove,Oe as formatMove,Mt as loadConfig,vt as schemaGen};
|
|
367
375
|
//# sourceMappingURL=index.js.map
|