按照本教程 https://github.com/web3j/web3j
将geth
客户端作为专用网络启动。这是合约代码
pragma solidity ^0.4.10;
contract Counter {
uint256 counter =0;
function increase() public {
counter++;
}
function decrease() public{
counter--;
}
function getCounter() public constant returns (uint256) {
return counter;
}
}
编译合约并生成合约的包装代码。生成了 Java 代码Counter.sol
,然后我尝试部署合约
Web3j web3 = Web3j.build(new org.web3j.protocol.http.HttpService("http://localhost:8080"));
Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
Counter contract = Counter.deploy(web3, credentials,Counter.GAS_PRICE;,Counter.GAS_LIMIT).send(); // constructor params
System.out.println("before increase counter "+contract.getCounter());
contract.increase();
System.out.println("after increase counter "+contract.getCounter());
contract.decrease();
System.out.println("after decrease counter "+contract.getCounter());
获取异常
ontract gas limit 4300000
[info] counter gas price 22000000000
[error] java.lang.RuntimeException: java.lang.RuntimeException: Error processing transaction request: insufficient funds for gas * price + value
[error] at org.web3j.tx.Contract.deploy(Contract.java:350)
[error] at org.web3j.tx.Contract.lambda$deployRemoteCall$5(Contract.java:384)
[error] at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)
[error] at models.smartcontract.FirstContractJava.main(FirstContractJava.java:33)
[error] Caused by: java.lang.RuntimeException: Error processing transaction request: insufficient funds for gas * price + value
[error] at org.web3j.tx.TransactionManager.processResponse(TransactionManager.java:67)
[error] at org.web3j.tx.TransactionManager.executeTransaction(TransactionManager.java:51)
[error] at org.web3j.tx.ManagedTransaction.send(ManagedTransaction.java:87)
[error] at org.web3j.tx.Contract.executeTransaction(Contract.java:275)
[error] at org.web3j.tx.Contract.create(Contract.java:317)
[error] at org.web3j.tx.Contract.deploy(Contract.java:346)
[error] ... 3 more
然后我使用以太坊钱包部署了合约,因为它为我们估算了 gas 限制和 gas 价格。它估计
gas price 86440
gas limit 186440
所以我改变了这样的代码
BigInteger gp = BigInteger.valueOf(86440);
BigInteger gl = BigInteger.valueOf(186440);
Counter contract = Counter.deploy(web3, credentials,gp,gl).send(); // constructor params
但例外仍然相同。请指导我如何解决此异常以及如何估算合同的汽油价格和汽油限制。