Javascript
為什麼我無法訪問 Uniswap v3 Factory .getPool() 函式?
作為個人練習,我正在嘗試將以下 Uniswap v2 項目轉換為 Uniswap v3:
Uniswap 代幣價格監控(這不是交換),即 ETH/DAI <https://www.youtube.com/watch?v=j_wVh_Sa1rU> <https://github.com/RudreshVeerkhare/UniswapPriceMonitor>
我專注於嘗試訂閱觸發的 WebSocket 事件,然後池價格被更新,並且已經達到需要獲取 DAI/WETH(500 費用等級)對的工廠池地址的地步。
我相信該功能可用,如下所示:
程式碼設置:
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();
問題是無法訪問合約功能,即 getPool:
pairPool = new ethers.Contract(await UniswapV3Factory.getPool(daiAddress, wethAddress, 500),
我換掉了契約
UniswapV3Pool
並嘗試執行一些功能 - 同樣的問題。
嘗試將線路切換到:
pairPool = new ethers.Contract(await uniswapV3Factory.getPool(daiAddress, wethAddress, 500),
uniswapV3Factory
是您要查詢的實際工廠實例,但此時您正在查詢UniswapV3Factory
,這只是 ABI。