Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(slippage): small order slippage v2 #4934

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from

Conversation

alfetopito
Copy link
Collaborator

@alfetopito alfetopito commented Oct 1, 2024

Update 2024/10/03

Added feature flag smartSlippageFeeMultiplierPercentage, that returns the multiplier value and can still be used to feature flag the individual feature of small orders slippage.

Also, slippage is now capped at 50%.

New testing steps

  1. Enable smartSlippageFeeMultiplierPercentage and disable isSmartSlippageEnabled
  2. Pick sell/buy/amounts and observe the suggested slippage
  • Should still work independent from BFF's slippage
  1. Pick a tiny sell amount and observe the UI and the console logs
  • Max suggested UI value should be 50%
  • Console logs should show more than 5000
    image

One more fun test is to add/pick new values to be served when on for the new feature flag:
image
And then observe the slippage change accordingly in the UI.

Summary

Supersedes #4911

Instead of using the fee % in relation to order size, calculate how much slippage is needed if the fee is 50% higher.
This value is added to the one returned by BFF.

⚠️ Tooltips still not updated

To Test

  1. Make sure the Launch darkly flag is on
  2. This will depend on current network conditions, so play around with input amounts to get it within those ranges
  3. Place the order
  • Should trade somewhat fast, hopefully. Not guaranteed, though.

You can also observe all the smart slippage values by filtering the console logs by SmartSlippage

Copy link

vercel bot commented Oct 1, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
cosmos ✅ Ready (Inspect) Visit Preview Oct 4, 2024 5:20pm
cowfi ✅ Ready (Inspect) Visit Preview Oct 4, 2024 5:20pm
explorer-dev ✅ Ready (Inspect) Visit Preview Oct 4, 2024 5:20pm
swap-dev ✅ Ready (Inspect) Visit Preview Oct 4, 2024 5:20pm
widget-configurator ✅ Ready (Inspect) Visit Preview Oct 4, 2024 5:20pm

@elena-zh
Copy link

elena-zh commented Oct 2, 2024

Hey @alfetopito , I have some questions to clarify/suggestions:

  1. Why is the bff slippage added to the newly calculated one?
    image

  2. Why the slippage amount to add is so huge? Is this calculated on top of the granular slippage of 20%? Buy order:
    image
    Sell order:
    image

  3. When I keep 'Confirm' modal opened, I can see that slippage amount is constantly changing there. IMO, it is not good thing as it leads to the trade outcome change. So I'd whether show 'Price updated' warning on the modal OR freeze the slippage value on the modal until the order placement
    image

  4. When a price is loaded, I can see several slippage requests on console. Is this OK?
    3 slipp

  5. As we discussed in the meeting, we have 50% slippage limitation in the settings, so it would be nice to have it as a max when calculating a custom one
    image

@elena-zh
Copy link

elena-zh commented Oct 4, 2024

Hey @alfetopito , it works :)
But could you please explain what 'when targeting is off' and 'fallback variation' means? and dependency between them?

Also I've noticed that when:

  1. 2 flags (smart slippage and fee multiplier) are off, the previously used slippage is not reset and is displayed for every trade
    to 0
  2. Same happens when set 'when targeting is off' to 0.

@alfetopito
Copy link
Collaborator Author

Hey @alfetopito , I have some questions to clarify/suggestions:

