Skip to content

Commit

Permalink
feat: Maker support for get-triggers (#496)
Browse files Browse the repository at this point in the history
## Description
This pull request adds support for Maker automation triggers. It's a
part of adding omni kit support to Maker in oasis borrow.

## Changes
- mapped all maker triggers
- added two types of mapping helpers:
- - simple trigger for triggers which are just mapped from the subgraph
data
- - advanced trigger for triggers that need more data (async)
- extracted `mapResponse` function, because the main file became too
large
- query string params are changed:
- -`dpm` is deprecated in favor of `account` (with backwards
compatibility)
- - this is because `account` field in subgraph works both for ds proxy
and dpm
- extracted `logger` to make sure it's one instance
- added `trigger group id` which is used for `auto buy` + `auto sell` to
combine into `constant multiple`

## Testing
```
/api/triggers?chainid=1&account=[ds proxy address]
```

## Next steps
- Add support to create triggers.


Please review and provide any feedback or suggestions for improvement.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Release Notes

- **New Features**
- Enhanced GraphQL query for triggers, now includes additional fields
and improved filtering by account.
- Introduced new trigger types and constants for Maker protocol,
expanding capabilities for automated trading strategies.
- Added a new field `triggerGroupId` to the GraphQL schema for better
categorization of triggers.
- Implemented functions for advanced trigger management and filtering,
including support for Maker vault information.

- **Bug Fixes**
- Updated parameter handling to maintain backward compatibility with
existing clients.

- **Refactor**
- Streamlined code structure by modularizing trigger processing logic,
improving maintainability and readability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
marcinciarka authored Sep 26, 2024
1 parent 0a8399c commit e934a60
Show file tree
Hide file tree
Showing 15 changed files with 1,255 additions and 527 deletions.
18 changes: 16 additions & 2 deletions packages/automation-subgraph/queries/triggers.query.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
query Triggers($dpm: Bytes!) {
query Triggers($account: Bytes!) {
triggers(
where: { account: $dpm, removedBlock: null }
where: { account: $account, removedBlock: null }
orderBy: addedTimestamp
orderDirection: desc
) {
Expand All @@ -12,7 +12,21 @@ query Triggers($dpm: Bytes!) {
triggerType
addedTimestamp
decodedData
triggerGroupId
decodedDataNames
cdp {
id
collateralToken {
address
symbol
decimals
}
urn {
id
}
ilk
ilkName
}
tokens {
address
symbol
Expand Down
1 change: 1 addition & 0 deletions packages/automation-subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ type Trigger {
where: Token_filter
): [Token!]!
triggerData: Bytes!
triggerGroupId: BigInt
triggerType: BigInt!
version: BigInt!
}
Expand Down
6 changes: 3 additions & 3 deletions packages/automation-subgraph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface SubgraphClientConfig {
}

export interface GetTriggersParams {
dpm: string
account: string
poolId?: string
}

Expand All @@ -41,10 +41,10 @@ export type GetOneTrigger = (params: GetOneTriggerParams) => Promise<OneTrigger
async function getTriggers(params: GetTriggersParams, config: SubgraphClientConfig) {
const url = getEndpoint(config.chainId, config.urlBase)
const triggers = await request(url, TriggersDocument, {
dpm: params.dpm,
account: params.account,
})

config.logger?.debug('Received triggers for account', { account: params.dpm, triggers })
config.logger?.debug('Received triggers for account', { account: params.account, triggers })

return triggers
}
Expand Down
118 changes: 118 additions & 0 deletions packages/triggers-shared/src/contracts/get-triggers-response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { ProtocolId } from '@summerfi/serverless-shared'
import { Price, TokenBalance } from '../types'

// maker legacy ids
export const MakerStopLossToCollateralID = 1n as const
export const MakerStopLossToDaiID = 2n as const
export const MakerBasicBuyID = 3n as const
export const MakerBasicSellID = 4n as const
export const MakerAutoTakeProfitToCollateralID = 7n as const
export const MakerAutoTakeProfitToDaiID = 8n as const

// maker legacy ids that were supposed to happen but never did
// export const MakerStopLossToCollateralV2ID = 101n as const
// export const MakerStopLossToDaiV2ID = 102n as const
// export const MakerBasicBuyV2ID = 103n as const
// export const MakerBasicSellV2ID = 104n as const
// export const MakerAutoTakeProfitToCollateralV2ID = 105n as const
// export const MakerAutoTakeProfitToDaiV2ID = 106n as const

export const AaveStopLossToCollateralV2ID = 111n as const
export const AaveStopLossToDebtV2ID = 112n as const
export const SparkStopLossToCollateralV2ID = 117n as const
Expand Down Expand Up @@ -42,6 +58,90 @@ export type Trigger = {
dynamicParams?: unknown
}

export type MakerStopLossToCollateral = Trigger & {
triggerTypeName: 'MakerStopLossToCollateral'
triggerType: typeof MakerStopLossToCollateralID
decodedParams: {
cdpId: string
collRatio: string
positionAddress: string
triggerType: string
maxCoverage: string
debtToken: string
collateralToken: string
executionLtv: string
ltv?: string
}
}
export type MakerStopLossToDai = Trigger & {
triggerTypeName: 'MakerStopLossToDai'
triggerType: typeof MakerStopLossToDaiID
decodedParams: {
cdpId: string
collRatio: string
positionAddress: string
triggerType: string
maxCoverage: string
debtToken: string
collateralToken: string
executionLtv: string
ltv?: string
}
}
export type MakerBasicBuy = Trigger & {
triggerTypeName: 'MakerBasicBuy'
triggerType: typeof MakerBasicBuyID
triggerGroupId?: string
decodedParams: {
cdpId: string
triggerType: string
executionLtv: string
targetLtv: string
maxBuyPrice: string
continuous: boolean
deviation: string
maxBaseFeeInGwei: string
}
}

export type MakerBasicSell = Trigger & {
triggerTypeName: 'MakerBasicSell'
triggerType: typeof MakerBasicSellID
triggerGroupId?: string
decodedParams: {
cdpId: string
triggerType: string
executionLtv: string
targetLtv: string
minSellPrice: string
continuous: boolean
deviation: string
maxBaseFeeInGwei: string
}
}

export type MakerAutoTakeProfitToCollateral = Trigger & {
triggerTypeName: 'MakerAutoTakeProfitToCollateral'
triggerType: typeof MakerAutoTakeProfitToCollateralID
triggerGroupId?: string
decodedParams: {
cdpId: string
triggerType: string
executionPrice: string
maxBaseFeeInGwei: string
}
}
export type MakerAutoTakeProfitToDebt = Trigger & {
triggerTypeName: 'MakerAutoTakeProfitToDai'
triggerType: typeof MakerAutoTakeProfitToDaiID
triggerGroupId?: string
decodedParams: {
cdpId: string
triggerType: string
executionPrice: string
maxBaseFeeInGwei: string
}
}
export type AaveStopLossToCollateral = Trigger & {
triggerTypeName: 'AaveStopLossToCollateralV2'
triggerType: typeof AaveStopLossToCollateralV2ID
Expand Down Expand Up @@ -429,6 +529,14 @@ export type MorphoBlueStopLoss = Trigger & {

export type GetTriggersResponse = {
triggers: {
[ProtocolId.MAKER]: {
basicBuy?: MakerBasicBuy
basicSell?: MakerBasicSell
stopLossToCollateral?: MakerStopLossToCollateral
stopLossToDebt?: MakerStopLossToDai
autoTakeProfitToCollateral?: MakerAutoTakeProfitToCollateral
autoTakeProfitToDebt?: MakerAutoTakeProfitToDebt
}
[ProtocolId.AAVE3]: {
basicBuy?: DmaAaveBasicBuy
basicSell?: DmaAaveBasicSell
Expand Down Expand Up @@ -490,6 +598,12 @@ export type GetTriggersResponse = {
sparkPartialTakeProfit?: DmaSparkPartialTakeProfit
}
flags: {
[ProtocolId.MAKER]: {
isBasicBuyEnabled: boolean
isBasicSellEnabled: boolean
isStopLossEnabled: boolean
isAutoTakeProfitEnabled: boolean
}
[ProtocolId.AAVE3]: {
isBasicBuyEnabled: boolean
isBasicSellEnabled: boolean
Expand Down Expand Up @@ -541,6 +655,10 @@ export type GetTriggersResponse = {
morphoBlueBasicSell?: Trigger
morphoBluePartialTakeProfit?: Trigger
morphoBlueStopLoss?: Trigger
makerStopLoss?: Trigger
makerBasicBuy?: Trigger
makerBasicSell?: Trigger
makerAutoTakeProfit?: Trigger
}
triggersCount: number
additionalData?: Record<string, unknown>
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions summerfi-api/get-triggers-function/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@aws-lambda-powertools/logger": "^2.0.4",
"@aws-lambda-powertools/metrics": "^2.0.4",
"@aws-lambda-powertools/tracer": "^2.0.4",
"@summerfi/abis": "workspace:*",
"@summerfi/automation-subgraph": "workspace:*",
"@summerfi/prices-subgraph": "workspace:*",
"@summerfi/serverless-shared": "workspace:*",
Expand Down
21 changes: 21 additions & 0 deletions summerfi-api/get-triggers-function/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { z } from 'zod'

import {
addressSchema,
chainIdSchema,
optionalPoolIdSchema,
urlOptionalSchema,
} from '@summerfi/serverless-shared/validators'

export const paramsSchema = z.object({
account: addressSchema,
poolId: optionalPoolIdSchema,
chainId: chainIdSchema,
rpc: urlOptionalSchema,
protocol: z.string().optional(),
getDetails: z
.boolean()
.or(z.string().transform((s) => s === 'true'))
.optional()
.default(false),
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TriggersQuery } from '@summerfi/automation-subgraph'

export const filterTrigger =
(triggerType: bigint) => (trigger: TriggersQuery['triggers'][number]) =>
BigInt(trigger.triggerType) === triggerType
Loading

0 comments on commit e934a60

Please sign in to comment.