web3j icon indicating copy to clipboard operation
web3j copied to clipboard

Contract JsonRpcError Try and Catch getData().toString() NPE during executeTransaction

Open matthew-cameron1 opened this issue 3 years ago • 1 comments

Issue_title

executeTransaction NPE inside try and catch of JsonRpcError

Issue_description

executeTransaction method inside Contract.java has a try-catch for a JsonRpcError. The error can have a variable data of type Object which is nullable if there is not one. .toString() is called on this without any null checks causing an NPE to be thrown and 0 information as to why the transaction execution failed being returned / thrown.

Issue_context

I would recommend checking if the data is null and passing in the error code + message on line 403 of Contract.java to allow for debugging and issue resolution so that it is not just an NPE

matthew-cameron1 avatar Jul 06 '22 22:07 matthew-cameron1

I will attached a PR to this however this would be my proposed change (tested and gives good results)

`TransactionReceipt executeTransaction( String data, BigInteger weiValue, String funcName, boolean constructor) throws TransactionException, IOException {

    TransactionReceipt receipt = null;
    try {
        if (gasProvider instanceof ContractEIP1559GasProvider) {
            ContractEIP1559GasProvider eip1559GasProvider =
                    (ContractEIP1559GasProvider) gasProvider;
            if (eip1559GasProvider.isEIP1559Enabled()) {
                receipt =
                        sendEIP1559(
                                eip1559GasProvider.getChainId(),
                                contractAddress,
                                data,
                                weiValue,
                                eip1559GasProvider.getGasLimit(funcName),
                                eip1559GasProvider.getMaxPriorityFeePerGas(funcName),
                                eip1559GasProvider.getMaxFeePerGas(funcName),
                                constructor);
            }
        }

        if (receipt == null) {
            receipt =
                    send(
                            contractAddress,
                            data,
                            weiValue,
                            gasProvider.getGasPrice(funcName),
                            gasProvider.getGasLimit(funcName),
                            constructor);
        }
    } catch (JsonRpcError error) {

        if (error.getData() != null) {
            throw new TransactionException(error.getData().toString());
        } else {
            throw new TransactionException(
                    String.format(
                            "JsonRpcError thrown with code %d. Message: %s",
                            error.getCode(), error.getMessage()));
        }
    }

    if (!(receipt instanceof EmptyTransactionReceipt)
            && receipt != null
            && !receipt.isStatusOK()) {
        throw new TransactionException(
                String.format(
                        "Transaction %s has failed with status: %s. "
                                + "Gas used: %s. "
                                + "Revert reason: '%s'.",
                        receipt.getTransactionHash(),
                        receipt.getStatus(),
                        receipt.getGasUsedRaw() != null
                                ? receipt.getGasUsed().toString()
                                : "unknown",
                        extractRevertReason(receipt, data, web3j, true, weiValue)),
                receipt);
    }
    return receipt;
}`

matthew-cameron1 avatar Jul 07 '22 04:07 matthew-cameron1

Suggested changes were added.

gtebrean avatar Dec 23 '22 10:12 gtebrean