aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/multisig/test/multi_sig_with_time_lock.ts
blob: 31c2155052e5b0a89cb96e29d139eade27c3324d (plain) (blame)
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
import {
    chaiSetup,
    constants,
    expectTransactionFailedAsync,
    expectTransactionFailedWithoutReasonAsync,
    increaseTimeAndMineBlockAsync,
    provider,
    txDefaults,
    web3Wrapper,
} from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { RevertReason } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import { LogWithDecodedArgs } from 'ethereum-types';
import * as _ from 'lodash';

import {
    MultiSigWalletWithTimeLockConfirmationEventArgs,
    MultiSigWalletWithTimeLockConfirmationTimeSetEventArgs,
    MultiSigWalletWithTimeLockContract,
    MultiSigWalletWithTimeLockExecutionEventArgs,
    MultiSigWalletWithTimeLockExecutionFailureEventArgs,
    MultiSigWalletWithTimeLockSubmissionEventArgs,
} from '../generated-wrappers/multi_sig_wallet_with_time_lock';
import { TestRejectEtherContract } from '../generated-wrappers/test_reject_ether';
import { artifacts } from '../src/artifacts';

import { MultiSigWrapper } from './utils/multi_sig_wrapper';

chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
// tslint:disable:no-unnecessary-type-assertion
describe('MultiSigWalletWithTimeLock', () => {
    let owners: string[];
    let notOwner: string;
    const REQUIRED_APPROVALS = new BigNumber(2);
    const SECONDS_TIME_LOCKED = new BigNumber(1000000);

    before(async () => {
        await blockchainLifecycle.startAsync();
    });
    after(async () => {
        await blockchainLifecycle.revertAsync();
    });
    before(async () => {
        const accounts = await web3Wrapper.getAvailableAddressesAsync();
        owners = [accounts[0], accounts[1], accounts[2]];
        notOwner = accounts[3];
    });

    let multiSig: MultiSigWalletWithTimeLockContract;
    let multiSigWrapper: MultiSigWrapper;

    beforeEach(async () => {
        await blockchainLifecycle.startAsync();
    });
    afterEach(async () => {
        await blockchainLifecycle.revertAsync();
    });

    describe('external_call', () => {
        it('should be internal', async () => {
            const secondsTimeLocked = new BigNumber(0);
            multiSig = await MultiSigWalletWithTimeLockContract.deployFrom0xArtifactAsync(
                artifacts.MultiSigWalletWithTimeLock,
                provider,
                txDefaults,
                owners,
                REQUIRED_APPROVALS,
                secondsTimeLocked,
            );
            expect(_.isUndefined((multiSig as any).external_call)).to.be.equal(true);
        });
    });
    describe('confirmTransaction', () => {
        let txId: BigNumber;
        beforeEach(async () => {
            const secondsTimeLocked = new BigNumber(0);
            multiSig = await MultiSigWalletWithTimeLockContract.deployFrom0xArtifactAsync(
                artifacts.MultiSigWalletWithTimeLock,
                provider,
                txDefaults,
                owners,
                REQUIRED_APPROVALS,
                secondsTimeLocked,
            );
            multiSigWrapper = new MultiSigWrapper(multiSig, provider);
            const destination = notOwner;
            const data = constants.NULL_BYTES;
            const txReceipt = await multiSigWrapper.submitTransactionAsync(destination, data, owners[0]);
            txId = (txReceipt.logs[0] as LogWithDecodedArgs<MultiSigWalletWithTimeLockSubmissionEventArgs>).args
                .transactionId;
        });
        it('should revert if called by a non-owner', async () => {
            await expectTransactionFailedWithoutReasonAsync(multiSigWrapper.confirmTransactionAsync(txId, notOwner));
        });
        it('should revert if transaction does not exist', async () => {
            const nonexistentTxId = new BigNumber(123456789);
            await expectTransactionFailedWithoutReasonAsync(
                multiSigWrapper.confirmTransactionAsync(nonexistentTxId, owners[1]),
            );
        });
        it('should revert if transaction is already confirmed by caller', async () => {
            await expectTransactionFailedWithoutReasonAsync(multiSigWrapper.confirmTransactionAsync(txId, owners[0]));
        });
        it('should confirm transaction for caller and log a Confirmation event', async () => {
            const txReceipt = await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
            const log = txReceipt.logs[0] as LogWithDecodedArgs<MultiSigWalletWithTimeLockConfirmationEventArgs>;
            expect(log.event).to.be.equal('Confirmation');
            expect(log.args.sender).to.be.equal(owners[1]);
            expect(log.args.transactionId).to.be.bignumber.equal(txId);
        });
        it('should revert if fully confirmed', async () => {
            await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
            await expectTransactionFailedAsync(
                multiSigWrapper.confirmTransactionAsync(txId, owners[2]),
                RevertReason.TxFullyConfirmed,
            );
        });
        it('should set the confirmation time of the transaction if it becomes fully confirmed', async () => {
            const txReceipt = await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
            const blockNum = await web3Wrapper.getBlockNumberAsync();
            const timestamp = new BigNumber(await web3Wrapper.getBlockTimestampAsync(blockNum));
            const log = txReceipt.logs[1] as LogWithDecodedArgs<MultiSigWalletWithTimeLockConfirmationTimeSetEventArgs>;
            expect(log.args.confirmationTime).to.be.bignumber.equal(timestamp);
            expect(log.args.transactionId).to.be.bignumber.equal(txId);
        });
    });
    describe('executeTransaction', () => {
        let txId: BigNumber;
        const secondsTimeLocked = new BigNumber(1000000);
        beforeEach(async () => {
            multiSig = await MultiSigWalletWithTimeLockContract.deployFrom0xArtifactAsync(
                artifacts.MultiSigWalletWithTimeLock,
                provider,
                txDefaults,
                owners,
                REQUIRED_APPROVALS,
                secondsTimeLocked,
            );
            multiSigWrapper = new MultiSigWrapper(multiSig, provider);
            const destination = notOwner;
            const data = constants.NULL_BYTES;
            const txReceipt = await multiSigWrapper.submitTransactionAsync(destination, data, owners[0]);
            txId = (txReceipt.logs[0] as LogWithDecodedArgs<MultiSigWalletWithTimeLockSubmissionEventArgs>).args
                .transactionId;
        });
        it('should revert if transaction has not been fully confirmed', async () => {
            await increaseTimeAndMineBlockAsync(secondsTimeLocked.toNumber());
            await expectTransactionFailedAsync(
                multiSigWrapper.executeTransactionAsync(txId, owners[1]),
                RevertReason.TxNotFullyConfirmed,
            );
        });
        it('should revert if time lock has not passed', async () => {
            await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
            await expectTransactionFailedAsync(
                multiSigWrapper.executeTransactionAsync(txId, owners[1]),
                RevertReason.TimeLockIncomplete,
            );
        });
        it('should execute a transaction and log an Execution event if successful and called by owner', async () => {
            await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
            await increaseTimeAndMineBlockAsync(secondsTimeLocked.toNumber());
            const txReceipt = await multiSigWrapper.executeTransactionAsync(txId, owners[1]);
            const log = txReceipt.logs[0] as LogWithDecodedArgs<MultiSigWalletWithTimeLockExecutionEventArgs>;
            expect(log.event).to.be.equal('Execution');
            expect(log.args.transactionId).to.be.bignumber.equal(txId);
        });
        it('should execute a transaction and log an Execution event if successful and called by non-owner', async () => {
            await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
            await increaseTimeAndMineBlockAsync(secondsTimeLocked.toNumber());
            const txReceipt = await multiSigWrapper.executeTransactionAsync(txId, notOwner);
            const log = txReceipt.logs[0] as LogWithDecodedArgs<MultiSigWalletWithTimeLockExecutionEventArgs>;
            expect(log.event).to.be.equal('Execution');
            expect(log.args.transactionId).to.be.bignumber.equal(txId);
        });
        it('should revert if a required confirmation is revoked before executeTransaction is called', async () => {
            await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
            await increaseTimeAndMineBlockAsync(secondsTimeLocked.toNumber());
            await multiSigWrapper.revokeConfirmationAsync(txId, owners[0]);
            await expectTransactionFailedAsync(
                multiSigWrapper.executeTransactionAsync(txId, owners[1]),
                RevertReason.TxNotFullyConfirmed,
            );
        });
        it('should revert if transaction has been executed', async () => {
            await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
            await increaseTimeAndMineBlockAsync(secondsTimeLocked.toNumber());
            const txReceipt = await multiSigWrapper.executeTransactionAsync(txId, owners[1]);
            const log = txReceipt.logs[0] as LogWithDecodedArgs<MultiSigWalletWithTimeLockExecutionEventArgs>;
            expect(log.args.transactionId).to.be.bignumber.equal(txId);
            await expectTransactionFailedWithoutReasonAsync(multiSigWrapper.executeTransactionAsync(txId, owners[1]));
        });
        it("should log an ExecutionFailure event and not update the transaction's execution state if unsuccessful", async () => {
            const contractWithoutFallback = await TestRejectEtherContract.deployFrom0xArtifactAsync(
                artifacts.TestRejectEther,
                provider,
                txDefaults,
            );
            const data = constants.NULL_BYTES;
            const value = new BigNumber(10);
            const submissionTxReceipt = await multiSigWrapper.submitTransactionAsync(
                contractWithoutFallback.address,
                data,
                owners[0],
                { value },
            );
            const newTxId = (submissionTxReceipt.logs[0] as LogWithDecodedArgs<
                MultiSigWalletWithTimeLockSubmissionEventArgs
            >).args.transactionId;
            await multiSigWrapper.confirmTransactionAsync(newTxId, owners[1]);
            await increaseTimeAndMineBlockAsync(secondsTimeLocked.toNumber());
            const txReceipt = await multiSigWrapper.executeTransactionAsync(newTxId, owners[1]);
            const executionFailureLog = txReceipt.logs[0] as LogWithDecodedArgs<
                MultiSigWalletWithTimeLockExecutionFailureEventArgs
            >;
            expect(executionFailureLog.event).to.be.equal('ExecutionFailure');
            expect(executionFailureLog.args.transactionId).to.be.bignumber.equal(newTxId);
        });
    });
    describe('changeTimeLock', () => {
        describe('initially non-time-locked', async () => {
            before(async () => {
                await blockchainLifecycle.startAsync();
            });
            after(async () => {
                await blockchainLifecycle.revertAsync();
            });
            before('deploy a wallet', async () => {
                const secondsTimeLocked = new BigNumber(0);
                multiSig = await MultiSigWalletWithTimeLockContract.deployFrom0xArtifactAsync(
                    artifacts.MultiSigWalletWithTimeLock,
                    provider,
                    txDefaults,
                    owners,
                    REQUIRED_APPROVALS,
                    secondsTimeLocked,
                );
                multiSigWrapper = new MultiSigWrapper(multiSig, provider);
            });

            it('should throw when not called by wallet', async () => {
                return expectTransactionFailedWithoutReasonAsync(
                    multiSig.changeTimeLock.sendTransactionAsync(SECONDS_TIME_LOCKED, { from: owners[0] }),
                );
            });

            it('should throw without enough confirmations', async () => {
                const destination = multiSig.address;
                const changeTimeLockData = multiSig.changeTimeLock.getABIEncodedTransactionData(SECONDS_TIME_LOCKED);
                const res = await multiSigWrapper.submitTransactionAsync(destination, changeTimeLockData, owners[0]);
                const log = res.logs[0] as LogWithDecodedArgs<MultiSigWalletWithTimeLockSubmissionEventArgs>;
                const txId = log.args.transactionId;
                return expectTransactionFailedAsync(
                    multiSig.executeTransaction.sendTransactionAsync(txId, { from: owners[0] }),
                    RevertReason.TxNotFullyConfirmed,
                );
            });

            it('should set confirmation time with enough confirmations', async () => {
                const destination = multiSig.address;
                const changeTimeLockData = multiSig.changeTimeLock.getABIEncodedTransactionData(SECONDS_TIME_LOCKED);
                const subRes = await multiSigWrapper.submitTransactionAsync(destination, changeTimeLockData, owners[0]);
                const subLog = subRes.logs[0] as LogWithDecodedArgs<MultiSigWalletWithTimeLockSubmissionEventArgs>;
                const txId = subLog.args.transactionId;

                const confirmRes = await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
                expect(confirmRes.logs).to.have.length(2);

                const blockNum = await web3Wrapper.getBlockNumberAsync();
                const blockInfo = await web3Wrapper.getBlockIfExistsAsync(blockNum);
                if (_.isUndefined(blockInfo)) {
                    throw new Error(`Unexpectedly failed to fetch block at #${blockNum}`);
                }
                const timestamp = new BigNumber(blockInfo.timestamp);
                const confirmationTimeBigNum = new BigNumber(await multiSig.confirmationTimes.callAsync(txId));

                expect(timestamp).to.be.bignumber.equal(confirmationTimeBigNum);
            });

            it('should be executable with enough confirmations and secondsTimeLocked of 0', async () => {
                const destination = multiSig.address;
                const changeTimeLockData = multiSig.changeTimeLock.getABIEncodedTransactionData(SECONDS_TIME_LOCKED);
                const subRes = await multiSigWrapper.submitTransactionAsync(destination, changeTimeLockData, owners[0]);
                const subLog = subRes.logs[0] as LogWithDecodedArgs<MultiSigWalletWithTimeLockSubmissionEventArgs>;
                const txId = subLog.args.transactionId;

                await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
                await multiSigWrapper.executeTransactionAsync(txId, owners[1]);

                const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.callAsync());
                expect(secondsTimeLocked).to.be.bignumber.equal(SECONDS_TIME_LOCKED);
            });
        });
        describe('initially time-locked', async () => {
            before(async () => {
                await blockchainLifecycle.startAsync();
            });
            after(async () => {
                await blockchainLifecycle.revertAsync();
            });
            let txId: BigNumber;
            const newSecondsTimeLocked = new BigNumber(0);
            before('deploy a wallet, submit transaction to change timelock, and confirm the transaction', async () => {
                multiSig = await MultiSigWalletWithTimeLockContract.deployFrom0xArtifactAsync(
                    artifacts.MultiSigWalletWithTimeLock,
                    provider,
                    txDefaults,
                    owners,
                    REQUIRED_APPROVALS,
                    SECONDS_TIME_LOCKED,
                );
                multiSigWrapper = new MultiSigWrapper(multiSig, provider);

                const changeTimeLockData = multiSig.changeTimeLock.getABIEncodedTransactionData(newSecondsTimeLocked);
                const res = await multiSigWrapper.submitTransactionAsync(
                    multiSig.address,
                    changeTimeLockData,
                    owners[0],
                );
                const log = res.logs[0] as LogWithDecodedArgs<MultiSigWalletWithTimeLockSubmissionEventArgs>;
                txId = log.args.transactionId;
                await multiSigWrapper.confirmTransactionAsync(txId, owners[1]);
            });

            it('should throw if it has enough confirmations but is not past the time lock', async () => {
                return expectTransactionFailedAsync(
                    multiSig.executeTransaction.sendTransactionAsync(txId, { from: owners[0] }),
                    RevertReason.TimeLockIncomplete,
                );
            });

            it('should execute if it has enough confirmations and is past the time lock', async () => {
                await increaseTimeAndMineBlockAsync(SECONDS_TIME_LOCKED.toNumber());
                await web3Wrapper.awaitTransactionSuccessAsync(
                    await multiSig.executeTransaction.sendTransactionAsync(txId, { from: owners[0] }),
                    constants.AWAIT_TRANSACTION_MINED_MS,
                );

                const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.callAsync());
                expect(secondsTimeLocked).to.be.bignumber.equal(newSecondsTimeLocked);
            });
        });
    });
});
// tslint:enable:no-unnecessary-type-assertion