-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf63f79
commit 1545d2b
Showing
11 changed files
with
340 additions
and
27 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
packages/triggers-calculations/src/get-current-morphoblue-stop-loss.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
136
packages/triggers-calculations/src/get-morphoblue-position.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.