Skip to content

Commit af5494c

Browse files
committed
fix: capacity calculations
Signed-off-by: Tomás Migone <[email protected]>
1 parent 797fb8c commit af5494c

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/mappings/helpers/helpers.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,16 +1155,18 @@ export function updateDelegationExchangeRateForProvision(provision: Provision):
11551155
// Need to remove, or find a fix
11561156
export function calculateCapacities(indexer: Indexer): Indexer {
11571157
let graphNetwork = GraphNetwork.load('1')!
1158-
let tokensDelegatedMax = indexer.stakedTokens.times(BigInt.fromI32(graphNetwork.delegationRatio))
1158+
let tokensDelegatedMax = indexer.provisionedTokens.times(BigInt.fromI32(graphNetwork.delegationRatio))
11591159

11601160
// Eligible to add to the capacity
1161+
let delegatedTokens = indexer.delegatedTokens.minus(indexer.delegatedThawingTokens)
11611162
indexer.delegatedCapacity =
1162-
indexer.delegatedTokens < tokensDelegatedMax ? indexer.delegatedTokens : tokensDelegatedMax
1163+
delegatedTokens < tokensDelegatedMax ? delegatedTokens : tokensDelegatedMax
11631164

1164-
indexer.tokenCapacity = indexer.stakedTokens.plus(indexer.delegatedCapacity)
1165+
indexer.tokenCapacity = indexer.provisionedTokens
1166+
.minus(indexer.thawingTokens)
1167+
.plus(indexer.delegatedCapacity)
11651168
indexer.availableStake = indexer.tokenCapacity
11661169
.minus(indexer.allocatedTokens)
1167-
.minus(indexer.lockedTokens)
11681170
return indexer
11691171
}
11701172

src/mappings/horizonStaking.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BigInt } from '@graphprotocol/graph-ts'
22
import { addresses } from '../../config/addresses'
33
import { AllowedLockedVerifierSet, DelegatedTokensWithdrawn, DelegationFeeCutSet, DelegationSlashed, DelegationSlashingEnabled, HorizonStakeDeposited, HorizonStakeLocked, HorizonStakeWithdrawn, MaxThawingPeriodSet, OperatorSet, StakeDelegatedWithdrawn, ThawingPeriodCleared, TokensDelegated, TokensDeprovisioned, TokensToDelegationPoolAdded, TokensUndelegated } from '../types/HorizonStaking/HorizonStaking'
44
import { DelegatedStake, Delegator, Indexer, Provision, ThawRequest } from '../types/schema'
5-
import { createOrLoadDataService, createOrLoadDelegatedStakeForProvision, createOrLoadDelegator, createOrLoadEpoch, createOrLoadGraphAccount, createOrLoadGraphNetwork, createOrLoadHorizonOperator, createOrLoadIndexer, createOrLoadProvision, joinID, updateAdvancedIndexerMetrics, updateAdvancedProvisionMetrics, updateDelegationExchangeRate, updateDelegationExchangeRateForProvision } from './helpers/helpers'
5+
import { calculateCapacities, createOrLoadDataService, createOrLoadDelegatedStakeForProvision, createOrLoadDelegator, createOrLoadEpoch, createOrLoadGraphAccount, createOrLoadGraphNetwork, createOrLoadHorizonOperator, createOrLoadIndexer, createOrLoadProvision, joinID, updateAdvancedIndexerMetrics, updateAdvancedProvisionMetrics, updateDelegationExchangeRate, updateDelegationExchangeRateForProvision } from './helpers/helpers'
66
import {
77
ProvisionCreated,
88
ProvisionIncreased,
@@ -126,6 +126,7 @@ export function handleProvisionThawed(event: ProvisionThawed): void {
126126
let provision = createOrLoadProvision(event.params.serviceProvider, event.params.verifier, event.block.timestamp)
127127

128128
indexer.thawingTokens = indexer.thawingTokens.plus(event.params.tokens)
129+
indexer = calculateCapacities(indexer as Indexer)
129130
indexer.save()
130131

131132
dataService.totalTokensThawing = dataService.totalTokensThawing.plus(event.params.tokens)
@@ -146,6 +147,7 @@ export function handleTokensDeprovisioned(event: TokensDeprovisioned): void {
146147

147148
indexer.provisionedTokens = indexer.provisionedTokens.minus(event.params.tokens)
148149
indexer.thawingTokens = indexer.thawingTokens.minus(event.params.tokens)
150+
indexer = calculateCapacities(indexer as Indexer)
149151
indexer.save()
150152

151153
dataService.totalTokensProvisioned = dataService.totalTokensProvisioned.minus(event.params.tokens)

src/mappings/subgraphService.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BigDecimal, BigInt, Bytes, ethereum, log } from "@graphprotocol/graph-ts"
22
import { AllocationClosed, AllocationCreated, AllocationResized, CurationCutSet, DelegationRatioSet, IndexingRewardsCollected, MaxPOIStalenessSet, ProvisionTokensRangeSet, QueryFeesCollected, RewardsDestinationSet, ServiceProviderRegistered, StakeToFeesRatioSet, ThawingPeriodRangeSet, VerifierCutRangeSet } from "../types/SubgraphService/SubgraphService"
3-
import { batchUpdateSubgraphSignalledTokens, calculatePricePerShare, createOrLoadDataService, createOrLoadGraphNetwork, createOrLoadEpoch,createOrLoadIndexerQueryFeePaymentAggregation, createOrLoadPaymentSource, createOrLoadProvision, createOrLoadSubgraphDeployment, joinID, updateDelegationExchangeRate } from "./helpers/helpers"
3+
import { batchUpdateSubgraphSignalledTokens, calculatePricePerShare, createOrLoadDataService, createOrLoadGraphNetwork, createOrLoadEpoch,createOrLoadIndexerQueryFeePaymentAggregation, createOrLoadPaymentSource, createOrLoadProvision, createOrLoadSubgraphDeployment, joinID, updateDelegationExchangeRate, calculateCapacities } from "./helpers/helpers"
44
import { Allocation, Indexer, PoiSubmission, SubgraphDeployment } from "../types/schema"
55
import { addresses } from "../../config/addresses"
66
import { tuplePrefixBytes } from "./helpers/decoder"
@@ -64,6 +64,7 @@ export function handleAllocationCreated(event: AllocationCreated): void {
6464
indexer.allocatedTokens = indexer.allocatedTokens.plus(event.params.tokens)
6565
indexer.totalAllocationCount = indexer.totalAllocationCount.plus(BigInt.fromI32(1))
6666
indexer.allocationCount = indexer.allocationCount + 1
67+
indexer = calculateCapacities(indexer as Indexer)
6768
indexer.save()
6869

6970
// update provision
@@ -140,6 +141,7 @@ export function handleAllocationClosed(event: AllocationClosed): void {
140141

141142
indexer.allocatedTokens = indexer.allocatedTokens.minus(event.params.tokens)
142143
indexer.allocationCount = indexer.allocationCount - 1
144+
indexer = calculateCapacities(indexer as Indexer)
143145
indexer.save()
144146

145147
// update provision
@@ -189,6 +191,7 @@ export function handleAllocationResized(event: AllocationResized): void {
189191
// update indexer
190192
let indexer = Indexer.load(indexerID)!
191193
indexer.allocatedTokens = indexer.allocatedTokens.plus(diffTokens)
194+
indexer = calculateCapacities(indexer as Indexer)
192195
indexer.save()
193196

194197
// update provision

0 commit comments

Comments
 (0)