Uniswap v3 Factory Functions – How to Access the .getPool() Function in Node.js

contract-debuggingfunctionjavascriptnodejsuniswap

As a personal exercise, I am trying to convert the following Uniswap v2 project to Uniswap v3:

Uniswap token price monitoring (this is not swapping), i.e. ETH/DAI
https://www.youtube.com/watch?v=j_wVh_Sa1rU
https://github.com/RudreshVeerkhare/UniswapPriceMonitor

I am focusing trying to subscribe to the WebSocket event that fires then the pool price is updated and have gotten up to the point of needing to get the factory pool address for the DAI/WETH (500 fee tier) pair.

I believe the function is available as seen here:

console log of factory

Code setup:

require("dotenv").config({});

const { ethers } = require('ethers');
let alchemyRopstenProvider = new ethers.providers.JsonRpcProvider(process.env.RPC_ALCHEMY_HTTP_ROPSTEN_URL);
let alchemyRopstenWSProvider = new ethers.providers.WebSocketProvider(process.env.RPC_ALCHEMY_WSS_ROPSTEN_url, "ropsten");

//import {abi as FACTORY_ABI, bytecode as FACTORY_BYTECODE} from '@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json'
const FACTORY_ABI = require("@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json").abi
const FACTORY_BYTECODE = require("@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json").bytecode
const UniswapV3Factory = require("@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json");        // ABI file
const UniswapV3Pool = require("@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json");
const provider = alchemyRopstenProvider;    // set the provider
const wsProvider = alchemyRopstenWSProvider;
let wallet = new ethers.Wallet(process.env.WALLET_PRIVATE_KEY, provider);
// needed to get things like pool addresses from token symbols AND feeTier (100,500,3000) - getPool(address,address,uint24)
const uniswapV3Factory = new ethers.Contract("0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f", FACTORY_ABI, wallet);
const Big = require("big.js");
//const Web3 = require('web3');   //change to ethers

module.exports = { ethers, provider, wsProvider  }
//
// End Config
//

console.log("starting...");

const INTERVAL = 5000;      // ms

// define address of Pair contract 
// Pair address is found from the Uniswap pools page; address from pair URL - https://v3.info.uniswap.org/pairs#/pools
// DAI - stable coin
const PAIR_ADDRESS = "0xc2e9f25be6257c210d7adf0d4cd6e3e881ba25f8";
const PAIR_NAME = "DAI/ETH";

// v3 require pool with feeTier - get from factor
const daiAddress = "0x6b175474e89094c44da98b954eedeac495271d0f";
const wethAddress = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";

const runBot = async () => {

    let pairPool;       // pairPool contract
    const loadPairs = async () => {
        pairPool = new ethers.Contract(await UniswapV3Factory.getPool(daiAddress, wethAddress, 500), // <<<=== .getPool is not a function
            UniswapV3Pool.abi,
            wallet
        );
    }
    await loadPairs();

The issue is not being able to access contract functions, i.e. getPool:

pairPool = new ethers.Contract(await UniswapV3Factory.getPool(daiAddress, wethAddress, 500),

I swapped out the contract for the UniswapV3Pool and tried executing some functions – same issue.

Best Answer

Try switching the line to:

pairPool = new ethers.Contract(await uniswapV3Factory.getPool(daiAddress, wethAddress, 500),

uniswapV3Factory is the actual factory instance you want to query, but at the moment you're quering UniswapV3Factory, which is just the ABI.

Related Topic