[web3j] Java calls the method in the contract on the chain

Prepare the infura link in advance , the contract address on the chain , the method name in the contract , and the private key of the wallet (click the three dots on the right of the account address in the little fox plug-in, and then click the account details to see it) . Note: The following example calls two get methods, passing parameter Uint

1. Packages used

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.3.1</version>
</dependency>
<!-- web3j -->
<dependency>
    <groupId>org.web3j</groupId>
    <artifactId>core</artifactId>
    <version>4.8.7</version>
</dependency>
<dependency>
    <groupId>org.web3j</groupId>
    <artifactId>codegen</artifactId>
    <version>5.0.0</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>


import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthCall;
import org.web3j.protocol.http.HttpService;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;

Two, the code


/**
 * @author maomo
 * @version 1.0.0
 * @date 2022-10-31
 **/
public class Web3Utils {
    static  Web3j web3j = Web3j.build(new HttpService("https://goerli.infura.io/v3/***************"));

    /** 链上合约地址 */
    public static final String CONTRACT_ADDRESS = "***************";

    /** 没有拥有者时返回的地址 */
    public static String DEFAULT_OWNER = "0x0000000000000000000000000000000000000000";
    /** 方法名,通过tokenId获取ownerAddress(这是erc721自带的方法) */
    public static String OWNER_OF = "ownerOf";
    /** 方法名,通过nftId获取ownerAddress(这是合约里自定义的方法) */
    public static String GET_OWNER_BY_NFT_ID = "getOwnerByNFTId";


    public static void main(String[] args) {
        System.out.println("ownerOf:"+new Web3Utils().getOwnerByTokenId(1));
        System.out.println("getOwnerByNFTId:"+new Web3Utils().getOwnerByNftId(1));
    }

    /**
     * 调用合约的只读方法,无需gas
     * @param functionName 合约方法名
     * @param param 参数
     * @throws Exception 异常
     */
    public String getValue(String functionName,String param) {
        try {
            Function function = new Function(
                    functionName,
                    Collections.singletonList(new Uint(new BigInteger(param))),
                    Collections.singletonList(new TypeReference<Address>() {
                    }));
            String encodedFunction = FunctionEncoder.encode(function);

            EthCall response = web3j.ethCall(
                    Transaction.createEthCallTransaction(null, contractAddress, encodedFunction),
                    DefaultBlockParameterName.LATEST)
                    .sendAsync().get();

            List<Type> results = FunctionReturnDecoder.decode(response.getValue(), function.getOutputParameters());
            Type type = results.get(0);
            String value = (String) type.getValue();
            if(ObjectUtils.isEmpty(value) || DEFAULT_OWNER.equals(value)){
                return null;
            }
            return (String)type.getValue();
        }catch (Exception ignore){
            return null;
        }
    }

}

Guess you like

Origin blog.csdn.net/qq_37928038/article/details/127618866