From b06f8239e1fe75703f88d34c0d225701406e28c2 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Sat, 22 Dec 2018 17:23:02 -0800 Subject: Finished porting new abi encoder to contracts --- packages/base-contract/src/index.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'packages/base-contract/src') diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts index 2fd50b941..aeb90e6f7 100644 --- a/packages/base-contract/src/index.ts +++ b/packages/base-contract/src/index.ts @@ -91,10 +91,7 @@ export class BaseContract { } 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), - ); + const revertReason = AbiEncoder.create('(string)').decode(ethers.utils.hexDataSlice(rawCallResult, REVERT_ERROR_SELECTOR_BYTES_LENGTH)); throw new Error(revertReason); } } @@ -140,6 +137,28 @@ export class BaseContract { }) as MethodAbi; return methodAbi; } + protected _strictEncodeArguments(functionSignature: string, functionArguments: any): string { + const abiEncoder = this._lookupAbiEncoder(functionSignature); + const inputAbi = abiEncoder.getDataItem().components; + if (inputAbi === undefined) { + throw new Error(`Undefined Method Input ABI`); + } + const params = abiUtils.parseEthersParams(inputAbi); + const rawEncoded = abiEncoder.encode(functionArguments); + const rawDecoded = abiEncoder.decodeAsArray(rawEncoded); + for (let i = 0; i < rawDecoded.length; i++) { + const original = functionArguments[i]; + const decoded = rawDecoded[i]; + if (!abiUtils.isAbiDataEqual(params.names[i], params.types[i], original, decoded)) { + throw new Error( + `Cannot safely encode argument: ${params.names[i]} (${original}) of type ${ + params.types[i] + }. (Possible type overflow or other encoding error)`, + ); + } + } + return rawEncoded; + } constructor( contractName: string, abi: ContractAbi, -- cgit v1.2.3