The PolkaMesh Backend Service is a comprehensive NestJS-powered automation engine that bridges blockchain events with AI job execution. It serves as the central orchestrator for the PolkaMesh ecosystem, handling event monitoring, job processing, attestation generation, and payment automation.
This production-ready backend service provides:
- π Event Automation: Real-time monitoring of 6 deployed smart contracts
- β‘ Job Execution: AI inference and training job processing
- π‘οΈ Attestation: Cryptographic proof generation for job results
- π° Payment Flow: Automated escrow release and settlement
- π Analytics: Comprehensive metrics and monitoring
- π API Gateway: RESTful endpoints for frontend integration
- π³ Containerized: Docker deployment ready
PolkaMesh-Backend-Service/
βββ src/
β βββ modules/
β β βββ blockchain/ # Polkadot.js integration
β β βββ contracts/ # Smart contract services
β β βββ jobs/ # AI job processing
β β βββ attestation/ # Proof generation
β βββ common/ # Shared utilities
β βββ config/ # Environment configuration
β βββ main.ts # Application entry point
βββ abis/ # Contract ABI files
βββ docker-compose.yml # Container orchestration
βββ README.md # This file
graph TD
subgraph "External"
U[Users/DApps]
P[Paseo Testnet]
end
subgraph "PolkaMesh Backend Service"
API[REST API Gateway]
BL[Blockchain Listener]
JE[Job Executor]
AG[Attestation Generator]
PM[Payment Manager]
DB[(Database)]
end
subgraph "Smart Contracts"
AJQ[AI Job Queue]
PE[Payment Escrow]
CPR[Provider Registry]
DNR[Data NFT Registry]
PJP[Phala Job Processor]
MEV[MEV Protection]
end
U --> API
API --> BL
BL --> P
P --> AJQ
P --> PE
P --> CPR
P --> DNR
P --> PJP
P --> MEV
BL --> JE
JE --> AG
AG --> PM
PM --> PE
API --> DB
JE --> DB
AG --> DB
sequenceDiagram
participant User
participant API as Backend API
participant BL as Blockchain Listener
participant JE as Job Executor
participant AG as Attestation Gen
participant Contract as Smart Contract
User->>Contract: Submit Job
Contract->>BL: JobSubmitted Event
BL->>JE: Process Job
JE->>JE: Execute AI Task
JE->>AG: Generate Proof
AG->>Contract: Submit Attestation
Contract->>Contract: Release Payment
API->>User: Job Complete Notification
| Endpoint | Method | Description |
|---|---|---|
/health |
GET | Service health check |
/status |
GET | System status and metrics |
/contracts |
GET | Contract deployment info |
| Endpoint | Method | Description |
|---|---|---|
/jobs |
GET | List all jobs |
/jobs/:id |
GET | Get job details |
/jobs/:id/status |
GET | Get job status |
/jobs/:id/result |
GET | Get job result |
| Endpoint | Method | Description |
|---|---|---|
/metrics |
GET | Performance metrics |
/events |
GET | Recent blockchain events |
/stats |
GET | System statistics |
// Get service status
const response = await fetch("http://localhost:3000/status");
const status = await response.json();
// Get job details
const job = await fetch("http://localhost:3000/jobs/123");
const jobData = await job.json();
// Get system metrics
const metrics = await fetch("http://localhost:3000/metrics");
const metricsData = await metrics.json();flowchart TD
A[JobSubmitted Event] --> B[Event Listener]
B --> C{Validate Job}
C -->|Valid| D[Add to Queue]
C -->|Invalid| E[Log Error]
D --> F[Job Executor]
F --> G[Process AI Task]
G --> H{Execution Success?}
H -->|Success| I[Generate Attestation]
H -->|Failure| J[Retry Logic]
I --> K[Submit to Chain]
K --> L[Release Payment]
J --> M{Max Retries?}
M -->|No| F
M -->|Yes| N[Mark Failed]
L --> O[Update Status]
N --> O
graph TD
subgraph "Event Sources"
AJQ[AI Job Queue Events]
PE[Payment Escrow Events]
PJP[Phala Processor Events]
MEV[MEV Protection Events]
end
subgraph "Event Processing"
EL[Event Listener]
EF[Event Filter]
EP[Event Processor]
end
subgraph "Action Handlers"
JH[Job Handler]
PH[Payment Handler]
AH[Attestation Handler]
MH[MEV Handler]
end
AJQ --> EL
PE --> EL
PJP --> EL
MEV --> EL
EL --> EF
EF --> EP
EP --> JH
EP --> PH
EP --> AH
EP --> MH
src/
βββ modules/
β βββ blockchain/
β β βββ blockchain.module.ts
β β βββ blockchain.service.ts
β β βββ event-listener.service.ts
β βββ contracts/
β β βββ contracts.module.ts
β β βββ payment-escrow.service.ts
β β βββ job-queue.service.ts
β β βββ phala-processor.service.ts
β β βββ mev-protection.service.ts
β βββ jobs/
β β βββ jobs.module.ts
β β βββ job-executor.service.ts
β β βββ model-runner.service.ts
β βββ attestation/
β βββ attestation.module.ts
β βββ attestation-generator.service.ts
βββ common/
β βββ guards/
β βββ interceptors/
β βββ pipes/
βββ config/
β βββ configuration.ts
βββ main.ts
# Development
npm run start:dev # Start with hot reload
npm run start:debug # Start with debugging
npm run start:prod # Production build and start
# Building
npm run build # Build for production
npm run prebuild # Pre-build setup
# Testing
npm run test # Run unit tests
npm run test:e2e # Run end-to-end tests
npm run test:cov # Test coverage report
# Code Quality
npm run lint # ESLint check
npm run lint:fix # Fix linting issues
npm run format # Prettier formatting
# Docker
npm run docker:build # Build Docker image
npm run docker:run # Run Docker container# Run all tests
npm test
# Run specific test suite
npm test -- --testNamePattern="BlockchainService"
# Run tests with coverage
npm run test:cov
# E2E testing
npm run test:e2e# 1. Build production image
docker build -t polkamesh-backend .
# 2. Run with environment variables
docker run -d \
--name polkamesh-backend \
-p 3000:3000 \
--env-file .env.production \
polkamesh-backend
# 3. Using Docker Compose (Recommended)
docker-compose -f docker-compose.prod.yml up -d# .env.production
NODE_ENV=production
PORT=3000
LOG_LEVEL=info
# Enhanced Security
JWT_SECRET=your-production-secret
API_RATE_LIMIT=1000
# Performance Optimization
MAX_CONCURRENT_JOBS=50
JOB_TIMEOUT=600
CACHE_TTL=3600{
"status": "ok",
"uptime": 3600,
"contracts": {
"paymentEscrow": "active",
"jobQueue": "active",
"phalaProcessor": "active",
"mevProtection": "active"
},
"performance": {
"totalJobs": 1250,
"successRate": "95.8%",
"avgProcessingTime": "2.3s"
}
}- Fork the repository
- Create feature branch:
git checkout -b feature/your-feature - Implement changes with tests
- Ensure linting passes:
npm run lint - Submit pull request
- TypeScript with strict type checking
- ESLint with Airbnb configuration
- Jest for testing
- NestJS best practices
Service connection failure:
# Check RPC endpoint
curl -X POST -H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"system_health","params":[],"id":1}' \
wss://rpc1.paseo.popnetwork.xyzHigh memory usage:
# Monitor memory
docker stats polkamesh-backend| Repository | Purpose | Status |
|---|---|---|
| PolkaMesh-Contracts | Smart contracts | β Deployed |
| PolkaMesh-SDK | TypeScript SDK | β Published |
| PolkaMesh-Frontend | Web interface | β Live |
| phala_phat_contract | TEE worker | β Ready |
Apache-2.0 - see LICENSE file for details
# Setup and run
git clone <repository>
cd PolkaMesh-Backend-Service
npm install
cp .env.example .env
npm run start:dev
# Production deployment
docker-compose up -dBuilt with β€οΈ for the Polkadot ecosystem using NestJS