Skip to content

Commit dca8950

Browse files
authoredAug 1, 2022
🎩 Allow string addresses in changeTokenBalance (#772)
1 parent 1bc6cd0 commit dca8950

9 files changed

+66
-5
lines changed
 

‎.changeset/beige-melons-prove.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ethereum-waffle/chai": patch
3+
---
4+
5+
🎩 Allow string addresses in `changeTokenBalance`

‎waffle-chai/src/matchers/changeEtherBalance.ts

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export function supportChangeEtherBalance(Assertion: Chai.AssertionStatic) {
1818
if (!('txResponse' in this)) {
1919
throw new Error('The changeEtherBalance matcher must be called on a transaction');
2020
}
21+
if (typeof account === 'string') {
22+
throw new Error(
23+
'A string address cannot be used as an account in changeEtherBalance.' +
24+
' Expecting an instance of Ethers Account.'
25+
);
26+
}
2127
return Promise.all([
2228
getBalanceChange(this.txResponse, account, options),
2329
getAddressOf(account)

‎waffle-chai/src/matchers/changeEtherBalances.ts

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ export function supportChangeEtherBalances(Assertion: Chai.AssertionStatic) {
1717
if (!('txResponse' in this)) {
1818
throw new Error('The changeEtherBalances matcher must be called on a transaction');
1919
}
20+
if (accounts.some(account => typeof account === 'string')) {
21+
throw new Error(
22+
'A string address cannot be used as an account in changeEtherBalances.' +
23+
' Expecting an instance of Ethers Account.'
24+
);
25+
}
2026
return Promise.all([
2127
getBalanceChanges(this.txResponse, accounts, options),
2228
getAddresses(accounts)

‎waffle-chai/src/matchers/changeTokenBalance.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function supportChangeTokenBalance(Assertion: Chai.AssertionStatic) {
66
Assertion.addMethod('changeTokenBalance', function (
77
this: any,
88
token: Contract,
9-
account: Account,
9+
account: Account | string,
1010
balanceChange: BigNumberish
1111
) {
1212
callPromise(this);
@@ -15,7 +15,7 @@ export function supportChangeTokenBalance(Assertion: Chai.AssertionStatic) {
1515
if (!('txReceipt' in this)) {
1616
throw new Error('The changeTokenBalance matcher must be called on a transaction');
1717
}
18-
const address = await getAddressOf(account);
18+
const address = typeof account === 'string' ? account : await getAddressOf(account);
1919
const actualChanges = await getBalanceChange(this.txReceipt, token, address);
2020
return [actualChanges, address];
2121
}).then(([actualChange, address]: [BigNumber, string]) => {

‎waffle-chai/src/matchers/changeTokenBalances.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function supportChangeTokenBalances(Assertion: Chai.AssertionStatic) {
66
Assertion.addMethod('changeTokenBalances', function (
77
this: any,
88
token: Contract,
9-
accounts: Account[],
9+
accounts: (Account | string)[],
1010
balanceChanges: BigNumberish[]
1111
) {
1212
callPromise(this);
@@ -40,8 +40,10 @@ export function supportChangeTokenBalances(Assertion: Chai.AssertionStatic) {
4040
});
4141
}
4242

43-
function getAddresses(accounts: Account[]) {
44-
return Promise.all(accounts.map((account) => getAddressOf(account)));
43+
function getAddresses(accounts: (Account | string)[]) {
44+
return Promise.all(accounts.map(
45+
(account) => typeof account === 'string' ? account : getAddressOf(account)
46+
));
4547
}
4648

4749
async function getBalances(token: Contract, addresses: string[], blockNumber: number) {

‎waffle-chai/test/matchers/changeEtherBalanceTest.ts

+14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ export const changeEtherBalanceTest = (
3737
).to.changeEtherBalance(sender, '-200');
3838
});
3939

40+
it('Breaks in a predictable way when addresses are passed as string', async () => {
41+
await expect(
42+
expect(() =>
43+
sender.sendTransaction({
44+
to: receiver.address,
45+
value: 200
46+
})
47+
).to.changeEtherBalance(sender.address, '-200')
48+
).to.eventually.be.rejectedWith(
49+
'A string address cannot be used as an account in changeEtherBalance.' +
50+
' Expecting an instance of Ethers Account.'
51+
);
52+
});
53+
4054
it('Should pass when expected balance change is passed as int and is equal to an actual', async () => {
4155
await expect(() =>
4256
sender.sendTransaction({

‎waffle-chai/test/matchers/changeEtherBalancesTest.ts

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ export const changeEtherBalancesTest = (
3838
})
3939
).to.changeEtherBalances([sender, contract], [-200, 200]);
4040
});
41+
42+
it('Breaks in a predictable way when addresses are passed as string', async () => {
43+
await expect(
44+
expect(() =>
45+
sender.sendTransaction({
46+
to: contract.address,
47+
value: 200
48+
})
49+
).to.changeEtherBalances([sender.address, contract.address], [-200, 200])
50+
).to.eventually.be.rejectedWith(
51+
'A string address cannot be used as an account in changeEtherBalances.' +
52+
' Expecting an instance of Ethers Account.'
53+
);
54+
});
4155
});
4256

4357
describe('Change balance, multiple accounts', () => {

‎waffle-chai/test/matchers/changeTokenBalanceTest.ts

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export const changeTokenBalanceTest = (provider: TestProvider) => {
2929
).to.changeTokenBalance(token, receiver, 200);
3030
});
3131

32+
it('Works with address passed as a string', async () => {
33+
await token.approve(receiver.address, 200);
34+
const connectedToken = token.connect(receiver);
35+
await expect(() =>
36+
connectedToken.transferFrom(sender.address, receiver.address, 200)
37+
).to.changeTokenBalance(token, receiver.address, 200);
38+
});
39+
3240
it('Should pass when expected balance change is passed as string and is equal to an actual', async () => {
3341
await expect(() =>
3442
token.transfer(receiver.address, 200)

‎waffle-chai/test/matchers/changeTokenBalancesTest.ts

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export const changeTokenBalancesTest = (provider: TestProvider) => {
2525
).to.changeTokenBalances(token, [sender, receiver], ['-200', 200]);
2626
});
2727

28+
it('Works with addresses passed as strings', async () => {
29+
await expect(() =>
30+
token.transfer(receiver.address, 200)
31+
).to.changeTokenBalances(token, [sender.address, receiver.address], ['-200', 200]);
32+
});
33+
2834
it('Should pass when negated and numbers don\'t match', async () => {
2935
await expect(() =>
3036
token.transfer(receiver.address, 200)

0 commit comments

Comments
 (0)
Please sign in to comment.