diff options
Diffstat (limited to 'packages/contracts/src/utils/formatters.ts')
-rw-r--r-- | packages/contracts/src/utils/formatters.ts | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/packages/contracts/src/utils/formatters.ts b/packages/contracts/src/utils/formatters.ts new file mode 100644 index 000000000..88e12a6b4 --- /dev/null +++ b/packages/contracts/src/utils/formatters.ts @@ -0,0 +1,107 @@ +import { SignedOrder } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { BatchCancelOrders, BatchFillOrders, MarketFillOrders } from './types'; + +export const formatters = { + createBatchFill( + signedOrders: SignedOrder[], + takerTokenFillAmounts: BigNumber[] = [], + ) { + const batchFill: BatchFillOrders = { + orderAddresses: [], + orderValues: [], + takerTokenFillAmounts, + v: [], + r: [], + s: [], + }; + _.forEach(signedOrders, signedOrder => { + batchFill.orderAddresses.push([ + signedOrder.maker, + signedOrder.taker, + signedOrder.makerTokenAddress, + signedOrder.takerTokenAddress, + signedOrder.feeRecipient, + ]); + batchFill.orderValues.push([ + signedOrder.makerTokenAmount, + signedOrder.takerTokenAmount, + signedOrder.makerFee, + signedOrder.takerFee, + signedOrder.expirationUnixTimestampSec, + signedOrder.salt, + ]); + batchFill.v.push(signedOrder.ecSignature.v); + batchFill.r.push(signedOrder.ecSignature.r); + batchFill.s.push(signedOrder.ecSignature.s); + if (takerTokenFillAmounts.length < signedOrders.length) { + batchFill.takerTokenFillAmounts.push(signedOrder.takerTokenAmount); + } + }); + return batchFill; + }, + createMarketFillOrders( + signedOrders: SignedOrder[], + takerTokenFillAmount: BigNumber, + ) { + const marketFillOrders: MarketFillOrders = { + orderAddresses: [], + orderValues: [], + takerTokenFillAmount, + v: [], + r: [], + s: [], + }; + signedOrders.forEach(signedOrder => { + marketFillOrders.orderAddresses.push([ + signedOrder.maker, + signedOrder.taker, + signedOrder.makerTokenAddress, + signedOrder.takerTokenAddress, + signedOrder.feeRecipient, + ]); + marketFillOrders.orderValues.push([ + signedOrder.makerTokenAmount, + signedOrder.takerTokenAmount, + signedOrder.makerFee, + signedOrder.takerFee, + signedOrder.expirationUnixTimestampSec, + signedOrder.salt, + ]); + marketFillOrders.v.push(signedOrder.ecSignature.v); + marketFillOrders.r.push(signedOrder.ecSignature.r); + marketFillOrders.s.push(signedOrder.ecSignature.s); + }); + return marketFillOrders; + }, + createBatchCancel(signedOrders: SignedOrder[], takerTokenCancelAmounts: BigNumber[] = []) { + const batchCancel: BatchCancelOrders = { + orderAddresses: [], + orderValues: [], + takerTokenCancelAmounts, + }; + signedOrders.forEach(signedOrder => { + batchCancel.orderAddresses.push([ + signedOrder.maker, + signedOrder.taker, + signedOrder.makerTokenAddress, + signedOrder.takerTokenAddress, + signedOrder.feeRecipient, + ]); + batchCancel.orderValues.push([ + signedOrder.makerTokenAmount, + signedOrder.takerTokenAmount, + signedOrder.makerFee, + signedOrder.takerFee, + signedOrder.expirationUnixTimestampSec, + signedOrder.salt, + ]); + if (takerTokenCancelAmounts.length < signedOrders.length) { + batchCancel.takerTokenCancelAmounts.push(signedOrder.takerTokenAmount); + } + }); + return batchCancel; + }, +}; |