wowok 1.2.5 → 1.2.8

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.
Files changed (51) hide show
  1. package/dist/demand.d.ts +3 -2
  2. package/dist/demand.d.ts.map +1 -1
  3. package/dist/demand.js +21 -7
  4. package/dist/entity.d.ts +2 -1
  5. package/dist/entity.d.ts.map +1 -1
  6. package/dist/entity.js +23 -7
  7. package/dist/exception.d.ts +3 -1
  8. package/dist/exception.d.ts.map +1 -1
  9. package/dist/exception.js +3 -1
  10. package/dist/guard.js +1 -1
  11. package/dist/machine.js +2 -2
  12. package/dist/permission.d.ts +20 -4
  13. package/dist/permission.d.ts.map +1 -1
  14. package/dist/permission.js +146 -13
  15. package/dist/protocol.d.ts +30 -6
  16. package/dist/protocol.d.ts.map +1 -1
  17. package/dist/protocol.js +124 -17
  18. package/dist/repository.d.ts +6 -3
  19. package/dist/repository.d.ts.map +1 -1
  20. package/dist/repository.js +59 -40
  21. package/dist/resource.d.ts +20 -6
  22. package/dist/resource.d.ts.map +1 -1
  23. package/dist/resource.js +58 -21
  24. package/dist/reward.d.ts +6 -3
  25. package/dist/reward.d.ts.map +1 -1
  26. package/dist/reward.js +43 -19
  27. package/dist/service.d.ts +6 -3
  28. package/dist/service.d.ts.map +1 -1
  29. package/dist/service.js +76 -44
  30. package/dist/utils.d.ts +15 -1
  31. package/dist/utils.d.ts.map +1 -1
  32. package/dist/utils.js +150 -13
  33. package/dist/vote.d.ts +2 -2
  34. package/dist/vote.d.ts.map +1 -1
  35. package/dist/vote.js +14 -14
  36. package/package.json +1 -1
  37. package/src/demand.ts +26 -12
  38. package/src/entity.ts +33 -6
  39. package/src/exception.ts +3 -1
  40. package/src/guard.ts +2 -2
  41. package/src/machine.ts +207 -55
  42. package/src/permission.ts +168 -41
  43. package/src/progress.ts +101 -43
  44. package/src/protocol.ts +129 -20
  45. package/src/repository.ts +160 -53
  46. package/src/resource.ts +75 -24
  47. package/src/reward.ts +53 -32
  48. package/src/service.ts +109 -74
  49. package/src/utils.ts +174 -22
  50. package/src/vote.ts +30 -33
  51. package/src/wowok.ts +2 -2
