Skip to content

Commit

Permalink
Display BTC and fees in UnmintDetails
Browse files Browse the repository at this point in the history
Display estimated BTC that will be received and fees in UnmintDetails. Besides
that I've added `isEstimated` parameter to `formatTokenAmount` function - it
displays the tilde next to the amount no matter the value.
  • Loading branch information
michalsmiarowski committed Jul 13, 2023
1 parent c729940 commit 0aad890
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/components/Modal/tBTC/InitiateUnminting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ const InitiateUnmintingBase: FC<InitiateUnmintingProps> = ({
<ModalBody>
<InfoBox variant="modal" mb="6">
<H5>
Through unminting you will get back{" ~"}
Through unminting you will get back{" "}
<InlineTokenBalance
tokenSymbol="BTC"
tokenDecimals={8}
precision={6}
higherPrecision={8}
tokenAmount={estimatedBTCAmount}
displayTildeBelow={0}
isEstimated
/>{" "}
BTC
</H5>
Expand Down
9 changes: 7 additions & 2 deletions src/components/TokenBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface TokenBalanceProps {
precision?: number
higherPrecision?: number
displayTildeBelow?: number
isEstimated?: boolean
}

export const InlineTokenBalance: FC<TokenBalanceProps & BoxProps> = ({
Expand All @@ -43,6 +44,7 @@ export const InlineTokenBalance: FC<TokenBalanceProps & BoxProps> = ({
higherPrecision = 6,
withHigherPrecision,
displayTildeBelow = 1,
isEstimated = false,
...restProps
}) => {
const _tokenAmount = useMemo(() => {
Expand All @@ -51,7 +53,8 @@ export const InlineTokenBalance: FC<TokenBalanceProps & BoxProps> = ({
tokenFormat,
tokenDecimals,
precision,
displayTildeBelow
displayTildeBelow,
isEstimated
)
}, [tokenAmount, tokenFormat, tokenDecimals, precision])

Expand All @@ -60,7 +63,9 @@ export const InlineTokenBalance: FC<TokenBalanceProps & BoxProps> = ({
tokenAmount || 0,
tokenFormat,
tokenDecimals,
higherPrecision
higherPrecision,
1,
isEstimated
)
}, [tokenAmount, tokenFormat, tokenDecimals, higherPrecision])

Expand Down
3 changes: 3 additions & 0 deletions src/hooks/tbtc/useFetchRedemptionDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface RedemptionDetails {
}
requestedAt: number
completedAt?: number
treasuryFee: string
isTimedOut: boolean
redemptionTimedOutTxHash?: string
btcAddress?: string
Expand Down Expand Up @@ -158,6 +159,7 @@ export const useFetchRedemptionDetails = (
redemptionCompletedTxHash: undefined,
requestedAt: requestedAt,
redemptionTimedOutTxHash: timedOutTxHash,
treasuryFee: redemptionRequest.treasuryFee,
isTimedOut,
})
return
Expand Down Expand Up @@ -205,6 +207,7 @@ export const useFetchRedemptionDetails = (
},
requestedAt: redemptionRequestedEventTimestamp,
completedAt: redemptionCompletedTimestamp,
treasuryFee: redemptionRequest.treasuryFee,
isTimedOut: false,
// TODO: convert the `scriptPubKey` to address.
btcAddress: "2Mzs2YNphdHmBoE7SE77cGB57JBXveNGtae",
Expand Down
35 changes: 24 additions & 11 deletions src/pages/tBTC/Bridge/UnmintDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { ProcessCompletedBrandGradientIcon } from "./components/BridgeProcessDet
import { featureFlags } from "../../../constants"
import { useFetchRedemptionDetails } from "../../../hooks/tbtc/useFetchRedemptionDetails"
import { BridgeProcessDetailsPageSkeleton } from "./components/BridgeProcessDetailsPageSkeleton"
import { BigNumber } from "ethers"

const pendingRedemption = {
redemptionRequestedTxHash:
Expand Down Expand Up @@ -95,9 +96,15 @@ export const UnmintDetails: PageComponent = () => {
}, [btcTxHash])

const isProcessCompleted = !!data?.redemptionCompletedTxHash?.bitcoin
const unmintedAmount = data?.amount ?? "0"
// requested unmint amount of tBTC in satoshi!. This means that this value is
// the amount before subtracting the fees.
const unmintAmount = data?.amount ?? "0"

const thresholdNetworkFee = data?.treasuryFee ?? "0"
const estimatedAmountToBeReceived = BigNumber.from(unmintAmount)
.sub(thresholdNetworkFee)
.toString()
const btcAddress = data?.btcAddress
const fee = "20000000000000000"
const time = dateAs(
(data?.completedAt ?? dateToUnixTimestamp()) - (data?.requestedAt ?? 0)
)
Expand Down Expand Up @@ -145,12 +152,15 @@ export const UnmintDetails: PageComponent = () => {
</Box>
)}
<InlineTokenBalance
tokenAmount={unmintedAmount}
tokenAmount={estimatedAmountToBeReceived}
withSymbol
tokenSymbol="tBTC"
tokenSymbol="BTC"
ml="auto"
tokenDecimals={8}
precision={6}
higherPrecision={8}
withHigherPrecision
isEstimated
/>
</BridgeProcessCardSubTitle>
<Timeline>
Expand Down Expand Up @@ -214,8 +224,8 @@ export const UnmintDetails: PageComponent = () => {
</Timeline>
{shouldDisplaySuccessStep || isProcessCompleted ? (
<SuccessStep
unmintedAmount={unmintedAmount}
thresholdNetworkFee={fee}
estimatedAmountToBeReceived={estimatedAmountToBeReceived}
thresholdNetworkFee={thresholdNetworkFee}
btcAddress={btcAddress!}
/>
) : (
Expand Down Expand Up @@ -290,27 +300,30 @@ export const UnmintDetails: PageComponent = () => {
}

const SuccessStep: FC<{
unmintedAmount: string
estimatedAmountToBeReceived: string
thresholdNetworkFee: string
btcAddress: string
}> = ({ unmintedAmount, thresholdNetworkFee, btcAddress }) => {
}> = ({ estimatedAmountToBeReceived, thresholdNetworkFee, btcAddress }) => {
return (
<>
<H5 mt="4">Success!</H5>
<Divider mt="9" mb="4" />
<List spacing="4">
<TransactionDetailsAmountItem
label="Unminted Amount"
tokenAmount={unmintedAmount}
tokenSymbol="tBTC"
tokenAmount={estimatedAmountToBeReceived}
tokenSymbol="BTC"
tokenDecimals={8}
precision={6}
higherPrecision={8}
withHigherPrecision
isEstimated
/>
<TransactionDetailsAmountItem
label="Threshold Network Fee"
tokenAmount={thresholdNetworkFee}
tokenSymbol="tBTC"
tokenSymbol="BTC"
tokenDecimals={8}
precision={6}
higherPrecision={8}
/>
Expand Down
8 changes: 5 additions & 3 deletions src/utils/formatAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const formatTokenAmount = (
format = "0,00.[0]0",
decimals = 18,
precision = 2,
displayTildeBelow = 1
displayTildeBelow = 1,
isEstimated = false // adds tilde to the amount no matter the value
) => {
const minAmountToDisplay = BigNumber.from(10).pow(decimals - precision)
const _rawAmount = BigNumber.from(rawAmount)
Expand Down Expand Up @@ -39,9 +40,10 @@ export const formatTokenAmount = (
const amountFromFormattedValue = numeral(formattedAmount).value() ?? 0

if (
Number.parseFloat(amountFromFormattedValue.toString()) !==
isEstimated ||
(Number.parseFloat(amountFromFormattedValue.toString()) !==
Number.parseFloat(tokenAmountInHumanReadableFormat) &&
displayTildeBelow > amountFromFormattedValue
displayTildeBelow > amountFromFormattedValue)
) {
return `~${formattedAmount}`
}
Expand Down

0 comments on commit 0aad890

Please sign in to comment.