@galacticcouncil/sdk 0.0.1-beta.12 → 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 +4 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +4 -4
- package/dist/types/api/tradeRouter.d.ts +22 -4
- package/dist/types/client/{capi.d.ts → cApi.d.ts} +0 -0
- package/dist/types/client/polkadotApi.d.ts +1 -0
- package/dist/types/pool/lbp/lbpPolkadotApiClient.d.ts +5 -0
- package/dist/types/pool/polkadotApiPoolService.d.ts +8 -4
- package/dist/types/pool/xyk/xykPolkadotApiClient.d.ts +4 -0
- package/dist/types/types.d.ts +3 -1
- package/dist/types/utils/math.d.ts +20 -0
- package/package.json +1 -1
- package/dist/types/client/polkadot.d.ts +0 -14
- package/dist/types/client/types.d.ts +0 -7
- package/dist/types/pool/polkadotPoolService.d.ts +0 -7
- package/dist/types/pool/xyk/xykPolkadotClient.d.ts +0 -8
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var Pt=Object.create;var Le=Object.defineProperty;var wt=Object.getOwnPropertyDescriptor;var bt=Object.getOwnPropertyNames;var yt=Object.getPrototypeOf,Bt=Object.prototype.hasOwnProperty;var at=(p,e)=>()=>(e||p((e={exports:{}}).exports,e),e.exports),Ot=(p,e)=>{for(var o in e)Le(p,o,{get:e[o],enumerable:!0})},lt=(p,e,o,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of bt(e))!Bt.call(p,n)&&n!==o&&Le(p,n,{get:()=>e[n],enumerable:!(s=wt(e,n))||s.enumerable});return p};var ct=(p,e,o)=>(o=p!=null?Pt(yt(p)):{},lt(e||!p||!p.__esModule?Le(o,"default",{value:p,enumerable:!0}):o,p)),It=p=>lt(Le({},"__esModule",{value:!0}),p);var dt=at((En,ye)=>{var At={},I,{TextEncoder:vt,TextDecoder:Et}=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"),kt=typeof ze.encodeInto=="function"?function(p,e){return ze.encodeInto(p,e)}:function(p,e){let o=ze.encode(p);return e.set(o),{read:p.length,written:o.length}};function z(p,e,o){if(o===void 0){let h=ze.encode(p),w=e(h.length);return Xe().subarray(w,w+h.length).set(h),q=h.length,w}let s=p.length,n=e(s),f=Xe(),u=0;for(;u<s;u++){let h=p.charCodeAt(u);if(h>127)break;f[n+u]=h}if(u!==s){u!==0&&(p=p.slice(u)),n=o(n,s,s=u+p.length*3);let h=Xe().subarray(n+u,n+s);u+=kt(p,h).written}return q=u,n}var Ue=null;function le(){return(Ue===null||Ue.buffer!==I.memory.buffer)&&(Ue=new Int32Array(I.memory.buffer)),Ue}var mt=new Et("utf-8",{ignoreBOM:!0,fatal:!0});mt.decode();function ke(p,e){return mt.decode(Xe().subarray(p,p+e))}ye.exports.calculate_pool_trade_fee=function(p,e,o){try{let f=I.__wbindgen_add_to_stack_pointer(-16),u=z(p,I.__wbindgen_malloc,I.__wbindgen_realloc),h=q;I.calculate_pool_trade_fee(f,u,h,e,o);var s=le()[f/4+0],n=le()[f/4+1];return ke(s,n)}finally{I.__wbindgen_add_to_stack_pointer(16),I.__wbindgen_free(s,n)}};ye.exports.get_spot_price=function(p,e,o,s,n){try{let h=I.__wbindgen_add_to_stack_pointer(-16),w=z(p,I.__wbindgen_malloc,I.__wbindgen_realloc),O=q,N=z(e,I.__wbindgen_malloc,I.__wbindgen_realloc),T=q,E=z(o,I.__wbindgen_malloc,I.__wbindgen_realloc),M=q,R=z(s,I.__wbindgen_malloc,I.__wbindgen_realloc),v=q,L=z(n,I.__wbindgen_malloc,I.__wbindgen_realloc),J=q;I.get_spot_price(h,w,O,N,T,E,M,R,v,L,J);var f=le()[h/4+0],u=le()[h/4+1];return ke(f,u)}finally{I.__wbindgen_add_to_stack_pointer(16),I.__wbindgen_free(f,u)}};ye.exports.calculate_out_given_in=function(p,e,o,s,n){try{let h=I.__wbindgen_add_to_stack_pointer(-16),w=z(p,I.__wbindgen_malloc,I.__wbindgen_realloc),O=q,N=z(e,I.__wbindgen_malloc,I.__wbindgen_realloc),T=q,E=z(o,I.__wbindgen_malloc,I.__wbindgen_realloc),M=q,R=z(s,I.__wbindgen_malloc,I.__wbindgen_realloc),v=q,L=z(n,I.__wbindgen_malloc,I.__wbindgen_realloc),J=q;I.calculate_out_given_in(h,w,O,N,T,E,M,R,v,L,J);var f=le()[h/4+0],u=le()[h/4+1];return ke(f,u)}finally{I.__wbindgen_add_to_stack_pointer(16),I.__wbindgen_free(f,u)}};ye.exports.calculate_in_given_out=function(p,e,o,s,n){try{let h=I.__wbindgen_add_to_stack_pointer(-16),w=z(p,I.__wbindgen_malloc,I.__wbindgen_realloc),O=q,N=z(e,I.__wbindgen_malloc,I.__wbindgen_realloc),T=q,E=z(o,I.__wbindgen_malloc,I.__wbindgen_realloc),M=q,R=z(s,I.__wbindgen_malloc,I.__wbindgen_realloc),v=q,L=z(n,I.__wbindgen_malloc,I.__wbindgen_realloc),J=q;I.calculate_in_given_out(h,w,O,N,T,E,M,R,v,L,J);var f=le()[h/4+0],u=le()[h/4+1];return ke(f,u)}finally{I.__wbindgen_add_to_stack_pointer(16),I.__wbindgen_free(f,u)}};ye.exports.calculate_linear_weights=function(p,e,o,s,n){try{let h=I.__wbindgen_add_to_stack_pointer(-16),w=z(p,I.__wbindgen_malloc,I.__wbindgen_realloc),O=q,N=z(e,I.__wbindgen_malloc,I.__wbindgen_realloc),T=q,E=z(o,I.__wbindgen_malloc,I.__wbindgen_realloc),M=q,R=z(s,I.__wbindgen_malloc,I.__wbindgen_realloc),v=q,L=z(n,I.__wbindgen_malloc,I.__wbindgen_realloc),J=q;I.calculate_linear_weights(h,w,O,N,T,E,M,R,v,L,J);var f=le()[h/4+0],u=le()[h/4+1];return ke(f,u)}finally{I.__wbindgen_add_to_stack_pointer(16),I.__wbindgen_free(f,u)}};var Ft=require("path").join(__dirname,"hydra_dx_wasm_bg.wasm"),Mt=require("fs").readFileSync(Ft),Rt=new WebAssembly.Module(Mt),Dt=new WebAssembly.Instance(Rt,At);I=Dt.exports;ye.exports.__wasm=I});var ht=at((oo,ue)=>{var qt={},B,{TextEncoder:Ut,TextDecoder:Xt}=require("util"),H=0,Ve=null;function Ye(){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(p,e){return Je.encodeInto(p,e)}:function(p,e){let o=Je.encode(p);return e.set(o),{read:p.length,written:o.length}};function C(p,e,o){if(o===void 0){let h=Je.encode(p),w=e(h.length);return Ye().subarray(w,w+h.length).set(h),H=h.length,w}let s=p.length,n=e(s),f=Ye(),u=0;for(;u<s;u++){let h=p.charCodeAt(u);if(h>127)break;f[n+u]=h}if(u!==s){u!==0&&(p=p.slice(u)),n=o(n,s,s=u+p.length*3);let h=Ye().subarray(n+u,n+s);u+=zt(p,h).written}return H=u,n}var $e=null;function Y(){return($e===null||$e.buffer!==B.memory.buffer)&&($e=new Int32Array(B.memory.buffer)),$e}var _t=new Xt("utf-8",{ignoreBOM:!0,fatal:!0});_t.decode();function _e(p,e){return _t.decode(Ye().subarray(p,p+e))}ue.exports.calculate_pool_trade_fee=function(p,e,o){try{let f=B.__wbindgen_add_to_stack_pointer(-16),u=C(p,B.__wbindgen_malloc,B.__wbindgen_realloc),h=H;B.calculate_pool_trade_fee(f,u,h,e,o);var s=Y()[f/4+0],n=Y()[f/4+1];return _e(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};ue.exports.get_spot_price=function(p,e,o){try{let f=B.__wbindgen_add_to_stack_pointer(-16),u=C(p,B.__wbindgen_malloc,B.__wbindgen_realloc),h=H,w=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),O=H,N=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),T=H;B.get_spot_price(f,u,h,w,O,N,T);var s=Y()[f/4+0],n=Y()[f/4+1];return _e(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};ue.exports.calculate_out_given_in=function(p,e,o){try{let f=B.__wbindgen_add_to_stack_pointer(-16),u=C(p,B.__wbindgen_malloc,B.__wbindgen_realloc),h=H,w=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),O=H,N=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),T=H;B.calculate_out_given_in(f,u,h,w,O,N,T);var s=Y()[f/4+0],n=Y()[f/4+1];return _e(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};ue.exports.calculate_in_given_out=function(p,e,o){try{let f=B.__wbindgen_add_to_stack_pointer(-16),u=C(p,B.__wbindgen_malloc,B.__wbindgen_realloc),h=H,w=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),O=H,N=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),T=H;B.calculate_in_given_out(f,u,h,w,O,N,T);var s=Y()[f/4+0],n=Y()[f/4+1];return _e(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};ue.exports.calculate_liquidity_in=function(p,e,o){try{let f=B.__wbindgen_add_to_stack_pointer(-16),u=C(p,B.__wbindgen_malloc,B.__wbindgen_realloc),h=H,w=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),O=H,N=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),T=H;B.calculate_liquidity_in(f,u,h,w,O,N,T);var s=Y()[f/4+0],n=Y()[f/4+1];return _e(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};ue.exports.calculate_shares=function(p,e,o){try{let f=B.__wbindgen_add_to_stack_pointer(-16),u=C(p,B.__wbindgen_malloc,B.__wbindgen_realloc),h=H,w=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),O=H,N=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),T=H;B.calculate_shares(f,u,h,w,O,N,T);var s=Y()[f/4+0],n=Y()[f/4+1];return _e(s,n)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(s,n)}};ue.exports.calculate_liquidity_out_asset_a=function(p,e,o,s){try{let u=B.__wbindgen_add_to_stack_pointer(-16),h=C(p,B.__wbindgen_malloc,B.__wbindgen_realloc),w=H,O=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),N=H,T=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),E=H,M=C(s,B.__wbindgen_malloc,B.__wbindgen_realloc),R=H;B.calculate_liquidity_out_asset_a(u,h,w,O,N,T,E,M,R);var n=Y()[u/4+0],f=Y()[u/4+1];return _e(n,f)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(n,f)}};ue.exports.calculate_liquidity_out_asset_b=function(p,e,o,s){try{let u=B.__wbindgen_add_to_stack_pointer(-16),h=C(p,B.__wbindgen_malloc,B.__wbindgen_realloc),w=H,O=C(e,B.__wbindgen_malloc,B.__wbindgen_realloc),N=H,T=C(o,B.__wbindgen_malloc,B.__wbindgen_realloc),E=H,M=C(s,B.__wbindgen_malloc,B.__wbindgen_realloc),R=H;B.calculate_liquidity_out_asset_b(u,h,w,O,N,T,E,M,R);var n=Y()[u/4+0],f=Y()[u/4+1];return _e(n,f)}finally{B.__wbindgen_add_to_stack_pointer(16),B.__wbindgen_free(n,f)}};var Kt=require("path").join(__dirname,"hydra_dx_wasm_bg.wasm"),Vt=require("fs").readFileSync(Kt),$t=new WebAssembly.Module(Vt),Yt=new WebAssembly.Instance($t,qt);B=Yt.exports;ue.exports.__wasm=B});var en={};Ot(en,{BigNumber:()=>Ne,DECIMAL_PLACES:()=>gt,INFINITY:()=>Tt,ONE:()=>be,PolkadotApiPoolService:()=>Fe,PoolError:()=>ut,PoolFactory:()=>Be,PoolType:()=>we,Router:()=>Oe,TradeRouter:()=>Re,TradeType:()=>nt,XykPool:()=>ge,ZERO:()=>xt,bnum:()=>D,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=[],f=new Ge,u=[];for(u.push([o,""]),f.enqueue(u);f.size()>0;){let h=f.dequeue();if(h==null||h.length>Nt)return n;let w=h[h.length-1];(s===null||w[0]===s)&&n.push(h),e[w[0]].forEach(N=>{if(this.isNotVisited(N[0],h)){let T=[...h];T.push(N),f.enqueue(T)}})}return n}buildAndPopulateGraph(e,o){let s=e[e.length-1],n=[];for(let f=0;f<=parseInt(s);f++)n.push([]);for(let[f,u,h]of o){let w=parseInt(u),O=parseInt(h);n[w].push([O,f])}return n}};function tt(p){let e={};for(let o of p){let s=o.tokens.length;for(let n=0;n<s;n++){e[o.tokens[n].id]||(e[o.tokens[n].id]=[]);for(let f=0;f<s;f++){if(n==f)continue;let u=[o.address,o.tokens[n].id,o.tokens[f].id];e[o.tokens[n].id].push(u)}}}return e}var Ee=class{getProposals(e,o,s){let n=tt(s),f=Object.keys(n),u=f.map(N=>n[N]).flat(),h=new ve,w=h.buildAndPopulateGraph(f,u),O=h.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 f=0;f<s.length;f++){let u=s[f],h=s[f+1];if(h==null)break;n.push(this.toEdge(u,h))}o.push(n)}return o}toEdge(e,o){return[o[1],e[0].toString(),o[0].toString()]}};var Pe=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 f=await this.getAccountBalance(e,n),h=(await this.getAssetMetadata(n)).toHuman();return{id:n.toString(),balance:f,decimals:h.decimals,symbol:h.symbol}});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 we=(n=>(n.XYK="XYK",n.LBP="LBP",n.Stable="Stable",n.Omni="Omni",n))(we||{}),ut=(u=>(u.TradingLimitReached="TradingLimitReached",u.InsufficientTradingAmount="InsufficientTradingAmount",u.InsufficientBalance="InsufficientBalance",u.MaxInRatioExceeded="MaxInRatioExceeded",u.AssetAmountNotReachedLimit="AssetAmountNotReachedLimit",u.AssetAmountExceededLimit="AssetAmountExceededLimit",u))(ut||{}),nt=(o=>(o.Buy="Buy",o.Sell="Sell",o))(nt||{});var He=class extends Pe{async getPools(){let o=(await this.api.query.xyk.poolAssets.entries()).map(async s=>{let n=this.getStorageKey(s,0),f=this.getStorageEntryArray(s),u=await this.getPoolTokens(n,f);return{address:n,type:"XYK",tradeFee:this.getTradeFee(),tokens:u}});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 ft(p){var e,o,s,n=y.prototype={constructor:y,toString:null,valueOf:null},f=new y(1),u=20,h=4,w=-7,O=21,N=-1e7,T=1e7,E=!1,M=1,R=0,v={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:"\xA0",suffix:""},L="0123456789abcdefghijklmnopqrstuvwxyz",J=!0;function y(t,r){var i,d,l,g,P,a,c,_,m=this;if(!(m instanceof y))return new y(t,r);if(r==null){if(t&&t._isBigNumber===!0){m.s=t.s,!t.c||t.e>T?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(g=0,P=t;P>=10;P/=10,g++);g>T?m.c=m.e=null:(m.e=g,m.c=[t]);return}_=String(t)}else{if(!St.test(_=String(t)))return s(m,_,a);m.s=_.charCodeAt(0)==45?(_=_.slice(1),-1):1}(g=_.indexOf("."))>-1&&(_=_.replace(".","")),(P=_.search(/e/i))>0?(g<0&&(g=P),g+=+_.slice(P+1),_=_.substring(0,P)):g<0&&(g=_.length)}else{if(W(r,2,L.length,"Base"),r==10&&J)return m=new y(t),te(m,u+m.e+1,h);if(_=String(t),a=typeof t=="number"){if(t*0!=0)return s(m,_,a,r);if(m.s=1/t<0?(_=_.slice(1),-1):1,y.DEBUG&&_.replace(/^0\.0*|\./,"").length>15)throw Error(pt+t)}else m.s=_.charCodeAt(0)===45?(_=_.slice(1),-1):1;for(i=L.slice(0,r),g=P=0,c=_.length;P<c;P++)if(i.indexOf(d=_.charAt(P))<0){if(d=="."){if(P>g){g=c;continue}}else if(!l&&(_==_.toUpperCase()&&(_=_.toLowerCase())||_==_.toLowerCase()&&(_=_.toUpperCase()))){l=!0,P=-1,g=0;continue}return s(m,String(t),a,r)}a=!1,_=o(_,r,10,m.s),(g=_.indexOf("."))>-1?_=_.replace(".",""):g=_.length}for(P=0;_.charCodeAt(P)===48;P++);for(c=_.length;_.charCodeAt(--c)===48;);if(_=_.slice(P,++c)){if(c-=P,a&&y.DEBUG&&c>15&&(t>rt||t!==ae(t)))throw Error(pt+m.s*t);if((g=g-P-1)>T)m.c=m.e=null;else if(g<N)m.c=[m.e=0];else{if(m.e=g,m.c=[],P=(g+1)%A,g<0&&(P+=A),P<c){for(P&&m.c.push(+_.slice(0,P)),c-=A;P<c;)m.c.push(+_.slice(P,P+=A));P=A-(_=_.slice(P)).length}else P-=c;for(;P--;_+="0");m.c.push(+_)}}else m.c=[m.e=0]}y.clone=ft,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],W(i,0,V,r),u=i),t.hasOwnProperty(r="ROUNDING_MODE")&&(i=t[r],W(i,0,8,r),h=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],O=i[1]):(W(i,-V,V,r),w=-(O=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],T=i[1];else if(W(i,-V,V,r),i)N=-(T=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))E=i;else throw E=!i,Error(ee+"crypto unavailable");else E=i;else throw Error(ee+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),R=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",L=i;else throw Error(ee+r+" invalid: "+i)}else throw Error(ee+"Object expected: "+t);return{DECIMAL_PLACES:u,ROUNDING_MODE:h,EXPONENTIAL_AT:[w,O],RANGE:[N,T],CRYPTO:E,MODULO_MODE:M,POW_PRECISION:R,FORMAT:v,ALPHABET:L}},y.isBigNumber=function(t){if(!t||t._isBigNumber!==!0)return!1;if(!y.DEBUG)return!0;var r,i,d=t.c,l=t.e,g=t.s;e:if({}.toString.call(d)=="[object Array]"){if((g===1||g===-1)&&l>=-V&&l<=V&&l===ae(l)){if(d[0]===0){if(l===0&&d.length===1)return!0;break e}if(r=(l+1)%A,r<1&&(r+=A),String(d[0]).length==r){for(r=0;r<d.length;r++)if(i=d[r],i<0||i>=ie||i!==ae(i))break e;if(i!==0)return!0}}}else if(d===null&&l===null&&(g===null||g===1||g===-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 d,l,g,P,a,c=0,_=[],m=new y(f);if(i==null?i=u:W(i,0,V),P=ot(i/A),E)if(crypto.getRandomValues){for(d=crypto.getRandomValues(new Uint32Array(P*=2));c<P;)a=d[c]*131072+(d[c+1]>>>11),a>=9e15?(l=crypto.getRandomValues(new Uint32Array(2)),d[c]=l[0],d[c+1]=l[1]):(_.push(a%1e14),c+=2);c=P/2}else if(crypto.randomBytes){for(d=crypto.randomBytes(P*=7);c<P;)a=(d[c]&31)*281474976710656+d[c+1]*1099511627776+d[c+2]*4294967296+d[c+3]*16777216+(d[c+4]<<16)+(d[c+5]<<8)+d[c+6],a>=9e15?crypto.randomBytes(7).copy(d,c):(_.push(a%1e14),c+=7);c=P/7}else throw E=!1,Error(ee+"crypto unavailable");if(!E)for(;c<P;)a=r(),a<9e15&&(_[c++]=a%1e14);for(P=_[--c],i%=A,P&&i&&(a=it[A-i],_[c]=ae(P/a)*a);_[c]===0;_.pop(),c--);if(c<0)_=[g=0];else{for(g=-1;_[0]===0;_.splice(0,1),g-=A);for(c=1,a=_[0];a>=10;a/=10,c++);c<A&&(g-=A-c)}return m.e=g,m.c=_,m}}(),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,d,l,g){for(var P,a=[0],c,_=0,m=i.length;_<m;){for(c=a.length;c--;a[c]*=d);for(a[0]+=g.indexOf(i.charAt(_++)),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,d,l,g,P){var a,c,_,m,b,S,x,F,U=i.indexOf("."),K=u,k=h;for(U>=0&&(m=R,R=0,i=i.replace(".",""),F=new y(d),S=F.pow(i.length-U),R=m,F.c=r(fe(oe(S.c),S.e,"0"),10,l,t),F.e=F.c.length),x=r(i,d,l,P?(a=L,t):(a=t,L)),_=m=x.length;x[--m]==0;x.pop());if(!x[0])return a.charAt(0);if(U<0?--_:(S.c=x,S.e=_,S.s=g,S=e(S,F,K,k,l),x=S.c,b=S.r,_=S.e),c=_+K+1,U=x[c],m=l/2,b=b||c<0||x[c+1]!=null,b=k<4?(U!=null||b)&&(k==0||k==(S.s<0?3:2)):U>m||U==m&&(k==4||b||k==6&&x[c-1]&1||k==(S.s<0?8:7)),c<1||!x[0])i=b?fe(a.charAt(1),-K,a.charAt(0)):a.charAt(0);else{if(x.length=c,b)for(--l;++x[--c]>l;)x[c]=0,c||(++_,x=[1].concat(x));for(m=x.length;!x[--m];);for(U=0,i="";U<=m;i+=a.charAt(x[U++]));i=fe(i,_,a.charAt(0))}return i}}(),e=function(){function t(d,l,g){var P,a,c,_,m=0,b=d.length,S=l%me,x=l/me|0;for(d=d.slice();b--;)c=d[b]%me,_=d[b]/me|0,P=x*c+_*S,a=S*c+P%me*me+m,m=(a/g|0)+(P/me|0)+x*_,d[b]=a%g;return m&&(d=[m].concat(d)),d}function r(d,l,g,P){var a,c;if(g!=P)c=g>P?1:-1;else for(a=c=0;a<g;a++)if(d[a]!=l[a]){c=d[a]>l[a]?1:-1;break}return c}function i(d,l,g,P){for(var a=0;g--;)d[g]-=a,a=d[g]<l[g]?1:0,d[g]=a*P+d[g]-l[g];for(;!d[0]&&d.length>1;d.splice(0,1));}return function(d,l,g,P,a){var c,_,m,b,S,x,F,U,K,k,G,$,De,je,et,se,Ae,ne=d.s==l.s?1:-1,Q=d.c,X=l.c;if(!Q||!Q[0]||!X||!X[0])return new y(!d.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=[],_=d.e-l.e,ne=g+_+1,a||(a=ie,_=re(d.e/A)-re(l.e/A),ne=ne/A|0),m=0;X[m]==(Q[m]||0);m++);if(X[m]>(Q[m]||0)&&_--,ne<0)K.push(1),b=!0;else{for(je=Q.length,se=X.length,m=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,k=Q.slice(0,se),G=k.length;G<se;k[G++]=0);Ae=X.slice(),Ae=[0].concat(Ae),et=X[0],X[1]>=a/2&&et++;do{if(S=0,c=r(X,k,se,G),c<0){if($=k[0],se!=G&&($=$*a+(k[1]||0)),S=ae($/et),S>1)for(S>=a&&(S=a-1),x=t(X,S,a),F=x.length,G=k.length;r(x,k,F,G)==1;)S--,i(x,se<F?Ae:X,F,a),F=x.length,c=1;else S==0&&(c=S=1),x=X.slice(),F=x.length;if(F<G&&(x=[0].concat(x)),i(k,x,G,a),G=k.length,c==-1)for(;r(X,k,se,G)<1;)S++,i(k,se<G?Ae:X,G,a),G=k.length}else c===0&&(S++,k=[0]);K[m++]=S,k[0]?k[G++]=Q[De]||0:(k=[Q[De]],G=1)}while((De++<je||k[0]!=null)&&ne--);b=k[0]!=null,K[0]||K.splice(0,1)}if(a==ie){for(m=1,ne=K[0];ne>=10;ne/=10,m++);te(U,g+(U.e=m+_*A-1)+1,P,b)}else U.e=_,U.r=+b;return U}}();function Qe(t,r,i,d){var l,g,P,a,c;if(i==null?i=h:W(i,0,8),!t.c)return t.toString();if(l=t.c[0],P=t.e,r==null)c=oe(t.c),c=d==1||d==2&&(P<=w||P>=O)?We(c,P):fe(c,P,"0");else if(t=te(new y(t),r,i),g=t.e,c=oe(t.c),a=c.length,d==1||d==2&&(r<=g||g<=w)){for(;a<r;c+="0",a++);c=We(c,g)}else if(r-=P,c=fe(c,g,"0"),g+1>a){if(--r>0)for(c+=".";r--;c+="0");}else if(r+=g-a,r>0)for(g+1==a&&(c+=".");r--;c+="0");return t.s<0&&l?"-"+c:c}function st(t,r){for(var i,d=1,l=new y(t[0]);d<t.length;d++)if(i=new y(t[d]),i.s)r.call(l,i)&&(l=i);else{l=i;break}return l}function Ze(t,r,i){for(var d=1,l=r.length;!r[--l];r.pop());for(l=r[0];l>=10;l/=10,d++);return(i=d+i*A-1)>T?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=/^\.([^.]+)$/,d=/^-?(Infinity|NaN)$/,l=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(g,P,a,c){var _,m=a?P:P.replace(l,"");if(d.test(m))g.s=isNaN(m)?null:m<0?-1:1;else{if(!a&&(m=m.replace(t,function(b,S,x){return _=(x=x.toLowerCase())=="x"?16:x=="b"?2:8,!c||c==_?S:b}),c&&(_=c,m=m.replace(r,"$1").replace(i,"0.$1")),P!=m))return new y(m,_);if(y.DEBUG)throw Error(ee+"Not a"+(c?" base "+c:"")+" number: "+P);g.s=null}g.c=g.e=null}}();function te(t,r,i,d){var l,g,P,a,c,_,m,b=t.c,S=it;if(b){e:{for(l=1,a=b[0];a>=10;a/=10,l++);if(g=r-l,g<0)g+=A,P=r,c=b[_=0],m=c/S[l-P-1]%10|0;else if(_=ot((g+1)/A),_>=b.length)if(d){for(;b.length<=_;b.push(0));c=m=0,l=1,g%=A,P=g-A+1}else break e;else{for(c=a=b[_],l=1;a>=10;a/=10,l++);g%=A,P=g-A+l,m=P<0?0:c/S[l-P-1]%10|0}if(d=d||r<0||b[_+1]!=null||(P<0?c:c%S[l-P-1]),d=i<4?(m||d)&&(i==0||i==(t.s<0?3:2)):m>5||m==5&&(i==4||d||i==6&&(g>0?P>0?c/S[l-P]:0:b[_-1])%10&1||i==(t.s<0?8:7)),r<1||!b[0])return b.length=0,d?(r-=t.e+1,b[0]=S[(A-r%A)%A],t.e=-r||0):b[0]=t.e=0,t;if(g==0?(b.length=_,a=1,_--):(b.length=_+1,a=S[A-g],b[_]=P>0?ae(c/S[l-P]%S[P])*a:0),d)for(;;)if(_==0){for(g=1,P=b[0];P>=10;P/=10,g++);for(P=b[0]+=a,a=1;P>=10;P/=10,a++);g!=a&&(t.e++,b[0]==ie&&(b[0]=1));break}else{if(b[_]+=a,b[_]!=ie)break;b[_--]=0,a=1}for(g=b.length;b[--g]===0;b.pop());}t.e>T?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?We(r,i):fe(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,d,l,g=this;if(t!=null)return W(t,0,V),r==null?r=h:W(r,0,8),te(new y(g),t+g.e+1,r);if(!(i=g.c))return null;if(d=((l=i.length-1)-re(this.e/A))*A,l=i[l])for(;l%10==0;l/=10,d--);return d<0&&(d=0),d},n.dividedBy=n.div=function(t,r){return e(this,new y(t,r),u,h)},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,d,l,g,P,a,c,_,m,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 m=new y(Math.pow(+pe(b),a?2-Ce(t):+pe(t))),r?m.mod(r):m;if(c=t.s<0,r){if(r.c?!r.c[0]:!r.s)return new y(NaN);d=!c&&b.isInteger()&&r.isInteger(),d&&(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 g=b.s<0&&Ce(t)?-0:0,b.e>-1&&(g=1/g),new y(c?1/g:g);R&&(g=ot(R/A+2))}for(a?(i=new y(.5),c&&(t.s=1),_=Ce(t)):(l=Math.abs(+pe(t)),_=l%2),m=new y(f);;){if(_){if(m=m.times(b),!m.c)break;g?m.c.length>g&&(m.c.length=g):d&&(m=m.mod(r))}if(l){if(l=ae(l/2),l===0)break;_=l%2}else if(t=t.times(i),te(t,t.e+1,1),t.e>14)_=Ce(t);else{if(l=+pe(t),l===0)break;_=l%2}b=b.times(b),g?b.c&&b.c.length>g&&(b.c.length=g):d&&(b=b.mod(r))}return d?m:(c&&(m=f.div(m)),r?m.mod(r):g?te(m,R,h,P):m)},n.integerValue=function(t){var r=new y(this);return t==null?t=h:W(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,d,l,g,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 c=P.e/A,_=t.e/A,m=P.c,b=t.c;if(!c||!_){if(!m||!b)return m?(t.s=-r,t):new y(b?P:NaN);if(!m[0]||!b[0])return b[0]?(t.s=-r,t):new y(m[0]?P:h==3?-0:0)}if(c=re(c),_=re(_),m=m.slice(),a=c-_){for((g=a<0)?(a=-a,l=m):(_=c,l=b),l.reverse(),r=a;r--;l.push(0));l.reverse()}else for(d=(g=(a=m.length)<(r=b.length))?a:r,a=r=0;r<d;r++)if(m[r]!=b[r]){g=m[r]<b[r];break}if(g&&(l=m,m=b,b=l,t.s=-t.s),r=(d=b.length)-(i=m.length),r>0)for(;r--;m[i++]=0);for(r=ie-1;d>a;){if(m[--d]<b[d]){for(i=d;i&&!m[--i];m[i]=r);--m[i],m[d]+=ie}m[d]-=b[d]}for(;m[0]==0;m.splice(0,1),--_);return m[0]?Ze(t,m,_):(t.s=h==3?-1:1,t.c=[t.e=0],t)},n.modulo=n.mod=function(t,r){var i,d,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?(d=t.s,t.s=1,i=e(l,t,0,3),t.s=d,i.s*=d):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,d,l,g,P,a,c,_,m,b,S,x,F,U,K,k=this,G=k.c,$=(t=new y(t,r)).c;if(!G||!$||!G[0]||!$[0])return!k.s||!t.s||G&&!G[0]&&!$||$&&!$[0]&&!G?t.c=t.e=t.s=null:(t.s*=k.s,!G||!$?t.c=t.e=null:(t.c=[0],t.e=0)),t;for(d=re(k.e/A)+re(t.e/A),t.s*=k.s,c=G.length,b=$.length,c<b&&(F=G,G=$,$=F,l=c,c=b,b=l),l=c+b,F=[];l--;F.push(0));for(U=ie,K=me,l=b;--l>=0;){for(i=0,S=$[l]%K,x=$[l]/K|0,P=c,g=l+P;g>l;)_=G[--P]%K,m=G[P]/K|0,a=x*_+m*S,_=S*_+a%K*K+F[g]+i,i=(_/U|0)+(a/K|0)+x*m,F[g--]=_%U;F[g]=i}return i?++d:F.splice(0,1),Ze(t,F,d)},n.negated=function(){var t=new y(this);return t.s=-t.s||null,t},n.plus=function(t,r){var i,d=this,l=d.s;if(t=new y(t,r),r=t.s,!l||!r)return new y(NaN);if(l!=r)return t.s=-r,d.minus(t);var g=d.e/A,P=t.e/A,a=d.c,c=t.c;if(!g||!P){if(!a||!c)return new y(l/0);if(!a[0]||!c[0])return c[0]?t:new y(a[0]?d:l*0)}if(g=re(g),P=re(P),a=a.slice(),l=g-P){for(l>0?(P=g,i=c):(l=-l,i=a),i.reverse();l--;i.push(0));i.reverse()}for(l=a.length,r=c.length,l-r<0&&(i=c,c=a,a=i,r=l),l=0;r;)l=(a[--r]=a[r]+c[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,d,l,g=this;if(t!=null&&t!==!!t)return W(t,1,V),r==null?r=h:W(r,0,8),te(new y(g),t,r);if(!(i=g.c))return null;if(l=i.length-1,d=l*A+1,l=i[l]){for(;l%10==0;l/=10,d--);for(l=i[0];l>=10;l/=10,d++);}return t&&g.e+1>d&&(d=g.e+1),d},n.shiftedBy=function(t){return W(t,-rt,rt),this.times("1e"+t)},n.squareRoot=n.sqrt=function(){var t,r,i,d,l,g=this,P=g.c,a=g.s,c=g.e,_=u+4,m=new y("0.5");if(a!==1||!P||!P[0])return new y(!a||a<0&&(!P||P[0])?NaN:P?g:1/0);if(a=Math.sqrt(+pe(g)),a==0||a==1/0?(r=oe(P),(r.length+c)%2==0&&(r+="0"),a=Math.sqrt(+r),c=re((c+1)/2)-(c<0||c%2),a==1/0?r="5e"+c:(r=a.toExponential(),r=r.slice(0,r.indexOf("e")+1)+c),i=new y(r)):i=new y(a+""),i.c[0]){for(c=i.e,a=c+_,a<3&&(a=0);;)if(l=i,i=m.times(l.plus(e(g,l,_,1))),oe(l.c).slice(0,a)===(r=oe(i.c)).slice(0,a))if(i.e<c&&--a,r=r.slice(a-3,a+1),r=="9999"||!d&&r=="4999"){if(!d&&(te(l,l.e+u+2,0),l.times(l).eq(g))){i=l;break}_+=4,a+=4,d=1}else{(!+r||!+r.slice(1)&&r.charAt(0)=="5")&&(te(i,i.e+u+2,1),t=!i.times(i).eq(g));break}}return te(i,i.e+u+1,h,t)},n.toExponential=function(t,r){return t!=null&&(W(t,0,V),t++),Qe(this,t,r,1)},n.toFixed=function(t,r){return t!=null&&(W(t,0,V),t=t+this.e+1),Qe(this,t,r)},n.toFormat=function(t,r,i){var d,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(d=l.toFixed(t,r),l.c){var g,P=d.split("."),a=+i.groupSize,c=+i.secondaryGroupSize,_=i.groupSeparator||"",m=P[0],b=P[1],S=l.s<0,x=S?m.slice(1):m,F=x.length;if(c&&(g=a,a=c,c=g,F-=g),a>0&&F>0){for(g=F%a||a,m=x.substr(0,g);g<F;g+=a)m+=_+x.substr(g,a);c>0&&(m+=_+x.slice(g)),S&&(m="-"+m)}d=b?m+(i.decimalSeparator||"")+((c=+i.fractionGroupSize)?b.replace(new RegExp("\\d{"+c+"}\\B","g"),"$&"+(i.fractionGroupSeparator||"")):b):m}return(i.prefix||"")+d+(i.suffix||"")},n.toFraction=function(t){var r,i,d,l,g,P,a,c,_,m,b,S,x=this,F=x.c;if(t!=null&&(a=new y(t),!a.isInteger()&&(a.c||a.s!==1)||a.lt(f)))throw Error(ee+"Argument "+(a.isInteger()?"out of range: ":"not an integer: ")+pe(a));if(!F)return new y(x);for(r=new y(f),_=i=new y(f),d=c=new y(f),S=oe(F),g=r.e=S.length-x.e-1,r.c[0]=it[(P=g%A)<0?A+P:P],t=!t||a.comparedTo(r)>0?g>0?r:_:a,P=T,T=1/0,a=new y(S),c.c[0]=0;m=e(a,r,0,1),l=i.plus(m.times(d)),l.comparedTo(t)!=1;)i=d,d=l,_=c.plus(m.times(l=_)),c=l,r=a.minus(m.times(l=r)),a=l;return l=e(t.minus(i),d,0,1),c=c.plus(l.times(_)),i=i.plus(l.times(d)),c.s=_.s=x.s,g=g*2,b=e(_,d,g,h).minus(x).abs().comparedTo(e(c,i,g,h).minus(x).abs())<1?[_,d]:[c,i],T=P,b},n.toNumber=function(){return+pe(this)},n.toPrecision=function(t,r){return t!=null&&W(t,1,V),Qe(this,t,r,2)},n.toString=function(t){var r,i=this,d=i.s,l=i.e;return l===null?d?(r="Infinity",d<0&&(r="-"+r)):r="NaN":(t==null?r=l<=w||l>=O?We(oe(i.c),l):fe(oe(i.c),l,"0"):t===10&&J?(i=te(new y(i),u+l+1,h),r=fe(oe(i.c),i.e,"0")):(W(t,2,L.length,"Base"),r=o(fe(oe(i.c),l,"0"),10,t,d,!0)),d<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,p!=null&&y.set(p),y}function re(p){var e=p|0;return p>0||p===e?e:e-1}function oe(p){for(var e,o,s=1,n=p.length,f=p[0]+"";s<n;){for(e=p[s++]+"",o=A-e.length;o--;e="0"+e);f+=e}for(n=f.length;f.charCodeAt(--n)===48;);return f.slice(0,n+1||1)}function Ie(p,e){var o,s,n=p.c,f=e.c,u=p.s,h=e.s,w=p.e,O=e.e;if(!u||!h)return null;if(o=n&&!n[0],s=f&&!f[0],o||s)return o?s?0:-h:u;if(u!=h)return u;if(o=u<0,s=w==O,!n||!f)return s?0:!n^o?1:-1;if(!s)return w>O^o?1:-1;for(h=(w=n.length)<(O=f.length)?w:O,u=0;u<h;u++)if(n[u]!=f[u])return n[u]>f[u]^o?1:-1;return w==O?0:w>O^o?1:-1}function W(p,e,o,s){if(p<e||p>o||p!==ae(p))throw Error(ee+(s||"Argument")+(typeof p=="number"?p<e||p>o?" out of range: ":" not an integer: ":" not a primitive number: ")+String(p))}function Ce(p){var e=p.c.length-1;return re(p.e/A)==e&&p.c[e]%2!=0}function We(p,e){return(p.length>1?p.charAt(0)+"."+p.slice(1):p)+(e<0?"e":"e+")+e}function fe(p,e,o){var s,n;if(e<0){for(n=o+".";++e;n+=o);p=n+p}else if(s=p.length,++e>s){for(n=o,e-=s;--e;n+=o);p+=n}else e<s&&(p=p.slice(0,e)+"."+p.slice(e));return p}var Ne=ft();var gt=12;Ne.config({EXPONENTIAL_AT:[-100,100],ROUNDING_MODE:1,DECIMAL_PLACES:gt});var xt=D(0),be=D(1),Tt=D("Infinity");function Z(p,e){let o=new Ne(e.toString()),s=new Ne(10).pow(o);return p.times(s)}function D(p){return new Ne(p.toString())}var ce=ct(dt());function Lt(p,e,o,s,n){return(0,ce.get_spot_price)(p,e,o,s,n)}function Gt(p,e,o,s,n){return(0,ce.calculate_in_given_out)(p,e,o,s,n)}function Ht(p,e,o,s,n){return(0,ce.calculate_out_given_in)(p,e,o,s,n)}function Ct(p,e,o,s,n){return(0,ce.calculate_linear_weights)(p,e,o,s,n)}function Wt(p,e,o){return(0,ce.calculate_pool_trade_fee)(p,e,o)}var de={getSpotPrice:Lt,calculateInGivenOut:Gt,calculateOutGivenIn:Ht,calculateLinearWeights:Ct,calculatePoolTradeFee:Wt};var Ke=class extends Pe{constructor(){super(...arguments);this.MAX_FINAL_WEIGHT=Z(D(100),6)}async getPools(){let s=(await this.api.query.lbp.poolData.entries()).map(async n=>{let f=this.getStorageKey(n,0),u=n[1].toJSON(),h=await this.getPoolTokens(f,u.assets),w=await this.getLinearWeight(u),O=D(w),N=this.MAX_FINAL_WEIGHT.minus(D(O)),T=h[0].id;return{address:f,type:"LBP",tradeFee:u.fee,repayFee:this.getRepayFee(),repayFeeApply:await this.isRepayFeeApplied(T,u),tokens:[{...h[0],weight:O},{...h[1],weight:N}]}});return Promise.all(s)}async getLinearWeight(o){let n=(await this.api.query.parachainSystem.validationData()).toJSON();return de.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 f=await this.getAccountBalance(o,s.feeCollector);return D(f).isLessThan(n)}catch{return!0}}};var Fe=class{constructor(e){this.api=e}async getPools(){let e=[],o=await new He(this.api).getPools(),s=await new Ke(this.api).getPools();return e.push(o),e.push(s),e.flat()}buildBuyTx(e,o,s,n,f){let u=this.api.tx.router.buy(e,o,s.toFixed(),n.toFixed(),f),h=()=>u;return{hex:u.toHex(),name:"RouterBuy",get:h}}buildSellTx(e,o,s,n,f){let u=this.api.tx.router.sell(e,o,s.toFixed(),n.toFixed(),f),h=()=>u;return{hex:u.toHex(),name:"RouterSell",get:h}}};function j(p,e){return p.shiftedBy(-1*e).toString()}function Se(p){return p[0]/p[1]*100}var xe=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 xe(e.address,e.tradeFee,e.tokens,e.repayFee,e.repayFeeApply)}constructor(e,o,s,n,f){this.type="LBP",this.address=e,this.tradeFee=o,this.tokens=s,this.repayFee=n,this.repayFeeApply=f}validPair(e,o){return!0}parsePoolPair(e,o){let s=new Map(this.tokens.map(w=>[w.id,w])),n=s.get(e),f=s.get(o);if(n==null)throw new Error("Pool does not contain tokenIn");if(f==null)throw new Error("Pool does not contain tokenOut");let u=D(n.balance),h=D(f.balance);return{assetIn:e,assetOut:o,decimalsIn:n.decimals,decimalsOut:f.decimals,weightIn:n.weight,weightOut:f.weight,balanceIn:u,balanceOut:h}}validateBuy(e,o){if(this.tokens[0].id===e.assetOut){let n=this.calculateTradeFee(o),f=o.plus(n),u=this.calculateInGivenOut(e,f),h=Se(this.repayFeeApply?this.repayFee:this.tradeFee);return{amountIn:u,calculatedIn:u,amountOut:o,feePct:h}}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),f=this.calculateTradeFee(n),u=n.minus(f),h=Se(this.repayFeeApply?this.repayFee:this.tradeFee);return{amountIn:o,calculatedOut:n,amountOut:u,feePct:h}}}calculateInGivenOut(e,o){let s=de.calculateInGivenOut(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),o.toString());return D(s)}calculateOutGivenIn(e,o){let s=de.calculateOutGivenIn(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),o.toString());return D(s)}spotPriceInGivenOut(e){let o=de.getSpotPrice(e.balanceOut.toString(),e.balanceIn.toString(),e.weightOut.toString(),e.weightIn.toString(),Z(be,e.decimalsIn).toString());return D(o)}spotPriceOutGivenIn(e){let o=de.getSpotPrice(e.balanceIn.toString(),e.balanceOut.toString(),e.weightIn.toString(),e.weightOut.toString(),Z(be,e.decimalsIn).toString());return D(o)}calculateTradeFee(e){let o=de.calculatePoolTradeFee(e.toString(),this.repayFeeApply?this.repayFee[0]:this.tradeFee[0],this.repayFeeApply?this.repayFee[1]:this.tradeFee[1]);return D(o)}};var he=ct(ht());function Jt(p,e,o){return(0,he.get_spot_price)(p,e,o)}function Qt(p,e,o){return(0,he.calculate_in_given_out)(p,e,o)}function Zt(p,e,o){return(0,he.calculate_out_given_in)(p,e,o)}function jt(p,e,o){return(0,he.calculate_pool_trade_fee)(p,e,o)}var Te={getSpotPrice:Jt,calculateInGivenOut:Qt,calculateOutGivenIn:Zt,calculatePoolTradeFee:jt};var ge=class{static fromPool(e){return new ge(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),f=s.get(o);if(n==null)throw new Error("Pool does not contain tokenIn");if(f==null)throw new Error("Pool does not contain tokenOut");let u=D(n.balance),h=D(f.balance);return{assetIn:e,assetOut:o,decimalsIn:n.decimals,decimalsOut:f.decimals,balanceIn:u,balanceOut:h}}validateBuy(e,o){let s=this.calculateInGivenOut(e,o),n=this.calculateTradeFee(s),f=s.plus(n),u=Se(this.tradeFee);return{amountIn:f,calculatedIn:s,amountOut:o,feePct:u}}validateSell(e,o){let s=this.calculateOutGivenIn(e,o),n=this.calculateTradeFee(s),f=s.minus(n),u=Se(this.tradeFee);return{amountIn:o,calculatedOut:s,amountOut:f,feePct:u}}calculateInGivenOut(e,o){let s=Te.calculateInGivenOut(e.balanceIn.toString(),e.balanceOut.toString(),o.toString());return D(s)}calculateOutGivenIn(e,o){let s=Te.calculateOutGivenIn(e.balanceIn.toString(),e.balanceOut.toString(),o.toString());return D(s)}spotPriceInGivenOut(e){let o=Te.getSpotPrice(e.balanceOut.toString(),e.balanceIn.toString(),Z(be,e.decimalsOut).toString());return D(o)}spotPriceOutGivenIn(e){let o=Te.getSpotPrice(e.balanceIn.toString(),e.balanceOut.toString(),Z(be,e.decimalsIn).toString());return D(o)}calculateTradeFee(e){let o=Te.calculatePoolTradeFee(e.toString(),this.tradeFee[0],this.tradeFee[1]);return D(o)}};var Be=class{static get(e){switch(e.type){case"XYK":return ge.fromPool(e);case"LBP":return xe.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 Ee,this.routerOptions={...this.defaultRouterOptions,...o}}async getPools(){let e=await this.poolService.getPools(),o=new Set(this.routerOptions.includeOnly);return o.size===0?e:e.filter(s=>o.has(s.type))}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(h=>h[h.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:f})=>({id:n,symbol:f}))).flat();return new Map(o.map(s=>[s.id,s]))}getPaths(e,o,s,n){return this.routeSuggester.getProposals(e,o,n).filter(h=>this.validPath(h,s)).map(h=>this.toHops(h,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 f=this.getPoolMap(s);return{assets:n,poolsMap:f}}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,f])=>{let u=o.get(s);return{poolId:s,poolType:u?.type,assetIn:n,assetOut:f}})}toPoolAssets(e,o){return e.map(s=>{let n=o.get(s);return{id:s,symbol:n?.symbol}})}};function Me(p,e,o,s){let n=p.shiftedBy(-1*e).multipliedBy(o),f=s;return n.minus(f).abs().div(n.plus(f).div(2)).multipliedBy(100).decimalPlaces(2)}var Re=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(h=>this.getSpotPrice(h,n)).sort((h,w)=>{let O=h.amount,N=w.amount;return O.isGreaterThan(N)?-1:1})[0]}getSpotPrice(e,o){let s=[];for(let u=0;u<e.length;u++){let h=e[u],w=o.get(h.poolId);if(w==null)throw new Error("Pool does not exit");let O=w.parsePoolPair(h.assetIn,h.assetOut),N=w.spotPriceOutGivenIn(O);s.push({amount:N,decimals:O.decimalsOut})}let n=s.map(u=>u.amount.shiftedBy(-1*u.decimals)).reduce((u,h)=>u.multipliedBy(h)),f=s[s.length-1].decimals;return{amount:Z(n,f).decimalPlaces(0,1),decimals:f}}async getBestSell(e,o,s){let n=await super.getPools();if(n.length===0)throw new Error("No pools configured");let{poolsMap:f}=await super.validateTokenPair(e,o,n),w=super.getPaths(e,o,f,n).map(v=>this.toSellSwaps(s,v,f)).sort((v,L)=>{let J=v[v.length-1].amountOut,y=L[L.length-1].amountOut;return J.isGreaterThan(y)?-1:1})[0],O=w[0],N=w[w.length-1],T=w.map(v=>v.spotPrice.shiftedBy(-1*v.assetOutDecimals)).reduce((v,L)=>v.multipliedBy(L)),E=Z(T,N.assetOutDecimals).decimalPlaces(0,1),M=Me(O.amountIn,O.assetInDecimals,E,N.calculatedOut),R=v=>this.poolService.buildSellTx(e,o,O.amountIn,v,w.map(L=>L));return{type:"Sell",amountIn:O.amountIn,amountOut:N.amountOut,spotPrice:E,priceImpactPct:M.toNumber(),swaps:w,toTx:R,toHuman(){return{type:"Sell",amountIn:j(O.amountIn,O.assetInDecimals),amountOut:j(N.amountOut,N.assetOutDecimals),spotPrice:j(E,N.assetOutDecimals),priceImpactPct:M.toNumber(),swaps:w.map(v=>v.toHuman())}}}}toSellSwaps(e,o,s){let n=[];for(let f=0;f<o.length;f++){let u=o[f],h=s.get(u.poolId);if(h==null)throw new Error("Pool does not exit");let w=h.parsePoolPair(u.assetIn,u.assetOut),O;f>0?O=n[f-1].amountOut:O=Z(D(e),w.decimalsIn);let{amountOut:N,calculatedOut:T,feePct:E}=h.validateSell(w,O),M=h.spotPriceOutGivenIn(w),R=Me(O,w.decimalsIn,M,T);n.push({...u,assetInDecimals:w.decimalsIn,assetOutDecimals:w.decimalsOut,amountIn:O,calculatedOut:T,amountOut:N,spotPrice:M,tradeFeePct:E,priceImpactPct:R.toNumber(),toHuman(){return{...u,amountIn:j(O,w.decimalsIn),calculatedOut:j(T,w.decimalsOut),amountOut:j(N,w.decimalsOut),spotPrice:j(M,w.decimalsOut),tradeFeePct:E,priceImpactPct:R.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:f}=await super.validateTokenPair(e,o,n),w=super.getPaths(e,o,f,n).map(v=>this.toBuySwaps(s,v,f)).sort((v,L)=>{let J=v[0].amountIn,y=L[0].amountIn;return J.isGreaterThan(y)?1:-1})[0],O=w[w.length-1],N=w[0],T=w.map(v=>v.spotPrice.shiftedBy(-1*v.assetInDecimals)).reduce((v,L)=>v.multipliedBy(L)),E=Z(T,N.assetInDecimals).decimalPlaces(0,1),M=Me(O.amountOut,O.assetOutDecimals,E,N.calculatedIn),R=v=>this.poolService.buildBuyTx(e,o,O.amountOut,v,w.map(L=>L));return{type:"Buy",amountOut:O.amountOut,amountIn:N.amountIn,spotPrice:E,priceImpactPct:M.toNumber(),swaps:w,toTx:R,toHuman(){return{type:"Buy",amountOut:j(O.amountOut,O.assetOutDecimals),amountIn:j(N.amountIn,N.assetInDecimals),spotPrice:j(E,N.assetInDecimals),priceImpactPct:M.toNumber(),swaps:w.map(v=>v.toHuman())}}}}toBuySwaps(e,o,s){let n=[];for(let f=o.length-1;f>=0;f--){let u=o[f],h=s.get(u.poolId);if(h==null)throw new Error("Pool does not exit");let w=h.parsePoolPair(u.assetIn,u.assetOut),O;f==o.length-1?O=Z(D(e),w.decimalsOut):O=n[0].amountIn;let{amountIn:N,calculatedIn:T,feePct:E}=h.validateBuy(w,O),M=h.spotPriceInGivenOut(w),R=Me(O,w.decimalsOut,M,T);n.unshift({...u,assetInDecimals:w.decimalsIn,assetOutDecimals:w.decimalsOut,amountOut:O,calculatedIn:T,amountIn:N,spotPrice:M,tradeFeePct:E,priceImpactPct:R.toNumber(),toHuman(){return{...u,amountOut:j(O,w.decimalsOut),calculatedIn:j(T,w.decimalsIn),amountIn:j(N,w.decimalsIn),spotPrice:j(M,w.decimalsIn),tradeFeePct:E,priceImpactPct:R.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
|