1. Why is the bff slippage added to the newly calculated one?
   ![image](https://private-user-images.githubusercontent.com/70885163/372885335-c7e44b34-c949-45d3-8054-26d86157515f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjgwMjU4NzUsIm5iZiI6MTcyODAyNTU3NSwicGF0aCI6Ii83MDg4NTE2My8zNzI4ODUzMzUtYzdlNDRiMzQtYzk0OS00NWQzLTgwNTQtMjZkODYxNTc1MTVmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDEwMDQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQxMDA0VDA3MDYxNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTM1YTJkNWU3MjgyYmQzMjUwNzMwY2ZkY2JmMWZmYjYxMDM5NzFhNTdiYzFlNGViMjg5M2RlMTUxMzJjMzk1M2EmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.iBIB5MXPBqKocIxlVQ2kFmCF0fDoyLi-hBVM10iKjKo)

Already explained over the call.
In summary, for bigger amounts it'll be negligible.
For smaller amounts, BFF will be adjusted.

2. Why the slippage amount to add is so huge? Is this calculated on top of the granular slippage of 20%? Buy order:
   ![image](https://private-user-images.githubusercontent.com/70885163/372887958-b47e510d-c87f-444a-9580-25af43dfc3f8.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjgwMjU4NzUsIm5iZiI6MTcyODAyNTU3NSwicGF0aCI6Ii83MDg4NTE2My8zNzI4ODc5NTgtYjQ3ZTUxMGQtYzg3Zi00NDRhLTk1ODAtMjVhZjQzZGZjM2Y4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDEwMDQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQxMDA0VDA3MDYxNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTMxOTQ4YmEwMjZhODVjNTNiMDlkNTNmZjk1ZWRjNjA5MGM0OWYxY2RjYTBjYWJhYTM2MTdjY2YzZjdhMjBiZTQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.mTtRHvpq57gVRLiYKNqMyiS3RicWdo7rjh2046cpTTo)
   Sell order:
   ![image](https://private-user-images.githubusercontent.com/70885163/372892191-1b016707-b5e8-4239-b9b6-5c9bf24fb747.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjgwMjU4NzUsIm5iZiI6MTcyODAyNTU3NSwicGF0aCI6Ii83MDg4NTE2My8zNzI4OTIxOTEtMWIwMTY3MDctYjVlOC00MjM5LWI5YjYtNWM5YmYyNGZiNzQ3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDEwMDQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQxMDA0VDA3MDYxNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTg2ZTA3MTAxOTM1YzA1ODdkMjZiMzc5NmNkZjgwZTAxOGNjODFhOGRkYmUwYmZjMzgwNmRkZWM0ZTNhNTNmYWImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.TmJt6A0wmh_CT25Xc-3tDouczxZlyHxRvDU0D0gc40U)

Doesn't it match the calculations in the spreadsheet? I mean, apart from BFF, you can disregard that.

3. When I keep 'Confirm' modal opened, I can see that slippage amount is constantly changing there. IMO, it is not good thing as it leads to the trade outcome change. So I'd whether show 'Price updated' warning on the modal OR freeze the slippage value on the modal until the order placement
   ![image](https://private-user-images.githubusercontent.com/70885163/372888930-d9725a42-2aa7-4203-9ea4-5f603e8c0256.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjgwMjU4NzUsIm5iZiI6MTcyODAyNTU3NSwicGF0aCI6Ii83MDg4NTE2My8zNzI4ODg5MzAtZDk3MjVhNDItMmFhNy00MjAzLTllYTQtNWY2MDNlOGMwMjU2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDEwMDQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQxMDA0VDA3MDYxNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI4NmUwYTA4YzM2MzUzZGY1NTA1OWZiZjdmMGM4YWIzNjZlYzQzZDkyNDhjMzE5Njc0Y2M0YjEzZjBhZDFhOTAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.baWrMiSfCooQYmYSOFp3FFtRMhpw6NkcO6mp2kfqfy8)

Good point, will fix it

4. When a price is loaded, I can see several slippage requests on console. Is this OK?
   ![3 slipp](https://private-user-images.githubusercontent.com/70885163/372889456-db732a6d-5eed-4c8e-bd59-ef56a51bdea0.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjgwMjU4NzUsIm5iZiI6MTcyODAyNTU3NSwicGF0aCI6Ii83MDg4NTE2My8zNzI4ODk0NTYtZGI3MzJhNmQtNWVlZC00YzhlLWJkNTktZWY1NmE1MWJkZWEwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDEwMDQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQxMDA0VDA3MDYxNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTM1ZjI4NGJhOGVkMGI4NzZhNGZkMDNiOGMzOGI1Mjk1YWFjNDhmNWRjMzcxY2ZkNDc2NWI4ZWQ5NmYzMjdlYjEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.XWVowPizL66X8izeWA2JDPdDixeOg5yRrKBzaD_AAfc)

It's ok, as it comes from different sources

5. As we discussed in the meeting, we have 50% slippage limitation in the settings, so it would be nice to have it as a max when calculating a custom one
   ![image](https://private-user-images.githubusercontent.com/70885163/372894079-f401876d-17e8-40f7-b763-12ffbef6eb8e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjgwMjU4NzUsIm5iZiI6MTcyODAyNTU3NSwicGF0aCI6Ii83MDg4NTE2My8zNzI4OTQwNzktZjQwMTg3NmQtMTdlOC00MGY3LWI3NjMtMTJmZmJlZjZlYjhlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDEwMDQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQxMDA0VDA3MDYxNVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQzYzc1N2NmNzRhMjBkYWY0MTE4N2RlNDIwODRhOTU1ZDMzMTkyNTQ2ZDQ4MjI2ODU3OTVmMGI1MTdmMTA1Y2QmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.9yfiwj5ITcNf3uYwcoABDZ3lbKwPhWiGwH5lNrs2fZM)

Added the limitation when both are added together to not go over 50%.

@alfetopito
Copy link
Collaborator Author

Hey @alfetopito , it works :) But could you please explain what 'when targeting is off' and 'fallback variation' means? and dependency between them?

Targeting off => serve 0
Fallback variations => different values to serve, if we want to change to something other than 50%

Also I've noticed that when:

1. 2 flags (smart slippage and fee multiplier) are off, the previously used slippage is not reset and is displayed for every trade
   ![to 0](https://private-user-images.githubusercontent.com/70885163/373690523-8156ce2d-0a04-4fbf-bf96-e49f39d1bed6.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjgwNTQ4MjcsIm5iZiI6MTcyODA1NDUyNywicGF0aCI6Ii83MDg4NTE2My8zNzM2OTA1MjMtODE1NmNlMmQtMGEwNC00ZmJmLWJmOTYtZTQ5ZjM5ZDFiZWQ2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDEwMDQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQxMDA0VDE1MDg0N1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWFmODgxMmJmYmUwYTY3ZjYxZjI1ZGM3ZmJiOTc5OTY0MTY1ZTY2MmI0N2EyMmVjYzUxZDRhNTM2M2QzMzA2NWQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.BwBn8o11OnbZDF8jpmaakpntlFlx31YkmHBRXlaYLv8)

2. Same happens when set 'when targeting is off' to 0.

The slippage was not being reset when both were null and there was a previously set value.
Fixed

@alfetopito
Copy link
Collaborator Author

@elena-zh point 3 from the initial comment is fixed 🤞


import { calculateBpsFromFeeMultiplier } from './calculateBpsFromFeeMultiplier'

import { useReceiveAmountInfo } from '../../../trade'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
import { useReceiveAmountInfo } from '../../../trade'
import { useReceiveAmountInfo } from 'modules/trade'

import { useDerivedTradeState, useIsWrapOrUnwrap } from 'modules/trade'

import { smartSwapSlippageAtom } from '../state/slippageValueAndTypeAtom'
import { useDerivedTradeState, useIsWrapOrUnwrap } from '../../../trade'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
import { useDerivedTradeState, useIsWrapOrUnwrap } from '../../../trade'
import { useDerivedTradeState, useIsWrapOrUnwrap } from 'modules/trade'

* More relevant for small orders in relation to fee amount, negligent for larger orders.
*/
export function useSmartSlippageFromFeeMultiplier(): number | undefined {
const { beforeNetworkCosts, afterNetworkCosts, costs, isSell } = useReceiveAmountInfo() || {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Awesome! Thanks for the using useReceiveAmountInfo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants