If a DEX doesn’t have enough liquidity in the token pair, the price impact will be disproportionately high, leading to significant slippage or failed transactions.
Game
If the DEX’s liquidity is low, the swap might incur heavy slippage, or worse, revert due to insufficient output tokens. What to do??
// SPDX-License-Identifier: MIT// Open me in VSCode and really think before opening the hints!// Add @audit tags wherever suspicious// Go to the solidity docs to complete missing knowledge of what's happening here// Solve by drafting a fix!pragmasolidity ^0.8.0;interface IDEX {functionswap(address tokenIn,address tokenOut,uint256 amountIn) externalreturns (uint256);functiongetAvailableLiquidity(address tokenOut) externalviewreturns (uint256);}contract InsufficientLiquidityGame { IDEX public dex;addresspublic tokenOut;constructor(address_dex,address_tokenOut) { dex =IDEX(_dex); tokenOut = _tokenOut; }functionexecuteSwap(address tokenIn,uint256 amountIn) public {uint256 amountOut = dex.swap(tokenIn, tokenOut, amountIn);require(amountOut >0,"Swap failed"); }}
Before performing a swap, consider how you might verify that the DEX has enough liquidity for the requested token.
Checking the DEX’s available liquidity for tokenOut can help you ensure that the swap is feasible and reduce the risk of slippage or failure.