From 595656d3fc06a221274eb6bee89e7ba6e4e8ae61 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 31 May 2017 20:08:28 +0200 Subject: Move isValidOrderHash to utils and implement assert.isValidOrderHash --- src/0x.js.ts | 8 ++++---- src/utils/assert.ts | 4 ++++ src/utils/utils.ts | 4 ++++ 3 files changed, 12 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index d231c579e..ac00d3b42 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -102,10 +102,10 @@ export class ZeroEx { return salt; } /** Checks if order hash is valid */ - public static isValidOrderHash(orderHash: string): boolean { - assert.isString('orderHash', orderHash); - const isValid = /^0x[0-9A-F]{64}$/i.test(orderHash); - return isValid; + public static isValidOrderHash(orderHashHex: string): boolean { + assert.isString('orderHashHex', orderHashHex); + const isValidOrderHash = utils.isValidOrderHash(orderHashHex); + return isValidOrderHash; } /** * A unit amount is defined as the amount of a token above the specified decimal places (integer part). diff --git a/src/utils/assert.ts b/src/utils/assert.ts index 1baf572d1..0088b3b13 100644 --- a/src/utils/assert.ts +++ b/src/utils/assert.ts @@ -2,6 +2,7 @@ import * as _ from 'lodash'; import * as BigNumber from 'bignumber.js'; import * as Web3 from 'web3'; import {SchemaValidator} from './schema_validator'; +import {utils} from './utils'; const HEX_REGEX = /^0x[0-9A-F]*$/i; @@ -27,6 +28,9 @@ export const assert = { isNumber(variableName: string, value: number): void { this.assert(_.isFinite(value), this.typeAssertionMessage(variableName, 'number', value)); }, + isValidOrderHash(variableName: string, value: string): void { + this.assert(utils.isValidOrderHash(value), this.typeAssertionMessage(variableName, 'orderHash', value)); + }, doesConformToSchema(variableName: string, value: object, schema: Schema): void { const schemaValidator = new SchemaValidator(); const validationResult = schemaValidator.validate(value, schema); diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 336eaf7bb..e6840a624 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -18,4 +18,8 @@ export const utils = { isParityNode(nodeVersion: string): boolean { return _.includes(nodeVersion, 'Parity'); }, + isValidOrderHash(orderHashHex: string) { + const isValid = /^0x[0-9A-F]{64}$/i.test(orderHashHex); + return isValid; + }, }; -- cgit v1.2.3 From 52c3330487c9cb1e3fc78cda752717b684deca38 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 31 May 2017 20:10:09 +0200 Subject: Implement getUnavailableTakerAmountAsync, getFilledTakerAmountAsync and getCanceledTakerAmountAsync --- src/contract_wrappers/exchange_wrapper.ts | 33 +++++++++++++++++++++++++++++++ src/types.ts | 9 +++++++++ 2 files changed, 42 insertions(+) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 3f6eb0dab..74303cf82 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import * as BigNumber from 'bignumber.js'; import {Web3Wrapper} from '../web3_wrapper'; import {ECSignature, ZeroExError, ExchangeContract} from '../types'; import {assert} from '../utils/assert'; @@ -37,6 +38,38 @@ export class ExchangeWrapper extends ContractWrapper { ); return isValidSignature; } + /** + * Returns the unavailable takerAmount of an order. Unavailable amount is defined as the total + * amount that has been filled or cancelled. The remaining takerAmount can be calculated by + * subtracting the unavailable amount from the total order takerAmount. + */ + public async getUnavailableTakerAmountAsync(orderHashHex: string): Promise { + assert.isValidOrderHash('orderHashHex', orderHashHex); + + const exchangeContract = await this.getExchangeContractAsync(); + const unavailableAmountInBaseUnits = await exchangeContract.getUnavailableValueT.call(orderHashHex); + return unavailableAmountInBaseUnits; + } + /** + * Retrieve the takerAmount of an order that has already been filled. + */ + public async getFilledTakerAmountAsync(orderHashHex: string): Promise { + assert.isValidOrderHash('orderHashHex', orderHashHex); + + const exchangeContract = await this.getExchangeContractAsync(); + const fillAmountInBaseUnits = await exchangeContract.filled.call(orderHashHex); + return fillAmountInBaseUnits; + } + /** + * Retrieve the takerAmount of an order that has been cancelled. + */ + public async getCanceledTakerAmountAsync(orderHashHex: string): Promise { + assert.isValidOrderHash('orderHashHex', orderHashHex); + + const exchangeContract = await this.getExchangeContractAsync(); + const cancelledAmountInBaseUnits = await exchangeContract.cancelled.call(orderHashHex); + return cancelledAmountInBaseUnits; + } private async getExchangeContractAsync(): Promise { if (!_.isUndefined(this.exchangeContractIfExists)) { return this.exchangeContractIfExists; diff --git a/src/types.ts b/src/types.ts index 3619c800f..25338a531 100644 --- a/src/types.ts +++ b/src/types.ts @@ -29,6 +29,15 @@ export interface ECSignature { export interface ExchangeContract { isValidSignature: any; + getUnavailableValueT: { + call: (orderHash: string) => BigNumber.BigNumber; + }; + filled: { + call: (orderHash: string) => BigNumber.BigNumber; + }; + cancelled: { + call: (orderHash: string) => BigNumber.BigNumber; + }; } export interface TokenContract { -- cgit v1.2.3 From aade6891e834dda7e80d76740882facdb45bc014 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 18:35:23 +0200 Subject: fix tslint error --- src/contract_wrappers/exchange_wrapper.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 0822ef875..6f6380bb8 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -153,7 +153,8 @@ export class ExchangeWrapper extends ContractWrapper { ); this.throwErrorLogsAsErrors(response.logs); } - private async validateFillOrderAsync(signedOrder: SignedOrder, fillAmount: BigNumber.BigNumber, senderAddress: string) { + private async validateFillOrderAsync(signedOrder: SignedOrder, fillAmount: BigNumber.BigNumber, + senderAddress: string) { if (fillAmount.eq(0)) { throw new Error(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); } -- cgit v1.2.3 From 293ce6f4b2e495589ae3675eb293fb9948f22dbb Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 09:39:05 +0200 Subject: Always wrap BigNumbers returned by web3 with our own version and add comment --- src/contract_wrappers/exchange_wrapper.ts | 12 +++++++++--- src/contract_wrappers/token_wrapper.ts | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 8b20d2b4d..2894c5a9f 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -70,7 +70,9 @@ export class ExchangeWrapper extends ContractWrapper { assert.isValidOrderHash('orderHashHex', orderHashHex); const exchangeContract = await this.getExchangeContractAsync(); - const unavailableAmountInBaseUnits = await exchangeContract.getUnavailableValueT.call(orderHashHex); + let unavailableAmountInBaseUnits = await exchangeContract.getUnavailableValueT.call(orderHashHex); + // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber + unavailableAmountInBaseUnits = new BigNumber(unavailableAmountInBaseUnits); return unavailableAmountInBaseUnits; } /** @@ -80,7 +82,9 @@ export class ExchangeWrapper extends ContractWrapper { assert.isValidOrderHash('orderHashHex', orderHashHex); const exchangeContract = await this.getExchangeContractAsync(); - const fillAmountInBaseUnits = await exchangeContract.filled.call(orderHashHex); + let fillAmountInBaseUnits = await exchangeContract.filled.call(orderHashHex); + // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber + fillAmountInBaseUnits = new BigNumber(fillAmountInBaseUnits); return fillAmountInBaseUnits; } /** @@ -90,7 +94,9 @@ export class ExchangeWrapper extends ContractWrapper { assert.isValidOrderHash('orderHashHex', orderHashHex); const exchangeContract = await this.getExchangeContractAsync(); - const cancelledAmountInBaseUnits = await exchangeContract.cancelled.call(orderHashHex); + let cancelledAmountInBaseUnits = await exchangeContract.cancelled.call(orderHashHex); + // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber + cancelledAmountInBaseUnits = new BigNumber(cancelledAmountInBaseUnits); return cancelledAmountInBaseUnits; } /** diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index cedbfbdae..69bcc9024 100644 --- a/src/contract_wrappers/token_wrapper.ts +++ b/src/contract_wrappers/token_wrapper.ts @@ -28,8 +28,7 @@ export class TokenWrapper extends ContractWrapper { const tokenContract = await this.getTokenContractAsync(tokenAddress); let balance = await tokenContract.balanceOf.call(ownerAddress); - // The BigNumber instance returned by Web3 is of a much older version then our own, we therefore - // should always re-instantiate the returned BigNumber after retrieval. + // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber balance = new BigNumber(balance); return balance; } @@ -44,6 +43,7 @@ export class TokenWrapper extends ContractWrapper { const tokenContract = await this.getTokenContractAsync(tokenAddress); const proxyAddress = await this.getProxyAddressAsync(); let allowanceInBaseUnits = await tokenContract.allowance.call(ownerAddress, proxyAddress); + // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber allowanceInBaseUnits = new BigNumber(allowanceInBaseUnits); return allowanceInBaseUnits; } -- cgit v1.2.3 From 707a5f55eb919b3460ae4ee7d58b9a0eaa56a07c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 09:43:03 +0200 Subject: Write tests for getUnavailableTakerAmountAsync, getFilledTakerAmountAsync and getCanceledTakerAmountAsync --- src/0x.js.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index 2bced325f..b42d5dee1 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -17,7 +17,7 @@ import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper'; import {ecSignatureSchema} from './schemas/ec_signature_schema'; import {TokenWrapper} from './contract_wrappers/token_wrapper'; import {SolidityTypes, ECSignature, ZeroExError} from './types'; -import {Order} from './types'; +import {Order, SignedOrder} from './types'; import {orderSchema} from './schemas/order_schemas'; import * as ExchangeArtifacts from './artifacts/Exchange.json'; @@ -116,17 +116,23 @@ export class ZeroEx { this.tokenRegistry.invalidateContractInstance(); this.token.invalidateContractInstances(); } - /** * Sets default account for sending transactions. */ public setTransactionSenderAccount(account: string): void { this.web3Wrapper.setDefaultAccount(account); } + /** + * Get the default account set for sending transactions. + */ + public async getTransactionSenderAccountAsync(): Promise { + const senderAccount = await this.web3Wrapper.getSenderAddressOrThrowAsync(); + return senderAccount; + } /** * Computes the orderHash given the order parameters and returns it as a hex encoded string. */ - public async getOrderHashHexAsync(order: Order): Promise { + public async getOrderHashHexAsync(order: Order|SignedOrder): Promise { const exchangeContractAddr = await this.getExchangeAddressAsync(); assert.doesConformToSchema('order', SchemaValidator.convertToJSONSchemaCompatibleObject(order as object), -- cgit v1.2.3 From 2cf5208f85bca11a507a5b0e2202f5f6eb531753 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 09:49:50 +0200 Subject: Add comment about the isString assertion lest the next developer tries to use a stricter assertion (i.e isHexString) which we intentionally did not do --- src/0x.js.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index b42d5dee1..666fd00c6 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -71,6 +71,8 @@ export class ZeroEx { } /** Checks if order hash is valid */ public static isValidOrderHash(orderHashHex: string): boolean { + // Since this method can be called to check if any arbitrary string conforms to an orderHash's + // format, we only assert that we were indeed passed a string. assert.isString('orderHashHex', orderHashHex); const isValidOrderHash = utils.isValidOrderHash(orderHashHex); return isValidOrderHash; -- cgit v1.2.3 From 114b5ea0fe78b9b9b5b4fc46e13b21e45f77a628 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 2 Jun 2017 09:59:10 +0200 Subject: improve comment --- src/0x.js.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/0x.js.ts b/src/0x.js.ts index 666fd00c6..0e9d71586 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -69,7 +69,10 @@ export class ZeroEx { const salt = randomNumber.times(factor).round(); return salt; } - /** Checks if order hash is valid */ + /** + * Checks if the supplied hex encoded order hash is valid. + * Note: Valid means it has the expected format, not that an order with the orderHash exists. + */ public static isValidOrderHash(orderHashHex: string): boolean { // Since this method can be called to check if any arbitrary string conforms to an orderHash's // format, we only assert that we were indeed passed a string. -- cgit v1.2.3