@0xobelisk/sui-common 1.0.2 → 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,
@@ -29,7 +29,7 @@ function generateImport(
29
29
  projectName: string,
30
30
  data: Record<string, SchemaData> | null,
31
31
  ) {
32
- if (data) {
32
+ if (data != null) {
33
33
  const names = Object.keys(data);
34
34
  return names
35
35
  .map(name => {
@@ -38,8 +38,9 @@ function generateImport(
38
38
  )}::${name};`;
39
39
  })
40
40
  .join('\n');
41
+ } else {
42
+ return '';
41
43
  }
42
-
43
44
  }
44
45
 
45
46
  export async function generateSchemaEvent(
@@ -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,
@@ -164,7 +180,7 @@ function generateImport(
164
180
  projectName: string,
165
181
  data: Record<string, SchemaData> | null,
166
182
  ) {
167
- if (data) {
183
+ if (data != null) {
168
184
  const names = Object.keys(data);
169
185
  return names
170
186
  .map(name => {
@@ -173,8 +189,9 @@ function generateImport(
173
189
  )}::${name};`;
174
190
  })
175
191
  .join('\n');
192
+ } else {
193
+ return '';
176
194
  }
177
-
178
195
  }
179
196
 
180
197
  export async function generateSchemaStructure(
@@ -184,138 +201,99 @@ export async function generateSchemaStructure(
184
201
  path: string,
185
202
  ) {
186
203
  console.log('\n🔨 Starting Schema Structure Generation...');
187
- for (const schemaName in schemas) {
188
- console.log(` ├─ Generating schema: ${schemaName}`);
189
204
  console.log(
190
- ` ├─ Output path: ${path}/contracts/${projectName}/sources/codegen/schemas/${schemaName}.move`,
205
+ ` ├─ Output path: ${path}/contracts/${projectName}/sources/codegen/schema.move`,
191
206
  );
192
207
  console.log(
193
208
  ` └─ Structure fields: ${
194
- Object.values(schemas[schemaName]).length
209
+ Object.values(schemas).length
195
210
  }`,
196
211
  );
197
- const schema = schemas[schemaName];
198
- const schemaMoudle = `module ${projectName}::${schemaName}_schema {
212
+ const schemaMoudle = `module ${projectName}::schema {
199
213
  use std::ascii::String;
200
214
  use std::ascii::string;
201
215
  use sui::package::UpgradeCap;
202
216
  use std::type_name;
203
- use dubhe::storage_migration;
217
+ use dubhe::storage;
204
218
  use dubhe::storage_value::{Self, StorageValue};
205
219
  use dubhe::storage_map::{Self, StorageMap};
206
220
  use dubhe::storage_double_map::{Self, StorageDoubleMap};
207
221
  use sui::dynamic_field as df;
208
- use sui::sui::SUI;
209
- use sui::coin::Coin;
210
- use sui::balance::Balance;
222
+
211
223
  ${generateImport(projectName, data)}
212
224
 
213
- public struct ${capitalizeAndRemoveUnderscores(
214
- schemaName,
215
- )} has key, store {
216
- id: UID
217
- }
225
+ public struct Schema has key, store { id: UID }
218
226
 
219
- ${Object.entries(schema)
227
+ ${Object.entries(schemas)
220
228
  .map(([key, value]) => {
221
- return `public fun borrow_${key}(self: &${capitalizeAndRemoveUnderscores(
222
- schemaName,
223
- )}) : &${value} {
224
- 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}")
225
231
  }
226
232
 
227
- public(package) fun ${key}(self: &mut ${capitalizeAndRemoveUnderscores(
228
- schemaName,
229
- )}): &mut ${value} {
230
- 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}")
231
235
  }
232
236
  `;
233
237
  })
234
238
  .join('')}
235
239
 
236
240
 
237
- public(package) fun create(ctx: &mut TxContext): ${capitalizeAndRemoveUnderscores(
238
- schemaName,
239
- )} {
241
+ public(package) fun create(ctx: &mut TxContext): Schema {
240
242
  let mut id = object::new(ctx);
241
- ${Object.entries(schema)
243
+ ${Object.entries(schemas)
242
244
  .map(([key, value]) => {
243
245
  let storage_type = '';
244
246
  if (value.includes('StorageValue')) {
245
- storage_type = `storage_value::new()`;
247
+ storage_type = `storage_value::new(b"${key}", ctx)`;
246
248
  } else if (value.includes('StorageMap')) {
247
- storage_type = `storage_map::new()`;
249
+ storage_type = `storage_map::new(b"${key}", ctx)`;
248
250
  } else if (
249
251
  value.includes('StorageDoubleMap')
250
252
  ) {
251
- storage_type = `storage_double_map::new()`;
253
+ storage_type = `storage_double_map::new(b"${key}", ctx)`;
252
254
  }
253
- return `storage_migration::add_field<${value}>(&mut id, b"${key}", ${storage_type});`;
255
+ return `storage::add_field<${value}>(&mut id, b"${key}", ${storage_type});`;
254
256
  })
255
- .join('')}
257
+ .join('\n')}
256
258
 
257
- ${capitalizeAndRemoveUnderscores(schemaName)} { id }
259
+ Schema { id }
258
260
  }
259
261
 
260
- public fun migrate(_${schemaName}: &mut ${capitalizeAndRemoveUnderscores(schemaName)}, _cap: &UpgradeCap) { }
262
+ public fun migrate(_schema: &mut Schema, _cap: &UpgradeCap, _ctx: &mut TxContext) { }
261
263
 
262
-
263
264
 
264
265
  // ======================================== View Functions ========================================
265
- ${Object.entries(schema)
266
+ ${Object.entries(schemas)
266
267
  .map(([key, value]) => {
267
268
  // @ts-ignore
268
269
  let all_types = value.match(/<(.+)>/)[1].split(',').map(type => type.trim());
269
270
  let para_key: string[] = [];
270
271
  let para_value = '';
271
272
  let borrow_key = '';
272
- let extra_code = '';
273
273
  if (value.includes('StorageValue')) {
274
274
  para_key = [];
275
275
  para_value = `${all_types[0]}`;
276
- borrow_key = 'borrow()';
276
+ borrow_key = 'get()';
277
277
  } else if (value.includes('StorageMap')) {
278
278
  para_key = [`key: ${all_types[0]}`];
279
279
  para_value = `${all_types[1]}`;
280
- borrow_key = 'borrow(key)';
281
- if (!value.includes('Balance') && !value.includes('Coin')) {
282
- extra_code = `public fun get_${key}_keys(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[0]}> {
283
- self.borrow_${key}().keys()
284
- }
285
-
286
- public fun get_${key}_values(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[1]}> {
287
- self.borrow_${key}().values()
288
- }`;
289
- }
280
+ borrow_key = 'get(key)';
290
281
  } else if (value.includes('StorageDoubleMap')) {
291
282
  para_key = [`key1: ${all_types[0]}`, `key2: ${all_types[1]}`];
292
283
  para_value = `${all_types[2]}`;
293
- borrow_key = 'borrow(key1, key2)';
294
- if (!value.includes('Balance') && !value.includes('Coin')) {
295
- extra_code = `public fun get_${key}_keys(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : (vector<${all_types[0]}>, vector<${all_types[1]}>) {
296
- self.borrow_${key}().keys()
297
- }
298
-
299
- public fun get_${key}_values(self: &${capitalizeAndRemoveUnderscores(schemaName)}) : vector<${all_types[2]}> {
300
- self.borrow_${key}().values()
301
- }`;
302
- }
284
+ borrow_key = 'get(key1, key2)';
303
285
  }
304
- return `public fun get_${key}(self: &${capitalizeAndRemoveUnderscores(schemaName)}, ${para_key}) : &${para_value} {
286
+ return `public fun get_${key}(self: &Schema, ${para_key}) : &${para_value} {
305
287
  self.borrow_${key}().${borrow_key}
306
- }
307
- ` + extra_code;
308
- })
309
- .join('')}
288
+ }`
289
+ }).join('\n')}
310
290
  // =========================================================================================================
311
-
312
-
313
- }`;
291
+ }`
314
292
  await formatAndWriteMove(
315
293
  schemaMoudle,
316
- `${path}/contracts/${projectName}/sources/codegen/schemas/${schemaName}.move`,
294
+ `${path}/contracts/${projectName}/sources/codegen/schema.move`,
317
295
  'formatAndWriteMove',
318
296
  );
319
- }
320
- console.log('✅ Schema Structure Generation Complete\n');
297
+ console.log('✅ Schema Structure Generation Complete\n');
321
298
  }
299
+