|
@ -1,13 +1,19 @@ |
|
|
'use strict'; |
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
var errors = require('./errors'); |
|
|
var JSUtil = require('./util/js'); |
|
|
var JSUtil = require('./util/js'); |
|
|
|
|
|
|
|
|
|
|
|
var UNITS = { |
|
|
|
|
|
'BTC' : [1e8, 8], |
|
|
|
|
|
'mBTC' : [1e5, 5], |
|
|
|
|
|
'uBTC' : [1e2, 2], |
|
|
|
|
|
'bits' : [1e2, 2], |
|
|
|
|
|
'satoshis' : [1, 0] |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* |
|
|
|
|
|
* Bitcore Unit |
|
|
|
|
|
* |
|
|
|
|
|
* Utility for handling and converting bitcoins units. The supported units are |
|
|
* Utility for handling and converting bitcoins units. The supported units are |
|
|
* BTC, mBTC, bits and satoshis. A unit instance can be created with an |
|
|
* 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}. |
|
|
* amount and a unit code, or alternatively using static methods like {fromBTC}. |
|
|
* You can consult for different representation of a unit instance using it's |
|
|
* You can consult for different representation of a unit instance using it's |
|
|
* {to} method, the fixed unit methods like {toSatoshis} or alternatively using |
|
|
* {to} method, the fixed unit methods like {toSatoshis} or alternatively using |
|
@ -15,7 +21,6 @@ var JSUtil = require('./util/js'); |
|
|
* |
|
|
* |
|
|
* @example |
|
|
* @example |
|
|
* ```javascript
|
|
|
* ```javascript
|
|
|
* |
|
|
|
|
|
* var sats = Unit.fromBTC(1.3).toSatoshis(); |
|
|
* var sats = Unit.fromBTC(1.3).toSatoshis(); |
|
|
* var mili = Unit.fromBits(1.3).to(Unit.mBTC); |
|
|
* var mili = Unit.fromBits(1.3).to(Unit.mBTC); |
|
|
* var btc = new Unit(1.3, Unit.bits).BTC; |
|
|
* var btc = new Unit(1.3, Unit.bits).BTC; |
|
@ -45,22 +50,14 @@ function Unit(amount, code) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Object.keys(UNITS).forEach(defineAccesor); |
|
|
Object.keys(UNITS).forEach(defineAccesor); |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
var UNITS = { |
|
|
|
|
|
'BTC' : [1e8, 8], |
|
|
|
|
|
'mBTC' : [1e5, 5], |
|
|
|
|
|
'uBTC' : [1e2, 2], |
|
|
|
|
|
'bits' : [1e2, 2], |
|
|
|
|
|
'satoshis' : [1, 0] |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Object.keys(UNITS).forEach(function(key) { |
|
|
Object.keys(UNITS).forEach(function(key) { |
|
|
Unit[key] = key; |
|
|
Unit[key] = key; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return a Unit instance created from JSON string or object |
|
|
* Returns a Unit instance created from JSON string or object |
|
|
* |
|
|
* |
|
|
* @param {String|Object} json - JSON with keys: amount and code |
|
|
* @param {String|Object} json - JSON with keys: amount and code |
|
|
* @returns {Unit} A Unit instance |
|
|
* @returns {Unit} A Unit instance |
|
@ -73,7 +70,7 @@ Unit.fromJSON = function fromJSON(json){ |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return a Unit instance created from an amount in BTC |
|
|
* Returns a Unit instance created from an amount in BTC |
|
|
* |
|
|
* |
|
|
* @param {Number} amount - The amount in BTC |
|
|
* @param {Number} amount - The amount in BTC |
|
|
* @returns {Unit} A Unit instance |
|
|
* @returns {Unit} A Unit instance |
|
@ -83,7 +80,7 @@ Unit.fromBTC = function(amount) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return a Unit instance created from an amount in mBTC |
|
|
* Returns a Unit instance created from an amount in mBTC |
|
|
* |
|
|
* |
|
|
* @param {Number} amount - The amount in mBTC |
|
|
* @param {Number} amount - The amount in mBTC |
|
|
* @returns {Unit} A Unit instance |
|
|
* @returns {Unit} A Unit instance |
|
@ -93,7 +90,7 @@ Unit.fromMilis = function(amount) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return a Unit instance created from an amount in bits |
|
|
* Returns a Unit instance created from an amount in bits |
|
|
* |
|
|
* |
|
|
* @param {Number} amount - The amount in bits |
|
|
* @param {Number} amount - The amount in bits |
|
|
* @returns {Unit} A Unit instance |
|
|
* @returns {Unit} A Unit instance |
|
@ -103,7 +100,7 @@ Unit.fromMicros = Unit.fromBits = function(amount) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return a Unit instance created from an amount in satoshis |
|
|
* Returns a Unit instance created from an amount in satoshis |
|
|
* |
|
|
* |
|
|
* @param {Number} amount - The amount in satoshis |
|
|
* @param {Number} amount - The amount in satoshis |
|
|
* @returns {Unit} A Unit instance |
|
|
* @returns {Unit} A Unit instance |
|
@ -113,26 +110,29 @@ Unit.fromSatoshis = function(amount) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Unit.prototype._from = function(amount, code) { |
|
|
Unit.prototype._from = function(amount, code) { |
|
|
if (!UNITS[code]) throw Error('Unknown unit code'); |
|
|
if (!UNITS[code]) { |
|
|
|
|
|
throw new errors.Unit.UnknownCode(code); |
|
|
|
|
|
} |
|
|
return parseInt((amount * UNITS[code][0]).toFixed()); |
|
|
return parseInt((amount * UNITS[code][0]).toFixed()); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return the value represented in the specified unit |
|
|
* Returns the value represented in the specified unit |
|
|
* |
|
|
* |
|
|
* @param {string} code - The unit code |
|
|
* @param {string} code - The unit code |
|
|
* @returns {Number} The converted value |
|
|
* @returns {Number} The converted value |
|
|
*/ |
|
|
*/ |
|
|
Unit.prototype.to = function(code) { |
|
|
Unit.prototype.to = function(code) { |
|
|
if (!UNITS[code]) throw Error('Unknown unit code'); |
|
|
if (!UNITS[code]) { |
|
|
|
|
|
throw new errors.Unit.UnknownCode(code); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
var value = this._value / UNITS[code][0]; |
|
|
var value = this._value / UNITS[code][0]; |
|
|
return parseFloat(value.toFixed(UNITS[code][1])); |
|
|
return parseFloat(value.toFixed(UNITS[code][1])); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return the value represented in BTC |
|
|
* Returns the value represented in BTC |
|
|
* |
|
|
* |
|
|
* @returns {Number} The value converted to BTC |
|
|
* @returns {Number} The value converted to BTC |
|
|
*/ |
|
|
*/ |
|
@ -141,26 +141,25 @@ Unit.prototype.toBTC = function() { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* |
|
|
* Returns the value represented in mBTC |
|
|
* Will return the value represented in mBTC |
|
|
|
|
|
* |
|
|
* |
|
|
* @returns {Number} The value converted to mBTC |
|
|
* @returns {Number} The value converted to mBTC |
|
|
*/ |
|
|
*/ |
|
|
Unit.prototype.toMilis = function(code) { |
|
|
Unit.prototype.toMilis = function() { |
|
|
return this.to(Unit.mBTC); |
|
|
return this.to(Unit.mBTC); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return the value represented in bits |
|
|
* Returns the value represented in bits |
|
|
* |
|
|
* |
|
|
* @returns {Number} The value converted to bits |
|
|
* @returns {Number} The value converted to bits |
|
|
*/ |
|
|
*/ |
|
|
Unit.prototype.toMicros = Unit.prototype.toBits = function(code) { |
|
|
Unit.prototype.toMicros = Unit.prototype.toBits = function() { |
|
|
return this.to(Unit.bits); |
|
|
return this.to(Unit.bits); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return the value represented in satoshis |
|
|
* Returns the value represented in satoshis |
|
|
* |
|
|
* |
|
|
* @returns {Number} The value converted to satoshis |
|
|
* @returns {Number} The value converted to satoshis |
|
|
*/ |
|
|
*/ |
|
@ -169,7 +168,7 @@ Unit.prototype.toSatoshis = function() { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return a the string representation of the value in satoshis |
|
|
* Returns a the string representation of the value in satoshis |
|
|
* |
|
|
* |
|
|
* @returns {String} the value in satoshis |
|
|
* @returns {String} the value in satoshis |
|
|
*/ |
|
|
*/ |
|
@ -178,7 +177,7 @@ Unit.prototype.toString = function() { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return a plain object representation of the Unit |
|
|
* Returns a plain object representation of the Unit |
|
|
* |
|
|
* |
|
|
* @returns {Object} An object with the keys: amount and code |
|
|
* @returns {Object} An object with the keys: amount and code |
|
|
*/ |
|
|
*/ |
|
@ -194,7 +193,7 @@ Unit.prototype.toJSON = function toJSON() { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Will return a string formatted for the console |
|
|
* Returns a string formatted for the console |
|
|
* |
|
|
* |
|
|
* @returns {String} the value in satoshis |
|
|
* @returns {String} the value in satoshis |
|
|
*/ |
|
|
*/ |
|
|