Skip to content

Instantly share code, notes, and snippets.

@dabit3
Created December 1, 2021 16:19
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dabit3/f0116831a17ef29da0387bf8dac2206c to your computer and use it in GitHub Desktop.
Connecting a web3 wallet
import { ethers } from 'ethers'
import Web3Modal from 'web3modal'
import WalletConnectProvider from '@walletconnect/web3-provider'
async function getWeb3Modal() {
const web3Modal = new Web3Modal({
network: 'mainnet',
cacheProvider: false,
providerOptions: {
walletconnect: {
package: WalletConnectProvider,
options: {
infuraId: YOUR_INFURA_ID
},
},
},
})
return web3Modal
}
/* invoke this function to prompt user and connect wallet */
async function connectWallet() {
const web3Modal = await getWeb3Modal()
const connection = await web3Modal.connect()
const provider = new ethers.providers.Web3Provider(connection)
const accounts = await provider.listAccounts()
console.log({ accounts })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment