How can I get a wallet address by using wallet connect package?

67 views Asked by At

I'm using wallet connect wagmi package for vue. This is my code.

<template>
    <w3m-button size="sm" label="Wallet" balance="show" />
</template>
    
<script setup>
    import { createWeb3Modal, defaultWagmiConfig } from '@web3modal/wagmi'
    import { mainnet, arbitrum, sepolia } from 'viem/chains'
    import { reconnect } from '@wagmi/core'
    
    
    const projectId = import.meta.env.VITE_PROJECT_ID
    
    const metadata = {
        name: 'Web3Modal',
        description: 'Web3Modal Example',
        url: 'https://web3modal.com',
        icons: ['https://avatars.githubusercontent.com/u/37784886']
    }
    
    const chains = [mainnet, sepolia]
    
    const config = defaultWagmiConfig({
        chains,
        projectId,
        metadata,
        enableWalletConnect: true,
        enableInjected: true,
        enableEIP6963: true,
        enableCoinbase: true
    })
    
    
    reconnect(config)
    
    createWeb3Modal({
        wagmiConfig: config,
        projectId,
        enableAnalytics: true
    })
</script>

When I connect to a wallet, I want to know the wallet address of the connected wallet. How can I do that?

1

There are 1 answers

0
David Odinaka Enyoghasim On
onMounted(async () => {    
import { getAccount } from '@wagmi/core'
    
    const interval = setInterval(async () => {
      const account = getAccount(config)
    
      if (account?.isConnected) {
        clearInterval(interval)
      }
      console.log(account)
    }, 1000)
}

call the wallet connect code in the onmounted function to.

there might be other ways but this is what worked for me at first