aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-02-24 06:13:35 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-02-28 04:05:46 +0800
commit93b2736d6533f983aeb74005004de7f40eb35851 (patch)
tree51f25e54b7afeebc7fc482bf78f12a0f9bb1eda1 /packages/contracts/src
parent34274a10428bbc07a4f075bcf4a3ccafe0c04f5e (diff)
downloaddexon-sol-tools-93b2736d6533f983aeb74005004de7f40eb35851.tar
dexon-sol-tools-93b2736d6533f983aeb74005004de7f40eb35851.tar.gz
dexon-sol-tools-93b2736d6533f983aeb74005004de7f40eb35851.tar.bz2
dexon-sol-tools-93b2736d6533f983aeb74005004de7f40eb35851.tar.lz
dexon-sol-tools-93b2736d6533f983aeb74005004de7f40eb35851.tar.xz
dexon-sol-tools-93b2736d6533f983aeb74005004de7f40eb35851.tar.zst
dexon-sol-tools-93b2736d6533f983aeb74005004de7f40eb35851.zip
Use the same templates as 0x.js
Diffstat (limited to 'packages/contracts/src')
-rw-r--r--packages/contracts/src/contract_wrappers/generated/base_contract.ts51
1 files changed, 43 insertions, 8 deletions
diff --git a/packages/contracts/src/contract_wrappers/generated/base_contract.ts b/packages/contracts/src/contract_wrappers/generated/base_contract.ts
index d492bd325..a465ddc38 100644
--- a/packages/contracts/src/contract_wrappers/generated/base_contract.ts
+++ b/packages/contracts/src/contract_wrappers/generated/base_contract.ts
@@ -1,12 +1,45 @@
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 _ethersInterface: ethersContracts.Interface;
+ protected _web3Wrapper: Web3Wrapper;
+ public abi: Web3.ContractAbi;
public address: string;
- protected _web3ContractInstance: Web3.ContractInstance;
- protected _defaults: Partial<TxData>;
- protected async _applyDefaultsToTxDataAsync<T extends TxData | TxDataPayable>(
+ 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> {
@@ -16,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
@@ -27,9 +61,10 @@ export class BaseContract {
}
return txDataWithDefaults;
}
- constructor(web3ContractInstance: Web3.ContractInstance, defaults?: Partial<TxData>) {
- this.address = web3ContractInstance.address;
- 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);
}
}