Skip to content

Commit

Permalink
chore: repay input field adjustments to avoid user errors
Browse files Browse the repository at this point in the history
  • Loading branch information
martines3000 committed Jan 19, 2025
1 parent 6858cfe commit 62e0f0b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 47 deletions.
60 changes: 36 additions & 24 deletions apps/frontend/src/components/InputDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ import { Button } from '../ui/button';
import { InputField } from './InputField';
import { PositionSummary } from './PositionSummary';

const getOverPayAmount = (
amount: BigNumber,
assetPrice: BigNumber
): BigNumber => {
const assetPriceInUsd = amount.times(assetPrice);

let overPayAmount;

// Overpay 0.01 USD
if (assetPriceInUsd.lte(10)) {
overPayAmount = new BigNumber(0.01).dividedBy(assetPrice);
} else if (assetPriceInUsd.lte(100)) {
overPayAmount = new BigNumber(0.1).dividedBy(assetPrice);
} else {
overPayAmount = new BigNumber(1).dividedBy(assetPrice);
}

return overPayAmount;
};

export const InputDialog = () => {
const { account } = useAccount();
const { isConnected } = useIsConnected();
Expand Down Expand Up @@ -180,16 +200,17 @@ export const InputDialog = () => {
}

if (action === 'REPAY') {
// Repay 1 cent more than owed to avoid staying in debt

return formatUnits(
const owed = formatUnits(
userSupplyBorrow.borrowed,
marketConfiguration.baseTokenDecimals
).plus(
BigNumber(0.001).div(
priceData?.prices[marketConfiguration.baseToken.bits] ?? 1
)
);

const overPayAmount = getOverPayAmount(
owed,
priceData.prices[marketConfiguration.baseToken.bits] ?? 1
);

return owed.plus(overPayAmount);
}
if (action === 'SUPPLY') {
if (actionTokenAssetId === marketConfiguration.baseToken.bits) {
Expand Down Expand Up @@ -434,22 +455,12 @@ export const InputDialog = () => {
marketConfiguration?.baseTokenDecimals
);

const userBorrowedModified =
userBorrowed.plus(
BigNumber(0.002).div(
priceData?.prices[marketConfiguration.baseToken.bits] ?? 1
)
) ?? BigNumber(0);
const overPayAmount = getOverPayAmount(
userBorrowed,
priceData.prices[marketConfiguration.baseToken.bits] ?? 1
);

const userBorrowedModifiedRepay =
userBorrowed
.plus(
BigNumber(0.001).div(
priceData?.prices[marketConfiguration.baseToken.bits] ?? 1
)
)
.decimalPlaces(marketConfiguration?.baseTokenDecimals) ??
BigNumber(0);
// const userBorrowedModified = userBorrowed.plus(overPayAmount);

const minOpenPositionValue = BigNumber(
marketConfiguration.baseBorrowMin.toString()
Expand All @@ -462,8 +473,9 @@ export const InputDialog = () => {
return `Your position must be at least ${minOpenPositionValue.toFixed()} ${SYMBOL_TO_NAME[appConfig.assets[marketConfiguration.baseToken.bits]]}. Please repay the entire amount or keep at least ${minOpenPositionValue.toFixed()} ${SYMBOL_TO_NAME[appConfig.assets[marketConfiguration.baseToken.bits]]} in your position.`;
}

if (tokenAmount.gt(userBorrowedModified))
return 'You are trying to repay more than your debt';
// if (tokenAmount.gt(userBorrowedModified)) {
// return 'You are trying to repay more than your debt';
// }
}

return null;
Expand Down
7 changes: 4 additions & 3 deletions apps/frontend/src/hooks/usePrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ export const usePrice = (marketParam?: string) => {
priceUpdateData,
};
},
refetchInterval: 3000,
refetchInterval: 5000,
enabled:
!!provider &&
!!priceFeedIdToAssetId &&
!!marketContract &&
!!pythContract,
staleTime: 3000,
refetchOnWindowFocus: false,
staleTime: 5000,
refetchOnWindowFocus: true,
refetchIntervalInBackground: true,
});
};
60 changes: 40 additions & 20 deletions apps/frontend/src/hooks/useUserRole.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useIsConnected } from '@fuels/react';
import BigNumber from 'bignumber.js';
import { useMemo } from 'react';
import { useMarketConfiguration } from './useMarketConfiguration';
import { usePrice } from './usePrice';
import { useUserSupplyBorrow } from './useUserSupplyBorrow';

export enum USER_ROLE {
Expand All @@ -13,26 +15,44 @@ export const useUserRole = () => {
const { isConnected } = useIsConnected();
const { data: userSupplyBorrow } = useUserSupplyBorrow();
const { data: marketConfiguration } = useMarketConfiguration();
const { data: priceData } = usePrice();

return useMemo(() => {
if (
!isConnected ||
!userSupplyBorrow ||
!marketConfiguration ||
!priceData
) {
return USER_ROLE.NONE;
}

// Supply treshold is $0.1
const supplyTreshold = BigNumber(0.1).dividedBy(
priceData.prices[marketConfiguration.baseToken.bits] ?? 1
);

// Borrow treshold is $10
const borrowTreshold = BigNumber(0.1).dividedBy(
priceData.prices[marketConfiguration.baseToken.bits] ?? 1
);

const userSuppliedUsd = userSupplyBorrow.supplied
.times(priceData.prices[marketConfiguration.baseToken.bits] ?? 1)
.dividedBy(BigNumber(10).pow(marketConfiguration.baseTokenDecimals));

const userBorrowedUsd = userSupplyBorrow.borrowed
.times(priceData.prices[marketConfiguration.baseToken.bits] ?? 1)
.dividedBy(BigNumber(10).pow(marketConfiguration.baseTokenDecimals));

if (userSuppliedUsd.gte(supplyTreshold)) {
return USER_ROLE.LENDER;
}

if (userBorrowedUsd.gte(borrowTreshold)) {
return USER_ROLE.BORROWER;
}

if (!isConnected || !userSupplyBorrow || !marketConfiguration) {
return USER_ROLE.NONE;
}

const supplyTreshold = BigNumber(0.1).times(
BigNumber(10).pow(marketConfiguration.baseTokenDecimals)
);

const borrowTreshold = BigNumber(0.1).times(
BigNumber(10).pow(marketConfiguration.baseTokenDecimals)
);

if (userSupplyBorrow.supplied.gt(supplyTreshold)) {
return USER_ROLE.LENDER;
}

if (userSupplyBorrow.borrowed.gt(borrowTreshold)) {
return USER_ROLE.BORROWER;
}

return USER_ROLE.NONE;
}, [isConnected, userSupplyBorrow, marketConfiguration, priceData]);
};

0 comments on commit 62e0f0b

Please sign in to comment.