Last active
May 10, 2018 00:28
-
-
Save andredublin/0907588a1e1a8bf2fba718eb235e730a to your computer and use it in GitHub Desktop.
solidity agent pattern
This file contains 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.23; | |
import "./MyToken.sol"; | |
contract Agent { | |
MyToken token; | |
constructor(MyToken _token) public { | |
token = _token | |
} | |
function transferTokens(address _to, address _from, uint256 _value) public { | |
MyToken(token).transfer(_to, _from, _value); | |
} | |
} |
This file contains 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.23; | |
import "./Ownable.sol"; | |
contract MyToken is Ownable { | |
/* This creates an array with all balances */ | |
mapping (address => uint256) public balanceOf; | |
address public agent; | |
modifier onlyAgent { | |
require(msg.sender == agent); | |
_; | |
} | |
setAgent(address _agent) public onlyOwner { | |
agent = _agent; | |
} | |
/* Send coins */ | |
function transfer(address _to, address _from, uint256 _value) public onlyAgent { | |
require(balanceOf[_from] >= _value); // Check if the sender has enough | |
require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows | |
balanceOf[_from] -= _value; // Subtract from the sender | |
balanceOf[_to] += _value; // Add the same to the recipient | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment