按照本教程进行操作 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;
}
}
编译合约并生成合约的包装器代码。
生成了 Counter.sol
的 Java 代码,然后我尝试部署合约
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
但异常仍然相同。 请指导我如何解决此异常以及如何估计契约(Contract)的 Gas 价格和 Gas 限额。
最佳答案
Web3j 没有提供非常好的默认 Gas 价格/限制值。我相信无论您制定什么契约(Contract)或尝试采取什么行动,它们都是硬编码的。话虽如此,如果您的帐户中有足够的以太币,它们的默认值应该没问题(大多数时候)。
汽油价格
Gas 价格会根据网络上的事件量而波动。您支付的费用越多,您的交易被处理的可能性就越大(也越快)。 Gas 价格以 Gwei 为单位(1 Gwei = 1000000000 Wei)。您可以在主网上看到最近的天然气价格 https://ethgasstation.info/ 。通常,您会看到大多数交易支付 1-10 Gwei。对于优先级较高的交易(通常是硬币/以太币转账,因为这些交易不会消耗大量 Gas),您可能会看到 Gas 价格为 100 甚至 1000 Gwei。如果您运行的是专用网络,您可以使用任何您想要的 Gas 价格(甚至 0),但您必须设置您的矿工以接受如此低的价格工作。例如,使用 geth
,您可以使用 --gasprice
选项设置最低 Gas 价格。
MINER OPTIONS: --mine Enable mining --minerthreads value Number of CPU threads to use for mining (default: 8) --etherbase value Public address for block mining rewards (default = first account created) (default: "0") --targetgaslimit value Target gas limit sets the artificial target gas floor for the blocks to mine (default: 4712388) --gasprice "18000000000" --> Minimal gas price to accept for mining a transactions <-- --extradata value Block extra data set by the miner (default = client version)
就您而言,默认的 22 Gwei 就可以了,但您可以将其降低到 1-5。但是,当您通过以太坊钱包部署时,86440 Wei 几乎肯定无法工作。
气体限制
Web3j 仅使用旧的默认 block 气体限制作为其默认值。随着时间的推移它发生了变化,目前约为 800 万。 Ropsten是固定的,大约是470万。 Web3j 的默认值 430 万只是为了确保您在测试环境中不会达到 block 大小限制。但是,如果您以 22 Gwei 开始指定 430 万个 Gas 的交易,则您的帐户中必须有 ~0.1 以太币。您应该能够将 Gas 限制降低到 200,000(基于部署的调试输出,但您需要发布契约(Contract)代码进行确认)。
余额
最后,请确保您的帐户中有以太币!在 geth
控制台中运行一个简单的 web3.eth.getBalance()
来确认您的余额。您可以在 genesis.json 中初始化您的私有(private)网络中的账户余额
{
...
"alloc": {
"<ACCT_ID>": {
"balance": "30000000000000000000000000000"
}
}
}
关于smartcontracts - web3j 处理事务请求时出错 : insufficient funds for gas * price + value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51455941/