1. 程式人生 > >20181117--深入淺出區塊鏈 智慧合約開發環境搭建及Hello World合約

20181117--深入淺出區塊鏈 智慧合約開發環境搭建及Hello World合約

安裝都完成了
geth --datadir “E:\blockchain\geth”testNet --dev console
啟動textNet

eth.accouts
檢視以太坊的賬戶

eth.getBalance(eth.accounts[0])
檢視賬戶餘額

personal.newAccount(“password”)
建立使用者

給新賬戶轉賬
我們知道沒有餘額的賬戶是沒法部署合約的,那我們就從預設賬戶轉1以太幣給新賬戶,使用以下命令(請使用你自己eth.accounts對應輸出的賬戶):

eth.sendTransaction({from: ‘0xf31498391f606b44934d4f6138d9c05799601b41’, to: ‘0x439cec3129137771b0210daa33e03cbf693608bb’, value: web3.toWei(1, “ether”)})

解鎖賬戶
在部署合約前需要先解鎖賬戶(就像銀行轉賬要輸入密碼一樣),使用以下命令:
personal.unlockAccount(eth.accounts[1],“password”)

編寫合約程式碼

pragma solidity ^0.4.18;
contract hello {
    string greeting;
    
    function hello(string _greeting) public {
        greeting = _greeting;
    }

    function say() constant public returns (string) {
        return greeting;
    }
}

簡單解釋下,我們定義了一個名為hello的合約,在合約初始化時儲存了一個字串(我們會傳入hello world),每次呼叫say返回字串。
把這段程式碼寫(拷貝)到Browser-Solidity,如果沒有錯誤,點選Details獲取部署程式碼,如:在這裡插入圖片描述

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

然後在geth的控制檯輸出
在這裡插入圖片描述

執行合約

> hello.say()
"Hello World"