Skip to content

Commit 81b4a25

Browse files
authored
Merge pull request #312 from graphprotocol/mde/fix-staking-legacy-call
2 parents 8a08240 + e0bb7f9 commit 81b4a25

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"prepublishOnly": "yarn & yarn build:ipfs",
1414
"build": "graph build",
1515
"build:ipfs:mainnet": "yarn && yarn prepare:mainnet && graph build --ipfs https://ipfs.network.thegraph.com",
16-
"deploy-mainnet": "yarn && yarn prepare:mainnet && graph deploy --studio graph-network-ethereum",
17-
"deploy-arbitrum": "yarn && yarn prepare:arbitrum && graph deploy --studio graph-network-arbitrum",
18-
"deploy-sepolia": "yarn && yarn prepare:sepolia && graph deploy --studio graph-network-sepolia",
19-
"deploy-arbitrum-sepolia": "yarn && yarn prepare:arbitrum-sepolia && graph deploy --studio graph-network-arbitrum-sepolia",
16+
"deploy-mainnet": "yarn && yarn prepare:mainnet && graph deploy graph-network-ethereum",
17+
"deploy-arbitrum": "yarn && yarn prepare:arbitrum && graph deploy graph-network-arbitrum",
18+
"deploy-sepolia": "yarn && yarn prepare:sepolia && graph deploy graph-network-sepolia",
19+
"deploy-arbitrum-sepolia": "yarn && yarn prepare:arbitrum-sepolia && graph deploy graph-network-arbitrum-sepolia",
2020
"deploy-arbitrum-sepolia-test": "yarn && yarn prepare:arbitrum-sepolia && graph deploy horizon-testing-arb-sepolia -l test-old",
2121
"deploy-studio": "yarn deploy-mainnet && yarn deploy-arbitrum && yarn deploy-sepolia && yarn deploy-arbitrum-sepolia",
2222
"prep:addresses:sepolia": "ts-node config/sepoliaAddressScript.ts && mustache ./config/generatedAddresses.json ./config/addresses.template.ts > ./config/addresses.ts",

