1

This is not an issue but just looking for an explanation. I have the following code to get the contract data:

const provider = new ethers.providers.Web3Provider(ethereum);
await provider.send("eth_accounts", []);

const signer = await provider.getSigner();
console.log(signer);

const account = await ethereum.request({method: 'eth_accounts'});
console.log(account[0]);

const contract = new ethers.Contract(contractAddress, contractABI, signer);
console.log(contract)

Everything is working fine but I am confused on why when I log out the address for the signer variable, it shows as null but when I log out the account variable I get my address. If I swap the value of signer that is inside of the contract variable with account it fails but it succeeds with signer even though the address is null and I am able to see my contract functions.

Could anyone explain this or point me to docs that has a better explanation?

If it even helps, I am using MetaMask wallet and the full file code is written in react/typscript here:

import {useEffect} from 'react'
import contractABI from '../utils/contractABI.json';

declare const window: Window &
  typeof globalThis & {
    ethereum: any
}

export default function Create() {
    const {ethers} = require('ethers');
    const contractAddress = '0x261d5ADa2C89369E1AfCAA98d52dEb124DD9f0Ff'
    const addNewStudent = async () => {
        try {
            const {ethereum} = window;
            if(ethereum) {
                const provider = new ethers.providers.Web3Provider(ethereum);
                await provider.send("eth_accounts", []);
                const signer = await provider.getSigner();
                const account = await ethereum.request({method: 'eth_accounts'});
                console.log(account[0]);
                const contract = new ethers.Contract(contractAddress, contractABI, signer);
                console.log(contract)
                // const addNewStudent = contract.addStudent
            }
        } catch (err: any) {
        console.log(err);
        }
    }

    useEffect(() => {
        addNewStudent();
    }, [])
  return (
    <div>Create</div>
  )
}

1 Answer 1

2

A Signer in Ethers.js is an object that represents an Ethereum account. It's used to send transactions to contracts and other accounts. getSigners is used to get a list of accounts in the node you're connecting to.

eth_accounts is used to return an array that is either empty or contains a single account address. The returned address, if any, is the address of the most recently used account that the caller is permitted to access.

See:

https://hardhat.org/hardhat-runner/docs/other-guides/waffle-testing

https://docs.metamask.io/guide/ethereum-provider.html#events

2
  • You mentioned Ethereum account. So are you talking about the metamask account I have?
    – amit
    Mar 5 at 5:58
  • What is the difference between these two: const [deployer] = await ethers.getSigners(); const signer = await new ethers.Wallet(privKey, provider);
    – amit
    Mar 5 at 5:58

Your Answer

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

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