@getclave/lifi-sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
38 lines (37 loc) • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getActionWithFallback = void 0;
const utils_1 = require("viem/utils");
const publicClient_1 = require("./publicClient");
/**
* Executes an action with a fallback to public client if the wallet client fails due to rate limiting
* or similar recoverable errors.
*
* Note: Only falls back to public client if the initial client was a wallet client (has an account address).
* If the initial client was already a public client, no fallback will occur.
*
* @param walletClient - The wallet client to use primarily
* @param action - The function or method to execute
* @param actionName - The name of the action (used for error handling)
* @param params - The parameters for the action
* @returns The result of the action
*/
const getActionWithFallback = async (walletClient, actionFn, name, params) => {
try {
return await (0, utils_1.getAction)(walletClient, actionFn, name)(params);
}
catch (error) {
// Only fall back if this was a wallet client (has an account address)
// If it was already a public client, we don't want to fall back
if (!walletClient.account?.address) {
throw error;
}
const chainId = walletClient.chain?.id;
if (!chainId) {
throw error;
}
const publicClient = await (0, publicClient_1.getPublicClient)(chainId);
return await (0, utils_1.getAction)(publicClient, actionFn, name)(params);
}
};
exports.getActionWithFallback = getActionWithFallback;