-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.tsx
647 lines (594 loc) · 23 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import {
AbstractCurrency,
Binance,
ChainId,
Currency,
CurrencyAmount,
Ether,
JSBI,
Moonriver,
NATIVE,
Token,
WNATIVE,
} from '../../sdk'
import React, { useCallback, useEffect, useState } from 'react'
import { AutoRow } from '../../components/Row'
import Container from '../../components/Container'
import Head from 'next/head'
import { ArrowDown, ArrowRight, Clock, Settings } from 'react-feather'
import Typography from '../../components/Typography'
import Web3Connect from '../../components/Web3Connect'
import { t } from '@lingui/macro'
import { useActiveWeb3React } from '../../hooks/useActiveWeb3React'
import { useLingui } from '@lingui/react'
import { useMultichainCurrencyBalance, useTokenBalance } from '../../state/wallet/hooks'
import DoubleGlowShadow from '../../components/DoubleGlowShadow'
import SolarbeamLogo from '../../components/SolarbeamLogo'
import { BottomGrouping } from '../../features/swap/styleds'
import Button from '../../components/Button'
import DualChainCurrencyInputPanel from '../../components/DualChainCurrencyInputPanel'
import ChainSelect from '../../components/ChainSelect'
import { Chain, DEFAULT_CHAIN_FROM, DEFAULT_CHAIN_TO } from '../../sdk/entities/Chain'
import { useBridgeInfo } from '../../features/bridge/hooks'
import useSWR, { SWRResponse } from 'swr'
import { getAddress } from 'ethers/lib/utils'
import { formatNumber } from '../../functions'
import { SUPPORTED_NETWORKS } from '../../modals/ChainModal'
import { NETWORK_ICON, NETWORK_LABEL } from '../../constants/networks'
import { ethers } from 'ethers'
import { useAnyswapTokenContract, useTokenContract } from '../../hooks'
import Loader from '../../components/Loader'
import { getWeb3ReactContext, useWeb3React } from '@web3-react/core'
import { BridgeContextName } from '../../constants'
import { bridgeInjected } from '../../connectors'
import NavLink from '../../components/NavLink'
import { useTransactionAdder } from '../../state/bridgeTransactions/hooks'
import { useRouter } from 'next/router'
import Modal from '../../components/Modal'
import ModalHeader from '../../components/ModalHeader'
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 } }
export default function Bridge() {
const { i18n } = useLingui()
const { account: activeAccount, chainId: activeChainId } = useActiveWeb3React()
const { account, chainId, library, activate } = useWeb3React(BridgeContextName)
const { push } = useRouter()
const addTransaction = useTransactionAdder()
const currentChainFrom = chainId &&
SUPPORTED_NETWORKS[chainId] && { id: chainId, icon: NETWORK_ICON[chainId], name: NETWORK_LABEL[chainId] }
useEffect(() => {
activate(bridgeInjected)
if (chainId) {
if (chainId == chainTo.id) {
setChainTo(chainFrom)
}
setChainFrom({ id: chainId, icon: NETWORK_ICON[chainId], name: NETWORK_LABEL[chainId] })
}
}, [activate, chainId, activeAccount, activeChainId])
const [chainFrom, setChainFrom] = useState<Chain | null>(currentChainFrom || DEFAULT_CHAIN_FROM)
const [chainTo, setChainTo] = useState<Chain | null>(
chainId == ChainId.MOONRIVER ? DEFAULT_CHAIN_FROM : DEFAULT_CHAIN_TO
)
const [tokenList, setTokenList] = useState<Currency[] | null>([])
const [currency0, setCurrency0] = useState<Currency | null>(null)
const [currencyAmount, setCurrencyAmount] = useState<string | null>('')
const [tokenToBridge, setTokenToBridge] = useState<AvailableChainsInfo | null>(null)
const currencyContract = useTokenContract(currency0?.isToken && currency0?.address, true)
const anyswapCurrencyContract = useAnyswapTokenContract(
currency0 && currency0.chainId == ChainId.MOONRIVER && tokenToBridge.other.ContractAddress,
true
)
const [pendingTx, setPendingTx] = useState(false)
const [showConfirmation, setShowConfirmation] = useState(false)
const selectedCurrencyBalance = useMultichainCurrencyBalance(
chainFrom?.id,
account ?? undefined,
currency0 ?? undefined
)
const { data: anyswapInfo, error }: SWRResponse<AnyswapTokensMap, Error> = useSWR(
'https://bridgeapi.anyswap.exchange/v2/serverInfo/1285',
(url) =>
fetch(url)
.then((result) => result.json())
.then((data) => {
let result: AnyswapTokensMap = {}
Object.keys(data || {}).map((key) => {
const info: AnyswapResultPairInfo = data[key]
let sourceContractAddress = info.SrcToken.ContractAddress
if (!sourceContractAddress) {
sourceContractAddress = WNATIVE[parseInt(info.srcChainID)].address
}
sourceContractAddress = sourceContractAddress.toLowerCase()
let existingSource = result[parseInt(info.srcChainID)]
if (!existingSource) {
result[parseInt(info.srcChainID)] = {
[sourceContractAddress]: {
destChainID: info.destChainID,
id: info.PairID,
logoUrl: info.logoUrl,
name: info.name,
symbol: info.symbol,
token: info.DestToken,
other: info.SrcToken,
},
}
} else {
result[parseInt(info.srcChainID)][sourceContractAddress] = {
destChainID: info.destChainID,
id: info.PairID,
logoUrl: info.logoUrl,
name: info.name,
symbol: info.symbol,
token: info.DestToken,
other: info.SrcToken,
}
}
let destContractAddress = info.DestToken.ContractAddress
if (!destContractAddress) {
destContractAddress = WNATIVE[parseInt(info.destChainID)].address
}
destContractAddress = destContractAddress.toLowerCase()
let existingDestination = result[parseInt(info.destChainID)]
if (!existingDestination) {
result[parseInt(info.destChainID)] = {
[destContractAddress]: {
destChainID: info.srcChainID,
id: info.PairID,
logoUrl: info.logoUrl,
name: info.name,
symbol: info.symbol,
token: info.SrcToken,
other: info.DestToken,
},
}
} else {
result[parseInt(info.destChainID)][destContractAddress] = {
destChainID: info.srcChainID,
id: info.PairID,
logoUrl: info.logoUrl,
name: info.name,
symbol: info.symbol,
token: info.SrcToken,
other: info.DestToken,
}
}
})
return result
})
)
useEffect(() => {
let tokens: Currency[] = Object.keys((anyswapInfo && anyswapInfo[chainFrom.id]) || {})
.filter((r) => anyswapInfo[chainFrom.id][r].destChainID == chainTo.id.toString())
.map((r) => {
const info: AvailableChainsInfo = anyswapInfo[chainFrom.id][r]
if (r.toLowerCase() == WNATIVE[chainFrom.id].address.toLowerCase()) {
if (chainFrom.id == ChainId.MOONRIVER) {
return Moonriver.onChain(chainFrom.id)
}
if (chainFrom.id == ChainId.BSC) {
return Binance.onChain(chainFrom.id)
}
if (chainFrom.id == ChainId.MAINNET) {
return Ether.onChain(chainFrom.id)
}
}
return new Token(chainFrom.id, getAddress(r), info.token.Decimals, info.token.Symbol, info.name)
})
setTokenList(tokens)
setCurrency0(null)
setCurrencyAmount('')
}, [chainFrom, anyswapInfo, chainTo.id])
const handleChainFrom = useCallback(
(chain: Chain) => {
let changeTo = chainTo
if (chainTo.id == chain.id) {
changeTo = chainFrom
}
if (changeTo.id !== ChainId.MOONRIVER && chain.id !== ChainId.MOONRIVER) {
setChainTo(DEFAULT_CHAIN_TO)
} else {
setChainTo(changeTo)
}
setChainFrom(chain)
},
[chainFrom, chainTo]
)
const handleChainTo = useCallback(
(chain: Chain) => {
let changeFrom = chainFrom
if (chainFrom.id == chain.id) {
changeFrom = chainTo
}
if (changeFrom.id !== ChainId.MOONRIVER && chain.id !== ChainId.MOONRIVER) {
setChainFrom(DEFAULT_CHAIN_TO)
} else {
setChainFrom(changeFrom)
}
setChainTo(chain)
},
[chainFrom, chainTo]
)
const handleTypeInput = useCallback(
(value: string) => {
setCurrencyAmount(value)
},
[setCurrencyAmount]
)
const handleCurrencySelect = useCallback(
(currency: Currency) => {
setCurrency0(currency)
handleTypeInput('')
if (currency) {
const tokenTo =
anyswapInfo[chainFrom.id][
currency.isToken ? currency?.address?.toLowerCase() : currency?.wrapped?.address?.toLowerCase()
]
setTokenToBridge(tokenTo)
}
},
[anyswapInfo, chainFrom.id, handleTypeInput]
)
const insufficientBalance = () => {
if (currencyAmount && selectedCurrencyBalance) {
try {
const balance = parseFloat(selectedCurrencyBalance.toFixed(currency0.decimals))
const amount = parseFloat(currencyAmount)
return amount > balance
} catch (ex) {
return false
}
}
return false
}
const aboveMin = () => {
if (currencyAmount && tokenToBridge) {
const amount = parseFloat(currencyAmount)
const minAmount = parseFloat(tokenToBridge?.other?.MinimumSwap.toString())
return amount >= minAmount
}
return false
}
const belowMax = () => {
if (currencyAmount && tokenToBridge) {
const amount = parseFloat(currencyAmount)
const maxAmount = parseFloat(tokenToBridge?.other?.MaximumSwap.toString())
return amount <= maxAmount
}
return false
}
const getAmountToReceive = () => {
if (!tokenToBridge) return 0
let fee = parseFloat(currencyAmount) * tokenToBridge?.other?.SwapFeeRate
if (fee < tokenToBridge?.other?.MinimumSwapFee) {
fee = tokenToBridge?.other?.MinimumSwapFee
} else if (fee > tokenToBridge?.other?.MaximumSwapFee) {
fee = tokenToBridge?.other?.MinimumSwapFee
}
return (parseFloat(currencyAmount) - fee).toFixed(6)
}
const buttonDisabled =
(chainFrom && chainFrom.id !== chainId) ||
!currency0 ||
!currencyAmount ||
currencyAmount == '' ||
!aboveMin() ||
!belowMax() ||
insufficientBalance() ||
pendingTx
const buttonText =
chainFrom && chainFrom.id !== chainId
? `Switch to ${chainFrom.name} Network`
: !currency0
? `Select a Token`
: !currencyAmount || currencyAmount == ''
? 'Enter an Amount'
: !aboveMin()
? `Below Minimum Amount`
: !belowMax()
? `Above Maximum Amount`
: insufficientBalance()
? `Insufficient Balance`
: pendingTx
? `Confirming Transaction`
: `Bridge ${currency0?.symbol}`
const bridgeToken = async () => {
const token = tokenToBridge.other
const depositAddress = currency0.chainId == ChainId.MOONRIVER ? token.ContractAddress : token.DepositAddress
const amountToBridge = ethers.utils.parseUnits(currencyAmount, token.Decimals)
setPendingTx(true)
try {
if (currency0.chainId == ChainId.MOONRIVER) {
if (currency0.isNative) {
} else if (currency0.isToken) {
const fn = anyswapCurrencyContract?.interface?.getFunction('Swapout')
const data = anyswapCurrencyContract.interface.encodeFunctionData(fn, [amountToBridge.toString(), account])
const tx = await library.getSigner().sendTransaction({
value: 0x0,
from: account,
to: currency0.address,
data,
})
addTransaction(tx, {
summary: `${i18n._(t`Bridge `)} ${tokenToBridge.symbol}`,
destChainId: chainTo.id.toString(),
srcChaindId: chainFrom.id.toString(),
pairId: tokenToBridge.id,
})
push('/bridge/history')
}
} else {
if (currency0.isNative) {
const tx = await library.getSigner().sendTransaction({
from: account,
to: depositAddress,
value: amountToBridge,
})
addTransaction(tx, {
summary: `${i18n._(t`Bridge `)} ${tokenToBridge.symbol}`,
destChainId: chainTo.id.toString(),
srcChaindId: chainFrom.id.toString(),
pairId: tokenToBridge.id,
})
push('/bridge/history')
} else if (currency0.isToken) {
const fn = currencyContract?.interface?.getFunction('transfer')
const data = currencyContract.interface.encodeFunctionData(fn, [depositAddress, amountToBridge.toString()])
const tx = await library.getSigner().sendTransaction({
value: 0x0,
from: account,
to: currency0.address,
data,
})
addTransaction(tx, {
summary: `${i18n._(t`Bridge `)} ${tokenToBridge.symbol}`,
destChainId: chainTo.id.toString(),
srcChaindId: chainFrom.id.toString(),
pairId: tokenToBridge.id,
})
push('/bridge/history')
}
}
} catch (ex) {
} finally {
setPendingTx(false)
}
}
const anyswapChains = [ChainId.MOONRIVER, ChainId.BSC, ChainId.MAINNET]
const availableChains = Object.keys(anyswapInfo || {})
.map((r) => parseInt(r))
.filter((r) => anyswapChains.includes(r))
return (
<>
<Modal isOpen={showConfirmation} onDismiss={() => setShowConfirmation(false)}>
<div className="space-y-4">
<ModalHeader title={i18n._(t`Bridge ${currency0?.symbol}`)} onClose={() => setShowConfirmation(false)} />
<Typography variant="sm" className="font-medium">
{i18n._(t`You are sending ${formatNumber(currencyAmount)} ${currency0?.symbol} from ${chainFrom?.name}`)}
</Typography>
<Typography variant="sm" className="font-medium">
{i18n._(t`You will receive ${formatNumber(getAmountToReceive())} ${currency0?.symbol} on ${chainTo?.name}`)}
</Typography>
<Button color="gradient" size="lg" disabled={pendingTx} onClick={() => bridgeToken()}>
<Typography variant="lg">
{pendingTx ? (
<div className={'p-2'}>
<AutoRow gap="6px" justify="center">
{buttonText} <Loader stroke="white" />
</AutoRow>
</div>
) : (
i18n._(t`Bridge ${currency0?.symbol}`)
)}
</Typography>
</Button>
</div>
</Modal>
<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`Bridge tokens to and from the Moonriver Network`)}
</Typography>
</div>
</div>
<div className="flex flex-row justify-between items-center text-center">
<ChainSelect
availableChains={availableChains}
label="From"
chain={chainFrom}
otherChain={chainTo}
onChainSelect={(chain) => handleChainFrom(chain)}
switchOnSelect={true}
/>
<button className={'sm:m-6'}>
<ArrowRight size="32" />
</button>
<ChainSelect
availableChains={availableChains}
label="To"
chain={chainTo}
otherChain={chainFrom}
onChainSelect={(chain) => handleChainTo(chain)}
switchOnSelect={false}
/>
</div>
<DualChainCurrencyInputPanel
label={i18n._(t`Token to bridge:`)}
value={currencyAmount}
currency={currency0}
onUserInput={handleTypeInput}
onMax={(amount) => handleTypeInput(amount)}
onCurrencySelect={(currency) => {
handleCurrencySelect(currency)
}}
chainFrom={chainFrom}
chainTo={chainTo}
tokenList={tokenList}
chainList={anyswapInfo}
/>
<BottomGrouping>
{!account ? (
<Web3Connect size="lg" color="gradient" className="w-full" />
) : (
<Button
onClick={() => setShowConfirmation(true)}
color={buttonDisabled ? 'gray' : 'gradient'}
size="lg"
disabled={buttonDisabled}
>
{pendingTx ? (
<div className={'p-2'}>
<AutoRow gap="6px" justify="center">
{buttonText} <Loader stroke="white" />
</AutoRow>
</div>
) : (
buttonText
)}
</Button>
)}
</BottomGrouping>
{currency0 && (
<div className={'p-2 sm:p-5 rounded bg-dark-800'}>
{tokenToBridge?.other?.MinimumSwapFee > 0 && (
<div className="flex flex-col justify-between space-y-3 sm:space-y-0 sm:flex-row">
<div className="text-sm font-medium text-secondary">
Minimum Bridge Fee: {formatNumber(tokenToBridge?.other?.MinimumSwapFee)}{' '}
{tokenToBridge?.other?.Symbol}
</div>
</div>
)}
{tokenToBridge?.other?.MaximumSwapFee > 0 && (
<div className="flex flex-col justify-between space-y-3 sm:space-y-0 sm:flex-row">
<div className="text-sm font-medium text-secondary">
Maximum Bridge Fee: {formatNumber(tokenToBridge?.other?.MaximumSwapFee)}{' '}
{tokenToBridge?.other?.Symbol}
</div>
</div>
)}
<div className="flex flex-col justify-between space-y-3 sm:space-y-0 sm:flex-row">
<div className="text-sm font-medium text-secondary">
Minimum Bridge Amount: {formatNumber(tokenToBridge?.other?.MinimumSwap)}{' '}
{tokenToBridge?.other?.Symbol}
</div>
</div>
<div className="flex flex-col justify-between space-y-3 sm:space-y-0 sm:flex-row">
<div className="text-sm font-medium text-secondary">
Maximum Bridge Amount: {formatNumber(tokenToBridge?.other?.MaximumSwap)}{' '}
{tokenToBridge?.other?.Symbol}
</div>
</div>
<div className="flex flex-col justify-between space-y-3 sm:space-y-0 sm:flex-row">
<div className="text-sm font-medium text-secondary">
Fee: {formatNumber(tokenToBridge?.other?.SwapFeeRate * 100)} %
</div>
</div>
<div className="flex flex-col justify-between space-y-3 sm:space-y-0 sm:flex-row">
<div className="text-sm font-medium text-secondary">
Amounts greater than {formatNumber(tokenToBridge?.other?.BigValueThreshold)}{' '}
{tokenToBridge?.other?.Symbol} could take up to 12 hours.
</div>
</div>
</div>
)}
<AutoRow
style={{ justifyItems: 'center', backgroundColor: '', padding: '12px 0px', borderRadius: '12px' }}
justify={'center'}
gap={'0 3px'}
>
{i18n._(t`Powered by Anyswap Network`)}
</AutoRow>
</div>
</DoubleGlowShadow>
</Container>
</>
)
}