wowok 1.2.4 → 1.2.7

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 (49) 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 +23 -7
  13. package/dist/permission.d.ts.map +1 -1
  14. package/dist/permission.js +149 -16
  15. package/dist/protocol.d.ts +35 -10
  16. package/dist/protocol.d.ts.map +1 -1
  17. package/dist/protocol.js +129 -21
  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 +15 -9
  28. package/dist/service.d.ts.map +1 -1
  29. package/dist/service.js +94 -56
  30. package/dist/utils.d.ts +18 -4
  31. package/dist/utils.d.ts.map +1 -1
  32. package/dist/utils.js +218 -45
  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 +22 -8
  38. package/src/entity.ts +25 -6
  39. package/src/exception.ts +3 -1
  40. package/src/guard.ts +1 -1
  41. package/src/machine.ts +2 -2
  42. package/src/permission.ts +168 -14
  43. package/src/protocol.ts +127 -17
  44. package/src/repository.ts +64 -44
  45. package/src/resource.ts +61 -20
  46. package/src/reward.ts +46 -23
  47. package/src/service.ts +100 -57
  48. package/src/utils.ts +186 -22
  49. package/src/vote.ts +14 -14
package/src/service.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { bcs, BCS, toHEX, fromHEX, getSuiMoveConfig } from '@mysten/bcs';
2
- import { IsValidArray, IsValidPercent, IsValidName_AllowEmpty, Bcs, array_unique, IsValidArgType, IsValidDesription,
2
+ import { IsValidArray, IsValidPercent, IsValidName_AllowEmpty, Bcs, array_unique, IsValidTokenType, IsValidDesription,
3
3
  IsValidAddress, IsValidEndpoint, OptionNone, IsValidUint, IsValidInt, IsValidName, } from './utils'
4
4
  import { FnCallType, GuardObject, PassportObject, PermissionObject, RepositoryObject, MachineObject, ServiceAddress,
5
5
  ServiceObject, DiscountObject, OrderObject, OrderAddress, CoinObject, Protocol, ValueType,
@@ -15,6 +15,7 @@ export type Service_Sale = {
15
15
  item:string;
16
16
  price:number;
17
17
  stock:number;
18
+ endpoint?:string;
18
19
  }
19
20
  export enum Service_Discount_Type {
20
21
  ratio = 0, // -off%
@@ -39,11 +40,12 @@ export type Customer_RequiredInfo = {
39
40
  customer_info_crypt: string[];
40
41
  }
41
42
  export enum BuyRequiredEnum {
42
- address = 'Address',
43
- phone = 'Phone',
44
- name = 'Name',
45
- postcode = 'Postcode'
43
+ address = 'address',
44
+ phone = 'phone',
45
+ name = 'name',
46
+ postcode = 'postcode'
46
47
  }
48
+
47
49
  export type Service_Buy = {
48
50
  item: string;
49
51
  max_price: number;
@@ -62,6 +64,8 @@ export class Service {
62
64
  protected object : TxbObject;
63
65
  protected protocol;
64
66
 
67
+ //static token2coin = (token:string) => { return '0x2::coin::Coin<' + token + '>'};
68
+
65
69
  get_pay_type() { return this.pay_token_type }
66
70
  get_object() { return this.object }
67
71
  private constructor(protocol: Protocol, pay_token_type:string, permission:PermissionObject) {
@@ -70,18 +74,18 @@ export class Service {
70
74
  this.permission = permission
71
75
  this.object = ''
72
76
  }
73
- static From(protocol: Protocol, pay_token_type:string, permission:PermissionObject, object:TxbObject) : Service {
74
- let s = new Service(protocol, pay_token_type, permission);
77
+ static From(protocol: Protocol, token_type:string, permission:PermissionObject, object:TxbObject) : Service {
78
+ let s = new Service(protocol, token_type, permission);
75
79
  s.object = Protocol.TXB_OBJECT(protocol.CurrentSession(), object);
76
80
  return s
77
81
  }
78
- static New(protocol: Protocol, pay_token_type:string, permission:PermissionObject, description:string,
82
+ static New(protocol: Protocol, token_type:string, permission:PermissionObject, description:string,
79
83
  payee_address:string, endpoint?:string, passport?:PassportObject) : Service {
80
84
  if (!Protocol.IsValidObjects([permission])) {
81
85
  ERROR(Errors.IsValidObjects)
82
86
  }
83
- if (!IsValidArgType(pay_token_type)) {
84
- ERROR(Errors.IsValidArgType, 'this.pay_token_type')
87
+ if (!IsValidTokenType(token_type)) {
88
+ ERROR(Errors.IsValidTokenType, 'New: pay_token_type')
85
89
  }
86
90
  if (!IsValidDesription(description)) {
87
91
  ERROR(Errors.IsValidDesription)
@@ -94,10 +98,12 @@ export class Service {
94
98
  ERROR(Errors.IsValidEndpoint)
95
99
  }
96
100
 
101
+ let pay_token_type = token_type;
97
102
  let s = new Service(protocol, pay_token_type, permission);
98
103
  let txb = protocol.CurrentSession();
99
104
  let ep = endpoint? txb.pure(Bcs.getInstance().ser(ValueType.TYPE_OPTION_STRING, endpoint)) : OptionNone(txb);
100
105
 
106
+
101
107
  if (passport) {
102
108
  s.object = txb.moveCall({
103
109
  target:protocol.ServiceFn('new_with_passport') as FnCallType,
@@ -107,7 +113,7 @@ export class Service {
107
113
  } else {
108
114
  s.object = txb.moveCall({
109
115
  target:protocol.ServiceFn('new') as FnCallType,
110
- arguments:[txb.pure(description), txb.pure(payee_address, BCS.ADDRESS), ep, Protocol.TXB_OBJECT(txb, permission)],
116
+ arguments:[txb.pure(description, BCS.STRING), txb.pure(payee_address, BCS.ADDRESS), ep, Protocol.TXB_OBJECT(txb, permission)],
111
117
  typeArguments:[pay_token_type],
112
118
  })
113
119
  }
@@ -251,6 +257,32 @@ export class Service {
251
257
  })
252
258
  }
253
259
  }
260
+ set_sale_endpoint(item:string, endpoint?:string, bNotFoundAssert:boolean=true, passport?:PassportObject) {
261
+ if (!IsValidName(item)) {
262
+ ERROR(Errors.IsValidName, 'set_sale_endpoint')
263
+ }
264
+ if (endpoint && !IsValidEndpoint(endpoint)) {
265
+ ERROR(Errors.IsValidEndpoint, 'set_sale_endpoint')
266
+ }
267
+ let txb = this.protocol.CurrentSession();
268
+ let ep = endpoint? txb.pure(Bcs.getInstance().ser(ValueType.TYPE_OPTION_STRING, endpoint)) : OptionNone(txb);
269
+ if (passport) {
270
+ txb.moveCall({
271
+ target:this.protocol.ServiceFn('sale_endpoint_set_with_passport') as FnCallType,
272
+ arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), txb.pure(item), ep,
273
+ txb.pure(bNotFoundAssert, BCS.BOOL), Protocol.TXB_OBJECT(txb, this.permission)],
274
+ typeArguments:[this.pay_token_type]
275
+ })
276
+ } else {
277
+ txb.moveCall({
278
+ target:this.protocol.ServiceFn('sale_endpoint_set') as FnCallType,
279
+ arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(item), ep,
280
+ txb.pure(bNotFoundAssert, BCS.BOOL), Protocol.TXB_OBJECT(txb, this.permission)],
281
+ typeArguments:[this.pay_token_type]
282
+ })
283
+ }
284
+ }
285
+
254
286
  set_payee(payee:string, passport?:PassportObject) {
255
287
  if (!IsValidAddress(payee)) {
256
288
  ERROR(Errors.IsValidAddress, 'payee');
@@ -272,8 +304,9 @@ export class Service {
272
304
  }
273
305
  }
274
306
  repository_add(repository:RepositoryObject, passport?:PassportObject) {
275
- if (!Protocol.IsValidObjects([this.object, this.permission, repository])) return false;
276
- if (!IsValidArgType(this.pay_token_type)) return false;
307
+ if (!Protocol.IsValidObjects([repository])) {
308
+ ERROR(Errors.IsValidObjects, 'repository_add');
309
+ }
277
310
 
278
311
  let txb = this.protocol.CurrentSession();
279
312
  if (passport) {
@@ -287,9 +320,8 @@ export class Service {
287
320
  target:this.protocol.ServiceFn('repository_add') as FnCallType,
288
321
  arguments:[Protocol.TXB_OBJECT(txb, this.object), Protocol.TXB_OBJECT(txb, repository), Protocol.TXB_OBJECT(txb, this.permission)],
289
322
  typeArguments:[this.pay_token_type]
290
- })
323
+ })
291
324
  }
292
-
293
325
  }
294
326
  repository_remove(repository_address?:string[], removeall?:boolean, passport?:PassportObject) {
295
327
  if (!removeall && !repository_address) {
@@ -487,14 +519,17 @@ export class Service {
487
519
  return bValid
488
520
  }
489
521
 
490
- add_sale(sales:Service_Sale[], passport?:PassportObject) {
522
+ add_sale(sales:Service_Sale[], bExistAssert:boolean=false, passport?:PassportObject) {
491
523
  if (!sales || !this.is_valid_sale(sales)) {
492
- ERROR(Errors.InvalidParam, 'Service_Sale')
524
+ ERROR(Errors.InvalidParam, 'add_sale')
493
525
  }
494
526
 
495
- let names: string[] = []; let price: number[] = []; let stock: number[] = [];
527
+ let names: string[] = []; let price: number[] = []; let stock: number[] = []; let endpoint: string[] = [];
496
528
  sales.forEach((s) => {
497
- names.push(s.item); price.push(s.price); stock.push(s.stock);
529
+ if (s.endpoint && !IsValidEndpoint(s.endpoint)) {
530
+ ERROR(Errors.IsValidEndpoint, 'add_sale')
531
+ }
532
+ names.push(s.item); price.push(s.price); stock.push(s.stock); endpoint.push(s.endpoint ?? '')
498
533
  })
499
534
 
500
535
  let txb = this.protocol.CurrentSession();
@@ -502,7 +537,8 @@ export class Service {
502
537
  txb.moveCall({
503
538
  target:this.protocol.ServiceFn('sales_add_with_passport') as FnCallType,
504
539
  arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, names)),
505
- txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, price)), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, stock)),
540
+ txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, endpoint)), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, price)),
541
+ txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, stock)), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_BOOL, bExistAssert)),
506
542
  Protocol.TXB_OBJECT(txb, this.permission)],