package/src/protocol.ts CHANGED
@@ -79,8 +79,8 @@ export enum OperatorType {
79
79
  export enum ValueType {
80
80
  TYPE_BOOL = 100,
81
81
  TYPE_ADDRESS = 101,
82
- TYPE_U64 = 102,
83
- TYPE_U8 = 103,
82
+ TYPE_U8 = 102,
83
+ TYPE_U64 = 103,
84
84
  TYPE_VEC_U8 = 104,
85
85
  TYPE_U128 = 105,
86
86
  TYPE_VEC_ADDRESS = 106,
@@ -101,6 +101,49 @@ export enum ValueType {
101
101
  TYPE_VEC_STRING = 121,
102
102
  TYPE_U256 = 122,
103
103
  }
104
+ export enum RepositoryValueType {
105
+ Address = 200,
106
+ Address_Vec = 201,
107
+ PositiveNumber = 202,
108
+ PositiveNumber_Vec = 203,
109
+ String = 204,
110
+ String_Vec = 205,
111
+ }
112
+
113
+ export const RepositoryValueTypeInfo = [
114
+ {type: RepositoryValueType.String, name:'String', description:'String.'},
115
+ {type: RepositoryValueType.Address, name:'Address', description:'Object id or Personal address.'},
116
+ {type: RepositoryValueType.PositiveNumber, name:'Positive number or Zero', description:'Positive number or 0. including u8, u16 ,..., u256'},
117
+ {type: RepositoryValueType.String_Vec, name:'String vector', description:'Vector of string.'},
118
+ {type: RepositoryValueType.Address_Vec, name:'Address vector', description:'Vector of address.'},
119
+ {type: RepositoryValueType.PositiveNumber_Vec, name:'Positive number or Zero vector', description:'Vector of positive number or 0'},
120
+ ]
121
+
122
+ export const ValueTypeInfo = [
123
+ {type:ValueType.TYPE_BOOL, name:'bool'},
124
+ {type:ValueType.TYPE_ADDRESS, name:'address'},
125
+ {type:ValueType.TYPE_U64, name:'u64'},
126
+ {type:ValueType.TYPE_U8, name:'u8'},
127
+ {type:ValueType.TYPE_VEC_U8, name:'vec-u8'},
128
+ {type:ValueType.TYPE_U128, name:'u128'},
129
+ {type:ValueType.TYPE_VEC_ADDRESS, name:'vec-address'},
130
+ {type:ValueType.TYPE_VEC_BOOL, name:'vec-bool'},
131
+ {type:ValueType.TYPE_VEC_VEC_U8, name:'vec-vec-u8'},
132
+ {type:ValueType.TYPE_VEC_U64, name:'vec-u64'},
133
+ {type:ValueType.TYPE_VEC_U128, name:'vec-u128'},
134
+ {type:ValueType.TYPE_OPTION_ADDRESS, name:'opt-address'},
135
+ {type:ValueType.TYPE_OPTION_BOOL, name:'opt-bool'},
136
+ {type:ValueType.TYPE_OPTION_U8, name:'opt-u8'},
137
+ {type:ValueType.TYPE_OPTION_U64, name:'opt-u64'},
138
+ {type:ValueType.TYPE_OPTION_U128, name:'opt-u128'},
139
+ {type:ValueType.TYPE_OPTION_U256, name:'opt-u256'},
140
+ {type:ValueType.TYPE_OPTION_STRING, name:'opt-string'},
141
+ {type:ValueType.TYPE_OPTION_VEC_U8, name:'opt-vec-u8'},
142
+ {type:ValueType.TYPE_VEC_U256, name:'vec-u256'},
143
+ {type:ValueType.TYPE_STRING, name:'string'},
144
+ {type:ValueType.TYPE_VEC_STRING, name:'vec-string'},
145
+ {type:ValueType.TYPE_U256, name:'u256'},
146
+ ]
104
147
 
105
148
  export const OperatorTypeArray = (Object.values(OperatorType) as []).filter((v)=>typeof(v) === 'number') as number[];
106
149
  export const ValueTypeArray = (Object.values(ValueType) as []).filter((v)=>typeof(v) === 'number') as number[];
@@ -155,6 +198,23 @@ export enum ENTRYPOINT {
155
198
  localnet = 'localnet'
156
199
  }
157
200
 
201
+ const TESTNET = {
202
+ package: "0xdf85200f7a9dcac175724d10d1002c7481f718bb8f609f2ff27da47ce5d04870",
203
+ wowok_object: '0xcbbc4a23b63ab77291e885998ef1d03ac13537bb6b8ffb108913f8196cd7f62b',
204
+ entity_object: '0x362aa575fa8c1506776f13e8f5849fd932b26b080f7a6f238856f42ce6114e31',
205
+ }
206
+
207
+ const MAINNET = {
208
+ package: "",
209
+ wowok_object: '',
210
+ entity_object: '',
211
+ }
212
+
213
+ export interface CoinTypeInfo {
214
+ symbol: string;
215
+ type: string;
216
+ decimals: number;
217
+ }
158
218
  export class Protocol {
159
219
  protected network = '';
160
220
  protected package = '';
@@ -175,6 +235,9 @@ export class Protocol {
175
235
  Protocol._instance = new Protocol();
176
236
  }; return Protocol._instance
177
237
  }
238
+ static Client() : SuiClient {
239
+ return new SuiClient({ url: Protocol.Instance().NetworkUrl() });
240
+ }
178
241
 
179
242
  UseNetwork(network:ENTRYPOINT=ENTRYPOINT.testnet) {
180
243
  this.network = network;
@@ -184,12 +247,16 @@ export class Protocol {
184
247
  case ENTRYPOINT.devnet:
185
248
  break;
186
249
  case ENTRYPOINT.testnet:
187
- this.package = "0x10edb3f8e11008c8c5cfe951989d10c353d885377fe6cbdfedea3b315fa6b06c";
188
- this.wowok_object = '0xb2a940236a1811ff43eaf902e165155687f9c756dcdd1fc70096551bffcb89fc';
189
- this.entity_object= '0x8dc27895c123c9736649a800fc58e621b7747d56f0b15a560d09a4412c42ae28';
250
+ this.package = TESTNET.package;
251
+ this.wowok_object = TESTNET.wowok_object;
252
+ this.entity_object= TESTNET.entity_object;
190
253
  this.graphql = 'https://sui-testnet.mystenlabs.com/graphql';
191
254
  break;
192
255
  case ENTRYPOINT.mainnet:
256
+ this.package = MAINNET.package;
257
+ this.wowok_object = MAINNET.wowok_object;
258
+ this.entity_object= MAINNET.entity_object;
259
+ this.graphql = 'https://sui-mainnet.mystenlabs.com/graphql';
193
260
  break;
194
261
  };
195
262
  }
@@ -212,7 +279,6 @@ export class Protocol {
212
279
  };
213
280
 
214
281
  MachineFn = (fn:any) => { return `${this.package}::${MODULES.machine}::${fn}`};
215
- NodeFn = (fn: any) => { return `${this.package}::${MODULES.node}::${fn}`};
216
282
  ProgressFn = (fn:any) => { return `${this.package}::${MODULES.progress}::${fn}`};
217
283
  CommunityFn = (fn: any) => { return `${this.package}::${MODULES.community}::${fn}`};
218
284
  RepositoryFn = (fn:any) => { return `${this.package}::${MODULES.repository}::${fn}`};
@@ -269,29 +335,70 @@ export class Protocol {
269
335
  }
270
336
 
271
337
  // used in service, discount, order, because service has COIN wrapper for TOKEN
272
- static SUI_TOKEN_TYPE = '0x2::sui::SUI'; // TOKEN_TYPE
338
+ static SUI_TOKEN_TYPE = '0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI'; // TOKEN_TYPE
273
339
  // used in demand, reward, ...
274
- static SUI_COIN_TYPE = '0x2::coin::Coin<0x2::sui::SUI>'; // COIN TYPE
340
+ static SUI_COIN_TYPE = '0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x2::sui::SUI>'; // COIN TYPE
275
341
  WOWOK_TOKEN_TYPE = () => { return this.package + '::wowok::WOWOK' }
276
342
  WOWOK_COIN_TYPE = () => { return '0x2::coin::Coin<' + this.package + '::wowok::WOWOK>'}
277
343
  COINS_TYPE = () => {
278
344
  switch(this.network) {
279
345
  case ENTRYPOINT.testnet:
280
- /*return [
281
- {name:'SUI', type:'0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI', decimals:9},
282
- {name:'WOW', type:this.WOWOK_TOKEN_TYPE(), decimals:9},
283
- ];*/
346
+ return this.CoinTypes_Testnet;
284
347
  case ENTRYPOINT.mainnet:
285
- return [
286
- {name:'SUI', type:'0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI', decimals:9},
287
- {name:'WOW', type:this.WOWOK_TOKEN_TYPE(), decimals:9},
288
- {name:'USDT', type:'0xc060006111016b8a020ad5b33834984a437aaa7d3c74c18e09a95d48aceab08c::coin::COIN', decimals:6},
289
- {name:'USDC', type:'0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN', decimals:6},
290
- {name:'WETH', type:'0xaf8cd5edc19c4512f4259f0bee101a40d41ebed738ade5874359610ef8eeced5::coin::COIN', decimals:8},
291
- {name:'WBNB', type:'0xb848cce11ef3a8f62eccea6eb5b35a12c4c2b1ee1af7755d02d7bd6218e8226f::coin::COIN', decimals:8},
292
- ];
348
+ return this.CoinTypes_Mainnet;
293
349
  }; return [];
294
350
  }
351
+ Update_CoinType = (token_type: string, decimals:number, symbol: string) => {
352
+ if (!symbol || !token_type) return ;
353
+ switch(this.network) {
354
+ case ENTRYPOINT.testnet:
355
+ var r = this.CoinTypes_Testnet.filter((v) => v?.type !== token_type);
356
+ r.push({symbol:symbol, type:token_type, decimals:decimals});
357
+ this.CoinTypes_Testnet = r;
358
+ break;
359
+ case ENTRYPOINT.mainnet:
360
+ var r = this.CoinTypes_Mainnet.filter((v) => v?.type !== token_type);
361
+ r.push({symbol:symbol, type:token_type, decimals:decimals});
362
+ this.CoinTypes_Mainnet = r;
363
+ break;
364
+ };
365
+ }
366
+ ExplorerUrl = (objectid: string, type:'object' | 'txblock' | 'account'='object') => {
367
+ if (this.network === ENTRYPOINT.testnet) {
368
+ return 'https://testnet.suivision.xyz/' + type + '/' + objectid;
369
+ } else if (this.network === ENTRYPOINT.mainnet) {
370
+ return 'https://suivision.xyz/' + type + '/' + objectid;
371
+ }; return ''
372
+ }
373
+
374
+ CoinTypes_Testnet:CoinTypeInfo[] = [
375
+ {symbol:'SUI', type:'0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI', decimals:9},
376
+ {symbol:'SUI', type:'0x2::sui::SUI', decimals:9},
377
+ {symbol:'WOW', type:TESTNET.package + '::wowok::WOWOK', decimals:9},
378
+ {symbol:'USDT', type:'0xc060006111016b8a020ad5b33834984a437aaa7d3c74c18e09a95d48aceab08c::coin::COIN', decimals:6},
379
+ {symbol:'USDC', type:'0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN', decimals:6},
380
+ {symbol:'WETH', type:'0xaf8cd5edc19c4512f4259f0bee101a40d41ebed738ade5874359610ef8eeced5::coin::COIN', decimals:8},
381
+ {symbol:'WBNB', type:'0xb848cce11ef3a8f62eccea6eb5b35a12c4c2b1ee1af7755d02d7bd6218e8226f::coin::COIN', decimals:8},
382
+ ];
383
+
384
+ CoinTypes_Mainnet:CoinTypeInfo[] = [
385
+ ];
386
+
387
+ GetCoinTypeInfo = (token_type: string, handler:(info:CoinTypeInfo)=>void) : CoinTypeInfo | 'loading' => {
388
+ let r = this.COINS_TYPE().find((v) => v?.type === token_type);
389
+ if (!r) {
390
+ Protocol.Client().getCoinMetadata({coinType:token_type}).then((res) => {
391
+ if (res?.decimals && res?.symbol) {
392
+ this.Update_CoinType(token_type, res?.decimals, res?.symbol);
393
+ handler({symbol:res.symbol, decimals:res.decimals, type:token_type});
394
+ }
395
+ }).catch((e) => {
396
+ console.log(e);
397
+ })
398
+ } else {
399
+ return r;
400
+ }; return 'loading';
401
+ }
295
402
 
296
403
  static CLOCK_OBJECT = Inputs.SharedObjectRef({
297
404
  objectId:"0x6",
@@ -316,6 +423,7 @@ export class Protocol {
316
423
  WOWOK_OBJECTS_PREFIX_TYPE = () => (Object.keys(MODULES) as Array<keyof typeof MODULES>).map((key) =>
317
424
  { return this.package + '::' + key + '::'; })
318
425
  object_name_from_type_repr = (type_repr:string) : string => {
426
+ if (!type_repr) return ''
319
427
  let i = type_repr.indexOf('::');
320
428
  if (i > 0 && type_repr.slice(0, i) === this.package) {
321
429
  i = type_repr.indexOf('<');
@@ -339,6 +447,7 @@ export class RpcResultParser {
339
447
  return names;
340
448
  }
341
449
  static objectids_from_response = (protocol:Protocol, response:SuiTransactionBlockResponse, concat_result?:Map<string, TxbObject[]>): Map<string, TxbObject[]> => {
450
+ // console.log(response)
342
451
  let ret = new Map<string, string[]>();
343
452
  if (response?.objectChanges) {
344
453
  response.objectChanges.forEach((change) => {
package/src/repository.ts CHANGED
@@ -1,18 +1,27 @@
1
1
  import { BCS } from '@mysten/bcs';
2
- import { Protocol, FnCallType, ValueType, RepositoryObject, RepositoryAddress, PermissionObject, PassportObject, TxbObject} from './protocol';
2
+ import { Protocol, FnCallType, ValueType, RepositoryValueType, RepositoryAddress, PermissionObject, PassportObject, TxbObject} from './protocol';
3
3
  import { PermissionIndexType, Permission } from './permission'
4
- import { Bcs, array_unique, IsValidDesription, IsValidAddress, IsValidArray, OptionNone, } from './utils';
4
+ import { Bcs, array_unique, IsValidDesription, IsValidAddress, IsValidArray, OptionNone, IsValidName, ValueTypeConvert} from './utils';
5
5
  import { ERROR, Errors } from './exception';
6
- import { Resource } from './resource';
6
+ import { MAX_U8, MAX_U128, MAX_U256, MAX_U64 } from './utils';
7
7
 
8
8
  export enum Repository_Policy_Mode {
9
9
  POLICY_MODE_FREE = 0,
10
10
  POLICY_MODE_STRICT = 1,
11
11
  }
12
+
13
+ export interface RepData {
14
+ id: string;
15
+ name: string;
16
+ dataType: RepositoryValueType;
17
+ data: string | string[];
18
+ object: string;
19
+ }
20
+
12
21
  export type Repository_Policy = {
13
22
  key:string;
14
23
  description: string;
15
- value_type: ValueType;
24
+ data_type: RepositoryValueType;
16
25
  permission?: PermissionIndexType; // PermissionIndex like, must be geater than 10000
17
26
  }
18
27
  export type Repository_Policy_Data = {
@@ -141,12 +150,9 @@ export class Repository {
141
150
  })
142
151
  }
143
152
  add_reference(references:string[], passport?:PassportObject) {
144
- if (!references) {
145
- ERROR(Errors.InvalidParam, 'references')
146
- }
147
-
153
+ if (references.length === 0) return;
148
154
  if (!IsValidArray(references, IsValidAddress)) {
149
- ERROR(Errors.IsValidArray, 'references')
155
+ ERROR(Errors.IsValidArray, 'add_reference')
150
156
  }
151
157
 
152
158
  let txb = this.protocol.CurrentSession();
@@ -167,12 +173,10 @@ export class Repository {
167
173
  }
168
174
  }
169
175
  remove_reference(references:string[], removeall?:boolean, passport?:PassportObject) {
170
- if (!references) {
171
- ERROR(Errors.InvalidParam, 'references')
172
- }
176
+ if (references.length === 0 && !removeall) return
173
177
 
174
178
  if (!IsValidArray(references, IsValidAddress)) {
175
- ERROR(Errors.IsValidArray, 'references')
179
+ ERROR(Errors.IsValidArray, 'remove_reference')
176
180
  }
177
181
 
178
182
  let txb = this.protocol.CurrentSession();
@@ -210,9 +214,7 @@ export class Repository {
210
214
  }
211
215
  // add or modify the old
212
216
  add_policies(policies:Repository_Policy[], passport?:PassportObject) {
213
- if (!policies) {
214
- ERROR(Errors.InvalidParam, 'policies')
215
- }
217
+ if (policies.length === 0) return;
216
218
 
217
219
  let bValid = true;
218
220
  policies.forEach((p) => {
@@ -233,7 +235,7 @@ export class Repository {
233
235
  arguments:[passport, Protocol.TXB_OBJECT(txb, this.object),
234
236
  txb.pure(policy.key),
235
237
  txb.pure(policy.description),
236
- permission_index, txb.pure(policy.value_type, BCS.U8),
238
+ permission_index, txb.pure(policy.data_type, BCS.U8),
237
239
  Protocol.TXB_OBJECT(txb, this.permission)]
238
240
  })
239
241
  } else {
@@ -242,53 +244,59 @@ export class Repository {
242
244
  arguments:[Protocol.TXB_OBJECT(txb, this.object),
243
245
  txb.pure(policy.key),
244
246
  txb.pure(policy.description),
245
- permission_index, txb.pure(policy.value_type, BCS.U8),
247
+ permission_index, txb.pure(policy.data_type, BCS.U8),
246
248
  Protocol.TXB_OBJECT(txb, this.permission)]
247
249
  })
248
250
  }
249
251
  });
250
252
  }
251
253
 
252
- remove_policies(policy_keys:string[], removeall?:boolean, passport?:PassportObject) {
253
- if (!removeall && !policy_keys) {
254
- ERROR(Errors.AllInvalid, 'policy_keys & removeall')
255
- }
256
- if (policy_keys && !IsValidArray(policy_keys, Repository.IsValidName)){
254
+ remove_policies(policy_keys:string[], passport?:PassportObject) {
255
+ if (policy_keys.length === 0) return ;
256
+ if (!IsValidArray(policy_keys, Repository.IsValidName)){
257
257
  ERROR(Errors.InvalidParam, 'policy_keys')
258
258
  }
259
259
 
260
260
  let txb = this.protocol.CurrentSession();
261
261
  if (passport) {
262
- if (removeall) {
263
- txb.moveCall({
264
- target:this.protocol.RepositoryFn('policy_remove_all_with_passport') as FnCallType,
265
- arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), Protocol.TXB_OBJECT(txb, this.permission)]
266
- })
267
- } else {
268
- txb.moveCall({
269
- target:this.protocol.RepositoryFn('policy_remove_with_passport') as FnCallType,
270
- arguments:[passport, Protocol.TXB_OBJECT(txb, this.object),
271
- txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(policy_keys))),
272
- Protocol.TXB_OBJECT(txb, this.permission)]
273
- })
274
- }
262
+ txb.moveCall({
263
+ target:this.protocol.RepositoryFn('policy_remove_with_passport') as FnCallType,
264
+ arguments:[passport, Protocol.TXB_OBJECT(txb, this.object),
265
+ txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(policy_keys))),
266
+ Protocol.TXB_OBJECT(txb, this.permission)]
267
+ })
275
268
  } else {
276
- if (removeall) {
277
- txb.moveCall({
278
- target:this.protocol.RepositoryFn('policy_remove_all') as FnCallType,
279
- arguments:[Protocol.TXB_OBJECT(txb, this.object), Protocol.TXB_OBJECT(txb, this.permission)]
280
- })
281
- } else {
282
- txb.moveCall({
283
- target:this.protocol.RepositoryFn('policy_remove') as FnCallType,
284
- arguments:[Protocol.TXB_OBJECT(txb, this.object),
285
- txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(policy_keys))),
286
- Protocol.TXB_OBJECT(txb, this.permission)]
287
- })
288
- }
269
+ txb.moveCall({
270
+ target:this.protocol.RepositoryFn('policy_remove') as FnCallType,
271
+ arguments:[Protocol.TXB_OBJECT(txb, this.object),
272
+ txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(policy_keys))),
273
+ Protocol.TXB_OBJECT(txb, this.permission)]
274
+ })
289
275
  }
290
-
291
276
  }
