pragma solidity ^0.4.24;
//标准ERC20Token接口
contract ERC20Token{
uint256 public totalSupply;
function balanceOf(address _owner) public constant returns (uint256 balance);
function transfer(address _to, uint256 _value) public returns (bool success);
function transferFrom(address _from, address _to, uint256 _value) public returns(bool success);
function approve(address _spender, uint256 _value) public returns (bool success);
function allowance(address _owner, address _spender) public constant returns(uint256 remaining);
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
//东大门EastToken
contract EastToken is ERC20Token {
string public name; //名称
string public symbol; //token简称
uint8 public decimals; //返回token使用的小数点后几位
mapping (address => uint256) public balances;
mapping (address => mapping (address => uint256)) public allowed;
//构造函数
constructor(uint256 _initialAmount, string _tokenName, uint8 _decimals, string _tokenSymbol) public {
totalSupply = _initialAmount * 10 ** uint256(_decimals); // 设置初始总量
balances[msg.sender] = totalSupply; //初始token数量给予消息发送者,即合约的创建者
name = _tokenName;
decimals = _decimals;
symbol = _tokenSymbol;
}
//调用者转账给to地址
function transfer(address _to, uint256 _value) public returns (bool success) {
//默认totalSupply不会超过最大值 (2^256 - 1),避免溢出的异常
require(balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]);
require(_to != 0x0); //不能转空地址
balances[msg.sender] -= _value; //从消息发送者账户中减去token数量_value
balances[_to] += _value; //往接收账户增加token数量_value
emit Transfer(msg.sender, _to, _value); //触发转币交易事件
return true;
}
//第三方调用转账
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value);
balances[_to] += _value; //接收账户增加token数量_value
balances[_from] -= _value; //支出账户_from减去token数量_value
allowed[_from][msg.sender] -= _value; //消息发送者可以从账户_from中转出的数量减少_value
emit Transfer(_from, _to, _value); //触发转币交易事件
return true;
}
//查询_owner的余额
function balanceOf(address _owner) public constant returns (uint256 balance) {
return balances[_owner];
}
//授权_spender在spender中花费_value
function approve(address _spender, uint256 _value) public returns (bool success){
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
//允许_spender从_owner中转出的token数
function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}
//批转10000
}