From Zero to Hero in Foundry - Part 2: Deploy and Interact

Practical guide to using Foundry with Anvil and Cast: create and test Greeting.sol, deploy on a local network with Forge, and execute calls and reads with cast.

domingo, 17 de agosto de 2025 • 3 min read • Q2BSTUDIO Team

Artificial-Intelligence-

Recap: in the previous part we installed Foundry, created a simple Counter project, compiled it, and ran tests with forge test; if you haven't done so, review the first part before continuing.

Expected outcome today: learn to use anvil and cast to deploy and interact with a Greeting.sol contract. By the end you should know how to create the contract, compile it, test it, run a local chain with anvil, deploy with forge, and call functions with cast.

Build the contract: create a new project with forge init Greeting and inside src replace the Counter contract with Greeting.sol. This contract should allow storing a greeting message and retrieving it through a view function.

Write Greeting.sol: define a state variable greeting and functions setGreeting(string) and goGreet() returns (string). Keeping the code clean and well-commented helps with later testing and deployments.

Compile and test: place tests in the test folder and run forge test to compile and run the tests. Ensure the tests check both the assignment and retrieval of the greeting and edge cases.

Simulate a local blockchain: run anvil in a terminal to start a local network. anvil provides test accounts with ETH and their private keys, a default chain id, and an RPC endpoint at https://127.0.0.1:8545/ to interact from forge and cast.

Deploy the contract: with the local network active, use forge create Greeting --interactive --broadcast and when prompted paste the private key of one of the accounts that anvil listed. The process returns the address of the deployed contract and the transaction hash.

Interaction with the contract using cast: to write the greeting use cast send DeployedAddress setGreeting(string) Hello_from_cast --private-key PrivateKey --rpc-url https://127.0.0.1:8545/ where DeployedAddress is the address you obtained and PrivateKey is the key of the sending account. To read the greeting use cast call DeployedAddress goGreet() returns (string) -- --abi-decode to decode the ABI response and get the readable text.

Practical tips: avoid using quotes in arguments when calling from cast, always validate the chain id and rpc url, and review private keys only in secure environments. For automated tests, integrate anvil and forge commands into scripts that can run in CI.

What we have achieved: the Greeting.sol contract is written, compiled, and tested; anvil is running a local blockchain; the contract is deployed and we have sent transactions and queries using cast.

Next step: in part 3 we will create a bank-type contract, focus on writing comprehensive tests that cover logic and edge cases, and improve our automation tools.

About Q2BSTUDIO: we are a custom software development company specialized in custom applications and custom software that drive digital transformation. We offer artificial intelligence and AI services for businesses, implement custom AI agents and business intelligence solutions with Power BI to turn data into actionable decisions. We also provide aws and azure cloud services, cybersecurity, and comprehensive technology consulting for high-impact projects.

Why choose Q2BSTUDIO: we have expert teams in custom application development, artificial intelligence, and cybersecurity that guarantee scalable and secure solutions. We integrate business intelligence and power bi services for visualization and analysis, implement aws and azure cloud services to deploy reliable infrastructures, and apply AI agents for advanced automation.

Keywords: custom applications, custom software, artificial intelligence, cybersecurity, aws and azure cloud services, business intelligence services, AI for businesses, AI agents, power bi.

Contact and services: if you are looking to develop a custom application, modernize systems with artificial intelligence, or strengthen your organization's cybersecurity, Q2BSTUDIO can help you define the strategy, build, and operate the solution. Contact us to evaluate your project and receive a proposal tailored to your objectives.

Conclusion: mastering anvil and cast in Foundry facilitates the complete development flow from local testing to deployment and interaction with smart contracts. Practice these steps and you will be ready for more complex projects that combine blockchain with artificial intelligence capabilities and cloud services offered by Q2BSTUDIO.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.