Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

asset-swapper: Quicker path-finding #2640

Merged
merged 4 commits into from
Jul 27, 2020

Conversation

dorothy-zbornak
Copy link
Contributor

@dorothy-zbornak dorothy-zbornak commented Jul 21, 2020

Description

We can reduce the number of iterations we go through (runLimit) to find a decent path if we:

  • Pre-sort all the individual (unmixed) paths by descending adjusted rate
  • When mixing two paths, sort the fill nodes such that:
    • Fill nodes are always contiguous (form a valid subpath)
    • Subpaths (segments of the same source) are sorted by descending rate.
  • Compute the "rate" of a path based off targetInput (fill amount) instead of min(targetInput, pathSize).
    • This favors paths that cover more of the total fill (not sure about this).
  • Increase the runLimit decay rate to 0.5 (prev 0.8). So we halve the number of iterations for each subsequent call to mixPaths().
  • Start the bestPath in mixPaths() off with the left path (the previous mixed path), instead of starting with none.

This makes it more likely that we walk the viable paths first, so we can cut runLimit down to something like 2**8 (previously 2**13).

This results in a much faster (2-3x) response time with small change in quotes:

ab-response-time

ab-realized

Notes:

  • WETH/DAI/USDC sims buy AND sell
  • orange is this version w/ runLimit = 2**8
  • blue is original version w/ runLimit = 2**13

We can probably tweak runLimit to improve results. This should buy us a little breathing room until we can entirely replace the algorithm with something better.

Testing instructions

Types of changes

Checklist:

  • Prefix PR title with [WIP] if necessary.
  • Add tests to cover changes as needed.
  • Update documentation as needed.
  • Add new entries to the relevant CHANGELOG.jsons.

@dorothy-zbornak dorothy-zbornak changed the title opt asset-swapper: Quicker path-finding Jul 21, 2020
@dorothy-zbornak dorothy-zbornak force-pushed the exp/asset-swapper/path-finding-vroom-vroom branch 2 times, most recently from 149a57a to 3b6b660 Compare July 21, 2020 19:35
let bestPath: Fill[] = [];
let bestPathInput = ZERO_AMOUNT;
let bestPathRate = ZERO_AMOUNT;
const _maxSteps = Math.max(maxSteps, 16);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also cap the minimum so everyone gets some action.

@dorothy-zbornak dorothy-zbornak force-pushed the exp/asset-swapper/path-finding-vroom-vroom branch from 3b6b660 to 51ce376 Compare July 21, 2020 21:18
// Unique ID of the original source path this fill belongs to.
// 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New field to tell consolidated sources apart.

@dorothy-zbornak dorothy-zbornak force-pushed the exp/asset-swapper/path-finding-vroom-vroom branch from 51ce376 to e3526e2 Compare July 21, 2020 21:44
Copy link
Contributor

@moodlezoup moodlezoup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall approach look good to me

return ZERO_AMOUNT;
}
return side === MarketOperation.Sell ? output.div(input) : input.div(output);
return side === MarketOperation.Sell ? output.div(targetInput) : targetInput.div(output);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm yeah idk about this –– have you AB tested this change alone?

Copy link
Contributor Author

@dorothy-zbornak dorothy-zbornak Jul 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7001: Using targetInput as input (this PR)
7002: Using input as input (original recipe)

ab-realized-rates

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷 whatever wins I guess

@@ -112,6 +114,7 @@ function dexQuotesToPaths(
ethToOutputRate: BigNumber,
fees: FeeSchedule,
): Fill[][] {
const sourcePathId = hexUtils.random();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be inside the for loop I think

Copy link
Contributor Author

@dorothy-zbornak dorothy-zbornak Jul 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want it to be unique to the whole path. So we can tell which fill belongs to which curve.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm ur rite

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay that fixed my sims

@dorothy-zbornak dorothy-zbornak force-pushed the exp/asset-swapper/path-finding-vroom-vroom branch 2 times, most recently from b5b688e to 43a7d59 Compare July 23, 2020 02:14
@coveralls
Copy link

coveralls commented Jul 23, 2020

Coverage Status

Coverage remained the same at 79.598% when pulling ec1006a on exp/asset-swapper/path-finding-vroom-vroom into 2d29014 on feat/btc-curves.

@dorothy-zbornak dorothy-zbornak marked this pull request as ready for review July 23, 2020 03:50
[ERC20BridgeSource.Native],
[ERC20BridgeSource.Eth2Dai, ERC20BridgeSource.Curve],
[ERC20BridgeSource.Eth2Dai],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait what's going on here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's a little funky that Curve comes right after Uniswap now, but both ways are technically valid so I'm like w/e 🤷

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sus but ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

welcome to asset-swapper lol

[ERC20BridgeSource.Native],
[ERC20BridgeSource.Eth2Dai, ERC20BridgeSource.Curve],
[ERC20BridgeSource.Eth2Dai],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sus but ok

@dorothy-zbornak dorothy-zbornak force-pushed the exp/asset-swapper/path-finding-vroom-vroom branch from ec54580 to ec1006a Compare July 23, 2020 04:39
Base automatically changed from feat/btc-curves to development July 23, 2020 20:43
@dorothy-zbornak dorothy-zbornak force-pushed the exp/asset-swapper/path-finding-vroom-vroom branch from ec1006a to d071054 Compare July 23, 2020 20:46
@dekz dekz merged commit ae2a6fb into development Jul 27, 2020
@dekz dekz deleted the exp/asset-swapper/path-finding-vroom-vroom branch July 27, 2020 05:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants