Remove marketSell and add to marketBuy implementation

This commit is contained in:
Brandon Millman
2018-08-17 00:15:52 -07:00
parent 2ef867f398
commit 3c973ba9f6
6 changed files with 51 additions and 65 deletions

View File

@@ -0,0 +1,5 @@
import { BigNumber } from '@0xproject/utils';
export const constants = {
ZERO_AMOUNT: new BigNumber(0),
};

View File

@@ -1,13 +1,11 @@
import { marketUtils } from '@0xproject/order-utils';
import { SignedOrder } from '@0xproject/types'; import { SignedOrder } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils'; import { BigNumber } from '@0xproject/utils';
import { import { constants } from './constants';
ForwarderHelper, import { ForwarderHelper, ForwarderHelperError, MarketBuyOrdersInfo, MarketBuyOrdersInfoRequest } from './types';
MarketBuyOrdersInfo,
MarketBuyOrdersInfoRequest, const SLIPPAGE_PERCENTAGE = new BigNumber(0.2); // 20% slippage protection, possibly move this into request interface
MarketSellOrdersInfo,
MarketSellOrdersInfoRequest,
} from './types';
export class ForwarderHelperImpl implements ForwarderHelper { export class ForwarderHelperImpl implements ForwarderHelper {
private _orders: SignedOrder[]; private _orders: SignedOrder[];
@@ -26,24 +24,40 @@ export class ForwarderHelperImpl implements ForwarderHelper {
this._remainingFillableFeeAmountsIfExists = remainingFillableFeeAmounts; this._remainingFillableFeeAmountsIfExists = remainingFillableFeeAmounts;
} }
public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo { public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo {
const { makerAssetFillAmount, feePercentage, acceptableEthAmountRange } = request; const { makerAssetFillAmount, feePercentage } = request;
// TODO: make the slippage percentage customizable
const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE);
const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount(
this._orders,
makerAssetFillAmount,
{
remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists,
slippageBufferAmount,
},
);
if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) {
throw new Error(ForwarderHelperError.InsufficientLiquidity);
}
// TODO: update this logic to find the minimum amount of feeOrders to cover the worst case as opposed to
// finding order that cover all fees, this will help with estimating ETH and minimizing gas usage
const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
resultOrders,
this._feeOrders,
{
remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists,
remainingFillableFeeAmounts: this._remainingFillableFeeAmountsIfExists,
},
);
if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) {
throw new Error(ForwarderHelperError.InsufficientZrxLiquidity);
}
// TODO: calculate min and max eth usage
return { return {
makerAssetFillAmount, makerAssetFillAmount,
orders: this._orders, orders: resultOrders,
feeOrders: this._feeOrders, feeOrders: resultFeeOrders,
minEthAmount: new BigNumber(0), minEthAmount: constants.ZERO_AMOUNT,
maxEthAmount: new BigNumber(0), maxEthAmount: constants.ZERO_AMOUNT,
feePercentage,
};
}
public getMarketSellOrdersInfo(request: MarketSellOrdersInfoRequest): MarketSellOrdersInfo {
const { ethAmount, feePercentage, acceptableFillAmountRange } = request;
return {
ethAmount,
orders: this._orders,
feeOrders: this._feeOrders,
minFillAmount: new BigNumber(0),
maxFillAmount: new BigNumber(0),
feePercentage, feePercentage,
}; };
} }

View File

