@cetusprotocol/dlmm-sdk 0.0.9 → 0.1.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 Rr=Object.create;var De=Object.defineProperty;var Dr=Object.getOwnPropertyDescriptor;var Fr=Object.getOwnPropertyNames;var Nr=Object.getPrototypeOf,qr=Object.prototype.hasOwnProperty;var Lr=(n,i)=>()=>(i||n((i={exports:{}}).exports,i),i.exports),jr=(n,i)=>{for(var t in i)De(n,t,{get:i[t],enumerable:!0})},lr=(n,i,t,r)=>{if(i&&typeof i=="object"||typeof i=="function")for(let e of Fr(i))!qr.call(n,e)&&e!==t&&De(n,e,{get:()=>i[e],enumerable:!(r=Dr(i,e))||r.enumerable});return n};var Mi=(n,i,t)=>(t=n!=null?Rr(Nr(n)):{},lr(i||!n||!n.__esModule?De(t,"default",{value:n,enumerable:!0}):t,n)),Wr=n=>lr(De({},"__esModule",{value:!0}),n);var Ne=Lr((dr,Ai)=>{"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 l=function(){};l.prototype=o.prototype,v.prototype=new l,v.prototype.constructor=v}function e(v,o,l){if(e.isBN(v))return v;this.negative=0,this.words=null,this.length=0,this.red=null,v!==null&&((o==="le"||o==="be")&&(l=o,o=10),this._init(v||0,o||10,l||"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,l){return o.cmp(l)>0?o:l},e.min=function(o,l){return o.cmp(l)<0?o:l},e.prototype._init=function(o,l,h){if(typeof o=="number")return this._initNumber(o,l,h);if(typeof o=="object")return this._initArray(o,l,h);l==="hex"&&(l=16),t(l===(l|0)&&l>=2&&l<=36),o=o.toString().replace(/\s+/g,"");var m=0;o[0]==="-"&&(m++,this.negative=1),m<o.length&&(l===16?this._parseHex(o,m,h):(this._parseBase(o,l,m),h==="le"&&this._initArray(this.toArray(),l,h)))},e.prototype._initNumber=function(o,l,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(),l,h)},e.prototype._initArray=function(o,l,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 u(v,o){var l=v.charCodeAt(o);if(l>=48&&l<=57)return l-48;if(l>=65&&l<=70)return l-55;if(l>=97&&l<=102)return l-87;t(!1,"Invalid character in "+v)}function d(v,o,l){var h=u(v,l);return l-1>=o&&(h|=u(v,l-1)<<4),h}e.prototype._parseHex=function(o,l,h){this.length=Math.ceil((o.length-l)/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>=l;m-=2)A=d(o,l,m)<<b,this.words[M]|=A&67108863,b>=18?(b-=18,M+=1,this.words[M]|=A>>>26):b+=8;else{var _=o.length-l;for(m=_%2===0?l+1:l;m<o.length;m+=2)A=d(o,l,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,l,h){for(var m=0,b=0,M=Math.min(v.length,l),A=o;A<M;A++){var _=v.charCodeAt(A)-48;m*=h,_>=49?b=_-49+10:_>=17?b=_-17+10:b=_,t(_>=0&&b<h,"Invalid character"),m+=b}return m}e.prototype._parseBase=function(o,l,h){this.words=[0],this.length=1;for(var m=0,b=1;b<=67108863;b*=l)m++;m--,b=b/l|0;for(var M=o.length-h,A=M%m,_=Math.min(M,M-A)+h,a=0,w=h;w<_;w+=m)a=f(o,w,w+m,l),this.imuln(b),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a);if(A!==0){var D=1;for(a=f(o,w,o.length,l),w=0;w<A;w++)D*=l;this.imuln(D),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 l=0;l<this.length;l++)o.words[l]=this.words[l];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 g=["","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,l){o=o||10,l=l|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],_=((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=g[6-_.length]+_+h:h=_+h}for(b!==0&&(h=b.toString(16)+h);h.length%l!==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 D=this.clone();for(D.negative=0;!D.isZero();){var L=D.modrn(w).toString(o);D=D.idivn(w),D.isZero()?h=L+h:h=g[a-L.length]+L+h}for(this.isZero()&&(h="0"+h);h.length%l!==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,l){return this.toArrayLike(s,o,l)}),e.prototype.toArray=function(o,l){return this.toArrayLike(Array,o,l)};var S=function(o,l){return o.allocUnsafe?o.allocUnsafe(l):new o(l)};e.prototype.toArrayLike=function(o,l,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=S(o,b),A=l==="le"?"LE":"BE";return this["_toArrayLike"+A](M,m),M},e.prototype._toArrayLikeLE=function(o,l){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,l){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 l=o,h=0;return l>=4096&&(h+=13,l>>>=13),l>=64&&(h+=7,l>>>=7),l>=8&&(h+=4,l>>>=4),l>=2&&(h+=2,l>>>=2),h+l},e.prototype._zeroBits=function(o){if(o===0)return 26;var l=o,h=0;return(l&8191)===0&&(h+=13,l>>>=13),(l&127)===0&&(h+=7,l>>>=7),(l&15)===0&&(h+=4,l>>>=4),(l&3)===0&&(h+=2,l>>>=2),(l&1)===0&&h++,h},e.prototype.bitLength=function(){var o=this.words[this.length-1],l=this._countBits(o);return(this.length-1)*26+l};function B(v){for(var o=new Array(v.bitLength()),l=0;l<o.length;l++){var h=l/26|0,m=l%26;o[l]=v.words[h]>>>m&1}return o}e.prototype.zeroBits=function(){if(this.isZero())return 0;for(var o=0,l=0;l<this.length;l++){var h=this._zeroBits(this.words[l]);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 l=0;l<o.length;l++)this.words[l]=this.words[l]|o.words[l];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 l;this.length>o.length?l=o:l=this;for(var h=0;h<l.length;h++)this.words[h]=this.words[h]&o.words[h];return this.length=l.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 l,h;this.length>o.length?(l=this,h=o):(l=o,h=this);for(var m=0;m<h.length;m++)this.words[m]=l.words[m]^h.words[m];if(this!==l)for(;m<l.length;m++)this.words[m]=l.words[m];return this.length=l.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 l=Math.ceil(o/26)|0,h=o%26;this._expand(l),h>0&&l--;for(var m=0;m<l;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,l){t(typeof o=="number"&&o>=0);var h=o/26|0,m=o%26;return this._expand(h+1),l?this.words[h]=this.words[h]|1<<m:this.words[h]=this.words[h]&~(1<<m),this._strip()},e.prototype.iadd=function(o){var l;if(this.negative!==0&&o.negative===0)return this.negative=0,l=this.isub(o),this.negative^=1,this._normSign();if(this.negative===0&&o.negative!==0)return o.negative=0,l=this.isub(o),o.negative=1,l._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++)l=(h.words[M]|0)+(m.words[M]|0)+b,this.words[M]=l&67108863,b=l>>>26;for(;b!==0&&M<h.length;M++)l=(h.words[M]|0)+b,this.words[M]=l&67108863,b=l>>>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 l;return o.negative!==0&&this.negative===0?(o.negative=0,l=this.sub(o),o.negative^=1,l):o.negative===0&&this.negative!==0?(this.negative=0,l=o.sub(this),this.negative=1,l):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 l=this.iadd(o);return o.negative=1,l._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++)l=(m.words[A]|0)-(b.words[A]|0)+M,M=l>>26,this.words[A]=l&67108863;for(;M!==0&&A<m.length;A++)l=(m.words[A]|0)+M,M=l>>26,this.words[A]=l&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,l){l.negative=o.negative^v.negative;var h=v.length+o.length|0;l.length=h,h=h-1|0;var m=v.words[0]|0,b=o.words[0]|0,M=m*b,A=M&67108863,_=M/67108864|0;l.words[0]=A;for(var a=1;a<h;a++){for(var w=_>>>26,D=_&67108863,L=Math.min(a,o.length-1),W=Math.max(0,a-v.length+1);W<=L;W++){var V=a-W|0;m=v.words[V]|0,b=o.words[W]|0,M=m*b+D,w+=M/67108864|0,D=M&67108863}l.words[a]=D|0,_=w|0}return _!==0?l.words[a]=_|0:l.length--,l._strip()}var k=function(o,l,h){var m=o.words,b=l.words,M=h.words,A=0,_,a,w,D=m[0]|0,L=D&8191,W=D>>>13,V=m[1]|0,nt=V&8191,lt=V>>>13,ue=m[2]|0,ut=ue&8191,dt=ue>>>13,Qi=m[3]|0,ht=Qi&8191,pt=Qi>>>13,Ki=m[4]|0,mt=Ki&8191,_t=Ki>>>13,$i=m[5]|0,gt=$i&8191,bt=$i>>>13,zi=m[6]|0,wt=zi&8191,vt=zi>>>13,Gi=m[7]|0,yt=Gi&8191,Mt=Gi>>>13,Vi=m[8]|0,Pt=Vi&8191,At=Vi>>>13,Xi=m[9]|0,St=Xi&8191,Bt=Xi>>>13,Ji=b[0]|0,Et=Ji&8191,It=Ji>>>13,Yi=b[1]|0,Ot=Yi&8191,xt=Yi>>>13,tr=b[2]|0,Tt=tr&8191,Ct=tr>>>13,er=b[3]|0,kt=er&8191,Rt=er>>>13,ir=b[4]|0,Dt=ir&8191,Ft=ir>>>13,rr=b[5]|0,Nt=rr&8191,qt=rr>>>13,nr=b[6]|0,Lt=nr&8191,jt=nr>>>13,or=b[7]|0,Wt=or&8191,Zt=or>>>13,sr=b[8]|0,Ut=sr&8191,Ht=sr>>>13,ar=b[9]|0,Qt=ar&8191,Kt=ar>>>13;h.negative=o.negative^l.negative,h.length=19,_=Math.imul(L,Et),a=Math.imul(L,It),a=a+Math.imul(W,Et)|0,w=Math.imul(W,It);var ri=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ri>>>26)|0,ri&=67108863,_=Math.imul(nt,Et),a=Math.imul(nt,It),a=a+Math.imul(lt,Et)|0,w=Math.imul(lt,It),_=_+Math.imul(L,Ot)|0,a=a+Math.imul(L,xt)|0,a=a+Math.imul(W,Ot)|0,w=w+Math.imul(W,xt)|0;var ni=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ni>>>26)|0,ni&=67108863,_=Math.imul(ut,Et),a=Math.imul(ut,It),a=a+Math.imul(dt,Et)|0,w=Math.imul(dt,It),_=_+Math.imul(nt,Ot)|0,a=a+Math.imul(nt,xt)|0,a=a+Math.imul(lt,Ot)|0,w=w+Math.imul(lt,xt)|0,_=_+Math.imul(L,Tt)|0,a=a+Math.imul(L,Ct)|0,a=a+Math.imul(W,Tt)|0,w=w+Math.imul(W,Ct)|0;var oi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(oi>>>26)|0,oi&=67108863,_=Math.imul(ht,Et),a=Math.imul(ht,It),a=a+Math.imul(pt,Et)|0,w=Math.imul(pt,It),_=_+Math.imul(ut,Ot)|0,a=a+Math.imul(ut,xt)|0,a=a+Math.imul(dt,Ot)|0,w=w+Math.imul(dt,xt)|0,_=_+Math.imul(nt,Tt)|0,a=a+Math.imul(nt,Ct)|0,a=a+Math.imul(lt,Tt)|0,w=w+Math.imul(lt,Ct)|0,_=_+Math.imul(L,kt)|0,a=a+Math.imul(L,Rt)|0,a=a+Math.imul(W,kt)|0,w=w+Math.imul(W,Rt)|0;var si=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(si>>>26)|0,si&=67108863,_=Math.imul(mt,Et),a=Math.imul(mt,It),a=a+Math.imul(_t,Et)|0,w=Math.imul(_t,It),_=_+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,_=_+Math.imul(ut,Tt)|0,a=a+Math.imul(ut,Ct)|0,a=a+Math.imul(dt,Tt)|0,w=w+Math.imul(dt,Ct)|0,_=_+Math.imul(nt,kt)|0,a=a+Math.imul(nt,Rt)|0,a=a+Math.imul(lt,kt)|0,w=w+Math.imul(lt,Rt)|0,_=_+Math.imul(L,Dt)|0,a=a+Math.imul(L,Ft)|0,a=a+Math.imul(W,Dt)|0,w=w+Math.imul(W,Ft)|0;var ai=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ai>>>26)|0,ai&=67108863,_=Math.imul(gt,Et),a=Math.imul(gt,It),a=a+Math.imul(bt,Et)|0,w=Math.imul(bt,It),_=_+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,_=_+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,_=_+Math.imul(ut,kt)|0,a=a+Math.imul(ut,Rt)|0,a=a+Math.imul(dt,kt)|0,w=w+Math.imul(dt,Rt)|0,_=_+Math.imul(nt,Dt)|0,a=a+Math.imul(nt,Ft)|0,a=a+Math.imul(lt,Dt)|0,w=w+Math.imul(lt,Ft)|0,_=_+Math.imul(L,Nt)|0,a=a+Math.imul(L,qt)|0,a=a+Math.imul(W,Nt)|0,w=w+Math.imul(W,qt)|0;var li=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(li>>>26)|0,li&=67108863,_=Math.imul(wt,Et),a=Math.imul(wt,It),a=a+Math.imul(vt,Et)|0,w=Math.imul(vt,It),_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Dt)|0,a=a+Math.imul(ut,Ft)|0,a=a+Math.imul(dt,Dt)|0,w=w+Math.imul(dt,Ft)|0,_=_+Math.imul(nt,Nt)|0,a=a+Math.imul(nt,qt)|0,a=a+Math.imul(lt,Nt)|0,w=w+Math.imul(lt,qt)|0,_=_+Math.imul(L,Lt)|0,a=a+Math.imul(L,jt)|0,a=a+Math.imul(W,Lt)|0,w=w+Math.imul(W,jt)|0;var ui=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ui>>>26)|0,ui&=67108863,_=Math.imul(yt,Et),a=Math.imul(yt,It),a=a+Math.imul(Mt,Et)|0,w=Math.imul(Mt,It),_=_+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,_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Nt)|0,a=a+Math.imul(ut,qt)|0,a=a+Math.imul(dt,Nt)|0,w=w+Math.imul(dt,qt)|0,_=_+Math.imul(nt,Lt)|0,a=a+Math.imul(nt,jt)|0,a=a+Math.imul(lt,Lt)|0,w=w+Math.imul(lt,jt)|0,_=_+Math.imul(L,Wt)|0,a=a+Math.imul(L,Zt)|0,a=a+Math.imul(W,Wt)|0,w=w+Math.imul(W,Zt)|0;var di=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(di>>>26)|0,di&=67108863,_=Math.imul(Pt,Et),a=Math.imul(Pt,It),a=a+Math.imul(At,Et)|0,w=Math.imul(At,It),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Lt)|0,a=a+Math.imul(ut,jt)|0,a=a+Math.imul(dt,Lt)|0,w=w+Math.imul(dt,jt)|0,_=_+Math.imul(nt,Wt)|0,a=a+Math.imul(nt,Zt)|0,a=a+Math.imul(lt,Wt)|0,w=w+Math.imul(lt,Zt)|0,_=_+Math.imul(L,Ut)|0,a=a+Math.imul(L,Ht)|0,a=a+Math.imul(W,Ut)|0,w=w+Math.imul(W,Ht)|0;var fi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(fi>>>26)|0,fi&=67108863,_=Math.imul(St,Et),a=Math.imul(St,It),a=a+Math.imul(Bt,Et)|0,w=Math.imul(Bt,It),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Wt)|0,a=a+Math.imul(ut,Zt)|0,a=a+Math.imul(dt,Wt)|0,w=w+Math.imul(dt,Zt)|0,_=_+Math.imul(nt,Ut)|0,a=a+Math.imul(nt,Ht)|0,a=a+Math.imul(lt,Ut)|0,w=w+Math.imul(lt,Ht)|0,_=_+Math.imul(L,Qt)|0,a=a+Math.imul(L,Kt)|0,a=a+Math.imul(W,Qt)|0,w=w+Math.imul(W,Kt)|0;var ci=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ci>>>26)|0,ci&=67108863,_=Math.imul(St,Ot),a=Math.imul(St,xt),a=a+Math.imul(Bt,Ot)|0,w=Math.imul(Bt,xt),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Ut)|0,a=a+Math.imul(ut,Ht)|0,a=a+Math.imul(dt,Ut)|0,w=w+Math.imul(dt,Ht)|0,_=_+Math.imul(nt,Qt)|0,a=a+Math.imul(nt,Kt)|0,a=a+Math.imul(lt,Qt)|0,w=w+Math.imul(lt,Kt)|0;var hi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(hi>>>26)|0,hi&=67108863,_=Math.imul(St,Tt),a=Math.imul(St,Ct),a=a+Math.imul(Bt,Tt)|0,w=Math.imul(Bt,Ct),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Qt)|0,a=a+Math.imul(ut,Kt)|0,a=a+Math.imul(dt,Qt)|0,w=w+Math.imul(dt,Kt)|0;var pi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(pi>>>26)|0,pi&=67108863,_=Math.imul(St,kt),a=Math.imul(St,Rt),a=a+Math.imul(Bt,kt)|0,w=Math.imul(Bt,Rt),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+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 mi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(mi>>>26)|0,mi&=67108863,_=Math.imul(St,Dt),a=Math.imul(St,Ft),a=a+Math.imul(Bt,Dt)|0,w=Math.imul(Bt,Ft),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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 _i=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(_i>>>26)|0,_i&=67108863,_=Math.imul(St,Nt),a=Math.imul(St,qt),a=a+Math.imul(Bt,Nt)|0,w=Math.imul(Bt,qt),_=_+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,_=_+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,_=_+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,_=_+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 gi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(gi>>>26)|0,gi&=67108863,_=Math.imul(St,Lt),a=Math.imul(St,jt),a=a+Math.imul(Bt,Lt)|0,w=Math.imul(Bt,jt),_=_+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,_=_+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,_=_+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 bi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(bi>>>26)|0,bi&=67108863,_=Math.imul(St,Wt),a=Math.imul(St,Zt),a=a+Math.imul(Bt,Wt)|0,w=Math.imul(Bt,Zt),_=_+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,_=_+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 wi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(wi>>>26)|0,wi&=67108863,_=Math.imul(St,Ut),a=Math.imul(St,Ht),a=a+Math.imul(Bt,Ut)|0,w=Math.imul(Bt,Ht),_=_+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 vi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(vi>>>26)|0,vi&=67108863,_=Math.imul(St,Qt),a=Math.imul(St,Kt),a=a+Math.imul(Bt,Qt)|0,w=Math.imul(Bt,Kt);var yi=(A+_|0)+((a&8191)<<13)|0;return A=(w+(a>>>13)|0)+(yi>>>26)|0,yi&=67108863,M[0]=ri,M[1]=ni,M[2]=oi,M[3]=si,M[4]=ai,M[5]=li,M[6]=ui,M[7]=di,M[8]=fi,M[9]=ci,M[10]=hi,M[11]=pi,M[12]=mi,M[13]=_i,M[14]=gi,M[15]=bi,M[16]=wi,M[17]=vi,M[18]=yi,A!==0&&(M[19]=A,h.length++),h};Math.imul||(k=C);function R(v,o,l){l.negative=o.negative^v.negative,l.length=v.length+o.length;for(var h=0,m=0,b=0;b<l.length-1;b++){var M=m;m=0;for(var A=h&67108863,_=Math.min(b,o.length-1),a=Math.max(0,b-v.length+1);a<=_;a++){var w=b-a,D=v.words[w]|0,L=o.words[a]|0,W=D*L,V=W&67108863;M=M+(W/67108864|0)|0,V=V+A|0,A=V&67108863,M=M+(V>>>26)|0,m+=M>>>26,M&=67108863}l.words[b]=A,h=M,M=m}return h!==0?l.words[b]=h:l.length--,l._strip()}function I(v,o,l){return R(v,o,l)}e.prototype.mulTo=function(o,l){var h,m=this.length+o.length;return this.length===10&&o.length===10?h=k(this,o,l):m<63?h=C(this,o,l):m<1024?h=R(this,o,l):h=I(this,o,l),h};function x(v,o){this.x=v,this.y=o}x.prototype.makeRBT=function(o){for(var l=new Array(o),h=e.prototype._countBits(o)-1,m=0;m<o;m++)l[m]=this.revBin(m,h,o);return l},x.prototype.revBin=function(o,l,h){if(o===0||o===h-1)return o;for(var m=0,b=0;b<l;b++)m|=(o&1)<<l-b-1,o>>=1;return m},x.prototype.permute=function(o,l,h,m,b,M){for(var A=0;A<M;A++)m[A]=l[o[A]],b[A]=h[o[A]]},x.prototype.transform=function(o,l,h,m,b,M){this.permute(M,o,l,h,m,b);for(var A=1;A<b;A<<=1)for(var _=A<<1,a=Math.cos(2*Math.PI/_),w=Math.sin(2*Math.PI/_),D=0;D<b;D+=_)for(var L=a,W=w,V=0;V<A;V++){var nt=h[D+V],lt=m[D+V],ue=h[D+V+A],ut=m[D+V+A],dt=L*ue-W*ut;ut=L*ut+W*ue,ue=dt,h[D+V]=nt+ue,m[D+V]=lt+ut,h[D+V+A]=nt-ue,m[D+V+A]=lt-ut,V!==_&&(dt=a*L-w*W,W=a*W+w*L,L=dt)}},x.prototype.guessLen13b=function(o,l){var h=Math.max(l,o)|1,m=h&1,b=0;for(h=h/2|0;h;h=h>>>1)b++;return 1<<b+1+m},x.prototype.conjugate=function(o,l,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=l[m],l[m]=-l[h-m-1],l[h-m-1]=-b}},x.prototype.normalize13b=function(o,l){for(var h=0,m=0;m<l/2;m++){var b=Math.round(o[2*m+1]/l)*8192+Math.round(o[2*m]/l)+h;o[m]=b&67108863,b<67108864?h=0:h=b/67108864|0}return o},x.prototype.convert13b=function(o,l,h,m){for(var b=0,M=0;M<l;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*l;M<m;++M)h[M]=0;t(b===0),t((b&-8192)===0)},x.prototype.stub=function(o){for(var l=new Array(o),h=0;h<o;h++)l[h]=0;return l},x.prototype.mulp=function(o,l,h){var m=2*this.guessLen13b(o.length,l.length),b=this.makeRBT(m),M=this.stub(m),A=new Array(m),_=new Array(m),a=new Array(m),w=new Array(m),D=new Array(m),L=new Array(m),W=h.words;W.length=m,this.convert13b(o.words,o.length,A,m),this.convert13b(l.words,l.length,w,m),this.transform(A,M,_,a,m,b),this.transform(w,M,D,L,m,b);for(var V=0;V<m;V++){var nt=_[V]*D[V]-a[V]*L[V];a[V]=_[V]*L[V]+a[V]*D[V],_[V]=nt}return this.conjugate(_,a,m),this.transform(_,a,W,M,m,b),this.conjugate(W,M,m),this.normalize13b(W,m),h.negative=o.negative^l.negative,h.length=o.length+l.length,h._strip()},e.prototype.mul=function(o){var l=new e(null);return l.words=new Array(this.length+o.length),this.mulTo(o,l)},e.prototype.mulf=function(o){var l=new e(null);return l.words=new Array(this.length+o.length),I(this,o,l)},e.prototype.imul=function(o){return this.clone().mulTo(o,this)},e.prototype.imuln=function(o){var l=o<0;l&&(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,l?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 l=B(o);if(l.length===0)return new e(1);for(var h=this,m=0;m<l.length&&l[m]===0;m++,h=h.sqr());if(++m<l.length)for(var b=h.sqr();m<l.length;m++,b=b.sqr())l[m]!==0&&(h=h.mul(b));return h},e.prototype.iushln=function(o){t(typeof o=="number"&&o>=0);var l=o%26,h=(o-l)/26,m=67108863>>>26-l<<26-l,b;if(l!==0){var M=0;for(b=0;b<this.length;b++){var A=this.words[b]&m,_=(this.words[b]|0)-A<<l;this.words[b]=_|M,M=A>>>26-l}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,l,h){t(typeof o=="number"&&o>=0);var m;l?m=(l-l%26)/26:m=0;var b=o%26,M=Math.min((o-b)/26,this.length),A=67108863^67108863>>>b<<b,_=h;if(m-=M,m=Math.max(0,m),_){for(var a=0;a<M;a++)_.words[a]=this.words[a];_.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 D=this.words[a]|0;this.words[a]=w<<26-b|D>>>b,w=D&A}return _&&w!==0&&(_.words[_.length++]=w),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},e.prototype.ishrn=function(o,l,h){return t(this.negative===0),this.iushrn(o,l,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 l=o%26,h=(o-l)/26,m=1<<l;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 l=o%26,h=(o-l)/26;if(t(this.negative===0,"imaskn works only with positive numbers"),this.length<=h)return this;if(l!==0&&h++,this.length=Math.min(h,this.length),l!==0){var m=67108863^67108863>>>l<<l;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 l=0;l<this.length&&this.words[l]>=67108864;l++)this.words[l]-=67108864,l===this.length-1?this.words[l+1]=1:this.words[l+1]++;return this.length=Math.max(this.length,l+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 l=0;l<this.length&&this.words[l]<0;l++)this.words[l]+=67108864,this.words[l+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,l,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 _=(o.words[b]|0)*l;M-=_&67108863,A=(M>>26)-(_/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,l){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 _=m.length-b.length,a;if(l!=="mod"){a=new e(null),a.length=_+1,a.words=new Array(a.length);for(var w=0;w<a.length;w++)a.words[w]=0}var D=m.clone()._ishlnsubmul(b,1,_);D.negative===0&&(m=D,a&&(a.words[_]=1));for(var L=_-1;L>=0;L--){var W=(m.words[b.length+L]|0)*67108864+(m.words[b.length+L-1]|0);for(W=Math.min(W/M|0,67108863),m._ishlnsubmul(b,W,L);m.negative!==0;)W--,m.negative=0,m._ishlnsubmul(b,1,L),m.isZero()||(m.negative^=1);a&&(a.words[L]=W)}return a&&a._strip(),m._strip(),l!=="div"&&h!==0&&m.iushrn(h),{div:a||null,mod:m}},e.prototype.divmod=function(o,l,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,l),l!=="mod"&&(m=M.div.neg()),l!=="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(),l),l!=="mod"&&(m=M.div.neg()),{div:m,mod:M.mod}):(this.negative&o.negative)!==0?(M=this.neg().divmod(o.neg(),l),l!=="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?l==="div"?{div:this.divn(o.words[0]),mod:null}:l==="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,l)},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 l=this.divmod(o);if(l.mod.isZero())return l.div;var h=l.div.negative!==0?l.mod.isub(o):l.mod,m=o.ushrn(1),b=o.andln(1),M=h.cmp(m);return M<0||b===1&&M===0?l.div:l.div.negative!==0?l.div.isubn(1):l.div.iaddn(1)},e.prototype.modrn=function(o){var l=o<0;l&&(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 l?-m:m},e.prototype.modn=function(o){return this.modrn(o)},e.prototype.idivn=function(o){var l=o<0;l&&(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(),l?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 l=this,h=o.clone();l.negative!==0?l=l.umod(o):l=l.clone();for(var m=new e(1),b=new e(0),M=new e(0),A=new e(1),_=0;l.isEven()&&h.isEven();)l.iushrn(1),h.iushrn(1),++_;for(var a=h.clone(),w=l.clone();!l.isZero();){for(var D=0,L=1;(l.words[0]&L)===0&&D<26;++D,L<<=1);if(D>0)for(l.iushrn(D);D-- >0;)(m.isOdd()||b.isOdd())&&(m.iadd(a),b.isub(w)),m.iushrn(1),b.iushrn(1);for(var W=0,V=1;(h.words[0]&V)===0&&W<26;++W,V<<=1);if(W>0)for(h.iushrn(W);W-- >0;)(M.isOdd()||A.isOdd())&&(M.iadd(a),A.isub(w)),M.iushrn(1),A.iushrn(1);l.cmp(h)>=0?(l.isub(h),m.isub(M),b.isub(A)):(h.isub(l),M.isub(m),A.isub(b))}return{a:M,b:A,gcd:h.iushln(_)}},e.prototype._invmp=function(o){t(o.negative===0),t(!o.isZero());var l=this,h=o.clone();l.negative!==0?l=l.umod(o):l=l.clone();for(var m=new e(1),b=new e(0),M=h.clone();l.cmpn(1)>0&&h.cmpn(1)>0;){for(var A=0,_=1;(l.words[0]&_)===0&&A<26;++A,_<<=1);if(A>0)for(l.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);l.cmp(h)>=0?(l.isub(h),m.isub(b)):(h.isub(l),b.isub(m))}var D;return l.cmpn(1)===0?D=m:D=b,D.cmpn(0)<0&&D.iadd(o),D},e.prototype.gcd=function(o){if(this.isZero())return o.abs();if(o.isZero())return this.abs();var l=this.clone(),h=o.clone();l.negative=0,h.negative=0;for(var m=0;l.isEven()&&h.isEven();m++)l.iushrn(1),h.iushrn(1);do{for(;l.isEven();)l.iushrn(1);for(;h.isEven();)h.iushrn(1);var b=l.cmp(h);if(b<0){var M=l;l=h,h=M}else if(b===0||h.cmpn(1)===0)break;l.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 l=o%26,h=(o-l)/26,m=1<<l;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 l=o<0;if(this.negative!==0&&!l)return-1;if(this.negative===0&&l)return 1;this._strip();var h;if(this.length>1)h=1;else{l&&(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 l=this.ucmp(o);return this.negative!==0?-l|0:l},e.prototype.ucmp=function(o){if(this.length>o.length)return 1;if(this.length<o.length)return-1;for(var l=0,h=this.length-1;h>=0;h--){var m=this.words[h]|0,b=o.words[h]|0;if(m!==b){m<b?l=-1:m>b&&(l=1);break}}return l},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 K(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 Z(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()}Z.prototype._tmp=function(){var o=new e(null);return o.words=new Array(Math.ceil(this.n/13)),o},Z.prototype.ireduce=function(o){var l=o,h;do this.split(l,this.tmp),l=this.imulK(l),l=l.iadd(this.tmp),h=l.bitLength();while(h>this.n);var m=h<this.n?-1:l.ucmp(this.p);return m===0?(l.words[0]=0,l.length=1):m>0?l.isub(this.p):l.strip!==void 0?l.strip():l._strip(),l},Z.prototype.split=function(o,l){o.iushrn(this.n,0,l)},Z.prototype.imulK=function(o){return o.imul(this.k)};function J(){Z.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}r(J,Z),J.prototype.split=function(o,l){for(var h=4194303,m=Math.min(o.length,9),b=0;b<m;b++)l.words[b]=o.words[b];if(l.length=m,o.length<=9){o.words[0]=0,o.length=1;return}var M=o.words[9];for(l.words[l.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},J.prototype.imulK=function(o){o.words[o.length]=0,o.words[o.length+1]=0,o.length+=2;for(var l=0,h=0;h<o.length;h++){var m=o.words[h]|0;l+=m*977,o.words[h]=l&67108863,l=m*64+(l/67108864|0)}return o.words[o.length-1]===0&&(o.length--,o.words[o.length-1]===0&&o.length--),o};function it(){Z.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}r(it,Z);function tt(){Z.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}r(tt,Z);function ot(){Z.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}r(ot,Z),ot.prototype.imulK=function(o){for(var l=0,h=0;h<o.length;h++){var m=(o.words[h]|0)*19+l,b=m&67108863;m>>>=26,o.words[h]=b,l=m}return l!==0&&(o.words[o.length++]=l),o},e._prime=function(o){if(z[o])return z[o];var l;if(o==="k256")l=new J;else if(o==="p224")l=new it;else if(o==="p192")l=new tt;else if(o==="p25519")l=new ot;else throw new Error("Unknown prime "+o);return z[o]=l,l};function K(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}K.prototype._verify1=function(o){t(o.negative===0,"red works only with positives"),t(o.red,"red works only with red numbers")},K.prototype._verify2=function(o,l){t((o.negative|l.negative)===0,"red works only with positives"),t(o.red&&o.red===l.red,"red works only with red numbers")},K.prototype.imod=function(o){return this.prime?this.prime.ireduce(o)._forceRed(this):(c(o,o.umod(this.m)._forceRed(this)),o)},K.prototype.neg=function(o){return o.isZero()?o.clone():this.m.sub(o)._forceRed(this)},K.prototype.add=function(o,l){this._verify2(o,l);var h=o.add(l);return h.cmp(this.m)>=0&&h.isub(this.m),h._forceRed(this)},K.prototype.iadd=function(o,l){this._verify2(o,l);var h=o.iadd(l);return h.cmp(this.m)>=0&&h.isub(this.m),h},K.prototype.sub=function(o,l){this._verify2(o,l);var h=o.sub(l);return h.cmpn(0)<0&&h.iadd(this.m),h._forceRed(this)},K.prototype.isub=function(o,l){this._verify2(o,l);var h=o.isub(l);return h.cmpn(0)<0&&h.iadd(this.m),h},K.prototype.shl=function(o,l){return this._verify1(o),this.imod(o.ushln(l))},K.prototype.imul=function(o,l){return this._verify2(o,l),this.imod(o.imul(l))},K.prototype.mul=function(o,l){return this._verify2(o,l),this.imod(o.mul(l))},K.prototype.isqr=function(o){return this.imul(o,o.clone())},K.prototype.sqr=function(o){return this.mul(o,o)},K.prototype.sqrt=function(o){if(o.isZero())return o.clone();var l=this.m.andln(3);if(t(l%2===1),l===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(),_=this.m.subn(1).iushrn(1),a=this.m.bitLength();for(a=new e(2*a*a).toRed(this);this.pow(a,_).cmp(A)!==0;)a.redIAdd(A);for(var w=this.pow(a,m),D=this.pow(o,m.addn(1).iushrn(1)),L=this.pow(o,m),W=b;L.cmp(M)!==0;){for(var V=L,nt=0;V.cmp(M)!==0;nt++)V=V.redSqr();t(nt<W);var lt=this.pow(w,new e(1).iushln(W-nt-1));D=D.redMul(lt),w=lt.redSqr(),L=L.redMul(w),W=nt}return D},K.prototype.invm=function(o){var l=o._invmp(this.m);return l.negative!==0?(l.negative=0,this.imod(l).redNeg()):this.imod(l)},K.prototype.pow=function(o,l){if(l.isZero())return new e(1).toRed(this);if(l.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,_=0,a=l.bitLength()%26;for(a===0&&(a=26),b=l.length-1;b>=0;b--){for(var w=l.words[b],D=a-1;D>=0;D--){var L=w>>D&1;if(M!==m[0]&&(M=this.sqr(M)),L===0&&A===0){_=0;continue}A<<=1,A|=L,_++,!(_!==h&&(b!==0||D!==0))&&(M=this.mul(M,m[A]),_=0,A=0)}a=26}return M},K.prototype.convertTo=function(o){var l=o.umod(this.m);return l===o?l.clone():l},K.prototype.convertFrom=function(o){var l=o.clone();return l.red=null,l},e.mont=function(o){return new st(o)};function st(v){K.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(st,K),st.prototype.convertTo=function(o){return this.imod(o.ushln(this.shift))},st.prototype.convertFrom=function(o){var l=this.imod(o.mul(this.rinv));return l.red=null,l},st.prototype.imul=function(o,l){if(o.isZero()||l.isZero())return o.words[0]=0,o.length=1,o;var h=o.imul(l),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)},st.prototype.mul=function(o,l){if(o.isZero()||l.isZero())return new e(0)._forceRed(this);var h=o.mul(l),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)},st.prototype.invm=function(o){var l=this.imod(o._invmp(this.m).mul(this.r2));return l._forceRed(this)}})(typeof Ai>"u"||Ai,dr)});var $n={};jr($n,{BASIS_POINT:()=>te,BASIS_POINT_MAX:()=>ve,BIN_BOUND:()=>ge,BinUtils:()=>F,CetusDlmmSDK:()=>Re,DEFAULT_MAX_WEIGHT:()=>ye,DEFAULT_MIN_WEIGHT:()=>_e,FEE_PRECISION:()=>xi,FeeUtils:()=>Oe,IlmUtils:()=>ji,MAX_BIN_ID:()=>Nn,MAX_BIN_PER_POSITION:()=>le,MAX_FEE_RATE:()=>Ke,MIN_BIN_ID:()=>Fn,ONE:()=>$e,PoolModule:()=>Ce,REWARD_PERIOD:()=>qn,REWARD_PERIOD_START_AT:()=>Ln,SCALE_OFFSET:()=>N,StrategyType:()=>Ee,StrategyUtils:()=>Ae,WeightUtils:()=>ee,buildPoolKey:()=>Li,default:()=>Kn,dlmmMainnet:()=>Pi,dlmmTestnet:()=>Zi,generateRewardSchedule:()=>Un,getRouterModule:()=>qi,parseBinInfo:()=>ki,parseBinInfoList:()=>Ci,parseCurrentRewardPeriodEmission:()=>Qn,parseDlmmBasePool:()=>Ti,parseDlmmPool:()=>Ie,parseDlmmPosition:()=>Ge,parseLiquidityShares:()=>Wn,parsePartner:()=>ze,parsePoolTransactionInfo:()=>Ni,parseRewardPeriodEmission:()=>Hn,parseStrategyType:()=>Zn,parsedDlmmPosFeeData:()=>Ri,parsedDlmmPosRewardData:()=>Di,parsedSwapQuoteData:()=>Fi,poolFilterEvenTypes:()=>Ir,safeAmount:()=>Pe,safeMulAmount:()=>ne});module.exports=Wr($n);var kr=require("@cetusprotocol/common-sdk");var Fe=require("@cetusprotocol/common-sdk"),Pi={env:"mainnet",full_rpc_url:Fe.FullRpcUrlMainnet,graph_rpc_url:Fe.GraphRpcUrlMainnet,dlmm_pool:{package_id:"0x5664f9d3fd82c84023870cfbda8ea84e14c8dd56ce557ad2116e0668581a682b",published_at:"0xd2e4a295d7c4ed9cbf7d2adb69bbfa575fafe4e3a3b72c76b73807b39d059f7e",version:2,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:"0x8d389fa25cb08ebc5e520bc520ed394eed9e62b56b7868acb398bf298b8a76f3",version:1}};var xe=require("@mysten/sui/transactions"),et=require("@cetusprotocol/common-sdk");var ur=require("@cetusprotocol/common-sdk");var me=class extends ur.BaseError{constructor(i,t,r){super(i,t||"UnknownError",r)}static isDlmmErrorCode(i,t){return this.isErrorCode(i,t)}},X=(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=Mi(Ne());var be=9e15,he=1e9,Si="0123456789abcdef",je="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",We="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Bi={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-be,maxE:be,crypto:!1},pr,ae,$=!0,Ue="[DecimalError] ",ce=Ue+"Invalid argument: ",mr=Ue+"Precision limit exceeded",_r=Ue+"crypto unavailable",gr="[object Decimal]",Vt=Math.floor,$t=Math.pow,Zr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Ur=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Hr=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,br=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,re=1e7,Q=7,Qr=9007199254740991,Kr=je.length-1,Ei=We.length-1,E={toStringTag:gr};E.absoluteValue=E.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),j(n)};E.ceil=function(){return j(new this.constructor(this),this.e+1,2)};E.clampedTo=E.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)};E.comparedTo=E.cmp=function(n){var i,t,r,e,s=this,u=s.d,d=(n=new s.constructor(n)).d,f=s.s,c=n.s;if(!u||!d)return!f||!c?NaN:f!==c?f:u===d?0:!u^f<0?1:-1;if(!u[0]||!d[0])return u[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=u.length,e=d.length,i=0,t=r<e?r:e;i<t;++i)if(u[i]!==d[i])return u[i]>d[i]^f<0?1:-1;return r===e?0:r>e^f<0?1:-1};E.cosine=E.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())+Q,r.rounding=1,t=$r(r,Pr(r,t)),r.precision=n,r.rounding=i,j(ae==2||ae==3?t.neg():t,n,i,!0)):new r(1):new r(NaN)};E.cubeRoot=E.cbrt=function(){var n,i,t,r,e,s,u,d,f,c,p=this,g=p.constructor;if(!p.isFinite()||p.isZero())return new g(p);for($=!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 g(t),r.s=p.s):r=new g(s.toString()),u=(n=g.precision)+3;;)if(d=r,f=d.times(d).times(d),c=f.plus(p),r=at(c.plus(p).times(d),c.plus(f),u+2,1),zt(d.d).slice(0,u)===(t=zt(r.d)).slice(0,u))if(t=t.slice(u-3,u+1),t=="9999"||!e&&t=="4999"){if(!e&&(j(d,n+1,0),d.times(d).times(d).eq(p))){r=d;break}u+=4,e=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(j(r,n+1,1),i=!r.times(r).times(r).eq(p));break}return $=!0,j(r,n,g.rounding,i)};E.decimalPlaces=E.dp=function(){var n,i=this.d,t=NaN;if(i){if(n=i.length-1,t=(n-Vt(this.e/Q))*Q,n=i[n],n)for(;n%10==0;n/=10)t--;t<0&&(t=0)}return t};E.dividedBy=E.div=function(n){return at(this,new this.constructor(n))};E.dividedToIntegerBy=E.divToInt=function(n){var i=this,t=i.constructor;return j(at(i,new t(n),0,1,1),t.precision,t.rounding)};E.equals=E.eq=function(n){return this.cmp(n)===0};E.floor=function(){return j(new this.constructor(this),this.e+1,3)};E.greaterThan=E.gt=function(n){return this.cmp(n)>0};E.greaterThanOrEqualTo=E.gte=function(n){var i=this.cmp(n);return i==1||i===0};E.hyperbolicCosine=E.cosh=function(){var n,i,t,r,e,s=this,u=s.constructor,d=new u(1);if(!s.isFinite())return new u(s.s?1/0:NaN);if(s.isZero())return d;t=u.precision,r=u.rounding,u.precision=t+Math.max(s.e,s.sd())+4,u.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(u,1,s.times(i),new u(1),!0);for(var f,c=n,p=new u(8);c--;)f=s.times(s),s=d.minus(f.times(p.minus(f.times(p))));return j(s,u.precision=t,u.rounding=r,!0)};E.hyperbolicSine=E.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 u,d=new s(5),f=new s(16),c=new s(20);n--;)u=e.times(e),e=e.times(d.plus(u.times(f.times(u).plus(c))))}return s.precision=i,s.rounding=t,j(e,i,t,!0)};E.hyperbolicTangent=E.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,at(t.sinh(),t.cosh(),r.precision=n,r.rounding=i)):new r(t.s)};E.inverseCosine=E.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))};E.inverseHyperbolicCosine=E.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,$=!1,t=t.times(t).minus(1).sqrt().plus(t),$=!0,r.precision=n,r.rounding=i,t.ln()):new r(t)};E.inverseHyperbolicSine=E.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,$=!1,t=t.times(t).plus(1).sqrt().plus(t),$=!0,r.precision=n,r.rounding=i,t.ln())};E.inverseHyperbolicTangent=E.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?j(new s(e),n,i,!0):(s.precision=t=r-e.e,e=at(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)};E.inverseSine=E.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)))};E.inverseTangent=E.atan=function(){var n,i,t,r,e,s,u,d,f,c=this,p=c.constructor,g=p.precision,y=p.rounding;if(c.isFinite()){if(c.isZero())return new p(c);if(c.abs().eq(1)&&g+4<=Ei)return u=oe(p,g+4,y).times(.25),u.s=c.s,u}else{if(!c.s)return new p(NaN);if(g+4<=Ei)return u=oe(p,g+4,y).times(.5),u.s=c.s,u}for(p.precision=d=g+10,p.rounding=1,t=Math.min(28,d/Q+2|0),n=t;n;--n)c=c.div(c.times(c).plus(1).sqrt().plus(1));for($=!1,i=Math.ceil(d/Q),r=1,f=c.times(c),u=new p(c),e=c;n!==-1;)if(e=e.times(f),s=u.minus(e.div(r+=2)),e=e.times(f),u=s.plus(e.div(r+=2)),u.d[i]!==void 0)for(n=i;u.d[n]===s.d[n]&&n--;);return t&&(u=u.times(2<<t-1)),$=!0,j(u,p.precision=g,p.rounding=y,!0)};E.isFinite=function(){return!!this.d};E.isInteger=E.isInt=function(){return!!this.d&&Vt(this.e/Q)>this.d.length-2};E.isNaN=function(){return!this.s};E.isNegative=E.isNeg=function(){return this.s<0};E.isPositive=E.isPos=function(){return this.s>0};E.isZero=function(){return!!this.d&&this.d[0]===0};E.lessThan=E.lt=function(n){return this.cmp(n)<0};E.lessThanOrEqualTo=E.lte=function(n){return this.cmp(n)<1};E.logarithm=E.log=function(n){var i,t,r,e,s,u,d,f,c=this,p=c.constructor,g=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($=!1,d=g+P,u=fe(c,d),r=i?Ze(p,d+10):fe(n,d),f=at(u,r,d,1),Se(f.d,e=g,y))do if(d+=10,u=fe(c,d),r=i?Ze(p,d+10):fe(n,d),f=at(u,r,d,1),!s){+zt(f.d).slice(e+1,e+15)+1==1e14&&(f=j(f,g+1,0));break}while(Se(f.d,e+=10,y));return $=!0,j(f,g,y)};E.minus=E.sub=function(n){var i,t,r,e,s,u,d,f,c,p,g,y,P=this,S=P.constructor;if(n=new S(n),!P.d||!n.d)return!P.s||!n.s?n=new S(NaN):P.d?n.s=-n.s:n=new S(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=S.precision,f=S.rounding,!c[0]||!y[0]){if(y[0])n.s=-n.s;else if(c[0])n=new S(P);else return new S(f===3?-0:0);return $?j(n,d,f):n}if(t=Vt(n.e/Q),p=Vt(P.e/Q),c=c.slice(),s=p-t,s){for(g=s<0,g?(i=c,s=-s,u=y.length):(i=y,t=p,u=c.length),r=Math.max(Math.ceil(d/Q),u)+2,s>r&&(s=r,i.length=1),i.reverse(),r=s;r--;)i.push(0);i.reverse()}else{for(r=c.length,u=y.length,g=r<u,g&&(u=r),r=0;r<u;r++)if(c[r]!=y[r]){g=c[r]<y[r];break}s=0}for(g&&(i=c,c=y,y=i,n.s=-n.s),u=c.length,r=y.length-u;r>0;--r)c[u++]=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[--u]===0;)c.pop();for(;c[0]===0;c.shift())--t;return c[0]?(n.d=c,n.e=He(c,t),$?j(n,d,f):n):new S(f===3?-0:0)};E.modulo=E.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]?j(new r(t),r.precision,r.rounding):($=!1,r.modulo==9?(i=at(t,n.abs(),0,3,1),i.s*=n.s):i=at(t,n,0,r.modulo,1),i=i.times(n),$=!0,t.minus(i))};E.naturalExponential=E.exp=function(){return Ii(this)};E.naturalLogarithm=E.ln=function(){return fe(this)};E.negated=E.neg=function(){var n=new this.constructor(this);return n.s=-n.s,j(n)};E.plus=E.add=function(n){var i,t,r,e,s,u,d,f,c,p,g=this,y=g.constructor;if(n=new y(n),!g.d||!n.d)return!g.s||!n.s?n=new y(NaN):g.d||(n=new y(n.d||g.s===n.s?g:NaN)),n;if(g.s!=n.s)return n.s=-n.s,g.minus(n);if(c=g.d,p=n.d,d=y.precision,f=y.rounding,!c[0]||!p[0])return p[0]||(n=new y(g)),$?j(n,d,f):n;if(s=Vt(g.e/Q),r=Vt(n.e/Q),c=c.slice(),e=s-r,e){for(e<0?(t=c,e=-e,u=p.length):(t=p,r=s,u=c.length),s=Math.ceil(d/Q),u=s>u?s+1:u+1,e>u&&(e=u,t.length=1),t.reverse();e--;)t.push(0);t.reverse()}for(u=c.length,e=p.length,u-e<0&&(e=u,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),u=c.length;c[--u]==0;)c.pop();return n.d=c,n.e=He(c,r),$?j(n,d,f):n};E.precision=E.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=wr(t.d),n&&t.e+1>i&&(i=t.e+1)):i=NaN,i};E.round=function(){var n=this,i=n.constructor;return j(new i(n),n.e+1,i.rounding)};E.sine=E.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())+Q,r.rounding=1,t=Gr(r,Pr(r,t)),r.precision=n,r.rounding=i,j(ae>2?t.neg():t,n,i,!0)):new r(NaN)};E.squareRoot=E.sqrt=function(){var n,i,t,r,e,s,u=this,d=u.d,f=u.e,c=u.s,p=u.constructor;if(c!==1||!d||!d[0])return new p(!c||c<0&&(!d||d[0])?NaN:d?u:1/0);for($=!1,c=Math.sqrt(+u),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(at(u,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&&(j(s,f+1,0),s.times(s).eq(u))){r=s;break}t+=4,e=1}else{(!+i||!+i.slice(1)&&i.charAt(0)=="5")&&(j(r,f+1,1),n=!r.times(r).eq(u));break}return $=!0,j(r,f,p.rounding,n)};E.tangent=E.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=at(t,new r(1).minus(t.times(t)).sqrt(),n+10,0),r.precision=n,r.rounding=i,j(ae==2||ae==4?t.neg():t,n,i,!0)):new r(NaN)};E.times=E.mul=function(n){var i,t,r,e,s,u,d,f,c,p=this,g=p.constructor,y=p.d,P=(n=new g(n)).d;if(n.s*=p.s,!y||!y[0]||!P||!P[0])return new g(!n.s||y&&!y[0]&&!P||P&&!P[0]&&!y?NaN:!y||!P?n.s/0:n.s*0);for(t=Vt(p.e/Q)+Vt(n.e/Q),f=y.length,c=P.length,f<c&&(s=y,y=P,P=s,u=f,f=c,c=u),s=[],u=f+c,r=u;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[--u];)s.pop();return i?++t:s.shift(),n.d=s,n.e=He(s,t),$?j(n,g.precision,g.rounding):n};E.toBinary=function(n,i){return Oi(this,2,n,i)};E.toDecimalPlaces=E.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),j(t,n+t.e+1,i))};E.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=j(new e(r),n+1,i),t=se(r,!0,n+1)),r.isNeg()&&!r.isZero()?"-"+t:t};E.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=j(new s(e),n+e.e+1,i),t=se(r,!1,n+r.e+1)),e.isNeg()&&!e.isZero()?"-"+t:t};E.toFraction=function(n){var i,t,r,e,s,u,d,f,c,p,g,y,P=this,S=P.d,B=P.constructor;if(!S)return new B(P);if(c=t=new B(1),r=f=new B(0),i=new B(r),s=i.e=wr(S)-P.e-1,u=s%Q,i.d[0]=$t(10,u<0?Q+u:u),n==null)n=s>0?i:c;else{if(d=new B(n),!d.isInt()||d.lt(c))throw Error(ce+d);n=d.gt(i)?s>0?i:c:d}for($=!1,d=new B(zt(S)),p=B.precision,B.precision=s=S.length*Q*2;g=at(d,i,0,1,1),e=t.plus(g.times(r)),e.cmp(n)!=1;)t=r,r=e,e=c,c=f.plus(g.times(e)),f=e,e=i,i=d.minus(g.times(e)),d=e;return e=at(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=at(c,r,s,1).minus(P).abs().cmp(at(f,t,s,1).minus(P).abs())<1?[c,r]:[f,t],B.precision=p,$=!0,y};E.toHexadecimal=E.toHex=function(n,i){return Oi(this,16,n,i)};E.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]?($=!1,t=at(t,n,0,i,1).times(n),$=!0,j(t)):(n.s=t.s,t=n),t};E.toNumber=function(){return+this};E.toOctal=function(n,i){return Oi(this,8,n,i)};E.toPower=E.pow=function(n){var i,t,r,e,s,u,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 j(d,r,s);if(i=Vt(n.e/Q),i>=n.d.length-1&&(t=c<0?-c:c)<=Qr)return e=vr(f,d,t,r),n.s<0?new f(1).div(e):j(e,r,s);if(u=d.s,u<0){if(i<n.d.length-1)return new f(NaN);if((n.d[i]&1)==0&&(u=1),d.e==0&&d.d[0]==1&&d.d.length==1)return d.s=u,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?u/0:0):($=!1,f.rounding=d.s=1,t=Math.min(12,(i+"").length),e=Ii(n.times(fe(d,r+t)),r),e.d&&(e=j(e,r+5,1),Se(e.d,r,s)&&(i=r+10,e=j(Ii(n.times(fe(d,i+t)),i),i+5,1),+zt(e.d).slice(r+1,r+15)+1==1e14&&(e=j(e,r+1,0)))),e.s=u,$=!0,f.rounding=s,j(e,r,s))};E.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=j(new e(r),n,i),t=se(r,n<=r.e||r.e<=e.toExpNeg,n)),r.isNeg()&&!r.isZero()?"-"+t:t};E.toSignificantDigits=E.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)),j(new r(t),n,i)};E.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};E.truncated=E.trunc=function(){return j(new this.constructor(this),this.e+1,1)};E.valueOf=E.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="",u=n[0];if(e>0){for(s+=u,i=1;i<e;i++)r=n[i]+"",t=Q-r.length,t&&(s+=de(t)),s+=r;u=n[i],r=u+"",t=Q-r.length,t&&(s+=de(t))}else if(u===0)return"0";for(;u%10===0;)u/=10;return s+u}function Jt(n,i,t){if(n!==~~n||n<i||n>t)throw Error(ce+n)}function Se(n,i,t,r){var e,s,u,d;for(s=n[0];s>=10;s/=10)--i;return--i<0?(i+=Q,e=0):(e=Math.ceil((i+1)/Q),i%=Q),s=$t(10,Q-i),d=n[e]%s|0,r==null?i<3?(i==0?d=d/100|0:i==1&&(d=d/10|0),u=t<4&&d==99999||t>3&&d==49999||d==5e4||d==0):u=(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),u=(r||t<4)&&d==9999||!r&&t>3&&d==4999):u=((r||t<4)&&d+1==s||!r&&t>3&&d+1==s/2)&&(n[e+1]/s/1e3|0)==$t(10,i-3)-1,u}function qe(n,i,t){for(var r,e=[0],s,u=0,d=n.length;u<d;){for(s=e.length;s--;)e[s]*=i;for(e[0]+=Si.indexOf(n.charAt(u++)),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 $r(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 u=i.times(i);i=u.times(u).minus(u).times(8).plus(1)}return n.precision-=t,i}var at=(function(){function n(r,e,s){var u,d=0,f=r.length;for(r=r.slice();f--;)u=r[f]*e+d,r[f]=u%s|0,d=u/s|0;return d&&r.unshift(d),r}function i(r,e,s,u){var d,f;if(s!=u)f=s>u?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,u){for(var d=0;s--;)r[s]-=d,d=r[s]<e[s]?1:0,r[s]=d*u+r[s]-e[s];for(;!r[0]&&r.length>1;)r.shift()}return function(r,e,s,u,d,f){var c,p,g,y,P,S,B,C,k,R,I,x,z,Z,J,it,tt,ot,K,st,v=r.constructor,o=r.s==e.s?1:-1,l=r.d,h=e.d;if(!l||!l[0]||!h||!h[0])return new v(!r.s||!e.s||(l?h&&l[0]==h[0]:!h)?NaN:l&&l[0]==0||!h?o*0:o/0);for(f?(P=1,p=r.e-e.e):(f=re,P=Q,p=Vt(r.e/P)-Vt(e.e/P)),K=h.length,tt=l.length,k=new v(o),R=k.d=[],g=0;h[g]==(l[g]||0);g++);if(h[g]>(l[g]||0)&&p--,s==null?(Z=s=v.precision,u=v.rounding):d?Z=s+(r.e-e.e)+1:Z=s,Z<0)R.push(1),S=!0;else{if(Z=Z/P+2|0,g=0,K==1){for(y=0,h=h[0],Z++;(g<tt||y)&&Z--;g++)J=y*f+(l[g]||0),R[g]=J/h|0,y=J%h|0;S=y||g<tt}else{for(y=f/(h[0]+1)|0,y>1&&(h=n(h,y,f),l=n(l,y,f),K=h.length,tt=l.length),it=K,I=l.slice(0,K),x=I.length;x<K;)I[x++]=0;st=h.slice(),st.unshift(0),ot=h[0],h[1]>=f/2&&++ot;do y=0,c=i(h,I,K,x),c<0?(z=I[0],K!=x&&(z=z*f+(I[1]||0)),y=z/ot|0,y>1?(y>=f&&(y=f-1),B=n(h,y,f),C=B.length,x=I.length,c=i(B,I,C,x),c==1&&(y--,t(B,K<C?st:h,C,f))):(y==0&&(c=y=1),B=h.slice()),C=B.length,C<x&&B.unshift(0),t(I,B,x,f),c==-1&&(x=I.length,c=i(h,I,K,x),c<1&&(y++,t(I,K<x?st:h,x,f))),x=I.length):c===0&&(y++,I=[0]),R[g++]=y,c&&I[0]?I[x++]=l[it]||0:(I=[l[it]],x=1);while((it++<tt||I[0]!==void 0)&&Z--);S=I[0]!==void 0}R[0]||R.shift()}if(P==1)k.e=p,pr=S;else{for(g=1,y=R[0];y>=10;y/=10)g++;k.e=g+p*P-1,j(k,d?s+k.e+1:s,u,S)}return k}})();function j(n,i,t,r){var e,s,u,d,f,c,p,g,y,P=n.constructor;t:if(i!=null){if(g=n.d,!g)return n;for(e=1,d=g[0];d>=10;d/=10)e++;if(s=i-e,s<0)s+=Q,u=i,p=g[y=0],f=p/$t(10,e-u-1)%10|0;else if(y=Math.ceil((s+1)/Q),d=g.length,y>=d)if(r){for(;d++<=y;)g.push(0);p=f=0,e=1,s%=Q,u=s-Q+1}else break t;else{for(p=d=g[y],e=1;d>=10;d/=10)e++;s%=Q,u=s-Q+e,f=u<0?0:p/$t(10,e-u-1)%10|0}if(r=r||i<0||g[y+1]!==void 0||(u<0?p:p%$t(10,e-u-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?u>0?p/$t(10,e-u):0:g[y-1])%10&1||t==(n.s<0?8:7)),i<1||!g[0])return g.length=0,c?(i-=n.e+1,g[0]=$t(10,(Q-i%Q)%Q),n.e=-i||0):g[0]=n.e=0,n;if(s==0?(g.length=y,d=1,y--):(g.length=y+1,d=$t(10,Q-s),g[y]=u>0?(p/$t(10,e-u)%$t(10,u)|0)*d:0),c)for(;;)if(y==0){for(s=1,u=g[0];u>=10;u/=10)s++;for(u=g[0]+=d,d=1;u>=10;u/=10)d++;s!=d&&(n.e++,g[0]==re&&(g[0]=1));break}else{if(g[y]+=d,g[y]!=re)break;g[y--]=0,d=1}for(s=g.length;g[--s]===0;)g.pop()}return $&&(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 Mr(n);var r,e=n.e,s=zt(n.d),u=s.length;return i?(t&&(r=t-u)>0?s=s.charAt(0)+"."+s.slice(1)+de(r):u>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-u)>0&&(s+=de(r))):e>=u?(s+=de(e+1-u),t&&(r=t-e-1)>0&&(s=s+"."+de(r))):((r=e+1)<u&&(s=s.slice(0,r)+"."+s.slice(r)),t&&(r=t-u)>0&&(e+1===u&&(s+="."),s+=de(r))),s}function He(n,i){var t=n[0];for(i*=Q;t>=10;t/=10)i++;return i}function Ze(n,i,t){if(i>Kr)throw $=!0,t&&(n.precision=t),Error(mr);return j(new n(je),i,1,!0)}function oe(n,i,t){if(i>Ei)throw Error(mr);return j(new n(We),i,t,!0)}function wr(n){var i=n.length-1,t=i*Q+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 vr(n,i,t,r){var e,s=new n(1),u=Math.ceil(r/Q+4);for($=!1;;){if(t%2&&(s=s.times(i),cr(s.d,u)&&(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),cr(i.d,u)}return $=!0,s}function fr(n){return n.d[n.d.length-1]&1}function yr(n,i,t){for(var r,e,s=new n(i[0]),u=0;++u<i.length;){if(e=new n(i[u]),!e.s){s=e;break}r=s.cmp(e),(r===t||r===0&&s.s===t)&&(s=e)}return s}function Ii(n,i){var t,r,e,s,u,d,f,c=0,p=0,g=0,y=n.constructor,P=y.rounding,S=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?($=!1,f=S):f=i,d=new y(.03125);n.e>-2;)n=n.times(d),g+=5;for(r=Math.log($t(2,g))/Math.LN10*2+5|0,f+=r,t=s=u=new y(1),y.precision=f;;){if(s=j(s.times(n),f,1),t=t.times(++p),d=u.plus(at(s,t,f,1)),zt(d.d).slice(0,f)===zt(u.d).slice(0,f)){for(e=g;e--;)u=j(u.times(u),f,1);if(i==null)if(c<3&&Se(u.d,f-r,P,c))y.precision=f+=10,t=s=d=new y(1),p=0,c++;else return j(u,y.precision=S,P,$=!0);else return y.precision=S,u}u=d}}function fe(n,i){var t,r,e,s,u,d,f,c,p,g,y,P=1,S=10,B=n,C=B.d,k=B.constructor,R=k.rounding,I=k.precision;if(B.s<0||!C||!C[0]||!B.e&&C[0]==1&&C.length==1)return new k(C&&!C[0]?-1/0:B.s!=1?NaN:C?0:B);if(i==null?($=!1,p=I):p=i,k.precision=p+=S,t=zt(C),r=t.charAt(0),Math.abs(s=B.e)<15e14){for(;r<7&&r!=1||r==1&&t.charAt(1)>3;)B=B.times(n),t=zt(B.d),r=t.charAt(0),P++;s=B.e,r>1?(B=new k("0."+t),s++):B=new k(r+"."+t.slice(1))}else return c=Ze(k,p+2,I).times(s+""),B=fe(new k(r+"."+t.slice(1)),p-S).plus(c),k.precision=I,i==null?j(B,I,R,$=!0):B;for(g=B,f=u=B=at(B.minus(1),B.plus(1),p,1),y=j(B.times(B),p,1),e=3;;){if(u=j(u.times(y),p,1),c=f.plus(at(u,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,I).times(s+""))),f=at(f,new k(P),p,1),i==null)if(Se(f.d,p-S,R,d))k.precision=p+=S,c=u=B=at(g.minus(1),g.plus(1),p,1),y=j(B.times(B),p,1),e=d=1;else return j(f,k.precision=I,R,$=!0);else return k.precision=I,f;f=c,e+=2}}function Mr(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)%Q,t<0&&(r+=Q),r<e){for(r&&n.d.push(+i.slice(0,r)),e-=Q;r<e;)n.d.push(+i.slice(r,r+=Q));i=i.slice(r),r=Q-i.length}else r-=e;for(;r--;)i+="0";n.d.push(+i),$&&(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 zr(n,i){var t,r,e,s,u,d,f,c,p;if(i.indexOf("_")>-1){if(i=i.replace(/(\d)_(?=\d)/g,"$1"),br.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(Ur.test(i))t=16,i=i.toLowerCase();else if(Zr.test(i))t=2;else if(Hr.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("."),u=s>=0,r=n.constructor,u&&(i=i.replace(".",""),d=i.length,s=d-s,e=vr(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,$=!1,u&&(n=at(n,e,d*4)),f&&(n=n.times(Math.abs(f)<54?$t(2,f):Be.pow(2,f))),$=!0,n)}function Gr(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),u=new n(16),d=new n(20);t--;)e=i.times(i),i=i.times(s.plus(e.times(u.times(e).minus(d))));return i}function we(n,i,t,r,e){var s,u,d,f,c=1,p=n.precision,g=Math.ceil(p/Q);for($=!1,f=t.times(t),d=new n(r);;){if(u=at(d.times(f),new n(i++*i++),p,1),d=e?r.plus(u):r.minus(u),r=at(u.times(f),new n(i++*i++),p,1),u=d.plus(r),u.d[g]!==void 0){for(s=g;u.d[s]===d.d[s]&&s--;);if(s==-1)break}s=d,d=r,r=u,u=s,c++}return $=!0,u.d.length=g+1,u}function Qe(n,i){for(var t=n;--i;)t*=n;return t}function Pr(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=fr(t)?r?2:3:r?4:1,i;ae=fr(t)?r?1:4:r?3:2}return i.minus(e).abs()}function Oi(n,i,t,r){var e,s,u,d,f,c,p,g,y,P=n.constructor,S=t!==void 0;if(S?(Jt(t,1,he),r===void 0?r=P.rounding:Jt(r,0,8)):(t=P.precision,r=P.rounding),!n.isFinite())p=Mr(n);else{for(p=se(n),u=p.indexOf("."),S?(e=2,i==16?t=t*4-3:i==8&&(t=t*3-2)):e=i,u>=0&&(p=p.replace(".",""),y=new P(1),y.e=p.length-u,y.d=qe(se(y),10,e),y.e=y.d.length),g=qe(p,10,e),s=f=g.length;g[--f]==0;)g.pop();if(!g[0])p=S?"0p+0":"0";else{if(u<0?s--:(n=new P(n),n.d=g,n.e=s,n=at(n,y,t,r,0,e),g=n.d,s=n.e,c=pr),u=g[t],d=e/2,c=c||g[t+1]!==void 0,c=r<4?(u!==void 0||c)&&(r===0||r===(n.s<0?3:2)):u>d||u===d&&(r===4||c||r===6&&g[t-1]&1||r===(n.s<0?8:7)),g.length=t,c)for(;++g[--t]>e-1;)g[t]=0,t||(++s,g.unshift(1));for(f=g.length;!g[f-1];--f);for(u=0,p="";u<f;u++)p+=Si.charAt(g[u]);if(S){if(f>1)if(i==16||i==8){for(u=i==16?4:3,--f;f%u;f++)p+="0";for(g=qe(p,e,i),f=g.length;!g[f-1];--f);for(u=1,p="1.";u<f;u++)p+=Si.charAt(g[u])}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 cr(n,i){if(n.length>i)return n.length=i,!0}function Vr(n){return new this(n).abs()}function Xr(n){return new this(n).acos()}function Jr(n){return new this(n).acosh()}function Yr(n,i){return new this(n).plus(i)}function tn(n){return new this(n).asin()}function en(n){return new this(n).asinh()}function rn(n){return new this(n).atan()}function nn(n){return new this(n).atanh()}function on(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(at(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(at(n,i,s,1)),t}function sn(n){return new this(n).cbrt()}function an(n){return j(n=new this(n),n.e+1,2)}function ln(n,i,t){return new this(n).clamp(i,t)}function un(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]=Bi[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]=Bi[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(_r);else this[t]=!1;else throw Error(ce+t+": "+r);return this}function dn(n){return new this(n).cos()}function fn(n){return new this(n).cosh()}function Ar(n){var i,t,r;function e(s){var u,d,f,c=this;if(!(c instanceof e))return new e(s);if(c.constructor=e,hr(s)){c.s=s.s,$?!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(u=0,d=s;d>=10;d/=10)u++;$?u>e.maxE?(c.e=NaN,c.d=null):u<e.minE?(c.e=0,c.d=[0]):(c.e=u,c.d=[s]):(c.e=u,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),br.test(s)?Le(c,s):zr(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=E,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=un,e.clone=Ar,e.isDecimal=hr,e.abs=Vr,e.acos=Xr,e.acosh=Jr,e.add=Yr,e.asin=tn,e.asinh=en,e.atan=rn,e.atanh=nn,e.atan2=on,e.cbrt=sn,e.ceil=an,e.clamp=ln,e.cos=dn,e.cosh=fn,e.div=cn,e.exp=hn,e.floor=pn,e.hypot=mn,e.ln=_n,e.log=gn,e.log10=wn,e.log2=bn,e.max=vn,e.min=yn,e.mod=Mn,e.mul=Pn,e.pow=An,e.random=Sn,e.round=Bn,e.sign=En,e.sin=In,e.sinh=On,e.sqrt=xn,e.sub=Tn,e.sum=Cn,e.tan=kn,e.tanh=Rn,e.trunc=Dn,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 cn(n,i){return new this(n).div(i)}function hn(n){return new this(n).exp()}function pn(n){return j(n=new this(n),n.e+1,3)}function mn(){var n,i,t=new this(0);for($=!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 $=!0,new this(1/0);t=i}return $=!0,t.sqrt()}function hr(n){return n instanceof Be||n&&n.toStringTag===gr||!1}function _n(n){return new this(n).ln()}function gn(n,i){return new this(n).log(i)}function bn(n){return new this(n).log(2)}function wn(n){return new this(n).log(10)}function vn(){return yr(this,arguments,-1)}function yn(){return yr(this,arguments,1)}function Mn(n,i){return new this(n).mod(i)}function Pn(n,i){return new this(n).mul(i)}function An(n,i){return new this(n).pow(i)}function Sn(n){var i,t,r,e,s=0,u=new this(1),d=[];if(n===void 0?n=this.precision:Jt(n,1,he),r=Math.ceil(n/Q),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(_r);else for(;s<r;)d[s++]=Math.random()*1e7|0;for(r=d[--s],n%=Q,r&&n&&(e=$t(10,Q-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-=Q)d.shift();for(r=1,e=d[0];e>=10;e/=10)r++;r<Q&&(t-=Q-r)}return u.e=t,u.d=d,u}function Bn(n){return j(n=new this(n),n.e+1,this.rounding)}function En(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 On(n){return new this(n).sinh()}function xn(n){return new this(n).sqrt()}function Tn(n,i){return new this(n).sub(i)}function Cn(){var n=0,i=arguments,t=new this(i[n]);for($=!1;t.s&&++n<i.length;)t=t.plus(i[n]);return $=!0,j(t,this.precision,this.rounding)}function kn(n){return new this(n).tan()}function Rn(n){return new this(n).tanh()}function Dn(n){return j(n=new this(n),n.e+1,1)}E[Symbol.for("nodejs.util.inspect.custom")]=E.toString;E[Symbol.toStringTag]="Decimal";var Be=E.constructor=Ar(Bi);je=new Be(je);We=new Be(We);var q=Be;var Ee=(r=>(r[r.Spot=0]="Spot",r[r.Curve=1]="Curve",r[r.BidAsk=2]="BidAsk",r))(Ee||{});var U=require("@cetusprotocol/common-sdk");var le=1e3,Fn=-443636,Nn=443636,ve=1e4,ye=2e3,_e=200,ge=443636n,Ke=1e8,xi=1e9,te=1e4,qn=604800,Ln=1747627200;var rt=Mi(Ne());var jn=new rt.default(524288),N=64,$e=new rt.default(1).shln(N),Sr=new rt.default(2).pow(new rt.default(128)).sub(new rt.default(1)),F=class n{static splitBinLiquidityInfo(i,t,r){let e=n.getPositionCount(t,r);if(e<=1)return[i];let s=[],u=t;for(let d=0;d<e;d++){let f=Math.min(u+1e3-2,r),c=i.bins.filter(p=>p.bin_id>=u&&p.bin_id<=f);s.push({bins:c,amount_a:c.reduce((p,g)=>(0,U.d)(p).plus(g.amount_a),(0,U.d)(0)).toFixed(0),amount_b:c.reduce((p,g)=>(0,U.d)(p).plus(g.amount_b),(0,U.d)(0)).toFixed(0)}),u=f+1}return s}static processBinsByRate(i,t){let r=[],e=(0,U.d)(0),s=(0,U.d)(0),u=!1;return i.forEach(d=>{let{amount_a:f,amount_b:c,liquidity:p="0"}=d,g=(0,U.d)(t).mul(p).toFixed(0),y=(0,U.d)(f).mul(t),P=(0,U.d)(c).mul(t);e=(0,U.d)(e).plus(y),s=(0,U.d)(s).plus(P),((0,U.d)(y).lt(1)&&(0,U.d)(y).gt(0)||(0,U.d)(P).lt(1)&&(0,U.d)(P).gt(0))&&(u=!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:g})}),{bins:{bins:r,amount_a:e.toFixed(0),amount_b:s.toFixed(0)},has_invalid_amount:u}}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,U.d)(t).gte((0,U.d)(s)))return{amount_a:r,amount_b:e};let u=(0,U.d)(t).div(s).mul(r).toFixed(0,q.ROUND_FLOOR),d=(0,U.d)(t).div(s).mul(e).toFixed(0,q.ROUND_FLOOR);return{amount_a:u,amount_b:d}}static getPositionCount(i,t){let e=(0,U.d)(t).sub(i).add(1).div(1e3);return Number(e.toFixed(0,q.ROUND_UP))}static getLiquidity(i,t,r){return(0,U.d)(r).mul(i).add((0,U.d)(t).mul((0,U.d)(2).pow(N))).toFixed(0)}static getAmountAFromLiquidity(i,t){return(0,U.d)(i).div((0,U.d)(t)).toFixed(0)}static getAmountBFromLiquidity(i){return(0,U.d)(i).div((0,U.d)(2).pow(N)).toFixed(0)}static getAmountsFromLiquidity(i,t,r,e){if((0,U.d)(e).isZero()&&X("LiquiditySupplyIsZero","Liquidity supply is zero"),(0,U.d)(r).gt((0,U.d)(e))&&X("InvalidDeltaLiquidity","Invalid delta liquidity"),(0,U.d)(r).isZero())return["0","0"];let s;(0,U.d)(i).isZero()?s="0":s=(0,U.d)(i).mul((0,U.d)(r)).div((0,U.d)(e)).toFixed(0,q.ROUND_FLOOR);let u;return(0,U.d)(t).isZero()?u="0":u=(0,U.d)(t).mul((0,U.d)(r)).div((0,U.d)(e)).toFixed(0,q.ROUND_FLOOR),[s,u]}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 q(t).div(new q(1e4));return new q(1).add(new q(r)).pow(new q(i)).toString()}static getBinIdFromPrice(i,t,r,e,s){let u=n.getPricePerLamport(e,s,i);return n.getBinIdFromLamportPrice(u,t,r)}static getBinIdFromLamportPrice(i,t,r){let e=new q(t).div(new q(1e4)),s=new q(i).log().dividedBy(new q(1).add(e).log());return(r?s.floor():s.ceil()).toNumber()}static getPricePerLamport(i,t,r){return new q(r).mul(new q(10**(t-i))).toString()}static getPriceFromLamport(i,t,r){return new q(r).div(new q(10**(t-i))).toString()}static getReversePrice(i){return new q(1).div(i).toString()}static getQPriceFromId(i,t){let r=new rt.default(t).shln(N).div(new rt.default(1e4)),e=$e.add(r);return n.pow(e,new rt.default(i)).toString()}static getPricePerLamportFromQPrice(i){return U.MathUtil.fromX64(new rt.default(i)).toString()}static pow(i,t){let r=t.isNeg();if(t.isZero())return $e;if(t=r?t.abs():t,t.gt(jn))return new rt.default(0);let e=i,s=$e;return e.gte(s)&&(e=Sr.div(e),r=!r),t.and(new rt.default(1)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(2)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(4)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(8)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(16)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(32)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(64)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(128)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(256)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(512)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(1024)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(2048)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(4096)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(8192)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(16384)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(32768)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(65536)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(131072)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(262144)).isZero()||(s=s.mul(e).shrn(N)),s.isZero()?new rt.default(0):(r&&(s=Sr.div(s)),s)}static binScore(i){let t=BigInt(i)+ge;return(t<0n||t>ge*2n)&&X("InvalidBinId",new Error("Invalid bin ID"),{[U.DETAILS_KEYS.METHOD_NAME]:"binScore",[U.DETAILS_KEYS.REQUEST_PARAMS]:{binId:i}}),t.toString()}static scoreToBinId(i){let t=BigInt(i)-ge;return(t<-ge||t>ge)&&X("InvalidBinId",new Error("Invalid score"),{[U.DETAILS_KEYS.METHOD_NAME]:"scoreToBinId",[U.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 q("18446744073709551615").log(10).div(new q(t).log(10)).floor(),s=e.neg(),u=e,d=(0,U.d)(1),f=(0,U.d)("340282366920938463463374607431768211455");for(;;){let c=(0,U.d)(n.getQPriceFromId(s.toNumber(),i));if(c.gt(d)&&!c.isZero())break;s=s.add(1)}for(;;){let c=(0,U.d)(n.getQPriceFromId(u.toNumber(),i));if(c.lt(f)&&!c.isZero())break;u=u.sub(1)}return{minBinId:s.toNumber(),maxBinId:u.toNumber()}}static getBinShift(i,t,r){let e=n.getPricePerLamportFromBinId(i,t),s=(0,U.d)(e).mul(1+r).toString(),u=n.getBinIdFromLamportPrice(s,t,!0),d=(0,U.d)(u).sub(i).abs().toFixed(0,q.ROUND_UP);return console.log("getBinShift Options:",{active_id:i,bin_shift:d}),Number(d)}};var Xt=require("@mysten/sui/bcs"),Er=require("blakejs");function Ti(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 X("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseDlmmBasePool",[G.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function Ie(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},u=i.reward_manager.fields,d=u.rewards.map(g=>{let y=g.fields.current_emission_rate,P=G.MathUtil.fromX64(new Br.default(y)),S=Math.floor(P.toNumber()*60*60*24).toString();return{reward_coin:(0,G.fixCoinType)(g.fields.reward_coin.fields.name,!1),emissions_per_second:P.toString(),emissions_per_day:S,period_emission_rates:{id:g.fields.period_emission_rates.fields.id.id,size:g.fields.period_emission_rates.fields.size}}}),f={is_public:u.is_public,emergency_reward_pause:u.emergency_reward_pause,vault:{id:u.vault.fields.id.id,size:u.vault.fields.size},rewards:d,last_updated_time:u.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),X("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),X("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseDlmmPosition",[G.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function Wn(n,i,t,r){let e=n.map((d,f)=>{let c=t+f,p=F.getPricePerLamportFromBinId(c,i);if(c===r.bin_id){let{amount_a:P,amount_b:S}=F.calculateOutByShare(r,d);return{bin_id:c,amount_a:P,amount_b:S,liquidity:d,price_per_lamport:p}}if(c<r.bin_id){let P=F.getAmountBFromLiquidity(d);return{bin_id:c,amount_a:"0",amount_b:P,liquidity:d,price_per_lamport:p}}let g=F.getQPriceFromId(c,i),y=F.getAmountAFromLiquidity(d,g);return{bin_id:c,amount_a:y,amount_b:"0",liquidity:d,price_per_lamport:p}}),s=e.reduce((d,f)=>d.add(new q(f.amount_a)),new q(0)).toFixed(0),u=e.reduce((d,f)=>d.add(new q(f.amount_b)),new q(0)).toFixed(0);return{bins:e,amount_a:s,amount_b:u}}function Ci(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:F.getPricePerLamportFromQPrice(r.price)}))}catch(i){return console.log("\u{1F680} ~ parseBinInfo ~ error:",i),[]}}function ki(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:F.getPricePerLamportFromQPrice(n.price)}}catch(i){return console.log("\u{1F680} ~ parseBinInfo ~ error:",i),X("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,u=e.reward,d=e.amount,f={coin_type:(0,G.fixCoinType)(u.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:u,amount_in:d,amount_out:f,fee:c,ref_fee:p,bin_swaps:g,from:y,target:P}=e,S=g.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:u,a2b:i,in_amount:d,out_amount:f,ref_fee_amount:p,fee_amount:c,bin_swaps:S,partner:s,from_coin_type:(0,G.fixCoinType)(y.name,!1),to_coin_type:(0,G.fixCoinType)(P.name,!1)}}}function Zn(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:u}=n;return u?.forEach((d,f)=>{let{name:c,address:p}=(0,G.extractStructTagFromType)(d.type);if(Ir.includes(c)&&p===t&&r===d.parsedJson.pool){let g={tx:d.id.txDigest,sender:d.sender,type:d.type,block_time:s||"0",index:`${i}_${f}`,parsed_json:d.parsedJson};e.push(g)}}),e}function Un(n,i,t){let r=[],e=0,s=new Date(n*1e3).getTime(),u=new Date().getTime();for(;;){let d=s+e*t;if(d>=u&&r.push(d),e>=i)break;e+=1}return r}function Hn(n,i,t,r){let e=[];for(let s=i;s<=t;s+=r){let u=n.findLast(d=>(0,G.d)(s).gte(d.time));u?e.push({time:s.toString(),emissions_per_second:u.emissions_per_second,emissions_per_day:u.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 Qn(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 Pe(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"),u=s.length,d=0;for(;d<u;){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]),g=(0,Er.blake2b)(p,void 0,32);return`0x${Buffer.from(g).toString("hex")}`}var O=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:u,total_amount_a:d,total_amount_b:f,active_bin_of_pool:c}=i,p=r<s||r>u,g=F.getQPriceFromId(r,e),y=(0,O.d)(0),P=(0,O.d)(0),S=(0,O.d)(200);if(t===2?S=(0,O.d)(200):t===1?S=(0,O.d)(2e3):t===0&&(S=(0,O.d)(1)),!p&&c){let tt=this.calculateActiveWeights(c.amount_a,c.amount_b,g,S);y=tt.active_weight_a,P=tt.active_weight_b}r===s&&(0,O.d)(f).isZero()&&(y=(0,O.d)(S).mul((0,O.d)(2).pow(N*2)).div((0,O.d)(g)).floor(),P=(0,O.d)(0)),r===u&&(0,O.d)(d).isZero()&&(P=(0,O.d)(S).mul((0,O.d)(2).pow(N)).floor(),y=(0,O.d)(0)),r>s&&r<u&&((0,O.d)(d).isZero()&&(P=(0,O.d)(S).mul((0,O.d)(2).pow(N)).floor(),y=(0,O.d)(0)),(0,O.d)(f).isZero()&&(y=(0,O.d)(S).mul((0,O.d)(2).pow(N*2)).div((0,O.d)(g)).floor(),P=(0,O.d)(0)));let B=p?(0,O.d)(0):y,C=p?(0,O.d)(0):P,k=(0,O.d)(2e3).sub((0,O.d)(200)).floor(),R=r>u?u:r,I=r<s?s:r,x=r>s?R===s?(0,O.d)(0):k.div((0,O.d)(R-s)).floor():(0,O.d)(0),z=u>r?I===u?(0,O.d)(0):k.div((0,O.d)(u-I)).floor():(0,O.d)(0),Z=s,J=[],it=[];for(;Z<=u;){let tt=(0,O.d)(0);if(Z<r){let ot=R-Z;t===0?tt=(0,O.d)(1):t===2?tt=(0,O.d)(S).add(x.mul(ot)).floor():t===1&&(tt=(0,O.d)(S).sub(x.mul(ot)).floor())}else if(Z>r){let ot=Z-I;t===0?tt=(0,O.d)(1):t===2?tt=(0,O.d)(S).add(z.mul(ot)).floor():t===1&&(tt=(0,O.d)(S).sub(z.mul(ot)).floor())}else tt=S;if(J.push(tt),Z<r)C=C.add(tt.mul((0,O.d)(2).pow(N))).floor(),it.push((0,O.d)(0));else if(Z>r){let ot=tt.mul((0,O.d)(2).pow(N*2)).div(F.getQPriceFromId(Z,e)).floor();it.push(ot),B=B.add(ot).floor()}else it.push((0,O.d)(0));Z+=1}return{...i,total_weight_a:B,total_weight_b:C,active_weight_a:y,active_weight_b:P,weights:J,weight_per_prices:it}}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,u=e-200,d=r>i?Math.floor(u/(r-i)):0,f=t>r?Math.floor(u/(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,u=e-s,d=r>i?Math.floor(u/(r-i)):0,f=t>r?Math.floor(u/(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 u=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,O.d)(0));if(u.cmp((0,O.d)(0))!=1)throw Error("Invalid parameters");return{bins:e.map(f=>{let c=F.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 g=(0,O.d)(f.weight).div(u),y=ne((0,O.d)(t),g).toString(),P="0",S=F.getQPriceFromId(f.bin_id,r),B=F.getLiquidity(P,y,S);return{bin_id:f.bin_id,amount_b:y,amount_a:P,price_per_lamport:c,liquidity:B}}}),amount_a:"0",amount_b:t}}static toAmountAskSide(i,t,r,e,s=!1){let u=e.filter(f=>f.bin_id>=i).reduce(function(f,c){if(c.bin_id<=i&&!s)return f;{let p=F.getPricePerLamportFromBinId(c.bin_id,t),g=new q(c.weight).div(p);return f.add(g)}},new q(0));if(u.cmp(new q(0))!=1)throw Error("Invalid parameters");return{bins:e.map(f=>{let c=F.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 q(f.weight).div(c).div(u),P=ne((0,O.d)(r),y).toString(),S="0",B=F.getQPriceFromId(f.bin_id,t),C=F.getLiquidity(P,S,B);return{bin_id:f.bin_id,amount_a:P,amount_b:S,price_per_lamport:c,liquidity:C}}}),amount_a:r,amount_b:"0"}}static toAmountBothSide(i,t,r,e,s,u,d){let f=!(0,O.d)(r).isZero()&&(0,O.d)(e).isZero(),c=(0,O.d)(r).isZero()&&!(0,O.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(g=>g.bin_id===i);if(p.length===1){let{totalWeightA:g,totalWeightB:y,activeWeightA:P,activeWeightB:S}=n.calculateTotalWeights(t,d,i,p[0],s,u,f?"a":c?"b":void 0),B=new q(r.toString()).div(g),C=new q(e.toString()).div(y),k=d.map(x=>{let z=F.getPricePerLamportFromBinId(x.bin_id,t);if(x.bin_id<i||x.bin_id===i&&c){let st=ne(C,new q(x.weight)),v="0",o=F.getQPriceFromId(x.bin_id,t),l=F.getLiquidity(v,st.toString(),o);return{bin_id:x.bin_id,amount_a:"0",amount_b:st.toString(),price_per_lamport:z,liquidity:l}}if(x.bin_id>i||x.bin_id===i&&f){let st=new q(x.weight).div(z),v=ne(B,new q(st)),o="0",l=F.getQPriceFromId(x.bin_id,t),h=F.getLiquidity(v.toString(),o,l);return{bin_id:x.bin_id,amount_a:v.toString(),amount_b:"0",price_per_lamport:z,liquidity:h}}let Z=ne(B,P),J=ne(C,S),it=Z.toString(),tt=J.toString(),ot=F.getQPriceFromId(x.bin_id,t),K=F.getLiquidity(it,tt,ot);return{bin_id:x.bin_id,amount_a:it,amount_b:tt,price_per_lamport:z,liquidity:K}}),R=k.reduce((x,z)=>(0,O.d)(x).add((0,O.d)(z.amount_a)),(0,O.d)(0)).toString(),I=k.reduce((x,z)=>(0,O.d)(x).add((0,O.d)(z.amount_b)),(0,O.d)(0)).toString();return{bins:k,amount_a:R,amount_b:I}}else{let{totalWeightA:g,totalWeightB:y}=n.calculateTotalWeights(t,d,i),P=new q(r.toString()).div(g),S=new q(e.toString()).div(y),B=d.map(R=>{let I=F.getPricePerLamportFromBinId(R.bin_id,t);if(R.bin_id<i){let x=ne(S,new q(R.weight));return{bin_id:R.bin_id,amount_a:"0",amount_b:x.toString(),price_per_lamport:I}}else{let x=new q(R.weight).div(I),z=ne(P,x);return{bin_id:R.bin_id,amount_a:z.toString(),amount_b:"0",price_per_lamport:I}}}),C=B.reduce((R,I)=>(0,O.d)(R).add((0,O.d)(I.amount_a)),(0,O.d)(0)).toString(),k=B.reduce((R,I)=>(0,O.d)(R).add((0,O.d)(I.amount_b)),(0,O.d)(0)).toString();return{bins:B,amount_a:C,amount_b:k}}}static autoFillCoinByWeight(i,t,r,e,s,u,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?u:void 0),g=(0,O.d)(0);e?g=c.isZero()?new q(0):new q(r).div(c):g=p.isZero()?new q(0):new q(r).div(p);let y=ne(g,e?p:c).toString();return n.toAmountBothSide(i,t,e?r:y,e?y:r,s,u,d)}static calculateActiveWeights(i,t,r,e){let s=(0,O.d)(r),u=(0,O.d)(i),d=(0,O.d)(t),f=(0,O.d)(0),c=(0,O.d)(0);if(u.isZero()&&d.isZero())f=(0,O.d)(e).mul((0,O.d)(2).pow(N*2)).div(s.mul(2)).floor(),c=(0,O.d)(e).mul((0,O.d)(2).pow(N)).div(2).floor();else{if(u.isZero())f=(0,O.d)(0);else{let p=d.mul((0,O.d)(2).pow(N)).div(u);f=(0,O.d)(e).mul((0,O.d)(2).pow(N*2)).div(s.add(p)).floor()}if(d.isZero())c=(0,O.d)(0);else{let p=(0,O.d)(2).pow(N).add(s.mul(u).div(d)).floor();c=(0,O.d)(e).mul((0,O.d)(2).pow(N*2)).div(p).floor()}}return{active_weight_a:f,active_weight_b:c}}static calculateTotalWeights(i,t,r,e,s,u,d){let f=(0,O.d)(F.getPricePerLamportFromBinId(r,i)),c=(0,O.d)(0),p=(0,O.d)(0);if(s&&u&&e&&!d)if((0,O.d)(s).isZero()&&(0,O.d)(u).isZero())c=new q(e.weight).div(f.mul(new q(2))),p=new q(e.weight).div(new q(2));else{let P=new q(s.toString()),S=new q(u.toString());(0,O.d)(s).isZero()||(c=new q(e.weight).div(f.add(S.div(P)))),(0,O.d)(u).isZero()||(p=new q(e.weight).div(new q(1).add(f.mul(P).div(S))))}let g=c,y=p;return t.forEach(P=>{if((P.bin_id<r||d==="b")&&(y=y.add(new q(P.weight))),P.bin_id>r||d==="a"){let S=F.getPricePerLamportFromBinId(P.bin_id,i),B=new q(P.weight).div(S);g=g.add(B)}}),{totalWeightA:g,totalWeightB:y,activeWeightA:c,activeWeightB:p}}};var Yt=require("@cetusprotocol/common-sdk");var Ae=class{static toAmountsByWeights(i){let{total_weight_a:t,total_weight_b:r,weights:e,weight_per_prices:s,total_amount_a:u,total_amount_b:d,active_weight_a:f,active_weight_b:c,lower_bin_id:p,upper_bin_id:g,active_id:y,bin_step:P}=i,S=r.isZero()?(0,Yt.d)(0):(0,Yt.d)(d).mul((0,Yt.d)(2).pow(N*2)).div(r).floor(),B=t.isZero()?(0,Yt.d)(0):(0,Yt.d)(u).mul((0,Yt.d)(2).pow(N*2)).div(t).floor(),C=Pe(B.mul(f).div((0,Yt.d)(2).pow(N*2))),k=Pe(S.mul(c).div((0,Yt.d)(2).pow(N*2))),R=g-p+1,I=p,x=0,z=[];for(;x<R;){let J=(0,Yt.d)(0),it=(0,Yt.d)(0),tt=F.getQPriceFromId(I,P);I<y?it=Pe(S.mul(e[x]).div((0,Yt.d)(2).pow(N))):I>y?J=Pe(B.mul(s[x]).div((0,Yt.d)(2).pow(N*2))):(J=C,it=k);let ot=F.getLiquidity(J.toString(),it.toString(),tt);z.push({bin_id:I,amount_a:J.toString(),amount_b:it.toString(),price_per_lamport:F.getPricePerLamportFromBinId(I,P),liquidity:ot}),I+=1,x+=1}return{bins:z,amount_a:u.toString(),amount_b:d.toString()}}static toAmountsBothSideByStrategy(i,t,r,e,s,u,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:u,active_bin_of_pool:f});return this.toAmountsByWeights(c)}static autoFillCoinByStrategy(i,t,r,e,s,u,d,f){switch(d){case 0:{let c=ee.toWeightSpotBalanced(s,u);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}case 1:{let c=ee.toWeightCurve(s,u,i);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}case 2:{let c=ee.toWeightBidAsk(s,u,i);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}}}static autoFillCoinByStrategyV2(i,t,r,e,s,u,d,f){let c=this.autoFillCoinByStrategy(i,t,r,e,s,u,d,f),p=ee.toWeight({strategy_type:d,active_id:i,bin_step:t,lower_bin_id:s,upper_bin_id:u,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:u}=e,d=F.getQPriceFromId(i.bin_id,s),f=F.getLiquidity(i.amount_a,i.amount_b,d),c=F.getLiquidity(t.amount_a,t.amount_b,d),p=(0,ft.d)(i.liquidity).mul(c).div(f).toFixed(0),{amount_a:g,amount_b:y}=F.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(u).mul(10),S=n.getVariableFee(r),B=(0,ft.d)(P).add(S).toFixed(0);(0,ft.d)(B).gt(1e8)&&(B=1e8.toString());let C="0",k="0";return(0,ft.d)(g).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),B):(0,ft.d)(y).gt(t.amount_b)&&(0,ft.d)(t.amount_a).gt(g)&&(C=n.calculateCompositionFee((0,ft.d)(t.amount_a).sub(g).toFixed(0),B)),{fees_a:C,fees_b:k}}};var H=require("@cetusprotocol/common-sdk");var ji=class{static calculateIlm(i){let{curvature:t,initial_price:r,max_price:e,bin_step:s,total_supply:u,pool_share_percentage:d,config:f}=i,{price_curve_points_num:c,liquidity_distribution_num:p,tokens_table_num:g,price_table_num:y}=f;if((0,H.d)(d).lt(0)||(0,H.d)(d).gt(100))throw new Error("Pool Share Percentage must be greater than 0 and less than 100.");let P=!1;if((0,H.d)(e).lt((0,H.d)(r)))throw new Error("Maximum Price must be greater or equal to Initial Price.");if((0,H.d)(e).eq(r)&&!(0,H.d)(t).eq(0))throw new Error("Curvature must be 0 when Maximum and Initial Price are equal.");if(!(0,H.d)(e).eq(r)&&(0,H.d)(t).eq(0))throw new Error("Maximum and Initial Price must be equal when Curvature is 0.");(0,H.d)(e).eq(r)&&(0,H.d)(t).eq(0)&&(P=!0);let S=_=>(0,H.d)(x).mul((0,H.d)(_).div(I).pow(t)).add((0,H.d)(r)).toString(),B=(_,a)=>{let w=(0,H.d)(x).mul((0,H.d)(I).pow(-t)).mul((0,H.d)(_).pow(t+1)).div(t+1).add((0,H.d)(r).mul(_)),D=(0,H.d)(x).mul((0,H.d)(I).pow(-t)).mul((0,H.d)(a).pow(t+1)).div(t+1).add((0,H.d)(r).mul(a));return w.sub(D)},C=_=>(0,H.d)(I).mul((0,H.d)(_).sub(r).div(x).pow(1/t)).toString(),k=_=>(0,H.d)(I).mul((0,H.d)(_).sub(r).pow(1/t-1)).div((0,H.d)(t).mul((0,H.d)(x).pow(1/t))).toString(),R=_=>(0,H.d)(_).sub(r).div(x).pow(1/t).mul(I),I=(0,H.d)(u).mul(d).div(100),x=(0,H.d)(e).sub((0,H.d)(r)).toFixed(8),z=[],Z=[],J=F.getBinIdFromLamportPrice(r,s,!1),it=F.getBinIdFromLamportPrice(e,s,!1),tt=(0,H.d)(0),ot=J;if(P)z.push(r),Z.push((0,H.d)(r).mul(I).toString());else for(;ot<=it;){let _=F.getPricePerLamportFromBinId(ot,s),a=F.getPricePerLamportFromBinId(ot+1,s),w=R(a).sub(R(_));tt=tt.add(w),Z.push((0,H.d)(w).mul(_).toString()),z.push(_),ot++}let K={price_curve:[],liquidity_curve:[],dlmm_bins:[],tokens_table:[],price_table:[],initial_fdv:(0,H.d)(r).mul(u).toString(),final_fdv:(0,H.d)(e).mul(u).toString(),usdc_in_pool:"0"};var st=(0,H.d)(x).div(100);let v=[];for(let _=0;_<c;_++){let a=(0,H.d)(_).mul(I).div(c-1).toString(),w=P?e:S(a);v.push({x:a,y:w})}K.price_curve=v;let o=[];if(P)o=[{x:r,y:"0"},{x:r,y:(0,H.d)(I).div(2).toString()},{x:r,y:I.toString()}];else for(let _=0;_<p;_++){let a=(0,H.d)(r).add((0,H.d)(_).mul((0,H.d)(st))).toFixed(9),w=k(a);o.push({x:a,y:w})}K.liquidity_curve=o;let l=[];z.forEach((_,a)=>{let w=_,D=Z[a];l.push({x:w,y:D})}),K.dlmm_bins=l;let h=[],m=(0,H.d)(0),b=(0,H.d)(I).div(g).toFixed(5);for(;m.lte(I);){let _=(0,H.d)(S(m.toString())).toFixed(8),a=m.toFixed(8),w=B(Number(a),0);h.push({withdrawn:a,price:_,usdc_in_pool:w.toString()}),m=m.add(b)}K.tokens_table=h;let M=(0,H.d)(x).mul(1e10).floor().div(1e11),A=[];if(P)A.push({withdrawn:I.toString(),price:r,usdc_in_pool:(0,H.d)(I).mul(r).toString()});else{let _=(0,H.d)(0),a=(0,H.d)(r);for(;_.lte(y);){let w=(0,H.d)(C(a.toFixed(8))).toFixed(8),D=B(Number(w),0).toFixed(8);A.push({withdrawn:w,price:a.toFixed(8),usdc_in_pool:D}),a=(0,H.d)(a).add(M),_=_.add((0,H.d)(1))}}return K.price_table=A,K.usdc_in_pool=A[A.length-1].usdc_in_pool,K}};var Or=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:u}=this._sdk.sdkOptions,{pools_id:d}=(0,et.getPackagerConfigs)(u),f=await this._sdk.FullClient.getDynamicFieldObject({parentId:d,name:{type:"0x2::object::ID",value:s}});return(0,et.getObjectFields)(f).value.fields.value.fields.pool_id}catch(s){return X("FetchError",s,{[et.DETAILS_KEYS.METHOD_NAME]:"getPoolAddress",[et.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",u=`${r.package_id}_getBasePoolList`;if(s){let d=this._sdk.getCache(u,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=Ti(c);e.data.push(p)}),s&&this._sdk.updateCache(`${r.package_id}_getPoolImmutables`,e.data)}catch(d){return X("FetchError",d,{[et.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(u=>u.id),{showContent:!0,showType:!0});r.has_next_page=e.has_next_page,r.next_cursor=e.next_cursor;for(let u of s){let d=Ie(u),f=`${d.id}_getDlmmPool`;this._sdk.updateCache(f,d),r.data.push(d)}}catch(s){return X("FetchError",s,{[et.DETAILS_KEYS.METHOD_NAME]:"getPools"})}return r}async getBinInfo(i,t,r,e=!0){try{let s=`${i}_getBinInfo_${t}`,u=this._sdk.getCache(s,e);if(u!==void 0)return u;let d=F.binScore(t),[f,c]=F.resolveBinPosition(d),g=(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=ki(g);return this._sdk.updateCache(s,y),y}catch{return{bin_id:t,amount_a:"0",amount_b:"0",liquidity:"0",price_per_lamport:F.getPricePerLamportFromBinId(t,r)}}}async getTotalFeeRate(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:e,coin_type_b:s}=i,u=new xe.Transaction;u.moveCall({target:`${t.published_at}::pool::get_total_fee_rate`,arguments:[u.object(r)],typeArguments:[e,s]});let d=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:u,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,u=1e3,d=[],f,c=!0;for(;c;){let p=new xe.Transaction,g;f!==void 0?g=p.moveCall({target:"0x1::option::some",arguments:[p.pure.u32(Number((0,et.asUintN)(BigInt(f))))],typeArguments:["u32"]}):g=p.moveCall({target:"0x1::option::none",typeArguments:["u32"]}),p.moveCall({target:`${t.published_at}::pool::fetch_bins`,arguments:[p.object(r),g,p.pure.u64(u)],typeArguments:[e,s]});let y=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:p,sender:(0,Wi.normalizeSuiAddress)("0x0")}),P=Ci(y);d.push(...P),f=P.length>0?P[P.length-1].bin_id+1:void 0,c=P.length===u}return d.sort((p,g)=>p.bin_id-g.bin_id)}async getPoolTransactionList({pool_id:i,pagination_args:t,order:r="descending",full_rpc_url:e}){let{FullClient:s,sdkOptions:u}=this._sdk,d;e?d=(0,et.createFullClient)(new Or.SuiClient({url:e})):d=s;let f={data:[],has_next_page:!1},c=50,p=t,g=t.limit||10;do{let y=await d.queryTransactionBlocksByPage({ChangedObject:i},{...p,limit:50},r);y.data.forEach((P,S)=>{let B=Ni(P,S,u.dlmm_pool.package_id,i);f.data=[...f.data,...B]}),f.has_next_page=y.has_next_page,f.next_cursor=y.next_cursor,p.cursor=y.next_cursor}while(f.data.length<g&&f.has_next_page);return f.data.length>g&&(f.data=f.data.slice(0,g),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 u=t;u<=r;u++){let d=await Promise.all([this.getBinInfo(i,u,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=Ie(e),u=`${s.id}_getDlmmPool`;this._sdk.updateCache(u,s),t.push(s)}}catch(r){return X("FetchError",r,{[et.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}}),u=Ie(s);return this._sdk.updateCache(r,u),u}catch(r){return X("FetchError",r,{[et.DETAILS_KEYS.METHOD_NAME]:"getPool",[et.DETAILS_KEYS.REQUEST_PARAMS]:i})}}async createPoolAndAddWithPricePayload(i){let{bin_step:t,url:r,coin_type_a:e,coin_type_b:s,bin_infos:u,price_base_coin:d,price:f,lower_price:c,upper_price:p,decimals_a:g,decimals_b:y,strategy_type:P,use_bin_infos:S,base_factor:B,pool_id:C}=i,k,R,I,x=u;if(d==="coin_a")k=F.getBinIdFromPrice(c,t,!1,g,y),R=F.getBinIdFromPrice(p,t,!0,g,y),I=F.getBinIdFromPrice(f,t,!0,g,y);else{k=F.getBinIdFromPrice((0,et.d)(1).div(p).toString(),t,!1,g,y),R=F.getBinIdFromPrice((0,et.d)(1).div(c).toString(),t,!0,g,y),I=F.getBinIdFromPrice((0,et.d)(1).div(f).toString(),t,!1,g,y);let J={pool_id:C,amount_a:u.amount_b,amount_b:u.amount_a,active_id:I,bin_step:t,lower_bin_id:k,upper_bin_id:R,active_bin_of_pool:void 0,strategy_type:i.strategy_type};x=await this.sdk.Position.calculateAddLiquidityInfo(J)}let Z={bin_step:t,url:r,coin_type_a:e,coin_type_b:s,bin_infos:x,lower_bin_id:k,upper_bin_id:R,active_id:I,strategy_type:P,use_bin_infos:S,base_factor:B};return this.createPoolAndAddLiquidityPayload(Z)}createPoolPayload(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{bin_step:e,base_factor:s,url:u,coin_type_a:d,coin_type_b:f,active_id:c}=i;t=t||new xe.Transaction;let{registry_id:p,global_config_id:g,versioned_id:y}=(0,et.getPackagerConfigs)(r),[P,S]=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,et.asUintN)(BigInt(c)))),t.pure.string(u||""),t.object(g),t.object(y),t.object(et.CLOCK_ADDRESS)],typeArguments:[d,f]});return t.moveCall({target:`${r.published_at}::registry::destroy_receipt`,arguments:[t.object(P),S,t.object(y)],typeArguments:[d,f]}),S}createPoolAndAddLiquidityPayload(i){let{bin_step:t,base_factor:r,url:e,active_id:s,coin_type_a:u,coin_type_b:d,bin_infos:f,lower_bin_id:c,upper_bin_id:p,strategy_type:g,use_bin_infos:y}=i,P=new xe.Transaction;(0,et.isSortedSymbols)((0,et.fixCoinType)(u,!1),(0,et.fixCoinType)(d,!1))&&X("InvalidCoinTypeSequence",new Error("invalid coin type sequence"),{[et.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[et.DETAILS_KEYS.REQUEST_PARAMS]:i});let S=p-c+1;S>1e3&&X("InvalidBinWidth",new Error("Width is too large"),{[et.DETAILS_KEYS.METHOD_NAME]:"openPosition",[et.DETAILS_KEYS.REQUEST_PARAMS]:i}),console.log("\u{1F680} ~ createPoolAndAddLiquidityPayload ~ option:",{...i,width:S});let C={pool_id:this.createPoolPayload({active_id:s,bin_step:t,base_factor:r,coin_type_a:u,coin_type_b:d},P),bin_infos:f,coin_type_a:u,coin_type_b:d,lower_bin_id:c,upper_bin_id:p,active_id:s,strategy_type:g,use_bin_infos:y,max_price_slippage:0,bin_step:t};return this.sdk.Position.addLiquidityPayload(C,P),P}};var Ve=require("@cetusprotocol/common-sdk"),Zi={env:"testnet",full_rpc_url:Ve.FullRpcUrlTestnet,graph_rpc_url:Ve.GraphRpcUrlTestnet,dlmm_pool:{package_id:"0x17a1f5a8779461ff44e942adf33325cce112c693d6a177ed77f035ca86d1fdb6",published_at:"0x6d32c1be32eefcea933c03dd5cb7c783d1d83f6b30c4d1131d955933747b1701",version:1,config:{registry_id:"0x319070e26a6809f439d3c4a45e63bf74939c5fe3165de7b65968ee8547f71bd0",pools_id:"0x505fcde74ab557d553832a87f169a0408ad3507ca4e84b25f7d32c2c1535765c",global_config_id:"0x88bb33e9eff2fccab980a0e4b43fc4572abd08f08304d47a20d3e4e99d94d159",versioned_id:"0xa710caae87b2129acc97fbb98ea7011e3137c3291b02c0fcce866d67d5d9e8d0",admin_cap_id:"0x6fc908894ad7c2ff16cca07a05af6760831a8b5e5dc34e40470dce6ee1760155",partners_id:"0xc5c31fe1550e39c9890e0fe3d2608dd9b408a10d74020e5ff72ccfffe4c9c879"}},dlmm_router:{package_id:"0xba3059875c8980ac171fc2bac81b9df172fb77fa0cb5a267636df701225b93ef",published_at:"0x59b7a2da6db8f9245a1db6169018af7124c0714fa77a84224967ead6be125127",version:1},faucet:{package_id:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",published_at:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",version:1}};var T=require("@cetusprotocol/common-sdk"),pe=require("@mysten/sui/transactions"),xr=require("@mysten/sui/utils");var Xe=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),X("GetObjectError",r,{[T.DETAILS_KEYS.METHOD_NAME]:"getOwnerPositionList",[T.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),X("GetObjectError",t,{[T.DETAILS_KEYS.METHOD_NAME]:"getPosition",[T.DETAILS_KEYS.REQUEST_PARAMS]:i})}}collectFeePayload(i,t){let{pool_id:r,position_id:e,coin_type_a:s,coin_type_b:u}=i,{dlmm_pool:d}=this.sdk.sdkOptions,{versioned_id:f,global_config_id:c}=(0,T.getPackagerConfigs)(d);t=t||new pe.Transaction;let[p,g]=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,u]}),y=T.CoinAssist.fromBalance(p,s,t),P=T.CoinAssist.fromBalance(g,u,t);return t.transferObjects([y,P],this.sdk.getSenderAddress()),t}updatePositionFeeAndRewards(i,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:e}=(0,T.getPackagerConfigs)(r),{pool_id:s,position_id:u,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(u),t.object(e),t.object(T.CLOCK_ADDRESS)],typeArguments:[d,f]}),t}collectRewardPayload(i,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:e,global_config_id:s}=(0,T.getPackagerConfigs)(r);return t=t||new pe.Transaction,i.forEach(u=>{let{pool_id:d,position_id:f,reward_coins:c,coin_type_a:p,coin_type_b:g}=u;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,g,y]}),S=T.CoinAssist.fromBalance(P,y,t);t.transferObjects([S],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:u,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:u,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:u,coin_type_a:d,coin_type_b:f}=i,{dlmm_router:c}=this.sdk.sdkOptions,p=F.getBinShift(e,u,s),g=Number((0,T.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(g)),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:u,coin_type_b:d}=i,{dlmm_pool:f}=this.sdk.sdkOptions,{versioned_id:c,global_config_id:p}=(0,T.getPackagerConfigs)(f);t=t||new pe.Transaction,this.updatePositionFeeAndRewards({pool_id:r,position_id:e,coin_type_a:u,coin_type_b:d},t),this.collectRewardPayload([{pool_id:r,position_id:e,reward_coins:s,coin_type_a:u,coin_type_b:d}],t);let[g,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(T.CLOCK_ADDRESS)],typeArguments:[u,d]}),S=T.CoinAssist.fromBalance(y,u,t),B=T.CoinAssist.fromBalance(P,d,t);return t.moveCall({target:`${f.published_at}::pool::destroy_close_position_cert`,arguments:[g,t.object(c)],typeArguments:[]}),t.transferObjects([S,B],this.sdk.getSenderAddress()),t}async getActiveBinIfInRange(i,t,r,e,s,u=!1){if(e<=r&&e>=t)return await this._sdk.Pool.getBinInfo(i,e,s,u)}calculateRemoveLiquidityInfo(i){let{bins:t,active_id:r,coin_amount:e}=i,s="fix_amount_a"in i,u=t.filter(y=>y.bin_id<r),d=t.filter(y=>y.bin_id>r),f=(0,T.d)(0),c=(0,T.d)(0),p=[];if(s){p=[...t];let{fix_amount_a:y}=i,P=t.find(C=>C.bin_id===r),S=d.reduce((C,k)=>(0,T.d)(C).plus(k.amount_a),(0,T.d)(0)).add(P?.amount_a||"0"),B=u.reduce((C,k)=>(0,T.d)(C).plus(k.amount_b),(0,T.d)(0)).add(P?.amount_b||"0");f=y?S:B,c=(0,T.d)(e).gte(f)?(0,T.d)(1):(0,T.d)(e).div(f)}else{let{is_only_a:y}=i;p=y?d:u,f=p.reduce((P,S)=>(0,T.d)(P).plus(y?S.amount_a:S.amount_b),(0,T.d)(0)),c=(0,T.d)(e).gte(f)?(0,T.d)(1):(0,T.d)(e).div(f)}if((0,T.d)(f).isZero())return X("InsufficientLiquidity",new Error("Insufficient liquidity"),{[T.DETAILS_KEYS.METHOD_NAME]:"calculateRemoveLiquidityResult",[T.DETAILS_KEYS.REQUEST_PARAMS]:i});let g=F.processBinsByRate([...p],c.toFixed());if(g.has_invalid_amount){let y=(0,T.d)(e),P=[],S=s?i.fix_amount_a:i.is_only_a;for(let B of p){if(y.lte(0))break;let C=S?B.amount_a:B.amount_b,k=S?B.amount_b:B.amount_a,R=(0,T.d)((0,T.d)(y).div(C).toFixed(9,q.ROUND_UP));R=R.gt(1)?(0,T.d)(1):R;let I=(0,T.d)(C).mul(R);y=y.minus(I);let x=(0,T.d)(k).mul(R),z=(0,T.d)(B.liquidity).mul(R);P.push({...B,amount_a:S?I.toFixed(0):x.toFixed(0),amount_b:S?x.toFixed(0):I.toFixed(0),liquidity:z.toFixed(0)})}return{bins:P,amount_a:P.reduce((B,C)=>(0,T.d)(B).plus(C.amount_a),(0,T.d)(0)).toFixed(0),amount_b:P.reduce((B,C)=>(0,T.d)(B).plus(C.amount_b),(0,T.d)(0)).toFixed(0)}}return g.bins}async calculateAddLiquidityInfo(i){let t="fix_amount_a"in i,{active_id:r,bin_step:e,lower_bin_id:s,upper_bin_id:u,active_bin_of_pool:d,strategy_type:f,pool_id:c}=i,p;if(t){let{coin_amount:g,fix_amount_a:y}=i;p=Ae.autoFillCoinByStrategyV2(r,e,g,y,s,u,f,d)}else p=Ae.toAmountsBothSideByStrategy(r,e,s,u,i.amount_a,i.amount_b,f,d);if(d&&c){let g=p.bins.findIndex(y=>y.bin_id===r);if(g!==-1){let y=await this._sdk.Pool.getPool(c,!1);if(y){let{fees_a:P,fees_b:S}=Oe.getCompositionFees(d,p.bins[g],y.variable_parameters),B=p.bins[g];B.amount_a=(0,T.d)(B.amount_a).sub(P).toFixed(0),B.amount_b=(0,T.d)(B.amount_b).sub(S).toFixed(0),p.bins[g]=B,console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_a:",P),console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_b:",S)}}}return p}removeLiquidityPayload(i){let{pool_id:t,position_id:r,bin_infos:e,reward_coins:s,slippage:u,coin_type_a:d,coin_type_b:f,active_id:c,collect_fee:p,bin_step:g,remove_percent:y}=i,{dlmm_pool:P}=this.sdk.sdkOptions,{bins:S}=e,B=new pe.Transaction;(p||s.length>0)&&this.updatePositionFeeAndRewards({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},B),p&&this.collectFeePayload({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},B),this.collectRewardPayload([{pool_id:t,position_id:r,reward_coins:s,coin_type_a:d,coin_type_b:f}],B);let{versioned_id:C,global_config_id:k}=(0,T.getPackagerConfigs)(P);if(y){let R=(0,T.asUintN)(BigInt(S[0].bin_id)),I=(0,T.asUintN)(BigInt(S[S.length-1].bin_id)),x=Number((0,T.d)(y).mul(1e4).toFixed(0)),[z,Z]=B.moveCall({target:`${P.published_at}::pool::remove_liquidity_by_percent`,arguments:[B.object(t),B.object(r),B.pure.u32(Number(R)),B.pure.u32(Number(I)),B.pure.u16(x),B.object(k),B.object(C),B.object(T.CLOCK_ADDRESS)],typeArguments:[d,f]}),J=T.CoinAssist.fromBalance(z,d,B),it=T.CoinAssist.fromBalance(Z,f,B);B.transferObjects([J,it],this.sdk.getSenderAddress())}else{let R=B.pure.vector("u32",S.map(it=>Number((0,T.asUintN)(BigInt(it.bin_id))))),I=B.pure.vector("u128",S.map(it=>it.liquidity)),[x,z]=B.moveCall({target:`${P.published_at}::pool::remove_liquidity`,arguments:[B.object(t),B.object(r),R,I,B.object(k),B.object(C),B.object(T.CLOCK_ADDRESS)],typeArguments:[d,f]}),Z=T.CoinAssist.fromBalance(x,d,B),J=T.CoinAssist.fromBalance(z,f,B);B.transferObjects([Z,J],this.sdk.getSenderAddress())}return this.validateActiveIdSlippage({pool_id:t,active_id:c,max_price_slippage:u,bin_step:g,coin_type_a:d,coin_type_b:f},B),B}async addLiquidityWithPricePayload(i){let{pool_id:t,bin_infos:r,coin_type_a:e,coin_type_b:s,price_base_coin:u,price:d,lower_price:f,upper_price:c,bin_step:p,strategy_type:g,active_bin_of_pool:y,decimals_a:P,decimals_b:S,use_bin_infos:B,max_price_slippage:C}=i,k,R,I,x=r;if(u==="coin_a")k=F.getBinIdFromPrice(f,p,!1,P,S),R=F.getBinIdFromPrice(c,p,!0,P,S),I=F.getBinIdFromPrice(d,p,!0,P,S);else{k=F.getBinIdFromPrice((0,T.d)(1).div(c).toString(),p,!1,P,S),R=F.getBinIdFromPrice((0,T.d)(1).div(f).toString(),p,!0,P,S),I=F.getBinIdFromPrice((0,T.d)(1).div(d).toString(),p,!1,P,S);let J={amount_a:r.amount_b,amount_b:r.amount_a,active_id:I,bin_step:p,lower_bin_id:k,upper_bin_id:R,active_bin_of_pool:y,strategy_type:g};x=await this.sdk.Position.calculateAddLiquidityInfo(J)}let Z={pool_id:t,active_id:I,bin_infos:x,coin_type_a:e,coin_type_b:s,lower_bin_id:k,upper_bin_id:R,strategy_type:g,use_bin_infos:B,max_price_slippage:C,bin_step:p};return this.addLiquidityPayload(Z)}addLiquidityPayload(i,t){let{pool_id:r,bin_infos:e,coin_type_a:s,coin_type_b:u,active_id:d,strategy_type:f,max_price_slippage:c,bin_step:p,use_bin_infos:g=!1}=i;t=t||new pe.Transaction;let y="lower_bin_id"in i,P=[];if(y){let S=F.splitBinLiquidityInfo(e,i.lower_bin_id,i.upper_bin_id);P.push(...S)}else{let S=i.position_id;P.push(e),(i.collect_fee||i.reward_coins.length>0)&&this.updatePositionFeeAndRewards({pool_id:r,position_id:S,coin_type_a:s,coin_type_b:u},t),i.collect_fee&&this.collectFeePayload({pool_id:r,position_id:S,coin_type_a:s,coin_type_b:u},t),i.reward_coins.length>0&&this.collectRewardPayload([{pool_id:r,position_id:S,reward_coins:i.reward_coins,coin_type_a:s,coin_type_b:u}],t)}return P.forEach((S,B)=>{console.log("\u{1F680} ~ PositionModule ~ addLiquidityPayload ~ liquidity_bin:",B,S);let{amount_a:C,amount_b:k,bins:R}=S,I=T.CoinAssist.buildCoinWithBalance(BigInt(C),s,t),x=T.CoinAssist.buildCoinWithBalance(BigInt(k),u,t);g?this.addLiquidityInternal({pool_id:r,coin_type_a:s,coin_type_b:u,active_id:d,liquidity_bin:S,tx:t,coin_a_obj_id:I,coin_b_obj_id:x,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:u,active_id:d,liquidity_bin:S,tx:t,max_price_slippage:c,bin_step:p,coin_a_obj_id:I,coin_b_obj_id:x,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:u,coin_type_b:d,active_id:f,liquidity_bin:c,tx:p,coin_a_obj_id:g,coin_b_obj_id:y,strategy_type:P}=i,{dlmm_pool:S,dlmm_router:B}=this.sdk.sdkOptions,{versioned_id:C,global_config_id:k}=(0,T.getPackagerConfigs)(S),{bins:R,amount_a:I,amount_b:x}=c,z=e,Z=R[0].bin_id,J=R[R.length-1].bin_id,it=(0,T.asUintN)(BigInt(Z)),tt=Number((0,T.asUintN)(BigInt(f))),ot=F.getBinShift(f,r,t),K=qi(P);if(e===void 0){let st=J-Z+1;st>1e3&&X("InvalidBinWidth",new Error("Width is too large"),{[T.DETAILS_KEYS.METHOD_NAME]:"openPosition"}),z=p.moveCall({target:`${B.published_at}::${K}::open_position`,arguments:[typeof s=="string"?p.object(s):s,g,y,p.pure.u64(I),p.pure.u64(x),p.pure.u32(Number(it)),p.pure.u16(Number(st)),p.pure.u32(Number(tt)),p.pure.u32(Number(ot)),p.object(k),p.object(C),p.object(T.CLOCK_ADDRESS)],typeArguments:[u,d]})}else{let st=R.filter(l=>l.amount_a!=="0"||l.amount_b!=="0");if(st.length===0)return X("InvalidParams",new Error("No bins to add liquidity"),{[T.DETAILS_KEYS.METHOD_NAME]:"addLiquidityStrategyInternal",[T.DETAILS_KEYS.REQUEST_PARAMS]:i});let v=(0,T.asUintN)(BigInt(st[0].bin_id)),o=(0,T.asUintN)(BigInt(st[st.length-1].bin_id));p.moveCall({target:`${B.published_at}::${K}::add_liquidity`,arguments:[typeof s=="string"?p.object(s):s,p.object(e),g,y,p.pure.u64(I),p.pure.u64(x),p.pure.u32(Number(v)),p.pure.u32(Number(o)),p.pure.u32(Number(tt)),p.pure.u32(Number(ot)),p.object(k),p.object(C),p.object(T.CLOCK_ADDRESS)],typeArguments:[u,d]})}return z?p.transferObjects([z,g,y],this.sdk.getSenderAddress()):p.transferObjects([g,y],this.sdk.getSenderAddress()),p}addLiquidityInternal(i){let{position_id:t,pool_id:r,coin_type_a:e,coin_type_b:s,active_id:u,liquidity_bin:d,tx:f,coin_a_obj_id:c,coin_b_obj_id:p,max_price_slippage:g,bin_step:y}=i,{bins:P}=d,{dlmm_pool:S,dlmm_router:B}=this.sdk.sdkOptions,{versioned_id:C,global_config_id:k}=(0,T.getPackagerConfigs)(S),R=f.pure.vector("u64",P.map(J=>J.amount_a)),I=f.pure.vector("u64",P.map(J=>J.amount_b)),x=f.makeMoveVec({elements:P.map(J=>f.pure.u32(Number((0,T.asUintN)(BigInt(J.bin_id))))),type:"u32"}),z=d.bins[0].bin_id,Z=d.bins[d.bins.length-1].bin_id;if(t===void 0){Z-z+1>1e3&&X("InvalidBinWidth",new Error("Width is too large"),{[T.DETAILS_KEYS.METHOD_NAME]:"openPosition"});let it=f.moveCall({target:`${B.published_at}::add_liquidity::open_position`,arguments:[typeof r=="string"?f.object(r):r,c,p,x,R,I,f.object(k),f.object(C),f.object(T.CLOCK_ADDRESS)],typeArguments:[e,s]});u>=z&&u<=Z&&this.validateActiveIdSlippage({pool_id:r,active_id:u,max_price_slippage:g,bin_step:y,coin_type_a:e,coin_type_b:s},f),f.transferObjects([c,p,it],this.sdk.getSenderAddress())}else f.moveCall({target:`${B.published_at}::add_liquidity::add_liquidity`,arguments:[typeof r=="string"?f.object(r):r,f.object(t),c,p,x,R,I,f.object(k),f.object(C),f.object(T.CLOCK_ADDRESS)],typeArguments:[e,s]}),u>=z&&u<=Z&&this.validateActiveIdSlippage({pool_id:r,active_id:u,max_price_slippage:g,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,xr.normalizeSuiAddress)("0x0")});if(r.error!=null)return X("FetchError",new Error(r.error),{[T.DETAILS_KEYS.METHOD_NAME]:"fetchPositionFeeAndReward",[T.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 Tr=require("@mysten/sui/utils");var Je=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:u,by_amount_in:d,in_amount:f}=i,{versioned_id:c,global_config_id:p}=(0,Gt.getPackagerConfigs)(t),g=new Ui.Transaction;g.moveCall({target:`${t.published_at}::pool::flash_swap`,arguments:[g.object(r),g.pure.bool(u),g.pure.bool(d),g.pure.u64(f),g.object(p),g.object(c),g.object(Gt.CLOCK_ADDRESS)],typeArguments:[e,s]});let y=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:g,sender:(0,Tr.normalizeSuiAddress)("0x0")});if(y.error!=null)return X("FetchError",new Error(y.error),{[Gt.DETAILS_KEYS.METHOD_NAME]:"fetchSwapQuote",[Gt.DETAILS_KEYS.REQUEST_PARAMS]:i});let P=Fi(y,u);return P==null?X("FetchError",new Error("No quote info"),{[Gt.DETAILS_KEYS.METHOD_NAME]:"preSwapQuote",[Gt.DETAILS_KEYS.REQUEST_PARAMS]:i}):(P.a2b=u,P)}swapPayload(i){let{dlmm_pool:t,dlmm_router:r}=this._sdk.sdkOptions,{quote_obj:e,coin_type_a:s,coin_type_b:u,by_amount_in:d,slippage:f,partner:c}=i,{pool_id:p,in_amount:g,out_amount:y,a2b:P}=e,S=new Ui.Transaction,B=d?g:(0,Gt.d)(g).mul(1+f).toFixed(0),C=d?(0,Gt.d)(y).mul(1-f).toFixed(0):y,k=Gt.CoinAssist.buildCoinWithBalance(BigInt(B),P?s:u,S),{versioned_id:R,global_config_id:I}=(0,Gt.getPackagerConfigs)(t);return console.log("\u{1F680} ~ SwapModule ~ option:",{...i,in_amount_limit:B,out_amount_limit:C}),c?S.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b_with_partner":"swap_b2a_with_partner"}`,arguments:[S.object(p),S.object(c),k,S.pure.bool(d),S.pure.u64(BigInt(d?g:y)),S.pure.u64(BigInt(d?C:B)),S.object(I),S.object(R),S.object(Gt.CLOCK_ADDRESS)],typeArguments:[s,u]}):S.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b":"swap_b2a"}`,arguments:[S.object(p),k,S.pure.bool(d),S.pure.u64(BigInt(d?g:y)),S.pure.u64(BigInt(d?C:B)),S.object(I),S.object(R),S.object(Gt.CLOCK_ADDRESS)],typeArguments:[s,u]}),S.transferObjects([k],this.sdk.getSenderAddress()),S}};var ke=require("@mysten/sui/transactions"),Y=require("@cetusprotocol/common-sdk");var Ye=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,Y.getPackagerConfigs)(t);try{let e=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}}),u=(0,Y.getObjectFields)(e).partners.fields.contents.map(d=>d.fields.value);u.length>0&&(await this._sdk.FullClient.batchGetObjects(u,{showContent:!0,showType:!0})).forEach(f=>{let c=ze(f);i.push(c)})}catch(e){return X("FetchError",e,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerList",[Y.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 u=await this._sdk.FullClient.getOwnedObjects({owner:i,options:{showContent:!0,showType:!0},filter:{StructType:`${r.package_id}::partner::PartnerCap`}}),d;return u.data.forEach(f=>{let c=(0,Y.getObjectFields)(f);c.partner_id===t&&(d=c.id.id,this._sdk.updateCache(e,d))}),d||X("NotFound",new Error("Partner cap not found"),{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[Y.DETAILS_KEYS.REQUEST_PARAMS]:{owner:i,partner_id:t}})}catch(e){return X("FetchError",e,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[Y.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(u=>{let d=(0,Y.getObjectFields)(u);console.log(d),r.push({coin_type:(0,Y.fixCoinType)(d.name,!1),balance:d.value})}),r}catch(t){return X("FetchError",t,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerBalance",[Y.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 X("FetchError",t,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartner",[Y.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:u,versioned_id:d}=(0,Y.getPackagerConfigs)(r);t=t||new ke.Transaction;let f=(0,Y.d)(s).mul(1e4);return f.gt(1e4)?X("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[Y.DETAILS_KEYS.METHOD_NAME]:"updateRefFeeRatePayload",[Y.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(u),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:u}=(0,Y.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(u)],typeArguments:[c]})}),d}updateTimeRangePayload(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{partner_id:e,start_time:s,end_time:u}=i,{global_config_id:d,versioned_id:f}=(0,Y.getPackagerConfigs)(r);t=t||new ke.Transaction;let c=s>1e12?Math.floor(s/1e3):s,p=u>1e12?Math.floor(u/1e3):u;return p<=c?X("InvalidParams",new Error("end_time must be greater than start_time"),{[Y.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[Y.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(Y.CLOCK_ADDRESS)],typeArguments:[]}),t)}createPartnerPayload(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{name:r,ref_fee_rate:e,start_time:s,end_time:u,recipient:d}=i,{partners_id:f,global_config_id:c,versioned_id:p}=(0,Y.getPackagerConfigs)(t),g=new ke.Transaction,y=(0,Y.d)(e).mul(1e4);if(y.gt(1e4))return X("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[Y.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[Y.DETAILS_KEYS.REQUEST_PARAMS]:i});let P=s>1e12?Math.floor(s/1e3):s,S=u>1e12?Math.floor(u/1e3):u;return S<=P?X("InvalidParams",new Error("end_time must be greater than start_time"),{[Y.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[Y.DETAILS_KEYS.REQUEST_PARAMS]:i}):(g.moveCall({target:`${t.published_at}::partner::create_partner`,arguments:[g.object(f),g.pure.string(r),g.pure.u64(y.toNumber()),g.pure.u64(P),g.pure.u64(S),g.pure.address(d),g.object(c),g.object(p)],typeArguments:[]}),g)}};var ti=require("@mysten/sui/transactions"),ct=require("@cetusprotocol/common-sdk"),Hi=Mi(Ne()),ei=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=[],u=e.data.map(p=>p.objectId);u.length>0&&(await this._sdk.FullClient.batchGetObjects(u,{showContent:!0})).forEach(g=>{let y=(0,ct.getObjectFields)(g),P=ct.MathUtil.u128ToI128(new Hi.default(y.value.fields.value.fields.bits)).toString(),S=y.name,B=new Date(Number(S)*1e3).toLocaleString(),k={emissions_per_second:"0",emissions_per_day:"0",emissions_per:ct.MathUtil.fromX64(new Hi.default(P)).toString(),time:S,visualized_time:B};s.push(k)});let d=s.sort((p,g)=>Number(p.time)-Number(g.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 g=d[p];if((0,ct.d)(g.time).lte(r))continue;c=(0,ct.d)(c).add((0,ct.d)(g.emissions_per)).toString();let y=(0,ct.d)(c).mul(3600*24).toString();(0,ct.d)(c).lt(0)?(g.emissions_per_second="0",g.emissions_per_day="0"):(g.emissions_per_second=c,g.emissions_per_day=y),f.push(g)}return f}addRewardPayload(i,t){t=t||new ti.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:u,reward_coin_type:d,reward_amount:f,start_time_seconds:c,end_time_seconds:p,coin_type_a:g,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(u),P,t.pure.option("u64",c),t.pure.u64(p),t.object(e),t.object(s),t.object(ct.CLOCK_ADDRESS)],typeArguments:[g,y,d]}),t}initRewardPayload(i,t){t=t||new ti.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:u,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(u),t.object(e),t.object(s),t.object(ct.CLOCK_ADDRESS)],typeArguments:[f,c,p]})}),t}buildRewardAccessPayload(i,t){t=t||new ti.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:u,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(u),t.object(e),t.object(s)],typeArguments:[f,c]}),t}};var Cr=require("@mysten/sui/transactions"),ie=require("@cetusprotocol/common-sdk");var ii=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}buildRewardWhiteListPayload(i,t){t=t||new Cr.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ie.getPackagerConfigs)(r),{reward_coin_types:u,type:d}=i;return u.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.getDynamicFields({parentId:i}),r=[],e=t.data.map(s=>s.objectId);return e.length>0&&(await this._sdk.FullClient.batchGetObjects(e,{showContent:!0})).forEach(u=>{let f={...(0,ie.getObjectFields)(u).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,u=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:u,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),X("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 u=await this._sdk.FullClient.getObject({id:t.registry_id,options:{showContent:!0}}),d=(0,ie.getObjectFields)(u);return t.pools_id=d.pools.fields.id.id,t}};var Re=class n extends kr.SdkWrapper{constructor(i){super(i),this._pool=new Ce(this),this._position=new Xe(this),this._swap=new Je(this),this._partner=new Ye(this),this._reward=new ei(this),this._config=new ii(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({...Pi,...i}):n.createCustomSDK({...Zi,...i})}static createCustomSDK(i){return new n(i)}};var Kn=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 Rr=Object.create;var De=Object.defineProperty;var Dr=Object.getOwnPropertyDescriptor;var Fr=Object.getOwnPropertyNames;var Nr=Object.getPrototypeOf,qr=Object.prototype.hasOwnProperty;var Lr=(n,i)=>()=>(i||n((i={exports:{}}).exports,i),i.exports),jr=(n,i)=>{for(var t in i)De(n,t,{get:i[t],enumerable:!0})},lr=(n,i,t,r)=>{if(i&&typeof i=="object"||typeof i=="function")for(let e of Fr(i))!qr.call(n,e)&&e!==t&&De(n,e,{get:()=>i[e],enumerable:!(r=Dr(i,e))||r.enumerable});return n};var Mi=(n,i,t)=>(t=n!=null?Rr(Nr(n)):{},lr(i||!n||!n.__esModule?De(t,"default",{value:n,enumerable:!0}):t,n)),Wr=n=>lr(De({},"__esModule",{value:!0}),n);var Ne=Lr((dr,Ai)=>{"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 l=function(){};l.prototype=o.prototype,v.prototype=new l,v.prototype.constructor=v}function e(v,o,l){if(e.isBN(v))return v;this.negative=0,this.words=null,this.length=0,this.red=null,v!==null&&((o==="le"||o==="be")&&(l=o,o=10),this._init(v||0,o||10,l||"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,l){return o.cmp(l)>0?o:l},e.min=function(o,l){return o.cmp(l)<0?o:l},e.prototype._init=function(o,l,h){if(typeof o=="number")return this._initNumber(o,l,h);if(typeof o=="object")return this._initArray(o,l,h);l==="hex"&&(l=16),t(l===(l|0)&&l>=2&&l<=36),o=o.toString().replace(/\s+/g,"");var m=0;o[0]==="-"&&(m++,this.negative=1),m<o.length&&(l===16?this._parseHex(o,m,h):(this._parseBase(o,l,m),h==="le"&&this._initArray(this.toArray(),l,h)))},e.prototype._initNumber=function(o,l,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(),l,h)},e.prototype._initArray=function(o,l,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 u(v,o){var l=v.charCodeAt(o);if(l>=48&&l<=57)return l-48;if(l>=65&&l<=70)return l-55;if(l>=97&&l<=102)return l-87;t(!1,"Invalid character in "+v)}function d(v,o,l){var h=u(v,l);return l-1>=o&&(h|=u(v,l-1)<<4),h}e.prototype._parseHex=function(o,l,h){this.length=Math.ceil((o.length-l)/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>=l;m-=2)A=d(o,l,m)<<b,this.words[M]|=A&67108863,b>=18?(b-=18,M+=1,this.words[M]|=A>>>26):b+=8;else{var _=o.length-l;for(m=_%2===0?l+1:l;m<o.length;m+=2)A=d(o,l,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,l,h){for(var m=0,b=0,M=Math.min(v.length,l),A=o;A<M;A++){var _=v.charCodeAt(A)-48;m*=h,_>=49?b=_-49+10:_>=17?b=_-17+10:b=_,t(_>=0&&b<h,"Invalid character"),m+=b}return m}e.prototype._parseBase=function(o,l,h){this.words=[0],this.length=1;for(var m=0,b=1;b<=67108863;b*=l)m++;m--,b=b/l|0;for(var M=o.length-h,A=M%m,_=Math.min(M,M-A)+h,a=0,w=h;w<_;w+=m)a=f(o,w,w+m,l),this.imuln(b),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a);if(A!==0){var D=1;for(a=f(o,w,o.length,l),w=0;w<A;w++)D*=l;this.imuln(D),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 l=0;l<this.length;l++)o.words[l]=this.words[l];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 g=["","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,l){o=o||10,l=l|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],_=((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=g[6-_.length]+_+h:h=_+h}for(b!==0&&(h=b.toString(16)+h);h.length%l!==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 D=this.clone();for(D.negative=0;!D.isZero();){var L=D.modrn(w).toString(o);D=D.idivn(w),D.isZero()?h=L+h:h=g[a-L.length]+L+h}for(this.isZero()&&(h="0"+h);h.length%l!==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,l){return this.toArrayLike(s,o,l)}),e.prototype.toArray=function(o,l){return this.toArrayLike(Array,o,l)};var S=function(o,l){return o.allocUnsafe?o.allocUnsafe(l):new o(l)};e.prototype.toArrayLike=function(o,l,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=S(o,b),A=l==="le"?"LE":"BE";return this["_toArrayLike"+A](M,m),M},e.prototype._toArrayLikeLE=function(o,l){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,l){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 l=o,h=0;return l>=4096&&(h+=13,l>>>=13),l>=64&&(h+=7,l>>>=7),l>=8&&(h+=4,l>>>=4),l>=2&&(h+=2,l>>>=2),h+l},e.prototype._zeroBits=function(o){if(o===0)return 26;var l=o,h=0;return(l&8191)===0&&(h+=13,l>>>=13),(l&127)===0&&(h+=7,l>>>=7),(l&15)===0&&(h+=4,l>>>=4),(l&3)===0&&(h+=2,l>>>=2),(l&1)===0&&h++,h},e.prototype.bitLength=function(){var o=this.words[this.length-1],l=this._countBits(o);return(this.length-1)*26+l};function B(v){for(var o=new Array(v.bitLength()),l=0;l<o.length;l++){var h=l/26|0,m=l%26;o[l]=v.words[h]>>>m&1}return o}e.prototype.zeroBits=function(){if(this.isZero())return 0;for(var o=0,l=0;l<this.length;l++){var h=this._zeroBits(this.words[l]);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 l=0;l<o.length;l++)this.words[l]=this.words[l]|o.words[l];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 l;this.length>o.length?l=o:l=this;for(var h=0;h<l.length;h++)this.words[h]=this.words[h]&o.words[h];return this.length=l.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 l,h;this.length>o.length?(l=this,h=o):(l=o,h=this);for(var m=0;m<h.length;m++)this.words[m]=l.words[m]^h.words[m];if(this!==l)for(;m<l.length;m++)this.words[m]=l.words[m];return this.length=l.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 l=Math.ceil(o/26)|0,h=o%26;this._expand(l),h>0&&l--;for(var m=0;m<l;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,l){t(typeof o=="number"&&o>=0);var h=o/26|0,m=o%26;return this._expand(h+1),l?this.words[h]=this.words[h]|1<<m:this.words[h]=this.words[h]&~(1<<m),this._strip()},e.prototype.iadd=function(o){var l;if(this.negative!==0&&o.negative===0)return this.negative=0,l=this.isub(o),this.negative^=1,this._normSign();if(this.negative===0&&o.negative!==0)return o.negative=0,l=this.isub(o),o.negative=1,l._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++)l=(h.words[M]|0)+(m.words[M]|0)+b,this.words[M]=l&67108863,b=l>>>26;for(;b!==0&&M<h.length;M++)l=(h.words[M]|0)+b,this.words[M]=l&67108863,b=l>>>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 l;return o.negative!==0&&this.negative===0?(o.negative=0,l=this.sub(o),o.negative^=1,l):o.negative===0&&this.negative!==0?(this.negative=0,l=o.sub(this),this.negative=1,l):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 l=this.iadd(o);return o.negative=1,l._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++)l=(m.words[A]|0)-(b.words[A]|0)+M,M=l>>26,this.words[A]=l&67108863;for(;M!==0&&A<m.length;A++)l=(m.words[A]|0)+M,M=l>>26,this.words[A]=l&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,l){l.negative=o.negative^v.negative;var h=v.length+o.length|0;l.length=h,h=h-1|0;var m=v.words[0]|0,b=o.words[0]|0,M=m*b,A=M&67108863,_=M/67108864|0;l.words[0]=A;for(var a=1;a<h;a++){for(var w=_>>>26,D=_&67108863,L=Math.min(a,o.length-1),W=Math.max(0,a-v.length+1);W<=L;W++){var V=a-W|0;m=v.words[V]|0,b=o.words[W]|0,M=m*b+D,w+=M/67108864|0,D=M&67108863}l.words[a]=D|0,_=w|0}return _!==0?l.words[a]=_|0:l.length--,l._strip()}var k=function(o,l,h){var m=o.words,b=l.words,M=h.words,A=0,_,a,w,D=m[0]|0,L=D&8191,W=D>>>13,V=m[1]|0,nt=V&8191,lt=V>>>13,ue=m[2]|0,ut=ue&8191,dt=ue>>>13,Qi=m[3]|0,ht=Qi&8191,pt=Qi>>>13,Ki=m[4]|0,mt=Ki&8191,_t=Ki>>>13,$i=m[5]|0,gt=$i&8191,bt=$i>>>13,zi=m[6]|0,wt=zi&8191,vt=zi>>>13,Gi=m[7]|0,yt=Gi&8191,Mt=Gi>>>13,Vi=m[8]|0,Pt=Vi&8191,At=Vi>>>13,Xi=m[9]|0,St=Xi&8191,Bt=Xi>>>13,Ji=b[0]|0,Et=Ji&8191,It=Ji>>>13,Yi=b[1]|0,Ot=Yi&8191,xt=Yi>>>13,tr=b[2]|0,Tt=tr&8191,Ct=tr>>>13,er=b[3]|0,kt=er&8191,Rt=er>>>13,ir=b[4]|0,Dt=ir&8191,Ft=ir>>>13,rr=b[5]|0,Nt=rr&8191,qt=rr>>>13,nr=b[6]|0,Lt=nr&8191,jt=nr>>>13,or=b[7]|0,Wt=or&8191,Zt=or>>>13,sr=b[8]|0,Ut=sr&8191,Ht=sr>>>13,ar=b[9]|0,Qt=ar&8191,Kt=ar>>>13;h.negative=o.negative^l.negative,h.length=19,_=Math.imul(L,Et),a=Math.imul(L,It),a=a+Math.imul(W,Et)|0,w=Math.imul(W,It);var ri=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ri>>>26)|0,ri&=67108863,_=Math.imul(nt,Et),a=Math.imul(nt,It),a=a+Math.imul(lt,Et)|0,w=Math.imul(lt,It),_=_+Math.imul(L,Ot)|0,a=a+Math.imul(L,xt)|0,a=a+Math.imul(W,Ot)|0,w=w+Math.imul(W,xt)|0;var ni=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ni>>>26)|0,ni&=67108863,_=Math.imul(ut,Et),a=Math.imul(ut,It),a=a+Math.imul(dt,Et)|0,w=Math.imul(dt,It),_=_+Math.imul(nt,Ot)|0,a=a+Math.imul(nt,xt)|0,a=a+Math.imul(lt,Ot)|0,w=w+Math.imul(lt,xt)|0,_=_+Math.imul(L,Tt)|0,a=a+Math.imul(L,Ct)|0,a=a+Math.imul(W,Tt)|0,w=w+Math.imul(W,Ct)|0;var oi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(oi>>>26)|0,oi&=67108863,_=Math.imul(ht,Et),a=Math.imul(ht,It),a=a+Math.imul(pt,Et)|0,w=Math.imul(pt,It),_=_+Math.imul(ut,Ot)|0,a=a+Math.imul(ut,xt)|0,a=a+Math.imul(dt,Ot)|0,w=w+Math.imul(dt,xt)|0,_=_+Math.imul(nt,Tt)|0,a=a+Math.imul(nt,Ct)|0,a=a+Math.imul(lt,Tt)|0,w=w+Math.imul(lt,Ct)|0,_=_+Math.imul(L,kt)|0,a=a+Math.imul(L,Rt)|0,a=a+Math.imul(W,kt)|0,w=w+Math.imul(W,Rt)|0;var si=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(si>>>26)|0,si&=67108863,_=Math.imul(mt,Et),a=Math.imul(mt,It),a=a+Math.imul(_t,Et)|0,w=Math.imul(_t,It),_=_+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,_=_+Math.imul(ut,Tt)|0,a=a+Math.imul(ut,Ct)|0,a=a+Math.imul(dt,Tt)|0,w=w+Math.imul(dt,Ct)|0,_=_+Math.imul(nt,kt)|0,a=a+Math.imul(nt,Rt)|0,a=a+Math.imul(lt,kt)|0,w=w+Math.imul(lt,Rt)|0,_=_+Math.imul(L,Dt)|0,a=a+Math.imul(L,Ft)|0,a=a+Math.imul(W,Dt)|0,w=w+Math.imul(W,Ft)|0;var ai=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ai>>>26)|0,ai&=67108863,_=Math.imul(gt,Et),a=Math.imul(gt,It),a=a+Math.imul(bt,Et)|0,w=Math.imul(bt,It),_=_+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,_=_+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,_=_+Math.imul(ut,kt)|0,a=a+Math.imul(ut,Rt)|0,a=a+Math.imul(dt,kt)|0,w=w+Math.imul(dt,Rt)|0,_=_+Math.imul(nt,Dt)|0,a=a+Math.imul(nt,Ft)|0,a=a+Math.imul(lt,Dt)|0,w=w+Math.imul(lt,Ft)|0,_=_+Math.imul(L,Nt)|0,a=a+Math.imul(L,qt)|0,a=a+Math.imul(W,Nt)|0,w=w+Math.imul(W,qt)|0;var li=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(li>>>26)|0,li&=67108863,_=Math.imul(wt,Et),a=Math.imul(wt,It),a=a+Math.imul(vt,Et)|0,w=Math.imul(vt,It),_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Dt)|0,a=a+Math.imul(ut,Ft)|0,a=a+Math.imul(dt,Dt)|0,w=w+Math.imul(dt,Ft)|0,_=_+Math.imul(nt,Nt)|0,a=a+Math.imul(nt,qt)|0,a=a+Math.imul(lt,Nt)|0,w=w+Math.imul(lt,qt)|0,_=_+Math.imul(L,Lt)|0,a=a+Math.imul(L,jt)|0,a=a+Math.imul(W,Lt)|0,w=w+Math.imul(W,jt)|0;var ui=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ui>>>26)|0,ui&=67108863,_=Math.imul(yt,Et),a=Math.imul(yt,It),a=a+Math.imul(Mt,Et)|0,w=Math.imul(Mt,It),_=_+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,_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Nt)|0,a=a+Math.imul(ut,qt)|0,a=a+Math.imul(dt,Nt)|0,w=w+Math.imul(dt,qt)|0,_=_+Math.imul(nt,Lt)|0,a=a+Math.imul(nt,jt)|0,a=a+Math.imul(lt,Lt)|0,w=w+Math.imul(lt,jt)|0,_=_+Math.imul(L,Wt)|0,a=a+Math.imul(L,Zt)|0,a=a+Math.imul(W,Wt)|0,w=w+Math.imul(W,Zt)|0;var di=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(di>>>26)|0,di&=67108863,_=Math.imul(Pt,Et),a=Math.imul(Pt,It),a=a+Math.imul(At,Et)|0,w=Math.imul(At,It),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Lt)|0,a=a+Math.imul(ut,jt)|0,a=a+Math.imul(dt,Lt)|0,w=w+Math.imul(dt,jt)|0,_=_+Math.imul(nt,Wt)|0,a=a+Math.imul(nt,Zt)|0,a=a+Math.imul(lt,Wt)|0,w=w+Math.imul(lt,Zt)|0,_=_+Math.imul(L,Ut)|0,a=a+Math.imul(L,Ht)|0,a=a+Math.imul(W,Ut)|0,w=w+Math.imul(W,Ht)|0;var fi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(fi>>>26)|0,fi&=67108863,_=Math.imul(St,Et),a=Math.imul(St,It),a=a+Math.imul(Bt,Et)|0,w=Math.imul(Bt,It),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Wt)|0,a=a+Math.imul(ut,Zt)|0,a=a+Math.imul(dt,Wt)|0,w=w+Math.imul(dt,Zt)|0,_=_+Math.imul(nt,Ut)|0,a=a+Math.imul(nt,Ht)|0,a=a+Math.imul(lt,Ut)|0,w=w+Math.imul(lt,Ht)|0,_=_+Math.imul(L,Qt)|0,a=a+Math.imul(L,Kt)|0,a=a+Math.imul(W,Qt)|0,w=w+Math.imul(W,Kt)|0;var ci=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ci>>>26)|0,ci&=67108863,_=Math.imul(St,Ot),a=Math.imul(St,xt),a=a+Math.imul(Bt,Ot)|0,w=Math.imul(Bt,xt),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Ut)|0,a=a+Math.imul(ut,Ht)|0,a=a+Math.imul(dt,Ut)|0,w=w+Math.imul(dt,Ht)|0,_=_+Math.imul(nt,Qt)|0,a=a+Math.imul(nt,Kt)|0,a=a+Math.imul(lt,Qt)|0,w=w+Math.imul(lt,Kt)|0;var hi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(hi>>>26)|0,hi&=67108863,_=Math.imul(St,Tt),a=Math.imul(St,Ct),a=a+Math.imul(Bt,Tt)|0,w=Math.imul(Bt,Ct),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+Math.imul(ut,Qt)|0,a=a+Math.imul(ut,Kt)|0,a=a+Math.imul(dt,Qt)|0,w=w+Math.imul(dt,Kt)|0;var pi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(pi>>>26)|0,pi&=67108863,_=Math.imul(St,kt),a=Math.imul(St,Rt),a=a+Math.imul(Bt,kt)|0,w=Math.imul(Bt,Rt),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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,_=_+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 mi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(mi>>>26)|0,mi&=67108863,_=Math.imul(St,Dt),a=Math.imul(St,Ft),a=a+Math.imul(Bt,Dt)|0,w=Math.imul(Bt,Ft),_=_+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,_=_+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,_=_+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,_=_+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,_=_+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 _i=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(_i>>>26)|0,_i&=67108863,_=Math.imul(St,Nt),a=Math.imul(St,qt),a=a+Math.imul(Bt,Nt)|0,w=Math.imul(Bt,qt),_=_+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,_=_+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,_=_+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,_=_+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 gi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(gi>>>26)|0,gi&=67108863,_=Math.imul(St,Lt),a=Math.imul(St,jt),a=a+Math.imul(Bt,Lt)|0,w=Math.imul(Bt,jt),_=_+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,_=_+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,_=_+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 bi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(bi>>>26)|0,bi&=67108863,_=Math.imul(St,Wt),a=Math.imul(St,Zt),a=a+Math.imul(Bt,Wt)|0,w=Math.imul(Bt,Zt),_=_+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,_=_+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 wi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(wi>>>26)|0,wi&=67108863,_=Math.imul(St,Ut),a=Math.imul(St,Ht),a=a+Math.imul(Bt,Ut)|0,w=Math.imul(Bt,Ht),_=_+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 vi=(A+_|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(vi>>>26)|0,vi&=67108863,_=Math.imul(St,Qt),a=Math.imul(St,Kt),a=a+Math.imul(Bt,Qt)|0,w=Math.imul(Bt,Kt);var yi=(A+_|0)+((a&8191)<<13)|0;return A=(w+(a>>>13)|0)+(yi>>>26)|0,yi&=67108863,M[0]=ri,M[1]=ni,M[2]=oi,M[3]=si,M[4]=ai,M[5]=li,M[6]=ui,M[7]=di,M[8]=fi,M[9]=ci,M[10]=hi,M[11]=pi,M[12]=mi,M[13]=_i,M[14]=gi,M[15]=bi,M[16]=wi,M[17]=vi,M[18]=yi,A!==0&&(M[19]=A,h.length++),h};Math.imul||(k=C);function R(v,o,l){l.negative=o.negative^v.negative,l.length=v.length+o.length;for(var h=0,m=0,b=0;b<l.length-1;b++){var M=m;m=0;for(var A=h&67108863,_=Math.min(b,o.length-1),a=Math.max(0,b-v.length+1);a<=_;a++){var w=b-a,D=v.words[w]|0,L=o.words[a]|0,W=D*L,V=W&67108863;M=M+(W/67108864|0)|0,V=V+A|0,A=V&67108863,M=M+(V>>>26)|0,m+=M>>>26,M&=67108863}l.words[b]=A,h=M,M=m}return h!==0?l.words[b]=h:l.length--,l._strip()}function I(v,o,l){return R(v,o,l)}e.prototype.mulTo=function(o,l){var h,m=this.length+o.length;return this.length===10&&o.length===10?h=k(this,o,l):m<63?h=C(this,o,l):m<1024?h=R(this,o,l):h=I(this,o,l),h};function x(v,o){this.x=v,this.y=o}x.prototype.makeRBT=function(o){for(var l=new Array(o),h=e.prototype._countBits(o)-1,m=0;m<o;m++)l[m]=this.revBin(m,h,o);return l},x.prototype.revBin=function(o,l,h){if(o===0||o===h-1)return o;for(var m=0,b=0;b<l;b++)m|=(o&1)<<l-b-1,o>>=1;return m},x.prototype.permute=function(o,l,h,m,b,M){for(var A=0;A<M;A++)m[A]=l[o[A]],b[A]=h[o[A]]},x.prototype.transform=function(o,l,h,m,b,M){this.permute(M,o,l,h,m,b);for(var A=1;A<b;A<<=1)for(var _=A<<1,a=Math.cos(2*Math.PI/_),w=Math.sin(2*Math.PI/_),D=0;D<b;D+=_)for(var L=a,W=w,V=0;V<A;V++){var nt=h[D+V],lt=m[D+V],ue=h[D+V+A],ut=m[D+V+A],dt=L*ue-W*ut;ut=L*ut+W*ue,ue=dt,h[D+V]=nt+ue,m[D+V]=lt+ut,h[D+V+A]=nt-ue,m[D+V+A]=lt-ut,V!==_&&(dt=a*L-w*W,W=a*W+w*L,L=dt)}},x.prototype.guessLen13b=function(o,l){var h=Math.max(l,o)|1,m=h&1,b=0;for(h=h/2|0;h;h=h>>>1)b++;return 1<<b+1+m},x.prototype.conjugate=function(o,l,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=l[m],l[m]=-l[h-m-1],l[h-m-1]=-b}},x.prototype.normalize13b=function(o,l){for(var h=0,m=0;m<l/2;m++){var b=Math.round(o[2*m+1]/l)*8192+Math.round(o[2*m]/l)+h;o[m]=b&67108863,b<67108864?h=0:h=b/67108864|0}return o},x.prototype.convert13b=function(o,l,h,m){for(var b=0,M=0;M<l;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*l;M<m;++M)h[M]=0;t(b===0),t((b&-8192)===0)},x.prototype.stub=function(o){for(var l=new Array(o),h=0;h<o;h++)l[h]=0;return l},x.prototype.mulp=function(o,l,h){var m=2*this.guessLen13b(o.length,l.length),b=this.makeRBT(m),M=this.stub(m),A=new Array(m),_=new Array(m),a=new Array(m),w=new Array(m),D=new Array(m),L=new Array(m),W=h.words;W.length=m,this.convert13b(o.words,o.length,A,m),this.convert13b(l.words,l.length,w,m),this.transform(A,M,_,a,m,b),this.transform(w,M,D,L,m,b);for(var V=0;V<m;V++){var nt=_[V]*D[V]-a[V]*L[V];a[V]=_[V]*L[V]+a[V]*D[V],_[V]=nt}return this.conjugate(_,a,m),this.transform(_,a,W,M,m,b),this.conjugate(W,M,m),this.normalize13b(W,m),h.negative=o.negative^l.negative,h.length=o.length+l.length,h._strip()},e.prototype.mul=function(o){var l=new e(null);return l.words=new Array(this.length+o.length),this.mulTo(o,l)},e.prototype.mulf=function(o){var l=new e(null);return l.words=new Array(this.length+o.length),I(this,o,l)},e.prototype.imul=function(o){return this.clone().mulTo(o,this)},e.prototype.imuln=function(o){var l=o<0;l&&(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,l?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 l=B(o);if(l.length===0)return new e(1);for(var h=this,m=0;m<l.length&&l[m]===0;m++,h=h.sqr());if(++m<l.length)for(var b=h.sqr();m<l.length;m++,b=b.sqr())l[m]!==0&&(h=h.mul(b));return h},e.prototype.iushln=function(o){t(typeof o=="number"&&o>=0);var l=o%26,h=(o-l)/26,m=67108863>>>26-l<<26-l,b;if(l!==0){var M=0;for(b=0;b<this.length;b++){var A=this.words[b]&m,_=(this.words[b]|0)-A<<l;this.words[b]=_|M,M=A>>>26-l}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,l,h){t(typeof o=="number"&&o>=0);var m;l?m=(l-l%26)/26:m=0;var b=o%26,M=Math.min((o-b)/26,this.length),A=67108863^67108863>>>b<<b,_=h;if(m-=M,m=Math.max(0,m),_){for(var a=0;a<M;a++)_.words[a]=this.words[a];_.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 D=this.words[a]|0;this.words[a]=w<<26-b|D>>>b,w=D&A}return _&&w!==0&&(_.words[_.length++]=w),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},e.prototype.ishrn=function(o,l,h){return t(this.negative===0),this.iushrn(o,l,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 l=o%26,h=(o-l)/26,m=1<<l;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 l=o%26,h=(o-l)/26;if(t(this.negative===0,"imaskn works only with positive numbers"),this.length<=h)return this;if(l!==0&&h++,this.length=Math.min(h,this.length),l!==0){var m=67108863^67108863>>>l<<l;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 l=0;l<this.length&&this.words[l]>=67108864;l++)this.words[l]-=67108864,l===this.length-1?this.words[l+1]=1:this.words[l+1]++;return this.length=Math.max(this.length,l+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 l=0;l<this.length&&this.words[l]<0;l++)this.words[l]+=67108864,this.words[l+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,l,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 _=(o.words[b]|0)*l;M-=_&67108863,A=(M>>26)-(_/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,l){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 _=m.length-b.length,a;if(l!=="mod"){a=new e(null),a.length=_+1,a.words=new Array(a.length);for(var w=0;w<a.length;w++)a.words[w]=0}var D=m.clone()._ishlnsubmul(b,1,_);D.negative===0&&(m=D,a&&(a.words[_]=1));for(var L=_-1;L>=0;L--){var W=(m.words[b.length+L]|0)*67108864+(m.words[b.length+L-1]|0);for(W=Math.min(W/M|0,67108863),m._ishlnsubmul(b,W,L);m.negative!==0;)W--,m.negative=0,m._ishlnsubmul(b,1,L),m.isZero()||(m.negative^=1);a&&(a.words[L]=W)}return a&&a._strip(),m._strip(),l!=="div"&&h!==0&&m.iushrn(h),{div:a||null,mod:m}},e.prototype.divmod=function(o,l,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,l),l!=="mod"&&(m=M.div.neg()),l!=="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(),l),l!=="mod"&&(m=M.div.neg()),{div:m,mod:M.mod}):(this.negative&o.negative)!==0?(M=this.neg().divmod(o.neg(),l),l!=="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?l==="div"?{div:this.divn(o.words[0]),mod:null}:l==="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,l)},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 l=this.divmod(o);if(l.mod.isZero())return l.div;var h=l.div.negative!==0?l.mod.isub(o):l.mod,m=o.ushrn(1),b=o.andln(1),M=h.cmp(m);return M<0||b===1&&M===0?l.div:l.div.negative!==0?l.div.isubn(1):l.div.iaddn(1)},e.prototype.modrn=function(o){var l=o<0;l&&(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 l?-m:m},e.prototype.modn=function(o){return this.modrn(o)},e.prototype.idivn=function(o){var l=o<0;l&&(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(),l?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 l=this,h=o.clone();l.negative!==0?l=l.umod(o):l=l.clone();for(var m=new e(1),b=new e(0),M=new e(0),A=new e(1),_=0;l.isEven()&&h.isEven();)l.iushrn(1),h.iushrn(1),++_;for(var a=h.clone(),w=l.clone();!l.isZero();){for(var D=0,L=1;(l.words[0]&L)===0&&D<26;++D,L<<=1);if(D>0)for(l.iushrn(D);D-- >0;)(m.isOdd()||b.isOdd())&&(m.iadd(a),b.isub(w)),m.iushrn(1),b.iushrn(1);for(var W=0,V=1;(h.words[0]&V)===0&&W<26;++W,V<<=1);if(W>0)for(h.iushrn(W);W-- >0;)(M.isOdd()||A.isOdd())&&(M.iadd(a),A.isub(w)),M.iushrn(1),A.iushrn(1);l.cmp(h)>=0?(l.isub(h),m.isub(M),b.isub(A)):(h.isub(l),M.isub(m),A.isub(b))}return{a:M,b:A,gcd:h.iushln(_)}},e.prototype._invmp=function(o){t(o.negative===0),t(!o.isZero());var l=this,h=o.clone();l.negative!==0?l=l.umod(o):l=l.clone();for(var m=new e(1),b=new e(0),M=h.clone();l.cmpn(1)>0&&h.cmpn(1)>0;){for(var A=0,_=1;(l.words[0]&_)===0&&A<26;++A,_<<=1);if(A>0)for(l.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);l.cmp(h)>=0?(l.isub(h),m.isub(b)):(h.isub(l),b.isub(m))}var D;return l.cmpn(1)===0?D=m:D=b,D.cmpn(0)<0&&D.iadd(o),D},e.prototype.gcd=function(o){if(this.isZero())return o.abs();if(o.isZero())return this.abs();var l=this.clone(),h=o.clone();l.negative=0,h.negative=0;for(var m=0;l.isEven()&&h.isEven();m++)l.iushrn(1),h.iushrn(1);do{for(;l.isEven();)l.iushrn(1);for(;h.isEven();)h.iushrn(1);var b=l.cmp(h);if(b<0){var M=l;l=h,h=M}else if(b===0||h.cmpn(1)===0)break;l.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 l=o%26,h=(o-l)/26,m=1<<l;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 l=o<0;if(this.negative!==0&&!l)return-1;if(this.negative===0&&l)return 1;this._strip();var h;if(this.length>1)h=1;else{l&&(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 l=this.ucmp(o);return this.negative!==0?-l|0:l},e.prototype.ucmp=function(o){if(this.length>o.length)return 1;if(this.length<o.length)return-1;for(var l=0,h=this.length-1;h>=0;h--){var m=this.words[h]|0,b=o.words[h]|0;if(m!==b){m<b?l=-1:m>b&&(l=1);break}}return l},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 K(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 Z(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()}Z.prototype._tmp=function(){var o=new e(null);return o.words=new Array(Math.ceil(this.n/13)),o},Z.prototype.ireduce=function(o){var l=o,h;do this.split(l,this.tmp),l=this.imulK(l),l=l.iadd(this.tmp),h=l.bitLength();while(h>this.n);var m=h<this.n?-1:l.ucmp(this.p);return m===0?(l.words[0]=0,l.length=1):m>0?l.isub(this.p):l.strip!==void 0?l.strip():l._strip(),l},Z.prototype.split=function(o,l){o.iushrn(this.n,0,l)},Z.prototype.imulK=function(o){return o.imul(this.k)};function J(){Z.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}r(J,Z),J.prototype.split=function(o,l){for(var h=4194303,m=Math.min(o.length,9),b=0;b<m;b++)l.words[b]=o.words[b];if(l.length=m,o.length<=9){o.words[0]=0,o.length=1;return}var M=o.words[9];for(l.words[l.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},J.prototype.imulK=function(o){o.words[o.length]=0,o.words[o.length+1]=0,o.length+=2;for(var l=0,h=0;h<o.length;h++){var m=o.words[h]|0;l+=m*977,o.words[h]=l&67108863,l=m*64+(l/67108864|0)}return o.words[o.length-1]===0&&(o.length--,o.words[o.length-1]===0&&o.length--),o};function it(){Z.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}r(it,Z);function tt(){Z.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}r(tt,Z);function ot(){Z.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}r(ot,Z),ot.prototype.imulK=function(o){for(var l=0,h=0;h<o.length;h++){var m=(o.words[h]|0)*19+l,b=m&67108863;m>>>=26,o.words[h]=b,l=m}return l!==0&&(o.words[o.length++]=l),o},e._prime=function(o){if(z[o])return z[o];var l;if(o==="k256")l=new J;else if(o==="p224")l=new it;else if(o==="p192")l=new tt;else if(o==="p25519")l=new ot;else throw new Error("Unknown prime "+o);return z[o]=l,l};function K(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}K.prototype._verify1=function(o){t(o.negative===0,"red works only with positives"),t(o.red,"red works only with red numbers")},K.prototype._verify2=function(o,l){t((o.negative|l.negative)===0,"red works only with positives"),t(o.red&&o.red===l.red,"red works only with red numbers")},K.prototype.imod=function(o){return this.prime?this.prime.ireduce(o)._forceRed(this):(c(o,o.umod(this.m)._forceRed(this)),o)},K.prototype.neg=function(o){return o.isZero()?o.clone():this.m.sub(o)._forceRed(this)},K.prototype.add=function(o,l){this._verify2(o,l);var h=o.add(l);return h.cmp(this.m)>=0&&h.isub(this.m),h._forceRed(this)},K.prototype.iadd=function(o,l){this._verify2(o,l);var h=o.iadd(l);return h.cmp(this.m)>=0&&h.isub(this.m),h},K.prototype.sub=function(o,l){this._verify2(o,l);var h=o.sub(l);return h.cmpn(0)<0&&h.iadd(this.m),h._forceRed(this)},K.prototype.isub=function(o,l){this._verify2(o,l);var h=o.isub(l);return h.cmpn(0)<0&&h.iadd(this.m),h},K.prototype.shl=function(o,l){return this._verify1(o),this.imod(o.ushln(l))},K.prototype.imul=function(o,l){return this._verify2(o,l),this.imod(o.imul(l))},K.prototype.mul=function(o,l){return this._verify2(o,l),this.imod(o.mul(l))},K.prototype.isqr=function(o){return this.imul(o,o.clone())},K.prototype.sqr=function(o){return this.mul(o,o)},K.prototype.sqrt=function(o){if(o.isZero())return o.clone();var l=this.m.andln(3);if(t(l%2===1),l===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(),_=this.m.subn(1).iushrn(1),a=this.m.bitLength();for(a=new e(2*a*a).toRed(this);this.pow(a,_).cmp(A)!==0;)a.redIAdd(A);for(var w=this.pow(a,m),D=this.pow(o,m.addn(1).iushrn(1)),L=this.pow(o,m),W=b;L.cmp(M)!==0;){for(var V=L,nt=0;V.cmp(M)!==0;nt++)V=V.redSqr();t(nt<W);var lt=this.pow(w,new e(1).iushln(W-nt-1));D=D.redMul(lt),w=lt.redSqr(),L=L.redMul(w),W=nt}return D},K.prototype.invm=function(o){var l=o._invmp(this.m);return l.negative!==0?(l.negative=0,this.imod(l).redNeg()):this.imod(l)},K.prototype.pow=function(o,l){if(l.isZero())return new e(1).toRed(this);if(l.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,_=0,a=l.bitLength()%26;for(a===0&&(a=26),b=l.length-1;b>=0;b--){for(var w=l.words[b],D=a-1;D>=0;D--){var L=w>>D&1;if(M!==m[0]&&(M=this.sqr(M)),L===0&&A===0){_=0;continue}A<<=1,A|=L,_++,!(_!==h&&(b!==0||D!==0))&&(M=this.mul(M,m[A]),_=0,A=0)}a=26}return M},K.prototype.convertTo=function(o){var l=o.umod(this.m);return l===o?l.clone():l},K.prototype.convertFrom=function(o){var l=o.clone();return l.red=null,l},e.mont=function(o){return new st(o)};function st(v){K.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(st,K),st.prototype.convertTo=function(o){return this.imod(o.ushln(this.shift))},st.prototype.convertFrom=function(o){var l=this.imod(o.mul(this.rinv));return l.red=null,l},st.prototype.imul=function(o,l){if(o.isZero()||l.isZero())return o.words[0]=0,o.length=1,o;var h=o.imul(l),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)},st.prototype.mul=function(o,l){if(o.isZero()||l.isZero())return new e(0)._forceRed(this);var h=o.mul(l),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)},st.prototype.invm=function(o){var l=this.imod(o._invmp(this.m).mul(this.r2));return l._forceRed(this)}})(typeof Ai>"u"||Ai,dr)});var $n={};jr($n,{BASIS_POINT:()=>te,BASIS_POINT_MAX:()=>ve,BIN_BOUND:()=>ge,BinUtils:()=>F,CetusDlmmSDK:()=>Re,DEFAULT_MAX_WEIGHT:()=>ye,DEFAULT_MIN_WEIGHT:()=>_e,FEE_PRECISION:()=>xi,FeeUtils:()=>Oe,IlmUtils:()=>ji,MAX_BIN_ID:()=>Nn,MAX_BIN_PER_POSITION:()=>le,MAX_FEE_RATE:()=>Ke,MIN_BIN_ID:()=>Fn,ONE:()=>$e,PoolModule:()=>Ce,REWARD_PERIOD:()=>qn,REWARD_PERIOD_START_AT:()=>Ln,SCALE_OFFSET:()=>N,StrategyType:()=>Ee,StrategyUtils:()=>Ae,WeightUtils:()=>ee,buildPoolKey:()=>Li,default:()=>Kn,dlmmMainnet:()=>Pi,dlmmTestnet:()=>Zi,generateRewardSchedule:()=>Un,getRouterModule:()=>qi,parseBinInfo:()=>ki,parseBinInfoList:()=>Ci,parseCurrentRewardPeriodEmission:()=>Qn,parseDlmmBasePool:()=>Ti,parseDlmmPool:()=>Ie,parseDlmmPosition:()=>Ge,parseLiquidityShares:()=>Wn,parsePartner:()=>ze,parsePoolTransactionInfo:()=>Ni,parseRewardPeriodEmission:()=>Hn,parseStrategyType:()=>Zn,parsedDlmmPosFeeData:()=>Ri,parsedDlmmPosRewardData:()=>Di,parsedSwapQuoteData:()=>Fi,poolFilterEvenTypes:()=>Ir,safeAmount:()=>Pe,safeMulAmount:()=>ne});module.exports=Wr($n);var kr=require("@cetusprotocol/common-sdk");var Fe=require("@cetusprotocol/common-sdk"),Pi={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"),et=require("@cetusprotocol/common-sdk");var ur=require("@cetusprotocol/common-sdk");var me=class extends ur.BaseError{constructor(i,t,r){super(i,t||"UnknownError",r)}static isDlmmErrorCode(i,t){return this.isErrorCode(i,t)}},X=(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=Mi(Ne());var be=9e15,he=1e9,Si="0123456789abcdef",je="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",We="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Bi={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-be,maxE:be,crypto:!1},pr,ae,$=!0,Ue="[DecimalError] ",ce=Ue+"Invalid argument: ",mr=Ue+"Precision limit exceeded",_r=Ue+"crypto unavailable",gr="[object Decimal]",Vt=Math.floor,$t=Math.pow,Zr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Ur=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Hr=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,br=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,re=1e7,Q=7,Qr=9007199254740991,Kr=je.length-1,Ei=We.length-1,E={toStringTag:gr};E.absoluteValue=E.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),j(n)};E.ceil=function(){return j(new this.constructor(this),this.e+1,2)};E.clampedTo=E.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)};E.comparedTo=E.cmp=function(n){var i,t,r,e,s=this,u=s.d,d=(n=new s.constructor(n)).d,f=s.s,c=n.s;if(!u||!d)return!f||!c?NaN:f!==c?f:u===d?0:!u^f<0?1:-1;if(!u[0]||!d[0])return u[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=u.length,e=d.length,i=0,t=r<e?r:e;i<t;++i)if(u[i]!==d[i])return u[i]>d[i]^f<0?1:-1;return r===e?0:r>e^f<0?1:-1};E.cosine=E.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())+Q,r.rounding=1,t=$r(r,Pr(r,t)),r.precision=n,r.rounding=i,j(ae==2||ae==3?t.neg():t,n,i,!0)):new r(1):new r(NaN)};E.cubeRoot=E.cbrt=function(){var n,i,t,r,e,s,u,d,f,c,p=this,g=p.constructor;if(!p.isFinite()||p.isZero())return new g(p);for($=!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 g(t),r.s=p.s):r=new g(s.toString()),u=(n=g.precision)+3;;)if(d=r,f=d.times(d).times(d),c=f.plus(p),r=at(c.plus(p).times(d),c.plus(f),u+2,1),zt(d.d).slice(0,u)===(t=zt(r.d)).slice(0,u))if(t=t.slice(u-3,u+1),t=="9999"||!e&&t=="4999"){if(!e&&(j(d,n+1,0),d.times(d).times(d).eq(p))){r=d;break}u+=4,e=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(j(r,n+1,1),i=!r.times(r).times(r).eq(p));break}return $=!0,j(r,n,g.rounding,i)};E.decimalPlaces=E.dp=function(){var n,i=this.d,t=NaN;if(i){if(n=i.length-1,t=(n-Vt(this.e/Q))*Q,n=i[n],n)for(;n%10==0;n/=10)t--;t<0&&(t=0)}return t};E.dividedBy=E.div=function(n){return at(this,new this.constructor(n))};E.dividedToIntegerBy=E.divToInt=function(n){var i=this,t=i.constructor;return j(at(i,new t(n),0,1,1),t.precision,t.rounding)};E.equals=E.eq=function(n){return this.cmp(n)===0};E.floor=function(){return j(new this.constructor(this),this.e+1,3)};E.greaterThan=E.gt=function(n){return this.cmp(n)>0};E.greaterThanOrEqualTo=E.gte=function(n){var i=this.cmp(n);return i==1||i===0};E.hyperbolicCosine=E.cosh=function(){var n,i,t,r,e,s=this,u=s.constructor,d=new u(1);if(!s.isFinite())return new u(s.s?1/0:NaN);if(s.isZero())return d;t=u.precision,r=u.rounding,u.precision=t+Math.max(s.e,s.sd())+4,u.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(u,1,s.times(i),new u(1),!0);for(var f,c=n,p=new u(8);c--;)f=s.times(s),s=d.minus(f.times(p.minus(f.times(p))));return j(s,u.precision=t,u.rounding=r,!0)};E.hyperbolicSine=E.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 u,d=new s(5),f=new s(16),c=new s(20);n--;)u=e.times(e),e=e.times(d.plus(u.times(f.times(u).plus(c))))}return s.precision=i,s.rounding=t,j(e,i,t,!0)};E.hyperbolicTangent=E.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,at(t.sinh(),t.cosh(),r.precision=n,r.rounding=i)):new r(t.s)};E.inverseCosine=E.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))};E.inverseHyperbolicCosine=E.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,$=!1,t=t.times(t).minus(1).sqrt().plus(t),$=!0,r.precision=n,r.rounding=i,t.ln()):new r(t)};E.inverseHyperbolicSine=E.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,$=!1,t=t.times(t).plus(1).sqrt().plus(t),$=!0,r.precision=n,r.rounding=i,t.ln())};E.inverseHyperbolicTangent=E.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?j(new s(e),n,i,!0):(s.precision=t=r-e.e,e=at(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)};E.inverseSine=E.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)))};E.inverseTangent=E.atan=function(){var n,i,t,r,e,s,u,d,f,c=this,p=c.constructor,g=p.precision,y=p.rounding;if(c.isFinite()){if(c.isZero())return new p(c);if(c.abs().eq(1)&&g+4<=Ei)return u=oe(p,g+4,y).times(.25),u.s=c.s,u}else{if(!c.s)return new p(NaN);if(g+4<=Ei)return u=oe(p,g+4,y).times(.5),u.s=c.s,u}for(p.precision=d=g+10,p.rounding=1,t=Math.min(28,d/Q+2|0),n=t;n;--n)c=c.div(c.times(c).plus(1).sqrt().plus(1));for($=!1,i=Math.ceil(d/Q),r=1,f=c.times(c),u=new p(c),e=c;n!==-1;)if(e=e.times(f),s=u.minus(e.div(r+=2)),e=e.times(f),u=s.plus(e.div(r+=2)),u.d[i]!==void 0)for(n=i;u.d[n]===s.d[n]&&n--;);return t&&(u=u.times(2<<t-1)),$=!0,j(u,p.precision=g,p.rounding=y,!0)};E.isFinite=function(){return!!this.d};E.isInteger=E.isInt=function(){return!!this.d&&Vt(this.e/Q)>this.d.length-2};E.isNaN=function(){return!this.s};E.isNegative=E.isNeg=function(){return this.s<0};E.isPositive=E.isPos=function(){return this.s>0};E.isZero=function(){return!!this.d&&this.d[0]===0};E.lessThan=E.lt=function(n){return this.cmp(n)<0};E.lessThanOrEqualTo=E.lte=function(n){return this.cmp(n)<1};E.logarithm=E.log=function(n){var i,t,r,e,s,u,d,f,c=this,p=c.constructor,g=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($=!1,d=g+P,u=fe(c,d),r=i?Ze(p,d+10):fe(n,d),f=at(u,r,d,1),Se(f.d,e=g,y))do if(d+=10,u=fe(c,d),r=i?Ze(p,d+10):fe(n,d),f=at(u,r,d,1),!s){+zt(f.d).slice(e+1,e+15)+1==1e14&&(f=j(f,g+1,0));break}while(Se(f.d,e+=10,y));return $=!0,j(f,g,y)};E.minus=E.sub=function(n){var i,t,r,e,s,u,d,f,c,p,g,y,P=this,S=P.constructor;if(n=new S(n),!P.d||!n.d)return!P.s||!n.s?n=new S(NaN):P.d?n.s=-n.s:n=new S(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=S.precision,f=S.rounding,!c[0]||!y[0]){if(y[0])n.s=-n.s;else if(c[0])n=new S(P);else return new S(f===3?-0:0);return $?j(n,d,f):n}if(t=Vt(n.e/Q),p=Vt(P.e/Q),c=c.slice(),s=p-t,s){for(g=s<0,g?(i=c,s=-s,u=y.length):(i=y,t=p,u=c.length),r=Math.max(Math.ceil(d/Q),u)+2,s>r&&(s=r,i.length=1),i.reverse(),r=s;r--;)i.push(0);i.reverse()}else{for(r=c.length,u=y.length,g=r<u,g&&(u=r),r=0;r<u;r++)if(c[r]!=y[r]){g=c[r]<y[r];break}s=0}for(g&&(i=c,c=y,y=i,n.s=-n.s),u=c.length,r=y.length-u;r>0;--r)c[u++]=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[--u]===0;)c.pop();for(;c[0]===0;c.shift())--t;return c[0]?(n.d=c,n.e=He(c,t),$?j(n,d,f):n):new S(f===3?-0:0)};E.modulo=E.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]?j(new r(t),r.precision,r.rounding):($=!1,r.modulo==9?(i=at(t,n.abs(),0,3,1),i.s*=n.s):i=at(t,n,0,r.modulo,1),i=i.times(n),$=!0,t.minus(i))};E.naturalExponential=E.exp=function(){return Ii(this)};E.naturalLogarithm=E.ln=function(){return fe(this)};E.negated=E.neg=function(){var n=new this.constructor(this);return n.s=-n.s,j(n)};E.plus=E.add=function(n){var i,t,r,e,s,u,d,f,c,p,g=this,y=g.constructor;if(n=new y(n),!g.d||!n.d)return!g.s||!n.s?n=new y(NaN):g.d||(n=new y(n.d||g.s===n.s?g:NaN)),n;if(g.s!=n.s)return n.s=-n.s,g.minus(n);if(c=g.d,p=n.d,d=y.precision,f=y.rounding,!c[0]||!p[0])return p[0]||(n=new y(g)),$?j(n,d,f):n;if(s=Vt(g.e/Q),r=Vt(n.e/Q),c=c.slice(),e=s-r,e){for(e<0?(t=c,e=-e,u=p.length):(t=p,r=s,u=c.length),s=Math.ceil(d/Q),u=s>u?s+1:u+1,e>u&&(e=u,t.length=1),t.reverse();e--;)t.push(0);t.reverse()}for(u=c.length,e=p.length,u-e<0&&(e=u,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),u=c.length;c[--u]==0;)c.pop();return n.d=c,n.e=He(c,r),$?j(n,d,f):n};E.precision=E.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=wr(t.d),n&&t.e+1>i&&(i=t.e+1)):i=NaN,i};E.round=function(){var n=this,i=n.constructor;return j(new i(n),n.e+1,i.rounding)};E.sine=E.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())+Q,r.rounding=1,t=Gr(r,Pr(r,t)),r.precision=n,r.rounding=i,j(ae>2?t.neg():t,n,i,!0)):new r(NaN)};E.squareRoot=E.sqrt=function(){var n,i,t,r,e,s,u=this,d=u.d,f=u.e,c=u.s,p=u.constructor;if(c!==1||!d||!d[0])return new p(!c||c<0&&(!d||d[0])?NaN:d?u:1/0);for($=!1,c=Math.sqrt(+u),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(at(u,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&&(j(s,f+1,0),s.times(s).eq(u))){r=s;break}t+=4,e=1}else{(!+i||!+i.slice(1)&&i.charAt(0)=="5")&&(j(r,f+1,1),n=!r.times(r).eq(u));break}return $=!0,j(r,f,p.rounding,n)};E.tangent=E.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=at(t,new r(1).minus(t.times(t)).sqrt(),n+10,0),r.precision=n,r.rounding=i,j(ae==2||ae==4?t.neg():t,n,i,!0)):new r(NaN)};E.times=E.mul=function(n){var i,t,r,e,s,u,d,f,c,p=this,g=p.constructor,y=p.d,P=(n=new g(n)).d;if(n.s*=p.s,!y||!y[0]||!P||!P[0])return new g(!n.s||y&&!y[0]&&!P||P&&!P[0]&&!y?NaN:!y||!P?n.s/0:n.s*0);for(t=Vt(p.e/Q)+Vt(n.e/Q),f=y.length,c=P.length,f<c&&(s=y,y=P,P=s,u=f,f=c,c=u),s=[],u=f+c,r=u;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[--u];)s.pop();return i?++t:s.shift(),n.d=s,n.e=He(s,t),$?j(n,g.precision,g.rounding):n};E.toBinary=function(n,i){return Oi(this,2,n,i)};E.toDecimalPlaces=E.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),j(t,n+t.e+1,i))};E.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=j(new e(r),n+1,i),t=se(r,!0,n+1)),r.isNeg()&&!r.isZero()?"-"+t:t};E.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=j(new s(e),n+e.e+1,i),t=se(r,!1,n+r.e+1)),e.isNeg()&&!e.isZero()?"-"+t:t};E.toFraction=function(n){var i,t,r,e,s,u,d,f,c,p,g,y,P=this,S=P.d,B=P.constructor;if(!S)return new B(P);if(c=t=new B(1),r=f=new B(0),i=new B(r),s=i.e=wr(S)-P.e-1,u=s%Q,i.d[0]=$t(10,u<0?Q+u:u),n==null)n=s>0?i:c;else{if(d=new B(n),!d.isInt()||d.lt(c))throw Error(ce+d);n=d.gt(i)?s>0?i:c:d}for($=!1,d=new B(zt(S)),p=B.precision,B.precision=s=S.length*Q*2;g=at(d,i,0,1,1),e=t.plus(g.times(r)),e.cmp(n)!=1;)t=r,r=e,e=c,c=f.plus(g.times(e)),f=e,e=i,i=d.minus(g.times(e)),d=e;return e=at(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=at(c,r,s,1).minus(P).abs().cmp(at(f,t,s,1).minus(P).abs())<1?[c,r]:[f,t],B.precision=p,$=!0,y};E.toHexadecimal=E.toHex=function(n,i){return Oi(this,16,n,i)};E.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]?($=!1,t=at(t,n,0,i,1).times(n),$=!0,j(t)):(n.s=t.s,t=n),t};E.toNumber=function(){return+this};E.toOctal=function(n,i){return Oi(this,8,n,i)};E.toPower=E.pow=function(n){var i,t,r,e,s,u,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 j(d,r,s);if(i=Vt(n.e/Q),i>=n.d.length-1&&(t=c<0?-c:c)<=Qr)return e=vr(f,d,t,r),n.s<0?new f(1).div(e):j(e,r,s);if(u=d.s,u<0){if(i<n.d.length-1)return new f(NaN);if((n.d[i]&1)==0&&(u=1),d.e==0&&d.d[0]==1&&d.d.length==1)return d.s=u,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?u/0:0):($=!1,f.rounding=d.s=1,t=Math.min(12,(i+"").length),e=Ii(n.times(fe(d,r+t)),r),e.d&&(e=j(e,r+5,1),Se(e.d,r,s)&&(i=r+10,e=j(Ii(n.times(fe(d,i+t)),i),i+5,1),+zt(e.d).slice(r+1,r+15)+1==1e14&&(e=j(e,r+1,0)))),e.s=u,$=!0,f.rounding=s,j(e,r,s))};E.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=j(new e(r),n,i),t=se(r,n<=r.e||r.e<=e.toExpNeg,n)),r.isNeg()&&!r.isZero()?"-"+t:t};E.toSignificantDigits=E.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)),j(new r(t),n,i)};E.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};E.truncated=E.trunc=function(){return j(new this.constructor(this),this.e+1,1)};E.valueOf=E.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="",u=n[0];if(e>0){for(s+=u,i=1;i<e;i++)r=n[i]+"",t=Q-r.length,t&&(s+=de(t)),s+=r;u=n[i],r=u+"",t=Q-r.length,t&&(s+=de(t))}else if(u===0)return"0";for(;u%10===0;)u/=10;return s+u}function Jt(n,i,t){if(n!==~~n||n<i||n>t)throw Error(ce+n)}function Se(n,i,t,r){var e,s,u,d;for(s=n[0];s>=10;s/=10)--i;return--i<0?(i+=Q,e=0):(e=Math.ceil((i+1)/Q),i%=Q),s=$t(10,Q-i),d=n[e]%s|0,r==null?i<3?(i==0?d=d/100|0:i==1&&(d=d/10|0),u=t<4&&d==99999||t>3&&d==49999||d==5e4||d==0):u=(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),u=(r||t<4)&&d==9999||!r&&t>3&&d==4999):u=((r||t<4)&&d+1==s||!r&&t>3&&d+1==s/2)&&(n[e+1]/s/1e3|0)==$t(10,i-3)-1,u}function qe(n,i,t){for(var r,e=[0],s,u=0,d=n.length;u<d;){for(s=e.length;s--;)e[s]*=i;for(e[0]+=Si.indexOf(n.charAt(u++)),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 $r(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 u=i.times(i);i=u.times(u).minus(u).times(8).plus(1)}return n.precision-=t,i}var at=(function(){function n(r,e,s){var u,d=0,f=r.length;for(r=r.slice();f--;)u=r[f]*e+d,r[f]=u%s|0,d=u/s|0;return d&&r.unshift(d),r}function i(r,e,s,u){var d,f;if(s!=u)f=s>u?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,u){for(var d=0;s--;)r[s]-=d,d=r[s]<e[s]?1:0,r[s]=d*u+r[s]-e[s];for(;!r[0]&&r.length>1;)r.shift()}return function(r,e,s,u,d,f){var c,p,g,y,P,S,B,C,k,R,I,x,z,Z,J,it,tt,ot,K,st,v=r.constructor,o=r.s==e.s?1:-1,l=r.d,h=e.d;if(!l||!l[0]||!h||!h[0])return new v(!r.s||!e.s||(l?h&&l[0]==h[0]:!h)?NaN:l&&l[0]==0||!h?o*0:o/0);for(f?(P=1,p=r.e-e.e):(f=re,P=Q,p=Vt(r.e/P)-Vt(e.e/P)),K=h.length,tt=l.length,k=new v(o),R=k.d=[],g=0;h[g]==(l[g]||0);g++);if(h[g]>(l[g]||0)&&p--,s==null?(Z=s=v.precision,u=v.rounding):d?Z=s+(r.e-e.e)+1:Z=s,Z<0)R.push(1),S=!0;else{if(Z=Z/P+2|0,g=0,K==1){for(y=0,h=h[0],Z++;(g<tt||y)&&Z--;g++)J=y*f+(l[g]||0),R[g]=J/h|0,y=J%h|0;S=y||g<tt}else{for(y=f/(h[0]+1)|0,y>1&&(h=n(h,y,f),l=n(l,y,f),K=h.length,tt=l.length),it=K,I=l.slice(0,K),x=I.length;x<K;)I[x++]=0;st=h.slice(),st.unshift(0),ot=h[0],h[1]>=f/2&&++ot;do y=0,c=i(h,I,K,x),c<0?(z=I[0],K!=x&&(z=z*f+(I[1]||0)),y=z/ot|0,y>1?(y>=f&&(y=f-1),B=n(h,y,f),C=B.length,x=I.length,c=i(B,I,C,x),c==1&&(y--,t(B,K<C?st:h,C,f))):(y==0&&(c=y=1),B=h.slice()),C=B.length,C<x&&B.unshift(0),t(I,B,x,f),c==-1&&(x=I.length,c=i(h,I,K,x),c<1&&(y++,t(I,K<x?st:h,x,f))),x=I.length):c===0&&(y++,I=[0]),R[g++]=y,c&&I[0]?I[x++]=l[it]||0:(I=[l[it]],x=1);while((it++<tt||I[0]!==void 0)&&Z--);S=I[0]!==void 0}R[0]||R.shift()}if(P==1)k.e=p,pr=S;else{for(g=1,y=R[0];y>=10;y/=10)g++;k.e=g+p*P-1,j(k,d?s+k.e+1:s,u,S)}return k}})();function j(n,i,t,r){var e,s,u,d,f,c,p,g,y,P=n.constructor;t:if(i!=null){if(g=n.d,!g)return n;for(e=1,d=g[0];d>=10;d/=10)e++;if(s=i-e,s<0)s+=Q,u=i,p=g[y=0],f=p/$t(10,e-u-1)%10|0;else if(y=Math.ceil((s+1)/Q),d=g.length,y>=d)if(r){for(;d++<=y;)g.push(0);p=f=0,e=1,s%=Q,u=s-Q+1}else break t;else{for(p=d=g[y],e=1;d>=10;d/=10)e++;s%=Q,u=s-Q+e,f=u<0?0:p/$t(10,e-u-1)%10|0}if(r=r||i<0||g[y+1]!==void 0||(u<0?p:p%$t(10,e-u-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?u>0?p/$t(10,e-u):0:g[y-1])%10&1||t==(n.s<0?8:7)),i<1||!g[0])return g.length=0,c?(i-=n.e+1,g[0]=$t(10,(Q-i%Q)%Q),n.e=-i||0):g[0]=n.e=0,n;if(s==0?(g.length=y,d=1,y--):(g.length=y+1,d=$t(10,Q-s),g[y]=u>0?(p/$t(10,e-u)%$t(10,u)|0)*d:0),c)for(;;)if(y==0){for(s=1,u=g[0];u>=10;u/=10)s++;for(u=g[0]+=d,d=1;u>=10;u/=10)d++;s!=d&&(n.e++,g[0]==re&&(g[0]=1));break}else{if(g[y]+=d,g[y]!=re)break;g[y--]=0,d=1}for(s=g.length;g[--s]===0;)g.pop()}return $&&(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 Mr(n);var r,e=n.e,s=zt(n.d),u=s.length;return i?(t&&(r=t-u)>0?s=s.charAt(0)+"."+s.slice(1)+de(r):u>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-u)>0&&(s+=de(r))):e>=u?(s+=de(e+1-u),t&&(r=t-e-1)>0&&(s=s+"."+de(r))):((r=e+1)<u&&(s=s.slice(0,r)+"."+s.slice(r)),t&&(r=t-u)>0&&(e+1===u&&(s+="."),s+=de(r))),s}function He(n,i){var t=n[0];for(i*=Q;t>=10;t/=10)i++;return i}function Ze(n,i,t){if(i>Kr)throw $=!0,t&&(n.precision=t),Error(mr);return j(new n(je),i,1,!0)}function oe(n,i,t){if(i>Ei)throw Error(mr);return j(new n(We),i,t,!0)}function wr(n){var i=n.length-1,t=i*Q+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 vr(n,i,t,r){var e,s=new n(1),u=Math.ceil(r/Q+4);for($=!1;;){if(t%2&&(s=s.times(i),cr(s.d,u)&&(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),cr(i.d,u)}return $=!0,s}function fr(n){return n.d[n.d.length-1]&1}function yr(n,i,t){for(var r,e,s=new n(i[0]),u=0;++u<i.length;){if(e=new n(i[u]),!e.s){s=e;break}r=s.cmp(e),(r===t||r===0&&s.s===t)&&(s=e)}return s}function Ii(n,i){var t,r,e,s,u,d,f,c=0,p=0,g=0,y=n.constructor,P=y.rounding,S=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?($=!1,f=S):f=i,d=new y(.03125);n.e>-2;)n=n.times(d),g+=5;for(r=Math.log($t(2,g))/Math.LN10*2+5|0,f+=r,t=s=u=new y(1),y.precision=f;;){if(s=j(s.times(n),f,1),t=t.times(++p),d=u.plus(at(s,t,f,1)),zt(d.d).slice(0,f)===zt(u.d).slice(0,f)){for(e=g;e--;)u=j(u.times(u),f,1);if(i==null)if(c<3&&Se(u.d,f-r,P,c))y.precision=f+=10,t=s=d=new y(1),p=0,c++;else return j(u,y.precision=S,P,$=!0);else return y.precision=S,u}u=d}}function fe(n,i){var t,r,e,s,u,d,f,c,p,g,y,P=1,S=10,B=n,C=B.d,k=B.constructor,R=k.rounding,I=k.precision;if(B.s<0||!C||!C[0]||!B.e&&C[0]==1&&C.length==1)return new k(C&&!C[0]?-1/0:B.s!=1?NaN:C?0:B);if(i==null?($=!1,p=I):p=i,k.precision=p+=S,t=zt(C),r=t.charAt(0),Math.abs(s=B.e)<15e14){for(;r<7&&r!=1||r==1&&t.charAt(1)>3;)B=B.times(n),t=zt(B.d),r=t.charAt(0),P++;s=B.e,r>1?(B=new k("0."+t),s++):B=new k(r+"."+t.slice(1))}else return c=Ze(k,p+2,I).times(s+""),B=fe(new k(r+"."+t.slice(1)),p-S).plus(c),k.precision=I,i==null?j(B,I,R,$=!0):B;for(g=B,f=u=B=at(B.minus(1),B.plus(1),p,1),y=j(B.times(B),p,1),e=3;;){if(u=j(u.times(y),p,1),c=f.plus(at(u,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,I).times(s+""))),f=at(f,new k(P),p,1),i==null)if(Se(f.d,p-S,R,d))k.precision=p+=S,c=u=B=at(g.minus(1),g.plus(1),p,1),y=j(B.times(B),p,1),e=d=1;else return j(f,k.precision=I,R,$=!0);else return k.precision=I,f;f=c,e+=2}}function Mr(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)%Q,t<0&&(r+=Q),r<e){for(r&&n.d.push(+i.slice(0,r)),e-=Q;r<e;)n.d.push(+i.slice(r,r+=Q));i=i.slice(r),r=Q-i.length}else r-=e;for(;r--;)i+="0";n.d.push(+i),$&&(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 zr(n,i){var t,r,e,s,u,d,f,c,p;if(i.indexOf("_")>-1){if(i=i.replace(/(\d)_(?=\d)/g,"$1"),br.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(Ur.test(i))t=16,i=i.toLowerCase();else if(Zr.test(i))t=2;else if(Hr.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("."),u=s>=0,r=n.constructor,u&&(i=i.replace(".",""),d=i.length,s=d-s,e=vr(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,$=!1,u&&(n=at(n,e,d*4)),f&&(n=n.times(Math.abs(f)<54?$t(2,f):Be.pow(2,f))),$=!0,n)}function Gr(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),u=new n(16),d=new n(20);t--;)e=i.times(i),i=i.times(s.plus(e.times(u.times(e).minus(d))));return i}function we(n,i,t,r,e){var s,u,d,f,c=1,p=n.precision,g=Math.ceil(p/Q);for($=!1,f=t.times(t),d=new n(r);;){if(u=at(d.times(f),new n(i++*i++),p,1),d=e?r.plus(u):r.minus(u),r=at(u.times(f),new n(i++*i++),p,1),u=d.plus(r),u.d[g]!==void 0){for(s=g;u.d[s]===d.d[s]&&s--;);if(s==-1)break}s=d,d=r,r=u,u=s,c++}return $=!0,u.d.length=g+1,u}function Qe(n,i){for(var t=n;--i;)t*=n;return t}function Pr(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=fr(t)?r?2:3:r?4:1,i;ae=fr(t)?r?1:4:r?3:2}return i.minus(e).abs()}function Oi(n,i,t,r){var e,s,u,d,f,c,p,g,y,P=n.constructor,S=t!==void 0;if(S?(Jt(t,1,he),r===void 0?r=P.rounding:Jt(r,0,8)):(t=P.precision,r=P.rounding),!n.isFinite())p=Mr(n);else{for(p=se(n),u=p.indexOf("."),S?(e=2,i==16?t=t*4-3:i==8&&(t=t*3-2)):e=i,u>=0&&(p=p.replace(".",""),y=new P(1),y.e=p.length-u,y.d=qe(se(y),10,e),y.e=y.d.length),g=qe(p,10,e),s=f=g.length;g[--f]==0;)g.pop();if(!g[0])p=S?"0p+0":"0";else{if(u<0?s--:(n=new P(n),n.d=g,n.e=s,n=at(n,y,t,r,0,e),g=n.d,s=n.e,c=pr),u=g[t],d=e/2,c=c||g[t+1]!==void 0,c=r<4?(u!==void 0||c)&&(r===0||r===(n.s<0?3:2)):u>d||u===d&&(r===4||c||r===6&&g[t-1]&1||r===(n.s<0?8:7)),g.length=t,c)for(;++g[--t]>e-1;)g[t]=0,t||(++s,g.unshift(1));for(f=g.length;!g[f-1];--f);for(u=0,p="";u<f;u++)p+=Si.charAt(g[u]);if(S){if(f>1)if(i==16||i==8){for(u=i==16?4:3,--f;f%u;f++)p+="0";for(g=qe(p,e,i),f=g.length;!g[f-1];--f);for(u=1,p="1.";u<f;u++)p+=Si.charAt(g[u])}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 cr(n,i){if(n.length>i)return n.length=i,!0}function Vr(n){return new this(n).abs()}function Xr(n){return new this(n).acos()}function Jr(n){return new this(n).acosh()}function Yr(n,i){return new this(n).plus(i)}function tn(n){return new this(n).asin()}function en(n){return new this(n).asinh()}function rn(n){return new this(n).atan()}function nn(n){return new this(n).atanh()}function on(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(at(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(at(n,i,s,1)),t}function sn(n){return new this(n).cbrt()}function an(n){return j(n=new this(n),n.e+1,2)}function ln(n,i,t){return new this(n).clamp(i,t)}function un(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]=Bi[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]=Bi[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(_r);else this[t]=!1;else throw Error(ce+t+": "+r);return this}function dn(n){return new this(n).cos()}function fn(n){return new this(n).cosh()}function Ar(n){var i,t,r;function e(s){var u,d,f,c=this;if(!(c instanceof e))return new e(s);if(c.constructor=e,hr(s)){c.s=s.s,$?!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(u=0,d=s;d>=10;d/=10)u++;$?u>e.maxE?(c.e=NaN,c.d=null):u<e.minE?(c.e=0,c.d=[0]):(c.e=u,c.d=[s]):(c.e=u,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),br.test(s)?Le(c,s):zr(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=E,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=un,e.clone=Ar,e.isDecimal=hr,e.abs=Vr,e.acos=Xr,e.acosh=Jr,e.add=Yr,e.asin=tn,e.asinh=en,e.atan=rn,e.atanh=nn,e.atan2=on,e.cbrt=sn,e.ceil=an,e.clamp=ln,e.cos=dn,e.cosh=fn,e.div=cn,e.exp=hn,e.floor=pn,e.hypot=mn,e.ln=_n,e.log=gn,e.log10=wn,e.log2=bn,e.max=vn,e.min=yn,e.mod=Mn,e.mul=Pn,e.pow=An,e.random=Sn,e.round=Bn,e.sign=En,e.sin=In,e.sinh=On,e.sqrt=xn,e.sub=Tn,e.sum=Cn,e.tan=kn,e.tanh=Rn,e.trunc=Dn,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 cn(n,i){return new this(n).div(i)}function hn(n){return new this(n).exp()}function pn(n){return j(n=new this(n),n.e+1,3)}function mn(){var n,i,t=new this(0);for($=!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 $=!0,new this(1/0);t=i}return $=!0,t.sqrt()}function hr(n){return n instanceof Be||n&&n.toStringTag===gr||!1}function _n(n){return new this(n).ln()}function gn(n,i){return new this(n).log(i)}function bn(n){return new this(n).log(2)}function wn(n){return new this(n).log(10)}function vn(){return yr(this,arguments,-1)}function yn(){return yr(this,arguments,1)}function Mn(n,i){return new this(n).mod(i)}function Pn(n,i){return new this(n).mul(i)}function An(n,i){return new this(n).pow(i)}function Sn(n){var i,t,r,e,s=0,u=new this(1),d=[];if(n===void 0?n=this.precision:Jt(n,1,he),r=Math.ceil(n/Q),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(_r);else for(;s<r;)d[s++]=Math.random()*1e7|0;for(r=d[--s],n%=Q,r&&n&&(e=$t(10,Q-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-=Q)d.shift();for(r=1,e=d[0];e>=10;e/=10)r++;r<Q&&(t-=Q-r)}return u.e=t,u.d=d,u}function Bn(n){return j(n=new this(n),n.e+1,this.rounding)}function En(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 On(n){return new this(n).sinh()}function xn(n){return new this(n).sqrt()}function Tn(n,i){return new this(n).sub(i)}function Cn(){var n=0,i=arguments,t=new this(i[n]);for($=!1;t.s&&++n<i.length;)t=t.plus(i[n]);return $=!0,j(t,this.precision,this.rounding)}function kn(n){return new this(n).tan()}function Rn(n){return new this(n).tanh()}function Dn(n){return j(n=new this(n),n.e+1,1)}E[Symbol.for("nodejs.util.inspect.custom")]=E.toString;E[Symbol.toStringTag]="Decimal";var Be=E.constructor=Ar(Bi);je=new Be(je);We=new Be(We);var q=Be;var Ee=(r=>(r[r.Spot=0]="Spot",r[r.Curve=1]="Curve",r[r.BidAsk=2]="BidAsk",r))(Ee||{});var U=require("@cetusprotocol/common-sdk");var le=1e3,Fn=-443636,Nn=443636,ve=1e4,ye=2e3,_e=200,ge=443636n,Ke=1e8,xi=1e9,te=1e4,qn=604800,Ln=1747627200;var rt=Mi(Ne());var jn=new rt.default(524288),N=64,$e=new rt.default(1).shln(N),Sr=new rt.default(2).pow(new rt.default(128)).sub(new rt.default(1)),F=class n{static splitBinLiquidityInfo(i,t,r){let e=n.getPositionCount(t,r);if(e<=1)return[i];let s=[],u=t;for(let d=0;d<e;d++){let f=Math.min(u+1e3-2,r),c=i.bins.filter(p=>p.bin_id>=u&&p.bin_id<=f);s.push({bins:c,amount_a:c.reduce((p,g)=>(0,U.d)(p).plus(g.amount_a),(0,U.d)(0)).toFixed(0),amount_b:c.reduce((p,g)=>(0,U.d)(p).plus(g.amount_b),(0,U.d)(0)).toFixed(0)}),u=f+1}return s}static processBinsByRate(i,t){let r=[],e=(0,U.d)(0),s=(0,U.d)(0),u=!1;return i.forEach(d=>{let{amount_a:f,amount_b:c,liquidity:p="0"}=d,g=(0,U.d)(t).mul(p).toFixed(0),y=(0,U.d)(f).mul(t),P=(0,U.d)(c).mul(t);e=(0,U.d)(e).plus(y),s=(0,U.d)(s).plus(P),((0,U.d)(y).lt(1)&&(0,U.d)(y).gt(0)||(0,U.d)(P).lt(1)&&(0,U.d)(P).gt(0))&&(u=!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:g})}),{bins:{bins:r,amount_a:e.toFixed(0),amount_b:s.toFixed(0)},has_invalid_amount:u}}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,U.d)(t).gte((0,U.d)(s)))return{amount_a:r,amount_b:e};let u=(0,U.d)(t).div(s).mul(r).toFixed(0,q.ROUND_FLOOR),d=(0,U.d)(t).div(s).mul(e).toFixed(0,q.ROUND_FLOOR);return{amount_a:u,amount_b:d}}static getPositionCount(i,t){let e=(0,U.d)(t).sub(i).add(1).div(1e3);return Number(e.toFixed(0,q.ROUND_UP))}static getLiquidity(i,t,r){return(0,U.d)(r).mul(i).add((0,U.d)(t).mul((0,U.d)(2).pow(N))).toFixed(0)}static getAmountAFromLiquidity(i,t){return(0,U.d)(i).div((0,U.d)(t)).toFixed(0)}static getAmountBFromLiquidity(i){return(0,U.d)(i).div((0,U.d)(2).pow(N)).toFixed(0)}static getAmountsFromLiquidity(i,t,r,e){if((0,U.d)(e).isZero()&&X("LiquiditySupplyIsZero","Liquidity supply is zero"),(0,U.d)(r).gt((0,U.d)(e))&&X("InvalidDeltaLiquidity","Invalid delta liquidity"),(0,U.d)(r).isZero())return["0","0"];let s;(0,U.d)(i).isZero()?s="0":s=(0,U.d)(i).mul((0,U.d)(r)).div((0,U.d)(e)).toFixed(0,q.ROUND_FLOOR);let u;return(0,U.d)(t).isZero()?u="0":u=(0,U.d)(t).mul((0,U.d)(r)).div((0,U.d)(e)).toFixed(0,q.ROUND_FLOOR),[s,u]}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 q(t).div(new q(1e4));return new q(1).add(new q(r)).pow(new q(i)).toString()}static getBinIdFromPrice(i,t,r,e,s){let u=n.getPricePerLamport(e,s,i);return n.getBinIdFromLamportPrice(u,t,r)}static getBinIdFromLamportPrice(i,t,r){let e=new q(t).div(new q(1e4)),s=new q(i).log().dividedBy(new q(1).add(e).log());return(r?s.floor():s.ceil()).toNumber()}static getPricePerLamport(i,t,r){return new q(r).mul(new q(10**(t-i))).toString()}static getPriceFromLamport(i,t,r){return new q(r).div(new q(10**(t-i))).toString()}static getReversePrice(i){return new q(1).div(i).toString()}static getQPriceFromId(i,t){let r=new rt.default(t).shln(N).div(new rt.default(1e4)),e=$e.add(r);return n.pow(e,new rt.default(i)).toString()}static getPricePerLamportFromQPrice(i){return U.MathUtil.fromX64(new rt.default(i)).toString()}static pow(i,t){let r=t.isNeg();if(t.isZero())return $e;if(t=r?t.abs():t,t.gt(jn))return new rt.default(0);let e=i,s=$e;return e.gte(s)&&(e=Sr.div(e),r=!r),t.and(new rt.default(1)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(2)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(4)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(8)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(16)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(32)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(64)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(128)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(256)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(512)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(1024)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(2048)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(4096)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(8192)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(16384)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(32768)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(65536)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(131072)).isZero()||(s=s.mul(e).shrn(N)),e=e.mul(e).shrn(N),t.and(new rt.default(262144)).isZero()||(s=s.mul(e).shrn(N)),s.isZero()?new rt.default(0):(r&&(s=Sr.div(s)),s)}static binScore(i){let t=BigInt(i)+ge;return(t<0n||t>ge*2n)&&X("InvalidBinId",new Error("Invalid bin ID"),{[U.DETAILS_KEYS.METHOD_NAME]:"binScore",[U.DETAILS_KEYS.REQUEST_PARAMS]:{binId:i}}),t.toString()}static scoreToBinId(i){let t=BigInt(i)-ge;return(t<-ge||t>ge)&&X("InvalidBinId",new Error("Invalid score"),{[U.DETAILS_KEYS.METHOD_NAME]:"scoreToBinId",[U.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 q("18446744073709551615").log(10).div(new q(t).log(10)).floor(),s=e.neg(),u=e,d=(0,U.d)(1),f=(0,U.d)("340282366920938463463374607431768211455");for(;;){let c=(0,U.d)(n.getQPriceFromId(s.toNumber(),i));if(c.gt(d)&&!c.isZero())break;s=s.add(1)}for(;;){let c=(0,U.d)(n.getQPriceFromId(u.toNumber(),i));if(c.lt(f)&&!c.isZero())break;u=u.sub(1)}return{minBinId:s.toNumber(),maxBinId:u.toNumber()}}static getBinShift(i,t,r){let e=n.getPricePerLamportFromBinId(i,t),s=(0,U.d)(e).mul(1+r).toString(),u=n.getBinIdFromLamportPrice(s,t,!0),d=(0,U.d)(u).sub(i).abs().toFixed(0,q.ROUND_UP);return console.log("getBinShift Options:",{active_id:i,bin_shift:d}),Number(d)}};var Xt=require("@mysten/sui/bcs"),Er=require("blakejs");function Ti(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 X("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseDlmmBasePool",[G.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function Ie(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},u=i.reward_manager.fields,d=u.rewards.map(g=>{let y=g.fields.current_emission_rate,P=G.MathUtil.fromX64(new Br.default(y)),S=Math.floor(P.toNumber()*60*60*24).toString();return{reward_coin:(0,G.fixCoinType)(g.fields.reward_coin.fields.name,!1),emissions_per_second:P.toString(),emissions_per_day:S,period_emission_rates:{id:g.fields.period_emission_rates.fields.id.id,size:g.fields.period_emission_rates.fields.size}}}),f={is_public:u.is_public,emergency_reward_pause:u.emergency_reward_pause,vault:{id:u.vault.fields.id.id,size:u.vault.fields.size},rewards:d,last_updated_time:u.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),X("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),X("ParseError",i,{[G.DETAILS_KEYS.METHOD_NAME]:"parseDlmmPosition",[G.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function Wn(n,i,t,r){let e=n.map((d,f)=>{let c=t+f,p=F.getPricePerLamportFromBinId(c,i);if(c===r.bin_id){let{amount_a:P,amount_b:S}=F.calculateOutByShare(r,d);return{bin_id:c,amount_a:P,amount_b:S,liquidity:d,price_per_lamport:p}}if(c<r.bin_id){let P=F.getAmountBFromLiquidity(d);return{bin_id:c,amount_a:"0",amount_b:P,liquidity:d,price_per_lamport:p}}let g=F.getQPriceFromId(c,i),y=F.getAmountAFromLiquidity(d,g);return{bin_id:c,amount_a:y,amount_b:"0",liquidity:d,price_per_lamport:p}}),s=e.reduce((d,f)=>d.add(new q(f.amount_a)),new q(0)).toFixed(0),u=e.reduce((d,f)=>d.add(new q(f.amount_b)),new q(0)).toFixed(0);return{bins:e,amount_a:s,amount_b:u}}function Ci(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:F.getPricePerLamportFromQPrice(r.price)}))}catch(i){return console.log("\u{1F680} ~ parseBinInfo ~ error:",i),[]}}function ki(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:F.getPricePerLamportFromQPrice(n.price)}}catch(i){return console.log("\u{1F680} ~ parseBinInfo ~ error:",i),X("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,u=e.reward,d=e.amount,f={coin_type:(0,G.fixCoinType)(u.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:u,amount_in:d,amount_out:f,fee:c,ref_fee:p,bin_swaps:g,from:y,target:P}=e,S=g.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:u,a2b:i,in_amount:d,out_amount:f,ref_fee_amount:p,fee_amount:c,bin_swaps:S,partner:s,from_coin_type:(0,G.fixCoinType)(y.name,!1),to_coin_type:(0,G.fixCoinType)(P.name,!1)}}}function Zn(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:u}=n;return u?.forEach((d,f)=>{let{name:c,address:p}=(0,G.extractStructTagFromType)(d.type);if(Ir.includes(c)&&p===t&&r===d.parsedJson.pool){let g={tx:d.id.txDigest,sender:d.sender,type:d.type,block_time:s||"0",index:`${i}_${f}`,parsed_json:d.parsedJson};e.push(g)}}),e}function Un(n,i,t){let r=[],e=0,s=new Date(n*1e3).getTime(),u=new Date().getTime();for(;;){let d=s+e*t;if(d>=u&&r.push(d),e>=i)break;e+=1}return r}function Hn(n,i,t,r){let e=[];for(let s=i;s<=t;s+=r){let u=n.findLast(d=>(0,G.d)(s).gte(d.time));u?e.push({time:s.toString(),emissions_per_second:u.emissions_per_second,emissions_per_day:u.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 Qn(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 Pe(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"),u=s.length,d=0;for(;d<u;){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]),g=(0,Er.blake2b)(p,void 0,32);return`0x${Buffer.from(g).toString("hex")}`}var O=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:u,total_amount_a:d,total_amount_b:f,active_bin_of_pool:c}=i,p=r<s||r>u,g=F.getQPriceFromId(r,e),y=(0,O.d)(0),P=(0,O.d)(0),S=(0,O.d)(200);if(t===2?S=(0,O.d)(200):t===1?S=(0,O.d)(2e3):t===0&&(S=(0,O.d)(1)),!p&&c){let tt=this.calculateActiveWeights(c.amount_a,c.amount_b,g,S);y=tt.active_weight_a,P=tt.active_weight_b}r===s&&(0,O.d)(f).isZero()&&(y=(0,O.d)(S).mul((0,O.d)(2).pow(N*2)).div((0,O.d)(g)).floor(),P=(0,O.d)(0)),r===u&&(0,O.d)(d).isZero()&&(P=(0,O.d)(S).mul((0,O.d)(2).pow(N)).floor(),y=(0,O.d)(0)),r>s&&r<u&&((0,O.d)(d).isZero()&&(P=(0,O.d)(S).mul((0,O.d)(2).pow(N)).floor(),y=(0,O.d)(0)),(0,O.d)(f).isZero()&&(y=(0,O.d)(S).mul((0,O.d)(2).pow(N*2)).div((0,O.d)(g)).floor(),P=(0,O.d)(0)));let B=p?(0,O.d)(0):y,C=p?(0,O.d)(0):P,k=(0,O.d)(2e3).sub((0,O.d)(200)).floor(),R=r>u?u:r,I=r<s?s:r,x=r>s?R===s?(0,O.d)(0):k.div((0,O.d)(R-s)).floor():(0,O.d)(0),z=u>r?I===u?(0,O.d)(0):k.div((0,O.d)(u-I)).floor():(0,O.d)(0),Z=s,J=[],it=[];for(;Z<=u;){let tt=(0,O.d)(0);if(Z<r){let ot=R-Z;t===0?tt=(0,O.d)(1):t===2?tt=(0,O.d)(S).add(x.mul(ot)).floor():t===1&&(tt=(0,O.d)(S).sub(x.mul(ot)).floor())}else if(Z>r){let ot=Z-I;t===0?tt=(0,O.d)(1):t===2?tt=(0,O.d)(S).add(z.mul(ot)).floor():t===1&&(tt=(0,O.d)(S).sub(z.mul(ot)).floor())}else tt=S;if(J.push(tt),Z<r)C=C.add(tt.mul((0,O.d)(2).pow(N))).floor(),it.push((0,O.d)(0));else if(Z>r){let ot=tt.mul((0,O.d)(2).pow(N*2)).div(F.getQPriceFromId(Z,e)).floor();it.push(ot),B=B.add(ot).floor()}else it.push((0,O.d)(0));Z+=1}return{...i,total_weight_a:B,total_weight_b:C,active_weight_a:y,active_weight_b:P,weights:J,weight_per_prices:it}}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,u=e-200,d=r>i?Math.floor(u/(r-i)):0,f=t>r?Math.floor(u/(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,u=e-s,d=r>i?Math.floor(u/(r-i)):0,f=t>r?Math.floor(u/(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 u=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,O.d)(0));if(u.cmp((0,O.d)(0))!=1)throw Error("Invalid parameters");return{bins:e.map(f=>{let c=F.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 g=(0,O.d)(f.weight).div(u),y=ne((0,O.d)(t),g).toString(),P="0",S=F.getQPriceFromId(f.bin_id,r),B=F.getLiquidity(P,y,S);return{bin_id:f.bin_id,amount_b:y,amount_a:P,price_per_lamport:c,liquidity:B}}}),amount_a:"0",amount_b:t}}static toAmountAskSide(i,t,r,e,s=!1){let u=e.filter(f=>f.bin_id>=i).reduce(function(f,c){if(c.bin_id<=i&&!s)return f;{let p=F.getPricePerLamportFromBinId(c.bin_id,t),g=new q(c.weight).div(p);return f.add(g)}},new q(0));if(u.cmp(new q(0))!=1)throw Error("Invalid parameters");return{bins:e.map(f=>{let c=F.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 q(f.weight).div(c).div(u),P=ne((0,O.d)(r),y).toString(),S="0",B=F.getQPriceFromId(f.bin_id,t),C=F.getLiquidity(P,S,B);return{bin_id:f.bin_id,amount_a:P,amount_b:S,price_per_lamport:c,liquidity:C}}}),amount_a:r,amount_b:"0"}}static toAmountBothSide(i,t,r,e,s,u,d){let f=!(0,O.d)(r).isZero()&&(0,O.d)(e).isZero(),c=(0,O.d)(r).isZero()&&!(0,O.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(g=>g.bin_id===i);if(p.length===1){let{totalWeightA:g,totalWeightB:y,activeWeightA:P,activeWeightB:S}=n.calculateTotalWeights(t,d,i,p[0],s,u,f?"a":c?"b":void 0),B=new q(r.toString()).div(g),C=new q(e.toString()).div(y),k=d.map(x=>{let z=F.getPricePerLamportFromBinId(x.bin_id,t);if(x.bin_id<i||x.bin_id===i&&c){let st=ne(C,new q(x.weight)),v="0",o=F.getQPriceFromId(x.bin_id,t),l=F.getLiquidity(v,st.toString(),o);return{bin_id:x.bin_id,amount_a:"0",amount_b:st.toString(),price_per_lamport:z,liquidity:l}}if(x.bin_id>i||x.bin_id===i&&f){let st=new q(x.weight).div(z),v=ne(B,new q(st)),o="0",l=F.getQPriceFromId(x.bin_id,t),h=F.getLiquidity(v.toString(),o,l);return{bin_id:x.bin_id,amount_a:v.toString(),amount_b:"0",price_per_lamport:z,liquidity:h}}let Z=ne(B,P),J=ne(C,S),it=Z.toString(),tt=J.toString(),ot=F.getQPriceFromId(x.bin_id,t),K=F.getLiquidity(it,tt,ot);return{bin_id:x.bin_id,amount_a:it,amount_b:tt,price_per_lamport:z,liquidity:K}}),R=k.reduce((x,z)=>(0,O.d)(x).add((0,O.d)(z.amount_a)),(0,O.d)(0)).toString(),I=k.reduce((x,z)=>(0,O.d)(x).add((0,O.d)(z.amount_b)),(0,O.d)(0)).toString();return{bins:k,amount_a:R,amount_b:I}}else{let{totalWeightA:g,totalWeightB:y}=n.calculateTotalWeights(t,d,i),P=new q(r.toString()).div(g),S=new q(e.toString()).div(y),B=d.map(R=>{let I=F.getPricePerLamportFromBinId(R.bin_id,t);if(R.bin_id<i){let x=ne(S,new q(R.weight));return{bin_id:R.bin_id,amount_a:"0",amount_b:x.toString(),price_per_lamport:I}}else{let x=new q(R.weight).div(I),z=ne(P,x);return{bin_id:R.bin_id,amount_a:z.toString(),amount_b:"0",price_per_lamport:I}}}),C=B.reduce((R,I)=>(0,O.d)(R).add((0,O.d)(I.amount_a)),(0,O.d)(0)).toString(),k=B.reduce((R,I)=>(0,O.d)(R).add((0,O.d)(I.amount_b)),(0,O.d)(0)).toString();return{bins:B,amount_a:C,amount_b:k}}}static autoFillCoinByWeight(i,t,r,e,s,u,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?u:void 0),g=(0,O.d)(0);e?g=c.isZero()?new q(0):new q(r).div(c):g=p.isZero()?new q(0):new q(r).div(p);let y=ne(g,e?p:c).toString();return n.toAmountBothSide(i,t,e?r:y,e?y:r,s,u,d)}static calculateActiveWeights(i,t,r,e){let s=(0,O.d)(r),u=(0,O.d)(i),d=(0,O.d)(t),f=(0,O.d)(0),c=(0,O.d)(0);if(u.isZero()&&d.isZero())f=(0,O.d)(e).mul((0,O.d)(2).pow(N*2)).div(s.mul(2)).floor(),c=(0,O.d)(e).mul((0,O.d)(2).pow(N)).div(2).floor();else{if(u.isZero())f=(0,O.d)(0);else{let p=d.mul((0,O.d)(2).pow(N)).div(u);f=(0,O.d)(e).mul((0,O.d)(2).pow(N*2)).div(s.add(p)).floor()}if(d.isZero())c=(0,O.d)(0);else{let p=(0,O.d)(2).pow(N).add(s.mul(u).div(d)).floor();c=(0,O.d)(e).mul((0,O.d)(2).pow(N*2)).div(p).floor()}}return{active_weight_a:f,active_weight_b:c}}static calculateTotalWeights(i,t,r,e,s,u,d){let f=(0,O.d)(F.getPricePerLamportFromBinId(r,i)),c=(0,O.d)(0),p=(0,O.d)(0);if(s&&u&&e&&!d)if((0,O.d)(s).isZero()&&(0,O.d)(u).isZero())c=new q(e.weight).div(f.mul(new q(2))),p=new q(e.weight).div(new q(2));else{let P=new q(s.toString()),S=new q(u.toString());(0,O.d)(s).isZero()||(c=new q(e.weight).div(f.add(S.div(P)))),(0,O.d)(u).isZero()||(p=new q(e.weight).div(new q(1).add(f.mul(P).div(S))))}let g=c,y=p;return t.forEach(P=>{if((P.bin_id<r||d==="b")&&(y=y.add(new q(P.weight))),P.bin_id>r||d==="a"){let S=F.getPricePerLamportFromBinId(P.bin_id,i),B=new q(P.weight).div(S);g=g.add(B)}}),{totalWeightA:g,totalWeightB:y,activeWeightA:c,activeWeightB:p}}};var Yt=require("@cetusprotocol/common-sdk");var Ae=class{static toAmountsByWeights(i){let{total_weight_a:t,total_weight_b:r,weights:e,weight_per_prices:s,total_amount_a:u,total_amount_b:d,active_weight_a:f,active_weight_b:c,lower_bin_id:p,upper_bin_id:g,active_id:y,bin_step:P}=i,S=r.isZero()?(0,Yt.d)(0):(0,Yt.d)(d).mul((0,Yt.d)(2).pow(N*2)).div(r).floor(),B=t.isZero()?(0,Yt.d)(0):(0,Yt.d)(u).mul((0,Yt.d)(2).pow(N*2)).div(t).floor(),C=Pe(B.mul(f).div((0,Yt.d)(2).pow(N*2))),k=Pe(S.mul(c).div((0,Yt.d)(2).pow(N*2))),R=g-p+1,I=p,x=0,z=[];for(;x<R;){let J=(0,Yt.d)(0),it=(0,Yt.d)(0),tt=F.getQPriceFromId(I,P);I<y?it=Pe(S.mul(e[x]).div((0,Yt.d)(2).pow(N))):I>y?J=Pe(B.mul(s[x]).div((0,Yt.d)(2).pow(N*2))):(J=C,it=k);let ot=F.getLiquidity(J.toString(),it.toString(),tt);z.push({bin_id:I,amount_a:J.toString(),amount_b:it.toString(),price_per_lamport:F.getPricePerLamportFromBinId(I,P),liquidity:ot}),I+=1,x+=1}return{bins:z,amount_a:u.toString(),amount_b:d.toString()}}static toAmountsBothSideByStrategy(i,t,r,e,s,u,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:u,active_bin_of_pool:f});return this.toAmountsByWeights(c)}static autoFillCoinByStrategy(i,t,r,e,s,u,d,f){switch(d){case 0:{let c=ee.toWeightSpotBalanced(s,u);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}case 1:{let c=ee.toWeightCurve(s,u,i);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}case 2:{let c=ee.toWeightBidAsk(s,u,i);return ee.autoFillCoinByWeight(i,t,r,e,f?.amount_a||"0",f?.amount_b||"0",c)}}}static autoFillCoinByStrategyV2(i,t,r,e,s,u,d,f){let c=this.autoFillCoinByStrategy(i,t,r,e,s,u,d,f),p=ee.toWeight({strategy_type:d,active_id:i,bin_step:t,lower_bin_id:s,upper_bin_id:u,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:u}=e,d=F.getQPriceFromId(i.bin_id,s),f=F.getLiquidity(i.amount_a,i.amount_b,d),c=F.getLiquidity(t.amount_a,t.amount_b,d),p=(0,ft.d)(i.liquidity).mul(c).div(f).toFixed(0),{amount_a:g,amount_b:y}=F.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(u).mul(10),S=n.getVariableFee(r),B=(0,ft.d)(P).add(S).toFixed(0);(0,ft.d)(B).gt(1e8)&&(B=1e8.toString());let C="0",k="0";return(0,ft.d)(g).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),B):(0,ft.d)(y).gt(t.amount_b)&&(0,ft.d)(t.amount_a).gt(g)&&(C=n.calculateCompositionFee((0,ft.d)(t.amount_a).sub(g).toFixed(0),B)),{fees_a:C,fees_b:k}}};var H=require("@cetusprotocol/common-sdk");var ji=class{static calculateIlm(i){let{curvature:t,initial_price:r,max_price:e,bin_step:s,total_supply:u,pool_share_percentage:d,config:f}=i,{price_curve_points_num:c,liquidity_distribution_num:p,tokens_table_num:g,price_table_num:y}=f;if((0,H.d)(d).lt(0)||(0,H.d)(d).gt(100))throw new Error("Pool Share Percentage must be greater than 0 and less than 100.");let P=!1;if((0,H.d)(e).lt((0,H.d)(r)))throw new Error("Maximum Price must be greater or equal to Initial Price.");if((0,H.d)(e).eq(r)&&!(0,H.d)(t).eq(0))throw new Error("Curvature must be 0 when Maximum and Initial Price are equal.");if(!(0,H.d)(e).eq(r)&&(0,H.d)(t).eq(0))throw new Error("Maximum and Initial Price must be equal when Curvature is 0.");(0,H.d)(e).eq(r)&&(0,H.d)(t).eq(0)&&(P=!0);let S=_=>(0,H.d)(x).mul((0,H.d)(_).div(I).pow(t)).add((0,H.d)(r)).toString(),B=(_,a)=>{let w=(0,H.d)(x).mul((0,H.d)(I).pow(-t)).mul((0,H.d)(_).pow(t+1)).div(t+1).add((0,H.d)(r).mul(_)),D=(0,H.d)(x).mul((0,H.d)(I).pow(-t)).mul((0,H.d)(a).pow(t+1)).div(t+1).add((0,H.d)(r).mul(a));return w.sub(D)},C=_=>(0,H.d)(I).mul((0,H.d)(_).sub(r).div(x).pow(1/t)).toString(),k=_=>(0,H.d)(I).mul((0,H.d)(_).sub(r).pow(1/t-1)).div((0,H.d)(t).mul((0,H.d)(x).pow(1/t))).toString(),R=_=>(0,H.d)(_).sub(r).div(x).pow(1/t).mul(I),I=(0,H.d)(u).mul(d).div(100),x=(0,H.d)(e).sub((0,H.d)(r)).toFixed(8),z=[],Z=[],J=F.getBinIdFromLamportPrice(r,s,!1),it=F.getBinIdFromLamportPrice(e,s,!1),tt=(0,H.d)(0),ot=J;if(P)z.push(r),Z.push((0,H.d)(r).mul(I).toString());else for(;ot<=it;){let _=F.getPricePerLamportFromBinId(ot,s),a=F.getPricePerLamportFromBinId(ot+1,s),w=R(a).sub(R(_));tt=tt.add(w),Z.push((0,H.d)(w).mul(_).toString()),z.push(_),ot++}let K={price_curve:[],liquidity_curve:[],dlmm_bins:[],tokens_table:[],price_table:[],initial_fdv:(0,H.d)(r).mul(u).toString(),final_fdv:(0,H.d)(e).mul(u).toString(),usdc_in_pool:"0"};var st=(0,H.d)(x).div(100);let v=[];for(let _=0;_<c;_++){let a=(0,H.d)(_).mul(I).div(c-1).toString(),w=P?e:S(a);v.push({x:a,y:w})}K.price_curve=v;let o=[];if(P)o=[{x:r,y:"0"},{x:r,y:(0,H.d)(I).div(2).toString()},{x:r,y:I.toString()}];else for(let _=0;_<p;_++){let a=(0,H.d)(r).add((0,H.d)(_).mul((0,H.d)(st))).toFixed(9),w=k(a);o.push({x:a,y:w})}K.liquidity_curve=o;let l=[];z.forEach((_,a)=>{let w=_,D=Z[a];l.push({x:w,y:D})}),K.dlmm_bins=l;let h=[],m=(0,H.d)(0),b=(0,H.d)(I).div(g).toFixed(5);for(;m.lte(I);){let _=(0,H.d)(S(m.toString())).toFixed(8),a=m.toFixed(8),w=B(Number(a),0);h.push({withdrawn:a,price:_,usdc_in_pool:w.toString()}),m=m.add(b)}K.tokens_table=h;let M=(0,H.d)(x).mul(1e10).floor().div(1e11),A=[];if(P)A.push({withdrawn:I.toString(),price:r,usdc_in_pool:(0,H.d)(I).mul(r).toString()});else{let _=(0,H.d)(0),a=(0,H.d)(r);for(;_.lte(y);){let w=(0,H.d)(C(a.toFixed(8))).toFixed(8),D=B(Number(w),0).toFixed(8);A.push({withdrawn:w,price:a.toFixed(8),usdc_in_pool:D}),a=(0,H.d)(a).add(M),_=_.add((0,H.d)(1))}}return K.price_table=A,K.usdc_in_pool=A[A.length-1].usdc_in_pool,K}};var Or=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:u}=this._sdk.sdkOptions,{pools_id:d}=(0,et.getPackagerConfigs)(u),f=await this._sdk.FullClient.getDynamicFieldObject({parentId:d,name:{type:"0x2::object::ID",value:s}});return(0,et.getObjectFields)(f).value.fields.value.fields.pool_id}catch(s){return X("FetchError",s,{[et.DETAILS_KEYS.METHOD_NAME]:"getPoolAddress",[et.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",u=`${r.package_id}_getBasePoolList`;if(s){let d=this._sdk.getCache(u,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=Ti(c);e.data.push(p)}),s&&this._sdk.updateCache(`${r.package_id}_getPoolImmutables`,e.data)}catch(d){return X("FetchError",d,{[et.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(u=>u.id),{showContent:!0,showType:!0});r.has_next_page=e.has_next_page,r.next_cursor=e.next_cursor;for(let u of s){let d=Ie(u),f=`${d.id}_getDlmmPool`;this._sdk.updateCache(f,d),r.data.push(d)}}catch(s){return X("FetchError",s,{[et.DETAILS_KEYS.METHOD_NAME]:"getPools"})}return r}async getBinInfo(i,t,r,e=!0){try{let s=`${i}_getBinInfo_${t}`,u=this._sdk.getCache(s,e);if(u!==void 0)return u;let d=F.binScore(t),[f,c]=F.resolveBinPosition(d),g=(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=ki(g);return this._sdk.updateCache(s,y),y}catch{return{bin_id:t,amount_a:"0",amount_b:"0",liquidity:"0",price_per_lamport:F.getPricePerLamportFromBinId(t,r)}}}async getTotalFeeRate(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:e,coin_type_b:s}=i,u=new xe.Transaction;u.moveCall({target:`${t.published_at}::pool::get_total_fee_rate`,arguments:[u.object(r)],typeArguments:[e,s]});let d=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:u,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,u=1e3,d=[],f,c=!0;for(;c;){let p=new xe.Transaction,g;f!==void 0?g=p.moveCall({target:"0x1::option::some",arguments:[p.pure.u32(Number((0,et.asUintN)(BigInt(f))))],typeArguments:["u32"]}):g=p.moveCall({target:"0x1::option::none",typeArguments:["u32"]}),p.moveCall({target:`${t.published_at}::pool::fetch_bins`,arguments:[p.object(r),g,p.pure.u64(u)],typeArguments:[e,s]});let y=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:p,sender:(0,Wi.normalizeSuiAddress)("0x0")}),P=Ci(y);d.push(...P),f=P.length>0?P[P.length-1].bin_id+1:void 0,c=P.length===u}return d.sort((p,g)=>p.bin_id-g.bin_id)}async getPoolTransactionList({pool_id:i,pagination_args:t,order:r="descending",full_rpc_url:e}){let{FullClient:s,sdkOptions:u}=this._sdk,d;e?d=(0,et.createFullClient)(new Or.SuiClient({url:e})):d=s;let f={data:[],has_next_page:!1},c=50,p=t,g=t.limit||10;do{let y=await d.queryTransactionBlocksByPage({ChangedObject:i},{...p,limit:50},r);y.data.forEach((P,S)=>{let B=Ni(P,S,u.dlmm_pool.package_id,i);f.data=[...f.data,...B]}),f.has_next_page=y.has_next_page,f.next_cursor=y.next_cursor,p.cursor=y.next_cursor}while(f.data.length<g&&f.has_next_page);return f.data.length>g&&(f.data=f.data.slice(0,g),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 u=t;u<=r;u++){let d=await Promise.all([this.getBinInfo(i,u,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=Ie(e),u=`${s.id}_getDlmmPool`;this._sdk.updateCache(u,s),t.push(s)}}catch(r){return X("FetchError",r,{[et.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}}),u=Ie(s);return this._sdk.updateCache(r,u),u}catch(r){return X("FetchError",r,{[et.DETAILS_KEYS.METHOD_NAME]:"getPool",[et.DETAILS_KEYS.REQUEST_PARAMS]:i})}}async createPoolAndAddWithPricePayload(i){let{bin_step:t,url:r,coin_type_a:e,coin_type_b:s,bin_infos:u,price_base_coin:d,price:f,lower_price:c,upper_price:p,decimals_a:g,decimals_b:y,strategy_type:P,use_bin_infos:S,base_factor:B,pool_id:C}=i,k,R,I,x=u;if(d==="coin_a")k=F.getBinIdFromPrice(c,t,!1,g,y),R=F.getBinIdFromPrice(p,t,!0,g,y),I=F.getBinIdFromPrice(f,t,!0,g,y);else{k=F.getBinIdFromPrice((0,et.d)(1).div(p).toString(),t,!1,g,y),R=F.getBinIdFromPrice((0,et.d)(1).div(c).toString(),t,!0,g,y),I=F.getBinIdFromPrice((0,et.d)(1).div(f).toString(),t,!1,g,y);let J={pool_id:C,amount_a:u.amount_b,amount_b:u.amount_a,active_id:I,bin_step:t,lower_bin_id:k,upper_bin_id:R,active_bin_of_pool:void 0,strategy_type:i.strategy_type};x=await this.sdk.Position.calculateAddLiquidityInfo(J)}let Z={bin_step:t,url:r,coin_type_a:e,coin_type_b:s,bin_infos:x,lower_bin_id:k,upper_bin_id:R,active_id:I,strategy_type:P,use_bin_infos:S,base_factor:B};return this.createPoolAndAddLiquidityPayload(Z)}createPoolPayload(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{bin_step:e,base_factor:s,url:u,coin_type_a:d,coin_type_b:f,active_id:c}=i;t=t||new xe.Transaction;let{registry_id:p,global_config_id:g,versioned_id:y}=(0,et.getPackagerConfigs)(r),[P,S]=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,et.asUintN)(BigInt(c)))),t.pure.string(u||""),t.object(g),t.object(y),t.object(et.CLOCK_ADDRESS)],typeArguments:[d,f]});return t.moveCall({target:`${r.published_at}::registry::destroy_receipt`,arguments:[t.object(P),S,t.object(y)],typeArguments:[d,f]}),S}createPoolAndAddLiquidityPayload(i){let{bin_step:t,base_factor:r,url:e,active_id:s,coin_type_a:u,coin_type_b:d,bin_infos:f,lower_bin_id:c,upper_bin_id:p,strategy_type:g,use_bin_infos:y}=i,P=new xe.Transaction;(0,et.isSortedSymbols)((0,et.fixCoinType)(u,!1),(0,et.fixCoinType)(d,!1))&&X("InvalidCoinTypeSequence",new Error("invalid coin type sequence"),{[et.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[et.DETAILS_KEYS.REQUEST_PARAMS]:i});let S=p-c+1;S>1e3&&X("InvalidBinWidth",new Error("Width is too large"),{[et.DETAILS_KEYS.METHOD_NAME]:"openPosition",[et.DETAILS_KEYS.REQUEST_PARAMS]:i}),console.log("\u{1F680} ~ createPoolAndAddLiquidityPayload ~ option:",{...i,width:S});let C={pool_id:this.createPoolPayload({active_id:s,bin_step:t,base_factor:r,coin_type_a:u,coin_type_b:d},P),bin_infos:f,coin_type_a:u,coin_type_b:d,lower_bin_id:c,upper_bin_id:p,active_id:s,strategy_type:g,use_bin_infos:y,max_price_slippage:0,bin_step:t};return this.sdk.Position.addLiquidityPayload(C,P),P}};var Ve=require("@cetusprotocol/common-sdk"),Zi={env:"testnet",full_rpc_url:Ve.FullRpcUrlTestnet,graph_rpc_url:Ve.GraphRpcUrlTestnet,dlmm_pool:{package_id:"0x17a1f5a8779461ff44e942adf33325cce112c693d6a177ed77f035ca86d1fdb6",published_at:"0x6d32c1be32eefcea933c03dd5cb7c783d1d83f6b30c4d1131d955933747b1701",version:1,config:{registry_id:"0x319070e26a6809f439d3c4a45e63bf74939c5fe3165de7b65968ee8547f71bd0",pools_id:"0x505fcde74ab557d553832a87f169a0408ad3507ca4e84b25f7d32c2c1535765c",global_config_id:"0x88bb33e9eff2fccab980a0e4b43fc4572abd08f08304d47a20d3e4e99d94d159",versioned_id:"0xa710caae87b2129acc97fbb98ea7011e3137c3291b02c0fcce866d67d5d9e8d0",admin_cap_id:"0x6fc908894ad7c2ff16cca07a05af6760831a8b5e5dc34e40470dce6ee1760155",partners_id:"0xc5c31fe1550e39c9890e0fe3d2608dd9b408a10d74020e5ff72ccfffe4c9c879"}},dlmm_router:{package_id:"0xba3059875c8980ac171fc2bac81b9df172fb77fa0cb5a267636df701225b93ef",published_at:"0x59b7a2da6db8f9245a1db6169018af7124c0714fa77a84224967ead6be125127",version:1},faucet:{package_id:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",published_at:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",version:1}};var T=require("@cetusprotocol/common-sdk"),pe=require("@mysten/sui/transactions"),xr=require("@mysten/sui/utils");var Xe=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),X("GetObjectError",r,{[T.DETAILS_KEYS.METHOD_NAME]:"getOwnerPositionList",[T.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),X("GetObjectError",t,{[T.DETAILS_KEYS.METHOD_NAME]:"getPosition",[T.DETAILS_KEYS.REQUEST_PARAMS]:i})}}collectFeePayload(i,t){let{pool_id:r,position_id:e,coin_type_a:s,coin_type_b:u}=i,{dlmm_pool:d}=this.sdk.sdkOptions,{versioned_id:f,global_config_id:c}=(0,T.getPackagerConfigs)(d);t=t||new pe.Transaction;let[p,g]=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,u]}),y=T.CoinAssist.fromBalance(p,s,t),P=T.CoinAssist.fromBalance(g,u,t);return t.transferObjects([y,P],this.sdk.getSenderAddress()),t}updatePositionFeeAndRewards(i,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:e}=(0,T.getPackagerConfigs)(r),{pool_id:s,position_id:u,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(u),t.object(e),t.object(T.CLOCK_ADDRESS)],typeArguments:[d,f]}),t}collectRewardPayload(i,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:e,global_config_id:s}=(0,T.getPackagerConfigs)(r);return t=t||new pe.Transaction,i.forEach(u=>{let{pool_id:d,position_id:f,reward_coins:c,coin_type_a:p,coin_type_b:g}=u;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,g,y]}),S=T.CoinAssist.fromBalance(P,y,t);t.transferObjects([S],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:u,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:u,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:u,coin_type_a:d,coin_type_b:f}=i,{dlmm_router:c}=this.sdk.sdkOptions,p=F.getBinShift(e,u,s),g=Number((0,T.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(g)),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:u,coin_type_b:d}=i,{dlmm_pool:f}=this.sdk.sdkOptions,{versioned_id:c,global_config_id:p}=(0,T.getPackagerConfigs)(f);t=t||new pe.Transaction,this.updatePositionFeeAndRewards({pool_id:r,position_id:e,coin_type_a:u,coin_type_b:d},t),this.collectRewardPayload([{pool_id:r,position_id:e,reward_coins:s,coin_type_a:u,coin_type_b:d}],t);let[g,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(T.CLOCK_ADDRESS)],typeArguments:[u,d]}),S=T.CoinAssist.fromBalance(y,u,t),B=T.CoinAssist.fromBalance(P,d,t);return t.moveCall({target:`${f.published_at}::pool::destroy_close_position_cert`,arguments:[g,t.object(c)],typeArguments:[]}),t.transferObjects([S,B],this.sdk.getSenderAddress()),t}async getActiveBinIfInRange(i,t,r,e,s,u=!1){if(e<=r&&e>=t)return await this._sdk.Pool.getBinInfo(i,e,s,u)}calculateRemoveLiquidityInfo(i){let{bins:t,active_id:r,coin_amount:e}=i,s="fix_amount_a"in i,u=t.filter(y=>y.bin_id<r),d=t.filter(y=>y.bin_id>r),f=(0,T.d)(0),c=(0,T.d)(0),p=[];if(s){p=[...t];let{fix_amount_a:y}=i,P=t.find(C=>C.bin_id===r),S=d.reduce((C,k)=>(0,T.d)(C).plus(k.amount_a),(0,T.d)(0)).add(P?.amount_a||"0"),B=u.reduce((C,k)=>(0,T.d)(C).plus(k.amount_b),(0,T.d)(0)).add(P?.amount_b||"0");f=y?S:B,c=(0,T.d)(e).gte(f)?(0,T.d)(1):(0,T.d)(e).div(f)}else{let{is_only_a:y}=i;p=y?d:u,f=p.reduce((P,S)=>(0,T.d)(P).plus(y?S.amount_a:S.amount_b),(0,T.d)(0)),c=(0,T.d)(e).gte(f)?(0,T.d)(1):(0,T.d)(e).div(f)}if((0,T.d)(f).isZero())return X("InsufficientLiquidity",new Error("Insufficient liquidity"),{[T.DETAILS_KEYS.METHOD_NAME]:"calculateRemoveLiquidityResult",[T.DETAILS_KEYS.REQUEST_PARAMS]:i});let g=F.processBinsByRate([...p],c.toFixed());if(g.has_invalid_amount){let y=(0,T.d)(e),P=[],S=s?i.fix_amount_a:i.is_only_a;for(let B of p){if(y.lte(0))break;let C=S?B.amount_a:B.amount_b,k=S?B.amount_b:B.amount_a,R=(0,T.d)((0,T.d)(y).div(C).toFixed(9,q.ROUND_UP));R=R.gt(1)?(0,T.d)(1):R;let I=(0,T.d)(C).mul(R);y=y.minus(I);let x=(0,T.d)(k).mul(R),z=(0,T.d)(B.liquidity).mul(R);P.push({...B,amount_a:S?I.toFixed(0):x.toFixed(0),amount_b:S?x.toFixed(0):I.toFixed(0),liquidity:z.toFixed(0)})}return{bins:P,amount_a:P.reduce((B,C)=>(0,T.d)(B).plus(C.amount_a),(0,T.d)(0)).toFixed(0),amount_b:P.reduce((B,C)=>(0,T.d)(B).plus(C.amount_b),(0,T.d)(0)).toFixed(0)}}return g.bins}async calculateAddLiquidityInfo(i){let t="fix_amount_a"in i,{active_id:r,bin_step:e,lower_bin_id:s,upper_bin_id:u,active_bin_of_pool:d,strategy_type:f,pool_id:c}=i,p;if(t){let{coin_amount:g,fix_amount_a:y}=i;p=Ae.autoFillCoinByStrategyV2(r,e,g,y,s,u,f,d)}else p=Ae.toAmountsBothSideByStrategy(r,e,s,u,i.amount_a,i.amount_b,f,d);if(d&&c){let g=p.bins.findIndex(y=>y.bin_id===r);if(g!==-1){let y=await this._sdk.Pool.getPool(c,!1);if(y){let{fees_a:P,fees_b:S}=Oe.getCompositionFees(d,p.bins[g],y.variable_parameters),B=p.bins[g];B.amount_a=(0,T.d)(B.amount_a).sub(P).toFixed(0),B.amount_b=(0,T.d)(B.amount_b).sub(S).toFixed(0),p.bins[g]=B,console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_a:",P),console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_b:",S)}}}return p}removeLiquidityPayload(i){let{pool_id:t,position_id:r,bin_infos:e,reward_coins:s,slippage:u,coin_type_a:d,coin_type_b:f,active_id:c,collect_fee:p,bin_step:g,remove_percent:y}=i,{dlmm_pool:P}=this.sdk.sdkOptions,{bins:S}=e,B=new pe.Transaction;(p||s.length>0)&&this.updatePositionFeeAndRewards({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},B),p&&this.collectFeePayload({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},B),this.collectRewardPayload([{pool_id:t,position_id:r,reward_coins:s,coin_type_a:d,coin_type_b:f}],B);let{versioned_id:C,global_config_id:k}=(0,T.getPackagerConfigs)(P);if(y){let R=(0,T.asUintN)(BigInt(S[0].bin_id)),I=(0,T.asUintN)(BigInt(S[S.length-1].bin_id)),x=Number((0,T.d)(y).mul(1e4).toFixed(0)),[z,Z]=B.moveCall({target:`${P.published_at}::pool::remove_liquidity_by_percent`,arguments:[B.object(t),B.object(r),B.pure.u32(Number(R)),B.pure.u32(Number(I)),B.pure.u16(x),B.object(k),B.object(C),B.object(T.CLOCK_ADDRESS)],typeArguments:[d,f]}),J=T.CoinAssist.fromBalance(z,d,B),it=T.CoinAssist.fromBalance(Z,f,B);B.transferObjects([J,it],this.sdk.getSenderAddress())}else{let R=B.pure.vector("u32",S.map(it=>Number((0,T.asUintN)(BigInt(it.bin_id))))),I=B.pure.vector("u128",S.map(it=>it.liquidity)),[x,z]=B.moveCall({target:`${P.published_at}::pool::remove_liquidity`,arguments:[B.object(t),B.object(r),R,I,B.object(k),B.object(C),B.object(T.CLOCK_ADDRESS)],typeArguments:[d,f]}),Z=T.CoinAssist.fromBalance(x,d,B),J=T.CoinAssist.fromBalance(z,f,B);B.transferObjects([Z,J],this.sdk.getSenderAddress())}return this.validateActiveIdSlippage({pool_id:t,active_id:c,max_price_slippage:u,bin_step:g,coin_type_a:d,coin_type_b:f},B),B}async addLiquidityWithPricePayload(i){let{pool_id:t,bin_infos:r,coin_type_a:e,coin_type_b:s,price_base_coin:u,price:d,lower_price:f,upper_price:c,bin_step:p,strategy_type:g,active_bin_of_pool:y,decimals_a:P,decimals_b:S,use_bin_infos:B,max_price_slippage:C}=i,k,R,I,x=r;if(u==="coin_a")k=F.getBinIdFromPrice(f,p,!1,P,S),R=F.getBinIdFromPrice(c,p,!0,P,S),I=F.getBinIdFromPrice(d,p,!0,P,S);else{k=F.getBinIdFromPrice((0,T.d)(1).div(c).toString(),p,!1,P,S),R=F.getBinIdFromPrice((0,T.d)(1).div(f).toString(),p,!0,P,S),I=F.getBinIdFromPrice((0,T.d)(1).div(d).toString(),p,!1,P,S);let J={amount_a:r.amount_b,amount_b:r.amount_a,active_id:I,bin_step:p,lower_bin_id:k,upper_bin_id:R,active_bin_of_pool:y,strategy_type:g};x=await this.sdk.Position.calculateAddLiquidityInfo(J)}let Z={pool_id:t,active_id:I,bin_infos:x,coin_type_a:e,coin_type_b:s,lower_bin_id:k,upper_bin_id:R,strategy_type:g,use_bin_infos:B,max_price_slippage:C,bin_step:p};return this.addLiquidityPayload(Z)}addLiquidityPayload(i,t){let{pool_id:r,bin_infos:e,coin_type_a:s,coin_type_b:u,active_id:d,strategy_type:f,max_price_slippage:c,bin_step:p,use_bin_infos:g=!1}=i;t=t||new pe.Transaction;let y="lower_bin_id"in i,P=[];if(y){let S=F.splitBinLiquidityInfo(e,i.lower_bin_id,i.upper_bin_id);P.push(...S)}else{let S=i.position_id;P.push(e),(i.collect_fee||i.reward_coins.length>0)&&this.updatePositionFeeAndRewards({pool_id:r,position_id:S,coin_type_a:s,coin_type_b:u},t),i.collect_fee&&this.collectFeePayload({pool_id:r,position_id:S,coin_type_a:s,coin_type_b:u},t),i.reward_coins.length>0&&this.collectRewardPayload([{pool_id:r,position_id:S,reward_coins:i.reward_coins,coin_type_a:s,coin_type_b:u}],t)}return P.forEach((S,B)=>{console.log("\u{1F680} ~ PositionModule ~ addLiquidityPayload ~ liquidity_bin:",B,S);let{amount_a:C,amount_b:k,bins:R}=S,I=T.CoinAssist.buildCoinWithBalance(BigInt(C),s,t),x=T.CoinAssist.buildCoinWithBalance(BigInt(k),u,t);g?this.addLiquidityInternal({pool_id:r,coin_type_a:s,coin_type_b:u,active_id:d,liquidity_bin:S,tx:t,coin_a_obj_id:I,coin_b_obj_id:x,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:u,active_id:d,liquidity_bin:S,tx:t,max_price_slippage:c,bin_step:p,coin_a_obj_id:I,coin_b_obj_id:x,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:u,coin_type_b:d,active_id:f,liquidity_bin:c,tx:p,coin_a_obj_id:g,coin_b_obj_id:y,strategy_type:P}=i,{dlmm_pool:S,dlmm_router:B}=this.sdk.sdkOptions,{versioned_id:C,global_config_id:k}=(0,T.getPackagerConfigs)(S),{bins:R,amount_a:I,amount_b:x}=c,z=e,Z=R[0].bin_id,J=R[R.length-1].bin_id,it=(0,T.asUintN)(BigInt(Z)),tt=Number((0,T.asUintN)(BigInt(f))),ot=F.getBinShift(f,r,t),K=qi(P);if(e===void 0){let st=J-Z+1;st>1e3&&X("InvalidBinWidth",new Error("Width is too large"),{[T.DETAILS_KEYS.METHOD_NAME]:"openPosition"}),z=p.moveCall({target:`${B.published_at}::${K}::open_position`,arguments:[typeof s=="string"?p.object(s):s,g,y,p.pure.u64(I),p.pure.u64(x),p.pure.u32(Number(it)),p.pure.u16(Number(st)),p.pure.u32(Number(tt)),p.pure.u32(Number(ot)),p.object(k),p.object(C),p.object(T.CLOCK_ADDRESS)],typeArguments:[u,d]})}else{let st=R.filter(l=>l.amount_a!=="0"||l.amount_b!=="0");if(st.length===0)return X("InvalidParams",new Error("No bins to add liquidity"),{[T.DETAILS_KEYS.METHOD_NAME]:"addLiquidityStrategyInternal",[T.DETAILS_KEYS.REQUEST_PARAMS]:i});let v=(0,T.asUintN)(BigInt(st[0].bin_id)),o=(0,T.asUintN)(BigInt(st[st.length-1].bin_id));p.moveCall({target:`${B.published_at}::${K}::add_liquidity`,arguments:[typeof s=="string"?p.object(s):s,p.object(e),g,y,p.pure.u64(I),p.pure.u64(x),p.pure.u32(Number(v)),p.pure.u32(Number(o)),p.pure.u32(Number(tt)),p.pure.u32(Number(ot)),p.object(k),p.object(C),p.object(T.CLOCK_ADDRESS)],typeArguments:[u,d]})}return z?p.transferObjects([z,g,y],this.sdk.getSenderAddress()):p.transferObjects([g,y],this.sdk.getSenderAddress()),p}addLiquidityInternal(i){let{position_id:t,pool_id:r,coin_type_a:e,coin_type_b:s,active_id:u,liquidity_bin:d,tx:f,coin_a_obj_id:c,coin_b_obj_id:p,max_price_slippage:g,bin_step:y}=i,{bins:P}=d,{dlmm_pool:S,dlmm_router:B}=this.sdk.sdkOptions,{versioned_id:C,global_config_id:k}=(0,T.getPackagerConfigs)(S),R=f.pure.vector("u64",P.map(J=>J.amount_a)),I=f.pure.vector("u64",P.map(J=>J.amount_b)),x=f.makeMoveVec({elements:P.map(J=>f.pure.u32(Number((0,T.asUintN)(BigInt(J.bin_id))))),type:"u32"}),z=d.bins[0].bin_id,Z=d.bins[d.bins.length-1].bin_id;if(t===void 0){Z-z+1>1e3&&X("InvalidBinWidth",new Error("Width is too large"),{[T.DETAILS_KEYS.METHOD_NAME]:"openPosition"});let it=f.moveCall({target:`${B.published_at}::add_liquidity::open_position`,arguments:[typeof r=="string"?f.object(r):r,c,p,x,R,I,f.object(k),f.object(C),f.object(T.CLOCK_ADDRESS)],typeArguments:[e,s]});u>=z&&u<=Z&&this.validateActiveIdSlippage({pool_id:r,active_id:u,max_price_slippage:g,bin_step:y,coin_type_a:e,coin_type_b:s},f),f.transferObjects([c,p,it],this.sdk.getSenderAddress())}else f.moveCall({target:`${B.published_at}::add_liquidity::add_liquidity`,arguments:[typeof r=="string"?f.object(r):r,f.object(t),c,p,x,R,I,f.object(k),f.object(C),f.object(T.CLOCK_ADDRESS)],typeArguments:[e,s]}),u>=z&&u<=Z&&this.validateActiveIdSlippage({pool_id:r,active_id:u,max_price_slippage:g,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,xr.normalizeSuiAddress)("0x0")});if(r.error!=null)return X("FetchError",new Error(r.error),{[T.DETAILS_KEYS.METHOD_NAME]:"fetchPositionFeeAndReward",[T.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 Tr=require("@mysten/sui/utils");var Je=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:u,by_amount_in:d,in_amount:f}=i,{versioned_id:c,global_config_id:p}=(0,Gt.getPackagerConfigs)(t),g=new Ui.Transaction;g.moveCall({target:`${t.published_at}::pool::flash_swap`,arguments:[g.object(r),g.pure.bool(u),g.pure.bool(d),g.pure.u64(f),g.object(p),g.object(c),g.object(Gt.CLOCK_ADDRESS)],typeArguments:[e,s]});let y=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:g,sender:(0,Tr.normalizeSuiAddress)("0x0")});if(y.error!=null)return X("FetchError",new Error(y.error),{[Gt.DETAILS_KEYS.METHOD_NAME]:"fetchSwapQuote",[Gt.DETAILS_KEYS.REQUEST_PARAMS]:i});let P=Fi(y,u);return P==null?X("FetchError",new Error("No quote info"),{[Gt.DETAILS_KEYS.METHOD_NAME]:"preSwapQuote",[Gt.DETAILS_KEYS.REQUEST_PARAMS]:i}):(P.a2b=u,P)}swapPayload(i){let{dlmm_pool:t,dlmm_router:r}=this._sdk.sdkOptions,{quote_obj:e,coin_type_a:s,coin_type_b:u,by_amount_in:d,slippage:f,partner:c}=i,{pool_id:p,in_amount:g,out_amount:y,a2b:P}=e,S=new Ui.Transaction,B=d?g:(0,Gt.d)(g).mul(1+f).toFixed(0),C=d?(0,Gt.d)(y).mul(1-f).toFixed(0):y,k=Gt.CoinAssist.buildCoinWithBalance(BigInt(B),P?s:u,S),{versioned_id:R,global_config_id:I}=(0,Gt.getPackagerConfigs)(t);return console.log("\u{1F680} ~ SwapModule ~ option:",{...i,in_amount_limit:B,out_amount_limit:C}),c?S.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b_with_partner":"swap_b2a_with_partner"}`,arguments:[S.object(p),S.object(c),k,S.pure.bool(d),S.pure.u64(BigInt(d?g:y)),S.pure.u64(BigInt(d?C:B)),S.object(I),S.object(R),S.object(Gt.CLOCK_ADDRESS)],typeArguments:[s,u]}):S.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b":"swap_b2a"}`,arguments:[S.object(p),k,S.pure.bool(d),S.pure.u64(BigInt(d?g:y)),S.pure.u64(BigInt(d?C:B)),S.object(I),S.object(R),S.object(Gt.CLOCK_ADDRESS)],typeArguments:[s,u]}),S.transferObjects([k],this.sdk.getSenderAddress()),S}};var ke=require("@mysten/sui/transactions"),Y=require("@cetusprotocol/common-sdk");var Ye=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,Y.getPackagerConfigs)(t);try{let e=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}}),u=(0,Y.getObjectFields)(e).partners.fields.contents.map(d=>d.fields.value);u.length>0&&(await this._sdk.FullClient.batchGetObjects(u,{showContent:!0,showType:!0})).forEach(f=>{let c=ze(f);i.push(c)})}catch(e){return X("FetchError",e,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerList",[Y.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 u=await this._sdk.FullClient.getOwnedObjects({owner:i,options:{showContent:!0,showType:!0},filter:{StructType:`${r.package_id}::partner::PartnerCap`}}),d;return u.data.forEach(f=>{let c=(0,Y.getObjectFields)(f);c.partner_id===t&&(d=c.id.id,this._sdk.updateCache(e,d))}),d||X("NotFound",new Error("Partner cap not found"),{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[Y.DETAILS_KEYS.REQUEST_PARAMS]:{owner:i,partner_id:t}})}catch(e){return X("FetchError",e,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[Y.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(u=>{let d=(0,Y.getObjectFields)(u);console.log(d),r.push({coin_type:(0,Y.fixCoinType)(d.name,!1),balance:d.value})}),r}catch(t){return X("FetchError",t,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerBalance",[Y.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 X("FetchError",t,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartner",[Y.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:u,versioned_id:d}=(0,Y.getPackagerConfigs)(r);t=t||new ke.Transaction;let f=(0,Y.d)(s).mul(1e4);return f.gt(1e4)?X("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[Y.DETAILS_KEYS.METHOD_NAME]:"updateRefFeeRatePayload",[Y.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(u),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:u}=(0,Y.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(u)],typeArguments:[c]})}),d}updateTimeRangePayload(i,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{partner_id:e,start_time:s,end_time:u}=i,{global_config_id:d,versioned_id:f}=(0,Y.getPackagerConfigs)(r);t=t||new ke.Transaction;let c=s>1e12?Math.floor(s/1e3):s,p=u>1e12?Math.floor(u/1e3):u;return p<=c?X("InvalidParams",new Error("end_time must be greater than start_time"),{[Y.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[Y.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(Y.CLOCK_ADDRESS)],typeArguments:[]}),t)}createPartnerPayload(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{name:r,ref_fee_rate:e,start_time:s,end_time:u,recipient:d}=i,{partners_id:f,global_config_id:c,versioned_id:p}=(0,Y.getPackagerConfigs)(t),g=new ke.Transaction,y=(0,Y.d)(e).mul(1e4);if(y.gt(1e4))return X("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[Y.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[Y.DETAILS_KEYS.REQUEST_PARAMS]:i});let P=s>1e12?Math.floor(s/1e3):s,S=u>1e12?Math.floor(u/1e3):u;return S<=P?X("InvalidParams",new Error("end_time must be greater than start_time"),{[Y.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[Y.DETAILS_KEYS.REQUEST_PARAMS]:i}):(g.moveCall({target:`${t.published_at}::partner::create_partner`,arguments:[g.object(f),g.pure.string(r),g.pure.u64(y.toNumber()),g.pure.u64(P),g.pure.u64(S),g.pure.address(d),g.object(c),g.object(p)],typeArguments:[]}),g)}};var ti=require("@mysten/sui/transactions"),ct=require("@cetusprotocol/common-sdk"),Hi=Mi(Ne()),ei=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=[],u=e.data.map(p=>p.objectId);u.length>0&&(await this._sdk.FullClient.batchGetObjects(u,{showContent:!0})).forEach(g=>{let y=(0,ct.getObjectFields)(g),P=ct.MathUtil.u128ToI128(new Hi.default(y.value.fields.value.fields.bits)).toString(),S=y.name,B=new Date(Number(S)*1e3).toLocaleString(),k={emissions_per_second:"0",emissions_per_day:"0",emissions_per:ct.MathUtil.fromX64(new Hi.default(P)).toString(),time:S,visualized_time:B};s.push(k)});let d=s.sort((p,g)=>Number(p.time)-Number(g.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 g=d[p];if((0,ct.d)(g.time).lte(r))continue;c=(0,ct.d)(c).add((0,ct.d)(g.emissions_per)).toString();let y=(0,ct.d)(c).mul(3600*24).toString();(0,ct.d)(c).lt(0)?(g.emissions_per_second="0",g.emissions_per_day="0"):(g.emissions_per_second=c,g.emissions_per_day=y),f.push(g)}return f}addRewardPayload(i,t){t=t||new ti.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:u,reward_coin_type:d,reward_amount:f,start_time_seconds:c,end_time_seconds:p,coin_type_a:g,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(u),P,t.pure.option("u64",c),t.pure.u64(p),t.object(e),t.object(s),t.object(ct.CLOCK_ADDRESS)],typeArguments:[g,y,d]}),t}initRewardPayload(i,t){t=t||new ti.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:u,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(u),t.object(e),t.object(s),t.object(ct.CLOCK_ADDRESS)],typeArguments:[f,c,p]})}),t}buildRewardAccessPayload(i,t){t=t||new ti.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ct.getPackagerConfigs)(r),{pool_id:u,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(u),t.object(e),t.object(s)],typeArguments:[f,c]}),t}};var Cr=require("@mysten/sui/transactions"),ie=require("@cetusprotocol/common-sdk");var ii=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}buildRewardWhiteListPayload(i,t){t=t||new Cr.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=(0,ie.getPackagerConfigs)(r),{reward_coin_types:u,type:d}=i;return u.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.getDynamicFields({parentId:i}),r=[],e=t.data.map(s=>s.objectId);return e.length>0&&(await this._sdk.FullClient.batchGetObjects(e,{showContent:!0})).forEach(u=>{let f={...(0,ie.getObjectFields)(u).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,u=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:u,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),X("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 u=await this._sdk.FullClient.getObject({id:t.registry_id,options:{showContent:!0}}),d=(0,ie.getObjectFields)(u);return t.pools_id=d.pools.fields.id.id,t}};var Re=class n extends kr.SdkWrapper{constructor(i){super(i),this._pool=new Ce(this),this._position=new Xe(this),this._swap=new Je(this),this._partner=new Ye(this),this._reward=new ei(this),this._config=new ii(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({...Pi,...i}):n.createCustomSDK({...Zi,...i})}static createCustomSDK(i){return new n(i)}};var Kn=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: