5 使用nodejs 获取uniswap V3两个币对交易价格问题

我根据uniswap v3 sdk文档上写一个获取主网USDC-WETH的币种交换价格方法,getCoinPoolPrice()执行后返回一个0.603694 的值,跟实际价格出入太大了并且也没报错,请教一下问题出在哪了 谢谢。。

const POOL_FACTORY_CONTRACT_ADDRESS = '0x1F98431c8aD98523631AE4a59f267346ea31F984';
const QUOTER_CONTRACT_ADDRESS = "0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6";
const chainId = SupportedChainId.MAINNET;
const quoterContract = new ethers.Contract(QUOTER_CONTRACT_ADDRESS, Quoter.abi,provider);

const USDC = new Token(chainId,'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',6);
const WETH = new Token(chainId,'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',18);

const tokens= {
    in: USDC,
    amountIn: 1000,
    out: WETH,
    fee: FeeAmount.MEDIUM,
}
async function getPoolConstants(){
    const currentPoolAddress =computePoolAddress({
        factoryAddress: POOL_FACTORY_CONTRACT_ADDRESS,
        tokenA: tokens.in,
        tokenB: tokens.out,
        fee: FeeAmount.MEDIUM,
    });
    const poolContract = new ethers.Contract(currentPoolAddress, IUniswapV3PoolABI.abi, provider);

    const [token0, token1, fee] = await Promise.all([
        poolContract.token0(),
        poolContract.token1(),
        poolContract.fee(),
    ])

    return {
        token0,
        token1,
        fee,
    }
}

async function getCoinPoolPrice(){
    const poolConstants = await getPoolConstants();

    // quoteExactInputSingle - given the amount you want to swap, produces a quote for the amount out for a swap of a single pool
    // quoteExactInput - given the amount you want to swap, produces a quote for the amount out for a swap over multiple pools
    // quoteExactOutputSingle - given the amount you want to get out, produces a quote for the amount in for a swap over a single pool
    // quoteExactOutput - given the amount you want to get out, produces a quote for the amount in for a swap over multiple pools
    const quotedAmountOut = await quoterContract.callStatic.quoteExactInputSingle(
        poolConstants.token0,
        poolConstants.token1,
        poolConstants.fee,
        fromReadableAmount(
            tokens.amountIn,
            tokens.in.decimals
        ).toString(),
        0
    )
    const a = ethers.utils.formatUnits(quotedAmountOut, 18).slice(-0, 8);
    console.log(a);
}
export function fromReadableAmount(
    amount,
    decimals
){
    return ethers.utils.parseUnits(amount.toString(), decimals)
}
请先 登录 后评论

最佳答案 2023-02-27 09:53

得出的结果是数量吧,想要eth/usdc,价格你把amountIn改为1就行了

请先 登录 后评论

其它 1 个回答

用户_13756
请先 登录 后评论
  • 2 关注
  • 1 收藏,3226 浏览
  • 用户_13756 提出于 2023-02-24 16:34