make the slippage percentage customizable by integrator

This commit is contained in:
fragosti
2018-09-18 16:42:52 +02:00
parent b947609ff1
commit 0003666050
3 changed files with 20 additions and 7 deletions

View File

@@ -13,6 +13,7 @@ import {
AssetBuyerError, AssetBuyerError,
AssetBuyerOrdersAndFillableAmounts, AssetBuyerOrdersAndFillableAmounts,
BuyQuote, BuyQuote,
BuyQuoteRequestOpts,
OrderFetcher, OrderFetcher,
OrderFetcherResponse, OrderFetcherResponse,
} from './types'; } from './types';
@@ -160,12 +161,13 @@ export class AssetBuyer {
*/ */
public async getBuyQuoteAsync( public async getBuyQuoteAsync(
assetBuyAmount: BigNumber, assetBuyAmount: BigNumber,
feePercentage: number = constants.DEFAULT_FEE_PERCENTAGE, options: Partial<BuyQuoteRequestOpts>,
forceOrderRefresh: boolean = false,
): Promise<BuyQuote> { ): Promise<BuyQuote> {
const { feePercentage, forceOrderRefresh, slippagePercentage } = { ...options, ...constants.DEFAULT_BUY_QUOTE_REQUEST_OPTS };
assert.isBigNumber('assetBuyAmount', assetBuyAmount); assert.isBigNumber('assetBuyAmount', assetBuyAmount);
assert.isNumber('feePercentage', feePercentage); assert.isNumber('feePercentage', feePercentage);
assert.isBoolean('forceOrderRefresh', forceOrderRefresh); assert.isBoolean('forceOrderRefresh', forceOrderRefresh);
assert.isNumber('feePercentage', slippagePercentage);
// we should refresh if: // we should refresh if:
// we do not have any orders OR // we do not have any orders OR
// we are forced to OR // we are forced to OR
@@ -185,13 +187,11 @@ export class AssetBuyer {
ordersAndFillableAmounts = this ordersAndFillableAmounts = this
._currentOrdersAndFillableAmountsIfExists as AssetBuyerOrdersAndFillableAmounts; ._currentOrdersAndFillableAmountsIfExists as AssetBuyerOrdersAndFillableAmounts;
} }
// TODO: optimization
// make the slippage percentage customizable by integrator
const buyQuote = buyQuoteCalculator.calculate( const buyQuote = buyQuoteCalculator.calculate(
ordersAndFillableAmounts, ordersAndFillableAmounts,
assetBuyAmount, assetBuyAmount,
feePercentage, feePercentage,
constants.DEFAULT_SLIPPAGE_PERCENTAGE, slippagePercentage,
); );
return buyQuote; return buyQuote;
} }

View File

@@ -1,11 +1,18 @@
import { BigNumber } from '@0xproject/utils'; import { BigNumber } from '@0xproject/utils';
import { BuyQuoteRequestOpts } from './types';
const DEFAULT_BUY_QUOTE_REQUEST_OPTS: BuyQuoteRequestOpts = {
feePercentage: 0,
forceOrderRefresh: false,
slippagePercentage: 0.2, // 20% slippage protection
};
export const constants = { export const constants = {
ZERO_AMOUNT: new BigNumber(0), ZERO_AMOUNT: new BigNumber(0),
NULL_ADDRESS: '0x0000000000000000000000000000000000000000', NULL_ADDRESS: '0x0000000000000000000000000000000000000000',
MAINNET_NETWORK_ID: 1, MAINNET_NETWORK_ID: 1,
DEFAULT_SLIPPAGE_PERCENTAGE: 0.2, // 20% slippage protection
DEFAULT_ORDER_REFRESH_INTERVAL_MS: 10000, // 10 seconds DEFAULT_ORDER_REFRESH_INTERVAL_MS: 10000, // 10 seconds
DEFAULT_FEE_PERCENTAGE: 0,
ETHER_TOKEN_DECIMALS: 18, ETHER_TOKEN_DECIMALS: 18,
DEFAULT_BUY_QUOTE_REQUEST_OPTS,
}; };

View File

@@ -52,6 +52,12 @@ export interface BuyQuote {
feePercentage?: number; feePercentage?: number;
} }
export interface BuyQuoteRequestOpts {
feePercentage: number;
forceOrderRefresh: boolean;
slippagePercentage: number;
}
/** /**
* Possible errors thrown by an AssetBuyer instance or associated static methods. * Possible errors thrown by an AssetBuyer instance or associated static methods.
*/ */