This project demonstrates advanced smart contract development on Starknet using Cairo 1.0, featuring a comprehensive counter contract with ERC20 token integration, ownership patterns, event emission, and comprehensive testing. Built as the final project for Starknet Basecamp 13, it showcases production-ready development practices and real-world blockchain interactions.
- π Ownership Control: OpenZeppelin's Ownable pattern for secure access management
- π° ERC20 Integration: STRK token payments for premium operations
- π Event-Driven Architecture: Comprehensive event emission for frontend integration
- π§ͺ 100% Test Coverage: Extensive test suite covering all scenarios
- π Production Ready: Error handling, security checks, and gas optimization
- π¨ Modern Frontend: Next.js with TypeScript and Tailwind CSS
#[starknet::interface]
pub trait ICounter<T> {
fn get_counter(self: @T) -> u32;
fn increase_counter(ref self: T);
fn decrease_counter(ref self: T);
fn set_counter(ref self: T, new_value: u32);
fn reset_counter(ref self: T);
}| Function | Access Level | Description | Cost |
|---|---|---|---|
get_counter() |
Public | Read current counter value | Free |
increase_counter() |
Public | Increment counter by 1 | Gas only |
decrease_counter() |
Public | Decrement counter by 1 (with bounds check) | Gas only |
set_counter() |
Owner Only | Set counter to any value | Gas only |
reset_counter() |
Public | Reset counter to 0 | 1 STRK |
- OpenZeppelin Integration: Secure ownership management
- Owner-Only Functions:
set_counter()restricted to contract owner - Flexible Ownership: Transferrable ownership capabilities
- STRK Token Payments: Reset operation requires 1 STRK payment
- Balance Validation: Checks user has sufficient STRK tokens
- Allowance Management: Requires token approval before reset
- Automatic Transfer: STRK sent to contract owner upon reset
#[derive(Drop, starknet::Event)]
pub struct CounterChanged {
#[key]
pub caller: ContractAddress,
pub old_value: u32,
pub new_value: u32,
pub reason: ChangeReason,
}Event Types:
Increase: Counter incrementedDecrease: Counter decrementedSet: Owner set new valueReset: Counter reset with payment
| Test Category | Tests | Coverage |
|---|---|---|
| Basic Operations | 4 | β 100% |
| Access Control | 2 | β 100% |
| Error Handling | 2 | β 100% |
| ERC20 Integration | 3 | β 100% |
| Event Emission | 6 | β 100% |
#[test]
fn test_contract_initialization()
fn test_increase_counter()
fn test_decrease_counter_happy_path()
fn test_set_counter_owner()
fn test_reset_counter_success()#[test]
#[should_panic(expected: "Counter cannot be less than 0")]
fn test_decrease_counter_fail_path()
#[test]
#[should_panic]
fn test_set_counter_non_owner()
#[test]
#[should_panic(expected: "Caller does not have enough STARK tokens")]
fn test_reset_counter_insufficient_balance()
#[test]
#[should_panic(expected: "Contract is not allowed to spend the caller's STARK tokens")]
fn test_reset_counter_insufficient_allowance()- Event Spy: Validates all events are emitted correctly
- Address Cheating: Tests different caller addresses
- Balance Manipulation: Tests ERC20 token scenarios
- Multi-Contract Interaction: STRK token approval and transfer testing
- Node.js (v22+)
- Yarn
- Starknet Devnet
- Scarb (v2.11.4)
- Starknet Foundry (v0.46.0)
# Clone the repository
git clone <your-repo-url>
cd snapp
# Install dependencies
yarn install
# Start local Starknet network
yarn chain
# Deploy contracts (in new terminal)
yarn deploy
# Start frontend (in new terminal)
yarn start# Run all tests
yarn test
# Run with coverage
yarn test --coverage
# Run specific test file
yarn test test_counter.cairopackages/
βββ nextjs/ # Frontend application
β βββ app/ # Next.js 13+ app directory
β βββ components/ # React components
β βββ hooks/ # Custom React hooks
β βββ utils/ # Utility functions
β βββ contracts/ # Contract ABIs and addresses
βββ snfoundry/ # Smart contract development
βββ contracts/
β βββ src/
β β βββ counter.cairo # Main counter contract
β β βββ utils.cairo # Helper functions
β β βββ lib.cairo # Library file
β βββ tests/
β βββ test_counter.cairo # Comprehensive test suite
βββ scripts-ts/ # Deployment scripts
- Storage: Simple counter state with OpenZeppelin ownership
- Constructor: Initializes counter value and sets owner
- Functions: Five main functions with different access levels
pub fn strk_address() -> ContractAddress
pub fn strk_to_fri(amount: u256) -> u256- Scaffold-Stark Hooks:
useScaffoldReadContract,useScaffoldWriteContract - Multi-Write Support:
useScaffoldMultiWriteContractfor token approval + reset - Event Monitoring: Real-time event listening and display
- Balance Integration: STRK balance checking and display
- Ownership Pattern: Critical functions restricted to owner
- Public Functions: Safe operations available to all users
- Payment Verification: STRK balance and allowance checks
- Balance Validation: Prevents insufficient balance operations
- Allowance Checks: Requires explicit token approval
- Atomic Operations: Multi-write transactions ensure consistency
- Bounds Checking: Prevents counter underflow
- Assert Statements: Clear error messages for failed operations
- Graceful Failures: Proper panic messages for debugging
- Minimal Storage: Only essential state variables
- Efficient Events: Key-indexed events for easy filtering
- Gas-Efficient Operations: Optimized for Starknet's gas model
- Batch Operations: Multi-write support for complex transactions
- Starknet Devnet: Local development and testing
- Starknet Sepolia: Testnet deployment
- Starknet Mainnet: Production deployment ready
This project was developed as part of Starknet Basecamp 13. While it's a final project, contributions and improvements are welcome!
- Testing: All new features must include comprehensive tests
- Documentation: Update README for any new functionality
- Code Style: Follow Cairo and TypeScript best practices
- Security: Review all access controls and token interactions
This project demonstrates mastery of:
- β Cairo 1.0 smart contract development
- β OpenZeppelin component integration
- β ERC20 token interaction patterns
- β Event-driven architecture
- β Comprehensive testing with Starknet Foundry
- β Frontend integration with React/Next.js
- β Production deployment practices
- β Security patterns and access control
This project is licensed under the MIT License - see the LICENSE file for details.
Built with β€οΈ for Starknet Basecamp 13
Starknet β’ Cairo β’ Scaffold-Stark