0

I am working on a project that just needs to retrieve the user's public address from the Metamask wallet into my project app. Can I get some help regarding this issue? I have tried the official Metamask SDK but it gets stuck on the wallet and does not return to the app with a connect wallet prompt as it shows in the example. I have tried Implementing the same using WalletConnect but couldn't get it to work as well. It would be extremely helpful if someone could share an example. Thank you. Added below is a snippet of the code used to connect Metamask using the Metamask SDK.

const connect = async () => { 
  try { 
  console.log('Connection Started:::::::'); 
   const result = await ethereum.request({method: 'eth_requestAccounts'}); 
   console.log('RESULT', result?.[0]); 
    setAccount(result?.[0]); 
    getBalance(); 
    } catch (e) { console.log('ERROR', e); } 
};
7
  • Hey, can you show the code you are using?
    – donoso.eth
    Dec 29, 2022 at 9:29
  • Using Metamask SDK: const connect = async () => { try { console.log('Connection Started:::::::'); const result = await ethereum.request({method: 'eth_requestAccounts'}); console.log('RESULT', result?.[0]); setAccount(result?.[0]); getBalance(); } catch (e) { console.log('ERROR', e); } }; Dec 29, 2022 at 9:31
  • The Declarations are as follows: const sdk = new MetaMaskSDK({ openDeeplink: link => { Linking.openURL(link); }, timer: BackgroundTimer, dappMetadata: { name: 'React Native Test Dapp', url: 'example.com', }, }); const ethereum = sdk.getProvider(); const provider = new ethers.providers.Web3Provider(ethereum); Dec 29, 2022 at 9:32
  • It is quite difficult to read, would be possible that you edit your question add into it with code format?
    – donoso.eth
    Dec 29, 2022 at 10:19
  • I've include the forst code snoppet, so you can see how it works
    – donoso.eth
    Dec 29, 2022 at 10:26

1 Answer 1

1

Use below code snippet to use wallet connect to get wallet address:

import * as React from "react";
import {Button, Text, TouchableOpacity, StyleSheet } from "react-native";
import { useWalletConnect } from "@walletconnect/react-native-dapp";

export default function WalletConnectExperience() {
  const connector = useWalletConnect();

  const connectWallet = React.useCallback(() => {
    return connector.connect();
  }, [connector]);

  return (
    <>
      {!connector.connected ? (
        <Button onPress={connectWallet} label="Connect wallet" />
      ) : (
        <>
          <Text>{connector.accounts[0]}</Text>
        </>
      )}
    </>
  );
}

Metamask sdk refrences: https://c0f4f41c-2f55-4863-921b-sdk-docs.github.io/guide/metamask-sdk-js/metamask-sdk-react-native.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.