Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Oct 5, 2023
1 parent 48e3fad commit 59d5e0f
Showing 1 changed file with 28 additions and 35 deletions.
63 changes: 28 additions & 35 deletions helpers/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ const fetchCCTXByInbound = async (
emitter: any,
spinners: any,
API: string,
cctxList: any,
cctxs: any,
json: Boolean
) => {
try {
const url = `${API}/zeta-chain/crosschain/inTxHashToCctx/${hash}`;
const apiResponse = await axios.get(url);
const res = apiResponse?.data?.inTxHashToCctx?.cctx_index;
res.forEach((hash: any) => {
if (hash && !cctxList[hash] && !spinners[hash]) {
cctxList[hash] = [];
if (hash && !cctxs[hash] && !spinners[hash]) {
cctxs[hash] = [];
if (!json && emitter) {
emitter.emit("add", { hash, text: hash });
spinners[hash] = true;
Expand All @@ -50,7 +50,7 @@ const fetchCCTXData = async (
emitter: any,
spinners: any,
API: string,
cctxList: any,
cctxs: any,
pendingNonces: any,
json: Boolean
) => {
Expand All @@ -74,14 +74,14 @@ const fetchCCTXData = async (
status: cctx?.cctx_status?.status,
status_message: cctx?.cctx_status?.status_message,
};
const lastCCTX = cctxList[hash][cctxList[hash].length - 1];
const isEmpty = cctxList[hash].length === 0;
const lastCCTX = cctxs[hash][cctxs[hash].length - 1];
const isEmpty = cctxs[hash].length === 0;
const statusDefined =
tx.status !== undefined && tx.status_message !== undefined;
if (isEmpty || (statusDefined && lastCCTX.status !== tx.status)) {
cctxList[hash].push(tx);
const sender = cctxList[hash]?.[0].sender_chain_id;
const receiver = cctxList[hash]?.[0].receiver_chainId;
cctxs[hash].push(tx);
const sender = cctxs[hash]?.[0].sender_chain_id;
const receiver = cctxs[hash]?.[0].receiver_chainId;
let queue;
if (pendingNonces) {
const pending = pendingNonces.find(
Expand All @@ -91,7 +91,7 @@ const fetchCCTXData = async (
const diff = current - pending;
queue = diff > 0 ? ` (${diff} in queue)` : "";
}
const path = cctxList[hash]
const path = cctxs[hash]
.map(
(x: any) =>
`${x.status} ${x.status_message && "(" + x.status_message + ")"}`
Expand Down Expand Up @@ -150,85 +150,78 @@ export const trackCCTX = async (
const TSS = await fetchTSS(API);

return new Promise((resolve, reject) => {
let cctxList: any = {};
let cctxs: any = {};
let pendingNonces: any = [];

setInterval(async () => {
pendingNonces = await fetchNonces(API, TSS);
if (Object.keys(cctxList).length === 0) {
if (Object.keys(cctxs).length === 0) {
if (!json && emitter) {
emitter.emit("search-add", {
text: `Looking for cross-chain transactions (CCTXs) on ZetaChain...\n`,
});
spinners["search"] = true;
}
await fetchCCTXByInbound(hash, emitter, spinners, API, cctxList, json);
await fetchCCTXByInbound(hash, emitter, spinners, API, cctxs, json);
}
if (
Object.keys(cctxList).length === 0 &&
!cctxList[hash] &&
Object.keys(cctxs).length === 0 &&
!cctxs[hash] &&
(await getCCTX(hash, API)) &&
!cctxList[hash]
!cctxs[hash]
) {
cctxList[hash] = [];
cctxs[hash] = [];
if (!spinners[hash] && !json && emitter) {
spinners[hash] = true;
emitter.emit("add", { hash, text: hash });
spinners[hash] = true;
}
}
for (const txHash in cctxList) {
await fetchCCTXByInbound(
txHash,
emitter,
spinners,
API,
cctxList,
json
);
for (const txHash in cctxs) {
await fetchCCTXByInbound(txHash, emitter, spinners, API, cctxs, json);
}
if (Object.keys(cctxList).length > 0) {
if (Object.keys(cctxs).length > 0) {
if (spinners["search"] && !json && emitter) {
emitter.emit("search-end", {
text: `CCTXs on ZetaChain found.\n`,
});
spinners["search"] = false;
}
for (const hash in cctxList) {
for (const hash in cctxs) {
try {
fetchCCTXData(
hash,
emitter,
spinners,
API,
cctxList,
cctxs,
pendingNonces,
json
);
} catch (error) {}
}
}
if (
Object.keys(cctxList).length > 0 &&
Object.keys(cctxList)
Object.keys(cctxs).length > 0 &&
Object.keys(cctxs)
.map((c: any) => {
const last = cctxList[c][cctxList[c].length - 1];
const last = cctxs[c][cctxs[c].length - 1];
return last?.status;
})
.filter((s) => !["OutboundMined", "Aborted", "Reverted"].includes(s))
.length === 0
) {
const allOutboundMined = Object.keys(cctxList)
const allOutboundMined = Object.keys(cctxs)
.map((c: any) => {
const last = cctxList[c][cctxList[c].length - 1];
const last = cctxs[c][cctxs[c].length - 1];
return last?.status;
})
.every((s) => s === "OutboundMined");

if (!allOutboundMined) {
reject("CCTX aborted or reverted");
} else {
if (json) console.log(JSON.stringify(cctxList, null, 2));
if (json) console.log(JSON.stringify(cctxs, null, 2));
resolve();
}
}
Expand Down

0 comments on commit 59d5e0f

Please sign in to comment.