Skip to content

Commit

Permalink
feat: add chain change subscribe to evm namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
nikaaru committed Nov 23, 2024
1 parent 455bafc commit 9117c1a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions wallets/core/src/namespaces/evm/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,35 @@ export function changeAccountSubscriber(
},
];
}

export function changeChainSubscriber(
instance: () => ProviderAPI
): [Subscriber<EvmActions>, SubscriberCleanUp<EvmActions>] {
let eventCallback: EIP1193EventMap['chainChanged'];

return [
(context) => {
const evmInstance = instance();

if (!evmInstance) {
throw new Error(
'Trying to subscribe to your EVM wallet, but seems its instance is not available.'
);
}

const [, setState] = context.state();

eventCallback = async (chainId: string) => {
setState('network', chainId);
};
evmInstance.on('chainChanged', eventCallback);
},
() => {
const evmInstance = instance();

if (eventCallback && evmInstance) {
evmInstance.removeListener('chainChanged', eventCallback);
}
},
];
}

0 comments on commit 9117c1a

Please sign in to comment.