1

I have created a contract and want to now connect my front-end using Sepolia.

I cannot figure out the right way to connect to the host and start my Dapp. I am using Yarn to run the Dapp.

window.addEventListener('load', function() {

   window.ethereum.enable();

        // Checking if Web3 has been injected by the browser (Mist/MetaMask)
        if (typeof web3 !== 'undefined') {
            // Use Mist/MetaMask's provider
            web3js = new Web3(window.ethereum);
        } else {
            // Handle the case where the user doesn't have Metamask installed
            // Probably show them a message prompting them to install Metamask
            // set the provider you want from Web3.providers
        // Sepolia
        web3js = new Web3.providers.HttpProvider("https://rpc2.sepolia.org");

        }

        web3js.eth.defaultAccount = web3js.eth.accounts[0];

        // Now you can start your app & access web3 freely:
        startApp()
      })

Can someone please help?

2
  • I recommend using a metamask / wallet as provider. If you are using an RPC like this you can not access the user accounts. Commented Jun 30, 2023 at 5:33
  • Thanks! How would you go about doing that? Commented Jun 30, 2023 at 6:42

1 Answer 1

0

First of all, make sure you have deployed your smart contract on the Sepolia testnet. You can use RemixIDE. Make sure you deploy the contract with the InjectedProvider() option to connect to your Metamask wallet's Sepolia account.

You can access the user's accounts and sign the contract with the user's wallet like that:

      const provider = new ethers.providers.JsonRpcProvider(
        "sepolia rpc url"
      );
      await provider.send("eth_requestAccounts", []);
      const signer = provider.getSigner();

      let userAddress = await signer.getAddress();

      // replace with your contract's address and abi
      const contract = new Contract(contractAddress, abi, signer);
      
      await contract.getBalance(); // any other interaction with the contract, based on its interface

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.