diff options
Diffstat (limited to 'packages/contracts/util/exchange_wrapper.ts')
-rw-r--r-- | packages/contracts/util/exchange_wrapper.ts | 73 |
1 files changed, 50 insertions, 23 deletions
diff --git a/packages/contracts/util/exchange_wrapper.ts b/packages/contracts/util/exchange_wrapper.ts index ca79f92c4..7a07239f5 100644 --- a/packages/contracts/util/exchange_wrapper.ts +++ b/packages/contracts/util/exchange_wrapper.ts @@ -1,14 +1,17 @@ +import { TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js'; import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; +import * as Web3 from 'web3'; import { formatters } from './formatters'; import { Order } from './order'; -import { ContractInstance } from './types'; export class ExchangeWrapper { - private _exchange: ContractInstance; - constructor(exchangeContractInstance: ContractInstance) { + private _exchange: Web3.ContractInstance; + private _zeroEx: ZeroEx; + constructor(exchangeContractInstance: Web3.ContractInstance, zeroEx: ZeroEx) { this._exchange = exchangeContractInstance; + this._zeroEx = zeroEx; } public async fillOrderAsync( order: Order, @@ -17,10 +20,10 @@ export class ExchangeWrapper { fillTakerTokenAmount?: BigNumber; shouldThrowOnInsufficientBalanceOrAllowance?: boolean; } = {}, - ) { + ): Promise<TransactionReceiptWithDecodedLogs> { const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance; const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount); - const tx = await this._exchange.fillOrder( + const txHash = await this._exchange.fillOrder( params.orderAddresses, params.orderValues, params.fillTakerTokenAmount, @@ -30,24 +33,36 @@ export class ExchangeWrapper { params.s, { from }, ); + const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); + tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); _.each(tx.logs, log => wrapLogBigNumbers(log)); return tx; } - public async cancelOrderAsync(order: Order, from: string, opts: { cancelTakerTokenAmount?: BigNumber } = {}) { + public async cancelOrderAsync( + order: Order, + from: string, + opts: { cancelTakerTokenAmount?: BigNumber } = {}, + ): Promise<TransactionReceiptWithDecodedLogs> { const params = order.createCancel(opts.cancelTakerTokenAmount); - const tx = await this._exchange.cancelOrder( + const txHash = await this._exchange.cancelOrder( params.orderAddresses, params.orderValues, params.cancelTakerTokenAmount, { from }, ); + const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); + tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); _.each(tx.logs, log => wrapLogBigNumbers(log)); return tx; } - public async fillOrKillOrderAsync(order: Order, from: string, opts: { fillTakerTokenAmount?: BigNumber } = {}) { + public async fillOrKillOrderAsync( + order: Order, + from: string, + opts: { fillTakerTokenAmount?: BigNumber } = {}, + ): Promise<TransactionReceiptWithDecodedLogs> { const shouldThrowOnInsufficientBalanceOrAllowance = true; const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount); - const tx = await this._exchange.fillOrKillOrder( + const txHash = await this._exchange.fillOrKillOrder( params.orderAddresses, params.orderValues, params.fillTakerTokenAmount, @@ -56,6 +71,8 @@ export class ExchangeWrapper { params.s, { from }, ); + const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); + tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); _.each(tx.logs, log => wrapLogBigNumbers(log)); return tx; } @@ -66,14 +83,14 @@ export class ExchangeWrapper { fillTakerTokenAmounts?: BigNumber[]; shouldThrowOnInsufficientBalanceOrAllowance?: boolean; } = {}, - ) { + ): Promise<TransactionReceiptWithDecodedLogs> { const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance; const params = formatters.createBatchFill( orders, shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmounts, ); - const tx = await this._exchange.batchFillOrders( + const txHash = await this._exchange.batchFillOrders( params.orderAddresses, params.orderValues, params.fillTakerTokenAmounts, @@ -83,16 +100,23 @@ export class ExchangeWrapper { params.s, { from }, ); + const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); + tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); _.each(tx.logs, log => wrapLogBigNumbers(log)); return tx; } public async batchFillOrKillOrdersAsync( orders: Order[], from: string, - opts: { fillTakerTokenAmounts?: BigNumber[] } = {}, - ) { - const params = formatters.createBatchFill(orders, undefined, opts.fillTakerTokenAmounts); - const tx = await this._exchange.batchFillOrKillOrders( + opts: { fillTakerTokenAmounts?: BigNumber[]; shouldThrowOnInsufficientBalanceOrAllowance?: boolean } = {}, + ): Promise<TransactionReceiptWithDecodedLogs> { + const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance; + const params = formatters.createBatchFill( + orders, + shouldThrowOnInsufficientBalanceOrAllowance, + opts.fillTakerTokenAmounts, + ); + const txHash = await this._exchange.batchFillOrKillOrders( params.orderAddresses, params.orderValues, params.fillTakerTokenAmounts, @@ -101,24 +125,23 @@ export class ExchangeWrapper { params.s, { from }, ); + const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); + tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); _.each(tx.logs, log => wrapLogBigNumbers(log)); return tx; } public async fillOrdersUpToAsync( orders: Order[], from: string, - opts: { - fillTakerTokenAmount?: BigNumber; - shouldThrowOnInsufficientBalanceOrAllowance?: boolean; - } = {}, - ) { + opts: { fillTakerTokenAmount: BigNumber; shouldThrowOnInsufficientBalanceOrAllowance?: boolean }, + ): Promise<TransactionReceiptWithDecodedLogs> { const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance; const params = formatters.createFillUpTo( orders, shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount, ); - const tx = await this._exchange.fillOrdersUpTo( + const txHash = await this._exchange.fillOrdersUpTo( params.orderAddresses, params.orderValues, params.fillTakerTokenAmount, @@ -128,6 +151,8 @@ export class ExchangeWrapper { params.s, { from }, ); + const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); + tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); _.each(tx.logs, log => wrapLogBigNumbers(log)); return tx; } @@ -135,14 +160,16 @@ export class ExchangeWrapper { orders: Order[], from: string, opts: { cancelTakerTokenAmounts?: BigNumber[] } = {}, - ) { + ): Promise<TransactionReceiptWithDecodedLogs> { const params = formatters.createBatchCancel(orders, opts.cancelTakerTokenAmounts); - const tx = await this._exchange.batchCancelOrders( + const txHash = await this._exchange.batchCancelOrders( params.orderAddresses, params.orderValues, params.cancelTakerTokenAmounts, { from }, ); + const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); + tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); _.each(tx.logs, log => wrapLogBigNumbers(log)); return tx; } |