Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinciarka committed Apr 29, 2024
1 parent bf63f79 commit 1545d2b
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { getPropertyFromTriggerParams, Trigger } from '@summerfi/triggers-shared/contracts'
import { safeParseBigInt } from '@summerfi/serverless-shared'
import { PositionLike, CurrentStopLoss } from '@summerfi/triggers-shared'
import { Logger } from '@aws-lambda-powertools/logger'
import {
calculateLtv,
calculateCollateralPriceInDebtBasedOnLtv,
} from '@summerfi/triggers-calculations'

export function getCurrentMorphoBlueStopLoss(
triggers: { triggerGroup: { morphoBlueStopLoss?: Trigger }; triggersCount: number },
position: PositionLike,
logger?: Logger,
): CurrentStopLoss | undefined {
const currentStopLoss = triggers.triggerGroup.morphoBlueStopLoss

if (!currentStopLoss) {
return undefined
}

const executionLtv = safeParseBigInt(
getPropertyFromTriggerParams({
trigger: currentStopLoss,
property: 'executionLtv',
}),
)

const ltv = safeParseBigInt(
getPropertyFromTriggerParams({
trigger: currentStopLoss,
property: 'ltv',
}),
)

const executionPrice = safeParseBigInt(
getPropertyFromTriggerParams({
trigger: currentStopLoss,
property: 'executionPrice',
}),
)

if (executionLtv === undefined && ltv === undefined && executionPrice === undefined) {
logger?.warn('Stop loss trigger has no executionLtv or executionPrice')
return undefined
}

const stopLossExecutionLtv =
executionLtv ??
ltv ??
calculateLtv({
...position,
collateralPriceInDebt: executionPrice!,
})

const stopLossExecutionPrice =
executionPrice ??
calculateCollateralPriceInDebtBasedOnLtv({
...position,
ltv: stopLossExecutionLtv,
})

return {
triggerData: currentStopLoss.triggerData as `0x${string}`,
id: safeParseBigInt(currentStopLoss.triggerId) ?? 0n,
executionLTV: stopLossExecutionLtv,
executionPrice: stopLossExecutionPrice,
triggersOnAccount: triggers.triggersCount,
}
}
136 changes: 136 additions & 0 deletions packages/triggers-calculations/src/get-morphoblue-position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { PositionLike, PRICE_DECIMALS, TokenBalance } from '@summerfi/triggers-shared'
import { Address } from '@summerfi/serverless-shared'
import { PublicClient } from 'viem'
import { aavePoolDataProviderAbi, aaveOracleAbi, erc20Abi } from '@summerfi/abis'
import { calculateLtv, isStablecoin } from './helpers'
import { Logger } from '@aws-lambda-powertools/logger'

