-
Notifications
You must be signed in to change notification settings - Fork 466
Asset-swapper: Fallback orders + refactors #2513
Asset-swapper: Fallback orders + refactors #2513
Conversation
53af414
to
4b54365
Compare
4b54365
to
1f04198
Compare
66304b5
to
3d4cd22
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taken a quick peek but wanted to discuss some things to get the clear in my mind.
I feel like there are 2 approaches to the goal of lowering reverts with a backup path.
- Find the best path using on-chain sources, use 0x to improve price if available. So we have a backup to 0x failing.
- Find the best path, create another backup path incase the best path fails. This feels a bit difficult to reason about. It could be 100% Uniswap, then 100% 0x as a backup (previously this would be just Uniswap, so little to be gained in this scenario). It could also be 100% Uniswap then 100% Kyber as a backup, which could be a conflict. It can also be the inverse of 100% Kyber with 100% Uniswap backup, also a potential conflict.
I am biased to the 1) approach as that seems simple, but keen to hear your thoughts. In my head 0x should never be the backup as I find it difficult to reason the case where Uniswap would fail and 0x orders would still be fillable.
When a path includes Curve, and a backup path includes another Curve, this is now 2million+ faux gas estimate as a "just in case".
packages/asset-swapper/src/utils/market_operation_utils/index.ts
Outdated
Show resolved
Hide resolved
packages/asset-swapper/src/utils/market_operation_utils/path_optimizer.ts
Outdated
Show resolved
Hide resolved
packages/asset-swapper/src/utils/market_operation_utils/index.ts
Outdated
Show resolved
Hide resolved
What do you think of:
The separate quotes thing is to ensure the optimizer is solving for as close to the actual fill amount as possible, which gives us better pricing. |
`@0x/asset-swapper`: add fallback orders. `@0x/asset-swapper`: Remove `noConflicts` and `dustThreshold` options. `@0x/asset-swapper`: Add `allowFallback` option.
`@0x/asset-swapper`: Make native fills one single path. `@0x/asset-swapper`: Redo the optimizer algo again to be more thorough. `@0x/asset-swapper`: Make `getMedianSellRate()` return `1` if maker token == taker token.
…imal path. `@0x/asset-swapper`: Exclude conflicting sources across both optimal and fallback paths.
1c6ca0c
to
cc12ad8
Compare
`@0x/asset-swapper`: Rename `fees` `SwapQuoter` option to `feeSchedule`.
21e1c53
to
05bf55d
Compare
`@0x/asset-swapper`: Allow Kyber conflicts in fallback path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Want to spend some more time tomorrow just going through the market operations utils deeper. But this is looking good.
/** | ||
* Estimated gas consumed by each liquidity source. | ||
*/ | ||
gasSchedule: { [source: string]: number }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on removing the feeSchedule
and just adding 150e3
to the gasSchedule
where feeSchedule
is used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could go either way. It's probably not good design for AS to assume the protocol fee, even though it already does in a few places (we should remove these).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also not particularly hard for the caller to do the operation you described theirself.
004008e
to
9f2e6b3
Compare
…bug. `@0x/asset-swapper`: Add `maxFallbackSlippage` option.
9f2e6b3
to
37597ec
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of little things but this looks good. Going to simbot now and think more on what this looks like with gasLimit
and if we'd need any adjustment. But worse case we wouldn't be introducing more reverts, just transferring the reported revert as OOG.
packages/asset-swapper/src/utils/market_operation_utils/constants.ts
Outdated
Show resolved
Hide resolved
packages/asset-swapper/src/utils/market_operation_utils/fills.ts
Outdated
Show resolved
Hide resolved
packages/asset-swapper/src/utils/market_operation_utils/fills.ts
Outdated
Show resolved
Hide resolved
packages/asset-swapper/src/utils/market_operation_utils/sampler_operations.ts
Show resolved
Hide resolved
c82fd94
to
1a9063a
Compare
Description
Generate fallback orders
If the
allowFallback
option is passed intoSwapQuoter
, fallback orders can be generated. How this works is we first generate an optimized quote. Then if this quote includes native orders, we will generate a second quote for the same size as the native orders and append it to the original quote. This fallback quote will not reuse any sources in the original quote.We do, however, allow Kyber conflicts in the fallback quote because, even though in theory this should reduce reverts, in reality it increases reverts because with conflicting sources omitted we very often cannot find an fallback path. Even with conflicts, early simulations (w/ bridgeSliipage = 1%) never seem to fail on these quotes, so I think we good?
Gas estimates
Because
eth_estimateGas
will always fail to correctly anticipate the gas cost of fallback orders, AS now attempts to provide agas
estimate in thebestCaseQuoteInfo
andworstCaseQuoteInfo
fields. I'm lazy so they're both the same right now but in the future we can get more clever with the best case.New(ish) optimizer algorithm
I tweaked the algorithm and cleaned it up a lot. We still try to explore as many fill combinations as we can (we have to because fees make price curves non-convex). The algo should also run a bit faster now thanks to Remco's suggestion of iteratively solving two paths at a time, rather than all at once. This is important because we now essentially run it twice in order to generate the fallback orders.
Massive refactors to
market_operation_utils
Yeah, that was turning into a hot mess. All the code in that directory are much nicer and simpler now.
Changes to swap quote options
Removed:
slippagePercentage
: The fallback orders essentially double the possible fill amount already.dustFractionThreshold
: No longer necessary because the optimizer can handle a larger # of paths.noConflicts
: We never set this to false so it's now always on.Added:
allowFallback
: Iftrue
(default), allows the generation of fallback ordersgasSchedule
: Estimated gas usage of filling a liquidity source-- similar tofeeSchedule
but expressed in gas instead of ETH.maxFallbackSlippage
: The maximum rate % change allowed in the fallback (compared to the optimal quote). If the fallback's slippage exceeds this %, no fallback will be provided.Renamed:
fees
->feeSchedule
Ordering of orders in quote
Because of how the new optimizer works, there is no longer a strong guarantee that orders will be sorted by decreasing rate. If you relied on this, sorry boo.
Bugs
Testing instructions
Types of changes
Checklist:
[WIP]
if necessary.