Skip to content

Commit 30b4d1f

Browse files
authored
refactor: swap out NewDisplayCurrencyConverter (#2383)
* refactor: in send-lightning swap NewDisplayCurrencyConverter * refactor: in send-on-chain swap NewDisplayCurrencyConverter * refactor: in reimburseFee swap NewDisplayCurrencyConverter * refactor: in send-intraledger swap NewDisplayCurrencyConverter * chore: remove unused 'NewDisplayCurrencyConverter'
1 parent 34e2e3e commit 30b4d1f

File tree

10 files changed

+253
-111
lines changed

10 files changed

+253
-111
lines changed

src/app/payments/send-intraledger.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@ import { getPubkeysToSkipProbe } from "@config"
22

33
import { AccountValidator } from "@domain/accounts"
44
import { PaymentSendStatus } from "@domain/bitcoin/lightning"
5-
import {
6-
DisplayCurrency,
7-
NewDisplayCurrencyConverter,
8-
usdMinorToMajorUnit,
9-
} from "@domain/fiat"
5+
import { DisplayCurrency, usdMinorToMajorUnit } from "@domain/fiat"
106
import {
117
InvalidLightningPaymentFlowBuilderStateError,
128
InvalidZeroAmountPriceRatioInputError,
139
LightningPaymentFlowBuilder,
14-
WalletPriceRatio,
1510
ZeroAmountForUsdRecipientError,
1611
} from "@domain/payments"
1712
import { ErrorLevel, WalletCurrency } from "@domain/shared"
@@ -35,7 +30,11 @@ import {
3530
import { ResourceExpiredLockServiceError } from "@domain/lock"
3631

3732
import { Accounts } from "@app"
38-
import { btcFromUsdMidPriceFn, usdFromBtcMidPriceFn } from "@app/prices"
33+
import {
34+
btcFromUsdMidPriceFn,
35+
getCurrentPriceAsDisplayPriceRatio,
36+
usdFromBtcMidPriceFn,
37+
} from "@app/prices"
3938
import { validateIsBtcWallet, validateIsUsdWallet } from "@app/wallets"
4039

4140
import {
@@ -255,13 +254,14 @@ const executePaymentViaIntraledger = async <
255254
const balanceCheck = paymentFlow.checkBalanceForSend(balance)
256255
if (balanceCheck instanceof Error) return balanceCheck
257256

258-
const priceRatio = WalletPriceRatio({
259-
usd: paymentFlow.usdPaymentAmount,
260-
btc: paymentFlow.btcPaymentAmount,
257+
const { displayCurrency } = senderAccount
258+
const displayPriceRatio = await getCurrentPriceAsDisplayPriceRatio({
259+
currency: displayCurrency,
261260
})
262-
if (priceRatio instanceof Error) return priceRatio
263-
const displayCentsPerSat = priceRatio.usdPerSat()
264-
const converter = NewDisplayCurrencyConverter(displayCentsPerSat)
261+
if (displayPriceRatio instanceof Error) return displayPriceRatio
262+
const amountDisplayCurrencyAsNumber = Number(
263+
displayPriceRatio.convertFromWallet(paymentFlow.btcPaymentAmount).amountInMinor,
264+
) as DisplayCurrencyBaseAmount
265265

266266
if (signal.aborted) {
267267
return new ResourceExpiredLockServiceError(signal.error?.message)
@@ -275,9 +275,9 @@ const executePaymentViaIntraledger = async <
275275
metadata = LedgerFacade.WalletIdTradeIntraAccountLedgerMetadata({
276276
paymentAmounts: paymentFlow,
277277

278-
amountDisplayCurrency: converter.fromUsdAmount(paymentFlow.usdPaymentAmount),
278+
amountDisplayCurrency: amountDisplayCurrencyAsNumber,
279279
feeDisplayCurrency: 0 as DisplayCurrencyBaseAmount,
280-
displayCurrency: DisplayCurrency.Usd,
280+
displayCurrency,
281281

282282
memoOfPayer: memo || undefined,
283283
})
@@ -286,9 +286,9 @@ const executePaymentViaIntraledger = async <
286286
LedgerFacade.WalletIdIntraledgerLedgerMetadata({
287287
paymentAmounts: paymentFlow,
288288

289-
amountDisplayCurrency: converter.fromUsdAmount(paymentFlow.usdPaymentAmount),
289+
amountDisplayCurrency: amountDisplayCurrencyAsNumber,
290290
feeDisplayCurrency: 0 as DisplayCurrencyBaseAmount,
291-
displayCurrency: DisplayCurrency.Usd,
291+
displayCurrency,
292292

293293
memoOfPayer: memo || undefined,
294294
senderUsername: senderAccount.username,

src/app/payments/send-lightning.ts

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import {
77
PaymentSendStatus,
88
} from "@domain/bitcoin/lightning"
99
import { AlreadyPaidError, CouldNotFindLightningPaymentFlowError } from "@domain/errors"
10-
import {
11-
DisplayCurrency,
12-
NewDisplayCurrencyConverter,
13-
usdMinorToMajorUnit,
14-
} from "@domain/fiat"
10+
import { DisplayCurrency, usdMinorToMajorUnit } from "@domain/fiat"
1511
import {
1612
checkedToBtcPaymentAmount,
1713
checkedToUsdPaymentAmount,
@@ -48,6 +44,7 @@ import { addAttributesToCurrentSpan } from "@services/tracing"
4844

4945
import { Wallets } from "@app"
5046
import { validateIsBtcWallet, validateIsUsdWallet } from "@app/wallets"
47+
import { getCurrentPriceAsDisplayPriceRatio } from "@app/prices"
5148

5249
import { ResourceExpiredLockServiceError } from "@domain/lock"
5350

@@ -94,9 +91,16 @@ export const payInvoiceByWalletId = async ({
9491
paymentFlow,
9592
senderWallet,
9693
senderUsername: senderAccount.username,
94+
senderDisplayCurrency: senderAccount.displayCurrency,
95+
memo,
96+
})
97+
: executePaymentViaLn({
98+
decodedInvoice,
99+
paymentFlow,
100+
senderWallet,
101+
senderDisplayCurrency: senderAccount.displayCurrency,
97102
memo,
98103
})
99-
: executePaymentViaLn({ decodedInvoice, paymentFlow, senderWallet, memo })
100104
}
101105

102106
const payNoAmountInvoiceByWalletId = async ({
@@ -133,9 +137,16 @@ const payNoAmountInvoiceByWalletId = async ({
133137
paymentFlow,
134138
senderWallet,
135139
senderUsername: senderAccount.username,
140+
senderDisplayCurrency: senderAccount.displayCurrency,
141+
memo,
142+
})
143+
: executePaymentViaLn({
144+
decodedInvoice,
145+
paymentFlow,
146+
senderWallet,
147+
senderDisplayCurrency: senderAccount.displayCurrency,
136148
memo,
137149
})
138-
: executePaymentViaLn({ decodedInvoice, paymentFlow, senderWallet, memo })
139150
}
140151

141152
export const payNoAmountInvoiceByWalletIdForBtcWallet = async (
@@ -322,11 +333,13 @@ const executePaymentViaIntraledger = async <
322333
paymentFlow,
323334
senderWallet,
324335
senderUsername,
336+
senderDisplayCurrency,
325337
memo,
326338
}: {
327339
paymentFlow: PaymentFlow<S, R>
328340
senderWallet: WalletDescriptor<S>
329341
senderUsername: Username | undefined
342+
senderDisplayCurrency: DisplayCurrency
330343
memo: string | null
331344
}): Promise<PaymentSendStatus | ApplicationError> => {
332345
addAttributesToCurrentSpan({
@@ -380,14 +393,13 @@ const executePaymentViaIntraledger = async <
380393
const balanceCheck = paymentFlow.checkBalanceForSend(balance)
381394
if (balanceCheck instanceof Error) return balanceCheck
382395

383-
const priceRatio = WalletPriceRatio({
384-
usd: paymentFlow.usdPaymentAmount,
385-
btc: paymentFlow.btcPaymentAmount,
396+
const displayPriceRatio = await getCurrentPriceAsDisplayPriceRatio({
397+
currency: senderDisplayCurrency,
386398
})
387-
if (priceRatio instanceof Error) return priceRatio
388-
const displayCentsPerSat = priceRatio.usdPerSat()
389-
390-
const converter = NewDisplayCurrencyConverter(displayCentsPerSat)
399+
if (displayPriceRatio instanceof Error) return displayPriceRatio
400+
const amountDisplayCurrencyAsNumber = Number(
401+
displayPriceRatio.convertFromWallet(paymentFlow.btcPaymentAmount).amountInMinor,
402+
) as DisplayCurrencyBaseAmount
391403

392404
if (signal.aborted) {
393405
return new ResourceExpiredLockServiceError(signal.error?.message)
@@ -404,9 +416,9 @@ const executePaymentViaIntraledger = async <
404416
pubkey: recipientPubkey,
405417
paymentAmounts: paymentFlow,
406418

407-
amountDisplayCurrency: converter.fromUsdAmount(paymentFlow.usdPaymentAmount),
419+
amountDisplayCurrency: amountDisplayCurrencyAsNumber,
408420
feeDisplayCurrency: 0 as DisplayCurrencyBaseAmount,
409-
displayCurrency: DisplayCurrency.Usd,
421+
displayCurrency: senderDisplayCurrency,
410422

411423
memoOfPayer: memo || undefined,
412424
}))
@@ -417,9 +429,9 @@ const executePaymentViaIntraledger = async <
417429
pubkey: recipientPubkey,
418430
paymentAmounts: paymentFlow,
419431

420-
amountDisplayCurrency: converter.fromUsdAmount(paymentFlow.usdPaymentAmount),
432+
amountDisplayCurrency: amountDisplayCurrencyAsNumber,
421433
feeDisplayCurrency: 0 as DisplayCurrencyBaseAmount,
422-
displayCurrency: DisplayCurrency.Usd,
434+
displayCurrency: senderDisplayCurrency,
423435

424436
memoOfPayer: memo || undefined,
425437
senderUsername,
@@ -483,11 +495,13 @@ const executePaymentViaLn = async ({
483495
decodedInvoice,
484496
paymentFlow,
485497
senderWallet,
498+
senderDisplayCurrency,
486499
memo,
487500
}: {
488501
decodedInvoice: LnInvoice
489502
paymentFlow: PaymentFlow<WalletCurrency, WalletCurrency>
490503
senderWallet: Wallet
504+
senderDisplayCurrency: DisplayCurrency
491505
memo: string | null
492506
}): Promise<PaymentSendStatus | ApplicationError> => {
493507
addAttributesToCurrentSpan({
@@ -525,22 +539,25 @@ const executePaymentViaLn = async ({
525539
const lndService = LndService()
526540
if (lndService instanceof Error) return lndService
527541

528-
const priceRatio = WalletPriceRatio({
529-
usd: paymentFlow.usdPaymentAmount,
530-
btc: paymentFlow.btcPaymentAmount,
542+
const displayPriceRatio = await getCurrentPriceAsDisplayPriceRatio({
543+
currency: senderDisplayCurrency,
531544
})
532-
if (priceRatio instanceof Error) return priceRatio
533-
const displayCentsPerSat = priceRatio.usdPerSat()
534-
535-
const converter = NewDisplayCurrencyConverter(displayCentsPerSat)
545+
if (displayPriceRatio instanceof Error) return displayPriceRatio
546+
const amountDisplayCurrencyAsNumber = Number(
547+
displayPriceRatio.convertFromWallet(paymentFlow.btcPaymentAmount).amountInMinor,
548+
) as DisplayCurrencyBaseAmount
549+
const feeDisplayCurrencyAsNumber = Number(
550+
displayPriceRatio.convertFromWallet(paymentFlow.btcProtocolAndBankFee)
551+
.amountInMinor,
552+
) as DisplayCurrencyBaseAmount
536553

537554
if (signal.aborted) {
538555
return new ResourceExpiredLockServiceError(signal.error?.message)
539556
}
540557

541558
const metadata = LedgerFacade.LnSendLedgerMetadata({
542-
amountDisplayCurrency: converter.fromUsdAmount(paymentFlow.usdPaymentAmount),
543-
feeDisplayCurrency: converter.fromUsdAmount(paymentFlow.usdProtocolAndBankFee),
559+
amountDisplayCurrency: amountDisplayCurrencyAsNumber,
560+
feeDisplayCurrency: feeDisplayCurrencyAsNumber,
544561
displayCurrency: DisplayCurrency.Usd,
545562

546563
paymentAmounts: paymentFlow,
@@ -573,6 +590,12 @@ const executePaymentViaLn = async ({
573590
if (journal instanceof Error) return journal
574591
const { journalId } = journal
575592

593+
const walletPriceRatio = WalletPriceRatio({
594+
usd: paymentFlow.usdPaymentAmount,
595+
btc: paymentFlow.btcPaymentAmount,
596+
})
597+
if (walletPriceRatio instanceof Error) return walletPriceRatio
598+
576599
let payResult: PayInvoiceResult | LightningServiceError
577600
if (rawRoute) {
578601
payResult = await lndService.payInvoiceViaRoutes({
@@ -584,7 +607,7 @@ const executePaymentViaLn = async ({
584607
const maxFeeCheckArgs = {
585608
maxFeeAmount: paymentFlow.btcProtocolAndBankFee,
586609
btcPaymentAmount: paymentFlow.btcPaymentAmount,
587-
priceRatio,
610+
priceRatio: walletPriceRatio,
588611
senderWalletCurrency: paymentFlow.senderWalletDescriptor().currency,
589612
}
590613
const maxFeeCheck = LnFees().verifyMaxFee(maxFeeCheckArgs)
@@ -638,6 +661,8 @@ const executePaymentViaLn = async ({
638661
if (!rawRoute) {
639662
const reimbursed = await Wallets.reimburseFee({
640663
paymentFlow,
664+
senderDisplayAmount: amountDisplayCurrencyAsNumber,
665+
senderDisplayCurrency,
641666
journalId,
642667
actualFee: payResult.roundedUpFee,
643668
revealedPreImage: payResult.revealedPreImage,

src/app/payments/update-pending-payments.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,19 @@ const updatePendingPayment = wrapAsyncToRunInSpan({
230230
})
231231
if (pendingPayment.feeKnownInAdvance) return true
232232

233-
const { displayAmount, displayFee } = pendingPayment
234-
if (displayAmount === undefined || displayFee === undefined) {
233+
const { displayAmount, displayFee, displayCurrency } = pendingPayment
234+
if (
235+
displayAmount === undefined ||
236+
displayFee === undefined ||
237+
displayCurrency === undefined
238+
) {
235239
return new UnknownLedgerError("missing display-related values in transaction")
236240
}
237241

238242
return Wallets.reimburseFee({
239243
paymentFlow,
244+
senderDisplayAmount: displayAmount,
245+
senderDisplayCurrency: displayCurrency,
240246
journalId: pendingPayment.journalId,
241247
actualFee: roundedUpFee,
242248
revealedPreImage,

src/app/wallets/reimburse-fee.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
import { toSats } from "@domain/bitcoin"
2-
import { DisplayCurrency, NewDisplayCurrencyConverter, toCents } from "@domain/fiat"
2+
import { MajorExponent, toCents } from "@domain/fiat"
33
import { LedgerTransactionType } from "@domain/ledger"
44
import { FeeReimbursement } from "@domain/ledger/fee-reimbursement"
5-
import { WalletPriceRatio } from "@domain/payments"
6-
import { paymentAmountFromNumber, WalletCurrency } from "@domain/shared"
5+
import { DisplayPriceRatio, WalletPriceRatio } from "@domain/payments"
6+
import {
7+
displayAmountFromNumber,
8+
paymentAmountFromNumber,
9+
WalletCurrency,
10+
} from "@domain/shared"
711

812
import * as LedgerFacade from "@services/ledger/facade"
913
import { baseLogger } from "@services/logger"
1014

11-
export const reimburseFee = async <S extends WalletCurrency, R extends WalletCurrency>({
15+
export const reimburseFee = async <
16+
S extends WalletCurrency,
17+
R extends WalletCurrency,
18+
T extends DisplayCurrency,
19+
>({
1220
paymentFlow,
21+
senderDisplayAmount,
22+
senderDisplayCurrency,
1323
journalId,
1424
actualFee,
1525
revealedPreImage,
1626
}: {
1727
paymentFlow: PaymentFlow<S, R>
28+
senderDisplayAmount: DisplayCurrencyBaseAmount
29+
senderDisplayCurrency: T
1830
journalId: LedgerJournalId
1931
actualFee: Satoshis
2032
revealedPreImage?: RevealedPreImage
@@ -51,9 +63,24 @@ export const reimburseFee = async <S extends WalletCurrency, R extends WalletCur
5163
return true
5264
}
5365

54-
const displayCentsPerSat = priceRatio.usdPerSat()
55-
const converter = NewDisplayCurrencyConverter(displayCentsPerSat)
56-
const reimburseAmountDisplayCurrency = converter.fromUsdAmount(feeDifference.usd)
66+
const displayAmount = displayAmountFromNumber({
67+
amount: senderDisplayAmount,
68+
currency: senderDisplayCurrency,
69+
})
70+
if (displayAmount instanceof Error) return displayAmount
71+
72+
const displayPriceRatio = DisplayPriceRatio({
73+
displayAmountInMinorUnit: displayAmount,
74+
walletAmount: paymentFlow.btcPaymentAmount,
75+
displayMajorExponent: MajorExponent.STANDARD,
76+
})
77+
if (displayPriceRatio instanceof Error) return displayPriceRatio
78+
const reimburseAmountDisplayCurrency = displayPriceRatio.convertFromWallet(
79+
feeDifference.btc,
80+
)
81+
const reimburseAmountAsNumber = Number(
82+
reimburseAmountDisplayCurrency.amountInMinor,
83+
) as DisplayCurrencyBaseAmount
5784

5885
const paymentHash = paymentFlow.paymentHashForFlow()
5986
if (paymentHash instanceof Error) return paymentHash
@@ -69,9 +96,9 @@ export const reimburseFee = async <S extends WalletCurrency, R extends WalletCur
6996
satsFee: toSats(0),
7097
centsFee: toCents(0),
7198

72-
displayAmount: reimburseAmountDisplayCurrency,
99+
displayAmount: reimburseAmountAsNumber,
73100
displayFee: 0 as DisplayCurrencyBaseAmount,
74-
displayCurrency: DisplayCurrency.Usd,
101+
displayCurrency: senderDisplayCurrency,
75102
}
76103

77104
const txMetadata: LnLedgerTransactionMetadataUpdate = {

0 commit comments

Comments
 (0)