sudo add-apt-repository ppa:ethereum/ethereum sudo apt-get update sudo apt-get install solc
裝完以後執行 wheris solc
會返回solc的可執行文件路徑 /usr/bin/solc
web
一、啓動geth
, 命令以下,樓主把參數換成你本身的json
geth --identity "newEth" --rpc --rpccorsdomain "*" --datadir "cdata" --port 30303 --rpcapi "personal,db,eth,net,web3" --networkid 999 --rpcport 8080 console
二、添加編譯器api
> admin.setSolc("/usr/bin/solc") "solc, the solidity compiler commandline interface\nVersion: 0.4.10+commit.f0d539ae.Linux.g++\n" > eth.getCompilers() ["Solidity"]
> source="contract test { function multiply(uint a) returns(uint d) { return a * 7; }}" "contract test { function multiply(uint a) returns(uint d) { return a * 7; }}" > contractCompiled = eth.compile.solidity(source) { <stdin>:test: { code: "0x60606040523415600b57fe5b5b60788061001a6000396000f300606060405263ffffffff60e060020a600035041663c6888fa181146020575bfe5b3415602757fe5b60306004356042565b60408051918252519081900360200190f35b600781025b9190505600a165627a7a72305820e5878550146c3d0f5d5030fd4b2b033475091b9eabea79f5026d14bc0d103db60029", info: { abiDefinition: [{...}], compilerOptions: "--combined-json bin,abi,userdoc,devdoc --add-std --optimize", compilerVersion: "0.4.10", developerDoc: { methods: {} }, language: "Solidity", languageVersion: "0.4.10", source: "contract test { function multiply(uint a) returns(uint d) { return a * 7; }}", userDoc: { methods: {} } } } }
不少人都是按照相似官方文檔 相似的步驟,因此卡在這一步。部署合約最重要的是拿到合約編譯後的字節碼 和 abi, 對此不清楚的能夠看社區其它的帖cors
> code=contractCompiled["<stdin>:test"].code "0x60606040523415600b57fe5b5b60788061001a6000396000f300606060405263ffffffff60e060020a600035041663c6888fa181146020575bfe5b3415602757fe5b60306004356042565b60408051918252519081900360200190f35b600781025b9190505600a165627a7a72305820e5878550146c3d0f5d5030fd4b2b033475091b9eabea79f5026d14bc0d103db60029" > > abi=contractCompiled["<stdin>:test"].info.abiDefinition [{ constant: false, inputs: [{ name: "a", type: "uint256" }], name: "multiply", outputs: [{ name: "d", type: "uint256" }], payable: false, type: "function" }] > > var contract=eth.contract(abi) undefined > personal.unlockAccount(eth.accounts[0]) Unlock account 0xa122277be213f56221b6140998c03d860a60e1f8 Passphrase: // 此處輸入密碼,解鎖帳戶 > var contractInstance=contract.new({from:eth.accounts[0], data:code, gas:300000}) I0401 08:15:43.260996 internal/ethapi/api.go:1141] Tx(0x2bb5b5a407bf4885a90dbd9d3b44b00b32ee64c3cbd1c9c1d5bee40cc3b0d95f) created: 0x3f915fa924f4ab4e25feda28f147fd483e29ea77 undefined
而後開啓挖礦miner.start()
, 等待打包(部署合約是一筆交易)後,合約就成功部署到了私有鏈上,來查看一下dom
> contractInstance { abi: [{ constant: false, inputs: [{...}], name: "multiply", outputs: [{...}], payable: false, type: "function" }], address: "0x3f915fa924f4ab4e25feda28f147fd483e29ea77", transactionHash: "0x2bb5b5a407bf4885a90dbd9d3b44b00b32ee64c3cbd1c9c1d5bee40cc3b0d95f", allEvents: function(), multiply: function() }
能夠看到,上面的address
即合約帳戶的地址 細心的同窗可能發現,上面在獲取合約類 contract
和 contractInstance
的時候,我使用了var
關鍵字,而後出現了undefined
,不少同窗在QQ羣裏問,其實這並非錯誤,只是JS中兩種不一樣的定義變量的方法,有興趣的同窗能夠google it, 可是在這裏有沒有var
關鍵字並不影響ide
上述演示的合約代碼沿用的是樓主的,其實並很差,由於它並不會修改合約的狀態變量,直接call
操做便可,而不用發起一筆交易。關於調用合約方法的三種方式 請戳這裏ui
> contractInstance.multiply.call(6)