From 77edd4cacb5d0b32bba9246f4f3b5338e37e89bb Mon Sep 17 00:00:00 2001 From: halaprix Date: Fri, 9 Feb 2024 13:18:57 +0100 Subject: [PATCH 1/4] chore: add `aaveTrailingStopLoss` --- packages/automation/package.json | 2 +- packages/automation/src/abi-coding.ts | 47 +++++++++++++++ packages/automation/src/mapping.ts | 85 ++++++++++++++++++++++++++- packages/automation/src/types.ts | 3 + 4 files changed, 134 insertions(+), 3 deletions(-) diff --git a/packages/automation/package.json b/packages/automation/package.json index ac9d689..1cecde3 100644 --- a/packages/automation/package.json +++ b/packages/automation/package.json @@ -1,7 +1,7 @@ { "name": "@oasisdex/automation", "packageManager": "yarn@1.22.21", - "version": "1.6.0-alpha.9", + "version": "1.6.0-alpha.10", "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 4bf6d8d..205fe23 100644 --- a/packages/automation/src/abi-coding.ts +++ b/packages/automation/src/abi-coding.ts @@ -1,9 +1,11 @@ import { utils } from 'ethers'; import { commandAddressMapping, + commandOffchainDataTypeJsonMapping, commandTypeJsonMapping, getDefinitionForCommandAddress, getDefinitionForCommandType, + getOffchainDataDefinitionForCommandType, } from './mapping'; import { CommandContractType, TriggerType, triggerTypeToCommandContractTypeMap } from './types'; @@ -21,6 +23,11 @@ export function decodeTriggerDataByType(type: CommandContractType, data: string) return utils.defaultAbiCoder.decode(paramTypes, data); } +export function decodeOffchainTriggerDataByType(type: CommandContractType, data: string): utils.Result { + const paramTypes = getOffchainDataDefinitionForCommandType(type); + return utils.defaultAbiCoder.decode(paramTypes, data); +} + export function decodeTriggerDataByTriggerType( triggerType: TriggerType, data: string, @@ -30,6 +37,15 @@ export function decodeTriggerDataByTriggerType( return utils.defaultAbiCoder.decode(paramTypes, data); } +export function decodeOffchainTriggerDataByTriggerType( + triggerType: TriggerType, + data: string, +): utils.Result { + const type = triggerTypeToCommandContractTypeMap[triggerType]; + const paramTypes = getOffchainDataDefinitionForCommandType(type); + return utils.defaultAbiCoder.decode(paramTypes, data); +} + export function decodeTriggerDataAsJson( commandAddress: string, network: number, @@ -67,6 +83,23 @@ export function decodeTriggerDataByTriggerTypeAsJson( return acc; }, {}); } + +export function decodeOffchainTriggerDataByTriggerTypeAsJson( + triggerType: TriggerType, + data: string, +): utils.Result { + const type = triggerTypeToCommandContractTypeMap[triggerType]; + const arr: any[] = decodeOffchainTriggerDataByType(type, data) as any[]; + const offchainDataType = commandOffchainDataTypeJsonMapping[type] + if (!offchainDataType) { + throw new Error(`No offchain data mapping for type ${type}`); + } + return arr.reduce((acc, curr, idx) => { + acc[offchainDataType[idx]] = curr.toString(); + return acc; + }, {}); +} + export function encodeTriggerData( commandAddress: string, network: number, @@ -89,3 +122,17 @@ export function encodeTriggerDataByTriggerType( const paramTypes = getDefinitionForCommandType(commandType); return utils.defaultAbiCoder.encode(paramTypes, values); } + +export function encodeOffchainTriggerDataByType(type: CommandContractType, values: readonly any[]): string { + const paramTypes = getOffchainDataDefinitionForCommandType(type); + return utils.defaultAbiCoder.encode(paramTypes, values); +} + +export function encodeTriggerOffchainDataByTriggerType( + triggerType: TriggerType, + values: readonly any[], +): string { + const commandType = triggerTypeToCommandContractTypeMap[triggerType]; + const paramTypes = getOffchainDataDefinitionForCommandType(commandType); + return utils.defaultAbiCoder.encode(paramTypes, values); +} \ No newline at end of file diff --git a/packages/automation/src/mapping.ts b/packages/automation/src/mapping.ts index 224efc6..84ff23d 100644 --- a/packages/automation/src/mapping.ts +++ b/packages/automation/src/mapping.ts @@ -142,8 +142,28 @@ export const commandTypeJsonMapping: Record = { 'operationName', 'executionLtv', ], + [CommandContractType.DmaAaveTrailingStopLossCommandV2]: [ + 'positionAddress', + 'triggerType', + 'maxCoverage', + 'debtToken', + 'collateralToken', + 'operationName', + 'collateralOracle', + 'collateralAddedRoundId', + 'debtOracle', + 'debtAddedRoundId', + 'trailingDistance', + 'closeToCollateral' + ], +}; +export const commandOffchainDataTypeJsonMapping: Partial> = { + [CommandContractType.DmaAaveTrailingStopLossCommandV2]: [ + 'collateralMaxPriceRoundId', + 'debtClosestPriceRoundId', + 'debtNextPriceRoundId', + ], }; - export const commandAddressMapping: Record< number, Record @@ -266,7 +286,7 @@ export const commandAddressMapping: Record< ]), ); -export const defaultCommandTypeMapping = { +export const defaultCommandTypeMapping: Record = { [CommandContractType.CloseCommand]: ['uint256', 'uint16', 'uint256'], [CommandContractType.SimpleAAVESellCommand]: [ 'address', @@ -379,6 +399,28 @@ export const defaultCommandTypeMapping = { 'bytes32', // operationName 'uint256', // executionLTV ], + [CommandContractType.DmaAaveTrailingStopLossCommandV2]: [ + 'address', //positionAddress + 'uint16', // triggerType + 'uint256', // maxCoverage + 'address', // debtToken + 'address', // collateralToken + 'bytes32', // operationName + 'address', // collateralOracle + 'uint80', // collateralAddedRoundId + 'address', // debtOracle + 'uint80', // debtAddedRoundId + 'uint256', // trailingDistance + 'bool', // closeToCollateral + ], +} as const; + +export const defaultCommandOffchainDataTypeMapping: Partial> = { + [CommandContractType.DmaAaveTrailingStopLossCommandV2]: [ + 'uint80', // collateralMaxPriceRoundId + 'uint80', // debtClosestPriceRoundId + 'uint80', // debtNextPriceRoundId + ], } as const; export function getCommandAddresses(network: number): Record { @@ -415,6 +457,45 @@ export function getDefinitionForCommandType(type: CommandContractType): ParamDef return defaultCommandTypeMapping[type]; } +/** + * Retrieves the offchain data definition for a given command type. + * @param type - The command type. + * @returns The offchain data definition for the command type. + * @throws Error if the command type is unknown. + */ +export function getOffchainDataDefinitionForCommandType(type: Partial): ParamDefinition { + const offchainDataType = defaultCommandOffchainDataTypeMapping[type] + if (!offchainDataType) { + throw new Error( + `Unknown command type ${type}. Supported types: ${Object.keys(defaultCommandOffchainDataTypeMapping).join( + ', ', + )}.`, + ); + } + + return offchainDataType; +} + +/** + * Retrieves the offchain data definition for a given trigger type. + * @param triggerType - The trigger type for which to retrieve the offchain data definition. + * @returns The offchain data definition for the specified trigger type. + * @throws An error if the command type is unknown or not supported. + */ +export function getOffchainDataDefinitionForTriggerType(triggerType: Partial): ParamDefinition { + const type = triggerTypeToCommandContractTypeMap[triggerType]; + const offchainDataType = defaultCommandOffchainDataTypeMapping[type] + if (!offchainDataType) { + throw new Error( + `Unknown command type ${type}. Supported types: ${Object.keys(defaultCommandOffchainDataTypeMapping).join( + ', ', + )}.`, + ); + } + + return offchainDataType; +} + /** * Retrieves the parameter definition for a given trigger type. * @param triggerType The type of trigger. diff --git a/packages/automation/src/types.ts b/packages/automation/src/types.ts index e192b6c..c59edae 100644 --- a/packages/automation/src/types.ts +++ b/packages/automation/src/types.ts @@ -25,6 +25,7 @@ export enum CommandContractType { DmaAaveBasicSellCommandV2 = 'DmaAaveV3BasicSellCommandV2', DmaSparkStopLossCommandV2 = 'DmaSparkStopLossCommandV2', DmaAaveStopLossCommandV2 = 'DmaAaveV3StopLossCommandV2', + DmaAaveTrailingStopLossCommandV2 = 'DmaAaveV3TrailingStopLossCommandV2', } export enum TriggerType { @@ -53,6 +54,7 @@ export enum TriggerType { DmaAaveStopLossToDebtV2 = 124, DmaSparkStopLossToCollateralV2 = 125, DmaSparkStopLossToDebtV2 = 126, + DmaAaveTrailingStopLossV2 = 10001, } export const triggerTypeToCommandContractTypeMap: Record = { @@ -81,6 +83,7 @@ export const triggerTypeToCommandContractTypeMap: Record Date: Tue, 13 Feb 2024 10:19:54 +0100 Subject: [PATCH 2/4] fix: format --- packages/automation/src/abi-coding.ts | 14 ++++++++---- packages/automation/src/mapping.ts | 31 ++++++++++++++++----------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/automation/src/abi-coding.ts b/packages/automation/src/abi-coding.ts index 205fe23..38aa7be 100644 --- a/packages/automation/src/abi-coding.ts +++ b/packages/automation/src/abi-coding.ts @@ -23,7 +23,10 @@ export function decodeTriggerDataByType(type: CommandContractType, data: string) return utils.defaultAbiCoder.decode(paramTypes, data); } -export function decodeOffchainTriggerDataByType(type: CommandContractType, data: string): utils.Result { +export function decodeOffchainTriggerDataByType( + type: CommandContractType, + data: string, +): utils.Result { const paramTypes = getOffchainDataDefinitionForCommandType(type); return utils.defaultAbiCoder.decode(paramTypes, data); } @@ -90,7 +93,7 @@ export function decodeOffchainTriggerDataByTriggerTypeAsJson( ): utils.Result { const type = triggerTypeToCommandContractTypeMap[triggerType]; const arr: any[] = decodeOffchainTriggerDataByType(type, data) as any[]; - const offchainDataType = commandOffchainDataTypeJsonMapping[type] + const offchainDataType = commandOffchainDataTypeJsonMapping[type]; if (!offchainDataType) { throw new Error(`No offchain data mapping for type ${type}`); } @@ -123,7 +126,10 @@ export function encodeTriggerDataByTriggerType( return utils.defaultAbiCoder.encode(paramTypes, values); } -export function encodeOffchainTriggerDataByType(type: CommandContractType, values: readonly any[]): string { +export function encodeOffchainTriggerDataByType( + type: CommandContractType, + values: readonly any[], +): string { const paramTypes = getOffchainDataDefinitionForCommandType(type); return utils.defaultAbiCoder.encode(paramTypes, values); } @@ -135,4 +141,4 @@ export function encodeTriggerOffchainDataByTriggerType( const commandType = triggerTypeToCommandContractTypeMap[triggerType]; const paramTypes = getOffchainDataDefinitionForCommandType(commandType); return utils.defaultAbiCoder.encode(paramTypes, values); -} \ No newline at end of file +} diff --git a/packages/automation/src/mapping.ts b/packages/automation/src/mapping.ts index 84ff23d..10f6b52 100644 --- a/packages/automation/src/mapping.ts +++ b/packages/automation/src/mapping.ts @@ -154,7 +154,7 @@ export const commandTypeJsonMapping: Record = { 'debtOracle', 'debtAddedRoundId', 'trailingDistance', - 'closeToCollateral' + 'closeToCollateral', ], }; export const commandOffchainDataTypeJsonMapping: Partial> = { @@ -415,7 +415,10 @@ export const defaultCommandTypeMapping: Record> = { +export const defaultCommandOffchainDataTypeMapping: Partial> = { [CommandContractType.DmaAaveTrailingStopLossCommandV2]: [ 'uint80', // collateralMaxPriceRoundId 'uint80', // debtClosestPriceRoundId @@ -463,13 +466,15 @@ export function getDefinitionForCommandType(type: CommandContractType): ParamDef * @returns The offchain data definition for the command type. * @throws Error if the command type is unknown. */ -export function getOffchainDataDefinitionForCommandType(type: Partial): ParamDefinition { - const offchainDataType = defaultCommandOffchainDataTypeMapping[type] +export function getOffchainDataDefinitionForCommandType( + type: Partial, +): ParamDefinition { + const offchainDataType = defaultCommandOffchainDataTypeMapping[type]; if (!offchainDataType) { throw new Error( - `Unknown command type ${type}. Supported types: ${Object.keys(defaultCommandOffchainDataTypeMapping).join( - ', ', - )}.`, + `Unknown command type ${type}. Supported types: ${Object.keys( + defaultCommandOffchainDataTypeMapping, + ).join(', ')}.`, ); } @@ -482,14 +487,16 @@ export function getOffchainDataDefinitionForCommandType(type: Partial): ParamDefinition { +export function getOffchainDataDefinitionForTriggerType( + triggerType: Partial, +): ParamDefinition { const type = triggerTypeToCommandContractTypeMap[triggerType]; - const offchainDataType = defaultCommandOffchainDataTypeMapping[type] + const offchainDataType = defaultCommandOffchainDataTypeMapping[type]; if (!offchainDataType) { throw new Error( - `Unknown command type ${type}. Supported types: ${Object.keys(defaultCommandOffchainDataTypeMapping).join( - ', ', - )}.`, + `Unknown command type ${type}. Supported types: ${Object.keys( + defaultCommandOffchainDataTypeMapping, + ).join(', ')}.`, ); } From dd44fa9be781baf70b080cb34986e69640f66ba4 Mon Sep 17 00:00:00 2001 From: halaprix Date: Fri, 16 Feb 2024 11:57:59 +0100 Subject: [PATCH 3/4] chore: bump version --- packages/automation/package.json | 2 +- packages/automation/src/types.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/automation/package.json b/packages/automation/package.json index 1cecde3..5ed5105 100644 --- a/packages/automation/package.json +++ b/packages/automation/package.json @@ -1,7 +1,7 @@ { "name": "@oasisdex/automation", "packageManager": "yarn@1.22.21", - "version": "1.6.0-alpha.10", + "version": "1.6.0-alpha.14", "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/types.ts b/packages/automation/src/types.ts index c59edae..6b4def1 100644 --- a/packages/automation/src/types.ts +++ b/packages/automation/src/types.ts @@ -54,7 +54,7 @@ export enum TriggerType { DmaAaveStopLossToDebtV2 = 124, DmaSparkStopLossToCollateralV2 = 125, DmaSparkStopLossToDebtV2 = 126, - DmaAaveTrailingStopLossV2 = 10001, + DmaAaveTrailingStopLossV2 = 10005, } export const triggerTypeToCommandContractTypeMap: Record = { From 07f1b89149cf742669a802e032b490f4197b944e Mon Sep 17 00:00:00 2001 From: Halaprix Date: Thu, 22 Feb 2024 20:07:06 +0100 Subject: [PATCH 4/4] chore: bump trigger types (#111) --- packages/automation/package.json | 2 +- packages/automation/src/mapping.ts | 3 +++ packages/automation/src/types.ts | 10 +++++----- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/automation/package.json b/packages/automation/package.json index 5ed5105..2f17d63 100644 --- a/packages/automation/package.json +++ b/packages/automation/package.json @@ -1,7 +1,7 @@ { "name": "@oasisdex/automation", "packageManager": "yarn@1.22.21", - "version": "1.6.0-alpha.14", + "version": "1.6.0-alpha.16", "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/mapping.ts b/packages/automation/src/mapping.ts index 10f6b52..2e8d910 100644 --- a/packages/automation/src/mapping.ts +++ b/packages/automation/src/mapping.ts @@ -247,6 +247,9 @@ export const commandAddressMapping: Record< '0x4A13b02ef24B2906a33e48e8F0AaF343C5316327': { type: CommandContractType.DmaAaveBasicSellCommandV2, }, + '0xea0c35bd1c2fae4d540ce30d9738bc55147f2a9c': { + type: CommandContractType.DmaAaveStopLossCommandV2, + }, }, [EthereumNetwork.BASE]: { '0xb7CB13e4cD2D64e739b5746563978Ab7ee36B064': { diff --git a/packages/automation/src/types.ts b/packages/automation/src/types.ts index 6b4def1..5233d5a 100644 --- a/packages/automation/src/types.ts +++ b/packages/automation/src/types.ts @@ -50,11 +50,11 @@ export enum TriggerType { SparkStopLossToDebtV2 = 118, DmaAaveBasicBuyV2 = 121, DmaAaveBasicSellV2 = 122, - DmaAaveStopLossToCollateralV2 = 123, - DmaAaveStopLossToDebtV2 = 124, - DmaSparkStopLossToCollateralV2 = 125, - DmaSparkStopLossToDebtV2 = 126, - DmaAaveTrailingStopLossV2 = 10005, + DmaAaveStopLossToCollateralV2 = 127, + DmaAaveStopLossToDebtV2 = 128, + DmaSparkStopLossToCollateralV2 = 129, + DmaSparkStopLossToDebtV2 = 130, + DmaAaveTrailingStopLossV2 = 10006, } export const triggerTypeToCommandContractTypeMap: Record = {