Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #859 from MDhondt/get-revert-reason
Retrieve revert reason for EthCall
- Loading branch information
Showing
2 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
32 changes: 31 additions & 1 deletion
32
core/src/main/java/org/web3j/protocol/core/methods/response/EthCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,42 @@ | ||
package org.web3j.protocol.core.methods.response; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.web3j.abi.FunctionReturnDecoder; | ||
import org.web3j.abi.TypeReference; | ||
import org.web3j.abi.datatypes.Type; | ||
import org.web3j.abi.datatypes.Utf8String; | ||
import org.web3j.abi.datatypes.generated.AbiTypes; | ||
import org.web3j.protocol.core.Response; | ||
|
||
/** | ||
* eth_call. | ||
*/ | ||
public class EthCall extends Response<String> { | ||
|
||
// Numeric.toHexString(Hash.sha3("Error(string)".getBytes())).substring(0, 10) | ||
private static final String errorMethodId = "0x08c379a0"; | ||
|
||
@SuppressWarnings("unchecked") | ||
private static final List<TypeReference<Type>> revertReasonType = Collections.singletonList( | ||
TypeReference.create((Class<Type>) AbiTypes.getType("string"))); | ||
|
||
public String getValue() { | ||
return getResult(); | ||
} | ||
} | ||
|
||
public boolean reverts() { | ||
return getValue() != null && getValue().startsWith(errorMethodId); | ||
} | ||
|
||
public String getRevertReason() { | ||
if (reverts()) { | ||
String hexRevertReason = getValue().substring(errorMethodId.length()); | ||
List<Type> decoded = FunctionReturnDecoder.decode(hexRevertReason, revertReasonType); | ||
Utf8String decodedRevertReason = (Utf8String) decoded.get(0); | ||
return decodedRevertReason.getValue(); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters