Reputation: 3475
Following this tutorial https://github.com/web3j/web3j
Started the geth
client as a private network.
Here is the contract code
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;
}
}
Compiled the contract and genetaed the wrapper code for the contract.
Java code for Counter.sol
was generated, then I tried to deploy the contract
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());
Getting exception
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
Then I deployed the contract using ethereum wallet because it estimates the gas limit and gas price for us. It estimated
gas price 86440
gas limit 186440
So I changed the code like this
BigInteger gp = BigInteger.valueOf(86440);
BigInteger gl = BigInteger.valueOf(186440);
Counter contract = Counter.deploy(web3, credentials,gp,gl).send(); // constructor params
But the exception remained same. Please guide me how to resolve this exception also how to estimate gas price and gas limit for a contract.
Upvotes: 2
Views: 4334
Reputation: 10961
Web3j doesn't give very good default gas price/limit values. I believe they are hardcoded regardless of the contract you develop or the action you try to take. That being said, their default values SHOULD be ok (most of the time) if you have enough ether in your account.
Gas Price
Gas prices fluctuate depending on how much activity is on the network. The more you pay, the more likely (and faster) your transaction will be picked up. Gas prices are measured in Gwei (1 Gwei = 1000000000 Wei). You can see recent gas prices being in MainNet at https://ethgasstation.info/. Usually, you'll see most transactions paying 1-10 Gwei. For higher priority transactions (usually coin/ether transfers as those transactions don't consume a lot of gas), you may see gas prices at 100 or even 1000 Gwei. If you're running a private network, you can use whatever gas price you want (even 0), but you have to set up your miners to accept work at that low of a price. For example, with geth
, you can set the minimal gas price with the --gasprice
option.
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)
In your case, the default 22 Gwei is ok, but you can probably lower that to 1-5. However, the 86440 Wei when you deployed through Ethereum Wallet almost certainly won't work.
Gas Limit
Web3j just uses an old default block gas limit as its default value. It has changed over time and is currently around 8 million. Ropsten is fixed and is about 4.7 million. Web3j's default of 4.3 million is just to make sure you don't hit block size limits in test environments. However, if you start a transaction specifying 4.3 million gas at 22 Gwei, you have to have ~0.1 ether in your account. You should be able to lower you gas limit to 200,000 (based off your debug output from deployment, but you would need to post the contract code to confirm).
Balance
Finally, make sure you have ether in your account! Run a simple web3.eth.getBalance()
in your geth
console to confirm your balance. You can initialize an account balance in your private network in the genesis.json
{
...
"alloc": {
"<ACCT_ID>": {
"balance": "30000000000000000000000000000"
}
}
}
Upvotes: 2