Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lw/6242 fix hh tasks v2 #145

Open
wants to merge 10 commits into
base: automation_v2
Choose a base branch
from
2 changes: 1 addition & 1 deletion scripts/common/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const addresses = {
EXCHANGE: '0xb5eb8cb6ced6b6f8e13bcd502fb489db4a726c7b',
AUTOMATION_SERVICE_REGISTRY: '0x9b4Ae7b164d195df9C4Da5d08Be88b2848b2EaDA',
AUTOMATION_BOT: '0x6E87a7A0A03E51A741075fDf4D1FCce39a4Df01b',
AUTOMATION_BOT_STORAGE: '',
AUTOMATION_BOT_STORAGE: '', // for testing remove on local insert addres here ~Ł
AUTOMATION_EXECUTOR: '0x87607992FDd5eAe12201bFBE83432D469944EE1C',
AUTOMATION_MCD_VIEW: '0x55Dc2Be8020bCa72E58e665dC931E03B749ea5E0',
AUTOMATION_MCD_UTILS: '0x68Ff2d96EDD4aFfcE9CBE82BF55F0B70acb483Ea',
Expand Down
9 changes: 8 additions & 1 deletion scripts/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ function getTriggerDataTypes(triggerType: TriggerType) {
throw new Error(`Error determining trigger data types. Unsupported trigger type: ${triggerType}`)
}
}

// replace with common encodeTriggerDataByType ?
export function encodeTriggerData(vaultId: number, triggerType: TriggerType, ...rest: any[]): BytesLike {
const args = [vaultId, triggerType, ...rest]
const types = getTriggerDataTypes(triggerType)
// TODO ŁW I want to use methods from common to be consistent with oasis-borrow
// const types = getDefinitionForCommandType(triggerType),

console.log('args')
console.log(args)
console.log('types')
console.log(types)
return utils.defaultAbiCoder.encode(types, args)
}

