diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-09-15 18:59:23 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-09-15 20:14:48 +0800 |
commit | f1a22e9bd7943bc9cb8d8308daca0c60af6e0039 (patch) | |
tree | 91f3e8f54103156500518ad521816e6df409d1d1 /packages/asset-buyer | |
parent | 7b46cef83dca0a743bd598a70076004983cbf294 (diff) | |
download | dexon-sol-tools-f1a22e9bd7943bc9cb8d8308daca0c60af6e0039.tar.gz dexon-sol-tools-f1a22e9bd7943bc9cb8d8308daca0c60af6e0039.tar.zst dexon-sol-tools-f1a22e9bd7943bc9cb8d8308daca0c60af6e0039.zip |
Flesh out the AssetBuyer class
Diffstat (limited to 'packages/asset-buyer')
-rw-r--r-- | packages/asset-buyer/src/asset_buyer.ts | 227 | ||||
-rw-r--r-- | packages/asset-buyer/src/asset_buyers/asset_buyer.ts | 129 | ||||
-rw-r--r-- | packages/asset-buyer/src/forwarder_helper_factory.ts | 1 | ||||
-rw-r--r-- | packages/asset-buyer/src/index.ts | 15 | ||||
-rw-r--r-- | packages/asset-buyer/src/types.ts | 50 | ||||
-rw-r--r-- | packages/asset-buyer/src/utils/assert.ts | 23 | ||||
-rw-r--r-- | packages/asset-buyer/src/utils/buy_quote_calculator.ts | 60 | ||||
-rw-r--r-- | packages/asset-buyer/src/utils/forwarder_helper_impl_config_utils.ts | 92 | ||||
-rw-r--r-- | packages/asset-buyer/src/utils/order_fetcher_response_processor.ts | 180 |
9 files changed, 544 insertions, 233 deletions
diff --git a/packages/asset-buyer/src/asset_buyer.ts b/packages/asset-buyer/src/asset_buyer.ts new file mode 100644 index 000000000..9cbdd8df6 --- /dev/null +++ b/packages/asset-buyer/src/asset_buyer.ts @@ -0,0 +1,227 @@ +import { ContractWrappers } from '@0xproject/contract-wrappers'; +import { assetDataUtils, marketUtils } from '@0xproject/order-utils'; +import { SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { Provider } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { constants } from './constants'; +import { + AssetBuyerError, + AssetBuyerOrdersAndFillableAmounts, + BuyQuote, + OrderFetcher, + OrderFetcherResponse, +} from './types'; +import { assert } from './utils/assert'; +import { buyQuoteCalculator } from './utils/buy_quote_calculator'; +import { orderFetcherResponseProcessor } from './utils/order_fetcher_response_processor'; + +const SLIPPAGE_PERCENTAGE = 0.2; // 20% slippage protection, possibly move this into request interface +const DEFAULT_ORDER_REFRESH_INTERVAL_MS = 10000; // 10 seconds +const DEFAULT_FEE_PERCENTAGE = 0; +const ETHER_TOKEN_DECIMALS = 18; + +export class AssetBuyer { + public readonly provider: Provider; + public readonly assetData: string; + public readonly orderFetcher: OrderFetcher; + public readonly networkId: number; + public readonly orderRefreshIntervalMs: number; + private _contractWrappers: ContractWrappers; + private _lastRefreshTimeIfExists?: number; + private _currentOrdersAndFillableAmountsIfExists?: AssetBuyerOrdersAndFillableAmounts; + /** + * Instantiates a new AssetBuyer instance + * @param provider The Provider instance you would like to use for interacting with the Ethereum network. + * @param assetData The assetData of the desired asset to buy (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md). + * @param orderFetcher An object that conforms to OrderFetcher, see type for definition. + * @param networkId The ethereum network id. Defaults to 1 (mainnet). + * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states. + * Defaults to 10000ms (10s). + * @return An instance of AssetBuyer + */ + constructor( + provider: Provider, + assetData: string, + orderFetcher: OrderFetcher, + networkId: number = constants.MAINNET_NETWORK_ID, + orderRefreshIntervalMs: number = DEFAULT_ORDER_REFRESH_INTERVAL_MS, + ) { + assert.isWeb3Provider('provider', provider); + assert.isString('assetData', assetData); + assert.isValidOrderFetcher('orderFetcher', orderFetcher); + assert.isNumber('networkId', networkId); + assert.isNumber('orderRefreshIntervalMs', orderRefreshIntervalMs); + this.provider = provider; + this.assetData = assetData; + this.orderFetcher = orderFetcher; + this.networkId = networkId; + this.orderRefreshIntervalMs = orderRefreshIntervalMs; + this._contractWrappers = new ContractWrappers(this.provider, { + networkId, + }); + } + /** + * Get a BuyQuote containing all information relevant to fulfilling a buy. + * Pass the BuyQuote to executeBuyQuoteAsync to execute the buy. + * @param assetBuyAmount The amount of asset to buy. + * @param feePercentage The affiliate fee percentage. Defaults to 0. + * @param forceOrderRefresh If set to true, new orders and state will be fetched instead of waiting for + * the next orderRefreshIntervalMs. Defaults to false. + * @return An object that conforms to BuyQuote that satisfies the request. See type definition for more information. + */ + public async getBuyQuoteAsync( + assetBuyAmount: BigNumber, + feePercentage: number = DEFAULT_FEE_PERCENTAGE, + forceOrderRefresh: boolean = false, + ): Promise<BuyQuote> { + assert.isBigNumber('assetBuyAmount', assetBuyAmount); + assert.isNumber('feePercentage', feePercentage); + assert.isBoolean('forceOrderRefresh', forceOrderRefresh); + // we should refresh if: + // we do not have any orders OR + // we are forced to OR + // we have some last refresh time AND that time was sufficiently long ago + const shouldRefresh = + _.isUndefined(this._currentOrdersAndFillableAmountsIfExists) || + forceOrderRefresh || + (!_.isUndefined(this._lastRefreshTimeIfExists) && + this._lastRefreshTimeIfExists + this.orderRefreshIntervalMs < Date.now()); + let ordersAndFillableAmounts: AssetBuyerOrdersAndFillableAmounts; + if (shouldRefresh) { + ordersAndFillableAmounts = await this._getLatestOrdersAndFillableAmountsAsync(); + this._lastRefreshTimeIfExists = Date.now(); + this._currentOrdersAndFillableAmountsIfExists = ordersAndFillableAmounts; + } else { + // it is safe to cast to AssetBuyerOrdersAndFillableAmounts because shouldRefresh catches the undefined case above + ordersAndFillableAmounts = this + ._currentOrdersAndFillableAmountsIfExists as AssetBuyerOrdersAndFillableAmounts; + } + // TODO: optimization + // make the slippage percentage customizable by integrator + const buyQuote = buyQuoteCalculator.calculate( + ordersAndFillableAmounts, + assetBuyAmount, + feePercentage, + SLIPPAGE_PERCENTAGE, + ); + return buyQuote; + } + /** + * Given a BuyQuote and desired rate, attempt to execute the buy. + * @param buyQuote An object that conforms to BuyQuote. See type definition for more information. + * @param rate The desired rate to execute the buy at. Affects the amount of ETH sent with the transaction, defaults to buyQuote.maxRate. + * @param takerAddress The address to perform the buy. Defaults to the first available address from the provider. + * @param feeRecipient The address where affiliate fees are sent. Defaults to null address (0x000...000). + * @return A promise of the txHash. + */ + public async executeBuyQuoteAsync( + buyQuote: BuyQuote, + rate?: BigNumber, + takerAddress?: string, + feeRecipient: string = constants.NULL_ADDRESS, + ): Promise<string> { + assert.isValidBuyQuote('buyQuote', buyQuote); + if (!_.isUndefined(rate)) { + assert.isBigNumber('rate', rate); + } + if (!_.isUndefined(takerAddress)) { + assert.isETHAddressHex('takerAddress', takerAddress); + } + assert.isETHAddressHex('feeRecipient', feeRecipient); + const { orders, feeOrders, feePercentage, assetBuyAmount, maxRate } = buyQuote; + // if no takerAddress is provided, try to get one from the provider + let finalTakerAddress; + if (!_.isUndefined(takerAddress)) { + finalTakerAddress = takerAddress; + } else { + const web3Wrapper = new Web3Wrapper(this.provider); + const availableAddresses = await web3Wrapper.getAvailableAddressesAsync(); + const firstAvailableAddress = _.head(availableAddresses); + if (!_.isUndefined(firstAvailableAddress)) { + finalTakerAddress = firstAvailableAddress; + } else { + throw new Error(AssetBuyerError.NoAddressAvailable); + } + } + // if no rate is provided, default to the maxRate from buyQuote + const desiredRate = rate || maxRate; + // calculate how much eth is required to buy assetBuyAmount at the desired rate + const ethAmount = assetBuyAmount.dividedToIntegerBy(desiredRate); + // TODO: critical + // update the forwarder wrapper to take in feePercentage as a number instead of a BigNumber, verify with Amir that this is being done correctly + const feePercentageBigNumber = !_.isUndefined(feePercentage) + ? Web3Wrapper.toBaseUnitAmount(new BigNumber(1), ETHER_TOKEN_DECIMALS).mul(feePercentage) + : constants.ZERO_AMOUNT; + const txHash = await this._contractWrappers.forwarder.marketBuyOrdersWithEthAsync( + orders, + assetBuyAmount, + finalTakerAddress, + ethAmount, + feeOrders, + feePercentageBigNumber, + feeRecipient, + ); + return txHash; + } + /** + * Ask the order fetcher for orders and process them. + */ + private async _getLatestOrdersAndFillableAmountsAsync(): Promise<AssetBuyerOrdersAndFillableAmounts> { + // find ether token asset data + const etherTokenAssetData = this._getEtherTokenAssetData(); + // find zrx token asset data + const zrxTokenAssetData = this._getZrxTokenAssetData(); + // construct order fetcher requests + const targetOrderFetcherRequest = { + makerAssetData: this.assetData, + takerAssetData: etherTokenAssetData, + networkId: this.networkId, + }; + const feeOrderFetcherRequest = { + makerAssetData: zrxTokenAssetData, + takerAssetData: etherTokenAssetData, + networkId: this.networkId, + }; + const requests = [targetOrderFetcherRequest, feeOrderFetcherRequest]; + // fetch orders and possible fillable amounts + const [targetOrderFetcherResponse, feeOrderFetcherResponse] = await Promise.all( + _.map(requests, request => this.orderFetcher.fetchOrdersAsync(request)), + ); + // process the responses into one object + const ordersAndFillableAmounts = await orderFetcherResponseProcessor.processAsync( + targetOrderFetcherResponse, + feeOrderFetcherResponse, + zrxTokenAssetData, + this._contractWrappers.orderValidator, + ); + return ordersAndFillableAmounts; + } + /** + * Get the assetData that represents the WETH token. + * Will throw if WETH does not exist for the current network. + */ + private _getEtherTokenAssetData(): string { + const etherTokenAddressIfExists = this._contractWrappers.etherToken.getContractAddressIfExists(); + if (_.isUndefined(etherTokenAddressIfExists)) { + throw new Error(AssetBuyerError.NoEtherTokenContractFound); + } + const etherTokenAssetData = assetDataUtils.encodeERC20AssetData(etherTokenAddressIfExists); + return etherTokenAssetData; + } + /** + * Get the assetData that represents the ZRX token. + * Will throw if ZRX does not exist for the current network. + */ + private _getZrxTokenAssetData(): string { + let zrxTokenAssetData: string; + try { + zrxTokenAssetData = this._contractWrappers.exchange.getZRXAssetData(); + } catch (err) { + throw new Error(AssetBuyerError.NoZrxTokenContractFound); + } + return zrxTokenAssetData; + } +} diff --git a/packages/asset-buyer/src/asset_buyers/asset_buyer.ts b/packages/asset-buyer/src/asset_buyers/asset_buyer.ts deleted file mode 100644 index eb7f85e2b..000000000 --- a/packages/asset-buyer/src/asset_buyers/asset_buyer.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { ContractWrappers } from '@0xproject/contract-wrappers'; -import { marketUtils } from '@0xproject/order-utils'; -import { SignedOrder } from '@0xproject/types'; -import { BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; -import * as _ from 'lodash'; -import { Provider } from 'ethereum-types'; - -import { constants } from '../constants'; -import { AssetBuyerError, BuyQuote, BuyQuoteRequest } from '../types'; - -const SLIPPAGE_PERCENTAGE = new BigNumber(0.2); // 20% slippage protection, possibly move this into request interface - -export interface AssetBuyerConfig { - orders: SignedOrder[]; - feeOrders: SignedOrder[]; - remainingFillableMakerAssetAmounts?: BigNumber[]; - remainingFillableFeeAmounts?: BigNumber[]; - networkId?: number; -} - -export class AssetBuyer { - public readonly provider: Provider; - public readonly config: AssetBuyerConfig; - private _contractWrappers: ContractWrappers; - constructor(provider: Provider, config: AssetBuyerConfig) { - this.provider = provider; - this.config = config; - const networkId = this.config.networkId || constants.MAINNET_NETWORK_ID; - this._contractWrappers = new ContractWrappers(this.provider, { - networkId, - }); - } - /** - * Given a BuyQuoteRequest, returns a BuyQuote containing all information relevant to fulfilling the buy. Pass the BuyQuote - * to executeBuyQuoteAsync to execute the buy. - * @param buyQuoteRequest An object that conforms to BuyQuoteRequest. See type definition for more information. - * @return An object that conforms to BuyQuote that satisfies the request. See type definition for more information. - */ - public getBuyQuote(buyQuoteRequest: BuyQuoteRequest): BuyQuote { - const { assetBuyAmount, feePercentage } = buyQuoteRequest; - const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = this.config; - // TODO: optimization - // make the slippage percentage customizable - const slippageBufferAmount = assetBuyAmount.mul(SLIPPAGE_PERCENTAGE).round(); - const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( - orders, - assetBuyAmount, - { - remainingFillableMakerAssetAmounts, - slippageBufferAmount, - }, - ); - if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) { - throw new Error(AssetBuyerError.InsufficientAssetLiquidity); - } - // TODO: optimization - // 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, - feeOrders, - { - remainingFillableMakerAssetAmounts, - remainingFillableFeeAmounts, - }, - ); - if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) { - throw new Error(AssetBuyerError.InsufficientZrxLiquidity); - } - const assetData = orders[0].makerAssetData; - // TODO: critical - // calculate minRate and maxRate by calculating min and max eth usage and then dividing into - // assetBuyAmount to get assetData / WETH - return { - assetData, - orders: resultOrders, - feeOrders: resultFeeOrders, - minRate: constants.ZERO_AMOUNT, - maxRate: constants.ZERO_AMOUNT, - assetBuyAmount, - feePercentage, - }; - } - /** - * Given a BuyQuote and desired rate, attempt to execute the buy. - * @param buyQuote An object that conforms to BuyQuote. See type definition for more information. - * @param rate The desired rate to execute the buy at. Affects the amount of ETH sent with the transaction, defaults to buyQuote.maxRate. - * @param takerAddress The address to perform the buy. Defaults to the first available address from the provider. - * @param feeRecipient The address where affiliate fees are sent. Defaults to null address (0x000...000). - * @return A promise of the txHash. - */ - public async executeBuyQuoteAsync( - buyQuote: BuyQuote, - rate?: BigNumber, - takerAddress?: string, - feeRecipient: string = constants.NULL_ADDRESS, - ): Promise<string> { - const { orders, feeOrders, feePercentage, assetBuyAmount, maxRate } = buyQuote; - // if no takerAddress is provided, try to get one from the provider - let finalTakerAddress; - if (!_.isUndefined(takerAddress)) { - finalTakerAddress = takerAddress; - } else { - const web3Wrapper = new Web3Wrapper(this.provider); - const availableAddresses = await web3Wrapper.getAvailableAddressesAsync(); - const firstAvailableAddress = _.head(availableAddresses); - if (!_.isUndefined(firstAvailableAddress)) { - finalTakerAddress = firstAvailableAddress; - } else { - throw new Error(AssetBuyerError.NoAddressAvailable); - } - } - // if no rate is provided, default to the maxRate from buyQuote - const desiredRate = rate || maxRate; - // calculate how much eth is required to buy assetBuyAmount at the desired rate - const ethAmount = assetBuyAmount.dividedToIntegerBy(desiredRate); - const txHash = await this._contractWrappers.forwarder.marketBuyOrdersWithEthAsync( - orders, - assetBuyAmount, - finalTakerAddress, - ethAmount, - feeOrders, - feePercentage, - feeRecipient, - ); - return txHash; - } -} diff --git a/packages/asset-buyer/src/forwarder_helper_factory.ts b/packages/asset-buyer/src/forwarder_helper_factory.ts index 9a3832e81..4c4adfda0 100644 --- a/packages/asset-buyer/src/forwarder_helper_factory.ts +++ b/packages/asset-buyer/src/forwarder_helper_factory.ts @@ -89,6 +89,7 @@ // } catch (err) { // throw new Error(ForwarderHelperFactoryError.StandardRelayerApiError); // } + // // validate orders and find remaining fillable from on chain state or sra api // let ordersAndRemainingFillableMakerAssetAmounts: OrdersAndRemainingFillableMakerAssetAmounts; // let feeOrdersAndRemainingFillableMakerAssetAmounts: OrdersAndRemainingFillableMakerAssetAmounts; diff --git a/packages/asset-buyer/src/index.ts b/packages/asset-buyer/src/index.ts index 299b32edd..67ad06084 100644 --- a/packages/asset-buyer/src/index.ts +++ b/packages/asset-buyer/src/index.ts @@ -1,2 +1,13 @@ -export { AssetBuyerError, BuyQuote, BuyQuoteRequest } from './types'; -export { AssetBuyer } from './asset_buyers/asset_buyer'; +export { Provider } from 'ethereum-types'; +export { SignedOrder } from '@0xproject/types'; +export { BigNumber } from '@0xproject/utils'; + +export { AssetBuyer } from './asset_buyer'; +export { + AssetBuyerError, + BuyQuote, + OrderFetcher, + OrderFetcherRequest, + OrderFetcherResponse, + SignedOrderWithRemainingFillableMakerAssetAmount, +} from './types'; diff --git a/packages/asset-buyer/src/types.ts b/packages/asset-buyer/src/types.ts index 8a12d0cf8..0da30f48d 100644 --- a/packages/asset-buyer/src/types.ts +++ b/packages/asset-buyer/src/types.ts @@ -2,22 +2,45 @@ import { SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; /** - * assetBuyAmount: The amount of asset to buy. - * feePercentage: Optional affiliate percentage amount factoring into eth amount calculations. + * makerAssetData: The assetData representing the desired makerAsset. + * takerAssetData: The assetData representing the desired takerAsset. + * networkId: The networkId that the desired orders should be for. */ -export interface BuyQuoteRequest { - assetBuyAmount: BigNumber; - feePercentage?: BigNumber; +export interface OrderFetcherRequest { + makerAssetData: string; + takerAssetData: string; + networkId: number; +} + +/** + * orders: An array of orders with optional remaining fillable makerAsset amounts. See type for more info. + */ +export interface OrderFetcherResponse { + orders: SignedOrderWithRemainingFillableMakerAssetAmount[]; +} + +/** + * A normal SignedOrder with one extra optional property `remainingFillableMakerAssetAmount` + * remainingFillableMakerAssetAmount: The amount of the makerAsset that is available to be filled + */ +export interface SignedOrderWithRemainingFillableMakerAssetAmount extends SignedOrder { + remainingFillableMakerAssetAmount?: BigNumber; +} +/** + * Given an OrderFetchRequest, get an OrderFetchResponse. + */ +export interface OrderFetcher { + fetchOrdersAsync: (orderFetchRequest: OrderFetcherRequest) => Promise<OrderFetcherResponse>; } /** - * assetData: The asset information. + * assetData: String that represents a specific asset (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md). * orders: An array of objects conforming to SignedOrder. These orders can be used to cover the requested assetBuyAmount plus slippage. * feeOrders: An array of objects conforming to SignedOrder. These orders can be used to cover the fees for the orders param above. * minRate: Min rate that needs to be paid in order to execute the buy. * maxRate: Max rate that can be paid in order to execute the buy. - * assetBuyAmount: The amount of asset to buy. Passed through directly from the request. - * feePercentage: Affiliate fee percentage used to calculate the eth amounts above. Passed through directly from the request. + * assetBuyAmount: The amount of asset to buy. + * feePercentage: Optional affiliate fee percentage used to calculate the eth amounts above. */ export interface BuyQuote { assetData: string; @@ -26,11 +49,11 @@ export interface BuyQuote { minRate: BigNumber; maxRate: BigNumber; assetBuyAmount: BigNumber; - feePercentage?: BigNumber; + feePercentage?: number; } /** - * Possible errors thrown by an AssetBuyer instance or associated static methods + * Possible errors thrown by an AssetBuyer instance or associated static methods. */ export enum AssetBuyerError { NoEtherTokenContractFound = 'NO_ETHER_TOKEN_CONTRACT_FOUND', @@ -40,3 +63,10 @@ export enum AssetBuyerError { InsufficientZrxLiquidity = 'INSUFFICIENT_ZRX_LIQUIDITY', NoAddressAvailable = 'NO_ADDRESS_AVAILABLE', } + +export interface AssetBuyerOrdersAndFillableAmounts { + orders: SignedOrderWithRemainingFillableMakerAssetAmount[]; + feeOrders: SignedOrderWithRemainingFillableMakerAssetAmount[]; + remainingFillableMakerAssetAmounts: BigNumber[]; + remainingFillableFeeAmounts: BigNumber[]; +} diff --git a/packages/asset-buyer/src/utils/assert.ts b/packages/asset-buyer/src/utils/assert.ts new file mode 100644 index 000000000..c4d611477 --- /dev/null +++ b/packages/asset-buyer/src/utils/assert.ts @@ -0,0 +1,23 @@ +import { assert as sharedAssert } from '@0xproject/assert'; +import { schemas } from '@0xproject/json-schemas'; +import * as _ from 'lodash'; + +import { BuyQuote, OrderFetcher } from '../types'; + +export const assert = { + ...sharedAssert, + isValidBuyQuote(variableName: string, buyQuote: BuyQuote): void { + sharedAssert.isHexString(`${variableName}.assetData`, buyQuote.assetData); + sharedAssert.doesConformToSchema(`${variableName}.orders`, buyQuote.orders, schemas.signedOrdersSchema); + sharedAssert.doesConformToSchema(`${variableName}.feeOrders`, buyQuote.feeOrders, schemas.signedOrdersSchema); + sharedAssert.isBigNumber(`${variableName}.minRate`, buyQuote.minRate); + sharedAssert.isBigNumber(`${variableName}.maxRate`, buyQuote.maxRate); + sharedAssert.isBigNumber(`${variableName}.assetBuyAmount`, buyQuote.assetBuyAmount); + if (!_.isUndefined(buyQuote.feePercentage)) { + sharedAssert.isNumber(`${variableName}.feePercentage`, buyQuote.feePercentage); + } + }, + isValidOrderFetcher(variableName: string, orderFetcher: OrderFetcher): void { + sharedAssert.isFunction(`${variableName}.fetchOrdersAsync`, orderFetcher.fetchOrdersAsync); + }, +}; diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts new file mode 100644 index 000000000..e05ab1e55 --- /dev/null +++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts @@ -0,0 +1,60 @@ +import { marketUtils } from '@0xproject/order-utils'; +import { BigNumber } from '@0xproject/utils'; + +import { constants } from '../constants'; +import { AssetBuyerError, AssetBuyerOrdersAndFillableAmounts, BuyQuote } from '../types'; + +export const buyQuoteCalculator = { + calculate( + ordersAndFillableAmounts: AssetBuyerOrdersAndFillableAmounts, + assetBuyAmount: BigNumber, + feePercentage: number, + slippagePercentage: number, + ): BuyQuote { + const { + orders, + feeOrders, + remainingFillableMakerAssetAmounts, + remainingFillableFeeAmounts, + } = ordersAndFillableAmounts; + const slippageBufferAmount = assetBuyAmount.mul(slippagePercentage).round(); + const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( + orders, + assetBuyAmount, + { + remainingFillableMakerAssetAmounts, + slippageBufferAmount, + }, + ); + if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) { + throw new Error(AssetBuyerError.InsufficientAssetLiquidity); + } + // TODO: optimization + // 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, + feeOrders, + { + remainingFillableMakerAssetAmounts, + remainingFillableFeeAmounts, + }, + ); + if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) { + throw new Error(AssetBuyerError.InsufficientZrxLiquidity); + } + const assetData = orders[0].makerAssetData; + // TODO: critical + // calculate minRate and maxRate by calculating min and max eth usage and then dividing into + // assetBuyAmount to get assetData / WETH, needs to take into account feePercentage as well + return { + assetData, + orders: resultOrders, + feeOrders: resultFeeOrders, + minRate: constants.ZERO_AMOUNT, + maxRate: constants.ZERO_AMOUNT, + assetBuyAmount, + feePercentage, + }; + }, +}; diff --git a/packages/asset-buyer/src/utils/forwarder_helper_impl_config_utils.ts b/packages/asset-buyer/src/utils/forwarder_helper_impl_config_utils.ts deleted file mode 100644 index d3cbb651a..000000000 --- a/packages/asset-buyer/src/utils/forwarder_helper_impl_config_utils.ts +++ /dev/null @@ -1,92 +0,0 @@ -// import { sortingUtils } from '@0xproject/order-utils'; -// import { SignedOrder } from '@0xproject/types'; -// import { BigNumber } from '@0xproject/utils'; -// import * as _ from 'lodash'; - -// import { ForwarderHelperImplConfig } from '@0xproject/asset-buyer/src/asset_buyer'; - -// interface SignedOrderWithAmount extends SignedOrder { -// remainingFillAmount: BigNumber; -// } - -// export const forwarderHelperImplConfigUtils = { -// sortedConfig(config: ForwarderHelperImplConfig): ForwarderHelperImplConfig { -// const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = config; -// // TODO: provide a feeRate to the sorting function to more accurately sort based on the current market for ZRX tokens -// const orderSorter = (ordersToSort: SignedOrder[]) => { -// return sortingUtils.sortOrdersByFeeAdjustedRate(ordersToSort); -// }; -// const sortOrdersResult = sortOrdersAndRemainingFillAmounts( -// orderSorter, -// orders, -// remainingFillableMakerAssetAmounts, -// ); -// const feeOrderSorter = (ordersToSort: SignedOrder[]) => { -// return sortingUtils.sortFeeOrdersByFeeAdjustedRate(ordersToSort); -// }; -// const sortFeeOrdersResult = sortOrdersAndRemainingFillAmounts( -// feeOrderSorter, -// feeOrders, -// remainingFillableFeeAmounts, -// ); -// return { -// orders: sortOrdersResult.orders, -// feeOrders: sortFeeOrdersResult.orders, -// remainingFillableMakerAssetAmounts: sortOrdersResult.remainingFillAmounts, -// remainingFillableFeeAmounts: sortFeeOrdersResult.remainingFillAmounts, -// }; -// }, -// }; - -// type OrderSorter = (orders: SignedOrder[]) => SignedOrder[]; - -// function sortOrdersAndRemainingFillAmounts( -// orderSorter: OrderSorter, -// orders: SignedOrder[], -// remainingFillAmounts?: BigNumber[], -// ): { orders: SignedOrder[]; remainingFillAmounts?: BigNumber[] } { -// if (!_.isUndefined(remainingFillAmounts)) { -// // Bundle orders together with their remainingFillAmounts so that we can sort them together -// const orderWithAmounts = bundleSignedOrderWithAmounts(orders, remainingFillAmounts); -// // Sort -// const sortedOrderWithAmounts = orderSorter(orderWithAmounts) as SignedOrderWithAmount[]; -// // Unbundle after sorting -// const unbundledSortedOrderWithAmounts = unbundleSignedOrderWithAmounts(sortedOrderWithAmounts); -// return { -// orders: unbundledSortedOrderWithAmounts.orders, -// remainingFillAmounts: unbundledSortedOrderWithAmounts.amounts, -// }; -// } else { -// const sortedOrders = orderSorter(orders); -// return { -// orders: sortedOrders, -// }; -// } -// } - -// function bundleSignedOrderWithAmounts(orders: SignedOrder[], amounts: BigNumber[]): SignedOrderWithAmount[] { -// const ordersAndAmounts = _.map(orders, (order, index) => { -// return { -// ...order, -// remainingFillAmount: amounts[index], -// }; -// }); -// return ordersAndAmounts; -// } - -// function unbundleSignedOrderWithAmounts( -// signedOrderWithAmounts: SignedOrderWithAmount[], -// ): { orders: SignedOrder[]; amounts: BigNumber[] } { -// const orders = _.map(signedOrderWithAmounts, order => { -// const { remainingFillAmount, ...rest } = order; -// return rest; -// }); -// const amounts = _.map(signedOrderWithAmounts, order => { -// const { remainingFillAmount } = order; -// return remainingFillAmount; -// }); -// return { -// orders, -// amounts, -// }; -// } diff --git a/packages/asset-buyer/src/utils/order_fetcher_response_processor.ts b/packages/asset-buyer/src/utils/order_fetcher_response_processor.ts new file mode 100644 index 000000000..04c5355eb --- /dev/null +++ b/packages/asset-buyer/src/utils/order_fetcher_response_processor.ts @@ -0,0 +1,180 @@ +import { OrderAndTraderInfo, OrderStatus, OrderValidatorWrapper } from '@0xproject/contract-wrappers'; +import { sortingUtils } from '@0xproject/order-utils'; +import { RemainingFillableCalculator } from '@0xproject/order-utils/lib/src/remaining_fillable_calculator'; +import { SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { constants } from '../constants'; +import { + AssetBuyerOrdersAndFillableAmounts, + OrderFetcherResponse, + SignedOrderWithRemainingFillableMakerAssetAmount, +} from '../types'; + +import { orderUtils } from './order_utils'; + +interface OrdersAndRemainingFillableMakerAssetAmounts { + orders: SignedOrder[]; + remainingFillableMakerAssetAmounts: BigNumber[]; +} + +export const orderFetcherResponseProcessor = { + /** + * Take the responses for the target orders to buy and fee orders and process them. + * Processing includes: + * - Drop orders that are expired or not open orders (null taker address) + * - If shouldValidateOnChain, attempt to grab fillable amounts from on-chain otherwise assume completely fillable + * - Sort by rate + */ + async processAsync( + targetOrderFetcherResponse: OrderFetcherResponse, + feeOrderFetcherResponse: OrderFetcherResponse, + zrxTokenAssetData: string, + orderValidator?: OrderValidatorWrapper, + ): Promise<AssetBuyerOrdersAndFillableAmounts> { + // drop orders that are expired or not open + const filteredTargetOrders = filterOutExpiredAndNonOpenOrders(targetOrderFetcherResponse.orders); + const filteredFeeOrders = filterOutExpiredAndNonOpenOrders(feeOrderFetcherResponse.orders); + // set the orders to be sorted equal to the filtered orders + let unsortedTargetOrders = filteredTargetOrders; + let unsortedFeeOrders = filteredFeeOrders; + // if an orderValidator is provided, use on chain information to calculate remaining fillable makerAsset amounts + if (!_.isUndefined(orderValidator)) { + // TODO: critical + // try catch these requests and throw a more domain specific error + // TODO: optimization + // reduce this to once RPC call buy combining orders into one array and then splitting up the response + const [targetOrdersAndTradersInfo, feeOrdersAndTradersInfo] = await Promise.all( + _.map([filteredTargetOrders, filteredFeeOrders], ordersToBeValidated => { + const takerAddresses = _.map(ordersToBeValidated, () => constants.NULL_ADDRESS); + return orderValidator.getOrdersAndTradersInfoAsync(ordersToBeValidated, takerAddresses); + }), + ); + // take orders + on chain information and find the valid orders and remaining fillable maker asset amounts + unsortedTargetOrders = getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain( + filteredTargetOrders, + targetOrdersAndTradersInfo, + zrxTokenAssetData, + ); + // take orders + on chain information and find the valid orders and remaining fillable maker asset amounts + unsortedFeeOrders = getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain( + filteredFeeOrders, + feeOrdersAndTradersInfo, + zrxTokenAssetData, + ); + } + // sort orders by rate + // TODO: optimization + // provide a feeRate to the sorting function to more accurately sort based on the current market for ZRX tokens + const sortedTargetOrders = sortingUtils.sortOrdersByFeeAdjustedRate(unsortedTargetOrders); + const sortedFeeOrders = sortingUtils.sortFeeOrdersByFeeAdjustedRate(unsortedFeeOrders); + // unbundle orders and fillable amounts and compile final result + const targetOrdersAndRemainingFillableMakerAssetAmounts = unbundleOrdersWithAmounts(sortedTargetOrders); + const feeOrdersAndRemainingFillableMakerAssetAmounts = unbundleOrdersWithAmounts(sortedFeeOrders); + return { + orders: targetOrdersAndRemainingFillableMakerAssetAmounts.orders, + feeOrders: feeOrdersAndRemainingFillableMakerAssetAmounts.orders, + remainingFillableMakerAssetAmounts: + targetOrdersAndRemainingFillableMakerAssetAmounts.remainingFillableMakerAssetAmounts, + remainingFillableFeeAmounts: + feeOrdersAndRemainingFillableMakerAssetAmounts.remainingFillableMakerAssetAmounts, + }; + }, +}; + +/** + * Given an array of orders, return a new array with expired and non open orders filtered out. + */ +function filterOutExpiredAndNonOpenOrders( + orders: SignedOrderWithRemainingFillableMakerAssetAmount[], +): SignedOrderWithRemainingFillableMakerAssetAmount[] { + const result = _.filter(orders, order => { + return orderUtils.isOpenOrder(order) && orderUtils.isOrderExpired(order); + }); + return result; +} + +/** + * Given an array of orders and corresponding on-chain infos, return a subset of the orders + * that are still fillable orders with their corresponding remainingFillableMakerAssetAmounts. + */ +function getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain( + inputOrders: SignedOrder[], + ordersAndTradersInfo: OrderAndTraderInfo[], + zrxAssetData: string, +): SignedOrderWithRemainingFillableMakerAssetAmount[] { + // iterate through the input orders and find the ones that are still fillable + // for the orders that are still fillable, calculate the remaining fillable maker asset amount + const result = _.reduce( + inputOrders, + (accOrders, order, index) => { + // get corresponding on-chain state for the order + const { orderInfo, traderInfo } = ordersAndTradersInfo[index]; + // if the order IS NOT fillable, do not add anything to the accumulations and continue iterating + if (orderInfo.orderStatus !== OrderStatus.FILLABLE) { + return accOrders; + } + // if the order IS fillable, add the order and calculate the remaining fillable amount + const transferrableAssetAmount = BigNumber.min([traderInfo.makerAllowance, traderInfo.makerBalance]); + const transferrableFeeAssetAmount = BigNumber.min([ + traderInfo.makerZrxAllowance, + traderInfo.makerZrxBalance, + ]); + const remainingTakerAssetAmount = order.takerAssetAmount.minus(orderInfo.orderTakerAssetFilledAmount); + const remainingMakerAssetAmount = orderUtils.calculateRemainingMakerAssetAmount( + order, + remainingTakerAssetAmount, + ); + const remainingFillableCalculator = new RemainingFillableCalculator( + order.makerFee, + order.makerAssetAmount, + order.makerAssetData === zrxAssetData, + transferrableAssetAmount, + transferrableFeeAssetAmount, + remainingMakerAssetAmount, + ); + const remainingFillableAmount = remainingFillableCalculator.computeRemainingFillable(); + // if the order does not have any remaining fillable makerAsset, do not add anything to the accumulations and continue iterating + if (remainingFillableAmount.lte(constants.ZERO_AMOUNT)) { + return accOrders; + } + const orderWithRemainingFillableMakerAssetAmount = { + ...order, + remainingFillableMakerAssetAmount: remainingFillableAmount, + }; + const newAccOrders = _.concat(accOrders, orderWithRemainingFillableMakerAssetAmount); + return newAccOrders; + }, + [] as SignedOrderWithRemainingFillableMakerAssetAmount[], + ); + return result; +} + +/** + * Given an array of orders with remaining fillable maker asset amounts. Unbundle into an instance of OrdersAndRemainingFillableMakerAssetAmounts. + * If an order is missing a corresponding remainingFillableMakerAssetAmount, assume it is completely fillable. + */ +function unbundleOrdersWithAmounts( + ordersWithAmounts: SignedOrderWithRemainingFillableMakerAssetAmount[], +): OrdersAndRemainingFillableMakerAssetAmounts { + const result = _.reduce( + ordersWithAmounts, + (acc, orderWithAmount) => { + const { orders, remainingFillableMakerAssetAmounts } = acc; + const { remainingFillableMakerAssetAmount, ...order } = orderWithAmount; + // if we are still missing a remainingFillableMakerAssetAmount, assume the order is completely fillable + const newRemainingAmount = remainingFillableMakerAssetAmount || order.makerAssetAmount; + const newAcc = { + orders: _.concat(orders, order), + remainingFillableMakerAssetAmounts: _.concat(remainingFillableMakerAssetAmounts, newRemainingAmount), + }; + return newAcc; + }, + { + orders: [] as SignedOrder[], + remainingFillableMakerAssetAmounts: [] as BigNumber[], + }, + ); + return result; +} |