Documentation

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.

Important

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. 1

    Navigate to service directory

    cd cylend-service
  2. 2

    Configure environment variables

    Set OWNER_PRIVATE_KEY and other configs in .env (see Configuration)

  3. 3

    Start the service

    npm run dev

    For production: npm run build && npm run start

Caution

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 endpoint
SAPPHIRE_RPC_URLSapphire Testnet RPC URL
CORE_ADDRESSLendingCore contract address
OWNER_PRIVATE_KEYRequired: Owner wallet private key
POLL_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

✓ Connected to Ponder API
✓ Connected to Sapphire RPC
✓ Monitoring for actions...

Action Processing Logs

→ New action detected: 0xabc...123
→ Checking price staleness...
→ Processing action 0xabc...123
✓ Action processed successfully

Error Logs

✗ Failed to process action 0xabc...123
Retrying (attempt 1/3)...

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
Tip

See the Deployment Guide for detailed production deployment instructions.

Learn More