Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pragma solidity ^0.4.11;
contract BackwardsCompatibleApprove {
struct Allowance {
uint amount;
bool used;
}
mapping(address => mapping(address => Allowance)) allowed;
mapping(address => uint256) balances;
function approve(address _spender, uint _value) public returns (bool success) {
if(allowed[msg.sender][_spender].used){
allowed[msg.sender][_spender].amount = 0;
allowed[msg.sender][_spender].used=false;
Approval(msg.sender,_spender,0);
return false;
} else {
allowed[msg.sender][_spender].amount=_value;
Approval(msg.sender,_spender,_value);
return true;
}
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
require(_value <= balances[_from]);
require(_value <= allowed[_from][msg.sender].amount);
balances[_from] -= _value;
balances[_to] += _value;
allowed[_from][msg.sender].amount -= _value;
if(_value > 0){
allowed[_from][msg.sender].used = true;
}
Transfer(_from, _to, _value);
return true;
}
event Transfer(address indexed _from, address indexed _to, uint _value);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.