exp/orderbook: Use the correct asset when calculating liquidity pool disbursements #4018
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Checklist
PR Structure
otherwise).
services/friendbot
, orall
ordoc
if the changes are broad or impact manypackages.
Thoroughness
.md
files, etc... affected by this change). Take a look in the
docs
folder for a given service,like this one.
Release planning
needed with deprecations, added features, breaking changes, and DB schema changes.
semver, or if it's mainly a patch change. The PR is targeted at the next
release branch if it's not a patch change.
What
Flips the order in which assets are evaluated when calculating deposit requirements (or "pool expectations"). This is the actual fix:
The problem is not with liquidity pool math, but rather in the way we consume liquidity pools during the orderbook search.
The other changes are to add/update test cases accordingly; refer to the commit message in 6f77891 for a thorough breakdown and justification of them.
Why
#4014 raised a valid issue about the
/paths/strict-receive
endpoint returning impossible paths. A follow-up investigation (comment) puts us down the right path: somehow, somewhere, assets are being swapped.In the issue, we attempt to find an ETH -> USDC path and get results when there shouldn't be any. If we track this through the path-finding algorithm, without this fix, it claims to find the following path:
The first of these is true: we need 0.1134 BTC to get 70000 USDC. This is a standard offer evaluation. The second hop, though, is nonsense. The BTC/YBX pool in question has reserves of 453280/19884933, respectively. It would be impossible to get 11340000 BTC from it, since obviously 453280 < 11340000. The liquidity pool math is tested extensively (see
TestLiquidityPoolMath
andTestLiquidityPoolExchanges
inpools_test.go
), so the issue must be elsewhere.Turns out, this 603359 number is legitimate: this is how many BTC it would take to get 11340000 YBX. That's the wrong exchange direction! When we're working backwards from the destination (as is the case of strict-receive and
FindPaths
), we need to see how much of the asset at the previous hop we'd need to get a payout of the current asset amount. The bug is that we find how much of the current hop we'd need to get the previous amount...Essentially, the mistake is using the incorrect assets corresponding to
X/x
andY/y
in the formula from CAP-38.This closes #4014.
Known limitations
Should we do a minor version bump?