Skip to content

Commit

Permalink
Add polymorhpic skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
reductionista committed Feb 15, 2022
1 parent 0440233 commit 1819512
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { TransactionResponse, TerraCommand } from '@chainlink/gauntlet-terra'
import { Contract, CONTRACT_LIST, getContract, TerraABI, TERRA_OPERATIONS } from '../../lib/contracts'
import { DEFAULT_RELEASE_VERSION } from '../../lib/constants'
import schema from '../../lib/schema'
import findPolymorphic from './polymorphic'

export { findPolymorphic }

export interface AbstractOpts {
contract: Contract
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { TerraCommand } from '@chainlink/gauntlet-terra'
import { MultisigTerraCommand } from '../contracts/multisig'

type COMMANDS = {
custom:any[]
}

export default (commands:COMMANDS, slug:string) => {
const slugs:string[] = slug.split(':')
if (slugs.length < 3) {
return null
}
const op:string = slugs.pop()!
const command:any = commands.custom[slugs.join()]
if (!!command) return undefined

switch(op) {
case 'multisig':
case 'propose':
case 'vote':
case 'execute':
case 'approve': // vote yes, then execute if threshold is reached
return class Command extends MultisigTerraCommand {
static id = slugs.join()
multisigOp = () => { return op }
commandType = () => { return command }
}
default:
return undefined
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { CreateGroup } from './group'
import { CreateWallet } from './wallet'
import {MultisigTerraCommand} from './multisig'

export default [CreateGroup, CreateWallet]
export { MultisigTerraCommand }
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// multisig.ts
//
// For now, propose, vote, and execute functionality are all combined into one CONTRACT:COMMAND::multisig meta-command
// This is parallel to how things are implemented in Solana. The execute happens automatically as soon as the last
// vote required to exceeed the threshold is cast. And the difference between propose and vote is distinguished by
// whether the --proposal=PROPOSAL_HASH flag is passed. Later, we may want to split this into CONTRACT::COMMAND::propose,
// CONTRACT::COMMAND::vote, and CONTRACT::COMMAND::execute. We may also want to add CONTRACT::COMMAND::close, to
// abort a proposal early (before it expires), disallowing any further voting on it.

import { logger, prompt } from '@chainlink/gauntlet-core/dist/utils'
import { Result } from '@chainlink/gauntlet-core'
import { TerraCommand, TransactionResponse } from '@chainlink/gauntlet-terra'
import { CATEGORIES } from '../../../lib/constants'
import { CONTRACT_LIST, Contract, getContract } from '../../../lib/contracts'

type StringGetter = () => string

abstract class MultisigTerraCommand extends TerraCommand {
static category = CATEGORIES.MULTISIG

commandType:any
multisigOp:StringGetter

command:TerraCommand
multisigAddress:string
multisigContract: Promise<Contract>

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

logger.info(`Running ${this.commandType()} in multisig mode`)
this.command = new this.commandType()(flags, args)
this.command.invokeMiddlewares(this.command, this.command.middlewares)
this.require(!!process.env.MULTISIG_ADDRESS, 'Please set MULTISIG_ADDRESS env var')
this.multisigContract = getContract(CONTRACT_LIST.MULTISIG, flags.version)
this.multisigAddress = process.env.MULTISIG_ADDRESS!
}

execute = async (): Promise<Result<TransactionResponse>> => {
if ( MultisigTerraCommand.id[1] == 'deploy' )
this.command.run()

return {
responses: [
{
tx: new TransactionResponse,
contract: ''
}
]
} as Result<TransactionResponse>
}
}

export { MultisigTerraCommand }
4 changes: 2 additions & 2 deletions packages-ts/gauntlet-terra-contracts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { executeCLI } from '@chainlink/gauntlet-core'
import { existsSync } from 'fs'
import path from 'path'
import Terra from './commands'
import { makeAbstractCommand } from './commands/abstract'
import { makeAbstractCommand, findPolymorphic } from './commands/abstract'
import { defaultFlags } from './lib/args'

const commands = {
custom: [...Terra],
loadDefaultFlags: () => defaultFlags,
abstract: {
findPolymorphic: () => undefined,
findPolymorphic: () => findPolymorphic,
makeCommand: makeAbstractCommand,
},
}
Expand Down
6 changes: 5 additions & 1 deletion packages-ts/gauntlet-terra-contracts/src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import JTD from 'ajv/dist/jtd'

const ajv = new Ajv().addFormat('uint8', (value: any) => !isNaN(value))

ajv.addFormat('uint32', {
type: 'number',
validate: (x) => !isNaN(x),
})

ajv.addFormat('uint64', {
type: 'number',
validate: (x) => !isNaN(x),
Expand All @@ -14,6 +19,5 @@ ajv.addFormat('uint32', {
})

export default ajv

const jtd = new JTD()
export { jtd }

0 comments on commit 1819512

Please sign in to comment.