Uniswap / v2-sdk

🛠 An SDK for building applications on top of Uniswap V2
https://uniswap.org/docs/v2/SDK/getting-started
MIT License
428 stars 1.09k forks source link

Fails to return reserve outputs from getReserves() #115

Open Samboy76 opened 1 year ago

Samboy76 commented 1 year ago

Hello,

I have an issue in that I cannot get passed executing code line await pairContract.functions.getReserves(); shown in below code snippet. There is no error caught in trycatch so what am I doing wrong here? Any suggestions please?

const getPair = async (other) => {
    const pairAddress = Pair.getAddress(other, WETH[other.chainId], provider,);
    console.log("pairAddress ", pairAddress);

    const pairContract = new ethers.Contract(
        pairAddress,
        UniswapV2Pair.abi,
        provider,
    );

    console.log("pairContract ", pairContract.address);
    try {
        const reserves = await pairContract.functions.getReserves();
        console.log("reserves ", reserves);
    } catch (error) {
        console.log("error text ", error);
    }

    const [reserve0, reserve1] = reserves;

    const tokens = [other, WETH[other.chainId]];
    const [token0, token1] = tokens[0].sortsBefore(tokens[1]) ? tokens : [tokens[1], tokens[0]];

    const pair = new Pair(new TokenAmount(token0, reserve0), new TokenAmount(token1, reserve1));
    return pair;
};

Truffle v5.5.31 (core: 5.5.31) Ganache v7.4.3 Solidity - >=0.5.0 <0.9.0 (solc-js) Node v16.17.0 Web3.js v1.7.4

Thank you

Florian-S-A-W commented 1 year ago

Hi @Samboy76

Did you verify that the Pair address is correct? Does a pair exist for the two tokens?

What version of the SDK are you using? Pair.getAddress only accepts 2 parameters in the latest version. Please make sure you are using the v2-sdk package and not the deprecated uniswap/sdk

I can not reproduce your issue, calling getReserves on any UniswapV2Pair contract works for me. If the address is correct and the address you are calling is a UniswapV2Pair, this is not an issue with this SDK but either with the RPC provider you are using or with Web3.js.