diff --git a/centrifuge-app/src/utils/tinlake/useTinlakePools.ts b/centrifuge-app/src/utils/tinlake/useTinlakePools.ts index 50c924761c..e0e994d3c4 100644 --- a/centrifuge-app/src/utils/tinlake/useTinlakePools.ts +++ b/centrifuge-app/src/utils/tinlake/useTinlakePools.ts @@ -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) => { @@ -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(),