Skip to content

Commit

Permalink
refactor: fetcher not using global signals (#427)
Browse files Browse the repository at this point in the history
* refactor: fetcher no signal

dont use signal in fetcher, pass it instead

* use const

---------

Co-authored-by: michael1011 <me@michael1011.at>
  • Loading branch information
dni and michael1011 authored Jan 7, 2024
1 parent 161f463 commit ea57df7
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/components/CreateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export const CreateButton = () => {
await new Promise((resolve) => {
fetcher(
"/createswap",
assetName,
(data) => {
data.date = new Date().getTime();
data.reverse = reverse();
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Hero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
});
Expand Down
1 change: 1 addition & 0 deletions src/pages/Pay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const Pay = () => {
setSwap(currentSwap);
fetcher(
"/swapstatus",
currentSwap.asset,
(data: any) => {
setSwapStatus(data.status);
setSwapStatusTransaction(data.transaction);
Expand Down
3 changes: 3 additions & 0 deletions src/pages/Refund.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const Refund = () => {
const refundInfo = refundJson();
fetcher(
"/getswaptransaction",
refundInfo.asset,
async (data: any) => {
if (data.timeoutEta) {
setTimeoutEta(data.timeoutEta * 1000);
Expand Down Expand Up @@ -149,6 +150,7 @@ const Refund = () => {
.map((swap) => {
fetcher(
"/swapstatus",
swap.asset,
(status: any) => {
if (
!updateSwapStatus(swap.id, status.status) &&
Expand All @@ -166,6 +168,7 @@ const Refund = () => {
// Make sure coins were locked for the swap with status "swap.expired"
fetcher(
"/getswaptransaction",
swap.asset,
() => {
addToRefundableSwaps(swap);
},
Expand Down
1 change: 1 addition & 0 deletions src/status/SwapExpired.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const SwapExpired = () => {
setTransactionToRefund(null);
fetcher(
"/getswaptransaction",
swap().asset,
(res: any) => {
log.debug(`got swap transaction for ${swap().id}`);
setTransactionToRefund(res);
Expand Down
39 changes: 22 additions & 17 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import log from "loglevel";
import { pairs } from "../config";
import { BTC, RBTC } from "../consts";
import {
asset,
ref,
refundAddress,
setConfig,
Expand Down Expand Up @@ -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,
Expand All @@ -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);
};

Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -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) {
Expand All @@ -286,7 +288,7 @@ export async function refund(swap: any, t: any) {
}
},
{
currency: asset_name,
currency: assetName,
transactionHex: refundTransaction,
},
(error) => {
Expand Down Expand Up @@ -324,7 +326,7 @@ export async function refund(swap: any, t: any) {

export async function getfeeestimation(swap: any): Promise<number> {
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]);
Expand Down Expand Up @@ -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();
Expand All @@ -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");
Expand All @@ -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(
Expand All @@ -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) {
Expand All @@ -425,7 +428,7 @@ export const claim = async (swap: any) => {
}
},
{
currency: asset_name,
currency: assetName,
transactionHex: claimTransaction,
},
);
Expand All @@ -434,6 +437,7 @@ export const claim = async (swap: any) => {
export const fetchPairs = () => {
fetcher(
"/getpairs",
BTC,
(data: any) => {
log.debug("getpairs", data);
setOnline(true);
Expand All @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/swapChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const runSwapCheck = async () => {
await new Promise<void>((resolve) => {
fetcher(
"/swapstatus",
swap.asset,
(data) => {
setSwapStatusAndClaim(data, swap);
resolve();
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/swapChecker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
Expand Down

2 comments on commit ea57df7

@vercel
Copy link

@vercel vercel bot commented on ea57df7 Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on ea57df7 Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.