Unverified Commit 6ad275be by Facundo Spagnuolo Committed by GitHub

Improve ERC20s tests coverage (#712)

* Improve StandardToken tests coverage
* Improve BasicToken test coverage
* Improve MintableToken test coverage
* Improve BurnableToken test coverage
* Improve PausableToken tests coverage
parent 7a0bfdfb
import assertRevert from '../helpers/assertRevert'; import assertRevert from '../helpers/assertRevert';
const BasicToken = artifacts.require('BasicTokenMock');
var BasicTokenMock = artifacts.require('BasicTokenMock'); contract('StandardToken', function ([_, owner, recipient, anotherAccount]) {
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
contract('BasicToken', function (accounts) { beforeEach(async function () {
it('should return the correct totalSupply after construction', async function () { this.token = await BasicToken.new(owner, 100);
let token = await BasicTokenMock.new(accounts[0], 100); });
let totalSupply = await token.totalSupply();
describe('total supply', function () {
it('returns the total amount of tokens', async function () {
const totalSupply = await this.token.totalSupply();
assert.equal(totalSupply, 100); assert.equal(totalSupply, 100);
});
}); });
it('should return correct balances after transfer', async function () { describe('balanceOf', function () {
let token = await BasicTokenMock.new(accounts[0], 100); describe('when the requested account has no tokens', function () {
await token.transfer(accounts[1], 100); it('returns zero', async function () {
const balance = await this.token.balanceOf(anotherAccount);
let firstAccountBalance = await token.balanceOf(accounts[0]); assert.equal(balance, 0);
assert.equal(firstAccountBalance, 0); });
});
let secondAccountBalance = await token.balanceOf(accounts[1]); describe('when the requested account has some tokens', function () {
assert.equal(secondAccountBalance, 100); it('returns the total amount of tokens', async function () {
}); const balance = await this.token.balanceOf(owner);
it('should throw an error when trying to transfer more than balance', async function () { assert.equal(balance, 100);
let token = await BasicTokenMock.new(accounts[0], 100); });
await assertRevert(token.transfer(accounts[1], 101)); });
}); });
it('should throw an error when trying to transfer to 0x0', async function () { describe('transfer', function () {
let token = await BasicTokenMock.new(accounts[0], 100); describe('when the recipient is not the zero address', function () {
await assertRevert(token.transfer(0x0, 100)); 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 }));
});
});
}); });
}); });
import assertRevert from '../helpers/assertRevert';
const EVMRevert = require('../helpers/EVMRevert.js');
const BurnableTokenMock = artifacts.require('BurnableTokenMock'); const BurnableTokenMock = artifacts.require('BurnableTokenMock');
const BigNumber = web3.BigNumber;
require('chai') contract('BurnableToken', function ([owner]) {
.use(require('chai-as-promised')) beforeEach(async function () {
.use(require('chai-bignumber')(BigNumber)) this.token = await BurnableTokenMock.new(owner, 1000);
.should(); });
const expect = require('chai').expect; describe('burn', function () {
const from = owner;
contract('BurnableToken', function (accounts) { describe('when the given amount is not greater than balance of the sender', function () {
let token; const amount = 100;
let expectedTokenSupply = new BigNumber(999);
beforeEach(async function () { it('burns the requested amount', async function () {
token = await BurnableTokenMock.new(accounts[0], 1000); await this.token.burn(amount, { from });
});
it('owner should be able to burn tokens', async function () { const balance = await this.token.balanceOf(from);
const { logs } = await token.burn(1, { from: accounts[0] }); assert.equal(balance, 900);
});
const balance = await token.balanceOf(accounts[0]); it('emits a burn event', async function () {
balance.should.be.bignumber.equal(expectedTokenSupply); const { logs } = await this.token.burn(amount, { from });
const totalSupply = await token.totalSupply(); assert.equal(logs.length, 1);
totalSupply.should.be.bignumber.equal(expectedTokenSupply); assert.equal(logs[0].event, 'Burn');
assert.equal(logs[0].args.burner, owner);
assert.equal(logs[0].args.value, amount);
});
});
const event = logs.find(e => e.event === 'Burn'); describe('when the given amount is greater than the balance of the sender', function () {
expect(event).to.exist; const amount = 1001;
});
it('cannot burn more tokens than your balance', async function () { it('reverts', async function () {
await token.burn(2000, { from: accounts[0] }) await assertRevert(this.token.burn(amount, { from }));
.should.be.rejectedWith(EVMRevert); });
});
}); });
}); });
import assertRevert from '../helpers/assertRevert';
const MintableToken = artifacts.require('MintableToken');
import expectThrow from '../helpers/expectThrow'; contract('Mintable', function ([owner, anotherAccount]) {
var MintableToken = artifacts.require('MintableToken');
contract('Mintable', function (accounts) {
let token;
beforeEach(async function () { beforeEach(async function () {
token = await MintableToken.new(); this.token = await MintableToken.new({ from: owner });
}); });
it('should start with a totalSupply of 0', async function () { describe('minting finished', function () {
let totalSupply = await token.totalSupply(); describe('when the token is not finished', function () {
it('returns false', async function () {
const mintingFinished = await this.token.mintingFinished();
assert.equal(mintingFinished, false);
});
});
describe('when the token is finished', function () {
beforeEach(async function () {
await this.token.finishMinting({ from: owner });
});
assert.equal(totalSupply, 0); it('returns true', async function () {
const mintingFinished = await this.token.mintingFinished.call();
assert.equal(mintingFinished, true);
});
});
}); });
it('should return mintingFinished false after construction', async function () { describe('finish minting', function () {
let mintingFinished = await token.mintingFinished(); describe('when the sender is the token owner', function () {
const from = owner;
assert.equal(mintingFinished, false); describe('when the token was not finished', function () {
}); it('finishes token minting', async function () {
await this.token.finishMinting({ from });
const mintingFinished = await this.token.mintingFinished();
assert.equal(mintingFinished, true);
});
it('emits a mint finished event', async function () {
const { logs } = await this.token.finishMinting({ from });
it('should mint a given amount of tokens to a given address', async function () { assert.equal(logs.length, 1);
const result = await token.mint(accounts[0], 100); assert.equal(logs[0].event, 'MintFinished');
assert.equal(result.logs[0].event, 'Mint'); });
assert.equal(result.logs[0].args.to.valueOf(), accounts[0]); });
assert.equal(result.logs[0].args.amount.valueOf(), 100);
assert.equal(result.logs[1].event, 'Transfer');
assert.equal(result.logs[1].args.from.valueOf(), 0x0);
let balance0 = await token.balanceOf(accounts[0]); describe('when the token was already finished', function () {
assert(balance0, 100); beforeEach(async function () {
await this.token.finishMinting({ from });
});
let totalSupply = await token.totalSupply(); it('reverts', async function () {
assert(totalSupply, 100); await assertRevert(this.token.finishMinting({ from }));
});
});
});
describe('when the sender is not the token owner', function () {
const from = anotherAccount;
describe('when the token was not finished', function () {
it('reverts', async function () {
await assertRevert(this.token.finishMinting({ from }));
});
});
describe('when the token was already finished', function () {
beforeEach(async function () {
await this.token.finishMinting({ from: owner });
});
it('reverts', async function () {
await assertRevert(this.token.finishMinting({ from }));
});
});
});
}); });
it('should fail to mint after call to finishMinting', async function () { describe('mint', function () {
await token.finishMinting(); const amount = 100;
assert.equal(await token.mintingFinished(), true);
await expectThrow(token.mint(accounts[0], 100)); describe('when the sender is the token owner', function () {
const from = owner;
describe('when the token was not finished', function () {
it('mints the requested amount', async function () {
await this.token.mint(owner, amount, { from });
const balance = await this.token.balanceOf(owner);
assert.equal(balance, amount);
});
it('emits a mint finished event', async function () {
const { logs } = await this.token.mint(owner, amount, { from });
assert.equal(logs.length, 2);
assert.equal(logs[0].event, 'Mint');
assert.equal(logs[0].args.to, owner);
assert.equal(logs[0].args.amount, amount);
assert.equal(logs[1].event, 'Transfer');
});
});
describe('when the token minting is finished', function () {
beforeEach(async function () {
await this.token.finishMinting({ from });
});
it('reverts', async function () {
await assertRevert(this.token.mint(owner, amount, { from }));
});
});
});
describe('when the sender is not the token owner', function () {
const from = anotherAccount;
describe('when the token was not finished', function () {
it('reverts', async function () {
await assertRevert(this.token.mint(owner, amount, { from }));
});
});
describe('when the token was already finished', function () {
beforeEach(async function () {
await this.token.finishMinting({ from: owner });
});
it('reverts', async function () {
await assertRevert(this.token.mint(owner, amount, { from }));
});
});
});
}); });
}); });
'user strict';
import assertRevert from '../helpers/assertRevert'; import assertRevert from '../helpers/assertRevert';
var PausableTokenMock = artifacts.require('PausableTokenMock'); const PausableToken = artifacts.require('PausableTokenMock');
contract('PausableToken', function (accounts) {
let token;
contract('PausableToken', function ([_, owner, recipient, anotherAccount]) {
beforeEach(async function () { beforeEach(async function () {
token = await PausableTokenMock.new(accounts[0], 100); this.token = await PausableToken.new(owner, 100, { from: owner });
}); });
it('should return paused false after construction', async function () { describe('pause', function () {
let paused = await token.paused(); describe('when the sender is the token owner', function () {
const from = owner;
assert.equal(paused, false); describe('when the token is unpaused', function () {
}); it('pauses the token', async function () {
await this.token.pause({ from });
it('should return paused true after pause', async function () { const paused = await this.token.paused();
await token.pause(); assert.equal(paused, true);
let paused = await token.paused(); });
assert.equal(paused, true); it('emits a paused event', async function () {
}); const { logs } = await this.token.pause({ from });
it('should return paused false after pause and unpause', async function () { assert.equal(logs.length, 1);
await token.pause(); assert.equal(logs[0].event, 'Pause');
await token.unpause(); });
let paused = await token.paused(); });
assert.equal(paused, false); describe('when the token is paused', function () {
}); beforeEach(async function () {
await this.token.pause({ from });
});
it('reverts', async function () {
await assertRevert(this.token.pause({ from }));
});
});
});
it('should be able to transfer if transfers are unpaused', async function () { describe('when the sender is not the token owner', function () {
await token.transfer(accounts[1], 100); const from = anotherAccount;
let balance0 = await token.balanceOf(accounts[0]);
assert.equal(balance0, 0);
let balance1 = await token.balanceOf(accounts[1]); it('reverts', async function () {
assert.equal(balance1, 100); await assertRevert(this.token.pause({ from }));
});
});
}); });
it('should be able to transfer after transfers are paused and unpaused', async function () { describe('unpause', function () {
await token.pause(); describe('when the sender is the token owner', function () {
await token.unpause(); const from = owner;
await token.transfer(accounts[1], 100);
let balance0 = await token.balanceOf(accounts[0]);
assert.equal(balance0, 0);
let balance1 = await token.balanceOf(accounts[1]); describe('when the token is paused', function () {
assert.equal(balance1, 100); beforeEach(async function () {
}); await this.token.pause({ from });
});
it('unpauses the token', async function () {
await this.token.unpause({ from });
it('should throw an error trying to transfer while transactions are paused', async function () { const paused = await this.token.paused();
await token.pause(); assert.equal(paused, false);
await assertRevert(token.transfer(accounts[1], 100)); });
it('emits an unpaused event', async function () {
const { logs } = await this.token.unpause({ from });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Unpause');
});
});
describe('when the token is unpaused', function () {
it('reverts', async function () {
await assertRevert(this.token.unpause({ from }));
});
});
});
describe('when the sender is not the token owner', function () {
const from = anotherAccount;
it('reverts', async function () {
await assertRevert(this.token.unpause({ from }));
});
});
}); });
it('should throw an error trying to transfer from another account while transactions are paused', async function () { describe('pausable token', function () {
await token.pause(); const from = owner;
await assertRevert(token.transferFrom(accounts[0], accounts[1], 100));
describe('paused', function () {
it('is not paused by default', async function () {
const paused = await this.token.paused({ from });
assert.equal(paused, false);
});
it('is paused after being paused', async function () {
await this.token.pause({ from });
const paused = await this.token.paused({ from });
assert.equal(paused, true);
});
it('is not paused after being paused and then unpaused', async function () {
await this.token.pause({ from });
await this.token.unpause({ from });
const paused = await this.token.paused();
assert.equal(paused, false);
});
});
describe('transfer', function () {
it('allows to transfer when unpaused', async function () {
await this.token.transfer(recipient, 100, { from: owner });
const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 0);
const recipientBalance = await this.token.balanceOf(recipient);
assert.equal(recipientBalance, 100);
});
it('allows to transfer when paused and then unpaused', async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });
await this.token.transfer(recipient, 100, { from: owner });
const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 0);
const recipientBalance = await this.token.balanceOf(recipient);
assert.equal(recipientBalance, 100);
});
it('reverts when trying to transfer when paused', async function () {
await this.token.pause({ from: owner });
await assertRevert(this.token.transfer(recipient, 100, { from: owner }));
});
});
describe('approve', function () {
it('allows to approve when unpaused', async function () {
await this.token.approve(anotherAccount, 40, { from: owner });
const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 40);
});
it('allows to transfer when paused and then unpaused', async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });
await this.token.approve(anotherAccount, 40, { from: owner });
const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 40);
});
it('reverts when trying to transfer when paused', async function () {
await this.token.pause({ from: owner });
await assertRevert(this.token.approve(anotherAccount, 40, { from: owner }));
});
});
describe('transfer from', function () {
beforeEach(async function () {
await this.token.approve(anotherAccount, 50, { from: owner });
});
it('allows to transfer from when unpaused', async function () {
await this.token.transferFrom(owner, recipient, 40, { from: anotherAccount });
const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 60);
const recipientBalance = await this.token.balanceOf(recipient);
assert.equal(recipientBalance, 40);
});
it('allows to transfer when paused and then unpaused', async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });
await this.token.transferFrom(owner, recipient, 40, { from: anotherAccount });
const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 60);
const recipientBalance = await this.token.balanceOf(recipient);
assert.equal(recipientBalance, 40);
});
it('reverts when trying to transfer from when paused', async function () {
await this.token.pause({ from: owner });
await assertRevert(this.token.transferFrom(owner, recipient, 40, { from: anotherAccount }));
});
});
describe('decrease approval', function () {
beforeEach(async function () {
await this.token.approve(anotherAccount, 100, { from: owner });
});
it('allows to decrease approval when unpaused', async function () {
await this.token.decreaseApproval(anotherAccount, 40, { from: owner });
const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 60);
});
it('allows to decrease approval when paused and then unpaused', async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });
await this.token.decreaseApproval(anotherAccount, 40, { from: owner });
const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 60);
});
it('reverts when trying to transfer when paused', async function () {
await this.token.pause({ from: owner });
await assertRevert(this.token.decreaseApproval(anotherAccount, 40, { from: owner }));
});
});
describe('increase approval', function () {
beforeEach(async function () {
await this.token.approve(anotherAccount, 100, { from: owner });
});
it('allows to increase approval when unpaused', async function () {
await this.token.increaseApproval(anotherAccount, 40, { from: owner });
const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 140);
});
it('allows to increase approval when paused and then unpaused', async function () {
await this.token.pause({ from: owner });
await this.token.unpause({ from: owner });
await this.token.increaseApproval(anotherAccount, 40, { from: owner });
const allowance = await this.token.allowance(owner, anotherAccount);
assert.equal(allowance, 140);
});
it('reverts when trying to increase approval when paused', async function () {
await this.token.pause({ from: owner });
await assertRevert(this.token.increaseApproval(anotherAccount, 40, { from: owner }));
});
});
}); });
}); });
import assertRevert from '../helpers/assertRevert'; import assertRevert from '../helpers/assertRevert';
const StandardTokenMock = artifacts.require('StandardTokenMock');
var StandardTokenMock = artifacts.require('StandardTokenMock'); contract('StandardToken', function ([_, owner, recipient, anotherAccount]) {
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
contract('StandardToken', function (accounts) {
let token;
beforeEach(async function () { beforeEach(async function () {
token = await StandardTokenMock.new(accounts[0], 100); this.token = await StandardTokenMock.new(owner, 100);
}); });
it('should return the correct totalSupply after construction', async function () { describe('total supply', function () {
let totalSupply = await token.totalSupply(); it('returns the total amount of tokens', async function () {
const totalSupply = await this.token.totalSupply();
assert.equal(totalSupply, 100); assert.equal(totalSupply, 100);
});
}); });
it('should return the correct allowance amount after approval', async function () { describe('balanceOf', function () {
let token = await StandardTokenMock.new(); describe('when the requested account has no tokens', function () {
await token.approve(accounts[1], 100); it('returns zero', async function () {
let allowance = await token.allowance(accounts[0], accounts[1]); const balance = await this.token.balanceOf(anotherAccount);
assert.equal(allowance, 100); assert.equal(balance, 0);
}); });
});
it('should return correct balances after transfer', async function () { describe('when the requested account has some tokens', function () {
let token = await StandardTokenMock.new(accounts[0], 100); it('returns the total amount of tokens', async function () {
await token.transfer(accounts[1], 100); const balance = await this.token.balanceOf(owner);
let balance0 = await token.balanceOf(accounts[0]);
assert.equal(balance0, 0);
let balance1 = await token.balanceOf(accounts[1]); assert.equal(balance, 100);
assert.equal(balance1, 100); });
});
}); });
it('should throw an error when trying to transfer more than balance', async function () { describe('transfer', function () {
let token = await StandardTokenMock.new(accounts[0], 100); describe('when the recipient is not the zero address', function () {
await assertRevert(token.transfer(accounts[1], 101)); const to = recipient;
});
it('should return correct balances after transfering from another account', async function () { describe('when the sender does not have enough balance', function () {
let token = await StandardTokenMock.new(accounts[0], 100); const amount = 101;
await token.approve(accounts[1], 100);
await token.transferFrom(accounts[0], accounts[2], 100, { from: accounts[1] });
let balance0 = await token.balanceOf(accounts[0]); it('reverts', async function () {
assert.equal(balance0, 0); await assertRevert(this.token.transfer(to, amount, { from: owner }));
});
});
let balance1 = await token.balanceOf(accounts[2]); describe('when the sender has enough balance', function () {
assert.equal(balance1, 100); const amount = 100;
let balance2 = await token.balanceOf(accounts[1]); it('transfers the requested amount', async function () {
assert.equal(balance2, 0); await this.token.transfer(to, amount, { from: owner });
});
it('should throw an error when trying to transfer more than allowed', async function () { const senderBalance = await this.token.balanceOf(owner);
await token.approve(accounts[1], 99); assert.equal(senderBalance, 0);
await assertRevert(token.transferFrom(accounts[0], accounts[2], 100, { from: accounts[1] }));
}); 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));
});
});
});
it('should throw an error when trying to transferFrom more than _from has', async function () { describe('when the recipient is the zero address', function () {
let balance0 = await token.balanceOf(accounts[0]); const to = ZERO_ADDRESS;
await token.approve(accounts[1], 99);
await assertRevert(token.transferFrom(accounts[0], accounts[2], balance0 + 1, { from: accounts[1] })); it('reverts', async function () {
await assertRevert(this.token.transfer(to, 100, { from: owner }));
});
});
}); });
describe('validating allowance updates to spender', function () { describe('approve', function () {
let preApproved; describe('when the spender is not the zero address', function () {
const spender = recipient;
describe('when the sender has enough balance', function () {
const amount = 100;
it('emits an approval event', async function () {
const { logs } = await this.token.approve(spender, amount, { from: owner });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Approval');
assert.equal(logs[0].args.owner, owner);
assert.equal(logs[0].args.spender, spender);
assert(logs[0].args.value.eq(amount));
});
describe('when there was no approved amount before', function () {
it('approves the requested amount', async function () {
await this.token.approve(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, amount);
});
});
describe('when the spender had an approved amount', function () {
beforeEach(async function () {
await this.token.approve(spender, 1, { from: owner });
});
it('approves the requested amount and replaces the previous one', async function () {
await this.token.approve(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, amount);
});
});
});
describe('when the sender does not have enough balance', function () {
const amount = 101;
it('emits an approval event', async function () {
const { logs } = await this.token.approve(spender, amount, { from: owner });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Approval');
assert.equal(logs[0].args.owner, owner);
assert.equal(logs[0].args.spender, spender);
assert(logs[0].args.value.eq(amount));
});
it('should start with zero', async function () { describe('when there was no approved amount before', function () {
preApproved = await token.allowance(accounts[0], accounts[1]); it('approves the requested amount', async function () {
assert.equal(preApproved, 0); await this.token.approve(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, amount);
});
});
describe('when the spender had an approved amount', function () {
beforeEach(async function () {
await this.token.approve(spender, 1, { from: owner });
});
it('approves the requested amount and replaces the previous one', async function () {
await this.token.approve(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, amount);
});
});
});
}); });
it('should increase by 50 then decrease by 10', async function () { describe('when the spender is the zero address', function () {
await token.increaseApproval(accounts[1], 50); const amount = 100;
let postIncrease = await token.allowance(accounts[0], accounts[1]); const spender = ZERO_ADDRESS;
preApproved.plus(50).should.be.bignumber.equal(postIncrease);
await token.decreaseApproval(accounts[1], 10); it('approves the requested amount', async function () {
let postDecrease = await token.allowance(accounts[0], accounts[1]); await this.token.approve(spender, amount, { from: owner });
postIncrease.minus(10).should.be.bignumber.equal(postDecrease);
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, amount);
});
it('emits an approval event', async function () {
const { logs } = await this.token.approve(spender, amount, { from: owner });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Approval');
assert.equal(logs[0].args.owner, owner);
assert.equal(logs[0].args.spender, spender);
assert(logs[0].args.value.eq(amount));
});
}); });
}); });
it('should increase by 50 then set to 0 when decreasing by more than 50', async function () { describe('transfer from', function () {
await token.approve(accounts[1], 50); const spender = recipient;
await token.decreaseApproval(accounts[1], 60);
let postDecrease = await token.allowance(accounts[0], accounts[1]); describe('when the recipient is not the zero address', function () {
postDecrease.should.be.bignumber.equal(0); const to = anotherAccount;
describe('when the spender has enough approved balance', function () {
beforeEach(async function () {
await this.token.approve(spender, 100, { from: owner });
});
describe('when the owner has enough balance', function () {
const amount = 100;
it('transfers the requested amount', async function () {
await this.token.transferFrom(owner, to, amount, { from: spender });
const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 0);
const recipientBalance = await this.token.balanceOf(to);
assert.equal(recipientBalance, amount);
});
it('decreases the spender allowance', async function () {
await this.token.transferFrom(owner, to, amount, { from: spender });
const allowance = await this.token.allowance(owner, spender);
assert(allowance.eq(0));
});
it('emits a transfer event', async function () {
const { logs } = await this.token.transferFrom(owner, to, amount, { from: spender });
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 owner does not have enough balance', function () {
const amount = 101;
it('reverts', async function () {
await assertRevert(this.token.transferFrom(owner, to, amount, { from: spender }));
});
});
});
describe('when the spender does not have enough approved balance', function () {
beforeEach(async function () {
await this.token.approve(spender, 99, { from: owner });
});
describe('when the owner has enough balance', function () {
const amount = 100;
it('reverts', async function () {
await assertRevert(this.token.transferFrom(owner, to, amount, { from: spender }));
});
});
describe('when the owner does not have enough balance', function () {
const amount = 101;
it('reverts', async function () {
await assertRevert(this.token.transferFrom(owner, to, amount, { from: spender }));
});
});
});
});
describe('when the recipient is the zero address', function () {
const amount = 100;
const to = ZERO_ADDRESS;
beforeEach(async function () {
await this.token.approve(spender, amount, { from: owner });
});
it('reverts', async function () {
await assertRevert(this.token.transferFrom(owner, to, amount, { from: spender }));
});
});
}); });
it('should throw an error when trying to transfer to 0x0', async function () { describe('decrease approval', function () {
let token = await StandardTokenMock.new(accounts[0], 100); describe('when the spender is not the zero address', function () {
await assertRevert(token.transfer(0x0, 100)); const spender = recipient;
describe('when the sender has enough balance', function () {
const amount = 100;
it('emits an approval event', async function () {
const { logs } = await this.token.decreaseApproval(spender, amount, { from: owner });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Approval');
assert.equal(logs[0].args.owner, owner);
assert.equal(logs[0].args.spender, spender);
assert(logs[0].args.value.eq(0));
});
describe('when there was no approved amount before', function () {
it('keeps the allowance to zero', async function () {
await this.token.decreaseApproval(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, 0);
});
});
describe('when the spender had an approved amount', function () {
beforeEach(async function () {
await this.token.approve(spender, amount + 1, { from: owner });
});
it('decreases the spender allowance subtracting the requested amount', async function () {
await this.token.decreaseApproval(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, 1);
});
});
});
describe('when the sender does not have enough balance', function () {
const amount = 101;
it('emits an approval event', async function () {
const { logs } = await this.token.decreaseApproval(spender, amount, { from: owner });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Approval');
assert.equal(logs[0].args.owner, owner);
assert.equal(logs[0].args.spender, spender);
assert(logs[0].args.value.eq(0));
});
describe('when there was no approved amount before', function () {
it('keeps the allowance to zero', async function () {
await this.token.decreaseApproval(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, 0);
});
});
describe('when the spender had an approved amount', function () {
beforeEach(async function () {
await this.token.approve(spender, amount + 1, { from: owner });
});
it('decreases the spender allowance subtracting the requested amount', async function () {
await this.token.decreaseApproval(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, 1);
});
});
});
});
describe('when the spender is the zero address', function () {
const amount = 100;
const spender = ZERO_ADDRESS;
it('decreases the requested amount', async function () {
await this.token.decreaseApproval(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, 0);
});
it('emits an approval event', async function () {
const { logs } = await this.token.decreaseApproval(spender, amount, { from: owner });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Approval');
assert.equal(logs[0].args.owner, owner);
assert.equal(logs[0].args.spender, spender);
assert(logs[0].args.value.eq(0));
});
});
}); });
it('should throw an error when trying to transferFrom to 0x0', async function () { describe('increase approval', function () {
let token = await StandardTokenMock.new(accounts[0], 100); const amount = 100;
await token.approve(accounts[1], 100);
await assertRevert(token.transferFrom(accounts[0], 0x0, 100, { from: accounts[1] })); describe('when the spender is not the zero address', function () {
const spender = recipient;
describe('when the sender has enough balance', function () {
it('emits an approval event', async function () {
const { logs } = await this.token.increaseApproval(spender, amount, { from: owner });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Approval');
assert.equal(logs[0].args.owner, owner);
assert.equal(logs[0].args.spender, spender);
assert(logs[0].args.value.eq(amount));
});
describe('when there was no approved amount before', function () {
it('approves the requested amount', async function () {
await this.token.increaseApproval(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, amount);
});
});
describe('when the spender had an approved amount', function () {
beforeEach(async function () {
await this.token.approve(spender, 1, { from: owner });
});
it('increases the spender allowance adding the requested amount', async function () {
await this.token.increaseApproval(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, amount + 1);
});
});
});
describe('when the sender does not have enough balance', function () {
const amount = 101;
it('emits an approval event', async function () {
const { logs } = await this.token.increaseApproval(spender, amount, { from: owner });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Approval');
assert.equal(logs[0].args.owner, owner);
assert.equal(logs[0].args.spender, spender);
assert(logs[0].args.value.eq(amount));
});
describe('when there was no approved amount before', function () {
it('approves the requested amount', async function () {
await this.token.increaseApproval(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, amount);
});
});
describe('when the spender had an approved amount', function () {
beforeEach(async function () {
await this.token.approve(spender, 1, { from: owner });
});
it('increases the spender allowance adding the requested amount', async function () {
await this.token.increaseApproval(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, amount + 1);
});
});
});
});
describe('when the spender is the zero address', function () {
const spender = ZERO_ADDRESS;
it('approves the requested amount', async function () {
await this.token.increaseApproval(spender, amount, { from: owner });
const allowance = await this.token.allowance(owner, spender);
assert.equal(allowance, amount);
});
it('emits an approval event', async function () {
const { logs } = await this.token.increaseApproval(spender, amount, { from: owner });
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Approval');
assert.equal(logs[0].args.owner, owner);
assert.equal(logs[0].args.spender, spender);
assert(logs[0].args.value.eq(amount));
});
});
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment