Deploying and Calling contracts?
Lets deploy your first smart contract
Now that all the setup is complete, we can move onto deploying and interacting with contracts.
While in the Foundry project directory, you can deploy your contract using the forge create
command. Here’s how to deploy a Counter.sol contract:
The command breakdown:
forge create
: The command to deploy a contract--rpc-url
: Your network’s RPC URL--private-key
: Your wallet’s private key (never share this!)src/Counter.sol:Counter
: The path to your contract file and the contract name after the colon
Importance of Dry Runs
This will generate a build version of the contract. This is always good to run before actually deploying onto blockchain in order to see what if command has bugs but more importantly save money. If you deployment, you will still pay gas fees.
Real Run
To put the transaction on Testnet, we will need to add the --broadcast
flag at the end of the function.
The output will resemble this
Deployer
-> Your public addressDeployed
to -> Deployed contract addressTransaction hash
-> Transaction that we just signed above ^
Put this into Sepolia Ether Scan to analyze the transaction and contract hash.
Interacting with the Smart Contract
It doesn’t cost money to view the state of the blockchain. Notice that viewing the blockchain we use call.
Changing the state on the blockchain requires paying gas. Viewing does not, hence why we are not signing with our private key