mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-03 05:55:03 +01:00
fix: types for FiatRatesBySymbol, add typedObjectFromEntries util
This commit is contained in:
committed by
Peter Sanderson
parent
6e746fd978
commit
61e33e5410
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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';
|
||||
|
||||
5
packages/utils/src/typedObjectFromEntries.ts
Normal file
5
packages/utils/src/typedObjectFromEntries.ts
Normal 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;
|
||||
}
|
||||
21
packages/utils/tests/typedObjectFromEntries.type-test.ts
Normal file
21
packages/utils/tests/typedObjectFromEntries.type-test.ts
Normal 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, '']),
|
||||
);
|
||||
6
packages/utils/tests/typedObjectKeys.type-test.ts
Normal file
6
packages/utils/tests/typedObjectKeys.type-test.ts
Normal 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);
|
||||
@@ -122,6 +122,7 @@ export const findClosestTimestampValue = (
|
||||
*
|
||||
* @param {TickerId} ticker
|
||||
* @param {number[]} timestamps
|
||||
* @param {BaseCurrencyCode} fiatCurrencyCode
|
||||
*/
|
||||
export const getFiatRatesForTimestamps = async (
|
||||
ticker: TickerId,
|
||||
|
||||
Reference in New Issue
Block a user