34
loading...
This website collects cookies to deliver better user experience
0xd2f8ed343386FB042178c7e133A837CB8043d0dc
npx create-react-app
yarn add ethers
import './App.css';
import { useState } from 'react';
import { ethers } from 'ethers';
function App() {
const [name, setName] = useState("");
return (
<div className="App">
<h1>{name}</h1>
</div>
);
}
export default App;
<div className="App">
<button className ="button" onClick={() => handleWalletConnect()}>connect</button>
<h1>{name}</h1>
</div>
const [name, setName] = useState("");
const handleWalletConnect = async () => {
const { ethereum } = window;
if(ethereum) {
const provider = new ethers.providers.Web3Provider(ethereum)
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner()
const address = await signer.getAddress()
const ens = await provider.lookupAddress(address);
if (ens !== null) {
setName(ens)
} else {
setName(address)
}
} else {
alert('no wallet detected!')
}
}
const { ethereum } = window
const provider = new ethers.providers.Web3Provider(ethereum)
await provider.send("eth_requestAccounts", [])
const signer = provider.getSigner()
const address = await signer.getAddress()
const ens = await provider.lookupAddress(address)