0

I'm attempting to get the pair address of the WETH/DAI pair on Quickswap on the Mumbai test net. I have this quick script:

const ethers = require('ethers');
const Big = require('big.js');
const PATH = require('path');
const CC = require('./common_code');
const address_book = require('./address_book');
const IUniswapV2Factory = require('./IUniswapV2Factory.json');

const PROVIDER = ethers.getDefaultProvider('https://speedy-nodes-nyc.moralis.io/MY_API_KEY/polygon/mumbai');

const FACTORY_ADDR = address_book.address_book['qs_factory'];
const DAI = address_book.address_book['dai'];
const WETH = address_book.address_book['weth'];

const FACTORY = new ethers.Contract(FACTORY_ADDR, IUniswapV2Factory.abi, PROVIDER);

const pairAddr = CC.getPairAddr(FACTORY, DAI, WETH);

console.log(pairAddr);

Where the common_code.js file contains two functions:

const Big = require('big.js');
const ethers = require('ethers');

async function fetchReserves(contract) {
    const reserves = await contract.functions.getReserves();
    return [Big(reserves.reserve0), Big(reserves.reserve1)];
};

async function getPairAddr(factory, tokenA, tokenB) {
    const pairAddr = await factory.functions.getPair(tokenA, tokenB);
    return  pairAddr;
}

module.exports = { fetchReserves, getPairAddr };

and the address_book.js file is as follows:

const address_book = {
    "dai": "0xcB1e72786A6eb3b44C2a2429e317c8a2462CFeb1",
    "weth": "0xA6FA4fB5f76172d178d61B04b0ecd319C5d1C0aa",
    "qs_factory": "0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32"
};

module.exports = { address_book };

The IUniswapV2Factory.json is the abi linked at the bottom of this page, I also got the Factory address from this page as well.

I followed the instructions for calling the getPair() function here

But every time I run this script I get this on the console:

Promise {<pending>}

This is within an async function with an await statement on it. Does anyone know what is causing this error or what I'm doing wrong?

Shouldn't it wait for the promise to finish and then log it?

1 Answer 1

1

You need to await for getPairAddr as well, since it's async - meaning returns a Promise.

Change this -

const pairAddr = CC.getPairAddr(FACTORY, DAI, WETH);

to this -

const pairAddr = await CC.getPairAddr(FACTORY, DAI, WETH);

or, if you don't want to wrap your code in an async function, can also -

CC.getPairAddr(FACTORY, DAI, WETH).then(pairAddr => {
    console.log(pairAddr);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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