@0xobelisk/sui-common 1.0.3 → 1.0.5

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.
@@ -13,7 +13,7 @@ export async function generateDefaultSchema(
13
13
  }
14
14
 
15
15
  async function generateDappSchemaMetadata(config: DubheConfig, srcPrefix: string) {
16
- const path = `${srcPrefix}/contracts/${config.name}/sources/codegen/schemas/default/dapp/metadata.move`
16
+ const path = `${srcPrefix}/contracts/${config.name}/sources/codegen/dapp/metadata.move`
17
17
  if (!existsSync(path)) {
18
18
  let code = `module ${config.name}::dapp_metadata {
19
19
  use std::ascii::String;
@@ -122,107 +122,108 @@ async function generateDappSchemaMetadata(config: DubheConfig, srcPrefix: string
122
122
 
123
123
 
124
124
  async function generateDappSchema(config: DubheConfig, srcPrefix: string) {
125
- const path = `${srcPrefix}/contracts/${config.name}/sources/codegen/schemas/default/dapp/schema.move`
125
+ const path = `${srcPrefix}/contracts/${config.name}/sources/codegen/dapp/schema.move`
126
126
  if (!existsSync(path)) {
127
127
  let code = `module ${config.name}::dapp_schema {
128
- use ${config.name}::dapp_metadata::DappMetadata;
129
- use dubhe::storage_value;
130
- use dubhe::storage_value::StorageValue;
131
- use dubhe::storage_migration;
132
- use sui::transfer::public_share_object;
133
- use dubhe::type_info;
134
-
135
- public struct Dapp has key, store { id: UID }
136
-
137
- public fun borrow_admin(self: &Dapp): &StorageValue<address> {
138
- storage_migration::borrow_field(&self.id, b"admin")
139
- }
140
-
141
- public(package) fun borrow_mut_admin(self: &mut Dapp): &mut StorageValue<address> {
142
- storage_migration::borrow_mut_field(&mut self.id, b"admin")
143
- }
144
-
145
- public fun borrow_package_id(self: &Dapp): &StorageValue<address> {
146
- storage_migration::borrow_field(&self.id, b"package_id")
147
- }
148
-
149
- public(package) fun borrow_mut_package_id(self: &mut Dapp): &mut StorageValue<address> {
150
- storage_migration::borrow_mut_field(&mut self.id, b"package_id")
151
- }
152
-
153
- public fun borrow_version(self: &Dapp): &StorageValue<u32> {
154
- storage_migration::borrow_field(&self.id, b"version")
155
- }
156
-
157
- public(package) fun borrow_mut_version(self: &mut Dapp): &mut StorageValue<u32> {
158
- storage_migration::borrow_mut_field(&mut self.id, b"version")
159
- }
160
-
161
- public fun borrow_metadata(self: &Dapp): &StorageValue<DappMetadata> {
162
- storage_migration::borrow_field(&self.id, b"metadata")
163
- }
164
-
165
- public(package) fun borrow_mut_metadata(self: &mut Dapp): &mut StorageValue<DappMetadata> {
166
- storage_migration::borrow_mut_field(&mut self.id, b"metadata")
167
- }
168
-
169
- public fun borrow_schemas(self: &Dapp): &StorageValue<vector<address>> {
170
- storage_migration::borrow_field(&self.id, b"schemas")
171
- }
172
-
173
- public(package) fun borrow_mut_schemas(self: &mut Dapp): &mut StorageValue<vector<address>> {
174
- storage_migration::borrow_mut_field(&mut self.id, b"schemas")
175
- }
176
-
177
- public fun borrow_safe_mode(self: &Dapp): &StorageValue<bool> {
178
- storage_migration::borrow_field(&self.id, b"safe_mode")
179
- }
180
-
181
- public(package) fun borrow_mut_safe_mode(self: &mut Dapp): &mut StorageValue<bool> {
182
- storage_migration::borrow_mut_field(&mut self.id, b"safe_mode")
183
- }
184
-
185
- public(package) fun create(ctx: &mut TxContext): Dapp {
186
- let mut id = object::new(ctx);
187
- storage_migration::add_field<StorageValue<address>>(&mut id, b"admin", storage_value::new());
188
- storage_migration::add_field<StorageValue<address>>(&mut id, b"package_id", storage_value::new());
189
- storage_migration::add_field<StorageValue<u32>>(&mut id, b"version", storage_value::new());
190
- storage_migration::add_field<StorageValue<DappMetadata>>(&mut id, b"metadata", storage_value::new());
191
- storage_migration::add_field<StorageValue<vector<address>>>(&mut id, b"schemas", storage_value::new());
192
- storage_migration::add_field<StorageValue<bool>>(&mut id, b"safe_mode", storage_value::new());
193
- Dapp { id }
194
- }
195
-
196
- public(package) fun upgrade<DappKey: drop>(dapp: &mut Dapp, ctx: &TxContext) {
197
- assert!(dapp.borrow_metadata().contains(), 0);
198
- assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
199
-
200
- let new_package_id = type_info::current_package_id<DappKey>();
201
- dapp.borrow_mut_package_id().set(new_package_id);
202
- dapp.borrow_mut_version().mutate!(|version| {
203
- *version = *version + 1;
204
- });
205
- }
206
-
207
- public(package) fun add_schema<Schema: key + store>(dapp: &mut Dapp, schema: Schema, ctx: &TxContext) {
208
- assert!(dapp.borrow_metadata().contains(), 0);
209
- assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
210
-
211
- let schema_id = object::id_address(&schema);
212
- dapp.borrow_mut_schemas().borrow_mut().push_back(schema_id);
213
- public_share_object(schema);
214
- }
215
-
216
- #[test_only]
217
- public fun create_dapp_for_testing(ctx: &mut TxContext): Dapp {
218
- create(ctx)
219
- }
220
-
221
- #[test_only]
222
- public fun distroy_dapp_for_testing(dapp: Dapp) {
223
- let Dapp { id } = dapp;
224
- id.delete();
225
- }
128
+ use ${config.name}::dapp_metadata::DappMetadata;
129
+ use dubhe::storage_value;
130
+ use dubhe::storage_value::StorageValue;
131
+ use dubhe::storage;
132
+ use sui::transfer::public_share_object;
133
+ use dubhe::type_info;
134
+
135
+ public struct Dapp has key, store {
136
+ id: UID,
137
+ }
138
+
139
+ public fun borrow_admin(self: &Dapp): &StorageValue<address> {
140
+ storage::borrow_field(&self.id, b"admin")
141
+ }
142
+
143
+ public(package) fun admin(self: &mut Dapp): &mut StorageValue<address> {
144
+ storage::borrow_mut_field(&mut self.id, b"admin")
145
+ }
146
+
147
+ public fun borrow_package_id(self: &Dapp): &StorageValue<address> {
148
+ storage::borrow_field(&self.id, b"package_id")
149
+ }
150
+
151
+ public(package) fun package_id(self: &mut Dapp): &mut StorageValue<address> {
152
+ storage::borrow_mut_field(&mut self.id, b"package_id")
153
+ }
154
+
155
+ public fun borrow_version(self: &Dapp): &StorageValue<u32> {
156
+ storage::borrow_field(&self.id, b"version")
157
+ }
158
+
159
+ public(package) fun version(self: &mut Dapp): &mut StorageValue<u32> {
160
+ storage::borrow_mut_field(&mut self.id, b"version")
161
+ }
162
+
163
+ public fun borrow_metadata(self: &Dapp): &StorageValue<DappMetadata> {
164
+ storage::borrow_field(&self.id, b"metadata")
165
+ }
166
+
167
+ public(package) fun metadata(self: &mut Dapp): &mut StorageValue<DappMetadata> {
168
+ storage::borrow_mut_field(&mut self.id, b"metadata")
169
+ }
170
+
171
+ public fun borrow_safe_mode(self: &Dapp): &StorageValue<bool> {
172
+ storage::borrow_field(&self.id, b"safe_mode")
173
+ }
174
+
175
+ public(package) fun safe_mode(self: &mut Dapp): &mut StorageValue<bool> {
176
+ storage::borrow_mut_field(&mut self.id, b"safe_mode")
177
+ }
178
+
179
+ public(package) fun borrow_schemas(self: &Dapp): &StorageValue<vector<address>> {
180
+ storage::borrow_field(&self.id, b"schemas")
181
+ }
182
+
183
+ public(package) fun schemas(self: &mut Dapp): &mut StorageValue<vector<address>> {
184
+ storage::borrow_mut_field(&mut self.id, b"schemas")
185
+ }
186
+
187
+
188
+ public(package) fun create(ctx: &mut TxContext): Dapp {
189
+ let mut id = object::new(ctx);
190
+ storage::add_field<StorageValue<address>>(&mut id, b"admin", storage_value::new(b"admin", ctx));
191
+ storage::add_field<StorageValue<address>>(&mut id, b"package_id", storage_value::new(b"package_id", ctx));
192
+ storage::add_field<StorageValue<u32>>(&mut id, b"version", storage_value::new(b"version", ctx));
193
+ storage::add_field<StorageValue<DappMetadata>>(&mut id, b"metadata", storage_value::new(b"metadata", ctx));
194
+ storage::add_field<StorageValue<bool>>(&mut id, b"safe_mode", storage_value::new(b"safe_mode", ctx));
195
+ storage::add_field<StorageValue<vector<address>>>(&mut id, b"schemas", storage_value::new(b"schemas", ctx));
196
+ Dapp { id }
197
+ }
198
+
199
+ public(package) fun upgrade<DappKey: drop>(dapp: &mut Dapp, ctx: &TxContext) {
200
+ assert!(dapp.borrow_metadata().contains(), 0);
201
+ assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
202
+ let new_package_id = type_info::current_package_id<DappKey>();
203
+ dapp.package_id().set(new_package_id);
204
+ let current_version = dapp.version()[];
205
+ dapp.version().set(current_version + 1);
206
+ }
207
+
208
+ public(package) fun add_schema<Schema: key + store>(dapp: &mut Dapp, schema: Schema) {
209
+ let mut schemas = dapp.schemas()[];
210
+ schemas.push_back(object::id_address<Schema>(&schema));
211
+ dapp.schemas().set(schemas);
212
+ public_share_object(schema);
213
+ }
214
+
215
+ #[test_only]
216
+
217
+ public fun create_dapp_for_testing(ctx: &mut TxContext): Dapp {
218
+ create(ctx)
219
+ }
220
+
221
+ #[test_only]
222
+
223
+ public fun distroy_dapp_for_testing(dapp: Dapp) {
224
+ let Dapp { id } = dapp;
225
+ id.delete();
226
+ }
226
227
  }
227
228
  `;
228
229
  await formatAndWriteMove(
@@ -234,27 +235,26 @@ async function generateDappSchema(config: DubheConfig, srcPrefix: string) {
234
235
  }
235
236
 
236
237
  async function generateDappSystem(config: DubheConfig, srcPrefix: string) {
237
- const path = `${srcPrefix}/contracts/${config.name}/sources/codegen/schemas/default/dapp/system.move`
238
+ const path = `${srcPrefix}/contracts/${config.name}/sources/codegen/dapp/system.move`
238
239
  if (!existsSync(path)) {
239
240
  let code = `module ${config.name}::dapp_system {
240
- use std::ascii::String;
241
- use std::ascii;
242
- use dubhe::type_info;
243
- use sui::clock::Clock;
244
- use ${config.name}::dapp_schema;
245
- use ${config.name}::dapp_metadata;
246
- use ${config.name}::dapp_schema::Dapp;
247
-
248
- public struct DappKey has drop {}
249
-
250
- public(package) fun new(): DappKey {
251
- DappKey { }
252
- }
253
-
254
- public(package) fun create(name: String, description: String, clock: &Clock, ctx: &mut TxContext): Dapp {
255
- let mut dapp = dapp_schema::create(ctx);
256
- assert!(!dapp.borrow_metadata().contains(), 0);
257
- dapp.borrow_mut_metadata().set(
241
+ use std::ascii::String;
242
+ use std::ascii;
243
+ use dubhe::type_info;
244
+ use sui::clock::Clock;
245
+ use ${config.name}::dapp_schema;
246
+ use ${config.name}::dapp_metadata;
247
+ use ${config.name}::dapp_schema::Dapp;
248
+
249
+ public struct DappKey has drop {}
250
+ public(package) fun new(): DappKey {
251
+ DappKey { }
252
+ }
253
+
254
+ public(package) fun create(name: String, description: String, clock: &Clock, ctx: &mut TxContext): Dapp {
255
+ let mut dapp = dapp_schema::create(ctx);
256
+ assert!(!dapp.borrow_metadata().contains(), 0);
257
+ dapp.metadata().set(
258
258
  dapp_metadata::new(
259
259
  name,
260
260
  description,
@@ -264,20 +264,28 @@ async function generateDappSystem(config: DubheConfig, srcPrefix: string) {
264
264
  vector[]
265
265
  )
266
266
  );
267
- let package_id = type_info::current_package_id<DappKey>();
268
- dapp.borrow_mut_package_id().set(package_id);
269
- dapp.borrow_mut_admin().set(ctx.sender());
270
- dapp.borrow_mut_version().set(1);
271
- dapp.borrow_mut_safe_mode().set(false);
272
- dapp.borrow_mut_schemas().set(vector[]);
273
- dapp
274
- }
275
-
276
- public entry fun set_metadata(dapp: &mut Dapp, name: String, description: String, icon_url: String, website_url: String, partners: vector<String>, ctx: &TxContext) {
277
- assert!(dapp.borrow_admin().contains(), 0);
278
- assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
279
- let created_at = dapp.borrow_mut_metadata().take().get_created_at();
280
- dapp.borrow_mut_metadata().set(
267
+ let package_id = type_info::current_package_id<DappKey>();
268
+ dapp.package_id().set(package_id);
269
+ dapp.admin().set(ctx.sender());
270
+ dapp.version().set(1);
271
+ dapp.safe_mode().set(false);
272
+ dapp.schemas().set(vector[]);
273
+ dapp
274
+ }
275
+
276
+ public entry fun set_metadata(
277
+ dapp: &mut Dapp,
278
+ name: String,
279
+ description: String,
280
+ icon_url: String,
281
+ website_url: String,
282
+ partners: vector<String>,
283
+ ctx: &TxContext,
284
+ ) {
285
+ let admin = dapp.admin().try_get();
286
+ assert!(admin == option::some(ctx.sender()), 0);
287
+ let created_at = dapp.metadata().remove().get_created_at();
288
+ dapp.metadata().set(
281
289
  dapp_metadata::new(
282
290
  name,
283
291
  description,
@@ -287,36 +295,35 @@ async function generateDappSystem(config: DubheConfig, srcPrefix: string) {
287
295
  partners
288
296
  )
289
297
  );
290
- }
291
-
292
- public entry fun transfer_ownership(dapp: &mut Dapp, new_admin: address, ctx: &mut TxContext) {
293
- assert!(dapp.borrow_admin().contains(), 0);
294
- assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
295
-
296
- dapp.borrow_mut_admin().set(new_admin);
297
- }
298
-
299
- public entry fun set_safe_mode(dapp: &mut Dapp, safe_mode: bool, ctx: &TxContext) {
300
- assert!(dapp.borrow_admin().contains(), 0);
301
- assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
302
-
303
- dapp.borrow_mut_safe_mode().set(safe_mode);
304
- }
305
-
306
- public fun ensure_no_safe_mode(dapp: &Dapp) {
307
- assert!(!dapp.borrow_safe_mode().get(), 0);
308
- }
309
-
310
- public fun ensure_has_authority(dapp: &Dapp, ctx: &TxContext) {
311
- assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
312
- }
313
-
314
- public fun ensure_has_schema<Schema: key + store>(dapp: &Dapp, schema: &Schema) {
315
- let schema_id = object::id_address(schema);
316
- assert!(dapp.borrow_schemas().get().contains(&schema_id), 0);
317
- }
298
+ }
299
+
300
+ public entry fun transfer_ownership(dapp: &mut Dapp, new_admin: address, ctx: &mut TxContext) {
301
+ let admin = dapp.admin().try_get();
302
+ assert!(admin == option::some(ctx.sender()), 0);
303
+ dapp.admin().set(new_admin);
304
+ }
305
+
306
+ public entry fun set_safe_mode(dapp: &mut Dapp, safe_mode: bool, ctx: &TxContext) {
307
+ let admin = dapp.admin().try_get();
308
+ assert!(admin == option::some(ctx.sender()), 0);
309
+ dapp.safe_mode().set(safe_mode);
310
+ }
311
+
312
+ public fun ensure_no_safe_mode(dapp: &Dapp) {
313
+ assert!(!dapp.borrow_safe_mode()[], 0);
314
+ }
315
+
316
+ public fun ensure_has_authority(dapp: &Dapp, ctx: &TxContext) {
317
+ assert!(dapp.borrow_admin().get() == ctx.sender(), 0);
318
+ }
319
+
320
+ public fun ensure_has_schema<Schema: key + store>(dapp: &Dapp, schema: &Schema) {
321
+ let schema_id = object::id_address(schema);
322
+ assert!(dapp.borrow_schemas().get().contains(&schema_id), 0);
323
+ }
318
324
  }
319
325
 
326
+
320
327
  `;
321
328
  await formatAndWriteMove(
322
329
  code,
@@ -20,27 +20,23 @@ export async function generateSchemaError(
20
20
  path: string
21
21
  ) {
22
22
  console.log('\nšŸ“¦ Starting Schema Error Generation...');
23
- for (const key of Object.keys(errors)) {
24
- const name = key
25
- const message = errors[key]
26
- console.log(
27
- ` └─ Generating ${name} message: ${message}`);
28
- let code = `module ${projectName}::${convertToSnakeCase(name)}_error {
29
- #[error]
30
- const ${name}: vector<u8> = b"${message}";
31
- /// Get the error message.
32
- public fun message(): vector<u8> { ${name} }
33
- /// Abort execution with the given error code.
34
- public fun emit() { abort ${name} }
35
- /// Require that the given condition is true, otherwise abort with the given error code.
36
- public fun require(condition: bool) { if (!condition) { emit() } }`
37
- await formatAndWriteMove(
38
- code,
39
- `${path}/contracts/${projectName}/sources/codegen/errors/${convertToSnakeCase(
40
- name
41
- )}_error.move`,
42
- 'formatAndWriteMove'
43
- );
44
- }
23
+
24
+ let code = `module ${projectName}::errors {
25
+ ${Object.entries(errors).map(([name, message]) => {
26
+ console.log(` ā”œā”€ Generating Error: ${name}`);
27
+ console.log(` │ └─ Message: ${message}`);
28
+ return `#[error]
29
+ const ${name}: vector<u8> = b"${message}";
30
+ public fun ${convertToSnakeCase(name)}_error(condition: bool) { assert!(condition, ${name}) }
31
+ `
32
+ }).join('\n')}
33
+ }`
34
+
35
+
36
+ await formatAndWriteMove(
37
+ code,
38
+ `${path}/contracts/${projectName}/sources/codegen/errors.move`,
39
+ 'formatAndWriteMove'
40
+ );
45
41
  console.log('āœ… Schema Error Generation Complete\n');
46
42
  }
@@ -71,19 +71,40 @@ export async function generateSchemaEvent(
71
71
  ${getStructAttrs(fields as Record<string, string>)}
72
72
  }
73
73
  }
74
-
75
- public fun emit(${getStructAttrsWithType(fields as Record<string, string>)}) {
76
- event::emit(${name}Event {
77
- ${getStructAttrs(fields as Record<string, string>)}
78
- });
79
74
  }`;
80
75
  await formatAndWriteMove(
81
76
  code,
82
- `${path}/contracts/${projectName}/sources/codegen/events/${convertToSnakeCase(
77
+ `${path}/contracts/${projectName}/sources/codegen/data/${convertToSnakeCase(
83
78
  name
84
79
  )}_event.move`,
85
80
  'formatAndWriteMove'
86
81
  );
87
82
  }
83
+
84
+ let code = `module ${projectName}::events {
85
+ use std::ascii::{String, string};
86
+ ${generateImport(projectName, data)}
87
+ ${Object.entries(events).map(([name, fields]) => {
88
+ return `
89
+ use ${projectName}::${convertToSnakeCase(name)}_event::${name}Event;
90
+ use ${projectName}::${convertToSnakeCase(name)}_event;
91
+ public fun ${convertToSnakeCase(name)}_event(${getStructAttrsWithType(fields as Record<string, string>)}) {
92
+ dubhe::storage_event::emit_set_record<${name}Event, ${name}Event, ${name}Event>(
93
+ string(b"${convertToSnakeCase(name)}_event"),
94
+ option::none(),
95
+ option::none(),
96
+ option::some(${convertToSnakeCase(name)}_event::new(${getStructAttrs(fields as Record<string, string>)}))
97
+ )
98
+ }
99
+ `
100
+ }).join('\n')}
101
+ }`
102
+
103
+
104
+ await formatAndWriteMove(
105
+ code,
106
+ `${path}/contracts/${projectName}/sources/codegen/events.move`,
107
+ 'formatAndWriteMove'
108
+ );
88
109
  console.log('āœ… Schema Event Generation Complete\n');
89
110
  }