Skip to content

Commit

Permalink
refactor: remove API endpoint based on pair
Browse files Browse the repository at this point in the history
We do not need that feature anymore and it adds a lot complexity
for every single API call that is being made
  • Loading branch information
michael1011 committed Aug 15, 2024
1 parent 9f5122a commit 255e511
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 155 deletions.
2 changes: 1 addition & 1 deletion src/components/CreateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export const CreateButton = () => {
}

if (msg === "invalid pair hash") {
setPairs(await getPairs(assetReceive()));
setPairs(await getPairs());
notify("error", t("feecheck"));
} else {
notify("error", msg);
Expand Down
3 changes: 0 additions & 3 deletions src/components/RefundButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const RefundEvm = ({
);
} else {
const { signature } = await getEipRefundSignature(
asset,
// The preimage hash can be used as an identifier
preimageHash,
// The endpoints for submarine and chain swap call the same endpoint
Expand Down Expand Up @@ -195,7 +194,6 @@ const RefundButton = ({
setRefundRunning(true);

const transactionToRefund = await getLockupTransaction(
swap().assetSend,
swap().id,
swap().type,
);
Expand Down Expand Up @@ -255,7 +253,6 @@ const RefundButton = ({
if (!swap()) return;

const transactionToRefund = await getLockupTransaction(
swap().assetSend,
swap().id,
swap().type,
);
Expand Down
62 changes: 16 additions & 46 deletions src/components/SwapChecker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OutputType } from "boltz-core";
import log from "loglevel";
import { createEffect, onCleanup, onMount } from "solid-js";

import { BTC, LBTC, RBTC } from "../consts/Assets";
import { RBTC } from "../consts/Assets";
import { SwapType } from "../consts/Enums";
import {
swapStatusFinal,
Expand Down Expand Up @@ -133,7 +133,7 @@ export const SwapChecker = () => {
const { notify, updateSwapStatus, getSwap, getSwaps, setSwapStorage, t } =
useGlobalContext();

const assetWebsocket = new Map<string, BoltzWebSocket>();
let ws: BoltzWebSocket | undefined = undefined;

const prepareSwap = async (swapId: string, data: any) => {
const currentSwap = await getSwap(swapId);
Expand Down Expand Up @@ -170,19 +170,13 @@ export const SwapChecker = () => {
}

if (data.status === swapStatusSuccess.InvoiceSettled) {
data.transaction = await getReverseTransaction(
getRelevantAssetForSwap(currentSwap),
currentSwap.id,
);
data.transaction = await getReverseTransaction(currentSwap.id);
} else if (
currentSwap.type === SwapType.Chain &&
data.status === swapStatusSuccess.TransactionClaimed
) {
data.transaction = (
await getChainSwapTransactions(
getRelevantAssetForSwap(currentSwap),
currentSwap.id,
)
await getChainSwapTransactions(currentSwap.id)
).serverLock.transaction;
}

Expand Down Expand Up @@ -246,14 +240,6 @@ export const SwapChecker = () => {
};

onMount(async () => {
const urlsToAsset = new Map<string, string[]>();
for (const [asset, url] of [BTC, LBTC, RBTC].map((asset) => [
asset,
getApiUrl(asset),
])) {
urlsToAsset.set(url, (urlsToAsset.get(url) || []).concat(asset));
}

const swapsToCheck = (await getSwaps()).filter(
(s) =>
!swapStatusFinal.includes(s.status) ||
Expand All @@ -263,32 +249,18 @@ export const SwapChecker = () => {
s.claimTx === undefined),
);

for (const [url, assets] of urlsToAsset.entries()) {
log.debug(`opening ws for assets [${assets.join(", ")}]: ${url}`);
const ws = new BoltzWebSocket(
url,
new Set<string>(
swapsToCheck
.filter((s) =>
assets.includes(getRelevantAssetForSwap(s)),
)
.map((s) => s.id),
),
prepareSwap,
claimSwap,
);
ws.connect();
for (const asset of assets) {
assetWebsocket.set(asset, ws);
}
}
log.debug(`Opening WebSocket: ${getApiUrl()}`);
ws = new BoltzWebSocket(
getApiUrl(),
new Set<string>(swapsToCheck.map((s) => s.id)),
prepareSwap,
claimSwap,
);
ws.connect();
});

onCleanup(() => {
const sockets = assetWebsocket.values();
assetWebsocket.clear();

for (const ws of sockets) {
if (ws !== undefined) {
ws.close();
}
});
Expand All @@ -298,12 +270,10 @@ export const SwapChecker = () => {
if (activeSwap === undefined || activeSwap === null) {
return;
}
// on page reload assetWebsocket is not yet initialized
const ws = assetWebsocket.get(getRelevantAssetForSwap(activeSwap));
if (ws === undefined) {
return;
// on page reload assetWebsocket might not be initialized yet
if (ws !== undefined) {
ws.subscribeUpdates([activeSwap.id]);
}
ws.subscribeUpdates([activeSwap.id]);
});

return "";
Expand Down
7 changes: 3 additions & 4 deletions src/context/Global.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from "solid-js";

import { config } from "../config";
import { BTC } from "../consts/Assets";
import { Denomination } from "../consts/Enums";
import { swapStatusFinal } from "../consts/SwapStatus";
import { detectLanguage } from "../i18n/detect";
Expand Down Expand Up @@ -76,7 +75,7 @@ export type GlobalContextType = {
audio?: boolean,
) => void;
playNotificationSound: () => void;
fetchPairs: (asset?: string) => void;
fetchPairs: () => void;

getLogs: () => Promise<Record<string, string[]>>;
clearLogs: () => Promise<void>;
Expand Down Expand Up @@ -192,8 +191,8 @@ const GlobalProvider = (props: { children: any }) => {
audio.play();
};

const fetchPairs = (asset: string = BTC) => {
getPairs(asset)
const fetchPairs = () => {
getPairs()
.then((data) => {
log.debug("getpairs", data);
setOnline(true);
Expand Down
2 changes: 1 addition & 1 deletion src/context/Web3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const Web3SignerProvider = (props: {
return undefined;
}

return (await getContracts(RBTC))["rsk"];
return (await getContracts())["rsk"];
});

const getEtherSwap = () => {
Expand Down
3 changes: 1 addition & 2 deletions src/pages/Hero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import bitcoin from "../assets/bitcoin-icon.svg";
import lightning from "../assets/lightning-icon.svg";
import liquid from "../assets/liquid-icon.svg";
import rbtc from "../assets/rootstock-icon.svg";
import { BTC } from "../consts/Assets";
import { Denomination } from "../consts/Enums";
import { useGlobalContext } from "../context/Global";
import Create from "../pages/Create";
Expand All @@ -33,7 +32,7 @@ export const Hero = () => {

onMount(async () => {
try {
const statsRes = await getNodeStats(BTC);
const statsRes = await getNodeStats();

log.debug("node stats", statsRes);
const stats = statsRes.BTC.total;
Expand Down
5 changes: 2 additions & 3 deletions src/pages/Pay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import TransactionConfirmed from "../status/TransactionConfirmed";
import TransactionLockupFailed from "../status/TransactionLockupFailed";
import TransactionMempool from "../status/TransactionMempool";
import { getSwapStatus } from "../utils/boltzClient";
import { getRelevantAssetForSwap } from "../utils/swapCreator";

const Pay = () => {
const params = useParams();
Expand All @@ -48,8 +47,8 @@ const Pay = () => {
if (currentSwap) {
log.debug("selecting swap", currentSwap);
setSwap(currentSwap);
const asset = getRelevantAssetForSwap(currentSwap);
const res = await getSwapStatus(asset, currentSwap.id);

const res = await getSwapStatus(currentSwap.id);
setSwapStatus(res.status);
setSwapStatusTransaction(res.transaction);
setFailureReason(res.failureReason);
Expand Down
18 changes: 10 additions & 8 deletions src/pages/Refund.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useNavigate } from "@solidjs/router";
import log from "loglevel";
import QrScanner from "qr-scanner";
import { Show, createEffect, createSignal, onMount } from "solid-js";
import { Show, createEffect, createSignal, onCleanup, onMount } from "solid-js";

import BlockExplorer from "../components/BlockExplorer";
import ConnectWallet from "../components/ConnectWallet";
Expand Down Expand Up @@ -105,6 +105,12 @@ const Refund = () => {

let refundScanAbort: AbortController | undefined = undefined;

onCleanup(() => {
if (refundScanAbort) {
refundScanAbort.abort();
}
});

createEffect(async () => {
setLogRefundableSwaps([]);

Expand Down Expand Up @@ -167,7 +173,7 @@ const Refund = () => {
)
.map(async (swap) => {
try {
const res = await getSwapStatus(swap.assetSend, swap.id);
const res = await getSwapStatus(swap.id);
if (
!(await updateSwapStatus(swap.id, res.status)) &&
Object.values(swapStatusFailed).includes(res.status)
Expand All @@ -177,12 +183,8 @@ const Refund = () => {
return;
}

// Make sure coins were locked for the swap with status "swap.expired"
await getLockupTransaction(
swap.assetSend,
swap.id,
swap.type,
);
// Make sure coins were locked for the swap with the status "swap.expired"
await getLockupTransaction(swap.id, swap.type);
addToRefundableSwaps(swap);
}
} catch (e) {
Expand Down
6 changes: 1 addition & 5 deletions src/status/SwapExpired.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ const SwapExpired = () => {
createEffect(async () => {
setTransactionToRefund(null);
try {
const res = await getLockupTransaction(
swap().assetSend,
swap().id,
swap().type,
);
const res = await getLockupTransaction(swap().id, swap().type);
log.debug(`got swap transaction for ${swap().id}`);
setTransactionToRefund(res.hex);
} catch (error: any) {
Expand Down
Loading

0 comments on commit 255e511

Please sign in to comment.