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

Morpho Blue automations #209

Merged
merged 16 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
194 changes: 166 additions & 28 deletions packages/triggers-shared/src/contracts/get-triggers-response.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ProtocolId } from '@summerfi/serverless-shared'
import { Price, TokenBalance } from '../types'

export const AaveStopLossToCollateralV2ID = 111n as const
Expand Down Expand Up @@ -27,6 +28,12 @@ export const DmaSparkPartialTakeProfitID = 134n as const
export const DmaAaveTrailingStopLossID = 10006n as const
export const DmaSparkTrailingStopLossID = 10007n as const

export const MorphoBlueBasicBuyV2ID = 111111n as const
export const MorphoBlueBasicSellV2ID = 111111n as const
export const MorphoBluePartialTakeProfitID = 111111n as const
export const MorphoBlueTrailingStopLossID = 111111n as const
export const MorphoBlueStopLossV2ID = 111111n as const

export type Trigger = {
triggerId: string
triggerData: string
Expand Down Expand Up @@ -317,44 +324,168 @@ export type DmaSparkPartialTakeProfit = Trigger & {
}
}

export type MorphoBlueBasicBuy = Trigger & {
triggerTypeName: 'MorphoBlueBasicBuyV2'
triggerType: typeof MorphoBlueBasicBuyV2ID
decodedParams: {
positionAddress: string
triggerType: string
maxCoverage: string
debtToken: string
collateralToken: string
operationName: string
executionLtv: string
targetLtv: string
maxBuyPrice: string
deviation: string
maxBaseFeeInGwei: string
}
}

export type MorphoBlueBasicSell = Trigger & {
triggerTypeName: 'MorphoBlueBasicSellV2'
triggerType: typeof MorphoBlueBasicSellV2ID
decodedParams: {
positionAddress: string
triggerType: string
maxCoverage: string
debtToken: string
collateralToken: string
operationName: string
executionLtv: string
targetLtv: string
minSellPrice: string
deviation: string
maxBaseFeeInGwei: string
}
}

export type MorphoBluePartialTakeProfit = Trigger & {
triggerTypeName: 'MorphoBluePartialTakeProfit'
triggerType: typeof MorphoBluePartialTakeProfitID
decodedParams: {
positionAddress: string
triggerType: string
maxCoverage: string
debtToken: string
collateralToken: string
operationName: string
executionLtv: string
targetLtv: string
executionPrice: string
deviation: string
withdrawToDebt: string
}
dynamicParams?: {
nextProfit: {
triggerPrice: Price
realizedProfitInCollateral: TokenBalance
realizedProfitInDebt: TokenBalance
totalProfitInCollateral: TokenBalance
totalProfitInDebt: TokenBalance
stopLossDynamicPrice?: Price
fee: TokenBalance
totalFee: TokenBalance
}
}
}

export type MorphoBlueTrailingStopLoss = Trigger & {
triggerTypeName: 'MorphoBlueTrailingStopLoss'
triggerType: typeof MorphoBlueTrailingStopLossID
decodedParams: {
positionAddress: string
triggerType: string
maxCoverage: string
debtToken: string
collateralToken: string
operationName: string
collateralOracle: string
collateralAddedRoundId: string
debtOracle: string
debtAddedRoundId: string
trailingDistance: string
closeToCollateral: string
}
dynamicParams: {
executionPrice?: string
originalExecutionPrice?: string
}
}

export type MorphoBlueStopLoss = Trigger & {
triggerTypeName: 'MorphoBlueStopLossV2'
triggerType: typeof MorphoBlueStopLossV2ID
decodedParams: {
positionAddress: string
triggerType: string
maxCoverage: string
debtToken: string
collateralToken: string
executionLtv: string
ltv: string
}
}

