It is a method to execute the contract created by solidity using web3j. Learn how to convert sol files to Java classes. The contracts used in this article use the contents described in the following articles.
How to convert solidity contract to Java contract class
sample.java
try {
SampleGasProvider gasProvider = new SampleGasProvider(BigInteger.valueOf(500), BigInteger.valueOf(200000));
Addition contract = Addition.deploy(
web3j,
credentials,
gasProvider
).send();
//Output the address of the contract
System.out.println("contract address: " + contract.getContractAddress());
//Method call (no transaction)
System.out.println("get: " + contract.get().send());
System.out.println("add: " + contract.add(BigInteger.valueOf(10)).sendAsync());
System.out.println("get: " + contract.get().send());
} catch (Exception e) {
e.printStackTrace();
}
SampleGasProvider.java
package jp.ethereum;
import java.math.BigInteger;
import org.web3j.tx.gas.ContractGasProvider;
public class SampleGasProvider implements ContractGasProvider{
private BigInteger gasPrice;
private BigInteger gasLimit;
public SampleGasProvider(BigInteger gasPrice, BigInteger gasLimit) {
this.gasPrice = gasPrice;
this.gasLimit = gasLimit;
}
@Override
public BigInteger getGasPrice(String contractFunc) {
return this.gasPrice;
}
@Override
public BigInteger getGasPrice() {
return this.gasPrice;
}
@Override
public BigInteger getGasLimit(String contractFunc) {
return this.gasLimit;
}
@Override
public BigInteger getGasLimit() {
return this.gasLimit;
}
}
The content of the code is simple. First, deploy the contract. When this process is complete, a transaction for deploying the contract is created, populated into the block, and then completed.
Sample.java
Addition contract = Addition.deploy(
web3j,
credentials,
gasProvider
).send();
Execution result (geth console)
INFO [09-16|17:19:41.983] Submitted contract creation fullhash=0xd169416410c2e6e7bdc02a2a27384ed2425e1433f39117a3810675fcfed85353 contract=0xf5ad7542173e8944D1aE16B4394eAA34cfdA4814
Then execute the contract.
Sample.java
System.out.println("get: " + contract.get().send());
System.out.println("add: " + contract.add(BigInteger.valueOf(10)).sendAsync());
In this example, the get process of the contract is called first. Since get only refers to the value, no transaction is created and a response is returned immediately.
Since the next add process is the process of updating the value, a transaction is created. Since we are using "send Async", we get a response before it is populated into the block. In this example, the output will be as follows.
add: java.util.concurrent.CompletableFuture@18271936[Not completed]
If you need to wait for processing to be captured in the block, make the following changes:
Sample.java
System.out.println("add: " + contract.add(BigInteger.valueOf(10)).send());
The code above was to deploy and execute the contract, but there is also a way to execute the already deployed contract.
Sample.java
Addition contract = Addition.load(
address,
web3j,
credentials,
gasProvider
);
The deployment and arguments are almost the same, but the first argument is the address of the contract. (Including 0x) Subsequent operations will not change at all.
Recommended Posts