@cetusprotocol/dlmm-sdk 0.1.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var kr=Object.create;var De=Object.defineProperty;var Rr=Object.getOwnPropertyDescriptor;var Dr=Object.getOwnPropertyNames;var Fr=Object.getPrototypeOf,Nr=Object.prototype.hasOwnProperty;var qr=(n,i)=>()=>(i||n((i={exports:{}}).exports,i),i.exports),Lr=(n,i)=>{for(var t in i)De(n,t,{get:i[t],enumerable:!0})},ar=(n,i,t,r)=>{if(i&&typeof i=="object"||typeof i=="function")for(let e of Dr(i))!Nr.call(n,e)&&e!==t&&De(n,e,{get:()=>i[e],enumerable:!(r=Rr(i,e))||r.enumerable});return n};var Pi=(n,i,t)=>(t=n!=null?kr(Fr(n)):{},ar(i||!n||!n.__esModule?De(t,"default",{value:n,enumerable:!0}):t,n)),jr=n=>ar(De({},"__esModule",{value:!0}),n);var Ne=qr((ur,Bi)=>{"use strict";(function(n,i){"use strict";function t(v,o){if(!v)throw new Error(o||"Assertion failed")}function r(v,o){v.super_=o;var u=function(){};u.prototype=o.prototype,v.prototype=new u,v.prototype.constructor=v}function e(v,o,u){if(e.isBN(v))return v;this.negative=0,this.words=null,this.length=0,this.red=null,v!==null&&((o==="le"||o==="be")&&(u=o,o=10),this._init(v||0,o||10,u||"be"))}typeof n=="object"?n.exports=e:i.BN=e,e.BN=e,e.wordSize=26;var s;try{typeof window<"u"&&typeof window.Buffer<"u"?s=window.Buffer:s=require("buffer").Buffer}catch{}e.isBN=function(o){return o instanceof e?!0:o!==null&&typeof o=="object"&&o.constructor.wordSize===e.wordSize&&Array.isArray(o.words)},e.max=function(o,u){return o.cmp(u)>0?o:u},e.min=function(o,u){return o.cmp(u)<0?o:u},e.prototype._init=function(o,u,h){if(typeof o=="number")return this._initNumber(o,u,h);if(typeof o=="object")return this._initArray(o,u,h);u==="hex"&&(u=16),t(u===(u|0)&&u>=2&&u<=36),o=o.toString().replace(/\s+/g,"");var m=0;o[0]==="-"&&(m++,this.negative=1),m<o.length&&(u===16?this._parseHex(o,m,h):(this._parseBase(o,u,m),h==="le"&&this._initArray(this.toArray(),u,h)))},e.prototype._initNumber=function(o,u,h){o<0&&(this.negative=1,o=-o),o<67108864?(this.words=[o&67108863],this.length=1):o<4503599627370496?(this.words=[o&67108863,o/67108864&67108863],this.length=2):(t(o<9007199254740992),this.words=[o&67108863,o/67108864&67108863,1],this.length=3),h==="le"&&this._initArray(this.toArray(),u,h)},e.prototype._initArray=function(o,u,h){if(t(typeof o.length=="number"),o.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(o.length/3),this.words=new Array(this.length);for(var m=0;m<this.length;m++)this.words[m]=0;var b,M,A=0;if(h==="be")for(m=o.length-1,b=0;m>=0;m-=3)M=o[m]|o[m-1]<<8|o[m-2]<<16,this.words[b]|=M<<A&67108863,this.words[b+1]=M>>>26-A&67108863,A+=24,A>=26&&(A-=26,b++);else if(h==="le")for(m=0,b=0;m<o.length;m+=3)M=o[m]|o[m+1]<<8|o[m+2]<<16,this.words[b]|=M<<A&67108863,this.words[b+1]=M>>>26-A&67108863,A+=24,A>=26&&(A-=26,b++);return this._strip()};function l(v,o){var u=v.charCodeAt(o);if(u>=48&&u<=57)return u-48;if(u>=65&&u<=70)return u-55;if(u>=97&&u<=102)return u-87;t(!1,"Invalid character in "+v)}function d(v,o,u){var h=l(v,u);return u-1>=o&&(h|=l(v,u-1)<<4),h}e.prototype._parseHex=function(o,u,h){this.length=Math.ceil((o.length-u)/6),this.words=new Array(this.length);for(var m=0;m<this.length;m++)this.words[m]=0;var b=0,M=0,A;if(h==="be")for(m=o.length-1;m>=u;m-=2)A=d(o,u,m)<<b,this.words[M]|=A&67108863,b>=18?(b-=18,M+=1,this.words[M]|=A>>>26):b+=8;else{var g=o.length-u;for(m=g%2===0?u+1:u;m<o.length;m+=2)A=d(o,u,m)<<b,this.words[M]|=A&67108863,b>=18?(b-=18,M+=1,this.words[M]|=A>>>26):b+=8}this._strip()};function f(v,o,u,h){for(var m=0,b=0,M=Math.min(v.length,u),A=o;A<M;A++){var g=v.charCodeAt(A)-48;m*=h,g>=49?b=g-49+10:g>=17?b=g-17+10:b=g,t(g>=0&&b<h,"Invalid character"),m+=b}return m}e.prototype._parseBase=function(o,u,h){this.words=[0],this.length=1;for(var m=0,b=1;b<=67108863;b*=u)m++;m--,b=b/u|0;for(var M=o.length-h,A=M%m,g=Math.min(M,M-A)+h,a=0,w=h;w<g;w+=m)a=f(o,w,w+m,u),this.imuln(b),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a);if(A!==0){var F=1;for(a=f(o,w,o.length,u),w=0;w<A;w++)F*=u;this.imuln(F),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a)}this._strip()},e.prototype.copy=function(o){o.words=new Array(this.length);for(var u=0;u<this.length;u++)o.words[u]=this.words[u];o.length=this.length,o.negative=this.negative,o.red=this.red};function c(v,o){v.words=o.words,v.length=o.length,v.negative=o.negative,v.red=o.red}if(e.prototype._move=function(o){c(o,this)},e.prototype.clone=function(){var o=new e(null);return this.copy(o),o},e.prototype._expand=function(o){for(;this.length<o;)this.words[this.length++]=0;return this},e.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},e.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{e.prototype[Symbol.for("nodejs.util.inspect.custom")]=p}catch{e.prototype.inspect=p}else e.prototype.inspect=p;function p(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var _=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],y=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],P=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];e.prototype.toString=function(o,u){o=o||10,u=u|0||1;var h;if(o===16||o==="hex"){h="";for(var m=0,b=0,M=0;M<this.length;M++){var A=this.words[M],g=((A<<m|b)&16777215).toString(16);b=A>>>24-m&16777215,m+=2,m>=26&&(m-=26,M--),b!==0||M!==this.length-1?h=_[6-g.length]+g+h:h=g+h}for(b!==0&&(h=b.toString(16)+h);h.length%u!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}if(o===(o|0)&&o>=2&&o<=36){var a=y[o],w=P[o];h="";var F=this.clone();for(F.negative=0;!F.isZero();){var N=F.modrn(w).toString(o);F=F.idivn(w),F.isZero()?h=N+h:h=_[a-N.length]+N+h}for(this.isZero()&&(h="0"+h);h.length%u!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}t(!1,"Base should be between 2 and 36")},e.prototype.toNumber=function(){var o=this.words[0];return this.length===2?o+=this.words[1]*67108864:this.length===3&&this.words[2]===1?o+=4503599627370496+this.words[1]*67108864:this.length>2&&t(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-o:o},e.prototype.toJSON=function(){return this.toString(16,2)},s&&(e.prototype.toBuffer=function(o,u){return this.toArrayLike(s,o,u)}),e.prototype.toArray=function(o,u){return this.toArrayLike(Array,o,u)};var B=function(o,u){return o.allocUnsafe?o.allocUnsafe(u):new o(u)};e.prototype.toArrayLike=function(o,u,h){this._strip();var m=this.byteLength(),b=h||Math.max(1,m);t(m<=b,"byte array longer than desired length"),t(b>0,"Requested array length <= 0");var M=B(o,b),A=u==="le"?"LE":"BE";return this["_toArrayLike"+A](M,m),M},e.prototype._toArrayLikeLE=function(o,u){for(var h=0,m=0,b=0,M=0;b<this.length;b++){var A=this.words[b]<<M|m;o[h++]=A&255,h<o.length&&(o[h++]=A>>8&255),h<o.length&&(o[h++]=A>>16&255),M===6?(h<o.length&&(o[h++]=A>>24&255),m=0,M=0):(m=A>>>24,M+=2)}if(h<o.length)for(o[h++]=m;h<o.length;)o[h++]=0},e.prototype._toArrayLikeBE=function(o,u){for(var h=o.length-1,m=0,b=0,M=0;b<this.length;b++){var A=this.words[b]<<M|m;o[h--]=A&255,h>=0&&(o[h--]=A>>8&255),h>=0&&(o[h--]=A>>16&255),M===6?(h>=0&&(o[h--]=A>>24&255),m=0,M=0):(m=A>>>24,M+=2)}if(h>=0)for(o[h--]=m;h>=0;)o[h--]=0},Math.clz32?e.prototype._countBits=function(o){return 32-Math.clz32(o)}:e.prototype._countBits=function(o){var u=o,h=0;return u>=4096&&(h+=13,u>>>=13),u>=64&&(h+=7,u>>>=7),u>=8&&(h+=4,u>>>=4),u>=2&&(h+=2,u>>>=2),h+u},e.prototype._zeroBits=function(o){if(o===0)return 26;var u=o,h=0;return(u&8191)===0&&(h+=13,u>>>=13),(u&127)===0&&(h+=7,u>>>=7),(u&15)===0&&(h+=4,u>>>=4),(u&3)===0&&(h+=2,u>>>=2),(u&1)===0&&h++,h},e.prototype.bitLength=function(){var o=this.words[this.length-1],u=this._countBits(o);return(this.length-1)*26+u};function S(v){for(var o=new Array(v.bitLength()),u=0;u<o.length;u++){var h=u/26|0,m=u%26;o[u]=v.words[h]>>>m&1}return o}e.prototype.zeroBits=function(){if(this.isZero())return 0;for(var o=0,u=0;u<this.length;u++){var h=this._zeroBits(this.words[u]);if(o+=h,h!==26)break}return o},e.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},e.prototype.toTwos=function(o){return this.negative!==0?this.abs().inotn(o).iaddn(1):this.clone()},e.prototype.fromTwos=function(o){return this.testn(o-1)?this.notn(o).iaddn(1).ineg():this.clone()},e.prototype.isNeg=function(){return this.negative!==0},e.prototype.neg=function(){return this.clone().ineg()},e.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},e.prototype.iuor=function(o){for(;this.length<o.length;)this.words[this.length++]=0;for(var u=0;u<o.length;u++)this.words[u]=this.words[u]|o.words[u];return this._strip()},e.prototype.ior=function(o){return t((this.negative|o.negative)===0),this.iuor(o)},e.prototype.or=function(o){return this.length>o.length?this.clone().ior(o):o.clone().ior(this)},e.prototype.uor=function(o){return this.length>o.length?this.clone().iuor(o):o.clone().iuor(this)},e.prototype.iuand=function(o){var u;this.length>o.length?u=o:u=this;for(var h=0;h<u.length;h++)this.words[h]=this.words[h]&o.words[h];return this.length=u.length,this._strip()},e.prototype.iand=function(o){return t((this.negative|o.negative)===0),this.iuand(o)},e.prototype.and=function(o){return this.length>o.length?this.clone().iand(o):o.clone().iand(this)},e.prototype.uand=function(o){return this.length>o.length?this.clone().iuand(o):o.clone().iuand(this)},e.prototype.iuxor=function(o){var u,h;this.length>o.length?(u=this,h=o):(u=o,h=this);for(var m=0;m<h.length;m++)this.words[m]=u.words[m]^h.words[m];if(this!==u)for(;m<u.length;m++)this.words[m]=u.words[m];return this.length=u.length,this._strip()},e.prototype.ixor=function(o){return t((this.negative|o.negative)===0),this.iuxor(o)},e.prototype.xor=function(o){return this.length>o.length?this.clone().ixor(o):o.clone().ixor(this)},e.prototype.uxor=function(o){return this.length>o.length?this.clone().iuxor(o):o.clone().iuxor(this)},e.prototype.inotn=function(o){t(typeof o=="number"&&o>=0);var u=Math.ceil(o/26)|0,h=o%26;this._expand(u),h>0&&u--;for(var m=0;m<u;m++)this.words[m]=~this.words[m]&67108863;return h>0&&(this.words[m]=~this.words[m]&67108863>>26-h),this._strip()},e.prototype.notn=function(o){return this.clone().inotn(o)},e.prototype.setn=function(o,u){t(typeof o=="number"&&o>=0);var h=o/26|0,m=o%26;return this._expand(h+1),u?this.words[h]=this.words[h]|1<<m:this.words[h]=this.words[h]&~(1<<m),this._strip()},e.prototype.iadd=function(o){var u;if(this.negative!==0&&o.negative===0)return this.negative=0,u=this.isub(o),this.negative^=1,this._normSign();if(this.negative===0&&o.negative!==0)return o.negative=0,u=this.isub(o),o.negative=1,u._normSign();var h,m;this.length>o.length?(h=this,m=o):(h=o,m=this);for(var b=0,M=0;M<m.length;M++)u=(h.words[M]|0)+(m.words[M]|0)+b,this.words[M]=u&67108863,b=u>>>26;for(;b!==0&&M<h.length;M++)u=(h.words[M]|0)+b,this.words[M]=u&67108863,b=u>>>26;if(this.length=h.length,b!==0)this.words[this.length]=b,this.length++;else if(h!==this)for(;M<h.length;M++)this.words[M]=h.words[M];return this},e.prototype.add=function(o){var u;return o.negative!==0&&this.negative===0?(o.negative=0,u=this.sub(o),o.negative^=1,u):o.negative===0&&this.negative!==0?(this.negative=0,u=o.sub(this),this.negative=1,u):this.length>o.length?this.clone().iadd(o):o.clone().iadd(this)},e.prototype.isub=function(o){if(o.negative!==0){o.negative=0;var u=this.iadd(o);return o.negative=1,u._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(o),this.negative=1,this._normSign();var h=this.cmp(o);if(h===0)return this.negative=0,this.length=1,this.words[0]=0,this;var m,b;h>0?(m=this,b=o):(m=o,b=this);for(var M=0,A=0;A<b.length;A++)u=(m.words[A]|0)-(b.words[A]|0)+M,M=u>>26,this.words[A]=u&67108863;for(;M!==0&&A<m.length;A++)u=(m.words[A]|0)+M,M=u>>26,this.words[A]=u&67108863;if(M===0&&A<m.length&&m!==this)for(;A<m.length;A++)this.words[A]=m.words[A];return this.length=Math.max(this.length,A),m!==this&&(this.negative=1),this._strip()},e.prototype.sub=function(o){return this.clone().isub(o)};function C(v,o,u){u.negative=o.negative^v.negative;var h=v.length+o.length|0;u.length=h,h=h-1|0;var m=v.words[0]|0,b=o.words[0]|0,M=m*b,A=M&67108863,g=M/67108864|0;u.words[0]=A;for(var a=1;a<h;a++){for(var w=g>>>26,F=g&67108863,N=Math.min(a,o.length-1),U=Math.max(0,a-v.length+1);U<=N;U++){var V=a-U|0;m=v.words[V]|0,b=o.words[U]|0,M=m*b+F,w+=M/67108864|0,F=M&67108863}u.words[a]=F|0,g=w|0}return g!==0?u.words[a]=g|0:u.length--,u._strip()}var k=function(o,u,h){var m=o.words,b=u.words,M=h.words,A=0,g,a,w,F=m[0]|0,N=F&8191,U=F>>>13,V=m[1]|0,tt=V&8191,L=V>>>13,ot=m[2]|0,Q=ot&8191,et=ot>>>13,Ae=m[3]|0,ht=Ae&8191,pt=Ae>>>13,Qi=m[4]|0,mt=Qi&8191,_t=Qi>>>13,Ki=m[5]|0,gt=Ki&8191,bt=Ki>>>13,$i=m[6]|0,wt=$i&8191,vt=$i>>>13,zi=m[7]|0,yt=zi&8191,Mt=zi>>>13,Gi=m[8]|0,Pt=Gi&8191,At=Gi>>>13,Vi=m[9]|0,Bt=Vi&8191,St=Vi>>>13,Xi=b[0]|0,It=Xi&8191,Et=Xi>>>13,Ji=b[1]|0,Ot=Ji&8191,xt=Ji>>>13,Yi=b[2]|0,Tt=Yi&8191,Ct=Yi>>>13,tr=b[3]|0,kt=tr&8191,Rt=tr>>>13,er=b[4]|0,Dt=er&8191,Ft=er>>>13,ir=b[5]|0,Nt=ir&8191,qt=ir>>>13,rr=b[6]|0,Lt=rr&8191,jt=rr>>>13,nr=b[7]|0,Wt=nr&8191,Zt=nr>>>13,or=b[8]|0,Ut=or&8191,Ht=or>>>13,sr=b[9]|0,Qt=sr&8191,Kt=sr>>>13;h.negative=o.negative^u.negative,h.length=19,g=Math.imul(N,It),a=Math.imul(N,Et),a=a+Math.imul(U,It)|0,w=Math.imul(U,Et);var ni=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ni>>>26)|0,ni&=67108863,g=Math.imul(tt,It),a=Math.imul(tt,Et),a=a+Math.imul(L,It)|0,w=Math.imul(L,Et),g=g+Math.imul(N,Ot)|0,a=a+Math.imul(N,xt)|0,a=a+Math.imul(U,Ot)|0,w=w+Math.imul(U,xt)|0;var oi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(oi>>>26)|0,oi&=67108863,g=Math.imul(Q,It),a=Math.imul(Q,Et),a=a+Math.imul(et,It)|0,w=Math.imul(et,Et),g=g+Math.imul(tt,Ot)|0,a=a+Math.imul(tt,xt)|0,a=a+Math.imul(L,Ot)|0,w=w+Math.imul(L,xt)|0,g=g+Math.imul(N,Tt)|0,a=a+Math.imul(N,Ct)|0,a=a+Math.imul(U,Tt)|0,w=w+Math.imul(U,Ct)|0;var si=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(si>>>26)|0,si&=67108863,g=Math.imul(ht,It),a=Math.imul(ht,Et),a=a+Math.imul(pt,It)|0,w=Math.imul(pt,Et),g=g+Math.imul(Q,Ot)|0,a=a+Math.imul(Q,xt)|0,a=a+Math.imul(et,Ot)|0,w=w+Math.imul(et,xt)|0,g=g+Math.imul(tt,Tt)|0,a=a+Math.imul(tt,Ct)|0,a=a+Math.imul(L,Tt)|0,w=w+Math.imul(L,Ct)|0,g=g+Math.imul(N,kt)|0,a=a+Math.imul(N,Rt)|0,a=a+Math.imul(U,kt)|0,w=w+Math.imul(U,Rt)|0;var ai=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ai>>>26)|0,ai&=67108863,g=Math.imul(mt,It),a=Math.imul(mt,Et),a=a+Math.imul(_t,It)|0,w=Math.imul(_t,Et),g=g+Math.imul(ht,Ot)|0,a=a+Math.imul(ht,xt)|0,a=a+Math.imul(pt,Ot)|0,w=w+Math.imul(pt,xt)|0,g=g+Math.imul(Q,Tt)|0,a=a+Math.imul(Q,Ct)|0,a=a+Math.imul(et,Tt)|0,w=w+Math.imul(et,Ct)|0,g=g+Math.imul(tt,kt)|0,a=a+Math.imul(tt,Rt)|0,a=a+Math.imul(L,kt)|0,w=w+Math.imul(L,Rt)|0,g=g+Math.imul(N,Dt)|0,a=a+Math.imul(N,Ft)|0,a=a+Math.imul(U,Dt)|0,w=w+Math.imul(U,Ft)|0;var li=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(li>>>26)|0,li&=67108863,g=Math.imul(gt,It),a=Math.imul(gt,Et),a=a+Math.imul(bt,It)|0,w=Math.imul(bt,Et),g=g+Math.imul(mt,Ot)|0,a=a+Math.imul(mt,xt)|0,a=a+Math.imul(_t,Ot)|0,w=w+Math.imul(_t,xt)|0,g=g+Math.imul(ht,Tt)|0,a=a+Math.imul(ht,Ct)|0,a=a+Math.imul(pt,Tt)|0,w=w+Math.imul(pt,Ct)|0,g=g+Math.imul(Q,kt)|0,a=a+Math.imul(Q,Rt)|0,a=a+Math.imul(et,kt)|0,w=w+Math.imul(et,Rt)|0,g=g+Math.imul(tt,Dt)|0,a=a+Math.imul(tt,Ft)|0,a=a+Math.imul(L,Dt)|0,w=w+Math.imul(L,Ft)|0,g=g+Math.imul(N,Nt)|0,a=a+Math.imul(N,qt)|0,a=a+Math.imul(U,Nt)|0,w=w+Math.imul(U,qt)|0;var ui=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ui>>>26)|0,ui&=67108863,g=Math.imul(wt,It),a=Math.imul(wt,Et),a=a+Math.imul(vt,It)|0,w=Math.imul(vt,Et),g=g+Math.imul(gt,Ot)|0,a=a+Math.imul(gt,xt)|0,a=a+Math.imul(bt,Ot)|0,w=w+Math.imul(bt,xt)|0,g=g+Math.imul(mt,Tt)|0,a=a+Math.imul(mt,Ct)|0,a=a+Math.imul(_t,Tt)|0,w=w+Math.imul(_t,Ct)|0,g=g+Math.imul(ht,kt)|0,a=a+Math.imul(ht,Rt)|0,a=a+Math.imul(pt,kt)|0,w=w+Math.imul(pt,Rt)|0,g=g+Math.imul(Q,Dt)|0,a=a+Math.imul(Q,Ft)|0,a=a+Math.imul(et,Dt)|0,w=w+Math.imul(et,Ft)|0,g=g+Math.imul(tt,Nt)|0,a=a+Math.imul(tt,qt)|0,a=a+Math.imul(L,Nt)|0,w=w+Math.imul(L,qt)|0,g=g+Math.imul(N,Lt)|0,a=a+Math.imul(N,jt)|0,a=a+Math.imul(U,Lt)|0,w=w+Math.imul(U,jt)|0;var di=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(di>>>26)|0,di&=67108863,g=Math.imul(yt,It),a=Math.imul(yt,Et),a=a+Math.imul(Mt,It)|0,w=Math.imul(Mt,Et),g=g+Math.imul(wt,Ot)|0,a=a+Math.imul(wt,xt)|0,a=a+Math.imul(vt,Ot)|0,w=w+Math.imul(vt,xt)|0,g=g+Math.imul(gt,Tt)|0,a=a+Math.imul(gt,Ct)|0,a=a+Math.imul(bt,Tt)|0,w=w+Math.imul(bt,Ct)|0,g=g+Math.imul(mt,kt)|0,a=a+Math.imul(mt,Rt)|0,a=a+Math.imul(_t,kt)|0,w=w+Math.imul(_t,Rt)|0,g=g+Math.imul(ht,Dt)|0,a=a+Math.imul(ht,Ft)|0,a=a+Math.imul(pt,Dt)|0,w=w+Math.imul(pt,Ft)|0,g=g+Math.imul(Q,Nt)|0,a=a+Math.imul(Q,qt)|0,a=a+Math.imul(et,Nt)|0,w=w+Math.imul(et,qt)|0,g=g+Math.imul(tt,Lt)|0,a=a+Math.imul(tt,jt)|0,a=a+Math.imul(L,Lt)|0,w=w+Math.imul(L,jt)|0,g=g+Math.imul(N,Wt)|0,a=a+Math.imul(N,Zt)|0,a=a+Math.imul(U,Wt)|0,w=w+Math.imul(U,Zt)|0;var fi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(fi>>>26)|0,fi&=67108863,g=Math.imul(Pt,It),a=Math.imul(Pt,Et),a=a+Math.imul(At,It)|0,w=Math.imul(At,Et),g=g+Math.imul(yt,Ot)|0,a=a+Math.imul(yt,xt)|0,a=a+Math.imul(Mt,Ot)|0,w=w+Math.imul(Mt,xt)|0,g=g+Math.imul(wt,Tt)|0,a=a+Math.imul(wt,Ct)|0,a=a+Math.imul(vt,Tt)|0,w=w+Math.imul(vt,Ct)|0,g=g+Math.imul(gt,kt)|0,a=a+Math.imul(gt,Rt)|0,a=a+Math.imul(bt,kt)|0,w=w+Math.imul(bt,Rt)|0,g=g+Math.imul(mt,Dt)|0,a=a+Math.imul(mt,Ft)|0,a=a+Math.imul(_t,Dt)|0,w=w+Math.imul(_t,Ft)|0,g=g+Math.imul(ht,Nt)|0,a=a+Math.imul(ht,qt)|0,a=a+Math.imul(pt,Nt)|0,w=w+Math.imul(pt,qt)|0,g=g+Math.imul(Q,Lt)|0,a=a+Math.imul(Q,jt)|0,a=a+Math.imul(et,Lt)|0,w=w+Math.imul(et,jt)|0,g=g+Math.imul(tt,Wt)|0,a=a+Math.imul(tt,Zt)|0,a=a+Math.imul(L,Wt)|0,w=w+Math.imul(L,Zt)|0,g=g+Math.imul(N,Ut)|0,a=a+Math.imul(N,Ht)|0,a=a+Math.imul(U,Ut)|0,w=w+Math.imul(U,Ht)|0;var ci=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ci>>>26)|0,ci&=67108863,g=Math.imul(Bt,It),a=Math.imul(Bt,Et),a=a+Math.imul(St,It)|0,w=Math.imul(St,Et),g=g+Math.imul(Pt,Ot)|0,a=a+Math.imul(Pt,xt)|0,a=a+Math.imul(At,Ot)|0,w=w+Math.imul(At,xt)|0,g=g+Math.imul(yt,Tt)|0,a=a+Math.imul(yt,Ct)|0,a=a+Math.imul(Mt,Tt)|0,w=w+Math.imul(Mt,Ct)|0,g=g+Math.imul(wt,kt)|0,a=a+Math.imul(wt,Rt)|0,a=a+Math.imul(vt,kt)|0,w=w+Math.imul(vt,Rt)|0,g=g+Math.imul(gt,Dt)|0,a=a+Math.imul(gt,Ft)|0,a=a+Math.imul(bt,Dt)|0,w=w+Math.imul(bt,Ft)|0,g=g+Math.imul(mt,Nt)|0,a=a+Math.imul(mt,qt)|0,a=a+Math.imul(_t,Nt)|0,w=w+Math.imul(_t,qt)|0,g=g+Math.imul(ht,Lt)|0,a=a+Math.imul(ht,jt)|0,a=a+Math.imul(pt,Lt)|0,w=w+Math.imul(pt,jt)|0,g=g+Math.imul(Q,Wt)|0,a=a+Math.imul(Q,Zt)|0,a=a+Math.imul(et,Wt)|0,w=w+Math.imul(et,Zt)|0,g=g+Math.imul(tt,Ut)|0,a=a+Math.imul(tt,Ht)|0,a=a+Math.imul(L,Ut)|0,w=w+Math.imul(L,Ht)|0,g=g+Math.imul(N,Qt)|0,a=a+Math.imul(N,Kt)|0,a=a+Math.imul(U,Qt)|0,w=w+Math.imul(U,Kt)|0;var hi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(hi>>>26)|0,hi&=67108863,g=Math.imul(Bt,Ot),a=Math.imul(Bt,xt),a=a+Math.imul(St,Ot)|0,w=Math.imul(St,xt),g=g+Math.imul(Pt,Tt)|0,a=a+Math.imul(Pt,Ct)|0,a=a+Math.imul(At,Tt)|0,w=w+Math.imul(At,Ct)|0,g=g+Math.imul(yt,kt)|0,a=a+Math.imul(yt,Rt)|0,a=a+Math.imul(Mt,kt)|0,w=w+Math.imul(Mt,Rt)|0,g=g+Math.imul(wt,Dt)|0,a=a+Math.imul(wt,Ft)|0,a=a+Math.imul(vt,Dt)|0,w=w+Math.imul(vt,Ft)|0,g=g+Math.imul(gt,Nt)|0,a=a+Math.imul(gt,qt)|0,a=a+Math.imul(bt,Nt)|0,w=w+Math.imul(bt,qt)|0,g=g+Math.imul(mt,Lt)|0,a=a+Math.imul(mt,jt)|0,a=a+Math.imul(_t,Lt)|0,w=w+Math.imul(_t,jt)|0,g=g+Math.imul(ht,Wt)|0,a=a+Math.imul(ht,Zt)|0,a=a+Math.imul(pt,Wt)|0,w=w+Math.imul(pt,Zt)|0,g=g+Math.imul(Q,Ut)|0,a=a+Math.imul(Q,Ht)|0,a=a+Math.imul(et,Ut)|0,w=w+Math.imul(et,Ht)|0,g=g+Math.imul(tt,Qt)|0,a=a+Math.imul(tt,Kt)|0,a=a+Math.imul(L,Qt)|0,w=w+Math.imul(L,Kt)|0;var pi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(pi>>>26)|0,pi&=67108863,g=Math.imul(Bt,Tt),a=Math.imul(Bt,Ct),a=a+Math.imul(St,Tt)|0,w=Math.imul(St,Ct),g=g+Math.imul(Pt,kt)|0,a=a+Math.imul(Pt,Rt)|0,a=a+Math.imul(At,kt)|0,w=w+Math.imul(At,Rt)|0,g=g+Math.imul(yt,Dt)|0,a=a+Math.imul(yt,Ft)|0,a=a+Math.imul(Mt,Dt)|0,w=w+Math.imul(Mt,Ft)|0,g=g+Math.imul(wt,Nt)|0,a=a+Math.imul(wt,qt)|0,a=a+Math.imul(vt,Nt)|0,w=w+Math.imul(vt,qt)|0,g=g+Math.imul(gt,Lt)|0,a=a+Math.imul(gt,jt)|0,a=a+Math.imul(bt,Lt)|0,w=w+Math.imul(bt,jt)|0,g=g+Math.imul(mt,Wt)|0,a=a+Math.imul(mt,Zt)|0,a=a+Math.imul(_t,Wt)|0,w=w+Math.imul(_t,Zt)|0,g=g+Math.imul(ht,Ut)|0,a=a+Math.imul(ht,Ht)|0,a=a+Math.imul(pt,Ut)|0,w=w+Math.imul(pt,Ht)|0,g=g+Math.imul(Q,Qt)|0,a=a+Math.imul(Q,Kt)|0,a=a+Math.imul(et,Qt)|0,w=w+Math.imul(et,Kt)|0;var mi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(mi>>>26)|0,mi&=67108863,g=Math.imul(Bt,kt),a=Math.imul(Bt,Rt),a=a+Math.imul(St,kt)|0,w=Math.imul(St,Rt),g=g+Math.imul(Pt,Dt)|0,a=a+Math.imul(Pt,Ft)|0,a=a+Math.imul(At,Dt)|0,w=w+Math.imul(At,Ft)|0,g=g+Math.imul(yt,Nt)|0,a=a+Math.imul(yt,qt)|0,a=a+Math.imul(Mt,Nt)|0,w=w+Math.imul(Mt,qt)|0,g=g+Math.imul(wt,Lt)|0,a=a+Math.imul(wt,jt)|0,a=a+Math.imul(vt,Lt)|0,w=w+Math.imul(vt,jt)|0,g=g+Math.imul(gt,Wt)|0,a=a+Math.imul(gt,Zt)|0,a=a+Math.imul(bt,Wt)|0,w=w+Math.imul(bt,Zt)|0,g=g+Math.imul(mt,Ut)|0,a=a+Math.imul(mt,Ht)|0,a=a+Math.imul(_t,Ut)|0,w=w+Math.imul(_t,Ht)|0,g=g+Math.imul(ht,Qt)|0,a=a+Math.imul(ht,Kt)|0,a=a+Math.imul(pt,Qt)|0,w=w+Math.imul(pt,Kt)|0;var _i=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(_i>>>26)|0,_i&=67108863,g=Math.imul(Bt,Dt),a=Math.imul(Bt,Ft),a=a+Math.imul(St,Dt)|0,w=Math.imul(St,Ft),g=g+Math.imul(Pt,Nt)|0,a=a+Math.imul(Pt,qt)|0,a=a+Math.imul(At,Nt)|0,w=w+Math.imul(At,qt)|0,g=g+Math.imul(yt,Lt)|0,a=a+Math.imul(yt,jt)|0,a=a+Math.imul(Mt,Lt)|0,w=w+Math.imul(Mt,jt)|0,g=g+Math.imul(wt,Wt)|0,a=a+Math.imul(wt,Zt)|0,a=a+Math.imul(vt,Wt)|0,w=w+Math.imul(vt,Zt)|0,g=g+Math.imul(gt,Ut)|0,a=a+Math.imul(gt,Ht)|0,a=a+Math.imul(bt,Ut)|0,w=w+Math.imul(bt,Ht)|0,g=g+Math.imul(mt,Qt)|0,a=a+Math.imul(mt,Kt)|0,a=a+Math.imul(_t,Qt)|0,w=w+Math.imul(_t,Kt)|0;var gi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(gi>>>26)|0,gi&=67108863,g=Math.imul(Bt,Nt),a=Math.imul(Bt,qt),a=a+Math.imul(St,Nt)|0,w=Math.imul(St,qt),g=g+Math.imul(Pt,Lt)|0,a=a+Math.imul(Pt,jt)|0,a=a+Math.imul(At,Lt)|0,w=w+Math.imul(At,jt)|0,g=g+Math.imul(yt,Wt)|0,a=a+Math.imul(yt,Zt)|0,a=a+Math.imul(Mt,Wt)|0,w=w+Math.imul(Mt,Zt)|0,g=g+Math.imul(wt,Ut)|0,a=a+Math.imul(wt,Ht)|0,a=a+Math.imul(vt,Ut)|0,w=w+Math.imul(vt,Ht)|0,g=g+Math.imul(gt,Qt)|0,a=a+Math.imul(gt,Kt)|0,a=a+Math.imul(bt,Qt)|0,w=w+Math.imul(bt,Kt)|0;var bi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(bi>>>26)|0,bi&=67108863,g=Math.imul(Bt,Lt),a=Math.imul(Bt,jt),a=a+Math.imul(St,Lt)|0,w=Math.imul(St,jt),g=g+Math.imul(Pt,Wt)|0,a=a+Math.imul(Pt,Zt)|0,a=a+Math.imul(At,Wt)|0,w=w+Math.imul(At,Zt)|0,g=g+Math.imul(yt,Ut)|0,a=a+Math.imul(yt,Ht)|0,a=a+Math.imul(Mt,Ut)|0,w=w+Math.imul(Mt,Ht)|0,g=g+Math.imul(wt,Qt)|0,a=a+Math.imul(wt,Kt)|0,a=a+Math.imul(vt,Qt)|0,w=w+Math.imul(vt,Kt)|0;var wi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(wi>>>26)|0,wi&=67108863,g=Math.imul(Bt,Wt),a=Math.imul(Bt,Zt),a=a+Math.imul(St,Wt)|0,w=Math.imul(St,Zt),g=g+Math.imul(Pt,Ut)|0,a=a+Math.imul(Pt,Ht)|0,a=a+Math.imul(At,Ut)|0,w=w+Math.imul(At,Ht)|0,g=g+Math.imul(yt,Qt)|0,a=a+Math.imul(yt,Kt)|0,a=a+Math.imul(Mt,Qt)|0,w=w+Math.imul(Mt,Kt)|0;var vi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(vi>>>26)|0,vi&=67108863,g=Math.imul(Bt,Ut),a=Math.imul(Bt,Ht),a=a+Math.imul(St,Ut)|0,w=Math.imul(St,Ht),g=g+Math.imul(Pt,Qt)|0,a=a+Math.imul(Pt,Kt)|0,a=a+Math.imul(At,Qt)|0,w=w+Math.imul(At,Kt)|0;var yi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(yi>>>26)|0,yi&=67108863,g=Math.imul(Bt,Qt),a=Math.imul(Bt,Kt),a=a+Math.imul(St,Qt)|0,w=Math.imul(St,Kt);var Mi=(A+g|0)+((a&8191)<<13)|0;return A=(w+(a>>>13)|0)+(Mi>>>26)|0,Mi&=67108863,M[0]=ni,M[1]=oi,M[2]=si,M[3]=ai,M[4]=li,M[5]=ui,M[6]=di,M[7]=fi,M[8]=ci,M[9]=hi,M[10]=pi,M[11]=mi,M[12]=_i,M[13]=gi,M[14]=bi,M[15]=wi,M[16]=vi,M[17]=yi,M[18]=Mi,A!==0&&(M[19]=A,h.length++),h};Math.imul||(k=C);function R(v,o,u){u.negative=o.negative^v.negative,u.length=v.length+o.length;for(var h=0,m=0,b=0;b<u.length-1;b++){var M=m;m=0;for(var A=h&67108863,g=Math.min(b,o.length-1),a=Math.max(0,b-v.length+1);a<=g;a++){var w=b-a,F=v.words[w]|0,N=o.words[a]|0,U=F*N,V=U&67108863;M=M+(U/67108864|0)|0,V=V+A|0,A=V&67108863,M=M+(V>>>26)|0,m+=M>>>26,M&=67108863}u.words[b]=A,h=M,M=m}return h!==0?u.words[b]=h:u.length--,u._strip()}function x(v,o,u){return R(v,o,u)}e.prototype.mulTo=function(o,u){var h,m=this.length+o.length;return this.length===10&&o.length===10?h=k(this,o,u):m<63?h=C(this,o,u):m<1024?h=R(this,o,u):h=x(this,o,u),h};function T(v,o){this.x=v,this.y=o}T.prototype.makeRBT=function(o){for(var u=new Array(o),h=e.prototype._countBits(o)-1,m=0;m<o;m++)u[m]=this.revBin(m,h,o);return u},T.prototype.revBin=function(o,u,h){if(o===0||o===h-1)return o;for(var m=0,b=0;b<u;b++)m|=(o&1)<<u-b-1,o>>=1;return m},T.prototype.permute=function(o,u,h,m,b,M){for(var A=0;A<M;A++)m[A]=u[o[A]],b[A]=h[o[A]]},T.prototype.transform=function(o,u,h,m,b,M){this.permute(M,o,u,h,m,b);for(var A=1;A<b;A<<=1)for(var g=A<<1,a=Math.cos(2*Math.PI/g),w=Math.sin(2*Math.PI/g),F=0;F<b;F+=g)for(var N=a,U=w,V=0;V<A;V++){var tt=h[F+V],L=m[F+V],ot=h[F+V+A],Q=m[F+V+A],et=N*ot-U*Q;Q=N*Q+U*ot,ot=et,h[F+V]=tt+ot,m[F+V]=L+Q,h[F+V+A]=tt-ot,m[F+V+A]=L-Q,V!==g&&(et=a*N-w*U,U=a*U+w*N,N=et)}},T.prototype.guessLen13b=function(o,u){var h=Math.max(u,o)|1,m=h&1,b=0;for(h=h/2|0;h;h=h>>>1)b++;return 1<<b+1+m},T.prototype.conjugate=function(o,u,h){if(!(h<=1))for(var m=0;m<h/2;m++){var b=o[m];o[m]=o[h-m-1],o[h-m-1]=b,b=u[m],u[m]=-u[h-m-1],u[h-m-1]=-b}},T.prototype.normalize13b=function(o,u){for(var h=0,m=0;m<u/2;m++){var b=Math.round(o[2*m+1]/u)*8192+Math.round(o[2*m]/u)+h;o[m]=b&67108863,b<67108864?h=0:h=b/67108864|0}return o},T.prototype.convert13b=function(o,u,h,m){for(var b=0,M=0;M<u;M++)b=b+(o[M]|0),h[2*M]=b&8191,b=b>>>13,h[2*M+1]=b&8191,b=b>>>13;for(M=2*u;M<m;++M)h[M]=0;t(b===0),t((b&-8192)===0)},T.prototype.stub=function(o){for(var u=new Array(o),h=0;h<o;h++)u[h]=0;return u},T.prototype.mulp=function(o,u,h){var m=2*this.guessLen13b(o.length,u.length),b=this.makeRBT(m),M=this.stub(m),A=new Array(m),g=new Array(m),a=new Array(m),w=new Array(m),F=new Array(m),N=new Array(m),U=h.words;U.length=m,this.convert13b(o.words,o.length,A,m),this.convert13b(u.words,u.length,w,m),this.transform(A,M,g,a,m,b),this.transform(w,M,F,N,m,b);for(var V=0;V<m;V++){var tt=g[V]*F[V]-a[V]*N[V];a[V]=g[V]*N[V]+a[V]*F[V],g[V]=tt}return this.conjugate(g,a,m),this.transform(g,a,U,M,m,b),this.conjugate(U,M,m),this.normalize13b(U,m),h.negative=o.negative^u.negative,h.length=o.length+u.length,h._strip()},e.prototype.mul=function(o){var u=new e(null);return u.words=new Array(this.length+o.length),this.mulTo(o,u)},e.prototype.mulf=function(o){var u=new e(null);return u.words=new Array(this.length+o.length),x(this,o,u)},e.prototype.imul=function(o){return this.clone().mulTo(o,this)},e.prototype.imuln=function(o){var u=o<0;u&&(o=-o),t(typeof o=="number"),t(o<67108864);for(var h=0,m=0;m<this.length;m++){var b=(this.words[m]|0)*o,M=(b&67108863)+(h&67108863);h>>=26,h+=b/67108864|0,h+=M>>>26,this.words[m]=M&67108863}return h!==0&&(this.words[m]=h,this.length++),this.length=o===0?1:this.length,u?this.ineg():this},e.prototype.muln=function(o){return this.clone().imuln(o)},e.prototype.sqr=function(){return this.mul(this)},e.prototype.isqr=function(){return this.imul(this.clone())},e.prototype.pow=function(o){var u=S(o);if(u.length===0)return new e(1);for(var h=this,m=0;m<u.length&&u[m]===0;m++,h=h.sqr());if(++m<u.length)for(var b=h.sqr();m<u.length;m++,b=b.sqr())u[m]!==0&&(h=h.mul(b));return h},e.prototype.iushln=function(o){t(typeof o=="number"&&o>=0);var u=o%26,h=(o-u)/26,m=67108863>>>26-u<<26-u,b;if(u!==0){var M=0;for(b=0;b<this.length;b++){var A=this.words[b]&m,g=(this.words[b]|0)-A<<u;this.words[b]=g|M,M=A>>>26-u}M&&(this.words[b]=M,this.length++)}if(h!==0){for(b=this.length-1;b>=0;b--)this.words[b+h]=this.words[b];for(b=0;b<h;b++)this.words[b]=0;this.length+=h}return this._strip()},e.prototype.ishln=function(o){return t(this.negative===0),this.iushln(o)},e.prototype.iushrn=function(o,u,h){t(typeof o=="number"&&o>=0);var m;u?m=(u-u%26)/26:m=0;var b=o%26,M=Math.min((o-b)/26,this.length),A=67108863^67108863>>>b<<b,g=h;if(m-=M,m=Math.max(0,m),g){for(var a=0;a<M;a++)g.words[a]=this.words[a];g.length=M}if(M!==0)if(this.length>M)for(this.length-=M,a=0;a<this.length;a++)this.words[a]=this.words[a+M];else this.words[0]=0,this.length=1;var w=0;for(a=this.length-1;a>=0&&(w!==0||a>=m);a--){var F=this.words[a]|0;this.words[a]=w<<26-b|F>>>b,w=F&A}return g&&w!==0&&(g.words[g.length++]=w),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},e.prototype.ishrn=function(o,u,h){return t(this.negative===0),this.iushrn(o,u,h)},e.prototype.shln=function(o){return this.clone().ishln(o)},e.prototype.ushln=function(o){return this.clone().iushln(o)},e.prototype.shrn=function(o){return this.clone().ishrn(o)},e.prototype.ushrn=function(o){return this.clone().iushrn(o)},e.prototype.testn=function(o){t(typeof o=="number"&&o>=0);var u=o%26,h=(o-u)/26,m=1<<u;if(this.length<=h)return!1;var b=this.words[h];return!!(b&m)},e.prototype.imaskn=function(o){t(typeof o=="number"&&o>=0);var u=o%26,h=(o-u)/26;if(t(this.negative===0,"imaskn works only with positive numbers"),this.length<=h)return this;if(u!==0&&h++,this.length=Math.min(h,this.length),u!==0){var m=67108863^67108863>>>u<<u;this.words[this.length-1]&=m}return this._strip()},e.prototype.maskn=function(o){return this.clone().imaskn(o)},e.prototype.iaddn=function(o){return t(typeof o=="number"),t(o<67108864),o<0?this.isubn(-o):this.negative!==0?this.length===1&&(this.words[0]|0)<=o?(this.words[0]=o-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(o),this.negative=1,this):this._iaddn(o)},e.prototype._iaddn=function(o){this.words[0]+=o;for(var u=0;u<this.length&&this.words[u]>=67108864;u++)this.words[u]-=67108864,u===this.length-1?this.words[u+1]=1:this.words[u+1]++;return this.length=Math.max(this.length,u+1),this},e.prototype.isubn=function(o){if(t(typeof o=="number"),t(o<67108864),o<0)return this.iaddn(-o);if(this.negative!==0)return this.negative=0,this.iaddn(o),this.negative=1,this;if(this.words[0]-=o,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var u=0;u<this.length&&this.words[u]<0;u++)this.words[u]+=67108864,this.words[u+1]-=1;return this._strip()},e.prototype.addn=function(o){return this.clone().iaddn(o)},e.prototype.subn=function(o){return this.clone().isubn(o)},e.prototype.iabs=function(){return this.negative=0,this},e.prototype.abs=function(){return this.clone().iabs()},e.prototype._ishlnsubmul=function(o,u,h){var m=o.length+h,b;this._expand(m);var M,A=0;for(b=0;b<o.length;b++){M=(this.words[b+h]|0)+A;var g=(o.words[b]|0)*u;M-=g&67108863,A=(M>>26)-(g/67108864|0),this.words[b+h]=M&67108863}for(;b<this.length-h;b++)M=(this.words[b+h]|0)+A,A=M>>26,this.words[b+h]=M&67108863;if(A===0)return this._strip();for(t(A===-1),A=0,b=0;b<this.length;b++)M=-(this.words[b]|0)+A,A=M>>26,this.words[b]=M&67108863;return this.negative=1,this._strip()},e.prototype._wordDiv=function(o,u){var h=this.length-o.length,m=this.clone(),b=o,M=b.words[b.length-1]|0,A=this._countBits(M);h=26-A,h!==0&&(b=b.ushln(h),m.iushln(h),M=b.words[b.length-1]|0);var g=m.length-b.length,a;if(u!=="mod"){a=new e(null),a.length=g+1,a.words=new Array(a.length);for(var w=0;w<a.length;w++)a.words[w]=0}var F=m.clone()._ishlnsubmul(b,1,g);F.negative===0&&(m=F,a&&(a.words[g]=1));for(var N=g-1;N>=0;N--){var U=(m.words[b.length+N]|0)*67108864+(m.words[b.length+N-1]|0);for(U=Math.min(U/M|0,67108863),m._ishlnsubmul(b,U,N);m.negative!==0;)U--,m.negative=0,m._ishlnsubmul(b,1,N),m.isZero()||(m.negative^=1);a&&(a.words[N]=U)}return a&&a._strip(),m._strip(),u!=="div"&&h!==0&&m.iushrn(h),{div:a||null,mod:m}},e.prototype.divmod=function(o,u,h){if(t(!o.isZero()),this.isZero())return{div:new e(0),mod:new e(0)};var m,b,M;return this.negative!==0&&o.negative===0?(M=this.neg().divmod(o,u),u!=="mod"&&(m=M.div.neg()),u!=="div"&&(b=M.mod.neg(),h&&b.negative!==0&&b.iadd(o)),{div:m,mod:b}):this.negative===0&&o.negative!==0?(M=this.divmod(o.neg(),u),u!=="mod"&&(m=M.div.neg()),{div:m,mod:M.mod}):(this.negative&o.negative)!==0?(M=this.neg().divmod(o.neg(),u),u!=="div"&&(b=M.mod.neg(),h&&b.negative!==0&&b.isub(o)),{div:M.div,mod:b}):o.length>this.length||this.cmp(o)<0?{div:new e(0),mod:this}:o.length===1?u==="div"?{div:this.divn(o.words[0]),mod:null}:u==="mod"?{div:null,mod:new e(this.modrn(o.words[0]))}:{div:this.divn(o.words[0]),mod:new e(this.modrn(o.words[0]))}:this._wordDiv(o,u)},e.prototype.div=function(o){return this.divmod(o,"div",!1).div},e.prototype.mod=function(o){return this.divmod(o,"mod",!1).mod},e.prototype.umod=function(o){return this.divmod(o,"mod",!0).mod},e.prototype.divRound=function(o){var u=this.divmod(o);if(u.mod.isZero())return u.div;var h=u.div.negative!==0?u.mod.isub(o):u.mod,m=o.ushrn(1),b=o.andln(1),M=h.cmp(m);return M<0||b===1&&M===0?u.div:u.div.negative!==0?u.div.isubn(1):u.div.iaddn(1)},e.prototype.modrn=function(o){var u=o<0;u&&(o=-o),t(o<=67108863);for(var h=(1<<26)%o,m=0,b=this.length-1;b>=0;b--)m=(h*m+(this.words[b]|0))%o;return u?-m:m},e.prototype.modn=function(o){return this.modrn(o)},e.prototype.idivn=function(o){var u=o<0;u&&(o=-o),t(o<=67108863);for(var h=0,m=this.length-1;m>=0;m--){var b=(this.words[m]|0)+h*67108864;this.words[m]=b/o|0,h=b%o}return this._strip(),u?this.ineg():this},e.prototype.divn=function(o){return this.clone().idivn(o)},e.prototype.egcd=function(o){t(o.negative===0),t(!o.isZero());var u=this,h=o.clone();u.negative!==0?u=u.umod(o):u=u.clone();for(var m=new e(1),b=new e(0),M=new e(0),A=new e(1),g=0;u.isEven()&&h.isEven();)u.iushrn(1),h.iushrn(1),++g;for(var a=h.clone(),w=u.clone();!u.isZero();){for(var F=0,N=1;(u.words[0]&N)===0&&F<26;++F,N<<=1);if(F>0)for(u.iushrn(F);F-- >0;)(m.isOdd()||b.isOdd())&&(m.iadd(a),b.isub(w)),m.iushrn(1),b.iushrn(1);for(var U=0,V=1;(h.words[0]&V)===0&&U<26;++U,V<<=1);if(U>0)for(h.iushrn(U);U-- >0;)(M.isOdd()||A.isOdd())&&(M.iadd(a),A.isub(w)),M.iushrn(1),A.iushrn(1);u.cmp(h)>=0?(u.isub(h),m.isub(M),b.isub(A)):(h.isub(u),M.isub(m),A.isub(b))}return{a:M,b:A,gcd:h.iushln(g)}},e.prototype._invmp=function(o){t(o.negative===0),t(!o.isZero());var u=this,h=o.clone();u.negative!==0?u=u.umod(o):u=u.clone();for(var m=new e(1),b=new e(0),M=h.clone();u.cmpn(1)>0&&h.cmpn(1)>0;){for(var A=0,g=1;(u.words[0]&g)===0&&A<26;++A,g<<=1);if(A>0)for(u.iushrn(A);A-- >0;)m.isOdd()&&m.iadd(M),m.iushrn(1);for(var a=0,w=1;(h.words[0]&w)===0&&a<26;++a,w<<=1);if(a>0)for(h.iushrn(a);a-- >0;)b.isOdd()&&b.iadd(M),b.iushrn(1);u.cmp(h)>=0?(u.isub(h),m.isub(b)):(h.isub(u),b.isub(m))}var F;return u.cmpn(1)===0?F=m:F=b,F.cmpn(0)<0&&F.iadd(o),F},e.prototype.gcd=function(o){if(this.isZero())return o.abs();if(o.isZero())return this.abs();var u=this.clone(),h=o.clone();u.negative=0,h.negative=0;for(var m=0;u.isEven()&&h.isEven();m++)u.iushrn(1),h.iushrn(1);do{for(;u.isEven();)u.iushrn(1);for(;h.isEven();)h.iushrn(1);var b=u.cmp(h);if(b<0){var M=u;u=h,h=M}else if(b===0||h.cmpn(1)===0)break;u.isub(h)}while(!0);return h.iushln(m)},e.prototype.invm=function(o){return this.egcd(o).a.umod(o)},e.prototype.isEven=function(){return(this.words[0]&1)===0},e.prototype.isOdd=function(){return(this.words[0]&1)===1},e.prototype.andln=function(o){return this.words[0]&o},e.prototype.bincn=function(o){t(typeof o=="number");var u=o%26,h=(o-u)/26,m=1<<u;if(this.length<=h)return this._expand(h+1),this.words[h]|=m,this;for(var b=m,M=h;b!==0&&M<this.length;M++){var A=this.words[M]|0;A+=b,b=A>>>26,A&=67108863,this.words[M]=A}return b!==0&&(this.words[M]=b,this.length++),this},e.prototype.isZero=function(){return this.length===1&&this.words[0]===0},e.prototype.cmpn=function(o){var u=o<0;if(this.negative!==0&&!u)return-1;if(this.negative===0&&u)return 1;this._strip();var h;if(this.length>1)h=1;else{u&&(o=-o),t(o<=67108863,"Number is too big");var m=this.words[0]|0;h=m===o?0:m<o?-1:1}return this.negative!==0?-h|0:h},e.prototype.cmp=function(o){if(this.negative!==0&&o.negative===0)return-1;if(this.negative===0&&o.negative!==0)return 1;var u=this.ucmp(o);return this.negative!==0?-u|0:u},e.prototype.ucmp=function(o){if(this.length>o.length)return 1;if(this.length<o.length)return-1;for(var u=0,h=this.length-1;h>=0;h--){var m=this.words[h]|0,b=o.words[h]|0;if(m!==b){m<b?u=-1:m>b&&(u=1);break}}return u},e.prototype.gtn=function(o){return this.cmpn(o)===1},e.prototype.gt=function(o){return this.cmp(o)===1},e.prototype.gten=function(o){return this.cmpn(o)>=0},e.prototype.gte=function(o){return this.cmp(o)>=0},e.prototype.ltn=function(o){return this.cmpn(o)===-1},e.prototype.lt=function(o){return this.cmp(o)===-1},e.prototype.lten=function(o){return this.cmpn(o)<=0},e.prototype.lte=function(o){return this.cmp(o)<=0},e.prototype.eqn=function(o){return this.cmpn(o)===0},e.prototype.eq=function(o){return this.cmp(o)===0},e.red=function(o){return new X(o)},e.prototype.toRed=function(o){return t(!this.red,"Already a number in reduction context"),t(this.negative===0,"red works only with positives"),o.convertTo(this)._forceRed(o)},e.prototype.fromRed=function(){return t(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},e.prototype._forceRed=function(o){return this.red=o,this},e.prototype.forceRed=function(o){return t(!this.red,"Already a number in reduction context"),this._forceRed(o)},e.prototype.redAdd=function(o){return t(this.red,"redAdd works only with red numbers"),this.red.add(this,o)},e.prototype.redIAdd=function(o){return t(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,o)},e.prototype.redSub=function(o){return t(this.red,"redSub works only with red numbers"),this.red.sub(this,o)},e.prototype.redISub=function(o){return t(this.red,"redISub works only with red numbers"),this.red.isub(this,o)},e.prototype.redShl=function(o){return t(this.red,"redShl works only with red numbers"),this.red.shl(this,o)},e.prototype.redMul=function(o){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,o),this.red.mul(this,o)},e.prototype.redIMul=function(o){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,o),this.red.imul(this,o)},e.prototype.redSqr=function(){return t(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},e.prototype.redISqr=function(){return t(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},e.prototype.redSqrt=function(){return t(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},e.prototype.redInvm=function(){return t(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},e.prototype.redNeg=function(){return t(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},e.prototype.redPow=function(o){return t(this.red&&!o.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,o)};var Z={k256:null,p224:null,p192:null,p25519:null};function j(v,o){this.name=v,this.p=new e(o,16),this.n=this.p.bitLength(),this.k=new e(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}j.prototype._tmp=function(){var o=new e(null);return o.words=new Array(Math.ceil(this.n/13)),o},j.prototype.ireduce=function(o){var u=o,h;do this.split(u,this.tmp),u=this.imulK(u),u=u.iadd(this.tmp),h=u.bitLength();while(h>this.n);var m=h<this.n?-1:u.ucmp(this.p);return m===0?(u.words[0]=0,u.length=1):m>0?u.isub(this.p):u.strip!==void 0?u.strip():u._strip(),u},j.prototype.split=function(o,u){o.iushrn(this.n,0,u)},j.prototype.imulK=function(o){return o.imul(this.k)};function Y(){j.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}r(Y,j),Y.prototype.split=function(o,u){for(var h=4194303,m=Math.min(o.length,9),b=0;b<m;b++)u.words[b]=o.words[b];if(u.length=m,o.length<=9){o.words[0]=0,o.length=1;return}var M=o.words[9];for(u.words[u.length++]=M&h,b=10;b<o.length;b++){var A=o.words[b]|0;o.words[b-10]=(A&h)<<4|M>>>22,M=A}M>>>=22,o.words[b-10]=M,M===0&&o.length>10?o.length-=10:o.length-=9},Y.prototype.imulK=function(o){o.words[o.length]=0,o.words[o.length+1]=0,o.length+=2;for(var u=0,h=0;h<o.length;h++){var m=o.words[h]|0;u+=m*977,o.words[h]=u&67108863,u=m*64+(u/67108864|0)}return o.words[o.length-1]===0&&(o.length--,o.words[o.length-1]===0&&o.length--),o};function nt(){j.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}r(nt,j);function st(){j.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}r(st,j);function dt(){j.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}r(dt,j),dt.prototype.imulK=function(o){for(var u=0,h=0;h<o.length;h++){var m=(o.words[h]|0)*19+u,b=m&67108863;m>>>=26,o.words[h]=b,u=m}return u!==0&&(o.words[o.length++]=u),o},e._prime=function(o){if(Z[o])return Z[o];var u;if(o==="k256")u=new Y;else if(o==="p224")u=new nt;else if(o==="p192")u=new st;else if(o==="p25519")u=new dt;else throw new Error("Unknown prime "+o);return Z[o]=u,u};function X(v){if(typeof v=="string"){var o=e._prime(v);this.m=o.p,this.prime=o}else t(v.gtn(1),"modulus must be greater than 1"),this.m=v,this.prime=null}X.prototype._verify1=function(o){t(o.negative===0,"red works only with positives"),t(o.red,"red works only with red numbers")},X.prototype._verify2=function(o,u){t((o.negative|u.negative)===0,"red works only with positives"),t(o.red&&o.red===u.red,"red works only with red numbers")},X.prototype.imod=function(o){return this.prime?this.prime.ireduce(o)._forceRed(this):(c(o,o.umod(this.m)._forceRed(this)),o)},X.prototype.neg=function(o){return o.isZero()?o.clone():this.m.sub(o)._forceRed(this)},X.prototype.add=function(o,u){this._verify2(o,u);var h=o.add(u);return h.cmp(this.m)>=0&&h.isub(this.m),h._forceRed(this)},X.prototype.iadd=function(o,u){this._verify2(o,u);var h=o.iadd(u);return h.cmp(this.m)>=0&&h.isub(this.m),h},X.prototype.sub=function(o,u){this._verify2(o,u);var h=o.sub(u);return h.cmpn(0)<0&&h.iadd(this.m),h._forceRed(this)},X.prototype.isub=function(o,u){this._verify2(o,u);var h=o.isub(u);return h.cmpn(0)<0&&h.iadd(this.m),h},X.prototype.shl=function(o,u){return this._verify1(o),this.imod(o.ushln(u))},X.prototype.imul=function(o,u){return this._verify2(o,u),this.imod(o.imul(u))},X.prototype.mul=function(o,u){return this._verify2(o,u),this.imod(o.mul(u))},X.prototype.isqr=function(o){return this.imul(o,o.clone())},X.prototype.sqr=function(o){return this.mul(o,o)},X.prototype.sqrt=function(o){if(o.isZero())return o.clone();var u=this.m.andln(3);if(t(u%2===1),u===3){var h=this.m.add(new e(1)).iushrn(2);return this.pow(o,h)}for(var m=this.m.subn(1),b=0;!m.isZero()&&m.andln(1)===0;)b++,m.iushrn(1);t(!m.isZero());var M=new e(1).toRed(this),A=M.redNeg(),g=this.m.subn(1).iushrn(1),a=this.m.bitLength();for(a=new e(2*a*a).toRed(this);this.pow(a,g).cmp(A)!==0;)a.redIAdd(A);for(var w=this.pow(a,m),F=this.pow(o,m.addn(1).iushrn(1)),N=this.pow(o,m),U=b;N.cmp(M)!==0;){for(var V=N,tt=0;V.cmp(M)!==0;tt++)V=V.redSqr();t(tt<U);var L=this.pow(w,new e(1).iushln(U-tt-1));F=F.redMul(L),w=L.redSqr(),N=N.redMul(w),U=tt}return F},X.prototype.invm=function(o){var u=o._invmp(this.m);return u.negative!==0?(u.negative=0,this.imod(u).redNeg()):this.imod(u)},X.prototype.pow=function(o,u){if(u.isZero())return new e(1).toRed(this);if(u.cmpn(1)===0)return o.clone();var h=4,m=new Array(1<<h);m[0]=new e(1).toRed(this),m[1]=o;for(var b=2;b<m.length;b++)m[b]=this.mul(m[b-1],o);var M=m[0],A=0,g=0,a=u.bitLength()%26;for(a===0&&(a=26),b=u.length-1;b>=0;b--){for(var w=u.words[b],F=a-1;F>=0;F--){var N=w>>F&1;if(M!==m[0]&&(M=this.sqr(M)),N===0&&A===0){g=0;continue}A<<=1,A|=N,g++,!(g!==h&&(b!==0||F!==0))&&(M=this.mul(M,m[A]),g=0,A=0)}a=26}return M},X.prototype.convertTo=function(o){var u=o.umod(this.m);return u===o?u.clone():u},X.prototype.convertFrom=function(o){var u=o.clone();return u.red=null,u},e.mont=function(o){return new lt(o)};function lt(v){X.call(this,v),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new e(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}r(lt,X),lt.prototype.convertTo=function(o){return this.imod(o.ushln(this.shift))},lt.prototype.convertFrom=function(o){var u=this.imod(o.mul(this.rinv));return u.red=null,u},lt.prototype.imul=function(o,u){if(o.isZero()||u.isZero())return o.words[0]=0,o.length=1,o;var h=o.imul(u),m=h.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),b=h.isub(m).iushrn(this.shift),M=b;return b.cmp(this.m)>=0?M=b.isub(this.m):b.cmpn(0)<0&&(M=b.iadd(this.m)),M._forceRed(this)},lt.prototype.mul=function(o,u){if(o.isZero()||u.isZero())return new e(0)._forceRed(this);var h=o.mul(u),m=h.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),b=h.isub(m).iushrn(this.shift),M=b;return b.cmp(this.m)>=0?M=b.isub(this.m):b.cmpn(0)<0&&(M=b.iadd(this.m)),M._forceRed(this)},lt.prototype.invm=function(o){var u=this.imod(o._invmp(this.m).mul(this.r2));return u._forceRed(this)}})(typeof Bi>"u"||Bi,ur)});var Kn={};Lr(Kn,{BASIS_POINT:()=>te,BASIS_POINT_MAX:()=>ue,BIN_BOUND:()=>ge,BinUtils:()=>D,CetusDlmmSDK:()=>Re,DEFAULT_MAX_WEIGHT:()=>ve,DEFAULT_MIN_WEIGHT:()=>_e,FEE_PRECISION:()=>Ti,FeeUtils:()=>Oe,IlmUtils:()=>ji,MAX_BIN_ID:()=>Fn,MAX_BIN_PER_POSITION:()=>le,MAX_FEE_RATE:()=>Ke,MIN_BIN_ID:()=>Dn,ONE:()=>$e,PoolModule:()=>Ce,REWARD_PERIOD:()=>Nn,REWARD_PERIOD_START_AT:()=>qn,SCALE_OFFSET:()=>q,StrategyType:()=>Ie,StrategyUtils:()=>Pe,WeightUtils:()=>ee,buildPoolKey:()=>Li,default:()=>Qn,dlmmMainnet:()=>Ai,dlmmTestnet:()=>Zi,generateRewardSchedule:()=>Zn,getRouterModule:()=>qi,parseBinInfo:()=>Ve,parseBinInfoList:()=>ki,parseCurrentRewardPeriodEmission:()=>Hn,parseDlmmBasePool:()=>Ci,parseDlmmPool:()=>Ee,parseDlmmPosition:()=>Ge,parseLiquidityShares:()=>jn,parsePartner:()=>ze,parsePoolTransactionInfo:()=>Ni,parseRewardPeriodEmission:()=>Un,parseStrategyType:()=>Wn,parsedDlmmPosFeeData:()=>Ri,parsedDlmmPosRewardData:()=>Di,parsedSwapQuoteData:()=>Fi,poolFilterEvenTypes:()=>Ir,safeAmount:()=>Me,safeMulAmount:()=>ne});module.exports=jr(Kn);var Cr=require("@cetusprotocol/common-sdk");var Fe=require("@cetusprotocol/common-sdk"),Ai={env:"mainnet",full_rpc_url:Fe.FullRpcUrlMainnet,graph_rpc_url:Fe.GraphRpcUrlMainnet,dlmm_pool:{package_id:"0x5664f9d3fd82c84023870cfbda8ea84e14c8dd56ce557ad2116e0668581a682b",published_at:"0xa4c6f46bd6b456e6477bcddf0652e0d2d8fb4767e306533e6e885302ee28cfab",version:4,config:{registry_id:"0xb1d55e7d895823c65f98d99b81a69436cf7d1638629c9ccb921326039cda1f1b",pools_id:"0xc3683b2356cac6423e9ecaea20955c7cc193998b016e5b884730ed1192174991",global_config_id:"0xf31b605d117f959b9730e8c07b08b856cb05143c5e81d5751c90d2979e82f599",versioned_id:"0x05370b2d656612dd5759cbe80463de301e3b94a921dfc72dd9daa2ecdeb2d0a8",admin_cap_id:"0xc4c42bc31cb54beb679dccd547f8bdb970cb6dc989bd1f85a4fed4812ed95d6e",partners_id:"0x5c0affc8d363b6abb1f32790c229165215f4edead89a9bc7cd95dad717b4296a"}},dlmm_router:{package_id:"0x8d389fa25cb08ebc5e520bc520ed394eed9e62b56b7868acb398bf298b8a76f3",published_at:"0x8b34660be96911d07088c754a8b759f0c59626936a002789b389f82ec801d3b8",version:2}};var xe=require("@mysten/sui/transactions"),it=require("@cetusprotocol/common-sdk");var lr=require("@cetusprotocol/common-sdk");var me=class extends lr.BaseError{constructor(i,t,r){super(i,t||"UnknownError",r)}static isDlmmErrorCode(i,t){return this.isErrorCode(i,t)}},J=(n,i,t)=>{let r={...t,stack:i instanceof Error?i.stack:void 0};throw i instanceof Error?new me(i.message,n,r):new me(i,n,r)};var G=require("@cetusprotocol/common-sdk"),Br=Pi(Ne());var be=9e15,he=1e9,Si="0123456789abcdef",je="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",We="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Ii={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-be,maxE:be,crypto:!1},hr,ae,z=!0,Ue="[DecimalError] ",ce=Ue+"Invalid argument: ",pr=Ue+"Precision limit exceeded",mr=Ue+"crypto unavailable",_r="[object Decimal]",Vt=Math.floor,$t=Math.pow,Wr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Zr=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Ur=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,gr=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,re=1e7,$=7,Hr=9007199254740991,Qr=je.length-1,Ei=We.length-1,I={toStringTag:_r};I.absoluteValue=I.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),H(n)};I.ceil=function(){return H(new this.constructor(this),this.e+1,2)};I.clampedTo=I.clamp=function(n,i){var t,r=this,e=r.constructor;if(n=new e(n),i=new e(i),!n.s||!i.s)return new e(NaN);if(n.gt(i))throw Error(ce+i);return t=r.cmp(n),t<0?n:r.cmp(i)>0?i:new e(r)};I.comparedTo=I.cmp=function(n){var i,t,r,e,s=this,l=s.d,d=(n=new s.constructor(n)).d,f=s.s,c=n.s;if(!l||!d)return!f||!c?NaN:f!==c?f:l===d?0:!l^f<0?1:-1;if(!l[0]||!d[0])return l[0]?f:d[0]?-c:0;if(f!==c)return f;if(s.e!==n.e)return s.e>n.e^f<0?1:-1;for(r=l.length,e=d.length,i=0,t=r<e?r:e;i<t;++i)if(l[i]!==d[i])return l[i]>d[i]^f<0?1:-1;return r===e?0:r>e^f<0?1:-1};I.cosine=I.cos=function(){var n,i,t=this,r=t.constructor;return t.d?t.d[0]?(n=r.precision,i=r.rounding,r.precision=n+Math.max(t.e,t.sd())+$,r.rounding=1,t=Kr(r,Mr(r,t)),r.precision=n,r.rounding=i,H(ae==2||ae==3?t.neg():t,n,i,!0)):new r(1):new r(NaN)};I.cubeRoot=I.cbrt=function(){var n,i,t,r,e,s,l,d,f,c,p=this,_=p.constructor;if(!p.isFinite()||p.isZero())return new _(p);for(z=!1,s=p.s*$t(p.s*p,1/3),!s||Math.abs(s)==1/0?(t=zt(p.d),n=p.e,(s=(n-t.length+1)%3)&&(t+=s==1||s==-2?"0":"00"),s=$t(t,1/3),n=Vt((n+1)/3)-(n%3==(n<0?-1:2)),s==1/0?t="5e"+n:(t=s.toExponential(),t=t.slice(0,t.indexOf("e")+1)+n),r=new _(t),r.s=p.s):r=new _(s.toString()),l=(n=_.precision)+3;;)if(d=r,f=d.times(d).times(d),c=f.plus(p),r=ut(c.plus(p).times(d),c.plus(f),l+2,1),zt(d.d).slice(0,l)===(t=zt(r.d)).slice(0,l))if(t=t.slice(l-3,l+1),t=="9999"||!e&&t=="4999"){if(!e&&(H(d,n+1,0),d.times(d).times(d).eq(p))){r=d;break}l+=4,e=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(H(r,n+1,1),i=!r.times(r).times(r).eq(p));break}return z=!0,H(r,n,_.rounding,i)};I.decimalPlaces=I.dp=function(){var n,i=this.d,t=NaN;if(i){if(n=i.length-1,t=(n-Vt(this.e/$))*$,n=i[n],n)for(;n%10==0;n/=10)t--;t<0&&(t=0)}return t};I.dividedBy=I.div=function(n){return ut(this,new this.constructor(n))};I.dividedToIntegerBy=I.divToInt=function(n){var i=this,t=i.constructor;return H(ut(i,new t(n),0,1,1),t.precision,t.rounding)};I.equals=I.eq=function(n){return this.cmp(n)===0};I.floor=function(){return H(new this.constructor(this),this.e+1,3)};I.greaterThan=I.gt=function(n){return this.cmp(n)>0};I.greaterThanOrEqualTo=I.gte=function(n){var i=this.cmp(n);return i==1||i===0};I.hyperbolicCosine=I.cosh=function(){var n,i,t,r,e,s=this,l=s.constructor,d=new l(1);if(!s.isFinite())return new l(s.s?1/0:NaN);if(s.isZero())return d;t=l.precision,r=l.rounding,l.precision=t+Math.max(s.e,s.sd())+4,l.rounding=1,e=s.d.length,e<32?(n=Math.ceil(e/3),i=(1/Qe(4,n)).toString()):(n=16,i="2.3283064365386962890625e-10"),s=we(l,1,s.times(i),new l(1),!0);for(var f,c=n,p=new l(8);c--;)f=s.times(s),s=d.minus(f.times(p.minus(f.times(p))));return H(s,l.precision=t,l.rounding=r,!0)};I.hyperbolicSine=I.sinh=function(){var n,i,t,r,e=this,s=e.constructor;if(!e.isFinite()||e.isZero())return new s(e);if(i=s.precision,t=s.rounding,s.precision=i+Math.max(e.e,e.sd())+4,s.rounding=1,r=e.d.length,r<3)e=we(s,2,e,e,!0);else{n=1.4*Math.sqrt(r),n=n>16?16:n|0,e=e.times(1/Qe(5,n)),e=we(s,2,e,e,!0);for(var l,d=new s(5),f=new s(16),c=new s(20);n--;)l=e.times(e),e=e.times(d.plus(l.times(f.times(l).plus(c))))}return s.precision=i,s.rounding=t,H(e,i,t,!0)};I.hyperbolicTangent=I.tanh=function(){var n,i,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,i=r.rounding,r.precision=n+7,r.rounding=1,ut(t.sinh(),t.cosh(),r.precision=n,r.rounding=i)):new r(t.s)};I.inverseCosine=I.acos=function(){var n=this,i=n.constructor,t=n.abs().cmp(1),r=i.precision,e=i.rounding;return t!==-1?t===0?n.isNeg()?oe(i,r,e):new i(0):new i(NaN):n.isZero()?oe(i,r+4,e).times(.5):(i.precision=r+6,i.rounding=1,n=new i(1).minus(n).div(n.plus(1)).sqrt().atan(),i.precision=r,i.rounding=e,n.times(2))};I.inverseHyperbolicCosine=I.acosh=function(){var n,i,t=this,r=t.constructor;return t.lte(1)?new r(t.eq(1)?0:NaN):t.isFinite()?(n=r.precision,i=r.rounding,r.precision=n+Math.max(Math.abs(t.e),t.sd())+4,r.rounding=1,z=!1,t=t.times(t).minus(1).sqrt().plus(t),z=!0,r.precision=n,r.rounding=i,t.ln()):new r(t)};I.inverseHyperbolicSine=I.asinh=function(){var n,i,t=this,r=t.constructor;return!t.isFinite()||t.isZero()?new r(t):(n=r.precision,i=r.rounding,r.precision=n+2*Math.max(Math.abs(t.e),t.sd())+6,r.rounding=1,z=!1,t=t.times(t).plus(1).sqrt().plus(t),z=!0,r.precision=n,r.rounding=i,t.ln())};I.inverseHyperbolicTangent=I.atanh=function(){var n,i,t,r,e=this,s=e.constructor;return e.isFinite()?e.e>=0?new s(e.abs().eq(1)?e.s/0:e.isZero()?e:NaN):(n=s.precision,i=s.rounding,r=e.sd(),Math.max(r,n)<2*-e.e-1?H(new s(e),n,i,!0):(s.precision=t=r-e.e,e=ut(e.plus(1),new s(1).minus(e),t+n,1),s.precision=n+4,s.rounding=1,e=e.ln(),s.precision=n,s.rounding=i,e.times(.5))):new s(NaN)};I.inverseSine=I.asin=function(){var n,i,t,r,e=this,s=e.constructor;return e.isZero()?new s(e):(i=e.abs().cmp(1),t=s.precision,r=s.rounding,i!==-1?i===0?(n=oe(s,t+4,r).times(.5),n.s=e.s,n):new s(NaN):(s.precision=t+6,s.rounding=1,e=e.div(new s(1).minus(e.times(e)).sqrt().plus(1)).atan(),s.precision=t,s.rounding=r,e.times(2)))};I.inverseTangent=I.atan=function(){var n,i,t,r,e,s,l,d,f,c=this,p=c.constructor,_=p.precision,y=p.rounding;if(c.isFinite()){if(c.isZero())return new p(c);if(c.abs().eq(1)&&_+4<=Ei)return l=oe(p,_+4,y).times(.25),l.s=c.s,l}else{if(!c.s)return new p(NaN);if(_+4<=Ei)return l=oe(p,_+4,y).times(.5),l.s=c.s,l}for(p.precision=d=_+10,p.rounding=1,t=Math.min(28,d/$+2|0),n=t;n;--n)c=c.div(c.times(c).plus(1).sqrt().plus(1));for(z=!1,i=Math.ceil(d/$),r=1,f=c.times(c),l=new p(c),e=c;n!==-1;)if(e=e.times(f),s=l.minus(e.div(r+=2)),e=e.times(f),l=s.plus(e.div(r+=2)),l.d[i]!==void 0)for(n=i;l.d[n]===s.d[n]&&n--;);return t&&(l=l.times(2<<t-1)),z=!0,H(l,p.precision=_,p.rounding=y,!0)};I.isFinite=function(){return!!this.d};I.isInteger=I.isInt=function(){return!!this.d&&Vt(this.e/$)>this.d.length-2};I.isNaN=function(){return!this.s};I.isNegative=I.isNeg=function(){return this.s<0};I.isPositive=I.isPos=function(){return this.s>0};I.isZero=function(){return!!this.d&&this.d[0]===0};I.lessThan=I.lt=function(n){return this.cmp(n)<0};I.lessThanOrEqualTo=I.lte=function(n){return this.cmp(n)<1};I.logarithm=I.log=function(n){var i,t,r,e,s,l,d,f,c=this,p=c.constructor,_=p.precision,y=p.rounding,P=5;if(n==null)n=new p(10),i=!0;else{if(n=new p(n),t=n.d,n.s<0||!t||!t[0]||n.eq(1))return new p(NaN);i=n.eq(10)}if(t=c.d,c.s<0||!t||!t[0]||c.eq(1))return new p(t&&!t[0]?-1/0:c.s!=1?NaN:t?0:1/0);if(i)if(t.length>1)s=!0;else{for(e=t[0];e%10===0;)e/=10;s=e!==1}if(z=!1,d=_+P,l=fe(c,d),r=i?Ze(p,d+10):fe(n,d),f=ut(l,r,d,1),Be(f.d,e=_,y))do if(d+=10,l=fe(c,d),r=i?Ze(p,d+10):fe(n,d),f=ut(l,r,d,1),!s){+zt(f.d).slice(e+1,e+15)+1==1e14&&(f=H(f,_+1,0));break}while(Be(f.d,e+=10,y));return z=!0,H(f,_,y)};I.minus=I.sub=function(n){var i,t,r,e,s,l,d,f,c,p,_,y,P=this,B=P.constructor;if(n=new B(n),!P.d||!n.d)return!P.s||!n.s?n=new B(NaN):P.d?n.s=-n.s:n=new B(n.d||P.s!==n.s?P:NaN),n;if(P.s!=n.s)return n.s=-n.s,P.plus(n);if(c=P.d,y=n.d,d=B.precision,f=B.rounding,!c[0]||!y[0]){if(y[0])n.s=-n.s;else if(c[0])n=new B(P);else return new B(f===3?-0:0);return z?H(n,d,f):n}if(t=Vt(n.e/$),p=Vt(P.e/$),c=c.slice(),s=p-t,s){for(_=s<0,_?(i=c,s=-s,l=y.length):(i=y,t=p,l=c.length),r=Math.max(Math.ceil(d/$),l)+2,s>r&&(s=r,i.length=1),i.reverse(),r=s;r--;)i.push(0);i.reverse()}else{for(r=c.length,l=y.length,_=r<l,_&&(l=r),r=0;r<l;r++)if(c[r]!=y[r]){_=c[r]<y[r];break}s=0}for(_&&(i=c,c=y,y=i,n.s=-n.s),l=c.length,r=y.length-l;r>0;--r)c[l++]=0;for(r=y.length;r>s;){if(c[--r]<y[r]){for(e=r;e&&c[--e]===0;)c[e]=re-1;--c[e],c[r]+=re}c[r]-=y[r]}for(;c[--l]===0;)c.pop();for(;c[0]===0;c.shift())--t;return c[0]?(n.d=c,n.e=He(c,t),z?H(n,d,f):n):new B(f===3?-0:0)};I.modulo=I.mod=function(n){var i,t=this,r=t.constructor;return n=new r(n),!t.d||!n.s||n.d&&!n.d[0]?new r(NaN):!n.d||t.d&&!t.d[0]?H(new r(t),r.precision,r.rounding):(z=!1,r.modulo==9?(i=ut(t,n.abs(),0,3,1),i.s*=n.s):i=ut(t,n,0,r.modulo,1),i=i.times(n),z=!0,t.minus(i))};I.naturalExponential=I.exp=function(){return Oi(this)};I.naturalLogarithm=I.ln=function(){return fe(this)};I.negated=I.neg=function(){var n=new this.constructor(this);return n.s=-n.s,H(n)};I.plus=I.add=function(n){var i,t,r,e,s,l,d,f,c,p,_=this,y=_.constructor;if(n=new y(n),!_.d||!n.d)return!_.s||!n.s?n=new y(NaN):_.d||(n=new y(n.d||_.s===n.s?_:NaN)),n;if(_.s!=n.s)return n.s=-n.s,_.minus(n);if(c=_.d,p=n.d,d=y.precision,f=y.rounding,!c[0]||!p[0])return p[0]||(n=new y(_)),z?H(n,d,f):n;if(s=Vt(_.e/$),r=Vt(n.e/$),c=c.slice(),e=s-r,e){for(e<0?(t=c,e=-e,l=p.length):(t=p,r=s,l=c.length),s=Math.ceil(d/$),l=s>l?s+1:l+1,e>l&&(e=l,t.length=1),t.reverse();e--;)t.push(0);t.reverse()}for(l=c.length,e=p.length,l-e<0&&(e=l,t=p,p=c,c=t),i=0;e;)i=(c[--e]=c[e]+p[e]+i)/re|0,c[e]%=re;for(i&&(c.unshift(i),++r),l=c.length;c[--l]==0;)c.pop();return n.d=c,n.e=He(c,r),z?H(n,d,f):n};I.precision=I.sd=function(n){var i,t=this;if(n!==void 0&&n!==!!n&&n!==1&&n!==0)throw Error(ce+n);return t.d?(i=br(t.d),n&&t.e+1>i&&(i=t.e+1)):i=NaN,i};I.round=function(){var n=this,i=n.constructor;return H(new i(n),n.e+1,i.rounding)};I.sine=I.sin=function(){var n,i,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,i=r.rounding,r.precision=n+Math.max(t.e,t.sd())+$,r.rounding=1,t=zr(r,Mr(r,t)),r.precision=n,r.rounding=i,H(ae>2?t.neg():t,n,i,!0)):new r(NaN)};I.squareRoot=I.sqrt=function(){var n,i,t,r,e,s,l=this,d=l.d,f=l.e,c=l.s,p=l.constructor;if(c!==1||!d||!d[0])return new p(!c||c<0&&(!d||d[0])?NaN:d?l:1/0);for(z=!1,c=Math.sqrt(+l),c==0||c==1/0?(i=zt(d),(i.length+f)%2==0&&(i+="0"),c=Math.sqrt(i),f=Vt((f+1)/2)-(f<0||f%2),c==1/0?i="5e"+f:(i=c.toExponential(),i=i.slice(0,i.indexOf("e")+1)+f),r=new p(i)):r=new p(c.toString()),t=(f=p.precision)+3;;)if(s=r,r=s.plus(ut(l,s,t+2,1)).times(.5),zt(s.d).slice(0,t)===(i=zt(r.d)).slice(0,t))if(i=i.slice(t-3,t+1),i=="9999"||!e&&i=="4999"){if(!e&&(H(s,f+1,0),s.times(s).eq(l))){r=s;break}t+=4,e=1}else{(!+i||!+i.slice(1)&&i.charAt(0)=="5")&&(H(r,f+1,1),n=!r.times(r).eq(l));break}return z=!0,H(r,f,p.rounding,n)};I.tangent=I.tan=function(){var n,i,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,i=r.rounding,r.precision=n+10,r.rounding=1,t=t.sin(),t.s=1,t=ut(t,new r(1).minus(t.times(t)).sqrt(),n+10,0),r.precision=n,r.rounding=i,H(ae==2||ae==4?t.neg():t,n,i,!0)):new r(NaN)};I.times=I.mul=function(n){var i,t,r,e,s,l,d,f,c,p=this,_=p.constructor,y=p.d,P=(n=new _(n)).d;if(n.s*=p.s,!y||!y[0]||!P||!P[0])return new _(!n.s||y&&!y[0]&&!P||P&&!P[0]&&!y?NaN:!y||!P?n.s/0:n.s*0);for(t=Vt(p.e/$)+Vt(n.e/$),f=y.length,c=P.length,f<c&&(s=y,y=P,P=s,l=f,f=c,c=l),s=[],l=f+c,r=l;r--;)s.push(0);for(r=c;--r>=0;){for(i=0,e=f+r;e>r;)d=s[e]+P[r]*y[e-r-1]+i,s[e--]=d%re|0,i=d/re|0;s[e]=(s[e]+i)%re|0}for(;!s[--l];)s.pop();return i?++t:s.shift(),n.d=s,n.e=He(s,t),z?H(n,_.precision,_.rounding):n};I.toBinary=function(n,i){return xi(this,2,n,i)};I.toDecimalPlaces=I.toDP=function(n,i){var t=this,r=t.constructor;return t=new r(t),n===void 0?t:(Jt(n,0,he),i===void 0?i=r.rounding:Jt(i,0,8),H(t,n+t.e+1,i))};I.toExponential=function(n,i){var t,r=this,e=r.constructor;return n===void 0?t=se(r,!0):(Jt(n,0,he),i===void 0?i=e.rounding:Jt(i,0,8),r=H(new e(r),n+1,i),t=se(r,!0,n+1)),r.isNeg()&&!r.isZero()?"-"+t:t};I.toFixed=function(n,i){var t,r,e=this,s=e.constructor;return n===void 0?t=se(e):(Jt(n,0,he),i===void 0?i=s.rounding:Jt(i,0,8),r=H(new s(e),n+e.e+1,i),t=se(r,!1,n+r.e+1)),e.isNeg()&&!e.isZero()?"-"+t:t};I.toFraction=function(n){var i,t,r,e,s,l,d,f,c,p,_,y,P=this,B=P.d,S=P.constructor;if(!B)return new S(P);if(c=t=new S(1),r=f=new S(0),i=new S(r),s=i.e=br(B)-P.e-1,l=s%$,i.d[0]=$t(10,l<0?$+l:l),n==null)n=s>0?i:c;else{if(d=new S(n),!d.isInt()||d.lt(c))throw Error(ce+d);n=d.gt(i)?s>0?i:c:d}for(z=!1,d=new S(zt(B)),p=S.precision,S.precision=s=B.length*$*2;_=ut(d,i,0,1,1),e=t.plus(_.times(r)),e.cmp(n)!=1;)t=r,r=e,e=c,c=f.plus(_.times(e)),f=e,e=i,i=d.minus(_.times(e)),d=e;return e=ut(n.minus(t),r,0,1,1),f=f.plus(e.times(c)),t=t.plus(e.times(r)),f.s=c.s=P.s,y=ut(c,r,s,1).minus(P).abs().cmp(ut(f,t,s,1).minus(P).abs())<1?[c,r]:[f,t],S.precision=p,z=!0,y};I.toHexadecimal=I.toHex=function(n,i){return xi(this,16,n,i)};I.toNearest=function(n,i){var t=this,r=t.constructor;if(t=new r(t),n==null){if(!t.d)return t;n=new r(1),i=r.rounding}else{if(n=new r(n),i===void 0?i=r.rounding:Jt(i,0,8),!t.d)return n.s?t:n;if(!n.d)return n.s&&(n.s=t.s),n}return n.d[0]?(z=!1,t=ut(t,n,0,i,1).times(n),z=!0,H(t)):(n.s=t.s,t=n),t};I.toNumber=function(){return+this};I.toOctal=function(n,i){return xi(this,8,n,i)};I.toPower=I.pow=function(n){var i,t,r,e,s,l,d=this,f=d.constructor,c=+(n=new f(n));if(!d.d||!n.d||!d.d[0]||!n.d[0])return new f($t(+d,c));if(d=new f(d),d.eq(1))return d;if(r=f.precision,s=f.rounding,n.eq(1))return H(d,r,s);if(i=Vt(n.e/$),i>=n.d.length-1&&(t=c<0?-c:c)<=Hr)return e=wr(f,d,t,r),n.s<0?new f(1).div(e):H(e,r,s);if(l=d.s,l<0){if(i<n.d.length-1)return new f(NaN);if((n.d[i]&1)==0&&(l=1),d.e==0&&d.d[0]==1&&d.d.length==1)return d.s=l,d}return t=$t(+d,c),i=t==0||!isFinite(t)?Vt(c*(Math.log("0."+zt(d.d))/Math.LN10+d.e+1)):new f(t+"").e,i>f.maxE+1||i<f.minE-1?new f(i>0?l/0:0):(z=!1,f.rounding=d.s=1,t=Math.min(12,(i+"").length),e=Oi(n.times(fe(d,r+t)),r),e.d&&(e=H(e,r+5,1),Be(e.d,r,s)&&(i=r+10,e=H(Oi(n.times(fe(d,i+t)),i),i+5,1),+zt(e.d).slice(r+1,r+15)+1==1e14&&(e=H(e,r+1,0)))),e.s=l,z=!0,f.rounding=s,H(e,r,s))};I.toPrecision=function(n,i){var t,r=this,e=r.constructor;return n===void 0?t=se(r,r.e<=e.toExpNeg||r.e>=e.toExpPos):(Jt(n,1,he),i===void 0?i=e.rounding:Jt(i,0,8),r=H(new e(r),n,i),t=se(r,n<=r.e||r.e<=e.toExpNeg,n)),r.isNeg()&&!r.isZero()?"-"+t:t};I.toSignificantDigits=I.toSD=function(n,i){var t=this,r=t.constructor;return n===void 0?(n=r.precision,i=r.rounding):(Jt(n,1,he),i===void 0?i=r.rounding:Jt(i,0,8)),H(new r(t),n,i)};I.toString=function(){var n=this,i=n.constructor,t=se(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()&&!n.isZero()?"-"+t:t};I.truncated=I.trunc=function(){return H(new this.constructor(this),this.e+1,1)};I.valueOf=I.toJSON=function(){var n=this,i=n.constructor,t=se(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()?"-"+t:t};function zt(n){var i,t,r,e=n.length-1,s="",l=n[0];if(e>0){for(s+=l,i=1;i<e;i++)r=n[i]+"",t=$-r.length,t&&(s+=de(t)),s+=r;l=n[i],r=l+"",t=$-r.length,t&&(s+=de(t))}else if(l===0)return"0";for(;l%10===0;)l/=10;return s+l}function Jt(n,i,t){if(n!==~~n||n<i||n>t)throw Error(ce+n)}function Be(n,i,t,r){var e,s,l,d;for(s=n[0];s>=10;s/=10)--i;return--i<0?(i+=$,e=0):(e=Math.ceil((i+1)/$),i%=$),s=$t(10,$-i),d=n[e]%s|0,r==null?i<3?(i==0?d=d/100|0:i==1&&(d=d/10|0),l=t<4&&d==99999||t>3&&d==49999||d==5e4||d==0):l=(t<4&&d+1==s||t>3&&d+1==s/2)&&(n[e+1]/s/100|0)==$t(10,i-2)-1||(d==s/2||d==0)&&(n[e+1]/s/100|0)==0:i<4?(i==0?d=d/1e3|0:i==1?d=d/100|0:i==2&&(d=d/10|0),l=(r||t<4)&&d==9999||!r&&t>3&&d==4999):l=((r||t<4)&&d+1==s||!r&&t>3&&d+1==s/2)&&(n[e+1]/s/1e3|0)==$t(10,i-3)-1,l}function qe(n,i,t){for(var r,e=[0],s,l=0,d=n.length;l<d;){for(s=e.length;s--;)e[s]*=i;for(e[0]+=Si.indexOf(n.charAt(l++)),r=0;r<e.length;r++)e[r]>t-1&&(e[r+1]===void 0&&(e[r+1]=0),e[r+1]+=e[r]/t|0,e[r]%=t)}return e.reverse()}function Kr(n,i){var t,r,e;if(i.isZero())return i;r=i.d.length,r<32?(t=Math.ceil(r/3),e=(1/Qe(4,t)).toString()):(t=16,e="2.3283064365386962890625e-10"),n.precision+=t,i=we(n,1,i.times(e),new n(1));for(var s=t;s--;){var l=i.times(i);i=l.times(l).minus(l).times(8).plus(1)}return n.precision-=t,i}var ut=(function(){function n(r,e,s){var l,d=0,f=r.length;for(r=r.slice();f--;)l=r[f]*e+d,r[f]=l%s|0,d=l/s|0;return d&&r.unshift(d),r}function i(r,e,s,l){var d,f;if(s!=l)f=s>l?1:-1;else for(d=f=0;d<s;d++)if(r[d]!=e[d]){f=r[d]>e[d]?1:-1;break}return f}function t(r,e,s,l){for(var d=0;s--;)r[s]-=d,d=r[s]<e[s]?1:0,r[s]=d*l+r[s]-e[s];for(;!r[0]&&r.length>1;)r.shift()}return function(r,e,s,l,d,f){var c,p,_,y,P,B,S,C,k,R,x,T,Z,j,Y,nt,st,dt,X,lt,v=r.constructor,o=r.s==e.s?1:-1,u=r.d,h=e.d;if(!u||!u[0]||!h||!h[0])return new v(!r.s||!e.s||(u?h&&u[0]==h[0]:!h)?NaN:u&&u[0]==0||!h?o*0:o/0);for(f?(P=1,p=r.e-e.e):(f=re,P=$,p=Vt(r.e/P)-Vt(e.e/P)),X=h.length,st=u.length,k=new v(o),R=k.d=[],_=0;h[_]==(u[_]||0);_++);if(h[_]>(u[_]||0)&&p--,s==null?(j=s=v.precision,l=v.rounding):d?j=s+(r.e-e.e)+1:j=s,j<0)R.push(1),B=!0;else{if(j=j/P+2|0,_=0,X==1){for(y=0,h=h[0],j++;(_<st||y)&&j--;_++)Y=y*f+(u[_]||0),R[_]=Y/h|0,y=Y%h|0;B=y||_<st}else{for(y=f/(h[0]+1)|0,y>1&&(h=n(h,y,f),u=n(u,y,f),X=h.length,st=u.length),nt=X,x=u.slice(0,X),T=x.length;T<X;)x[T++]=0;lt=h.slice(),lt.unshift(0),dt=h[0],h[1]>=f/2&&++dt;do y=0,c=i(h,x,X,T),c<0?(Z=x[0],X!=T&&(Z=Z*f+(x[1]||0)),y=Z/dt|0,y>1?(y>=f&&(y=f-1),S=n(h,y,f),C=S.length,T=x.length,c=i(S,x,C,T),c==1&&(y--,t(S,X<C?lt:h,C,f))):(y==0&&(c=y=1),S=h.slice()),C=S.length,C<T&&S.unshift(0),t(x,S,T,f),c==-1&&(T=x.length,c=i(h,x,X,T),c<1&&(y++,t(x,X<T?lt:h,T,f))),T=x.length):c===0&&(y++,x=[0]),R[_++]=y,c&&x[0]?x[T++]=u[nt]||0:(x=[u[nt]],T=1);while((nt++<st||x[0]!==void 0)&&j--);B=x[0]!==void 0}R[0]||R.shift()}if(P==1)k.e=p,hr=B;else{for(_=1,y=R[0];y>=10;y/=10)_++;k.e=_+p*P-1,H(k,d?s+k.e+1:s,l,B)}return k}})();function H(n,i,t,r){var e,s,l,d,f,c,p,_,y,P=n.constructor;t:if(i!=null){if(_=n.d,!_)return n;for(e=1,d=_[0];d>=10;d/=10)e++;if(s=i-e,s<0)s+=$,l=i,p=_[y=0],f=p/$t(10,e-l-1)%10|0;else if(y=Math.ceil((s+1)/$),d=_.length,y>=d)if(r){for(;d++<=y;)_.push(0);p=f=0,e=1,s%=$,l=s-$+1}else break t;else{for(p=d=_[y],e=1;d>=10;d/=10)e++;s%=$,l=s-$+e,f=l<0?0:p/$t(10,e-l-1)%10|0}if(r=r||i<0||_[y+1]!==void 0||(l<0?p:p%$t(10,e-l-1)),c=t<4?(f||r)&&(t==0||t==(n.s<0?3:2)):f>5||f==5&&(t==4||r||t==6&&(s>0?l>0?p/$t(10,e-l):0:_[y-1])%10&1||t==(n.s<0?8:7)),i<1||!_[0])return _.length=0,c?(i-=n.e+1,_[0]=$t(10,($-i%$)%$),n.e=-i||0):_[0]=n.e=0,n;if(s==0?(_.length=y,d=1,y--):(_.length=y+1,d=$t(10,$-s),_[y]=l>0?(p/$t(10,e-l)%$t(10,l)|0)*d:0),c)for(;;)if(y==0){for(s=1,l=_[0];l>=10;l/=10)s++;for(l=_[0]+=d,d=1;l>=10;l/=10)d++;s!=d&&(n.e++,_[0]==re&&(_[0]=1));break}else{if(_[y]+=d,_[y]!=re)break;_[y--]=0,d=1}for(s=_.length;_[--s]===0;)_.pop()}return z&&(n.e>P.maxE?(n.d=null,n.e=NaN):n.e<P.minE&&(n.e=0,n.d=[0])),n}function se(n,i,t){if(!n.isFinite())return yr(n);var r,e=n.e,s=zt(n.d),l=s.length;return i?(t&&(r=t-l)>0?s=s.charAt(0)+"."+s.slice(1)+de(r):l>1&&(s=s.charAt(0)+"."+s.slice(1)),s=s+(n.e<0?"e":"e+")+n.e):e<0?(s="0."+de(-e-1)+s,t&&(r=t-l)>0&&(s+=de(r))):e>=l?(s+=de(e+1-l),t&&(r=t-e-1)>0&&(s=s+"."+de(r))):((r=e+1)<l&&(s=s.slice(0,r)+"."+s.slice(r)),t&&(r=t-l)>0&&(e+1===l&&(s+="."),s+=de(r))),s}function He(n,i){var t=n[0];for(i*=$;t>=10;t/=10)i++;return i}function Ze(n,i,t){if(i>Qr)throw z=!0,t&&(n.precision=t),Error(pr);return H(new n(je),i,1,!0)}function oe(n,i,t){if(i>Ei)throw Error(pr);return H(new n(We),i,t,!0)}function br(n){var i=n.length-1,t=i*$+1;if(i=n[i],i){for(;i%10==0;i/=10)t--;for(i=n[0];i>=10;i/=10)t++}return t}function de(n){for(var i="";n--;)i+="0";return i}function wr(n,i,t,r){var e,s=new n(1),l=Math.ceil(r/$+4);for(z=!1;;){if(t%2&&(s=s.times(i),fr(s.d,l)&&(e=!0)),t=Vt(t/2),t===0){t=s.d.length-1,e&&s.d[t]===0&&++s.d[t];break}i=i.times(i),fr(i.d,l)}return z=!0,s}function dr(n){return n.d[n.d.length-1]&1}function vr(n,i,t){for(var r,e,s=new n(i[0]),l=0;++l<i.length;){if(e=new n(i[l]),!e.s){s=e;break}r=s.cmp(e),(r===t||r===0&&s.s===t)&&(s=e)}return s}function Oi(n,i){var t,r,e,s,l,d,f,c=0,p=0,_=0,y=n.constructor,P=y.rounding,B=y.precision;if(!n.d||!n.d[0]||n.e>17)return new y(n.d?n.d[0]?n.s<0?0:1/0:1:n.s?n.s<0?0:n:NaN);for(i==null?(z=!1,f=B):f=i,d=new y(.03125);n.e>-2;)n=n.times(d),_+=5;for(r=Math.log($t(2,_))/Math.LN10*2+5|0,f+=r,t=s=l=new y(1),y.precision=f;;){if(s=H(s.times(n),f,1),t=t.times(++p),d=l.plus(ut(s,t,f,1)),zt(d.d).slice(0,f)===zt(l.d).slice(0,f)){for(e=_;e--;)l=H(l.times(l),f,1);if(i==null)if(c<3&&Be(l.d,f-r,P,c))y.precision=f+=10,t=s=d=new y(1),p=0,c++;else return H(l,y.precision=B,P,z=!0);else return y.precision=B,l}l=d}}function fe(n,i){var t,r,e,s,l,d,f,c,p,_,y,P=1,B=10,S=n,C=S.d,k=S.constructor,R=k.rounding,x=k.precision;if(S.s<0||!C||!C[0]||!S.e&&C[0]==1&&C.length==1)return new k(C&&!C[0]?-1/0:S.s!=1?NaN:C?0:S);if(i==null?(z=!1,p=x):p=i,k.precision=p+=B,t=zt(C),r=t.charAt(0),Math.abs(s=S.e)<15e14){for(;r<7&&r!=1||r==1&&t.charAt(1)>3;)S=S.times(n),t=zt(S.d),r=t.charAt(0),P++;s=S.e,r>1?(S=new k("0."+t),s++):S=new k(r+"."+t.slice(1))}else return c=Ze(k,p+2,x).times(s+""),S=fe(new k(r+"."+t.slice(1)),p-B).plus(c),k.precision=x,i==null?H(S,x,R,z=!0):S;for(_=S,f=l=S=ut(S.minus(1),S.plus(1),p,1),y=H(S.times(S),p,1),e=3;;){if(l=H(l.times(y),p,1),c=f.plus(ut(l,new k(e),p,1)),zt(c.d).slice(0,p)===zt(f.d).slice(0,p))if(f=f.times(2),s!==0&&(f=f.plus(Ze(k,p+2,x).times(s+""))),f=ut(f,new k(P),p,1),i==null)if(Be(f.d,p-B,R,d))k.precision=p+=B,c=l=S=ut(_.minus(1),_.plus(1),p,1),y=H(S.times(S),p,1),e=d=1;else return H(f,k.precision=x,R,z=!0);else return k.precision=x,f;f=c,e+=2}}function yr(n){return String(n.s*n.s/0)}function Le(n,i){var t,r,e;for((t=i.indexOf("."))>-1&&(i=i.replace(".","")),(r=i.search(/e/i))>0?(t<0&&(t=r),t+=+i.slice(r+1),i=i.substring(0,r)):t<0&&(t=i.length),r=0;i.charCodeAt(r)===48;r++);for(e=i.length;i.charCodeAt(e-1)===48;--e);if(i=i.slice(r,e),i){if(e-=r,n.e=t=t-r-1,n.d=[],r=(t+1)%$,t<0&&(r+=$),r<e){for(r&&n.d.push(+i.slice(0,r)),e-=$;r<e;)n.d.push(+i.slice(r,r+=$));i=i.slice(r),r=$-i.length}else r-=e;for(;r--;)i+="0";n.d.push(+i),z&&(n.e>n.constructor.maxE?(n.d=null,n.e=NaN):n.e<n.constructor.minE&&(n.e=0,n.d=[0]))}else n.e=0,n.d=[0];return n}function $r(n,i){var t,r,e,s,l,d,f,c,p;if(i.indexOf("_")>-1){if(i=i.replace(/(\d)_(?=\d)/g,"$1"),gr.test(i))return Le(n,i)}else if(i==="Infinity"||i==="NaN")return+i||(n.s=NaN),n.e=NaN,n.d=null,n;if(Zr.test(i))t=16,i=i.toLowerCase();else if(Wr.test(i))t=2;else if(Ur.test(i))t=8;else throw Error(ce+i);for(s=i.search(/p/i),s>0?(f=+i.slice(s+1),i=i.substring(2,s)):i=i.slice(2),s=i.indexOf("."),l=s>=0,r=n.constructor,l&&(i=i.replace(".",""),d=i.length,s=d-s,e=wr(r,new r(t),s,s*2)),c=qe(i,t,re),p=c.length-1,s=p;c[s]===0;--s)c.pop();return s<0?new r(n.s*0):(n.e=He(c,p),n.d=c,z=!1,l&&(n=ut(n,e,d*4)),f&&(n=n.times(Math.abs(f)<54?$t(2,f):Se.pow(2,f))),z=!0,n)}function zr(n,i){var t,r=i.d.length;if(r<3)return i.isZero()?i:we(n,2,i,i);t=1.4*Math.sqrt(r),t=t>16?16:t|0,i=i.times(1/Qe(5,t)),i=we(n,2,i,i);for(var e,s=new n(5),l=new n(16),d=new n(20);t--;)e=i.times(i),i=i.times(s.plus(e.times(l.times(e).minus(d))));return i}function we(n,i,t,r,e){var s,l,d,f,c=1,p=n.precision,_=Math.ceil(p/$);for(z=!1,f=t.times(t),d=new n(r);;){if(l=ut(d.times(f),new n(i++*i++),p,1),d=e?r.plus(l):r.minus(l),r=ut(l.times(f),new n(i++*i++),p,1),l=d.plus(r),l.d[_]!==void 0){for(s=_;l.d[s]===d.d[s]&&s--;);if(s==-1)break}s=d,d=r,r=l,l=s,c++}return z=!0,l.d.length=_+1,l}function Qe(n,i){for(var t=n;--i;)t*=n;return t}function Mr(n,i){var t,r=i.s<0,e=oe(n,n.precision,1),s=e.times(.5);if(i=i.abs(),i.lte(s))return ae=r?4:1,i;if(t=i.divToInt(e),t.isZero())ae=r?3:2;else{if(i=i.minus(t.times(e)),i.lte(s))return ae=dr(t)?r?2:3:r?4:1,i;ae=dr(t)?r?1:4:r?3:2}return i.minus(e).abs()}function xi(n,i,t,r){var e,s,l,d,f,c,p,_,y,P=n.constructor,B=t!==void 0;if(B?(Jt(t,1,he),r===void 0?r=P.rounding:Jt(r,0,8)):(t=P.precision,r=P.rounding),!n.isFinite())p=yr(n);else{for(p=se(n),l=p.indexOf("."),B?(e=2,i==16?t=t*4-3:i==8&&(t=t*3-2)):e=i,l>=0&&(p=p.replace(".",""),y=new P(1),y.e=p.length-l,y.d=qe(se(y),10,e),y.e=y.d.length),_=qe(p,10,e),s=f=_.length;_[--f]==0;)_.pop();if(!_[0])p=B?"0p+0":"0";else{if(l<0?s--:(n=new P(n),n.d=_,n.e=s,n=ut(n,y,t,r,0,e),_=n.d,s=n.e,c=hr),l=_[t],d=e/2,c=c||_[t+1]!==void 0,c=r<4?(l!==void 0||c)&&(r===0||r===(n.s<0?3:2)):l>d||l===d&&(r===4||c||r===6&&_[t-1]&1||r===(n.s<0?8:7)),_.length=t,c)for(;++_[--t]>e-1;)_[t]=0,t||(++s,_.unshift(1));for(f=_.length;!_[f-1];--f);for(l=0,p="";l<f;l++)p+=Si.charAt(_[l]);if(B){if(f>1)if(i==16||i==8){for(l=i==16?4:3,--f;f%l;f++)p+="0";for(_=qe(p,e,i),f=_.length;!_[f-1];--f);for(l=1,p="1.";l<f;l++)p+=Si.charAt(_[l])}else p=p.charAt(0)+"."+p.slice(1);p=p+(s<0?"p":"p+")+s}else if(s<0){for(;++s;)p="0"+p;p="0."+p}else if(++s>f)for(s-=f;s--;)p+="0";else s<f&&(p=p.slice(0,s)+"."+p.slice(s))}p=(i==16?"0x":i==2?"0b":i==8?"0o":"")+p}return n.s<0?"-"+p:p}function fr(n,i){if(n.length>i)return n.length=i,!0}function Gr(n){return new this(n).abs()}function Vr(n){return new this(n).acos()}function Xr(n){return new this(n).acosh()}function Jr(n,i){return new this(n).plus(i)}function Yr(n){return new this(n).asin()}function tn(n){return new this(n).asinh()}function en(n){return new this(n).atan()}function rn(n){return new this(n).atanh()}function nn(n,i){n=new this(n),i=new this(i);var t,r=this.precision,e=this.rounding,s=r+4;return!n.s||!i.s?t=new this(NaN):!n.d&&!i.d?(t=oe(this,s,1).times(i.s>0?.25:.75),t.s=n.s):!i.d||n.isZero()?(t=i.s<0?oe(this,r,e):new this(0),t.s=n.s):!n.d||i.isZero()?(t=oe(this,s,1).times(.5),t.s=n.s):i.s<0?(this.precision=s,this.rounding=1,t=this.atan(ut(n,i,s,1)),i=oe(this,s,1),this.precision=r,this.rounding=e,t=n.s<0?t.minus(i):t.plus(i)):t=this.atan(ut(n,i,s,1)),t}function on(n){return new this(n).cbrt()}function sn(n){return H(n=new this(n),n.e+1,2)}function an(n,i,t){return new this(n).clamp(i,t)}function ln(n){if(!n||typeof n!="object")throw Error(Ue+"Object expected");var i,t,r,e=n.defaults===!0,s=["precision",1,he,"rounding",0,8,"toExpNeg",-be,0,"toExpPos",0,be,"maxE",0,be,"minE",-be,0,"modulo",0,9];for(i=0;i<s.length;i+=3)if(t=s[i],e&&(this[t]=Ii[t]),(r=n[t])!==void 0)if(Vt(r)===r&&r>=s[i+1]&&r<=s[i+2])this[t]=r;else throw Error(ce+t+": "+r);if(t="crypto",e&&(this[t]=Ii[t]),(r=n[t])!==void 0)if(r===!0||r===!1||r===0||r===1)if(r)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[t]=!0;else throw Error(mr);else this[t]=!1;else throw Error(ce+t+": "+r);return this}function un(n){return new this(n).cos()}function dn(n){return new this(n).cosh()}function Pr(n){var i,t,r;function e(s){var l,d,f,c=this;if(!(c instanceof e))return new e(s);if(c.constructor=e,cr(s)){c.s=s.s,z?!s.d||s.e>e.maxE?(c.e=NaN,c.d=null):s.e<e.minE?(c.e=0,c.d=[0]):(c.e=s.e,c.d=s.d.slice()):(c.e=s.e,c.d=s.d?s.d.slice():s.d);return}if(f=typeof s,f==="number"){if(s===0){c.s=1/s<0?-1:1,c.e=0,c.d=[0];return}if(s<0?(s=-s,c.s=-1):c.s=1,s===~~s&&s<1e7){for(l=0,d=s;d>=10;d/=10)l++;z?l>e.maxE?(c.e=NaN,c.d=null):l<e.minE?(c.e=0,c.d=[0]):(c.e=l,c.d=[s]):(c.e=l,c.d=[s]);return}if(s*0!==0){s||(c.s=NaN),c.e=NaN,c.d=null;return}return Le(c,s.toString())}if(f==="string")return(d=s.charCodeAt(0))===45?(s=s.slice(1),c.s=-1):(d===43&&(s=s.slice(1)),c.s=1),gr.test(s)?Le(c,s):$r(c,s);if(f==="bigint")return s<0?(s=-s,c.s=-1):c.s=1,Le(c,s.toString());throw Error(ce+s)}if(e.prototype=I,e.ROUND_UP=0,e.ROUND_DOWN=1,e.ROUND_CEIL=2,e.ROUND_FLOOR=3,e.ROUND_HALF_UP=4,e.ROUND_HALF_DOWN=5,e.ROUND_HALF_EVEN=6,e.ROUND_HALF_CEIL=7,e.ROUND_HALF_FLOOR=8,e.EUCLID=9,e.config=e.set=ln,e.clone=Pr,e.isDecimal=cr,e.abs=Gr,e.acos=Vr,e.acosh=Xr,e.add=Jr,e.asin=Yr,e.asinh=tn,e.atan=en,e.atanh=rn,e.atan2=nn,e.cbrt=on,e.ceil=sn,e.clamp=an,e.cos=un,e.cosh=dn,e.div=fn,e.exp=cn,e.floor=hn,e.hypot=pn,e.ln=mn,e.log=_n,e.log10=bn,e.log2=gn,e.max=wn,e.min=vn,e.mod=yn,e.mul=Mn,e.pow=Pn,e.random=An,e.round=Bn,e.sign=Sn,e.sin=In,e.sinh=En,e.sqrt=On,e.sub=xn,e.sum=Tn,e.tan=Cn,e.tanh=kn,e.trunc=Rn,n===void 0&&(n={}),n&&n.defaults!==!0)for(r=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],i=0;i<r.length;)n.hasOwnProperty(t=r[i++])||(n[t]=this[t]);return e.config(n),e}function fn(n,i){return new this(n).div(i)}function cn(n){return new this(n).exp()}function hn(n){return H(n=new this(n),n.e+1,3)}function pn(){var n,i,t=new this(0);for(z=!1,n=0;n<arguments.length;)if(i=new this(arguments[n++]),i.d)t.d&&(t=t.plus(i.times(i)));else{if(i.s)return z=!0,new this(1/0);t=i}return z=!0,t.sqrt()}function cr(n){return n instanceof Se||n&&n.toStringTag===_r||!1}function mn(n){return new this(n).ln()}function _n(n,i){return new this(n).log(i)}function gn(n){return new this(n).log(2)}function bn(n){return new this(n).log(10)}function wn(){return vr(this,arguments,-1)}function vn(){return vr(this,arguments,1)}function yn(n,i){return new this(n).mod(i)}function Mn(n,i){return new this(n).mul(i)}function Pn(n,i){return new this(n).pow(i)}function An(n){var i,t,r,e,s=0,l=new this(1),d=[];if(n===void 0?n=this.precision:Jt(n,1,he),r=Math.ceil(n/$),this.crypto)if(crypto.getRandomValues)for(i=crypto.getRandomValues(new Uint32Array(r));s<r;)e=i[s],e>=429e7?i[s]=crypto.getRandomValues(new Uint32Array(1))[0]:d[s++]=e%1e7;else if(crypto.randomBytes){for(i=crypto.randomBytes(r*=4);s<r;)e=i[s]+(i[s+1]<<8)+(i[s+2]<<16)+((i[s+3]&127)<<24),e>=214e7?crypto.randomBytes(4).copy(i,s):(d.push(e%1e7),s+=4);s=r/4}else throw Error(mr);else for(;s<r;)d[s++]=Math.random()*1e7|0;for(r=d[--s],n%=$,r&&n&&(e=$t(10,$-n),d[s]=(r/e|0)*e);d[s]===0;s--)d.pop();if(s<0)t=0,d=[0];else{for(t=-1;d[0]===0;t-=$)d.shift();for(r=1,e=d[0];e>=10;e/=10)r++;r<$&&(t-=$-r)}return l.e=t,l.d=d,l}function Bn(n){return H(n=new this(n),n.e+1,this.rounding)}function Sn(n){return n=new this(n),n.d?n.d[0]?n.s:0*n.s:n.s||NaN}function In(n){return new this(n).sin()}function En(n){return new this(n).sinh()}function On(n){return new this(n).sqrt()}function xn(n,i){return new this(n).sub(i)}function Tn(){var n=0,i=arguments,t=new this(i[n]);for(z=!1;t.s&&++n<i.length;)t=t.plus(i[n]);return z=!0,H(t,this.precision,this.rounding)}function Cn(n){return new this(n).tan()}function kn(n){return new this(n).tanh()}function Rn(n){return H(n=new this(n),n.e+1,1)}I[Symbol.for("nodejs.util.inspect.custom")]=I.toString;I[Symbol.toStringTag]="Decimal";var Se=I.constructor=Pr(Ii);je=new Se(je);We=new Se(We);var W=Se;var Ie=(r=>(r[r.Spot=0]="Spot",r[r.Curve=1]="Curve",r[r.BidAsk=2]="BidAsk",r))(Ie||{});var K=require("@cetusprotocol/common-sdk");var le=1e3,Dn=-443636,Fn=443636,ue=1e4,ve=2e3,_e=200,ge=443636n,Ke=1e8,Ti=1e9,te=1e4,Nn=604800,qn=1747627200;var at=Pi(Ne());var Ln=new at.default(524288),q=64,$e=new at.default(1).shln(q),Ar=new at.default(2).pow(new at.default(128)).sub(new at.default(1)),D=class n{static splitBinLiquidityInfo(i,t,r){let e=n.getPositionCount(t,r);if(e<=1)return[i];let s=[],l=t;for(let d=0;d<e;d++){let f=Math.min(l+1e3-2,r),c=i.bins.filter(p=>p.bin_id>=l&&p.bin_id<=f);s.push({bins:c,amount_a:c.reduce((p,_)=>(0,K.d)(p).plus(_.amount_a),(0,K.d)(0)).toFixed(0),amount_b:c.reduce((p,_)=>(0,K.d)(p).plus(_.amount_b),(0,K.d)(0)).toFixed(0)}),l=f+1}return s}static processBinsByRate(i,t){let r=[],e=(0,K.d)(0),s=(0,K.d)(0),l=!1;return i.forEach(d=>{let{amount_a:f,amount_b:c,liquidity:p="0"}=d,_=(0,K.d)(t).mul(p).toFixed(0),y=(0,K.d)(f).mul(t),P=(0,K.d)(c).mul(t);e=(0,K.d)(e).plus(y),s=(0,K.d)(s).plus(P),((0,K.d)(y).lt(1)&&(0,K.d)(y).gt(0)||(0,K.d)(P).lt(1)&&(0,K.d)(P).gt(0))&&(l=!0),r.push({bin_id:d.bin_id,amount_a:y.toFixed(0),amount_b:P.toFixed(0),price_per_lamport:d.price_per_lamport,liquidity:_})}),{bins:{bins:r,amount_a:e.toFixed(0),amount_b:s.toFixed(0)},has_invalid_amount:l}}static calculateOutByShare(i,t){let{amount_a:r,amount_b:e,liquidity:s="0"}=i;if(s==="0")return{amount_a:"0",amount_b:"0"};if((0,K.d)(t).gte((0,K.d)(s)))return{amount_a:r,amount_b:e};let l=(0,K.d)(t).div(s).mul(r).toFixed(0,W.ROUND_FLOOR),d=(0,K.d)(t).div(s).mul(e).toFixed(0,W.ROUND_FLOOR);return{amount_a:l,amount_b:d}}static getPositionCount(i,t){let e=(0,K.d)(t).sub(i).add(1).div(1e3);return Number(e.toFixed(0,W.ROUND_UP))}static getLiquidity(i,t,r){return(0,K.d)(r).mul(i).add((0,K.d)(t).mul((0,K.d)(2).pow(q))).toFixed(0)}static getAmountAFromLiquidity(i,t){return(0,K.d)(i).div((0,K.d)(t)).toFixed(0)}static getAmountBFromLiquidity(i){return(0,K.d)(i).div((0,K.d)(2).pow(q)).toFixed(0)}static getAmountsFromLiquidity(i,t,r,e){if((0,K.d)(e).isZero()&&J("LiquiditySupplyIsZero","Liquidity supply is zero"),(0,K.d)(r).gt((0,K.d)(e))&&J("InvalidDeltaLiquidity","Invalid delta liquidity"),(0,K.d)(r).isZero())return["0","0"];let s;(0,K.d)(i).isZero()?s="0":s=(0,K.d)(i).mul((0,K.d)(r)).div((0,K.d)(e)).toFixed(0,W.ROUND_FLOOR);let l;return(0,K.d)(t).isZero()?l="0":l=(0,K.d)(t).mul((0,K.d)(r)).div((0,K.d)(e)).toFixed(0,W.ROUND_FLOOR),[s,l]}static getPriceFromBinId(i,t,r,e){let s=n.getPricePerLamportFromBinId(i,t);return n.getPriceFromLamport(r,e,s).toString()}static getPricePerLamportFromBinId(i,t){let r=new W(t).div(new W(1e4));return new W(1).add(new W(r)).pow(new W(i)).toString()}static getBinIdFromPrice(i,t,r,e,s){let l=n.getPricePerLamport(e,s,i);return n.getBinIdFromLamportPrice(l,t,r)}static getBinIdFromLamportPrice(i,t,r){let e=new W(t).div(new W(1e4)),s=new W(i).log().dividedBy(new W(1).add(e).log());return(r?s.floor():s.ceil()).toNumber()}static getPricePerLamport(i,t,r){return new W(r).mul(new W(10**(t-i))).toString()}static getPriceFromLamport(i,t,r){return new W(r).div(new W(10**(t-i))).toString()}static getReversePrice(i){return new W(1).div(i).toString()}static getQPriceFromId(i,t){let r=new at.default(t).shln(q).div(new at.default(1e4)),e=$e.add(r);return n.pow(e,new at.default(i)).toString()}static getPricePerLamportFromQPrice(i){return K.MathUtil.fromX64(new at.default(i)).toString()}static pow(i,t){let r=t.isNeg();if(t.isZero())return $e;if(t=r?t.abs():t,t.gt(Ln))return new at.default(0);let e=i,s=$e;return e.gte(s)&&(e=Ar.div(e),r=!r),t.and(new at.default(1)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(2)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(4)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(8)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(16)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(32)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(64)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(128)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(256)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(512)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(1024)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(2048)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(4096)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(8192)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(16384)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(32768)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(65536)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(131072)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(262144)).isZero()||(s=s.mul(e).shrn(q)),s.isZero()?new at.default(0):(r&&(s=Ar.div(s)),s)}static binScore(i){let t=BigInt(i)+ge;return(t<0n||t>ge*2n)&&J("InvalidBinId",new Error("Invalid bin ID"),{[K.DETAILS_KEYS.METHOD_NAME]:"binScore",[K.DETAILS_KEYS.REQUEST_PARAMS]:{binId:i}}),t.toString()}static scoreToBinId(i){let t=BigInt(i)-ge;return(t<-ge||t>ge)&&J("InvalidBinId",new Error("Invalid score"),{[K.DETAILS_KEYS.METHOD_NAME]:"scoreToBinId",[K.DETAILS_KEYS.REQUEST_PARAMS]:{score:i}}),Number(t)}static resolveBinPosition(i){let t=BigInt(i),r=t>>4n,e=Number(t&0xfn);return[r.toString(),e]}static findMinMaxBinId(i){let t=1+i/1e4,e=new W("18446744073709551615").log(10).div(new W(t).log(10)).floor(),s=e.neg(),l=e,d=(0,K.d)(1),f=(0,K.d)("340282366920938463463374607431768211455");for(;;){let c=(0,K.d)(n.getQPriceFromId(s.toNumber(),i));if(c.gt(d)&&!c.isZero())break;s=s.add(1)}for(;;){let c=(0,K.d)(n.getQPriceFromId(l.toNumber(),i));if(c.lt(f)&&!c.isZero())break;l=l.sub(1)}return{minBinId:s.toNumber(),maxBinId:l.toNumber()}}static getBinShift(i,t,r){let e=n.getPricePerLamportFromBinId(i,t),s=(0,K.d)(e).mul(1+r).toString(),l=n.getBinIdFromLamportPrice(s,t,!0),d=(0,K.d)(l).sub(i).abs().toFixed(0,W.ROUND_UP);return console.log("getBinShift Options:",{active_id:i,bin_shift:d}),Number(d)}};var Xt=require("@mysten/sui/bcs"),Sr=require("blakejs");function Ci(n){try{let i=n.parsedJson;return{id:i.pool_id,bin_step:Number(i.bin_step),coin_type_a:(0,G.fixCoinType)(i.coin_type_a,!1),coin_type_b:(0,G.fixCoinType)(i.coin_type_b,!1)}}catch(i){return J("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseDlmmBasePool",[G.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function Ee(n){try{let i=(0,G.getObjectFields)(n),t=(0,G.getObjectType)(n),r=(0,G.extractStructTagFromType)(t),e={bin_step:i.bin_manager.fields.bin_step,bin_manager_handle:i.bin_manager.fields.bins.fields.id.id,size:i.bin_manager.fields.bins.fields.size},s={bin_step:i.position_manager.fields.bin_step,position_index:i.position_manager.fields.position_index,position_handle:i.position_manager.fields.positions.fields.id.id,size:i.position_manager.fields.positions.fields.size},l=i.reward_manager.fields,d=l.rewards.map(_=>{let y=_.fields.current_emission_rate,P=G.MathUtil.fromX64(new Br.default(y)),B=Math.floor(P.toNumber()*60*60*24).toString();return{reward_coin:(0,G.fixCoinType)(_.fields.reward_coin.fields.name,!1),emissions_per_second:P.toString(),emissions_per_day:B,period_emission_rates:{id:_.fields.period_emission_rates.fields.id.id,size:_.fields.period_emission_rates.fields.size}}}),f={is_public:l.is_public,emergency_reward_pause:l.emergency_reward_pause,vault:{id:l.vault.fields.id.id,size:l.vault.fields.size},rewards:d,last_updated_time:l.last_updated_time},c={volatility_accumulator:i.v_parameters.fields.volatility_accumulator,volatility_reference:i.v_parameters.fields.volatility_reference,index_reference:(0,G.asIntN)(BigInt(i.v_parameters.fields.index_reference.fields.bits)),last_update_timestamp:i.v_parameters.fields.last_update_timestamp,bin_step_config:i.v_parameters.fields.bin_step_config.fields},p={id:i.id.id,bin_step:Number(i.bin_step),coin_type_a:(0,G.fixCoinType)(r.type_arguments[0],!1),coin_type_b:(0,G.fixCoinType)(r.type_arguments[1],!1),pool_type:t,index:Number(i.index),bin_manager:e,variable_parameters:c,active_id:(0,G.asIntN)(BigInt(i.active_id.fields.bits)),permissions:i.permissions.fields,balance_a:i.balance_a,balance_b:i.balance_b,base_fee_rate:i.base_fee_rate,protocol_fee_a:i.protocol_fee_a,protocol_fee_b:i.protocol_fee_b,url:i.url,reward_manager:f,position_manager:s};return p.bin_step=p.bin_manager.bin_step,p}catch(i){return console.log("\u{1F680} ~ parseDlmmPool ~ error:",i),J("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseDlmmPool",[G.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function ze(n){let i=(0,G.getObjectFields)(n),t=(0,G.getObjectType)(n),r=(0,G.extractStructTagFromType)(t);return{id:i.id.id,name:i.name,ref_fee_rate:(0,G.d)(i.ref_fee_rate).div(1e4).toNumber(),start_time:Number(i.start_time),end_time:Number(i.end_time),balances:{id:i.balances.fields.id.id,size:i.balances.fields.size},type:r.full_address}}function Ge(n){try{let i=(0,G.getObjectFields)(n);return{uri:i.uri,index:i.index,id:i.id.id,name:i.name,pool_id:i.pool_id,lower_bin_id:(0,G.asIntN)(BigInt(i.lower_bin_id.fields.bits)),upper_bin_id:(0,G.asIntN)(BigInt(i.upper_bin_id.fields.bits)),liquidity_shares:i.liquidity_shares,description:i.description,coin_type_a:(0,G.fixCoinType)(i.coin_type_a,!1),coin_type_b:(0,G.fixCoinType)(i.coin_type_b,!1)}}catch(i){return console.log("\u{1F680} ~ parseDlmmPosition ~ error:",i),J("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseDlmmPosition",[G.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function jn(n,i,t,r){let e=n.map((d,f)=>{let c=t+f,p=D.getPricePerLamportFromBinId(c,i);if(c===r.bin_id){let{amount_a:P,amount_b:B}=D.calculateOutByShare(r,d);return{bin_id:c,amount_a:P,amount_b:B,liquidity:d,price_per_lamport:p}}if(c<r.bin_id){let P=D.getAmountBFromLiquidity(d);return{bin_id:c,amount_a:"0",amount_b:P,liquidity:d,price_per_lamport:p}}let _=D.getQPriceFromId(c,i),y=D.getAmountAFromLiquidity(d,_);return{bin_id:c,amount_a:y,amount_b:"0",liquidity:d,price_per_lamport:p}}),s=e.reduce((d,f)=>d.add(new W(f.amount_a)),new W(0)).toFixed(0),l=e.reduce((d,f)=>d.add(new W(f.amount_b)),new W(0)).toFixed(0);return{bins:e,amount_a:s,amount_b:l}}function ki(n){try{let i=Xt.bcs.struct("BinAmount",{id:Xt.bcs.struct("I32",{bits:Xt.bcs.u32()}),amount_a:Xt.bcs.u64(),amount_b:Xt.bcs.u64(),price:Xt.bcs.u128(),liquidity_supply:Xt.bcs.u128(),rewards_growth_global:Xt.bcs.vector(Xt.bcs.u128()),fee_a_growth_global:Xt.bcs.u128(),fee_b_growth_global:Xt.bcs.u128()});return Xt.bcs.vector(i).parse(Uint8Array.from(n.results[1].returnValues[0][0])).map(r=>({bin_id:(0,G.asIntN)(BigInt(r.id.bits)),amount_a:r.amount_a,amount_b:r.amount_b,liquidity:r.liquidity_supply,price_per_lamport:D.getPricePerLamportFromQPrice(r.price)}))}catch(i){return console.log("\u{1F680} ~ parseBinInfo ~ error:",i),[]}}function Ve(n){try{return{bin_id:(0,G.asIntN)(BigInt(n.id.fields.bits)),amount_a:n.amount_a,amount_b:n.amount_b,liquidity:n.liquidity_share,price_per_lamport:D.getPricePerLamportFromQPrice(n.price)}}catch(i){return J("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseBinInfo",[G.DETAILS_KEYS.REQUEST_PARAMS]:{fields:n}})}}function Ri(n){let i={},t=n.events?.filter(r=>r.type.includes("pool::CollectFeeEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:e}=t[r],s={position_id:e.position,fee_owned_a:e.fee_a,fee_owned_b:e.fee_b};i[e.position]=s}return i}function Di(n){let i={},t=n.events?.filter(r=>r.type.includes("pool::CollectRewardEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:e}=t[r],s=e.position,l=e.reward,d=e.amount,f={coin_type:(0,G.fixCoinType)(l.name,!1),reward_owned:d},c=i[s];c?c.rewards.push(f):c={position_id:s,rewards:[f]},i[s]=c}return i}function Fi(n,i){let t=n.events?.filter(r=>r.type.includes("pool::SwapEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:e}=t[r],{partner:s,pool:l,amount_in:d,amount_out:f,fee:c,ref_fee:p,bin_swaps:_,from:y,target:P}=e,B=_.map(C=>({bin_id:C.bin_id.bits,in_amount:C.amount_in,out_amount:C.amount_out,fee:C.fee,var_fee_rate:C.var_fee_rate}));return{pool_id:l,a2b:i,in_amount:d,out_amount:f,ref_fee_amount:p,fee_amount:c,bin_swaps:B,partner:s,from_coin_type:(0,G.fixCoinType)(y.name,!1),to_coin_type:(0,G.fixCoinType)(P.name,!1)}}}function Wn(n){switch(n){case 0:return 0;case 1:return 1;case 2:return 2}}var Ir=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","ClosePositionEvent"];function Ni(n,i,t,r){let e=[],{timestampMs:s,events:l}=n;return l?.forEach((d,f)=>{let{name:c,address:p}=(0,G.extractStructTagFromType)(d.type);if(Ir.includes(c)&&p===t&&r===d.parsedJson.pool){let _={tx:d.id.txDigest,sender:d.sender,type:d.type,block_time:s||"0",index:`${i}_${f}`,parsed_json:d.parsedJson};e.push(_)}}),e}function Zn(n,i,t){let r=[],e=0,s=new Date(n*1e3).getTime(),l=new Date().getTime();for(;;){let d=s+e*t;if(d>=l&&r.push(d),e>=i)break;e+=1}return r}function Un(n,i,t,r){let e=[];for(let s=i;s<=t;s+=r){let l=n.findLast(d=>(0,G.d)(s).gte(d.time));l?e.push({time:s.toString(),emissions_per_second:l.emissions_per_second,emissions_per_day:l.emissions_per_day,visualized_time:new Date(s*1e3).toLocaleString()}):e.push({emissions_per_day:"0",time:s.toString(),emissions_per_second:"0",visualized_time:new Date(s*1e3).toLocaleString()})}return e}function Hn(n){if(n.length===0)return;let i=new Date().getTime()/1e3,t=n.findLast(r=>(0,G.d)(i).gte(r.time));return t||n[n.length-1]}function ne(n,i){let t=n.mul(i);if(t.gt(0)&&t.lt(1))throw new me(`Multiplication ${t} is less than 1`,"AmountTooSmall");return t.floor()}function Me(n){if(n.gt(0)&&n.lt(1))throw new me(`Multiplication ${n.toString()} is less than 1`,"AmountTooSmall");return n.floor()}function qi(n){switch(n){case 0:return"spot";case 1:return"curve";case 2:return"bid_ask"}}function Li(n,i,t,r){let e=Buffer.from(n,"utf8"),s=Buffer.from(i,"utf8"),l=s.length,d=0;for(;d<l;){let y=s[d];e=Buffer.concat([e,Buffer.from([y])]),d++}let f=Xt.bcs.u16().serialize(t).toBytes(),c=Xt.bcs.u16().serialize(r).toBytes(),p=Buffer.concat([e,f,c]),_=(0,Sr.blake2b)(p,void 0,32);return`0x${Buffer.from(_).toString("hex")}`}var E=require("@cetusprotocol/common-sdk");var ee=class n{static toWeight(i){console.log("\u{1F680} ~ WeightUtils ~ toWeight ~ options:",i);let{strategy_type:t,active_id:r,bin_step:e,lower_bin_id:s,upper_bin_id:l,total_amount_a:d,total_amount_b:f,active_bin_of_pool:c}=i,p=r<s||r>l,_=D.getQPriceFromId(r,e),y=(0,E.d)(0),P=(0,E.d)(0),B=(0,E.d)(200);if(t===2?B=(0,E.d)(200):t===1?B=(0,E.d)(2e3):t===0&&(B=(0,E.d)(1)),!p&&c){let st=this.calculateActiveWeights(c.amount_a,c.amount_b,_,B);y=st.active_weight_a,P=st.active_weight_b}r===s&&(0,E.d)(f).isZero()&&(y=(0,E.d)(B).mul((0,E.d)(2).pow(q*2)).div((0,E.d)(_)).floor(),P=(0,E.d)(0)),r===l&&(0,E.d)(d).isZero()&&(P=(0,E.d)(B).mul((0,E.d)(2).pow(q)).floor(),y=(0,E.d)(0)),r>s&&r<l&&((0,E.d)(d).isZero()&&(P=(0,E.d)(B).mul((0,E.d)(2).pow(q)).floor(),y=(0,E.d)(0)),(0,E.d)(f).isZero()&&(y=(0,E.d)(B).mul((0,E.d)(2).pow(q*2)).div((0,E.d)(_)).floor(),P=(0,E.d)(0)));let S=p?(0,E.d)(0):y,C=p?(0,E.d)(0):P,k=(0,E.d)(2e3).sub((0,E.d)(200)).floor(),R=r>l?l:r,x=r<s?s:r,T=r>s?R===s?(0,E.d)(0):k.div((0,E.d)(R-s)).floor():(0,E.d)(0),Z=l>r?x===l?(0,E.d)(0):k.div((0,E.d)(l-x)).floor():(0,E.d)(0),j=s,Y=[],nt=[];for(;j<=l;){let st=(0,E.d)(0);if(j<r){let dt=R-j;t===0?st=(0,E.d)(1):t===2?st=(0,E.d)(B).add(T.mul(dt)).floor():t===1&&(st=(0,E.d)(B).sub(T.mul(dt)).floor())}else if(j>r){let dt=j-x;t===0?st=(0,E.d)(1):t===2?st=(0,E.d)(B).add(Z.mul(dt)).floor():t===1&&(st=(0,E.d)(B).sub(Z.mul(dt)).floor())}else st=B;if(Y.push(st),j<r)C=C.add(st.mul((0,E.d)(2).pow(q))).floor(),nt.push((0,E.d)(0));else if(j>r){let dt=st.mul((0,E.d)(2).pow(q*2)).div(D.getQPriceFromId(j,e)).floor();nt.push(dt),S=S.add(dt).floor()}else nt.push((0,E.d)(0));j+=1}return{...i,total_weight_a:S,total_weight_b:C,active_weight_a:y,active_weight_b:P,weights:Y,weight_per_prices:nt}}static toWeightSpotBalanced(i,t){let r=[];for(let e=i;e<=t;e++)r.push({bin_id:e,weight:1});return r}static toWeightDescendingOrder(i,t){let r=[];for(let e=i;e<=t;e++)r.push({bin_id:e,weight:t-e+1});return r}static toWeightAscendingOrder(i,t){let r=[];for(let e=i;e<=t;e++)r.push({bin_id:e,weight:e-i+1});return r}static toWeightCurve(i,t,r){if(r<i)return n.toWeightDescendingOrder(i,t);if(r>t)return n.toWeightAscendingOrder(i,t);let e=2e3,l=e-200,d=r>i?Math.floor(l/(r-i)):0,f=t>r?Math.floor(l/(t-r)):0,c=[];for(let p=i;p<=t;p++)p<r?c.push({bin_id:p,weight:e-(r-p)*d}):p>r?c.push({bin_id:p,weight:e-(p-r)*f}):c.push({bin_id:p,weight:e});return c}static toWeightBidAsk(i,t,r){if(r>t)return n.toWeightDescendingOrder(i,t);if(r<i)return n.toWeightAscendingOrder(i,t);let e=2e3,s=200,l=e-s,d=r>i?Math.floor(l/(r-i)):0,f=t>r?Math.floor(l/(t-r)):0,c=[];for(let p=i;p<=t;p++)p<r?c.push({bin_id:p,weight:s+(r-p)*d}):p>r?c.push({bin_id:p,weight:s+(p-r)*f}):c.push({bin_id:p,weight:s});return c}static toAmountBidSide(i,t,r,e,s=!1){let l=e.filter(f=>f.bin_id<=i).reduce(function(f,c){return s?c.bin_id>i?f:f.add(c.weight):c.bin_id>=i?f:f.add(c.weight)},(0,E.d)(0));if(l.cmp((0,E.d)(0))!=1)throw Error("Invalid parameters");return{bins:e.map(f=>{let c=D.getPricePerLamportFromBinId(f.bin_id,r);if(!(f.bin_id<=i)||f.bin_id>=i&&!s)return{bin_id:f.bin_id,amount_a:"0",amount_b:"0",price_per_lamport:c,liquidity:"0"};{let _=(0,E.d)(f.weight).div(l),y=ne((0,E.d)(t),_).toString(),P="0",B=D.getQPriceFromId(f.bin_id,r),S=D.getLiquidity(P,y,B);return{bin_id:f.bin_id,amount_b:y,amount_a:P,price_per_lamport:c,liquidity:S}}}),amount_a:"0",amount_b:t}}static toAmountAskSide(i,t,r,e,s=!1){let l=e.filter(f=>f.bin_id>=i).reduce(function(f,c){if(c.bin_id<=i&&!s)return f;{let p=D.getPricePerLamportFromBinId(c.bin_id,t),_=new W(c.weight).div(p);return f.add(_)}},new W(0));if(l.cmp(new W(0))!=1)throw Error("Invalid parameters");return{bins:e.map(f=>{let c=D.getPricePerLamportFromBinId(f.bin_id,t);if(!(f.bin_id>=i)||f.bin_id<=i&&!s)return{bin_id:f.bin_id,amount_a:"0",amount_b:"0",price_per_lamport:c,liquidity:"0"};{let y=new W(f.weight).div(c).div(l),P=ne((0,E.d)(r),y).toString(),B="0",S=D.getQPriceFromId(f.bin_id,t),C=D.getLiquidity(P,B,S);return{bin_id:f.bin_id,amount_a:P,amount_b:B,price_per_lamport:c,liquidity:C}}}),amount_a:r,amount_b:"0"}}static toAmountBothSide(i,t,r,e,s,l,d){let f=!(0,E.d)(r).isZero()&&(0,E.d)(e).isZero(),c=(0,E.d)(r).isZero()&&!(0,E.d)(e).isZero();if(i>d[d.length-1].bin_id)return n.toAmountBidSide(i,e,t,d);if(c&&i!==d[d.length-1].bin_id)return n.toAmountBidSide(i,e,t,d,!0);if(i<d[0].bin_id)return n.toAmountAskSide(i,t,r,d);if(f&&i!==d[0].bin_id)return n.toAmountAskSide(i,t,r,d,!0);let p=d.filter(_=>_.bin_id===i);if(p.length===1){let{totalWeightA:_,totalWeightB:y,activeWeightA:P,activeWeightB:B}=n.calculateTotalWeights(t,d,i,p[0],s,l,f?"a":c?"b":void 0),S=new W(r.toString()).div(_),C=new W(e.toString()).div(y),k=d.map(T=>{let Z=D.getPricePerLamportFromBinId(T.bin_id,t);if(T.bin_id<i||T.bin_id===i&&c){let lt=ne(C,new W(T.weight)),v="0",o=D.getQPriceFromId(T.bin_id,t),u=D.getLiquidity(v,lt.toString(),o);return{bin_id:T.bin_id,amount_a:"0",amount_b:lt.toString(),price_per_lamport:Z,liquidity:u}}if(T.bin_id>i||T.bin_id===i&&f){let lt=new W(T.weight).div(Z),v=ne(S,new W(lt)),o="0",u=D.getQPriceFromId(T.bin_id,t),h=D.getLiquidity(v.toString(),o,u);return{bin_id:T.bin_id,amount_a:v.toString(),amount_b:"0",price_per_lamport:Z,liquidity:h}}let j=ne(S,P),Y=ne(C,B),nt=j.toString(),st=Y.toString(),dt=D.getQPriceFromId(T.bin_id,t),X=D.getLiquidity(nt,st,dt);return{bin_id:T.bin_id,amount_a:nt,amount_b:st,price_per_lamport:Z,liquidity:X}}),R=k.reduce((T,Z)=>(0,E.d)(T).add((0,E.d)(Z.amount_a)),(0,E.d)(0)).toString(),x=k.reduce((T,Z)=>(0,E.d)(T).add((0,E.d)(Z.amount_b)),(0,E.d)(0)).toString();return{bins:k,amount_a:R,amount_b:x}}else{let{totalWeightA:_,totalWeightB:y}=n.calculateTotalWeights(t,d,i),P=new W(r.toString()).div(_),B=new W(e.toString()).div(y),S=d.map(R=>{let x=D.getPricePerLamportFromBinId(R.bin_id,t);if(R.bin_id<i){let T=ne(B,new W(R.weight));return{bin_id:R.bin_id,amount_a:"0",amount_b:T.toString(),price_per_lamport:x}}else{let T=new W(R.weight).div(x),Z=ne(P,T);return{bin_id:R.bin_id,amount_a:Z.toString(),amount_b:"0",price_per_lamport:x}}}),C=S.reduce((R,x)=>(0,E.d)(R).add((0,E.d)(x.amount_a)),(0,E.d)(0)).toString(),k=S.reduce((R,x)=>(0,E.d)(R).add((0,E.d)(x.amount_b)),(0,E.d)(0)).toString();return{bins:S,amount_a:C,amount_b:k}}}static autoFillCoinByWeight(i,t,r,e,s,l,d){if(i>d[d.length-1].bin_id)return n.toAmountBidSide(i,r,t,d);if(i<d[0].bin_id)return n.toAmountAskSide(i,t,r,d);let f=d.filter(P=>P.bin_id===i),{totalWeightA:c,totalWeightB:p}=n.calculateTotalWeights(t,d,i,f.length===1?f[0]:void 0,f.length===1?s:void 0,f.length===1?l:void 0),_=(0,E.d)(0);e?_=c.isZero()?new W(0):new W(r).div(c):_=p.isZero()?new W(0):new W(r).div(p);let y=ne(_,e?p:c).toString();return n.toAmountBothSide(i,t,e?r:y,e?y:r,s,l,d)}static calculateActiveWeights(i,t,r,e){let s=(0,E.d)(r),l=(0,E.d)(i),d=(0,E.d)(t),f=(0,E.d)(0),c=(0,E.d)(0);if(l.isZero()&&d.isZero())f=(0,E.d)(e).mul((0,E.d)(2).pow(q*2)).div(s.mul(2)).floor(),c=(0,E.d)(e).mul((0,E.d)(2).pow(q)).div(2).floor();else{if(l.isZero())f=(0,E.d)(0);else{let p=d.mul((0,E.d)(2).pow(q)).div(l);f=(0,E.d)(e).mul((0,E.d)(2).pow(q*2)).div(s.add(p)).floor()}if(d.isZero())c=(0,E.d)(0);else{let p=(0,E.d)(2).pow(q).add(s.mul(l).div(d)).floor();c=(0,E.d)(e).mul((0,E.d)(2).pow(q*2)).div(p).floor()}}return{active_weight_a:f,active_weight_b:c}}static calculateTotalWeights(i,t,r,e,s,l,d){let f=(0,E.d)(D.getPricePerLamportFromBinId(r,i)),c=(0,E.d)(0),p=(0,E.d)(0);if(s&&l&&e&&!d)if((0,E.d)(s).isZero()&&(0,E.d)(l).isZero())c=new W(e.weight).div(f.mul(new W(2))),p=new W(e.weight).div(new W(2));else{let P=new W(s.toString()),B=new W(l.toString());(0,E.d)(s).isZero()||(c=new W(e.weight).div(f.add(B.div(P)))),(0,E.d)(l).isZero()||(p=new W(e.weight).div(new W(1).add(f.mul(P).div(B))))}let _=c,y=p;return t.forEach(P=>{if((P.bin_id<r||d==="b")&&(y=y.add(new W(P.weight))),P.bin_id>r||d==="a"){let B=D.getPricePerLamportFromBinId(P.bin_id,i),S=new W(P.weight).div(B);_=_.add(S)}}),{totalWeightA:_,totalWeightB:y,activeWeightA:c,activeWeightB:p}}};var Yt=require("@cetusprotocol/common-sdk");var Pe=class{static toAmountsByWeights(i){let{total_weight_a:t,total_weight_b:r,weights:e,weight_per_prices:s,total_amount_a:l,total_amount_b:d,active_weight_a:f,active_weight_b:c,lower_bin_id:p,upper_bin_id:_,active_id:y,bin_step:P}=i,B=r.isZero()?(0,Yt.d)(0):(0,Yt.d)(d).mul((0,Yt.d)(2).pow(q*2)).div(r).floor(),S=t.isZero()?(0,Yt.d)(0):(0,Yt.d)(l).mul((0,Yt.d)(2).pow(q*2)).div(t).floor(),C=Me(S.mul(f).div((0,Yt.d)(2).pow(q*2))),k=Me(B.mul(c).div((0,Yt.d)(2).pow(q*2))),R=_-p+1,x=p,T=0,Z=[];for(;T<R;){let Y=(0,Yt.d)(0),nt=(0,Yt.d)(0),st=D.getQPriceFromId(x,P);x<y?nt=Me(B.mul(e[T]).div((0,Yt.d)(2).pow(q))):x>y?Y=Me(S.mul(s[T]).div((0,Yt.d)(2).pow(q*2))):(Y=C,nt=k);let dt=D.getLiquidity(Y.toString(),nt.toString(),st);Z.push({bin_id:x,amount_a:Y.toString(),amount_b:nt.toString(),price_per_lamport:D.getPricePerLamportFromBinId(x,P),liquidity:dt}),x+=1,T+=1}return{bins:Z,amount_a:l.toString(),amount_b:d.toString()}}static toAmountsBothSideByStrategy(i,t,r,e,s,l,d,f){let c=ee.toWeight({strategy_type:d,active_id:i,bin_step:t,lower_bin_id:r,upper_bin_id:e,total_amount_a:s,total_amount_b:l,active_bin_of_pool:f});return this.toAmountsByWeights(c)}static autoFillCoinByStrategy(i,t,r,e,s,l,d,f){switch(d){case 0:{let c=ee.toWeightSpotBalanced(s,l);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}case 1:{let c=ee.toWeightCurve(s,l,i);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}case 2:{let c=ee.toWeightBidAsk(s,l,i);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}}}static autoFillCoinByStrategyV2(i,t,r,e,s,l,d,f){let c=this.autoFillCoinByStrategy(i,t,r,e,s,l,d,f),p=ee.toWeight({strategy_type:d,active_id:i,bin_step:t,lower_bin_id:s,upper_bin_id:l,total_amount_a:c.amount_a,total_amount_b:c.amount_b,active_bin_of_pool:f});return this.toAmountsByWeights(p)}};var ft=require("@cetusprotocol/common-sdk");var Oe=class n{static getVariableFee(i){let{volatility_accumulator:t,bin_step_config:r}=i,{variable_fee_control:e,bin_step:s}=r;return(0,ft.d)(e).gt(0)?(0,ft.d)(t).mul(s).pow(2).mul(e).add(99999999999).div(1e11).toFixed(0):"0"}static calculateCompositionFee(i,t){let r=(0,ft.d)(i).mul(t);return(0,ft.d)(r).mul((0,ft.d)(1e9).add(t)).div(1e18).toFixed(0)}static calculateProtocolFee(i,t){return(0,ft.d)(i).mul(t).div(1e4).ceil().toFixed(0)}static getProtocolFees(i,t,r){let e=n.calculateProtocolFee(i,r),s=n.calculateProtocolFee(t,r);return{protocol_fee_a:e,protocol_fee_b:s}}static getCompositionFees(i,t,r){let{bin_step_config:e}=r;if((0,ft.d)(i.liquidity||"0").eq((0,ft.d)(0)))return{fees_a:"0",fees_b:"0"};let{bin_step:s,base_factor:l}=e,d=D.getQPriceFromId(i.bin_id,s),f=D.getLiquidity(i.amount_a,i.amount_b,d),c=D.getLiquidity(t.amount_a,t.amount_b,d),p=(0,ft.d)(i.liquidity).mul(c).div(f).toFixed(0),{amount_a:_,amount_b:y}=D.calculateOutByShare({bin_id:i.bin_id,liquidity:(0,ft.d)(i.liquidity).add(p).toFixed(0),amount_a:(0,ft.d)(i.amount_a).add(t.amount_a).toFixed(0),amount_b:(0,ft.d)(i.amount_b).add(t.amount_b).toFixed(0),price_per_lamport:i.price_per_lamport},p),P=(0,ft.d)(s).mul(l).mul(10),B=n.getVariableFee(r),S=(0,ft.d)(P).add(B).toFixed(0);(0,ft.d)(S).gt(1e8)&&(S=1e8.toString());let C="0",k="0";return(0,ft.d)(_).gt(t.amount_a)&&(0,ft.d)(t.amount_b).gt(y)?k=n.calculateCompositionFee((0,ft.d)(t.amount_b).sub(y).toFixed(0),S):(0,ft.d)(y).gt(t.amount_b)&&(0,ft.d)(t.amount_a).gt(_)&&(C=n.calculateCompositionFee((0,ft.d)(t.amount_a).sub(_).toFixed(0),S)),{fees_a:C,fees_b:k}}};var ji=class{static calculateIlm(i){let{curvature:t,initial_price:r,max_price:e,bin_step:s,total_supply:l,pool_share_percentage:d,config:f}=i,{price_curve_points_num:c,liquidity_distribution_num:p,tokens_table_num:_,price_table_num:y}=f;if(d<0||d>100)throw new Error("Pool Share Percentage must be greater than 0 and less than 100.");let P=!1;if(e<r)throw new Error("Maximum Price must be greater or equal to Initial Price.");if(e===r&&t!==0)throw new Error("Curvature must be 0 when Maximum and Initial Price are equal.");if(e!==r&&t===0)throw new Error("Maximum and Initial Price must be equal when Curvature is 0.");e===r&&t===0&&(P=!0);let B=L=>j*Math.pow(L/Z,t)+r,S=(L,ot)=>{let Q=j*Math.pow(Z,-t)*Math.pow(L,t+1)/(t+1)+r*L,et=j*Math.pow(Z,-t)*Math.pow(ot,t+1)/(t+1)+r*ot;return Q-et},C=L=>Z*Math.pow((L-r)/j,1/t),k=L=>Z*Math.pow(L-r,1/t-1)/(t*Math.pow(j,1/t)),R=L=>Math.pow((L-r)/j,1/t)*Z,x=(L,ot,Q)=>{let et=ot/1e4,Ae=Math.log(L)/Math.log(1+et);return Q?Math.floor(Ae):Math.ceil(Ae)},T=(L,ot)=>{let Q=ot/1e4;return Math.pow(1+Q,L)},Z=l*d/100,j=e-r,Y=[],nt=[],st=x(r,s,!1),dt=x(e,s,!1),X=0,lt=st;if(P)Y.push(r),nt.push(r*Z);else for(;lt<=dt;){let L=T(lt,s),ot=T(lt+1,s),Q=R(ot)-R(L);X=X+Q,nt.push(Q*L),Y.push(L),lt++}let v={price_curve:{data:[],min_y:0,max_y:0},liquidity_curve:{data:[],min_y:0,max_y:0},dlmm_bins:{data:[],min_y:0,max_y:0},tokens_table:[],price_table:[],initial_fdv:r*l,final_fdv:e*l,usdc_in_pool:0};var o=j/100;let u=[],h=0,m=0;for(let L=0;L<c;L++){let ot=L*Z/(c-1),Q=P?e:B(ot);h=h<Q?h:Q,m=m>Q?m:Q,u.push({x:ot,y:Q})}v.price_curve={data:u,min_y:h,max_y:m};let b=[],M=0,A=0;if(P)M=0,A=Z,b=[{x:r,y:0},{x:r,y:Z/2},{x:r,y:Z}];else for(let L=0;L<p;L++){let ot=r+L*o,Q=k(ot);b.push({x:ot,y:Q}),M=M<Q?M:Q,A=A>Q?A:Q}v.liquidity_curve={data:b,min_y:M,max_y:A};let g=[],a=0,w=0;Y.forEach((L,ot)=>{let Q=L,et=nt[ot];g.push({x:Q,y:et}),a=a<et?a:et,w=w>et?w:et}),v.dlmm_bins={data:g,min_y:a,max_y:w};let F=[],N=0,U=Z/_;for(;N<=Z;){let L=B(N),ot=N,Q=S(ot,0);F.push({withdrawn:ot,price:L,usdc_in_pool:Q}),N=N+U}v.tokens_table=F;let V=Math.floor(j*1e10)/1e11,tt=[];if(P)tt.push({withdrawn:Z,price:r,usdc_in_pool:Z*r});else{let L=0,ot=r;for(;L<=y;){let Q=C(ot),et=S(Q,0);tt.push({withdrawn:Q,price:ot,usdc_in_pool:et}),ot=ot+V,L=L+1}}return v.price_table=tt,v.usdc_in_pool=tt[tt.length-1].usdc_in_pool,v}};var Er=require("@mysten/sui/client"),Wi=require("@mysten/sui/utils"),Te=require("@mysten/sui/bcs"),Ce=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async getPoolAddress(i,t,r,e){try{let s=Li(i,t,r,e),{dlmm_pool:l}=this._sdk.sdkOptions,{pools_id:d}=(0,it.getPackagerConfigs)(l),f=await this._sdk.FullClient.getDynamicFieldObject({parentId:d,name:{type:"0x2::object::ID",value:s}});return(0,it.getObjectFields)(f).value.fields.value.fields.pool_id}catch(s){return J("FetchError",s,{[it.DETAILS_KEYS.METHOD_NAME]:"getPoolAddress",[it.DETAILS_KEYS.REQUEST_PARAMS]:{coin_type_a:i,coin_type_b:t,bin_step:r,base_factor:e}})}}async getBasePoolList(i="all",t=!1){let{dlmm_pool:r}=this._sdk.sdkOptions,e={data:[],has_next_page:!1},s=i==="all",l=`${r.package_id}_getBasePoolList`;if(s){let d=this._sdk.getCache(l,t);if(d&&d.length>0)return e.data.push(...d),e}try{let d=`${r.package_id}::registry::CreatePoolEvent`,f=await this._sdk.FullClient.queryEventsByPage({MoveEventType:d},i);e.has_next_page=f.has_next_page,e.next_cursor=f.next_cursor,f.data.forEach(c=>{let p=Ci(c);e.data.push(p)}),s&&this._sdk.updateCache(`${r.package_id}_getPoolImmutables`,e.data)}catch(d){return J("FetchError",d,{[it.DETAILS_KEYS.METHOD_NAME]:"getBasePoolList"})}return e}async getPools(i="all",t=!1){let r={data:[],has_next_page:!1},e=await this.getBasePoolList(i,t);if(e.data.length===0)return r;try{let s=await this._sdk.FullClient.batchGetObjects(e.data.map(l=>l.id),{showContent:!0,showType:!0});r.has_next_page=e.has_next_page,r.next_cursor=e.next_cursor;for(let l of s){let d=Ee(l),f=`${d.id}_getDlmmPool`;this._sdk.updateCache(f,d),r.data.push(d)}}catch(s){return J("FetchError",s,{[it.DETAILS_KEYS.METHOD_NAME]:"getPools"})}return r}async getBinInfo(i,t,r,e=!0){try{let s=`${i}_getBinInfo_${t}`,l=this._sdk.getCache(s,e);if(l!==void 0)return l;let d=D.binScore(t),[f,c]=D.resolveBinPosition(d),_=(await this._sdk.FullClient.getDynamicFieldObject({parentId:i,name:{type:"u64",value:f}})).data.content.fields.value.fields.value.fields.group.fields.bins[c].fields,y=Ve(_);return this._sdk.updateCache(s,y),y}catch{return{bin_id:t,amount_a:"0",amount_b:"0",liquidity:"0",price_per_lamport:D.getPricePerLamportFromBinId(t,r)}}}async getBinInfoList(i){let t=[],r=i.map(s=>{let{bin_manager_handle:l,bin_id:d,bin_step:f}=s,c=D.binScore(d),[p,_]=D.resolveBinPosition(c),y=(0,it.deriveDynamicFieldIdByType)(l,p,"u64","u64");return{groupIndex:p,offsetInGroup:_,bin_id:d,bin_step:f,bin_manager_handle:l,dynamic_field_id:y}});return(await this._sdk.FullClient.batchGetObjects(r.map(s=>s.dynamic_field_id),{showContent:!0,showType:!0})).forEach((s,l)=>{let{offsetInGroup:d,bin_manager_handle:f,bin_step:c,bin_id:p}=r[l];try{let y=(0,it.getObjectFields)(s).value.fields.value.fields.group.fields.bins[d].fields,P=Ve(y);t.push({...P,bin_manager_handle:f,bin_step:c})}catch{t.push({bin_id:p,amount_a:"0",amount_b:"0",liquidity:"0",price_per_lamport:D.getPricePerLamportFromBinId(p,c),bin_manager_handle:f,bin_step:c})}}),t}async getTotalFeeRate(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:e,coin_type_b:s}=i,l=new xe.Transaction;l.moveCall({target:`${t.published_at}::pool::get_total_fee_rate`,arguments:[l.object(r)],typeArguments:[e,s]});let d=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:l,sender:(0,Wi.normalizeSuiAddress)("0x0")});return Te.bcs.struct("FeeRate",{base_fee_rate:Te.bcs.u64(),var_fee_rate:Te.bcs.u64(),total_fee_rate:Te.bcs.u64()}).parse(Uint8Array.from(d.results[0].returnValues[0][0]))}async getPoolBinInfo(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:e,coin_type_b:s}=i,l=1e3,d=[],f,c=!0;for(;c;){let p=new xe.Transaction,_;f!==void 0?_=p.moveCall({target:"0x1::option::some",arguments:[p.pure.u32(Number((0,it.asUintN)(BigInt(f))))],typeArguments:["u32"]}):_=p.moveCall({target:"0x1::option::none",typeArguments:["u32"]}),p.moveCall({target:`${t.published_at}::pool::fetch_bins`,arguments:[p.object(r),_,p.pure.u64(l)],typeArguments:[e,s]});let y=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:p,sender:(0,Wi.normalizeSuiAddress)("0x0")}),P=ki(y);d.push(...P),f=P.length>0?P[P.length-1].bin_id+1:void 0,c=P.length===l}return d.sort((p,_)=>p.bin_id-_.bin_id)}async getPoolTransactionList({pool_id:i,pagination_args:t,order:r="descending",full_rpc_url:e}){let{FullClient:s,sdkOptions:l}=this._sdk,d;e?d=(0,it.createFullClient)(new Er.SuiClient({url:e})):d=s;let f={data:[],has_next_page:!1},c=50,p=t,_=t.limit||10;do{let y=await d.queryTransactionBlocksByPage({ChangedObject:i},{...p,limit:50},r);y.data.forEach((P,B)=>{let S=Ni(P,B,l.dlmm_pool.package_id,i);f.data=[...f.data,...S]}),f.has_next_page=y.has_next_page,f.next_cursor=y.next_cursor,p.cursor=y.next_cursor}while(f.data.length<_&&f.has_next_page);return f.data.length>_&&(f.data=f.data.slice(0,_),f.has_next_page=!0),f.data.length>0&&(f.next_cursor=f.data[f.data.length-1].tx),f}async getRangeBinInfo(i,t,r,e){let s=[];for(let l=t;l<=r;l++){let d=await Promise.all([this.getBinInfo(i,l,e)]);s.push(...d)}return s}async getAssignPoolList(i){if(i.length===0)return[];let t=[];try{let r=await this._sdk.FullClient.batchGetObjects(i,{showContent:!0,showType:!0});for(let e of r){let s=Ee(e),l=`${s.id}_getDlmmPool`;this._sdk.updateCache(l,s),t.push(s)}}catch(r){return J("FetchError",r,{[it.DETAILS_KEYS.METHOD_NAME]:"getAssignPoolList"})}return t}async getPool(i,t=!0){try{let r=`${i}_getDlmmPool`,e=this._sdk.getCache(r,t);if(e!==void 0)return e;let s=await this._sdk.FullClient.getObject({id:i,options:{showType:!0,showContent:!0}}),l=Ee(s);return this._sdk.updateCache(r,l),l}catch(r){return J("FetchError",r,{[it.DETAILS_KEYS.METHOD_NAME]:"getPool",[it.DETAILS_KEYS.REQUEST_PARAMS]:i})}}async createPoolAndAddWithPricePayload(i){let{bin_step:t,url:r,coin_type_a:e,coin_type_b:s,bin_infos:l,price_base_coin:d,price:f,lower_price:c,upper_price:p,decimals_a:_,decimals_b:y,strategy_type:P,use_bin_infos:B,base_factor:S,pool_id:C}=i,k,R,x,T=l;if(d==="coin_a")k=D.getBinIdFromPrice(c,t,!1,_,y),R=D.getBinIdFromPrice(p,t,!0,_,y),x=D.getBinIdFromPrice(f,t,!0,_,y);else{k=D.getBinIdFromPrice((0,it.d)(1).div(p).toString(),t,!1,_,y),R=D.getBinIdFromPrice((0,it.d)(1).div(c).toString(),t,!0,_,y),x=D.getBinIdFromPrice((0,it.d)(1).div(f).toString(),t,!1,_,y);let Y={pool_id:C,amount_a:l.amount_b,amount_b:l.amount_a,active_id:x,bin_step:t,lower_bin_id:k,upper_bin_id:R,active_bin_of_pool:void 0,strategy_type:i.strategy_type};T=await this.sdk.Position.calculateAddLiquidityInfo(Y)}let j={bin_step:t,url:r,coin_type_a:e,coin_type_b:s,bin_infos:T,lower_bin_id:k,upper_bin_id:R,active_id:x,strategy_type:P,use_bin_infos:B,base_factor:S};return this.createPoolAndAddLiquidityPayload(j)}createPoolPayload(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{bin_step:e,base_factor:s,url:l,coin_type_a:d,coin_type_b:f,active_id:c}=i;t=t||new xe.Transaction;let{registry_id:p,global_config_id:_,versioned_id:y}=(0,it.getPackagerConfigs)(r),[P,B]=t.moveCall({target:`${r.published_at}::registry::create_pool_v2`,arguments:[t.object(p),t.pure.u16(e),t.pure.u16(s),t.pure.u32(Number((0,it.asUintN)(BigInt(c)))),t.pure.string(l||""),t.object(_),t.object(y),t.object(it.CLOCK_ADDRESS)],typeArguments:[d,f]});return t.moveCall({target:`${r.published_at}::registry::destroy_receipt`,arguments:[t.object(P),B,t.object(y)],typeArguments:[d,f]}),B}createPoolAndAddLiquidityPayload(i){let{bin_step:t,base_factor:r,url:e,active_id:s,coin_type_a:l,coin_type_b:d,bin_infos:f,lower_bin_id:c,upper_bin_id:p,strategy_type:_,use_bin_infos:y}=i,P=new xe.Transaction;(0,it.isSortedSymbols)((0,it.fixCoinType)(l,!1),(0,it.fixCoinType)(d,!1))&&J("InvalidCoinTypeSequence",new Error("invalid coin type sequence"),{[it.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[it.DETAILS_KEYS.REQUEST_PARAMS]:i});let B=p-c+1;B>1e3&&J("InvalidBinWidth",new Error("Width is too large"),{[it.DETAILS_KEYS.METHOD_NAME]:"openPosition",[it.DETAILS_KEYS.REQUEST_PARAMS]:i}),console.log("\u{1F680} ~ createPoolAndAddLiquidityPayload ~ option:",{...i,width:B});let C={pool_id:this.createPoolPayload({active_id:s,bin_step:t,base_factor:r,coin_type_a:l,coin_type_b:d},P),bin_infos:f,coin_type_a:l,coin_type_b:d,lower_bin_id:c,upper_bin_id:p,active_id:s,strategy_type:_,use_bin_infos:y,max_price_slippage:0,bin_step:t};return this.sdk.Position.addLiquidityPayload(C,P),P}};var Xe=require("@cetusprotocol/common-sdk"),Zi={env:"testnet",full_rpc_url:Xe.FullRpcUrlTestnet,graph_rpc_url:Xe.GraphRpcUrlTestnet,dlmm_pool:{package_id:"0xb382224d12558da5f87624765065a8c7e8f5c899d0ee890610e2bb4e8c686be9",published_at:"0x1cdac9c678c5ec89c80409db8865c9c3f8e6207dfd988fb4ddd11e2806db0bf9",version:1,config:{registry_id:"0xdc91c4f094557b9d2a35fc6159ef32649a54c4aa9350860bf0d4b52b5f0a3990",pools_id:"0xb518b1de84a1ba1ab6c9a2d71fcdf382ef36045a4660497b3d77fb0a6df7709e",global_config_id:"0xe84ebca8d61cdd9312cec3787204d0ee1063d424a81dcda62cfb8b1887041a9f",versioned_id:"0xf8478d6dc081bc266229f25ac9c31a96b5e99ecbd82222e883368dda95829065",admin_cap_id:"0x273dbda2a1d62460a01a07831aec0fa8191a41341d634fb43f15acdb627edbce",partners_id:"0xd7ac594ed2e7756f0d4a98503dd970f314f2619a10c3613c705253a155cc9fea"}},dlmm_router:{package_id:"0xbc76d04e910452518efd5fd63d5fffe77c24855063c9cdb5501e06896bc34908",published_at:"0xe122ffbcfe5091398ba15a60e654e6589fd586b3865d5b4118292754f8cde301",version:1},faucet:{package_id:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",published_at:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",version:1}};var O=require("@cetusprotocol/common-sdk"),pe=require("@mysten/sui/transactions"),Or=require("@mysten/sui/utils");var Je=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}buildPositionType(){return`${this._sdk.sdkOptions.dlmm_pool.package_id}::position::Position`}async getOwnerPositionList(i){let t=[];try{(await this._sdk.FullClient.getOwnedObjectsByPage(i,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{StructType:this.buildPositionType()}})).data.forEach(e=>{t.push(Ge(e))})}catch(r){console.log("\u{1F680} ~ PositionModule ~ getOwnerPositionList ~ error:",r),J("GetObjectError",r,{[O.DETAILS_KEYS.METHOD_NAME]:"getOwnerPositionList",[O.DETAILS_KEYS.REQUEST_PARAMS]:i})}return t}async getPosition(i){try{let t=await this._sdk.FullClient.getObject({id:i,options:{showType:!0,showContent:!0,showOwner:!0}});return Ge(t)}catch(t){return console.log("\u{1F680} ~ PositionModule ~ getPosition ~ error:",t),J("GetObjectError",t,{[O.DETAILS_KEYS.METHOD_NAME]:"getPosition",[O.DETAILS_KEYS.REQUEST_PARAMS]:i})}}collectFeePayload(i,t){let{pool_id:r,position_id:e,coin_type_a:s,coin_type_b:l}=i,{dlmm_pool:d}=this.sdk.sdkOptions,{versioned_id:f,global_config_id:c}=(0,O.getPackagerConfigs)(d);t=t||new pe.Transaction;let[p,_]=t.moveCall({target:`${d.published_at}::pool::collect_position_fee`,arguments:[t.object(r),t.object(e),t.object(c),t.object(f)],typeArguments:[s,l]}),y=O.CoinAssist.fromBalance(p,s,t),P=O.CoinAssist.fromBalance(_,l,t);return t.transferObjects([y,P],this.sdk.getSenderAddress()),t}updatePositionFeeAndRewards(i,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:e}=(0,O.getPackagerConfigs)(r),{pool_id:s,position_id:l,coin_type_a:d,coin_type_b:f}=i;return t.moveCall({target:`${r.published_at}::pool::update_position_fee_and_rewards`,arguments:[t.object(s),t.pure.id(l),t.object(e),t.object(O.CLOCK_ADDRESS)],typeArguments:[d,f]}),t}collectRewardPayload(i,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:e,global_config_id:s}=(0,O.getPackagerConfigs)(r);return t=t||new pe.Transaction,i.forEach(l=>{let{pool_id:d,position_id:f,reward_coins:c,coin_type_a:p,coin_type_b:_}=l;c.forEach(y=>{let P=t.moveCall({target:`${r.published_at}::pool::collect_position_reward`,arguments:[t.object(d),t.object(f),t.object(s),t.object(e)],typeArguments:[p,_,y]}),B=O.CoinAssist.fromBalance(P,y,t);t.transferObjects([B],this.sdk.getSenderAddress())})}),t}collectRewardAndFeePayload(i,t){return t=t||new pe.Transaction,i.forEach(r=>{let{pool_id:e,position_id:s,reward_coins:l,coin_type_a:d,coin_type_b:f}=r;this.updatePositionFeeAndRewards({pool_id:e,position_id:s,coin_type_a:d,coin_type_b:f},t),this.collectFeePayload({pool_id:e,position_id:s,coin_type_a:d,coin_type_b:f},t),this.collectRewardPayload([{pool_id:e,position_id:s,reward_coins:l,coin_type_a:d,coin_type_b:f}],t)}),t}validateActiveIdSlippage(i,t){let{pool_id:r,active_id:e,max_price_slippage:s,bin_step:l,coin_type_a:d,coin_type_b:f}=i,{dlmm_router:c}=this.sdk.sdkOptions,p=D.getBinShift(e,l,s),_=Number((0,O.asUintN)(BigInt(e)));return t.moveCall({target:`${c.published_at}::utils::validate_active_id_slippage`,arguments:[typeof r=="string"?t.object(r):r,t.pure.u32(Number(_)),t.pure.u32(Number(p))],typeArguments:[d,f]}),t}closePositionPayload(i,t){let{pool_id:r,position_id:e,reward_coins:s,coin_type_a:l,coin_type_b:d}=i,{dlmm_pool:f}=this.sdk.sdkOptions,{versioned_id:c,global_config_id:p}=(0,O.getPackagerConfigs)(f);t=t||new pe.Transaction,this.updatePositionFeeAndRewards({pool_id:r,position_id:e,coin_type_a:l,coin_type_b:d},t),this.collectRewardPayload([{pool_id:r,position_id:e,reward_coins:s,coin_type_a:l,coin_type_b:d}],t);let[_,y,P]=t.moveCall({target:`${f.published_at}::pool::close_position`,arguments:[t.object(r),t.object(e),t.object(p),t.object(c),t.object(O.CLOCK_ADDRESS)],typeArguments:[l,d]}),B=O.CoinAssist.fromBalance(y,l,t),S=O.CoinAssist.fromBalance(P,d,t);return t.moveCall({target:`${f.published_at}::pool::destroy_close_position_cert`,arguments:[_,t.object(c)],typeArguments:[]}),t.transferObjects([B,S],this.sdk.getSenderAddress()),t}async getActiveBinIfInRange(i,t,r,e,s,l=!1){if(e<=r&&e>=t)return await this._sdk.Pool.getBinInfo(i,e,s,l)}calculateRemoveLiquidityInfo(i){let{bins:t,active_id:r,coin_amount:e}=i,s="fix_amount_a"in i,l=t.filter(y=>y.bin_id<r),d=t.filter(y=>y.bin_id>r),f=(0,O.d)(0),c=(0,O.d)(0),p=[];if(s){p=[...t];let{fix_amount_a:y}=i,P=t.find(C=>C.bin_id===r),B=d.reduce((C,k)=>(0,O.d)(C).plus(k.amount_a),(0,O.d)(0)).add(P?.amount_a||"0"),S=l.reduce((C,k)=>(0,O.d)(C).plus(k.amount_b),(0,O.d)(0)).add(P?.amount_b||"0");f=y?B:S,c=(0,O.d)(e).gte(f)?(0,O.d)(1):(0,O.d)(e).div(f)}else{let{is_only_a:y}=i;p=y?d:l,f=p.reduce((P,B)=>(0,O.d)(P).plus(y?B.amount_a:B.amount_b),(0,O.d)(0)),c=(0,O.d)(e).gte(f)?(0,O.d)(1):(0,O.d)(e).div(f)}if((0,O.d)(f).isZero())return J("InsufficientLiquidity",new Error("Insufficient liquidity"),{[O.DETAILS_KEYS.METHOD_NAME]:"calculateRemoveLiquidityResult",[O.DETAILS_KEYS.REQUEST_PARAMS]:i});let _=D.processBinsByRate([...p],c.toFixed());if(_.has_invalid_amount){let y=(0,O.d)(e),P=[],B=s?i.fix_amount_a:i.is_only_a;for(let S of p){if(y.lte(0))break;let C=B?S.amount_a:S.amount_b,k=B?S.amount_b:S.amount_a,R=(0,O.d)((0,O.d)(y).div(C).toFixed(9,W.ROUND_UP));R=R.gt(1)?(0,O.d)(1):R;let x=(0,O.d)(C).mul(R);y=y.minus(x);let T=(0,O.d)(k).mul(R),Z=(0,O.d)(S.liquidity).mul(R);P.push({...S,amount_a:B?x.toFixed(0):T.toFixed(0),amount_b:B?T.toFixed(0):x.toFixed(0),liquidity:Z.toFixed(0)})}return{bins:P,amount_a:P.reduce((S,C)=>(0,O.d)(S).plus(C.amount_a),(0,O.d)(0)).toFixed(0),amount_b:P.reduce((S,C)=>(0,O.d)(S).plus(C.amount_b),(0,O.d)(0)).toFixed(0)}}return _.bins}async calculateAddLiquidityInfo(i){let t="fix_amount_a"in i,{active_id:r,bin_step:e,lower_bin_id:s,upper_bin_id:l,active_bin_of_pool:d,strategy_type:f,pool_id:c}=i,p;if(t){let{coin_amount:_,fix_amount_a:y}=i;p=Pe.autoFillCoinByStrategyV2(r,e,_,y,s,l,f,d)}else p=Pe.toAmountsBothSideByStrategy(r,e,s,l,i.amount_a,i.amount_b,f,d);if(d&&c){let _=p.bins.findIndex(y=>y.bin_id===r);if(_!==-1){let y=await this._sdk.Pool.getPool(c,!1);if(y){let{fees_a:P,fees_b:B}=Oe.getCompositionFees(d,p.bins[_],y.variable_parameters),S=p.bins[_];S.amount_a=(0,O.d)(S.amount_a).sub(P).toFixed(0),S.amount_b=(0,O.d)(S.amount_b).sub(B).toFixed(0),p.bins[_]=S,console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_a:",P),console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_b:",B)}}}return p}removeLiquidityPayload(i){let{pool_id:t,position_id:r,bin_infos:e,reward_coins:s,slippage:l,coin_type_a:d,coin_type_b:f,active_id:c,collect_fee:p,bin_step:_,remove_percent:y}=i,{dlmm_pool:P}=this.sdk.sdkOptions,{bins:B}=e,S=new pe.Transaction;(p||s.length>0)&&this.updatePositionFeeAndRewards({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},S),p&&this.collectFeePayload({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},S),this.collectRewardPayload([{pool_id:t,position_id:r,reward_coins:s,coin_type_a:d,coin_type_b:f}],S);let{versioned_id:C,global_config_id:k}=(0,O.getPackagerConfigs)(P);if(y){let R=(0,O.asUintN)(BigInt(B[0].bin_id)),x=(0,O.asUintN)(BigInt(B[B.length-1].bin_id)),T=Number((0,O.d)(y).mul(1e4).toFixed(0)),[Z,j]=S.moveCall({target:`${P.published_at}::pool::remove_liquidity_by_percent`,arguments:[S.object(t),S.object(r),S.pure.u32(Number(R)),S.pure.u32(Number(x)),S.pure.u16(T),S.object(k),S.object(C),S.object(O.CLOCK_ADDRESS)],typeArguments:[d,f]}),Y=O.CoinAssist.fromBalance(Z,d,S),nt=O.CoinAssist.fromBalance(j,f,S);S.transferObjects([Y,nt],this.sdk.getSenderAddress())}else{let R=S.pure.vector("u32",B.map(nt=>Number((0,O.asUintN)(BigInt(nt.bin_id))))),x=S.pure.vector("u128",B.map(nt=>nt.liquidity)),[T,Z]=S.moveCall({target:`${P.published_at}::pool::remove_liquidity`,arguments:[S.object(t),S.object(r),R,x,S.object(k),S.object(C),S.object(O.CLOCK_ADDRESS)],typeArguments:[d,f]}),j=O.CoinAssist.fromBalance(T,d,S),Y=O.CoinAssist.fromBalance(Z,f,S);S.transferObjects([j,Y],this.sdk.getSenderAddress())}return this.validateActiveIdSlippage({pool_id:t,active_id:c,max_price_slippage:l,bin_step:_,coin_type_a:d,coin_type_b:f},S),S}async addLiquidityWithPricePayload(i){let{pool_id:t,bin_infos:r,coin_type_a:e,coin_type_b:s,price_base_coin:l,price:d,lower_price:f,upper_price:c,bin_step:p,strategy_type:_,active_bin_of_pool:y,decimals_a:P,decimals_b:B,use_bin_infos:S,max_price_slippage:C}=i,k,R,x,T=r;if(l==="coin_a")k=D.getBinIdFromPrice(f,p,!1,P,B),R=D.getBinIdFromPrice(c,p,!0,P,B),x=D.getBinIdFromPrice(d,p,!0,P,B);else{k=D.getBinIdFromPrice((0,O.d)(1).div(c).toString(),p,!1,P,B),R=D.getBinIdFromPrice((0,O.d)(1).div(f).toString(),p,!0,P,B),x=D.getBinIdFromPrice((0,O.d)(1).div(d).toString(),p,!1,P,B);let Y={amount_a:r.amount_b,amount_b:r.amount_a,active_id:x,bin_step:p,lower_bin_id:k,upper_bin_id:R,active_bin_of_pool:y,strategy_type:_};T=await this.sdk.Position.calculateAddLiquidityInfo(Y)}let j={pool_id:t,active_id:x,bin_infos:T,coin_type_a:e,coin_type_b:s,lower_bin_id:k,upper_bin_id:R,strategy_type:_,use_bin_infos:S,max_price_slippage:C,bin_step:p};return this.addLiquidityPayload(j)}addLiquidityPayload(i,t){let{pool_id:r,bin_infos:e,coin_type_a:s,coin_type_b:l,active_id:d,strategy_type:f,max_price_slippage:c,bin_step:p,use_bin_infos:_=!1}=i;t=t||new pe.Transaction;let y="lower_bin_id"in i,P=[];if(y){let B=D.splitBinLiquidityInfo(e,i.lower_bin_id,i.upper_bin_id);P.push(...B)}else{let B=i.position_id;P.push(e),(i.collect_fee||i.reward_coins.length>0)&&this.updatePositionFeeAndRewards({pool_id:r,position_id:B,coin_type_a:s,coin_type_b:l},t),i.collect_fee&&this.collectFeePayload({pool_id:r,position_id:B,coin_type_a:s,coin_type_b:l},t),i.reward_coins.length>0&&this.collectRewardPayload([{pool_id:r,position_id:B,reward_coins:i.reward_coins,coin_type_a:s,coin_type_b:l}],t)}return P.forEach((B,S)=>{console.log("\u{1F680} ~ PositionModule ~ addLiquidityPayload ~ liquidity_bin:",S,B);let{amount_a:C,amount_b:k,bins:R}=B,x=O.CoinAssist.buildCoinWithBalance(BigInt(C),s,t),T=O.CoinAssist.buildCoinWithBalance(BigInt(k),l,t);_?this.addLiquidityInternal({pool_id:r,coin_type_a:s,coin_type_b:l,active_id:d,liquidity_bin:B,tx:t,coin_a_obj_id:x,coin_b_obj_id:T,position_id:y?void 0:i.position_id,max_price_slippage:c,bin_step:p}):this.addLiquidityStrategyInternal({pool_id:r,coin_type_a:s,coin_type_b:l,active_id:d,liquidity_bin:B,tx:t,max_price_slippage:c,bin_step:p,coin_a_obj_id:x,coin_b_obj_id:T,strategy_type:f,position_id:y?void 0:i.position_id})}),t}addLiquidityStrategyInternal(i){let{max_price_slippage:t,bin_step:r,position_id:e,pool_id:s,coin_type_a:l,coin_type_b:d,active_id:f,liquidity_bin:c,tx:p,coin_a_obj_id:_,coin_b_obj_id:y,strategy_type:P}=i,{dlmm_pool:B,dlmm_router:S}=this.sdk.sdkOptions,{versioned_id:C,global_config_id:k}=(0,O.getPackagerConfigs)(B),{bins:R,amount_a:x,amount_b:T}=c,Z=e,j=R[0].bin_id,Y=R[R.length-1].bin_id,nt=(0,O.asUintN)(BigInt(j)),st=Number((0,O.asUintN)(BigInt(f))),dt=D.getBinShift(f,r,t),X=qi(P);if(e===void 0){let lt=Y-j+1;lt>1e3&&J("InvalidBinWidth",new Error("Width is too large"),{[O.DETAILS_KEYS.METHOD_NAME]:"openPosition"}),Z=p.moveCall({target:`${S.published_at}::${X}::open_position`,arguments:[typeof s=="string"?p.object(s):s,_,y,p.pure.u64(x),p.pure.u64(T),p.pure.u32(Number(nt)),p.pure.u16(Number(lt)),p.pure.u32(Number(st)),p.pure.u32(Number(dt)),p.object(k),p.object(C),p.object(O.CLOCK_ADDRESS)],typeArguments:[l,d]})}else{let lt=R.filter(u=>u.amount_a!=="0"||u.amount_b!=="0");if(lt.length===0)return J("InvalidParams",new Error("No bins to add liquidity"),{[O.DETAILS_KEYS.METHOD_NAME]:"addLiquidityStrategyInternal",[O.DETAILS_KEYS.REQUEST_PARAMS]:i});let v=(0,O.asUintN)(BigInt(lt[0].bin_id)),o=(0,O.asUintN)(BigInt(lt[lt.length-1].bin_id));p.moveCall({target:`${S.published_at}::${X}::add_liquidity`,arguments:[typeof s=="string"?p.object(s):s,p.object(e),_,y,p.pure.u64(x),p.pure.u64(T),p.pure.u32(Number(v)),p.pure.u32(Number(o)),p.pure.u32(Number(st)),p.pure.u32(Number(dt)),p.object(k),p.object(C),p.object(O.CLOCK_ADDRESS)],typeArguments:[l,d]})}return Z?p.transferObjects([Z,_,y],this.sdk.getSenderAddress()):p.transferObjects([_,y],this.sdk.getSenderAddress()),p}addLiquidityInternal(i){let{position_id:t,pool_id:r,coin_type_a:e,coin_type_b:s,active_id:l,liquidity_bin:d,tx:f,coin_a_obj_id:c,coin_b_obj_id:p,max_price_slippage:_,bin_step:y}=i,{bins:P}=d,{dlmm_pool:B,dlmm_router:S}=this.sdk.sdkOptions,{versioned_id:C,global_config_id:k}=(0,O.getPackagerConfigs)(B),R=f.pure.vector("u64",P.map(Y=>Y.amount_a)),x=f.pure.vector("u64",P.map(Y=>Y.amount_b)),T=f.makeMoveVec({elements:P.map(Y=>f.pure.u32(Number((0,O.asUintN)(BigInt(Y.bin_id))))),type:"u32"}),Z=d.bins[0].bin_id,j=d.bins[d.bins.length-1].bin_id;if(t===void 0){j-Z+1>1e3&&J("InvalidBinWidth",new Error("Width is too large"),{[O.DETAILS_KEYS.METHOD_NAME]:"openPosition"});let nt=f.moveCall({target:`${S.published_at}::add_liquidity::open_position`,arguments:[typeof r=="string"?f.object(r):r,c,p,T,R,x,f.object(k),f.object(C),f.object(O.CLOCK_ADDRESS)],typeArguments:[e,s]});l>=Z&&l<=j&&this.validateActiveIdSlippage({pool_id:r,active_id:l,max_price_slippage:_,bin_step:y,coin_type_a:e,coin_type_b:s},f),f.transferObjects([c,p,nt],this.sdk.getSenderAddress())}else f.moveCall({target:`${S.published_at}::add_liquidity::add_liquidity`,arguments:[typeof r=="string"?f.object(r):r,f.object(t),c,p,T,R,x,f.object(k),f.object(C),f.object(O.CLOCK_ADDRESS)],typeArguments:[e,s]}),l>=Z&&l<=j&&this.validateActiveIdSlippage({pool_id:r,active_id:l,max_price_slippage:_,bin_step:y,coin_type_a:e,coin_type_b:s},f),f.transferObjects([c,p],this.sdk.getSenderAddress());return f}async fetchPositionFeeAndReward(i){let t=new pe.Transaction;this.collectRewardAndFeePayload(i,t);let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:t,sender:(0,Or.normalizeSuiAddress)("0x0")});if(r.error!=null)return J("FetchError",new Error(r.error),{[O.DETAILS_KEYS.METHOD_NAME]:"fetchPositionFeeAndReward",[O.DETAILS_KEYS.REQUEST_PARAMS]:{options:i,totalOptions:i.length}});let e=Ri(r),s=Di(r);return{feeData:e,rewardData:s}}};var Ui=require("@mysten/sui/transactions"),Gt=require("@cetusprotocol/common-sdk");var xr=require("@mysten/sui/utils");var Ye=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async preSwapQuote(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:e,coin_type_b:s,a2b:l,by_amount_in:d,in_amount:f}=i,{versioned_id:c,global_config_id:p}=(0,Gt.getPackagerConfigs)(t),_=new Ui.Transaction;_.moveCall({target:`${t.published_at}::pool::flash_swap`,arguments:[_.object(r),_.pure.bool(l),_.pure.bool(d),_.pure.u64(f),_.object(p),_.object(c),_.object(Gt.CLOCK_ADDRESS)],typeArguments:[e,s]});let y=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:_,sender:(0,xr.normalizeSuiAddress)("0x0")});if(y.error!=null)return J("FetchError",new Error(y.error),{[Gt.DETAILS_KEYS.METHOD_NAME]:"fetchSwapQuote",[Gt.DETAILS_KEYS.REQUEST_PARAMS]:i});let P=Fi(y,l);return P==null?J("FetchError",new Error("No quote info"),{[Gt.DETAILS_KEYS.METHOD_NAME]:"preSwapQuote",[Gt.DETAILS_KEYS.REQUEST_PARAMS]:i}):(P.a2b=l,P)}swapPayload(i){let{dlmm_pool:t,dlmm_router:r}=this._sdk.sdkOptions,{quote_obj:e,coin_type_a:s,coin_type_b:l,by_amount_in:d,slippage:f,partner:c}=i,{pool_id:p,in_amount:_,out_amount:y,a2b:P}=e,B=new Ui.Transaction,S=d?_:(0,Gt.d)(_).mul(1+f).toFixed(0),C=d?(0,Gt.d)(y).mul(1-f).toFixed(0):y,k=Gt.CoinAssist.buildCoinWithBalance(BigInt(S),P?s:l,B),{versioned_id:R,global_config_id:x}=(0,Gt.getPackagerConfigs)(t);return console.log("\u{1F680} ~ SwapModule ~ option:",{...i,in_amount_limit:S,out_amount_limit:C}),c?B.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b_with_partner":"swap_b2a_with_partner"}`,arguments:[B.object(p),B.object(c),k,B.pure.bool(d),B.pure.u64(BigInt(d?_:y)),B.pure.u64(BigInt(d?C:S)),B.object(x),B.object(R),B.object(Gt.CLOCK_ADDRESS)],typeArguments:[s,l]}):B.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b":"swap_b2a"}`,arguments:[B.object(p),k,B.pure.bool(d),B.pure.u64(BigInt(d?_:y)),B.pure.u64(BigInt(d?C:S)),B.object(x),B.object(R),B.object(Gt.CLOCK_ADDRESS)],typeArguments:[s,l]}),B.transferObjects([k],this.sdk.getSenderAddress()),B}};var ke=require("@mysten/sui/transactions"),rt=require("@cetusprotocol/common-sdk");var ti=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async getPartnerList(){let i=[],{dlmm_pool:t}=this._sdk.sdkOptions,{partners_id:r}=(0,rt.getPackagerConfigs)(t);try{let e=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}}),l=(0,rt.getObjectFields)(e).partners.fields.contents.map(d=>d.fields.value);l.length>0&&(await this._sdk.FullClient.batchGetObjects(l,{showContent:!0,showType:!0})).forEach(f=>{let c=ze(f);i.push(c)})}catch(e){return J("FetchError",e,{[rt.DETAILS_KEYS.METHOD_NAME]:"getPartnerList",[rt.DETAILS_KEYS.REQUEST_PARAMS]:r})}return i}async getPartnerCapId(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions;try{let e=`partner_cap_id_${i}_${t}`,s=this._sdk.getCache(e);if(s)return s;let l=await this._sdk.FullClient.getOwnedObjects({owner:i,options:{showContent:!0,showType:!0},filter:{StructType:`${r.package_id}::partner::PartnerCap`}}),d;return l.data.forEach(f=>{let c=(0,rt.getObjectFields)(f);c.partner_id===t&&(d=c.id.id,this._sdk.updateCache(e,d))}),d||J("NotFound",new Error("Partner cap not found"),{[rt.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[rt.DETAILS_KEYS.REQUEST_PARAMS]:{owner:i,partner_id:t}})}catch(e){return J("FetchError",e,{[rt.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[rt.DETAILS_KEYS.REQUEST_PARAMS]:{owner:i,partner_id:t}})}}async getPartnerBalance(i){try{let t=await this._sdk.FullClient.getDynamicFieldsByPage(i),r=[],e=t.data.map(s=>s.objectId);return e.length>0&&(await this._sdk.FullClient.batchGetObjects(e,{showContent:!0,showType:!0})).forEach(l=>{let d=(0,rt.getObjectFields)(l);console.log(d),r.push({coin_type:(0,rt.fixCoinType)(d.name,!1),balance:d.value})}),r}catch(t){return J("FetchError",t,{[rt.DETAILS_KEYS.METHOD_NAME]:"getPartnerBalance",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i})}}async getPartner(i){try{let t=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0}});return ze(t)}catch(t){return J("FetchError",t,{[rt.DETAILS_KEYS.METHOD_NAME]:"getPartner",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i})}}updateRefFeeRatePayload(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{partner_id:e,ref_fee_rate:s}=i,{global_config_id:l,versioned_id:d}=(0,rt.getPackagerConfigs)(r);t=t||new ke.Transaction;let f=(0,rt.d)(s).mul(1e4);return f.gt(1e4)?J("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[rt.DETAILS_KEYS.METHOD_NAME]:"updateRefFeeRatePayload",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i}):(t.moveCall({target:`${r.published_at}::partner::update_ref_fee_rate`,arguments:[t.object(e),t.pure.u64(f.toNumber()),t.object(l),t.object(d)],typeArguments:[]}),t)}async claimRefFeePayload(i){let{partner_id:t,partner_cap_id:r,fee_coin_types:e}=i,{dlmm_pool:s}=this._sdk.sdkOptions,{versioned_id:l}=(0,rt.getPackagerConfigs)(s),d=new ke.Transaction,f=r;return f||(f=await this.getPartnerCapId(this._sdk.getSenderAddress(),t)),e.forEach(c=>{d.moveCall({target:`${s.published_at}::partner::claim_ref_fee`,arguments:[d.object(t),d.object(f),d.object(l)],typeArguments:[c]})}),d}updateTimeRangePayload(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{partner_id:e,start_time:s,end_time:l}=i,{global_config_id:d,versioned_id:f}=(0,rt.getPackagerConfigs)(r);t=t||new ke.Transaction;let c=s>1e12?Math.floor(s/1e3):s,p=l>1e12?Math.floor(l/1e3):l;return p<=c?J("InvalidParams",new Error("end_time must be greater than start_time"),{[rt.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i}):(t.moveCall({target:`${r.published_at}::partner::update_time_range`,arguments:[t.object(e),t.pure.u64(c),t.pure.u64(p),t.object(d),t.object(f),t.object(rt.CLOCK_ADDRESS)],typeArguments:[]}),t)}createPartnerPayload(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{name:r,ref_fee_rate:e,start_time:s,end_time:l,recipient:d}=i,{partners_id:f,global_config_id:c,versioned_id:p}=(0,rt.getPackagerConfigs)(t),_=new ke.Transaction,y=(0,rt.d)(e).mul(1e4);if(y.gt(1e4))return J("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[rt.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i});let P=s>1e12?Math.floor(s/1e3):s,B=l>1e12?Math.floor(l/1e3):l;return B<=P?J("InvalidParams",new Error("end_time must be greater than start_time"),{[rt.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i}):(_.moveCall({target:`${t.published_at}::partner::create_partner`,arguments:[_.object(f),_.pure.string(r),_.pure.u64(y.toNumber()),_.pure.u64(P),_.pure.u64(B),_.pure.address(d),_.object(c),_.object(p)],typeArguments:[]}),_)}};var ei=require("@mysten/sui/transactions"),ct=require("@cetusprotocol/common-sdk"),Hi=Pi(Ne()),ii=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async getRewardPeriodEmission(i,t,r){let e=await this._sdk.FullClient.getDynamicFieldsByPage(i),s=[],l=e.data.map(p=>p.objectId);l.length>0&&(await this._sdk.FullClient.batchGetObjects(l,{showContent:!0})).forEach(_=>{let y=(0,ct.getObjectFields)(_),P=ct.MathUtil.u128ToI128(new Hi.default(y.value.fields.value.fields.bits)).toString(),B=y.name,S=new Date(Number(B)*1e3).toLocaleString(),k={emissions_per_second:"0",emissions_per_day:"0",emissions_per:ct.MathUtil.fromX64(new Hi.default(P)).toString(),time:B,visualized_time:S};s.push(k)});let d=s.sort((p,_)=>Number(p.time)-Number(_.time)),f=[];f.push({emissions_per_second:t,emissions_per_day:(0,ct.d)(t).mul(3600*24).toString(),emissions_per:"0",time:r.toString(),visualized_time:new Date(r*1e3).toLocaleString()});let c=t;for(let p=0;p<d.length;p++){let _=d[p];if((0,ct.d)(_.time).lte(r))continue;c=(0,ct.d)(c).add((0,ct.d)(_.emissions_per)).toString();let y=(0,ct.d)(c).mul(3600*24).toString();(0,ct.d)(c).lt(0)?(_.emissions_per_second="0",_.emissions_per_day="0"):(_.emissions_per_second=c,_.emissions_per_day=y),f.push(_)}return f}addRewardPayload(i,t){t=t||new ei.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:l,reward_coin_type:d,reward_amount:f,start_time_seconds:c,end_time_seconds:p,coin_type_a:_,coin_type_b:y}=i,P=ct.CoinAssist.buildCoinWithBalance(BigInt(f),d,t);return t.pure.option("u64",c),t.moveCall({target:`${r.published_at}::pool::add_reward`,arguments:[t.object(l),P,t.pure.option("u64",c),t.pure.u64(p),t.object(e),t.object(s),t.object(ct.CLOCK_ADDRESS)],typeArguments:[_,y,d]}),t}initRewardPayload(i,t){t=t||new ei.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:l,reward_coin_types:d,coin_type_a:f,coin_type_b:c}=i;return d.forEach(p=>{t.moveCall({target:`${r.published_at}::pool::initialize_reward`,arguments:[t.object(l),t.object(e),t.object(s),t.object(ct.CLOCK_ADDRESS)],typeArguments:[f,c,p]})}),t}buildRewardAccessPayload(i,t){t=t||new ei.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:l,type:d,coin_type_a:f,coin_type_b:c}=i;return t.moveCall({target:`${r.published_at}::pool::${d==="to_public"?"make_reward_public":"make_reward_private"}`,arguments:[t.object(l),t.object(e),t.object(s)],typeArguments:[f,c]}),t}};var Tr=require("@mysten/sui/transactions"),ie=require("@cetusprotocol/common-sdk");var ri=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}buildRewardWhiteListPayload(i,t){t=t||new Tr.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ie.getPackagerConfigs)(r),{reward_coin_types:l,type:d}=i;return l.forEach(f=>{t.moveCall({target:`${r.published_at}::config::${d==="add"?"add_reward_whitelist":"remove_reward_whitelist"}`,arguments:[t.object(e),t.object(s)],typeArguments:[f]})}),t}async getBinStepConfigList(i){let t=await this._sdk.FullClient.getDynamicFieldsByPage(i),r=[],e=t.data.map(s=>s.objectId);return e.length>0&&(await this._sdk.FullClient.batchGetObjects(e,{showContent:!0})).forEach(l=>{let f={...(0,ie.getObjectFields)(l).value.fields};r.push(f)}),r}async getDlmmGlobalConfig(){let{dlmm_pool:i}=this._sdk.sdkOptions,{global_config_id:t}=(0,ie.getPackagerConfigs)(i);try{let r=await this._sdk.FullClient.getObject({id:t,options:{showContent:!0}}),e=(0,ie.getObjectFields)(r),s=e.reward_config.fields,l=s.reward_white_list?.fields?.contents?.map(f=>(0,ie.fixCoinType)(f.fields.key.fields.name,!1))||[];return{id:e.id.id,acl:{id:e.acl.fields.permissions.fields.id.id,size:e.acl.fields.permissions.fields.size},allowed_list:{id:e.allowed_list.fields.id.id,size:e.allowed_list.fields.size},denied_list:{id:e.denied_list.fields.id.id,size:e.denied_list.fields.size},bin_steps:{id:e.bin_steps.fields.id.id,size:e.bin_steps.fields.size},reward_white_list:l,blocked_position:{id:e.restriction.fields.blocked_position.fields.permissions.fields.id.id,size:e.restriction.fields.blocked_position.fields.permissions.fields.size},blocked_user:{id:e.restriction.fields.blocked_user.fields.permissions.fields.id.id,size:e.restriction.fields.blocked_user.fields.permissions.fields.size},min_reward_duration:Number(s.min_reward_duration),non_manager_initialize_reward_cap:Number(s.manager_reserved_reward_init_slots),reward_public:s.reward_public}}catch(r){return console.log("fetchGlobalConfig error: ",r),J("FetchError",r,{[ie.DETAILS_KEYS.METHOD_NAME]:"getBinStepConfigs",[ie.DETAILS_KEYS.REQUEST_PARAMS]:t})}}async fetchDlmmSdkConfigs(){let{dlmm_pool:i}=this._sdk.sdkOptions,t={registry_id:"",pools_id:"",global_config_id:"",versioned_id:"",admin_cap_id:"",partners_id:""},e=(await this._sdk.FullClient.getObject({id:i.package_id,options:{showContent:!0,showPreviousTransaction:!0}})).data?.previousTransaction;(await this._sdk.FullClient.getTransactionBlock({digest:e,options:{showEvents:!0}})).events?.forEach(f=>{let c=f.type,p=f.parsedJson;c.includes("versioned::InitEvent")&&(t.versioned_id=p.versioned),c.includes("partner::InitPartnerEvent")&&(t.partners_id=p.partners_id),c.includes("config::InitEvent")&&(t.global_config_id=p.config_id),c.includes("admin_cap::InitEvent")&&(t.admin_cap_id=p.admin_cap_id),c.includes("registry::RegistryEvent")&&(t.registry_id=p.pools_id)});let l=await this._sdk.FullClient.getObject({id:t.registry_id,options:{showContent:!0}}),d=(0,ie.getObjectFields)(l);return t.pools_id=d.pools.fields.id.id,t}};var Re=class n extends Cr.SdkWrapper{constructor(i){super(i),this._pool=new Ce(this),this._position=new Je(this),this._swap=new Ye(this),this._partner=new ti(this),this._reward=new ii(this),this._config=new ri(this)}get Pool(){return this._pool}get Position(){return this._position}get Swap(){return this._swap}get Partner(){return this._partner}get Reward(){return this._reward}get Config(){return this._config}static createSDK(i){let{env:t="mainnet"}=i;return t==="mainnet"?n.createCustomSDK({...Ai,...i}):n.createCustomSDK({...Zi,...i})}static createCustomSDK(i){return new n(i)}};var Qn=Re;0&&(module.exports={BASIS_POINT,BASIS_POINT_MAX,BIN_BOUND,BinUtils,CetusDlmmSDK,DEFAULT_MAX_WEIGHT,DEFAULT_MIN_WEIGHT,FEE_PRECISION,FeeUtils,IlmUtils,MAX_BIN_ID,MAX_BIN_PER_POSITION,MAX_FEE_RATE,MIN_BIN_ID,ONE,PoolModule,REWARD_PERIOD,REWARD_PERIOD_START_AT,SCALE_OFFSET,StrategyType,StrategyUtils,WeightUtils,buildPoolKey,dlmmMainnet,dlmmTestnet,generateRewardSchedule,getRouterModule,parseBinInfo,parseBinInfoList,parseCurrentRewardPeriodEmission,parseDlmmBasePool,parseDlmmPool,parseDlmmPosition,parseLiquidityShares,parsePartner,parsePoolTransactionInfo,parseRewardPeriodEmission,parseStrategyType,parsedDlmmPosFeeData,parsedDlmmPosRewardData,parsedSwapQuoteData,poolFilterEvenTypes,safeAmount,safeMulAmount});
1
+ "use strict";var kr=Object.create;var De=Object.defineProperty;var Rr=Object.getOwnPropertyDescriptor;var Dr=Object.getOwnPropertyNames;var Fr=Object.getPrototypeOf,Nr=Object.prototype.hasOwnProperty;var qr=(n,i)=>()=>(i||n((i={exports:{}}).exports,i),i.exports),Lr=(n,i)=>{for(var t in i)De(n,t,{get:i[t],enumerable:!0})},ar=(n,i,t,r)=>{if(i&&typeof i=="object"||typeof i=="function")for(let e of Dr(i))!Nr.call(n,e)&&e!==t&&De(n,e,{get:()=>i[e],enumerable:!(r=Rr(i,e))||r.enumerable});return n};var Pi=(n,i,t)=>(t=n!=null?kr(Fr(n)):{},ar(i||!n||!n.__esModule?De(t,"default",{value:n,enumerable:!0}):t,n)),jr=n=>ar(De({},"__esModule",{value:!0}),n);var Ne=qr((ur,Bi)=>{"use strict";(function(n,i){"use strict";function t(v,o){if(!v)throw new Error(o||"Assertion failed")}function r(v,o){v.super_=o;var u=function(){};u.prototype=o.prototype,v.prototype=new u,v.prototype.constructor=v}function e(v,o,u){if(e.isBN(v))return v;this.negative=0,this.words=null,this.length=0,this.red=null,v!==null&&((o==="le"||o==="be")&&(u=o,o=10),this._init(v||0,o||10,u||"be"))}typeof n=="object"?n.exports=e:i.BN=e,e.BN=e,e.wordSize=26;var s;try{typeof window<"u"&&typeof window.Buffer<"u"?s=window.Buffer:s=require("buffer").Buffer}catch{}e.isBN=function(o){return o instanceof e?!0:o!==null&&typeof o=="object"&&o.constructor.wordSize===e.wordSize&&Array.isArray(o.words)},e.max=function(o,u){return o.cmp(u)>0?o:u},e.min=function(o,u){return o.cmp(u)<0?o:u},e.prototype._init=function(o,u,h){if(typeof o=="number")return this._initNumber(o,u,h);if(typeof o=="object")return this._initArray(o,u,h);u==="hex"&&(u=16),t(u===(u|0)&&u>=2&&u<=36),o=o.toString().replace(/\s+/g,"");var m=0;o[0]==="-"&&(m++,this.negative=1),m<o.length&&(u===16?this._parseHex(o,m,h):(this._parseBase(o,u,m),h==="le"&&this._initArray(this.toArray(),u,h)))},e.prototype._initNumber=function(o,u,h){o<0&&(this.negative=1,o=-o),o<67108864?(this.words=[o&67108863],this.length=1):o<4503599627370496?(this.words=[o&67108863,o/67108864&67108863],this.length=2):(t(o<9007199254740992),this.words=[o&67108863,o/67108864&67108863,1],this.length=3),h==="le"&&this._initArray(this.toArray(),u,h)},e.prototype._initArray=function(o,u,h){if(t(typeof o.length=="number"),o.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(o.length/3),this.words=new Array(this.length);for(var m=0;m<this.length;m++)this.words[m]=0;var b,M,A=0;if(h==="be")for(m=o.length-1,b=0;m>=0;m-=3)M=o[m]|o[m-1]<<8|o[m-2]<<16,this.words[b]|=M<<A&67108863,this.words[b+1]=M>>>26-A&67108863,A+=24,A>=26&&(A-=26,b++);else if(h==="le")for(m=0,b=0;m<o.length;m+=3)M=o[m]|o[m+1]<<8|o[m+2]<<16,this.words[b]|=M<<A&67108863,this.words[b+1]=M>>>26-A&67108863,A+=24,A>=26&&(A-=26,b++);return this._strip()};function l(v,o){var u=v.charCodeAt(o);if(u>=48&&u<=57)return u-48;if(u>=65&&u<=70)return u-55;if(u>=97&&u<=102)return u-87;t(!1,"Invalid character in "+v)}function d(v,o,u){var h=l(v,u);return u-1>=o&&(h|=l(v,u-1)<<4),h}e.prototype._parseHex=function(o,u,h){this.length=Math.ceil((o.length-u)/6),this.words=new Array(this.length);for(var m=0;m<this.length;m++)this.words[m]=0;var b=0,M=0,A;if(h==="be")for(m=o.length-1;m>=u;m-=2)A=d(o,u,m)<<b,this.words[M]|=A&67108863,b>=18?(b-=18,M+=1,this.words[M]|=A>>>26):b+=8;else{var g=o.length-u;for(m=g%2===0?u+1:u;m<o.length;m+=2)A=d(o,u,m)<<b,this.words[M]|=A&67108863,b>=18?(b-=18,M+=1,this.words[M]|=A>>>26):b+=8}this._strip()};function f(v,o,u,h){for(var m=0,b=0,M=Math.min(v.length,u),A=o;A<M;A++){var g=v.charCodeAt(A)-48;m*=h,g>=49?b=g-49+10:g>=17?b=g-17+10:b=g,t(g>=0&&b<h,"Invalid character"),m+=b}return m}e.prototype._parseBase=function(o,u,h){this.words=[0],this.length=1;for(var m=0,b=1;b<=67108863;b*=u)m++;m--,b=b/u|0;for(var M=o.length-h,A=M%m,g=Math.min(M,M-A)+h,a=0,w=h;w<g;w+=m)a=f(o,w,w+m,u),this.imuln(b),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a);if(A!==0){var F=1;for(a=f(o,w,o.length,u),w=0;w<A;w++)F*=u;this.imuln(F),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a)}this._strip()},e.prototype.copy=function(o){o.words=new Array(this.length);for(var u=0;u<this.length;u++)o.words[u]=this.words[u];o.length=this.length,o.negative=this.negative,o.red=this.red};function c(v,o){v.words=o.words,v.length=o.length,v.negative=o.negative,v.red=o.red}if(e.prototype._move=function(o){c(o,this)},e.prototype.clone=function(){var o=new e(null);return this.copy(o),o},e.prototype._expand=function(o){for(;this.length<o;)this.words[this.length++]=0;return this},e.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},e.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{e.prototype[Symbol.for("nodejs.util.inspect.custom")]=p}catch{e.prototype.inspect=p}else e.prototype.inspect=p;function p(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var _=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],y=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],P=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];e.prototype.toString=function(o,u){o=o||10,u=u|0||1;var h;if(o===16||o==="hex"){h="";for(var m=0,b=0,M=0;M<this.length;M++){var A=this.words[M],g=((A<<m|b)&16777215).toString(16);b=A>>>24-m&16777215,m+=2,m>=26&&(m-=26,M--),b!==0||M!==this.length-1?h=_[6-g.length]+g+h:h=g+h}for(b!==0&&(h=b.toString(16)+h);h.length%u!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}if(o===(o|0)&&o>=2&&o<=36){var a=y[o],w=P[o];h="";var F=this.clone();for(F.negative=0;!F.isZero();){var N=F.modrn(w).toString(o);F=F.idivn(w),F.isZero()?h=N+h:h=_[a-N.length]+N+h}for(this.isZero()&&(h="0"+h);h.length%u!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}t(!1,"Base should be between 2 and 36")},e.prototype.toNumber=function(){var o=this.words[0];return this.length===2?o+=this.words[1]*67108864:this.length===3&&this.words[2]===1?o+=4503599627370496+this.words[1]*67108864:this.length>2&&t(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-o:o},e.prototype.toJSON=function(){return this.toString(16,2)},s&&(e.prototype.toBuffer=function(o,u){return this.toArrayLike(s,o,u)}),e.prototype.toArray=function(o,u){return this.toArrayLike(Array,o,u)};var B=function(o,u){return o.allocUnsafe?o.allocUnsafe(u):new o(u)};e.prototype.toArrayLike=function(o,u,h){this._strip();var m=this.byteLength(),b=h||Math.max(1,m);t(m<=b,"byte array longer than desired length"),t(b>0,"Requested array length <= 0");var M=B(o,b),A=u==="le"?"LE":"BE";return this["_toArrayLike"+A](M,m),M},e.prototype._toArrayLikeLE=function(o,u){for(var h=0,m=0,b=0,M=0;b<this.length;b++){var A=this.words[b]<<M|m;o[h++]=A&255,h<o.length&&(o[h++]=A>>8&255),h<o.length&&(o[h++]=A>>16&255),M===6?(h<o.length&&(o[h++]=A>>24&255),m=0,M=0):(m=A>>>24,M+=2)}if(h<o.length)for(o[h++]=m;h<o.length;)o[h++]=0},e.prototype._toArrayLikeBE=function(o,u){for(var h=o.length-1,m=0,b=0,M=0;b<this.length;b++){var A=this.words[b]<<M|m;o[h--]=A&255,h>=0&&(o[h--]=A>>8&255),h>=0&&(o[h--]=A>>16&255),M===6?(h>=0&&(o[h--]=A>>24&255),m=0,M=0):(m=A>>>24,M+=2)}if(h>=0)for(o[h--]=m;h>=0;)o[h--]=0},Math.clz32?e.prototype._countBits=function(o){return 32-Math.clz32(o)}:e.prototype._countBits=function(o){var u=o,h=0;return u>=4096&&(h+=13,u>>>=13),u>=64&&(h+=7,u>>>=7),u>=8&&(h+=4,u>>>=4),u>=2&&(h+=2,u>>>=2),h+u},e.prototype._zeroBits=function(o){if(o===0)return 26;var u=o,h=0;return(u&8191)===0&&(h+=13,u>>>=13),(u&127)===0&&(h+=7,u>>>=7),(u&15)===0&&(h+=4,u>>>=4),(u&3)===0&&(h+=2,u>>>=2),(u&1)===0&&h++,h},e.prototype.bitLength=function(){var o=this.words[this.length-1],u=this._countBits(o);return(this.length-1)*26+u};function S(v){for(var o=new Array(v.bitLength()),u=0;u<o.length;u++){var h=u/26|0,m=u%26;o[u]=v.words[h]>>>m&1}return o}e.prototype.zeroBits=function(){if(this.isZero())return 0;for(var o=0,u=0;u<this.length;u++){var h=this._zeroBits(this.words[u]);if(o+=h,h!==26)break}return o},e.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},e.prototype.toTwos=function(o){return this.negative!==0?this.abs().inotn(o).iaddn(1):this.clone()},e.prototype.fromTwos=function(o){return this.testn(o-1)?this.notn(o).iaddn(1).ineg():this.clone()},e.prototype.isNeg=function(){return this.negative!==0},e.prototype.neg=function(){return this.clone().ineg()},e.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},e.prototype.iuor=function(o){for(;this.length<o.length;)this.words[this.length++]=0;for(var u=0;u<o.length;u++)this.words[u]=this.words[u]|o.words[u];return this._strip()},e.prototype.ior=function(o){return t((this.negative|o.negative)===0),this.iuor(o)},e.prototype.or=function(o){return this.length>o.length?this.clone().ior(o):o.clone().ior(this)},e.prototype.uor=function(o){return this.length>o.length?this.clone().iuor(o):o.clone().iuor(this)},e.prototype.iuand=function(o){var u;this.length>o.length?u=o:u=this;for(var h=0;h<u.length;h++)this.words[h]=this.words[h]&o.words[h];return this.length=u.length,this._strip()},e.prototype.iand=function(o){return t((this.negative|o.negative)===0),this.iuand(o)},e.prototype.and=function(o){return this.length>o.length?this.clone().iand(o):o.clone().iand(this)},e.prototype.uand=function(o){return this.length>o.length?this.clone().iuand(o):o.clone().iuand(this)},e.prototype.iuxor=function(o){var u,h;this.length>o.length?(u=this,h=o):(u=o,h=this);for(var m=0;m<h.length;m++)this.words[m]=u.words[m]^h.words[m];if(this!==u)for(;m<u.length;m++)this.words[m]=u.words[m];return this.length=u.length,this._strip()},e.prototype.ixor=function(o){return t((this.negative|o.negative)===0),this.iuxor(o)},e.prototype.xor=function(o){return this.length>o.length?this.clone().ixor(o):o.clone().ixor(this)},e.prototype.uxor=function(o){return this.length>o.length?this.clone().iuxor(o):o.clone().iuxor(this)},e.prototype.inotn=function(o){t(typeof o=="number"&&o>=0);var u=Math.ceil(o/26)|0,h=o%26;this._expand(u),h>0&&u--;for(var m=0;m<u;m++)this.words[m]=~this.words[m]&67108863;return h>0&&(this.words[m]=~this.words[m]&67108863>>26-h),this._strip()},e.prototype.notn=function(o){return this.clone().inotn(o)},e.prototype.setn=function(o,u){t(typeof o=="number"&&o>=0);var h=o/26|0,m=o%26;return this._expand(h+1),u?this.words[h]=this.words[h]|1<<m:this.words[h]=this.words[h]&~(1<<m),this._strip()},e.prototype.iadd=function(o){var u;if(this.negative!==0&&o.negative===0)return this.negative=0,u=this.isub(o),this.negative^=1,this._normSign();if(this.negative===0&&o.negative!==0)return o.negative=0,u=this.isub(o),o.negative=1,u._normSign();var h,m;this.length>o.length?(h=this,m=o):(h=o,m=this);for(var b=0,M=0;M<m.length;M++)u=(h.words[M]|0)+(m.words[M]|0)+b,this.words[M]=u&67108863,b=u>>>26;for(;b!==0&&M<h.length;M++)u=(h.words[M]|0)+b,this.words[M]=u&67108863,b=u>>>26;if(this.length=h.length,b!==0)this.words[this.length]=b,this.length++;else if(h!==this)for(;M<h.length;M++)this.words[M]=h.words[M];return this},e.prototype.add=function(o){var u;return o.negative!==0&&this.negative===0?(o.negative=0,u=this.sub(o),o.negative^=1,u):o.negative===0&&this.negative!==0?(this.negative=0,u=o.sub(this),this.negative=1,u):this.length>o.length?this.clone().iadd(o):o.clone().iadd(this)},e.prototype.isub=function(o){if(o.negative!==0){o.negative=0;var u=this.iadd(o);return o.negative=1,u._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(o),this.negative=1,this._normSign();var h=this.cmp(o);if(h===0)return this.negative=0,this.length=1,this.words[0]=0,this;var m,b;h>0?(m=this,b=o):(m=o,b=this);for(var M=0,A=0;A<b.length;A++)u=(m.words[A]|0)-(b.words[A]|0)+M,M=u>>26,this.words[A]=u&67108863;for(;M!==0&&A<m.length;A++)u=(m.words[A]|0)+M,M=u>>26,this.words[A]=u&67108863;if(M===0&&A<m.length&&m!==this)for(;A<m.length;A++)this.words[A]=m.words[A];return this.length=Math.max(this.length,A),m!==this&&(this.negative=1),this._strip()},e.prototype.sub=function(o){return this.clone().isub(o)};function C(v,o,u){u.negative=o.negative^v.negative;var h=v.length+o.length|0;u.length=h,h=h-1|0;var m=v.words[0]|0,b=o.words[0]|0,M=m*b,A=M&67108863,g=M/67108864|0;u.words[0]=A;for(var a=1;a<h;a++){for(var w=g>>>26,F=g&67108863,N=Math.min(a,o.length-1),U=Math.max(0,a-v.length+1);U<=N;U++){var V=a-U|0;m=v.words[V]|0,b=o.words[U]|0,M=m*b+F,w+=M/67108864|0,F=M&67108863}u.words[a]=F|0,g=w|0}return g!==0?u.words[a]=g|0:u.length--,u._strip()}var k=function(o,u,h){var m=o.words,b=u.words,M=h.words,A=0,g,a,w,F=m[0]|0,N=F&8191,U=F>>>13,V=m[1]|0,tt=V&8191,L=V>>>13,ot=m[2]|0,Q=ot&8191,et=ot>>>13,Ae=m[3]|0,ht=Ae&8191,pt=Ae>>>13,Qi=m[4]|0,mt=Qi&8191,_t=Qi>>>13,Ki=m[5]|0,gt=Ki&8191,bt=Ki>>>13,$i=m[6]|0,wt=$i&8191,vt=$i>>>13,zi=m[7]|0,yt=zi&8191,Mt=zi>>>13,Gi=m[8]|0,Pt=Gi&8191,At=Gi>>>13,Vi=m[9]|0,Bt=Vi&8191,St=Vi>>>13,Xi=b[0]|0,It=Xi&8191,Et=Xi>>>13,Ji=b[1]|0,Ot=Ji&8191,xt=Ji>>>13,Yi=b[2]|0,Tt=Yi&8191,Ct=Yi>>>13,tr=b[3]|0,kt=tr&8191,Rt=tr>>>13,er=b[4]|0,Dt=er&8191,Ft=er>>>13,ir=b[5]|0,Nt=ir&8191,qt=ir>>>13,rr=b[6]|0,Lt=rr&8191,jt=rr>>>13,nr=b[7]|0,Wt=nr&8191,Zt=nr>>>13,or=b[8]|0,Ut=or&8191,Ht=or>>>13,sr=b[9]|0,Qt=sr&8191,Kt=sr>>>13;h.negative=o.negative^u.negative,h.length=19,g=Math.imul(N,It),a=Math.imul(N,Et),a=a+Math.imul(U,It)|0,w=Math.imul(U,Et);var ni=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ni>>>26)|0,ni&=67108863,g=Math.imul(tt,It),a=Math.imul(tt,Et),a=a+Math.imul(L,It)|0,w=Math.imul(L,Et),g=g+Math.imul(N,Ot)|0,a=a+Math.imul(N,xt)|0,a=a+Math.imul(U,Ot)|0,w=w+Math.imul(U,xt)|0;var oi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(oi>>>26)|0,oi&=67108863,g=Math.imul(Q,It),a=Math.imul(Q,Et),a=a+Math.imul(et,It)|0,w=Math.imul(et,Et),g=g+Math.imul(tt,Ot)|0,a=a+Math.imul(tt,xt)|0,a=a+Math.imul(L,Ot)|0,w=w+Math.imul(L,xt)|0,g=g+Math.imul(N,Tt)|0,a=a+Math.imul(N,Ct)|0,a=a+Math.imul(U,Tt)|0,w=w+Math.imul(U,Ct)|0;var si=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(si>>>26)|0,si&=67108863,g=Math.imul(ht,It),a=Math.imul(ht,Et),a=a+Math.imul(pt,It)|0,w=Math.imul(pt,Et),g=g+Math.imul(Q,Ot)|0,a=a+Math.imul(Q,xt)|0,a=a+Math.imul(et,Ot)|0,w=w+Math.imul(et,xt)|0,g=g+Math.imul(tt,Tt)|0,a=a+Math.imul(tt,Ct)|0,a=a+Math.imul(L,Tt)|0,w=w+Math.imul(L,Ct)|0,g=g+Math.imul(N,kt)|0,a=a+Math.imul(N,Rt)|0,a=a+Math.imul(U,kt)|0,w=w+Math.imul(U,Rt)|0;var ai=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ai>>>26)|0,ai&=67108863,g=Math.imul(mt,It),a=Math.imul(mt,Et),a=a+Math.imul(_t,It)|0,w=Math.imul(_t,Et),g=g+Math.imul(ht,Ot)|0,a=a+Math.imul(ht,xt)|0,a=a+Math.imul(pt,Ot)|0,w=w+Math.imul(pt,xt)|0,g=g+Math.imul(Q,Tt)|0,a=a+Math.imul(Q,Ct)|0,a=a+Math.imul(et,Tt)|0,w=w+Math.imul(et,Ct)|0,g=g+Math.imul(tt,kt)|0,a=a+Math.imul(tt,Rt)|0,a=a+Math.imul(L,kt)|0,w=w+Math.imul(L,Rt)|0,g=g+Math.imul(N,Dt)|0,a=a+Math.imul(N,Ft)|0,a=a+Math.imul(U,Dt)|0,w=w+Math.imul(U,Ft)|0;var li=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(li>>>26)|0,li&=67108863,g=Math.imul(gt,It),a=Math.imul(gt,Et),a=a+Math.imul(bt,It)|0,w=Math.imul(bt,Et),g=g+Math.imul(mt,Ot)|0,a=a+Math.imul(mt,xt)|0,a=a+Math.imul(_t,Ot)|0,w=w+Math.imul(_t,xt)|0,g=g+Math.imul(ht,Tt)|0,a=a+Math.imul(ht,Ct)|0,a=a+Math.imul(pt,Tt)|0,w=w+Math.imul(pt,Ct)|0,g=g+Math.imul(Q,kt)|0,a=a+Math.imul(Q,Rt)|0,a=a+Math.imul(et,kt)|0,w=w+Math.imul(et,Rt)|0,g=g+Math.imul(tt,Dt)|0,a=a+Math.imul(tt,Ft)|0,a=a+Math.imul(L,Dt)|0,w=w+Math.imul(L,Ft)|0,g=g+Math.imul(N,Nt)|0,a=a+Math.imul(N,qt)|0,a=a+Math.imul(U,Nt)|0,w=w+Math.imul(U,qt)|0;var ui=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ui>>>26)|0,ui&=67108863,g=Math.imul(wt,It),a=Math.imul(wt,Et),a=a+Math.imul(vt,It)|0,w=Math.imul(vt,Et),g=g+Math.imul(gt,Ot)|0,a=a+Math.imul(gt,xt)|0,a=a+Math.imul(bt,Ot)|0,w=w+Math.imul(bt,xt)|0,g=g+Math.imul(mt,Tt)|0,a=a+Math.imul(mt,Ct)|0,a=a+Math.imul(_t,Tt)|0,w=w+Math.imul(_t,Ct)|0,g=g+Math.imul(ht,kt)|0,a=a+Math.imul(ht,Rt)|0,a=a+Math.imul(pt,kt)|0,w=w+Math.imul(pt,Rt)|0,g=g+Math.imul(Q,Dt)|0,a=a+Math.imul(Q,Ft)|0,a=a+Math.imul(et,Dt)|0,w=w+Math.imul(et,Ft)|0,g=g+Math.imul(tt,Nt)|0,a=a+Math.imul(tt,qt)|0,a=a+Math.imul(L,Nt)|0,w=w+Math.imul(L,qt)|0,g=g+Math.imul(N,Lt)|0,a=a+Math.imul(N,jt)|0,a=a+Math.imul(U,Lt)|0,w=w+Math.imul(U,jt)|0;var di=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(di>>>26)|0,di&=67108863,g=Math.imul(yt,It),a=Math.imul(yt,Et),a=a+Math.imul(Mt,It)|0,w=Math.imul(Mt,Et),g=g+Math.imul(wt,Ot)|0,a=a+Math.imul(wt,xt)|0,a=a+Math.imul(vt,Ot)|0,w=w+Math.imul(vt,xt)|0,g=g+Math.imul(gt,Tt)|0,a=a+Math.imul(gt,Ct)|0,a=a+Math.imul(bt,Tt)|0,w=w+Math.imul(bt,Ct)|0,g=g+Math.imul(mt,kt)|0,a=a+Math.imul(mt,Rt)|0,a=a+Math.imul(_t,kt)|0,w=w+Math.imul(_t,Rt)|0,g=g+Math.imul(ht,Dt)|0,a=a+Math.imul(ht,Ft)|0,a=a+Math.imul(pt,Dt)|0,w=w+Math.imul(pt,Ft)|0,g=g+Math.imul(Q,Nt)|0,a=a+Math.imul(Q,qt)|0,a=a+Math.imul(et,Nt)|0,w=w+Math.imul(et,qt)|0,g=g+Math.imul(tt,Lt)|0,a=a+Math.imul(tt,jt)|0,a=a+Math.imul(L,Lt)|0,w=w+Math.imul(L,jt)|0,g=g+Math.imul(N,Wt)|0,a=a+Math.imul(N,Zt)|0,a=a+Math.imul(U,Wt)|0,w=w+Math.imul(U,Zt)|0;var fi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(fi>>>26)|0,fi&=67108863,g=Math.imul(Pt,It),a=Math.imul(Pt,Et),a=a+Math.imul(At,It)|0,w=Math.imul(At,Et),g=g+Math.imul(yt,Ot)|0,a=a+Math.imul(yt,xt)|0,a=a+Math.imul(Mt,Ot)|0,w=w+Math.imul(Mt,xt)|0,g=g+Math.imul(wt,Tt)|0,a=a+Math.imul(wt,Ct)|0,a=a+Math.imul(vt,Tt)|0,w=w+Math.imul(vt,Ct)|0,g=g+Math.imul(gt,kt)|0,a=a+Math.imul(gt,Rt)|0,a=a+Math.imul(bt,kt)|0,w=w+Math.imul(bt,Rt)|0,g=g+Math.imul(mt,Dt)|0,a=a+Math.imul(mt,Ft)|0,a=a+Math.imul(_t,Dt)|0,w=w+Math.imul(_t,Ft)|0,g=g+Math.imul(ht,Nt)|0,a=a+Math.imul(ht,qt)|0,a=a+Math.imul(pt,Nt)|0,w=w+Math.imul(pt,qt)|0,g=g+Math.imul(Q,Lt)|0,a=a+Math.imul(Q,jt)|0,a=a+Math.imul(et,Lt)|0,w=w+Math.imul(et,jt)|0,g=g+Math.imul(tt,Wt)|0,a=a+Math.imul(tt,Zt)|0,a=a+Math.imul(L,Wt)|0,w=w+Math.imul(L,Zt)|0,g=g+Math.imul(N,Ut)|0,a=a+Math.imul(N,Ht)|0,a=a+Math.imul(U,Ut)|0,w=w+Math.imul(U,Ht)|0;var ci=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ci>>>26)|0,ci&=67108863,g=Math.imul(Bt,It),a=Math.imul(Bt,Et),a=a+Math.imul(St,It)|0,w=Math.imul(St,Et),g=g+Math.imul(Pt,Ot)|0,a=a+Math.imul(Pt,xt)|0,a=a+Math.imul(At,Ot)|0,w=w+Math.imul(At,xt)|0,g=g+Math.imul(yt,Tt)|0,a=a+Math.imul(yt,Ct)|0,a=a+Math.imul(Mt,Tt)|0,w=w+Math.imul(Mt,Ct)|0,g=g+Math.imul(wt,kt)|0,a=a+Math.imul(wt,Rt)|0,a=a+Math.imul(vt,kt)|0,w=w+Math.imul(vt,Rt)|0,g=g+Math.imul(gt,Dt)|0,a=a+Math.imul(gt,Ft)|0,a=a+Math.imul(bt,Dt)|0,w=w+Math.imul(bt,Ft)|0,g=g+Math.imul(mt,Nt)|0,a=a+Math.imul(mt,qt)|0,a=a+Math.imul(_t,Nt)|0,w=w+Math.imul(_t,qt)|0,g=g+Math.imul(ht,Lt)|0,a=a+Math.imul(ht,jt)|0,a=a+Math.imul(pt,Lt)|0,w=w+Math.imul(pt,jt)|0,g=g+Math.imul(Q,Wt)|0,a=a+Math.imul(Q,Zt)|0,a=a+Math.imul(et,Wt)|0,w=w+Math.imul(et,Zt)|0,g=g+Math.imul(tt,Ut)|0,a=a+Math.imul(tt,Ht)|0,a=a+Math.imul(L,Ut)|0,w=w+Math.imul(L,Ht)|0,g=g+Math.imul(N,Qt)|0,a=a+Math.imul(N,Kt)|0,a=a+Math.imul(U,Qt)|0,w=w+Math.imul(U,Kt)|0;var hi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(hi>>>26)|0,hi&=67108863,g=Math.imul(Bt,Ot),a=Math.imul(Bt,xt),a=a+Math.imul(St,Ot)|0,w=Math.imul(St,xt),g=g+Math.imul(Pt,Tt)|0,a=a+Math.imul(Pt,Ct)|0,a=a+Math.imul(At,Tt)|0,w=w+Math.imul(At,Ct)|0,g=g+Math.imul(yt,kt)|0,a=a+Math.imul(yt,Rt)|0,a=a+Math.imul(Mt,kt)|0,w=w+Math.imul(Mt,Rt)|0,g=g+Math.imul(wt,Dt)|0,a=a+Math.imul(wt,Ft)|0,a=a+Math.imul(vt,Dt)|0,w=w+Math.imul(vt,Ft)|0,g=g+Math.imul(gt,Nt)|0,a=a+Math.imul(gt,qt)|0,a=a+Math.imul(bt,Nt)|0,w=w+Math.imul(bt,qt)|0,g=g+Math.imul(mt,Lt)|0,a=a+Math.imul(mt,jt)|0,a=a+Math.imul(_t,Lt)|0,w=w+Math.imul(_t,jt)|0,g=g+Math.imul(ht,Wt)|0,a=a+Math.imul(ht,Zt)|0,a=a+Math.imul(pt,Wt)|0,w=w+Math.imul(pt,Zt)|0,g=g+Math.imul(Q,Ut)|0,a=a+Math.imul(Q,Ht)|0,a=a+Math.imul(et,Ut)|0,w=w+Math.imul(et,Ht)|0,g=g+Math.imul(tt,Qt)|0,a=a+Math.imul(tt,Kt)|0,a=a+Math.imul(L,Qt)|0,w=w+Math.imul(L,Kt)|0;var pi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(pi>>>26)|0,pi&=67108863,g=Math.imul(Bt,Tt),a=Math.imul(Bt,Ct),a=a+Math.imul(St,Tt)|0,w=Math.imul(St,Ct),g=g+Math.imul(Pt,kt)|0,a=a+Math.imul(Pt,Rt)|0,a=a+Math.imul(At,kt)|0,w=w+Math.imul(At,Rt)|0,g=g+Math.imul(yt,Dt)|0,a=a+Math.imul(yt,Ft)|0,a=a+Math.imul(Mt,Dt)|0,w=w+Math.imul(Mt,Ft)|0,g=g+Math.imul(wt,Nt)|0,a=a+Math.imul(wt,qt)|0,a=a+Math.imul(vt,Nt)|0,w=w+Math.imul(vt,qt)|0,g=g+Math.imul(gt,Lt)|0,a=a+Math.imul(gt,jt)|0,a=a+Math.imul(bt,Lt)|0,w=w+Math.imul(bt,jt)|0,g=g+Math.imul(mt,Wt)|0,a=a+Math.imul(mt,Zt)|0,a=a+Math.imul(_t,Wt)|0,w=w+Math.imul(_t,Zt)|0,g=g+Math.imul(ht,Ut)|0,a=a+Math.imul(ht,Ht)|0,a=a+Math.imul(pt,Ut)|0,w=w+Math.imul(pt,Ht)|0,g=g+Math.imul(Q,Qt)|0,a=a+Math.imul(Q,Kt)|0,a=a+Math.imul(et,Qt)|0,w=w+Math.imul(et,Kt)|0;var mi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(mi>>>26)|0,mi&=67108863,g=Math.imul(Bt,kt),a=Math.imul(Bt,Rt),a=a+Math.imul(St,kt)|0,w=Math.imul(St,Rt),g=g+Math.imul(Pt,Dt)|0,a=a+Math.imul(Pt,Ft)|0,a=a+Math.imul(At,Dt)|0,w=w+Math.imul(At,Ft)|0,g=g+Math.imul(yt,Nt)|0,a=a+Math.imul(yt,qt)|0,a=a+Math.imul(Mt,Nt)|0,w=w+Math.imul(Mt,qt)|0,g=g+Math.imul(wt,Lt)|0,a=a+Math.imul(wt,jt)|0,a=a+Math.imul(vt,Lt)|0,w=w+Math.imul(vt,jt)|0,g=g+Math.imul(gt,Wt)|0,a=a+Math.imul(gt,Zt)|0,a=a+Math.imul(bt,Wt)|0,w=w+Math.imul(bt,Zt)|0,g=g+Math.imul(mt,Ut)|0,a=a+Math.imul(mt,Ht)|0,a=a+Math.imul(_t,Ut)|0,w=w+Math.imul(_t,Ht)|0,g=g+Math.imul(ht,Qt)|0,a=a+Math.imul(ht,Kt)|0,a=a+Math.imul(pt,Qt)|0,w=w+Math.imul(pt,Kt)|0;var _i=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(_i>>>26)|0,_i&=67108863,g=Math.imul(Bt,Dt),a=Math.imul(Bt,Ft),a=a+Math.imul(St,Dt)|0,w=Math.imul(St,Ft),g=g+Math.imul(Pt,Nt)|0,a=a+Math.imul(Pt,qt)|0,a=a+Math.imul(At,Nt)|0,w=w+Math.imul(At,qt)|0,g=g+Math.imul(yt,Lt)|0,a=a+Math.imul(yt,jt)|0,a=a+Math.imul(Mt,Lt)|0,w=w+Math.imul(Mt,jt)|0,g=g+Math.imul(wt,Wt)|0,a=a+Math.imul(wt,Zt)|0,a=a+Math.imul(vt,Wt)|0,w=w+Math.imul(vt,Zt)|0,g=g+Math.imul(gt,Ut)|0,a=a+Math.imul(gt,Ht)|0,a=a+Math.imul(bt,Ut)|0,w=w+Math.imul(bt,Ht)|0,g=g+Math.imul(mt,Qt)|0,a=a+Math.imul(mt,Kt)|0,a=a+Math.imul(_t,Qt)|0,w=w+Math.imul(_t,Kt)|0;var gi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(gi>>>26)|0,gi&=67108863,g=Math.imul(Bt,Nt),a=Math.imul(Bt,qt),a=a+Math.imul(St,Nt)|0,w=Math.imul(St,qt),g=g+Math.imul(Pt,Lt)|0,a=a+Math.imul(Pt,jt)|0,a=a+Math.imul(At,Lt)|0,w=w+Math.imul(At,jt)|0,g=g+Math.imul(yt,Wt)|0,a=a+Math.imul(yt,Zt)|0,a=a+Math.imul(Mt,Wt)|0,w=w+Math.imul(Mt,Zt)|0,g=g+Math.imul(wt,Ut)|0,a=a+Math.imul(wt,Ht)|0,a=a+Math.imul(vt,Ut)|0,w=w+Math.imul(vt,Ht)|0,g=g+Math.imul(gt,Qt)|0,a=a+Math.imul(gt,Kt)|0,a=a+Math.imul(bt,Qt)|0,w=w+Math.imul(bt,Kt)|0;var bi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(bi>>>26)|0,bi&=67108863,g=Math.imul(Bt,Lt),a=Math.imul(Bt,jt),a=a+Math.imul(St,Lt)|0,w=Math.imul(St,jt),g=g+Math.imul(Pt,Wt)|0,a=a+Math.imul(Pt,Zt)|0,a=a+Math.imul(At,Wt)|0,w=w+Math.imul(At,Zt)|0,g=g+Math.imul(yt,Ut)|0,a=a+Math.imul(yt,Ht)|0,a=a+Math.imul(Mt,Ut)|0,w=w+Math.imul(Mt,Ht)|0,g=g+Math.imul(wt,Qt)|0,a=a+Math.imul(wt,Kt)|0,a=a+Math.imul(vt,Qt)|0,w=w+Math.imul(vt,Kt)|0;var wi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(wi>>>26)|0,wi&=67108863,g=Math.imul(Bt,Wt),a=Math.imul(Bt,Zt),a=a+Math.imul(St,Wt)|0,w=Math.imul(St,Zt),g=g+Math.imul(Pt,Ut)|0,a=a+Math.imul(Pt,Ht)|0,a=a+Math.imul(At,Ut)|0,w=w+Math.imul(At,Ht)|0,g=g+Math.imul(yt,Qt)|0,a=a+Math.imul(yt,Kt)|0,a=a+Math.imul(Mt,Qt)|0,w=w+Math.imul(Mt,Kt)|0;var vi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(vi>>>26)|0,vi&=67108863,g=Math.imul(Bt,Ut),a=Math.imul(Bt,Ht),a=a+Math.imul(St,Ut)|0,w=Math.imul(St,Ht),g=g+Math.imul(Pt,Qt)|0,a=a+Math.imul(Pt,Kt)|0,a=a+Math.imul(At,Qt)|0,w=w+Math.imul(At,Kt)|0;var yi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(yi>>>26)|0,yi&=67108863,g=Math.imul(Bt,Qt),a=Math.imul(Bt,Kt),a=a+Math.imul(St,Qt)|0,w=Math.imul(St,Kt);var Mi=(A+g|0)+((a&8191)<<13)|0;return A=(w+(a>>>13)|0)+(Mi>>>26)|0,Mi&=67108863,M[0]=ni,M[1]=oi,M[2]=si,M[3]=ai,M[4]=li,M[5]=ui,M[6]=di,M[7]=fi,M[8]=ci,M[9]=hi,M[10]=pi,M[11]=mi,M[12]=_i,M[13]=gi,M[14]=bi,M[15]=wi,M[16]=vi,M[17]=yi,M[18]=Mi,A!==0&&(M[19]=A,h.length++),h};Math.imul||(k=C);function R(v,o,u){u.negative=o.negative^v.negative,u.length=v.length+o.length;for(var h=0,m=0,b=0;b<u.length-1;b++){var M=m;m=0;for(var A=h&67108863,g=Math.min(b,o.length-1),a=Math.max(0,b-v.length+1);a<=g;a++){var w=b-a,F=v.words[w]|0,N=o.words[a]|0,U=F*N,V=U&67108863;M=M+(U/67108864|0)|0,V=V+A|0,A=V&67108863,M=M+(V>>>26)|0,m+=M>>>26,M&=67108863}u.words[b]=A,h=M,M=m}return h!==0?u.words[b]=h:u.length--,u._strip()}function x(v,o,u){return R(v,o,u)}e.prototype.mulTo=function(o,u){var h,m=this.length+o.length;return this.length===10&&o.length===10?h=k(this,o,u):m<63?h=C(this,o,u):m<1024?h=R(this,o,u):h=x(this,o,u),h};function T(v,o){this.x=v,this.y=o}T.prototype.makeRBT=function(o){for(var u=new Array(o),h=e.prototype._countBits(o)-1,m=0;m<o;m++)u[m]=this.revBin(m,h,o);return u},T.prototype.revBin=function(o,u,h){if(o===0||o===h-1)return o;for(var m=0,b=0;b<u;b++)m|=(o&1)<<u-b-1,o>>=1;return m},T.prototype.permute=function(o,u,h,m,b,M){for(var A=0;A<M;A++)m[A]=u[o[A]],b[A]=h[o[A]]},T.prototype.transform=function(o,u,h,m,b,M){this.permute(M,o,u,h,m,b);for(var A=1;A<b;A<<=1)for(var g=A<<1,a=Math.cos(2*Math.PI/g),w=Math.sin(2*Math.PI/g),F=0;F<b;F+=g)for(var N=a,U=w,V=0;V<A;V++){var tt=h[F+V],L=m[F+V],ot=h[F+V+A],Q=m[F+V+A],et=N*ot-U*Q;Q=N*Q+U*ot,ot=et,h[F+V]=tt+ot,m[F+V]=L+Q,h[F+V+A]=tt-ot,m[F+V+A]=L-Q,V!==g&&(et=a*N-w*U,U=a*U+w*N,N=et)}},T.prototype.guessLen13b=function(o,u){var h=Math.max(u,o)|1,m=h&1,b=0;for(h=h/2|0;h;h=h>>>1)b++;return 1<<b+1+m},T.prototype.conjugate=function(o,u,h){if(!(h<=1))for(var m=0;m<h/2;m++){var b=o[m];o[m]=o[h-m-1],o[h-m-1]=b,b=u[m],u[m]=-u[h-m-1],u[h-m-1]=-b}},T.prototype.normalize13b=function(o,u){for(var h=0,m=0;m<u/2;m++){var b=Math.round(o[2*m+1]/u)*8192+Math.round(o[2*m]/u)+h;o[m]=b&67108863,b<67108864?h=0:h=b/67108864|0}return o},T.prototype.convert13b=function(o,u,h,m){for(var b=0,M=0;M<u;M++)b=b+(o[M]|0),h[2*M]=b&8191,b=b>>>13,h[2*M+1]=b&8191,b=b>>>13;for(M=2*u;M<m;++M)h[M]=0;t(b===0),t((b&-8192)===0)},T.prototype.stub=function(o){for(var u=new Array(o),h=0;h<o;h++)u[h]=0;return u},T.prototype.mulp=function(o,u,h){var m=2*this.guessLen13b(o.length,u.length),b=this.makeRBT(m),M=this.stub(m),A=new Array(m),g=new Array(m),a=new Array(m),w=new Array(m),F=new Array(m),N=new Array(m),U=h.words;U.length=m,this.convert13b(o.words,o.length,A,m),this.convert13b(u.words,u.length,w,m),this.transform(A,M,g,a,m,b),this.transform(w,M,F,N,m,b);for(var V=0;V<m;V++){var tt=g[V]*F[V]-a[V]*N[V];a[V]=g[V]*N[V]+a[V]*F[V],g[V]=tt}return this.conjugate(g,a,m),this.transform(g,a,U,M,m,b),this.conjugate(U,M,m),this.normalize13b(U,m),h.negative=o.negative^u.negative,h.length=o.length+u.length,h._strip()},e.prototype.mul=function(o){var u=new e(null);return u.words=new Array(this.length+o.length),this.mulTo(o,u)},e.prototype.mulf=function(o){var u=new e(null);return u.words=new Array(this.length+o.length),x(this,o,u)},e.prototype.imul=function(o){return this.clone().mulTo(o,this)},e.prototype.imuln=function(o){var u=o<0;u&&(o=-o),t(typeof o=="number"),t(o<67108864);for(var h=0,m=0;m<this.length;m++){var b=(this.words[m]|0)*o,M=(b&67108863)+(h&67108863);h>>=26,h+=b/67108864|0,h+=M>>>26,this.words[m]=M&67108863}return h!==0&&(this.words[m]=h,this.length++),this.length=o===0?1:this.length,u?this.ineg():this},e.prototype.muln=function(o){return this.clone().imuln(o)},e.prototype.sqr=function(){return this.mul(this)},e.prototype.isqr=function(){return this.imul(this.clone())},e.prototype.pow=function(o){var u=S(o);if(u.length===0)return new e(1);for(var h=this,m=0;m<u.length&&u[m]===0;m++,h=h.sqr());if(++m<u.length)for(var b=h.sqr();m<u.length;m++,b=b.sqr())u[m]!==0&&(h=h.mul(b));return h},e.prototype.iushln=function(o){t(typeof o=="number"&&o>=0);var u=o%26,h=(o-u)/26,m=67108863>>>26-u<<26-u,b;if(u!==0){var M=0;for(b=0;b<this.length;b++){var A=this.words[b]&m,g=(this.words[b]|0)-A<<u;this.words[b]=g|M,M=A>>>26-u}M&&(this.words[b]=M,this.length++)}if(h!==0){for(b=this.length-1;b>=0;b--)this.words[b+h]=this.words[b];for(b=0;b<h;b++)this.words[b]=0;this.length+=h}return this._strip()},e.prototype.ishln=function(o){return t(this.negative===0),this.iushln(o)},e.prototype.iushrn=function(o,u,h){t(typeof o=="number"&&o>=0);var m;u?m=(u-u%26)/26:m=0;var b=o%26,M=Math.min((o-b)/26,this.length),A=67108863^67108863>>>b<<b,g=h;if(m-=M,m=Math.max(0,m),g){for(var a=0;a<M;a++)g.words[a]=this.words[a];g.length=M}if(M!==0)if(this.length>M)for(this.length-=M,a=0;a<this.length;a++)this.words[a]=this.words[a+M];else this.words[0]=0,this.length=1;var w=0;for(a=this.length-1;a>=0&&(w!==0||a>=m);a--){var F=this.words[a]|0;this.words[a]=w<<26-b|F>>>b,w=F&A}return g&&w!==0&&(g.words[g.length++]=w),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},e.prototype.ishrn=function(o,u,h){return t(this.negative===0),this.iushrn(o,u,h)},e.prototype.shln=function(o){return this.clone().ishln(o)},e.prototype.ushln=function(o){return this.clone().iushln(o)},e.prototype.shrn=function(o){return this.clone().ishrn(o)},e.prototype.ushrn=function(o){return this.clone().iushrn(o)},e.prototype.testn=function(o){t(typeof o=="number"&&o>=0);var u=o%26,h=(o-u)/26,m=1<<u;if(this.length<=h)return!1;var b=this.words[h];return!!(b&m)},e.prototype.imaskn=function(o){t(typeof o=="number"&&o>=0);var u=o%26,h=(o-u)/26;if(t(this.negative===0,"imaskn works only with positive numbers"),this.length<=h)return this;if(u!==0&&h++,this.length=Math.min(h,this.length),u!==0){var m=67108863^67108863>>>u<<u;this.words[this.length-1]&=m}return this._strip()},e.prototype.maskn=function(o){return this.clone().imaskn(o)},e.prototype.iaddn=function(o){return t(typeof o=="number"),t(o<67108864),o<0?this.isubn(-o):this.negative!==0?this.length===1&&(this.words[0]|0)<=o?(this.words[0]=o-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(o),this.negative=1,this):this._iaddn(o)},e.prototype._iaddn=function(o){this.words[0]+=o;for(var u=0;u<this.length&&this.words[u]>=67108864;u++)this.words[u]-=67108864,u===this.length-1?this.words[u+1]=1:this.words[u+1]++;return this.length=Math.max(this.length,u+1),this},e.prototype.isubn=function(o){if(t(typeof o=="number"),t(o<67108864),o<0)return this.iaddn(-o);if(this.negative!==0)return this.negative=0,this.iaddn(o),this.negative=1,this;if(this.words[0]-=o,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var u=0;u<this.length&&this.words[u]<0;u++)this.words[u]+=67108864,this.words[u+1]-=1;return this._strip()},e.prototype.addn=function(o){return this.clone().iaddn(o)},e.prototype.subn=function(o){return this.clone().isubn(o)},e.prototype.iabs=function(){return this.negative=0,this},e.prototype.abs=function(){return this.clone().iabs()},e.prototype._ishlnsubmul=function(o,u,h){var m=o.length+h,b;this._expand(m);var M,A=0;for(b=0;b<o.length;b++){M=(this.words[b+h]|0)+A;var g=(o.words[b]|0)*u;M-=g&67108863,A=(M>>26)-(g/67108864|0),this.words[b+h]=M&67108863}for(;b<this.length-h;b++)M=(this.words[b+h]|0)+A,A=M>>26,this.words[b+h]=M&67108863;if(A===0)return this._strip();for(t(A===-1),A=0,b=0;b<this.length;b++)M=-(this.words[b]|0)+A,A=M>>26,this.words[b]=M&67108863;return this.negative=1,this._strip()},e.prototype._wordDiv=function(o,u){var h=this.length-o.length,m=this.clone(),b=o,M=b.words[b.length-1]|0,A=this._countBits(M);h=26-A,h!==0&&(b=b.ushln(h),m.iushln(h),M=b.words[b.length-1]|0);var g=m.length-b.length,a;if(u!=="mod"){a=new e(null),a.length=g+1,a.words=new Array(a.length);for(var w=0;w<a.length;w++)a.words[w]=0}var F=m.clone()._ishlnsubmul(b,1,g);F.negative===0&&(m=F,a&&(a.words[g]=1));for(var N=g-1;N>=0;N--){var U=(m.words[b.length+N]|0)*67108864+(m.words[b.length+N-1]|0);for(U=Math.min(U/M|0,67108863),m._ishlnsubmul(b,U,N);m.negative!==0;)U--,m.negative=0,m._ishlnsubmul(b,1,N),m.isZero()||(m.negative^=1);a&&(a.words[N]=U)}return a&&a._strip(),m._strip(),u!=="div"&&h!==0&&m.iushrn(h),{div:a||null,mod:m}},e.prototype.divmod=function(o,u,h){if(t(!o.isZero()),this.isZero())return{div:new e(0),mod:new e(0)};var m,b,M;return this.negative!==0&&o.negative===0?(M=this.neg().divmod(o,u),u!=="mod"&&(m=M.div.neg()),u!=="div"&&(b=M.mod.neg(),h&&b.negative!==0&&b.iadd(o)),{div:m,mod:b}):this.negative===0&&o.negative!==0?(M=this.divmod(o.neg(),u),u!=="mod"&&(m=M.div.neg()),{div:m,mod:M.mod}):(this.negative&o.negative)!==0?(M=this.neg().divmod(o.neg(),u),u!=="div"&&(b=M.mod.neg(),h&&b.negative!==0&&b.isub(o)),{div:M.div,mod:b}):o.length>this.length||this.cmp(o)<0?{div:new e(0),mod:this}:o.length===1?u==="div"?{div:this.divn(o.words[0]),mod:null}:u==="mod"?{div:null,mod:new e(this.modrn(o.words[0]))}:{div:this.divn(o.words[0]),mod:new e(this.modrn(o.words[0]))}:this._wordDiv(o,u)},e.prototype.div=function(o){return this.divmod(o,"div",!1).div},e.prototype.mod=function(o){return this.divmod(o,"mod",!1).mod},e.prototype.umod=function(o){return this.divmod(o,"mod",!0).mod},e.prototype.divRound=function(o){var u=this.divmod(o);if(u.mod.isZero())return u.div;var h=u.div.negative!==0?u.mod.isub(o):u.mod,m=o.ushrn(1),b=o.andln(1),M=h.cmp(m);return M<0||b===1&&M===0?u.div:u.div.negative!==0?u.div.isubn(1):u.div.iaddn(1)},e.prototype.modrn=function(o){var u=o<0;u&&(o=-o),t(o<=67108863);for(var h=(1<<26)%o,m=0,b=this.length-1;b>=0;b--)m=(h*m+(this.words[b]|0))%o;return u?-m:m},e.prototype.modn=function(o){return this.modrn(o)},e.prototype.idivn=function(o){var u=o<0;u&&(o=-o),t(o<=67108863);for(var h=0,m=this.length-1;m>=0;m--){var b=(this.words[m]|0)+h*67108864;this.words[m]=b/o|0,h=b%o}return this._strip(),u?this.ineg():this},e.prototype.divn=function(o){return this.clone().idivn(o)},e.prototype.egcd=function(o){t(o.negative===0),t(!o.isZero());var u=this,h=o.clone();u.negative!==0?u=u.umod(o):u=u.clone();for(var m=new e(1),b=new e(0),M=new e(0),A=new e(1),g=0;u.isEven()&&h.isEven();)u.iushrn(1),h.iushrn(1),++g;for(var a=h.clone(),w=u.clone();!u.isZero();){for(var F=0,N=1;(u.words[0]&N)===0&&F<26;++F,N<<=1);if(F>0)for(u.iushrn(F);F-- >0;)(m.isOdd()||b.isOdd())&&(m.iadd(a),b.isub(w)),m.iushrn(1),b.iushrn(1);for(var U=0,V=1;(h.words[0]&V)===0&&U<26;++U,V<<=1);if(U>0)for(h.iushrn(U);U-- >0;)(M.isOdd()||A.isOdd())&&(M.iadd(a),A.isub(w)),M.iushrn(1),A.iushrn(1);u.cmp(h)>=0?(u.isub(h),m.isub(M),b.isub(A)):(h.isub(u),M.isub(m),A.isub(b))}return{a:M,b:A,gcd:h.iushln(g)}},e.prototype._invmp=function(o){t(o.negative===0),t(!o.isZero());var u=this,h=o.clone();u.negative!==0?u=u.umod(o):u=u.clone();for(var m=new e(1),b=new e(0),M=h.clone();u.cmpn(1)>0&&h.cmpn(1)>0;){for(var A=0,g=1;(u.words[0]&g)===0&&A<26;++A,g<<=1);if(A>0)for(u.iushrn(A);A-- >0;)m.isOdd()&&m.iadd(M),m.iushrn(1);for(var a=0,w=1;(h.words[0]&w)===0&&a<26;++a,w<<=1);if(a>0)for(h.iushrn(a);a-- >0;)b.isOdd()&&b.iadd(M),b.iushrn(1);u.cmp(h)>=0?(u.isub(h),m.isub(b)):(h.isub(u),b.isub(m))}var F;return u.cmpn(1)===0?F=m:F=b,F.cmpn(0)<0&&F.iadd(o),F},e.prototype.gcd=function(o){if(this.isZero())return o.abs();if(o.isZero())return this.abs();var u=this.clone(),h=o.clone();u.negative=0,h.negative=0;for(var m=0;u.isEven()&&h.isEven();m++)u.iushrn(1),h.iushrn(1);do{for(;u.isEven();)u.iushrn(1);for(;h.isEven();)h.iushrn(1);var b=u.cmp(h);if(b<0){var M=u;u=h,h=M}else if(b===0||h.cmpn(1)===0)break;u.isub(h)}while(!0);return h.iushln(m)},e.prototype.invm=function(o){return this.egcd(o).a.umod(o)},e.prototype.isEven=function(){return(this.words[0]&1)===0},e.prototype.isOdd=function(){return(this.words[0]&1)===1},e.prototype.andln=function(o){return this.words[0]&o},e.prototype.bincn=function(o){t(typeof o=="number");var u=o%26,h=(o-u)/26,m=1<<u;if(this.length<=h)return this._expand(h+1),this.words[h]|=m,this;for(var b=m,M=h;b!==0&&M<this.length;M++){var A=this.words[M]|0;A+=b,b=A>>>26,A&=67108863,this.words[M]=A}return b!==0&&(this.words[M]=b,this.length++),this},e.prototype.isZero=function(){return this.length===1&&this.words[0]===0},e.prototype.cmpn=function(o){var u=o<0;if(this.negative!==0&&!u)return-1;if(this.negative===0&&u)return 1;this._strip();var h;if(this.length>1)h=1;else{u&&(o=-o),t(o<=67108863,"Number is too big");var m=this.words[0]|0;h=m===o?0:m<o?-1:1}return this.negative!==0?-h|0:h},e.prototype.cmp=function(o){if(this.negative!==0&&o.negative===0)return-1;if(this.negative===0&&o.negative!==0)return 1;var u=this.ucmp(o);return this.negative!==0?-u|0:u},e.prototype.ucmp=function(o){if(this.length>o.length)return 1;if(this.length<o.length)return-1;for(var u=0,h=this.length-1;h>=0;h--){var m=this.words[h]|0,b=o.words[h]|0;if(m!==b){m<b?u=-1:m>b&&(u=1);break}}return u},e.prototype.gtn=function(o){return this.cmpn(o)===1},e.prototype.gt=function(o){return this.cmp(o)===1},e.prototype.gten=function(o){return this.cmpn(o)>=0},e.prototype.gte=function(o){return this.cmp(o)>=0},e.prototype.ltn=function(o){return this.cmpn(o)===-1},e.prototype.lt=function(o){return this.cmp(o)===-1},e.prototype.lten=function(o){return this.cmpn(o)<=0},e.prototype.lte=function(o){return this.cmp(o)<=0},e.prototype.eqn=function(o){return this.cmpn(o)===0},e.prototype.eq=function(o){return this.cmp(o)===0},e.red=function(o){return new X(o)},e.prototype.toRed=function(o){return t(!this.red,"Already a number in reduction context"),t(this.negative===0,"red works only with positives"),o.convertTo(this)._forceRed(o)},e.prototype.fromRed=function(){return t(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},e.prototype._forceRed=function(o){return this.red=o,this},e.prototype.forceRed=function(o){return t(!this.red,"Already a number in reduction context"),this._forceRed(o)},e.prototype.redAdd=function(o){return t(this.red,"redAdd works only with red numbers"),this.red.add(this,o)},e.prototype.redIAdd=function(o){return t(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,o)},e.prototype.redSub=function(o){return t(this.red,"redSub works only with red numbers"),this.red.sub(this,o)},e.prototype.redISub=function(o){return t(this.red,"redISub works only with red numbers"),this.red.isub(this,o)},e.prototype.redShl=function(o){return t(this.red,"redShl works only with red numbers"),this.red.shl(this,o)},e.prototype.redMul=function(o){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,o),this.red.mul(this,o)},e.prototype.redIMul=function(o){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,o),this.red.imul(this,o)},e.prototype.redSqr=function(){return t(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},e.prototype.redISqr=function(){return t(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},e.prototype.redSqrt=function(){return t(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},e.prototype.redInvm=function(){return t(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},e.prototype.redNeg=function(){return t(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},e.prototype.redPow=function(o){return t(this.red&&!o.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,o)};var Z={k256:null,p224:null,p192:null,p25519:null};function j(v,o){this.name=v,this.p=new e(o,16),this.n=this.p.bitLength(),this.k=new e(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}j.prototype._tmp=function(){var o=new e(null);return o.words=new Array(Math.ceil(this.n/13)),o},j.prototype.ireduce=function(o){var u=o,h;do this.split(u,this.tmp),u=this.imulK(u),u=u.iadd(this.tmp),h=u.bitLength();while(h>this.n);var m=h<this.n?-1:u.ucmp(this.p);return m===0?(u.words[0]=0,u.length=1):m>0?u.isub(this.p):u.strip!==void 0?u.strip():u._strip(),u},j.prototype.split=function(o,u){o.iushrn(this.n,0,u)},j.prototype.imulK=function(o){return o.imul(this.k)};function Y(){j.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}r(Y,j),Y.prototype.split=function(o,u){for(var h=4194303,m=Math.min(o.length,9),b=0;b<m;b++)u.words[b]=o.words[b];if(u.length=m,o.length<=9){o.words[0]=0,o.length=1;return}var M=o.words[9];for(u.words[u.length++]=M&h,b=10;b<o.length;b++){var A=o.words[b]|0;o.words[b-10]=(A&h)<<4|M>>>22,M=A}M>>>=22,o.words[b-10]=M,M===0&&o.length>10?o.length-=10:o.length-=9},Y.prototype.imulK=function(o){o.words[o.length]=0,o.words[o.length+1]=0,o.length+=2;for(var u=0,h=0;h<o.length;h++){var m=o.words[h]|0;u+=m*977,o.words[h]=u&67108863,u=m*64+(u/67108864|0)}return o.words[o.length-1]===0&&(o.length--,o.words[o.length-1]===0&&o.length--),o};function nt(){j.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}r(nt,j);function st(){j.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}r(st,j);function dt(){j.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}r(dt,j),dt.prototype.imulK=function(o){for(var u=0,h=0;h<o.length;h++){var m=(o.words[h]|0)*19+u,b=m&67108863;m>>>=26,o.words[h]=b,u=m}return u!==0&&(o.words[o.length++]=u),o},e._prime=function(o){if(Z[o])return Z[o];var u;if(o==="k256")u=new Y;else if(o==="p224")u=new nt;else if(o==="p192")u=new st;else if(o==="p25519")u=new dt;else throw new Error("Unknown prime "+o);return Z[o]=u,u};function X(v){if(typeof v=="string"){var o=e._prime(v);this.m=o.p,this.prime=o}else t(v.gtn(1),"modulus must be greater than 1"),this.m=v,this.prime=null}X.prototype._verify1=function(o){t(o.negative===0,"red works only with positives"),t(o.red,"red works only with red numbers")},X.prototype._verify2=function(o,u){t((o.negative|u.negative)===0,"red works only with positives"),t(o.red&&o.red===u.red,"red works only with red numbers")},X.prototype.imod=function(o){return this.prime?this.prime.ireduce(o)._forceRed(this):(c(o,o.umod(this.m)._forceRed(this)),o)},X.prototype.neg=function(o){return o.isZero()?o.clone():this.m.sub(o)._forceRed(this)},X.prototype.add=function(o,u){this._verify2(o,u);var h=o.add(u);return h.cmp(this.m)>=0&&h.isub(this.m),h._forceRed(this)},X.prototype.iadd=function(o,u){this._verify2(o,u);var h=o.iadd(u);return h.cmp(this.m)>=0&&h.isub(this.m),h},X.prototype.sub=function(o,u){this._verify2(o,u);var h=o.sub(u);return h.cmpn(0)<0&&h.iadd(this.m),h._forceRed(this)},X.prototype.isub=function(o,u){this._verify2(o,u);var h=o.isub(u);return h.cmpn(0)<0&&h.iadd(this.m),h},X.prototype.shl=function(o,u){return this._verify1(o),this.imod(o.ushln(u))},X.prototype.imul=function(o,u){return this._verify2(o,u),this.imod(o.imul(u))},X.prototype.mul=function(o,u){return this._verify2(o,u),this.imod(o.mul(u))},X.prototype.isqr=function(o){return this.imul(o,o.clone())},X.prototype.sqr=function(o){return this.mul(o,o)},X.prototype.sqrt=function(o){if(o.isZero())return o.clone();var u=this.m.andln(3);if(t(u%2===1),u===3){var h=this.m.add(new e(1)).iushrn(2);return this.pow(o,h)}for(var m=this.m.subn(1),b=0;!m.isZero()&&m.andln(1)===0;)b++,m.iushrn(1);t(!m.isZero());var M=new e(1).toRed(this),A=M.redNeg(),g=this.m.subn(1).iushrn(1),a=this.m.bitLength();for(a=new e(2*a*a).toRed(this);this.pow(a,g).cmp(A)!==0;)a.redIAdd(A);for(var w=this.pow(a,m),F=this.pow(o,m.addn(1).iushrn(1)),N=this.pow(o,m),U=b;N.cmp(M)!==0;){for(var V=N,tt=0;V.cmp(M)!==0;tt++)V=V.redSqr();t(tt<U);var L=this.pow(w,new e(1).iushln(U-tt-1));F=F.redMul(L),w=L.redSqr(),N=N.redMul(w),U=tt}return F},X.prototype.invm=function(o){var u=o._invmp(this.m);return u.negative!==0?(u.negative=0,this.imod(u).redNeg()):this.imod(u)},X.prototype.pow=function(o,u){if(u.isZero())return new e(1).toRed(this);if(u.cmpn(1)===0)return o.clone();var h=4,m=new Array(1<<h);m[0]=new e(1).toRed(this),m[1]=o;for(var b=2;b<m.length;b++)m[b]=this.mul(m[b-1],o);var M=m[0],A=0,g=0,a=u.bitLength()%26;for(a===0&&(a=26),b=u.length-1;b>=0;b--){for(var w=u.words[b],F=a-1;F>=0;F--){var N=w>>F&1;if(M!==m[0]&&(M=this.sqr(M)),N===0&&A===0){g=0;continue}A<<=1,A|=N,g++,!(g!==h&&(b!==0||F!==0))&&(M=this.mul(M,m[A]),g=0,A=0)}a=26}return M},X.prototype.convertTo=function(o){var u=o.umod(this.m);return u===o?u.clone():u},X.prototype.convertFrom=function(o){var u=o.clone();return u.red=null,u},e.mont=function(o){return new lt(o)};function lt(v){X.call(this,v),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new e(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}r(lt,X),lt.prototype.convertTo=function(o){return this.imod(o.ushln(this.shift))},lt.prototype.convertFrom=function(o){var u=this.imod(o.mul(this.rinv));return u.red=null,u},lt.prototype.imul=function(o,u){if(o.isZero()||u.isZero())return o.words[0]=0,o.length=1,o;var h=o.imul(u),m=h.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),b=h.isub(m).iushrn(this.shift),M=b;return b.cmp(this.m)>=0?M=b.isub(this.m):b.cmpn(0)<0&&(M=b.iadd(this.m)),M._forceRed(this)},lt.prototype.mul=function(o,u){if(o.isZero()||u.isZero())return new e(0)._forceRed(this);var h=o.mul(u),m=h.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),b=h.isub(m).iushrn(this.shift),M=b;return b.cmp(this.m)>=0?M=b.isub(this.m):b.cmpn(0)<0&&(M=b.iadd(this.m)),M._forceRed(this)},lt.prototype.invm=function(o){var u=this.imod(o._invmp(this.m).mul(this.r2));return u._forceRed(this)}})(typeof Bi>"u"||Bi,ur)});var Kn={};Lr(Kn,{BASIS_POINT:()=>te,BASIS_POINT_MAX:()=>ue,BIN_BOUND:()=>ge,BinUtils:()=>D,CetusDlmmSDK:()=>Re,DEFAULT_MAX_WEIGHT:()=>ve,DEFAULT_MIN_WEIGHT:()=>_e,FEE_PRECISION:()=>Ti,FeeUtils:()=>Oe,IlmUtils:()=>ji,MAX_BIN_ID:()=>Fn,MAX_BIN_PER_POSITION:()=>le,MAX_FEE_RATE:()=>Ke,MIN_BIN_ID:()=>Dn,ONE:()=>$e,PoolModule:()=>Ce,REWARD_PERIOD:()=>Nn,REWARD_PERIOD_START_AT:()=>qn,SCALE_OFFSET:()=>q,StrategyType:()=>Ie,StrategyUtils:()=>Pe,WeightUtils:()=>ee,buildPoolKey:()=>Li,default:()=>Qn,dlmmMainnet:()=>Ai,dlmmTestnet:()=>Zi,generateRewardSchedule:()=>Zn,getRouterModule:()=>qi,parseBinInfo:()=>Ve,parseBinInfoList:()=>ki,parseCurrentRewardPeriodEmission:()=>Hn,parseDlmmBasePool:()=>Ci,parseDlmmPool:()=>Ee,parseDlmmPosition:()=>Ge,parseLiquidityShares:()=>jn,parsePartner:()=>ze,parsePoolTransactionInfo:()=>Ni,parseRewardPeriodEmission:()=>Un,parseStrategyType:()=>Wn,parsedDlmmPosFeeData:()=>Ri,parsedDlmmPosRewardData:()=>Di,parsedSwapQuoteData:()=>Fi,poolFilterEvenTypes:()=>Ir,safeAmount:()=>Me,safeMulAmount:()=>ne});module.exports=jr(Kn);var Cr=require("@cetusprotocol/common-sdk");var Fe=require("@cetusprotocol/common-sdk"),Ai={env:"mainnet",full_rpc_url:Fe.FullRpcUrlMainnet,graph_rpc_url:Fe.GraphRpcUrlMainnet,dlmm_pool:{package_id:"0x5664f9d3fd82c84023870cfbda8ea84e14c8dd56ce557ad2116e0668581a682b",published_at:"0x44d0fb760f9f7ce532a4a97abddf2e7982c9b9b50ac94cf62de8f4d11356d306",version:5,config:{registry_id:"0xb1d55e7d895823c65f98d99b81a69436cf7d1638629c9ccb921326039cda1f1b",pools_id:"0xc3683b2356cac6423e9ecaea20955c7cc193998b016e5b884730ed1192174991",global_config_id:"0xf31b605d117f959b9730e8c07b08b856cb05143c5e81d5751c90d2979e82f599",versioned_id:"0x05370b2d656612dd5759cbe80463de301e3b94a921dfc72dd9daa2ecdeb2d0a8",admin_cap_id:"0xc4c42bc31cb54beb679dccd547f8bdb970cb6dc989bd1f85a4fed4812ed95d6e",partners_id:"0x5c0affc8d363b6abb1f32790c229165215f4edead89a9bc7cd95dad717b4296a"}},dlmm_router:{package_id:"0x8d389fa25cb08ebc5e520bc520ed394eed9e62b56b7868acb398bf298b8a76f3",published_at:"0xdb974d2654a62bb0668f58ee7919b5e41566a6c3336c3a006a412d5137487c67",version:3}};var xe=require("@mysten/sui/transactions"),it=require("@cetusprotocol/common-sdk");var lr=require("@cetusprotocol/common-sdk");var me=class extends lr.BaseError{constructor(i,t,r){super(i,t||"UnknownError",r)}static isDlmmErrorCode(i,t){return this.isErrorCode(i,t)}},J=(n,i,t)=>{let r={...t,stack:i instanceof Error?i.stack:void 0};throw i instanceof Error?new me(i.message,n,r):new me(i,n,r)};var G=require("@cetusprotocol/common-sdk"),Br=Pi(Ne());var be=9e15,he=1e9,Si="0123456789abcdef",je="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",We="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Ii={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-be,maxE:be,crypto:!1},hr,ae,z=!0,Ue="[DecimalError] ",ce=Ue+"Invalid argument: ",pr=Ue+"Precision limit exceeded",mr=Ue+"crypto unavailable",_r="[object Decimal]",Vt=Math.floor,$t=Math.pow,Wr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Zr=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Ur=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,gr=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,re=1e7,$=7,Hr=9007199254740991,Qr=je.length-1,Ei=We.length-1,I={toStringTag:_r};I.absoluteValue=I.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),H(n)};I.ceil=function(){return H(new this.constructor(this),this.e+1,2)};I.clampedTo=I.clamp=function(n,i){var t,r=this,e=r.constructor;if(n=new e(n),i=new e(i),!n.s||!i.s)return new e(NaN);if(n.gt(i))throw Error(ce+i);return t=r.cmp(n),t<0?n:r.cmp(i)>0?i:new e(r)};I.comparedTo=I.cmp=function(n){var i,t,r,e,s=this,l=s.d,d=(n=new s.constructor(n)).d,f=s.s,c=n.s;if(!l||!d)return!f||!c?NaN:f!==c?f:l===d?0:!l^f<0?1:-1;if(!l[0]||!d[0])return l[0]?f:d[0]?-c:0;if(f!==c)return f;if(s.e!==n.e)return s.e>n.e^f<0?1:-1;for(r=l.length,e=d.length,i=0,t=r<e?r:e;i<t;++i)if(l[i]!==d[i])return l[i]>d[i]^f<0?1:-1;return r===e?0:r>e^f<0?1:-1};I.cosine=I.cos=function(){var n,i,t=this,r=t.constructor;return t.d?t.d[0]?(n=r.precision,i=r.rounding,r.precision=n+Math.max(t.e,t.sd())+$,r.rounding=1,t=Kr(r,Mr(r,t)),r.precision=n,r.rounding=i,H(ae==2||ae==3?t.neg():t,n,i,!0)):new r(1):new r(NaN)};I.cubeRoot=I.cbrt=function(){var n,i,t,r,e,s,l,d,f,c,p=this,_=p.constructor;if(!p.isFinite()||p.isZero())return new _(p);for(z=!1,s=p.s*$t(p.s*p,1/3),!s||Math.abs(s)==1/0?(t=zt(p.d),n=p.e,(s=(n-t.length+1)%3)&&(t+=s==1||s==-2?"0":"00"),s=$t(t,1/3),n=Vt((n+1)/3)-(n%3==(n<0?-1:2)),s==1/0?t="5e"+n:(t=s.toExponential(),t=t.slice(0,t.indexOf("e")+1)+n),r=new _(t),r.s=p.s):r=new _(s.toString()),l=(n=_.precision)+3;;)if(d=r,f=d.times(d).times(d),c=f.plus(p),r=ut(c.plus(p).times(d),c.plus(f),l+2,1),zt(d.d).slice(0,l)===(t=zt(r.d)).slice(0,l))if(t=t.slice(l-3,l+1),t=="9999"||!e&&t=="4999"){if(!e&&(H(d,n+1,0),d.times(d).times(d).eq(p))){r=d;break}l+=4,e=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(H(r,n+1,1),i=!r.times(r).times(r).eq(p));break}return z=!0,H(r,n,_.rounding,i)};I.decimalPlaces=I.dp=function(){var n,i=this.d,t=NaN;if(i){if(n=i.length-1,t=(n-Vt(this.e/$))*$,n=i[n],n)for(;n%10==0;n/=10)t--;t<0&&(t=0)}return t};I.dividedBy=I.div=function(n){return ut(this,new this.constructor(n))};I.dividedToIntegerBy=I.divToInt=function(n){var i=this,t=i.constructor;return H(ut(i,new t(n),0,1,1),t.precision,t.rounding)};I.equals=I.eq=function(n){return this.cmp(n)===0};I.floor=function(){return H(new this.constructor(this),this.e+1,3)};I.greaterThan=I.gt=function(n){return this.cmp(n)>0};I.greaterThanOrEqualTo=I.gte=function(n){var i=this.cmp(n);return i==1||i===0};I.hyperbolicCosine=I.cosh=function(){var n,i,t,r,e,s=this,l=s.constructor,d=new l(1);if(!s.isFinite())return new l(s.s?1/0:NaN);if(s.isZero())return d;t=l.precision,r=l.rounding,l.precision=t+Math.max(s.e,s.sd())+4,l.rounding=1,e=s.d.length,e<32?(n=Math.ceil(e/3),i=(1/Qe(4,n)).toString()):(n=16,i="2.3283064365386962890625e-10"),s=we(l,1,s.times(i),new l(1),!0);for(var f,c=n,p=new l(8);c--;)f=s.times(s),s=d.minus(f.times(p.minus(f.times(p))));return H(s,l.precision=t,l.rounding=r,!0)};I.hyperbolicSine=I.sinh=function(){var n,i,t,r,e=this,s=e.constructor;if(!e.isFinite()||e.isZero())return new s(e);if(i=s.precision,t=s.rounding,s.precision=i+Math.max(e.e,e.sd())+4,s.rounding=1,r=e.d.length,r<3)e=we(s,2,e,e,!0);else{n=1.4*Math.sqrt(r),n=n>16?16:n|0,e=e.times(1/Qe(5,n)),e=we(s,2,e,e,!0);for(var l,d=new s(5),f=new s(16),c=new s(20);n--;)l=e.times(e),e=e.times(d.plus(l.times(f.times(l).plus(c))))}return s.precision=i,s.rounding=t,H(e,i,t,!0)};I.hyperbolicTangent=I.tanh=function(){var n,i,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,i=r.rounding,r.precision=n+7,r.rounding=1,ut(t.sinh(),t.cosh(),r.precision=n,r.rounding=i)):new r(t.s)};I.inverseCosine=I.acos=function(){var n=this,i=n.constructor,t=n.abs().cmp(1),r=i.precision,e=i.rounding;return t!==-1?t===0?n.isNeg()?oe(i,r,e):new i(0):new i(NaN):n.isZero()?oe(i,r+4,e).times(.5):(i.precision=r+6,i.rounding=1,n=new i(1).minus(n).div(n.plus(1)).sqrt().atan(),i.precision=r,i.rounding=e,n.times(2))};I.inverseHyperbolicCosine=I.acosh=function(){var n,i,t=this,r=t.constructor;return t.lte(1)?new r(t.eq(1)?0:NaN):t.isFinite()?(n=r.precision,i=r.rounding,r.precision=n+Math.max(Math.abs(t.e),t.sd())+4,r.rounding=1,z=!1,t=t.times(t).minus(1).sqrt().plus(t),z=!0,r.precision=n,r.rounding=i,t.ln()):new r(t)};I.inverseHyperbolicSine=I.asinh=function(){var n,i,t=this,r=t.constructor;return!t.isFinite()||t.isZero()?new r(t):(n=r.precision,i=r.rounding,r.precision=n+2*Math.max(Math.abs(t.e),t.sd())+6,r.rounding=1,z=!1,t=t.times(t).plus(1).sqrt().plus(t),z=!0,r.precision=n,r.rounding=i,t.ln())};I.inverseHyperbolicTangent=I.atanh=function(){var n,i,t,r,e=this,s=e.constructor;return e.isFinite()?e.e>=0?new s(e.abs().eq(1)?e.s/0:e.isZero()?e:NaN):(n=s.precision,i=s.rounding,r=e.sd(),Math.max(r,n)<2*-e.e-1?H(new s(e),n,i,!0):(s.precision=t=r-e.e,e=ut(e.plus(1),new s(1).minus(e),t+n,1),s.precision=n+4,s.rounding=1,e=e.ln(),s.precision=n,s.rounding=i,e.times(.5))):new s(NaN)};I.inverseSine=I.asin=function(){var n,i,t,r,e=this,s=e.constructor;return e.isZero()?new s(e):(i=e.abs().cmp(1),t=s.precision,r=s.rounding,i!==-1?i===0?(n=oe(s,t+4,r).times(.5),n.s=e.s,n):new s(NaN):(s.precision=t+6,s.rounding=1,e=e.div(new s(1).minus(e.times(e)).sqrt().plus(1)).atan(),s.precision=t,s.rounding=r,e.times(2)))};I.inverseTangent=I.atan=function(){var n,i,t,r,e,s,l,d,f,c=this,p=c.constructor,_=p.precision,y=p.rounding;if(c.isFinite()){if(c.isZero())return new p(c);if(c.abs().eq(1)&&_+4<=Ei)return l=oe(p,_+4,y).times(.25),l.s=c.s,l}else{if(!c.s)return new p(NaN);if(_+4<=Ei)return l=oe(p,_+4,y).times(.5),l.s=c.s,l}for(p.precision=d=_+10,p.rounding=1,t=Math.min(28,d/$+2|0),n=t;n;--n)c=c.div(c.times(c).plus(1).sqrt().plus(1));for(z=!1,i=Math.ceil(d/$),r=1,f=c.times(c),l=new p(c),e=c;n!==-1;)if(e=e.times(f),s=l.minus(e.div(r+=2)),e=e.times(f),l=s.plus(e.div(r+=2)),l.d[i]!==void 0)for(n=i;l.d[n]===s.d[n]&&n--;);return t&&(l=l.times(2<<t-1)),z=!0,H(l,p.precision=_,p.rounding=y,!0)};I.isFinite=function(){return!!this.d};I.isInteger=I.isInt=function(){return!!this.d&&Vt(this.e/$)>this.d.length-2};I.isNaN=function(){return!this.s};I.isNegative=I.isNeg=function(){return this.s<0};I.isPositive=I.isPos=function(){return this.s>0};I.isZero=function(){return!!this.d&&this.d[0]===0};I.lessThan=I.lt=function(n){return this.cmp(n)<0};I.lessThanOrEqualTo=I.lte=function(n){return this.cmp(n)<1};I.logarithm=I.log=function(n){var i,t,r,e,s,l,d,f,c=this,p=c.constructor,_=p.precision,y=p.rounding,P=5;if(n==null)n=new p(10),i=!0;else{if(n=new p(n),t=n.d,n.s<0||!t||!t[0]||n.eq(1))return new p(NaN);i=n.eq(10)}if(t=c.d,c.s<0||!t||!t[0]||c.eq(1))return new p(t&&!t[0]?-1/0:c.s!=1?NaN:t?0:1/0);if(i)if(t.length>1)s=!0;else{for(e=t[0];e%10===0;)e/=10;s=e!==1}if(z=!1,d=_+P,l=fe(c,d),r=i?Ze(p,d+10):fe(n,d),f=ut(l,r,d,1),Be(f.d,e=_,y))do if(d+=10,l=fe(c,d),r=i?Ze(p,d+10):fe(n,d),f=ut(l,r,d,1),!s){+zt(f.d).slice(e+1,e+15)+1==1e14&&(f=H(f,_+1,0));break}while(Be(f.d,e+=10,y));return z=!0,H(f,_,y)};I.minus=I.sub=function(n){var i,t,r,e,s,l,d,f,c,p,_,y,P=this,B=P.constructor;if(n=new B(n),!P.d||!n.d)return!P.s||!n.s?n=new B(NaN):P.d?n.s=-n.s:n=new B(n.d||P.s!==n.s?P:NaN),n;if(P.s!=n.s)return n.s=-n.s,P.plus(n);if(c=P.d,y=n.d,d=B.precision,f=B.rounding,!c[0]||!y[0]){if(y[0])n.s=-n.s;else if(c[0])n=new B(P);else return new B(f===3?-0:0);return z?H(n,d,f):n}if(t=Vt(n.e/$),p=Vt(P.e/$),c=c.slice(),s=p-t,s){for(_=s<0,_?(i=c,s=-s,l=y.length):(i=y,t=p,l=c.length),r=Math.max(Math.ceil(d/$),l)+2,s>r&&(s=r,i.length=1),i.reverse(),r=s;r--;)i.push(0);i.reverse()}else{for(r=c.length,l=y.length,_=r<l,_&&(l=r),r=0;r<l;r++)if(c[r]!=y[r]){_=c[r]<y[r];break}s=0}for(_&&(i=c,c=y,y=i,n.s=-n.s),l=c.length,r=y.length-l;r>0;--r)c[l++]=0;for(r=y.length;r>s;){if(c[--r]<y[r]){for(e=r;e&&c[--e]===0;)c[e]=re-1;--c[e],c[r]+=re}c[r]-=y[r]}for(;c[--l]===0;)c.pop();for(;c[0]===0;c.shift())--t;return c[0]?(n.d=c,n.e=He(c,t),z?H(n,d,f):n):new B(f===3?-0:0)};I.modulo=I.mod=function(n){var i,t=this,r=t.constructor;return n=new r(n),!t.d||!n.s||n.d&&!n.d[0]?new r(NaN):!n.d||t.d&&!t.d[0]?H(new r(t),r.precision,r.rounding):(z=!1,r.modulo==9?(i=ut(t,n.abs(),0,3,1),i.s*=n.s):i=ut(t,n,0,r.modulo,1),i=i.times(n),z=!0,t.minus(i))};I.naturalExponential=I.exp=function(){return Oi(this)};I.naturalLogarithm=I.ln=function(){return fe(this)};I.negated=I.neg=function(){var n=new this.constructor(this);return n.s=-n.s,H(n)};I.plus=I.add=function(n){var i,t,r,e,s,l,d,f,c,p,_=this,y=_.constructor;if(n=new y(n),!_.d||!n.d)return!_.s||!n.s?n=new y(NaN):_.d||(n=new y(n.d||_.s===n.s?_:NaN)),n;if(_.s!=n.s)return n.s=-n.s,_.minus(n);if(c=_.d,p=n.d,d=y.precision,f=y.rounding,!c[0]||!p[0])return p[0]||(n=new y(_)),z?H(n,d,f):n;if(s=Vt(_.e/$),r=Vt(n.e/$),c=c.slice(),e=s-r,e){for(e<0?(t=c,e=-e,l=p.length):(t=p,r=s,l=c.length),s=Math.ceil(d/$),l=s>l?s+1:l+1,e>l&&(e=l,t.length=1),t.reverse();e--;)t.push(0);t.reverse()}for(l=c.length,e=p.length,l-e<0&&(e=l,t=p,p=c,c=t),i=0;e;)i=(c[--e]=c[e]+p[e]+i)/re|0,c[e]%=re;for(i&&(c.unshift(i),++r),l=c.length;c[--l]==0;)c.pop();return n.d=c,n.e=He(c,r),z?H(n,d,f):n};I.precision=I.sd=function(n){var i,t=this;if(n!==void 0&&n!==!!n&&n!==1&&n!==0)throw Error(ce+n);return t.d?(i=br(t.d),n&&t.e+1>i&&(i=t.e+1)):i=NaN,i};I.round=function(){var n=this,i=n.constructor;return H(new i(n),n.e+1,i.rounding)};I.sine=I.sin=function(){var n,i,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,i=r.rounding,r.precision=n+Math.max(t.e,t.sd())+$,r.rounding=1,t=zr(r,Mr(r,t)),r.precision=n,r.rounding=i,H(ae>2?t.neg():t,n,i,!0)):new r(NaN)};I.squareRoot=I.sqrt=function(){var n,i,t,r,e,s,l=this,d=l.d,f=l.e,c=l.s,p=l.constructor;if(c!==1||!d||!d[0])return new p(!c||c<0&&(!d||d[0])?NaN:d?l:1/0);for(z=!1,c=Math.sqrt(+l),c==0||c==1/0?(i=zt(d),(i.length+f)%2==0&&(i+="0"),c=Math.sqrt(i),f=Vt((f+1)/2)-(f<0||f%2),c==1/0?i="5e"+f:(i=c.toExponential(),i=i.slice(0,i.indexOf("e")+1)+f),r=new p(i)):r=new p(c.toString()),t=(f=p.precision)+3;;)if(s=r,r=s.plus(ut(l,s,t+2,1)).times(.5),zt(s.d).slice(0,t)===(i=zt(r.d)).slice(0,t))if(i=i.slice(t-3,t+1),i=="9999"||!e&&i=="4999"){if(!e&&(H(s,f+1,0),s.times(s).eq(l))){r=s;break}t+=4,e=1}else{(!+i||!+i.slice(1)&&i.charAt(0)=="5")&&(H(r,f+1,1),n=!r.times(r).eq(l));break}return z=!0,H(r,f,p.rounding,n)};I.tangent=I.tan=function(){var n,i,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,i=r.rounding,r.precision=n+10,r.rounding=1,t=t.sin(),t.s=1,t=ut(t,new r(1).minus(t.times(t)).sqrt(),n+10,0),r.precision=n,r.rounding=i,H(ae==2||ae==4?t.neg():t,n,i,!0)):new r(NaN)};I.times=I.mul=function(n){var i,t,r,e,s,l,d,f,c,p=this,_=p.constructor,y=p.d,P=(n=new _(n)).d;if(n.s*=p.s,!y||!y[0]||!P||!P[0])return new _(!n.s||y&&!y[0]&&!P||P&&!P[0]&&!y?NaN:!y||!P?n.s/0:n.s*0);for(t=Vt(p.e/$)+Vt(n.e/$),f=y.length,c=P.length,f<c&&(s=y,y=P,P=s,l=f,f=c,c=l),s=[],l=f+c,r=l;r--;)s.push(0);for(r=c;--r>=0;){for(i=0,e=f+r;e>r;)d=s[e]+P[r]*y[e-r-1]+i,s[e--]=d%re|0,i=d/re|0;s[e]=(s[e]+i)%re|0}for(;!s[--l];)s.pop();return i?++t:s.shift(),n.d=s,n.e=He(s,t),z?H(n,_.precision,_.rounding):n};I.toBinary=function(n,i){return xi(this,2,n,i)};I.toDecimalPlaces=I.toDP=function(n,i){var t=this,r=t.constructor;return t=new r(t),n===void 0?t:(Jt(n,0,he),i===void 0?i=r.rounding:Jt(i,0,8),H(t,n+t.e+1,i))};I.toExponential=function(n,i){var t,r=this,e=r.constructor;return n===void 0?t=se(r,!0):(Jt(n,0,he),i===void 0?i=e.rounding:Jt(i,0,8),r=H(new e(r),n+1,i),t=se(r,!0,n+1)),r.isNeg()&&!r.isZero()?"-"+t:t};I.toFixed=function(n,i){var t,r,e=this,s=e.constructor;return n===void 0?t=se(e):(Jt(n,0,he),i===void 0?i=s.rounding:Jt(i,0,8),r=H(new s(e),n+e.e+1,i),t=se(r,!1,n+r.e+1)),e.isNeg()&&!e.isZero()?"-"+t:t};I.toFraction=function(n){var i,t,r,e,s,l,d,f,c,p,_,y,P=this,B=P.d,S=P.constructor;if(!B)return new S(P);if(c=t=new S(1),r=f=new S(0),i=new S(r),s=i.e=br(B)-P.e-1,l=s%$,i.d[0]=$t(10,l<0?$+l:l),n==null)n=s>0?i:c;else{if(d=new S(n),!d.isInt()||d.lt(c))throw Error(ce+d);n=d.gt(i)?s>0?i:c:d}for(z=!1,d=new S(zt(B)),p=S.precision,S.precision=s=B.length*$*2;_=ut(d,i,0,1,1),e=t.plus(_.times(r)),e.cmp(n)!=1;)t=r,r=e,e=c,c=f.plus(_.times(e)),f=e,e=i,i=d.minus(_.times(e)),d=e;return e=ut(n.minus(t),r,0,1,1),f=f.plus(e.times(c)),t=t.plus(e.times(r)),f.s=c.s=P.s,y=ut(c,r,s,1).minus(P).abs().cmp(ut(f,t,s,1).minus(P).abs())<1?[c,r]:[f,t],S.precision=p,z=!0,y};I.toHexadecimal=I.toHex=function(n,i){return xi(this,16,n,i)};I.toNearest=function(n,i){var t=this,r=t.constructor;if(t=new r(t),n==null){if(!t.d)return t;n=new r(1),i=r.rounding}else{if(n=new r(n),i===void 0?i=r.rounding:Jt(i,0,8),!t.d)return n.s?t:n;if(!n.d)return n.s&&(n.s=t.s),n}return n.d[0]?(z=!1,t=ut(t,n,0,i,1).times(n),z=!0,H(t)):(n.s=t.s,t=n),t};I.toNumber=function(){return+this};I.toOctal=function(n,i){return xi(this,8,n,i)};I.toPower=I.pow=function(n){var i,t,r,e,s,l,d=this,f=d.constructor,c=+(n=new f(n));if(!d.d||!n.d||!d.d[0]||!n.d[0])return new f($t(+d,c));if(d=new f(d),d.eq(1))return d;if(r=f.precision,s=f.rounding,n.eq(1))return H(d,r,s);if(i=Vt(n.e/$),i>=n.d.length-1&&(t=c<0?-c:c)<=Hr)return e=wr(f,d,t,r),n.s<0?new f(1).div(e):H(e,r,s);if(l=d.s,l<0){if(i<n.d.length-1)return new f(NaN);if((n.d[i]&1)==0&&(l=1),d.e==0&&d.d[0]==1&&d.d.length==1)return d.s=l,d}return t=$t(+d,c),i=t==0||!isFinite(t)?Vt(c*(Math.log("0."+zt(d.d))/Math.LN10+d.e+1)):new f(t+"").e,i>f.maxE+1||i<f.minE-1?new f(i>0?l/0:0):(z=!1,f.rounding=d.s=1,t=Math.min(12,(i+"").length),e=Oi(n.times(fe(d,r+t)),r),e.d&&(e=H(e,r+5,1),Be(e.d,r,s)&&(i=r+10,e=H(Oi(n.times(fe(d,i+t)),i),i+5,1),+zt(e.d).slice(r+1,r+15)+1==1e14&&(e=H(e,r+1,0)))),e.s=l,z=!0,f.rounding=s,H(e,r,s))};I.toPrecision=function(n,i){var t,r=this,e=r.constructor;return n===void 0?t=se(r,r.e<=e.toExpNeg||r.e>=e.toExpPos):(Jt(n,1,he),i===void 0?i=e.rounding:Jt(i,0,8),r=H(new e(r),n,i),t=se(r,n<=r.e||r.e<=e.toExpNeg,n)),r.isNeg()&&!r.isZero()?"-"+t:t};I.toSignificantDigits=I.toSD=function(n,i){var t=this,r=t.constructor;return n===void 0?(n=r.precision,i=r.rounding):(Jt(n,1,he),i===void 0?i=r.rounding:Jt(i,0,8)),H(new r(t),n,i)};I.toString=function(){var n=this,i=n.constructor,t=se(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()&&!n.isZero()?"-"+t:t};I.truncated=I.trunc=function(){return H(new this.constructor(this),this.e+1,1)};I.valueOf=I.toJSON=function(){var n=this,i=n.constructor,t=se(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()?"-"+t:t};function zt(n){var i,t,r,e=n.length-1,s="",l=n[0];if(e>0){for(s+=l,i=1;i<e;i++)r=n[i]+"",t=$-r.length,t&&(s+=de(t)),s+=r;l=n[i],r=l+"",t=$-r.length,t&&(s+=de(t))}else if(l===0)return"0";for(;l%10===0;)l/=10;return s+l}function Jt(n,i,t){if(n!==~~n||n<i||n>t)throw Error(ce+n)}function Be(n,i,t,r){var e,s,l,d;for(s=n[0];s>=10;s/=10)--i;return--i<0?(i+=$,e=0):(e=Math.ceil((i+1)/$),i%=$),s=$t(10,$-i),d=n[e]%s|0,r==null?i<3?(i==0?d=d/100|0:i==1&&(d=d/10|0),l=t<4&&d==99999||t>3&&d==49999||d==5e4||d==0):l=(t<4&&d+1==s||t>3&&d+1==s/2)&&(n[e+1]/s/100|0)==$t(10,i-2)-1||(d==s/2||d==0)&&(n[e+1]/s/100|0)==0:i<4?(i==0?d=d/1e3|0:i==1?d=d/100|0:i==2&&(d=d/10|0),l=(r||t<4)&&d==9999||!r&&t>3&&d==4999):l=((r||t<4)&&d+1==s||!r&&t>3&&d+1==s/2)&&(n[e+1]/s/1e3|0)==$t(10,i-3)-1,l}function qe(n,i,t){for(var r,e=[0],s,l=0,d=n.length;l<d;){for(s=e.length;s--;)e[s]*=i;for(e[0]+=Si.indexOf(n.charAt(l++)),r=0;r<e.length;r++)e[r]>t-1&&(e[r+1]===void 0&&(e[r+1]=0),e[r+1]+=e[r]/t|0,e[r]%=t)}return e.reverse()}function Kr(n,i){var t,r,e;if(i.isZero())return i;r=i.d.length,r<32?(t=Math.ceil(r/3),e=(1/Qe(4,t)).toString()):(t=16,e="2.3283064365386962890625e-10"),n.precision+=t,i=we(n,1,i.times(e),new n(1));for(var s=t;s--;){var l=i.times(i);i=l.times(l).minus(l).times(8).plus(1)}return n.precision-=t,i}var ut=(function(){function n(r,e,s){var l,d=0,f=r.length;for(r=r.slice();f--;)l=r[f]*e+d,r[f]=l%s|0,d=l/s|0;return d&&r.unshift(d),r}function i(r,e,s,l){var d,f;if(s!=l)f=s>l?1:-1;else for(d=f=0;d<s;d++)if(r[d]!=e[d]){f=r[d]>e[d]?1:-1;break}return f}function t(r,e,s,l){for(var d=0;s--;)r[s]-=d,d=r[s]<e[s]?1:0,r[s]=d*l+r[s]-e[s];for(;!r[0]&&r.length>1;)r.shift()}return function(r,e,s,l,d,f){var c,p,_,y,P,B,S,C,k,R,x,T,Z,j,Y,nt,st,dt,X,lt,v=r.constructor,o=r.s==e.s?1:-1,u=r.d,h=e.d;if(!u||!u[0]||!h||!h[0])return new v(!r.s||!e.s||(u?h&&u[0]==h[0]:!h)?NaN:u&&u[0]==0||!h?o*0:o/0);for(f?(P=1,p=r.e-e.e):(f=re,P=$,p=Vt(r.e/P)-Vt(e.e/P)),X=h.length,st=u.length,k=new v(o),R=k.d=[],_=0;h[_]==(u[_]||0);_++);if(h[_]>(u[_]||0)&&p--,s==null?(j=s=v.precision,l=v.rounding):d?j=s+(r.e-e.e)+1:j=s,j<0)R.push(1),B=!0;else{if(j=j/P+2|0,_=0,X==1){for(y=0,h=h[0],j++;(_<st||y)&&j--;_++)Y=y*f+(u[_]||0),R[_]=Y/h|0,y=Y%h|0;B=y||_<st}else{for(y=f/(h[0]+1)|0,y>1&&(h=n(h,y,f),u=n(u,y,f),X=h.length,st=u.length),nt=X,x=u.slice(0,X),T=x.length;T<X;)x[T++]=0;lt=h.slice(),lt.unshift(0),dt=h[0],h[1]>=f/2&&++dt;do y=0,c=i(h,x,X,T),c<0?(Z=x[0],X!=T&&(Z=Z*f+(x[1]||0)),y=Z/dt|0,y>1?(y>=f&&(y=f-1),S=n(h,y,f),C=S.length,T=x.length,c=i(S,x,C,T),c==1&&(y--,t(S,X<C?lt:h,C,f))):(y==0&&(c=y=1),S=h.slice()),C=S.length,C<T&&S.unshift(0),t(x,S,T,f),c==-1&&(T=x.length,c=i(h,x,X,T),c<1&&(y++,t(x,X<T?lt:h,T,f))),T=x.length):c===0&&(y++,x=[0]),R[_++]=y,c&&x[0]?x[T++]=u[nt]||0:(x=[u[nt]],T=1);while((nt++<st||x[0]!==void 0)&&j--);B=x[0]!==void 0}R[0]||R.shift()}if(P==1)k.e=p,hr=B;else{for(_=1,y=R[0];y>=10;y/=10)_++;k.e=_+p*P-1,H(k,d?s+k.e+1:s,l,B)}return k}})();function H(n,i,t,r){var e,s,l,d,f,c,p,_,y,P=n.constructor;t:if(i!=null){if(_=n.d,!_)return n;for(e=1,d=_[0];d>=10;d/=10)e++;if(s=i-e,s<0)s+=$,l=i,p=_[y=0],f=p/$t(10,e-l-1)%10|0;else if(y=Math.ceil((s+1)/$),d=_.length,y>=d)if(r){for(;d++<=y;)_.push(0);p=f=0,e=1,s%=$,l=s-$+1}else break t;else{for(p=d=_[y],e=1;d>=10;d/=10)e++;s%=$,l=s-$+e,f=l<0?0:p/$t(10,e-l-1)%10|0}if(r=r||i<0||_[y+1]!==void 0||(l<0?p:p%$t(10,e-l-1)),c=t<4?(f||r)&&(t==0||t==(n.s<0?3:2)):f>5||f==5&&(t==4||r||t==6&&(s>0?l>0?p/$t(10,e-l):0:_[y-1])%10&1||t==(n.s<0?8:7)),i<1||!_[0])return _.length=0,c?(i-=n.e+1,_[0]=$t(10,($-i%$)%$),n.e=-i||0):_[0]=n.e=0,n;if(s==0?(_.length=y,d=1,y--):(_.length=y+1,d=$t(10,$-s),_[y]=l>0?(p/$t(10,e-l)%$t(10,l)|0)*d:0),c)for(;;)if(y==0){for(s=1,l=_[0];l>=10;l/=10)s++;for(l=_[0]+=d,d=1;l>=10;l/=10)d++;s!=d&&(n.e++,_[0]==re&&(_[0]=1));break}else{if(_[y]+=d,_[y]!=re)break;_[y--]=0,d=1}for(s=_.length;_[--s]===0;)_.pop()}return z&&(n.e>P.maxE?(n.d=null,n.e=NaN):n.e<P.minE&&(n.e=0,n.d=[0])),n}function se(n,i,t){if(!n.isFinite())return yr(n);var r,e=n.e,s=zt(n.d),l=s.length;return i?(t&&(r=t-l)>0?s=s.charAt(0)+"."+s.slice(1)+de(r):l>1&&(s=s.charAt(0)+"."+s.slice(1)),s=s+(n.e<0?"e":"e+")+n.e):e<0?(s="0."+de(-e-1)+s,t&&(r=t-l)>0&&(s+=de(r))):e>=l?(s+=de(e+1-l),t&&(r=t-e-1)>0&&(s=s+"."+de(r))):((r=e+1)<l&&(s=s.slice(0,r)+"."+s.slice(r)),t&&(r=t-l)>0&&(e+1===l&&(s+="."),s+=de(r))),s}function He(n,i){var t=n[0];for(i*=$;t>=10;t/=10)i++;return i}function Ze(n,i,t){if(i>Qr)throw z=!0,t&&(n.precision=t),Error(pr);return H(new n(je),i,1,!0)}function oe(n,i,t){if(i>Ei)throw Error(pr);return H(new n(We),i,t,!0)}function br(n){var i=n.length-1,t=i*$+1;if(i=n[i],i){for(;i%10==0;i/=10)t--;for(i=n[0];i>=10;i/=10)t++}return t}function de(n){for(var i="";n--;)i+="0";return i}function wr(n,i,t,r){var e,s=new n(1),l=Math.ceil(r/$+4);for(z=!1;;){if(t%2&&(s=s.times(i),fr(s.d,l)&&(e=!0)),t=Vt(t/2),t===0){t=s.d.length-1,e&&s.d[t]===0&&++s.d[t];break}i=i.times(i),fr(i.d,l)}return z=!0,s}function dr(n){return n.d[n.d.length-1]&1}function vr(n,i,t){for(var r,e,s=new n(i[0]),l=0;++l<i.length;){if(e=new n(i[l]),!e.s){s=e;break}r=s.cmp(e),(r===t||r===0&&s.s===t)&&(s=e)}return s}function Oi(n,i){var t,r,e,s,l,d,f,c=0,p=0,_=0,y=n.constructor,P=y.rounding,B=y.precision;if(!n.d||!n.d[0]||n.e>17)return new y(n.d?n.d[0]?n.s<0?0:1/0:1:n.s?n.s<0?0:n:NaN);for(i==null?(z=!1,f=B):f=i,d=new y(.03125);n.e>-2;)n=n.times(d),_+=5;for(r=Math.log($t(2,_))/Math.LN10*2+5|0,f+=r,t=s=l=new y(1),y.precision=f;;){if(s=H(s.times(n),f,1),t=t.times(++p),d=l.plus(ut(s,t,f,1)),zt(d.d).slice(0,f)===zt(l.d).slice(0,f)){for(e=_;e--;)l=H(l.times(l),f,1);if(i==null)if(c<3&&Be(l.d,f-r,P,c))y.precision=f+=10,t=s=d=new y(1),p=0,c++;else return H(l,y.precision=B,P,z=!0);else return y.precision=B,l}l=d}}function fe(n,i){var t,r,e,s,l,d,f,c,p,_,y,P=1,B=10,S=n,C=S.d,k=S.constructor,R=k.rounding,x=k.precision;if(S.s<0||!C||!C[0]||!S.e&&C[0]==1&&C.length==1)return new k(C&&!C[0]?-1/0:S.s!=1?NaN:C?0:S);if(i==null?(z=!1,p=x):p=i,k.precision=p+=B,t=zt(C),r=t.charAt(0),Math.abs(s=S.e)<15e14){for(;r<7&&r!=1||r==1&&t.charAt(1)>3;)S=S.times(n),t=zt(S.d),r=t.charAt(0),P++;s=S.e,r>1?(S=new k("0."+t),s++):S=new k(r+"."+t.slice(1))}else return c=Ze(k,p+2,x).times(s+""),S=fe(new k(r+"."+t.slice(1)),p-B).plus(c),k.precision=x,i==null?H(S,x,R,z=!0):S;for(_=S,f=l=S=ut(S.minus(1),S.plus(1),p,1),y=H(S.times(S),p,1),e=3;;){if(l=H(l.times(y),p,1),c=f.plus(ut(l,new k(e),p,1)),zt(c.d).slice(0,p)===zt(f.d).slice(0,p))if(f=f.times(2),s!==0&&(f=f.plus(Ze(k,p+2,x).times(s+""))),f=ut(f,new k(P),p,1),i==null)if(Be(f.d,p-B,R,d))k.precision=p+=B,c=l=S=ut(_.minus(1),_.plus(1),p,1),y=H(S.times(S),p,1),e=d=1;else return H(f,k.precision=x,R,z=!0);else return k.precision=x,f;f=c,e+=2}}function yr(n){return String(n.s*n.s/0)}function Le(n,i){var t,r,e;for((t=i.indexOf("."))>-1&&(i=i.replace(".","")),(r=i.search(/e/i))>0?(t<0&&(t=r),t+=+i.slice(r+1),i=i.substring(0,r)):t<0&&(t=i.length),r=0;i.charCodeAt(r)===48;r++);for(e=i.length;i.charCodeAt(e-1)===48;--e);if(i=i.slice(r,e),i){if(e-=r,n.e=t=t-r-1,n.d=[],r=(t+1)%$,t<0&&(r+=$),r<e){for(r&&n.d.push(+i.slice(0,r)),e-=$;r<e;)n.d.push(+i.slice(r,r+=$));i=i.slice(r),r=$-i.length}else r-=e;for(;r--;)i+="0";n.d.push(+i),z&&(n.e>n.constructor.maxE?(n.d=null,n.e=NaN):n.e<n.constructor.minE&&(n.e=0,n.d=[0]))}else n.e=0,n.d=[0];return n}function $r(n,i){var t,r,e,s,l,d,f,c,p;if(i.indexOf("_")>-1){if(i=i.replace(/(\d)_(?=\d)/g,"$1"),gr.test(i))return Le(n,i)}else if(i==="Infinity"||i==="NaN")return+i||(n.s=NaN),n.e=NaN,n.d=null,n;if(Zr.test(i))t=16,i=i.toLowerCase();else if(Wr.test(i))t=2;else if(Ur.test(i))t=8;else throw Error(ce+i);for(s=i.search(/p/i),s>0?(f=+i.slice(s+1),i=i.substring(2,s)):i=i.slice(2),s=i.indexOf("."),l=s>=0,r=n.constructor,l&&(i=i.replace(".",""),d=i.length,s=d-s,e=wr(r,new r(t),s,s*2)),c=qe(i,t,re),p=c.length-1,s=p;c[s]===0;--s)c.pop();return s<0?new r(n.s*0):(n.e=He(c,p),n.d=c,z=!1,l&&(n=ut(n,e,d*4)),f&&(n=n.times(Math.abs(f)<54?$t(2,f):Se.pow(2,f))),z=!0,n)}function zr(n,i){var t,r=i.d.length;if(r<3)return i.isZero()?i:we(n,2,i,i);t=1.4*Math.sqrt(r),t=t>16?16:t|0,i=i.times(1/Qe(5,t)),i=we(n,2,i,i);for(var e,s=new n(5),l=new n(16),d=new n(20);t--;)e=i.times(i),i=i.times(s.plus(e.times(l.times(e).minus(d))));return i}function we(n,i,t,r,e){var s,l,d,f,c=1,p=n.precision,_=Math.ceil(p/$);for(z=!1,f=t.times(t),d=new n(r);;){if(l=ut(d.times(f),new n(i++*i++),p,1),d=e?r.plus(l):r.minus(l),r=ut(l.times(f),new n(i++*i++),p,1),l=d.plus(r),l.d[_]!==void 0){for(s=_;l.d[s]===d.d[s]&&s--;);if(s==-1)break}s=d,d=r,r=l,l=s,c++}return z=!0,l.d.length=_+1,l}function Qe(n,i){for(var t=n;--i;)t*=n;return t}function Mr(n,i){var t,r=i.s<0,e=oe(n,n.precision,1),s=e.times(.5);if(i=i.abs(),i.lte(s))return ae=r?4:1,i;if(t=i.divToInt(e),t.isZero())ae=r?3:2;else{if(i=i.minus(t.times(e)),i.lte(s))return ae=dr(t)?r?2:3:r?4:1,i;ae=dr(t)?r?1:4:r?3:2}return i.minus(e).abs()}function xi(n,i,t,r){var e,s,l,d,f,c,p,_,y,P=n.constructor,B=t!==void 0;if(B?(Jt(t,1,he),r===void 0?r=P.rounding:Jt(r,0,8)):(t=P.precision,r=P.rounding),!n.isFinite())p=yr(n);else{for(p=se(n),l=p.indexOf("."),B?(e=2,i==16?t=t*4-3:i==8&&(t=t*3-2)):e=i,l>=0&&(p=p.replace(".",""),y=new P(1),y.e=p.length-l,y.d=qe(se(y),10,e),y.e=y.d.length),_=qe(p,10,e),s=f=_.length;_[--f]==0;)_.pop();if(!_[0])p=B?"0p+0":"0";else{if(l<0?s--:(n=new P(n),n.d=_,n.e=s,n=ut(n,y,t,r,0,e),_=n.d,s=n.e,c=hr),l=_[t],d=e/2,c=c||_[t+1]!==void 0,c=r<4?(l!==void 0||c)&&(r===0||r===(n.s<0?3:2)):l>d||l===d&&(r===4||c||r===6&&_[t-1]&1||r===(n.s<0?8:7)),_.length=t,c)for(;++_[--t]>e-1;)_[t]=0,t||(++s,_.unshift(1));for(f=_.length;!_[f-1];--f);for(l=0,p="";l<f;l++)p+=Si.charAt(_[l]);if(B){if(f>1)if(i==16||i==8){for(l=i==16?4:3,--f;f%l;f++)p+="0";for(_=qe(p,e,i),f=_.length;!_[f-1];--f);for(l=1,p="1.";l<f;l++)p+=Si.charAt(_[l])}else p=p.charAt(0)+"."+p.slice(1);p=p+(s<0?"p":"p+")+s}else if(s<0){for(;++s;)p="0"+p;p="0."+p}else if(++s>f)for(s-=f;s--;)p+="0";else s<f&&(p=p.slice(0,s)+"."+p.slice(s))}p=(i==16?"0x":i==2?"0b":i==8?"0o":"")+p}return n.s<0?"-"+p:p}function fr(n,i){if(n.length>i)return n.length=i,!0}function Gr(n){return new this(n).abs()}function Vr(n){return new this(n).acos()}function Xr(n){return new this(n).acosh()}function Jr(n,i){return new this(n).plus(i)}function Yr(n){return new this(n).asin()}function tn(n){return new this(n).asinh()}function en(n){return new this(n).atan()}function rn(n){return new this(n).atanh()}function nn(n,i){n=new this(n),i=new this(i);var t,r=this.precision,e=this.rounding,s=r+4;return!n.s||!i.s?t=new this(NaN):!n.d&&!i.d?(t=oe(this,s,1).times(i.s>0?.25:.75),t.s=n.s):!i.d||n.isZero()?(t=i.s<0?oe(this,r,e):new this(0),t.s=n.s):!n.d||i.isZero()?(t=oe(this,s,1).times(.5),t.s=n.s):i.s<0?(this.precision=s,this.rounding=1,t=this.atan(ut(n,i,s,1)),i=oe(this,s,1),this.precision=r,this.rounding=e,t=n.s<0?t.minus(i):t.plus(i)):t=this.atan(ut(n,i,s,1)),t}function on(n){return new this(n).cbrt()}function sn(n){return H(n=new this(n),n.e+1,2)}function an(n,i,t){return new this(n).clamp(i,t)}function ln(n){if(!n||typeof n!="object")throw Error(Ue+"Object expected");var i,t,r,e=n.defaults===!0,s=["precision",1,he,"rounding",0,8,"toExpNeg",-be,0,"toExpPos",0,be,"maxE",0,be,"minE",-be,0,"modulo",0,9];for(i=0;i<s.length;i+=3)if(t=s[i],e&&(this[t]=Ii[t]),(r=n[t])!==void 0)if(Vt(r)===r&&r>=s[i+1]&&r<=s[i+2])this[t]=r;else throw Error(ce+t+": "+r);if(t="crypto",e&&(this[t]=Ii[t]),(r=n[t])!==void 0)if(r===!0||r===!1||r===0||r===1)if(r)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[t]=!0;else throw Error(mr);else this[t]=!1;else throw Error(ce+t+": "+r);return this}function un(n){return new this(n).cos()}function dn(n){return new this(n).cosh()}function Pr(n){var i,t,r;function e(s){var l,d,f,c=this;if(!(c instanceof e))return new e(s);if(c.constructor=e,cr(s)){c.s=s.s,z?!s.d||s.e>e.maxE?(c.e=NaN,c.d=null):s.e<e.minE?(c.e=0,c.d=[0]):(c.e=s.e,c.d=s.d.slice()):(c.e=s.e,c.d=s.d?s.d.slice():s.d);return}if(f=typeof s,f==="number"){if(s===0){c.s=1/s<0?-1:1,c.e=0,c.d=[0];return}if(s<0?(s=-s,c.s=-1):c.s=1,s===~~s&&s<1e7){for(l=0,d=s;d>=10;d/=10)l++;z?l>e.maxE?(c.e=NaN,c.d=null):l<e.minE?(c.e=0,c.d=[0]):(c.e=l,c.d=[s]):(c.e=l,c.d=[s]);return}if(s*0!==0){s||(c.s=NaN),c.e=NaN,c.d=null;return}return Le(c,s.toString())}if(f==="string")return(d=s.charCodeAt(0))===45?(s=s.slice(1),c.s=-1):(d===43&&(s=s.slice(1)),c.s=1),gr.test(s)?Le(c,s):$r(c,s);if(f==="bigint")return s<0?(s=-s,c.s=-1):c.s=1,Le(c,s.toString());throw Error(ce+s)}if(e.prototype=I,e.ROUND_UP=0,e.ROUND_DOWN=1,e.ROUND_CEIL=2,e.ROUND_FLOOR=3,e.ROUND_HALF_UP=4,e.ROUND_HALF_DOWN=5,e.ROUND_HALF_EVEN=6,e.ROUND_HALF_CEIL=7,e.ROUND_HALF_FLOOR=8,e.EUCLID=9,e.config=e.set=ln,e.clone=Pr,e.isDecimal=cr,e.abs=Gr,e.acos=Vr,e.acosh=Xr,e.add=Jr,e.asin=Yr,e.asinh=tn,e.atan=en,e.atanh=rn,e.atan2=nn,e.cbrt=on,e.ceil=sn,e.clamp=an,e.cos=un,e.cosh=dn,e.div=fn,e.exp=cn,e.floor=hn,e.hypot=pn,e.ln=mn,e.log=_n,e.log10=bn,e.log2=gn,e.max=wn,e.min=vn,e.mod=yn,e.mul=Mn,e.pow=Pn,e.random=An,e.round=Bn,e.sign=Sn,e.sin=In,e.sinh=En,e.sqrt=On,e.sub=xn,e.sum=Tn,e.tan=Cn,e.tanh=kn,e.trunc=Rn,n===void 0&&(n={}),n&&n.defaults!==!0)for(r=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],i=0;i<r.length;)n.hasOwnProperty(t=r[i++])||(n[t]=this[t]);return e.config(n),e}function fn(n,i){return new this(n).div(i)}function cn(n){return new this(n).exp()}function hn(n){return H(n=new this(n),n.e+1,3)}function pn(){var n,i,t=new this(0);for(z=!1,n=0;n<arguments.length;)if(i=new this(arguments[n++]),i.d)t.d&&(t=t.plus(i.times(i)));else{if(i.s)return z=!0,new this(1/0);t=i}return z=!0,t.sqrt()}function cr(n){return n instanceof Se||n&&n.toStringTag===_r||!1}function mn(n){return new this(n).ln()}function _n(n,i){return new this(n).log(i)}function gn(n){return new this(n).log(2)}function bn(n){return new this(n).log(10)}function wn(){return vr(this,arguments,-1)}function vn(){return vr(this,arguments,1)}function yn(n,i){return new this(n).mod(i)}function Mn(n,i){return new this(n).mul(i)}function Pn(n,i){return new this(n).pow(i)}function An(n){var i,t,r,e,s=0,l=new this(1),d=[];if(n===void 0?n=this.precision:Jt(n,1,he),r=Math.ceil(n/$),this.crypto)if(crypto.getRandomValues)for(i=crypto.getRandomValues(new Uint32Array(r));s<r;)e=i[s],e>=429e7?i[s]=crypto.getRandomValues(new Uint32Array(1))[0]:d[s++]=e%1e7;else if(crypto.randomBytes){for(i=crypto.randomBytes(r*=4);s<r;)e=i[s]+(i[s+1]<<8)+(i[s+2]<<16)+((i[s+3]&127)<<24),e>=214e7?crypto.randomBytes(4).copy(i,s):(d.push(e%1e7),s+=4);s=r/4}else throw Error(mr);else for(;s<r;)d[s++]=Math.random()*1e7|0;for(r=d[--s],n%=$,r&&n&&(e=$t(10,$-n),d[s]=(r/e|0)*e);d[s]===0;s--)d.pop();if(s<0)t=0,d=[0];else{for(t=-1;d[0]===0;t-=$)d.shift();for(r=1,e=d[0];e>=10;e/=10)r++;r<$&&(t-=$-r)}return l.e=t,l.d=d,l}function Bn(n){return H(n=new this(n),n.e+1,this.rounding)}function Sn(n){return n=new this(n),n.d?n.d[0]?n.s:0*n.s:n.s||NaN}function In(n){return new this(n).sin()}function En(n){return new this(n).sinh()}function On(n){return new this(n).sqrt()}function xn(n,i){return new this(n).sub(i)}function Tn(){var n=0,i=arguments,t=new this(i[n]);for(z=!1;t.s&&++n<i.length;)t=t.plus(i[n]);return z=!0,H(t,this.precision,this.rounding)}function Cn(n){return new this(n).tan()}function kn(n){return new this(n).tanh()}function Rn(n){return H(n=new this(n),n.e+1,1)}I[Symbol.for("nodejs.util.inspect.custom")]=I.toString;I[Symbol.toStringTag]="Decimal";var Se=I.constructor=Pr(Ii);je=new Se(je);We=new Se(We);var W=Se;var Ie=(r=>(r[r.Spot=0]="Spot",r[r.Curve=1]="Curve",r[r.BidAsk=2]="BidAsk",r))(Ie||{});var K=require("@cetusprotocol/common-sdk");var le=1e3,Dn=-443636,Fn=443636,ue=1e4,ve=2e3,_e=200,ge=443636n,Ke=1e8,Ti=1e9,te=1e4,Nn=604800,qn=1747627200;var at=Pi(Ne());var Ln=new at.default(524288),q=64,$e=new at.default(1).shln(q),Ar=new at.default(2).pow(new at.default(128)).sub(new at.default(1)),D=class n{static splitBinLiquidityInfo(i,t,r){let e=n.getPositionCount(t,r);if(e<=1)return[i];let s=[],l=t;for(let d=0;d<e;d++){let f=Math.min(l+1e3-2,r),c=i.bins.filter(p=>p.bin_id>=l&&p.bin_id<=f);s.push({bins:c,amount_a:c.reduce((p,_)=>(0,K.d)(p).plus(_.amount_a),(0,K.d)(0)).toFixed(0),amount_b:c.reduce((p,_)=>(0,K.d)(p).plus(_.amount_b),(0,K.d)(0)).toFixed(0)}),l=f+1}return s}static processBinsByRate(i,t){let r=[],e=(0,K.d)(0),s=(0,K.d)(0),l=!1;return i.forEach(d=>{let{amount_a:f,amount_b:c,liquidity:p="0"}=d,_=(0,K.d)(t).mul(p).toFixed(0),y=(0,K.d)(f).mul(t),P=(0,K.d)(c).mul(t);e=(0,K.d)(e).plus(y),s=(0,K.d)(s).plus(P),((0,K.d)(y).lt(1)&&(0,K.d)(y).gt(0)||(0,K.d)(P).lt(1)&&(0,K.d)(P).gt(0))&&(l=!0),r.push({bin_id:d.bin_id,amount_a:y.toFixed(0),amount_b:P.toFixed(0),price_per_lamport:d.price_per_lamport,liquidity:_})}),{bins:{bins:r,amount_a:e.toFixed(0),amount_b:s.toFixed(0)},has_invalid_amount:l}}static calculateOutByShare(i,t){let{amount_a:r,amount_b:e,liquidity:s="0"}=i;if(s==="0")return{amount_a:"0",amount_b:"0"};if((0,K.d)(t).gte((0,K.d)(s)))return{amount_a:r,amount_b:e};let l=(0,K.d)(t).div(s).mul(r).toFixed(0,W.ROUND_FLOOR),d=(0,K.d)(t).div(s).mul(e).toFixed(0,W.ROUND_FLOOR);return{amount_a:l,amount_b:d}}static getPositionCount(i,t){let e=(0,K.d)(t).sub(i).add(1).div(1e3);return Number(e.toFixed(0,W.ROUND_UP))}static getLiquidity(i,t,r){return(0,K.d)(r).mul(i).add((0,K.d)(t).mul((0,K.d)(2).pow(q))).toFixed(0)}static getAmountAFromLiquidity(i,t){return(0,K.d)(i).div((0,K.d)(t)).toFixed(0)}static getAmountBFromLiquidity(i){return(0,K.d)(i).div((0,K.d)(2).pow(q)).toFixed(0)}static getAmountsFromLiquidity(i,t,r,e){if((0,K.d)(e).isZero()&&J("LiquiditySupplyIsZero","Liquidity supply is zero"),(0,K.d)(r).gt((0,K.d)(e))&&J("InvalidDeltaLiquidity","Invalid delta liquidity"),(0,K.d)(r).isZero())return["0","0"];let s;(0,K.d)(i).isZero()?s="0":s=(0,K.d)(i).mul((0,K.d)(r)).div((0,K.d)(e)).toFixed(0,W.ROUND_FLOOR);let l;return(0,K.d)(t).isZero()?l="0":l=(0,K.d)(t).mul((0,K.d)(r)).div((0,K.d)(e)).toFixed(0,W.ROUND_FLOOR),[s,l]}static getPriceFromBinId(i,t,r,e){let s=n.getPricePerLamportFromBinId(i,t);return n.getPriceFromLamport(r,e,s).toString()}static getPricePerLamportFromBinId(i,t){let r=new W(t).div(new W(1e4));return new W(1).add(new W(r)).pow(new W(i)).toString()}static getBinIdFromPrice(i,t,r,e,s){let l=n.getPricePerLamport(e,s,i);return n.getBinIdFromLamportPrice(l,t,r)}static getBinIdFromLamportPrice(i,t,r){let e=new W(t).div(new W(1e4)),s=new W(i).log().dividedBy(new W(1).add(e).log());return(r?s.floor():s.ceil()).toNumber()}static getPricePerLamport(i,t,r){return new W(r).mul(new W(10**(t-i))).toString()}static getPriceFromLamport(i,t,r){return new W(r).div(new W(10**(t-i))).toString()}static getReversePrice(i){return new W(1).div(i).toString()}static getQPriceFromId(i,t){let r=new at.default(t).shln(q).div(new at.default(1e4)),e=$e.add(r);return n.pow(e,new at.default(i)).toString()}static getPricePerLamportFromQPrice(i){return K.MathUtil.fromX64(new at.default(i)).toString()}static pow(i,t){let r=t.isNeg();if(t.isZero())return $e;if(t=r?t.abs():t,t.gt(Ln))return new at.default(0);let e=i,s=$e;return e.gte(s)&&(e=Ar.div(e),r=!r),t.and(new at.default(1)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(2)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(4)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(8)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(16)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(32)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(64)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(128)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(256)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(512)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(1024)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(2048)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(4096)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(8192)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(16384)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(32768)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(65536)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(131072)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new at.default(262144)).isZero()||(s=s.mul(e).shrn(q)),s.isZero()?new at.default(0):(r&&(s=Ar.div(s)),s)}static binScore(i){let t=BigInt(i)+ge;return(t<0n||t>ge*2n)&&J("InvalidBinId",new Error("Invalid bin ID"),{[K.DETAILS_KEYS.METHOD_NAME]:"binScore",[K.DETAILS_KEYS.REQUEST_PARAMS]:{binId:i}}),t.toString()}static scoreToBinId(i){let t=BigInt(i)-ge;return(t<-ge||t>ge)&&J("InvalidBinId",new Error("Invalid score"),{[K.DETAILS_KEYS.METHOD_NAME]:"scoreToBinId",[K.DETAILS_KEYS.REQUEST_PARAMS]:{score:i}}),Number(t)}static resolveBinPosition(i){let t=BigInt(i),r=t>>4n,e=Number(t&0xfn);return[r.toString(),e]}static findMinMaxBinId(i){let t=1+i/1e4,e=new W("18446744073709551615").log(10).div(new W(t).log(10)).floor(),s=e.neg(),l=e,d=(0,K.d)(1),f=(0,K.d)("340282366920938463463374607431768211455");for(;;){let c=(0,K.d)(n.getQPriceFromId(s.toNumber(),i));if(c.gt(d)&&!c.isZero())break;s=s.add(1)}for(;;){let c=(0,K.d)(n.getQPriceFromId(l.toNumber(),i));if(c.lt(f)&&!c.isZero())break;l=l.sub(1)}return{minBinId:s.toNumber(),maxBinId:l.toNumber()}}static getBinShift(i,t,r){let e=n.getPricePerLamportFromBinId(i,t),s=(0,K.d)(e).mul(1+r).toString(),l=n.getBinIdFromLamportPrice(s,t,!0),d=(0,K.d)(l).sub(i).abs().toFixed(0,W.ROUND_UP);return console.log("getBinShift Options:",{active_id:i,bin_shift:d}),Number(d)}};var Xt=require("@mysten/sui/bcs"),Sr=require("blakejs");function Ci(n){try{let i=n.parsedJson;return{id:i.pool_id,bin_step:Number(i.bin_step),coin_type_a:(0,G.fixCoinType)(i.coin_type_a,!1),coin_type_b:(0,G.fixCoinType)(i.coin_type_b,!1)}}catch(i){return J("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseDlmmBasePool",[G.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function Ee(n){try{let i=(0,G.getObjectFields)(n),t=(0,G.getObjectType)(n),r=(0,G.extractStructTagFromType)(t),e={bin_step:i.bin_manager.fields.bin_step,bin_manager_handle:i.bin_manager.fields.bins.fields.id.id,size:i.bin_manager.fields.bins.fields.size},s={bin_step:i.position_manager.fields.bin_step,position_index:i.position_manager.fields.position_index,position_handle:i.position_manager.fields.positions.fields.id.id,size:i.position_manager.fields.positions.fields.size},l=i.reward_manager.fields,d=l.rewards.map(_=>{let y=_.fields.current_emission_rate,P=G.MathUtil.fromX64(new Br.default(y)),B=Math.floor(P.toNumber()*60*60*24).toString();return{reward_coin:(0,G.fixCoinType)(_.fields.reward_coin.fields.name,!1),emissions_per_second:P.toString(),emissions_per_day:B,period_emission_rates:{id:_.fields.period_emission_rates.fields.id.id,size:_.fields.period_emission_rates.fields.size}}}),f={is_public:l.is_public,emergency_reward_pause:l.emergency_reward_pause,vault:{id:l.vault.fields.id.id,size:l.vault.fields.size},rewards:d,last_updated_time:l.last_updated_time},c={volatility_accumulator:i.v_parameters.fields.volatility_accumulator,volatility_reference:i.v_parameters.fields.volatility_reference,index_reference:(0,G.asIntN)(BigInt(i.v_parameters.fields.index_reference.fields.bits)),last_update_timestamp:i.v_parameters.fields.last_update_timestamp,bin_step_config:i.v_parameters.fields.bin_step_config.fields},p={id:i.id.id,bin_step:Number(i.bin_step),coin_type_a:(0,G.fixCoinType)(r.type_arguments[0],!1),coin_type_b:(0,G.fixCoinType)(r.type_arguments[1],!1),pool_type:t,index:Number(i.index),bin_manager:e,variable_parameters:c,active_id:(0,G.asIntN)(BigInt(i.active_id.fields.bits)),permissions:i.permissions.fields,balance_a:i.balance_a,balance_b:i.balance_b,base_fee_rate:i.base_fee_rate,protocol_fee_a:i.protocol_fee_a,protocol_fee_b:i.protocol_fee_b,url:i.url,reward_manager:f,position_manager:s};return p.bin_step=p.bin_manager.bin_step,p}catch(i){return console.log("\u{1F680} ~ parseDlmmPool ~ error:",i),J("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseDlmmPool",[G.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function ze(n){let i=(0,G.getObjectFields)(n),t=(0,G.getObjectType)(n),r=(0,G.extractStructTagFromType)(t);return{id:i.id.id,name:i.name,ref_fee_rate:(0,G.d)(i.ref_fee_rate).div(1e4).toNumber(),start_time:Number(i.start_time),end_time:Number(i.end_time),balances:{id:i.balances.fields.id.id,size:i.balances.fields.size},type:r.full_address}}function Ge(n){try{let i=(0,G.getObjectFields)(n);return{uri:i.uri,index:i.index,id:i.id.id,name:i.name,pool_id:i.pool_id,lower_bin_id:(0,G.asIntN)(BigInt(i.lower_bin_id.fields.bits)),upper_bin_id:(0,G.asIntN)(BigInt(i.upper_bin_id.fields.bits)),liquidity_shares:i.liquidity_shares,description:i.description,coin_type_a:(0,G.fixCoinType)(i.coin_type_a,!1),coin_type_b:(0,G.fixCoinType)(i.coin_type_b,!1)}}catch(i){return console.log("\u{1F680} ~ parseDlmmPosition ~ error:",i),J("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseDlmmPosition",[G.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function jn(n,i,t,r){let e=n.map((d,f)=>{let c=t+f,p=D.getPricePerLamportFromBinId(c,i);if(c===r.bin_id){let{amount_a:P,amount_b:B}=D.calculateOutByShare(r,d);return{bin_id:c,amount_a:P,amount_b:B,liquidity:d,price_per_lamport:p}}if(c<r.bin_id){let P=D.getAmountBFromLiquidity(d);return{bin_id:c,amount_a:"0",amount_b:P,liquidity:d,price_per_lamport:p}}let _=D.getQPriceFromId(c,i),y=D.getAmountAFromLiquidity(d,_);return{bin_id:c,amount_a:y,amount_b:"0",liquidity:d,price_per_lamport:p}}),s=e.reduce((d,f)=>d.add(new W(f.amount_a)),new W(0)).toFixed(0),l=e.reduce((d,f)=>d.add(new W(f.amount_b)),new W(0)).toFixed(0);return{bins:e,amount_a:s,amount_b:l}}function ki(n){try{let i=Xt.bcs.struct("BinAmount",{id:Xt.bcs.struct("I32",{bits:Xt.bcs.u32()}),amount_a:Xt.bcs.u64(),amount_b:Xt.bcs.u64(),price:Xt.bcs.u128(),liquidity_supply:Xt.bcs.u128(),rewards_growth_global:Xt.bcs.vector(Xt.bcs.u128()),fee_a_growth_global:Xt.bcs.u128(),fee_b_growth_global:Xt.bcs.u128()});return Xt.bcs.vector(i).parse(Uint8Array.from(n.results[1].returnValues[0][0])).map(r=>({bin_id:(0,G.asIntN)(BigInt(r.id.bits)),amount_a:r.amount_a,amount_b:r.amount_b,liquidity:r.liquidity_supply,price_per_lamport:D.getPricePerLamportFromQPrice(r.price)}))}catch(i){return console.log("\u{1F680} ~ parseBinInfo ~ error:",i),[]}}function Ve(n){try{return{bin_id:(0,G.asIntN)(BigInt(n.id.fields.bits)),amount_a:n.amount_a,amount_b:n.amount_b,liquidity:n.liquidity_share,price_per_lamport:D.getPricePerLamportFromQPrice(n.price)}}catch(i){return J("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseBinInfo",[G.DETAILS_KEYS.REQUEST_PARAMS]:{fields:n}})}}function Ri(n){let i={},t=n.events?.filter(r=>r.type.includes("pool::CollectFeeEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:e}=t[r],s={position_id:e.position,fee_owned_a:e.fee_a,fee_owned_b:e.fee_b};i[e.position]=s}return i}function Di(n){let i={},t=n.events?.filter(r=>r.type.includes("pool::CollectRewardEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:e}=t[r],s=e.position,l=e.reward,d=e.amount,f={coin_type:(0,G.fixCoinType)(l.name,!1),reward_owned:d},c=i[s];c?c.rewards.push(f):c={position_id:s,rewards:[f]},i[s]=c}return i}function Fi(n,i){let t=n.events?.filter(r=>r.type.includes("pool::SwapEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:e}=t[r],{partner:s,pool:l,amount_in:d,amount_out:f,fee:c,ref_fee:p,bin_swaps:_,from:y,target:P}=e,B=_.map(C=>({bin_id:C.bin_id.bits,in_amount:C.amount_in,out_amount:C.amount_out,fee:C.fee,var_fee_rate:C.var_fee_rate}));return{pool_id:l,a2b:i,in_amount:d,out_amount:f,ref_fee_amount:p,fee_amount:c,bin_swaps:B,partner:s,from_coin_type:(0,G.fixCoinType)(y.name,!1),to_coin_type:(0,G.fixCoinType)(P.name,!1)}}}function Wn(n){switch(n){case 0:return 0;case 1:return 1;case 2:return 2}}var Ir=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","ClosePositionEvent"];function Ni(n,i,t,r){let e=[],{timestampMs:s,events:l}=n;return l?.forEach((d,f)=>{let{name:c,address:p}=(0,G.extractStructTagFromType)(d.type);if(Ir.includes(c)&&p===t&&r===d.parsedJson.pool){let _={tx:d.id.txDigest,sender:d.sender,type:d.type,block_time:s||"0",index:`${i}_${f}`,parsed_json:d.parsedJson};e.push(_)}}),e}function Zn(n,i,t){let r=[],e=0,s=new Date(n*1e3).getTime(),l=new Date().getTime();for(;;){let d=s+e*t;if(d>=l&&r.push(d),e>=i)break;e+=1}return r}function Un(n,i,t,r){let e=[];for(let s=i;s<=t;s+=r){let l=n.findLast(d=>(0,G.d)(s).gte(d.time));l?e.push({time:s.toString(),emissions_per_second:l.emissions_per_second,emissions_per_day:l.emissions_per_day,visualized_time:new Date(s*1e3).toLocaleString()}):e.push({emissions_per_day:"0",time:s.toString(),emissions_per_second:"0",visualized_time:new Date(s*1e3).toLocaleString()})}return e}function Hn(n){if(n.length===0)return;let i=new Date().getTime()/1e3,t=n.findLast(r=>(0,G.d)(i).gte(r.time));return t||n[n.length-1]}function ne(n,i){let t=n.mul(i);if(t.gt(0)&&t.lt(1))throw new me(`Multiplication ${t} is less than 1`,"AmountTooSmall");return t.floor()}function Me(n){if(n.gt(0)&&n.lt(1))throw new me(`Multiplication ${n.toString()} is less than 1`,"AmountTooSmall");return n.floor()}function qi(n){switch(n){case 0:return"spot";case 1:return"curve";case 2:return"bid_ask"}}function Li(n,i,t,r){let e=Buffer.from(n,"utf8"),s=Buffer.from(i,"utf8"),l=s.length,d=0;for(;d<l;){let y=s[d];e=Buffer.concat([e,Buffer.from([y])]),d++}let f=Xt.bcs.u16().serialize(t).toBytes(),c=Xt.bcs.u16().serialize(r).toBytes(),p=Buffer.concat([e,f,c]),_=(0,Sr.blake2b)(p,void 0,32);return`0x${Buffer.from(_).toString("hex")}`}var E=require("@cetusprotocol/common-sdk");var ee=class n{static toWeight(i){console.log("\u{1F680} ~ WeightUtils ~ toWeight ~ options:",i);let{strategy_type:t,active_id:r,bin_step:e,lower_bin_id:s,upper_bin_id:l,total_amount_a:d,total_amount_b:f,active_bin_of_pool:c}=i,p=r<s||r>l,_=D.getQPriceFromId(r,e),y=(0,E.d)(0),P=(0,E.d)(0),B=(0,E.d)(200);if(t===2?B=(0,E.d)(200):t===1?B=(0,E.d)(2e3):t===0&&(B=(0,E.d)(1)),!p&&c){let st=this.calculateActiveWeights(c.amount_a,c.amount_b,_,B);y=st.active_weight_a,P=st.active_weight_b}r===s&&(0,E.d)(f).isZero()&&(y=(0,E.d)(B).mul((0,E.d)(2).pow(q*2)).div((0,E.d)(_)).floor(),P=(0,E.d)(0)),r===l&&(0,E.d)(d).isZero()&&(P=(0,E.d)(B).mul((0,E.d)(2).pow(q)).floor(),y=(0,E.d)(0)),r>s&&r<l&&((0,E.d)(d).isZero()&&(P=(0,E.d)(B).mul((0,E.d)(2).pow(q)).floor(),y=(0,E.d)(0)),(0,E.d)(f).isZero()&&(y=(0,E.d)(B).mul((0,E.d)(2).pow(q*2)).div((0,E.d)(_)).floor(),P=(0,E.d)(0)));let S=p?(0,E.d)(0):y,C=p?(0,E.d)(0):P,k=(0,E.d)(2e3).sub((0,E.d)(200)).floor(),R=r>l?l:r,x=r<s?s:r,T=r>s?R===s?(0,E.d)(0):k.div((0,E.d)(R-s)).floor():(0,E.d)(0),Z=l>r?x===l?(0,E.d)(0):k.div((0,E.d)(l-x)).floor():(0,E.d)(0),j=s,Y=[],nt=[];for(;j<=l;){let st=(0,E.d)(0);if(j<r){let dt=R-j;t===0?st=(0,E.d)(1):t===2?st=(0,E.d)(B).add(T.mul(dt)).floor():t===1&&(st=(0,E.d)(B).sub(T.mul(dt)).floor())}else if(j>r){let dt=j-x;t===0?st=(0,E.d)(1):t===2?st=(0,E.d)(B).add(Z.mul(dt)).floor():t===1&&(st=(0,E.d)(B).sub(Z.mul(dt)).floor())}else st=B;if(Y.push(st),j<r)C=C.add(st.mul((0,E.d)(2).pow(q))).floor(),nt.push((0,E.d)(0));else if(j>r){let dt=st.mul((0,E.d)(2).pow(q*2)).div(D.getQPriceFromId(j,e)).floor();nt.push(dt),S=S.add(dt).floor()}else nt.push((0,E.d)(0));j+=1}return{...i,total_weight_a:S,total_weight_b:C,active_weight_a:y,active_weight_b:P,weights:Y,weight_per_prices:nt}}static toWeightSpotBalanced(i,t){let r=[];for(let e=i;e<=t;e++)r.push({bin_id:e,weight:1});return r}static toWeightDescendingOrder(i,t){let r=[];for(let e=i;e<=t;e++)r.push({bin_id:e,weight:t-e+1});return r}static toWeightAscendingOrder(i,t){let r=[];for(let e=i;e<=t;e++)r.push({bin_id:e,weight:e-i+1});return r}static toWeightCurve(i,t,r){if(r<i)return n.toWeightDescendingOrder(i,t);if(r>t)return n.toWeightAscendingOrder(i,t);let e=2e3,l=e-200,d=r>i?Math.floor(l/(r-i)):0,f=t>r?Math.floor(l/(t-r)):0,c=[];for(let p=i;p<=t;p++)p<r?c.push({bin_id:p,weight:e-(r-p)*d}):p>r?c.push({bin_id:p,weight:e-(p-r)*f}):c.push({bin_id:p,weight:e});return c}static toWeightBidAsk(i,t,r){if(r>t)return n.toWeightDescendingOrder(i,t);if(r<i)return n.toWeightAscendingOrder(i,t);let e=2e3,s=200,l=e-s,d=r>i?Math.floor(l/(r-i)):0,f=t>r?Math.floor(l/(t-r)):0,c=[];for(let p=i;p<=t;p++)p<r?c.push({bin_id:p,weight:s+(r-p)*d}):p>r?c.push({bin_id:p,weight:s+(p-r)*f}):c.push({bin_id:p,weight:s});return c}static toAmountBidSide(i,t,r,e,s=!1){let l=e.filter(f=>f.bin_id<=i).reduce(function(f,c){return s?c.bin_id>i?f:f.add(c.weight):c.bin_id>=i?f:f.add(c.weight)},(0,E.d)(0));if(l.cmp((0,E.d)(0))!=1)throw Error("Invalid parameters");return{bins:e.map(f=>{let c=D.getPricePerLamportFromBinId(f.bin_id,r);if(!(f.bin_id<=i)||f.bin_id>=i&&!s)return{bin_id:f.bin_id,amount_a:"0",amount_b:"0",price_per_lamport:c,liquidity:"0"};{let _=(0,E.d)(f.weight).div(l),y=ne((0,E.d)(t),_).toString(),P="0",B=D.getQPriceFromId(f.bin_id,r),S=D.getLiquidity(P,y,B);return{bin_id:f.bin_id,amount_b:y,amount_a:P,price_per_lamport:c,liquidity:S}}}),amount_a:"0",amount_b:t}}static toAmountAskSide(i,t,r,e,s=!1){let l=e.filter(f=>f.bin_id>=i).reduce(function(f,c){if(c.bin_id<=i&&!s)return f;{let p=D.getPricePerLamportFromBinId(c.bin_id,t),_=new W(c.weight).div(p);return f.add(_)}},new W(0));if(l.cmp(new W(0))!=1)throw Error("Invalid parameters");return{bins:e.map(f=>{let c=D.getPricePerLamportFromBinId(f.bin_id,t);if(!(f.bin_id>=i)||f.bin_id<=i&&!s)return{bin_id:f.bin_id,amount_a:"0",amount_b:"0",price_per_lamport:c,liquidity:"0"};{let y=new W(f.weight).div(c).div(l),P=ne((0,E.d)(r),y).toString(),B="0",S=D.getQPriceFromId(f.bin_id,t),C=D.getLiquidity(P,B,S);return{bin_id:f.bin_id,amount_a:P,amount_b:B,price_per_lamport:c,liquidity:C}}}),amount_a:r,amount_b:"0"}}static toAmountBothSide(i,t,r,e,s,l,d){let f=!(0,E.d)(r).isZero()&&(0,E.d)(e).isZero(),c=(0,E.d)(r).isZero()&&!(0,E.d)(e).isZero();if(i>d[d.length-1].bin_id)return n.toAmountBidSide(i,e,t,d);if(c&&i!==d[d.length-1].bin_id)return n.toAmountBidSide(i,e,t,d,!0);if(i<d[0].bin_id)return n.toAmountAskSide(i,t,r,d);if(f&&i!==d[0].bin_id)return n.toAmountAskSide(i,t,r,d,!0);let p=d.filter(_=>_.bin_id===i);if(p.length===1){let{totalWeightA:_,totalWeightB:y,activeWeightA:P,activeWeightB:B}=n.calculateTotalWeights(t,d,i,p[0],s,l,f?"a":c?"b":void 0),S=new W(r.toString()).div(_),C=new W(e.toString()).div(y),k=d.map(T=>{let Z=D.getPricePerLamportFromBinId(T.bin_id,t);if(T.bin_id<i||T.bin_id===i&&c){let lt=ne(C,new W(T.weight)),v="0",o=D.getQPriceFromId(T.bin_id,t),u=D.getLiquidity(v,lt.toString(),o);return{bin_id:T.bin_id,amount_a:"0",amount_b:lt.toString(),price_per_lamport:Z,liquidity:u}}if(T.bin_id>i||T.bin_id===i&&f){let lt=new W(T.weight).div(Z),v=ne(S,new W(lt)),o="0",u=D.getQPriceFromId(T.bin_id,t),h=D.getLiquidity(v.toString(),o,u);return{bin_id:T.bin_id,amount_a:v.toString(),amount_b:"0",price_per_lamport:Z,liquidity:h}}let j=ne(S,P),Y=ne(C,B),nt=j.toString(),st=Y.toString(),dt=D.getQPriceFromId(T.bin_id,t),X=D.getLiquidity(nt,st,dt);return{bin_id:T.bin_id,amount_a:nt,amount_b:st,price_per_lamport:Z,liquidity:X}}),R=k.reduce((T,Z)=>(0,E.d)(T).add((0,E.d)(Z.amount_a)),(0,E.d)(0)).toString(),x=k.reduce((T,Z)=>(0,E.d)(T).add((0,E.d)(Z.amount_b)),(0,E.d)(0)).toString();return{bins:k,amount_a:R,amount_b:x}}else{let{totalWeightA:_,totalWeightB:y}=n.calculateTotalWeights(t,d,i),P=new W(r.toString()).div(_),B=new W(e.toString()).div(y),S=d.map(R=>{let x=D.getPricePerLamportFromBinId(R.bin_id,t);if(R.bin_id<i){let T=ne(B,new W(R.weight));return{bin_id:R.bin_id,amount_a:"0",amount_b:T.toString(),price_per_lamport:x}}else{let T=new W(R.weight).div(x),Z=ne(P,T);return{bin_id:R.bin_id,amount_a:Z.toString(),amount_b:"0",price_per_lamport:x}}}),C=S.reduce((R,x)=>(0,E.d)(R).add((0,E.d)(x.amount_a)),(0,E.d)(0)).toString(),k=S.reduce((R,x)=>(0,E.d)(R).add((0,E.d)(x.amount_b)),(0,E.d)(0)).toString();return{bins:S,amount_a:C,amount_b:k}}}static autoFillCoinByWeight(i,t,r,e,s,l,d){if(i>d[d.length-1].bin_id)return n.toAmountBidSide(i,r,t,d);if(i<d[0].bin_id)return n.toAmountAskSide(i,t,r,d);let f=d.filter(P=>P.bin_id===i),{totalWeightA:c,totalWeightB:p}=n.calculateTotalWeights(t,d,i,f.length===1?f[0]:void 0,f.length===1?s:void 0,f.length===1?l:void 0),_=(0,E.d)(0);e?_=c.isZero()?new W(0):new W(r).div(c):_=p.isZero()?new W(0):new W(r).div(p);let y=ne(_,e?p:c).toString();return n.toAmountBothSide(i,t,e?r:y,e?y:r,s,l,d)}static calculateActiveWeights(i,t,r,e){let s=(0,E.d)(r),l=(0,E.d)(i),d=(0,E.d)(t),f=(0,E.d)(0),c=(0,E.d)(0);if(l.isZero()&&d.isZero())f=(0,E.d)(e).mul((0,E.d)(2).pow(q*2)).div(s.mul(2)).floor(),c=(0,E.d)(e).mul((0,E.d)(2).pow(q)).div(2).floor();else{if(l.isZero())f=(0,E.d)(0);else{let p=d.mul((0,E.d)(2).pow(q)).div(l);f=(0,E.d)(e).mul((0,E.d)(2).pow(q*2)).div(s.add(p)).floor()}if(d.isZero())c=(0,E.d)(0);else{let p=(0,E.d)(2).pow(q).add(s.mul(l).div(d)).floor();c=(0,E.d)(e).mul((0,E.d)(2).pow(q*2)).div(p).floor()}}return{active_weight_a:f,active_weight_b:c}}static calculateTotalWeights(i,t,r,e,s,l,d){let f=(0,E.d)(D.getPricePerLamportFromBinId(r,i)),c=(0,E.d)(0),p=(0,E.d)(0);if(s&&l&&e&&!d)if((0,E.d)(s).isZero()&&(0,E.d)(l).isZero())c=new W(e.weight).div(f.mul(new W(2))),p=new W(e.weight).div(new W(2));else{let P=new W(s.toString()),B=new W(l.toString());(0,E.d)(s).isZero()||(c=new W(e.weight).div(f.add(B.div(P)))),(0,E.d)(l).isZero()||(p=new W(e.weight).div(new W(1).add(f.mul(P).div(B))))}let _=c,y=p;return t.forEach(P=>{if((P.bin_id<r||d==="b")&&(y=y.add(new W(P.weight))),P.bin_id>r||d==="a"){let B=D.getPricePerLamportFromBinId(P.bin_id,i),S=new W(P.weight).div(B);_=_.add(S)}}),{totalWeightA:_,totalWeightB:y,activeWeightA:c,activeWeightB:p}}};var Yt=require("@cetusprotocol/common-sdk");var Pe=class{static toAmountsByWeights(i){let{total_weight_a:t,total_weight_b:r,weights:e,weight_per_prices:s,total_amount_a:l,total_amount_b:d,active_weight_a:f,active_weight_b:c,lower_bin_id:p,upper_bin_id:_,active_id:y,bin_step:P}=i,B=r.isZero()?(0,Yt.d)(0):(0,Yt.d)(d).mul((0,Yt.d)(2).pow(q*2)).div(r).floor(),S=t.isZero()?(0,Yt.d)(0):(0,Yt.d)(l).mul((0,Yt.d)(2).pow(q*2)).div(t).floor(),C=Me(S.mul(f).div((0,Yt.d)(2).pow(q*2))),k=Me(B.mul(c).div((0,Yt.d)(2).pow(q*2))),R=_-p+1,x=p,T=0,Z=[];for(;T<R;){let Y=(0,Yt.d)(0),nt=(0,Yt.d)(0),st=D.getQPriceFromId(x,P);x<y?nt=Me(B.mul(e[T]).div((0,Yt.d)(2).pow(q))):x>y?Y=Me(S.mul(s[T]).div((0,Yt.d)(2).pow(q*2))):(Y=C,nt=k);let dt=D.getLiquidity(Y.toString(),nt.toString(),st);Z.push({bin_id:x,amount_a:Y.toString(),amount_b:nt.toString(),price_per_lamport:D.getPricePerLamportFromBinId(x,P),liquidity:dt}),x+=1,T+=1}return{bins:Z,amount_a:l.toString(),amount_b:d.toString()}}static toAmountsBothSideByStrategy(i,t,r,e,s,l,d,f){let c=ee.toWeight({strategy_type:d,active_id:i,bin_step:t,lower_bin_id:r,upper_bin_id:e,total_amount_a:s,total_amount_b:l,active_bin_of_pool:f});return this.toAmountsByWeights(c)}static autoFillCoinByStrategy(i,t,r,e,s,l,d,f){switch(d){case 0:{let c=ee.toWeightSpotBalanced(s,l);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}case 1:{let c=ee.toWeightCurve(s,l,i);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}case 2:{let c=ee.toWeightBidAsk(s,l,i);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}}}static autoFillCoinByStrategyV2(i,t,r,e,s,l,d,f){let c=this.autoFillCoinByStrategy(i,t,r,e,s,l,d,f),p=ee.toWeight({strategy_type:d,active_id:i,bin_step:t,lower_bin_id:s,upper_bin_id:l,total_amount_a:c.amount_a,total_amount_b:c.amount_b,active_bin_of_pool:f});return this.toAmountsByWeights(p)}};var ft=require("@cetusprotocol/common-sdk");var Oe=class n{static getVariableFee(i){let{volatility_accumulator:t,bin_step_config:r}=i,{variable_fee_control:e,bin_step:s}=r;return(0,ft.d)(e).gt(0)?(0,ft.d)(t).mul(s).pow(2).mul(e).add(99999999999).div(1e11).toFixed(0):"0"}static calculateCompositionFee(i,t){let r=(0,ft.d)(i).mul(t);return(0,ft.d)(r).mul((0,ft.d)(1e9).add(t)).div(1e18).toFixed(0)}static calculateProtocolFee(i,t){return(0,ft.d)(i).mul(t).div(1e4).ceil().toFixed(0)}static getProtocolFees(i,t,r){let e=n.calculateProtocolFee(i,r),s=n.calculateProtocolFee(t,r);return{protocol_fee_a:e,protocol_fee_b:s}}static getCompositionFees(i,t,r){let{bin_step_config:e}=r;if((0,ft.d)(i.liquidity||"0").eq((0,ft.d)(0)))return{fees_a:"0",fees_b:"0"};let{bin_step:s,base_factor:l}=e,d=D.getQPriceFromId(i.bin_id,s),f=D.getLiquidity(i.amount_a,i.amount_b,d),c=D.getLiquidity(t.amount_a,t.amount_b,d),p=(0,ft.d)(i.liquidity).mul(c).div(f).toFixed(0),{amount_a:_,amount_b:y}=D.calculateOutByShare({bin_id:i.bin_id,liquidity:(0,ft.d)(i.liquidity).add(p).toFixed(0),amount_a:(0,ft.d)(i.amount_a).add(t.amount_a).toFixed(0),amount_b:(0,ft.d)(i.amount_b).add(t.amount_b).toFixed(0),price_per_lamport:i.price_per_lamport},p),P=(0,ft.d)(s).mul(l).mul(10),B=n.getVariableFee(r),S=(0,ft.d)(P).add(B).toFixed(0);(0,ft.d)(S).gt(1e8)&&(S=1e8.toString());let C="0",k="0";return(0,ft.d)(_).gt(t.amount_a)&&(0,ft.d)(t.amount_b).gt(y)?k=n.calculateCompositionFee((0,ft.d)(t.amount_b).sub(y).toFixed(0),S):(0,ft.d)(y).gt(t.amount_b)&&(0,ft.d)(t.amount_a).gt(_)&&(C=n.calculateCompositionFee((0,ft.d)(t.amount_a).sub(_).toFixed(0),S)),{fees_a:C,fees_b:k}}};var ji=class{static calculateIlm(i){let{curvature:t,initial_price:r,max_price:e,bin_step:s,total_supply:l,pool_share_percentage:d,config:f}=i,{price_curve_points_num:c,liquidity_distribution_num:p,tokens_table_num:_,price_table_num:y}=f;if(d<0||d>100)throw new Error("Pool Share Percentage must be greater than 0 and less than 100.");let P=!1;if(e<r)throw new Error("Maximum Price must be greater or equal to Initial Price.");if(e===r&&t!==0)throw new Error("Curvature must be 0 when Maximum and Initial Price are equal.");if(e!==r&&t===0)throw new Error("Maximum and Initial Price must be equal when Curvature is 0.");e===r&&t===0&&(P=!0);let B=L=>j*Math.pow(L/Z,t)+r,S=(L,ot)=>{let Q=j*Math.pow(Z,-t)*Math.pow(L,t+1)/(t+1)+r*L,et=j*Math.pow(Z,-t)*Math.pow(ot,t+1)/(t+1)+r*ot;return Q-et},C=L=>Z*Math.pow((L-r)/j,1/t),k=L=>Z*Math.pow(L-r,1/t-1)/(t*Math.pow(j,1/t)),R=L=>Math.pow((L-r)/j,1/t)*Z,x=(L,ot,Q)=>{let et=ot/1e4,Ae=Math.log(L)/Math.log(1+et);return Q?Math.floor(Ae):Math.ceil(Ae)},T=(L,ot)=>{let Q=ot/1e4;return Math.pow(1+Q,L)},Z=l*d/100,j=e-r,Y=[],nt=[],st=x(r,s,!1),dt=x(e,s,!1),X=0,lt=st;if(P)Y.push(r),nt.push(r*Z);else for(;lt<=dt;){let L=T(lt,s),ot=T(lt+1,s),Q=R(ot)-R(L);X=X+Q,nt.push(Q*L),Y.push(L),lt++}let v={price_curve:{data:[],min_y:0,max_y:0},liquidity_curve:{data:[],min_y:0,max_y:0},dlmm_bins:{data:[],min_y:0,max_y:0},tokens_table:[],price_table:[],initial_fdv:r*l,final_fdv:e*l,usdc_in_pool:0};var o=j/100;let u=[],h=0,m=0;for(let L=0;L<c;L++){let ot=L*Z/(c-1),Q=P?e:B(ot);h=h<Q?h:Q,m=m>Q?m:Q,u.push({x:ot,y:Q})}v.price_curve={data:u,min_y:h,max_y:m};let b=[],M=0,A=0;if(P)M=0,A=Z,b=[{x:r,y:0},{x:r,y:Z/2},{x:r,y:Z}];else for(let L=0;L<p;L++){let ot=r+L*o,Q=k(ot);b.push({x:ot,y:Q}),M=M<Q?M:Q,A=A>Q?A:Q}v.liquidity_curve={data:b,min_y:M,max_y:A};let g=[],a=0,w=0;Y.forEach((L,ot)=>{let Q=L,et=nt[ot];g.push({x:Q,y:et}),a=a<et?a:et,w=w>et?w:et}),v.dlmm_bins={data:g,min_y:a,max_y:w};let F=[],N=0,U=Z/_;for(;N<=Z;){let L=B(N),ot=N,Q=S(ot,0);F.push({withdrawn:ot,price:L,usdc_in_pool:Q}),N=N+U}v.tokens_table=F;let V=Math.floor(j*1e10)/1e11,tt=[];if(P)tt.push({withdrawn:Z,price:r,usdc_in_pool:Z*r});else{let L=0,ot=r;for(;L<=y;){let Q=C(ot),et=S(Q,0);tt.push({withdrawn:Q,price:ot,usdc_in_pool:et}),ot=ot+V,L=L+1}}return v.price_table=tt,v.usdc_in_pool=tt[tt.length-1].usdc_in_pool,v}};var Er=require("@mysten/sui/client"),Wi=require("@mysten/sui/utils"),Te=require("@mysten/sui/bcs"),Ce=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async getPoolAddress(i,t,r,e){try{let s=Li(i,t,r,e),{dlmm_pool:l}=this._sdk.sdkOptions,{pools_id:d}=(0,it.getPackagerConfigs)(l),f=await this._sdk.FullClient.getDynamicFieldObject({parentId:d,name:{type:"0x2::object::ID",value:s}});return(0,it.getObjectFields)(f).value.fields.value.fields.pool_id}catch(s){return J("FetchError",s,{[it.DETAILS_KEYS.METHOD_NAME]:"getPoolAddress",[it.DETAILS_KEYS.REQUEST_PARAMS]:{coin_type_a:i,coin_type_b:t,bin_step:r,base_factor:e}})}}async getBasePoolList(i="all",t=!1){let{dlmm_pool:r}=this._sdk.sdkOptions,e={data:[],has_next_page:!1},s=i==="all",l=`${r.package_id}_getBasePoolList`;if(s){let d=this._sdk.getCache(l,t);if(d&&d.length>0)return e.data.push(...d),e}try{let d=`${r.package_id}::registry::CreatePoolEvent`,f=await this._sdk.FullClient.queryEventsByPage({MoveEventType:d},i);e.has_next_page=f.has_next_page,e.next_cursor=f.next_cursor,f.data.forEach(c=>{let p=Ci(c);e.data.push(p)}),s&&this._sdk.updateCache(`${r.package_id}_getPoolImmutables`,e.data)}catch(d){return J("FetchError",d,{[it.DETAILS_KEYS.METHOD_NAME]:"getBasePoolList"})}return e}async getPools(i="all",t=!1){let r={data:[],has_next_page:!1},e=await this.getBasePoolList(i,t);if(e.data.length===0)return r;try{let s=await this._sdk.FullClient.batchGetObjects(e.data.map(l=>l.id),{showContent:!0,showType:!0});r.has_next_page=e.has_next_page,r.next_cursor=e.next_cursor;for(let l of s){let d=Ee(l),f=`${d.id}_getDlmmPool`;this._sdk.updateCache(f,d),r.data.push(d)}}catch(s){return J("FetchError",s,{[it.DETAILS_KEYS.METHOD_NAME]:"getPools"})}return r}async getBinInfo(i,t,r,e=!0){try{let s=`${i}_getBinInfo_${t}`,l=this._sdk.getCache(s,e);if(l!==void 0)return l;let d=D.binScore(t),[f,c]=D.resolveBinPosition(d),_=(await this._sdk.FullClient.getDynamicFieldObject({parentId:i,name:{type:"u64",value:f}})).data.content.fields.value.fields.value.fields.group.fields.bins[c].fields,y=Ve(_);return this._sdk.updateCache(s,y),y}catch{return{bin_id:t,amount_a:"0",amount_b:"0",liquidity:"0",price_per_lamport:D.getPricePerLamportFromBinId(t,r)}}}async getBinInfoList(i){let t=[],r=i.map(s=>{let{bin_manager_handle:l,bin_id:d,bin_step:f}=s,c=D.binScore(d),[p,_]=D.resolveBinPosition(c),y=(0,it.deriveDynamicFieldIdByType)(l,p,"u64","u64");return{groupIndex:p,offsetInGroup:_,bin_id:d,bin_step:f,bin_manager_handle:l,dynamic_field_id:y}});return(await this._sdk.FullClient.batchGetObjects(r.map(s=>s.dynamic_field_id),{showContent:!0,showType:!0})).forEach((s,l)=>{let{offsetInGroup:d,bin_manager_handle:f,bin_step:c,bin_id:p}=r[l];try{let y=(0,it.getObjectFields)(s).value.fields.value.fields.group.fields.bins[d].fields,P=Ve(y);t.push({...P,bin_manager_handle:f,bin_step:c})}catch{t.push({bin_id:p,amount_a:"0",amount_b:"0",liquidity:"0",price_per_lamport:D.getPricePerLamportFromBinId(p,c),bin_manager_handle:f,bin_step:c})}}),t}async getTotalFeeRate(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:e,coin_type_b:s}=i,l=new xe.Transaction;l.moveCall({target:`${t.published_at}::pool::get_total_fee_rate`,arguments:[l.object(r)],typeArguments:[e,s]});let d=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:l,sender:(0,Wi.normalizeSuiAddress)("0x0")});return Te.bcs.struct("FeeRate",{base_fee_rate:Te.bcs.u64(),var_fee_rate:Te.bcs.u64(),total_fee_rate:Te.bcs.u64()}).parse(Uint8Array.from(d.results[0].returnValues[0][0]))}async getPoolBinInfo(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:e,coin_type_b:s}=i,l=1e3,d=[],f,c=!0;for(;c;){let p=new xe.Transaction,_;f!==void 0?_=p.moveCall({target:"0x1::option::some",arguments:[p.pure.u32(Number((0,it.asUintN)(BigInt(f))))],typeArguments:["u32"]}):_=p.moveCall({target:"0x1::option::none",typeArguments:["u32"]}),p.moveCall({target:`${t.published_at}::pool::fetch_bins`,arguments:[p.object(r),_,p.pure.u64(l)],typeArguments:[e,s]});let y=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:p,sender:(0,Wi.normalizeSuiAddress)("0x0")}),P=ki(y);d.push(...P),f=P.length>0?P[P.length-1].bin_id+1:void 0,c=P.length===l}return d.sort((p,_)=>p.bin_id-_.bin_id)}async getPoolTransactionList({pool_id:i,pagination_args:t,order:r="descending",full_rpc_url:e}){let{FullClient:s,sdkOptions:l}=this._sdk,d;e?d=(0,it.createFullClient)(new Er.SuiClient({url:e})):d=s;let f={data:[],has_next_page:!1},c=50,p=t,_=t.limit||10;do{let y=await d.queryTransactionBlocksByPage({ChangedObject:i},{...p,limit:50},r);y.data.forEach((P,B)=>{let S=Ni(P,B,l.dlmm_pool.package_id,i);f.data=[...f.data,...S]}),f.has_next_page=y.has_next_page,f.next_cursor=y.next_cursor,p.cursor=y.next_cursor}while(f.data.length<_&&f.has_next_page);return f.data.length>_&&(f.data=f.data.slice(0,_),f.has_next_page=!0),f.data.length>0&&(f.next_cursor=f.data[f.data.length-1].tx),f}async getRangeBinInfo(i,t,r,e){let s=[];for(let l=t;l<=r;l++){let d=await Promise.all([this.getBinInfo(i,l,e)]);s.push(...d)}return s}async getAssignPoolList(i){if(i.length===0)return[];let t=[];try{let r=await this._sdk.FullClient.batchGetObjects(i,{showContent:!0,showType:!0});for(let e of r){let s=Ee(e),l=`${s.id}_getDlmmPool`;this._sdk.updateCache(l,s),t.push(s)}}catch(r){return J("FetchError",r,{[it.DETAILS_KEYS.METHOD_NAME]:"getAssignPoolList"})}return t}async getPool(i,t=!0){try{let r=`${i}_getDlmmPool`,e=this._sdk.getCache(r,t);if(e!==void 0)return e;let s=await this._sdk.FullClient.getObject({id:i,options:{showType:!0,showContent:!0}}),l=Ee(s);return this._sdk.updateCache(r,l),l}catch(r){return J("FetchError",r,{[it.DETAILS_KEYS.METHOD_NAME]:"getPool",[it.DETAILS_KEYS.REQUEST_PARAMS]:i})}}async createPoolAndAddWithPricePayload(i){let{bin_step:t,url:r,coin_type_a:e,coin_type_b:s,bin_infos:l,price_base_coin:d,price:f,lower_price:c,upper_price:p,decimals_a:_,decimals_b:y,strategy_type:P,use_bin_infos:B,base_factor:S,pool_id:C}=i,k,R,x,T=l;if(d==="coin_a")k=D.getBinIdFromPrice(c,t,!1,_,y),R=D.getBinIdFromPrice(p,t,!0,_,y),x=D.getBinIdFromPrice(f,t,!0,_,y);else{k=D.getBinIdFromPrice((0,it.d)(1).div(p).toString(),t,!1,_,y),R=D.getBinIdFromPrice((0,it.d)(1).div(c).toString(),t,!0,_,y),x=D.getBinIdFromPrice((0,it.d)(1).div(f).toString(),t,!1,_,y);let Y={pool_id:C,amount_a:l.amount_b,amount_b:l.amount_a,active_id:x,bin_step:t,lower_bin_id:k,upper_bin_id:R,active_bin_of_pool:void 0,strategy_type:i.strategy_type};T=await this.sdk.Position.calculateAddLiquidityInfo(Y)}let j={bin_step:t,url:r,coin_type_a:e,coin_type_b:s,bin_infos:T,lower_bin_id:k,upper_bin_id:R,active_id:x,strategy_type:P,use_bin_infos:B,base_factor:S};return this.createPoolAndAddLiquidityPayload(j)}createPoolPayload(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{bin_step:e,base_factor:s,url:l,coin_type_a:d,coin_type_b:f,active_id:c}=i;t=t||new xe.Transaction;let{registry_id:p,global_config_id:_,versioned_id:y}=(0,it.getPackagerConfigs)(r),[P,B]=t.moveCall({target:`${r.published_at}::registry::create_pool_v2`,arguments:[t.object(p),t.pure.u16(e),t.pure.u16(s),t.pure.u32(Number((0,it.asUintN)(BigInt(c)))),t.pure.string(l||""),t.object(_),t.object(y),t.object(it.CLOCK_ADDRESS)],typeArguments:[d,f]});return t.moveCall({target:`${r.published_at}::registry::destroy_receipt`,arguments:[t.object(P),B,t.object(y)],typeArguments:[d,f]}),B}createPoolAndAddLiquidityPayload(i){let{bin_step:t,base_factor:r,url:e,active_id:s,coin_type_a:l,coin_type_b:d,bin_infos:f,lower_bin_id:c,upper_bin_id:p,strategy_type:_,use_bin_infos:y}=i,P=new xe.Transaction;(0,it.isSortedSymbols)((0,it.fixCoinType)(l,!1),(0,it.fixCoinType)(d,!1))&&J("InvalidCoinTypeSequence",new Error("invalid coin type sequence"),{[it.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[it.DETAILS_KEYS.REQUEST_PARAMS]:i});let B=p-c+1;B>1e3&&J("InvalidBinWidth",new Error("Width is too large"),{[it.DETAILS_KEYS.METHOD_NAME]:"openPosition",[it.DETAILS_KEYS.REQUEST_PARAMS]:i}),console.log("\u{1F680} ~ createPoolAndAddLiquidityPayload ~ option:",{...i,width:B});let C={pool_id:this.createPoolPayload({active_id:s,bin_step:t,base_factor:r,coin_type_a:l,coin_type_b:d},P),bin_infos:f,coin_type_a:l,coin_type_b:d,lower_bin_id:c,upper_bin_id:p,active_id:s,strategy_type:_,use_bin_infos:y,max_price_slippage:0,bin_step:t};return this.sdk.Position.addLiquidityPayload(C,P),P}};var Xe=require("@cetusprotocol/common-sdk"),Zi={env:"testnet",full_rpc_url:Xe.FullRpcUrlTestnet,graph_rpc_url:Xe.GraphRpcUrlTestnet,dlmm_pool:{package_id:"0xb382224d12558da5f87624765065a8c7e8f5c899d0ee890610e2bb4e8c686be9",published_at:"0x1cdac9c678c5ec89c80409db8865c9c3f8e6207dfd988fb4ddd11e2806db0bf9",version:1,config:{registry_id:"0xdc91c4f094557b9d2a35fc6159ef32649a54c4aa9350860bf0d4b52b5f0a3990",pools_id:"0xb518b1de84a1ba1ab6c9a2d71fcdf382ef36045a4660497b3d77fb0a6df7709e",global_config_id:"0xe84ebca8d61cdd9312cec3787204d0ee1063d424a81dcda62cfb8b1887041a9f",versioned_id:"0xf8478d6dc081bc266229f25ac9c31a96b5e99ecbd82222e883368dda95829065",admin_cap_id:"0x273dbda2a1d62460a01a07831aec0fa8191a41341d634fb43f15acdb627edbce",partners_id:"0xd7ac594ed2e7756f0d4a98503dd970f314f2619a10c3613c705253a155cc9fea"}},dlmm_router:{package_id:"0xbc76d04e910452518efd5fd63d5fffe77c24855063c9cdb5501e06896bc34908",published_at:"0xe122ffbcfe5091398ba15a60e654e6589fd586b3865d5b4118292754f8cde301",version:1},faucet:{package_id:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",published_at:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",version:1}};var O=require("@cetusprotocol/common-sdk"),pe=require("@mysten/sui/transactions"),Or=require("@mysten/sui/utils");var Je=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}buildPositionType(){return`${this._sdk.sdkOptions.dlmm_pool.package_id}::position::Position`}async getOwnerPositionList(i){let t=[];try{(await this._sdk.FullClient.getOwnedObjectsByPage(i,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{StructType:this.buildPositionType()}})).data.forEach(e=>{t.push(Ge(e))})}catch(r){console.log("\u{1F680} ~ PositionModule ~ getOwnerPositionList ~ error:",r),J("GetObjectError",r,{[O.DETAILS_KEYS.METHOD_NAME]:"getOwnerPositionList",[O.DETAILS_KEYS.REQUEST_PARAMS]:i})}return t}async getPosition(i){try{let t=await this._sdk.FullClient.getObject({id:i,options:{showType:!0,showContent:!0,showOwner:!0}});return Ge(t)}catch(t){return console.log("\u{1F680} ~ PositionModule ~ getPosition ~ error:",t),J("GetObjectError",t,{[O.DETAILS_KEYS.METHOD_NAME]:"getPosition",[O.DETAILS_KEYS.REQUEST_PARAMS]:i})}}collectFeePayload(i,t){let{pool_id:r,position_id:e,coin_type_a:s,coin_type_b:l}=i,{dlmm_pool:d}=this.sdk.sdkOptions,{versioned_id:f,global_config_id:c}=(0,O.getPackagerConfigs)(d);t=t||new pe.Transaction;let[p,_]=t.moveCall({target:`${d.published_at}::pool::collect_position_fee`,arguments:[t.object(r),t.object(e),t.object(c),t.object(f)],typeArguments:[s,l]}),y=O.CoinAssist.fromBalance(p,s,t),P=O.CoinAssist.fromBalance(_,l,t);return t.transferObjects([y,P],this.sdk.getSenderAddress()),t}updatePositionFeeAndRewards(i,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:e}=(0,O.getPackagerConfigs)(r),{pool_id:s,position_id:l,coin_type_a:d,coin_type_b:f}=i;return t.moveCall({target:`${r.published_at}::pool::update_position_fee_and_rewards`,arguments:[t.object(s),t.pure.id(l),t.object(e),t.object(O.CLOCK_ADDRESS)],typeArguments:[d,f]}),t}collectRewardPayload(i,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:e,global_config_id:s}=(0,O.getPackagerConfigs)(r);return t=t||new pe.Transaction,i.forEach(l=>{let{pool_id:d,position_id:f,reward_coins:c,coin_type_a:p,coin_type_b:_}=l;c.forEach(y=>{let P=t.moveCall({target:`${r.published_at}::pool::collect_position_reward`,arguments:[t.object(d),t.object(f),t.object(s),t.object(e)],typeArguments:[p,_,y]}),B=O.CoinAssist.fromBalance(P,y,t);t.transferObjects([B],this.sdk.getSenderAddress())})}),t}collectRewardAndFeePayload(i,t){return t=t||new pe.Transaction,i.forEach(r=>{let{pool_id:e,position_id:s,reward_coins:l,coin_type_a:d,coin_type_b:f}=r;this.updatePositionFeeAndRewards({pool_id:e,position_id:s,coin_type_a:d,coin_type_b:f},t),this.collectFeePayload({pool_id:e,position_id:s,coin_type_a:d,coin_type_b:f},t),this.collectRewardPayload([{pool_id:e,position_id:s,reward_coins:l,coin_type_a:d,coin_type_b:f}],t)}),t}validateActiveIdSlippage(i,t){let{pool_id:r,active_id:e,max_price_slippage:s,bin_step:l,coin_type_a:d,coin_type_b:f}=i,{dlmm_router:c}=this.sdk.sdkOptions,p=D.getBinShift(e,l,s),_=Number((0,O.asUintN)(BigInt(e)));return t.moveCall({target:`${c.published_at}::utils::validate_active_id_slippage`,arguments:[typeof r=="string"?t.object(r):r,t.pure.u32(Number(_)),t.pure.u32(Number(p))],typeArguments:[d,f]}),t}closePositionPayload(i,t){let{pool_id:r,position_id:e,reward_coins:s,coin_type_a:l,coin_type_b:d}=i,{dlmm_pool:f}=this.sdk.sdkOptions,{versioned_id:c,global_config_id:p}=(0,O.getPackagerConfigs)(f);t=t||new pe.Transaction,this.updatePositionFeeAndRewards({pool_id:r,position_id:e,coin_type_a:l,coin_type_b:d},t),this.collectRewardPayload([{pool_id:r,position_id:e,reward_coins:s,coin_type_a:l,coin_type_b:d}],t);let[_,y,P]=t.moveCall({target:`${f.published_at}::pool::close_position`,arguments:[t.object(r),t.object(e),t.object(p),t.object(c),t.object(O.CLOCK_ADDRESS)],typeArguments:[l,d]}),B=O.CoinAssist.fromBalance(y,l,t),S=O.CoinAssist.fromBalance(P,d,t);return t.moveCall({target:`${f.published_at}::pool::destroy_close_position_cert`,arguments:[_,t.object(c)],typeArguments:[]}),t.transferObjects([B,S],this.sdk.getSenderAddress()),t}async getActiveBinIfInRange(i,t,r,e,s,l=!1){if(e<=r&&e>=t)return await this._sdk.Pool.getBinInfo(i,e,s,l)}calculateRemoveLiquidityInfo(i){let{bins:t,active_id:r,coin_amount:e}=i,s="fix_amount_a"in i,l=t.filter(y=>y.bin_id<r),d=t.filter(y=>y.bin_id>r),f=(0,O.d)(0),c=(0,O.d)(0),p=[];if(s){p=[...t];let{fix_amount_a:y}=i,P=t.find(C=>C.bin_id===r),B=d.reduce((C,k)=>(0,O.d)(C).plus(k.amount_a),(0,O.d)(0)).add(P?.amount_a||"0"),S=l.reduce((C,k)=>(0,O.d)(C).plus(k.amount_b),(0,O.d)(0)).add(P?.amount_b||"0");f=y?B:S,c=(0,O.d)(e).gte(f)?(0,O.d)(1):(0,O.d)(e).div(f)}else{let{is_only_a:y}=i;p=y?d:l,f=p.reduce((P,B)=>(0,O.d)(P).plus(y?B.amount_a:B.amount_b),(0,O.d)(0)),c=(0,O.d)(e).gte(f)?(0,O.d)(1):(0,O.d)(e).div(f)}if((0,O.d)(f).isZero())return J("InsufficientLiquidity",new Error("Insufficient liquidity"),{[O.DETAILS_KEYS.METHOD_NAME]:"calculateRemoveLiquidityResult",[O.DETAILS_KEYS.REQUEST_PARAMS]:i});let _=D.processBinsByRate([...p],c.toFixed());if(_.has_invalid_amount){let y=(0,O.d)(e),P=[],B=s?i.fix_amount_a:i.is_only_a;for(let S of p){if(y.lte(0))break;let C=B?S.amount_a:S.amount_b,k=B?S.amount_b:S.amount_a,R=(0,O.d)((0,O.d)(y).div(C).toFixed(9,W.ROUND_UP));R=R.gt(1)?(0,O.d)(1):R;let x=(0,O.d)(C).mul(R);y=y.minus(x);let T=(0,O.d)(k).mul(R),Z=(0,O.d)(S.liquidity).mul(R);P.push({...S,amount_a:B?x.toFixed(0):T.toFixed(0),amount_b:B?T.toFixed(0):x.toFixed(0),liquidity:Z.toFixed(0)})}return{bins:P,amount_a:P.reduce((S,C)=>(0,O.d)(S).plus(C.amount_a),(0,O.d)(0)).toFixed(0),amount_b:P.reduce((S,C)=>(0,O.d)(S).plus(C.amount_b),(0,O.d)(0)).toFixed(0)}}return _.bins}async calculateAddLiquidityInfo(i){let t="fix_amount_a"in i,{active_id:r,bin_step:e,lower_bin_id:s,upper_bin_id:l,active_bin_of_pool:d,strategy_type:f,pool_id:c}=i,p;if(t){let{coin_amount:_,fix_amount_a:y}=i;p=Pe.autoFillCoinByStrategyV2(r,e,_,y,s,l,f,d)}else p=Pe.toAmountsBothSideByStrategy(r,e,s,l,i.amount_a,i.amount_b,f,d);if(d&&c){let _=p.bins.findIndex(y=>y.bin_id===r);if(_!==-1){let y=await this._sdk.Pool.getPool(c,!1);if(y){let{fees_a:P,fees_b:B}=Oe.getCompositionFees(d,p.bins[_],y.variable_parameters),S=p.bins[_];S.amount_a=(0,O.d)(S.amount_a).sub(P).toFixed(0),S.amount_b=(0,O.d)(S.amount_b).sub(B).toFixed(0),p.bins[_]=S,console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_a:",P),console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_b:",B)}}}return p}removeLiquidityPayload(i){let{pool_id:t,position_id:r,bin_infos:e,reward_coins:s,slippage:l,coin_type_a:d,coin_type_b:f,active_id:c,collect_fee:p,bin_step:_,remove_percent:y}=i,{dlmm_pool:P}=this.sdk.sdkOptions,{bins:B}=e,S=new pe.Transaction;(p||s.length>0)&&this.updatePositionFeeAndRewards({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},S),p&&this.collectFeePayload({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},S),this.collectRewardPayload([{pool_id:t,position_id:r,reward_coins:s,coin_type_a:d,coin_type_b:f}],S);let{versioned_id:C,global_config_id:k}=(0,O.getPackagerConfigs)(P);if(y){let R=(0,O.asUintN)(BigInt(B[0].bin_id)),x=(0,O.asUintN)(BigInt(B[B.length-1].bin_id)),T=Number((0,O.d)(y).mul(1e4).toFixed(0)),[Z,j]=S.moveCall({target:`${P.published_at}::pool::remove_liquidity_by_percent`,arguments:[S.object(t),S.object(r),S.pure.u32(Number(R)),S.pure.u32(Number(x)),S.pure.u16(T),S.object(k),S.object(C),S.object(O.CLOCK_ADDRESS)],typeArguments:[d,f]}),Y=O.CoinAssist.fromBalance(Z,d,S),nt=O.CoinAssist.fromBalance(j,f,S);S.transferObjects([Y,nt],this.sdk.getSenderAddress())}else{let R=S.pure.vector("u32",B.map(nt=>Number((0,O.asUintN)(BigInt(nt.bin_id))))),x=S.pure.vector("u128",B.map(nt=>nt.liquidity)),[T,Z]=S.moveCall({target:`${P.published_at}::pool::remove_liquidity`,arguments:[S.object(t),S.object(r),R,x,S.object(k),S.object(C),S.object(O.CLOCK_ADDRESS)],typeArguments:[d,f]}),j=O.CoinAssist.fromBalance(T,d,S),Y=O.CoinAssist.fromBalance(Z,f,S);S.transferObjects([j,Y],this.sdk.getSenderAddress())}return this.validateActiveIdSlippage({pool_id:t,active_id:c,max_price_slippage:l,bin_step:_,coin_type_a:d,coin_type_b:f},S),S}async addLiquidityWithPricePayload(i){let{pool_id:t,bin_infos:r,coin_type_a:e,coin_type_b:s,price_base_coin:l,price:d,lower_price:f,upper_price:c,bin_step:p,strategy_type:_,active_bin_of_pool:y,decimals_a:P,decimals_b:B,use_bin_infos:S,max_price_slippage:C}=i,k,R,x,T=r;if(l==="coin_a")k=D.getBinIdFromPrice(f,p,!1,P,B),R=D.getBinIdFromPrice(c,p,!0,P,B),x=D.getBinIdFromPrice(d,p,!0,P,B);else{k=D.getBinIdFromPrice((0,O.d)(1).div(c).toString(),p,!1,P,B),R=D.getBinIdFromPrice((0,O.d)(1).div(f).toString(),p,!0,P,B),x=D.getBinIdFromPrice((0,O.d)(1).div(d).toString(),p,!1,P,B);let Y={amount_a:r.amount_b,amount_b:r.amount_a,active_id:x,bin_step:p,lower_bin_id:k,upper_bin_id:R,active_bin_of_pool:y,strategy_type:_};T=await this.sdk.Position.calculateAddLiquidityInfo(Y)}let j={pool_id:t,active_id:x,bin_infos:T,coin_type_a:e,coin_type_b:s,lower_bin_id:k,upper_bin_id:R,strategy_type:_,use_bin_infos:S,max_price_slippage:C,bin_step:p};return this.addLiquidityPayload(j)}addLiquidityPayload(i,t){let{pool_id:r,bin_infos:e,coin_type_a:s,coin_type_b:l,active_id:d,strategy_type:f,max_price_slippage:c,bin_step:p,use_bin_infos:_=!1}=i;t=t||new pe.Transaction;let y="lower_bin_id"in i,P=[];if(y){let B=D.splitBinLiquidityInfo(e,i.lower_bin_id,i.upper_bin_id);P.push(...B)}else{let B=i.position_id;P.push(e),(i.collect_fee||i.reward_coins.length>0)&&this.updatePositionFeeAndRewards({pool_id:r,position_id:B,coin_type_a:s,coin_type_b:l},t),i.collect_fee&&this.collectFeePayload({pool_id:r,position_id:B,coin_type_a:s,coin_type_b:l},t),i.reward_coins.length>0&&this.collectRewardPayload([{pool_id:r,position_id:B,reward_coins:i.reward_coins,coin_type_a:s,coin_type_b:l}],t)}return P.forEach((B,S)=>{console.log("\u{1F680} ~ PositionModule ~ addLiquidityPayload ~ liquidity_bin:",S,B);let{amount_a:C,amount_b:k,bins:R}=B,x=O.CoinAssist.buildCoinWithBalance(BigInt(C),s,t),T=O.CoinAssist.buildCoinWithBalance(BigInt(k),l,t);_?this.addLiquidityInternal({pool_id:r,coin_type_a:s,coin_type_b:l,active_id:d,liquidity_bin:B,tx:t,coin_a_obj_id:x,coin_b_obj_id:T,position_id:y?void 0:i.position_id,max_price_slippage:c,bin_step:p}):this.addLiquidityStrategyInternal({pool_id:r,coin_type_a:s,coin_type_b:l,active_id:d,liquidity_bin:B,tx:t,max_price_slippage:c,bin_step:p,coin_a_obj_id:x,coin_b_obj_id:T,strategy_type:f,position_id:y?void 0:i.position_id})}),t}addLiquidityStrategyInternal(i){let{max_price_slippage:t,bin_step:r,position_id:e,pool_id:s,coin_type_a:l,coin_type_b:d,active_id:f,liquidity_bin:c,tx:p,coin_a_obj_id:_,coin_b_obj_id:y,strategy_type:P}=i,{dlmm_pool:B,dlmm_router:S}=this.sdk.sdkOptions,{versioned_id:C,global_config_id:k}=(0,O.getPackagerConfigs)(B),{bins:R,amount_a:x,amount_b:T}=c,Z=e,j=R[0].bin_id,Y=R[R.length-1].bin_id,nt=(0,O.asUintN)(BigInt(j)),st=Number((0,O.asUintN)(BigInt(f))),dt=D.getBinShift(f,r,t),X=qi(P);if(e===void 0){let lt=Y-j+1;lt>1e3&&J("InvalidBinWidth",new Error("Width is too large"),{[O.DETAILS_KEYS.METHOD_NAME]:"openPosition"}),Z=p.moveCall({target:`${S.published_at}::${X}::open_position`,arguments:[typeof s=="string"?p.object(s):s,_,y,p.pure.u64(x),p.pure.u64(T),p.pure.u32(Number(nt)),p.pure.u16(Number(lt)),p.pure.u32(Number(st)),p.pure.u32(Number(dt)),p.object(k),p.object(C),p.object(O.CLOCK_ADDRESS)],typeArguments:[l,d]})}else{let lt=R.filter(u=>u.amount_a!=="0"||u.amount_b!=="0");if(lt.length===0)return J("InvalidParams",new Error("No bins to add liquidity"),{[O.DETAILS_KEYS.METHOD_NAME]:"addLiquidityStrategyInternal",[O.DETAILS_KEYS.REQUEST_PARAMS]:i});let v=(0,O.asUintN)(BigInt(lt[0].bin_id)),o=(0,O.asUintN)(BigInt(lt[lt.length-1].bin_id));p.moveCall({target:`${S.published_at}::${X}::add_liquidity`,arguments:[typeof s=="string"?p.object(s):s,p.object(e),_,y,p.pure.u64(x),p.pure.u64(T),p.pure.u32(Number(v)),p.pure.u32(Number(o)),p.pure.u32(Number(st)),p.pure.u32(Number(dt)),p.object(k),p.object(C),p.object(O.CLOCK_ADDRESS)],typeArguments:[l,d]})}return Z?p.transferObjects([Z,_,y],this.sdk.getSenderAddress()):p.transferObjects([_,y],this.sdk.getSenderAddress()),p}addLiquidityInternal(i){let{position_id:t,pool_id:r,coin_type_a:e,coin_type_b:s,active_id:l,liquidity_bin:d,tx:f,coin_a_obj_id:c,coin_b_obj_id:p,max_price_slippage:_,bin_step:y}=i,{bins:P}=d,{dlmm_pool:B,dlmm_router:S}=this.sdk.sdkOptions,{versioned_id:C,global_config_id:k}=(0,O.getPackagerConfigs)(B),R=f.pure.vector("u64",P.map(Y=>Y.amount_a)),x=f.pure.vector("u64",P.map(Y=>Y.amount_b)),T=f.makeMoveVec({elements:P.map(Y=>f.pure.u32(Number((0,O.asUintN)(BigInt(Y.bin_id))))),type:"u32"}),Z=d.bins[0].bin_id,j=d.bins[d.bins.length-1].bin_id;if(t===void 0){j-Z+1>1e3&&J("InvalidBinWidth",new Error("Width is too large"),{[O.DETAILS_KEYS.METHOD_NAME]:"openPosition"});let nt=f.moveCall({target:`${S.published_at}::add_liquidity::open_position`,arguments:[typeof r=="string"?f.object(r):r,c,p,T,R,x,f.object(k),f.object(C),f.object(O.CLOCK_ADDRESS)],typeArguments:[e,s]});l>=Z&&l<=j&&this.validateActiveIdSlippage({pool_id:r,active_id:l,max_price_slippage:_,bin_step:y,coin_type_a:e,coin_type_b:s},f),f.transferObjects([c,p,nt],this.sdk.getSenderAddress())}else f.moveCall({target:`${S.published_at}::add_liquidity::add_liquidity`,arguments:[typeof r=="string"?f.object(r):r,f.object(t),c,p,T,R,x,f.object(k),f.object(C),f.object(O.CLOCK_ADDRESS)],typeArguments:[e,s]}),l>=Z&&l<=j&&this.validateActiveIdSlippage({pool_id:r,active_id:l,max_price_slippage:_,bin_step:y,coin_type_a:e,coin_type_b:s},f),f.transferObjects([c,p],this.sdk.getSenderAddress());return f}async fetchPositionFeeAndReward(i){let t=new pe.Transaction;this.collectRewardAndFeePayload(i,t);let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:t,sender:(0,Or.normalizeSuiAddress)("0x0")});if(r.error!=null)return J("FetchError",new Error(r.error),{[O.DETAILS_KEYS.METHOD_NAME]:"fetchPositionFeeAndReward",[O.DETAILS_KEYS.REQUEST_PARAMS]:{options:i,totalOptions:i.length}});let e=Ri(r),s=Di(r);return{feeData:e,rewardData:s}}};var Ui=require("@mysten/sui/transactions"),Gt=require("@cetusprotocol/common-sdk");var xr=require("@mysten/sui/utils");var Ye=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async preSwapQuote(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:e,coin_type_b:s,a2b:l,by_amount_in:d,in_amount:f}=i,{versioned_id:c,global_config_id:p}=(0,Gt.getPackagerConfigs)(t),_=new Ui.Transaction;_.moveCall({target:`${t.published_at}::pool::flash_swap`,arguments:[_.object(r),_.pure.bool(l),_.pure.bool(d),_.pure.u64(f),_.object(p),_.object(c),_.object(Gt.CLOCK_ADDRESS)],typeArguments:[e,s]});let y=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:_,sender:(0,xr.normalizeSuiAddress)("0x0")});if(y.error!=null)return J("FetchError",new Error(y.error),{[Gt.DETAILS_KEYS.METHOD_NAME]:"fetchSwapQuote",[Gt.DETAILS_KEYS.REQUEST_PARAMS]:i});let P=Fi(y,l);return P==null?J("FetchError",new Error("No quote info"),{[Gt.DETAILS_KEYS.METHOD_NAME]:"preSwapQuote",[Gt.DETAILS_KEYS.REQUEST_PARAMS]:i}):(P.a2b=l,P)}swapPayload(i){let{dlmm_pool:t,dlmm_router:r}=this._sdk.sdkOptions,{quote_obj:e,coin_type_a:s,coin_type_b:l,by_amount_in:d,slippage:f,partner:c}=i,{pool_id:p,in_amount:_,out_amount:y,a2b:P}=e,B=new Ui.Transaction,S=d?_:(0,Gt.d)(_).mul(1+f).toFixed(0),C=d?(0,Gt.d)(y).mul(1-f).toFixed(0):y,k=Gt.CoinAssist.buildCoinWithBalance(BigInt(S),P?s:l,B),{versioned_id:R,global_config_id:x}=(0,Gt.getPackagerConfigs)(t);return console.log("\u{1F680} ~ SwapModule ~ option:",{...i,in_amount_limit:S,out_amount_limit:C}),c?B.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b_with_partner":"swap_b2a_with_partner"}`,arguments:[B.object(p),B.object(c),k,B.pure.bool(d),B.pure.u64(BigInt(d?_:y)),B.pure.u64(BigInt(d?C:S)),B.object(x),B.object(R),B.object(Gt.CLOCK_ADDRESS)],typeArguments:[s,l]}):B.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b":"swap_b2a"}`,arguments:[B.object(p),k,B.pure.bool(d),B.pure.u64(BigInt(d?_:y)),B.pure.u64(BigInt(d?C:S)),B.object(x),B.object(R),B.object(Gt.CLOCK_ADDRESS)],typeArguments:[s,l]}),B.transferObjects([k],this.sdk.getSenderAddress()),B}};var ke=require("@mysten/sui/transactions"),rt=require("@cetusprotocol/common-sdk");var ti=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async getPartnerList(){let i=[],{dlmm_pool:t}=this._sdk.sdkOptions,{partners_id:r}=(0,rt.getPackagerConfigs)(t);try{let e=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}}),l=(0,rt.getObjectFields)(e).partners.fields.contents.map(d=>d.fields.value);l.length>0&&(await this._sdk.FullClient.batchGetObjects(l,{showContent:!0,showType:!0})).forEach(f=>{let c=ze(f);i.push(c)})}catch(e){return J("FetchError",e,{[rt.DETAILS_KEYS.METHOD_NAME]:"getPartnerList",[rt.DETAILS_KEYS.REQUEST_PARAMS]:r})}return i}async getPartnerCapId(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions;try{let e=`partner_cap_id_${i}_${t}`,s=this._sdk.getCache(e);if(s)return s;let l=await this._sdk.FullClient.getOwnedObjects({owner:i,options:{showContent:!0,showType:!0},filter:{StructType:`${r.package_id}::partner::PartnerCap`}}),d;return l.data.forEach(f=>{let c=(0,rt.getObjectFields)(f);c.partner_id===t&&(d=c.id.id,this._sdk.updateCache(e,d))}),d||J("NotFound",new Error("Partner cap not found"),{[rt.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[rt.DETAILS_KEYS.REQUEST_PARAMS]:{owner:i,partner_id:t}})}catch(e){return J("FetchError",e,{[rt.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[rt.DETAILS_KEYS.REQUEST_PARAMS]:{owner:i,partner_id:t}})}}async getPartnerBalance(i){try{let t=await this._sdk.FullClient.getDynamicFieldsByPage(i),r=[],e=t.data.map(s=>s.objectId);return e.length>0&&(await this._sdk.FullClient.batchGetObjects(e,{showContent:!0,showType:!0})).forEach(l=>{let d=(0,rt.getObjectFields)(l);console.log(d),r.push({coin_type:(0,rt.fixCoinType)(d.name,!1),balance:d.value})}),r}catch(t){return J("FetchError",t,{[rt.DETAILS_KEYS.METHOD_NAME]:"getPartnerBalance",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i})}}async getPartner(i){try{let t=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0}});return ze(t)}catch(t){return J("FetchError",t,{[rt.DETAILS_KEYS.METHOD_NAME]:"getPartner",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i})}}updateRefFeeRatePayload(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{partner_id:e,ref_fee_rate:s}=i,{global_config_id:l,versioned_id:d}=(0,rt.getPackagerConfigs)(r);t=t||new ke.Transaction;let f=(0,rt.d)(s).mul(1e4);return f.gt(1e4)?J("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[rt.DETAILS_KEYS.METHOD_NAME]:"updateRefFeeRatePayload",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i}):(t.moveCall({target:`${r.published_at}::partner::update_ref_fee_rate`,arguments:[t.object(e),t.pure.u64(f.toNumber()),t.object(l),t.object(d)],typeArguments:[]}),t)}async claimRefFeePayload(i){let{partner_id:t,partner_cap_id:r,fee_coin_types:e}=i,{dlmm_pool:s}=this._sdk.sdkOptions,{versioned_id:l}=(0,rt.getPackagerConfigs)(s),d=new ke.Transaction,f=r;return f||(f=await this.getPartnerCapId(this._sdk.getSenderAddress(),t)),e.forEach(c=>{d.moveCall({target:`${s.published_at}::partner::claim_ref_fee`,arguments:[d.object(t),d.object(f),d.object(l)],typeArguments:[c]})}),d}updateTimeRangePayload(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{partner_id:e,start_time:s,end_time:l}=i,{global_config_id:d,versioned_id:f}=(0,rt.getPackagerConfigs)(r);t=t||new ke.Transaction;let c=s>1e12?Math.floor(s/1e3):s,p=l>1e12?Math.floor(l/1e3):l;return p<=c?J("InvalidParams",new Error("end_time must be greater than start_time"),{[rt.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i}):(t.moveCall({target:`${r.published_at}::partner::update_time_range`,arguments:[t.object(e),t.pure.u64(c),t.pure.u64(p),t.object(d),t.object(f),t.object(rt.CLOCK_ADDRESS)],typeArguments:[]}),t)}createPartnerPayload(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{name:r,ref_fee_rate:e,start_time:s,end_time:l,recipient:d}=i,{partners_id:f,global_config_id:c,versioned_id:p}=(0,rt.getPackagerConfigs)(t),_=new ke.Transaction,y=(0,rt.d)(e).mul(1e4);if(y.gt(1e4))return J("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[rt.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i});let P=s>1e12?Math.floor(s/1e3):s,B=l>1e12?Math.floor(l/1e3):l;return B<=P?J("InvalidParams",new Error("end_time must be greater than start_time"),{[rt.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[rt.DETAILS_KEYS.REQUEST_PARAMS]:i}):(_.moveCall({target:`${t.published_at}::partner::create_partner`,arguments:[_.object(f),_.pure.string(r),_.pure.u64(y.toNumber()),_.pure.u64(P),_.pure.u64(B),_.pure.address(d),_.object(c),_.object(p)],typeArguments:[]}),_)}};var ei=require("@mysten/sui/transactions"),ct=require("@cetusprotocol/common-sdk"),Hi=Pi(Ne()),ii=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async getRewardPeriodEmission(i,t,r){let e=await this._sdk.FullClient.getDynamicFieldsByPage(i),s=[],l=e.data.map(p=>p.objectId);l.length>0&&(await this._sdk.FullClient.batchGetObjects(l,{showContent:!0})).forEach(_=>{let y=(0,ct.getObjectFields)(_),P=ct.MathUtil.u128ToI128(new Hi.default(y.value.fields.value.fields.bits)).toString(),B=y.name,S=new Date(Number(B)*1e3).toLocaleString(),k={emissions_per_second:"0",emissions_per_day:"0",emissions_per:ct.MathUtil.fromX64(new Hi.default(P)).toString(),time:B,visualized_time:S};s.push(k)});let d=s.sort((p,_)=>Number(p.time)-Number(_.time)),f=[];f.push({emissions_per_second:t,emissions_per_day:(0,ct.d)(t).mul(3600*24).toString(),emissions_per:"0",time:r.toString(),visualized_time:new Date(r*1e3).toLocaleString()});let c=t;for(let p=0;p<d.length;p++){let _=d[p];if((0,ct.d)(_.time).lte(r))continue;c=(0,ct.d)(c).add((0,ct.d)(_.emissions_per)).toString();let y=(0,ct.d)(c).mul(3600*24).toString();(0,ct.d)(c).lt(0)?(_.emissions_per_second="0",_.emissions_per_day="0"):(_.emissions_per_second=c,_.emissions_per_day=y),f.push(_)}return f}addRewardPayload(i,t){t=t||new ei.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:l,reward_coin_type:d,reward_amount:f,start_time_seconds:c,end_time_seconds:p,coin_type_a:_,coin_type_b:y}=i,P=ct.CoinAssist.buildCoinWithBalance(BigInt(f),d,t);return t.pure.option("u64",c),t.moveCall({target:`${r.published_at}::pool::add_reward`,arguments:[t.object(l),P,t.pure.option("u64",c),t.pure.u64(p),t.object(e),t.object(s),t.object(ct.CLOCK_ADDRESS)],typeArguments:[_,y,d]}),t}initRewardPayload(i,t){t=t||new ei.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:l,reward_coin_types:d,coin_type_a:f,coin_type_b:c}=i;return d.forEach(p=>{t.moveCall({target:`${r.published_at}::pool::initialize_reward`,arguments:[t.object(l),t.object(e),t.object(s),t.object(ct.CLOCK_ADDRESS)],typeArguments:[f,c,p]})}),t}buildRewardAccessPayload(i,t){t=t||new ei.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:l,type:d,coin_type_a:f,coin_type_b:c}=i;return t.moveCall({target:`${r.published_at}::pool::${d==="to_public"?"make_reward_public":"make_reward_private"}`,arguments:[t.object(l),t.object(e),t.object(s)],typeArguments:[f,c]}),t}};var Tr=require("@mysten/sui/transactions"),ie=require("@cetusprotocol/common-sdk");var ri=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}buildRewardWhiteListPayload(i,t){t=t||new Tr.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ie.getPackagerConfigs)(r),{reward_coin_types:l,type:d}=i;return l.forEach(f=>{t.moveCall({target:`${r.published_at}::config::${d==="add"?"add_reward_whitelist":"remove_reward_whitelist"}`,arguments:[t.object(e),t.object(s)],typeArguments:[f]})}),t}async getBinStepConfigList(i){let t=await this._sdk.FullClient.getDynamicFieldsByPage(i),r=[],e=t.data.map(s=>s.objectId);return e.length>0&&(await this._sdk.FullClient.batchGetObjects(e,{showContent:!0})).forEach(l=>{let f={...(0,ie.getObjectFields)(l).value.fields};r.push(f)}),r}async getDlmmGlobalConfig(){let{dlmm_pool:i}=this._sdk.sdkOptions,{global_config_id:t}=(0,ie.getPackagerConfigs)(i);try{let r=await this._sdk.FullClient.getObject({id:t,options:{showContent:!0}}),e=(0,ie.getObjectFields)(r),s=e.reward_config.fields,l=s.reward_white_list?.fields?.contents?.map(f=>(0,ie.fixCoinType)(f.fields.key.fields.name,!1))||[];return{id:e.id.id,acl:{id:e.acl.fields.permissions.fields.id.id,size:e.acl.fields.permissions.fields.size},allowed_list:{id:e.allowed_list.fields.id.id,size:e.allowed_list.fields.size},denied_list:{id:e.denied_list.fields.id.id,size:e.denied_list.fields.size},bin_steps:{id:e.bin_steps.fields.id.id,size:e.bin_steps.fields.size},reward_white_list:l,blocked_position:{id:e.restriction.fields.blocked_position.fields.permissions.fields.id.id,size:e.restriction.fields.blocked_position.fields.permissions.fields.size},blocked_user:{id:e.restriction.fields.blocked_user.fields.permissions.fields.id.id,size:e.restriction.fields.blocked_user.fields.permissions.fields.size},min_reward_duration:Number(s.min_reward_duration),non_manager_initialize_reward_cap:Number(s.manager_reserved_reward_init_slots),reward_public:s.reward_public}}catch(r){return console.log("fetchGlobalConfig error: ",r),J("FetchError",r,{[ie.DETAILS_KEYS.METHOD_NAME]:"getBinStepConfigs",[ie.DETAILS_KEYS.REQUEST_PARAMS]:t})}}async fetchDlmmSdkConfigs(){let{dlmm_pool:i}=this._sdk.sdkOptions,t={registry_id:"",pools_id:"",global_config_id:"",versioned_id:"",admin_cap_id:"",partners_id:""},e=(await this._sdk.FullClient.getObject({id:i.package_id,options:{showContent:!0,showPreviousTransaction:!0}})).data?.previousTransaction;(await this._sdk.FullClient.getTransactionBlock({digest:e,options:{showEvents:!0}})).events?.forEach(f=>{let c=f.type,p=f.parsedJson;c.includes("versioned::InitEvent")&&(t.versioned_id=p.versioned),c.includes("partner::InitPartnerEvent")&&(t.partners_id=p.partners_id),c.includes("config::InitEvent")&&(t.global_config_id=p.config_id),c.includes("admin_cap::InitEvent")&&(t.admin_cap_id=p.admin_cap_id),c.includes("registry::RegistryEvent")&&(t.registry_id=p.pools_id)});let l=await this._sdk.FullClient.getObject({id:t.registry_id,options:{showContent:!0}}),d=(0,ie.getObjectFields)(l);return t.pools_id=d.pools.fields.id.id,t}};var Re=class n extends Cr.SdkWrapper{constructor(i){super(i),this._pool=new Ce(this),this._position=new Je(this),this._swap=new Ye(this),this._partner=new ti(this),this._reward=new ii(this),this._config=new ri(this)}get Pool(){return this._pool}get Position(){return this._position}get Swap(){return this._swap}get Partner(){return this._partner}get Reward(){return this._reward}get Config(){return this._config}static createSDK(i){let{env:t="mainnet"}=i;return t==="mainnet"?n.createCustomSDK({...Ai,...i}):n.createCustomSDK({...Zi,...i})}static createCustomSDK(i){return new n(i)}};var Qn=Re;0&&(module.exports={BASIS_POINT,BASIS_POINT_MAX,BIN_BOUND,BinUtils,CetusDlmmSDK,DEFAULT_MAX_WEIGHT,DEFAULT_MIN_WEIGHT,FEE_PRECISION,FeeUtils,IlmUtils,MAX_BIN_ID,MAX_BIN_PER_POSITION,MAX_FEE_RATE,MIN_BIN_ID,ONE,PoolModule,REWARD_PERIOD,REWARD_PERIOD_START_AT,SCALE_OFFSET,StrategyType,StrategyUtils,WeightUtils,buildPoolKey,dlmmMainnet,dlmmTestnet,generateRewardSchedule,getRouterModule,parseBinInfo,parseBinInfoList,parseCurrentRewardPeriodEmission,parseDlmmBasePool,parseDlmmPool,parseDlmmPosition,parseLiquidityShares,parsePartner,parsePoolTransactionInfo,parseRewardPeriodEmission,parseStrategyType,parsedDlmmPosFeeData,parsedDlmmPosRewardData,parsedSwapQuoteData,poolFilterEvenTypes,safeAmount,safeMulAmount});
2
2
  /*! Bundled license information:
3
3
 
4
4
  decimal.js/decimal.mjs: