-
Notifications
You must be signed in to change notification settings - Fork 358
/
Copy pathtest-multisigs.ts
185 lines (170 loc) · 6.18 KB
/
test-multisigs.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
import "@moonbeam-network/api-augment";
import { beforeAll, describeSuite, expect } from "@moonwall/cli";
import { blake2AsHex, createKeyMulti } from "@polkadot/util-crypto";
import { u8aToHex } from "@polkadot/util";
import {
ALITH_ADDRESS,
BALTATHAR_ADDRESS,
CHARLETH_ADDRESS,
DOROTHY_ADDRESS,
alith,
baltathar,
} from "@moonwall/util";
// This test cases in this suite are dependent on each other, and must be run in order.
// TODO: Make the test cases atomic
describeSuite({
id: "D012401",
title: "Multisigs - perform multisigs operations",
foundationMethods: "dev",
testCases: ({ context, it }) => {
let threshold: number;
let call: any;
let encodedCall: string;
let encodedCallHash: string;
// multisig accountId
let encodedMultisigId: Uint8Array;
let multisigId: string;
beforeAll(async function () {
// set threshold and create multisig accountId
threshold = 2;
encodedMultisigId = createKeyMulti([ALITH_ADDRESS, BALTATHAR_ADDRESS, CHARLETH_ADDRESS], 2);
multisigId = u8aToHex(encodedMultisigId.slice(0, 20));
// encode and hash the call we want to dispatch as a multisig operation
call = context.polkadotJs().tx.balances.transferKeepAlive(DOROTHY_ADDRESS, 20);
encodedCall = call.method.toHex();
encodedCallHash = blake2AsHex(encodedCall);
});
it({
id: "T01",
title: "Should create a multisig operation with asMulti",
test: async () => {
// set signatories
const otherSignatories = [BALTATHAR_ADDRESS, CHARLETH_ADDRESS];
const block = await context.createBlock(
context
.polkadotJs()
.tx.multisig.asMulti(threshold, otherSignatories, null, encodedCall, {})
.signAsync(alith)
);
// check the event 'NewMultisig' was emitted
const records = await context.polkadotJs().query.system.events();
const events = records.filter(
({ event }) => event.section == "multisig" && event.method == "NewMultisig"
);
expect(events).to.have.lengthOf(1);
expect(block.result!.successful).to.be.true;
},
});
it({
id: "T02",
title: "Should be able to approve a multisig operation with approveAsMulti",
test: async function () {
// signatories (sorted)
const otherSignatories = [CHARLETH_ADDRESS, ALITH_ADDRESS];
// create a new multisig operation
await context.createBlock(
context
.polkadotJs()
.tx.multisig.asMulti(threshold, otherSignatories, null, encodedCall, {})
.signAsync(alith),
{ allowFailures: true }
);
// take the info of the new multisig operation saved in storage
const multisigInfo = await context
.polkadotJs()
.query.multisig.multisigs(multisigId, encodedCallHash);
const block = await context.createBlock(
context
.polkadotJs()
.tx.multisig.approveAsMulti(
threshold,
otherSignatories,
multisigInfo.unwrap().when,
encodedCallHash,
{}
)
.signAsync(baltathar),
{ allowFailures: true }
);
// check the event 'MultisigApproval' was emitted
const records = await context.polkadotJs().query.system.events();
const events = records.filter(
({ event }) => event.section == "multisig" && event.method == "MultisigApproval"
);
expect(events).to.have.lengthOf(1);
expect(block.result!.successful).to.be.true;
},
});
it({
id: "T03",
title: "Should be able to cancel a multisig operation",
test: async () => {
const otherSignatories = [BALTATHAR_ADDRESS, CHARLETH_ADDRESS];
// create a new multisig operation
await context.createBlock(
context
.polkadotJs()
.tx.multisig.asMulti(threshold, otherSignatories, null, encodedCall, {})
.signAsync(alith),
{ allowFailures: true }
);
// take the info of the new multisig operation saved in storage
const multisigInfo = await context
.polkadotJs()
.query.multisig.multisigs(multisigId, encodedCallHash);
const block = await context.createBlock(
context
.polkadotJs()
.tx.multisig.cancelAsMulti(
threshold,
otherSignatories,
multisigInfo.unwrap().when,
encodedCallHash
)
.signAsync(alith)
);
const records = await context.polkadotJs().query.system.events();
const events = records.filter(
({ event }) => event.section == "multisig" && event.method == "MultisigCancelled"
);
expect(events, "event 'MultisigCancelled' was not emitted").to.have.lengthOf(1);
expect(block.result!.successful).to.be.true;
},
});
it({
id: "T04",
title: "Should fail if signatories are out of order",
test: async () => {
const otherSignatories = [CHARLETH_ADDRESS, BALTATHAR_ADDRESS];
const block = await context.createBlock(
context
.polkadotJs()
.tx.multisig.asMulti(threshold, otherSignatories, null, encodedCall, {})
.signAsync(alith),
{ allowFailures: true }
);
expect(block.result!.error!.name, "signatories (they are not sorted)").to.equal(
"SignatoriesOutOfOrder"
);
expect(block.result!.successful).to.be.false;
},
});
it({
id: "T05",
title: "Should fail if sender is present in signatories",
test: async () => {
// signatories (with sender in signatories)
const otherSignatories = [ALITH_ADDRESS, BALTATHAR_ADDRESS];
const block = await context.createBlock(
context
.polkadotJs()
.tx.multisig.asMulti(threshold, otherSignatories, null, encodedCall, {})
.signAsync(alith),
{ allowFailures: true }
);
expect(block.result!.error!.name).to.equal("SenderInSignatories");
expect(block.result!.successful).to.be.false;
},
});
},
});