wowok 1.4.23 → 1.4.27

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/src/vote.ts CHANGED
@@ -1,16 +1,27 @@
1
1
  import { FnCallType, PassportObject, PermissionObject, GuardObject, VoteAddress, Protocol, TxbObject} from './protocol';
2
- import { IsValidDesription, IsValidUintLarge, IsValidAddress, Bcs, array_unique, IsValidArray, IsValidName } from './utils';
2
+ import { IsValidDesription, IsValidAddress, Bcs, array_unique, IsValidArray, IsValidName, IsValidU64, IsValidU256, IsValidU8 } from './utils';
3
3
  import { ERROR, Errors } from './exception';
4
4
  import { ValueType } from './protocol';
5
5
  import { Transaction as TransactionBlock} from '@mysten/sui/transactions';
6
6
 
7
-
8
-
9
7
  export type VoteOption = {
10
8
  name:string;
11
9
  reference_address?:string;
12
10
  }
13
11
 
12
+ export type QueryVotedResult = {
13
+ who: string;
14
+ voted: number[]
15
+ weight: bigint;
16
+ error?: string;
17
+ }
18
+
19
+ export type VoteGuardWeight = {
20
+ guard: GuardObject;
21
+ weight: bigint;
22
+ }
23
+
24
+ export type OnQueryVoted = (result: QueryVotedResult) => void;
14
25
  export class Vote {
15
26
  protected permission;
16
27
  protected object : TxbObject;
@@ -35,10 +46,10 @@ export class Vote {
35
46
  if (!IsValidDesription(description)) {
36
47
  ERROR(Errors.IsValidDesription)
37
48
  }
38
- if (!IsValidUintLarge(time)) {
49
+ if (!IsValidU64(time)) {
39
50
  ERROR(Errors.IsValidUint, 'time')
40
51
  }
41
- if (max_choice_count && !IsValidUintLarge(max_choice_count)) {
52
+ if (max_choice_count && !IsValidU64(max_choice_count)) {
42
53
  ERROR(Errors.IsValidUint, 'max_choice_count')
43
54
  }
44
55
  if (max_choice_count && max_choice_count > Vote.MAX_CHOICE_COUNT) {
@@ -120,25 +131,32 @@ export class Vote {
120
131
  }
121
132
 
122
133
  }
123
- add_guard(guard:GuardObject, weight:number, passport?:PassportObject) {
124
- if (!Protocol.IsValidObjects([guard])) {
125
- ERROR(Errors.IsValidObjects, 'add_guard')
126
- }
127
- if (!IsValidUintLarge(weight)) {
128
- ERROR(Errors.IsValidUint, 'add_guard')
134
+ add_guard(guards:VoteGuardWeight[], passport?:PassportObject) {
135
+ if (guards.length === 0) return;
136
+ let bValid = true;
137
+ guards.forEach((v) => {
138
+ if (!IsValidU64(v.weight) || v.weight === BigInt(0)) bValid = false;
139
+ if (!Protocol.IsValidObjects([v.guard])) bValid = false;
140
+ })
141
+ if (!bValid) {
142
+ ERROR(Errors.InvalidParam, 'add_guard.gurads')
129
143
  }
130
144
 
131
145
  if (passport) {
132
- this.txb.moveCall({
133
- target:Protocol.Instance().VoteFn('guard_add_with_passport') as FnCallType,
134
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, guard),
135
- this.txb.pure.u64(weight), Protocol.TXB_OBJECT(this.txb, this.permission)]
146
+ guards.forEach((guard) => {
147
+ this.txb.moveCall({
148
+ target:Protocol.Instance().VoteFn('guard_add_with_passport') as FnCallType,
149
+ arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, guard.guard),
150
+ this.txb.pure.u64(guard.weight), Protocol.TXB_OBJECT(this.txb, this.permission)]
151
+ })
136
152
  })
137
153
  } else {
138
- this.txb.moveCall({
139
- target:Protocol.Instance().VoteFn('guard_add') as FnCallType,
140
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, guard),
141
- this.txb.pure.u64(weight), Protocol.TXB_OBJECT(this.txb, this.permission)]
154
+ guards.forEach((guard) => {
155
+ this.txb.moveCall({
156
+ target:Protocol.Instance().VoteFn('guard_add') as FnCallType,
157
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, guard.guard),
158
+ this.txb.pure.u64(guard.weight), Protocol.TXB_OBJECT(this.txb, this.permission)]
159
+ })
142
160
  })
143
161
  }
144
162
  }
@@ -186,7 +204,7 @@ export class Vote {
186
204
  let bValid = true;
187
205
  options.forEach((v) => {
188
206
  if (!IsValidName(v.name)) bValid = false;
189
- if (v?.reference_address && IsValidAddress(v.reference_address)) bValid = false;
207
+ if (v?.reference_address && !IsValidAddress(v.reference_address)) bValid = false;
190
208
  })
191
209
  if (!bValid) {
192
210
  ERROR(Errors.InvalidParam, 'options')
@@ -214,8 +232,8 @@ export class Vote {
214
232
  if (!removeall && options.length===0) {
215
233
  return
216
234
  }
217
- if (options && !IsValidArray(options, IsValidAddress)) {
218
- ERROR(Errors.IsValidArray, 'remove_option')
235
+ if (!IsValidArray(options, IsValidName)) {
236
+ ERROR(Errors.IsValidArray, 'remove_option.options')
219
237
  }
220
238
 
221
239
  if (passport) {
@@ -249,7 +267,7 @@ export class Vote {
249
267
  }
250
268
  }
251
269
  set_max_choice_count(max_choice_count:number, passport?:PassportObject) {
252
- if (!IsValidUintLarge(max_choice_count) || max_choice_count > Vote.MAX_CHOICE_COUNT) {
270
+ if (!IsValidU64(max_choice_count) || max_choice_count > Vote.MAX_CHOICE_COUNT) {
253
271
  ERROR(Errors.InvalidParam, 'max_choice_count')
254
272
  }
255
273
 
@@ -297,7 +315,7 @@ export class Vote {
297
315
  }
298
316
 
299
317
  expand_deadline(ms_expand:boolean, time:number, passport?:PassportObject) {
300
- if (!IsValidUintLarge(time)) {
318
+ if (!IsValidU64(time)) {
301
319
  ERROR(Errors.IsValidUint, 'time')
302
320
  }
303
321
 
@@ -319,38 +337,57 @@ export class Vote {
319
337
  lock_guard(passport?:PassportObject) {
320
338
  if (passport) {
321
339
  this.txb.moveCall({
322
- target:Protocol.Instance().VoteFn('guard_lock_with_passport') as FnCallType,
340
+ target:Protocol.Instance().VoteFn('guard_locked_with_passport') as FnCallType,
323
341
  arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, this.permission)]
324
342
  })
325
343
  } else {
326
344
  this.txb.moveCall({
327
- target:Protocol.Instance().VoteFn('guard_lock') as FnCallType,
345
+ target:Protocol.Instance().VoteFn('guard_locked') as FnCallType,
328
346
  arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, this.permission)]
329
347
  })
330
348
  }
331
349
  }
332
350
 
333
- agree(options:string[], passport?:PassportObject) {
351
+ agree(options:number[], passport?:PassportObject) {
334
352
  if (options.length === 0) return;
335
353
  if (options.length > Vote.MAX_CHOICE_COUNT) {
336
- ERROR(Errors.InvalidParam, 'agree')
354
+ ERROR(Errors.InvalidParam, 'agree.options')
355
+ }
356
+ if (!IsValidArray(options, (v:any) => {
357
+ return IsValidU8(v) && v <= Vote.MAX_AGREES_COUNT;
358
+ })) {
359
+ ERROR(Errors.IsValidArray, 'agree.options')
337
360
  }
338
361
 
362
+ const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
363
+
339
364
  if (passport) {
340
365
  this.txb.moveCall({
341
- target:Protocol.Instance().VoteFn('with_passport') as FnCallType,
366
+ target:Protocol.Instance().VoteFn('vote_with_passport') as FnCallType,
342
367
  arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object),
343
- this.txb.pure.vector('string', array_unique(options))]
368
+ this.txb.pure.vector('u8', array_unique(options)), this.txb.object(clock)]
344
369
  })
345
370
  } else {
346
371
  this.txb.moveCall({
347
- target:Protocol.Instance().VoteFn('this.object') as FnCallType,
372
+ target:Protocol.Instance().VoteFn('vote') as FnCallType,
348
373
  arguments:[Protocol.TXB_OBJECT(this.txb, this.object),
349
- this.txb.pure.vector('string', array_unique(options))]
374
+ this.txb.pure.vector('u8', array_unique(options)), this.txb.object(clock)]
350
375
  })
351
376
  }
352
377
  }
378
+ QueryVoted(address_queried:string, event:OnQueryVoted, sender?:string) {
379
+ Protocol.Client().devInspectTransactionBlock({sender:sender ?? address_queried, transactionBlock:this.txb}).then((res) => {
380
+ if (res.results && res.results[0].returnValues && res.results[0].returnValues.length !== 3 ) {
381
+ event({who:address_queried, error:'not match', voted:[], weight:BigInt(0)});
382
+ return
383
+ }
384
+ console.log((res.results as any)[0].returnValues);
353
385
 
386
+ }).catch((e) => {
387
+ console.log(e);
388
+ event({who:address_queried, error:'error', weight:BigInt(0), voted:[]});
389
+ })
390
+ }
354
391
  change_permission(new_permission:PermissionObject) {
355
392
  if (!Protocol.IsValidObjects([new_permission])) {
356
393
  ERROR(Errors.IsValidObjects)
@@ -364,5 +401,6 @@ export class Vote {
364
401
  }
365
402
 
366
403
  static MAX_AGREES_COUNT = 100;
367
- static MAX_CHOICE_COUNT = 100;
404
+ static MAX_CHOICE_COUNT = 100;
405
+ static MAX_GUARD_COUNT = 16;
368
406
  }
@@ -0,0 +1,164 @@
1
+ import { FnCallType, PaymentObject, ReceivedObject, PaymentAddress, Protocol, TxbObject, CoinObject, PassportObject} from './protocol';
2
+ import { IsValidDesription, IsValidAddress, Bcs, array_unique, IsValidArray, IsValidName, IsValidU64, IsValidU256, IsValidU8 } from './utils';
3
+ import { ERROR, Errors } from './exception';
4
+ import { DepositParam, WithdrawParam, WithdrawItem } from './treasury';
5
+ import { Transaction as TransactionBlock} from '@mysten/sui/transactions';
6
+
7
+ export interface WithholdingGuard {
8
+ guard: string,
9
+ amount: bigint,
10
+ }
11
+ export interface WithholdingParam {
12
+ guards: WithholdingGuard[]
13
+ }
14
+
15
+ export class Withholding {
16
+ protected object : TxbObject;
17
+ protected pay_token_type;
18
+ protected txb;
19
+
20
+ get_pay_type() { return this.pay_token_type }
21
+ get_object() { return this.object }
22
+ private constructor(txb:TransactionBlock, pay_token_type:string, ) {
23
+ this.object = '';
24
+ this.pay_token_type = pay_token_type;
25
+ this.txb = txb;
26
+ }
27
+
28
+ static From(txb:TransactionBlock, pay_token_type:string, object:TxbObject) : Withholding {
29
+ if (!pay_token_type) ERROR(Errors.InvalidParam, 'Withholding.From.pay_token_type');
30
+ let v = new Withholding(txb, pay_token_type);
31
+ v.object = Protocol.TXB_OBJECT(txb, object)
32
+ return v
33
+ }
34
+
35
+ static New(txb:TransactionBlock, pay_token_type:string, param:WithholdingParam) : Withholding {
36
+ if (!pay_token_type) ERROR(Errors.InvalidParam, 'Withholding.New_fromAddress.pay_token_type');
37
+ if (param.guards.length === 0 || param.guards.length > Withholding.MAX_GUARD_COUNT) {
38
+ ERROR(Errors.InvalidParam, 'Withholding.New.param.guards length')
39
+ }
40
+ if (!IsValidArray(param.guards, (item:WithholdingGuard) => IsValidAddress(item.guard) && IsValidU64(item.amount))) {
41
+ ERROR(Errors.InvalidParam, 'Withholding.New.param.guards')
42
+ }
43
+
44
+ let v = new Withholding(txb, pay_token_type);
45
+ const clock = txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
46
+
47
+ v.object = txb.moveCall({
48
+ target:Protocol.Instance().WithholdingFn('new') as FnCallType,
49
+ arguments:[],
50
+ typeArguments:[pay_token_type],
51
+ })
52
+
53
+ param.guards.forEach((i) => {
54
+ txb.moveCall({
55
+ target:Protocol.Instance().WithholdingFn('add_guard') as FnCallType,
56
+ arguments:[txb.object(v.object), txb.object(i.guard), txb.pure.u64(i.amount)],
57
+ typeArguments:[pay_token_type],
58
+ })
59
+ })
60
+ return v
61
+ }
62
+
63
+ launch() : PaymentAddress {
64
+ return this.txb.moveCall({
65
+ target:Protocol.Instance().WithholdingFn('create') as FnCallType,
66
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object)],
67
+ typeArguments:[this.pay_token_type],
68
+ })
69
+ }
70
+
71
+ receive(payment:PaymentObject, received:ReceivedObject) {
72
+ if (!Protocol.IsValidObjects([payment, received])) {
73
+ ERROR(Errors.IsValidArray, 'receive.payment&received');
74
+ }
75
+ const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
76
+
77
+ return this.txb.moveCall({
78
+ target:Protocol.Instance().WithholdingFn('receive') as FnCallType,
79
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.object(received), this.txb.object(payment),
80
+ this.txb.object(clock)],
81
+ typeArguments:[this.pay_token_type],
82
+ })
83
+ }
84
+
85
+ deposit(param:DepositParam) {
86
+ if (!Protocol.IsValidObjects([param.coin])) {
87
+ ERROR(Errors.IsValidObjects, 'deposit.param.coin')
88
+ }
89
+ if (!IsValidDesription(param.remark)) {
90
+ ERROR(Errors.IsValidDesription, 'deposit.param.remark')
91
+ }
92
+ if (param?.for_object && !IsValidAddress(param.for_object)) {
93
+ ERROR(Errors.IsValidAddress, 'deposit.param.for_object')
94
+ }
95
+ if (param?.for_guard && !IsValidAddress(param.for_guard)) {
96
+ ERROR(Errors.IsValidAddress, 'deposit.param.for_guard')
97
+ }
98
+ if (param.index !== undefined && !IsValidU64(param.index)) {
99
+ ERROR(Errors.InvalidParam, 'deposit.param.index')
100
+ }
101
+
102
+ const for_obj = this.txb.pure.option('address', param.for_object ?? undefined);
103
+ const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
104
+
105
+ if (param.for_guard) {
106
+ return this.txb.moveCall({
107
+ target:Protocol.Instance().WithholdingFn('deposit_forGuard') as FnCallType,
108
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, param.coin), this.txb.pure.u64(param.index),
109
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(param.for_guard), this.txb.object(clock)],
110
+ typeArguments:[this.pay_token_type],
111
+ })
112
+ } else {
113
+ return this.txb.moveCall({
114
+ target:Protocol.Instance().WithholdingFn('deposit') as FnCallType,
115
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, param.coin), this.txb.pure.u64(param.index),
116
+ this.txb.pure.string(param.remark), for_obj, this.txb.object(clock)],
117
+ typeArguments:[this.pay_token_type],
118
+ })
119
+ }
120
+ }
121
+
122
+ // param.treasury -> coins ; param.receiver -> null
123
+ withdraw(guard:string, param:WithdrawParam, passport:PassportObject) {
124
+ if (param.items.length === 0) return undefined;
125
+ if (!IsValidArray(param.items, (item:WithdrawItem) => IsValidU64(item.amount) && IsValidAddress(item.address))) {
126
+ ERROR(Errors.IsValidArray, 'withdraw.param.items')
127
+ }
128
+ if (!IsValidDesription(param.remark)) {
129
+ ERROR(Errors.IsValidDesription, 'withdraw.param.remark')
130
+ }
131
+ if (!IsValidU64(param.index)) {
132
+ ERROR(Errors.IsValidU64, 'withdraw.param.index')
133
+ }
134
+ if (param?.for_guard && !IsValidAddress(param.for_guard)) {
135
+ ERROR(Errors.IsValidAddress, 'withdraw.param.for_guard')
136
+ }
137
+ if (param?.for_object && !IsValidAddress(param.for_object)) {
138
+ ERROR(Errors.IsValidAddress, 'withdraw.param.for_object')
139
+ }
140
+
141
+ const for_obj = this.txb.pure.option('address', param.for_object ?? undefined);
142
+ const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
143
+
144
+ if (param.for_guard) {
145
+ return this.txb.moveCall({
146
+ target:Protocol.Instance().WithholdingFn('withdraw_forGuard') as FnCallType,
147
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.object(guard), this.txb.object(passport),
148
+ this.txb.pure.vector('address', param.items.map(i=>i.address)), this.txb.pure.vector('u64', param.items.map(i=>i.amount)),
149
+ this.txb.pure.u64(param.index), this.txb.pure.string(param.remark), for_obj, this.txb.object(param.for_guard), this.txb.object(clock)],
150
+ typeArguments:[this.pay_token_type],
151
+ })
152
+ } else {
153
+ return this.txb.moveCall({
154
+ target:Protocol.Instance().WithholdingFn('withdraw') as FnCallType,
155
+ arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.object(guard), this.txb.object(passport),
156
+ this.txb.pure.vector('address', param.items.map(i=>i.address)), this.txb.pure.vector('u64', param.items.map(i=>i.amount)),
157
+ this.txb.pure.u64(param.index), this.txb.pure.string(param.remark), for_obj, this.txb.object(clock)],
158
+ typeArguments:[this.pay_token_type],
159
+ })
160
+ }
161
+ }
162
+
163
+ static MAX_GUARD_COUNT = 16;
164
+ }