@lifi/sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
42 lines • 1.77 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.switchChain = void 0;
const actions_1 = require("viem/actions");
const utils_1 = require("viem/utils");
const constants_js_1 = require("../../errors/constants.js");
const errors_js_1 = require("../../errors/errors.js");
const switchChain = async (client, statusManager, step, process, targetChainId, allowUserInteraction, executionOptions) => {
const currentChainId = (await (0, utils_1.getAction)(client, actions_1.getChainId, 'getChainId')(undefined));
if (currentChainId === targetChainId) {
return client;
}
if (!allowUserInteraction) {
return;
}
try {
if (!executionOptions?.switchChainHook) {
throw new errors_js_1.ProviderError(constants_js_1.LiFiErrorCode.ChainSwitchError, 'Chain switch hook is not provided.');
}
const updatedClient = await executionOptions.switchChainHook(targetChainId);
let updatedChainId;
if (updatedClient) {
updatedChainId = (await (0, utils_1.getAction)(updatedClient, actions_1.getChainId, 'getChainId')(undefined));
}
if (updatedChainId !== targetChainId) {
throw new errors_js_1.ProviderError(constants_js_1.LiFiErrorCode.ChainSwitchError, 'Chain switch required.');
}
return updatedClient;
}
catch (error) {
statusManager.updateProcess(step, process.type, 'FAILED', {
error: {
message: error.message,
code: constants_js_1.LiFiErrorCode.ChainSwitchError,
},
});
statusManager.updateExecution(step, 'FAILED');
throw error;
}
};
exports.switchChain = switchChain;
//# sourceMappingURL=switchChain.js.map
;