From ea57df716312ff875bcf68e51c38fd8ef8a45c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Mon, 8 Jan 2024 00:54:48 +0100 Subject: [PATCH] refactor: fetcher not using global signals (#427) * refactor: fetcher no signal dont use signal in fetcher, pass it instead * use const --------- Co-authored-by: michael1011 --- src/components/CreateButton.tsx | 1 + src/pages/Hero.tsx | 3 ++- src/pages/Pay.tsx | 1 + src/pages/Refund.tsx | 3 +++ src/status/SwapExpired.tsx | 1 + src/utils/helper.ts | 39 +++++++++++++++++++-------------- src/utils/swapChecker.ts | 1 + tests/utils/swapChecker.spec.ts | 2 +- 8 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/components/CreateButton.tsx b/src/components/CreateButton.tsx index 70f82083..dafff972 100644 --- a/src/components/CreateButton.tsx +++ b/src/components/CreateButton.tsx @@ -144,6 +144,7 @@ export const CreateButton = () => { await new Promise((resolve) => { fetcher( "/createswap", + assetName, (data) => { data.date = new Date().getTime(); data.reverse = reverse(); diff --git a/src/pages/Hero.tsx b/src/pages/Hero.tsx index a4ae97ce..f7ddeffb 100644 --- a/src/pages/Hero.tsx +++ b/src/pages/Hero.tsx @@ -6,6 +6,7 @@ import bitcoin from "../assets/bitcoin-icon.svg"; import lightning from "../assets/lightning-icon.svg"; import liquid from "../assets/liquid-icon.svg"; import { ambossUrl } from "../config"; +import { BTC } from "../consts"; import t from "../i18n"; import Create from "../pages/Create"; import "../style/hero.scss"; @@ -36,7 +37,7 @@ export const Hero = () => { window.open(ambossUrl, "_blank"); }; - fetcher("/nodestats", (data: any) => { + fetcher("/nodestats", BTC, (data: any) => { log.debug("nodestats", data); setNodeStats(data.nodes.BTC); }); diff --git a/src/pages/Pay.tsx b/src/pages/Pay.tsx index 84d3befd..62e6756c 100644 --- a/src/pages/Pay.tsx +++ b/src/pages/Pay.tsx @@ -48,6 +48,7 @@ const Pay = () => { setSwap(currentSwap); fetcher( "/swapstatus", + currentSwap.asset, (data: any) => { setSwapStatus(data.status); setSwapStatusTransaction(data.transaction); diff --git a/src/pages/Refund.tsx b/src/pages/Refund.tsx index 4d22b1c5..8136dde2 100644 --- a/src/pages/Refund.tsx +++ b/src/pages/Refund.tsx @@ -100,6 +100,7 @@ const Refund = () => { const refundInfo = refundJson(); fetcher( "/getswaptransaction", + refundInfo.asset, async (data: any) => { if (data.timeoutEta) { setTimeoutEta(data.timeoutEta * 1000); @@ -149,6 +150,7 @@ const Refund = () => { .map((swap) => { fetcher( "/swapstatus", + swap.asset, (status: any) => { if ( !updateSwapStatus(swap.id, status.status) && @@ -166,6 +168,7 @@ const Refund = () => { // Make sure coins were locked for the swap with status "swap.expired" fetcher( "/getswaptransaction", + swap.asset, () => { addToRefundableSwaps(swap); }, diff --git a/src/status/SwapExpired.tsx b/src/status/SwapExpired.tsx index 3e424ed6..ac379489 100644 --- a/src/status/SwapExpired.tsx +++ b/src/status/SwapExpired.tsx @@ -20,6 +20,7 @@ const SwapExpired = () => { setTransactionToRefund(null); fetcher( "/getswaptransaction", + swap().asset, (res: any) => { log.debug(`got swap transaction for ${swap().id}`); setTransactionToRefund(res); diff --git a/src/utils/helper.ts b/src/utils/helper.ts index b2460f0e..226b2e09 100644 --- a/src/utils/helper.ts +++ b/src/utils/helper.ts @@ -9,7 +9,6 @@ import log from "loglevel"; import { pairs } from "../config"; import { BTC, RBTC } from "../consts"; import { - asset, ref, refundAddress, setConfig, @@ -115,6 +114,7 @@ export const getApiUrl = (asset: string) => { export const fetcher = ( url: string, + asset: string = BTC, cb: (value: any) => void, params: any | undefined = null, errorCb = errorHandler, @@ -130,7 +130,7 @@ export const fetcher = ( body: JSON.stringify(params), }; } - const apiUrl = getApiUrl(asset()) + url; + const apiUrl = getApiUrl(asset) + url; fetch(apiUrl, opts).then(checkResponse).then(cb).catch(errorCb); }; @@ -143,6 +143,7 @@ export const checkForFailed = (swap: any, data: any) => { fetcher( "/getswaptransaction", + swap.asset, (data: any) => { if (swap.asset !== RBTC && !data.transactionHex) { log.error("no mempool tx found"); @@ -192,11 +193,11 @@ export async function refund(swap: any, t: any) { log.debug("starting to refund swap", swap); setRefundTx(""); - const asset_name = swap.asset; + const assetName = swap.asset; let output: DecodedAddress; try { - output = decodeAddress(asset_name, refundAddress()); + output = decodeAddress(assetName, refundAddress()); } catch (e) { log.error(e); setNotificationType("error"); @@ -212,6 +213,7 @@ export async function refund(swap: any, t: any) { txToRefund = await new Promise((resolve, reject) => { fetcher( "/getswaptransaction", + swap.asset, (res: any) => { log.debug(`got swap transaction for ${swap.id}`); resolve(res); @@ -227,11 +229,10 @@ export async function refund(swap: any, t: any) { }); } - const Transaction = getTransaction(asset_name); - const constructRefundTransaction = - getConstructRefundTransaction(asset_name); - const net = getNetwork(asset_name); - const assetHash = asset_name === "L-BTC" ? net.assetHash : undefined; + const Transaction = getTransaction(assetName); + const constructRefundTransaction = getConstructRefundTransaction(assetName); + const net = getNetwork(assetName); + const assetHash = assetName === "L-BTC" ? net.assetHash : undefined; let tx = Transaction.fromHex(txToRefund.transactionHex); let script = Buffer.from(swap.redeemScript, "hex"); @@ -263,6 +264,7 @@ export async function refund(swap: any, t: any) { log.debug("refund_tx", refundTransaction); fetcher( "/broadcasttransaction", + assetName, (data: any) => { log.debug("refund result:", data); if (data.transactionId) { @@ -286,7 +288,7 @@ export async function refund(swap: any, t: any) { } }, { - currency: asset_name, + currency: assetName, transactionHex: refundTransaction, }, (error) => { @@ -324,7 +326,7 @@ export async function refund(swap: any, t: any) { export async function getfeeestimation(swap: any): Promise { return new Promise((resolve) => { - fetcher("/getfeeestimation", (data: any) => { + fetcher("/getfeeestimation", swap.asset, (data: any) => { log.debug("getfeeestimation: ", data); let asset = swap.asset; resolve(data[asset]); @@ -366,7 +368,7 @@ export const claim = async (swap: any) => { } await setup(); - const asset_name = swap.asset; + const assetName = swap.asset; log.info("claiming swap: ", swap.id); let mempool_tx = swapStatusTransaction(); @@ -378,9 +380,9 @@ export const claim = async (swap: any) => { } log.debug("mempool_tx", mempool_tx.hex); - const Transaction = getTransaction(asset_name); - const net = getNetwork(asset_name); - const assetHash = asset_name === "L-BTC" ? net.assetHash : undefined; + const Transaction = getTransaction(assetName); + const net = getNetwork(assetName); + const assetHash = assetName === "L-BTC" ? net.assetHash : undefined; let tx = Transaction.fromHex(mempool_tx.hex); let redeemScript = Buffer.from(swap.redeemScript, "hex"); @@ -393,7 +395,7 @@ export const claim = async (swap: any) => { let preimage = Buffer.from(swap.preimage, "hex"); log.debug("preimage: ", preimage); const { script, blindingKey } = decodeAddress( - asset_name, + assetName, swap.onchainAddress, ); const claimTransaction = createAdjustedClaim( @@ -415,6 +417,7 @@ export const claim = async (swap: any) => { log.debug("claim_tx", claimTransaction); fetcher( "/broadcasttransaction", + assetName, (data: any) => { log.debug("claim result:", data); if (data.transactionId) { @@ -425,7 +428,7 @@ export const claim = async (swap: any) => { } }, { - currency: asset_name, + currency: assetName, transactionHex: claimTransaction, }, ); @@ -434,6 +437,7 @@ export const claim = async (swap: any) => { export const fetchPairs = () => { fetcher( "/getpairs", + BTC, (data: any) => { log.debug("getpairs", data); setOnline(true); @@ -452,6 +456,7 @@ export const feeCheck = async (notification: any) => { return new Promise((resolve) => { fetcher( "/getpairs", + BTC, (data: any) => { log.debug("getpairs", data); if (feeChecker(data.pairs)) { diff --git a/src/utils/swapChecker.ts b/src/utils/swapChecker.ts index db87c9f0..4716120a 100644 --- a/src/utils/swapChecker.ts +++ b/src/utils/swapChecker.ts @@ -78,6 +78,7 @@ const runSwapCheck = async () => { await new Promise((resolve) => { fetcher( "/swapstatus", + swap.asset, (data) => { setSwapStatusAndClaim(data, swap); resolve(); diff --git a/tests/utils/swapChecker.spec.ts b/tests/utils/swapChecker.spec.ts index 10739e07..86fb77ba 100644 --- a/tests/utils/swapChecker.spec.ts +++ b/tests/utils/swapChecker.spec.ts @@ -23,7 +23,7 @@ vi.mock("../../src/utils/helper", async () => { return { isMobile: () => false, getApiUrl: () => apiUrl, - fetcher: (_path, cb, data) => { + fetcher: (_path, _asset, cb, data) => { fetcherCallData.push(data); cb(); },