How does the fundMe.provider.getBalance work? #378
-
When we checked the value of our contract with, fundMe.provider.getBalance, how does fundMe have its own provider and how did it get there? Also, we used similar keywords like fundMe.fund that I also don't quite understand. Are these coming from a package, and if so, which one? TLDR: I want to know how the fundMe.provider.getBalance and fund keywords are working under the hood. Thanks :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Great question! So our When we call
When we created the contract, we did so with: fundMe = await ethers.getContract("FundMe") And a little magic happened here:
So, we can just access the provider directly with |
Beta Was this translation helpful? Give feedback.
Great question!
So our
fundMe
variable is a contract object. That comes with a signer or provider.When we call
fundMe.fund(....)
and send a transaction, it needs both a:signer
: someone to sign/send the transactionprovider
: a blockchain connection to send it to (your RPC_URL)When we created the contract, we did so with:
And a little magic happened here:
accounts[0]
from ourhardhat.config.js
to be the signernetwork
we were on as the provider.So, we can just access the provider directly with
fundMe.provider.getBalance
. Make sense?