From 8ba02eb05c09c739ad0c4e9117d256ef4a95dd38 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Tue, 24 Oct 2023 01:01:32 +0800 Subject: [PATCH] fix: migrate cctx tracking to using isomorphic fetch (#77) --- helpers/index.ts | 2 ++ helpers/prepare.ts | 16 ++++++++++++++++ helpers/tx.ts | 29 +++++++++++++++++++---------- 3 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 helpers/prepare.ts diff --git a/helpers/index.ts b/helpers/index.ts index 4aeaafea..814a1416 100644 --- a/helpers/index.ts +++ b/helpers/index.ts @@ -1,4 +1,6 @@ export * from "./balances"; export * from "./fees"; export * from "./pools"; +export * from "./prepare"; +export * from "./sendZETA"; export * from "./tx"; diff --git a/helpers/prepare.ts b/helpers/prepare.ts new file mode 100644 index 00000000..5caecbd3 --- /dev/null +++ b/helpers/prepare.ts @@ -0,0 +1,16 @@ +import { ethers } from "ethers"; + +export const prepareData = (contract: string, types: string[], args: any[]) => { + const params = prepareParams(types, args); + return `${contract}${params.slice(2)}`; +}; + +export const prepareParams = (types: string[], args: any[]) => { + const abiCoder = ethers.utils.defaultAbiCoder; + for (let i = 0; i < args.length; i++) { + if (types[i] === "bytes32") { + args[i] = ethers.utils.hexlify(ethers.utils.zeroPad(args[i], 32)); + } + } + return abiCoder.encode(types, args); +}; diff --git a/helpers/tx.ts b/helpers/tx.ts index e1a68f21..f54872d0 100644 --- a/helpers/tx.ts +++ b/helpers/tx.ts @@ -1,9 +1,16 @@ 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 apiFetch = async (url: string) => { + const response = await fetch(url); + if (!response.ok) { + throw new Error(`Fetch failed with status: ${response.status}`); + } + return await response.json(); +}; + const getEndpoint = (key: any): string => { const endpoint = getEndpoints(key, "zeta_testnet")[0]?.url; if (!endpoint) { @@ -33,8 +40,8 @@ const fetchCCTXByInbound = async ( ) => { try { const url = `${API}/zeta-chain/crosschain/inTxHashToCctx/${hash}`; - const apiResponse = await axios.get(url); - const res = apiResponse?.data?.inTxHashToCctx?.cctx_index; + const apiResponseData = await apiFetch(url); + const res = apiResponseData?.inTxHashToCctx?.cctx_index; res.forEach((hash: any) => { if (hash && !cctxs[hash] && !spinners[hash]) { cctxs[hash] = []; @@ -119,16 +126,16 @@ const fetchCCTXData = async ( const getCCTX = async (hash: string, API: string) => { try { const url = `${API}/zeta-chain/crosschain/cctx/${hash}`; - const apiResponse = await axios.get(url); - return apiResponse?.data?.CrossChainTx; + const apiResponseData = await apiFetch(url); + return apiResponseData?.CrossChainTx; } catch (e) {} }; const fetchNonces = async (API: string, TSS: string) => { try { const url = `${API}/zeta-chain/crosschain/pendingNonces`; - const apiResponse = await axios.get(url); - const nonces = apiResponse?.data?.pending_nonces; + const apiResponseData = await apiFetch(url); + const nonces = apiResponseData?.pending_nonces; return nonces.filter((n: any) => n.tss === TSS); } catch (e) {} }; @@ -136,8 +143,8 @@ const fetchNonces = async (API: string, TSS: string) => { const fetchTSS = async (API: string) => { try { const url = `${API}/zeta-chain/crosschain/TSS`; - const apiResponse = await axios.get(url); - return apiResponse?.data?.TSS.tss_pubkey; + const apiResponseData = await apiFetch(url); + return apiResponseData?.TSS.tss_pubkey; } catch (e) {} }; @@ -155,7 +162,7 @@ export const trackCCTX = async ( let cctxs: any = {}; let pendingNonces: any = []; - setInterval(async () => { + const intervalID = setInterval(async () => { pendingNonces = await fetchNonces(API, TSS); if (Object.keys(cctxs).length === 0) { if (!json && emitter) { @@ -219,6 +226,8 @@ export const trackCCTX = async ( }) .every((s) => s === "OutboundMined"); + clearInterval(intervalID); + if (!allOutboundMined) { reject("CCTX aborted or reverted"); } else {