Skip to content

Commit

Permalink
refactor: use BigNumber constructor instead of custom wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 28, 2021
1 parent a4d13bd commit 4488b4d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/utils/amount-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @example import { AmountFormatter } from '@aeternity/aepp-sdk'
*/
import BigNumber from 'bignumber.js'
import { asBigNumber, isBigNumber } from './bignumber'
import { isBigNumber } from './bignumber'

/**
* AE amount formats
Expand Down Expand Up @@ -90,7 +90,7 @@ export const formatAmount = (
if (!denominations.includes(targetDenomination)) throw new Error(`Invalid target denomination: ${targetDenomination}`)
if (!isBigNumber(value)) throw new Error(`Value ${value.toString()} is not type of number`)

return asBigNumber(value)
return new BigNumber(value)
.shiftedBy(DENOMINATION_MAGNITUDE[denomination] - DENOMINATION_MAGNITUDE[targetDenomination])
.toFixed()
}
Expand Down
10 changes: 1 addition & 9 deletions src/utils/bignumber.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Big Number Helpers
* @module @aeternity/aepp-sdk/es/utils/bignumber
* @example import { parseBigNumber, asBigNumber, isBigNumber, ceil } from '@aeternity/aepp-sdk/es/utils/bignumber'
* @example import { parseBigNumber, isBigNumber, ceil } from '@aeternity/aepp-sdk/es/utils/bignumber'
*/
import BigNumber from 'bignumber.js'

Expand All @@ -13,14 +13,6 @@ import BigNumber from 'bignumber.js'
export const parseBigNumber = (number: string | number | BigNumber): string =>
new BigNumber(number.toString()).toString(10)

/**
* Convert number to BigNumber instance
* @param {String|Number|BigNumber} number number to convert
* @return {BigNumber}
*/
export const asBigNumber = (number: string | number | BigNumber): BigNumber =>
new BigNumber(number.toString())

/**
* Check if value is BigNumber, Number or number string representation
* @param {String|Number|BigNumber} number number to convert
Expand Down
47 changes: 24 additions & 23 deletions test/unit/amount-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
*/

import { describe, it } from 'mocha'
import BigNumber from 'bignumber.js'
import '..'
import { AE_AMOUNT_FORMATS, formatAmount, toAe, toAettos } from '../../src/utils/amount-formatter'
import { asBigNumber, parseBigNumber } from '../../src/utils/bignumber'
import { parseBigNumber } from '../../src/utils/bignumber'

describe('Amount Formatter', function () {
it('to aettos', async () => {
Expand All @@ -34,36 +35,36 @@ describe('Amount Formatter', function () {
})
it('to Ae', () => {
[
[1, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(1).div(1e18)],
[10, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(10).div(1e18)],
[100, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(100).div(1e18)],
[10012312, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(10012312).div(1e18)],
[1, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1).div(1e18)],
[10, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(10).div(1e18)],
[100, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(100).div(1e18)],
[10012312, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(10012312).div(1e18)],
[1, AE_AMOUNT_FORMATS.AE, 1]
].forEach(
([v, d, e]) => parseBigNumber(e).should.be.equal(toAe(v, { denomination: d.toString() }))
)
})
it('format', () => {
[
[1, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(1e18)],
[10, AE_AMOUNT_FORMATS.AETTOS, AE_AMOUNT_FORMATS.AE, asBigNumber(10).div(1e18)],
[1e18, AE_AMOUNT_FORMATS.AETTOS, AE_AMOUNT_FORMATS.AE, asBigNumber(1)],
[10012312, AE_AMOUNT_FORMATS.AETTOS, AE_AMOUNT_FORMATS.AE, asBigNumber(10012312).div(1e18)],
[1, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1e18)],
[10, AE_AMOUNT_FORMATS.AETTOS, AE_AMOUNT_FORMATS.AE, new BigNumber(10).div(1e18)],
[1e18, AE_AMOUNT_FORMATS.AETTOS, AE_AMOUNT_FORMATS.AE, new BigNumber(1)],
[10012312, AE_AMOUNT_FORMATS.AETTOS, AE_AMOUNT_FORMATS.AE, new BigNumber(10012312).div(1e18)],
[1, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.AE, 1],
[1, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AE, asBigNumber(0.000000000001)],
[1, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(1000000)],
[1e6, AE_AMOUNT_FORMATS.AETTOS, AE_AMOUNT_FORMATS.PICO_AE, asBigNumber(1)],
[0.0001, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AE, asBigNumber(0.0000000000000001)],
[0.000000000001, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.PICO_AE, asBigNumber(1)],
[0.000001, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(1)],
[0.000000000001, AE_AMOUNT_FORMATS.MICRO_AE, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(1)],
[0.000001, AE_AMOUNT_FORMATS.MICRO_AE, AE_AMOUNT_FORMATS.PICO_AE, asBigNumber(1)],
[1, AE_AMOUNT_FORMATS.MILI_AE, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(1000000000000000)],
[1, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.MILI_AE, asBigNumber(1000)],
[1, AE_AMOUNT_FORMATS.NANO_AE, AE_AMOUNT_FORMATS.AE, asBigNumber(0.000000001)],
[1, AE_AMOUNT_FORMATS.NANO_AE, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(1000000000)],
[1, AE_AMOUNT_FORMATS.FEMTO_AE, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(1000)],
[1, AE_AMOUNT_FORMATS.NANO_AE, AE_AMOUNT_FORMATS.FEMTO_AE, asBigNumber(1000000)]
[1, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AE, new BigNumber(0.000000000001)],
[1, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1000000)],
[1e6, AE_AMOUNT_FORMATS.AETTOS, AE_AMOUNT_FORMATS.PICO_AE, new BigNumber(1)],
[0.0001, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AE, new BigNumber(0.0000000000000001)],
[0.000000000001, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.PICO_AE, new BigNumber(1)],
[0.000001, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1)],
[0.000000000001, AE_AMOUNT_FORMATS.MICRO_AE, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1)],
[0.000001, AE_AMOUNT_FORMATS.MICRO_AE, AE_AMOUNT_FORMATS.PICO_AE, new BigNumber(1)],
[1, AE_AMOUNT_FORMATS.MILI_AE, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1000000000000000)],
[1, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.MILI_AE, new BigNumber(1000)],
[1, AE_AMOUNT_FORMATS.NANO_AE, AE_AMOUNT_FORMATS.AE, new BigNumber(0.000000001)],
[1, AE_AMOUNT_FORMATS.NANO_AE, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1000000000)],
[1, AE_AMOUNT_FORMATS.FEMTO_AE, AE_AMOUNT_FORMATS.AETTOS, new BigNumber(1000)],
[1, AE_AMOUNT_FORMATS.NANO_AE, AE_AMOUNT_FORMATS.FEMTO_AE, new BigNumber(1000000)]
].forEach(
([v, dF, dT, e]) => parseBigNumber(e).should.be.equal(formatAmount(v, { denomination: dF.toString(), targetDenomination: dT.toString() }))
)
Expand Down

0 comments on commit 4488b4d

Please sign in to comment.