WalletConnect / modal-react-native

Simplest and most minimal way to connect your users with WalletConnect
https://docs.walletconnect.com/2.0/advanced/walletconnectmodal/about?platform=react-native
Apache License 2.0
31 stars 9 forks source link

[bug]: Listener is not working #110

Closed pratul-mindfire closed 5 months ago

pratul-mindfire commented 8 months ago

Description

Listener is not working

WalletConnect Modal SDK version

"@walletconnect/modal-react-native": "1.1.0",

Output of npx react-native info

System: OS: macOS 14.2.1 CPU: (8) arm64 Apple M1 Memory: 79.47 MB / 8.00 GB Shell: version: "5.9" path: /bin/zsh Binaries: Node: version: 21.6.0 path: /opt/homebrew/bin/node Yarn: version: 1.22.21 path: /opt/homebrew/bin/yarn npm: version: 10.2.4 path: /opt/homebrew/bin/npm Watchman: version: 2023.12.04.00 path: /opt/homebrew/bin/watchman Managers: CocoaPods: version: 1.14.3 path: /Users/pratuldawande/.gem/bin/pod SDKs: iOS SDK: Platforms:

Expo Version (if applies)

No response

Steps to reproduce

Listener is not working . I have tested for metamask and rainbow wallet

Snack, code example, screenshot, or link to a repository

const {isConnected, provider, open} = useWalletConnectModal(); const [client, setClient] = useState(); const [modalVisible, setModalVisible] = useState(false); const [rpcResponse, setRpcResponse] = useState(); const [loading, setLoading] = useState(false);

useEffect(() => { // const handleChainChanged = (newChainId: string) => { // console.log(newChainId, 'newChainId') // } // const handleAccountsChanged = (newAccount: string) => { // console.log(newAccount, 'newAccount') // }

// provider?.events.addListener('chainChanged', handleChainChanged);
// provider?.events.addListener('accountsChanged', handleAccountsChanged);

// return () => {
//   provider?.events.removeAllListeners();
// }
provider?.on("chainChanged", (newChain: unknown) => {
  console.log('useEffect::ethereum on "chainChanged"', newChain);
  //setChain(newChain as string);
});
provider?.on("connect", () => {
  console.log("connect1234");

});
provider?.on("accountsChanged", (_accounts: unknown) => {
  const accounts = _accounts as string[];
  //dispatch(setLogin(!isLogin));
  console.log('useEffect::ethereum on "accountsChanged"', accounts);

});

}, [])

ignaciosantise commented 8 months ago

Hey @pratul-mindfire 👋

try setting the listeners when the provider is initialized. Let me know if if works now

  useEffect(() => {
    if (provider) {
      provider.on('chainChanged', (newChain: unknown) => {
        console.log('useEffect::ethereum on "chainChanged"', newChain);
      });
      provider.on('connect', () => {
        console.log('connect1234');
      });
      provider.on('accountsChanged', (_accounts: unknown) => {
        const accounts = _accounts as string[];
        console.log('useEffect::ethereum on "accountsChanged"', accounts);
      });
    }
  }, [provider]);