Hosting a Testnet and Deploying a Contract
Using Foundry to start a local testnet and deploy a Solidity contract.
Last updated
Was this helpful?
Using Foundry to start a local testnet and deploy a Solidity contract.
Last updated
Was this helpful?
We use for this.
in the desired folder.
This will give you an RPC URL, plus 10 accounts and their private keys.
The RPC URL should be the one provided by anvil
src/Counter.sol
is the file path to the smart contract's Solidity source code
Counter
is the name of the smart contract
--private-key
is one of the private keys displayed by anvil
--broadcast
broadcasts the deployment of the transaction
Without the --broadcast
, foundry will do a "dry run" - simulate the contract's execution without actually deploying it. Broadcasting it will return an address!
We can increment the counter as follows:
cast send
is used for transactions that write to the blockchain, and therefore require signing.
getCounter()
is a view
function, so does not require a transaction to call. We use cast call
instead, which does not requires a private key:
And we see it returns the value 1
! If we cast send
another increment call and then cast call
to read the counter again, we see it's incremented again: