Merge tag 'v2.2.0' of github.com:OpenZeppelin/openzeppelin-solidity
v2.2.0
This commit is contained in:
@ -73,89 +73,6 @@ contract('ERC20', function ([_, initialHolder, recipient, anotherAccount]) {
|
||||
});
|
||||
});
|
||||
|
||||
describe('approve', function () {
|
||||
describe('when the spender is not the zero address', function () {
|
||||
const spender = recipient;
|
||||
|
||||
describe('when the sender has enough balance', function () {
|
||||
const amount = initialSupply;
|
||||
|
||||
it('emits an approval event', async function () {
|
||||
const { logs } = await this.token.approve(spender, amount, { from: initialHolder });
|
||||
|
||||
expectEvent.inLogs(logs, 'Approval', {
|
||||
owner: initialHolder,
|
||||
spender: spender,
|
||||
value: amount,
|
||||
});
|
||||
});
|
||||
|
||||
describe('when there was no approved amount before', function () {
|
||||
it('approves the requested amount', async function () {
|
||||
await this.token.approve(spender, amount, { from: initialHolder });
|
||||
|
||||
(await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal(amount);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the spender had an approved amount', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.approve(spender, new BN(1), { from: initialHolder });
|
||||
});
|
||||
|
||||
it('approves the requested amount and replaces the previous one', async function () {
|
||||
await this.token.approve(spender, amount, { from: initialHolder });
|
||||
|
||||
(await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal(amount);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the sender does not have enough balance', function () {
|
||||
const amount = initialSupply.addn(1);
|
||||
|
||||
it('emits an approval event', async function () {
|
||||
const { logs } = await this.token.approve(spender, amount, { from: initialHolder });
|
||||
|
||||
expectEvent.inLogs(logs, 'Approval', {
|
||||
owner: initialHolder,
|
||||
spender: spender,
|
||||
value: amount,
|
||||
});
|
||||
});
|
||||
|
||||
describe('when there was no approved amount before', function () {
|
||||
it('approves the requested amount', async function () {
|
||||
await this.token.approve(spender, amount, { from: initialHolder });
|
||||
|
||||
(await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal(amount);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the spender had an approved amount', function () {
|
||||
beforeEach(async function () {
|
||||
await this.token.approve(spender, new BN(1), { from: initialHolder });
|
||||
});
|
||||
|
||||
it('approves the requested amount and replaces the previous one', async function () {
|
||||
await this.token.approve(spender, amount, { from: initialHolder });
|
||||
|
||||
(await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal(amount);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the spender is the zero address', function () {
|
||||
const amount = initialSupply;
|
||||
const spender = ZERO_ADDRESS;
|
||||
|
||||
it('reverts', async function () {
|
||||
await shouldFail.reverting(this.token.approve(spender, amount, { from: initialHolder }));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('transfer from', function () {
|
||||
const spender = recipient;
|
||||
|
||||
@ -546,4 +463,100 @@ contract('ERC20', function ([_, initialHolder, recipient, anotherAccount]) {
|
||||
describeBurnFrom('for less amount than allowance', allowance.subn(1));
|
||||
});
|
||||
});
|
||||
|
||||
describe('approve', function () {
|
||||
testApprove(initialHolder, recipient, initialSupply, function (owner, spender, amount) {
|
||||
return this.token.approve(spender, amount, { from: owner });
|
||||
});
|
||||
});
|
||||
|
||||
describe('_approve', function () {
|
||||
testApprove(initialHolder, recipient, initialSupply, function (owner, spender, amount) {
|
||||
return this.token.approveInternal(owner, spender, amount);
|
||||
});
|
||||
|
||||
describe('when the owner is the zero address', function () {
|
||||
it('reverts', async function () {
|
||||
await shouldFail.reverting(this.token.approveInternal(ZERO_ADDRESS, recipient, initialSupply));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function testApprove (owner, spender, supply, approve) {
|
||||
describe('when the spender is not the zero address', function () {
|
||||
describe('when the sender has enough balance', function () {
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
describe('when there was no approved amount before', function () {
|
||||
it('approves the requested amount', async function () {
|
||||
await approve.call(this, owner, spender, amount);
|
||||
|
||||
(await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the spender had an approved amount', function () {
|
||||
beforeEach(async function () {
|
||||
await approve.call(this, owner, spender, new BN(1));
|
||||
});
|
||||
|
||||
it('approves the requested amount and replaces the previous one', async function () {
|
||||
await approve.call(this, owner, spender, amount);
|
||||
|
||||
(await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the sender does not have enough balance', function () {
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
describe('when there was no approved amount before', function () {
|
||||
it('approves the requested amount', async function () {
|
||||
await approve.call(this, owner, spender, amount);
|
||||
|
||||
(await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the spender had an approved amount', function () {
|
||||
beforeEach(async function () {
|
||||
await approve.call(this, owner, spender, new BN(1));
|
||||
});
|
||||
|
||||
it('approves the requested amount and replaces the previous one', async function () {
|
||||
await approve.call(this, owner, spender, amount);
|
||||
|
||||
(await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the spender is the zero address', function () {
|
||||
it('reverts', async function () {
|
||||
await shouldFail.reverting(approve.call(this, owner, ZERO_ADDRESS, supply));
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@ -1,91 +1,122 @@
|
||||
const { shouldFail } = require('openzeppelin-test-helpers');
|
||||
|
||||
const SafeERC20Helper = artifacts.require('SafeERC20Helper');
|
||||
const ERC20ReturnFalseMock = artifacts.require('ERC20ReturnFalseMock');
|
||||
const ERC20ReturnTrueMock = artifacts.require('ERC20ReturnTrueMock');
|
||||
const ERC20NoReturnMock = artifacts.require('ERC20NoReturnMock');
|
||||
const SafeERC20Wrapper = artifacts.require('SafeERC20Wrapper');
|
||||
|
||||
contract('SafeERC20', function () {
|
||||
beforeEach(async function () {
|
||||
this.helper = await SafeERC20Helper.new();
|
||||
contract('SafeERC20', function ([_, hasNoCode]) {
|
||||
describe('with address that has no contract code', function () {
|
||||
beforeEach(async function () {
|
||||
this.wrapper = await SafeERC20Wrapper.new(hasNoCode);
|
||||
});
|
||||
|
||||
shouldRevertOnAllCalls();
|
||||
});
|
||||
|
||||
describe('with token that returns false on all calls', function () {
|
||||
it('reverts on transfer', async function () {
|
||||
await shouldFail.reverting(this.helper.doFailingTransfer());
|
||||
beforeEach(async function () {
|
||||
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnFalseMock.new()).address);
|
||||
});
|
||||
|
||||
it('reverts on transferFrom', async function () {
|
||||
await shouldFail.reverting(this.helper.doFailingTransferFrom());
|
||||
});
|
||||
|
||||
it('reverts on approve', async function () {
|
||||
await shouldFail.reverting(this.helper.doFailingApprove());
|
||||
});
|
||||
|
||||
it('reverts on increaseAllowance', async function () {
|
||||
await shouldFail.reverting(this.helper.doFailingIncreaseAllowance());
|
||||
});
|
||||
|
||||
it('reverts on decreaseAllowance', async function () {
|
||||
await shouldFail.reverting(this.helper.doFailingDecreaseAllowance());
|
||||
});
|
||||
shouldRevertOnAllCalls();
|
||||
});
|
||||
|
||||
describe('with token that returns true on all calls', function () {
|
||||
it('doesn\'t revert on transfer', async function () {
|
||||
await this.helper.doSucceedingTransfer();
|
||||
beforeEach(async function () {
|
||||
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnTrueMock.new()).address);
|
||||
});
|
||||
|
||||
it('doesn\'t revert on transferFrom', async function () {
|
||||
await this.helper.doSucceedingTransferFrom();
|
||||
shouldOnlyRevertOnErrors();
|
||||
});
|
||||
|
||||
describe('with token that returns no boolean values', function () {
|
||||
beforeEach(async function () {
|
||||
this.wrapper = await SafeERC20Wrapper.new((await ERC20NoReturnMock.new()).address);
|
||||
});
|
||||
|
||||
describe('approvals', function () {
|
||||
context('with zero allowance', function () {
|
||||
beforeEach(async function () {
|
||||
await this.helper.setAllowance(0);
|
||||
});
|
||||
shouldOnlyRevertOnErrors();
|
||||
});
|
||||
});
|
||||
|
||||
it('doesn\'t revert when approving a non-zero allowance', async function () {
|
||||
await this.helper.doSucceedingApprove(100);
|
||||
});
|
||||
function shouldRevertOnAllCalls () {
|
||||
it('reverts on transfer', async function () {
|
||||
await shouldFail.reverting(this.wrapper.transfer());
|
||||
});
|
||||
|
||||
it('doesn\'t revert when approving a zero allowance', async function () {
|
||||
await this.helper.doSucceedingApprove(0);
|
||||
});
|
||||
it('reverts on transferFrom', async function () {
|
||||
await shouldFail.reverting(this.wrapper.transferFrom());
|
||||
});
|
||||
|
||||
it('doesn\'t revert when increasing the allowance', async function () {
|
||||
await this.helper.doSucceedingIncreaseAllowance(10);
|
||||
});
|
||||
it('reverts on approve', async function () {
|
||||
await shouldFail.reverting(this.wrapper.approve(0));
|
||||
});
|
||||
|
||||
it('reverts when decreasing the allowance', async function () {
|
||||
await shouldFail.reverting(this.helper.doSucceedingDecreaseAllowance(10));
|
||||
});
|
||||
it('reverts on increaseAllowance', async function () {
|
||||
await shouldFail.reverting(this.wrapper.increaseAllowance(0));
|
||||
});
|
||||
|
||||
it('reverts on decreaseAllowance', async function () {
|
||||
await shouldFail.reverting(this.wrapper.decreaseAllowance(0));
|
||||
});
|
||||
}
|
||||
|
||||
function shouldOnlyRevertOnErrors () {
|
||||
it('doesn\'t revert on transfer', async function () {
|
||||
await this.wrapper.transfer();
|
||||
});
|
||||
|
||||
it('doesn\'t revert on transferFrom', async function () {
|
||||
await this.wrapper.transferFrom();
|
||||
});
|
||||
|
||||
describe('approvals', function () {
|
||||
context('with zero allowance', function () {
|
||||
beforeEach(async function () {
|
||||
await this.wrapper.setAllowance(0);
|
||||
});
|
||||
|
||||
context('with non-zero allowance', function () {
|
||||
beforeEach(async function () {
|
||||
await this.helper.setAllowance(100);
|
||||
});
|
||||
it('doesn\'t revert when approving a non-zero allowance', async function () {
|
||||
await this.wrapper.approve(100);
|
||||
});
|
||||
|
||||
it('reverts when approving a non-zero allowance', async function () {
|
||||
await shouldFail.reverting(this.helper.doSucceedingApprove(20));
|
||||
});
|
||||
it('doesn\'t revert when approving a zero allowance', async function () {
|
||||
await this.wrapper.approve(0);
|
||||
});
|
||||
|
||||
it('doesn\'t revert when approving a zero allowance', async function () {
|
||||
await this.helper.doSucceedingApprove(0);
|
||||
});
|
||||
it('doesn\'t revert when increasing the allowance', async function () {
|
||||
await this.wrapper.increaseAllowance(10);
|
||||
});
|
||||
|
||||
it('doesn\'t revert when increasing the allowance', async function () {
|
||||
await this.helper.doSucceedingIncreaseAllowance(10);
|
||||
});
|
||||
it('reverts when decreasing the allowance', async function () {
|
||||
await shouldFail.reverting(this.wrapper.decreaseAllowance(10));
|
||||
});
|
||||
});
|
||||
|
||||
it('doesn\'t revert when decreasing the allowance to a positive value', async function () {
|
||||
await this.helper.doSucceedingDecreaseAllowance(50);
|
||||
});
|
||||
context('with non-zero allowance', function () {
|
||||
beforeEach(async function () {
|
||||
await this.wrapper.setAllowance(100);
|
||||
});
|
||||
|
||||
it('reverts when decreasing the allowance to a negative value', async function () {
|
||||
await shouldFail.reverting(this.helper.doSucceedingDecreaseAllowance(200));
|
||||
});
|
||||
it('reverts when approving a non-zero allowance', async function () {
|
||||
await shouldFail.reverting(this.wrapper.approve(20));
|
||||
});
|
||||
|
||||
it('doesn\'t revert when approving a zero allowance', async function () {
|
||||
await this.wrapper.approve(0);
|
||||
});
|
||||
|
||||
it('doesn\'t revert when increasing the allowance', async function () {
|
||||
await this.wrapper.increaseAllowance(10);
|
||||
});
|
||||
|
||||
it('doesn\'t revert when decreasing the allowance to a positive value', async function () {
|
||||
await this.wrapper.decreaseAllowance(50);
|
||||
});
|
||||
|
||||
it('reverts when decreasing the allowance to a negative value', async function () {
|
||||
await shouldFail.reverting(this.wrapper.decreaseAllowance(200));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ function shouldBehaveLikeERC721PausedToken (owner, [recipient, operator]) {
|
||||
});
|
||||
|
||||
describe('exists', function () {
|
||||
it('should return token existance', async function () {
|
||||
it('should return token existence', async function () {
|
||||
(await this.token.exists(firstTokenId)).should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user