Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Cost models on Arbitrum #858

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"scripts": {
"bootstrap": "lerna bootstrap",
"prepare": "lerna run prepare",
"format": "lerna run format",
"release": "./scripts/release.sh",
"test": "lerna --concurrency 1 run test --stream --ignore @graphprotocol/indexer-service",
"test:ci": "lerna --concurrency 1 run test:ci --stream --ignore @graphprotocol/indexer-service",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Logger } from '@graphprotocol/common-ts'
import { caip2IdRegex } from '@graphprotocol/indexer-common'
import { DataTypes, QueryInterface } from 'sequelize'

interface MigrationContext {
queryInterface: QueryInterface
logger: Logger
}

interface Context {
context: MigrationContext
}

export async function up({ context }: Context): Promise<void> {
const { queryInterface, logger } = context

logger.debug(`Checking if 'CostModel' table exists`)
const tables = await queryInterface.showAllTables()
if (!tables.includes('CostModel')) {
logger.info(`Indexing rules table does not exist, migration not necessary`)
return
}

logger.debug(`Checking if 'CostModel' table needs to be migrated`)
const table = await queryInterface.describeTable('CostModel')
const protocolNetwork = table.protocolNetwork
if (protocolNetwork) {
logger.info(
`'protocolNetwork' column already exist, migration not necessary`,
)
return
}

logger.info(`Add 'protocolNetwork' column to 'CostModel' table`)
await queryInterface.addColumn('CostModel', 'protocolNetwork', {
type: DataTypes.STRING,
allowNull: false,
validate: {
is: caip2IdRegex,
},
})
}

export async function down({ context }: Context): Promise<void> {
const { queryInterface, logger } = context

return await queryInterface.sequelize.transaction({}, async transaction => {
const tables = await queryInterface.showAllTables()

if (tables.includes('CostModel')) {
logger.info(`Remove 'protocolNetwork' column`)
await context.queryInterface.removeColumn(
'CostModel',
'protocolNetwork',
{
transaction,
},
)
}
})
}
12 changes: 10 additions & 2 deletions packages/indexer-cli/src/commands/indexer/cost/set/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs'

