1. 程式人生 > >NEW 建立自己的數字貨幣(ERC20代幣)進行ICO (新增修改SOL程式碼)

NEW 建立自己的數字貨幣(ERC20代幣)進行ICO (新增修改SOL程式碼)

本文從技術角度詳細介紹如何基於以太坊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,這時你就可以看到你建立的代幣了,如圖:

哈哈,你已經完成了代幣的建立和部署(正式網路和測試網路部署方法一樣),可以在Etherscan查詢到我們剛剛部署的代幣。可以用它進行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;
    }
}