UNPKG

bitcoin-conversion

Version:

[![npm](https://img.shields.io/npm/v/bitcoin-conversion?style=plastic)](https://www.npmjs.com/package/bitcoin-conversion)

3 lines (2 loc) 18.4 kB
"use strict";function t(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 r(r){return function(){var e=this,n=arguments;return new Promise((function(o,a){var i=r.apply(e,n);function u(r){t(i,o,a,u,c,"next",r)}function c(r){t(i,o,a,u,c,"throw",r)}u(void 0)}))}}function e(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function n(t,r){var n;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(n=function(t,r){if(t){if("string"==typeof t)return e(t,void 0);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(t,void 0):void 0}}(t))||r&&t&&"number"==typeof t.length){n&&(t=n);var o=0;return function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(n=t[Symbol.iterator]()).next.bind(n)}Object.defineProperty(exports,"__esModule",{value:!0});var o,a=(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 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 u=x(i,e);if(u){if(u===p)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===p)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 p={};function f(){}function h(){}function l(){}var v={};v[o]=function(){return this};var d=Object.getPrototypeOf,m=d&&d(d(T([])));m&&m!==r&&e.call(m,o)&&(v=m);var y=l.prototype=f.prototype=Object.create(v);function w(t){["next","throw","return"].forEach((function(r){u(t,r,(function(t){return this._invoke(r,t)}))}))}function b(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 p=c.arg,f=p.value;return f&&"object"==typeof f&&e.call(f,"__await")?r.resolve(f.__await).then((function(t){n("next",t,i,u)}),(function(t){n("throw",t,i,u)})):r.resolve(f).then((function(t){p.value=t,i(p)}),(function(t){return n("throw",t,i,u)}))}u(c.arg)}(o,a,n,i)}))}return n=n?n.then(i,i):i()}}function x(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,x(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 T(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:L}}function L(){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(b.prototype),b.prototype[a]=function(){return this},t.AsyncIterator=b,t.async=function(r,e,n,o,a){void 0===a&&(a=Promise);var i=new b(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=T,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 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,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:T(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)}}(o={exports:{}}),o.exports),i=function(){var t=r(a.mark((function t(r,e){var n,o;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=O(r,"amountInBtc"),t.t0=O,t.next=4,k(e);case 4:return t.t1=t.sent,t.t2="BTC-"+e+" rate",o=(0,t.t0)(t.t1,t.t2),t.abrupt("return",n*o);case 8:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),u=function(){var t=r(a.mark((function t(r){var e,n;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=O(r,"amountInBtc"),t.t0=O,t.next=4,w();case 4:return t.t1=t.sent,n=(0,t.t0)(t.t1,"PEPECASH/BTC rate"),t.abrupt("return",I(e,n,"bitcoinToPepecash"));case 7:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),c=function(t){return 1e8*O(t,"amountInBtc")},s=function(){var t=r(a.mark((function t(r){var e,n;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=O(r,"amountInBtc"),t.t0=O,t.next=4,b();case 4:return t.t1=t.sent,n=(0,t.t0)(t.t1,"XCP/BTC rate"),t.abrupt("return",I(e,n,"bitcoinToXcp"));case 7:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),p=function(){var t=r(a.mark((function t(r,e){var n,o;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=O(r,"amountInCrypto"),t.t0=O,t.next=4,y(e);case 4:return t.t1=t.sent,t.t2=e+"/BTC rate",o=(0,t.t0)(t.t1,t.t2),t.abrupt("return",n*o);case 8:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),f=function(){var t=r(a.mark((function t(r){var e,n;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=O(r,"amountInPepecash"),t.t0=O,t.next=4,w();case 4:return t.t1=t.sent,n=(0,t.t0)(t.t1,"PEPECASH/BTC rate"),t.abrupt("return",e*n);case 7:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),h=function(){var t=r(a.mark((function t(r){var e,n;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=O(r,"amountInXcp"),t.t0=O,t.next=4,b();case 4:return t.t1=t.sent,n=(0,t.t0)(t.t1,"XCP/BTC rate"),t.abrupt("return",e*n);case 7:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),l=function(t){var r=O(t,"amountInSatoshis");return I(r,1e8,"satoshisToBitcoin")},v=function(){var t=r(a.mark((function t(r,e){var n;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=l(r),t.next=3,i(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)}}(),d=function(){var t=r(a.mark((function t(r,e){var n,o;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=O(r,"amountInCurrency"),t.t0=O,t.next=4,k(e);case 4:return t.t1=t.sent,t.t2="BTC-"+e+" rate",o=(0,t.t0)(t.t1,t.t2),t.abrupt("return",I(n,o,"fiatToBitcoin"));case 8:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),m=function(){var t=r(a.mark((function t(r,e){return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,d(r,e);case 2:return t.abrupt("return",c(t.sent));case 4:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}(),y=function(){var t=r(a.mark((function t(r){var e;return a.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=r(a.mark((function t(){var r;return a.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=r(a.mark((function t(){var r;return a.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)}}(),x={},g={},k=function(){var t=r(a.mark((function t(r){var e,o,i,u,c,s,p,f,h;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e=Date.now(),!((o=x[r])&&e-o.timestamp<6e4)){t.next=4;break}return t.abrupt("return",o.rate);case 4:i=[],u=n(H);case 6:if((c=u()).done){t.next=28;break}if(!((p=g[(s=c.value).name])&&p>Date.now())){t.next=12;break}return i.push({provider:s.name,reason:"cooldown until "+new Date(p).toISOString()}),t.abrupt("continue",26);case 12:return t.prev=12,t.next=15,s.getRate(r);case 15:return f=_(t.sent),x[r]={rate:f,timestamp:Date.now()},t.abrupt("return",f);case 21:t.prev=21,t.t0=t.catch(12),h=X(t.t0,s.name),i.push({provider:s.name,reason:h.message}),h.cooldownMs&&(g[s.name]=Date.now()+h.cooldownMs);case 26:t.next=6;break;case 28:if(!(o&&Date.now()-o.timestamp<=6e5)){t.next=30;break}return t.abrupt("return",o.rate);case 30:throw new Error("Failed to fetch BTC-"+r+" rate from all providers. "+A(i));case 31:case"end":return t.stop()}}),t,null,[[12,21]])})));return function(r){return t.apply(this,arguments)}}(),E=function(){var t=r(a.mark((function t(r){var e,n,o,i;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,B("https://api.coinbase.com/v2/prices/BTC-"+r+"/spot");case 2:return o=(e=t.sent).data,S("coinbase",(n=e.response).status,n.headers.get("retry-after"),o),i=P("coinbase",o,["data","amount"]),t.abrupt("return",N("coinbase",i,r));case 8:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),T=function(){var t=r(a.mark((function t(r){var e,n,o,i;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,B("https://bitpay.com/rates/BTC/"+r);case 2:return o=(e=t.sent).data,S("bitpay",(n=e.response).status,n.headers.get("retry-after"),o),i=P("bitpay",o,["data","rate"]),t.abrupt("return",N("bitpay",i,r));case 8:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),L=function(){var t=r(a.mark((function t(r){var e,n,o,i,u;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,B("https://blockchain.info/ticker");case 2:return o=(e=t.sent).data,S("blockchain",(n=e.response).status,n.headers.get("retry-after"),o),i=P("blockchain",o,[r]),u=P("blockchain",i,["last"]),t.abrupt("return",N("blockchain",u,r));case 9:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),j=function(){var t=r(a.mark((function t(r){var e,n,o,i,u,c;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=r.toLowerCase(),t.next=3,B("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies="+e);case 3:return i=(n=t.sent).data,S("coingecko",(o=n.response).status,o.headers.get("retry-after"),i),u=P("coingecko",i,["bitcoin"]),c=P("coingecko",u,[e]),t.abrupt("return",N("coingecko",c,r));case 10:case"end":return t.stop()}}),t)})));return function(r){return t.apply(this,arguments)}}(),B=function(){var t=r(a.mark((function t(r){var e,n,o;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=new AbortController,n=setTimeout((function(){return e.abort()}),3e3),t.prev=2,t.next=5,fetch(r,{signal:e.signal});case 5:return o=t.sent,t.next=8,C(o);case 8:return t.abrupt("return",{response:o,data:t.sent});case 10:return t.prev=10,clearTimeout(n),t.finish(10);case 13:case"end":return t.stop()}}),t,null,[[2,,10,13]])})));return function(r){return t.apply(this,arguments)}}(),C=function(){var t=r(a.mark((function t(r){return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,r.json();case 3:return t.abrupt("return",t.sent);case 6:return t.prev=6,t.t0=t.catch(0),t.abrupt("return",null);case 9:case"end":return t.stop()}}),t,null,[[0,6]])})));return function(r){return t.apply(this,arguments)}}(),S=function(t,r,e,n){var o;if(200!==r){var a=null===n?"no response body":R(n),i=429===r?null!=(o=F(e))?o:6e4:void 0;throw G(t,"HTTP "+r+": "+a,i)}},P=function(t,r,e){for(var o,a=r,i=n(e);!(o=i()).done;){var u=o.value;if(!D(a)||!(u in a))throw G(t,'missing field "'+e.join(".")+'"');a=a[u]}return a},N=function(t,r,e){if("number"==typeof r||"string"==typeof r)return r;throw G(t,"invalid rate type for "+e)},_=function(t){var r=O(t,"provider rate",{allowCommas:!0});return String(r)},O=function(t,r,e){if(void 0===e&&(e={}),"number"==typeof t){if(!Number.isFinite(t))throw new Error("invalid numeric input for "+r+": "+t);return t}var n=t.trim();if(""===n)throw new Error("invalid numeric input for "+r+": empty string");var o=e.allowCommas?n.replace(/,/g,""):n,a=Number(o);if(!Number.isFinite(a))throw new Error("invalid numeric input for "+r+": "+t);return a},I=function(t,r,e){if(!Number.isFinite(r)||0===r)throw new Error("division by zero for "+e);return t/r},F=function(t){if(t){var r=Number(t);if(!Number.isNaN(r)&&Number.isFinite(r)&&r>0)return 1e3*r;var e=Date.parse(t);if(!Number.isNaN(e)){var n=e-Date.now();return n>0?n:void 0}}},A=function(t){return 0===t.length?"No providers were attempted.":t.map((function(t){return t.provider+": "+t.reason})).join("; ")},R=function(t){try{return JSON.stringify(t)}catch(r){return String(t)}},D=function(t){return"object"==typeof t&&null!==t},G=function(t,r,e){var n=new Error("["+t+"] "+r);return e&&(n.cooldownMs=e),n},X=function(t,r){return M(t)?G(r,"timeout after 3000ms"):"object"==typeof t&&null!==t&&"message"in t?t:G(r,String(t))},M=function(t){return"object"==typeof t&&null!==t&&"name"in t&&"AbortError"===t.name},H=[{name:"coinbase",getRate:E},{name:"bitpay",getRate:T},{name:"blockchain",getRate:L},{name:"coingecko",getRate:j}];exports.bitcoinToFiat=i,exports.bitcoinToPepecash=u,exports.bitcoinToSatoshis=c,exports.bitcoinToXcp=s,exports.cryptoToBitcoin=p,exports.fiatToBitcoin=d,exports.fiatToSatoshis=m,exports.getCryptoBtcRate=y,exports.getFiatBtcRate=k,exports.getPepecashBtcRate=w,exports.getXcpBtcRate=b,exports.pepecashToBitcoin=f,exports.satoshisToBitcoin=l,exports.satoshisToFiat=v,exports.xcpToBitcoin=h; //# sourceMappingURL=bitcoin-conversion.cjs.production.min.js.map