From 4f900f317d819b63722551077897bdec6892f026 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Thu, 5 Oct 2023 22:39:31 +0300 Subject: [PATCH 1/9] refactor: cctx tracking --- helpers/tx.ts | 114 ++++++++++++++++++++++++++++++++++---------------- package.json | 5 ++- tasks/cctx.ts | 4 +- yarn.lock | 5 +++ 4 files changed, 88 insertions(+), 40 deletions(-) diff --git a/helpers/tx.ts b/helpers/tx.ts index f07727a0..82e35ab3 100644 --- a/helpers/tx.ts +++ b/helpers/tx.ts @@ -4,6 +4,7 @@ import axios from "axios"; import clc from "cli-color"; import { ethers } from "ethers"; import Spinnies from "spinnies"; +import EventEmitter from "eventemitter3"; const getEndpoint = (key: any): string => { const endpoint = getEndpoints(key, "zeta_testnet")[0]?.url; @@ -26,7 +27,8 @@ const findByChainId = (config: any, targetChainId: Number): Object | null => { const fetchCCTXByInbound = async ( hash: string, - spinnies: any, + emitter: any, + spinners: any, API: string, cctxList: any, json: Boolean @@ -36,16 +38,16 @@ const fetchCCTXByInbound = async ( const apiResponse = await axios.get(url); const res = apiResponse?.data?.inTxHashToCctx?.cctx_index; res.forEach((cctxHash: any) => { - if ( - cctxHash && - !cctxList[cctxHash] && - !spinnies.spinners[`spinner-${cctxHash}`] - ) { + if (cctxHash && !cctxList[cctxHash] && !spinners[`spinner-${cctxHash}`]) { cctxList[cctxHash] = []; if (!json) { - spinnies.add(`spinner-${cctxHash}`, { - text: `${cctxHash}`, - }); + if (emitter) { + emitter.emit("add", { + hash: cctxHash, + text: `${cctxHash}`, + }); + } + spinners[`spinner-${cctxHash}`] = true; } } }); @@ -54,7 +56,8 @@ const fetchCCTXByInbound = async ( const fetchCCTXData = async ( cctxHash: string, - spinnies: any, + emitter: any, + spinners: any, API: string, cctxList: any, pendingNonces: any, @@ -94,7 +97,8 @@ const fetchCCTXData = async ( (n: any) => n.chain_id === tx.receiver_chainId )?.nonce_low; const current = tx.outbound_tx_tss_nonce; - queue = current > pending ? ` (${current - pending} in queue)` : ""; + const diff = current - pending; + queue = diff > 0 ? ` (${diff} in queue)` : ""; } const path = cctxList[cctxHash] .map( @@ -104,18 +108,18 @@ const fetchCCTXData = async ( }` ) .join(" → "); - const text = { - text: `${cctxHash}: ${sender} → ${receiver}${queue}: ${path}`, - }; - const id = `spinner-${cctxHash}`; - if (!json && spinnies.spinners[id]) { + const text = `${cctxHash}: ${sender} → ${receiver}${queue}: ${path}`; + + if (!json && spinners[`spinner-${cctxHash}`]) { const s = tx.status; if (s == "OutboundMined" || s == "Reverted") { - spinnies.succeed(id, text); + if (emitter) emitter.emit("succeed", { hash: cctxHash, text }); + spinners[`spinner-${cctxHash}`] = false; } else if (s == "Aborted") { - spinnies.fail(id, text); + if (emitter) emitter.emit("fail", { hash: cctxHash, text }); + spinners[`spinner-${cctxHash}`] = false; } else { - spinnies.update(id, text); + if (emitter) emitter.emit("update", { hash: cctxHash, text }); } } } @@ -146,11 +150,30 @@ const fetchTSS = async (API: string) => { } catch (e) {} }; -export const trackCCTX = async ( +export const trackCCTXInteractive = async ( hash: string, json: Boolean = false +) => { + const s = new Spinnies(); + const emitter = new EventEmitter(); + + const searchAddText = `Looking for cross-chain transactions (CCTXs) on ZetaChain...\n`; + emitter + .on("search-add", () => s.add(`search`, { text: searchAddText })) + .on("search-end", ({ text }) => s.succeed(`search`, { text })) + .on("add", ({ hash, text }) => s.add(hash, { text })) + .on("succeed", ({ hash, text }) => s.succeed(hash, { text })) + .on("fail", ({ hash, text }) => s.fail(hash, { text })) + .on("update", ({ hash, text }) => s.update(hash, { text })); + await trackCCTX(hash, json, emitter); +}; + +export const trackCCTX = async ( + hash: string, + json: Boolean = false, + emitter: any = null ): Promise => { - const spinnies = new Spinnies(); + const spinners: any = {}; const API = getEndpoint("cosmos-http"); const TSS = await fetchTSS(API); @@ -162,37 +185,56 @@ export const trackCCTX = async ( const loopInterval = setInterval(async () => { pendingNonces = await fetchNonces(API, TSS); if (Object.keys(cctxList).length === 0) { - if (!json) { - spinnies.add(`search`, { - text: `Looking for cross-chain transactions (CCTXs) on ZetaChain...\n`, - }); + if (!json && emitter) { + if (emitter) { + emitter.emit("search-add", { + text: `Looking for cross-chain transactions (CCTXs) on ZetaChain...\n`, + }); + } + spinners["search"] = true; } - await fetchCCTXByInbound(hash, spinnies, API, cctxList, json); + await fetchCCTXByInbound(hash, emitter, spinners, API, cctxList, json); } if (Object.keys(cctxList).length === 0 && !cctxList[hash]) { if ((await getCCTX(hash, API)) && !cctxList[hash]) { cctxList[hash] = []; - if (!spinnies.spinners[`spinner-${hash}`] && !json) { - spinnies.add(`spinner-${hash}`, { - text: `${hash}`, - }); + if (!spinners[`spinner-${hash}`] && !json) { + spinners[`spinner-${hash}`] = true; + if (emitter) { + emitter.emit("add", { + hash: hash, + text: `${hash}`, + }); + spinners[`spinner-${hash}`] = true; + } } } } for (const txHash in cctxList) { - await fetchCCTXByInbound(txHash, spinnies, API, cctxList, json); + await fetchCCTXByInbound( + txHash, + emitter, + spinners, + API, + cctxList, + json + ); } if (Object.keys(cctxList).length > 0) { - if (spinnies.spinners["search"] && !json) { - spinnies.succeed(`search`, { - text: `CCTXs on ZetaChain found.\n`, - }); + if (spinners["search"] && !json) { + if (emitter) { + emitter.emit("search-end", { + text: `CCTXs on ZetaChain found.\n`, + }); + } + spinners["search"] = false; } for (const cctxHash in cctxList) { try { fetchCCTXData( cctxHash, - spinnies, + emitter, + spinners, API, cctxList, pendingNonces, diff --git a/package.json b/package.json index 1da1b53d..62584775 100644 --- a/package.json +++ b/package.json @@ -80,11 +80,12 @@ "bech32": "^2.0.0", "bip39": "^3.1.0", "bitcoinjs-lib": "^6.1.3", - "dotenv": "16.0.3", "cli-color": "^2.0.3", + "dotenv": "16.0.3", "ecpair": "^2.1.0", "envfile": "^6.18.0", "ethers": "5.4.7", + "eventemitter3": "^5.0.1", "form-data": "^4.0.0", "handlebars": "4.7.7", "hardhat": "^2.15.0", @@ -94,4 +95,4 @@ "tiny-secp256k1": "^2.2.3", "ws": "^8.13.0" } -} \ No newline at end of file +} diff --git a/tasks/cctx.ts b/tasks/cctx.ts index 7195c4c0..313eeeac 100644 --- a/tasks/cctx.ts +++ b/tasks/cctx.ts @@ -1,12 +1,12 @@ import { task } from "hardhat/config"; import { HardhatRuntimeEnvironment } from "hardhat/types"; -import { trackCCTX } from "../helpers"; +import { trackCCTXInteractive } from "../helpers"; declare const hre: any; const main = async (args: any, hre: HardhatRuntimeEnvironment) => { - await trackCCTX(args.tx); + await trackCCTXInteractive(args.tx, args.json); }; export const cctxTask = task( diff --git a/yarn.lock b/yarn.lock index 774c0d69..7a02723e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3897,6 +3897,11 @@ event-emitter@^0.3.5: d "1" es5-ext "~0.10.14" +eventemitter3@^5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-5.0.1.tgz#53f5ffd0a492ac800721bb42c66b841de96423c4" + integrity sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA== + evp_bytestokey@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz#7fcbdb198dc71959432efe13842684e0525acb02" From 76769086be785c7871c209b7c5914b2d2a9adb3c Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Thu, 5 Oct 2023 23:04:03 +0300 Subject: [PATCH 2/9] refactor --- helpers/tx.ts | 46 +++++-------------- package.json | 3 +- tasks/cctx.ts | 20 +++++++-- yarn.lock | 119 -------------------------------------------------- 4 files changed, 29 insertions(+), 159 deletions(-) diff --git a/helpers/tx.ts b/helpers/tx.ts index 82e35ab3..95b7fda9 100644 --- a/helpers/tx.ts +++ b/helpers/tx.ts @@ -1,10 +1,6 @@ -import { getEndpoints } from "@zetachain/networks"; -import { getHardhatConfigNetworks } from "@zetachain/networks"; +import { getEndpoints, getHardhatConfigNetworks } from "@zetachain/networks"; import axios from "axios"; -import clc from "cli-color"; import { ethers } from "ethers"; -import Spinnies from "spinnies"; -import EventEmitter from "eventemitter3"; const getEndpoint = (key: any): string => { const endpoint = getEndpoints(key, "zeta_testnet")[0]?.url; @@ -38,7 +34,7 @@ const fetchCCTXByInbound = async ( const apiResponse = await axios.get(url); const res = apiResponse?.data?.inTxHashToCctx?.cctx_index; res.forEach((cctxHash: any) => { - if (cctxHash && !cctxList[cctxHash] && !spinners[`spinner-${cctxHash}`]) { + if (cctxHash && !cctxList[cctxHash] && !spinners[cctxHash]) { cctxList[cctxHash] = []; if (!json) { if (emitter) { @@ -47,7 +43,7 @@ const fetchCCTXByInbound = async ( text: `${cctxHash}`, }); } - spinners[`spinner-${cctxHash}`] = true; + spinners[cctxHash] = true; } } }); @@ -103,21 +99,19 @@ const fetchCCTXData = async ( const path = cctxList[cctxHash] .map( (x: any) => - `${clc.bold.underline(x.status)} ${ - x.status_message && "(" + x.status_message + ")" - }` + `${x.status} ${x.status_message && "(" + x.status_message + ")"}` ) .join(" → "); const text = `${cctxHash}: ${sender} → ${receiver}${queue}: ${path}`; - if (!json && spinners[`spinner-${cctxHash}`]) { + if (!json && spinners[cctxHash]) { const s = tx.status; if (s == "OutboundMined" || s == "Reverted") { if (emitter) emitter.emit("succeed", { hash: cctxHash, text }); - spinners[`spinner-${cctxHash}`] = false; + spinners[cctxHash] = false; } else if (s == "Aborted") { if (emitter) emitter.emit("fail", { hash: cctxHash, text }); - spinners[`spinner-${cctxHash}`] = false; + spinners[cctxHash] = false; } else { if (emitter) emitter.emit("update", { hash: cctxHash, text }); } @@ -150,24 +144,6 @@ const fetchTSS = async (API: string) => { } catch (e) {} }; -export const trackCCTXInteractive = async ( - hash: string, - json: Boolean = false -) => { - const s = new Spinnies(); - const emitter = new EventEmitter(); - - const searchAddText = `Looking for cross-chain transactions (CCTXs) on ZetaChain...\n`; - emitter - .on("search-add", () => s.add(`search`, { text: searchAddText })) - .on("search-end", ({ text }) => s.succeed(`search`, { text })) - .on("add", ({ hash, text }) => s.add(hash, { text })) - .on("succeed", ({ hash, text }) => s.succeed(hash, { text })) - .on("fail", ({ hash, text }) => s.fail(hash, { text })) - .on("update", ({ hash, text }) => s.update(hash, { text })); - await trackCCTX(hash, json, emitter); -}; - export const trackCCTX = async ( hash: string, json: Boolean = false, @@ -182,7 +158,7 @@ export const trackCCTX = async ( let cctxList: any = {}; let pendingNonces: any = []; - const loopInterval = setInterval(async () => { + setInterval(async () => { pendingNonces = await fetchNonces(API, TSS); if (Object.keys(cctxList).length === 0) { if (!json && emitter) { @@ -198,14 +174,14 @@ export const trackCCTX = async ( if (Object.keys(cctxList).length === 0 && !cctxList[hash]) { if ((await getCCTX(hash, API)) && !cctxList[hash]) { cctxList[hash] = []; - if (!spinners[`spinner-${hash}`] && !json) { - spinners[`spinner-${hash}`] = true; + if (!spinners[hash] && !json) { + spinners[hash] = true; if (emitter) { emitter.emit("add", { hash: hash, text: `${hash}`, }); - spinners[`spinner-${hash}`] = true; + spinners[hash] = true; } } } diff --git a/package.json b/package.json index 62584775..15a5e3f8 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,6 @@ "bech32": "^2.0.0", "bip39": "^3.1.0", "bitcoinjs-lib": "^6.1.3", - "cli-color": "^2.0.3", "dotenv": "16.0.3", "ecpair": "^2.1.0", "envfile": "^6.18.0", @@ -95,4 +94,4 @@ "tiny-secp256k1": "^2.2.3", "ws": "^8.13.0" } -} +} \ No newline at end of file diff --git a/tasks/cctx.ts b/tasks/cctx.ts index 313eeeac..cba8d708 100644 --- a/tasks/cctx.ts +++ b/tasks/cctx.ts @@ -1,11 +1,25 @@ import { task } from "hardhat/config"; import { HardhatRuntimeEnvironment } from "hardhat/types"; -import { trackCCTXInteractive } from "../helpers"; +import Spinnies from "spinnies"; +import EventEmitter from "eventemitter3"; -declare const hre: any; +import { trackCCTX } from "../helpers"; -const main = async (args: any, hre: HardhatRuntimeEnvironment) => { +const trackCCTXInteractive = async (hash: string, json: Boolean = false) => { + const s = new Spinnies(); + const emitter = new EventEmitter(); + emitter + .on("search-add", ({ text }) => s.add(`search`, { text })) + .on("search-end", ({ text }) => s.succeed(`search`, { text })) + .on("add", ({ hash, text }) => s.add(hash, { text })) + .on("succeed", ({ hash, text }) => s.succeed(hash, { text })) + .on("fail", ({ hash, text }) => s.fail(hash, { text })) + .on("update", ({ hash, text }) => s.update(hash, { text })); + await trackCCTX(hash, json, emitter); +}; + +const main = async (args: any) => { await trackCCTXInteractive(args.tx, args.json); }; diff --git a/yarn.lock b/yarn.lock index 7a02723e..dc3dab5c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2757,17 +2757,6 @@ clean-stack@^2.0.0: resolved "https://registry.yarnpkg.com/clean-stack/-/clean-stack-2.2.0.tgz#ee8472dbb129e727b31e8a10a427dee9dfe4008b" integrity sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A== -cli-color@^2.0.3: - version "2.0.3" - resolved "https://registry.yarnpkg.com/cli-color/-/cli-color-2.0.3.tgz#73769ba969080629670f3f2ef69a4bf4e7cc1879" - integrity sha512-OkoZnxyC4ERN3zLzZaY9Emb7f/MhBOIpePv0Ycok0fJYT+Ouo00UBEIwsVsr0yoow++n5YWlSUgST9GKhNHiRQ== - dependencies: - d "^1.0.1" - es5-ext "^0.10.61" - es6-iterator "^2.0.3" - memoizee "^0.4.15" - timers-ext "^0.1.7" - cli-cursor@^3.0.0, cli-cursor@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-3.1.0.tgz#264305a7ae490d1d03bf0c9ba7c925d1753af307" @@ -3023,14 +3012,6 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.2: resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b" integrity sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow== -d@1, d@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/d/-/d-1.0.1.tgz#8698095372d58dbee346ffd0c7093f99f8f9eb5a" - integrity sha512-m62ShEObQ39CfralilEQRjH6oAMtNCV1xJyEx5LpRYUVN+EviphDgUc/F3hnYbADmkiNs67Y+3ylmlG7Lnu+FA== - dependencies: - es5-ext "^0.10.50" - type "^1.0.1" - dashdash@^1.12.0: version "1.14.1" resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0" @@ -3398,42 +3379,6 @@ es-to-primitive@^1.2.1: is-date-object "^1.0.1" is-symbol "^1.0.2" -es5-ext@^0.10.35, es5-ext@^0.10.46, es5-ext@^0.10.50, es5-ext@^0.10.53, es5-ext@^0.10.61, es5-ext@~0.10.14, es5-ext@~0.10.2, es5-ext@~0.10.46: - version "0.10.62" - resolved "https://registry.yarnpkg.com/es5-ext/-/es5-ext-0.10.62.tgz#5e6adc19a6da524bf3d1e02bbc8960e5eb49a9a5" - integrity sha512-BHLqn0klhEpnOKSrzn/Xsz2UIW8j+cGmo9JLzr8BiUapV8hPL9+FliFqjwr9ngW7jWdnxv6eO+/LqyhJVqgrjA== - dependencies: - es6-iterator "^2.0.3" - es6-symbol "^3.1.3" - next-tick "^1.1.0" - -es6-iterator@^2.0.3: - version "2.0.3" - resolved "https://registry.yarnpkg.com/es6-iterator/-/es6-iterator-2.0.3.tgz#a7de889141a05a94b0854403b2d0a0fbfa98f3b7" - integrity sha512-zw4SRzoUkd+cl+ZoE15A9o1oQd920Bb0iOJMQkQhl3jNc03YqVjAhG7scf9C5KWRU/R13Orf588uCC6525o02g== - dependencies: - d "1" - es5-ext "^0.10.35" - es6-symbol "^3.1.1" - -es6-symbol@^3.1.1, es6-symbol@^3.1.3: - version "3.1.3" - resolved "https://registry.yarnpkg.com/es6-symbol/-/es6-symbol-3.1.3.tgz#bad5d3c1bcdac28269f4cb331e431c78ac705d18" - integrity sha512-NJ6Yn3FuDinBaBRWl/q5X/s4koRHBrgKAu+yGI6JCBeiu3qrcbJhwT2GeR/EXVfylRk8dpQVJoLEFhK+Mu31NA== - dependencies: - d "^1.0.1" - ext "^1.1.2" - -es6-weak-map@^2.0.3: - version "2.0.3" - resolved "https://registry.yarnpkg.com/es6-weak-map/-/es6-weak-map-2.0.3.tgz#b6da1f16cc2cc0d9be43e6bdbfc5e7dfcdf31d53" - integrity sha512-p5um32HOTO1kP+w7PRnB+5lQ43Z6muuMuIMffvDN8ZB4GcnjLBV6zGStpbASIMk4DCAvEaamhe2zhyCb/QXXsA== - dependencies: - d "1" - es5-ext "^0.10.46" - es6-iterator "^2.0.3" - es6-symbol "^3.1.1" - escalade@^3.1.1: version "3.1.1" resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40" @@ -3889,14 +3834,6 @@ ethjs-util@0.1.6, ethjs-util@^0.1.6: is-hex-prefixed "1.0.0" strip-hex-prefix "1.0.0" -event-emitter@^0.3.5: - version "0.3.5" - resolved "https://registry.yarnpkg.com/event-emitter/-/event-emitter-0.3.5.tgz#df8c69eef1647923c7157b9ce83840610b02cc39" - integrity sha512-D9rRn9y7kLPnJ+hMq7S/nhvoKwwvVJahBi2BPmx3bvbsEdK3W9ii8cBSGjP+72/LnM4n6fo3+dkCX5FeTQruXA== - dependencies: - d "1" - es5-ext "~0.10.14" - eventemitter3@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-5.0.1.tgz#53f5ffd0a492ac800721bb42c66b841de96423c4" @@ -3937,13 +3874,6 @@ expand-range@^1.8.1: dependencies: fill-range "^2.1.0" -ext@^1.1.2: - version "1.7.0" - resolved "https://registry.yarnpkg.com/ext/-/ext-1.7.0.tgz#0ea4383c0103d60e70be99e9a7f11027a33c4f5f" - integrity sha512-6hxeJYaL110a9b5TEJSj0gojyHQAmA2ch5Os+ySCiA1QGdS697XWY1pzsrSjqA9LDEEgdB/KypIlR59RcLuHYw== - dependencies: - type "^2.7.2" - extend-shallow@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f" @@ -5228,11 +5158,6 @@ is-primitive@^2.0.0: resolved "https://registry.yarnpkg.com/is-primitive/-/is-primitive-2.0.0.tgz#207bab91638499c07b2adf240a41a87210034575" integrity sha512-N3w1tFaRfk3UrPfqeRyD+GYDASU3W5VinKhlORy8EWVf/sIdDL9GAcew85XmktCfH+ngG7SRXEVDoO18WMdB/Q== -is-promise@^2.2.2: - version "2.2.2" - resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.2.2.tgz#39ab959ccbf9a774cf079f7b40c7a26f763135f1" - integrity sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ== - is-regex@^1.1.4: version "1.1.4" resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.1.4.tgz#eef5663cd59fa4c0ae339505323df6854bb15958" @@ -5678,13 +5603,6 @@ lru-cache@^6.0.0: resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.0.1.tgz#0a3be479df549cca0e5d693ac402ff19537a6b7a" integrity sha512-IJ4uwUTi2qCccrioU6g9g/5rvvVl13bsdczUUcqbciD9iLr095yj8DQKdObriEvuNSx325N1rV1O0sJFszx75g== -lru-queue@^0.1.0: - version "0.1.0" - resolved "https://registry.yarnpkg.com/lru-queue/-/lru-queue-0.1.0.tgz#2738bd9f0d3cf4f84490c5736c48699ac632cda3" - integrity sha512-BpdYkt9EvGl8OfWHDQPISVpcl5xZthb+XPsbELj5AQXxIC8IriDZIQYjBJPEm5rS420sjZ0TLEzRcq5KdBhYrQ== - dependencies: - es5-ext "~0.10.2" - lru_map@^0.3.3: version "0.3.3" resolved "https://registry.yarnpkg.com/lru_map/-/lru_map-0.3.3.tgz#b5c8351b9464cbd750335a79650a0ec0e56118dd" @@ -5731,20 +5649,6 @@ md5.js@^1.3.4: inherits "^2.0.1" safe-buffer "^5.1.2" -memoizee@^0.4.15: - version "0.4.15" - resolved "https://registry.yarnpkg.com/memoizee/-/memoizee-0.4.15.tgz#e6f3d2da863f318d02225391829a6c5956555b72" - integrity sha512-UBWmJpLZd5STPm7PMUlOw/TSy972M+z8gcyQ5veOnSDRREz/0bmpyTfKt3/51DhEBqCZQn1udM/5flcSPYhkdQ== - dependencies: - d "^1.0.1" - es5-ext "^0.10.53" - es6-weak-map "^2.0.3" - event-emitter "^0.3.5" - is-promise "^2.2.2" - lru-queue "^0.1.0" - next-tick "^1.1.0" - timers-ext "^0.1.7" - memory-level@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/memory-level/-/memory-level-1.0.0.tgz#7323c3fd368f9af2f71c3cd76ba403a17ac41692" @@ -6100,11 +6004,6 @@ neo-async@^2.6.0, neo-async@^2.6.2: resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f" integrity sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw== -next-tick@1, next-tick@^1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.1.0.tgz#1836ee30ad56d67ef281b22bd199f709449b35eb" - integrity sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ== - nise@^5.1.4: version "5.1.4" resolved "https://registry.yarnpkg.com/nise/-/nise-5.1.4.tgz#491ce7e7307d4ec546f5a659b2efe94a18b4bbc0" @@ -7677,14 +7576,6 @@ then-request@^6.0.0: promise "^8.0.0" qs "^6.4.0" -timers-ext@^0.1.7: - version "0.1.7" - resolved "https://registry.yarnpkg.com/timers-ext/-/timers-ext-0.1.7.tgz#6f57ad8578e07a3fb9f91d9387d65647555e25c6" - integrity sha512-b85NUNzTSdodShTIbky6ZF02e8STtVVfD+fu4aXXShEELpozH+bCpJLYMPZbsABN2wDH7fJpqIoXxJpzbf0NqQ== - dependencies: - es5-ext "~0.10.46" - next-tick "1" - tiny-secp256k1@^2.2.3: version "2.2.3" resolved "https://registry.yarnpkg.com/tiny-secp256k1/-/tiny-secp256k1-2.2.3.tgz#fe1dde11a64fcee2091157d4b78bcb300feb9b65" @@ -7861,16 +7752,6 @@ type-fest@^0.7.1: resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.7.1.tgz#8dda65feaf03ed78f0a3f9678f1869147f7c5c48" integrity sha512-Ne2YiiGN8bmrmJJEuTWTLJR32nh/JdL1+PSicowtNb0WFpn59GK8/lfD61bVtzguz7b3PBt74nxpv/Pw5po5Rg== -type@^1.0.1: - version "1.2.0" - resolved "https://registry.yarnpkg.com/type/-/type-1.2.0.tgz#848dd7698dafa3e54a6c479e759c4bc3f18847a0" - integrity sha512-+5nt5AAniqsCnu2cEQQdpzCAh33kVx8n0VoFidKpB1dVVLAN/F+bgVOqOJqOnEnrhp222clB5p3vUlD+1QAnfg== - -type@^2.7.2: - version "2.7.2" - resolved "https://registry.yarnpkg.com/type/-/type-2.7.2.tgz#2376a15a3a28b1efa0f5350dcf72d24df6ef98d0" - integrity sha512-dzlvlNlt6AXU7EBSfpAscydQ7gXB+pPGsPnfJnZpiNJBDj7IaJzQlBZYGdEi4R9HmPdBv2XmWJ6YUtoTa7lmCw== - typechain@^8.1.0: version "8.3.1" resolved "https://registry.yarnpkg.com/typechain/-/typechain-8.3.1.tgz#dccbc839b94877997536c356380eff7325395cfb" From aba9bf28fe8ec48142f84691090ffc0dd62a9657 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Thu, 5 Oct 2023 23:04:29 +0300 Subject: [PATCH 3/9] refactor --- helpers/tx.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/helpers/tx.ts b/helpers/tx.ts index 95b7fda9..3835682a 100644 --- a/helpers/tx.ts +++ b/helpers/tx.ts @@ -33,17 +33,17 @@ const fetchCCTXByInbound = async ( const url = `${API}/zeta-chain/crosschain/inTxHashToCctx/${hash}`; const apiResponse = await axios.get(url); const res = apiResponse?.data?.inTxHashToCctx?.cctx_index; - res.forEach((cctxHash: any) => { - if (cctxHash && !cctxList[cctxHash] && !spinners[cctxHash]) { - cctxList[cctxHash] = []; + res.forEach((hash: any) => { + if (hash && !cctxList[hash] && !spinners[hash]) { + cctxList[hash] = []; if (!json) { if (emitter) { emitter.emit("add", { - hash: cctxHash, - text: `${cctxHash}`, + hash, + text: hash, }); } - spinners[cctxHash] = true; + spinners[hash] = true; } } }); From ba2332eca2f4f543af73392e1e56d51d1ebbcfd7 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Thu, 5 Oct 2023 23:06:32 +0300 Subject: [PATCH 4/9] refactor --- helpers/tx.ts | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/helpers/tx.ts b/helpers/tx.ts index 3835682a..4fa83f3b 100644 --- a/helpers/tx.ts +++ b/helpers/tx.ts @@ -38,10 +38,7 @@ const fetchCCTXByInbound = async ( cctxList[hash] = []; if (!json) { if (emitter) { - emitter.emit("add", { - hash, - text: hash, - }); + emitter.emit("add", { hash, text: hash }); } spinners[hash] = true; } @@ -51,7 +48,7 @@ const fetchCCTXByInbound = async ( }; const fetchCCTXData = async ( - cctxHash: string, + hash: string, emitter: any, spinners: any, API: string, @@ -60,7 +57,7 @@ const fetchCCTXData = async ( json: Boolean ) => { const networks = getHardhatConfigNetworks(); - const cctx = await getCCTX(cctxHash, API); + const cctx = await getCCTX(hash, API); const receiver_chainId = cctx?.outbound_tx_params[0]?.receiver_chainId; const outbound_tx_hash = cctx?.outbound_tx_params[0]?.outbound_tx_hash; let confirmed_on_destination = false; @@ -79,14 +76,14 @@ const fetchCCTXData = async ( status: cctx?.cctx_status?.status, status_message: cctx?.cctx_status?.status_message, }; - const lastCCTX = cctxList[cctxHash][cctxList[cctxHash].length - 1]; - const isEmpty = cctxList[cctxHash].length === 0; + const lastCCTX = cctxList[hash][cctxList[hash].length - 1]; + const isEmpty = cctxList[hash].length === 0; const statusDefined = tx.status !== undefined && tx.status_message !== undefined; if (isEmpty || (statusDefined && lastCCTX.status !== tx.status)) { - cctxList[cctxHash].push(tx); - const sender = cctxList[cctxHash]?.[0].sender_chain_id; - const receiver = cctxList[cctxHash]?.[0].receiver_chainId; + cctxList[hash].push(tx); + const sender = cctxList[hash]?.[0].sender_chain_id; + const receiver = cctxList[hash]?.[0].receiver_chainId; let queue; if (pendingNonces) { const pending = pendingNonces.find( @@ -96,24 +93,24 @@ const fetchCCTXData = async ( const diff = current - pending; queue = diff > 0 ? ` (${diff} in queue)` : ""; } - const path = cctxList[cctxHash] + const path = cctxList[hash] .map( (x: any) => `${x.status} ${x.status_message && "(" + x.status_message + ")"}` ) .join(" → "); - const text = `${cctxHash}: ${sender} → ${receiver}${queue}: ${path}`; + const text = `${hash}: ${sender} → ${receiver}${queue}: ${path}`; - if (!json && spinners[cctxHash]) { + if (!json && spinners[hash]) { const s = tx.status; if (s == "OutboundMined" || s == "Reverted") { - if (emitter) emitter.emit("succeed", { hash: cctxHash, text }); - spinners[cctxHash] = false; + if (emitter) emitter.emit("succeed", { hash, text }); + spinners[hash] = false; } else if (s == "Aborted") { - if (emitter) emitter.emit("fail", { hash: cctxHash, text }); - spinners[cctxHash] = false; + if (emitter) emitter.emit("fail", { hash, text }); + spinners[hash] = false; } else { - if (emitter) emitter.emit("update", { hash: cctxHash, text }); + if (emitter) emitter.emit("update", { hash, text }); } } } @@ -205,10 +202,10 @@ export const trackCCTX = async ( } spinners["search"] = false; } - for (const cctxHash in cctxList) { + for (const hash in cctxList) { try { fetchCCTXData( - cctxHash, + hash, emitter, spinners, API, From 48e3fad0d47de0b1c925b4945cffdc4c384181b6 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Thu, 5 Oct 2023 23:12:02 +0300 Subject: [PATCH 5/9] refactor --- helpers/tx.ts | 56 ++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/helpers/tx.ts b/helpers/tx.ts index 4fa83f3b..7e2b0d9c 100644 --- a/helpers/tx.ts +++ b/helpers/tx.ts @@ -36,10 +36,8 @@ const fetchCCTXByInbound = async ( res.forEach((hash: any) => { if (hash && !cctxList[hash] && !spinners[hash]) { cctxList[hash] = []; - if (!json) { - if (emitter) { - emitter.emit("add", { hash, text: hash }); - } + if (!json && emitter) { + emitter.emit("add", { hash, text: hash }); spinners[hash] = true; } } @@ -101,16 +99,16 @@ const fetchCCTXData = async ( .join(" → "); const text = `${hash}: ${sender} → ${receiver}${queue}: ${path}`; - if (!json && spinners[hash]) { + if (!json && spinners[hash] && emitter) { const s = tx.status; if (s == "OutboundMined" || s == "Reverted") { - if (emitter) emitter.emit("succeed", { hash, text }); + emitter.emit("succeed", { hash, text }); spinners[hash] = false; } else if (s == "Aborted") { - if (emitter) emitter.emit("fail", { hash, text }); + emitter.emit("fail", { hash, text }); spinners[hash] = false; } else { - if (emitter) emitter.emit("update", { hash, text }); + emitter.emit("update", { hash, text }); } } } @@ -159,28 +157,24 @@ export const trackCCTX = async ( pendingNonces = await fetchNonces(API, TSS); if (Object.keys(cctxList).length === 0) { if (!json && emitter) { - if (emitter) { - emitter.emit("search-add", { - text: `Looking for cross-chain transactions (CCTXs) on ZetaChain...\n`, - }); - } + 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); } - if (Object.keys(cctxList).length === 0 && !cctxList[hash]) { - if ((await getCCTX(hash, API)) && !cctxList[hash]) { - cctxList[hash] = []; - if (!spinners[hash] && !json) { - spinners[hash] = true; - if (emitter) { - emitter.emit("add", { - hash: hash, - text: `${hash}`, - }); - spinners[hash] = true; - } - } + if ( + Object.keys(cctxList).length === 0 && + !cctxList[hash] && + (await getCCTX(hash, API)) && + !cctxList[hash] + ) { + cctxList[hash] = []; + if (!spinners[hash] && !json && emitter) { + spinners[hash] = true; + emitter.emit("add", { hash, text: hash }); + spinners[hash] = true; } } for (const txHash in cctxList) { @@ -194,12 +188,10 @@ export const trackCCTX = async ( ); } if (Object.keys(cctxList).length > 0) { - if (spinners["search"] && !json) { - if (emitter) { - emitter.emit("search-end", { - text: `CCTXs on ZetaChain found.\n`, - }); - } + if (spinners["search"] && !json && emitter) { + emitter.emit("search-end", { + text: `CCTXs on ZetaChain found.\n`, + }); spinners["search"] = false; } for (const hash in cctxList) { From 59d5e0f4177f79c60fbb96c34c0dd79af9275ba5 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Thu, 5 Oct 2023 23:16:41 +0300 Subject: [PATCH 6/9] refactor --- helpers/tx.ts | 63 +++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/helpers/tx.ts b/helpers/tx.ts index 7e2b0d9c..1ceb08d6 100644 --- a/helpers/tx.ts +++ b/helpers/tx.ts @@ -26,7 +26,7 @@ const fetchCCTXByInbound = async ( emitter: any, spinners: any, API: string, - cctxList: any, + cctxs: any, json: Boolean ) => { try { @@ -34,8 +34,8 @@ const fetchCCTXByInbound = async ( 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; @@ -50,7 +50,7 @@ const fetchCCTXData = async ( emitter: any, spinners: any, API: string, - cctxList: any, + cctxs: any, pendingNonces: any, json: Boolean ) => { @@ -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( @@ -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 + ")"}` @@ -150,58 +150,51 @@ 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 ); @@ -209,18 +202,18 @@ export const trackCCTX = async ( } } 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"); @@ -228,7 +221,7 @@ export const trackCCTX = async ( 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(); } } From 40bb5ce09b7d4f0a9db12a69a0d7ec9a6a8e6f11 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Thu, 5 Oct 2023 23:20:08 +0300 Subject: [PATCH 7/9] lint --- tasks/cctx.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tasks/cctx.ts b/tasks/cctx.ts index cba8d708..e7986775 100644 --- a/tasks/cctx.ts +++ b/tasks/cctx.ts @@ -1,8 +1,7 @@ +import EventEmitter from "eventemitter3"; import { task } from "hardhat/config"; import { HardhatRuntimeEnvironment } from "hardhat/types"; - import Spinnies from "spinnies"; -import EventEmitter from "eventemitter3"; import { trackCCTX } from "../helpers"; From 55f3f829e4886fadf78f00296235016da950ba51 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Thu, 5 Oct 2023 23:20:17 +0300 Subject: [PATCH 8/9] refactor --- tasks/cctx.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tasks/cctx.ts b/tasks/cctx.ts index e7986775..daeb0487 100644 --- a/tasks/cctx.ts +++ b/tasks/cctx.ts @@ -1,6 +1,5 @@ import EventEmitter from "eventemitter3"; import { task } from "hardhat/config"; -import { HardhatRuntimeEnvironment } from "hardhat/types"; import Spinnies from "spinnies"; import { trackCCTX } from "../helpers"; From 75c390097b8a840088bb50ecab929add378fca2b Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Thu, 5 Oct 2023 23:24:24 +0300 Subject: [PATCH 9/9] refactor --- helpers/tx.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/helpers/tx.ts b/helpers/tx.ts index 1ceb08d6..52d03484 100644 --- a/helpers/tx.ts +++ b/helpers/tx.ts @@ -157,9 +157,8 @@ export const trackCCTX = async ( pendingNonces = await fetchNonces(API, TSS); if (Object.keys(cctxs).length === 0) { if (!json && emitter) { - emitter.emit("search-add", { - text: `Looking for cross-chain transactions (CCTXs) on ZetaChain...\n`, - }); + const text = `Looking for cross-chain transactions (CCTXs) on ZetaChain...\n`; + emitter.emit("search-add", { text }); spinners["search"] = true; } await fetchCCTXByInbound(hash, emitter, spinners, API, cctxs, json);