Skip to content

Commit 2bde452

Browse files
committed
common: use protocolNetwork configured, update indexingRules properties in model
1 parent e9900f8 commit 2bde452

File tree

4 files changed

+42
-73
lines changed

4 files changed

+42
-73
lines changed

packages/indexer-common/src/indexer-management/models/indexing-rule.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,26 @@ export class IndexingRule
6666
extends Model<IndexingRuleAttributes, IndexingRuleCreationAttributes>
6767
implements IndexingRuleAttributes
6868
{
69-
public id!: number
70-
public identifier!: string
71-
public identifierType!: SubgraphIdentifierType
72-
public allocationAmount!: string | null
73-
public allocationLifetime!: number | null
74-
public autoRenewal!: boolean
75-
public parallelAllocations!: number | null
76-
public maxAllocationPercentage!: number | null
77-
public minSignal!: string | null
78-
public maxSignal!: string | null
79-
public minStake!: string | null
80-
public minAverageQueryFees!: string | null
81-
public custom!: string | null
82-
public decisionBasis!: IndexingDecisionBasis
83-
public requireSupported!: boolean
84-
public safety!: boolean
85-
public protocolNetwork!: string
69+
public declare id: number
70+
public declare identifier: string
71+
public declare identifierType: SubgraphIdentifierType
72+
public declare allocationAmount: string | null
73+
public declare allocationLifetime: number | null
74+
public declare autoRenewal: boolean
75+
public declare parallelAllocations: number | null
76+
public declare maxAllocationPercentage: number | null
77+
public declare minSignal: string | null
78+
public declare maxSignal: string | null
79+
public declare minStake: string | null
80+
public declare minAverageQueryFees: string | null
81+
public declare custom: string | null
82+
public declare decisionBasis: IndexingDecisionBasis
83+
public declare requireSupported: boolean
84+
public declare safety: boolean
85+
public declare protocolNetwork: string
8686

87-
public createdAt!: Date
88-
public updatedAt!: Date
87+
public declare createdAt: Date
88+
public declare updatedAt: Date
8989

9090
// eslint-disable-next-line @typescript-eslint/ban-types
9191
public toGraphQL(): object {

packages/indexer-common/src/indexer-management/resolvers/actions.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
IndexerManagementModels,
1414
OrderDirection,
1515
validateActionInputs,
16-
validateNetworkIdentifier,
1716
} from '@graphprotocol/indexer-common'
1817
import { literal, Op, Transaction } from 'sequelize'
1918
import { ActionManager } from '../actions'
@@ -48,9 +47,7 @@ async function executeQueueOperation(
4847

4948
// Check for duplicated actions
5049
const duplicateActions = actionsAwaitingExecution.filter(
51-
(a) =>
52-
a.deploymentID === action.deploymentID &&
53-
a.protocolNetwork === action.protocolNetwork,
50+
(a) => a.deploymentID === action.deploymentID,
5451
)
5552
if (duplicateActions.length === 0) {
5653
logger.trace('Inserting Action in database', { action })
@@ -158,13 +155,8 @@ export default {
158155
throw Error('IndexerManagementClient must be in `network` mode to modify actions')
159156
}
160157

161-
// Sanitize protocol network identifier
162158
actions.forEach((action) => {
163-
try {
164-
action.protocolNetwork = validateNetworkIdentifier(action.protocolNetwork)
165-
} catch (e) {
166-
throw Error(`Invalid value for the field 'protocolNetwork'. ${e}`)
167-
}
159+
action.protocolNetwork = network.networkMonitor.networkCAIPID
168160
})
169161

170162
// Let Network Monitors validate actions based on their protocol networks

packages/indexer-common/src/indexer-management/resolvers/indexing-rules.ts

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { IndexerManagementDefaults, IndexerManagementResolverContext } from '../
1010
import { Transaction } from 'sequelize/types'
1111
import { fetchIndexingRules } from '../rules'
1212
import { processIdentifier } from '../../'
13-
import { validateNetworkIdentifier } from '../../parsers'
1413
import groupBy from 'lodash.groupby'
1514

1615
const resetGlobalRule = async (
@@ -35,17 +34,14 @@ export default {
3534
identifier: indexingRuleIdentifier,
3635
merged,
3736
}: { identifier: IndexingRuleIdentifier; merged: boolean },
38-
{ models }: IndexerManagementResolverContext,
37+
{ models, network }: IndexerManagementResolverContext,
3938
): Promise<object | null> => {
4039
const [identifier] = await processIdentifier(indexingRuleIdentifier.identifier, {
4140
all: false,
4241
global: true,
4342
})
4443

45-
// Sanitize protocol network identifier
46-
const protocolNetwork = validateNetworkIdentifier(
47-
indexingRuleIdentifier.protocolNetwork,
48-
)
44+
const protocolNetwork = network.networkMonitor.networkCAIPID
4945

5046
const rule = await models.IndexingRule.findOne({
5147
where: { identifier, protocolNetwork },
@@ -62,36 +58,22 @@ export default {
6258
},
6359

6460
indexingRules: async (
65-
{
66-
merged,
67-
protocolNetwork: uncheckedProtocolNetwork,
68-
}: { merged: boolean; protocolNetwork: string | undefined },
69-
{ models }: IndexerManagementResolverContext,
61+
{ merged }: { merged: boolean; protocolNetwork: string | undefined },
62+
{ models, network }: IndexerManagementResolverContext,
7063
): Promise<object[]> => {
71-
// Convert the input `protocolNetwork` value to a CAIP2-ID
72-
const protocolNetwork = uncheckedProtocolNetwork
73-
? validateNetworkIdentifier(uncheckedProtocolNetwork)
74-
: undefined
64+
const protocolNetwork = network.networkMonitor.networkCAIPID
7565
return await fetchIndexingRules(models, merged, protocolNetwork)
7666
},
7767

7868
setIndexingRule: async (
7969
{ rule }: { rule: IndexingRuleCreationAttributes },
80-
{ models }: IndexerManagementResolverContext,
70+
{ models, network }: IndexerManagementResolverContext,
8171
): Promise<object> => {
8272
if (!rule.identifier) {
8373
throw Error('Cannot set indexingRule without identifier')
8474
}
8575

86-
if (!rule.protocolNetwork) {
87-
throw Error("Cannot set an indexing rule without the field 'protocolNetwork'")
88-
} else {
89-
try {
90-
rule.protocolNetwork = validateNetworkIdentifier(rule.protocolNetwork)
91-
} catch (e) {
92-
throw Error(`Invalid value for the field 'protocolNetwork'. ${e}`)
93-
}
94-
}
76+
rule.protocolNetwork = network.networkMonitor.networkCAIPID
9577

9678
const [identifier] = await processIdentifier(rule.identifier, {
9779
all: false,
@@ -106,17 +88,14 @@ export default {
10688

10789
deleteIndexingRule: async (
10890
{ identifier: indexingRuleIdentifier }: { identifier: IndexingRuleIdentifier },
109-
{ models, defaults }: IndexerManagementResolverContext,
91+
{ models, defaults, network }: IndexerManagementResolverContext,
11092
): Promise<boolean> => {
11193
const [identifier] = await processIdentifier(indexingRuleIdentifier.identifier, {
11294
all: false,
11395
global: true,
11496
})
11597

116-
// Sanitize protocol network identifier
117-
const protocolNetwork = validateNetworkIdentifier(
118-
indexingRuleIdentifier.protocolNetwork,
119-
)
98+
const protocolNetwork = network.networkMonitor.networkCAIPID
12099

121100
const validatedRuleIdentifier = {
122101
protocolNetwork,
@@ -146,13 +125,13 @@ export default {
146125

147126
deleteIndexingRules: async (
148127
{ identifiers: indexingRuleIdentifiers }: { identifiers: IndexingRuleIdentifier[] },
149-
{ models, defaults }: IndexerManagementResolverContext,
128+
{ models, defaults, network }: IndexerManagementResolverContext,
150129
): Promise<boolean> => {
151130
let totalNumDeleted = 0
152131

153132
// Sanitize protocol network identifiers
154133
for (const identifier of indexingRuleIdentifiers) {
155-
identifier.protocolNetwork = validateNetworkIdentifier(identifier.protocolNetwork)
134+
identifier.protocolNetwork = network.networkMonitor.networkCAIPID
156135
}
157136

158137
// Batch deletions by the `IndexingRuleIdentifier.protocolNetwork` attribute .

packages/indexer-common/src/indexer-management/rules.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import {
66
IndexingRuleAttributes,
77
} from '@graphprotocol/indexer-common'
88
import { parseIndexingRule } from '../rules'
9-
import groupBy from 'lodash.groupby'
109

1110
export const fetchIndexingRules = async (
1211
models: IndexerManagementModels,
1312
merged: boolean,
1413
protocolNetwork?: string,
1514
): Promise<IndexingRuleAttributes[]> => {
1615
// If unspecified, select indexing rules from all protocol networks
17-
const whereClause = protocolNetwork ? { protocolNetwork } : {}
16+
const logger = new Logger({ name: 'indexer-common' })
17+
18+
logger.info(`Fetching indexing rules for network '${protocolNetwork}'`)
19+
const whereClause = {} // protocolNetwork ?{ protocolNetwork }: {}
1820
const rules = await models.IndexingRule.findAll({
1921
where: whereClause,
2022
order: [
@@ -23,16 +25,12 @@ export const fetchIndexingRules = async (
2325
],
2426
})
2527
if (merged) {
26-
// Merge rules by protocol network
27-
return Object.entries(groupBy(rules, (rule) => rule.protocolNetwork))
28-
.map(([protocolNetwork, rules]) => {
29-
const global = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL)
30-
if (!global) {
31-
throw Error(`Could not find global rule for network '${protocolNetwork}'`)
32-
}
33-
return rules.map((rule) => rule.mergeGlobal(global))
34-
})
35-
.flat()
28+
// Merge global rule into all rules
29+
const global = rules.find((rule) => rule.identifier === INDEXING_RULE_GLOBAL)
30+
if (!global) {
31+
throw Error(`Could not find global rule for network '${protocolNetwork}'`)
32+
}
33+
return rules.map((rule) => rule.mergeGlobal(global))
3634
} else {
3735
return rules
3836
}

0 commit comments

Comments
 (0)