Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: blocktower write off logic #39

Merged
merged 6 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions 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 @@ -194,8 +197,16 @@ const LoanData: React.FC<Props> = (props: Props) => {
</TableCell>
</TableRow>
<TableRow>
<TableCell scope="row">Total repaid</TableCell>
<TableCell style={{ textAlign: 'end' }}>
<TableCell
scope="row"
border={isBTPool && writeOffPercentageData === '0' && { color: 'transparent' }}
>
Total repaid
</TableCell>
<TableCell
style={{ textAlign: 'end' }}
border={isBTPool && writeOffPercentageData === '0' && { color: 'transparent' }}
>
<LoadingValue done={!isLoanDataLoading}>
{addThousandsSeparators(
toPrecision(baseToDisplay(loanData?.repaysAggregatedAmount || new BN(0), 18), 2)
Expand All @@ -204,6 +215,19 @@ const LoanData: React.FC<Props> = (props: Props) => {
</LoadingValue>
</TableCell>
</TableRow>
{isBTPool && writeOffPercentageData === '100' && (
<TableRow>
<TableCell scope="row" border={{ color: 'transparent' }}>
Written off
</TableCell>
<TableCell style={{ textAlign: 'end' }} border={{ color: 'transparent' }}>
<LoadingValue done={!isLoanDataLoading}>
{addThousandsSeparators(toPrecision(baseToDisplay(props.loan?.debt || new BN(0), 18), 2))}{' '}
{props.poolConfig.metadata.currencySymbol || 'DAI'}
</LoadingValue>
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</Box>
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
Loading