Skip to content

Commit

Permalink
fix: migrate cctx tracking to using isomorphic fetch (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Oct 23, 2023
1 parent bad7fe3 commit 8ba02eb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 2 additions & 0 deletions helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from "./balances";
export * from "./fees";
export * from "./pools";
export * from "./prepare";
export * from "./sendZETA";
export * from "./tx";
16 changes: 16 additions & 0 deletions helpers/prepare.ts
Original file line number Diff line number Diff line change
@@ -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);
};
29 changes: 19 additions & 10 deletions helpers/tx.ts
Original file line number Diff line number Diff line change
@@ -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";

Check failure on line 4 in helpers/tx.ts

View workflow job for this annotation

GitHub Actions / build

Could not find a declaration file for module 'isomorphic-fetch'. '/home/runner/work/toolkit/toolkit/node_modules/isomorphic-fetch/fetch-npm-node.js' implicitly has an 'any' type.

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) {
Expand Down Expand Up @@ -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] = [];
Expand Down Expand Up @@ -119,25 +126,25 @@ 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) {}
};

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) {}
};

Expand All @@ -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) {
Expand Down Expand Up @@ -219,6 +226,8 @@ export const trackCCTX = async (
})
.every((s) => s === "OutboundMined");

clearInterval(intervalID);

if (!allOutboundMined) {
reject("CCTX aborted or reverted");
} else {
Expand Down

0 comments on commit 8ba02eb

Please sign in to comment.