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.
- package/dist/demand.d.ts +3 -2
- package/dist/demand.d.ts.map +1 -1
- package/dist/demand.js +21 -7
- package/dist/entity.d.ts +2 -1
- package/dist/entity.d.ts.map +1 -1
- package/dist/entity.js +23 -7
- package/dist/exception.d.ts +3 -1
- package/dist/exception.d.ts.map +1 -1
- package/dist/exception.js +3 -1
- package/dist/guard.js +1 -1
- package/dist/machine.js +2 -2
- package/dist/permission.d.ts +20 -4
- package/dist/permission.d.ts.map +1 -1
- package/dist/permission.js +146 -13
- package/dist/protocol.d.ts +30 -6
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +124 -17
- package/dist/repository.d.ts +6 -3
- package/dist/repository.d.ts.map +1 -1
- package/dist/repository.js +59 -40
- package/dist/resource.d.ts +20 -6
- package/dist/resource.d.ts.map +1 -1
- package/dist/resource.js +58 -21
- package/dist/reward.d.ts +6 -3
- package/dist/reward.d.ts.map +1 -1
- package/dist/reward.js +43 -19
- package/dist/service.d.ts +6 -3
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +76 -44
- package/dist/utils.d.ts +15 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +150 -13
- package/dist/vote.d.ts +2 -2
- package/dist/vote.d.ts.map +1 -1
- package/dist/vote.js +14 -14
- package/package.json +1 -1
- package/src/demand.ts +26 -12
- package/src/entity.ts +33 -6
- package/src/exception.ts +3 -1
- package/src/guard.ts +2 -2
- package/src/machine.ts +207 -55
- package/src/permission.ts +168 -41
- package/src/progress.ts +101 -43
- package/src/protocol.ts +129 -20
- package/src/repository.ts +160 -53
- package/src/resource.ts +75 -24
- package/src/reward.ts +53 -32
- package/src/service.ts +109 -74
- package/src/utils.ts +174 -22
- package/src/vote.ts +30 -33
- package/src/wowok.ts +2 -2
package/src/service.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { bcs, BCS, toHEX, fromHEX, getSuiMoveConfig } from '@mysten/bcs';
|
|
2
|
-
import { IsValidArray, IsValidPercent, IsValidName_AllowEmpty, Bcs, array_unique,
|
|
3
|
-
IsValidAddress, IsValidEndpoint, OptionNone,
|
|
2
|
+
import { IsValidArray, IsValidPercent, IsValidName_AllowEmpty, Bcs, array_unique, IsValidTokenType, IsValidDesription,
|
|
3
|
+
IsValidAddress, IsValidEndpoint, OptionNone, IsValidUintLarge, IsValidInt, IsValidName, } from './utils'
|
|
4
4
|
import { FnCallType, GuardObject, PassportObject, PermissionObject, RepositoryObject, MachineObject, ServiceAddress,
|
|
5
5
|
ServiceObject, DiscountObject, OrderObject, OrderAddress, CoinObject, Protocol, ValueType,
|
|
6
6
|
TxbObject} from './protocol';
|
|
@@ -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%
|
|
@@ -63,7 +64,7 @@ export class Service {
|
|
|
63
64
|
protected object : TxbObject;
|
|
64
65
|
protected protocol;
|
|
65
66
|
|
|
66
|
-
static token2coin = (token:string) => { return '0x2::coin::Coin<' + token + '>'};
|
|
67
|
+
//static token2coin = (token:string) => { return '0x2::coin::Coin<' + token + '>'};
|
|
67
68
|
|
|
68
69
|
get_pay_type() { return this.pay_token_type }
|
|
69
70
|
get_object() { return this.object }
|
|
@@ -74,7 +75,7 @@ export class Service {
|
|
|
74
75
|
this.object = ''
|
|
75
76
|
}
|
|
76
77
|
static From(protocol: Protocol, token_type:string, permission:PermissionObject, object:TxbObject) : Service {
|
|
77
|
-
let s = new Service(protocol,
|
|
78
|
+
let s = new Service(protocol, token_type, permission);
|
|
78
79
|
s.object = Protocol.TXB_OBJECT(protocol.CurrentSession(), object);
|
|
79
80
|
return s
|
|
80
81
|
}
|
|
@@ -83,8 +84,8 @@ export class Service {
|
|
|
83
84
|
if (!Protocol.IsValidObjects([permission])) {
|
|
84
85
|
ERROR(Errors.IsValidObjects)
|
|
85
86
|
}
|
|
86
|
-
if (!
|
|
87
|
-
ERROR(Errors.
|
|
87
|
+
if (!IsValidTokenType(token_type)) {
|
|
88
|
+
ERROR(Errors.IsValidTokenType, 'New: pay_token_type')
|
|
88
89
|
}
|
|
89
90
|
if (!IsValidDesription(description)) {
|
|
90
91
|
ERROR(Errors.IsValidDesription)
|
|
@@ -97,7 +98,7 @@ export class Service {
|
|
|
97
98
|
ERROR(Errors.IsValidEndpoint)
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
let pay_token_type =
|
|
101
|
+
let pay_token_type = token_type;
|
|
101
102
|
let s = new Service(protocol, pay_token_type, permission);
|
|
102
103
|
let txb = protocol.CurrentSession();
|
|
103
104
|
let ep = endpoint? txb.pure(Bcs.getInstance().ser(ValueType.TYPE_OPTION_STRING, endpoint)) : OptionNone(txb);
|
|
@@ -112,7 +113,7 @@ export class Service {
|
|
|
112
113
|
} else {
|
|
113
114
|
s.object = txb.moveCall({
|
|
114
115
|
target:protocol.ServiceFn('new') as FnCallType,
|
|
115
|
-
arguments:[txb.pure(description
|
|
116
|
+
arguments:[txb.pure(description), txb.pure(payee_address, BCS.ADDRESS), ep, Protocol.TXB_OBJECT(txb, permission)],
|
|
116
117
|
typeArguments:[pay_token_type],
|
|
117
118
|
})
|
|
118
119
|
}
|
|
@@ -210,7 +211,7 @@ export class Service {
|
|
|
210
211
|
if (!IsValidName(item)) {
|
|
211
212
|
ERROR(Errors.IsValidName, 'item')
|
|
212
213
|
}
|
|
213
|
-
if (!
|
|
214
|
+
if (!IsValidUintLarge(stock_add)) {
|
|
214
215
|
ERROR(Errors.IsValidUint, 'stock_add')
|
|
215
216
|
}
|
|
216
217
|
|
|
@@ -235,7 +236,7 @@ export class Service {
|
|
|
235
236
|
if (!IsValidName(item)) {
|
|
236
237
|
ERROR(Errors.IsValidName, 'item')
|
|
237
238
|
}
|
|
238
|
-
if (!
|
|
239
|
+
if (!IsValidUintLarge(stock_reduce)) {
|
|
239
240
|
ERROR(Errors.IsValidUint, 'stock_reduce')
|
|
240
241
|
}
|
|
241
242
|
|
|
@@ -256,6 +257,32 @@ export class Service {
|
|
|
256
257
|
})
|
|
257
258
|
}
|
|
258
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
|
+
|
|
259
286
|
set_payee(payee:string, passport?:PassportObject) {
|
|
260
287
|
if (!IsValidAddress(payee)) {
|
|
261
288
|
ERROR(Errors.IsValidAddress, 'payee');
|
|
@@ -293,14 +320,13 @@ export class Service {
|
|
|
293
320
|
target:this.protocol.ServiceFn('repository_add') as FnCallType,
|
|
294
321
|
arguments:[Protocol.TXB_OBJECT(txb, this.object), Protocol.TXB_OBJECT(txb, repository), Protocol.TXB_OBJECT(txb, this.permission)],
|
|
295
322
|
typeArguments:[this.pay_token_type]
|
|
296
|
-
})
|
|
323
|
+
})
|
|
297
324
|
}
|
|
298
325
|
}
|
|
299
|
-
repository_remove(repository_address
|
|
300
|
-
if (!removeall &&
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
if (repository_address && !IsValidArray(repository_address, IsValidAddress)) {
|
|
326
|
+
repository_remove(repository_address:string[], removeall?:boolean, passport?:PassportObject) {
|
|
327
|
+
if (!removeall && repository_address.length===0) return;
|
|
328
|
+
|
|
329
|
+
if (!IsValidArray(repository_address, IsValidAddress)) {
|
|
304
330
|
ERROR(Errors.IsValidArray, 'repository_address');
|
|
305
331
|
}
|
|
306
332
|
|
|
@@ -339,6 +365,8 @@ export class Service {
|
|
|
339
365
|
}
|
|
340
366
|
|
|
341
367
|
add_withdraw_guards(guards:Service_Guard_Percent[], passport?:PassportObject) {
|
|
368
|
+
if (guards.length === 0) return;
|
|
369
|
+
|
|
342
370
|
let bValid = true;
|
|
343
371
|
guards.forEach((v) => {
|
|
344
372
|
if (!Protocol.IsValidObjects([v.guard])) bValid = false;
|
|
@@ -367,12 +395,12 @@ export class Service {
|
|
|
367
395
|
}
|
|
368
396
|
})
|
|
369
397
|
}
|
|
370
|
-
remove_withdraw_guards(guard_address
|
|
371
|
-
if (!removeall &&
|
|
372
|
-
|
|
398
|
+
remove_withdraw_guards(guard_address:string[], removeall?:boolean, passport?:PassportObject) {
|
|
399
|
+
if (!removeall && guard_address.length===0) {
|
|
400
|
+
return
|
|
373
401
|
}
|
|
374
402
|
|
|
375
|
-
if (
|
|
403
|
+
if (!IsValidArray(guard_address, IsValidAddress)) {
|
|
376
404
|
ERROR(Errors.IsValidArray, 'guard_address')
|
|
377
405
|
}
|
|
378
406
|
|
|
@@ -410,6 +438,8 @@ export class Service {
|
|
|
410
438
|
}
|
|
411
439
|
}
|
|
412
440
|
add_refund_guards(guards:Service_Guard_Percent[], passport?:PassportObject) {
|
|
441
|
+
if (guards.length === 0) return;
|
|
442
|
+
|
|
413
443
|
let bValid = true;
|
|
414
444
|
guards.forEach((v) => {
|
|
415
445
|
if (!Protocol.IsValidObjects([v.guard])) bValid = false;
|
|
@@ -438,11 +468,9 @@ export class Service {
|
|
|
438
468
|
}
|
|
439
469
|
})
|
|
440
470
|
}
|
|
441
|
-
remove_refund_guards(guard_address
|
|
442
|
-
if (
|
|
443
|
-
|
|
444
|
-
}
|
|
445
|
-
if (guard_address && !IsValidArray(guard_address, IsValidAddress)) {
|
|
471
|
+
remove_refund_guards(guard_address:string[], removeall?:boolean, passport?:PassportObject) {
|
|
472
|
+
if (guard_address.length===0 && !removeall) return ;
|
|
473
|
+
if (!IsValidArray(guard_address, IsValidAddress)) {
|
|
446
474
|
ERROR(Errors.InvalidParam, 'guard_address')
|
|
447
475
|
}
|
|
448
476
|
|
|
@@ -485,21 +513,26 @@ export class Service {
|
|
|
485
513
|
sales.forEach((v) => {
|
|
486
514
|
if (!IsValidName(v.item)) bValid = false;
|
|
487
515
|
if (!IsValidInt(v.price)) bValid = false;
|
|
488
|
-
if (!
|
|
516
|
+
if (!IsValidUintLarge(v.stock)) bValid = false;
|
|
489
517
|
if (names.includes(v.item)) bValid = false;
|
|
490
518
|
names.push(v.item)
|
|
491
519
|
})
|
|
492
520
|
return bValid
|
|
493
521
|
}
|
|
494
522
|
|
|
495
|
-
|
|
496
|
-
if (
|
|
497
|
-
|
|
523
|
+
add_sales(sales:Service_Sale[], bExistAssert:boolean=false, passport?:PassportObject) {
|
|
524
|
+
if (sales.length === 0) return;
|
|
525
|
+
|
|
526
|
+
if (!this.is_valid_sale(sales)) {
|
|
527
|
+
ERROR(Errors.InvalidParam, 'add_sales')
|
|
498
528
|
}
|
|
499
529
|
|
|
500
|
-
let names: string[] = []; let price: number[] = []; let stock: number[] = [];
|
|
530
|
+
let names: string[] = []; let price: number[] = []; let stock: number[] = []; let endpoint: string[] = [];
|
|
501
531
|
sales.forEach((s) => {
|
|
502
|
-
|
|
532
|
+
if (s.endpoint && !IsValidEndpoint(s.endpoint)) {
|
|
533
|
+
ERROR(Errors.IsValidEndpoint, 'add_sales')
|
|
534
|
+
}
|
|
535
|
+
names.push(s.item); price.push(s.price); stock.push(s.stock); endpoint.push(s.endpoint ?? '')
|
|
503
536
|
})
|
|
504
537
|
|
|
505
538
|
let txb = this.protocol.CurrentSession();
|
|
@@ -507,7 +540,8 @@ export class Service {
|
|
|
507
540
|
txb.moveCall({
|
|
508
541
|
target:this.protocol.ServiceFn('sales_add_with_passport') as FnCallType,
|
|
509
542
|
arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, names)),
|
|
510
|
-
txb.pure(Bcs.getInstance().ser(ValueType.
|
|
543
|
+
txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, endpoint)), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, price)),
|
|
544
|
+
txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, stock)), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_BOOL, bExistAssert)),
|
|
511
545
|
Protocol.TXB_OBJECT(txb, this.permission)],
|
|
512
546
|
typeArguments:[this.pay_token_type]
|
|
513
547
|
})
|
|
@@ -515,52 +549,36 @@ export class Service {
|
|
|
515
549
|
txb.moveCall({
|
|
516
550
|
target:this.protocol.ServiceFn('sales_add') as FnCallType,
|
|
517
551
|
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, names)),
|
|
552
|
+
txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, endpoint)),
|
|
518
553
|
txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, price)), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_U64, stock)),
|
|
554
|
+
txb.pure(Bcs.getInstance().ser(ValueType.TYPE_BOOL, bExistAssert)),
|
|
519
555
|
Protocol.TXB_OBJECT(txb, this.permission)],
|
|
520
556
|
typeArguments:[this.pay_token_type]
|
|
521
557
|
})
|
|
522
558
|
}
|
|
523
|
-
|
|
524
559
|
}
|
|
525
|
-
remove_sales(sales
|
|
526
|
-
if (
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
ERROR(Errors.IsValidArray, 'sales')
|
|
560
|
+
remove_sales(sales:string[], passport?:PassportObject) {
|
|
561
|
+
if (sales.length === 0) return;
|
|
562
|
+
|
|
563
|
+
if (!IsValidArray(sales, IsValidName)) {
|
|
564
|
+
ERROR(Errors.IsValidArray, 'remove_sales')
|
|
531
565
|
}
|
|
532
566
|
|
|
533
567
|
let txb = this.protocol.CurrentSession();
|
|
534
568
|
if (passport) {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
} else {
|
|
542
|
-
txb.moveCall({
|
|
543
|
-
target:this.protocol.ServiceFn('sales_remove_with_passport') as FnCallType,
|
|
544
|
-
arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(sales!))),
|
|
545
|
-
Protocol.TXB_OBJECT(txb, this.permission)],
|
|
546
|
-
typeArguments:[this.pay_token_type]
|
|
547
|
-
})
|
|
548
|
-
}
|
|
569
|
+
txb.moveCall({
|
|
570
|
+
target:this.protocol.ServiceFn('sales_remove_with_passport') as FnCallType,
|
|
571
|
+
arguments:[passport, Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(sales!))),
|
|
572
|
+
Protocol.TXB_OBJECT(txb, this.permission)],
|
|
573
|
+
typeArguments:[this.pay_token_type]
|
|
574
|
+
})
|
|
549
575
|
} else {
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
} else {
|
|
557
|
-
txb.moveCall({
|
|
558
|
-
target:this.protocol.ServiceFn('sales_remove') as FnCallType,
|
|
559
|
-
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(sales!))),
|
|
560
|
-
Protocol.TXB_OBJECT(txb, this.permission)],
|
|
561
|
-
typeArguments:[this.pay_token_type]
|
|
562
|
-
})
|
|
563
|
-
}
|
|
576
|
+
txb.moveCall({
|
|
577
|
+
target:this.protocol.ServiceFn('sales_remove') as FnCallType,
|
|
578
|
+
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(Bcs.getInstance().ser(ValueType.TYPE_VEC_STRING, array_unique(sales!))),
|
|
579
|
+
Protocol.TXB_OBJECT(txb, this.permission)],
|
|
580
|
+
typeArguments:[this.pay_token_type]
|
|
581
|
+
})
|
|
564
582
|
}
|
|
565
583
|
}
|
|
566
584
|
|
|
@@ -572,11 +590,11 @@ export class Service {
|
|
|
572
590
|
let bValid = true;
|
|
573
591
|
discount_dispatch.forEach((v) => {
|
|
574
592
|
if (!IsValidAddress(v.receiver)) bValid = false;
|
|
575
|
-
if (!
|
|
593
|
+
if (!IsValidUintLarge(v.count) || v.count > Service.MAX_DISCOUNT_COUNT_ONCE) bValid = false;
|
|
576
594
|
if (!IsValidName_AllowEmpty(v.discount.name)) bValid = false;
|
|
577
595
|
if (v.discount.type == Service_Discount_Type.ratio && !IsValidPercent(v.discount.off)) bValid = false;
|
|
578
|
-
if (!
|
|
579
|
-
if (v.discount?.time_start && !
|
|
596
|
+
if (!IsValidUintLarge(v.discount.duration_minutes)) bValid = false;
|
|
597
|
+
if (v.discount?.time_start && !IsValidUintLarge(v.discount.time_start)) bValid = false;
|
|
580
598
|
if (v.discount?.price_greater && !IsValidInt(v.discount.price_greater)) bValid = false;
|
|
581
599
|
})
|
|
582
600
|
if (!bValid) {
|
|
@@ -766,8 +784,6 @@ export class Service {
|
|
|
766
784
|
}
|
|
767
785
|
|
|
768
786
|
let req = array_unique(customer_required) as string[];
|
|
769
|
-
console.log(req)
|
|
770
|
-
console.log(this.pay_token_type)
|
|
771
787
|
let txb = this.protocol.CurrentSession();
|
|
772
788
|
if (passport) {
|
|
773
789
|
txb.moveCall({
|
|
@@ -918,12 +934,12 @@ export class Service {
|
|
|
918
934
|
buy_items.forEach((v) => {
|
|
919
935
|
if (!IsValidName(v.item)) bValid = false;
|
|
920
936
|
if (!IsValidInt(v.max_price)) bValid = false;
|
|
921
|
-
if (!
|
|
937
|
+
if (!IsValidUintLarge(v.count)) bValid = false;
|
|
922
938
|
if (names.includes(v.item)) bValid = false;
|
|
923
939
|
names.push(v.item)
|
|
924
940
|
})
|
|
925
941
|
if (!bValid) {
|
|
926
|
-
ERROR(Errors.InvalidParam, 'buy_items')
|
|
942
|
+
ERROR(Errors.InvalidParam, 'buy_items 2')
|
|
927
943
|
}
|
|
928
944
|
|
|
929
945
|
let name:string[] = []; let price:number[] = []; let stock:number[] = []; let order;
|
|
@@ -1018,4 +1034,23 @@ export class Service {
|
|
|
1018
1034
|
static MAX_DISCOUNT_RECEIVER_COUNT = 20;
|
|
1019
1035
|
static MAX_GUARD_COUNT = 16;
|
|
1020
1036
|
static MAX_REPOSITORY_COUNT = 16;
|
|
1037
|
+
|
|
1038
|
+
static parseObjectType = (chain_type:string | undefined | null) : string => {
|
|
1039
|
+
if (chain_type) {
|
|
1040
|
+
const s = 'service::Service<'
|
|
1041
|
+
const i = chain_type.indexOf(s);
|
|
1042
|
+
if (i > 0) {
|
|
1043
|
+
return chain_type.slice(i + s.length, chain_type.length-1);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
return '';
|
|
1047
|
+
}
|
|
1048
|
+
static endpoint = (service_endpoint:string, item_endpoint:string, item_name:string) => {
|
|
1049
|
+
if (item_endpoint) {
|
|
1050
|
+
return item_endpoint
|
|
1051
|
+
} else if (service_endpoint) {
|
|
1052
|
+
return service_endpoint + '/sales/' + encodeURI(item_name);
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1021
1056
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,11 +1,37 @@
|
|
|
1
|
-
import { bcs, BCS, toHEX, fromHEX, getSuiMoveConfig, TypeName,
|
|
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
|
+
import { SuiObjectResponse, DynamicFieldPage } from '@mysten/sui.js/client';
|
|
3
4
|
import { ERROR, Errors } from './exception';
|
|
4
5
|
import { isValidSuiAddress, isValidSuiObjectId } from '@mysten/sui.js/utils'
|
|
5
|
-
import { ValueType } from './protocol'
|
|
6
|
+
import { RepositoryValueType, ValueType, Protocol } from './protocol'
|
|
7
|
+
|
|
8
|
+
export const MAX_U8 = BigInt('255');
|
|
9
|
+
export const MAX_U64 = BigInt('18446744073709551615');
|
|
10
|
+
export const MAX_U128 = BigInt('340282366920938463463374607431768211455');
|
|
11
|
+
export const MAX_U256 = BigInt('115792089237316195423570985008687907853269984665640564039457584007913129639935');
|
|
6
12
|
|
|
7
13
|
export const OPTION_NONE = 0;
|
|
8
|
-
|
|
14
|
+
|
|
15
|
+
export const ValueTypeConvert = (type:ValueType | null | undefined) : RepositoryValueType | number => {
|
|
16
|
+
if (type === ValueType.TYPE_U8 || type === ValueType.TYPE_U64 || type === ValueType.TYPE_U128 ||
|
|
17
|
+
type === ValueType.TYPE_U256 || type === ValueType.TYPE_BOOL) {
|
|
18
|
+
return RepositoryValueType.PositiveNumber
|
|
19
|
+
} else if (type === ValueType.TYPE_VEC_U8 || type === ValueType.TYPE_VEC_U64 || type === ValueType.TYPE_VEC_U128 ||
|
|
20
|
+
type === ValueType.TYPE_VEC_U256|| type === ValueType.TYPE_VEC_BOOL) {
|
|
21
|
+
return RepositoryValueType.PositiveNumber_Vec
|
|
22
|
+
} else if (type === ValueType.TYPE_ADDRESS) {
|
|
23
|
+
return RepositoryValueType.Address
|
|
24
|
+
} else if (type === ValueType.TYPE_VEC_ADDRESS) {
|
|
25
|
+
return RepositoryValueType.Address_Vec
|
|
26
|
+
} else if (type === ValueType.TYPE_STRING) {
|
|
27
|
+
return RepositoryValueType.String
|
|
28
|
+
} else if (type === ValueType.TYPE_VEC_STRING) {
|
|
29
|
+
return RepositoryValueType.String_Vec
|
|
30
|
+
}
|
|
31
|
+
return -1;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const readOption = (arr: number[], de:ValueType) : {bNone:boolean, value:any} => {
|
|
9
35
|
let o = arr.splice(0, 1);
|
|
10
36
|
if (o[0] == 1) { // true
|
|
11
37
|
return {bNone:false, value:Bcs.getInstance().de(de, Uint8Array.from(arr))};
|
|
@@ -133,6 +159,20 @@ export class Bcs {
|
|
|
133
159
|
'none': null,
|
|
134
160
|
'some': 'T',
|
|
135
161
|
});
|
|
162
|
+
this.bcs.registerStructType('EntStruct', {
|
|
163
|
+
'avatar': 'vector<u8>',
|
|
164
|
+
'resource': "Option<address>",
|
|
165
|
+
'like': BCS.U32,
|
|
166
|
+
'dislike': BCS.U32,
|
|
167
|
+
})
|
|
168
|
+
this.bcs.registerStructType('PersonalInfo', {
|
|
169
|
+
'name': 'vector<u8>',
|
|
170
|
+
'description': 'vector<u8>',
|
|
171
|
+
'avatar': BCS.STRING,
|
|
172
|
+
'twitter': BCS.STRING,
|
|
173
|
+
'discord': BCS.STRING,
|
|
174
|
+
'homepage': BCS.STRING,
|
|
175
|
+
})
|
|
136
176
|
}
|
|
137
177
|
static getInstance() : Bcs {
|
|
138
178
|
if (!Bcs._instance) {
|
|
@@ -140,7 +180,16 @@ export class Bcs {
|
|
|
140
180
|
};
|
|
141
181
|
return Bcs._instance;
|
|
142
182
|
}
|
|
143
|
-
|
|
183
|
+
|
|
184
|
+
ser_option_u32(data:Uint8Array | any) : Uint8Array {
|
|
185
|
+
return this.bcs.ser('Option<u32>', {'some': data}).toBytes();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
ser(type:ValueType | string, data:Uint8Array | any) : Uint8Array {
|
|
189
|
+
if (typeof(type) === 'string') {
|
|
190
|
+
return this.bcs.ser(type, data).toBytes();
|
|
191
|
+
}
|
|
192
|
+
|
|
144
193
|
switch(type) {
|
|
145
194
|
case ValueType.TYPE_BOOL:
|
|
146
195
|
return this.bcs.ser(BCS.BOOL, data).toBytes();
|
|
@@ -201,6 +250,7 @@ export class Bcs {
|
|
|
201
250
|
case ValueType.TYPE_U64:
|
|
202
251
|
return this.bcs.de(BCS.U64, data);
|
|
203
252
|
case ValueType.TYPE_U8:
|
|
253
|
+
console.log(data)
|
|
204
254
|
return this.bcs.de(BCS.U8, data);
|
|
205
255
|
case ValueType.TYPE_VEC_U8:
|
|
206
256
|
return this.bcs.de('vector<u8>', data);
|
|
@@ -242,6 +292,30 @@ export class Bcs {
|
|
|
242
292
|
ERROR(Errors.bcsTypeInvalid, 'de');
|
|
243
293
|
}
|
|
244
294
|
}
|
|
295
|
+
|
|
296
|
+
de_ent(data:Uint8Array) : any {
|
|
297
|
+
const struct_vec = this.bcs.de('vector<u8>', data);
|
|
298
|
+
return this.bcs.de('EntStruct', Uint8Array.from(struct_vec));
|
|
299
|
+
/* const reader = new BcsReader(data);
|
|
300
|
+
const total_len = reader.readULEB();
|
|
301
|
+
console.log(avatar_len)
|
|
302
|
+
const avatar = reader.readBytes(avatar_len);
|
|
303
|
+
console.log(avatar)
|
|
304
|
+
const option_resource = reader.read8();
|
|
305
|
+
var resource = '';
|
|
306
|
+
if (option_resource != 0) {
|
|
307
|
+
resource = reader.read256();
|
|
308
|
+
}
|
|
309
|
+
const like = reader.read32();
|
|
310
|
+
const dislike = reader.read32();
|
|
311
|
+
return {avatar:avatar, resource:resource, like:like, dislike:dislike}*/
|
|
312
|
+
}
|
|
313
|
+
de_entInfo(data:Uint8Array) : any {
|
|
314
|
+
let r = this.bcs.de('PersonalInfo', data);
|
|
315
|
+
r.name = new TextDecoder().decode(Uint8Array.from(r.name));
|
|
316
|
+
r.description = new TextDecoder().decode(Uint8Array.from(r.description));
|
|
317
|
+
return r
|
|
318
|
+
}
|
|
245
319
|
}
|
|
246
320
|
|
|
247
321
|
export function stringToUint8Array(str:string) : Uint8Array {
|
|
@@ -264,7 +338,6 @@ export function numToUint8Array(num:number) : Uint8Array {
|
|
|
264
338
|
return new Uint8Array(a)
|
|
265
339
|
}
|
|
266
340
|
|
|
267
|
-
// 判断是否为数组
|
|
268
341
|
export const isArr = (origin: any): boolean => {
|
|
269
342
|
let str = '[object Array]'
|
|
270
343
|
return Object.prototype.toString.call(origin) == str ? true : false
|
|
@@ -304,7 +377,16 @@ export const IsValidAddress = (addr:string) : boolean => {
|
|
|
304
377
|
}
|
|
305
378
|
return true
|
|
306
379
|
}
|
|
307
|
-
export const
|
|
380
|
+
export const IsValidUintLarge = (value:string | number) : boolean => {
|
|
381
|
+
try {
|
|
382
|
+
const v = BigInt(value);
|
|
383
|
+
if (v <= MAX_U256) {
|
|
384
|
+
return true
|
|
385
|
+
}
|
|
386
|
+
} catch (e) {
|
|
387
|
+
}; return false
|
|
388
|
+
}
|
|
389
|
+
export const IsValidTokenType = (argType: string) : boolean => {
|
|
308
390
|
if (!argType || argType.length === 0) {
|
|
309
391
|
return false;
|
|
310
392
|
}
|
|
@@ -312,17 +394,20 @@ export const IsValidArgType = (argType: string) : boolean => {
|
|
|
312
394
|
if (arr.length !== 3) {
|
|
313
395
|
return false;
|
|
314
396
|
}
|
|
315
|
-
if (!IsValidAddress(arr[0]) || arr[1].length === 0 || arr[2].length === 0) {
|
|
397
|
+
if ((!IsValidAddress(arr[0]) && arr[0] != '0x2') || arr[1].length === 0 || arr[2].length === 0) {
|
|
316
398
|
return false;
|
|
317
399
|
}
|
|
318
400
|
return true;
|
|
319
401
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
value = parseInt(value as string);
|
|
402
|
+
export const IsValidArgType = (argType: string) : boolean => {
|
|
403
|
+
if (!argType || argType.length === 0) {
|
|
404
|
+
return false;
|
|
324
405
|
}
|
|
325
|
-
|
|
406
|
+
let arr = argType.split('::');
|
|
407
|
+
if (arr.length < 3) {
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
return true;
|
|
326
411
|
}
|
|
327
412
|
export const IsValidInt = (value: number | string) : boolean => {
|
|
328
413
|
if (typeof(value) === 'string') {
|
|
@@ -337,13 +422,37 @@ export const IsValidPercent = (value: number | string) : boolean => {
|
|
|
337
422
|
return Number.isSafeInteger(value) && value > 0 && value <= 100
|
|
338
423
|
}
|
|
339
424
|
export const IsValidArray = (arr: any[], validFunc:any) : boolean => {
|
|
340
|
-
let
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
bValid = false;
|
|
425
|
+
for (let i = 0; i < arr.length; ++i) {
|
|
426
|
+
if (!validFunc(arr[i])) {
|
|
427
|
+
return false
|
|
344
428
|
}
|
|
345
|
-
}
|
|
346
|
-
return
|
|
429
|
+
}
|
|
430
|
+
return true
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
export const ResolveU64 = (value:bigint) : bigint => {
|
|
434
|
+
const max = MAX_U64;
|
|
435
|
+
if (value > max) {
|
|
436
|
+
return max;
|
|
437
|
+
} else {
|
|
438
|
+
return value
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export const ResolveBalance = (balance:string, decimals:number) : string => {
|
|
443
|
+
if (!balance) return ''
|
|
444
|
+
if (balance === '0') return '0'
|
|
445
|
+
if (decimals <= 0) return balance;
|
|
446
|
+
var pos = decimals - balance.length;
|
|
447
|
+
if (pos === 0) {
|
|
448
|
+
return '.' + balance;
|
|
449
|
+
} else if (pos < 0) {
|
|
450
|
+
let start = balance.slice(0, Math.abs(pos));
|
|
451
|
+
let end = balance.slice(Math.abs(pos));
|
|
452
|
+
return start + '.' + end;
|
|
453
|
+
} else {
|
|
454
|
+
return '.' + balance.padStart(decimals, '0');
|
|
455
|
+
}
|
|
347
456
|
}
|
|
348
457
|
|
|
349
458
|
export const OptionNone = (txb:TransactionBlock) : TransactionArgument => { return txb.pure([], BCS.U8) };
|
|
@@ -355,9 +464,9 @@ export type ArgType = {
|
|
|
355
464
|
}
|
|
356
465
|
export const ParseType = (type:string) : ArgType => {
|
|
357
466
|
if (type) {
|
|
358
|
-
const COIN = '
|
|
467
|
+
const COIN = '0x2::coin::Coin<';
|
|
359
468
|
let i = type.indexOf(COIN);
|
|
360
|
-
if (i
|
|
469
|
+
if (i >= 0) {
|
|
361
470
|
let coin = type.slice(i+COIN.length, type.length-1);
|
|
362
471
|
if (coin.indexOf('<') === -1) {
|
|
363
472
|
while (coin[coin.length-1] == '>') {
|
|
@@ -398,5 +507,48 @@ export function isValidHttpUrl(url:string) : boolean {
|
|
|
398
507
|
return false;
|
|
399
508
|
}
|
|
400
509
|
|
|
401
|
-
return r.protocol === "http:" || r.protocol === "https:";
|
|
402
|
-
}
|
|
510
|
+
return r.protocol === "http:" || r.protocol === "https:" || r.protocol === 'ipfs:';
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
export interface query_object_param {
|
|
514
|
+
id:string;
|
|
515
|
+
onBegin?:(id:string)=>void;
|
|
516
|
+
onObjectRes?:(id:string, res:SuiObjectResponse)=>void;
|
|
517
|
+
onDynamicRes?:(id:string, res:DynamicFieldPage)=>void;
|
|
518
|
+
onFieldsRes?:(id:string, fields_res:SuiObjectResponse[])=>void;
|
|
519
|
+
onObjectErr?:(id:string, err:any)=>void;
|
|
520
|
+
onDynamicErr?:(id:string, err:any)=>void;
|
|
521
|
+
onFieldsErr?:(id:string, err:any)=>void;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
export const query_object = (param:query_object_param) => {
|
|
525
|
+
if (param.id) {
|
|
526
|
+
if(param?.onBegin) param.onBegin(param.id);
|
|
527
|
+
Protocol.Client().getObject({id:param.id, options:{showContent:true, showType:true}}).then((res) => {
|
|
528
|
+
if (res.error) {
|
|
529
|
+
if(param?.onObjectErr) param.onObjectErr(param.id, res.error);
|
|
530
|
+
} else {
|
|
531
|
+
if(param?.onObjectRes) param.onObjectRes(param.id, res);
|
|
532
|
+
}
|
|
533
|
+
}).catch((err) => {
|
|
534
|
+
console.log(err)
|
|
535
|
+
if (param?.onObjectErr) param.onObjectErr(param.id, err);
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
Protocol.Client().getDynamicFields({parentId:param.id}).then((res) => {
|
|
539
|
+
if (param?.onDynamicRes) param.onDynamicRes(param.id, res);
|
|
540
|
+
|
|
541
|
+
if (res.data.length > 0) {
|
|
542
|
+
Protocol.Client().multiGetObjects({ids:res.data.map(v => v.objectId), options:{showContent:true}}).then((fields) => {
|
|
543
|
+
if (param?.onFieldsRes) param.onFieldsRes(param.id, fields);
|
|
544
|
+
}).catch((err) => {
|
|
545
|
+
console.log(err)
|
|
546
|
+
if (param?.onFieldsErr) param.onFieldsErr(param.id, err);
|
|
547
|
+
})
|
|
548
|
+
}
|
|
549
|
+
}).catch((err) => {
|
|
550
|
+
console.log(err)
|
|
551
|
+
if (param?.onDynamicErr) param.onDynamicErr(param.id, err);
|
|
552
|
+
})
|
|
553
|
+
}
|
|
554
|
+
}
|