aboutsummaryrefslogtreecommitdiffstats
path: root/src/ts/contract_wrappers/contract_wrapper.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-05-27 00:54:56 +0800
committerGitHub <noreply@github.com>2017-05-27 00:54:56 +0800
commitb897bdab79fe566ffc8c19c6ec9f1bb7260fa95e (patch)
tree65abedae4adfa1694db877a8e041e57c29a547f9 /src/ts/contract_wrappers/contract_wrapper.ts
parentf338c68f126cba0f1b49c2928f276158b64d8ee7 (diff)
parent5d4c2dcb2950747c7cb2d95df340c23c981d70d3 (diff)
downloaddexon-0x-contracts-b897bdab79fe566ffc8c19c6ec9f1bb7260fa95e.tar
dexon-0x-contracts-b897bdab79fe566ffc8c19c6ec9f1bb7260fa95e.tar.gz
dexon-0x-contracts-b897bdab79fe566ffc8c19c6ec9f1bb7260fa95e.tar.bz2
dexon-0x-contracts-b897bdab79fe566ffc8c19c6ec9f1bb7260fa95e.tar.lz
dexon-0x-contracts-b897bdab79fe566ffc8c19c6ec9f1bb7260fa95e.tar.xz
dexon-0x-contracts-b897bdab79fe566ffc8c19c6ec9f1bb7260fa95e.tar.zst
dexon-0x-contracts-b897bdab79fe566ffc8c19c6ec9f1bb7260fa95e.zip
Merge pull request #14 from 0xProject/implementFirstExchangeMethod
Implement first exchange method
Diffstat (limited to 'src/ts/contract_wrappers/contract_wrapper.ts')
-rw-r--r--src/ts/contract_wrappers/contract_wrapper.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/ts/contract_wrappers/contract_wrapper.ts b/src/ts/contract_wrappers/contract_wrapper.ts
new file mode 100644
index 000000000..9f4cd8039
--- /dev/null
+++ b/src/ts/contract_wrappers/contract_wrapper.ts
@@ -0,0 +1,48 @@
+import * as _ from 'lodash';
+import contract = require('truffle-contract');
+import {Web3Wrapper} from '../web3_wrapper';
+import {ZeroExError} from '../types';
+import {utils} from '../utils/utils';
+
+export class ContractWrapper {
+ public web3Wrapper: Web3Wrapper;
+ constructor(web3Wrapper: Web3Wrapper) {
+ this.web3Wrapper = web3Wrapper;
+ }
+ protected async instantiateContractIfExistsAsync(artifact: Artifact, address?: string): Promise<ContractInstance> {
+ const c = await contract(artifact);
+ const providerObj = this.web3Wrapper.getCurrentProvider();
+ c.setProvider(providerObj);
+
+ const networkIdIfExists = await this.web3Wrapper.getNetworkIdIfExistsAsync();
+ const artifactNetworkConfigs = _.isUndefined(networkIdIfExists) ?
+ undefined :
+ artifact.networks[networkIdIfExists];
+ let contractAddress;
+ if (!_.isUndefined(address)) {
+ contractAddress = address;
+ } else if (!_.isUndefined(artifactNetworkConfigs)) {
+ contractAddress = artifactNetworkConfigs.address;
+ }
+
+ if (!_.isUndefined(contractAddress)) {
+ const doesContractExist = await this.web3Wrapper.doesContractExistAtAddressAsync(contractAddress);
+ if (!doesContractExist) {
+ throw new Error(ZeroExError.CONTRACT_DOES_NOT_EXIST);
+ }
+ }
+
+ try {
+ const contractInstance = _.isUndefined(address) ? await c.deployed() : await c.at(address);
+ return contractInstance;
+ } catch (err) {
+ const errMsg = `${err}`;
+ if (_.includes(errMsg, 'not been deployed to detected network')) {
+ throw new Error(ZeroExError.CONTRACT_DOES_NOT_EXIST);
+ } else {
+ utils.consoleLog(`Notice: Error encountered: ${err} ${err.stack}`);
+ throw new Error(ZeroExError.UNHANDLED_ERROR);
+ }
+ }
+ }
+}