Skip to content

Instantly share code, notes, and snippets.

@retotrinkler
Last active September 5, 2017 09:31
Show Gist options
  • Save retotrinkler/85bdf245fed0fb502a964e76aa203a41 to your computer and use it in GitHub Desktop.
Save retotrinkler/85bdf245fed0fb502a964e76aa203a41 to your computer and use it in GitHub Desktop.
EtherToken.sol
pragma solidity ^0.4.11;
/// @title ERC20 Token Protocol
/// @author Melonport AG <[email protected]>
/// @notice See https://github.com/ethereum/EIPs/issues/20
contract ERC20Protocol {
// CONSTANT METHODS
function totalSupply() constant returns (uint256 totalSupply) {}
function balanceOf(address _owner) constant returns (uint256 balance) {}
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {}
// NON-CONSTANT METHODS
function transfer(address _to, uint256 _value) returns (bool success) {}
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {}
function approve(address _spender, uint256 _value) returns (bool success) {}
// EVENTS
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
/// @title ERC20 Token
/// @author Melonport AG <[email protected]>
/// @notice Original taken from https://github.com/ethereum/EIPs/issues/20
/// @notice Checked against integer overflow
contract ERC20 is ERC20Protocol {
function transfer(address _to, uint256 _value) returns (bool success) {
if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
balances[msg.sender] -= _value;
balances[_to] += _value;
Transfer(msg.sender, _to, _value);
return true;
} else { return false; }
}
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {
if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]) {
balances[_to] += _value;
balances[_from] -= _value;
allowed[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
return true;
} else { return false; }
}
function balanceOf(address _owner) constant returns (uint256 balance) {
return balances[_owner];
}
function approve(address _spender, uint256 _value) returns (bool success) {
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}
mapping (address => uint256) balances;
mapping (address => mapping (address => uint256)) allowed;
uint256 public totalSupply;
}
/// @title Asset Contract.
/// @author Melonport AG <[email protected]>
contract Asset is ERC20 {
// FIELDS
// Constant token specific fields
string public name;
string public symbol;
uint public decimals;
// CONSTANT METHODS
function getName() constant returns (string) { return name; }
function getSymbol() constant returns (string) { return symbol; }
function getDecimals() constant returns (uint) { return decimals; }
// NON-CONSTANT METHODS
function Asset(string _name, string _symbol, uint _decimals) {
name = _name; // Set the name for display purposes
symbol = _symbol; // Set the symbol for display purposes
decimals = _decimals; // Defined in price feed protocol
}
}
/// @title Math operations with safety checks
/// @author Melonport AG <[email protected]>
/// @notice From https://github.com/status-im/status-network-token/blob/master/contracts/SafeMath.sol
library SafeMath {
function mul(uint a, uint b) internal returns (uint) {
uint c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div(uint a, uint b) internal returns (uint) {
uint c = a / b;
return c;
}
function sub(uint a, uint b) internal returns (uint) {
assert(b <= a);
return a - b;
}
function add(uint a, uint b) internal returns (uint) {
uint c = a + b;
assert(c >= a);
return c;
}
function max64(uint64 a, uint64 b) internal constant returns (uint64) {
return a >= b ? a : b;
}
function min64(uint64 a, uint64 b) internal constant returns (uint64) {
return a < b ? a : b;
}
function max256(uint256 a, uint256 b) internal constant returns (uint256) {
return a >= b ? a : b;
}
function min256(uint256 a, uint256 b) internal constant returns (uint256) {
return a < b ? a : b;
}
}
/// @title PreminedAsset Contract.
/// @author Melonport AG <[email protected]>
/// @notice Premined amount used to make markets
contract PreminedAsset is Asset {
using SafeMath for uint256;
// METHODS
function PreminedAsset(string _name, string _symbol, uint8 _decimals, uint _amount)
Asset(_name, _symbol, _decimals)
{
balances[msg.sender] = balances[msg.sender].add(_amount);
totalSupply = totalSupply.add(_amount);
}
}
/// @title EtherToken Contract.
/// @author Melonport AG <[email protected]>
/// @notice Make Ether into a ERC20 compliant token
/// @notice Compliant to https://github.com/nexusdev/dappsys/blob/04451acf23f017beecb1a4cad4702deadc929811/contracts/token/base.sol
contract EtherToken is PreminedAsset {
using SafeMath for uint256;
// FIELDS
// Constant token specific fields
string public constant name = "Ether Token";
string public constant symbol = "ETH-T";
uint8 public constant decimals = 18;
uint public constant preminedAmount = 10**28;
// EVENTS
event Deposit(address indexed who, uint amount);
event Withdrawal(address indexed who, uint amount);
// METHODS
modifier balances_msg_sender_at_least(uint x) {
assert(balances[msg.sender] >= x);
_;
}
// NON-CONSTANT METHODS
function EtherToken()
PreminedAsset(name, symbol, decimals, preminedAmount)
{}
/// Post: Exchanged Ether against Token
function() payable { deposit(); }
/// Post: Exchanged Ether against Token
function deposit()
payable
returns (bool)
{
balances[msg.sender] = balances[msg.sender].add(msg.value);
Deposit(msg.sender, msg.value);
return true;
}
/// Post: Exchanged Token against Ether
function withdraw(uint amount)
balances_msg_sender_at_least(amount)
returns (bool)
{
balances[msg.sender] = balances[msg.sender].add(amount);
assert(msg.sender.send(amount));
Withdrawal(msg.sender, amount);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment