Skip to content

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 SourceContractPurpose
FactoryRegistryFactoryRegistryTracks factory registrations and market registry events
DCPPFactoryDCPPFactoryIndexes binary market creation events
MultiOutcomeMarketFactoryMultiOutcomeMarketFactoryIndexes N-outcome market creation events

Template Data Sources

Templates are instantiated dynamically when new contracts are deployed:

TemplateContractTrigger
BinaryCPMMBinaryCPMMCreated 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:

EntityDescription
MarketA prediction market with metadata, reserves, status, and settlement info
TradeAn individual buy or sell transaction on a market
UserA wallet address that has interacted with the platform
UserPositionA user's current holdings in a specific market (YES/NO shares, cost basis)
MultiOutcomeMarketAn N-option market with linked outcome slots
AMMMarketMappingLinks a BinaryCPMM address to its parent Market entity
VolumeStatsAggregated 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:

EventTrade TypeDescription
CollateralSwappedForOutcomebuyUser purchased outcome tokens with collateral
OutcomeSoldForCollateralsellUser sold outcome tokens back for collateral
SwappedsellToken-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

ParameterValue
NetworkBSC Chapel (testnet, chain ID 97)
Start BlockConfigured per data source based on deployment block
HostingGoldsky managed service
Query EndpointProvided 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.