Merge pull request #341 from lsaether/master
Added contracts/token/BurnableToken.sol
This commit is contained in:
27
contracts/token/BurnableToken.sol
Normal file
27
contracts/token/BurnableToken.sol
Normal file
@ -0,0 +1,27 @@
|
||||
pragma solidity ^0.4.13;
|
||||
|
||||
import './StandardToken.sol';
|
||||
|
||||
/**
|
||||
* @title Burnable Token
|
||||
* @dev Token that can be irreversibly burned (destroyed).
|
||||
*/
|
||||
contract BurnableToken is StandardToken {
|
||||
|
||||
/**
|
||||
* @dev Burns a specific amount of tokens.
|
||||
* @param _value The amount of token to be burned.
|
||||
*/
|
||||
function burn(uint _value)
|
||||
public
|
||||
{
|
||||
require(_value > 0);
|
||||
|
||||
address burner = msg.sender;
|
||||
balances[burner] = balances[burner].sub(_value);
|
||||
totalSupply = totalSupply.sub(_value);
|
||||
Burn(burner, _value);
|
||||
}
|
||||
|
||||
event Burn(address indexed burner, uint indexed value);
|
||||
}
|
||||
3226
package-lock.json
generated
Normal file
3226
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
39
test/BurnableToken.js
Normal file
39
test/BurnableToken.js
Normal file
@ -0,0 +1,39 @@
|
||||
'use strict'
|
||||
|
||||
const EVMThrow = require('./helpers/EVMThrow.js')
|
||||
const BurnableTokenMock = artifacts.require("./helpers/BurnableTokenMock.sol")
|
||||
const BigNumber = web3.BigNumber
|
||||
|
||||
require('chai')
|
||||
.use(require('chai-as-promised'))
|
||||
.use(require('chai-bignumber')(BigNumber))
|
||||
.should()
|
||||
|
||||
const expect = require('chai').expect
|
||||
|
||||
contract('BurnableToken', function (accounts) {
|
||||
let token
|
||||
let expectedTokenSupply = new BigNumber(999)
|
||||
|
||||
beforeEach(async function () {
|
||||
token = await BurnableTokenMock.new(accounts[0], 1000)
|
||||
})
|
||||
|
||||
it('owner should be able to burn tokens', async function () {
|
||||
const { logs } = await token.burn(1, { from: accounts[0] })
|
||||
|
||||
const balance = await token.balanceOf(accounts[0])
|
||||
balance.should.be.bignumber.equal(expectedTokenSupply)
|
||||
|
||||
const totalSupply = await token.totalSupply()
|
||||
totalSupply.should.be.bignumber.equal(expectedTokenSupply)
|
||||
|
||||
const event = logs.find(e => e.event === 'Burn')
|
||||
expect(event).to.exist
|
||||
})
|
||||
|
||||
it('cannot burn more tokens than your balance', async function () {
|
||||
await token.burn(2000, { from: accounts[0] })
|
||||
.should.be.rejectedWith(EVMThrow)
|
||||
})
|
||||
})
|
||||
12
test/helpers/BurnableTokenMock.sol
Normal file
12
test/helpers/BurnableTokenMock.sol
Normal file
@ -0,0 +1,12 @@
|
||||
pragma solidity ^0.4.13;
|
||||
|
||||
import '../../contracts/token/BurnableToken.sol';
|
||||
|
||||
contract BurnableTokenMock is BurnableToken {
|
||||
|
||||
function BurnableTokenMock(address initialAccount, uint initialBalance) {
|
||||
balances[initialAccount] = initialBalance;
|
||||
totalSupply = initialBalance;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user