This project demonstrates a basic funding and withdrawal smart contract, FundMe
, written in Solidity, along with the deployment and interaction scripts using the Foundry framework.
The FundMe
contract allows users to send Ether to the contract and store it securely until the contract owner withdraws it. This repository contains:
- Solidity contracts for
FundMe
- Scripts to deploy, fund, and withdraw from the contract using Foundry and
forge-std
- Comprehensive tests for contract interaction
- Fund Contract: Users can fund the contract by sending Ether.
- Withdraw Funds: The contract owner can withdraw the funds.
- Deployment & Interactions: Includes scripts for deploying and interacting with the contract.
- Testing: Test the funding and withdrawal processes using Foundry's testing framework.
- git
- You'll know you did it right if you can run
git --version
and see a response likegit version x.x.x
- You'll know you did it right if you can run
- foundry
- You'll know you did it right if you can run
forge --version
and see a response likeforge 0.2.0 (816e00b 2023-03-16T00:05:26.396218Z)
- You'll know you did it right if you can run
forge script script/DeployFundMe.s.sol
We talk about 4 test tiers in the video.
- Unit
- Integration
- Forked
- Staging
This repo covers #1 and #3.
forge test
or
forge test --match-test testFunctionName
or
forge test --fork-url $SEPOLIA_RPC_URL
forge coverage
- Setup environment variables
You'll want to set your SEPOLIA_RPC_URL
as an environment variable. You can add it to a .env
file, similar to what you see in .env.example
.
SEPOLIA_RPC_URL
: This is the URL of the Sepolia testnet node you're working with. You can get set up with one for free from Alchemy.
Optionally, add your ETHERSCAN_API_KEY
if you want to verify your contract on Etherscan.
-
Import and set up your account using cast wallet: Instead of using private keys in plain text, you can securely manage accounts with cast wallet.
First, import your private key securely using:
cast wallet import defaultKey --interactive
cast wallet list
Once imported, you can use your account safely in any script.
- Get testnet ETH
Head over to faucets.chain.link and get some testnet ETH. You should see the ETH show up in your wallet.
- Deploy
forge script script/DeployFundMe.s.sol --rpc-url $SEPOLIA_RPC_URL --account defaultKey --sender $SENDER --broadcast --verify --etherscan-api-key $ETHERSCAN_API_KEY -vvvv
After deploying to a testnet or local net, you can run the scripts.
Using cast with a locally deployed contract example:
cast send <FUNDME_CONTRACT_ADDRESS> "fund()" --value 0.1ether --account defaultKey
or
forge install Cyfrin/foundry-devops --no-commit
forge script script/Interactions.s.sol --rpc-url $SEPOLIA_RPC_URL --account defaultKey --sender $SENDER --broadcast -vvvv
cast send <FUNDME_CONTRACT_ADDRESS> "withdraw()" --account defaultKey
You can estimate how much gas things cost by running:
forge snapshot
And you'll see an output file called .gas-snapshot
.
To run code formatting:
forge fmt
Chainlink-brownie-contracts is an official repo. The repository is owned and maintained by the Chainlink team for this very purpose and receives releases from the proper Chainlink release process. You can see it's still the smartcontractkit
organization as well.
https://github.com/smartcontractkit/chainlink-brownie-contracts