Skip to content

Commit 26c6c44

Browse files
committed
refactor: extract IndexingAgreement types to IIndexingAgreement interface
1 parent 3c1a2f1 commit 26c6c44

5 files changed

Lines changed: 62 additions & 86 deletions

File tree

packages/subgraph-service/contracts/DisputeManager.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { IDisputeManager } from "@graphprotocol/interfaces/contracts/subgraph-se
1111
import { ISubgraphService } from "@graphprotocol/interfaces/contracts/subgraph-service/ISubgraphService.sol";
1212
import { IAttestation } from "@graphprotocol/interfaces/contracts/subgraph-service/internal/IAttestation.sol";
1313
import { IAllocation } from "@graphprotocol/interfaces/contracts/subgraph-service/internal/IAllocation.sol";
14+
import { IIndexingAgreement } from "@graphprotocol/interfaces/contracts/subgraph-service/internal/IIndexingAgreement.sol";
1415

1516
import { TokenUtils } from "@graphprotocol/contracts/contracts/utils/TokenUtils.sol";
1617
import { PPMMath } from "@graphprotocol/horizon/contracts/libraries/PPMMath.sol";
1718
import { MathUtils } from "@graphprotocol/horizon/contracts/libraries/MathUtils.sol";
1819
import { Attestation } from "./libraries/Attestation.sol";
19-
import { IndexingAgreement } from "./libraries/IndexingAgreement.sol";
2020

2121
import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
2222
import { Initializable } from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
@@ -540,15 +540,15 @@ contract DisputeManager is
540540
uint256 _entities,
541541
uint256 _blockNumber
542542
) private returns (bytes32) {
543-
IndexingAgreement.AgreementWrapper memory wrapper = _getSubgraphService().getIndexingAgreement(_agreementId);
543+
IIndexingAgreement.AgreementWrapper memory wrapper = _getSubgraphService().getIndexingAgreement(_agreementId);
544544

545545
// Agreement must have been collected on and be a version 1
546546
require(
547547
wrapper.collectorAgreement.lastCollectionAt > 0,
548548
DisputeManagerIndexingAgreementNotDisputable(_agreementId)
549549
);
550550
require(
551-
wrapper.agreement.version == IndexingAgreement.IndexingAgreementVersion.V1,
551+
wrapper.agreement.version == IIndexingAgreement.IndexingAgreementVersion.V1,
552552
DisputeManagerIndexingAgreementInvalidVersion(wrapper.agreement.version)
553553
);
554554

packages/subgraph-service/contracts/SubgraphService.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { IRewardsIssuer } from "@graphprotocol/interfaces/contracts/contracts/re
88
import { IDataService } from "@graphprotocol/interfaces/contracts/data-service/IDataService.sol";
99
import { ISubgraphService } from "@graphprotocol/interfaces/contracts/subgraph-service/ISubgraphService.sol";
1010
import { IAllocation } from "@graphprotocol/interfaces/contracts/subgraph-service/internal/IAllocation.sol";
11+
import { IIndexingAgreement } from "@graphprotocol/interfaces/contracts/subgraph-service/internal/IIndexingAgreement.sol";
1112
import { ILegacyAllocation } from "@graphprotocol/interfaces/contracts/subgraph-service/internal/ILegacyAllocation.sol";
1213
import { IRecurringCollector } from "@graphprotocol/interfaces/contracts/horizon/IRecurringCollector.sol";
1314

@@ -518,7 +519,7 @@ contract SubgraphService is
518519
/// @inheritdoc ISubgraphService
519520
function getIndexingAgreement(
520521
bytes16 agreementId
521-
) external view returns (IndexingAgreement.AgreementWrapper memory) {
522+
) external view returns (IIndexingAgreement.AgreementWrapper memory) {
522523
return IndexingAgreement._getStorageManager().get(agreementId);
523524
}
524525

packages/subgraph-service/contracts/libraries/AllocationHandler.sol

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ library AllocationHandler {
267267

268268
// Update total allocated tokens for the subgraph deployment
269269
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] =
270-
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] +
271-
allocation.tokens;
270+
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] + allocation.tokens;
272271

