-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.tsx
327 lines (307 loc) · 12.4 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { ChainId, CurrencyAmount, Ether, WNATIVE } from '../../../sdk'
import React, { FC, useEffect, useMemo, useState } from 'react'
import Container from '../../../components/Container'
import Head from 'next/head'
import Typography from '../../../components/Typography'
import { t } from '@lingui/macro'
import { useActiveWeb3React } from '../../../hooks/useActiveWeb3React'
import { useLingui } from '@lingui/react'
import DoubleGlowShadow from '../../../components/DoubleGlowShadow'
import SolarbeamLogo from '../../../components/SolarbeamLogo'
import useSWR, { SWRResponse } from 'swr'
import NavLink from '../../../components/NavLink'
import Button from '../../../components/Button'
import { BottomGrouping } from '../../../features/swap/styleds'
import Web3Connect from '../../../components/Web3Connect'
import { useWeb3React } from '@web3-react/core'
import { BridgeContextName } from '../../../constants'
import { bridgeInjected } from '../../../connectors'
import { isTransactionRecent, useAllTransactions } from '../../../state/bridgeTransactions/hooks'
import { TransactionDetails } from '../../../state/transactions/reducer'
import { useDispatch } from 'react-redux'
import { AppDispatch } from '../../../state'
import { getExplorerLink } from '../../../functions/explorer'
import { CheckCircleIcon, ExclamationIcon, RefreshIcon, XCircleIcon } from '@heroicons/react/solid'
import { classNames } from '../../../functions'
import Loader from '../../../components/Loader'
import ExternalLink from '../../../components/ExternalLink'
import { RefreshCw } from 'react-feather'
import { NETWORK_ICON, NETWORK_LABEL } from '../../../constants/networks'
import moment from 'moment'
import { FixedSizeList } from 'react-window'
type AnyswapTokenInfo = {
ID: string
Name: string
Symbol: string
Decimals: number
Description: string
BaseFeePercent: number
BigValueThreshold: number
DepositAddress: string
ContractAddress: string
DcrmAddress: string
DisableSwap: boolean
IsDelegateContract: boolean
MaximumSwap: number
MaximumSwapFee: number
MinimumSwap: number
MinimumSwapFee: number
PlusGasPricePercentage: number
SwapFeeRate: number
}
type AnyswapResultPairInfo = {
DestToken: AnyswapTokenInfo
PairID: string
SrcToken: AnyswapTokenInfo
destChainID: string
logoUrl: string
name: string
srcChainID: string
symbol: string
}
type AvailableChainsInfo = {
id: string
token: AnyswapTokenInfo
other: AnyswapTokenInfo
logoUrl: string
name: string
symbol: string
destChainID: string
}
export type AnyswapTokensMap = { [chainId: number]: { [contract: string]: AvailableChainsInfo } }
// we want the latest one to come first, so return negative if a is after b
function newTransactionsFirst(a: TransactionDetails, b: TransactionDetails) {
return b.addedTime - a.addedTime
}
const Transaction: FC<{ chainId: string; hash: string }> = ({ chainId, hash }) => {
const { i18n } = useLingui()
const allTransactions = useAllTransactions()
const dispatch = useDispatch<AppDispatch>()
const [status, setStatus] = useState(null)
const tx = allTransactions?.[chainId][hash]
const summary = tx?.summary
const destChainId = tx?.destChainId
const srcChaindId = tx?.srcChaindId
const from = tx?.from
const pairId = tx?.pairId
const tzTime = parseInt(tx?.addedTime) / 1000
const addedTime = moment.unix(tzTime).fromNow()
const getUrl = () => {
if (srcChaindId == ChainId.MOONRIVER) {
return `https://bridgeapi.anyswap.exchange/v2/getWithdrawHashStatus/${from}/${hash}/${srcChaindId}/${pairId}/${destChainId}`
} else {
return `https://bridgeapi.anyswap.exchange/v2/getHashStatus/${from}/${hash}/${destChainId}/${pairId}/${srcChaindId}`
}
}
const { data: anyswapInfo, error }: SWRResponse<any, Error> = useSWR(`${getUrl()}`, (url) =>
fetch(url)
.then((result) => result.json())
.then((data) => {
if (data && data.msg == 'Success') {
let resultStatus = data?.info?.status || 8
setStatus(resultStatus)
}
})
)
if (!chainId) return null
return (
<div className={'w-full px-2 py-2 text-left rounded select-none bg-dark-700 text-primary text-sm md:text-lg'}>
<div className="flex flex-col px-2 md:px-0 md:flex-row">
<div className="flex justify-between flex-row md:flex-none md:w-40">
<div className="text-base font-bold text-primary items-center md:hidden">
<div className="w-40">{i18n._(t`Date`)}</div>
</div>
<Typography variant="sm" className="flex items-center py-0.5">
{addedTime}
</Typography>
</div>
<div className="flex justify-between flex-row md:flex-grow">
<div className="text-base font-bold text-primary items-center md:hidden">
<div className="w-40">{i18n._(t`Transaction`)}</div>
</div>
<ExternalLink
href={getExplorerLink(parseInt(chainId), hash, 'transaction')}
className="flex items-center justify-between gap-2"
>
<Typography variant="sm" className="flex items-center hover:underline py-0.5">
{summary} ↗
</Typography>
</ExternalLink>
</div>
<div className="flex justify-between flex-row md:flex-none md:w-24">
<div className="text-base font-bold text-primary items-center md:hidden">
<div className="w-40">{i18n._(t`From`)}</div>
</div>
<Typography variant="sm" className="flex items-center py-0.5">
{NETWORK_LABEL[srcChaindId]}
</Typography>
</div>
<div className="flex justify-between flex-row md:flex-none md:w-24">
<div className="text-base font-bold text-primary items-center md:hidden">
<div className="w-40">{i18n._(t`To`)}</div>
</div>
<Typography variant="sm" className="flex items-center py-0.5">
{NETWORK_LABEL[destChainId]}
</Typography>
</div>
<div className="flex justify-between md:justify-end flex-row md:flex-none md:w-24 ">
<div className="text-base font-bold text-primary items-center md:hidden">
<div className="w-40">{i18n._(t`Status`)}</div>
</div>
<Typography variant="sm" className="flex items-center md:py-0.5 justify-end">
<div className={'text-primary'}>
{status === null ? (
<Loader stroke={'white'} />
) : status == 0 ? (
'Pending'
) : status == 8 ? (
'Processing'
) : status == 9 ? (
'Minting'
) : status == 10 ? (
'Complete'
) : (
'Pending'
)}
</div>
</Typography>
</div>
</div>
</div>
)
}
function renderTransactions(
address: string,
transactions: { [chainId: number]: { [txHash: string]: TransactionDetails } }
) {
const txs = []
Object.keys(transactions).forEach((chainId, i) => {
const chainTxs = transactions[chainId]
Object.keys(chainTxs).forEach((hash, idx) => {
const tx = chainTxs[hash]
if (tx.from.toString() == address?.toString()) {
txs.push({ ...tx, chainId, hash })
}
})
})
return (
<div className="flex flex-col flex-nowrap gap-2">
{txs
.sort((tx, tx1) => tx1.addedTime - tx.addedTime)
.map((tx, i) => {
return <Transaction key={i} hash={tx.hash} chainId={tx.chainId} />
})}
</div>
)
}
export default function Bridge() {
const { i18n } = useLingui()
const { account: activeAccount, chainId: activeChainId } = useActiveWeb3React()
const { account, chainId, library, activate } = useWeb3React(BridgeContextName)
const [refresher, setRefresher] = useState(0)
const allTransactions = useAllTransactions(refresher)
useEffect(() => {
activate(bridgeInjected)
}, [activate, chainId, activeAccount, activeChainId])
return (
<>
<Head>
<title>{i18n._(t`Bridge`)} | Solarbeam</title>
<meta key="description" name="description" content="Bridge" />
</Head>
<SolarbeamLogo />
<Container maxWidth="2xl" className="space-y-6">
<DoubleGlowShadow opacity="0.6">
<div className="p-4 space-y-4 rounded bg-dark-900" style={{ zIndex: 1 }}>
<div className="flex items-center justify-center mb-4 space-x-3">
<div className="grid grid-cols-3 rounded p-3px bg-dark-800 h-[46px]">
<NavLink
activeClassName="font-bold border rounded text-high-emphesis border-dark-700 bg-dark-700"
exact
href={{
pathname: '/bridge',
}}
>
<a className="flex items-center justify-center px-4 text-base font-medium text-center rounded-md text-secondary hover:text-high-emphesis ">
<Typography component="h1" variant="lg">
{i18n._(t`Bridge`)}
</Typography>
</a>
</NavLink>
<NavLink
activeClassName="font-bold border rounded text-high-emphesis border-dark-700 bg-dark-700"
exact
href={{
pathname: '/bridge/history',
}}
>
<a className="flex items-center justify-center px-4 text-base font-medium text-center rounded-md text-secondary hover:text-high-emphesis">
<Typography component="h1" variant="lg">
{i18n._(t`History`)}
</Typography>
</a>
</NavLink>
<NavLink
activeClassName="font-bold border rounded text-high-emphesis border-dark-700 bg-dark-700"
exact
href={{
pathname: '/bridge/faucet',
}}
>
<a className="flex items-center justify-center px-4 text-base font-medium text-center rounded-md text-secondary hover:text-high-emphesis">
<Typography component="h1" variant="lg">
{i18n._(t`Faucet`)}
</Typography>
</a>
</NavLink>
</div>
</div>
<div className="p-4 text-center">
<div className="justify-between space-x-3 items-center">
<Typography component="h3" variant="base">
{i18n._(t`View the history of all your bridge interactions`)}
</Typography>
</div>
</div>
<div className="flex items-center justify-between px-4">
<Typography weight={700}></Typography>
<div>
<RefreshCw
onClick={() => setRefresher(refresher + 1)}
size={20}
className={'font-emphasis hover:font-high-emphasis cursor-pointer'}
/>
</div>
</div>
<BottomGrouping>
{!account && activeAccount ? (
<Web3Connect size="lg" color="gradient" className="w-full" />
) : (
<div className="space-y-2 p-4 rounded bg-dark-800 mb-2 h-[455px] overflow-y-auto">
{allTransactions && Object.keys(allTransactions).length > 0 ? (
<>
<div className="flex px-2 hidden md:flex text-base font-bold text-primary items-center">
<div className="flex-none w-40">{i18n._(t`Date`)}</div>
<div className="flex-grow">{i18n._(t`Transaction`)}</div>
<div className="flex-none w-24">{i18n._(t`From`)}</div>
<div className="flex-none w-24">{i18n._(t`To`)}</div>
<div className="flex-none w-24 text-right">{i18n._(t`Status`)}</div>
</div>
<div className="flex-col mt-2">
{renderTransactions(activeAccount || account, allTransactions)}
</div>
</>
) : (
<Typography variant="sm" className="text-secondary">
{i18n._(t`Your transactions will appear here...`)}
</Typography>
)}
</div>
)}
</BottomGrouping>
</div>
</DoubleGlowShadow>
</Container>
</>
)
}