@@ -216,6 +216,15 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
216
216
Staking (address (_proxy)).initialize (_controller);
217
217
}
218
218
219
+ /**
220
+ * @dev Set the minimum indexer stake required to.
221
+ * @param _minimumIndexerStake Minimum indexer stake
222
+ */
223
+ function setMinimumIndexerStake (uint256 _minimumIndexerStake ) external override onlyGovernor {
224
+ minimumIndexerStake = _minimumIndexerStake;
225
+ emit ParameterUpdated ("minimumIndexerStake " );
226
+ }
227
+
219
228
/**
220
229
* @dev Set the thawing period for unstaking.
221
230
* @param _thawingPeriod Period in blocks to wait for token withdrawals after unstaking
@@ -478,6 +487,7 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
478
487
479
488
/**
480
489
* @dev Get the total amount of tokens available to use in allocations.
490
+ * This considers the indexer stake and delegated tokens according to delegation ratio
481
491
* @param _indexer Address of the indexer
482
492
* @return Amount of tokens staked by the indexer
483
493
*/
@@ -490,20 +500,7 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
490
500
? pool.tokens
491
501
: tokensDelegatedMax;
492
502
493
- uint256 tokensUsed = indexerStake.tokensUsed ();
494
- uint256 tokensCapacity = indexerStake.tokensStaked.add (tokensDelegated);
495
-
496
- // If more tokens are used than the current capacity, the indexer is overallocated.
497
- // This means the indexer doesn't have available capacity to create new allocations.
498
- // We can reach this state when the indexer has funds allocated and then any
499
- // of these conditions happen:
500
- // - The delegationRatio ratio is reduced.
501
- // - The indexer stake is slashed.
502
- // - A delegator removes enough stake.
503
- if (tokensUsed > tokensCapacity) {
504
- return 0 ;
505
- }
506
- return tokensCapacity.sub (tokensUsed);
503
+ return indexerStake.tokensAvailableWithDelegation (tokensDelegated);
507
504
}
508
505
509
506
/**
@@ -533,6 +530,12 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
533
530
function stakeTo (address _indexer , uint256 _tokens ) public override notPartialPaused {
534
531
require (_tokens > 0 , "Cannot stake zero tokens " );
535
532
533
+ // Ensure minimum stake
534
+ require (
535
+ stakes[_indexer].tokensSecureStake ().add (_tokens) >= minimumIndexerStake,
536
+ "Stake must be above minimum required "
537
+ );
538
+
536
539
// Transfer tokens to stake from caller to this contract
537
540
require (graphToken ().transferFrom (msg .sender , address (this ), _tokens), "!transfer " );
538
541
@@ -554,6 +557,13 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
554
557
"Not enough tokens available to unstake "
555
558
);
556
559
560
+ // Ensure minimum stake
561
+ uint256 newStake = indexerStake.tokensSecureStake ().sub (_tokens);
562
+ require (
563
+ newStake == 0 || newStake >= minimumIndexerStake,
564
+ "Stake must be above minimum required "
565
+ );
566
+
557
567
indexerStake.lockTokens (_tokens, thawingPeriod);
558
568
559
569
emit StakeLocked (indexer, indexerStake.tokensLocked, indexerStake.tokensLockedUntil);
@@ -564,10 +574,9 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
564
574
*/
565
575
function withdraw () external override notPaused {
566
576
address indexer = msg .sender ;
567
- Stakes.Indexer storage indexerStake = stakes[indexer];
568
577
569
578
// Get tokens available for withdraw and update balance
570
- uint256 tokensToWithdraw = indexerStake .withdrawTokens ();
579
+ uint256 tokensToWithdraw = stakes[indexer] .withdrawTokens ();
571
580
require (tokensToWithdraw > 0 , "No tokens available to withdraw " );
572
581
573
582
// Return tokens to the indexer
@@ -849,7 +858,10 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
849
858
}
850
859
851
860
/**
852
- * @dev Stake tokens on the indexer
861
+ * @dev Stake tokens on the indexer.
862
+ * This function does not check minimum indexer stake requirement to allow
863
+ * to be called by functions that increase the stake when collecting rewards
864
+ * without reverting
853
865
* @param _indexer Address of staking party
854
866
* @param _tokens Amount of tokens to stake
855
867
*/
0 commit comments