Skip to content

Commit d282bbe

Browse files
✨ (ledger-button): Use Fiat detailed endpoint (#440)
2 parents 341834a + 7cc3692 commit d282bbe

29 files changed

Lines changed: 448 additions & 103 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@ledgerhq/ledger-wallet-provider-core": minor
3+
"@ledgerhq/ledger-wallet-provider": minor
4+
---
5+
6+
Use Fiat detailed endpoint

packages/ledger-button-core/src/api/LedgerButtonCore.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ import { SignTransactionParams } from "./model/signing/SignTransactionParams.js"
1515
import { SignTypedMessageParams } from "./model/signing/SignTypedMessageParams.js";
1616
import { getChainIdFromCurrencyId } from "./utils/index.js";
1717
import { accountModuleTypes } from "../internal/account/accountModuleTypes.js";
18-
import {
19-
DEFAULT_FIAT_CURRENCY,
20-
SUPPORTED_FIAT_CURRENCIES,
21-
} from "../internal/account/model/constant.js";
2218
import {
2319
Account,
2420
type AccountService,
@@ -40,6 +36,10 @@ import { consentModuleTypes } from "../internal/consent/consentModuleTypes.js";
4036
import { type ConsentService } from "../internal/consent/ConsentService.js";
4137
import { contextModuleTypes } from "../internal/context/contextModuleTypes.js";
4238
import { ContextService } from "../internal/context/ContextService.js";
39+
import { DEFAULT_FIAT_CURRENCY } from "../internal/currency/constant.js";
40+
import { currencyModuleTypes } from "../internal/currency/currencyModuleTypes.js";
41+
import type { FiatCurrency } from "../internal/currency/datasource/fiatCurrencyTypes.js";
42+
import type { CurrencyService } from "../internal/currency/service/CurrencyService.js";
4343
import { dAppConfigModuleTypes } from "../internal/dAppConfig/di/dAppConfigModuleTypes.js";
4444
import { type DAppConfigService } from "../internal/dAppConfig/service/DAppConfigService.js";
4545
import { deviceModuleTypes } from "../internal/device/deviceModuleTypes.js";
@@ -170,19 +170,9 @@ export class LedgerButtonCore {
170170
.get<IsMobileUseCase>(platformModuleTypes.IsMobileUseCase)
171171
.execute();
172172

173-
const storedFiatCurrencyMaybe = await this.container
174-
.get<StorageService>(storageModuleTypes.StorageService)
175-
.getPreferredFiatCurrency();
176-
177-
const storedFiatCurrency = storedFiatCurrencyMaybe.orDefault(
178-
DEFAULT_FIAT_CURRENCY,
179-
);
180-
const preferredFiatCurrency = (
181-
SUPPORTED_FIAT_CURRENCIES as readonly string[]
182-
).includes(storedFiatCurrency)
183-
? storedFiatCurrency
184-
: DEFAULT_FIAT_CURRENCY;
185-
173+
const preferredFiatCurrency = await this.container
174+
.get<CurrencyService>(currencyModuleTypes.CurrencyService)
175+
.initialize();
186176
this._contextService.onEvent({
187177
type: "initialize_context",
188178
context: {
@@ -526,10 +516,16 @@ export class LedgerButtonCore {
526516
);
527517
}
528518

519+
getSupportedFiatCurrencies(): FiatCurrency[] {
520+
return this.container
521+
.get<CurrencyService>(currencyModuleTypes.CurrencyService)
522+
.getSupportedFiatCurrencies();
523+
}
524+
529525
async savePreferredFiatCurrency(currency: string): Promise<void> {
530526
this._logger.debug("Saving preferred fiat currency", { currency });
531527
await this.container
532-
.get<StorageService>(storageModuleTypes.StorageService)
528+
.get<CurrencyService>(currencyModuleTypes.CurrencyService)
533529
.savePreferredFiatCurrency(currency);
534530
this._contextService.onEvent({
535531
type: "preferred_fiat_currency_changed",

packages/ledger-button-core/src/api/model/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type {
88
Network,
99
Token,
1010
} from "../../internal/account/service/AccountService.js";
11+
export type { FiatCurrency } from "../../internal/currency/datasource/fiatCurrencyTypes.js";
1112
export type { PendingTransaction } from "../../internal/pending-transaction/model/PendingTransaction.js";
1213
export type {
1314
TransactionHistoryItem,

packages/ledger-button-core/src/internal/account/model/constant.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/ledger-button-core/src/internal/account/use-case/HydrateAccountWithBalanceUseCase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
type TokenBalance,
99
} from "../../balance/model/types.js";
1010
import type { BalanceService } from "../../balance/service/BalanceService.js";
11-
import { formatBalance } from "../../currency/formatCurrency.js";
11+
import { formatBalance } from "../../currency/currencyUtils.js";
1212
import { getChainIdFromCurrencyId } from "../../evm-provider/utils/chainUtils.js";
1313
import { loggerModuleTypes } from "../../logger/loggerModuleTypes.js";
1414
import type { LoggerPublisher } from "../../logger/service/LoggerPublisher.js";

packages/ledger-button-core/src/internal/account/use-case/hydrateAccountWithFiatUseCase.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function createMockContext(
3636
welcomeScreenCompleted: false,
3737
hasTrackingConsent: undefined,
3838
isMobilePlatform: false,
39-
preferredFiatCurrency: "usd",
39+
preferredFiatCurrency: "USD",
4040
...overrides,
4141
};
4242
}
@@ -99,7 +99,7 @@ describe("HydrateAccountWithFiatUseCase", () => {
9999
getContext: vi.fn(),
100100
onEvent: vi.fn(),
101101
};
102-
setPreferredFiatCurrency("usd");
102+
setPreferredFiatCurrency("USD");
103103

104104
useCase = new HydrateAccountWithFiatUseCase(
105105
createMockLoggerFactory(),
@@ -108,7 +108,7 @@ describe("HydrateAccountWithFiatUseCase", () => {
108108
);
109109

110110
vi.clearAllMocks();
111-
setPreferredFiatCurrency("usd");
111+
setPreferredFiatCurrency("USD");
112112
});
113113

114114
describe("execute", () => {
@@ -167,7 +167,7 @@ describe("HydrateAccountWithFiatUseCase", () => {
167167
});
168168
expect(
169169
mockCounterValueDataSource.getCounterValues,
170-
).toHaveBeenCalledWith(["ethereum"], "usd");
170+
).toHaveBeenCalledWith(["ethereum"], "USD");
171171
});
172172
});
173173

@@ -212,7 +212,7 @@ describe("HydrateAccountWithFiatUseCase", () => {
212212
});
213213
expect(
214214
mockCounterValueDataSource.getCounterValues,
215-
).toHaveBeenCalledWith(["ethereum"], "usd");
215+
).toHaveBeenCalledWith(["ethereum"], "USD");
216216
});
217217

218218
it("should use the preferred fiat currency from context", async () => {
@@ -222,7 +222,7 @@ describe("HydrateAccountWithFiatUseCase", () => {
222222
mockCounterValueDataSource.getCounterValues.mockResolvedValue(
223223
Right(counterValueResult),
224224
);
225-
setPreferredFiatCurrency("eur");
225+
setPreferredFiatCurrency("EUR");
226226

227227
const result = await useCase.execute(baseAccount);
228228

@@ -232,7 +232,7 @@ describe("HydrateAccountWithFiatUseCase", () => {
232232
});
233233
expect(
234234
mockCounterValueDataSource.getCounterValues,
235-
).toHaveBeenCalledWith(["ethereum"], "eur");
235+
).toHaveBeenCalledWith(["ethereum"], "EUR");
236236
});
237237

238238
it("should handle decimal balance values", async () => {
@@ -275,7 +275,7 @@ describe("HydrateAccountWithFiatUseCase", () => {
275275
});
276276
expect(
277277
mockCounterValueDataSource.getCounterValues,
278-
).toHaveBeenCalledWith(["ethereum", "ethereum/erc20/usdc"], "usd");
278+
).toHaveBeenCalledWith(["ethereum", "ethereum/erc20/usdc"], "USD");
279279
});
280280
});
281281
});

