Skip to content

Commit

Permalink
Merge pull request #272 from berachain/fix/honey/allowance-badAsset
Browse files Browse the repository at this point in the history
Fix/honey/allowance bad asset
  • Loading branch information
SnowBera authored Dec 21, 2024
2 parents 5f51392 + b939a6d commit 04a6c06
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
28 changes: 22 additions & 6 deletions apps/honey/src/components/swap-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import { Card, CardContent, CardHeader, CardTitle } from "@bera/ui/card";
import { Icons } from "@bera/ui/icons";
import { Skeleton } from "@bera/ui/skeleton";
import { Tabs, TabsList, TabsTrigger } from "@bera/ui/tabs";
import { Address, parseUnits } from "viem";
import { Address, formatUnits, parseUnits } from "viem";

import { usePsm } from "~/hooks/usePsm";
import { cn } from "@bera/ui";
import BigNumber from "bignumber.js";

export function SwapCard() {
const [tabValue, setTabValue] = useState<"mint" | "burn">("mint");
Expand Down Expand Up @@ -68,6 +69,13 @@ export function SwapCard() {
setToAmount(newToAmounts);
};

const userFriendlyAmount = (
amount: string | undefined,
): string | undefined => {
if (!amount) return undefined;
return new BigNumber(amount).decimalPlaces(2, 1).toString();
};

return (
<div className="w-full">
<Card className="relative z-10 m-auto block w-full max-w-[500px] bg-background shadow-2xl">
Expand All @@ -79,7 +87,7 @@ export function SwapCard() {
<Skeleton className="absolute right-6 top-5 h-6 w-40" />
) : (
<div className="absolute right-6 top-5 text-base font-medium text-muted-foreground">
Static fee of <FormattedNumber value={fee ?? 0} />%
Static fee of <FormattedNumber value={fee ?? 0} percent />
</div>
)}
</CardTitle>
Expand Down Expand Up @@ -119,7 +127,9 @@ export function SwapCard() {
)}
>
<TokenInput
amount={fromAmount[selectedFrom?.[0]?.address!]}
amount={userFriendlyAmount(
fromAmount[selectedFrom?.[0]?.address!],
)}
balance={fromBalance?.[0]}
selected={selectedFrom?.[0]}
selectable={selectedFrom?.[0]?.address !== honey?.address}
Expand Down Expand Up @@ -155,7 +165,9 @@ export function SwapCard() {
<>
<hr />
<TokenInput
amount={fromAmount[selectedFrom?.[1]?.address!]}
amount={userFriendlyAmount(
fromAmount[selectedFrom?.[1]?.address!],
)}
balance={fromBalance?.[1]}
selected={selectedFrom?.[1]}
selectable={selectedFrom?.[1]?.address !== honey?.address}
Expand Down Expand Up @@ -198,7 +210,9 @@ export function SwapCard() {
)}
>
<TokenInput
amount={toAmount[selectedTo?.[0]?.address!]}
amount={userFriendlyAmount(
toAmount[selectedTo?.[0]?.address!],
)}
balance={toBalance?.[0]}
selected={selectedTo?.[0]}
selectable={selectedTo?.[0]?.address !== honey?.address}
Expand Down Expand Up @@ -236,7 +250,9 @@ export function SwapCard() {
<>
<hr />
<TokenInput
amount={toAmount[selectedTo?.[1]?.address!]}
amount={userFriendlyAmount(
toAmount[selectedTo?.[1]?.address!],
)}
balance={toBalance?.[1]}
selected={selectedTo?.[1]}
selectable={selectedTo?.[1]?.address !== honey?.address}
Expand Down
26 changes: 10 additions & 16 deletions apps/honey/src/hooks/usePsm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,12 @@ export const usePsm = (): PsmHookReturn => {
const isBadCollateral1 = useIsBadCollateralAsset({
collateral: collaterals[0]?.address,
});
const isBadCollateral2 = useIsBadCollateralAsset({
collateral: collaterals[1]?.address,
});
const isBadCollateral =
isBadCollateral1.data?.isBlacklisted ||
isBadCollateral1.data?.isDepegged ||
isBadCollateral2.data?.isBlacklisted ||
isBadCollateral2.data?.isDepegged;
// const isBadCollateral2 = useIsBadCollateralAsset({
// collateral: collaterals[1]?.address,
// });
const isBadCollateral = isBasketModeEnabled
? false
: isBadCollateral1.data?.isBlacklisted || isBadCollateral1.data?.isDepegged;

// ===== TOKEN BALANCES =====
// retrieve token balances for both input and output tokens
Expand Down Expand Up @@ -297,16 +295,14 @@ export const usePsm = (): PsmHookReturn => {
// User input collateral amount (fromAmount)
// Set the resulting Honey amount
setToAmount({
[honey?.address!]: new BigNumber(formatUnits(previewRes.honey, 18))
.decimalPlaces(2, 1)
.toString(),
[honey?.address!]: formatUnits(previewRes.honey, 18),
});

// In basket mode, update all collaterals except the one user is currently modifying
if (previewBasketMode && changedAsset) {
setFromAmount((prevColl) => ({
...newCollaterals,
[changedAsset]: Number(prevColl[changedAsset]).toFixed(2),
[changedAsset]: prevColl[changedAsset],
}));
}
} else {
Expand All @@ -325,16 +321,14 @@ export const usePsm = (): PsmHookReturn => {
// User input collateral amount (toAmount)
// Set required Honey amount
setFromAmount({
[honey?.address!]: new BigNumber(formatUnits(previewRes.honey, 18))
.decimalPlaces(2, 1)
.toString(),
[honey?.address!]: formatUnits(previewRes.honey, 18),
});

// In basket mode, update all collaterals except the one user is currently modifying
if (previewBasketMode && changedAsset) {
setToAmount((prevColl) => ({
...newCollaterals,
[changedAsset]: Number(prevColl[changedAsset]).toFixed(2),
[changedAsset]: prevColl[changedAsset],
}));
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/berajs/src/actions/honey/getCollateralRates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export const getCollateralRates = async ({
for (const coll of collateralList) {
const collIdx = collateralList.indexOf(coll);
collateralRates.single[coll] = {
mintFee: Number(formatUnits(mintRates[collIdx], 18)),
redeemFee: Number(formatUnits(redeemRates[collIdx], 18)),
mintFee: 1 - +formatUnits(mintRates[collIdx], 18),
redeemFee: 1 - +formatUnits(redeemRates[collIdx], 18),
};
totalWeightedMintCollateralsFee +=
collateralRates.single[coll].mintFee *
Expand Down

0 comments on commit 04a6c06

Please sign in to comment.