Skip to content

Commit

Permalink
Added useFetchRedeemTx hook
Browse files Browse the repository at this point in the history
The SDK currently doesn't set the redeem TX in all cases, so we
use this hook as a stop-gap until it does.
  • Loading branch information
kev1n-peters committed Aug 7, 2024
1 parent dc52896 commit 8e6b28e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
67 changes: 67 additions & 0 deletions wormhole-connect/src/hooks/useFetchRedeemTx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { useContext, useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { setRedeemTx } from 'store/redeem';
import { RouteContext } from 'contexts/RouteContext';
import { sleep } from 'utils';
import {
api,
toChain,
TransferState,
UniversalAddress,
} from '@wormhole-foundation/sdk';
import config from 'config';

// TODO: this hook is a stop-gap until the SDK reliably
// sets the redeem tx hash on the receipt
const useFetchRedeemTx = () => {

Check failure on line 16 in wormhole-connect/src/hooks/useFetchRedeemTx.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
const dispatch = useDispatch();
const { receipt } = useContext(RouteContext);

useEffect(() => {
if (
!receipt ||
receipt.state < TransferState.Attested ||
'originTxs' in receipt === false ||
receipt.originTxs.length === 0
) {
return;
}

let isActive = true;

const fetchRedeemTx = async () => {
const wormholeApi = config.wormholeApi.replace(/\/$/, '');
const { txid } = receipt.originTxs[receipt.originTxs.length - 1];
while (isActive) {
try {
const vaa = await api.getVaaByTxHash(wormholeApi, txid);
if (vaa) {
const status = await api.getTransactionStatus(wormholeApi, {
chain: toChain(vaa.emitterChain),
emitter: new UniversalAddress(vaa.emitterAddr),
sequence: BigInt(vaa.sequence),
});
const redeemTx = status?.globalTx?.destinationTx?.txHash;
if (redeemTx) {
if (isActive) {
dispatch(setRedeemTx(redeemTx));
}
break;
}
}
} catch (e) {
console.warn(e);
}
await sleep(10_000);
}
};

fetchRedeemTx();

return () => {
isActive = false;
};
}, [receipt]);
};

export default useFetchRedeemTx;
7 changes: 6 additions & 1 deletion wormhole-connect/src/views/Redeem/ExplorerLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LINK } from 'utils/style';
import config from 'config';
import LaunchIcon from '@mui/icons-material/Launch';
import { TransferSide } from 'config/types';
import { isEvmChain } from 'utils/sdk';

const useStyles = makeStyles()((theme) => ({
link: {
Expand Down Expand Up @@ -45,7 +46,11 @@ function ExplorerLink(props: ExplorerLinkProps) {
? `${chainConfig.explorerUrl}txs/${props.txHash}`
: `${chainConfig.explorerUrl}transactions/${props.txHash}`;
} else {
explorerLink = `${chainConfig.explorerUrl}tx/${props.txHash}`;
let txHash = props.txHash;
if (isEvmChain(chainConfig.key)) {
txHash = txHash.startsWith('0x') ? txHash : '0x' + txHash;
}
explorerLink = `${chainConfig.explorerUrl}tx/${txHash}`;
}
} else if (props.type === 'address') {
if (chainConfig.key === 'aptos') {
Expand Down
2 changes: 2 additions & 0 deletions wormhole-connect/src/views/Redeem/Redeem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import config from 'config';
import useConfirmBeforeLeaving from 'utils/confirmBeforeLeaving';

import useTrackTransfer from 'hooks/useTrackTransfer';
import useFetchRedeemTx from 'hooks/useFetchRedeemTx';

function Redeem({
txData,
Expand Down Expand Up @@ -149,6 +150,7 @@ function Redeem({

// useDeliveryStatus();
useTrackTransfer();
useFetchRedeemTx();

return txData?.fromChain ? (
<div
Expand Down

0 comments on commit 8e6b28e

Please sign in to comment.