Swapping token – after a wallet is connected
A user cannot perform token swapping without connecting to a wallet. After connecting to a wallet, the code needs to do the following checks before actually swapping the tokens:
- Verify that the spending amount does not exceed the token balance in the wallet
- Verify whether the account allows
AMMRouter
to transfer the spending amount to the liquidity pool (the instance of theTokenPair
smart contract)
For balance verification, we can load the balances of the two tokens, similar to how we did for AddLiquidity.js
, and disable the SWAP button when the balance is less than the spending amount.
For checking the allowance quota for AMMRouter
to transfer the user’s token, we can check the allowed transferring amount by calling the allowance
function of the ERC20 token, and compare the amount with the spending amount. If the allowed amount is less than the spending amount, the ENABLE button will show up, and the...