wowok 1.4.35 → 1.5.38

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 DELETED
@@ -1,406 +0,0 @@
1
- import { FnCallType, PassportObject, PermissionObject, GuardObject, VoteAddress, Protocol, TxbObject} from './protocol';
2
- import { IsValidDesription, IsValidAddress, Bcs, array_unique, IsValidArray, IsValidName, IsValidU64, IsValidU256, IsValidU8 } from './utils';
3
- import { ERROR, Errors } from './exception';
4
- import { ValueType } from './protocol';
5
- import { Transaction as TransactionBlock} from '@mysten/sui/transactions';
6
-
7
- export type VoteOption = {
8
- name:string;
9
- reference_address?:string;
10
- }
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;
25
- export class Vote {
26
- protected permission;
27
- protected object : TxbObject;
28
- protected txb;
29
-
30
- get_object() { return this.object }
31
- private constructor(txb:TransactionBlock, permission:PermissionObject) {
32
- this.object = '';
33
- this.txb = txb;
34
- this.permission = permission;
35
- }
36
- static From(txb:TransactionBlock, permission:PermissionObject, object:TxbObject) : Vote {
37
- let v = new Vote(txb, permission);
38
- v.object = Protocol.TXB_OBJECT(txb, object)
39
- return v
40
- }
41
- static New(txb:TransactionBlock, permission:PermissionObject, description:string, minutes_duration:boolean, time:number,
42
- max_choice_count?:number, reference_address?:string, passport?:PassportObject) : Vote {
43
- if (!Protocol.IsValidObjects([permission])) {
44
- ERROR(Errors.IsValidObjects, 'permission')
45
- }
46
- if (!IsValidDesription(description)) {
47
- ERROR(Errors.IsValidDesription)
48
- }
49
- if (!IsValidU64(time)) {
50
- ERROR(Errors.IsValidUint, 'time')
51
- }
52
- if (max_choice_count && !IsValidU64(max_choice_count)) {
53
- ERROR(Errors.IsValidUint, 'max_choice_count')
54
- }
55
- if (max_choice_count && max_choice_count > Vote.MAX_CHOICE_COUNT) {
56
- ERROR(Errors.InvalidParam, 'max_choice_count')
57
- }
58
- if (reference_address && !IsValidAddress(reference_address)) {
59
- ERROR(Errors.IsValidAddress, 'reference_address')
60
- }
61
-
62
- let v = new Vote(txb, permission);
63
-
64
- let reference = txb.pure.option('address', reference_address ? reference_address : undefined);
65
- let choice_count = max_choice_count ? max_choice_count : 1;
66
- const clock = txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
67
- if (passport) {
68
- v.object = txb.moveCall({
69
- target:Protocol.Instance().VoteFn('new_with_passport') as FnCallType,
70
- arguments:[passport, txb.pure.string(description), reference, txb.object(clock), txb.pure.bool(minutes_duration),
71
- txb.pure.u64(time), txb.pure.u8(choice_count), Protocol.TXB_OBJECT(txb, permission)]
72
- })
73
- } else {
74
- v.object = txb.moveCall({
75
- target:Protocol.Instance().VoteFn('new') as FnCallType,
76
- arguments:[txb.pure.string(description), reference, txb.object(clock), txb.pure.bool(minutes_duration),
77
- txb.pure.u64(time), txb.pure.u8(choice_count), Protocol.TXB_OBJECT(txb, permission)]
78
- })
79
- }
80
- return v
81
- }
82
-
83
- launch() : VoteAddress {
84
- return this.txb.moveCall({
85
- target:Protocol.Instance().VoteFn('create') as FnCallType,
86
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object)]
87
- })
88
- }
89
-
90
- destroy() {
91
- this.txb.moveCall({
92
- target:Protocol.Instance().VoteFn('destroy') as FnCallType,
93
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object)]
94
- })
95
- }
96
-
97
- set_description(description:string, passport?:PassportObject) {
98
- if (!IsValidDesription(description)) {
99
- ERROR(Errors.IsValidDesription)
100
- }
101
-
102
- if (passport) {
103
- this.txb.moveCall({
104
- target:Protocol.Instance().VoteFn('description_set_with_passport') as FnCallType,
105
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.string(description), Protocol.TXB_OBJECT(this.txb, this.permission)]
106
- })
107
- } else {
108
- this.txb.moveCall({
109
- target:Protocol.Instance().VoteFn('description_set') as FnCallType,
110
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.string(description), Protocol.TXB_OBJECT(this.txb, this.permission)]
111
- })
112
- }
113
- }
114
-
115
- set_reference(reference_address?:string|null, passport?:PassportObject) {
116
- if (reference_address && !IsValidAddress(reference_address)) {
117
- ERROR(Errors.IsValidAddress)
118
- }
119
-
120
- let reference = this.txb.pure.option('address', reference_address ? reference_address : undefined);
121
- if (passport) {
122
- this.txb.moveCall({
123
- target:Protocol.Instance().VoteFn('reference_set_with_passport') as FnCallType,
124
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), reference, Protocol.TXB_OBJECT(this.txb, this.permission)]
125
- })
126
- } else {
127
- this.txb.moveCall({
128
- target:Protocol.Instance().VoteFn('reference_set') as FnCallType,
129
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), reference, Protocol.TXB_OBJECT(this.txb, this.permission)]
130
- })
131
- }
132
-
133
- }
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')
143
- }
144
-
145
- if (passport) {
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
- })
152
- })
153
- } else {
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
- })
160
- })
161
- }
162
- }
163
-
164
- remove_guard(guard_address:string[], removeall?:boolean, passport?:PassportObject) {
165
- if (!removeall && guard_address.length===0) return;
166
-
167
- if (!IsValidArray(guard_address, IsValidAddress)) {
168
- ERROR(Errors.IsValidArray, 'remove_guard')
169
- }
170
-
171
- if (passport) {
172
- if (removeall) {
173
- this.txb.moveCall({
174
- target:Protocol.Instance().VoteFn('guard_remove_all_with_passport') as FnCallType,
175
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, this.permission)]
176
- })
177
- } else {
178
- this.txb.moveCall({
179
- target:Protocol.Instance().VoteFn('guard_remove_with_passport') as FnCallType,
180
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object),
181
- this.txb.pure.vector('address', array_unique(guard_address)), Protocol.TXB_OBJECT(this.txb, this.permission)]
182
- })
183
- }
184
- } else {
185
- if (removeall) {
186
- this.txb.moveCall({
187
- target:Protocol.Instance().VoteFn('guard_remove_all') as FnCallType,
188
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, this.permission)]
189
- })
190
- } else {
191
- this.txb.moveCall({
192
- target:Protocol.Instance().VoteFn('guard_remove') as FnCallType,
193
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object),
194
- this.txb.pure.vector('address', array_unique(guard_address)),
195
- Protocol.TXB_OBJECT(this.txb, this.permission)]
196
- })
197
- }
198
- }
199
- }
200
-
201
- add_option(options:VoteOption[], passport?:PassportObject) {
202
- if (options.length === 0) return ;
203
-
204
- let bValid = true;
205
- options.forEach((v) => {
206
- if (!IsValidName(v.name)) bValid = false;
207
- if (v?.reference_address && !IsValidAddress(v.reference_address)) bValid = false;
208
- })
209
- if (!bValid) {
210
- ERROR(Errors.InvalidParam, 'options')
211
- }
212
-
213
- options.forEach((option) => {
214
- let reference = this.txb.pure.option('address', option?.reference_address) ;
215
-
216
- if (passport) {
217
- this.txb.moveCall({
218
- target:Protocol.Instance().VoteFn('agrees_add_with_passport') as FnCallType,
219
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.string(option.name),
220
- reference, Protocol.TXB_OBJECT(this.txb, this.permission)]
221
- })
222
- } else {
223
- this.txb.moveCall({
224
- target:Protocol.Instance().VoteFn('agrees_add') as FnCallType,
225
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.string(option.name),
226
- reference, Protocol.TXB_OBJECT(this.txb, this.permission)]
227
- })
228
- }
229
- })
230
- }
231
- remove_option(options:string[], removeall?:boolean, passport?:PassportObject) {
232
- if (!removeall && options.length===0) {
233
- return
234
- }
235
- if (!IsValidArray(options, IsValidName)) {
236
- ERROR(Errors.IsValidArray, 'remove_option.options')
237
- }
238
-
239
- if (passport) {
240
- if (removeall) {
241
- this.txb.moveCall({
242
- target:Protocol.Instance().VoteFn('agrees_remove_all_with_passport') as FnCallType,
243
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, this.permission)]
244
- })
245
- } else {
246
- this.txb.moveCall({
247
- target:Protocol.Instance().VoteFn('agrees_remove_with_passport') as FnCallType,
248
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object),
249
- this.txb.pure.vector('string', array_unique(options)),
250
- Protocol.TXB_OBJECT(this.txb, this.permission)]
251
- })
252
- }
253
- } else {
254
- if (removeall) {
255
- this.txb.moveCall({
256
- target:Protocol.Instance().VoteFn('agrees_remove_all') as FnCallType,
257
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, this.permission)]
258
- })
259
- } else {
260
- this.txb.moveCall({
261
- target:Protocol.Instance().VoteFn('agrees_remove') as FnCallType,
262
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object),
263
- this.txb.pure.vector('string', array_unique(options)),
264
- Protocol.TXB_OBJECT(this.txb, this.permission)]
265
- })
266
- }
267
- }
268
- }
269
- set_max_choice_count(max_choice_count:number, passport?:PassportObject) {
270
- if (!IsValidU64(max_choice_count) || max_choice_count > Vote.MAX_CHOICE_COUNT) {
271
- ERROR(Errors.InvalidParam, 'max_choice_count')
272
- }
273
-
274
- if (passport) {
275
- this.txb.moveCall({
276
- target:Protocol.Instance().VoteFn('max_choice_count_set_with_passport') as FnCallType,
277
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object),
278
- this.txb.pure.u8(max_choice_count), Protocol.TXB_OBJECT(this.txb, this.permission)]
279
- })
280
- } else {
281
- this.txb.moveCall({
282
- target:Protocol.Instance().VoteFn('max_choice_count_set') as FnCallType,
283
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.u8(max_choice_count), Protocol.TXB_OBJECT(this.txb, this.permission)]
284
- })
285
- }
286
- }
287
-
288
- open_voting(passport?:PassportObject) {
289
- if (passport) {
290
- this.txb.moveCall({
291
- target:Protocol.Instance().VoteFn('options_locked_for_voting_with_passport') as FnCallType,
292
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, this.permission)]
293
- })
294
- } else {
295
- this.txb.moveCall({
296
- target:Protocol.Instance().VoteFn('options_locked_for_voting') as FnCallType,
297
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, this.permission)]
298
- })
299
- }
300
- }
301
-
302
- lock_deadline(passport?:PassportObject) {
303
- const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
304
- if (passport) {
305
- this.txb.moveCall({
306
- target:Protocol.Instance().VoteFn('deadline_locked_with_passport') as FnCallType,
307
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)]
308
- })
309
- } else {
310
- this.txb.moveCall({
311
- target:Protocol.Instance().VoteFn('deadline_locked') as FnCallType,
312
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.object(clock), Protocol.TXB_OBJECT(this.txb, this.permission)]
313
- })
314
- }
315
- }
316
-
317
- expand_deadline(ms_expand:boolean, time:number, passport?:PassportObject) {
318
- if (!IsValidU64(time)) {
319
- ERROR(Errors.IsValidUint, 'time')
320
- }
321
-
322
- if (passport) {
323
- this.txb.moveCall({
324
- target:Protocol.Instance().VoteFn('deadline_expand_with_passport') as FnCallType,
325
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.bool(ms_expand),
326
- this.txb.pure.u64(time), Protocol.TXB_OBJECT(this.txb, this.permission)]
327
- })
328
- } else {
329
- this.txb.moveCall({
330
- target:Protocol.Instance().VoteFn('deadline_expand') as FnCallType,
331
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), this.txb.pure.bool(ms_expand),
332
- this.txb.pure.u64(time), Protocol.TXB_OBJECT(this.txb, this.permission)]
333
- })
334
- }
335
- }
336
-
337
- lock_guard(passport?:PassportObject) {
338
- if (passport) {
339
- this.txb.moveCall({
340
- target:Protocol.Instance().VoteFn('guard_locked_with_passport') as FnCallType,
341
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, this.permission)]
342
- })
343
- } else {
344
- this.txb.moveCall({
345
- target:Protocol.Instance().VoteFn('guard_locked') as FnCallType,
346
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb, this.permission)]
347
- })
348
- }
349
- }
350
-
351
- agree(options:number[], passport?:PassportObject) {
352
- if (options.length === 0) return;
353
- if (options.length > Vote.MAX_CHOICE_COUNT) {
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')
360
- }
361
-
362
- const clock = this.txb.sharedObjectRef(Protocol.CLOCK_OBJECT);
363
-
364
- if (passport) {
365
- this.txb.moveCall({
366
- target:Protocol.Instance().VoteFn('vote_with_passport') as FnCallType,
367
- arguments:[passport, Protocol.TXB_OBJECT(this.txb, this.object),
368
- this.txb.pure.vector('u8', array_unique(options)), this.txb.object(clock)]
369
- })
370
- } else {
371
- this.txb.moveCall({
372
- target:Protocol.Instance().VoteFn('vote') as FnCallType,
373
- arguments:[Protocol.TXB_OBJECT(this.txb, this.object),
374
- this.txb.pure.vector('u8', array_unique(options)), this.txb.object(clock)]
375
- })
376
- }
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);
385
-
386
- }).catch((e) => {
387
- console.log(e);
388
- event({who:address_queried, error:'error', weight:BigInt(0), voted:[]});
389
- })
390
- }
391
- change_permission(new_permission:PermissionObject) {
392
- if (!Protocol.IsValidObjects([new_permission])) {
393
- ERROR(Errors.IsValidObjects)
394
- }
395
-
396
- this.txb.moveCall({
397
- target:Protocol.Instance().VoteFn('this.permission_set') as FnCallType,
398
- arguments: [Protocol.TXB_OBJECT(this.txb, this.object), Protocol.TXB_OBJECT(this.txb,this.permission), Protocol.TXB_OBJECT(this.txb, new_permission)],
399
- })
400
- this.permission = new_permission
401
- }
402
-
403
- static MAX_AGREES_COUNT = 100;
404
- static MAX_CHOICE_COUNT = 100;
405
- static MAX_GUARD_COUNT = 16;
406
- }