Reduce ERC20 allowance before triggering transfer (#3056)

* Reduce ERC20 allowance before triggering transfer

* adapt ERC777 to reduce allowance before transfer

* fix test for ERC777

* use smaller number to reduce balance

* simplify test description

* don't use deprecated expectEvents.inLogs

* fix test

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
Francisco Giordano
2021-12-31 06:17:12 -03:00
committed by GitHub
parent ef0273fde1
commit a9f994f063
5 changed files with 90 additions and 93 deletions

View File

@ -40,7 +40,7 @@ function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recip
describe('when the recipient is not the zero address', function () {
const to = anotherAccount;
describe('when the spender has enough approved balance', function () {
describe('when the spender has enough allowance', function () {
beforeEach(async function () {
await this.token.approve(spender, initialSupply, { from: initialHolder });
});
@ -63,58 +63,67 @@ function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recip
});
it('emits a transfer event', async function () {
const { logs } = await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
expectEvent.inLogs(logs, 'Transfer', {
from: tokenOwner,
to: to,
value: amount,
});
expectEvent(
await this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
'Transfer',
{ from: tokenOwner, to: to, value: amount },
);
});
it('emits an approval event', async function () {
const { logs } = await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
expectEvent.inLogs(logs, 'Approval', {
owner: tokenOwner,
spender: spender,
value: await this.token.allowance(tokenOwner, spender),
});
expectEvent(
await this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
'Approval',
{ owner: tokenOwner, spender: spender, value: await this.token.allowance(tokenOwner, spender) },
);
});
});
describe('when the token owner does not have enough balance', function () {
const amount = initialSupply.addn(1);
const amount = initialSupply;
beforeEach('reducing balance', async function () {
await this.token.transfer(to, 1, { from: tokenOwner });
});
it('reverts', async function () {
await expectRevert(this.token.transferFrom(
tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer amount exceeds balance`,
await expectRevert(
this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
`${errorPrefix}: transfer amount exceeds balance`,
);
});
});
});
describe('when the spender does not have enough approved balance', function () {
describe('when the spender does not have enough allowance', function () {
const allowance = initialSupply.subn(1);
beforeEach(async function () {
await this.token.approve(spender, initialSupply.subn(1), { from: tokenOwner });
await this.token.approve(spender, allowance, { from: tokenOwner });
});
describe('when the token owner has enough balance', function () {
const amount = initialSupply;
it('reverts', async function () {
await expectRevert(this.token.transferFrom(
tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer amount exceeds allowance`,
await expectRevert(
this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
`${errorPrefix}: transfer amount exceeds allowance`,
);
});
});
describe('when the token owner does not have enough balance', function () {
const amount = initialSupply.addn(1);
const amount = allowance;
beforeEach('reducing balance', async function () {
await this.token.transfer(to, 2, { from: tokenOwner });
});
it('reverts', async function () {
await expectRevert(this.token.transferFrom(
tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer amount exceeds balance`,
await expectRevert(
this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
`${errorPrefix}: transfer amount exceeds balance`,
);
});
});
@ -143,8 +152,9 @@ function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recip
const to = recipient;
it('reverts', async function () {
await expectRevert(this.token.transferFrom(
tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer from the zero address`,
await expectRevert(
this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
'from the zero address',
);
});
});
@ -183,13 +193,11 @@ function shouldBehaveLikeERC20Transfer (errorPrefix, from, to, balance, transfer
});
it('emits a transfer event', async function () {
const { logs } = await transfer.call(this, from, to, amount);
expectEvent.inLogs(logs, 'Transfer', {
from,
to,
value: amount,
});
expectEvent(
await transfer.call(this, from, to, amount),
'Transfer',
{ from, to, value: amount },
);
});
});
@ -205,13 +213,11 @@ function shouldBehaveLikeERC20Transfer (errorPrefix, from, to, balance, transfer
});
it('emits a transfer event', async function () {
const { logs } = await transfer.call(this, from, to, amount);
expectEvent.inLogs(logs, 'Transfer', {
from,
to,
value: amount,
});
expectEvent(
await transfer.call(this, from, to, amount),
'Transfer',
{ from, to, value: amount },
);
});
});
});
@ -231,13 +237,11 @@ function shouldBehaveLikeERC20Approve (errorPrefix, owner, spender, supply, appr
const amount = supply;
it('emits an approval event', async function () {
const { logs } = await approve.call(this, owner, spender, amount);
expectEvent.inLogs(logs, 'Approval', {
owner: owner,
spender: spender,
value: amount,
});
expectEvent(
await approve.call(this, owner, spender, amount),
'Approval',
{ owner: owner, spender: spender, value: amount },
);
});
describe('when there was no approved amount before', function () {
@ -265,13 +269,11 @@ function shouldBehaveLikeERC20Approve (errorPrefix, owner, spender, supply, appr
const amount = supply.addn(1);
it('emits an approval event', async function () {
const { logs } = await approve.call(this, owner, spender, amount);
expectEvent.inLogs(logs, 'Approval', {
owner: owner,
spender: spender,
value: amount,
});
expectEvent(
await approve.call(this, owner, spender, amount),
'Approval',
{ owner: owner, spender: spender, value: amount },
);
});
describe('when there was no approved amount before', function () {