-
Notifications
You must be signed in to change notification settings - Fork 11
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
Showing
6 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
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
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 @@ | ||
export { MakerPosition } from './maker-position' |
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,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, | ||
) | ||
} | ||
} |
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
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), | ||
) | ||
} |