Add StandardBurnableToken implementation (#870)

* Add StandardBurnableToken implementation

BurnableToken that extends from StandardToken and adds a
burnFrom method that decrements allowance. Equivalent to
a transferFrom plus burn in a single operation.

* Return event object from expectEvent helper

* Add comment on Approval event in burnFrom function

* Improvements on burnable token tests

- Inject initial balance as a parameter to the behaviour
- Use expectEvent helper for assertions on events
- Use chai bignumber for numbers
- Change to bdd-style assertions
This commit is contained in:
Santiago Palladino
2018-04-11 16:15:33 -03:00
committed by GitHub
parent 9e1c934ffd
commit 0926729c8f
7 changed files with 174 additions and 43 deletions

View File

@ -16,14 +16,17 @@ contract BurnableToken is BasicToken {
* @param _value The amount of token to be burned.
*/
function burn(uint256 _value) public {
require(_value <= balances[msg.sender]);
_burn(msg.sender, _value);
}
function _burn(address _who, uint256 _value) internal {
require(_value <= balances[_who]);
// no need to require value <= totalSupply, since that would imply the
// sender's balance is greater than the totalSupply, which *should* be an assertion failure
address burner = msg.sender;
balances[burner] = balances[burner].sub(_value);
balances[_who] = balances[_who].sub(_value);
totalSupply_ = totalSupply_.sub(_value);
emit Burn(burner, _value);
emit Transfer(burner, address(0), _value);
emit Burn(_who, _value);
emit Transfer(_who, address(0), _value);
}
}

View File

@ -0,0 +1,24 @@
pragma solidity ^0.4.18;
import "./BurnableToken.sol";
import "./StandardToken.sol";
/**
* @title Standard Burnable Token
* @dev Adds burnFrom method to ERC20 implementations
*/
contract StandardBurnableToken is BurnableToken, StandardToken {
/**
* @dev Burns a specific amount of tokens from the target address and decrements allowance
* @param _from address The address which you want to send tokens from
* @param _value uint256 The amount of token to be burned
*/
function burnFrom(address _from, uint256 _value) public {
require(_value <= allowed[_from][msg.sender]);
// Should https://github.com/OpenZeppelin/zeppelin-solidity/issues/707 be accepted,
// this function needs to emit an event with the updated approval.
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
_burn(_from, _value);
}
}