@@ -17,6 +17,7 @@ import {
1717 SlasherUpdate ,
1818 AssetHolderUpdate ,
1919 AllocationClosed1 ,
20+ RebateCollected ,
2021} from '../types/Staking/Staking'
2122import {
2223 ParameterUpdated , StakingExtension ,
@@ -448,6 +449,7 @@ export function handleAllocationCreated(event: AllocationCreated): void {
448449 allocation . createdAtBlockHash = event . block . hash
449450 allocation . queryFeesCollected = BigInt . fromI32 ( 0 )
450451 allocation . queryFeeRebates = BigInt . fromI32 ( 0 )
452+ allocation . distributedRebates = BigInt . fromI32 ( 0 )
451453 allocation . curatorRewards = BigInt . fromI32 ( 0 )
452454 allocation . indexingRewards = BigInt . fromI32 ( 0 )
453455 allocation . indexingIndexerRewards = BigInt . fromI32 ( 0 )
@@ -462,14 +464,18 @@ export function handleAllocationCreated(event: AllocationCreated): void {
462464 getAndUpdateNetworkDailyData ( graphNetwork as GraphNetwork , event . block . timestamp )
463465}
464466
465- // Transfers tokens from a state channel to the staking contract
466- // Burns fees if protocolPercentage > 0
467- // Collects curationFees to go to curator rewards
468- // calls collect() on curation, which is handled in curation.ts
469- // adds to the allocations collected fees
470- // if closed, it will add fees to the rebate pool
471- // Note - the name event.param.rebateFees is confusing. Rebate fees are better described
472- // as query Fees. rebate is from cobbs douglas, which we get from claim()
467+ /**
468+ * @dev handleAllocationCollected
469+ * Note: this handler is for the AllocationCollected event prior to exponential rebates upgrade
470+ * - Transfers tokens from a state channel to the staking contract
471+ * - Burns fees if protocolPercentage > 0
472+ * - Collects curationFees to go to curator rewards
473+ * - calls collect() on curation, which is handled in curation.ts
474+ * - adds to the allocations collected fees
475+ * - if closed, it will add fees to the rebate pool
476+ * - Note - the name event.param.rebateFees is confusing. Rebate fees are better described
477+ * as query Fees. rebate is from cobbs douglas, which we get from claim()
478+ */
473479export function handleAllocationCollected ( event : AllocationCollected ) : void {
474480 let subgraphDeploymentID = event . params . subgraphDeploymentID . toHexString ( )
475481 let indexerID = event . params . indexer . toHexString ( )
@@ -584,6 +590,7 @@ export function handleAllocationClosed(event: AllocationClosed): void {
584590 getAndUpdateSubgraphDeploymentDailyData ( deployment as SubgraphDeployment , event . block . timestamp )
585591 getAndUpdateNetworkDailyData ( graphNetwork as GraphNetwork , event . block . timestamp )
586592}
593+
587594/**
588595 * @dev handleAllocationClosed
589596 * - update the indexers stake
@@ -676,7 +683,7 @@ export function handleRebateClaimed(event: RebateClaimed): void {
676683 let allocation = Allocation . load ( allocationID ) !
677684 allocation . queryFeeRebates = event . params . tokens
678685 allocation . delegationFees = event . params . delegationFees
679- allocation . status = 'Claimed'
686+ allocation . status = 'Closed' // ' Claimed' is the correct status for pre exponential rebates
680687 allocation . save ( )
681688
682689 // update pool
@@ -731,6 +738,97 @@ export function handleParameterUpdated(event: ParameterUpdated): void {
731738 graphNetwork . save ( )
732739 getAndUpdateNetworkDailyData ( graphNetwork as GraphNetwork , event . block . timestamp )
733740}
741+
742+ /**
743+ * @dev handleRebateCollected
744+ * - update indexer
745+ * - update allocation
746+ * - update epoch
747+ * - update subgraph deployment
748+ * - update graph network
749+ */
750+ export function handleRebateCollected ( event : RebateCollected ) : void {
751+ let graphNetwork = createOrLoadGraphNetwork ( )
752+ let subgraphDeploymentID = event . params . subgraphDeploymentID . toHexString ( )
753+ let indexerID = event . params . indexer . toHexString ( )
754+ let allocationID = event . params . allocationID . toHexString ( )
755+
756+ // update indexer
757+ let indexer = Indexer . load ( indexerID ) !
758+ indexer . queryFeesCollected = indexer . queryFeesCollected . plus ( event . params . queryFees )
759+ indexer . queryFeeRebates = indexer . queryFeeRebates . plus ( event . params . queryRebates )
760+ indexer . delegatorQueryFees = indexer . delegatorQueryFees . plus ( event . params . delegationRewards )
761+ indexer . delegatedTokens = indexer . delegatedTokens . plus ( event . params . delegationRewards )
762+ if ( indexer . delegatorShares != BigInt . fromI32 ( 0 ) ) {
763+ indexer = updateDelegationExchangeRate ( indexer as Indexer )
764+ }
765+ indexer = updateAdvancedIndexerMetrics ( indexer as Indexer )
766+ indexer . save ( )
767+
768+ // update allocation
769+ // queryFees is the total token value minus the curation and protocol fees, as can be seen in the contracts
770+ let allocation = Allocation . load ( allocationID ) !
771+ allocation . queryFeesCollected = allocation . queryFeesCollected . plus ( event . params . queryFees )
772+ allocation . curatorRewards = allocation . curatorRewards . plus ( event . params . curationFees )
773+ allocation . queryFeeRebates = event . params . queryRebates
774+ allocation . distributedRebates = allocation . distributedRebates . plus ( event . params . queryRebates )
775+ allocation . delegationFees = event . params . delegationRewards
776+ allocation . status = 'Closed'
777+ allocation . save ( )
778+
779+ // // Update epoch
780+ // let epoch = createOrLoadEpoch(
781+ // addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!,
782+ // )
783+ // epoch.totalQueryFees = epoch.totalQueryFees.plus(event.params.tokens)
784+ // epoch.taxedQueryFees = epoch.taxedQueryFees.plus(event.params.protocolTax)
785+ // epoch.queryFeesCollected = epoch.queryFeesCollected.plus(event.params.queryFees)
786+ // epoch.curatorQueryFees = epoch.curatorQueryFees.plus(event.params.curationFees)
787+ // epoch.queryFeeRebates = epoch.queryFeeRebates.plus(event.params.queryRebates)
788+ // epoch.save()
789+
790+ // update subgraph deployment
791+ let deployment = SubgraphDeployment . load ( subgraphDeploymentID ) !
792+ deployment . queryFeesAmount = deployment . queryFeesAmount . plus ( event . params . queryFees )
793+ deployment . signalledTokens = deployment . signalledTokens . plus ( event . params . curationFees )
794+ deployment . curatorFeeRewards = deployment . curatorFeeRewards . plus ( event . params . curationFees )
795+ deployment . pricePerShare = calculatePricePerShare ( deployment as SubgraphDeployment )
796+ deployment . queryFeeRebates = deployment . queryFeeRebates . plus ( event . params . queryRebates )
797+ deployment . save ( )
798+
799+ // update graph network
800+ graphNetwork . totalQueryFees = graphNetwork . totalQueryFees . plus ( event . params . tokens )
801+ graphNetwork . totalIndexerQueryFeesCollected = graphNetwork . totalIndexerQueryFeesCollected . plus (
802+ event . params . queryFees ,
803+ )
804+ graphNetwork . totalCuratorQueryFees = graphNetwork . totalCuratorQueryFees . plus (
805+ event . params . curationFees ,
806+ )
807+ graphNetwork . totalTaxedQueryFees = graphNetwork . totalTaxedQueryFees . plus ( event . params . protocolTax )
808+ graphNetwork . totalUnclaimedQueryFeeRebates = graphNetwork . totalUnclaimedQueryFeeRebates . plus (
809+ event . params . queryFees ,
810+ )
811+ graphNetwork . totalIndexerQueryFeeRebates = graphNetwork . totalIndexerQueryFeeRebates . plus (
812+ event . params . queryRebates ,
813+ )
814+ graphNetwork . totalDelegatorQueryFeeRebates = graphNetwork . totalDelegatorQueryFeeRebates . plus (
815+ event . params . delegationRewards ,
816+ )
817+ graphNetwork . totalUnclaimedQueryFeeRebates = graphNetwork . totalUnclaimedQueryFeeRebates . minus (
818+ event . params . delegationRewards . plus ( event . params . queryRebates ) ,
819+ )
820+ graphNetwork . save ( )
821+
822+ batchUpdateDelegatorsForIndexer ( indexer . id , event . block . timestamp )
823+
824+ getAndUpdateIndexerDailyData ( indexer as Indexer , event . block . timestamp )
825+ getAndUpdateSubgraphDeploymentDailyData (
826+ deployment as SubgraphDeployment ,
827+ event . block . timestamp ,
828+ )
829+ getAndUpdateNetworkDailyData ( graphNetwork as GraphNetwork , event . block . timestamp )
830+ }
831+
734832//
735833// export function handleSetOperator(event: SetOperator): void {
736834// let graphAccount = createOrLoadGraphAccount(
0 commit comments