diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-03-30 02:08:52 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-04-21 04:56:17 +0800 |
commit | 75f77f3e8675c468d88456aec02561732c7646fb (patch) | |
tree | 6cbc55dca9fda65c7872e078ff134cf86107356c /packages/contracts/src/utils | |
parent | 7a66da5a51603d4d87d644f3635f8c7f1d00bb36 (diff) | |
download | dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar.gz dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar.bz2 dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar.lz dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar.xz dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar.zst dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.zip |
Add marketBuyOrders, rename variables, and fx old tests
Diffstat (limited to 'packages/contracts/src/utils')
-rw-r--r-- | packages/contracts/src/utils/exchange_wrapper.ts | 88 | ||||
-rw-r--r-- | packages/contracts/src/utils/formatters.ts | 79 | ||||
-rw-r--r-- | packages/contracts/src/utils/order_utils.ts | 30 | ||||
-rw-r--r-- | packages/contracts/src/utils/types.ts | 28 |
4 files changed, 121 insertions, 104 deletions
diff --git a/packages/contracts/src/utils/exchange_wrapper.ts b/packages/contracts/src/utils/exchange_wrapper.ts index 179fe44a5..743a9c496 100644 --- a/packages/contracts/src/utils/exchange_wrapper.ts +++ b/packages/contracts/src/utils/exchange_wrapper.ts @@ -22,12 +22,12 @@ export class ExchangeWrapper { public async fillOrderAsync( signedOrder: SignedOrder, from: string, - opts: { takerTokenFillAmount?: BigNumber } = {}, + opts: { takerSellAmount?: BigNumber } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = orderUtils.createFill(signedOrder, opts.takerTokenFillAmount); + const params = orderUtils.createFill(signedOrder, opts.takerSellAmount); const txHash = await this._exchange.fillOrder.sendTransactionAsync( params.order, - params.takerTokenFillAmount, + params.takerSellAmount, params.signature, { from }, ); @@ -43,12 +43,12 @@ export class ExchangeWrapper { public async fillOrKillOrderAsync( signedOrder: SignedOrder, from: string, - opts: { takerTokenFillAmount?: BigNumber } = {}, + opts: { takerSellAmount?: BigNumber } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = orderUtils.createFill(signedOrder, opts.takerTokenFillAmount); + const params = orderUtils.createFill(signedOrder, opts.takerSellAmount); const txHash = await this._exchange.fillOrKillOrder.sendTransactionAsync( params.order, - params.takerTokenFillAmount, + params.takerSellAmount, params.signature, { from }, ); @@ -58,12 +58,12 @@ export class ExchangeWrapper { public async fillOrderNoThrowAsync( signedOrder: SignedOrder, from: string, - opts: { takerTokenFillAmount?: BigNumber } = {}, + opts: { takerSellAmount?: BigNumber } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = orderUtils.createFill(signedOrder, opts.takerTokenFillAmount); + const params = orderUtils.createFill(signedOrder, opts.takerSellAmount); const txHash = await this._exchange.fillOrderNoThrow.sendTransactionAsync( params.order, - params.takerTokenFillAmount, + params.takerSellAmount, params.signature, { from }, ); @@ -73,12 +73,12 @@ export class ExchangeWrapper { public async batchFillOrdersAsync( orders: SignedOrder[], from: string, - opts: { takerTokenFillAmounts?: BigNumber[] } = {}, + opts: { takerSellAmounts?: BigNumber[] } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = formatters.createBatchFill(orders, opts.takerTokenFillAmounts); + const params = formatters.createBatchFill(orders, opts.takerSellAmounts); const txHash = await this._exchange.batchFillOrders.sendTransactionAsync( params.orders, - params.takerTokenFillAmounts, + params.takerSellAmounts, params.signatures, { from }, ); @@ -88,12 +88,12 @@ export class ExchangeWrapper { public async batchFillOrKillOrdersAsync( orders: SignedOrder[], from: string, - opts: { takerTokenFillAmounts?: BigNumber[] } = {}, + opts: { takerSellAmounts?: BigNumber[] } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = formatters.createBatchFill(orders, opts.takerTokenFillAmounts); + const params = formatters.createBatchFill(orders, opts.takerSellAmounts); const txHash = await this._exchange.batchFillOrKillOrders.sendTransactionAsync( params.orders, - params.takerTokenFillAmounts, + params.takerSellAmounts, params.signatures, { from }, ); @@ -103,42 +103,72 @@ export class ExchangeWrapper { public async batchFillOrdersNoThrowAsync( orders: SignedOrder[], from: string, - opts: { takerTokenFillAmounts?: BigNumber[] } = {}, + opts: { takerSellAmounts?: BigNumber[] } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = formatters.createBatchFill(orders, opts.takerTokenFillAmounts); + const params = formatters.createBatchFill(orders, opts.takerSellAmounts); const txHash = await this._exchange.batchFillOrdersNoThrow.sendTransactionAsync( params.orders, - params.takerTokenFillAmounts, + params.takerSellAmounts, params.signatures, { from }, ); const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } - public async marketFillOrdersAsync( + public async marketSellOrdersAsync( orders: SignedOrder[], from: string, - opts: { takerTokenFillAmount: BigNumber }, + opts: { takerSellAmount: BigNumber }, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = formatters.createMarketFillOrders(orders, opts.takerTokenFillAmount); - const txHash = await this._exchange.marketFillOrders.sendTransactionAsync( + const params = formatters.createMarketSellOrders(orders, opts.takerSellAmount); + const txHash = await this._exchange.marketSellOrders.sendTransactionAsync( params.orders, - params.takerTokenFillAmount, + params.takerSellAmount, params.signatures, { from }, ); const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } - public async marketFillOrdersNoThrowAsync( + public async marketSellOrdersNoThrowAsync( orders: SignedOrder[], from: string, - opts: { takerTokenFillAmount: BigNumber }, + opts: { takerSellAmount: BigNumber }, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = formatters.createMarketFillOrders(orders, opts.takerTokenFillAmount); - const txHash = await this._exchange.marketFillOrdersNoThrow.sendTransactionAsync( + const params = formatters.createMarketSellOrders(orders, opts.takerSellAmount); + const txHash = await this._exchange.marketSellOrdersNoThrow.sendTransactionAsync( params.orders, - params.takerTokenFillAmount, + params.takerSellAmount, + params.signatures, + { from }, + ); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + return tx; + } + public async marketBuyOrdersAsync( + orders: SignedOrder[], + from: string, + opts: { takerBuyAmount: BigNumber }, + ): Promise<TransactionReceiptWithDecodedLogs> { + const params = formatters.createMarketBuyOrders(orders, opts.takerBuyAmount); + const txHash = await this._exchange.marketBuyOrders.sendTransactionAsync( + params.orders, + params.takerBuyAmount, + params.signatures, + { from }, + ); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + return tx; + } + public async marketBuyOrdersNoThrowAsync( + orders: SignedOrder[], + from: string, + opts: { takerBuyAmount: BigNumber }, + ): Promise<TransactionReceiptWithDecodedLogs> { + const params = formatters.createMarketBuyOrders(orders, opts.takerBuyAmount); + const txHash = await this._exchange.marketBuyOrdersNoThrow.sendTransactionAsync( + params.orders, + params.takerBuyAmount, params.signatures, { from }, ); @@ -191,7 +221,7 @@ export class ExchangeWrapper { ); return partialAmount; } - public async getFilledTakerTokenAmountAsync(orderHashHex: string): Promise<BigNumber> { + public async getMakerAmountBoughtAsync(orderHashHex: string): Promise<BigNumber> { const filledAmount = new BigNumber(await this._exchange.filled.callAsync(orderHashHex)); return filledAmount; } diff --git a/packages/contracts/src/utils/formatters.ts b/packages/contracts/src/utils/formatters.ts index 2b261f967..d70c03cb8 100644 --- a/packages/contracts/src/utils/formatters.ts +++ b/packages/contracts/src/utils/formatters.ts @@ -1,78 +1,59 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; -import { BatchCancelOrders, BatchFillOrders, MarketFillOrders, SignedOrder } from './types'; +import { orderUtils } from './order_utils'; +import { BatchCancelOrders, BatchFillOrders, MarketBuyOrders, MarketSellOrders, SignedOrder } from './types'; export const formatters = { - createBatchFill(signedOrders: SignedOrder[], takerTokenFillAmounts: BigNumber[] = []) { + createBatchFill(signedOrders: SignedOrder[], takerSellAmounts: BigNumber[] = []) { const batchFill: BatchFillOrders = { orders: [], signatures: [], - takerTokenFillAmounts, + takerSellAmounts, }; _.forEach(signedOrders, signedOrder => { - batchFill.orders.push({ - makerAddress: signedOrder.makerAddress, - takerAddress: signedOrder.takerAddress, - makerTokenAddress: signedOrder.makerTokenAddress, - takerTokenAddress: signedOrder.takerTokenAddress, - feeRecipientAddress: signedOrder.feeRecipientAddress, - makerTokenAmount: signedOrder.makerTokenAmount, - takerTokenAmount: signedOrder.takerTokenAmount, - makerFeeAmount: signedOrder.makerFeeAmount, - takerFeeAmount: signedOrder.takerFeeAmount, - expirationTimeSeconds: signedOrder.expirationTimeSeconds, - salt: signedOrder.salt, - }); + const orderStruct = orderUtils.getOrderStruct(signedOrder); + batchFill.orders.push(orderStruct); batchFill.signatures.push(signedOrder.signature); - if (takerTokenFillAmounts.length < signedOrders.length) { - batchFill.takerTokenFillAmounts.push(signedOrder.takerTokenAmount); + if (takerSellAmounts.length < signedOrders.length) { + batchFill.takerSellAmounts.push(signedOrder.makerBuyAmount); } }); return batchFill; }, - createMarketFillOrders(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber) { - const marketFillOrders: MarketFillOrders = { + createMarketSellOrders(signedOrders: SignedOrder[], takerSellAmount: BigNumber) { + const marketSellOrders: MarketSellOrders = { orders: [], signatures: [], - takerTokenFillAmount, + takerSellAmount, }; _.forEach(signedOrders, signedOrder => { - marketFillOrders.orders.push({ - makerAddress: signedOrder.makerAddress, - takerAddress: signedOrder.takerAddress, - makerTokenAddress: signedOrder.makerTokenAddress, - takerTokenAddress: signedOrder.takerTokenAddress, - feeRecipientAddress: signedOrder.feeRecipientAddress, - makerTokenAmount: signedOrder.makerTokenAmount, - takerTokenAmount: signedOrder.takerTokenAmount, - makerFeeAmount: signedOrder.makerFeeAmount, - takerFeeAmount: signedOrder.takerFeeAmount, - expirationTimeSeconds: signedOrder.expirationTimeSeconds, - salt: signedOrder.salt, - }); - marketFillOrders.signatures.push(signedOrder.signature); + const orderStruct = orderUtils.getOrderStruct(signedOrder); + marketSellOrders.orders.push(orderStruct); + marketSellOrders.signatures.push(signedOrder.signature); }); - return marketFillOrders; + return marketSellOrders; + }, + createMarketBuyOrders(signedOrders: SignedOrder[], takerBuyAmount: BigNumber) { + const marketBuyOrders: MarketBuyOrders = { + orders: [], + signatures: [], + takerBuyAmount, + }; + _.forEach(signedOrders, signedOrder => { + const orderStruct = orderUtils.getOrderStruct(signedOrder); + marketBuyOrders.orders.push(orderStruct); + marketBuyOrders.signatures.push(signedOrder.signature); + }); + return marketBuyOrders; }, createBatchCancel(signedOrders: SignedOrder[]) { const batchCancel: BatchCancelOrders = { orders: [], }; _.forEach(signedOrders, signedOrder => { - batchCancel.orders.push({ - makerAddress: signedOrder.makerAddress, - takerAddress: signedOrder.takerAddress, - makerTokenAddress: signedOrder.makerTokenAddress, - takerTokenAddress: signedOrder.takerTokenAddress, - feeRecipientAddress: signedOrder.feeRecipientAddress, - makerTokenAmount: signedOrder.makerTokenAmount, - takerTokenAmount: signedOrder.takerTokenAmount, - makerFeeAmount: signedOrder.makerFeeAmount, - takerFeeAmount: signedOrder.takerFeeAmount, - expirationTimeSeconds: signedOrder.expirationTimeSeconds, - salt: signedOrder.salt, - }); + const orderStruct = orderUtils.getOrderStruct(signedOrder); + batchCancel.orders.push(orderStruct); }); return batchCancel; }, diff --git a/packages/contracts/src/utils/order_utils.ts b/packages/contracts/src/utils/order_utils.ts index c14dc4e80..1f808024a 100644 --- a/packages/contracts/src/utils/order_utils.ts +++ b/packages/contracts/src/utils/order_utils.ts @@ -7,10 +7,10 @@ import { crypto } from './crypto'; import { OrderStruct, SignatureType, SignedOrder, UnsignedOrder } from './types'; export const orderUtils = { - createFill: (signedOrder: SignedOrder, takerTokenFillAmount?: BigNumber) => { + createFill: (signedOrder: SignedOrder, takerSellAmount?: BigNumber) => { const fill = { order: orderUtils.getOrderStruct(signedOrder), - takerTokenFillAmount: takerTokenFillAmount || signedOrder.takerTokenAmount, + takerSellAmount: takerSellAmount || signedOrder.makerBuyAmount, signature: signedOrder.signature, }; return fill; @@ -18,7 +18,7 @@ export const orderUtils = { createCancel(signedOrder: SignedOrder, takerTokenCancelAmount?: BigNumber) { const cancel = { order: orderUtils.getOrderStruct(signedOrder), - takerTokenCancelAmount: takerTokenCancelAmount || signedOrder.takerTokenAmount, + takerTokenCancelAmount: takerTokenCancelAmount || signedOrder.makerBuyAmount, }; return cancel; }, @@ -29,10 +29,10 @@ export const orderUtils = { makerTokenAddress: signedOrder.makerTokenAddress, takerTokenAddress: signedOrder.takerTokenAddress, feeRecipientAddress: signedOrder.feeRecipientAddress, - makerTokenAmount: signedOrder.makerTokenAmount, - takerTokenAmount: signedOrder.takerTokenAmount, - makerFeeAmount: signedOrder.makerFeeAmount, - takerFeeAmount: signedOrder.takerFeeAmount, + makerSellAmount: signedOrder.makerSellAmount, + makerBuyAmount: signedOrder.makerBuyAmount, + makerFee: signedOrder.makerFee, + takerFee: signedOrder.takerFee, expirationTimeSeconds: signedOrder.expirationTimeSeconds, salt: signedOrder.salt, }; @@ -46,10 +46,10 @@ export const orderUtils = { 'address makerTokenAddress', 'address takerTokenAddress', 'address feeRecipientAddress', - 'uint256 makerTokenAmount', - 'uint256 takerTokenAmount', - 'uint256 makerFeeAmount', - 'uint256 takerFeeAmount', + 'uint256 makerSellAmount', + 'uint256 makerBuyAmount', + 'uint256 makerFee', + 'uint256 takerFee', 'uint256 expirationTimeSeconds', 'uint256 salt', ]); @@ -60,10 +60,10 @@ export const orderUtils = { order.makerTokenAddress, order.takerTokenAddress, order.feeRecipientAddress, - order.makerTokenAmount, - order.takerTokenAmount, - order.makerFeeAmount, - order.takerFeeAmount, + order.makerSellAmount, + order.makerBuyAmount, + order.makerFee, + order.takerFee, order.expirationTimeSeconds, order.salt, ]); diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts index ce87e9851..570f664e0 100644 --- a/packages/contracts/src/utils/types.ts +++ b/packages/contracts/src/utils/types.ts @@ -14,13 +14,19 @@ export interface SubmissionContractEventArgs { export interface BatchFillOrders { orders: OrderStruct[]; signatures: string[]; - takerTokenFillAmounts: BigNumber[]; + takerSellAmounts: BigNumber[]; } -export interface MarketFillOrders { +export interface MarketSellOrders { orders: OrderStruct[]; signatures: string[]; - takerTokenFillAmount: BigNumber; + takerSellAmount: BigNumber; +} + +export interface MarketBuyOrders { + orders: OrderStruct[]; + signatures: string[]; + takerBuyAmount: BigNumber; } export interface BatchCancelOrders { @@ -37,10 +43,10 @@ export interface DefaultOrderParams { feeRecipientAddress: string; makerTokenAddress: string; takerTokenAddress: string; - makerTokenAmount: BigNumber; - takerTokenAmount: BigNumber; - makerFeeAmount: BigNumber; - takerFeeAmount: BigNumber; + makerSellAmount: BigNumber; + makerBuyAmount: BigNumber; + makerFee: BigNumber; + takerFee: BigNumber; } export interface TransactionDataParams { @@ -122,10 +128,10 @@ export interface OrderStruct { makerTokenAddress: string; takerTokenAddress: string; feeRecipientAddress: string; - makerTokenAmount: BigNumber; - takerTokenAmount: BigNumber; - makerFeeAmount: BigNumber; - takerFeeAmount: BigNumber; + makerSellAmount: BigNumber; + makerBuyAmount: BigNumber; + makerFee: BigNumber; + takerFee: BigNumber; expirationTimeSeconds: BigNumber; salt: BigNumber; } |