Skip to content

Commit

Permalink
Maker omni
Browse files Browse the repository at this point in the history
  • Loading branch information
piekczyk committed Aug 22, 2024
1 parent 8467f7f commit 5e46d31
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/dma-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ export type {
} from './types/ajna'
export { AjnaEarnPosition, AjnaPosition } from './types/ajna'
export * from './types/cumulatives'
export { MakerPosition } from './types/maker'
export { MorphoBluePosition } from './types/morphoblue'
export { Network } from '@deploy-configurations/types/network'
3 changes: 3 additions & 0 deletions packages/dma-library/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from './ajna'
import type { Strategy } from './common'
import { FlashloanProvider } from './common'
import { MakerPosition } from './maker'
import { MorphoBlueMarket, MorphoBluePosition } from './morphoblue'
import type {
IOperation,
Expand Down Expand Up @@ -123,6 +124,8 @@ export type { Swap }
export { MorphoBluePosition }
export type { MorphoBlueMarket }

export { MakerPosition }

export type {
Erc4626CommonDependencies,
Erc4626DepositPayload,
Expand Down
1 change: 1 addition & 0 deletions packages/dma-library/src/types/maker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MakerPosition } from './maker-position'
195 changes: 195 additions & 0 deletions packages/dma-library/src/types/maker/maker-position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import { Address } from '@deploy-configurations/types/address'
import { ONE, ZERO } from '@dma-common/constants'
import { negativeToZero, normalizeValue } from '@dma-common/utils/common'
import { LendingPosition } from '@dma-library/types/morphoblue/morphoblue-position'
import { getBuyingPower } from '@dma-library/views/common'
import { MorphoCumulativesData } from '@dma-library/views/morpho'
import { RiskRatio } from '@domain'
import { BigNumber } from 'bignumber.js'

export class MakerPosition implements LendingPosition {
constructor(
public owner: Address,
public collateralAmount: BigNumber,
public debtAmount: BigNumber,
public marketCollateralPrice: BigNumber,
public osmCurrentCollateralPrice: BigNumber,
public osmNextCollateralPrice: BigNumber,
public debtPrice: BigNumber,
// collateral / debt
public price: BigNumber,
public rate: BigNumber,
public pnl: {
withFees: BigNumber
withoutFees: BigNumber
cumulatives: MorphoCumulativesData
},
public liquidationRatio: BigNumber,
public penalty: BigNumber,
) {}

get liquidationPrice() {
return normalizeValue(
ONE.div(this.collateralAmount.times(this.maxRiskRatio.loanToValue).div(this.debtAmount)),
)
}

get marketPrice() {
return this.price
}

get liquidationToMarketPrice() {
return this.liquidationPrice.div(this.marketPrice)
}

// How much collateral can we withdraw to not get liquidated, (to get to the verge of liquidation)
get collateralAvailable() {
const collateralAvailable = this.collateralAmount.minus(
this.debtAmount.div(this.maxRiskRatio.loanToValue).div(this.price),
)

return negativeToZero(normalizeValue(collateralAvailable))
}

get riskRatio() {
const loanToValue = this.debtAmount.div(this.collateralAmount.times(this.price))

return new RiskRatio(normalizeValue(loanToValue), RiskRatio.TYPE.LTV)
}

get maxRiskRatio() {
return new RiskRatio(normalizeValue(this.liquidationRatio), RiskRatio.TYPE.LTV)
}

get borrowRate(): BigNumber {
return this.rate
}

get netValue(): BigNumber {
return this.collateralAmount
.times(this.osmCurrentCollateralPrice)
.minus(this.debtAmount.times(this.debtPrice))
}

get minRiskRatio() {
return new RiskRatio(normalizeValue(ZERO), RiskRatio.TYPE.LTV)
}

get buyingPower() {
return getBuyingPower({
netValue: this.netValue,
collateralPrice: this.osmCurrentCollateralPrice,
marketPrice: this.marketPrice,
debtAmount: this.debtAmount,
maxRiskRatio: this.maxRiskRatio,
})
}

get liquidationPenalty() {
console.log('liquidationPenalty', this.penalty)
return this.penalty
}

debtAvailable(collateralAmount?: BigNumber, debtAmount?: BigNumber) {
// (debt + addDebt) / ((col + addedColl) * price) = lltv
// lltv*price*(col + addedColl) - debt = addDebt

return negativeToZero(
normalizeValue(
this.maxRiskRatio.loanToValue
.times(this.marketPrice)
.times(collateralAmount || this.collateralAmount)
.minus(debtAmount || this.debtAmount),
),
)
}

deposit(collateralAmount: BigNumber) {
const newCollateralAmount = negativeToZero(this.collateralAmount.plus(collateralAmount))
return new MakerPosition(
this.owner,
newCollateralAmount,
this.debtAmount,
this.marketCollateralPrice,
this.osmCurrentCollateralPrice,
this.osmNextCollateralPrice,
this.debtPrice,
this.price,
this.rate,
this.pnl,
this.liquidationRatio,
this.penalty,
)
}

withdraw(collateralAmount: BigNumber) {
const newCollateralAmount = negativeToZero(this.collateralAmount.minus(collateralAmount))
return new MakerPosition(
this.owner,
newCollateralAmount,
this.debtAmount,
this.marketCollateralPrice,
this.osmCurrentCollateralPrice,
this.osmNextCollateralPrice,
this.debtPrice,
this.price,
this.rate,
this.pnl,
this.liquidationRatio,
this.penalty,
)
}

borrow(quoteAmount: BigNumber): MakerPosition {
const newDebt = negativeToZero(this.debtAmount.plus(quoteAmount))
return new MakerPosition(
this.owner,
this.collateralAmount,
newDebt,
this.marketCollateralPrice,
this.osmCurrentCollateralPrice,
this.osmNextCollateralPrice,
this.debtPrice,
this.price,
this.rate,
this.pnl,
this.liquidationRatio,
this.penalty,
)
}

payback(quoteAmount: BigNumber): MakerPosition {
const newDebt = negativeToZero(this.debtAmount.minus(quoteAmount))
return new MakerPosition(
this.owner,
this.collateralAmount,
newDebt,
this.marketCollateralPrice,
this.osmCurrentCollateralPrice,
this.osmNextCollateralPrice,
this.debtPrice,
this.price,
this.rate,
this.pnl,
this.liquidationRatio,
this.penalty,
)
}

close(): MakerPosition {
return new MakerPosition(
this.owner,
ZERO,
ZERO,
this.marketCollateralPrice,
this.osmCurrentCollateralPrice,
this.osmNextCollateralPrice,
this.debtPrice,
this.price,
this.rate,
this.pnl,
this.liquidationRatio,
this.penalty,
)
}
}
6 changes: 6 additions & 0 deletions packages/dma-library/src/views/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AaveVersion } from '@dma-library/types/aave'
import { getMakerPosition } from '@dma-library/views/maker'

