SushiSwap is a popular Uniswap V2 fork that allows for simple token swaps using the ISushiRouter interface. Below is an example of swapping an ERC20 token for ETH on Scroll Mainnet.
Learn more at UniV2 Docs.
_approve(address(this), address(router), amountToSwap);
router.swapExactTokensForETHSupportingFeeOnTransferTokens(
amountToSwap,
0,
sellPath,
address(this),
block.timestamp
);
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
// Import the Sushi router interface and OpenZeppelin's IERC20
import {IUniswapV2Router02} from "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract SushiScrollDemo {
IUniswapV2Router02 public immutable router;
address public constant DAI = 0x7984E363c38b590bB4CA35aEd5133Ef2c6619C40; // DAI on Scroll Mainnet
address public constant WETH = 0x5300000000000000000000000000000000000004; // WETH on Scroll Mainnet
constructor(address _router) {
router = IUniswapV2Router02(_router);
}
// Swaps a specified amount of DAI for ETH
function swapDAIForETH(uint256 amountToSwap) external {
// Transfer DAI from sender to this contract
IERC20(DAI).transferFrom(msg.sender, address(this), amountToSwap);
// Approve the router to spend DAI
IERC20(DAI).approve(address(router), amountToSwap);
address[] memory path = new address[](2);
path[0] = DAI;
path[1] = WETH;
// Execute the swap; ETH is sent to the sender
router.swapExactTokensForETHSupportingFeeOnTransferTokens(
amountToSwap,
0, // WARNING: Set to 0 for simplicity; consider slippage in production
path,
msg.sender,
block.timestamp
);
}
}