Skip to content

Commit

Permalink
Refactor LpLock component to improve destination details handling and…
Browse files Browse the repository at this point in the history
… add error handling in light client interactions
  • Loading branch information
arentant committed Dec 18, 2024
1 parent a2eb611 commit 2a6b334
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
45 changes: 24 additions & 21 deletions components/Swap/AtomicChat/Actions/LpLock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

}

Expand Down
6 changes: 4 additions & 2 deletions lib/lightClient/providers/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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,
},
},
},
Expand All @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions pages/api/consensusRpc/[...slug].ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
19 changes: 14 additions & 5 deletions public/workers/helios/heliosWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit 2a6b334

Please sign in to comment.