Throw revert reasons from contract wrappers

This commit is contained in:
Leonid Logvinov
2018-09-27 11:21:39 +02:00
parent 5d73eebf6a
commit cb99ebf78a
2 changed files with 17 additions and 0 deletions

View File

@@ -5,6 +5,10 @@
{
"note": "Change the way we detect BN to work with the newest ethers.js",
"pr": 1069
},
{
"note": "Add baseContract._throwIfRevertWithReasonCallResult",
"pr": 1069
}
]
},

View File

@@ -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