diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/0x.js.ts | 19 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 39 | ||||
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 4 | ||||
-rw-r--r-- | src/types.ts | 9 | ||||
-rw-r--r-- | src/utils/assert.ts | 4 | ||||
-rw-r--r-- | src/utils/utils.ts | 4 |
6 files changed, 70 insertions, 9 deletions
diff --git a/src/0x.js.ts b/src/0x.js.ts index 7cf313666..40290467a 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'; @@ -69,11 +69,16 @@ export class ZeroEx { const salt = randomNumber.times(factor).round(); 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; + /** + * 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. + 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). @@ -132,7 +137,7 @@ export class ZeroEx { /** * Computes the orderHash for a given order and returns it as a hex encoded string. */ - public async getOrderHashHexAsync(order: Order): Promise<string> { + public async getOrderHashHexAsync(order: Order|SignedOrder): Promise<string> { const exchangeContractAddr = await this.getExchangeAddressAsync(); assert.doesConformToSchema('order', SchemaValidator.convertToJSONSchemaCompatibleObject(order as object), diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 4aa532bdd..cb869b498 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, @@ -60,6 +61,44 @@ 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<BigNumber.BigNumber> { + assert.isValidOrderHash('orderHashHex', orderHashHex); + + const exchangeContract = await this.getExchangeContractAsync(); + 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; + } + /** + * Retrieve the takerAmount of an order that has already been filled. + */ + public async getFilledTakerAmountAsync(orderHashHex: string): Promise<BigNumber.BigNumber> { + assert.isValidOrderHash('orderHashHex', orderHashHex); + + const exchangeContract = await this.getExchangeContractAsync(); + 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; + } + /** + * Retrieve the takerAmount of an order that has been cancelled. + */ + public async getCanceledTakerAmountAsync(orderHashHex: string): Promise<BigNumber.BigNumber> { + assert.isValidOrderHash('orderHashHex', orderHashHex); + + const exchangeContract = await this.getExchangeContractAsync(); + 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; + } + /** * Fills a signed order with a fillAmount denominated in baseUnits of the taker token. * Since the order in which transactions are included in the next block is indeterminate, race-conditions * could arise where a users balance or allowance changes before the fillOrder executes. Because of this, diff --git a/src/contract_wrappers/token_wrapper.ts b/src/contract_wrappers/token_wrapper.ts index 8bec39a52..c8b557d0d 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; } @@ -64,6 +63,7 @@ export class TokenWrapper extends ContractWrapper { const tokenContract = await this.getTokenContractAsync(tokenAddress); let allowanceInBaseUnits = await tokenContract.allowance.call(ownerAddress, spenderAddress); + // Wrap BigNumbers returned from web3 with our own (later) version of BigNumber allowanceInBaseUnits = new BigNumber(allowanceInBaseUnits); return allowanceInBaseUnits; } diff --git a/src/types.ts b/src/types.ts index d17dadf29..9d3127325 100644 --- a/src/types.ts +++ b/src/types.ts @@ -37,6 +37,9 @@ export type OrderValues = [BigNumber.BigNumber, BigNumber.BigNumber, BigNumber.B export interface ExchangeContract { isValidSignature: any; + getUnavailableValueT: { + call: (orderHash: string) => BigNumber.BigNumber; + }; isRoundingError: { call: (takerTokenAmount: BigNumber.BigNumber, fillTakerAmount: BigNumber.BigNumber, makerTokenAmount: BigNumber.BigNumber, txOpts: TxOpts) => Promise<boolean>; @@ -47,6 +50,12 @@ export interface ExchangeContract { estimateGas: (orderAddresses: OrderAddresses, orderValues: OrderValues, fillAmount: BigNumber.BigNumber, shouldCheckTransfer: boolean, v: number, r: string, s: string, txOpts: TxOpts) => number; }; + filled: { + call: (orderHash: string) => BigNumber.BigNumber; + }; + cancelled: { + call: (orderHash: string) => BigNumber.BigNumber; + }; ZRX: { call: () => Promise<string>; }; diff --git a/src/utils/assert.ts b/src/utils/assert.ts index 446974b10..5a31e1b16 100644 --- a/src/utils/assert.ts +++ b/src/utils/assert.ts @@ -3,6 +3,7 @@ import * as BigNumber from 'bignumber.js'; import * as Web3 from 'web3'; import {Web3Wrapper} from '../web3_wrapper'; import {SchemaValidator} from './schema_validator'; +import {utils} from './utils'; const HEX_REGEX = /^0x[0-9A-F]*$/i; @@ -33,6 +34,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)); + }, isBoolean(variableName: string, value: boolean): void { this.assert(_.isBoolean(value), this.typeAssertionMessage(variableName, 'boolean', value)); }, 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; + }, }; |