首先要開發以太坊的智能合約,須要EVM(以太坊虛擬機),也就是須要運行的環境,咱們能夠經過 geth 來設置開發環境:web
geth --datadir testNet --dev console 2>> test.log
命令參數說明(更多命令詳解可閱讀Geth命令用法-參數詳解篇):
–dev 啓用開發者網絡(模式),開發者網絡會使用POA共識,默認預分配一個開發者帳戶而且會自動開啓挖礦。
–datadir 後面的參數是區塊數據及祕鑰存放目錄。
第一次輸入命令後,它會放在當前目錄下新建一個testNet目錄來存放數據。
console 進入控制檯
2>> test.log 表示把控制檯日誌輸出到test.log文件網絡
而後就進入了以太坊客戶端 console 界面。spa
在 console 裏,你能夠經過相關命令來創建帳戶、挖礦、轉帳、查看區塊高度等。日誌
接着,創建智能合約:code
pragma solidity >=0.4.22 <0.6.0;
contract man {
string word;
constructor(string memory s) public {
word = s;
}
function say() public view returns (string memory) {
return word;
}
}
能夠在 remix (在線 solidity 編譯器)編譯你的代碼blog
選擇編譯器版本,而後編譯,再點擊 detail ,彈出詳細信息,開發
var s = "hello"; var manContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"say","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"s","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]); var man = manContract.new( s, { from: web3.eth.accounts[2], data: '0x608060405234801561001057600080fd5b506040516102ce3803806102ce8339810180604052602081101561003357600080fd5b81019080805164010000000081111561004b57600080fd5b8201602081018481111561005e57600080fd5b815164010000000081118282018710171561007857600080fd5b505080519093506100929250600091506020840190610099565b5050610134565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100da57805160ff1916838001178555610107565b82800160010185558215610107579182015b828111156101075782518255916020019190600101906100ec565b50610113929150610117565b5090565b61013191905b80821115610113576000815560010161011d565b90565b61018b806101436000396000f3fe608060405234801561001057600080fd5b5060043610610047577c01000000000000000000000000000000000000000000000000000000006000350463954ab4b2811461004c575b600080fd5b6100546100c9565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561008e578181015183820152602001610076565b50505050905090810190601f1680156100bb5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60008054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156101555780601f1061012a57610100808354040283529160200191610155565b820191906000526020600020905b81548152906001019060200180831161013857829003601f168201915b505050505090509056fea165627a7a723058200ed7e0241815237b5376e1b879e7461322ec6e49ab75cf7474fc8e8547b1c8bc0029', gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } })
將文本複製起來,修改其中的變量,from 地址等,而後在 geth console 中輸入,則部署成功rem
運行合約,發現error部署
解決方式:get
eth.defaultAccount=eth.coinbase
最後獲得正確結果: