Multi hopping quote and swap Uniswap V3

arian salmanzadeh
4 min readJul 17, 2023

In the previous article we discussed about multi hopping quote and swap in Uniswap V2, in this article we will cover Uniswap V3 multi hopping both in JavaScript and solidity.

Frist start with getting quote and displaying it. For this Uniswap provided a contract called Quoter which has some interesting function in it. One of the functions is called “quoteExactInput”. This function will return the output amount.

One very important thing to consider is that functions in the quoter contract are not consider as view function and are not gas efficient. Therefore, these functions should be called off-chain and not on-chain.

First we’ll create the quoter function in JavaScript:

for getting the amount out value we have to call the function below with the proper arguments

function quoteExactInput(bytes path, uint256 amountIn) external returns (uint256 amountOut)

as you can see this function gets a path as bytes and the amount in as uint.

For obtaining path first we need to define an array like below:

[token A address — fee — token B address — fee — token C address]

Now we will change the array to bytes by calling one of the methods in ethers.js

as you can see, we passed 3000 for the fee. This means we want to get quote between 1inch-weth with 0.3% fee and API3-Weth with 0.3% fee. The solidity Pack method is as same as the abi.encodePacked() in solidity.

at last, we will call the quoterExatInput function from quoter contract

by calling this function it will return the expected amount of the wanted token.

unfortunately, this function is only a off-chain function therefore, it should only be called off-chain with the keyword “callstatic”.

After getting the returned amount lets implement the swap. for swap we will call the function “exactInput” on the router smart contract.

function exactInput(struct ISwapRouter.ExactInputParams params) external returns (uint256 amountOut)

the input struct of the above function is as below:

struct ExactInput Params {

bytes path;

address recipient;

uint256 deadline;

uint256 amountIn;

uint256 amountOutMinimum;

}

As you can see above, path is of type bytes so we can use the path we have perversely obtained.

For recipient we can use our own wallet address.

For Deadline we can set it to 0 but if we want to fill this you can pass the current time stamp plus the deadline time in seconds.

Also, amountOutMinimum can set to 0 but in production mode this should not be 0. We can pass the expected receive amount minus 10 percent.

Fortunately, swap can be called on-chain therefore let’s see an example of to implement a multi hop swap on-chain in solidity.

First, we create a contract and import Uniswap interfaces, we can also declare an interface but let’s use the Uniswap interface.

After that we have to declare the variables including input token, the middleware token and output token. because fees vary in Uniswap v3 we also have to specify the pool for the swapping tokens. In this example we preferred pools with 0.3% fee. So that being said, now we have to create a struct of type ISwapRouter.ExactInputParams.

The completed function for a multi hop looks like below

Conclusion

The proper way to get quotes from Uniswap V3 is to use the quoter contracts from Uniswap V3. In this article we covered how to quote and swap between pool using a middleware token. this method is called multi hopping and is used in cases when no pool is found for a specific pair of tokens.

--

--