Skip to content

Commit

Permalink
common, cli: relax --network option req. when fetching disputes
Browse files Browse the repository at this point in the history
  • Loading branch information
tilacog committed Jul 25, 2023
1 parent 51e2074 commit f21a460
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 35 deletions.
20 changes: 3 additions & 17 deletions packages/indexer-cli/src/commands/indexer/disputes/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chalk from 'chalk'
import { loadValidatedConfig } from '../../../config'
import { createIndexerManagementClient } from '../../../client'
import { disputes, printDisputes } from '../../../disputes'
import { requireProtocolNetworkOption, parseOutputFormat } from '../../../command-helpers'
import { parseOutputFormat, extractProtocolNetworkOption } from '../../../command-helpers'

const HELP = `
${chalk.bold(
Expand All @@ -25,7 +25,7 @@ module.exports = {
run: async (toolbox: GluegunToolbox) => {
const { print, parameters } = toolbox

const { h, help, o, output, n, network } = parameters.options
const { h, help, o, output } = parameters.options
const [status, minAllocationClosedEpoch] = parameters.array || []
const outputFormat = parseOutputFormat(print, o || output || 'table')

Expand All @@ -50,32 +50,18 @@ module.exports = {
return
}

let protocolNetwork: string | null = null
if (n || network) {
try {
// TODO:L2: Protocol Network should be optional in this case. To make it so, we
// also need to relax the requirement for the protocol network field on all
// GrapQL quieries and respective resolvers involving POI disputes.
protocolNetwork = requireProtocolNetworkOption(parameters.options)
} catch (error) {
print.error(`Failed to parse network option: ${error.message()}`)
process.exitCode = 1
return
}
}

const config = loadValidatedConfig()

// Create indexer API client
const client = await createIndexerManagementClient({ url: config.api })
try {
const protocolNetwork = extractProtocolNetworkOption(parameters.options)
const storedDisputes = await disputes(
client,
status,
+minAllocationClosedEpoch,
protocolNetwork,
)

printDisputes(print, outputFormat, storedDisputes)
} catch (error) {
print.error(error.toString())
Expand Down
14 changes: 11 additions & 3 deletions packages/indexer-cli/src/disputes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,22 @@ export const disputes = async (
client: IndexerManagementClient,
status: string,
minClosedEpoch: number,
protocolNetwork: string | null,
protocolNetwork: string | undefined,
): Promise<Partial<POIDisputeAttributes>[]> => {
try {
const result = await client
.query(
gql`
query disputes($status: String!, $minClosedEpoch: Int!, $protocolNetwork: String) {
disputes(status: $status, minClosedEpoch: $minClosedEpoch, $protocolNetwork) {
query disputes(
$status: String!
$minClosedEpoch: Int!
$protocolNetwork: String
) {
disputes(
status: $status
minClosedEpoch: $minClosedEpoch
protocolNetwork: $protocolNetwork
) {
allocationID
allocationIndexer
allocationAmount
Expand Down
2 changes: 1 addition & 1 deletion packages/indexer-common/src/indexer-management/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ const SCHEMA_SDL = gql`
disputes(
status: String!
minClosedEpoch: Int!
protocolNetwork: String!
protocolNetwork: String
): [POIDispute]!
disputesClosedAfter(closedAfterBlock: BigInt!, protocolNetwork: String): [POIDispute]!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { POIDispute, POIDisputeIdentifier, POIDisputeCreationAttributes } from '../models'
import { IndexerManagementResolverContext } from '../client'
import { validateNetworkIdentifier } from '../../parsers'
import { Op } from 'sequelize'
import { Op, WhereOptions } from 'sequelize'
import groupBy from 'lodash.groupby'

export default {
Expand All @@ -22,24 +22,25 @@ export default {
status,
minClosedEpoch,
protocolNetwork: uncheckedProtocolNetwork,
}: { status: string; minClosedEpoch: number; protocolNetwork: string },
}: { status: string; minClosedEpoch: number; protocolNetwork: string | undefined },
{ models }: IndexerManagementResolverContext,
): Promise<object | null> => {
// Sanitize protocol network identifier
const protocolNetwork = validateNetworkIdentifier(uncheckedProtocolNetwork)
const protocolNetwork = uncheckedProtocolNetwork
? validateNetworkIdentifier(uncheckedProtocolNetwork)
: undefined

const sqlAndExpression: WhereOptions<any> = [
{ status },
{ closedEpoch: { [Op.gte]: minClosedEpoch } },
]

if (protocolNetwork) {
sqlAndExpression.push({ protocolNetwork })
}

const disputes = await models.POIDispute.findAll({
where: {
[Op.and]: [
{ status },
{
closedEpoch: {
[Op.gte]: minClosedEpoch,
},
},
{ protocolNetwork },
],
},
where: { [Op.and]: sqlAndExpression },
order: [['allocationAmount', 'DESC']],
})
return disputes.map((dispute) => dispute.toGraphQL())
Expand Down

0 comments on commit f21a460

Please sign in to comment.