@@ -2,22 +2,30 @@ import {MockProvider} from '@ethereum-waffle/provider';
2
2
import { expect , AssertionError } from 'chai' ;
3
3
import { Contract , Wallet } from 'ethers' ;
4
4
5
- import { BASE_FEE_PER_GAS , TX_GAS } from './constants' ;
6
-
7
- export const changeEtherBalancesTest = ( provider : MockProvider ) => {
5
+ interface ChangeEtherBalancesTestOptions {
6
+ txGasFees : number | ( ( ) => number ) ;
7
+ baseFeePerGas : number ;
8
+ }
9
+
10
+ export const changeEtherBalancesTest = (
11
+ provider : MockProvider ,
12
+ options : ChangeEtherBalancesTestOptions
13
+ ) => {
14
+ let txGasFees : number ;
15
+ let baseFeePerGas : number ;
8
16
let sender : Wallet ;
9
17
let receiver : Wallet ;
10
18
let contractWallet : Wallet ;
11
19
let contract : Contract ;
12
- let txGasFees : number ;
13
20
14
21
before ( ( ) => {
22
+ txGasFees = typeof options . txGasFees === 'function' ? options . txGasFees ( ) : options . txGasFees ;
23
+ baseFeePerGas = options . baseFeePerGas ;
15
24
const wallets = provider . getWallets ( ) ;
16
25
sender = wallets [ 0 ] ;
17
26
receiver = wallets [ 1 ] ;
18
27
contractWallet = wallets [ 2 ] ;
19
28
contract = new Contract ( contractWallet . address , [ ] , provider ) ;
20
- txGasFees = BASE_FEE_PER_GAS * TX_GAS ;
21
29
} ) ;
22
30
23
31
describe ( 'Transaction Callback' , ( ) => {
@@ -37,7 +45,7 @@ export const changeEtherBalancesTest = (provider: MockProvider) => {
37
45
await expect ( ( ) =>
38
46
sender . sendTransaction ( {
39
47
to : receiver . address ,
40
- gasPrice : BASE_FEE_PER_GAS ,
48
+ gasPrice : baseFeePerGas ,
41
49
value : 200
42
50
} )
43
51
) . to . changeEtherBalances ( [ sender , receiver ] , [ '-200' , 200 ] ) ;
@@ -47,7 +55,7 @@ export const changeEtherBalancesTest = (provider: MockProvider) => {
47
55
await expect ( ( ) =>
48
56
sender . sendTransaction ( {
49
57
to : receiver . address ,
50
- gasPrice : BASE_FEE_PER_GAS ,
58
+ gasPrice : baseFeePerGas ,
51
59
value : 200
52
60
} )
53
61
) . to . changeEtherBalances ( [ sender , receiver , contract ] , [ - ( txGasFees + 200 ) , 200 , 0 ] , { includeFee : true } ) ;
@@ -57,7 +65,7 @@ export const changeEtherBalancesTest = (provider: MockProvider) => {
57
65
await expect ( ( ) =>
58
66
sender . sendTransaction ( {
59
67
to : receiver . address ,
60
- gasPrice : BASE_FEE_PER_GAS ,
68
+ gasPrice : baseFeePerGas ,
61
69
value : 200
62
70
} )
63
71
) . to . not . changeEtherBalances ( [ sender , receiver ] , [ - ( txGasFees + 201 ) , 200 ] ) ;
@@ -74,7 +82,7 @@ export const changeEtherBalancesTest = (provider: MockProvider) => {
74
82
expect ( ( ) =>
75
83
sender . sendTransaction ( {
76
84
to : receiver . address ,
77
- gasPrice : BASE_FEE_PER_GAS ,
85
+ gasPrice : baseFeePerGas ,
78
86
value : 200
79
87
} )
80
88
) . to . changeEtherBalances ( [ sender , receiver ] , [ - 200 , 201 ] )
@@ -87,7 +95,7 @@ export const changeEtherBalancesTest = (provider: MockProvider) => {
87
95
expect ( ( ) =>
88
96
sender . sendTransaction ( {
89
97
to : receiver . address ,
90
- gasPrice : BASE_FEE_PER_GAS ,
98
+ gasPrice : baseFeePerGas ,
91
99
value : 200
92
100
} )
93
101
) . to . changeEtherBalances ( [ sender , receiver ] , [ - 201 , 200 ] )
@@ -103,7 +111,7 @@ export const changeEtherBalancesTest = (provider: MockProvider) => {
103
111
expect ( ( ) =>
104
112
sender . sendTransaction ( {
105
113
to : receiver . address ,
106
- gasPrice : BASE_FEE_PER_GAS ,
114
+ gasPrice : baseFeePerGas ,
107
115
value : 200
108
116
} )
109
117
) . to . not . changeEtherBalances ( [ sender , receiver ] , [ - 200 , 200 ] )
@@ -131,7 +139,7 @@ export const changeEtherBalancesTest = (provider: MockProvider) => {
131
139
it ( 'Should pass when all expected balance changes are equal to actual values' , async ( ) => {
132
140
await expect ( await sender . sendTransaction ( {
133
141
to : receiver . address ,
134
- gasPrice : BASE_FEE_PER_GAS ,
142
+ gasPrice : baseFeePerGas ,
135
143
value : 200
136
144
} )
137
145
) . to . changeEtherBalances ( [ sender , receiver ] , [ ( - ( txGasFees + 200 ) ) . toString ( ) , 200 ] , { includeFee : true } ) ;
@@ -140,7 +148,7 @@ export const changeEtherBalancesTest = (provider: MockProvider) => {
140
148
it ( 'Should take into account transaction fee' , async ( ) => {
141
149
await expect ( await sender . sendTransaction ( {
142
150
to : receiver . address ,
143
- gasPrice : BASE_FEE_PER_GAS ,
151
+ gasPrice : baseFeePerGas ,
144
152
value : 200
145
153
} )
146
154
) . to . changeEtherBalances ( [ sender , receiver , contract ] , [ - ( txGasFees + 200 ) , 200 , 0 ] , { includeFee : true } ) ;
@@ -164,14 +172,14 @@ export const changeEtherBalancesTest = (provider: MockProvider) => {
164
172
await expect (
165
173
expect ( await sender . sendTransaction ( {
166
174
to : receiver . address ,
167
- gasPrice : BASE_FEE_PER_GAS ,
175
+ gasPrice : baseFeePerGas ,
168
176
value : 200
169
177
} )
170
178
) . to . changeEtherBalances ( [ sender , receiver ] , [ - 200 , 200 ] , { includeFee : true } )
171
179
) . to . be . eventually . rejectedWith (
172
180
AssertionError ,
173
181
`Expected ${ sender . address } ,${ receiver . address } to change balance ` +
174
- ' by -200,200 wei, but it has changed by -18375000000200 ,200 wei'
182
+ ` by -200,200 wei, but it has changed by -${ txGasFees + 200 } ,200 wei`
175
183
) ;
176
184
} ) ;
177
185
0 commit comments