Refactoring smart contracts to support native tokens in a DEX
As we discussed in previous chapters, the frontend of the DEX interacts with the AMMRouter
smart contract to add liquidity, remove liquidity, and swap tokens. The AMMRouter
smart contract transfers the tokens from the user to one instance of the TokenPair
smart contract when adding liquidity and swapping tokens. It transfers tokens back from one of the TokenPair
smart contracts to a user when removing liquidity and swapping tokens. If a liquidity pool-based DEX wants to deal with ETH or any native token, these processes need extra steps that involve wrapping or unwrapping the native tokens.
Next, we will discuss how AMMRouter
works as the intermediary between the native token and TokenPair
smart contracts.
The router as the intermediary
The router of a DEX plays an important role in performing all the extra steps for handling native tokens. It means the router will work as the intermediary between the user and...