1
1
"use client" ;
2
2
3
- import { ChangeEventHandler , useCallback , useEffect , useMemo , useRef , useState } from "react" ;
4
- import data from "../data/data.json" ;
3
+ import { ChangeEventHandler , useCallback , useMemo , useRef , useState } from "react" ;
5
4
import Image from "next/image" ;
6
- import ChainSelectInput , { chainType } from "./chainSelectInput" ;
5
+ import ChainSelectInput from "./chainSelectInput" ;
7
6
import SuccessModal from "./successModal" ;
8
7
import PendingModal from "./pendingModal" ;
9
- import { formatBalance , getAssetIconSrc , isExceedingCrossChainLimit , isValidAddress , parseCross } from "@/utils" ;
8
+ import {
9
+ formatBalance ,
10
+ getAssetIconSrc ,
11
+ getAvailableSourceAsset ,
12
+ getAvailableSourceChain ,
13
+ getAvailableSourceChainOptions ,
14
+ getAvailableTargetAsset ,
15
+ getAvailableTargetChain ,
16
+ getAvailableTargetChainOptions ,
17
+ isExceedingCrossChainLimit ,
18
+ isValidAddress ,
19
+ } from "@/utils" ;
10
20
import { useTransfer } from "@/hooks" ;
11
21
import { BN , BN_ZERO , bnToBn } from "@polkadot/util" ;
12
22
import { formatUnits , parseUnits } from "viem" ;
13
23
import { WalletID } from "@/types" ;
14
24
import { useAccount , useNetwork , useSwitchNetwork } from "wagmi" ;
15
25
import notification from "@/ui/notification" ;
16
- import { supportedTokenList } from "@/config/tokens" ;
17
26
import { useTrail , animated , useSpring } from "@react-spring/web" ;
18
27
import WalletSelectionModal from "./walletSelectionModal" ;
28
+ import { assetCategories } from "@/config/asset-categories" ;
19
29
20
30
export default function AppBox ( ) {
21
31
const [ connectModal , setConnectModal ] = useState ( false ) ;
22
-
23
- const { defaultSourceChainOptions, defaultSourceAssetOptions } = parseCross ( ) ;
24
- const [ selectedAsset , setSelectedAsset ] = useState ( supportedTokenList [ 0 ] ) ;
25
- const [ allowedChain , setAllowedChain ] = useState < any > ( [ ] ) ;
26
- // const [sourceAssetOptions, setSourceAssetOptions] = useState(defaultSourceAssetOptions);
27
32
const [ successModal , setSuccessModal ] = useState < boolean > ( false ) ;
28
33
const [ receipt , setReceipt ] = useState < boolean | null > ( false ) ;
29
34
@@ -58,7 +63,10 @@ export default function AppBox() {
58
63
activeSenderAccount,
59
64
feeBalanceOnSourceChain,
60
65
sourceNativeBalance,
66
+ assetCategory,
67
+ setAssetCategory,
61
68
} = useTransfer ( ) ;
69
+
62
70
const handleCloseSuccessModal = useCallback ( ( ) => {
63
71
setSuccessModal ( false ) ;
64
72
setReceipt ( null ) ;
@@ -69,7 +77,15 @@ export default function AppBox() {
69
77
updateSourceNativeBalance ( ) ;
70
78
updateTargetNativeBalance ( ) ;
71
79
updateFeeBalanceOnSourceChain ( ) ;
72
- } , [ ] ) ;
80
+ } , [
81
+ setTransferAmount ,
82
+ updateFeeBalanceOnSourceChain ,
83
+ updateSourceAssetBalance ,
84
+ updateSourceNativeBalance ,
85
+ updateTargetAssetBalance ,
86
+ updateTargetAssetSupply ,
87
+ updateTargetNativeBalance ,
88
+ ] ) ;
73
89
74
90
const sourceChainRef = useRef ( sourceChain ) ;
75
91
const targetChainRef = useRef ( targetChain ) ;
@@ -158,8 +174,6 @@ export default function AppBox() {
158
174
return null ;
159
175
} , [ bridgeInstance , feeBalanceOnSourceChain ] ) ;
160
176
161
- // console.log("fee alert", feeAlert);
162
-
163
177
const existentialAlertOnSourceChain = useMemo ( ( ) => {
164
178
if (
165
179
sourceChain . existential &&
@@ -219,26 +233,12 @@ export default function AppBox() {
219
233
! ! existentialAlertOnSourceChain ||
220
234
! ! existentialAlertOnTargetChain ;
221
235
222
- // console.log(
223
- // "check this now",
224
- // sender?.address,
225
- // sender?.valid,
226
- // recipient?.address,
227
- // recipient?.valid,
228
- // transferAmount.input,
229
- // transferAmount.valid,
230
- // feeAlert,
231
- // existentialAlertOnSourceChain,
232
- // existentialAlertOnTargetChain,
233
- // );
234
-
235
236
const handleSend = useCallback ( async ( ) => {
236
237
if ( needSwitchNetwork ) {
237
238
switchNetwork ?.( sourceChain . id ) ;
238
239
} else if ( bridgeInstance && recipient ) {
239
240
const callback = {
240
241
successCb : ( receipt : any ) => {
241
- console . log ( "receipt" , receipt ) ;
242
242
setReceipt ( receipt ) ;
243
243
setBusy ( false ) ;
244
244
setSuccessModal ( true ) ;
@@ -266,69 +266,14 @@ export default function AppBox() {
266
266
needSwitchNetwork ,
267
267
recipient ,
268
268
sender ?. address ,
269
- setTransferAmount ,
270
269
sourceChain . id ,
271
270
switchNetwork ,
272
271
transfer ,
273
272
transferAmount . amount ,
274
273
transferAmount . input ,
275
- updateFeeBalanceOnSourceChain ,
276
- updateSourceAssetBalance ,
277
- updateTargetAssetBalance ,
278
274
updateTargetAssetSupply ,
279
- updateSourceNativeBalance ,
280
- updateTargetNativeBalance ,
281
275
] ) ;
282
276
283
- useEffect ( ( ) => {
284
- console . log ( "defaultSourceAssetOptions" , sourceChain . assets ) ;
285
- let sourceChainOptions : any = [ ] ;
286
- for ( const item of selectedAsset . allowedSource ) {
287
- for ( const chain of defaultSourceChainOptions ) {
288
- if ( chain . name === item ) {
289
- sourceChainOptions . push ( chain ) ;
290
- }
291
- }
292
- }
293
- let selectedSourceAsset ;
294
- for ( const item of sourceChainOptions [ 0 ] . assets ) {
295
- if ( selectedAsset . icon === item . icon ) {
296
- selectedSourceAsset = item ;
297
- }
298
- }
299
- setAllowedChain ( [ ...sourceChainOptions ] ) ;
300
- setSourceChain ( sourceChainOptions [ 0 ] ) ;
301
- setSourceAsset ( selectedSourceAsset ) ;
302
- setTargetChain ( sourceChainOptions [ 1 ] ) ;
303
- } , [ selectedAsset ] ) ;
304
-
305
- useEffect ( ( ) => {
306
- console . log ( allowedChain ) ;
307
- if ( allowedChain . length > 0 ) {
308
- if ( sourceChain . name === allowedChain [ 0 ] . name && targetChain . name === allowedChain [ 0 ] . name ) {
309
- setTargetChain ( allowedChain [ 1 ] ) ;
310
- } else if ( sourceChain . name === allowedChain [ 1 ] . name && targetChain . name === allowedChain [ 1 ] . name ) {
311
- setTargetChain ( allowedChain [ 0 ] ) ;
312
- }
313
- }
314
- for ( const asset of sourceChain . assets ) {
315
- if ( selectedAsset . name === asset . name ) setSourceAsset ( asset ) ;
316
- }
317
- } , [ sourceChain ] ) ;
318
-
319
- useEffect ( ( ) => {
320
- if ( allowedChain . length > 0 ) {
321
- if ( targetChain . name === allowedChain [ 0 ] . name && sourceChain . name == allowedChain [ 0 ] . name ) {
322
- setSourceChain ( allowedChain [ 1 ] ) ;
323
- } else if ( targetChain . name === allowedChain [ 1 ] . name && sourceChain . name === allowedChain [ 1 ] . name ) {
324
- setSourceChain ( allowedChain [ 0 ] ) ;
325
- }
326
- }
327
- for ( const asset of targetChain . assets ) {
328
- if ( selectedAsset . name === asset . name ) setTargetAsset ( asset ) ;
329
- }
330
- } , [ targetChain ] ) ;
331
-
332
277
const trails = useTrail ( 5 , {
333
278
from : { transform : "translateX(-100%)" , opacity : 0 } ,
334
279
to : { opacity : 1 , transform : "translateX(0)" } ,
@@ -339,8 +284,6 @@ export default function AppBox() {
339
284
to : { opacity : 1 , transform : "translateY(0)" } ,
340
285
} ) ;
341
286
342
- // console.log("check this.................", sender, needSwitchNetwork, disabledSend);
343
-
344
287
return (
345
288
< >
346
289
< animated . section
@@ -355,16 +298,24 @@ export default function AppBox() {
355
298
< p className = "text-[12px] leading-[15.22px] text-[#12161980]" > Token</ p >
356
299
</ div >
357
300
< div className = "flex items-center gap-[10px]" >
358
- { supportedTokenList . map ( ( item : any ) => (
301
+ { assetCategories . map ( ( item ) => (
359
302
< div
360
303
className = "flex items-center gap-[10px] duration-500"
361
304
key = { item . name }
362
305
style = { {
363
- maxWidth : selectedAsset . name === item . name ? "100px" : "30px" ,
364
- transitionDelay : selectedAsset . name === item . name ? "0.4s" : "0s" ,
306
+ maxWidth : assetCategory === item . category ? "100px" : "30px" ,
307
+ transitionDelay : assetCategory === item . category ? "0.4s" : "0s" ,
365
308
} }
366
309
onClick = { ( ) => {
367
- setSelectedAsset ( item ) ;
310
+ setAssetCategory ( item . category ) ;
311
+ const _sourceChain = getAvailableSourceChain ( getAvailableSourceChainOptions ( item . category ) ) ;
312
+ const _sourceAsset = getAvailableSourceAsset ( _sourceChain , item . category ) ;
313
+ const _targetChain = getAvailableTargetChain ( getAvailableTargetChainOptions ( _sourceAsset ) ) ;
314
+ const _targetAsset = getAvailableTargetAsset ( _targetChain , item . category ) ;
315
+ setSourceChain ( _sourceChain ) ;
316
+ setSourceAsset ( _sourceAsset ) ;
317
+ setTargetChain ( _targetChain ) ;
318
+ setTargetAsset ( _targetAsset ) ;
368
319
} }
369
320
>
370
321
< Image
@@ -385,7 +336,19 @@ export default function AppBox() {
385
336
>
386
337
< div className = "flex h-[30px] items-center justify-between" >
387
338
< p className = "text-[12px] leading-[15.22px] text-[#12161980]" > Sender</ p >
388
- < ChainSelectInput options = { allowedChain } who = "sender" />
339
+ < ChainSelectInput
340
+ value = { sourceChain }
341
+ options = { getAvailableSourceChainOptions ( assetCategory ) }
342
+ onSelect = { ( chain ) => {
343
+ setSourceChain ( chain ) ;
344
+ const _sourceAsset = getAvailableSourceAsset ( chain , assetCategory ) ;
345
+ const _targetChain = getAvailableTargetChain ( getAvailableTargetChainOptions ( _sourceAsset ) ) ;
346
+ const _targetAsset = getAvailableTargetAsset ( _targetChain , assetCategory ) ;
347
+ setSourceAsset ( _sourceAsset ) ;
348
+ setTargetChain ( _targetChain ) ;
349
+ setTargetAsset ( _targetAsset ) ;
350
+ } }
351
+ />
389
352
</ div >
390
353
{ sender ? (
391
354
< input
@@ -413,7 +376,15 @@ export default function AppBox() {
413
376
>
414
377
< div className = "flex h-[30px] items-center justify-between" >
415
378
< p className = "text-[12px] leading-[15.22px] text-[#12161980]" > Recipient</ p >
416
- < ChainSelectInput options = { allowedChain } who = "target" />
379
+ < ChainSelectInput
380
+ value = { targetChain }
381
+ options = { getAvailableTargetChainOptions ( sourceAsset ) }
382
+ onSelect = { ( chain ) => {
383
+ setTargetChain ( chain ) ;
384
+ const _targetAsset = getAvailableTargetAsset ( chain , assetCategory ) ;
385
+ setTargetAsset ( _targetAsset ) ;
386
+ } }
387
+ />
417
388
</ div >
418
389
< input
419
390
type = "text"
0 commit comments