Read Data From Your Smart Contract On Anvil using Cast

Noah Hein
InstructorNoah Hein
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Cast is another tool that Foundry leverages to make smart contract development easier for you. Cast will allow you to read/write data from your contracts through the terminal with minimal inputs. We'll learn how to use cast and how to read our deployed smart contract numbers.

Instructor: [0:00] Here, what we're going to want to call is we will use getNum(). We will copy that. We can go ahead and clear that out. We'll use cast call. Cast call, for the first argument, it will take the contract that we're interacting with. We can hop back over to Anvil and grab "This is our contract." We can cast call on here. Then we can do "getNum()". [0:34] Here you can see that it returns a hexadecimal number. If you plug this into a converter, this a represents 10. That reads successfully from our smart contract. You can see here that whenever you go back to Anvil, nothing changed, no state changed here. You can see if we run this again, it does that.

[1:02] All that it does is it just runs eth_chainId and eth_call, which are both read operations. No actual state is changing on chain. Normally, this is something that Anvil is giving you. You wouldn't see any of these read operations performed in the explorer. This is the client logging out events for us so that we can see what is happening under the hood here.

egghead
egghead
~ 34 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today