273272
emit AllocationHandler.AllocationCreated(
274273
params._indexer,
@@ -566,10 +565,7 @@ library AllocationHandler {
566565
IAllocation.State memory allocation = _allocations.get(_allocationId);
567566

568567
// Reclaim uncollected rewards before closing
569-
uint256 reclaimedRewards = graphRewardsManager.reclaimRewards(
570-
RewardsCondition.CLOSE_ALLOCATION,
571-
_allocationId
572-
);
568+
uint256 reclaimedRewards = graphRewardsManager.reclaimRewards(RewardsCondition.CLOSE_ALLOCATION, _allocationId);
573569

574570
// Take rewards snapshot to prevent other allos from counting tokens from this allo
575571
_allocations.snapshotRewards(
@@ -588,8 +584,7 @@ library AllocationHandler {
588584

589585
// Update total allocated tokens for the subgraph deployment
590586
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] =
591-
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] -
592-
allocation.tokens;
587+
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] - allocation.tokens;
593588

594589
emit AllocationHandler.AllocationClosed(
595590
allocation.indexer,
@@ -628,23 +623,15 @@ library AllocationHandler {
628623
tokensDelegationRewards = pool.shares > 0 ? _rewardsCollected.mulPPM(delegatorCut) : 0;
629624
if (tokensDelegationRewards > 0) {
630625
_params.graphToken.approve(address(_params.graphStaking), tokensDelegationRewards);
631-
_params.graphStaking.addToDelegationPool(
632-
_allocation.indexer,
633-
_params.dataService,
634-
tokensDelegationRewards
635-
);
626+
_params.graphStaking.addToDelegationPool(_allocation.indexer, _params.dataService, tokensDelegationRewards);
636627
}
637628

638629
// Distribute indexer share
639630
tokensIndexerRewards = _rewardsCollected - tokensDelegationRewards;
640631
if (tokensIndexerRewards > 0) {
641632
if (_params._paymentsDestination == address(0)) {
642633
_params.graphToken.approve(address(_params.graphStaking), tokensIndexerRewards);
643-
_params.graphStaking.stakeToProvision(
644-
_allocation.indexer,
645-
_params.dataService,
646-
tokensIndexerRewards
647-
);
634+
_params.graphStaking.stakeToProvision(_allocation.indexer, _params.dataService, tokensIndexerRewards);
648635
} else {
649636
_params.graphToken.pushTokens(_params._paymentsDestination, tokensIndexerRewards);
650637
}

packages/subgraph-service/contracts/libraries/IndexingAgreement.sol

Lines changed: 52 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,21 @@
11
// SPDX-License-Identifier: GPL-3.0-or-later
22
pragma solidity 0.8.33;
33

4-
import { IGraphPayments } from "@graphprotocol/horizon/contracts/interfaces/IGraphPayments.sol";
5-
import { IRecurringCollector } from "@graphprotocol/horizon/contracts/interfaces/IRecurringCollector.sol";
4+
import { IGraphPayments } from "@graphprotocol/interfaces/contracts/horizon/IGraphPayments.sol";
5+
import { IRecurringCollector } from "@graphprotocol/interfaces/contracts/horizon/IRecurringCollector.sol";
6+
import { ISubgraphService } from "@graphprotocol/interfaces/contracts/subgraph-service/ISubgraphService.sol";
7+
import { IIndexingAgreement } from "@graphprotocol/interfaces/contracts/subgraph-service/internal/IIndexingAgreement.sol";
8+
import { IAllocation } from "@graphprotocol/interfaces/contracts/subgraph-service/internal/IAllocation.sol";
69

7-
import { ISubgraphService } from "../interfaces/ISubgraphService.sol";
810
import { AllocationHandler } from "../libraries/AllocationHandler.sol";
911
import { Directory } from "../utilities/Directory.sol";
1012
import { Allocation } from "./Allocation.sol";
1113
import { IndexingAgreementDecoder } from "./IndexingAgreementDecoder.sol";
1214

1315
library IndexingAgreement {
1416
using IndexingAgreement for StorageManager;
15-
using Allocation for Allocation.State;
16-
using Allocation for mapping(address => Allocation.State);
17-
18-
/// @notice Versions of Indexing Agreement Metadata
19-
enum IndexingAgreementVersion {
20-
V1
21-
}
22-
23-
/**
24-
* @notice Indexer Agreement Data
25-
* @param allocationId The allocation ID
26-
* @param version The indexing agreement version
27-
*/
28-
struct State {
29-
address allocationId;
30-
IndexingAgreementVersion version;
31-
}
32-
33-
/**
34-
* @notice Wrapper for Indexing Agreement and Collector Agreement Data
35-
* @dev This struct is used to encapsulate the state of an indexing agreement
36-
* @param agreement The indexing agreement state
37-
* @param collectorAgreement The collector agreement data
38-
*/
39-
struct AgreementWrapper {
40-
State agreement;
41-
IRecurringCollector.AgreementData collectorAgreement;
42-
}
17+
using Allocation for IAllocation.State;
18+
using Allocation for mapping(address => IAllocation.State);
4319

4420
/**
4521
* @notice Accept Indexing Agreement metadata
@@ -49,7 +25,7 @@ library IndexingAgreement {
4925
*/
5026
struct AcceptIndexingAgreementMetadata {
5127
bytes32 subgraphDeploymentId;
52-
IndexingAgreementVersion version;
28+
IIndexingAgreement.IndexingAgreementVersion version;
5329
bytes terms;
5430
}
5531

@@ -59,7 +35,7 @@ library IndexingAgreement {
5935
* @param terms The indexing agreement terms
6036
*/
6137
struct UpdateIndexingAgreementMetadata {
62-
IndexingAgreementVersion version;
38+
IIndexingAgreement.IndexingAgreementVersion version;
6339
bytes terms;
6440
}
6541

@@ -118,7 +94,7 @@ library IndexingAgreement {
11894
* @custom:storage-location erc7201:graphprotocol.subgraph-service.storage.StorageManager.IndexingAgreement
11995
*/
12096
struct StorageManager {
121-
mapping(bytes16 => State) agreements;
97+
mapping(bytes16 => IIndexingAgreement.State) agreements;
12298
mapping(bytes16 agreementId => IndexingAgreementTermsV1 data) termsV1;
12399
mapping(address allocationId => bytes16 agreementId) allocationToActiveAgreementId;
124100
}
@@ -188,7 +164,7 @@ library IndexingAgreement {
188164
bytes16 indexed agreementId,
189165
address allocationId,
190166
bytes32 subgraphDeploymentId,
191-
IndexingAgreementVersion version,
167+
IIndexingAgreement.IndexingAgreementVersion version,
192168
bytes versionTerms
193169
);
194170

@@ -206,15 +182,15 @@ library IndexingAgreement {
206182
address indexed payer,
207183
bytes16 indexed agreementId,
208184
address allocationId,
209-
IndexingAgreementVersion version,
185+
IIndexingAgreement.IndexingAgreementVersion version,
210186
bytes versionTerms
211187
);
212188

213189
/**
214190
* @notice Thrown when trying to interact with an agreement with an invalid version
215191
* @param version The invalid version
216192
*/
217-
error IndexingAgreementInvalidVersion(IndexingAgreementVersion version);
193+
error IndexingAgreementInvalidVersion(IIndexingAgreement.IndexingAgreementVersion version);
218194

219195
/**
220196
* @notice Thrown when an agreement is not for the subgraph data service
@@ -301,11 +277,11 @@ library IndexingAgreement {
301277
*/
302278
function accept(
303279
StorageManager storage self,
304-
mapping(address allocationId => Allocation.State allocation) storage allocations,
280+
mapping(address allocationId => IAllocation.State allocation) storage allocations,
305281
address allocationId,
306282
IRecurringCollector.SignedRCA calldata signedRCA
307283
) external returns (bytes16) {
308-
Allocation.State memory allocation = _requireValidAllocation(
284+
IAllocation.State memory allocation = _requireValidAllocation(
309285
allocations,
310286
allocationId,
311287
signedRCA.rca.serviceProvider
@@ -328,7 +304,7 @@ library IndexingAgreement {
328304
signedRCA.rca.nonce
329305
);
330306

331-
State storage agreement = self.agreements[agreementId];
307+
IIndexingAgreement.State storage agreement = self.agreements[agreementId];
332308

333309
require(agreement.allocationId == address(0), IndexingAgreementAlreadyAccepted(agreementId));
334310

@@ -351,7 +327,10 @@ library IndexingAgreement {
351327
agreement.version = metadata.version;
352328
agreement.allocationId = allocationId;
353329

354-
require(metadata.version == IndexingAgreementVersion.V1, IndexingAgreementInvalidVersion(metadata.version));
330+
require(
331+
metadata.version == IIndexingAgreement.IndexingAgreementVersion.V1,
332+
IndexingAgreementInvalidVersion(metadata.version)
333+
);
355334
_setTermsV1(self, agreementId, metadata.terms, signedRCA.rca.maxOngoingTokensPerSecond);
356335

357336
emit IndexingAgreementAccepted(
@@ -388,7 +367,7 @@ library IndexingAgreement {
388367
address indexer,
389368
IRecurringCollector.SignedRCAU calldata signedRCAU
390369
) external {
391-
AgreementWrapper memory wrapper = _get(self, signedRCAU.rcau.agreementId);
370+
IIndexingAgreement.AgreementWrapper memory wrapper = _get(self, signedRCAU.rcau.agreementId);
392371
require(_isActive(wrapper), IndexingAgreementNotActive(signedRCAU.rcau.agreementId));
393372
require(
394373
wrapper.collectorAgreement.serviceProvider == indexer,
@@ -399,8 +378,14 @@ library IndexingAgreement {
399378
signedRCAU.rcau.metadata
400379
);
401380

402-
require(wrapper.agreement.version == IndexingAgreementVersion.V1, "internal: invalid version");
403-
require(metadata.version == IndexingAgreementVersion.V1, IndexingAgreementInvalidVersion(metadata.version));
381+
require(
382+
wrapper.agreement.version == IIndexingAgreement.IndexingAgreementVersion.V1,
383+
"internal: invalid version"
384+
);
385+
require(
386+
metadata.version == IIndexingAgreement.IndexingAgreementVersion.V1,
387+
IndexingAgreementInvalidVersion(metadata.version)
388+
);
404389
_setTermsV1(
405390
self,
406391
signedRCAU.rcau.agreementId,
@@ -436,7 +421,7 @@ library IndexingAgreement {
436421
* @param agreementId The id of the agreement to cancel
437422
*/
438423
function cancel(StorageManager storage self, address indexer, bytes16 agreementId) external {
439-
AgreementWrapper memory wrapper = _get(self, agreementId);
424+
IIndexingAgreement.AgreementWrapper memory wrapper = _get(self, agreementId);
440425
require(_isActive(wrapper), IndexingAgreementNotActive(agreementId));
441426
require(
442427
wrapper.collectorAgreement.serviceProvider == indexer,
@@ -473,7 +458,7 @@ library IndexingAgreement {
473458
return;
474459
}
475460

476-
AgreementWrapper memory wrapper = _get(self, agreementId);
461+
IIndexingAgreement.AgreementWrapper memory wrapper = _get(self, agreementId);
477462
if (!_isActive(wrapper)) {
478463
return;
479464
}
@@ -504,7 +489,7 @@ library IndexingAgreement {
504489
* @param agreementId The id of the agreement to cancel
505490
*/
506491
function cancelByPayer(StorageManager storage self, bytes16 agreementId) external {
507-
AgreementWrapper memory wrapper = _get(self, agreementId);
492+
IIndexingAgreement.AgreementWrapper memory wrapper = _get(self, agreementId);
508493
require(_isActive(wrapper), IndexingAgreementNotActive(agreementId));
509494
require(
510495
_directory().recurringCollector().isAuthorized(wrapper.collectorAgreement.payer, msg.sender),
@@ -540,11 +525,11 @@ library IndexingAgreement {
540525
*/
541526
function collect(
542527
StorageManager storage self,
543-
mapping(address allocationId => Allocation.State allocation) storage allocations,
528+
mapping(address allocationId => IAllocation.State allocation) storage allocations,
544529
CollectParams memory params
545530
) external returns (address, uint256) {
546-
AgreementWrapper memory wrapper = _get(self, params.agreementId);
547-
Allocation.State memory allocation = _requireValidAllocation(
531+
IIndexingAgreement.AgreementWrapper memory wrapper = _get(self, params.agreementId);
532+
IAllocation.State memory allocation = _requireValidAllocation(
548533
allocations,
549534
wrapper.agreement.allocationId,
550535
wrapper.collectorAgreement.serviceProvider
@@ -560,7 +545,7 @@ library IndexingAgreement {
560545
require(_isValid(wrapper) && isCollectable, IndexingAgreementNotCollectable(params.agreementId));
561546

562547
require(
563-
wrapper.agreement.version == IndexingAgreementVersion.V1,
548+
wrapper.agreement.version == IIndexingAgreement.IndexingAgreementVersion.V1,
564549
IndexingAgreementInvalidVersion(wrapper.agreement.version)
565550
);
566551

@@ -610,8 +595,11 @@ library IndexingAgreement {
610595
* @param agreementId The id of the indexing agreement
611596
* @return The indexing agreement wrapper containing the agreement state and collector agreement data
612597
*/
613-
function get(StorageManager storage self, bytes16 agreementId) external view returns (AgreementWrapper memory) {
614-
AgreementWrapper memory wrapper = _get(self, agreementId);
598+
function get(
599+
StorageManager storage self,
600+
bytes16 agreementId
601+
) external view returns (IIndexingAgreement.AgreementWrapper memory) {
602+
IIndexingAgreement.AgreementWrapper memory wrapper = _get(self, agreementId);
615603
require(wrapper.collectorAgreement.dataService == address(this), IndexingAgreementNotActive(agreementId));
616604

617605
return wrapper;
@@ -665,7 +653,7 @@ library IndexingAgreement {
665653
function _cancel(
666654
StorageManager storage _manager,
667655
bytes16 _agreementId,
668-
State memory _agreement,
656+
IIndexingAgreement.State memory _agreement,
669657
IRecurringCollector.AgreementData memory _collectorAgreement,
670658
IRecurringCollector.CancelAgreementBy _cancelBy
671659
) private {
@@ -698,11 +686,11 @@ library IndexingAgreement {
698686
* @return The allocation state
699687
*/
700688
function _requireValidAllocation(
701-
mapping(address => Allocation.State) storage _allocations,
689+
mapping(address => IAllocation.State) storage _allocations,
702690
address _allocationId,
703691
address _indexer
704-
) private view returns (Allocation.State memory) {
705-
Allocation.State memory allocation = _allocations.get(_allocationId);
692+
) private view returns (IAllocation.State memory) {
693+
IAllocation.State memory allocation = _allocations.get(_allocationId);
706694
require(
707695
allocation.indexer == _indexer,
708696
ISubgraphService.SubgraphServiceAllocationNotAuthorized(_indexer, _allocationId)
@@ -738,7 +726,7 @@ library IndexingAgreement {
738726
* @param wrapper The agreement wrapper containing the indexing agreement and collector agreement data
739727
* @return True if the agreement is active, false otherwise
740728
**/
741-
function _isActive(AgreementWrapper memory wrapper) private view returns (bool) {
729+
function _isActive(IIndexingAgreement.AgreementWrapper memory wrapper) private view returns (bool) {
742730
return _isValid(wrapper) && wrapper.collectorAgreement.state == IRecurringCollector.AgreementState.Accepted;
743731
}
744732

@@ -759,7 +747,7 @@ library IndexingAgreement {
759747
* @param wrapper The agreement wrapper containing the indexing agreement and collector agreement data
760748
* @return True if the agreement is valid, false otherwise
761749
**/
762-
function _isValid(AgreementWrapper memory wrapper) private view returns (bool) {
750+
function _isValid(IIndexingAgreement.AgreementWrapper memory wrapper) private view returns (bool) {
763751
return wrapper.collectorAgreement.dataService == address(this) && wrapper.agreement.allocationId != address(0);
764752
}
765753

@@ -778,9 +766,12 @@ library IndexingAgreement {
778766
* @param agreementId The id of the indexing agreement
779767
* @return The indexing agreement wrapper containing the agreement state and collector agreement data
780768
*/
781-
function _get(StorageManager storage self, bytes16 agreementId) private view returns (AgreementWrapper memory) {
769+
function _get(
770+
StorageManager storage self,
771+
bytes16 agreementId
772+
) private view returns (IIndexingAgreement.AgreementWrapper memory) {
782773
return
783-
AgreementWrapper({
774+
IIndexingAgreement.AgreementWrapper({
784775
agreement: self.agreements[agreementId],
785776
collectorAgreement: _directory().recurringCollector().getAgreement(agreementId)
786777
});

0 commit comments

Comments
 (0)