bitcoin-conversion
Advanced tools
Comparing version 0.6.1 to 0.7.0
@@ -1196,7 +1196,8 @@ 'use strict'; | ||
var rateCache = {}; | ||
var CACHE_TTL_MS = 60000; // 1 minute cache | ||
var CACHE_TTL_MS = 60000; // 1 minute cache | ||
var getFiatBtcRate = /*#__PURE__*/function () { | ||
var _ref13 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee13(currency) { | ||
var now, cachedData, response, json, data, rate; | ||
var now, cachedData, response, data, rate, _response, _data, _rate, json; | ||
return runtime_1.wrap(function _callee13$(_context13) { | ||
@@ -1217,27 +1218,20 @@ while (1) { | ||
case 4: | ||
_context13.next = 6; | ||
return fetch("https://api.coindesk.com/v1/bpi/currentprice/" + currency.toLowerCase() + ".json"); | ||
_context13.prev = 4; | ||
_context13.next = 7; | ||
return fetch("https://api.coinbase.com/v2/prices/BTC-" + currency + "/spot"); | ||
case 6: | ||
case 7: | ||
response = _context13.sent; | ||
if (!(response.status !== 200)) { | ||
_context13.next = 12; | ||
if (!(response.status === 200)) { | ||
_context13.next = 15; | ||
break; | ||
} | ||
_context13.next = 10; | ||
_context13.next = 11; | ||
return response.json(); | ||
case 10: | ||
json = _context13.sent; | ||
throw Error(json); | ||
case 12: | ||
_context13.next = 14; | ||
return response.json(); | ||
case 14: | ||
case 11: | ||
data = _context13.sent; | ||
rate = currencyJs(data.bpi[currency].rate, { | ||
rate = currencyJs(data.data.amount, { | ||
separator: '', | ||
@@ -1253,3 +1247,54 @@ symbol: '' | ||
case 18: | ||
case 15: | ||
_context13.next = 20; | ||
break; | ||
case 17: | ||
_context13.prev = 17; | ||
_context13.t0 = _context13["catch"](4); | ||
console.warn('Coinbase API failed, falling back to Coindesk:', _context13.t0); | ||
case 20: | ||
_context13.prev = 20; | ||
_context13.next = 23; | ||
return fetch("https://api.coindesk.com/v1/bpi/currentprice/" + currency.toLowerCase() + ".json"); | ||
case 23: | ||
_response = _context13.sent; | ||
if (!(_response.status === 200)) { | ||
_context13.next = 31; | ||
break; | ||
} | ||
_context13.next = 27; | ||
return _response.json(); | ||
case 27: | ||
_data = _context13.sent; | ||
_rate = currencyJs(_data.bpi[currency].rate, { | ||
separator: '', | ||
symbol: '' | ||
}).format(); // Cache the new rate | ||
rateCache[currency] = { | ||
rate: _rate, | ||
timestamp: now | ||
}; | ||
return _context13.abrupt("return", _rate); | ||
case 31: | ||
_context13.next = 33; | ||
return _response.json(); | ||
case 33: | ||
json = _context13.sent; | ||
throw Error(json); | ||
case 37: | ||
_context13.prev = 37; | ||
_context13.t1 = _context13["catch"](20); | ||
throw new Error("Both Coinbase and Coindesk APIs failed: " + _context13.t1); | ||
case 40: | ||
case "end": | ||
@@ -1259,3 +1304,3 @@ return _context13.stop(); | ||
} | ||
}, _callee13); | ||
}, _callee13, null, [[4, 17], [20, 37]]); | ||
})); | ||
@@ -1262,0 +1307,0 @@ |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,r=(t=require("currency.js"))&&"object"==typeof t&&"default"in t?t.default:t,e=require("decimal.js-light");function n(t,r,e,n,o,a,i){try{var u=t[a](i),c=u.value}catch(t){return void e(t)}u.done?r(c):Promise.resolve(c).then(n,o)}function o(t){return function(){var r=this,e=arguments;return new Promise((function(o,a){var i=t.apply(r,e);function u(t){n(i,o,a,u,c,"next",t)}function c(t){n(i,o,a,u,c,"throw",t)}u(void 0)}))}}var a,i=(function(t){var r=function(t){var r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag";function u(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{u({},"")}catch(t){u=function(t,r,e){return t[r]=e}}function c(t,r,e,n){var o=Object.create((r&&r.prototype instanceof p?r:p).prototype),a=new E(n||[]);return o._invoke=function(t,r,e){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(e.method=o,e.arg=a;;){var i=e.delegate;if(i){var u=g(i,e);if(u){if(u===f)continue;return u}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var c=s(t,r,e);if("normal"===c.type){if(n=e.done?"completed":"suspendedYield",c.arg===f)continue;return{value:c.arg,done:e.done}}"throw"===c.type&&(n="completed",e.method="throw",e.arg=c.arg)}}}(t,e,a),o}function s(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var f={};function p(){}function h(){}function l(){}var v={};v[o]=function(){return this};var d=Object.getPrototypeOf,m=d&&d(d(k([])));m&&m!==r&&e.call(m,o)&&(v=m);var y=l.prototype=p.prototype=Object.create(v);function w(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function x(t,r){var n;this._invoke=function(o,a){function i(){return new r((function(n,i){!function n(o,a,i,u){var c=s(t[o],t,a);if("throw"!==c.type){var f=c.arg,p=f.value;return p&&"object"==typeof p&&e.call(p,"__await")?r.resolve(p.__await).then((function(t){n("next",t,i,u)}),(function(t){n("throw",t,i,u)})):r.resolve(p).then((function(t){f.value=t,i(f)}),(function(t){return n("throw",t,i,u)}))}u(c.arg)}(o,a,n,i)}))}return n=n?n.then(i,i):i()}}function g(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,g(t,r),"throw"===r.method))return f;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=s(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,f;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,f):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,f)}function b(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function L(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function E(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function k(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,a=function r(){for(;++n<t.length;)if(e.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=void 0,r.done=!0,r};return a.next=a}}return{next:j}}function j(){return{value:void 0,done:!0}}return h.prototype=y.constructor=l,l.constructor=h,h.displayName=u(l,i,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===h||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,l):(t.__proto__=l,u(t,i,"GeneratorFunction")),t.prototype=Object.create(y),t},t.awrap=function(t){return{__await:t}},w(x.prototype),x.prototype[a]=function(){return this},t.AsyncIterator=x,t.async=function(r,e,n,o,a){void 0===a&&(a=Promise);var i=new x(c(r,e,n,o),a);return t.isGeneratorFunction(e)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},w(y),u(y,i,"Generator"),y[o]=function(){return this},y.toString=function(){return"[object Generator]"},t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=k,E.prototype={constructor:E,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(L),!t)for(var r in this)"t"===r.charAt(0)&&e.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(e,n){return i.type="throw",i.arg=t,r.next=e,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=e.call(a,"catchLoc"),c=e.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,r){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=r&&r<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=r,a?(this.method="next",this.next=a.finallyLoc,f):this.complete(i)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),f},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),L(e),f}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;L(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:k(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=r}catch(t){Function("r","regeneratorRuntime = r")(r)}}(a={exports:{}}),a.exports),u=function(){var t=o(i.mark((function t(r,n){var o;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=new e.Decimal(r),t.next=3,L(n);case 3:return t.abrupt("return",o.mul(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),c=function(){var t=o(i.mark((function t(r){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=new e.Decimal(r),t.next=3,x();case 3:return t.abrupt("return",n.div(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),s=function(t){return new e.Decimal(t).mul(1e8).toNumber()},f=function(){var t=o(i.mark((function t(r){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=new e.Decimal(r),t.next=3,g();case 3:return t.abrupt("return",n.div(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),p=function(){var t=o(i.mark((function t(r,n){var o;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=new e.Decimal(r),t.next=3,w(n);case 3:return t.abrupt("return",o.times(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),h=function(){var t=o(i.mark((function t(r){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=new e.Decimal(r),t.next=3,x();case 3:return t.abrupt("return",n.mul(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),l=function(){var t=o(i.mark((function t(r){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=new e.Decimal(r),t.next=3,g();case 3:return t.abrupt("return",n.mul(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),v=function(t){return new e.Decimal(t).div(1e8).toNumber()},d=function(){var t=o(i.mark((function t(r,e){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=v(r),t.next=3,u(n,e);case 3:return t.abrupt("return",t.sent);case 5:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),m=function(){var t=o(i.mark((function t(r,n){var o,a;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=new e.Decimal(r),t.next=3,L(n);case 3:return a=new e.Decimal(t.sent),t.abrupt("return",o.div(a).toNumber());case 6:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),y=function(){var t=o(i.mark((function t(r,e){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,m(r,e);case 2:return t.abrupt("return",s(t.sent));case 4:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),w=function(){var t=o(i.mark((function t(r){var e;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,fetch("https://min-api.cryptocompare.com/data/price?fsym="+r+"&tsyms=BTC");case 2:if(200===(e=t.sent).status){t.next=8;break}return t.next=6,e.json();case 6:throw Error(t.sent);case 8:return t.next=10,e.json();case 10:return t.abrupt("return",t.sent.BTC);case 12:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),x=function(){var t=o(i.mark((function t(){var r;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,fetch("https://xchain.io/api/asset/PEPECASH");case 2:if(200===(r=t.sent).status){t.next=8;break}return t.next=6,r.json();case 6:throw Error(t.sent);case 8:return t.next=10,r.json();case 10:return t.abrupt("return",t.sent.estimated_value.btc);case 12:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}(),g=function(){var t=o(i.mark((function t(){var r;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,fetch("https://xchain.io/api/asset/XCP");case 2:if(200===(r=t.sent).status){t.next=8;break}return t.next=6,r.json();case 6:throw Error(t.sent);case 8:return t.next=10,r.json();case 10:return t.abrupt("return",t.sent.estimated_value.btc);case 12:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}(),b={},L=function(){var t=o(i.mark((function t(e){var n,o,a,u;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=Date.now(),!((o=b[e])&&n-o.timestamp<6e4)){t.next=4;break}return t.abrupt("return",o.rate);case 4:return t.next=6,fetch("https://api.coindesk.com/v1/bpi/currentprice/"+e.toLowerCase()+".json");case 6:if(200===(a=t.sent).status){t.next=12;break}return t.next=10,a.json();case 10:throw Error(t.sent);case 12:return t.next=14,a.json();case 14:return u=r(t.sent.bpi[e].rate,{separator:"",symbol:""}).format(),b[e]={rate:u,timestamp:n},t.abrupt("return",u);case 18:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}();exports.bitcoinToFiat=u,exports.bitcoinToPepecash=c,exports.bitcoinToSatoshis=s,exports.bitcoinToXcp=f,exports.cryptoToBitcoin=p,exports.fiatToBitcoin=m,exports.fiatToSatoshis=y,exports.getCryptoBtcRate=w,exports.getFiatBtcRate=L,exports.getPepecashBtcRate=x,exports.getXcpBtcRate=g,exports.pepecashToBitcoin=h,exports.satoshisToBitcoin=v,exports.satoshisToFiat=d,exports.xcpToBitcoin=l; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,r=(t=require("currency.js"))&&"object"==typeof t&&"default"in t?t.default:t,e=require("decimal.js-light");function n(t,r,e,n,o,a,i){try{var c=t[a](i),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).then(n,o)}function o(t){return function(){var r=this,e=arguments;return new Promise((function(o,a){var i=t.apply(r,e);function c(t){n(i,o,a,c,u,"next",t)}function u(t){n(i,o,a,c,u,"throw",t)}c(void 0)}))}}var a,i=(function(t){var r=function(t){var r=Object.prototype,e=r.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag";function c(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{c({},"")}catch(t){c=function(t,r,e){return t[r]=e}}function u(t,r,e,n){var o=Object.create((r&&r.prototype instanceof f?r:f).prototype),a=new E(n||[]);return o._invoke=function(t,r,e){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(e.method=o,e.arg=a;;){var i=e.delegate;if(i){var c=b(i,e);if(c){if(c===p)continue;return c}}if("next"===e.method)e.sent=e._sent=e.arg;else if("throw"===e.method){if("suspendedStart"===n)throw n="completed",e.arg;e.dispatchException(e.arg)}else"return"===e.method&&e.abrupt("return",e.arg);n="executing";var u=s(t,r,e);if("normal"===u.type){if(n=e.done?"completed":"suspendedYield",u.arg===p)continue;return{value:u.arg,done:e.done}}"throw"===u.type&&(n="completed",e.method="throw",e.arg=u.arg)}}}(t,e,a),o}function s(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var p={};function f(){}function h(){}function l(){}var v={};v[o]=function(){return this};var d=Object.getPrototypeOf,m=d&&d(d(L([])));m&&m!==r&&e.call(m,o)&&(v=m);var y=l.prototype=f.prototype=Object.create(v);function x(t){["next","throw","return"].forEach((function(r){c(t,r,(function(t){return this._invoke(r,t)}))}))}function w(t,r){var n;this._invoke=function(o,a){function i(){return new r((function(n,i){!function n(o,a,i,c){var u=s(t[o],t,a);if("throw"!==u.type){var p=u.arg,f=p.value;return f&&"object"==typeof f&&e.call(f,"__await")?r.resolve(f.__await).then((function(t){n("next",t,i,c)}),(function(t){n("throw",t,i,c)})):r.resolve(f).then((function(t){p.value=t,i(p)}),(function(t){return n("throw",t,i,c)}))}c(u.arg)}(o,a,n,i)}))}return n=n?n.then(i,i):i()}}function b(t,r){var e=t.iterator[r.method];if(void 0===e){if(r.delegate=null,"throw"===r.method){if(t.iterator.return&&(r.method="return",r.arg=void 0,b(t,r),"throw"===r.method))return p;r.method="throw",r.arg=new TypeError("The iterator does not provide a 'throw' method")}return p}var n=s(e,t.iterator,r.arg);if("throw"===n.type)return r.method="throw",r.arg=n.arg,r.delegate=null,p;var o=n.arg;return o?o.done?(r[t.resultName]=o.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,p):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,p)}function g(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function k(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function E(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(g,this),this.reset(!0)}function L(t){if(t){var r=t[o];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,a=function r(){for(;++n<t.length;)if(e.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=void 0,r.done=!0,r};return a.next=a}}return{next:j}}function j(){return{value:void 0,done:!0}}return h.prototype=y.constructor=l,l.constructor=h,h.displayName=c(l,i,"GeneratorFunction"),t.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===h||"GeneratorFunction"===(r.displayName||r.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,l):(t.__proto__=l,c(t,i,"GeneratorFunction")),t.prototype=Object.create(y),t},t.awrap=function(t){return{__await:t}},x(w.prototype),w.prototype[a]=function(){return this},t.AsyncIterator=w,t.async=function(r,e,n,o,a){void 0===a&&(a=Promise);var i=new w(u(r,e,n,o),a);return t.isGeneratorFunction(e)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},x(y),c(y,i,"Generator"),y[o]=function(){return this},y.toString=function(){return"[object Generator]"},t.keys=function(t){var r=[];for(var e in t)r.push(e);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=L,E.prototype={constructor:E,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!t)for(var r in this)"t"===r.charAt(0)&&e.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(e,n){return i.type="throw",i.arg=t,r.next=e,n&&(r.method="next",r.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var c=e.call(a,"catchLoc"),u=e.call(a,"finallyLoc");if(c&&u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,r){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&e.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=r&&r<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=r,a?(this.method="next",this.next=a.finallyLoc,p):this.complete(i)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),p},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),k(e),p}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;k(e)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,e){return this.delegate={iterator:L(t),resultName:r,nextLoc:e},"next"===this.method&&(this.arg=void 0),p}},t}(t.exports);try{regeneratorRuntime=r}catch(t){Function("r","regeneratorRuntime = r")(r)}}(a={exports:{}}),a.exports),c=function(){var t=o(i.mark((function t(r,n){var o;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=new e.Decimal(r),t.next=3,k(n);case 3:return t.abrupt("return",o.mul(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),u=function(){var t=o(i.mark((function t(r){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=new e.Decimal(r),t.next=3,w();case 3:return t.abrupt("return",n.div(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),s=function(t){return new e.Decimal(t).mul(1e8).toNumber()},p=function(){var t=o(i.mark((function t(r){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=new e.Decimal(r),t.next=3,b();case 3:return t.abrupt("return",n.div(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),f=function(){var t=o(i.mark((function t(r,n){var o;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=new e.Decimal(r),t.next=3,x(n);case 3:return t.abrupt("return",o.times(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),h=function(){var t=o(i.mark((function t(r){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=new e.Decimal(r),t.next=3,w();case 3:return t.abrupt("return",n.mul(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),l=function(){var t=o(i.mark((function t(r){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=new e.Decimal(r),t.next=3,b();case 3:return t.abrupt("return",n.mul(t.sent).toNumber());case 5:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),v=function(t){return new e.Decimal(t).div(1e8).toNumber()},d=function(){var t=o(i.mark((function t(r,e){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=v(r),t.next=3,c(n,e);case 3:return t.abrupt("return",t.sent);case 5:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),m=function(){var t=o(i.mark((function t(r,n){var o,a;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return o=new e.Decimal(r),t.next=3,k(n);case 3:return a=new e.Decimal(t.sent),t.abrupt("return",o.div(a).toNumber());case 6:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),y=function(){var t=o(i.mark((function t(r,e){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,m(r,e);case 2:return t.abrupt("return",s(t.sent));case 4:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),x=function(){var t=o(i.mark((function t(r){var e;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,fetch("https://min-api.cryptocompare.com/data/price?fsym="+r+"&tsyms=BTC");case 2:if(200===(e=t.sent).status){t.next=8;break}return t.next=6,e.json();case 6:throw Error(t.sent);case 8:return t.next=10,e.json();case 10:return t.abrupt("return",t.sent.BTC);case 12:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),w=function(){var t=o(i.mark((function t(){var r;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,fetch("https://xchain.io/api/asset/PEPECASH");case 2:if(200===(r=t.sent).status){t.next=8;break}return t.next=6,r.json();case 6:throw Error(t.sent);case 8:return t.next=10,r.json();case 10:return t.abrupt("return",t.sent.estimated_value.btc);case 12:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}(),b=function(){var t=o(i.mark((function t(){var r;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,fetch("https://xchain.io/api/asset/XCP");case 2:if(200===(r=t.sent).status){t.next=8;break}return t.next=6,r.json();case 6:throw Error(t.sent);case 8:return t.next=10,r.json();case 10:return t.abrupt("return",t.sent.estimated_value.btc);case 12:case"end":return t.stop()}}),t)})));return function(){return t.apply(this,arguments)}}(),g={},k=function(){var t=o(i.mark((function t(e){var n,o,a,c,u,s;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n=Date.now(),!((o=g[e])&&n-o.timestamp<6e4)){t.next=4;break}return t.abrupt("return",o.rate);case 4:return t.prev=4,t.next=7,fetch("https://api.coinbase.com/v2/prices/BTC-"+e+"/spot");case 7:if(200!==(a=t.sent).status){t.next=15;break}return t.next=11,a.json();case 11:return c=r(t.sent.data.amount,{separator:"",symbol:""}).format(),g[e]={rate:c,timestamp:n},t.abrupt("return",c);case 15:t.next=20;break;case 17:t.prev=17,t.t0=t.catch(4),console.warn("Coinbase API failed, falling back to Coindesk:",t.t0);case 20:return t.prev=20,t.next=23,fetch("https://api.coindesk.com/v1/bpi/currentprice/"+e.toLowerCase()+".json");case 23:if(200!==(u=t.sent).status){t.next=31;break}return t.next=27,u.json();case 27:return s=r(t.sent.bpi[e].rate,{separator:"",symbol:""}).format(),g[e]={rate:s,timestamp:n},t.abrupt("return",s);case 31:return t.next=33,u.json();case 33:throw Error(t.sent);case 37:throw t.prev=37,t.t1=t.catch(20),new Error("Both Coinbase and Coindesk APIs failed: "+t.t1);case 40:case"end":return t.stop()}}),t,null,[[4,17],[20,37]])})));return function(r){return t.apply(this,arguments)}}();exports.bitcoinToFiat=c,exports.bitcoinToPepecash=u,exports.bitcoinToSatoshis=s,exports.bitcoinToXcp=p,exports.cryptoToBitcoin=f,exports.fiatToBitcoin=m,exports.fiatToSatoshis=y,exports.getCryptoBtcRate=x,exports.getFiatBtcRate=k,exports.getPepecashBtcRate=w,exports.getXcpBtcRate=b,exports.pepecashToBitcoin=h,exports.satoshisToBitcoin=v,exports.satoshisToFiat=d,exports.xcpToBitcoin=l; | ||
//# sourceMappingURL=bitcoin-conversion.cjs.production.min.js.map |
@@ -1190,7 +1190,8 @@ import currencyJs from 'currency.js'; | ||
var rateCache = {}; | ||
var CACHE_TTL_MS = 60000; // 1 minute cache | ||
var CACHE_TTL_MS = process.env.NODE_ENV === 'test' ? 0 : 60000; // 1 minute cache | ||
var getFiatBtcRate = /*#__PURE__*/function () { | ||
var _ref13 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee13(currency) { | ||
var now, cachedData, response, json, data, rate; | ||
var now, cachedData, response, data, rate, _response, _data, _rate, json; | ||
return runtime_1.wrap(function _callee13$(_context13) { | ||
@@ -1211,27 +1212,20 @@ while (1) { | ||
case 4: | ||
_context13.next = 6; | ||
return fetch("https://api.coindesk.com/v1/bpi/currentprice/" + currency.toLowerCase() + ".json"); | ||
_context13.prev = 4; | ||
_context13.next = 7; | ||
return fetch("https://api.coinbase.com/v2/prices/BTC-" + currency + "/spot"); | ||
case 6: | ||
case 7: | ||
response = _context13.sent; | ||
if (!(response.status !== 200)) { | ||
_context13.next = 12; | ||
if (!(response.status === 200)) { | ||
_context13.next = 15; | ||
break; | ||
} | ||
_context13.next = 10; | ||
_context13.next = 11; | ||
return response.json(); | ||
case 10: | ||
json = _context13.sent; | ||
throw Error(json); | ||
case 12: | ||
_context13.next = 14; | ||
return response.json(); | ||
case 14: | ||
case 11: | ||
data = _context13.sent; | ||
rate = currencyJs(data.bpi[currency].rate, { | ||
rate = currencyJs(data.data.amount, { | ||
separator: '', | ||
@@ -1247,3 +1241,54 @@ symbol: '' | ||
case 18: | ||
case 15: | ||
_context13.next = 20; | ||
break; | ||
case 17: | ||
_context13.prev = 17; | ||
_context13.t0 = _context13["catch"](4); | ||
console.warn('Coinbase API failed, falling back to Coindesk:', _context13.t0); | ||
case 20: | ||
_context13.prev = 20; | ||
_context13.next = 23; | ||
return fetch("https://api.coindesk.com/v1/bpi/currentprice/" + currency.toLowerCase() + ".json"); | ||
case 23: | ||
_response = _context13.sent; | ||
if (!(_response.status === 200)) { | ||
_context13.next = 31; | ||
break; | ||
} | ||
_context13.next = 27; | ||
return _response.json(); | ||
case 27: | ||
_data = _context13.sent; | ||
_rate = currencyJs(_data.bpi[currency].rate, { | ||
separator: '', | ||
symbol: '' | ||
}).format(); // Cache the new rate | ||
rateCache[currency] = { | ||
rate: _rate, | ||
timestamp: now | ||
}; | ||
return _context13.abrupt("return", _rate); | ||
case 31: | ||
_context13.next = 33; | ||
return _response.json(); | ||
case 33: | ||
json = _context13.sent; | ||
throw Error(json); | ||
case 37: | ||
_context13.prev = 37; | ||
_context13.t1 = _context13["catch"](20); | ||
throw new Error("Both Coinbase and Coindesk APIs failed: " + _context13.t1); | ||
case 40: | ||
case "end": | ||
@@ -1253,3 +1298,3 @@ return _context13.stop(); | ||
} | ||
}, _callee13); | ||
}, _callee13, null, [[4, 17], [20, 37]]); | ||
})); | ||
@@ -1256,0 +1301,0 @@ |
{ | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -133,3 +133,3 @@ import currencyJs from 'currency.js'; | ||
>; | ||
const CACHE_TTL_MS = 60000; // 1 minute cache | ||
const CACHE_TTL_MS = process.env.NODE_ENV === 'test' ? 0 : 60000; // 1 minute cache | ||
@@ -145,19 +145,35 @@ export const getFiatBtcRate = async (currency: SupportedCurrencies): Promise<string> => { | ||
const response = await fetch( | ||
`https://api.coindesk.com/v1/bpi/currentprice/${currency.toLowerCase()}.json` | ||
); | ||
if (response.status !== 200) { | ||
// Try Coinbase API first | ||
try { | ||
const response = await fetch(`https://api.coinbase.com/v2/prices/BTC-${currency}/spot`); | ||
if (response.status === 200) { | ||
const data = await response.json(); | ||
const rate = currencyJs(data.data.amount, { separator: '', symbol: '' }).format(); | ||
// Cache the new rate | ||
rateCache[currency] = { rate, timestamp: now }; | ||
return rate; | ||
} | ||
} catch (error) { | ||
console.warn('Coinbase API failed, falling back to Coindesk:', error); | ||
} | ||
// Fallback to Coindesk API | ||
try { | ||
const response = await fetch( | ||
`https://api.coindesk.com/v1/bpi/currentprice/${currency.toLowerCase()}.json` | ||
); | ||
if (response.status === 200) { | ||
const data = await response.json(); | ||
const rate = currencyJs(data.bpi[currency].rate, { separator: '', symbol: '' }).format(); | ||
// Cache the new rate | ||
rateCache[currency] = { rate, timestamp: now }; | ||
return rate; | ||
} | ||
const json = await response.json(); | ||
throw Error(json); | ||
} catch (error) { | ||
throw new Error(`Both Coinbase and Coindesk APIs failed: ${error}`); | ||
} | ||
const data = await response.json(); | ||
const rate = currencyJs(data.bpi[currency].rate, { | ||
separator: '', | ||
symbol: '', | ||
}).format(); | ||
// Cache the new rate | ||
rateCache[currency] = { rate, timestamp: now }; | ||
return rate; | ||
}; | ||
@@ -164,0 +180,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
296959
2633
2
18