Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
337 changes: 337 additions & 0 deletions packages/subgraph/abis/GraphPayments.json

Large diffs are not rendered by default.

680 changes: 680 additions & 0 deletions packages/subgraph/abis/PaymentsEscrow.json

Large diffs are not rendered by default.

189 changes: 174 additions & 15 deletions packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ type GraphNetwork @entity(immutable: false) {
id: Bytes!

# Counts
"Active service providers"
"Active service providers (tokensStaked > 0)"
countServiceProviders: Int!
"Active data services (verifiers)"
"Active data services (at least one provision with tokens > 0)"
countDataServices: Int!
"Active provisions"
"Active provisions (tokens > 0)"
countProvisions: Int!
"Active delegation pools"
"Active delegation pools (tokens > 0)"
countDelegationPools: Int!
"Provision slash events"
"Active payers (tokensEscrowed > 0)"
countPayers: Int!
"Active collectors (tokensEscrowed > 0)"
countCollectors: Int!
"Active escrow accounts (tokens > 0)"
countEscrowAccounts: Int!
"Total provision slash events (cumulative)"
countProvisionSlashEvents: Int!
"Delegation pool slash events"
"Total delegation pool slash events (cumulative)"
countDelegationPoolSlashEvents: Int!

# Stake aggregates
Expand All @@ -35,6 +41,24 @@ type GraphNetwork @entity(immutable: false) {
tokensSlashedFromProvisions: BigInt!
"Total tokens slashed from delegation pools"
tokensSlashedFromDelegationPools: BigInt!

# Payment collection aggregates
"Total tokens collected in the protocol"
tokensCollected: BigInt!
"Tokens burned as protocol tax"
tokensDistributedAsProtocolTax: BigInt!
"Tokens distributed to service providers"
tokensDistributedToServiceProviders: BigInt!
"Tokens distributed to delegation pools"
tokensDistributedToDelegationPools: BigInt!
"Tokens distributed to data services"
tokensDistributedToDataServices: BigInt!

# Payment escrow aggregates
"Total tokens held in escrow"
tokensEscrowed: BigInt!
"Total tokens currently thawing in escrow"
tokensThawingFromEscrow: BigInt!
}

type ServiceProvider @entity(immutable: false) {
Expand All @@ -50,13 +74,17 @@ type ServiceProvider @entity(immutable: false) {
provisionThawRequests: [ProvisionThawRequest!]! @derivedFrom(field: "serviceProvider")
"Operator authorizations for this service provider"
operatorAuthorizations: [OperatorAuthorization!]! @derivedFrom(field: "serviceProvider")
"Escrow accounts where this service provider is the receiver"
escrowAccounts: [EscrowAccount!]! @derivedFrom(field: "serviceProvider")

# Counts
"Number of active provisions"
"Active provisions for this service provider (tokens > 0)"
countProvisions: Int!
"Provision slash events"
"Active escrow accounts for this service provider (tokens > 0)"
countEscrowAccounts: Int!
"Total provision slash events for this service provider (cumulative)"
countProvisionSlashEvents: Int!
"Delegation pool slash events"
"Total delegation pool slash events for this service provider (cumulative)"
countDelegationPoolSlashEvents: Int!

# Stake
Expand All @@ -75,6 +103,22 @@ type ServiceProvider @entity(immutable: false) {
"Tokens currently thawing from delegation pools"
tokensDelegatedThawing: BigInt!

# Payment collection
"Total tokens collected for this service provider"
tokensCollected: BigInt!
"Tokens distributed to this service provider"
tokensDistributedToServiceProvider: BigInt!
"Tokens distributed as protocol tax from this service provider's collections"
tokensDistributedAsProtocolTax: BigInt!
"Tokens distributed to delegation pools from this service provider's collections"
tokensDistributedToDelegationPools: BigInt!
"Tokens distributed to data services from this service provider's collections"
tokensDistributedToDataServices: BigInt!

# Escrow
"Total tokens in escrow for this service provider"
tokensEscrowed: BigInt!

# Slashing
"Total tokens slashed"
tokensSlashed: BigInt!
Expand Down Expand Up @@ -109,15 +153,15 @@ type DataService @entity(immutable: false) {
operatorAuthorizations: [OperatorAuthorization!]! @derivedFrom(field: "dataService")

# Counts
"Active service providers with provisions to this data service"
"Active service providers with provisions to this data service (provision tokens > 0)"
countServiceProviders: Int!
"Active provisions"
"Active provisions to this data service (tokens > 0)"
countProvisions: Int!
"Active delegation pools"
"Active delegation pools for this data service (tokens > 0)"
countDelegationPools: Int!
"Provision slash events"
"Total provision slash events for this data service (cumulative)"
countProvisionSlashEvents: Int!
"Delegation pool slash events"
"Total delegation pool slash events for this data service (cumulative)"
countDelegationPoolSlashEvents: Int!

# Tokens
Expand All @@ -138,6 +182,18 @@ type DataService @entity(immutable: false) {
"Tokens slashed from delegation pools"
tokensSlashedFromDelegationPools: BigInt!

# Payment collection
"Total tokens collected through this data service"
tokensCollected: BigInt!
"Tokens distributed to this data service (data service cut)"
tokensDistributedToDataService: BigInt!
"Tokens distributed as protocol tax from collections through this data service"
tokensDistributedAsProtocolTax: BigInt!
"Tokens distributed to delegation pools from collections through this data service"
tokensDistributedToDelegationPools: BigInt!
"Tokens distributed to service providers from collections through this data service"
tokensDistributedToServiceProviders: BigInt!

# Metadata
"Block number when entity was created"
createdAtBlock: BigInt!
Expand Down Expand Up @@ -166,6 +222,10 @@ type DelegationPool @entity(immutable: false) {
shares: BigInt!
"Tokens currently thawing"
tokensThawing: BigInt!
"Total tokens distributed to this pool from payments"
tokensDistributed: BigInt!
"Total tokens slashed from this pool"
tokensSlashed: BigInt!

# Legacy delegation parameters (only set for legacy pools migrated from pre-Horizon)
"[Legacy] Percentage of indexing rewards for the indexer, in PPM. Used to calculate delegation rewards from legacy allocations."
Expand Down Expand Up @@ -201,6 +261,10 @@ type Provision @entity(immutable: false) {
tokens: BigInt!
"Tokens currently thawing"
tokensThawing: BigInt!
"Total tokens collected through this provision"
tokensCollected: BigInt!
"Total tokens slashed from this provision"
tokensSlashed: BigInt!

# Parameters
"Maximum cut the verifier can take (PPM)"
Expand Down Expand Up @@ -300,7 +364,7 @@ type Operator @entity(immutable: false) {
authorizations: [OperatorAuthorization!]! @derivedFrom(field: "operator")

# Counts
"Active authorizations (allowed=true)"
"Active authorizations for this operator (allowed == true)"
countAuthorizations: Int!

# Metadata
Expand Down Expand Up @@ -340,3 +404,98 @@ type OperatorAuthorization @entity(immutable: false) {
"Timestamp when entity was last updated"
updatedAt: BigInt!
}

type Payer @entity(immutable: false) {
"Payer address"
id: Bytes!

# Relationships
"Escrow accounts funded by this payer"
escrowAccounts: [EscrowAccount!]! @derivedFrom(field: "payer")

# Counts
"Active escrow accounts funded by this payer (tokens > 0)"
countEscrowAccounts: Int!

# Tokens
"Total tokens in escrow"
tokensEscrowed: BigInt!
"Total tokens thawing"
tokensThawing: BigInt!
"Total tokens collected from escrow"
tokensCollected: BigInt!

# Metadata
"Block number when entity was created"
createdAtBlock: BigInt!
"Timestamp when entity was created"
createdAt: BigInt!
"Block number when entity was last updated"
updatedAtBlock: BigInt!
"Timestamp when entity was last updated"
updatedAt: BigInt!
}

type Collector @entity(immutable: false) {
"Collector contract address"
id: Bytes!

# Relationships
"Escrow accounts using this collector"
escrowAccounts: [EscrowAccount!]! @derivedFrom(field: "collector")

# Counts
"Active escrow accounts using this collector (tokens > 0)"
countEscrowAccounts: Int!

# Tokens
"Total tokens in escrow"
tokensEscrowed: BigInt!
"Total tokens thawing"
tokensThawing: BigInt!
"Total tokens collected"
tokensCollected: BigInt!

# Metadata
"Block number when entity was created"
createdAtBlock: BigInt!
"Timestamp when entity was created"
createdAt: BigInt!
"Block number when entity was last updated"
updatedAtBlock: BigInt!
"Timestamp when entity was last updated"
updatedAt: BigInt!
}

type EscrowAccount @entity(immutable: false) {
"Concatenation of payer, collector, and service provider addresses"
id: Bytes!

# Relationships
"Payer that deposited funds into the escrow account"
payer: Payer!
"Collector allowed to withdraw funds from the account"
collector: Collector!
"Service provider that can receive funds from the account"
serviceProvider: ServiceProvider!

# Tokens
"Available tokens"
tokens: BigInt!
"Tokens currently thawing"
tokensThawing: BigInt!
"Timestamp when thawing completes (0 if not thawing)"
thawEndTimestamp: BigInt!
"Total tokens collected from this escrow account"
tokensCollected: BigInt!

# Metadata
"Block number when entity was created"
createdAtBlock: BigInt!
"Timestamp when entity was created"
createdAt: BigInt!
"Block number when entity was last updated"
updatedAtBlock: BigInt!
"Timestamp when entity was last updated"
updatedAt: BigInt!
}
48 changes: 48 additions & 0 deletions packages/subgraph/src/entities/collector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { BigInt, Bytes, ethereum } from "@graphprotocol/graph-ts"
import { Collector } from "../../generated/schema"
import { BIGINT_ZERO } from "../common/constants"

export class CollectorResult {
entity: Collector
isNew: boolean

constructor(entity: Collector, isNew: boolean) {
this.entity = entity
this.isNew = isNew
}
}

/**
* Gets or creates a Collector entity.
* Collectors are contracts authorized to collect payments from escrow accounts.
*/
export function getOrCreateCollector(id: Bytes, blockNumber: BigInt, timestamp: BigInt): CollectorResult {
let entity = Collector.load(id)
let isNew = entity == null

if (entity == null) {
entity = new Collector(id)

// Counts
entity.countEscrowAccounts = 0

// Tokens
entity.tokensEscrowed = BIGINT_ZERO
entity.tokensThawing = BIGINT_ZERO
entity.tokensCollected = BIGINT_ZERO

// Metadata
entity.createdAtBlock = blockNumber
entity.createdAt = timestamp
entity.updatedAtBlock = blockNumber
entity.updatedAt = timestamp
}

return new CollectorResult(entity, isNew)
}

export function saveCollector(collector: Collector, block: ethereum.Block): void {
collector.updatedAtBlock = block.number
collector.updatedAt = block.timestamp
collector.save()
}
11 changes: 11 additions & 0 deletions packages/subgraph/src/entities/dataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export class DataServiceResult {
}
}

/**
* Gets or creates a DataService entity.
* Data services (verifiers) are contracts that service providers provision stake to.
*/
export function getOrCreateDataService(
id: Bytes,
blockNumber: BigInt,
Expand Down Expand Up @@ -41,6 +45,13 @@ export function getOrCreateDataService(
entity.tokensSlashedFromProvisions = BIGINT_ZERO
entity.tokensSlashedFromDelegationPools = BIGINT_ZERO

// Payments
entity.tokensCollected = BIGINT_ZERO
entity.tokensDistributedToDataService = BIGINT_ZERO
entity.tokensDistributedAsProtocolTax = BIGINT_ZERO
entity.tokensDistributedToDelegationPools = BIGINT_ZERO
entity.tokensDistributedToServiceProviders = BIGINT_ZERO

// Metadata
entity.createdAtBlock = blockNumber
entity.createdAt = timestamp
Expand Down
8 changes: 8 additions & 0 deletions packages/subgraph/src/entities/delegationPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ export function getOrCreateDelegationPool(

if (entity == null) {
entity = new DelegationPool(id)

// Relationships
entity.serviceProvider = serviceProvider
entity.dataService = dataService

// Pool state
entity.tokens = BIGINT_ZERO
entity.shares = BIGINT_ZERO
entity.tokensThawing = BIGINT_ZERO
entity.tokensDistributed = BIGINT_ZERO
entity.tokensSlashed = BIGINT_ZERO

// Metadata
entity.createdAtBlock = blockNumber
entity.createdAt = timestamp
entity.updatedAtBlock = blockNumber
Expand Down
Loading
Loading