NEW 创建自己的数字货币(ERC20代币)进行ICO (新增修改SOL代码)
发布日期:2021-06-30 16:00:41 浏览次数:2 分类:技术文章

本文共 10656 字,大约阅读时间需要 35 分钟。

本文从技术角度详细介绍如何基于以太坊ERC20创建代币的流程.

本身不太想写这边文章,可是朋友公司遇到最可怕60万代创建ERC20 Token的事件,特此出文!

即将新增:

. 以太坊私链,联盟链技术部署细节;

. Fabric私链,联盟链技术部署细节;

注部分大学实验室骗局破灭,警示进入区块链公司误盲目受骗,敬请关注!

写在前面

本文所讲的代币是使用以太坊智能合约创建,阅读本文前,你应该对以太坊、智能合约有所了解,如果你还不了解,建议你先看!

 

代币Token

如果不那么追求精确的定义,代币就是数字货币,比特币、以太币就是一个代币。 

利用以太坊的智能合约可以轻松编写出属于自己的代币,代币可以代表任何可以交易的东西,如:积分、财产、证书等等。 
因此不管是出于商业,还是学习很多人想创建一个自己的代币,先贴一个图看看创建的代币是什么样子。 

今天我们就来详细讲一讲怎样创建一个这样的代币。

 

ERC20 Token

也许你经常看到ERC20和代币一同出现, ERC20是以太坊定义的一个。 

要求我们在实现代币的时候必须要遵守的协议,如指定代币名称、总量、实现代币交易函数等,只有支持了协议才能被以太坊钱包支持。 
其接口如下:

