ERC20的标准接口是这样的:

contract ERC20 { 
    function name() constant returns (string name) 
    function symbol() constant returns (string symbol) 
    function decimals() constant returns (uint8 decimals) 
    function totalSupply() constant returns (uint totalSupply); 
    function balanceOf(address _owner) constant returns (uint balance); 
    function transfer(address _to, uint _value) returns (bool success); 
    function transferFrom(address _from, address _to, uint _value) returns (bool success); 
    function approve(address _spender, uint _value) returns (bool success); 
    function allowance(address _owner, address _spender) constant returns (uint remaining); 
    event Transfer(address indexed _from, address indexed _to, uint _value); 
    event Approval(address indexed _owner, address indexed _spender, uint _value); 
}

name

返回ERC20代币的名字,例如"My test token"。

symbol

返回代币的简称,例如:MTT,这个也是我们一般在代币交易所看到的名字。

decimals

返回token使用的小数点后几位。比如如果设置为3,就是支持0.001表示。

totalSupply

返回token的总供应量

balanceOf

返回某个地址(账户)的账户余额

transfer

从代币合约的调用者地址上转移 _value的数量token到的地址 _to,并且必须触发Transfer事件。

transferFrom

从地址 _from发送数量为 _value的token到地址 _to,必须触发Transfer事件。

transferFrom方法用于允许合同代理某人转移token。条件是from账户必须经过了approve。这个后面会举例说明。

approve

允许 _spender多次取回您的帐户,最高达 _value金额。 如果再次调用此函数,它将以 _value覆盖当前的余量。

allowance

返回 _spender仍然被允许从 _owner提取的金额。

后面三个方法不好理解,这里还需要补充说明一下,

approve是授权第三方(比如某个服务合约)从发送者账户转移代币,然后通过 transferFrom() 函数来执行具体的转移操作。

账户A有1000个ETC,想允许B账户随意调用他的100个ETC,过程如下:

基于ERC20编写的一个代币合约

pragma solidity ^0.4.16;
contract Token{
  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);
}

contract TokenDemo is Token {

  string public name; //名称,例如"My test token"
  uint8 public decimals; //返回token使用的小数点后几位。比如如果设置为3,就是支持0.001表示.
  string public symbol; //token简称,like MTT

  function TokenDemo(uint256 _initialAmount, string _tokenName, uint8 _decimalUnits, string _tokenSymbol) public {
    totalSupply = _initialAmount * 10 ** uint256(_decimalUnits); // 设置初始总量
    balances[msg.sender] = totalSupply; // 初始token数量给予消息发送者,因为是构造函数,所以这里也是合约的创建者

    name = _tokenName; 
    decimals = _decimalUnits; 
    symbol = _tokenSymbol;
  }

  function transfer(address _to, uint256 _value) public returns (bool success) {
    //默认totalSupply 不会超过最大值 (2^256 - 1).
    //如果随着时间的推移将会有新的token生成,则可以用下面这句避免溢出的异常
    require(balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]);
    require(_to != 0x0);
    balances[msg.sender] -= _value;//从消息发送者账户中减去token数量_value
    balances[_to] += _value;//往接收账户增加token数量_value
    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
    Transfer(_from, _to, _value);//触发转币交易事件
    return true;
  }
  function balanceOf(address _owner) public constant returns (uint256 balance) {
    return balances[_owner];
  }


  function approve(address _spender, uint256 _value) public returns (bool success) { 
    allowed[msg.sender][_spender] = _value;
    Approval(msg.sender, _spender, _value);
    return true;
  }

  function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {
    return allowed[_owner][_spender];//允许_spender从_owner中转出的token数
  }
  mapping (address => uint256) balances;
  mapping (address => mapping (address => uint256)) allowed;
}

这里可能有人会有疑问,name,totalSupply这些按照标准不应该都是方法吗,怎么这里定义的是属性变量? 这是因为solidity会自动给public变量生成同名的getter接口。

部署测试

以下是环境的部署测试流程:

一: 用solcjs或solc编译合约,如图

二: 部署执行以下代码,token_abi的值为编译完成后的abi文件内容

