Multihop Trades

Introduction

The examples below demonstrate the two styles of multi-hop swapping available in v3. They are not production-ready code and are implemented in a simplified manner for learning purposes.

Setting up the contract

Declare the solidity version that will be used to compile the contract, as well as the helper contracts from Uniswap.

// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import '@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol';
import '@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol';

Create a contract named SwapExamples and declare an immutable public variable, swapRouter, of type ISwapRouter. This variable enables us to call functions from the ISwapRouter interface.

contract ExampleTrades {
    ISwapRouter public immutable brokerbotRouter;

Hardcode the token contract addresses and pool fee tiers for this example. In a production scenario, you would typically use an input parameter and pass it into a memory variable, allowing the contract to dynamically change the brokerbots and tokens it interacts with for each transaction. However, for the sake of conceptual simplicity, we are hardcoding them here.

    address public constant BASE_TOKEN = 0xB4272071eCAdd69d933AdcD19cA99fe80664fc08; //XCHF
    address public constant SHARE_TOKEN = 0x6f38e0f1a73c96cB3f42598613EA3474F09cB200; // DAKS
    address public constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
    address public constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

    // For this example, we will set the pool fee to 0.3%.
    uint24 public constant poolFee = 3000;
    
    constructor(ISwapRouter _brokerbotRouter) {
        brokerbotRouter = _brokerbotRouter;
    }

Buy Shares via Multi Hop

Buying shares through a multi-hop trade involves swapping a variable amount of the input token for a fixed amount of the share token. This process may include an arbitrary number of intermediary swaps. It's important to note that the Path is encoded in reverse because, when purchasing an exact amount of shares, the trade is executed in reverse order. This reverse execution is necessary to pass the required variables for the chain of transactions.

Input Parameter

  • path: The path is a sequence of (tokenAddress, fee, tokenAddress) encoded in reverse order. These elements are the variables necessary to compute each pool contract address in our sequence of swaps. The BrokerbotRouter respectively the UniswapRouter code will automatically identify the correct pool using these variables and execute the necessary swaps within each pool in our sequence.

  • recipient: The destination address of the outbound asset.

  • deadline: The Unix time specifies when a transaction will be reverted. This is done to protect against extended delays and the increased likelihood of significant price fluctuations during that time.

  • amountOut: The desired amount of shares to buy.

  • amountInMaximum: The maximum amount of USDC willing to be swapped for the specified amountOut of shares.

Calling the Function

Selling Shares via Multi Hop

Selling shares through a multi-hop trade involves swapping a fixed amount of shares for the maximum possible amount of desired output tokens. This process may include an arbitrary number of intermediary swaps in between.

Input Parameters

  • path: The path is a sequence of (tokenAddress, fee, tokenAddress) elements, which are the variables required to calculate each pool contract address in our sequence of swaps. The BrokerbotRouter/UniswapRouter code will automatically identify the correct pool using these variables and execute the necessary swaps within each pool in our sequence.

  • recipient: The destination address of the outbound asset.

  • deadline: The Unix time specifies when a transaction will be reverted. This is done to protect against extended delays and the increased likelihood of significant price fluctuations during that time.

  • amountIn: The amount of shares to sell

  • amountOutMin: The minimum required amount of the outbound asset is the threshold below which the transaction will revert. For the purpose of this example, we are setting it to 0. In a production environment, it's advisable to utilize the BrokerbotQuoter to obtain a quote for the expected price.

Calling the Function

The Full Contract

Last updated

Was this helpful?