2019独角兽企业重金招聘Python工程师标准>>>
1、安装solc,Ubuntu 16.04下
sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install solc
装完之后执行 wheris solc
会返回solc的可执行文件路径 /usr/bin/solc
2、添加编译器
1、启动geth
, 命令如下,楼主把参数换成你自己的
geth --identity "newEth" --rpc --rpccorsdomain "*" --datadir "cdata" --port 30303 --rpcapi "personal,db,eth,net,web3" --networkid 999 --rpcport 8080 console
2、添加编译器
> admin.setSolc("/usr/bin/solc")
"solc, the solidity compiler commandline interface\nVersion: 0.4.10+commit.f0d539ae.Linux.g++\n"
> eth.getCompilers()
["Solidity"]
3、编译合约代码
> 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)
{
: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: {}
}
}
}
}
4、部署合约
很多人都是按照类似官方文档 类似的步骤,所以卡在这一步。部署合约最重要的是拿到合约编译后的字节码 和 abi, 对此不清楚的可以看社区其它的帖
> code=contractCompiled[":test"].code
"0x60606040523415600b57fe5b5b60788061001a6000396000f300606060405263ffffffff60e060020a600035041663c6888fa181146020575bfe5b3415602757fe5b60306004356042565b60408051918252519081900360200190f35b600781025b9190505600a165627a7a72305820e5878550146c3d0f5d5030fd4b2b033475091b9eabea79f5026d14bc0d103db60029"
>
> abi=contractCompiled[":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()
, 等待打包(部署合约是一笔交易)后,合约就成功部署到了私有链上,来查看一下
> 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
关键字并不影响
5、调用合约方法
上述演示的合约代码沿用的是楼主的,其实并不好,因为它并不会修改合约的状态变量,直接call
操作即可,而不用发起一笔交易。关于调用合约方法的三种方式 请戳这里
> contractInstance.multiply.call(6)