主页 > 苹果手机怎么下载imtoken > 使用 Remix 和 MetaMask 发行以太坊 ERC20 Token

使用 Remix 和 MetaMask 发行以太坊 ERC20 Token

苹果手机怎么下载imtoken 2023-01-17 13:21:47

ERC20是在以太坊网络上发行代币(Token)的标准协议接口。 协议的具体描述位于github上。 标准协议使令牌能够用于不同的应用程序,例如钱包和去中心化交易所。 目前有很多实现标准协议的Token Example,我们将使用提供的例子来演示操作。 我们先看看这个实现库中的主要合约文件:

ERC20 Token协议实现了Token.sol ERC 20协议的抽象定义

//Abstract contract for the full ERC 20 Token standard
// https://github.com/ethereum/EIPs/issues/20
pragma solidity ^0.4.8;
contract Token {
    /// token总量,默认会为public变量生成一个getter函数接口,名称为totalSupply().
    uint256 public totalSupply;
    /// 获取账户_owner拥有token的数量
    function balanceOf(address _owner) constant returns (uint256 balance);
    //从消息发送者账户中往_to账户转数量为_value的token
    function transfer(address _to, uint256 _value) returns (bool success);
    //从账户_from中往账户_to转数量为_value的token,与approve方法配合使用
    function transferFrom(address _from, address _to, uint256 _value) returns  (bool success);
    //消息发送账户设置账户_spender能从发送账户中转出数量为_value的token
    function approve(address _spender, uint256 _value) returns (bool success);
    //获取账户_spender可以从账户_owner中转出token的数量
    function allowance(address _owner, address _spender) constant returns  (uint256 remaining);
    //发生转账时必须要触发的事件 
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    //当函数approve(address _spender, uint256 _value)成功执行时必须触发的事件
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}

StandardToken.sol ERC20 协议的标准实现

You should inherit from StandardToken or, for a token like you would want to
deploy in something like Mist, see HumanStandardToken.sol.
(This implements ONLY the standard functions and NOTHING else.
If you deploy this, you won't have anything useful.)
Implements ERC 20 Token standard: https://github.com/ethereum/EIPs/issues/20.*/
pragma solidity ^0.4.8;
import "./Token.sol";
contract StandardToken is Token {
    function transfer(address _to, uint256 _value) returns (bool success) {
        //默认totalSupply 不会超过最大值 (2^256 - 1).
        //如果随着时间的推移将会有新的token生成,则可以用下面这句避免溢出的异常
        //require(balances[msg.sender] >= _value && balances[_to] + _value >balances[_to]);
        require(balances[msg.sender] >= _value);
        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) returns (bool success) {
        //require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]);
        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) constant returns (uint256 balance) {
        return balances[_owner];
    }
    //授权账户_spender可以从消息发送者账户转出数量为_value的token
    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];//允许_spender从_owner中转出的token数
    }
    mapping (address => uint256) balances;
    mapping (address => mapping (address => uint256)) allowed;
}

HumanStandardToken.sol 特定令牌实现

