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

chore: add aaveTrailingStopLoss #110

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion packages/automation/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@oasisdex/automation",
"packageManager": "[email protected]",
"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",
Expand Down
53 changes: 53 additions & 0 deletions packages/automation/src/abi-coding.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { utils } from 'ethers';
import {
commandAddressMapping,
commandOffchainDataTypeJsonMapping,
commandTypeJsonMapping,
getDefinitionForCommandAddress,
getDefinitionForCommandType,
getOffchainDataDefinitionForCommandType,
} from './mapping';
import { CommandContractType, TriggerType, triggerTypeToCommandContractTypeMap } from './types';

Expand All @@ -21,6 +23,14 @@ 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,
Expand All @@ -30,6 +40,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,
Expand Down Expand Up @@ -67,6 +86,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,
Expand All @@ -89,3 +125,20 @@ 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);
}
92 changes: 90 additions & 2 deletions packages/automation/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,28 @@ export const commandTypeJsonMapping: Record<CommandContractType, string[]> = {
'operationName',
'executionLtv',
],
[CommandContractType.DmaAaveTrailingStopLossCommandV2]: [
'positionAddress',
'triggerType',
'maxCoverage',
'debtToken',
'collateralToken',
'operationName',
'collateralOracle',
'collateralAddedRoundId',
'debtOracle',
'debtAddedRoundId',
'trailingDistance',
'closeToCollateral',
],
};
export const commandOffchainDataTypeJsonMapping: Partial<Record<CommandContractType, string[]>> = {
[CommandContractType.DmaAaveTrailingStopLossCommandV2]: [
'collateralMaxPriceRoundId',
'debtClosestPriceRoundId',
'debtNextPriceRoundId',
],
};

export const commandAddressMapping: Record<
number,
Record<string, CommandContractInfo>
Expand Down Expand Up @@ -266,7 +286,7 @@ export const commandAddressMapping: Record<
]),
);

export const defaultCommandTypeMapping = {
export const defaultCommandTypeMapping: Record<CommandContractType, ParamDefinition> = {
[CommandContractType.CloseCommand]: ['uint256', 'uint16', 'uint256'],
[CommandContractType.SimpleAAVESellCommand]: [
'address',
Expand Down Expand Up @@ -379,6 +399,31 @@ 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<Record<
CommandContractType,
ParamDefinition
>> = {
[CommandContractType.DmaAaveTrailingStopLossCommandV2]: [
'uint80', // collateralMaxPriceRoundId
'uint80', // debtClosestPriceRoundId
'uint80', // debtNextPriceRoundId
],
} as const;

export function getCommandAddresses(network: number): Record<CommandContractType, string[]> {
Expand Down Expand Up @@ -415,6 +460,49 @@ 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<CommandContractType>,
): 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<TriggerType>,
): 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.
Expand Down
3 changes: 3 additions & 0 deletions packages/automation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export enum CommandContractType {
DmaAaveBasicSellCommandV2 = 'DmaAaveV3BasicSellCommandV2',
DmaSparkStopLossCommandV2 = 'DmaSparkStopLossCommandV2',
DmaAaveStopLossCommandV2 = 'DmaAaveV3StopLossCommandV2',
DmaAaveTrailingStopLossCommandV2 = 'DmaAaveV3TrailingStopLossCommandV2',
}

export enum TriggerType {
Expand Down Expand Up @@ -53,6 +54,7 @@ export enum TriggerType {
DmaAaveStopLossToDebtV2 = 124,
DmaSparkStopLossToCollateralV2 = 125,
DmaSparkStopLossToDebtV2 = 126,
DmaAaveTrailingStopLossV2 = 10001,
}

export const triggerTypeToCommandContractTypeMap: Record<TriggerType, CommandContractType> = {
Expand Down Expand Up @@ -81,6 +83,7 @@ export const triggerTypeToCommandContractTypeMap: Record<TriggerType, CommandCon
[TriggerType.DmaAaveStopLossToDebtV2]: CommandContractType.DmaAaveStopLossCommandV2,
[TriggerType.DmaSparkStopLossToCollateralV2]: CommandContractType.DmaSparkStopLossCommandV2,
[TriggerType.DmaSparkStopLossToDebtV2]: CommandContractType.DmaSparkStopLossCommandV2,
[TriggerType.DmaAaveTrailingStopLossV2]: CommandContractType.DmaAaveTrailingStopLossCommandV2,
};

export enum TriggerGroupType {
Expand Down
Loading