Skip to content

Commit e8f5971

Browse files
Merge pull request #290 from graphprotocol/tmigone/invert-cuts
feat: invert cuts to keep meaning consistent with pre-horizon
2 parents 932ed20 + c4fd754 commit e8f5971

File tree

8 files changed

+121
-74
lines changed

8 files changed

+121
-74
lines changed

schema.graphql

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,10 @@ type Indexer @entity(immutable: false) {
769769
indexerIndexingRewards: BigInt!
770770
"The total amount of indexing rewards given to delegators"
771771
delegatorIndexingRewards: BigInt!
772-
"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"
772+
"[Horizon only] 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"
773773
indexerRewardsOwnGenerationRatio: BigDecimal!
774+
"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. This is the indexerRewardsOwnGenerationRatio before Horizon"
775+
legacyIndexerRewardsOwnGenerationRatio: BigDecimal!
774776
"Whether the indexer has been transferred from L1 to L2 partially or fully"
775777
transferredToL2: Boolean!
776778
"Timestamp for the FIRST L1 -> L2 Transfer"
@@ -813,18 +815,26 @@ type Indexer @entity(immutable: false) {
813815
delegatorShares: BigInt!
814816
"Exchange rate of of tokens received for each share"
815817
delegationExchangeRate: BigDecimal!
816-
"The percent of indexing rewards generated by the total stake that the Indexer keeps for itself. In parts per million"
818+
"[Horizon only] The percent of indexing rewards generated by the total stake that the Indexer keeps for itself. In parts per million"
817819
indexingRewardCut: Int!
818-
"The percent of indexing rewards generated by the delegated stake that the Indexer keeps for itself"
820+
"The percent of indexing rewards generated by the total stake that the Indexer keeps for itself. In parts per million. This is the indexingRewardCut before Horizon"
821+
legacyIndexingRewardCut: Int!
822+
"[Horizon only] The percent of indexing rewards generated by the delegated stake that the Indexer keeps for itself"
819823
indexingRewardEffectiveCut: BigDecimal!
824+
"The percent of indexing rewards generated by the delegated stake that the Indexer keeps for itself. This is the indexingRewardEffectiveCut before Horizon"
825+
legacyIndexingRewardEffectiveCut: BigDecimal!
820826
"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."
821827
overDelegationDilution: BigDecimal!
822828
"The total amount of query fees given to delegators"
823829
delegatorQueryFees: BigInt!
824-
"The percent of query rebate rewards the Indexer keeps for itself. In parts per million"
830+
"[Horizon only] The percent of query rebate rewards generated by the total stake that the Indexer keeps for itself. In parts per million"
825831
queryFeeCut: Int!
826-
"The percent of query rebate rewards generated by the delegated stake that the Indexer keeps for itself"
832+
"The percent of query rebate rewards generated by the total stake that the Indexer keeps for itself. In parts per million. This is the queryFeeCut before Horizon"
833+
legacyQueryFeeCut: Int!
834+
"[Horizon only] The percent of query rebate rewards generated by the delegated stake that the Indexer keeps for itself"
827835
queryFeeEffectiveCut: BigDecimal!
836+
"The percent of query rebate rewards generated by the delegated stake that the Indexer keeps for itself. This is the queryFeeEffectiveCut before Horizon"
837+
legacyQueryFeeEffectiveCut: BigDecimal!
828838
"Amount of blocks a delegator chooses for the waiting period for changing their params"
829839
delegatorParameterCooldown: Int!
830840
"Block number for the last time the delegator updated their parameters"
@@ -834,14 +844,6 @@ type Indexer @entity(immutable: false) {
834844

835845
# Provisioning
836846
provisions: [Provision!]! @derivedFrom(field: "indexer")
837-
838-
# Metrics
839-
"NOT IMPLEMENTED - Total return this indexer has earned"
840-
totalReturn: BigDecimal!
841-
"NOT IMPLEMENTED - Annualized rate of return for the indexer"
842-
annualizedReturn: BigDecimal! # You must multiple by 100 to get percentage
843-
"NOT IMPLEMENTED - Staking efficiency of the indexer"
844-
stakingEfficiency: BigDecimal!
845847
}
846848

847849
type PaymentSource @entity(immutable: false) {

src/mappings/helpers/helpers.ts

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export function createOrLoadIndexer(indexerAddress: Bytes, timestamp: BigInt ):
153153
indexer.queryFeeRebates = BigInt.fromI32(0)
154154
indexer.rewardsEarned = BigInt.fromI32(0)
155155
indexer.indexerRewardsOwnGenerationRatio = BigDecimal.fromString('0')
156+
indexer.legacyIndexerRewardsOwnGenerationRatio = BigDecimal.fromString('0')
156157

157158
indexer.delegatedCapacity = BigInt.fromI32(0)
158159
indexer.tokenCapacity = BigInt.fromI32(0)
@@ -163,24 +164,24 @@ export function createOrLoadIndexer(indexerAddress: Bytes, timestamp: BigInt ):
163164
indexer.delegatedStakeRatio = BigDecimal.fromString('0')
164165
indexer.delegatorShares = BigInt.fromI32(0)
165166
indexer.delegationExchangeRate = BigDecimal.fromString('1')
166-
indexer.indexingRewardCut = 0
167+
indexer.indexingRewardCut = 1000000
167168
indexer.indexingRewardEffectiveCut = BigDecimal.fromString('0')
169+
indexer.legacyIndexingRewardCut = 0
170+
indexer.legacyIndexingRewardEffectiveCut = BigDecimal.fromString('0')
168171
indexer.overDelegationDilution = BigDecimal.fromString('0')
169172
indexer.delegatorIndexingRewards = BigInt.fromI32(0)
170173
indexer.indexerIndexingRewards = BigInt.fromI32(0)
171174
indexer.delegatorQueryFees = BigInt.fromI32(0)
172-
indexer.queryFeeCut = 0
175+
indexer.queryFeeCut = 1000000
173176
indexer.queryFeeEffectiveCut = BigDecimal.fromString('0')
177+
indexer.legacyQueryFeeCut = 0
178+
indexer.legacyQueryFeeEffectiveCut = BigDecimal.fromString('0')
174179
indexer.delegatorParameterCooldown = 0
175180
indexer.lastDelegationParameterUpdate = 0
176181
indexer.forcedClosures = 0
177182
indexer.allocationCount = 0
178183
indexer.totalAllocationCount = BigInt.fromI32(0)
179184

180-
indexer.totalReturn = BigDecimal.fromString('0')
181-
indexer.annualizedReturn = BigDecimal.fromString('0')
182-
indexer.stakingEfficiency = BigDecimal.fromString('0')
183-
184185
indexer.url = ''
185186
indexer.geoHash = ''
186187
indexer.rewardsDestination = Address.fromString('0x0000000000000000000000000000000000000000')
@@ -226,9 +227,9 @@ export function createOrLoadProvision(indexerAddress: Bytes, verifierAddress: By
226227
provision.maxVerifierCutPending = BigInt.fromI32(0)
227228
provision.thawingPeriod = BigInt.fromI32(0)
228229
provision.thawingPeriodPending = BigInt.fromI32(0)
229-
provision.queryFeeCut = BigInt.fromI32(0)
230-
provision.indexingFeeCut = BigInt.fromI32(0)
231-
provision.indexingRewardsCut = BigInt.fromI32(0)
230+
provision.queryFeeCut = BigInt.fromI32(1000000)
231+
provision.indexingFeeCut = BigInt.fromI32(1000000)
232+
provision.indexingRewardsCut = BigInt.fromI32(1000000)
232233
provision.indexingRewardEffectiveCut = BigInt.fromI32(0).toBigDecimal()
233234
provision.queryFeeEffectiveCut = BigInt.fromI32(0).toBigDecimal()
234235
provision.overDelegationDilution = BigInt.fromI32(0).toBigDecimal()
@@ -956,25 +957,44 @@ export function calculateDelegatedStakeRatio(indexer: Indexer): BigDecimal {
956957
}
957958

958959
export function calculateIndexingRewardEffectiveCut(indexer: Indexer): BigDecimal {
959-
let delegatorCut =
960-
BigInt.fromI32(1000000 - indexer.indexingRewardCut).toBigDecimal() /
961-
BigDecimal.fromString('1000000')
960+
let delegatorCut = BigInt.fromI32(indexer.indexingRewardCut).toBigDecimal() / BigDecimal.fromString('1000000')
962961
return indexer.delegatedStakeRatio == BigDecimal.fromString('0')
963962
? BigDecimal.fromString('0')
964963
: BigDecimal.fromString('1') - delegatorCut / indexer.delegatedStakeRatio
965964
}
966965

967966
export function calculateQueryFeeEffectiveCut(indexer: Indexer): BigDecimal {
968-
let delegatorCut =
969-
BigInt.fromI32(1000000 - indexer.queryFeeCut).toBigDecimal() / BigDecimal.fromString('1000000')
967+
let delegatorCut = BigInt.fromI32(indexer.queryFeeCut).toBigDecimal() / BigDecimal.fromString('1000000')
970968
return indexer.delegatedStakeRatio == BigDecimal.fromString('0')
971969
? BigDecimal.fromString('0')
972970
: BigDecimal.fromString('1') - delegatorCut / indexer.delegatedStakeRatio
973971
}
974972

975973
export function calculateIndexerRewardOwnGenerationRatio(indexer: Indexer): BigDecimal {
976974
let rewardCut =
977-
BigInt.fromI32(indexer.indexingRewardCut).toBigDecimal() / BigDecimal.fromString('1000000')
975+
BigInt.fromI32(1000000 -indexer.indexingRewardCut).toBigDecimal() / BigDecimal.fromString('1000000')
976+
return indexer.ownStakeRatio == BigDecimal.fromString('0')
977+
? BigDecimal.fromString('0')
978+
: rewardCut / indexer.ownStakeRatio
979+
}
980+
981+
export function calculateLegacyIndexingRewardEffectiveCut(indexer: Indexer): BigDecimal {
982+
let delegatorCut = BigInt.fromI32(indexer.legacyIndexingRewardCut).toBigDecimal() / BigDecimal.fromString('1000000')
983+
return indexer.delegatedStakeRatio == BigDecimal.fromString('0')
984+
? BigDecimal.fromString('0')
985+
: BigDecimal.fromString('1') - delegatorCut / indexer.delegatedStakeRatio
986+
}
987+
988+
export function calculateLegacyQueryFeeEffectiveCut(indexer: Indexer): BigDecimal {
989+
let delegatorCut = BigInt.fromI32(indexer.legacyQueryFeeCut).toBigDecimal() / BigDecimal.fromString('1000000')
990+
return indexer.delegatedStakeRatio == BigDecimal.fromString('0')
991+
? BigDecimal.fromString('0')
992+
: BigDecimal.fromString('1') - delegatorCut / indexer.delegatedStakeRatio
993+
}
994+
995+
export function calculateLegacyIndexerRewardOwnGenerationRatio(indexer: Indexer): BigDecimal {
996+
let rewardCut =
997+
BigInt.fromI32(1000000 - indexer.legacyIndexingRewardCut).toBigDecimal() / BigDecimal.fromString('1000000')
978998
return indexer.ownStakeRatio == BigDecimal.fromString('0')
979999
? BigDecimal.fromString('0')
9801000
: rewardCut / indexer.ownStakeRatio
@@ -1004,6 +1024,18 @@ export function updateAdvancedIndexerMetrics(indexer: Indexer): Indexer {
10041024
return indexer as Indexer
10051025
}
10061026

1027+
export function updateLegacyAdvancedIndexerMetrics(indexer: Indexer): Indexer {
1028+
indexer.ownStakeRatio = calculateOwnStakeRatio(indexer as Indexer)
1029+
indexer.delegatedStakeRatio = calculateDelegatedStakeRatio(indexer as Indexer)
1030+
indexer.legacyIndexingRewardEffectiveCut = calculateLegacyIndexingRewardEffectiveCut(indexer as Indexer)
1031+
indexer.legacyQueryFeeEffectiveCut = calculateLegacyQueryFeeEffectiveCut(indexer as Indexer)
1032+
indexer.legacyIndexerRewardsOwnGenerationRatio = calculateLegacyIndexerRewardOwnGenerationRatio(
1033+
indexer as Indexer,
1034+
)
1035+
indexer.overDelegationDilution = calculateOverdelegationDilution(indexer as Indexer)
1036+
return indexer as Indexer
1037+
}
1038+
10071039
export function calculateOwnStakeRatioForProvision(provision: Provision): BigDecimal {
10081040
let totalTokens = provision.tokensProvisioned.plus(provision.delegatedTokens)
10091041
return totalTokens == BigInt.fromI32(0)
@@ -1020,7 +1052,7 @@ export function calculateDelegatedStakeRatioForProvision(provision: Provision):
10201052

10211053
export function calculateIndexingRewardEffectiveCutForProvision(provision: Provision): BigDecimal {
10221054
let delegatorCut =
1023-
provision.indexingRewardsCut.toBigDecimal() /
1055+
BigInt.fromI32(1000000).minus(provision.indexingRewardsCut).toBigDecimal() /
10241056
BigDecimal.fromString('1000000')
10251057
return provision.delegatedStakeRatio == BigDecimal.fromString('0')
10261058
? BigDecimal.fromString('0')
@@ -1029,15 +1061,17 @@ export function calculateIndexingRewardEffectiveCutForProvision(provision: Provi
10291061

10301062
export function calculateQueryFeeEffectiveCutForProvision(provision: Provision): BigDecimal {
10311063
let delegatorCut =
1032-
provision.queryFeeCut.toBigDecimal() / BigDecimal.fromString('1000000')
1064+
BigInt.fromI32(1000000).minus(provision.queryFeeCut).toBigDecimal() /
1065+
BigDecimal.fromString('1000000')
10331066
return provision.delegatedStakeRatio == BigDecimal.fromString('0')
10341067
? BigDecimal.fromString('0')
10351068
: BigDecimal.fromString('1') - delegatorCut / provision.delegatedStakeRatio
10361069
}
10371070

10381071
export function calculateIndexerRewardOwnGenerationRatioForProvision(provision: Provision): BigDecimal {
10391072
let delegatorCut =
1040-
provision.indexingRewardsCut.toBigDecimal() / BigDecimal.fromString('1000000')
1073+
BigInt.fromI32(1000000).minus(provision.indexingRewardsCut).toBigDecimal() /
1074+
BigDecimal.fromString('1000000')
10411075
return provision.ownStakeRatio == BigDecimal.fromString('0')
10421076
? BigDecimal.fromString('0')
10431077
: (BigDecimal.fromString('1') - delegatorCut) / provision.ownStakeRatio
@@ -1215,7 +1249,7 @@ export function batchUpdateSubgraphSignalledTokens(deployment: SubgraphDeploymen
12151249
}
12161250
}
12171251

1218-
export function convertBigIntSubgraphIDToBase58(bigIntRepresentation: BigInt): String {
1252+
export function convertBigIntSubgraphIDToBase58(bigIntRepresentation: BigInt): string {
12191253
// Might need to unpad the BigInt since `fromUnsignedBytes` pads one byte with a zero.
12201254
// Although for the events where the uint256 is provided, we probably don't need to unpad.
12211255
let hexString = bigIntRepresentation.toHexString()

src/mappings/horizonStaking.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,21 @@ export function handleOperatorSet(event: OperatorSet): void {
218218

219219
export function handleDelegationFeeCutSet(event: DelegationFeeCutSet): void {
220220
let provision = createOrLoadProvision(event.params.serviceProvider, event.params.verifier, event.block.timestamp)
221-
provision.queryFeeCut = event.params.paymentType == 0 ? event.params.feeCut : provision.queryFeeCut
222-
provision.indexingFeeCut = event.params.paymentType == 1 ? event.params.feeCut : provision.indexingFeeCut
223-
provision.indexingRewardsCut = event.params.paymentType == 2 ? event.params.feeCut : provision.indexingRewardsCut
221+
222+
// Before horizon the cuts represented how much the indexer took home. After horizon they now represent how much the delegators get
223+
// To avoid confusion and more changes on explorer we invert the meaning here to keep it consistent.
224+
let invertedCut = BigInt.fromI32(1_000_000).minus(event.params.feeCut)
225+
provision.queryFeeCut = event.params.paymentType == 0 ? invertedCut : provision.queryFeeCut
226+
provision.indexingFeeCut = event.params.paymentType == 1 ? invertedCut : provision.indexingFeeCut
227+
provision.indexingRewardsCut = event.params.paymentType == 2 ? invertedCut : provision.indexingRewardsCut
228+
provision = updateAdvancedProvisionMetrics(provision as Provision)
224229
provision.save()
230+
231+
let indexer = Indexer.load(event.params.serviceProvider.toHexString())!
232+
indexer.indexingRewardCut = event.params.paymentType == 2 ? invertedCut.toI32() : indexer.indexingRewardCut
233+
indexer.queryFeeCut = event.params.paymentType == 0 ? invertedCut.toI32() : indexer.queryFeeCut
234+
indexer = updateAdvancedIndexerMetrics(indexer as Indexer)
235+
indexer.save()
225236
}
226237

227238
export function handleProvisionSlashed(event: ProvisionSlashed): void {

src/mappings/l1staking.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '../types/L1Staking/L1Staking'
77

88
import { Indexer, DelegatedStake, GraphNetwork } from '../types/schema'
9-
import { calculateCapacities, createOrLoadGraphNetwork, joinID, updateAdvancedIndexerMetrics, updateDelegationExchangeRate } from './helpers/helpers'
9+
import { calculateCapacities, createOrLoadGraphNetwork, joinID, updateLegacyAdvancedIndexerMetrics, updateDelegationExchangeRate } from './helpers/helpers'
1010

1111
/*
1212
/// @dev Emitted when an indexer transfers their stake to L2.
@@ -34,7 +34,7 @@ export function handleIndexerStakeTransferredToL2(event: IndexerStakeTransferred
3434
indexer.lastTransferredToL2At = event.block.timestamp
3535
indexer.lastTransferredToL2AtBlockNumber = event.block.number
3636
indexer.lastTransferredToL2AtTx = event.transaction.hash.toHexString()
37-
indexer = updateAdvancedIndexerMetrics(indexer as Indexer)
37+
indexer = updateLegacyAdvancedIndexerMetrics(indexer as Indexer)
3838
indexer = calculateCapacities(indexer as Indexer)
3939
indexer.save()
4040

@@ -84,7 +84,7 @@ export function handleDelegationTransferredToL2(event: DelegationTransferredToL2
8484
if (indexer.delegatorShares != BigInt.fromI32(0)) {
8585
indexer = updateDelegationExchangeRate(indexer as Indexer)
8686
}
87-
indexer = updateAdvancedIndexerMetrics(indexer as Indexer)
87+
indexer = updateLegacyAdvancedIndexerMetrics(indexer as Indexer)
8888
indexer = calculateCapacities(indexer as Indexer)
8989
indexer.save()
9090

src/mappings/rewardsManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import {
1010
createOrLoadSubgraphDeployment,
1111
createOrLoadEpoch,
12-
updateAdvancedIndexerMetrics,
12+
updateLegacyAdvancedIndexerMetrics,
1313
updateDelegationExchangeRate,
1414
createOrLoadGraphNetwork
1515
} from './helpers/helpers'
@@ -28,7 +28,7 @@ export function handleRewardsAssigned(event: RewardsAssigned): void {
2828
indexer.delegatedTokens == BigInt.fromI32(0)
2929
? event.params.amount
3030
: event.params.amount
31-
.times(BigInt.fromI32(indexer.indexingRewardCut))
31+
.times(BigInt.fromI32(indexer.legacyIndexingRewardCut))
3232
.div(BigInt.fromI32(1000000))
3333

3434
let delegatorIndexingRewards = event.params.amount.minus(indexerIndexingRewards)
@@ -40,7 +40,7 @@ export function handleRewardsAssigned(event: RewardsAssigned): void {
4040
if (indexer.delegatorShares != BigInt.fromI32(0)) {
4141
indexer = updateDelegationExchangeRate(indexer as Indexer)
4242
}
43-
indexer = updateAdvancedIndexerMetrics(indexer as Indexer)
43+
indexer = updateLegacyAdvancedIndexerMetrics(indexer as Indexer)
4444
indexer.save()
4545

4646
// update allocation

0 commit comments

Comments
 (0)