From bc8fc534332b5ea82f881bdd3a75773384714f4d Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 25 May 2017 19:47:11 +0200 Subject: Add initial exchange contract function, set up web3Wrapper, added types and utils --- src/ts/contract_wrappers/contract_wrapper.ts | 51 ++++++++++++++++++++++++++++ src/ts/contract_wrappers/exchange_wrapper.ts | 37 ++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/ts/contract_wrappers/contract_wrapper.ts create mode 100644 src/ts/contract_wrappers/exchange_wrapper.ts (limited to 'src/ts/contract_wrappers') diff --git a/src/ts/contract_wrappers/contract_wrapper.ts b/src/ts/contract_wrappers/contract_wrapper.ts new file mode 100644 index 000000000..72bfffe95 --- /dev/null +++ b/src/ts/contract_wrappers/contract_wrapper.ts @@ -0,0 +1,51 @@ +import * as _ from 'lodash'; +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; + } + // this.exchange = await this.instantiateContractIfExistsAsync(ExchangeArtifacts); + protected async instantiateContractIfExistsAsync(artifact: Artifact, address?: string): Promise { + const c = await contract(artifact); + const providerObj = this.web3Wrapper.getCurrentProvider(); + c.setProvider(providerObj); + + const networkId = await this.web3Wrapper.getNetworkIdIfExistsAsync(); + const artifactNetworkConfigs = _.isUndefined(networkId) ? undefined : artifact.networks[networkId]; + 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 { + let contractInstance; + if (_.isUndefined(address)) { + contractInstance = await c.deployed(); + } else { + contractInstance = await c.at(address); + } + return contractInstance; + } catch (err) { + const errMsg = `${err}`; + utils.consoleLog(`Notice: Error encountered: ${err} ${err.stack}`); + if (_.includes(errMsg, 'not been deployed to detected network')) { + throw new Error(ZeroExError.CONTRACT_DOES_NOT_EXIST); + } else { + throw new Error(ZeroExError.UNHANDLED_ERROR); + } + } + } +} diff --git a/src/ts/contract_wrappers/exchange_wrapper.ts b/src/ts/contract_wrappers/exchange_wrapper.ts new file mode 100644 index 000000000..502a1089b --- /dev/null +++ b/src/ts/contract_wrappers/exchange_wrapper.ts @@ -0,0 +1,37 @@ +import * as _ from 'lodash'; +import {Web3Wrapper} from '../web3_wrapper'; +import {ECSignature, ZeroExError, ExchangeContract} from '../types'; +import {assert} from '../utils/assert'; +import {ContractWrapper} from './contract_wrapper'; +import * as ExchangeArtifacts from '../artifacts/Exchange.json'; +import {ECSignatureSchema} from '../schemas/ec_signature_schema'; + +export class ExchangeWrapper extends ContractWrapper { + constructor(web3Wrapper: Web3Wrapper) { + super(web3Wrapper); + } + public async isValidSignatureAsync(maker: string, ecSignature: ECSignature, dataHex: string) { + assert.isString('maker', maker); + assert.doesConformToSchema('ecSignature', ecSignature, ECSignatureSchema); + assert.isHexString('dataHex', dataHex); + + const senderAddressIfExists = this.web3Wrapper.getSenderAddressIfExistsAsync(); + assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES); + + // TODO: remove any here + const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); + const exchangeInstance = contractInstance as ExchangeContract; + + const isValidSignature = await exchangeInstance.isValidSignature.call( + maker, + dataHex, + ecSignature.v, + ecSignature.r, + ecSignature.s, + { + from: senderAddressIfExists, + }, + ); + return isValidSignature; + } +} -- cgit v1.2.3 From f75595c6cd2a3edebca37d7d0063538116844074 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 May 2017 20:12:55 +0200 Subject: Change the order of params in isValidSignatureAsync to be consistent --- src/ts/contract_wrappers/exchange_wrapper.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/ts/contract_wrappers') diff --git a/src/ts/contract_wrappers/exchange_wrapper.ts b/src/ts/contract_wrappers/exchange_wrapper.ts index 502a1089b..5a576a7fa 100644 --- a/src/ts/contract_wrappers/exchange_wrapper.ts +++ b/src/ts/contract_wrappers/exchange_wrapper.ts @@ -3,17 +3,17 @@ import {Web3Wrapper} from '../web3_wrapper'; import {ECSignature, ZeroExError, ExchangeContract} from '../types'; import {assert} from '../utils/assert'; import {ContractWrapper} from './contract_wrapper'; -import * as ExchangeArtifacts from '../artifacts/Exchange.json'; +import * as ExchangeArtifacts from '../../artifacts/Exchange.json'; import {ECSignatureSchema} from '../schemas/ec_signature_schema'; export class ExchangeWrapper extends ContractWrapper { constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); } - public async isValidSignatureAsync(maker: string, ecSignature: ECSignature, dataHex: string) { - assert.isString('maker', maker); - assert.doesConformToSchema('ecSignature', ecSignature, ECSignatureSchema); + public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, maker: string) { assert.isHexString('dataHex', dataHex); + assert.doesConformToSchema('ecSignature', ecSignature, ECSignatureSchema); + assert.isString('maker', maker); const senderAddressIfExists = this.web3Wrapper.getSenderAddressIfExistsAsync(); assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES); -- cgit v1.2.3 From 123cfb7c00bcfba0a24e2ac4593834b134a76806 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 26 May 2017 17:09:47 +0200 Subject: Add missing require --- src/ts/contract_wrappers/contract_wrapper.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src/ts/contract_wrappers') diff --git a/src/ts/contract_wrappers/contract_wrapper.ts b/src/ts/contract_wrappers/contract_wrapper.ts index 72bfffe95..aeb52b0e1 100644 --- a/src/ts/contract_wrappers/contract_wrapper.ts +++ b/src/ts/contract_wrappers/contract_wrapper.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import contract = require('truffle-contract'); import {Web3Wrapper} from '../web3_wrapper'; import {ZeroExError} from '../types'; import {utils} from '../utils/utils'; -- cgit v1.2.3 From 5a6df03b64630b4ccfe1a211a113413b9f1d4e5c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 26 May 2017 17:10:02 +0200 Subject: cleanup isValidSignature method --- src/ts/contract_wrappers/exchange_wrapper.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/ts/contract_wrappers') diff --git a/src/ts/contract_wrappers/exchange_wrapper.ts b/src/ts/contract_wrappers/exchange_wrapper.ts index 5a576a7fa..b02390eba 100644 --- a/src/ts/contract_wrappers/exchange_wrapper.ts +++ b/src/ts/contract_wrappers/exchange_wrapper.ts @@ -10,20 +10,21 @@ export class ExchangeWrapper extends ContractWrapper { constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); } - public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, maker: string) { + public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, + signerAddressHex: string): Promise { assert.isHexString('dataHex', dataHex); assert.doesConformToSchema('ecSignature', ecSignature, ECSignatureSchema); - assert.isString('maker', maker); + assert.isETHAddressHex('signerAddressHex', signerAddressHex); - const senderAddressIfExists = this.web3Wrapper.getSenderAddressIfExistsAsync(); + const senderAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync(); assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES); - // TODO: remove any here + // TODO: remove `any` here const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); const exchangeInstance = contractInstance as ExchangeContract; const isValidSignature = await exchangeInstance.isValidSignature.call( - maker, + signerAddressHex, dataHex, ecSignature.v, ecSignature.r, -- cgit v1.2.3 From a4ec739ce685989dde01c38cf45f4c21026f3c20 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 26 May 2017 17:40:35 +0200 Subject: remove comment --- src/ts/contract_wrappers/contract_wrapper.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'src/ts/contract_wrappers') diff --git a/src/ts/contract_wrappers/contract_wrapper.ts b/src/ts/contract_wrappers/contract_wrapper.ts index aeb52b0e1..c3495fa21 100644 --- a/src/ts/contract_wrappers/contract_wrapper.ts +++ b/src/ts/contract_wrappers/contract_wrapper.ts @@ -9,7 +9,6 @@ export class ContractWrapper { constructor(web3Wrapper: Web3Wrapper) { this.web3Wrapper = web3Wrapper; } - // this.exchange = await this.instantiateContractIfExistsAsync(ExchangeArtifacts); protected async instantiateContractIfExistsAsync(artifact: Artifact, address?: string): Promise { const c = await contract(artifact); const providerObj = this.web3Wrapper.getCurrentProvider(); -- cgit v1.2.3 From 066c7ff0aa5c2cd657ae40b72bf1268119a86255 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 26 May 2017 17:40:51 +0200 Subject: rename networkId to networkIdIfExists --- src/ts/contract_wrappers/contract_wrapper.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/ts/contract_wrappers') diff --git a/src/ts/contract_wrappers/contract_wrapper.ts b/src/ts/contract_wrappers/contract_wrapper.ts index c3495fa21..ee1c6c90d 100644 --- a/src/ts/contract_wrappers/contract_wrapper.ts +++ b/src/ts/contract_wrappers/contract_wrapper.ts @@ -14,8 +14,10 @@ export class ContractWrapper { const providerObj = this.web3Wrapper.getCurrentProvider(); c.setProvider(providerObj); - const networkId = await this.web3Wrapper.getNetworkIdIfExistsAsync(); - const artifactNetworkConfigs = _.isUndefined(networkId) ? undefined : artifact.networks[networkId]; + const networkIdIfExists = await this.web3Wrapper.getNetworkIdIfExistsAsync(); + const artifactNetworkConfigs = _.isUndefined(networkIdIfExists) ? + undefined : + artifact.networks[networkIdIfExists]; let contractAddress; if (!_.isUndefined(address)) { contractAddress = address; -- cgit v1.2.3 From 79249f5e8a37e139c9297a94479f269bf61514af Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 26 May 2017 17:41:08 +0200 Subject: move console log into UNHANDLED_ERROR clause --- src/ts/contract_wrappers/contract_wrapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ts/contract_wrappers') diff --git a/src/ts/contract_wrappers/contract_wrapper.ts b/src/ts/contract_wrappers/contract_wrapper.ts index ee1c6c90d..225cb960f 100644 --- a/src/ts/contract_wrappers/contract_wrapper.ts +++ b/src/ts/contract_wrappers/contract_wrapper.ts @@ -42,10 +42,10 @@ export class ContractWrapper { return contractInstance; } catch (err) { const errMsg = `${err}`; - utils.consoleLog(`Notice: Error encountered: ${err} ${err.stack}`); 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); } } -- cgit v1.2.3 From 90db58bb2adfd09ee672420f625f906e761d722e Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 26 May 2017 18:20:34 +0200 Subject: remove todo --- src/ts/contract_wrappers/exchange_wrapper.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'src/ts/contract_wrappers') diff --git a/src/ts/contract_wrappers/exchange_wrapper.ts b/src/ts/contract_wrappers/exchange_wrapper.ts index b02390eba..38043dd55 100644 --- a/src/ts/contract_wrappers/exchange_wrapper.ts +++ b/src/ts/contract_wrappers/exchange_wrapper.ts @@ -19,7 +19,6 @@ export class ExchangeWrapper extends ContractWrapper { const senderAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync(); assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES); - // TODO: remove `any` here const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); const exchangeInstance = contractInstance as ExchangeContract; -- cgit v1.2.3 From a34d083cad3a07965bcb1687dd6534a30d79b7d0 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 26 May 2017 18:35:35 +0200 Subject: Refactor to ternary --- src/ts/contract_wrappers/contract_wrapper.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src/ts/contract_wrappers') diff --git a/src/ts/contract_wrappers/contract_wrapper.ts b/src/ts/contract_wrappers/contract_wrapper.ts index 225cb960f..9f4cd8039 100644 --- a/src/ts/contract_wrappers/contract_wrapper.ts +++ b/src/ts/contract_wrappers/contract_wrapper.ts @@ -33,12 +33,7 @@ export class ContractWrapper { } try { - let contractInstance; - if (_.isUndefined(address)) { - contractInstance = await c.deployed(); - } else { - contractInstance = await c.at(address); - } + const contractInstance = _.isUndefined(address) ? await c.deployed() : await c.at(address); return contractInstance; } catch (err) { const errMsg = `${err}`; -- cgit v1.2.3