Skip to content

Commit fc55162

Browse files
committed
fix: handle HorizonRewardsAssigned event on RewardsManager
1 parent ebf3a1b commit fc55162

File tree

3 files changed

+127
-54
lines changed

3 files changed

+127
-54
lines changed

abis/RewardsManagerStitched.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,31 @@
6262
"name": "RewardsAssigned",
6363
"type": "event"
6464
},
65+
{
66+
"anonymous": false,
67+
"inputs": [
68+
{
69+
"indexed": true,
70+
"internalType": "address",
71+
"name": "indexer",
72+
"type": "address"
73+
},
74+
{
75+
"indexed": true,
76+
"internalType": "address",
77+
"name": "allocationID",
78+
"type": "address"
79+
},
80+
{
81+
"indexed": false,
82+
"internalType": "uint256",
83+
"name": "amount",
84+
"type": "uint256"
85+
}
86+
],
87+
"name": "HorizonRewardsAssigned",
88+
"type": "event"
89+
},
6590
{
6691
"anonymous": false,
6792
"inputs": [

src/mappings/rewardsManager.ts

Lines changed: 100 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Address, BigInt } from '@graphprotocol/graph-ts'
22
import { Indexer, Allocation, SubgraphDeployment } from '../types/schema'
33
import {
44
RewardsAssigned,
5+
HorizonRewardsAssigned,
56
ParameterUpdated,
67
RewardsManagerStitched as RewardsManager,
78
RewardsDenylistUpdated,
@@ -16,22 +17,109 @@ import {
1617
import { addresses } from '../../config/addresses'
1718

1819
export function handleRewardsAssigned(event: RewardsAssigned): void {
19-
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
20-
let indexerID = event.params.indexer.toHexString()
20+
processRewardsAssigned(
21+
event.params.indexer,
22+
event.params.allocationID.toHexString(),
23+
event.params.amount,
24+
event.block.number,
25+
event.block.timestamp,
26+
event.address,
27+
)
28+
}
29+
30+
/**
31+
* @dev handleHorizonRewardsAssigned
32+
* - Handles the HorizonRewardsAssigned event emitted after Horizon upgrade
33+
* - Only processes rewards for legacy allocations (created via old Staking contract)
34+
* - New allocations (via SubgraphService) are handled by IndexingRewardsCollected instead
35+
*/
36+
export function handleHorizonRewardsAssigned(event: HorizonRewardsAssigned): void {
2137
let allocationID = event.params.allocationID.toHexString()
38+
let allocation = Allocation.load(allocationID)
39+
40+
// Skip if allocation doesn't exist or is not a legacy allocation
41+
// New allocations have their rewards tracked via IndexingRewardsCollected from SubgraphService
42+
if (allocation == null || !allocation.isLegacy) {
43+
return
44+
}
45+
46+
processRewardsAssigned(
47+
event.params.indexer,
48+
allocationID,
49+
event.params.amount,
50+
event.block.number,
51+
event.block.timestamp,
52+
event.address,
53+
)
54+
}
55+
56+
/**
57+
* @dev handleParameterUpdated
58+
* - handlers updating all parameters
59+
*/
60+
export function handleParameterUpdated(event: ParameterUpdated): void {
61+
let parameter = event.params.param
62+
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
63+
let rewardsManager = RewardsManager.bind(event.address as Address)
64+
65+
if (parameter == 'issuanceRate') {
66+
graphNetwork.networkGRTIssuance = rewardsManager.issuanceRate()
67+
} else if (parameter == 'issuancePerBlock') {
68+
graphNetwork.networkGRTIssuancePerBlock = rewardsManager.issuancePerBlock()
69+
} else if (parameter == 'subgraphAvailabilityOracle') {
70+
graphNetwork.subgraphAvailabilityOracle = rewardsManager.subgraphAvailabilityOracle()
71+
}
72+
graphNetwork.save()
73+
}
74+
75+
// export function handleImplementationUpdated(event: ImplementationUpdated): void {
76+
// let graphNetwork = GraphNetwork.load('1')
77+
// let implementations = graphNetwork.rewardsManagerImplementations
78+
// implementations.push(event.params.newImplementation)
79+
// graphNetwork.rewardsManagerImplementations = implementations
80+
// graphNetwork.save()
81+
// }
82+
83+
export function handleRewardsDenyListUpdated(event: RewardsDenylistUpdated): void {
84+
let subgraphDeployment = SubgraphDeployment.load(event.params.subgraphDeploymentID.toHexString())
85+
if (subgraphDeployment != null) {
86+
if (event.params.sinceBlock.toI32() == 0) {
87+
subgraphDeployment.deniedAt = 0
88+
} else {
89+
subgraphDeployment.deniedAt = event.params.sinceBlock.toI32()
90+
}
91+
subgraphDeployment.save()
92+
}
93+
// We might need to handle the case where the subgraph deployment doesn't exists later
94+
}
95+
96+
/**
97+
* @dev processRewardsAssigned
98+
* - Common logic for processing rewards assigned events (both legacy RewardsAssigned and HorizonRewardsAssigned)
99+
*/
100+
function processRewardsAssigned(
101+
indexerAddress: Address,
102+
allocationID: string,
103+
amount: BigInt,
104+
blockNumber: BigInt,
105+
blockTimestamp: BigInt,
106+
eventAddress: Address,
107+
): void {
108+
let graphNetwork = createOrLoadGraphNetwork(blockNumber, eventAddress)
109+
let indexerID = indexerAddress.toHexString()
22110

23111
// update indexer
24112
let indexer = Indexer.load(indexerID)!
25-
indexer.rewardsEarned = indexer.rewardsEarned.plus(event.params.amount)
113+
indexer.rewardsEarned = indexer.rewardsEarned.plus(amount)
26114
// If the delegation pool has zero tokens, the contracts don't give away any rewards
27115
let indexerIndexingRewards =
28116
indexer.delegatedTokens == BigInt.fromI32(0)
29-
? event.params.amount
30-
: event.params.amount
117+
? amount
118+
: amount
31119
.times(BigInt.fromI32(indexer.legacyIndexingRewardCut))
32120
.div(BigInt.fromI32(1000000))
33121

34-
let delegatorIndexingRewards = event.params.amount.minus(indexerIndexingRewards)
122+
let delegatorIndexingRewards = amount.minus(indexerIndexingRewards)
35123

36124
indexer.delegatorIndexingRewards = indexer.delegatorIndexingRewards.plus(delegatorIndexingRewards)
37125
indexer.indexerIndexingRewards = indexer.indexerIndexingRewards.plus(indexerIndexingRewards)
@@ -46,16 +134,16 @@ export function handleRewardsAssigned(event: RewardsAssigned): void {
46134
// update allocation
47135
// no status updated, Claimed happens when RebateClaimed, and it is done
48136
let allocation = Allocation.load(allocationID)!
49-
allocation.indexingRewards = allocation.indexingRewards.plus(event.params.amount)
137+
allocation.indexingRewards = allocation.indexingRewards.plus(amount)
50138
allocation.indexingIndexerRewards = allocation.indexingIndexerRewards.plus(indexerIndexingRewards)
51139
allocation.indexingDelegatorRewards = allocation.indexingDelegatorRewards.plus(
52140
delegatorIndexingRewards,
53141
)
54142
allocation.save()
55143

56144
// Update epoch
57-
let epoch = createOrLoadEpoch(addresses.isL1 ? event.block.number : graphNetwork.currentL1BlockNumber!, graphNetwork)
58-
epoch.totalRewards = epoch.totalRewards.plus(event.params.amount)
145+
let epoch = createOrLoadEpoch(addresses.isL1 ? blockNumber : graphNetwork.currentL1BlockNumber!, graphNetwork)
146+
epoch.totalRewards = epoch.totalRewards.plus(amount)
59147
epoch.totalIndexerRewards = epoch.totalIndexerRewards.plus(indexerIndexingRewards)
60148
epoch.totalDelegatorRewards = epoch.totalDelegatorRewards.plus(delegatorIndexingRewards)
61149
epoch.save()
@@ -64,12 +152,10 @@ export function handleRewardsAssigned(event: RewardsAssigned): void {
64152
let subgraphDeploymentID = allocation.subgraphDeployment
65153
let subgraphDeployment = createOrLoadSubgraphDeployment(
66154
subgraphDeploymentID,
67-
event.block.timestamp,
155+
blockTimestamp,
68156
graphNetwork,
69157
)
70-
subgraphDeployment.indexingRewardAmount = subgraphDeployment.indexingRewardAmount.plus(
71-
event.params.amount,
72-
)
158+
subgraphDeployment.indexingRewardAmount = subgraphDeployment.indexingRewardAmount.plus(amount)
73159
subgraphDeployment.indexingIndexerRewardAmount = subgraphDeployment.indexingIndexerRewardAmount.plus(
74160
indexerIndexingRewards,
75161
)
@@ -79,7 +165,7 @@ export function handleRewardsAssigned(event: RewardsAssigned): void {
79165
subgraphDeployment.save()
80166

81167
// update graph network
82-
graphNetwork.totalIndexingRewards = graphNetwork.totalIndexingRewards.plus(event.params.amount)
168+
graphNetwork.totalIndexingRewards = graphNetwork.totalIndexingRewards.plus(amount)
83169
graphNetwork.totalIndexingIndexerRewards = graphNetwork.totalIndexingIndexerRewards.plus(
84170
indexerIndexingRewards,
85171
)
@@ -89,43 +175,3 @@ export function handleRewardsAssigned(event: RewardsAssigned): void {
89175
graphNetwork.totalDelegatedTokens = graphNetwork.totalDelegatedTokens.plus(delegatorIndexingRewards)
90176
graphNetwork.save()
91177
}
92-
93-
/**
94-
* @dev handleParameterUpdated
95-
* - handlers updating all parameters
96-
*/
97-
export function handleParameterUpdated(event: ParameterUpdated): void {
98-
let parameter = event.params.param
99-
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
100-
let rewardsManager = RewardsManager.bind(event.address as Address)
101-
102-
if (parameter == 'issuanceRate') {
103-
graphNetwork.networkGRTIssuance = rewardsManager.issuanceRate()
104-
} else if (parameter == 'issuancePerBlock') {
105-
graphNetwork.networkGRTIssuancePerBlock = rewardsManager.issuancePerBlock()
106-
} else if (parameter == 'subgraphAvailabilityOracle') {
107-
graphNetwork.subgraphAvailabilityOracle = rewardsManager.subgraphAvailabilityOracle()
108-
}
109-
graphNetwork.save()
110-
}
111-
112-
// export function handleImplementationUpdated(event: ImplementationUpdated): void {
113-
// let graphNetwork = GraphNetwork.load('1')
114-
// let implementations = graphNetwork.rewardsManagerImplementations
115-
// implementations.push(event.params.newImplementation)
116-
// graphNetwork.rewardsManagerImplementations = implementations
117-
// graphNetwork.save()
118-
// }
119-
120-
export function handleRewardsDenyListUpdated(event: RewardsDenylistUpdated): void {
121-
let subgraphDeployment = SubgraphDeployment.load(event.params.subgraphDeploymentID.toHexString())
122-
if (subgraphDeployment != null) {
123-
if (event.params.sinceBlock.toI32() == 0) {
124-
subgraphDeployment.deniedAt = 0
125-
} else {
126-
subgraphDeployment.deniedAt = event.params.sinceBlock.toI32()
127-
}
128-
subgraphDeployment.save()
129-
}
130-
// We might need to handle the case where the subgraph deployment doesn't exists later
131-
}

subgraph.template.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,8 @@ dataSources:
756756
eventHandlers:
757757
- event: RewardsAssigned(indexed address,indexed address,uint256,uint256)
758758
handler: handleRewardsAssigned
759+
- event: HorizonRewardsAssigned(indexed address,indexed address,uint256)
760+
handler: handleHorizonRewardsAssigned
759761
- event: RewardsDenylistUpdated(indexed bytes32,uint256)
760762
handler: handleRewardsDenyListUpdated
761763
# - event: ImplementationUpdated(address,address)

0 commit comments

Comments
 (0)