Skip to content

Commit a4a0771

Browse files
committed
feat: allow anyone to close allocation after max allocation period
1 parent 726562c commit a4a0771

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

contracts/staking/Staking.sol

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
134134
* An amount of `tokens` get unallocated from `subgraphDeploymentID`.
135135
* The `effectiveAllocation` are the tokens allocated from creation to closing.
136136
* This event also emits the POI (proof of indexing) submitted by the indexer.
137-
* `isDelegator` is true if the sender was one of the indexer's delegators.
137+
* `isPublic` is true if the sender was someone other than the indexer.
138138
*/
139139
event AllocationClosed(
140140
address indexed indexer,
@@ -145,7 +145,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
145145
uint256 effectiveAllocation,
146146
address sender,
147147
bytes32 poi,
148-
bool isDelegator
148+
bool isPublic
149149
);
150150

151151
/**
@@ -1176,9 +1176,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
11761176
// Indexer or operator can close an allocation
11771177
// Delegators are also allowed but only after maxAllocationEpochs passed
11781178
bool isIndexer = _isAuth(alloc.indexer);
1179-
if (epochs > maxAllocationEpochs) {
1180-
require(isIndexer || isDelegator(alloc.indexer, msg.sender), "!auth-or-del");
1181-
} else {
1179+
if (epochs <= maxAllocationEpochs) {
11821180
require(isIndexer, "!auth");
11831181
}
11841182

test/lib/testHelpers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ export const advanceToNextEpoch = async (epochManager: EpochManager): Promise<vo
8484
await advanceBlockTo(nextEpochBlock)
8585
}
8686

87+
export const advanceEpochs = async (epochManager: EpochManager, n: number): Promise<void> => {
88+
for (let i = 0; i < n + 1; i++) {
89+
await advanceToNextEpoch(epochManager)
90+
}
91+
}
92+
8793
export const evmSnapshot = async (): Promise<number> => provider().send('evm_snapshot', [])
8894
export const evmRevert = async (id: number): Promise<boolean> => provider().send('evm_revert', [id])
8995

test/staking/allocation.test.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
toBN,
1616
toGRT,
1717
Account,
18+
advanceEpochs,
1819
} from '../lib/testHelpers'
1920

2021
const { AddressZero, HashZero } = constants
@@ -581,16 +582,14 @@ describe('Staking:Allocation', () => {
581582
await staking.connect(me.signer).closeAllocation(allocationID, poi)
582583
})
583584

584-
it('should close an allocation (by delegator)', async function () {
585+
it('should close an allocation (by public)', async function () {
586+
// Reject to close if public address and under max allocation epochs
587+
const tx1 = staking.connect(me.signer).closeAllocation(allocationID, poi)
588+
await expect(tx1).revertedWith('<epochs')
589+
585590
// Move max allocation epochs to close by delegator
586591
const maxAllocationEpochs = await staking.maxAllocationEpochs()
587-
for (let i = 0; i < maxAllocationEpochs + 1; i++) {
588-
await advanceToNextEpoch(epochManager)
589-
}
590-
591-
// Reject to close if the address is not delegator
592-
const tx1 = staking.connect(me.signer).closeAllocation(allocationID, poi)
593-
await expect(tx1).revertedWith('!auth')
592+
await advanceEpochs(epochManager, maxAllocationEpochs)
594593

595594
// Calculations
596595
const beforeAlloc = await staking.getAllocation(allocationID)
@@ -605,9 +604,8 @@ describe('Staking:Allocation', () => {
605604
// Setup
606605
await grt.connect(governor.signer).mint(me.address, toGRT('1'))
607606
await grt.connect(me.signer).approve(staking.address, toGRT('1'))
608-
await staking.connect(me.signer).delegate(indexer.address, toGRT('1'))
609607

610-
// Should close by delegator
608+
// Should close by public
611609
const tx = staking.connect(me.signer).closeAllocation(allocationID, poi)
612610
await expect(tx)
613611
.emit(staking, 'AllocationClosed')

0 commit comments

Comments
 (0)