@0xobelisk/sui-common 0.5.8 → 0.5.10
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.js +131 -376
- package/dist/index.js.map +1 -1
- package/package.json +11 -7
- package/src/codegen/debug.ts +10 -0
- package/src/codegen/types/index.ts +23 -24
- package/src/codegen/utils/format.ts +4 -7
- package/src/codegen/utils/formatAndWrite.ts +31 -14
- package/src/codegen/utils/index.ts +0 -1
- package/src/codegen/utils/renderMove/common.ts +9 -422
- package/src/codegen/utils/renderMove/generateDappKey.ts +19 -0
- package/src/codegen/utils/renderMove/generateSchema.ts +184 -197
- package/src/codegen/utils/renderMove/generateScript.ts +43 -12
- package/src/codegen/utils/renderMove/generateSystem.ts +8 -38
- package/src/codegen/utils/renderMove/generateToml.ts +4 -5
- package/src/codegen/utils/renderMove/worldgen.ts +15 -17
- package/src/debug.ts +10 -0
- package/src/codegen/utils/renderMove/generateAppKey.ts +0 -24
- package/src/codegen/utils/renderMove/generateEntityKey.ts +0 -52
- package/src/codegen/utils/renderMove/generateEps.ts +0 -159
- package/src/codegen/utils/renderMove/generateInit.ts +0 -45
package/dist/index.js
CHANGED
|
@@ -1,403 +1,158 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
`}function h(e,t){
|
|
7
|
-
${e} {
|
|
8
|
-
${c(t," ").join(`,
|
|
9
|
-
`)}
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
`}function N(e){return` public fun register(_obelisk_world: &mut World, admin_cap: &AdminCap, ctx: &mut TxContext) {
|
|
13
|
-
schema::add<Table<address,${e}>>(_obelisk_world, SCHEMA_ID, table::new<address, ${e}>(ctx), admin_cap);
|
|
14
|
-
}`}function R(e,t){return` public(package) fun set(_obelisk_world: &mut World, _obelisk_entity_key: address, ${d(t," ")}) {
|
|
15
|
-
let _obelisk_schema = schema::get_mut<Table<address,${e}>, AppKey>(app_key::new(), _obelisk_world, SCHEMA_ID);
|
|
16
|
-
let _obelisk_data = new(${c(t," ")});
|
|
17
|
-
if(table::contains<address, ${e}>(_obelisk_schema, _obelisk_entity_key)) {
|
|
18
|
-
*table::borrow_mut<address, ${e}>(_obelisk_schema, _obelisk_entity_key) = _obelisk_data;
|
|
19
|
-
} else {
|
|
20
|
-
table::add(_obelisk_schema, _obelisk_entity_key, _obelisk_data);
|
|
21
|
-
};
|
|
22
|
-
events::emit_set(SCHEMA_ID, SCHEMA_TYPE, some(_obelisk_entity_key), _obelisk_data)
|
|
23
|
-
}
|
|
24
|
-
`}function O(e){return` public(package) fun remove(_obelisk_world: &mut World, _obelisk_entity_key: address) {
|
|
25
|
-
let _obelisk_schema = schema::get_mut<Table<address,${e}>, AppKey>(app_key::new(),_obelisk_world, SCHEMA_ID);
|
|
26
|
-
assert!(table::contains<address, ${e}>(_obelisk_schema, _obelisk_entity_key), EEntityDoesNotExist);
|
|
27
|
-
table::remove(_obelisk_schema, _obelisk_entity_key);
|
|
28
|
-
events::emit_remove(SCHEMA_ID, _obelisk_entity_key)
|
|
29
|
-
}
|
|
30
|
-
`}function I(e,t){return typeof t=="string"?"":`
|
|
31
|
-
`+Object.entries(t).map(([r,s])=>` public(package) fun set_${r}(_obelisk_world: &mut World, _obelisk_entity_key: address, ${r}: ${s}) {
|
|
32
|
-
let _obelisk_schema = schema::get_mut<Table<address,${e}>, AppKey>(app_key::new(),_obelisk_world, SCHEMA_ID);
|
|
33
|
-
assert!(table::contains<address, ${e}>(_obelisk_schema, _obelisk_entity_key), EEntityDoesNotExist);
|
|
34
|
-
let _obelisk_data = table::borrow_mut<address, ${e}>(_obelisk_schema, _obelisk_entity_key);
|
|
35
|
-
_obelisk_data.${r} = ${r};
|
|
36
|
-
events::emit_set(SCHEMA_ID, SCHEMA_TYPE, some(_obelisk_entity_key), *_obelisk_data)
|
|
37
|
-
}
|
|
38
|
-
`).join(`
|
|
39
|
-
`)}function D(e,t){return` public fun get(_obelisk_world: &World, _obelisk_entity_key: address): ${j(t)} {
|
|
40
|
-
let _obelisk_schema = schema::get<Table<address,${e}>>(_obelisk_world, SCHEMA_ID);
|
|
41
|
-
assert!(table::contains<address, ${e}>(_obelisk_schema, _obelisk_entity_key), EEntityDoesNotExist);
|
|
42
|
-
let _obelisk_data = table::borrow<address, ${e}>(_obelisk_schema, _obelisk_entity_key);
|
|
43
|
-
(
|
|
44
|
-
${se(t," ").join(`,
|
|
45
|
-
`)}
|
|
46
|
-
)
|
|
47
|
-
}
|
|
48
|
-
`}function F(e,t){return typeof t=="string"?"":`
|
|
49
|
-
`+Object.entries(t).map(([r,s])=>` public fun get_${r}(_obelisk_world: &World, _obelisk_entity_key: address): ${s} {
|
|
50
|
-
let _obelisk_schema = schema::get<Table<address,${e}>>(_obelisk_world, SCHEMA_ID);
|
|
51
|
-
assert!(table::contains<address, ${e}>(_obelisk_schema, _obelisk_entity_key), EEntityDoesNotExist);
|
|
52
|
-
let _obelisk_data = table::borrow<address, ${e}>(_obelisk_schema, _obelisk_entity_key);
|
|
53
|
-
_obelisk_data.${r}
|
|
54
|
-
}
|
|
55
|
-
`).join(`
|
|
56
|
-
`)}function H(e){return` public fun contains(_obelisk_world: &World, _obelisk_entity_key: address): bool {
|
|
57
|
-
let _obelisk_schema = schema::get<Table<address,${e}>>(_obelisk_world, SCHEMA_ID);
|
|
58
|
-
table::contains<address, ${e}>(_obelisk_schema, _obelisk_entity_key)
|
|
59
|
-
}`}function V(e,t,r){return` public fun register(_obelisk_world: &mut World, admin_cap: &AdminCap, _ctx: &mut TxContext) {
|
|
60
|
-
let _obelisk_schema = new(${re(t,r)});
|
|
61
|
-
schema::add<${e}>(_obelisk_world, SCHEMA_ID, _obelisk_schema, admin_cap);
|
|
62
|
-
events::emit_set(SCHEMA_ID, SCHEMA_TYPE, none(), _obelisk_schema);
|
|
63
|
-
}`}function B(e,t){return` public(package) fun set(_obelisk_world: &mut World, ${d(t," ")}) {
|
|
64
|
-
let _obelisk_schema = schema::get_mut<${e}, AppKey>(app_key::new(),_obelisk_world, SCHEMA_ID);
|
|
65
|
-
${typeof t=="string"?` _obelisk_schema.value = value;
|
|
66
|
-
events::emit_set(SCHEMA_ID, SCHEMA_TYPE, none(), _obelisk_schema.value);`:` let _obelisk_data = new(${c(t," ")});
|
|
67
|
-
*_obelisk_schema = _obelisk_data;
|
|
68
|
-
events::emit_set(SCHEMA_ID, SCHEMA_TYPE, none(), _obelisk_data);`}
|
|
69
|
-
}`}function K(e,t){return typeof t=="string"?"":`
|
|
70
|
-
`+Object.entries(t).map(([r,s])=>`
|
|
71
|
-
public(package) fun set_${r}(_obelisk_world: &mut World, ${r}: ${s}) {
|
|
72
|
-
let _obelisk_schema = schema::get_mut<${e}, AppKey>(app_key::new(),_obelisk_world, SCHEMA_ID);
|
|
73
|
-
_obelisk_schema.${r} = ${r};
|
|
74
|
-
events::emit_set(SCHEMA_ID, SCHEMA_TYPE, none(), *_obelisk_schema)
|
|
75
|
-
}`).join(`
|
|
76
|
-
`)}function U(e,t){return` public fun get(_obelisk_world: &World): ${j(t)} {
|
|
77
|
-
let _obelisk_schema = schema::get<${e}>(_obelisk_world, SCHEMA_ID);
|
|
78
|
-
(
|
|
79
|
-
${typeof t=="string"?" _obelisk_schema.value":Object.entries(t).map(([r,s])=>` _obelisk_schema.${r},`).join(`
|
|
80
|
-
`)}
|
|
81
|
-
)
|
|
82
|
-
}`}function Y(e,t){return typeof t=="string"?"":`
|
|
83
|
-
`+Object.entries(t).map(([r,s])=>`
|
|
84
|
-
public fun get_${r}(_obelisk_world: &World): ${s} {
|
|
85
|
-
let _obelisk_schema = schema::get<${e}>(_obelisk_world, SCHEMA_ID);
|
|
86
|
-
_obelisk_schema.${r}
|
|
87
|
-
}`).join(`
|
|
88
|
-
`)}import{existsSync as oe}from"fs";function G(e,t){e.systems.map(r=>{if(!oe(`${t}/contracts/${e.name}/sources/system/${r}.move`)){let s=`module ${e.name}::${r} {
|
|
1
|
+
import y from"prettier";import I from"prettier-plugin-move";async function S(e,t){let r;t&&(r=await y.resolveConfig(t));try{return y.format(e,{plugins:[I],parser:"move-parse",printWidth:120,semi:!0,tabWidth:2,useTabs:!1,bracketSpacing:!0,...r})}catch(o){let i;return o instanceof Error?i=o.message:i=o,console.log(`Error during output formatting: ${i}`),e}}async function h(e){return y.format(e,{parser:"typescript"})}import $ from"node:fs/promises";import A from"node:path";import v from"debug";var f=v("obelisk:common"),U=v("obelisk:common");f.log=console.debug.bind(console);U.log=console.error.bind(console);var g=f.extend("codegen"),B=f.extend("codegen");g.log=console.debug.bind(console);B.log=console.error.bind(console);async function a(e,t,r){let o=await S(e),s=` // Copyright (c) Obelisk Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
#[allow(unused_use)]
|
|
4
|
+
|
|
5
|
+
/* Autogenerated file. Do not edit manually. */
|
|
6
|
+
`+o;(t.includes("deploy_hook")||t.includes(".toml")||t.includes("system"))&&(s=o),await $.mkdir(A.dirname(t),{recursive:!0}),await $.writeFile(t,s),g(`${r}: ${t}`)}async function ue(e,t,r){let o=await h(e);await $.mkdir(A.dirname(t),{recursive:!0}),await $.writeFile(t,o),g(`${r}: ${t}`)}function le(e){return e.replace(/\\/g,"/")}import{existsSync as k}from"fs";import d from"fs";function _(e){d.existsSync(e)&&(d.readdirSync(e).forEach(t=>{let r=`${e}/${t}`;d.lstatSync(r).isDirectory()?_(r):d.unlinkSync(r)}),d.rmdirSync(e))}function T(e){return Object.entries(e).map(([t,r])=>`${t}`).join(",")}function M(e){return typeof e=="string"?e:`(${Object.entries(e).map(([t,r])=>`${r}`)})`}function l(e){return Object.entries(e).map(([t,r])=>`${t}: ${r}`)}function C(e,t){return typeof e=="string"?[`${t}self.value`]:Object.entries(e).map(([r,o])=>`${t}self.${r}`)}import{existsSync as K}from"fs";async function D(e,t){e.systems.map(async r=>{if(!K(`${t}/contracts/${e.name}/sources/system/${r}.move`)){let o=`module ${e.name}::${r}_system {
|
|
89
7
|
|
|
90
8
|
}
|
|
91
|
-
`;
|
|
9
|
+
`;await a(o,`${t}/contracts/${e.name}/sources/system/${r}.move`,"formatAndWriteMove")}})}async function j(e,t){let r=`[package]
|
|
92
10
|
name = "${e.name}"
|
|
93
11
|
version = "0.0.1"
|
|
94
12
|
edition = "2024.beta"
|
|
95
13
|
|
|
96
14
|
[dependencies]
|
|
97
15
|
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.31.1" }
|
|
98
|
-
|
|
99
|
-
Obelisk = { local = "/Volumes/project/obelisk-engine/packages/obelisk-framework" }
|
|
16
|
+
Obelisk = { git = "https://github.com/0xobelisk/obelisk-engine.git", subdir = "packages/obelisk-framework", rev = "main" }
|
|
100
17
|
|
|
101
18
|
[addresses]
|
|
102
19
|
sui = "0x2"
|
|
103
|
-
obelisk = "
|
|
20
|
+
obelisk = "0x3dc2d6239eed38c9798444afbf4bada0998ec98edc365713864405fe64203256"
|
|
104
21
|
${e.name} = "0x0"
|
|
105
|
-
`;
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
public fun from_object<T: key + store>(object: &T): address {
|
|
111
|
-
object::id_address(object)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
public fun from_bytes(bytes: vector<u8>): address {
|
|
115
|
-
address::from_bytes(keccak256(&bytes))
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public fun from_u256(x: u256): address {
|
|
119
|
-
address::from_u256(x)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
public fun from_address_with_seed(addr: address, seed: vector<u8>): address {
|
|
123
|
-
let mut data = address::to_bytes(addr);
|
|
124
|
-
vector::append(&mut data, seed);
|
|
125
|
-
from_bytes(data)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
public fun from_address_with_u256(addr: address, x: u256): address {
|
|
129
|
-
let mut data = address::to_bytes(addr);
|
|
130
|
-
vector::append(&mut data, bcs::to_bytes<u256>(&x));
|
|
131
|
-
from_bytes(data)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
public fun from_object_with_seed<T: key + store>(object: &T, seed: vector<u8>): address {
|
|
135
|
-
let mut data = address::to_bytes(object::id_address(object));
|
|
136
|
-
vector::append(&mut data, seed);
|
|
137
|
-
from_bytes(data)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
public fun from_object_with_u256<T: key + store>(object: &T, x: u256): address {
|
|
141
|
-
let mut data = address::to_bytes(object::id_address(object));
|
|
142
|
-
vector::append(&mut data, bcs::to_bytes<u256>(&x));
|
|
143
|
-
from_bytes(data)
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
`;i(r,`${t}/contracts/${e.name}/sources/entity_key.move`,"formatAndWriteMove")}function P(e,t){let r=`#[allow(lint(share_owned))]
|
|
147
|
-
|
|
148
|
-
module ${e.name}::init {
|
|
149
|
-
use std::ascii::string;
|
|
150
|
-
use ${e.name}::app_key::AppKey;
|
|
151
|
-
use obelisk::access_control;
|
|
152
|
-
use obelisk::world;
|
|
153
|
-
${M(e.name,e.schemas).join(`
|
|
22
|
+
`;await a(r,`${t}/contracts/${e.name}/Move.toml`,"formatAndWriteMove")}function m(e){return e.split("_").map((t,r)=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join("")}function L(e,t){return Object.entries(t).map(([r,o])=>`public(package) fun set_${r}(self: &mut ${e}, ${r}: ${o}) {
|
|
23
|
+
self.${r} = ${r};
|
|
24
|
+
}`).join(`
|
|
25
|
+
`)}function V(e,t){return`public(package) fun set(self: &mut ${e}, ${l(t)}) {
|
|
26
|
+
${Object.entries(t).map(([r])=>`self.${r} = ${r};`).join(`
|
|
154
27
|
`)}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
28
|
+
}`}function N(e,t){return`public fun get(self: &${e}): ${M(t)} {
|
|
29
|
+
(${C(t,"")})
|
|
30
|
+
}`}function z(e,t){return Object.entries(t).map(([r,o])=>`public fun get_${r}(self: &${e}): ${o} {
|
|
31
|
+
self.${r}
|
|
32
|
+
}`).join(`
|
|
33
|
+
`)}function u(e){return e.replace(/([A-Z])/g,"_$1").toLowerCase().replace(/^_/,"")}async function O(e,t,r){for(let o in t){let i=t[o];if(i.data)for(let s of i.data){let c="",p=i.data.filter(n=>Array.isArray(n.fields)).map(n=>n.name);Array.isArray(s.fields)?c=`module ${e}::${o}_${u(s.name)} {
|
|
34
|
+
public enum ${s.name} has copy, drop , store {
|
|
35
|
+
${s.fields}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
${s.fields.map(n=>`public fun new_${u(n)}(): ${s.name} {
|
|
39
|
+
${s.name}::${n}
|
|
40
|
+
}`).join("")}`:c=`module ${e}::${o}_${u(s.name)} {
|
|
41
|
+
use std::ascii::String;
|
|
42
|
+
${p.map(n=>`use ${e}::${o}_${u(n)}::${n};`).join(`
|
|
164
43
|
`)}
|
|
165
44
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
45
|
+
public struct ${s.name} has copy, drop , store {
|
|
46
|
+
${l(s.fields)}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public fun new(${l(s.fields)}): ${s.name} {
|
|
50
|
+
${s.name} {
|
|
51
|
+
${T(s.fields)}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
${N(s.name,s.fields)}
|
|
56
|
+
${z(s.name,s.fields)}
|
|
57
|
+
${L(s.name,s.fields)}
|
|
58
|
+
${V(s.name,s.fields)}
|
|
59
|
+
}`,await a(c,`${r}/contracts/${e}/sources/codegen/schemas/${o}_${u(s.name)}.move`,"formatAndWriteMove")}}}function G(e,t,r){return r.data?r.data.map(o=>`use ${e}::${t}_${u(o.name)}::${o.name};`).join(`
|
|
60
|
+
`):""}async function W(e,t,r){for(let o in t){console.log(`Schema: ${o}`);let i=t[o],s=`module ${e}::${o}_schema {
|
|
61
|
+
use std::ascii::String;
|
|
62
|
+
use std::type_name;
|
|
63
|
+
use sui::transfer::{public_share_object};
|
|
64
|
+
use obelisk::dapps_system;
|
|
65
|
+
use obelisk::dapps_schema::Dapps;
|
|
66
|
+
use obelisk::storage_value::{Self, StorageValue};
|
|
67
|
+
use obelisk::storage_map::{Self, StorageMap};
|
|
68
|
+
use obelisk::storage_double_map::{Self, StorageDoubleMap};
|
|
69
|
+
use ${e}::dapp_key::DappKey;
|
|
70
|
+
${G(e,o,i)}
|
|
71
|
+
|
|
72
|
+
public struct ${m(o)} has key, store {
|
|
73
|
+
id: UID,
|
|
74
|
+
${l(i.structure)}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
${Object.entries(i.structure).map(([c,p])=>`public fun borrow_${c}(self: &${m(o)}) : &${p} {
|
|
78
|
+
&self.${c}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public(package) fun borrow_mut_${c}(self: &mut ${m(o)}): &mut ${p} {
|
|
82
|
+
&mut self.${c}
|
|
83
|
+
}
|
|
84
|
+
`).join("")}
|
|
85
|
+
|
|
86
|
+
public entry fun register(dapps: &mut Dapps, ctx: &mut TxContext) {
|
|
87
|
+
let package_id = dapps_system::current_package_id<DappKey>();
|
|
88
|
+
assert!(dapps.borrow_metadata().contains_key(package_id), 0);
|
|
89
|
+
assert!(dapps.borrow_admin().get(package_id) == ctx.sender(), 0);
|
|
90
|
+
let schema = type_name::get<${m(o)}>().into_string();
|
|
91
|
+
assert!(!dapps.borrow_schemas().get(package_id).contains(&schema), 0);
|
|
92
|
+
|
|
93
|
+
public_share_object(${m(o)} {
|
|
94
|
+
id: object::new(ctx),
|
|
95
|
+
${Object.entries(i.structure).map(([c,p])=>{let n="";return p.includes("StorageValue")?n="storage_value::new()":p.includes("StorageMap")?n="storage_map::new()":p.includes("StorageDoubleMap")&&(n="storage_double_map::new()"),`${c}: ${n},`}).join(" ")}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
dapps_system::add_schema<${m(o)}>(dapps, package_id, ctx);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
}`;await a(s,`${r}/contracts/${e}/sources/codegen/schemas/${o}.move`,"formatAndWriteMove")}}import{existsSync as H}from"fs";async function R(e,t){if(!H(`${t}/contracts/${e.name}/sources/script/deploy_hook.move`)){let r=`module ${e.name}::deploy_hook {
|
|
102
|
+
use obelisk::dapps_schema::Dapps;
|
|
103
|
+
use obelisk::dapps_system;
|
|
104
|
+
use ${e.name}::dapp_key::DappKey;
|
|
105
|
+
use std::ascii;
|
|
106
|
+
use sui::clock::Clock;
|
|
170
107
|
#[test_only]
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
${
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
${e.
|
|
189
|
-
|
|
190
|
-
use ${e.projectName}::app_key;
|
|
191
|
-
use ${e.projectName}::app_key::AppKey;
|
|
192
|
-
use obelisk::schema;
|
|
193
|
-
use obelisk::events;
|
|
194
|
-
use obelisk::world::{World, AdminCap};
|
|
195
|
-
|
|
196
|
-
const SCHEMA_ID: vector<u8> = b"${e.schemaName}";
|
|
197
|
-
const SCHEMA_TYPE: u8 = 1;
|
|
198
|
-
|
|
199
|
-
${g(e.valueType)}
|
|
200
|
-
${f(e.structName,e.valueType)}
|
|
201
|
-
${h(e.structName,e.valueType)}
|
|
202
|
-
${V(e.structName,e.realType,e.defaultValue)}
|
|
203
|
-
|
|
204
|
-
${B(e.structName,e.valueType)}${K(e.structName,e.valueType)}
|
|
205
|
-
|
|
206
|
-
${U(e.structName,e.valueType)}${Y(e.structName,e.valueType)}
|
|
207
|
-
}
|
|
208
|
-
`}function ce(e){return`module ${e.projectName}::${e.schemaName}_schema {
|
|
209
|
-
${e.needImportString?` use std::ascii::String;
|
|
210
|
-
`:" "}use std::option::some;
|
|
211
|
-
use ${e.projectName}::app_key;
|
|
212
|
-
use ${e.projectName}::app_key::AppKey;
|
|
213
|
-
use sui::table::{Self, Table};
|
|
214
|
-
use obelisk::schema;
|
|
215
|
-
use obelisk::events;
|
|
216
|
-
use obelisk::world::{World, AdminCap};
|
|
217
|
-
|
|
218
|
-
/// Entity does not exist
|
|
219
|
-
const EEntityDoesNotExist: u64 = 0;
|
|
220
|
-
|
|
221
|
-
const SCHEMA_ID: vector<u8> = b"${e.schemaName}";
|
|
222
|
-
const SCHEMA_TYPE: u8 = 0;
|
|
223
|
-
|
|
224
|
-
${g(e.valueType)}
|
|
225
|
-
${f(e.structName,e.valueType)}
|
|
226
|
-
${h(e.structName,e.valueType)}
|
|
227
|
-
${N(e.structName)}
|
|
228
|
-
|
|
229
|
-
${R(e.structName,e.valueType)}${I(e.structName,e.valueType)}
|
|
230
|
-
${D(e.structName,e.valueType)}${F(e.structName,e.valueType)}
|
|
231
|
-
${O(e.structName)}
|
|
232
|
-
${H(e.structName)}
|
|
233
|
-
}
|
|
234
|
-
`}import{existsSync as q}from"fs";function J(e,t){if(!q(`${t}/contracts/${e.name}/sources/script/deploy_hook.move`)){let r=`module ${e.name}::deploy_hook {
|
|
235
|
-
use obelisk::world::{World, AdminCap};
|
|
236
|
-
|
|
237
|
-
/// Not the right admin for this world
|
|
238
|
-
const ENotAdmin: u64 = 0;
|
|
239
|
-
|
|
240
|
-
public entry fun run(world: &mut World, admin_cap: &AdminCap) {
|
|
241
|
-
assert!(world.admin() == object::id(admin_cap), ENotAdmin);
|
|
108
|
+
use obelisk::dapps_schema;
|
|
109
|
+
#[test_only]
|
|
110
|
+
use sui::clock;
|
|
111
|
+
#[test_only]
|
|
112
|
+
use sui::test_scenario;
|
|
113
|
+
#[test_only]
|
|
114
|
+
use sui::test_scenario::Scenario;
|
|
115
|
+
|
|
116
|
+
public entry fun run(dapps: &mut Dapps, clock: &Clock, ctx: &mut TxContext) {
|
|
117
|
+
// Register the dapp to obelisk.
|
|
118
|
+
dapps_system::register<DappKey>(
|
|
119
|
+
dapps,
|
|
120
|
+
ascii::string(b"${e.name}"),
|
|
121
|
+
ascii::string(b"${e.description}"),
|
|
122
|
+
clock,
|
|
123
|
+
ctx
|
|
124
|
+
);
|
|
125
|
+
${Object.keys(e.schemas).map(o=>`${e.name}::${o}_schema::register(dapps, ctx);`).join(`
|
|
126
|
+
`)}
|
|
242
127
|
|
|
243
128
|
// Logic that needs to be automated once the contract is deployed
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
#[test_only]
|
|
248
|
-
public fun deploy_hook_for_testing(world: &mut World, admin_cap: &AdminCap){
|
|
249
|
-
run(world, admin_cap)
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
`;i(r,`${t}/contracts/${e.name}/sources/script/deploy_hook.move`,"formatAndWriteMove")}}function X(e,t){if(!q(`${t}/contracts/${e.name}/sources/script/migrate.move`)){let r=`module ${e.name}::migrate {
|
|
253
|
-
use obelisk::world::{World, AdminCap};
|
|
254
|
-
|
|
255
|
-
/// Not the right admin for this world
|
|
256
|
-
const ENotAdmin: u64 = 0;
|
|
257
|
-
const EWrongVersion: u64 = 1;
|
|
258
|
-
const ENotUpgrade: u64 = 2;
|
|
259
|
-
const VERSION: u64 = 1;
|
|
260
129
|
|
|
261
|
-
public entry fun run(world: &mut World, admin_cap: &AdminCap) {
|
|
262
|
-
assert!(world.admin() == object::id(admin_cap), ENotAdmin);
|
|
263
|
-
assert!(world.version() < VERSION, ENotUpgrade);
|
|
264
|
-
*obelisk::world::mut_version(world, admin_cap) = VERSION;
|
|
265
130
|
}
|
|
266
131
|
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
132
|
+
#[test_only]
|
|
133
|
+
public fun deploy_hook_for_testing(): (Scenario, Dapps) {
|
|
134
|
+
let mut scenario = test_scenario::begin(@0xA);
|
|
135
|
+
{
|
|
136
|
+
let ctx = test_scenario::ctx(&mut scenario);
|
|
137
|
+
dapps_schema::init_dapps_for_testing(ctx);
|
|
138
|
+
test_scenario::next_tx(&mut scenario,@0xA);
|
|
139
|
+
};
|
|
140
|
+
let mut dapps = test_scenario::take_shared<Dapps>(&scenario);
|
|
141
|
+
let ctx = test_scenario::ctx(&mut scenario);
|
|
142
|
+
let clock = clock::create_for_testing(ctx);
|
|
143
|
+
run(&mut dapps, &clock, ctx);
|
|
144
|
+
clock::destroy_for_testing(clock);
|
|
145
|
+
test_scenario::next_tx(&mut scenario,@0xA);
|
|
146
|
+
(scenario, dapps)
|
|
147
|
+
}
|
|
270
148
|
}
|
|
271
|
-
`;
|
|
149
|
+
`;await a(r,`${t}/contracts/${e.name}/sources/script/deploy_hook.move`,"formatAndWriteMove")}}async function E(e,t){let r=`module ${e.name}::dapp_key {
|
|
272
150
|
/// Authorization token for the app.
|
|
273
|
-
public struct
|
|
274
|
-
|
|
275
|
-
public(package) fun new(): AppKey {
|
|
276
|
-
AppKey { }
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
`;i(r,`${t}/contracts/${e.name}/sources/codegen/app_key.move`,"formatAndWriteMove")}function it(e,t){let r="";t===void 0?r=process.cwd():r=t,de(`${r}/contracts/${e.name}`)?k(`${r}/contracts/${e.name}/sources/codegen`):(z(e,r),L(e,r)),G(e,r),J(e,r),X(e,r),Q(e,r),P(e,r),Z(e,r)}function ct(e,t,r){me(e,t,r),le(e,t)}function me(e,t,r){r===void 0&&(r=1);let s=`module ${e}::world {
|
|
280
|
-
use std::ascii::{String, string};
|
|
281
|
-
use std::vector;
|
|
282
|
-
use sui::tx_context::TxContext;
|
|
283
|
-
use sui::bag::{Self, Bag};
|
|
284
|
-
use sui::object::{Self, UID, ID};
|
|
285
|
-
|
|
286
|
-
const VERSION: u64 = ${r};
|
|
287
|
-
|
|
288
|
-
/// Schema does not exist
|
|
289
|
-
const ESchemaDoesNotExist: u64 = 0;
|
|
290
|
-
/// Schema already exists
|
|
291
|
-
const ESchemaAlreadyExists: u64 = 1;
|
|
292
|
-
/// Not the right admin for this world
|
|
293
|
-
const ENotAdmin: u64 = 2;
|
|
294
|
-
/// Migration is not an upgrade
|
|
295
|
-
const ENotUpgrade: u64 = 3;
|
|
296
|
-
/// Calling functions from the wrong package version
|
|
297
|
-
const EWrongVersion: u64 = 4;
|
|
298
|
-
|
|
299
|
-
struct AdminCap has key, store {
|
|
300
|
-
id: UID,
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
struct World has key, store {
|
|
304
|
-
id: UID,
|
|
305
|
-
/// Name of the world
|
|
306
|
-
name: String,
|
|
307
|
-
/// Description of the world
|
|
308
|
-
description: String,
|
|
309
|
-
/// Schemas of the world
|
|
310
|
-
schemas: Bag,
|
|
311
|
-
/// Schema names of the world
|
|
312
|
-
schema_names: vector<String>,
|
|
313
|
-
/// admin of the world
|
|
314
|
-
admin: ID,
|
|
315
|
-
/// Version of the world
|
|
316
|
-
version: u64
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
public fun create(name: String, description: String, ctx: &mut TxContext): (World, AdminCap) {
|
|
320
|
-
let admin_cap = AdminCap {
|
|
321
|
-
id: object::new(ctx),
|
|
322
|
-
};
|
|
323
|
-
let _obelisk_world = World {
|
|
324
|
-
id: object::new(ctx),
|
|
325
|
-
name,
|
|
326
|
-
description,
|
|
327
|
-
schemas: bag::new(ctx),
|
|
328
|
-
schema_names: vector::empty(),
|
|
329
|
-
admin: object::id(&admin_cap),
|
|
330
|
-
version: VERSION
|
|
331
|
-
};
|
|
332
|
-
(_obelisk_world, admin_cap)
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
public fun get_admin(_obelisk_world: &World): ID {
|
|
336
|
-
_obelisk_world.admin
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
public fun info(_obelisk_world: &World): (String, String, u64) {
|
|
340
|
-
(_obelisk_world.name, _obelisk_world.description, _obelisk_world.version)
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
public fun schema_names(_obelisk_world: &World): vector<String> {
|
|
344
|
-
_obelisk_world.schema_names
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
public fun get_schema<T : store>(_obelisk_world: &World, _obelisk_schema_id: vector<u8>): &T {
|
|
348
|
-
assert!(_obelisk_world.version == VERSION, EWrongVersion);
|
|
349
|
-
assert!(bag::contains(&_obelisk_world.schemas, _obelisk_schema_id), ESchemaDoesNotExist);
|
|
350
|
-
bag::borrow<vector<u8>, T>(&_obelisk_world.schemas, _obelisk_schema_id)
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
public fun get_mut_schema<T : store>(_obelisk_world: &mut World, _obelisk_schema_id: vector<u8>): &mut T {
|
|
354
|
-
assert!(_obelisk_world.version == VERSION, EWrongVersion);
|
|
355
|
-
assert!(bag::contains(&_obelisk_world.schemas, _obelisk_schema_id), ESchemaDoesNotExist);
|
|
356
|
-
bag::borrow_mut<vector<u8>, T>(&mut _obelisk_world.schemas, _obelisk_schema_id)
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
public fun add_schema<T : store>(_obelisk_world: &mut World, _obelisk_schema_id: vector<u8>, schema: T, admin_cap: &AdminCap){
|
|
360
|
-
assert!(_obelisk_world.admin == object::id(admin_cap), ENotAdmin);
|
|
361
|
-
assert!(_obelisk_world.version == VERSION, EWrongVersion);
|
|
362
|
-
assert!(!bag::contains(&_obelisk_world.schemas, _obelisk_schema_id), ESchemaAlreadyExists);
|
|
363
|
-
vector::push_back(&mut _obelisk_world.schema_names, string(_obelisk_schema_id));
|
|
364
|
-
bag::add<vector<u8>,T>(&mut _obelisk_world.schemas, _obelisk_schema_id, schema);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
public fun contains(_obelisk_world: &mut World, _obelisk_schema_id: vector<u8>): bool {
|
|
368
|
-
assert!(_obelisk_world.version == VERSION, EWrongVersion);
|
|
369
|
-
bag::contains(&mut _obelisk_world.schemas, _obelisk_schema_id)
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
entry fun migrate(_obelisk_world: &mut World, admin_cap: &AdminCap) {
|
|
373
|
-
assert!(_obelisk_world.admin == object::id(admin_cap), ENotAdmin);
|
|
374
|
-
assert!(_obelisk_world.version < VERSION, ENotUpgrade);
|
|
375
|
-
_obelisk_world.version = VERSION;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
`;i(s,`${t}/contracts/${e}/sources/codegen/eps/world.move`,"formatAndWriteMove")}function le(e,t){let r=`module ${e}::events {
|
|
379
|
-
use sui::event;
|
|
380
|
-
use std::option::Option;
|
|
381
|
-
|
|
382
|
-
struct SchemaSetRecord<T: copy + drop> has copy, drop {
|
|
383
|
-
_obelisk_schema_id: vector<u8>,
|
|
384
|
-
_obelisk_schema_type: u8, // obelisk_schema_type_enum => { 0: common, 1: singleton, 2: ephemeral }
|
|
385
|
-
_obelisk_entity_key: Option<address>,
|
|
386
|
-
data: T
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
struct SchemaRemoveRecord has copy, drop {
|
|
390
|
-
_obelisk_schema_id: vector<u8>,
|
|
391
|
-
_obelisk_entity_key: address
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
public fun emit_set<T: copy + drop>(_obelisk_schema_id: vector<u8>, _obelisk_schema_type: u8, _obelisk_entity_key: Option<address>, data: T) {
|
|
395
|
-
event::emit(SchemaSetRecord { _obelisk_schema_id, _obelisk_schema_type, _obelisk_entity_key, data})
|
|
396
|
-
}
|
|
151
|
+
public struct DappKey has drop {}
|
|
397
152
|
|
|
398
|
-
public fun
|
|
399
|
-
|
|
153
|
+
public(package) fun new(): DappKey {
|
|
154
|
+
DappKey { }
|
|
400
155
|
}
|
|
401
156
|
}
|
|
402
|
-
`;
|
|
157
|
+
`;await a(r,`${t}/contracts/${e.name}/sources/codegen/dapp_key.move`,"formatAndWriteMove")}async function Be(e,t){let r="";t===void 0?r=process.cwd():r=t,k(`${r}/contracts/${e.name}`)&&_(`${r}/contracts/${e.name}/sources/codegen`),k(`${r}/contracts/${e.name}/Move.toml`)||await j(e,r),k(`${r}/contracts/${e.name}/sources/script/deploy_hook.move`)||await R(e,r),await D(e,r),await O(e.name,e.schemas,r),await W(e.name,e.schemas,r),await E(e,r)}import{findUp as Q}from"find-up";import x from"path";import X from"esbuild";var b=class extends Error{name="NotInsideProjectError";message="You are not inside a Obelisk project"};import{rmSync as Y}from"fs";import{pathToFileURL as Z}from"url";import q from"os";var J=["obelisk.config.js","obelisk.config.mjs","obelisk.config.ts","obelisk.config.mts"],w="obelisk.config.example.mjs";async function Ye(e){e=await F(e);try{return await X.build({entryPoints:[e],format:"esm",outfile:w,platform:"node",bundle:!0,packages:"external"}),e=await F(w,!0),(await import(e+`?update=${Date.now()}`)).obeliskConfig}finally{Y(w,{force:!0})}}async function F(e,t){return e===void 0?e=await P():x.isAbsolute(e)||(e=x.join(process.cwd(),e),e=x.normalize(e)),t&&q.platform()==="win32"?Z(e).href:e}async function P(){let e=await Q(J);if(e===void 0)throw new b;return e}export{a as formatAndWriteMove,ue as formatAndWriteTypescript,S as formatMove,h as formatTypescript,Ye as loadConfig,le as posixPath,F as resolveConfigPath,Be as worldgen};
|
|
403
158
|
//# sourceMappingURL=index.js.map
|