schema.graphql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,10 @@ type Indexer @entity(immutable: false) {
745745
provisionedTokens: BigInt!
746746
"CURRENT tokens thawing from provisions to data services in the protocol. Only for Horizon"
747747
thawingTokens: BigInt!
748-
"CURRENT tokens allocated on all subgraphs"
748+
"CURRENT tokens allocated on all subgraphs - including legacy and horizon allocations"
749749
allocatedTokens: BigInt!
750+
"[Legacy only] CURRENT tokens allocated on all subgraphs ONLY for legacy allocations"
751+
legacyAllocatedTokens: BigInt!
750752
"NOT IMPLEMENTED - Tokens that have been unstaked and withdrawn"
751753
unstakedTokens: BigInt! # will be used for return % calcs
752754
"CURRENT tokens locked"
@@ -1509,6 +1511,7 @@ enum DisputeType {
15091511
SingleQuery
15101512
Conflicting
15111513
Indexing
1514+
Legacy
15121515
}
15131516

15141517
enum DisputeStatus {

src/mappings/helpers/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export function createOrLoadIndexer(indexerAddress: Bytes, timestamp: BigInt ):
146146
indexer.provisionedTokens = BigInt.fromI32(0)
147147
indexer.thawingTokens = BigInt.fromI32(0)
148148
indexer.allocatedTokens = BigInt.fromI32(0)
149+
indexer.legacyAllocatedTokens = BigInt.fromI32(0)
149150
indexer.lockedTokens = BigInt.fromI32(0)
150151
indexer.legacyLockedTokens = BigInt.fromI32(0)
151152
indexer.unstakedTokens = BigInt.fromI32(0)

src/mappings/staking.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export function handleStakeDeposited(event: StakeDeposited): void {
9898

9999
/**
100100
* @dev handleStakeLocked
101+
* Handler for legacy stake locking
101102
* - updated the Indexers stake
102103
* - note - the contracts work by not changing the tokensStaked amount, so here, capacity does not
103104
* get changed
@@ -127,6 +128,7 @@ export function handleStakeLocked(event: StakeLocked): void {
127128

128129
/**
129130
* @dev handleStakeWithdrawn
131+
* Handler for legacy stake withdrawal
130132
* - updated the Indexers stake
131133
* - updates the GraphNetwork total stake
132134
*/
@@ -161,21 +163,42 @@ export function handleStakeSlashed(event: StakeSlashed): void {
161163
let id = event.params.indexer.toHexString()
162164
let indexer = Indexer.load(id)!
163165

164-
indexer.stakedTokens = indexer.stakedTokens.minus(event.params.tokens)
166+
let slashedTokens = event.params.tokens
167+
168+
// When tokens are slashed, locked tokens might need to be unlocked if indexer overallocated
169+
if (slashedTokens.gt(BigInt.fromI32(0))) {
170+
let tokensUsed = indexer.legacyAllocatedTokens.plus(indexer.legacyLockedTokens)
171+
let tokensAvailable = tokensUsed.gt(indexer.stakedTokens)
172+
? BigInt.fromI32(0)
173+
: indexer.stakedTokens.minus(tokensUsed)
174+
175+
if (slashedTokens.gt(tokensAvailable) && indexer.legacyLockedTokens.gt(BigInt.fromI32(0))) {
176+
let tokensOverAllocated = slashedTokens.minus(tokensAvailable)
177+
// Calculate min(tokensOverAllocated, lockedTokens)
178+
let tokensToUnlock = tokensOverAllocated.lt(indexer.legacyLockedTokens)
179+
? tokensOverAllocated
180+
: indexer.legacyLockedTokens
181+
182+
indexer.legacyLockedTokens = indexer.legacyLockedTokens.minus(tokensToUnlock)
183+
indexer.lockedTokens = indexer.lockedTokens.minus(tokensToUnlock)
184+
185+
if (indexer.legacyLockedTokens.equals(BigInt.fromI32(0))) {
186+
indexer.legacyTokensLockedUntil = 0
187+
}
188+
if (indexer.lockedTokens.equals(BigInt.fromI32(0))) {
189+
indexer.tokensLockedUntil = 0
190+
}
191+
}
192+
}
193+
194+
indexer.stakedTokens = indexer.stakedTokens.minus(slashedTokens)
165195

166-
// We need to call into stakes mapping, because locked tokens might have been
167-
// decremented, and this is not released in the event
168-
// To fix this we would need to indicate in the event how many locked tokens were released
169-
let staking = Staking.bind(event.address)
170-
let indexerStored = staking.stakes(event.params.indexer)
171-
indexer.lockedTokens = indexerStored.tokensLocked
172-
indexer.legacyLockedTokens = indexerStored.tokensLocked
173196
indexer = updateLegacyAdvancedIndexerMetrics(indexer as Indexer)
174197
indexer = calculateCapacities(indexer as Indexer)
175198
indexer.save()
176199

177200
// Update graph network
178-
graphNetwork.totalTokensStaked = graphNetwork.totalTokensStaked.minus(event.params.tokens)
201+
graphNetwork.totalTokensStaked = graphNetwork.totalTokensStaked.minus(slashedTokens)
179202
graphNetwork.save()
180203
}
181204

@@ -337,6 +360,7 @@ export function handleAllocationCreated(event: AllocationCreated): void {
337360

338361
// update indexer
339362
let indexer = Indexer.load(indexerID)!
363+
indexer.legacyAllocatedTokens = indexer.legacyAllocatedTokens.plus(event.params.tokens)
340364
indexer.allocatedTokens = indexer.allocatedTokens.plus(event.params.tokens)
341365
indexer.totalAllocationCount = indexer.totalAllocationCount.plus(BigInt.fromI32(1))
342366
indexer.allocationCount = indexer.allocationCount + 1
@@ -517,6 +541,7 @@ export function handleAllocationClosed(event: AllocationClosed): void {
517541
allocation.forceClosed = false
518542
}
519543
indexer.allocatedTokens = indexer.allocatedTokens.minus(event.params.tokens)
544+
indexer.legacyAllocatedTokens = indexer.legacyAllocatedTokens.minus(event.params.tokens)
520545
indexer.allocationCount = indexer.allocationCount - 1
521546
indexer = updateLegacyAdvancedIndexerMetrics(indexer as Indexer)
522547
indexer = calculateCapacities(indexer as Indexer)
@@ -579,6 +604,7 @@ export function handleAllocationClosedCobbDouglas(event: AllocationClosed1): voi
579604
} else {
580605
allocation.forceClosed = false
581606
}
607+
indexer.legacyAllocatedTokens = indexer.legacyAllocatedTokens.minus(event.params.tokens)
582608
indexer.allocatedTokens = indexer.allocatedTokens.minus(event.params.tokens)
583609
indexer.allocationCount = indexer.allocationCount - 1
584610
indexer = updateLegacyAdvancedIndexerMetrics(indexer as Indexer)

0 commit comments

Comments
 (0)