Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added useFetchRedeemTx hook #2340

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions wormhole-connect/src/hooks/useFetchRedeemTx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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 = (): void => {
const dispatch = useDispatch();
const { receipt } = useContext(RouteContext);

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

let isActive = true;

const fetchRedeemTx = async () => {
// TODO: remove once this is published: https://github.com/wormhole-foundation/wormhole-sdk-ts/pull/661
const wormholeApi = config.wormholeApi.replace(/\/$/, '');
artursapek marked this conversation as resolved.
Show resolved Hide resolved

const { txid } = receipt.originTxs[receipt.originTxs.length - 1];
let retry = 0;
while (isActive && retry < 10) {
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);
kev1n-peters marked this conversation as resolved.
Show resolved Hide resolved
retry++;
}
};

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
Loading