@0xobelisk/sui-common 1.0.3 → 1.0.4

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,
@@ -7,6 +7,21 @@ import {
7
7
  getStructAttrsQuery,
8
8
  } from './common';
9
9
 
10
+ function sortByFirstLetter(arr: string[]): string[] {
11
+ return arr.sort((a, b) => {
12
+ const firstLetterA = a.charAt(0).toLowerCase();
13
+ const firstLetterB = b.charAt(0).toLowerCase();
14
+
15
+ if (firstLetterA < firstLetterB) {
16
+ return -1;
17
+ }
18
+ if (firstLetterA > firstLetterB) {
19
+ return 1;
20
+ }
21
+ return 0;
22
+ });
23
+ }
24
+
10
25
  export function capitalizeAndRemoveUnderscores(input: string): string {
11
26
  return input
12
27
  .split('_')
@@ -100,14 +115,15 @@ export async function generateSchemaData(
100
115
  console.log(enumNames)
101
116
 
102
117
  if (Array.isArray(fields)) {
118
+ const sortByFirstLetterFields = sortByFirstLetter(fields);
103
119
  code = `module ${projectName}::${convertToSnakeCase(
104
120
  name,
105
121
  )} {
106
122
  public enum ${name} has copy, drop , store {
107
- ${fields}
123
+ ${sortByFirstLetterFields}
108
124
  }
109
125
 
110
- ${fields
126
+ ${sortByFirstLetterFields
111
127
  .map((field: string) => {
112
128
  return `public fun new_${convertToSnakeCase(
113
129
  field,
@@ -185,138 +201,99 @@ export async function generateSchemaStructure(
185
201
  path: string,
186
202
  ) {
187
203
  console.log('\n🔨 Starting Schema Structure Generation...');
188
- for (const schemaName in schemas) {
189
- console.log(` ├─ Generating schema: ${schemaName}`);
190
204
  console.log(
191
- ` ├─ Output path: ${path}/contracts/${projectName}/sources/codegen/schemas/${schemaName}.move`,
205
+ ` ├─ Output path: ${path}/contracts/${projectName}/sources/codegen/schema.move`,
192
206
  );
193
207
  console.log(
194
208
  ` └─ Structure fields: ${
195
- Object.values(schemas[schemaName]).length
209
+ Object.values(schemas).length
196
210
  }`,
197
211
  );
198
- const schema = schemas[schemaName];
199
- const schemaMoudle = `module ${projectName}::${schemaName}_schema {
212
+ const schemaMoudle = `module ${projectName}::schema {
200
213
  use std::ascii::String;
201
214
  use std::ascii::string;
202
215
  use sui::package::UpgradeCap;
203
216
  use std::type_name;
204
- use dubhe::storage_migration;
217
+ use dubhe::storage;
205
218
  use dubhe::storage_value::{Self, StorageValue};
206
219
  use dubhe::storage_map::{Self, StorageMap};
207
220
  use dubhe::storage_double_map::{Self, StorageDoubleMap};
208
221
  use sui::dynamic_field as df;
209
- use sui::sui::SUI;
210
- use sui::coin::Coin;
211
- use sui::balance::Balance;
222
+
212
223
  ${generateImport(projectName, data)}
213
224
 
214
- public struct ${capitalizeAndRemoveUnderscores(
215
- schemaName,
216
- )} has key, store {
217
- id: UID
218
- }
225
+ public struct Schema has key, store { id: UID }
219
226
 
220
- ${Object.entries(schema)
227
+ ${Object.entries(schemas)
221
228
  .map(([key, value]) => {
222
- return `public fun borrow_${key}(self: &${capitalizeAndRemoveUnderscores(
223
- schemaName,
224
- )}) : &${value} {
225
- storage_migration::borrow_field(&self.id, b"${key}")
229
+ return `public fun borrow_${key}(self: &Schema) : &${value} {
230
+ storage::borrow_field(&self.id, b"${key}")
226
231
  }
227
232
 
228
- public(package) fun ${key}(self: &mut ${capitalizeAndRemoveUnderscores(
229
- schemaName,
230
- )}): &mut ${value} {
231
- storage_migration::borrow_mut_field(&mut self.id, b"${key}")
233
+ public(package) fun ${key}(self: &mut Schema): &mut ${value} {
234
+ storage::borrow_mut_field(&mut self.id, b"${key}")
232
235
  }
233
236
  `;
234
237
  })
235
238
  .join('')}
236
239
 
237
240
 
238
- public(package) fun create(ctx: &mut TxContext): ${capitalizeAndRemoveUnderscores(
239
- schemaName,
240
- )} {
241
+ public(package) fun create(ctx: &mut TxContext): Schema {
241
242
  let mut id = object::new(ctx);
242
- ${Object.entries(schema)
243
+ ${Object.entries(schemas)
243
244
  .map(([key, value]) => {
244
245
  let storage_type = '';
245
246
  if (value.includes('StorageValue')) {
246
- storage_type = `storage_value::new()`;
247
+ storage_type = `storage_value::new(b"${key}", ctx)`;
247
248
  } else if (value.includes('StorageMap')) {
248
- storage_type = `storage_map::new()`;
249
+ storage_type = `storage_map::new(b"${key}", ctx)`;
249
250
  } else if (
250
251
  value.includes('StorageDoubleMap')
251
252
  ) {
252
- storage_type = `storage_double_map::new()`;
253
+ storage_type = `storage_double_map::new(b"${key}", ctx)`;
253
254
  }
254
- return `storage_migration::add_field<${value}>(&mut id, b"${key}", ${storage_type});`;
255
+ return `storage::add_field<${value}>(&mut id, b"${key}", ${storage_type});`;
255
256
  })
256
- .join('')}
257
+ .join('\n')}
257
258
 
258
- ${capitalizeAndRemoveUnderscores(schemaName)} { id }
259
+ Schema { id }
259
260
  }
260
261
 
261
- public fun migrate(_${schemaName}: &mut ${capitalizeAndRemoveUnderscores(schemaName)}, _cap: &UpgradeCap) { }
262
+ public fun migrate(_schema: &mut Schema, _cap: &UpgradeCap, _ctx: &mut TxContext) { }
262
263
 
263
-
264
264
 
265
265
  // ======================================== View Functions ========================================
266
- ${Object.entries(schema)
266
+ ${Object.entries(schemas)
267
267
  .map(([key, value]) => {
268
268
  // @ts-ignore
269
269
  let all_types = value.match(/<(.+)>/)[1].split(',').map(type => type.trim());
270
270
  let para_key: string[] = [];
271
271
  let para_value = '';
272
272
  let borrow_key = '';
273
- let extra_code = '';
274
273
  if (value.includes('StorageValue')) {
275
274
  para_key = [];
276
275
  para_value = `${all_types[0]}`;
277
- borrow_key = 'borrow()';
276
+ borrow_key = 'get()';
278
277
  } else if (value.includes('StorageMap')) {
279
278
  para_key = [`key: ${all_types[0]}`];
280
279
  para_value = `${all_types[1]}`;
281
- borrow_key = 'borrow(key)';
282
- if (!value.includes('Balance') && !value.includes('Coin')) {
283
- extra_code = `public fun get_${key}_keys(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[0]}> {
284
- self.borrow_${key}().keys()
285
- }
286
-
287
- public fun get_${key}_values(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[1]}> {
288
- self.borrow_${key}().values()
289
- }`;
290
- }
280
+ borrow_key = 'get(key)';
291
281
  } else if (value.includes('StorageDoubleMap')) {
292
282
  para_key = [`key1: ${all_types[0]}`, `key2: ${all_types[1]}`];
293
283
  para_value = `${all_types[2]}`;
294
- borrow_key = 'borrow(key1, key2)';
295
- if (!value.includes('Balance') && !value.includes('Coin')) {
296
- extra_code = `public fun get_${key}_keys(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : (vector<${all_types[0]}>, vector<${all_types[1]}>) {
297
- self.borrow_${key}().keys()
298
- }
299
-
300
- public fun get_${key}_values(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[2]}> {
301
- self.borrow_${key}().values()
302
- }`;
303
- }
284
+ borrow_key = 'get(key1, key2)';
304
285
  }
305
- return `public fun get_${key}(self: &${capitalizeAndRemoveUnderscores(schemaName)}, ${para_key}) : &${para_value} {
286
+ return `public fun get_${key}(self: &Schema, ${para_key}) : &${para_value} {
306
287
  self.borrow_${key}().${borrow_key}
307
- }
308
- ` + extra_code;
309
- })
310
- .join('')}
288
+ }`
289
+ }).join('\n')}
311
290
  // =========================================================================================================
312
-
313
-
314
- }`;
291
+ }`
315
292
  await formatAndWriteMove(
316
293
  schemaMoudle,
317
- `${path}/contracts/${projectName}/sources/codegen/schemas/${schemaName}.move`,
294
+ `${path}/contracts/${projectName}/sources/codegen/schema.move`,
318
295
  'formatAndWriteMove',
319
296
  );
320
- }
321
- console.log('✅ Schema Structure Generation Complete\n');
297
+ console.log('✅ Schema Structure Generation Complete\n');
322
298
  }
299
+
@@ -35,24 +35,17 @@ export async function generateDeployHook(
35
35
  use std::ascii::string;
36
36
  use sui::clock::Clock;
37
37
  use ${config.name}::dapp_system;
38
- ${importSchemas}
39
38
  public entry fun run(clock: &Clock, ctx: &mut TxContext) {
40
39
  // Create a dapp.
41
40
  let mut dapp = dapp_system::create(string(b"${config.name}"),string(b"${config.description}"), clock , ctx);
42
41
 
43
42
  // Create schemas
44
- ${Object.keys(config.schemas).map(schemaName => {
45
- return `let mut ${schemaName} = ${config.name}::${schemaName}_schema::create(ctx);`;
46
- }).join('\n')}
43
+ let mut schema = ${config.name}::schema::create(ctx);
47
44
  // Logic that needs to be automated once the contract is deployed
48
45
  {
49
46
  };
50
47
  // Authorize schemas and public share objects
51
- ${Object.keys(config.schemas).map(schemaName => {
52
- return `
53
- dapp.add_schema<${capitalizeAndRemoveUnderscores(schemaName)}>(${schemaName}, ctx);
54
- `;
55
- }).join('\n')}
48
+ dapp.add_schema(schema);
56
49
  sui::transfer::public_share_object(dapp);
57
50
  }
58
51
  }` } else {
@@ -61,15 +54,12 @@ export async function generateDeployHook(
61
54
  use std::ascii::string;
62
55
  use sui::clock::Clock;
63
56
  use ${config.name}::dapp_system;
64
- ${importSchemas}
65
57
  public entry fun run(clock: &Clock, ctx: &mut TxContext) {
66
58
  // Create a dapp.
67
59
  let mut dapp = dapp_system::create(string(b"${config.name}"),string(b"${config.description}"), clock , ctx);
68
60
 
69
61
  // Create schemas
70
- ${Object.keys(config.schemas).map(schemaName => {
71
- return `let mut ${schemaName} = ${config.name}::${schemaName}_schema::create(ctx);`;
72
- }).join('\n')}
62
+ let mut schema = ${config.name}::schema::create(ctx);
73
63
  // Logic that needs to be automated once the contract is deployed
74
64
 
75
65
  {
@@ -77,11 +67,7 @@ export async function generateDeployHook(
77
67
  };
78
68
 
79
69
  // Authorize schemas and public share objects
80
- ${Object.keys(config.schemas).map(schemaName => {
81
- return `
82
- dapp.add_schema<${capitalizeAndRemoveUnderscores(schemaName)}>(${schemaName}, ctx);
83
- `;
84
- }).join('\n')}
70
+ dapp.add_schema(schema);
85
71
  sui::transfer::public_share_object(dapp);
86
72
  }
87
73
  }`