diff options
Diffstat (limited to 'src/contract_wrappers')
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 85 | ||||
-rw-r--r-- | src/contract_wrappers/token_wrapper.ts | 4 |
2 files changed, 86 insertions, 3 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 3fb187de2..de614f471 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -1,4 +1,6 @@ import * as _ from 'lodash'; +import * as BigNumber from 'bignumber.js'; +import promisify = require('es6-promisify'); import {Web3Wrapper} from '../web3_wrapper'; import { ECSignature, @@ -10,8 +12,16 @@ import { OrderAddresses, SignedOrder, ContractEvent, + ZeroExError, + ExchangeEvents, + SubscriptionOpts, + IndexFilterValues, + CreateContractEvent, + ContractEventObj, + EventCallback, } from '../types'; import {assert} from '../utils/assert'; +import {utils} from '../utils/utils'; import {ContractWrapper} from './contract_wrapper'; import * as ExchangeArtifacts from '../artifacts/Exchange.json'; import {ecSignatureSchema} from '../schemas/ec_signature_schema'; @@ -31,12 +41,15 @@ export class ExchangeWrapper extends ContractWrapper { [ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.ORDER_BALANCE_ALLOWANCE_ERROR, }; private exchangeContractIfExists?: ExchangeContract; + private exchangeLogEventObjs: ContractEventObj[]; private tokenWrapper: TokenWrapper; constructor(web3Wrapper: Web3Wrapper, tokenWrapper: TokenWrapper) { super(web3Wrapper); this.tokenWrapper = tokenWrapper; + this.exchangeLogEventObjs = []; } - public invalidateContractInstance(): void { + public async invalidateContractInstanceAsync(): Promise<void> { + await this.stopWatchingExchangeLogEventsAsync(); delete this.exchangeContractIfExists; } public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, @@ -61,6 +74,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. The caller can * decide whether they want the call to throw if the balance/allowance checks fail by setting * shouldCheckTransfer to false. If set to true, the call will fail without throwing, preserving gas costs. @@ -120,6 +171,38 @@ export class ExchangeWrapper extends ContractWrapper { ); this.throwErrorLogsAsErrors(response.logs); } + /** + * Subscribe to an event type emitted by the Exchange smart contract + */ + public async subscribeAsync(eventName: ExchangeEvents, subscriptionOpts: SubscriptionOpts, + indexFilterValues: IndexFilterValues, callback: EventCallback) { + const exchangeContract = await this.getExchangeContractAsync(); + let createLogEvent: CreateContractEvent; + switch (eventName) { + case ExchangeEvents.LogFill: + createLogEvent = exchangeContract.LogFill; + break; + case ExchangeEvents.LogError: + createLogEvent = exchangeContract.LogError; + break; + case ExchangeEvents.LogCancel: + createLogEvent = exchangeContract.LogCancel; + break; + default: + utils.spawnSwitchErr('ExchangeEvents', eventName); + return; + } + + const logEventObj: ContractEventObj = createLogEvent(indexFilterValues, subscriptionOpts); + logEventObj.watch(callback); + this.exchangeLogEventObjs.push(logEventObj); + } + private async stopWatchingExchangeLogEventsAsync() { + for (const logEventObj of this.exchangeLogEventObjs) { + await promisify(logEventObj.stopWatching, logEventObj)(); + } + this.exchangeLogEventObjs = []; + } private async validateFillOrderAsync(signedOrder: SignedOrder, fillTakerAmountInBaseUnits: BigNumber.BigNumber, senderAddress: string) { if (fillTakerAmountInBaseUnits.eq(0)) { 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; } |