Write Data To 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

To send data, we'll use cast send to send a transaction that we signed with our private key to the network. We use Cast to call the increment function we built in a previous lesson. Then we will check Anvil to see the logs that our transaction generated.

Noah Hein: [0:01] If we want to change state on the chain, and we want to send a transaction, that will increase our contract's state. In this case, we're going to change it from 10 to 11. [0:14] We're going to call the increment. We'll go back over here, and we'll do cast send. Cast send takes a different parameter here. Instead of getNum, we're going to want to go back to our ABI. We'll open up another window, and we'll do that again.

[0:33] We'll forge inspect <Number> abi. Here we can see that this is decrement. Let's grab wherever our increment is, so here. That's increment. What we will do is we will say instead of getNum, increment. Since we are sending something up onto the chain, we are going to need to pass it a private key. We can go back to Anvil. Again, we can grab this private key.

[1:12] To show you that this is totally permissionless, we'll grab something else. This would replicate or simulate someone else interacting with the smart contract that you deployed up onto the chain. We'll do private-key. Then, we will send that.

[1:29] Awesome. Here, you can see that we got 1 back. We got a new blockHash. You can see now that our blockNumber has incremented to 2. You can see from our original deployment, the blockNumber was 1.

[1:45] Unlike the regular blockchain that is constantly moving forward the blockNumbers, Anvil isn't unnecessarily using your CPU. It will only "mine" a transaction whenever you send something. If you go back over to your Anvil server, you will see that "Hey, there is a new transaction. We have a new blockNumber."

[2:06] We'll call that one more time to just show, "Hey, cool, all of this is working." You can see that the blockNumber increments to 3. Awesome. It looks like those transactions are going through. Let's call getNum again. If you read hexadecimal, you can see that this would now map to 12. You can see that a was 10. Then, b would have been 11. Now, c is 12.

egghead
egghead
~ just now

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