-
Notifications
You must be signed in to change notification settings - Fork 3
feat: liquidation #403
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
Merged
Merged
feat: liquidation #403
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e1e1538
feat: liquidation temp
antoncoding 9907b91
feat: ui
antoncoding 67a025c
chore: review fix
antoncoding 5da160e
chore: fix review
antoncoding 4bf45a1
chore: liquidation
antoncoding 698b8d5
chore: rewview fixes
antoncoding 58127e2
feat: estimate repay
antoncoding d6e116a
chore: fix col span
antoncoding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { useCallback } from 'react'; | ||
| import { type Address, encodeFunctionData } from 'viem'; | ||
| import { useConnection } from 'wagmi'; | ||
| import morphoAbi from '@/abis/morpho'; | ||
| import { formatBalance } from '@/utils/balance'; | ||
| import { getMorphoAddress } from '@/utils/morpho'; | ||
| import type { Market } from '@/utils/types'; | ||
| import { useERC20Approval } from './useERC20Approval'; | ||
| import { useTransactionWithToast } from './useTransactionWithToast'; | ||
| import { useTransactionTracking } from './useTransactionTracking'; | ||
|
|
||
| type UseLiquidateTransactionProps = { | ||
| market: Market; | ||
| borrower: Address; | ||
| seizedAssets: bigint; | ||
| repaidShares: bigint; | ||
| estimatedRepaidAmount: bigint; // raw loan token estimate before approval buffer | ||
| onSuccess?: () => void; | ||
| }; | ||
|
|
||
| const APPROVAL_BUFFER_BPS = 400n; | ||
| const BPS_SCALE = 10_000n; | ||
|
|
||
| const addBufferBpsUp = (amount: bigint, bps: bigint): bigint => { | ||
| if (amount === 0n) return 0n; | ||
| return (amount * (BPS_SCALE + bps) + (BPS_SCALE - 1n)) / BPS_SCALE; | ||
| }; | ||
|
|
||
| export function useLiquidateTransaction({ | ||
| market, | ||
| borrower, | ||
| seizedAssets, | ||
| repaidShares, | ||
| estimatedRepaidAmount, | ||
| onSuccess, | ||
| }: UseLiquidateTransactionProps) { | ||
| const { address: account, chainId } = useConnection(); | ||
|
|
||
| const tracking = useTransactionTracking('liquidate'); | ||
| const morphoAddress = chainId ? getMorphoAddress(chainId) : undefined; | ||
| const hasSeizedAssets = seizedAssets > 0n; | ||
| const hasRepaidShares = repaidShares > 0n; | ||
| const hasExactlyOneLiquidationMode = hasSeizedAssets !== hasRepaidShares; | ||
|
|
||
| // Liquidation repays debt in both modes: | ||
| // - repaidShares > 0 (max/share-based) | ||
| // - seizedAssets > 0 (asset-based) | ||
| const approvalAmount = hasExactlyOneLiquidationMode ? addBufferBpsUp(estimatedRepaidAmount, APPROVAL_BUFFER_BPS) : 0n; | ||
|
|
||
| const { isApproved, approve } = useERC20Approval({ | ||
| token: market.loanAsset.address as Address, | ||
| spender: morphoAddress ?? '0x', | ||
| amount: approvalAmount, | ||
| tokenSymbol: market.loanAsset.symbol, | ||
| chainId, | ||
| }); | ||
|
|
||
| const { isConfirming: liquidatePending, sendTransactionAsync } = useTransactionWithToast({ | ||
| toastId: 'liquidate', | ||
| pendingText: `Liquidating ${formatBalance(seizedAssets, market.collateralAsset.decimals)} ${market.collateralAsset.symbol}`, | ||
| successText: 'Liquidation successful', | ||
| errorText: 'Failed to liquidate', | ||
| chainId, | ||
| pendingDescription: `Liquidating borrower ${borrower.slice(0, 6)}...`, | ||
| successDescription: `Successfully liquidated ${borrower.slice(0, 6)}`, | ||
| onSuccess, | ||
| ...tracking, | ||
| }); | ||
|
|
||
| const liquidate = useCallback(async () => { | ||
| if (!account || !chainId || !morphoAddress) return; | ||
| if (!hasExactlyOneLiquidationMode) { | ||
| throw new Error('Invalid liquidation params: exactly one of seizedAssets or repaidShares must be non-zero'); | ||
| } | ||
|
|
||
| const marketParams = { | ||
| loanToken: market.loanAsset.address as `0x${string}`, | ||
| collateralToken: market.collateralAsset.address as `0x${string}`, | ||
| oracle: market.oracleAddress as `0x${string}`, | ||
| irm: market.irmAddress as `0x${string}`, | ||
| lltv: BigInt(market.lltv), | ||
| }; | ||
|
|
||
| const liquidateTx = encodeFunctionData({ | ||
| abi: morphoAbi, | ||
| functionName: 'liquidate', | ||
| args: [marketParams, borrower, hasSeizedAssets ? seizedAssets : 0n, hasRepaidShares ? repaidShares : 0n, '0x'], | ||
| }); | ||
|
|
||
| await sendTransactionAsync({ to: morphoAddress as Address, data: liquidateTx }); | ||
| }, [ | ||
| account, | ||
| chainId, | ||
| market, | ||
| borrower, | ||
| hasExactlyOneLiquidationMode, | ||
| hasSeizedAssets, | ||
| seizedAssets, | ||
| hasRepaidShares, | ||
| repaidShares, | ||
| morphoAddress, | ||
| sendTransactionAsync, | ||
| ]); | ||
|
|
||
| const handleLiquidate = useCallback(async () => { | ||
| if (!hasExactlyOneLiquidationMode) { | ||
| throw new Error('Invalid liquidation params: exactly one of seizedAssets or repaidShares must be non-zero'); | ||
| } | ||
| if (!isApproved) await approve(); | ||
| await liquidate(); | ||
| }, [hasExactlyOneLiquidationMode, isApproved, approve, liquidate]); | ||
|
|
||
| return { liquidatePending, liquidate, handleLiquidate }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.