You'd need to do the following steps. You could also check out defi-minimal for more minimal examples of working with smart contracts.
We are going to use uniswap, but sushiswap has essentially the same functions.
1. Get the contract's interface
Get the Sushiswap/Uniswap ABI in your code by adding its interface including the functions you'd want to use. For swapping, it might look like this:
interface IUniswapV2Router { function swapExactTokensForTokens( uint256 amountIn, uint256 amountOutMin, address[] calldata path, address to, uint256 deadline ) external returns (uint256[] memory amounts); }
2. Get Contract addresses
Let's say you wait to work with Mainnet ETH with the Uniswap router.
// Uniswap Mainnet ETH Router: "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
Note: It's also good to know the WETH address since most tokens have a pool with WETH.
3. Build a swap function with input parameters
According to the uniswap abi / docs you need for input parameters:
uint256 amountIn, // amount of tokenA in uint256 amountOutMin, // minimum amount of tokenB you're expecting back address[] calldata path, // the path of trading you expect. // Like what pools are you going to interact with? For example: // USDC -> WETH -> SOME_OBSCURE_TOKEN // or // DAI -> WETH // This requires an understand of how the pools work address to, // who will recieve the tokens uint256 deadline // how long is this trade good for
Then, you'd build an example func like so:
address private constant UNISWAP_V2_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; address private constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; function swap( address _tokenIn, address _tokenOut, uint256 _amountIn, uint256 _amountOutMin, address _to ) external { IERC20(_tokenIn).transferFrom(msg.sender, address(this), _amountIn); IERC20(_tokenIn).approve(UNISWAP_V2_ROUTER, _amountIn); address[] memory path; if (_tokenIn == WETH || _tokenOut == WETH) { path = new address[](2); path[0] = _tokenIn; path[1] = _tokenOut; } else { path = new address[](3); path[0] = _tokenIn; path[1] = WETH; path[2] = _tokenOut; } IUniswapV2Router(UNISWAP_V2_ROUTER).swapExactTokensForTokens( _amountIn, _amountOutMin, path, _to, block.timestamp ); }
And your contract can now perform swaps.