@lifi/sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
47 lines • 1.44 kB
JavaScript
import { ChainId } from '@lifi/types';
import { SuiClient } from '@mysten/sui/client';
import { getRpcUrls } from '../rpc.js';
const clients = new Map();
/**
* Initializes the Sui clients if they haven't been initialized yet.
* @returns - Promise that resolves when clients are initialized.
*/
export const ensureClients = async () => {
const rpcUrls = await getRpcUrls(ChainId.SUI);
for (const rpcUrl of rpcUrls) {
if (!clients.get(rpcUrl)) {
const client = new SuiClient({ url: rpcUrl });
clients.set(rpcUrl, client);
}
}
};
/**
* Wrapper around getting the client (RPC provider) for Sui
* @returns - Sui RPC clients
*/
export const getSuiClients = async () => {
await ensureClients();
return Array.from(clients.values());
};
/**
* Calls a function on the SuiClient instances with retry logic.
* @param fn - The function to call, which receives a SuiClient instance.
* @returns - The result of the function call.
*/
export async function callSuiWithRetry(fn) {
// Ensure clients are initialized
await ensureClients();
let lastError = null;
for (const client of clients.values()) {
try {
const result = await fn(client);
return result;
}
catch (error) {
lastError = error;
}
}
// Throw the last encountered error
throw lastError;
}
//# sourceMappingURL=suiClient.js.map