Skip to content

Commit

Permalink
Merge pull request #1656 from alexdupre/revert-reason-fix
Browse files Browse the repository at this point in the history
Fix retrieval of revert reason by using the same weiAmount in the call.
  • Loading branch information
andrii-kl authored Apr 27, 2022
2 parents a35ec52 + 14daa04 commit f9eeacc
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ public static Transaction createFunctionCallTransaction(
return new Transaction(from, nonce, gasPrice, gasLimit, to, null, data);
}

public static Transaction createEthCallTransaction(
String from, String to, String data, BigInteger weiValue) {

return new Transaction(from, null, null, null, to, weiValue, data);
}

public static Transaction createEthCallTransaction(String from, String to, String data) {

return new Transaction(from, null, null, null, to, null, data);
Expand Down
48 changes: 27 additions & 21 deletions core/src/main/java/org/web3j/tx/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.web3j.protocol.core.methods.response.EthGetCode;
import org.web3j.protocol.core.methods.response.Log;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.exceptions.JsonRpcError;
import org.web3j.protocol.exceptions.TransactionException;
import org.web3j.tx.exceptions.ContractCallException;
import org.web3j.tx.gas.ContractEIP1559GasProvider;
Expand Down Expand Up @@ -365,34 +366,39 @@ TransactionReceipt executeTransaction(
throws TransactionException, IOException {

TransactionReceipt receipt = null;
if (gasProvider instanceof ContractEIP1559GasProvider) {
ContractEIP1559GasProvider eip1559GasProvider =
(ContractEIP1559GasProvider) gasProvider;
if (eip1559GasProvider.isEIP1559Enabled()) {
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 =
sendEIP1559(
eip1559GasProvider.getChainId(),
send(
contractAddress,
data,
weiValue,
eip1559GasProvider.getGasLimit(funcName),
eip1559GasProvider.getMaxPriorityFeePerGas(funcName),
eip1559GasProvider.getMaxFeePerGas(funcName),
gasProvider.getGasPrice(funcName),
gasProvider.getGasLimit(funcName),
constructor);
}
}
if (receipt == null) {
receipt =
send(
contractAddress,
data,
weiValue,
gasProvider.getGasPrice(funcName),
gasProvider.getGasLimit(funcName),
constructor);
} catch (JsonRpcError error) {
throw new TransactionException(error.getData().toString());
}

if (!receipt.isStatusOK()) {
if (receipt != null && !receipt.isStatusOK()) {
throw new TransactionException(
String.format(
"Transaction %s has failed with status: %s. "
Expand All @@ -403,7 +409,7 @@ TransactionReceipt executeTransaction(
receipt.getGasUsedRaw() != null
? receipt.getGasUsed().toString()
: "unknown",
extractRevertReason(receipt, data, web3j, true)),
extractRevertReason(receipt, data, web3j, true, weiValue)),
receipt);
}
return receipt;
Expand Down
53 changes: 49 additions & 4 deletions core/src/main/java/org/web3j/utils/RevertReasonExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.web3j.utils;

import java.io.IOException;
import java.math.BigInteger;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
Expand All @@ -31,20 +32,22 @@ public class RevertReasonExtractor {
* @param data the reverted transaction data
* @param web3j Web3j instance
* @param revertReasonCallEnabled flag of reason retrieval via additional call
* @param weiValue the value sent in the reverted transaction
* @return the reverted transaction error reason if exists or null otherwise
* @throws IOException if the call to the node fails
*/
public static String extractRevertReason(
TransactionReceipt transactionReceipt,
String data,
Web3j web3j,
Boolean revertReasonCallEnabled)
Boolean revertReasonCallEnabled,
BigInteger weiValue)
throws IOException {

if (transactionReceipt.getRevertReason() != null) {
return transactionReceipt.getRevertReason();
} else if (revertReasonCallEnabled) {
String revertReason = retrieveRevertReason(transactionReceipt, data, web3j);
String revertReason = retrieveRevertReason(transactionReceipt, data, web3j, weiValue);
if (revertReason != null) {
transactionReceipt.setRevertReason(revertReason);
return revertReason;
Expand All @@ -53,26 +56,68 @@ public static String extractRevertReason(
return MISSING_REASON;
}

/**
* Extracts the error reason of a reverted transaction (if one exists and enabled).
*
* @param transactionReceipt the reverted transaction receipt
* @param data the reverted transaction data
* @param web3j Web3j instance
* @param revertReasonCallEnabled flag of reason retrieval via additional call
* @return the reverted transaction error reason if exists or null otherwise
* @throws IOException if the call to the node fails
*/
@Deprecated
public static String extractRevertReason(
TransactionReceipt transactionReceipt,
String data,
Web3j web3j,
Boolean revertReasonCallEnabled)
throws IOException {

return extractRevertReason(transactionReceipt, data, web3j, revertReasonCallEnabled, null);
}

/**
* Retrieves the error reason of a reverted transaction (if one exists).
*
* @param transactionReceipt the reverted transaction receipt
* @param data the reverted transaction data
* @param web3j Web3j instance
* @param weiValue the value sent in the reverted transaction
* @return the reverted transaction error reason if exists or null otherwise
* @throws IOException if the call to the node fails
*/
public static String retrieveRevertReason(
TransactionReceipt transactionReceipt, String data, Web3j web3j) throws IOException {
TransactionReceipt transactionReceipt, String data, Web3j web3j, BigInteger weiValue)
throws IOException {

if (transactionReceipt.getBlockNumber() == null) {
return null;
}
return web3j.ethCall(
Transaction.createEthCallTransaction(
transactionReceipt.getFrom(), transactionReceipt.getTo(), data),
transactionReceipt.getFrom(),
transactionReceipt.getTo(),
data,
weiValue),
DefaultBlockParameter.valueOf(transactionReceipt.getBlockNumber()))
.send()
.getRevertReason();
}

/**
* Retrieves the error reason of a reverted transaction (if one exists).
*
* @param transactionReceipt the reverted transaction receipt
* @param data the reverted transaction data
* @param web3j Web3j instance
* @return the reverted transaction error reason if exists or null otherwise
* @throws IOException if the call to the node fails
*/
@Deprecated
public static String retrieveRevertReason(
TransactionReceipt transactionReceipt, String data, Web3j web3j) throws IOException {

return retrieveRevertReason(transactionReceipt, data, web3j, null);
}
}

0 comments on commit f9eeacc

Please sign in to comment.