Skip to content

Commit

Permalink
fix: blocktower write off logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JP Angelle committed Nov 22, 2023
1 parent 17e72cc commit b055bb5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tinlake-ui/components/Loan/Data/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -95,6 +96,8 @@ const LoanData: React.FC<Props> = (props: Props) => {
])
}

const isBTPool = BT_POOL_FEED_CONTRACTS.includes(props.poolConfig.addresses.FEED.toLowerCase())

return (
<>
<Card p="medium" maxWidth={{ medium: 900 }}>
Expand Down Expand Up @@ -198,7 +201,17 @@ const LoanData: React.FC<Props> = (props: Props) => {
<TableCell style={{ textAlign: 'end' }}>
<LoadingValue done={!isLoanDataLoading}>
{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'}
</LoadingValue>
Expand Down
22 changes: 22 additions & 0 deletions tinlake-ui/utils/useAssetListWriteOffStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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<Record<string, BN>>(
(acc, writeOffGroup, index) => {
acc[index] = writeOffGroup.percentage
Expand Down
11 changes: 11 additions & 0 deletions tinlake-ui/utils/useWriteOffPercentage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
5 changes: 5 additions & 0 deletions tinlake.js/src/actions/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ export function AdminActions<ActionsBase extends Constructor<TinlakeParams>>(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))
}
Expand Down Expand Up @@ -347,6 +351,7 @@ export type IAdminActions = {
getWriteOffPercentage(rateGroup: BN): Promise<BN>
writeOff(loanId: number): Promise<PendingTransaction>
getRateGroup(loanId: number): Promise<BN>
getIsLoanWrittenOff(loanId: number): Promise<boolean>
closePool(): Promise<PendingTransaction>
unclosePool(): Promise<PendingTransaction>
}
Expand Down

0 comments on commit b055bb5

Please sign in to comment.