UNPKG

@frak-labs/components

Version:

Frak Wallet components, helping any person to interact with the Frak wallet.

34 lines (33 loc) 1.46 kB
import { formatAmount, getCurrencyAmountKey, getSupportedCurrency } from "@frak-labs/core-sdk"; //#region src/utils/format/formatReward.ts /** * Format an {@link EstimatedReward} into a human-readable string. * * - `fixed` → e.g. `"5 €"` * - `percentage` → if `basketAmount` is provided, computes the actual value * (e.g. `"10 €"`), otherwise returns `"10 %"` * - `tiered` → max tier value, e.g. `"50 €"` */ function formatEstimatedReward(reward, currency, basketAmount) { const supportedCurrency = getSupportedCurrency(currency); const key = getCurrencyAmountKey(supportedCurrency); switch (reward.payoutType) { case "fixed": return formatAmount(Math.round(reward.amount[key]), supportedCurrency); case "percentage": if (basketAmount !== void 0) return formatAmount(Math.round(reward.percent * basketAmount / 100), supportedCurrency); return `${reward.percent} %`; case "tiered": { const max = reward.tiers.reduce((acc, tier) => Math.max(acc, tier.amount[key]), 0); return formatAmount(Math.round(max), supportedCurrency); } } } /** * Replace the `{REWARD}` placeholder in a text string with a reward value. * If no reward is provided, returns the text with `{REWARD}` stripped. */ function applyRewardPlaceholder(text, reward) { return reward ? text.replace("{REWARD}", reward) : text.replace("{REWARD}", ""); } //#endregion export { formatEstimatedReward as n, applyRewardPlaceholder as t };