1
1
const { expect } = require ( "chai" ) ;
2
2
const { isBigNumber} = require ( "hardhat/common" ) ;
3
+ const { ethers } = require ( "hardhat" ) ;
3
4
const { uniq, shuffle} = require ( "lodash" ) ;
5
+
4
6
function sleep ( ms ) {
5
7
return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
6
8
}
@@ -14,6 +16,31 @@ function debug(msg) {
14
16
//console.log(msg)
15
17
}
16
18
19
+ async function sendTransactionAndCheckGas ( sender , recipient , amount ) {
20
+ // Get the balance of the sender before the transaction
21
+ const balanceBefore = await ethers . provider . getBalance ( sender . address ) ;
22
+
23
+ // Send the transaction
24
+ const tx = await sender . sendTransaction ( {
25
+ to : recipient . address ,
26
+ value : amount
27
+ } ) ;
28
+
29
+ // Wait for the transaction to be mined and get the receipt
30
+ const receipt = await tx . wait ( ) ;
31
+
32
+ // Get the balance of the sender after the transaction
33
+ const balanceAfter = await ethers . provider . getBalance ( sender . address ) ;
34
+
35
+ // Calculate the total cost of the transaction (amount + gas fees)
36
+ const gasPrice = receipt . gasPrice ;
37
+ const gasUsed = receipt . gasUsed ;
38
+ const totalCost = gasPrice * gasUsed + BigInt ( amount ) ;
39
+
40
+ // Check that the sender's balance decreased by the total cost
41
+ return balanceBefore - balanceAfter === totalCost
42
+ }
43
+
17
44
function generateWallet ( ) {
18
45
const wallet = ethers . Wallet . createRandom ( ) ;
19
46
return wallet . connect ( ethers . provider ) ;
@@ -103,7 +130,7 @@ describe("EVM Test", function () {
103
130
return
104
131
}
105
132
let signers = await ethers . getSigners ( ) ;
106
- owner = signers [ 0 ]
133
+ owner = signers [ 0 ] ;
107
134
debug ( `OWNER = ${ owner . address } ` )
108
135
109
136
const TestToken = await ethers . getContractFactory ( "TestToken" )
@@ -311,8 +338,139 @@ describe("EVM Test", function () {
311
338
} ) ;
312
339
} )
313
340
314
- describe ( "JSON-RPC" , function ( ) {
341
+ describe ( "Gas tests" , function ( ) {
342
+ it ( "Should deduct correct amount of gas on transfer" , async function ( ) {
343
+ const balanceBefore = await ethers . provider . getBalance ( owner ) ;
315
344
345
+ const feeData = await ethers . provider . getFeeData ( ) ;
346
+ const gasPrice = Number ( feeData . gasPrice ) ;
347
+
348
+ const zero = ethers . parseUnits ( '0' , 'ether' )
349
+ const txResponse = await owner . sendTransaction ( {
350
+ to : owner . address ,
351
+ gasPrice : gasPrice ,
352
+ value : zero ,
353
+ type : 1 ,
354
+ } ) ;
355
+ await txResponse . wait ( ) ;
356
+
357
+ const balanceAfter = await ethers . provider . getBalance ( owner ) ;
358
+
359
+ const diff = balanceBefore - balanceAfter ;
360
+ expect ( diff ) . to . equal ( 21000 * gasPrice ) ;
361
+
362
+ const success = await sendTransactionAndCheckGas ( owner , owner , 0 )
363
+ expect ( success ) . to . be . true
364
+ } ) ;
365
+
366
+ it ( "Should fail if insufficient gas is provided" , async function ( ) {
367
+ const feeData = await ethers . provider . getFeeData ( ) ;
368
+ const gasPrice = Number ( feeData . gasPrice ) ;
369
+ const zero = ethers . parseUnits ( '0' , 'ether' )
370
+ expect ( owner . sendTransaction ( {
371
+ to : owner . address ,
372
+ gasPrice : gasPrice - 1 ,
373
+ value : zero ,
374
+ type : 1 ,
375
+ } ) ) . to . be . reverted ;
376
+ } ) ;
377
+
378
+ it ( "Should deduct correct amount even if higher gas price is used" , async function ( ) {
379
+ const balanceBefore = await ethers . provider . getBalance ( owner ) ;
380
+
381
+ const feeData = await ethers . provider . getFeeData ( ) ;
382
+ const gasPrice = Number ( feeData . gasPrice ) ;
383
+ const higherGasPrice = Number ( gasPrice + 9 )
384
+ console . log ( `gasPrice = ${ gasPrice } ` )
385
+
386
+ const zero = ethers . parseUnits ( '0' , 'ether' )
387
+ const txResponse = await owner . sendTransaction ( {
388
+ to : owner . address ,
389
+ value : zero ,
390
+ gasPrice : higherGasPrice ,
391
+ type : 1 ,
392
+ } ) ;
393
+ const receipt = await txResponse . wait ( ) ;
394
+
395
+ const balanceAfter = await ethers . provider . getBalance ( owner ) ;
396
+
397
+ const diff = balanceBefore - balanceAfter ;
398
+ expect ( diff ) . to . equal ( 21000 * higherGasPrice ) ;
399
+
400
+ const success = await sendTransactionAndCheckGas ( owner , owner , 0 )
401
+ expect ( success ) . to . be . true
402
+ } ) ;
403
+
404
+ describe ( "EIP-1559" , async function ( ) {
405
+ const zero = ethers . parseUnits ( '0' , 'ether' )
406
+ const twoGwei = ethers . parseUnits ( "2" , "gwei" ) ;
407
+ const oneGwei = ethers . parseUnits ( "1" , "gwei" ) ;
408
+
409
+ const testCases = [
410
+ [ "No truncation from max priority fee" , oneGwei , oneGwei ] ,
411
+ [ "With truncation from max priority fee" , oneGwei , twoGwei ] ,
412
+ [ "With complete truncation from max priority fee" , zero , twoGwei ]
413
+ ] ;
414
+
415
+ it ( "Should be able to send many EIP-1559 txs" , async function ( ) {
416
+ const oneGwei = ethers . parseUnits ( "1" , "gwei" ) ;
417
+ const zero = ethers . parseUnits ( '0' , 'ether' )
418
+ for ( let i = 0 ; i < 10 ; i ++ ) {
419
+ const txResponse = await owner . sendTransaction ( {
420
+ to : owner . address ,
421
+ value : zero ,
422
+ maxPriorityFeePerGas : oneGwei ,
423
+ maxFeePerGas : oneGwei ,
424
+ type : 2
425
+ } ) ;
426
+ await txResponse . wait ( ) ;
427
+ }
428
+ } ) ;
429
+
430
+ describe ( "Differing maxPriorityFeePerGas and maxFeePerGas" , async function ( ) {
431
+ testCases . forEach ( async ( [ name , maxPriorityFeePerGas , maxFeePerGas ] ) => {
432
+ it ( `EIP-1559 test: ${ name } ` , async function ( ) {
433
+ console . log ( `maxPriorityFeePerGas = ${ maxPriorityFeePerGas } ` )
434
+ console . log ( `maxFeePerGas = ${ maxFeePerGas } ` )
435
+ const balanceBefore = await ethers . provider . getBalance ( owner ) ;
436
+ const feeData = await ethers . provider . getFeeData ( ) ;
437
+ const gasPrice = Number ( feeData . gasPrice ) ;
438
+
439
+ console . log ( `gasPrice = ${ gasPrice } ` )
440
+
441
+ const zero = ethers . parseUnits ( '0' , 'ether' )
442
+ const txResponse = await owner . sendTransaction ( {
443
+ to : owner . address ,
444
+ value : zero ,
445
+ maxPriorityFeePerGas : maxPriorityFeePerGas ,
446
+ maxFeePerGas : maxFeePerGas ,
447
+ type : 2
448
+ } ) ;
449
+ const receipt = await txResponse . wait ( ) ;
450
+
451
+ expect ( receipt ) . to . not . be . null ;
452
+ expect ( receipt . status ) . to . equal ( 1 ) ;
453
+
454
+ const balanceAfter = await ethers . provider . getBalance ( owner ) ;
455
+
456
+ const tip = Math . min (
457
+ Number ( maxFeePerGas ) - gasPrice ,
458
+ Number ( maxPriorityFeePerGas )
459
+ ) ;
460
+ console . log ( `tip = ${ tip } ` )
461
+ const effectiveGasPrice = tip + gasPrice ;
462
+ console . log ( `effectiveGasPrice = ${ effectiveGasPrice } ` )
463
+
464
+ const diff = balanceBefore - balanceAfter ;
465
+ console . log ( `diff = ${ diff } ` )
466
+ expect ( diff ) . to . equal ( 21000 * effectiveGasPrice ) ;
467
+ } ) ;
468
+ } ) ;
469
+ } ) ;
470
+ } ) ;
471
+ } ) ;
472
+
473
+ describe ( "JSON-RPC" , function ( ) {
316
474
it ( "Should retrieve a transaction by its hash" , async function ( ) {
317
475
// Send a transaction to get its hash
318
476
const txResponse = await evmTester . setBoolVar ( true ) ;
@@ -481,11 +639,6 @@ describe("EVM Test", function () {
481
639
const isContract = code !== '0x' ;
482
640
expect ( isContract ) . to . be . true ;
483
641
} ) ;
484
-
485
642
} ) ;
486
-
487
-
488
-
489
643
} ) ;
490
-
491
644
} ) ;
0 commit comments