Skip to content

Commit

Permalink
Cull paths which cannot improve price
Browse files Browse the repository at this point in the history
  • Loading branch information
dekz committed Apr 1, 2021
1 parent 164a5d4 commit e232e5e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/asset-swapper/src/utils/market_operation_utils/path.ts
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 @@ -152,6 +153,16 @@ export class Path {
return getRate(this.side, input, output);
}

/**
* Returns the first fill rate. The best possible rate this path can offer, given the fills.
*/
public minRate(): BigNumber {
if (!this._firstFill) {
throw new Error('Unable to calculate minRate');
}
return getRate(this.side, this._firstFill.input, this._firstFill.output);
}

public adjustedSlippage(maxRate: BigNumber): number {
if (maxRate.eq(0)) {
return 0;
Expand Down Expand Up @@ -248,6 +259,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 @@ -45,7 +45,12 @@ export function fillsToSortedPaths(
): 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;
// Any path which has a min rate that is less than the best adjusted completed rate has no chance of improving
// the overall route.
const possiblePaths = sortedPaths.filter(p =>
p.minRate().isGreaterThanOrEqualTo(sortedPaths[0].adjustedCompleteRate()),
);
return possiblePaths;
}

function mixPaths(
Expand Down

0 comments on commit e232e5e

Please sign in to comment.