@lifi/sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
47 lines • 1.52 kB
JavaScript
import { ChainId } from '@lifi/types';
import { Connection } from '@solana/web3.js';
import { getRpcUrls } from '../rpc.js';
const connections = new Map();
/**
* Initializes the Solana connections if they haven't been initialized yet.
* @returns - Promise that resolves when connections are initialized.
*/
export const ensureConnections = async () => {
const rpcUrls = await getRpcUrls(ChainId.SOL);
for (const rpcUrl of rpcUrls) {
if (!connections.get(rpcUrl)) {
const connection = new Connection(rpcUrl);
connections.set(rpcUrl, connection);
}
}
};
/**
* Wrapper around getting the connection (RPC provider) for Solana
* @returns - Solana RPC connections
*/
export const getSolanaConnections = async () => {
await ensureConnections();
return Array.from(connections.values());
};
/**
* 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;
}
//# sourceMappingURL=connection.js.map