Skip to content

Instantly share code, notes, and snippets.

@andredublin
Last active May 10, 2018 00:28
Show Gist options
  • Save andredublin/0907588a1e1a8bf2fba718eb235e730a to your computer and use it in GitHub Desktop.
Save andredublin/0907588a1e1a8bf2fba718eb235e730a to your computer and use it in GitHub Desktop.
solidity agent pattern
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);
}
}
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