contract ERC20Interface {    string public constant name = "Token Name";    string public constant symbol = "SYM";    uint8 public constant decimals = 18;  // 18 is the most common number of decimal places​    function totalSupply() public constant returns (uint);    function balanceOf(address tokenOwner) public constant returns (uint balance);    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);    function transfer(address to, uint tokens) public returns (bool success);    function approve(address spender, uint tokens) public returns (bool success);    function transferFrom(address from, address to, uint tokens) public returns (bool success);​    event Transfer(address indexed from, address indexed to, uint tokens);    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);}

简单说明一下: 

name : 代币名称 
symbol: 代币符号 
decimals: 代币小数点位数,代币的最小单位, 18表示我们可以拥有 .0000000000000000001单位个代币。 
totalSupply() : 发行代币总量。 
balanceOf(): 查看对应账号的代币余额。 
transfer(): 实现代币交易,用于给用户发送代币(从我们的账户里)。 
transferFrom(): 实现代币用户之间的交易。 
allowance(): 控制代币的交易,如可交易账号及资产。 
approve(): 允许用户可花费的代币数。

 

编写代币合约代码

代币合约代码:

pragma solidity ^0.4.16;interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }contract TokenERC20 {    string public name;    string public symbol;    uint8 public decimals = 18;  // 18 是建议的默认值    uint256 public totalSupply;    mapping (address => uint256) public balanceOf;  //     mapping (address => mapping (address => uint256)) public allowance;    event Transfer(address indexed from, address indexed to, uint256 value);    event Burn(address indexed from, uint256 value);    function TokenERC20(uint256 initialSupply, string tokenName, string tokenSymbol) public {        totalSupply = initialSupply * 10 ** uint256(decimals);        balanceOf[msg.sender] = totalSupply;        name = tokenName;        symbol = tokenSymbol;    }    function _transfer(address _from, address _to, uint _value) internal {        require(_to != 0x0);        require(balanceOf[_from] >= _value);        require(balanceOf[_to] + _value > balanceOf[_to]);        uint previousBalances = balanceOf[_from] + balanceOf[_to];        balanceOf[_from] -= _value;        balanceOf[_to] += _value;        Transfer(_from, _to, _value);        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);    }    function transfer(address _to, uint256 _value) public {        _transfer(msg.sender, _to, _value);    }    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {        require(_value <= allowance[_from][msg.sender]);     // Check allowance        allowance[_from][msg.sender] -= _value;        _transfer(_from, _to, _value);        return true;    }    function approve(address _spender, uint256 _value) public        returns (bool success) {        allowance[msg.sender][_spender] = _value;        return true;    }    function approveAndCall(address _spender, uint256 _value, bytes _extraData) public returns (bool success) {        tokenRecipient spender = tokenRecipient(_spender);        if (approve(_spender, _value)) {            spender.receiveApproval(msg.sender, _value, this, _extraData);            return true;        }    }    function burn(uint256 _value) public returns (bool success) {        require(balanceOf[msg.sender] >= _value);        balanceOf[msg.sender] -= _value;        totalSupply -= _value;        Burn(msg.sender, _value);        return true;    }    function burnFrom(address _from, uint256 _value) public returns (bool success) {        require(balanceOf[_from] >= _value);        require(_value <= allowance[_from][msg.sender]);        balanceOf[_from] -= _value;        allowance[_from][msg.sender] -= _value;        totalSupply -= _value;        Burn(_from, _value);        return true;    }}

 

部署

在开发测试智能合约时,和是两个非常好用的工具,今天就用他们来完成部署。

  1. 安装和配置MetaMask请参考,不同的上本文选择了以太坊的测试网络Ropsten,如果你没有余额请点击购买buy,进入的网站可以送一些测试以太币给你,配置好之后,界面应该如下: 

 

2. 浏览器打开Remix Solidity IDE,复制以上源码粘贴上,在右侧选项参考如图的设置: 

注意Environment和Account和MetaMask保持一致,然后选择合约TokenERC20,填入你想要的发行量,名称及代号,就可以创建合约了。 

这时MetaMask会弹出一个交易确认框,点SUBMIT。待合约部署交易确认之后,复制合约地址。

 

3. 打开Metamask界面,切换到TOKENS,点添加合约,出现如下对话框: 

填入刚刚复制的地址,点ADD,这时你就可以看到你创建的代币了,如图:

哈哈,你已经完成了代币的创建和部署(正式网络和测试网络部署方法一样),可以在查询到我们刚刚部署的代币。可以用它进行ICO了,从此走上人生巅峰(玩笑话,不鼓励大家发行无意义的代币)。

 

代币交易

由于MetaMask插件没有提供代币交易功能,同时考虑到很多人并没有以太坊钱包或是被以太坊钱包网络同步问题折磨,今天我用来讲解代币交易。 

1. 进入, 第一次进入有一些安全提示需要用户确认。 
2. 进入之后,按照下图进行设置: 

3. 连接上之后,如图 

需要添加代币,填入代币合约地址。 

4. 进行代币转账交易 

在接下来的交易确认也,点击确认即可。 

5. 交易完成后,可以看到MetaMask中代币余额减少了,如图:

代币交易是不是很简单,只要明白了交易流程,使用其他的钱包也是一样的道理。

如果在创建中遇到问题,请搜索微信:kaitiren可为大家解答问题。

参考文档

修改一个发布代码错误问题:

 

pragma solidity ^0.4.16;interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }contract TokenERC20 {    // Public variables of the token    string public name;    string public symbol;    uint8 public decimals = 18;    // 18 decimals is the strongly suggested default, avoid changing it    uint256 public totalSupply;    // This creates an array with all balances    mapping (address => uint256) public balanceOf;    mapping (address => mapping (address => uint256)) public allowance;    // This generates a public event on the blockchain that will notify clients    event Transfer(address indexed from, address indexed to, uint256 value);    // This notifies clients about the amount burnt    event Burn(address indexed from, uint256 value);    /**     * Constructor function     *     * Initializes contract with initial supply tokens to the creator of the contract     */    function TokenERC20(        uint256 initialSupply,        string tokenName,        string tokenSymbol    ) public {        totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount        balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens        name = tokenName; // Set the name for display purposes        symbol = tokenSymbol; // Set the symbol for display purposes    }    /**     * Internal transfer, only can be called by this contract     */    function _transfer(address _from, address _to, uint _value) internal {        // Prevent transfer to 0x0 address. Use burn() instead        require(_to != 0x0);        // Check if the sender has enough        require(balanceOf[_from] >= _value);        // Check for overflows        require(balanceOf[_to] + _value >= balanceOf[_to]);        // Save this for an assertion in the future        uint previousBalances = balanceOf[_from] + balanceOf[_to];        // Subtract from the sender        balanceOf[_from] -= _value;        // Add the same to the recipient        balanceOf[_to] += _value;        emit Transfer(_from, _to, _value);        // Asserts are used to use static analysis to find bugs in your code. They should never fail        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);    }    /**     * Transfer tokens     *     * Send `_value` tokens to `_to` from your account     *     * @param _to The address of the recipient     * @param _value the amount to send     */    function transfer(address _to, uint256 _value) public {        _transfer(msg.sender, _to, _value);    }    /**     * Transfer tokens from other address     *     * Send `_value` tokens to `_to` on behalf of `_from`     *     * @param _from The address of the sender     * @param _to The address of the recipient     * @param _value the amount to send     */    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {        require(_value <= allowance[_from][msg.sender]); // Check allowance        allowance[_from][msg.sender] -= _value;        _transfer(_from, _to, _value);        return true;    }    /**     * Set allowance for other address     *     * Allows `_spender` to spend no more than `_value` tokens on your behalf     *     * @param _spender The address authorized to spend     * @param _value the max amount they can spend     */    function approve(address _spender, uint256 _value) public        returns (bool success) {        allowance[msg.sender][_spender] = _value;        return true;    }    /**     * Set allowance for other address and notify     *     * Allows `_spender` to spend no more than `_value` tokens on your behalf, and then ping the contract about it     *     * @param _spender The address authorized to spend     * @param _value the max amount they can spend     * @param _extraData some extra information to send to the approved contract     */    function approveAndCall(address _spender, uint256 _value, bytes _extraData)        public        returns (bool success) {        tokenRecipient spender = tokenRecipient(_spender);        if (approve(_spender, _value)) {            spender.receiveApproval(msg.sender, _value, this, _extraData);            return true;        }    }    /**     * Destroy tokens     *     * Remove `_value` tokens from the system irreversibly     *     * @param _value the amount of money to burn     */    function burn(uint256 _value) public returns (bool success) {        require(balanceOf[msg.sender] >= _value); // Check if the sender has enough        balanceOf[msg.sender] -= _value; // Subtract from the sender        totalSupply -= _value; // Updates totalSupply        emit Burn(msg.sender, _value);        return true;    }    /**     * Destroy tokens from other account     *     * Remove `_value` tokens from the system irreversibly on behalf of `_from`.     *     * @param _from the address of the sender     * @param _value the amount of money to burn     */    function burnFrom(address _from, uint256 _value) public returns (bool success) {        require(balanceOf[_from] >= _value); // Check if the targeted balance is enough        require(_value <= allowance[_from][msg.sender]); // Check allowance        balanceOf[_from] -= _value; // Subtract from the targeted balance        allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance        totalSupply -= _value; // Update totalSupply        emit Burn(_from, _value);        return true;    }}
  •  

转载地址:https://kaitiren.blog.csdn.net/article/details/83182595 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:阿里P8架构师谈:MySQL数据库的索引原理、与慢SQL优化的5大原则
下一篇:如何逆向破解抓包工具Charles!

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月25日 13时56分18秒