编写智能合约代码
pragma solidity ^0.4.4;
contract test {
function multiply(uint a) returns(uint d){
return a * 7;
}
}
获取智能合约字节码和abi
- 用truffle获取,详见上一章
- 用remix获取:代码拷贝到https://remix.ethereum.org,编译,然后拷贝字节码和ABI。
字节码
合约代码转化为16进制以后的数据,所以记得加上0x!!
0x6060604052341561000f57600080fd5b60b18061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60006007820290509190505600a165627a7a72305820b1b22e476dba064d783813defae9aeb370c57fb48b012fd4a9e2c0cee54010670029
用code 存起来
> var code = "0x6060604052341561000f57600080fd5b60b18061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60006007820290509190505600a165627a7a72305820b1b22e476dba064d783813defae9aeb370c57fb48b012fd4a9e2c0cee54010670029"
undefined
> code
"0x6060604052341561000f57600080fd5b60b18061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60006007820290509190505600a165627a7a72305820b1b22e476dba064d783813defae9aeb370c57fb48b012fd4a9e2c0cee54010670029"
>
ABI
在这里应该用solc来获取比较好,但是笔者怎么都装不上solc于是用另外的方法。
在[http://www.bejson.com]删除空格
[{\"constant\":false,\"inputs\":[{\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"multiply\",\"outputs\":[{\"name\":\"d\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]
解锁有以太币的账号
> personal.unlockAccount(personal.listAccounts[1])
Unlock account 0xc2dec5a4c6e0e55651baaf9be4f91dbf2aadc017
Passphrase:
true
预估合约需要的gas费
> web3.eth.estimateGas({data:code})
99930
部署合约
- 创建合约对象
> var myContract = eth.contract(abi);
- 实例化:
from有余额的解锁账号
gas费用需要大于预估的gas费
var contract = myContract.new({from:personal.listAccounts[1],data:code,gas:100000});
undefined
> contract
{
abi: [{
constant: false,
inputs: [{...}],
name: "multiply",
outputs: [{...}],
payable: false,
stateMutability: "nonpayable",
type: "function"
}],
address: undefined,
transactionHash: "0x8486197429110bad4591157ce26db5b0a855cda40aabd44b949f67c17b66f915"
}
>
- 挖矿之后,调用
> miner.start()
null
> miner.stop()
true
> txpool.status
{
pending: 0,
queued: 0
}
> eth.getCode(contract.address)
"0x606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60006007820290509190505600a165627a7a72305820b1b22e476dba064d783813defae9aeb370c57fb48b012fd4a9e2c0cee54010670029"
> contract.address
"0x12918ddf1b06ca989809382c10190e61319c0406"
> contract.multiply.call(6);
42
>