A Node.js server that listens to AllocationCreated smart contract events and filters them based on a configured provider ID.
- β
Listens to
AllocationCreatedevents from a specified smart contract - π― Filters events by configured provider ID
- π Can fetch and process past events from a specified block
- π Graceful shutdown handling
- π‘οΈ Error handling and connection validation
- π Comprehensive logging
npm installCopy the example environment file and configure it:
cp env.example .envEdit .env with your configuration:
# Blockchain RPC URL
# For WebSocket (recommended for real-time events): wss://your-websocket-endpoint
# For HTTP RPC: https://your-rpc-endpoint-here
RPC_URL=wss://your-websocket-endpoint
# Contract address to listen for events
CONTRACT_ADDRESS=0x1234567890123456789012345678901234567890
# Configured provider ID to filter events
PROVIDER_ID=123
# Size limits for allocations (in bytes)
MIN_SIZE=1048576
MAX_SIZE=107374182400
# Filecoin client address for boostd command
FIL_CLIENT_ADDRESS=t3tejq3lb3szsq7spvttqohsfpsju2jof2dbive2qujgz2idqaj2etuolzgbmro3owsmpuebmoghwxgt6ricvq
# Start epoch offset for boostd command (optional)
# If specified, start-epoch will be calculated as: block_number + START_EPOCH_OFFSET
# If not specified, the boostd command will run without --start-epoch flag
# START_EPOCH_OFFSET=807
# Download directory for temporary files
DOWNLOAD_DIR=./downloads
# Optional: Delayed cleanup of successfully processed files (in hours)
# If specified, files will be automatically deleted after this many hours
# If not specified, files will remain until manually cleaned up
# DELAYED_CLEANUP_HOURS=24
# Optional: Starting block number to listen from (leave empty for latest)
START_BLOCK=
# Optional: Network name for logging
NETWORK_NAME=filecoinMake sure to set these required environment variables:
RPC_URL: Your blockchain endpoint (WebSocketwss://recommended for real-time events, or HTTPhttps://)CONTRACT_ADDRESS: The smart contract address to listen toPROVIDER_ID: Your provider ID to filter eventsMIN_SIZE: Minimum allocation size in bytes (e.g., 1048576 for 1MB)MAX_SIZE: Maximum allocation size in bytes (e.g., 107374182400 for 100GB)FIL_CLIENT_ADDRESS: Your Filecoin client address for boostd commandsDOWNLOAD_DIR: Directory for temporary file downloads (e.g., ./downloads)
START_EPOCH_OFFSET: Number of blocks to add to the current block number for start-epoch calculation (e.g., 807). If not specified, boostd commands will run without the --start-epoch flagDELAYED_CLEANUP_HOURS: Number of hours to wait before automatically deleting successfully processed files (e.g., 24). If not specified, files will remain until manually cleaned upSTART_BLOCK: Starting block number to fetch past events from (leave empty for latest)NETWORK_NAME: Network name for logging purposes
WebSocket (Recommended):
- Use
wss://URLs for real-time event listening - Maintains persistent connection for instant event delivery
- More efficient for continuous event monitoring
HTTP RPC (Alternative):
- Use
https://URLs for standard RPC connections - Uses polling mechanism which may have slight delays
- Good for testing or when WebSocket is not available
npm startnpm run devClean up old downloaded files:
npm run cleanupSet cleanup age threshold with environment variable:
CLEANUP_MAX_AGE_HOURS=48 npm run cleanup- Initialization: The server connects to the blockchain using the provided RPC URL
- Contract Setup: Initializes the smart contract interface with the AllocationCreated event ABI
- Event Listening: Starts listening for new
AllocationCreatedevents - Filtering: Only processes events where the
providermatches your configuredPROVIDER_ID - Processing: Calls the
processAllocation()method for matching events (ready for your business logic)
The server listens for events with this structure:
event AllocationCreated(
address indexed client,
uint64 indexed allocationId,
uint64 indexed provider,
bytes data,
uint64 size,
int64 termMin,
int64 termMax,
int64 expiration,
string downloadURL
);When an allocation matches your provider ID, the server automatically:
- Size Validation: Checks if the allocation size is within your configured MIN_SIZE and MAX_SIZE limits
- CID Conversion: Converts the data field (hex bytes) back to a Piece CID
- File Download: Downloads the file from the provided URL to your configured download directory
- Start Epoch Calculation: If START_EPOCH_OFFSET is configured, calculates start-epoch as block number + offset for better timing control
- Boost Integration: Executes the
boostd import-directcommand with the correct parameters and proper error handling - File Management: Manages downloaded files based on configuration - either schedules delayed cleanup or retains files for manual cleanup
The START_EPOCH_OFFSET environment variable allows you to control when your storage deals become active:
- When configured: The start-epoch is calculated as
block_number + START_EPOCH_OFFSET- Example: If an allocation event occurs at block 1000 and
START_EPOCH_OFFSET=807, the boostd command will use--start-epoch=1807
- Example: If an allocation event occurs at block 1000 and
- When not configured: The boostd command runs without the
--start-epochflag, allowing immediate activation - Block number unavailable: If the block number cannot be determined from the event, the command runs without start-epoch and logs a warning
This feature provides better control over deal timing and helps ensure deals activate at the appropriate blockchain epoch.
Due to the asynchronous nature of boostd import-direct, files cannot be deleted immediately after the command succeeds. The system provides multiple approaches for file cleanup:
Configure DELAYED_CLEANUP_HOURS to automatically delete files after a specified time:
DELAYED_CLEANUP_HOURS=24 # Delete files after 24 hoursThis schedules cleanup using in-memory timers. Files are deleted after the specified hours, allowing sufficient time for boostd to complete the import process.
Use the cleanup script to manually remove old files:
# Clean files older than 24 hours (default)
npm run cleanup
# Clean files older than 48 hours
CLEANUP_MAX_AGE_HOURS=48 npm run cleanupSet up automated cleanup with cron for production environments:
# Add to crontab (runs daily at 2 AM)
0 2 * * * cd /path/to/project && npm run cleanup- Successful imports: Files are retained based on your configuration
- Failed imports: Files are always kept for debugging purposes
- Manual override: Use the cleanup script anytime to remove old files
Make sure you have:
boostdinstalled and accessible in your PATH- Proper Filecoin node setup and configuration
- Network access to download files from the provided URLs
- Sufficient disk space in your download directory
The server handles graceful shutdown with Ctrl+C (SIGINT) or SIGTERM signals.
- Connection Issues: Verify your
RPC_URLis correct and accessible - No Events: Check that the
CONTRACT_ADDRESSis correct and has the AllocationCreated event - Provider Filtering: Ensure your
PROVIDER_IDmatches the expected format (uint64) - Block Number Issues: If you see warnings about missing block numbers with START_EPOCH_OFFSET configured, check your RPC provider supports full event metadata
- Boostd Command Failures: Check that boostd is properly installed and configured, and verify your FIL_CLIENT_ADDRESS is correct
- File Download Issues: Ensure network access to download URLs and sufficient disk space in your download directory
- File Cleanup Issues: If delayed cleanup isn't working, check that the process stays running, or use manual cleanup with cron jobs
- Disk Space: Monitor your download directory size and adjust cleanup frequency as needed