1. 程式人生 > >以太坊上發行ERC20代幣

以太坊上發行ERC20代幣

auto 默認值 在那 entity 密碼 byte 錯誤提示 快捷訪問 meta

ERC20 代幣生成

環境

虛擬主機: ubuntu 18虛擬機

宿主主機: win10; ip:192.168.0.160

1.部署以太坊

1.1 安裝GO

安裝go,並編譯geth

將下載好的golang包上傳到root目錄。

sudo apt-get install -y build-essential golang

1.2 下載源代碼編譯

git clone https://github.com/ethereum/go-ethereum
cd go-ethereum
make geth
# 添加快捷訪問
cp build/bin/geth /usr/bin/geth

1.3 部署節點

1.3.1 創建節點目錄

創建數據存儲目錄,分別為8001-8002以此類推

mkdir -p /node/node8001
mkdir -p /node/node8002

1.3.2 創建賬號

geth  account  new - -datadir  "/node/node8001"  

執行後會要求設置賬戶的unlock口令,請記住配置的口令. 會生成一個密碼保存文件夾(keystore), 內含一個密碼保存文件,名字如下

UTC--2018-12-13T02-58-07.160085261Z--f8c5cad127f7053e143204450c18b6bc1f353c9b

1.3.3 生成創世文件

生成文件名為genesis.json的文件, 內容如下:

{
"config": {
    "chainId": 115,
    "homesteadBlock": 0,
    "byzantiumBlock": 12,
    "eip155Block": 0,
    "eip158Block": 0
},
"nonce":"0x0000000000000042",
"mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty":"0x0001",
"alloc":{"f8c5cad127f7053e143204450c18b6bc1f353c9b":{"balance":"990000000000000000000000000000"}},
"timestamp":"0x5b3bcd2f",
"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData":"",
"gasLimit":"0xffffffff"
}

1.3.4 根據創世文件信息,創建節點

/usr/bin/gethfw --datadir /node/node8001 init ./genesis.json
/usr/bin/gethfw --datadir /node/node8002 init ./genesis.json

1.3.5 啟動節點

單獨啟一個終端來啟動節點

/usr/bin/geth --targetgaslimit 4294967295 --identity "node8001" --datadir /node/node8001  --port 8801 --rpc --rpcaddr "0.0.0.0" --rpcport 8145 --rpccorsdomain "http://192.168.0.160:8081" --rpcapi "admin,debug,eth,net,personal,shh,txpool,web3"  --networkid 115  --nodiscover --mine

單獨啟一個終端來啟動節點

/usr/bin/geth --targetgaslimit 4294967295 --identity "node8002" --datadir /node/node8002  --port 8802 --rpc --rpcaddr "0.0.0.0" --rpcport 8546 --rpccorsdomain "http://192.168.0.160:8081" --rpcapi "admin,debug,eth,net,personal,shh,txpool,web3"  --networkid 115 --nodiscover

此時,節點已經啟動OK, 我們可以來查看一下節點狀態了:

先前的兩個終端就讓他們掛在那裏執行程序,另外開啟一個終端,執行如下命令:

/usr/bin/gethfw attach ipc:/node/node8001/geth.ipc

然後會自動進入控制臺了, 輸入admin命令,可以查看當前鏈的信息,輸出類似如下:

{
datadir: "/node/node8001",
nodeInfo: {
? enode: "enode://9e7c5f604f49638524b3d95078bb4644e4e379f6fa532094ccd1bbd9c975e81a1e66fd602530c274240ee0f563f917fc4de0665e3e5abeaf021f3efaa0565eeb@[::]:8801?discport=0",
? ip: "::",
? listenAddr: "[::]:8801",
? name: "Geth/node8001/v1.8.3-stable/linux-amd64/go1.10.4",
? ports: {
? discovery: 0,
? listener: 8801
? },
? protocols: {
? eth: {
? config: {...},
? difficulty: 78940001,
? genesis: "0xa6ebc49109d376f9afc21fb6ab9d4b62da2442c6d81e6ab90633a03e851693d6",
? head: "0xafe6c156e1920039334e4a470aa80c7d8ad9202fdf62a38ed2ec27e076685949",
? network: 115

......

至此, 以太坊私鏈算是啟動完成了。

2.部署Remix

參考https://github.com/ethereum/remix-ide 文檔, 開始部署。

2.1 下載Remix-IDE源碼

git clone https://github.com/ethereum/remix-ide.git

2.2 安裝Remix-IDE

cd remix-ide

2.3 啟動Remix-IDE

npm start

此時,就可以在localhost:8080訪問RemixIDe了。

3.部署ERC20代幣

通過在瀏覽器打開localhost:8080地址, 進入Remix-IDE.

3.1 在新建智能合約

加入如下代碼

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;
    }
}

3.2 編譯智能合約

在compile頁中, 選中Auto Compile, 進行自動編譯智能合約, (應用不會出現紅色錯誤提示)

3.3 部署合約

在Run頁中, Environment中,選中inject web3(前提是, 已經安裝好了metamash插件,並且已經導入了賬號,配置後賬號環境)。

在account項, 選擇一個有幣的賬號

最後通過點擊deploy按鈕來部署合約, 在部署前, 可填寫構造函數的參數如 20000000, "TESTINT COIN", "TC"

最終完成部署

以太坊上發行ERC20代幣