Skip to content

Commit

Permalink
Revert "feat(dev-frontend): 🎸 replace redemption details w/ plain inf…
Browse files Browse the repository at this point in the history
…o text"

This reverts commit db4e103.
  • Loading branch information
apttx committed Feb 4, 2025
1 parent db4e103 commit d84e6d8
Showing 1 changed file with 85 additions and 4 deletions.
89 changes: 85 additions & 4 deletions packages/dev-frontend/src/components/RedeemHchf/RedeemHchf.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from "react";
import { Flex, Button, Box, Card, Heading } from "theme-ui";
import { Decimal } from "@liquity/lib-base";
import React, { useEffect, useState } from "react";
import { Flex, Button, Box, Card, Heading, Spinner } from "theme-ui";
import { Decimal, getRedemptionDetails, Percent } from "@liquity/lib-base";
import { useLiquitySelector } from "@liquity/lib-react";

import { ActionDescription, Amount } from "../ActionDescription";
Expand All @@ -15,6 +15,7 @@ import { LoadingButton } from "../LoadingButton";
import { EditableRow } from "../Trove/Editor";
import { ErrorDescription } from "../ErrorDescription";
import { useConstants } from "../../hooks/constants";
import { InfoIcon } from "../InfoIcon";

const TRANSACTION_ID = "trove-adjustment";

Expand Down Expand Up @@ -91,6 +92,64 @@ export const RedeemHchf: React.FC = () => {

const editingState = useState<string>();

const [areRedemptionDetailsLoading, setAreRedemptionDetailsLoading] = useState(false);
const [redemptionDetails, setRedemptionDetails] = useState<
| (ReturnType<typeof getRedemptionDetails> & { redemptionFeePercent: Percent<Decimal, Decimal> })
| null
>();
useEffect(() => {
let mounted = true;
setRedemptionDetails(null);
setAreRedemptionDetailsLoading(true);

if (amountOfHchfToRedeem.isZero) {
setRedemptionDetails(null);
setAreRedemptionDetailsLoading(false);
return;
}

Promise.all([
liquity.getFees(),
liquity.getTotal(),
liquity.getTroves({ sortedBy: "ascendingCollateralRatio", first: 1000, startingAt: 0 })
])

.then(([fees, total, troves]) => {
if (!mounted) {
return;
}

const redeemedFractionOfSupply = amountOfHchfToRedeem.div(total.debt);
const redemptionFee = fees.redemptionRate(redeemedFractionOfSupply);
const redemptionDetails = getRedemptionDetails({
redeemedHchf: amountOfHchfToRedeem,
redemptionFee,
totalHbar: total.collateral,
totalHchf: total.debt,
sortedTroves: troves
});

if (!redemptionDetails) {
setRedemptionDetails(null);
setAreRedemptionDetailsLoading(false);
return;
}

const redemptionFeePercent = new Percent<Decimal, Decimal>(redemptionFee);
const extendedRedemptionDetails = {
...redemptionDetails,
redemptionFeePercent
};

setRedemptionDetails(extendedRedemptionDetails);
setAreRedemptionDetailsLoading(false);
});

return () => {
mounted = false;
};
}, [liquity, amountOfHchfToRedeem]);

return (
<Card>
<Heading
Expand Down Expand Up @@ -147,7 +206,29 @@ export const RedeemHchf: React.FC = () => {
</ErrorDescription>
)}

<ActionDescription>{redemptionInformation}</ActionDescription>
<ActionDescription
icon={areRedemptionDetailsLoading && <Spinner sx={{ flex: "1.3333rem 0 0" }} />}
>
{redemptionDetails ? (
<>
You will redeem {redemptionDetails.redeemedHchf.prettify(2)} HCHF for{" "}
{redemptionDetails.receivedHbar.prettify(2)} HBAR for a fee of{" "}
{redemptionDetails.redemptionFeeInHbar.prettify(2)} HBAR (
{redemptionDetails.redemptionFeePercent.prettify()}).
<InfoIcon
tooltip={
<>
These numbers are an approximation.
<br />
{redemptionInformation}
</>
}
/>
</>
) : (
redemptionInformation
)}
</ActionDescription>

<Flex variant="layout.actions">
{needsSpenderApproval && !hchfContractHasHchfTokenAllowance ? (
Expand Down

0 comments on commit d84e6d8

Please sign in to comment.