507
543
  typeArguments:[this.pay_token_type]
508
544
  })
@@ -510,15 +546,16 @@ export class Service {
510
546
  txb.moveCall({
511
547
  target:this.protocol.ServiceFn('sales_add') as FnCallType,
512
548
  arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, names)),
549
+ txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, endpoint)),
513
550
  txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, price)), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, stock)),
551
+ txb.pure(Bcs.getInstance().ser(ValueType.TYPE_BOOL, bExistAssert)),
514
552
  Protocol.TXB_OBJECT(txb, this.permission)],
515
553
  typeArguments:[this.pay_token_type]
516
554
  })
517
555
  }
518
-
519
556
  }
520
- remove_sales(sales?:string[], removeall?:boolean, passport?:PassportObject) {
521
- if (!removeall && !sales) {
557
+ remove_sales(sales?:string[], passport?:PassportObject) {
558
+ if (!sales) {
522
559
  ERROR(Errors.AllInvalid, 'sales & removeall')
523
560
  }
524
561
  if (sales && !IsValidArray(sales, IsValidName)) {
@@ -527,35 +564,19 @@ export class Service {
527
564
 
528
565
  let txb = this.protocol.CurrentSession();
529
566
  if (passport) {
530
- if (removeall) {
531
- txb.moveCall({
532
- target:this.protocol.ServiceFn('sales_remove_all_with_passport') as FnCallType,
533
- arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), Protocol.TXB_OBJECT(txb, this.permission)],
534
- typeArguments:[this.pay_token_type]
535
- })
536
- } else {
537
- txb.moveCall({
538
- target:this.protocol.ServiceFn('sales_remove_with_passport') as FnCallType,
539
- arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(sales!))),
540
- Protocol.TXB_OBJECT(txb, this.permission)],
541
- typeArguments:[this.pay_token_type]
542
- })
543
- }
567
+ txb.moveCall({
568
+ target:this.protocol.ServiceFn('sales_remove_with_passport') as FnCallType,
569
+ arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(sales!))),
570
+ Protocol.TXB_OBJECT(txb, this.permission)],
571
+ typeArguments:[this.pay_token_type]
572
+ })
544
573
  } else {
545
- if (removeall) {
546
- txb.moveCall({
547
- target:this.protocol.ServiceFn('sales_remove_all') as FnCallType,
548
- arguments:[Protocol.TXB_OBJECT(txb, this.object), Protocol.TXB_OBJECT(txb, this.permission)],
549
- typeArguments:[this.pay_token_type]
550
- })
551
- } else {
552
- txb.moveCall({
553
- target:this.protocol.ServiceFn('sales_remove') as FnCallType,
554
- arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(sales!))),
555
- Protocol.TXB_OBJECT(txb, this.permission)],
556
- typeArguments:[this.pay_token_type]
557
- })
558
- }
574
+ txb.moveCall({
575
+ target:this.protocol.ServiceFn('sales_remove') as FnCallType,
576
+ arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(sales!))),
577
+ Protocol.TXB_OBJECT(txb, this.permission)],
578
+ typeArguments:[this.pay_token_type]
579
+ })
559
580
  }
