From ebd599cbe5b48c70324174fb1422d0743bb9e036 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Mon, 16 Oct 2023 14:38:28 +0400 Subject: [PATCH] getPools --- helpers/index.ts | 1 + helpers/pools.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ helpers/tx.ts | 1 + tasks/index.ts | 1 + tasks/pools.ts | 19 +++++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 helpers/pools.ts create mode 100644 tasks/pools.ts diff --git a/helpers/index.ts b/helpers/index.ts index 6d92fad4..49ca5918 100644 --- a/helpers/index.ts +++ b/helpers/index.ts @@ -1,3 +1,4 @@ export * from "./balances"; export * from "./fees"; export * from "./tx"; +export * from "./pools"; diff --git a/helpers/pools.ts b/helpers/pools.ts new file mode 100644 index 00000000..a98bb1d2 --- /dev/null +++ b/helpers/pools.ts @@ -0,0 +1,54 @@ +import UniswapV2Factory from "@uniswap/v2-core/build/UniswapV2Factory.json"; +import UniswapV2Pair from "@uniswap/v2-core/build/UniswapV2Pair.json"; +import { getAddress } from "@zetachain/protocol-contracts"; +import { ethers } from "ethers"; +import { getEndpoints } from "@zetachain/networks/dist/src/getEndpoints"; +import fetch from "isomorphic-fetch"; + +export const getPools = async () => { + const api = getEndpoints("cosmos-http", "zeta_testnet")[0]?.url; + const endpoint = `${api}/zeta-chain/zetacore/fungible/foreign_coins`; + const response = await fetch(endpoint); + const data = await response.json(); + + const rpc = getEndpoints("evm", "zeta_testnet")[0]?.url; + const provider = new ethers.providers.JsonRpcProvider(rpc); + + const uniswapV2FactoryAddress = getAddress( + "uniswapv2Factory", + "zeta_testnet" + ); + const zetaTokenAddress = getAddress("zetaToken", "zeta_testnet"); + + const UniswapV2FactoryContract = new ethers.Contract( + uniswapV2FactoryAddress, + UniswapV2Factory.abi, + provider + ); + + const poolPromises = data.foreignCoins.map(async (token: any) => { + const zrc20Address = token.zrc20_contract_address; + const pair = await UniswapV2FactoryContract.getPair( + zrc20Address, + zetaTokenAddress + ); + + let reservesZRC20 = "0"; + let reservesZETA = "0"; + + if (pair !== ethers.constants.AddressZero) { + const uniswapPairContract = new ethers.Contract( + pair, + UniswapV2Pair.abi, + provider + ); + const reserves = await uniswapPairContract.getReserves(); + reservesZRC20 = ethers.utils.formatEther(reserves[0]); + reservesZETA = ethers.utils.formatEther(reserves[1]); + } + return { ...token, reservesZRC20, reservesZETA }; + }); + + const pools = await Promise.all(poolPromises); + return pools; +}; diff --git a/helpers/tx.ts b/helpers/tx.ts index 5b04f88b..c68b8a9e 100644 --- a/helpers/tx.ts +++ b/helpers/tx.ts @@ -2,6 +2,7 @@ import { getEndpoints } from "@zetachain/networks/dist/src/getEndpoints"; import networks from "@zetachain/networks/dist/src/networks"; import axios from "axios"; import { ethers } from "ethers"; +import fetch from "isomorphic-fetch"; const getEndpoint = (key: any): string => { const endpoint = getEndpoints(key, "zeta_testnet")[0]?.url; diff --git a/tasks/index.ts b/tasks/index.ts index 09cec504..1211c985 100644 --- a/tasks/index.ts +++ b/tasks/index.ts @@ -9,3 +9,4 @@ export { sendBTCTask } from "./sendBTC"; export { sendZETATask } from "./sendZETA"; export { sendZRC20Task } from "./sendZRC20"; export { verifyTask } from "./verify"; +export { poolsTask } from "./pools"; diff --git a/tasks/pools.ts b/tasks/pools.ts new file mode 100644 index 00000000..3bfb6686 --- /dev/null +++ b/tasks/pools.ts @@ -0,0 +1,19 @@ +import { task } from "hardhat/config"; +import { HardhatRuntimeEnvironment } from "hardhat/types"; +import { getPools } from "../helpers/pools"; + +const main = async (args: any, hre: HardhatRuntimeEnvironment) => { + const pools = await getPools(); + const poolsFiltered = pools + .map((n: any) => ({ + name: n.asset ? n.name : n.symbol, + "ZRC-20": parseFloat(n.reservesZRC20).toFixed(2), + ZETA: parseFloat(n.reservesZETA).toFixed(2), + })) + .sort((a: any, b: any) => { + if (a.name > b.name) return -1; + }); + console.table(poolsFiltered); +}; + +export const poolsTask = task("pools", "", main);