-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathTransactionProgress.tsx
152 lines (150 loc) · 4.57 KB
/
TransactionProgress.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
ChainId,
CHAIN_ID_ACALA,
CHAIN_ID_AURORA,
CHAIN_ID_CELO,
CHAIN_ID_ETH,
CHAIN_ID_FANTOM,
CHAIN_ID_KARURA,
CHAIN_ID_KLAYTN,
CHAIN_ID_MOONBEAM,
CHAIN_ID_OASIS,
CHAIN_ID_POLYGON,
CHAIN_ID_SOLANA,
isEVMChain,
} from "@certusone/wormhole-sdk";
import { LinearProgress, makeStyles, Typography } from "@material-ui/core";
import { Connection } from "@solana/web3.js";
import { useEffect, useState } from "react";
import { useEthereumProvider } from "../contexts/EthereumProviderContext";
import { Transaction } from "../store/transferSlice";
import { CHAINS_BY_ID, CLUSTER, SOLANA_HOST } from "../utils/consts";
import SmartBlock from "./SmartBlock";
const useStyles = makeStyles((theme) => ({
root: {
marginTop: theme.spacing(2),
textAlign: "center",
},
message: {
marginTop: theme.spacing(1),
},
}));
export default function TransactionProgress({
chainId,
tx,
isSendComplete,
}: {
chainId: ChainId;
tx: Transaction | undefined;
isSendComplete: boolean;
}) {
const classes = useStyles();
const { provider } = useEthereumProvider();
const [currentBlock, setCurrentBlock] = useState(0);
useEffect(() => {
if (isSendComplete || !tx) return;
if (isEVMChain(chainId) && provider) {
let cancelled = false;
(async () => {
while (!cancelled) {
await new Promise((resolve) => setTimeout(resolve, 500));
try {
const newBlock =
chainId === CHAIN_ID_ETH
? (await provider.getBlock("finalized")).number
: await provider.getBlockNumber();
if (!cancelled) {
setCurrentBlock(newBlock);
}
} catch (e) {
console.error(e);
}
}
})();
return () => {
cancelled = true;
};
}
if (chainId === CHAIN_ID_SOLANA) {
let cancelled = false;
const connection = new Connection(SOLANA_HOST, "confirmed");
const sub = connection.onSlotChange((slotInfo) => {
if (!cancelled) {
setCurrentBlock(slotInfo.slot);
}
});
return () => {
cancelled = true;
connection.removeSlotChangeListener(sub);
};
}
}, [isSendComplete, chainId, provider, tx]);
if (chainId === CHAIN_ID_ETH) {
if (!isSendComplete && tx && tx.block && currentBlock) {
const isFinalized = currentBlock >= tx.block;
return (
<div className={classes.root}>
<Typography variant="body2" className={classes.message}>
{!isFinalized
? `Waiting for finality on ${CHAINS_BY_ID[chainId].name} which may take up to 15 minutes.`
: `Waiting for Wormhole Network consensus...`}
</Typography>
{!isFinalized ? (
<>
<span>Last finalized block number</span>
<SmartBlock chainId={chainId} blockNumber={currentBlock} />
<span>This transaction's block number</span>
<SmartBlock chainId={chainId} blockNumber={tx.block} />
</>
) : null}
</div>
);
}
} else {
const blockDiff =
tx && tx.block && currentBlock ? currentBlock - tx.block : undefined;
const expectedBlocks = // minimum confirmations enforced by guardians or specified by the contract
chainId === CHAIN_ID_POLYGON
? CLUSTER === "testnet"
? 64
: 512
: chainId === CHAIN_ID_OASIS ||
chainId === CHAIN_ID_AURORA ||
chainId === CHAIN_ID_FANTOM ||
chainId === CHAIN_ID_KARURA ||
chainId === CHAIN_ID_ACALA ||
chainId === CHAIN_ID_KLAYTN ||
chainId === CHAIN_ID_CELO ||
chainId === CHAIN_ID_MOONBEAM
? 1 // these chains only require 1 conf
: chainId === CHAIN_ID_SOLANA
? 32
: isEVMChain(chainId)
? 15
: 1;
if (
!isSendComplete &&
(chainId === CHAIN_ID_SOLANA || isEVMChain(chainId)) &&
blockDiff !== undefined
) {
return (
<div className={classes.root}>
<LinearProgress
value={
blockDiff < expectedBlocks
? (blockDiff / expectedBlocks) * 75
: 75
}
variant="determinate"
/>
<Typography variant="body2" className={classes.message}>
{blockDiff < expectedBlocks
? `Waiting for ${blockDiff} / ${expectedBlocks} confirmations on ${CHAINS_BY_ID[chainId].name}...`
: `Waiting for Wormhole Network consensus...`}
</Typography>
</div>
);
}
}
return null;
}