@@ -13,6 +13,7 @@ import {
1313 ApiClientTonApi ,
1414 ApiClientToncenter ,
1515 createTonCenterStreamingProvider ,
16+ createTonApiStreamingProvider ,
1617} from '@ton/appkit' ;
1718import { createDeDustProvider } from '@ton/appkit/swap/dedust' ;
1819import { createOmnistonProvider } from '@ton/appkit/swap/omniston' ;
@@ -22,27 +23,49 @@ import { createDecentProvider } from '@ton/appkit/crypto-onramp/decent';
2223import { createTonApiGaslessProvider } from '@ton/appkit/gasless/tonapi' ;
2324
2425import {
26+ ENV_TON_API_PROVIDER ,
2527 ENV_TON_API_KEY_TESTNET ,
2628 ENV_TON_API_KEY_MAINNET ,
29+ ENV_TON_API_KEY_TETRA ,
2730 ENV_DECENT_API_KEY ,
2831 ENV_TONCONNECT_MANIFEST_URL ,
2932} from '@/core/configs/env' ;
3033
31- const mainnetApiClient = new ApiClientToncenter ( {
32- network : Network . mainnet ( ) ,
33- apiKey : ENV_TON_API_KEY_MAINNET ,
34- } ) ;
34+ const useTonApi = ENV_TON_API_PROVIDER === 'tonapi' ;
3535
36- const testnetApiClient = new ApiClientToncenter ( {
37- network : Network . testnet ( ) ,
38- apiKey : ENV_TON_API_KEY_TESTNET ,
39- } ) ;
36+ // Switch the underlying API client between Toncenter and TonAPI based on the
37+ // configured provider. When 'tonapi' is selected the keys below are treated as
38+ // TonAPI keys; otherwise they are Toncenter keys.
39+ const createApiClient = ( network : Network , apiKey ?: string ) =>
40+ useTonApi ? new ApiClientTonApi ( { network, apiKey } ) : new ApiClientToncenter ( { network, apiKey } ) ;
41+
42+ const mainnetApiClient = createApiClient ( Network . mainnet ( ) , ENV_TON_API_KEY_MAINNET ) ;
4043
44+ const testnetApiClient = createApiClient ( Network . testnet ( ) , ENV_TON_API_KEY_TESTNET ) ;
45+
46+ // Tetra is a TonAPI-only network, so it always uses the TonAPI client.
4147const tetraApiClient = new ApiClientTonApi ( {
4248 network : Network . tetra ( ) ,
4349 endpoint : 'https://tetra.tonapi.io' ,
50+ apiKey : ENV_TON_API_KEY_TETRA ,
4451} ) ;
4552
53+ // Match the streaming provider to the configured API provider.
54+ const createStreamingProvider = useTonApi ? createTonApiStreamingProvider : createTonCenterStreamingProvider ;
55+
56+ // Pass the keys to the gasless relayer only when TonAPI is the configured
57+ // provider — otherwise they are Toncenter keys and don't apply. Without a key
58+ // it falls back to the public TonAPI endpoints.
59+ const gaslessConfig =
60+ useTonApi && ( ENV_TON_API_KEY_MAINNET || ENV_TON_API_KEY_TESTNET )
61+ ? {
62+ chains : {
63+ [ Network . mainnet ( ) . chainId ] : { apiKey : ENV_TON_API_KEY_MAINNET } ,
64+ [ Network . testnet ( ) . chainId ] : { apiKey : ENV_TON_API_KEY_TESTNET } ,
65+ } ,
66+ }
67+ : undefined ;
68+
4669export const appKit = new AppKit ( {
4770 networks : {
4871 [ Network . mainnet ( ) . chainId ] : { apiClient : mainnetApiClient } ,
@@ -62,8 +85,8 @@ export const appKit = new AppKit({
6285 createTonstakersProvider ( ) ,
6386 createLayerswapProvider ( ) ,
6487 createDecentProvider ( { apiKey : ENV_DECENT_API_KEY } ) ,
65- createTonCenterStreamingProvider ( { network : Network . mainnet ( ) , apiKey : ENV_TON_API_KEY_MAINNET } ) ,
66- createTonCenterStreamingProvider ( { network : Network . testnet ( ) , apiKey : ENV_TON_API_KEY_TESTNET } ) ,
67- createTonApiGaslessProvider ( ) ,
88+ createStreamingProvider ( { network : Network . mainnet ( ) , apiKey : ENV_TON_API_KEY_MAINNET } ) ,
89+ createStreamingProvider ( { network : Network . testnet ( ) , apiKey : ENV_TON_API_KEY_TESTNET } ) ,
90+ createTonApiGaslessProvider ( gaslessConfig ) ,
6891 ] ,
6992} ) ;
0 commit comments