11import { BigNumber , BigNumberish , providers } from 'ethers' ;
2+ import { transactionPromise } from '../transaction-promise' ;
23import { ensure } from './calledOnContract/utils' ;
34import { Account , getAddressOf } from './misc/account' ;
45import { BalanceChangeOptions } from './misc/balance' ;
@@ -10,53 +11,48 @@ export function supportChangeEtherBalance(Assertion: Chai.AssertionStatic) {
1011 balanceChange : BigNumberish ,
1112 options : BalanceChangeOptions
1213 ) {
13- const subject = this . _obj ;
14- const derivedPromise = Promise . all ( [
15- getBalanceChange ( subject , account , options ) ,
16- getAddressOf ( account )
17- ] ) . then (
18- ( [ actualChange , address ] ) => {
19- this . assert (
20- actualChange . eq ( BigNumber . from ( balanceChange ) ) ,
21- `Expected "${ address } " to change balance by ${ balanceChange } wei, ` +
14+ transactionPromise ( this ) ;
15+ const isNegated = this . __flags . negate === true ;
16+ const derivedPromise = this . txPromise . then ( ( ) => {
17+ return Promise . all ( [
18+ getBalanceChange ( this . txResponse , account , options ) ,
19+ getAddressOf ( account )
20+ ] ) ;
21+ } ) . then ( ( [ actualChange , address ] : [ BigNumber , string ] ) => {
22+ const isCurrentlyNegated = this . __flags . negate === true ;
23+ this . __flags . negate = isNegated ;
24+ this . assert (
25+ actualChange . eq ( BigNumber . from ( balanceChange ) ) ,
26+ `Expected "${ address } " to change balance by ${ balanceChange } wei, ` +
2227 `but it has changed by ${ actualChange } wei` ,
23- `Expected "${ address } " to not change balance by ${ balanceChange } wei,` ,
24- balanceChange ,
25- actualChange
26- ) ;
27- }
28+ `Expected "${ address } " to not change balance by ${ balanceChange } wei,` ,
29+ balanceChange ,
30+ actualChange
31+ ) ;
32+ this . __flags . negate = isCurrentlyNegated ;
33+ }
2834 ) ;
2935 this . then = derivedPromise . then . bind ( derivedPromise ) ;
3036 this . catch = derivedPromise . catch . bind ( derivedPromise ) ;
31- this . promise = derivedPromise ;
37+ this . txPromise = derivedPromise ;
3238 return this ;
3339 } ) ;
3440}
3541
3642export async function getBalanceChange (
37- transaction :
38- | providers . TransactionResponse
39- | ( ( ) => Promise < providers . TransactionResponse > | providers . TransactionResponse ) ,
43+ txResponse : providers . TransactionResponse ,
4044 account : Account ,
4145 options ?: BalanceChangeOptions
4246) {
4347 ensure ( account . provider !== undefined , TypeError , 'Provider not found' ) ;
44-
45- let txResponse : providers . TransactionResponse ;
46-
47- if ( typeof transaction === 'function' ) {
48- txResponse = await transaction ( ) ;
49- } else {
50- txResponse = transaction ;
51- }
52-
5348 const txReceipt = await txResponse . wait ( ) ;
5449 const txBlockNumber = txReceipt . blockNumber ;
50+ const address = await getAddressOf ( account ) ;
5551
56- const balanceAfter = await account . provider . getBalance ( getAddressOf ( account ) , txBlockNumber ) ;
57- const balanceBefore = await account . provider . getBalance ( getAddressOf ( account ) , txBlockNumber - 1 ) ;
52+ const balanceAfter = await account . provider . getBalance ( address , txBlockNumber ) ;
53+ const balanceBefore = await account . provider . getBalance ( address , txBlockNumber - 1 ) ;
5854
59- if ( options ?. includeFee !== true && await getAddressOf ( account ) === txResponse . from ) {
55+ if ( options ?. includeFee !== true && address === txReceipt . from ) {
6056 const gasPrice = txResponse . gasPrice ?? txReceipt . effectiveGasPrice ;
6157 const gasUsed = txReceipt . gasUsed ;
6258 const txFee = gasPrice . mul ( gasUsed ) ;
0 commit comments