This repository has been archived by the owner on Oct 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ethtools.js
305 lines (234 loc) · 7.57 KB
/
ethtools.js
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
Template Controllers
@module Packages
*/
/**
Helper functions for ethereum dapps
@class [packages] ethereum:tools
@constructor
*/
var isMeteorPackage = true;
// setup LocalStore if not available
if(typeof LocalStore === 'undefined') {
isMeteorPackage = false;
LocalStore = {
get: function(){},
set: function(){}
};
}
// stup Tracker if not available
if(typeof Tracker === 'undefined')
Tracker = {
Dependency: function(){
return {
depend: function(){},
changed: function(){}
}
}
};
var dependency = new Tracker.Dependency;
/**
Check for supported currencies
@method supportedCurrencies
@param {String} unit
@return {String}
*/
var supportedCurrencies = function(unit){
return (unit === 'usd' ||
unit === 'eur' ||
unit === 'btc');
};
/**
Gets the ether unit if not set from localstorage
@method getUnit
@param {String} unit
@return {String}
*/
var getUnit = function(unit){
if(!_.isString(unit)) {
unit = LocalStore.get('dapp_etherUnit');
if(!unit) {
unit = 'ether';
LocalStore.set('dapp_etherUnit', unit);
}
}
return unit;
};
/**
Helper functions for ethereum dapps
@class EthTools
@constructor
*/
EthTools = {
lang: 'en'
};
if(isMeteorPackage) {
/**
Sets the default unit used by all EthTools functions, if no unit is provided.
EthTools.setUnit('ether')
@method setUnit
@param {String} unit the unit like 'ether', or 'eur'
@param {Boolean}
**/
EthTools.setUnit = function(unit){
if(supportedCurrencies(unit)) {
LocalStore.set('dapp_etherUnit', unit);
return true;
} else {
try {
web3.toWei(1, unit);
LocalStore.set('dapp_etherUnit', unit);
return true;
} catch(e) {
return false;
}
}
};
/**
Get the default unit used by all EthTools functions, if no unit is provided.
EthTools.getUnit()
@method getUnit
@return {String} unit the unit like 'ether', or 'eur'
**/
EthTools.getUnit = function(){
return LocalStore.get('dapp_etherUnit');
};
}
/**
Sets the locale to display numbers in different formats.
EthTools.setLocale('de')
@method language
@param {String} lang the locale like "de" or "de-DE"
**/
EthTools.setLocale = function(lang){
var lang = lang.substr(0,2).toLowerCase();
// numeral.language(lang);
EthTools.lang = lang;
dependency.changed();
return lang;
};
/**
Formats a given number
EthTools.formatNumber(10000, "0.0[000]")
@method formatNumber
@param {Number|String|BigNumber} number the number to format
@param {String} format the format string e.g. "0,0.0[000]" see http://numeraljs.com for more.
@return {String} The formated time
**/
EthTools.formatNumber = function(number, format){
var length = optionalLength = 0;
dependency.depend();
if(!_.isFinite(number) && !(number instanceof BigNumber))
number = 0;
if(format instanceof Spacebars.kw)
format = null;
if(_.isString(number))
number = new BigNumber(number, 10);
if(_.isFinite(number) && !_.isObject(number))
number = new BigNumber(number);
options = (EthTools.lang === 'en')
? { decimalSeparator: '.',
groupSeparator: ',',
groupSize: 3
}
: { decimalSeparator: ',',
groupSeparator: ' ',
groupSize: 3
};
BigNumber.config({ FORMAT: options });
// get segment positions (0,0. | 0 | [0])
if(format && ~format.indexOf('.')) {
var decimalPos = format.indexOf('.');
if(~format.indexOf('[')) {
length = format.substr(decimalPos, format.indexOf('[') - decimalPos).replace(/[\.\[\]]/g,'').length;
optionalLength = format.substr(format.indexOf('[')).replace(/[\[\]]/g,'').length;
} else {
length = format.substr(decimalPos).replace(/[\.\[\]]/g,'').length;
optionalLength = 0;
}
}
var fullLength = length + optionalLength;
number = number.toFormat(fullLength ? fullLength : undefined);
// if segements are detected, rebuild the number string
if(fullLength) {
var beforeDecimal = number.substr(0, number.indexOf(options.decimalSeparator) + 1);
var afterDecimal = number.replace(beforeDecimal, '').substr(0, length);
var afterDecimalOptional = number.replace(beforeDecimal, '').substr(length, optionalLength).replace(/0*$/,'');
beforeDecimal = beforeDecimal.replace(options.decimalSeparator, '');
return (!afterDecimal && !afterDecimalOptional)
? beforeDecimal
: beforeDecimal + options.decimalSeparator + afterDecimal + afterDecimalOptional;
// otherwise simply return the formated number
} else {
return number;
}
};
/**
Formats a number of wei to a balance.
EthTools.formatBalance(myNumber, "0,0.0[0000] unit")
@method (formatBalance)
@param {String} number
@param {String} format the format string
@return {String} The formatted number
**/
EthTools.formatBalance = function(number, format, unit){
dependency.depend();
if(!_.isFinite(number) && !(number instanceof BigNumber))
number = 0;
if(format instanceof Spacebars.kw)
format = null;
format = format || '0,0.[00000000]';
unit = getUnit(unit);
if(typeof EthTools.ticker !== 'undefined' && supportedCurrencies(unit)) {
var ticker = EthTools.ticker.findOne(unit, {fields: {price: 1}});
// convert first to ether
number = web3.fromWei(number, 'ether');
// then times the currency
if(ticker) {
number = (number instanceof BigNumber || _.isObject(number))
? number.times(ticker.price)
: new BigNumber(String(number), 10).times(ticker.price);
} else {
number = '0';
}
} else {
number = web3.fromWei(number, unit.toLowerCase());
}
var isUppercase = (format.indexOf('UNIT') !== -1);
var cleanedFormat = format.replace(/ *unit */i,'').replace(/ +/,'');
var format = format.replace(cleanedFormat, '__format__');
if(format.toLowerCase().indexOf('unit') !== -1) {
return format.replace('__format__', EthTools.formatNumber(number, cleanedFormat)).replace(/unit/i, (isUppercase ? unit.toUpperCase() : unit));
} else
return EthTools.formatNumber(number, cleanedFormat);
};
/**
Formats any of the supported currency to ethereum wei.
EthTools.toWei(myNumber, unit)
@method (toWei)
@param {String} number
@return {String} unit
**/
EthTools.toWei = function(number, unit){
if(!_.isFinite(number) && !(number instanceof BigNumber))
return number;
unit = getUnit(unit);
if(typeof EthTools.ticker !== 'undefined' && supportedCurrencies(unit)) {
var ticker = EthTools.ticker.findOne(unit, {fields: {price: 1}});
// convert first to ether
number = web3.toWei(number, 'ether');
// then times the currency
if(ticker) {
number = (number instanceof BigNumber || _.isObject(number))
? number.dividedBy(ticker.price)
: new BigNumber(String(number), 10).dividedBy(ticker.price);
// make sure the number is flat
number = number.round(0).toString(10);
} else {
number = '0';
}
} else {
number = web3.toWei(number, unit.toLowerCase());
}
return number;
};