-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
sendTransaction.ts
1302 lines (1165 loc) · 48.4 KB
/
sendTransaction.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { assert } from "chai";
import { BN, bufferToHex, toBuffer, zeroAddress } from "ethereumjs-util";
import { Client } from "undici";
import {
numberToRpcQuantity,
rpcQuantityToNumber,
rpcQuantityToBN,
} from "../../../../../../../src/internal/core/jsonrpc/types/base-types";
import { InvalidInputError } from "../../../../../../../src/internal/core/providers/errors";
import { workaroundWindowsCiFailures } from "../../../../../../utils/workaround-windows-ci-failures";
import {
assertInvalidInputError,
assertReceiptMatchesGethOne,
assertTransactionFailure,
} from "../../../../helpers/assertions";
import { EXAMPLE_REVERT_CONTRACT } from "../../../../helpers/contracts";
import { setCWD } from "../../../../helpers/cwd";
import { getPendingBaseFeePerGas } from "../../../../helpers/getPendingBaseFeePerGas";
import {
DEFAULT_ACCOUNTS_ADDRESSES,
DEFAULT_ACCOUNTS_BALANCES,
DEFAULT_BLOCK_GAS_LIMIT,
PROVIDERS,
} from "../../../../helpers/providers";
import { retrieveForkBlockNumber } from "../../../../helpers/retrieveForkBlockNumber";
import { sendDummyTransaction } from "../../../../helpers/sendDummyTransaction";
import {
deployContract,
sendTxToZeroAddress,
} from "../../../../helpers/transactions";
import { useHelpers } from "../../../../helpers/useHelpers";
import { compileLiteral } from "../../../../stack-traces/compilation";
import {
EIP1559RpcTransactionOutput,
RpcBlockOutput,
} from "../../../../../../../src/internal/hardhat-network/provider/output";
import { EthereumProvider } from "../../../../../../../src/types";
describe("Eth module", function () {
PROVIDERS.forEach(({ name, useProvider, isFork, isJsonRpc, chainId }) => {
if (isFork) {
this.timeout(50000);
}
workaroundWindowsCiFailures.call(this, { isFork });
describe(`${name} provider`, function () {
setCWD();
describe("eth_sendTransaction", async function () {
useProvider();
useHelpers();
const getFirstBlock = async () =>
isFork ? retrieveForkBlockNumber(this.ctx.hardhatNetworkProvider) : 0;
// Because of the way we are testing this (i.e. integration testing) it's almost impossible to
// fully test this method in a reasonable amount of time. This is because it executes the core
// of Ethereum: its state transition function.
//
// We have mostly test about logic added on top of that, and will add new ones whenever
// suitable. This is approximately the same as assuming that @ethereumjs/vm is correct, which
// seems reasonable, and if it weren't we should address the issues there.
describe("Params validation", function () {
it("Should fail for tx sent from account that is neither local nor marked as impersonated", async function () {
await assertTransactionFailure(
this.provider,
{
from: zeroAddress(),
to: DEFAULT_ACCOUNTS_ADDRESSES[0],
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(
await getPendingBaseFeePerGas(this.provider)
),
},
"unknown account",
InvalidInputError.CODE
);
});
it("Should fail if sending to the null address without data", async function () {
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
},
"contract creation without any data provided",
InvalidInputError.CODE
);
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(
await getPendingBaseFeePerGas(this.provider)
),
},
"contract creation without any data provided",
InvalidInputError.CODE
);
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
data: "0x",
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(
await getPendingBaseFeePerGas(this.provider)
),
},
"contract creation without any data provided",
InvalidInputError.CODE
);
});
it("Should accept EIP-1559 transactions", async function () {
const hash = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: DEFAULT_ACCOUNTS_ADDRESSES[1],
value: numberToRpcQuantity(1),
gas: numberToRpcQuantity(21000),
maxFeePerGas: numberToRpcQuantity(
await getPendingBaseFeePerGas(this.provider)
),
maxPriorityFeePerGas: numberToRpcQuantity(
await getPendingBaseFeePerGas(this.provider)
),
},
]);
assert.match(hash, /^0x[a-f\d]{64}$/);
});
it("Should throw if tx includes gasPrice, maxFeePerGas and maxPriorityFeePerGas", async function () {
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: DEFAULT_ACCOUNTS_ADDRESSES[1],
value: numberToRpcQuantity(1),
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(
await getPendingBaseFeePerGas(this.provider)
),
maxFeePerGas: numberToRpcQuantity(10),
maxPriorityFeePerGas: numberToRpcQuantity(10),
},
"Cannot send both gasPrice and maxFeePerGas",
InvalidInputError.CODE
);
});
it("Should throw if tx includes gasPrice and maxFeePerGas", async function () {
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: DEFAULT_ACCOUNTS_ADDRESSES[1],
value: numberToRpcQuantity(1),
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(10),
maxFeePerGas: numberToRpcQuantity(10),
},
"Cannot send both gasPrice and maxFeePerGas",
InvalidInputError.CODE
);
});
it("Should throw if tx includes gasPrice and maxPriorityFeePerGas", async function () {
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: DEFAULT_ACCOUNTS_ADDRESSES[1],
value: numberToRpcQuantity(1),
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(1),
maxPriorityFeePerGas: numberToRpcQuantity(1),
},
"Cannot send both gasPrice and maxPriorityFeePerGas",
InvalidInputError.CODE
);
});
it("Should throw if maxPriorityFeePerGas is bigger than maxFeePerGas", async function () {
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: DEFAULT_ACCOUNTS_ADDRESSES[1],
value: numberToRpcQuantity(1),
gas: numberToRpcQuantity(21000),
maxFeePerGas: numberToRpcQuantity(10),
maxPriorityFeePerGas: numberToRpcQuantity(20),
},
"maxPriorityFeePerGas (20) is bigger than maxFeePerGas (10)",
InvalidInputError.CODE
);
});
it("Should succeed if sending an explicit null for an optional parameter value", async function () {
assert.match(
await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[1],
gas: null,
gasPrice: null,
value: null,
nonce: null,
data: null,
accessList: null,
chainId: null,
},
]),
/^0x[a-f\d]{64}$/
);
});
});
describe("when automine is enabled", () => {
it("Should return a valid transaction hash", async function () {
const hash = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: DEFAULT_ACCOUNTS_ADDRESSES[1],
value: numberToRpcQuantity(1),
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(
await getPendingBaseFeePerGas(this.provider)
),
},
]);
assert.match(hash, /^0x[a-f\d]{64}$/);
});
describe("With just from and data", function () {
for (const toValue of [undefined, null]) {
it(`Should work with a 'to' value of ${toValue}`, async function () {
const firstBlock = await getFirstBlock();
const hash = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
data: "0x00",
to: toValue,
},
]);
const receipt = await this.provider.send(
"eth_getTransactionReceipt",
[hash]
);
const receiptFromGeth = {
blockHash:
"0x01490da2af913e9a868430b7b4c5060fc29cbdb1692bb91d3c72c734acd73bc8",
blockNumber: "0x6",
contractAddress: "0x6ea84fcbef576d66896dc2c32e139b60e641170c",
cumulativeGasUsed: "0xcf0c",
from: "0xda4585f6e68ed1cdfdad44a08dbe3979ec74ad8f",
gasUsed: "0xcf0c",
logs: [],
logsBloom:
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
status: "0x1",
to: null,
transactionHash:
"0xbd24cbe9c1633b98e61d93619230341141d2cff49470ed6afa739cee057fd0aa",
transactionIndex: "0x0",
};
assertReceiptMatchesGethOne(
receipt,
receiptFromGeth,
firstBlock + 1
);
});
}
});
it("Should throw if the tx nonce is higher than the account nonce", async function () {
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
nonce: numberToRpcQuantity(1),
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
},
],
"Nonce too high. Expected nonce to be 0 but got 1. Note that transactions can't be queued when automining."
);
});
it("Should throw if the tx nonce is lower than the account nonce", async function () {
await sendTxToZeroAddress(
this.provider,
DEFAULT_ACCOUNTS_ADDRESSES[1]
);
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
nonce: numberToRpcQuantity(0),
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
},
],
"Nonce too low. Expected nonce to be 1 but got 0."
);
});
it("Should throw if the transaction fails", async function () {
// Not enough gas
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: zeroAddress(),
gas: numberToRpcQuantity(1),
},
],
"Transaction requires at least 21000 gas but got 1"
);
// Not enough balance
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: zeroAddress(),
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(DEFAULT_ACCOUNTS_BALANCES[0]),
},
],
"sender doesn't have enough funds to send tx"
);
// Gas is larger than block gas limit
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: zeroAddress(),
gas: numberToRpcQuantity(DEFAULT_BLOCK_GAS_LIMIT + 1),
},
],
`Transaction gas limit is ${
DEFAULT_BLOCK_GAS_LIMIT + 1
} and exceeds block gas limit of ${DEFAULT_BLOCK_GAS_LIMIT}`
);
// Invalid opcode. We try to deploy a contract with an invalid opcode in the deployment code
// The transaction gets executed anyway, so the account is updated
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
data: "0xAA",
},
"Transaction reverted without a reason"
);
// Out of gas. This a deployment transaction that pushes 0x00 multiple times
// The transaction gets executed anyway, so the account is updated.
//
// Note: this test is pretty fragile, as the tx needs to have enough gas
// to pay for the calldata, but not enough to execute. This costs changed
// with istanbul, and may change again in the future.
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
data: "0x6000600060006000600060006000600060006000600060006000600060006000600060006000600060006000600060006000",
gas: numberToRpcQuantity(53500),
},
"out of gas"
);
// Revert. This is a deployment transaction that immediately reverts without a reason
// The transaction gets executed anyway, so the account is updated
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
data: "0x60006000fd",
},
"Transaction reverted without a reason"
);
// This is a contract that reverts with A in its constructor
await assertTransactionFailure(
this.provider,
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
data: "0x6080604052348015600f57600080fd5b506040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260018152602001807f410000000000000000000000000000000000000000000000000000000000000081525060200191505060405180910390fdfe",
},
"reverted with reason string 'A'"
);
});
describe("when there are pending transactions in the mempool", () => {
describe("when the sent transaction fits in the first block", () => {
it("Should throw if the sender doesn't have enough balance as a result of mining pending transactions first", async function () {
const gasPrice = 10;
const firstBlock = await getFirstBlock();
const wholeAccountBalance = numberToRpcQuantity(
DEFAULT_ACCOUNTS_BALANCES[0].subn(gasPrice * 21_000)
);
await this.provider.send("evm_setAutomine", [false]);
await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
nonce: numberToRpcQuantity(0),
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(gasPrice),
value: wholeAccountBalance,
},
]);
await this.provider.send("evm_setAutomine", [true]);
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(
await getPendingBaseFeePerGas(this.provider)
),
value: wholeAccountBalance,
},
],
"sender doesn't have enough funds to send tx"
);
assert.equal(
rpcQuantityToNumber(
await this.provider.send("eth_blockNumber")
),
firstBlock
);
assert.lengthOf(
await this.provider.send("eth_pendingTransactions"),
1
);
});
});
describe("when multiple blocks have to be mined before the sent transaction is included", () => {
beforeEach(async function () {
await this.provider.send("evm_setBlockGasLimit", [
numberToRpcQuantity(45000),
]);
});
it("Should eventually mine the sent transaction", async function () {
await this.provider.send("evm_setAutomine", [false]);
const blockNumberBefore = rpcQuantityToNumber(
await this.provider.send("eth_blockNumber")
);
await sendDummyTransaction(this.provider, 0, {
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
});
await sendDummyTransaction(this.provider, 1, {
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
});
await sendDummyTransaction(this.provider, 2, {
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
});
await sendDummyTransaction(this.provider, 3, {
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
});
await this.provider.send("evm_setAutomine", [true]);
const txHash = await sendDummyTransaction(this.provider, 4, {
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
});
const blockAfter = await this.provider.send(
"eth_getBlockByNumber",
["latest", false]
);
const blockNumberAfter = rpcQuantityToNumber(blockAfter.number);
assert.equal(blockNumberAfter, blockNumberBefore + 3);
assert.lengthOf(blockAfter.transactions, 1);
assert.sameDeepMembers(blockAfter.transactions, [txHash]);
});
it("Should throw if the sender doesn't have enough balance as a result of mining pending transactions first", async function () {
const gasPrice = await getPendingBaseFeePerGas(this.provider);
const sendTransaction = async (
nonce: number,
value: BN | number
) => {
return this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
nonce: numberToRpcQuantity(nonce),
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(gasPrice),
value: numberToRpcQuantity(value),
},
]);
};
const initialBalance = DEFAULT_ACCOUNTS_BALANCES[1];
const firstBlock = await getFirstBlock();
await this.provider.send("evm_setAutomine", [false]);
await sendTransaction(0, 0);
await sendTransaction(1, 0);
await sendTransaction(
2,
initialBalance.sub(gasPrice.muln(21_000).muln(3))
);
await this.provider.send("evm_setAutomine", [true]);
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
gas: numberToRpcQuantity(21000),
gasPrice: numberToRpcQuantity(gasPrice),
value: numberToRpcQuantity(100),
},
],
"sender doesn't have enough funds to send tx"
);
assert.equal(
rpcQuantityToNumber(
await this.provider.send("eth_blockNumber")
),
firstBlock
);
assert.lengthOf(
await this.provider.send("eth_pendingTransactions"),
3
);
});
});
});
it("Should throw if a tx can't be mined in the next block because of its fees", async function () {
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
maxFeePerGas: numberToRpcQuantity(1),
},
],
"too low for the next block, which has a baseFeePerGas of"
);
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
gasPrice: numberToRpcQuantity(1),
},
],
"too low for the next block, which has a baseFeePerGas of"
);
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
gasPrice: numberToRpcQuantity(1),
accessList: [],
},
],
"too low for the next block, which has a baseFeePerGas of"
);
});
});
describe("when automine is disabled", () => {
beforeEach(async function () {
await this.provider.send("evm_setAutomine", [false]);
});
it("Should not throw if the tx nonce is higher than the account nonce", async function () {
await assert.isFulfilled(
this.provider.send("eth_sendTransaction", [
{
nonce: numberToRpcQuantity(1),
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
},
])
);
});
it("Should throw if the tx nonce is lower than the account nonce", async function () {
await sendTxToZeroAddress(
this.provider,
DEFAULT_ACCOUNTS_ADDRESSES[1]
);
await this.provider.send("evm_mine");
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
nonce: numberToRpcQuantity(0),
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
},
],
"Nonce too low. Expected nonce to be at least 1 but got 0."
);
});
it("Should throw an error if the same transaction is sent twice", async function () {
const txParams = {
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[1],
nonce: numberToRpcQuantity(0),
};
const hash = await this.provider.send("eth_sendTransaction", [
txParams,
]);
await assertTransactionFailure(
this.provider,
txParams,
`Known transaction: ${bufferToHex(hash)}`
);
});
it("Should replace pending transactions", async function () {
const gasPrice = await getPendingBaseFeePerGas(this.provider);
const txHash1 = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
nonce: numberToRpcQuantity(0),
gasPrice: numberToRpcQuantity(gasPrice),
},
]);
let tx1 = await this.provider.send("eth_getTransactionByHash", [
txHash1,
]);
assert.isNotNull(tx1);
const txHash2 = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
nonce: numberToRpcQuantity(0),
gasPrice: numberToRpcQuantity(gasPrice.muln(2)),
},
]);
tx1 = await this.provider.send("eth_getTransactionByHash", [
txHash1,
]);
const tx2 = await this.provider.send("eth_getTransactionByHash", [
txHash2,
]);
assert.isNull(tx1);
assert.isNotNull(tx2);
const pendingTxs = await this.provider.send(
"eth_pendingTransactions"
);
assert.lengthOf(pendingTxs, 1);
assert.equal(pendingTxs[0].hash, tx2.hash);
await this.provider.send("evm_mine");
const minedBlock = await this.provider.send(
"eth_getBlockByNumber",
["latest", false]
);
assert.lengthOf(minedBlock.transactions, 1);
assert.equal(minedBlock.transactions[0], tx2.hash);
});
it("Should replace queued transactions", async function () {
const txHash1 = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
nonce: numberToRpcQuantity(2),
gasPrice: numberToRpcQuantity(20),
},
]);
let tx1 = await this.provider.send("eth_getTransactionByHash", [
txHash1,
]);
assert.isNotNull(tx1);
const txHash2 = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
nonce: numberToRpcQuantity(2),
gasPrice: numberToRpcQuantity(30),
},
]);
tx1 = await this.provider.send("eth_getTransactionByHash", [
txHash1,
]);
const tx2 = await this.provider.send("eth_getTransactionByHash", [
txHash2,
]);
assert.isNull(tx1);
assert.isNotNull(tx2);
await this.provider.send("evm_mine");
const minedBlock = await this.provider.send(
"eth_getBlockByNumber",
["latest", false]
);
assert.lengthOf(minedBlock.transactions, 0);
});
it("Should throw an error if the replacement gasPrice, maxFeePerGas or maxPriorityFeePerGas are too low", async function () {
const baseFeePerGas = await getPendingBaseFeePerGas(this.provider);
const txHash1 = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
nonce: numberToRpcQuantity(0),
gasPrice: numberToRpcQuantity(baseFeePerGas),
},
]);
let tx1 = await this.provider.send("eth_getTransactionByHash", [
txHash1,
]);
assert.isNotNull(tx1);
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
nonce: numberToRpcQuantity(0),
gasPrice: numberToRpcQuantity(baseFeePerGas.addn(1)),
},
],
"Replacement transaction underpriced."
);
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
nonce: numberToRpcQuantity(0),
maxFeePerGas: numberToRpcQuantity(baseFeePerGas.addn(1)),
},
],
"Replacement transaction underpriced."
);
await assertInvalidInputError(
this.provider,
"eth_sendTransaction",
[
{
from: DEFAULT_ACCOUNTS_ADDRESSES[1],
to: DEFAULT_ACCOUNTS_ADDRESSES[2],
nonce: numberToRpcQuantity(0),
maxPriorityFeePerGas: numberToRpcQuantity(
baseFeePerGas.addn(1)
),
},
],
"Replacement transaction underpriced."
);
// check that original tx was not replaced
tx1 = await this.provider.send("eth_getTransactionByHash", [
txHash1,
]);
assert.isNotNull(tx1);
const pendingTxs = await this.provider.send(
"eth_pendingTransactions"
);
assert.lengthOf(pendingTxs, 1);
assert.equal(pendingTxs[0].hash, tx1.hash);
await this.provider.send("evm_mine");
const minedBlock = await this.provider.send(
"eth_getBlockByNumber",
["latest", false]
);
assert.lengthOf(minedBlock.transactions, 1);
assert.equal(minedBlock.transactions[0], tx1.hash);
});
});
describe("Fee params default values", function () {
let nextBlockBaseFee: BN;
const ONE_GWEI = new BN(10).pow(new BN(9));
beforeEach(async function () {
// We disable automining as enqueueing the txs is enough and we want
// to test some that may have a low maxFeePerGas
await this.provider.send("evm_setAutomine", [false]);
const pendingBlock: RpcBlockOutput = await this.provider.send(
"eth_getBlockByNumber",
["pending", false]
);
nextBlockBaseFee = rpcQuantityToBN(pendingBlock.baseFeePerGas!);
});
describe("When no fee param is provided", function () {
it("Should use 1gwei maxPriorityFeePerGas and base the maxFeePerGas on that plus 2 * next block's baseFee", async function () {
const txHash = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: DEFAULT_ACCOUNTS_ADDRESSES[0],
},
]);
const tx: EIP1559RpcTransactionOutput = await this.provider.send(
"eth_getTransactionByHash",
[txHash]
);
assert.equal(
tx.maxPriorityFeePerGas,
numberToRpcQuantity(ONE_GWEI)
);
assert.equal(
tx.maxFeePerGas,
numberToRpcQuantity(nextBlockBaseFee.muln(2).add(ONE_GWEI))
);
});
});
describe("When maxFeePerGas is provided", function () {
it("Should use 1gwei maxPriorityFeePerGas if maxFeePerGas is >= 1gwei", async function () {
const txHash = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: DEFAULT_ACCOUNTS_ADDRESSES[0],
maxFeePerGas: numberToRpcQuantity(ONE_GWEI.muln(2)),
},
]);
const tx: EIP1559RpcTransactionOutput = await this.provider.send(
"eth_getTransactionByHash",
[txHash]
);
assert.equal(
tx.maxPriorityFeePerGas,
numberToRpcQuantity(ONE_GWEI)
);
assert.equal(
tx.maxFeePerGas,
numberToRpcQuantity(ONE_GWEI.muln(2))
);
});
it("Should use 1gwei maxPriorityFeePerGas if maxFeePerGas is < 1gwei", async function () {
const txHash = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: DEFAULT_ACCOUNTS_ADDRESSES[0],
maxFeePerGas: numberToRpcQuantity(10000),
},
]);
const tx: EIP1559RpcTransactionOutput = await this.provider.send(
"eth_getTransactionByHash",
[txHash]
);
assert.equal(tx.maxPriorityFeePerGas, numberToRpcQuantity(10000));
assert.equal(tx.maxFeePerGas, numberToRpcQuantity(10000));
});
});
describe("When maxPriorityFeePerGas is provided", function () {
it("Should use the maxPriorityFeePerGas and base the maxFeePerGas on that plus 2 * next block's baseFee", async function () {
const txHash = await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: DEFAULT_ACCOUNTS_ADDRESSES[0],
maxPriorityFeePerGas: numberToRpcQuantity(1000),
},
]);
const tx: EIP1559RpcTransactionOutput = await this.provider.send(
"eth_getTransactionByHash",
[txHash]
);
assert.equal(tx.maxPriorityFeePerGas, numberToRpcQuantity(1000));
assert.equal(
tx.maxFeePerGas,
numberToRpcQuantity(nextBlockBaseFee.muln(2).addn(1000))
);
});
});
});
describe("return txHash", () => {
it("Should return the hash of an out of gas transaction", async function () {
if (!isJsonRpc || isFork) {
this.skip();
}
try {
await this.provider.send("eth_sendTransaction", [
{
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
to: "0x0000000000000000000000000000000000000001",
gas: numberToRpcQuantity(21000), // Address 1 is a precompile, so this will OOG
gasPrice: numberToRpcQuantity(
await getPendingBaseFeePerGas(this.provider)
),
},
]);
assert.fail("Tx should have failed");
} catch (e: any) {
assert.notInclude(e.message, "Tx should have failed");
assert.isDefined(e.data.txHash);
}
});
it("Should return the hash of a reverted transaction", async function () {
if (!isJsonRpc || isFork) {
this.skip();
}
try {
const contractAddress = await deployContract(
this.provider,
`0x${EXAMPLE_REVERT_CONTRACT.bytecode.object}`
);
await this.provider.send("eth_sendTransaction", [
{
to: contractAddress,
from: DEFAULT_ACCOUNTS_ADDRESSES[0],
data: `${EXAMPLE_REVERT_CONTRACT.selectors.f}0000000000000000000000000000000000000000000000000000000000000000`,
},
]);
assert.fail("Tx should have failed");
} catch (e: any) {
assert.notInclude(e.message, "Tx should have failed");
assert.isDefined(e.data.txHash);
}
});
});
// This test checks that an on-chain value can be set to 0
// To do this, we transfer all the balance of the 0x0000...0001 account