Skip to content

Commit

Permalink
Update NIM HTLC timestamp field to milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Nov 22, 2024
1 parent 5a4a10b commit 776a6af
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@linusborg/vue-simple-portal": "^0.1.4",
"@nimiq/core": "^2.0.1",
"@nimiq/electrum-client": "https://github.com/nimiq/electrum-client#build",
"@nimiq/fastspot-api": "^1.10.2",
"@nimiq/fastspot-api": "^1.10.3",
"@nimiq/hub-api": "^1.10.0",
"@nimiq/iqons": "^1.5.2",
"@nimiq/libswap": "^1.5.1",
Expand Down
27 changes: 18 additions & 9 deletions src/components/modals/TransactionModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -607,15 +607,24 @@ export default defineComponent({
const showRefundButton = computed(() => !isIncoming.value
&& (
// funded but not redeemed htlc which is now expired
(swapInfo.value?.in?.asset === SwapAsset.NIM
&& (swapInfo.value.in.htlc?.timeoutBlockHeight || Number.POSITIVE_INFINITY) <= blockHeight.value
&& !swapInfo.value.out)
// funded but not redeemed htlc proxy (no actual htlc had been created)
|| (isSwapProxy.value
&& !relatedTx.value
&& transaction.value.state === TransactionState.CONFIRMED
&& transaction.value.blockHeight! <= blockHeight.value - 15) // consider proxy "expired" after 15 blocks
( // funded but not redeemed htlc which is now expired
swapInfo.value?.in?.asset === SwapAsset.NIM
&& swapInfo.value.in.htlc && (
('timeoutMs' in swapInfo.value.in.htlc && swapInfo.value.in.htlc.timeoutMs <= Date.now())
|| (
'timeoutBlockHeight' in swapInfo.value.in.htlc
// @ts-expect-error PoW field
&& swapInfo.value.in.htlc.timeoutBlockHeight <= blockHeight.value
)
)
&& !swapInfo.value.out
) || ( // funded but not redeemed htlc proxy (no actual htlc had been created)
isSwapProxy.value
&& !relatedTx.value
&& transaction.value.state === TransactionState.CONFIRMED
// consider proxy "expired" after 15 blocks
&& transaction.value.blockHeight! <= blockHeight.value - 15
)
)
// Only display the refund button for Ledger accounts as the Keyguard signs automatic refund transaction.
// Note that we only check the active account here to save us scanning through all our accounts as typically
Expand Down
19 changes: 11 additions & 8 deletions src/components/swap/SwapNotification.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ export default defineComponent({
const timestamp = await getServerTime(true);
const swap = activeSwap.value!;
/**
* Timestamps in seconds
*/
const remainingTimes: number[] = [];
// When we haven't funded our HTLC yet, we need to abort when the quote expires.
Expand All @@ -144,27 +147,27 @@ export default defineComponent({
// Otherwise, the swap expires when the first HTLC expires
for (const contract of Object.values(swap.contracts)) {
let timeout: number;
switch (contract!.asset) {
case SwapAsset.NIM: {
const height = useNetworkStore().height.value;
const { timeoutBlock } = (contract as Contract<SwapAsset.NIM>).htlc;
if (timeoutBlock <= height) return true;
remainingTimes.push((timeoutBlock - height) * 60);
const { timeoutTime } = (contract as Contract<SwapAsset.NIM>).htlc;
timeout = Math.floor(timeoutTime / 1e3);
break;
}
case SwapAsset.BTC:
case SwapAsset.USDC_MATIC:
case SwapAsset.USDT_MATIC:
case SwapAsset.EUR: {
const { timeout } = contract as Contract<
({ timeout } = contract as Contract<
SwapAsset.BTC | SwapAsset.USDC_MATIC | SwapAsset.USDT_MATIC | SwapAsset.EUR
>;
if (timeout <= timestamp) return true;
remainingTimes.push(timeout - timestamp);
>);
break;
}
default: throw new Error('Invalid swap asset');
}
if (timeout <= timestamp) return true;
remainingTimes.push(timeout - timestamp);
}
// eslint-disable-next-line no-console
Expand Down
2 changes: 1 addition & 1 deletion src/stores/Swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type SwapNimData = {
address?: string,
refundAddress: string,
redeemAddress: string,
timeoutBlockHeight: number,
timeoutMs: number,
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/stores/Transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ async function detectSwap(transaction: Transaction, knownTransactions: Transacti
address: transaction.recipient,
refundAddress: fundingData.sender,
redeemAddress: fundingData.recipient,
timeoutBlockHeight: fundingData.timeout,
timeoutMs: fundingData.timeout,
},
});

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,11 @@
resolved "https://registry.yarnpkg.com/@nimiq/fastspot-api/-/fastspot-api-1.10.2.tgz#ae2cbe5b41359875bece9ce0957209ca1b486d21"
integrity sha512-rPy3DhWlqTOj4k9/YMS2mizjR1rLsQzqWzMnJ+xDEGprgb6/jhDBZs/CW+jONccRQWrvuyBYTrrxdLgn3imKjQ==

"@nimiq/fastspot-api@^1.10.3":
version "1.10.3"
resolved "https://registry.yarnpkg.com/@nimiq/fastspot-api/-/fastspot-api-1.10.3.tgz#9a4b6c5594708f3dd76f8bf1b0fd189ca886dbce"
integrity sha512-CUkIAcF7R3OlN45idB+yC9Wnuz2YXTPx25xYswCdmbMGu/EGZFNnGHLIe6HkbHd8Eh+uBrwqBmLZGm8jyQ9+6A==

"@nimiq/hub-api@^1.10.0":
version "1.10.0"
resolved "https://registry.yarnpkg.com/@nimiq/hub-api/-/hub-api-1.10.0.tgz#fa7c33d402f10dc5816fb2537d737a79f903fe15"
Expand Down

0 comments on commit 776a6af

Please sign in to comment.