Skip to content

Commit f699476

Browse files
committed
fix: address gh pr feedback
1 parent e716d99 commit f699476

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

schema.graphql

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ type DataService @entity {
246246
"Total GRT currently in allocations for this DataService"
247247
totalTokensAllocated: BigInt!
248248

249+
"Ratio of max staked delegation tokens to indexers stake that earns rewards"
250+
delegationRatio: Int
251+
249252
provisions: [Provision!]! @derivedFrom(field: "dataService")
250253
}
251254

@@ -867,6 +870,10 @@ type Provision @entity {
867870

868871
tokensAllocated: BigInt!
869872

873+
tokensSlashedServiceProvider: BigInt!
874+
875+
tokensSlashedDelegationPool: BigInt!
876+
870877
totalAllocationCount: BigInt!
871878

872879
allocationCount: Int!
@@ -904,7 +911,7 @@ type Provision @entity {
904911

905912
"The percent of indexing rewards generated by the delegated stake that the Indexer keeps for itself"
906913
indexingRewardEffectiveCut: BigDecimal!
907-
"The percent of query rebate rewards generated by the delegated stake that the Indexer keeps for itself"
914+
"The percent of query fees generated by the delegated stake that the Indexer keeps for itself"
908915
queryFeeEffectiveCut: BigDecimal!
909916
"The percent of reward dilution delegators experience because of overdelegation. Overdelegated stake can't be used to generate rewards but still gets accounted while distributing the generated rewards. This causes dilution of the rewards for the rest of the pool."
910917
overDelegationDilution: BigDecimal!
@@ -934,11 +941,11 @@ type Provision @entity {
934941
delegatorShares: BigInt!
935942
"Exchange rate of of tokens received for each share"
936943
delegationExchangeRate: BigDecimal!
937-
"Ratio between the amount of the indexers own stake over the total usable stake."
944+
"Ratio between the amount of the indexers own stake over the total usable stake (capped by the delegationRatio)."
938945
ownStakeRatio: BigDecimal!
939-
"Ratio between the amount of delegated stake over the total usable stake."
946+
"Ratio between the amount of delegated stake over the total usable stake (capped by the delegationRatio)."
940947
delegatedStakeRatio: BigDecimal!
941-
"Percentage of indexers' own rewards received in relation to its own stake. 1 (100%) means that the indexer is receiving the exact amount that is generated by his own stake"
948+
"Percentage of indexers' own rewards received in relation to its own stake. 1 (100%) means that the indexer is receiving the exact amount that is generated by his own stake, the value can be over 100% or below depending on the amount of delegation and cuts set"
942949
indexerRewardsOwnGenerationRatio: BigDecimal!
943950

944951
"Service registry URL for the indexer"

src/mappings/helpers/helpers.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ export function createOrLoadProvision(indexerAddress: Bytes, verifierAddress: By
202202
provision.dataService = verifierAddress.toHexString()
203203
provision.tokensProvisioned = BigInt.fromI32(0)
204204
provision.tokensAllocated = BigInt.fromI32(0)
205+
provision.tokensSlashedServiceProvider = BigInt.fromI32(0)
206+
provision.tokensSlashedDelegationPool = BigInt.fromI32(0)
205207
provision.totalAllocationCount = BigInt.fromI32(0)
206208
provision.allocationCount = 0
207209
provision.tokensThawing = BigInt.fromI32(0)
@@ -993,18 +995,21 @@ export function calculateQueryFeeEffectiveCutForProvision(provision: Provision):
993995
}
994996

995997
export function calculateIndexerRewardOwnGenerationRatioForProvision(provision: Provision): BigDecimal {
996-
let rewardCut =
998+
let delegatorCut =
997999
provision.indexingRewardsCut.toBigDecimal() / BigDecimal.fromString('1000000')
9981000
return provision.ownStakeRatio == BigDecimal.fromString('0')
9991001
? BigDecimal.fromString('0')
1000-
: rewardCut / provision.ownStakeRatio
1002+
: (BigDecimal.fromString('1') - delegatorCut) / provision.ownStakeRatio
10011003
}
10021004

10031005
export function calculateOverdelegationDilutionForProvision(provision: Provision): BigDecimal {
10041006
let provisionedTokensBD = provision.tokensProvisioned.toBigDecimal()
10051007
let delegatedTokensBD = provision.delegatedTokens.toBigDecimal()
1006-
let graphNetwork = GraphNetwork.load('1')!
1007-
let delegationRatioBD = BigInt.fromI32(graphNetwork.delegationRatio).toBigDecimal()
1008+
let dataService = DataService.load(provision.dataService)!
1009+
if (dataService.delegationRatio == null) {
1010+
return BigDecimal.fromString('0')
1011+
}
1012+
let delegationRatioBD = BigInt.fromI32(dataService.delegationRatio).toBigDecimal()
10081013
let maxDelegatedStake = provisionedTokensBD * delegationRatioBD
10091014
return provisionedTokensBD == BigDecimal.fromString('0')
10101015
? BigDecimal.fromString('0')

src/mappings/horizonStaking.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,16 @@ export function handleProvisionSlashed(event: ProvisionSlashed): void {
230230
indexer.provisionedTokens = indexer.provisionedTokens.minus(event.params.tokens)
231231
indexer.stakedTokens = indexer.stakedTokens.minus(event.params.tokens)
232232
indexer.save()
233-
233+
234234
dataService.totalTokensProvisioned = dataService.totalTokensProvisioned.minus(event.params.tokens)
235235
dataService.save()
236-
236+
237237
graphNetwork.totalTokensProvisioned = graphNetwork.totalTokensProvisioned.minus(event.params.tokens)
238238
graphNetwork.totalTokensStaked = graphNetwork.totalTokensStaked.minus(event.params.tokens)
239239
graphNetwork.save()
240-
240+
241241
provision.tokensProvisioned = provision.tokensProvisioned.minus(event.params.tokens)
242+
provision.tokensSlashedServiceProvider = provision.tokensSlashedServiceProvider.plus(event.params.tokens)
242243
provision.save()
243244
}
244245

@@ -366,6 +367,7 @@ export function handleDelegationSlashed(event: DelegationSlashed): void {
366367
// update provision
367368
let provision = createOrLoadProvision(event.params.serviceProvider, event.params.verifier, event.block.timestamp)
368369
provision.delegatedTokens = provision.delegatedTokens.minus(event.params.tokens)
370+
provision.tokensSlashedDelegationPool = provision.tokensSlashedDelegationPool.plus(event.params.tokens)
369371
if (provision.delegatorShares != BigInt.fromI32(0)) {
370372
provision = updateDelegationExchangeRateForProvision(provision as Provision)
371373
}

src/mappings/subgraphService.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BigDecimal, BigInt, ethereum, log } from "@graphprotocol/graph-ts"
2-
import { AllocationClosed, AllocationCreated, AllocationResized, IndexingRewardsCollected, QueryFeesCollected, RewardsDestinationSet, ServiceProviderRegistered } from "../types/SubgraphService/SubgraphService"
3-
import { batchUpdateSubgraphSignalledTokens, calculatePricePerShare, createOrLoadEpoch, createOrLoadGraphNetwork, createOrLoadIndexerQueryFeePaymentAggregation, createOrLoadPaymentSource, createOrLoadProvision, createOrLoadSubgraphDeployment, joinID, updateDelegationExchangeRate } from "./helpers/helpers"
2+
import { AllocationClosed, AllocationCreated, AllocationResized, DelegationRatioSet, IndexingRewardsCollected, QueryFeesCollected, RewardsDestinationSet, ServiceProviderRegistered } from "../types/SubgraphService/SubgraphService"
3+
import { batchUpdateSubgraphSignalledTokens, calculatePricePerShare, createOrLoadDataService, createOrLoadEpoch, createOrLoadGraphNetwork, createOrLoadIndexerQueryFeePaymentAggregation, createOrLoadPaymentSource, createOrLoadProvision, createOrLoadSubgraphDeployment, joinID, updateDelegationExchangeRate } from "./helpers/helpers"
44
import { Allocation, GraphAccount, Indexer, PoiSubmission, SubgraphDeployment } from "../types/schema"
55
import { addresses } from "../../config/addresses"
66

@@ -23,6 +23,12 @@ export function handleRewardsDestinationSet(event: RewardsDestinationSet): void
2323
provision.save()
2424
}
2525

26+
export function handleDelegationRatioSet(event: DelegationRatioSet): void {
27+
let dataService = createOrLoadDataService(event.address)
28+
dataService.delegationRatio = event.params.ratio
29+
dataService.save()
30+
}
31+
2632
export function handleAllocationCreated(event: AllocationCreated): void {
2733
let graphNetwork = createOrLoadGraphNetwork(event.block.number, event.address)
2834
let subgraphDeploymentID = event.params.subgraphDeploymentId.toHexString()

subgraph.template.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ dataSources:
473473
handler: handleIndexingRewardsCollected
474474
- event: QueryFeesCollected(indexed address,indexed address,indexed address,bytes32,uint256,uint256)
475475
handler: handleQueryFeesCollected
476+
- event: DelegationRatioSet(uint32)
477+
handler: handleDelegationRatioSet
476478
- kind: ethereum/contract
477479
name: GraphPayments
478480
network: {{network}}

0 commit comments

Comments
 (0)