using web3j
for loading a contract which is deployed on the private network
following this tutorial
https://web3j.readthedocs.io/en/latest/smart_contracts.html#constant-methods
smart contract
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 created its java wrapper code its java wrapper code is
package models.smartcontract;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
/**
* <p>Auto generated code.
* <p><strong>Do not modify!</strong>
* <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
* <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
*
* <p>Generated with web3j version 3.4.0.
*/
public class Counter extends Contract {
private static final String BINARY = "60806040526000805534801561001457600080fd5b5060ea806100236000396000f30060806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416638ada066e81146057578063d732d95514607b578063e8927fbc14608f575b600080fd5b348015606257600080fd5b50606960a1565b60408051918252519081900360200190f35b348015608657600080fd5b50608d60a7565b005b348015609a57600080fd5b50608d60b3565b60005490565b60008054600019019055565b6000805460010190555600a165627a7a72305820e1abe3f0dcb8c434de0fe090f66a6d6965a9a74dd54b521d92c04bbe40d5069f0029";
public static final String FUNC_GETCOUNTER = "getCounter";
public static final String FUNC_DECREASE = "decrease";
public static final String FUNC_INCREASE = "increase";
protected Counter(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}
protected Counter(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
public RemoteCall<BigInteger> getCounter() {
final Function function = new Function(FUNC_GETCOUNTER,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
public RemoteCall<TransactionReceipt> decrease() {
final Function function = new Function(
FUNC_DECREASE,
Arrays.<Type>asList(),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}
public RemoteCall<TransactionReceipt> increase() {
final Function function = new Function(
FUNC_INCREASE,
Arrays.<Type>asList(),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}
public static RemoteCall<Counter> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(Counter.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
}
public static RemoteCall<Counter> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(Counter.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
}
public static Counter load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new Counter(contractAddress, web3j, credentials, gasPrice, gasLimit);
}
public static Counter load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new Counter(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
}
java class code is
Web3j web3 = Web3j.build(new org.web3j.protocol.http.HttpService("http://localhost:8080"));
Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
System.out.println("clientVersion is -> "+clientVersion);
Credentials credentials = WalletUtils.loadCredentials("wss123456", "/home/sara/.ethereum/privatenet/keystore/UTC--2018-07-20T11-40-45.429000000Z--9e0910461ff92d0b77a5aa90769764a8f1a8d6d9.json");
BigInteger gp = BigInteger.valueOf(86440);
BigInteger gl = BigInteger.valueOf(186440);
Counter contract = Counter.load("0xF9DdE8E8dE5fd4A0a79BA96a9aAf4F7C7c860E13",web3, credentials,gp,gl); // constructor params
System.out.println("before increase counter "+contract.getCounter().send());
TransactionReceipt transactionReceipt = contract1.increase().send();
System.out.println("after increase counter "+contract.getCounter().send());
contract.getCounter().send()
is displaying 0
and i am getting exception on this line
TransactionReceipt transactionReceipt = contract1.increase().send();
here are the exception logs
info] clientVersion is -> Geth/MyNodeName/v1.8.10-stable-eae63c51/linux-amd64/go1.10
[info] before increase counter 0
[error] 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.executeTransaction(Contract.java:259)
[error] at org.web3j.tx.Contract.executeTransaction(Contract.java:253)
[error] at org.web3j.tx.Contract.lambda$executeRemoteCallTransaction$3(Contract.java:305)
[error] at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)
[error] at models.smartcontract.FirstContractJava.main(FirstContractJava.java:46)
is that the right way to call the methods? also why contract1.increase().send(); methods is throwing an exception is there anything which is missing? please guide