Uniswap / v3-periphery

šŸ¦„ šŸ¦„ šŸ¦„ Peripheral smart contracts for interacting with Uniswap v3
https://uniswap.org
GNU General Public License v2.0
1.16k stars 1.08k forks source link

Am I utilising `getChainedPrice` correctly? #325

Open acemasterjb opened 1 year ago

acemasterjb commented 1 year ago

Hello, I'm trying to utilise getChainedPrice to get the TWAP of a token route and was just wondering if I'm using it correctly:

function getRouteTWAP(
        address[] calldata pools,
        address[] calldata tokens,
        uint32 period,
        uint128 baseAmount
    ) external view returns (uint256) {
        uint256 poolsLength = pools.length;
        int24[] memory tickRoute = new int24[](poolsLength);

        for (uint256 i = 0; i < poolsLength; i++) {
            (tickRoute[i], ) = OracleLibrary.consult(pools[i], period);
        }
        int256 chainedMeanTick = OracleLibrary.getChainedPrice(
            tokens,
            tickRoute
        );

        return
            OracleLibrary.getQuoteAtTick(
                int24(chainedMeanTick),
                baseAmount,
                tokens[0],
                tokens[tokens.length - 1]
            );
    }

My uncertainty about this implementation stems from the fact that getChainedPrice returns an int256 instead of a int24 that is required for getQuoteAtTick.

My second question is: why is the synthetic tick returned by getChainedPrice an int256? Should I be asserting that the synthetic tick doesn't overflow before casting to an int24?