admin 管理员组文章数量: 1086019
I'm building an application with next.js and web3. To connect the user wallet to the front-end I'm using web3modal as following:
const Home: NextPage = () => {
const [signer, setSigner] = useState<JsonRpcSigner | null>(null)
async function connect() {
const web3Modal = new Web3Modal()
const connection = await web3Modal.connect()
const provider = new ethers.providers.Web3Provider(connection)
const signer = provider.getSigner()
setSigner(signer)
}
return (
<div className="flex justify-center">
<button onClick={() => connect()}>Connect wallet</button>
{ signer && (
<h3>wallet connected: {signer._address}</h3>
)}
</div>
)
}
The user can successfully connect the wallet, unfortunately signer._address
is always null. I'd like to display to the user with address has just been connected, how can I fix this?
I'm building an application with next.js and web3. To connect the user wallet to the front-end I'm using web3modal as following:
const Home: NextPage = () => {
const [signer, setSigner] = useState<JsonRpcSigner | null>(null)
async function connect() {
const web3Modal = new Web3Modal()
const connection = await web3Modal.connect()
const provider = new ethers.providers.Web3Provider(connection)
const signer = provider.getSigner()
setSigner(signer)
}
return (
<div className="flex justify-center">
<button onClick={() => connect()}>Connect wallet</button>
{ signer && (
<h3>wallet connected: {signer._address}</h3>
)}
</div>
)
}
The user can successfully connect the wallet, unfortunately signer._address
is always null. I'd like to display to the user with address has just been connected, how can I fix this?
3 Answers
Reset to default 5This is not a question about web3Modal, but a question about ethers.
You can get the address like this:
const [address, setAddress] = useState()
// ...
setAddress(await signer.getAddress())
The documentation is here: https://docs.ethers.io/v5/api/signer/#Signer-getaddress
provider.getSigner()
returns a promise.
You should use await to get the actual address
const signer = provider.getSigner();
const address = await signer.getAddress();
setAddress(address);
you can get the address using the following
const account=(await provider.listAccounts())[0]
本文标签: javascriptHow do I get the address of the connected wallet with web3modalStack Overflow
版权声明:本文标题:javascript - How do I get the address of the connected wallet with web3modal? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744089537a2531772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论