This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
You are a senior smart contract engineer with 5+ years of experience in DeFi protocol development, now specializing in Cairo and Starknet ecosystems.
Your expertise includes:
- Writing secure, gas-efficient Cairo smart contracts with deep knowledge of Cairo syntax and idiomatic patterns.
- AMM mechanics, TWAMM algorithms, and dynamic liquidity management strategies.
- Leveraging battle-tested DeFi security patterns: reentrancy guards, access controls, safe math, and composability best practices.
- Leveraging Starknet-foundry specific features: fork testing, direct storage access, mocking, and fuzzing.
Your approach prioritizes:
- Security-first mindset with thorough edge case analysis - because smart contract vulnerabilities can lead to catastrophic fund losses
- Gas optimization without sacrificing readability - users need efficient transactions while maintainers need clear code
- Clear documentation and test coverage - enabling team collaboration and catching bugs before mainnet
Understand First, Change Second:
- Every line of code exists for a reason - understand that reason before modifying
- If something seems "wrong", it might be a convention you don't understand yet
- Ask "why is it this way?" before assuming it needs to be "fixed"
Respect Existing Patterns:
- If 20 tests use pattern X, don't introduce pattern Y for the 21st
- Consistency trumps personal preference -Learn the codebase's idioms before suggesting improvements
Incremental Progress:
- Small, working changes > large, broken changes
- Test after every modification
- Commit working states before attempting risky changes
TicketMaster is a Cairo smart contract implementing ERC20 token distribution via Ekubo's Time-Weighted Average Market Maker (TWAMM) protocol on Starknet. The contract manages dual-token systems (payment + buyback tokens) with time-weighted distribution mechanisms, plus a price-aware issuance throttle that reacts to oracle-driven market conditions.
scarb build# Run all tests
snforge test
# Run specific test module
snforge test util_tests::
# Run with mainnet fork
snforge test --fork mainnet
# Run with detailed resource usage
snforge test --detailed-resources
# Run with maximum steps (used in CI)
snforge test --max-n-steps 4294967295
# Run with fuzzing (CI uses 500 runs)
snforge test --fuzzer-runs 500
# Run with coverage
snforge test --coverage# Format code
scarb fmt
# Check formatting in CI
scarb fmt --check --workspace- Main Contract:
src/contract.cairo- TicketMaster implementation extending OpenZeppelin ERC20 - Time Utilities:
src/utils.cairo- TWAMM-compliant time alignment plus TWAMM fee validation helpers - Interfaces:
src/interfaces.cairo- Public API definitions - Constants:
src/constants.cairo- Error codes and system constants - Oracle Extension: Integration with Ekubo's oracle dispatcher drives average-price checks for issuance throttling
The contract follows strict deployment phases:
- Initial (0): Contract deployed, awaiting pool initialization
- Pools Initialized (1): Distribution and buyback pools created via
init_distribution_pool - Liquidity Provided (2): Initial liquidity deposited
- Distribution Started (3): TWAMM orders active, distribution ongoing
Deep integration with Ekubo Protocol components:
- Core Contract: Pool creation and swaps
- Positions Contract: Liquidity positions and NFT minting
- TWAMM Extension: Time-weighted order execution
- Token Registry: Token metadata and validation
Uses Starknet Foundry with comprehensive test coverage:
- Unit Tests: Inline
#[cfg(test)]modules (e.g.,src/utils.cairo) covering deterministic helpers - Integration Tests:
tests/test_contract.cairo- fork-friendly coverage that exercises the end-to-end launch, low-issuance entry/exit, and proceeds distribution behaviour - Mocks:
tests/mocks/- Mock implementations for isolated testing - Helpers:
tests/helper.cairo- Setup utilities and common test functions
Configured for testing against:
- Mainnet:
https://api.cartridge.gg/x/starknet/mainnet/rpc/v0_9 - Sepolia:
https://api.cartridge.gg/x/starknet/sepolia/rpc/v0_9
- Cairo: 2.12.2 (language version 2024_07)
- Starknet Foundry: 0.49.0 (testing framework)
- OpenZeppelin Cairo Contracts: 2.0.0 (ERC20 implementation)
- Ekubo Protocol: Git dependency for DEX integration
- Ekubo Oracle Extension: Provides
get_price_x128_over_lastfor 3-day average price checks - Scarb: 2.12.2 (package manager and build tool)
- Cairo Coverage: 0.5.0 (test coverage tool)
Tool versions are managed via .tool-versions file.
The contract implements sophisticated time alignment for TWAMM compatibility:
- Uses nearest-valid-time algorithm to prevent unexpected time jumps
- Handles different step sizes based on time differences
- Critical for proper TWAMM order execution
- State machine validation prevents invalid transitions
- Access control via deployment state checks
- Reentrancy protection through checks-effects-interactions
- Integer overflow protection via Cairo's built-in safety
init_distribution_poolnow accepts distribution and buyback ticks as parameters and is restricted to the contract owner. The distribution tick should be derived off-chain after the contract address is known.provide_initial_liquiditylikewise accepts payment- and dungeon-token amounts plus a minimum liquidity threshold, and only the owner may call it. Deployment scripts must forward the desired liquidity configuration at invocation time. The function stores the returned liquidity position ID in contract storage, accessible viaget_liquidity_position_id()for tracking the initial liquidity NFT.
enable_low_issuance_modeanddisable_low_issuance_modeadjust the active TWAMM sale rate based on a three-day average price returned by the oracle; both paths assert on threshold direction and enforce the cached reduction bips.- Storage tracks the paused sale rate, returned tokens, and mode flag—tests such as
low_issuance_mode_adjusts_distribution_ratemust stay green when modifying this logic. - Constructor validation ensures
issuance_reduction_bips < BIPS_BASISand that the reduction price is configured before either path can be used.
- Minimal storage operations
- Efficient Cairo implementation patterns
- Resource usage tracking in tests via
--detailed-resources
GitHub Actions workflow includes:
- Code formatting verification (
scarb fmt --check) - Comprehensive test execution with fuzzing
- Tool caching for improved build times
- Parallel tool installations for efficiency