diff options
Diffstat (limited to 'packages/base-contract/src/index.ts')
-rw-r--r-- | packages/base-contract/src/index.ts | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts index 12f974445..fbb8478b7 100644 --- a/packages/base-contract/src/index.ts +++ b/packages/base-contract/src/index.ts @@ -1,5 +1,5 @@ -import { abiUtils, BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { abiUtils, BigNumber } from '@0x/utils'; +import { Web3Wrapper } from '@0x/web3-wrapper'; import { AbiDefinition, AbiType, @@ -17,9 +17,14 @@ import * as _ from 'lodash'; import { formatABIDataItem } from './utils'; export interface EthersInterfaceByFunctionSignature { - [key: string]: ethers.Interface; + [key: string]: ethers.utils.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.utils.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]; @@ -103,7 +117,7 @@ export class BaseContract { } } } - protected _lookupEthersInterface(functionSignature: string): ethers.Interface { + protected _lookupEthersInterface(functionSignature: string): ethers.utils.Interface { const ethersInterface = this._ethersInterfacesByFunctionSignature[functionSignature]; if (_.isUndefined(ethersInterface)) { throw new Error(`Failed to lookup method with function signature '${functionSignature}'`); @@ -115,7 +129,8 @@ export class BaseContract { if (abiDefinition.type !== AbiType.Function) { return false; } - const abiFunctionSignature = abiUtils.getFunctionSignature(abiDefinition); + // tslint:disable-next-line:no-unnecessary-type-assertion + const abiFunctionSignature = abiUtils.getFunctionSignature(abiDefinition as MethodAbi); if (abiFunctionSignature === functionSignature) { return true; } @@ -140,7 +155,7 @@ export class BaseContract { this._ethersInterfacesByFunctionSignature = {}; _.each(methodAbis, methodAbi => { const functionSignature = abiUtils.getFunctionSignature(methodAbi); - this._ethersInterfacesByFunctionSignature[functionSignature] = new ethers.Interface([methodAbi]); + this._ethersInterfacesByFunctionSignature[functionSignature] = new ethers.utils.Interface([methodAbi]); }); } } |