Skip to content

Commit

Permalink
added inspection instruction wrapper. Inspect command
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoAD committed Jan 20, 2022
1 parent a6a740a commit 84c69da
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 131 deletions.
2 changes: 1 addition & 1 deletion packages-ts/gauntlet-terra-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"@chainlink/gauntlet-core": "0.0.7",
"@chainlink/gauntlet-terra": "*",
"@terra-money/terra.js": "^2.1.2",
"@terra-money/terra.js": "^3.0.4",
"ajv": "^8.6.3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,3 @@ export const instructionToCommand = (instruction: AbstractInstruction<any, any>)
}
}
}

export interface InspectInstruction<Input, OnchainData> {
instruction: {
contract: string
function: string
}
inspect: (input: Input, data: OnchainData) => boolean
makeInput: (flags: any) => Promise<Input>
}

export const instructionToInspectCommand = <Input, OnchainData>(
inspectInstruction: InspectInstruction<Input, OnchainData>,
) => {
const id = `${inspectInstruction.instruction.contract}:inspect`
return class Command extends TerraCommand {
static id = id
command: AbstractCommand

constructor(flags, args) {
super(flags, args)
}

execute = async (): Promise<Result<TransactionResponse>> => {
const abstractCommand = await makeAbstractCommand(id, this.flags, this.args)
abstractCommand.invokeMiddlewares(abstractCommand, abstractCommand.middlewares)

const generatedData = await inspectInstruction.makeInput(this.flags)
const { data } = await abstractCommand.execute()
const inspection = inspectInstruction.inspect(generatedData, data)
return {
data: inspection,
responses: [
{
contract: this.args[0],
},
],
} as Result<TransactionResponse>
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const parseParams = (commandOpts: AbstractOpts, params: any): AbstractPar
throw new Error(`Error validating parameters for function ${commandOpts.function}`)
}

return params
return data
}

type AbstractExecute = (params: any, address?: string) => Promise<Result<TransactionResponse>>
Expand Down Expand Up @@ -142,9 +142,7 @@ export default class AbstractCommand extends TerraCommand {
logger.loading(`Executing ${this.opts.function} from contract ${this.opts.contract.id} at ${address}`)
logger.log('Input Params:', params)
await prompt(`Continue?`)
const tx = await this.call(address, {
[this.opts.function]: params,
})
const tx = await this.call(address, params)
logger.success(`Execution finished at tx ${tx.hash}`)
return {
responses: [
Expand All @@ -158,10 +156,8 @@ export default class AbstractCommand extends TerraCommand {

abstractQuery: AbstractExecute = async (params: any, address: string) => {
logger.loading(`Calling ${this.opts.function} from contract ${this.opts.contract.id} at ${address}`)
const result = await this.query(address, {
[this.opts.function]: params,
})
logger.success(`Query finished with result: ${result}`)
const result = await this.query(address, params)
logger.debug(`Query finished with result: ${JSON.stringify(result)}`)
return {
data: result,
responses: [
Expand Down Expand Up @@ -190,7 +186,6 @@ export default class AbstractCommand extends TerraCommand {
help: this.abstractHelp,
}

// TODO: The contract address should come from a specific flag
const address = this.args[0]
return operations[this.opts.action](this.params, address)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import AbstractCommand, { makeAbstractCommand } from '.'
import { Result } from '@chainlink/gauntlet-core'
import { TerraCommand, TransactionResponse } from '@chainlink/gauntlet-terra'

export type InspectionInput<CommandInput, Expected> = {
commandInput?: CommandInput
expected: Expected
}

/**
* Inspection commands need to match this interface
* command: {
* contract: Contract related to the inspection
* id: Name of the command the user will execute
* }
* instructions: instruction[] Set of abstract query commands the inspection command will run
* makeInput: Receives flags and args. Should return the input the underneath commands, and the expected result we want
* makeOnchainData: Parses every instruction command result to match the same interface the Inspection command expects
* inspect: Compares both expected and onchain data.
*/
export interface InspectInstruction<CommandInput, Expected> {
command: {
contract: 'ocr2'
id: 'inspect'
}
instructions: {
contract: string
function: string
}[]
makeInput: (flags: any, args: string[]) => Promise<InspectionInput<CommandInput, Expected>>
makeOnchainData: (instructionsData: any[]) => Expected
inspect: (expected: Expected, data: Expected) => boolean
}

export const instructionToInspectCommand = <CommandInput, Expected>(
inspectInstruction: InspectInstruction<CommandInput, Expected>,
) => {
const id = `${inspectInstruction.command.contract}:${inspectInstruction.command.id}`
return class Command extends TerraCommand {
static id = id
command: AbstractCommand

constructor(flags, args) {
super(flags, args)
}

execute = async (): Promise<Result<TransactionResponse>> => {
const input = await inspectInstruction.makeInput(this.flags, this.args)
const commands = await Promise.all(
inspectInstruction.instructions.map((instruction) =>
makeAbstractCommand(
`${instruction.contract}:${instruction.function}`,
this.flags,
this.args,
input.commandInput,
),
),
)

const data = await Promise.all(
commands.map(async (command) => {
command.invokeMiddlewares(command, command.middlewares)
const { data } = await command.execute()
return data
}),
)

const onchainData = inspectInstruction.makeOnchainData(data)
const inspection = inspectInstruction.inspect(input.expected, onchainData)
return {
data: inspection,
responses: [
{
tx: {
hash: '',
wait: () => ({ success: inspection }),
},
contract: this.args[0],
},
],
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getRDD } from '../../../lib/rdd'
import { instructionToCommand, AbstractInstruction } from '../../abstract/wrapper'
import { instructionToCommand, AbstractInstruction } from '../../abstract/executionWrapper'

type CommandInput = {
billingAccessController: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import Deploy from './deploy'
import SetBilling from './setBilling'
import SetConfig from './setConfig'
import SetPayees from './setPayees'
import Inspect from './inspection/inspect'

export default [SetupFlow, Deploy, SetBilling, SetConfig, SetPayees, OCR2InitializeFlow]
export default [SetupFlow, Deploy, SetBilling, SetConfig, SetPayees, OCR2InitializeFlow, Inspect]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import DeployOCR2 from './deploy'
import SetBilling from './setBilling'
import SetConfig from './setConfig'
import SetPayees from './setPayees'
import Inspect from './inspection/inspect'

export default class OCR2InitializeFlow extends FlowCommand<TransactionResponse> {
static id = 'ocr2:initialize:flow'
Expand Down Expand Up @@ -47,16 +48,11 @@ export default class OCR2InitializeFlow extends FlowCommand<TransactionResponse>
args: [this.getReportStepDataById(FlowCommand.ID.contract(this.stepIds.OCR_2))],
},
// Inspection here
// {
// name: 'Inspection',
// command: OCR2Inspect,
// flags: {
// state: FlowCommand.ID.contract(this.stepIds.OCR_2),
// billingAccessController: process.env.BILLING_ACCESS_CONTROLLER || this.flags.billingAccessController,
// requesterAccessController: process.env.REQUESTER_ACCESS_CONTROLLER || this.flags.requesterAccessController,
// link: process.env.LINK || this.flags.link,
// },
// },
{
name: 'Inspection',
command: Inspect,
args: [this.getReportStepDataById(FlowCommand.ID.contract(this.stepIds.OCR_2))],
},
]
}

Expand Down
Loading

0 comments on commit 84c69da

Please sign in to comment.