Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow ERC20._transfer from the zero address. #1752

Merged
merged 10 commits into from
May 16, 2019
94 changes: 49 additions & 45 deletions test/token/ERC20/ERC20.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,11 @@ function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recip
});

describe('transfer', function () {
describe('when the recipient is not the zero address', function () {
const to = recipient;

describe('when the sender does not have enough balance', function () {
const amount = initialSupply.addn(1);

it('reverts', async function () {
await shouldFail.reverting.withMessage(this.token.transfer(to, amount, { from: initialHolder }),
'SafeMath: subtraction overflow'
);
});
});

describe('when the sender has enough balance', function () {
const amount = initialSupply;

it('transfers the requested amount', async function () {
await this.token.transfer(to, amount, { from: initialHolder });

(await this.token.balanceOf(initialHolder)).should.be.bignumber.equal('0');

(await this.token.balanceOf(to)).should.be.bignumber.equal(amount);
});

it('emits a transfer event', async function () {
const { logs } = await this.token.transfer(to, amount, { from: initialHolder });

expectEvent.inLogs(logs, 'Transfer', {
from: initialHolder,
to: to,
value: amount,
});
});
});
});

describe('when the recipient is the zero address', function () {
const to = ZERO_ADDRESS;

it('reverts', async function () {
await shouldFail.reverting.withMessage(this.token.transfer(to, initialSupply, { from: initialHolder }),
`${errorPrefix}: transfer to the zero address`
);
});
});
shouldBehaveLikeERC20Transfer('ERC20', initialHolder, recipient, initialSupply,
function(from, to, value) {
return this.token.transfer(to, value, { from });
}
);
});

describe('transfer from', function () {
Expand Down Expand Up @@ -197,6 +157,50 @@ function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recip
});
}

function shouldBehaveLikeERC20Transfer (errorPrefix, from, to, balance, transfer) {
describe('when the recipient is not the zero address', function () {
describe('when the sender does not have enough balance', function () {
const amount = balance.addn(1);

it('reverts', async function () {
await shouldFail.reverting.withMessage(transfer.call(this, from, to, amount),
nventuro marked this conversation as resolved.
Show resolved Hide resolved
'SafeMath: subtraction overflow'
);
});
});

describe('when the sender transfers all balance', function () {
const amount = balance;

it('transfers the requested amount', async function () {
await transfer.call(this, from, to, amount);

(await this.token.balanceOf(from)).should.be.bignumber.equal('0');

(await this.token.balanceOf(to)).should.be.bignumber.equal(amount);
});

it('emits a transfer event', async function () {
const { logs } = await transfer.call(this, from, to, amount);

expectEvent.inLogs(logs, 'Transfer', {
from,
to,
value: amount,
});
});
});
});

describe('when the recipient is the zero address', function () {
it('reverts', async function () {
await shouldFail.reverting.withMessage(transfer.call(this, from, ZERO_ADDRESS, balance),
`${errorPrefix}: transfer to the zero address`
);
});
});
}

function shouldBehaveLikeERC20Approve (errorPrefix, owner, spender, supply, approve) {
describe('when the spender is not the zero address', function () {
describe('when the sender has enough balance', function () {
Expand Down