Skip to content

Commit

Permalink
mostly there
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj committed Apr 1, 2024
1 parent cffb378 commit 94926f7
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 70 deletions.
69 changes: 36 additions & 33 deletions packages/indexer-cli/src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import {
ActionFilter,
ActionInput,
ActionParams,
ActionResult,
ActionStatus,
ActionType,
ActionUpdateInput,
GeneratedGraphQLTypes,
IndexerManagementClient,
nullPassThrough,
OrderDirection,
parseBoolean,
validateNetworkIdentifier,
} from '@graphprotocol/indexer-common'
import { validatePOI, validateRequiredParams } from './command-helpers'
import gql from 'graphql-tag'
Expand All @@ -27,17 +23,17 @@ export interface GenericActionInputParams {

// Make separate functions for each action type parsing from generic?
export async function buildActionInput(
type: ActionType,
type: GeneratedGraphQLTypes.ActionType,
actionParams: GenericActionInputParams,
source: string,
reason: string,
status: ActionStatus,
status: GeneratedGraphQLTypes.ActionStatus,
priority: number,
protocolNetwork: string,
): Promise<ActionInput> {
): Promise<GeneratedGraphQLTypes.ActionInput> {
await validateActionInput(type, actionParams)
switch (type) {
case ActionType.ALLOCATE:
case 'allocate':
return {
deploymentID: actionParams.targetDeployment,
amount: actionParams.param1?.toString(),
Expand All @@ -48,7 +44,7 @@ export async function buildActionInput(
priority,
protocolNetwork,
}
case ActionType.UNALLOCATE: {
case 'unallocate': {
let poi = actionParams.param2
if (poi == '0' || poi == '0x0') {
poi = utils.hexlify(Array(32).fill(0))
Expand All @@ -66,7 +62,7 @@ export async function buildActionInput(
protocolNetwork,
}
}
case ActionType.REALLOCATE: {
case 'reallocate': {
let poi = actionParams.param3
if (poi == '0' || poi == '0x0') {
poi = utils.hexlify(Array(32).fill(0))
Expand All @@ -89,18 +85,18 @@ export async function buildActionInput(
}

export async function validateActionInput(
type: ActionType,
type: GeneratedGraphQLTypes.ActionType,
actionParams: GenericActionInputParams,
): Promise<void> {
let requiredFields: string[] = []
switch (type) {
case ActionType.ALLOCATE:
case 'allocate':
requiredFields = requiredFields.concat(['targetDeployment', 'param1'])
break
case ActionType.UNALLOCATE:
case 'unallocate':
requiredFields = requiredFields.concat(['targetDeployment', 'param1'])
break
case ActionType.REALLOCATE:
case 'reallocate':
requiredFields = requiredFields.concat(['targetDeployment', 'param1', 'param2'])
}

Expand All @@ -110,30 +106,34 @@ export async function validateActionInput(
)
}

export function validateActionType(input: string): ActionType {
const validVariants = Object.keys(ActionType).map(variant =>
variant.toLocaleLowerCase(),
export function validateActionType(input: string): GeneratedGraphQLTypes.ActionType {
const validVariants = Object.keys(GeneratedGraphQLTypes.ActionType).map(variant =>
variant.toLowerCase(),
)
if (!validVariants.includes(input.toLocaleLowerCase())) {
if (!validVariants.includes(input.toLowerCase())) {
throw Error(
`Invalid 'ActionType' "${input}", must be one of ['${validVariants.join(`', '`)}']`,
)
}
return ActionType[input.toUpperCase() as keyof typeof ActionType]
return GeneratedGraphQLTypes.ActionType[
input.toLowerCase() as keyof typeof GeneratedGraphQLTypes.ActionType
]
}

export function validateActionStatus(input: string): ActionStatus {
const validVariants = Object.keys(ActionStatus).map(variant =>
variant.toLocaleLowerCase(),
export function validateActionStatus(input: string): GeneratedGraphQLTypes.ActionStatus {
const validVariants = Object.keys(GeneratedGraphQLTypes.ActionStatus).map(variant =>
variant.toLowerCase(),
)
if (!validVariants.includes(input.toLocaleLowerCase())) {
if (!validVariants.includes(input.toLowerCase())) {
throw Error(
`Invalid 'ActionStatus' "${input}", must be one of ['${validVariants.join(
`', '`,
)}']`,
)
}
return ActionStatus[input.toUpperCase() as keyof typeof ActionStatus]
return GeneratedGraphQLTypes.ActionStatus[
input.toUpperCase() as keyof typeof GeneratedGraphQLTypes.ActionStatus
]
}

export function buildActionFilter(
Expand All @@ -142,8 +142,8 @@ export function buildActionFilter(
status: string | undefined,
source: string | undefined,
reason: string | undefined,
): ActionFilter {
const filter: ActionFilter = {}
): GeneratedGraphQLTypes.ActionFilter {
const filter: GeneratedGraphQLTypes.ActionFilter = {}
if (id) {
filter.id = +id
}
Expand All @@ -169,7 +169,7 @@ export function buildActionFilter(

export async function queueActions(
client: IndexerManagementClient,
actions: ActionInput[],
actions: GeneratedGraphQLTypes.ActionInput[],
): Promise<ActionResult[]> {
const result = await client
.mutation(
Expand Down Expand Up @@ -202,8 +202,11 @@ export async function queueActions(
return result.data.queueActions
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ACTION_PARAMS_PARSERS: Record<keyof ActionUpdateInput, (x: never) => any> = {
const ACTION_PARAMS_PARSERS: Record<
keyof GeneratedGraphQLTypes.ActionUpdateInput,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(x: never) => any
> = {
deploymentID: x => nullPassThrough(x),
allocationID: x => x,
amount: nullPassThrough(parseGRT),
Expand All @@ -212,7 +215,7 @@ const ACTION_PARAMS_PARSERS: Record<keyof ActionUpdateInput, (x: never) => any>
type: x => validateActionType(x),
status: x => validateActionStatus(x),
reason: nullPassThrough,
protocolNetwork: x => validateNetworkIdentifier(x),
id: x => nullPassThrough(x),
}

/**
Expand Down Expand Up @@ -374,7 +377,7 @@ export async function fetchAction(

export async function fetchActions(
client: IndexerManagementClient,
actionFilter: ActionFilter,
actionFilter: GeneratedGraphQLTypes.ActionFilter,
first?: number,
orderBy?: ActionParams,
orderDirection?: OrderDirection,
Expand Down Expand Up @@ -461,7 +464,7 @@ export async function deleteActions(

export async function updateActions(
client: IndexerManagementClient,
filter: ActionFilter,
filter: GeneratedGraphQLTypes.ActionFilter,
action: ActionUpdateInput,
): Promise<ActionResult[]> {
const result = await client
Expand Down
4 changes: 2 additions & 2 deletions packages/indexer-cli/src/commands/indexer/actions/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
extractProtocolNetworkOption,
} from '../../../command-helpers'
import { approveActions, fetchActions } from '../../../actions'
import { ActionStatus, resolveChainAlias } from '@graphprotocol/indexer-common'
import { GeneratedGraphQLTypes, resolveChainAlias } from '@graphprotocol/indexer-common'

const HELP = `
${chalk.bold('graph indexer actions approve')} [options] [<actionID1> ...]
Expand Down Expand Up @@ -65,7 +65,7 @@ module.exports = {
)
}
const queuedActions = await fetchActions(client, {
status: ActionStatus.QUEUED,
status: GeneratedGraphQLTypes.ActionStatus.queued,
protocolNetwork,
})

Expand Down
8 changes: 4 additions & 4 deletions packages/indexer-cli/src/commands/indexer/actions/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ module.exports = {
} = parameters.options

const [action] = fixParameters(parameters, { h, help }) || []
let orderByParam = ActionParams.ID
let orderDirectionValue = OrderDirection.DESC
let orderByParam = ActionParams.id
let orderDirectionValue = OrderDirection.desc
const outputFormat = o || output || 'table'

const protocolNetwork: string | undefined = extractProtocolNetworkOption(
Expand Down Expand Up @@ -142,8 +142,8 @@ module.exports = {
if (orderBy) {
orderByParam = ActionParams[orderBy.toUpperCase() as keyof typeof ActionParams]
orderDirectionValue = orderDirection
? OrderDirection[orderDirection.toUpperCase() as keyof typeof OrderDirection]
: OrderDirection.DESC
? OrderDirection[orderDirection.toLowerCase() as keyof typeof OrderDirection]
: OrderDirection.desc
}

if (!['undefined', 'number'].includes(typeof first)) {
Expand Down
10 changes: 3 additions & 7 deletions packages/indexer-cli/src/commands/indexer/actions/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import {
printObjectOrArray,
} from '../../../command-helpers'
import { buildActionInput, queueActions, validateActionType } from '../../../actions'
import {
ActionInput,
ActionStatus,
resolveChainAlias,
} from '@graphprotocol/indexer-common'
import { GeneratedGraphQLTypes, resolveChainAlias } from '@graphprotocol/indexer-common'

const HELP = `
${chalk.bold(
Expand Down Expand Up @@ -65,7 +61,7 @@ module.exports = {
const [type, targetDeployment, param1, param2, param3, param4] =
parameters.array || []

let actionInputParams: ActionInput
let actionInputParams: GeneratedGraphQLTypes.ActionInput
try {
if (!['json', 'yaml', 'table'].includes(outputFormat)) {
throw Error(
Expand All @@ -80,7 +76,7 @@ module.exports = {
{ targetDeployment, param1, param2, param3, param4 },
decisionSource,
decisionReason,
ActionStatus.QUEUED,
'queued',
executionPriority,
networkIdentifier,
)
Expand Down
4 changes: 2 additions & 2 deletions packages/indexer-cli/src/commands/indexer/actions/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import chalk from 'chalk'

import {
Action,
ActionFilter,
ActionUpdateInput,
GeneratedGraphQLTypes,
resolveChainAlias,
} from '@graphprotocol/indexer-common'
import { loadValidatedConfig } from '../../../config'
Expand Down Expand Up @@ -44,7 +44,7 @@ module.exports = {

const [...setValues] = fixParameters(parameters, { h, help }) || []
let updateActionInput: ActionUpdateInput = {}
let actionFilter: ActionFilter = {}
let actionFilter: GeneratedGraphQLTypes.ActionFilter = {}

const outputFormat = o || output || 'table'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { BigNumber } from 'ethers'
import { createAllocation } from '../../../allocations'
import {
processIdentifier,
SubgraphIdentifierType,
validateNetworkIdentifier,
} from '@graphprotocol/indexer-common'
import { printObjectOrArray } from '../../../command-helpers'
Expand Down Expand Up @@ -73,7 +72,7 @@ module.exports = {
all: false,
global: false,
})
if (type !== SubgraphIdentifierType.DEPLOYMENT) {
if (type !== 'deployment') {
throw Error(
`Invalid 'deploymentID' provided (${deploymentID}), must be bytes32 or base58 formatted)`,
)
Expand Down
6 changes: 3 additions & 3 deletions packages/indexer-cli/src/commands/indexer/allocations/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createIndexerManagementClient } from '../../../client'
import { extractProtocolNetworkOption, fixParameters } from '../../../command-helpers'
import gql from 'graphql-tag'
import { SubgraphDeploymentID } from '@graphprotocol/common-ts'
import { processIdentifier, SubgraphIdentifierType } from '@graphprotocol/indexer-common'
import { GeneratedGraphQLTypes, processIdentifier } from '@graphprotocol/indexer-common'
import { IndexerAllocation, printIndexerAllocations } from '../../../allocations'
import { utils } from 'ethers'

Expand Down Expand Up @@ -75,14 +75,14 @@ module.exports = {
}

let deploymentString: string | undefined = undefined
let type: SubgraphIdentifierType
let type: GeneratedGraphQLTypes.IdentifierType

if (deployment) {
;[deploymentString, type] = await processIdentifier(deployment, {
all: true,
global: false,
})
if (type !== SubgraphIdentifierType.DEPLOYMENT) {
if (type !== 'deployment') {
throw Error(
`Invalid '--deployment' must be a valid deployment ID (bytes32 or base58 formatted)`,
)
Expand Down
4 changes: 2 additions & 2 deletions packages/indexer-cli/src/commands/indexer/rules/maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
fixParameters,
parseOutputFormat,
} from '../../../command-helpers'
import { IndexingDecisionBasis, processIdentifier } from '@graphprotocol/indexer-common'
import { processIdentifier } from '@graphprotocol/indexer-common'
import { setIndexingRule, displayRules, parseIndexingRule } from '../../../rules'

const HELP = `
Expand Down Expand Up @@ -54,7 +54,7 @@ module.exports = {
const inputRule = parseIndexingRule({
identifier,
identifierType,
decisionBasis: IndexingDecisionBasis.RULES,
decisionBasis: 'rules',
protocolNetwork,
})

Expand Down
4 changes: 2 additions & 2 deletions packages/indexer-cli/src/commands/indexer/rules/offchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
fixParameters,
parseOutputFormat,
} from '../../../command-helpers'
import { IndexingDecisionBasis, processIdentifier } from '@graphprotocol/indexer-common'
import { processIdentifier } from '@graphprotocol/indexer-common'
import { setIndexingRule, displayRules, parseIndexingRule } from '../../../rules'

const HELP = `
Expand Down Expand Up @@ -56,7 +56,7 @@ module.exports = {
identifier,
identifierType,
protocolNetwork,
decisionBasis: IndexingDecisionBasis.OFFCHAIN,
decisionBasis: 'offchain',
})

// Update the indexing rule according to the key/value pairs
Expand Down
4 changes: 2 additions & 2 deletions packages/indexer-cli/src/commands/indexer/rules/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
fixParameters,
parseOutputFormat,
} from '../../../command-helpers'
import { IndexingDecisionBasis, processIdentifier } from '@graphprotocol/indexer-common'
import { processIdentifier } from '@graphprotocol/indexer-common'
import { setIndexingRule, displayRules, parseIndexingRule } from '../../../rules'

const HELP = `
Expand Down Expand Up @@ -56,7 +56,7 @@ module.exports = {
const inputRule = parseIndexingRule({
identifier,
identifierType,
decisionBasis: IndexingDecisionBasis.ALWAYS,
decisionBasis: 'always',
protocolNetwork,
})

Expand Down
4 changes: 2 additions & 2 deletions packages/indexer-cli/src/commands/indexer/rules/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
fixParameters,
parseOutputFormat,
} from '../../../command-helpers'
import { IndexingDecisionBasis, processIdentifier } from '@graphprotocol/indexer-common'
import { processIdentifier } from '@graphprotocol/indexer-common'
import { setIndexingRule, displayRules, parseIndexingRule } from '../../../rules'

const HELP = `
Expand Down Expand Up @@ -56,7 +56,7 @@ module.exports = {
const inputRule = parseIndexingRule({
identifier,
identifierType,
decisionBasis: IndexingDecisionBasis.NEVER,
decisionBasis: 'never',
protocolNetwork,
})

Expand Down
Loading

0 comments on commit 94926f7

Please sign in to comment.