Multihop swaps
Setting up the Contract
Declare the solidity version that will be used to compile the contract. If needed (if you are using older versions of solidity) add abicoder v2 to allow arbitrary nested arrays and structs to be encoded and decoded in calldata, a feature that is used when completing a swap.
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity =0.8.20;Import the two needed contracts from the npm package installation.
import '@cryptoalgebra/integral-periphery/contracts/interfaces/ISwapRouter.sol';
import '@cryptoalgebra/integral-periphery/contracts/libraries/TransferHelper.sol';Create a contract called SwapExamples, and declare an immutable public variable swapRouter of type ISwapRouter. This allows us to call functions in the ISwapRouter interface.
contract SwapExamples {
// For the scope of these swap examples,
// we will detail the design considerations when using `exactInput`, `exactInputSingle`, `exactOutput`, and `exactOutputSingle`.
// It should be noted that for the sake of these examples we pass in the swap router as a constructor argument instead of inheriting it.
// More advanced example contracts will detail how to inherit the swap router safely.
// This example swaps DAI/WMATIC for single path swaps and DAI/USDC/WMATIC for multi path swaps.
ISwapRouter public immutable swapRouter;Now, hardcode the token contract addresses for the example. In production, you would likely use an input parameter for this and pass the input into a memory variable, allowing the contract to change the pools and tokens it interacts with on a per-transaction basis, but for conceptual simplicity, we are hardcoding them here.
address public constant DAI = 0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063;
address public constant WMATIC = 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270;
address public constant USDC = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174;
constructor(ISwapRouter _swapRouter) {
swapRouter = _swapRouter;
}Exact Input Multi Hop Swaps
Exact input multi hop swaps will swap a fixed amount on a given input token for the maximum amount possible for a given output, and can include an arbitrary number of intermediary swaps.
Input Parameters
path: The path is a sequence of (tokenAddress - pluginDeployer - tokenAddress), which are the variables needed to compute each pool contract address in our sequence of swaps. The multihop swap router code will automatically find the correct pool with these variables, and execute the swap needed within each pool in our sequence. For base pools, use the zero address as the plugin deployer parameterrecipient: is the destination address of the outbound asset.deadline: is the unix time after which a transaction will be reverted, in order to protect against long delays and the increased chance of large price swings therein.amountIn: the amount of the inbound asset.amountOutMin: the minimum amount of the outbound asset, less than which will cause the transaction to revert. For this example we will set it to 0, in production one will need to use the SDK to quote an expected price, or an on chain price oracle for more advanced manipulation resistant systems.
Calling the function
Exact Output Multihop Swap
An exact output swap will trade a variable amount of the input token for a fixed amount of the outbound token. This one can be seen as a less common technique used for multihop swaps. The code for swapping is mostly the same except for one major difference – the Path is encoded backwards, as an exact output swap is executed in reverse order to pass down the necessary variables for the chain of transactions
Input Parameters
path: The path is a sequence oftokenAddress - deployer - tokenAddress, encoded in reverse order, which are the variables needed to compute each pool contract address in our sequence of swaps. This way, the multihop swap router code will automatically find the correct pool with these variables, and execute the swap needed within each pool in our sequence.recipient: the destination address of the outbound asset.deadline: the unix time after which a transaction will be reverted, to protect against long delays and the increased chance of wild price swings therein.amountOut: The desired amount of WMATIC.amountInMaximum: The maximum amount of DAI willing to be swapped for the specified amountOut of WMATIC.
Calling the function
The Full Contract
Last updated