-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add page to list the refundable HTLCs
- Loading branch information
1 parent
0d41d71
commit 0dc6734
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { getHTLCs as getEVMHTLCs } from "../registry/evm-htlcs.js"; | ||
import { getHTLCs as getArchethicHtlcs } from "../registry/archethic-htlcs.js"; | ||
import { HTLC_STATUS } from "../archethic/get-htlc-statuses.js"; | ||
import config from "config"; | ||
|
||
const ARCHETHIC_ENDPOINT = config.get("archethic.endpoint"); | ||
const EVM_NETWORKS = config.get("evm"); | ||
|
||
export default function (db) { | ||
return async (req, res) => { | ||
const chargeableHTLCs = filterRefund(merge( | ||
await getArchethicHtlcs(db, "chargeable"), | ||
await getEVMHTLCs(db, "chargeable"), | ||
"chargeable", | ||
)); | ||
|
||
const signedHTLCs = filterRefund(merge( | ||
await getArchethicHtlcs(db, "signed"), | ||
await getEVMHTLCs(db, "signed"), | ||
"signed", | ||
)); | ||
|
||
const htlcs = [...chargeableHTLCs, ...signedHTLCs]; | ||
|
||
res.json(htlcs.map((htlc) => { | ||
if (htlc.type == "chargeable") { | ||
return { archethicAddress: htlc.address, claimingAmount: htlc.amount / 100_000_000 } | ||
} | ||
else { | ||
return { evmAddress: htlc.evmHtlc.address, chain: htlc.evmHtlc.chain, claimingAmount: htlc.evmHtlc.amount } | ||
} | ||
})); | ||
}; | ||
} | ||
|
||
function merge(archethicHtlcs, evmHtlcs, type) { | ||
// dump mumbai | ||
archethicHtlcs = archethicHtlcs.filter((htlc) => htlc.evmChainID != 80001); | ||
|
||
let evmHtlcsToDiscard = []; | ||
|
||
// match HTLCs (best effort) | ||
for (const archethicHtlc of archethicHtlcs) { | ||
archethicHtlc.type = type; | ||
if (archethicHtlc.evmContract) { | ||
const match = evmHtlcs.find( | ||
(evmHtlc) => | ||
evmHtlc.address.toLowerCase() == | ||
archethicHtlc.evmContract.toLowerCase(), | ||
); | ||
if (match != null) { | ||
archethicHtlc.evmHtlc = match; | ||
evmHtlcsToDiscard.push(archethicHtlc.evmHtlc.address); | ||
} | ||
} else { | ||
// Try to match based on the locktime (2s tolerance) | ||
const matches = evmHtlcs.filter( | ||
(evmHtlc) => Math.abs(evmHtlc.lockTime - archethicHtlc.endTime) < 2, | ||
); | ||
if (matches.length == 1) { | ||
archethicHtlc.evmHtlc = matches[0]; | ||
evmHtlcsToDiscard.push(archethicHtlc.evmHtlc.address); | ||
} | ||
} | ||
} | ||
|
||
// evm HTLCs with no match in archethic | ||
for (const evmHtlc of evmHtlcs) { | ||
if (evmHtlcsToDiscard.includes(evmHtlc.address)) continue; | ||
|
||
archethicHtlcs.push({ evmHtlc: evmHtlc }); | ||
} | ||
return archethicHtlcs; | ||
} | ||
|
||
function filterRefund(htlcs) { | ||
return htlcs.filter((htlc) => { | ||
if (!htlc.evmHtlc || !htlc.creationTime) { | ||
return false | ||
} | ||
|
||
return (htlc.evmHtlc.status == "REFUNDED" && htlc.status != 2) || (htlc.status == 2 && htlc.evmHtlc.status != "REFUNDED") | ||
}) | ||
} |