Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Counter Smart Contract Demo

A simple project demonstrating how to develop, test, deploy and interact with an Ethereum smart contract using Foundry and a Python backend.


Project Structure

.
├── backend/        # FastAPI + Web3.py
├── lib/            # Foundry dependencies
├── postman/        # Postman collection
├── script/         # Deployment scripts
├── src/            # Smart contracts
├── test/           # Foundry tests
├── foundry.toml
├── LICENSE
└── README.md

Stack

  • Solidity
  • Foundry
  • Anvil
  • Python
  • FastAPI
  • Web3.py
  • Postman

Local Setup

1. Install Foundry

curl -L https://foundry.paradigm.xyz | bash

foundryup

Verify the installation:

forge --version
cast --version
anvil --version

2. Build the project

forge build

3. Run the tests

forge test

4. Start a local blockchain

anvil

5. Deploy the smart contract

forge script script/Counter.s.sol:CounterScript \
--rpc-url http://127.0.0.1:8545 \
--private-key <ANVIL_PRIVATE_KEY> \
--broadcast

Copy the deployed contract address.


Testing with Cast

Read current value

cast call <CONTRACT_ADDRESS> \
"number()(uint256)" \
--rpc-url http://127.0.0.1:8545

Set counter value

cast send <CONTRACT_ADDRESS> \
"setNumber(uint256)" 10 \
--private-key <ANVIL_PRIVATE_KEY> \
--rpc-url http://127.0.0.1:8545

Increment counter

cast send <CONTRACT_ADDRESS> \
"increment()" \
--private-key <ANVIL_PRIVATE_KEY> \
--rpc-url http://127.0.0.1:8545

Verify the updated value

cast call <CONTRACT_ADDRESS> \
"number()(uint256)" \
--rpc-url http://127.0.0.1:8545

Backend Setup

Install dependencies

cd backend

pip install fastapi uvicorn web3

Configure the contract address

Update the following value inside:

backend/main.py
CONTRACT_ADDRESS = "<DEPLOYED_CONTRACT_ADDRESS>"

Start the backend

uvicorn main:app --reload

Testing with Postman

Import the collection located in:

postman/

Available requests:

  • GET - Blockchain status
  • GET - Get counter number
  • POST - Set counter number
  • POST - Increment counter number

About

End-to-end blockchain demo with Solidity, Foundry, Python and Postman.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages