Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/components/Send/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useState, useCallback, useEffect, useMemo } from 'react'
import { AppActions, StateDispatch } from 'states/stateProvider/reducer'
import { calculateCycles } from 'services/remote/wallets'

import { outputsToTotalCapacity, priceToFee } from 'utils/formatters'
import { outputsToTotalAmount, priceToFee } from 'utils/formatters'
import { verifyAddress, verifyAmount, verifyAmountRange, verifyTransactionOutputs } from 'utils/validators'
import { ErrorCode } from 'utils/const'
import { MAX_DECIMAL_DIGITS } from '../../utils/const'
Expand Down Expand Up @@ -58,7 +58,7 @@ const useOnTransactionChange = (
cyclesTimer = setTimeout(() => {
if (verifyTransactionOutputs(items)) {
setIsTransactionValid(true)
const totalAmount = outputsToTotalCapacity(items)
const totalAmount = outputsToTotalAmount(items)
setTotalAmount(totalAmount)
calculateCycles({
walletID,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { CapacityUnit } from 'utils/const'

const fixtures = [
{
ckb: {
amount: `1.234`,
unit: CapacityUnit.CKB,
},
expected: `123400000`,
},
{
ckb: {
amount: `1.23456789`,
unit: CapacityUnit.CKB,
},
expected: `123456789`,
},
{
ckb: {
amount: `1.0`,
unit: CapacityUnit.CKB,
},
expected: `100000000`,
},
{
ckb: {
amount: `1.`,
unit: CapacityUnit.CKB,
},
expected: `100000000`,
},
{
ckb: {
amount: `0.123`,
unit: CapacityUnit.CKB,
},
expected: `12300000`,
},
{
ckb: {
amount: `.123`,
unit: CapacityUnit.CKB,
},
expected: `12300000`,
},
{
ckb: {
amount: `12345678901234567890123456789012345678901234567890123456789012345678901234`,
unit: CapacityUnit.CKB,
},
expected: `1234567890123456789012345678901234567890123456789012345678901234567890123400000000`,
},
{
ckb: {
amount: `12345678901234567890123456789012345678901234567890123456789012345678901234`,
unit: CapacityUnit.CKKB,
},
expected: `1234567890123456789012345678901234567890123456789012345678901234567890123400000000000`,
},
{
ckb: {
amount: `12345678901234567890123456789012345678901234567890123456789012345678901234`,
unit: CapacityUnit.CKGB,
},
expected: `1234567890123456789012345678901234567890123456789012345678901234567890123400000000000000000`,
},
]

export default fixtures
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CapacityUnit } from 'utils/const'
import { CKBToShannonFormatter } from 'utils/formatters'
import fixtures from './fixtures'

const fixtureTable = fixtures.map(({ ckb: { amount, unit }, expected }) => [amount, unit, expected])

describe(`Verify CKB to Shannons formatter`, () => {
test.each(fixtureTable)(`%s %s => %s shannons`, (amount: string, unit: CapacityUnit, expected: string) => {
expect(CKBToShannonFormatter(amount, unit)).toBe(expected)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const fixtures: {
[title: string]: {
addresses: {
balance: string | undefined
}[]
expected: string
}
} = {
basic: {
addresses: [
{
balance: '100',
},
{
balance: '10000',
},
{
balance: '200',
},
{
balance: '10000',
},
],
expected: '20300',
},
'number large than MAX SAFE INTEGER': {
addresses: [
{
balance: '100',
},
{
balance: '10000',
},
{
balance: '200',
},
{
balance: '100000000000000000000000000000000000000000000000000000000000',
},
],
expected: '100000000000000000000000000000000000000000000000000000010300',
},
'address has negative balance': {
addresses: [
{
balance: '-100',
},
{
balance: '10000',
},
{
balance: '200',
},
{
balance: '100000000000000000000000000000000000000000000000000000000000',
},
],
expected: '100000000000000000000000000000000000000000000000000000010100',
},
'address has undefined balance': {
addresses: [
{
balance: undefined,
},
{
balance: '10000',
},
{
balance: '200',
},
{
balance: '100000000000000000000000000000000000000000000000000000000000',
},
],
expected: '100000000000000000000000000000000000000000000000000000010200',
},
}

export default fixtures
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { addressesToBalance } from 'utils/formatters'
import fixtures from './fixtures'

const fixtureTable = Object.entries(fixtures).map(([title, { addresses, expected }]) => [title, addresses, expected])

describe(`Verify addresses to balance`, () => {
test.each(fixtureTable)(`%s`, (_title: string, addresses: any, expected: string) => {
expect(addressesToBalance(addresses)).toBe(expected)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fixtures = [
{
value: {
shannons: `1234567890`,
unit: `CKB`,
exchange: `0.000000001`,
},
expected: `1.23456789 CKB`,
},
{
value: {
shannons: `1234567890`,
unit: `CKB`,
exchange: `0.00065`,
},
expected: `802,469.1285 CKB`,
},
{
value: {
shannons: `1234567890`,
unit: `CNY`,
exchange: `0.00065`,
},
expected: `802,469.1285 CNY`,
},
{
value: {
shannons: `1234567890123456789012345678901234567890123456789012345678901234567890`,
unit: `CNY`,
exchange: `0.65`,
},
expected: `802,469,128,580,246,912,858,024,691,285,802,469,128,580,246,912,858,024,691,285,802,469,128.5 CNY`,
},
{
value: {
shannons: `12345678901234567890123456789012345678901234567890123456789012345678901234`,
unit: `CNY`,
exchange: `0.65`,
},
expected: `8,024,691,285,802,469,128,580,246,912,858,024,691,285,802,469,128,580,246,912,858,024,691,285,802.1 CNY`,
},
]
export default fixtures
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { currencyFormatter } from 'utils/formatters'
import fixtures from './fixtures'

const fixtureTable = fixtures.map(({ value, expected }) => [value, expected])

describe(`Verify currency formatter`, () => {
test.each(fixtureTable)(`%j => %s`, (value: any, expected: string) => {
expect(currencyFormatter(value.shannons, value.unit, value.exchange)).toBe(expected)
})
})
Loading