Set up and configure Stellar Horizon API endpoints for different networks in React Native
import { Horizon, Networks } from "@stellar/stellar-sdk";
// Public Horizon endpoints
const mainnetServer = new Horizon.Server("https://horizon.stellar.org");
const testnetServer = new Horizon.Server("https://horizon-testnet.stellar.org");
// Custom Horizon endpoint (e.g., self-hosted or third-party provider)
const customServer = new Horizon.Server("https://your-custom-horizon.example.com", {
allowHttp: false, // set to true only for local development
});
import { useMemo } from 'react';
import { useClient, useAccount } from '@getpara/react-native-wallet';
import { createParaStellarSigner } from '@getpara/stellar-sdk-v14-integration';
import { Horizon, Networks } from "@stellar/stellar-sdk";
type StellarNetwork = "mainnet" | "testnet";
const NETWORK_CONFIG = {
mainnet: {
horizonUrl: "https://horizon.stellar.org",
networkPassphrase: Networks.PUBLIC,
},
testnet: {
horizonUrl: "https://horizon-testnet.stellar.org",
networkPassphrase: Networks.TESTNET,
},
} as const;
function useStellarNetwork(network: StellarNetwork) {
const para = useClient();
const { isConnected } = useAccount();
const config = NETWORK_CONFIG[network];
const signer = useMemo(() => {
if (!para || !isConnected) return null;
return createParaStellarSigner({
para,
networkPassphrase: config.networkPassphrase,
});
}, [para, isConnected, config.networkPassphrase]);
const server = new Horizon.Server(config.horizonUrl);
return { server, signer, isLoading: !para, networkPassphrase: config.networkPassphrase };
}
import { Horizon } from "@stellar/stellar-sdk";
async function checkHorizonHealth() {
const server = new Horizon.Server("https://horizon.stellar.org");
// Get ledger info
const ledger = await server.ledgers().order("desc").limit(1).call();
const latestLedger = ledger.records[0];
console.log("Latest ledger:", latestLedger.sequence);
console.log("Closed at:", latestLedger.closed_at);
// Get fee stats
const feeStats = await server.feeStats();
console.log("Base fee:", feeStats.last_ledger_base_fee);
console.log("Fee charged (p50):", feeStats.fee_charged.p50);
}