UNPKG

@openocean.finance/widget-sdk

Version:

OpenOcean Any-to-Any Cross-Chain-Swap SDK

48 lines 2 kB
"use strict"; 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, allowUserInteraction, switchChainHook) => { const currentChainId = (await (0, utils_1.getAction)(client, actions_1.getChainId, 'getChainId')(undefined)); if (currentChainId === step.action.fromChainId) { return client; } step.execution = statusManager.initExecutionObject(step); statusManager.updateExecution(step, 'ACTION_REQUIRED'); let switchProcess = statusManager.findOrCreateProcess({ step, type: 'SWITCH_CHAIN', status: 'ACTION_REQUIRED', }); if (!allowUserInteraction) { return; } try { const updatedClient = await switchChainHook?.(step.action.fromChainId); let updatedChainId; if (updatedClient) { updatedChainId = (await (0, utils_1.getAction)(updatedClient, actions_1.getChainId, 'getChainId')(undefined)); } if (updatedChainId !== step.action.fromChainId) { throw new errors_js_1.ProviderError(constants_js_1.OpenOceanErrorCode.ChainSwitchError, 'Chain switch required.'); } switchProcess = statusManager.updateProcess(step, switchProcess.type, 'DONE'); statusManager.updateExecution(step, 'PENDING'); return updatedClient; } catch (error) { statusManager.updateProcess(step, switchProcess.type, 'FAILED', { error: { message: error.message, code: constants_js_1.OpenOceanErrorCode.ChainSwitchError, }, }); statusManager.updateExecution(step, 'FAILED'); throw error; } }; exports.switchChain = switchChain; //# sourceMappingURL=switchChain.js.map