Skip to content

Commit 71a6f95

Browse files
authored
Merge pull request #869 from ionicprotocol/fix/repay-dust
2 parents f5dbe84 + 9e7a982 commit 71a6f95

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

packages/ui/hooks/market/useBorrow.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ export const useBorrow = ({
4141
const { refetch: refetchMaxBorrow, data: maxBorrowAmount } =
4242
useMaxBorrowAmount(selectedMarketData, comptrollerAddress, chainId);
4343

44-
const { addStepsForAction, transactionSteps, upsertTransactionStep } =
45-
useTransactionSteps();
4644
const { currentSdk, address } = useMultiIonic();
4745

4846
const amountAsBInt = useMemo(

packages/ui/hooks/market/useRepay.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export const useRepay = ({
4242
const { addStepsForType, upsertStepForType } = useManageDialogContext();
4343
const { transactionSteps } = useTransactionSteps();
4444

45+
const INTEREST_BUFFER_MULTIPLIER = 1.01;
46+
4547
const amountAsBInt = useMemo(
4648
() =>
4749
parseUnits(
@@ -51,6 +53,17 @@ export const useRepay = ({
5153
[amount, selectedMarketData.underlyingDecimals]
5254
);
5355

56+
const amountWithBufferAsBInt = useMemo(() => {
57+
const amountAsNumber = Number(amount);
58+
if (isNaN(amountAsNumber) || amountAsNumber === 0) return 0n;
59+
60+
const amountWithBuffer = amountAsNumber * INTEREST_BUFFER_MULTIPLIER;
61+
return parseUnits(
62+
amountWithBuffer.toString(),
63+
selectedMarketData.underlyingDecimals
64+
);
65+
}, [amount, selectedMarketData.underlyingDecimals]);
66+
5467
const handleUtilization = useCallback(
5568
(newUtilizationPercentage: number) => {
5669
const maxAmountNumber = Number(
@@ -124,11 +137,12 @@ export const useRepay = ({
124137
selectedMarketData.cToken
125138
]);
126139

127-
if (currentAllowance < amountAsBInt) {
140+
// Use buffered amount for allowance check and approval
141+
if (currentAllowance < amountWithBufferAsBInt) {
128142
const approveTx = await currentSdk.approve(
129143
selectedMarketData.cToken,
130144
selectedMarketData.underlyingToken,
131-
amountAsBInt
145+
amountWithBufferAsBInt // Using buffered amount for approval
132146
);
133147

134148
upsertStepForType(TransactionType.REPAY, {
@@ -156,12 +170,12 @@ export const useRepay = ({
156170
amountAsBInt >= (selectedMarketData.borrowBalance ?? 0n);
157171
const repayAmount = isRepayingMax ? maxUint256 : amountAsBInt;
158172

159-
// Verify final allowance
173+
// Verify final allowance against buffered amount
160174
const finalAllowance = await token.read.allowance([
161175
address,
162176
selectedMarketData.cToken
163177
]);
164-
if (finalAllowance < amountAsBInt) {
178+
if (finalAllowance < amountWithBufferAsBInt) {
165179
throw new Error('Insufficient allowance after approval');
166180
}
167181

@@ -244,6 +258,7 @@ export const useRepay = ({
244258
address,
245259
amount,
246260
amountAsBInt,
261+
amountWithBufferAsBInt,
247262
currentBorrowAmountAsFloat,
248263
selectedMarketData,
249264
addStepsForType,

0 commit comments

Comments
 (0)