Skip to content

Commit

Permalink
feat(AmountFormatter): Use formatAmount in TxBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak committed Feb 13, 2020
1 parent 50922e2 commit 994b779
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
4 changes: 2 additions & 2 deletions es/tx/builder/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigNumber } from 'bignumber.js'
import { toAettos, AE_AMOUNT_FORMATS } from '../../utils/amount-formatter'
import { AE_AMOUNT_FORMATS, formatAmount } from '../../utils/amount-formatter'
import { assertedType, rlp } from '../../utils/crypto'

import {
Expand Down Expand Up @@ -178,7 +178,7 @@ function transformParams (params, schema, { denomination } = {}) {
// console.log(params)
params = schema
.filter(([_, t]) => t === FIELD_TYPES.amount)
.reduce((acc, [key]) => ({ ...params, [key]: toAettos(params[key], { denomination }) }), params)
.reduce((acc, [key]) => ({ ...params, [key]: formatAmount(params[key], { denomination }) }), params)
return Object
.entries(params)
.reduce(
Expand Down
60 changes: 25 additions & 35 deletions es/utils/amount-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import { asBigNumber, isBigNumber } from './bignumber'
import { BigNumber } from 'bignumber.js'

const AE_MULT = 1e18
/**
* AE amount formats
* @type {{AE: string, AETTOS: string}}
Expand All @@ -36,21 +35,15 @@ export const AE_AMOUNT_FORMATS = {
MICRO_AE: 'microAe'
}

/**
*
* @type {{[string]: number}}
*/
const DENOMINATION_MAGNITUDE = {
[AE_AMOUNT_FORMATS.AE]: 18,
[AE_AMOUNT_FORMATS.AETTOS]: 0,
[AE_AMOUNT_FORMATS.MICRO_AE]: -6,
[AE_AMOUNT_FORMATS.PICO_AE]: -12
}

const AMOUNT_TO_AETTOS = {
[AE_AMOUNT_FORMATS.AE]: v => asBigNumber(v).times(AE_MULT),
[AE_AMOUNT_FORMATS.AETTOS]: asBigNumber
}

const AMOUNT_TO_AE = {
[AE_AMOUNT_FORMATS.AE]: asBigNumber,
[AE_AMOUNT_FORMATS.AETTOS]: v => asBigNumber(v).div(AE_MULT)
[AE_AMOUNT_FORMATS.MICRO_AE]: 12,
[AE_AMOUNT_FORMATS.PICO_AE]: 4
}

/**
Expand All @@ -60,25 +53,7 @@ const AMOUNT_TO_AE = {
* @param {String} [options.denomination='aettos'] denomination of amount, can be ['ae', 'aettos']
* @return {BigNumber}
*/
export const toAe = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS, target = AE_AMOUNT_FORMATS.AETTOS }) => {
if (!AMOUNT_TO_AETTOS[denomination]) throw new Error(`Invalid denomination. Current: ${denomination}, available [${Object.keys(AE_AMOUNT_FORMATS)}]`)
if (!isBigNumber(value)) throw new Error(`Value ${value} is not type of number`)
return AMOUNT_TO_AE[denomination](value)
}

export const formatAmount = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS, targetDenomination = AE_AMOUNT_FORMATS.AETTOS }) => {
if (!Object.values(AE_AMOUNT_FORMATS).includes(denomination)) throw new Error(`Invalid denomination. Current: ${denomination}, available [${Object.keys(AE_AMOUNT_FORMATS)}]`)
if (!Object.values(AE_AMOUNT_FORMATS).includes(targetDenomination)) throw new Error(`Invalid target denomination. Current: ${targetDenomination}, available [${Object.keys(AE_AMOUNT_FORMATS)}]`)
if (!isBigNumber(value)) throw new Error(`Value ${value} is not type of number`)
if (asBigNumber(value).lt(0)) throw Error('Value is less then 0')
// transform to aettos
value = asBigNumber(value).shiftedBy(DENOMINATION_MAGNITUDE[denomination])

if (targetDenomination === AE_AMOUNT_FORMATS.AETTOS) return value
return value
.shiftedBy(-DENOMINATION_MAGNITUDE[targetDenomination])
.toFixed()
}
export const toAe = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS }) => formatAmount(value, { denomination, targetDenomination: AE_AMOUNT_FORMATS.AE })

/**
* Convert amount to aettos
Expand All @@ -87,10 +62,25 @@ export const formatAmount = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS, t
* @param {String} [options.denomination='aettos'] denomination of amount, can be ['ae', 'aettos']
* @return {BigNumber}
*/
export const toAettos = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS } = {}) => {
if (!AMOUNT_TO_AETTOS[denomination]) throw new Error(`Invalid denomination. Current: ${denomination}, available [${Object.keys(AE_AMOUNT_FORMATS)}]`)
export const toAettos = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS } = {}) => formatAmount(value, { denomination })

/**
* Convert amount from one to other denomination
* @param {String|Number|BigNumber} value amount to convert
* @param {Object} [options={}] options
* @param {String} [options.denomination='aettos'] denomination of amount, can be ['ae', 'aettos']
* @param {String} [options.targetDenomination='aettos'] target denomination, can be ['ae', 'aettos']
* @return {BigNumber}
*/
export const formatAmount = (value, { denomination = AE_AMOUNT_FORMATS.AETTOS, targetDenomination = AE_AMOUNT_FORMATS.AETTOS }) => {
if (!Object.values(AE_AMOUNT_FORMATS).includes(denomination)) throw new Error(`Invalid denomination. Current: ${denomination}, available [${Object.keys(AE_AMOUNT_FORMATS)}]`)
if (!Object.values(AE_AMOUNT_FORMATS).includes(targetDenomination)) throw new Error(`Invalid target denomination. Current: ${targetDenomination}, available [${Object.keys(AE_AMOUNT_FORMATS)}]`)
if (!isBigNumber(value)) throw new Error(`Value ${value} is not type of number`)
return AMOUNT_TO_AETTOS[denomination](value)
if (asBigNumber(value).lt(0)) throw Error('Value is less then 0')

return asBigNumber(value)
.shiftedBy(DENOMINATION_MAGNITUDE[denomination] - DENOMINATION_MAGNITUDE[targetDenomination])
.toFixed()
}

const prefixes = [
Expand Down
8 changes: 7 additions & 1 deletion test/unit/amount-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ describe('Amount Formatter', function () {
[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.AE, 1]
[1, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.AE, 1],
[1, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AE, asBigNumber(0.00000000000001)],
[1, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(10000)],
[1e4, AE_AMOUNT_FORMATS.AETTOS, AE_AMOUNT_FORMATS.PICO_AE, asBigNumber(1)],
[0.0001, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AE, asBigNumber(0.000000000000000001)],
[0.00000000000001, AE_AMOUNT_FORMATS.AE, AE_AMOUNT_FORMATS.PICO_AE, asBigNumber(1)],
[0.0001, AE_AMOUNT_FORMATS.PICO_AE, AE_AMOUNT_FORMATS.AETTOS, asBigNumber(1)]
].forEach(
([v, dF, dT, e]) => parseBigNumber(e).should.be.equal(formatAmount(v, { denomination: dF, targetDenomination: dT }).toString(10))
)
Expand Down

0 comments on commit 994b779

Please sign in to comment.