Skip to content

Commit

Permalink
Filter paths by marginalRate < BestAdjustedRate
Browse files Browse the repository at this point in the history
  • Loading branch information
dekz committed Mar 24, 2021
1 parent 2f86a73 commit 8fbeb3e
Show file tree
Hide file tree
Showing 4 changed files with 2,519 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/asset-swapper/src/utils/market_operation_utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,16 @@ export class MarketOperationUtils {
const _unoptimizedPath = fillsToSortedPaths(fills, side, inputAmount, penaltyOpts)[0];
const unoptimizedPath = _unoptimizedPath ? _unoptimizedPath.collapse(orderOpts) : undefined;

console.log(
JSON.stringify(
dexQuotes
.filter(qs => qs && qs.length > 0)
.map(qs => ({ source: qs[0].source, inputOutputs: qs.map(q => [q.input, q.output]) })),
null,
2,
),
);

// Find the optimal path
const optimalPath = await findOptimalPathAsync(side, fills, inputAmount, opts.runLimit, penaltyOpts);
const optimalPathRate = optimalPath ? optimalPath.adjustedRate() : ZERO_AMOUNT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Path {
public sourceFlags: number = 0;
protected _size: PathSize = { input: ZERO_AMOUNT, output: ZERO_AMOUNT };
protected _adjustedSize: PathSize = { input: ZERO_AMOUNT, output: ZERO_AMOUNT };
private _firstFill!: Fill;

public static create(
side: MarketOperation,
Expand Down Expand Up @@ -147,6 +148,10 @@ export class Path {
return getCompleteRate(this.side, input, output, this.targetInput);
}

public marginalRate(): BigNumber {
return getRate(this.side, this._firstFill.input, this._firstFill.output);
}

public adjustedRate(): BigNumber {
const { input, output } = this.adjustedSize();
return getRate(this.side, input, output);
Expand Down Expand Up @@ -248,6 +253,9 @@ export class Path {
}

private _addFillSize(fill: Fill): void {
if (!this._firstFill) {
this._firstFill = fill;
}
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ export async function findOptimalPathAsync(
return optimalPath.isComplete() ? optimalPath : undefined;
}

export async function findOptimalPath2Async(
side: MarketOperation,
fills: Fill[][],
targetInput: BigNumber,
runLimit: number = 2 ** 8,
opts: PathPenaltyOpts = DEFAULT_PATH_PENALTY_OPTS,
): Promise<Path | undefined> {
// Sort fill arrays by descending adjusted completed rate.
const sortedPaths = fillsToSortedPaths2(fills, side, targetInput, opts);
if (sortedPaths.length === 0) {
return undefined;
}
let optimalPath = sortedPaths[0];
const rates = rateBySourcePathId(side, fills, targetInput);
for (const [i, path] of sortedPaths.slice(1).entries()) {
optimalPath = mixPaths(side, optimalPath, path, targetInput, runLimit * RUN_LIMIT_DECAY_FACTOR ** i, rates);
// Yield to event loop.
await Promise.resolve();
}
return optimalPath.isComplete() ? optimalPath : undefined;
}

// Sort fill arrays by descending adjusted completed rate.
export function fillsToSortedPaths(
fills: Fill[][],
Expand All @@ -48,6 +70,17 @@ export function fillsToSortedPaths(
return sortedPaths;
}

export function fillsToSortedPaths2(
fills: Fill[][],
side: MarketOperation,
targetInput: BigNumber,
opts: PathPenaltyOpts,
): Path[] {
const paths = fills.map(singleSourceFills => Path.create(side, singleSourceFills, targetInput, opts));
const sortedPaths = paths.sort((a, b) => b.adjustedCompleteRate().comparedTo(a.adjustedCompleteRate()));
return sortedPaths.filter(p => p.marginalRate().isGreaterThanOrEqualTo(sortedPaths[0].adjustedCompleteRate()));
}

function mixPaths(
side: MarketOperation,
pathA: Path,
Expand Down
Loading

0 comments on commit 8fbeb3e

Please sign in to comment.