From 5c24912fb92f4c880ea6ccbfa023a282fd43972b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20G=C5=82ownia?= Date: Wed, 7 Feb 2024 13:11:16 +0100 Subject: [PATCH] chore: fix quantum to decimal places --- libs/liquidity/src/lib/liquidity-table.tsx | 12 +++++----- libs/utils/src/lib/format/number.spec.ts | 26 ++++------------------ libs/utils/src/lib/format/number.ts | 6 ++--- libs/utils/src/lib/format/range.spec.ts | 18 +++++++-------- 4 files changed, 22 insertions(+), 40 deletions(-) diff --git a/libs/liquidity/src/lib/liquidity-table.tsx b/libs/liquidity/src/lib/liquidity-table.tsx index 523297efb..db3b4dd44 100644 --- a/libs/liquidity/src/lib/liquidity-table.tsx +++ b/libs/liquidity/src/lib/liquidity-table.tsx @@ -94,7 +94,7 @@ export const LiquidityTable = ({ return `${addDecimalsFormatNumberQuantum( value, assetDecimalPlaces ?? 0, - quantum ?? 0 + quantum ?? 1 )}`; }; @@ -165,7 +165,7 @@ export const LiquidityTable = ({ return `${addDecimalsFormatNumberQuantum( newValue, assetDecimalPlaces ?? 0, - quantum ?? 0 + quantum ?? 1 )}`; }; @@ -227,7 +227,7 @@ export const LiquidityTable = ({ addDecimalsFormatNumberQuantum( pendingCommitmentAmount, assetDecimalPlaces ?? 0, - quantum ?? 0 + quantum ?? 1 ); if ( @@ -238,7 +238,7 @@ export const LiquidityTable = ({ addDecimalsFormatNumberQuantum( currentCommitmentAmount, assetDecimalPlaces ?? 0, - quantum ?? 0 + quantum ?? 1 ); return ( @@ -286,7 +286,7 @@ export const LiquidityTable = ({ addDecimalsFormatNumberQuantum( pendingCommitmentAmount, assetDecimalPlaces ?? 0, - quantum ?? 0 + quantum ?? 1 ); if ( @@ -297,7 +297,7 @@ export const LiquidityTable = ({ addDecimalsFormatNumberQuantum( currentCommitmentAmount, assetDecimalPlaces ?? 0, - quantum ?? 0 + quantum ?? 1 ); return ( diff --git a/libs/utils/src/lib/format/number.spec.ts b/libs/utils/src/lib/format/number.spec.ts index eb9d8ad3f..7e1ace053 100644 --- a/libs/utils/src/lib/format/number.spec.ts +++ b/libs/utils/src/lib/format/number.spec.ts @@ -32,28 +32,10 @@ describe('number utils', () => { ); it.each([ - { v: new BigNumber(123000), d: 5, o: '1.23', q: 0.1 }, - { v: new BigNumber(123000), d: 3, o: '123.00', q: 0.1 }, - { v: new BigNumber(123000), d: 1, o: '12,300.00', q: 0.1 }, - { v: new BigNumber(123001000), d: 2, o: '1,230,010.00', q: 0.1 }, - { v: new BigNumber(123001), d: 2, o: '1,230.01', q: 100 }, - { v: new BigNumber(123001), d: 2, o: '1,230.01', q: 0.1 }, - { v: new BigNumber(123001), d: 2, o: '1,230.01', q: 1 }, - - { - v: BigNumber('123456789123456789'), - d: 10, - o: '12,345,678.91234568', - q: '0.00003846', - }, - { - v: BigNumber('123456789123456789'), - d: 10, - o: '12,345,678.91', - q: '1', - }, - // USDT / USDC - { v: new BigNumber(12345678), d: 6, o: '12.35', q: 1000000 }, + { v: '1234000000000000000', d: 18, q: '1000000000000000000', o: '1.23' }, //vega + { v: '1235000000000000000', d: 18, q: '1000000000000000000', o: '1.24' }, //vega + { v: '1230012', d: 6, q: '1000000', o: '1.23' }, // USDT + { v: '1234560000000000000', d: 18, q: '500000000000000', o: '1.2346' }, // WEth ])( 'formats with addDecimalsFormatNumberQuantum given number correctly', ({ v, d, o, q }) => { diff --git a/libs/utils/src/lib/format/number.ts b/libs/utils/src/lib/format/number.ts index 47744d120..10d9d9fc8 100644 --- a/libs/utils/src/lib/format/number.ts +++ b/libs/utils/src/lib/format/number.ts @@ -167,9 +167,9 @@ export const addDecimalsFormatNumberQuantum = ( if (isNaN(Number(quantum))) { return addDecimalsFormatNumber(rawValue, decimalPlaces); } - const numberDP = - (typeof quantum === 'number' ? quantum.toString() : quantum).split('.')[1] - ?.length || 0; + const numberDP = Math.ceil( + Math.abs(Math.log10(toBigNum(quantum, decimalPlaces).toNumber())) + ); return addDecimalsFormatNumber( rawValue, decimalPlaces, diff --git a/libs/utils/src/lib/format/range.spec.ts b/libs/utils/src/lib/format/range.spec.ts index 7fb62d2f4..5af45f802 100644 --- a/libs/utils/src/lib/format/range.spec.ts +++ b/libs/utils/src/lib/format/range.spec.ts @@ -17,17 +17,17 @@ describe('formatValue', () => { }); it.each([ - { v: 123000, d: 5, o: '1.23', q: '0.1' }, - { v: 123000, d: 3, o: '123.00', q: '0.1' }, - { v: 123000, d: 1, o: '12,300.00', q: '0.1' }, - { v: 123001000, d: 2, o: '1,230,010.00', q: '0.1' }, + { v: 123000, d: 5, o: '1.23', q: '1' }, + { v: 123000, d: 3, o: '123.00', q: '1' }, + { v: 123000, d: 1, o: '12,300.00', q: '1' }, + { v: 123001000, d: 2, o: '1,230,010.00', q: '1' }, { v: 123001, d: 2, o: '1,230.01', q: '100' }, - { v: 123001, d: 2, o: '1,230.01', q: '0.1' }, + { v: 123001, d: 2, o: '1,230.01', q: '1' }, { v: '123456789123456789', d: 10, - o: '12,345,678.91234568', - q: '0.00003846', + o: '12,345,678.91235', + q: '384600', }, ])( 'formats with formatValue with quantum given number correctly', @@ -43,14 +43,14 @@ describe('formatRange', () => { max: 12300011111, d: 5, o: '1.23 - 123,000.11', - q: '0.1', + q: '1000', }, { min: 123000, max: 12300011111, d: 3, o: '123.00 - 12,300,011.11', - q: '0.1', + q: '100', }, { min: 123000,