From b055bb572a6aa5c384582ec63e3c6d67835904d2 Mon Sep 17 00:00:00 2001 From: JP Angelle Date: Tue, 21 Nov 2023 18:07:14 -0600 Subject: [PATCH] fix: blocktower write off logic --- tinlake-ui/components/Loan/Data/index.tsx | 15 ++++++++++++- .../utils/useAssetListWriteOffStatus.ts | 22 +++++++++++++++++++ tinlake-ui/utils/useWriteOffPercentage.ts | 11 ++++++++++ tinlake.js/src/actions/admin.ts | 5 +++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/tinlake-ui/components/Loan/Data/index.tsx b/tinlake-ui/components/Loan/Data/index.tsx index e0db7913..7817d818 100644 --- a/tinlake-ui/components/Loan/Data/index.tsx +++ b/tinlake-ui/components/Loan/Data/index.tsx @@ -13,6 +13,7 @@ import { getAddressLink } from '../../../utils/etherscanLinkGenerator' import { Fixed27Base } from '../../../utils/ratios' import { toPrecision } from '../../../utils/toPrecision' import { Asset } from '../../../utils/useAsset' +import { BT_POOL_FEED_CONTRACTS } from '../../../utils/useAssetListWriteOffStatus' import { useLoan } from '../../../utils/useLoan' import { RiskGroup, usePool } from '../../../utils/usePool' import { useWriteOffPercentage } from '../../../utils/useWriteOffPercentage' @@ -95,6 +96,8 @@ const LoanData: React.FC = (props: Props) => { ]) } + const isBTPool = BT_POOL_FEED_CONTRACTS.includes(props.poolConfig.addresses.FEED.toLowerCase()) + return ( <> @@ -198,7 +201,17 @@ const LoanData: React.FC = (props: Props) => { {addThousandsSeparators( - toPrecision(baseToDisplay(loanData?.repaysAggregatedAmount || new BN(0), 18), 2) + isBTPool && writeOffPercentageData === '100' + ? toPrecision( + baseToDisplay( + new BN(loanData?.borrowsAggregatedAmount || new BN(0)).sub( + props.loan?.debt || new BN(0) + ) || new BN(0), + 18 + ), + 2 + ) + : toPrecision(baseToDisplay(loanData?.repaysAggregatedAmount || new BN(0), 18), 2) )}{' '} {props.poolConfig.metadata.currencySymbol || 'DAI'} diff --git a/tinlake-ui/utils/useAssetListWriteOffStatus.ts b/tinlake-ui/utils/useAssetListWriteOffStatus.ts index 001b164c..003ab26f 100644 --- a/tinlake-ui/utils/useAssetListWriteOffStatus.ts +++ b/tinlake-ui/utils/useAssetListWriteOffStatus.ts @@ -19,6 +19,13 @@ const CONTRACTS_WITH_WRITEOFFS_METHOD = [ '0x468eb2408c6f24662a291892550952eb0d70b707', ] +export const BT_POOL_FEED_CONTRACTS = [ + '0x60eeba86ce045d54ce625d71a5c2baebfb2e46e9', + '0x479506bff98b18d62e62862a02a55047ca6583fa', + '0xea5e577df382889497534a0258345e78bbd4e31d', + '0xeff42b6d4527a6a2fb429082386b34f5d4050b2c', +] + export function useAssetListWriteOffStatus(loans: Loan[], addresses: Pool['addresses']) { const tinlake = useTinlake() @@ -71,6 +78,21 @@ export function useAssetListWriteOffStatus(loans: Loan[], addresses: Pool['addre })) } + if (BT_POOL_FEED_CONTRACTS.includes(feedContractAddress.toLowerCase())) { + const isLoanWrittenOffCalls: Call[] = loans.map(({ loanId }) => ({ + target: feedContractAddress, + call: ['isLoanWrittenOff(uint256)(bool)', loanId], + returns: [[loanId]], + })) + + const isLoansWrittenOff = (await multicall(isLoanWrittenOffCalls)) as { [key: string]: BN } + + return loans.map((loan) => ({ + ...loan, + status: isLoansWrittenOff[loan.loanId] ? 'repaid' : loan.status, + })) + } + const writeOffGroups = (await tinlake.getWriteOffGroups()).reduce>( (acc, writeOffGroup, index) => { acc[index] = writeOffGroup.percentage diff --git a/tinlake-ui/utils/useWriteOffPercentage.ts b/tinlake-ui/utils/useWriteOffPercentage.ts index 380df560..b790600c 100644 --- a/tinlake-ui/utils/useWriteOffPercentage.ts +++ b/tinlake-ui/utils/useWriteOffPercentage.ts @@ -2,9 +2,20 @@ import { ITinlake } from '@centrifuge/tinlake-js' import BN from 'bn.js' import { useQuery } from 'react-query' import { useTinlake } from '../components/TinlakeProvider' +import { BT_POOL_FEED_CONTRACTS } from './useAssetListWriteOffStatus' export async function calculateWriteOffPercentage(tinlake: ITinlake, loanId: number) { try { + if (BT_POOL_FEED_CONTRACTS.includes(tinlake.contractAddresses.FEED.toLowerCase() as string)) { + const isLoanWrittenOff = await tinlake.getIsLoanWrittenOff(loanId) + + if (isLoanWrittenOff) { + return '100' + } + + return '0' + } + const rateGroup = await tinlake.getRateGroup(loanId) if (rateGroup.lt(new BN(1000))) { diff --git a/tinlake.js/src/actions/admin.ts b/tinlake.js/src/actions/admin.ts index ee394fab..9430f04e 100644 --- a/tinlake.js/src/actions/admin.ts +++ b/tinlake.js/src/actions/admin.ts @@ -254,6 +254,10 @@ export function AdminActions>(Bas return await this.toBN(this.contract('PILE').loanRates(loanId)) } + getIsLoanWrittenOff = async (loanId: number) => { + return await this.contract('FEED').isLoanWrittenOff(loanId) + } + writeOff = async (loanId: number) => { return this.pending(this.contract('FEED').writeOff(loanId, this.overrides)) } @@ -347,6 +351,7 @@ export type IAdminActions = { getWriteOffPercentage(rateGroup: BN): Promise writeOff(loanId: number): Promise getRateGroup(loanId: number): Promise + getIsLoanWrittenOff(loanId: number): Promise closePool(): Promise unclosePool(): Promise }