Skip to content

Commit

Permalink
Log error instead of throwing (#1540)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophialittlejohn authored Aug 18, 2023
1 parent 30de726 commit b17f6c5
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions centrifuge-app/src/utils/tinlake/useTinlakePools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,64 +583,68 @@ async function getPools(pools: IpfsPools): Promise<{ pools: TinlakePool[] }> {
const capacityGivenMaxReservePerPool: { [key: string]: BN } = {}
const capacityGivenMaxDropRatioPerPool: { [key: string]: BN } = {}
Object.keys(multicallData).forEach((poolId: string) => {
const state = multicallData[poolId]

// Investments will reduce the creditline and therefore reduce the senior debt
const newUsedCreditline = state.unusedCreditline
? BN.max(
new BN(0),
(state.usedCreditline || new BN(0))
.sub(state.pendingSeniorInvestments)
.sub(state.pendingJuniorInvestments)
.add(state.pendingSeniorRedemptions)
.add(state.pendingJuniorRedemptions)
)
: new BN(0)

const newUnusedCreditline = state.unusedCreditline ? state.availableCreditline?.sub(newUsedCreditline) : new BN(0)

const newReserve = BN.max(
new BN(0),
state.reserve
.add(state.pendingSeniorInvestments)
.add(state.pendingJuniorInvestments)
.sub(state.pendingSeniorRedemptions)
.sub(state.pendingJuniorRedemptions)
.sub(newUsedCreditline)
)
try {
const state = multicallData[poolId]

// Investments will reduce the creditline and therefore reduce the senior debt
const newUsedCreditline = state.unusedCreditline
? BN.max(
new BN(0),
(state.usedCreditline || new BN(0))
.sub(state.pendingSeniorInvestments)
.sub(state.pendingJuniorInvestments)
.add(state.pendingSeniorRedemptions)
.add(state.pendingJuniorRedemptions)
)
: new BN(0)

const newUnusedCreditline = state.unusedCreditline ? state.availableCreditline?.sub(newUsedCreditline) : new BN(0)

const newReserve = BN.max(
new BN(0),
state.reserve
.add(state.pendingSeniorInvestments)
.add(state.pendingJuniorInvestments)
.sub(state.pendingSeniorRedemptions)
.sub(state.pendingJuniorRedemptions)
.sub(newUsedCreditline)
)

const capacityGivenMaxReserve = BN.max(
new BN(0),
state.maxReserve.sub(newReserve).sub(newUnusedCreditline || new BN(0))
)
const capacityGivenMaxReserve = BN.max(
new BN(0),
state.maxReserve.sub(newReserve).sub(newUnusedCreditline || new BN(0))
)

// senior debt is reduced by any increase in the used creditline or increased by any decrease in the used creditline
const newSeniorDebt = (state.usedCreditline || new BN(0)).gt(newUsedCreditline)
? state.seniorDebt.sub((state.usedCreditline || new BN(0)).sub(newUsedCreditline))
: state.seniorDebt.add(newUsedCreditline.sub(state.usedCreditline || new BN(0)))
// senior debt is reduced by any increase in the used creditline or increased by any decrease in the used creditline
const newSeniorDebt = (state.usedCreditline || new BN(0)).gt(newUsedCreditline)
? state.seniorDebt.sub((state.usedCreditline || new BN(0)).sub(newUsedCreditline))
: state.seniorDebt.add(newUsedCreditline.sub(state.usedCreditline || new BN(0)))

// TODO: the change in senior balance should be multiplied by the mat here
const newSeniorBalance = (state.usedCreditline || new BN(0)).gt(newUsedCreditline)
? state.seniorBalance.sub((state.usedCreditline || new BN(0)).sub(newUsedCreditline))
: state.seniorBalance.add(newUsedCreditline.sub(state.usedCreditline || new BN(0)))
// TODO: the change in senior balance should be multiplied by the mat here
const newSeniorBalance = (state.usedCreditline || new BN(0)).gt(newUsedCreditline)
? state.seniorBalance.sub((state.usedCreditline || new BN(0)).sub(newUsedCreditline))
: state.seniorBalance.add(newUsedCreditline.sub(state.usedCreditline || new BN(0)))

const newSeniorAsset = newSeniorDebt
.add(newSeniorBalance)
.add(state.pendingSeniorInvestments)
.sub(state.pendingSeniorRedemptions)
const newSeniorAsset = newSeniorDebt
.add(newSeniorBalance)
.add(state.pendingSeniorInvestments)
.sub(state.pendingSeniorRedemptions)

const newJuniorAsset = state.netAssetValue.add(newReserve).sub(newSeniorAsset)
const maxPoolSize = newJuniorAsset
.mul(Fixed27Base.mul(new BN(10).pow(new BN(6))).div(Fixed27Base.sub(state.maxSeniorRatio)))
.div(new BN(10).pow(new BN(6)))
const newJuniorAsset = state.netAssetValue.add(newReserve).sub(newSeniorAsset)
const maxPoolSize = newJuniorAsset
.mul(Fixed27Base.mul(new BN(10).pow(new BN(6))).div(Fixed27Base.sub(state?.maxSeniorRatio)))
.div(new BN(10).pow(new BN(6)))

const maxSeniorAsset = maxPoolSize.sub(newJuniorAsset)
const maxSeniorAsset = maxPoolSize.sub(newJuniorAsset)

const capacityGivenMaxDropRatio = BN.max(new BN(0), maxSeniorAsset.sub(newSeniorAsset))
const capacityGivenMaxDropRatio = BN.max(new BN(0), maxSeniorAsset.sub(newSeniorAsset))

capacityPerPool[poolId] = BN.min(capacityGivenMaxReserve, capacityGivenMaxDropRatio)
capacityGivenMaxReservePerPool[poolId] = capacityGivenMaxReserve
capacityGivenMaxDropRatioPerPool[poolId] = capacityGivenMaxDropRatio
capacityPerPool[poolId] = BN.min(capacityGivenMaxReserve, capacityGivenMaxDropRatio)
capacityGivenMaxReservePerPool[poolId] = capacityGivenMaxReserve
capacityGivenMaxDropRatioPerPool[poolId] = capacityGivenMaxDropRatio
} catch (e) {
console.error(e)
}
})

const combined = pools.active.map((p) => {
Expand Down Expand Up @@ -788,7 +792,7 @@ async function getPools(pools: IpfsPools): Promise<{ pools: TinlakePool[] }> {
id: `${id}-1`,
seniority: 1,
balance: data.seniorBalance,
minRiskBuffer: Rate.fromFloat(Dec(1).sub(data.maxSeniorRatio.toDecimal())),
minRiskBuffer: Rate.fromFloat(Dec(1).sub(data.maxSeniorRatio.toDecimal() || Dec(0))),
currentRiskBuffer: Rate.fromFloat(Dec(1).sub(data.seniorRatio.toDecimal())),
interestRatePerSec: data.seniorInterestRate,
lastUpdatedInterest: new Date().toISOString(),
Expand Down

0 comments on commit b17f6c5

Please sign in to comment.