refactor: allow bigint in createMoney

This commit is contained in:
kyranjamie
2023-06-21 16:16:01 +02:00
committed by Fara Woolf
parent cf7b8dbd62
commit 3672e8904a
4 changed files with 9 additions and 6 deletions

View File

@@ -27,7 +27,7 @@ export function PsbtUnsignedOutputItem({
const isOutputCurrentAddress =
addressFromScript === addressNativeSegwit || addressFromScript === addressTaproot;
const outputValueAsMoney = createMoney(Number(output.amount), 'BTC');
const outputValueAsMoney = createMoney(output.amount, 'BTC');
return (
<PsbtDecodedNodeLayout

View File

@@ -6,7 +6,6 @@ import BigNumber from 'bignumber.js';
import { CryptoCurrencies } from '@shared/models/currencies.model';
import { MarketData, createMarketData, createMarketPair } from '@shared/models/market.model';
import { Money, createMoney, currencyDecimalsMap } from '@shared/models/money.model';
import { createMoneyFromDecimal } from '@shared/models/money.model';
import { calculateMeanAverage } from '@app/common/math/calculate-averages';
import { convertAmountToFractionalUnit } from '@app/common/money/calculate-money';

View File

@@ -1,11 +1,11 @@
import BigNumber from 'bignumber.js';
import { BTC_DECIMALS, STX_DECIMALS } from '@shared/constants';
import { isUndefined } from '@shared/utils';
import { isBigInt, isUndefined } from '@shared/utils';
import type { Currencies } from './currencies.model';
export type NumType = BigNumber | number;
export type NumType = BigNumber | bigint | number;
export interface Money {
readonly amount: BigNumber;
@@ -52,7 +52,7 @@ export function createMoneyFromDecimal(
): Money {
throwWhenDecimalUnknown(symbol, resolution);
const decimals = getDecimalsOfSymbolIfKnown(symbol) ?? resolution;
const amount = new BigNumber(value).shiftedBy(decimals);
const amount = new BigNumber(isBigInt(value) ? value.toString() : value).shiftedBy(decimals);
return Object.freeze({ amount, symbol, decimals });
}
@@ -64,6 +64,6 @@ export function createMoneyFromDecimal(
export function createMoney(value: NumType, symbol: Currencies, resolution?: number): Money {
throwWhenDecimalUnknown(symbol, resolution);
const decimals = getDecimalsOfSymbolIfKnown(symbol) ?? resolution;
const amount = new BigNumber(value);
const amount = new BigNumber(isBigInt(value) ? value.toString() : value);
return Object.freeze({ amount, symbol, decimals });
}

View File

@@ -6,6 +6,10 @@ export function isString(value: unknown): value is string {
return typeof value === 'string';
}
export function isBigInt(value: unknown): value is bigint {
return typeof value === 'bigint';
}
export function isUndefined(value: unknown): value is undefined {
return typeof value === 'undefined';
}