Indexing Layer
CloudBank uses The Graph protocol hosted on Goldsky to index on-chain events into a queryable GraphQL API. The subgraph transforms raw blockchain events into structured entities optimized for frontend consumption.
Overview
The subgraph is written in AssemblyScript (a TypeScript-like language that compiles to WebAssembly) and deployed to Goldsky's hosted indexing service. The current deployment targets BSC Chapel (BNB Smart Chain testnet).
Data Sources
The subgraph tracks events from multiple contract types, configured as both direct data sources and templates.
Direct Data Sources
These are contracts deployed at known, fixed addresses:
| Data Source | Contract | Purpose |
|---|---|---|
| FactoryRegistry | FactoryRegistry | Tracks factory registrations and market registry events |
| DCPPFactory | DCPPFactory | Indexes binary market creation events |
| MultiOutcomeMarketFactory | MultiOutcomeMarketFactory | Indexes N-outcome market creation events |
Template Data Sources
Templates are instantiated dynamically when new contracts are deployed:
| Template | Contract | Trigger |
|---|---|---|
| BinaryCPMM | BinaryCPMM | Created by DCPPFactory for each new market |
When a MarketCreatedWithAMM event is detected, the mapping creates a new BinaryCPMM template instance to begin indexing that market's trading activity.
Entity Schema
The subgraph defines the following core entities:
| Entity | Description |
|---|---|
| Market | A prediction market with metadata, reserves, status, and settlement info |
| Trade | An individual buy or sell transaction on a market |
| User | A wallet address that has interacted with the platform |
| UserPosition | A user's current holdings in a specific market (YES/NO shares, cost basis) |
| MultiOutcomeMarket | An N-option market with linked outcome slots |
| AMMMarketMapping | Links a BinaryCPMM address to its parent Market entity |
| VolumeStats | Aggregated trading volume statistics (daily, weekly, all-time) |
Event Handlers
Market Creation
The MarketCreatedWithAMM event from DCPPFactory triggers the creation of a new Market entity with:
- Question text, category, and end time.
- Initial YES/NO reserves from the seed liquidity.
- Creator address and stake amount.
- A new BinaryCPMM template instance for trade indexing.
Trade Indexing
Trading events on each BinaryCPMM instance are mapped to Trade entities:
| Event | Trade Type | Description |
|---|---|---|
CollateralSwappedForOutcome | buy | User purchased outcome tokens with collateral |
OutcomeSoldForCollateral | sell | User sold outcome tokens back for collateral |
Swapped | sell | Token-to-token swap (recorded as a sell with swap metadata) |
Each trade updates the associated Market reserves, the User entity's trade count, and the UserPosition cost basis and share balance.
PnL Calculation
The subgraph computes realized profit and loss for each user position on the fly:
realizedPnL = proceeds - (costBasis * sharesSold / totalShares)- costBasis — The cumulative collateral spent acquiring the position.
- sharesSold — The number of shares disposed of in the current transaction.
- totalShares — The user's total share balance before the transaction.
- proceeds — The collateral received from the sale.
This proportional cost-basis method ensures accurate PnL tracking even when positions are built up across multiple purchases at different prices.
IPFS Metadata
Market metadata (detailed descriptions, category tags, resolution sources) is stored on IPFS and referenced by CID in the market creation event. The subgraph fetches this metadata during indexing to extract:
- Category — Used for frontend filtering (Sports, Crypto, Politics, etc.).
- Subcategory — Finer-grained classification within each category.
- Image URL — Thumbnail for market cards in the UI.
IPFS fetching is handled via The Graph's built-in IPFS data source capability with retry logic for transient gateway failures.
Network Configuration
| Parameter | Value |
|---|---|
| Network | BSC Chapel (testnet, chain ID 97) |
| Start Block | Configured per data source based on deployment block |
| Hosting | Goldsky managed service |
| Query Endpoint | Provided by Goldsky after deployment |
The subgraph is deployed via the Goldsky CLI as part of the CI/CD pipeline, with separate deployments for development and staging environments.