Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

🎶 ABA Test: Sell and buy back to calculate slippage #1495

Closed
Tracked by #1627
anxolin opened this issue Sep 29, 2021 · 5 comments
Closed
Tracked by #1627

🎶 ABA Test: Sell and buy back to calculate slippage #1495

anxolin opened this issue Sep 29, 2021 · 5 comments
Assignees

Comments

@anxolin
Copy link
Contributor

anxolin commented Sep 29, 2021

Description
Xdai, has many USD estimations missing. This is because, ther USD oracles are for mainnet.

This ABA test (A->B->A test) cosist on simulate doing a trade back and forth the asset, and see how much you get back from your original.

In a naive implementation we could say that slippage is half of that.
i.e.
10 DAI gives me 9 USDC
9 USDC gives me 8 DAI
100 * (10-8)/10/2 ----> 10%

However, dividing by two wouldn't give us the estimated slippage.

The idea is to solve it in the following way:

Note: It has a small error factoring (p-ps)(1-s)) (one of the signs are wrong)
image

Context:
https://gnosisinc.slack.com/archives/CPZA1AGKY/p1632220484032000?thread_ts=1632210034.021000&cid=CPZA1AGKY

@anxolin anxolin changed the title ABA Test: Sell and buy back to calculate slippage 🎶 ABA Test: Sell and buy back to calculate slippage Sep 29, 2021
@anxolin
Copy link
Contributor Author

anxolin commented Oct 25, 2021

As we discussed in the sync, @W3stside this could be implemented after #1689

Also, we mentioned about writing down an example for buy sell orders.

As I see it, the ABA test will have different parts. The most important will be a function that receives the original amount of FROM tokens, and the estimated FROM tokens you get back after trading forth-and-back.

What forth-and-back means for:

  • Sell orders: Basically, once we have the quote (User sells 10 DAI, receives at least 9 USDC), we sell what he would receive again (user sell 9 USDC, receives at least 8 DAI). Then we will use the ABA formula to calculate the slippage based on these 2 values (10DAI and 8 DAI)
  • Buy orders: Similarly, once we have the quote (Users buys 10 USDC, and sells at most 11 DAI for it), we sell what he would receive (user sells 10 USDC for at least 9 DAI). Then we will use the ABA formula to calculate the slippage based on these 2 values (11DAI and 9 DAI)

@anxolin
Copy link
Contributor Author

anxolin commented Nov 30, 2021

So, if this method is correct, the generalised formula should be:

Slippage = 1 +- sqrt(Pab * Pba)

Note that depending how u define Pba u might need to divide instead of multiply inside the square root. In the formula is supposed to be the price expressed in the same quote, this way is multiply, but if it’s just the inverse should be a division

For a practical example, we could use the original:

  • AB trade: 100A are turned into 80B
  • BA trade: 90B is turned into 56A

Pab = 80/100 = 0.8
Pba = 56/80 = 0.7
S = 1 - sqrt( 0.8*0.7 ) = 0.2516685226 --> 25%, valid cause is a number between 0 and 1
S = 1 + sqrt( 0.8*0.7 ) = 1.7483314774 --> Discarded, out of the range [0, 1]

The generalisation of the formula:
IMG_3548 2

@elena-zh
Copy link

Hey @anxolin , will we explain somehow to a user that calculation of the price impact has changed?
Will we have something like a tooltip for this, or new FAQ question?
image

@anxolin
Copy link
Contributor Author

anxolin commented Nov 30, 2021

Hey @anxolin , will we explain somehow to a user that calculation of the price impact has changed?
Will we have something like a tooltip for this, or new FAQ question?

Good question. In principle we would avoid adding it to FAQ, ideally should be easy to understand the number (it should give reasonable numbers).

If users ask a lot, we could give more context

@anxolin
Copy link
Contributor Author

anxolin commented Nov 30, 2021

There’s something it was bugging me a bit, and is that I was not 100% sure about how to pick the sign in the sqrt result. Is it + or -

My first intuition was that it should be a number between 0 and 1, so we discard the other.

I tried to reflect more on this, and now I think we should always take the NEGATIVE sign

Again, i tried to reason with math, so probably i made some mistake, although what i see have kind of sense.

Here is my reasoning. I think, when the S is negative, means, I don’t have slippage, so im making money with the round trip. I would expect a negative slippage and unbounded! u can make as much as u want 😃

When the S is positive, then it means i have some slippage, so i'm loosing some money. How much I can loose? All! (but not more). This means that S is between 0 and 1

With this, I could formulate the 3 base cases:
IMG_3551

image

The part i didn’t get completly now is, why 1 - αPba is always >0 i just assume it was, because otherwise there was no solution in the real domain of numbers.

would that make sense?

@elena-zh elena-zh closed this as completed Dec 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants