Skip to content

Commit c68f033

Browse files
committed
feat: experimental payment source disambiguation
1 parent 29d3662 commit c68f033

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

schema.graphql

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,48 @@ type Indexer @entity {
771771
annualizedReturn: BigDecimal! # You must multiple by 100 to get percentage
772772
"NOT IMPLEMENTED - Staking efficiency of the indexer"
773773
stakingEfficiency: BigDecimal!
774+
775+
# Query fees breakdown by payment address
776+
777+
}
778+
779+
type PaymentSource @entity {
780+
"Address used on the payment"
781+
id: ID!
782+
783+
"Total query fees generated in the network"
784+
totalQueryFees: BigInt!
785+
"Total query fees collected by indexers"
786+
totalIndexerQueryFeesCollected: BigInt!
787+
"Total query fees rebates claimed by indexers"
788+
totalIndexerQueryFeeRebates: BigInt!
789+
"Total query fees rebates claimed by delegators"
790+
totalDelegatorQueryFeeRebates: BigInt!
791+
"Total query fees payed to curators"
792+
totalCuratorQueryFees: BigInt!
793+
"Total protocol taxes applied to the query fees"
794+
totalTaxedQueryFees: BigInt!
795+
# It is hard to separate the unclaimed and rebates lost
796+
"Total unclaimed rebates. Includes unclaimed rebates, and rebates lost in rebates mechanism "
797+
totalUnclaimedQueryFeeRebates: BigInt!
798+
799+
graphNetwork: GraphNetwork! # to make a derived list on GraphNetwork
800+
}
801+
802+
type IndexerQueryFeePaymentAggregation @entity {
803+
"Join ID of indexer address and PaymentSource address"
804+
id: ID!
805+
806+
"Total query fees collected. Includes the portion given to delegators"
807+
queryFeesCollected: BigInt!
808+
"Query fee rebate amount claimed from the protocol through rebates mechanism. Does not include portion given to delegators"
809+
queryFeeRebates: BigInt!
810+
"The total amount of query fees given to delegators"
811+
delegatorQueryFees: BigInt!
812+
813+
indexer: Indexer!
814+
815+
paymentSource: PaymentSource!
774816
}
775817

776818
"""

src/mappings/helpers/helpers.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
DelegatedStake,
2525
NameSignalSubgraphRelation,
2626
CurrentSubgraphDeploymentRelation,
27+
PaymentSource,
28+
IndexerQueryFeePaymentAggregation,
2729
} from '../../types/schema'
2830
import {
2931
SubgraphDeploymentManifest as SubgraphDeploymentManifestTemplate
@@ -185,6 +187,44 @@ export function createOrLoadIndexer(indexerAddress: Bytes, timestamp: BigInt): I
185187
return indexer as Indexer
186188
}
187189

190+
export function createOrLoadPaymentSource(paymentAddress: Bytes): PaymentSource {
191+
let id = paymentAddress.toHexString()
192+
let paymentSource = PaymentSource.load(id)
193+
if (paymentSource == null) {
194+
let paymentSource = new PaymentSource(id)
195+
paymentSource.graphNetwork = "1"
196+
paymentSource.totalQueryFees = BigInt.fromI32(0)
197+
paymentSource.totalIndexerQueryFeesCollected = BigInt.fromI32(0)
198+
paymentSource.totalIndexerQueryFeeRebates = BigInt.fromI32(0)
199+
paymentSource.totalDelegatorQueryFeeRebates = BigInt.fromI32(0)
200+
paymentSource.totalCuratorQueryFees = BigInt.fromI32(0)
201+
paymentSource.totalTaxedQueryFees = BigInt.fromI32(0)
202+
paymentSource.totalUnclaimedQueryFeeRebates = BigInt.fromI32(0)
203+
204+
paymentSource.save()
205+
}
206+
207+
return paymentSource as PaymentSource
208+
}
209+
210+
export function createOrLoadIndexerQueryFeePaymentAggregation(paymentAddress: Bytes, indexerAddress: Bytes): IndexerQueryFeePaymentAggregation {
211+
let id = paymentAddress.toHexString().concat("-").concat(indexerAddress.toHexString())
212+
let aggregation = IndexerQueryFeePaymentAggregation.load(id)
213+
if (aggregation == null) {
214+
let aggregation = new IndexerQueryFeePaymentAggregation(id)
215+
aggregation.indexer = indexerAddress.toHexString()
216+
aggregation.paymentSource = paymentAddress.toHexString()
217+
aggregation.queryFeesCollected = BigInt.fromI32(0)
218+
aggregation.queryFeeRebates = BigInt.fromI32(0)
219+
aggregation.delegatorQueryFees = BigInt.fromI32(0)
220+
221+
aggregation.save()
222+
}
223+
224+
return aggregation as IndexerQueryFeePaymentAggregation
225+
}
226+
227+
188228
export function createOrLoadDelegator(delegatorAddress: Bytes, timestamp: BigInt): Delegator {
189229
let id = delegatorAddress.toHexString()
190230
let delegator = Delegator.load(id)

src/mappings/staking.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
GraphAccount,
2929
Delegator,
3030
DelegatedStake,
31+
IndexerQueryFeePaymentAggregation,
3132
} from '../types/schema'
3233

3334
import {
@@ -45,6 +46,8 @@ import {
4546
batchUpdateSubgraphSignalledTokens,
4647
createOrLoadGraphNetwork,
4748
calculateCapacities,
49+
createOrLoadIndexerQueryFeePaymentAggregation,
50+
createOrLoadPaymentSource,
4851
} from './helpers/helpers'
4952
import { addresses } from '../../config/addresses'
5053

@@ -394,12 +397,17 @@ export function handleAllocationCollected(event: AllocationCollected): void {
394397
let subgraphDeploymentID = event.params.subgraphDeploymentID.toHexString()
395398
let indexerID = event.params.indexer.toHexString()
396399
let allocationID = event.params.allocationID.toHexString()
400+
let paymentAddress = event.transaction.from
397401

398402
// update indexer
399403
let indexer = Indexer.load(indexerID)!
400404
indexer.queryFeesCollected = indexer.queryFeesCollected.plus(event.params.rebateFees)
401405
indexer.save()
402406

407+
let paymentAggregation = createOrLoadIndexerQueryFeePaymentAggregation(paymentAddress, event.params.indexer)
408+
paymentAggregation.queryFeesCollected = paymentAggregation.queryFeesCollected.plus(event.params.rebateFees)
409+
paymentAggregation.save()
410+
403411
// update allocation
404412
// rebateFees is the total token value minus the curation and protocol fees, as can be seen in the contracts
405413
let allocation = Allocation.load(allocationID)!
@@ -457,6 +465,20 @@ export function handleAllocationCollected(event: AllocationCollected): void {
457465
event.params.rebateFees,
458466
)
459467
graphNetwork.save()
468+
469+
let paymentSource = createOrLoadPaymentSource(paymentAddress)
470+
paymentSource.totalQueryFees = paymentSource.totalQueryFees.plus(event.params.tokens)
471+
paymentSource.totalIndexerQueryFeesCollected = paymentSource.totalIndexerQueryFeesCollected.plus(
472+
event.params.rebateFees,
473+
)
474+
paymentSource.totalCuratorQueryFees = paymentSource.totalCuratorQueryFees.plus(
475+
event.params.curationFees,
476+
)
477+
paymentSource.totalTaxedQueryFees = paymentSource.totalTaxedQueryFees.plus(taxedFees)
478+
paymentSource.totalUnclaimedQueryFeeRebates = paymentSource.totalUnclaimedQueryFeeRebates.plus(
479+
event.params.rebateFees,
480+
)
481+
paymentSource.save()
460482
}
461483

462484
/**
@@ -683,6 +705,7 @@ export function handleRebateCollected(event: RebateCollected): void {
683705
let subgraphDeploymentID = event.params.subgraphDeploymentID.toHexString()
684706
let indexerID = event.params.indexer.toHexString()
685707
let allocationID = event.params.allocationID.toHexString()
708+
let paymentAddress = event.transaction.from
686709

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

722+
// Replicate for payment source specific aggregation
723+
let paymentAggregation = createOrLoadIndexerQueryFeePaymentAggregation(paymentAddress, event.params.indexer)
724+
paymentAggregation.queryFeesCollected = paymentAggregation.queryFeesCollected.plus(event.params.queryFees)
725+
paymentAggregation.queryFeeRebates = paymentAggregation.queryFeeRebates.plus(event.params.queryRebates)
726+
paymentAggregation.delegatorQueryFees = paymentAggregation.delegatorQueryFees.plus(event.params.delegationRewards)
727+
paymentAggregation.save()
728+
699729
// update allocation
700730
// queryFees is the total token value minus the curation and protocol fees, as can be seen in the contracts
701731
let allocation = Allocation.load(allocationID)!
@@ -751,6 +781,30 @@ export function handleRebateCollected(event: RebateCollected): void {
751781
)
752782
graphNetwork.totalDelegatedTokens = graphNetwork.totalDelegatedTokens.plus(event.params.delegationRewards)
753783
graphNetwork.save()
784+
785+
// Replicate for payment source specific data
786+
let paymentSource = createOrLoadPaymentSource(paymentAddress)
787+
paymentSource.totalQueryFees = paymentSource.totalQueryFees.plus(event.params.tokens)
788+
paymentSource.totalIndexerQueryFeesCollected = paymentSource.totalIndexerQueryFeesCollected.plus(
789+
event.params.queryFees,
790+
)
791+
paymentSource.totalCuratorQueryFees = paymentSource.totalCuratorQueryFees.plus(
792+
event.params.curationFees,
793+
)
794+
paymentSource.totalTaxedQueryFees = paymentSource.totalTaxedQueryFees.plus(event.params.protocolTax)
795+
paymentSource.totalUnclaimedQueryFeeRebates = paymentSource.totalUnclaimedQueryFeeRebates.plus(
796+
event.params.queryFees,
797+
)
798+
paymentSource.totalIndexerQueryFeeRebates = paymentSource.totalIndexerQueryFeeRebates.plus(
799+
event.params.queryRebates,
800+
)
801+
paymentSource.totalDelegatorQueryFeeRebates = paymentSource.totalDelegatorQueryFeeRebates.plus(
802+
event.params.delegationRewards,
803+
)
804+
paymentSource.totalUnclaimedQueryFeeRebates = paymentSource.totalUnclaimedQueryFeeRebates.minus(
805+
event.params.delegationRewards.plus(event.params.queryRebates),
806+
)
807+
paymentSource.save()
754808
}
755809

756810
/**

0 commit comments

Comments
 (0)