560
581
  }
561
582
 
@@ -760,21 +781,22 @@ export class Service {
760
781
  ERROR(Errors.InvalidParam, 'customer_required')
761
782
  }
762
783
 
784
+ let req = array_unique(customer_required) as string[];
763
785
  let txb = this.protocol.CurrentSession();
764
786
  if (passport) {
765
787
  txb.moveCall({
766
788
  target:this.protocol.ServiceFn('required_set_with_passport') as FnCallType,
767
789
  arguments:[passport, Protocol.TXB_OBJECT(txb, this.object),
768
- txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_VEC_U8, array_unique(customer_required))),
769
- txb.pure(pubkey, 'vector<u8>'), Protocol.TXB_OBJECT(txb, this.permission)],
790
+ txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, req)),
791
+ txb.pure(pubkey, BCS.STRING), Protocol.TXB_OBJECT(txb, this.permission)],
770
792
  typeArguments:[this.pay_token_type]
771
793
  })
772
794
  } else {
773
795
  txb.moveCall({
774
796
  target:this.protocol.ServiceFn('required_set') as FnCallType,
775
797
  arguments:[Protocol.TXB_OBJECT(txb, this.object),
776
- txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_VEC_U8, array_unique(customer_required))),
777
- txb.pure(pubkey, 'vector<u8>'), Protocol.TXB_OBJECT(txb, this.permission)],
798
+ txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, req)),
799
+ txb.pure(pubkey, BCS.STRING), Protocol.TXB_OBJECT(txb, this.permission)],
778
800
  typeArguments:[this.pay_token_type]
