@galacticcouncil/sdk 0.0.1-beta.16 → 0.0.1-beta.17
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.esm.js +1 -1
- package/dist/index.esm.js.map +3 -3
- package/dist/index.js +1 -1
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
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 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});
|
|
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((Rn,Be)=>{var Et={},N,{TextEncoder:kt,TextDecoder:Ft}=require("util"),U=0,ze=null;function Ve(){return(ze===null||ze.buffer!==N.memory.buffer)&&(ze=new Uint8Array(N.memory.buffer)),ze}var Ye=new kt("utf-8"),Rt=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 V(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),U=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+=Rt(m,p).written}return U=u,n}var Ke=null;function pe(){return(Ke===null||Ke.buffer!==N.memory.buffer)&&(Ke=new Int32Array(N.memory.buffer)),Ke}var ft=new Ft("utf-8",{ignoreBOM:!0,fatal:!0});ft.decode();function Me(m,e){return ft.decode(Ve().subarray(m,m+e))}Be.exports.calculate_pool_trade_fee=function(m,e,o){try{let l=N.__wbindgen_add_to_stack_pointer(-16),u=V(m,N.__wbindgen_malloc,N.__wbindgen_realloc),p=U;N.calculate_pool_trade_fee(l,u,p,e,o);var s=pe()[l/4+0],n=pe()[l/4+1];return Me(s,n)}finally{N.__wbindgen_add_to_stack_pointer(16),N.__wbindgen_free(s,n)}};Be.exports.get_spot_price=function(m,e,o,s,n){try{let p=N.__wbindgen_add_to_stack_pointer(-16),w=V(m,N.__wbindgen_malloc,N.__wbindgen_realloc),y=U,I=V(e,N.__wbindgen_malloc,N.__wbindgen_realloc),x=U,E=V(o,N.__wbindgen_malloc,N.__wbindgen_realloc),L=U,A=V(s,N.__wbindgen_malloc,N.__wbindgen_realloc),H=U,K=V(n,N.__wbindgen_malloc,N.__wbindgen_realloc),$=U;N.get_spot_price(p,w,y,I,x,E,L,A,H,K,$);var l=pe()[p/4+0],u=pe()[p/4+1];return Me(l,u)}finally{N.__wbindgen_add_to_stack_pointer(16),N.__wbindgen_free(l,u)}};Be.exports.calculate_out_given_in=function(m,e,o,s,n){try{let p=N.__wbindgen_add_to_stack_pointer(-16),w=V(m,N.__wbindgen_malloc,N.__wbindgen_realloc),y=U,I=V(e,N.__wbindgen_malloc,N.__wbindgen_realloc),x=U,E=V(o,N.__wbindgen_malloc,N.__wbindgen_realloc),L=U,A=V(s,N.__wbindgen_malloc,N.__wbindgen_realloc),H=U,K=V(n,N.__wbindgen_malloc,N.__wbindgen_realloc),$=U;N.calculate_out_given_in(p,w,y,I,x,E,L,A,H,K,$);var l=pe()[p/4+0],u=pe()[p/4+1];return Me(l,u)}finally{N.__wbindgen_add_to_stack_pointer(16),N.__wbindgen_free(l,u)}};Be.exports.calculate_in_given_out=function(m,e,o,s,n){try{let p=N.__wbindgen_add_to_stack_pointer(-16),w=V(m,N.__wbindgen_malloc,N.__wbindgen_realloc),y=U,I=V(e,N.__wbindgen_malloc,N.__wbindgen_realloc),x=U,E=V(o,N.__wbindgen_malloc,N.__wbindgen_realloc),L=U,A=V(s,N.__wbindgen_malloc,N.__wbindgen_realloc),H=U,K=V(n,N.__wbindgen_malloc,N.__wbindgen_realloc),$=U;N.calculate_in_given_out(p,w,y,I,x,E,L,A,H,K,$);var l=pe()[p/4+0],u=pe()[p/4+1];return Me(l,u)}finally{N.__wbindgen_add_to_stack_pointer(16),N.__wbindgen_free(l,u)}};Be.exports.calculate_linear_weights=function(m,e,o,s,n){try{let p=N.__wbindgen_add_to_stack_pointer(-16),w=V(m,N.__wbindgen_malloc,N.__wbindgen_realloc),y=U,I=V(e,N.__wbindgen_malloc,N.__wbindgen_realloc),x=U,E=V(o,N.__wbindgen_malloc,N.__wbindgen_realloc),L=U,A=V(s,N.__wbindgen_malloc,N.__wbindgen_realloc),H=U,K=V(n,N.__wbindgen_malloc,N.__wbindgen_realloc),$=U;N.calculate_linear_weights(p,w,y,I,x,E,L,A,H,K,$);var l=pe()[p/4+0],u=pe()[p/4+1];return Me(l,u)}finally{N.__wbindgen_add_to_stack_pointer(16),N.__wbindgen_free(l,u)}};var Mt=require("path").join(__dirname,"hydra_dx_wasm_bg.wasm"),Lt=require("fs").readFileSync(Mt),Dt=new WebAssembly.Module(Lt),Gt=new WebAssembly.Instance(Dt,Et);N=Gt.exports;Be.exports.__wasm=N});var ht=lt((po,me)=>{var Xt={},B,{TextEncoder:zt,TextDecoder:Kt}=require("util"),C=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 W(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),C=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 C=u,n}var Qe=null;function j(){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))}me.exports.calculate_pool_trade_fee=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=W(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=C;B.calculate_pool_trade_fee(l,u,p,e,o);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};me.exports.get_spot_price=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=W(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=C,w=W(e,B.__wbindgen_malloc,B.__wbindgen_realloc),y=C,I=W(o,B.__wbindgen_malloc,B.__wbindgen_realloc),x=C;B.get_spot_price(l,u,p,w,y,I,x);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};me.exports.calculate_out_given_in=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=W(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=C,w=W(e,B.__wbindgen_malloc,B.__wbindgen_realloc),y=C,I=W(o,B.__wbindgen_malloc,B.__wbindgen_realloc),x=C;B.calculate_out_given_in(l,u,p,w,y,I,x);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};me.exports.calculate_in_given_out=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=W(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=C,w=W(e,B.__wbindgen_malloc,B.__wbindgen_realloc),y=C,I=W(o,B.__wbindgen_malloc,B.__wbindgen_realloc),x=C;B.calculate_in_given_out(l,u,p,w,y,I,x);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};me.exports.calculate_liquidity_in=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=W(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=C,w=W(e,B.__wbindgen_malloc,B.__wbindgen_realloc),y=C,I=W(o,B.__wbindgen_malloc,B.__wbindgen_realloc),x=C;B.calculate_liquidity_in(l,u,p,w,y,I,x);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};me.exports.calculate_shares=function(m,e,o){try{let l=B.__wbindgen_add_to_stack_pointer(-16),u=W(m,B.__wbindgen_malloc,B.__wbindgen_realloc),p=C,w=W(e,B.__wbindgen_malloc,B.__wbindgen_realloc),y=C,I=W(o,B.__wbindgen_malloc,B.__wbindgen_realloc),x=C;B.calculate_shares(l,u,p,w,y,I,x);var s=j()[l/4+0],n=j()[l/4+1];return be(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};me.exports.calculate_liquidity_out_asset_a=function(m,e,o,s){try{let u=B.__wbindgen_add_to_stack_pointer(-16),p=W(m,B.__wbindgen_malloc,B.__wbindgen_realloc),w=C,y=W(e,B.__wbindgen_malloc,B.__wbindgen_realloc),I=C,x=W(o,B.__wbindgen_malloc,B.__wbindgen_realloc),E=C,L=W(s,B.__wbindgen_malloc,B.__wbindgen_realloc),A=C;B.calculate_liquidity_out_asset_a(u,p,w,y,I,x,E,L,A);var n=j()[u/4+0],l=j()[u/4+1];return be(n,l)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(n,l)}};me.exports.calculate_liquidity_out_asset_b=function(m,e,o,s){try{let u=B.__wbindgen_add_to_stack_pointer(-16),p=W(m,B.__wbindgen_malloc,B.__wbindgen_realloc),w=C,y=W(e,B.__wbindgen_malloc,B.__wbindgen_realloc),I=C,x=W(o,B.__wbindgen_malloc,B.__wbindgen_realloc),E=C,L=W(s,B.__wbindgen_malloc,B.__wbindgen_realloc),A=C;B.calculate_liquidity_out_asset_b(u,p,w,y,I,x,E,L,A);var n=j()[u/4+0],l=j()[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;me.exports.__wasm=B});var nn={};xt(nn,{BigNumber:()=>Te,DECIMAL_PLACES:()=>mt,INFINITY:()=>At,ONE:()=>ue,PolkadotApiPoolService:()=>Le,PoolError:()=>ot,PoolFactory:()=>Ie,PoolType:()=>he,Router:()=>xe,TradeRouter:()=>Ge,TradeType:()=>rt,XykPool:()=>_e,ZERO:()=>vt,bnum:()=>M,scale:()=>te});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 St=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>St)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 x=[...p];x.push(I),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,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 Re=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 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),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 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),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 Tt=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,it=Math.ceil,ce=Math.floor,ne="[BigNumber Error] ",pt=ne+"Number primitive has more than 15 significant digits: ",ae=1e14,v=14,st=9007199254740991,at=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],Pe=1e7,J=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,x=1e7,E=!1,L=1,A=0,H={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:"\xA0",suffix:""},K="0123456789abcdefghijklmnopqrstuvwxyz",$=!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>x?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>x?d.c=d.e=null:(d.e=f,d.c=[t]);return}h=String(t)}else{if(!Tt.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(q(r,2,K.length,"Base"),r==10&&$)return d=new O(t),D(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=K.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!==ce(t)))throw Error(pt+d.s*t);if((f=f-P-1)>x)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],q(i,0,J,r),u=i),t.hasOwnProperty(r="ROUNDING_MODE")&&(i=t[r],q(i,0,8,r),p=i),t.hasOwnProperty(r="EXPONENTIAL_AT")&&(i=t[r],i&&i.pop?(q(i[0],-J,0,r),q(i[1],0,J,r),w=i[0],y=i[1]):(q(i,-J,J,r),w=-(y=i<0?-i:i))),t.hasOwnProperty(r="RANGE"))if(i=t[r],i&&i.pop)q(i[0],-J,-1,r),q(i[1],1,J,r),I=i[0],x=i[1];else if(q(i,-J,J,r),i)I=-(x=i<0?-i:i);else throw Error(ne+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))E=i;else throw E=!i,Error(ne+"crypto unavailable");else E=i;else throw Error(ne+r+" not true or false: "+i);if(t.hasOwnProperty(r="MODULO_MODE")&&(i=t[r],q(i,0,9,r),L=i),t.hasOwnProperty(r="POW_PRECISION")&&(i=t[r],q(i,0,J,r),A=i),t.hasOwnProperty(r="FORMAT"))if(i=t[r],typeof i=="object")H=i;else throw Error(ne+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",K=i;else throw Error(ne+r+" invalid: "+i)}else throw Error(ne+"Object expected: "+t);return{DECIMAL_PLACES:u,ROUNDING_MODE:p,EXPONENTIAL_AT:[w,y],RANGE:[I,x],CRYPTO:E,MODULO_MODE:L,POW_PRECISION:A,FORMAT:H,ALPHABET:K}},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>=-J&&c<=J&&c===ce(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>=ae||i!==ce(i))break e;if(i!==0)return!0}}}else if(_===null&&c===null&&(f===null||f===1||f===-1))return!0;throw Error(ne+"Invalid BigNumber: "+t)},O.maximum=O.max=function(){return Ne(arguments,n.lt)},O.minimum=O.min=function(){return Ne(arguments,n.gt)},O.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,f,P,a,g=0,h=[],d=new O(l);if(i==null?i=u:q(i,0,J),P=it(i/v),E)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 E=!1,Error(ne+"crypto unavailable");if(!E)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]=ce(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,S,T,R,X=i.indexOf("."),Y=u,F=p;for(X>=0&&(d=A,A=0,i=i.replace(".",""),R=new O(_),S=R.pow(i.length-X),A=d,R.c=r(de(ie(S.c),S.e,"0"),10,c,t),R.e=R.c.length),T=r(i,_,c,P?(a=K,t):(a=t,K)),h=d=T.length;T[--d]==0;T.pop());if(!T[0])return a.charAt(0);if(X<0?--h:(S.c=T,S.e=h,S.s=f,S=e(S,R,Y,F,c),T=S.c,b=S.r,h=S.e),g=h+Y+1,X=T[g],d=c/2,b=b||g<0||T[g+1]!=null,b=F<4?(X!=null||b)&&(F==0||F==(S.s<0?3:2)):X>d||X==d&&(F==4||b||F==6&&T[g-1]&1||F==(S.s<0?8:7)),g<1||!T[0])i=b?de(a.charAt(1),-Y,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(X=0,i="";X<=d;i+=a.charAt(T[X++]));i=de(i,h,a.charAt(0))}return i}}(),e=function(){function t(_,c,f){var P,a,g,h,d=0,b=_.length,S=c%Pe,T=c/Pe|0;for(_=_.slice();b--;)g=_[b]%Pe,h=_[b]/Pe|0,P=T*g+h*S,a=S*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,S,T,R,X,Y,F,G,Z,Ce,et,tt,le,ke,re=_.s==c.s?1:-1,ee=_.c,z=c.c;if(!ee||!ee[0]||!z||!z[0])return new O(!_.s||!c.s||(ee?z&&ee[0]==z[0]:!z)?NaN:ee&&ee[0]==0||!z?re*0:re/0);for(X=new O(re),Y=X.c=[],h=_.e-c.e,re=f+h+1,a||(a=ae,h=se(_.e/v)-se(c.e/v),re=re/v|0),d=0;z[d]==(ee[d]||0);d++);if(z[d]>(ee[d]||0)&&h--,re<0)Y.push(1),b=!0;else{for(et=ee.length,le=z.length,d=0,re+=2,S=ce(a/(z[0]+1)),S>1&&(z=t(z,S,a),ee=t(ee,S,a),le=z.length,et=ee.length),Ce=le,F=ee.slice(0,le),G=F.length;G<le;F[G++]=0);ke=z.slice(),ke=[0].concat(ke),tt=z[0],z[1]>=a/2&&tt++;do{if(S=0,g=r(z,F,le,G),g<0){if(Z=F[0],le!=G&&(Z=Z*a+(F[1]||0)),S=ce(Z/tt),S>1)for(S>=a&&(S=a-1),T=t(z,S,a),R=T.length,G=F.length;r(T,F,R,G)==1;)S--,i(T,le<R?ke:z,R,a),R=T.length,g=1;else S==0&&(g=S=1),T=z.slice(),R=T.length;if(R<G&&(T=[0].concat(T)),i(F,T,G,a),G=F.length,g==-1)for(;r(z,F,le,G)<1;)S++,i(F,le<G?ke:z,G,a),G=F.length}else g===0&&(S++,F=[0]);Y[d++]=S,F[0]?F[G++]=ee[Ce]||0:(F=[ee[Ce]],G=1)}while((Ce++<et||F[0]!=null)&&re--);b=F[0]!=null,Y[0]||Y.splice(0,1)}if(a==ae){for(d=1,re=Y[0];re>=10;re/=10,d++);D(X,f+(X.e=d+h*v-1)+1,P,b)}else X.e=h,X.r=+b;return X}}();function fe(t,r,i,_){var c,f,P,a,g;if(i==null?i=p:q(i,0,8),!t.c)return t.toString();if(c=t.c[0],P=t.e,r==null)g=ie(t.c),g=_==1||_==2&&(P<=w||P>=y)?Xe(g,P):de(g,P,"0");else if(t=D(new O(t),r,i),f=t.e,g=ie(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 Ne(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 k(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)>x?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,S,T){return h=(T=T.toLowerCase())=="x"?16:T=="b"?2:8,!g||g==h?S: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(ne+"Not a"+(g?" base "+g:"")+" number: "+P);f.s=null}f.c=f.e=null}}();function D(t,r,i,_){var c,f,P,a,g,h,d,b=t.c,S=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/S[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/S[c-P-1]%10|0}if(_=_||r<0||b[h+1]!=null||(P<0?g:g%S[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/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[(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=S[v-f],b[h]=P>0?ce(g/S[c-P]%S[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]==ae&&(b[0]=1));break}else{if(b[h]+=a,b[h]!=ae)break;b[h--]=0,a=1}for(f=b.length;b[--f]===0;b.pop());}t.e>x?t.c=t.e=null:t.e<I&&(t.c=[t.e=0])}return t}function oe(t){var r,i=t.e;return i===null?t.toString():(r=ie(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 Se(this,new O(t,r))},n.decimalPlaces=n.dp=function(t,r){var i,_,c,f=this;if(t!=null)return q(t,0,J),r==null?r=p:q(r,0,8),D(new O(f),t+f.e+1,r);if(!(i=f.c))return null;if(_=((c=i.length-1)-se(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(ne+"Exponent not an integer: "+oe(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(+oe(b),a?2-Ue(t):+oe(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(+oe(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=ce(c/2),c===0)break;h=c%2}else if(t=t.times(i),D(t,t.e+1,1),t.e>14)h=Ue(t);else{if(c=+oe(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?D(d,A,p,P):d)},n.integerValue=function(t){var r=new O(this);return t==null?t=p:q(t,0,8),D(r,r.e+1,t)},n.isEqualTo=n.eq=function(t,r){return Se(this,new O(t,r))===0},n.isFinite=function(){return!!this.c},n.isGreaterThan=n.gt=function(t,r){return Se(this,new O(t,r))>0},n.isGreaterThanOrEqualTo=n.gte=function(t,r){return(r=Se(this,new O(t,r)))===1||r===0},n.isInteger=function(){return!!this.c&&se(this.e/v)>this.c.length-2},n.isLessThan=n.lt=function(t,r){return Se(this,new O(t,r))<0},n.isLessThanOrEqualTo=n.lte=function(t,r){return(r=Se(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=se(g),h=se(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=ae-1;_>a;){if(d[--_]<b[_]){for(i=_;i&&!d[--i];d[i]=r);--d[i],d[_]+=ae}d[_]-=b[_]}for(;d[0]==0;d.splice(0,1),--h);return d[0]?k(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):(L==9?(_=t.s,t.s=1,i=e(c,t,0,3),t.s=_,i.s*=_):i=e(c,t,0,L),t=c.minus(i.times(t)),!t.c[0]&&L==1&&(t.s=c.s),t)},n.multipliedBy=n.times=function(t,r){var i,_,c,f,P,a,g,h,d,b,S,T,R,X,Y,F=this,G=F.c,Z=(t=new O(t,r)).c;if(!G||!Z||!G[0]||!Z[0])return!F.s||!t.s||G&&!G[0]&&!Z||Z&&!Z[0]&&!G?t.c=t.e=t.s=null:(t.s*=F.s,!G||!Z?t.c=t.e=null:(t.c=[0],t.e=0)),t;for(_=se(F.e/v)+se(t.e/v),t.s*=F.s,g=G.length,b=Z.length,g<b&&(R=G,G=Z,Z=R,c=g,g=b,b=c),c=g+b,R=[];c--;R.push(0));for(X=ae,Y=Pe,c=b;--c>=0;){for(i=0,S=Z[c]%Y,T=Z[c]/Y|0,P=g,f=c+P;f>c;)h=G[--P]%Y,d=G[P]/Y|0,a=T*h+d*S,h=S*h+a%Y*Y+R[f]+i,i=(h/X|0)+(a/Y|0)+T*d,R[f--]=h%X;R[f]=i}return i?++_:R.splice(0,1),k(t,R,_)},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=se(f),P=se(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)/ae|0,a[r]=ae===a[r]?0:a[r]%ae;return c&&(a=[c].concat(a),++P),k(t,a,P)},n.precision=n.sd=function(t,r){var i,_,c,f=this;if(t!=null&&t!==!!t)return q(t,1,J),r==null?r=p:q(r,0,8),D(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 q(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(+oe(f)),a==0||a==1/0?(r=ie(P),(r.length+g)%2==0&&(r+="0"),a=Math.sqrt(+r),g=se((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))),ie(c.c).slice(0,a)===(r=ie(i.c)).slice(0,a))if(i.e<g&&--a,r=r.slice(a-3,a+1),r=="9999"||!_&&r=="4999"){if(!_&&(D(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")&&(D(i,i.e+u+2,1),t=!i.times(i).eq(f));break}}return D(i,i.e+u+1,p,t)},n.toExponential=function(t,r){return t!=null&&(q(t,0,J),t++),fe(this,t,r,1)},n.toFixed=function(t,r){return t!=null&&(q(t,0,J),t=t+this.e+1),fe(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=H;else if(typeof i!="object")throw Error(ne+"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],S=c.s<0,T=S?d.slice(1):d,R=T.length;if(g&&(f=a,a=g,g=f,R-=f),a>0&&R>0){for(f=R%a||a,d=T.substr(0,f);f<R;f+=a)d+=h+T.substr(f,a);g>0&&(d+=h+T.slice(f)),S&&(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,S,T=this,R=T.c;if(t!=null&&(a=new O(t),!a.isInteger()&&(a.c||a.s!==1)||a.lt(l)))throw Error(ne+"Argument "+(a.isInteger()?"out of range: ":"not an integer: ")+oe(a));if(!R)return new O(T);for(r=new O(l),h=i=new O(l),_=g=new O(l),S=ie(R),f=r.e=S.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=x,x=1/0,a=new O(S),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],x=P,b},n.toNumber=function(){return+oe(this)},n.toPrecision=function(t,r){return t!=null&&q(t,1,J),fe(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):de(ie(i.c),c,"0"):t===10&&$?(i=D(new O(i),u+c+1,p),r=de(ie(i.c),i.e,"0")):(q(t,2,K.length,"Base"),r=o(de(ie(i.c),c,"0"),10,t,_,!0)),_<0&&i.c[0]&&(r="-"+r)),r},n.valueOf=n.toJSON=function(){return oe(this)},n._isBigNumber=!0,n[Symbol.toStringTag]="BigNumber",n[Symbol.for("nodejs.util.inspect.custom")]=n.valueOf,m!=null&&O.set(m),O}function se(m){var e=m|0;return m>0||m===e?e:e-1}function ie(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 Se(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 q(m,e,o,s){if(m<e||m>o||m!==ce(m))throw Error(ne+(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 se(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 Te=gt();var mt=12;Te.config({EXPONENTIAL_AT:[-100,100],ROUNDING_MODE:1,DECIMAL_PLACES:mt});var vt=M(0),ue=M(1),At=M("Infinity");function te(m,e){let o=new Te(e.toString()),s=new Te(10).pow(o);return m.times(s)}function M(m){return new Te(m.toString())}var ge=ut(dt());function Ct(m,e,o,s,n){return(0,ge.get_spot_price)(m,e,o,s,n)}function Wt(m,e,o,s,n){return(0,ge.calculate_in_given_out)(m,e,o,s,n)}function Ht(m,e,o,s,n){return(0,ge.calculate_out_given_in)(m,e,o,s,n)}function qt(m,e,o,s,n){return(0,ge.calculate_linear_weights)(m,e,o,s,n)}function Ut(m,e,o){return(0,ge.calculate_pool_trade_fee)(m,e,o)}var we={getSpotPrice:Ct,calculateInGivenOut:Wt,calculateOutGivenIn:Ht,calculateLinearWeights:qt,calculatePoolTradeFee:Ut};var Je=class extends Oe{constructor(){super(...arguments);this.MAX_FINAL_WEIGHT=te(M(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(),x=await this.getLinearWeight(u),E=M(x),L=this.MAX_FINAL_WEIGHT.minus(M(E)),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:E},{...p[1],weight:L}],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=M(u),w=this.MAX_FINAL_WEIGHT.minus(M(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=M(s.repayTarget);try{let l=await this.getAccountBalance(o,s.feeCollector);return M(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 Q(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=M(n.balance),p=M(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 M(s)}calculateOutGivenIn(e,o){let s=we.calculateOutGivenIn(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),o.toString());return M(s)}spotPriceInGivenOut(e){let o=we.getSpotPrice(e.balanceOut.toString(),e.balanceIn.toString(),e.weightOut.toString(),e.weightIn.toString(),te(ue,e.decimalsOut).toString());return M(o)}spotPriceOutGivenIn(e){let o=we.getSpotPrice(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),te(ue,e.decimalsIn).toString());return M(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 M(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=M(n.balance),p=M(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 M(s)}calculateOutGivenIn(e,o){let s=Ee.calculateOutGivenIn(e.balanceIn.toString(),e.balanceOut.toString(),o.toString());return M(s)}spotPriceInGivenOut(e){let o=Ee.getSpotPrice(e.balanceOut.toString(),e.balanceIn.toString(),te(ue,e.decimalsOut).toString());return M(o)}spotPriceOutGivenIn(e){let o=Ee.getSpotPrice(e.balanceIn.toString(),e.balanceOut.toString(),te(ue,e.decimalsIn).toString());return M(o)}calculateTradeFee(e){let o=Ee.calculatePoolTradeFee(e.toString(),this.tradeFee[0],this.tradeFee[1]);return M(o)}};var Ie=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 xe=class{constructor(e,o){this.defaultRouterOptions={includeOnly:[]};this.poolService=e,this.routeSuggester=new Re,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,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 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 ue.minus(e.div(m)).multipliedBy(100).decimalPlaces(2)}function wt(m,e){return e.div(m).minus(ue).multipliedBy(100).decimalPlaces(2)}var Ge=class extends xe{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:te(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(k=>this.toSellSwaps(s,k,l)).sort((k,D)=>{let oe=k[k.length-1].amountOut,t=D[D.length-1].amountOut;return oe.isGreaterThan(t)?-1:1}),y=w.find(k=>k.every(D=>D.errors.length==0))||w[0],I=y[0],x=y[y.length-1],E=this.isDirectTrade(y),L=y.map(k=>k.spotPrice.shiftedBy(-1*k.assetOutDecimals)).reduce((k,D)=>k.multipliedBy(D)),A=te(L,x.assetOutDecimals).decimalPlaces(0,1),H=De(I.amountIn,I.assetInDecimals,A,x.calculatedOut),K=E?x.calculatedOut:this.calculateDelta0Y(I.amountIn,y,l),$=x.amountOut,O=E?x.tradeFeePct:Pt(K,$).toNumber(),fe=K.minus($),Ne=k=>this.poolService.buildSellTx(e,o,I.amountIn,k,y.map(D=>D));return{type:"Sell",amountIn:I.amountIn,amountOut:x.amountOut,spotPrice:A,tradeFee:fe,tradeFeePct:O,priceImpactPct:H.toNumber(),swaps:y,toTx:Ne,toHuman(){return{type:"Sell",amountIn:Q(I.amountIn,I.assetInDecimals),amountOut:Q(x.amountOut,x.assetOutDecimals),spotPrice:Q(A,x.assetOutDecimals),tradeFee:Q(fe,x.assetOutDecimals),tradeFeePct:O,priceImpactPct:H.toNumber(),swaps:y.map(k=>k.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=te(M(e),w.decimalsIn);let{amountOut:I,calculatedOut:x,feePct:E,errors:L}=p.validateSell(w,y),A=p.spotPriceOutGivenIn(w),H=De(y,w.decimalsIn,A,x);n.push({...u,assetInDecimals:w.decimalsIn,assetOutDecimals:w.decimalsOut,amountIn:y,calculatedOut:x,amountOut:I,spotPrice:A,tradeFeePct:E,priceImpactPct:H.toNumber(),errors:L,toHuman(){return{...u,amountIn:Q(y,w.decimalsIn),calculatedOut:Q(x,w.decimalsOut),amountOut:Q(I,w.decimalsOut),spotPrice:Q(A,w.decimalsOut),tradeFeePct:E,priceImpactPct:H.toNumber(),errors:L}}})}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(k=>this.toBuySwaps(s,k,l)).sort((k,D)=>{let oe=k[0].amountIn,t=D[0].amountIn;return oe.isGreaterThan(t)?1:-1}),y=w.find(k=>k.every(D=>D.errors.length==0))||w[0],I=y[y.length-1],x=y[0],E=this.isDirectTrade(y),L=y.map(k=>k.spotPrice.shiftedBy(-1*k.assetInDecimals)).reduce((k,D)=>k.multipliedBy(D)),A=te(L,x.assetInDecimals).decimalPlaces(0,1),H=De(I.amountOut,I.assetOutDecimals,A,x.calculatedIn),K=E?x.calculatedIn:this.calculateDelta0X(I.amountOut,y,l),$=x.amountIn,O=E?x.tradeFeePct:wt(K,$),fe=$.minus(K),Ne=k=>this.poolService.buildBuyTx(e,o,I.amountOut,k,y.map(D=>D));return{type:"Buy",amountOut:I.amountOut,amountIn:x.amountIn,spotPrice:A,tradeFee:fe,tradeFeePct:O,priceImpactPct:H.toNumber(),swaps:y,toTx:Ne,toHuman(){return{type:"Buy",amountOut:Q(I.amountOut,I.assetOutDecimals),amountIn:Q(x.amountIn,x.assetInDecimals),spotPrice:Q(A,x.assetInDecimals),tradeFee:Q(fe,x.assetInDecimals),tradeFeePct:O,priceImpactPct:H.toNumber(),swaps:y.map(k=>k.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=te(M(e),w.decimalsOut):y=n[0].amountIn;let{amountIn:I,calculatedIn:x,feePct:E,errors:L}=p.validateBuy(w,y),A=p.spotPriceInGivenOut(w),H=De(y,w.decimalsOut,A,x);n.unshift({...u,assetInDecimals:w.decimalsIn,assetOutDecimals:w.decimalsOut,amountOut:y,calculatedIn:x,amountIn:I,spotPrice:A,tradeFeePct:E,priceImpactPct:H.toNumber(),errors:L,toHuman(){return{...u,amountOut:Q(y,w.decimalsOut),calculatedIn:Q(x,w.decimalsIn),amountIn:Q(I,w.decimalsIn),spotPrice:Q(A,w.decimalsIn),tradeFeePct:E,priceImpactPct:H.toNumber(),errors:L}}})}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
|