Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add PaymentSource entities for disambiguation on query fee payments #275

Merged
merged 2 commits into from
Feb 6, 2025
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
42 changes: 42 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,48 @@ type Indexer @entity {
annualizedReturn: BigDecimal! # You must multiple by 100 to get percentage
"NOT IMPLEMENTED - Staking efficiency of the indexer"
stakingEfficiency: BigDecimal!

# Query fees breakdown by payment address

}

type PaymentSource @entity {
"Address used on the payment"
id: ID!

"Total query fees generated in the network"
totalQueryFees: BigInt!
"Total query fees collected by indexers"
totalIndexerQueryFeesCollected: BigInt!
"Total query fees rebates claimed by indexers"
totalIndexerQueryFeeRebates: BigInt!
"Total query fees rebates claimed by delegators"
totalDelegatorQueryFeeRebates: BigInt!
"Total query fees payed to curators"
totalCuratorQueryFees: BigInt!
"Total protocol taxes applied to the query fees"
totalTaxedQueryFees: BigInt!
# It is hard to separate the unclaimed and rebates lost
"Total unclaimed rebates. Includes unclaimed rebates, and rebates lost in rebates mechanism "
totalUnclaimedQueryFeeRebates: BigInt!

graphNetwork: GraphNetwork! # to make a derived list on GraphNetwork
}

type IndexerQueryFeePaymentAggregation @entity {
"Join ID of indexer address and PaymentSource address"
id: ID!

"Total query fees collected. Includes the portion given to delegators"
queryFeesCollected: BigInt!
"Query fee rebate amount claimed from the protocol through rebates mechanism. Does not include portion given to delegators"
queryFeeRebates: BigInt!
"The total amount of query fees given to delegators"
delegatorQueryFees: BigInt!

indexer: Indexer!

paymentSource: PaymentSource!
}

