0

Can I perform the following functions using UniswapV2Router at once? Theoretically, as far as I know, if I call both getAmountsIn() and swapTokensForExactTokens() within a single function, both logics will be executed in the same transaction. Therefore, can I use the _estimateAmount[0] from getAmountsIn() as the amountInMax parameter in swapTokensForExactTokens(), and have the user only approve() and transferFrom() the exact _estimateAmount[0] of tokenA to obtain the exact amountOut of tokenB without applying additional slippage? However, I'm not confident in my understanding.

function getEstimateAmountAndSwap() {
    uint[] memory _estimateAmount = router.getAmountsIn(_amount, _path);
    IERC20(tokenA).transferFrom(_user, address(this), _estimateAmount[0]);
    router.swapTokensForExactTokens(_amount, _estimateAmount[0], _path, _user, deadline);
}

1 Answer 1

1

You can set slippage to zero by using the exact amount out at time of you calling. But there is a good chance that on any active pool, a transaction mined before theirs will push price out of range. In which case the transaction will fail and gas fee to execute up until that point will be lost.

If you can be sure, because you are paying a high enough bribe to guarantee a position in the block, or if swapping on a pool with very little activity, it will be fine. Though in those cases it's no different to having set 0.5% slippage and there having been no action on the pool before yours, as you would still just get the amount quoted.

Slippage is more for protection of front running or delayed execution. If set to something low enough so as to make sandwiching unprofitable, it will just act as a buffer for transactions mined before yours, else you will get as much as the pool can give you.

In your case swapping to an exact amount rather than from, you will probably need to handle the refund of unspent tokens.

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.