I am writing some smart contract code that needs to add liquidity to a Uniswap V2 pair.
I have already performed some validation checks at the start of my transaction function to ensure that the current price is acceptable for providing liquidity.
My contract holds a lot of Token A
and a small bit of Token B
.
I would like my contract to use all remaining Token B
for providing liquidity, along with whatever amount of Token A that is neccessary.
This means I would like to specify an exact amount of Token B
, and let Uniswap calculate how much Token A
to take away from me.
Is this the correct way to achieve this?
uint exactTokenBAmount = _tokenB.balanceOf(address(this));
_tokenA.approve(address(_router), 2 ** 256 - 1);
_tokenB.approve(address(_router), exactTokenBAmount);
_router.addLiquidity(address(_tokenA), address(_tokenB), 0, exactTokenBAmount, 0, exactTokenBAmount, address(this), block.timestamp);
As you can see, I am specifying the exact amount for Token B
, but specifying 0
for Token A
in the hopes that Uniswap can calculate this amount itself.
Are there any problems with this way of doing it, and if so, is there another way I should be doing this?