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

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.