-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitsharesjs-ws.min.js
1 lines (1 loc) · 12.5 KB
/
bitsharesjs-ws.min.js
1
(function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"==typeof window?"undefined"==typeof global?"undefined"==typeof self?this:self:global:window,b.bitshares_ws=a()}})(function(){return function(){function b(d,e,g){function a(j,i){if(!e[j]){if(!d[j]){var f="function"==typeof require&&require;if(!i&&f)return f(j,!0);if(h)return h(j,!0);var c=new Error("Cannot find module '"+j+"'");throw c.code="MODULE_NOT_FOUND",c}var k=e[j]={exports:{}};d[j][0].call(k.exports,function(b){var c=d[j][1][b];return a(c||b)},k,k.exports,b,d,e,g)}return e[j].exports}for(var h="function"==typeof require&&require,c=0;c<g.length;c++)a(g[c]);return a}return b}()({1:[function(a,b,c){"use strict";function d(b){return b&&b.__esModule?b:{default:b}}Object.defineProperty(c,"__esModule",{value:!0}),c.orders=c.crypto=c.history=c.network=c.db=c.close=c.chainId=c.instance=c.reset=c.setAutoReconnect=c.setRpcConnectionStatusCallback=void 0;var e=d(a("./ChainWebSocket")),f=d(a("./GrapheneApi")),g=d(a("./ChainConfig")),h=!1,i=null,j=null;const k=b=>{j=b,i&&i.setRpcConnectionStatusCallback(b)};c.setRpcConnectionStatusCallback=k;c.setAutoReconnect=b=>{h=b};c.reset=(f="ws://localhost:8090",a,b=4e3,c,d)=>m().then(()=>(i=t(),i.setRpcConnectionStatusCallback(j),i&&a&&i.connect(f,b,c,d),i));const l=(f="ws://localhost:8090",a,b=4e3,c,d)=>(i||(i=t(),i.setRpcConnectionStatusCallback(j)),i&&a&&i.connect(f,b,c),d&&(i.closeCb=d),i);c.instance=l;c.chainId=()=>l().chain_id;const m=async()=>{i&&(await i.close(),i=null)};c.close=m;const n=d=>new Proxy([],{get:(a,e)=>(...a)=>i[d].exec(e,[...a])}),o=n("_db");c.db=o;const p=n("_net");c.network=p;const q=n("_hist");c.history=q;const r=n("_crypt");c.crypto=r;const s=n("_orders");c.orders=s;const t=()=>({connect:(d,a,j={enableCrypto:!1,enableOrders:!1})=>{if(i.url=d,"undefined"!=typeof window&&window.location&&"https:"===window.location.protocol&&0>d.indexOf("wss://"))throw new Error("Secure domains require wss connection");i.ws_rpc&&(i.ws_rpc.statusCb=null,i.ws_rpc.keepAliveCb=null,i.ws_rpc.on_close=null,i.ws_rpc.on_reconnect=null),i.ws_rpc=new e.default(d,i.statusCb,a,h,b=>{i._db&&!b&&i._db.exec("get_objects",[["2.1.0"]]).catch(()=>{})}),i.init_promise=i.ws_rpc.login("","").then(()=>{i._db=new f.default(i.ws_rpc,"database"),i._net=new f.default(i.ws_rpc,"network_broadcast"),i._hist=new f.default(i.ws_rpc,"history"),j.enableOrders&&(i._orders=new f.default(i.ws_rpc,"orders")),j.enableCrypto&&(i._crypt=new f.default(i.ws_rpc,"crypto"));var c=i._db.init().then(()=>i._db.exec("get_chain_id",[]).then(b=>(i.chain_id=b,g.default.setChainId(b))));i.ws_rpc.on_reconnect=()=>{i.ws_rpc&&i.ws_rpc.login("","").then(()=>{i._db.init().then(()=>{i.statusCb&&i.statusCb("reconnect")}),i._net.init(),i._hist.init(),j.enableOrders&&i._orders.init(),j.enableCrypto&&i._crypt.init()})},i.ws_rpc.on_close=()=>{i.close().then(()=>{i.closeCb&&i.closeCb()})};let a=[c,i._net.init(),i._hist.init()];return j.enableOrders&&a.push(i._orders.init()),j.enableCrypto&&a.push(i._crypt.init()),Promise.all(a)}).catch(a=>(console.error(d,"Failed to initialize with error",a&&a.message),i.close().then(()=>{throw a})))},close:async()=>{i.ws_rpc&&1===i.ws_rpc.ws.readyState&&(await i.ws_rpc.close()),i.ws_rpc=null},db_api:()=>i._db,network_api:()=>i._net,history_api:()=>i._hist,crypto_api:()=>i._crypt,orders_api:()=>i._orders,setRpcConnectionStatusCallback:b=>i.statusCb=b})},{"./ChainConfig":2,"./ChainWebSocket":3,"./GrapheneApi":5}],2:[function(a,b,c){"use strict";Object.defineProperty(c,"__esModule",{value:!0}),c.default=void 0;var d={core_asset:"CORE",address_prefix:"GPH",expire_in_secs:15,expire_in_secs_proposal:86400,review_in_secs_committee:86400,networks:{BitShares:{core_asset:"BTS",address_prefix:"BTS",chain_id:"4018d7844c78f6a6c41c6a552b898022310fc5dec06da467ee7905a8dad512c8"},Muse:{core_asset:"MUSE",address_prefix:"MUSE",chain_id:"45ad2d3f9ef92a49b55c2227eb06123f613bb35dd08bd876f2aea21925a67a67"},Test:{core_asset:"TEST",address_prefix:"TEST",chain_id:"39f5e2ede1f8bc1a3a54a7914414e3779e33193f1f5693510e73cb7a87617447"},Obelisk:{core_asset:"GOV",address_prefix:"FEW",chain_id:"1cfde7c388b9e8ac06462d68aadbd966b58f88797637d9af805b4560b0e9661e"}},setChainId:e=>{let a=Object.entries(d.networks).find(([a,b])=>{if(b.chain_id===e)return d.network_name=a,b.address_prefix&&(d.address_prefix=b.address_prefix),!0});return a?{network_name:a[0],network:a[1]}:void console.log("Unknown chain id (this may be a testnet)",e)},reset:()=>{d.core_asset="CORE",d.address_prefix="GPH",d.expire_in_secs=15,d.expire_in_secs_proposal=86400,console.log("Chain config reset")},setPrefix:(b="GPH")=>d.address_prefix=b};c.default=d},{}],3:[function(a,b,c){"use strict";function f(d,a,b){return a in d?Object.defineProperty(d,a,{value:b,enumerable:!0,configurable:!0,writable:!0}):d[a]=b,d}var g=function(b){return b&&b.__esModule?b:{default:b}}(a("isomorphic-ws"));Object.defineProperty(c,"__esModule",{value:!0}),c.default=void 0;const h=!1,i=5,j=10;c.default=class{constructor(k,a,b=5e3,c=!0,d=null){f(this,"connect",(c,a)=>new Promise((b,e)=>{this.current_reject=e,this.current_resolve=b;try{this.ws=new g.default(c)}catch(a){this.ws={readyState:3,close:()=>{}},e(new Error("Invalid url",c," closed"))}this.ws.onopen=this.onOpen,this.ws.onerror=this.onError,this.ws.onmessage=this.onMessage,this.ws.onclose=this.onClose,this.connectionTimeout=setTimeout(()=>{this.current_reject&&(this.current_reject=null,this.close(),e(new Error("Connection attempt timed out after "+a/1e3+"s")))},a)})),f(this,"onOpen",()=>{clearTimeout(this.connectionTimeout),this.statusCb&&this.statusCb("open"),this.on_reconnect&&this.on_reconnect(),this.keepalive_timer=setInterval(()=>(this.recv_life--,0==this.recv_life?(console.error(this.url+" connection is dead, terminating ws"),void this.close()):void(this.send_life--,0==this.send_life&&(this.keepAliveCb&&this.keepAliveCb(this.closed),this.send_life=i))),5e3),this.current_reject=null,this.current_resolve()}),f(this,"onError",b=>{this.keepalive_timer&&(clearInterval(this.keepalive_timer),this.keepalive_timer=void 0),clearTimeout(this.connectionTimeout),this.statusCb&&this.statusCb("error"),this.current_reject&&this.current_reject(b)}),f(this,"onMessage",b=>{this.recv_life=j,this.listener(JSON.parse(b.data))}),f(this,"onClose",()=>{this.closed=!0,this.keepalive_timer&&(clearInterval(this.keepalive_timer),this.keepalive_timer=void 0);for(var b=this.responseCbId+1;b<=this.cbId;b+=1)this.cbs[b].reject(new Error("connection closed"));this.statusCb&&this.statusCb("closed"),this._closeCb&&this._closeCb(),this.on_close&&this.on_close()}),f(this,"call",d=>{if(1!==this.ws.readyState)return Promise.reject(new Error("websocket state error:"+this.ws.readyState));let a=d[1];if(h&&console.log("[ChainWebSocket] >---- call -----> \"id\":"+(this.cbId+1),JSON.stringify(d)),this.cbId+=1,["set_subscribe_callback","subscribe_to_market","broadcast_transaction_with_callback","set_pending_transaction_callback","set_block_applied_callback"].includes(a)&&(this.subs[this.cbId]={callback:d[2][0]},d[2][0]=this.cbId),["unsubscribe_from_market","unsubscribe_from_accounts"].includes(a)){if("function"!=typeof d[2][0])throw new Error("First parameter of unsub must be the original callback");let c=d[2].splice(0,1)[0];for(let b in this.subs)if(this.subs[b].callback===c){this.unsub[this.cbId]=b;break}}var e={method:"call",params:d};return e.id=this.cbId,this.send_life=i,new Promise((c,a)=>{this.cbs[this.cbId]={time:new Date,resolve:c,reject:a},this.ws.send(JSON.stringify(e))})}),f(this,"listener",d=>{h&&console.log("[ChainWebSocket] <---- reply ----<",JSON.stringify(d));let a=!1,e=null;"notice"===d.method&&(a=!0,d.id=d.params[0]),a?e=this.subs[d.id].callback:(e=this.cbs[d.id],this.responseCbId=d.id),e&&!a?(d.error?e.reject(d.error):e.resolve(d.result),delete this.cbs[d.id],this.unsub[d.id]&&(delete this.subs[this.unsub[d.id]],delete this.unsub[d.id])):e&&a?e(d.params[1]):console.log("Warning: unknown websocket response: ",d)}),f(this,"login",(c,a)=>this.connect_promise.then(()=>this.call([1,"login",[c,a]]))),f(this,"close",()=>new Promise(b=>(clearInterval(this.keepalive_timer),this.keepalive_timer=void 0,this._closeCb=()=>{b(),this._closeCb=null},this.ws?void(this.ws.terminate?this.ws.terminate():this.ws.close(),3===this.ws.readyState&&b()):(console.log("Websocket already cleared",this),b())))),this.url=k,this.statusCb=a,this.current_reject=null,this.on_reconnect=null,this.closed=!1,this.send_life=i,this.recv_life=j,this.keepAliveCb=d,this.cbId=0,this.responseCbId=0,this.cbs={},this.subs={},this.unsub={},this.connect_promise=this.connect(k,b)}}},{"isomorphic-ws":7}],4:[function(a,b,c){"use strict";function h(d,a,b){return a in d?Object.defineProperty(d,a,{value:b,enumerable:!0,configurable:!0,writable:!0}):d[a]=b,d}Object.defineProperty(c,"__esModule",{value:!0}),c.default=void 0;var i=function(e){if(e&&e.__esModule)return e;var a={};if(null!=e)for(var b in e)if(Object.prototype.hasOwnProperty.call(e,b)){var c=Object.defineProperty&&Object.getOwnPropertyDescriptor?Object.getOwnPropertyDescriptor(e,b):{};c.get||c.set?Object.defineProperty(a,b,c):a[b]=e[b]}return a.default=e,a}(a("./ApiInstances")),j=function(b){return b&&b.__esModule?b:{default:b}}(a("./ChainWebSocket"));c.default=class{constructor({url:a,urls:b,autoFallback:c,closeCb:d,optionalApis:e,urlChangeCallback:f}){h(this,"setCloseCb",b=>{this.closeCb=b}),h(this,"logFailure",(e,a,b)=>{let c=b&&b.message?b.message:"";console.error(e,"Failed to connect to "+a+(c?" Error: "+JSON.stringify(c):""))}),h(this,"_onClose",()=>{this.isConnected=!1,this.closeCb&&(this.closeCb(),this.setCloseCb(null)),this.autoFallback&&this.connectWithFallback()}),h(this,"connect",async(d=!0,a=this.url)=>{try{let b=await i.instance(a,d,void 0,this.optionalApis,this._onClose).init_promise;return this.url=a,this.isConnected=!0,b}catch(b){throw await i.close(),b}}),h(this,"connectWithFallback",async(f=!0,a=this.url,g=0,c=null,d=null)=>{if(g>this.urls.length)return d(new Error("Tried "+g+" connections, none of which worked: "+JSON.stringify(this.urls.concat(this.url))));try{return await this.connect(f,a)}catch(a){return this.urlChangeCallback&&this.urlChangeCallback(this.urls[g]),this.connectWithFallback(f,this.urls[g],g+1,c,d)}}),h(this,"checkConnections",async(h="",a="",b,c)=>{let i={},d=this.urls.concat(this.url),e=d.map(async e=>{let b=new j.default(e,()=>{},void 0,!1);i[e]=new Date().getTime();try{await b.login(h,a);let c={[e]:new Date().getTime()-i[e]};return await b.close(),c}catch(c){return e===this.url?this.url=this.urls[0]:this.urls=this.urls.filter(a=>a!==e),void(await b.close())}});try{let c=await Promise.all(e),a=c.filter(a=>!!a).sort((b,c)=>Object.values(b)[0]-Object.values(c)[0]).reduce((d,b)=>{let c=Object.keys(b)[0];return d[c]=b[c],d},{});return console.log(`Checked ${c.length} connections, ${c.length-Object.keys(a).length} failed`),a}catch(d){return this.checkConnections(h,a,b,c)}}),this.url=a,this.urls=b.filter(b=>b!==a),this.autoFallback=c,this.closeCb=d,this.optionalApis=e||{},this.isConnected=!1,this.urlChangeCallback=f}static close(){return i.close()}}},{"./ApiInstances":1,"./ChainWebSocket":3}],5:[function(a,b,c){"use strict";Object.defineProperty(c,"__esModule",{value:!0}),c.default=void 0;c.default=class{constructor(c,a){this.ws_rpc=c,this.api_name=a}init(){var c=this;return this.ws_rpc.call([1,this.api_name,[]]).then(a=>(c.api_id=a,c))}exec(c,a){return this.ws_rpc.call([this.api_id,c,a]).catch(b=>{throw b})}}},{}],6:[function(a,b,c){"use strict";function d(b){return b&&b.__esModule?b:{default:b}}var e=function(e){if(e&&e.__esModule)return e;var a={};if(null!=e)for(var b in e)if(Object.prototype.hasOwnProperty.call(e,b)){var c=Object.defineProperty&&Object.getOwnPropertyDescriptor?Object.getOwnPropertyDescriptor(e,b):{};c.get||c.set?Object.defineProperty(a,b,c):a[b]=e[b]}return a.default=e,a}(a("./ApiInstances"));Object.defineProperty(c,"__esModule",{value:!0}),Object.defineProperty(c,"Manager",{enumerable:!0,get:function(){return f.default}}),Object.defineProperty(c,"ChainConfig",{enumerable:!0,get:function(){return g.default}}),c.Apis=void 0,c.Apis=e;var f=d(a("./ConnectionManager")),g=d(a("./ChainConfig"))},{"./ApiInstances":1,"./ChainConfig":2,"./ConnectionManager":4}],7:[function(a,b){(function(a){var c=null;"undefined"==typeof WebSocket?"undefined"==typeof MozWebSocket?"undefined"==typeof a?"undefined"==typeof window?"undefined"!=typeof self&&(c=self.WebSocket||self.MozWebSocket):c=window.WebSocket||window.MozWebSocket:c=a.WebSocket||a.MozWebSocket:c=MozWebSocket:c=WebSocket,b.exports=c}).call(this,"undefined"==typeof global?"undefined"==typeof self?"undefined"==typeof window?{}:window:self:global)},{}]},{},[6])(6)});