从环境搭建到正式上线,ERC20开发极简指南



  • 文章作者:Bertrand
    文章来源:SCC社区

    环境情况

    无操作系统需求

    使用在线Remix IDE

    打开Remix IDE Online

    开发源码

    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 TokenTemplate is Token {
    
        string public name;
        uint8 public decimals;
        string public symbol;
    
        function TokenTemplate(uint256 _initialAmount, string _tokenName, uint8 _decimalUnits, string _tokenSymbol) public {
            totalSupply = _initialAmount * 10 ** uint256(_decimalUnits);
            balances[msg.sender] = totalSupply;
    
            name = _tokenName;
            decimals = _decimalUnits;
            symbol = _tokenSymbol;
        }
    
        function transfer(address _to, uint256 _value) public returns (bool success) {
            require(balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]);
            require(_to != 0x0);
            balances[msg.sender] -= _value;
            balances[_to] += _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;
            balances[_from] -= _value;
            allowed[_from][msg.sender] -= _value;
            emit 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;
            emit Approval(msg.sender, _spender, _value);
            return true;
        }
    
        function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {
            return allowed[_owner][_spender];
        }
        mapping (address => uint256) balances;
        mapping (address => mapping (address => uint256)) allowed;
    }
    

    合约参数

    需要输入

    _initialAmount: 	1000000000
    _tokenName:			PonyCoin
    _decimalUnits:		8
    _tokenSymbol:		PCN
    

    下载MetaMask插件

    可以到chrome extentions里面下载,完成后会有一系列配置信息,第一次使用会帮你生成帐号(密码+助记词),比如我这边生成的

    film easily matter venue neutral twin memory area attack shadow civil judge
    

    然后选择Rinkeby Test Net

    获取测试ETH

    在获取eth之前,需要使用钱包创建用户
    Rinkeby

    部署合约到Rinkeby

    在remix右侧的Run菜单中Environment中选择Injected Web3,选择合约TokenTemplatecreate中填写以下参数

    100000,"Pony Coin", 6, "PCN"
    

    然后点击create,会在console的窗口中提示

    creation of TokenTemplate pending...
    

    到MetaMask界面中选择submit即可,成功则会显示合约的链接

    https://rinkeby.etherscan.io/tx/0x8103ad91fd0217d3906b1226b276d646b8e48ac45e0bae5cc4cd8ee63152c64b
    

    验证并发布合约

    可以通过这个地址找到合约的地址交易地址

    找到合约地址

    0xBc3b5cd02e83B28F64BE4171E950243Bf2fEEC3c
    

    然后选择Contract Code,然后选择Verify And Publish,需要填写以下的信息

    Contract Name: TokenTemplate //你部署合约类的名字
    Compiler: 0.4.21+commit.dfe3193c //可以在remix->settings中找到
    Optimization: No //实际上我们编译中没有加入优化参数
    Enter the Solidity Contract Code below: 你合约的代码
    

    然后点击Verify And Publish,成功后点击Start Over及可以成功发布可被检索的合约

    ERC20代币转帐

    MetaMask并不支持erc20代币转帐,可以打开wallet online,在合约处选择查看代币,需要填写以下信息

    合约地址: 0xBc3b5cd02e83B28F64BE4171E950243Bf2fEEC3c
    

    添加以后如果你的帐户拥有这个代币就可以在发送的时候选择代币,另外发送完后记得还得回MetaMask把请求提交一下



  • @michael 不是的,是因为你的compiler在线加载失败,解决方案是进行翻墙,然后点击setting,选择Solidity version,等待loading结束即可解决



  • @michael 这个是获取solc编译器列表出错导致的原因,重新加载一下,也可以打开控制台看一下是否加载成功。



  • remix版本升级了,有报错(mock compiler: source not found),是不是应该用新地址,http://remix.ethereum.org/#optimize=false&version=soljson-v0.4.19+commit.c4cbbb05.js


Log in to reply
 

Popular Topics

|

Looks like your connection to SCC was lost, please wait while we try to reconnect.