Skip to content

Commit

Permalink
Update claim btc to confirm btc
Browse files Browse the repository at this point in the history
  • Loading branch information
qgatssdev committed Jan 8, 2025
1 parent caddd07 commit e514982
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button } from 'components/button'
import { WarningIcon } from 'components/icons/warningIcon'
import { useClaimBitcoinDeposit } from 'hooks/useBtcTunnel'
import { useConfirmBitcoinDeposit } from 'hooks/useBtcTunnel'
import { useTranslations } from 'next-intl'
import { type FormEvent, useContext, useEffect } from 'react'
import { type BtcDepositOperation, BtcDepositStatus } from 'types/tunnel'
Expand All @@ -13,13 +13,13 @@ type Props = {
deposit: BtcDepositOperation
}

export const ClaimBtcDeposit = function ({ deposit }: Props) {
export const ConfirmBtcDeposit = function ({ deposit }: Props) {
const {
claimBitcoinDeposit,
claimBitcoinDepositReceipt,
claimBitcoinDepositError,
claimBitcoinDepositReceiptError,
} = useClaimBitcoinDeposit(deposit)
confirmBitcoinDeposit,
confirmBitcoinDepositReceipt,
confirmBitcoinDepositError,
confirmBitcoinDepositReceiptError,
} = useConfirmBitcoinDeposit(deposit)
const [operationStatus, setOperationStatus] = useContext(
BtcToEvmDepositContext,
)
Expand All @@ -30,32 +30,32 @@ export const ClaimBtcDeposit = function ({ deposit }: Props) {
useEffect(
function clearAfterSuccessfulClaim() {
if (
claimBitcoinDepositReceipt?.status !== 'success' ||
confirmBitcoinDepositReceipt?.status !== 'success' ||
operationStatus !== 'claiming'
) {
return
}
setOperationStatus('idle')
},
[claimBitcoinDepositReceipt, operationStatus, setOperationStatus],
[confirmBitcoinDepositReceipt, operationStatus, setOperationStatus],
)

useEffect(
function handleUserRejection() {
if (claimBitcoinDepositError && isClaiming) {
if (confirmBitcoinDepositError && isClaiming) {
setOperationStatus('rejected')
}
},
[claimBitcoinDepositError, isClaiming, setOperationStatus],
[confirmBitcoinDepositError, isClaiming, setOperationStatus],
)

useEffect(
function handleTransactionFailure() {
if (claimBitcoinDepositReceiptError && isClaiming) {
if (confirmBitcoinDepositReceiptError && isClaiming) {
setOperationStatus('failed')
}
},
[claimBitcoinDepositReceiptError, isClaiming, setOperationStatus],
[confirmBitcoinDepositReceiptError, isClaiming, setOperationStatus],
)

const isReadyToClaim = deposit.status === BtcDepositStatus.BTC_READY_CLAIM
Expand All @@ -65,7 +65,7 @@ export const ClaimBtcDeposit = function ({ deposit }: Props) {
if (!isReadyToClaim) {
return
}
claimBitcoinDeposit()
confirmBitcoinDeposit()
setOperationStatus('claiming')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
BtcToEvmDepositContext,
BtcToEvmDepositProvider,
} from '../../_context/btcToEvmContext'
import { ClaimBtcDeposit } from '../claimBtcDeposit'
import { ConfirmBtcDeposit } from '../confirmBtcDeposit'
import { RetryBtcDeposit } from '../retryBtcDeposit'

import { Operation } from './operation'
Expand All @@ -26,7 +26,7 @@ const getCallToAction = function (deposit: BtcDepositOperation) {
case BtcDepositStatus.DEPOSIT_TX_FAILED:
return <RetryBtcDeposit deposit={deposit} />
case BtcDepositStatus.BTC_READY_CLAIM:
return <ClaimBtcDeposit deposit={deposit} />
return <ConfirmBtcDeposit deposit={deposit} />
default:
return null
}
Expand Down
54 changes: 28 additions & 26 deletions webapp/hooks/useBtcTunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { getBitcoinTimestamp } from 'utils/bitcoin'
import { getEvmBlock } from 'utils/evmApi'
import {
claimBtcDeposit,
confirmBtcDeposit,
initiateBtcDeposit,
initiateBtcWithdrawal,
} from 'utils/hemi'
Expand All @@ -31,21 +31,23 @@ import { useHemiClient, useHemiWalletClient } from './useHemiClient'
import { useTunnelHistory } from './useTunnelHistory'
import { useWaitForTransactionReceipt as useWaitForBtcTransactionReceipt } from './useWaitForTransactionReceipt'

export const useClaimBitcoinDeposit = function (deposit: BtcDepositOperation) {
export const useConfirmBitcoinDeposit = function (
deposit: BtcDepositOperation,
) {
const { address } = useEvmAccount()
const hemiClient = useHemiClient()
const queryClient = useQueryClient()
const { hemiWalletClient } = useHemiWalletClient()
const { updateDeposit } = useTunnelHistory()

const {
error: claimBitcoinDepositError,
reset: resetClaimBitcoinDeposit,
mutate: claimBitcoinDeposit,
data: claimBitcoinDepositTxHash,
error: confirmBitcoinDepositError,
reset: resetConfirmBitcoinDeposit,
mutate: confirmBitcoinDeposit,
data: confirmBitcoinDepositTxHash,
} = useMutation({
mutationFn: () =>
claimBtcDeposit({
confirmBtcDeposit({
deposit,
from: address,
hemiClient,
Expand All @@ -57,54 +59,54 @@ export const useClaimBitcoinDeposit = function (deposit: BtcDepositOperation) {
})

const {
data: claimBitcoinDepositReceipt,
error: claimBitcoinDepositReceiptError,
queryKey: claimBitcoinDepositQueryKey,
} = useWaitForEvmTransactionReceipt({ hash: claimBitcoinDepositTxHash })
data: confirmBitcoinDepositReceipt,
error: confirmBitcoinDepositReceiptError,
queryKey: confirmBitcoinDepositQueryKey,
} = useWaitForEvmTransactionReceipt({ hash: confirmBitcoinDepositTxHash })

const clearClaimBitcoinDepositState = useCallback(
const clearConfirmBitcoinDepositState = useCallback(
function () {
// reset the claiming state
resetClaimBitcoinDeposit()
resetConfirmBitcoinDeposit()
// clear deposit receipt state
queryClient.invalidateQueries({ queryKey: claimBitcoinDepositQueryKey })
queryClient.invalidateQueries({ queryKey: confirmBitcoinDepositQueryKey })
},
[claimBitcoinDepositQueryKey, queryClient, resetClaimBitcoinDeposit],
[confirmBitcoinDepositQueryKey, queryClient, resetConfirmBitcoinDeposit],
)

useEffect(
function handleClaimSuccess() {
if (claimBitcoinDepositReceipt?.status !== 'success') {
if (confirmBitcoinDepositReceipt?.status !== 'success') {
return
}
if (deposit.status !== BtcDepositStatus.BTC_READY_CLAIM) {
return
}
updateDeposit(deposit, {
claimTransactionHash: claimBitcoinDepositReceipt.transactionHash,
claimTransactionHash: confirmBitcoinDepositReceipt.transactionHash,
status: BtcDepositStatus.BTC_DEPOSITED,
})
},
[claimBitcoinDepositReceipt, deposit, updateDeposit],
[confirmBitcoinDepositReceipt, deposit, updateDeposit],
)

const handleClaim = function () {
if (deposit.status !== BtcDepositStatus.BTC_READY_CLAIM) {
return
}
clearClaimBitcoinDepositState()
clearConfirmBitcoinDepositState()
// clear any previous transaction hash, which may come from failed attempts
updateDeposit(deposit, { claimTransactionHash: undefined })
claimBitcoinDeposit()
confirmBitcoinDeposit()
}

return {
claimBitcoinDeposit: handleClaim,
claimBitcoinDepositError,
claimBitcoinDepositReceipt,
claimBitcoinDepositReceiptError,
claimBitcoinDepositTxHash,
clearClaimBitcoinDepositState,
clearConfirmBitcoinDepositState,
confirmBitcoinDeposit: handleClaim,
confirmBitcoinDepositError,
confirmBitcoinDepositReceipt,
confirmBitcoinDepositReceiptError,
confirmBitcoinDepositTxHash,
}
}

Expand Down
2 changes: 1 addition & 1 deletion webapp/utils/hemi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const initiateBtcDeposit = function ({
)
}

export const claimBtcDeposit = ({
export const confirmBtcDeposit = ({
deposit,
from,
hemiClient,
Expand Down

0 comments on commit e514982

Please sign in to comment.