Skip to content

Commit

Permalink
Refactor codebase to centralize LTV and percentage schemas
Browse files Browse the repository at this point in the history
The codebase has been refactored to centralize the LTV and percentage schemas, now hosted within the `serverless-shared` package. Code that previously imported these schemas from `triggers-shared` has also been updated to reflect this change. Simultaneously, the `min-net-value-map.ts` file was deleted as it was not being used.
  • Loading branch information
jakubswierczek committed Apr 16, 2024
1 parent 0d26c08 commit 1710aa7
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 83 deletions.
13 changes: 13 additions & 0 deletions packages/serverless-shared/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,16 @@ export const ltvSchema = bigIntSchema.refine((ltv) => ltv >= 0n && ltv < 10_000n
},
message: 'LTV must be between 0 and 10_000',
})

export const percentageSchema = bigIntSchema.refine(
(percentage) => percentage >= 0n && percentage <= 10_000n,
{
params: {
code: 'percentage-out-of-range',
},
message: 'Percentage must be between 0 and 100_000',
},
)

export type LTV = z.infer<typeof ltvSchema>
export type Percentage = z.infer<typeof percentageSchema>
28 changes: 8 additions & 20 deletions packages/triggers-shared/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import { z } from 'zod'
import { PRICE_DECIMALS } from './constants'
import { addressSchema, bigIntSchema, ChainId, ProtocolId } from '@summerfi/serverless-shared'
import {
addressSchema,
bigIntSchema,
ChainId,
LTV,
ltvSchema,
ProtocolId,
} from '@summerfi/serverless-shared'

export const priceSchema = bigIntSchema.describe(`Price with ${PRICE_DECIMALS} decimals`)

export const maxGasFeeSchema = bigIntSchema.describe('Max gas fee in Gwei')

export const ltvSchema = bigIntSchema.refine((ltv) => ltv >= 0n && ltv < 10_000n, {
params: {
code: 'ltv-out-of-range',
},
message: 'LTV must be between 0 and 10_000',
})

export const percentageSchema = bigIntSchema.refine(
(percentage) => percentage >= 0n && percentage <= 10_000n,
{
params: {
code: 'percentage-out-of-range',
},
message: 'Percentage must be between 0 and 100_000',
},
)

