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

Update default slippage #718

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

vyorkin
Copy link
Contributor

@vyorkin vyorkin commented Feb 18, 2025

No description provided.

@vyorkin vyorkin requested a review from zerts February 18, 2025 05:07
Copy link

github-actions bot commented Feb 18, 2025

📦 build.zip [updated at Feb 20, 7:33:39 AM UTC]

);

const { defaultSlippagePercent } = getSlippageOptions(chain);
const slippage = userSlippage ?? defaultSlippagePercent / 100;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think, it can be more consistent to use the same units all over the code.
What do you think about just have defaultSlippage = 0.01?

Copy link
Contributor

Choose a reason for hiding this comment

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

However, I can see that is was also this inconsistent before, so not a big problem then

Copy link
Contributor Author

@vyorkin vyorkin Feb 18, 2025

Choose a reason for hiding this comment

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

I agree, I am also not happy about this but decided to leave it as it was before. I mean I didn't want to refactor this as part of the PR and keep changes minimal, easier to review

? toPercents(userSlippage)
: defaultSlippagePercent;

const [percentValue, setPercentValue] = useState(() => String(slippage));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we cast String() in the previous expression? And use here just useState(slippage)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


const DEFAULT_SLIPPAGE_OPTIONS: SlippageOptions = [1, [0.5, 1.0]];
const SLIPPAGE_BY_CHAIN: SlippageConfig = {
ethereum: [1, [0.2, 0.5]],
Copy link
Contributor

@zerts zerts Feb 18, 2025

Choose a reason for hiding this comment

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

shouldn't it be ethereum: [0.5, [0.2, 0.5]],?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. Well let's make this an array then? This first value is a bit confusing :)

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... maybe, this is why I added comment above the SlippageOptions types:
// [defaultOptionIndex, [option1, option2]]
The first field is the defaultOptionIndex

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just updated this part, it is an object now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed these types as we discussed ✅

import type { Chain } from 'src/modules/networks/Chain';

// [defaultOptionIndex, [option1, option2]]
type SlippageOptions = [number, [number, number]];
Copy link
Contributor

@zerts zerts Feb 18, 2025

Choose a reason for hiding this comment

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

What do you think about making this an object?

type SlippageOptions = {
  default: number,
  options: [number, number]
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I initially implemented the SlippageOptions type as an object type, exactly as shown in your snippet. However, I later reconsidered this approach as I dislike the repetition of field names, especially considering we might switch to firebase configuration in the future. In my opinion, a more elegant solution would be to use a direct "tuple type" like this: type SlippageOptions = [number, number, number]; with a descriptive comment above. But perhaps this is just my preference...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just discussed this with @everdimension , going to update this type to: { default: number; options: [number, number]; }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated ✅

</>
)}
renderWhenOpen={() => {
invariant(chain, 'Chain must be defined');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't this lead to an error for the user? Shouldn't we just wait until chain is not null?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I see that the line that should call for this view shows only when chain is not null. So may be there is no problem

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we have similar invariant below, shouldn't be a problem

@zerts
Copy link
Contributor

zerts commented Feb 18, 2025

We need to change validation logic here:
May be we can to change validation to >1%, not >= 1%?
Zerts Screenshot 2025-02-18 at 12 13@2x

@zerts
Copy link
Contributor

zerts commented Feb 18, 2025

Also now we show warning line when slippage is 1%
May be we need to show it in plain color, not yellow?
Zerts Screenshot 2025-02-18 at 12 15@2x


const { default: defaultSlippagePercent } = getSlippageOptions(chain);
const slippagePercent = String(
userSlippage ? toPercents(userSlippage) : defaultSlippagePercent
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't forget this PR top be merged https://github.com/zeriontech/transactions-ui/pull/15/files and package.json to be updated

Copy link
Contributor

Choose a reason for hiding this comment

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

Also here we need to use ?? instead of ?
Slippage: 0 technically should be possible.

);

const { default: defaultSlippagePercent } = getSlippageOptions(chain);
const slippage = userSlippage ?? defaultSlippagePercent / 100;
Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/zeriontech/zerion-wallet-extension/pull/718/files#diff-efaab4620150428de87d86195860eecaa845e8f1f328a06e0f71c10d09f5297aR102-R104

Let's share this code together. Maybe inside a function. Cause right now the have different implementation even with the fact that they should be almost similar :))

As an idea, getSlippageOptions can take userSlippage as an optional param and take this into consideration


const price = swapView.receiveAsset?.price?.value || 0;
const fiatValue = new BigNumber(receiveInput || 0)
.times(price)
.times(1 - slippage);

return slippage >= HIGH_SLIPPAGE_THRESHOLD ? (
return slippage > HIGH_SLIPPAGE_THRESHOLD ? (
Copy link
Contributor

Choose a reason for hiding this comment

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

There was a discussion about this logic not so long ago but I think, this is ok for now. I don't want to make HIGH_SLIPPAGE_THRESHOLD dependant on chain too in this PR

getSlippageOptions(chain);

const slippage = String(
userSlippage ? toPercents(userSlippage) : defaultSlippagePercent
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

2 participants