Si vous souhaitez utiliser le contrat utilisé en solidité avec web3j, vous devez utiliser des classes Java pour faire fonctionner le contrat.
Bien sûr, il est possible de créer une classe qui gère ce contrat à partir de zéro, mais je laisserai une note car il y avait un moyen simple de le créer.
Un outil de conversion de fichiers sol en fichiers java est fourni sur la ligne de commande, alors téléchargez-le.
Le réglage est terminé lorsque l'affichage suivant apparaît.
_ _____ _ _
| | |____ (_) (_)
__ _____| |__ / /_ _ ___
\ \ /\ / / _ \ '_ \ \ \ | | | / _ \
\ V V / __/ |_) |.___/ / | _ | || (_) |
\_/\_/ \___|_.__/ \____/| |(_)|_| \___/
_/ |
|__/
Usage: web3j version|wallet|solidity ...
Tout d'abord, préparez le fichier sol.
addition.sol
pragma solidity ^0.4.24;
contract Addition{
int num = 0;
function add(int a) public {
num += a;
}
function get() public view returns(int) {
return num;
}
}
Compilez ce fichier sol et extrayez bin et abi.
solc --bin addition.sol > addition.bin
solc --abi addition.sol > addition.abi
Les fichiers de sortie sont les suivants.
addition.bin
======= addition.sol:Addition =======
Binary:
60806040526000805534801561001457600080fd5b5060e7806100236000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636d4ce63c14604e57806387db03b7146076575b600080fd5b348015605957600080fd5b50606060a0565b6040518082815260200191505060405180910390f35b348015608157600080fd5b50609e6004803603810190808035906020019092919050505060a9565b005b60008054905090565b806000808282540192505081905550505600a165627a7a723058200522274b97a5967f28327168f4654fbd3921238ed6de8e155c966f7dad168e020029
addition.abi
======= addition.sol:Addition =======
Contract JSON ABI
[{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"a","type":"int256"}],"name":"add","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]
Pour ces deux fichiers, supprimez les premières lignes (jusqu'à ~ Binary: / ~ JSON ABI) car elles seront inutiles pour un traitement futur.
Créez avec la commande suivante
web3j solidity generate -a=addition.abi -b=addition.bin -o=src -p=jp.ethereum.contracts
Résultat d'exécution
_ _____ _ _
| | |____ (_) (_)
__ _____| |__ / /_ _ ___
\ \ /\ / / _ \ '_ \ \ \ | | | / _ \
\ V V / __/ |_) |.___/ / | _ | || (_) |
\_/\_/ \___|_.__/ \____/| |(_)|_| \___/
_/ |
|__/
Generating jp.ethereum.contracts.Addition ... File written to src
Si ce résultat est obtenu, la sortie est terminée. Il est sorti dans le répertoire spécifié par l'option "-O". Dans ce cas, le package "jp.ethereum.contracts" spécifié par "-p" est créé sous le répertoire src et Addtion.java est créé sous ce package.
addtion.java
package jp.ethereum.contracts;
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.Int256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;
/**
* <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 4.5.0.
*/
public class Addition extends Contract {
private static final String BINARY = "60806040526000805534801561001457600080fd5b5060e7806100236000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636d4ce63c14604e57806387db03b7146076575b600080fd5b348015605957600080fd5b50606060a0565b6040518082815260200191505060405180910390f35b348015608157600080fd5b50609e6004803603810190808035906020019092919050505060a9565b005b60008054905090565b806000808282540192505081905550505600a165627a7a723058200522274b97a5967f28327168f4654fbd3921238ed6de8e155c966f7dad168e020029\r\n";
public static final String FUNC_GET = "get";
public static final String FUNC_ADD = "add";
@Deprecated
protected Addition(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}
protected Addition(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
}
@Deprecated
protected Addition(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
protected Addition(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);
}
public RemoteFunctionCall<BigInteger> get() {
final Function function = new Function(FUNC_GET,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Int256>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
public RemoteFunctionCall<TransactionReceipt> add(BigInteger a) {
final Function function = new Function(
FUNC_ADD,
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Int256(a)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}
@Deprecated
public static Addition load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new Addition(contractAddress, web3j, credentials, gasPrice, gasLimit);
}
@Deprecated
public static Addition load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new Addition(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
public static Addition load(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
return new Addition(contractAddress, web3j, credentials, contractGasProvider);
}
public static Addition load(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
return new Addition(contractAddress, web3j, transactionManager, contractGasProvider);
}
public static RemoteCall<Addition> deploy(Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
return deployRemoteCall(Addition.class, web3j, credentials, contractGasProvider, BINARY, "");
}
@Deprecated
public static RemoteCall<Addition> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(Addition.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
}
public static RemoteCall<Addition> deploy(Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
return deployRemoteCall(Addition.class, web3j, transactionManager, contractGasProvider, BINARY, "");
}
@Deprecated
public static RemoteCall<Addition> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(Addition.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
}
}
Cette classe est requise lors de l'exécution d'un contrat à l'aide de Web3j. Il existe de nombreuses méthodes obsolètes, alors soyez prudent lorsque vous les utilisez.
Recommended Posts