1
1
import { useWalletClient , useWallets } from "@/app/hooks" ;
2
- import {
3
- DynamicConnectButton ,
4
- useDynamicContext ,
5
- } from "@dynamic-labs/sdk-react-core" ;
2
+ import { DynamicConnectButton , useDynamicContext } from "@dynamic-labs/sdk-react-core" ;
6
3
import classNames from "classnames" ;
7
4
import { useEffect , useMemo , useState } from "react" ;
8
5
import {
@@ -69,58 +66,42 @@ export function Redeem() {
69
66
///////////////////////
70
67
// State
71
68
///////////////////////
72
- const [ walletClient , setWalletClient ] = useState < WalletClient <
73
- Transport ,
74
- Chain ,
75
- Account
76
- > | null > ( null ) ;
69
+ const [ walletClient , setWalletClient ] = useState < WalletClient < Transport , Chain , Account > | null > ( null ) ;
77
70
const [ redeemAmount , setRedeemAmount ] = useState < string > ( "" ) ;
78
71
const [ receiveAsset , setReceiveAsset ] = useState < string > ( tokenAddresses [ 0 ] ) ;
79
72
const [ assetPerTethRate , setAssetPerTethRate ] = useState < string > ( "" ) ;
80
73
const [ ethPerAssetRate , setEthPerAssetRate ] = useState ( "" ) ;
81
74
const [ ethPerTethRate , setEthPerTethRate ] = useState ( "" ) ;
82
75
const [ depositPending , setDepositPending ] = useState < boolean > ( false ) ;
83
- const [ tokenBalanceAsBigInt , setTokenBalanceAsBigInt ] = useState < bigint > (
84
- BigInt ( 1 ) ,
85
- ) ;
76
+ const [ tokenBalanceAsBigInt , setTokenBalanceAsBigInt ] = useState < bigint > ( BigInt ( 1 ) ) ;
86
77
const [ isLoadingTokenBalance , setIsLoadingTokenBalance ] = useState ( false ) ;
87
78
const [ isModalOpen , setIsModalOpen ] = useState ( false ) ;
88
79
const [ currentTx , setCurrentTx ] = useState < any > ( null ) ;
89
80
const [ depositTxHash , setDepositTxHash ] = useState < string > ( "" ) ;
90
81
const [ tethBalance , setTethBalance ] = useState < string > ( "" ) ;
91
82
const [ tethBalanceLoading , setTethBalanceLoading ] = useState ( false ) ;
92
83
const [ ethPrice , setEthPrice ] = useState < string > ( "" ) ;
93
- const [ sourceChain , setSourceChain ] = useState < SelectOption | undefined > (
94
- chainOptions [ 0 ] ,
95
- ) ;
84
+ const [ sourceChain , setSourceChain ] = useState < SelectOption | undefined > ( chainOptions [ 0 ] ) ;
96
85
97
86
///////////////////////
98
87
// Derived values
99
88
///////////////////////
100
89
const publicClient = createPublicClient ( {
101
90
chain : mainnet ,
102
- transport : http (
103
- "https://empty-responsive-patron.quiknode.pro/91dfa8475605dcdec9afdc8273578c9f349774a1/" ,
104
- ) ,
91
+ transport : http ( "https://empty-responsive-patron.quiknode.pro/91dfa8475605dcdec9afdc8273578c9f349774a1/" ) ,
105
92
cacheTime : 0 ,
106
93
} ) ;
107
94
108
- const atomicPrice =
109
- ( BigInt ( assetPerTethRate ) *
110
- ( BigInt ( 1e18 ) - parseUnits ( slippage . toString ( ) , 18 ) ) ) /
111
- BigInt ( 1e18 ) ;
95
+ const atomicPrice = ( BigInt ( assetPerTethRate ) * ( BigInt ( 1e18 ) - parseUnits ( slippage . toString ( ) , 18 ) ) ) / BigInt ( 1e18 ) ;
112
96
113
97
const formattedTokenBalance = formatUnits ( BigInt ( tethBalance ) , 18 ) ;
114
98
const atomicPriceAsBigInt = BigInt ( atomicPrice ) ;
115
99
const redeemAmountAsBigInt = BigInt ( parseUnits ( redeemAmount , 18 ) ) ;
116
100
117
101
// Withdraw fee
118
- const withdrawFeeInTeth =
119
- ( redeemAmountAsBigInt * parseUnits ( slippage . toString ( ) , 18 ) ) / BigInt ( 1e18 ) ;
120
- const withdrawFeeInEth =
121
- ( withdrawFeeInTeth * BigInt ( ethPerTethRate ) ) / BigInt ( 1e18 ) ;
122
- const withdrawFeeInUsdAsBigInt =
123
- ( withdrawFeeInEth * BigInt ( ethPrice ) ) / BigInt ( 1e8 ) ;
102
+ const withdrawFeeInTeth = ( redeemAmountAsBigInt * parseUnits ( slippage . toString ( ) , 18 ) ) / BigInt ( 1e18 ) ;
103
+ const withdrawFeeInEth = ( withdrawFeeInTeth * BigInt ( ethPerTethRate ) ) / BigInt ( 1e18 ) ;
104
+ const withdrawFeeInUsdAsBigInt = ( withdrawFeeInEth * BigInt ( ethPrice ) ) / BigInt ( 1e8 ) ;
124
105
const withdrawFeeInUsd = Number ( formatUnits ( withdrawFeeInUsdAsBigInt , 18 ) ) ;
125
106
const formattedWithdrawFeeInUsd =
126
107
withdrawFeeInUsd > 0 && withdrawFeeInUsd < 0.01
@@ -131,8 +112,7 @@ export function Redeem() {
131
112
} ) . format ( withdrawFeeInUsd ) } `;
132
113
133
114
// Bridge fee
134
- const bridgeFeeInUsdAsBigInt =
135
- ( interchainTransferFee * BigInt ( ethPrice ) ) / BigInt ( 1e8 ) ;
115
+ const bridgeFeeInUsdAsBigInt = ( interchainTransferFee * BigInt ( ethPrice ) ) / BigInt ( 1e8 ) ;
136
116
const bridgeFeeInUsd = Number ( formatUnits ( bridgeFeeInUsdAsBigInt , 18 ) ) ;
137
117
const formattedBridgeFeeInUsd =
138
118
bridgeFeeInUsd > 0 && bridgeFeeInUsd < 0.01
@@ -144,8 +124,7 @@ export function Redeem() {
144
124
145
125
// Total fees
146
126
const totalFeesInEth = withdrawFeeInEth + interchainTransferFee ;
147
- const totalFeesInUsdAsBigInt =
148
- ( totalFeesInEth * BigInt ( ethPrice ) ) / BigInt ( 1e8 ) ;
127
+ const totalFeesInUsdAsBigInt = ( totalFeesInEth * BigInt ( ethPrice ) ) / BigInt ( 1e8 ) ;
149
128
const totalFeesInUsd = Number ( formatUnits ( totalFeesInUsdAsBigInt , 18 ) ) ;
150
129
const formattedTotalFeesInUsd =
151
130
totalFeesInUsd > 0 && totalFeesInUsd < 0.01
@@ -158,23 +137,16 @@ export function Redeem() {
158
137
const isOverBalance = BigInt ( tethBalance ) < redeemAmountAsBigInt ;
159
138
160
139
const isMintDisabled =
161
- depositPending ||
162
- ! redeemAmount ||
163
- ! receiveAsset ||
164
- ! evmWallet ||
165
- isOverBalance ||
166
- Number ( redeemAmount ) === 0 ;
140
+ depositPending || ! redeemAmount || ! receiveAsset || ! evmWallet || isOverBalance || Number ( redeemAmount ) === 0 ;
167
141
168
142
const evmAddress = evmWallet ?. address as `0x${string } ` | undefined ;
169
143
const svmAddress = solWallet ?. address as `0x${string } ` | undefined ;
170
144
171
145
const ethPriceAsBigInt = ethPrice ? BigInt ( ethPrice ) : BigInt ( 0 ) ;
172
146
173
147
// Redeem amount
174
- const redeemAmountInEth =
175
- ( redeemAmountAsBigInt * BigInt ( ethPerTethRate ) ) / BigInt ( 1e18 ) ;
176
- const redeemAmountInUsd =
177
- ( redeemAmountInEth * ethPriceAsBigInt ) / BigInt ( 1e8 ) ;
148
+ const redeemAmountInEth = ( redeemAmountAsBigInt * BigInt ( ethPerTethRate ) ) / BigInt ( 1e18 ) ;
149
+ const redeemAmountInUsd = ( redeemAmountInEth * ethPriceAsBigInt ) / BigInt ( 1e8 ) ;
178
150
const redeemAmountInUsdFormatted = Number ( formatUnits ( redeemAmountInUsd , 18 ) ) ;
179
151
const formattedRedeemAmountInUsd =
180
152
redeemAmountInUsdFormatted > 0 && redeemAmountInUsdFormatted < 0.01
@@ -195,11 +167,8 @@ export function Redeem() {
195
167
? ( receiveAmountAsBigInt * BigInt ( ethPerAssetRate ) ) / BigInt ( 1e18 )
196
168
: BigInt ( 0 ) ;
197
169
198
- const receiveAmountInUsd =
199
- ( receiveAmountInEth * ethPriceAsBigInt ) / BigInt ( 1e8 ) ;
200
- const receiveAmountInUsdFormatted = Number (
201
- formatUnits ( receiveAmountInUsd , 18 ) ,
202
- ) ;
170
+ const receiveAmountInUsd = ( receiveAmountInEth * ethPriceAsBigInt ) / BigInt ( 1e8 ) ;
171
+ const receiveAmountInUsdFormatted = Number ( formatUnits ( receiveAmountInUsd , 18 ) ) ;
203
172
const formattedReceiveAmountInUsd =
204
173
receiveAmountInUsdFormatted > 0 && receiveAmountInUsdFormatted < 0.01
205
174
? "<$0.01"
@@ -210,9 +179,7 @@ export function Redeem() {
210
179
211
180
// Memoized because it iterates over an array
212
181
const { depositAssetLabel, depositAssetIcon } = useMemo ( ( ) => {
213
- const tokenOption = tokenOptions . find (
214
- ( token ) => token . value === receiveAsset ,
215
- ) ;
182
+ const tokenOption = tokenOptions . find ( ( token ) => token . value === receiveAsset ) ;
216
183
return {
217
184
depositAssetLabel : tokenOption ?. label ,
218
185
depositAssetIcon : tokenOption ?. imageSrc ,
@@ -236,12 +203,7 @@ export function Redeem() {
236
203
link : `https://etherscan.io/tx/${ depositTxHash } ` ,
237
204
} ,
238
205
] ;
239
- } , [
240
- atomicRequestApprovalState ,
241
- atomicRequestState ,
242
- depositTxHash ,
243
- tokenTransferState ,
244
- ] ) ;
206
+ } , [ atomicRequestApprovalState , atomicRequestState , depositTxHash , tokenTransferState ] ) ;
245
207
246
208
///////////////////////
247
209
// Use effects
@@ -250,9 +212,7 @@ export function Redeem() {
250
212
useEffect ( ( ) => {
251
213
let lWalletClient =
252
214
//@ts -ignore
253
- evmWallet ?. connector . getWalletClient <
254
- WalletClient < Transport , Chain , Account >
255
- > ( ) ;
215
+ evmWallet ?. connector . getWalletClient < WalletClient < Transport , Chain , Account > > ( ) ;
256
216
lWalletClient && ( lWalletClient . cacheTime = 0 ) ;
257
217
setWalletClient ( lWalletClient ?? null ) ;
258
218
} , [ evmWallet ?. connector ] ) ;
@@ -262,20 +222,10 @@ export function Redeem() {
262
222
async function getSvmBalance ( ) {
263
223
try {
264
224
setTethBalanceLoading ( true ) ;
265
- if ( tethBalance != "" ) {
266
- return ;
267
- }
268
225
if ( sourceChain ?. value === "eclipse" && svmAddress ) {
269
- const balance = await getSolanaBalance (
270
- svmAddress ,
271
- tethSvmTokenAddress ,
272
- ) ;
226
+ const balance = await getSolanaBalance ( svmAddress , tethSvmTokenAddress ) ;
273
227
setTethBalance ( balance . toString ( ) ) ;
274
- } else if (
275
- sourceChain ?. value === "ethereum" &&
276
- evmAddress &&
277
- publicClient
278
- ) {
228
+ } else if ( sourceChain ?. value === "ethereum" && evmAddress && publicClient ) {
279
229
const balance = await balanceOf ( {
280
230
tokenAddress : tethEvmTokenAddress ,
281
231
userAddress : evmAddress ,
@@ -311,14 +261,11 @@ export function Redeem() {
311
261
if ( asset === "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" ) {
312
262
_ethPerAssetRate = BigInt ( 1e18 ) ;
313
263
} else {
314
- _ethPerAssetRate = await getRate (
315
- { tokenAddress : asset } ,
316
- { publicClient } ,
317
- ) ;
264
+ _ethPerAssetRate = await getRate ( { tokenAddress : asset } , { publicClient } ) ;
318
265
}
319
266
const _ethPerTethRate = await getRateInQuote (
320
267
{ quote : "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" } , // WETH
321
- { publicClient } ,
268
+ { publicClient }
322
269
) ;
323
270
const _ethPrice = await latestRoundData ( { publicClient } ) ;
324
271
@@ -348,8 +295,7 @@ export function Redeem() {
348
295
useEffect ( ( ) => {
349
296
async function getTokenBalance ( ) {
350
297
try {
351
- if ( ! publicClient || ! evmWallet || tokenBalanceAsBigInt != BigInt ( 1 ) )
352
- return ;
298
+ if ( ! publicClient || ! evmWallet || tokenBalanceAsBigInt != BigInt ( 1 ) ) return ;
353
299
setIsLoadingTokenBalance ( true ) ;
354
300
const _tokenBalanceAsBigInt = await balanceOf ( {
355
301
tokenAddress : receiveAsset as `0x${string } `,
@@ -408,9 +354,7 @@ export function Redeem() {
408
354
setIsModalOpen ( true ) ;
409
355
410
356
// Make the swap (this step takes up to 24 hours)
411
- const deadlineInSec = BigInt (
412
- Math . floor ( Date . now ( ) / 1000 ) + deadlineDaysFromNow * 24 * 60 * 60 ,
413
- ) ;
357
+ const deadlineInSec = BigInt ( Math . floor ( Date . now ( ) / 1000 ) + deadlineDaysFromNow * 24 * 60 * 60 ) ;
414
358
const offerAmount = parseUnits ( redeemAmount , 18 ) ;
415
359
416
360
if ( ! evmAddress ) throw new Error ( "No EVM address found" ) ;
@@ -443,15 +387,10 @@ export function Redeem() {
443
387
offerAddress : tethEvmTokenAddress ,
444
388
wantAddress : receiveAsset as `0x${string } `,
445
389
} ,
446
- { publicClient } ,
390
+ { publicClient }
447
391
) ;
448
- const {
449
- atomicPrice : pendingAtomicPrice ,
450
- offerAmount : pendingOfferAmount ,
451
- } = pendingAtomicRequest ;
452
- const atomicRequestAlreadyExists =
453
- pendingAtomicPrice === atomicPrice &&
454
- pendingOfferAmount === offerAmount ;
392
+ const { atomicPrice : pendingAtomicPrice , offerAmount : pendingOfferAmount } = pendingAtomicRequest ;
393
+ const atomicRequestAlreadyExists = pendingAtomicPrice === atomicPrice && pendingOfferAmount === offerAmount ;
455
394
456
395
if ( ! atomicRequestAlreadyExists ) {
457
396
const txHash = await updateAtomicRequest (
@@ -465,7 +404,7 @@ export function Redeem() {
465
404
inSolve : false ,
466
405
} ,
467
406
} ,
468
- { publicClient, walletClient } ,
407
+ { publicClient, walletClient }
469
408
) ;
470
409
} else {
471
410
setAtomicRequestState ( StepStatus . LOADING ) ;
@@ -482,11 +421,7 @@ export function Redeem() {
482
421
483
422
return (
484
423
< >
485
- < div
486
- className = {
487
- isModalOpen ? "mint-status-overlay active" : "mint-status-overlay"
488
- }
489
- > </ div >
424
+ < div className = { isModalOpen ? "mint-status-overlay active" : "mint-status-overlay" } > </ div >
490
425
{ isModalOpen && (
491
426
< MintTransactionDetails
492
427
fromDeposit = { true }
@@ -532,9 +467,7 @@ export function Redeem() {
532
467
userAddress = { evmAddress }
533
468
inputValue = { formattedReceiveAmount }
534
469
disabled = { true }
535
- depositAsset = { tokenOptions . find (
536
- ( token ) => token . value === receiveAsset ,
537
- ) }
470
+ depositAsset = { tokenOptions . find ( ( token ) => token . value === receiveAsset ) }
538
471
tokenBalance = { tokenBalanceAsBigInt }
539
472
usdValue = { formattedReceiveAmountInUsd }
540
473
handleDisconnect = { ( ) => evmWallet && handleUnlinkWallet ( evmWallet . id ) }
@@ -568,12 +501,7 @@ export function Redeem() {
568
501
buttonClassName = "wallet-connect-button w-full"
569
502
buttonContainerClassName = "submit-button connect-btn"
570
503
>
571
- < span style = { { width : "100%" } } >
572
- { " " }
573
- { ! evmAddress && ! svmAddress
574
- ? "Connect Wallets"
575
- : "Connect Wallet" }
576
- </ span >
504
+ < span style = { { width : "100%" } } > { ! evmAddress && ! svmAddress ? "Connect Wallets" : "Connect Wallet" } </ span >
577
505
</ DynamicConnectButton >
578
506
) }
579
507
</ div >
0 commit comments