1

I have a simple code to calculates the needed token amount for the cases when I buy a token with 5% slippage. I just simply check how many tokens could I receive for the current price, get its 95% and use the amount for the swapExactETHForTokens function's amountOutMin. My question is that should I pay attention for the decimal of the token? Is there any scenario when I have to use it to calculate the correct token number which fits to the slippage?

This is how I do it now. I don't use the decimal at all.

def get_slippage(token_price, budget, slippage_percentage):
    original_amount = round(budget / token_price)
    one_percent = original_amount / 100
    token_amount_slippage_5p = round(one_percent * slippage_percentage)
    return token_amount_slippage_5p

1 Answer 1

1

You do need to worry about the decimals. You cannot use absolute values because it will mean a completely different number, 100 for a token with 18 decimals would mean 10 ** 16, if there are sandwiches out there, they will sandwich all of it and you will get exactly that, minOut is an important parameter to protect yourself against big slippage and sandwiches.

1
  • 18 decimals wouldn't be 10 ** 18 instead of 10 ** 16?
    – rihekopo
    Commented Mar 18, 2022 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.