Backend Service
Automated service for processing encrypted actions on Sapphire
Why a Backend Service?
The backend service is a critical component that automates action processing on the Sapphire network. Here's why it's necessary:
Owner-Only Function
The processAction() function on LendingCore is protected with onlyOwner modifier and can only be called by the contract deployer.
Security
Calling this function directly from the frontend would expose the owner private key, creating a significant security risk.
Automation
Users shouldn't have to manually trigger action processing. The service monitors for new actions and processes them automatically.
Without the backend service, submitted actions will remain pending indefinitely. The service is required for production deployments.
How It Works
1. Event Monitoring
The service continuously monitors the Ponder GraphQL API for new EncryptedActionStored events on Sapphire. Polling interval is configurable (default: 10 seconds).
2. Price Validation
Before processing, the service checks if oracle prices are stale. If needed, it calls updatePriceFromRoflOracle() to fetch latest prices.
3. Action Processing
The service calls processAction(actionId) on the LendingCore contract using the owner private key. The contract decrypts the action, validates it, and executes the lending logic.
4. Error Handling & Retry
If processing fails (network issues, insufficient gas, etc.), the service retries with exponential backoff. Failed actions after max retries are logged for manual intervention.
Quick Start
Get the backend service running:
- 1
Navigate to service directory
cd cylend-service - 2
Configure environment variables
Set
OWNER_PRIVATE_KEYand other configs in.env(see Configuration) - 3
Start the service
npm run devFor production:
npm run build && npm run start
The OWNER_PRIVATE_KEY must be the deployer address with permissions to call processAction(). Store this securely using environment managers or secret vaults in production!
Configuration Options
Available environment variables:
PONDER_API_URLPonder GraphQL API endpointSAPPHIRE_RPC_URLSapphire Testnet RPC URLCORE_ADDRESSLendingCore contract addressOWNER_PRIVATE_KEYRequired: Owner wallet private keyPOLL_INTERVALPolling interval in ms (default: 10000)MAX_RETRIESMax retries for failed txs (default: 3)RETRY_DELAYDelay between retries in ms (default: 5000)LOG_LEVELLog level: debug|info|warn|error (default: info)Monitoring & Logs
The service provides detailed logging for monitoring:
Startup Logs
Action Processing Logs
Error Logs
Production Deployment
Best practices for deploying the service to production:
- ✓Use Process Manager: Deploy with PM2, systemd, or Docker for automatic restarts
- ✓Secure Private Keys: Use AWS Secrets Manager, Vault, or environment encryption
- ✓Monitor Logs: Set up log aggregation with CloudWatch, Datadog, or similar
- ✓Alerting: Configure alerts for processing failures and missed actions
- ✓Dedicated RPC: Use dedicated RPC endpoints (Alchemy, Infura) for reliability
See the Deployment Guide for detailed production deployment instructions.