export type GetTriggersResponse = {
triggers: {
aaveStopLossToCollateral?: AaveStopLossToCollateral
aaveStopLossToCollateralDMA?: AaveStopLossToCollateralDMA
aaveStopLossToDebt?: AaveStopLossToDebt
aaveStopLossToDebtDMA?: AaveStopLossToDebtDMA
sparkStopLossToCollateral?: SparkStopLossToCollateral
sparkStopLossToCollateralDMA?: SparkStopLossToCollateralDMA
sparkStopLossToDebt?: SparkStopLossToDebt
sparkStopLossToDebtDMA?: SparkStopLossToDebtDMA
aaveBasicBuy?: DmaAaveBasicBuy
aaveBasicSell?: DmaAaveBasicSell
sparkBasicBuy?: DmaSparkBasicBuy
sparkBasicSell?: DmaSparkBasicSell
aaveTrailingStopLossDMA?: DmaAaveTrailingStopLoss
sparkTrailingStopLossDMA?: DmaSparkTrailingStopLoss
aavePartialTakeProfit?: DmaAavePartialTakeProfit
sparkPartialTakeProfit?: DmaSparkPartialTakeProfit
[ProtocolId.AAVE_V3]: {
aaveStopLossToCollateral?: AaveStopLossToCollateral
aaveStopLossToCollateralDMA?: AaveStopLossToCollateralDMA
aaveStopLossToDebt?: AaveStopLossToDebt
aaveStopLossToDebtDMA?: AaveStopLossToDebtDMA
aaveBasicBuy?: DmaAaveBasicBuy
aaveBasicSell?: DmaAaveBasicSell
aaveTrailingStopLossDMA?: DmaAaveTrailingStopLoss
aavePartialTakeProfit?: DmaAavePartialTakeProfit
}
[ProtocolId.SPARK]: {
sparkStopLossToCollateral?: SparkStopLossToCollateral
sparkStopLossToCollateralDMA?: SparkStopLossToCollateralDMA
sparkStopLossToDebt?: SparkStopLossToDebt
sparkStopLossToDebtDMA?: SparkStopLossToDebtDMA
sparkBasicBuy?: DmaSparkBasicBuy
sparkBasicSell?: DmaSparkBasicSell
sparkTrailingStopLossDMA?: DmaSparkTrailingStopLoss
sparkPartialTakeProfit?: DmaSparkPartialTakeProfit
}
[ProtocolId.MORPHO_BLUE]: {
[key: `0x${string}`]: {
stopLoss?: MorphoBlueStopLoss
basicBuy?: MorphoBlueBasicBuy
basicSell?: MorphoBlueBasicSell
trailingStopLoss?: MorphoBlueTrailingStopLoss
partialTakeProfit?: MorphoBluePartialTakeProfit
}
}
}
flags: {
isAaveStopLossEnabled: boolean
isSparkStopLossEnabled: boolean
isAaveBasicBuyEnabled: boolean
isAaveBasicSellEnabled: boolean
isAavePartialTakeProfitEnabled: boolean
isAaveStopLossEnabled: boolean
isSparkBasicBuyEnabled: boolean
isSparkBasicSellEnabled: boolean
isAavePartialTakeProfitEnabled: boolean
isSparkPartialTakeProfitEnabled: boolean
isSparkStopLossEnabled: boolean
isMorphoBlueBasicBuyEnabled: boolean
isMorphoBlueBasicSellEnabled: boolean
isMorphoBluePartialTakeProfitEnabled: boolean
isMorphoBlueStopLossEnabled: boolean
}
triggerGroup: {
aaveStopLoss?: Trigger
sparkStopLoss?: Trigger
aaveBasicBuy?: Trigger
aaveBasicSell?: Trigger
aavePartialTakeProfit?: Trigger
aaveStopLoss?: Trigger
sparkBasicBuy?: Trigger
sparkBasicSell?: Trigger
aavePartialTakeProfit?: Trigger
sparkPartialTakeProfit?: Trigger
sparkStopLoss?: Trigger
morphoBlueBasicBuy?: Trigger
morphoBlueBasicSell?: Trigger
morphoBluePartialTakeProfit?: Trigger
morphoBlueStopLoss?: Trigger
}
triggersCount: number
additionalData?: Record<string, unknown>
Expand All @@ -363,20 +494,27 @@ export type GetTriggersResponse = {
export type AllDecodedParamsKeys =
| keyof AaveStopLossToCollateral['decodedParams']
| keyof AaveStopLossToDebt['decodedParams']
| keyof SparkStopLossToCollateral['decodedParams']
| keyof SparkStopLossToDebt['decodedParams']
| keyof DmaAaveBasicBuy['decodedParams']
| keyof DmaAaveBasicSell['decodedParams']
| keyof DmaAaveTrailingStopLoss['decodedParams']
| keyof DmaAaveTrailingStopLoss['dynamicParams']
| keyof DmaAavePartialTakeProfit['decodedParams']
| keyof DmaAavePartialTakeProfit['dynamicParams']
| keyof DmaAaveTrailingStopLoss['decodedParams']
| keyof DmaAaveTrailingStopLoss['dynamicParams']
| keyof DmaSparkBasicBuy['decodedParams']
| keyof DmaSparkBasicSell['decodedParams']
| keyof DmaSparkTrailingStopLoss['decodedParams']
| keyof DmaSparkTrailingStopLoss['dynamicParams']
| keyof DmaSparkPartialTakeProfit['decodedParams']
| keyof DmaSparkPartialTakeProfit['dynamicParams']
| keyof DmaSparkTrailingStopLoss['decodedParams']
| keyof DmaSparkTrailingStopLoss['dynamicParams']
| keyof SparkStopLossToCollateral['decodedParams']
| keyof SparkStopLossToDebt['decodedParams']
| keyof MorphoBlueBasicBuy['decodedParams']
| keyof MorphoBlueBasicSell['decodedParams']
| keyof MorphoBluePartialTakeProfit['decodedParams']
| keyof MorphoBluePartialTakeProfit['dynamicParams']
| keyof MorphoBlueTrailingStopLoss['decodedParams']
| keyof MorphoBlueTrailingStopLoss['dynamicParams']
| keyof MorphoBlueStopLoss['decodedParams']

export const getPropertyFromTriggerParams = ({
trigger,
Expand Down
58 changes: 41 additions & 17 deletions summerfi-api/get-triggers-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {
getDmaAaveTrailingStopLoss,
getDmaSparkTrailingStopLoss,
} from './trigger-parsers'
import { ChainId, getRpcGatewayEndpoint, IRpcConfig } from '@summerfi/serverless-shared'
import { ChainId, getRpcGatewayEndpoint, IRpcConfig, ProtocolId } from '@summerfi/serverless-shared'
import { getAddresses } from '@summerfi/triggers-shared'

const logger = new Logger({ serviceName: 'getTriggersFunction' })
Expand Down Expand Up @@ -373,22 +373,46 @@ export const handler = async (

const response: GetTriggersResponse = {
triggers: {
aaveStopLossToCollateral,
aaveStopLossToCollateralDMA,
aaveStopLossToDebt,
aaveStopLossToDebtDMA,
sparkStopLossToCollateral,
sparkStopLossToCollateralDMA,
sparkStopLossToDebt,
sparkStopLossToDebtDMA,
aaveBasicBuy,
aaveBasicSell,
aaveTrailingStopLossDMA,
sparkBasicSell,
sparkBasicBuy,
sparkTrailingStopLossDMA,
aavePartialTakeProfit,
sparkPartialTakeProfit,
[ProtocolId.AAVE_V3]: {
aaveStopLossToCollateral,
aaveStopLossToCollateralDMA,
aaveStopLossToDebt,
aaveStopLossToDebtDMA,
aaveBasicBuy,
aaveBasicSell,
aaveTrailingStopLossDMA,
aavePartialTakeProfit,
},
[ProtocolId.SPARK]: {
sparkStopLossToCollateral,
sparkStopLossToCollateralDMA,
sparkStopLossToDebt,
sparkStopLossToDebtDMA,
sparkBasicSell,
sparkBasicBuy,
sparkTrailingStopLossDMA,
sparkPartialTakeProfit,
},
[ProtocolId.MORPHO_BLUE]: {
['0xtest']: {
stopLoss: {
triggerTypeName: 'MorphoBlueStopLossV2',
triggerType: 111111n,
triggerId: '10000000319',
triggerData:
'0x000000000000000000000000d0281cc68cdbf77d49d9a8a7691a8d53e30869d9000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000596a35b7000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda029130000000000000000000000004200000000000000000000000000000000000006436c6f7365414156455633506f736974696f6e5f3400000000000000000000000000000000000000000000000000000000000000000000000000000000001d7a',
decodedParams: {
positionAddress: '0xd0281cc68cdbf77d49d9a8a7691a8d53e30869d9',
triggerType: '128',
maxCoverage: '1500132791',
executionLtv: '7546',
debtToken: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
collateralToken: '0x4200000000000000000000000000000000000006',
operationName: '0x436c6f7365414156455633506f736974696f6e5f340000000000000000000000',
},
},
},
},
},
flags: {
isAaveStopLossEnabled: hasAnyDefined(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const upsertErrorsValidation = paramsSchema
)
.refine(
({ triggers, triggerData }) => {
const autoSellTrigger = triggers.triggers.aaveBasicSell
const autoSellTrigger = triggers.triggers['aave-v3'].aaveBasicSell
if (!autoSellTrigger) {
return true
}
Expand All @@ -110,7 +110,7 @@ const upsertErrorsValidation = paramsSchema
.refine(
({ triggers, action }) => {
if (action === SupportedActions.Add) {
return triggers.triggers.aaveBasicBuy === undefined
return triggers.triggers['aave-v3'].aaveBasicBuy === undefined
}
return true
},
Expand All @@ -123,7 +123,8 @@ const upsertErrorsValidation = paramsSchema
)
.refine(
({ triggers, action }) => {
if (action === SupportedActions.Update) return triggers.triggers.aaveBasicBuy !== undefined
if (action === SupportedActions.Update)
return triggers.triggers['aave-v3'].aaveBasicBuy !== undefined
return true
},
{
Expand All @@ -150,7 +151,7 @@ const upsertErrorsValidation = paramsSchema
)
.refine(
({ triggerData, triggers }) => {
const partialTakeProfit = triggers.triggers.aavePartialTakeProfit
const partialTakeProfit = triggers.triggers['aave-v3'].aavePartialTakeProfit
if (!partialTakeProfit) {
return true
}
Expand All @@ -173,7 +174,8 @@ const upsertErrorsValidation = paramsSchema

const deleteErrorsValidation = paramsSchema.refine(
({ triggers, action }) => {
if (action === SupportedActions.Remove) return triggers.triggers.aaveBasicBuy !== undefined
if (action === SupportedActions.Remove)
return triggers.triggers['aave-v3'].aaveBasicBuy !== undefined
return true
},
{
Expand All @@ -198,7 +200,7 @@ const warningsValidation = paramsSchema
)
.refine(
({ triggerData, triggers }) => {
const autoSellTrigger = triggers.triggers.aaveBasicSell
const autoSellTrigger = triggers.triggers['aave-v3'].aaveBasicSell
if (!autoSellTrigger) {
return true
}
Expand Down
Loading
Loading