Perform a Swap
Executing a Swap: Two Approaches
There are two methods available for initiating a swap, each with its own distinct advantages. The first method involves direct interaction with the Pair contract, while the second utilizes the Router contract. The primary distinction lies in the fact that the second method can facilitate the swapping of ETH, wherein the Router converts ETH to WETH before executing the actual swap. This section will provide guidance on both options.
1. Direct Swap within Pair Contract
Within the Pair contract, you'll encounter the following two functions. You have the flexibility to select the token for swapping, be it token0 or token1, and proceed with the swap.
// Pair.sol
function swap0in(
address to,
uint input,
uint minOutput) external lock returns (uint output)
function swap1in(
address to,
uint input,
uint minOutput) external lock returns (uint output)Parameters:
to: Address to receive swapped token.input: Amount of token to swap.minOutput: Minimum amount of token expected to receive.
Return Value:
output: Amount of token swapped.
Set Up your Contract
Declare the solidity version used to compile the contract.
Import the pair interface and IERC20 interface.
Write your own contract, here we name it
MySwapTest. Remember that the caller mustapprovethe contract to withdraw the tokens from the calling address's account to execute a swap.
Token0 and Token1, which is which?
The table presented below outlines the corresponding relationship. Specifically, concerning the Polygon zkEVM, token0 represents $DYSN, and token1 represents $USDC within the DYSON-USDC pair. The order is determined based on the address size, as follows:
Verification of this information can also be done through Blockchain Explorer, including Polygonscan and Lineascan.
Polygon zkEVM:
DYSON-USDC pair
$DYSN
$USDC
WETH-USDC pair
$WETH
$USDC
Linea:
WETH-USDC pair
$USDC
$WETH
2. Swap through Router Contract
Alternatively, you can execute a swap through the Router contract. You can initiate a swap using the functions below within the Router contract.
Parameters:
tokenIn: Address of the spent token.tokenOut: Address of the received token.index: Number of the pair instance.to: Address that will receive tokenOut.input: Amount of tokenIn to swap.minOutput: Minimum amount of tokenOut expected.
Return Value:
output: Amount of token swapped.
In the case of swapping ETH, you can use the functions below.
Parameters:
tokenOut: Address of received token.index: Number of pair instance.to: Address that will receive tokenOut.minOutput: Minimum of token1 expected to receive.
Return Value:
output: Amount of tokenOut received.
Parameters:
tokenIn: Address of spent token.index: Number of pair instance.to: Address that will receive ETH.input: Amount of tokenIn to swap.minOutput: Minimum of ETH expected to receive.
Return Value:
output: Amount of ETH received.
Set Up your Contract
Declare the solidity version used to compile the contract.
Import IRouter interface and IERC20 interface.
Create your custom contract. The following code snippets showcase the process of swapping between $DYSN and $USDC, as well as between $WETH and $USDC.
swapWithMultiHops
In the context of Dyson Finance, assuming there are currently three pairs: token0-token1, token1-token2, and token2-token3. If a user intends to swap from token0 to token3, they should employ the following functions: swapWithMultiHops, swapETHInWithMultiHops, or swapETHOutWithMultiHops to achieve the desired outcome, as opposed to invoking a regular swap function.
Parameters:
tokens: Array of swapping tokens.indexes: Array of pair instance.to: Address that will receive tokenOut.input: Amount of tokenIn to swap.minOutput: Minimum amount of tokenOut expected.
Return Value:
output: Amount of tokenOut received.
Reiterating, our platform indeed facilitates the swapping of ETH in and out with multiple hops. The fundamental logic aligns with the original swapWithMultiHops function, with these specific functions aiding in the seamless conversion between ETH and WETH as required. In particular, swapETHInWithMultiHops transforms the user's input ETH to WETH before the actual swap, while swapETHOutWithMultiHops converts the output WETH back to ETH after swap and returns it to the user.
Set Up your Contract
Declare the solidity version used to compile the contract.
Import IRouter interface.
Write your own contract. Let's consider a scenario where there are three pairs:
token0-token1,token1-token2, andtoken2-token3, with each pair having an index of 1. Now, if a user aims to execute a swap from token0 to token3 using theswapWithMultiHopsfunction, they need to provide an array that signifies the token paths:[token0, token1, token2, token3], along with the indices of each pair:[1, 1, 1].
Last updated