From c6d738ed0c7363a1074bab58ece4bdbdc0ac7932 Mon Sep 17 00:00:00 2001 From: Michael Zhu Date: Mon, 7 Sep 2020 20:56:05 -0700 Subject: [PATCH] Refactor asset-swapper --- contracts/zero-ex/package.json | 2 +- contracts/zero-ex/src/artifacts.ts | 2 + contracts/zero-ex/src/wrappers.ts | 1 + contracts/zero-ex/tsconfig.json | 1 + packages/asset-swapper/src/index.ts | 2 +- .../utils/market_operation_utils/constants.ts | 6 + .../src/utils/market_operation_utils/fills.ts | 338 +++++------------- .../src/utils/market_operation_utils/index.ts | 72 ++-- .../market_operation_utils/multihop_utils.ts | 2 +- .../utils/market_operation_utils/orders.ts | 41 +-- .../src/utils/market_operation_utils/path.ts | 287 +++++++++++++++ .../market_operation_utils/path_optimizer.ts | 139 +++---- .../market_operation_utils/rate_utils.ts | 48 +++ .../src/utils/market_operation_utils/types.ts | 21 +- .../src/utils/quote_report_generator.ts | 6 +- .../src/utils/quote_simulation.ts | 2 +- .../src/utils/swap_quote_calculator.ts | 115 +++--- .../test/market_operation_utils_test.ts | 12 +- 18 files changed, 597 insertions(+), 500 deletions(-) create mode 100644 packages/asset-swapper/src/utils/market_operation_utils/path.ts create mode 100644 packages/asset-swapper/src/utils/market_operation_utils/rate_utils.ts diff --git a/contracts/zero-ex/package.json b/contracts/zero-ex/package.json index ee3cac9677..c4f9bbc6c7 100644 --- a/contracts/zero-ex/package.json +++ b/contracts/zero-ex/package.json @@ -39,7 +39,7 @@ "publish:private": "yarn build && gitpkg publish" }, "config": { - "publicInterfaceContracts": "IZeroEx,ZeroEx,FullMigration,InitialMigration,IFlashWallet,IAllowanceTarget,IERC20Transformer,IOwnableFeature,ISimpleFunctionRegistryFeature,ITokenSpenderFeature,ITransformERC20Feature,FillQuoteTransformer,PayTakerTransformer,WethTransformer,OwnableFeature,SimpleFunctionRegistryFeature,TransformERC20Feature,TokenSpenderFeature,AffiliateFeeTransformer,SignatureValidatorFeature,MetaTransactionsFeature,LogMetadataTransformer,BridgeAdapter", + "publicInterfaceContracts": "IZeroEx,ZeroEx,FullMigration,InitialMigration,IFlashWallet,IAllowanceTarget,IERC20Transformer,IOwnableFeature,ISimpleFunctionRegistryFeature,ITokenSpenderFeature,ITransformERC20Feature,FillQuoteTransformer,PayTakerTransformer,WethTransformer,OwnableFeature,SimpleFunctionRegistryFeature,TransformERC20Feature,TokenSpenderFeature,AffiliateFeeTransformer,SignatureValidatorFeature,MetaTransactionsFeature,LogMetadataTransformer,BridgeAdapter,LiquidityProviderFeature", "abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.", "abis": "./test/generated-artifacts/@(AffiliateFeeTransformer|AllowanceTarget|BootstrapFeature|BridgeAdapter|FillQuoteTransformer|FixinCommon|FixinEIP712|FixinReentrancyGuard|FlashWallet|FullMigration|IAllowanceTarget|IBootstrapFeature|IBridgeAdapter|IERC20Bridge|IERC20Transformer|IExchange|IFeature|IFlashWallet|IGasToken|ILiquidityProviderFeature|IMetaTransactionsFeature|IOwnableFeature|ISignatureValidatorFeature|ISimpleFunctionRegistryFeature|ITestSimpleFunctionRegistryFeature|ITokenSpenderFeature|ITransformERC20Feature|IUniswapFeature|IZeroEx|InitialMigration|LibBootstrap|LibCommonRichErrors|LibERC20Transformer|LibLiquidityProviderRichErrors|LibLiquidityProviderStorage|LibMetaTransactionsRichErrors|LibMetaTransactionsStorage|LibMigrate|LibOwnableRichErrors|LibOwnableStorage|LibProxyRichErrors|LibProxyStorage|LibReentrancyGuardStorage|LibSignatureRichErrors|LibSignedCallData|LibSimpleFunctionRegistryRichErrors|LibSimpleFunctionRegistryStorage|LibSpenderRichErrors|LibStorage|LibTokenSpenderStorage|LibTransformERC20RichErrors|LibTransformERC20Storage|LibWalletRichErrors|LiquidityProviderFeature|LogMetadataTransformer|MetaTransactionsFeature|MixinAdapterAddresses|MixinBalancer|MixinCurve|MixinKyber|MixinMStable|MixinMooniswap|MixinOasis|MixinUniswap|MixinUniswapV2|MixinZeroExBridge|OwnableFeature|PayTakerTransformer|SignatureValidatorFeature|SimpleFunctionRegistryFeature|TestCallTarget|TestDelegateCaller|TestFillQuoteTransformerBridge|TestFillQuoteTransformerExchange|TestFillQuoteTransformerHost|TestFullMigration|TestInitialMigration|TestMetaTransactionsTransformERC20Feature|TestMigrator|TestMintTokenERC20Transformer|TestMintableERC20Token|TestSimpleFunctionRegistryFeatureImpl1|TestSimpleFunctionRegistryFeatureImpl2|TestTokenSpender|TestTokenSpenderERC20Token|TestTransformERC20|TestTransformerBase|TestTransformerDeployerTransformer|TestTransformerHost|TestWeth|TestWethTransformerHost|TestZeroExFeature|TokenSpenderFeature|TransformERC20Feature|Transformer|TransformerDeployer|UniswapFeature|WethTransformer|ZeroEx).json" }, diff --git a/contracts/zero-ex/src/artifacts.ts b/contracts/zero-ex/src/artifacts.ts index b51add69e6..9e41b7606f 100644 --- a/contracts/zero-ex/src/artifacts.ts +++ b/contracts/zero-ex/src/artifacts.ts @@ -18,6 +18,7 @@ import * as ISimpleFunctionRegistryFeature from '../generated-artifacts/ISimpleF import * as ITokenSpenderFeature from '../generated-artifacts/ITokenSpenderFeature.json'; import * as ITransformERC20Feature from '../generated-artifacts/ITransformERC20Feature.json'; import * as IZeroEx from '../generated-artifacts/IZeroEx.json'; +import * as LiquidityProviderFeature from '../generated-artifacts/LiquidityProviderFeature.json'; import * as LogMetadataTransformer from '../generated-artifacts/LogMetadataTransformer.json'; import * as MetaTransactionsFeature from '../generated-artifacts/MetaTransactionsFeature.json'; import * as OwnableFeature from '../generated-artifacts/OwnableFeature.json'; @@ -52,4 +53,5 @@ export const artifacts = { MetaTransactionsFeature: MetaTransactionsFeature as ContractArtifact, LogMetadataTransformer: LogMetadataTransformer as ContractArtifact, BridgeAdapter: BridgeAdapter as ContractArtifact, + LiquidityProviderFeature: LiquidityProviderFeature as ContractArtifact, }; diff --git a/contracts/zero-ex/src/wrappers.ts b/contracts/zero-ex/src/wrappers.ts index ea09d3416d..0c88b3fa82 100644 --- a/contracts/zero-ex/src/wrappers.ts +++ b/contracts/zero-ex/src/wrappers.ts @@ -16,6 +16,7 @@ export * from '../generated-wrappers/i_token_spender_feature'; export * from '../generated-wrappers/i_transform_erc20_feature'; export * from '../generated-wrappers/i_zero_ex'; export * from '../generated-wrappers/initial_migration'; +export * from '../generated-wrappers/liquidity_provider_feature'; export * from '../generated-wrappers/log_metadata_transformer'; export * from '../generated-wrappers/meta_transactions_feature'; export * from '../generated-wrappers/ownable_feature'; diff --git a/contracts/zero-ex/tsconfig.json b/contracts/zero-ex/tsconfig.json index de0a013816..0f0163243e 100644 --- a/contracts/zero-ex/tsconfig.json +++ b/contracts/zero-ex/tsconfig.json @@ -16,6 +16,7 @@ "generated-artifacts/ITransformERC20Feature.json", "generated-artifacts/IZeroEx.json", "generated-artifacts/InitialMigration.json", + "generated-artifacts/LiquidityProviderFeature.json", "generated-artifacts/LogMetadataTransformer.json", "generated-artifacts/MetaTransactionsFeature.json", "generated-artifacts/OwnableFeature.json", diff --git a/packages/asset-swapper/src/index.ts b/packages/asset-swapper/src/index.ts index f9029e256b..79e86a3f75 100644 --- a/packages/asset-swapper/src/index.ts +++ b/packages/asset-swapper/src/index.ts @@ -119,6 +119,7 @@ export { SwapQuoterRfqtOpts, } from './types'; export { affiliateFeeUtils } from './utils/affiliate_fee_utils'; +export { SOURCE_FLAGS } from './utils/market_operation_utils/constants'; export { Parameters, SamplerContractCall, @@ -136,7 +137,6 @@ export { FeeSchedule, Fill, FillData, - FillFlags, GetMarketOrdersRfqtOpts, KyberFillData, LiquidityProviderFillData, diff --git a/packages/asset-swapper/src/utils/market_operation_utils/constants.ts b/packages/asset-swapper/src/utils/market_operation_utils/constants.ts index 1b65b5538a..8b4c7dd2e5 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/constants.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/constants.ts @@ -58,6 +58,7 @@ export const DEFAULT_GET_MARKET_ORDERS_OPTS: GetMarketOrdersOpts = { sampleDistributionBase: 1.05, feeSchedule: {}, gasSchedule: {}, + exchangeProxyOverhead: () => ZERO_AMOUNT, allowFallback: true, shouldBatchBridgeOrders: true, shouldGenerateQuoteReport: false, @@ -68,6 +69,11 @@ export const DEFAULT_GET_MARKET_ORDERS_OPTS: GetMarketOrdersOpts = { */ export const FEE_QUOTE_SOURCES = [ERC20BridgeSource.Uniswap, ERC20BridgeSource.UniswapV2]; +export const SOURCE_FLAGS: { [source in ERC20BridgeSource]: number } = Object.assign( + {}, + ...Object.values(ERC20BridgeSource).map((source: ERC20BridgeSource, index) => ({ [source]: 1 << index })), +); + /** * Mainnet Curve configuration */ diff --git a/packages/asset-swapper/src/utils/market_operation_utils/fills.ts b/packages/asset-swapper/src/utils/market_operation_utils/fills.ts index 7ae13d60af..24ce21bbec 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/fills.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/fills.ts @@ -3,15 +3,15 @@ import { BigNumber, hexUtils } from '@0x/utils'; import { MarketOperation, SignedOrderWithFillableAmounts } from '../../types'; import { fillableAmountsUtils } from '../../utils/fillable_amounts_utils'; -import { POSITIVE_INF, ZERO_AMOUNT } from './constants'; -import { CollapsedFill, DexSample, ERC20BridgeSource, FeeSchedule, Fill, FillFlags, MultiHopFillData } from './types'; +import { POSITIVE_INF, SOURCE_FLAGS, ZERO_AMOUNT } from './constants'; +import { DexSample, ERC20BridgeSource, FeeSchedule, Fill } from './types'; // tslint:disable: prefer-for-of no-bitwise completed-docs /** - * Create fill paths from orders and dex quotes. + * Create `Fill` objects from orders and dex quotes. */ -export function createFillPaths(opts: { +export function createFills(opts: { side: MarketOperation; orders?: SignedOrderWithFillableAmounts[]; dexQuotes?: DexSample[][]; @@ -28,30 +28,50 @@ export function createFillPaths(opts: { const dexQuotes = opts.dexQuotes || []; const ethToOutputRate = opts.ethToOutputRate || ZERO_AMOUNT; const ethToInputRate = opts.ethToInputRate || ZERO_AMOUNT; - // Create native fill paths. - const nativePath = nativeOrdersToPath(side, orders, opts.targetInput, ethToOutputRate, ethToInputRate, feeSchedule); - // Create DEX fill paths. - const dexPaths = dexQuotesToPaths(side, dexQuotes, ethToOutputRate, feeSchedule); - return filterPaths([...dexPaths, nativePath].map(p => clipPathToInput(p, opts.targetInput)), excludedSources); + // Create native fills. + const nativeFills = nativeOrdersToFills( + side, + orders, + opts.targetInput, + ethToOutputRate, + ethToInputRate, + feeSchedule, + ); + // Create DEX fills. + const dexFills = dexQuotes.map(singleSourceSamples => + dexSamplesToFills(side, singleSourceSamples, ethToOutputRate, ethToInputRate, feeSchedule), + ); + return [...dexFills, nativeFills] + .map(p => clipFillsToInput(p, opts.targetInput)) + .filter(fills => hasLiquidity(fills) && !excludedSources.includes(fills[0].source)); } -function filterPaths(paths: Fill[][], excludedSources: ERC20BridgeSource[]): Fill[][] { - return paths.filter(path => { - if (path.length === 0) { - return false; +function clipFillsToInput(fills: Fill[], targetInput: BigNumber = POSITIVE_INF): Fill[] { + const clipped: Fill[] = []; + let input = ZERO_AMOUNT; + for (const fill of fills) { + if (input.gte(targetInput)) { + break; } - const [input, output] = getPathSize(path); - if (input.eq(0) || output.eq(0)) { - return false; - } - if (excludedSources.includes(path[0].source)) { - return false; - } - return true; - }); + input = input.plus(fill.input); + clipped.push(fill); + } + return clipped; } -function nativeOrdersToPath( +function hasLiquidity(fills: Fill[]): boolean { + if (fills.length === 0) { + return false; + } + const totalInput = BigNumber.sum(...fills.map(fill => fill.input)); + const totalOutput = BigNumber.sum(...fills.map(fill => fill.output)); + if (totalInput.isZero() || totalOutput.isZero()) { + return false; + } + return true; +} + +function nativeOrdersToFills( side: MarketOperation, orders: SignedOrderWithFillableAmounts[], targetInput: BigNumber = POSITIVE_INF, @@ -61,7 +81,7 @@ function nativeOrdersToPath( ): Fill[] { const sourcePathId = hexUtils.random(); // Create a single path from all orders. - let path: Array = []; + let fills: Array = []; for (const order of orders) { const makerAmount = fillableAmountsUtils.getMakerAssetAmountSwappedAfterOrderFees(order); const takerAmount = fillableAmountsUtils.getTakerAssetAmountSwappedAfterOrderFees(order); @@ -87,13 +107,13 @@ function nativeOrdersToPath( if (adjustedRate.lte(0)) { continue; } - path.push({ + fills.push({ sourcePathId, adjustedRate, adjustedOutput, input: clippedInput, output: clippedOutput, - flags: 0, + flags: SOURCE_FLAGS[ERC20BridgeSource.Native], index: 0, // TBD parent: undefined, // TBD source: ERC20BridgeSource.Native, @@ -101,240 +121,56 @@ function nativeOrdersToPath( }); } // Sort by descending adjusted rate. - path = path.sort((a, b) => b.adjustedRate.comparedTo(a.adjustedRate)); + fills = fills.sort((a, b) => b.adjustedRate.comparedTo(a.adjustedRate)); // Re-index fills. - for (let i = 0; i < path.length; ++i) { - path[i].parent = i === 0 ? undefined : path[i - 1]; - path[i].index = i; + for (let i = 0; i < fills.length; ++i) { + fills[i].parent = i === 0 ? undefined : fills[i - 1]; + fills[i].index = i; } - return path; + return fills; } -function dexQuotesToPaths( +function dexSamplesToFills( side: MarketOperation, - dexQuotes: DexSample[][], + samples: DexSample[], ethToOutputRate: BigNumber, + ethToInputRate: BigNumber, fees: FeeSchedule, -): Fill[][] { - const paths: Fill[][] = []; - for (let quote of dexQuotes) { - const sourcePathId = hexUtils.random(); - const path: Fill[] = []; - // Drop any non-zero entries. This can occur if the any fills on Kyber were UniswapReserves - // We need not worry about Kyber fills going to UniswapReserve as the input amount - // we fill is the same as we sampled. I.e we received [0,20,30] output from [1,2,3] input - // and we only fill [2,3] on Kyber (as 1 returns 0 output) - quote = quote.filter(q => !q.output.isZero()); - for (let i = 0; i < quote.length; i++) { - const sample = quote[i]; - const prevSample = i === 0 ? undefined : quote[i - 1]; - const { source, fillData } = sample; - const input = sample.input.minus(prevSample ? prevSample.input : 0); - const output = sample.output.minus(prevSample ? prevSample.output : 0); - const fee = fees[source] === undefined ? 0 : fees[source]!(sample.fillData); - const penalty = - i === 0 // Only the first fill in a DEX path incurs a penalty. - ? ethToOutputRate.times(fee) - : ZERO_AMOUNT; - const adjustedOutput = side === MarketOperation.Sell ? output.minus(penalty) : output.plus(penalty); - - path.push({ - sourcePathId, - input, - output, - adjustedOutput, - source, - fillData, - index: i, - parent: i !== 0 ? path[path.length - 1] : undefined, - flags: sourceToFillFlags(source), - }); +): Fill[] { + const sourcePathId = hexUtils.random(); + const fills: Fill[] = []; + // Drop any non-zero entries. This can occur if the any fills on Kyber were UniswapReserves + // We need not worry about Kyber fills going to UniswapReserve as the input amount + // we fill is the same as we sampled. I.e we received [0,20,30] output from [1,2,3] input + // and we only fill [2,3] on Kyber (as 1 returns 0 output) + samples = samples.filter(q => !q.output.isZero()); + for (let i = 0; i < samples.length; i++) { + const sample = samples[i]; + const prevSample = i === 0 ? undefined : samples[i - 1]; + const { source, fillData } = sample; + const input = sample.input.minus(prevSample ? prevSample.input : 0); + const output = sample.output.minus(prevSample ? prevSample.output : 0); + const fee = fees[source] === undefined ? 0 : fees[source]!(sample.fillData); + let penalty = ZERO_AMOUNT; + if (i === 0) { + // Only the first fill in a DEX path incurs a penalty. + penalty = !ethToOutputRate.isZero() + ? ethToOutputRate.times(fee) + : ethToInputRate.times(fee).times(output.dividedToIntegerBy(input)); } - paths.push(path); - } - return paths; -} + const adjustedOutput = side === MarketOperation.Sell ? output.minus(penalty) : output.plus(penalty); -export function getTwoHopAdjustedRate( - side: MarketOperation, - twoHopQuote: DexSample, - targetInput: BigNumber, - ethToOutputRate: BigNumber, - fees: FeeSchedule = {}, -): BigNumber { - const { output, input, fillData } = twoHopQuote; - if (input.isLessThan(targetInput) || output.isZero()) { - return ZERO_AMOUNT; - } - const penalty = ethToOutputRate.times(fees[ERC20BridgeSource.MultiHop]!(fillData)); - const adjustedOutput = side === MarketOperation.Sell ? output.minus(penalty) : output.plus(penalty); - return side === MarketOperation.Sell ? adjustedOutput.div(input) : input.div(adjustedOutput); -} - -function sourceToFillFlags(source: ERC20BridgeSource): number { - switch (source) { - case ERC20BridgeSource.Uniswap: - return FillFlags.ConflictsWithMultiBridge; - case ERC20BridgeSource.LiquidityProvider: - return FillFlags.ConflictsWithMultiBridge; - case ERC20BridgeSource.MultiBridge: - return FillFlags.MultiBridge; - default: - return 0; - } -} - -export function getPathSize(path: Fill[], targetInput: BigNumber = POSITIVE_INF): [BigNumber, BigNumber] { - let input = ZERO_AMOUNT; - let output = ZERO_AMOUNT; - for (const fill of path) { - if (input.plus(fill.input).gte(targetInput)) { - const di = targetInput.minus(input); - input = input.plus(di); - output = output.plus(fill.output.times(di.div(fill.input))); - break; - } else { - input = input.plus(fill.input); - output = output.plus(fill.output); - } - } - return [input.integerValue(), output.integerValue()]; -} - -export function getPathAdjustedSize(path: Fill[], targetInput: BigNumber = POSITIVE_INF): [BigNumber, BigNumber] { - let input = ZERO_AMOUNT; - let output = ZERO_AMOUNT; - for (const fill of path) { - if (input.plus(fill.input).gte(targetInput)) { - const di = targetInput.minus(input); - if (di.gt(0)) { - input = input.plus(di); - // Penalty does not get interpolated. - const penalty = fill.adjustedOutput.minus(fill.output); - output = output.plus(fill.output.times(di.div(fill.input)).plus(penalty)); - } - break; - } else { - input = input.plus(fill.input); - output = output.plus(fill.adjustedOutput); - } - } - return [input.integerValue(), output.integerValue()]; -} - -export function isValidPath(path: Fill[], skipDuplicateCheck: boolean = false): boolean { - let flags = 0; - for (let i = 0; i < path.length; ++i) { - // Fill must immediately follow its parent. - if (path[i].parent) { - if (i === 0 || path[i - 1] !== path[i].parent) { - return false; - } - } - if (!skipDuplicateCheck) { - // Fill must not be duplicated. - for (let j = 0; j < i; ++j) { - if (path[i] === path[j]) { - return false; - } - } - } - flags |= path[i].flags; - } - return arePathFlagsAllowed(flags); -} - -export function arePathFlagsAllowed(flags: number): boolean { - const multiBridgeConflict = FillFlags.MultiBridge | FillFlags.ConflictsWithMultiBridge; - return (flags & multiBridgeConflict) !== multiBridgeConflict; -} - -export function clipPathToInput(path: Fill[], targetInput: BigNumber = POSITIVE_INF): Fill[] { - const clipped: Fill[] = []; - let input = ZERO_AMOUNT; - for (const fill of path) { - if (input.gte(targetInput)) { - break; - } - input = input.plus(fill.input); - clipped.push(fill); - } - return clipped; -} - -export function collapsePath(path: Fill[]): CollapsedFill[] { - const collapsed: CollapsedFill[] = []; - for (const fill of path) { - const source = fill.source; - if (collapsed.length !== 0 && source !== ERC20BridgeSource.Native) { - const prevFill = collapsed[collapsed.length - 1]; - // If the last fill is from the same source, merge them. - if (prevFill.sourcePathId === fill.sourcePathId) { - prevFill.input = prevFill.input.plus(fill.input); - prevFill.output = prevFill.output.plus(fill.output); - prevFill.fillData = fill.fillData; - prevFill.subFills.push(fill); - continue; - } - } - collapsed.push({ - sourcePathId: fill.sourcePathId, - source: fill.source, - fillData: fill.fillData, - input: fill.input, - output: fill.output, - subFills: [fill], + fills.push({ + sourcePathId, + input, + output, + adjustedOutput, + source, + fillData, + index: i, + parent: i !== 0 ? fills[fills.length - 1] : undefined, + flags: SOURCE_FLAGS[source], }); } - return collapsed; -} - -export function getPathAdjustedCompleteRate(side: MarketOperation, path: Fill[], targetInput: BigNumber): BigNumber { - const [input, output] = getPathAdjustedSize(path, targetInput); - return getCompleteRate(side, input, output, targetInput); -} - -export function getPathAdjustedRate(side: MarketOperation, path: Fill[], targetInput: BigNumber): BigNumber { - const [input, output] = getPathAdjustedSize(path, targetInput); - return getRate(side, input, output); -} - -export function getPathAdjustedSlippage( - side: MarketOperation, - path: Fill[], - inputAmount: BigNumber, - maxRate: BigNumber, -): number { - if (maxRate.eq(0)) { - return 0; - } - const totalRate = getPathAdjustedRate(side, path, inputAmount); - const rateChange = maxRate.minus(totalRate); - return rateChange.div(maxRate).toNumber(); -} - -export function getCompleteRate( - side: MarketOperation, - input: BigNumber, - output: BigNumber, - targetInput: BigNumber, -): BigNumber { - if (input.eq(0) || output.eq(0) || targetInput.eq(0)) { - return ZERO_AMOUNT; - } - // Penalize paths that fall short of the entire input amount by a factor of - // input / targetInput => (i / t) - if (side === MarketOperation.Sell) { - // (o / i) * (i / t) => (o / t) - return output.div(targetInput); - } - // (i / o) * (i / t) - return input.div(output).times(input.div(targetInput)); -} - -export function getRate(side: MarketOperation, input: BigNumber, output: BigNumber): BigNumber { - if (input.eq(0) || output.eq(0)) { - return ZERO_AMOUNT; - } - return side === MarketOperation.Sell ? output.div(input) : input.div(output); + return fills; } diff --git a/packages/asset-swapper/src/utils/market_operation_utils/index.ts b/packages/asset-swapper/src/utils/market_operation_utils/index.ts index dea88d3537..998099b140 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/index.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/index.ts @@ -14,12 +14,12 @@ import { FEE_QUOTE_SOURCES, ONE_ETHER, SELL_SOURCE_FILTER, + SOURCE_FLAGS, ZERO_AMOUNT, } from './constants'; -import { createFillPaths, getPathAdjustedRate, getPathAdjustedSlippage } from './fills'; +import { createFills } from './fills'; import { getBestTwoHopQuote } from './multihop_utils'; import { - createOrdersFromPath, createOrdersFromTwoHopSample, createSignedOrdersFromRfqtIndicativeQuotes, createSignedOrdersWithFillableAmounts, @@ -32,12 +32,12 @@ import { AggregationError, DexSample, ERC20BridgeSource, + ExchangeProxyOverhead, FeeSchedule, GetMarketOrdersOpts, MarketSideLiquidity, OptimizedMarketOrder, OptimizerResult, - OptimizerResultWithReport, OrderDomain, TokenAdjacencyGraph, } from './types'; @@ -359,7 +359,7 @@ export class MarketOperationUtils { nativeOrders: SignedOrder[], takerAmount: BigNumber, opts?: Partial, - ): Promise { + ): Promise { const _opts = { ...DEFAULT_GET_MARKET_ORDERS_OPTS, ...opts }; const marketSideLiquidity = await this.getMarketSellLiquidityAsync(nativeOrders, takerAmount, _opts); const optimizerResult = await this._generateOptimizedOrdersAsync(marketSideLiquidity, { @@ -396,7 +396,7 @@ export class MarketOperationUtils { nativeOrders: SignedOrder[], makerAmount: BigNumber, opts?: Partial, - ): Promise { + ): Promise { const _opts = { ...DEFAULT_GET_MARKET_ORDERS_OPTS, ...opts }; const marketSideLiquidity = await this.getMarketBuyLiquidityAsync(nativeOrders, makerAmount, _opts); const optimizerResult = await this._generateOptimizedOrdersAsync(marketSideLiquidity, { @@ -526,6 +526,7 @@ export class MarketOperationUtils { maxFallbackSlippage?: number; excludedSources?: ERC20BridgeSource[]; feeSchedule?: FeeSchedule; + exchangeProxyOverhead?: ExchangeProxyOverhead; allowFallback?: boolean; shouldBatchBridgeOrders?: boolean; }, @@ -554,8 +555,8 @@ export class MarketOperationUtils { shouldBatchBridgeOrders: !!opts.shouldBatchBridgeOrders, }; - // Convert native orders and dex quotes into fill paths. - const paths = createFillPaths({ + // Convert native orders and dex quotes into `Fill` objects. + const fills = createFills({ side, // Augment native orders with their fillable amounts. orders: [ @@ -571,11 +572,16 @@ export class MarketOperationUtils { }); // Find the optimal path. - let optimalPath = (await findOptimalPathAsync(side, paths, inputAmount, opts.runLimit)) || []; - if (optimalPath.length === 0) { + const optimizerOpts = { + ethToOutputRate, + ethToInputRate, + exchangeProxyOverhead: opts.exchangeProxyOverhead || (() => ZERO_AMOUNT), + }; + let optimalPath = await findOptimalPathAsync(side, fills, inputAmount, opts.runLimit, optimizerOpts); + if (optimalPath === undefined) { throw new Error(AggregationError.NoOptimalPath); } - const optimalPathRate = getPathAdjustedRate(side, optimalPath, inputAmount); + const optimalPathRate = optimalPath.adjustedRate(); const { adjustedRate: bestTwoHopRate, quote: bestTwoHopQuote } = getBestTwoHopQuote( marketSideLiquidity, @@ -583,39 +589,35 @@ export class MarketOperationUtils { ); if (bestTwoHopQuote && bestTwoHopRate.isGreaterThan(optimalPathRate)) { const twoHopOrders = createOrdersFromTwoHopSample(bestTwoHopQuote, orderOpts); - return { optimizedOrders: twoHopOrders, liquidityDelivered: bestTwoHopQuote, isTwoHop: true }; + return { + optimizedOrders: twoHopOrders, + liquidityDelivered: bestTwoHopQuote, + sourceFlags: SOURCE_FLAGS[ERC20BridgeSource.MultiHop], + }; } // Generate a fallback path if native orders are in the optimal path. - const nativeSubPath = optimalPath.filter(f => f.source === ERC20BridgeSource.Native); - if (opts.allowFallback && nativeSubPath.length !== 0) { + const nativeFills = optimalPath.fills.filter(f => f.source === ERC20BridgeSource.Native); + if (opts.allowFallback && nativeFills.length !== 0) { // We create a fallback path that is exclusive of Native liquidity // This is the optimal on-chain path for the entire input amount - const nonNativePaths = paths.filter(p => p.length > 0 && p[0].source !== ERC20BridgeSource.Native); - const nonNativeOptimalPath = - (await findOptimalPathAsync(side, nonNativePaths, inputAmount, opts.runLimit)) || []; + const nonNativeFills = fills.filter(p => p.length > 0 && p[0].source !== ERC20BridgeSource.Native); + const nonNativeOptimalPath = await findOptimalPathAsync(side, nonNativeFills, inputAmount, opts.runLimit); // Calculate the slippage of on-chain sources compared to the most optimal path - const fallbackSlippage = getPathAdjustedSlippage(side, nonNativeOptimalPath, inputAmount, optimalPathRate); - if (nativeSubPath.length === optimalPath.length || fallbackSlippage <= maxFallbackSlippage) { - // If the last fill is Native and penultimate is not, then the intention was to partial fill - // In this case we drop it entirely as we can't handle a failure at the end and we don't - // want to fully fill when it gets prepended to the front below - const [last, penultimateIfExists] = optimalPath.slice().reverse(); - const lastNativeFillIfExists = - last.source === ERC20BridgeSource.Native && - penultimateIfExists && - penultimateIfExists.source !== ERC20BridgeSource.Native - ? last - : undefined; - // By prepending native paths to the front they cannot split on-chain sources and incur - // an additional protocol fee. I.e [Uniswap,Native,Kyber] becomes [Native,Uniswap,Kyber] - // In the previous step we dropped any hanging Native partial fills, as to not fully fill - optimalPath = [...nativeSubPath.filter(f => f !== lastNativeFillIfExists), ...nonNativeOptimalPath]; + if ( + nonNativeOptimalPath !== undefined && + (nativeFills.length === optimalPath.fills.length || + nonNativeOptimalPath.adjustedSlippage(optimalPathRate) <= maxFallbackSlippage) + ) { + optimalPath.addFallback(nonNativeOptimalPath); } } - const optimizedOrders = createOrdersFromPath(optimalPath, orderOpts); - const liquidityDelivered = _.flatten(optimizedOrders.map(order => order.fills)); - return { optimizedOrders, liquidityDelivered, isTwoHop: false }; + const collapsedPath = optimalPath.collapse(orderOpts); + return { + optimizedOrders: collapsedPath.orders, + liquidityDelivered: collapsedPath.collapsedFills, + sourceFlags: collapsedPath.sourceFlags, + } as OptimizerResult; } } diff --git a/packages/asset-swapper/src/utils/market_operation_utils/multihop_utils.ts b/packages/asset-swapper/src/utils/market_operation_utils/multihop_utils.ts index 28c5043d62..146f0b7826 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/multihop_utils.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/multihop_utils.ts @@ -2,7 +2,7 @@ import { BigNumber } from '@0x/utils'; import * as _ from 'lodash'; import { ZERO_AMOUNT } from './constants'; -import { getTwoHopAdjustedRate } from './fills'; +import { getTwoHopAdjustedRate } from './rate_utils'; import { DexSample, FeeSchedule, MarketSideLiquidity, MultiHopFillData, TokenAdjacencyGraph } from './types'; /** diff --git a/packages/asset-swapper/src/utils/market_operation_utils/orders.ts b/packages/asset-swapper/src/utils/market_operation_utils/orders.ts index efcb699a7a..67cfbc118e 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/orders.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/orders.ts @@ -16,7 +16,6 @@ import { WALLET_SIGNATURE, ZERO_AMOUNT, } from './constants'; -import { collapsePath } from './fills'; import { getMultiBridgeIntermediateToken } from './multibridge_utils'; import { AggregationError, @@ -26,7 +25,6 @@ import { CurveFillData, DexSample, ERC20BridgeSource, - Fill, KyberFillData, LiquidityProviderFillData, MooniswapFillData, @@ -155,37 +153,6 @@ export interface CreateOrderFromPathOpts { shouldBatchBridgeOrders: boolean; } -// Convert sell fills into orders. -export function createOrdersFromPath(path: Fill[], opts: CreateOrderFromPathOpts): OptimizedMarketOrder[] { - const [makerToken, takerToken] = getMakerTakerTokens(opts); - const collapsedPath = collapsePath(path); - const orders: OptimizedMarketOrder[] = []; - for (let i = 0; i < collapsedPath.length; ) { - if (collapsedPath[i].source === ERC20BridgeSource.Native) { - orders.push(createNativeOrder(collapsedPath[i] as NativeCollapsedFill)); - ++i; - continue; - } - // If there are contiguous bridge orders, we can batch them together. - const contiguousBridgeFills = [collapsedPath[i]]; - for (let j = i + 1; j < collapsedPath.length; ++j) { - if (collapsedPath[j].source === ERC20BridgeSource.Native) { - break; - } - contiguousBridgeFills.push(collapsedPath[j]); - } - // Always use DexForwarderBridge unless configured not to - if (!opts.shouldBatchBridgeOrders) { - orders.push(createBridgeOrder(contiguousBridgeFills[0], makerToken, takerToken, opts)); - i += 1; - } else { - orders.push(createBatchedBridgeOrder(contiguousBridgeFills, opts)); - i += contiguousBridgeFills.length; - } - } - return orders; -} - export function createOrdersFromTwoHopSample( sample: DexSample, opts: CreateOrderFromPathOpts, @@ -248,7 +215,7 @@ function getBridgeAddressFromFill(fill: CollapsedFill, opts: CreateOrderFromPath throw new Error(AggregationError.NoBridgeForSource); } -function createBridgeOrder( +export function createBridgeOrder( fill: CollapsedFill, makerToken: string, takerToken: string, @@ -362,7 +329,7 @@ function createBridgeOrder( }; } -function createBatchedBridgeOrder(fills: CollapsedFill[], opts: CreateOrderFromPathOpts): OptimizedMarketOrder { +export function createBatchedBridgeOrder(fills: CollapsedFill[], opts: CreateOrderFromPathOpts): OptimizedMarketOrder { const [makerToken, takerToken] = getMakerTakerTokens(opts); let totalMakerAssetAmount = ZERO_AMOUNT; let totalTakerAssetAmount = ZERO_AMOUNT; @@ -403,7 +370,7 @@ function createBatchedBridgeOrder(fills: CollapsedFill[], opts: CreateOrderFromP }; } -function getMakerTakerTokens(opts: CreateOrderFromPathOpts): [string, string] { +export function getMakerTakerTokens(opts: CreateOrderFromPathOpts): [string, string] { const makerToken = opts.side === MarketOperation.Sell ? opts.outputToken : opts.inputToken; const takerToken = opts.side === MarketOperation.Sell ? opts.inputToken : opts.outputToken; return [makerToken, takerToken]; @@ -525,7 +492,7 @@ function createCommonBridgeOrderFields(orderDomain: OrderDomain): CommonBridgeOr }; } -function createNativeOrder(fill: NativeCollapsedFill): OptimizedMarketOrder { +export function createNativeOrder(fill: NativeCollapsedFill): OptimizedMarketOrder { return { fills: [fill], ...fill.fillData!.order, // tslint:disable-line:no-non-null-assertion diff --git a/packages/asset-swapper/src/utils/market_operation_utils/path.ts b/packages/asset-swapper/src/utils/market_operation_utils/path.ts new file mode 100644 index 0000000000..89333f6925 --- /dev/null +++ b/packages/asset-swapper/src/utils/market_operation_utils/path.ts @@ -0,0 +1,287 @@ +import { BigNumber } from '@0x/utils'; + +import { MarketOperation } from '../../types'; + +import { POSITIVE_INF, SOURCE_FLAGS, ZERO_AMOUNT } from './constants'; +import { + createBatchedBridgeOrder, + createBridgeOrder, + createNativeOrder, + CreateOrderFromPathOpts, + getMakerTakerTokens, +} from './orders'; +import { getCompleteRate, getRate } from './rate_utils'; +import { + CollapsedFill, + ERC20BridgeSource, + ExchangeProxyOverhead, + Fill, + NativeCollapsedFill, + OptimizedMarketOrder, +} from './types'; + +// tslint:disable: prefer-for-of no-bitwise completed-docs + +export interface PathSize { + input: BigNumber; + output: BigNumber; +} + +export interface PathPenaltyOpts { + ethToOutputRate: BigNumber; + ethToInputRate: BigNumber; + exchangeProxyOverhead: ExchangeProxyOverhead; +} + +export const DEFAULT_PATH_PENALTY_OPTS: PathPenaltyOpts = { + ethToOutputRate: ZERO_AMOUNT, + ethToInputRate: ZERO_AMOUNT, + exchangeProxyOverhead: () => ZERO_AMOUNT, +}; + +export class Path { + public collapsedFills?: ReadonlyArray; + public orders?: OptimizedMarketOrder[]; + public sourceFlags: number = 0; + protected _size: PathSize = { input: ZERO_AMOUNT, output: ZERO_AMOUNT }; + protected _adjustedSize: PathSize = { input: ZERO_AMOUNT, output: ZERO_AMOUNT }; + + protected constructor( + protected readonly side: MarketOperation, + public fills: ReadonlyArray, + protected readonly targetInput: BigNumber, + public readonly pathPenaltyOpts: PathPenaltyOpts, + ) {} + + public static create( + side: MarketOperation, + fills: ReadonlyArray, + targetInput: BigNumber = POSITIVE_INF, + pathPenaltyOpts: PathPenaltyOpts = DEFAULT_PATH_PENALTY_OPTS, + ): Path { + const path = new Path(side, fills, targetInput, pathPenaltyOpts); + fills.forEach(fill => { + path.sourceFlags |= fill.flags; + path._addFillSize(fill); + }); + return path; + } + + public static clone(base: Path): Path { + const clonedPath = new Path(base.side, base.fills.slice(), base.targetInput, base.pathPenaltyOpts); + clonedPath.sourceFlags = base.sourceFlags; + clonedPath._size = { ...base._size }; + clonedPath._adjustedSize = { ...base._adjustedSize }; + clonedPath.collapsedFills = base.collapsedFills === undefined ? undefined : base.collapsedFills.slice(); + clonedPath.orders = base.orders === undefined ? undefined : base.orders.slice(); + return clonedPath; + } + + public append(fill: Fill): this { + (this.fills as Fill[]).push(fill); + this.sourceFlags |= fill.flags; + this._addFillSize(fill); + return this; + } + + public addFallback(fallback: Path): this { + // If the last fill is Native and penultimate is not, then the intention was to partial fill + // In this case we drop it entirely as we can't handle a failure at the end and we don't + // want to fully fill when it gets prepended to the front below + const [last, penultimateIfExists] = this.fills.slice().reverse(); + const lastNativeFillIfExists = + last.source === ERC20BridgeSource.Native && + penultimateIfExists && + penultimateIfExists.source !== ERC20BridgeSource.Native + ? last + : undefined; + // By prepending native paths to the front they cannot split on-chain sources and incur + // an additional protocol fee. I.e [Uniswap,Native,Kyber] becomes [Native,Uniswap,Kyber] + // In the previous step we dropped any hanging Native partial fills, as to not fully fill + const nativeFills = this.fills.filter(f => f.source === ERC20BridgeSource.Native); + this.fills = [...nativeFills.filter(f => f !== lastNativeFillIfExists), ...fallback.fills]; + // Recompute the source flags + this.sourceFlags = this.fills.reduce((flags, fill) => (flags |= fill.flags), 0); + return this; + } + + public collapse(opts: CreateOrderFromPathOpts): CollapsedPath { + const [makerToken, takerToken] = getMakerTakerTokens(opts); + const collapsedFills = this.collapsedFills === undefined ? this._collapseFills() : this.collapsedFills; + this.orders = []; + for (let i = 0; i < collapsedFills.length; ) { + if (collapsedFills[i].source === ERC20BridgeSource.Native) { + this.orders.push(createNativeOrder(collapsedFills[i] as NativeCollapsedFill)); + ++i; + continue; + } + // If there are contiguous bridge orders, we can batch them together. + const contiguousBridgeFills = [collapsedFills[i]]; + for (let j = i + 1; j < collapsedFills.length; ++j) { + if (collapsedFills[j].source === ERC20BridgeSource.Native) { + break; + } + contiguousBridgeFills.push(collapsedFills[j]); + } + // Always use DexForwarderBridge unless configured not to + if (!opts.shouldBatchBridgeOrders) { + this.orders.push(createBridgeOrder(contiguousBridgeFills[0], makerToken, takerToken, opts)); + i += 1; + } else { + this.orders.push(createBatchedBridgeOrder(contiguousBridgeFills, opts)); + i += contiguousBridgeFills.length; + } + } + return this as CollapsedPath; + } + + public size(): PathSize { + return this._size; + } + + public adjustedSize(): PathSize { + const { input, output } = this._adjustedSize; + const { exchangeProxyOverhead, ethToOutputRate, ethToInputRate } = this.pathPenaltyOpts; + const gasOverhead = exchangeProxyOverhead(this.sourceFlags); + const pathPenalty = !ethToOutputRate.isZero() + ? ethToOutputRate.times(gasOverhead) + : ethToInputRate.times(gasOverhead).times(output.dividedToIntegerBy(input)); + return { + input, + output: this.side === MarketOperation.Sell ? output.minus(pathPenalty) : output.plus(pathPenalty), + }; + } + + public adjustedCompleteRate(): BigNumber { + const { input, output } = this.adjustedSize(); + return getCompleteRate(this.side, input, output, this.targetInput); + } + + public adjustedRate(): BigNumber { + const { input, output } = this.adjustedSize(); + return getRate(this.side, input, output); + } + + public adjustedSlippage(maxRate: BigNumber): number { + if (maxRate.eq(0)) { + return 0; + } + const totalRate = this.adjustedRate(); + const rateChange = maxRate.minus(totalRate); + return rateChange.div(maxRate).toNumber(); + } + + public isBetterThan(other: Path): boolean { + if (!this.targetInput.isEqualTo(other.targetInput)) { + throw new Error(`Target input mismatch: ${this.targetInput} !== ${other.targetInput}`); + } + const { targetInput } = this; + const { input } = this._size; + const { input: otherInput } = other._size; + if (input.isLessThan(targetInput) || otherInput.isLessThan(targetInput)) { + return input.isGreaterThan(otherInput); + } else { + return this.adjustedCompleteRate().isGreaterThan(other.adjustedCompleteRate()); + } + // if (otherInput.isLessThan(targetInput)) { + // return input.isGreaterThan(otherInput); + // } else if (input.isGreaterThanOrEqualTo(targetInput)) { + // return this.adjustedCompleteRate().isGreaterThan(other.adjustedCompleteRate()); + // } + // return false; + } + + public isComplete(): boolean { + const { input } = this._size; + return input.gte(this.targetInput); + } + + public isValid(skipDuplicateCheck: boolean = false): boolean { + for (let i = 0; i < this.fills.length; ++i) { + // Fill must immediately follow its parent. + if (this.fills[i].parent) { + if (i === 0 || this.fills[i - 1] !== this.fills[i].parent) { + return false; + } + } + if (!skipDuplicateCheck) { + // Fill must not be duplicated. + for (let j = 0; j < i; ++j) { + if (this.fills[i] === this.fills[j]) { + return false; + } + } + } + } + return doSourcesConflict(this.sourceFlags); + } + + public isValidNextFill(fill: Fill): boolean { + if (this.fills.length === 0) { + return !fill.parent; + } + if (this.fills[this.fills.length - 1] === fill.parent) { + return true; + } + if (fill.parent) { + return false; + } + return doSourcesConflict(this.sourceFlags | fill.flags); + } + + private _collapseFills(): ReadonlyArray { + this.collapsedFills = []; + for (const fill of this.fills) { + const source = fill.source; + if (this.collapsedFills.length !== 0 && source !== ERC20BridgeSource.Native) { + const prevFill = this.collapsedFills[this.collapsedFills.length - 1]; + // If the last fill is from the same source, merge them. + if (prevFill.sourcePathId === fill.sourcePathId) { + prevFill.input = prevFill.input.plus(fill.input); + prevFill.output = prevFill.output.plus(fill.output); + prevFill.fillData = fill.fillData; + prevFill.subFills.push(fill); + continue; + } + } + (this.collapsedFills as CollapsedFill[]).push({ + sourcePathId: fill.sourcePathId, + source: fill.source, + fillData: fill.fillData, + input: fill.input, + output: fill.output, + subFills: [fill], + }); + } + return this.collapsedFills; + } + + private _addFillSize(fill: Fill): void { + if (this._size.input.plus(fill.input).isGreaterThan(this.targetInput)) { + const remainingInput = this.targetInput.minus(this._size.input); + const scaledFillOutput = fill.output.times(remainingInput.div(fill.input)); + this._size.input = this.targetInput; + this._size.output = this._size.output.plus(scaledFillOutput); + // Penalty does not get interpolated. + const penalty = fill.adjustedOutput.minus(fill.output); + this._adjustedSize.input = this.targetInput; + this._adjustedSize.output = this._adjustedSize.output.plus(scaledFillOutput).plus(penalty); + } else { + this._size.input = this._size.input.plus(fill.input); + this._size.output = this._size.output.plus(fill.output); + this._adjustedSize.input = this._adjustedSize.input.plus(fill.input); + this._adjustedSize.output = this._adjustedSize.output.plus(fill.adjustedOutput); + } + } +} + +export interface CollapsedPath extends Path { + readonly collapsedFills: ReadonlyArray; + readonly orders: OptimizedMarketOrder[]; +} + +const MULTIBRIDGE_SOURCES = SOURCE_FLAGS.LiquidityProvider | SOURCE_FLAGS.Uniswap; +export function doSourcesConflict(flags: number): boolean { + const multiBridgeConflict = flags & SOURCE_FLAGS.MultiBridge && flags & MULTIBRIDGE_SOURCES; + return !multiBridgeConflict; +} diff --git a/packages/asset-swapper/src/utils/market_operation_utils/path_optimizer.ts b/packages/asset-swapper/src/utils/market_operation_utils/path_optimizer.ts index f649c59e07..31244d2299 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/path_optimizer.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/path_optimizer.ts @@ -1,17 +1,9 @@ import { BigNumber } from '@0x/utils'; +import * as _ from 'lodash'; import { MarketOperation } from '../../types'; -import { ZERO_AMOUNT } from './constants'; -import { - arePathFlagsAllowed, - getCompleteRate, - getPathAdjustedCompleteRate, - getPathAdjustedRate, - getPathAdjustedSize, - getPathSize, - isValidPath, -} from './fills'; +import { DEFAULT_PATH_PENALTY_OPTS, Path, PathPenaltyOpts } from './path'; import { Fill } from './types'; // tslint:disable: prefer-for-of custom-no-magic-numbers completed-docs no-bitwise @@ -19,134 +11,93 @@ import { Fill } from './types'; const RUN_LIMIT_DECAY_FACTOR = 0.5; /** - * Find the optimal mixture of paths that maximizes (for sells) or minimizes + * Find the optimal mixture of fills that maximizes (for sells) or minimizes * (for buys) output, while meeting the input requirement. */ export async function findOptimalPathAsync( side: MarketOperation, - paths: Fill[][], + fills: Fill[][], targetInput: BigNumber, runLimit: number = 2 ** 8, -): Promise { - // Sort paths by descending adjusted completed rate. - const sortedPaths = paths - .slice(0) - .sort((a, b) => - getPathAdjustedCompleteRate(side, b, targetInput).comparedTo( - getPathAdjustedCompleteRate(side, a, targetInput), - ), - ); - let optimalPath = sortedPaths[0] || []; + opts: PathPenaltyOpts = DEFAULT_PATH_PENALTY_OPTS, +): Promise { + const rates = rateBySourcePathId(side, fills, targetInput); + const paths = fills.map(singleSourceFills => Path.create(side, singleSourceFills, targetInput, opts)); + // Sort fill arrays by descending adjusted completed rate. + const sortedPaths = paths.sort((a, b) => b.adjustedCompleteRate().comparedTo(a.adjustedCompleteRate())); + if (sortedPaths.length === 0) { + return undefined; + } + let optimalPath = sortedPaths[0]; for (const [i, path] of sortedPaths.slice(1).entries()) { - optimalPath = mixPaths(side, optimalPath, path, targetInput, runLimit * RUN_LIMIT_DECAY_FACTOR ** i); + optimalPath = mixPaths(side, optimalPath, path, targetInput, runLimit * RUN_LIMIT_DECAY_FACTOR ** i, rates); // Yield to event loop. await Promise.resolve(); } - return isPathComplete(optimalPath, targetInput) ? optimalPath : undefined; + return optimalPath.isComplete() ? optimalPath : undefined; } function mixPaths( side: MarketOperation, - pathA: Fill[], - pathB: Fill[], + pathA: Path, + pathB: Path, targetInput: BigNumber, maxSteps: number, -): Fill[] { + rateBySourcePathId: { [id: string]: BigNumber }, +): Path { const _maxSteps = Math.max(maxSteps, 32); let steps = 0; // We assume pathA is the better of the two initially. - let bestPath: Fill[] = pathA; - let [bestPathInput, bestPathOutput] = getPathAdjustedSize(pathA, targetInput); - let bestPathRate = getCompleteRate(side, bestPathInput, bestPathOutput, targetInput); - const _isBetterPath = (input: BigNumber, rate: BigNumber) => { - if (bestPathInput.lt(targetInput)) { - return input.gt(bestPathInput); - } else if (input.gte(targetInput)) { - return rate.gt(bestPathRate); - } - return false; - }; - const _walk = (path: Fill[], input: BigNumber, output: BigNumber, flags: number, remainingFills: Fill[]) => { + let bestPath: Path = pathA; + + const _walk = (path: Path, remainingFills: Fill[]) => { steps += 1; - const rate = getCompleteRate(side, input, output, targetInput); - if (_isBetterPath(input, rate)) { + if (path.isBetterThan(bestPath)) { bestPath = path; - bestPathInput = input; - bestPathOutput = output; - bestPathRate = rate; } - const remainingInput = targetInput.minus(input); - if (remainingInput.gt(0)) { + const remainingInput = targetInput.minus(path.size().input); + if (remainingInput.isGreaterThan(0)) { for (let i = 0; i < remainingFills.length && steps < _maxSteps; ++i) { const fill = remainingFills[i]; // Only walk valid paths. - if (!isValidNextPathFill(path, flags, fill)) { + if (!path.isValidNextFill(fill)) { continue; } // Remove this fill from the next list of candidate fills. const nextRemainingFills = remainingFills.slice(); nextRemainingFills.splice(i, 1); // Recurse. - _walk( - [...path, fill], - input.plus(BigNumber.min(remainingInput, fill.input)), - output.plus( - // Clip the output of the next fill to the remaining - // input. - clipFillAdjustedOutput(fill, remainingInput), - ), - flags | fill.flags, - nextRemainingFills, - ); + _walk(Path.clone(path).append(fill), nextRemainingFills); } } }; - const allFills = [...pathA, ...pathB]; - const sources = allFills.filter(f => f.index === 0).map(f => f.sourcePathId); - const rateBySource = Object.assign( - {}, - ...sources.map(s => ({ - [s]: getPathAdjustedRate(side, allFills.filter(f => f.sourcePathId === s), targetInput), - })), - ); + const allFills = [...pathA.fills, ...pathB.fills]; // Sort subpaths by rate and keep fills contiguous to improve our // chances of walking ideal, valid paths first. const sortedFills = allFills.sort((a, b) => { if (a.sourcePathId !== b.sourcePathId) { - return rateBySource[b.sourcePathId].comparedTo(rateBySource[a.sourcePathId]); + return rateBySourcePathId[b.sourcePathId].comparedTo(rateBySourcePathId[a.sourcePathId]); } return a.index - b.index; }); - _walk([], ZERO_AMOUNT, ZERO_AMOUNT, 0, sortedFills); - if (!isValidPath(bestPath)) { + _walk(Path.create(side, [], targetInput, pathA.pathPenaltyOpts), sortedFills); + if (!bestPath.isValid()) { throw new Error('nooope'); } return bestPath; } -function isValidNextPathFill(path: Fill[], pathFlags: number, fill: Fill): boolean { - if (path.length === 0) { - return !fill.parent; - } - if (path[path.length - 1] === fill.parent) { - return true; - } - if (fill.parent) { - return false; - } - return arePathFlagsAllowed(pathFlags | fill.flags); -} - -function isPathComplete(path: Fill[], targetInput: BigNumber): boolean { - const [input] = getPathSize(path); - return input.gte(targetInput); -} - -function clipFillAdjustedOutput(fill: Fill, remainingInput: BigNumber): BigNumber { - if (fill.input.lte(remainingInput)) { - return fill.adjustedOutput; - } - // Penalty does not get interpolated. - const penalty = fill.adjustedOutput.minus(fill.output); - return remainingInput.times(fill.output.div(fill.input)).plus(penalty); +function rateBySourcePathId( + side: MarketOperation, + fills: Fill[][], + targetInput: BigNumber, +): { [id: string]: BigNumber } { + const flattenedFills = _.flatten(fills); + const sourcePathIds = flattenedFills.filter(f => f.index === 0).map(f => f.sourcePathId); + return Object.assign( + {}, + ...sourcePathIds.map(s => ({ + [s]: Path.create(side, flattenedFills.filter(f => f.sourcePathId === s), targetInput).adjustedRate(), + })), + ); } diff --git a/packages/asset-swapper/src/utils/market_operation_utils/rate_utils.ts b/packages/asset-swapper/src/utils/market_operation_utils/rate_utils.ts new file mode 100644 index 0000000000..713972cb33 --- /dev/null +++ b/packages/asset-swapper/src/utils/market_operation_utils/rate_utils.ts @@ -0,0 +1,48 @@ +import { BigNumber } from '@0x/utils'; + +import { MarketOperation } from '../../types'; + +import { ZERO_AMOUNT } from './constants'; +import { DexSample, ERC20BridgeSource, FeeSchedule, MultiHopFillData } from './types'; + +export function getTwoHopAdjustedRate( + side: MarketOperation, + twoHopQuote: DexSample, + targetInput: BigNumber, + ethToOutputRate: BigNumber, + fees: FeeSchedule = {}, +): BigNumber { + const { output, input, fillData } = twoHopQuote; + if (input.isLessThan(targetInput) || output.isZero()) { + return ZERO_AMOUNT; + } + const penalty = ethToOutputRate.times(fees[ERC20BridgeSource.MultiHop]!(fillData)); + const adjustedOutput = side === MarketOperation.Sell ? output.minus(penalty) : output.plus(penalty); + return side === MarketOperation.Sell ? adjustedOutput.div(input) : input.div(adjustedOutput); +} + +export function getCompleteRate( + side: MarketOperation, + input: BigNumber, + output: BigNumber, + targetInput: BigNumber, +): BigNumber { + if (input.eq(0) || output.eq(0) || targetInput.eq(0)) { + return ZERO_AMOUNT; + } + // Penalize paths that fall short of the entire input amount by a factor of + // input / targetInput => (i / t) + if (side === MarketOperation.Sell) { + // (o / i) * (i / t) => (o / t) + return output.div(targetInput); + } + // (i / o) * (i / t) + return input.div(output).times(input.div(targetInput)); +} + +export function getRate(side: MarketOperation, input: BigNumber, output: BigNumber): BigNumber { + if (input.eq(0) || output.eq(0)) { + return ZERO_AMOUNT; + } + return side === MarketOperation.Sell ? output.div(input) : input.div(output); +} diff --git a/packages/asset-swapper/src/utils/market_operation_utils/types.ts b/packages/asset-swapper/src/utils/market_operation_utils/types.ts index 5e7ea158fd..c6af06a62e 100644 --- a/packages/asset-swapper/src/utils/market_operation_utils/types.ts +++ b/packages/asset-swapper/src/utils/market_operation_utils/types.ts @@ -156,16 +156,6 @@ export interface DexSample extends Source output: BigNumber; } -/** - * Flags for `Fill` objects. - */ -export enum FillFlags { - ConflictsWithKyber = 0x1, - Kyber = 0x2, - ConflictsWithMultiBridge = 0x4, - MultiBridge = 0x8, -} - /** * Represents a node on a fill path. */ @@ -174,8 +164,8 @@ export interface Fill extends SourceInfo< // This is generated when the path is generated and is useful to distinguish // paths that have the same `source` IDs but are distinct (e.g., Curves). sourcePathId: string; - // See `FillFlags`. - flags: FillFlags; + // See `SOURCE_FLAGS`. + flags: number; // Input fill amount (taker asset amount in a sell, maker asset amount in a buy). input: BigNumber; // Output fill amount (maker asset amount in a sell, taker asset amount in a buy). @@ -234,6 +224,7 @@ export interface GetMarketOrdersRfqtOpts extends RfqtRequestOpts { export type FeeEstimate = (fillData?: FillData) => number | BigNumber; export type FeeSchedule = Partial<{ [key in ERC20BridgeSource]: FeeEstimate }>; +export type ExchangeProxyOverhead = (sourceFlags: number) => BigNumber; /** * Options for `getMarketSellOrdersAsync()` and `getMarketBuyOrdersAsync()`. @@ -288,6 +279,7 @@ export interface GetMarketOrdersOpts { * Estimated gas consumed by each liquidity source. */ gasSchedule: FeeSchedule; + exchangeProxyOverhead: ExchangeProxyOverhead; /** * Whether to pad the quote with a redundant fallback quote using different * sources. Defaults to `true`. @@ -321,11 +313,8 @@ export interface SourceQuoteOperation export interface OptimizerResult { optimizedOrders: OptimizedMarketOrder[]; - isTwoHop: boolean; + sourceFlags: number; liquidityDelivered: CollapsedFill[] | DexSample; -} - -export interface OptimizerResultWithReport extends OptimizerResult { quoteReport?: QuoteReport; } diff --git a/packages/asset-swapper/src/utils/quote_report_generator.ts b/packages/asset-swapper/src/utils/quote_report_generator.ts index 81fce3c0f0..31b49dcda8 100644 --- a/packages/asset-swapper/src/utils/quote_report_generator.ts +++ b/packages/asset-swapper/src/utils/quote_report_generator.ts @@ -64,7 +64,7 @@ export function generateQuoteReport( multiHopQuotes: Array>, nativeOrders: SignedOrder[], orderFillableAmounts: BigNumber[], - liquidityDelivered: CollapsedFill[] | DexSample, + liquidityDelivered: ReadonlyArray | DexSample, quoteRequestor?: QuoteRequestor, ): QuoteReport { const dexReportSourcesConsidered = dexQuotes.map(quote => _dexSampleToReportSource(quote, marketOperation)); @@ -101,7 +101,9 @@ export function generateQuoteReport( } }); } else { - sourcesDelivered = [_multiHopSampleToReportSource(liquidityDelivered, marketOperation)]; + sourcesDelivered = [ + _multiHopSampleToReportSource(liquidityDelivered as DexSample, marketOperation), + ]; } return { sourcesConsidered, diff --git a/packages/asset-swapper/src/utils/quote_simulation.ts b/packages/asset-swapper/src/utils/quote_simulation.ts index 5e46046e79..863abb1489 100644 --- a/packages/asset-swapper/src/utils/quote_simulation.ts +++ b/packages/asset-swapper/src/utils/quote_simulation.ts @@ -349,7 +349,7 @@ function fromIntermediateQuoteFillResult(ir: IntermediateQuoteFillResult, quoteI }; } -export function getFlattenedFillsFromOrders(orders: OptimizedMarketOrder[]): CollapsedFill[] { +function getFlattenedFillsFromOrders(orders: OptimizedMarketOrder[]): CollapsedFill[] { const fills: CollapsedFill[] = []; for (const o of orders) { fills.push(...o.fills); diff --git a/packages/asset-swapper/src/utils/swap_quote_calculator.ts b/packages/asset-swapper/src/utils/swap_quote_calculator.ts index 2d16f48a3e..3f4e56eb10 100644 --- a/packages/asset-swapper/src/utils/swap_quote_calculator.ts +++ b/packages/asset-swapper/src/utils/swap_quote_calculator.ts @@ -16,6 +16,7 @@ import { } from '../types'; import { MarketOperationUtils } from './market_operation_utils'; +import { SOURCE_FLAGS } from './market_operation_utils/constants'; import { convertNativeOrderToFullyFillableOptimizedOrders } from './market_operation_utils/orders'; import { ERC20BridgeSource, @@ -130,70 +131,74 @@ export class SwapQuoteCalculator { let optimizedOrders: OptimizedMarketOrder[]; let quoteReport: QuoteReport | undefined; - let isTwoHop = false; + let sourceFlags: number = 0; - { - // Scale fees by gas price. - const _opts: GetMarketOrdersOpts = { - ...opts, - feeSchedule: _.mapValues(opts.feeSchedule, gasCost => (fillData?: FillData) => - gasCost === undefined ? 0 : gasPrice.times(gasCost(fillData)), - ), - }; + // Scale fees by gas price. + const _opts: GetMarketOrdersOpts = { + ...opts, + feeSchedule: _.mapValues(opts.feeSchedule, gasCost => (fillData?: FillData) => + gasCost === undefined ? 0 : gasPrice.times(gasCost(fillData)), + ), + exchangeProxyOverhead: (sourceFlags: number) => gasPrice.times(opts.exchangeProxyOverhead!(sourceFlags)), + }; - const firstOrderMakerAssetData = !!prunedOrders[0] - ? assetDataUtils.decodeAssetDataOrThrow(prunedOrders[0].makerAssetData) - : { assetProxyId: '' }; + const firstOrderMakerAssetData = !!prunedOrders[0] + ? assetDataUtils.decodeAssetDataOrThrow(prunedOrders[0].makerAssetData) + : { assetProxyId: '' }; - if (firstOrderMakerAssetData.assetProxyId === AssetProxyId.ERC721) { - // HACK: to conform ERC721 orders to the output of market operation utils, assumes complete fillable - optimizedOrders = prunedOrders.map(o => convertNativeOrderToFullyFillableOptimizedOrders(o)); + if (firstOrderMakerAssetData.assetProxyId === AssetProxyId.ERC721) { + // HACK: to conform ERC721 orders to the output of market operation utils, assumes complete fillable + optimizedOrders = prunedOrders.map(o => convertNativeOrderToFullyFillableOptimizedOrders(o)); + } else { + if (operation === MarketOperation.Buy) { + const buyResult = await this._marketOperationUtils.getMarketBuyOrdersAsync( + prunedOrders, + assetFillAmount, + _opts, + ); + optimizedOrders = buyResult.optimizedOrders; + quoteReport = buyResult.quoteReport; + sourceFlags = buyResult.sourceFlags; } else { - if (operation === MarketOperation.Buy) { - const buyResult = await this._marketOperationUtils.getMarketBuyOrdersAsync( - prunedOrders, - assetFillAmount, - _opts, - ); - optimizedOrders = buyResult.optimizedOrders; - quoteReport = buyResult.quoteReport; - isTwoHop = buyResult.isTwoHop; - } else { - const sellResult = await this._marketOperationUtils.getMarketSellOrdersAsync( - prunedOrders, - assetFillAmount, - _opts, - ); - optimizedOrders = sellResult.optimizedOrders; - quoteReport = sellResult.quoteReport; - isTwoHop = sellResult.isTwoHop; - } + const sellResult = await this._marketOperationUtils.getMarketSellOrdersAsync( + prunedOrders, + assetFillAmount, + _opts, + ); + optimizedOrders = sellResult.optimizedOrders; + quoteReport = sellResult.quoteReport; + sourceFlags = sellResult.sourceFlags; } } // assetData information for the result const { makerAssetData, takerAssetData } = prunedOrders[0]; - return isTwoHop - ? createTwoHopSwapQuote( - makerAssetData, - takerAssetData, - optimizedOrders, - operation, - assetFillAmount, - gasPrice, - opts.gasSchedule, - quoteReport, - ) - : createSwapQuote( - makerAssetData, - takerAssetData, - optimizedOrders, - operation, - assetFillAmount, - gasPrice, - opts.gasSchedule, - quoteReport, - ); + const swapQuote = + sourceFlags === SOURCE_FLAGS[ERC20BridgeSource.MultiHop] + ? createTwoHopSwapQuote( + makerAssetData, + takerAssetData, + optimizedOrders, + operation, + assetFillAmount, + gasPrice, + opts.gasSchedule, + quoteReport, + ) + : createSwapQuote( + makerAssetData, + takerAssetData, + optimizedOrders, + operation, + assetFillAmount, + gasPrice, + opts.gasSchedule, + quoteReport, + ); + const exchangeProxyOverhead = _opts.exchangeProxyOverhead(sourceFlags).toNumber(); + swapQuote.bestCaseQuoteInfo.gas += exchangeProxyOverhead; + swapQuote.worstCaseQuoteInfo.gas += exchangeProxyOverhead; + return swapQuote; } } diff --git a/packages/asset-swapper/test/market_operation_utils_test.ts b/packages/asset-swapper/test/market_operation_utils_test.ts index 77261f3b6b..1e3323c97a 100644 --- a/packages/asset-swapper/test/market_operation_utils_test.ts +++ b/packages/asset-swapper/test/market_operation_utils_test.ts @@ -24,7 +24,7 @@ import { SELL_SOURCE_FILTER, ZERO_AMOUNT, } from '../src/utils/market_operation_utils/constants'; -import { createFillPaths } from '../src/utils/market_operation_utils/fills'; +import { createFills } from '../src/utils/market_operation_utils/fills'; import { DexOrderSampler } from '../src/utils/market_operation_utils/sampler'; import { BATCH_SOURCE_FILTERS } from '../src/utils/market_operation_utils/sampler_operations'; import { @@ -1299,7 +1299,7 @@ describe('MarketOperationUtils tests', () => { it('batches contiguous bridge sources', async () => { const rates: RatesBySource = { ...ZERO_RATES }; - rates[ERC20BridgeSource.Native] = [0.5, 0.01, 0.01, 0.01]; + rates[ERC20BridgeSource.Native] = [0.3, 0.01, 0.01, 0.01]; rates[ERC20BridgeSource.Eth2Dai] = [0.49, 0.02, 0.01, 0.01]; rates[ERC20BridgeSource.Uniswap] = [0.48, 0.01, 0.01, 0.01]; replaceSamplerOps({ @@ -1318,14 +1318,14 @@ describe('MarketOperationUtils tests', () => { expect(improvedOrders).to.be.length(2); const orderFillSources = getSortedOrderSources(MarketOperation.Sell, improvedOrders); expect(orderFillSources).to.deep.eq([ - [ERC20BridgeSource.Native], [ERC20BridgeSource.Eth2Dai, ERC20BridgeSource.Uniswap], + [ERC20BridgeSource.Native], ]); }); }); }); - describe('createFillPaths', () => { + describe('createFills', () => { const takerAssetAmount = new BigNumber(5000000); const ethToOutputRate = new BigNumber(0.5); // tslint:disable-next-line:no-object-literal-type-assertion @@ -1359,7 +1359,7 @@ describe('MarketOperationUtils tests', () => { }; it('penalizes native fill based on target amount when target is smaller', () => { - const path = createFillPaths({ + const path = createFills({ side: MarketOperation.Sell, orders, dexQuotes: [], @@ -1372,7 +1372,7 @@ describe('MarketOperationUtils tests', () => { }); it('penalizes native fill based on available amount when target is larger', () => { - const path = createFillPaths({ + const path = createFills({ side: MarketOperation.Sell, orders, dexQuotes: [],