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)