aboutsummaryrefslogtreecommitdiffstats
path: root/packages/base-contract/src
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-09-27 17:21:39 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-09-27 17:21:39 +0800
commitcb99ebf78aad869e61ec2ca7c48bb25178274ec5 (patch)
tree680aa25182a6d89db550664152e7a97f0c6b9b40 /packages/base-contract/src
parent5d73eebf6abe52763ea6984f85102157abea5b6c (diff)
downloaddexon-sol-tools-cb99ebf78aad869e61ec2ca7c48bb25178274ec5.tar.gz
dexon-sol-tools-cb99ebf78aad869e61ec2ca7c48bb25178274ec5.tar.zst
dexon-sol-tools-cb99ebf78aad869e61ec2ca7c48bb25178274ec5.zip
Throw revert reasons from contract wrappers
Diffstat (limited to 'packages/base-contract/src')
-rw-r--r--packages/base-contract/src/index.ts13
1 files changed, 13 insertions, 0 deletions
diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts
index 90e576c24..538c87431 100644
--- a/packages/base-contract/src/index.ts
+++ b/packages/base-contract/src/index.ts
@@ -20,6 +20,10 @@ export interface EthersInterfaceByFunctionSignature {
[key: string]: ethers.Interface;
}
+const REVERT_ERROR_SELECTOR = '08c379a0';
+const REVERT_ERROR_SELECTOR_OFFSET = 2;
+const REVERT_ERROR_SELECTOR_END = 10;
+
export class BaseContract {
protected _ethersInterfacesByFunctionSignature: EthersInterfaceByFunctionSignature;
protected _web3Wrapper: Web3Wrapper;
@@ -82,6 +86,15 @@ export class BaseContract {
}
return txDataWithDefaults;
}
+ protected static _throwIfRevertWithReasonCallResult(rawCallResult: string): void {
+ if (rawCallResult.slice(REVERT_ERROR_SELECTOR_OFFSET, REVERT_ERROR_SELECTOR_END) === REVERT_ERROR_SELECTOR) {
+ const revertReason = ethers.utils.defaultAbiCoder.decode(
+ ['string'],
+ ethers.utils.hexDataSlice(rawCallResult, 4),
+ );
+ throw new Error(revertReason);
+ }
+ }
// Throws if the given arguments cannot be safely/correctly encoded based on
// the given inputAbi. An argument may not be considered safely encodeable
// if it overflows the corresponding Solidity type, there is a bug in the