779
801
  })
780
802
  }
@@ -915,7 +937,7 @@ export class Service {
915
937
  names.push(v.item)
916
938
  })
917
939
  if (!bValid) {
918
- ERROR(Errors.InvalidParam, 'buy_items')
940
+ ERROR(Errors.InvalidParam, 'buy_items 2')
919
941
  }
920
942
 
921
943
  let name:string[] = []; let price:number[] = []; let stock:number[] = []; let order;
@@ -1007,5 +1029,26 @@ export class Service {
1007
1029
  }
1008
1030
 
1009
1031
  static MAX_DISCOUNT_COUNT_ONCE = 200;
1010
- static MAX_DISCOUNT_RECEIVER_COUNT = 200;
1032
+ static MAX_DISCOUNT_RECEIVER_COUNT = 20;
1033
+ static MAX_GUARD_COUNT = 16;
1034
+ static MAX_REPOSITORY_COUNT = 16;
1035
+
1036
+ static parseObjectType = (chain_type:string | undefined | null) : string => {
1037
+ if (chain_type) {
1038
+ const s = 'service::Service<'
1039
+ const i = chain_type.indexOf(s);
1040
+ if (i > 0) {
1041
+ return chain_type.slice(i + s.length, chain_type.length-1);
1042
+ }
1043
+ }
1044
+ return '';
1045
+ }
1046
+ static endpoint = (service_endpoint:string, item_endpoint:string, item_name:string) => {
1047
+ if (item_endpoint) {
1048
+ return item_endpoint
1049
+ } else if (service_endpoint) {
1050
+ return service_endpoint + '/sales/' + encodeURI(item_name);
1051
+ }
1052
+ }
1053
+
1011
1054
  }
package/src/utils.ts CHANGED
@@ -1,10 +1,64 @@
1
- import { bcs, BCS, toHEX, fromHEX, getSuiMoveConfig, TypeName, StructTypeDefinition } from '@mysten/bcs';
1
+ import { bcs, BCS, toHEX, fromHEX, getSuiMoveConfig, TypeName, BcsReader } from '@mysten/bcs';
2
2
  import { TransactionBlock, Inputs, TransactionResult, TransactionArgument } from '@mysten/sui.js/transactions';
