Calling safeMint non-payable function using web3j

82 Views Asked by At

I'm trying to implement the NFT minting function for a Java project. First, I created the test project in Alchemy and deployed the smart contract using Remix IDE. Next, using remix I called the safeMint function and minted an NFT, then successfully transferred it to my wallet. To test my Java code, I first implemented the following function that displays the wallet balance:

public BigInteger getWalletBalance(String contractId, String walletId)
{
    Function function = new Function(
            "balanceOf",
            List.of(
                    new Address(walletId)
            ),
            List.of(new TypeReference<Type>() {})
    );

    String requestData = FunctionEncoder.encode(function);
    Transaction transaction = Transaction.createEthCallTransaction(null, contractId, requestData);

    try {
        var callResult = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
        return new BigInteger(callResult.getValue().substring(2), 16);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

This part of the integration worked with no issues, and I was able to successfully call the pure function, and get the valid result from the platform.

Next, I've decided to call non-payable function called safeMint:

public void mintNft(String contractId, String walletId, NftMintRequest nftMintRequest)
{
    Function function = new Function(
            "safeMint",
            List.of(
                    new Address(walletId),
                    new Utf8String("ifps://Qmc71tw4FTUK9j2sco2vSiEfuYk4CmagChPWg2RmrtXcuW")
            ),
            List.of(new TypeReference<Type>() {})
    );

    String requestData = FunctionEncoder.encode(function);
    Transaction transaction = Transaction.createFunctionCallTransaction(
            walletId,
            BigInteger.ONE,
            BigInteger.valueOf(3000000),
            BigInteger.valueOf(3000000),
            contractId,
            BigInteger.ONE,
            requestData
    );

    try {
        var callResult = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
        System.out.println(callResult.getValue());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

There are two issues I've encountered so far:

  1. RPC responds with error message "execution reverted".
  2. Since I have to support the transaction with gas fee, I have to somehow authorize it using my wallet private key. But I didn't manage to find any way to do this.

What do I have to fix in my code so I can call safeMint method as well?

0

There are 0 best solutions below