Expand Down
26 changes: 22 additions & 4 deletions scripts/tasks/create-trigger.task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TriggerGroupType, TriggerType } from '@oasisdex/automation'
import { CommandContractType, encodeTriggerDataByType, TriggerGroupType, TriggerType } from '@oasisdex/automation'
import { BigNumber } from 'bignumber.js'
import { Signer, BigNumber as EthersBN } from 'ethers'
import { types } from 'hardhat/config'
Expand Down Expand Up @@ -27,6 +27,7 @@ interface CreateTriggerArgs extends BaseTaskArgs {
createTask<CreateTriggerArgs>('create-trigger', 'Creates an automation trigger for a user')
.addParam('vault', 'The vault (cdp) ID', undefined, params.bignumber, false)
.addParam('type', 'The trigger type', TriggerType.StopLossToDai, types.int)
.addParam('continuous', 'Is trigger supposed to be continuous', false, types.boolean)
.addParam(
'params',
"The remaining args for the trigger data (i.e. 170). See `encodeTriggerData` for more info.\n For BasicBuy it's [execCollRatio,targetCollRatio,maxBuyPrice,contnuous,deviation,maxBaseFeeInGwei] eg '[23200,21900,'0',true,100,200]'",
Expand Down Expand Up @@ -75,7 +76,11 @@ createTask<CreateTriggerArgs>('create-trigger', 'Creates an automation trigger f
)}`,
)
}
triggerIdToReplace = Math.max(...activeTriggerIds) ?? 0

triggerIdToReplace =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was returning -Infinity for an empty array

Array.isArray(activeTriggerIds) && activeTriggerIds.length !== 0
? Math.max(...activeTriggerIds) ?? 0
: 0
}

let signer: Signer = hre.ethers.provider.getSigner(0)
Expand All @@ -100,8 +105,21 @@ createTask<CreateTriggerArgs>('create-trigger', 'Creates an automation trigger f
})
).wait()
}
const typesToCommandsMap = {
[TriggerType.StopLossToCollateral]: CommandContractType.CloseCommand,
[TriggerType.StopLossToDai]: CommandContractType.CloseCommand,
[TriggerType.BasicBuy]: CommandContractType.BasicBuyCommand,
[TriggerType.BasicSell]: CommandContractType.BasicSellCommand,
[TriggerType.AutoTakeProfitToCollateral]: CommandContractType.AutoTakeProfitCommand,
[TriggerType.AutoTakeProfitToDai]: CommandContractType.AutoTakeProfitCommand,
}
const triggerType = args.type as TriggerType
if (!(triggerType in typesToCommandsMap)) {
throw new Error(`Unknown trigger type ${triggerType}`)
}
const commandType = typesToCommandsMap[triggerType]
const triggerData = encodeTriggerDataByType(commandType, [args.vault.toNumber(), args.type, ...args.params])

const triggerData = encodeTriggerData(args.vault.toNumber(), args.type, ...args.params)
const addTriggerData = bot.interface.encodeFunctionData('addTriggers', [
TriggerGroupType.SingleTrigger,
[args.continuous],
Expand All @@ -126,7 +144,7 @@ createTask<CreateTriggerArgs>('create-trigger', 'Creates an automation trigger f

const tx = await proxy.connect(signer).execute(bot.address, addTriggerData, await hardhatUtils.getGasSettings())
const receipt = await tx.wait()

// check if it works
const [triggerAddedEvent] = getEvents(receipt, bot.interface.getEvent('TriggerAdded'))

if (!triggerAddedEvent) {
Expand Down
20 changes: 13 additions & 7 deletions scripts/tasks/remove-trigger.task.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// WORK IN PROGRESS
// It could be tested on local network but fails due to UNPREDICTABLE_GAS_LIMIT issues
// To be continued after goerli deployment...
import BigNumber from 'bignumber.js'
import { Signer } from 'ethers'
import { types } from 'hardhat/config'
import { coalesceNetwork, HardhatUtils, Network } from '../common'
import { coalesceNetwork, getEvents, HardhatUtils, isLocalNetwork, Network } from '../common'
import { BaseTaskArgs, createTask } from './base.task'
import { params } from './params'

interface RemoveTriggerArgs extends BaseTaskArgs {
vault: BigNumber
trigger: BigNumber
allowance: boolean
forked?: Network
Expand All @@ -13,6 +18,7 @@ interface RemoveTriggerArgs extends BaseTaskArgs {
createTask<RemoveTriggerArgs>('remove-trigger', 'Removes a trigger for a user')
.addParam('trigger', 'The trigger ID', '', params.bignumber)
.addParam('allowance', 'The flag whether to remove allowance', false, types.boolean)
.addParam('vault', 'The vault ID', '', params.bignumber) // previously it was available in trigger but no longer is
.setAction(async (args: RemoveTriggerArgs, hre) => {
const { name: network } = hre.network
console.log(
Expand All @@ -25,9 +31,9 @@ createTask<RemoveTriggerArgs>('remove-trigger', 'Removes a trigger for a user')
'AutomationBotStorage',
hardhatUtils.addresses.AUTOMATION_BOT_STORAGE,
)

/* const triggerInfo = await storage.activeTriggers(args.trigger.toString())
if (triggerInfo.commandAddress.eq('0x0000000000000000000000000000000000000000')) {
const vault = args.vault.toString()
const triggerInfo = await storage.activeTriggers(args.trigger.toString())
if (triggerInfo.commandAddress === '0x0000000000000000000000000000000000000000') {
throw new Error(`Trigger with id ${args.trigger.toString()} is not active`)
}

Expand All @@ -47,8 +53,8 @@ createTask<RemoveTriggerArgs>('remove-trigger', 'Removes a trigger for a user')
console.log(`Impersonating proxy owner ${currentProxyOwner}...`)
signer = await hardhatUtils.impersonate(currentProxyOwner)
}

const removeTriggerData = bot.interface.encodeFunctionData('removeTriggers', [
// use removeTrigger or removeTriggers also for single trigger? ~Ł
const removeTriggerData = bot.interface.encodeFunctionData('removeTrigger', [
[args.trigger.toString()],
args.allowance,
])
Expand All @@ -73,5 +79,5 @@ createTask<RemoveTriggerArgs>('remove-trigger', 'Removes a trigger for a user')
throw new Error(`Failed to remove trigger. Contract Receipt: ${JSON.stringify(receipt)}`)
}

console.log([`Trigger with id ${args.trigger.toString()} was succesfully removed`].concat(info).join('\n')) */
console.log([`Trigger with id ${args.trigger.toString()} was succesfully removed`].concat(info).join('\n'))
})