aboutsummaryrefslogtreecommitdiffstats
path: root/packages/base-contract
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base-contract')
-rw-r--r--packages/base-contract/CHANGELOG.json3
-rw-r--r--packages/base-contract/CHANGELOG.md4
-rw-r--r--packages/base-contract/package.json14
-rw-r--r--packages/base-contract/src/index.ts48
4 files changed, 57 insertions, 12 deletions
diff --git a/packages/base-contract/CHANGELOG.json b/packages/base-contract/CHANGELOG.json
index da957ec83..ede16cdc4 100644
--- a/packages/base-contract/CHANGELOG.json
+++ b/packages/base-contract/CHANGELOG.json
@@ -6,7 +6,8 @@
"note": "Contract wrappers now accept Provider and defaults instead of Web3Wrapper",
"pr": 501
}
- ]
+ ],
+ "timestamp": 1523462196
},
{
"version": "0.1.0",
diff --git a/packages/base-contract/CHANGELOG.md b/packages/base-contract/CHANGELOG.md
index aebb0a5d9..851df7b62 100644
--- a/packages/base-contract/CHANGELOG.md
+++ b/packages/base-contract/CHANGELOG.md
@@ -5,6 +5,10 @@ Edit the package's CHANGELOG.json file only.
CHANGELOG
+## v0.2.0 - _April 11, 2018_
+
+ * Contract wrappers now accept Provider and defaults instead of Web3Wrapper (#501)
+
## v0.1.0 - _April 2, 2018_
* Add tests for traversing ABI tree (#485)
diff --git a/packages/base-contract/package.json b/packages/base-contract/package.json
index 449986119..5e14b7de5 100644
--- a/packages/base-contract/package.json
+++ b/packages/base-contract/package.json
@@ -1,6 +1,6 @@
{
"name": "@0xproject/base-contract",
- "version": "0.1.0",
+ "version": "0.2.0",
"description": "0x Base TS contract",
"main": "lib/src/index.js",
"types": "lib/src/index.d.ts",
@@ -26,8 +26,8 @@
},
"homepage": "https://github.com/0xProject/0x-monorepo/packages/base-contract/README.md",
"devDependencies": {
- "@0xproject/monorepo-scripts": "^0.1.16",
- "@0xproject/tslint-config": "^0.4.14",
+ "@0xproject/monorepo-scripts": "^0.1.17",
+ "@0xproject/tslint-config": "^0.4.15",
"@types/lodash": "4.14.104",
"chai": "^4.0.1",
"copyfiles": "^1.2.0",
@@ -38,10 +38,10 @@
"typescript": "2.7.1"
},
"dependencies": {
- "@0xproject/types": "^0.5.0",
- "@0xproject/typescript-typings": "^0.0.3",
- "@0xproject/utils": "^0.5.0",
- "@0xproject/web3-wrapper": "^0.5.0",
+ "@0xproject/types": "^0.6.0",
+ "@0xproject/typescript-typings": "^0.1.0",
+ "@0xproject/utils": "^0.5.1",
+ "@0xproject/web3-wrapper": "^0.6.0",
"ethers-contracts": "^2.2.1",
"lodash": "^4.17.4"
},
diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts
index bba686f8b..bfa99fac1 100644
--- a/packages/base-contract/src/index.ts
+++ b/packages/base-contract/src/index.ts
@@ -1,13 +1,26 @@
-import { ContractAbi, DataItem, Provider, TxData, TxDataPayable } from '@0xproject/types';
-import { BigNumber } from '@0xproject/utils';
+import {
+ AbiDefinition,
+ AbiType,
+ ContractAbi,
+ DataItem,
+ MethodAbi,
+ Provider,
+ TxData,
+ TxDataPayable,
+} from '@0xproject/types';
+import { abiUtils, BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as ethersContracts from 'ethers-contracts';
import * as _ from 'lodash';
import { formatABIDataItem } from './utils';
+export interface EthersInterfaceByFunctionSignature {
+ [key: string]: ethersContracts.Interface;
+}
+
export class BaseContract {
- protected _ethersInterface: ethersContracts.Interface;
+ protected _ethersInterfacesByFunctionSignature: EthersInterfaceByFunctionSignature;
protected _web3Wrapper: Web3Wrapper;
public abi: ContractAbi;
public address: string;
@@ -49,10 +62,37 @@ export class BaseContract {
}
return txDataWithDefaults;
}
+ protected _lookupEthersInterface(functionSignature: string): ethersContracts.Interface {
+ const ethersInterface = this._ethersInterfacesByFunctionSignature[functionSignature];
+ if (_.isUndefined(ethersInterface)) {
+ throw new Error(`Failed to lookup method with function signature '${functionSignature}'`);
+ }
+ return ethersInterface;
+ }
+ protected _lookupAbi(functionSignature: string): MethodAbi {
+ const methodAbi = _.find(this.abi, (abiDefinition: AbiDefinition) => {
+ if (abiDefinition.type !== AbiType.Function) {
+ return false;
+ }
+ const abiFunctionSignature = abiUtils.getFunctionSignature(abiDefinition);
+ if (abiFunctionSignature === functionSignature) {
+ return true;
+ }
+ return false;
+ }) as MethodAbi;
+ return methodAbi;
+ }
constructor(abi: ContractAbi, address: string, provider: Provider, defaults?: Partial<TxData>) {
this._web3Wrapper = new Web3Wrapper(provider, defaults);
this.abi = abi;
this.address = address;
- this._ethersInterface = new ethersContracts.Interface(abi);
+ const methodAbis = this.abi.filter(
+ (abiDefinition: AbiDefinition) => abiDefinition.type === AbiType.Function,
+ ) as MethodAbi[];
+ this._ethersInterfacesByFunctionSignature = {};
+ _.each(methodAbis, methodAbi => {
+ const functionSignature = abiUtils.getFunctionSignature(methodAbi);
+ this._ethersInterfacesByFunctionSignature[functionSignature] = new ethersContracts.Interface([methodAbi]);
+ });
}
}