wowok 1.2.7 → 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/package.json +1 -1
- package/src/demand.ts +5 -5
- package/src/entity.ts +8 -0
- package/src/guard.ts +1 -1
- package/src/machine.ts +205 -53
- package/src/permission.ts +91 -119
- package/src/progress.ts +101 -43
- package/src/protocol.ts +9 -10
- package/src/repository.ts +107 -20
- package/src/resource.ts +19 -9
- package/src/reward.ts +9 -11
- package/src/service.ts +35 -33
- package/src/utils.ts +67 -41
- package/src/vote.ts +18 -21
- package/src/wowok.ts +2 -2
package/src/repository.ts
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import { BCS } from '@mysten/bcs';
|
|
2
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, IsValidName, } from './utils';
|
|
4
|
+
import { Bcs, array_unique, IsValidDesription, IsValidAddress, IsValidArray, OptionNone, IsValidName, ValueTypeConvert} from './utils';
|
|
5
5
|
import { ERROR, Errors } from './exception';
|
|
6
|
-
import {
|
|
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
12
|
|
|
13
|
+
export interface RepData {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
dataType: RepositoryValueType;
|
|
17
|
+
data: string | string[];
|
|
18
|
+
object: string;
|
|
19
|
+
}
|
|
13
20
|
|
|
14
21
|
export type Repository_Policy = {
|
|
15
22
|
key:string;
|
|
@@ -143,14 +150,11 @@ export class Repository {
|
|
|
143
150
|
})
|
|
144
151
|
}
|
|
145
152
|
add_reference(references:string[], passport?:PassportObject) {
|
|
146
|
-
if (
|
|
147
|
-
ERROR(Errors.InvalidParam, 'add_reference')
|
|
148
|
-
}
|
|
149
|
-
|
|
153
|
+
if (references.length === 0) return;
|
|
150
154
|
if (!IsValidArray(references, IsValidAddress)) {
|
|
151
155
|
ERROR(Errors.IsValidArray, 'add_reference')
|
|
152
156
|
}
|
|
153
|
-
|
|
157
|
+
|
|
154
158
|
let txb = this.protocol.CurrentSession();
|
|
155
159
|
if (passport) {
|
|
156
160
|
txb.moveCall({
|
|
@@ -169,11 +173,9 @@ export class Repository {
|
|
|
169
173
|
}
|
|
170
174
|
}
|
|
171
175
|
remove_reference(references:string[], removeall?:boolean, passport?:PassportObject) {
|
|
172
|
-
if (
|
|
173
|
-
ERROR(Errors.InvalidParam, 'remove_reference')
|
|
174
|
-
}
|
|
176
|
+
if (references.length === 0 && !removeall) return
|
|
175
177
|
|
|
176
|
-
if (
|
|
178
|
+
if (!IsValidArray(references, IsValidAddress)) {
|
|
177
179
|
ERROR(Errors.IsValidArray, 'remove_reference')
|
|
178
180
|
}
|
|
179
181
|
|
|
@@ -212,9 +214,7 @@ export class Repository {
|
|
|
212
214
|
}
|
|
213
215
|
// add or modify the old
|
|
214
216
|
add_policies(policies:Repository_Policy[], passport?:PassportObject) {
|
|
215
|
-
if (
|
|
216
|
-
ERROR(Errors.InvalidParam, 'policies')
|
|
217
|
-
}
|
|
217
|
+
if (policies.length === 0) return;
|
|
218
218
|
|
|
219
219
|
let bValid = true;
|
|
220
220
|
policies.forEach((p) => {
|
|
@@ -252,10 +252,8 @@ export class Repository {
|
|
|
252
252
|
}
|
|
253
253
|
|
|
254
254
|
remove_policies(policy_keys:string[], passport?:PassportObject) {
|
|
255
|
-
if (
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
if (policy_keys && !IsValidArray(policy_keys, Repository.IsValidName)){
|
|
255
|
+
if (policy_keys.length === 0) return ;
|
|
256
|
+
if (!IsValidArray(policy_keys, Repository.IsValidName)){
|
|
259
257
|
ERROR(Errors.InvalidParam, 'policy_keys')
|
|
260
258
|
}
|
|
261
259
|
|
|
@@ -286,14 +284,14 @@ export class Repository {
|
|
|
286
284
|
txb.moveCall({
|
|
287
285
|
target:this.protocol.RepositoryFn('policy_rename_with_passport') as FnCallType,
|
|
288
286
|
arguments:[passport, Protocol.TXB_OBJECT(txb, this.object),
|
|
289
|
-
txb.pure(policy_key
|
|
287
|
+
txb.pure(policy_key), txb.pure(new_policy_key),
|
|
290
288
|
Protocol.TXB_OBJECT(txb, this.permission)]
|
|
291
289
|
})
|
|
292
290
|
} else {
|
|
293
291
|
txb.moveCall({
|
|
294
292
|
target:this.protocol.RepositoryFn('policy_rename') as FnCallType,
|
|
295
293
|
arguments:[Protocol.TXB_OBJECT(txb, this.object),
|
|
296
|
-
txb.pure(policy_key
|
|
294
|
+
txb.pure(policy_key), txb.pure(new_policy_key),
|
|
297
295
|
Protocol.TXB_OBJECT(txb, this.permission)]
|
|
298
296
|
})
|
|
299
297
|
}
|
|
@@ -421,5 +419,94 @@ export class Repository {
|
|
|
421
419
|
}
|
|
422
420
|
return '';
|
|
423
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
|
+
}
|
|
424
511
|
}
|
|
425
512
|
|
package/src/resource.ts
CHANGED
|
@@ -36,15 +36,22 @@ export class Resource {
|
|
|
36
36
|
return r
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
launch() {
|
|
40
|
+
if (!this.object) ERROR(Errors.Fail, 'launch object Invalid');
|
|
41
|
+
let txb = this.protocol.CurrentSession();
|
|
42
|
+
txb.moveCall({
|
|
43
|
+
target:this.protocol.ResourceFn('create') as FnCallType,
|
|
44
|
+
arguments:[Protocol.TXB_OBJECT(txb, this.object)]
|
|
45
|
+
});
|
|
46
|
+
}
|
|
39
47
|
add(name:string, object:string[]) {
|
|
40
48
|
if (!IsValidName(name)) ERROR(Errors.IsValidName, 'add');
|
|
41
|
-
if (!object) ERROR(Errors.InvalidParam, 'add')
|
|
42
49
|
if (!IsValidArray(object, IsValidAddress)) ERROR(Errors.IsValidArray, 'add');
|
|
43
50
|
|
|
44
51
|
let txb = this.protocol.CurrentSession();
|
|
45
52
|
txb.moveCall({
|
|
46
53
|
target:this.protocol.ResourceFn('add') as FnCallType,
|
|
47
|
-
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(name
|
|
54
|
+
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(name), txb.pure(object, 'vector<address>')]
|
|
48
55
|
});
|
|
49
56
|
}
|
|
50
57
|
|
|
@@ -60,22 +67,22 @@ export class Resource {
|
|
|
60
67
|
});
|
|
61
68
|
}
|
|
62
69
|
|
|
63
|
-
remove(name:string, object
|
|
70
|
+
remove(name:string, object:string[], removeall?:boolean) {
|
|
64
71
|
if (!IsValidName(name)) ERROR(Errors.IsValidName, 'Resource: remove');
|
|
65
|
-
if (
|
|
72
|
+
if (object.length===0 && !removeall) return;
|
|
66
73
|
|
|
67
74
|
let txb = this.protocol.CurrentSession();
|
|
68
75
|
if (removeall) {
|
|
69
76
|
txb.moveCall({
|
|
70
77
|
target:this.protocol.ResourceFn('remove_all') as FnCallType,
|
|
71
|
-
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(name
|
|
78
|
+
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(name)]
|
|
72
79
|
});
|
|
73
80
|
} else if(object) {
|
|
74
81
|
if (!IsValidArray(object, IsValidAddress)) ERROR(Errors.IsValidArray, 'Resource: remove');
|
|
75
82
|
|
|
76
83
|
txb.moveCall({
|
|
77
84
|
target:this.protocol.ResourceFn('remove') as FnCallType,
|
|
78
|
-
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(name
|
|
85
|
+
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(name), txb.pure(object, 'vector<address>')]
|
|
79
86
|
});
|
|
80
87
|
}
|
|
81
88
|
}
|
|
@@ -98,7 +105,7 @@ export class Resource {
|
|
|
98
105
|
let txb = this.protocol.CurrentSession();
|
|
99
106
|
txb.moveCall({
|
|
100
107
|
target:this.protocol.ResourceFn('rename') as FnCallType,
|
|
101
|
-
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(old_name
|
|
108
|
+
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(old_name), txb.pure(new_name)]
|
|
102
109
|
});
|
|
103
110
|
}
|
|
104
111
|
|
|
@@ -109,10 +116,13 @@ export class Resource {
|
|
|
109
116
|
if (!IsValidArray(tags, IsValidName)) ERROR(Errors.IsValidArray, 'add_tags');
|
|
110
117
|
if (tags.length > Resource.MAX_TAGS) ERROR(Errors.InvalidParam, 'add_tags');
|
|
111
118
|
|
|
112
|
-
|
|
119
|
+
const txb = this.protocol.CurrentSession();
|
|
120
|
+
const encode = new TextEncoder();
|
|
121
|
+
|
|
113
122
|
txb.moveCall({
|
|
114
123
|
target:this.protocol.ResourceFn('tags_add') as FnCallType,
|
|
115
|
-
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(object, BCS.ADDRESS),
|
|
124
|
+
arguments:[Protocol.TXB_OBJECT(txb, this.object), txb.pure(object, BCS.ADDRESS),
|
|
125
|
+
txb.pure(nick),
|
|
116
126
|
txb.pure(tags, 'vector<string>')
|
|
117
127
|
]
|
|
118
128
|
});
|
package/src/reward.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TransactionArgument, TransactionBlock, type TransactionResult } from '@mysten/sui.js/transactions';
|
|
2
2
|
import { BCS} from '@mysten/bcs';
|
|
3
3
|
import { FnCallType, GuardObject, PassportObject, PermissionObject, RewardAddress, Protocol, TxbObject, } from './protocol';
|
|
4
|
-
import { array_unique, IsValidAddress, IsValidArgType, IsValidArray, IsValidDesription,
|
|
4
|
+
import { array_unique, IsValidAddress, IsValidArgType, IsValidArray, IsValidDesription, IsValidUintLarge, } from './utils';
|
|
5
5
|
import { ERROR, Errors } from './exception';
|
|
6
6
|
import { Resource } from './resource';
|
|
7
7
|
|
|
@@ -41,7 +41,7 @@ export class Reward {
|
|
|
41
41
|
if (!IsValidDesription(description)) {
|
|
42
42
|
ERROR(Errors.IsValidDesription)
|
|
43
43
|
}
|
|
44
|
-
if (!
|
|
44
|
+
if (!IsValidUintLarge(time)) {
|
|
45
45
|
ERROR(Errors.IsValidUint, 'time')
|
|
46
46
|
}
|
|
47
47
|
|
|
@@ -101,7 +101,7 @@ export class Reward {
|
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
expand_time(ms_expand:boolean, time:number, passport?:PassportObject) {
|
|
104
|
-
if (!
|
|
104
|
+
if (!IsValidUintLarge(time)) {
|
|
105
105
|
ERROR(Errors.IsValidUint, 'minutes_expand')
|
|
106
106
|
}
|
|
107
107
|
|
|
@@ -125,13 +125,11 @@ export class Reward {
|
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
add_guard(gurads:RewardGuardPortions[], passport?:PassportObject) {
|
|
128
|
-
if (
|
|
129
|
-
ERROR(Errors.InvalidParam, 'gurads')
|
|
130
|
-
}
|
|
128
|
+
if (gurads.length === 0) return;
|
|
131
129
|
|
|
132
130
|
let bValid = true;
|
|
133
131
|
gurads.forEach((v) => {
|
|
134
|
-
if (!
|
|
132
|
+
if (!IsValidUintLarge(v.portions) || v.portions > Reward.MAX_PORTIONS_COUNT) bValid = false;
|
|
135
133
|
if (!Protocol.IsValidObjects([v.guard])) bValid = false;
|
|
136
134
|
})
|
|
137
135
|
if (!bValid) {
|
|
@@ -162,11 +160,11 @@ export class Reward {
|
|
|
162
160
|
}
|
|
163
161
|
|
|
164
162
|
remove_guard(guards:string[], removeall?:boolean, passport?:PassportObject) {
|
|
165
|
-
if (!removeall &&
|
|
166
|
-
|
|
163
|
+
if (!removeall && guards.length===0) {
|
|
164
|
+
return
|
|
167
165
|
}
|
|
168
166
|
|
|
169
|
-
if (
|
|
167
|
+
if (!IsValidArray(guards, IsValidAddress)) {
|
|
170
168
|
ERROR(Errors.IsValidArray, 'guards')
|
|
171
169
|
}
|
|
172
170
|
|
|
@@ -333,6 +331,6 @@ export class Reward {
|
|
|
333
331
|
}
|
|
334
332
|
return '';
|
|
335
333
|
}
|
|
336
|
-
static MAX_PORTIONS_COUNT =
|
|
334
|
+
static MAX_PORTIONS_COUNT = 600;
|
|
337
335
|
static MAX_GUARD_COUNT = 16;
|
|
338
336
|
}
|
package/src/service.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { bcs, BCS, toHEX, fromHEX, getSuiMoveConfig } from '@mysten/bcs';
|
|
2
2
|
import { IsValidArray, IsValidPercent, IsValidName_AllowEmpty, Bcs, array_unique, IsValidTokenType, IsValidDesription,
|
|
3
|
-
IsValidAddress, IsValidEndpoint, OptionNone,
|
|
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';
|
|
@@ -113,7 +113,7 @@ export class Service {
|
|
|
113
113
|
} else {
|
|
114
114
|
s.object = txb.moveCall({
|
|
115
115
|
target:protocol.ServiceFn('new') as FnCallType,
|
|
116
|
-
arguments:[txb.pure(description
|
|
116
|
+
arguments:[txb.pure(description), txb.pure(payee_address, BCS.ADDRESS), ep, Protocol.TXB_OBJECT(txb, permission)],
|
|
117
117
|
typeArguments:[pay_token_type],
|
|
118
118
|
})
|
|
119
119
|
}
|
|
@@ -211,7 +211,7 @@ export class Service {
|
|
|
211
211
|
if (!IsValidName(item)) {
|
|
212
212
|
ERROR(Errors.IsValidName, 'item')
|
|
213
213
|
}
|
|
214
|
-
if (!
|
|
214
|
+
if (!IsValidUintLarge(stock_add)) {
|
|
215
215
|
ERROR(Errors.IsValidUint, 'stock_add')
|
|
216
216
|
}
|
|
217
217
|
|
|
@@ -236,7 +236,7 @@ export class Service {
|
|
|
236
236
|
if (!IsValidName(item)) {
|
|
237
237
|
ERROR(Errors.IsValidName, 'item')
|
|
238
238
|
}
|
|
239
|
-
if (!
|
|
239
|
+
if (!IsValidUintLarge(stock_reduce)) {
|
|
240
240
|
ERROR(Errors.IsValidUint, 'stock_reduce')
|
|
241
241
|
}
|
|
242
242
|
|
|
@@ -323,11 +323,10 @@ export class Service {
|
|
|
323
323
|
})
|
|
324
324
|
}
|
|
325
325
|
}
|
|
326
|
-
repository_remove(repository_address
|
|
327
|
-
if (!removeall &&
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
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)) {
|
|
331
330
|
ERROR(Errors.IsValidArray, 'repository_address');
|
|
332
331
|
}
|
|
333
332
|
|
|
@@ -366,6 +365,8 @@ export class Service {
|
|
|
366
365
|
}
|
|
367
366
|
|
|
368
367
|
add_withdraw_guards(guards:Service_Guard_Percent[], passport?:PassportObject) {
|
|
368
|
+
if (guards.length === 0) return;
|
|
369
|
+
|
|
369
370
|
let bValid = true;
|
|
370
371
|
guards.forEach((v) => {
|
|
371
372
|
if (!Protocol.IsValidObjects([v.guard])) bValid = false;
|
|
@@ -394,12 +395,12 @@ export class Service {
|
|
|
394
395
|
}
|
|
395
396
|
})
|
|
396
397
|
}
|
|
397
|
-
remove_withdraw_guards(guard_address
|
|
398
|
-
if (!removeall &&
|
|
399
|
-
|
|
398
|
+
remove_withdraw_guards(guard_address:string[], removeall?:boolean, passport?:PassportObject) {
|
|
399
|
+
if (!removeall && guard_address.length===0) {
|
|
400
|
+
return
|
|
400
401
|
}
|
|
401
402
|
|
|
402
|
-
if (
|
|
403
|
+
if (!IsValidArray(guard_address, IsValidAddress)) {
|
|
403
404
|
ERROR(Errors.IsValidArray, 'guard_address')
|
|
404
405
|
}
|
|
405
406
|
|
|
@@ -437,6 +438,8 @@ export class Service {
|
|
|
437
438
|
}
|
|
438
439
|
}
|
|
439
440
|
add_refund_guards(guards:Service_Guard_Percent[], passport?:PassportObject) {
|
|
441
|
+
if (guards.length === 0) return;
|
|
442
|
+
|
|
440
443
|
let bValid = true;
|
|
441
444
|
guards.forEach((v) => {
|
|
442
445
|
if (!Protocol.IsValidObjects([v.guard])) bValid = false;
|
|
@@ -465,11 +468,9 @@ export class Service {
|
|
|
465
468
|
}
|
|
466
469
|
})
|
|
467
470
|
}
|
|
468
|
-
remove_refund_guards(guard_address
|
|
469
|
-
if (
|
|
470
|
-
|
|
471
|
-
}
|
|
472
|
-
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)) {
|
|
473
474
|
ERROR(Errors.InvalidParam, 'guard_address')
|
|
474
475
|
}
|
|
475
476
|
|
|
@@ -512,22 +513,24 @@ export class Service {
|
|
|
512
513
|
sales.forEach((v) => {
|
|
513
514
|
if (!IsValidName(v.item)) bValid = false;
|
|
514
515
|
if (!IsValidInt(v.price)) bValid = false;
|
|
515
|
-
if (!
|
|
516
|
+
if (!IsValidUintLarge(v.stock)) bValid = false;
|
|
516
517
|
if (names.includes(v.item)) bValid = false;
|
|
517
518
|
names.push(v.item)
|
|
518
519
|
})
|
|
519
520
|
return bValid
|
|
520
521
|
}
|
|
521
522
|
|
|
522
|
-
|
|
523
|
-
if (
|
|
524
|
-
|
|
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')
|
|
525
528
|
}
|
|
526
529
|
|
|
527
530
|
let names: string[] = []; let price: number[] = []; let stock: number[] = []; let endpoint: string[] = [];
|
|
528
531
|
sales.forEach((s) => {
|
|
529
532
|
if (s.endpoint && !IsValidEndpoint(s.endpoint)) {
|
|
530
|
-
ERROR(Errors.IsValidEndpoint, '
|
|
533
|
+
ERROR(Errors.IsValidEndpoint, 'add_sales')
|
|
531
534
|
}
|
|
532
535
|
names.push(s.item); price.push(s.price); stock.push(s.stock); endpoint.push(s.endpoint ?? '')
|
|
533
536
|
})
|
|
@@ -554,12 +557,11 @@ export class Service {
|
|
|
554
557
|
})
|
|
555
558
|
}
|
|
556
559
|
}
|
|
557
|
-
remove_sales(sales
|
|
558
|
-
if (
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
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')
|
|
563
565
|
}
|
|
564
566
|
|
|
565
567
|
let txb = this.protocol.CurrentSession();
|
|
@@ -588,11 +590,11 @@ export class Service {
|
|
|
588
590
|
let bValid = true;
|
|
589
591
|
discount_dispatch.forEach((v) => {
|
|
590
592
|
if (!IsValidAddress(v.receiver)) bValid = false;
|
|
591
|
-
if (!
|
|
593
|
+
if (!IsValidUintLarge(v.count) || v.count > Service.MAX_DISCOUNT_COUNT_ONCE) bValid = false;
|
|
592
594
|
if (!IsValidName_AllowEmpty(v.discount.name)) bValid = false;
|
|
593
595
|
if (v.discount.type == Service_Discount_Type.ratio && !IsValidPercent(v.discount.off)) bValid = false;
|
|
594
|
-
if (!
|
|
595
|
-
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;
|
|
596
598
|
if (v.discount?.price_greater && !IsValidInt(v.discount.price_greater)) bValid = false;
|
|
597
599
|
})
|
|
598
600
|
if (!bValid) {
|
|
@@ -932,7 +934,7 @@ export class Service {
|
|
|
932
934
|
buy_items.forEach((v) => {
|
|
933
935
|
if (!IsValidName(v.item)) bValid = false;
|
|
934
936
|
if (!IsValidInt(v.max_price)) bValid = false;
|
|
935
|
-
if (!
|
|
937
|
+
if (!IsValidUintLarge(v.count)) bValid = false;
|
|
936
938
|
if (names.includes(v.item)) bValid = false;
|
|
937
939
|
names.push(v.item)
|
|
938
940
|
})
|
package/src/utils.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
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 { RepositoryValueType, ValueType } from './protocol'
|
|
6
|
+
import { RepositoryValueType, ValueType, Protocol } from './protocol'
|
|
6
7
|
|
|
7
|
-
export const MAX_U8 = BigInt('
|
|
8
|
+
export const MAX_U8 = BigInt('255');
|
|
8
9
|
export const MAX_U64 = BigInt('18446744073709551615');
|
|
9
10
|
export const MAX_U128 = BigInt('340282366920938463463374607431768211455');
|
|
10
11
|
export const MAX_U256 = BigInt('115792089237316195423570985008687907853269984665640564039457584007913129639935');
|
|
@@ -30,36 +31,7 @@ export const ValueTypeConvert = (type:ValueType | null | undefined) : Repository
|
|
|
30
31
|
return -1;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
export const
|
|
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
|
-
|
|
62
|
-
export const readOption = (arr: number[], de:ValueType) : {bNone:boolean, value:any}=> {
|
|
34
|
+
export const readOption = (arr: number[], de:ValueType) : {bNone:boolean, value:any} => {
|
|
63
35
|
let o = arr.splice(0, 1);
|
|
64
36
|
if (o[0] == 1) { // true
|
|
65
37
|
return {bNone:false, value:Bcs.getInstance().de(de, Uint8Array.from(arr))};
|
|
@@ -209,7 +181,15 @@ export class Bcs {
|
|
|
209
181
|
return Bcs._instance;
|
|
210
182
|
}
|
|
211
183
|
|
|
212
|
-
|
|
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
|
+
|
|
213
193
|
switch(type) {
|
|
214
194
|
case ValueType.TYPE_BOOL:
|
|
215
195
|
return this.bcs.ser(BCS.BOOL, data).toBytes();
|
|
@@ -397,6 +377,15 @@ export const IsValidAddress = (addr:string) : boolean => {
|
|
|
397
377
|
}
|
|
398
378
|
return true
|
|
399
379
|
}
|
|
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
|
+
}
|
|
400
389
|
export const IsValidTokenType = (argType: string) : boolean => {
|
|
401
390
|
if (!argType || argType.length === 0) {
|
|
402
391
|
return false;
|
|
@@ -420,12 +409,6 @@ export const IsValidArgType = (argType: string) : boolean => {
|
|
|
420
409
|
}
|
|
421
410
|
return true;
|
|
422
411
|
}
|
|
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
412
|
export const IsValidInt = (value: number | string) : boolean => {
|
|
430
413
|
if (typeof(value) === 'string') {
|
|
431
414
|
value = parseInt(value as string);
|
|
@@ -468,7 +451,7 @@ export const ResolveBalance = (balance:string, decimals:number) : string => {
|
|
|
468
451
|
let end = balance.slice(Math.abs(pos));
|
|
469
452
|
return start + '.' + end;
|
|
470
453
|
} else {
|
|
471
|
-
return '.' + balance.padStart(
|
|
454
|
+
return '.' + balance.padStart(decimals, '0');
|
|
472
455
|
}
|
|
473
456
|
}
|
|
474
457
|
|
|
@@ -525,4 +508,47 @@ export function isValidHttpUrl(url:string) : boolean {
|
|
|
525
508
|
}
|
|
526
509
|
|
|
527
510
|
return r.protocol === "http:" || r.protocol === "https:" || r.protocol === 'ipfs:';
|
|
528
|
-
}
|
|
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
|
+
}
|