3
3
  import { ERROR, Errors } from './exception';
4
4
  import { isValidSuiAddress, isValidSuiObjectId } from '@mysten/sui.js/utils'
5
- import { ValueType } from './protocol'
5
+ import { RepositoryValueType, ValueType } from './protocol'
6
+
7
+ export const MAX_U8 = BigInt('256');
8
+ export const MAX_U64 = BigInt('18446744073709551615');
9
+ export const MAX_U128 = BigInt('340282366920938463463374607431768211455');
10
+ export const MAX_U256 = BigInt('115792089237316195423570985008687907853269984665640564039457584007913129639935');
6
11
 
7
12
  export const OPTION_NONE = 0;
13
+
14
+ export const ValueTypeConvert = (type:ValueType | null | undefined) : RepositoryValueType | number => {
15
+ if (type === ValueType.TYPE_U8 || type === ValueType.TYPE_U64 || type === ValueType.TYPE_U128 ||
16
+ type === ValueType.TYPE_U256 || type === ValueType.TYPE_BOOL) {
17
+ return RepositoryValueType.PositiveNumber
18
+ } else if (type === ValueType.TYPE_VEC_U8 || type === ValueType.TYPE_VEC_U64 || type === ValueType.TYPE_VEC_U128 ||
19
+ type === ValueType.TYPE_VEC_U256|| type === ValueType.TYPE_VEC_BOOL) {
20
+ return RepositoryValueType.PositiveNumber_Vec
21
+ } else if (type === ValueType.TYPE_ADDRESS) {
22
+ return RepositoryValueType.Address
23
+ } else if (type === ValueType.TYPE_VEC_ADDRESS) {
24
+ return RepositoryValueType.Address_Vec
25
+ } else if (type === ValueType.TYPE_STRING) {
26
+ return RepositoryValueType.String
27
+ } else if (type === ValueType.TYPE_VEC_STRING) {
28
+ return RepositoryValueType.String_Vec
29
+ }
30
+ return -1;
31
+ }
32
+
33
+ export const ResolveRepositoryData = (dataType:RepositoryValueType, data:Uint8Array) : {type:ValueType, data: Uint8Array} | undefined => {
34
+ if (dataType === RepositoryValueType.String) {
35
+ return {type: ValueType.TYPE_STRING, data: Bcs.getInstance().ser(ValueType.TYPE_VEC_U8, new TextEncoder().encode(data.toString()))}
36
+ } else if (dataType === RepositoryValueType.PositiveNumber) {
37
+ try {
38
+ const value = BigInt(data.toString());
39
+ var t = ValueType.TYPE_U8;
40
+ if (value <= MAX_U8) {
41
+ } else if (value <= MAX_U64) {
42
+ t = ValueType.TYPE_U64;
43
+ } else if (value <= MAX_U128) {
44
+ t = ValueType.TYPE_U128;
45
+ } else if (value <= MAX_U256) {
46
+ t = ValueType.TYPE_U256;
47
+ } else {
48
+ return undefined
49
+ }
50
+ } catch (e) {
51
+ console.log(e)
52
+ return undefined
53
+ }
54
+ return {type:t, data:Bcs.getInstance().ser(t, data)}
55
+ } else if (dataType === RepositoryValueType.Address) {
56
+ return {type:ValueType.TYPE_ADDRESS, data:Bcs.getInstance().ser(ValueType.TYPE_ADDRESS, data)}
57
+ }
58
+ //@ todo vector....
59
+ return undefined
60
+ }
61
+
8
62
  export const readOption = (arr: number[], de:ValueType) : {bNone:boolean, value:any}=> {
9
63
  let o = arr.splice(0, 1);
10
64
  if (o[0] == 1) { // true
@@ -133,6 +187,20 @@ export class Bcs {
133
187
  'none': null,
134
188
  'some': 'T',
135
189
  });
190
+ this.bcs.registerStructType('EntStruct', {
191
+ 'avatar': 'vector<u8>',
192
+ 'resource': "Option<address>",
193
+ 'like': BCS.U32,
194
+ 'dislike': BCS.U32,
195
+ })
196
+ this.bcs.registerStructType('PersonalInfo', {
197
+ 'name': 'vector<u8>',
198
+ 'description': 'vector<u8>',
199
+ 'avatar': BCS.STRING,
200
+ 'twitter': BCS.STRING,
201
+ 'discord': BCS.STRING,
202
+ 'homepage': BCS.STRING,
203
+ })
136
204
  }
