David Sun 74d3b9334c
Add liquidity source breakdown to asset-swapper (#2465)
* add liquidity source breakdown to asset-swapper

* remove debug line

* use OptimizedMarketOrder metadata

* updated change-log

* prettier + lint

* bug fixes

* fixes

* Prettier

* Fix types

Co-authored-by: Jacob Evans <dekz@dekz.net>
2020-02-04 19:03:28 +10:00

58 lines
1.8 KiB
TypeScript

import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import { ERC20BridgeSource } from '../../src';
import { constants } from '../../src/constants';
import { MarketOperation, SignedOrderWithFillableAmounts, SwapQuote } from '../../src/types';
import { ProtocolFeeUtils } from '../../src/utils/protocol_fee_utils';
/**
* Creates a swap quote given orders.
*/
export async function getFullyFillableSwapQuoteWithNoFeesAsync(
makerAssetData: string,
takerAssetData: string,
orders: SignedOrderWithFillableAmounts[],
operation: MarketOperation,
gasPrice: BigNumber,
protocolFeeUtils: ProtocolFeeUtils,
): Promise<SwapQuote> {
const makerAssetFillAmount = BigNumber.sum(...[0, ...orders.map(o => o.makerAssetAmount)]);
const totalTakerAssetAmount = BigNumber.sum(...[0, ...orders.map(o => o.takerAssetAmount)]);
const quoteInfo = {
makerAssetAmount: makerAssetFillAmount,
feeTakerAssetAmount: constants.ZERO_AMOUNT,
takerAssetAmount: totalTakerAssetAmount,
totalTakerAssetAmount,
protocolFeeInWeiAmount: await protocolFeeUtils.calculateWorstCaseProtocolFeeAsync(orders, gasPrice),
};
const breakdown = {
[ERC20BridgeSource.Native]: new BigNumber(1),
};
const quoteBase = {
makerAssetData,
takerAssetData,
orders,
gasPrice,
bestCaseQuoteInfo: quoteInfo,
worstCaseQuoteInfo: quoteInfo,
sourceBreakdown: breakdown,
};
if (operation === MarketOperation.Buy) {
return {
...quoteBase,
type: MarketOperation.Buy,
makerAssetFillAmount,
};
} else {
return {
...quoteBase,
type: MarketOperation.Sell,
takerAssetFillAmount: totalTakerAssetAmount,
};
}
}