Skip to content

Commit

Permalink
Implement fees ceiling
Browse files Browse the repository at this point in the history
  • Loading branch information
kpyszkowski committed Aug 13, 2024
1 parent 7a3ee4a commit 8054c94
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useTokenAmountField } from "#/components/shared/TokenAmountForm/TokenAm
import { FeesTooltip } from "#/components/TransactionModal/FeesTooltip"
import { useMinDepositAmount, useTransactionDetails } from "#/hooks"
import { CurrencyType } from "#/types"
import { DESIRED_DECIMALS_FOR_FEE } from "#/constants"
import { DESIRED_DECIMALS_FOR_FEE, FEE_CEIL_PRECISION } from "#/constants"

function StakeDetails({ currency }: { currency: CurrencyType }) {
const { value = 0n } = useTokenAmountField()
Expand Down Expand Up @@ -36,6 +36,7 @@ function StakeDetails({ currency }: { currency: CurrencyType }) {
currency,
amount: total,
desiredDecimals: DESIRED_DECIMALS_FOR_FEE,
ceilPrecision: FEE_CEIL_PRECISION,
}}
to={{
currency: "usd",
Expand Down
6 changes: 4 additions & 2 deletions dapp/src/components/shared/CurrencyBalance/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type CurrencyBalanceProps = {
amount?: AmountType
shouldBeFormatted?: boolean
desiredDecimals?: number
ceilPrecision?: number
size?: ResponsiveValue<string>
variant?: ResponsiveValue<
| "greater-balance-md"
Expand All @@ -34,6 +35,7 @@ export function CurrencyBalance({
amount,
shouldBeFormatted = true,
desiredDecimals: customDesiredDecimals,
ceilPrecision,
size,
variant,
balanceFontWeight = "bold",
Expand All @@ -58,10 +60,10 @@ export function CurrencyBalance({
const balance = useMemo(() => {
const value = amount ?? 0
if (shouldBeFormatted || typeof value === "bigint")
return formatTokenAmount(value, decimals, desiredDecimals)
return formatTokenAmount(value, decimals, desiredDecimals, ceilPrecision)

return numberToLocaleString(value, desiredDecimals)
}, [amount, decimals, desiredDecimals, shouldBeFormatted])
}, [amount, decimals, desiredDecimals, shouldBeFormatted, ceilPrecision])

return (
<Box as={as} __css={styles.container}>
Expand Down
1 change: 1 addition & 0 deletions dapp/src/constants/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Currency, CurrencyType } from "#/types"
import env from "./env"

export const DESIRED_DECIMALS_FOR_FEE = 5
export const FEE_CEIL_PRECISION = 4

export const BITCOIN: Currency = {
name: "Bitcoin",
Expand Down
8 changes: 7 additions & 1 deletion dapp/src/utils/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,24 @@ export const formatTokenAmount = (
amount: number | string | bigint,
decimals = 18,
desiredDecimals = 2,
ceilPrecision = desiredDecimals,
) => {
const fixedPoint = BigInt(amount)

if (fixedPoint === 0n) {
return `0.${"0".repeat(desiredDecimals)}`
}

const formattedAmount = bigIntToUserAmount(
let formattedAmount = bigIntToUserAmount(
fixedPoint,
decimals,
desiredDecimals,
)
if (ceilPrecision !== desiredDecimals) {
formattedAmount =
Math.ceil(formattedAmount * 10 ** ceilPrecision) / 10 ** ceilPrecision
}

const minAmountToDisplay = 1 / 10 ** Math.min(desiredDecimals, decimals)

if (minAmountToDisplay > formattedAmount) {
Expand Down

0 comments on commit 8054c94

Please sign in to comment.