import { atom } from 'jotai' ; import { currentNetworkState } from '@app/store/network/networks' ; import { fetcher as fetchApi } from '@app/common/api/wrapped-fetch' ; import { Configuration , AccountsApi , SmartContractsApi , InfoApi , TransactionsApi , BlocksApi , FaucetsApi , BNSApi , BurnchainApi , FeesApi , SearchApi , RosettaApi , MicroblocksApi , TokensApi , } from '@stacks/blockchain-api-client' ; import type { Middleware , RequestContext } from '@stacks/blockchain-api-client' ; import { MICROBLOCKS_ENABLED } from '@shared/constants' ; param config function apiClients ( config ) { const smartContractsApi = new SmartContractsApi ( config ) ; const accountsApi = new AccountsApi ( config ) ; const infoApi = new InfoApi ( config ) ; const transactionsApi = new TransactionsApi ( config ) ; const microblocksApi = new MicroblocksApi ( config ) ; const blocksApi = new BlocksApi ( config ) ; const faucetsApi = new FaucetsApi ( config ) ; const bnsApi = new BNSApi ( config ) ; const burnchainApi = new BurnchainApi ( config ) ; const feesApi = new FeesApi ( config ) ; const searchApi = new SearchApi ( config ) ; const rosettaApi = new RosettaApi ( config ) ; const tokensApi = new TokensApi ( config ) ; return { smartContractsApi , accountsApi , infoApi , transactionsApi , microblocksApi , blocksApi , faucetsApi , bnsApi , burnchainApi , feesApi , searchApi , rosettaApi , tokensApi , } ; } const unanchoredMiddleware = { pre : ( context ) => { const url = new URL ( context . url ) ; if ( ! url . toString ( ) . includes ( '/v2' ) ) url . searchParams . set ( 'unanchored' , 'true' ) ; return Promise . resolve ( { init : context . init , url : url . toString ( ) , } ) ; } , } ; function createConfig ( basePath , anchored ? ) { const middleware = [ ] ; if ( MICROBLOCKS_ENABLED && ! anchored ) middleware . push ( unanchoredMiddleware ) ; return new Configuration ( { basePath , fetchApi , middleware , } ) ; } export const apiClientState = atom ( get => { const network = get ( currentNetworkState ) ; const config = createConfig ( network . url ) ; return apiClients ( config ) ; } ) ; export const apiClientAnchoredState = atom ( get => { const network = get ( currentNetworkState ) ; const config = createConfig ( network . url , true ) ; return apiClients ( config ) ; } ) ;