From 0d73b0e0de33fef99c558bb0399bcd69ea65d033 Mon Sep 17 00:00:00 2001 From: halaprix Date: Tue, 7 Nov 2023 11:51:06 +0100 Subject: [PATCH 1/4] feat: common add aave bs and bb in automation package [sc-12625] --- packages/automation/src/mapping.ts | 82 +++++++++++++++++++++++------- packages/automation/src/types.ts | 12 ++--- 2 files changed, 68 insertions(+), 26 deletions(-) diff --git a/packages/automation/src/mapping.ts b/packages/automation/src/mapping.ts index 9c01abb..d48e073 100644 --- a/packages/automation/src/mapping.ts +++ b/packages/automation/src/mapping.ts @@ -96,6 +96,30 @@ export const commandTypeJsonMapping: Record = { 'executionPrice', 'maxBaseFeeInGwei', ], + [CommandContractType.AaveBasicSellCommandV2]: [ + 'positionAddress', + 'triggerType', + 'maxCoverage', + 'debtToken', + 'collateralToken', + 'execCollRatio', + 'targetCollRatio', + 'minSellPrice', + 'deviation', + 'maxBaseFeeInGwei', + ], + [CommandContractType.AaveBasicBuyCommandV2]: [ + 'positionAddress', + 'triggerType', + 'maxCoverage', + 'debtToken', + 'collateralToken', + 'execCollRatio', + 'targetCollRatio', + 'maxBuyPrice', + 'deviation', + 'maxBaseFeeInGwei', + ], }; export const commandAddressMapping: Record< @@ -228,26 +252,48 @@ 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 + 'uint256', // execCollRatio + 'uint256', // targetCollRatio + 'uint256', // maxBuyPrice + 'uint64', // deviation + 'uint32', // maxBaseFeeInGwei + ], + [CommandContractType.AaveBasicSellCommandV2]: [ + 'address', //positionAddress + 'uint16', // triggerType + 'uint256', // maxCoverage + 'address', // debtToken + 'address', // collateralToken + 'uint256', // execCollRatio + 'uint256', // targetCollRatio + 'uint256', // minSellPrice + 'uint64', // deviation + 'uint32', // maxBaseFeeInGwei ], } as const; diff --git a/packages/automation/src/types.ts b/packages/automation/src/types.ts index ed7a6c9..dde2778 100644 --- a/packages/automation/src/types.ts +++ b/packages/automation/src/types.ts @@ -19,6 +19,8 @@ export enum CommandContractType { MakerBasicBuyCommandV2 = 'MakerBasicBuyCommandV2', MakerBasicSellCommandV2 = 'MakerBasicSellCommandV2', SparkStopLossCommandV2 = 'SparkStopLossCommandV2', + AaveBasicBuyCommandV2 = 'AaveBasicBuyCommandV2', + AaveBasicSellCommandV2 = 'AaveBasicSellCommandV2', } export enum TriggerType { @@ -37,18 +39,12 @@ export enum TriggerType { MakerBasicSellV2 = 104, MakerAutoTakeProfitToCollateralV2 = 105, MakerAutoTakeProfitToDaiV2 = 106, - // AaveStopLossToCollateralV2 = 107, - // AaveStopLossToDebtV2 = 108, - // AaveStopLossToCollateralV2 = 109, - // AaveStopLossToDebtV2 = 110, AaveStopLossToCollateralV2 = 111, AaveStopLossToDebtV2 = 112, - // SparkStopLossToCollateralV2 = 113, - // SparkStopLossToDebtV2 = 114, - // SparkStopLossToCollateralV2 = 115, - // SparkStopLossToDebtV2 = 116, SparkStopLossToCollateralV2 = 117, SparkStopLossToDebtV2 = 118, + AaveBasicBuyV2 = 119, + AaceBasicSellV2 = 120, } export enum TriggerGroupType { From a7ddb70276f577784111ec8721123ee587dca3f7 Mon Sep 17 00:00:00 2001 From: Halaprix Date: Thu, 23 Nov 2023 16:01:59 +0100 Subject: [PATCH 2/4] feat: add new decoding/encoding methods (#105) * feat: add new decoding/encoding methods * feat: missing action --- packages/automation/package.json | 2 +- packages/automation/src/abi-coding.ts | 41 +++++++++++++--- packages/automation/src/index.ts | 10 +++- packages/automation/src/mapping.ts | 39 +++++++++++++-- packages/automation/src/types.ts | 26 +++++++++- packages/automation/test/abi-coding.test.ts | 54 +++++++++++++++++++-- 6 files changed, 154 insertions(+), 18 deletions(-) diff --git a/packages/automation/package.json b/packages/automation/package.json index 4062eca..8514ee0 100644 --- a/packages/automation/package.json +++ b/packages/automation/package.json @@ -1,6 +1,6 @@ { "name": "@oasisdex/automation", - "version": "1.5.8", + "version": "1.5.9-alpha4", "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 d48e073..1ef6392 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 = { @@ -102,8 +104,9 @@ export const commandTypeJsonMapping: Record = { 'maxCoverage', 'debtToken', 'collateralToken', - 'execCollRatio', - 'targetCollRatio', + 'opHash', + 'execLtv', + 'targetLtv', 'minSellPrice', 'deviation', 'maxBaseFeeInGwei', @@ -114,8 +117,9 @@ export const commandTypeJsonMapping: Record = { 'maxCoverage', 'debtToken', 'collateralToken', - 'execCollRatio', - 'targetCollRatio', + 'opHash', + 'execLtv', + 'targetLtv', 'maxBuyPrice', 'deviation', 'maxBaseFeeInGwei', @@ -277,6 +281,7 @@ export const defaultCommandTypeMapping = { 'uint256', // maxCoverage 'address', // debtToken 'address', // collateralToken + 'bytes32', // opHash 'uint256', // execCollRatio 'uint256', // targetCollRatio 'uint256', // maxBuyPrice @@ -289,6 +294,7 @@ export const defaultCommandTypeMapping = { 'uint256', // maxCoverage 'address', // debtToken 'address', // collateralToken + 'bytes32', // opHash 'uint256', // execCollRatio 'uint256', // targetCollRatio 'uint256', // minSellPrice @@ -313,6 +319,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({ From dc6a20b6e14f2c3ae700983092732c26bc5b27ac Mon Sep 17 00:00:00 2001 From: Jakub Swierczek Date: Thu, 21 Dec 2023 17:38:20 +0100 Subject: [PATCH 3/4] v1.5.9-alpha5 --- packages/automation/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/automation/package.json b/packages/automation/package.json index 8514ee0..35d85e3 100644 --- a/packages/automation/package.json +++ b/packages/automation/package.json @@ -1,6 +1,7 @@ { "name": "@oasisdex/automation", - "version": "1.5.9-alpha4", + "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", From 5898a2640e6056e2ffc6a5367cae59feef3925cf Mon Sep 17 00:00:00 2001 From: Jakub Swierczek Date: Thu, 21 Dec 2023 17:48:39 +0100 Subject: [PATCH 4/4] new version. --- package.json | 1 + packages/automation/src/mapping.ts | 6 ++++++ 2 files changed, 7 insertions(+) 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/src/mapping.ts b/packages/automation/src/mapping.ts index 1ef6392..5885d7f 100644 --- a/packages/automation/src/mapping.ts +++ b/packages/automation/src/mapping.ts @@ -197,6 +197,12 @@ export const commandAddressMapping: Record< '0x2af43189E85CEA21aa8FA5d61139b771328d8D30': { type: CommandContractType.SparkStopLossCommandV2, }, + '0x72241841022bc824B0b66e3D27D8937D36dA4FDF': { + type: CommandContractType.AaveBasicBuyCommandV2, + }, + '0x31d767f6556CE3fC55d6245C9aEF3575aa64BABf': { + type: CommandContractType.AaveBasicSellCommandV2, + }, }, }).map(([network, mapping]) => [ network,