277
+ rename_policy(policy_key:string, new_policy_key:string, passport?:PassportObject) {
278
+ if (!IsValidName(policy_key) || !IsValidName(new_policy_key)) {
279
+ ERROR(Errors.IsValidName, 'change_policy')
280
+ }
281
+
282
+ let txb = this.protocol.CurrentSession();
283
+ if (passport) {
284
+ txb.moveCall({
285
+ target:this.protocol.RepositoryFn('policy_rename_with_passport') as FnCallType,
286
+ arguments:[passport, Protocol.TXB_OBJECT(txb, this.object),
287
+ txb.pure(policy_key), txb.pure(new_policy_key),
288
+ Protocol.TXB_OBJECT(txb, this.permission)]
289
+ })
290
+ } else {
291
+ txb.moveCall({
292
+ target:this.protocol.RepositoryFn('policy_rename') as FnCallType,
293
+ arguments:[Protocol.TXB_OBJECT(txb, this.object),
294
+ txb.pure(policy_key), txb.pure(new_policy_key),
295
+ Protocol.TXB_OBJECT(txb, this.permission)]
296
+ })
297
+ }
298
+ }
299
+
292
300
  // PermissionIndex.description_set
293
301
  set_description(description:string, passport?:PassportObject) {
294
302
  if (!IsValidDesription(description)){
@@ -347,7 +355,6 @@ export class Repository {
347
355
  Protocol.TXB_OBJECT(txb, this.permission)]
348
356
  })