This Token Contract implements the standard token functionality (https://github.com/ethereum/EIPs/issues/20) as well as the following OPTIONAL extras intended for use by humans.
In other words. This is intended for deployment in something like a Token Factory or Mist wallet, and then used by humans.
Imagine coins, currencies, shares, voting weight, etc.
Machine-based, rapid creation of many tokens would not necessarily need these extra features or will be minted in other manners.
1) Initial Finite Supply (upon creation one specifies how much is minted).
2) In the absence of a token registry: Optional Decimal, Symbol & Name.
3) Optional approveAndCall() functionality to notify a contract if an approval() has occurred.
.*/
import "./StandardToken.sol";
pragma solidity ^0.4.8;
contract HumanStandardToken is StandardToken {
    /* Public variables of the token */
    string public name;                   //名称: eg Simon Bucks
    uint8 public decimals;                //最多的小数位数How many decimals to show. ie. There could 1000 base units with 3 decimals. Meaning 0.980 SBX = 980 base units. It's like comparing 1 wei to 1 ether.
    string public symbol;                 //token简称: eg SBX
    string public version = 'H0.1';       //版本
    function HumanStandardToken(uint256 _initialAmount, string _tokenName, uint8 _decimalUnits, string _tokenSymbol) {
        balances[msg.sender] = _initialAmount; // 初始token数量给予消息发送者
        totalSupply = _initialAmount;         // 设置初始总量
        name = _tokenName;                   // token名称
        decimals = _decimalUnits;           // 小数位数
        symbol = _tokenSymbol;             // token简称
    }
    /* 同意转出并调用接收合约(根据自己需求实现) */
    function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) {
        allowed[msg.sender][_spender] = _value;
        Approval(msg.sender, _spender, _value);
        //call the receiveApproval function on the contract you want to be notified. This crafts the function signature manually so one doesn't have to include a contract in here just for this.
        //receiveApproval(address _from, uint256 _value, address _tokenContract, bytes _extraData)
        //it is assumed that when does this that the call *should* succeed, otherwise one would use vanilla approve instead.
        
        require(_spender.call(bytes4(bytes32(sha3("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData));
        return true;
    }
}

代币合约发布

我们将使用 MetaMask(chrome 插件安装链接)和 Remix() 并选择在 Ropsten 测试网络上发布 Token 合约(获得免费的 ETH:)。

将 MetaMask 切换到 Ropsten 网络

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

2.将所有相关代码复制到Remix

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

在右上角红框标记的Environment中选择Injected Web3,下面的Account list会列出MetaMask中的账户信息。 然后在右下红框中填写初始化HumanStandardToken需要的参数,初始代币数量(100000000),代币名称描述(My Test Token),十进制数(8),代币简称(MTT) ).

代币合约发布

点击HumanStandardToken合约处的create按钮,会弹出如下界面:

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

点击SUBMIT将交易请求发布到Rosten网络,交易将成功打包。 可以发现本次操作的交易id为0x9fb4144f5a28ac46d43c689994de3cfa29cdb538582bde1ea1d646efcb7dcfa,合约地址为0x06750c263b1a344b9b01b32888e93bc3c28a950d。 交易截图如下:

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

值得一提的是usdt转账查询网站,这笔交易在1700000区块后正好赶上了Ropsten的BYZANTIUM(拜占庭)分叉。遗憾的是,这笔交易最终是在1700017区块打包的,而不是在1700001区块。

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

4.合约源码上传

接下来我们看看如何上传合约源码。 首先打开合约0x06750c263b1a344b9b01b32888e93bc3c28a950d的详情页,切换到合约代码页签,如下图所示。

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

点击Contract code选项卡中的“Verify And Publish”链接,填写下图红框中的内容。

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

填写完以上数据后,点击验证并发布。 如果验证通过,会出现如下页面:

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

最后,我们切换到合约的详情页,点击合约源标签,可以看到上传的合约源码,如下图:

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

如果您遇到“此时无法验证合约”的错误,请确认您在提交合约源码时,选择的编译器版本和优化选项与您在Remix中的选择一致。

添加释放的Token到钱包

我们可以将上面生成的Token添加到MetaMask中的MetaMask钱包中,如下图。

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

点击添加后usdt转账查询网站,在Tokens选项卡中可以看到上面发布的Token MTT。 (因为我们在创建HumanStandardToken的时候,初始输入的数量是100000000,十进制是8,这个初始数量的单位是token的最低单位,所以总量是1MTT,大家可以根据看你自己的情况。)

国外网站电子转账_网站嵌入支付宝转账页面_usdt转账查询网站

图片.png

接下来,您可以在 Ropsten 测试网络上进行 Token 传输和相关测试操作。 如果测试没有问题,按照同样的流程将合约部署到以太坊主网发行真实代币。

祝你好运! ! !