1. 程式人生 > >以太坊系列(一)搭建環境,部署第一合約

以太坊系列(一)搭建環境,部署第一合約

  1. 安裝geth

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum
注: 筆者這裡用的是ubuntu系統,若您使用的是其他系統,請自行百度

  1. 檢查安裝
    geth --help

基本操作:

  1. 啟動geth客戶端: geth --datadir account_stored_dir --dev console 2>> test.log
dev 啟用開發者網路(模式),開發者網路會使用POA共識,預設預分配一個開發者賬戶並且會自動開啟挖礦。
datadir 後面的引數是區塊資料及祕鑰存放目錄。
第一次輸入命令後,它會放在當前目錄下新建一個testNet目錄來存放資料。
console 進入控制檯
2>> test.log 表示把控制檯日誌輸出到test.log檔案
  1. 檢視賬號 eth.accounts || personal.listAccounts
  2. 檢視餘額:eth.getBalance(eth.accounts[0])
  3. 建立賬號:personal.newAccount(“JadenGu”)
  4. 給新賬戶轉賬:
    eth.sendTransaction({from: '0x91923aba2536f5ca24909904a1561ccd83a510e0', to: '0x28d0642a5a705ee698cec95cdf7ab27a52b955cc', value: web3.toWei(1, "ether")}
  1. 解鎖賬戶:personal.unlockAccount(eth.accounts[1], “JadenGu”)

接下來就到了擼碼時刻了:

pragma solidity ^0.4.18;

contract hello {
    string greeting;
    
    function hello(string _greeting) public {
        greeting = _greeting;
    }
    
    function say() constant public returns(string) {
        return greeting;
    }
}

程式碼寫好了,怎麼部署呢?別急,我們需要先將solidity編譯成WEB3DEPLOY 的程式碼,How to do?

將我們寫好的合約程式碼copy 到 https://remix.ethereum.org,compile之後開啟details 然後 copy WEB3DEPLOY 程式碼 如下:

var _greeting = ""; //此處需手動修改
var helloContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"say","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var hello = helloContract.new(
   _greeting,
   {
     from: web3.eth.accounts[1], 
     data: '0x608060405234801561001057600080fd5b506040516102a83803806102a8833981018060405281019080805182019291905050508060009080519060200190610049929190610050565b50506100f5565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009157805160ff19168380011785556100bf565b828001600101855582156100bf579182015b828111156100be5782518255916020019190600101906100a3565b5b5090506100cc91906100d0565b5090565b6100f291905b808211156100ee5760008160009055506001016100d6565b5090565b90565b6101a4806101046000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063954ab4b214610046575b600080fd5b34801561005257600080fd5b5061005b6100d6565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009b578082015181840152602081019050610080565b50505050905090810190601f1680156100c85780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561016e5780601f106101435761010080835404028352916020019161016e565b820191906000526020600020905b81548152906001019060200180831161015157829003601f168201915b50505050509050905600a165627a7a72305820a40f5449d774a10d5bff7fd8c7325798fd380c476f9ab4daf2e2f671256586260029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

將以上程式碼段copy到geth客戶端,回車後,看到下邊這句話:
Contract mined! address 。。。 就表示部署成功了。
執行:hello.say();
So happy_