fix: types for FiatRatesBySymbol, add typedObjectFromEntries util

This commit is contained in:
Peter Sanderson
2025-07-04 10:13:51 +02:00
committed by Peter Sanderson
parent 6e746fd978
commit 61e33e5410
7 changed files with 48 additions and 11 deletions

View File

@@ -1,5 +1,7 @@
import type tls from 'tls';
import type { BaseCurrencyCode } from '@suite-common/suite-config';
import type { Transaction as BlockbookTransaction, VinVout } from './blockbook';
import type {
AddressAlias,
@@ -116,9 +118,9 @@ export type TransactionDetail = {
totalOutput: string;
};
export interface FiatRatesBySymbol {
[symbol: string]: number | undefined;
}
export type FiatRatesBySymbol = {
[K in BaseCurrencyCode]?: number | undefined;
};
export interface AccountBalanceHistory {
time: number;

View File

@@ -1,7 +1,9 @@
import { fromUnixTime, getUnixTime, startOfMonth } from 'date-fns';
import { BaseCurrencyCode } from '@suite-common/suite-config';
import { toFiatCurrency } from '@suite-common/wallet-utils';
import type { FiatRatesBySymbol } from '@trezor/connect';
import { typedObjectFromEntries, typedObjectKeys } from '@trezor/utils';
import { BigNumber } from '@trezor/utils/src/bigNumber';
import {
@@ -15,14 +17,13 @@ import { ObjectType, TypeName, sumFiatValueMapInPlace } from './utilsShared';
const calcFiatValueMap = (
amount: string,
rates: FiatRatesBySymbol,
): { [k: string]: string | undefined } => {
const fiatValueMap: { [k: string]: string | undefined } = {};
Object.keys(rates).forEach(fiatSymbol => {
fiatValueMap[fiatSymbol] = toFiatCurrency(amount, rates?.[fiatSymbol]) ?? '0';
});
return fiatValueMap;
};
): { [K in BaseCurrencyCode]?: string | undefined } =>
typedObjectFromEntries(
typedObjectKeys(rates).map(fiatSymbol => [
fiatSymbol,
toFiatCurrency(amount, rates[fiatSymbol]) ?? '0',
]),
);
const isAccountAggregatedHistory = (
history: AggregatedAccountHistory | AggregatedDashboardHistory,

View File

@@ -55,6 +55,7 @@ export * from './throttler';
export * from './throwError';
export * from './topologicalSort';
export * from './typedEventEmitter';
export * from './typedObjectFromEntries';
export * from './typedObjectKeys';
export * from './urlToOnion';
export * from './zip';

View File

@@ -0,0 +1,5 @@
export function typedObjectFromEntries<T extends readonly (readonly [string, any])[]>(
entries: T,
): { [K in T[number] as K[0]]: K[1] } {
return Object.fromEntries(entries) as any;
}

View File

@@ -0,0 +1,21 @@
import { typedObjectFromEntries } from '../src/typedObjectFromEntries';
import { typedObjectKeys } from '../src/typedObjectKeys';
const map = {
a: 1,
b: 2,
};
export const _test: { [K in keyof typeof map]: number } = typedObjectFromEntries([
['a', 10],
['b', 20],
] as const);
export const _test2: { [K in keyof typeof map]: number } = typedObjectFromEntries(
typedObjectKeys(map).map(k => [k, map[k] * 2]),
);
// @ts-expect-error String cannot be assigned to number as map-value
export const _test3: { [K in keyof typeof map]: number } = typedObjectFromEntries(
typedObjectKeys(map).map(k => [k, '']),
);

View File

@@ -0,0 +1,6 @@
import { typedObjectKeys } from '../src/typedObjectKeys';
type AB = { a: number; b: number } | { b: string };
const ab: AB = { b: 'B' };
export const _test: 'b'[] = typedObjectKeys(ab);

View File

@@ -122,6 +122,7 @@ export const findClosestTimestampValue = (
*
* @param {TickerId} ticker
* @param {number[]} timestamps
* @param {BaseCurrencyCode} fiatCurrencyCode
*/
export const getFiatRatesForTimestamps = async (
ticker: TickerId,