2022-05-20 09:26:34 -07:00
|
|
|
import {
|
|
|
|
ChainId,
|
|
|
|
CHAIN_ID_AVAX,
|
|
|
|
CHAIN_ID_POLYGON,
|
|
|
|
isEVMChain,
|
|
|
|
} from "@certusone/wormhole-sdk";
|
2022-01-23 10:21:23 -08:00
|
|
|
import { LinearProgress, makeStyles, Typography } from "@material-ui/core";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { useEthereumProvider } from "../contexts/EthereumProviderContext";
|
2022-01-31 10:36:15 -08:00
|
|
|
import { getChainName } from "../utils/consts";
|
2022-01-23 10:21:23 -08:00
|
|
|
|
|
|
|
const useStyles = makeStyles((theme) => ({
|
|
|
|
root: {
|
|
|
|
marginTop: theme.spacing(2),
|
|
|
|
textAlign: "center",
|
|
|
|
},
|
|
|
|
message: {
|
|
|
|
marginTop: theme.spacing(1),
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
export default function TransactionProgress({
|
|
|
|
chainId,
|
|
|
|
txBlockNumber,
|
2022-01-26 09:07:33 -08:00
|
|
|
hasSignedVAA,
|
|
|
|
isTargetSwapComplete,
|
2022-01-23 10:21:23 -08:00
|
|
|
}: {
|
|
|
|
chainId: ChainId;
|
|
|
|
txBlockNumber: number | undefined;
|
2022-01-26 09:07:33 -08:00
|
|
|
hasSignedVAA: boolean;
|
|
|
|
isTargetSwapComplete: boolean;
|
2022-01-23 10:21:23 -08:00
|
|
|
}) {
|
|
|
|
const classes = useStyles();
|
|
|
|
const { provider } = useEthereumProvider();
|
|
|
|
const [currentBlock, setCurrentBlock] = useState(0);
|
|
|
|
useEffect(() => {
|
2022-01-26 09:07:33 -08:00
|
|
|
if (hasSignedVAA || !txBlockNumber) return;
|
2022-01-23 10:21:23 -08:00
|
|
|
if (isEVMChain(chainId) && provider) {
|
|
|
|
let cancelled = false;
|
|
|
|
(async () => {
|
|
|
|
while (!cancelled) {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
|
|
try {
|
|
|
|
const newBlock = await provider.getBlockNumber();
|
|
|
|
if (!cancelled) {
|
|
|
|
setCurrentBlock(newBlock);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
return () => {
|
|
|
|
cancelled = true;
|
|
|
|
};
|
|
|
|
}
|
2022-01-26 09:07:33 -08:00
|
|
|
}, [hasSignedVAA, chainId, provider, txBlockNumber]);
|
2022-01-31 10:36:15 -08:00
|
|
|
let blockDiff =
|
2022-01-23 10:21:23 -08:00
|
|
|
txBlockNumber !== undefined && txBlockNumber && currentBlock
|
|
|
|
? currentBlock - txBlockNumber
|
|
|
|
: 0;
|
2022-05-20 09:26:34 -07:00
|
|
|
const expectedBlocks =
|
|
|
|
chainId === CHAIN_ID_POLYGON ? 512 : CHAIN_ID_AVAX ? 1 : 15;
|
2022-01-31 10:36:15 -08:00
|
|
|
blockDiff = Math.min(Math.max(blockDiff, 0), expectedBlocks);
|
2022-01-23 10:21:23 -08:00
|
|
|
let value;
|
|
|
|
let valueBuffer;
|
|
|
|
let message;
|
2022-01-26 09:07:33 -08:00
|
|
|
if (!hasSignedVAA) {
|
|
|
|
value = (blockDiff / expectedBlocks) * 50;
|
|
|
|
valueBuffer = 50;
|
2022-01-31 10:36:15 -08:00
|
|
|
message = `Waiting for ${blockDiff} / ${expectedBlocks} confirmations on ${getChainName(
|
|
|
|
chainId
|
|
|
|
)}...`;
|
2022-01-26 09:07:33 -08:00
|
|
|
} else if (!isTargetSwapComplete) {
|
|
|
|
value = 50;
|
|
|
|
valueBuffer = 100;
|
|
|
|
message = "Waiting for relayer to complete swap...";
|
|
|
|
} else {
|
|
|
|
value = 100;
|
|
|
|
valueBuffer = 100;
|
|
|
|
message = "Success!";
|
2022-01-23 10:21:23 -08:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div className={classes.root}>
|
|
|
|
<LinearProgress
|
|
|
|
variant="buffer"
|
|
|
|
value={value}
|
|
|
|
valueBuffer={valueBuffer}
|
|
|
|
/>
|
|
|
|
<Typography variant="body2" className={classes.message}>
|
|
|
|
{message}
|
|
|
|
</Typography>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|