forked from eosdac/eosdac-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestHelpers.ts
683 lines (645 loc) · 19.2 KB
/
TestHelpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
import {
Account,
AccountManager,
ContractDeployer,
nextBlock,
sleep,
EOSManager,
generateTypes,
Contract,
} from 'lamington';
import * as ecc from 'eosjs-ecc';
import { factory } from './LoggingConfig';
import { EosioToken } from './external_contracts/eosio.token/eosio.token';
// Dac contracts
import { Dacdirectory } from './dacdirectory/dacdirectory';
import { Daccustodian } from './daccustodian/daccustodian';
import { Eosdactokens } from './eosdactokens/eosdactokens';
// import { Dacmultisigs } from "./dacmultisigs/dacmultisigs";
// import { Dacproposals } from "./dacproposals/dacproposals";
import * as fs from 'fs';
import * as path from 'path';
const log = factory.getLogger('TestHelper');
export var NUMBER_OF_REG_MEMBERS = 16;
export var NUMBER_OF_CANDIDATES = 7;
export async function debugPromise<T>(
promise: Promise<T>,
successMessage: string,
errorMessage?: string
) {
let debugPrefix = 'debugPromise - ';
let successString = debugPrefix + successMessage + ': ';
let errorString = errorMessage
? debugPrefix + errorMessage + ': '
: debugPrefix + 'error - ' + successMessage + ': ';
return promise
.then(value => {
log.info(successString + JSON.stringify(value, null, 4));
return value;
})
.catch(err => {
log.error(errorString + err);
return err;
});
}
// Shared Instances to use between tests.
let shared: SharedTestObjects;
let _regmembers: Account[];
let _candidates: Account[];
export interface SharedTestObjects {
readonly auth_account: Account;
readonly treasury_account: Account;
// === Dac Contracts
readonly dacdirectory_contract: Dacdirectory;
readonly daccustodian_contract: Daccustodian;
readonly dac_token_contract: Eosdactokens;
// === Shared Values
readonly configured_dac_id: string;
readonly configured_dac_memberterms: string;
readonly eosiotoken_contract: EosioToken;
// readonly regmembers: Array<Account>;
}
let shouldAllowCreateTestObjects = true;
export async function initAndGetSharedObjects(): Promise<SharedTestObjects> {
log.info('Called initAndGetSharedObjects');
if (shouldAllowCreateTestObjects) {
shouldAllowCreateTestObjects = false;
// log.info('Getting passed the if block');
await sleep(1500);
EOSManager.initWithDefaults();
let tempSharedObjects: SharedTestObjects = {
auth_account: await new_account('eosdacauth'),
treasury_account: await new_account('treasury'),
// Configure Dac contracts
dacdirectory_contract: await ContractDeployer.deployWithName(
'dacdirectory/dacdirectory',
'dacdirectory'
),
daccustodian_contract: await ContractDeployer.deployWithName(
'daccustodian/daccustodian',
'daccustodian'
),
dac_token_contract: await ContractDeployer.deployWithName(
'eosdactokens/eosdactokens',
'eosdactokens'
),
// Other objects
configured_dac_id: 'eosdacio',
configured_dac_memberterms: 'AgreedMemberTermsHashValue',
eosiotoken_contract: await ContractDeployer.deployWithName(
'./external_contracts/eosio.token/eosio.token',
'eosio.token'
),
};
// Further setup after the inital singleton object have been created.
await setup_tokens(tempSharedObjects);
await add_token_contract_permissions(tempSharedObjects);
await register_dac_with_directory(tempSharedObjects);
await setup_dac_memberterms(tempSharedObjects);
log.info('returning new shared');
shared = tempSharedObjects;
return shared;
} else if (shared) {
log.info('returning existing shared');
return shared;
}
}
export async function regmembers(): Promise<Account[]> {
return _regmembers ||
(_regmembers = await getRegMembers(NUMBER_OF_REG_MEMBERS))
? _regmembers
: Promise.reject('Error occurred!!!');
}
async function getRegMembers(count: number): Promise<Account[]> {
let newMembers = await AccountManager.createAccounts(count);
let termsPromises = newMembers
.map(account => {
return shared.dac_token_contract.memberrege(
account.name,
shared.configured_dac_memberterms,
shared.configured_dac_id,
{ from: account }
);
})
.concat(
newMembers.map(account => {
return shared.dac_token_contract.transfer(
shared.dac_token_contract.account.name,
account.name,
'1000.0000 EOSDAC',
'',
{ from: shared.dac_token_contract.account }
);
})
);
await debugPromise(
Promise.all(termsPromises),
'running `getRegMembers`: ' + count
);
newMembers.forEach(member => {
log.info('created member: ' + member.name);
});
return newMembers;
}
export async function candidates(): Promise<Account[]> {
return _candidates ||
(_candidates = await getCandidates(NUMBER_OF_CANDIDATES))
? _candidates
: Promise.reject('Error occurred!!!');
}
async function getCandidates(count: number): Promise<Account[]> {
let newCandidates = await getRegMembers(count);
for (let { candidate, index } of newCandidates.map((candidate, index) => ({
candidate,
index,
}))) {
await debugPromise(
shared.dac_token_contract.transfer(
candidate.name,
shared.daccustodian_contract.account.name,
'12.0000 EOSDAC',
'',
{
from: candidate,
}
),
'sending candidate funds for staking'
);
let indexOption = index % 3;
let payAmount = '';
if (indexOption == 0) {
payAmount = '15.0000 EOS';
} else if (indexOption == 1) {
payAmount = '20.0000 EOS';
} else {
payAmount = '25.0000 EOS';
}
await debugPromise(
shared.daccustodian_contract.nominatecane(
candidate.name,
payAmount,
shared.configured_dac_id,
{
from: candidate,
}
),
'nominate candidate'
);
}
return newCandidates;
}
async function setup_tokens(tempSharedObjects: SharedTestObjects) {
await tempSharedObjects.dac_token_contract.create(
tempSharedObjects.dac_token_contract.account.name,
'100000.0000 EOSDAC',
false,
{ from: tempSharedObjects.dac_token_contract.account }
);
await tempSharedObjects.dac_token_contract.issue(
tempSharedObjects.dac_token_contract.account.name,
'100000.0000 EOSDAC',
'Initial Token holder',
{ from: tempSharedObjects.dac_token_contract.account }
);
}
// Not used for now but could be useful later
async function setup_external(name: string) {
const compiled_dir = path.normalize(
`${__dirname}/../.lamington/compiled_contracts/${name}`
);
if (!fs.existsSync(compiled_dir)) {
fs.mkdirSync(compiled_dir);
}
fs.copyFileSync(
`${__dirname}/external_contracts/${name}.wasm`,
`${compiled_dir}/${name}.wasm`
);
fs.copyFileSync(
`${__dirname}/external_contracts/${name}.abi`,
`${compiled_dir}/${name}.abi`
);
await debugPromise(
generateTypes(`contracts/external_contracts/${name}/${name}`),
'generating types for external contract: ' + name
);
}
export async function new_account(name: string): Promise<Account> {
log.info('About to create account: ' + name);
const privateKey = await ecc.unsafeRandomKey();
const account = new Account(name, privateKey);
await AccountManager.setupAccount(account);
return account;
}
export function eosio_dot_code_perm(account: Account): any {
return singleAuthority(account, 'eosio.code');
}
export interface PermissionLevelWeight {
name: string;
permission: string;
weight: number;
}
export function singleAuthority(account: Account, permission: string): any {
return authorities([
{ name: account.name, permission: permission, weight: 1 },
]);
}
export function authorities(
permissionLevelWeights: PermissionLevelWeight[],
threshold: number = 1
): any {
return {
threshold: threshold,
accounts: permissionLevelWeights.map(permLevelWeight => {
return {
permission: {
actor: permLevelWeight.name,
permission: permLevelWeight.permission,
},
weight: permLevelWeight.weight,
};
}),
keys: [],
waits: [],
};
}
async function register_dac_with_directory(
tempSharedObjects: SharedTestObjects
) {
await tempSharedObjects.dacdirectory_contract.regdac(
tempSharedObjects.auth_account.name,
tempSharedObjects.configured_dac_id,
{
contract: tempSharedObjects.dac_token_contract.account.name,
symbol: '4,EOSDAC',
},
'dac_title',
[],
[
{ key: Account_type.AUTH, value: tempSharedObjects.auth_account.name },
{
key: Account_type.CUSTODIAN,
value: tempSharedObjects.daccustodian_contract.account.name,
},
],
{
auths: [
{ actor: tempSharedObjects.auth_account.name, permission: 'active' },
],
}
);
}
async function add_token_contract_permissions(
tempSharedObjects: SharedTestObjects
) {
// Construct the update actions
const actions: any = [
// Add the issue permission as a child of active to dac_token
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.dac_token_contract.account.active,
data: {
account: tempSharedObjects.dac_token_contract.account.name,
permission: 'issue',
parent: 'active',
auth: eosio_dot_code_perm(tempSharedObjects.dac_token_contract.account),
},
},
// Add the notify permission as a child of active to dac_token
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.dac_token_contract.account.active,
data: {
account: tempSharedObjects.dac_token_contract.account.name,
permission: 'notify',
parent: 'active',
auth: eosio_dot_code_perm(tempSharedObjects.dac_token_contract.account),
},
},
// Add the xfer permission as a child of active to provided account
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.dac_token_contract.account.active,
data: {
account: tempSharedObjects.dac_token_contract.account.name,
permission: 'xfer',
parent: 'active',
auth: eosio_dot_code_perm(tempSharedObjects.dac_token_contract.account),
},
},
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.daccustodian_contract.account.active,
data: {
account: tempSharedObjects.daccustodian_contract.account.name,
permission: 'pay',
parent: 'active',
auth: eosio_dot_code_perm(
tempSharedObjects.daccustodian_contract.account
),
},
},
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.auth_account.owner,
data: {
account: tempSharedObjects.auth_account.name,
permission: 'owner',
parent: '',
auth: eosio_dot_code_perm(
tempSharedObjects.daccustodian_contract.account
),
},
},
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.daccustodian_contract.account.owner,
data: {
account: tempSharedObjects.daccustodian_contract.account.name,
permission: 'owner',
parent: '',
auth: singleAuthority(tempSharedObjects.auth_account, 'active'),
},
},
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.daccustodian_contract.account.owner,
data: {
account: tempSharedObjects.daccustodian_contract.account.name,
permission: 'active',
parent: 'owner',
auth: singleAuthority(tempSharedObjects.auth_account, 'active'),
},
},
];
const link_actions: any = [
// Link issue permission of account to the issue action of account.
{
account: 'eosio',
name: 'linkauth',
authorization: tempSharedObjects.dac_token_contract.account.active,
data: {
account: tempSharedObjects.dac_token_contract.account.name,
code: tempSharedObjects.dac_token_contract.account.name,
type: 'issue',
requirement: 'issue',
},
},
// Link the notify permission of account to the weightobsv action of custodian
{
account: 'eosio',
name: 'linkauth',
authorization: tempSharedObjects.dac_token_contract.account.active,
data: {
account: tempSharedObjects.dac_token_contract.account.name,
code: tempSharedObjects.daccustodian_contract.account.name,
type: 'weightobsv',
requirement: 'notify',
},
},
// Link the notify permission of account to the stakeobsv action of custodian
{
account: 'eosio',
name: 'linkauth',
authorization: tempSharedObjects.dac_token_contract.account.active,
data: {
account: tempSharedObjects.dac_token_contract.account.name,
code: tempSharedObjects.daccustodian_contract.account.name,
type: 'stakeobsv',
requirement: 'notify',
},
},
// Link the notify permission of account to the refund action of dac token
{
account: 'eosio',
name: 'linkauth',
authorization: tempSharedObjects.dac_token_contract.account.active,
data: {
account: tempSharedObjects.dac_token_contract.account.name,
code: tempSharedObjects.dac_token_contract.account.name,
type: 'refund',
requirement: 'notify',
},
},
// Link the notify permission of dac_token to the balanceobsv action of voting.
{
account: 'eosio',
name: 'linkauth',
authorization: tempSharedObjects.dac_token_contract.account.active,
data: {
account: tempSharedObjects.dac_token_contract.account.name,
code: tempSharedObjects.daccustodian_contract.account.name, // or should be voting account
type: 'balanceobsv',
requirement: 'notify',
},
},
{
account: 'eosio',
name: 'linkauth',
authorization: tempSharedObjects.dac_token_contract.account.active,
data: {
account: tempSharedObjects.dac_token_contract.account.name,
code: tempSharedObjects.daccustodian_contract.account.name, // or should be voting account
type: 'capturestake',
requirement: 'notify',
},
},
// Link the xfer permission of account to the transfer action of account.
{
account: 'eosio',
name: 'linkauth',
authorization: tempSharedObjects.dac_token_contract.account.active,
data: {
account: tempSharedObjects.dac_token_contract.account.name,
code: tempSharedObjects.dac_token_contract.account.name,
type: 'transfer',
requirement: 'xfer',
},
},
// Link the xfer permission of account to the transfer action of account.
{
account: 'eosio',
name: 'linkauth',
authorization: tempSharedObjects.daccustodian_contract.account.active,
data: {
account: tempSharedObjects.daccustodian_contract.account.name,
code: tempSharedObjects.dac_token_contract.account.name,
type: 'transfer',
requirement: 'xfer',
},
},
{
account: 'eosio',
name: 'linkauth',
authorization: tempSharedObjects.daccustodian_contract.account.active,
data: {
account: tempSharedObjects.daccustodian_contract.account.name,
code: tempSharedObjects.daccustodian_contract.account.name,
type: 'clearstake',
requirement: 'pay',
},
},
];
// Execute the transaction actions
await debugPromise(EOSManager.transact({ actions }), 'Add auth actions');
await EOSManager.transact({
actions: [
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.auth_account.active,
data: {
account: tempSharedObjects.auth_account.name,
permission: 'high',
parent: 'active',
auth: eosio_dot_code_perm(
tempSharedObjects.daccustodian_contract.account
),
},
},
],
});
await EOSManager.transact({
actions: [
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.auth_account.active,
data: {
account: tempSharedObjects.auth_account.name,
permission: 'med',
parent: 'high',
auth: eosio_dot_code_perm(
tempSharedObjects.daccustodian_contract.account
),
},
},
],
});
await EOSManager.transact({
actions: [
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.auth_account.active,
data: {
account: tempSharedObjects.auth_account.name,
permission: 'low',
parent: 'med',
auth: eosio_dot_code_perm(
tempSharedObjects.daccustodian_contract.account
),
},
},
],
});
await EOSManager.transact({
actions: [
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.auth_account.active,
data: {
account: tempSharedObjects.auth_account.name,
permission: 'one',
parent: 'low',
auth: eosio_dot_code_perm(
tempSharedObjects.daccustodian_contract.account
),
},
},
],
});
await EOSManager.transact({
actions: [
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.auth_account.active,
data: {
account: tempSharedObjects.auth_account.name,
permission: 'admin',
parent: 'one',
auth: eosio_dot_code_perm(
tempSharedObjects.daccustodian_contract.account
),
},
},
],
});
await EOSManager.transact({
actions: [
{
account: 'eosio',
name: 'updateauth',
authorization: tempSharedObjects.daccustodian_contract.account.active,
data: {
account: tempSharedObjects.daccustodian_contract.account.name,
permission: 'xfer',
parent: 'active',
auth: authorities(
[
{
name: tempSharedObjects.daccustodian_contract.account.name,
permission: 'eosio.code',
weight: 1,
},
{
name: tempSharedObjects.auth_account.name,
permission: 'med',
weight: 1,
},
],
2
),
},
},
],
});
await debugPromise(
EOSManager.transact({ actions: link_actions }),
'Linking actions'
);
}
async function setup_dac_memberterms(tempSharedObjects: SharedTestObjects) {
await debugPromise(
tempSharedObjects.dac_token_contract.newmemtermse(
'teermsstring',
tempSharedObjects.configured_dac_memberterms,
tempSharedObjects.configured_dac_id,
{ from: tempSharedObjects.auth_account }
),
'setting member terms'
);
}
export enum Account_type {
AUTH = 0,
TREASURY = 1,
CUSTODIAN = 2,
MSIGS = 3,
SERVICE = 5,
PROPOSALS = 6,
ESCROW = 7,
VOTING = 8,
EXTERNAL = 254,
OTHER = 255,
}
enum ref_type {
HOMEPAGE = 0,
LOGO_URL = 1,
DESCRIPTION = 2,
LOGO_NOTEXT_URL = 3,
BACKGROUND_URL = 4,
COLORS = 5,
CLIENT_EXTENSION = 6,
}
enum dac_state_type {
dac_state_typeINACTIVE = 0,
dac_state_typeACTIVE = 1,
}