token_code值为编译完成后二进制内容,即.bin文件内容;from为部署合约的地址,确保该账户上余额,如碰到图一错误,需要解锁账号,具体操作参考图二

token_abi = [
  {"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},
  {"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},
  {"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},
  {"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},
  {"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},
  {"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},
  {"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},
  {"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},
  {"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},
  {"inputs":[{"name":"_initialAmount","type":"uint256"},{"name":"_tokenName","type":"string"},{"name":"_decimalUnits","type":"uint8"},{"name":"_tokenSymbol","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},
  {"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},
  {"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},
  {"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},
  {"indexed":true,"name":"_spender","type":"address"},
  {"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}
] 

test_token = web3.eth.contract(token_abi) 
token_code = '0x608060405234801561001057600080fd5b50604051610735380380610735833981016040908152815160208084015183850151606086015160ff8216600a0a85026000818155600160a060020a0333168152600486529690962095909555908501805193959094919391019161007a916001918601906100a7565b506002805460ff191660ff8416179055805161009d9060039060208401906100a7565b5050505050610142565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100e857805160ff1916838001178555610115565b82800160010185558215610115579182015b828111156101155782518255916020019190600101906100fa565b50610121929150610125565b5090565b61013f91905b80821115610121576000815560010161012b565b90565b6105e4806101516000396000f3006080604052600436106100985763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166306fdde03811461009d578063095ea7b31461012757806318160ddd1461015f57806323b872dd14610186578063313ce567146101b057806370a08231146101db57806395d89b41146101fc578063a9059cbb14610211578063dd62ed3e14610235575b600080fd5b3480156100a957600080fd5b506100b261025c565b6040805160208082528351818301528351919283929083019185019080838360005b838110156100ec5781810151838201526020016100d4565b50505050905090810190601f1680156101195780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561013357600080fd5b5061014b600160a060020a03600435166024356102e9565b604080519115158252519081900360200190f35b34801561016b57600080fd5b50610174610353565b60408051918252519081900360200190f35b34801561019257600080fd5b5061014b600160a060020a0360043581169060243516604435610359565b3480156101bc57600080fd5b506101c561043d565b6040805160ff9092168252519081900360200190f35b3480156101e757600080fd5b50610174600160a060020a0360043516610446565b34801561020857600080fd5b506100b2610461565b34801561021d57600080fd5b5061014b600160a060020a03600435166024356104bc565b34801561024157600080fd5b50610174600160a060020a036004358116906024351661058d565b60018054604080516020600284861615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156102e15780601f106102b6576101008083540402835291602001916102e1565b820191906000526020600020905b8154815290600101906020018083116102c457829003601f168201915b505050505081565b600160a060020a03338116600081815260056020908152604080832094871680845294825280832086905580518681529051929493927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925929181900390910190a350600192915050565b60005481565b600160a060020a03831660009081526004602052604081205482118015906103a75750600160a060020a03808516600090815260056020908152604080832033909416835292905220548211155b15156103b257600080fd5b600160a060020a03808416600081815260046020908152604080832080548801905588851680845281842080548990039055600583528184203390961684529482529182902080548790039055815186815291519293927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35060019392505050565b60025460ff1681565b600160a060020a031660009081526004602052604090205490565b6003805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156102e15780601f106102b6576101008083540402835291602001916102e1565b600160a060020a03331660009081526004602052604081205482118015906104fd5750600160a060020a038316600090815260046020526040902054828101115b151561050857600080fd5b600160a060020a038316151561051d57600080fd5b600160a060020a03338116600081815260046020908152604080832080548890039055938716808352918490208054870190558351868152935191937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929081900390910190a350600192915050565b600160a060020a039182166000908152600560209081526040808320939094168252919091522054905600a165627a7a7230582016464ed0c4d48736761517a8ae4c3f126efa298a7d5cfc6d819a7513ec3383ab0029' 

instance = test_token.new({from: '0x547f0a45c12f9428e5aa060fb259c20616cd74a0', data: token_code, gas: 3000000}, function (e, contract){     
  console.log(e, contract);     
  if (typeof contract.address !== 'undefined') {          
    console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);     
  }  
})

图一

图二

三:部署成功后会看到如下提示