5

In V2, you could call the router contract with getAmountsIn or getAmountsOut, to see how much of a token you would get from swapping x amount of a token, or how many tokens you would have to swap to get x amount of a token.

I'm really struggling with figuring out how to achieve the same with V3. Quoter contract has them as transactions (getQuotedAmountsIn/getQuotedAmountsOut). Is there a way in V3 to calculate the output of a swap without actually sending a transaction and paying gas?

Seems like any similar question is left unanswered. Hoping for some help on this one.

2 Answers 2

1

You can try using Quoter.sol's function quoteExactInputSingle(), it gives the expected amount for the swap in a given single pool. There are other functions for other kinds of swaps.

Github: https://github.com/Uniswap/v3-periphery/blob/v1.0.0/contracts/interfaces/IQuoter.sol

Deployment address: 0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6 (Ethereum mainnet, testnet, polygon, etc.)

0

Using oracle in solidity

import "@uniswap/v3-periphery/contracts/libraries/OracleLibrary.sol";

// you declare first and then initalize in the constructor
address public immutable token0;
address public immutable token1;
address public immutable pool;

function getQuote(
    address tokenIn,
    uint128 amountIn,
    // seconds ago is the duration of Twap
    uint32 secondsAgo
) external view returns (uint amountOut) {
    // add your require logic here
    address tokenOut = tokenIn == token0 ? token1 : token0;
    (int24 tick, uint128 harmonicMeanLiquidity ) = OracleLibrary.consult(pool, secondsAgo);
    amountOut = OracleLibrary.getQuoteAtTick(tick, amountIn, tokenIn, tokenOut);
}
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.