-
Notifications
You must be signed in to change notification settings - Fork 306
/
index.tsx
284 lines (270 loc) · 10.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
import { memo, useMemo, useState } from 'react'
import { Trans } from 'react-i18next'
import { Box, Button, Link, TextField, Typography, useTheme } from '@mui/material'
import { makeStyles } from '@masknet/theme'
import { NUMERIC_INPUT_REGEXP_PATTERN, NetworkPluginID } from '@masknet/shared-base'
import { useChainContext, useChainIdSupport, useFungibleToken, useFungibleTokenBalance } from '@masknet/web3-hooks-base'
import { EVMExplorerResolver } from '@masknet/web3-providers'
import { CopyButton, TokenIcon } from '@masknet/shared'
import { Icons } from '@masknet/icons'
import { isGreaterThan, isZero, leftShift, rightShift } from '@masknet/web3-shared-base'
import { GasSettingMenu } from '../GasSettingMenu/index.js'
import type { TransactionDetail } from '../../pages/Wallet/type.js'
import { useMaskSharedTrans } from '../../../shared-ui/index.js'
import type { GasConfig } from '@masknet/web3-shared-evm'
const useStyles = makeStyles()((theme) => ({
title: {
fontSize: 18,
fontWeight: 700,
textAlign: 'center',
lineHeight: '22px',
},
tips: {
fontSize: 12,
fontWeight: 700,
textAlign: 'center',
color: theme.palette.maskColor.second,
marginTop: theme.spacing(1.25),
},
tokenInfo: {
background: theme.palette.maskColor.modalTitleBg,
padding: theme.spacing(1),
marginTop: theme.spacing(1.25),
display: 'flex',
alignItems: 'center',
borderRadius: 99,
},
tokenIcon: {
width: 24,
height: 24,
borderRadius: '50%',
},
name: {
fontSize: 12,
fontWeight: 700,
lineHeight: '16px',
},
address: {
fontSize: 12,
transform: 'scale(0.8333)',
color: theme.palette.maskColor.second,
fontWeight: 400,
transformOrigin: 'left',
},
link: {
width: 16,
height: 16,
color: theme.palette.maskColor.main,
},
amountInfo: {
marginTop: theme.spacing(3.25),
padding: theme.spacing(1.5),
borderRadius: 8,
border: `1px solid ${theme.palette.maskColor.line}`,
display: 'flex',
flexDirection: 'column',
rowGap: 10,
},
input: {
paddingRight: '0px !important',
background: theme.palette.maskColor.input,
},
max: {
fontWeight: 400,
textTransform: 'uppercase',
},
spender: {
color: theme.palette.maskColor.second,
fontSize: 12,
fontWeight: 700,
lineHeight: '16px',
display: 'flex',
flexDirection: 'column',
},
spenderAddress: {
marginTop: 4,
color: theme.palette.maskColor.main,
fontSize: 12,
fontWeight: 700,
lineHeight: '16px',
display: 'flex',
alignItems: 'center',
columnGap: 4,
},
gasFeeTitle: {
fontSize: 14,
fontWeight: 700,
color: theme.palette.maskColor.second,
},
}))
interface UnlockERC20TokenProps {
transaction: TransactionDetail
handleChange: (amount: string) => void
paymentToken?: string
onConfigChange: (config: GasConfig) => void
onPaymentTokenChange: (paymentToken: string) => void
}
export const UnlockERC20Token = memo<UnlockERC20TokenProps>(function UnlockERC20Token({
transaction,
handleChange,
onConfigChange,
onPaymentTokenChange,
paymentToken,
}) {
const t = useMaskSharedTrans()
const { classes } = useStyles()
const theme = useTheme()
const [value, setValue] = useState('')
const { chainId } = useChainContext<NetworkPluginID.PLUGIN_EVM>()
const isSupport1559 = useChainIdSupport(NetworkPluginID.PLUGIN_EVM, 'EIP1559', chainId)
const { data: token } = useFungibleToken(
NetworkPluginID.PLUGIN_EVM,
transaction.formattedTransaction?.tokenInAddress,
undefined,
{ chainId },
)
const { data: balance = '0' } = useFungibleTokenBalance(
NetworkPluginID.PLUGIN_EVM,
transaction.formattedTransaction?.tokenInAddress,
)
const initConfig = useMemo(() => {
if (!transaction?.computedPayload) return
if (isSupport1559) {
if (transaction.computedPayload.maxFeePerGas && transaction.computedPayload.maxPriorityFeePerGas)
return {
maxFeePerGas: transaction.computedPayload.maxFeePerGas,
maxPriorityFeePerGas: transaction.computedPayload.maxPriorityFeePerGas,
}
return
}
if (!transaction.computedPayload.gasPrice) return
return {
gasPrice: transaction.computedPayload.gasPrice,
}
}, [transaction?.computedPayload, isSupport1559])
const tips = useMemo(() => {
if (isZero(value)) {
return t.popups_wallet_unlock_erc20_revoke_tips()
}
if (isGreaterThan(value, leftShift(balance, token?.decimals))) {
return t.popups_wallet_unlock_erc20_balance_too_high_tips()
}
return t.popups_wallet_unlock_erc20_balance_tips({ amount: value, symbol: String(token?.symbol) })
}, [value, balance, token])
if (!transaction.formattedTransaction) return null
return (
<Box>
<Typography className={classes.title}>
<Trans
i18nKey="popups_wallet_unlock_erc20_title"
components={{ br: <br /> }}
values={{ symbol: token?.symbol }}
/>
</Typography>
<Typography className={classes.tips}>
<Trans i18nKey="popups_wallet_unlock_erc20_tips" components={{ br: <br /> }} />
</Typography>
<Box className={classes.tokenInfo}>
<TokenIcon
address={token?.address ?? ''}
chainId={chainId}
name={token?.name}
className={classes.tokenIcon}
/>
<Box width="262px" mr="18px" ml={1}>
<Typography className={classes.name}>{token?.symbol}</Typography>
<Typography className={classes.address}>{token?.address}</Typography>
</Box>
{token?.address ?
<Box display="flex" columnGap={1} alignItems="center">
<CopyButton text={token.address} size={16} />
<Link
href={EVMExplorerResolver.addressLink(chainId, token.address)}
className={classes.link}
target="_blank"
rel="noopener noreferrer">
<Icons.LinkOut size={16} />
</Link>
</Box>
: null}
</Box>
<Box className={classes.amountInfo}>
<TextField
fullWidth
placeholder={t.popups_wallet_unlock_erc20_placeholder()}
value={value}
onChange={(e) => {
setValue(e.target.value)
handleChange(rightShift(e.target.value, token?.decimals).toString())
}}
InputProps={{
endAdornment: (
<Button
variant="text"
className={classes.max}
onClick={() => {
setValue(leftShift(balance, token?.decimals).toString())
handleChange(balance)
}}>
{t.max()}
</Button>
),
disableUnderline: true,
className: classes.input,
type: 'number',
inputProps: {
pattern: NUMERIC_INPUT_REGEXP_PATTERN,
min: 0,
},
}}
/>
{value ?
<Typography
fontSize={12}
fontWeight={700}
color={
isGreaterThan(value, leftShift(balance, token?.decimals)) ?
theme.palette.maskColor.danger
: theme.palette.maskColor.warn
}>
{tips}
</Typography>
: null}
<Typography className={classes.name}>{t.popups_wallet_unlock_erc20_requested_by()}</Typography>
{transaction.formattedTransaction.popup?.spender ?
<Typography className={classes.spender} component="div">
{t.contract()}:
<Typography className={classes.spenderAddress}>
{transaction.formattedTransaction.popup?.spender}{' '}
<Link
target="_blank"
rel="noopener noreferrer"
href={EVMExplorerResolver.addressLink(
chainId,
transaction.formattedTransaction.popup.spender,
)}
className={classes.link}
style={{ color: theme.palette.maskColor.second }}>
<Icons.LinkOut size={16} />
</Link>
</Typography>
</Typography>
: null}
</Box>
<Box mt={3.75} display="flex" justifyContent="space-between" alignItems="center">
<Typography className={classes.gasFeeTitle}>{t.popups_wallet_gas_fee()}</Typography>
{transaction.computedPayload.gas && initConfig ?
<GasSettingMenu
defaultGasLimit={transaction.computedPayload.gas}
defaultGasConfig={initConfig}
onChange={onConfigChange}
onPaymentTokenChange={onPaymentTokenChange}
owner={transaction.owner}
paymentToken={paymentToken}
allowMaskAsGas={transaction.allowMaskAsGas}
/>
: null}
</Box>
</Box>
)
})