Skip to content

Commit

Permalink
fix: convert onchain receive notification display amount to major unit (
Browse files Browse the repository at this point in the history
#2318)

* test: add check from onchain confirmed receive notification

* fix: convert usd to major unit for onchain receive notification
  • Loading branch information
vindard authored Feb 12, 2023
1 parent c8f8f09 commit 931552b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/app/wallets/update-on-chain-receipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,16 @@ const processTxForWallet = async (
)
if (recipientUser instanceof Error) return recipientUser

const displayPaymentAmount = {
amount: Number((amountDisplayCurrency / 100).toFixed(2)),
currency: displayCurrency,
}

await notifications.onChainTxReceived({
recipientAccountId: wallet.accountId,
recipientWalletId: wallet.id,
paymentAmount: { amount: BigInt(sats), currency: wallet.currency },
displayPaymentAmount: {
amount: amountDisplayCurrency,
currency: displayCurrency,
},
displayPaymentAmount,
txHash: tx.rawTx.txHash,
recipientDeviceTokens: recipientUser.deviceTokens,
recipientLanguage: recipientUser.language,
Expand Down
64 changes: 62 additions & 2 deletions test/integration/02-user-wallet/02-receive-onchain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { LedgerTransactionType } from "@domain/ledger"
import { NotificationType } from "@domain/notifications"
import { PriceRatio } from "@domain/payments"
import { OnChainAddressCreateRateLimiterExceededError } from "@domain/rate-limit/errors"
import { WalletCurrency } from "@domain/shared"
import { AmountCalculator, paymentAmountFromNumber, WalletCurrency } from "@domain/shared"
import { DepositFeeCalculator, TxStatus } from "@domain/wallets"

import { onchainTransactionEventHandler } from "@servers/trigger"
Expand Down Expand Up @@ -59,6 +59,8 @@ let walletIdA: WalletId
let walletIdB: WalletId
let accountIdA: AccountId

const calc = AmountCalculator()

const accountLimits = getAccountLimits({ level: 1 })

const locale = getLocale()
Expand Down Expand Up @@ -139,10 +141,68 @@ describe("UserWallet - On chain", () => {
})

it("receives on-chain transaction", async () => {
const sendNotification = jest.fn()
jest
.spyOn(PushNotificationsServiceImpl, "PushNotificationsService")
.mockImplementationOnce(() => ({
sendNotification,
}))

const amountSats = getRandomAmountOfSats()
const receivedBtc = { amount: BigInt(amountSats), currency: WalletCurrency.Btc }

// Execute receive
await sendToWalletTestWrapper({
walletId: walletIdA,
amountSats: getRandomAmountOfSats(),
amountSats,
})

// Calculate receive display amount
const account = await AccountsRepository().findById(accountIdA)
if (account instanceof Error) throw account

const receivedUsd = await usdFromBtcMidPriceFn({
amount: BigInt(amountSats),
currency: WalletCurrency.Btc,
})
if (receivedUsd instanceof Error) return receivedUsd

const fee = DepositFeeCalculator().onChainDepositFee({
amount: amountSats,
ratio: account.depositFeeRatio,
})
const satsFee = paymentAmountFromNumber({
amount: fee,
currency: WalletCurrency.Btc,
})
if (satsFee instanceof Error) throw satsFee

const priceRatio = PriceRatio({ usd: receivedUsd, btc: receivedBtc })
if (priceRatio instanceof Error) return priceRatio

const bankFee = {
usdBankFee: priceRatio.convertFromBtcToCeil(satsFee),
btcBankFee: satsFee,
}

const usdToCreditReceiver = calc.sub(receivedUsd, bankFee.usdBankFee)

const displayAmount = {
amount: Number((Number(usdToCreditReceiver.amount) / 100).toFixed(2)),
currency: DisplayCurrency.Usd,
}

// Check received notification
const receivedNotification = createPushNotificationContent({
type: NotificationType.OnchainReceipt,
userLanguage: locale,
amount: receivedBtc,
displayAmount,
})

expect(sendNotification.mock.calls.length).toBe(1)
expect(sendNotification.mock.calls[0][0].title).toBe(receivedNotification.title)
expect(sendNotification.mock.calls[0][0].body).toBe(receivedNotification.body)
})

it("retrieves on-chain transactions by address", async () => {
Expand Down

0 comments on commit 931552b

Please sign in to comment.