-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(common): helpers to deal with bigint
- Loading branch information
1 parent
3bc4d12
commit 7161e4a
Showing
8 changed files
with
281 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {bigintFormatter} from './bigint-formatter' | ||
|
||
describe('bigintFormatter', () => { | ||
// NOTE: decimal and thousands separators are locale-dependent | ||
it('it works', () => { | ||
let value = BigInt(1_234_567_890) | ||
let decimalPlaces = 2 | ||
let formattedValue = bigintFormatter({value, decimalPlaces}) | ||
expect(formattedValue).toBe('12,345,678.90') | ||
|
||
value = BigInt(189) | ||
decimalPlaces = 6 | ||
formattedValue = bigintFormatter({value, decimalPlaces}) | ||
expect(formattedValue).toBe('0.000189') | ||
|
||
value = BigInt(1_000_000) | ||
formattedValue = bigintFormatter({value, decimalPlaces}) | ||
expect(formattedValue).toBe('1.000000') | ||
|
||
value = BigInt(1_000_000) | ||
decimalPlaces = 0 | ||
formattedValue = bigintFormatter({value, decimalPlaces}) | ||
expect(formattedValue).toBe('1,000,000') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {splitBigInt} from './split-bigint' | ||
|
||
export function bigintFormatter({ | ||
value, | ||
decimalPlaces, | ||
}: { | ||
value: bigint | ||
decimalPlaces: number | ||
}) { | ||
return splitBigInt(value, decimalPlaces).bn.toFormat(decimalPlaces) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,33 @@ | ||
import BigNumber from 'bignumber.js' | ||
import {freeze} from 'immer' | ||
|
||
export function splitBigInt( | ||
bigInt: bigint, | ||
decimalPlaces: number, | ||
): {int: bigint; dec: bigint} { | ||
export function splitBigInt(bigInt: bigint, decimalPlaces: number) { | ||
const scale: bigint = BigInt(10) ** BigInt(decimalPlaces) | ||
const integerPart: bigint = bigInt / scale | ||
const decimalPart: bigint = bigInt % scale | ||
|
||
const isNegative = bigInt < 0 | ||
const sign = isNegative ? '-' : '' | ||
const signAdjust = isNegative ? -1n : 1n | ||
|
||
const absoluteBigInt = signAdjust * bigInt | ||
const integer = (absoluteBigInt / scale).toString() | ||
const fraction = | ||
decimalPlaces > 0 | ||
? (absoluteBigInt % scale).toString().padStart(decimalPlaces, '0') | ||
: '' | ||
const separator = decimalPlaces > 0 ? '.' : '' | ||
|
||
const str = `${sign}${integer}${separator}${fraction}` | ||
|
||
const bn = new BigNumber(str) | ||
|
||
return freeze({ | ||
int: integerPart, | ||
dec: decimalPart, | ||
bn: new BigNumber(`${integerPart}.${decimalPart}`), | ||
decimalPlaces, | ||
|
||
bi: bigInt, | ||
integer, | ||
fraction, | ||
|
||
bn, | ||
str, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import {balanceFormatter} from './balance-formatter' | ||
import {tokenBalanceMocks} from '../adapters/token-balance.mocks' | ||
|
||
describe('balanceFormatter', () => { | ||
it('it works', () => { | ||
let formattedBalance = balanceFormatter({ | ||
template: '{{symbol}} {{value}} {{ticker}}', | ||
})(tokenBalanceMocks.primaryETH) | ||
|
||
expect(formattedBalance).toBe('Ξ 0.000000000001000000 ETH') | ||
|
||
formattedBalance = balanceFormatter({ | ||
template: '{{ticker}} {{value}} {{symbol}}', | ||
})(tokenBalanceMocks.primaryETH) | ||
|
||
expect(formattedBalance).toBe('ETH 0.000000000001000000 Ξ') | ||
|
||
formattedBalance = balanceFormatter()(tokenBalanceMocks.primaryETH) | ||
|
||
expect(formattedBalance).toBe('0.000000000001000000') | ||
|
||
formattedBalance = balanceFormatter({dropTraillingZeros: true})({ | ||
info: { | ||
...tokenBalanceMocks.primaryETH.info, | ||
decimals: 10, | ||
}, | ||
balance: 123_456_000_000n, | ||
}) | ||
|
||
expect(formattedBalance).toBe('12.3456') | ||
|
||
formattedBalance = balanceFormatter({dropTraillingZeros: true})({ | ||
info: { | ||
...tokenBalanceMocks.primaryETH.info, | ||
decimals: 10, | ||
}, | ||
balance: 123_456_000_100n, | ||
}) | ||
|
||
expect(formattedBalance).toBe('12.34560001') | ||
|
||
formattedBalance = balanceFormatter()({ | ||
info: { | ||
...tokenBalanceMocks.primaryETH.info, | ||
decimals: 10, | ||
}, | ||
balance: 123_456_000_000n, | ||
}) | ||
|
||
expect(formattedBalance).toBe('12.3456000000') | ||
|
||
formattedBalance = balanceFormatter()({ | ||
info: { | ||
...tokenBalanceMocks.primaryETH.info, | ||
decimals: 10, | ||
}, | ||
balance: 123_456_789_123_456n, | ||
}) | ||
|
||
expect(formattedBalance).toBe('12,345.6789123456') | ||
|
||
formattedBalance = balanceFormatter({dropTraillingZeros: true})({ | ||
info: { | ||
...tokenBalanceMocks.primaryETH.info, | ||
decimals: 0, | ||
}, | ||
balance: 10n, | ||
}) | ||
|
||
expect(formattedBalance).toBe('10') | ||
|
||
formattedBalance = balanceFormatter({dropTraillingZeros: true})({ | ||
info: { | ||
...tokenBalanceMocks.primaryETH.info, | ||
decimals: 1, | ||
}, | ||
balance: 0n, | ||
}) | ||
|
||
expect(formattedBalance).toBe('0.0') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {bigintFormatter} from '@yoroi/common' | ||
import {Portfolio} from '@yoroi/types' | ||
|
||
type BalanceFormatterConfig = Readonly<{ | ||
template?: string | ||
dropTraillingZeros?: boolean | ||
}> | ||
/** | ||
* Formats the balance of a token in a portfolio. | ||
* | ||
* @param config - The configuration options for the balance formatter. | ||
* @param config.template - The template string to format the balance. Default is '{{value}}'. | ||
* @param config.dropTraillingZeros - Whether to drop trailing zeros in the formatted balance. Default is false. | ||
* @description The template string can contain the following placeholders: {{symbol}}, {{ticker}}, and {{value}}. | ||
* | ||
* @returns A function that takes a token balance and returns the formatted balance string. | ||
*/ | ||
export function balanceFormatter({ | ||
dropTraillingZeros = false, | ||
template = '{{value}}', | ||
}: BalanceFormatterConfig = {}) { | ||
return ({ | ||
balance, | ||
info: {decimals, ticker, symbol}, | ||
}: Portfolio.Token.Balance) => { | ||
const fmtBalance = bigintFormatter({ | ||
value: balance, | ||
decimalPlaces: decimals, | ||
}) | ||
|
||
let trimmedValue = fmtBalance | ||
|
||
if (decimals > 0 && dropTraillingZeros) { | ||
// locale dependent, so it grabs the farthest first | ||
const decimalSeparatorIndex = Math.max( | ||
fmtBalance.lastIndexOf(','), | ||
fmtBalance.lastIndexOf('.'), | ||
) | ||
const nonZeroIndex = fmtBalance | ||
.substring(decimalSeparatorIndex) | ||
.search(/[1-9]0*$/) | ||
if (nonZeroIndex !== -1) { | ||
trimmedValue = fmtBalance.substring( | ||
0, | ||
decimalSeparatorIndex + nonZeroIndex + 1, | ||
) | ||
} | ||
} | ||
|
||
return template | ||
.replace('{{symbol}}', symbol) | ||
.replace('{{ticker}}', ticker) | ||
.replace('{{value}}', trimmedValue) | ||
} | ||
} |