From 156bbef80383f74707a92df6bbd340555a922b6f Mon Sep 17 00:00:00 2001 From: VK Date: Thu, 18 Jun 2026 11:54:58 +0400 Subject: [PATCH] feat(appkit-minter): add env variable to select between toncenter and tonapi --- .../appkit-minter/src/core/configs/app-kit.ts | 45 ++++++++++++++----- apps/appkit-minter/src/core/configs/env.ts | 2 + 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/apps/appkit-minter/src/core/configs/app-kit.ts b/apps/appkit-minter/src/core/configs/app-kit.ts index fe58469b1..0bb2c00fb 100644 --- a/apps/appkit-minter/src/core/configs/app-kit.ts +++ b/apps/appkit-minter/src/core/configs/app-kit.ts @@ -13,6 +13,7 @@ import { ApiClientTonApi, ApiClientToncenter, createTonCenterStreamingProvider, + createTonApiStreamingProvider, } from '@ton/appkit'; import { createDeDustProvider } from '@ton/appkit/swap/dedust'; import { createOmnistonProvider } from '@ton/appkit/swap/omniston'; @@ -22,27 +23,49 @@ import { createDecentProvider } from '@ton/appkit/crypto-onramp/decent'; import { createTonApiGaslessProvider } from '@ton/appkit/gasless/tonapi'; import { + ENV_TON_API_PROVIDER, ENV_TON_API_KEY_TESTNET, ENV_TON_API_KEY_MAINNET, + ENV_TON_API_KEY_TETRA, ENV_DECENT_API_KEY, ENV_TONCONNECT_MANIFEST_URL, } from '@/core/configs/env'; -const mainnetApiClient = new ApiClientToncenter({ - network: Network.mainnet(), - apiKey: ENV_TON_API_KEY_MAINNET, -}); +const useTonApi = ENV_TON_API_PROVIDER === 'tonapi'; -const testnetApiClient = new ApiClientToncenter({ - network: Network.testnet(), - apiKey: ENV_TON_API_KEY_TESTNET, -}); +// Switch the underlying API client between Toncenter and TonAPI based on the +// configured provider. When 'tonapi' is selected the keys below are treated as +// TonAPI keys; otherwise they are Toncenter keys. +const createApiClient = (network: Network, apiKey?: string) => + useTonApi ? new ApiClientTonApi({ network, apiKey }) : new ApiClientToncenter({ network, apiKey }); + +const mainnetApiClient = createApiClient(Network.mainnet(), ENV_TON_API_KEY_MAINNET); +const testnetApiClient = createApiClient(Network.testnet(), ENV_TON_API_KEY_TESTNET); + +// Tetra is a TonAPI-only network, so it always uses the TonAPI client. const tetraApiClient = new ApiClientTonApi({ network: Network.tetra(), endpoint: 'https://tetra.tonapi.io', + apiKey: ENV_TON_API_KEY_TETRA, }); +// Match the streaming provider to the configured API provider. +const createStreamingProvider = useTonApi ? createTonApiStreamingProvider : createTonCenterStreamingProvider; + +// Pass the keys to the gasless relayer only when TonAPI is the configured +// provider — otherwise they are Toncenter keys and don't apply. Without a key +// it falls back to the public TonAPI endpoints. +const gaslessConfig = + useTonApi && (ENV_TON_API_KEY_MAINNET || ENV_TON_API_KEY_TESTNET) + ? { + chains: { + [Network.mainnet().chainId]: { apiKey: ENV_TON_API_KEY_MAINNET }, + [Network.testnet().chainId]: { apiKey: ENV_TON_API_KEY_TESTNET }, + }, + } + : undefined; + export const appKit = new AppKit({ networks: { [Network.mainnet().chainId]: { apiClient: mainnetApiClient }, @@ -62,8 +85,8 @@ export const appKit = new AppKit({ createTonstakersProvider(), createLayerswapProvider(), createDecentProvider({ apiKey: ENV_DECENT_API_KEY }), - createTonCenterStreamingProvider({ network: Network.mainnet(), apiKey: ENV_TON_API_KEY_MAINNET }), - createTonCenterStreamingProvider({ network: Network.testnet(), apiKey: ENV_TON_API_KEY_TESTNET }), - createTonApiGaslessProvider(), + createStreamingProvider({ network: Network.mainnet(), apiKey: ENV_TON_API_KEY_MAINNET }), + createStreamingProvider({ network: Network.testnet(), apiKey: ENV_TON_API_KEY_TESTNET }), + createTonApiGaslessProvider(gaslessConfig), ], }); diff --git a/apps/appkit-minter/src/core/configs/env.ts b/apps/appkit-minter/src/core/configs/env.ts index 3b92e35fe..57fbd7049 100644 --- a/apps/appkit-minter/src/core/configs/env.ts +++ b/apps/appkit-minter/src/core/configs/env.ts @@ -6,10 +6,12 @@ * */ +export const ENV_TON_API_PROVIDER = import.meta.env?.VITE_TON_API_PROVIDER === 'tonapi' ? 'tonapi' : 'toncenter'; export const ENV_TON_API_KEY_MAINNET = import.meta.env.VITE_TON_API_KEY ?? '25a9b2326a34b39a5fa4b264fb78fb4709e1bd576fc5e6b176639f5b71e94b0d'; export const ENV_TON_API_KEY_TESTNET = import.meta.env.VITE_TON_API_TESTNET_KEY ?? 'd852b54d062f631565761042cccea87fa6337c41eb19b075e6c7fb88898a3992'; +export const ENV_TON_API_KEY_TETRA = import.meta.env.VITE_TON_API_TETRA_KEY ?? ''; export const ENV_DECENT_API_KEY = import.meta.env.VITE_DECENT_API_KEY ?? '5c951bc81da566bbd030ba8e20724063'; const DEV_TONCONNECT_MANIFEST_URL = 'https://tonconnect-sdk-demo-dapp.vercel.app/tonconnect-manifest.json';