Skip to content

Commit 8f60bb9

Browse files
Merge branch 'juanmardefago/horizon-stage-1-signed' into juanmardefago/horizon-legacy-locked-tokens
2 parents 586d0a7 + e8f5971 commit 8f60bb9

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
@@ -773,8 +773,10 @@ type Indexer @entity(immutable: false) {
773773
indexerIndexingRewards: BigInt!
774774
"The total amount of indexing rewards given to delegators"
775775
delegatorIndexingRewards: BigInt!
776-
"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"
776+
"[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"
777777
indexerRewardsOwnGenerationRatio: BigDecimal!
778+
"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"
779+
legacyIndexerRewardsOwnGenerationRatio: BigDecimal!
778780
"Whether the indexer has been transferred from L1 to L2 partially or fully"
779781
transferredToL2: Boolean!
780782
"Timestamp for the FIRST L1 -> L2 Transfer"
@@ -817,18 +819,26 @@ type Indexer @entity(immutable: false) {
817819
delegatorShares: BigInt!
818820
"Exchange rate of of tokens received for each share"
819821
delegationExchangeRate: BigDecimal!
820-
"The percent of indexing rewards generated by the total stake that the Indexer keeps for itself. In parts per million"
822+
"[Horizon only] The percent of indexing rewards generated by the total stake that the Indexer keeps for itself. In parts per million"
821823
indexingRewardCut: Int!
822-
"The percent of indexing rewards generated by the delegated stake that the Indexer keeps for itself"
824+
"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"
825+
legacyIndexingRewardCut: Int!
826+
"[Horizon only] The percent of indexing rewards generated by the delegated stake that the Indexer keeps for itself"
823827
indexingRewardEffectiveCut: BigDecimal!
828+
"The percent of indexing rewards generated by the delegated stake that the Indexer keeps for itself. This is the indexingRewardEffectiveCut before Horizon"
829+
legacyIndexingRewardEffectiveCut: BigDecimal!
824830
"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."
825831
overDelegationDilution: BigDecimal!
826832
"The total amount of query fees given to delegators"
827833
delegatorQueryFees: BigInt!
828-
"The percent of query rebate rewards the Indexer keeps for itself. In parts per million"
834+
"[Horizon only] The percent of query rebate rewards generated by the total stake that the Indexer keeps for itself. In parts per million"
829835
queryFeeCut: Int!
830-
"The percent of query rebate rewards generated by the delegated stake that the Indexer keeps for itself"
836+
"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"
837+
legacyQueryFeeCut: Int!
838+
"[Horizon only] The percent of query rebate rewards generated by the delegated stake that the Indexer keeps for itself"
831839
queryFeeEffectiveCut: BigDecimal!
840+
"The percent of query rebate rewards generated by the delegated stake that the Indexer keeps for itself. This is the queryFeeEffectiveCut before Horizon"
841+
legacyQueryFeeEffectiveCut: BigDecimal!
832842
"Amount of blocks a delegator chooses for the waiting period for changing their params"
833843
delegatorParameterCooldown: Int!
834844
"Block number for the last time the delegator updated their parameters"
@@ -838,14 +848,6 @@ type Indexer @entity(immutable: false) {
838848

839849
# Provisioning
840850
provisions: [Provision!]! @derivedFrom(field: "indexer")
841-
842-
# Metrics
843-
"NOT IMPLEMENTED - Total return this indexer has earned"
844-
totalReturn: BigDecimal!
845-
"NOT IMPLEMENTED - Annualized rate of return for the indexer"
846-
annualizedReturn: BigDecimal! # You must multiple by 100 to get percentage
847-
"NOT IMPLEMENTED - Staking efficiency of the indexer"
848-
stakingEfficiency: BigDecimal!
849851
}
850852

851853
type PaymentSource @entity(immutable: false) {

src/mappings/helpers/helpers.ts

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

159160
indexer.delegatedCapacity = BigInt.fromI32(0)
160161
indexer.tokenCapacity = BigInt.fromI32(0)
@@ -165,24 +166,24 @@ export function createOrLoadIndexer(indexerAddress: Bytes, timestamp: BigInt ):
165166
indexer.delegatedStakeRatio = BigDecimal.fromString('0')
166167
indexer.delegatorShares = BigInt.fromI32(0)
167168
indexer.delegationExchangeRate = BigDecimal.fromString('1')
168-
indexer.indexingRewardCut = 0
169+
indexer.indexingRewardCut = 1000000
169170
indexer.indexingRewardEffectiveCut = BigDecimal.fromString('0')
171+
indexer.legacyIndexingRewardCut = 0
172+
indexer.legacyIndexingRewardEffectiveCut = BigDecimal.fromString('0')
170173
indexer.overDelegationDilution = BigDecimal.fromString('0')
171174
indexer.delegatorIndexingRewards = BigInt.fromI32(0)
172175
indexer.indexerIndexingRewards = BigInt.fromI32(0)
173176
indexer.delegatorQueryFees = BigInt.fromI32(0)
174-
indexer.queryFeeCut = 0
177+
indexer.queryFeeCut = 1000000
175178
indexer.queryFeeEffectiveCut = BigDecimal.fromString('0')
179+
indexer.legacyQueryFeeCut = 0
180+
indexer.legacyQueryFeeEffectiveCut = BigDecimal.fromString('0')
176181
indexer.delegatorParameterCooldown = 0
177182
indexer.lastDelegationParameterUpdate = 0
178183
indexer.forcedClosures = 0
179184
indexer.allocationCount = 0
180185
indexer.totalAllocationCount = BigInt.fromI32(0)
181186

182-
indexer.totalReturn = BigDecimal.fromString('0')
183-
indexer.annualizedReturn = BigDecimal.fromString('0')
184-
indexer.stakingEfficiency = BigDecimal.fromString('0')
185-
186187
indexer.url = ''
187188
indexer.geoHash = ''
188189
indexer.rewardsDestination = Address.fromString('0x0000000000000000000000000000000000000000')
@@ -228,9 +229,9 @@ export function createOrLoadProvision(indexerAddress: Bytes, verifierAddress: By
228229
provision.maxVerifierCutPending = BigInt.fromI32(0)
229230
provision.thawingPeriod = BigInt.fromI32(0)
230231
provision.thawingPeriodPending = BigInt.fromI32(0)
231-
provision.queryFeeCut = BigInt.fromI32(0)
232-
provision.indexingFeeCut = BigInt.fromI32(0)
233-
provision.indexingRewardsCut = BigInt.fromI32(0)
232+
provision.queryFeeCut = BigInt.fromI32(1000000)
233+
provision.indexingFeeCut = BigInt.fromI32(1000000)
234+
provision.indexingRewardsCut = BigInt.fromI32(1000000)
234235
provision.indexingRewardEffectiveCut = BigInt.fromI32(0).toBigDecimal()
235236
provision.queryFeeEffectiveCut = BigInt.fromI32(0).toBigDecimal()
236237
provision.overDelegationDilution = BigInt.fromI32(0).toBigDecimal()
@@ -960,25 +961,44 @@ export function calculateDelegatedStakeRatio(indexer: Indexer): BigDecimal {
960961
}
961962

962963
export function calculateIndexingRewardEffectiveCut(indexer: Indexer): BigDecimal {
963-
let delegatorCut =
964-
BigInt.fromI32(1000000 - indexer.indexingRewardCut).toBigDecimal() /
965-
BigDecimal.fromString('1000000')
964+
let delegatorCut = BigInt.fromI32(indexer.indexingRewardCut).toBigDecimal() / BigDecimal.fromString('1000000')
966965
return indexer.delegatedStakeRatio == BigDecimal.fromString('0')
967966
? BigDecimal.fromString('0')
968967
: BigDecimal.fromString('1') - delegatorCut / indexer.delegatedStakeRatio
969968
}
970969

971970
export function calculateQueryFeeEffectiveCut(indexer: Indexer): BigDecimal {
972-
let delegatorCut =
973-
BigInt.fromI32(1000000 - indexer.queryFeeCut).toBigDecimal() / BigDecimal.fromString('1000000')
971+
let delegatorCut = BigInt.fromI32(indexer.queryFeeCut).toBigDecimal() / BigDecimal.fromString('1000000')
974972
return indexer.delegatedStakeRatio == BigDecimal.fromString('0')
975973
? BigDecimal.fromString('0')
976974
: BigDecimal.fromString('1') - delegatorCut / indexer.delegatedStakeRatio
977975
}
978976

979977
export function calculateIndexerRewardOwnGenerationRatio(indexer: Indexer): BigDecimal {
980978
let rewardCut =
981-
BigInt.fromI32(indexer.indexingRewardCut).toBigDecimal() / BigDecimal.fromString('1000000')
979+
BigInt.fromI32(1000000 -indexer.indexingRewardCut).toBigDecimal() / BigDecimal.fromString('1000000')
980+
return indexer.ownStakeRatio == BigDecimal.fromString('0')
981+
? BigDecimal.fromString('0')
982+
: rewardCut / indexer.ownStakeRatio
983+
}
984+
985+
export function calculateLegacyIndexingRewardEffectiveCut(indexer: Indexer): BigDecimal {
986+
let delegatorCut = BigInt.fromI32(indexer.legacyIndexingRewardCut).toBigDecimal() / BigDecimal.fromString('1000000')
987+
return indexer.delegatedStakeRatio == BigDecimal.fromString('0')
988+
? BigDecimal.fromString('0')
989+
: BigDecimal.fromString('1') - delegatorCut / indexer.delegatedStakeRatio
990+
}
991+
992+
export function calculateLegacyQueryFeeEffectiveCut(indexer: Indexer): BigDecimal {
993+
let delegatorCut = BigInt.fromI32(indexer.legacyQueryFeeCut).toBigDecimal() / BigDecimal.fromString('1000000')
994+
return indexer.delegatedStakeRatio == BigDecimal.fromString('0')
995+
? BigDecimal.fromString('0')
996+
: BigDecimal.fromString('1') - delegatorCut / indexer.delegatedStakeRatio
997+
}
998+
999+
export function calculateLegacyIndexerRewardOwnGenerationRatio(indexer: Indexer): BigDecimal {
1000+
let rewardCut =
1001+
BigInt.fromI32(1000000 - indexer.legacyIndexingRewardCut).toBigDecimal() / BigDecimal.fromString('1000000')
9821002
return indexer.ownStakeRatio == BigDecimal.fromString('0')
9831003
? BigDecimal.fromString('0')
9841004
: rewardCut / indexer.ownStakeRatio
@@ -1008,6 +1028,18 @@ export function updateAdvancedIndexerMetrics(indexer: Indexer): Indexer {
10081028
return indexer as Indexer
10091029
}
10101030

1031+
export function updateLegacyAdvancedIndexerMetrics(indexer: Indexer): Indexer {
1032+
indexer.ownStakeRatio = calculateOwnStakeRatio(indexer as Indexer)
1033+
indexer.delegatedStakeRatio = calculateDelegatedStakeRatio(indexer as Indexer)
1034+
indexer.legacyIndexingRewardEffectiveCut = calculateLegacyIndexingRewardEffectiveCut(indexer as Indexer)
1035+
indexer.legacyQueryFeeEffectiveCut = calculateLegacyQueryFeeEffectiveCut(indexer as Indexer)
1036+
indexer.legacyIndexerRewardsOwnGenerationRatio = calculateLegacyIndexerRewardOwnGenerationRatio(
1037+
indexer as Indexer,
1038+
)
1039+
indexer.overDelegationDilution = calculateOverdelegationDilution(indexer as Indexer)
1040+
return indexer as Indexer
1041+
}
1042+
10111043
export function calculateOwnStakeRatioForProvision(provision: Provision): BigDecimal {
10121044
let totalTokens = provision.tokensProvisioned.plus(provision.delegatedTokens)
10131045
return totalTokens == BigInt.fromI32(0)
@@ -1024,7 +1056,7 @@ export function calculateDelegatedStakeRatioForProvision(provision: Provision):
10241056

10251057
export function calculateIndexingRewardEffectiveCutForProvision(provision: Provision): BigDecimal {
10261058
let delegatorCut =
1027-
provision.indexingRewardsCut.toBigDecimal() /
1059+
BigInt.fromI32(1000000).minus(provision.indexingRewardsCut).toBigDecimal() /
10281060
BigDecimal.fromString('1000000')
10291061
return provision.delegatedStakeRatio == BigDecimal.fromString('0')
10301062
? BigDecimal.fromString('0')
@@ -1033,15 +1065,17 @@ export function calculateIndexingRewardEffectiveCutForProvision(provision: Provi
10331065

10341066
export function calculateQueryFeeEffectiveCutForProvision(provision: Provision): BigDecimal {
10351067
let delegatorCut =
1036-
provision.queryFeeCut.toBigDecimal() / BigDecimal.fromString('1000000')
1068+
BigInt.fromI32(1000000).minus(provision.queryFeeCut).toBigDecimal() /
1069+
BigDecimal.fromString('1000000')
10371070
return provision.delegatedStakeRatio == BigDecimal.fromString('0')
10381071
? BigDecimal.fromString('0')
10391072
: BigDecimal.fromString('1') - delegatorCut / provision.delegatedStakeRatio
10401073
}
10411074

10421075
export function calculateIndexerRewardOwnGenerationRatioForProvision(provision: Provision): BigDecimal {
10431076
let delegatorCut =
1044-
provision.indexingRewardsCut.toBigDecimal() / BigDecimal.fromString('1000000')
1077+
BigInt.fromI32(1000000).minus(provision.indexingRewardsCut).toBigDecimal() /
1078+
BigDecimal.fromString('1000000')
10451079
return provision.ownStakeRatio == BigDecimal.fromString('0')
10461080
? BigDecimal.fromString('0')
10471081
: (BigDecimal.fromString('1') - delegatorCut) / provision.ownStakeRatio
@@ -1219,7 +1253,7 @@ export function batchUpdateSubgraphSignalledTokens(deployment: SubgraphDeploymen
12191253
}
12201254
}
12211255

1222-
export function convertBigIntSubgraphIDToBase58(bigIntRepresentation: BigInt): String {
1256+
export function convertBigIntSubgraphIDToBase58(bigIntRepresentation: BigInt): string {
12231257
// Might need to unpad the BigInt since `fromUnsignedBytes` pads one byte with a zero.
12241258
// Although for the events where the uint256 is provided, we probably don't need to unpad.
12251259
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)