From 2a6b3346dcb2d86f05bf4b1870d0c343614a7c37 Mon Sep 17 00:00:00 2001 From: Aren Date: Wed, 18 Dec 2024 13:14:55 +0400 Subject: [PATCH] Refactor LpLock component to improve destination details handling and add error handling in light client interactions --- components/Swap/AtomicChat/Actions/LpLock.tsx | 45 ++++++++++--------- lib/lightClient/providers/evm/index.ts | 6 ++- pages/api/consensusRpc/[...slug].ts | 29 ++++++++++++ public/workers/helios/heliosWorker.js | 19 +++++--- 4 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 pages/api/consensusRpc/[...slug].ts diff --git a/components/Swap/AtomicChat/Actions/LpLock.tsx b/components/Swap/AtomicChat/Actions/LpLock.tsx index 28509a59f..efa5c380e 100644 --- a/components/Swap/AtomicChat/Actions/LpLock.tsx +++ b/components/Swap/AtomicChat/Actions/LpLock.tsx @@ -27,31 +27,34 @@ export const LpLockingAssets: FC = () => { }) const destinationDetails = await lightClient.getHashlock() - setDestinationDetails(destinationDetails) - } else { - let lockHandler: any = undefined - lockHandler = setInterval(async () => { - if (!network.chain_id) - throw Error("No chain id") + if (destinationDetails) { + setDestinationDetails(destinationDetails) + return + } + } + + let lockHandler: any = undefined + lockHandler = setInterval(async () => { + if (!network.chain_id) + throw Error("No chain id") - const destiantionDetails = await provider.getDetails({ - type: asset?.contract ? 'erc20' : 'native', - chainId: network.chain_id, - id: commitId, - contractAddress: atomicContract - }) + const destiantionDetails = await provider.getDetails({ + type: asset?.contract ? 'erc20' : 'native', + chainId: network.chain_id, + id: commitId, + contractAddress: atomicContract + }) - if (destiantionDetails?.hashlock) { - setDestinationDetails(destiantionDetails) - clearInterval(lockHandler) - } + if (destiantionDetails?.hashlock) { + setDestinationDetails(destiantionDetails) + clearInterval(lockHandler) + } - }, 5000) + }, 5000) - return () => { - lockHandler && clearInterval(lockHandler); - }; - } + return () => { + lockHandler && clearInterval(lockHandler); + }; } diff --git a/lib/lightClient/providers/evm/index.ts b/lib/lightClient/providers/evm/index.ts index 04971f7f7..ceaca0a0e 100644 --- a/lib/lightClient/providers/evm/index.ts +++ b/lib/lightClient/providers/evm/index.ts @@ -5,6 +5,7 @@ import EVM_PHTLC from '../../../abis/atomic/EVM_PHTLC.json' import { Commit } from "../../../../Models/PHTLC" import KnownInternalNames from "../../../knownIds" import { Network, Token } from "../../../../Models/Network" +import { hexToBigInt } from "viem" export default class EVMLightClient extends _LightClient { @@ -35,6 +36,7 @@ export default class EVMLightClient extends _LightClient { commitId: commitId, abi: token.contract ? EVMERC20_PHTLC : EVM_PHTLC, contractAddress: atomicContract, + hostname: window.location.origin, }, }, }, @@ -45,8 +47,8 @@ export default class EVMLightClient extends _LightClient { const result = event.data.data const parsedResult: Commit = { ...result, - secret: Number(result.secret) !== 1 ? result.secret : null, - amount: formatAmount(Number(result.amount), token.decimals), + secret: Number(hexToBigInt(result.secret._hex)) !== 1 ? result.secret : null, + amount: formatAmount(Number(hexToBigInt(result.amount._hex)), token.decimals), timelock: Number(result.timelock) } console.log('Worker event:', event) diff --git a/pages/api/consensusRpc/[...slug].ts b/pages/api/consensusRpc/[...slug].ts new file mode 100644 index 000000000..a95f8deed --- /dev/null +++ b/pages/api/consensusRpc/[...slug].ts @@ -0,0 +1,29 @@ +import axios from 'axios'; +import { NextApiRequest, NextApiResponse } from 'next' + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const slug = req.query.slug && (req.query.slug as string[]).join('/'); + + const queryParams = {} + for(const key in req.query) { + if(key !== 'slug') { + queryParams[key] = req.query[key] + } + } + + const searchParams = new URLSearchParams(queryParams); + + const rpcRes = await axios.get(`http://unstable.sepolia.beacon-api.nimbus.team/${slug}${searchParams ? `?${searchParams.toString()}` : ''}`) + + if (!rpcRes) { + res.status(400).json({ error: { message: "Failed" } }) + return + } else if (rpcRes) { + res.status(200).json(rpcRes.data) + return + } + + else { + res.status(500) + } +} \ No newline at end of file diff --git a/public/workers/helios/heliosWorker.js b/public/workers/helios/heliosWorker.js index 32be363ae..d652f8403 100644 --- a/public/workers/helios/heliosWorker.js +++ b/public/workers/helios/heliosWorker.js @@ -5,13 +5,14 @@ self.onmessage = (e) => { case 'init': const configEthereum = { executionRpc: "https://eth-sepolia.g.alchemy.com/v2/ErGCcrn6KRA91KfnRkqtyb3SJVdYGz1S", - consensusRpc: 'http://unstable.sepolia.beacon-api.nimbus.team', - checkpoint: "0x81f12a3e1ba2ce7559d61320705b44888a102ccaf8e590547440daad74a6512d", - dbType: "localstorage" + consensusRpc: e.data.payload.data.commitConfigs.hostname + '/api/consensusRpc', + checkpoint: "0x5d7fbedda647649b940f099fe79832dc0b031b08e5558ff7371bcce472471ab4", + dbType: "localstorage", + network: 'sepolia' }; const opstackConfigs = { - executionRpc: "https://opt-mainnet.g.alchemy.com/v2/a--NIcyeycPntQX42kunxUIVkg6_ekYc", - network: "op-mainnet", + executionRpc: "https://opt-sepolia.g.alchemy.com/v2/ErGCcrn6KRA91KfnRkqtyb3SJVdYGz1S", + network: "op-sepolia", }; const configs = e.data.payload.data.commitConfigs.network?.includes('optimism') ? opstackConfigs : configEthereum; getCommit(configs, e.data.payload.data.commitConfigs); @@ -41,8 +42,16 @@ async function getCommit(providerConfig, commitConfigs) { } let getDetailsHandler = undefined; (async () => { + let attempts = 0; getDetailsHandler = setInterval(async () => { try { + if (attempts > 20) { + clearInterval(getDetailsHandler); + self.postMessage({ type: 'commitDetails', data: null }); + return; + } + + attempts++; const data = await getCommitDetails(web3Provider); if (data?.hashlock && data?.hashlock !== "0x0100000000000000000000000000000000000000000000000000000000000000" && data?.hashlock !== "0x0000000000000000000000000000000000000000000000000000000000000000") { self.postMessage({ type: 'commitDetails', data: data });