packages/ledger-button-core/src/internal/account/use-case/hydrateAccountWithFiatUseCase.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@ export class HydrateAccountWithFiatUseCase {
3232
}
3333

3434
async execute(account: Account): Promise<AccountWithFiat> {
35-
const targetCurrency =
36-
this.contextService.getContext().preferredFiatCurrency;
35+
const currency = this.contextService.getContext().preferredFiatCurrency;
3736
this.logHydrationStart(account);
3837

3938
const balance = account.balance ?? "0";
4039
const balanceNum = this.parseBalance(balance);
4140
if (Number.isNaN(balanceNum) || balanceNum === 0) {
42-
const currency = targetCurrency.toUpperCase();
4341
return enrichWithLoadingStates({
4442
...account,
4543
fiatBalance: { value: "0.00", currency },
@@ -49,7 +47,7 @@ export class HydrateAccountWithFiatUseCase {
4947

5048
const counterValuesResult = await this.fetchCounterValues(
5149
account,
52-
targetCurrency,
50+
currency,
5351
);
5452

5553
return counterValuesResult.caseOf<AccountWithFiat>({
@@ -62,8 +60,6 @@ export class HydrateAccountWithFiatUseCase {
6260
});
6361
},
6462
Right: (counterValues) => {
65-
const currency = targetCurrency.toUpperCase();
66-
6763
const accountFiatBalance = this.calculateAccountFiat(
6864
balance,
6965
counterValues[0]?.rate,

packages/ledger-button-core/src/internal/balance/model/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { formatBalance } from "../../currency/formatCurrency.js";
1+
import { formatBalance } from "../../currency/currencyUtils.js";
22

33
export type AccountBalance = {
44
nativeBalance: NativeBalance;

packages/ledger-button-core/src/internal/context/DefaultContextService.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { beforeEach, describe, expect, it, test, vi } from "vitest";
22

3-
import { DEFAULT_FIAT_CURRENCY } from "../account/model/constant.js";
43
import type { Account } from "../account/service/AccountService.js";
4+
import { DEFAULT_FIAT_CURRENCY } from "../currency/constant.js";
55
import type { Device } from "../device/model/Device.js";
66
import * as chainUtils from "../evm-provider/utils/chainUtils.js";
77
import type { LoggerPublisher } from "../logger/service/LoggerPublisher.js";
@@ -319,7 +319,9 @@ describe("DefaultContextService", () => {
319319

320320
describe("preferred_fiat_currency_changed event", () => {
321321
it("should set preferredFiatCurrency to the given currency", () => {
322-
expect(service.getContext().preferredFiatCurrency).toBe(DEFAULT_FIAT_CURRENCY);
322+
expect(service.getContext().preferredFiatCurrency).toBe(
323+
DEFAULT_FIAT_CURRENCY,
324+
);
323325

324326
service.onEvent({
325327
type: "preferred_fiat_currency_changed",

packages/ledger-button-core/src/internal/context/DefaultContextService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { BehaviorSubject, Observable } from "rxjs";
33

44
import { type ContextEvent } from "./model/ContextEvent.js";
55
import type { ButtonCoreContext } from "../../api/model/ButtonCoreContext.js";
6-
import { DEFAULT_FIAT_CURRENCY } from "../account/model/constant.js";
76
import {
87
type Account,
98
type DetailedAccount,
109
} from "../account/service/AccountService.js";
10+
import { DEFAULT_FIAT_CURRENCY } from "../currency/constant.js";
1111
import {
1212
getChainIdFromCurrencyId,
1313
getCurrencyIdFromChainId,

0 commit comments

Comments
 (0)