Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maker omni #698

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
191 changes: 191 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,191 @@
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() {
return this.penalty
}

debtAvailable(collateralAmount?: BigNumber, debtAmount?: BigNumber) {
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
140 changes: 140 additions & 0 deletions packages/dma-library/src/views/maker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
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 = position.collateral ? new BigNumber(position.collateral) : ZERO
const debtAmount = position.normalizedDebt
? new BigNumber(position.normalizedDebt).times(position.ilk.rate)
: ZERO
const rate = new BigNumber(Number(position.ilk.stabilityFee) - 1)

return new MakerPosition(
proxyAddress,
collateralAmount,
debtAmount,
marketCollateralPriceUSD,
osmCurrentCollateralPriceUSD,
osmNextCollateralPriceUSD,
quotePriceUSD,
osmCurrentCollateralPriceUSD.div(quotePriceUSD),
rate,
{
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),
)
}
Loading