Skip to content

Latest commit

 

History

History
46 lines (41 loc) · 1.61 KB

File metadata and controls

46 lines (41 loc) · 1.61 KB

ethersjs

ethersjs lib - examples : connect to the blockchain, read & write.

  • Connect web to blockchain (WEB3): here
  • Read from blockchain directly and/or from wallet: here
  • Listening smart contract from events: here

important:

ethersjs v6 have different method for detecting web3

example:

example wallet connect, without external libs. (run in webserver)

<!DOCTYPE html>
<html>
<head>
  <title>Web3 Wallet Connect Example</title>
  <link rel="stylesheet" href="https://bootswatch.com/4/darkly/bootstrap.min.css">
</head>
<body>
  <button id="connect-button" class="btn btn-primary m-5">Connect to Wallet</button>
  <script>
    const connectButton = document.getElementById('connect-button');
    const provider = window.ethereum;

    const connectToWallet = async () => {
      if (typeof provider !== 'undefined') {
        try {
          await provider.request({ method: 'eth_requestAccounts' });
          console.log('Connected to wallet:', provider.selectedAddress);
          // Do something with the connected wallet
        } catch (error) {
          console.error('Failed to connect to wallet:', error);
        }
      } else {
        console.error('No Web3 provider found');
      }
    };

    connectButton.addEventListener('click', connectToWallet);
  </script>
</body>
</html>