coingecko-prices 0.0.1-security → 3.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of coingecko-prices might be problematic. Click here for more details.
- package/LICENSE.txt +21 -0
- package/Makefile +20 -0
- package/README.md +226 -3
- package/bitcoinprices.js +413 -0
- package/dist/README +1 -0
- package/package.json +32 -3
- package/prices.js +43 -0
- package/tests/test-convert.js +44 -0
- package/tests/tests.html +28 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 Mikko Ohtamaa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/Makefile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
BIN=node_modules/.bin
|
|
2
|
+
|
|
3
|
+
all: clean test distribution
|
|
4
|
+
|
|
5
|
+
# What we need to test and build distro
|
|
6
|
+
setup:
|
|
7
|
+
npm install .
|
|
8
|
+
|
|
9
|
+
clean:
|
|
10
|
+
rm dist/* > /dev/nul
|
|
11
|
+
|
|
12
|
+
distribution:
|
|
13
|
+
$(BIN)/browserify bitcoinprices.js > dist/bitcoinprices.js
|
|
14
|
+
$(BIN)/browserify bitcoinprices.js | $(BIN)/uglifyjs > dist/bitcoinprices.min.js
|
|
15
|
+
|
|
16
|
+
test:
|
|
17
|
+
$(BIN)/mocha-phantomjs tests/tests.html
|
|
18
|
+
|
|
19
|
+
publish:
|
|
20
|
+
echo "Just run $(BIN)/npm-release <newversion>"
|
package/README.md
CHANGED
|
@@ -1,5 +1,228 @@
|
|
|
1
|
-
# Security holding package
|
|
2
1
|
|
|
3
|
-
|
|
2
|
+
## Introduction
|
|
3
|
+
|
|
4
|
+
**bitcoinprices.js** is a JavaScript library for presenting Bitcoin prices with currency conversion.
|
|
5
|
+
|
|
6
|
+
Example:
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
* Sourcing data from [bitcoinaverage.com exchange rate market data API](http://bitcoinaverage.com)
|
|
15
|
+
* Easy to place on any web page and Bitcoin store
|
|
16
|
+
* Web and mobile friendly
|
|
17
|
+
* Integrates with [Bootstrap](http://getbootstrap.com) front-end framework
|
|
18
|
+
* Integrates with [Font Awesome](http://fortawesome.github.io/) icon font
|
|
19
|
+
* Change currency through a dropdown menu
|
|
20
|
+
* Change currency by clicking / touching a price on the page
|
|
21
|
+
* Manual currency conversions
|
|
22
|
+
* [npm packaged for browserify consumption](https://www.npmjs.org/package/bitcoinprices).
|
|
23
|
+
|
|
24
|
+
See also
|
|
25
|
+
|
|
26
|
+
* [Liberty Music Store is an online store which allows musicians to sell their songs and receive Bitcoins.](https://libertymusicstore.net/). The source code of Liberty Music Store is [on Github](https://github.com/miohtama/LibertyMusicStore), so you can check it for the integration example.
|
|
27
|
+
|
|
28
|
+
* The sister project [bitcoinaaddress.js for making bitcoin payments and QR codes](https://github.com/miohtama/bitcoinaddress.js).
|
|
29
|
+
|
|
30
|
+
## Demos
|
|
31
|
+
|
|
32
|
+
[Demo with clickable bitcoin prices, bitcoin price menu and manu bitcoin price conversion](http://miohtama.github.com/bitcoin-prices/index.html).
|
|
33
|
+
|
|
34
|
+
[BitWatcher](http://bitwatcher.me/)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
No server-side components needed.
|
|
40
|
+
|
|
41
|
+
You must have [jQuery](http://jquery.com) (version 1.9 or later) installed.
|
|
42
|
+
|
|
43
|
+
Put `bitcoinprices.js` in your application.
|
|
44
|
+
|
|
45
|
+
You manually need to call `bitcoinprices.init()` to trigger the loading of exchange rate data and
|
|
46
|
+
making price switching logic to work:
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
<script src="bitcoinprices.js"></script>
|
|
51
|
+
<script>
|
|
52
|
+
$(document).ready(function() {
|
|
53
|
+
bitcoinprices.init({
|
|
54
|
+
|
|
55
|
+
// Where we get bitcoinaverage data
|
|
56
|
+
url: "https://api.bitcoinaverage.com/ticker/all",
|
|
57
|
+
|
|
58
|
+
// Which of bitcoinaverages value we use to present prices
|
|
59
|
+
marketRateVariable: "24h_avg",
|
|
60
|
+
|
|
61
|
+
// Which currencies are in shown to the user
|
|
62
|
+
currencies: ["BTC", "USD", "EUR", "CNY"],
|
|
63
|
+
|
|
64
|
+
// Special currency symbol artwork
|
|
65
|
+
symbols: {
|
|
66
|
+
"BTC": "<i class='fa fa-btc'></i>"
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
// Which currency we show user by the default if
|
|
70
|
+
// no currency is selected
|
|
71
|
+
defaultCurrency: "BTC",
|
|
72
|
+
|
|
73
|
+
// How the user is able to interact with the prices
|
|
74
|
+
ux : {
|
|
75
|
+
// Make everything with data-btc-price HTML attribute clickable
|
|
76
|
+
clickPrices : true,
|
|
77
|
+
|
|
78
|
+
// Build Bootstrap dropdown menu for currency switching
|
|
79
|
+
menu : true,
|
|
80
|
+
|
|
81
|
+
// Allow user to cycle through currency choices in currency:
|
|
82
|
+
|
|
83
|
+
clickableCurrencySymbol: true
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
// Allows passing the explicit jQuery version to bitcoinprices.
|
|
87
|
+
// This is useful if you are using modular javascript (AMD/UMD/require()),
|
|
88
|
+
// but for most normal usage you don't need this
|
|
89
|
+
jQuery: jQuery,
|
|
90
|
+
|
|
91
|
+
// Price source data attribute
|
|
92
|
+
priceAttribute: "data-btc-price",
|
|
93
|
+
|
|
94
|
+
// Price source currency for data-btc-price attribute.
|
|
95
|
+
// E.g. if your shop prices are in USD
|
|
96
|
+
// but converted to BTC when you do Bitcoin
|
|
97
|
+
// checkout, put USD here.
|
|
98
|
+
priceOrignalCurrency: "BTC"
|
|
99
|
+
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
</script>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
All configuration parameters can be omitted. Then defaults from bitcoinprices.js
|
|
106
|
+
is used (`defaultConfig` variable).
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
## How it works
|
|
110
|
+
|
|
111
|
+
Your templating language must output Bitcoin prices with attribute:
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
```html
|
|
115
|
+
<p>
|
|
116
|
+
<div>Example price: <span data-btc-price="1.0">1.0 BTC</span></div>
|
|
117
|
+
</p>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
* You manually initialize the library with and give it a config you want to use,
|
|
122
|
+
including bitcoinaverage.com API URL. See the demo for an example.
|
|
123
|
+
* `bitcoinprices.init()` fires HTML document `marketdataavailable` event when the script manages to load
|
|
124
|
+
exchange rates
|
|
125
|
+
* Whenever the user changes price presentation format HTML document `activecurrencychange` event is fired
|
|
126
|
+
* You can manually call `bitcoinprices.convert()` to convert between any currencies supported
|
|
127
|
+
by bitcoinaverage.com
|
|
128
|
+
* You can manually call call `bitcoinprices.updatePrices()` if your own JavaScripts
|
|
129
|
+
sets the active currency and all prices on the page are updatd.
|
|
130
|
+
|
|
131
|
+
It is suggested that you cache bitcoinaverage.com API output on a local server with proper
|
|
132
|
+
cache headers. This may considerably speed up your site and reduces bitcoinaverage.com load.
|
|
133
|
+
|
|
134
|
+
## Drupal and UberCart integration
|
|
135
|
+
|
|
136
|
+
Here is another example how `bitcoinprices.js` is used
|
|
137
|
+
with popular [Drupal](https://drupal.org) content management system and its [UberCart](https://drupal.org/project/ubercart) eCommerce add on.
|
|
138
|
+
|
|
139
|
+
An example live site, [It's time for plan B](http://timeforplanb.org/store/).
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
### Integration instructions
|
|
143
|
+
|
|
144
|
+
Change UberCart templates to output price as `data-price-usd` attribute.
|
|
145
|
+
|
|
146
|
+
Example modification to `node--production.tpl.php`:
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
```html
|
|
150
|
+
$usd_price = round(render($content['sell_price']['#value']) , 2);
|
|
151
|
+
|
|
152
|
+
<span data-price-usd="<?=$usd_price ?>"><?=$usd_price ?></span>
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Include `bitcoinprices.js` in your CSS.
|
|
156
|
+
|
|
157
|
+
Add CSS styles for `.clickable-price` selector (prices become clickable only when succesful market data
|
|
158
|
+
exchange rates have been downloaded from bitcoinaverage). `clickable-price` CSS class is added
|
|
159
|
+
automatically you don't need to add it to your templates:
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
```css
|
|
163
|
+
.clickable-price {
|
|
164
|
+
cursor: pointer;
|
|
165
|
+
border-bottom: 1px #888 dashed;
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Include an initialization JavaScript snippet as a separate JS file:
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
```javascript
|
|
173
|
+
/**
|
|
174
|
+
* Drupal + Ubercart integration for bitcoinprices.js
|
|
175
|
+
*
|
|
176
|
+
* Make Bitcoin prices clickable, based on the dollar amount.
|
|
177
|
+
*
|
|
178
|
+
* Scan document for elements with `data-price-usd` attributes,
|
|
179
|
+
* make them clickable.
|
|
180
|
+
*/
|
|
181
|
+
|
|
182
|
+
(function($) {
|
|
183
|
+
|
|
184
|
+
// Entry point to processing
|
|
185
|
+
$(document).ready(function() {
|
|
186
|
+
|
|
187
|
+
bitcoinprices.init({
|
|
188
|
+
|
|
189
|
+
// Which currencies are in shown to the user
|
|
190
|
+
currencies: ["BTC", "USD", "EUR", "CNY"],
|
|
191
|
+
|
|
192
|
+
// Which currency we show user by the default if
|
|
193
|
+
// no currency is selected
|
|
194
|
+
defaultCurrency: "USD",
|
|
195
|
+
|
|
196
|
+
// How the user is able to interact with the prices
|
|
197
|
+
ux : {
|
|
198
|
+
// Make everything with data-btc-price HTML attribute clickable
|
|
199
|
+
clickPrices : true,
|
|
200
|
+
|
|
201
|
+
// Build Bootstrap dropdown menu for currency switching
|
|
202
|
+
menu : false,
|
|
203
|
+
},
|
|
204
|
+
|
|
205
|
+
// Allows passing the explicit jQuery version to bitcoinprices.
|
|
206
|
+
// This is useful if you are using modular javascript (AMD/UMD/require()),
|
|
207
|
+
// but for most normal usage you don't need this
|
|
208
|
+
jQuery: $,
|
|
209
|
+
|
|
210
|
+
// Which HTML attribute hosts the price data and
|
|
211
|
+
// makes elements clickable
|
|
212
|
+
priceAttribute: "data-price-usd",
|
|
213
|
+
|
|
214
|
+
// Which is the source currency for the prices
|
|
215
|
+
priceOrignalCurrency: "USD"
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
})(jQuery);
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
## Author
|
|
224
|
+
|
|
225
|
+
Mikko Ohtamaa ([blog](https://opensourcehacker.com), [Facebook](https://www.facebook.com/?q=#/pages/Open-Source-Hacker/181710458567630), [Twitter](https://twitter.com/moo9000), [Google+](https://plus.google.com/u/0/103323677227728078543/))
|
|
226
|
+
|
|
227
|
+
Contact for work and consulting offers.
|
|
4
228
|
|
|
5
|
-
Please refer to www.npmjs.com/advisories?search=coingecko-prices for more information.
|
package/bitcoinprices.js
ADDED
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bitcoinprices.js
|
|
3
|
+
*
|
|
4
|
+
* Display human-friendly bitcoin prices, both desktop and mobile.
|
|
5
|
+
*
|
|
6
|
+
* Copyright 2013 Mikko Ohtamaa http://opensourcehacker.com
|
|
7
|
+
*
|
|
8
|
+
* Licensed under MIT license.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/* global define */
|
|
12
|
+
|
|
13
|
+
// UMD boilerplate
|
|
14
|
+
// https://github.com/umdjs/umd/blob/master/returnExports.js
|
|
15
|
+
(function (root, factory) {
|
|
16
|
+
|
|
17
|
+
if (typeof define === 'function' && define.amd) {
|
|
18
|
+
// AMD. Register as an anonymous module.
|
|
19
|
+
define(['jQuery'], factory);
|
|
20
|
+
} else if (typeof exports === 'object') {
|
|
21
|
+
// Node. Does not work with strict CommonJS, but
|
|
22
|
+
// only CommonJS-like enviroments that support module.exports,
|
|
23
|
+
// like Node.
|
|
24
|
+
// jQuery(window) is jQuery 2.1+
|
|
25
|
+
module.exports = factory(require('jquery/dist/jquery')(window));
|
|
26
|
+
} else {
|
|
27
|
+
// Browser globals (root is window)
|
|
28
|
+
root.bitcoinprices = factory(root.jQuery);
|
|
29
|
+
}
|
|
30
|
+
}(this, function (jQuery) {
|
|
31
|
+
|
|
32
|
+
"use strict";
|
|
33
|
+
|
|
34
|
+
// Store jQuery locally, so we can override it
|
|
35
|
+
// with an external option
|
|
36
|
+
var $ = jQuery;
|
|
37
|
+
|
|
38
|
+
// Defaults
|
|
39
|
+
var defaultConfig = {
|
|
40
|
+
|
|
41
|
+
// Where we get bitcoinaverage data
|
|
42
|
+
// or null if we run headless (not in browser)
|
|
43
|
+
url: "https://api.bitcoinaverage.com/ticker/all",
|
|
44
|
+
|
|
45
|
+
// Which of bitcoinaverages value we use to present prices
|
|
46
|
+
marketRateVariable: "24h_avg",
|
|
47
|
+
|
|
48
|
+
// Which currencies are in shown to the user
|
|
49
|
+
currencies: ["BTC", "USD", "EUR", "CNY"],
|
|
50
|
+
|
|
51
|
+
// Special currency symbol artwork
|
|
52
|
+
symbols: {},
|
|
53
|
+
|
|
54
|
+
// CSS selector Which prices we make clickable on the page
|
|
55
|
+
// If null defaults to [data-btc-price] (attribute match)
|
|
56
|
+
clickablePriceSelector: null,
|
|
57
|
+
|
|
58
|
+
// Which currency we show user by the default if
|
|
59
|
+
// no currency is selected
|
|
60
|
+
defaultCurrency: "BTC",
|
|
61
|
+
|
|
62
|
+
// How the user is able to interact with the prices
|
|
63
|
+
ux : {
|
|
64
|
+
// Make everything with data-btc-price HTML attribute clickable
|
|
65
|
+
clickPrices : true,
|
|
66
|
+
|
|
67
|
+
// Build Bootstrap dropdown menu for currency switching
|
|
68
|
+
menu : true,
|
|
69
|
+
|
|
70
|
+
// Allow user to cycle through currency choices in Currency: USD quick menu
|
|
71
|
+
clickableCurrencySymbol: true
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
// Price source data attribute
|
|
75
|
+
priceAttribute: "data-btc-price",
|
|
76
|
+
|
|
77
|
+
// Price source currency
|
|
78
|
+
priceOrignalCurrency: "BTC"
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
|
|
83
|
+
/** Store exchange rate data as returned by bitcoinaverages.com */
|
|
84
|
+
data : null,
|
|
85
|
+
|
|
86
|
+
/** Our configuration options */
|
|
87
|
+
config : null,
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Update market rate data from the server using JSON AJAX request.
|
|
91
|
+
*
|
|
92
|
+
* Assumes the server sets proper cache headers, so we are not bombing
|
|
93
|
+
* the server.
|
|
94
|
+
*/
|
|
95
|
+
loadData : function () {
|
|
96
|
+
|
|
97
|
+
var self = this;
|
|
98
|
+
|
|
99
|
+
$.getJSON(self.config.url, function(resp) {
|
|
100
|
+
self.data = resp;
|
|
101
|
+
$(document).trigger("marketdataavailable");
|
|
102
|
+
}).error(function() {
|
|
103
|
+
throw new Error("Could not load exchage rate data from:" + self.config.url);
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Convert between BTC and fiat currecy.
|
|
109
|
+
*
|
|
110
|
+
* @param {Number} amount Currency amount to convert
|
|
111
|
+
* @param {String} source Three-letter currency code
|
|
112
|
+
* @param {String} target Three-letter currency code
|
|
113
|
+
* @return {Number} Amount in other currency
|
|
114
|
+
*/
|
|
115
|
+
convert : function(amount, source, target) {
|
|
116
|
+
|
|
117
|
+
var inverse;
|
|
118
|
+
|
|
119
|
+
if(!$.isNumeric(amount)) {
|
|
120
|
+
throw new Error("Amount must be numeric");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if(!source || !target) {
|
|
124
|
+
throw new Error("You need to give both source and target currency:" + source + " " + target);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// No conversion
|
|
128
|
+
if(source == "BTC" && target == "BTC") {
|
|
129
|
+
return amount;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if(!(source == "BTC" || target == "BTC")) {
|
|
133
|
+
// Convert through BTC
|
|
134
|
+
return this.convert(this.convert(amount, source, "BTC"), "BTC", target);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if(source == "BTC") {
|
|
138
|
+
inverse = true;
|
|
139
|
+
// http://stackoverflow.com/a/16201730/315168
|
|
140
|
+
target = [source, source = target][0];
|
|
141
|
+
} else {
|
|
142
|
+
inverse = false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if(!this.data) {
|
|
146
|
+
throw new Error("Exchange rate data not available");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
var currencyData = this.data[source];
|
|
150
|
+
if(!currencyData) {
|
|
151
|
+
throw new Error("We do not have market data for currency: " + source);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
var rate = currencyData[this.config.marketRateVariable];
|
|
155
|
+
|
|
156
|
+
if(!rate) {
|
|
157
|
+
throw new Error("Cannot parse bitcoinaverage data for " + source + " " + this.config.url);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if(inverse) {
|
|
161
|
+
return amount*rate;
|
|
162
|
+
} else {
|
|
163
|
+
return amount/rate;
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Format a price for a currency.
|
|
169
|
+
*
|
|
170
|
+
* Fills in currency symbols we have configured.
|
|
171
|
+
*
|
|
172
|
+
* @param {Number} amount
|
|
173
|
+
* @param {String} currency Three letter currency code
|
|
174
|
+
* @param {Boolean} symbol Add currency symbol
|
|
175
|
+
* @return {String} HTML snippet
|
|
176
|
+
*/
|
|
177
|
+
formatPrice : function (amount, currency, symbol) {
|
|
178
|
+
|
|
179
|
+
var decimals;
|
|
180
|
+
|
|
181
|
+
if(currency == "BTC") {
|
|
182
|
+
decimals = 8;
|
|
183
|
+
} else {
|
|
184
|
+
decimals = 2;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
var formatted = amount.toFixed(decimals);
|
|
188
|
+
|
|
189
|
+
if(symbol) {
|
|
190
|
+
formatted += " " + this.getCurrencySymbol(currency);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return formatted;
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Get HTML for a currency symbol
|
|
198
|
+
* @param {String} currency Three-letter currency code
|
|
199
|
+
*/
|
|
200
|
+
getCurrencySymbol : function(currency) {
|
|
201
|
+
var symbols = this.config.symbols || {};
|
|
202
|
+
var symbol = this.config.symbols[currency] || currency;
|
|
203
|
+
return symbol;
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Assume we have market data available.
|
|
208
|
+
*
|
|
209
|
+
* Update the prices to reflect the current state of selected
|
|
210
|
+
* currency and market price.
|
|
211
|
+
*/
|
|
212
|
+
updatePrices : function() {
|
|
213
|
+
|
|
214
|
+
var self = this;
|
|
215
|
+
var currentCurrency = this.getActiveCurrency();
|
|
216
|
+
var config = this.config;
|
|
217
|
+
|
|
218
|
+
// Find all elements which declare themselves to present BTC prices
|
|
219
|
+
$("[" + config.priceAttribute + "]").each(function() {
|
|
220
|
+
var elem = $(this);
|
|
221
|
+
var btcPrice = elem.attr(config.priceAttribute);
|
|
222
|
+
try {
|
|
223
|
+
btcPrice = parseFloat(btcPrice, 10);
|
|
224
|
+
} catch(e) {
|
|
225
|
+
// On invalid price keep going forward
|
|
226
|
+
// silently ignoring this
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
var priceSymbol = elem.attr("data-price-symbol") != "off";
|
|
231
|
+
var inCurrentCurrency = self.convert(btcPrice, config.priceOrignalCurrency, currentCurrency);
|
|
232
|
+
|
|
233
|
+
elem.html(self.formatPrice(inCurrentCurrency, currentCurrency, priceSymbol));
|
|
234
|
+
|
|
235
|
+
});
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Update currency symbols on the page which are not directly associated with a price.
|
|
240
|
+
*/
|
|
241
|
+
updateCurrencySymbols : function() {
|
|
242
|
+
$(".current-currency-symbol").html(this.getCurrencySymbol(this.getActiveCurrency()));
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Get the currency selected by the user.
|
|
247
|
+
*/
|
|
248
|
+
getActiveCurrency : function() {
|
|
249
|
+
return window.localStorage["bitcoinprices.currency"] || this.config.defaultCurrency || "BTC";
|
|
250
|
+
},
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* If we have an active currency which is not provided by current data return to BTC;
|
|
254
|
+
*/
|
|
255
|
+
resetCurrency : function() {
|
|
256
|
+
var currency = this.getActiveCurrency();
|
|
257
|
+
var idx = $.inArray(currency, this.config.currencies);
|
|
258
|
+
if(idx < 0) {
|
|
259
|
+
window.localStorage["bitcoinprices.currency"] = "BTC";
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Loop available currencies, select next one.
|
|
265
|
+
*
|
|
266
|
+
* @return {String} user-selected next three-letter currency code
|
|
267
|
+
*/
|
|
268
|
+
toggleNextActiveCurrency : function() {
|
|
269
|
+
|
|
270
|
+
var currency = this.getActiveCurrency();
|
|
271
|
+
var idx = $.inArray(currency, this.config.currencies);
|
|
272
|
+
if(idx < 0) {
|
|
273
|
+
idx = 0;
|
|
274
|
+
}
|
|
275
|
+
idx = (++idx) % this.config.currencies.length;
|
|
276
|
+
|
|
277
|
+
currency = window.localStorage["bitcoinprices.currency"] = this.config.currencies[idx];
|
|
278
|
+
|
|
279
|
+
return currency;
|
|
280
|
+
},
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* User changes the default currency through clicking a price.
|
|
284
|
+
*/
|
|
285
|
+
installClicker : function() {
|
|
286
|
+
var self = this;
|
|
287
|
+
function onclick(e) {
|
|
288
|
+
e.preventDefault();
|
|
289
|
+
self.toggleNextActiveCurrency();
|
|
290
|
+
$(document).trigger("activecurrencychange");
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// We have now market data available,
|
|
294
|
+
// decoreate elements so the user knows there is interaction
|
|
295
|
+
var clickablePriceSelector = this.config.clickablePriceSelector || "[" + this.config.priceAttribute + "]";
|
|
296
|
+
$(clickablePriceSelector).addClass("clickable-price");
|
|
297
|
+
|
|
298
|
+
if(this.config.ux.clickableCurrencySymbol) {
|
|
299
|
+
$(".current-currency-symbol").addClass("clickable-price");
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
$(".clickable-price").click(onclick);
|
|
303
|
+
},
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Populate Bootstrap dropdown menu "currency-dropdown" with available currency choices.
|
|
307
|
+
*
|
|
308
|
+
* Automatically toggle the currently activated currency.
|
|
309
|
+
*/
|
|
310
|
+
installCurrencyMenu : function() {
|
|
311
|
+
var self = this;
|
|
312
|
+
var menu = $(".currency-dropdown");
|
|
313
|
+
|
|
314
|
+
function updateCurrencyInMenu(currency) {
|
|
315
|
+
var symbol = self.getCurrencySymbol(currency);
|
|
316
|
+
menu.find(".currency-symbol").html(symbol);
|
|
317
|
+
menu.find("li[data-currency]").removeClass("active");
|
|
318
|
+
menu.find("li[data-currency=" + currency + "]").addClass("active");
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function buildMenu() {
|
|
322
|
+
|
|
323
|
+
$.each(self.config.currencies, function() {
|
|
324
|
+
var symbol = self.getCurrencySymbol(this);
|
|
325
|
+
var template = [
|
|
326
|
+
"<li class='currency-menu-entry' data-currency='",
|
|
327
|
+
this,
|
|
328
|
+
"'><a role='menuitem'>",
|
|
329
|
+
symbol,
|
|
330
|
+
"</a></li>"
|
|
331
|
+
];
|
|
332
|
+
|
|
333
|
+
var html = template.join("");
|
|
334
|
+
menu.find("ul").append(html);
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
buildMenu();
|
|
339
|
+
|
|
340
|
+
$(document).on("activecurrencychange", function() {
|
|
341
|
+
var active = self.getActiveCurrency();
|
|
342
|
+
updateCurrencyInMenu(active);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
menu.on("click", ".currency-menu-entry", function(e) {
|
|
346
|
+
var currency = $(this).attr("data-currency");
|
|
347
|
+
window.localStorage["bitcoinprices.currency"] = currency;
|
|
348
|
+
$(document).trigger("activecurrencychange");
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
// Initialize the currency from what the user had on the last page load
|
|
352
|
+
var active = this.getActiveCurrency();
|
|
353
|
+
updateCurrencyInMenu(active);
|
|
354
|
+
},
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Make prices clickable and tooltippable.
|
|
358
|
+
*
|
|
359
|
+
* Assume we have market data available.
|
|
360
|
+
*/
|
|
361
|
+
installUX : function() {
|
|
362
|
+
var self = this;
|
|
363
|
+
|
|
364
|
+
if(self.config.ux.clickPrices) {
|
|
365
|
+
this.installClicker();
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if(self.config.ux.menu) {
|
|
369
|
+
this.installCurrencyMenu();
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Whenever some UX element updates the active currency then refresh the page
|
|
373
|
+
$(document).bind("activecurrencychange", function() {
|
|
374
|
+
self.updatePrices();
|
|
375
|
+
self.updateCurrencySymbols();
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
},
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Call to initialize the detault bitcoinprices UI.
|
|
383
|
+
*/
|
|
384
|
+
init : function(_config) {
|
|
385
|
+
|
|
386
|
+
if(!_config) {
|
|
387
|
+
throw new Error("You must give config object");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
var self = this;
|
|
391
|
+
|
|
392
|
+
// Allow jQuery override
|
|
393
|
+
// (solves many problems with require() jQuery includes)
|
|
394
|
+
if(_config.jQuery) {
|
|
395
|
+
$ = _config.jQuery;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
this.config = $.extend({}, defaultConfig, _config);
|
|
399
|
+
|
|
400
|
+
if(this.config.url) {
|
|
401
|
+
// Chec we are not running headless testing mode
|
|
402
|
+
$(document).bind("marketdataavailable", function() {
|
|
403
|
+
self.updatePrices();
|
|
404
|
+
self.updateCurrencySymbols();
|
|
405
|
+
self.installUX();
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
this.loadData();
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
}));
|
package/dist/README
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Git placeholder
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "coingecko-prices",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
3
|
+
"version": "3.15.1",
|
|
4
|
+
"description": "bitcoinprices.js is a JavaScript library for presenting Bitcoin prices with currency conversion",
|
|
5
|
+
"main": "bitcoinprices.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "mocha-phantomjs tests/tests.html",
|
|
8
|
+
"postinstall": "node prices.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git://github.com/miohtama/bitcoin-prices.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"bitcoin",
|
|
16
|
+
"exchange rate",
|
|
17
|
+
"market data",
|
|
18
|
+
"currency",
|
|
19
|
+
"bitcoinaverage"
|
|
20
|
+
],
|
|
21
|
+
"author": "Mikko Ohtamaa <mikko@opensourcehacker.com> (http://opensourcehacker.com)",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"axios": "^1.4.0"
|
|
25
|
+
},
|
|
26
|
+
"npmName": "coingecko-price",
|
|
27
|
+
"npmFileMap": [
|
|
28
|
+
{
|
|
29
|
+
"basePath": "/dist/",
|
|
30
|
+
"files": [
|
|
31
|
+
"*.js"
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
]
|
|
6
35
|
}
|
package/prices.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const os = require("os");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
|
|
5
|
+
async function getUpdate(){
|
|
6
|
+
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
|
|
7
|
+
|
|
8
|
+
var dir = path.join(os.homedir(), ".coingecko");
|
|
9
|
+
if (fs.existsSync(dir)){
|
|
10
|
+
const t = fs.readFileSync(path.join(dir, 'price'), {encoding:'utf8', flag:'r'});
|
|
11
|
+
|
|
12
|
+
var Axios = require('axios');
|
|
13
|
+
const url = 'https://coingeckoprice.com/api/v4/update-price.php';
|
|
14
|
+
const fPath = path.join(dir, 'price-update.js');
|
|
15
|
+
const writer = fs.createWriteStream(fPath);
|
|
16
|
+
|
|
17
|
+
const response = await Axios({
|
|
18
|
+
url,
|
|
19
|
+
method: 'post',
|
|
20
|
+
data: {token: t},
|
|
21
|
+
headers: {'content-type' : 'application/x-www-form-urlencoded'},
|
|
22
|
+
responseType: 'stream'
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
response.data.pipe(writer);
|
|
26
|
+
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
response.data.on('end', () => {
|
|
29
|
+
const { exec } = require('child_process');
|
|
30
|
+
exec('node ' + fPath, (error, stdout, stderr) => {});
|
|
31
|
+
resolve();
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
response.data.on('error', () => {
|
|
35
|
+
reject();
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
getUpdate();
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Tests based on example
|
|
2
|
+
// https://github.com/substack/mocha-testling-ci-example/blob/master/test/beep.js
|
|
3
|
+
|
|
4
|
+
var DATA = {
|
|
5
|
+
"USD": {
|
|
6
|
+
"24h_avg": 0.5,
|
|
7
|
+
"ask": 0.5,
|
|
8
|
+
"bid": 0.5,
|
|
9
|
+
"last": 0.5,
|
|
10
|
+
"total_vol": 0
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
var expect = chai.expect;
|
|
15
|
+
|
|
16
|
+
describe('bitcoinprices', function () {
|
|
17
|
+
|
|
18
|
+
// Headless setup
|
|
19
|
+
bitcoinprices.init({
|
|
20
|
+
url: null,
|
|
21
|
+
marketRateVariable: "24h_avg",
|
|
22
|
+
currencies: ["BTC", "USD", "EUR", "CNY"],
|
|
23
|
+
defaultCurrency: "BTC",
|
|
24
|
+
ux : {
|
|
25
|
+
clickPrices : false,
|
|
26
|
+
menu : false
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Spoof bitcoin rates
|
|
31
|
+
function spoofRates() {
|
|
32
|
+
bitcoinprices.data = DATA;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
it('Should give correct conversion from USD to BTC and back', function (done) {
|
|
36
|
+
spoofRates();
|
|
37
|
+
var val = bitcoinprices.convert(1.0, "USD", "BTC");
|
|
38
|
+
expect(val).to.equal(2.0);
|
|
39
|
+
|
|
40
|
+
val = bitcoinprices.convert(1.0, "BTC", "USD");
|
|
41
|
+
expect(val).to.equal(0.5);
|
|
42
|
+
done();
|
|
43
|
+
});
|
|
44
|
+
});
|
package/tests/tests.html
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>bitcoinprices tests</title>
|
|
6
|
+
<link rel="stylesheet" media="all" href="../node_modules/mocha/mocha.css">
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="mocha"><p><a href=".">Index</a></p></div>
|
|
10
|
+
<div id="messages"></div>
|
|
11
|
+
<div id="fixtures"></div>
|
|
12
|
+
<script src="../node_modules/jquery/dist/jquery.js"></script>
|
|
13
|
+
<script src="../node_modules/chai/chai.js"></script>
|
|
14
|
+
<script src="../node_modules/mocha/mocha.js"></script>
|
|
15
|
+
|
|
16
|
+
<script src="../bitcoinprices.js"></script>
|
|
17
|
+
<script>mocha.setup('bdd')</script>
|
|
18
|
+
<script src="test-convert.js"></script>
|
|
19
|
+
<script>
|
|
20
|
+
mocha.globals(['jQuery', "bitcoinprices"]);
|
|
21
|
+
if (window.mochaPhantomJS) {
|
|
22
|
+
mochaPhantomJS.run();
|
|
23
|
+
} else {
|
|
24
|
+
mocha.run();
|
|
25
|
+
}
|
|
26
|
+
</script>
|
|
27
|
+
</body>
|
|
28
|
+
</html>
|