Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/services/ibex/webhook-server/routes/on-receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { ibexWebhookPaths } from "@services/ibex/webhook-config"

import { authenticate, logRequest, validateIbexIp } from "../middleware"

const USDT_MICROS_PER_MAJOR_UNIT = 1_000_000

interface PaymentContext {
receiverWallet: Wallet
recipientAccount: Account
Expand Down Expand Up @@ -255,6 +257,8 @@ const parsePaymentHash = (hash: unknown) =>
const toPaymentAmount = (currency: WalletCurrency) => (dollarAmount: number) => {
let amount: PaymentAmount<WalletCurrency>["amount"] | undefined
if (currency === WalletCurrency.Usd) amount = BigInt(Math.round(dollarAmount * 100))
if (currency === WalletCurrency.Usdt)
amount = BigInt(Math.round(dollarAmount * USDT_MICROS_PER_MAJOR_UNIT))
return { amount: amount as PaymentAmount<WalletCurrency>["amount"], currency }
}

Expand Down
33 changes: 26 additions & 7 deletions src/services/notifications/create-push-notification-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { WalletCurrency } from "@domain/shared"
import { getLanguageOrDefault } from "@domain/locale"

const i18n = getI18nInstance()
const USDT_MICROS_PER_MAJOR_UNIT = 1_000_000

const customToLocaleString = (
number: number,
Expand Down Expand Up @@ -36,20 +37,38 @@ export const createPushNotificationContent = <T extends DisplayCurrency>({
} => {
const locale = getLanguageOrDefault(userLanguage)
const baseCurrency = amount.currency
const walletCurrencyLabel = baseCurrency === WalletCurrency.Usdt ? "USD" : baseCurrency
const notificationType = type === "balance" ? type : `transaction.${type}`
const title = i18n.__(
{ phrase: `notification.${notificationType}.title`, locale },
{ walletCurrency: baseCurrency },
{ walletCurrency: walletCurrencyLabel },
)
const baseCurrencyName = baseCurrency === WalletCurrency.Btc ? "sats" : ""
const displayedBaseAmount =
baseCurrency === WalletCurrency.Usd ? Number(amount.amount) / 100 : amount.amount
let baseCurrencyName = ""
let displayedBaseAmount: number | bigint = amount.amount
let numberFormatOptions: Intl.NumberFormatOptions = { style: "decimal" }

if (baseCurrency === WalletCurrency.Btc) {
baseCurrencyName = "sats"
}

if (baseCurrency === WalletCurrency.Usd) {
displayedBaseAmount = Number(amount.amount) / 100
numberFormatOptions = {
currency: baseCurrency,
style: "currency",
currencyDisplay: "narrowSymbol",
}
}

if (baseCurrency === WalletCurrency.Usdt) {
baseCurrencyName = "USD"
displayedBaseAmount = Number(amount.amount) / USDT_MICROS_PER_MAJOR_UNIT
}

const baseCurrencyAmount = customToLocaleString(Number(displayedBaseAmount), locale, {
minimumFractionDigits: 0,
maximumFractionDigits: MajorExponent.STANDARD,
currency: baseCurrency,
style: baseCurrency === WalletCurrency.Btc ? "decimal" : "currency",
currencyDisplay: "narrowSymbol",
...numberFormatOptions,
})

let body = i18n.__(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NotificationType } from "@domain/notifications"
import { WalletCurrency } from "@domain/shared"
import { createPushNotificationContent } from "@services/notifications/create-push-notification-content"

describe("createPushNotificationContent", () => {
it("formats USDT invoice-paid notifications with USD-facing copy", () => {
const content = createPushNotificationContent({
type: NotificationType.LnInvoicePaid,
userLanguage: "en" as UserLanguageOrEmpty,
amount: {
amount: 20_000n,
currency: WalletCurrency.Usdt,
} as PaymentAmount<typeof WalletCurrency.Usdt>,
})

expect(content).toEqual({
title: "USD Transaction",
body: "+0.02 USD",
})
})
})
Loading