diff --git a/packages/neuron-wallet/src/utils/shannonToCKB.ts b/packages/neuron-wallet/src/utils/shannonToCKB.ts deleted file mode 100644 index 82c1a01871..0000000000 --- a/packages/neuron-wallet/src/utils/shannonToCKB.ts +++ /dev/null @@ -1,18 +0,0 @@ -const DECIMAL = 8 - -const shannonToCKB = (shannon: bigint) => { - if (shannon === BigInt(0)) { - return `+0.${'0'.repeat(DECIMAL)}` - } - - const isNegative = shannon < 0 - const absStr = isNegative ? `${shannon}`.slice(1) : `${shannon}` - if (absStr.length <= DECIMAL) { - return `${isNegative ? '-' : '+'}0.${absStr.padStart(DECIMAL, '0')}` - } - const int = absStr.slice(0, -1 * DECIMAL) - const dec = absStr.slice(-1 * DECIMAL) - return `${isNegative ? '-' : '+'}${int}.${dec}` -} - -export default shannonToCKB diff --git a/packages/neuron-wallet/src/utils/to-csv-row.ts b/packages/neuron-wallet/src/utils/to-csv-row.ts index ce3d32e842..be58299716 100644 --- a/packages/neuron-wallet/src/utils/to-csv-row.ts +++ b/packages/neuron-wallet/src/utils/to-csv-row.ts @@ -1,5 +1,5 @@ import { t } from 'i18next' -import shannonToCKB from '../utils/shannonToCKB' +import { BIish, formatUnit, ckbDecimals } from '@ckb-lumos/bi' import sudtValueToAmount from '../utils/sudt-value-to-amount' import Transaction from '../models/chain/transaction' import { DEFAULT_UDT_SYMBOL } from '../utils/const' @@ -9,6 +9,15 @@ export const formatDatetime = (datetime: Date) => { return `${isoFmt.substr(0, 10)} ${isoFmt.substr(11, 12)}` } +export const formatAmount = (value: BIish) => { + return new Intl.NumberFormat('en-US', { + useGrouping: false, + signDisplay: 'always', + minimumFractionDigits: ckbDecimals, + maximumFractionDigits: ckbDecimals, + }).format(formatUnit(value, 'ckb') as any) +} + const toCSVRow = ( tx: Pick< Transaction, @@ -41,7 +50,7 @@ const toCSVRow = ( txType = +tx.sudtInfo.amount <= 0 ? `UDT ${SEND_TYPE}` : `UDT ${RECEIVE_TYPE}` } } else { - amount = shannonToCKB(BigInt(tx.value ?? '')) + amount = formatAmount(BigInt(tx.value ?? '0')) if (tx.nervosDao) { txType = `Nervos DAO` } else if (['create', 'destroy'].includes(tx.type || '')) { diff --git a/packages/neuron-wallet/tests/utils/shannonToCKB.test.ts b/packages/neuron-wallet/tests/utils/shannonToCKB.test.ts deleted file mode 100644 index 0f7eb6d8ad..0000000000 --- a/packages/neuron-wallet/tests/utils/shannonToCKB.test.ts +++ /dev/null @@ -1,48 +0,0 @@ -import shannonToCKB from '../../src/utils/shannonToCKB' - -const fixtures: { shannon: bigint; expected: string }[] = [ - { - shannon: BigInt(0), - expected: '+0.00000000', - }, - { - shannon: BigInt(1234567), - expected: '+0.01234567', - }, - { - shannon: BigInt(-1234567), - expected: '-0.01234567', - }, - { - shannon: BigInt(12345678), - expected: '+0.12345678', - }, - { - shannon: BigInt(-12345678), - expected: '-0.12345678', - }, - { - shannon: BigInt(123456789), - expected: '+1.23456789', - }, - { - shannon: BigInt(-123456789), - expected: '-1.23456789', - }, - { - shannon: BigInt(12345678900000000), - expected: '+123456789.00000000', - }, - { - shannon: BigInt(-12345678900000000), - expected: '-123456789.00000000', - }, -] - -describe('Test Shannon To CKB', () => { - const fixtureTable: [bigint, string][] = fixtures.map(fixture => [fixture.shannon, fixture.expected]) - test.each(fixtureTable)(`%s shannon => %s CKB`, (shannon, expected) => { - const actual = shannonToCKB(shannon) - expect(actual).toBe(expected) - }) -}) diff --git a/packages/neuron-wallet/tests/utils/to-csv-row/fixtures.json b/packages/neuron-wallet/tests/utils/to-csv-row/fixtures.json index 9cad69bfe1..ad1b9872aa 100644 --- a/packages/neuron-wallet/tests/utils/to-csv-row/fixtures.json +++ b/packages/neuron-wallet/tests/utils/to-csv-row/fixtures.json @@ -35,11 +35,11 @@ "timestamp": "1591954855123", "blockNumber": "0", "nervosDao": false, - "value": "-900000000", + "value": "-12345678", "description": "description" } }, - "expected": "2020-06-12 09:40:55.123,0,hash,Send,-9.00000000,,\"description\"\n" + "expected": "2020-06-12 09:40:55.123,0,hash,Send,-0.12345678,,\"description\"\n" }, "Nervos DAO": { "data": { @@ -49,11 +49,11 @@ "timestamp": "1591954855123", "blockNumber": "0", "nervosDao": true, - "value": "-900000000", + "value": "-123456789", "description": "description" } }, - "expected": "2020-06-12 09:40:55.123,0,hash,Nervos DAO,-9.00000000,,\"description\"\n" + "expected": "2020-06-12 09:40:55.123,0,hash,Nervos DAO,-1.23456789,,\"description\"\n" }, "Receive unknown UDT": { "data": { @@ -251,11 +251,11 @@ "timestamp": "1591954855123", "blockNumber": "0", "nervosDao": false, - "value": "-900000000", + "value": "12345678900000000", "description": "description" } }, - "expected": "2020-06-12 09:40:55.123,0,hash,Create CKB Asset Account,-9.00000000,,\"description\"\n" + "expected": "2020-06-12 09:40:55.123,0,hash,Create CKB Asset Account,+123456789.00000000,,\"description\"\n" }, "Destroy CKB Asset Account": { "data": { @@ -265,10 +265,10 @@ "timestamp": "1591954855123", "blockNumber": "0", "nervosDao": false, - "value": "-900000000", + "value": "-12345678900000000", "description": "description" } }, - "expected": "2020-06-12 09:40:55.123,0,hash,Destroy CKB Asset Account,-9.00000000,,\"description\"\n" + "expected": "2020-06-12 09:40:55.123,0,hash,Destroy CKB Asset Account,-123456789.00000000,,\"description\"\n" } }