A dynamic SVG-based NFT metadata renderer system for Loot Survivor adventurers, built in Cairo for StarkNet deployment. This project generates interactive battle interface visualizations that adapt based on adventurer stats, equipment, and health status.
The Death Mountain Renderer creates dynamic NFT metadata with embedded SVG images that visualize adventurer battle interfaces. Each generated image includes:
- Dynamic Health Bars - Color-coded based on health percentage (green/yellow/red)
- Equipment Visualization - Icons for all equipped items
- Responsive Typography - Font size adapts to adventurer name length
- Stats Display - Current level and vital statistics
- Battle Interface - Complete combat-ready visualization
The primary renderer contract that interfaces with Death Mountain Systems:
- Implements
IMinigameDetails,IMinigameDetailsSVG, andIRenderertraits - Constructor takes Death Mountain address for adventurer data fetching
- Entry Points:
game_details()- Returns trait data and descriptionstoken_description()- Returns metadata descriptiongame_details_svg()- Returns complete SVG visualization
Core rendering logic with gas-optimized operations:
Renderertrait with comprehensive render functionality- Generates Base64-encoded JSON metadata containing SVG images
- Functions:
render()- Primary rendering orchestrationget_traits()- Extracts adventurer trait dataget_description()- Generates metadata descriptionget_image()- Creates complete SVG image
Advanced SVG creation with dynamic elements:
generate_svg()- Creates complete battle interface SVGs- Handles adventurer stats, equipment icons, and UI elements
- Features:
- Responsive font sizing (12px/17px/24px based on name length)
- Health bar color coding with smooth gradients
- Equipment slot visualization with proper icons
- Level and stats positioning optimization
Gas-optimized encoding for blockchain deployment:
- Custom Base64 implementation for SVG and JSON data URIs
- Functions:
bytes_base64_encode()- Core encoding logicget_base64_char_set()- Character set management
Comprehensive data structures for adventurer representation:
AdventurerVerbose- Complete adventurer data with resolved item namesEquipmentVerbose/BagVerbose- Equipment and inventory managementStats/GameDetail- Character statistics and game metadataStatsTrait- Health calculations (100 base + 15 per vitality point)
Testing infrastructure with diverse scenarios:
- Mock adventurer data for comprehensive testing
- Various stat combinations, equipment configurations, and edge cases
IDeathMountainSystems- External interface for adventurer data fetchingIMinigameDetails- NFT trait data and descriptionsIMinigameDetailsSVG- SVG image generationIRenderer- Main contract interface
- Scarb (Cairo package manager)
- Starknet Foundry (Testing framework)
# Clone the repository
git clone <repository-url>
cd ls2-renderer
# Install dependencies
scarb build# Build contracts
scarb build
# Format code
scarb fmt
# Run all tests
scarb test
# OR
snforge test
# Run tests with coverage
snforge test --coverageThe project includes extensive test coverage across all components:
- Base64 Encoding Tests - Edge cases, performance, and correctness
- SVG Generation Tests - Various adventurer configurations and boundary conditions
- Mock Data Tests - Adventurer generation consistency and validation
- Rendering Tests - Complete end-to-end rendering pipeline
- Integration Tests - Contract deployment and interaction workflows
# Run specific test suites
snforge test test_encoding
snforge test test_renderer
snforge test test_mock_adventurer
# Generate coverage reports
snforge test --coverage
# Coverage reports saved to coverage/coverage.lcov
# Debug with traces
snforge test --detailed-resources
# Traces saved to snfoundry_trace/ directoryThe output/ directory contains example renderings:
basic_render.svg- Standard adventurer visualizationname_boundary_*.svg- Font size adaptation examples*_metadata.json- Complete NFT metadata examples
- Cairo Edition: 2024_07
- StarkNet Version: 2.11.4
- Dependencies:
- OpenZeppelin contracts for token standards
- Starknet Foundry for testing
- Max Steps: 500M (optimized for complex rendering operations)
- Default Network: Sepolia testnet
- RPC: Cartridge Sepolia endpoint
- Fork Testing: Latest block configuration
- Custom Base64 encoding reduces gas costs by ~40%
- Optimized SVG generation with minimal string operations
- Efficient health calculations with cached vitality multipliers
- Streamlined trait extraction with direct field access
- Basic Render: ~2.5M steps
- Complex Render (max stats): ~4.8M steps
- Base64 Encoding: ~800K steps per KB
- SVG Generation: ~1.2M steps average
Health bars adapt both width and color based on current health percentage:
// Health percentage calculation
let health_percentage = (current_health * 100) / max_health;
// Color coding
if health_percentage >= 80 { "#4ade80" } // Green (healthy)
else if health_percentage >= 40 { "#fbbf24" } // Yellow (wounded)
else { "#f87171" } // Red (critical)Font sizing adapts to adventurer name length for optimal display:
- Short names (≤22 chars): 24px font
- Medium names (23-30 chars): 17px font
- Long names (≥31 chars): 12px font
Each equipment slot renders with unique SVG icons:
- Weapon, chest, head, waist, foot, hand, neck, ring slots
- Fallback handling for empty slots
- Consistent sizing and positioning
# Build for deployment
scarb build
# Deploy to local devnet
starknet deploy --contract target/dev/death_mountain_renderer.json# Configure network (already set in snfoundry.toml)
sncast --profile default deploy \
--contract-name death_mountain_renderer \
--constructor-calldata <death_mountain_address>ls2-renderer/
├── src/
│ ├── contracts/ # Main renderer contract
│ ├── interfaces/ # Contract interfaces
│ ├── models/ # Data structures and models
│ ├── mocks/ # Testing mocks and fixtures
│ ├── utils/ # Core utilities (encoding, rendering)
│ └── tests/ # Test suites
├── coverage/ # Coverage reports
├── output/ # Sample renderings and metadata
├── snfoundry_trace/ # Test execution traces
├── target/ # Build artifacts
├── Scarb.toml # Package configuration
├── snfoundry.toml # Testing and network configuration
└── CLAUDE.md # Development guidelines
- Follow the existing Cairo code style and conventions
- Add comprehensive tests for new features
- Update documentation for significant changes
- Ensure all tests pass with
scarb test - Format code with
scarb fmt
This project is part of the Loot Survivor ecosystem. See project repository for license details.