diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-24 03:35:44 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-28 04:05:23 +0800 |
commit | 2d561bc8a05e8d1fca91cde93bae2080d87be926 (patch) | |
tree | 10d785eff73c67ecae09a337ed3721ff6b6102a9 /packages/0x.js/src/contract_wrappers/generated/base_contract.ts | |
parent | f5275d3ad75d2a989556de99cdef82bcf2cd687c (diff) | |
download | dexon-sol-tools-2d561bc8a05e8d1fca91cde93bae2080d87be926.tar.gz dexon-sol-tools-2d561bc8a05e8d1fca91cde93bae2080d87be926.tar.zst dexon-sol-tools-2d561bc8a05e8d1fca91cde93bae2080d87be926.zip |
Allow users to specify the contracts backend in abi-gen
Diffstat (limited to 'packages/0x.js/src/contract_wrappers/generated/base_contract.ts')
-rw-r--r-- | packages/0x.js/src/contract_wrappers/generated/base_contract.ts | 53 |
1 files changed, 45 insertions, 8 deletions
diff --git a/packages/0x.js/src/contract_wrappers/generated/base_contract.ts b/packages/0x.js/src/contract_wrappers/generated/base_contract.ts index d8fac7eea..a62c785bc 100644 --- a/packages/0x.js/src/contract_wrappers/generated/base_contract.ts +++ b/packages/0x.js/src/contract_wrappers/generated/base_contract.ts @@ -1,11 +1,45 @@ -import {TxData, TxDataPayable} from '@0xproject/types'; +import { TxData, TxDataPayable } from '@0xproject/types'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as ethersContracts from 'ethers-contracts'; import * as _ from 'lodash'; import * as Web3 from 'web3'; export class BaseContract { - protected _web3ContractInstance: Web3.ContractInstance; - protected _defaults: Partial<TxData>; - protected async _applyDefaultsToTxDataAsync<T extends TxData|TxDataPayable>( + protected _ethersInterface: ethersContracts.Interface; + protected _web3Wrapper: Web3Wrapper; + protected _abi: Web3.ContractAbi; + protected _address: string; + protected static _transformABIData( + abis: Web3.DataItem[], + values: any[], + transformation: (type: string, value: any) => any, + ): any { + return _.map(values, (value: any, i: number) => + BaseContract._transformTypedData(abis[i].type, value, transformation), + ); + } + protected static _lowercaseAddress(type: string, value: string): string { + return type === 'address' ? value.toLowerCase() : value; + } + protected static _bigNumberToString(type: string, value: string): string { + return _.isObject(value) && (value as any).isBigNumber ? value.toString() : value; + } + private static _transformTypedData( + type: string, + values: any, + transformation: (type: string, value: any) => any, + ): any { + const trailingArrayRegex = /\[\d*\]$/; + if (type.match(trailingArrayRegex)) { + const arrayItemType = type.replace(trailingArrayRegex, ''); + return _.map(values, (value: any, i: number) => + this._transformTypedData(arrayItemType, value, transformation), + ); + } else { + return transformation(type, values); + } + } + protected async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>( txData: T, estimateGasAsync?: (txData: T) => Promise<number>, ): Promise<TxData> { @@ -15,7 +49,8 @@ export class BaseContract { // 3. Gas estimate calculation + safety margin const removeUndefinedProperties = _.pickBy; const txDataWithDefaults = { - ...removeUndefinedProperties(this._defaults), + to: this._address, + ...removeUndefinedProperties(this._web3Wrapper.getContractDefaults()), ...removeUndefinedProperties(txData as any), // HACK: TS can't prove that T is spreadable. // Awaiting https://github.com/Microsoft/TypeScript/pull/13288 to be merged @@ -26,8 +61,10 @@ export class BaseContract { } return txDataWithDefaults; } - constructor(web3ContractInstance: Web3.ContractInstance, defaults: Partial<TxData>) { - this._web3ContractInstance = web3ContractInstance; - this._defaults = defaults; + constructor(web3Wrapper: Web3Wrapper, abi: Web3.ContractAbi, address: string) { + this._web3Wrapper = web3Wrapper; + this._abi = abi; + this._address = address; + this._ethersInterface = new ethersContracts.Interface(abi); } } |