add bitcoin utils
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
import Decimal from 'decimal.js';
|
||||
|
||||
export const BTC_ATOMIC_PER_BTC = new Decimal(100_000_000);
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface BitcoinConfirmationTier {
|
||||
upToTotalFiat?: string;
|
||||
minConfirmations: number;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { convertBtcAtomicToBtc } from './convertBtcAtomicToBtc';
|
||||
|
||||
describe('convertBtcAtomicToBtc', () => {
|
||||
it('converts atomic units to BTC', () => {
|
||||
expect(convertBtcAtomicToBtc('100000000')).toBe('1.00000000');
|
||||
});
|
||||
|
||||
it('converts a single atomic unit to BTC', () => {
|
||||
expect(convertBtcAtomicToBtc('1')).toBe('0.00000001');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
import Decimal from 'decimal.js';
|
||||
import { BTC_ATOMIC_PER_BTC } from '../../consts/btcAtomicPerBtc';
|
||||
|
||||
export const convertBtcAtomicToBtc = (
|
||||
amountAtomic: string,
|
||||
rounding: Decimal.Rounding = Decimal.ROUND_HALF_UP
|
||||
): string => {
|
||||
return new Decimal(amountAtomic).div(BTC_ATOMIC_PER_BTC).toDecimalPlaces(8, rounding).toFixed(8);
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
import { convertBtcToBtcAtomic } from './convertBtcToBtcAtomic';
|
||||
|
||||
describe('convertBtcToBtcAtomic', () => {
|
||||
it('converts one BTC to atomic units', () => {
|
||||
expect(convertBtcToBtcAtomic('1')).toBe('100000000');
|
||||
});
|
||||
|
||||
it('converts the smallest display unit to atomic units', () => {
|
||||
expect(convertBtcToBtcAtomic('0.00000001')).toBe('1');
|
||||
});
|
||||
|
||||
it('returns an integer string without scientific notation', () => {
|
||||
expect(convertBtcToBtcAtomic('0.00125907')).toBe('125907');
|
||||
expect(convertBtcToBtcAtomic('0.00125907')).not.toMatch(/e/i);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
import Decimal from 'decimal.js';
|
||||
import { BTC_ATOMIC_PER_BTC } from '../../consts/btcAtomicPerBtc';
|
||||
|
||||
export const convertBtcToBtcAtomic = (amountBtc: string): string => {
|
||||
return new Decimal(amountBtc).mul(BTC_ATOMIC_PER_BTC).toDecimalPlaces(0, Decimal.ROUND_HALF_UP).toString();
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
import { convertFiatToBtc } from './convertFiatToBtc';
|
||||
|
||||
describe('convertFiatToBtc', () => {
|
||||
it('formats small amounts without scientific notation', () => {
|
||||
expect(convertFiatToBtc(0.01, 100_000)).toBe('0.00000010');
|
||||
});
|
||||
|
||||
it('converts fiat to BTC at the shop rate', () => {
|
||||
expect(convertFiatToBtc(50_000, 100_000)).toBe('0.50000000');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,5 @@
|
||||
import Decimal from 'decimal.js';
|
||||
|
||||
export const convertFiatToBtc = (fiatAmount: number, fiatPerBtc: number): string => {
|
||||
return new Decimal(fiatAmount).div(fiatPerBtc).toDecimalPlaces(8, Decimal.ROUND_HALF_UP).toFixed(8);
|
||||
};
|
||||
Reference in New Issue
Block a user