"""
Expand Down
40 changes: 40 additions & 0 deletions src/mappings/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
DelegatedStake,
NameSignalSubgraphRelation,
CurrentSubgraphDeploymentRelation,
PaymentSource,
IndexerQueryFeePaymentAggregation,
} from '../../types/schema'
import {
SubgraphDeploymentManifest as SubgraphDeploymentManifestTemplate
Expand Down Expand Up @@ -185,6 +187,44 @@ export function createOrLoadIndexer(indexerAddress: Bytes, timestamp: BigInt): I
return indexer as Indexer
}

export function createOrLoadPaymentSource(paymentAddress: Bytes): PaymentSource {
let id = paymentAddress.toHexString()
let paymentSource = PaymentSource.load(id)
if (paymentSource == null) {
paymentSource = new PaymentSource(id)
paymentSource.graphNetwork = "1"
paymentSource.totalQueryFees = BigInt.fromI32(0)
paymentSource.totalIndexerQueryFeesCollected = BigInt.fromI32(0)
paymentSource.totalIndexerQueryFeeRebates = BigInt.fromI32(0)
paymentSource.totalDelegatorQueryFeeRebates = BigInt.fromI32(0)
paymentSource.totalCuratorQueryFees = BigInt.fromI32(0)
paymentSource.totalTaxedQueryFees = BigInt.fromI32(0)
paymentSource.totalUnclaimedQueryFeeRebates = BigInt.fromI32(0)

paymentSource.save()
}

return paymentSource as PaymentSource
}

export function createOrLoadIndexerQueryFeePaymentAggregation(paymentAddress: Bytes, indexerAddress: Bytes): IndexerQueryFeePaymentAggregation {
let id = paymentAddress.toHexString().concat("-").concat(indexerAddress.toHexString())
let aggregation = IndexerQueryFeePaymentAggregation.load(id)
if (aggregation == null) {
aggregation = new IndexerQueryFeePaymentAggregation(id)
aggregation.indexer = indexerAddress.toHexString()
aggregation.paymentSource = paymentAddress.toHexString()
aggregation.queryFeesCollected = BigInt.fromI32(0)
aggregation.queryFeeRebates = BigInt.fromI32(0)
aggregation.delegatorQueryFees = BigInt.fromI32(0)

aggregation.save()
}

return aggregation as IndexerQueryFeePaymentAggregation
}


export function createOrLoadDelegator(delegatorAddress: Bytes, timestamp: BigInt): Delegator {
let id = delegatorAddress.toHexString()
let delegator = Delegator.load(id)
Expand Down
54 changes: 54 additions & 0 deletions src/mappings/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
GraphAccount,
Delegator,
DelegatedStake,
IndexerQueryFeePaymentAggregation,
} from '../types/schema'

import {
Expand All @@ -45,6 +46,8 @@ import {
batchUpdateSubgraphSignalledTokens,
createOrLoadGraphNetwork,
calculateCapacities,
createOrLoadIndexerQueryFeePaymentAggregation,
createOrLoadPaymentSource,
} from './helpers/helpers'
import { addresses } from '../../config/addresses'

Expand Down Expand Up @@ -394,12 +397,17 @@ export function handleAllocationCollected(event: AllocationCollected): void {
let subgraphDeploymentID = event.params.subgraphDeploymentID.toHexString()
let indexerID = event.params.indexer.toHexString()
let allocationID = event.params.allocationID.toHexString()
let paymentAddress = event.params.from

// update indexer
let indexer = Indexer.load(indexerID)!
indexer.queryFeesCollected = indexer.queryFeesCollected.plus(event.params.rebateFees)
indexer.save()

let paymentAggregation = createOrLoadIndexerQueryFeePaymentAggregation(paymentAddress, event.params.indexer)
paymentAggregation.queryFeesCollected = paymentAggregation.queryFeesCollected.plus(event.params.rebateFees)
paymentAggregation.save()

// update allocation
// rebateFees is the total token value minus the curation and protocol fees, as can be seen in the contracts
let allocation = Allocation.load(allocationID)!
Expand Down Expand Up @@ -457,6 +465,20 @@ export function handleAllocationCollected(event: AllocationCollected): void {
event.params.rebateFees,
)
graphNetwork.save()

let paymentSource = createOrLoadPaymentSource(paymentAddress)
paymentSource.totalQueryFees = paymentSource.totalQueryFees.plus(event.params.tokens)
paymentSource.totalIndexerQueryFeesCollected = paymentSource.totalIndexerQueryFeesCollected.plus(
event.params.rebateFees,
)
paymentSource.totalCuratorQueryFees = paymentSource.totalCuratorQueryFees.plus(
event.params.curationFees,
)
paymentSource.totalTaxedQueryFees = paymentSource.totalTaxedQueryFees.plus(taxedFees)
paymentSource.totalUnclaimedQueryFeeRebates = paymentSource.totalUnclaimedQueryFeeRebates.plus(
event.params.rebateFees,
)
paymentSource.save()
}

/**
Expand Down Expand Up @@ -683,6 +705,7 @@ export function handleRebateCollected(event: RebateCollected): void {
let subgraphDeploymentID = event.params.subgraphDeploymentID.toHexString()
let indexerID = event.params.indexer.toHexString()
let allocationID = event.params.allocationID.toHexString()
let paymentAddress = event.params.assetHolder

// update indexer
let indexer = Indexer.load(indexerID)!
Expand All @@ -696,6 +719,13 @@ export function handleRebateCollected(event: RebateCollected): void {
indexer = updateAdvancedIndexerMetrics(indexer as Indexer)
indexer.save()

// Replicate for payment source specific aggregation
let paymentAggregation = createOrLoadIndexerQueryFeePaymentAggregation(paymentAddress, event.params.indexer)
paymentAggregation.queryFeesCollected = paymentAggregation.queryFeesCollected.plus(event.params.queryFees)
paymentAggregation.queryFeeRebates = paymentAggregation.queryFeeRebates.plus(event.params.queryRebates)
paymentAggregation.delegatorQueryFees = paymentAggregation.delegatorQueryFees.plus(event.params.delegationRewards)
paymentAggregation.save()

// update allocation
// queryFees is the total token value minus the curation and protocol fees, as can be seen in the contracts
let allocation = Allocation.load(allocationID)!
Expand Down Expand Up @@ -751,6 +781,30 @@ export function handleRebateCollected(event: RebateCollected): void {
)
graphNetwork.totalDelegatedTokens = graphNetwork.totalDelegatedTokens.plus(event.params.delegationRewards)
graphNetwork.save()

// Replicate for payment source specific data
let paymentSource = createOrLoadPaymentSource(paymentAddress)
paymentSource.totalQueryFees = paymentSource.totalQueryFees.plus(event.params.tokens)
paymentSource.totalIndexerQueryFeesCollected = paymentSource.totalIndexerQueryFeesCollected.plus(
event.params.queryFees,
)
paymentSource.totalCuratorQueryFees = paymentSource.totalCuratorQueryFees.plus(
event.params.curationFees,
)
paymentSource.totalTaxedQueryFees = paymentSource.totalTaxedQueryFees.plus(event.params.protocolTax)
paymentSource.totalUnclaimedQueryFeeRebates = paymentSource.totalUnclaimedQueryFeeRebates.plus(
event.params.queryFees,
)
paymentSource.totalIndexerQueryFeeRebates = paymentSource.totalIndexerQueryFeeRebates.plus(
event.params.queryRebates,
)
paymentSource.totalDelegatorQueryFeeRebates = paymentSource.totalDelegatorQueryFeeRebates.plus(
event.params.delegationRewards,
)
paymentSource.totalUnclaimedQueryFeeRebates = paymentSource.totalUnclaimedQueryFeeRebates.minus(
event.params.delegationRewards.plus(event.params.queryRebates),
)
paymentSource.save()
}

/**
Expand Down