@@ -9,13 +9,6 @@ export interface ForwarderHelper {
* @return An object that conforms to MarketBuyOrdersInfo that satisfies the request. See type definition for more information. * @return An object that conforms to MarketBuyOrdersInfo that satisfies the request. See type definition for more information.
*/ */
getMarketBuyOrdersInfo: (request: MarketBuyOrdersInfoRequest) => MarketBuyOrdersInfo; getMarketBuyOrdersInfo: (request: MarketBuyOrdersInfoRequest) => MarketBuyOrdersInfo;
/**
* Given a MarketSellOrdersInfoRequest, returns a MarketSellOrdersInfo containing all information relevant to fulfilling the request
* using the ForwarderContract marketSellOrdersWithEth function.
* @param request An object that conforms to MarketSellOrdersInfoRequest. See type definition for more information.
* @return An object that conforms to MarketSellOrdersInfo that satisfies the request. See type definition for more information.
*/
getMarketSellOrdersInfo: (request: MarketSellOrdersInfoRequest) => MarketSellOrdersInfo;
} }
export enum ForwarderHelperError { export enum ForwarderHelperError {
@@ -26,12 +19,10 @@ export enum ForwarderHelperError {
/** /**
* makerAssetFillAmount: The amount of makerAsset requesting to be filled * makerAssetFillAmount: The amount of makerAsset requesting to be filled
* feePercentage: Optional affiliate percentage amount factoring into eth amount calculations * feePercentage: Optional affiliate percentage amount factoring into eth amount calculations
* acceptableEthAmountRange: maximum difference between min and max eth cost
*/ */
export interface MarketBuyOrdersInfoRequest { export interface MarketBuyOrdersInfoRequest {
makerAssetFillAmount: BigNumber; makerAssetFillAmount: BigNumber;
feePercentage?: BigNumber; feePercentage?: BigNumber;
acceptableEthAmountRange?: BigNumber;
} }
/** /**
@@ -50,31 +41,3 @@ export interface MarketBuyOrdersInfo {
maxEthAmount: BigNumber; maxEthAmount: BigNumber;
feePercentage?: BigNumber; feePercentage?: BigNumber;
} }
/**
* ethAmount: The amount of eth used to fill
* feePercentage: Optional affiliate percentage amount factoring into eth amount calculations
* acceptableFillAmountRange: maximum difference between min and max asset filled
*/
export interface MarketSellOrdersInfoRequest {
ethAmount: BigNumber;
feePercentage?: BigNumber;
acceptableFillAmountRange?: BigNumber;
}
/**
* ethAmount: The amount of eth used to fill
* orders: An array of objects conforming to SignedOrder. These orders can be used to cover the requested ethAmount plus slippage
* feeOrders: An array of objects conforming to SignedOrder. These orders can be used to cover the fees for the orders param above
* minFillAmount: Amount of asset purchased in the worst case
* maxFillAmount: Amount of asset purchased in the best case
* feePercentage: Affiliate fee percentage used to calculate the eth amounts above. Passed thru directly from the request
*/
export interface MarketSellOrdersInfo {
ethAmount: BigNumber;
orders: SignedOrder[];
feeOrders: SignedOrder[];
minFillAmount: BigNumber;
maxFillAmount: BigNumber;
feePercentage?: BigNumber;
}

View File

@@ -14,6 +14,10 @@
"note": "note":
"Update marketUtils api such that all optional parameters are bundled into one optional param and more defaults are provided", "Update marketUtils api such that all optional parameters are bundled into one optional param and more defaults are provided",
"pr": 954 "pr": 954
},
{
"note":
"Rename `resultOrders` to `resultFeeOrders` for object returned by `findFeeOrdersThatCoverFeesForTargetOrders` in `marketUtils` api"
} }
] ]
}, },

View File

@@ -84,7 +84,7 @@ export const marketUtils = {
orders: T[], orders: T[],
feeOrders: T[], feeOrders: T[],
opts?: FindFeeOrdersThatCoverFeesForTargetOrdersOpts, opts?: FindFeeOrdersThatCoverFeesForTargetOrdersOpts,
): { resultOrders: T[]; remainingFeeAmount: BigNumber } { ): { resultFeeOrders: T[]; remainingFeeAmount: BigNumber } {
assert.doesConformToSchema('orders', orders, schemas.ordersSchema); assert.doesConformToSchema('orders', orders, schemas.ordersSchema);
assert.doesConformToSchema('feeOrders', feeOrders, schemas.ordersSchema); assert.doesConformToSchema('feeOrders', feeOrders, schemas.ordersSchema);
// try to get remainingFillableMakerAssetAmounts from opts, if it's not there, use makerAssetAmount values from orders // try to get remainingFillableMakerAssetAmounts from opts, if it's not there, use makerAssetAmount values from orders
@@ -137,7 +137,7 @@ export const marketUtils = {
}, },
); );
return { return {
resultOrders, resultFeeOrders: resultOrders,
remainingFeeAmount: remainingFillAmount, remainingFeeAmount: remainingFillAmount,
}; };
// TODO: add more orders here to cover rounding // TODO: add more orders here to cover rounding

View File

@@ -5256,9 +5256,9 @@ ethereumjs-wallet@~0.6.0:
utf8 "^3.0.0" utf8 "^3.0.0"
uuid "^3.3.2" uuid "^3.3.2"
ethers@3.0.22: ethers@0xproject/ethers.js#eip-838-reasons, ethers@3.0.22:
version "3.0.22" version "3.0.18"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-3.0.22.tgz#7fab1ea16521705837aa43c15831877b2716b436" resolved "https://codeload.github.com/0xproject/ethers.js/tar.gz/b91342bd200d142af0165d6befddf783c8ae8447"
dependencies: dependencies:
aes-js "3.0.0" aes-js "3.0.0"
bn.js "^4.4.0" bn.js "^4.4.0"