import { loadValidatedConfig } from '../../../../config'
import { createIndexerManagementClient } from '../../../../client'
import { fixParameters } from '../../../../command-helpers'
import { extractProtocolNetworkOption, fixParameters } from '../../../../command-helpers'
import {
parseCostModel,
parseDeploymentID,
Expand All @@ -18,7 +18,7 @@ ${chalk.bold('graph indexer cost set model')} [options] <deployment-id> <file>
${chalk.bold('graph indexer cost set model')} [options] global <file>

${chalk.dim('Options:')}

-n, --network Protocol network
-h, --help Show usage information
-o, --output table|json|yaml Choose the output format: table (default), JSON, or YAML
`
Expand All @@ -31,6 +31,13 @@ module.exports = {
const { print, parameters } = toolbox

const { h, help, merged, o, output } = parameters.options

const protocolNetwork = extractProtocolNetworkOption(parameters.options, true)

if (!protocolNetwork) {
throw new Error('Protocol network is required')
}

const [deployment, filename] = fixParameters(parameters, { h, help, merged }) || []
const outputFormat = o || output || 'table'

Expand Down Expand Up @@ -69,6 +76,7 @@ module.exports = {
deployment,
model,
variables: null,
protocolNetwork,
})

try {
Expand Down
9 changes: 9 additions & 0 deletions packages/indexer-cli/src/cost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CostModelAttributes,
GraphQLCostModel,
IndexerManagementClient,
validateNetworkIdentifier,
} from '@graphprotocol/indexer-common'
import gql from 'graphql-tag'
import yaml from 'yaml'
Expand Down Expand Up @@ -30,6 +31,7 @@ const COST_MODEL_PARSERS: Record<keyof CostModelAttributes, (x: never) => any> =
deployment: parseDeploymentID,
model: x => x,
variables: nullPassThrough(JSON.parse),
protocolNetwork: x => validateNetworkIdentifier(x),
}

const COST_MODEL_FORMATTERS: Record<
Expand All @@ -40,6 +42,7 @@ const COST_MODEL_FORMATTERS: Record<
deployment: (d: SubgraphDeploymentIDIsh) => (typeof d === 'string' ? d : d.ipfsHash),
model: x => x,
variables: nullPassThrough(s => JSON.stringify(s, null, 2)),
protocolNetwork: x => validateNetworkIdentifier(x),
}

const COST_MODEL_CONVERTERS_FROM_GRAPHQL: Record<
Expand All @@ -51,6 +54,7 @@ const COST_MODEL_CONVERTERS_FROM_GRAPHQL: Record<
deployment: parseDeploymentID,
model: x => x,
variables: nullPassThrough(JSON.parse),
protocolNetwork: x => validateNetworkIdentifier(x),
}

const COST_MODEL_CONVERTERS_TO_GRAPHQL: Record<
Expand All @@ -62,6 +66,7 @@ const COST_MODEL_CONVERTERS_TO_GRAPHQL: Record<
deployment: (x: SubgraphDeploymentIDIsh) => x.toString(),
model: x => x,
variables: nullPassThrough(JSON.stringify),
protocolNetwork: x => validateNetworkIdentifier(x),
}

/**
Expand Down Expand Up @@ -199,6 +204,7 @@ export const costModels = async (
deployment
model
variables
protocolNetwork
}
}
`)
Expand All @@ -223,6 +229,7 @@ export const costModel = async (
deployment
model
variables
protocolNetwork
}
}
`,
Expand Down Expand Up @@ -253,6 +260,7 @@ export const setCostModel = async (
deployment
model
variables
protocolNetwork
}
}
`,
Expand All @@ -279,6 +287,7 @@ export const deleteCostModels = async (
deployment
model
variables
protocolNetwork
}
}
`,
Expand Down
2 changes: 2 additions & 0 deletions packages/indexer-common/src/indexer-management/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,14 @@ const SCHEMA_SDL = gql`
deployment: String!
model: String
variables: String
protocolNetwork: String!
}

input CostModelInput {
deployment: String!
model: String
variables: String
protocolNetwork: String!
}

type Query {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import { Optional, Model, DataTypes, Sequelize } from 'sequelize'
import { utils } from 'ethers'
import { caip2IdRegex } from '../../parsers/validators'

export interface GraphQLCostModel {
deployment: string
model: string | null | undefined
variables: string | null | undefined
protocolNetwork: string
}

export const parseGraphQLCostModel = (
Expand All @@ -21,6 +23,7 @@ export const parseGraphQLCostModel = (
deployment: costModel.deployment,
model: costModel.model || null,
variables: variables,
protocolNetwork: costModel.protocolNetwork,
}
} catch (error) {
throw new Error(`Failed to parse GraphQL cost model: ${error}`)
Expand All @@ -37,6 +40,7 @@ export interface CostModelAttributes {
deployment: string
model: string | null
variables: CostModelVariables | null
protocolNetwork: string
}

export interface CostModelCreationAttributes
Expand All @@ -50,6 +54,7 @@ export class CostModel
public deployment!: string
public model!: string | null
public variables!: CostModelVariables | null
public protocolNetwork!: string

public createdAt!: Date
public updatedAt!: Date
Expand Down Expand Up @@ -99,6 +104,13 @@ export const defineCostModelModels = (sequelize: Sequelize): CostModelModels =>
},
},
},
protocolNetwork: {
type: DataTypes.STRING,
allowNull: false,
validate: {
is: caip2IdRegex,
},
},
model: {
type: DataTypes.TEXT,
allowNull: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/indexer-service/src/server/cost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const createCostServer = async ({
deployment: String!
model: String
variables: String
protocolNetwork: String!
}

type Query {
Expand Down Expand Up @@ -126,6 +127,7 @@ export const createCostServer = async ({
deployment
model
variables
protocolNetwork
}
}
`,
Expand Down Expand Up @@ -172,6 +174,7 @@ export const createCostServer = async ({
deployment
model
variables
protocolNetwork
}
}
`,
Expand Down
Loading