diff --git a/package.json b/package.json index 0a6262d..4330d56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "root", "version": "1.0.0", + "packageManager": "yarn@1.22.21", "private": true, "devDependencies": { "@typescript-eslint/eslint-plugin": "^4.6.1", diff --git a/packages/automation/package.json b/packages/automation/package.json index 4062eca..35d85e3 100644 --- a/packages/automation/package.json +++ b/packages/automation/package.json @@ -1,6 +1,7 @@ { "name": "@oasisdex/automation", - "version": "1.5.8", + "packageManager": "yarn@1.22.21", + "version": "1.5.9-alpha5", "description": "The set of utilities for Oasis automation", "homepage": "https://github.com/OasisDEX/common#readme", "main": "lib/src/index.js", diff --git a/packages/automation/src/abi-coding.ts b/packages/automation/src/abi-coding.ts index 0b9bae3..4bf6d8d 100644 --- a/packages/automation/src/abi-coding.ts +++ b/packages/automation/src/abi-coding.ts @@ -5,7 +5,7 @@ import { getDefinitionForCommandAddress, getDefinitionForCommandType, } from './mapping'; -import { CommandContractType } from './types'; +import { CommandContractType, TriggerType, triggerTypeToCommandContractTypeMap } from './types'; export function decodeTriggerData( commandAddress: string, @@ -16,6 +16,20 @@ export function decodeTriggerData( return utils.defaultAbiCoder.decode(paramTypes, data); } +export function decodeTriggerDataByType(type: CommandContractType, data: string): utils.Result { + const paramTypes = getDefinitionForCommandType(type); + return utils.defaultAbiCoder.decode(paramTypes, data); +} + +export function decodeTriggerDataByTriggerType( + triggerType: TriggerType, + data: string, +): utils.Result { + const type = triggerTypeToCommandContractTypeMap[triggerType]; + const paramTypes = getDefinitionForCommandType(type); + return utils.defaultAbiCoder.decode(paramTypes, data); +} + export function decodeTriggerDataAsJson( commandAddress: string, network: number, @@ -30,11 +44,6 @@ export function decodeTriggerDataAsJson( }, {}); } -export function decodeTriggerDataByType(type: CommandContractType, data: string): utils.Result { - const paramTypes = getDefinitionForCommandType(type); - return utils.defaultAbiCoder.decode(paramTypes, data); -} - export function decodeTriggerDataByTypeAsJson( type: CommandContractType, data: string, @@ -46,7 +55,18 @@ export function decodeTriggerDataByTypeAsJson( return acc; }, {}); } +export function decodeTriggerDataByTriggerTypeAsJson( + triggerType: TriggerType, + data: string, +): utils.Result { + const type = triggerTypeToCommandContractTypeMap[triggerType]; + const arr: any[] = decodeTriggerDataByType(type, data) as any[]; + return arr.reduce((acc, curr, idx) => { + acc[commandTypeJsonMapping[type][idx]] = curr.toString(); + return acc; + }, {}); +} export function encodeTriggerData( commandAddress: string, network: number, @@ -60,3 +80,12 @@ export function encodeTriggerDataByType(type: CommandContractType, values: reado const paramTypes = getDefinitionForCommandType(type); return utils.defaultAbiCoder.encode(paramTypes, values); } + +export function encodeTriggerDataByTriggerType( + triggerType: TriggerType, + values: readonly any[], +): string { + const commandType = triggerTypeToCommandContractTypeMap[triggerType]; + const paramTypes = getDefinitionForCommandType(commandType); + return utils.defaultAbiCoder.encode(paramTypes, values); +} diff --git a/packages/automation/src/index.ts b/packages/automation/src/index.ts index 6290c06..3cd5b18 100644 --- a/packages/automation/src/index.ts +++ b/packages/automation/src/index.ts @@ -1,11 +1,19 @@ export { decodeTriggerData, decodeTriggerDataByType, + decodeTriggerDataByTriggerType, encodeTriggerData, encodeTriggerDataByType, decodeTriggerDataAsJson, + encodeTriggerDataByTriggerType, decodeTriggerDataByTypeAsJson, + decodeTriggerDataByTriggerTypeAsJson, } from './abi-coding'; export { getCommandAddresses } from './mapping'; -export { CommandContractType, TriggerType, TriggerGroupType } from './types'; +export { + CommandContractType, + TriggerType, + TriggerGroupType, + triggerTypeToCommandContractTypeMap, +} from './types'; diff --git a/packages/automation/src/mapping.ts b/packages/automation/src/mapping.ts index 9c01abb..5885d7f 100644 --- a/packages/automation/src/mapping.ts +++ b/packages/automation/src/mapping.ts @@ -3,6 +3,8 @@ import { CommandContractType, EthereumNetwork, ParamDefinition, + triggerTypeToCommandContractTypeMap, + TriggerType, } from './types'; export const commandTypeJsonMapping: Record = { @@ -96,6 +98,32 @@ export const commandTypeJsonMapping: Record = { 'executionPrice', 'maxBaseFeeInGwei', ], + [CommandContractType.AaveBasicSellCommandV2]: [ + 'positionAddress', + 'triggerType', + 'maxCoverage', + 'debtToken', + 'collateralToken', + 'opHash', + 'execLtv', + 'targetLtv', + 'minSellPrice', + 'deviation', + 'maxBaseFeeInGwei', + ], + [CommandContractType.AaveBasicBuyCommandV2]: [ + 'positionAddress', + 'triggerType', + 'maxCoverage', + 'debtToken', + 'collateralToken', + 'opHash', + 'execLtv', + 'targetLtv', + 'maxBuyPrice', + 'deviation', + 'maxBaseFeeInGwei', + ], }; export const commandAddressMapping: Record< @@ -169,6 +197,12 @@ export const commandAddressMapping: Record< '0x2af43189E85CEA21aa8FA5d61139b771328d8D30': { type: CommandContractType.SparkStopLossCommandV2, }, + '0x72241841022bc824B0b66e3D27D8937D36dA4FDF': { + type: CommandContractType.AaveBasicBuyCommandV2, + }, + '0x31d767f6556CE3fC55d6245C9aEF3575aa64BABf': { + type: CommandContractType.AaveBasicSellCommandV2, + }, }, }).map(([network, mapping]) => [ network, @@ -228,26 +262,50 @@ export const defaultCommandTypeMapping = { [CommandContractType.MakerStopLossCommandV2]: ['uint256', 'uint16', 'uint256', 'uint256'], [CommandContractType.MakerAutoTakeProfitCommandV2]: ['uint256', 'uint16', 'uint256', 'uint32'], [CommandContractType.MakerBasicBuyCommandV2]: [ - 'uint256', - 'uint16', - 'uint256', - 'uint256', - 'uint256', - 'uint256', - 'bool', - 'uint64', - 'uint32', + 'uint256', //cdpId + 'uint16', // triggerType + 'uint256', // maxCoverage + 'uint256', // execCollRatio + 'uint256', // targetCollRatio + 'uint256', // maxBuyPrice + 'uint64', // deviation + 'uint32', // maxBaseFeeInGwei ], [CommandContractType.MakerBasicSellCommandV2]: [ - 'uint256', - 'uint16', - 'uint256', - 'uint256', - 'uint256', - 'uint256', - 'bool', - 'uint64', - 'uint32', + 'uint256', //cdpId + 'uint16', // triggerType + 'uint256', // maxCoverage + 'uint256', // execCollRatio + 'uint256', // targetCollRatio + 'uint256', // minSellPrice + 'uint64', // deviation + 'uint32', // maxBaseFeeInGwei + ], + [CommandContractType.AaveBasicBuyCommandV2]: [ + 'address', //positionAddress + 'uint16', // triggerType + 'uint256', // maxCoverage + 'address', // debtToken + 'address', // collateralToken + 'bytes32', // opHash + 'uint256', // execCollRatio + 'uint256', // targetCollRatio + 'uint256', // maxBuyPrice + 'uint64', // deviation + 'uint32', // maxBaseFeeInGwei + ], + [CommandContractType.AaveBasicSellCommandV2]: [ + 'address', //positionAddress + 'uint16', // triggerType + 'uint256', // maxCoverage + 'address', // debtToken + 'address', // collateralToken + 'bytes32', // opHash + 'uint256', // execCollRatio + 'uint256', // targetCollRatio + 'uint256', // minSellPrice + 'uint64', // deviation + 'uint32', // maxBaseFeeInGwei ], } as const; @@ -267,6 +325,12 @@ export function getCommandAddresses(network: number): Record = { + [TriggerType.StopLossToCollateral]: CommandContractType.CloseCommand, + [TriggerType.StopLossToDai]: CommandContractType.CloseCommand, + [TriggerType.BasicBuy]: CommandContractType.BasicBuyCommand, + [TriggerType.BasicSell]: CommandContractType.BasicSellCommand, + [TriggerType.AutoTakeProfitToCollateral]: CommandContractType.AutoTakeProfitCommand, + [TriggerType.AutoTakeProfitToDai]: CommandContractType.AutoTakeProfitCommand, + [TriggerType.SimpleAAVESell]: CommandContractType.SimpleAAVESellCommand, + [TriggerType.AaveStopLossToCollateral]: CommandContractType.AaveStopLossCommand, + [TriggerType.AaveStopLossToDebt]: CommandContractType.AaveStopLossCommand, + [TriggerType.MakerStopLossToCollateralV2]: CommandContractType.MakerStopLossCommandV2, + [TriggerType.MakerStopLossToDaiV2]: CommandContractType.MakerStopLossCommandV2, + [TriggerType.MakerBasicBuyV2]: CommandContractType.MakerBasicBuyCommandV2, + [TriggerType.MakerBasicSellV2]: CommandContractType.MakerBasicSellCommandV2, + [TriggerType.MakerAutoTakeProfitToCollateralV2]: CommandContractType.MakerAutoTakeProfitCommandV2, + [TriggerType.MakerAutoTakeProfitToDaiV2]: CommandContractType.MakerAutoTakeProfitCommandV2, + [TriggerType.AaveStopLossToCollateralV2]: CommandContractType.AaveStopLossCommandV2, + [TriggerType.AaveStopLossToDebtV2]: CommandContractType.AaveStopLossCommandV2, + [TriggerType.SparkStopLossToCollateralV2]: CommandContractType.SparkStopLossCommandV2, + [TriggerType.SparkStopLossToDebtV2]: CommandContractType.SparkStopLossCommandV2, + [TriggerType.AaveBasicBuyV2]: CommandContractType.AaveBasicBuyCommandV2, + [TriggerType.AaveBasicSellV2]: CommandContractType.AaveBasicSellCommandV2, +}; + export enum TriggerGroupType { SingleTrigger = 65535, ConstantMultiple = 1, diff --git a/packages/automation/test/abi-coding.test.ts b/packages/automation/test/abi-coding.test.ts index a8c2fcc..ca88d38 100644 --- a/packages/automation/test/abi-coding.test.ts +++ b/packages/automation/test/abi-coding.test.ts @@ -3,9 +3,12 @@ import { BigNumber as EthersBN, constants, utils } from 'ethers'; import { decodeTriggerData, decodeTriggerDataAsJson, + decodeTriggerDataByTriggerType, + decodeTriggerDataByTriggerTypeAsJson, decodeTriggerDataByType, decodeTriggerDataByTypeAsJson, encodeTriggerData, + encodeTriggerDataByTriggerType, encodeTriggerDataByType, } from '../src/abi-coding'; import { @@ -13,7 +16,7 @@ import { getCommandAddresses, getDefinitionForCommandType, } from '../src/mapping'; -import { CommandContractType, EthereumNetwork } from '../src/types'; +import { CommandContractType, EthereumNetwork, TriggerType } from '../src/types'; describe('abi-coding', () => { const type = CommandContractType.CloseCommand; @@ -29,12 +32,14 @@ describe('abi-coding', () => { const result = encodeTriggerData(commandAddress, network, validValues); expect(result).to.eq(data); }); - it('can encode trigger data by command type', () => { const result = encodeTriggerDataByType(type, validValues); expect(result).to.eq(data); }); - + it('can encode trigger data by trigger type', () => { + const result = encodeTriggerDataByTriggerType(TriggerType.StopLossToCollateral, validValues); + expect(result).to.eq(data); + }); it('can encode if supplied command address is not lowercase', () => { const result = encodeTriggerData(commandAddress, network, validValues); expect(result).to.eq(data); @@ -75,8 +80,13 @@ describe('abi-coding', () => { expect(EthersBN.from(value).toNumber()).to.eq(validValues[idx]); }); }); - - it('can decode trigger data by command address', () => { + it('can decode trigger data by trigger type', () => { + const result = decodeTriggerDataByTriggerType(TriggerType.StopLossToCollateral, data); + result.forEach((value, idx) => { + expect(EthersBN.from(value).toNumber()).to.eq(validValues[idx]); + }); + }); + it('can decode trigger data by command type', () => { const result = decodeTriggerDataByType(type, data); result.forEach((value, idx) => { expect(EthersBN.from(value).toNumber()).to.eq(validValues[idx]); @@ -146,6 +156,14 @@ describe('abi-coding', () => { collRatio: '101', }); }); + it('decodeTriggerDataByTriggerTypeAsJson converts to correct json', () => { + const actual = decodeTriggerDataByTriggerTypeAsJson(TriggerType.StopLossToCollateral, data); + expect(actual).to.deep.eq({ + cdpId: '12', + triggerType: '1', + collRatio: '101', + }); + }); it('decodeTriggerDataByTypeAsJson converts to correct json', () => { const actual = decodeTriggerDataByTypeAsJson(CommandContractType.CloseCommand, data); expect(actual).to.deep.eq({ @@ -174,6 +192,19 @@ describe('abi-coding', () => { targetCollRatio: '100', }); }); + it('decodeTriggerDataByTriggerTypeAsJson converts to correct json', () => { + const actual = decodeTriggerDataByTriggerTypeAsJson(TriggerType.BasicBuy, data); + expect(actual).to.deep.eq({ + cdpId: '12', + triggerType: '1', + execCollRatio: '101', + maxBaseFeeInGwei: '100', + maxBuyPrice: '2000', + continuous: 'true', + deviation: '10', + targetCollRatio: '100', + }); + }); it('decodeTriggerDataByTypeAsJson converts to correct json', () => { const actual = decodeTriggerDataByTypeAsJson(CommandContractType.BasicBuyCommand, data); expect(actual).to.deep.eq({ @@ -204,6 +235,19 @@ describe('abi-coding', () => { ltv: '2000', }); }); + it('decodeTriggerDataByTriggerTypeAsJson converts to correct json', () => { + const actual = decodeTriggerDataByTriggerTypeAsJson( + TriggerType.AaveStopLossToCollateral, + data, + ); + expect(actual).to.deep.eq({ + positionAddress: '0xE78ACEa26B79564C4D29D8c1f5bAd3D4E0414676', + triggerType: '1', + collateralToken: '0xE78ACEa26B79564C4D29D8c1f5bAd3D4E0414676', + debtToken: '0xE78ACEa26B79564C4D29D8c1f5bAd3D4E0414676', + ltv: '2000', + }); + }); it('decodeTriggerDataByTypeAsJson converts to correct json', () => { const actual = decodeTriggerDataByTypeAsJson(CommandContractType.AaveStopLossCommand, data); expect(actual).to.deep.eq({