aboutsummaryrefslogtreecommitdiffstats
path: root/packages/base-contract/src/index.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-09-28 19:08:12 +0800
committerFabio Berger <me@fabioberger.com>2018-09-28 19:08:12 +0800
commitba7de7204d29d4004c347190be7a3b8c84951b82 (patch)
tree9dddbd1ded45484a6cb968cdf799bf5ce991477b /packages/base-contract/src/index.ts
parentf3ad64aa1c2930affbfd074316b5f407580b7523 (diff)
parenta737cfa004ee1dc18be935f61fb9c289ed5623fd (diff)
downloaddexon-sol-tools-ba7de7204d29d4004c347190be7a3b8c84951b82.tar
dexon-sol-tools-ba7de7204d29d4004c347190be7a3b8c84951b82.tar.gz
dexon-sol-tools-ba7de7204d29d4004c347190be7a3b8c84951b82.tar.bz2
dexon-sol-tools-ba7de7204d29d4004c347190be7a3b8c84951b82.tar.lz
dexon-sol-tools-ba7de7204d29d4004c347190be7a3b8c84951b82.tar.xz
dexon-sol-tools-ba7de7204d29d4004c347190be7a3b8c84951b82.tar.zst
dexon-sol-tools-ba7de7204d29d4004c347190be7a3b8c84951b82.zip
merge development
Diffstat (limited to 'packages/base-contract/src/index.ts')
-rw-r--r--packages/base-contract/src/index.ts22
1 files changed, 18 insertions, 4 deletions
diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts
index 12f974445..981e6fca6 100644
--- a/packages/base-contract/src/index.ts
+++ b/packages/base-contract/src/index.ts
@@ -20,6 +20,11 @@ export interface EthersInterfaceByFunctionSignature {
[key: string]: ethers.Interface;
}
+const REVERT_ERROR_SELECTOR = '08c379a0';
+const REVERT_ERROR_SELECTOR_OFFSET = 2;
+const REVERT_ERROR_SELECTOR_BYTES_LENGTH = 4;
+const REVERT_ERROR_SELECTOR_END = REVERT_ERROR_SELECTOR_OFFSET + REVERT_ERROR_SELECTOR_BYTES_LENGTH * 2;
+
export class BaseContract {
protected _ethersInterfacesByFunctionSignature: EthersInterfaceByFunctionSignature;
protected _web3Wrapper: Web3Wrapper;
@@ -61,7 +66,7 @@ export class BaseContract {
}
}
protected static _bnToBigNumber(_type: string, value: any): any {
- return _.isObject(value) && value._bn ? new BigNumber(value.toString()) : value;
+ return _.isObject(value) && value._hex ? new BigNumber(value.toString()) : value;
}
protected static async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>(
txData: T,
@@ -82,15 +87,24 @@ 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, REVERT_ERROR_SELECTOR_BYTES_LENGTH),
+ );
+ 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
// encoder, or the encoder performs unsafe type coercion.
public static strictArgumentEncodingCheck(inputAbi: DataItem[], args: any[]): void {
- const coder = ethers.utils.AbiCoder.defaultCoder;
+ const coder = new ethers.AbiCoder();
const params = abiUtils.parseEthersParams(inputAbi);
- const rawEncoded = coder.encode(params.names, params.types, args);
- const rawDecoded = coder.decode(params.names, params.types, rawEncoded);
+ const rawEncoded = coder.encode(inputAbi, args);
+ const rawDecoded = coder.decode(inputAbi, rawEncoded);
for (let i = 0; i < rawDecoded.length; i++) {
const original = args[i];
const decoded = rawDecoded[i];