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>
)
}