-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiSign.ts
More file actions
353 lines (280 loc) · 9.83 KB
/
MultiSign.ts
File metadata and controls
353 lines (280 loc) · 9.83 KB
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
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect, assert } from "chai";
import { ethers } from "hardhat";
import { iERC20 } from "./abi/erc20Fragment";
import { getTxIdFromEvents } from "./utils/helpers";
const parseEthers = ethers.utils.parseEther;
const stringToBytes = ethers.utils.toUtf8Bytes;
const keccak256 = ethers.utils.keccak256;
const mintSignature = iERC20.getSighash("mint");
const invalidTxId = keccak256("0x");
describe("MultiSign", function () {
async function deployFixture() {
const [owner, account1, account2, account3, notOwnerAccount, receiver] =
await ethers.getSigners();
const OWNERS = [owner, account1, account2, account3]; // *
const THRESHOLD = 3;
const MultiSign = await ethers.getContractFactory("MultiSign");
const multiSign = await MultiSign.connect(OWNERS[0]).deploy(
OWNERS.map((owner) => owner.address), // *
THRESHOLD,
);
return {
multiSign,
notOwnerAccount,
OWNERS,
receiver,
THRESHOLD,
};
}
// * Deployment -------------------------------------------------------------
describe("Deployment", async function () {
it("Should set owners correctly.", async function () {
const { OWNERS, multiSign } = await loadFixture(deployFixture);
for (const owner of OWNERS) {
assert(
(await multiSign.isOwner(owner.address)) === true,
`${owner.address} must be an owner.`,
);
}
assert(
(await multiSign.ownersCount()).toNumber() === 4,
"Owners count must be 4",
);
});
it("Should set threshold correctly.", async function () {
const { THRESHOLD, multiSign } = await loadFixture(deployFixture);
const threshold = await multiSign.threshold();
expect(threshold).to.be.equal(THRESHOLD);
});
});
// * Fund ------------------------------------------------------------------
describe("Fund", async function () {
it("should return the correct balance after funding", async function () {
const { multiSign, OWNERS } = await loadFixture(deployFixture);
const owner = OWNERS[0];
const value = parseEthers("10");
const tx = await multiSign.connect(owner).fund({ value });
expect(tx)
.to.emit(multiSign, "FoundsAdded")
.withArgs(owner.address, value);
expect(await ethers.provider.getBalance(multiSign.address)).to.equal(
value,
);
});
it("Should not fund from third-party.", async () => {
const { multiSign, OWNERS } = await loadFixture(deployFixture);
const owner = OWNERS[0];
const value = parseEthers("0");
let tx;
try {
tx = await multiSign.connect(owner).fund({ value });
} catch (error) {
expect(tx).to.revertedWith("MultSign: Zero value");
}
});
it("Should not fund from third-party.", async () => {
const { multiSign, notOwnerAccount } = await loadFixture(deployFixture);
const value = parseEthers("1");
let tx;
try {
tx = await multiSign.connect(notOwnerAccount).fund({ value });
} catch (error) {
expect(tx)
.to.revertedWithCustomError(multiSign, "NotAnOwner")
.withArgs(notOwnerAccount.address);
}
});
});
// * submitTransaction ------------------------------------------------------
describe("submitTransaction", async function () {
it("Should submit a transaction.", async () => {
const { OWNERS, multiSign, receiver } = await loadFixture(deployFixture);
for (const owner of OWNERS) {
const tx = await multiSign
.connect(owner)
.submitTransaction(receiver.address, parseEthers("1"), mintSignature);
expect(tx).to.emit(multiSign, "TransactionSubmitted");
expect(tx).to.emit(multiSign, "TransactionCreated");
expect(tx).to.emit(multiSign, "TransactionConfirmed");
const receipt = await tx.wait();
const txId = getTxIdFromEvents(receipt.events);
const confirmationCount = await multiSign.getConfirmationsCount(txId);
expect(confirmationCount.toNumber()).to.be.equal(1);
}
});
it("Should not submit from third-party.", async () => {
const { multiSign, notOwnerAccount, receiver } = await loadFixture(
deployFixture,
);
let tx;
try {
tx = await multiSign
.connect(notOwnerAccount)
.submitTransaction(receiver.address, parseEthers("1"), mintSignature);
} catch (error) {
expect(tx)
.to.revertedWithCustomError(multiSign, "NotAnOwner")
.withArgs(notOwnerAccount.address);
}
});
it("Should not submit with invalid `_to` value.", async () => {
const { OWNERS, multiSign } = await loadFixture(deployFixture);
// ! Zero address
let tx1;
try {
tx1 = await multiSign
.connect(OWNERS[0])
.submitTransaction("0x0", parseEthers("1"), mintSignature);
} catch (error) {
expect(tx1).to.revertedWith("MultiSign: Invalid address.");
}
// ! Contract address
let tx2;
try {
tx2 = await multiSign
.connect(OWNERS[0])
.submitTransaction(
multiSign.address,
parseEthers("1"),
mintSignature,
);
} catch (error) {
expect(tx2).to.revertedWith("MultiSign: Invalid address.");
}
});
it("Should not submit with invalid `_data` value.", async () => {
const { OWNERS, multiSign, receiver } = await loadFixture(deployFixture);
const owner = OWNERS[0];
let tx;
try {
tx = await multiSign
.connect(owner)
.submitTransaction(
receiver.address,
parseEthers("0"),
stringToBytes(""),
);
} catch (error) {
expect(tx).to.revertedWith("MultiSign: Invalid address.");
}
});
});
// * confirmTransaction -----------------------------------------------------
describe("confirmTransaction", async function () {
it("Should confirm a transaction.", async () => {
const { OWNERS, multiSign, receiver } = await loadFixture(deployFixture);
const [owner, other, other2, ...rest] = OWNERS;
const tx = await multiSign
.connect(owner)
.submitTransaction(receiver.address, parseEthers("1"), mintSignature);
const receipt = await tx.wait();
const txId = getTxIdFromEvents(receipt.events);
for (const owner of rest) {
const tx = await multiSign.connect(owner).confirmTransaction(txId);
expect(tx)
.to.emit(multiSign, "TransactionConfirmed")
.withArgs(owner.address, txId);
}
});
it("Should not confirm from third-party.", async () => {
const { OWNERS, multiSign, receiver, notOwnerAccount } =
await loadFixture(deployFixture);
const [owner] = OWNERS;
const submitTx = await multiSign
.connect(owner)
.submitTransaction(receiver.address, parseEthers("1"), mintSignature);
const receipt = await submitTx.wait();
const txId = getTxIdFromEvents(receipt.events);
let tx;
try {
tx = await multiSign.connect(notOwnerAccount).confirmTransaction(txId);
} catch (error) {
expect(tx)
.to.revertedWithCustomError(multiSign, "NotAnOwner")
.withArgs(notOwnerAccount.address);
}
});
it("Should not submit with inexistent `_txId` value.", async () => {
const { OWNERS, multiSign } = await loadFixture(deployFixture);
const [owner] = OWNERS;
let tx;
try {
tx = await multiSign.connect(owner).confirmTransaction(invalidTxId);
} catch (error) {
expect(tx)
.to.revertedWithCustomError(multiSign, "NotAnOwner")
.withArgs("MultiSign: Transaction does not exist.");
}
});
});
// * executeTransaction -----------------------------------------------------
describe("executeTransaction", async function () {
it("Should create and execute a transaction.", async () => {
const { OWNERS, multiSign, receiver } = await loadFixture(deployFixture);
const [ownerA, ownerB, ownerC, ...rest] = OWNERS;
let confirmationCount;
const txValue = parseEthers("1");
const fundValue = parseEthers("10");
// * Provisioning the contract with enough balance
await multiSign.connect(ownerA).fund({ value: fundValue });
const txA = await multiSign
.connect(ownerA)
.submitTransaction(receiver.address, txValue, mintSignature);
/*
* At this point we know that this `tx` have one confirmation as
* we test in `submitTransaction > Should submit a transaction`
**/
const receipt = await txA.wait();
const txId = getTxIdFromEvents(receipt.events);
await multiSign.connect(ownerB).confirmTransaction(txId);
confirmationCount = await multiSign.getConfirmationsCount(txId);
/*
* Must have 2 confirmations
**/
expect(confirmationCount.toNumber()).to.be.equal(2);
const txC = await multiSign.connect(ownerC).confirmTransaction(txId);
confirmationCount = await multiSign.getConfirmationsCount(txId);
/*
* As we set a threshold of 3, this confirmation should execute the
* transaction with id `txId`
**/
expect(txC)
.to.emit(multiSign, "TransactionExecuted")
.withArgs(txId, receiver.address, txValue);
expect(confirmationCount.toNumber()).to.be.equal(3);
});
it("Should not execute a valid transaction from third-party.", async () => {
const { OWNERS, multiSign, receiver, notOwnerAccount } =
await loadFixture(deployFixture);
const [owner] = OWNERS;
const tx = await multiSign
.connect(owner)
.submitTransaction(receiver.address, parseEthers("1"), mintSignature);
const receipt = await tx.wait();
const txId = getTxIdFromEvents(receipt.events);
let confirmTx;
try {
confirmTx = await multiSign
.connect(notOwnerAccount)
.executeTransaction(txId);
} catch (error) {
expect(confirmTx)
.to.revertedWithCustomError(multiSign, "NotAnOwner")
.withArgs("MultiSign: Transaction does not exist.");
}
});
it("Should not execute with inexistent `_txId` value.", async () => {
const { OWNERS, multiSign } = await loadFixture(deployFixture);
const [owner] = OWNERS;
let tx;
try {
tx = await multiSign.connect(owner).executeTransaction(invalidTxId);
} catch (error) {
expect(tx)
.to.revertedWithCustomError(multiSign, "NotAnOwner")
.withArgs("MultiSign: Transaction does not exist.");
}
});
});
});