export interface GetMorphoBluePositionParams {
address: Address
collateral: Address
debt: Address
}
export async function getMorphoBluePosition(
{ address, collateral, debt }: GetMorphoBluePositionParams,
publicClient: PublicClient,
addresses: { poolDataProvider: Address; oracle: Address },
logger?: Logger,
): Promise<PositionLike> {
const [
collateralData,
debtData,
oraclePrices,
collateralDecimals,
collateralSymbol,
debtDecimals,
debtSymbol,
] = await publicClient.multicall({
contracts: [
{
abi: aavePoolDataProviderAbi,
address: addresses.poolDataProvider,
functionName: 'getUserReserveData',
args: [collateral, address],
},
{
abi: aavePoolDataProviderAbi,
address: addresses.poolDataProvider,
functionName: 'getUserReserveData',
args: [debt, address],
},
{
abi: aaveOracleAbi,
address: addresses.oracle,
functionName: 'getAssetsPrices',
args: [[collateral, debt]],
},
{
abi: erc20Abi,
address: collateral,
functionName: 'decimals',
},
{
abi: erc20Abi,
address: collateral,
functionName: 'symbol',
},
{
abi: erc20Abi,
address: debt,
functionName: 'decimals',
},
{
abi: erc20Abi,
address: debt,
functionName: 'symbol',
},
],
allowFailure: true,
})

const collateralAmount = collateralData.status === 'success' ? collateralData.result[0] : 0n
const debtAmount = debtData.status === 'success' ? debtData.result[1] + debtData.result[2] : 0n

const [collateralPrice, debtPrice] =
oraclePrices.status === 'success' ? oraclePrices.result : [0n, 0n]

const collateralPriceInDebt =
collateralPrice == 0n || debtPrice === 0n
? 0n
: (collateralPrice * 10n ** PRICE_DECIMALS) / debtPrice

const collateralResult: TokenBalance = {
balance: collateralAmount,
token: {
decimals: collateralDecimals.status === 'success' ? collateralDecimals.result : 18,
symbol: collateralSymbol.status === 'success' ? collateralSymbol.result : '',
address: collateral,
},
}

const debtResult: TokenBalance = {
balance: debtAmount,
token: {
decimals: debtDecimals.status === 'success' ? debtDecimals.result : 18,
symbol: debtSymbol.status === 'success' ? debtSymbol.result : '',
address: debt,
},
}

const ltv = calculateLtv({
collateral: collateralResult,
debt: debtResult,
collateralPriceInDebt,
})

const debtValueUSD = (debtAmount * debtPrice) / 10n ** BigInt(debtResult.token.decimals)

const collateralValueUSD =
(collateralAmount * collateralPrice) / 10n ** BigInt(collateralResult.token.decimals)

const netValue = collateralValueUSD - debtValueUSD

logger?.debug('Position data', {
debt: debtResult,
collateral: collateralResult,
collateralPriceInDebt,
ltv,
netValue,
})

return {
hasStablecoinDebt: isStablecoin(debtPrice),
ltv,
collateral: collateralResult,
debt: debtResult,
address: address,
oraclePrices: {
collateralPrice,
debtPrice,
},
collateralPriceInDebt,
netValueUSD: netValue,
debtValueUSD,
collateralValueUSD,
}
}
8 changes: 5 additions & 3 deletions packages/triggers-calculations/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './helpers'
export * from './get-aave-position'
export * from './get-spark-position'
export * from './simulations'
export * from './get-current-aave-stop-loss'
export * from './get-current-morphoblue-stop-loss'
export * from './get-current-spark-stop-loss'
export * from './get-morphoblue-position'
export * from './get-spark-position'
export * from './helpers'
export * from './simulations'
2 changes: 2 additions & 0 deletions packages/triggers-shared/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const positionAddressesSchema = z.object({
debt: addressSchema,
})

export const poolIdSchema = z.string().startsWith('0x')

export const positionTokensPricesSchema = z.object({
collateralPrice: priceSchema,
debtPrice: priceSchema,
Expand Down
6 changes: 5 additions & 1 deletion summerfi-api/get-triggers-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ export const handler = async (
triggerType: '128',
maxCoverage: '1500132791',
executionLtv: '7546',
ltv: '7546',
debtToken: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
collateralToken: '0x4200000000000000000000000000000000000006',
operationName: '0x436c6f7365414156455633506f736974696f6e5f340000000000000000000000',
},
},
},
Expand All @@ -435,6 +435,10 @@ export const handler = async (
isSparkBasicSellEnabled: hasAnyDefined(sparkBasicSell),
isAavePartialTakeProfitEnabled: hasAnyDefined(aavePartialTakeProfit),
isSparkPartialTakeProfitEnabled: hasAnyDefined(sparkPartialTakeProfit),
isMorphoBlueBasicBuyEnabled: false,
isMorphoBlueBasicSellEnabled: false,
isMorphoBlueStopLossEnabled: false,
isMorphoBluePartialTakeProfitEnabled: false,
},
triggersCount: triggers.triggers.length,
triggerGroup: {
Expand Down
Loading

0 comments on commit 1545d2b

Please sign in to comment.