Skip to content

Commit

Permalink
fix: format
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarranzav committed Feb 7, 2025
1 parent 812632f commit 5b4df75
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ export class AllocationManager {
private graphNode: GraphNode,
private network: Network,
) {
this.dipsManager = new DipsManager(this.logger, this.models, this.graphNode, this.network, this)
this.dipsManager = new DipsManager(
this.logger,
this.models,
this.graphNode,
this.network,
this,
)
}

async executeBatch(actions: Action[]): Promise<AllocationResult[]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import { DataTypes, Sequelize, Model, CreationOptional, InferAttributes, InferCreationAttributes } from 'sequelize'
import {
DataTypes,
Sequelize,
Model,
CreationOptional,
InferAttributes,
InferCreationAttributes,
} from 'sequelize'

// Indexing Fees AKA "DIPs"

export class IndexingAgreement extends Model<
InferAttributes<IndexingAgreement>,
InferCreationAttributes<IndexingAgreement>
> {
declare id: CreationOptional<string>;
declare signature: Buffer;
declare signed_payload: Buffer;
declare protocol_network: string;
declare chain_id: string;
declare base_price_per_epoch: string;
declare price_per_entity: string;
declare subgraph_deployment_id: string;
declare service: string;
declare payee: string;
declare payer: string;
declare deadline: Date;
declare duration_epochs: bigint;
declare max_initial_amount: string;
declare max_ongoing_amount_per_epoch: string;
declare min_epochs_per_collection: bigint;
declare max_epochs_per_collection: bigint;
declare created_at: Date;
declare updated_at: Date;
declare cancelled_at: Date | null;
declare signed_cancellation_payload: Buffer | null;
declare current_allocation_id: string | null;
declare last_allocation_id: string | null;
declare last_payment_collected_at: Date | null;
declare id: CreationOptional<string>
declare signature: Buffer
declare signed_payload: Buffer
declare protocol_network: string
declare chain_id: string
declare base_price_per_epoch: string
declare price_per_entity: string
declare subgraph_deployment_id: string
declare service: string
declare payee: string
declare payer: string
declare deadline: Date
declare duration_epochs: bigint
declare max_initial_amount: string
declare max_ongoing_amount_per_epoch: string
declare min_epochs_per_collection: bigint
declare max_epochs_per_collection: bigint
declare created_at: Date
declare updated_at: Date
declare cancelled_at: Date | null
declare signed_cancellation_payload: Buffer | null
declare current_allocation_id: string | null
declare last_allocation_id: string | null
declare last_payment_collected_at: Date | null
}

export interface IndexingFeesModels {
Expand Down
58 changes: 40 additions & 18 deletions packages/indexer-common/src/indexing-fees/dips.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { formatGRT, Logger, SubgraphDeploymentID } from "@graphprotocol/common-ts";
import { AllocationManager, GraphNode, IndexerManagementModels, IndexingDecisionBasis, IndexingRuleAttributes, Network, SubgraphIdentifierType, upsertIndexingRule } from '@graphprotocol/indexer-common'
import { Op } from "sequelize";
import { formatGRT, Logger, SubgraphDeploymentID } from '@graphprotocol/common-ts'
import {
AllocationManager,
GraphNode,
IndexerManagementModels,
IndexingDecisionBasis,
IndexingRuleAttributes,
Network,
SubgraphIdentifierType,
upsertIndexingRule,
} from '@graphprotocol/indexer-common'
import { Op } from 'sequelize'

export class DipsManager {
constructor(
Expand All @@ -20,12 +29,14 @@ export class DipsManager {
})
if (agreement) {
// TODO use dips-proto to cancel agreement via grpc

// Mark the agreement as cancelled
}
}
// Update the current and last allocation ids for an agreement if it exists
async tryUpdateAgreementAllocation(oldAllocationId: string, newAllocationId: string | null) {
async tryUpdateAgreementAllocation(
oldAllocationId: string,
newAllocationId: string | null,
) {
const agreement = await this.models.IndexingAgreement.findOne({
where: {
current_allocation_id: oldAllocationId,
Expand Down Expand Up @@ -65,17 +76,22 @@ export class DipsManager {
// (tap-agent will take care of aggregating it into a RAV)

// Mark the agreement as having had a payment collected
await this.models.IndexingAgreement.update({
last_payment_collected_at: new Date(),
}, {
where: {
last_allocation_id: lastAllocationId,
await this.models.IndexingAgreement.update(
{
last_payment_collected_at: new Date(),
},
})
{
where: {
last_allocation_id: lastAllocationId,
},
},
)
}
async ensureAgreementRules() {
if (!this.parent) {
this.logger.error('DipsManager has no parent AllocationManager, cannot ensure agreement rules')
this.logger.error(
'DipsManager has no parent AllocationManager, cannot ensure agreement rules',
)
return
}
// Get all the indexing agreements that are not cancelled
Expand All @@ -87,20 +103,26 @@ export class DipsManager {
// For each agreement, check that there is an indexing rule to always
// allocate to the agreement's subgraphDeploymentId, and if not, create one
for (const agreement of indexingAgreements) {
const subgraphDeploymentID = new SubgraphDeploymentID(agreement.subgraph_deployment_id)
const subgraphDeploymentID = new SubgraphDeploymentID(
agreement.subgraph_deployment_id,
)
// If there is not yet an indexingRule that deems this deployment worth allocating to, make one
if (!(await this.parent.matchingRuleExists(this.logger, subgraphDeploymentID))) {
this.logger.debug(
`Creating indexing rule for agreement ${agreement.id}`,
)
this.logger.debug(`Creating indexing rule for agreement ${agreement.id}`)
const indexingRule = {
identifier: agreement.subgraph_deployment_id,
allocationAmount: formatGRT(this.network.specification.indexerOptions.dipsAllocationAmount),
allocationAmount: formatGRT(
this.network.specification.indexerOptions.dipsAllocationAmount,
),
identifierType: SubgraphIdentifierType.DEPLOYMENT,
decisionBasis: IndexingDecisionBasis.ALWAYS,
protocolNetwork: this.network.specification.networkIdentifier,
autoRenewal: true,
allocationLifetime: Math.max(Number(agreement.min_epochs_per_collection), Number(agreement.max_epochs_per_collection) - this.network.specification.indexerOptions.dipsEpochsMargin),
allocationLifetime: Math.max(
Number(agreement.min_epochs_per_collection),
Number(agreement.max_epochs_per_collection) -
this.network.specification.indexerOptions.dipsEpochsMargin,
),
} as Partial<IndexingRuleAttributes>

await upsertIndexingRule(this.logger, this.models, indexingRule)
Expand Down

0 comments on commit 5b4df75

Please sign in to comment.