aboutsummaryrefslogtreecommitdiffstats
path: root/packages/base-contract/src/index.ts
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-03-02 05:04:55 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-03-02 05:04:55 +0800
commit0c16f0ea221d65e66942c27a69dda1c54f49e775 (patch)
tree5530d18fcd6e6c5e15272de3cb915fc2025c01bf /packages/base-contract/src/index.ts
parent003d43b03ae373ca24d6d728ffa6b0f2dfcda79a (diff)
parent451a0dacbe85d7a0d16ef007c1eb945a4c1977cb (diff)
downloaddexon-sol-tools-0c16f0ea221d65e66942c27a69dda1c54f49e775.tar
dexon-sol-tools-0c16f0ea221d65e66942c27a69dda1c54f49e775.tar.gz
dexon-sol-tools-0c16f0ea221d65e66942c27a69dda1c54f49e775.tar.bz2
dexon-sol-tools-0c16f0ea221d65e66942c27a69dda1c54f49e775.tar.lz
dexon-sol-tools-0c16f0ea221d65e66942c27a69dda1c54f49e775.tar.xz
dexon-sol-tools-0c16f0ea221d65e66942c27a69dda1c54f49e775.tar.zst
dexon-sol-tools-0c16f0ea221d65e66942c27a69dda1c54f49e775.zip
Merge branch 'development' into feature/sra-reporter
* development: (71 commits) Set max to 2 ETH/2 ZRX Add missing types from website Add dependencies Update the README Move BaseContract to its own package Upgrate prettier remove unused import Move more configs into docsInfo and remove logic that does not belong there elsewhere Fix a bug with displaying solidity functions returning multiple return values Add ethers-contracts as a dependency Include types for ethers-contracts Fix the version Include types for ethers-contracts Rename idx to i Remove tslint disable Move BaseContract to web3Wrapper Merge ifs Fix an option description Add link to the docs Improve CHANGELOG entry ...
Diffstat (limited to 'packages/base-contract/src/index.ts')
-rw-r--r--packages/base-contract/src/index.ts68
1 files changed, 68 insertions, 0 deletions
diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts
new file mode 100644
index 000000000..cc1e16a13
--- /dev/null
+++ b/packages/base-contract/src/index.ts
@@ -0,0 +1,68 @@
+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 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 => 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> {
+ // Gas amount sourced with the following priorities:
+ // 1. Optional param passed in to public method call
+ // 2. Global config passed in at library instantiation
+ // 3. Gas estimate calculation + safety margin
+ const removeUndefinedProperties = _.pickBy;
+ const txDataWithDefaults = {
+ 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
+ };
+ if (_.isUndefined(txDataWithDefaults.gas) && !_.isUndefined(estimateGasAsync)) {
+ const estimatedGas = await estimateGasAsync(txData);
+ txDataWithDefaults.gas = estimatedGas;
+ }
+ return txDataWithDefaults;
+ }
+ constructor(web3Wrapper: Web3Wrapper, abi: Web3.ContractAbi, address: string) {
+ this._web3Wrapper = web3Wrapper;
+ this.abi = abi;
+ this.address = address;
+ this._ethersInterface = new ethersContracts.Interface(abi);
+ }
+}