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: add new decoding/encoding methods #105

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
2 changes: 1 addition & 1 deletion packages/automation/package.json
Original file line number Diff line number Diff line change
@@ -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",
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';
39 changes: 35 additions & 4 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 @@ -102,8 +104,9 @@ export const commandTypeJsonMapping: Record<CommandContractType, string[]> = {
'maxCoverage',
'debtToken',
'collateralToken',
'execCollRatio',
'targetCollRatio',
'opHash',
'execLtv',
'targetLtv',
'minSellPrice',
'deviation',
'maxBaseFeeInGwei',
Expand All @@ -114,8 +117,9 @@ export const commandTypeJsonMapping: Record<CommandContractType, string[]> = {
'maxCoverage',
'debtToken',
'collateralToken',
'execCollRatio',
'targetCollRatio',
'opHash',
'execLtv',
'targetLtv',
'maxBuyPrice',
'deviation',
'maxBaseFeeInGwei',
Expand Down Expand Up @@ -277,6 +281,7 @@ export const defaultCommandTypeMapping = {
'uint256', // maxCoverage
'address', // debtToken
'address', // collateralToken
'bytes32', // opHash
'uint256', // execCollRatio
'uint256', // targetCollRatio
'uint256', // maxBuyPrice
Expand All @@ -289,6 +294,7 @@ export const defaultCommandTypeMapping = {
'uint256', // maxCoverage
'address', // debtToken
'address', // collateralToken
'bytes32', // opHash
'uint256', // execCollRatio
'uint256', // targetCollRatio
'uint256', // minSellPrice
Expand All @@ -313,6 +319,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 @@ -325,6 +337,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
26 changes: 25 additions & 1 deletion packages/automation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,33 @@ export enum TriggerType {
SparkStopLossToCollateralV2 = 117,
SparkStopLossToDebtV2 = 118,
AaveBasicBuyV2 = 119,
AaceBasicSellV2 = 120,
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
54 changes: 49 additions & 5 deletions packages/automation/test/abi-coding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { BigNumber as EthersBN, constants, utils } from 'ethers';
import {
decodeTriggerData,
decodeTriggerDataAsJson,
decodeTriggerDataByTriggerType,
decodeTriggerDataByTriggerTypeAsJson,
decodeTriggerDataByType,
decodeTriggerDataByTypeAsJson,
encodeTriggerData,
encodeTriggerDataByTriggerType,
encodeTriggerDataByType,
} from '../src/abi-coding';
import {
commandAddressMapping,
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;
Expand All @@ -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);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down