This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 467
/
Copy pathtransactions_unit_tests.ts
703 lines (656 loc) · 38 KB
/
transactions_unit_tests.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import { blockchainTests, constants, describe, expect, transactionHashUtils } from '@0x/contracts-test-utils';
import { EIP712DomainWithDefaultSchema, ZeroExTransaction } from '@0x/types';
import { BigNumber, ExchangeRevertErrors, hexUtils, StringRevertError } from '@0x/utils';
import { LogWithDecodedArgs } from 'ethereum-types';
import * as _ from 'lodash';
import { artifacts } from './artifacts';
import { TestTransactionsContract, TestTransactionsTransactionExecutionEventArgs } from './wrappers';
blockchainTests.resets('Transaction Unit Tests', ({ provider, web3Wrapper, txDefaults }) => {
let transactionsContract: TestTransactionsContract;
let accounts: string[];
let domain: EIP712DomainWithDefaultSchema;
const randomSignature = () => hexUtils.random(66);
const EMPTY_ZERO_EX_TRANSACTION = {
salt: constants.ZERO_AMOUNT,
expirationTimeSeconds: constants.ZERO_AMOUNT,
gasPrice: constants.ZERO_AMOUNT,
signerAddress: constants.NULL_ADDRESS,
data: constants.NULL_BYTES,
domain: {
verifyingContract: constants.NULL_ADDRESS,
chainId: 0,
},
};
const DEADBEEF_RETURN_DATA = '0xdeadbeef';
const INVALID_SIGNATURE = '0x0000';
before(async () => {
// A list of available addresses to use during testing.
accounts = await web3Wrapper.getAvailableAddressesAsync();
// Deploy the transaction test contract.
transactionsContract = await TestTransactionsContract.deployFrom0xArtifactAsync(
artifacts.TestTransactions,
provider,
txDefaults,
{},
);
// Set the default domain.
domain = {
verifyingContract: transactionsContract.address,
chainId: 1337,
};
});
/**
* Generates calldata for a call to `executable()` in the `TestTransactions` contract.
*/
function getExecutableCallData(shouldSucceed: boolean, callData: string, returnData: string): string {
return (transactionsContract as any)
.executable(shouldSucceed, callData, returnData)
.getABIEncodedTransactionData();
}
interface GenerateZeroExTransactionParams {
salt?: BigNumber;
expirationTimeSeconds?: BigNumber;
gasPrice?: BigNumber;
signerAddress?: string;
data?: string;
domain?: EIP712DomainWithDefaultSchema;
shouldSucceed?: boolean;
callData?: string;
returnData?: string;
}
async function generateZeroExTransactionAsync(
opts: GenerateZeroExTransactionParams = {},
): Promise<ZeroExTransaction> {
const shouldSucceed = opts.shouldSucceed === undefined ? true : opts.shouldSucceed;
const callData = opts.callData === undefined ? constants.NULL_BYTES : opts.callData;
const returnData = opts.returnData === undefined ? constants.NULL_BYTES : opts.returnData;
const data = opts.data === undefined ? getExecutableCallData(shouldSucceed, callData, returnData) : opts.data;
const gasPrice = opts.gasPrice === undefined ? new BigNumber(constants.DEFAULT_GAS_PRICE) : opts.gasPrice;
const _domain = opts.domain === undefined ? domain : opts.domain;
const expirationTimeSeconds =
opts.expirationTimeSeconds === undefined ? constants.MAX_UINT256 : opts.expirationTimeSeconds;
const transaction = {
...EMPTY_ZERO_EX_TRANSACTION,
...opts,
data,
expirationTimeSeconds,
domain: _domain,
gasPrice,
};
return transaction;
}
describe('batchExecuteTransaction', () => {
it('should revert if the only call to executeTransaction fails', async () => {
// Create an expired transaction that will fail when used to call `batchExecuteTransactions()`.
const transaction = await generateZeroExTransactionAsync({ shouldSucceed: false });
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
// Create the StringRevertError that reflects the returndata that will be returned by the failed transaction.
const executableError = new StringRevertError('EXECUTABLE_FAILED');
const expectedError = new ExchangeRevertErrors.TransactionExecutionError(
transactionHash,
executableError.encode(),
);
// Call the `batchExecuteTransactions()` function and ensure that it reverts with the expected revert error.
const tx = transactionsContract
.batchExecuteTransactions([transaction], [randomSignature()])
.awaitTransactionSuccessAsync();
return expect(tx).to.revertWith(expectedError);
});
it('should revert if the second call to executeTransaction fails', async () => {
// Create a transaction that will succeed when used to call `batchExecuteTransactions()`.
const transaction1 = await generateZeroExTransactionAsync();
// Create a transaction that will fail when used to call `batchExecuteTransactions()` because the call to executable will fail.
const transaction2 = await generateZeroExTransactionAsync({ shouldSucceed: false });
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction2);
// Create the StringRevertError that reflects the returndata that will be returned by the failed transaction.
const executableError = new StringRevertError('EXECUTABLE_FAILED');
const expectedError = new ExchangeRevertErrors.TransactionExecutionError(
transactionHash,
executableError.encode(),
);
// Call the `batchExecuteTransactions()` function and ensure that it reverts with the expected revert error.
const tx = transactionsContract
.batchExecuteTransactions([transaction1, transaction2], [randomSignature(), randomSignature()])
.awaitTransactionSuccessAsync();
return expect(tx).to.revertWith(expectedError);
});
it('should revert if the first call to executeTransaction fails', async () => {
// Create a transaction that will fail when used to call `batchExecuteTransactions()` because the call to executable will fail.
const transaction1 = await generateZeroExTransactionAsync({ shouldSucceed: false });
// Create a transaction that will succeed when used to call `batchExecuteTransactions()`.
const transaction2 = await generateZeroExTransactionAsync();
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction1);
// Create the StringRevertError that reflects the returndata that will be returned by the failed transaction.
const executableError = new StringRevertError('EXECUTABLE_FAILED');
const expectedError = new ExchangeRevertErrors.TransactionExecutionError(
transactionHash,
executableError.encode(),
);
// Call the `batchExecuteTransactions()` function and ensure that it reverts with the expected revert error.
const tx = transactionsContract
.batchExecuteTransactions([transaction1, transaction2], [randomSignature(), randomSignature()])
.awaitTransactionSuccessAsync();
return expect(tx).to.revertWith(expectedError);
});
it('should revert if the same transaction is executed twice in a batch', async () => {
// Create a transaction that will succeed when used to call `batchExecuteTransactions()`.
const transaction1 = await generateZeroExTransactionAsync({ signerAddress: accounts[1] });
// Duplicate the first transaction. This should cause the call to `batchExecuteTransactions()` to fail
// because this transaction will have the same order hash as transaction1.
const transaction2 = transaction1;
const transactionHash2 = transactionHashUtils.getTransactionHashHex(transaction2);
// Call the `batchExecuteTransactions()` function and ensure that it reverts with the expected revert error.
const expectedError = new ExchangeRevertErrors.TransactionError(
ExchangeRevertErrors.TransactionErrorCode.AlreadyExecuted,
transactionHash2,
);
const tx = transactionsContract
.batchExecuteTransactions([transaction1, transaction2], [randomSignature(), randomSignature()])
.awaitTransactionSuccessAsync({
from: accounts[0],
});
return expect(tx).to.revertWith(expectedError);
});
it('should succeed if the only call to executeTransaction succeeds', async () => {
// Create a transaction that will succeed when used to call `batchExecuteTransactions()`.
const transaction = await generateZeroExTransactionAsync({
signerAddress: accounts[1],
returnData: DEADBEEF_RETURN_DATA,
});
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const validSignature = randomSignature();
const contractFn = transactionsContract.batchExecuteTransactions([transaction], [validSignature]);
const result = await contractFn.callAsync({ from: accounts[0] });
const receipt = await contractFn.awaitTransactionSuccessAsync({ from: accounts[0] });
expect(result.length).to.be.eq(1);
const returnData = transactionsContract.getABIDecodedReturnData<void>('executeTransaction', result[0]);
expect(returnData).to.equal(DEADBEEF_RETURN_DATA);
// Ensure that the correct number of events were logged.
const logs = receipt.logs as Array<LogWithDecodedArgs<TestTransactionsTransactionExecutionEventArgs>>;
expect(logs.length).to.be.eq(2);
// Ensure that the correct events were logged.
expect(logs[0].event).to.be.eq('ExecutableCalled');
expect(logs[0].args.data).to.be.eq(constants.NULL_BYTES);
expect(logs[0].args.contextAddress).to.be.eq(accounts[1]);
expect(logs[0].args.returnData).to.be.eq(DEADBEEF_RETURN_DATA);
expect(logs[1].event).to.be.eq('TransactionExecution');
expect(logs[1].args.transactionHash).to.eq(transactionHash);
});
it('should succeed if the both calls to executeTransaction succeed', async () => {
// Create two transactions that will succeed when used to call `batchExecuteTransactions()`.
const transaction1 = await generateZeroExTransactionAsync({
signerAddress: accounts[0],
returnData: DEADBEEF_RETURN_DATA,
});
const returnData2 = '0xbeefdead';
const transaction2 = await generateZeroExTransactionAsync({
signerAddress: accounts[1],
returnData: returnData2,
});
const transactionHash1 = transactionHashUtils.getTransactionHashHex(transaction1);
const transactionHash2 = transactionHashUtils.getTransactionHashHex(transaction2);
const contractFn = transactionsContract.batchExecuteTransactions(
[transaction1, transaction2],
[randomSignature(), randomSignature()],
);
const result = await contractFn.callAsync({ from: accounts[0] });
const receipt = await contractFn.awaitTransactionSuccessAsync({ from: accounts[0] });
expect(result.length).to.be.eq(2);
expect(transactionsContract.getABIDecodedReturnData('executeTransaction', result[0])).to.equal(
DEADBEEF_RETURN_DATA,
);
expect(transactionsContract.getABIDecodedReturnData('executeTransaction', result[1])).to.equal(returnData2);
// Verify that the correct number of events were logged.
const logs = receipt.logs as Array<LogWithDecodedArgs<TestTransactionsTransactionExecutionEventArgs>>;
expect(logs.length).to.be.eq(4);
// Ensure that the correct events were logged.
expect(logs[0].event).to.be.eq('ExecutableCalled');
expect(logs[0].args.data).to.be.eq(constants.NULL_BYTES);
expect(logs[0].args.returnData).to.be.eq(DEADBEEF_RETURN_DATA);
expect(logs[0].args.contextAddress).to.be.eq(constants.NULL_ADDRESS);
expect(logs[1].event).to.be.eq('TransactionExecution');
expect(logs[1].args.transactionHash).to.eq(transactionHash1);
expect(logs[2].event).to.be.eq('ExecutableCalled');
expect(logs[2].args.data).to.be.eq(constants.NULL_BYTES);
expect(logs[2].args.returnData).to.be.eq('0xbeefdead');
expect(logs[2].args.contextAddress).to.be.eq(accounts[1]);
expect(logs[3].event).to.be.eq('TransactionExecution');
expect(logs[3].args.transactionHash).to.eq(transactionHash2);
});
it('should not allow recursion if currentContextAddress is already set', async () => {
const innerTransaction1 = await generateZeroExTransactionAsync({ signerAddress: accounts[0] });
const innerTransaction2 = await generateZeroExTransactionAsync({ signerAddress: accounts[1] });
const innerBatchExecuteTransaction = await generateZeroExTransactionAsync({
signerAddress: accounts[2],
callData: transactionsContract
.batchExecuteTransactions(
[innerTransaction1, innerTransaction2],
[randomSignature(), randomSignature()],
)
.getABIEncodedTransactionData(),
});
const outerExecuteTransaction = await generateZeroExTransactionAsync({
signerAddress: accounts[1],
callData: transactionsContract
.executeTransaction(innerBatchExecuteTransaction, randomSignature())
.getABIEncodedTransactionData(),
});
const innerBatchExecuteTransactionHash = transactionHashUtils.getTransactionHashHex(
innerBatchExecuteTransaction,
);
const innerExpectedError = new ExchangeRevertErrors.TransactionInvalidContextError(
innerBatchExecuteTransactionHash,
accounts[1],
);
const outerExecuteTransactionHash = transactionHashUtils.getTransactionHashHex(outerExecuteTransaction);
const outerExpectedError = new ExchangeRevertErrors.TransactionExecutionError(
outerExecuteTransactionHash,
innerExpectedError.encode(),
);
const tx = transactionsContract
.batchExecuteTransactions([outerExecuteTransaction], [randomSignature()])
.awaitTransactionSuccessAsync({ from: accounts[2] });
return expect(tx).to.revertWith(outerExpectedError);
});
it('should allow recursion as long as currentContextAddress is not set', async () => {
const innerTransaction1 = await generateZeroExTransactionAsync({ signerAddress: accounts[0] });
const innerTransaction2 = await generateZeroExTransactionAsync({ signerAddress: accounts[1] });
// From this point on, all transactions and calls will have the same sender, which does not change currentContextAddress when called
const innerBatchExecuteTransaction = await generateZeroExTransactionAsync({
signerAddress: accounts[2],
callData: transactionsContract
.batchExecuteTransactions(
[innerTransaction1, innerTransaction2],
[randomSignature(), randomSignature()],
)
.getABIEncodedTransactionData(),
});
const outerExecuteTransaction = await generateZeroExTransactionAsync({
signerAddress: accounts[2],
callData: transactionsContract
.executeTransaction(innerBatchExecuteTransaction, randomSignature())
.getABIEncodedTransactionData(),
});
return expect(
transactionsContract
.batchExecuteTransactions([outerExecuteTransaction], [randomSignature()])
.awaitTransactionSuccessAsync({ from: accounts[2] }),
).to.be.fulfilled('');
});
});
describe('executeTransaction', () => {
function getExecuteTransactionCallData(transaction: ZeroExTransaction, signature: string): string {
return (transactionsContract as any)
.executeTransaction(transaction, signature)
.getABIEncodedTransactionData();
}
it('should revert if the current time is past the expiration time', async () => {
const transaction = await generateZeroExTransactionAsync({
expirationTimeSeconds: constants.ZERO_AMOUNT,
});
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const expectedError = new ExchangeRevertErrors.TransactionError(
ExchangeRevertErrors.TransactionErrorCode.Expired,
transactionHash,
);
const tx = transactionsContract
.executeTransaction(transaction, randomSignature())
.awaitTransactionSuccessAsync();
return expect(tx).to.revertWith(expectedError);
});
it('should revert if the transaction is submitted with a gasPrice that does not equal the required gasPrice', async () => {
const transaction = await generateZeroExTransactionAsync();
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const actualGasPrice = transaction.gasPrice.plus(1);
const expectedError = new ExchangeRevertErrors.TransactionGasPriceError(
transactionHash,
actualGasPrice,
transaction.gasPrice,
);
const tx = transactionsContract
.executeTransaction(transaction, randomSignature())
.awaitTransactionSuccessAsync({
gasPrice: actualGasPrice,
});
return expect(tx).to.revertWith(expectedError);
});
it('should revert if reentrancy occurs in the middle of an executeTransaction call and msg.sender != signer for both calls', async () => {
const validSignature = randomSignature();
const innerTransaction = await generateZeroExTransactionAsync({ signerAddress: accounts[0] });
const innerTransactionHash = transactionHashUtils.getTransactionHashHex(innerTransaction);
const outerTransaction = await generateZeroExTransactionAsync({
signerAddress: accounts[0],
callData: getExecuteTransactionCallData(innerTransaction, validSignature),
returnData: DEADBEEF_RETURN_DATA,
});
const outerTransactionHash = transactionHashUtils.getTransactionHashHex(outerTransaction);
const errorData = new ExchangeRevertErrors.TransactionInvalidContextError(
innerTransactionHash,
accounts[0],
).encode();
const expectedError = new ExchangeRevertErrors.TransactionExecutionError(outerTransactionHash, errorData);
const tx = transactionsContract
.executeTransaction(outerTransaction, validSignature)
.awaitTransactionSuccessAsync({
from: accounts[1], // Different then the signing addresses
});
return expect(tx).to.revertWith(expectedError);
});
it('should revert if reentrancy occurs in the middle of an executeTransaction call and msg.sender != signer and then msg.sender == signer', async () => {
const validSignature = randomSignature();
const innerTransaction = await generateZeroExTransactionAsync({ signerAddress: accounts[1] });
const innerTransactionHash = transactionHashUtils.getTransactionHashHex(innerTransaction);
const outerTransaction = await generateZeroExTransactionAsync({
signerAddress: accounts[0],
callData: getExecuteTransactionCallData(innerTransaction, validSignature),
returnData: DEADBEEF_RETURN_DATA,
});
const outerTransactionHash = transactionHashUtils.getTransactionHashHex(outerTransaction);
const errorData = new ExchangeRevertErrors.TransactionInvalidContextError(
innerTransactionHash,
accounts[0],
).encode();
const expectedError = new ExchangeRevertErrors.TransactionExecutionError(outerTransactionHash, errorData);
const tx = transactionsContract
.executeTransaction(outerTransaction, validSignature)
.awaitTransactionSuccessAsync({
from: accounts[1], // Different then the signing addresses
});
return expect(tx).to.revertWith(expectedError);
});
it('should allow reentrancy in the middle of an executeTransaction call if msg.sender == signer for both calls', async () => {
const validSignature = randomSignature();
const innerTransaction = await generateZeroExTransactionAsync({ signerAddress: accounts[0] });
const outerTransaction = await generateZeroExTransactionAsync({
signerAddress: accounts[0],
callData: getExecuteTransactionCallData(innerTransaction, validSignature),
returnData: DEADBEEF_RETURN_DATA,
});
return expect(
transactionsContract.executeTransaction(outerTransaction, validSignature).awaitTransactionSuccessAsync({
from: accounts[0],
}),
).to.be.fulfilled('');
});
it('should allow reentrancy in the middle of an executeTransaction call if msg.sender == signer and then msg.sender != signer', async () => {
const validSignature = randomSignature();
const innerTransaction = await generateZeroExTransactionAsync({ signerAddress: accounts[1] });
const outerTransaction = await generateZeroExTransactionAsync({
signerAddress: accounts[0],
callData: getExecuteTransactionCallData(innerTransaction, validSignature),
returnData: DEADBEEF_RETURN_DATA,
});
return expect(
transactionsContract.executeTransaction(outerTransaction, validSignature).awaitTransactionSuccessAsync({
from: accounts[0],
}),
).to.be.fulfilled('');
});
it('should revert if the transaction has been executed previously', async () => {
const validSignature = randomSignature();
const transaction = await generateZeroExTransactionAsync();
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
// Use the transaction in execute transaction.
await expect(
transactionsContract.executeTransaction(transaction, validSignature).awaitTransactionSuccessAsync(),
).to.be.fulfilled('');
// Use the same transaction to make another call
const expectedError = new ExchangeRevertErrors.TransactionError(
ExchangeRevertErrors.TransactionErrorCode.AlreadyExecuted,
transactionHash,
);
const tx = transactionsContract
.executeTransaction(transaction, validSignature)
.awaitTransactionSuccessAsync();
return expect(tx).to.revertWith(expectedError);
});
it('should revert if the signer != msg.sender and the signature is not valid', async () => {
const transaction = await generateZeroExTransactionAsync({ signerAddress: accounts[1] });
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const expectedError = new ExchangeRevertErrors.SignatureError(
ExchangeRevertErrors.SignatureErrorCode.BadTransactionSignature,
transactionHash,
accounts[1],
INVALID_SIGNATURE,
);
const tx = transactionsContract
.executeTransaction(transaction, INVALID_SIGNATURE)
.awaitTransactionSuccessAsync({
from: accounts[0],
});
return expect(tx).to.revertWith(expectedError);
});
it('should revert if the signer == msg.sender but the delegatecall fails', async () => {
// This calldata is encoded to fail when it hits the executable function.
const transaction = await generateZeroExTransactionAsync({
signerAddress: accounts[1],
shouldSucceed: false,
});
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const executableError = new StringRevertError('EXECUTABLE_FAILED');
const expectedError = new ExchangeRevertErrors.TransactionExecutionError(
transactionHash,
executableError.encode(),
);
const tx = transactionsContract
.executeTransaction(transaction, randomSignature())
.awaitTransactionSuccessAsync({
from: accounts[1],
});
return expect(tx).to.revertWith(expectedError);
});
it('should revert if the signer != msg.sender and the signature is valid but the delegatecall fails', async () => {
// This calldata is encoded to fail when it hits the executable function.
const transaction = await generateZeroExTransactionAsync({
signerAddress: accounts[1],
shouldSucceed: false,
});
const validSignature = randomSignature(); // Valid because length != 2
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const executableError = new StringRevertError('EXECUTABLE_FAILED');
const expectedError = new ExchangeRevertErrors.TransactionExecutionError(
transactionHash,
executableError.encode(),
);
const tx = transactionsContract
.executeTransaction(transaction, validSignature)
.awaitTransactionSuccessAsync({
from: accounts[0],
});
return expect(tx).to.revertWith(expectedError);
});
it('should succeed with the correct return hash and event emitted when msg.sender != signer', async () => {
// This calldata is encoded to succeed when it hits the executable function.
const validSignature = randomSignature(); // Valid because length != 2
const transaction = await generateZeroExTransactionAsync({
signerAddress: accounts[1],
returnData: DEADBEEF_RETURN_DATA,
});
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const contractFn = transactionsContract.executeTransaction(transaction, validSignature);
const result = await contractFn.callAsync({ from: accounts[0] });
const receipt = await contractFn.awaitTransactionSuccessAsync({ from: accounts[0] });
expect(transactionsContract.getABIDecodedReturnData('executeTransaction', result)).to.equal(
DEADBEEF_RETURN_DATA,
);
// Ensure that the correct number of events were logged.
const logs = receipt.logs as Array<LogWithDecodedArgs<TestTransactionsTransactionExecutionEventArgs>>;
expect(logs.length).to.be.eq(2);
// Ensure that the correct events were logged.
expect(logs[0].event).to.be.eq('ExecutableCalled');
expect(logs[0].args.data).to.be.eq(constants.NULL_BYTES);
expect(logs[0].args.returnData).to.be.eq(DEADBEEF_RETURN_DATA);
expect(logs[0].args.contextAddress).to.be.eq(accounts[1]);
expect(logs[1].event).to.be.eq('TransactionExecution');
expect(logs[1].args.transactionHash).to.eq(transactionHash);
});
it('should succeed with the correct return hash and event emitted when msg.sender == signer', async () => {
// This calldata is encoded to succeed when it hits the executable function.
const validSignature = randomSignature(); // Valid because length != 2
const transaction = await generateZeroExTransactionAsync({
signerAddress: accounts[0],
returnData: DEADBEEF_RETURN_DATA,
});
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const contractFn = transactionsContract.executeTransaction(transaction, validSignature);
const result = await contractFn.callAsync({ from: accounts[0] });
const receipt = await contractFn.awaitTransactionSuccessAsync({ from: accounts[0] });
expect(transactionsContract.getABIDecodedReturnData('executeTransaction', result)).to.equal(
DEADBEEF_RETURN_DATA,
);
// Ensure that the correct number of events were logged.
const logs = receipt.logs as Array<LogWithDecodedArgs<TestTransactionsTransactionExecutionEventArgs>>;
expect(logs.length).to.be.eq(2);
// Ensure that the correct events were logged.
expect(logs[0].event).to.be.eq('ExecutableCalled');
expect(logs[0].args.data).to.be.eq(constants.NULL_BYTES);
expect(logs[0].args.returnData).to.be.eq(DEADBEEF_RETURN_DATA);
expect(logs[0].args.contextAddress).to.be.eq(constants.NULL_ADDRESS);
expect(logs[1].event).to.be.eq('TransactionExecution');
expect(logs[1].args.transactionHash).to.eq(transactionHash);
});
});
blockchainTests.resets('assertExecutableTransaction', () => {
it('should revert if the transaction is expired', async () => {
const transaction = await generateZeroExTransactionAsync({
expirationTimeSeconds: constants.ZERO_AMOUNT,
});
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const expectedError = new ExchangeRevertErrors.TransactionError(
ExchangeRevertErrors.TransactionErrorCode.Expired,
transactionHash,
);
expect(
transactionsContract.assertExecutableTransaction(transaction, randomSignature()).callAsync(),
).to.revertWith(expectedError);
});
it('should revert if the gasPrice is less than required', async () => {
const transaction = await generateZeroExTransactionAsync();
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const actualGasPrice = transaction.gasPrice.minus(1);
const expectedError = new ExchangeRevertErrors.TransactionGasPriceError(
transactionHash,
actualGasPrice,
transaction.gasPrice,
);
expect(
transactionsContract.assertExecutableTransaction(transaction, randomSignature()).callAsync({
gasPrice: actualGasPrice,
}),
).to.revertWith(expectedError);
});
it('should revert if the gasPrice is greater than required', async () => {
const transaction = await generateZeroExTransactionAsync();
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const actualGasPrice = transaction.gasPrice.plus(1);
const expectedError = new ExchangeRevertErrors.TransactionGasPriceError(
transactionHash,
actualGasPrice,
transaction.gasPrice,
);
expect(
transactionsContract.assertExecutableTransaction(transaction, randomSignature()).callAsync({
gasPrice: actualGasPrice,
}),
).to.revertWith(expectedError);
});
it('should revert if currentContextAddress is non-zero', async () => {
await transactionsContract.setCurrentContextAddress(accounts[0]).awaitTransactionSuccessAsync();
const transaction = await generateZeroExTransactionAsync();
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const expectedError = new ExchangeRevertErrors.TransactionInvalidContextError(transactionHash, accounts[0]);
expect(
transactionsContract.assertExecutableTransaction(transaction, randomSignature()).callAsync(),
).to.revertWith(expectedError);
});
it('should revert if the transaction has already been executed', async () => {
const transaction = await generateZeroExTransactionAsync();
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
await transactionsContract.setTransactionExecuted(transactionHash).awaitTransactionSuccessAsync();
const expectedError = new ExchangeRevertErrors.TransactionError(
ExchangeRevertErrors.TransactionErrorCode.AlreadyExecuted,
transactionHash,
);
expect(
transactionsContract.assertExecutableTransaction(transaction, randomSignature()).callAsync(),
).to.revertWith(expectedError);
});
it('should revert if signer != msg.sender and the signature is invalid', async () => {
const transaction = await generateZeroExTransactionAsync({ signerAddress: accounts[0] });
const transactionHash = transactionHashUtils.getTransactionHashHex(transaction);
const expectedError = new ExchangeRevertErrors.SignatureError(
ExchangeRevertErrors.SignatureErrorCode.BadTransactionSignature,
transactionHash,
accounts[0],
INVALID_SIGNATURE,
);
expect(
transactionsContract.assertExecutableTransaction(transaction, INVALID_SIGNATURE).callAsync({
from: accounts[1],
}),
).to.revertWith(expectedError);
});
it('should be successful if signer == msg.sender and the signature is invalid', async () => {
const transaction = await generateZeroExTransactionAsync({ signerAddress: accounts[0] });
return expect(
transactionsContract.assertExecutableTransaction(transaction, INVALID_SIGNATURE).callAsync({
from: accounts[0],
}),
).to.be.fulfilled('');
});
it('should be successful if signer == msg.sender and the signature is valid', async () => {
const transaction = await generateZeroExTransactionAsync({ signerAddress: accounts[0] });
return expect(
transactionsContract.assertExecutableTransaction(transaction, randomSignature()).callAsync({
from: accounts[0],
}),
).to.be.fulfilled('');
});
it('should be successful if not expired, the gasPrice is correct, the tx has not been executed, currentContextAddress has not been set, signer != msg.sender, and the signature is valid', async () => {
const transaction = await generateZeroExTransactionAsync({ signerAddress: accounts[0] });
return expect(
transactionsContract.assertExecutableTransaction(transaction, randomSignature()).callAsync({
from: accounts[1],
}),
).to.be.fulfilled('');
});
});
describe('setCurrentContextAddressIfRequired', () => {
it('should set the currentContextAddress if signer not equal to sender', async () => {
const randomAddress = hexUtils.random(20);
await transactionsContract
.setCurrentContextAddressIfRequired(randomAddress, randomAddress)
.awaitTransactionSuccessAsync();
const currentContextAddress = await transactionsContract.currentContextAddress().callAsync();
expect(currentContextAddress).to.eq(randomAddress);
});
it('should not set the currentContextAddress if signer equal to sender', async () => {
const randomAddress = hexUtils.random(20);
await transactionsContract
.setCurrentContextAddressIfRequired(accounts[0], randomAddress)
.awaitTransactionSuccessAsync({
from: accounts[0],
});
const currentContextAddress = await transactionsContract.currentContextAddress().callAsync();
expect(currentContextAddress).to.eq(constants.NULL_ADDRESS);
});
});
describe('getCurrentContext', () => {
it('should return the sender address when there is not a saved context address', async () => {
const currentContextAddress = await transactionsContract.getCurrentContextAddress().callAsync({
from: accounts[0],
});
expect(currentContextAddress).to.be.eq(accounts[0]);
});
it('should return the sender address when there is a saved context address', async () => {
// Set the current context address to the taker address
await transactionsContract.setCurrentContextAddress(accounts[1]).awaitTransactionSuccessAsync();
// Ensure that the queried current context address is the same as the address that was set.
const currentContextAddress = await transactionsContract.getCurrentContextAddress().callAsync({
from: accounts[0],
});
expect(currentContextAddress).to.be.eq(accounts[1]);
});
});
});
// tslint:disable-line:max-file-line-count