-
Notifications
You must be signed in to change notification settings - Fork 3
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
709d673
commit fbb6f66
Showing
5 changed files
with
251 additions
and
204 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import { BigNumber } from 'ethers' | ||
|
||
export interface FclConfig { | ||
minLiquidity: string | ||
riskYieldMultiplierInBps: number | ||
flcIndex: number | ||
maxLiquidity: string | ||
} | ||
|
||
export interface FclConfigWithApy extends FclConfig { | ||
apy: number | ||
firstLossCoverIndex: number | ||
} | ||
|
||
export interface PoolApy { | ||
flcConfigsWithApy: FclConfigWithApy[] | ||
blendedApy: number | ||
seniorTrancheApy: number | ||
juniorTrancheApy: number | ||
} | ||
|
||
const getFlcTotalAssetsAfterRisk = ( | ||
flcConfigs: { | ||
minLiquidity: string | ||
riskYieldMultiplierInBps: number | ||
}[], | ||
BP_FACTOR: number, | ||
): BigNumber => { | ||
const flcTotalAssetsAfterRisk = flcConfigs.reduce( | ||
(acc, flcConfig) => | ||
acc.add( | ||
BigNumber.from(flcConfig.minLiquidity) | ||
.mul(flcConfig.riskYieldMultiplierInBps) | ||
.div(BP_FACTOR), | ||
), | ||
BigNumber.from(0), | ||
) | ||
return flcTotalAssetsAfterRisk | ||
} | ||
|
||
const getPoolApyV2Base = ( | ||
protocolFeeInBps: number, | ||
yieldInBps: number, | ||
rewardRateInBpsForPoolOwner: number, | ||
rewardRateInBpsForEA: number, | ||
liquidityCap: string, | ||
seniorDeployedAssets: string, | ||
juniorDeployedAssets: string, | ||
maxSeniorJuniorRatio: number, | ||
fixedSeniorYieldInBps: number, | ||
tranchesRiskAdjustmentInBps: number, | ||
BP_FACTOR: number, | ||
FirstLossCoverIndexes: string[] = [], | ||
flcConfigs: FclConfig[] = [], | ||
): PoolApy => { | ||
const APY = yieldInBps / BP_FACTOR | ||
|
||
const liquidityCapBN = BigNumber.from(liquidityCap) | ||
const seniorDeployedAssetsBN = BigNumber.from(seniorDeployedAssets) | ||
const juniorDeployedAssetsBN = BigNumber.from(juniorDeployedAssets) | ||
const totalDeployedAssetsBN = seniorDeployedAssetsBN.add( | ||
juniorDeployedAssetsBN, | ||
) | ||
const seniorMaxAssetsBN = liquidityCapBN | ||
.sub(totalDeployedAssetsBN) | ||
.add(seniorDeployedAssetsBN) | ||
const currentMaxSeniorJuniorRatio = seniorMaxAssetsBN | ||
.div(juniorDeployedAssetsBN) | ||
.toNumber() | ||
|
||
let juniorAssetsBN = liquidityCapBN.div(maxSeniorJuniorRatio + 1) | ||
let seniorAssetsBN = liquidityCapBN.sub(juniorAssetsBN) | ||
if (currentMaxSeniorJuniorRatio < maxSeniorJuniorRatio) { | ||
juniorAssetsBN = juniorDeployedAssetsBN | ||
seniorAssetsBN = seniorMaxAssetsBN | ||
} | ||
|
||
const totalProfitBN = liquidityCapBN | ||
.mul(Math.round(APY * BP_FACTOR)) | ||
.div(BP_FACTOR) | ||
const postPoolProfitRatio = | ||
(1 - protocolFeeInBps / BP_FACTOR) * | ||
(1 - | ||
rewardRateInBpsForPoolOwner / BP_FACTOR - | ||
rewardRateInBpsForEA / BP_FACTOR) | ||
const poolPostProfitBN = totalProfitBN | ||
.mul(Math.round(postPoolProfitRatio * BP_FACTOR)) | ||
.div(BP_FACTOR) | ||
const blendedApy = | ||
poolPostProfitBN.mul(BP_FACTOR).div(liquidityCap).toNumber() / BP_FACTOR | ||
|
||
let seniorTrancheApy = 0 | ||
let seniorPostProfitBN = BigNumber.from(0) | ||
if (fixedSeniorYieldInBps > 0) { | ||
seniorTrancheApy = fixedSeniorYieldInBps / BP_FACTOR | ||
seniorPostProfitBN = poolPostProfitBN.sub( | ||
seniorAssetsBN | ||
.mul(Math.round(seniorTrancheApy * BP_FACTOR)) | ||
.div(BP_FACTOR), | ||
) | ||
} else { | ||
const riskAdjustment = tranchesRiskAdjustmentInBps / BP_FACTOR | ||
const seniorProfitBN = seniorAssetsBN | ||
.mul( | ||
Math.round( | ||
postPoolProfitRatio * (1 - riskAdjustment) * APY * BP_FACTOR, | ||
), | ||
) | ||
.div(BP_FACTOR) | ||
seniorTrancheApy = postPoolProfitRatio * (1 - riskAdjustment) * APY | ||
seniorPostProfitBN = poolPostProfitBN.sub(seniorProfitBN) | ||
} | ||
|
||
const flcTotalAssetsAfterRiskBN = getFlcTotalAssetsAfterRisk( | ||
flcConfigs, | ||
BP_FACTOR, | ||
) | ||
const weightBN = juniorAssetsBN.add(flcTotalAssetsAfterRiskBN) | ||
|
||
const juniorProfitBN = juniorAssetsBN.mul(seniorPostProfitBN).div(weightBN) | ||
const juniorTrancheApy = | ||
juniorProfitBN.mul(BP_FACTOR).div(juniorAssetsBN).toNumber() / BP_FACTOR | ||
const flcTotalProfitBN = flcTotalAssetsAfterRiskBN | ||
.mul(seniorPostProfitBN) | ||
.div(weightBN) | ||
const flcConfigsWithApy = flcConfigs.map((flcConfig) => { | ||
const minLiquidityBN = BigNumber.from(flcConfig.minLiquidity) | ||
const flcAssetsAfterRiskBN = minLiquidityBN | ||
.mul(flcConfig.riskYieldMultiplierInBps) | ||
.div(BP_FACTOR) | ||
|
||
let apy = 0 | ||
if (minLiquidityBN.gt(0) && flcAssetsAfterRiskBN.gt(0)) { | ||
const flcProfitBN = flcAssetsAfterRiskBN | ||
.mul(flcTotalProfitBN) | ||
.div(flcTotalAssetsAfterRiskBN) | ||
apy = | ||
flcProfitBN.mul(BP_FACTOR).div(flcConfig.minLiquidity).toNumber() / | ||
BP_FACTOR | ||
} | ||
|
||
return { | ||
...flcConfig, | ||
firstLossCoverIndex: Number(FirstLossCoverIndexes[flcConfig.flcIndex]), | ||
apy, | ||
} | ||
}) | ||
|
||
return { | ||
flcConfigsWithApy, | ||
blendedApy, | ||
seniorTrancheApy, | ||
juniorTrancheApy, | ||
} | ||
} | ||
|
||
export const getPoolApyV2 = ( | ||
protocolFeeInBps: number, | ||
yieldInBps: number, | ||
rewardRateInBpsForPoolOwner: number, | ||
rewardRateInBpsForEA: number, | ||
liquidityCap: string, | ||
seniorDeployedAssets: string, | ||
juniorDeployedAssets: string, | ||
maxSeniorJuniorRatio: number, | ||
fixedSeniorYieldInBps: number, | ||
tranchesRiskAdjustmentInBps: number, | ||
BP_FACTOR: number, | ||
FirstLossCoverIndexes: string[] = [], | ||
flcConfigs: FclConfig[] = [], | ||
): PoolApy & { maxJuniorTrancheApy: number } => { | ||
const realApyResult = getPoolApyV2Base( | ||
protocolFeeInBps, | ||
yieldInBps, | ||
rewardRateInBpsForPoolOwner, | ||
rewardRateInBpsForEA, | ||
liquidityCap, | ||
seniorDeployedAssets, | ||
juniorDeployedAssets, | ||
maxSeniorJuniorRatio, | ||
fixedSeniorYieldInBps, | ||
tranchesRiskAdjustmentInBps, | ||
BP_FACTOR, | ||
FirstLossCoverIndexes, | ||
flcConfigs, | ||
) | ||
|
||
const liquidityCapBN = BigNumber.from(liquidityCap) | ||
const juniorAssetsBN = liquidityCapBN.div(maxSeniorJuniorRatio + 1) | ||
const seniorAssetsBN = liquidityCapBN.sub(juniorAssetsBN) | ||
const maxJuniorApyResult = getPoolApyV2Base( | ||
protocolFeeInBps, | ||
yieldInBps, | ||
rewardRateInBpsForPoolOwner, | ||
rewardRateInBpsForEA, | ||
liquidityCap, | ||
seniorAssetsBN.toString(), | ||
juniorAssetsBN.toString(), | ||
maxSeniorJuniorRatio, | ||
fixedSeniorYieldInBps, | ||
tranchesRiskAdjustmentInBps, | ||
BP_FACTOR, | ||
) | ||
|
||
return { | ||
...realApyResult, | ||
maxJuniorTrancheApy: maxJuniorApyResult.juniorTrancheApy, | ||
} | ||
} |
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,21 +1,22 @@ | ||
export * from './JsonRpcConnector' | ||
export * from './apy' | ||
export * from './chain' | ||
export * from './common' | ||
export * from './config' | ||
export * from './const' | ||
export * from './credit' | ||
export * from './env' | ||
export * from './errors' | ||
export * from './JsonRpcConnector' | ||
export * from './jsonRpcEndpoints' | ||
export * from './notifi' | ||
export * from './number' | ||
export * from './pool' | ||
export * from './poolContract' | ||
export * from './realWorldReceivable' | ||
export * from './request' | ||
export * from './scientificToDecimal' | ||
export * from './string' | ||
export * from './style' | ||
export * from './time' | ||
export * from './transaction' | ||
export * from './web3' | ||
export * from './notifi' | ||
export * from './realWorldReceivable' | ||
export * from './env' |
Oops, something went wrong.