DEV Community

ssadek1976
ssadek1976

Posted on

Ethereum contract call methods do not return any values

Hi there,

I have a problem when running the following 4 lines of code from the main JS script (see below):

  • await uniswapFactory.getPair(daiAddress, wethAddress)
  • await sushiFactory.getPair(daiAddress, wethAddress),
  • const uniswapReserves = await uniswapDaiEth.getReserves();
  • const sushiReserves = await sushiEthDai.getReserves();

For the first two, if I substituted them with hardcoded token pair addresses from Etherscan into the code it works. If I run the first two directly into Truffle console it resolves the pair addresses but strangely enough it doesn´t resolve when I execute the JS script!

For the above two .getReserves() function calls it doesn´t retrieve any of the reserve values unless if I ran them as they are under Truffle console environment then it retrieves them OK.

I appreciate your assistance here as it´s been bugging me for the full last two days and am running out of options to resolve this.

Full JS script

`require('dotenv').config();
const { INFURA_API_KEY, PRIVATE_KEY } = process.env;
const { ethers } = require('ethers');

const UniswapV2Pair = require(".././build/contracts/IUniswapV2Pair.json");
const UniswapV2Factory = require('.././build/contracts/IUniswapV2Factory.json');

const daiAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
const wethAddress = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";

const privateKey = process.env.PRIVATE_KEY;
const infuraProvider = new ethers.providers.InfuraProvider('mainnet', process.env.INFURA_API_KEY);
const wallet = new ethers.Wallet(privateKey, infuraProvider);

const testBasic = async () => {
const uniswapFactory = new ethers.Contract(
"0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
UniswapV2Factory.abi,
wallet
);

const sushiFactory = new ethers.Contract(
"0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac",
UniswapV2Factory.abi,
wallet
);

let uniswapDaiEth;
let sushiEthDai;

const loadPairs = async () => {
uniswapDaiEth = new ethers.Contract(
await uniswapFactory.getPair(daiAddress, wethAddress),
UniswapV2Pair.abi,
wallet
);
sushiEthDai = new ethers.Contract(
await sushiFactory.getPair(daiAddress, wethAddress),
UniswapV2Pair.abi,
wallet
);
};
await loadPairs();

infuraProvider.on('block', async (blockNumber) => {
try {
const uniswapReserves = await uniswapDaiEth.getReserves();
const sushiReserves = await sushiEthDai.getReserves();
} catch (err) {
console.error(err);
}
});
};

testBasic();`

Thank you very much for your help!

Best, Samuel

Top comments (0)