@galacticcouncil/sdk 0.0.1-beta.14 → 0.0.1-beta.16

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.
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var bt=Object.create;var We=Object.defineProperty;var yt=Object.getOwnPropertyDescriptor;var Bt=Object.getOwnPropertyNames;var Ot=Object.getPrototypeOf,Nt=Object.prototype.hasOwnProperty;var at=(g,e)=>()=>(e||g((e={exports:{}}).exports,e),e.exports),It=(g,e)=>{for(var o in e)We(g,o,{get:e[o],enumerable:!0})},lt=(g,e,o,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of Bt(e))!Nt.call(g,n)&&n!==o&&We(g,n,{get:()=>e[n],enumerable:!(s=yt(e,n))||s.enumerable});return g};var ct=(g,e,o)=>(o=g!=null?bt(Ot(g)):{},lt(e||!g||!g.__esModule?We(o,"default",{value:g,enumerable:!0}):o,g)),St=g=>lt(We({},"__esModule",{value:!0}),g);var mt=at((Mn,Ne)=>{var kt={},I,{TextEncoder:Et,TextDecoder:Ft}=require("util"),H=0,ze=null;function Ve(){return(ze===null||ze.buffer!==I.memory.buffer)&&(ze=new Uint8Array(I.memory.buffer)),ze}var Ye=new Et("utf-8"),Mt=typeof Ye.encodeInto=="function"?function(g,e){return Ye.encodeInto(g,e)}:function(g,e){let o=Ye.encode(g);return e.set(o),{read:g.length,written:o.length}};function z(g,e,o){if(o===void 0){let f=Ye.encode(g),w=e(f.length);return Ve().subarray(w,w+f.length).set(f),H=f.length,w}let s=g.length,n=e(s),l=Ve(),u=0;for(;u<s;u++){let f=g.charCodeAt(u);if(f>127)break;l[n+u]=f}if(u!==s){u!==0&&(g=g.slice(u)),n=o(n,s,s=u+g.length*3);let f=Ve().subarray(n+u,n+s);u+=Mt(g,f).written}return H=u,n}var Ke=null;function pe(){return(Ke===null||Ke.buffer!==I.memory.buffer)&&(Ke=new Int32Array(I.memory.buffer)),Ke}var dt=new Ft("utf-8",{ignoreBOM:!0,fatal:!0});dt.decode();function De(g,e){return dt.decode(Ve().subarray(g,g+e))}Ne.exports.calculate_pool_trade_fee=function(g,e,o){try{let l=I.__wbindgen_add_to_stack_pointer(-16),u=z(g,I.__wbindgen_malloc,I.__wbindgen_realloc),f=H;I.calculate_pool_trade_fee(l,u,f,e,o);var s=pe()[l/4+0],n=pe()[l/4+1];return De(s,n)}finally{I.__wbindgen_add_to_stack_pointer(16),I.__wbindgen_free(s,n)}};Ne.exports.get_spot_price=function(g,e,o,s,n){try{let f=I.__wbindgen_add_to_stack_pointer(-16),w=z(g,I.__wbindgen_malloc,I.__wbindgen_realloc),y=H,N=z(e,I.__wbindgen_malloc,I.__wbindgen_realloc),x=H,v=z(o,I.__wbindgen_malloc,I.__wbindgen_realloc),M=H,E=z(s,I.__wbindgen_malloc,I.__wbindgen_realloc),Y=H,X=z(n,I.__wbindgen_malloc,I.__wbindgen_realloc),$=H;I.get_spot_price(f,w,y,N,x,v,M,E,Y,X,$);var l=pe()[f/4+0],u=pe()[f/4+1];return De(l,u)}finally{I.__wbindgen_add_to_stack_pointer(16),I.__wbindgen_free(l,u)}};Ne.exports.calculate_out_given_in=function(g,e,o,s,n){try{let f=I.__wbindgen_add_to_stack_pointer(-16),w=z(g,I.__wbindgen_malloc,I.__wbindgen_realloc),y=H,N=z(e,I.__wbindgen_malloc,I.__wbindgen_realloc),x=H,v=z(o,I.__wbindgen_malloc,I.__wbindgen_realloc),M=H,E=z(s,I.__wbindgen_malloc,I.__wbindgen_realloc),Y=H,X=z(n,I.__wbindgen_malloc,I.__wbindgen_realloc),$=H;I.calculate_out_given_in(f,w,y,N,x,v,M,E,Y,X,$);var l=pe()[f/4+0],u=pe()[f/4+1];return De(l,u)}finally{I.__wbindgen_add_to_stack_pointer(16),I.__wbindgen_free(l,u)}};Ne.exports.calculate_in_given_out=function(g,e,o,s,n){try{let f=I.__wbindgen_add_to_stack_pointer(-16),w=z(g,I.__wbindgen_malloc,I.__wbindgen_realloc),y=H,N=z(e,I.__wbindgen_malloc,I.__wbindgen_realloc),x=H,v=z(o,I.__wbindgen_malloc,I.__wbindgen_realloc),M=H,E=z(s,I.__wbindgen_malloc,I.__wbindgen_realloc),Y=H,X=z(n,I.__wbindgen_malloc,I.__wbindgen_realloc),$=H;I.calculate_in_given_out(f,w,y,N,x,v,M,E,Y,X,$);var l=pe()[f/4+0],u=pe()[f/4+1];return De(l,u)}finally{I.__wbindgen_add_to_stack_pointer(16),I.__wbindgen_free(l,u)}};Ne.exports.calculate_linear_weights=function(g,e,o,s,n){try{let f=I.__wbindgen_add_to_stack_pointer(-16),w=z(g,I.__wbindgen_malloc,I.__wbindgen_realloc),y=H,N=z(e,I.__wbindgen_malloc,I.__wbindgen_realloc),x=H,v=z(o,I.__wbindgen_malloc,I.__wbindgen_realloc),M=H,E=z(s,I.__wbindgen_malloc,I.__wbindgen_realloc),Y=H,X=z(n,I.__wbindgen_malloc,I.__wbindgen_realloc),$=H;I.calculate_linear_weights(f,w,y,N,x,v,M,E,Y,X,$);var l=pe()[f/4+0],u=pe()[f/4+1];return De(l,u)}finally{I.__wbindgen_add_to_stack_pointer(16),I.__wbindgen_free(l,u)}};var Dt=require("path").join(__dirname,"hydra_dx_wasm_bg.wasm"),Lt=require("fs").readFileSync(Dt),Rt=new WebAssembly.Module(Lt),Gt=new WebAssembly.Instance(Rt,kt);I=Gt.exports;Ne.exports.__wasm=I});var ht=at((po,fe)=>{var Xt={},O,{TextEncoder:zt,TextDecoder:Kt}=require("util"),G=0,Je=null;function Ze(){return(Je===null||Je.buffer!==O.memory.buffer)&&(Je=new Uint8Array(O.memory.buffer)),Je}var je=new zt("utf-8"),Vt=typeof je.encodeInto=="function"?function(g,e){return je.encodeInto(g,e)}:function(g,e){let o=je.encode(g);return e.set(o),{read:g.length,written:o.length}};function C(g,e,o){if(o===void 0){let f=je.encode(g),w=e(f.length);return Ze().subarray(w,w+f.length).set(f),G=f.length,w}let s=g.length,n=e(s),l=Ze(),u=0;for(;u<s;u++){let f=g.charCodeAt(u);if(f>127)break;l[n+u]=f}if(u!==s){u!==0&&(g=g.slice(u)),n=o(n,s,s=u+g.length*3);let f=Ze().subarray(n+u,n+s);u+=Vt(g,f).written}return G=u,n}var Qe=null;function j(){return(Qe===null||Qe.buffer!==O.memory.buffer)&&(Qe=new Int32Array(O.memory.buffer)),Qe}var _t=new Kt("utf-8",{ignoreBOM:!0,fatal:!0});_t.decode();function be(g,e){return _t.decode(Ze().subarray(g,g+e))}fe.exports.calculate_pool_trade_fee=function(g,e,o){try{let l=O.__wbindgen_add_to_stack_pointer(-16),u=C(g,O.__wbindgen_malloc,O.__wbindgen_realloc),f=G;O.calculate_pool_trade_fee(l,u,f,e,o);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{O.__wbindgen_add_to_stack_pointer(16),O.__wbindgen_free(s,n)}};fe.exports.get_spot_price=function(g,e,o){try{let l=O.__wbindgen_add_to_stack_pointer(-16),u=C(g,O.__wbindgen_malloc,O.__wbindgen_realloc),f=G,w=C(e,O.__wbindgen_malloc,O.__wbindgen_realloc),y=G,N=C(o,O.__wbindgen_malloc,O.__wbindgen_realloc),x=G;O.get_spot_price(l,u,f,w,y,N,x);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{O.__wbindgen_add_to_stack_pointer(16),O.__wbindgen_free(s,n)}};fe.exports.calculate_out_given_in=function(g,e,o){try{let l=O.__wbindgen_add_to_stack_pointer(-16),u=C(g,O.__wbindgen_malloc,O.__wbindgen_realloc),f=G,w=C(e,O.__wbindgen_malloc,O.__wbindgen_realloc),y=G,N=C(o,O.__wbindgen_malloc,O.__wbindgen_realloc),x=G;O.calculate_out_given_in(l,u,f,w,y,N,x);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{O.__wbindgen_add_to_stack_pointer(16),O.__wbindgen_free(s,n)}};fe.exports.calculate_in_given_out=function(g,e,o){try{let l=O.__wbindgen_add_to_stack_pointer(-16),u=C(g,O.__wbindgen_malloc,O.__wbindgen_realloc),f=G,w=C(e,O.__wbindgen_malloc,O.__wbindgen_realloc),y=G,N=C(o,O.__wbindgen_malloc,O.__wbindgen_realloc),x=G;O.calculate_in_given_out(l,u,f,w,y,N,x);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{O.__wbindgen_add_to_stack_pointer(16),O.__wbindgen_free(s,n)}};fe.exports.calculate_liquidity_in=function(g,e,o){try{let l=O.__wbindgen_add_to_stack_pointer(-16),u=C(g,O.__wbindgen_malloc,O.__wbindgen_realloc),f=G,w=C(e,O.__wbindgen_malloc,O.__wbindgen_realloc),y=G,N=C(o,O.__wbindgen_malloc,O.__wbindgen_realloc),x=G;O.calculate_liquidity_in(l,u,f,w,y,N,x);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{O.__wbindgen_add_to_stack_pointer(16),O.__wbindgen_free(s,n)}};fe.exports.calculate_shares=function(g,e,o){try{let l=O.__wbindgen_add_to_stack_pointer(-16),u=C(g,O.__wbindgen_malloc,O.__wbindgen_realloc),f=G,w=C(e,O.__wbindgen_malloc,O.__wbindgen_realloc),y=G,N=C(o,O.__wbindgen_malloc,O.__wbindgen_realloc),x=G;O.calculate_shares(l,u,f,w,y,N,x);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{O.__wbindgen_add_to_stack_pointer(16),O.__wbindgen_free(s,n)}};fe.exports.calculate_liquidity_out_asset_a=function(g,e,o,s){try{let u=O.__wbindgen_add_to_stack_pointer(-16),f=C(g,O.__wbindgen_malloc,O.__wbindgen_realloc),w=G,y=C(e,O.__wbindgen_malloc,O.__wbindgen_realloc),N=G,x=C(o,O.__wbindgen_malloc,O.__wbindgen_realloc),v=G,M=C(s,O.__wbindgen_malloc,O.__wbindgen_realloc),E=G;O.calculate_liquidity_out_asset_a(u,f,w,y,N,x,v,M,E);var n=j()[u/4+0],l=j()[u/4+1];return be(n,l)}finally{O.__wbindgen_add_to_stack_pointer(16),O.__wbindgen_free(n,l)}};fe.exports.calculate_liquidity_out_asset_b=function(g,e,o,s){try{let u=O.__wbindgen_add_to_stack_pointer(-16),f=C(g,O.__wbindgen_malloc,O.__wbindgen_realloc),w=G,y=C(e,O.__wbindgen_malloc,O.__wbindgen_realloc),N=G,x=C(o,O.__wbindgen_malloc,O.__wbindgen_realloc),v=G,M=C(s,O.__wbindgen_malloc,O.__wbindgen_realloc),E=G;O.calculate_liquidity_out_asset_b(u,f,w,y,N,x,v,M,E);var n=j()[u/4+0],l=j()[u/4+1];return be(n,l)}finally{O.__wbindgen_add_to_stack_pointer(16),O.__wbindgen_free(n,l)}};var Yt=require("path").join(__dirname,"hydra_dx_wasm_bg.wasm"),$t=require("fs").readFileSync(Yt),Jt=new WebAssembly.Module($t),Qt=new WebAssembly.Instance(Jt,Xt);O=Qt.exports;fe.exports.__wasm=O});var nn={};It(nn,{BigNumber:()=>xe,DECIMAL_PLACES:()=>ft,INFINITY:()=>vt,ONE:()=>ue,PolkadotApiPoolService:()=>Le,PoolError:()=>ut,PoolFactory:()=>Ie,PoolType:()=>he,Router:()=>Se,TradeRouter:()=>Ge,TradeType:()=>ot,XykPool:()=>_e,ZERO:()=>At,bnum:()=>D,scale:()=>ne});module.exports=St(nn);var He=class{constructor(e=1/0){this.capacity=e;this.storage=[]}enqueue(e){if(this.size()===this.capacity)throw Error("Queue has reached max capacity, you cannot add more items");this.storage.push(e)}dequeue(){return this.storage.shift()}size(){return this.storage.length}};var Tt=4,Fe=class{isNotVisited(e,o){let s=!0;return o.forEach(n=>{n[0]===e&&(s=!1)}),s}findPaths(e,o,s){let n=[],l=new He,u=[];for(u.push([o,""]),l.enqueue(u);l.size()>0;){let f=l.dequeue();if(f==null||f.length>Tt)return n;let w=f[f.length-1];(s===null||w[0]===s)&&n.push(f),e[w[0]].forEach(N=>{if(this.isNotVisited(N[0],f)){let x=[...f];x.push(N),l.enqueue(x)}})}return n}buildAndPopulateGraph(e,o){let s=e[e.length-1],n=[];for(let l=0;l<=parseInt(s);l++)n.push([]);for(let[l,u,f]of o){let w=parseInt(u),y=parseInt(f);n[w].push([y,l])}return n}};function nt(g){let e={};for(let o of g){let s=o.tokens.length;for(let n=0;n<s;n++){e[o.tokens[n].id]||(e[o.tokens[n].id]=[]);for(let l=0;l<s;l++){if(n==l)continue;let u=[o.address,o.tokens[n].id,o.tokens[l].id];e[o.tokens[n].id].push(u)}}}return e}var Me=class{getProposals(e,o,s){let n=nt(s),l=Object.keys(n),u=l.map(N=>n[N]).flat(),f=new Fe,w=f.buildAndPopulateGraph(l,u),y=f.findPaths(w,parseInt(e),o?parseInt(o):null);return this.parsePaths(y)}parsePaths(e){let o=[];for(let s of e){let n=[];for(let l=0;l<s.length;l++){let u=s[l],f=s[l+1];if(f==null)break;n.push(this.toEdge(u,f))}o.push(n)}return o}toEdge(e,o){return[o[1],e[0].toString(),o[0].toString()]}};var he=(n=>(n.XYK="XYK",n.LBP="LBP",n.Stable="Stable",n.Omni="Omni",n))(he||{}),ut=(u=>(u.TradingLimitReached="TradingLimitReached",u.InsufficientTradingAmount="InsufficientTradingAmount",u.InsufficientBalance="InsufficientBalance",u.MaxInRatioExceeded="MaxInRatioExceeded",u.AssetAmountNotReachedLimit="AssetAmountNotReachedLimit",u.AssetAmountExceededLimit="AssetAmountExceededLimit",u))(ut||{}),ot=(o=>(o.Buy="Buy",o.Sell="Sell",o))(ot||{});var Oe=class{constructor(e){this.api=e}getStorageKey(e,o){return e[0].toHuman()[o]}getStorageEntryArray(e){return e[1].toHuman()}async getPoolTokens(e,o){let s=o.map(async n=>{let l=await this.getAccountBalance(e,n),f=(await this.getAssetMetadata(n)).toHuman();return{id:n.toString(),balance:l,decimals:f.decimals,symbol:f.symbol}});return Promise.all(s)}async syncPoolTokens(e,o){let s=o.map(async n=>{let l=await this.getAccountBalance(e,n.id);return{...n,balance:l}});return Promise.all(s)}async getAssetMetadata(e){return await this.api.query.assetRegistry.assetMetadataMap(e)}async getAccountBalance(e,o){return o==="0"?await this.getSystemAccountBalance(e):await this.getTokenAccountBalance(e,o)}async getSystemAccountBalance(e){let{data:{free:o}}=await this.api.query.system.account(e);return o.toString()}async getTokenAccountBalance(e,o){let{free:s}=await this.api.query.tokens.accounts(e,o);return s.toString()}};var qe=class extends Oe{constructor(){super(...arguments);this.pools=[];this._poolsLoaded=!1}async getPools(){return this._poolsLoaded?this.pools=await this.syncPools():(this.pools=await this.loadPools(),this._poolsLoaded=!0),this.pools}async loadPools(){let s=(await this.api.query.xyk.poolAssets.entries()).map(async n=>{let l=this.getStorageKey(n,0),u=this.getStorageEntryArray(n),f=await this.getPoolTokens(l,u);return{address:l,type:"XYK",tradeFee:this.getTradeFee(),tokens:f}});return Promise.all(s)}async syncPools(){let o=this.pools.map(async s=>({...s,tokens:await this.syncPoolTokens(s.address,s.tokens)}));return Promise.all(o)}getTradeFee(){return this.api.consts.xyk.getExchangeFee.toJSON()}};var xt=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,rt=Math.ceil,ce=Math.floor,oe="[BigNumber Error] ",pt=oe+"Number primitive has more than 15 significant digits: ",ae=1e14,A=14,it=9007199254740991,st=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],Pe=1e7,V=1e9;function gt(g){var e,o,s,n=B.prototype={constructor:B,toString:null,valueOf:null},l=new B(1),u=20,f=4,w=-7,y=21,N=-1e7,x=1e7,v=!1,M=1,E=0,Y={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:"\xA0",suffix:""},X="0123456789abcdefghijklmnopqrstuvwxyz",$=!0;function B(t,r){var i,_,c,d,P,a,p,h,m=this;if(!(m instanceof B))return new B(t,r);if(r==null){if(t&&t._isBigNumber===!0){m.s=t.s,!t.c||t.e>x?m.c=m.e=null:t.e<N?m.c=[m.e=0]:(m.e=t.e,m.c=t.c.slice());return}if((a=typeof t=="number")&&t*0==0){if(m.s=1/t<0?(t=-t,-1):1,t===~~t){for(d=0,P=t;P>=10;P/=10,d++);d>x?m.c=m.e=null:(m.e=d,m.c=[t]);return}h=String(t)}else{if(!xt.test(h=String(t)))return s(m,h,a);m.s=h.charCodeAt(0)==45?(h=h.slice(1),-1):1}(d=h.indexOf("."))>-1&&(h=h.replace(".","")),(P=h.search(/e/i))>0?(d<0&&(d=P),d+=+h.slice(P+1),h=h.substring(0,P)):d<0&&(d=h.length)}else{if(W(r,2,X.length,"Base"),r==10&&$)return m=new B(t),Q(m,u+m.e+1,f);if(h=String(t),a=typeof t=="number"){if(t*0!=0)return s(m,h,a,r);if(m.s=1/t<0?(h=h.slice(1),-1):1,B.DEBUG&&h.replace(/^0\.0*|\./,"").length>15)throw Error(pt+t)}else m.s=h.charCodeAt(0)===45?(h=h.slice(1),-1):1;for(i=X.slice(0,r),d=P=0,p=h.length;P<p;P++)if(i.indexOf(_=h.charAt(P))<0){if(_=="."){if(P>d){d=p;continue}}else if(!c&&(h==h.toUpperCase()&&(h=h.toLowerCase())||h==h.toLowerCase()&&(h=h.toUpperCase()))){c=!0,P=-1,d=0;continue}return s(m,String(t),a,r)}a=!1,h=o(h,r,10,m.s),(d=h.indexOf("."))>-1?h=h.replace(".",""):d=h.length}for(P=0;h.charCodeAt(P)===48;P++);for(p=h.length;h.charCodeAt(--p)===48;);if(h=h.slice(P,++p)){if(p-=P,a&&B.DEBUG&&p>15&&(t>it||t!==ce(t)))throw Error(pt+m.s*t);if((d=d-P-1)>x)m.c=m.e=null;else if(d<N)m.c=[m.e=0];else{if(m.e=d,m.c=[],P=(d+1)%A,d<0&&(P+=A),P<p){for(P&&m.c.push(+h.slice(0,P)),p-=A;P<p;)m.c.push(+h.slice(P,P+=A));P=A-(h=h.slice(P)).length}else P-=p;for(;P--;h+="0");m.c.push(+h)}}else m.c=[m.e=0]}B.clone=gt,B.ROUND_UP=0,B.ROUND_DOWN=1,B.ROUND_CEIL=2,B.ROUND_FLOOR=3,B.ROUND_HALF_UP=4,B.ROUND_HALF_DOWN=5,B.ROUND_HALF_EVEN=6,B.ROUND_HALF_CEIL=7,B.ROUND_HALF_FLOOR=8,B.EUCLID=9,B.config=B.set=function(t){var r,i;if(t!=null)if(typeof t=="object"){if(t.hasOwnProperty(r="DECIMAL_PLACES")&&(i=t[r],W(i,0,V,r),u=i),t.hasOwnProperty(r="ROUNDING_MODE")&&(i=t[r],W(i,0,8,r),f=i),t.hasOwnProperty(r="EXPONENTIAL_AT")&&(i=t[r],i&&i.pop?(W(i[0],-V,0,r),W(i[1],0,V,r),w=i[0],y=i[1]):(W(i,-V,V,r),w=-(y=i<0?-i:i))),t.hasOwnProperty(r="RANGE"))if(i=t[r],i&&i.pop)W(i[0],-V,-1,r),W(i[1],1,V,r),N=i[0],x=i[1];else if(W(i,-V,V,r),i)N=-(x=i<0?-i:i);else throw Error(oe+r+" cannot be zero: "+i);if(t.hasOwnProperty(r="CRYPTO"))if(i=t[r],i===!!i)if(i)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))v=i;else throw v=!i,Error(oe+"crypto unavailable");else v=i;else throw Error(oe+r+" not true or false: "+i);if(t.hasOwnProperty(r="MODULO_MODE")&&(i=t[r],W(i,0,9,r),M=i),t.hasOwnProperty(r="POW_PRECISION")&&(i=t[r],W(i,0,V,r),E=i),t.hasOwnProperty(r="FORMAT"))if(i=t[r],typeof i=="object")Y=i;else throw Error(oe+r+" not an object: "+i);if(t.hasOwnProperty(r="ALPHABET"))if(i=t[r],typeof i=="string"&&!/^.?$|[+\-.\s]|(.).*\1/.test(i))$=i.slice(0,10)=="0123456789",X=i;else throw Error(oe+r+" invalid: "+i)}else throw Error(oe+"Object expected: "+t);return{DECIMAL_PLACES:u,ROUNDING_MODE:f,EXPONENTIAL_AT:[w,y],RANGE:[N,x],CRYPTO:v,MODULO_MODE:M,POW_PRECISION:E,FORMAT:Y,ALPHABET:X}},B.isBigNumber=function(t){if(!t||t._isBigNumber!==!0)return!1;if(!B.DEBUG)return!0;var r,i,_=t.c,c=t.e,d=t.s;e:if({}.toString.call(_)=="[object Array]"){if((d===1||d===-1)&&c>=-V&&c<=V&&c===ce(c)){if(_[0]===0){if(c===0&&_.length===1)return!0;break e}if(r=(c+1)%A,r<1&&(r+=A),String(_[0]).length==r){for(r=0;r<_.length;r++)if(i=_[r],i<0||i>=ae||i!==ce(i))break e;if(i!==0)return!0}}}else if(_===null&&c===null&&(d===null||d===1||d===-1))return!0;throw Error(oe+"Invalid BigNumber: "+t)},B.maximum=B.max=function(){return ee(arguments,n.lt)},B.minimum=B.min=function(){return ee(arguments,n.gt)},B.random=function(){var t=9007199254740992,r=Math.random()*t&2097151?function(){return ce(Math.random()*t)}:function(){return(Math.random()*1073741824|0)*8388608+(Math.random()*8388608|0)};return function(i){var _,c,d,P,a,p=0,h=[],m=new B(l);if(i==null?i=u:W(i,0,V),P=rt(i/A),v)if(crypto.getRandomValues){for(_=crypto.getRandomValues(new Uint32Array(P*=2));p<P;)a=_[p]*131072+(_[p+1]>>>11),a>=9e15?(c=crypto.getRandomValues(new Uint32Array(2)),_[p]=c[0],_[p+1]=c[1]):(h.push(a%1e14),p+=2);p=P/2}else if(crypto.randomBytes){for(_=crypto.randomBytes(P*=7);p<P;)a=(_[p]&31)*281474976710656+_[p+1]*1099511627776+_[p+2]*4294967296+_[p+3]*16777216+(_[p+4]<<16)+(_[p+5]<<8)+_[p+6],a>=9e15?crypto.randomBytes(7).copy(_,p):(h.push(a%1e14),p+=7);p=P/7}else throw v=!1,Error(oe+"crypto unavailable");if(!v)for(;p<P;)a=r(),a<9e15&&(h[p++]=a%1e14);for(P=h[--p],i%=A,P&&i&&(a=st[A-i],h[p]=ce(P/a)*a);h[p]===0;h.pop(),p--);if(p<0)h=[d=0];else{for(d=-1;h[0]===0;h.splice(0,1),d-=A);for(p=1,a=h[0];a>=10;a/=10,p++);p<A&&(d-=A-p)}return m.e=d,m.c=h,m}}(),B.sum=function(){for(var t=1,r=arguments,i=new B(r[0]);t<r.length;)i=i.plus(r[t++]);return i},o=function(){var t="0123456789";function r(i,_,c,d){for(var P,a=[0],p,h=0,m=i.length;h<m;){for(p=a.length;p--;a[p]*=_);for(a[0]+=d.indexOf(i.charAt(h++)),P=0;P<a.length;P++)a[P]>c-1&&(a[P+1]==null&&(a[P+1]=0),a[P+1]+=a[P]/c|0,a[P]%=c)}return a.reverse()}return function(i,_,c,d,P){var a,p,h,m,b,S,T,F,q=i.indexOf("."),K=u,k=f;for(q>=0&&(m=E,E=0,i=i.replace(".",""),F=new B(_),S=F.pow(i.length-q),E=m,F.c=r(me(ie(S.c),S.e,"0"),10,c,t),F.e=F.c.length),T=r(i,_,c,P?(a=X,t):(a=t,X)),h=m=T.length;T[--m]==0;T.pop());if(!T[0])return a.charAt(0);if(q<0?--h:(S.c=T,S.e=h,S.s=d,S=e(S,F,K,k,c),T=S.c,b=S.r,h=S.e),p=h+K+1,q=T[p],m=c/2,b=b||p<0||T[p+1]!=null,b=k<4?(q!=null||b)&&(k==0||k==(S.s<0?3:2)):q>m||q==m&&(k==4||b||k==6&&T[p-1]&1||k==(S.s<0?8:7)),p<1||!T[0])i=b?me(a.charAt(1),-K,a.charAt(0)):a.charAt(0);else{if(T.length=p,b)for(--c;++T[--p]>c;)T[p]=0,p||(++h,T=[1].concat(T));for(m=T.length;!T[--m];);for(q=0,i="";q<=m;i+=a.charAt(T[q++]));i=me(i,h,a.charAt(0))}return i}}(),e=function(){function t(_,c,d){var P,a,p,h,m=0,b=_.length,S=c%Pe,T=c/Pe|0;for(_=_.slice();b--;)p=_[b]%Pe,h=_[b]/Pe|0,P=T*p+h*S,a=S*p+P%Pe*Pe+m,m=(a/d|0)+(P/Pe|0)+T*h,_[b]=a%d;return m&&(_=[m].concat(_)),_}function r(_,c,d,P){var a,p;if(d!=P)p=d>P?1:-1;else for(a=p=0;a<d;a++)if(_[a]!=c[a]){p=_[a]>c[a]?1:-1;break}return p}function i(_,c,d,P){for(var a=0;d--;)_[d]-=a,a=_[d]<c[d]?1:0,_[d]=a*P+_[d]-c[d];for(;!_[0]&&_.length>1;_.splice(0,1));}return function(_,c,d,P,a){var p,h,m,b,S,T,F,q,K,k,R,Z,Ce,et,tt,le,Ee,re=_.s==c.s?1:-1,te=_.c,U=c.c;if(!te||!te[0]||!U||!U[0])return new B(!_.s||!c.s||(te?U&&te[0]==U[0]:!U)?NaN:te&&te[0]==0||!U?re*0:re/0);for(q=new B(re),K=q.c=[],h=_.e-c.e,re=d+h+1,a||(a=ae,h=se(_.e/A)-se(c.e/A),re=re/A|0),m=0;U[m]==(te[m]||0);m++);if(U[m]>(te[m]||0)&&h--,re<0)K.push(1),b=!0;else{for(et=te.length,le=U.length,m=0,re+=2,S=ce(a/(U[0]+1)),S>1&&(U=t(U,S,a),te=t(te,S,a),le=U.length,et=te.length),Ce=le,k=te.slice(0,le),R=k.length;R<le;k[R++]=0);Ee=U.slice(),Ee=[0].concat(Ee),tt=U[0],U[1]>=a/2&&tt++;do{if(S=0,p=r(U,k,le,R),p<0){if(Z=k[0],le!=R&&(Z=Z*a+(k[1]||0)),S=ce(Z/tt),S>1)for(S>=a&&(S=a-1),T=t(U,S,a),F=T.length,R=k.length;r(T,k,F,R)==1;)S--,i(T,le<F?Ee:U,F,a),F=T.length,p=1;else S==0&&(p=S=1),T=U.slice(),F=T.length;if(F<R&&(T=[0].concat(T)),i(k,T,R,a),R=k.length,p==-1)for(;r(U,k,le,R)<1;)S++,i(k,le<R?Ee:U,R,a),R=k.length}else p===0&&(S++,k=[0]);K[m++]=S,k[0]?k[R++]=te[Ce]||0:(k=[te[Ce]],R=1)}while((Ce++<et||k[0]!=null)&&re--);b=k[0]!=null,K[0]||K.splice(0,1)}if(a==ae){for(m=1,re=K[0];re>=10;re/=10,m++);Q(q,d+(q.e=m+h*A-1)+1,P,b)}else q.e=h,q.r=+b;return q}}();function L(t,r,i,_){var c,d,P,a,p;if(i==null?i=f:W(i,0,8),!t.c)return t.toString();if(c=t.c[0],P=t.e,r==null)p=ie(t.c),p=_==1||_==2&&(P<=w||P>=y)?Xe(p,P):me(p,P,"0");else if(t=Q(new B(t),r,i),d=t.e,p=ie(t.c),a=p.length,_==1||_==2&&(r<=d||d<=w)){for(;a<r;p+="0",a++);p=Xe(p,d)}else if(r-=P,p=me(p,d,"0"),d+1>a){if(--r>0)for(p+=".";r--;p+="0");}else if(r+=d-a,r>0)for(d+1==a&&(p+=".");r--;p+="0");return t.s<0&&c?"-"+p:p}function ee(t,r){for(var i,_=1,c=new B(t[0]);_<t.length;_++)if(i=new B(t[_]),i.s)r.call(c,i)&&(c=i);else{c=i;break}return c}function Be(t,r,i){for(var _=1,c=r.length;!r[--c];r.pop());for(c=r[0];c>=10;c/=10,_++);return(i=_+i*A-1)>x?t.c=t.e=null:i<N?t.c=[t.e=0]:(t.e=i,t.c=r),t}s=function(){var t=/^(-?)0([xbo])(?=\w[\w.]*$)/i,r=/^([^.]+)\.$/,i=/^\.([^.]+)$/,_=/^-?(Infinity|NaN)$/,c=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(d,P,a,p){var h,m=a?P:P.replace(c,"");if(_.test(m))d.s=isNaN(m)?null:m<0?-1:1;else{if(!a&&(m=m.replace(t,function(b,S,T){return h=(T=T.toLowerCase())=="x"?16:T=="b"?2:8,!p||p==h?S:b}),p&&(h=p,m=m.replace(r,"$1").replace(i,"0.$1")),P!=m))return new B(m,h);if(B.DEBUG)throw Error(oe+"Not a"+(p?" base "+p:"")+" number: "+P);d.s=null}d.c=d.e=null}}();function Q(t,r,i,_){var c,d,P,a,p,h,m,b=t.c,S=st;if(b){e:{for(c=1,a=b[0];a>=10;a/=10,c++);if(d=r-c,d<0)d+=A,P=r,p=b[h=0],m=p/S[c-P-1]%10|0;else if(h=rt((d+1)/A),h>=b.length)if(_){for(;b.length<=h;b.push(0));p=m=0,c=1,d%=A,P=d-A+1}else break e;else{for(p=a=b[h],c=1;a>=10;a/=10,c++);d%=A,P=d-A+c,m=P<0?0:p/S[c-P-1]%10|0}if(_=_||r<0||b[h+1]!=null||(P<0?p:p%S[c-P-1]),_=i<4?(m||_)&&(i==0||i==(t.s<0?3:2)):m>5||m==5&&(i==4||_||i==6&&(d>0?P>0?p/S[c-P]:0:b[h-1])%10&1||i==(t.s<0?8:7)),r<1||!b[0])return b.length=0,_?(r-=t.e+1,b[0]=S[(A-r%A)%A],t.e=-r||0):b[0]=t.e=0,t;if(d==0?(b.length=h,a=1,h--):(b.length=h+1,a=S[A-d],b[h]=P>0?ce(p/S[c-P]%S[P])*a:0),_)for(;;)if(h==0){for(d=1,P=b[0];P>=10;P/=10,d++);for(P=b[0]+=a,a=1;P>=10;P/=10,a++);d!=a&&(t.e++,b[0]==ae&&(b[0]=1));break}else{if(b[h]+=a,b[h]!=ae)break;b[h--]=0,a=1}for(d=b.length;b[--d]===0;b.pop());}t.e>x?t.c=t.e=null:t.e<N&&(t.c=[t.e=0])}return t}function de(t){var r,i=t.e;return i===null?t.toString():(r=ie(t.c),r=i<=w||i>=y?Xe(r,i):me(r,i,"0"),t.s<0?"-"+r:r)}return n.absoluteValue=n.abs=function(){var t=new B(this);return t.s<0&&(t.s=1),t},n.comparedTo=function(t,r){return Te(this,new B(t,r))},n.decimalPlaces=n.dp=function(t,r){var i,_,c,d=this;if(t!=null)return W(t,0,V),r==null?r=f:W(r,0,8),Q(new B(d),t+d.e+1,r);if(!(i=d.c))return null;if(_=((c=i.length-1)-se(this.e/A))*A,c=i[c])for(;c%10==0;c/=10,_--);return _<0&&(_=0),_},n.dividedBy=n.div=function(t,r){return e(this,new B(t,r),u,f)},n.dividedToIntegerBy=n.idiv=function(t,r){return e(this,new B(t,r),0,1)},n.exponentiatedBy=n.pow=function(t,r){var i,_,c,d,P,a,p,h,m,b=this;if(t=new B(t),t.c&&!t.isInteger())throw Error(oe+"Exponent not an integer: "+de(t));if(r!=null&&(r=new B(r)),a=t.e>14,!b.c||!b.c[0]||b.c[0]==1&&!b.e&&b.c.length==1||!t.c||!t.c[0])return m=new B(Math.pow(+de(b),a?2-Ue(t):+de(t))),r?m.mod(r):m;if(p=t.s<0,r){if(r.c?!r.c[0]:!r.s)return new B(NaN);_=!p&&b.isInteger()&&r.isInteger(),_&&(b=b.mod(r))}else{if(t.e>9&&(b.e>0||b.e<-1||(b.e==0?b.c[0]>1||a&&b.c[1]>=24e7:b.c[0]<8e13||a&&b.c[0]<=9999975e7)))return d=b.s<0&&Ue(t)?-0:0,b.e>-1&&(d=1/d),new B(p?1/d:d);E&&(d=rt(E/A+2))}for(a?(i=new B(.5),p&&(t.s=1),h=Ue(t)):(c=Math.abs(+de(t)),h=c%2),m=new B(l);;){if(h){if(m=m.times(b),!m.c)break;d?m.c.length>d&&(m.c.length=d):_&&(m=m.mod(r))}if(c){if(c=ce(c/2),c===0)break;h=c%2}else if(t=t.times(i),Q(t,t.e+1,1),t.e>14)h=Ue(t);else{if(c=+de(t),c===0)break;h=c%2}b=b.times(b),d?b.c&&b.c.length>d&&(b.c.length=d):_&&(b=b.mod(r))}return _?m:(p&&(m=l.div(m)),r?m.mod(r):d?Q(m,E,f,P):m)},n.integerValue=function(t){var r=new B(this);return t==null?t=f:W(t,0,8),Q(r,r.e+1,t)},n.isEqualTo=n.eq=function(t,r){return Te(this,new B(t,r))===0},n.isFinite=function(){return!!this.c},n.isGreaterThan=n.gt=function(t,r){return Te(this,new B(t,r))>0},n.isGreaterThanOrEqualTo=n.gte=function(t,r){return(r=Te(this,new B(t,r)))===1||r===0},n.isInteger=function(){return!!this.c&&se(this.e/A)>this.c.length-2},n.isLessThan=n.lt=function(t,r){return Te(this,new B(t,r))<0},n.isLessThanOrEqualTo=n.lte=function(t,r){return(r=Te(this,new B(t,r)))===-1||r===0},n.isNaN=function(){return!this.s},n.isNegative=function(){return this.s<0},n.isPositive=function(){return this.s>0},n.isZero=function(){return!!this.c&&this.c[0]==0},n.minus=function(t,r){var i,_,c,d,P=this,a=P.s;if(t=new B(t,r),r=t.s,!a||!r)return new B(NaN);if(a!=r)return t.s=-r,P.plus(t);var p=P.e/A,h=t.e/A,m=P.c,b=t.c;if(!p||!h){if(!m||!b)return m?(t.s=-r,t):new B(b?P:NaN);if(!m[0]||!b[0])return b[0]?(t.s=-r,t):new B(m[0]?P:f==3?-0:0)}if(p=se(p),h=se(h),m=m.slice(),a=p-h){for((d=a<0)?(a=-a,c=m):(h=p,c=b),c.reverse(),r=a;r--;c.push(0));c.reverse()}else for(_=(d=(a=m.length)<(r=b.length))?a:r,a=r=0;r<_;r++)if(m[r]!=b[r]){d=m[r]<b[r];break}if(d&&(c=m,m=b,b=c,t.s=-t.s),r=(_=b.length)-(i=m.length),r>0)for(;r--;m[i++]=0);for(r=ae-1;_>a;){if(m[--_]<b[_]){for(i=_;i&&!m[--i];m[i]=r);--m[i],m[_]+=ae}m[_]-=b[_]}for(;m[0]==0;m.splice(0,1),--h);return m[0]?Be(t,m,h):(t.s=f==3?-1:1,t.c=[t.e=0],t)},n.modulo=n.mod=function(t,r){var i,_,c=this;return t=new B(t,r),!c.c||!t.s||t.c&&!t.c[0]?new B(NaN):!t.c||c.c&&!c.c[0]?new B(c):(M==9?(_=t.s,t.s=1,i=e(c,t,0,3),t.s=_,i.s*=_):i=e(c,t,0,M),t=c.minus(i.times(t)),!t.c[0]&&M==1&&(t.s=c.s),t)},n.multipliedBy=n.times=function(t,r){var i,_,c,d,P,a,p,h,m,b,S,T,F,q,K,k=this,R=k.c,Z=(t=new B(t,r)).c;if(!R||!Z||!R[0]||!Z[0])return!k.s||!t.s||R&&!R[0]&&!Z||Z&&!Z[0]&&!R?t.c=t.e=t.s=null:(t.s*=k.s,!R||!Z?t.c=t.e=null:(t.c=[0],t.e=0)),t;for(_=se(k.e/A)+se(t.e/A),t.s*=k.s,p=R.length,b=Z.length,p<b&&(F=R,R=Z,Z=F,c=p,p=b,b=c),c=p+b,F=[];c--;F.push(0));for(q=ae,K=Pe,c=b;--c>=0;){for(i=0,S=Z[c]%K,T=Z[c]/K|0,P=p,d=c+P;d>c;)h=R[--P]%K,m=R[P]/K|0,a=T*h+m*S,h=S*h+a%K*K+F[d]+i,i=(h/q|0)+(a/K|0)+T*m,F[d--]=h%q;F[d]=i}return i?++_:F.splice(0,1),Be(t,F,_)},n.negated=function(){var t=new B(this);return t.s=-t.s||null,t},n.plus=function(t,r){var i,_=this,c=_.s;if(t=new B(t,r),r=t.s,!c||!r)return new B(NaN);if(c!=r)return t.s=-r,_.minus(t);var d=_.e/A,P=t.e/A,a=_.c,p=t.c;if(!d||!P){if(!a||!p)return new B(c/0);if(!a[0]||!p[0])return p[0]?t:new B(a[0]?_:c*0)}if(d=se(d),P=se(P),a=a.slice(),c=d-P){for(c>0?(P=d,i=p):(c=-c,i=a),i.reverse();c--;i.push(0));i.reverse()}for(c=a.length,r=p.length,c-r<0&&(i=p,p=a,a=i,r=c),c=0;r;)c=(a[--r]=a[r]+p[r]+c)/ae|0,a[r]=ae===a[r]?0:a[r]%ae;return c&&(a=[c].concat(a),++P),Be(t,a,P)},n.precision=n.sd=function(t,r){var i,_,c,d=this;if(t!=null&&t!==!!t)return W(t,1,V),r==null?r=f:W(r,0,8),Q(new B(d),t,r);if(!(i=d.c))return null;if(c=i.length-1,_=c*A+1,c=i[c]){for(;c%10==0;c/=10,_--);for(c=i[0];c>=10;c/=10,_++);}return t&&d.e+1>_&&(_=d.e+1),_},n.shiftedBy=function(t){return W(t,-it,it),this.times("1e"+t)},n.squareRoot=n.sqrt=function(){var t,r,i,_,c,d=this,P=d.c,a=d.s,p=d.e,h=u+4,m=new B("0.5");if(a!==1||!P||!P[0])return new B(!a||a<0&&(!P||P[0])?NaN:P?d:1/0);if(a=Math.sqrt(+de(d)),a==0||a==1/0?(r=ie(P),(r.length+p)%2==0&&(r+="0"),a=Math.sqrt(+r),p=se((p+1)/2)-(p<0||p%2),a==1/0?r="5e"+p:(r=a.toExponential(),r=r.slice(0,r.indexOf("e")+1)+p),i=new B(r)):i=new B(a+""),i.c[0]){for(p=i.e,a=p+h,a<3&&(a=0);;)if(c=i,i=m.times(c.plus(e(d,c,h,1))),ie(c.c).slice(0,a)===(r=ie(i.c)).slice(0,a))if(i.e<p&&--a,r=r.slice(a-3,a+1),r=="9999"||!_&&r=="4999"){if(!_&&(Q(c,c.e+u+2,0),c.times(c).eq(d))){i=c;break}h+=4,a+=4,_=1}else{(!+r||!+r.slice(1)&&r.charAt(0)=="5")&&(Q(i,i.e+u+2,1),t=!i.times(i).eq(d));break}}return Q(i,i.e+u+1,f,t)},n.toExponential=function(t,r){return t!=null&&(W(t,0,V),t++),L(this,t,r,1)},n.toFixed=function(t,r){return t!=null&&(W(t,0,V),t=t+this.e+1),L(this,t,r)},n.toFormat=function(t,r,i){var _,c=this;if(i==null)t!=null&&r&&typeof r=="object"?(i=r,r=null):t&&typeof t=="object"?(i=t,t=r=null):i=Y;else if(typeof i!="object")throw Error(oe+"Argument not an object: "+i);if(_=c.toFixed(t,r),c.c){var d,P=_.split("."),a=+i.groupSize,p=+i.secondaryGroupSize,h=i.groupSeparator||"",m=P[0],b=P[1],S=c.s<0,T=S?m.slice(1):m,F=T.length;if(p&&(d=a,a=p,p=d,F-=d),a>0&&F>0){for(d=F%a||a,m=T.substr(0,d);d<F;d+=a)m+=h+T.substr(d,a);p>0&&(m+=h+T.slice(d)),S&&(m="-"+m)}_=b?m+(i.decimalSeparator||"")+((p=+i.fractionGroupSize)?b.replace(new RegExp("\\d{"+p+"}\\B","g"),"$&"+(i.fractionGroupSeparator||"")):b):m}return(i.prefix||"")+_+(i.suffix||"")},n.toFraction=function(t){var r,i,_,c,d,P,a,p,h,m,b,S,T=this,F=T.c;if(t!=null&&(a=new B(t),!a.isInteger()&&(a.c||a.s!==1)||a.lt(l)))throw Error(oe+"Argument "+(a.isInteger()?"out of range: ":"not an integer: ")+de(a));if(!F)return new B(T);for(r=new B(l),h=i=new B(l),_=p=new B(l),S=ie(F),d=r.e=S.length-T.e-1,r.c[0]=st[(P=d%A)<0?A+P:P],t=!t||a.comparedTo(r)>0?d>0?r:h:a,P=x,x=1/0,a=new B(S),p.c[0]=0;m=e(a,r,0,1),c=i.plus(m.times(_)),c.comparedTo(t)!=1;)i=_,_=c,h=p.plus(m.times(c=h)),p=c,r=a.minus(m.times(c=r)),a=c;return c=e(t.minus(i),_,0,1),p=p.plus(c.times(h)),i=i.plus(c.times(_)),p.s=h.s=T.s,d=d*2,b=e(h,_,d,f).minus(T).abs().comparedTo(e(p,i,d,f).minus(T).abs())<1?[h,_]:[p,i],x=P,b},n.toNumber=function(){return+de(this)},n.toPrecision=function(t,r){return t!=null&&W(t,1,V),L(this,t,r,2)},n.toString=function(t){var r,i=this,_=i.s,c=i.e;return c===null?_?(r="Infinity",_<0&&(r="-"+r)):r="NaN":(t==null?r=c<=w||c>=y?Xe(ie(i.c),c):me(ie(i.c),c,"0"):t===10&&$?(i=Q(new B(i),u+c+1,f),r=me(ie(i.c),i.e,"0")):(W(t,2,X.length,"Base"),r=o(me(ie(i.c),c,"0"),10,t,_,!0)),_<0&&i.c[0]&&(r="-"+r)),r},n.valueOf=n.toJSON=function(){return de(this)},n._isBigNumber=!0,n[Symbol.toStringTag]="BigNumber",n[Symbol.for("nodejs.util.inspect.custom")]=n.valueOf,g!=null&&B.set(g),B}function se(g){var e=g|0;return g>0||g===e?e:e-1}function ie(g){for(var e,o,s=1,n=g.length,l=g[0]+"";s<n;){for(e=g[s++]+"",o=A-e.length;o--;e="0"+e);l+=e}for(n=l.length;l.charCodeAt(--n)===48;);return l.slice(0,n+1||1)}function Te(g,e){var o,s,n=g.c,l=e.c,u=g.s,f=e.s,w=g.e,y=e.e;if(!u||!f)return null;if(o=n&&!n[0],s=l&&!l[0],o||s)return o?s?0:-f:u;if(u!=f)return u;if(o=u<0,s=w==y,!n||!l)return s?0:!n^o?1:-1;if(!s)return w>y^o?1:-1;for(f=(w=n.length)<(y=l.length)?w:y,u=0;u<f;u++)if(n[u]!=l[u])return n[u]>l[u]^o?1:-1;return w==y?0:w>y^o?1:-1}function W(g,e,o,s){if(g<e||g>o||g!==ce(g))throw Error(oe+(s||"Argument")+(typeof g=="number"?g<e||g>o?" out of range: ":" not an integer: ":" not a primitive number: ")+String(g))}function Ue(g){var e=g.c.length-1;return se(g.e/A)==e&&g.c[e]%2!=0}function Xe(g,e){return(g.length>1?g.charAt(0)+"."+g.slice(1):g)+(e<0?"e":"e+")+e}function me(g,e,o){var s,n;if(e<0){for(n=o+".";++e;n+=o);g=n+g}else if(s=g.length,++e>s){for(n=o,e-=s;--e;n+=o);g+=n}else e<s&&(g=g.slice(0,e)+"."+g.slice(e));return g}var xe=gt();var ft=12;xe.config({EXPONENTIAL_AT:[-100,100],ROUNDING_MODE:1,DECIMAL_PLACES:ft});var At=D(0),ue=D(1),vt=D("Infinity");function ne(g,e){let o=new xe(e.toString()),s=new xe(10).pow(o);return g.times(s)}function D(g){return new xe(g.toString())}var ge=ct(mt());function Ct(g,e,o,s,n){return(0,ge.get_spot_price)(g,e,o,s,n)}function Wt(g,e,o,s,n){return(0,ge.calculate_in_given_out)(g,e,o,s,n)}function Ht(g,e,o,s,n){return(0,ge.calculate_out_given_in)(g,e,o,s,n)}function qt(g,e,o,s,n){return(0,ge.calculate_linear_weights)(g,e,o,s,n)}function Ut(g,e,o){return(0,ge.calculate_pool_trade_fee)(g,e,o)}var we={getSpotPrice:Ct,calculateInGivenOut:Wt,calculateOutGivenIn:Ht,calculateLinearWeights:qt,calculatePoolTradeFee:Ut};var $e=class extends Oe{constructor(){super(...arguments);this.MAX_FINAL_WEIGHT=ne(D(100),6);this.poolsData=new Map([]);this.pools=[];this._poolsLoaded=!1}async getPools(){return this._poolsLoaded?this.pools=await this.syncPools():(this.pools=await this.loadPools(),this._poolsLoaded=!0),this.pools}async loadPools(){let s=(await this.api.query.lbp.poolData.entries()).map(async n=>{let l=this.getStorageKey(n,0),u=n[1].toJSON();this.poolsData.set(l,u);let f=await this.getPoolTokens(l,u.assets),w=await this.getLinearWeight(u),y=D(w),N=this.MAX_FINAL_WEIGHT.minus(D(y)),x=f[0].id;return{address:l,type:"LBP",tradeFee:u.fee,repayFee:this.getRepayFee(),repayFeeApply:await this.isRepayFeeApplied(x,u),tokens:[{...f[0],weight:y},{...f[1],weight:N}]}});return Promise.all(s)}async syncPools(){let o=this.pools.map(async s=>{let n=this.poolsData.get(s.address),l=await this.syncPoolTokens(s.address,s.tokens),u=await this.getLinearWeight(n),f=D(u),w=this.MAX_FINAL_WEIGHT.minus(D(f)),y=l[0].id;return{...s,repayFeeApply:await this.isRepayFeeApplied(y,n),tokens:[{...l[0],weight:f},{...l[1],weight:w}]}});return Promise.all(o)}async getLinearWeight(o){let n=(await this.api.query.parachainSystem.validationData()).toJSON();return we.calculateLinearWeights(o.start.toString(),o.end.toString(),o.initialWeight.toString(),o.finalWeight.toString(),n.relayParentNumber.toString())}getRepayFee(){return this.api.consts.lbp.repayFee.toJSON()}async isRepayFeeApplied(o,s){let n=D(s.repayTarget);try{let l=await this.getAccountBalance(o,s.feeCollector);return D(l).isLessThan(n)}catch{return!0}}};var Le=class{constructor(e){this.api=e,this.xykClient=new qe(this.api),this.lbpClient=new $e(this.api)}async getPools(e){if(e.length==0)return(await Promise.all([this.xykClient.getPools(),this.lbpClient.getPools()])).flat();let o=[];return e.forEach(n=>{switch(n){case"XYK":o.push(this.xykClient.getPools());break;case"LBP":o.push(this.lbpClient.getPools());break}}),(await Promise.all(o)).flat()}buildBuyTx(e,o,s,n,l){let u=this.api.tx.router.buy(e,o,s.toFixed(),n.toFixed(),l),f=()=>u;return{hex:u.toHex(),name:"RouterBuy",get:f}}buildSellTx(e,o,s,n,l){let u=this.api.tx.router.sell(e,o,s.toFixed(),n.toFixed(),l),f=()=>u;return{hex:u.toHex(),name:"RouterSell",get:f}}};function J(g,e){return g.shiftedBy(-1*e).toString()}function Ae(g){return g[0]/g[1]*100}var ve=class{static fromPool(e){if(!e.repayFee)throw new Error("LBP Pool missing repayFee");if(!e.repayFeeApply)throw new Error("LBP Pool missing repayFeeApply");return new ve(e.address,e.tradeFee,e.tokens,e.repayFee,e.repayFeeApply)}constructor(e,o,s,n,l){this.type="LBP",this.address=e,this.tradeFee=o,this.tokens=s,this.repayFee=n,this.repayFeeApply=l}validPair(e,o){return!0}parsePoolPair(e,o){let s=new Map(this.tokens.map(w=>[w.id,w])),n=s.get(e),l=s.get(o);if(n==null)throw new Error("Pool does not contain tokenIn");if(l==null)throw new Error("Pool does not contain tokenOut");let u=D(n.balance),f=D(l.balance);return{assetIn:e,assetOut:o,decimalsIn:n.decimals,decimalsOut:l.decimals,weightIn:n.weight,weightOut:l.weight,balanceIn:u,balanceOut:f}}validateBuy(e,o){if(this.tokens[0].id===e.assetOut){let n=this.calculateTradeFee(o),l=o.plus(n),u=this.calculateInGivenOut(e,l),f=Ae(this.repayFeeApply?this.repayFee:this.tradeFee);return{amountIn:u,calculatedIn:u,amountOut:o,feePct:f}}else{let n=this.calculateInGivenOut(e,o);return{amountIn:n,calculatedIn:n,amountOut:o,feePct:0}}}validateSell(e,o){if(this.tokens[0].id===e.assetIn){let n=this.calculateOutGivenIn(e,o);return{amountIn:o,calculatedOut:n,amountOut:n,feePct:0}}else{let n=this.calculateOutGivenIn(e,o),l=this.calculateTradeFee(n),u=n.minus(l),f=Ae(this.repayFeeApply?this.repayFee:this.tradeFee);return{amountIn:o,calculatedOut:n,amountOut:u,feePct:f}}}calculateInGivenOut(e,o){let s=we.calculateInGivenOut(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),o.toString());return D(s)}calculateOutGivenIn(e,o){let s=we.calculateOutGivenIn(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),o.toString());return D(s)}spotPriceInGivenOut(e){let o=we.getSpotPrice(e.balanceOut.toString(),e.balanceIn.toString(),e.weightOut.toString(),e.weightIn.toString(),ne(ue,e.decimalsOut).toString());return D(o)}spotPriceOutGivenIn(e){let o=we.getSpotPrice(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),ne(ue,e.decimalsIn).toString());return D(o)}calculateTradeFee(e){let o=we.calculatePoolTradeFee(e.toString(),this.repayFeeApply?this.repayFee[0]:this.tradeFee[0],this.repayFeeApply?this.repayFee[1]:this.tradeFee[1]);return D(o)}};var ye=ct(ht());function Zt(g,e,o){return(0,ye.get_spot_price)(g,e,o)}function jt(g,e,o){return(0,ye.calculate_in_given_out)(g,e,o)}function en(g,e,o){return(0,ye.calculate_out_given_in)(g,e,o)}function tn(g,e,o){return(0,ye.calculate_pool_trade_fee)(g,e,o)}var ke={getSpotPrice:Zt,calculateInGivenOut:jt,calculateOutGivenIn:en,calculatePoolTradeFee:tn};var _e=class{static fromPool(e){return new _e(e.address,e.tradeFee,e.tokens)}constructor(e,o,s){this.type="XYK",this.address=e,this.tradeFee=o,this.tokens=s}validPair(e,o){return!0}parsePoolPair(e,o){let s=new Map(this.tokens.map(w=>[w.id,w])),n=s.get(e),l=s.get(o);if(n==null)throw new Error("Pool does not contain tokenIn");if(l==null)throw new Error("Pool does not contain tokenOut");let u=D(n.balance),f=D(l.balance);return{assetIn:e,assetOut:o,decimalsIn:n.decimals,decimalsOut:l.decimals,balanceIn:u,balanceOut:f}}validateBuy(e,o){let s=this.calculateInGivenOut(e,o),n=this.calculateTradeFee(s),l=s.plus(n),u=Ae(this.tradeFee);return{amountIn:l,calculatedIn:s,amountOut:o,feePct:u}}validateSell(e,o){let s=this.calculateOutGivenIn(e,o),n=this.calculateTradeFee(s),l=s.minus(n),u=Ae(this.tradeFee);return{amountIn:o,calculatedOut:s,amountOut:l,feePct:u}}calculateInGivenOut(e,o){let s=ke.calculateInGivenOut(e.balanceIn.toString(),e.balanceOut.toString(),o.toString());return D(s)}calculateOutGivenIn(e,o){let s=ke.calculateOutGivenIn(e.balanceIn.toString(),e.balanceOut.toString(),o.toString());return D(s)}spotPriceInGivenOut(e){let o=ke.getSpotPrice(e.balanceOut.toString(),e.balanceIn.toString(),ne(ue,e.decimalsOut).toString());return D(o)}spotPriceOutGivenIn(e){let o=ke.getSpotPrice(e.balanceIn.toString(),e.balanceOut.toString(),ne(ue,e.decimalsIn).toString());return D(o)}calculateTradeFee(e){let o=ke.calculatePoolTradeFee(e.toString(),this.tradeFee[0],this.tradeFee[1]);return D(o)}};var Ie=class{static get(e){switch(e.type){case"XYK":return _e.fromPool(e);case"LBP":return ve.fromPool(e);default:throw new Error("Pool type "+e.type+" is not supported yet")}}};var Se=class{constructor(e,o){this.defaultRouterOptions={includeOnly:[]};this.poolService=e,this.routeSuggester=new Me,this.routerOptions={...this.defaultRouterOptions,...o}}async getPools(){let e=this.routerOptions.includeOnly;return await this.poolService.getPools(e)}async getAllAssets(){let e=await this.getPools();if(e.length===0)throw new Error("No pools configured");let o=await this.getAssets(e);return[...new Map(o).values()]}async getAssetPairs(e){let o=await this.getPools();if(o.length===0)throw new Error("No pools configured");let{assets:s,poolsMap:n}=await this.validateToken(e,o),u=this.getPaths(e,null,n,o).map(f=>f[f.length-1].assetOut);return this.toPoolAssets([...new Set(u)],s)}async getAllPaths(e,o){let s=await this.getPools();if(s.length===0)throw new Error("No pools configured");let{poolsMap:n}=await this.validateTokenPair(e,o,s);return this.getPaths(e,o,n,s)}async getAssets(e){let o=e.map(s=>s.tokens.map(({id:n,symbol:l})=>({id:n,symbol:l}))).flat();return new Map(o.map(s=>[s.id,s]))}getPaths(e,o,s,n){return this.routeSuggester.getProposals(e,o,n).filter(f=>this.validPath(f,s)).map(f=>this.toHops(f,s))}async validateTokenPair(e,o,s){let n=await this.getAssets(s);if(n.get(e)==null)throw new Error(e+" is not supported token");if(n.get(o)==null)throw new Error(o+" is not supported token");let l=this.getPoolMap(s);return{assets:n,poolsMap:l}}async validateToken(e,o){let s=await this.getAssets(o);if(s.get(e)==null)throw new Error(e+" is not supported token");let n=this.getPoolMap(o);return{assets:s,poolsMap:n}}getPoolMap(e){return new Map(e.map(o=>[o.address,Ie.get(o)]))}validPath(e,o){return e.length>0&&e.map(s=>this.validEdge(s,o)).reduce((s,n)=>s&&n)}validEdge([e,o,s],n){return n.get(e)?.validPair(o,s)||!1}toHops(e,o){return e.map(([s,n,l])=>{let u=o.get(s);return{poolId:s,poolType:u?.type,assetIn:n,assetOut:l}})}toPoolAssets(e,o){return e.map(s=>{let n=o.get(s);return{id:s,symbol:n?.symbol}})}};function Re(g,e,o,s){let n=g.shiftedBy(-1*e).multipliedBy(o),l=s;return n.minus(l).abs().div(n.plus(l).div(2)).multipliedBy(100).decimalPlaces(2)}function Pt(g,e){return ue.minus(e.div(g)).multipliedBy(100).decimalPlaces(2)}function wt(g,e){return e.div(g).minus(ue).multipliedBy(100).decimalPlaces(2)}var Ge=class extends Se{async getBestSpotPrice(e,o){let s=await super.getPools();if(s.length===0)throw new Error("No pools configured");let{poolsMap:n}=await super.validateTokenPair(e,o,s);return super.getPaths(e,o,n,s).map(f=>this.getSpotPrice(f,n)).sort((f,w)=>{let y=f.amount,N=w.amount;return y.isGreaterThan(N)?-1:1})[0]}getSpotPrice(e,o){let s=[];for(let u=0;u<e.length;u++){let f=e[u],w=o.get(f.poolId);if(w==null)throw new Error("Pool does not exit");let y=w.parsePoolPair(f.assetIn,f.assetOut),N=w.spotPriceOutGivenIn(y);s.push({amount:N,decimals:y.decimalsOut})}let n=s.map(u=>u.amount.shiftedBy(-1*u.decimals)).reduce((u,f)=>u.multipliedBy(f)),l=s[s.length-1].decimals;return{amount:ne(n,l).decimalPlaces(0,1),decimals:l}}async getBestSell(e,o,s){let n=await super.getPools();if(n.length===0)throw new Error("No pools configured");let{poolsMap:l}=await super.validateTokenPair(e,o,n),w=super.getPaths(e,o,l,n).map(L=>this.toSellSwaps(s,L,l)).sort((L,ee)=>{let Be=L[L.length-1].amountOut,Q=ee[ee.length-1].amountOut;return Be.isGreaterThan(Q)?-1:1})[0],y=w[0],N=w[w.length-1],x=w.map(L=>L.spotPrice.shiftedBy(-1*L.assetOutDecimals)).reduce((L,ee)=>L.multipliedBy(ee)),v=ne(x,N.assetOutDecimals).decimalPlaces(0,1),M=Re(y.amountIn,y.assetInDecimals,v,N.calculatedOut),E=this.calculateDelta0Y(y.amountIn,w,l),Y=N.amountOut,X=Pt(E,Y),$=E.minus(Y),B=L=>this.poolService.buildSellTx(e,o,y.amountIn,L,w.map(ee=>ee));return{type:"Sell",amountIn:y.amountIn,amountOut:N.amountOut,spotPrice:v,tradeFee:$,tradeFeePct:X.toNumber(),priceImpactPct:M.toNumber(),swaps:w,toTx:B,toHuman(){return{type:"Sell",amountIn:J(y.amountIn,y.assetInDecimals),amountOut:J(N.amountOut,N.assetOutDecimals),spotPrice:J(v,N.assetOutDecimals),tradeFee:J($,N.assetOutDecimals),tradeFeePct:X.toNumber(),priceImpactPct:M.toNumber(),swaps:w.map(L=>L.toHuman())}}}}calculateDelta0Y(e,o,s){let n=[];for(let l=0;l<o.length;l++){let u=o[l],f=s.get(u.poolId);if(f==null)throw new Error("Pool does not exit");let w=f.parsePoolPair(u.assetIn,u.assetOut),y;l>0?y=n[l-1]:y=e;let N=f.calculateOutGivenIn(w,y);n.push(N)}return n[n.length-1]}toSellSwaps(e,o,s){let n=[];for(let l=0;l<o.length;l++){let u=o[l],f=s.get(u.poolId);if(f==null)throw new Error("Pool does not exit");let w=f.parsePoolPair(u.assetIn,u.assetOut),y;l>0?y=n[l-1].amountOut:y=ne(D(e),w.decimalsIn);let{amountOut:N,calculatedOut:x,feePct:v}=f.validateSell(w,y),M=f.spotPriceOutGivenIn(w),E=Re(y,w.decimalsIn,M,x);n.push({...u,assetInDecimals:w.decimalsIn,assetOutDecimals:w.decimalsOut,amountIn:y,calculatedOut:x,amountOut:N,spotPrice:M,tradeFeePct:v,priceImpactPct:E.toNumber(),toHuman(){return{...u,amountIn:J(y,w.decimalsIn),calculatedOut:J(x,w.decimalsOut),amountOut:J(N,w.decimalsOut),spotPrice:J(M,w.decimalsOut),tradeFeePct:v,priceImpactPct:E.toNumber()}}})}return n}async getBestBuy(e,o,s){let n=await super.getPools();if(n.length===0)throw new Error("No pools configured");let{poolsMap:l}=await super.validateTokenPair(e,o,n),w=super.getPaths(e,o,l,n).map(L=>this.toBuySwaps(s,L,l)).sort((L,ee)=>{let Be=L[0].amountIn,Q=ee[0].amountIn;return Be.isGreaterThan(Q)?1:-1})[0],y=w[w.length-1],N=w[0],x=w.map(L=>L.spotPrice.shiftedBy(-1*L.assetInDecimals)).reduce((L,ee)=>L.multipliedBy(ee)),v=ne(x,N.assetInDecimals).decimalPlaces(0,1),M=Re(y.amountOut,y.assetOutDecimals,v,N.calculatedIn),E=this.calculateDelta0X(y.amountOut,w,l),Y=N.amountIn,X=wt(E,Y),$=Y.minus(E),B=L=>this.poolService.buildBuyTx(e,o,y.amountOut,L,w.map(ee=>ee));return{type:"Buy",amountOut:y.amountOut,amountIn:N.amountIn,spotPrice:v,tradeFee:$,tradeFeePct:X.toNumber(),priceImpactPct:M.toNumber(),swaps:w,toTx:B,toHuman(){return{type:"Buy",amountOut:J(y.amountOut,y.assetOutDecimals),amountIn:J(N.amountIn,N.assetInDecimals),spotPrice:J(v,N.assetInDecimals),tradeFee:J($,N.assetInDecimals),tradeFeePct:X.toNumber(),priceImpactPct:M.toNumber(),swaps:w.map(L=>L.toHuman())}}}}calculateDelta0X(e,o,s){let n=[];for(let l=o.length-1;l>=0;l--){let u=o[l],f=s.get(u.poolId);if(f==null)throw new Error("Pool does not exit");let w=f.parsePoolPair(u.assetIn,u.assetOut),y;l==o.length-1?y=e:y=n[0];let N=f.calculateInGivenOut(w,y);n.unshift(N)}return n[0]}toBuySwaps(e,o,s){let n=[];for(let l=o.length-1;l>=0;l--){let u=o[l],f=s.get(u.poolId);if(f==null)throw new Error("Pool does not exit");let w=f.parsePoolPair(u.assetIn,u.assetOut),y;l==o.length-1?y=ne(D(e),w.decimalsOut):y=n[0].amountIn;let{amountIn:N,calculatedIn:x,feePct:v}=f.validateBuy(w,y),M=f.spotPriceInGivenOut(w),E=Re(y,w.decimalsOut,M,x);n.unshift({...u,assetInDecimals:w.decimalsIn,assetOutDecimals:w.decimalsOut,amountOut:y,calculatedIn:x,amountIn:N,spotPrice:M,tradeFeePct:v,priceImpactPct:E.toNumber(),toHuman(){return{...u,amountOut:J(y,w.decimalsOut),calculatedIn:J(x,w.decimalsIn),amountIn:J(N,w.decimalsIn),spotPrice:J(M,w.decimalsIn),tradeFeePct:v,priceImpactPct:E.toNumber()}}})}return n}};0&&(module.exports={BigNumber,DECIMAL_PLACES,INFINITY,ONE,PolkadotApiPoolService,PoolError,PoolFactory,PoolType,Router,TradeRouter,TradeType,XykPool,ZERO,bnum,scale});
1
+ "use strict";var bt=Object.create;var We=Object.defineProperty;var yt=Object.getOwnPropertyDescriptor;var Ot=Object.getOwnPropertyNames;var Bt=Object.getPrototypeOf,It=Object.prototype.hasOwnProperty;var lt=(m,e)=>()=>(e||m((e={exports:{}}).exports,e),e.exports),xt=(m,e)=>{for(var o in e)We(m,o,{get:e[o],enumerable:!0})},ct=(m,e,o,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of Ot(e))!It.call(m,n)&&n!==o&&We(m,n,{get:()=>e[n],enumerable:!(s=yt(e,n))||s.enumerable});return m};var ut=(m,e,o)=>(o=m!=null?bt(Bt(m)):{},ct(e||!m||!m.__esModule?We(o,"default",{value:m,enumerable:!0}):o,m)),Nt=m=>ct(We({},"__esModule",{value:!0}),m);var dt=lt((Mn,Ie)=>{var Et={},x,{TextEncoder:kt,TextDecoder:Ft}=require("util"),q=0,ze=null;function Ve(){return(ze===null||ze.buffer!==x.memory.buffer)&&(ze=new Uint8Array(x.memory.buffer)),ze}var Ye=new kt("utf-8"),Mt=typeof Ye.encodeInto=="function"?function(m,e){return Ye.encodeInto(m,e)}:function(m,e){let o=Ye.encode(m);return e.set(o),{read:m.length,written:o.length}};function K(m,e,o){if(o===void 0){let p=Ye.encode(m),w=e(p.length);return Ve().subarray(w,w+p.length).set(p),q=p.length,w}let s=m.length,n=e(s),l=Ve(),u=0;for(;u<s;u++){let p=m.charCodeAt(u);if(p>127)break;l[n+u]=p}if(u!==s){u!==0&&(m=m.slice(u)),n=o(n,s,s=u+m.length*3);let p=Ve().subarray(n+u,n+s);u+=Mt(m,p).written}return q=u,n}var Ke=null;function ge(){return(Ke===null||Ke.buffer!==x.memory.buffer)&&(Ke=new Int32Array(x.memory.buffer)),Ke}var ft=new Ft("utf-8",{ignoreBOM:!0,fatal:!0});ft.decode();function Re(m,e){return ft.decode(Ve().subarray(m,m+e))}Ie.exports.calculate_pool_trade_fee=function(m,e,o){try{let l=x.__wbindgen_add_to_stack_pointer(-16),u=K(m,x.__wbindgen_malloc,x.__wbindgen_realloc),p=q;x.calculate_pool_trade_fee(l,u,p,e,o);var s=ge()[l/4+0],n=ge()[l/4+1];return Re(s,n)}finally{x.__wbindgen_add_to_stack_pointer(16),x.__wbindgen_free(s,n)}};Ie.exports.get_spot_price=function(m,e,o,s,n){try{let p=x.__wbindgen_add_to_stack_pointer(-16),w=K(m,x.__wbindgen_malloc,x.__wbindgen_realloc),y=q,I=K(e,x.__wbindgen_malloc,x.__wbindgen_realloc),S=q,k=K(o,x.__wbindgen_malloc,x.__wbindgen_realloc),M=q,A=K(s,x.__wbindgen_malloc,x.__wbindgen_realloc),W=q,z=K(n,x.__wbindgen_malloc,x.__wbindgen_realloc),J=q;x.get_spot_price(p,w,y,I,S,k,M,A,W,z,J);var l=ge()[p/4+0],u=ge()[p/4+1];return Re(l,u)}finally{x.__wbindgen_add_to_stack_pointer(16),x.__wbindgen_free(l,u)}};Ie.exports.calculate_out_given_in=function(m,e,o,s,n){try{let p=x.__wbindgen_add_to_stack_pointer(-16),w=K(m,x.__wbindgen_malloc,x.__wbindgen_realloc),y=q,I=K(e,x.__wbindgen_malloc,x.__wbindgen_realloc),S=q,k=K(o,x.__wbindgen_malloc,x.__wbindgen_realloc),M=q,A=K(s,x.__wbindgen_malloc,x.__wbindgen_realloc),W=q,z=K(n,x.__wbindgen_malloc,x.__wbindgen_realloc),J=q;x.calculate_out_given_in(p,w,y,I,S,k,M,A,W,z,J);var l=ge()[p/4+0],u=ge()[p/4+1];return Re(l,u)}finally{x.__wbindgen_add_to_stack_pointer(16),x.__wbindgen_free(l,u)}};Ie.exports.calculate_in_given_out=function(m,e,o,s,n){try{let p=x.__wbindgen_add_to_stack_pointer(-16),w=K(m,x.__wbindgen_malloc,x.__wbindgen_realloc),y=q,I=K(e,x.__wbindgen_malloc,x.__wbindgen_realloc),S=q,k=K(o,x.__wbindgen_malloc,x.__wbindgen_realloc),M=q,A=K(s,x.__wbindgen_malloc,x.__wbindgen_realloc),W=q,z=K(n,x.__wbindgen_malloc,x.__wbindgen_realloc),J=q;x.calculate_in_given_out(p,w,y,I,S,k,M,A,W,z,J);var l=ge()[p/4+0],u=ge()[p/4+1];return Re(l,u)}finally{x.__wbindgen_add_to_stack_pointer(16),x.__wbindgen_free(l,u)}};Ie.exports.calculate_linear_weights=function(m,e,o,s,n){try{let p=x.__wbindgen_add_to_stack_pointer(-16),w=K(m,x.__wbindgen_malloc,x.__wbindgen_realloc),y=q,I=K(e,x.__wbindgen_malloc,x.__wbindgen_realloc),S=q,k=K(o,x.__wbindgen_malloc,x.__wbindgen_realloc),M=q,A=K(s,x.__wbindgen_malloc,x.__wbindgen_realloc),W=q,z=K(n,x.__wbindgen_malloc,x.__wbindgen_realloc),J=q;x.calculate_linear_weights(p,w,y,I,S,k,M,A,W,z,J);var l=ge()[p/4+0],u=ge()[p/4+1];return Re(l,u)}finally{x.__wbindgen_add_to_stack_pointer(16),x.__wbindgen_free(l,u)}};var Rt=require("path").join(__dirname,"hydra_dx_wasm_bg.wasm"),Lt=require("fs").readFileSync(Rt),Dt=new WebAssembly.Module(Lt),Gt=new WebAssembly.Instance(Dt,Et);x=Gt.exports;Ie.exports.__wasm=x});var ht=lt((po,fe)=>{var Xt={},B,{TextEncoder:zt,TextDecoder:Kt}=require("util"),G=0,$e=null;function Ze(){return($e===null||$e.buffer!==B.memory.buffer)&&($e=new Uint8Array(B.memory.buffer)),$e}var je=new zt("utf-8"),Vt=typeof je.encodeInto=="function"?function(m,e){return je.encodeInto(m,e)}:function(m,e){let o=je.encode(m);return e.set(o),{read:m.length,written:o.length}};function C(m,e,o){if(o===void 0){let p=je.encode(m),w=e(p.length);return Ze().subarray(w,w+p.length).set(p),G=p.length,w}let s=m.length,n=e(s),l=Ze(),u=0;for(;u<s;u++){let p=m.charCodeAt(u);if(p>127)break;l[n+u]=p}if(u!==s){u!==0&&(m=m.slice(u)),n=o(n,s,s=u+m.length*3);let p=Ze().subarray(n+u,n+s);u+=Vt(m,p).written}return G=u,n}var Qe=null;function ee(){return(Qe===null||Qe.buffer!==B.memory.buffer)&&(Qe=new Int32Array(B.memory.buffer)),Qe}var _t=new Kt("utf-8",{ignoreBOM:!0,fatal:!0});_t.decode();function be(m,e){return _t.decode(Ze().subarray(m,m+e))}fe.exports.calculate_pool_trade_fee=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=C(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=G;B.calculate_pool_trade_fee(l,u,p,e,o);var s=ee()[l/4+0],n=ee()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};fe.exports.get_spot_price=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=C(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=G,w=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),y=G,I=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),S=G;B.get_spot_price(l,u,p,w,y,I,S);var s=ee()[l/4+0],n=ee()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};fe.exports.calculate_out_given_in=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=C(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=G,w=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),y=G,I=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),S=G;B.calculate_out_given_in(l,u,p,w,y,I,S);var s=ee()[l/4+0],n=ee()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};fe.exports.calculate_in_given_out=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=C(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=G,w=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),y=G,I=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),S=G;B.calculate_in_given_out(l,u,p,w,y,I,S);var s=ee()[l/4+0],n=ee()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};fe.exports.calculate_liquidity_in=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=C(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=G,w=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),y=G,I=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),S=G;B.calculate_liquidity_in(l,u,p,w,y,I,S);var s=ee()[l/4+0],n=ee()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};fe.exports.calculate_shares=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=C(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=G,w=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),y=G,I=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),S=G;B.calculate_shares(l,u,p,w,y,I,S);var s=ee()[l/4+0],n=ee()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};fe.exports.calculate_liquidity_out_asset_a=function(m,e,o,s){try{let u=B.__wbindgen_add_to_stack_pointer(-16),p=C(m,B.__wbindgen_malloc,B.__wbindgen_realloc),w=G,y=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),I=G,S=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),k=G,M=C(s,B.__wbindgen_malloc,B.__wbindgen_realloc),A=G;B.calculate_liquidity_out_asset_a(u,p,w,y,I,S,k,M,A);var n=ee()[u/4+0],l=ee()[u/4+1];return be(n,l)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(n,l)}};fe.exports.calculate_liquidity_out_asset_b=function(m,e,o,s){try{let u=B.__wbindgen_add_to_stack_pointer(-16),p=C(m,B.__wbindgen_malloc,B.__wbindgen_realloc),w=G,y=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),I=G,S=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),k=G,M=C(s,B.__wbindgen_malloc,B.__wbindgen_realloc),A=G;B.calculate_liquidity_out_asset_b(u,p,w,y,I,S,k,M,A);var n=ee()[u/4+0],l=ee()[u/4+1];return be(n,l)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(n,l)}};var Yt=require("path").join(__dirname,"hydra_dx_wasm_bg.wasm"),Jt=require("fs").readFileSync(Yt),$t=new WebAssembly.Module(Jt),Qt=new WebAssembly.Instance($t,Xt);B=Qt.exports;fe.exports.__wasm=B});var nn={};xt(nn,{BigNumber:()=>Se,DECIMAL_PLACES:()=>mt,INFINITY:()=>At,ONE:()=>pe,PolkadotApiPoolService:()=>Le,PoolError:()=>ot,PoolFactory:()=>xe,PoolType:()=>he,Router:()=>Ne,TradeRouter:()=>Ge,TradeType:()=>rt,XykPool:()=>_e,ZERO:()=>vt,bnum:()=>R,scale:()=>ne});module.exports=Nt(nn);var He=class{constructor(e=1/0){this.capacity=e;this.storage=[]}enqueue(e){if(this.size()===this.capacity)throw Error("Queue has reached max capacity, you cannot add more items");this.storage.push(e)}dequeue(){return this.storage.shift()}size(){return this.storage.length}};var Tt=4,Fe=class{isNotVisited(e,o){let s=!0;return o.forEach(n=>{n[0]===e&&(s=!1)}),s}findPaths(e,o,s){let n=[],l=new He,u=[];for(u.push([o,""]),l.enqueue(u);l.size()>0;){let p=l.dequeue();if(p==null||p.length>Tt)return n;let w=p[p.length-1];(s===null||w[0]===s)&&n.push(p),e[w[0]].forEach(I=>{if(this.isNotVisited(I[0],p)){let S=[...p];S.push(I),l.enqueue(S)}})}return n}buildAndPopulateGraph(e,o){let s=e[e.length-1],n=[];for(let l=0;l<=parseInt(s);l++)n.push([]);for(let[l,u,p]of o){let w=parseInt(u),y=parseInt(p);n[w].push([y,l])}return n}};function nt(m){let e={};for(let o of m){let s=o.tokens.length;for(let n=0;n<s;n++){e[o.tokens[n].id]||(e[o.tokens[n].id]=[]);for(let l=0;l<s;l++){if(n==l)continue;let u=[o.address,o.tokens[n].id,o.tokens[l].id];e[o.tokens[n].id].push(u)}}}return e}var Me=class{getProposals(e,o,s){let n=nt(s),l=Object.keys(n),u=l.map(I=>n[I]).flat(),p=new Fe,w=p.buildAndPopulateGraph(l,u),y=p.findPaths(w,parseInt(e),o?parseInt(o):null);return this.parsePaths(y)}parsePaths(e){let o=[];for(let s of e){let n=[];for(let l=0;l<s.length;l++){let u=s[l],p=s[l+1];if(p==null)break;n.push(this.toEdge(u,p))}o.push(n)}return o}toEdge(e,o){return[o[1],e[0].toString(),o[0].toString()]}};var he=(n=>(n.XYK="XYK",n.LBP="LBP",n.Stable="Stable",n.Omni="Omni",n))(he||{}),ot=(s=>(s.InsufficientTradingAmount="InsufficientTradingAmount",s.MaxInRatioExceeded="MaxInRatioExceeded",s.MaxOutRatioExceeded="MaxOutRatioExceeded",s))(ot||{}),rt=(o=>(o.Buy="Buy",o.Sell="Sell",o))(rt||{});var Be=class{constructor(e){this.api=e}getStorageKey(e,o){return e[0].toHuman()[o]}getStorageEntryArray(e){return e[1].toHuman()}async getPoolTokens(e,o){let s=o.map(async n=>{let l=await this.getAccountBalance(e,n),p=(await this.getAssetMetadata(n)).toHuman();return{id:n.toString(),balance:l,decimals:p.decimals,symbol:p.symbol}});return Promise.all(s)}async syncPoolTokens(e,o){let s=o.map(async n=>{let l=await this.getAccountBalance(e,n.id);return{...n,balance:l}});return Promise.all(s)}async getAssetMetadata(e){return await this.api.query.assetRegistry.assetMetadataMap(e)}async getAccountBalance(e,o){return o==="0"?await this.getSystemAccountBalance(e):await this.getTokenAccountBalance(e,o)}async getSystemAccountBalance(e){let{data:{free:o}}=await this.api.query.system.account(e);return o.toString()}async getTokenAccountBalance(e,o){let{free:s}=await this.api.query.tokens.accounts(e,o);return s.toString()}};var qe=class extends Be{constructor(){super(...arguments);this.pools=[];this._poolsLoaded=!1}async getPools(){return this._poolsLoaded?this.pools=await this.syncPools():(this.pools=await this.loadPools(),this._poolsLoaded=!0),this.pools}async loadPools(){let s=(await this.api.query.xyk.poolAssets.entries()).map(async n=>{let l=this.getStorageKey(n,0),u=this.getStorageEntryArray(n),p=await this.getPoolTokens(l,u),w=this.api.consts.xyk.maxInRatio.toJSON(),y=this.api.consts.xyk.maxOutRatio.toJSON(),I=this.api.consts.xyk.minTradingLimit.toJSON();return{address:l,type:"XYK",tradeFee:this.getTradeFee(),tokens:p,maxInRatio:w,maxOutRatio:y,minTradingLimit:I}});return Promise.all(s)}async syncPools(){let o=this.pools.map(async s=>({...s,tokens:await this.syncPoolTokens(s.address,s.tokens)}));return Promise.all(o)}getTradeFee(){return this.api.consts.xyk.getExchangeFee.toJSON()}};var St=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,it=Math.ceil,ue=Math.floor,oe="[BigNumber Error] ",pt=oe+"Number primitive has more than 15 significant digits: ",le=1e14,v=14,st=9007199254740991,at=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],Pe=1e7,Y=1e9;function gt(m){var e,o,s,n=O.prototype={constructor:O,toString:null,valueOf:null},l=new O(1),u=20,p=4,w=-7,y=21,I=-1e7,S=1e7,k=!1,M=1,A=0,W={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:"\xA0",suffix:""},z="0123456789abcdefghijklmnopqrstuvwxyz",J=!0;function O(t,r){var i,_,c,f,P,a,g,h,d=this;if(!(d instanceof O))return new O(t,r);if(r==null){if(t&&t._isBigNumber===!0){d.s=t.s,!t.c||t.e>S?d.c=d.e=null:t.e<I?d.c=[d.e=0]:(d.e=t.e,d.c=t.c.slice());return}if((a=typeof t=="number")&&t*0==0){if(d.s=1/t<0?(t=-t,-1):1,t===~~t){for(f=0,P=t;P>=10;P/=10,f++);f>S?d.c=d.e=null:(d.e=f,d.c=[t]);return}h=String(t)}else{if(!St.test(h=String(t)))return s(d,h,a);d.s=h.charCodeAt(0)==45?(h=h.slice(1),-1):1}(f=h.indexOf("."))>-1&&(h=h.replace(".","")),(P=h.search(/e/i))>0?(f<0&&(f=P),f+=+h.slice(P+1),h=h.substring(0,P)):f<0&&(f=h.length)}else{if(H(r,2,z.length,"Base"),r==10&&J)return d=new O(t),Z(d,u+d.e+1,p);if(h=String(t),a=typeof t=="number"){if(t*0!=0)return s(d,h,a,r);if(d.s=1/t<0?(h=h.slice(1),-1):1,O.DEBUG&&h.replace(/^0\.0*|\./,"").length>15)throw Error(pt+t)}else d.s=h.charCodeAt(0)===45?(h=h.slice(1),-1):1;for(i=z.slice(0,r),f=P=0,g=h.length;P<g;P++)if(i.indexOf(_=h.charAt(P))<0){if(_=="."){if(P>f){f=g;continue}}else if(!c&&(h==h.toUpperCase()&&(h=h.toLowerCase())||h==h.toLowerCase()&&(h=h.toUpperCase()))){c=!0,P=-1,f=0;continue}return s(d,String(t),a,r)}a=!1,h=o(h,r,10,d.s),(f=h.indexOf("."))>-1?h=h.replace(".",""):f=h.length}for(P=0;h.charCodeAt(P)===48;P++);for(g=h.length;h.charCodeAt(--g)===48;);if(h=h.slice(P,++g)){if(g-=P,a&&O.DEBUG&&g>15&&(t>st||t!==ue(t)))throw Error(pt+d.s*t);if((f=f-P-1)>S)d.c=d.e=null;else if(f<I)d.c=[d.e=0];else{if(d.e=f,d.c=[],P=(f+1)%v,f<0&&(P+=v),P<g){for(P&&d.c.push(+h.slice(0,P)),g-=v;P<g;)d.c.push(+h.slice(P,P+=v));P=v-(h=h.slice(P)).length}else P-=g;for(;P--;h+="0");d.c.push(+h)}}else d.c=[d.e=0]}O.clone=gt,O.ROUND_UP=0,O.ROUND_DOWN=1,O.ROUND_CEIL=2,O.ROUND_FLOOR=3,O.ROUND_HALF_UP=4,O.ROUND_HALF_DOWN=5,O.ROUND_HALF_EVEN=6,O.ROUND_HALF_CEIL=7,O.ROUND_HALF_FLOOR=8,O.EUCLID=9,O.config=O.set=function(t){var r,i;if(t!=null)if(typeof t=="object"){if(t.hasOwnProperty(r="DECIMAL_PLACES")&&(i=t[r],H(i,0,Y,r),u=i),t.hasOwnProperty(r="ROUNDING_MODE")&&(i=t[r],H(i,0,8,r),p=i),t.hasOwnProperty(r="EXPONENTIAL_AT")&&(i=t[r],i&&i.pop?(H(i[0],-Y,0,r),H(i[1],0,Y,r),w=i[0],y=i[1]):(H(i,-Y,Y,r),w=-(y=i<0?-i:i))),t.hasOwnProperty(r="RANGE"))if(i=t[r],i&&i.pop)H(i[0],-Y,-1,r),H(i[1],1,Y,r),I=i[0],S=i[1];else if(H(i,-Y,Y,r),i)I=-(S=i<0?-i:i);else throw Error(oe+r+" cannot be zero: "+i);if(t.hasOwnProperty(r="CRYPTO"))if(i=t[r],i===!!i)if(i)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))k=i;else throw k=!i,Error(oe+"crypto unavailable");else k=i;else throw Error(oe+r+" not true or false: "+i);if(t.hasOwnProperty(r="MODULO_MODE")&&(i=t[r],H(i,0,9,r),M=i),t.hasOwnProperty(r="POW_PRECISION")&&(i=t[r],H(i,0,Y,r),A=i),t.hasOwnProperty(r="FORMAT"))if(i=t[r],typeof i=="object")W=i;else throw Error(oe+r+" not an object: "+i);if(t.hasOwnProperty(r="ALPHABET"))if(i=t[r],typeof i=="string"&&!/^.?$|[+\-.\s]|(.).*\1/.test(i))J=i.slice(0,10)=="0123456789",z=i;else throw Error(oe+r+" invalid: "+i)}else throw Error(oe+"Object expected: "+t);return{DECIMAL_PLACES:u,ROUNDING_MODE:p,EXPONENTIAL_AT:[w,y],RANGE:[I,S],CRYPTO:k,MODULO_MODE:M,POW_PRECISION:A,FORMAT:W,ALPHABET:z}},O.isBigNumber=function(t){if(!t||t._isBigNumber!==!0)return!1;if(!O.DEBUG)return!0;var r,i,_=t.c,c=t.e,f=t.s;e:if({}.toString.call(_)=="[object Array]"){if((f===1||f===-1)&&c>=-Y&&c<=Y&&c===ue(c)){if(_[0]===0){if(c===0&&_.length===1)return!0;break e}if(r=(c+1)%v,r<1&&(r+=v),String(_[0]).length==r){for(r=0;r<_.length;r++)if(i=_[r],i<0||i>=le||i!==ue(i))break e;if(i!==0)return!0}}}else if(_===null&&c===null&&(f===null||f===1||f===-1))return!0;throw Error(oe+"Invalid BigNumber: "+t)},O.maximum=O.max=function(){return L(arguments,n.lt)},O.minimum=O.min=function(){return L(arguments,n.gt)},O.random=function(){var t=9007199254740992,r=Math.random()*t&2097151?function(){return ue(Math.random()*t)}:function(){return(Math.random()*1073741824|0)*8388608+(Math.random()*8388608|0)};return function(i){var _,c,f,P,a,g=0,h=[],d=new O(l);if(i==null?i=u:H(i,0,Y),P=it(i/v),k)if(crypto.getRandomValues){for(_=crypto.getRandomValues(new Uint32Array(P*=2));g<P;)a=_[g]*131072+(_[g+1]>>>11),a>=9e15?(c=crypto.getRandomValues(new Uint32Array(2)),_[g]=c[0],_[g+1]=c[1]):(h.push(a%1e14),g+=2);g=P/2}else if(crypto.randomBytes){for(_=crypto.randomBytes(P*=7);g<P;)a=(_[g]&31)*281474976710656+_[g+1]*1099511627776+_[g+2]*4294967296+_[g+3]*16777216+(_[g+4]<<16)+(_[g+5]<<8)+_[g+6],a>=9e15?crypto.randomBytes(7).copy(_,g):(h.push(a%1e14),g+=7);g=P/7}else throw k=!1,Error(oe+"crypto unavailable");if(!k)for(;g<P;)a=r(),a<9e15&&(h[g++]=a%1e14);for(P=h[--g],i%=v,P&&i&&(a=at[v-i],h[g]=ue(P/a)*a);h[g]===0;h.pop(),g--);if(g<0)h=[f=0];else{for(f=-1;h[0]===0;h.splice(0,1),f-=v);for(g=1,a=h[0];a>=10;a/=10,g++);g<v&&(f-=v-g)}return d.e=f,d.c=h,d}}(),O.sum=function(){for(var t=1,r=arguments,i=new O(r[0]);t<r.length;)i=i.plus(r[t++]);return i},o=function(){var t="0123456789";function r(i,_,c,f){for(var P,a=[0],g,h=0,d=i.length;h<d;){for(g=a.length;g--;a[g]*=_);for(a[0]+=f.indexOf(i.charAt(h++)),P=0;P<a.length;P++)a[P]>c-1&&(a[P+1]==null&&(a[P+1]=0),a[P+1]+=a[P]/c|0,a[P]%=c)}return a.reverse()}return function(i,_,c,f,P){var a,g,h,d,b,N,T,F,U=i.indexOf("."),V=u,E=p;for(U>=0&&(d=A,A=0,i=i.replace(".",""),F=new O(_),N=F.pow(i.length-U),A=d,F.c=r(de(se(N.c),N.e,"0"),10,c,t),F.e=F.c.length),T=r(i,_,c,P?(a=z,t):(a=t,z)),h=d=T.length;T[--d]==0;T.pop());if(!T[0])return a.charAt(0);if(U<0?--h:(N.c=T,N.e=h,N.s=f,N=e(N,F,V,E,c),T=N.c,b=N.r,h=N.e),g=h+V+1,U=T[g],d=c/2,b=b||g<0||T[g+1]!=null,b=E<4?(U!=null||b)&&(E==0||E==(N.s<0?3:2)):U>d||U==d&&(E==4||b||E==6&&T[g-1]&1||E==(N.s<0?8:7)),g<1||!T[0])i=b?de(a.charAt(1),-V,a.charAt(0)):a.charAt(0);else{if(T.length=g,b)for(--c;++T[--g]>c;)T[g]=0,g||(++h,T=[1].concat(T));for(d=T.length;!T[--d];);for(U=0,i="";U<=d;i+=a.charAt(T[U++]));i=de(i,h,a.charAt(0))}return i}}(),e=function(){function t(_,c,f){var P,a,g,h,d=0,b=_.length,N=c%Pe,T=c/Pe|0;for(_=_.slice();b--;)g=_[b]%Pe,h=_[b]/Pe|0,P=T*g+h*N,a=N*g+P%Pe*Pe+d,d=(a/f|0)+(P/Pe|0)+T*h,_[b]=a%f;return d&&(_=[d].concat(_)),_}function r(_,c,f,P){var a,g;if(f!=P)g=f>P?1:-1;else for(a=g=0;a<f;a++)if(_[a]!=c[a]){g=_[a]>c[a]?1:-1;break}return g}function i(_,c,f,P){for(var a=0;f--;)_[f]-=a,a=_[f]<c[f]?1:0,_[f]=a*P+_[f]-c[f];for(;!_[0]&&_.length>1;_.splice(0,1));}return function(_,c,f,P,a){var g,h,d,b,N,T,F,U,V,E,D,j,Ce,et,tt,ce,ke,ie=_.s==c.s?1:-1,te=_.c,X=c.c;if(!te||!te[0]||!X||!X[0])return new O(!_.s||!c.s||(te?X&&te[0]==X[0]:!X)?NaN:te&&te[0]==0||!X?ie*0:ie/0);for(U=new O(ie),V=U.c=[],h=_.e-c.e,ie=f+h+1,a||(a=le,h=ae(_.e/v)-ae(c.e/v),ie=ie/v|0),d=0;X[d]==(te[d]||0);d++);if(X[d]>(te[d]||0)&&h--,ie<0)V.push(1),b=!0;else{for(et=te.length,ce=X.length,d=0,ie+=2,N=ue(a/(X[0]+1)),N>1&&(X=t(X,N,a),te=t(te,N,a),ce=X.length,et=te.length),Ce=ce,E=te.slice(0,ce),D=E.length;D<ce;E[D++]=0);ke=X.slice(),ke=[0].concat(ke),tt=X[0],X[1]>=a/2&&tt++;do{if(N=0,g=r(X,E,ce,D),g<0){if(j=E[0],ce!=D&&(j=j*a+(E[1]||0)),N=ue(j/tt),N>1)for(N>=a&&(N=a-1),T=t(X,N,a),F=T.length,D=E.length;r(T,E,F,D)==1;)N--,i(T,ce<F?ke:X,F,a),F=T.length,g=1;else N==0&&(g=N=1),T=X.slice(),F=T.length;if(F<D&&(T=[0].concat(T)),i(E,T,D,a),D=E.length,g==-1)for(;r(X,E,ce,D)<1;)N++,i(E,ce<D?ke:X,D,a),D=E.length}else g===0&&(N++,E=[0]);V[d++]=N,E[0]?E[D++]=te[Ce]||0:(E=[te[Ce]],D=1)}while((Ce++<et||E[0]!=null)&&ie--);b=E[0]!=null,V[0]||V.splice(0,1)}if(a==le){for(d=1,ie=V[0];ie>=10;ie/=10,d++);Z(U,f+(U.e=d+h*v-1)+1,P,b)}else U.e=h,U.r=+b;return U}}();function Oe(t,r,i,_){var c,f,P,a,g;if(i==null?i=p:H(i,0,8),!t.c)return t.toString();if(c=t.c[0],P=t.e,r==null)g=se(t.c),g=_==1||_==2&&(P<=w||P>=y)?Xe(g,P):de(g,P,"0");else if(t=Z(new O(t),r,i),f=t.e,g=se(t.c),a=g.length,_==1||_==2&&(r<=f||f<=w)){for(;a<r;g+="0",a++);g=Xe(g,f)}else if(r-=P,g=de(g,f,"0"),f+1>a){if(--r>0)for(g+=".";r--;g+="0");}else if(r+=f-a,r>0)for(f+1==a&&(g+=".");r--;g+="0");return t.s<0&&c?"-"+g:g}function L(t,r){for(var i,_=1,c=new O(t[0]);_<t.length;_++)if(i=new O(t[_]),i.s)r.call(c,i)&&(c=i);else{c=i;break}return c}function Q(t,r,i){for(var _=1,c=r.length;!r[--c];r.pop());for(c=r[0];c>=10;c/=10,_++);return(i=_+i*v-1)>S?t.c=t.e=null:i<I?t.c=[t.e=0]:(t.e=i,t.c=r),t}s=function(){var t=/^(-?)0([xbo])(?=\w[\w.]*$)/i,r=/^([^.]+)\.$/,i=/^\.([^.]+)$/,_=/^-?(Infinity|NaN)$/,c=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(f,P,a,g){var h,d=a?P:P.replace(c,"");if(_.test(d))f.s=isNaN(d)?null:d<0?-1:1;else{if(!a&&(d=d.replace(t,function(b,N,T){return h=(T=T.toLowerCase())=="x"?16:T=="b"?2:8,!g||g==h?N:b}),g&&(h=g,d=d.replace(r,"$1").replace(i,"0.$1")),P!=d))return new O(d,h);if(O.DEBUG)throw Error(oe+"Not a"+(g?" base "+g:"")+" number: "+P);f.s=null}f.c=f.e=null}}();function Z(t,r,i,_){var c,f,P,a,g,h,d,b=t.c,N=at;if(b){e:{for(c=1,a=b[0];a>=10;a/=10,c++);if(f=r-c,f<0)f+=v,P=r,g=b[h=0],d=g/N[c-P-1]%10|0;else if(h=it((f+1)/v),h>=b.length)if(_){for(;b.length<=h;b.push(0));g=d=0,c=1,f%=v,P=f-v+1}else break e;else{for(g=a=b[h],c=1;a>=10;a/=10,c++);f%=v,P=f-v+c,d=P<0?0:g/N[c-P-1]%10|0}if(_=_||r<0||b[h+1]!=null||(P<0?g:g%N[c-P-1]),_=i<4?(d||_)&&(i==0||i==(t.s<0?3:2)):d>5||d==5&&(i==4||_||i==6&&(f>0?P>0?g/N[c-P]:0:b[h-1])%10&1||i==(t.s<0?8:7)),r<1||!b[0])return b.length=0,_?(r-=t.e+1,b[0]=N[(v-r%v)%v],t.e=-r||0):b[0]=t.e=0,t;if(f==0?(b.length=h,a=1,h--):(b.length=h+1,a=N[v-f],b[h]=P>0?ue(g/N[c-P]%N[P])*a:0),_)for(;;)if(h==0){for(f=1,P=b[0];P>=10;P/=10,f++);for(P=b[0]+=a,a=1;P>=10;P/=10,a++);f!=a&&(t.e++,b[0]==le&&(b[0]=1));break}else{if(b[h]+=a,b[h]!=le)break;b[h--]=0,a=1}for(f=b.length;b[--f]===0;b.pop());}t.e>S?t.c=t.e=null:t.e<I&&(t.c=[t.e=0])}return t}function re(t){var r,i=t.e;return i===null?t.toString():(r=se(t.c),r=i<=w||i>=y?Xe(r,i):de(r,i,"0"),t.s<0?"-"+r:r)}return n.absoluteValue=n.abs=function(){var t=new O(this);return t.s<0&&(t.s=1),t},n.comparedTo=function(t,r){return Te(this,new O(t,r))},n.decimalPlaces=n.dp=function(t,r){var i,_,c,f=this;if(t!=null)return H(t,0,Y),r==null?r=p:H(r,0,8),Z(new O(f),t+f.e+1,r);if(!(i=f.c))return null;if(_=((c=i.length-1)-ae(this.e/v))*v,c=i[c])for(;c%10==0;c/=10,_--);return _<0&&(_=0),_},n.dividedBy=n.div=function(t,r){return e(this,new O(t,r),u,p)},n.dividedToIntegerBy=n.idiv=function(t,r){return e(this,new O(t,r),0,1)},n.exponentiatedBy=n.pow=function(t,r){var i,_,c,f,P,a,g,h,d,b=this;if(t=new O(t),t.c&&!t.isInteger())throw Error(oe+"Exponent not an integer: "+re(t));if(r!=null&&(r=new O(r)),a=t.e>14,!b.c||!b.c[0]||b.c[0]==1&&!b.e&&b.c.length==1||!t.c||!t.c[0])return d=new O(Math.pow(+re(b),a?2-Ue(t):+re(t))),r?d.mod(r):d;if(g=t.s<0,r){if(r.c?!r.c[0]:!r.s)return new O(NaN);_=!g&&b.isInteger()&&r.isInteger(),_&&(b=b.mod(r))}else{if(t.e>9&&(b.e>0||b.e<-1||(b.e==0?b.c[0]>1||a&&b.c[1]>=24e7:b.c[0]<8e13||a&&b.c[0]<=9999975e7)))return f=b.s<0&&Ue(t)?-0:0,b.e>-1&&(f=1/f),new O(g?1/f:f);A&&(f=it(A/v+2))}for(a?(i=new O(.5),g&&(t.s=1),h=Ue(t)):(c=Math.abs(+re(t)),h=c%2),d=new O(l);;){if(h){if(d=d.times(b),!d.c)break;f?d.c.length>f&&(d.c.length=f):_&&(d=d.mod(r))}if(c){if(c=ue(c/2),c===0)break;h=c%2}else if(t=t.times(i),Z(t,t.e+1,1),t.e>14)h=Ue(t);else{if(c=+re(t),c===0)break;h=c%2}b=b.times(b),f?b.c&&b.c.length>f&&(b.c.length=f):_&&(b=b.mod(r))}return _?d:(g&&(d=l.div(d)),r?d.mod(r):f?Z(d,A,p,P):d)},n.integerValue=function(t){var r=new O(this);return t==null?t=p:H(t,0,8),Z(r,r.e+1,t)},n.isEqualTo=n.eq=function(t,r){return Te(this,new O(t,r))===0},n.isFinite=function(){return!!this.c},n.isGreaterThan=n.gt=function(t,r){return Te(this,new O(t,r))>0},n.isGreaterThanOrEqualTo=n.gte=function(t,r){return(r=Te(this,new O(t,r)))===1||r===0},n.isInteger=function(){return!!this.c&&ae(this.e/v)>this.c.length-2},n.isLessThan=n.lt=function(t,r){return Te(this,new O(t,r))<0},n.isLessThanOrEqualTo=n.lte=function(t,r){return(r=Te(this,new O(t,r)))===-1||r===0},n.isNaN=function(){return!this.s},n.isNegative=function(){return this.s<0},n.isPositive=function(){return this.s>0},n.isZero=function(){return!!this.c&&this.c[0]==0},n.minus=function(t,r){var i,_,c,f,P=this,a=P.s;if(t=new O(t,r),r=t.s,!a||!r)return new O(NaN);if(a!=r)return t.s=-r,P.plus(t);var g=P.e/v,h=t.e/v,d=P.c,b=t.c;if(!g||!h){if(!d||!b)return d?(t.s=-r,t):new O(b?P:NaN);if(!d[0]||!b[0])return b[0]?(t.s=-r,t):new O(d[0]?P:p==3?-0:0)}if(g=ae(g),h=ae(h),d=d.slice(),a=g-h){for((f=a<0)?(a=-a,c=d):(h=g,c=b),c.reverse(),r=a;r--;c.push(0));c.reverse()}else for(_=(f=(a=d.length)<(r=b.length))?a:r,a=r=0;r<_;r++)if(d[r]!=b[r]){f=d[r]<b[r];break}if(f&&(c=d,d=b,b=c,t.s=-t.s),r=(_=b.length)-(i=d.length),r>0)for(;r--;d[i++]=0);for(r=le-1;_>a;){if(d[--_]<b[_]){for(i=_;i&&!d[--i];d[i]=r);--d[i],d[_]+=le}d[_]-=b[_]}for(;d[0]==0;d.splice(0,1),--h);return d[0]?Q(t,d,h):(t.s=p==3?-1:1,t.c=[t.e=0],t)},n.modulo=n.mod=function(t,r){var i,_,c=this;return t=new O(t,r),!c.c||!t.s||t.c&&!t.c[0]?new O(NaN):!t.c||c.c&&!c.c[0]?new O(c):(M==9?(_=t.s,t.s=1,i=e(c,t,0,3),t.s=_,i.s*=_):i=e(c,t,0,M),t=c.minus(i.times(t)),!t.c[0]&&M==1&&(t.s=c.s),t)},n.multipliedBy=n.times=function(t,r){var i,_,c,f,P,a,g,h,d,b,N,T,F,U,V,E=this,D=E.c,j=(t=new O(t,r)).c;if(!D||!j||!D[0]||!j[0])return!E.s||!t.s||D&&!D[0]&&!j||j&&!j[0]&&!D?t.c=t.e=t.s=null:(t.s*=E.s,!D||!j?t.c=t.e=null:(t.c=[0],t.e=0)),t;for(_=ae(E.e/v)+ae(t.e/v),t.s*=E.s,g=D.length,b=j.length,g<b&&(F=D,D=j,j=F,c=g,g=b,b=c),c=g+b,F=[];c--;F.push(0));for(U=le,V=Pe,c=b;--c>=0;){for(i=0,N=j[c]%V,T=j[c]/V|0,P=g,f=c+P;f>c;)h=D[--P]%V,d=D[P]/V|0,a=T*h+d*N,h=N*h+a%V*V+F[f]+i,i=(h/U|0)+(a/V|0)+T*d,F[f--]=h%U;F[f]=i}return i?++_:F.splice(0,1),Q(t,F,_)},n.negated=function(){var t=new O(this);return t.s=-t.s||null,t},n.plus=function(t,r){var i,_=this,c=_.s;if(t=new O(t,r),r=t.s,!c||!r)return new O(NaN);if(c!=r)return t.s=-r,_.minus(t);var f=_.e/v,P=t.e/v,a=_.c,g=t.c;if(!f||!P){if(!a||!g)return new O(c/0);if(!a[0]||!g[0])return g[0]?t:new O(a[0]?_:c*0)}if(f=ae(f),P=ae(P),a=a.slice(),c=f-P){for(c>0?(P=f,i=g):(c=-c,i=a),i.reverse();c--;i.push(0));i.reverse()}for(c=a.length,r=g.length,c-r<0&&(i=g,g=a,a=i,r=c),c=0;r;)c=(a[--r]=a[r]+g[r]+c)/le|0,a[r]=le===a[r]?0:a[r]%le;return c&&(a=[c].concat(a),++P),Q(t,a,P)},n.precision=n.sd=function(t,r){var i,_,c,f=this;if(t!=null&&t!==!!t)return H(t,1,Y),r==null?r=p:H(r,0,8),Z(new O(f),t,r);if(!(i=f.c))return null;if(c=i.length-1,_=c*v+1,c=i[c]){for(;c%10==0;c/=10,_--);for(c=i[0];c>=10;c/=10,_++);}return t&&f.e+1>_&&(_=f.e+1),_},n.shiftedBy=function(t){return H(t,-st,st),this.times("1e"+t)},n.squareRoot=n.sqrt=function(){var t,r,i,_,c,f=this,P=f.c,a=f.s,g=f.e,h=u+4,d=new O("0.5");if(a!==1||!P||!P[0])return new O(!a||a<0&&(!P||P[0])?NaN:P?f:1/0);if(a=Math.sqrt(+re(f)),a==0||a==1/0?(r=se(P),(r.length+g)%2==0&&(r+="0"),a=Math.sqrt(+r),g=ae((g+1)/2)-(g<0||g%2),a==1/0?r="5e"+g:(r=a.toExponential(),r=r.slice(0,r.indexOf("e")+1)+g),i=new O(r)):i=new O(a+""),i.c[0]){for(g=i.e,a=g+h,a<3&&(a=0);;)if(c=i,i=d.times(c.plus(e(f,c,h,1))),se(c.c).slice(0,a)===(r=se(i.c)).slice(0,a))if(i.e<g&&--a,r=r.slice(a-3,a+1),r=="9999"||!_&&r=="4999"){if(!_&&(Z(c,c.e+u+2,0),c.times(c).eq(f))){i=c;break}h+=4,a+=4,_=1}else{(!+r||!+r.slice(1)&&r.charAt(0)=="5")&&(Z(i,i.e+u+2,1),t=!i.times(i).eq(f));break}}return Z(i,i.e+u+1,p,t)},n.toExponential=function(t,r){return t!=null&&(H(t,0,Y),t++),Oe(this,t,r,1)},n.toFixed=function(t,r){return t!=null&&(H(t,0,Y),t=t+this.e+1),Oe(this,t,r)},n.toFormat=function(t,r,i){var _,c=this;if(i==null)t!=null&&r&&typeof r=="object"?(i=r,r=null):t&&typeof t=="object"?(i=t,t=r=null):i=W;else if(typeof i!="object")throw Error(oe+"Argument not an object: "+i);if(_=c.toFixed(t,r),c.c){var f,P=_.split("."),a=+i.groupSize,g=+i.secondaryGroupSize,h=i.groupSeparator||"",d=P[0],b=P[1],N=c.s<0,T=N?d.slice(1):d,F=T.length;if(g&&(f=a,a=g,g=f,F-=f),a>0&&F>0){for(f=F%a||a,d=T.substr(0,f);f<F;f+=a)d+=h+T.substr(f,a);g>0&&(d+=h+T.slice(f)),N&&(d="-"+d)}_=b?d+(i.decimalSeparator||"")+((g=+i.fractionGroupSize)?b.replace(new RegExp("\\d{"+g+"}\\B","g"),"$&"+(i.fractionGroupSeparator||"")):b):d}return(i.prefix||"")+_+(i.suffix||"")},n.toFraction=function(t){var r,i,_,c,f,P,a,g,h,d,b,N,T=this,F=T.c;if(t!=null&&(a=new O(t),!a.isInteger()&&(a.c||a.s!==1)||a.lt(l)))throw Error(oe+"Argument "+(a.isInteger()?"out of range: ":"not an integer: ")+re(a));if(!F)return new O(T);for(r=new O(l),h=i=new O(l),_=g=new O(l),N=se(F),f=r.e=N.length-T.e-1,r.c[0]=at[(P=f%v)<0?v+P:P],t=!t||a.comparedTo(r)>0?f>0?r:h:a,P=S,S=1/0,a=new O(N),g.c[0]=0;d=e(a,r,0,1),c=i.plus(d.times(_)),c.comparedTo(t)!=1;)i=_,_=c,h=g.plus(d.times(c=h)),g=c,r=a.minus(d.times(c=r)),a=c;return c=e(t.minus(i),_,0,1),g=g.plus(c.times(h)),i=i.plus(c.times(_)),g.s=h.s=T.s,f=f*2,b=e(h,_,f,p).minus(T).abs().comparedTo(e(g,i,f,p).minus(T).abs())<1?[h,_]:[g,i],S=P,b},n.toNumber=function(){return+re(this)},n.toPrecision=function(t,r){return t!=null&&H(t,1,Y),Oe(this,t,r,2)},n.toString=function(t){var r,i=this,_=i.s,c=i.e;return c===null?_?(r="Infinity",_<0&&(r="-"+r)):r="NaN":(t==null?r=c<=w||c>=y?Xe(se(i.c),c):de(se(i.c),c,"0"):t===10&&J?(i=Z(new O(i),u+c+1,p),r=de(se(i.c),i.e,"0")):(H(t,2,z.length,"Base"),r=o(de(se(i.c),c,"0"),10,t,_,!0)),_<0&&i.c[0]&&(r="-"+r)),r},n.valueOf=n.toJSON=function(){return re(this)},n._isBigNumber=!0,n[Symbol.toStringTag]="BigNumber",n[Symbol.for("nodejs.util.inspect.custom")]=n.valueOf,m!=null&&O.set(m),O}function ae(m){var e=m|0;return m>0||m===e?e:e-1}function se(m){for(var e,o,s=1,n=m.length,l=m[0]+"";s<n;){for(e=m[s++]+"",o=v-e.length;o--;e="0"+e);l+=e}for(n=l.length;l.charCodeAt(--n)===48;);return l.slice(0,n+1||1)}function Te(m,e){var o,s,n=m.c,l=e.c,u=m.s,p=e.s,w=m.e,y=e.e;if(!u||!p)return null;if(o=n&&!n[0],s=l&&!l[0],o||s)return o?s?0:-p:u;if(u!=p)return u;if(o=u<0,s=w==y,!n||!l)return s?0:!n^o?1:-1;if(!s)return w>y^o?1:-1;for(p=(w=n.length)<(y=l.length)?w:y,u=0;u<p;u++)if(n[u]!=l[u])return n[u]>l[u]^o?1:-1;return w==y?0:w>y^o?1:-1}function H(m,e,o,s){if(m<e||m>o||m!==ue(m))throw Error(oe+(s||"Argument")+(typeof m=="number"?m<e||m>o?" out of range: ":" not an integer: ":" not a primitive number: ")+String(m))}function Ue(m){var e=m.c.length-1;return ae(m.e/v)==e&&m.c[e]%2!=0}function Xe(m,e){return(m.length>1?m.charAt(0)+"."+m.slice(1):m)+(e<0?"e":"e+")+e}function de(m,e,o){var s,n;if(e<0){for(n=o+".";++e;n+=o);m=n+m}else if(s=m.length,++e>s){for(n=o,e-=s;--e;n+=o);m+=n}else e<s&&(m=m.slice(0,e)+"."+m.slice(e));return m}var Se=gt();var mt=12;Se.config({EXPONENTIAL_AT:[-100,100],ROUNDING_MODE:1,DECIMAL_PLACES:mt});var vt=R(0),pe=R(1),At=R("Infinity");function ne(m,e){let o=new Se(e.toString()),s=new Se(10).pow(o);return m.times(s)}function R(m){return new Se(m.toString())}var me=ut(dt());function Ct(m,e,o,s,n){return(0,me.get_spot_price)(m,e,o,s,n)}function Wt(m,e,o,s,n){return(0,me.calculate_in_given_out)(m,e,o,s,n)}function Ht(m,e,o,s,n){return(0,me.calculate_out_given_in)(m,e,o,s,n)}function qt(m,e,o,s,n){return(0,me.calculate_linear_weights)(m,e,o,s,n)}function Ut(m,e,o){return(0,me.calculate_pool_trade_fee)(m,e,o)}var we={getSpotPrice:Ct,calculateInGivenOut:Wt,calculateOutGivenIn:Ht,calculateLinearWeights:qt,calculatePoolTradeFee:Ut};var Je=class extends Be{constructor(){super(...arguments);this.MAX_FINAL_WEIGHT=ne(R(100),6);this.poolsData=new Map([]);this.pools=[];this._poolsLoaded=!1}async getPools(){return this._poolsLoaded?this.pools=await this.syncPools():(this.pools=await this.loadPools(),this._poolsLoaded=!0),this.pools}async loadPools(){let s=(await this.api.query.lbp.poolData.entries()).map(async n=>{let l=this.getStorageKey(n,0),u=n[1].toJSON();this.poolsData.set(l,u);let p=await this.getPoolTokens(l,u.assets),w=this.api.consts.lbp.maxInRatio.toJSON(),y=this.api.consts.lbp.maxOutRatio.toJSON(),I=this.api.consts.lbp.minTradingLimit.toJSON(),S=await this.getLinearWeight(u),k=R(S),M=this.MAX_FINAL_WEIGHT.minus(R(k)),A=p[0].id;return{address:l,type:"LBP",tradeFee:u.fee,repayFee:this.getRepayFee(),repayFeeApply:await this.isRepayFeeApplied(A,u),tokens:[{...p[0],weight:k},{...p[1],weight:M}],maxInRatio:w,maxOutRatio:y,minTradingLimit:I}});return Promise.all(s)}async syncPools(){let o=this.pools.map(async s=>{let n=this.poolsData.get(s.address),l=await this.syncPoolTokens(s.address,s.tokens),u=await this.getLinearWeight(n),p=R(u),w=this.MAX_FINAL_WEIGHT.minus(R(p)),y=l[0].id;return{...s,repayFeeApply:await this.isRepayFeeApplied(y,n),tokens:[{...l[0],weight:p},{...l[1],weight:w}]}});return Promise.all(o)}async getLinearWeight(o){let n=(await this.api.query.parachainSystem.validationData()).toJSON();return we.calculateLinearWeights(o.start.toString(),o.end.toString(),o.initialWeight.toString(),o.finalWeight.toString(),n.relayParentNumber.toString())}getRepayFee(){return this.api.consts.lbp.repayFee.toJSON()}async isRepayFeeApplied(o,s){let n=R(s.repayTarget);try{let l=await this.getAccountBalance(o,s.feeCollector);return R(l).isLessThan(n)}catch{return!0}}};var Le=class{constructor(e){this.api=e,this.xykClient=new qe(this.api),this.lbpClient=new Je(this.api)}async getPools(e){if(e.length==0)return(await Promise.all([this.xykClient.getPools(),this.lbpClient.getPools()])).flat();let o=[];return e.forEach(n=>{switch(n){case"XYK":o.push(this.xykClient.getPools());break;case"LBP":o.push(this.lbpClient.getPools());break}}),(await Promise.all(o)).flat()}buildBuyTx(e,o,s,n,l){let u=this.api.tx.router.buy(e,o,s.toFixed(),n.toFixed(),l),p=()=>u;return{hex:u.toHex(),name:"RouterBuy",get:p}}buildSellTx(e,o,s,n,l){let u=this.api.tx.router.sell(e,o,s.toFixed(),n.toFixed(),l),p=()=>u;return{hex:u.toHex(),name:"RouterSell",get:p}}};function $(m,e){return m.shiftedBy(-1*e).toString()}function ve(m){return m[0]/m[1]*100}var Ae=class{static fromPool(e){if(!e.repayFee)throw new Error("LBP Pool missing repayFee");if(!e.repayFeeApply)throw new Error("LBP Pool missing repayFeeApply");return new Ae(e.address,e.tradeFee,e.tokens,e.maxInRatio,e.maxOutRatio,e.minTradingLimit,e.repayFee,e.repayFeeApply)}constructor(e,o,s,n,l,u,p,w){this.type="LBP",this.address=e,this.tradeFee=o,this.tokens=s,this.maxInRatio=n,this.maxOutRatio=l,this.minTradingLimit=u,this.repayFee=p,this.repayFeeApply=w}validPair(e,o){return!0}parsePoolPair(e,o){let s=new Map(this.tokens.map(w=>[w.id,w])),n=s.get(e),l=s.get(o);if(n==null)throw new Error("Pool does not contain tokenIn");if(l==null)throw new Error("Pool does not contain tokenOut");let u=R(n.balance),p=R(l.balance);return{assetIn:e,assetOut:o,decimalsIn:n.decimals,decimalsOut:l.decimals,weightIn:n.weight,weightOut:l.weight,balanceIn:u,balanceOut:p}}validateBuy(e,o){if(this.tokens[0].id===e.assetOut){let n=this.calculateTradeFee(o),l=o.plus(n),u=this.calculateInGivenOut(e,l),p=ve(this.repayFeeApply?this.repayFee:this.tradeFee);return{amountIn:u,calculatedIn:u,amountOut:o,feePct:p}}else{let n=this.calculateInGivenOut(e,o);return{amountIn:n,calculatedIn:n,amountOut:o,feePct:0}}}validateSell(e,o){if(this.tokens[0].id===e.assetIn){let n=this.calculateOutGivenIn(e,o);return{amountIn:o,calculatedOut:n,amountOut:n,feePct:0}}else{let n=this.calculateOutGivenIn(e,o),l=this.calculateTradeFee(n),u=n.minus(l),p=ve(this.repayFeeApply?this.repayFee:this.tradeFee);return{amountIn:o,calculatedOut:n,amountOut:u,feePct:p}}}calculateInGivenOut(e,o){let s=we.calculateInGivenOut(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),o.toString());return R(s)}calculateOutGivenIn(e,o){let s=we.calculateOutGivenIn(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),o.toString());return R(s)}spotPriceInGivenOut(e){let o=we.getSpotPrice(e.balanceOut.toString(),e.balanceIn.toString(),e.weightOut.toString(),e.weightIn.toString(),ne(pe,e.decimalsOut).toString());return R(o)}spotPriceOutGivenIn(e){let o=we.getSpotPrice(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),ne(pe,e.decimalsIn).toString());return R(o)}calculateTradeFee(e){let o=we.calculatePoolTradeFee(e.toString(),this.repayFeeApply?this.repayFee[0]:this.tradeFee[0],this.repayFeeApply?this.repayFee[1]:this.tradeFee[1]);return R(o)}};var ye=ut(ht());function Zt(m,e,o){return(0,ye.get_spot_price)(m,e,o)}function jt(m,e,o){return(0,ye.calculate_in_given_out)(m,e,o)}function en(m,e,o){return(0,ye.calculate_out_given_in)(m,e,o)}function tn(m,e,o){return(0,ye.calculate_pool_trade_fee)(m,e,o)}var Ee={getSpotPrice:Zt,calculateInGivenOut:jt,calculateOutGivenIn:en,calculatePoolTradeFee:tn};var _e=class{static fromPool(e){return new _e(e.address,e.tradeFee,e.tokens,e.maxInRatio,e.maxOutRatio,e.minTradingLimit)}constructor(e,o,s,n,l,u){this.type="XYK",this.address=e,this.tradeFee=o,this.tokens=s,this.maxInRatio=n,this.maxOutRatio=l,this.minTradingLimit=u}validPair(e,o){return!0}parsePoolPair(e,o){let s=new Map(this.tokens.map(w=>[w.id,w])),n=s.get(e),l=s.get(o);if(n==null)throw new Error("Pool does not contain tokenIn");if(l==null)throw new Error("Pool does not contain tokenOut");let u=R(n.balance),p=R(l.balance);return{assetIn:e,assetOut:o,decimalsIn:n.decimals,decimalsOut:l.decimals,balanceIn:u,balanceOut:p}}validateBuy(e,o){let s=this.calculateInGivenOut(e,o),n=this.calculateTradeFee(s),l=s.plus(n),u=ve(this.tradeFee),p=[];o.isLessThan(this.minTradingLimit)&&p.push("InsufficientTradingAmount");let w=e.balanceOut.div(this.maxOutRatio);o.isGreaterThan(w)&&p.push("MaxOutRatioExceeded");let y=e.balanceIn.div(this.maxInRatio);return l.isGreaterThan(y)&&p.push("MaxInRatioExceeded"),{amountIn:l,calculatedIn:s,amountOut:o,feePct:u,errors:p}}validateSell(e,o){let s=this.calculateOutGivenIn(e,o),n=this.calculateTradeFee(s),l=s.minus(n),u=ve(this.tradeFee),p=[];o.isLessThan(this.minTradingLimit)&&p.push("InsufficientTradingAmount");let w=e.balanceIn.div(this.maxInRatio);o.isGreaterThan(w)&&p.push("MaxInRatioExceeded");let y=e.balanceOut.div(this.maxOutRatio);return l.isGreaterThan(y)&&p.push("MaxOutRatioExceeded"),{amountIn:o,calculatedOut:s,amountOut:l,feePct:u,errors:p}}calculateInGivenOut(e,o){let s=Ee.calculateInGivenOut(e.balanceIn.toString(),e.balanceOut.toString(),o.toString());return R(s)}calculateOutGivenIn(e,o){let s=Ee.calculateOutGivenIn(e.balanceIn.toString(),e.balanceOut.toString(),o.toString());return R(s)}spotPriceInGivenOut(e){let o=Ee.getSpotPrice(e.balanceOut.toString(),e.balanceIn.toString(),ne(pe,e.decimalsOut).toString());return R(o)}spotPriceOutGivenIn(e){let o=Ee.getSpotPrice(e.balanceIn.toString(),e.balanceOut.toString(),ne(pe,e.decimalsIn).toString());return R(o)}calculateTradeFee(e){let o=Ee.calculatePoolTradeFee(e.toString(),this.tradeFee[0],this.tradeFee[1]);return R(o)}};var xe=class{static get(e){switch(e.type){case"XYK":return _e.fromPool(e);case"LBP":return Ae.fromPool(e);default:throw new Error("Pool type "+e.type+" is not supported yet")}}};var Ne=class{constructor(e,o){this.defaultRouterOptions={includeOnly:[]};this.poolService=e,this.routeSuggester=new Me,this.routerOptions={...this.defaultRouterOptions,...o}}async getPools(){let e=this.routerOptions.includeOnly;return await this.poolService.getPools(e)}async getAllAssets(){let e=await this.getPools();if(e.length===0)throw new Error("No pools configured");let o=await this.getAssets(e);return[...new Map(o).values()]}async getAssetPairs(e){let o=await this.getPools();if(o.length===0)throw new Error("No pools configured");let{assets:s,poolsMap:n}=await this.validateToken(e,o),u=this.getPaths(e,null,n,o).map(p=>p[p.length-1].assetOut);return this.toPoolAssets([...new Set(u)],s)}async getAllPaths(e,o){let s=await this.getPools();if(s.length===0)throw new Error("No pools configured");let{poolsMap:n}=await this.validateTokenPair(e,o,s);return this.getPaths(e,o,n,s)}async getAssets(e){let o=e.map(s=>s.tokens.map(({id:n,symbol:l})=>({id:n,symbol:l}))).flat();return new Map(o.map(s=>[s.id,s]))}getPaths(e,o,s,n){return this.routeSuggester.getProposals(e,o,n).filter(p=>this.validPath(p,s)).map(p=>this.toHops(p,s))}async validateTokenPair(e,o,s){let n=await this.getAssets(s);if(n.get(e)==null)throw new Error(e+" is not supported token");if(n.get(o)==null)throw new Error(o+" is not supported token");let l=this.getPoolMap(s);return{assets:n,poolsMap:l}}async validateToken(e,o){let s=await this.getAssets(o);if(s.get(e)==null)throw new Error(e+" is not supported token");let n=this.getPoolMap(o);return{assets:s,poolsMap:n}}getPoolMap(e){return new Map(e.map(o=>[o.address,xe.get(o)]))}validPath(e,o){return e.length>0&&e.map(s=>this.validEdge(s,o)).reduce((s,n)=>s&&n)}validEdge([e,o,s],n){return n.get(e)?.validPair(o,s)||!1}toHops(e,o){return e.map(([s,n,l])=>{let u=o.get(s);return{poolId:s,poolType:u?.type,assetIn:n,assetOut:l}})}toPoolAssets(e,o){return e.map(s=>{let n=o.get(s);return{id:s,symbol:n?.symbol}})}};function De(m,e,o,s){let n=m.shiftedBy(-1*e).multipliedBy(o),l=s;return n.minus(l).abs().div(n.plus(l).div(2)).multipliedBy(100).decimalPlaces(2)}function Pt(m,e){return pe.minus(e.div(m)).multipliedBy(100).decimalPlaces(2)}function wt(m,e){return e.div(m).minus(pe).multipliedBy(100).decimalPlaces(2)}var Ge=class extends Ne{async getBestSpotPrice(e,o){let s=await super.getPools();if(s.length===0)throw new Error("No pools configured");let{poolsMap:n}=await super.validateTokenPair(e,o,s);return super.getPaths(e,o,n,s).map(p=>this.getSpotPrice(p,n)).sort((p,w)=>{let y=p.amount,I=w.amount;return y.isGreaterThan(I)?-1:1})[0]}getSpotPrice(e,o){let s=[];for(let u=0;u<e.length;u++){let p=e[u],w=o.get(p.poolId);if(w==null)throw new Error("Pool does not exit");let y=w.parsePoolPair(p.assetIn,p.assetOut),I=w.spotPriceOutGivenIn(y);s.push({amount:I,decimals:y.decimalsOut})}let n=s.map(u=>u.amount.shiftedBy(-1*u.decimals)).reduce((u,p)=>u.multipliedBy(p)),l=s[s.length-1].decimals;return{amount:ne(n,l).decimalPlaces(0,1),decimals:l}}isDirectTrade(e){return e.length==1}async getBestSell(e,o,s){let n=await super.getPools();if(n.length===0)throw new Error("No pools configured");let{poolsMap:l}=await super.validateTokenPair(e,o,n),w=super.getPaths(e,o,l,n).map(L=>this.toSellSwaps(s,L,l)).sort((L,Q)=>{let Z=L[L.length-1].amountOut,re=Q[Q.length-1].amountOut;return Z.isGreaterThan(re)?-1:1})[0],y=w[0],I=w[w.length-1],S=this.isDirectTrade(w),k=w.map(L=>L.spotPrice.shiftedBy(-1*L.assetOutDecimals)).reduce((L,Q)=>L.multipliedBy(Q)),M=ne(k,I.assetOutDecimals).decimalPlaces(0,1),A=De(y.amountIn,y.assetInDecimals,M,I.calculatedOut),W=S?I.calculatedOut:this.calculateDelta0Y(y.amountIn,w,l),z=I.amountOut,J=S?I.tradeFeePct:Pt(W,z).toNumber(),O=W.minus(z),Oe=L=>this.poolService.buildSellTx(e,o,y.amountIn,L,w.map(Q=>Q));return{type:"Sell",amountIn:y.amountIn,amountOut:I.amountOut,spotPrice:M,tradeFee:O,tradeFeePct:J,priceImpactPct:A.toNumber(),swaps:w,toTx:Oe,toHuman(){return{type:"Sell",amountIn:$(y.amountIn,y.assetInDecimals),amountOut:$(I.amountOut,I.assetOutDecimals),spotPrice:$(M,I.assetOutDecimals),tradeFee:$(O,I.assetOutDecimals),tradeFeePct:J,priceImpactPct:A.toNumber(),swaps:w.map(L=>L.toHuman())}}}}calculateDelta0Y(e,o,s){let n=[];for(let l=0;l<o.length;l++){let u=o[l],p=s.get(u.poolId);if(p==null)throw new Error("Pool does not exit");let w=p.parsePoolPair(u.assetIn,u.assetOut),y;l>0?y=n[l-1]:y=e;let I=p.calculateOutGivenIn(w,y);n.push(I)}return n[n.length-1]}toSellSwaps(e,o,s){let n=[];for(let l=0;l<o.length;l++){let u=o[l],p=s.get(u.poolId);if(p==null)throw new Error("Pool does not exit");let w=p.parsePoolPair(u.assetIn,u.assetOut),y;l>0?y=n[l-1].amountOut:y=ne(R(e),w.decimalsIn);let{amountOut:I,calculatedOut:S,feePct:k,errors:M}=p.validateSell(w,y),A=p.spotPriceOutGivenIn(w),W=De(y,w.decimalsIn,A,S);n.push({...u,assetInDecimals:w.decimalsIn,assetOutDecimals:w.decimalsOut,amountIn:y,calculatedOut:S,amountOut:I,spotPrice:A,tradeFeePct:k,priceImpactPct:W.toNumber(),errors:M,toHuman(){return{...u,amountIn:$(y,w.decimalsIn),calculatedOut:$(S,w.decimalsOut),amountOut:$(I,w.decimalsOut),spotPrice:$(A,w.decimalsOut),tradeFeePct:k,priceImpactPct:W.toNumber()}}})}return n}async getBestBuy(e,o,s){let n=await super.getPools();if(n.length===0)throw new Error("No pools configured");let{poolsMap:l}=await super.validateTokenPair(e,o,n),w=super.getPaths(e,o,l,n).map(L=>this.toBuySwaps(s,L,l)).sort((L,Q)=>{let Z=L[0].amountIn,re=Q[0].amountIn;return Z.isGreaterThan(re)?1:-1})[0],y=w[w.length-1],I=w[0],S=this.isDirectTrade(w),k=w.map(L=>L.spotPrice.shiftedBy(-1*L.assetInDecimals)).reduce((L,Q)=>L.multipliedBy(Q)),M=ne(k,I.assetInDecimals).decimalPlaces(0,1),A=De(y.amountOut,y.assetOutDecimals,M,I.calculatedIn),W=S?I.calculatedIn:this.calculateDelta0X(y.amountOut,w,l),z=I.amountIn,J=S?I.tradeFeePct:wt(W,z),O=z.minus(W),Oe=L=>this.poolService.buildBuyTx(e,o,y.amountOut,L,w.map(Q=>Q));return{type:"Buy",amountOut:y.amountOut,amountIn:I.amountIn,spotPrice:M,tradeFee:O,tradeFeePct:J,priceImpactPct:A.toNumber(),swaps:w,toTx:Oe,toHuman(){return{type:"Buy",amountOut:$(y.amountOut,y.assetOutDecimals),amountIn:$(I.amountIn,I.assetInDecimals),spotPrice:$(M,I.assetInDecimals),tradeFee:$(O,I.assetInDecimals),tradeFeePct:J,priceImpactPct:A.toNumber(),swaps:w.map(L=>L.toHuman())}}}}calculateDelta0X(e,o,s){let n=[];for(let l=o.length-1;l>=0;l--){let u=o[l],p=s.get(u.poolId);if(p==null)throw new Error("Pool does not exit");let w=p.parsePoolPair(u.assetIn,u.assetOut),y;l==o.length-1?y=e:y=n[0];let I=p.calculateInGivenOut(w,y);n.unshift(I)}return n[0]}toBuySwaps(e,o,s){let n=[];for(let l=o.length-1;l>=0;l--){let u=o[l],p=s.get(u.poolId);if(p==null)throw new Error("Pool does not exit");let w=p.parsePoolPair(u.assetIn,u.assetOut),y;l==o.length-1?y=ne(R(e),w.decimalsOut):y=n[0].amountIn;let{amountIn:I,calculatedIn:S,feePct:k,errors:M}=p.validateBuy(w,y),A=p.spotPriceInGivenOut(w),W=De(y,w.decimalsOut,A,S);n.unshift({...u,assetInDecimals:w.decimalsIn,assetOutDecimals:w.decimalsOut,amountOut:y,calculatedIn:S,amountIn:I,spotPrice:A,tradeFeePct:k,priceImpactPct:W.toNumber(),errors:M,toHuman(){return{...u,amountOut:$(y,w.decimalsOut),calculatedIn:$(S,w.decimalsIn),amountIn:$(I,w.decimalsIn),spotPrice:$(A,w.decimalsIn),tradeFeePct:k,priceImpactPct:W.toNumber()}}})}return n}};0&&(module.exports={BigNumber,DECIMAL_PLACES,INFINITY,ONE,PolkadotApiPoolService,PoolError,PoolFactory,PoolType,Router,TradeRouter,TradeType,XykPool,ZERO,bnum,scale});
2
2
  //# sourceMappingURL=index.js.map