137
205
  static getInstance() : Bcs {
138
206
  if (!Bcs._instance) {
@@ -140,6 +208,7 @@ export class Bcs {
140
208
  };
141
209
  return Bcs._instance;
142
210
  }
211
+
143
212
  ser(type:ValueType, data:Uint8Array | any) : Uint8Array {
144
213
  switch(type) {
145
214
  case ValueType.TYPE_BOOL:
@@ -201,6 +270,7 @@ export class Bcs {
201
270
  case ValueType.TYPE_U64:
202
271
  return this.bcs.de(BCS.U64, data);
203
272
  case ValueType.TYPE_U8:
273
+ console.log(data)
204
274
  return this.bcs.de(BCS.U8, data);
205
275
  case ValueType.TYPE_VEC_U8:
206
276
  return this.bcs.de('vector<u8>', data);
@@ -242,6 +312,30 @@ export class Bcs {
242
312
  ERROR(Errors.bcsTypeInvalid, 'de');
243
313
  }
244
314
  }
315
+
316
+ de_ent(data:Uint8Array) : any {
317
+ const struct_vec = this.bcs.de('vector<u8>', data);
318
+ return this.bcs.de('EntStruct', Uint8Array.from(struct_vec));
319
+ /* const reader = new BcsReader(data);
320
+ const total_len = reader.readULEB();
321
+ console.log(avatar_len)
322
+ const avatar = reader.readBytes(avatar_len);
323
+ console.log(avatar)
324
+ const option_resource = reader.read8();
325
+ var resource = '';
326
+ if (option_resource != 0) {
327
+ resource = reader.read256();
328
+ }
329
+ const like = reader.read32();
330
+ const dislike = reader.read32();
331
+ return {avatar:avatar, resource:resource, like:like, dislike:dislike}*/
332
+ }
333
+ de_entInfo(data:Uint8Array) : any {
334
+ let r = this.bcs.de('PersonalInfo', data);
335
+ r.name = new TextDecoder().decode(Uint8Array.from(r.name));
336
+ r.description = new TextDecoder().decode(Uint8Array.from(r.description));
337
+ return r
338
+ }
245
339
  }
246
340
 
247
341
  export function stringToUint8Array(str:string) : Uint8Array {
@@ -264,7 +358,6 @@ export function numToUint8Array(num:number) : Uint8Array {
264
358
  return new Uint8Array(a)
265
359
  }
266
360
 
267
- // 判断是否为数组
268
361
  export const isArr = (origin: any): boolean => {
269
362
  let str = '[object Array]'
270
363
  return Object.prototype.toString.call(origin) == str ? true : false
@@ -298,19 +391,85 @@ export const IsValidDesription = (description:string) : boolean => { return desc
298
391
  export const IsValidName = (name:string) : boolean => { if(!name) return false; return name.length <= MAX_NAME_LENGTH && name.length != 0 }
299
392
  export const IsValidName_AllowEmpty = (name:string) : boolean => { return name.length <= MAX_NAME_LENGTH }
300
393
  export const IsValidEndpoint = (endpoint:string) : boolean => { if (!endpoint) return false; return endpoint.length <= MAX_ENDPOINT_LENGTH }
301
- export const IsValidAddress = (addr:string) : boolean => { if (!addr || !isValidSuiAddress(addr)) return false; return true}
302
- export const IsValidArgType = (argType: string) : boolean => { if (!argType) return false; return argType.length != 0 }
303
- export const IsValidUint = (value: number) : boolean => { return Number.isSafeInteger(value) && value != 0 }
304
- export const IsValidInt = (value: number) : boolean => { return Number.isSafeInteger(value) }
305
- export const IsValidPercent = (value: number) : boolean => { return Number.isSafeInteger(value) && value > 0 && value <= 100 }
394
+ export const IsValidAddress = (addr:string) : boolean => {
395
+ if (!addr || !isValidSuiAddress(addr)) {
396
+ return false;
397
+ }
398
+ return true
399
+ }
400
+ export const IsValidTokenType = (argType: string) : boolean => {
401
+ if (!argType || argType.length === 0) {
402
+ return false;
403
+ }
404
+ let arr = argType.split('::');
405
+ if (arr.length !== 3) {
406
+ return false;
407
+ }
408
+ if ((!IsValidAddress(arr[0]) && arr[0] != '0x2') || arr[1].length === 0 || arr[2].length === 0) {
409
+ return false;
410
+ }
411
+ return true;
412
+ }
413
+ export const IsValidArgType = (argType: string) : boolean => {
414
+ if (!argType || argType.length === 0) {
415
+ return false;
416
+ }
417
+ let arr = argType.split('::');
418
+ if (arr.length < 3) {
419
+ return false;
420
+ }
421
+ return true;
422
+ }
423
+ export const IsValidUint = (value: number | string) : boolean => {
424
+ if (typeof(value) === 'string') {
425
+ value = parseInt(value as string);
426
+ }
427
+ return Number.isSafeInteger(value) && value > 0
428
+ }
429
+ export const IsValidInt = (value: number | string) : boolean => {
430
+ if (typeof(value) === 'string') {
431
+ value = parseInt(value as string);
432
+ }
433
+ return Number.isSafeInteger(value)
434
+ }
435
+ export const IsValidPercent = (value: number | string) : boolean => {
436
+ if (typeof(value) === 'string') {
437
+ value = parseInt(value as string);
438
+ }
439
+ return Number.isSafeInteger(value) && value > 0 && value <= 100
440
+ }
306
441
  export const IsValidArray = (arr: any[], validFunc:any) : boolean => {
307
- let bValid = true;
308
- arr.forEach((v) => {
309
- if (!validFunc(v)) {
310
- bValid = false;
442
+ for (let i = 0; i < arr.length; ++i) {
443
+ if (!validFunc(arr[i])) {
444
+ return false
311
445
  }
312
- })
313
- return bValid;
446
+ }
447
+ return true
448
+ }
449
+
450
+ export const ResolveU64 = (value:bigint) : bigint => {
451
+ const max = MAX_U64;
452
+ if (value > max) {
453
+ return max;
454
+ } else {
455
+ return value
456
+ }
457
+ }
458
+
459
+ export const ResolveBalance = (balance:string, decimals:number) : string => {
460
+ if (!balance) return ''
461
+ if (balance === '0') return '0'
462
+ if (decimals <= 0) return balance;
463
+ var pos = decimals - balance.length;
464
+ if (pos === 0) {
465
+ return '.' + balance;
466
+ } else if (pos < 0) {
467
+ let start = balance.slice(0, Math.abs(pos));
468
+ let end = balance.slice(Math.abs(pos));
469
+ return start + '.' + end;
470
+ } else {
471
+ return '.' + balance.padStart(pos, '0');
472
+ }
314
473
  }
315
474
 
316
475
  export const OptionNone = (txb:TransactionBlock) : TransactionArgument => { return txb.pure([], BCS.U8) };
@@ -321,13 +480,18 @@ export type ArgType = {
321
480
  token: string;
322
481
  }
323
482
  export const ParseType = (type:string) : ArgType => {
324
- let i = type.indexOf('<');
325
- if (i > 0 && type.length > 12) {
326
- let c = type.slice(0, i);
327
- if (c === '0x2::coin::Coin' || c === '0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin') {
328
- let coin = type.slice(i+1, type.length-1); // < >>
329
- let t = coin.lastIndexOf('::');
330
- return {isCoin:true, coin:coin, token:coin.slice(t+2)}
483
+ if (type) {
484
+ const COIN = '0x2::coin::Coin<';
485
+ let i = type.indexOf(COIN);
486
+ if (i >= 0) {
487
+ let coin = type.slice(i+COIN.length, type.length-1);
488
+ if (coin.indexOf('<') === -1) {
489
+ while (coin[coin.length-1] == '>') {
490
+ coin = coin.slice(0, -1);
491
+ };
492
+ let t = coin.lastIndexOf('::');
493
+ return {isCoin:true, coin:coin, token:coin.slice(t+2)}
494
+ }
331
495
  }
332
496
  }
333
497
  return {isCoin:false, coin:'', token:''}
@@ -360,5 +524,5 @@ export function isValidHttpUrl(url:string) : boolean {
360
524
  return false;
361
525
  }
362
526
 
363
- return r.protocol === "http:" || r.protocol === "https:";
527
+ return r.protocol === "http:" || r.protocol === "https:" || r.protocol === 'ipfs:';
364
528
  }
package/src/vote.ts CHANGED
@@ -28,7 +28,7 @@ export class Vote {
28
28
  v.object = Protocol.TXB_OBJECT(protocol.CurrentSession(), object)
29
29
  return v
30
30
  }
31
- static New(protocol:Protocol, permission:PermissionObject, description:string, minutes_duration:number,
31
+ static New(protocol:Protocol, permission:PermissionObject, description:string, minutes_duration:boolean, time:number,
32
32
  max_choice_count?:number, reference_address?:string, passport?:PassportObject) : Vote {
33
33
  if (!Protocol.IsValidObjects([permission])) {
34
34
  ERROR(Errors.IsValidObjects, 'permission')
@@ -36,8 +36,8 @@ export class Vote {
36
36
  if (!IsValidDesription(description)) {
37
37
  ERROR(Errors.IsValidDesription)
38
38
  }
39
- if (!IsValidUint(minutes_duration)) {
40
- ERROR(Errors.IsValidUint, 'minutes_duration')
39
+ if (!IsValidUint(time)) {
40
+ ERROR(Errors.IsValidUint, 'time')
41
41
  }
42
42
  if (max_choice_count && !IsValidUint(max_choice_count)) {
43
43
  ERROR(Errors.IsValidUint, 'max_choice_count')
@@ -57,14 +57,14 @@ export class Vote {
57
57
  if (passport) {
58
58
  v.object = txb.moveCall({
59
59
  target:protocol.VoteFn('new_with_passport') as FnCallType,
60
- arguments:[passport, txb.pure(description), reference, txb.pure(Protocol.CLOCK_OBJECT),
61
- txb.pure(minutes_duration, BCS.U64), txb.pure(choice_count, BCS.U8), Protocol.TXB_OBJECT(txb, permission)]
60
+ arguments:[passport, txb.pure(description), reference, txb.pure(Protocol.CLOCK_OBJECT), txb.pure(minutes_duration, BCS.BOOL),
61
+ txb.pure(time, BCS.U64), txb.pure(choice_count, BCS.U8), Protocol.TXB_OBJECT(txb, permission)]
62
62
  })
63
63
  } else {
64
64
  v.object = txb.moveCall({
65
65
  target:protocol.VoteFn('new') as FnCallType,
66
- arguments:[txb.pure(description), reference, txb.pure(Protocol.CLOCK_OBJECT),
67
- txb.pure(minutes_duration, BCS.U64), txb.pure(choice_count, BCS.U8), Protocol.TXB_OBJECT(txb, permission)]
66
+ arguments:[txb.pure(description), reference, txb.pure(Protocol.CLOCK_OBJECT), txb.pure(minutes_duration, BCS.BOOL),
67
+ txb.pure(time, BCS.U64), txb.pure(choice_count, BCS.U8), Protocol.TXB_OBJECT(txb, permission)]
68
68
  })
69
69
  }
70
70
  return v
@@ -314,23 +314,23 @@ export class Vote {
314
314
  }
315
315
  }
316
316
 
317
- expand_deadline(minutes_expand:number, passport?:PassportObject) {
318
- if (!IsValidUint(minutes_expand)) {
319
- ERROR(Errors.IsValidUint, 'minutes_expand')
317
+ expand_deadline(ms_expand:boolean, time:number, passport?:PassportObject) {
318
+ if (!IsValidUint(time)) {
319
+ ERROR(Errors.IsValidUint, 'time')
320
320
  }
321
321
 
322
322
  let txb = this.protocol.CurrentSession();
323
323
  if (passport) {
324
324
  txb.moveCall({
325
325
  target:this.protocol.VoteFn('deadline_expand_with_passport') as FnCallType,
326
- arguments:[passport, Protocol.TXB_OBJECT(txb, this.object),
327
- txb.pure(minutes_expand, BCS.U64), Protocol.TXB_OBJECT(txb, this.permission)]
326
+ arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), txb.pure(ms_expand, BCS.BOOL),
327
+ txb.pure(time, BCS.U64), Protocol.TXB_OBJECT(txb, this.permission)]
328
328
  })
329
329
  } else {
330
330
  txb.moveCall({
331
331
  target:this.protocol.VoteFn('deadline_expand') as FnCallType,
332
- arguments:[Protocol.TXB_OBJECT(txb, this.object),
333
- txb.pure(minutes_expand, BCS.U64), Protocol.TXB_OBJECT(txb, this.permission)]
332
+ arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(ms_expand, BCS.BOOL),
333
+ txb.pure(time, BCS.U64), Protocol.TXB_OBJECT(txb, this.permission)]
334
334
  })
335
335
  }
336
336