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