Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pools): speed up cmd by only querying relevant zrc20 pools #143

Merged
merged 7 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@
"spinnies": "^0.5.1",
"tiny-secp256k1": "^2.2.3",
"ws": "^8.13.0"
}
}
},
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
}
89 changes: 62 additions & 27 deletions packages/client/src/getPools.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,88 @@
import UniswapV2Factory from "@uniswap/v2-core/build/UniswapV2Factory.json";
import UniswapV2Pair from "@uniswap/v2-core/build/UniswapV2Pair.json";
import { getAddress, ParamChainName } from "@zetachain/protocol-contracts";
import SystemContract from "@zetachain/protocol-contracts/abi/zevm/SystemContract.sol/SystemContract.json";
import { ethers } from "ethers";

import { ZetaChainClient } from "./client";

export const getPools = async function (this: ZetaChainClient) {
const rpc = this.getEndpoint("evm", `zeta_${this.network}`);
const provider = new ethers.providers.StaticJsonRpcProvider(rpc);

const uniswapV2FactoryAddress = getAddress(
"uniswapV2Factory",
`zeta_${this.network}` as ParamChainName
);
const zetaNetwork = `zeta_${this.network}` as ParamChainName;
const uniswapV2FactoryAddress = getAddress("uniswapV2Factory", zetaNetwork);

if (!uniswapV2FactoryAddress) {
throw new Error("uniswapV2Factory is not defined");
}

const UniswapV2FactoryContract = new ethers.Contract(
uniswapV2FactoryAddress,
UniswapV2Factory.abi,
const systemContractAddress = getAddress("systemContract", zetaNetwork);
if (!systemContractAddress) {
throw new Error("System contract is not defined");
}

const systemContract = new ethers.Contract(
systemContractAddress,
SystemContract.abi,
provider
);

const totalPairs = await UniswapV2FactoryContract.allPairsLength();
let pairs = [];
for (let i = 0; i < totalPairs; i++) {
pairs.push(await UniswapV2FactoryContract.allPairs(i));
const zetaTokenAddress = getAddress("zetaToken", zetaNetwork);
if (!zetaTokenAddress) {
throw new Error("ZETA token address is not defined");
}

const poolPromises = pairs.map(async (pair: any) => {
let pool = {
pair,
t0: {},
t1: {},
} as any;
const pairContract = new ethers.Contract(pair, UniswapV2Pair.abi, provider);
const foreignCoins = await this.getForeignCoins();
const tokenAddresses = foreignCoins.map(
(coin: any) => coin.zrc20_contract_address
);
tokenAddresses.push(zetaTokenAddress);

const poolPromises = [];

pool.t0.address = await pairContract.token0();
pool.t1.address = await pairContract.token1();
for (let i = 0; i < tokenAddresses.length; i++) {
for (let j = i + 1; j < tokenAddresses.length; j++) {
fadeev marked this conversation as resolved.
Show resolved Hide resolved
const tokenA = tokenAddresses[i];
const tokenB = tokenAddresses[j];

const reserves = await pairContract.getReserves();
pool.t0.reserve = reserves[0];
pool.t1.reserve = reserves[1];
const poolPromise = (async () => {
const pair = await systemContract.uniswapv2PairFor(
uniswapV2FactoryAddress,
tokenA,
tokenB
);

return pool;
});
if (pair === ethers.constants.AddressZero) {
return null;
}

try {
const pairContract = new ethers.Contract(
pair,
UniswapV2Pair.abi,
provider
);
const token0 = await pairContract.token0();
const token1 = await pairContract.token1();
fadeev marked this conversation as resolved.
Show resolved Hide resolved
const reserves = await pairContract.getReserves();

return {
pair,
t0: { address: token0, reserve: reserves[0] },
t1: { address: token1, reserve: reserves[1] },
};
} catch (error) {
return null;
}
})();

poolPromises.push(poolPromise);
}
}

const pools = (await Promise.all(poolPromises)).filter(
(pool) => pool !== null
);

const pools = await Promise.all(poolPromises);
return pools;
};
4 changes: 2 additions & 2 deletions packages/tasks/src/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {

const tableData = {} as any;
poolsWithSymbolsAndDecimals.forEach((pool: any) => {
const r0 = parseFloat(pool.t0.reserve).toFixed(2);
const r1 = parseFloat(pool.t1.reserve).toFixed(2);
const r0 = parseFloat(pool.t0.reserve);
const r1 = parseFloat(pool.t1.reserve);

tableData[pool.pair] = {
Pool: `${pool.t0.symbol} / ${pool.t1.symbol}`,
Expand Down
Loading