Hosting a Testnet and Deploying a Contract
Using Foundry to start a local testnet and deploy a Solidity contract.
Foundry
We use Foundry for this.
Developing a Contract
$ forge initin the desired folder.
Starting a Testnet
anvilThis will give you an RPC URL, plus 10 accounts and their private keys.
Deploying a Contract
forge create --rpc-url 127.0.0.1:8545 src/Counter.sol:Counter --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --broadcastThe RPC URL should be the one provided by
anvilsrc/Counter.solis the file path to the smart contract's Solidity source codeCounteris the name of the smart contract--private-keyis one of the private keys displayed byanvil--broadcastbroadcasts 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!
Interacting with the Contract
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:
Last updated
Was this helpful?