From 3f8b33e2978dece39403254149d011543edae6fd Mon Sep 17 00:00:00 2001 From: Domino Valdano <2644901+reductionista@users.noreply.github.com> Date: Mon, 14 Feb 2022 15:47:36 -0800 Subject: [PATCH] Add WrapCommand, makeRawTransaction(), ProposalContext Also: fix compiler errors, workaround for gauntlet-core bug in type declaration for findPolymorphic --- .../src/commands/abstract/index.ts | 27 +++++++++----- .../src/commands/abstract/polymorphic.ts | 31 +++++++++++----- .../commands/contracts/multisig/multisig.ts | 37 ++++++++++--------- .../gauntlet-terra-contracts/src/index.ts | 2 +- .../gauntlet-terra/src/commands/types.ts | 4 ++ packages-ts/gauntlet-terra/src/index.ts | 4 +- 6 files changed, 66 insertions(+), 39 deletions(-) diff --git a/packages-ts/gauntlet-terra-contracts/src/commands/abstract/index.ts b/packages-ts/gauntlet-terra-contracts/src/commands/abstract/index.ts index e0ce6baab..e216049a9 100644 --- a/packages-ts/gauntlet-terra-contracts/src/commands/abstract/index.ts +++ b/packages-ts/gauntlet-terra-contracts/src/commands/abstract/index.ts @@ -189,19 +189,28 @@ export default class AbstractCommand extends TerraCommand { } // create and sign transaction, without executing - abstractPrepare = async () => { + makeRawTransaction = async () => { const operations = { - [TERRA_OPERATIONS.DEPLOY]: this.opts.prepareDeploy() - [TERRA_OPERATIONS.EXECUTE]: this.opts.prepareCall() + [TERRA_OPERATIONS.DEPLOY]: this.abstractPrepareDeploy, + [TERRA_OPERATIONS.EXECUTE]: this.abstractPrepareCall, + [TERRA_OPERATIONS.QUERY]: () => { throw Error("makeRawTransaction: cannot make a tx from a query commmand") }, + // TODO: [TERRA_OPERATIONS.UPLOAD]: this.abstractPrepareUpload, } - return await operations[this.opts.action](address, { - [this.opts.function]: this.params, - }) + + return await operations[this.opts.action](this.params, this.args[0]) } - const address = this.args[0] - return operations[this.opts.action](this.params, address) - } +abstractPrepareDeploy = async(params:any) => { + const codeId = this.codeIds[this.opts.contract.id] + this.require(!!codeId, `Code Id for contract ${this.opts.contract.id} not found`) + return await this.prepareDeploy(codeId, params) +} + +abstractPrepareCall = async(params:any, address:string) => { + return await this.prepareCall(address, { + [this.opts.function]: params, + }) +} execute = async () => { const operations = { diff --git a/packages-ts/gauntlet-terra-contracts/src/commands/abstract/polymorphic.ts b/packages-ts/gauntlet-terra-contracts/src/commands/abstract/polymorphic.ts index db7ced4eb..5266625b9 100644 --- a/packages-ts/gauntlet-terra-contracts/src/commands/abstract/polymorphic.ts +++ b/packages-ts/gauntlet-terra-contracts/src/commands/abstract/polymorphic.ts @@ -1,18 +1,21 @@ -import { TerraCommand } from '@chainlink/gauntlet-terra' +import { TerraCommand, TransactionResponse } from '@chainlink/gauntlet-terra' import { MultisigTerraCommand } from '../contracts/multisig' +import { Result, Command } from '@chainlink/gauntlet-core' -type COMMANDS = { - custom: any[] +class EmptyCommand extends Command { + constructor() { + super({help : false}, []) + } } -export default (commands: COMMANDS, slug: string) => { +export default (commands: any[], slug: string) : Command => { const slugs: string[] = slug.split(':') if (slugs.length < 3) { - return null + throw Error(`Command ${slugs.join(':')} not found`) } const op: string = slugs.pop()! - const command: any = commands.custom[slugs.join()] - if (!!command) return undefined + const command: any = commands[slugs.join()] + if (!!command) throw Error(`Command ${slugs.join(':')} not found`) switch (op) { case 'multisig': @@ -20,8 +23,13 @@ export default (commands: COMMANDS, slug: string) => { case 'vote': case 'execute': case 'approve': // vote yes, then execute if threshold is reached - return class Command extends MultisigTerraCommand { + class WrappedCommand extends MultisigTerraCommand { static id = slugs.join() + + constructor(flags, args) { + super(flags, args) + } + multisigOp = () => { return op } @@ -29,7 +37,12 @@ export default (commands: COMMANDS, slug: string) => { return command } } + + let cmd = new EmptyCommand() + let wc = Object.setPrototypeOf(WrappedCommand, EmptyCommand.prototype) + wc = Object.assign(wc, cmd) + return wc default: - return undefined + throw Error(`Command ${slugs.join(':')} not found`) } } diff --git a/packages-ts/gauntlet-terra-contracts/src/commands/contracts/multisig/multisig.ts b/packages-ts/gauntlet-terra-contracts/src/commands/contracts/multisig/multisig.ts index 6dd10302c..4b3f246f2 100644 --- a/packages-ts/gauntlet-terra-contracts/src/commands/contracts/multisig/multisig.ts +++ b/packages-ts/gauntlet-terra-contracts/src/commands/contracts/multisig/multisig.ts @@ -9,9 +9,16 @@ import { logger, prompt } from '@chainlink/gauntlet-core/dist/utils' import { Result } from '@chainlink/gauntlet-core' -import { TerraCommand, TransactionResponse } from '@chainlink/gauntlet-terra' +import { TerraCommand, RawTransaction, TransactionResponse } from '@chainlink/gauntlet-terra' import { CATEGORIES } from '../../../lib/constants' import { CONTRACT_LIST, Contract, getContract, TERRA_OPERATIONS } from '../../../lib/contracts' +import AbstractCommand from '../../abstract' + +type ProposalContext = { + rawTx: RawTransaction, + multisigSigner: string, + proposalState: any, +} type StringGetter = () => string @@ -37,26 +44,20 @@ abstract class MultisigTerraCommand extends TerraCommand { } execute = async (): Promise> => { - const tx = - - switch ( this.command.opts.action) { - case(TERRA_OPERATIONS.deploy): - tx = this.command.prepare_deploy() - } else if () { - this.command.prepare_call() - } - - const contract = getContract() + const tx = this.command.makeRawTransaction() + console.debug(tx) return { - responses: [ - { - tx: tx - contract: contract - } - ] - } as Result + responses: [], + } as Result } } +export const wrapCommand = (command) => { + return class CustomCommand extends MultisigTerraCommand { + static id = `${command.id}:multisig` + static category = CATEGORIES.MULTISIG + } +} + export { MultisigTerraCommand } diff --git a/packages-ts/gauntlet-terra-contracts/src/index.ts b/packages-ts/gauntlet-terra-contracts/src/index.ts index dc45d8d05..be4738e75 100644 --- a/packages-ts/gauntlet-terra-contracts/src/index.ts +++ b/packages-ts/gauntlet-terra-contracts/src/index.ts @@ -9,7 +9,7 @@ const commands = { custom: [...Terra], loadDefaultFlags: () => defaultFlags, abstract: { - findPolymorphic: () => findPolymorphic, + findPolymorphic: findPolymorphic, makeCommand: makeAbstractCommand, }, } diff --git a/packages-ts/gauntlet-terra/src/commands/types.ts b/packages-ts/gauntlet-terra/src/commands/types.ts index 569ef5ad6..96ab68346 100644 --- a/packages-ts/gauntlet-terra/src/commands/types.ts +++ b/packages-ts/gauntlet-terra/src/commands/types.ts @@ -1,6 +1,10 @@ +import { Tx as RawTransaction } from '@terra-money/terra.js' + export type TransactionResponse = { hash: string address?: string wait: () => { success: boolean } tx?: any } + +export { RawTransaction } diff --git a/packages-ts/gauntlet-terra/src/index.ts b/packages-ts/gauntlet-terra/src/index.ts index 5d73d8b88..a9af4e6fe 100644 --- a/packages-ts/gauntlet-terra/src/index.ts +++ b/packages-ts/gauntlet-terra/src/index.ts @@ -1,6 +1,6 @@ import TerraCommand from './commands/internal/terra' import { waitExecute } from './lib/execute' -import { TransactionResponse } from './commands/types' +import { RawTransaction, TransactionResponse } from './commands/types' import * as constants from './lib/constants' -export { TerraCommand, waitExecute, TransactionResponse, constants } +export { RawTransaction, TerraCommand, waitExecute, TransactionResponse, constants }