aboutsummaryrefslogtreecommitdiffstats
path: root/packages/base-contract/src/index.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2019-01-15 22:13:24 +0800
committerFabio Berger <me@fabioberger.com>2019-01-15 22:13:24 +0800
commit7c37d10d1f93791db75b1622efa775a361965c49 (patch)
tree7017104bea27120661f71701552214dc5efba909 /packages/base-contract/src/index.ts
parentaffd6170540034c31593a4f36ce1dbacb91b6b3e (diff)
parent18084588ea9fa724d6e32c9a49c79d49f189ba7c (diff)
downloaddexon-sol-tools-7c37d10d1f93791db75b1622efa775a361965c49.tar
dexon-sol-tools-7c37d10d1f93791db75b1622efa775a361965c49.tar.gz
dexon-sol-tools-7c37d10d1f93791db75b1622efa775a361965c49.tar.bz2
dexon-sol-tools-7c37d10d1f93791db75b1622efa775a361965c49.tar.lz
dexon-sol-tools-7c37d10d1f93791db75b1622efa775a361965c49.tar.xz
dexon-sol-tools-7c37d10d1f93791db75b1622efa775a361965c49.tar.zst
dexon-sol-tools-7c37d10d1f93791db75b1622efa775a361965c49.zip
Merge branch 'development' into feature/monorepo-scripts/publishToDockerHub
* development: (87 commits) Update packages/sol-tracing-utils/src/trace_collection_subprovider.ts Make mapping namings direct Remove unused tslint disable Revert "Remove logAsyncErrors hack" Remove logAsyncErrors hack Refactor logAsyncErrors to follow our conventions Export Sources and SourceCodes out of tracing utils Replace console.log with logUtils.log (#1515) strict decoding of return values using generics makerAssetFillAmount -> takerAssetFillAmount Ran prettier Linter Fix build after rebase Style cleanup for Compressed Calldata in Contract Wrappers PR Use simpler `_.find` to locate fillOrderBai Updated dutch auction wrapper Added back abi-gen-wrappers Renamed signatureParser.ts to signature_parser.ts Renamed decode rule `structsAsObjects` to `shouldConvertStructsToObjects` circle build failed. New commit to resubmit job. ...
Diffstat (limited to 'packages/base-contract/src/index.ts')
-rw-r--r--packages/base-contract/src/index.ts55
1 files changed, 34 insertions, 21 deletions
diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts
index fbb8478b7..c2b7b943b 100644
--- a/packages/base-contract/src/index.ts
+++ b/packages/base-contract/src/index.ts
@@ -1,4 +1,4 @@
-import { abiUtils, BigNumber } from '@0x/utils';
+import { AbiEncoder, abiUtils } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import {
AbiDefinition,
@@ -16,8 +16,8 @@ import * as _ from 'lodash';
import { formatABIDataItem } from './utils';
-export interface EthersInterfaceByFunctionSignature {
- [key: string]: ethers.utils.Interface;
+export interface AbiEncoderByFunctionSignature {
+ [key: string]: AbiEncoder.Method;
}
const REVERT_ERROR_SELECTOR = '08c379a0';
@@ -26,7 +26,7 @@ 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 _abiEncoderByFunctionSignature: AbiEncoderByFunctionSignature;
protected _web3Wrapper: Web3Wrapper;
public abi: ContractAbi;
public address: string;
@@ -65,9 +65,6 @@ export class BaseContract {
return defaultConstructorAbi;
}
}
- protected static _bnToBigNumber(_type: string, value: any): any {
- return _.isObject(value) && value._hex ? new BigNumber(value.toString()) : value;
- }
protected static async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>(
txData: T,
txDefaults: Partial<TxData>,
@@ -89,10 +86,15 @@ 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'],
+ const revertReasonArray = AbiEncoder.create('(string)').decodeAsArray(
ethers.utils.hexDataSlice(rawCallResult, REVERT_ERROR_SELECTOR_BYTES_LENGTH),
);
+ if (revertReasonArray.length !== 1) {
+ throw new Error(
+ `Cannot safely decode revert reason: Expected an array with one element, got ${revertReasonArray}`,
+ );
+ }
+ const revertReason = revertReasonArray[0];
throw new Error(revertReason);
}
}
@@ -100,11 +102,11 @@ export class BaseContract {
// 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 = new ethers.utils.AbiCoder();
+ public static strictArgumentEncodingCheck(inputAbi: DataItem[], args: any[]): string {
+ const abiEncoder = AbiEncoder.create(inputAbi);
const params = abiUtils.parseEthersParams(inputAbi);
- const rawEncoded = coder.encode(inputAbi, args);
- const rawDecoded = coder.decode(inputAbi, rawEncoded);
+ const rawEncoded = abiEncoder.encode(args);
+ const rawDecoded = abiEncoder.decodeAsArray(rawEncoded);
for (let i = 0; i < rawDecoded.length; i++) {
const original = args[i];
const decoded = rawDecoded[i];
@@ -116,13 +118,14 @@ export class BaseContract {
);
}
}
+ return rawEncoded;
}
- protected _lookupEthersInterface(functionSignature: string): ethers.utils.Interface {
- const ethersInterface = this._ethersInterfacesByFunctionSignature[functionSignature];
- if (_.isUndefined(ethersInterface)) {
+ protected _lookupAbiEncoder(functionSignature: string): AbiEncoder.Method {
+ const abiEncoder = this._abiEncoderByFunctionSignature[functionSignature];
+ if (_.isUndefined(abiEncoder)) {
throw new Error(`Failed to lookup method with function signature '${functionSignature}'`);
}
- return ethersInterface;
+ return abiEncoder;
}
protected _lookupAbi(functionSignature: string): MethodAbi {
const methodAbi = _.find(this.abi, (abiDefinition: AbiDefinition) => {
@@ -130,7 +133,7 @@ export class BaseContract {
return false;
}
// tslint:disable-next-line:no-unnecessary-type-assertion
- const abiFunctionSignature = abiUtils.getFunctionSignature(abiDefinition as MethodAbi);
+ const abiFunctionSignature = new AbiEncoder.Method(abiDefinition as MethodAbi).getSignature();
if (abiFunctionSignature === functionSignature) {
return true;
}
@@ -138,6 +141,15 @@ 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 abiEncodedArguments = abiEncoder.encode(functionArguments);
+ return abiEncodedArguments;
+ }
constructor(
contractName: string,
abi: ContractAbi,
@@ -152,10 +164,11 @@ export class BaseContract {
const methodAbis = this.abi.filter(
(abiDefinition: AbiDefinition) => abiDefinition.type === AbiType.Function,
) as MethodAbi[];
- this._ethersInterfacesByFunctionSignature = {};
+ this._abiEncoderByFunctionSignature = {};
_.each(methodAbis, methodAbi => {
- const functionSignature = abiUtils.getFunctionSignature(methodAbi);
- this._ethersInterfacesByFunctionSignature[functionSignature] = new ethers.utils.Interface([methodAbi]);
+ const abiEncoder = new AbiEncoder.Method(methodAbi);
+ const functionSignature = abiEncoder.getSignature();
+ this._abiEncoderByFunctionSignature[functionSignature] = abiEncoder;
});
}
}