Skip to content

Commit

Permalink
utils: fix amount to unit.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Mar 24, 2021
1 parent 2d1cbe9 commit 61e2787
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 53 deletions.
6 changes: 6 additions & 0 deletions lib/types.js
Expand Up @@ -97,3 +97,9 @@
* @see {module:network.types}
* @global
*/

/**
* One of `doo`, `uhns`, `mhns`, `hns`, `handshake`.
* @typedef {String} AmountUnitType
* @global
*/
75 changes: 22 additions & 53 deletions lib/ui/amount.js
Expand Up @@ -15,15 +15,15 @@ const pkg = require('../pkg');
* Amount
* Represents a currency amount (base unit internally).
* @alias module:currency.Amount
* @property {Amount} value
* @property {Number} value
*/

class Amount {
/**
* Create an amount.
* @constructor
* @param {(String|Number)?} value
* @param {String?} unit
* @param {AmountUnitType} [unit=doo]
*/

constructor(value, unit) {
Expand All @@ -36,8 +36,8 @@ class Amount {
/**
* Inject properties from options.
* @private
* @param {(String|Number)?} value
* @param {String?} unit
* @param {String|Number} value
* @param {AmountUnitType} [unit=doo]
* @returns {Amount}
*/

Expand All @@ -62,7 +62,7 @@ class Amount {

/**
* Get base unit string or value.
* @param {Boolean?} num
* @param {Boolean} [num=false] - Return a number.
* @returns {String|Amount}
*/

Expand All @@ -73,19 +73,9 @@ class Amount {
return this.value.toString(10);
}

/**
* Get bits string or value.
* @param {Boolean?} num
* @returns {String|Amount}
*/

toBits(num) {
return Amount.encode(this.value, 2, num);
}

/**
* Get mhns string or value.
* @param {Boolean?} num
* @param {Boolean} [num=false] - Return a number.
* @returns {String|Amount}
*/

Expand All @@ -95,7 +85,7 @@ class Amount {

/**
* Get currency string or value.
* @param {Boolean?} num
* @param {Boolean} [num=false] - Return a number.
* @returns {String|Amount}
*/

Expand All @@ -105,20 +95,20 @@ class Amount {

/**
* Get unit string or value.
* @param {String} unit
* @param {Boolean?} num
* @param {AmountUnitType} unit
* @param {Boolean} [num=false] - Return a number.
* @returns {String|Amount}
* @throws on incorrect unit type.
*/

to(unit, num) {
switch (unit) {
case pkg.base:
return this.toBase(num);
case `u${pkg.unit}`:
case 'bits':
return this.toBits(num);
return this.toBase(num);
case `m${pkg.unit}`:
return this.toMilli(num);
case pkg.unit:
case pkg.currency:
return this.toCoins(num);
}
Expand Down Expand Up @@ -160,18 +150,6 @@ class Amount {
return this;
}

/**
* Inject properties from bits.
* @private
* @param {Number|String} value
* @returns {Amount}
*/

fromBits(value) {
this.value = Amount.decode(value, 2);
return this;
}

/**
* Inject properties from mhns.
* @private
Expand Down Expand Up @@ -199,30 +177,30 @@ class Amount {
/**
* Inject properties from unit.
* @private
* @param {String} unit
* @param {AmountUnitType} unit
* @param {Number|String} value
* @returns {Amount}
* @throws on incorrect unit type.
*/

from(unit, value) {
switch (unit) {
case pkg.base:
return this.fromBase(value);
case `u${pkg.unit}`:
case 'bits':
return this.fromBits(value);
return this.fromBase(value);
case `m${pkg.unit}`:
return this.fromMilli(value);
case pkg.unit:
case pkg.currency:
return this.fromCoins(value);
}
throw new Error(`Unknown unit "${unit}".`);
}

/**
* Instantiate amount from options.
* @param {(String|Number)?} value
* @param {String?} unit
* @param {String|Number} value
* @param {AmountUnitType} [unit=doo]
* @returns {Amount}
*/

Expand Down Expand Up @@ -251,16 +229,6 @@ class Amount {
return new this().fromBase(value);
}

/**
* Instantiate amount from bits.
* @param {Number|String} value
* @returns {Amount}
*/

static fromBits(value) {
return new this().fromBits(value);
}

/**
* Instantiate amount from milliunit.
* @param {Number|String} value
Expand All @@ -283,7 +251,7 @@ class Amount {

/**
* Instantiate amount from unit.
* @param {String} unit
* @param {AmountUnitType} unit
* @param {Number|String} value
* @returns {Amount}
*/
Expand All @@ -306,7 +274,8 @@ class Amount {
* This function explicitly avoids any
* floating point arithmetic.
* @param {Amount} value - Base unit.
* @returns {String} Currency string.
* @param {Boolean} [num=false] - Return a number.
* @returns {String|Number} Currency string.
*/

static coin(value, num) {
Expand Down Expand Up @@ -334,7 +303,7 @@ class Amount {
* Safely convert base unit to a currency string.
* @param {Amount} value
* @param {Number} exp - Exponent.
* @param {Boolean} num - Return a number.
* @param {Boolean} [num=false] - Return a number.
* @returns {String|Number}
*/

Expand Down
60 changes: 60 additions & 0 deletions test/utils-test.js
Expand Up @@ -8,6 +8,27 @@ const Amount = require('../lib/ui/amount');
const fixed = require('../lib/utils/fixed');
const {COIN} = require('../lib/protocol/consensus');

const toFromVectors = [
{
value: 5460, // doos
units: {
doo: [5460, '5460'],
uhns: [5460, '5460'],
mhns: [5.46, '5.46'],
hns: [0.00546, '0.00546']
}
},
{
value: 54678 * 1000000,
units: {
doo: [54678 * 1000000, '54678000000'],
uhns: [54678 * 1000000, '54678000000'],
mhns: [54678 * 1000, '54678000.0'],
hns: [54678, '54678.0']
}
}
];

describe('Utils', function() {
it('should convert dollarydoos to hns', () => {
assert.strictEqual(Amount.coin(5460), '0.00546');
Expand Down Expand Up @@ -55,4 +76,43 @@ describe('Utils', function() {
assert.strictEqual(fixed.fromFloat(0.15645647, 8), 15645647);
assert.strictEqual(fixed.toFloat(15645647, 8), 0.15645647);
});

it('should convert Amount from units', () => {
for (const vector of toFromVectors) {
const units = Object.keys(vector.units);

for (const unit of units) {
const numAmount = Amount.from(unit, vector.units[unit][0]);
const strAmount = Amount.from(unit, vector.units[unit][1]);

assert.strictEqual(numAmount.toValue(), vector.value,
`Amount.from(${unit}, ${vector.units[unit][0]}) is not ${vector.value}.`
);

assert.strictEqual(strAmount.toValue(), vector.value,
`Amount.from(${unit}, '${vector.units[unit][1]}') is not ${vector.value}.`
);
}
}
});

it('should convert Amount to units', () => {
for (const vector of toFromVectors) {
const units = Object.keys(vector.units);
const amount = Amount.fromValue(vector.value);

for (const unit of units) {
const numValue = amount.to(unit, true);
const strValue = amount.to(unit, false);

assert.strictEqual(numValue, vector.units[unit][0],
`Amount(${vector.value}).to(${unit}, true) is not ${vector.units[unit][0]}.`
);

assert.strictEqual(strValue, vector.units[unit][1],
`Amount(${vector.value}).to(${unit}, false) is not '${vector.units[unit][1]}'.`
);
}
}
});
});

0 comments on commit 61e2787

Please sign in to comment.