1
1
import { useAccount , useChains , useDeposit , useWithdraw } from '@orderly.network/hooks' ;
2
- import { API } from '@orderly.network/types' ;
2
+ import { API , ChainNamespace } from '@orderly.network/types' ;
3
3
import { Cross1Icon } from '@radix-ui/react-icons' ;
4
4
import { Button , Dialog , Tabs } from '@radix-ui/themes' ;
5
5
import { useNotifications , useSetChain } from '@web3-onboard/react' ;
6
6
import { FixedNumber } from 'ethers' ;
7
7
import { FC , useEffect , useMemo , useState } from 'react' ;
8
+ import { match } from 'ts-pattern' ;
8
9
9
10
import { PendingButton , TokenInput } from '~/components' ;
10
11
import { useIsTestnet } from '~/hooks' ;
11
- import { supportedChainIds } from '~/utils' ;
12
+ import { supportedEvmChainIds , supportedSolanaChainIds } from '~/utils' ;
12
13
13
14
export const OrderlyDeposit : FC < {
14
15
walletBalance : FixedNumber ;
@@ -25,23 +26,25 @@ export const OrderlyDeposit: FC<{
25
26
const [ _ , customNotification ] = useNotifications ( ) ;
26
27
27
28
const [ isTestnet ] = useIsTestnet ( ) ;
28
- const { account } = useAccount ( ) ;
29
+ const { account, state } = useAccount ( ) ;
29
30
const [ chains ] = useChains ( isTestnet ? 'testnet' : 'mainnet' , {
30
- filter : ( item : API . Chain ) => supportedChainIds . includes ( item . network_infos ?. chain_id )
31
+ filter : ( item : API . Chain ) =>
32
+ supportedEvmChainIds . includes ( item . network_infos ?. chain_id ) ||
33
+ supportedSolanaChainIds . includes ( item . network_infos ?. chain_id )
31
34
} ) ;
32
- const [ { connectedChain } ] = useSetChain ( ) ;
35
+ const [ { connectedChain : connectedEvmChain } ] = useSetChain ( ) ;
33
36
const token = useMemo ( ( ) => {
34
37
return Array . isArray ( chains )
35
38
? chains
36
- . find ( ( chain ) => chain . network_infos . chain_id === Number ( connectedChain ?. id ) )
39
+ . find ( ( chain ) => chain . network_infos . chain_id === Number ( connectedEvmChain ?. id ) )
37
40
?. token_infos . find ( ( t ) => t . symbol === 'USDC' )
38
41
: undefined ;
39
- } , [ chains , connectedChain ] ) ;
42
+ } , [ chains , connectedEvmChain ] ) ;
40
43
const deposit = useDeposit ( {
41
44
address : token ?. address ,
42
45
decimals : token ?. decimals ,
43
46
srcToken : token ?. symbol ,
44
- srcChainId : Number ( connectedChain ?. id )
47
+ srcChainId : Number ( connectedEvmChain ?. id )
45
48
} ) ;
46
49
47
50
useEffect ( ( ) => {
@@ -221,27 +224,51 @@ export const OrderlyDeposit: FC<{
221
224
: 'Withdraw' }
222
225
</ PendingButton >
223
226
224
- { isTestnet && (
227
+ { isTestnet && state . chainNamespace != null && (
225
228
< PendingButton
226
229
disabled = { mintedTestUSDC }
227
230
onClick = { async ( ) => {
231
+ if ( state . chainNamespace == null ) return ;
228
232
const { update } = customNotification ( {
229
233
eventCode : 'mint' ,
230
234
type : 'pending' ,
231
235
message : 'Minting 1k USDC on testnet...'
232
236
} ) ;
233
237
try {
234
- const res = await fetch ( 'https://testnet-operator-evm.orderly.org/v1/faucet/usdc' , {
235
- method : 'POST' ,
236
- headers : {
237
- 'Content-Type' : 'application/json'
238
- } ,
239
- body : JSON . stringify ( {
240
- broker_id : 'orderly' ,
241
- chain_id : String ( Number ( connectedChain ?. id ) ) ,
242
- user_address : account . address
243
- } )
244
- } ) ;
238
+ const chainNamespace = state . chainNamespace ;
239
+ const res = await fetch (
240
+ match ( chainNamespace )
241
+ . with (
242
+ ChainNamespace . evm ,
243
+ ( ) => 'https://testnet-operator-evm.orderly.org/v1/faucet/usdc'
244
+ )
245
+ . with (
246
+ ChainNamespace . solana ,
247
+ ( ) => 'https://testnet-operator-sol.orderly.org/v1/faucet/usdc'
248
+ )
249
+ . exhaustive ( ) ,
250
+ {
251
+ method : 'POST' ,
252
+ headers : {
253
+ 'Content-Type' : 'application/json'
254
+ } ,
255
+ body : match ( chainNamespace )
256
+ . with ( ChainNamespace . evm , ( ) =>
257
+ JSON . stringify ( {
258
+ broker_id : import . meta. env . VITE_BROKER_ID ,
259
+ chain_id : String ( Number ( account . chainId ) ) ,
260
+ user_address : account . address
261
+ } )
262
+ )
263
+ . with ( ChainNamespace . solana , ( ) =>
264
+ JSON . stringify ( {
265
+ broker_id : import . meta. env . VITE_BROKER_ID ,
266
+ user_address : account . address
267
+ } )
268
+ )
269
+ . exhaustive ( )
270
+ }
271
+ ) ;
245
272
if ( ! res . ok ) {
246
273
throw new Error ( res . status === 429 ? 'Too many requests' : res . statusText ) ;
247
274
}
0 commit comments