Architecture

System Overview

Pactum is a three-tier system: a Web Application application serving both the dashboard UI and the REST API, Database as the persistence layer, and Arc Testnet as the settlement layer.


Component Relationships

Authentication

The system uses two separate authentication mechanisms:

ContextMechanismDetails
Dashboard (merchant)Session cookieEmail/password login with bcrypt hashing. Session stored as pactum_session cookie containing the user ID.
SDK/API (third-party)API key headerX-API-Key header containing a pactum_-prefixed key. The key is SHA-256 hashed and looked up in api_keys_pactum.

Data Flow — Usage Tracking

Data Flow — Settlement


Technology Stack

LayerTechnologyPurpose
FrontendWeb Application 16, React 19, Tailwind CSSDashboard UI and wallet pages
BackendWeb Application API RoutesRESTful API endpoints (serverless)
DatabaseDatabase (PostgreSQL)Data persistence with Row Level Security
Authenticationbcryptjs, HTTP-only cookiesDashboard session management
BlockchainArc TestnetOn-chain settlement layer (USDC-native gas)
Smart ContractSolidity 0.8.20PactumBilling — deposit, settlement, withdrawal
On-chain ClientviemContract interactions from the backend
StylingTailwind CSS, custom design tokensLedger-inspired visual language

Directory Structure

pactum/
├── app/
│   ├── api/
│   │   ├── auth/                 # Login and signup endpoints
│   │   └── v1/
│   │       ├── usage/track/      # Usage event recording (SDK endpoint)
│   │       ├── keys/             # API key CRUD
│   │       ├── policies/         # Spend policy management
│   │       ├── invoices/         # Invoice generation and listing
│   │       ├── settlement/cron/  # On-chain batch settlement trigger
│   │       ├── settle/           # Manual settlement trigger
│   │       ├── wallet/balance/   # Off-chain pending usage balance
│   │       └── receipts/[id]/    # Settlement receipt details
│   ├── dashboard/
│   │   ├── page.tsx              # Overview with live usage stats
│   │   ├── usage/                # Detailed usage analytics
│   │   ├── payouts/              # Settlement history and withdrawal
│   │   └── settings/             # Project settings, API keys, wallet config
│   ├── wallet/                   # End-user USDC deposit page
│   ├── login/                    # Email/password login
│   └── signup/                   # Registration
├── contracts/
│   └── PactumBilling.sol         # Core billing smart contract
├── lib/
│   ├── arc/config.ts             # Arc Testnet chain configuration and ABIs
│   ├── Database/                 # Database client (browser + admin)
│   ├── api-keys.ts               # Key generation, hashing, validation
│   ├── auth.ts                   # Password hashing, session cookies
│   └── invoices.ts               # Invoice aggregation logic
├── components/                   # Shared React UI components
├── database/migrations/          # SQL migration files (run in order)
└── tokens.css                    # Design token definitions

Key Design Decisions

Off-Chain Metering, On-Chain Settlement

Usage events are recorded in PostgreSQL (Database) for speed and cost efficiency. Individual API calls do not produce on-chain transactions. Instead, usage is aggregated and settled in batches via the batchSettleUsage contract function. This keeps per-request latency low while maintaining on-chain auditability.

State Channel Pattern

The system implements a State Channel pattern:

  1. Users deposit USDC into the PactumBilling smart contract.
  2. API usage is tracked off-chain with balance checks against both the on-chain deposit and accumulated pending usage.
  3. A settlement cron aggregates pending events and executes a single on-chain batch transfer.
  4. After settlement, usage event statuses transition from pending_settlement to settled.

Secure Backend Gateway

All database access is strictly routed through the Web Application API layer. Direct client database access is blocked at the infrastructure level, ensuring that sensitive data operations are fully controlled and audited by the application backend.