Skip to main content

Subscriptions.sol

Overview

The Subscriptions.sol contract provides two payment models for continuous AI agent services on Nexis: epoch-based subscriptions (periodic fixed payments) and rate-based streams (continuous per-second payments). This dual-model approach supports both traditional subscription patterns and novel streaming payment use cases. Contract Location: /nexis-appchain/packages/contracts-bedrock/contracts/Subscriptions.sol

Key Features

  • Epoch Subscriptions: Fixed amount payments at regular intervals (daily, weekly, monthly)
  • Payment Streams: Continuous per-second payment flows with on-demand withdrawals
  • Multi-Asset Support: ETH and ERC20 token payments
  • Prefunding: Optional upfront funding for multiple periods
  • Flexible Cancellation: Cancel anytime with automatic refunds
  • Agent Integration: Direct payments to agent owners via Agents.sol

Architecture


Payment Models Comparison


Core Data Structures

Subscription


Stream


Contract Roles


Epoch-Based Subscriptions

createSubscription

Create a new epoch-based subscription to an agent.
Parameters: Returns: subscriptionId - Unique subscription identifier Payment:
  • For ETH: msg.value must equal amountPerEpoch × prefundEpochs
  • For ERC20: Caller must approve contract to spend amountPerEpoch × prefundEpochs
Emits:
  • SubscriptionCreated(...)
  • SubscriptionFunded(...) (if prefunded)
Example (Daily ETH Subscription):
Example (Monthly USDC Subscription):

fundSubscription

Add funds to an existing subscription.
Parameters:
  • subscriptionId: Subscription ID
  • epochs: Number of epochs to fund
Requirements:
  • Subscription must be active
Emits: SubscriptionFunded(uint256 indexed subscriptionId, uint256 amount, address indexed funder) Example:

processSubscription

Process a subscription payment (can be called by anyone).
Requirements:
  • Subscription is active
  • Current time >= nextCharge
  • Balance >= amountPerEpoch
Behavior:
  1. Deducts amountPerEpoch from balance
  2. Increments nextCharge by epochDuration
  3. Transfers payment to agent owner
  4. Emits SubscriptionProcessed
Emits: SubscriptionProcessed(uint256 indexed subscriptionId, address indexed recipient, uint256 amount, uint64 nextCharge) Example (Manual Processing):
Example (Automated Processing Bot):

cancelSubscription

Cancel a subscription and refund remaining balance.
Authorization: Subscription payer or DEFAULT_ADMIN_ROLE Behavior:
  • Sets subscription to inactive
  • Refunds remaining balance to payer
Emits: SubscriptionCancelled(uint256 indexed subscriptionId, address indexed payer, uint256 refundedAmount) Example:

getSubscription

Query subscription details.
Example:

Rate-Based Payment Streams

createStream

Create a continuous payment stream to an agent.
Parameters: Requirements:
  • totalAmount % (end - start) == 0 (evenly divisible rate)
  • end > start
Calculation:
Payment:
  • Full totalAmount must be provided upfront
Emits: StreamCreated(...) Example (7-Day Stream):
Example (Hourly Rate Stream):

withdrawFromStream

Withdraw accrued stream payments (agent owner only).
Authorization: Agent owner (from Agents.sol) Behavior:
  • Calculates withdrawable amount based on elapsed time
  • Transfers to agent owner
  • Updates withdrawn amount
Formula:
Emits: StreamWithdrawn(uint256 indexed streamId, address indexed recipient, uint256 amount) Example:

cancelStream

Cancel a stream and refund unstreamed portion.
Authorization: Stream payer or DEFAULT_ADMIN_ROLE Behavior:
  1. Calculates and transfers any withdrawable amount to agent owner
  2. Calculates unstreamed amount
  3. Refunds unstreamed amount to payer
  4. Sets stream inactive
Emits:
  • StreamWithdrawn(...) (if withdrawable > 0)
  • StreamCancelled(uint256 indexed streamId, address indexed payer, uint256 refundedAmount)
Example:

getStream

Query stream details.
Example:

Events Reference

Subscription Events

Stream Events


Use Cases & Examples

Use Case 1: SaaS API Subscription


Use Case 2: Pay-Per-Inference (Streaming)


Use Case 3: Tiered Subscription with Auto-Renewal


Use Case 4: Escrow with Time-Release


Admin Functions

pause / unpause

Emergency pause contract operations.

setAgents

Update Agents.sol contract reference.

Integration Patterns

Subscription Dashboard


Stream Dashboard


Security Considerations

Reentrancy Protection

All state-changing functions with transfers use nonReentrant modifier.

Rate Calculation

  • Stream rates must divide evenly (totalAmount % duration == 0)
  • Prevents rounding errors and dust amounts

Authorization

  • Only agent owners can withdraw from streams
  • Only payers (or admin) can cancel subscriptions/streams

Time Manipulation

  • Uses block.timestamp for time calculations
  • Miners can manipulate up to ~15 seconds
  • Not critical for hourly/daily payments

Gas Optimization

  1. Subscription Processing: Use bots to batch-process multiple subscriptions
  2. Stream Withdrawals: Agents should withdraw periodically (e.g., weekly) rather than frequently
  3. Prefunding: Pay for multiple epochs upfront to reduce transaction costs

  • Agents.sol - Validates agent existence and retrieves owner addresses
  • Tasks.sol - Alternative one-time payment model
  • Treasury.sol - Could integrate for protocol fees on subscriptions

ABI & Deployment

ABI Location: /nexis-appchain/packages/contracts-bedrock/artifacts/contracts/Subscriptions.sol/Subscriptions.json Network Addresses:

Support & Resources