export enum SupportedActions {
Add = 'add',
Remove = 'remove',
Expand Down Expand Up @@ -88,8 +78,6 @@ export type PositionLike = z.infer<typeof positionSchema>
export type Token = z.infer<typeof tokenSchema>
export type TokenBalance = z.infer<typeof tokenBalanceSchema>
export type Price = z.infer<typeof priceSchema>
export type LTV = z.infer<typeof ltvSchema>
export type Percentage = z.infer<typeof percentageSchema>

export type CurrentTriggerLike = {
id: bigint
Expand Down
7 changes: 5 additions & 2 deletions summerfi-api/get-migrations-function/src/addressService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ADDRESSES, AaveLikeProtocol, Common, Core } from '@oasisdex/addresses'
import { AaveLikeProtocol, ADDRESSES, Common, Core } from '@oasisdex/addresses'
import {
Address,
ChainId,
Expand Down Expand Up @@ -41,13 +41,16 @@ export const createAddressService = (chainId: ChainId) => {
let val
switch (protocol) {
case ProtocolId.AAVE3:
case ProtocolId.AAVE_V3:
val = addresses['aave']['v3'][contract] as Address
break

case ProtocolId.SPARK:
val = addresses['spark'][contract] as Address
break

case ProtocolId.AAVE_V2:
val = addresses['aave']['v2'][contract] as Address
break
default:
throw Error(`Unknown protocol - (${protocol}) on (${network})`)
}
Expand Down
8 changes: 4 additions & 4 deletions summerfi-api/get-migrations-function/src/migrations-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export type IMigrationConfig = Record<ChainId, ProtocolId[]>
* MigrationConfig is a map of supported protocols per chain
*/
export const MigrationConfig: IMigrationConfig = {
[ChainId.MAINNET]: [ProtocolId.AAVE3, ProtocolId.SPARK],
[ChainId.ARBITRUM]: [ProtocolId.AAVE3],
[ChainId.OPTIMISM]: [ProtocolId.AAVE3],
[ChainId.BASE]: [ProtocolId.AAVE3],
[ChainId.MAINNET]: [ProtocolId.AAVE_V3, ProtocolId.SPARK],
[ChainId.ARBITRUM]: [ProtocolId.AAVE_V3],
[ChainId.OPTIMISM]: [ProtocolId.AAVE_V3],
[ChainId.BASE]: [ProtocolId.AAVE_V3],
[ChainId.SEPOLIA]: [],
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import {
import {
positionSchema,
priceSchema,
supportedActionsSchema,
SupportedActions,
ltvSchema,
supportedActionsSchema,
TWENTY_MILLIONS_DOllARS,
} from '@summerfi/triggers-shared'
import { z } from 'zod'
import { GetTriggersResponse } from '@summerfi/triggers-shared/contracts'
import { DerivedPrices } from '@summerfi/prices-subgraph'
import { safeParseBigInt } from '@summerfi/serverless-shared'
import { ltvSchema, safeParseBigInt } from '@summerfi/serverless-shared'

const paramsSchema = z.object({
position: positionSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import {
import {
positionSchema,
priceSchema,
supportedActionsSchema,
SupportedActions,
supportedActionsSchema,
TWENTY_MILLIONS_DOllARS,
ltvSchema,
} from '@summerfi/triggers-shared'
import { z } from 'zod'
import { GetTriggersResponse } from '@summerfi/triggers-shared/contracts'
import { DerivedPrices } from '@summerfi/prices-subgraph'
import { safeParseBigInt } from '@summerfi/serverless-shared'
import { ltvSchema, safeParseBigInt } from '@summerfi/serverless-shared'

const paramsSchema = z.object({
position: positionSchema,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { adjustToTargetRiskRatio, RiskRatio } from '@oasisdex/dma-library'
import BigNumber from 'bignumber.js'
import {
LTV,
MULTIPLY_DECIMALS,
ONE_PERCENT,
PERCENT_DECIMALS,
Expand All @@ -10,6 +9,7 @@ import {
PRICE_DECIMALS,
} from '@summerfi/triggers-shared'
import { Logger } from '@aws-lambda-powertools/logger'
import { LTV } from '@summerfi/serverless-shared'

export interface SimulatedPosition {
targetLTV: LTV
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { z } from 'zod'
import { TriggerType } from '@oasisdex/automation'
import { AutoBuyTriggerCustomErrorCodes } from '~types'
import {
ltvSchema,
maxGasFeeSchema,
maxUnit256,
positionAddressesSchema,
priceSchema,
supportedActionsSchema,
} from '@summerfi/triggers-shared'
import { addressSchema, urlOptionalSchema } from '@summerfi/serverless-shared'
import { addressSchema, ltvSchema, urlOptionalSchema } from '@summerfi/serverless-shared'

export const aaveBasicBuyTriggerDataSchema = z
.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { z } from 'zod'
import { TriggerType } from '@oasisdex/automation'
import { AutoSellTriggerCustomErrorCodes } from '~types'
import {
ltvSchema,
maxGasFeeSchema,
positionAddressesSchema,
priceSchema,
supportedActionsSchema,
} from '@summerfi/triggers-shared'
import { addressSchema, urlOptionalSchema } from '@summerfi/serverless-shared'
import { addressSchema, ltvSchema, urlOptionalSchema } from '@summerfi/serverless-shared'

export const aaveBasicSellTriggerDataSchema = z
.object({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { z } from 'zod'
import { TriggerType } from '@oasisdex/automation'
import {
ltvSchema,
percentageSchema,
positionAddressesSchema,
priceSchema,
supportedActionsSchema,
} from '@summerfi/triggers-shared'
import { addressSchema, urlOptionalSchema } from '@summerfi/serverless-shared'
import {
addressSchema,
ltvSchema,
percentageSchema,
urlOptionalSchema,
} from '@summerfi/serverless-shared'
import { dmaAaveStopLossTriggerDataSchema } from './aave-stop-loss'

export const aavePartialTakeProfitTriggerDataSchema = z.object({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { z } from 'zod'
import { TriggerType } from '@oasisdex/automation'
import { addressSchema, urlOptionalSchema } from '@summerfi/serverless-shared'
import {
ltvSchema,
positionAddressesSchema,
supportedActionsSchema,
} from '@summerfi/triggers-shared'
import { addressSchema, ltvSchema, urlOptionalSchema } from '@summerfi/serverless-shared'
import { positionAddressesSchema, supportedActionsSchema } from '@summerfi/triggers-shared'

export const dmaAaveStopLossToCollateralTriggerDataSchema = z.object({
type: z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { z } from 'zod'
import { TriggerType } from '@oasisdex/automation'
import { AutoBuyTriggerCustomErrorCodes } from '~types'
import {
ltvSchema,
maxGasFeeSchema,
maxUnit256,
positionAddressesSchema,
priceSchema,
supportedActionsSchema,
} from '@summerfi/triggers-shared'
import { addressSchema, urlOptionalSchema } from '@summerfi/serverless-shared'
import { addressSchema, ltvSchema, urlOptionalSchema } from '@summerfi/serverless-shared'

export const sparkBasicBuyTriggerDataSchema = z
.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { z } from 'zod'
import { TriggerType } from '@oasisdex/automation'
import { AutoSellTriggerCustomErrorCodes } from '~types'
import {
ltvSchema,
maxGasFeeSchema,
positionAddressesSchema,
priceSchema,
supportedActionsSchema,
} from '@summerfi/triggers-shared'
import { addressSchema, urlOptionalSchema } from '@summerfi/serverless-shared'
import { addressSchema, ltvSchema, urlOptionalSchema } from '@summerfi/serverless-shared'

export const sparkBasicSellTriggerDataSchema = z
.object({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { z } from 'zod'
import { TriggerType } from '@oasisdex/automation'
import {
ltvSchema,
percentageSchema,
positionAddressesSchema,
priceSchema,
supportedActionsSchema,
} from '@summerfi/triggers-shared'
import { addressSchema, urlOptionalSchema } from '@summerfi/serverless-shared'
import {
addressSchema,
ltvSchema,
percentageSchema,
urlOptionalSchema,
} from '@summerfi/serverless-shared'
import { dmaSparkStopLossTriggerDataSchema } from './spark-stop-loss'

export const sparkPartialTakeProfitTriggerDataSchema = z.object({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { z } from 'zod'
import { TriggerType } from '@oasisdex/automation'
import { addressSchema, urlOptionalSchema } from '@summerfi/serverless-shared'
import {
ltvSchema,
positionAddressesSchema,
supportedActionsSchema,
} from '@summerfi/triggers-shared'
import { addressSchema, ltvSchema, urlOptionalSchema } from '@summerfi/serverless-shared'
import { positionAddressesSchema, supportedActionsSchema } from '@summerfi/triggers-shared'

export const dmaSparkStopLossToCollateralTriggerDataSchema = z.object({
type: z
Expand Down

0 comments on commit 1710aa7

Please sign in to comment.