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

feat: common add aave bs and bb in automation package [sc-12625] #104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/automation/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
41 changes: 35 additions & 6 deletions packages/automation/src/abi-coding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getDefinitionForCommandAddress,
getDefinitionForCommandType,
} from './mapping';
import { CommandContractType } from './types';
import { CommandContractType, TriggerType, triggerTypeToCommandContractTypeMap } from './types';

export function decodeTriggerData(
commandAddress: string,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
}
10 changes: 9 additions & 1 deletion packages/automation/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
119 changes: 101 additions & 18 deletions packages/automation/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
CommandContractType,
EthereumNetwork,
ParamDefinition,
triggerTypeToCommandContractTypeMap,
TriggerType,
} from './types';

export const commandTypeJsonMapping: Record<CommandContractType, string[]> = {
Expand Down Expand Up @@ -96,6 +98,32 @@ export const commandTypeJsonMapping: Record<CommandContractType, string[]> = {
'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<
Expand Down Expand Up @@ -169,6 +197,12 @@ export const commandAddressMapping: Record<
'0x2af43189E85CEA21aa8FA5d61139b771328d8D30': {
type: CommandContractType.SparkStopLossCommandV2,
},
'0x72241841022bc824B0b66e3D27D8937D36dA4FDF': {
type: CommandContractType.AaveBasicBuyCommandV2,
},
'0x31d767f6556CE3fC55d6245C9aEF3575aa64BABf': {
type: CommandContractType.AaveBasicSellCommandV2,
},
},
}).map(([network, mapping]) => [
network,
Expand Down Expand Up @@ -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;

Expand All @@ -267,6 +325,12 @@ export function getCommandAddresses(network: number): Record<CommandContractType
);
}

/**
* Retrieves the parameter definition for a given command type.
* @param type - The command type.
* @returns The parameter definition for the specified command type.
* @throws Error if the command type is unknown.
*/
export function getDefinitionForCommandType(type: CommandContractType): ParamDefinition {
if (!(type in defaultCommandTypeMapping)) {
throw new Error(
Expand All @@ -279,6 +343,25 @@ export function getDefinitionForCommandType(type: CommandContractType): ParamDef
return defaultCommandTypeMapping[type];
}

/**
* Retrieves the parameter definition for a given trigger type.
* @param triggerType The type of trigger.
* @returns The parameter definition for the specified trigger type.
* @throws Error if the command type is unknown.
*/
export function getDefinitionForTriggerType(triggerType: TriggerType): ParamDefinition {
const type = triggerTypeToCommandContractTypeMap[triggerType];
if (!(type in defaultCommandTypeMapping)) {
throw new Error(
`Unknown command type ${type}. Supported types: ${Object.keys(defaultCommandTypeMapping).join(
', ',
)}.`,
);
}

return defaultCommandTypeMapping[type];
}

export function getDefinitionForCommandAddress(address: string, network: number): ParamDefinition {
const info = getCommandContractInfo(address, network);
return info.overwrite ?? getDefinitionForCommandType(info.type);
Expand Down
36 changes: 28 additions & 8 deletions packages/automation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export enum CommandContractType {
MakerBasicBuyCommandV2 = 'MakerBasicBuyCommandV2',
MakerBasicSellCommandV2 = 'MakerBasicSellCommandV2',
SparkStopLossCommandV2 = 'SparkStopLossCommandV2',
AaveBasicBuyCommandV2 = 'AaveBasicBuyCommandV2',
AaveBasicSellCommandV2 = 'AaveBasicSellCommandV2',
}

export enum TriggerType {
Expand All @@ -37,20 +39,38 @@ 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,
AaveBasicSellV2 = 120,
}

export const triggerTypeToCommandContractTypeMap: Record<TriggerType, CommandContractType> = {
[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,
Expand Down
Loading