349
357
  }
350
-
351
358
  }
352
359
 
353
360
  set_policy_permission(policy:string, permission_index?:number, passport?:PassportObject) {
@@ -376,7 +383,6 @@ export class Repository {
376
383
  arguments:[Protocol.TXB_OBJECT(txb, this.object), index, Protocol.TXB_OBJECT(txb, this.permission)]
377
384
  })
378
385
  }
379
-
380
386
  }
381
387
 
382
388
  change_permission(new_permission:PermissionObject) {
@@ -392,14 +398,115 @@ export class Repository {
392
398
  this.permission = new_permission
393
399
  }
394
400
 
395
- static MAX_POLICY_COUNT = 1000;
401
+ static MAX_POLICY_COUNT = 200;
396
402
  static MAX_KEY_LENGTH = 128;
397
403
  static MAX_VALUE_LENGTH = 204800;
404
+ static MAX_REFERENCE_COUNT = 100;
398
405
  static IsValidName = (key:string) => {
399
406
  return key.length <= Repository.MAX_KEY_LENGTH && key.length != 0;
400
407
  }
401
408
  static IsValidValue = (value:Uint8Array) => {
402
409
  return value.length < Repository.MAX_VALUE_LENGTH;
403
410
  }
411
+ static parseObjectType = (chain_type:string) : string => {
412
+ if (chain_type) {
413
+ const s = 'repository::Repository<'
414
+ const i = chain_type.indexOf(s);
415
+ if (i > 0) {
416
+ let r = chain_type.slice(i + s.length, chain_type.length-1);
417
+ return r
418
+ }
419
+ }
420
+ return '';
421
+ }
422
+
423
+ static rpc_de_data(fields:any) : RepData [] {
424
+ const rep: RepData[] = fields?.map((v:any) => {
425
+ const value = new Uint8Array((v?.data?.content?.fields as any)?.value);
426
+ const type = value?.length > 0 ? value[0] as ValueType : null;
427
+ var d : any = value.length > 0 ? value.slice(1) : Uint8Array.from([]);
428
+ if (type === ValueType.TYPE_STRING) {
429
+ d = Bcs.getInstance().de(ValueType.TYPE_VEC_U8, d);
430
+ d = new TextDecoder().decode(Uint8Array.from(d));
431
+ } else if (type === ValueType.TYPE_VEC_STRING) {
432
+ d = Bcs.getInstance().de(ValueType.TYPE_VEC_VEC_U8, d) as [];
433
+ d = d.map((i:any) => {
434
+ return new TextDecoder().decode(Uint8Array.from(i));
435
+ })
436
+ } else {
437
+ d = Bcs.getInstance().de(value[0], d);
438
+ if (type === ValueType.TYPE_ADDRESS) {
439
+ d = '0x' + d;
440
+ } else if (type === ValueType.TYPE_VEC_ADDRESS) {
441
+ d = d.map((v:string) => { return ('0x' + v) } );
442
+ }
443
+ };
444
+ return {object:v?.data?.content?.fields?.id?.id, id:(v?.data?.content?.fields as any)?.name?.fields?.id,
445
+ name:(v?.data?.content?.fields as any)?.name?.fields?.key,
446
+ data:d, dataType: ValueTypeConvert(type)
447
+ }
448
+ });
449
+ return rep;
450
+ }
451
+
452
+ static DataType2ValueType(data:string) : ValueType | undefined{
453
+ try {
454
+ const value = BigInt(data);
455
+ var t = ValueType.TYPE_U8;
456
+ if (value <= MAX_U8) {
457
+ } else if (value <= MAX_U64) {
458
+ t = ValueType.TYPE_U64;
459
+ } else if (value <= MAX_U128) {
460
+ t = ValueType.TYPE_U128;
461
+ } else if (value <= MAX_U256) {
462
+ t = ValueType.TYPE_U256;
463
+ } else {
464
+ return undefined
465
+ }
466
+ } catch (e) {
467
+ console.log(e)
468
+ } return undefined
469
+ }
470
+
471
+ static ResolveRepositoryData = (dataType:RepositoryValueType, data:string | string[]) : {type:ValueType, data: Uint8Array} | undefined => {
472
+ if (dataType === RepositoryValueType.String) {
473
+ return {type: ValueType.TYPE_STRING, data: Bcs.getInstance().ser(ValueType.TYPE_VEC_U8, new TextEncoder().encode(data.toString()))}
474
+ } else if (dataType === RepositoryValueType.PositiveNumber) {
475
+ const t = Repository.DataType2ValueType(data as string);
476
+ if (!t) return undefined;
477
+ return {type:t, data:Bcs.getInstance().ser(t, data)}
478
+ } else if (dataType === RepositoryValueType.Address) {
479
+ if (!IsValidAddress(data as string)) return undefined;
480
+ return {type:ValueType.TYPE_ADDRESS, data:Bcs.getInstance().ser(ValueType.TYPE_ADDRESS, data)}
481
+ } else if (dataType === RepositoryValueType.Address_Vec) {
482
+ for(let i = 0; i < (data as string[]).length; ++i) {
483
+ if (!IsValidAddress((data as string[])[i])) return undefined;
484
+ }
485
+ return {type:ValueType.TYPE_VEC_ADDRESS, data:Bcs.getInstance().ser(ValueType.TYPE_VEC_ADDRESS, data)}
486
+ } else if (dataType === RepositoryValueType.PositiveNumber_Vec) {
487
+ let type = ValueType.TYPE_U8;
488
+ for(let i = 0; i < (data as string[]).length; ++i) {
489
+ const t = Repository.DataType2ValueType(data as string);
490
+ if (!t) return undefined;
491
+ if (t > type) type = t;
492
+ }
493
+ if (type === ValueType.TYPE_U8) {
494
+ type = ValueType.TYPE_VEC_U8;
495
+ } else if (type === ValueType.TYPE_U64) {
496
+ type = ValueType.TYPE_VEC_U64;
497
+ } else if (type === ValueType.TYPE_U128) {
498
+ type = ValueType.TYPE_VEC_U128;
499
+ } else {
500
+ type = ValueType.TYPE_VEC_U256;
501
+ }
502
+ return {type:type, data:Bcs.getInstance().ser(type, data)}
503
+ } else if (dataType === RepositoryValueType.String_Vec) {
504
+ const r = (data as string[]).map((v:string) => {
505
+ return new TextEncoder().encode(v);
506
+ })
507
+ return {type: ValueType.TYPE_VEC_STRING, data: Bcs.getInstance().ser(ValueType.TYPE_VEC_VEC_U8, r)}
508
+ }
509
+ return undefined
510
+ }
404
511
  }
405
512