wowok 1.2.5 → 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 +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 +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 +165 -10
  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 +79 -46
  48. package/src/utils.ts +141 -15
  49. package/src/vote.ts +14 -14
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
@@ -304,7 +397,7 @@ export const IsValidAddress = (addr:string) : boolean => {
304
397
  }
305
398
  return true
306
399
  }
307
- export const IsValidArgType = (argType: string) : boolean => {
400
+ export const IsValidTokenType = (argType: string) : boolean => {
308
401
  if (!argType || argType.length === 0) {
309
402
  return false;
310
403
  }
@@ -312,12 +405,21 @@ export const IsValidArgType = (argType: string) : boolean => {
312
405
  if (arr.length !== 3) {
313
406
  return false;
314
407
  }
315
- if (!IsValidAddress(arr[0]) || arr[1].length === 0 || arr[2].length === 0) {
408
+ if ((!IsValidAddress(arr[0]) && arr[0] != '0x2') || arr[1].length === 0 || arr[2].length === 0) {
316
409
  return false;
317
410
  }
318
411
  return true;
319
412
  }
320
-
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
+ }
321
423
  export const IsValidUint = (value: number | string) : boolean => {
322
424
  if (typeof(value) === 'string') {
323
425
  value = parseInt(value as string);
@@ -337,13 +439,37 @@ export const IsValidPercent = (value: number | string) : boolean => {
337
439
  return Number.isSafeInteger(value) && value > 0 && value <= 100
338
440
  }
339
441
  export const IsValidArray = (arr: any[], validFunc:any) : boolean => {
340
- let bValid = true;
341
- arr.forEach((v) => {
342
- if (!validFunc(v)) {
343
- bValid = false;
442
+ for (let i = 0; i < arr.length; ++i) {
443
+ if (!validFunc(arr[i])) {
444
+ return false
344
445
  }
345
- })
346
- 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
+ }
347
473
  }
348
474
 
349
475
  export const OptionNone = (txb:TransactionBlock) : TransactionArgument => { return txb.pure([], BCS.U8) };
@@ -355,9 +481,9 @@ export type ArgType = {
355
481
  }
356
482
  export const ParseType = (type:string) : ArgType => {
357
483
  if (type) {
358
- const COIN = '0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<';
484
+ const COIN = '0x2::coin::Coin<';
359
485
  let i = type.indexOf(COIN);
360
- if (i > 0) {
486
+ if (i >= 0) {
361
487
  let coin = type.slice(i+COIN.length, type.length-1);
362
488
  if (coin.indexOf('<') === -1) {
363
489
  while (coin[coin.length-1] == '>') {
@@ -398,5 +524,5 @@ export function isValidHttpUrl(url:string) : boolean {
398
524
  return false;
399
525
  }
400
526
 
401
- return r.protocol === "http:" || r.protocol === "https:";
527
+ return r.protocol === "http:" || r.protocol === "https:" || r.protocol === 'ipfs:';
402
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