@lifi/sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
59 lines • 2.18 kB
JavaScript
import { ChainId } from '@lifi/types';
import { Connection } from '@solana/web3.js';
import { getRpcUrls } from '../rpc.js';
import { JitoConnection } from './jito/JitoConnection.js';
const connections = new Map();
/**
* Initializes the Solana connections if they haven't been initialized yet.
* @returns - Promise that resolves when connections are initialized.
*/
const ensureConnections = async () => {
const rpcUrls = await getRpcUrls(ChainId.SOL);
for (const rpcUrl of rpcUrls) {
if (!connections.get(rpcUrl)) {
const connection = (await JitoConnection.isJitoRpc(rpcUrl))
? new JitoConnection(rpcUrl)
: new Connection(rpcUrl);
connections.set(rpcUrl, connection);
}
}
};
/**
* Wrapper around getting the connection (RPC provider) for Solana
* Returns only non-Jito RPC connections (excludes JitoConnection instances)
* @returns - Solana RPC connections (excluding Jito connections)
*/
export const getSolanaConnections = async () => {
await ensureConnections();
return Array.from(connections.values()).filter((conn) => conn instanceof Connection && !(conn instanceof JitoConnection));
};
/**
* Get Jito-enabled connections only.
* @returns - Array of JitoConnection instances
*/
export const getJitoConnections = async () => {
await ensureConnections();
return Array.from(connections.values()).filter((conn) => conn instanceof JitoConnection);
};
/**
* Calls a function on the Connection instances with retry logic.
* @param fn - The function to call, which receives a Connection instance.
* @returns - The result of the function call.
*/
export async function callSolanaWithRetry(fn) {
// Ensure connections are initialized
await ensureConnections();
let lastError = null;
for (const connection of connections.values()) {
try {
const result = await fn(connection);
return result;
}
catch (error) {
lastError = error;
}
}
// Throw the last encountered error
throw lastError || new Error('No Solana RPC connections available');
}
//# sourceMappingURL=connection.js.map