Consolidated ERC20 Interface and Implementation Files (#1125)

* Consolidted ERC20 Interface and Implementation Files

* Fixed CanReclaimToken's tests to use StandardTokenMock instead of BasicTokenMock

* Changed token's variable type in TokenTimelock to ERC20

* Merged the StandardBurnableToken with BurnableToken since it now inherits from StandardToken; Fixed TokenTimelock so it uses SafeERC20 for ERC20

* Fixed variable type for _token in TokenTimelock constructor

* Fixed linting warning in BurnableToken

* Added back burnFrom tests.
This commit is contained in:
Doug Crescenzi
2018-08-02 22:12:31 -04:00
committed by Francisco Giordano
parent 3d86c58d2c
commit ef347ffccc
22 changed files with 183 additions and 344 deletions

View File

@ -1,7 +1,7 @@
const { expectThrow } = require('../helpers/expectThrow');
const CanReclaimToken = artifacts.require('CanReclaimToken');
const BasicTokenMock = artifacts.require('BasicTokenMock');
const StandardTokenMock = artifacts.require('StandardTokenMock');
contract('CanReclaimToken', function ([_, owner, anyone]) {
let token = null;
@ -9,8 +9,9 @@ contract('CanReclaimToken', function ([_, owner, anyone]) {
beforeEach(async function () {
// Create contract and token
token = await BasicTokenMock.new(owner, 100, { from: owner });
token = await StandardTokenMock.new(owner, 100, { from: owner });
canReclaimToken = await CanReclaimToken.new({ from: owner });
// Force token into contract
await token.transfer(canReclaimToken.address, 10, { from: owner });
const startBalance = await token.balanceOf(canReclaimToken.address);

View File

@ -1,82 +0,0 @@
const { assertRevert } = require('../../helpers/assertRevert');
const BasicToken = artifacts.require('BasicTokenMock');
contract('StandardToken', function ([_, owner, recipient, anotherAccount]) {
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
beforeEach(async function () {
this.token = await BasicToken.new(owner, 100);
});
describe('total supply', function () {
it('returns the total amount of tokens', async function () {
const totalSupply = await this.token.totalSupply();
assert.equal(totalSupply, 100);
});
});
describe('balanceOf', function () {
describe('when the requested account has no tokens', function () {
it('returns zero', async function () {
const balance = await this.token.balanceOf(anotherAccount);
assert.equal(balance, 0);
});
});
describe('when the requested account has some tokens', function () {
it('returns the total amount of tokens', async function () {
const balance = await this.token.balanceOf(owner);
assert.equal(balance, 100);
});
});
});
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 = 101;
it('reverts', async function () {
await assertRevert(this.token.transfer(to, amount, { from: owner }));
});
});
describe('when the sender has enough balance', function () {
const amount = 100;
it('transfers the requested amount', async function () {
await this.token.transfer(to, amount, { from: owner });
const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 0);
const recipientBalance = await this.token.balanceOf(to);
assert.equal(recipientBalance, amount);
});
it('emits a transfer event', async function () {
const { logs } = await this.token.transfer(to, amount, { from: owner });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Transfer');
assert.equal(logs[0].args.from, owner);
assert.equal(logs[0].args.to, to);
assert(logs[0].args.value.eq(amount));
});
});
});
describe('when the recipient is the zero address', function () {
const to = ZERO_ADDRESS;
it('reverts', async function () {
await assertRevert(this.token.transfer(to, 100, { from: owner }));
});
});
});
});

View File

