# ServiceAgreementRegistry

# Overview

This contract tracks all service Agreements for Indexers and Consumers. For now, Consumer can accept the plan created by Indexer from Plan Manager to generate close service agreement. Indexer can also accept Purchase Offer created by Consumer from purchase offer market to generate close service agreement. All generated service agreement need to register in this contract by calling establishServiceAgreement(). After this all SQT Toaken from agreements will be temporary hold in this contract, and approve reward distributor contract to take and distribute these Token.

# STATES

# settings

ISettings contract which stores SubQuery network contracts address

contract ISettings settings

# nextServiceAgreementId

the id for next ServiceAgreement

uint256 nextServiceAgreementId

# closedServiceAgreements

ServiceAgreementId => AgreementInfo

mapping(uint256 => struct ClosedServiceAgreementInfo) closedServiceAgreements

# establisherWhitelist

address can establishServiceAgreement, for now only PurchaceOfferMarket and PlanManager addresses

mapping(address => bool) establisherWhitelist

Emitted when closed service agreement established

# ClosedAgreementCreated

event ClosedAgreementCreated(address consumer, address indexer, bytes32 deploymentId, uint256 serviceAgreementId)

Initialize this contract. Load establisherWhitelist.

# initialize

function initialize(contract ISettings _settings, address[] _whitelist) external

See {IERC165-supportsInterface}.

# supportsInterface

function supportsInterface(bytes4 interfaceId) public view returns (bool)

# setSettings

function setSettings(contract ISettings _settings) external

# addEstablisher

function addEstablisher(address establisher) external

# removeEstablisher

function removeEstablisher(address establisher) external

# _afterTokenTransfer

function _afterTokenTransfer(address from, address to, uint256 tokenId, uint256 batchSize) internal

# createClosedServiceAgreement

function createClosedServiceAgreement(struct ClosedServiceAgreementInfo agreement, bool checkThreshold) external returns (uint256)

Establish the generated service agreement. For now only establish the close service agreement generated from PlanManager and PurchsaseOfferMarket. This function is called by PlanManager or PurchsaseOfferMarket when close service agreement generated, it temporary hold the SQT Token from these agreements, approve and nodify reward distributor contract to take and distribute these Token. All agreements register to this contract through this method. SQT need to be transfered before calling this function. When new agreement come we need to track the sumDailyReward of Indexer. In our design there is an upper limit on the rewards indexer can earn every day, and the limit will increase with the increase of the total staked amount of that indexer. This design can ensure our Customer to obtain high quality of service from Indexer, at the same time, it also encourages Indexer to provide better more stable services.

# _establishServiceAgreement

function _establishServiceAgreement(uint256 agreementId, bool checkThreshold) internal

A function allow Consumer call to renew its unexpired closed service agreement. We only allow the the agreement generated from PlanManager renewable which is created by Indexer and accepted by Consumer. We use the status planId in agreement to determine whether the agreement is renewable, since only the agreement generated from PlanManager come with the PlanId. Indexer can be prevente the agreement rennew by inactive the plan which bound to it. Consumer must renew befor the agreement expired.

# renewAgreement

function renewAgreement(uint256 agreementId) external

# closedServiceAgreementExpired

function closedServiceAgreementExpired(uint256 agreementId) public view returns (bool)

# getClosedServiceAgreement

function getClosedServiceAgreement(uint256 agreementId) external view returns (struct ClosedServiceAgreementInfo)