FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bitcore/lib/unit.js at master · pinkmagicdev/bitcore · GitHub
pinkmagicdev
bitcore
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
bitcore
/
lib
/
unit.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
238 lines (210 loc) · 5.76 KB
Breadcrumbs
bitcore
/
lib
/
unit.js
Copy path
File metadata and controls
238 lines (210 loc) · 5.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'use strict'
;
var
_
=
require
(
'lodash'
)
;
var
errors
=
require
(
'./errors'
)
;
var
$
=
require
(
'./util/preconditions'
)
;
var
UNITS
=
{
'BTC'
:
[
1e8
,
8
]
,
'mBTC'
:
[
1e5
,
5
]
,
'uBTC'
:
[
1e2
,
2
]
,
'bits'
:
[
1e2
,
2
]
,
'satoshis'
:
[
1
,
0
]
}
;
/**
* 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. 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|Number
} code - The unit of the amount or the exchange rate
*
@returns
{
Unit
} A new instance of an Unit
*
@constructor
*/
function
Unit
(
amount
,
code
)
{
if
(
!
(
this
instanceof
Unit
)
)
{
return
new
Unit
(
amount
,
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
)
;
var
self
=
this
;
var
defineAccesor
=
function
(
key
)
{
Object
.
defineProperty
(
self
,
key
,
{
get
:
function
(
)
{
return
self
.
to
(
key
)
;
}
,
enumerable
:
true
,
}
)
;
}
;
Object
.
keys
(
UNITS
)
.
forEach
(
defineAccesor
)
;
}
Object
.
keys
(
UNITS
)
.
forEach
(
function
(
key
)
{
Unit
[
key
]
=
key
;
}
)
;
/**
* Returns a Unit instance created from JSON string or object
*
*
@param
{
String|Object
} json - JSON with keys: amount and code
*
@returns
{
Unit
} A Unit instance
*/
Unit
.
fromObject
=
function
fromObject
(
data
)
{
$
.
checkArgument
(
_
.
isObject
(
data
)
,
'Argument is expected to be an object'
)
;
return
new
Unit
(
data
.
amount
,
data
.
code
)
;
}
;
/**
* Returns a Unit instance created from an amount in BTC
*
*
@param
{
Number
} amount - The amount in BTC
*
@returns
{
Unit
} A Unit instance
*/
Unit
.
fromBTC
=
function
(
amount
)
{
return
new
Unit
(
amount
,
Unit
.
BTC
)
;
}
;
/**
* Returns a Unit instance created from an amount in mBTC
*
*
@param
{
Number
} amount - The amount in mBTC
*
@returns
{
Unit
} A Unit instance
*/
Unit
.
fromMilis
=
function
(
amount
)
{
return
new
Unit
(
amount
,
Unit
.
mBTC
)
;
}
;
/**
* Returns a Unit instance created from an amount in bits
*
*
@param
{
Number
} amount - The amount in bits
*
@returns
{
Unit
} A Unit instance
*/
Unit
.
fromMicros
=
Unit
.
fromBits
=
function
(
amount
)
{
return
new
Unit
(
amount
,
Unit
.
bits
)
;
}
;
/**
* Returns a Unit instance created from an amount in satoshis
*
*
@param
{
Number
} amount - The amount in satoshis
*
@returns
{
Unit
} A Unit instance
*/
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
)
;
}
return
parseInt
(
(
amount
*
UNITS
[
code
]
[
0
]
)
.
toFixed
(
)
)
;
}
;
/**
* Returns the value represented in the specified unit
*
*
@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
)
;
}
var
value
=
this
.
_value
/
UNITS
[
code
]
[
0
]
;
return
parseFloat
(
value
.
toFixed
(
UNITS
[
code
]
[
1
]
)
)
;
}
;
/**
* Returns the value represented in BTC
*
*
@returns
{
Number
} The value converted to BTC
*/
Unit
.
prototype
.
toBTC
=
function
(
)
{
return
this
.
to
(
Unit
.
BTC
)
;
}
;
/**
* Returns the value represented in mBTC
*
*
@returns
{
Number
} The value converted to mBTC
*/
Unit
.
prototype
.
toMilis
=
function
(
)
{
return
this
.
to
(
Unit
.
mBTC
)
;
}
;
/**
* Returns the value represented in bits
*
*
@returns
{
Number
} The value converted to bits
*/
Unit
.
prototype
.
toMicros
=
Unit
.
prototype
.
toBits
=
function
(
)
{
return
this
.
to
(
Unit
.
bits
)
;
}
;
/**
* Returns the value represented in satoshis
*
*
@returns
{
Number
} The value converted to satoshis
*/
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
*
*
@returns
{
string
} the value in satoshis
*/
Unit
.
prototype
.
toString
=
function
(
)
{
return
this
.
satoshis
+
' satoshis'
;
}
;
/**
* Returns a plain object representation of the Unit
*
*
@returns
{
Object
} An object with the keys: amount and code
*/
Unit
.
prototype
.
toObject
=
Unit
.
prototype
.
toJSON
=
function
toObject
(
)
{
return
{
amount
:
this
.
BTC
,
code
:
Unit
.
BTC
}
;
}
;
/**
* Returns a string formatted for the console
*
*
@returns
{
string
} the value in satoshis
*/
Unit
.
prototype
.
inspect
=
function
(
)
{
return
'<Unit: '
+
this
.
toString
(
)
+
'>'
;
}
;
module
.
exports
=
Unit
;
Back
|
FazBrowse Home
|
New Git URL