@0xsequence/connect
Version:
Connect package for Sequence Web SDK
54 lines • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.walletConnect = void 0;
const wagmi_1 = require("wagmi");
const connectors_1 = require("wagmi/connectors");
const WalletConnectLogo_js_1 = require("./WalletConnectLogo.js");
const walletConnect = (options) => ({
id: 'wallet-connect',
logoDark: WalletConnectLogo_js_1.WalletConnectLogo,
logoLight: WalletConnectLogo_js_1.WalletConnectLogo,
name: 'WalletConnect',
type: 'wallet',
createConnector: () => {
const { defaultNetwork, ...walletConnectOptions } = options;
const baseConnector = (0, connectors_1.walletConnect)(walletConnectOptions);
return (0, wagmi_1.createConnector)(config => {
const connector = baseConnector(config);
const connect = async (params) => {
const targetChainId = params?.chainId ?? defaultNetwork ?? config.chains[0]?.id;
if (!targetChainId) {
throw new Error('No target chain ID available');
}
if (!connector.connect || !connector.switchChain) {
throw new Error('WalletConnect connector not properly initialized');
}
// First establish the basic connection
const result = await connector.connect();
// Only attempt to switch chains if we're not already on the target chain
if (result.chainId !== targetChainId) {
try {
// Switch to the target chain
await connector.switchChain({ chainId: targetChainId });
// Return the connection with the updated chain
return {
accounts: result.accounts,
chainId: targetChainId
};
}
catch (error) {
console.warn('Failed to switch chain:', error);
return result;
}
}
return result;
};
return {
...connector,
connect
};
});
}
});
exports.walletConnect = walletConnect;
//# sourceMappingURL=walletConnect.js.map