通过rpc,查询智能合约成员变量。
合约代码
contract Counter
{
uint public count = 10;
function inc(uint num) public returns (uint)
{
return count += num;
}
}
1、编译合约
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["contract Counter { uint public count = 10; function inc(uint num) public returns (uint) {return count += num; }}"],"id":1}' https://localhost:8080/rpc
{"jsonrpc":"2.0","id":1,"result":{"code":"0x6060604052600a600055341561001457600080fd5b60f1806100226000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306661abd14604e578063812600df146074575b600080fd5b3415605857600080fd5b605e60a8565b6040518082815260200191505060405180910390f35b3415607e57600080fd5b6092600480803590602001909190505060ae565b6040518082815260200191505060405180910390f35b60005481565b6000816000808282540192505081905590509190505600a165627a7a72305820f54d11b1783d8e0a981133229527af39ac08bbe7c3176c9330a07a199aa894d20029","info":{"source":"contract Counter { uint public count = 10; function inc(uint num) public returns (uint) {return count += num; }}","language":"Solidity","languageVersion":"0","compilerVersion":"0.4.19+commit.c4cbbb05.Linux.g++","abiDefinition":[{"anonymous":false,"constant":true,"payable":false,"name":"count","inputs":[],"outputs":[{"name":"","type":"uint256"}],"type":"function","stateMutability":"view"},{"anonymous":false,"constant":false,"payable":false,"name":"inc","inputs":[{"name":"num","type":"uint256"}],"outputs":[{"name":"","type":"uint256"}],"type":"function","stateMutability":"nonpayable"}],"userDoc":null,"developerDoc":null}}}
获取到编译后的”code“
。
2、获取所需要的费用
curl --data '{"jsonrpc":"2.0","method": "eth_estimateGas", "params": [{"from": "0xE15Fc439501334c0c1E30F92305855f6C3D2040A", "data": "0x6060604052600a600055341561001457600080fd5b60f1806100226000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306661abd14604e578063812600df146074575b600080fd5b3415605857600080fd5b605e60a8565b6040518082815260200191505060405180910390f35b3415607e57600080fd5b6092600480803590602001909190505060ae565b6040518082815260200191505060405180910390f35b60005481565b6000816000808282540192505081905590509190505600a165627a7a72305820f54d11b1783d8e0a981133229527af39ac08bbe7c3176c9330a07a199aa894d20029"}], "id": 5}' https://localhost:8080/rpc
{"jsonrpc":"2.0","id":5,"result":"0x3d0900"}
拿到费用0x3d0900
3、部署合约
curl --data '{"jsonrpc":"2.0","method": "eth_sendTransaction", "params": [{"from": "0xE15Fc439501334c0c1E30F92305855f6C3D2040A", "gas": "0x3d0900", "data": "0x6060604052600a600055341561001457600080fd5b60f1806100226000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306661abd14604e578063812600df146074575b600080fd5b3415605857600080fd5b605e60a8565b6040518082815260200191505060405180910390f35b3415607e57600080fd5b6092600480803590602001909190505060ae565b6040518082815260200191505060405180910390f35b60005481565b6000816000808282540192505081905590509190505600a165627a7a72305820f54d11b1783d8e0a981133229527af39ac08bbe7c3176c9330a07a199aa894d20029"}], "id": 6}' https://120.132.103.122:8080/rpc
{"jsonrpc":"2.0","id":6,"result":"0xe58bf25814b737be627c7ddc43283439725f7bee9919e663242640715b41e58f"}
4、获取合约地址
通过上面部署合约,返回的结果“0xe58bf25814b737be627c7ddc43283439725f7bee9919e663242640715b41e58f”来查询接受结果。
curl --data '{"jsonrpc":"2.0","method": "eth_getTransactionReceipt", "params": ["0xe58bf25814b737be627c7ddc43283439725f7bee9919e663242640715b41e58f"], "id": 7}' https://localhost:8080/rpc
{"jsonrpc":"2.0","id":7,"result":{"transactionHash":"0xe58bf25814b737be627c7ddc43283439725f7bee9919e663242640715b41e58f","transactionIndex":"0x0","blockHash":"0xd046e9f7f22d5436089d8375ed3eac79ff2ebeeb304b415d25b5678d8f727513","blockNumber":"0xb245","cumulativeGasUsed":"0x0217a0","gasUsed":"0x0217a0","contractAddress":"0x889e17bcad5514facb37f76f700832a300296e97","logs":[]}}
获取到合约地址: ”contractAddress":"0x889e17bcad5514facb37f76f700832a300296e97"
5、最后,获取合约变量
data是"0x06661abd"
, 是通过 web3.sha3("count()").substring(0, 8)
得来的。
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"from":"0xE15Fc439501334c0c1E30F92305855f6C3D2040A", "to":"0x889e17bcad5514facb37f76f700832a300296e97", "gas":"0x300000", "gasPrice":"0x10000000000", "data":"0x06661abd"}, "latest"],"id":1}' https://localhost:8080/rpc
{"jsonrpc":"2.0","id":1,"result":"0x000000000000000000000000000000000000000000000000000000000000000a"}
结果为0x000000000000000000000000000000000000000000000000000000000000000a
,从16进制转到10进制就是10
。
可以介绍一下program.java 和 VM.java 这两个类吗,最近要模仿以太坊做一个智能合约,这两个类里面的成员变量有点看不懂