import {
AaveView,
Expand Down Expand Up @@ -53,6 +54,10 @@ const ajna = {
const morpho = {
getPosition: getMorphoPosition,
}

const maker = {
getPosition: getMakerPosition,
}
const common = {
getErc4626Position: getErc4626Position,
}
Expand All @@ -61,6 +66,7 @@ const views = {
aave,
spark,
sparkOmni,
maker,
morpho,
common,
}
Expand Down
137 changes: 137 additions & 0 deletions packages/dma-library/src/views/maker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { ZERO } from '@dma-common/constants'
// import { LendingCumulativesData } from '@dma-library/types'
import { MakerPosition } from '@dma-library/types/maker/maker-position'
// import { GetCumulativesData } from '@dma-library/views'
import { BigNumber } from 'bignumber.js'
// import { ethers } from 'ethers'

interface Args {
proxyAddress: string
marketCollateralPriceUSD: BigNumber
osmCurrentCollateralPriceUSD: BigNumber
osmNextCollateralPriceUSD: BigNumber
quotePriceUSD: BigNumber
collateralPrecision: number
quotePrecision: number
marketId: string
}

// export type MakerCumulativesData = LendingCumulativesData

interface Dependencies {
// provider: ethers.providers.Provider
getPosition: () => Promise<any>
// getCumulatives: GetCumulativesData<MakerCumulativesData>
}

export async function getMakerPosition(
{
proxyAddress,
marketCollateralPriceUSD,
osmCurrentCollateralPriceUSD,
osmNextCollateralPriceUSD,
quotePriceUSD,
}: Args,
{ getPosition }: Dependencies,
): Promise<MakerPosition> {
const position = await getPosition()

// const morpho = new ethers.Contract(morphoAddress, morphoAbi, provider) as any as Morpho

// const marketParams = await morpho.idToMarketParams(marketId)
// const market = await morpho.market(marketId)
// const positionParams = await morpho.position(marketId, proxyAddress)

// const totals = {
// totalSupplyAssets: new BigNumber(market.totalSupplyAssets.toString()).div(
// TEN.pow(quotePrecision),
// ),
// totalSupplyShares: new BigNumber(market.totalSupplyShares.toString()).div(TEN.pow(24)),
// totalBorrowAssets: new BigNumber(market.totalBorrowAssets.toString()).div(
// TEN.pow(quotePrecision),
// ),
// totalBorrowShares: new BigNumber(market.totalBorrowShares.toString()).div(TEN.pow(24)),
// }
//
// const oracle = new ethers.Contract(marketParams.oracle, oracleAbi, provider) as any as Oracle
// const irm = new ethers.Contract(marketParams.irm, irmAbi, provider) as any as Irm
//
// const price = await oracle.price()
// const rate = await irm.borrowRateView(marketParams, market)
//
// const apy = getMarketRate(rate.toString())
//
// const debtAmount = toAssetsDown(
// new BigNumber(positionParams.borrowShares.toString()),
// new BigNumber(market.totalBorrowAssets.toString()),
// new BigNumber(market.totalBorrowShares.toString()),
// )
// .integerValue()
// .div(TEN.pow(quotePrecision))
// const collateralAmount = new BigNumber(positionParams.collateral.toString()).div(
// TEN.pow(collateralPrecision),
// )
//
// const cumulatives = await getCumulatives(proxyAddress, marketId)
//
// const {
// borrowCumulativeWithdrawInCollateralToken,
// borrowCumulativeDepositInCollateralToken,
// borrowCumulativeFeesInCollateralToken,
// } = cumulatives
//
// const netValue = collateralAmount.times(collateralPriceUSD).minus(debtAmount.times(quotePriceUSD))
//
// const pnl = {
// withFees: normalizeValue(
// borrowCumulativeWithdrawInCollateralToken
// .plus(netValue.div(collateralPriceUSD))
// .minus(borrowCumulativeDepositInCollateralToken)
// .minus(borrowCumulativeFeesInCollateralToken)
// .div(borrowCumulativeDepositInCollateralToken),
// ),
// withoutFees: normalizeValue(
// borrowCumulativeWithdrawInCollateralToken
// .plus(netValue.div(collateralPriceUSD))
// .minus(borrowCumulativeDepositInCollateralToken)
// .div(borrowCumulativeDepositInCollateralToken),
// ),
// cumulatives,
// }

const collateralAmount = new BigNumber(position.collateral)
const debtAmount = new BigNumber(position.normalizedDebt).times(position.ilk.rate)

return new MakerPosition(
proxyAddress,
collateralAmount,
debtAmount,
marketCollateralPriceUSD,
osmCurrentCollateralPriceUSD,
osmNextCollateralPriceUSD,
quotePriceUSD,
osmCurrentCollateralPriceUSD.div(quotePriceUSD),
new BigNumber(Number(position.ilk.stabilityFee) - 1),
{
withFees: ZERO,
withoutFees: ZERO,
cumulatives: {
borrowCumulativeDepositUSD: ZERO,
borrowCumulativeDepositInQuoteToken: ZERO,
borrowCumulativeDepositInCollateralToken: ZERO,
borrowCumulativeWithdrawUSD: ZERO,
borrowCumulativeWithdrawInQuoteToken: ZERO,
borrowCumulativeWithdrawInCollateralToken: ZERO,
borrowCumulativeCollateralDeposit: ZERO,
borrowCumulativeCollateralWithdraw: ZERO,
borrowCumulativeDebtDeposit: ZERO,
borrowCumulativeDebtWithdraw: ZERO,
borrowCumulativeFeesUSD: ZERO,
borrowCumulativeFeesInQuoteToken: ZERO,
borrowCumulativeFeesInCollateralToken: ZERO,
},
},
new BigNumber(1 / (Number(position.ilk.liquidationRatio) / 10 ** 27)),
new BigNumber(Number(position.ilk.liquidationPenalty) - 1),
)
}

0 comments on commit 5e46d31

Please sign in to comment.