@ -1,5 +1,7 @@
'use strict' ;
var _ = require ( 'lodash' ) ;
var errors = require ( './errors' ) ;
var JSUtil = require ( './util/js' ) ;
@ -15,19 +17,23 @@ var UNITS = {
* Utility for handling and converting bitcoins units . The supported units are
* BTC , mBTC , bits ( also named uBTC ) and satoshis . A unit instance can be created with an
* amount and a unit code , or alternatively using static methods like { fromBTC } .
* It also allows to be created from a fiat amount and the exchange rate , or
* alternatively using the { fromFiat } static method .
* You can consult for different representation of a unit instance using it ' s
* { to } method , the fixed unit methods like { toSatoshis } or alternatively using
* the unit accessors .
* the unit accessors . It also can be converted to a fiat amount by providing the
* corresponding BTC / fiat exchange rate .
*
* @ example
* ` ` ` javascript
* var sats = Unit . fromBTC ( 1.3 ) . toSatoshis ( ) ;
* var mili = Unit . fromBits ( 1.3 ) . to ( Unit . mBTC ) ;
* var bits = Unit . fromFiat ( 1.3 , 350 ) . bits ;
* var btc = new Unit ( 1.3 , Unit . bits ) . BTC ;
* ` ` `
*
* @ param { Number } amount - The amount to be represented
* @ param { String } code - The unit of the amount
* @ param { String | Number } code - The unit of the amount or the exchange rate
* @ returns { Unit } A new instance of an Unit
* @ constructor
* /
@ -36,8 +42,14 @@ function Unit(amount, code) {
return new Unit ( amount , code ) ;
}
this . _ amount = amount ;
this . _ code = code ;
// convert fiat to BTC
if ( _ . isNumber ( code ) ) {
if ( code <= 0 ) {
throw new errors . Unit . InvalidRate ( code ) ;
}
amount = amount / code ;
code = Unit . BTC ;
}
this . _ value = this . _ from ( amount , code ) ;
@ -109,6 +121,17 @@ Unit.fromSatoshis = function(amount) {
return new Unit ( amount , Unit . satoshis ) ;
} ;
/ * *
* Returns a Unit instance created from a fiat amount and exchange rate .
*
* @ param { Number } amount - The amount in fiat
* @ param { Number } rate - The exchange rate BTC / fiat
* @ returns { Unit } A Unit instance
* /
Unit . fromFiat = function ( amount , rate ) {
return new Unit ( amount , rate ) ;
} ;
Unit . prototype . _ from = function ( amount , code ) {
if ( ! UNITS [ code ] ) {
throw new errors . Unit . UnknownCode ( code ) ;
@ -119,10 +142,17 @@ Unit.prototype._from = function(amount, code) {
/ * *
* Returns the value represented in the specified unit
*
* @ param { string } code - The unit code
* @ param { String | Number } code - The unit code or exchange rate
* @ returns { Number } The converted value
* /
Unit . prototype . to = function ( code ) {
if ( _ . isNumber ( code ) ) {
if ( code <= 0 ) {
throw new errors . Unit . InvalidRate ( code ) ;
}
return parseFloat ( ( this . BTC * code ) . toFixed ( 2 ) ) ;
}
if ( ! UNITS [ code ] ) {
throw new errors . Unit . UnknownCode ( code ) ;
}
@ -167,6 +197,16 @@ Unit.prototype.toSatoshis = function() {
return this . to ( Unit . satoshis ) ;
} ;
/ * *
* Returns the value represented in fiat
*
* @ param { string } rate - The exchange rate between BTC / currency
* @ returns { Number } The value converted to satoshis
* /
Unit . prototype . atRate = function ( rate ) {
return this . to ( rate ) ;
} ;
/ * *
* Returns a the string representation of the value in satoshis
*
@ -183,8 +223,8 @@ Unit.prototype.toString = function() {
* /
Unit . prototype . toObject = function toObject ( ) {
return {
amount : this . _ amount ,
code : this . _ code
amount : this . BTC ,
code : Unit . BTC
} ;
} ;