@ -8,8 +8,8 @@ require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
function shouldBehaveLikeBurnableToken (owner, initialBalance) {
describe('as a basic burnable token', function () {
function shouldBehaveLikeBurnableToken (owner, initialBalance, [burner]) {
describe('burn', function () {
describe('when the given amount is not greater than balance of the sender', function () {
const amount = 100;
@ -44,6 +44,57 @@ function shouldBehaveLikeBurnableToken (owner, initialBalance) {
});
});
});
describe('burnFrom', function () {
describe('on success', function () {
const amount = 100;
beforeEach(async function () {
await this.token.approve(burner, 300, { from: owner });
const { logs } = await this.token.burnFrom(owner, amount, { from: burner });
this.logs = logs;
});
it('burns the requested amount', async function () {
const balance = await this.token.balanceOf(owner);
balance.should.be.bignumber.equal(initialBalance - amount);
});
it('decrements allowance', async function () {
const allowance = await this.token.allowance(owner, burner);
allowance.should.be.bignumber.equal(200);
});
it('emits a burn event', async function () {
const event = await inLogs(this.logs, 'Burn');
event.args.burner.should.eq(owner);
event.args.value.should.be.bignumber.equal(amount);
});
it('emits a transfer event', async function () {
const event = await inLogs(this.logs, 'Transfer');
event.args.from.should.eq(owner);
event.args.to.should.eq(ZERO_ADDRESS);
event.args.value.should.be.bignumber.equal(amount);
});
});
describe('when the given amount is greater than the balance of the sender', function () {
const amount = initialBalance + 1;
it('reverts', async function () {
await this.token.approve(burner, amount, { from: owner });
await assertRevert(this.token.burnFrom(owner, amount, { from: burner }));
});
});
describe('when the given amount is greater than the allowance', function () {
const amount = 100;
it('reverts', async function () {
await this.token.approve(burner, amount - 1, { from: owner });
await assertRevert(this.token.burnFrom(owner, amount, { from: burner }));
});
});
});
}
module.exports = {

View File

@ -1,12 +1,12 @@
const { shouldBehaveLikeBurnableToken } = require('./BurnableToken.behaviour');
const BurnableTokenMock = artifacts.require('BurnableTokenMock');
contract('BurnableToken', function ([_, owner]) {
contract('BurnableToken', function ([_, owner, ...otherAccounts]) {
const initialBalance = 1000;
beforeEach(async function () {
this.token = await BurnableTokenMock.new(owner, initialBalance, { from: owner });
});
shouldBehaveLikeBurnableToken(owner, initialBalance);
shouldBehaveLikeBurnableToken(owner, initialBalance, otherAccounts);
});

View File

@ -1,72 +0,0 @@
const { assertRevert } = require('../../helpers/assertRevert');
const { inLogs } = require('../../helpers/expectEvent');
const { shouldBehaveLikeBurnableToken } = require('./BurnableToken.behaviour');
const StandardBurnableTokenMock = artifacts.require('StandardBurnableTokenMock');
const BigNumber = web3.BigNumber;
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
contract('StandardBurnableToken', function ([_, owner, burner]) {
const initialBalance = 1000;
beforeEach(async function () {
this.token = await StandardBurnableTokenMock.new(owner, initialBalance);
});
shouldBehaveLikeBurnableToken(owner, initialBalance);
describe('burnFrom', function () {
describe('on success', function () {
const amount = 100;
beforeEach(async function () {
await this.token.approve(burner, 300, { from: owner });
const { logs } = await this.token.burnFrom(owner, amount, { from: burner });
this.logs = logs;
});
it('burns the requested amount', async function () {
const balance = await this.token.balanceOf(owner);
balance.should.be.bignumber.equal(initialBalance - amount);
});
it('decrements allowance', async function () {
const allowance = await this.token.allowance(owner, burner);
allowance.should.be.bignumber.equal(200);
});
it('emits a burn event', async function () {
const event = await inLogs(this.logs, 'Burn');
event.args.burner.should.eq(owner);
event.args.value.should.be.bignumber.equal(amount);
});
it('emits a transfer event', async function () {
const event = await inLogs(this.logs, 'Transfer');
event.args.from.should.eq(owner);
event.args.to.should.eq(ZERO_ADDRESS);
event.args.value.should.be.bignumber.equal(amount);
});
});
describe('when the given amount is greater than the balance of the sender', function () {
const amount = initialBalance + 1;
it('reverts', async function () {
await this.token.approve(burner, amount, { from: owner });
await assertRevert(this.token.burnFrom(owner, amount, { from: burner }));
});
});
describe('when the given amount is greater than the allowance', function () {
const amount = 100;
it('reverts', async function () {
await this.token.approve(burner, amount - 1, { from: owner });
await assertRevert(this.token.burnFrom(owner, amount, { from: burner }));
});
});
});
});

View File

@ -1,11 +1,11 @@
const { assertRevert } = require('../../helpers/assertRevert');
const StandardTokenMock = artifacts.require('StandardTokenMock');
const StandardToken = artifacts.require('StandardTokenMock');
contract('StandardToken', function ([_, owner, recipient, anotherAccount]) {
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
beforeEach(async function () {
this.token = await StandardTokenMock.new(owner, 100);
this.token = await StandardToken.new(owner, 100);
});
describe('total supply', function () {