Created
October 23, 2017 13:25
-
-
Save nateawelch/2956f0d3b5e662a44b83b8e4bec6cca6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | |
require((_value == 0) || (allowed[msg.sender][_spender].amount == 0 && !allowed[msg.sender][_spender].used)); | |
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