Skip to content

Commit

Permalink
feat: increase sampler granularity [LIT-843] (0xProject#1283)
Browse files Browse the repository at this point in the history
* increasing sampler granularity to 40

* updating native orders to use 40 sample granularity

* replacing magic numbers with constant
  • Loading branch information
SavDont authored Feb 22, 2023
1 parent 2f0ab5a commit 6da30a7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/asset-swapper/utils/market_operation_utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2197,17 +2197,18 @@ const DEFAULT_FEE_SCHEDULE: FeeSchedule = Object.keys(DEFAULT_GAS_SCHEDULE).redu
return acc;
}, {} as FeeSchedule);

const numSamples = 40;
export const DEFAULT_GET_MARKET_ORDERS_OPTS: Omit<GetMarketOrdersOpts, 'gasPrice'> = {
excludedSources: [],
includedSources: [],
bridgeSlippage: 0.005,
numSamples: 13,
numSamples: numSamples,
sampleDistributionBase: 1,
feeSchedule: DEFAULT_FEE_SCHEDULE,
exchangeProxyOverhead: () => ZERO_AMOUNT,
shouldGenerateQuoteReport: true,
tokenAdjacencyGraph: TokenAdjacencyGraph.getEmptyGraph(),
neonRouterNumSamples: 14,
neonRouterNumSamples: numSamples + 1,
fillAdjustor: new IdentityFillAdjustor(),
endpoint: 'price',
};
Original file line number Diff line number Diff line change
Expand Up @@ -262,23 +262,22 @@ export class PathOptimizer {
}
const fee = this.calculateOutputFee(nativeOrder).integerValue().toNumber();

// HACK: due to an issue with the Rust router interpolation we need to create exactly 13 samples from the native order
// HACK: due to an issue with the Rust router interpolation we need to create exactly 40 samples from the native order
const ids = [];
const inputs = [];
const outputs = [];
const outputFees = [];

// NOTE: Limit orders can be both larger or smaller than the input amount
// If the order is larger than the input we can scale the order to the size of
// the quote input (order pricing is constant) and then create 13 "samples" up to
// the quote input (order pricing is constant) and then create 40 "samples" up to
// and including the full quote input amount.
// If the order is smaller we don't need to scale anything, we will just end up
// with trailing duplicate samples for the order input as we cannot go higher
const scaleToInput = BigNumber.min(this.inputAmount.dividedBy(normalizedOrderInput), 1);

// TODO: replace constant with a proper sample size.
for (let i = 1; i <= 13; i++) {
const fraction = i / 13;
for (let i = 1; i < this.neonRouterNumSamples; i++) {
const fraction = i / (this.neonRouterNumSamples - 1);
const currentInput = BigNumber.min(
normalizedOrderInput.times(scaleToInput).times(fraction),
normalizedOrderInput,
Expand Down
6 changes: 3 additions & 3 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ const EXCHANGE_PROXY_OVERHEAD_FULLY_FEATURED = (sourceFlags: bigint) => {
}
};

const NEON_ROUTER_NUM_SAMPLES = 14;
const NUM_SAMPLES = 40;
// TODO(kimpers): Due to an issue with the Rust router we want to use equidistant samples when using the Rust router

export const ASSET_SWAPPER_MARKET_ORDERS_OPTS: Partial<SwapQuoteRequestOpts> = {
bridgeSlippage: DEFAULT_QUOTE_SLIPPAGE_PERCENTAGE,
numSamples: 13,
numSamples: NUM_SAMPLES,
sampleDistributionBase: SAMPLE_DISTRIBUTION_BASE,
neonRouterNumSamples: NEON_ROUTER_NUM_SAMPLES,
neonRouterNumSamples: NUM_SAMPLES + 1,
exchangeProxyOverhead: EXCHANGE_PROXY_OVERHEAD_FULLY_FEATURED,
shouldGenerateQuoteReport: true,
};
Expand Down

0 comments on commit 6da30a7

Please sign in to comment.