Skip to content

Commit

Permalink
resolves incorrect newm purchase fee (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
scandycuz authored Oct 22, 2024
1 parent 537da06 commit 34209be
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
formatNewmAmount,
formatUsdAmount,
} from "@newm-web/utils";
import { useGetAdaUsdConversionRateQuery } from "../../modules/wallet/api";
import {
useGetAdaUsdConversionRateQuery,
useGetNewmUsdConversionRateQuery,
} from "../../modules/wallet/api";

interface PurchaseStreamTokensModalProps {
readonly isLoading: boolean;
Expand Down Expand Up @@ -41,13 +44,13 @@ const PurchaseStreamTokensModal: FunctionComponent<
const { data: { usdPrice: adaUsdConversionRate = 0 } = {} } =
useGetAdaUsdConversionRateQuery();
const { data: { usdPrice: newmUsdConversionRate = 0 } = {} } =
useGetAdaUsdConversionRateQuery();
useGetNewmUsdConversionRateQuery();

const newmTransactionFeeUsd = 0.5;
const newmTransactionFeeNewm =
(newmTransactionFeeUsd * newmUsdConversionRate) / LOVELACE_CONVERSION;
newmTransactionFeeUsd / (newmUsdConversionRate / LOVELACE_CONVERSION);
const adaTransactionFeeUsd =
(TRANSACTION_FEE_IN_ADA * adaUsdConversionRate) / LOVELACE_CONVERSION;
TRANSACTION_FEE_IN_ADA * (adaUsdConversionRate / LOVELACE_CONVERSION);

return (
<Modal
Expand Down Expand Up @@ -125,7 +128,8 @@ const PurchaseStreamTokensModal: FunctionComponent<
</Typography>
<Typography variant="h4">
<Typography component="span" mr={ 0.5 } variant="subtitle2">
(≈ { formatUsdAmount(totalPurchaseValueUsd, 2) }){ " " }
(≈{ " " }
{ formatUsdAmount(totalPurchaseValueUsd, { precision: 2 }) }){ " " }
</Typography>
{ formatNewmAmount(totalPurchaseValueNewm) }
</Typography>
Expand Down Expand Up @@ -156,7 +160,7 @@ const PurchaseStreamTokensModal: FunctionComponent<
<Typography variant="subtitle1">NEWM fee</Typography>
<Typography variant="h4">
<Typography component="span" mr={ 0.5 } variant="subtitle2">
{ formatUsdAmount(newmTransactionFeeUsd, 2) }
{ formatUsdAmount(newmTransactionFeeUsd, { precision: 2 }) }
</Typography>
{ formatNewmAmount(newmTransactionFeeNewm) }
</Typography>
Expand All @@ -169,7 +173,7 @@ const PurchaseStreamTokensModal: FunctionComponent<
<Typography variant="subtitle1">Transaction fee</Typography>
<Typography variant="h4">
<Typography component="span" mr={ 0.5 } variant="subtitle2">
{ formatUsdAmount(adaTransactionFeeUsd, 2) }{ " " }
{ formatUsdAmount(adaTransactionFeeUsd, { precision: 2 }) }{ " " }
</Typography>
{ TRANSACTION_FEE_IN_ADA }
</Typography>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const CreateSale = () => {
const formattedPerTokenPrice =
saleCurrency === Currency.USD.name
? formatUsdAmount(perTokenPrice)
: formatNewmAmount(perTokenPrice, true);
: formatNewmAmount(perTokenPrice);

return (
<Form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
LOVELACE_CONVERSION,
formatNewmAmount,
formatPercentageAdaptive,
formatUsdAmount,
} from "@newm-web/utils";
import { Currency } from "@newm-web/types";
import { useGetNewmUsdConversionRateQuery } from "../../../../modules/crypto";
Expand Down Expand Up @@ -139,12 +140,9 @@ const StartSaleModal: FunctionComponent<StartSaleModalProps> = ({
<Typography component="span" mr={ 0.5 } variant="subtitle2">
{ isNEWMPriceUnavailable
? "(≈ $ N/A)"
: `(≈ ${currency(totalSaleValueInUSD, {
precision: 3,
symbol: "$",
}).format()})` }
: `(≈ ${formatUsdAmount(totalSaleValueInUSD)})` }
</Typography>
{ formatNewmAmount(totalSaleValueInNEWM, true) }
{ formatNewmAmount(totalSaleValueInNEWM) }
</Typography>
</Stack>
<Stack
Expand All @@ -159,12 +157,9 @@ const StartSaleModal: FunctionComponent<StartSaleModalProps> = ({
<Typography component="span" mr={ 0.5 } variant="subtitle2">
{ isNEWMPriceUnavailable
? "(≈ $ N/A)"
: `(≈ ${currency(pricePerStreamTokenInUSD, {
precision: 3,
symbol: "$",
}).format()})` }
: `(≈ ${formatUsdAmount(pricePerStreamTokenInUSD)})` }
</Typography>
{ formatNewmAmount(totalSaleValueInNEWM / tokensToSell, true) }
{ formatNewmAmount(totalSaleValueInNEWM / tokensToSell) }
</Typography>
</Stack>
</Stack>
Expand Down
8 changes: 6 additions & 2 deletions apps/studio/src/pages/home/wallet/EarningsSummaryModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ const EarningsSummaryModal: FunctionComponent<
<Typography variant="subtitle1">Earnings</Typography>
<Typography variant="h4">
<Typography component="span" mr={ 0.5 } variant="subtitle2">
(≈ { formatUsdAmount(unclaimedEarningsInUSD, 2) }){ " " }
(≈{ " " }
{ formatUsdAmount(unclaimedEarningsInUSD, { precision: 2 }) }){ " " }
</Typography>
{ formatNewmAmount(unclaimedEarningsInNEWM) }
</Typography>
Expand All @@ -93,7 +94,10 @@ const EarningsSummaryModal: FunctionComponent<
<Typography component="span" mr={ 0.5 } variant="subtitle2">
(≈ { formatUsdAmount(transactionFeeInUSD) }){ " " }
</Typography>
{ formatAdaAmount(transactionFeeInADA, false) }
{ formatAdaAmount(transactionFeeInADA, {
includeSymbol: false,
}) }{ " " }
</Typography>
</Stack>
</Stack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ const DisconnectWalletButton: FunctionComponent<
fontWeight={ 400 }
variant="h5"
>
(≈{ formatUsdAmount(newmUsdBalance, 2) })
(≈{ formatUsdAmount(newmUsdBalance, { precision: 2 }) })
</Typography>
</Stack>

Expand All @@ -263,7 +263,7 @@ const DisconnectWalletButton: FunctionComponent<
fontWeight={ 400 }
variant="h5"
>
(≈{ formatUsdAmount(adaUsdBalance, 2) })
(≈{ formatUsdAmount(adaUsdBalance, { precision: 2 }) })
</Typography>
</Stack>
</Stack>
Expand Down
35 changes: 29 additions & 6 deletions packages/utils/src/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,26 @@ export const NEWM_POLICY_ID = isProd
: "769c4c6e9bc3ba5406b9b89fb7beb6819e638ff2e2de63f008d5bcff";
export const NEWM_ASSET_NAME = isProd ? "4e45574d" : "744e45574d";

interface FormatCurrencyOptions {
readonly includeSymbol?: boolean;
readonly precision?: number;
}

/**
* Formats a numerical NEWM amount with the correct decimal
* places and symbol.
*/
export const formatNewmAmount = (amount?: number, includeSymbol = true) => {
export const formatNewmAmount = (
amount?: number,
options: FormatCurrencyOptions = { includeSymbol: true, precision: 2 }
) => {
if (!amount) return "0 Ɲ";

const { includeSymbol = true, precision = 2 } = options;

return currency(amount, {
pattern: "# !",
precision: 2,
precision,
symbol: includeSymbol ? "Ɲ" : "",
}).format();
};
Expand All @@ -26,12 +36,17 @@ export const formatNewmAmount = (amount?: number, includeSymbol = true) => {
* Formats a numerical ADA amount with the correct decimal
* places and symbol.
*/
export const formatAdaAmount = (amount?: number, includeSymbol = true) => {
export const formatAdaAmount = (
amount?: number,
options: FormatCurrencyOptions = { includeSymbol: true, precision: 2 }
) => {
if (!amount) return "₳ 0";

const { includeSymbol = true, precision = 2 } = options;

return currency(amount, {
pattern: "! #",
precision: 2,
precision,
symbol: includeSymbol ? "₳" : "",
}).format();
};
Expand All @@ -41,10 +56,18 @@ export const formatAdaAmount = (amount?: number, includeSymbol = true) => {
* rather than the standard two, based on the exchange rate
* for NEWM to USD.
*/
export const formatUsdAmount = (amount?: number, precision = 3) => {
export const formatUsdAmount = (
amount?: number,
options: FormatCurrencyOptions = { includeSymbol: true, precision: 4 }
) => {
if (!amount) return "$0";

return currency(amount, { precision }).format();
const { includeSymbol = true, precision = 4 } = options;

return currency(amount, {
precision,
symbol: includeSymbol ? "$" : "",
}).format();
};

export const Currency = {
Expand Down

0 comments on commit 34209be

Please sign in to comment.