@cetusprotocol/sui-clmm-sdk 1.0.1 → 1.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.mjs CHANGED
@@ -1,4 +1,4 @@
1
- var nr=Object.create;var En=Object.defineProperty;var or=Object.getOwnPropertyDescriptor;var rr=Object.getOwnPropertyNames;var sr=Object.getPrototypeOf,ar=Object.prototype.hasOwnProperty;var cr=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var lr=(o,t)=>()=>(t||o((t={exports:{}}).exports,t),t.exports);var ur=(o,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of rr(t))!ar.call(o,n)&&n!==e&&En(o,n,{get:()=>t[n],enumerable:!(i=or(t,n))||i.enumerable});return o};var ne=(o,t,e)=>(e=o!=null?nr(sr(o)):{},ur(t||!o||!o.__esModule?En(e,"default",{value:o,enumerable:!0}):e,o));var Gt=lr((ro,tn)=>{"use strict";(function(o,t){"use strict";function e(y,s){if(!y)throw new Error(s||"Assertion failed")}function i(y,s){y.super_=s;var l=function(){};l.prototype=s.prototype,y.prototype=new l,y.prototype.constructor=y}function n(y,s,l){if(n.isBN(y))return y;this.negative=0,this.words=null,this.length=0,this.red=null,y!==null&&((s==="le"||s==="be")&&(l=s,s=10),this._init(y||0,s||10,l||"be"))}typeof o=="object"?o.exports=n:t.BN=n,n.BN=n,n.wordSize=26;var r;try{typeof window<"u"&&typeof window.Buffer<"u"?r=window.Buffer:r=cr("buffer").Buffer}catch{}n.isBN=function(s){return s instanceof n?!0:s!==null&&typeof s=="object"&&s.constructor.wordSize===n.wordSize&&Array.isArray(s.words)},n.max=function(s,l){return s.cmp(l)>0?s:l},n.min=function(s,l){return s.cmp(l)<0?s:l},n.prototype._init=function(s,l,_){if(typeof s=="number")return this._initNumber(s,l,_);if(typeof s=="object")return this._initArray(s,l,_);l==="hex"&&(l=16),e(l===(l|0)&&l>=2&&l<=36),s=s.toString().replace(/\s+/g,"");var p=0;s[0]==="-"&&(p++,this.negative=1),p<s.length&&(l===16?this._parseHex(s,p,_):(this._parseBase(s,l,p),_==="le"&&this._initArray(this.toArray(),l,_)))},n.prototype._initNumber=function(s,l,_){s<0&&(this.negative=1,s=-s),s<67108864?(this.words=[s&67108863],this.length=1):s<4503599627370496?(this.words=[s&67108863,s/67108864&67108863],this.length=2):(e(s<9007199254740992),this.words=[s&67108863,s/67108864&67108863,1],this.length=3),_==="le"&&this._initArray(this.toArray(),l,_)},n.prototype._initArray=function(s,l,_){if(e(typeof s.length=="number"),s.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(s.length/3),this.words=new Array(this.length);for(var p=0;p<this.length;p++)this.words[p]=0;var g,v,C=0;if(_==="be")for(p=s.length-1,g=0;p>=0;p-=3)v=s[p]|s[p-1]<<8|s[p-2]<<16,this.words[g]|=v<<C&67108863,this.words[g+1]=v>>>26-C&67108863,C+=24,C>=26&&(C-=26,g++);else if(_==="le")for(p=0,g=0;p<s.length;p+=3)v=s[p]|s[p+1]<<8|s[p+2]<<16,this.words[g]|=v<<C&67108863,this.words[g+1]=v>>>26-C&67108863,C+=24,C>=26&&(C-=26,g++);return this._strip()};function a(y,s){var l=y.charCodeAt(s);if(l>=48&&l<=57)return l-48;if(l>=65&&l<=70)return l-55;if(l>=97&&l<=102)return l-87;e(!1,"Invalid character in "+y)}function u(y,s,l){var _=a(y,l);return l-1>=s&&(_|=a(y,l-1)<<4),_}n.prototype._parseHex=function(s,l,_){this.length=Math.ceil((s.length-l)/6),this.words=new Array(this.length);for(var p=0;p<this.length;p++)this.words[p]=0;var g=0,v=0,C;if(_==="be")for(p=s.length-1;p>=l;p-=2)C=u(s,l,p)<<g,this.words[v]|=C&67108863,g>=18?(g-=18,v+=1,this.words[v]|=C>>>26):g+=8;else{var m=s.length-l;for(p=m%2===0?l+1:l;p<s.length;p+=2)C=u(s,l,p)<<g,this.words[v]|=C&67108863,g>=18?(g-=18,v+=1,this.words[v]|=C>>>26):g+=8}this._strip()};function d(y,s,l,_){for(var p=0,g=0,v=Math.min(y.length,l),C=s;C<v;C++){var m=y.charCodeAt(C)-48;p*=_,m>=49?g=m-49+10:m>=17?g=m-17+10:g=m,e(m>=0&&g<_,"Invalid character"),p+=g}return p}n.prototype._parseBase=function(s,l,_){this.words=[0],this.length=1;for(var p=0,g=1;g<=67108863;g*=l)p++;p--,g=g/l|0;for(var v=s.length-_,C=v%p,m=Math.min(v,v-C)+_,c=0,b=_;b<m;b+=p)c=d(s,b,b+p,l),this.imuln(g),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c);if(C!==0){var A=1;for(c=d(s,b,s.length,l),b=0;b<C;b++)A*=l;this.imuln(A),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c)}this._strip()},n.prototype.copy=function(s){s.words=new Array(this.length);for(var l=0;l<this.length;l++)s.words[l]=this.words[l];s.length=this.length,s.negative=this.negative,s.red=this.red};function f(y,s){y.words=s.words,y.length=s.length,y.negative=s.negative,y.red=s.red}if(n.prototype._move=function(s){f(s,this)},n.prototype.clone=function(){var s=new n(null);return this.copy(s),s},n.prototype._expand=function(s){for(;this.length<s;)this.words[this.length++]=0;return this},n.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},n.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{n.prototype[Symbol.for("nodejs.util.inspect.custom")]=h}catch{n.prototype.inspect=h}else n.prototype.inspect=h;function h(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var w=["","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"],P=[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],M=[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];n.prototype.toString=function(s,l){s=s||10,l=l|0||1;var _;if(s===16||s==="hex"){_="";for(var p=0,g=0,v=0;v<this.length;v++){var C=this.words[v],m=((C<<p|g)&16777215).toString(16);g=C>>>24-p&16777215,p+=2,p>=26&&(p-=26,v--),g!==0||v!==this.length-1?_=w[6-m.length]+m+_:_=m+_}for(g!==0&&(_=g.toString(16)+_);_.length%l!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}if(s===(s|0)&&s>=2&&s<=36){var c=P[s],b=M[s];_="";var A=this.clone();for(A.negative=0;!A.isZero();){var S=A.modrn(b).toString(s);A=A.idivn(b),A.isZero()?_=S+_:_=w[c-S.length]+S+_}for(this.isZero()&&(_="0"+_);_.length%l!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}e(!1,"Base should be between 2 and 36")},n.prototype.toNumber=function(){var s=this.words[0];return this.length===2?s+=this.words[1]*67108864:this.length===3&&this.words[2]===1?s+=4503599627370496+this.words[1]*67108864:this.length>2&&e(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-s:s},n.prototype.toJSON=function(){return this.toString(16,2)},r&&(n.prototype.toBuffer=function(s,l){return this.toArrayLike(r,s,l)}),n.prototype.toArray=function(s,l){return this.toArrayLike(Array,s,l)};var T=function(s,l){return s.allocUnsafe?s.allocUnsafe(l):new s(l)};n.prototype.toArrayLike=function(s,l,_){this._strip();var p=this.byteLength(),g=_||Math.max(1,p);e(p<=g,"byte array longer than desired length"),e(g>0,"Requested array length <= 0");var v=T(s,g),C=l==="le"?"LE":"BE";return this["_toArrayLike"+C](v,p),v},n.prototype._toArrayLikeLE=function(s,l){for(var _=0,p=0,g=0,v=0;g<this.length;g++){var C=this.words[g]<<v|p;s[_++]=C&255,_<s.length&&(s[_++]=C>>8&255),_<s.length&&(s[_++]=C>>16&255),v===6?(_<s.length&&(s[_++]=C>>24&255),p=0,v=0):(p=C>>>24,v+=2)}if(_<s.length)for(s[_++]=p;_<s.length;)s[_++]=0},n.prototype._toArrayLikeBE=function(s,l){for(var _=s.length-1,p=0,g=0,v=0;g<this.length;g++){var C=this.words[g]<<v|p;s[_--]=C&255,_>=0&&(s[_--]=C>>8&255),_>=0&&(s[_--]=C>>16&255),v===6?(_>=0&&(s[_--]=C>>24&255),p=0,v=0):(p=C>>>24,v+=2)}if(_>=0)for(s[_--]=p;_>=0;)s[_--]=0},Math.clz32?n.prototype._countBits=function(s){return 32-Math.clz32(s)}:n.prototype._countBits=function(s){var l=s,_=0;return l>=4096&&(_+=13,l>>>=13),l>=64&&(_+=7,l>>>=7),l>=8&&(_+=4,l>>>=4),l>=2&&(_+=2,l>>>=2),_+l},n.prototype._zeroBits=function(s){if(s===0)return 26;var l=s,_=0;return(l&8191)===0&&(_+=13,l>>>=13),(l&127)===0&&(_+=7,l>>>=7),(l&15)===0&&(_+=4,l>>>=4),(l&3)===0&&(_+=2,l>>>=2),(l&1)===0&&_++,_},n.prototype.bitLength=function(){var s=this.words[this.length-1],l=this._countBits(s);return(this.length-1)*26+l};function x(y){for(var s=new Array(y.bitLength()),l=0;l<s.length;l++){var _=l/26|0,p=l%26;s[l]=y.words[_]>>>p&1}return s}n.prototype.zeroBits=function(){if(this.isZero())return 0;for(var s=0,l=0;l<this.length;l++){var _=this._zeroBits(this.words[l]);if(s+=_,_!==26)break}return s},n.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},n.prototype.toTwos=function(s){return this.negative!==0?this.abs().inotn(s).iaddn(1):this.clone()},n.prototype.fromTwos=function(s){return this.testn(s-1)?this.notn(s).iaddn(1).ineg():this.clone()},n.prototype.isNeg=function(){return this.negative!==0},n.prototype.neg=function(){return this.clone().ineg()},n.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},n.prototype.iuor=function(s){for(;this.length<s.length;)this.words[this.length++]=0;for(var l=0;l<s.length;l++)this.words[l]=this.words[l]|s.words[l];return this._strip()},n.prototype.ior=function(s){return e((this.negative|s.negative)===0),this.iuor(s)},n.prototype.or=function(s){return this.length>s.length?this.clone().ior(s):s.clone().ior(this)},n.prototype.uor=function(s){return this.length>s.length?this.clone().iuor(s):s.clone().iuor(this)},n.prototype.iuand=function(s){var l;this.length>s.length?l=s:l=this;for(var _=0;_<l.length;_++)this.words[_]=this.words[_]&s.words[_];return this.length=l.length,this._strip()},n.prototype.iand=function(s){return e((this.negative|s.negative)===0),this.iuand(s)},n.prototype.and=function(s){return this.length>s.length?this.clone().iand(s):s.clone().iand(this)},n.prototype.uand=function(s){return this.length>s.length?this.clone().iuand(s):s.clone().iuand(this)},n.prototype.iuxor=function(s){var l,_;this.length>s.length?(l=this,_=s):(l=s,_=this);for(var p=0;p<_.length;p++)this.words[p]=l.words[p]^_.words[p];if(this!==l)for(;p<l.length;p++)this.words[p]=l.words[p];return this.length=l.length,this._strip()},n.prototype.ixor=function(s){return e((this.negative|s.negative)===0),this.iuxor(s)},n.prototype.xor=function(s){return this.length>s.length?this.clone().ixor(s):s.clone().ixor(this)},n.prototype.uxor=function(s){return this.length>s.length?this.clone().iuxor(s):s.clone().iuxor(this)},n.prototype.inotn=function(s){e(typeof s=="number"&&s>=0);var l=Math.ceil(s/26)|0,_=s%26;this._expand(l),_>0&&l--;for(var p=0;p<l;p++)this.words[p]=~this.words[p]&67108863;return _>0&&(this.words[p]=~this.words[p]&67108863>>26-_),this._strip()},n.prototype.notn=function(s){return this.clone().inotn(s)},n.prototype.setn=function(s,l){e(typeof s=="number"&&s>=0);var _=s/26|0,p=s%26;return this._expand(_+1),l?this.words[_]=this.words[_]|1<<p:this.words[_]=this.words[_]&~(1<<p),this._strip()},n.prototype.iadd=function(s){var l;if(this.negative!==0&&s.negative===0)return this.negative=0,l=this.isub(s),this.negative^=1,this._normSign();if(this.negative===0&&s.negative!==0)return s.negative=0,l=this.isub(s),s.negative=1,l._normSign();var _,p;this.length>s.length?(_=this,p=s):(_=s,p=this);for(var g=0,v=0;v<p.length;v++)l=(_.words[v]|0)+(p.words[v]|0)+g,this.words[v]=l&67108863,g=l>>>26;for(;g!==0&&v<_.length;v++)l=(_.words[v]|0)+g,this.words[v]=l&67108863,g=l>>>26;if(this.length=_.length,g!==0)this.words[this.length]=g,this.length++;else if(_!==this)for(;v<_.length;v++)this.words[v]=_.words[v];return this},n.prototype.add=function(s){var l;return s.negative!==0&&this.negative===0?(s.negative=0,l=this.sub(s),s.negative^=1,l):s.negative===0&&this.negative!==0?(this.negative=0,l=s.sub(this),this.negative=1,l):this.length>s.length?this.clone().iadd(s):s.clone().iadd(this)},n.prototype.isub=function(s){if(s.negative!==0){s.negative=0;var l=this.iadd(s);return s.negative=1,l._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(s),this.negative=1,this._normSign();var _=this.cmp(s);if(_===0)return this.negative=0,this.length=1,this.words[0]=0,this;var p,g;_>0?(p=this,g=s):(p=s,g=this);for(var v=0,C=0;C<g.length;C++)l=(p.words[C]|0)-(g.words[C]|0)+v,v=l>>26,this.words[C]=l&67108863;for(;v!==0&&C<p.length;C++)l=(p.words[C]|0)+v,v=l>>26,this.words[C]=l&67108863;if(v===0&&C<p.length&&p!==this)for(;C<p.length;C++)this.words[C]=p.words[C];return this.length=Math.max(this.length,C),p!==this&&(this.negative=1),this._strip()},n.prototype.sub=function(s){return this.clone().isub(s)};function $(y,s,l){l.negative=s.negative^y.negative;var _=y.length+s.length|0;l.length=_,_=_-1|0;var p=y.words[0]|0,g=s.words[0]|0,v=p*g,C=v&67108863,m=v/67108864|0;l.words[0]=C;for(var c=1;c<_;c++){for(var b=m>>>26,A=m&67108863,S=Math.min(c,s.length-1),R=Math.max(0,c-y.length+1);R<=S;R++){var q=c-R|0;p=y.words[q]|0,g=s.words[R]|0,v=p*g+A,b+=v/67108864|0,A=v&67108863}l.words[c]=A|0,m=b|0}return m!==0?l.words[c]=m|0:l.length--,l._strip()}var j=function(s,l,_){var p=s.words,g=l.words,v=_.words,C=0,m,c,b,A=p[0]|0,S=A&8191,R=A>>>13,q=p[1]|0,U=q&8191,W=q>>>13,zt=p[2]|0,H=zt&8191,G=zt>>>13,me=p[3]|0,Q=me&8191,J=me>>>13,He=p[4]|0,X=He&8191,Y=He>>>13,yn=p[5]|0,tt=yn&8191,et=yn>>>13,vn=p[6]|0,it=vn&8191,nt=vn>>>13,Pn=p[7]|0,ot=Pn&8191,rt=Pn>>>13,Cn=p[8]|0,st=Cn&8191,at=Cn>>>13,Mn=p[9]|0,ct=Mn&8191,lt=Mn>>>13,kn=g[0]|0,ut=kn&8191,dt=kn>>>13,An=g[1]|0,ft=An&8191,_t=An>>>13,Tn=g[2]|0,ht=Tn&8191,pt=Tn>>>13,xn=g[3]|0,mt=xn&8191,gt=xn>>>13,Sn=g[4]|0,bt=Sn&8191,wt=Sn>>>13,In=g[5]|0,yt=In&8191,vt=In>>>13,Rn=g[6]|0,Pt=Rn&8191,Ct=Rn>>>13,On=g[7]|0,Mt=On&8191,kt=On>>>13,Fn=g[8]|0,At=Fn&8191,Tt=Fn>>>13,Bn=g[9]|0,xt=Bn&8191,St=Bn>>>13;_.negative=s.negative^l.negative,_.length=19,m=Math.imul(S,ut),c=Math.imul(S,dt),c=c+Math.imul(R,ut)|0,b=Math.imul(R,dt);var Ti=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ti>>>26)|0,Ti&=67108863,m=Math.imul(U,ut),c=Math.imul(U,dt),c=c+Math.imul(W,ut)|0,b=Math.imul(W,dt),m=m+Math.imul(S,ft)|0,c=c+Math.imul(S,_t)|0,c=c+Math.imul(R,ft)|0,b=b+Math.imul(R,_t)|0;var xi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(xi>>>26)|0,xi&=67108863,m=Math.imul(H,ut),c=Math.imul(H,dt),c=c+Math.imul(G,ut)|0,b=Math.imul(G,dt),m=m+Math.imul(U,ft)|0,c=c+Math.imul(U,_t)|0,c=c+Math.imul(W,ft)|0,b=b+Math.imul(W,_t)|0,m=m+Math.imul(S,ht)|0,c=c+Math.imul(S,pt)|0,c=c+Math.imul(R,ht)|0,b=b+Math.imul(R,pt)|0;var Si=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Si>>>26)|0,Si&=67108863,m=Math.imul(Q,ut),c=Math.imul(Q,dt),c=c+Math.imul(J,ut)|0,b=Math.imul(J,dt),m=m+Math.imul(H,ft)|0,c=c+Math.imul(H,_t)|0,c=c+Math.imul(G,ft)|0,b=b+Math.imul(G,_t)|0,m=m+Math.imul(U,ht)|0,c=c+Math.imul(U,pt)|0,c=c+Math.imul(W,ht)|0,b=b+Math.imul(W,pt)|0,m=m+Math.imul(S,mt)|0,c=c+Math.imul(S,gt)|0,c=c+Math.imul(R,mt)|0,b=b+Math.imul(R,gt)|0;var Ii=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ii>>>26)|0,Ii&=67108863,m=Math.imul(X,ut),c=Math.imul(X,dt),c=c+Math.imul(Y,ut)|0,b=Math.imul(Y,dt),m=m+Math.imul(Q,ft)|0,c=c+Math.imul(Q,_t)|0,c=c+Math.imul(J,ft)|0,b=b+Math.imul(J,_t)|0,m=m+Math.imul(H,ht)|0,c=c+Math.imul(H,pt)|0,c=c+Math.imul(G,ht)|0,b=b+Math.imul(G,pt)|0,m=m+Math.imul(U,mt)|0,c=c+Math.imul(U,gt)|0,c=c+Math.imul(W,mt)|0,b=b+Math.imul(W,gt)|0,m=m+Math.imul(S,bt)|0,c=c+Math.imul(S,wt)|0,c=c+Math.imul(R,bt)|0,b=b+Math.imul(R,wt)|0;var Ri=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ri>>>26)|0,Ri&=67108863,m=Math.imul(tt,ut),c=Math.imul(tt,dt),c=c+Math.imul(et,ut)|0,b=Math.imul(et,dt),m=m+Math.imul(X,ft)|0,c=c+Math.imul(X,_t)|0,c=c+Math.imul(Y,ft)|0,b=b+Math.imul(Y,_t)|0,m=m+Math.imul(Q,ht)|0,c=c+Math.imul(Q,pt)|0,c=c+Math.imul(J,ht)|0,b=b+Math.imul(J,pt)|0,m=m+Math.imul(H,mt)|0,c=c+Math.imul(H,gt)|0,c=c+Math.imul(G,mt)|0,b=b+Math.imul(G,gt)|0,m=m+Math.imul(U,bt)|0,c=c+Math.imul(U,wt)|0,c=c+Math.imul(W,bt)|0,b=b+Math.imul(W,wt)|0,m=m+Math.imul(S,yt)|0,c=c+Math.imul(S,vt)|0,c=c+Math.imul(R,yt)|0,b=b+Math.imul(R,vt)|0;var Oi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Oi>>>26)|0,Oi&=67108863,m=Math.imul(it,ut),c=Math.imul(it,dt),c=c+Math.imul(nt,ut)|0,b=Math.imul(nt,dt),m=m+Math.imul(tt,ft)|0,c=c+Math.imul(tt,_t)|0,c=c+Math.imul(et,ft)|0,b=b+Math.imul(et,_t)|0,m=m+Math.imul(X,ht)|0,c=c+Math.imul(X,pt)|0,c=c+Math.imul(Y,ht)|0,b=b+Math.imul(Y,pt)|0,m=m+Math.imul(Q,mt)|0,c=c+Math.imul(Q,gt)|0,c=c+Math.imul(J,mt)|0,b=b+Math.imul(J,gt)|0,m=m+Math.imul(H,bt)|0,c=c+Math.imul(H,wt)|0,c=c+Math.imul(G,bt)|0,b=b+Math.imul(G,wt)|0,m=m+Math.imul(U,yt)|0,c=c+Math.imul(U,vt)|0,c=c+Math.imul(W,yt)|0,b=b+Math.imul(W,vt)|0,m=m+Math.imul(S,Pt)|0,c=c+Math.imul(S,Ct)|0,c=c+Math.imul(R,Pt)|0,b=b+Math.imul(R,Ct)|0;var Fi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Fi>>>26)|0,Fi&=67108863,m=Math.imul(ot,ut),c=Math.imul(ot,dt),c=c+Math.imul(rt,ut)|0,b=Math.imul(rt,dt),m=m+Math.imul(it,ft)|0,c=c+Math.imul(it,_t)|0,c=c+Math.imul(nt,ft)|0,b=b+Math.imul(nt,_t)|0,m=m+Math.imul(tt,ht)|0,c=c+Math.imul(tt,pt)|0,c=c+Math.imul(et,ht)|0,b=b+Math.imul(et,pt)|0,m=m+Math.imul(X,mt)|0,c=c+Math.imul(X,gt)|0,c=c+Math.imul(Y,mt)|0,b=b+Math.imul(Y,gt)|0,m=m+Math.imul(Q,bt)|0,c=c+Math.imul(Q,wt)|0,c=c+Math.imul(J,bt)|0,b=b+Math.imul(J,wt)|0,m=m+Math.imul(H,yt)|0,c=c+Math.imul(H,vt)|0,c=c+Math.imul(G,yt)|0,b=b+Math.imul(G,vt)|0,m=m+Math.imul(U,Pt)|0,c=c+Math.imul(U,Ct)|0,c=c+Math.imul(W,Pt)|0,b=b+Math.imul(W,Ct)|0,m=m+Math.imul(S,Mt)|0,c=c+Math.imul(S,kt)|0,c=c+Math.imul(R,Mt)|0,b=b+Math.imul(R,kt)|0;var Bi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Bi>>>26)|0,Bi&=67108863,m=Math.imul(st,ut),c=Math.imul(st,dt),c=c+Math.imul(at,ut)|0,b=Math.imul(at,dt),m=m+Math.imul(ot,ft)|0,c=c+Math.imul(ot,_t)|0,c=c+Math.imul(rt,ft)|0,b=b+Math.imul(rt,_t)|0,m=m+Math.imul(it,ht)|0,c=c+Math.imul(it,pt)|0,c=c+Math.imul(nt,ht)|0,b=b+Math.imul(nt,pt)|0,m=m+Math.imul(tt,mt)|0,c=c+Math.imul(tt,gt)|0,c=c+Math.imul(et,mt)|0,b=b+Math.imul(et,gt)|0,m=m+Math.imul(X,bt)|0,c=c+Math.imul(X,wt)|0,c=c+Math.imul(Y,bt)|0,b=b+Math.imul(Y,wt)|0,m=m+Math.imul(Q,yt)|0,c=c+Math.imul(Q,vt)|0,c=c+Math.imul(J,yt)|0,b=b+Math.imul(J,vt)|0,m=m+Math.imul(H,Pt)|0,c=c+Math.imul(H,Ct)|0,c=c+Math.imul(G,Pt)|0,b=b+Math.imul(G,Ct)|0,m=m+Math.imul(U,Mt)|0,c=c+Math.imul(U,kt)|0,c=c+Math.imul(W,Mt)|0,b=b+Math.imul(W,kt)|0,m=m+Math.imul(S,At)|0,c=c+Math.imul(S,Tt)|0,c=c+Math.imul(R,At)|0,b=b+Math.imul(R,Tt)|0;var Ei=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ei>>>26)|0,Ei&=67108863,m=Math.imul(ct,ut),c=Math.imul(ct,dt),c=c+Math.imul(lt,ut)|0,b=Math.imul(lt,dt),m=m+Math.imul(st,ft)|0,c=c+Math.imul(st,_t)|0,c=c+Math.imul(at,ft)|0,b=b+Math.imul(at,_t)|0,m=m+Math.imul(ot,ht)|0,c=c+Math.imul(ot,pt)|0,c=c+Math.imul(rt,ht)|0,b=b+Math.imul(rt,pt)|0,m=m+Math.imul(it,mt)|0,c=c+Math.imul(it,gt)|0,c=c+Math.imul(nt,mt)|0,b=b+Math.imul(nt,gt)|0,m=m+Math.imul(tt,bt)|0,c=c+Math.imul(tt,wt)|0,c=c+Math.imul(et,bt)|0,b=b+Math.imul(et,wt)|0,m=m+Math.imul(X,yt)|0,c=c+Math.imul(X,vt)|0,c=c+Math.imul(Y,yt)|0,b=b+Math.imul(Y,vt)|0,m=m+Math.imul(Q,Pt)|0,c=c+Math.imul(Q,Ct)|0,c=c+Math.imul(J,Pt)|0,b=b+Math.imul(J,Ct)|0,m=m+Math.imul(H,Mt)|0,c=c+Math.imul(H,kt)|0,c=c+Math.imul(G,Mt)|0,b=b+Math.imul(G,kt)|0,m=m+Math.imul(U,At)|0,c=c+Math.imul(U,Tt)|0,c=c+Math.imul(W,At)|0,b=b+Math.imul(W,Tt)|0,m=m+Math.imul(S,xt)|0,c=c+Math.imul(S,St)|0,c=c+Math.imul(R,xt)|0,b=b+Math.imul(R,St)|0;var Ni=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ni>>>26)|0,Ni&=67108863,m=Math.imul(ct,ft),c=Math.imul(ct,_t),c=c+Math.imul(lt,ft)|0,b=Math.imul(lt,_t),m=m+Math.imul(st,ht)|0,c=c+Math.imul(st,pt)|0,c=c+Math.imul(at,ht)|0,b=b+Math.imul(at,pt)|0,m=m+Math.imul(ot,mt)|0,c=c+Math.imul(ot,gt)|0,c=c+Math.imul(rt,mt)|0,b=b+Math.imul(rt,gt)|0,m=m+Math.imul(it,bt)|0,c=c+Math.imul(it,wt)|0,c=c+Math.imul(nt,bt)|0,b=b+Math.imul(nt,wt)|0,m=m+Math.imul(tt,yt)|0,c=c+Math.imul(tt,vt)|0,c=c+Math.imul(et,yt)|0,b=b+Math.imul(et,vt)|0,m=m+Math.imul(X,Pt)|0,c=c+Math.imul(X,Ct)|0,c=c+Math.imul(Y,Pt)|0,b=b+Math.imul(Y,Ct)|0,m=m+Math.imul(Q,Mt)|0,c=c+Math.imul(Q,kt)|0,c=c+Math.imul(J,Mt)|0,b=b+Math.imul(J,kt)|0,m=m+Math.imul(H,At)|0,c=c+Math.imul(H,Tt)|0,c=c+Math.imul(G,At)|0,b=b+Math.imul(G,Tt)|0,m=m+Math.imul(U,xt)|0,c=c+Math.imul(U,St)|0,c=c+Math.imul(W,xt)|0,b=b+Math.imul(W,St)|0;var ji=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(ji>>>26)|0,ji&=67108863,m=Math.imul(ct,ht),c=Math.imul(ct,pt),c=c+Math.imul(lt,ht)|0,b=Math.imul(lt,pt),m=m+Math.imul(st,mt)|0,c=c+Math.imul(st,gt)|0,c=c+Math.imul(at,mt)|0,b=b+Math.imul(at,gt)|0,m=m+Math.imul(ot,bt)|0,c=c+Math.imul(ot,wt)|0,c=c+Math.imul(rt,bt)|0,b=b+Math.imul(rt,wt)|0,m=m+Math.imul(it,yt)|0,c=c+Math.imul(it,vt)|0,c=c+Math.imul(nt,yt)|0,b=b+Math.imul(nt,vt)|0,m=m+Math.imul(tt,Pt)|0,c=c+Math.imul(tt,Ct)|0,c=c+Math.imul(et,Pt)|0,b=b+Math.imul(et,Ct)|0,m=m+Math.imul(X,Mt)|0,c=c+Math.imul(X,kt)|0,c=c+Math.imul(Y,Mt)|0,b=b+Math.imul(Y,kt)|0,m=m+Math.imul(Q,At)|0,c=c+Math.imul(Q,Tt)|0,c=c+Math.imul(J,At)|0,b=b+Math.imul(J,Tt)|0,m=m+Math.imul(H,xt)|0,c=c+Math.imul(H,St)|0,c=c+Math.imul(G,xt)|0,b=b+Math.imul(G,St)|0;var qi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(qi>>>26)|0,qi&=67108863,m=Math.imul(ct,mt),c=Math.imul(ct,gt),c=c+Math.imul(lt,mt)|0,b=Math.imul(lt,gt),m=m+Math.imul(st,bt)|0,c=c+Math.imul(st,wt)|0,c=c+Math.imul(at,bt)|0,b=b+Math.imul(at,wt)|0,m=m+Math.imul(ot,yt)|0,c=c+Math.imul(ot,vt)|0,c=c+Math.imul(rt,yt)|0,b=b+Math.imul(rt,vt)|0,m=m+Math.imul(it,Pt)|0,c=c+Math.imul(it,Ct)|0,c=c+Math.imul(nt,Pt)|0,b=b+Math.imul(nt,Ct)|0,m=m+Math.imul(tt,Mt)|0,c=c+Math.imul(tt,kt)|0,c=c+Math.imul(et,Mt)|0,b=b+Math.imul(et,kt)|0,m=m+Math.imul(X,At)|0,c=c+Math.imul(X,Tt)|0,c=c+Math.imul(Y,At)|0,b=b+Math.imul(Y,Tt)|0,m=m+Math.imul(Q,xt)|0,c=c+Math.imul(Q,St)|0,c=c+Math.imul(J,xt)|0,b=b+Math.imul(J,St)|0;var Di=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Di>>>26)|0,Di&=67108863,m=Math.imul(ct,bt),c=Math.imul(ct,wt),c=c+Math.imul(lt,bt)|0,b=Math.imul(lt,wt),m=m+Math.imul(st,yt)|0,c=c+Math.imul(st,vt)|0,c=c+Math.imul(at,yt)|0,b=b+Math.imul(at,vt)|0,m=m+Math.imul(ot,Pt)|0,c=c+Math.imul(ot,Ct)|0,c=c+Math.imul(rt,Pt)|0,b=b+Math.imul(rt,Ct)|0,m=m+Math.imul(it,Mt)|0,c=c+Math.imul(it,kt)|0,c=c+Math.imul(nt,Mt)|0,b=b+Math.imul(nt,kt)|0,m=m+Math.imul(tt,At)|0,c=c+Math.imul(tt,Tt)|0,c=c+Math.imul(et,At)|0,b=b+Math.imul(et,Tt)|0,m=m+Math.imul(X,xt)|0,c=c+Math.imul(X,St)|0,c=c+Math.imul(Y,xt)|0,b=b+Math.imul(Y,St)|0;var Li=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Li>>>26)|0,Li&=67108863,m=Math.imul(ct,yt),c=Math.imul(ct,vt),c=c+Math.imul(lt,yt)|0,b=Math.imul(lt,vt),m=m+Math.imul(st,Pt)|0,c=c+Math.imul(st,Ct)|0,c=c+Math.imul(at,Pt)|0,b=b+Math.imul(at,Ct)|0,m=m+Math.imul(ot,Mt)|0,c=c+Math.imul(ot,kt)|0,c=c+Math.imul(rt,Mt)|0,b=b+Math.imul(rt,kt)|0,m=m+Math.imul(it,At)|0,c=c+Math.imul(it,Tt)|0,c=c+Math.imul(nt,At)|0,b=b+Math.imul(nt,Tt)|0,m=m+Math.imul(tt,xt)|0,c=c+Math.imul(tt,St)|0,c=c+Math.imul(et,xt)|0,b=b+Math.imul(et,St)|0;var $i=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+($i>>>26)|0,$i&=67108863,m=Math.imul(ct,Pt),c=Math.imul(ct,Ct),c=c+Math.imul(lt,Pt)|0,b=Math.imul(lt,Ct),m=m+Math.imul(st,Mt)|0,c=c+Math.imul(st,kt)|0,c=c+Math.imul(at,Mt)|0,b=b+Math.imul(at,kt)|0,m=m+Math.imul(ot,At)|0,c=c+Math.imul(ot,Tt)|0,c=c+Math.imul(rt,At)|0,b=b+Math.imul(rt,Tt)|0,m=m+Math.imul(it,xt)|0,c=c+Math.imul(it,St)|0,c=c+Math.imul(nt,xt)|0,b=b+Math.imul(nt,St)|0;var Ui=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ui>>>26)|0,Ui&=67108863,m=Math.imul(ct,Mt),c=Math.imul(ct,kt),c=c+Math.imul(lt,Mt)|0,b=Math.imul(lt,kt),m=m+Math.imul(st,At)|0,c=c+Math.imul(st,Tt)|0,c=c+Math.imul(at,At)|0,b=b+Math.imul(at,Tt)|0,m=m+Math.imul(ot,xt)|0,c=c+Math.imul(ot,St)|0,c=c+Math.imul(rt,xt)|0,b=b+Math.imul(rt,St)|0;var Ki=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ki>>>26)|0,Ki&=67108863,m=Math.imul(ct,At),c=Math.imul(ct,Tt),c=c+Math.imul(lt,At)|0,b=Math.imul(lt,Tt),m=m+Math.imul(st,xt)|0,c=c+Math.imul(st,St)|0,c=c+Math.imul(at,xt)|0,b=b+Math.imul(at,St)|0;var Wi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Wi>>>26)|0,Wi&=67108863,m=Math.imul(ct,xt),c=Math.imul(ct,St),c=c+Math.imul(lt,xt)|0,b=Math.imul(lt,St);var Hi=(C+m|0)+((c&8191)<<13)|0;return C=(b+(c>>>13)|0)+(Hi>>>26)|0,Hi&=67108863,v[0]=Ti,v[1]=xi,v[2]=Si,v[3]=Ii,v[4]=Ri,v[5]=Oi,v[6]=Fi,v[7]=Bi,v[8]=Ei,v[9]=Ni,v[10]=ji,v[11]=qi,v[12]=Di,v[13]=Li,v[14]=$i,v[15]=Ui,v[16]=Ki,v[17]=Wi,v[18]=Hi,C!==0&&(v[19]=C,_.length++),_};Math.imul||(j=$);function K(y,s,l){l.negative=s.negative^y.negative,l.length=y.length+s.length;for(var _=0,p=0,g=0;g<l.length-1;g++){var v=p;p=0;for(var C=_&67108863,m=Math.min(g,s.length-1),c=Math.max(0,g-y.length+1);c<=m;c++){var b=g-c,A=y.words[b]|0,S=s.words[c]|0,R=A*S,q=R&67108863;v=v+(R/67108864|0)|0,q=q+C|0,C=q&67108863,v=v+(q>>>26)|0,p+=v>>>26,v&=67108863}l.words[g]=C,_=v,v=p}return _!==0?l.words[g]=_:l.length--,l._strip()}function B(y,s,l){return K(y,s,l)}n.prototype.mulTo=function(s,l){var _,p=this.length+s.length;return this.length===10&&s.length===10?_=j(this,s,l):p<63?_=$(this,s,l):p<1024?_=K(this,s,l):_=B(this,s,l),_};function D(y,s){this.x=y,this.y=s}D.prototype.makeRBT=function(s){for(var l=new Array(s),_=n.prototype._countBits(s)-1,p=0;p<s;p++)l[p]=this.revBin(p,_,s);return l},D.prototype.revBin=function(s,l,_){if(s===0||s===_-1)return s;for(var p=0,g=0;g<l;g++)p|=(s&1)<<l-g-1,s>>=1;return p},D.prototype.permute=function(s,l,_,p,g,v){for(var C=0;C<v;C++)p[C]=l[s[C]],g[C]=_[s[C]]},D.prototype.transform=function(s,l,_,p,g,v){this.permute(v,s,l,_,p,g);for(var C=1;C<g;C<<=1)for(var m=C<<1,c=Math.cos(2*Math.PI/m),b=Math.sin(2*Math.PI/m),A=0;A<g;A+=m)for(var S=c,R=b,q=0;q<C;q++){var U=_[A+q],W=p[A+q],zt=_[A+q+C],H=p[A+q+C],G=S*zt-R*H;H=S*H+R*zt,zt=G,_[A+q]=U+zt,p[A+q]=W+H,_[A+q+C]=U-zt,p[A+q+C]=W-H,q!==m&&(G=c*S-b*R,R=c*R+b*S,S=G)}},D.prototype.guessLen13b=function(s,l){var _=Math.max(l,s)|1,p=_&1,g=0;for(_=_/2|0;_;_=_>>>1)g++;return 1<<g+1+p},D.prototype.conjugate=function(s,l,_){if(!(_<=1))for(var p=0;p<_/2;p++){var g=s[p];s[p]=s[_-p-1],s[_-p-1]=g,g=l[p],l[p]=-l[_-p-1],l[_-p-1]=-g}},D.prototype.normalize13b=function(s,l){for(var _=0,p=0;p<l/2;p++){var g=Math.round(s[2*p+1]/l)*8192+Math.round(s[2*p]/l)+_;s[p]=g&67108863,g<67108864?_=0:_=g/67108864|0}return s},D.prototype.convert13b=function(s,l,_,p){for(var g=0,v=0;v<l;v++)g=g+(s[v]|0),_[2*v]=g&8191,g=g>>>13,_[2*v+1]=g&8191,g=g>>>13;for(v=2*l;v<p;++v)_[v]=0;e(g===0),e((g&-8192)===0)},D.prototype.stub=function(s){for(var l=new Array(s),_=0;_<s;_++)l[_]=0;return l},D.prototype.mulp=function(s,l,_){var p=2*this.guessLen13b(s.length,l.length),g=this.makeRBT(p),v=this.stub(p),C=new Array(p),m=new Array(p),c=new Array(p),b=new Array(p),A=new Array(p),S=new Array(p),R=_.words;R.length=p,this.convert13b(s.words,s.length,C,p),this.convert13b(l.words,l.length,b,p),this.transform(C,v,m,c,p,g),this.transform(b,v,A,S,p,g);for(var q=0;q<p;q++){var U=m[q]*A[q]-c[q]*S[q];c[q]=m[q]*S[q]+c[q]*A[q],m[q]=U}return this.conjugate(m,c,p),this.transform(m,c,R,v,p,g),this.conjugate(R,v,p),this.normalize13b(R,p),_.negative=s.negative^l.negative,_.length=s.length+l.length,_._strip()},n.prototype.mul=function(s){var l=new n(null);return l.words=new Array(this.length+s.length),this.mulTo(s,l)},n.prototype.mulf=function(s){var l=new n(null);return l.words=new Array(this.length+s.length),B(this,s,l)},n.prototype.imul=function(s){return this.clone().mulTo(s,this)},n.prototype.imuln=function(s){var l=s<0;l&&(s=-s),e(typeof s=="number"),e(s<67108864);for(var _=0,p=0;p<this.length;p++){var g=(this.words[p]|0)*s,v=(g&67108863)+(_&67108863);_>>=26,_+=g/67108864|0,_+=v>>>26,this.words[p]=v&67108863}return _!==0&&(this.words[p]=_,this.length++),this.length=s===0?1:this.length,l?this.ineg():this},n.prototype.muln=function(s){return this.clone().imuln(s)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(s){var l=x(s);if(l.length===0)return new n(1);for(var _=this,p=0;p<l.length&&l[p]===0;p++,_=_.sqr());if(++p<l.length)for(var g=_.sqr();p<l.length;p++,g=g.sqr())l[p]!==0&&(_=_.mul(g));return _},n.prototype.iushln=function(s){e(typeof s=="number"&&s>=0);var l=s%26,_=(s-l)/26,p=67108863>>>26-l<<26-l,g;if(l!==0){var v=0;for(g=0;g<this.length;g++){var C=this.words[g]&p,m=(this.words[g]|0)-C<<l;this.words[g]=m|v,v=C>>>26-l}v&&(this.words[g]=v,this.length++)}if(_!==0){for(g=this.length-1;g>=0;g--)this.words[g+_]=this.words[g];for(g=0;g<_;g++)this.words[g]=0;this.length+=_}return this._strip()},n.prototype.ishln=function(s){return e(this.negative===0),this.iushln(s)},n.prototype.iushrn=function(s,l,_){e(typeof s=="number"&&s>=0);var p;l?p=(l-l%26)/26:p=0;var g=s%26,v=Math.min((s-g)/26,this.length),C=67108863^67108863>>>g<<g,m=_;if(p-=v,p=Math.max(0,p),m){for(var c=0;c<v;c++)m.words[c]=this.words[c];m.length=v}if(v!==0)if(this.length>v)for(this.length-=v,c=0;c<this.length;c++)this.words[c]=this.words[c+v];else this.words[0]=0,this.length=1;var b=0;for(c=this.length-1;c>=0&&(b!==0||c>=p);c--){var A=this.words[c]|0;this.words[c]=b<<26-g|A>>>g,b=A&C}return m&&b!==0&&(m.words[m.length++]=b),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},n.prototype.ishrn=function(s,l,_){return e(this.negative===0),this.iushrn(s,l,_)},n.prototype.shln=function(s){return this.clone().ishln(s)},n.prototype.ushln=function(s){return this.clone().iushln(s)},n.prototype.shrn=function(s){return this.clone().ishrn(s)},n.prototype.ushrn=function(s){return this.clone().iushrn(s)},n.prototype.testn=function(s){e(typeof s=="number"&&s>=0);var l=s%26,_=(s-l)/26,p=1<<l;if(this.length<=_)return!1;var g=this.words[_];return!!(g&p)},n.prototype.imaskn=function(s){e(typeof s=="number"&&s>=0);var l=s%26,_=(s-l)/26;if(e(this.negative===0,"imaskn works only with positive numbers"),this.length<=_)return this;if(l!==0&&_++,this.length=Math.min(_,this.length),l!==0){var p=67108863^67108863>>>l<<l;this.words[this.length-1]&=p}return this._strip()},n.prototype.maskn=function(s){return this.clone().imaskn(s)},n.prototype.iaddn=function(s){return e(typeof s=="number"),e(s<67108864),s<0?this.isubn(-s):this.negative!==0?this.length===1&&(this.words[0]|0)<=s?(this.words[0]=s-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(s),this.negative=1,this):this._iaddn(s)},n.prototype._iaddn=function(s){this.words[0]+=s;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},n.prototype.isubn=function(s){if(e(typeof s=="number"),e(s<67108864),s<0)return this.iaddn(-s);if(this.negative!==0)return this.negative=0,this.iaddn(s),this.negative=1,this;if(this.words[0]-=s,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()},n.prototype.addn=function(s){return this.clone().iaddn(s)},n.prototype.subn=function(s){return this.clone().isubn(s)},n.prototype.iabs=function(){return this.negative=0,this},n.prototype.abs=function(){return this.clone().iabs()},n.prototype._ishlnsubmul=function(s,l,_){var p=s.length+_,g;this._expand(p);var v,C=0;for(g=0;g<s.length;g++){v=(this.words[g+_]|0)+C;var m=(s.words[g]|0)*l;v-=m&67108863,C=(v>>26)-(m/67108864|0),this.words[g+_]=v&67108863}for(;g<this.length-_;g++)v=(this.words[g+_]|0)+C,C=v>>26,this.words[g+_]=v&67108863;if(C===0)return this._strip();for(e(C===-1),C=0,g=0;g<this.length;g++)v=-(this.words[g]|0)+C,C=v>>26,this.words[g]=v&67108863;return this.negative=1,this._strip()},n.prototype._wordDiv=function(s,l){var _=this.length-s.length,p=this.clone(),g=s,v=g.words[g.length-1]|0,C=this._countBits(v);_=26-C,_!==0&&(g=g.ushln(_),p.iushln(_),v=g.words[g.length-1]|0);var m=p.length-g.length,c;if(l!=="mod"){c=new n(null),c.length=m+1,c.words=new Array(c.length);for(var b=0;b<c.length;b++)c.words[b]=0}var A=p.clone()._ishlnsubmul(g,1,m);A.negative===0&&(p=A,c&&(c.words[m]=1));for(var S=m-1;S>=0;S--){var R=(p.words[g.length+S]|0)*67108864+(p.words[g.length+S-1]|0);for(R=Math.min(R/v|0,67108863),p._ishlnsubmul(g,R,S);p.negative!==0;)R--,p.negative=0,p._ishlnsubmul(g,1,S),p.isZero()||(p.negative^=1);c&&(c.words[S]=R)}return c&&c._strip(),p._strip(),l!=="div"&&_!==0&&p.iushrn(_),{div:c||null,mod:p}},n.prototype.divmod=function(s,l,_){if(e(!s.isZero()),this.isZero())return{div:new n(0),mod:new n(0)};var p,g,v;return this.negative!==0&&s.negative===0?(v=this.neg().divmod(s,l),l!=="mod"&&(p=v.div.neg()),l!=="div"&&(g=v.mod.neg(),_&&g.negative!==0&&g.iadd(s)),{div:p,mod:g}):this.negative===0&&s.negative!==0?(v=this.divmod(s.neg(),l),l!=="mod"&&(p=v.div.neg()),{div:p,mod:v.mod}):(this.negative&s.negative)!==0?(v=this.neg().divmod(s.neg(),l),l!=="div"&&(g=v.mod.neg(),_&&g.negative!==0&&g.isub(s)),{div:v.div,mod:g}):s.length>this.length||this.cmp(s)<0?{div:new n(0),mod:this}:s.length===1?l==="div"?{div:this.divn(s.words[0]),mod:null}:l==="mod"?{div:null,mod:new n(this.modrn(s.words[0]))}:{div:this.divn(s.words[0]),mod:new n(this.modrn(s.words[0]))}:this._wordDiv(s,l)},n.prototype.div=function(s){return this.divmod(s,"div",!1).div},n.prototype.mod=function(s){return this.divmod(s,"mod",!1).mod},n.prototype.umod=function(s){return this.divmod(s,"mod",!0).mod},n.prototype.divRound=function(s){var l=this.divmod(s);if(l.mod.isZero())return l.div;var _=l.div.negative!==0?l.mod.isub(s):l.mod,p=s.ushrn(1),g=s.andln(1),v=_.cmp(p);return v<0||g===1&&v===0?l.div:l.div.negative!==0?l.div.isubn(1):l.div.iaddn(1)},n.prototype.modrn=function(s){var l=s<0;l&&(s=-s),e(s<=67108863);for(var _=(1<<26)%s,p=0,g=this.length-1;g>=0;g--)p=(_*p+(this.words[g]|0))%s;return l?-p:p},n.prototype.modn=function(s){return this.modrn(s)},n.prototype.idivn=function(s){var l=s<0;l&&(s=-s),e(s<=67108863);for(var _=0,p=this.length-1;p>=0;p--){var g=(this.words[p]|0)+_*67108864;this.words[p]=g/s|0,_=g%s}return this._strip(),l?this.ineg():this},n.prototype.divn=function(s){return this.clone().idivn(s)},n.prototype.egcd=function(s){e(s.negative===0),e(!s.isZero());var l=this,_=s.clone();l.negative!==0?l=l.umod(s):l=l.clone();for(var p=new n(1),g=new n(0),v=new n(0),C=new n(1),m=0;l.isEven()&&_.isEven();)l.iushrn(1),_.iushrn(1),++m;for(var c=_.clone(),b=l.clone();!l.isZero();){for(var A=0,S=1;(l.words[0]&S)===0&&A<26;++A,S<<=1);if(A>0)for(l.iushrn(A);A-- >0;)(p.isOdd()||g.isOdd())&&(p.iadd(c),g.isub(b)),p.iushrn(1),g.iushrn(1);for(var R=0,q=1;(_.words[0]&q)===0&&R<26;++R,q<<=1);if(R>0)for(_.iushrn(R);R-- >0;)(v.isOdd()||C.isOdd())&&(v.iadd(c),C.isub(b)),v.iushrn(1),C.iushrn(1);l.cmp(_)>=0?(l.isub(_),p.isub(v),g.isub(C)):(_.isub(l),v.isub(p),C.isub(g))}return{a:v,b:C,gcd:_.iushln(m)}},n.prototype._invmp=function(s){e(s.negative===0),e(!s.isZero());var l=this,_=s.clone();l.negative!==0?l=l.umod(s):l=l.clone();for(var p=new n(1),g=new n(0),v=_.clone();l.cmpn(1)>0&&_.cmpn(1)>0;){for(var C=0,m=1;(l.words[0]&m)===0&&C<26;++C,m<<=1);if(C>0)for(l.iushrn(C);C-- >0;)p.isOdd()&&p.iadd(v),p.iushrn(1);for(var c=0,b=1;(_.words[0]&b)===0&&c<26;++c,b<<=1);if(c>0)for(_.iushrn(c);c-- >0;)g.isOdd()&&g.iadd(v),g.iushrn(1);l.cmp(_)>=0?(l.isub(_),p.isub(g)):(_.isub(l),g.isub(p))}var A;return l.cmpn(1)===0?A=p:A=g,A.cmpn(0)<0&&A.iadd(s),A},n.prototype.gcd=function(s){if(this.isZero())return s.abs();if(s.isZero())return this.abs();var l=this.clone(),_=s.clone();l.negative=0,_.negative=0;for(var p=0;l.isEven()&&_.isEven();p++)l.iushrn(1),_.iushrn(1);do{for(;l.isEven();)l.iushrn(1);for(;_.isEven();)_.iushrn(1);var g=l.cmp(_);if(g<0){var v=l;l=_,_=v}else if(g===0||_.cmpn(1)===0)break;l.isub(_)}while(!0);return _.iushln(p)},n.prototype.invm=function(s){return this.egcd(s).a.umod(s)},n.prototype.isEven=function(){return(this.words[0]&1)===0},n.prototype.isOdd=function(){return(this.words[0]&1)===1},n.prototype.andln=function(s){return this.words[0]&s},n.prototype.bincn=function(s){e(typeof s=="number");var l=s%26,_=(s-l)/26,p=1<<l;if(this.length<=_)return this._expand(_+1),this.words[_]|=p,this;for(var g=p,v=_;g!==0&&v<this.length;v++){var C=this.words[v]|0;C+=g,g=C>>>26,C&=67108863,this.words[v]=C}return g!==0&&(this.words[v]=g,this.length++),this},n.prototype.isZero=function(){return this.length===1&&this.words[0]===0},n.prototype.cmpn=function(s){var l=s<0;if(this.negative!==0&&!l)return-1;if(this.negative===0&&l)return 1;this._strip();var _;if(this.length>1)_=1;else{l&&(s=-s),e(s<=67108863,"Number is too big");var p=this.words[0]|0;_=p===s?0:p<s?-1:1}return this.negative!==0?-_|0:_},n.prototype.cmp=function(s){if(this.negative!==0&&s.negative===0)return-1;if(this.negative===0&&s.negative!==0)return 1;var l=this.ucmp(s);return this.negative!==0?-l|0:l},n.prototype.ucmp=function(s){if(this.length>s.length)return 1;if(this.length<s.length)return-1;for(var l=0,_=this.length-1;_>=0;_--){var p=this.words[_]|0,g=s.words[_]|0;if(p!==g){p<g?l=-1:p>g&&(l=1);break}}return l},n.prototype.gtn=function(s){return this.cmpn(s)===1},n.prototype.gt=function(s){return this.cmp(s)===1},n.prototype.gten=function(s){return this.cmpn(s)>=0},n.prototype.gte=function(s){return this.cmp(s)>=0},n.prototype.ltn=function(s){return this.cmpn(s)===-1},n.prototype.lt=function(s){return this.cmp(s)===-1},n.prototype.lten=function(s){return this.cmpn(s)<=0},n.prototype.lte=function(s){return this.cmp(s)<=0},n.prototype.eqn=function(s){return this.cmpn(s)===0},n.prototype.eq=function(s){return this.cmp(s)===0},n.red=function(s){return new L(s)},n.prototype.toRed=function(s){return e(!this.red,"Already a number in reduction context"),e(this.negative===0,"red works only with positives"),s.convertTo(this)._forceRed(s)},n.prototype.fromRed=function(){return e(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},n.prototype._forceRed=function(s){return this.red=s,this},n.prototype.forceRed=function(s){return e(!this.red,"Already a number in reduction context"),this._forceRed(s)},n.prototype.redAdd=function(s){return e(this.red,"redAdd works only with red numbers"),this.red.add(this,s)},n.prototype.redIAdd=function(s){return e(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,s)},n.prototype.redSub=function(s){return e(this.red,"redSub works only with red numbers"),this.red.sub(this,s)},n.prototype.redISub=function(s){return e(this.red,"redISub works only with red numbers"),this.red.isub(this,s)},n.prototype.redShl=function(s){return e(this.red,"redShl works only with red numbers"),this.red.shl(this,s)},n.prototype.redMul=function(s){return e(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.mul(this,s)},n.prototype.redIMul=function(s){return e(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.imul(this,s)},n.prototype.redSqr=function(){return e(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return e(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return e(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return e(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return e(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(s){return e(this.red&&!s.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,s)};var jt={k256:null,p224:null,p192:null,p25519:null};function z(y,s){this.name=y,this.p=new n(s,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}z.prototype._tmp=function(){var s=new n(null);return s.words=new Array(Math.ceil(this.n/13)),s},z.prototype.ireduce=function(s){var l=s,_;do this.split(l,this.tmp),l=this.imulK(l),l=l.iadd(this.tmp),_=l.bitLength();while(_>this.n);var p=_<this.n?-1:l.ucmp(this.p);return p===0?(l.words[0]=0,l.length=1):p>0?l.isub(this.p):l.strip!==void 0?l.strip():l._strip(),l},z.prototype.split=function(s,l){s.iushrn(this.n,0,l)},z.prototype.imulK=function(s){return s.imul(this.k)};function Lt(){z.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}i(Lt,z),Lt.prototype.split=function(s,l){for(var _=4194303,p=Math.min(s.length,9),g=0;g<p;g++)l.words[g]=s.words[g];if(l.length=p,s.length<=9){s.words[0]=0,s.length=1;return}var v=s.words[9];for(l.words[l.length++]=v&_,g=10;g<s.length;g++){var C=s.words[g]|0;s.words[g-10]=(C&_)<<4|v>>>22,v=C}v>>>=22,s.words[g-10]=v,v===0&&s.length>10?s.length-=10:s.length-=9},Lt.prototype.imulK=function(s){s.words[s.length]=0,s.words[s.length+1]=0,s.length+=2;for(var l=0,_=0;_<s.length;_++){var p=s.words[_]|0;l+=p*977,s.words[_]=l&67108863,l=p*64+(l/67108864|0)}return s.words[s.length-1]===0&&(s.length--,s.words[s.length-1]===0&&s.length--),s};function ee(){z.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}i(ee,z);function Zt(){z.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}i(Zt,z);function ie(){z.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}i(ie,z),ie.prototype.imulK=function(s){for(var l=0,_=0;_<s.length;_++){var p=(s.words[_]|0)*19+l,g=p&67108863;p>>>=26,s.words[_]=g,l=p}return l!==0&&(s.words[s.length++]=l),s},n._prime=function(s){if(jt[s])return jt[s];var l;if(s==="k256")l=new Lt;else if(s==="p224")l=new ee;else if(s==="p192")l=new Zt;else if(s==="p25519")l=new ie;else throw new Error("Unknown prime "+s);return jt[s]=l,l};function L(y){if(typeof y=="string"){var s=n._prime(y);this.m=s.p,this.prime=s}else e(y.gtn(1),"modulus must be greater than 1"),this.m=y,this.prime=null}L.prototype._verify1=function(s){e(s.negative===0,"red works only with positives"),e(s.red,"red works only with red numbers")},L.prototype._verify2=function(s,l){e((s.negative|l.negative)===0,"red works only with positives"),e(s.red&&s.red===l.red,"red works only with red numbers")},L.prototype.imod=function(s){return this.prime?this.prime.ireduce(s)._forceRed(this):(f(s,s.umod(this.m)._forceRed(this)),s)},L.prototype.neg=function(s){return s.isZero()?s.clone():this.m.sub(s)._forceRed(this)},L.prototype.add=function(s,l){this._verify2(s,l);var _=s.add(l);return _.cmp(this.m)>=0&&_.isub(this.m),_._forceRed(this)},L.prototype.iadd=function(s,l){this._verify2(s,l);var _=s.iadd(l);return _.cmp(this.m)>=0&&_.isub(this.m),_},L.prototype.sub=function(s,l){this._verify2(s,l);var _=s.sub(l);return _.cmpn(0)<0&&_.iadd(this.m),_._forceRed(this)},L.prototype.isub=function(s,l){this._verify2(s,l);var _=s.isub(l);return _.cmpn(0)<0&&_.iadd(this.m),_},L.prototype.shl=function(s,l){return this._verify1(s),this.imod(s.ushln(l))},L.prototype.imul=function(s,l){return this._verify2(s,l),this.imod(s.imul(l))},L.prototype.mul=function(s,l){return this._verify2(s,l),this.imod(s.mul(l))},L.prototype.isqr=function(s){return this.imul(s,s.clone())},L.prototype.sqr=function(s){return this.mul(s,s)},L.prototype.sqrt=function(s){if(s.isZero())return s.clone();var l=this.m.andln(3);if(e(l%2===1),l===3){var _=this.m.add(new n(1)).iushrn(2);return this.pow(s,_)}for(var p=this.m.subn(1),g=0;!p.isZero()&&p.andln(1)===0;)g++,p.iushrn(1);e(!p.isZero());var v=new n(1).toRed(this),C=v.redNeg(),m=this.m.subn(1).iushrn(1),c=this.m.bitLength();for(c=new n(2*c*c).toRed(this);this.pow(c,m).cmp(C)!==0;)c.redIAdd(C);for(var b=this.pow(c,p),A=this.pow(s,p.addn(1).iushrn(1)),S=this.pow(s,p),R=g;S.cmp(v)!==0;){for(var q=S,U=0;q.cmp(v)!==0;U++)q=q.redSqr();e(U<R);var W=this.pow(b,new n(1).iushln(R-U-1));A=A.redMul(W),b=W.redSqr(),S=S.redMul(b),R=U}return A},L.prototype.invm=function(s){var l=s._invmp(this.m);return l.negative!==0?(l.negative=0,this.imod(l).redNeg()):this.imod(l)},L.prototype.pow=function(s,l){if(l.isZero())return new n(1).toRed(this);if(l.cmpn(1)===0)return s.clone();var _=4,p=new Array(1<<_);p[0]=new n(1).toRed(this),p[1]=s;for(var g=2;g<p.length;g++)p[g]=this.mul(p[g-1],s);var v=p[0],C=0,m=0,c=l.bitLength()%26;for(c===0&&(c=26),g=l.length-1;g>=0;g--){for(var b=l.words[g],A=c-1;A>=0;A--){var S=b>>A&1;if(v!==p[0]&&(v=this.sqr(v)),S===0&&C===0){m=0;continue}C<<=1,C|=S,m++,!(m!==_&&(g!==0||A!==0))&&(v=this.mul(v,p[C]),m=0,C=0)}c=26}return v},L.prototype.convertTo=function(s){var l=s.umod(this.m);return l===s?l.clone():l},L.prototype.convertFrom=function(s){var l=s.clone();return l.red=null,l},n.mont=function(s){return new Et(s)};function Et(y){L.call(this,y),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new n(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)}i(Et,L),Et.prototype.convertTo=function(s){return this.imod(s.ushln(this.shift))},Et.prototype.convertFrom=function(s){var l=this.imod(s.mul(this.rinv));return l.red=null,l},Et.prototype.imul=function(s,l){if(s.isZero()||l.isZero())return s.words[0]=0,s.length=1,s;var _=s.imul(l),p=_.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=_.isub(p).iushrn(this.shift),v=g;return g.cmp(this.m)>=0?v=g.isub(this.m):g.cmpn(0)<0&&(v=g.iadd(this.m)),v._forceRed(this)},Et.prototype.mul=function(s,l){if(s.isZero()||l.isZero())return new n(0)._forceRed(this);var _=s.mul(l),p=_.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=_.isub(p).iushrn(this.shift),v=g;return g.cmp(this.m)>=0?v=g.isub(this.m):g.cmpn(0)<0&&(v=g.iadd(this.m)),v._forceRed(this)},Et.prototype.invm=function(s){var l=this.imod(s._invmp(this.m).mul(this.r2));return l._forceRed(this)}})(typeof tn>"u"||tn,ro)});import{SdkWrapper as ea}from"@cetusprotocol/common-sdk";import{normalizeSuiObjectId as Ji}from"@mysten/sui/utils";import{CACHE_TIME_24H as Wt,CACHE_TIME_5MIN as Cr,CachedContent as Mr,DETAILS_KEYS as je,extractStructTagFromType as Xi,fixCoinType as kr,getFutureTime as no,getObjectFields as Qe,getObjectId as Yi,getObjectPreviousTransactionDigest as Ar,getObjectType as Tr,getPackagerConfigs as ae,normalizeCoinType as oo}from"@cetusprotocol/common-sdk";var $n="3.7.7",dr=$n,be=typeof Buffer=="function",Nn=typeof TextDecoder=="function"?new TextDecoder:void 0,jn=typeof TextEncoder=="function"?new TextEncoder:void 0,fr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",Ee=Array.prototype.slice.call(fr),Ze=(o=>{let t={};return o.forEach((e,i)=>t[e]=i),t})(Ee),_r=/^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/,Rt=String.fromCharCode.bind(String),qn=typeof Uint8Array.from=="function"?Uint8Array.from.bind(Uint8Array):o=>new Uint8Array(Array.prototype.slice.call(o,0)),Un=o=>o.replace(/=/g,"").replace(/[+\/]/g,t=>t=="+"?"-":"_"),Kn=o=>o.replace(/[^A-Za-z0-9\+\/]/g,""),Wn=o=>{let t,e,i,n,r="",a=o.length%3;for(let u=0;u<o.length;){if((e=o.charCodeAt(u++))>255||(i=o.charCodeAt(u++))>255||(n=o.charCodeAt(u++))>255)throw new TypeError("invalid character found");t=e<<16|i<<8|n,r+=Ee[t>>18&63]+Ee[t>>12&63]+Ee[t>>6&63]+Ee[t&63]}return a?r.slice(0,a-3)+"===".substring(a):r},Gi=typeof btoa=="function"?o=>btoa(o):be?o=>Buffer.from(o,"binary").toString("base64"):Wn,Zi=be?o=>Buffer.from(o).toString("base64"):o=>{let e=[];for(let i=0,n=o.length;i<n;i+=4096)e.push(Rt.apply(null,o.subarray(i,i+4096)));return Gi(e.join(""))},ze=(o,t=!1)=>t?Un(Zi(o)):Zi(o),hr=o=>{if(o.length<2){var t=o.charCodeAt(0);return t<128?o:t<2048?Rt(192|t>>>6)+Rt(128|t&63):Rt(224|t>>>12&15)+Rt(128|t>>>6&63)+Rt(128|t&63)}else{var t=65536+(o.charCodeAt(0)-55296)*1024+(o.charCodeAt(1)-56320);return Rt(240|t>>>18&7)+Rt(128|t>>>12&63)+Rt(128|t>>>6&63)+Rt(128|t&63)}},pr=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g,Hn=o=>o.replace(pr,hr),Dn=be?o=>Buffer.from(o,"utf8").toString("base64"):jn?o=>Zi(jn.encode(o)):o=>Gi(Hn(o)),ge=(o,t=!1)=>t?Un(Dn(o)):Dn(o),Ln=o=>ge(o,!0),mr=/[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g,gr=o=>{switch(o.length){case 4:var t=(7&o.charCodeAt(0))<<18|(63&o.charCodeAt(1))<<12|(63&o.charCodeAt(2))<<6|63&o.charCodeAt(3),e=t-65536;return Rt((e>>>10)+55296)+Rt((e&1023)+56320);case 3:return Rt((15&o.charCodeAt(0))<<12|(63&o.charCodeAt(1))<<6|63&o.charCodeAt(2));default:return Rt((31&o.charCodeAt(0))<<6|63&o.charCodeAt(1))}},Zn=o=>o.replace(mr,gr),zn=o=>{if(o=o.replace(/\s+/g,""),!_r.test(o))throw new TypeError("malformed base64.");o+="==".slice(2-(o.length&3));let t,e="",i,n;for(let r=0;r<o.length;)t=Ze[o.charAt(r++)]<<18|Ze[o.charAt(r++)]<<12|(i=Ze[o.charAt(r++)])<<6|(n=Ze[o.charAt(r++)]),e+=i===64?Rt(t>>16&255):n===64?Rt(t>>16&255,t>>8&255):Rt(t>>16&255,t>>8&255,t&255);return e},Vi=typeof atob=="function"?o=>atob(Kn(o)):be?o=>Buffer.from(o,"base64").toString("binary"):zn,Gn=be?o=>qn(Buffer.from(o,"base64")):o=>qn(Vi(o).split("").map(t=>t.charCodeAt(0))),Vn=o=>Gn(Qn(o)),br=be?o=>Buffer.from(o,"base64").toString("utf8"):Nn?o=>Nn.decode(Gn(o)):o=>Zn(Vi(o)),Qn=o=>Kn(o.replace(/[-_]/g,t=>t=="-"?"+":"/")),zi=o=>br(Qn(o)),wr=o=>{if(typeof o!="string")return!1;let t=o.replace(/\s+/g,"").replace(/={0,2}$/,"");return!/[^\s0-9a-zA-Z\+/]/.test(t)||!/[^\s0-9a-zA-Z\-_]/.test(t)},Jn=o=>({value:o,enumerable:!1,writable:!0,configurable:!0}),Xn=function(){let o=(t,e)=>Object.defineProperty(String.prototype,t,Jn(e));o("fromBase64",function(){return zi(this)}),o("toBase64",function(t){return ge(this,t)}),o("toBase64URI",function(){return ge(this,!0)}),o("toBase64URL",function(){return ge(this,!0)}),o("toUint8Array",function(){return Vn(this)})},Yn=function(){let o=(t,e)=>Object.defineProperty(Uint8Array.prototype,t,Jn(e));o("toBase64",function(t){return ze(this,t)}),o("toBase64URI",function(){return ze(this,!0)}),o("toBase64URL",function(){return ze(this,!0)})},yr=()=>{Xn(),Yn()},Ge={version:$n,VERSION:dr,atob:Vi,atobPolyfill:zn,btoa:Gi,btoaPolyfill:Wn,fromBase64:zi,toBase64:ge,encode:ge,encodeURI:Ln,encodeURL:Ln,utob:Hn,btou:Zn,decode:zi,isValid:wr,fromUint8Array:ze,toUint8Array:Vn,extendString:Xn,extendUint8Array:Yn,extendBuiltins:yr};import{BaseError as vr}from"@cetusprotocol/common-sdk";var to=(f=>(f.InvalidSqrtPriceLimitDirection="InvalidSqrtPriceLimitDirection",f.ZeroTradableAmount="ZeroTradableAmount",f.AmountOutBelowMinimum="AmountOutBelowMinimum",f.AmountInAboveMaximum="AmountInAboveMaximum",f.NextTickNotFound="NextTickNotFound",f.TickArraySequenceInvalid="TickArraySequenceInvalid",f.TickArrayCrossingAboveMax="TickArrayCrossingAboveMax",f.TickArrayIndexNotInitialized="TickArrayIndexNotInitialized",f.ParamsLengthNotEqual="ParamsLengthNotEqual",f))(to||{}),Ve=(n=>(n.InvalidTickEvent="InvalidTickEvent",n.InvalidPositionObject="InvalidPositionObject",n.InvalidPositionRewardObject="InvalidPositionRewardObject",n.InvalidParams="InvalidParams",n))(Ve||{}),Ne=(f=>(f.InvalidCoinTypeSequence="InvalidCoinTypeSequence",f.InvalidTickIndex="InvalidTickIndex",f.InvalidPoolObject="InvalidPoolObject",f.InvalidTickObjectId="InvalidTickObjectId",f.InvalidTickObject="InvalidTickObject",f.InvalidTickFields="InvalidTickFields",f.PoolsNotFound="PoolsNotFound",f.StatsPoolsUrlNotSet="StatsPoolsUrlNotSet",f.FetchError="FetchError",f))(Ne||{}),eo=(e=>(e.NotFoundPartnerObject="NotFoundPartnerObject",e.InvalidPartnerRefFeeFields="InvalidPartnerRefFeeFields",e))(eo||{}),ye=(i=>(i.InvalidConfig="InvalidConfig",i.InvalidConfigHandle="InvalidConfigHandle",i.InvalidSimulateAccount="InvalidSimulateAccount",i))(ye||{}),Qi=(a=>(a.InvalidSendAddress="InvalidSendAddress",a.InvalidRecipientAddress="InvalidRecipientAddress",a.InvalidRecipientAndAmountLength="InvalidRecipientAndAmountLength",a.InsufficientBalance="InsufficientBalance",a.InvalidTarget="InvalidTarget",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a))(Qi||{}),Pr=(a=>(a.InvalidCoin="InvalidCoin",a.NotFoundPath="NotFoundPath",a.NoDowngradeNeedParams="NoDowngradeNeedParams",a.InvalidSwapCountUrl="InvalidSwapCountUrl",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a.InvalidServerResponse="InvalidServerResponse",a))(Pr||{}),io=(t=>(t.InvalidType="InvalidType",t))(io||{}),we=class extends vr{constructor(t,e,i){super(t,e||"UnknownError",i)}static isVaultsErrorCode(t,e){return this.isErrorCode(t,e)}},ve=(o,t,e)=>{throw new we(t.message,o,e)},E=(o,t,e)=>{throw new we(t,o,e)};var Je=class{constructor(t){this._cache={};this._sdk=t}get sdk(){return this._sdk}setTokenListCache(t){let{coin_list_handle:e}=ae(this.sdk.sdkOptions.cetus_config),i=`${e}_getCoinConfigs`,n=this.getCache(i),r=n?[...n,...t]:t;this.updateCache(i,r)}async getTokenListByCoinTypes(t){let e={},{coin_list_handle:i}=ae(this.sdk.sdkOptions.cetus_config),n=`${i}_getCoinConfigs`,r=this.getCache(n);if(r!==void 0){let u=r;for(let d of t)for(let f of u)if(oo(d)===oo(f.address)){e[d]=f;continue}}let a=t.filter(u=>e[u]===void 0);for(let u of a){let d=`${u}_metadata`,f=this.getCache(d);if(f!==void 0)e[u]=f;else{let h=await this._sdk.FullClient.getCoinMetadata({coinType:u});if(h){let w={id:h.id,pyth_id:"",name:h.name,symbol:h.symbol,official_symbol:h.symbol,coingecko_id:"",decimals:h.decimals,project_url:"",logo_url:h.iconUrl,address:u};e[u]=w,this.updateCache(d,w,Wt)}else console.log(`not found ${u}`)}}return e}async getCoinConfigs(t=!1,e=!0){let{coin_list_handle:i}=ae(this.sdk.sdkOptions.cetus_config),n=`${i}_getCoinConfigs`,r=this.getCache(n,t);if(r)return r;let u=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(u,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&E("FetchError",`when getCoinConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[je.METHOD_NAME]:"getCoinConfigs"});let w=this.buildCoinConfig(h,e);this.updateCache(`${i}_${w.address}_getCoinConfig`,w,Wt),f.push({...w})}),this.updateCache(n,f,Wt),f}async getCoinConfig(t,e=!1,i=!0){let{coin_list_handle:n}=ae(this.sdk.sdkOptions.cetus_config),r=`${n}_${t}_getCoinConfig`,a=this.getCache(r,e);if(a)return a;let u=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"0x1::type_name::TypeName",value:{name:kr(t)}}});(u.error!=null||u.data?.content?.dataType!=="moveObject")&&E("FetchError",`when getCoinConfig get object error: ${u.error}, please check the rpc and contracts address config.`,{[je.METHOD_NAME]:"getCoinConfig"});let d=this.buildCoinConfig(u,i);return this.updateCache(r,d,Wt),d}buildCoinConfig(t,e=!0){let i=Qe(t);i=i.value.fields;let n={...i};return n.id=Yi(t),n.address=Xi(i.coin_type.fields.name).full_address,i.pyth_id&&(n.pyth_id=Ji(i.pyth_id)),this.transformExtensions(n,i.extension_fields.fields.contents,e),delete n.coin_type,n}async getClmmPoolConfigs(t=!1,e=!0){let{clmm_pools_handle:i}=ae(this.sdk.sdkOptions.cetus_config),n=`${i}_getClmmPoolConfigs`,r=this.getCache(n,t);if(r)return r;let u=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(u,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&E("FetchError",`when getClmmPoolsConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[je.METHOD_NAME]:"getClmmPoolConfigs"});let w=this.buildClmmPoolConfig(h,e);this.updateCache(`${w.pool_address}_getClmmPoolConfig`,w,Wt),f.push({...w})}),this.updateCache(n,f,Wt),f}async getClmmPoolConfig(t,e=!1,i=!0){let{clmm_pools_handle:n}=ae(this.sdk.sdkOptions.cetus_config),r=`${t}_getClmmPoolConfig`,a=this.getCache(r,e);if(a)return a;let u=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"address",value:t}}),d=this.buildClmmPoolConfig(u,i);return this.updateCache(r,d,Wt),d}buildClmmPoolConfig(t,e=!0){let i=Qe(t);i=i.value.fields;let n={...i};return n.id=Yi(t),n.pool_address=Ji(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,e),n}async getLaunchpadPoolConfigs(t=!1,e=!0){let{launchpad_pools_handle:i}=ae(this.sdk.sdkOptions.cetus_config),n=`${i}_getLaunchpadPoolConfigs`,r=this.getCache(n,t);if(r)return r;let u=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(u,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&E("FetchError",`when getCoinConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[je.METHOD_NAME]:"getLaunchpadPoolConfigs"});let w=this.buildLaunchpadPoolConfig(h,e);this.updateCache(`${w.pool_address}_getLaunchpadPoolConfig`,w,Wt),f.push({...w})}),this.updateCache(n,f,Wt),f}async getLaunchpadPoolConfig(t,e=!1,i=!0){let{launchpad_pools_handle:n}=ae(this.sdk.sdkOptions.cetus_config),r=`${t}_getLaunchpadPoolConfig`,a=this.getCache(r,e);if(a)return a;let u=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"address",value:t}}),d=this.buildLaunchpadPoolConfig(u,i);return this.updateCache(r,d,Wt),d}buildLaunchpadPoolConfig(t,e=!0){let i=Qe(t);i=i.value.fields;let n={...i};n.id=Yi(t),n.pool_address=Ji(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,e);let r=[];i.social_media.fields.contents.forEach(a=>{r.push({name:a.fields.value.fields.name,link:a.fields.value.fields.link})}),n.social_media=r;try{n.regulation=decodeURIComponent(Ge.decode(n.regulation).replace(/%/g,"%25"))}catch{n.regulation=Ge.decode(n.regulation)}return n}transformExtensions(t,e,i=!0){let n=[];for(let r of e){let{key:a}=r.fields,{value:u}=r.fields;if(a==="labels")try{let d=decodeURIComponent(Ge.decode(u));try{u=JSON.parse(d)}catch{u=d}}catch{}i&&(t[a]=u),n.push({key:a,value:u})}delete t.extension_fields,i||(t.extensions=n)}async getCetusConfig(t=!1){let e=this._sdk.sdkOptions.cetus_config.package_id,i=`${e}_getCetusConfig`,n=this.getCache(i,t);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:e,options:{showPreviousTransaction:!0}}),a=Ar(r),u=await this._sdk.FullClient.queryEventsByPage({Transaction:a}),d={coin_list_id:"",launchpad_pools_id:"",clmm_pools_id:"",admin_cap_id:"",global_config_id:"",coin_list_handle:"",launchpad_pools_handle:"",clmm_pools_handle:""};if(u.data.length>0)for(let f of u.data)switch(Xi(f.type).name){case"InitCoinListEvent":d.coin_list_id=f.parsedJson.coin_list_id;break;case"InitLaunchpadPoolsEvent":d.launchpad_pools_id=f.parsedJson.launchpad_pools_id;break;case"InitClmmPoolsEvent":d.clmm_pools_id=f.parsedJson.pools_id;break;case"InitConfigEvent":d.global_config_id=f.parsedJson.global_config_id,d.admin_cap_id=f.parsedJson.admin_cap_id;break;default:break}return d=await this.getCetusConfigHandle(d),d.clmm_pools_id.length>0&&this.updateCache(i,d,Wt),d}async getCetusConfigHandle(t){let e=[t.clmm_pools_id,t.coin_list_id,t.launchpad_pools_id];return(await this._sdk.FullClient.multiGetObjects({ids:e,options:{showContent:!0}})).forEach(n=>{(n.error!=null||n.data?.content?.dataType!=="moveObject")&&E("InvalidConfigHandle",`when getCetusConfigHandle get objects error: ${n.error}, please check the rpc and contracts address config.`,{[je.METHOD_NAME]:"getCetusConfigHandle"});let r=Qe(n),a=Tr(n);switch(Xi(a).name){case"ClmmPools":t.clmm_pools_handle=r.pools.fields.id.id;break;case"CoinList":t.coin_list_handle=r.coins.fields.id.id;break;case"LaunchpadPools":t.launchpad_pools_handle=r.pools.fields.id.id;break;default:break}}),t}updateCache(t,e,i=Cr){let n=this._cache[t];n?(n.overdue_time=no(i),n.value=e):n=new Mr(e,no(i)),this._cache[t]=n}getCache(t,e=!1){try{let i=this._cache[t];if(!i)return;if(e||!i.isValid()){delete this._cache[t];return}return i.value}catch(i){console.error(`Error accessing cache for key ${t}:`,i);return}}};var Ho=ne(Gt());import{SuiClient as qs}from"@mysten/sui/client";import{Transaction as Ue}from"@mysten/sui/transactions";import{normalizeSuiAddress as Ae}from"@mysten/sui/utils";var so="partner",qe="pool_script",Vt="pool_script_v2",Xe="pool_script_v3",Ye="router",ao="router_with_partner",Qt="fetcher_script",ba="expect_swap",co="utils",wa="custodian_v2",ya="clob_v2",va="endpoints_v2",Pa=o=>{if(typeof o=="string"&&o.startsWith("0x"))return"object";if(typeof o=="number"||typeof o=="bigint")return"u64";if(typeof o=="boolean")return"bool";throw new we(`Unknown type for value: ${o}`,"InvalidType")};var Kt=ne(Gt());import{asIntN as he,buildNFT as Os,d as $e,DETAILS_KEYS as ui,extractStructTagFromType as ke,getMoveObjectType as jo,getObjectDeletedResponse as Fs,getObjectFields as dn,getObjectId as un,getObjectNotExistsResponse as Bs,getObjectOwner as Es,MathUtil as Ns}from"@cetusprotocol/common-sdk";var lo=(i=>(i.Deleted="Deleted",i.Exists="Exists",i.NotExists="NotExists",i))(lo||{}),uo=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent"];var Bt=ne(Gt());import{asUintN as Oo,FEE_RATE_DENOMINATOR as li,getDeltaDownFromOutput as Fo,getDeltaUpFromInput as Bo,getNextSqrtPriceFromInput as Ss,getNextSqrtPriceFromOutput as Is,MathUtil as _e,ZERO as Ut}from"@cetusprotocol/common-sdk";var Ro=ne(Gt());import{Transaction as cn}from"@mysten/sui/transactions";import{adjustForSlippage as Ms,CLOCK_ADDRESS as ci,CoinAssist as Dt,getPackagerConfigs as Io,MathUtil as ln,MAX_SQRT_PRICE as ks,MIN_SQRT_PRICE as As,U64_MAX as Ts,ZERO as xs}from"@cetusprotocol/common-sdk";var So=ne(Gt());import{Transaction as Ao}from"@mysten/sui/transactions";import{asUintN as To,ClmmPoolUtil as vs,CLOCK_ADDRESS as xo,CoinAssist as qt,getPackagerConfigs as Ps,normalizeCoinType as an}from"@cetusprotocol/common-sdk";var Pe=9e15,de=1e9,en="0123456789abcdef",ii="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",ni="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",nn={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Pe,maxE:Pe,crypto:!1},po,oe,N=!0,ri="[DecimalError] ",ue=ri+"Invalid argument: ",mo=ri+"Precision limit exceeded",go=ri+"crypto unavailable",bo="[object Decimal]",Ft=Math.floor,It=Math.pow,xr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Sr=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Ir=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,wo=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,Ht=1e7,F=7,Rr=9007199254740991,Or=ii.length-1,on=ni.length-1,k={toStringTag:bo};k.absoluteValue=k.abs=function(){var o=new this.constructor(this);return o.s<0&&(o.s=1),O(o)};k.ceil=function(){return O(new this.constructor(this),this.e+1,2)};k.clampedTo=k.clamp=function(o,t){var e,i=this,n=i.constructor;if(o=new n(o),t=new n(t),!o.s||!t.s)return new n(NaN);if(o.gt(t))throw Error(ue+t);return e=i.cmp(o),e<0?o:i.cmp(t)>0?t:new n(i)};k.comparedTo=k.cmp=function(o){var t,e,i,n,r=this,a=r.d,u=(o=new r.constructor(o)).d,d=r.s,f=o.s;if(!a||!u)return!d||!f?NaN:d!==f?d:a===u?0:!a^d<0?1:-1;if(!a[0]||!u[0])return a[0]?d:u[0]?-f:0;if(d!==f)return d;if(r.e!==o.e)return r.e>o.e^d<0?1:-1;for(i=a.length,n=u.length,t=0,e=i<n?i:n;t<e;++t)if(a[t]!==u[t])return a[t]>u[t]^d<0?1:-1;return i===n?0:i>n^d<0?1:-1};k.cosine=k.cos=function(){var o,t,e=this,i=e.constructor;return e.d?e.d[0]?(o=i.precision,t=i.rounding,i.precision=o+Math.max(e.e,e.sd())+F,i.rounding=1,e=Fr(i,Mo(i,e)),i.precision=o,i.rounding=t,O(oe==2||oe==3?e.neg():e,o,t,!0)):new i(1):new i(NaN)};k.cubeRoot=k.cbrt=function(){var o,t,e,i,n,r,a,u,d,f,h=this,w=h.constructor;if(!h.isFinite()||h.isZero())return new w(h);for(N=!1,r=h.s*It(h.s*h,1/3),!r||Math.abs(r)==1/0?(e=Ot(h.d),o=h.e,(r=(o-e.length+1)%3)&&(e+=r==1||r==-2?"0":"00"),r=It(e,1/3),o=Ft((o+1)/3)-(o%3==(o<0?-1:2)),r==1/0?e="5e"+o:(e=r.toExponential(),e=e.slice(0,e.indexOf("e")+1)+o),i=new w(e),i.s=h.s):i=new w(r.toString()),a=(o=w.precision)+3;;)if(u=i,d=u.times(u).times(u),f=d.plus(h),i=Z(f.plus(h).times(u),f.plus(d),a+2,1),Ot(u.d).slice(0,a)===(e=Ot(i.d)).slice(0,a))if(e=e.slice(a-3,a+1),e=="9999"||!n&&e=="4999"){if(!n&&(O(u,o+1,0),u.times(u).times(u).eq(h))){i=u;break}a+=4,n=1}else{(!+e||!+e.slice(1)&&e.charAt(0)=="5")&&(O(i,o+1,1),t=!i.times(i).times(i).eq(h));break}return N=!0,O(i,o,w.rounding,t)};k.decimalPlaces=k.dp=function(){var o,t=this.d,e=NaN;if(t){if(o=t.length-1,e=(o-Ft(this.e/F))*F,o=t[o],o)for(;o%10==0;o/=10)e--;e<0&&(e=0)}return e};k.dividedBy=k.div=function(o){return Z(this,new this.constructor(o))};k.dividedToIntegerBy=k.divToInt=function(o){var t=this,e=t.constructor;return O(Z(t,new e(o),0,1,1),e.precision,e.rounding)};k.equals=k.eq=function(o){return this.cmp(o)===0};k.floor=function(){return O(new this.constructor(this),this.e+1,3)};k.greaterThan=k.gt=function(o){return this.cmp(o)>0};k.greaterThanOrEqualTo=k.gte=function(o){var t=this.cmp(o);return t==1||t===0};k.hyperbolicCosine=k.cosh=function(){var o,t,e,i,n,r=this,a=r.constructor,u=new a(1);if(!r.isFinite())return new a(r.s?1/0:NaN);if(r.isZero())return u;e=a.precision,i=a.rounding,a.precision=e+Math.max(r.e,r.sd())+4,a.rounding=1,n=r.d.length,n<32?(o=Math.ceil(n/3),t=(1/ai(4,o)).toString()):(o=16,t="2.3283064365386962890625e-10"),r=Ce(a,1,r.times(t),new a(1),!0);for(var d,f=o,h=new a(8);f--;)d=r.times(r),r=u.minus(d.times(h.minus(d.times(h))));return O(r,a.precision=e,a.rounding=i,!0)};k.hyperbolicSine=k.sinh=function(){var o,t,e,i,n=this,r=n.constructor;if(!n.isFinite()||n.isZero())return new r(n);if(t=r.precision,e=r.rounding,r.precision=t+Math.max(n.e,n.sd())+4,r.rounding=1,i=n.d.length,i<3)n=Ce(r,2,n,n,!0);else{o=1.4*Math.sqrt(i),o=o>16?16:o|0,n=n.times(1/ai(5,o)),n=Ce(r,2,n,n,!0);for(var a,u=new r(5),d=new r(16),f=new r(20);o--;)a=n.times(n),n=n.times(u.plus(a.times(d.times(a).plus(f))))}return r.precision=t,r.rounding=e,O(n,t,e,!0)};k.hyperbolicTangent=k.tanh=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+7,i.rounding=1,Z(e.sinh(),e.cosh(),i.precision=o,i.rounding=t)):new i(e.s)};k.inverseCosine=k.acos=function(){var o=this,t=o.constructor,e=o.abs().cmp(1),i=t.precision,n=t.rounding;return e!==-1?e===0?o.isNeg()?Jt(t,i,n):new t(0):new t(NaN):o.isZero()?Jt(t,i+4,n).times(.5):(t.precision=i+6,t.rounding=1,o=new t(1).minus(o).div(o.plus(1)).sqrt().atan(),t.precision=i,t.rounding=n,o.times(2))};k.inverseHyperbolicCosine=k.acosh=function(){var o,t,e=this,i=e.constructor;return e.lte(1)?new i(e.eq(1)?0:NaN):e.isFinite()?(o=i.precision,t=i.rounding,i.precision=o+Math.max(Math.abs(e.e),e.sd())+4,i.rounding=1,N=!1,e=e.times(e).minus(1).sqrt().plus(e),N=!0,i.precision=o,i.rounding=t,e.ln()):new i(e)};k.inverseHyperbolicSine=k.asinh=function(){var o,t,e=this,i=e.constructor;return!e.isFinite()||e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+2*Math.max(Math.abs(e.e),e.sd())+6,i.rounding=1,N=!1,e=e.times(e).plus(1).sqrt().plus(e),N=!0,i.precision=o,i.rounding=t,e.ln())};k.inverseHyperbolicTangent=k.atanh=function(){var o,t,e,i,n=this,r=n.constructor;return n.isFinite()?n.e>=0?new r(n.abs().eq(1)?n.s/0:n.isZero()?n:NaN):(o=r.precision,t=r.rounding,i=n.sd(),Math.max(i,o)<2*-n.e-1?O(new r(n),o,t,!0):(r.precision=e=i-n.e,n=Z(n.plus(1),new r(1).minus(n),e+o,1),r.precision=o+4,r.rounding=1,n=n.ln(),r.precision=o,r.rounding=t,n.times(.5))):new r(NaN)};k.inverseSine=k.asin=function(){var o,t,e,i,n=this,r=n.constructor;return n.isZero()?new r(n):(t=n.abs().cmp(1),e=r.precision,i=r.rounding,t!==-1?t===0?(o=Jt(r,e+4,i).times(.5),o.s=n.s,o):new r(NaN):(r.precision=e+6,r.rounding=1,n=n.div(new r(1).minus(n.times(n)).sqrt().plus(1)).atan(),r.precision=e,r.rounding=i,n.times(2)))};k.inverseTangent=k.atan=function(){var o,t,e,i,n,r,a,u,d,f=this,h=f.constructor,w=h.precision,P=h.rounding;if(f.isFinite()){if(f.isZero())return new h(f);if(f.abs().eq(1)&&w+4<=on)return a=Jt(h,w+4,P).times(.25),a.s=f.s,a}else{if(!f.s)return new h(NaN);if(w+4<=on)return a=Jt(h,w+4,P).times(.5),a.s=f.s,a}for(h.precision=u=w+10,h.rounding=1,e=Math.min(28,u/F+2|0),o=e;o;--o)f=f.div(f.times(f).plus(1).sqrt().plus(1));for(N=!1,t=Math.ceil(u/F),i=1,d=f.times(f),a=new h(f),n=f;o!==-1;)if(n=n.times(d),r=a.minus(n.div(i+=2)),n=n.times(d),a=r.plus(n.div(i+=2)),a.d[t]!==void 0)for(o=t;a.d[o]===r.d[o]&&o--;);return e&&(a=a.times(2<<e-1)),N=!0,O(a,h.precision=w,h.rounding=P,!0)};k.isFinite=function(){return!!this.d};k.isInteger=k.isInt=function(){return!!this.d&&Ft(this.e/F)>this.d.length-2};k.isNaN=function(){return!this.s};k.isNegative=k.isNeg=function(){return this.s<0};k.isPositive=k.isPos=function(){return this.s>0};k.isZero=function(){return!!this.d&&this.d[0]===0};k.lessThan=k.lt=function(o){return this.cmp(o)<0};k.lessThanOrEqualTo=k.lte=function(o){return this.cmp(o)<1};k.logarithm=k.log=function(o){var t,e,i,n,r,a,u,d,f=this,h=f.constructor,w=h.precision,P=h.rounding,M=5;if(o==null)o=new h(10),t=!0;else{if(o=new h(o),e=o.d,o.s<0||!e||!e[0]||o.eq(1))return new h(NaN);t=o.eq(10)}if(e=f.d,f.s<0||!e||!e[0]||f.eq(1))return new h(e&&!e[0]?-1/0:f.s!=1?NaN:e?0:1/0);if(t)if(e.length>1)r=!0;else{for(n=e[0];n%10===0;)n/=10;r=n!==1}if(N=!1,u=w+M,a=le(f,u),i=t?oi(h,u+10):le(o,u),d=Z(a,i,u,1),De(d.d,n=w,P))do if(u+=10,a=le(f,u),i=t?oi(h,u+10):le(o,u),d=Z(a,i,u,1),!r){+Ot(d.d).slice(n+1,n+15)+1==1e14&&(d=O(d,w+1,0));break}while(De(d.d,n+=10,P));return N=!0,O(d,w,P)};k.minus=k.sub=function(o){var t,e,i,n,r,a,u,d,f,h,w,P,M=this,T=M.constructor;if(o=new T(o),!M.d||!o.d)return!M.s||!o.s?o=new T(NaN):M.d?o.s=-o.s:o=new T(o.d||M.s!==o.s?M:NaN),o;if(M.s!=o.s)return o.s=-o.s,M.plus(o);if(f=M.d,P=o.d,u=T.precision,d=T.rounding,!f[0]||!P[0]){if(P[0])o.s=-o.s;else if(f[0])o=new T(M);else return new T(d===3?-0:0);return N?O(o,u,d):o}if(e=Ft(o.e/F),h=Ft(M.e/F),f=f.slice(),r=h-e,r){for(w=r<0,w?(t=f,r=-r,a=P.length):(t=P,e=h,a=f.length),i=Math.max(Math.ceil(u/F),a)+2,r>i&&(r=i,t.length=1),t.reverse(),i=r;i--;)t.push(0);t.reverse()}else{for(i=f.length,a=P.length,w=i<a,w&&(a=i),i=0;i<a;i++)if(f[i]!=P[i]){w=f[i]<P[i];break}r=0}for(w&&(t=f,f=P,P=t,o.s=-o.s),a=f.length,i=P.length-a;i>0;--i)f[a++]=0;for(i=P.length;i>r;){if(f[--i]<P[i]){for(n=i;n&&f[--n]===0;)f[n]=Ht-1;--f[n],f[i]+=Ht}f[i]-=P[i]}for(;f[--a]===0;)f.pop();for(;f[0]===0;f.shift())--e;return f[0]?(o.d=f,o.e=si(f,e),N?O(o,u,d):o):new T(d===3?-0:0)};k.modulo=k.mod=function(o){var t,e=this,i=e.constructor;return o=new i(o),!e.d||!o.s||o.d&&!o.d[0]?new i(NaN):!o.d||e.d&&!e.d[0]?O(new i(e),i.precision,i.rounding):(N=!1,i.modulo==9?(t=Z(e,o.abs(),0,3,1),t.s*=o.s):t=Z(e,o,0,i.modulo,1),t=t.times(o),N=!0,e.minus(t))};k.naturalExponential=k.exp=function(){return rn(this)};k.naturalLogarithm=k.ln=function(){return le(this)};k.negated=k.neg=function(){var o=new this.constructor(this);return o.s=-o.s,O(o)};k.plus=k.add=function(o){var t,e,i,n,r,a,u,d,f,h,w=this,P=w.constructor;if(o=new P(o),!w.d||!o.d)return!w.s||!o.s?o=new P(NaN):w.d||(o=new P(o.d||w.s===o.s?w:NaN)),o;if(w.s!=o.s)return o.s=-o.s,w.minus(o);if(f=w.d,h=o.d,u=P.precision,d=P.rounding,!f[0]||!h[0])return h[0]||(o=new P(w)),N?O(o,u,d):o;if(r=Ft(w.e/F),i=Ft(o.e/F),f=f.slice(),n=r-i,n){for(n<0?(e=f,n=-n,a=h.length):(e=h,i=r,a=f.length),r=Math.ceil(u/F),a=r>a?r+1:a+1,n>a&&(n=a,e.length=1),e.reverse();n--;)e.push(0);e.reverse()}for(a=f.length,n=h.length,a-n<0&&(n=a,e=h,h=f,f=e),t=0;n;)t=(f[--n]=f[n]+h[n]+t)/Ht|0,f[n]%=Ht;for(t&&(f.unshift(t),++i),a=f.length;f[--a]==0;)f.pop();return o.d=f,o.e=si(f,i),N?O(o,u,d):o};k.precision=k.sd=function(o){var t,e=this;if(o!==void 0&&o!==!!o&&o!==1&&o!==0)throw Error(ue+o);return e.d?(t=yo(e.d),o&&e.e+1>t&&(t=e.e+1)):t=NaN,t};k.round=function(){var o=this,t=o.constructor;return O(new t(o),o.e+1,t.rounding)};k.sine=k.sin=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+Math.max(e.e,e.sd())+F,i.rounding=1,e=Er(i,Mo(i,e)),i.precision=o,i.rounding=t,O(oe>2?e.neg():e,o,t,!0)):new i(NaN)};k.squareRoot=k.sqrt=function(){var o,t,e,i,n,r,a=this,u=a.d,d=a.e,f=a.s,h=a.constructor;if(f!==1||!u||!u[0])return new h(!f||f<0&&(!u||u[0])?NaN:u?a:1/0);for(N=!1,f=Math.sqrt(+a),f==0||f==1/0?(t=Ot(u),(t.length+d)%2==0&&(t+="0"),f=Math.sqrt(t),d=Ft((d+1)/2)-(d<0||d%2),f==1/0?t="5e"+d:(t=f.toExponential(),t=t.slice(0,t.indexOf("e")+1)+d),i=new h(t)):i=new h(f.toString()),e=(d=h.precision)+3;;)if(r=i,i=r.plus(Z(a,r,e+2,1)).times(.5),Ot(r.d).slice(0,e)===(t=Ot(i.d)).slice(0,e))if(t=t.slice(e-3,e+1),t=="9999"||!n&&t=="4999"){if(!n&&(O(r,d+1,0),r.times(r).eq(a))){i=r;break}e+=4,n=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(O(i,d+1,1),o=!i.times(i).eq(a));break}return N=!0,O(i,d,h.rounding,o)};k.tangent=k.tan=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+10,i.rounding=1,e=e.sin(),e.s=1,e=Z(e,new i(1).minus(e.times(e)).sqrt(),o+10,0),i.precision=o,i.rounding=t,O(oe==2||oe==4?e.neg():e,o,t,!0)):new i(NaN)};k.times=k.mul=function(o){var t,e,i,n,r,a,u,d,f,h=this,w=h.constructor,P=h.d,M=(o=new w(o)).d;if(o.s*=h.s,!P||!P[0]||!M||!M[0])return new w(!o.s||P&&!P[0]&&!M||M&&!M[0]&&!P?NaN:!P||!M?o.s/0:o.s*0);for(e=Ft(h.e/F)+Ft(o.e/F),d=P.length,f=M.length,d<f&&(r=P,P=M,M=r,a=d,d=f,f=a),r=[],a=d+f,i=a;i--;)r.push(0);for(i=f;--i>=0;){for(t=0,n=d+i;n>i;)u=r[n]+M[i]*P[n-i-1]+t,r[n--]=u%Ht|0,t=u/Ht|0;r[n]=(r[n]+t)%Ht|0}for(;!r[--a];)r.pop();return t?++e:r.shift(),o.d=r,o.e=si(r,e),N?O(o,w.precision,w.rounding):o};k.toBinary=function(o,t){return sn(this,2,o,t)};k.toDecimalPlaces=k.toDP=function(o,t){var e=this,i=e.constructor;return e=new i(e),o===void 0?e:(Nt(o,0,de),t===void 0?t=i.rounding:Nt(t,0,8),O(e,o+e.e+1,t))};k.toExponential=function(o,t){var e,i=this,n=i.constructor;return o===void 0?e=Xt(i,!0):(Nt(o,0,de),t===void 0?t=n.rounding:Nt(t,0,8),i=O(new n(i),o+1,t),e=Xt(i,!0,o+1)),i.isNeg()&&!i.isZero()?"-"+e:e};k.toFixed=function(o,t){var e,i,n=this,r=n.constructor;return o===void 0?e=Xt(n):(Nt(o,0,de),t===void 0?t=r.rounding:Nt(t,0,8),i=O(new r(n),o+n.e+1,t),e=Xt(i,!1,o+i.e+1)),n.isNeg()&&!n.isZero()?"-"+e:e};k.toFraction=function(o){var t,e,i,n,r,a,u,d,f,h,w,P,M=this,T=M.d,x=M.constructor;if(!T)return new x(M);if(f=e=new x(1),i=d=new x(0),t=new x(i),r=t.e=yo(T)-M.e-1,a=r%F,t.d[0]=It(10,a<0?F+a:a),o==null)o=r>0?t:f;else{if(u=new x(o),!u.isInt()||u.lt(f))throw Error(ue+u);o=u.gt(t)?r>0?t:f:u}for(N=!1,u=new x(Ot(T)),h=x.precision,x.precision=r=T.length*F*2;w=Z(u,t,0,1,1),n=e.plus(w.times(i)),n.cmp(o)!=1;)e=i,i=n,n=f,f=d.plus(w.times(n)),d=n,n=t,t=u.minus(w.times(n)),u=n;return n=Z(o.minus(e),i,0,1,1),d=d.plus(n.times(f)),e=e.plus(n.times(i)),d.s=f.s=M.s,P=Z(f,i,r,1).minus(M).abs().cmp(Z(d,e,r,1).minus(M).abs())<1?[f,i]:[d,e],x.precision=h,N=!0,P};k.toHexadecimal=k.toHex=function(o,t){return sn(this,16,o,t)};k.toNearest=function(o,t){var e=this,i=e.constructor;if(e=new i(e),o==null){if(!e.d)return e;o=new i(1),t=i.rounding}else{if(o=new i(o),t===void 0?t=i.rounding:Nt(t,0,8),!e.d)return o.s?e:o;if(!o.d)return o.s&&(o.s=e.s),o}return o.d[0]?(N=!1,e=Z(e,o,0,t,1).times(o),N=!0,O(e)):(o.s=e.s,e=o),e};k.toNumber=function(){return+this};k.toOctal=function(o,t){return sn(this,8,o,t)};k.toPower=k.pow=function(o){var t,e,i,n,r,a,u=this,d=u.constructor,f=+(o=new d(o));if(!u.d||!o.d||!u.d[0]||!o.d[0])return new d(It(+u,f));if(u=new d(u),u.eq(1))return u;if(i=d.precision,r=d.rounding,o.eq(1))return O(u,i,r);if(t=Ft(o.e/F),t>=o.d.length-1&&(e=f<0?-f:f)<=Rr)return n=vo(d,u,e,i),o.s<0?new d(1).div(n):O(n,i,r);if(a=u.s,a<0){if(t<o.d.length-1)return new d(NaN);if((o.d[t]&1)==0&&(a=1),u.e==0&&u.d[0]==1&&u.d.length==1)return u.s=a,u}return e=It(+u,f),t=e==0||!isFinite(e)?Ft(f*(Math.log("0."+Ot(u.d))/Math.LN10+u.e+1)):new d(e+"").e,t>d.maxE+1||t<d.minE-1?new d(t>0?a/0:0):(N=!1,d.rounding=u.s=1,e=Math.min(12,(t+"").length),n=rn(o.times(le(u,i+e)),i),n.d&&(n=O(n,i+5,1),De(n.d,i,r)&&(t=i+10,n=O(rn(o.times(le(u,t+e)),t),t+5,1),+Ot(n.d).slice(i+1,i+15)+1==1e14&&(n=O(n,i+1,0)))),n.s=a,N=!0,d.rounding=r,O(n,i,r))};k.toPrecision=function(o,t){var e,i=this,n=i.constructor;return o===void 0?e=Xt(i,i.e<=n.toExpNeg||i.e>=n.toExpPos):(Nt(o,1,de),t===void 0?t=n.rounding:Nt(t,0,8),i=O(new n(i),o,t),e=Xt(i,o<=i.e||i.e<=n.toExpNeg,o)),i.isNeg()&&!i.isZero()?"-"+e:e};k.toSignificantDigits=k.toSD=function(o,t){var e=this,i=e.constructor;return o===void 0?(o=i.precision,t=i.rounding):(Nt(o,1,de),t===void 0?t=i.rounding:Nt(t,0,8)),O(new i(e),o,t)};k.toString=function(){var o=this,t=o.constructor,e=Xt(o,o.e<=t.toExpNeg||o.e>=t.toExpPos);return o.isNeg()&&!o.isZero()?"-"+e:e};k.truncated=k.trunc=function(){return O(new this.constructor(this),this.e+1,1)};k.valueOf=k.toJSON=function(){var o=this,t=o.constructor,e=Xt(o,o.e<=t.toExpNeg||o.e>=t.toExpPos);return o.isNeg()?"-"+e:e};function Ot(o){var t,e,i,n=o.length-1,r="",a=o[0];if(n>0){for(r+=a,t=1;t<n;t++)i=o[t]+"",e=F-i.length,e&&(r+=ce(e)),r+=i;a=o[t],i=a+"",e=F-i.length,e&&(r+=ce(e))}else if(a===0)return"0";for(;a%10===0;)a/=10;return r+a}function Nt(o,t,e){if(o!==~~o||o<t||o>e)throw Error(ue+o)}function De(o,t,e,i){var n,r,a,u;for(r=o[0];r>=10;r/=10)--t;return--t<0?(t+=F,n=0):(n=Math.ceil((t+1)/F),t%=F),r=It(10,F-t),u=o[n]%r|0,i==null?t<3?(t==0?u=u/100|0:t==1&&(u=u/10|0),a=e<4&&u==99999||e>3&&u==49999||u==5e4||u==0):a=(e<4&&u+1==r||e>3&&u+1==r/2)&&(o[n+1]/r/100|0)==It(10,t-2)-1||(u==r/2||u==0)&&(o[n+1]/r/100|0)==0:t<4?(t==0?u=u/1e3|0:t==1?u=u/100|0:t==2&&(u=u/10|0),a=(i||e<4)&&u==9999||!i&&e>3&&u==4999):a=((i||e<4)&&u+1==r||!i&&e>3&&u+1==r/2)&&(o[n+1]/r/1e3|0)==It(10,t-3)-1,a}function ti(o,t,e){for(var i,n=[0],r,a=0,u=o.length;a<u;){for(r=n.length;r--;)n[r]*=t;for(n[0]+=en.indexOf(o.charAt(a++)),i=0;i<n.length;i++)n[i]>e-1&&(n[i+1]===void 0&&(n[i+1]=0),n[i+1]+=n[i]/e|0,n[i]%=e)}return n.reverse()}function Fr(o,t){var e,i,n;if(t.isZero())return t;i=t.d.length,i<32?(e=Math.ceil(i/3),n=(1/ai(4,e)).toString()):(e=16,n="2.3283064365386962890625e-10"),o.precision+=e,t=Ce(o,1,t.times(n),new o(1));for(var r=e;r--;){var a=t.times(t);t=a.times(a).minus(a).times(8).plus(1)}return o.precision-=e,t}var Z=function(){function o(i,n,r){var a,u=0,d=i.length;for(i=i.slice();d--;)a=i[d]*n+u,i[d]=a%r|0,u=a/r|0;return u&&i.unshift(u),i}function t(i,n,r,a){var u,d;if(r!=a)d=r>a?1:-1;else for(u=d=0;u<r;u++)if(i[u]!=n[u]){d=i[u]>n[u]?1:-1;break}return d}function e(i,n,r,a){for(var u=0;r--;)i[r]-=u,u=i[r]<n[r]?1:0,i[r]=u*a+i[r]-n[r];for(;!i[0]&&i.length>1;)i.shift()}return function(i,n,r,a,u,d){var f,h,w,P,M,T,x,$,j,K,B,D,jt,z,Lt,ee,Zt,ie,L,Et,y=i.constructor,s=i.s==n.s?1:-1,l=i.d,_=n.d;if(!l||!l[0]||!_||!_[0])return new y(!i.s||!n.s||(l?_&&l[0]==_[0]:!_)?NaN:l&&l[0]==0||!_?s*0:s/0);for(d?(M=1,h=i.e-n.e):(d=Ht,M=F,h=Ft(i.e/M)-Ft(n.e/M)),L=_.length,Zt=l.length,j=new y(s),K=j.d=[],w=0;_[w]==(l[w]||0);w++);if(_[w]>(l[w]||0)&&h--,r==null?(z=r=y.precision,a=y.rounding):u?z=r+(i.e-n.e)+1:z=r,z<0)K.push(1),T=!0;else{if(z=z/M+2|0,w=0,L==1){for(P=0,_=_[0],z++;(w<Zt||P)&&z--;w++)Lt=P*d+(l[w]||0),K[w]=Lt/_|0,P=Lt%_|0;T=P||w<Zt}else{for(P=d/(_[0]+1)|0,P>1&&(_=o(_,P,d),l=o(l,P,d),L=_.length,Zt=l.length),ee=L,B=l.slice(0,L),D=B.length;D<L;)B[D++]=0;Et=_.slice(),Et.unshift(0),ie=_[0],_[1]>=d/2&&++ie;do P=0,f=t(_,B,L,D),f<0?(jt=B[0],L!=D&&(jt=jt*d+(B[1]||0)),P=jt/ie|0,P>1?(P>=d&&(P=d-1),x=o(_,P,d),$=x.length,D=B.length,f=t(x,B,$,D),f==1&&(P--,e(x,L<$?Et:_,$,d))):(P==0&&(f=P=1),x=_.slice()),$=x.length,$<D&&x.unshift(0),e(B,x,D,d),f==-1&&(D=B.length,f=t(_,B,L,D),f<1&&(P++,e(B,L<D?Et:_,D,d))),D=B.length):f===0&&(P++,B=[0]),K[w++]=P,f&&B[0]?B[D++]=l[ee]||0:(B=[l[ee]],D=1);while((ee++<Zt||B[0]!==void 0)&&z--);T=B[0]!==void 0}K[0]||K.shift()}if(M==1)j.e=h,po=T;else{for(w=1,P=K[0];P>=10;P/=10)w++;j.e=w+h*M-1,O(j,u?r+j.e+1:r,a,T)}return j}}();function O(o,t,e,i){var n,r,a,u,d,f,h,w,P,M=o.constructor;t:if(t!=null){if(w=o.d,!w)return o;for(n=1,u=w[0];u>=10;u/=10)n++;if(r=t-n,r<0)r+=F,a=t,h=w[P=0],d=h/It(10,n-a-1)%10|0;else if(P=Math.ceil((r+1)/F),u=w.length,P>=u)if(i){for(;u++<=P;)w.push(0);h=d=0,n=1,r%=F,a=r-F+1}else break t;else{for(h=u=w[P],n=1;u>=10;u/=10)n++;r%=F,a=r-F+n,d=a<0?0:h/It(10,n-a-1)%10|0}if(i=i||t<0||w[P+1]!==void 0||(a<0?h:h%It(10,n-a-1)),f=e<4?(d||i)&&(e==0||e==(o.s<0?3:2)):d>5||d==5&&(e==4||i||e==6&&(r>0?a>0?h/It(10,n-a):0:w[P-1])%10&1||e==(o.s<0?8:7)),t<1||!w[0])return w.length=0,f?(t-=o.e+1,w[0]=It(10,(F-t%F)%F),o.e=-t||0):w[0]=o.e=0,o;if(r==0?(w.length=P,u=1,P--):(w.length=P+1,u=It(10,F-r),w[P]=a>0?(h/It(10,n-a)%It(10,a)|0)*u:0),f)for(;;)if(P==0){for(r=1,a=w[0];a>=10;a/=10)r++;for(a=w[0]+=u,u=1;a>=10;a/=10)u++;r!=u&&(o.e++,w[0]==Ht&&(w[0]=1));break}else{if(w[P]+=u,w[P]!=Ht)break;w[P--]=0,u=1}for(r=w.length;w[--r]===0;)w.pop()}return N&&(o.e>M.maxE?(o.d=null,o.e=NaN):o.e<M.minE&&(o.e=0,o.d=[0])),o}function Xt(o,t,e){if(!o.isFinite())return Co(o);var i,n=o.e,r=Ot(o.d),a=r.length;return t?(e&&(i=e-a)>0?r=r.charAt(0)+"."+r.slice(1)+ce(i):a>1&&(r=r.charAt(0)+"."+r.slice(1)),r=r+(o.e<0?"e":"e+")+o.e):n<0?(r="0."+ce(-n-1)+r,e&&(i=e-a)>0&&(r+=ce(i))):n>=a?(r+=ce(n+1-a),e&&(i=e-n-1)>0&&(r=r+"."+ce(i))):((i=n+1)<a&&(r=r.slice(0,i)+"."+r.slice(i)),e&&(i=e-a)>0&&(n+1===a&&(r+="."),r+=ce(i))),r}function si(o,t){var e=o[0];for(t*=F;e>=10;e/=10)t++;return t}function oi(o,t,e){if(t>Or)throw N=!0,e&&(o.precision=e),Error(mo);return O(new o(ii),t,1,!0)}function Jt(o,t,e){if(t>on)throw Error(mo);return O(new o(ni),t,e,!0)}function yo(o){var t=o.length-1,e=t*F+1;if(t=o[t],t){for(;t%10==0;t/=10)e--;for(t=o[0];t>=10;t/=10)e++}return e}function ce(o){for(var t="";o--;)t+="0";return t}function vo(o,t,e,i){var n,r=new o(1),a=Math.ceil(i/F+4);for(N=!1;;){if(e%2&&(r=r.times(t),_o(r.d,a)&&(n=!0)),e=Ft(e/2),e===0){e=r.d.length-1,n&&r.d[e]===0&&++r.d[e];break}t=t.times(t),_o(t.d,a)}return N=!0,r}function fo(o){return o.d[o.d.length-1]&1}function Po(o,t,e){for(var i,n,r=new o(t[0]),a=0;++a<t.length;){if(n=new o(t[a]),!n.s){r=n;break}i=r.cmp(n),(i===e||i===0&&r.s===e)&&(r=n)}return r}function rn(o,t){var e,i,n,r,a,u,d,f=0,h=0,w=0,P=o.constructor,M=P.rounding,T=P.precision;if(!o.d||!o.d[0]||o.e>17)return new P(o.d?o.d[0]?o.s<0?0:1/0:1:o.s?o.s<0?0:o:NaN);for(t==null?(N=!1,d=T):d=t,u=new P(.03125);o.e>-2;)o=o.times(u),w+=5;for(i=Math.log(It(2,w))/Math.LN10*2+5|0,d+=i,e=r=a=new P(1),P.precision=d;;){if(r=O(r.times(o),d,1),e=e.times(++h),u=a.plus(Z(r,e,d,1)),Ot(u.d).slice(0,d)===Ot(a.d).slice(0,d)){for(n=w;n--;)a=O(a.times(a),d,1);if(t==null)if(f<3&&De(a.d,d-i,M,f))P.precision=d+=10,e=r=u=new P(1),h=0,f++;else return O(a,P.precision=T,M,N=!0);else return P.precision=T,a}a=u}}function le(o,t){var e,i,n,r,a,u,d,f,h,w,P,M=1,T=10,x=o,$=x.d,j=x.constructor,K=j.rounding,B=j.precision;if(x.s<0||!$||!$[0]||!x.e&&$[0]==1&&$.length==1)return new j($&&!$[0]?-1/0:x.s!=1?NaN:$?0:x);if(t==null?(N=!1,h=B):h=t,j.precision=h+=T,e=Ot($),i=e.charAt(0),Math.abs(r=x.e)<15e14){for(;i<7&&i!=1||i==1&&e.charAt(1)>3;)x=x.times(o),e=Ot(x.d),i=e.charAt(0),M++;r=x.e,i>1?(x=new j("0."+e),r++):x=new j(i+"."+e.slice(1))}else return f=oi(j,h+2,B).times(r+""),x=le(new j(i+"."+e.slice(1)),h-T).plus(f),j.precision=B,t==null?O(x,B,K,N=!0):x;for(w=x,d=a=x=Z(x.minus(1),x.plus(1),h,1),P=O(x.times(x),h,1),n=3;;){if(a=O(a.times(P),h,1),f=d.plus(Z(a,new j(n),h,1)),Ot(f.d).slice(0,h)===Ot(d.d).slice(0,h))if(d=d.times(2),r!==0&&(d=d.plus(oi(j,h+2,B).times(r+""))),d=Z(d,new j(M),h,1),t==null)if(De(d.d,h-T,K,u))j.precision=h+=T,f=a=x=Z(w.minus(1),w.plus(1),h,1),P=O(x.times(x),h,1),n=u=1;else return O(d,j.precision=B,K,N=!0);else return j.precision=B,d;d=f,n+=2}}function Co(o){return String(o.s*o.s/0)}function ei(o,t){var e,i,n;for((e=t.indexOf("."))>-1&&(t=t.replace(".","")),(i=t.search(/e/i))>0?(e<0&&(e=i),e+=+t.slice(i+1),t=t.substring(0,i)):e<0&&(e=t.length),i=0;t.charCodeAt(i)===48;i++);for(n=t.length;t.charCodeAt(n-1)===48;--n);if(t=t.slice(i,n),t){if(n-=i,o.e=e=e-i-1,o.d=[],i=(e+1)%F,e<0&&(i+=F),i<n){for(i&&o.d.push(+t.slice(0,i)),n-=F;i<n;)o.d.push(+t.slice(i,i+=F));t=t.slice(i),i=F-t.length}else i-=n;for(;i--;)t+="0";o.d.push(+t),N&&(o.e>o.constructor.maxE?(o.d=null,o.e=NaN):o.e<o.constructor.minE&&(o.e=0,o.d=[0]))}else o.e=0,o.d=[0];return o}function Br(o,t){var e,i,n,r,a,u,d,f,h;if(t.indexOf("_")>-1){if(t=t.replace(/(\d)_(?=\d)/g,"$1"),wo.test(t))return ei(o,t)}else if(t==="Infinity"||t==="NaN")return+t||(o.s=NaN),o.e=NaN,o.d=null,o;if(Sr.test(t))e=16,t=t.toLowerCase();else if(xr.test(t))e=2;else if(Ir.test(t))e=8;else throw Error(ue+t);for(r=t.search(/p/i),r>0?(d=+t.slice(r+1),t=t.substring(2,r)):t=t.slice(2),r=t.indexOf("."),a=r>=0,i=o.constructor,a&&(t=t.replace(".",""),u=t.length,r=u-r,n=vo(i,new i(e),r,r*2)),f=ti(t,e,Ht),h=f.length-1,r=h;f[r]===0;--r)f.pop();return r<0?new i(o.s*0):(o.e=si(f,h),o.d=f,N=!1,a&&(o=Z(o,n,u*4)),d&&(o=o.times(Math.abs(d)<54?It(2,d):Le.pow(2,d))),N=!0,o)}function Er(o,t){var e,i=t.d.length;if(i<3)return t.isZero()?t:Ce(o,2,t,t);e=1.4*Math.sqrt(i),e=e>16?16:e|0,t=t.times(1/ai(5,e)),t=Ce(o,2,t,t);for(var n,r=new o(5),a=new o(16),u=new o(20);e--;)n=t.times(t),t=t.times(r.plus(n.times(a.times(n).minus(u))));return t}function Ce(o,t,e,i,n){var r,a,u,d,f=1,h=o.precision,w=Math.ceil(h/F);for(N=!1,d=e.times(e),u=new o(i);;){if(a=Z(u.times(d),new o(t++*t++),h,1),u=n?i.plus(a):i.minus(a),i=Z(a.times(d),new o(t++*t++),h,1),a=u.plus(i),a.d[w]!==void 0){for(r=w;a.d[r]===u.d[r]&&r--;);if(r==-1)break}r=u,u=i,i=a,a=r,f++}return N=!0,a.d.length=w+1,a}function ai(o,t){for(var e=o;--t;)e*=o;return e}function Mo(o,t){var e,i=t.s<0,n=Jt(o,o.precision,1),r=n.times(.5);if(t=t.abs(),t.lte(r))return oe=i?4:1,t;if(e=t.divToInt(n),e.isZero())oe=i?3:2;else{if(t=t.minus(e.times(n)),t.lte(r))return oe=fo(e)?i?2:3:i?4:1,t;oe=fo(e)?i?1:4:i?3:2}return t.minus(n).abs()}function sn(o,t,e,i){var n,r,a,u,d,f,h,w,P,M=o.constructor,T=e!==void 0;if(T?(Nt(e,1,de),i===void 0?i=M.rounding:Nt(i,0,8)):(e=M.precision,i=M.rounding),!o.isFinite())h=Co(o);else{for(h=Xt(o),a=h.indexOf("."),T?(n=2,t==16?e=e*4-3:t==8&&(e=e*3-2)):n=t,a>=0&&(h=h.replace(".",""),P=new M(1),P.e=h.length-a,P.d=ti(Xt(P),10,n),P.e=P.d.length),w=ti(h,10,n),r=d=w.length;w[--d]==0;)w.pop();if(!w[0])h=T?"0p+0":"0";else{if(a<0?r--:(o=new M(o),o.d=w,o.e=r,o=Z(o,P,e,i,0,n),w=o.d,r=o.e,f=po),a=w[e],u=n/2,f=f||w[e+1]!==void 0,f=i<4?(a!==void 0||f)&&(i===0||i===(o.s<0?3:2)):a>u||a===u&&(i===4||f||i===6&&w[e-1]&1||i===(o.s<0?8:7)),w.length=e,f)for(;++w[--e]>n-1;)w[e]=0,e||(++r,w.unshift(1));for(d=w.length;!w[d-1];--d);for(a=0,h="";a<d;a++)h+=en.charAt(w[a]);if(T){if(d>1)if(t==16||t==8){for(a=t==16?4:3,--d;d%a;d++)h+="0";for(w=ti(h,n,t),d=w.length;!w[d-1];--d);for(a=1,h="1.";a<d;a++)h+=en.charAt(w[a])}else h=h.charAt(0)+"."+h.slice(1);h=h+(r<0?"p":"p+")+r}else if(r<0){for(;++r;)h="0"+h;h="0."+h}else if(++r>d)for(r-=d;r--;)h+="0";else r<d&&(h=h.slice(0,r)+"."+h.slice(r))}h=(t==16?"0x":t==2?"0b":t==8?"0o":"")+h}return o.s<0?"-"+h:h}function _o(o,t){if(o.length>t)return o.length=t,!0}function Nr(o){return new this(o).abs()}function jr(o){return new this(o).acos()}function qr(o){return new this(o).acosh()}function Dr(o,t){return new this(o).plus(t)}function Lr(o){return new this(o).asin()}function $r(o){return new this(o).asinh()}function Ur(o){return new this(o).atan()}function Kr(o){return new this(o).atanh()}function Wr(o,t){o=new this(o),t=new this(t);var e,i=this.precision,n=this.rounding,r=i+4;return!o.s||!t.s?e=new this(NaN):!o.d&&!t.d?(e=Jt(this,r,1).times(t.s>0?.25:.75),e.s=o.s):!t.d||o.isZero()?(e=t.s<0?Jt(this,i,n):new this(0),e.s=o.s):!o.d||t.isZero()?(e=Jt(this,r,1).times(.5),e.s=o.s):t.s<0?(this.precision=r,this.rounding=1,e=this.atan(Z(o,t,r,1)),t=Jt(this,r,1),this.precision=i,this.rounding=n,e=o.s<0?e.minus(t):e.plus(t)):e=this.atan(Z(o,t,r,1)),e}function Hr(o){return new this(o).cbrt()}function Zr(o){return O(o=new this(o),o.e+1,2)}function zr(o,t,e){return new this(o).clamp(t,e)}function Gr(o){if(!o||typeof o!="object")throw Error(ri+"Object expected");var t,e,i,n=o.defaults===!0,r=["precision",1,de,"rounding",0,8,"toExpNeg",-Pe,0,"toExpPos",0,Pe,"maxE",0,Pe,"minE",-Pe,0,"modulo",0,9];for(t=0;t<r.length;t+=3)if(e=r[t],n&&(this[e]=nn[e]),(i=o[e])!==void 0)if(Ft(i)===i&&i>=r[t+1]&&i<=r[t+2])this[e]=i;else throw Error(ue+e+": "+i);if(e="crypto",n&&(this[e]=nn[e]),(i=o[e])!==void 0)if(i===!0||i===!1||i===0||i===1)if(i)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[e]=!0;else throw Error(go);else this[e]=!1;else throw Error(ue+e+": "+i);return this}function Vr(o){return new this(o).cos()}function Qr(o){return new this(o).cosh()}function ko(o){var t,e,i;function n(r){var a,u,d,f=this;if(!(f instanceof n))return new n(r);if(f.constructor=n,ho(r)){f.s=r.s,N?!r.d||r.e>n.maxE?(f.e=NaN,f.d=null):r.e<n.minE?(f.e=0,f.d=[0]):(f.e=r.e,f.d=r.d.slice()):(f.e=r.e,f.d=r.d?r.d.slice():r.d);return}if(d=typeof r,d==="number"){if(r===0){f.s=1/r<0?-1:1,f.e=0,f.d=[0];return}if(r<0?(r=-r,f.s=-1):f.s=1,r===~~r&&r<1e7){for(a=0,u=r;u>=10;u/=10)a++;N?a>n.maxE?(f.e=NaN,f.d=null):a<n.minE?(f.e=0,f.d=[0]):(f.e=a,f.d=[r]):(f.e=a,f.d=[r]);return}if(r*0!==0){r||(f.s=NaN),f.e=NaN,f.d=null;return}return ei(f,r.toString())}if(d==="string")return(u=r.charCodeAt(0))===45?(r=r.slice(1),f.s=-1):(u===43&&(r=r.slice(1)),f.s=1),wo.test(r)?ei(f,r):Br(f,r);if(d==="bigint")return r<0?(r=-r,f.s=-1):f.s=1,ei(f,r.toString());throw Error(ue+r)}if(n.prototype=k,n.ROUND_UP=0,n.ROUND_DOWN=1,n.ROUND_CEIL=2,n.ROUND_FLOOR=3,n.ROUND_HALF_UP=4,n.ROUND_HALF_DOWN=5,n.ROUND_HALF_EVEN=6,n.ROUND_HALF_CEIL=7,n.ROUND_HALF_FLOOR=8,n.EUCLID=9,n.config=n.set=Gr,n.clone=ko,n.isDecimal=ho,n.abs=Nr,n.acos=jr,n.acosh=qr,n.add=Dr,n.asin=Lr,n.asinh=$r,n.atan=Ur,n.atanh=Kr,n.atan2=Wr,n.cbrt=Hr,n.ceil=Zr,n.clamp=zr,n.cos=Vr,n.cosh=Qr,n.div=Jr,n.exp=Xr,n.floor=Yr,n.hypot=ts,n.ln=es,n.log=is,n.log10=os,n.log2=ns,n.max=rs,n.min=ss,n.mod=as,n.mul=cs,n.pow=ls,n.random=us,n.round=ds,n.sign=fs,n.sin=_s,n.sinh=hs,n.sqrt=ps,n.sub=ms,n.sum=gs,n.tan=bs,n.tanh=ws,n.trunc=ys,o===void 0&&(o={}),o&&o.defaults!==!0)for(i=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],t=0;t<i.length;)o.hasOwnProperty(e=i[t++])||(o[e]=this[e]);return n.config(o),n}function Jr(o,t){return new this(o).div(t)}function Xr(o){return new this(o).exp()}function Yr(o){return O(o=new this(o),o.e+1,3)}function ts(){var o,t,e=new this(0);for(N=!1,o=0;o<arguments.length;)if(t=new this(arguments[o++]),t.d)e.d&&(e=e.plus(t.times(t)));else{if(t.s)return N=!0,new this(1/0);e=t}return N=!0,e.sqrt()}function ho(o){return o instanceof Le||o&&o.toStringTag===bo||!1}function es(o){return new this(o).ln()}function is(o,t){return new this(o).log(t)}function ns(o){return new this(o).log(2)}function os(o){return new this(o).log(10)}function rs(){return Po(this,arguments,-1)}function ss(){return Po(this,arguments,1)}function as(o,t){return new this(o).mod(t)}function cs(o,t){return new this(o).mul(t)}function ls(o,t){return new this(o).pow(t)}function us(o){var t,e,i,n,r=0,a=new this(1),u=[];if(o===void 0?o=this.precision:Nt(o,1,de),i=Math.ceil(o/F),this.crypto)if(crypto.getRandomValues)for(t=crypto.getRandomValues(new Uint32Array(i));r<i;)n=t[r],n>=429e7?t[r]=crypto.getRandomValues(new Uint32Array(1))[0]:u[r++]=n%1e7;else if(crypto.randomBytes){for(t=crypto.randomBytes(i*=4);r<i;)n=t[r]+(t[r+1]<<8)+(t[r+2]<<16)+((t[r+3]&127)<<24),n>=214e7?crypto.randomBytes(4).copy(t,r):(u.push(n%1e7),r+=4);r=i/4}else throw Error(go);else for(;r<i;)u[r++]=Math.random()*1e7|0;for(i=u[--r],o%=F,i&&o&&(n=It(10,F-o),u[r]=(i/n|0)*n);u[r]===0;r--)u.pop();if(r<0)e=0,u=[0];else{for(e=-1;u[0]===0;e-=F)u.shift();for(i=1,n=u[0];n>=10;n/=10)i++;i<F&&(e-=F-i)}return a.e=e,a.d=u,a}function ds(o){return O(o=new this(o),o.e+1,this.rounding)}function fs(o){return o=new this(o),o.d?o.d[0]?o.s:0*o.s:o.s||NaN}function _s(o){return new this(o).sin()}function hs(o){return new this(o).sinh()}function ps(o){return new this(o).sqrt()}function ms(o,t){return new this(o).sub(t)}function gs(){var o=0,t=arguments,e=new this(t[o]);for(N=!1;e.s&&++o<t.length;)e=e.plus(t[o]);return N=!0,O(e,this.precision,this.rounding)}function bs(o){return new this(o).tan()}function ws(o){return new this(o).tanh()}function ys(o){return O(o=new this(o),o.e+1,1)}k[Symbol.for("nodejs.util.inspect.custom")]=k.toString;k[Symbol.toStringTag]="Decimal";var Le=k.constructor=ko(nn);ii=new Le(ii);ni=new Le(ni);var I=Le;function Me(o){let t=qt.isSuiCoin(o.coin_type_a),e=qt.isSuiCoin(o.coin_type_b);return{is_adjust_coin_a:t,is_adjust_coin_b:e}}function Cs(o,t){return I.ceil(I(o).div(1+t)).toString()}var $t=class{static createCollectRewarderAndFeeParams(t,e,i,n,r,a){r===void 0&&(r=[...n]),a===void 0&&(a=[...n]);let u=an(i.coin_type_a),d=an(i.coin_type_b);if(i.collect_fee){let h=qt.buildCoinForAmount(e,r,BigInt(0),u,!1);r=h.remain_coins;let w=qt.buildCoinForAmount(e,a,BigInt(0),d,!1);a=w.remain_coins,e=t.Position.createCollectFeePayload({pool_id:i.pool_id,pos_id:i.pos_id,coin_type_a:i.coin_type_a,coin_type_b:i.coin_type_b},e,h.target_coin,w.target_coin)}let f=[];return i.rewarder_coin_types.forEach(h=>{switch(an(h)){case u:f.push(qt.buildCoinForAmount(e,r,BigInt(0),h,!1).target_coin);break;case d:f.push(qt.buildCoinForAmount(e,a,BigInt(0),h,!1).target_coin);break;default:f.push(qt.buildCoinForAmount(e,n,BigInt(0),h,!1).target_coin);break}}),e=t.Rewarder.createCollectRewarderPayload(i,e,f),e}static async adjustTransactionForGas(t,e,i,n){n.setSender(t.getSenderAddress());let r=qt.selectCoinAssetGreaterThanOrEqual(e,i).selected_coins,a=qt.calculateTotalBalance(e);if(r.length===0&&E("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let u=await t.FullClient.calculationTxGas(n);if(qt.selectCoinAssetGreaterThanOrEqual(e,BigInt(u),r.map(f=>f.coin_object_id)).selected_coins.length===0){let f=BigInt(u)+BigInt(500);if(a-i<f){i-=f,i<0&&E("InsufficientBalance","gas Insufficient balance");let h=new Ao;return{fixAmount:i,newTx:h}}}return{fixAmount:i}}static async buildAddLiquidityFixTokenForGas(t,e,i,n,r,a,u){r=await this.buildAddLiquidityFixToken(t,e,i,r,a,u);let{is_adjust_coin_a:d}=Me(i),f=d?i.amount_a:i.amount_b,h=await this.adjustTransactionForGas(t,qt.getCoinAssets(d?i.coin_type_a:i.coin_type_b,e),BigInt(f),r),{fixAmount:w}=h,{newTx:P}=h;if(P!=null){let M,T;if(d)i.amount_a=Number(w),M=this.buildAddLiquidityFixTokenCoinInput(P,!i.fix_amount_a,w.toString(),i.slippage,i.coin_type_a,e,!1,!0),T=this.buildAddLiquidityFixTokenCoinInput(P,i.fix_amount_a,i.amount_b,i.slippage,i.coin_type_b,e,!1,!0);else return i.amount_b=Number(w),M=this.buildAddLiquidityFixTokenCoinInput(P,!i.fix_amount_a,i.amount_a,i.slippage,i.coin_type_a,e,!1,!0),T=this.buildAddLiquidityFixTokenCoinInput(P,i.fix_amount_a,w.toString(),i.slippage,i.coin_type_b,e,!1,!0),i=this.fixAddLiquidityFixTokenParams(i,n.slippage,n.cur_sqrt_price),r=await this.buildAddLiquidityFixTokenArgs(P,t,e,i,M,T),r}return r}static async buildAddLiquidityFixToken(t,e,i,n,r,a){n=n||new Ao;let u,d;return r==null||a==null?(u=this.buildAddLiquidityFixTokenCoinInput(n,!i.fix_amount_a,i.amount_a,i.slippage,i.coin_type_a,e,!1,!0),d=this.buildAddLiquidityFixTokenCoinInput(n,i.fix_amount_a,i.amount_b,i.slippage,i.coin_type_b,e,!1,!0)):(u={target_coin:r,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},d={target_coin:a,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]}),n=this.buildAddLiquidityFixTokenArgs(n,t,e,i,u,d),n}static buildAddLiquidityFixTokenCoinInput(t,e,i,n,r,a,u=!0,d=!0){return e?qt.buildCoinForAmountInterval(t,a,{amount_second:BigInt(Cs(i,n)),amount_first:BigInt(i)},r,u,d):qt.buildCoinForAmount(t,a,BigInt(i),r,u,d)}static fixAddLiquidityFixTokenParams(t,e,i){let n=t.fix_amount_a?t.amount_a:t.amount_b,r=vs.estLiquidityAndCoinAmountFromOneAmounts(Number(t.tick_lower),Number(t.tick_upper),new So.default(n),t.fix_amount_a,!0,e,i);return t.amount_a=t.fix_amount_a?t.amount_a:r.coin_amount_limit_a,t.amount_b=t.fix_amount_a?r.coin_amount_limit_b:t.amount_b,t}static buildAddLiquidityFixTokenArgs(t,e,i,n,r,a){let u=[n.coin_type_a,n.coin_type_b],d=n.is_open?"open_position_with_liquidity_by_fix_coin":"add_liquidity_by_fix_coin",{clmm_pool:f,integrate:h}=e.sdkOptions;n.is_open||(t=this.createCollectRewarderAndFeeParams(e,t,n,i,r.remain_coins,a.remain_coins));let w=Ps(f),P=n.is_open?[t.object(w.global_config_id),t.object(n.pool_id),t.pure.u32(Number(To(BigInt(n.tick_lower)).toString())),t.pure.u32(Number(To(BigInt(n.tick_upper)).toString())),r.target_coin,a.target_coin,t.pure.u64(n.amount_a),t.pure.u64(n.amount_b),t.pure.bool(n.fix_amount_a),t.object(xo)]:[t.object(w.global_config_id),t.object(n.pool_id),t.object(n.pos_id),r.target_coin,a.target_coin,t.pure.u64(n.amount_a),t.pure.u64(n.amount_b),t.pure.bool(n.fix_amount_a),t.object(xo)];return t.moveCall({target:`${h.published_at}::${Vt}::${d}`,typeArguments:u,arguments:P}),t}static checkCoinThreshold(t,e,i,n,r,a){e&&i.moveCall({target:`${t.sdkOptions.integrate.published_at}::${Ye}::check_coin_threshold`,typeArguments:[a],arguments:[n,i.pure.u64(r)]})}};var Yt=class o{static getDefaultSqrtPriceLimit(t){return new Ro.default(t?As:ks)}static getDefaultOtherAmountThreshold(t){return t?xs:Ts}static async buildSwapTransactionForGas(t,e,i,n){let r=this.buildSwapTransaction(t,e,i);r.setSender(t.getSenderAddress());let a=await this.adjustTransactionForGas(t,Dt.getCoinAssets(e.a2b?e.coin_type_a:e.coin_type_b,i),BigInt(e.by_amount_in?e.amount:e.amount_limit),r),{fixAmount:u,newTx:d}=a;if(d!==void 0){d.setSender(t.getSenderAddress()),e.by_amount_in?e.amount=u.toString():e.amount_limit=u.toString(),e=await this.fixSwapParams(t,e,n);let f=Dt.buildCoinForAmount(r,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a),h=Dt.buildCoinForAmount(r,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b);r=this.buildSwapTransactionArgs(d,e,t.sdkOptions,f,h)}return r}static buildSwapTransactionArgs(t,e,i,n,r){let{clmm_pool:a,integrate:u}=i,d=o.getDefaultSqrtPriceLimit(e.a2b),f=[e.coin_type_a,e.coin_type_b],{global_config_id:h}=Io(a);h===void 0&&E("InvalidConfig","clmm.config.global_config_id is undefined");let w=e.swap_partner!==void 0,P=w?e.a2b?"swap_a2b_with_partner":"swap_b2a_with_partner":e.a2b?"swap_a2b":"swap_b2a",M=w?[t.object(h),t.object(e.pool_id),t.object(e.swap_partner),n.target_coin,r.target_coin,t.pure.bool(e.by_amount_in),t.pure.u64(e.amount),t.pure.u64(e.amount_limit),t.pure.u128(d.toString()),t.object(ci)]:[t.object(h),t.object(e.pool_id),n.target_coin,r.target_coin,t.pure.bool(e.by_amount_in),t.pure.u64(e.amount),t.pure.u64(e.amount_limit),t.pure.u128(d.toString()),t.object(ci)];return t.moveCall({target:`${u.published_at}::${Vt}::${P}`,typeArguments:f,arguments:M}),t}static async adjustTransactionForGas(t,e,i,n){n.setSender(t.getSenderAddress());let r=Dt.selectCoinAssetGreaterThanOrEqual(e,i).selected_coins,a=Dt.calculateTotalBalance(e);if(r.length===0&&E("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let u=await t.FullClient.calculationTxGas(n);if(Dt.selectCoinAssetGreaterThanOrEqual(e,BigInt(u),r.map(f=>f.coin_object_id)).selected_coins.length===0){let f=BigInt(u)+BigInt(500);if(a-i<f){i-=f,i<0&&E("InsufficientBalance","gas Insufficient balance");let h=new cn;return{fixAmount:i,newTx:h}}}return{fixAmount:i}}static buildSwapTransaction(t,e,i){let n=new cn;n.setSender(t.getSenderAddress());let r=Dt.buildCoinForAmount(n,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1),a=Dt.buildCoinForAmount(n,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1);return n=this.buildSwapTransactionArgs(n,e,t.sdkOptions,r,a),n}static async fixSwapParams(t,e,i){let{current_pool:n}=i;try{let r=await t.Swap.preSwap({decimals_a:i.decimals_a,decimals_b:i.decimals_b,a2b:e.a2b,by_amount_in:e.by_amount_in,amount:e.amount,pool:n,current_sqrt_price:n.current_sqrt_price,coin_type_a:n.coin_type_a,coin_type_b:n.coin_type_b}),a=i.by_amount_in?r.estimated_amount_out:r.estimated_amount_in,u=Ms(a,i.slippage,!i.by_amount_in);e.amount_limit=u.toString()}catch(r){ve("InvalidConfig",r)}return e}static async buildSwapTransactionWithoutTransferCoinsForGas(t,e,i,n){let{tx:r,coin_ab_s:a}=o.buildSwapTransactionWithoutTransferCoins(t,e,i);r.setSender(t.getSenderAddress());let u=await o.adjustTransactionForGas(t,Dt.getCoinAssets(e.a2b?e.coin_type_a:e.coin_type_b,i),BigInt(e.by_amount_in?e.amount:e.amount_limit),r),{fixAmount:d,newTx:f}=u;if(f!==void 0){f.setSender(t.getSenderAddress()),e.by_amount_in?e.amount=d.toString():e.amount_limit=d.toString(),e=await o.fixSwapParams(t,e,n);let h=Dt.buildCoinForAmount(r,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1,!0),w=Dt.buildCoinForAmount(r,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1,!0),P=o.buildSwapTransactionWithoutTransferCoinArgs(t,f,e,t.sdkOptions,h,w);r=P.tx,a=P.txRes}return{tx:r,coin_ab_s:a}}static buildSwapTransactionWithoutTransferCoins(t,e,i){let n=new cn;n.setSender(t.getSenderAddress());let r=Dt.buildCoinForAmount(n,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1,!0),a=Dt.buildCoinForAmount(n,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1,!0),u=o.buildSwapTransactionWithoutTransferCoinArgs(t,n,e,t.sdkOptions,r,a);return{tx:u.tx,coin_ab_s:u.txRes}}static buildSwapTransactionWithoutTransferCoinArgs(t,e,i,n,r,a){let{clmm_pool:u,integrate:d}=n,f=o.getDefaultSqrtPriceLimit(i.a2b),{global_config_id:h}=Io(u);h===void 0&&E("InvalidConfig","clmm.config.global_config_id is undefined");let w=i.swap_partner!==void 0,P=w?"swap_with_partner":"swap",M=w?ao:Ye,T=w?[e.object(h),e.object(i.pool_id),e.object(i.swap_partner),r.target_coin,a.target_coin,e.pure.bool(i.a2b),e.pure.bool(i.by_amount_in),e.pure.u64(i.amount),e.pure.u128(f.toString()),e.pure.bool(!1),e.object(ci)]:[e.object(h),e.object(i.pool_id),r.target_coin,a.target_coin,e.pure.bool(i.a2b),e.pure.bool(i.by_amount_in),e.pure.u64(i.amount),e.pure.u128(f.toString()),e.pure.bool(!1),e.object(ci)],x=[i.coin_type_a,i.coin_type_b],$=e.moveCall({target:`${d.published_at}::${M}::${P}`,typeArguments:x,arguments:T});if(i.by_amount_in){let j=i.a2b?i.coin_type_b:i.coin_type_a,K=i.a2b?$[1]:$[0],B=Number(i.amount_limit);$t.checkCoinThreshold(t,i.by_amount_in,e,K,B,j)}return{tx:e,txRes:$}}};function Ka(o,t,e){let i=t.mul(e).shln(64),n=t.shln(64).add(o.mul(e));return ln.divRoundUp(i,n)}function Wa(o,t,e){let i=t.mul(e).shln(64),n=t.shln(64).sub(o.mul(e));return ln.divRoundUp(i,n)}function Ha(o,t,e){return e.sub(ln.divRoundUp(o.shln(64),t))}function Za(o,t,e){return e.add(o.shln(64).div(t))}function Ja(o){let t=new Bt.default(o);return t.lt(Ut)?{bits:t.neg().xor(new Bt.default(2).pow(new Bt.default(64)).sub(new Bt.default(1))).add(new Bt.default(1)).toString()}:{bits:t.toString()}}function Eo(o){return{coin_type_a:o.coin_type_a,coin_type_b:o.coin_type_b,current_sqrt_price:new Bt.default(o.current_sqrt_price),current_tick_index:o.current_tick_index,fee_growth_global_a:new Bt.default(o.fee_growth_global_a),fee_growth_global_b:new Bt.default(o.fee_growth_global_b),fee_protocol_coin_a:new Bt.default(o.fee_protocol_coin_a),fee_protocol_coin_b:new Bt.default(o.fee_protocol_coin_b),fee_rate:new Bt.default(o.fee_rate),liquidity:new Bt.default(o.liquidity),tick_indexes:[],tick_spacing:Number(o.tick_spacing),ticks:[],collection_name:""}}function Rs(o,t,e,i,n,r){if(e===Ut)return{amount_in:Ut,amount_out:Ut,next_sqrt_price:t,fee_amount:Ut};let a=o.gte(t),u,d,f,h;if(r){let w=_e.checkMulDivFloor(i,_e.checkUnsignedSub(li,n),li,64),P=Bo(o,t,e,a);P.gt(w)?(u=w,h=_e.checkUnsignedSub(i,w),f=Ss(o,e,w,a)):(u=P,h=_e.checkMulDivCeil(u,n,li.sub(n),64),f=t),d=Fo(o,f,e,a)}else{let w=Fo(o,t,e,a);w.gt(i)?(d=i,f=Is(o,e,i,a)):(d=w,f=t),u=Bo(o,f,e,a),h=_e.checkMulDivCeil(u,n,li.sub(n),64)}return{amount_in:u,amount_out:d,next_sqrt_price:f,fee_amount:h}}function No(o,t,e,i,n){let r=e,a=i.liquidity,{current_sqrt_price:u}=i,d={amount_in:Ut,amount_out:Ut,fee_amount:Ut,ref_amount:Ut,next_sqrt_price:Ut,cross_tick_num:0},f,h,w=Yt.getDefaultSqrtPriceLimit(o);for(let P of n){if(o&&i.current_tick_index<P.index||!o&&i.current_tick_index>=P.index||P===null)continue;o&&w.gt(P.sqrt_price)||!o&&w.lt(P.sqrt_price)?f=w:f=P.sqrt_price;let M=Rs(u,f,a,r,i.fee_rate,t);if(M.amount_in.eq(Ut)||(r=t?r.sub(M.amount_in.add(M.fee_amount)):r.sub(M.amount_out)),d.amount_in=d.amount_in.add(M.amount_in),d.amount_out=d.amount_out.add(M.amount_out),d.fee_amount=d.fee_amount.add(M.fee_amount),M.next_sqrt_price.eq(P.sqrt_price)?(h=P.liquidity_net.mul(new Bt.default(-1)),o?_e.is_neg(h)?a=a.add(new Bt.default(Oo(BigInt(h.toString()),128))):a=a.add(h):_e.is_neg(h)?a=a.sub(new Bt.default(Oo(BigInt(h.toString()),128))):a=a.sub(h),u=P.sqrt_price):u=M.next_sqrt_price,d.cross_tick_num+=1,r.eq(Ut))break}return d.amount_in=d.amount_in.add(d.fee_amount),d.next_sqrt_price=u,d}function js(o,t,e){let i=ke(o).name,n=ke(t).name;return`${i}-${n}[${e}]`}function di(o){let t=jo(o),e=ke(t),i=dn(o);i==null&&E("InvalidPoolObject",`Pool id ${un(o)} not exists.`,{[ui.METHOD_NAME]:"buildPool"});let n=[];i.rewarder_manager.fields.rewarders.forEach(a=>{let{emissions_per_second:u}=a.fields,d=Ns.fromX64(new Kt.default(u)),f=Math.floor(d.toNumber()*60*60*24);n.push({emissions_per_second:u,coin_type:ke(a.fields.reward_coin.fields.name).source_address,growth_global:a.fields.growth_global,emissions_every_day:f})});let r={id:un(o),pool_type:t,coin_type_a:e.type_arguments[0],coin_type_b:e.type_arguments[1],coin_amount_a:i.coin_a,coin_amount_b:i.coin_b,current_sqrt_price:i.current_sqrt_price,current_tick_index:he(BigInt(i.current_tick_index.fields.bits)),fee_growth_global_a:i.fee_growth_global_a,fee_growth_global_b:i.fee_growth_global_b,fee_protocol_coin_a:i.fee_protocol_coin_a,fee_protocol_coin_b:i.fee_protocol_coin_b,fee_rate:i.fee_rate,is_pause:i.is_pause,liquidity:i.liquidity,position_manager:{positions_handle:i.position_manager.fields.positions.fields.id.id,size:i.position_manager.fields.positions.fields.size},rewarder_infos:n,rewarder_last_updated_time:i.rewarder_manager.fields.last_updated_time,tick_spacing:i.tick_spacing,ticks_handle:i.tick_manager.fields.ticks.fields.id.id,uri:i.url,index:Number(i.index),name:""};return r.name=js(r.coin_type_a,r.coin_type_b,r.tick_spacing),r}function fi(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&E("InvalidPositionObject",`Position not exists. Get Position error:${o.error}`,{[ui.METHOD_NAME]:"buildPosition"});let t={creator:"",description:"",image_url:"",link:"",name:"",project_url:""},e={...t,pos_object_id:"",owner:"",type:"",coin_type_a:"",coin_type_b:"",liquidity:"",tick_lower_index:0,tick_upper_index:0,index:0,pool:"",reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0",fee_growth_inside_a:"0",fee_owned_a:"0",fee_growth_inside_b:"0",fee_owned_b:"0",position_status:"Exists"},i=dn(o);if(i){let a=jo(o),u=Es(o);"nft"in i?(i=i.nft.fields,t.description=i.description,t.name=i.name,t.link=i.url):t=Os(o),e={...t,pos_object_id:i.id.id,owner:u.AddressOwner,type:a,liquidity:i.liquidity,coin_type_a:i.coin_type_a.fields.name,coin_type_b:i.coin_type_b.fields.name,tick_lower_index:he(BigInt(i.tick_lower_index.fields.bits)),tick_upper_index:he(BigInt(i.tick_upper_index.fields.bits)),index:i.index,pool:i.pool,reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0",fee_growth_inside_a:"0",fee_owned_a:"0",fee_growth_inside_b:"0",fee_owned_b:"0",position_status:"Exists"}}let n=Fs(o);n&&(e.pos_object_id=n.objectId,e.position_status="Deleted");let r=Bs(o);return r&&(e.pos_object_id=r,e.position_status="NotExists"),e}function _i(o){let t={reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0"};o="fields"in o?o.fields:o,o.rewards.forEach((r,a)=>{let{amount_owned:u,growth_inside:d}="fields"in r?r.fields:r;a===0?(t.reward_amount_owned_0=u,t.reward_growth_inside_0=d):a===1?(t.reward_amount_owned_1=u,t.reward_growth_inside_1=d):a===2&&(t.reward_amount_owned_2=u,t.reward_growth_inside_2=d)});let e="fields"in o.tick_lower_index?o.tick_lower_index.fields.bits:o.tick_lower_index.bits,i="fields"in o.tick_upper_index?o.tick_upper_index.fields.bits:o.tick_upper_index.bits;return{liquidity:o.liquidity,tick_lower_index:he(BigInt(e)),tick_upper_index:he(BigInt(i)),...t,fee_growth_inside_a:o.fee_growth_inside_a,fee_owned_a:o.fee_owned_a,fee_growth_inside_b:o.fee_growth_inside_b,fee_owned_b:o.fee_owned_b,pos_object_id:o.position_id}}function hi(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&E("InvalidTickObject",`Tick not exists. Get tick data error:${o.error}`,{[ui.METHOD_NAME]:"buildTickData"});let e=dn(o).value.fields.value.fields;return{object_id:un(o),index:he(BigInt(e.index.fields.bits)),sqrt_price:new Kt.default(e.sqrt_price),liquidity_net:new Kt.default(e.liquidity_net.fields.bits),liquidity_gross:new Kt.default(e.liquidity_gross),fee_growth_outside_a:new Kt.default(e.fee_growth_outside_a),fee_growth_outside_b:new Kt.default(e.fee_growth_outside_b),rewarders_growth_outside:e.rewards_growth_outside}}function qo(o){(!o||!o.index||!o.sqrt_price||!o.liquidity_net||!o.liquidity_gross||!o.fee_growth_outside_a||!o.fee_growth_outside_b)&&E("InvalidTickFields","Invalid tick fields.",{[ui.METHOD_NAME]:"buildTickDataByEvent"});let t=he(BigInt(o.index.bits)),e=new Kt.default(o.sqrt_price),i=new Kt.default(o.liquidity_net.bits),n=new Kt.default(o.liquidity_gross),r=new Kt.default(o.fee_growth_outside_a),a=new Kt.default(o.fee_growth_outside_b),u=o.rewards_growth_outside||[];return{object_id:"",index:t,sqrt_price:e,liquidity_net:i,liquidity_gross:n,fee_growth_outside_a:r,fee_growth_outside_b:a,rewarders_growth_outside:u}}function ac(o,t){return`Cetus LP | Pool${o}-${t}`}function Do(o,t,e){let i=[],{timestampMs:n,events:r}=o,a=["AddLiquidityEvent","RemoveLiquidityEvent","CollectFeeEvent","CollectRewardEvent","CollectRewardV2Event","HarvestEvent"];return r?.forEach((u,d)=>{let f=ke(u.type).name;if(a.includes(f)){let h={tx_digest:u.id.txDigest,package_id:u.packageId,transaction_module:u.transactionModule,sender:u.sender,type:u.type,timestamp_ms:n||"0",parsed_json:u.parsedJson,index:`${t}_${d}`};switch(f){case"CollectFeeEvent":e.includes(h.parsed_json.position)&&($e(h.parsed_json.amount_a).gt(0)||$e(h.parsed_json.amount_b).gt(0))&&i.push(h);break;case"RemoveLiquidityEvent":case"AddLiquidityEvent":($e(h.parsed_json.amount_a).gt(0)||$e(h.parsed_json.amount_b).gt(0))&&i.push(h);break;case"CollectRewardEvent":case"HarvestEvent":case"CollectRewardV2Event":(e.includes(h.parsed_json.position)||e.includes(h.parsed_json.wrapped_position_id))&&$e(h.parsed_json.amount).gt(0)&&i.push(h);break;default:break}}}),i}function Lo(o,t,e,i){let n=[],{timestampMs:r,events:a}=o;return a?.forEach((u,d)=>{let{name:f,address:h}=ke(u.type);if(uo.includes(f)&&h===e&&i===u.parsedJson.pool){let w={tx:u.id.txDigest,sender:u.sender,type:u.type,block_time:r||"0",index:`${t}_${d}`,parsed_json:u.parsedJson};n.push(w)}}),n}function pi(o,t,e,i){t.moveCall({target:`${o.sdkOptions.integrate.published_at}::${co}::transfer_coin_to_sender`,typeArguments:[i],arguments:[e]})}function $o(o,t,e,i,n){n!=null?t.transferObjects([e],t.pure.address(n)):pi(o,t,e,i)}import{asUintN as Te,CACHE_TIME_24H as mi,ClmmPoolUtil as Ds,CLOCK_ADDRESS as Uo,CoinAssist as gi,createFullClient as Ls,d as xe,DETAILS_KEYS as V,extractStructTagFromType as Se,getObjectPreviousTransactionDigest as $s,getPackagerConfigs as fn,isSortedSymbols as Ko,TickMath as _n,tickScore as Us,TickUtil as Wo}from"@cetusprotocol/common-sdk";var bi=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}async getPositionList(t,e="all"){let i={data:[],has_next_page:!0},n=await this._sdk.FullClient.getDynamicFieldsByPage(t,e);i.has_next_page=n.has_next_page,i.next_cursor=n.next_cursor;let r=n.data.map(u=>(u.error!=null&&E("InvalidConfig",`when getPositionList get position objects error: ${u.error}, please check the rpc, contracts address config and position id.`,{[V.METHOD_NAME]:"getPositionList"}),u.name.value)),a=await this._sdk.Position.getSimplePositionList(r);return i.data=a,i}async getPoolImmutablesWithPage(t="all",e=!1){let{package_id:i}=this._sdk.sdkOptions.clmm_pool,n=[],r={data:[],has_next_page:!1},a=t==="all",u=`${i}_getPoolImmutables`;if(a){let d=this._sdk.getCache(u,e);d&&n.push(...d)}if(n.length===0)try{let d=`${i}::factory::CreatePoolEvent`,f=await this._sdk.FullClient.queryEventsByPage({MoveEventType:d},t);r.has_next_page=f.has_next_page,r.next_cursor=f.next_cursor,f.data.forEach(h=>{let w=h.parsedJson;w&&n.push({id:w.pool_id,tick_spacing:w.tick_spacing,coin_type_a:Se(w.coin_type_a).full_address,coin_type_b:Se(w.coin_type_b).full_address})})}catch(d){console.log("getPoolImmutables",d)}return r.data=n,a&&this._sdk.updateCache(`${i}_getPoolImmutables`,n,mi),r}async getPoolsWithPage(t="all",e=!1){let i={data:[],has_next_page:!1},n=await this.getPoolImmutablesWithPage(t,e),r=await this._sdk.FullClient.batchGetObjects(n.data.map(a=>a.id),{showContent:!0,showType:!0});for(let a of r){(a.error!=null||a.data?.content?.dataType!=="moveObject")&&E("InvalidPoolObject",`getPoolWithPages error code: ${a.error?.code??"unknown error"}, please check config and object ids`,{[V.METHOD_NAME]:"getPoolsWithPage"});let u=di(a);i.data.push(u);let d=`${u.id}_getPoolObject`;this._sdk.updateCache(d,u,mi)}return i.has_next_page=n.has_next_page,i.next_cursor=n.next_cursor,i}async getAssignPools(t){if(t.length===0)return[];let e=[],i=await this._sdk.FullClient.batchGetObjects(t,{showContent:!0,showType:!0});for(let n of i){(n.error!=null||n.data?.content?.dataType!=="moveObject")&&E("InvalidPoolObject",`getPools error code: ${n.error?.code??"unknown error"}, please check config and object ids`,{[V.METHOD_NAME]:"getAssignPools"});let r=di(n);e.push(r);let a=`${r.id}_getPoolObject`;this._sdk.updateCache(a,r,mi)}return e}async getPool(t,e=!0){let i=`${t}_getPoolObject`,n=this._sdk.getCache(i,e);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:t,options:{showType:!0,showContent:!0}});(r.error!=null||r.data?.content?.dataType!=="moveObject")&&E("InvalidPoolObject",`getPool error code: ${r.error?.code??"unknown error"}, please check config and object id`,{[V.METHOD_NAME]:"getPool"});let a=di(r);return this._sdk.updateCache(i,a),a}async getPoolByCoins(t,e){if(t.length===0)return[];for(let d=0;d<t.length;d++)t[d]==="0x2::sui::SUI"&&(t[d]="0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI");let i=this._sdk.sdkOptions.stats_pools_url;i||E("StatsPoolsUrlNotSet","statsPoolsUrl is not set in the sdk options.",{[V.METHOD_NAME]:"getPoolByCoins"}),i+=`?order_by=-fees&limit=100&has_mining=true&has_farming=true&no_incentives=true&display_all_pools=true&coin_type=${t.join(",")}`;let n=await fetch(i),r;try{r=await n.json()}catch(d){ve("FetchError",d,{[V.METHOD_NAME]:"getPoolByCoins",[V.REQUEST_PARAMS]:i})}let a=r.data.lp_list,u=[];for(let d of a)t.includes(d.coin_a_address)&&t.includes(d.coin_b_address)&&(e!=null?d.object.feeRate===e&&u.push(d.address):u.push(d.address));return u.length>0?await this.getAssignPools(u):[]}async createPoolPayload(t,e){if(Ko(Ae(t.coin_type_a),Ae(t.coin_type_b))){let i=t.coin_type_b;t.coin_type_b=t.coin_type_a,t.coin_type_a=i;let n=t.metadata_b;t.metadata_b=t.metadata_a,t.metadata_a=n}return await this.createPoolAndAddLiquidity(t,e)}async createPoolRowPayload(t,e){if(Ko(Ae(t.coin_type_a),Ae(t.coin_type_b))){let i=t.coin_type_b;t.coin_type_b=t.coin_type_a,t.coin_type_a=i;let n=t.metadata_b;t.metadata_b=t.metadata_a,t.metadata_a=n}return await this.createPoolAndAddLiquidityRow(t,e)}async getClmmConfigs(t=!1){let{package_id:e}=this._sdk.sdkOptions.clmm_pool,i=`${e}_getInitEvent`,n=this._sdk.getCache(i,t);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:e,options:{showPreviousTransaction:!0}}),a=$s(r),u=(await this._sdk.FullClient.queryEventsByPage({Transaction:a})).data,d={pools_id:"",global_config_id:"",global_vault_id:"",admin_cap_id:""};return u.length>0&&(u.forEach(f=>{let h=f.parsedJson;if(f.type)switch(Se(f.type).full_address){case`${e}::config::InitConfigEvent`:d.global_config_id=h.global_config_id,d.admin_cap_id=h.admin_cap_id;break;case`${e}::factory::InitFactoryEvent`:d.pools_id=h.pools_id;break;case`${e}::rewarder::RewarderInitEvent`:d.global_vault_id=h.global_vault_id;break;case`${e}::partner::InitPartnerEvent`:d.partners_id=h.partners_id;break;default:break}}),this._sdk.updateCache(i,d,mi)),d}async getPoolTransactionList({pool_id:t,pagination_args:e,order:i="descending",full_rpc_url:n}){let{FullClient:r,sdkOptions:a}=this._sdk,u;n?u=Ls(new qs({url:n})):u=r;let d={data:[],has_next_page:!1},f=50,h=e,w=e.limit||10;do{let P=await u.queryTransactionBlocksByPage({ChangedObject:t},{...h,limit:50},i);P.data.forEach((M,T)=>{d.next_cursor=P.next_cursor;let x=Lo(M,T,a.clmm_pool.package_id,t);d.data=[...d.data,...x]}),d.has_next_page=P.has_next_page,d.next_cursor=P.next_cursor,h.cursor=P.next_cursor}while(d.data.length<w&&d.has_next_page);return d.data.length>w&&(d.data=d.data.slice(0,w),d.next_cursor=d.data[d.data.length-1].tx),d}async calculateCreatePoolWithPrice(t){let{current_price:e,slippage:i,tick_spacing:n,add_mode_params:r,price_base_coin:a,coin_decimals_a:u,coin_decimals_b:d}=t,f=_n.priceToSqrtPriceX64(a==="coin_a"?xe(e):xe(1).div(e),u,d),h=0,w=0;if(r.is_full_range)h=Wo.getMinIndex(n),w=Wo.getMaxIndex(n);else{let{min_price:B,max_price:D}=r;h=_n.priceToInitializeTickIndex(a==="coin_a"?xe(B):xe(1).div(D),u,d,n),w=_n.priceToInitializeTickIndex(a==="coin_a"?xe(D):xe(1).div(B),u,d,n)}let{coin_amount:P,fix_amount_a:M}=t,{coin_amount_limit_a:T,coin_amount_limit_b:x,liquidity_amount:$,coin_amount_a:j,coin_amount_b:K}=Ds.estLiquidityAndCoinAmountFromOneAmounts(h,w,new Ho.default(P),M,!0,i,f);return{coin_amount_a:j,coin_amount_b:K,coin_amount_limit_a:T,coin_amount_limit_b:x,liquidity:$,initialize_sqrt_price:f.toString(),tick_lower:h,tick_upper:w,fix_amount_a:M}}async createPoolAndAddLiquidity(t,e){e=e||new Ue,e.setSender(this.sdk.getSenderAddress());let{integrate:i,clmm_pool:n}=this.sdk.sdkOptions,r=fn(n),a=r.global_config_id,u=r.pools_id,d=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress()),f=gi.buildCoinForAmount(e,d,BigInt(t.amount_a),t.coin_type_a,!1,!0),h=gi.buildCoinForAmount(e,d,BigInt(t.amount_b),t.coin_type_b,!1,!0),w=[e.object(a),e.object(u),e.pure.u32(t.tick_spacing),e.pure.u128(t.initialize_sqrt_price),e.pure.string(t.uri),e.pure.u32(Number(Te(BigInt(t.tick_lower)).toString())),e.pure.u32(Number(Te(BigInt(t.tick_upper)).toString())),f.target_coin,h.target_coin,e.object(t.metadata_a),e.object(t.metadata_b),e.pure.bool(t.fix_amount_a),e.object(Uo)];return e.moveCall({target:`${i.published_at}::pool_creator_v2::create_pool_v2`,typeArguments:[t.coin_type_a,t.coin_type_b],arguments:w}),pi(this._sdk,e,f.target_coin,t.coin_type_a),pi(this._sdk,e,h.target_coin,t.coin_type_b),e}async createPoolAndAddLiquidityRow(t,e){e=e||new Ue;let{clmm_pool:i}=this.sdk.sdkOptions,n=fn(i),r=n.global_config_id,a=n.pools_id,u=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress()),d=gi.buildCoinForAmount(e,u,BigInt(t.amount_a),t.coin_type_a,!1,!0),f=gi.buildCoinForAmount(e,u,BigInt(t.amount_b),t.coin_type_b,!1,!0),h=[e.object(r),e.object(a),e.pure.u32(t.tick_spacing),e.pure.u128(t.initialize_sqrt_price),e.pure.string(t.uri),e.pure.u32(Number(Te(BigInt(t.tick_lower)).toString())),e.pure.u32(Number(Te(BigInt(t.tick_upper)).toString())),d.target_coin,f.target_coin,e.object(t.metadata_a),e.object(t.metadata_b),e.pure.bool(t.fix_amount_a),e.object(Uo)],w=e.moveCall({target:`${i.published_at}::pool_creator::create_pool_v2`,typeArguments:[t.coin_type_a,t.coin_type_b],arguments:h});return{tx:e,pos_id:w[0],remain_coin_a:w[1],remain_coin_b:w[2],remain_coin_type_a:t.coin_type_a,remain_coin_type_b:t.coin_type_b}}async createPoolWithPriceReturnPositionPayload(t,e){let{coin_type_a:i,coin_type_b:n,tick_spacing:r,uri:a,calculate_result:u}=t,{initialize_sqrt_price:d,tick_lower:f,tick_upper:h,liquidity:w,coin_amount_a:P,coin_amount_b:M,fix_amount_a:T,coin_amount_limit_a:x,coin_amount_limit_b:$}=u,j=await this._sdk.FullClient.fetchCoinMetadata(i),K=await this._sdk.FullClient.fetchCoinMetadata(n);if(j===null)return E("FetchError",`fetch coin ${i} metadata failed`,{[V.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[V.REQUEST_PARAMS]:t});if(K===null)return E("FetchError",`fetch coin ${n} metadata failed`,{[V.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[V.REQUEST_PARAMS]:t});let B=j.id,D=K.id;return this.createPoolRowPayload({tick_spacing:r,initialize_sqrt_price:d,uri:a||"",coin_type_a:i,coin_type_b:n,amount_a:T?P:x,amount_b:T?$:M,fix_amount_a:T,tick_lower:f,tick_upper:h,metadata_a:B,metadata_b:D},e)}async createPoolWithPricePayload(t){let{coin_type_a:e,coin_type_b:i,tick_spacing:n,uri:r,calculate_result:a}=t,{initialize_sqrt_price:u,tick_lower:d,tick_upper:f,liquidity:h,coin_amount_a:w,coin_amount_b:P,fix_amount_a:M,coin_amount_limit_a:T,coin_amount_limit_b:x}=a,$=await this._sdk.FullClient.fetchCoinMetadata(e),j=await this._sdk.FullClient.fetchCoinMetadata(i);if($===null)return E("FetchError",`fetch coin ${e} metadata failed`,{[V.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[V.REQUEST_PARAMS]:t});if(j===null)return E("FetchError",`fetch coin ${i} metadata failed`,{[V.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[V.REQUEST_PARAMS]:t});let K=$.id,B=j.id;return this.createPoolPayload({tick_spacing:n,initialize_sqrt_price:u,uri:r||"",coin_type_a:e,coin_type_b:i,amount_a:M?w:T,amount_b:M?x:P,fix_amount_a:M,tick_lower:d,tick_upper:f,metadata_a:K,metadata_b:B})}async fetchTicks(t){let e=[],i=[];for(;;){let r=await this.getTicks({pool_id:t.pool_id,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b,start:i,limit:512});if(e=[...e,...r],r.length<512)break;i=[Number(Te(BigInt(r[r.length-1].index)))]}return e}async getTicks(t){let{integrate:e}=this.sdk.sdkOptions,i=[],n=[t.coin_type_a,t.coin_type_b],r=new Ue,a=r.makeMoveVec({elements:t.start.map(f=>r.pure.u32(f)),type:"u32"}),u=[r.object(t.pool_id),a,r.pure.u64(t.limit.toString())];r.moveCall({target:`${e.published_at}::${Qt}::fetch_ticks`,arguments:u,typeArguments:n});let d=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:r,sender:Ae("0x0")});return d.error!=null&&E("InvalidTickObjectId",`getTicks error code: ${d.error??"unknown error"}, please check config and tick object ids`,{[V.METHOD_NAME]:"getTicks",[V.REQUEST_PARAMS]:t}),d.events?.forEach(f=>{Se(f.type).name==="FetchTicksResultEvent"&&f.parsedJson.ticks.forEach(h=>{i.push(qo(h))})}),i}async fetchPositionRewardList(t){let{integrate:e}=this.sdk.sdkOptions,i=[],n=[],r=512;for(;;){let a=[t.coin_type_a,t.coin_type_b],u=new Ue,d=u.pure.vector("id",n.map(P=>P)),f=[u.object(t.pool_id),d,u.pure.u64(r)];u.moveCall({target:`${e.published_at}::${Qt}::fetch_positions`,arguments:f,typeArguments:a});let h=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:u,sender:Ae("0x0")});h.error!=null&&E("InvalidPositionRewardObject",`fetch position reward error code: ${h.error??"unknown error"}, please check config and tick object ids`,{[V.METHOD_NAME]:"fetchPositionRewardList",[V.REQUEST_PARAMS]:t});let w=[];if(h?.events?.forEach(P=>{Se(P.type).name==="FetchPositionsEvent"&&P.parsedJson.positions.forEach(M=>{let T=_i(M);w.push(T)})}),i.push(...w),w.length<r)break;n=[w[w.length-1].pos_object_id]}return i}async fetchTicksByRpc(t){let e=[],i=null,n=50;for(;;){let r=[],a=await this.sdk.FullClient.getDynamicFields({parentId:t,cursor:i,limit:n});if(i=a.nextCursor,a.data.forEach(u=>{Se(u.objectType).module==="skip_list"&&r.push(u.objectId)}),e=[...e,...await this.getTicksByRpc(r)],!a.hasNextPage)break}return e}async getTicksByRpc(t){let e=[],i=await this.sdk.FullClient.batchGetObjects(t,{showContent:!0,showType:!0});for(let n of i){(n.error!=null||n.data?.content?.dataType!=="moveObject")&&E("InvalidTickObjectId",`getTicksByRpc error code: ${n.error?.code??"unknown error"}, please check config and tick object ids`,{[V.METHOD_NAME]:"getTicksByRpc"});let r=hi(n);r!=null&&e.push(r)}return e}async getTickDataByIndex(t,e){let i={type:"u64",value:Te(BigInt(Us(e).toString())).toString()},n=await this.sdk.FullClient.getDynamicFieldObject({parentId:t,name:i});return(n.error!=null||n.data?.content?.dataType!=="moveObject")&&E("InvalidTickIndex",`get tick by index: ${e} error: ${n.error}`,{[V.METHOD_NAME]:"getTickDataByIndex"}),hi(n)}async getTickDataByObjectId(t){let e=await this.sdk.FullClient.getObject({id:t,options:{showContent:!0}});return(e.error!=null||e.data?.content?.dataType!=="moveObject")&&E("InvalidTickObjectId",`getTicksByRpc error code: ${e.error?.code??"unknown error"}, please check config and tick object ids`,{[V.METHOD_NAME]:"getTickDataByObjectId"}),hi(e)}async getPartnerRefFeeAmount(t,e=!0){let i=await this._sdk.FullClient.batchGetObjects([t],{showOwner:!0,showContent:!0,showDisplay:e,showType:!0});i[0].data?.content?.dataType!=="moveObject"&&E("NotFoundPartnerObject",`get partner by object id: ${t} error: ${i[0].error}`,{[V.METHOD_NAME]:"getPartnerRefFeeAmount"});let n=i[0].data.content.fields.balances,r=await this._sdk.FullClient.getDynamicFieldsByPage(n.fields.id.id),a=[];r.data.forEach(f=>{f.objectId!=null&&a.push(f.objectId)});let u=[];return(await this._sdk.FullClient.batchGetObjects(a,{showOwner:!0,showContent:!0,showDisplay:e,showType:!0})).forEach(f=>{(f.error!=null||f.data?.content?.dataType!=="moveObject")&&E("InvalidPartnerRefFeeFields",`get coin by object id: ${f.data.objectId} error: ${f.error}`,{[V.METHOD_NAME]:"getPartnerRefFeeAmount"});let h={coin_type:f.data.content.fields.name,coin_object_id:f.data.objectId,balance:BigInt(f.data.content.fields.value)};u.push(h)}),u}async claimPartnerRefFeePayload(t,e,i){let n=new Ue,{clmm_pool:r}=this.sdk.sdkOptions,{global_config_id:a}=fn(r),u=[i],d=[n.object(a),n.object(t),n.object(e)];return n.moveCall({target:`${r.published_at}::${so}::claim_ref_fee`,arguments:d,typeArguments:u}),n}};var Ke=ne(Gt());import{SuiClient as Ks}from"@mysten/sui/client";import{Transaction as re}from"@mysten/sui/transactions";import{isValidSuiObjectId as Ws,normalizeSuiAddress as Hs}from"@mysten/sui/utils";import{asUintN as Ie,ClmmPoolUtil as Zo,CLOCK_ADDRESS as wi,CoinAssist as yi,d as te,DETAILS_KEYS as vi,extractStructTagFromType as Zs,getObjectFields as zs,getPackagerConfigs as se,TickMath as Re,TickUtil as Pi,createFullClient as Gs}from"@cetusprotocol/common-sdk";var Ci=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}buildPositionType(){return`${this._sdk.sdkOptions.clmm_pool.package_id}::position::Position`}async getPositionTransactionList({pos_id:t,origin_pos_id:e,full_rpc_url:i,pagination_args:n="all",order:r="ascending"}){let{FullClient:a}=this._sdk,u=[t];e&&u.push(e);let d;i?d=Gs(new Ks({url:i})):d=a;let f={data:[],has_next_page:!1};try{let h=await d.queryTransactionBlocksByPage({ChangedObject:t},n,r);return h.data.forEach((w,P)=>{let M=Do(w,P,u);f.data=[...f.data,...M]}),f.has_next_page=h.has_next_page,f.next_cursor=h.next_cursor,f}catch(h){ve("FetchError",h,{[vi.METHOD_NAME]:"getPositionTransactionList"})}return f}async getPositionList(t,e=[],i=!0){let n=[],r=await this._sdk.FullClient.getOwnedObjectsByPage(t,{options:{showType:!0,showContent:!0,showDisplay:i,showOwner:!0},filter:{Package:this._sdk.sdkOptions.clmm_pool.package_id}}),a=e.length>0;for(let u of r.data)if(Zs(u.data.type).full_address===this.buildPositionType()){let f=fi(u),h=`${f.pos_object_id}_getPositionList`;this._sdk.updateCache(h,f),a?e.includes(f.pool)&&n.push(f):n.push(f)}return n}async getPosition(t,e,i=!0,n=!0){let r=await this.getSimplePosition(e,n);return i&&(r=await this.updatePositionRewarders(t,r)),r}async getPositionById(t,e=!0,i=!0){let n=await this.getSimplePosition(t,i);if(e){let r=await this._sdk.Pool.getPool(n.pool,!1);return await this.updatePositionRewarders(r.position_manager.positions_handle,n)}return n}async getSimplePosition(t,e=!0){let i=`${t}_getPositionList`,n=this.getSimplePositionByCache(t);if(n===void 0){let r=await this.sdk.FullClient.getObject({id:t,options:{showContent:!0,showType:!0,showDisplay:e,showOwner:!0}});n=fi(r),this._sdk.updateCache(i,n)}return n}getSimplePositionByCache(t){let e=`${t}_getPositionList`;return this._sdk.getCache(e)}async getSimplePositionList(t,e=!0){let i=[],n=[];return t.forEach(r=>{let a=this.getSimplePositionByCache(r);a?i.push(a):n.push(r)}),n.length>0&&(await this._sdk.FullClient.batchGetObjects(n,{showOwner:!0,showContent:!0,showDisplay:e,showType:!0})).forEach(a=>{if(a.error==null){let u=fi(a);i.push(u);let d=`${u.pos_object_id}_getPositionList`;this._sdk.updateCache(d,u)}}),i}async updatePositionRewarders(t,e){let i=await this.getPositionRewarders(t,e.pos_object_id);return{...e,...i}}async getPositionRewarders(t,e){try{let i=await this._sdk.FullClient.getDynamicFieldObject({parentId:t,name:{type:"0x2::object::ID",value:e}}),r=zs(i.data).value.fields.value;return _i(r)}catch(i){console.log(i);return}}buildFetchPosFee(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r=[t.coin_type_a,t.coin_type_b],a=[e.object(se(i).global_config_id),e.object(t.pool_id),e.pure.address(t.position_id)];e.moveCall({target:`${n.published_at}::${Qt}::fetch_position_fees`,arguments:a,typeArguments:r})}parsedPosFeeData(t){let e={},i=t.events?.filter(n=>n.type.includes("fetcher_script::FetchPositionFeesEvent"));for(let n=0;n<i.length;n+=1){let{parsedJson:r}=i[n],a={position_id:r.position_id,fee_owned_a:r.fee_owned_a,fee_owned_b:r.fee_owned_b};e[r.position_id]=a}return e}async fetchPosFeeAmount(t){let{clmm_pool:e,integrate:i}=this.sdk.sdkOptions,n=new re;for(let d of t)this.buildFetchPosFee(d,n);let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:Hs("0x0")});r.error!=null&&E("InvalidPoolObject",`fetch position fee error code: ${r.error??"unknown error"}, please check config and position and pool object ids`,{[vi.METHOD_NAME]:"fetchPosFeeAmount"});let a=[],u=this.parsedPosFeeData(r);for(let d=0;d<t.length;d+=1){let f=u[t[d].position_id];if(f){let h={fee_owned_a:f.fee_owned_a,fee_owned_b:f.fee_owned_b,position_id:t[d].position_id};a.push(h)}}return a}async batchFetchPositionFees(t){let e=[];for(let n of t){let r=await this._sdk.Position.getPositionById(n,!1),a=await this._sdk.Pool.getPool(r.pool,!1);e.push({pool_id:a.id,position_id:r.pos_object_id,coin_type_a:a.coin_type_a,coin_type_b:a.coin_type_b})}let i={};if(e.length>0){let n=await this.fetchPosFeeAmount(e);for(let r of n)i[r.position_id]=r;return i}return i}async createAddLiquidityFixTokenPayload(t,e,i,n,r){let a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:u,is_adjust_coin_b:d}=Me(t);if(t=t,t.fix_amount_a&&u||!t.fix_amount_a&&d)return i=await $t.buildAddLiquidityFixTokenForGas(this._sdk,a,t,e,i,n,r),i}return $t.buildAddLiquidityFixToken(this._sdk,a,t,i,n,r)}async createAddLiquidityPayload(t,e,i,n){let{integrate:r,clmm_pool:a}=this._sdk.sdkOptions,u=Ie(BigInt(t.tick_lower)).toString(),d=Ie(BigInt(t.tick_upper)).toString(),f=[t.coin_type_a,t.coin_type_b];e=e||new re;let h=!Ws(t.pos_id),w=BigInt(t.max_amount_a),P=BigInt(t.max_amount_b),M,T;if(i==null||n==null){let x=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());M=yi.buildCoinForAmount(e,x,w,t.coin_type_a,!1,!0),T=yi.buildCoinForAmount(e,x,P,t.coin_type_b,!1,!0)}else M={target_coin:i,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},T={target_coin:n,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]};if(h)e.moveCall({target:`${r.published_at}::${Vt}::open_position_with_liquidity`,typeArguments:f,arguments:[e.object(se(a).global_config_id),e.object(t.pool_id),e.pure.u32(Number(u)),e.pure.u32(Number(d)),M.target_coin,T.target_coin,e.pure.u64(t.max_amount_a),e.pure.u64(t.max_amount_b),e.pure.u128(t.delta_liquidity),e.object(wi)]});else{let x=await this._sdk.FullClient.getOwnerCoinAssets(this._sdk.getSenderAddress());e=$t.createCollectRewarderAndFeeParams(this._sdk,e,t,x,M.remain_coins,T.remain_coins),e.moveCall({target:`${r.published_at}::${Vt}::add_liquidity`,typeArguments:f,arguments:[e.object(se(a).global_config_id),e.object(t.pool_id),e.object(t.pos_id),M.target_coin,T.target_coin,e.pure.u64(t.max_amount_a),e.pure.u64(t.max_amount_b),e.pure.u128(t.delta_liquidity),e.object(wi)]})}return e}async removeLiquidityPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r="remove_liquidity";e=e||new re;let a=[t.coin_type_a,t.coin_type_b],u=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());e=$t.createCollectRewarderAndFeeParams(this._sdk,e,t,u);let d=[e.object(se(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.pure.u128(t.delta_liquidity),e.pure.u64(t.min_amount_a),e.pure.u64(t.min_amount_b),e.object(wi)];return e.moveCall({target:`${n.published_at}::${qe}::${r}`,typeArguments:a,arguments:d}),e}async closePositionPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new re;let r=[t.coin_type_a,t.coin_type_b],a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());return e=$t.createCollectRewarderAndFeeParams(this._sdk,e,t,a),e.moveCall({target:`${n.published_at}::${qe}::close_position`,typeArguments:r,arguments:[e.object(se(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.pure.u64(t.min_amount_a),e.pure.u64(t.min_amount_b),e.object(wi)]}),e}openPositionPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new re;let r=[t.coin_type_a,t.coin_type_b],a=Ie(BigInt(t.tick_lower)).toString(),u=Ie(BigInt(t.tick_upper)).toString(),d=[e.object(se(i).global_config_id),e.object(t.pool_id),e.pure.u32(Number(a)),e.pure.u32(Number(u))];return e.moveCall({target:`${n.published_at}::${qe}::open_position`,typeArguments:r,arguments:d}),e}async openPositionWithPricePayload(t,e){let{pool_id:i}=t,{clmm_pool:n,integrate:r}=this.sdk.sdkOptions;e=e||new re;let a=await this.sdk.Pool.getPool(i,!1),u=Number(a.tick_spacing),d=0,f=0;if(t.is_full_range)d=Pi.getMinIndex(u),f=Pi.getMaxIndex(u);else{let{price_base_coin:P,min_price:M,max_price:T}=t;d=Re.priceToInitializeTickIndex(P==="coin_a"?te(M):te(1).div(T),t.coin_decimals_a,t.coin_decimals_b,u),f=Re.priceToInitializeTickIndex(P==="coin_a"?te(T):te(1).div(M),t.coin_decimals_a,t.coin_decimals_b,u)}let h=[a.coin_type_a,a.coin_type_b],w=[e.object(se(n).global_config_id),e.object(t.pool_id),e.pure.u32(Number(Ie(BigInt(d)))),e.pure.u32(Number(Ie(BigInt(f))))];return e.moveCall({target:`${r.published_at}::${qe}::open_position`,typeArguments:h,arguments:w}),e}async collectFeePayload(t,e,i,n){e=e||new re;let r=i||yi.buildCoinWithBalance(BigInt(0),t.coin_type_a,e),a=n||yi.buildCoinWithBalance(BigInt(0),t.coin_type_b,e);return this.createCollectFeePayload(t,e,r,a),e}createCollectFeePayload(t,e,i,n){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,u=[t.coin_type_a,t.coin_type_b],d=[e.object(se(r).global_config_id),e.object(t.pool_id),e.object(t.pos_id),i,n];return e.moveCall({target:`${a.published_at}::${Vt}::collect_fee`,typeArguments:u,arguments:d}),e}async calculateAddLiquidityResultWithPrice(t){let{pool_id:e,slippage:i,refresh_pool_price:n,add_mode_params:r}=t,a=await this.sdk.Pool.getPool(e,n),u=Number(a.tick_spacing),d=0,f=0;if(r.is_full_range)d=Pi.getMinIndex(u),f=Pi.getMaxIndex(u);else{let{price_base_coin:j,min_price:K,max_price:B}=r;d=Re.priceToInitializeTickIndex(j==="coin_a"?te(K):te(1).div(B),r.coin_decimals_a,r.coin_decimals_b,u),f=Re.priceToInitializeTickIndex(j==="coin_a"?te(B):te(1).div(K),r.coin_decimals_a,r.coin_decimals_b,u)}if("liquidity"in t){let{liquidity:j}=t,K=Re.tickIndexToSqrtPriceX64(d),B=Re.tickIndexToSqrtPriceX64(f),{coin_amount_a:D,coin_amount_b:jt}=Zo.getCoinAmountFromLiquidity(new Ke.default(j),new Ke.default(a.current_sqrt_price),K,B,!1),z=te(D).mul(1+i).toFixed(0,I.ROUND_UP),Lt=te(jt).mul(1+i).toFixed(0,I.ROUND_UP);return{coin_amount_a:D,coin_amount_b:jt,coin_amount_limit_a:z,coin_amount_limit_b:Lt,liquidity:j,tick_lower:d,tick_upper:f}}let{coin_amount:h,fix_amount_a:w}=t,{coin_amount_limit_a:P,coin_amount_limit_b:M,liquidity_amount:T,coin_amount_a:x,coin_amount_b:$}=Zo.estLiquidityAndCoinAmountFromOneAmounts(d,f,new Ke.default(h),w,!0,i,new Ke.default(a.current_sqrt_price));return{coin_amount_a:x,coin_amount_b:$,coin_amount_limit_a:P,coin_amount_limit_b:M,liquidity:T,tick_lower:d,tick_upper:f,fix_amount_a:w}}async addLiquidityWithPricePayload(t,e,i,n){let{pool_id:r,calculate_result:a}=t,{coin_amount_limit_a:u,coin_amount_limit_b:d,liquidity:f,tick_lower:h,tick_upper:w}=a;e=e||new re;let P=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityPayload({delta_liquidity:f,max_amount_a:u,max_amount_b:d,tick_lower:h,tick_upper:w,collect_fee:!1,rewarder_coin_types:[],coin_type_a:P.coin_type_a,coin_type_b:P.coin_type_b,pool_id:r,pos_id:""},e,i,n),e}async createAddLiquidityFixCoinWithPricePayload(t,e,i,n){let{pool_id:r,calculate_result:a}=t,{coin_amount_limit_a:u,coin_amount_limit_b:d,liquidity:f,tick_lower:h,tick_upper:w,fix_amount_a:P,coin_amount_a:M,coin_amount_b:T}=a;if(P===void 0)throw E("InvalidParams","fix_amount_a is undefined",{[vi.METHOD_NAME]:"addLiquidityFixCoinCoinWithPricePayload",[vi.REQUEST_PARAMS]:t});e=e||new re;let x=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityFixTokenPayload({amount_a:P?M:u,amount_b:P?d:T,slippage:0,fix_amount_a:P,is_open:!0,tick_lower:h,tick_upper:w,collect_fee:!1,rewarder_coin_types:[],coin_type_a:x.coin_type_a,coin_type_b:x.coin_type_b,pool_id:r,pos_id:""},void 0,e,i,n),e}createCollectFeeNoSendPayload(t,e,i,n){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,u=[t.coin_type_a,t.coin_type_b],d=[e.object(se(r).global_config_id),e.object(t.pool_id),e.object(t.pos_id),i,n];return e.moveCall({target:`${a.published_at}::${Xe}::collect_fee`,typeArguments:u,arguments:d}),e}};var wn=ne(Gt());import{Transaction as hn}from"@mysten/sui/transactions";import{normalizeSuiAddress as Vs}from"@mysten/sui/utils";import{CLOCK_ADDRESS as pn,CoinAssist as mn,DETAILS_KEYS as zo,getPackagerConfigs as gn,MathUtil as Qs,normalizeCoinType as bn,ZERO as Oe}from"@cetusprotocol/common-sdk";var Mi=class{constructor(t){this._sdk=t,this.growthGlobal=[Oe,Oe,Oe]}get sdk(){return this._sdk}async emissionsEveryDay(t){let i=(await this.sdk.Pool.getPool(t)).rewarder_infos;if(!i)return null;let n=[];for(let r of i){let a=Qs.fromX64(new wn.default(r.emissions_per_second));n.push({emissions:Math.floor(a.toNumber()*60*60*24),coin_type:r.coin_type})}return n}async batchFetchPositionRewarders(t){let e=[];for(let n of t){let r=await this._sdk.Position.getPositionById(n,!1),a=await this._sdk.Pool.getPool(r.pool,!1);e.push({pool_id:a.id,position_id:r.pos_object_id,coin_type_a:a.coin_type_a,coin_type_b:a.coin_type_b,rewarder_types:a.rewarder_infos.map(u=>u.coin_type)})}let i={};if(e.length>0){let n=await this.fetchPosRewardersAmount(e);for(let r of n)i[r.position_id]=r.rewarder_amounts;return i}return i}async fetchPositionRewarders(t,e){let i={pool_id:t.id,position_id:e,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b,rewarder_types:t.rewarder_infos.map(r=>r.coin_type)};return(await this.fetchPosRewardersAmount([i]))[0].rewarder_amounts}async batchFetchPositionFees(t){return await this._sdk.Position.batchFetchPositionFees(t)}async fetchPosFeeAmount(t){return await this._sdk.Position.fetchPosFeeAmount(t)}buildFetchPosReward(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r=[t.coin_type_a,t.coin_type_b],a=[e.object(gn(i).global_config_id),e.object(t.pool_id),e.pure.address(t.position_id),e.object(pn)];e.moveCall({target:`${n.published_at}::${Qt}::fetch_position_rewards`,arguments:a,typeArguments:r})}async fetchPosRewardersAmount(t){let e=new hn;for(let a of t)this.buildFetchPosReward(a,e);let i=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:e,sender:Vs("0x")});i.error!=null&&E("InvalidConfig",`fetch position rewards error code: ${i.error??"unknown error"}, please check config and params`,{[zo.METHOD_NAME]:"fetchPosRewardersAmount",[zo.REQUEST_PARAMS]:{params:t}});let n=this.parsedPosRewardData(i),r=[];for(let a=0;a<t.length;a+=1){let u=n[t[a].position_id];if(u){let d={pool_id:t[a].pool_id,position_id:t[a].position_id,rewarder_amounts:u.rewarder_amount.map((f,h)=>({amount_owned:f,coin_type:t[a].rewarder_types[h]}))};r.push(d)}}return r}parsedPosRewardData(t){let e={},i=t.events?.filter(n=>n.type.includes("fetcher_script::FetchPositionRewardsEvent"));for(let n=0;n<i.length;n+=1){let{parsedJson:r}=i[n],a={position_id:r.position_id,rewarder_amount:r.data};e[r.position_id]=a}return e}async fetchPoolRewardersAmount(t,e){let i=await this.sdk.Pool.getPool(e),n=await this.sdk.Position.getPositionList(t,[e]),r=[];for(let d of n)r.push({pool_id:i.id,position_id:d.pos_object_id,rewarder_types:i.rewarder_infos.map(f=>f.coin_type),coin_type_a:i.coin_type_a,coin_type_b:i.coin_type_b});let a=await this.fetchPosRewardersAmount(r),u=[Oe,Oe,Oe];if(a!=null)for(let d of a)for(let f=0;f<d.rewarder_amounts.length;f+=1)u[f]=u[f].add(new wn.default(d.rewarder_amounts[f].amount_owned));return u}async getPoolLowerAndUpperTicks(t,e){let i=[],n=[];for(let r of e){let a=await this.sdk.Pool.getTickDataByIndex(t,r.tick_lower_index),u=await this.sdk.Pool.getTickDataByIndex(t,r.tick_upper_index);i.push(a),n.push(u)}return[i,n]}async collectRewarderPayload(t){let e=await this.sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress(),null),i=new hn;return i=$t.createCollectRewarderAndFeeParams(this._sdk,i,t,e),i}async batchCollectRewardsPayload(t,e,i,n){let r=await this.sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress(),null);e=e||new hn;let a={};return t.forEach(u=>{let d=bn(u.coin_type_a),f=bn(u.coin_type_b);if(u.collect_fee){let w=a[d];w==null&&(i==null?w=mn.buildCoinForAmount(e,r,BigInt(0),d,!1):w={target_coin:i,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},a[d]=w);let P=a[f];P==null&&(n==null?P=mn.buildCoinForAmount(e,r,BigInt(0),f,!1):P={target_coin:n,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},a[f]=P),e=this._sdk.Position.createCollectFeeNoSendPayload({pool_id:u.pool_id,pos_id:u.pos_id,coin_type_a:u.coin_type_a,coin_type_b:u.coin_type_b},e,w.target_coin,P.target_coin)}let h=[];u.rewarder_coin_types.forEach(w=>{let P=bn(w),M=a[w];M===void 0&&(M=mn.buildCoinForAmount(e,r,BigInt(0),P,!1),a[P]=M),h.push(M.target_coin)}),e=this.createCollectRewarderNoSendPayload(u,e,h)}),Object.keys(a).forEach(u=>{let d=a[u];d.is_mint_zero_coin&&$o(this.sdk,e,d.target_coin,u,this.sdk.getSenderAddress())}),e}createCollectRewarderPayload(t,e,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=gn(n),u=[t.coin_type_a,t.coin_type_b];return t.rewarder_coin_types.forEach((d,f)=>{e&&e.moveCall({target:`${r.published_at}::${Vt}::collect_reward`,typeArguments:[...u,d],arguments:[e.object(a.global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.object(a.global_vault_id),i[f],e.object(pn)]})}),e}createCollectRewarderNoSendPayload(t,e,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=gn(n),u=[t.coin_type_a,t.coin_type_b];return t.rewarder_coin_types.forEach((d,f)=>{e&&e.moveCall({target:`${r.published_at}::${Xe}::collect_reward`,typeArguments:[...u,d],arguments:[e.object(a.global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.object(a.global_vault_id),i[f],e.object(pn)]})}),e}};var pe=ne(Gt());import{Transaction as Go}from"@mysten/sui/transactions";import{normalizeSuiAddress as Vo}from"@mysten/sui/utils";import{d as Fe,DETAILS_KEYS as fe,extractStructTagFromType as Qo,TickMath as Jo,U64_MAX as Js,ZERO as Xs}from"@cetusprotocol/common-sdk";var sl="amm_swap",al="Pool",ki=class{constructor(t){this.calculateSingleImpact=(t,e)=>e.minus(t).div(e).mul(100);this._sdk=t}get sdk(){return this._sdk}calculateSwapFee(t){let e=Fe(0);return t.forEach(i=>{let n=i.base_paths.length;if(n>0){let r=i.base_paths[0],a=r.label==="Cetus"?new I(r.fee_rate).div(10**6):new I(r.fee_rate).div(10**9),u=Fe(r.input_amount).div(10**r.from_decimal).mul(a);if(e=e.add(u),n>1){let d=i.base_paths[1],f=r.direction?r.current_price:new I(1).div(r.current_price),h=d.direction?d.current_price:new I(1).div(d.current_price),w=d.label==="Cetus"?new I(d.fee_rate).div(10**6):new I(d.fee_rate).div(10**9),M=Fe(d.output_amount).div(10**d.to_decimal).mul(w).div(f.mul(h));e=e.add(M)}}}),e.toString()}calculateSwapPriceImpact(t){let e=Fe(0);return t.forEach(i=>{let n=i.base_paths.length;if(n===1){let r=i.base_paths[0],a=Fe(r.output_amount).div(10**r.to_decimal),u=Fe(r.input_amount).div(10**r.from_decimal),d=a.div(u),f=r.direction?new I(r.current_price):new I(1).div(r.current_price);e=e.add(this.calculateSingleImpact(d,f))}if(n===2){let r=i.base_paths[0],a=i.base_paths[1],u=r.direction?new I(r.current_price):new I(1).div(r.current_price),d=a.direction?new I(a.current_price):new I(1).div(a.current_price),f=u.mul(d),h=new I(a.output_amount).div(10**a.to_decimal),w=new I(r.input_amount).div(10**r.from_decimal),P=h.div(w);e=e.add(this.calculateSingleImpact(P,f))}}),e.toString()}async preSwapWithMultiPool(t){let{integrate:e}=this.sdk.sdkOptions,i=new Go,n=[t.coin_type_a,t.coin_type_b];for(let f=0;f<t.pool_ids.length;f+=1){let h=[i.object(t.pool_ids[f]),i.pure.bool(t.a2b),i.pure.bool(t.by_amount_in),i.pure.u64(t.amount)];i.moveCall({target:`${e.published_at}::${Qt}::calculate_swap_result`,arguments:h,typeArguments:n})}let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:Vo("0x0")});r.error!=null&&E("InvalidConfig",`pre swap with multi pools error code: ${r.error??"unknown error"}, please check config and params`,{[fe.METHOD_NAME]:"preSwapWithMultiPool",[fe.REQUEST_PARAMS]:{params:t}});let a=r.events?.filter(f=>Qo(f.type).name==="CalculatedSwapResultEvent");if(a.length===0)return null;a.length!==t.pool_ids.length&&E("ParamsLengthNotEqual","valueData.length !== params.pools.length",{[fe.METHOD_NAME]:"preSwapWithMultiPool",[fe.REQUEST_PARAMS]:{params:t}});let u=t.by_amount_in?Xs:Js,d=0;for(let f=0;f<a.length;f+=1)if(!a[f].parsedJson.data.is_exceed)if(t.by_amount_in){let h=new pe.default(a[f].parsedJson.data.amount_out);h.gt(u)&&(d=f,u=h)}else{let h=new pe.default(a[f].parsedJson.data.amount_out);h.lt(u)&&(d=f,u=h)}return this.transformSwapWithMultiPoolData({pool_address:t.pool_ids[d],a2b:t.a2b,by_amount_in:t.by_amount_in,amount:t.amount,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b},a[d].parsedJson)}async preSwap(t){let{integrate:e}=this.sdk.sdkOptions,i=new Go,n=[t.coin_type_a,t.coin_type_b],r=[i.object(t.pool.id),i.pure.bool(t.a2b),i.pure.bool(t.by_amount_in),i.pure.u64(t.amount)];i.moveCall({target:`${e.published_at}::${Qt}::calculate_swap_result`,arguments:r,typeArguments:n});let a=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:Vo("0x0")});if(a.error!=null)return E("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[fe.METHOD_NAME]:"preSwap",[fe.REQUEST_PARAMS]:{params:t}});let u=a.events?.filter(d=>Qo(d.type).name==="CalculatedSwapResultEvent");return u.length===0?E("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[fe.METHOD_NAME]:"preSwap",[fe.REQUEST_PARAMS]:{params:t}}):this.transformSwapData(t,u[0].parsedJson.data)}transformSwapData(t,e){let i=e.amount_in&&e.fee_amount?new pe.default(e.amount_in).add(new pe.default(e.fee_amount)).toString():"";return{pool_address:t.pool.id,current_sqrt_price:t.current_sqrt_price,estimated_amount_in:i,estimated_amount_out:e.amount_out,estimated_end_sqrt_price:e.after_sqrt_price,estimated_fee_amount:e.fee_amount,is_exceed:e.is_exceed,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in}}transformSwapWithMultiPoolData(t,e){let{data:i}=e;console.log("json data. ",i);let n=i.amount_in&&i.fee_amount?new pe.default(i.amount_in).add(new pe.default(i.fee_amount)).toString():"";return{pool_address:t.pool_address,estimated_amount_in:n,estimated_amount_out:i.amount_out,estimated_end_sqrt_price:i.after_sqrt_price,estimated_start_sqrt_price:i.step_results[0].current_sqrt_price,estimated_fee_amount:i.fee_amount,is_exceed:i.is_exceed,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in}}calculateRates(t){let{current_pool:e}=t,i=Eo(e),n;t.a2b?n=t.swap_ticks.sort((P,M)=>M.index-P.index):n=t.swap_ticks.sort((P,M)=>P.index-M.index);let r=No(t.a2b,t.by_amount_in,t.amount,i,n),a=!1;t.by_amount_in?a=r.amount_in.lt(t.amount):a=r.amount_out.lt(t.amount);let u=Yt.getDefaultSqrtPriceLimit(t.a2b);t.a2b&&r.next_sqrt_price.lt(u)&&(a=!0),!t.a2b&&r.next_sqrt_price.gt(u)&&(a=!0);let d=0;r.cross_tick_num>6&&r.cross_tick_num<40&&(d=22e3*(r.cross_tick_num-6)),r.cross_tick_num>40&&(a=!0);let f=Jo.sqrtPriceX64ToPrice(i.current_sqrt_price,t.decimals_a,t.decimals_b).toNumber(),h=Jo.sqrtPriceX64ToPrice(r.next_sqrt_price,t.decimals_a,t.decimals_b).toNumber(),w=Math.abs(f-h)/f*100;return{estimated_amount_in:r.amount_in,estimated_amount_out:r.amount_out,estimated_end_sqrt_price:r.next_sqrt_price,estimated_fee_amount:r.fee_amount,is_exceed:a,extra_compute_limit:d,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in,price_impact_pct:w}}async createSwapPayload(t,e){let i=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:n,is_adjust_coin_b:r}=Me(t);if(t.a2b&&n||!t.a2b&&r)return await Yt.buildSwapTransactionForGas(this._sdk,t,i,e)}return Yt.buildSwapTransaction(this.sdk,t,i)}async createSwapWithoutTransferCoinsPayload(t,e){let i=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:n,is_adjust_coin_b:r}=Me(t);if(t.a2b&&n||!t.a2b&&r)return await Yt.buildSwapTransactionWithoutTransferCoinsForGas(this._sdk,t,i,e)}return Yt.buildSwapTransactionWithoutTransferCoins(this.sdk,t,i)}};import{FullRpcUrlMainnet as Ys}from"@cetusprotocol/common-sdk";var Xo={clmmConfig:{pools_id:"0xf699e7f2276f5c9a75944b37a0c5b5d9ddfd2471bf6242483b03ab2887d198d0",global_config_id:"0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f",global_vault_id:"0xce7bceef26d3ad1f6d9b6f13a953f053e6ed3ca77907516481ce99ae8e588f2b",admin_cap_id:"0x89c1a321291d15ddae5a086c9abc533dff697fde3d89e0ca836c41af73e36a75"},cetusConfig:{coin_list_id:"0x8cbc11d9e10140db3d230f50b4d30e9b721201c0083615441707ffec1ef77b23",launchpad_pools_id:"0x1098fac992eab3a0ab7acf15bb654fc1cf29b5a6142c4ef1058e6c408dd15115",clmm_pools_id:"0x15b6a27dd9ae03eb455aba03b39e29aad74abd3757b8e18c0755651b2ae5b71e",admin_cap_id:"0x39d78781750e193ce35c45ff32c6c0c3f2941fa3ddaf8595c90c555589ddb113",global_config_id:"0x0408fa4e4a4c03cc0de8f23d0c2bbfe8913d178713c9a271ed4080973fe42d8f",coin_list_handle:"0x49136005e90e28c4695419ed4194cc240603f1ea8eb84e62275eaff088a71063",launchpad_pools_handle:"0x5e194a8efcf653830daf85a85b52e3ae8f65dc39481d54b2382acda25068375c",clmm_pools_handle:"0x37f60eb2d9d227949b95da8fea810db3c32d1e1fa8ed87434fc51664f87d83cb"}},Yo={env:"mainnet",full_rpc_url:Ys,cetus_config:{package_id:"0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f",published_at:"0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f",config:Xo.cetusConfig},clmm_pool:{package_id:"0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb",published_at:"0xc6faf3703b0e8ba9ed06b7851134bbbe7565eb35ff823fd78432baa4cbeaa12e",config:Xo.clmmConfig},integrate:{package_id:"0x996c4d9480708fb8b92aa7acf819fb0497b5ec8e65ba06601cae2fb6db3312c3",published_at:"0x2d8c2e0fc6dd25b0214b3fa747e0fd27fd54608142cd2e4f64c1cd350cc4add4"},stats_pools_url:"https://api-sui.cetus.zone/v2/sui/stats_pools"};import{FullRpcUrlTestnet as ta}from"@cetusprotocol/common-sdk";var tr={clmmConfig:{pools_id:"0x50eb61dd5928cec5ea04711a2e9b72e5237e79e9fbcd2ce3d5469dc8708e0ee2",global_config_id:"0x9774e359588ead122af1c7e7f64e14ade261cfeecdb5d0eb4a5b3b4c8ab8bd3e",global_vault_id:"0xf78d2ee3c312f298882cb680695e5e8c81b1d441a646caccc058006c2851ddea",admin_cap_id:"0xa456f86a53fc31e1243f065738ff1fc93f5a62cc080ff894a0fb3747556a799b"},cetusConfig:{coin_list_id:"0x257eb2ba592a5480bba0a97d05338fab17cc3283f8df6998a0e12e4ab9b84478",launchpad_pools_id:"0xdc3a7bd66a6dcff73c77c866e87d73826e446e9171f34e1c1b656377314f94da",clmm_pools_id:"0x26c85500f5dd2983bf35123918a144de24e18936d0b234ef2b49fbb2d3d6307d",admin_cap_id:"0x1a496f6c67668eb2c27c99e07e1d61754715c1acf86dac45020c886ac601edb8",global_config_id:"0xe1f3db327e75f7ec30585fa52241edf66f7e359ef550b533f89aa1528dd1be52",coin_list_handle:"0x3204350fc603609c91675e07b8f9ac0999b9607d83845086321fca7f469de235",launchpad_pools_handle:"0xae67ff87c34aceea4d28107f9c6c62e297a111e9f8e70b9abbc2f4c9f5ec20fd",clmm_pools_handle:"0xd28736923703342b4752f5ed8c2f2a5c0cb2336c30e1fed42b387234ce8408ec"}},er={env:"testnet",full_rpc_url:ta,cetus_config:{package_id:"0xf5ff7d5ba73b581bca6b4b9fa0049cd320360abd154b809f8700a8fd3cfaf7ca",published_at:"0xf5ff7d5ba73b581bca6b4b9fa0049cd320360abd154b809f8700a8fd3cfaf7ca",config:tr.cetusConfig},clmm_pool:{package_id:"0x0c7ae833c220aa73a3643a0d508afa4ac5d50d97312ea4584e35f9eb21b9df12",published_at:"0x85e61285a10efc6602ab00df70a0c06357c384ef4c5633ecf73016df1500c704",config:tr.clmmConfig},integrate:{package_id:"0x2918cf39850de6d5d94d8196dc878c8c722cd79db659318e00bff57fbb4e2ede",published_at:"0x19dd42e05fa6c9988a60d30686ee3feb776672b5547e328d6dab16563da65293"},stats_pools_url:"https://api-sui.devcetus.com/v2/sui/stats_pools"};var Ai=class o extends ea{constructor(t){super(t),this._swap=new ki(this),this._pool=new bi(this),this._position=new Ci(this),this._config=new Je(this),this._rewarder=new Mi(this)}get Pool(){return this._pool}get Position(){return this._position}get CetusConfig(){return this._config}get Rewarder(){return this._rewarder}get Swap(){return this._swap}static createSDK(t){let{env:e="mainnet"}=t;return e==="mainnet"?o.createCustomSDK({...Yo,...t}):o.createCustomSDK({...er,...t})}static createCustomSDK(t){return new o(t)}};var We=ne(Gt());import{MathUtil as Be,TickMath as ir}from"@cetusprotocol/common-sdk";var ia=new We.default(365),na=new We.default(24),oa=new We.default(3600),ra=new We.default(.5);function Sl(o,t,e,i){return ia.mul(na).mul(oa).mul(ra).mul(o.mul(t).add(e).div(i))}function sa(o,t,e,i,n,r){let a=new I(o.toString()).div(new I(10**e)),u=new I(t.toString()).div(new I(10**i));return a.mul(n).add(u.mul(r))}function Il(o,t,e,i,n,r,a,u,d,f,h,w,P,M,T,x,$,j,K,B,D,jt,z,Lt){let ee=new I(w),Zt=new I(P),ie=new I(x),L=new I($),Et=new I(j),y=new I(K),s=new I(B),l=new I(D),_=new I(jt),p=new I(z),g=new I(Lt),v=ir.tickIndexToSqrtPriceX64(t),C=ir.tickIndexToSqrtPriceX64(e),m=Be.toX64Decimal(Be.fromX64(v)).round(),c=Be.toX64Decimal(Be.fromX64(C)).round(),b=Be.toX64Decimal(Be.fromX64(i)).round(),A,S=ee.mul(new I(10**r)).mul(c.mul(m)).div(c.sub(m)).round(),R=Zt.mul(new I(10**a)).div(c.sub(m)).round();o<t?A=S:o>e?A=R:A=I.min(S,R);let q=A.mul(b.sub(m)),W=A.mul(c.sub(b)).div(b.mul(c)).div(new I(10**r)).mul(s).add(q.div(new I(10**a).mul(l))),zt=sa(M,T,r,a,s,l),H=W.div(zt),G=A.eq(new I(0))?new I(0):new I(h/1e4).mul(ie).mul(new I(A.toString()).div(new I(n.toString()).add(new I(A.toString())))).div(W),me=H.eq(new I(0))?new I(0):H.mul(new I(36500/7)).div(W),Q=L.div(new I(10**u)).mul(_).mul(me),J=Et.div(new I(10**d)).mul(p).mul(me),He=y.div(new I(10**f)).mul(g).mul(me);return{fee_apr:G,pos_rewarder_0_apr:Q,pos_rewarder_1_apr:J,pos_rewarder_2_apr:He}}function Rl(o,t,e,i){let n=Math.max(o,e),a=Math.min(t,i)-n,u=t-o,d=i-e,f=new I(u.toString()),h=new I(d.toString()),w=new I(a.toString()),P=new I("0");return a<0?P=new I("0"):u===a?P=h.div(w):d===a?P=w.div(f):P=w.mul(w).div(h).div(f),P}var Ul=Ai;export{sl as AMM_SWAP_MODULE,Ai as CetusClmmSDK,we as ClmmError,ba as ClmmExpectSwapModule,Qt as ClmmFetcherModule,qe as ClmmIntegratePoolModule,Vt as ClmmIntegratePoolV2Module,Xe as ClmmIntegratePoolV3Module,Ye as ClmmIntegrateRouterModule,ao as ClmmIntegrateRouterWithPartnerModule,co as ClmmIntegrateUtilsModule,so as ClmmPartnerModule,lo as ClmmPositionStatus,ye as ConfigErrorCode,Je as ConfigModule,ya as DeepbookClobV2Module,wa as DeepbookCustodianV2Module,va as DeepbookEndpointsV2Module,al as POOL_STRUCT,eo as PartnerErrorCode,Ne as PoolErrorCode,bi as PoolModule,Ve as PositionErrorCode,Ci as PositionModule,$t as PositionUtils,Mi as RewarderModule,Pr as RouterErrorCode,to as SwapErrorCode,ki as SwapModule,Yt as SwapUtils,io as TypesErrorCode,Qi as UtilsErrorCode,ac as buildClmmPositionName,di as buildPool,Lo as buildPoolTransactionInfo,fi as buildPosition,_i as buildPositionReward,Do as buildPositionTransactionInfo,hi as buildTickData,qo as buildTickDataByEvent,$o as buildTransferCoin,pi as buildTransferCoinToSender,Yo as clmmMainnet,er as clmmTestnet,No as computeSwap,Rs as computeSwapStep,Ul as default,Sl as estPoolAPR,Il as estPositionAPRWithDeltaMethod,Rl as estPositionAPRWithMultiMethod,Me as findAdjustCoin,Pa as getDefaultSuiInputType,Ka as getLowerSqrtPriceFromCoinA,Ha as getLowerSqrtPriceFromCoinB,Wa as getUpperSqrtPriceFromCoinA,Za as getUpperSqrtPriceFromCoinB,ve as handleError,E as handleMessageError,Ja as newBits,uo as poolFilterEvenTypes,Eo as transClmmpoolDataWithoutTicks};
1
+ var hr=Object.create;var $n=Object.defineProperty;var pr=Object.getOwnPropertyDescriptor;var mr=Object.getOwnPropertyNames;var gr=Object.getPrototypeOf,br=Object.prototype.hasOwnProperty;var wr=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var yr=(o,t)=>()=>(t||o((t={exports:{}}).exports,t),t.exports);var vr=(o,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of mr(t))!br.call(o,n)&&n!==e&&$n(o,n,{get:()=>t[n],enumerable:!(i=pr(t,n))||i.enumerable});return o};var re=(o,t,e)=>(e=o!=null?hr(gr(o)):{},vr(t||!o||!o.__esModule?$n(e,"default",{value:o,enumerable:!0}):e,o));var Gt=yr((_o,on)=>{"use strict";(function(o,t){"use strict";function e(y,s){if(!y)throw new Error(s||"Assertion failed")}function i(y,s){y.super_=s;var u=function(){};u.prototype=s.prototype,y.prototype=new u,y.prototype.constructor=y}function n(y,s,u){if(n.isBN(y))return y;this.negative=0,this.words=null,this.length=0,this.red=null,y!==null&&((s==="le"||s==="be")&&(u=s,s=10),this._init(y||0,s||10,u||"be"))}typeof o=="object"?o.exports=n:t.BN=n,n.BN=n,n.wordSize=26;var r;try{typeof window<"u"&&typeof window.Buffer<"u"?r=window.Buffer:r=wr("buffer").Buffer}catch{}n.isBN=function(s){return s instanceof n?!0:s!==null&&typeof s=="object"&&s.constructor.wordSize===n.wordSize&&Array.isArray(s.words)},n.max=function(s,u){return s.cmp(u)>0?s:u},n.min=function(s,u){return s.cmp(u)<0?s:u},n.prototype._init=function(s,u,_){if(typeof s=="number")return this._initNumber(s,u,_);if(typeof s=="object")return this._initArray(s,u,_);u==="hex"&&(u=16),e(u===(u|0)&&u>=2&&u<=36),s=s.toString().replace(/\s+/g,"");var p=0;s[0]==="-"&&(p++,this.negative=1),p<s.length&&(u===16?this._parseHex(s,p,_):(this._parseBase(s,u,p),_==="le"&&this._initArray(this.toArray(),u,_)))},n.prototype._initNumber=function(s,u,_){s<0&&(this.negative=1,s=-s),s<67108864?(this.words=[s&67108863],this.length=1):s<4503599627370496?(this.words=[s&67108863,s/67108864&67108863],this.length=2):(e(s<9007199254740992),this.words=[s&67108863,s/67108864&67108863,1],this.length=3),_==="le"&&this._initArray(this.toArray(),u,_)},n.prototype._initArray=function(s,u,_){if(e(typeof s.length=="number"),s.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(s.length/3),this.words=new Array(this.length);for(var p=0;p<this.length;p++)this.words[p]=0;var g,v,C=0;if(_==="be")for(p=s.length-1,g=0;p>=0;p-=3)v=s[p]|s[p-1]<<8|s[p-2]<<16,this.words[g]|=v<<C&67108863,this.words[g+1]=v>>>26-C&67108863,C+=24,C>=26&&(C-=26,g++);else if(_==="le")for(p=0,g=0;p<s.length;p+=3)v=s[p]|s[p+1]<<8|s[p+2]<<16,this.words[g]|=v<<C&67108863,this.words[g+1]=v>>>26-C&67108863,C+=24,C>=26&&(C-=26,g++);return this._strip()};function a(y,s){var u=y.charCodeAt(s);if(u>=48&&u<=57)return u-48;if(u>=65&&u<=70)return u-55;if(u>=97&&u<=102)return u-87;e(!1,"Invalid character in "+y)}function l(y,s,u){var _=a(y,u);return u-1>=s&&(_|=a(y,u-1)<<4),_}n.prototype._parseHex=function(s,u,_){this.length=Math.ceil((s.length-u)/6),this.words=new Array(this.length);for(var p=0;p<this.length;p++)this.words[p]=0;var g=0,v=0,C;if(_==="be")for(p=s.length-1;p>=u;p-=2)C=l(s,u,p)<<g,this.words[v]|=C&67108863,g>=18?(g-=18,v+=1,this.words[v]|=C>>>26):g+=8;else{var m=s.length-u;for(p=m%2===0?u+1:u;p<s.length;p+=2)C=l(s,u,p)<<g,this.words[v]|=C&67108863,g>=18?(g-=18,v+=1,this.words[v]|=C>>>26):g+=8}this._strip()};function d(y,s,u,_){for(var p=0,g=0,v=Math.min(y.length,u),C=s;C<v;C++){var m=y.charCodeAt(C)-48;p*=_,m>=49?g=m-49+10:m>=17?g=m-17+10:g=m,e(m>=0&&g<_,"Invalid character"),p+=g}return p}n.prototype._parseBase=function(s,u,_){this.words=[0],this.length=1;for(var p=0,g=1;g<=67108863;g*=u)p++;p--,g=g/u|0;for(var v=s.length-_,C=v%p,m=Math.min(v,v-C)+_,c=0,b=_;b<m;b+=p)c=d(s,b,b+p,u),this.imuln(g),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c);if(C!==0){var A=1;for(c=d(s,b,s.length,u),b=0;b<C;b++)A*=u;this.imuln(A),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c)}this._strip()},n.prototype.copy=function(s){s.words=new Array(this.length);for(var u=0;u<this.length;u++)s.words[u]=this.words[u];s.length=this.length,s.negative=this.negative,s.red=this.red};function f(y,s){y.words=s.words,y.length=s.length,y.negative=s.negative,y.red=s.red}if(n.prototype._move=function(s){f(s,this)},n.prototype.clone=function(){var s=new n(null);return this.copy(s),s},n.prototype._expand=function(s){for(;this.length<s;)this.words[this.length++]=0;return this},n.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},n.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{n.prototype[Symbol.for("nodejs.util.inspect.custom")]=h}catch{n.prototype.inspect=h}else n.prototype.inspect=h;function h(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var w=["","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"],P=[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],k=[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];n.prototype.toString=function(s,u){s=s||10,u=u|0||1;var _;if(s===16||s==="hex"){_="";for(var p=0,g=0,v=0;v<this.length;v++){var C=this.words[v],m=((C<<p|g)&16777215).toString(16);g=C>>>24-p&16777215,p+=2,p>=26&&(p-=26,v--),g!==0||v!==this.length-1?_=w[6-m.length]+m+_:_=m+_}for(g!==0&&(_=g.toString(16)+_);_.length%u!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}if(s===(s|0)&&s>=2&&s<=36){var c=P[s],b=k[s];_="";var A=this.clone();for(A.negative=0;!A.isZero();){var x=A.modrn(b).toString(s);A=A.idivn(b),A.isZero()?_=x+_:_=w[c-x.length]+x+_}for(this.isZero()&&(_="0"+_);_.length%u!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}e(!1,"Base should be between 2 and 36")},n.prototype.toNumber=function(){var s=this.words[0];return this.length===2?s+=this.words[1]*67108864:this.length===3&&this.words[2]===1?s+=4503599627370496+this.words[1]*67108864:this.length>2&&e(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-s:s},n.prototype.toJSON=function(){return this.toString(16,2)},r&&(n.prototype.toBuffer=function(s,u){return this.toArrayLike(r,s,u)}),n.prototype.toArray=function(s,u){return this.toArrayLike(Array,s,u)};var T=function(s,u){return s.allocUnsafe?s.allocUnsafe(u):new s(u)};n.prototype.toArrayLike=function(s,u,_){this._strip();var p=this.byteLength(),g=_||Math.max(1,p);e(p<=g,"byte array longer than desired length"),e(g>0,"Requested array length <= 0");var v=T(s,g),C=u==="le"?"LE":"BE";return this["_toArrayLike"+C](v,p),v},n.prototype._toArrayLikeLE=function(s,u){for(var _=0,p=0,g=0,v=0;g<this.length;g++){var C=this.words[g]<<v|p;s[_++]=C&255,_<s.length&&(s[_++]=C>>8&255),_<s.length&&(s[_++]=C>>16&255),v===6?(_<s.length&&(s[_++]=C>>24&255),p=0,v=0):(p=C>>>24,v+=2)}if(_<s.length)for(s[_++]=p;_<s.length;)s[_++]=0},n.prototype._toArrayLikeBE=function(s,u){for(var _=s.length-1,p=0,g=0,v=0;g<this.length;g++){var C=this.words[g]<<v|p;s[_--]=C&255,_>=0&&(s[_--]=C>>8&255),_>=0&&(s[_--]=C>>16&255),v===6?(_>=0&&(s[_--]=C>>24&255),p=0,v=0):(p=C>>>24,v+=2)}if(_>=0)for(s[_--]=p;_>=0;)s[_--]=0},Math.clz32?n.prototype._countBits=function(s){return 32-Math.clz32(s)}:n.prototype._countBits=function(s){var u=s,_=0;return u>=4096&&(_+=13,u>>>=13),u>=64&&(_+=7,u>>>=7),u>=8&&(_+=4,u>>>=4),u>=2&&(_+=2,u>>>=2),_+u},n.prototype._zeroBits=function(s){if(s===0)return 26;var u=s,_=0;return(u&8191)===0&&(_+=13,u>>>=13),(u&127)===0&&(_+=7,u>>>=7),(u&15)===0&&(_+=4,u>>>=4),(u&3)===0&&(_+=2,u>>>=2),(u&1)===0&&_++,_},n.prototype.bitLength=function(){var s=this.words[this.length-1],u=this._countBits(s);return(this.length-1)*26+u};function S(y){for(var s=new Array(y.bitLength()),u=0;u<s.length;u++){var _=u/26|0,p=u%26;s[u]=y.words[_]>>>p&1}return s}n.prototype.zeroBits=function(){if(this.isZero())return 0;for(var s=0,u=0;u<this.length;u++){var _=this._zeroBits(this.words[u]);if(s+=_,_!==26)break}return s},n.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},n.prototype.toTwos=function(s){return this.negative!==0?this.abs().inotn(s).iaddn(1):this.clone()},n.prototype.fromTwos=function(s){return this.testn(s-1)?this.notn(s).iaddn(1).ineg():this.clone()},n.prototype.isNeg=function(){return this.negative!==0},n.prototype.neg=function(){return this.clone().ineg()},n.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},n.prototype.iuor=function(s){for(;this.length<s.length;)this.words[this.length++]=0;for(var u=0;u<s.length;u++)this.words[u]=this.words[u]|s.words[u];return this._strip()},n.prototype.ior=function(s){return e((this.negative|s.negative)===0),this.iuor(s)},n.prototype.or=function(s){return this.length>s.length?this.clone().ior(s):s.clone().ior(this)},n.prototype.uor=function(s){return this.length>s.length?this.clone().iuor(s):s.clone().iuor(this)},n.prototype.iuand=function(s){var u;this.length>s.length?u=s:u=this;for(var _=0;_<u.length;_++)this.words[_]=this.words[_]&s.words[_];return this.length=u.length,this._strip()},n.prototype.iand=function(s){return e((this.negative|s.negative)===0),this.iuand(s)},n.prototype.and=function(s){return this.length>s.length?this.clone().iand(s):s.clone().iand(this)},n.prototype.uand=function(s){return this.length>s.length?this.clone().iuand(s):s.clone().iuand(this)},n.prototype.iuxor=function(s){var u,_;this.length>s.length?(u=this,_=s):(u=s,_=this);for(var p=0;p<_.length;p++)this.words[p]=u.words[p]^_.words[p];if(this!==u)for(;p<u.length;p++)this.words[p]=u.words[p];return this.length=u.length,this._strip()},n.prototype.ixor=function(s){return e((this.negative|s.negative)===0),this.iuxor(s)},n.prototype.xor=function(s){return this.length>s.length?this.clone().ixor(s):s.clone().ixor(this)},n.prototype.uxor=function(s){return this.length>s.length?this.clone().iuxor(s):s.clone().iuxor(this)},n.prototype.inotn=function(s){e(typeof s=="number"&&s>=0);var u=Math.ceil(s/26)|0,_=s%26;this._expand(u),_>0&&u--;for(var p=0;p<u;p++)this.words[p]=~this.words[p]&67108863;return _>0&&(this.words[p]=~this.words[p]&67108863>>26-_),this._strip()},n.prototype.notn=function(s){return this.clone().inotn(s)},n.prototype.setn=function(s,u){e(typeof s=="number"&&s>=0);var _=s/26|0,p=s%26;return this._expand(_+1),u?this.words[_]=this.words[_]|1<<p:this.words[_]=this.words[_]&~(1<<p),this._strip()},n.prototype.iadd=function(s){var u;if(this.negative!==0&&s.negative===0)return this.negative=0,u=this.isub(s),this.negative^=1,this._normSign();if(this.negative===0&&s.negative!==0)return s.negative=0,u=this.isub(s),s.negative=1,u._normSign();var _,p;this.length>s.length?(_=this,p=s):(_=s,p=this);for(var g=0,v=0;v<p.length;v++)u=(_.words[v]|0)+(p.words[v]|0)+g,this.words[v]=u&67108863,g=u>>>26;for(;g!==0&&v<_.length;v++)u=(_.words[v]|0)+g,this.words[v]=u&67108863,g=u>>>26;if(this.length=_.length,g!==0)this.words[this.length]=g,this.length++;else if(_!==this)for(;v<_.length;v++)this.words[v]=_.words[v];return this},n.prototype.add=function(s){var u;return s.negative!==0&&this.negative===0?(s.negative=0,u=this.sub(s),s.negative^=1,u):s.negative===0&&this.negative!==0?(this.negative=0,u=s.sub(this),this.negative=1,u):this.length>s.length?this.clone().iadd(s):s.clone().iadd(this)},n.prototype.isub=function(s){if(s.negative!==0){s.negative=0;var u=this.iadd(s);return s.negative=1,u._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(s),this.negative=1,this._normSign();var _=this.cmp(s);if(_===0)return this.negative=0,this.length=1,this.words[0]=0,this;var p,g;_>0?(p=this,g=s):(p=s,g=this);for(var v=0,C=0;C<g.length;C++)u=(p.words[C]|0)-(g.words[C]|0)+v,v=u>>26,this.words[C]=u&67108863;for(;v!==0&&C<p.length;C++)u=(p.words[C]|0)+v,v=u>>26,this.words[C]=u&67108863;if(v===0&&C<p.length&&p!==this)for(;C<p.length;C++)this.words[C]=p.words[C];return this.length=Math.max(this.length,C),p!==this&&(this.negative=1),this._strip()},n.prototype.sub=function(s){return this.clone().isub(s)};function $(y,s,u){u.negative=s.negative^y.negative;var _=y.length+s.length|0;u.length=_,_=_-1|0;var p=y.words[0]|0,g=s.words[0]|0,v=p*g,C=v&67108863,m=v/67108864|0;u.words[0]=C;for(var c=1;c<_;c++){for(var b=m>>>26,A=m&67108863,x=Math.min(c,s.length-1),E=Math.max(0,c-y.length+1);E<=x;E++){var q=c-E|0;p=y.words[q]|0,g=s.words[E]|0,v=p*g+A,b+=v/67108864|0,A=v&67108863}u.words[c]=A|0,m=b|0}return m!==0?u.words[c]=m|0:u.length--,u._strip()}var j=function(s,u,_){var p=s.words,g=u.words,v=_.words,C=0,m,c,b,A=p[0]|0,x=A&8191,E=A>>>13,q=p[1]|0,U=q&8191,V=q>>>13,zt=p[2]|0,H=zt&8191,z=zt>>>13,ye=p[3]|0,Q=ye&8191,J=ye>>>13,Ge=p[4]|0,X=Ge&8191,Y=Ge>>>13,Tn=p[5]|0,tt=Tn&8191,et=Tn>>>13,An=p[6]|0,it=An&8191,nt=An>>>13,Sn=p[7]|0,ot=Sn&8191,rt=Sn>>>13,xn=p[8]|0,st=xn&8191,at=xn>>>13,In=p[9]|0,ct=In&8191,lt=In>>>13,En=g[0]|0,ut=En&8191,dt=En>>>13,On=g[1]|0,ft=On&8191,_t=On>>>13,Rn=g[2]|0,ht=Rn&8191,pt=Rn>>>13,Fn=g[3]|0,mt=Fn&8191,gt=Fn>>>13,Bn=g[4]|0,bt=Bn&8191,wt=Bn>>>13,Nn=g[5]|0,yt=Nn&8191,vt=Nn>>>13,jn=g[6]|0,Pt=jn&8191,Ct=jn>>>13,qn=g[7]|0,kt=qn&8191,Mt=qn>>>13,Dn=g[8]|0,Tt=Dn&8191,At=Dn>>>13,Ln=g[9]|0,St=Ln&8191,xt=Ln>>>13;_.negative=s.negative^u.negative,_.length=19,m=Math.imul(x,ut),c=Math.imul(x,dt),c=c+Math.imul(E,ut)|0,b=Math.imul(E,dt);var Ii=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ii>>>26)|0,Ii&=67108863,m=Math.imul(U,ut),c=Math.imul(U,dt),c=c+Math.imul(V,ut)|0,b=Math.imul(V,dt),m=m+Math.imul(x,ft)|0,c=c+Math.imul(x,_t)|0,c=c+Math.imul(E,ft)|0,b=b+Math.imul(E,_t)|0;var Ei=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ei>>>26)|0,Ei&=67108863,m=Math.imul(H,ut),c=Math.imul(H,dt),c=c+Math.imul(z,ut)|0,b=Math.imul(z,dt),m=m+Math.imul(U,ft)|0,c=c+Math.imul(U,_t)|0,c=c+Math.imul(V,ft)|0,b=b+Math.imul(V,_t)|0,m=m+Math.imul(x,ht)|0,c=c+Math.imul(x,pt)|0,c=c+Math.imul(E,ht)|0,b=b+Math.imul(E,pt)|0;var Oi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Oi>>>26)|0,Oi&=67108863,m=Math.imul(Q,ut),c=Math.imul(Q,dt),c=c+Math.imul(J,ut)|0,b=Math.imul(J,dt),m=m+Math.imul(H,ft)|0,c=c+Math.imul(H,_t)|0,c=c+Math.imul(z,ft)|0,b=b+Math.imul(z,_t)|0,m=m+Math.imul(U,ht)|0,c=c+Math.imul(U,pt)|0,c=c+Math.imul(V,ht)|0,b=b+Math.imul(V,pt)|0,m=m+Math.imul(x,mt)|0,c=c+Math.imul(x,gt)|0,c=c+Math.imul(E,mt)|0,b=b+Math.imul(E,gt)|0;var Ri=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ri>>>26)|0,Ri&=67108863,m=Math.imul(X,ut),c=Math.imul(X,dt),c=c+Math.imul(Y,ut)|0,b=Math.imul(Y,dt),m=m+Math.imul(Q,ft)|0,c=c+Math.imul(Q,_t)|0,c=c+Math.imul(J,ft)|0,b=b+Math.imul(J,_t)|0,m=m+Math.imul(H,ht)|0,c=c+Math.imul(H,pt)|0,c=c+Math.imul(z,ht)|0,b=b+Math.imul(z,pt)|0,m=m+Math.imul(U,mt)|0,c=c+Math.imul(U,gt)|0,c=c+Math.imul(V,mt)|0,b=b+Math.imul(V,gt)|0,m=m+Math.imul(x,bt)|0,c=c+Math.imul(x,wt)|0,c=c+Math.imul(E,bt)|0,b=b+Math.imul(E,wt)|0;var Fi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Fi>>>26)|0,Fi&=67108863,m=Math.imul(tt,ut),c=Math.imul(tt,dt),c=c+Math.imul(et,ut)|0,b=Math.imul(et,dt),m=m+Math.imul(X,ft)|0,c=c+Math.imul(X,_t)|0,c=c+Math.imul(Y,ft)|0,b=b+Math.imul(Y,_t)|0,m=m+Math.imul(Q,ht)|0,c=c+Math.imul(Q,pt)|0,c=c+Math.imul(J,ht)|0,b=b+Math.imul(J,pt)|0,m=m+Math.imul(H,mt)|0,c=c+Math.imul(H,gt)|0,c=c+Math.imul(z,mt)|0,b=b+Math.imul(z,gt)|0,m=m+Math.imul(U,bt)|0,c=c+Math.imul(U,wt)|0,c=c+Math.imul(V,bt)|0,b=b+Math.imul(V,wt)|0,m=m+Math.imul(x,yt)|0,c=c+Math.imul(x,vt)|0,c=c+Math.imul(E,yt)|0,b=b+Math.imul(E,vt)|0;var Bi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Bi>>>26)|0,Bi&=67108863,m=Math.imul(it,ut),c=Math.imul(it,dt),c=c+Math.imul(nt,ut)|0,b=Math.imul(nt,dt),m=m+Math.imul(tt,ft)|0,c=c+Math.imul(tt,_t)|0,c=c+Math.imul(et,ft)|0,b=b+Math.imul(et,_t)|0,m=m+Math.imul(X,ht)|0,c=c+Math.imul(X,pt)|0,c=c+Math.imul(Y,ht)|0,b=b+Math.imul(Y,pt)|0,m=m+Math.imul(Q,mt)|0,c=c+Math.imul(Q,gt)|0,c=c+Math.imul(J,mt)|0,b=b+Math.imul(J,gt)|0,m=m+Math.imul(H,bt)|0,c=c+Math.imul(H,wt)|0,c=c+Math.imul(z,bt)|0,b=b+Math.imul(z,wt)|0,m=m+Math.imul(U,yt)|0,c=c+Math.imul(U,vt)|0,c=c+Math.imul(V,yt)|0,b=b+Math.imul(V,vt)|0,m=m+Math.imul(x,Pt)|0,c=c+Math.imul(x,Ct)|0,c=c+Math.imul(E,Pt)|0,b=b+Math.imul(E,Ct)|0;var Ni=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ni>>>26)|0,Ni&=67108863,m=Math.imul(ot,ut),c=Math.imul(ot,dt),c=c+Math.imul(rt,ut)|0,b=Math.imul(rt,dt),m=m+Math.imul(it,ft)|0,c=c+Math.imul(it,_t)|0,c=c+Math.imul(nt,ft)|0,b=b+Math.imul(nt,_t)|0,m=m+Math.imul(tt,ht)|0,c=c+Math.imul(tt,pt)|0,c=c+Math.imul(et,ht)|0,b=b+Math.imul(et,pt)|0,m=m+Math.imul(X,mt)|0,c=c+Math.imul(X,gt)|0,c=c+Math.imul(Y,mt)|0,b=b+Math.imul(Y,gt)|0,m=m+Math.imul(Q,bt)|0,c=c+Math.imul(Q,wt)|0,c=c+Math.imul(J,bt)|0,b=b+Math.imul(J,wt)|0,m=m+Math.imul(H,yt)|0,c=c+Math.imul(H,vt)|0,c=c+Math.imul(z,yt)|0,b=b+Math.imul(z,vt)|0,m=m+Math.imul(U,Pt)|0,c=c+Math.imul(U,Ct)|0,c=c+Math.imul(V,Pt)|0,b=b+Math.imul(V,Ct)|0,m=m+Math.imul(x,kt)|0,c=c+Math.imul(x,Mt)|0,c=c+Math.imul(E,kt)|0,b=b+Math.imul(E,Mt)|0;var ji=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(ji>>>26)|0,ji&=67108863,m=Math.imul(st,ut),c=Math.imul(st,dt),c=c+Math.imul(at,ut)|0,b=Math.imul(at,dt),m=m+Math.imul(ot,ft)|0,c=c+Math.imul(ot,_t)|0,c=c+Math.imul(rt,ft)|0,b=b+Math.imul(rt,_t)|0,m=m+Math.imul(it,ht)|0,c=c+Math.imul(it,pt)|0,c=c+Math.imul(nt,ht)|0,b=b+Math.imul(nt,pt)|0,m=m+Math.imul(tt,mt)|0,c=c+Math.imul(tt,gt)|0,c=c+Math.imul(et,mt)|0,b=b+Math.imul(et,gt)|0,m=m+Math.imul(X,bt)|0,c=c+Math.imul(X,wt)|0,c=c+Math.imul(Y,bt)|0,b=b+Math.imul(Y,wt)|0,m=m+Math.imul(Q,yt)|0,c=c+Math.imul(Q,vt)|0,c=c+Math.imul(J,yt)|0,b=b+Math.imul(J,vt)|0,m=m+Math.imul(H,Pt)|0,c=c+Math.imul(H,Ct)|0,c=c+Math.imul(z,Pt)|0,b=b+Math.imul(z,Ct)|0,m=m+Math.imul(U,kt)|0,c=c+Math.imul(U,Mt)|0,c=c+Math.imul(V,kt)|0,b=b+Math.imul(V,Mt)|0,m=m+Math.imul(x,Tt)|0,c=c+Math.imul(x,At)|0,c=c+Math.imul(E,Tt)|0,b=b+Math.imul(E,At)|0;var qi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(qi>>>26)|0,qi&=67108863,m=Math.imul(ct,ut),c=Math.imul(ct,dt),c=c+Math.imul(lt,ut)|0,b=Math.imul(lt,dt),m=m+Math.imul(st,ft)|0,c=c+Math.imul(st,_t)|0,c=c+Math.imul(at,ft)|0,b=b+Math.imul(at,_t)|0,m=m+Math.imul(ot,ht)|0,c=c+Math.imul(ot,pt)|0,c=c+Math.imul(rt,ht)|0,b=b+Math.imul(rt,pt)|0,m=m+Math.imul(it,mt)|0,c=c+Math.imul(it,gt)|0,c=c+Math.imul(nt,mt)|0,b=b+Math.imul(nt,gt)|0,m=m+Math.imul(tt,bt)|0,c=c+Math.imul(tt,wt)|0,c=c+Math.imul(et,bt)|0,b=b+Math.imul(et,wt)|0,m=m+Math.imul(X,yt)|0,c=c+Math.imul(X,vt)|0,c=c+Math.imul(Y,yt)|0,b=b+Math.imul(Y,vt)|0,m=m+Math.imul(Q,Pt)|0,c=c+Math.imul(Q,Ct)|0,c=c+Math.imul(J,Pt)|0,b=b+Math.imul(J,Ct)|0,m=m+Math.imul(H,kt)|0,c=c+Math.imul(H,Mt)|0,c=c+Math.imul(z,kt)|0,b=b+Math.imul(z,Mt)|0,m=m+Math.imul(U,Tt)|0,c=c+Math.imul(U,At)|0,c=c+Math.imul(V,Tt)|0,b=b+Math.imul(V,At)|0,m=m+Math.imul(x,St)|0,c=c+Math.imul(x,xt)|0,c=c+Math.imul(E,St)|0,b=b+Math.imul(E,xt)|0;var Di=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Di>>>26)|0,Di&=67108863,m=Math.imul(ct,ft),c=Math.imul(ct,_t),c=c+Math.imul(lt,ft)|0,b=Math.imul(lt,_t),m=m+Math.imul(st,ht)|0,c=c+Math.imul(st,pt)|0,c=c+Math.imul(at,ht)|0,b=b+Math.imul(at,pt)|0,m=m+Math.imul(ot,mt)|0,c=c+Math.imul(ot,gt)|0,c=c+Math.imul(rt,mt)|0,b=b+Math.imul(rt,gt)|0,m=m+Math.imul(it,bt)|0,c=c+Math.imul(it,wt)|0,c=c+Math.imul(nt,bt)|0,b=b+Math.imul(nt,wt)|0,m=m+Math.imul(tt,yt)|0,c=c+Math.imul(tt,vt)|0,c=c+Math.imul(et,yt)|0,b=b+Math.imul(et,vt)|0,m=m+Math.imul(X,Pt)|0,c=c+Math.imul(X,Ct)|0,c=c+Math.imul(Y,Pt)|0,b=b+Math.imul(Y,Ct)|0,m=m+Math.imul(Q,kt)|0,c=c+Math.imul(Q,Mt)|0,c=c+Math.imul(J,kt)|0,b=b+Math.imul(J,Mt)|0,m=m+Math.imul(H,Tt)|0,c=c+Math.imul(H,At)|0,c=c+Math.imul(z,Tt)|0,b=b+Math.imul(z,At)|0,m=m+Math.imul(U,St)|0,c=c+Math.imul(U,xt)|0,c=c+Math.imul(V,St)|0,b=b+Math.imul(V,xt)|0;var Li=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Li>>>26)|0,Li&=67108863,m=Math.imul(ct,ht),c=Math.imul(ct,pt),c=c+Math.imul(lt,ht)|0,b=Math.imul(lt,pt),m=m+Math.imul(st,mt)|0,c=c+Math.imul(st,gt)|0,c=c+Math.imul(at,mt)|0,b=b+Math.imul(at,gt)|0,m=m+Math.imul(ot,bt)|0,c=c+Math.imul(ot,wt)|0,c=c+Math.imul(rt,bt)|0,b=b+Math.imul(rt,wt)|0,m=m+Math.imul(it,yt)|0,c=c+Math.imul(it,vt)|0,c=c+Math.imul(nt,yt)|0,b=b+Math.imul(nt,vt)|0,m=m+Math.imul(tt,Pt)|0,c=c+Math.imul(tt,Ct)|0,c=c+Math.imul(et,Pt)|0,b=b+Math.imul(et,Ct)|0,m=m+Math.imul(X,kt)|0,c=c+Math.imul(X,Mt)|0,c=c+Math.imul(Y,kt)|0,b=b+Math.imul(Y,Mt)|0,m=m+Math.imul(Q,Tt)|0,c=c+Math.imul(Q,At)|0,c=c+Math.imul(J,Tt)|0,b=b+Math.imul(J,At)|0,m=m+Math.imul(H,St)|0,c=c+Math.imul(H,xt)|0,c=c+Math.imul(z,St)|0,b=b+Math.imul(z,xt)|0;var $i=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+($i>>>26)|0,$i&=67108863,m=Math.imul(ct,mt),c=Math.imul(ct,gt),c=c+Math.imul(lt,mt)|0,b=Math.imul(lt,gt),m=m+Math.imul(st,bt)|0,c=c+Math.imul(st,wt)|0,c=c+Math.imul(at,bt)|0,b=b+Math.imul(at,wt)|0,m=m+Math.imul(ot,yt)|0,c=c+Math.imul(ot,vt)|0,c=c+Math.imul(rt,yt)|0,b=b+Math.imul(rt,vt)|0,m=m+Math.imul(it,Pt)|0,c=c+Math.imul(it,Ct)|0,c=c+Math.imul(nt,Pt)|0,b=b+Math.imul(nt,Ct)|0,m=m+Math.imul(tt,kt)|0,c=c+Math.imul(tt,Mt)|0,c=c+Math.imul(et,kt)|0,b=b+Math.imul(et,Mt)|0,m=m+Math.imul(X,Tt)|0,c=c+Math.imul(X,At)|0,c=c+Math.imul(Y,Tt)|0,b=b+Math.imul(Y,At)|0,m=m+Math.imul(Q,St)|0,c=c+Math.imul(Q,xt)|0,c=c+Math.imul(J,St)|0,b=b+Math.imul(J,xt)|0;var Ui=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ui>>>26)|0,Ui&=67108863,m=Math.imul(ct,bt),c=Math.imul(ct,wt),c=c+Math.imul(lt,bt)|0,b=Math.imul(lt,wt),m=m+Math.imul(st,yt)|0,c=c+Math.imul(st,vt)|0,c=c+Math.imul(at,yt)|0,b=b+Math.imul(at,vt)|0,m=m+Math.imul(ot,Pt)|0,c=c+Math.imul(ot,Ct)|0,c=c+Math.imul(rt,Pt)|0,b=b+Math.imul(rt,Ct)|0,m=m+Math.imul(it,kt)|0,c=c+Math.imul(it,Mt)|0,c=c+Math.imul(nt,kt)|0,b=b+Math.imul(nt,Mt)|0,m=m+Math.imul(tt,Tt)|0,c=c+Math.imul(tt,At)|0,c=c+Math.imul(et,Tt)|0,b=b+Math.imul(et,At)|0,m=m+Math.imul(X,St)|0,c=c+Math.imul(X,xt)|0,c=c+Math.imul(Y,St)|0,b=b+Math.imul(Y,xt)|0;var Ki=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ki>>>26)|0,Ki&=67108863,m=Math.imul(ct,yt),c=Math.imul(ct,vt),c=c+Math.imul(lt,yt)|0,b=Math.imul(lt,vt),m=m+Math.imul(st,Pt)|0,c=c+Math.imul(st,Ct)|0,c=c+Math.imul(at,Pt)|0,b=b+Math.imul(at,Ct)|0,m=m+Math.imul(ot,kt)|0,c=c+Math.imul(ot,Mt)|0,c=c+Math.imul(rt,kt)|0,b=b+Math.imul(rt,Mt)|0,m=m+Math.imul(it,Tt)|0,c=c+Math.imul(it,At)|0,c=c+Math.imul(nt,Tt)|0,b=b+Math.imul(nt,At)|0,m=m+Math.imul(tt,St)|0,c=c+Math.imul(tt,xt)|0,c=c+Math.imul(et,St)|0,b=b+Math.imul(et,xt)|0;var Vi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Vi>>>26)|0,Vi&=67108863,m=Math.imul(ct,Pt),c=Math.imul(ct,Ct),c=c+Math.imul(lt,Pt)|0,b=Math.imul(lt,Ct),m=m+Math.imul(st,kt)|0,c=c+Math.imul(st,Mt)|0,c=c+Math.imul(at,kt)|0,b=b+Math.imul(at,Mt)|0,m=m+Math.imul(ot,Tt)|0,c=c+Math.imul(ot,At)|0,c=c+Math.imul(rt,Tt)|0,b=b+Math.imul(rt,At)|0,m=m+Math.imul(it,St)|0,c=c+Math.imul(it,xt)|0,c=c+Math.imul(nt,St)|0,b=b+Math.imul(nt,xt)|0;var Hi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Hi>>>26)|0,Hi&=67108863,m=Math.imul(ct,kt),c=Math.imul(ct,Mt),c=c+Math.imul(lt,kt)|0,b=Math.imul(lt,Mt),m=m+Math.imul(st,Tt)|0,c=c+Math.imul(st,At)|0,c=c+Math.imul(at,Tt)|0,b=b+Math.imul(at,At)|0,m=m+Math.imul(ot,St)|0,c=c+Math.imul(ot,xt)|0,c=c+Math.imul(rt,St)|0,b=b+Math.imul(rt,xt)|0;var Wi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Wi>>>26)|0,Wi&=67108863,m=Math.imul(ct,Tt),c=Math.imul(ct,At),c=c+Math.imul(lt,Tt)|0,b=Math.imul(lt,At),m=m+Math.imul(st,St)|0,c=c+Math.imul(st,xt)|0,c=c+Math.imul(at,St)|0,b=b+Math.imul(at,xt)|0;var Zi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Zi>>>26)|0,Zi&=67108863,m=Math.imul(ct,St),c=Math.imul(ct,xt),c=c+Math.imul(lt,St)|0,b=Math.imul(lt,xt);var zi=(C+m|0)+((c&8191)<<13)|0;return C=(b+(c>>>13)|0)+(zi>>>26)|0,zi&=67108863,v[0]=Ii,v[1]=Ei,v[2]=Oi,v[3]=Ri,v[4]=Fi,v[5]=Bi,v[6]=Ni,v[7]=ji,v[8]=qi,v[9]=Di,v[10]=Li,v[11]=$i,v[12]=Ui,v[13]=Ki,v[14]=Vi,v[15]=Hi,v[16]=Wi,v[17]=Zi,v[18]=zi,C!==0&&(v[19]=C,_.length++),_};Math.imul||(j=$);function K(y,s,u){u.negative=s.negative^y.negative,u.length=y.length+s.length;for(var _=0,p=0,g=0;g<u.length-1;g++){var v=p;p=0;for(var C=_&67108863,m=Math.min(g,s.length-1),c=Math.max(0,g-y.length+1);c<=m;c++){var b=g-c,A=y.words[b]|0,x=s.words[c]|0,E=A*x,q=E&67108863;v=v+(E/67108864|0)|0,q=q+C|0,C=q&67108863,v=v+(q>>>26)|0,p+=v>>>26,v&=67108863}u.words[g]=C,_=v,v=p}return _!==0?u.words[g]=_:u.length--,u._strip()}function B(y,s,u){return K(y,s,u)}n.prototype.mulTo=function(s,u){var _,p=this.length+s.length;return this.length===10&&s.length===10?_=j(this,s,u):p<63?_=$(this,s,u):p<1024?_=K(this,s,u):_=B(this,s,u),_};function D(y,s){this.x=y,this.y=s}D.prototype.makeRBT=function(s){for(var u=new Array(s),_=n.prototype._countBits(s)-1,p=0;p<s;p++)u[p]=this.revBin(p,_,s);return u},D.prototype.revBin=function(s,u,_){if(s===0||s===_-1)return s;for(var p=0,g=0;g<u;g++)p|=(s&1)<<u-g-1,s>>=1;return p},D.prototype.permute=function(s,u,_,p,g,v){for(var C=0;C<v;C++)p[C]=u[s[C]],g[C]=_[s[C]]},D.prototype.transform=function(s,u,_,p,g,v){this.permute(v,s,u,_,p,g);for(var C=1;C<g;C<<=1)for(var m=C<<1,c=Math.cos(2*Math.PI/m),b=Math.sin(2*Math.PI/m),A=0;A<g;A+=m)for(var x=c,E=b,q=0;q<C;q++){var U=_[A+q],V=p[A+q],zt=_[A+q+C],H=p[A+q+C],z=x*zt-E*H;H=x*H+E*zt,zt=z,_[A+q]=U+zt,p[A+q]=V+H,_[A+q+C]=U-zt,p[A+q+C]=V-H,q!==m&&(z=c*x-b*E,E=c*E+b*x,x=z)}},D.prototype.guessLen13b=function(s,u){var _=Math.max(u,s)|1,p=_&1,g=0;for(_=_/2|0;_;_=_>>>1)g++;return 1<<g+1+p},D.prototype.conjugate=function(s,u,_){if(!(_<=1))for(var p=0;p<_/2;p++){var g=s[p];s[p]=s[_-p-1],s[_-p-1]=g,g=u[p],u[p]=-u[_-p-1],u[_-p-1]=-g}},D.prototype.normalize13b=function(s,u){for(var _=0,p=0;p<u/2;p++){var g=Math.round(s[2*p+1]/u)*8192+Math.round(s[2*p]/u)+_;s[p]=g&67108863,g<67108864?_=0:_=g/67108864|0}return s},D.prototype.convert13b=function(s,u,_,p){for(var g=0,v=0;v<u;v++)g=g+(s[v]|0),_[2*v]=g&8191,g=g>>>13,_[2*v+1]=g&8191,g=g>>>13;for(v=2*u;v<p;++v)_[v]=0;e(g===0),e((g&-8192)===0)},D.prototype.stub=function(s){for(var u=new Array(s),_=0;_<s;_++)u[_]=0;return u},D.prototype.mulp=function(s,u,_){var p=2*this.guessLen13b(s.length,u.length),g=this.makeRBT(p),v=this.stub(p),C=new Array(p),m=new Array(p),c=new Array(p),b=new Array(p),A=new Array(p),x=new Array(p),E=_.words;E.length=p,this.convert13b(s.words,s.length,C,p),this.convert13b(u.words,u.length,b,p),this.transform(C,v,m,c,p,g),this.transform(b,v,A,x,p,g);for(var q=0;q<p;q++){var U=m[q]*A[q]-c[q]*x[q];c[q]=m[q]*x[q]+c[q]*A[q],m[q]=U}return this.conjugate(m,c,p),this.transform(m,c,E,v,p,g),this.conjugate(E,v,p),this.normalize13b(E,p),_.negative=s.negative^u.negative,_.length=s.length+u.length,_._strip()},n.prototype.mul=function(s){var u=new n(null);return u.words=new Array(this.length+s.length),this.mulTo(s,u)},n.prototype.mulf=function(s){var u=new n(null);return u.words=new Array(this.length+s.length),B(this,s,u)},n.prototype.imul=function(s){return this.clone().mulTo(s,this)},n.prototype.imuln=function(s){var u=s<0;u&&(s=-s),e(typeof s=="number"),e(s<67108864);for(var _=0,p=0;p<this.length;p++){var g=(this.words[p]|0)*s,v=(g&67108863)+(_&67108863);_>>=26,_+=g/67108864|0,_+=v>>>26,this.words[p]=v&67108863}return _!==0&&(this.words[p]=_,this.length++),this.length=s===0?1:this.length,u?this.ineg():this},n.prototype.muln=function(s){return this.clone().imuln(s)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(s){var u=S(s);if(u.length===0)return new n(1);for(var _=this,p=0;p<u.length&&u[p]===0;p++,_=_.sqr());if(++p<u.length)for(var g=_.sqr();p<u.length;p++,g=g.sqr())u[p]!==0&&(_=_.mul(g));return _},n.prototype.iushln=function(s){e(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26,p=67108863>>>26-u<<26-u,g;if(u!==0){var v=0;for(g=0;g<this.length;g++){var C=this.words[g]&p,m=(this.words[g]|0)-C<<u;this.words[g]=m|v,v=C>>>26-u}v&&(this.words[g]=v,this.length++)}if(_!==0){for(g=this.length-1;g>=0;g--)this.words[g+_]=this.words[g];for(g=0;g<_;g++)this.words[g]=0;this.length+=_}return this._strip()},n.prototype.ishln=function(s){return e(this.negative===0),this.iushln(s)},n.prototype.iushrn=function(s,u,_){e(typeof s=="number"&&s>=0);var p;u?p=(u-u%26)/26:p=0;var g=s%26,v=Math.min((s-g)/26,this.length),C=67108863^67108863>>>g<<g,m=_;if(p-=v,p=Math.max(0,p),m){for(var c=0;c<v;c++)m.words[c]=this.words[c];m.length=v}if(v!==0)if(this.length>v)for(this.length-=v,c=0;c<this.length;c++)this.words[c]=this.words[c+v];else this.words[0]=0,this.length=1;var b=0;for(c=this.length-1;c>=0&&(b!==0||c>=p);c--){var A=this.words[c]|0;this.words[c]=b<<26-g|A>>>g,b=A&C}return m&&b!==0&&(m.words[m.length++]=b),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},n.prototype.ishrn=function(s,u,_){return e(this.negative===0),this.iushrn(s,u,_)},n.prototype.shln=function(s){return this.clone().ishln(s)},n.prototype.ushln=function(s){return this.clone().iushln(s)},n.prototype.shrn=function(s){return this.clone().ishrn(s)},n.prototype.ushrn=function(s){return this.clone().iushrn(s)},n.prototype.testn=function(s){e(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26,p=1<<u;if(this.length<=_)return!1;var g=this.words[_];return!!(g&p)},n.prototype.imaskn=function(s){e(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26;if(e(this.negative===0,"imaskn works only with positive numbers"),this.length<=_)return this;if(u!==0&&_++,this.length=Math.min(_,this.length),u!==0){var p=67108863^67108863>>>u<<u;this.words[this.length-1]&=p}return this._strip()},n.prototype.maskn=function(s){return this.clone().imaskn(s)},n.prototype.iaddn=function(s){return e(typeof s=="number"),e(s<67108864),s<0?this.isubn(-s):this.negative!==0?this.length===1&&(this.words[0]|0)<=s?(this.words[0]=s-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(s),this.negative=1,this):this._iaddn(s)},n.prototype._iaddn=function(s){this.words[0]+=s;for(var u=0;u<this.length&&this.words[u]>=67108864;u++)this.words[u]-=67108864,u===this.length-1?this.words[u+1]=1:this.words[u+1]++;return this.length=Math.max(this.length,u+1),this},n.prototype.isubn=function(s){if(e(typeof s=="number"),e(s<67108864),s<0)return this.iaddn(-s);if(this.negative!==0)return this.negative=0,this.iaddn(s),this.negative=1,this;if(this.words[0]-=s,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var u=0;u<this.length&&this.words[u]<0;u++)this.words[u]+=67108864,this.words[u+1]-=1;return this._strip()},n.prototype.addn=function(s){return this.clone().iaddn(s)},n.prototype.subn=function(s){return this.clone().isubn(s)},n.prototype.iabs=function(){return this.negative=0,this},n.prototype.abs=function(){return this.clone().iabs()},n.prototype._ishlnsubmul=function(s,u,_){var p=s.length+_,g;this._expand(p);var v,C=0;for(g=0;g<s.length;g++){v=(this.words[g+_]|0)+C;var m=(s.words[g]|0)*u;v-=m&67108863,C=(v>>26)-(m/67108864|0),this.words[g+_]=v&67108863}for(;g<this.length-_;g++)v=(this.words[g+_]|0)+C,C=v>>26,this.words[g+_]=v&67108863;if(C===0)return this._strip();for(e(C===-1),C=0,g=0;g<this.length;g++)v=-(this.words[g]|0)+C,C=v>>26,this.words[g]=v&67108863;return this.negative=1,this._strip()},n.prototype._wordDiv=function(s,u){var _=this.length-s.length,p=this.clone(),g=s,v=g.words[g.length-1]|0,C=this._countBits(v);_=26-C,_!==0&&(g=g.ushln(_),p.iushln(_),v=g.words[g.length-1]|0);var m=p.length-g.length,c;if(u!=="mod"){c=new n(null),c.length=m+1,c.words=new Array(c.length);for(var b=0;b<c.length;b++)c.words[b]=0}var A=p.clone()._ishlnsubmul(g,1,m);A.negative===0&&(p=A,c&&(c.words[m]=1));for(var x=m-1;x>=0;x--){var E=(p.words[g.length+x]|0)*67108864+(p.words[g.length+x-1]|0);for(E=Math.min(E/v|0,67108863),p._ishlnsubmul(g,E,x);p.negative!==0;)E--,p.negative=0,p._ishlnsubmul(g,1,x),p.isZero()||(p.negative^=1);c&&(c.words[x]=E)}return c&&c._strip(),p._strip(),u!=="div"&&_!==0&&p.iushrn(_),{div:c||null,mod:p}},n.prototype.divmod=function(s,u,_){if(e(!s.isZero()),this.isZero())return{div:new n(0),mod:new n(0)};var p,g,v;return this.negative!==0&&s.negative===0?(v=this.neg().divmod(s,u),u!=="mod"&&(p=v.div.neg()),u!=="div"&&(g=v.mod.neg(),_&&g.negative!==0&&g.iadd(s)),{div:p,mod:g}):this.negative===0&&s.negative!==0?(v=this.divmod(s.neg(),u),u!=="mod"&&(p=v.div.neg()),{div:p,mod:v.mod}):(this.negative&s.negative)!==0?(v=this.neg().divmod(s.neg(),u),u!=="div"&&(g=v.mod.neg(),_&&g.negative!==0&&g.isub(s)),{div:v.div,mod:g}):s.length>this.length||this.cmp(s)<0?{div:new n(0),mod:this}:s.length===1?u==="div"?{div:this.divn(s.words[0]),mod:null}:u==="mod"?{div:null,mod:new n(this.modrn(s.words[0]))}:{div:this.divn(s.words[0]),mod:new n(this.modrn(s.words[0]))}:this._wordDiv(s,u)},n.prototype.div=function(s){return this.divmod(s,"div",!1).div},n.prototype.mod=function(s){return this.divmod(s,"mod",!1).mod},n.prototype.umod=function(s){return this.divmod(s,"mod",!0).mod},n.prototype.divRound=function(s){var u=this.divmod(s);if(u.mod.isZero())return u.div;var _=u.div.negative!==0?u.mod.isub(s):u.mod,p=s.ushrn(1),g=s.andln(1),v=_.cmp(p);return v<0||g===1&&v===0?u.div:u.div.negative!==0?u.div.isubn(1):u.div.iaddn(1)},n.prototype.modrn=function(s){var u=s<0;u&&(s=-s),e(s<=67108863);for(var _=(1<<26)%s,p=0,g=this.length-1;g>=0;g--)p=(_*p+(this.words[g]|0))%s;return u?-p:p},n.prototype.modn=function(s){return this.modrn(s)},n.prototype.idivn=function(s){var u=s<0;u&&(s=-s),e(s<=67108863);for(var _=0,p=this.length-1;p>=0;p--){var g=(this.words[p]|0)+_*67108864;this.words[p]=g/s|0,_=g%s}return this._strip(),u?this.ineg():this},n.prototype.divn=function(s){return this.clone().idivn(s)},n.prototype.egcd=function(s){e(s.negative===0),e(!s.isZero());var u=this,_=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var p=new n(1),g=new n(0),v=new n(0),C=new n(1),m=0;u.isEven()&&_.isEven();)u.iushrn(1),_.iushrn(1),++m;for(var c=_.clone(),b=u.clone();!u.isZero();){for(var A=0,x=1;(u.words[0]&x)===0&&A<26;++A,x<<=1);if(A>0)for(u.iushrn(A);A-- >0;)(p.isOdd()||g.isOdd())&&(p.iadd(c),g.isub(b)),p.iushrn(1),g.iushrn(1);for(var E=0,q=1;(_.words[0]&q)===0&&E<26;++E,q<<=1);if(E>0)for(_.iushrn(E);E-- >0;)(v.isOdd()||C.isOdd())&&(v.iadd(c),C.isub(b)),v.iushrn(1),C.iushrn(1);u.cmp(_)>=0?(u.isub(_),p.isub(v),g.isub(C)):(_.isub(u),v.isub(p),C.isub(g))}return{a:v,b:C,gcd:_.iushln(m)}},n.prototype._invmp=function(s){e(s.negative===0),e(!s.isZero());var u=this,_=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var p=new n(1),g=new n(0),v=_.clone();u.cmpn(1)>0&&_.cmpn(1)>0;){for(var C=0,m=1;(u.words[0]&m)===0&&C<26;++C,m<<=1);if(C>0)for(u.iushrn(C);C-- >0;)p.isOdd()&&p.iadd(v),p.iushrn(1);for(var c=0,b=1;(_.words[0]&b)===0&&c<26;++c,b<<=1);if(c>0)for(_.iushrn(c);c-- >0;)g.isOdd()&&g.iadd(v),g.iushrn(1);u.cmp(_)>=0?(u.isub(_),p.isub(g)):(_.isub(u),g.isub(p))}var A;return u.cmpn(1)===0?A=p:A=g,A.cmpn(0)<0&&A.iadd(s),A},n.prototype.gcd=function(s){if(this.isZero())return s.abs();if(s.isZero())return this.abs();var u=this.clone(),_=s.clone();u.negative=0,_.negative=0;for(var p=0;u.isEven()&&_.isEven();p++)u.iushrn(1),_.iushrn(1);do{for(;u.isEven();)u.iushrn(1);for(;_.isEven();)_.iushrn(1);var g=u.cmp(_);if(g<0){var v=u;u=_,_=v}else if(g===0||_.cmpn(1)===0)break;u.isub(_)}while(!0);return _.iushln(p)},n.prototype.invm=function(s){return this.egcd(s).a.umod(s)},n.prototype.isEven=function(){return(this.words[0]&1)===0},n.prototype.isOdd=function(){return(this.words[0]&1)===1},n.prototype.andln=function(s){return this.words[0]&s},n.prototype.bincn=function(s){e(typeof s=="number");var u=s%26,_=(s-u)/26,p=1<<u;if(this.length<=_)return this._expand(_+1),this.words[_]|=p,this;for(var g=p,v=_;g!==0&&v<this.length;v++){var C=this.words[v]|0;C+=g,g=C>>>26,C&=67108863,this.words[v]=C}return g!==0&&(this.words[v]=g,this.length++),this},n.prototype.isZero=function(){return this.length===1&&this.words[0]===0},n.prototype.cmpn=function(s){var u=s<0;if(this.negative!==0&&!u)return-1;if(this.negative===0&&u)return 1;this._strip();var _;if(this.length>1)_=1;else{u&&(s=-s),e(s<=67108863,"Number is too big");var p=this.words[0]|0;_=p===s?0:p<s?-1:1}return this.negative!==0?-_|0:_},n.prototype.cmp=function(s){if(this.negative!==0&&s.negative===0)return-1;if(this.negative===0&&s.negative!==0)return 1;var u=this.ucmp(s);return this.negative!==0?-u|0:u},n.prototype.ucmp=function(s){if(this.length>s.length)return 1;if(this.length<s.length)return-1;for(var u=0,_=this.length-1;_>=0;_--){var p=this.words[_]|0,g=s.words[_]|0;if(p!==g){p<g?u=-1:p>g&&(u=1);break}}return u},n.prototype.gtn=function(s){return this.cmpn(s)===1},n.prototype.gt=function(s){return this.cmp(s)===1},n.prototype.gten=function(s){return this.cmpn(s)>=0},n.prototype.gte=function(s){return this.cmp(s)>=0},n.prototype.ltn=function(s){return this.cmpn(s)===-1},n.prototype.lt=function(s){return this.cmp(s)===-1},n.prototype.lten=function(s){return this.cmpn(s)<=0},n.prototype.lte=function(s){return this.cmp(s)<=0},n.prototype.eqn=function(s){return this.cmpn(s)===0},n.prototype.eq=function(s){return this.cmp(s)===0},n.red=function(s){return new L(s)},n.prototype.toRed=function(s){return e(!this.red,"Already a number in reduction context"),e(this.negative===0,"red works only with positives"),s.convertTo(this)._forceRed(s)},n.prototype.fromRed=function(){return e(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},n.prototype._forceRed=function(s){return this.red=s,this},n.prototype.forceRed=function(s){return e(!this.red,"Already a number in reduction context"),this._forceRed(s)},n.prototype.redAdd=function(s){return e(this.red,"redAdd works only with red numbers"),this.red.add(this,s)},n.prototype.redIAdd=function(s){return e(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,s)},n.prototype.redSub=function(s){return e(this.red,"redSub works only with red numbers"),this.red.sub(this,s)},n.prototype.redISub=function(s){return e(this.red,"redISub works only with red numbers"),this.red.isub(this,s)},n.prototype.redShl=function(s){return e(this.red,"redShl works only with red numbers"),this.red.shl(this,s)},n.prototype.redMul=function(s){return e(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.mul(this,s)},n.prototype.redIMul=function(s){return e(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.imul(this,s)},n.prototype.redSqr=function(){return e(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return e(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return e(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return e(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return e(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(s){return e(this.red&&!s.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,s)};var jt={k256:null,p224:null,p192:null,p25519:null};function Z(y,s){this.name=y,this.p=new n(s,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}Z.prototype._tmp=function(){var s=new n(null);return s.words=new Array(Math.ceil(this.n/13)),s},Z.prototype.ireduce=function(s){var u=s,_;do this.split(u,this.tmp),u=this.imulK(u),u=u.iadd(this.tmp),_=u.bitLength();while(_>this.n);var p=_<this.n?-1:u.ucmp(this.p);return p===0?(u.words[0]=0,u.length=1):p>0?u.isub(this.p):u.strip!==void 0?u.strip():u._strip(),u},Z.prototype.split=function(s,u){s.iushrn(this.n,0,u)},Z.prototype.imulK=function(s){return s.imul(this.k)};function Lt(){Z.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}i(Lt,Z),Lt.prototype.split=function(s,u){for(var _=4194303,p=Math.min(s.length,9),g=0;g<p;g++)u.words[g]=s.words[g];if(u.length=p,s.length<=9){s.words[0]=0,s.length=1;return}var v=s.words[9];for(u.words[u.length++]=v&_,g=10;g<s.length;g++){var C=s.words[g]|0;s.words[g-10]=(C&_)<<4|v>>>22,v=C}v>>>=22,s.words[g-10]=v,v===0&&s.length>10?s.length-=10:s.length-=9},Lt.prototype.imulK=function(s){s.words[s.length]=0,s.words[s.length+1]=0,s.length+=2;for(var u=0,_=0;_<s.length;_++){var p=s.words[_]|0;u+=p*977,s.words[_]=u&67108863,u=p*64+(u/67108864|0)}return s.words[s.length-1]===0&&(s.length--,s.words[s.length-1]===0&&s.length--),s};function ne(){Z.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}i(ne,Z);function Zt(){Z.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}i(Zt,Z);function oe(){Z.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}i(oe,Z),oe.prototype.imulK=function(s){for(var u=0,_=0;_<s.length;_++){var p=(s.words[_]|0)*19+u,g=p&67108863;p>>>=26,s.words[_]=g,u=p}return u!==0&&(s.words[s.length++]=u),s},n._prime=function(s){if(jt[s])return jt[s];var u;if(s==="k256")u=new Lt;else if(s==="p224")u=new ne;else if(s==="p192")u=new Zt;else if(s==="p25519")u=new oe;else throw new Error("Unknown prime "+s);return jt[s]=u,u};function L(y){if(typeof y=="string"){var s=n._prime(y);this.m=s.p,this.prime=s}else e(y.gtn(1),"modulus must be greater than 1"),this.m=y,this.prime=null}L.prototype._verify1=function(s){e(s.negative===0,"red works only with positives"),e(s.red,"red works only with red numbers")},L.prototype._verify2=function(s,u){e((s.negative|u.negative)===0,"red works only with positives"),e(s.red&&s.red===u.red,"red works only with red numbers")},L.prototype.imod=function(s){return this.prime?this.prime.ireduce(s)._forceRed(this):(f(s,s.umod(this.m)._forceRed(this)),s)},L.prototype.neg=function(s){return s.isZero()?s.clone():this.m.sub(s)._forceRed(this)},L.prototype.add=function(s,u){this._verify2(s,u);var _=s.add(u);return _.cmp(this.m)>=0&&_.isub(this.m),_._forceRed(this)},L.prototype.iadd=function(s,u){this._verify2(s,u);var _=s.iadd(u);return _.cmp(this.m)>=0&&_.isub(this.m),_},L.prototype.sub=function(s,u){this._verify2(s,u);var _=s.sub(u);return _.cmpn(0)<0&&_.iadd(this.m),_._forceRed(this)},L.prototype.isub=function(s,u){this._verify2(s,u);var _=s.isub(u);return _.cmpn(0)<0&&_.iadd(this.m),_},L.prototype.shl=function(s,u){return this._verify1(s),this.imod(s.ushln(u))},L.prototype.imul=function(s,u){return this._verify2(s,u),this.imod(s.imul(u))},L.prototype.mul=function(s,u){return this._verify2(s,u),this.imod(s.mul(u))},L.prototype.isqr=function(s){return this.imul(s,s.clone())},L.prototype.sqr=function(s){return this.mul(s,s)},L.prototype.sqrt=function(s){if(s.isZero())return s.clone();var u=this.m.andln(3);if(e(u%2===1),u===3){var _=this.m.add(new n(1)).iushrn(2);return this.pow(s,_)}for(var p=this.m.subn(1),g=0;!p.isZero()&&p.andln(1)===0;)g++,p.iushrn(1);e(!p.isZero());var v=new n(1).toRed(this),C=v.redNeg(),m=this.m.subn(1).iushrn(1),c=this.m.bitLength();for(c=new n(2*c*c).toRed(this);this.pow(c,m).cmp(C)!==0;)c.redIAdd(C);for(var b=this.pow(c,p),A=this.pow(s,p.addn(1).iushrn(1)),x=this.pow(s,p),E=g;x.cmp(v)!==0;){for(var q=x,U=0;q.cmp(v)!==0;U++)q=q.redSqr();e(U<E);var V=this.pow(b,new n(1).iushln(E-U-1));A=A.redMul(V),b=V.redSqr(),x=x.redMul(b),E=U}return A},L.prototype.invm=function(s){var u=s._invmp(this.m);return u.negative!==0?(u.negative=0,this.imod(u).redNeg()):this.imod(u)},L.prototype.pow=function(s,u){if(u.isZero())return new n(1).toRed(this);if(u.cmpn(1)===0)return s.clone();var _=4,p=new Array(1<<_);p[0]=new n(1).toRed(this),p[1]=s;for(var g=2;g<p.length;g++)p[g]=this.mul(p[g-1],s);var v=p[0],C=0,m=0,c=u.bitLength()%26;for(c===0&&(c=26),g=u.length-1;g>=0;g--){for(var b=u.words[g],A=c-1;A>=0;A--){var x=b>>A&1;if(v!==p[0]&&(v=this.sqr(v)),x===0&&C===0){m=0;continue}C<<=1,C|=x,m++,!(m!==_&&(g!==0||A!==0))&&(v=this.mul(v,p[C]),m=0,C=0)}c=26}return v},L.prototype.convertTo=function(s){var u=s.umod(this.m);return u===s?u.clone():u},L.prototype.convertFrom=function(s){var u=s.clone();return u.red=null,u},n.mont=function(s){return new Bt(s)};function Bt(y){L.call(this,y),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new n(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)}i(Bt,L),Bt.prototype.convertTo=function(s){return this.imod(s.ushln(this.shift))},Bt.prototype.convertFrom=function(s){var u=this.imod(s.mul(this.rinv));return u.red=null,u},Bt.prototype.imul=function(s,u){if(s.isZero()||u.isZero())return s.words[0]=0,s.length=1,s;var _=s.imul(u),p=_.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=_.isub(p).iushrn(this.shift),v=g;return g.cmp(this.m)>=0?v=g.isub(this.m):g.cmpn(0)<0&&(v=g.iadd(this.m)),v._forceRed(this)},Bt.prototype.mul=function(s,u){if(s.isZero()||u.isZero())return new n(0)._forceRed(this);var _=s.mul(u),p=_.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=_.isub(p).iushrn(this.shift),v=g;return g.cmp(this.m)>=0?v=g.isub(this.m):g.cmpn(0)<0&&(v=g.iadd(this.m)),v._forceRed(this)},Bt.prototype.invm=function(s){var u=this.imod(s._invmp(this.m).mul(this.r2));return u._forceRed(this)}})(typeof on>"u"||on,_o)});import{SdkWrapper as ba}from"@cetusprotocol/common-sdk";import{normalizeSuiObjectId as tn}from"@mysten/sui/utils";import{CACHE_TIME_24H as Ht,CACHE_TIME_5MIN as Fr,CachedContent as Br,DETAILS_KEYS as Le,extractStructTagFromType as en,fixCoinType as Nr,getFutureTime as uo,getObjectFields as ti,getObjectId as nn,getObjectPreviousTransactionDigest as jr,getObjectType as qr,getPackagerConfigs as le,normalizeCoinType as fo}from"@cetusprotocol/common-sdk";var Zn="3.7.7",Pr=Zn,Pe=typeof Buffer=="function",Un=typeof TextDecoder=="function"?new TextDecoder:void 0,Kn=typeof TextEncoder=="function"?new TextEncoder:void 0,Cr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",qe=Array.prototype.slice.call(Cr),Qe=(o=>{let t={};return o.forEach((e,i)=>t[e]=i),t})(qe),kr=/^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/,Et=String.fromCharCode.bind(String),Vn=typeof Uint8Array.from=="function"?Uint8Array.from.bind(Uint8Array):o=>new Uint8Array(Array.prototype.slice.call(o,0)),zn=o=>o.replace(/=/g,"").replace(/[+\/]/g,t=>t=="+"?"-":"_"),Gn=o=>o.replace(/[^A-Za-z0-9\+\/]/g,""),Qn=o=>{let t,e,i,n,r="",a=o.length%3;for(let l=0;l<o.length;){if((e=o.charCodeAt(l++))>255||(i=o.charCodeAt(l++))>255||(n=o.charCodeAt(l++))>255)throw new TypeError("invalid character found");t=e<<16|i<<8|n,r+=qe[t>>18&63]+qe[t>>12&63]+qe[t>>6&63]+qe[t&63]}return a?r.slice(0,a-3)+"===".substring(a):r},Ji=typeof btoa=="function"?o=>btoa(o):Pe?o=>Buffer.from(o,"binary").toString("base64"):Qn,Gi=Pe?o=>Buffer.from(o).toString("base64"):o=>{let e=[];for(let i=0,n=o.length;i<n;i+=4096)e.push(Et.apply(null,o.subarray(i,i+4096)));return Ji(e.join(""))},Je=(o,t=!1)=>t?zn(Gi(o)):Gi(o),Mr=o=>{if(o.length<2){var t=o.charCodeAt(0);return t<128?o:t<2048?Et(192|t>>>6)+Et(128|t&63):Et(224|t>>>12&15)+Et(128|t>>>6&63)+Et(128|t&63)}else{var t=65536+(o.charCodeAt(0)-55296)*1024+(o.charCodeAt(1)-56320);return Et(240|t>>>18&7)+Et(128|t>>>12&63)+Et(128|t>>>6&63)+Et(128|t&63)}},Tr=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g,Jn=o=>o.replace(Tr,Mr),Hn=Pe?o=>Buffer.from(o,"utf8").toString("base64"):Kn?o=>Gi(Kn.encode(o)):o=>Ji(Jn(o)),ve=(o,t=!1)=>t?zn(Hn(o)):Hn(o),Wn=o=>ve(o,!0),Ar=/[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g,Sr=o=>{switch(o.length){case 4:var t=(7&o.charCodeAt(0))<<18|(63&o.charCodeAt(1))<<12|(63&o.charCodeAt(2))<<6|63&o.charCodeAt(3),e=t-65536;return Et((e>>>10)+55296)+Et((e&1023)+56320);case 3:return Et((15&o.charCodeAt(0))<<12|(63&o.charCodeAt(1))<<6|63&o.charCodeAt(2));default:return Et((31&o.charCodeAt(0))<<6|63&o.charCodeAt(1))}},Xn=o=>o.replace(Ar,Sr),Yn=o=>{if(o=o.replace(/\s+/g,""),!kr.test(o))throw new TypeError("malformed base64.");o+="==".slice(2-(o.length&3));let t,e="",i,n;for(let r=0;r<o.length;)t=Qe[o.charAt(r++)]<<18|Qe[o.charAt(r++)]<<12|(i=Qe[o.charAt(r++)])<<6|(n=Qe[o.charAt(r++)]),e+=i===64?Et(t>>16&255):n===64?Et(t>>16&255,t>>8&255):Et(t>>16&255,t>>8&255,t&255);return e},Xi=typeof atob=="function"?o=>atob(Gn(o)):Pe?o=>Buffer.from(o,"base64").toString("binary"):Yn,to=Pe?o=>Vn(Buffer.from(o,"base64")):o=>Vn(Xi(o).split("").map(t=>t.charCodeAt(0))),eo=o=>to(io(o)),xr=Pe?o=>Buffer.from(o,"base64").toString("utf8"):Un?o=>Un.decode(to(o)):o=>Xn(Xi(o)),io=o=>Gn(o.replace(/[-_]/g,t=>t=="-"?"+":"/")),Qi=o=>xr(io(o)),Ir=o=>{if(typeof o!="string")return!1;let t=o.replace(/\s+/g,"").replace(/={0,2}$/,"");return!/[^\s0-9a-zA-Z\+/]/.test(t)||!/[^\s0-9a-zA-Z\-_]/.test(t)},no=o=>({value:o,enumerable:!1,writable:!0,configurable:!0}),oo=function(){let o=(t,e)=>Object.defineProperty(String.prototype,t,no(e));o("fromBase64",function(){return Qi(this)}),o("toBase64",function(t){return ve(this,t)}),o("toBase64URI",function(){return ve(this,!0)}),o("toBase64URL",function(){return ve(this,!0)}),o("toUint8Array",function(){return eo(this)})},ro=function(){let o=(t,e)=>Object.defineProperty(Uint8Array.prototype,t,no(e));o("toBase64",function(t){return Je(this,t)}),o("toBase64URI",function(){return Je(this,!0)}),o("toBase64URL",function(){return Je(this,!0)})},Er=()=>{oo(),ro()},Xe={version:Zn,VERSION:Pr,atob:Xi,atobPolyfill:Yn,btoa:Ji,btoaPolyfill:Qn,fromBase64:Qi,toBase64:ve,encode:ve,encodeURI:Wn,encodeURL:Wn,utob:Jn,btou:Xn,decode:Qi,isValid:Ir,fromUint8Array:Je,toUint8Array:eo,extendString:oo,extendUint8Array:ro,extendBuiltins:Er};import{BaseError as Or}from"@cetusprotocol/common-sdk";var so=(f=>(f.InvalidSqrtPriceLimitDirection="InvalidSqrtPriceLimitDirection",f.ZeroTradableAmount="ZeroTradableAmount",f.AmountOutBelowMinimum="AmountOutBelowMinimum",f.AmountInAboveMaximum="AmountInAboveMaximum",f.NextTickNotFound="NextTickNotFound",f.TickArraySequenceInvalid="TickArraySequenceInvalid",f.TickArrayCrossingAboveMax="TickArrayCrossingAboveMax",f.TickArrayIndexNotInitialized="TickArrayIndexNotInitialized",f.ParamsLengthNotEqual="ParamsLengthNotEqual",f))(so||{}),Ye=(r=>(r.InvalidTickEvent="InvalidTickEvent",r.InvalidPositionObject="InvalidPositionObject",r.InvalidPositionRewardObject="InvalidPositionRewardObject",r.InvalidParams="InvalidParams",r.FetchError="FetchError",r))(Ye||{}),De=(f=>(f.InvalidCoinTypeSequence="InvalidCoinTypeSequence",f.InvalidTickIndex="InvalidTickIndex",f.InvalidPoolObject="InvalidPoolObject",f.InvalidTickObjectId="InvalidTickObjectId",f.InvalidTickObject="InvalidTickObject",f.InvalidTickFields="InvalidTickFields",f.PoolsNotFound="PoolsNotFound",f.StatsPoolsUrlNotSet="StatsPoolsUrlNotSet",f.FetchError="FetchError",f))(De||{}),ao=(e=>(e.ClmmVestNotSet="ClmmVestNotSet",e.ClmmVestFetchError="ClmmVestFetchError",e))(ao||{}),co=(e=>(e.NotFoundPartnerObject="NotFoundPartnerObject",e.InvalidPartnerRefFeeFields="InvalidPartnerRefFeeFields",e))(co||{}),ke=(i=>(i.InvalidConfig="InvalidConfig",i.InvalidConfigHandle="InvalidConfigHandle",i.InvalidSimulateAccount="InvalidSimulateAccount",i))(ke||{}),Yi=(a=>(a.InvalidSendAddress="InvalidSendAddress",a.InvalidRecipientAddress="InvalidRecipientAddress",a.InvalidRecipientAndAmountLength="InvalidRecipientAndAmountLength",a.InsufficientBalance="InsufficientBalance",a.InvalidTarget="InvalidTarget",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a))(Yi||{}),Rr=(a=>(a.InvalidCoin="InvalidCoin",a.NotFoundPath="NotFoundPath",a.NoDowngradeNeedParams="NoDowngradeNeedParams",a.InvalidSwapCountUrl="InvalidSwapCountUrl",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a.InvalidServerResponse="InvalidServerResponse",a))(Rr||{}),lo=(t=>(t.InvalidType="InvalidType",t))(lo||{}),Ce=class extends Or{constructor(t,e,i){super(t,e||"UnknownError",i)}static isVaultsErrorCode(t,e){return this.isErrorCode(t,e)}},$t=(o,t,e)=>{throw new Ce(t.message,o,e)},R=(o,t,e)=>{throw new Ce(t,o,e)};var ei=class{constructor(t){this._cache={};this._sdk=t}get sdk(){return this._sdk}setTokenListCache(t){let{coin_list_handle:e}=le(this.sdk.sdkOptions.cetus_config),i=`${e}_getCoinConfigs`,n=this.getCache(i),r=n?[...n,...t]:t;this.updateCache(i,r)}async getTokenListByCoinTypes(t){let e={},{coin_list_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getCoinConfigs`,r=this.getCache(n);if(r!==void 0){let l=r;for(let d of t)for(let f of l)if(fo(d)===fo(f.address)){e[d]=f;continue}}let a=t.filter(l=>e[l]===void 0);for(let l of a){let d=`${l}_metadata`,f=this.getCache(d);if(f!==void 0)e[l]=f;else{let h=await this._sdk.FullClient.getCoinMetadata({coinType:l});if(h){let w={id:h.id,pyth_id:"",name:h.name,symbol:h.symbol,official_symbol:h.symbol,coingecko_id:"",decimals:h.decimals,project_url:"",logo_url:h.iconUrl,address:l};e[l]=w,this.updateCache(d,w,Ht)}else console.log(`not found ${l}`)}}return e}async getCoinConfigs(t=!1,e=!0){let{coin_list_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getCoinConfigs`,r=this.getCache(n,t);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getCoinConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[Le.METHOD_NAME]:"getCoinConfigs"});let w=this.buildCoinConfig(h,e);this.updateCache(`${i}_${w.address}_getCoinConfig`,w,Ht),f.push({...w})}),this.updateCache(n,f,Ht),f}async getCoinConfig(t,e=!1,i=!0){let{coin_list_handle:n}=le(this.sdk.sdkOptions.cetus_config),r=`${n}_${t}_getCoinConfig`,a=this.getCache(r,e);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"0x1::type_name::TypeName",value:{name:Nr(t)}}});(l.error!=null||l.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getCoinConfig get object error: ${l.error}, please check the rpc and contracts address config.`,{[Le.METHOD_NAME]:"getCoinConfig"});let d=this.buildCoinConfig(l,i);return this.updateCache(r,d,Ht),d}buildCoinConfig(t,e=!0){let i=ti(t);i=i.value.fields;let n={...i};return n.id=nn(t),n.address=en(i.coin_type.fields.name).full_address,i.pyth_id&&(n.pyth_id=tn(i.pyth_id)),this.transformExtensions(n,i.extension_fields.fields.contents,e),delete n.coin_type,n}async getClmmPoolConfigs(t=!1,e=!0){let{clmm_pools_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getClmmPoolConfigs`,r=this.getCache(n,t);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getClmmPoolsConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[Le.METHOD_NAME]:"getClmmPoolConfigs"});let w=this.buildClmmPoolConfig(h,e);this.updateCache(`${w.pool_address}_getClmmPoolConfig`,w,Ht),f.push({...w})}),this.updateCache(n,f,Ht),f}async getClmmPoolConfig(t,e=!1,i=!0){let{clmm_pools_handle:n}=le(this.sdk.sdkOptions.cetus_config),r=`${t}_getClmmPoolConfig`,a=this.getCache(r,e);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"address",value:t}}),d=this.buildClmmPoolConfig(l,i);return this.updateCache(r,d,Ht),d}buildClmmPoolConfig(t,e=!0){let i=ti(t);i=i.value.fields;let n={...i};return n.id=nn(t),n.pool_address=tn(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,e),n}async getLaunchpadPoolConfigs(t=!1,e=!0){let{launchpad_pools_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getLaunchpadPoolConfigs`,r=this.getCache(n,t);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getCoinConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[Le.METHOD_NAME]:"getLaunchpadPoolConfigs"});let w=this.buildLaunchpadPoolConfig(h,e);this.updateCache(`${w.pool_address}_getLaunchpadPoolConfig`,w,Ht),f.push({...w})}),this.updateCache(n,f,Ht),f}async getLaunchpadPoolConfig(t,e=!1,i=!0){let{launchpad_pools_handle:n}=le(this.sdk.sdkOptions.cetus_config),r=`${t}_getLaunchpadPoolConfig`,a=this.getCache(r,e);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"address",value:t}}),d=this.buildLaunchpadPoolConfig(l,i);return this.updateCache(r,d,Ht),d}buildLaunchpadPoolConfig(t,e=!0){let i=ti(t);i=i.value.fields;let n={...i};n.id=nn(t),n.pool_address=tn(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,e);let r=[];i.social_media.fields.contents.forEach(a=>{r.push({name:a.fields.value.fields.name,link:a.fields.value.fields.link})}),n.social_media=r;try{n.regulation=decodeURIComponent(Xe.decode(n.regulation).replace(/%/g,"%25"))}catch{n.regulation=Xe.decode(n.regulation)}return n}transformExtensions(t,e,i=!0){let n=[];for(let r of e){let{key:a}=r.fields,{value:l}=r.fields;if(a==="labels")try{let d=decodeURIComponent(Xe.decode(l));try{l=JSON.parse(d)}catch{l=d}}catch{}i&&(t[a]=l),n.push({key:a,value:l})}delete t.extension_fields,i||(t.extensions=n)}async getCetusConfig(t=!1){let e=this._sdk.sdkOptions.cetus_config.package_id,i=`${e}_getCetusConfig`,n=this.getCache(i,t);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:e,options:{showPreviousTransaction:!0}}),a=jr(r),l=await this._sdk.FullClient.queryEventsByPage({Transaction:a}),d={coin_list_id:"",launchpad_pools_id:"",clmm_pools_id:"",admin_cap_id:"",global_config_id:"",coin_list_handle:"",launchpad_pools_handle:"",clmm_pools_handle:""};if(l.data.length>0)for(let f of l.data)switch(en(f.type).name){case"InitCoinListEvent":d.coin_list_id=f.parsedJson.coin_list_id;break;case"InitLaunchpadPoolsEvent":d.launchpad_pools_id=f.parsedJson.launchpad_pools_id;break;case"InitClmmPoolsEvent":d.clmm_pools_id=f.parsedJson.pools_id;break;case"InitConfigEvent":d.global_config_id=f.parsedJson.global_config_id,d.admin_cap_id=f.parsedJson.admin_cap_id;break;default:break}return d=await this.getCetusConfigHandle(d),d.clmm_pools_id.length>0&&this.updateCache(i,d,Ht),d}async getCetusConfigHandle(t){let e=[t.clmm_pools_id,t.coin_list_id,t.launchpad_pools_id];return(await this._sdk.FullClient.multiGetObjects({ids:e,options:{showContent:!0}})).forEach(n=>{(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidConfigHandle",`when getCetusConfigHandle get objects error: ${n.error}, please check the rpc and contracts address config.`,{[Le.METHOD_NAME]:"getCetusConfigHandle"});let r=ti(n),a=qr(n);switch(en(a).name){case"ClmmPools":t.clmm_pools_handle=r.pools.fields.id.id;break;case"CoinList":t.coin_list_handle=r.coins.fields.id.id;break;case"LaunchpadPools":t.launchpad_pools_handle=r.pools.fields.id.id;break;default:break}}),t}updateCache(t,e,i=Fr){let n=this._cache[t];n?(n.overdue_time=uo(i),n.value=e):n=new Br(e,uo(i)),this._cache[t]=n}getCache(t,e=!1){try{let i=this._cache[t];if(!i)return;if(e||!i.isValid()){delete this._cache[t];return}return i.value}catch(i){console.error(`Error accessing cache for key ${t}:`,i);return}}};var er=re(Gt());import{SuiClient as Xs}from"@mysten/sui/client";import{Transaction as We}from"@mysten/sui/transactions";import{normalizeSuiAddress as xe}from"@mysten/sui/utils";var ho="position_liquidity_snapshot",po=(i=>(i.Deleted="Deleted",i.Exists="Exists",i.NotExists="NotExists",i))(po||{}),mo=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent"];var Ft=re(Gt());import{asUintN as Do,FEE_RATE_DENOMINATOR as _i,getDeltaDownFromOutput as Lo,getDeltaUpFromInput as $o,getNextSqrtPriceFromInput as Ls,getNextSqrtPriceFromOutput as $s,MathUtil as me,ZERO as Kt}from"@cetusprotocol/common-sdk";var qo=re(Gt());import{Transaction as fn}from"@mysten/sui/transactions";import{adjustForSlippage as Bs,CLOCK_ADDRESS as fi,CoinAssist as Dt,getPackagerConfigs as jo,MathUtil as _n,MAX_SQRT_PRICE as Ns,MIN_SQRT_PRICE as js,U64_MAX as qs,ZERO as Ds}from"@cetusprotocol/common-sdk";var go="partner",$e="pool_script",Qt="pool_script_v2",ii="pool_script_v3",ni="router",bo="router_with_partner",Jt="fetcher_script",Na="expect_swap",wo="utils",ja="custodian_v2",qa="clob_v2",Da="endpoints_v2",La=o=>{if(typeof o=="string"&&o.startsWith("0x"))return"object";if(typeof o=="number"||typeof o=="bigint")return"u64";if(typeof o=="boolean")return"bool";throw new Ce(`Unknown type for value: ${o}`,"InvalidType")};var No=re(Gt());import{Transaction as un}from"@mysten/sui/transactions";import{asUintN as Fo,ClmmPoolUtil as Os,CLOCK_ADDRESS as Bo,CoinAssist as qt,getPackagerConfigs as Rs,normalizeCoinType as dn}from"@cetusprotocol/common-sdk";var Me=9e15,_e=1e9,rn="0123456789abcdef",si="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",ai="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",sn={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Me,maxE:Me,crypto:!1},Co,se,N=!0,li="[DecimalError] ",fe=li+"Invalid argument: ",ko=li+"Precision limit exceeded",Mo=li+"crypto unavailable",To="[object Decimal]",Rt=Math.floor,It=Math.pow,Dr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Lr=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,$r=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,Ao=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,Wt=1e7,F=7,Ur=9007199254740991,Kr=si.length-1,an=ai.length-1,M={toStringTag:To};M.absoluteValue=M.abs=function(){var o=new this.constructor(this);return o.s<0&&(o.s=1),O(o)};M.ceil=function(){return O(new this.constructor(this),this.e+1,2)};M.clampedTo=M.clamp=function(o,t){var e,i=this,n=i.constructor;if(o=new n(o),t=new n(t),!o.s||!t.s)return new n(NaN);if(o.gt(t))throw Error(fe+t);return e=i.cmp(o),e<0?o:i.cmp(t)>0?t:new n(i)};M.comparedTo=M.cmp=function(o){var t,e,i,n,r=this,a=r.d,l=(o=new r.constructor(o)).d,d=r.s,f=o.s;if(!a||!l)return!d||!f?NaN:d!==f?d:a===l?0:!a^d<0?1:-1;if(!a[0]||!l[0])return a[0]?d:l[0]?-f:0;if(d!==f)return d;if(r.e!==o.e)return r.e>o.e^d<0?1:-1;for(i=a.length,n=l.length,t=0,e=i<n?i:n;t<e;++t)if(a[t]!==l[t])return a[t]>l[t]^d<0?1:-1;return i===n?0:i>n^d<0?1:-1};M.cosine=M.cos=function(){var o,t,e=this,i=e.constructor;return e.d?e.d[0]?(o=i.precision,t=i.rounding,i.precision=o+Math.max(e.e,e.sd())+F,i.rounding=1,e=Vr(i,Oo(i,e)),i.precision=o,i.rounding=t,O(se==2||se==3?e.neg():e,o,t,!0)):new i(1):new i(NaN)};M.cubeRoot=M.cbrt=function(){var o,t,e,i,n,r,a,l,d,f,h=this,w=h.constructor;if(!h.isFinite()||h.isZero())return new w(h);for(N=!1,r=h.s*It(h.s*h,1/3),!r||Math.abs(r)==1/0?(e=Ot(h.d),o=h.e,(r=(o-e.length+1)%3)&&(e+=r==1||r==-2?"0":"00"),r=It(e,1/3),o=Rt((o+1)/3)-(o%3==(o<0?-1:2)),r==1/0?e="5e"+o:(e=r.toExponential(),e=e.slice(0,e.indexOf("e")+1)+o),i=new w(e),i.s=h.s):i=new w(r.toString()),a=(o=w.precision)+3;;)if(l=i,d=l.times(l).times(l),f=d.plus(h),i=W(f.plus(h).times(l),f.plus(d),a+2,1),Ot(l.d).slice(0,a)===(e=Ot(i.d)).slice(0,a))if(e=e.slice(a-3,a+1),e=="9999"||!n&&e=="4999"){if(!n&&(O(l,o+1,0),l.times(l).times(l).eq(h))){i=l;break}a+=4,n=1}else{(!+e||!+e.slice(1)&&e.charAt(0)=="5")&&(O(i,o+1,1),t=!i.times(i).times(i).eq(h));break}return N=!0,O(i,o,w.rounding,t)};M.decimalPlaces=M.dp=function(){var o,t=this.d,e=NaN;if(t){if(o=t.length-1,e=(o-Rt(this.e/F))*F,o=t[o],o)for(;o%10==0;o/=10)e--;e<0&&(e=0)}return e};M.dividedBy=M.div=function(o){return W(this,new this.constructor(o))};M.dividedToIntegerBy=M.divToInt=function(o){var t=this,e=t.constructor;return O(W(t,new e(o),0,1,1),e.precision,e.rounding)};M.equals=M.eq=function(o){return this.cmp(o)===0};M.floor=function(){return O(new this.constructor(this),this.e+1,3)};M.greaterThan=M.gt=function(o){return this.cmp(o)>0};M.greaterThanOrEqualTo=M.gte=function(o){var t=this.cmp(o);return t==1||t===0};M.hyperbolicCosine=M.cosh=function(){var o,t,e,i,n,r=this,a=r.constructor,l=new a(1);if(!r.isFinite())return new a(r.s?1/0:NaN);if(r.isZero())return l;e=a.precision,i=a.rounding,a.precision=e+Math.max(r.e,r.sd())+4,a.rounding=1,n=r.d.length,n<32?(o=Math.ceil(n/3),t=(1/di(4,o)).toString()):(o=16,t="2.3283064365386962890625e-10"),r=Te(a,1,r.times(t),new a(1),!0);for(var d,f=o,h=new a(8);f--;)d=r.times(r),r=l.minus(d.times(h.minus(d.times(h))));return O(r,a.precision=e,a.rounding=i,!0)};M.hyperbolicSine=M.sinh=function(){var o,t,e,i,n=this,r=n.constructor;if(!n.isFinite()||n.isZero())return new r(n);if(t=r.precision,e=r.rounding,r.precision=t+Math.max(n.e,n.sd())+4,r.rounding=1,i=n.d.length,i<3)n=Te(r,2,n,n,!0);else{o=1.4*Math.sqrt(i),o=o>16?16:o|0,n=n.times(1/di(5,o)),n=Te(r,2,n,n,!0);for(var a,l=new r(5),d=new r(16),f=new r(20);o--;)a=n.times(n),n=n.times(l.plus(a.times(d.times(a).plus(f))))}return r.precision=t,r.rounding=e,O(n,t,e,!0)};M.hyperbolicTangent=M.tanh=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+7,i.rounding=1,W(e.sinh(),e.cosh(),i.precision=o,i.rounding=t)):new i(e.s)};M.inverseCosine=M.acos=function(){var o=this,t=o.constructor,e=o.abs().cmp(1),i=t.precision,n=t.rounding;return e!==-1?e===0?o.isNeg()?Xt(t,i,n):new t(0):new t(NaN):o.isZero()?Xt(t,i+4,n).times(.5):(t.precision=i+6,t.rounding=1,o=new t(1).minus(o).div(o.plus(1)).sqrt().atan(),t.precision=i,t.rounding=n,o.times(2))};M.inverseHyperbolicCosine=M.acosh=function(){var o,t,e=this,i=e.constructor;return e.lte(1)?new i(e.eq(1)?0:NaN):e.isFinite()?(o=i.precision,t=i.rounding,i.precision=o+Math.max(Math.abs(e.e),e.sd())+4,i.rounding=1,N=!1,e=e.times(e).minus(1).sqrt().plus(e),N=!0,i.precision=o,i.rounding=t,e.ln()):new i(e)};M.inverseHyperbolicSine=M.asinh=function(){var o,t,e=this,i=e.constructor;return!e.isFinite()||e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+2*Math.max(Math.abs(e.e),e.sd())+6,i.rounding=1,N=!1,e=e.times(e).plus(1).sqrt().plus(e),N=!0,i.precision=o,i.rounding=t,e.ln())};M.inverseHyperbolicTangent=M.atanh=function(){var o,t,e,i,n=this,r=n.constructor;return n.isFinite()?n.e>=0?new r(n.abs().eq(1)?n.s/0:n.isZero()?n:NaN):(o=r.precision,t=r.rounding,i=n.sd(),Math.max(i,o)<2*-n.e-1?O(new r(n),o,t,!0):(r.precision=e=i-n.e,n=W(n.plus(1),new r(1).minus(n),e+o,1),r.precision=o+4,r.rounding=1,n=n.ln(),r.precision=o,r.rounding=t,n.times(.5))):new r(NaN)};M.inverseSine=M.asin=function(){var o,t,e,i,n=this,r=n.constructor;return n.isZero()?new r(n):(t=n.abs().cmp(1),e=r.precision,i=r.rounding,t!==-1?t===0?(o=Xt(r,e+4,i).times(.5),o.s=n.s,o):new r(NaN):(r.precision=e+6,r.rounding=1,n=n.div(new r(1).minus(n.times(n)).sqrt().plus(1)).atan(),r.precision=e,r.rounding=i,n.times(2)))};M.inverseTangent=M.atan=function(){var o,t,e,i,n,r,a,l,d,f=this,h=f.constructor,w=h.precision,P=h.rounding;if(f.isFinite()){if(f.isZero())return new h(f);if(f.abs().eq(1)&&w+4<=an)return a=Xt(h,w+4,P).times(.25),a.s=f.s,a}else{if(!f.s)return new h(NaN);if(w+4<=an)return a=Xt(h,w+4,P).times(.5),a.s=f.s,a}for(h.precision=l=w+10,h.rounding=1,e=Math.min(28,l/F+2|0),o=e;o;--o)f=f.div(f.times(f).plus(1).sqrt().plus(1));for(N=!1,t=Math.ceil(l/F),i=1,d=f.times(f),a=new h(f),n=f;o!==-1;)if(n=n.times(d),r=a.minus(n.div(i+=2)),n=n.times(d),a=r.plus(n.div(i+=2)),a.d[t]!==void 0)for(o=t;a.d[o]===r.d[o]&&o--;);return e&&(a=a.times(2<<e-1)),N=!0,O(a,h.precision=w,h.rounding=P,!0)};M.isFinite=function(){return!!this.d};M.isInteger=M.isInt=function(){return!!this.d&&Rt(this.e/F)>this.d.length-2};M.isNaN=function(){return!this.s};M.isNegative=M.isNeg=function(){return this.s<0};M.isPositive=M.isPos=function(){return this.s>0};M.isZero=function(){return!!this.d&&this.d[0]===0};M.lessThan=M.lt=function(o){return this.cmp(o)<0};M.lessThanOrEqualTo=M.lte=function(o){return this.cmp(o)<1};M.logarithm=M.log=function(o){var t,e,i,n,r,a,l,d,f=this,h=f.constructor,w=h.precision,P=h.rounding,k=5;if(o==null)o=new h(10),t=!0;else{if(o=new h(o),e=o.d,o.s<0||!e||!e[0]||o.eq(1))return new h(NaN);t=o.eq(10)}if(e=f.d,f.s<0||!e||!e[0]||f.eq(1))return new h(e&&!e[0]?-1/0:f.s!=1?NaN:e?0:1/0);if(t)if(e.length>1)r=!0;else{for(n=e[0];n%10===0;)n/=10;r=n!==1}if(N=!1,l=w+k,a=de(f,l),i=t?ci(h,l+10):de(o,l),d=W(a,i,l,1),Ue(d.d,n=w,P))do if(l+=10,a=de(f,l),i=t?ci(h,l+10):de(o,l),d=W(a,i,l,1),!r){+Ot(d.d).slice(n+1,n+15)+1==1e14&&(d=O(d,w+1,0));break}while(Ue(d.d,n+=10,P));return N=!0,O(d,w,P)};M.minus=M.sub=function(o){var t,e,i,n,r,a,l,d,f,h,w,P,k=this,T=k.constructor;if(o=new T(o),!k.d||!o.d)return!k.s||!o.s?o=new T(NaN):k.d?o.s=-o.s:o=new T(o.d||k.s!==o.s?k:NaN),o;if(k.s!=o.s)return o.s=-o.s,k.plus(o);if(f=k.d,P=o.d,l=T.precision,d=T.rounding,!f[0]||!P[0]){if(P[0])o.s=-o.s;else if(f[0])o=new T(k);else return new T(d===3?-0:0);return N?O(o,l,d):o}if(e=Rt(o.e/F),h=Rt(k.e/F),f=f.slice(),r=h-e,r){for(w=r<0,w?(t=f,r=-r,a=P.length):(t=P,e=h,a=f.length),i=Math.max(Math.ceil(l/F),a)+2,r>i&&(r=i,t.length=1),t.reverse(),i=r;i--;)t.push(0);t.reverse()}else{for(i=f.length,a=P.length,w=i<a,w&&(a=i),i=0;i<a;i++)if(f[i]!=P[i]){w=f[i]<P[i];break}r=0}for(w&&(t=f,f=P,P=t,o.s=-o.s),a=f.length,i=P.length-a;i>0;--i)f[a++]=0;for(i=P.length;i>r;){if(f[--i]<P[i]){for(n=i;n&&f[--n]===0;)f[n]=Wt-1;--f[n],f[i]+=Wt}f[i]-=P[i]}for(;f[--a]===0;)f.pop();for(;f[0]===0;f.shift())--e;return f[0]?(o.d=f,o.e=ui(f,e),N?O(o,l,d):o):new T(d===3?-0:0)};M.modulo=M.mod=function(o){var t,e=this,i=e.constructor;return o=new i(o),!e.d||!o.s||o.d&&!o.d[0]?new i(NaN):!o.d||e.d&&!e.d[0]?O(new i(e),i.precision,i.rounding):(N=!1,i.modulo==9?(t=W(e,o.abs(),0,3,1),t.s*=o.s):t=W(e,o,0,i.modulo,1),t=t.times(o),N=!0,e.minus(t))};M.naturalExponential=M.exp=function(){return cn(this)};M.naturalLogarithm=M.ln=function(){return de(this)};M.negated=M.neg=function(){var o=new this.constructor(this);return o.s=-o.s,O(o)};M.plus=M.add=function(o){var t,e,i,n,r,a,l,d,f,h,w=this,P=w.constructor;if(o=new P(o),!w.d||!o.d)return!w.s||!o.s?o=new P(NaN):w.d||(o=new P(o.d||w.s===o.s?w:NaN)),o;if(w.s!=o.s)return o.s=-o.s,w.minus(o);if(f=w.d,h=o.d,l=P.precision,d=P.rounding,!f[0]||!h[0])return h[0]||(o=new P(w)),N?O(o,l,d):o;if(r=Rt(w.e/F),i=Rt(o.e/F),f=f.slice(),n=r-i,n){for(n<0?(e=f,n=-n,a=h.length):(e=h,i=r,a=f.length),r=Math.ceil(l/F),a=r>a?r+1:a+1,n>a&&(n=a,e.length=1),e.reverse();n--;)e.push(0);e.reverse()}for(a=f.length,n=h.length,a-n<0&&(n=a,e=h,h=f,f=e),t=0;n;)t=(f[--n]=f[n]+h[n]+t)/Wt|0,f[n]%=Wt;for(t&&(f.unshift(t),++i),a=f.length;f[--a]==0;)f.pop();return o.d=f,o.e=ui(f,i),N?O(o,l,d):o};M.precision=M.sd=function(o){var t,e=this;if(o!==void 0&&o!==!!o&&o!==1&&o!==0)throw Error(fe+o);return e.d?(t=So(e.d),o&&e.e+1>t&&(t=e.e+1)):t=NaN,t};M.round=function(){var o=this,t=o.constructor;return O(new t(o),o.e+1,t.rounding)};M.sine=M.sin=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+Math.max(e.e,e.sd())+F,i.rounding=1,e=Wr(i,Oo(i,e)),i.precision=o,i.rounding=t,O(se>2?e.neg():e,o,t,!0)):new i(NaN)};M.squareRoot=M.sqrt=function(){var o,t,e,i,n,r,a=this,l=a.d,d=a.e,f=a.s,h=a.constructor;if(f!==1||!l||!l[0])return new h(!f||f<0&&(!l||l[0])?NaN:l?a:1/0);for(N=!1,f=Math.sqrt(+a),f==0||f==1/0?(t=Ot(l),(t.length+d)%2==0&&(t+="0"),f=Math.sqrt(t),d=Rt((d+1)/2)-(d<0||d%2),f==1/0?t="5e"+d:(t=f.toExponential(),t=t.slice(0,t.indexOf("e")+1)+d),i=new h(t)):i=new h(f.toString()),e=(d=h.precision)+3;;)if(r=i,i=r.plus(W(a,r,e+2,1)).times(.5),Ot(r.d).slice(0,e)===(t=Ot(i.d)).slice(0,e))if(t=t.slice(e-3,e+1),t=="9999"||!n&&t=="4999"){if(!n&&(O(r,d+1,0),r.times(r).eq(a))){i=r;break}e+=4,n=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(O(i,d+1,1),o=!i.times(i).eq(a));break}return N=!0,O(i,d,h.rounding,o)};M.tangent=M.tan=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+10,i.rounding=1,e=e.sin(),e.s=1,e=W(e,new i(1).minus(e.times(e)).sqrt(),o+10,0),i.precision=o,i.rounding=t,O(se==2||se==4?e.neg():e,o,t,!0)):new i(NaN)};M.times=M.mul=function(o){var t,e,i,n,r,a,l,d,f,h=this,w=h.constructor,P=h.d,k=(o=new w(o)).d;if(o.s*=h.s,!P||!P[0]||!k||!k[0])return new w(!o.s||P&&!P[0]&&!k||k&&!k[0]&&!P?NaN:!P||!k?o.s/0:o.s*0);for(e=Rt(h.e/F)+Rt(o.e/F),d=P.length,f=k.length,d<f&&(r=P,P=k,k=r,a=d,d=f,f=a),r=[],a=d+f,i=a;i--;)r.push(0);for(i=f;--i>=0;){for(t=0,n=d+i;n>i;)l=r[n]+k[i]*P[n-i-1]+t,r[n--]=l%Wt|0,t=l/Wt|0;r[n]=(r[n]+t)%Wt|0}for(;!r[--a];)r.pop();return t?++e:r.shift(),o.d=r,o.e=ui(r,e),N?O(o,w.precision,w.rounding):o};M.toBinary=function(o,t){return ln(this,2,o,t)};M.toDecimalPlaces=M.toDP=function(o,t){var e=this,i=e.constructor;return e=new i(e),o===void 0?e:(Nt(o,0,_e),t===void 0?t=i.rounding:Nt(t,0,8),O(e,o+e.e+1,t))};M.toExponential=function(o,t){var e,i=this,n=i.constructor;return o===void 0?e=Yt(i,!0):(Nt(o,0,_e),t===void 0?t=n.rounding:Nt(t,0,8),i=O(new n(i),o+1,t),e=Yt(i,!0,o+1)),i.isNeg()&&!i.isZero()?"-"+e:e};M.toFixed=function(o,t){var e,i,n=this,r=n.constructor;return o===void 0?e=Yt(n):(Nt(o,0,_e),t===void 0?t=r.rounding:Nt(t,0,8),i=O(new r(n),o+n.e+1,t),e=Yt(i,!1,o+i.e+1)),n.isNeg()&&!n.isZero()?"-"+e:e};M.toFraction=function(o){var t,e,i,n,r,a,l,d,f,h,w,P,k=this,T=k.d,S=k.constructor;if(!T)return new S(k);if(f=e=new S(1),i=d=new S(0),t=new S(i),r=t.e=So(T)-k.e-1,a=r%F,t.d[0]=It(10,a<0?F+a:a),o==null)o=r>0?t:f;else{if(l=new S(o),!l.isInt()||l.lt(f))throw Error(fe+l);o=l.gt(t)?r>0?t:f:l}for(N=!1,l=new S(Ot(T)),h=S.precision,S.precision=r=T.length*F*2;w=W(l,t,0,1,1),n=e.plus(w.times(i)),n.cmp(o)!=1;)e=i,i=n,n=f,f=d.plus(w.times(n)),d=n,n=t,t=l.minus(w.times(n)),l=n;return n=W(o.minus(e),i,0,1,1),d=d.plus(n.times(f)),e=e.plus(n.times(i)),d.s=f.s=k.s,P=W(f,i,r,1).minus(k).abs().cmp(W(d,e,r,1).minus(k).abs())<1?[f,i]:[d,e],S.precision=h,N=!0,P};M.toHexadecimal=M.toHex=function(o,t){return ln(this,16,o,t)};M.toNearest=function(o,t){var e=this,i=e.constructor;if(e=new i(e),o==null){if(!e.d)return e;o=new i(1),t=i.rounding}else{if(o=new i(o),t===void 0?t=i.rounding:Nt(t,0,8),!e.d)return o.s?e:o;if(!o.d)return o.s&&(o.s=e.s),o}return o.d[0]?(N=!1,e=W(e,o,0,t,1).times(o),N=!0,O(e)):(o.s=e.s,e=o),e};M.toNumber=function(){return+this};M.toOctal=function(o,t){return ln(this,8,o,t)};M.toPower=M.pow=function(o){var t,e,i,n,r,a,l=this,d=l.constructor,f=+(o=new d(o));if(!l.d||!o.d||!l.d[0]||!o.d[0])return new d(It(+l,f));if(l=new d(l),l.eq(1))return l;if(i=d.precision,r=d.rounding,o.eq(1))return O(l,i,r);if(t=Rt(o.e/F),t>=o.d.length-1&&(e=f<0?-f:f)<=Ur)return n=xo(d,l,e,i),o.s<0?new d(1).div(n):O(n,i,r);if(a=l.s,a<0){if(t<o.d.length-1)return new d(NaN);if((o.d[t]&1)==0&&(a=1),l.e==0&&l.d[0]==1&&l.d.length==1)return l.s=a,l}return e=It(+l,f),t=e==0||!isFinite(e)?Rt(f*(Math.log("0."+Ot(l.d))/Math.LN10+l.e+1)):new d(e+"").e,t>d.maxE+1||t<d.minE-1?new d(t>0?a/0:0):(N=!1,d.rounding=l.s=1,e=Math.min(12,(t+"").length),n=cn(o.times(de(l,i+e)),i),n.d&&(n=O(n,i+5,1),Ue(n.d,i,r)&&(t=i+10,n=O(cn(o.times(de(l,t+e)),t),t+5,1),+Ot(n.d).slice(i+1,i+15)+1==1e14&&(n=O(n,i+1,0)))),n.s=a,N=!0,d.rounding=r,O(n,i,r))};M.toPrecision=function(o,t){var e,i=this,n=i.constructor;return o===void 0?e=Yt(i,i.e<=n.toExpNeg||i.e>=n.toExpPos):(Nt(o,1,_e),t===void 0?t=n.rounding:Nt(t,0,8),i=O(new n(i),o,t),e=Yt(i,o<=i.e||i.e<=n.toExpNeg,o)),i.isNeg()&&!i.isZero()?"-"+e:e};M.toSignificantDigits=M.toSD=function(o,t){var e=this,i=e.constructor;return o===void 0?(o=i.precision,t=i.rounding):(Nt(o,1,_e),t===void 0?t=i.rounding:Nt(t,0,8)),O(new i(e),o,t)};M.toString=function(){var o=this,t=o.constructor,e=Yt(o,o.e<=t.toExpNeg||o.e>=t.toExpPos);return o.isNeg()&&!o.isZero()?"-"+e:e};M.truncated=M.trunc=function(){return O(new this.constructor(this),this.e+1,1)};M.valueOf=M.toJSON=function(){var o=this,t=o.constructor,e=Yt(o,o.e<=t.toExpNeg||o.e>=t.toExpPos);return o.isNeg()?"-"+e:e};function Ot(o){var t,e,i,n=o.length-1,r="",a=o[0];if(n>0){for(r+=a,t=1;t<n;t++)i=o[t]+"",e=F-i.length,e&&(r+=ue(e)),r+=i;a=o[t],i=a+"",e=F-i.length,e&&(r+=ue(e))}else if(a===0)return"0";for(;a%10===0;)a/=10;return r+a}function Nt(o,t,e){if(o!==~~o||o<t||o>e)throw Error(fe+o)}function Ue(o,t,e,i){var n,r,a,l;for(r=o[0];r>=10;r/=10)--t;return--t<0?(t+=F,n=0):(n=Math.ceil((t+1)/F),t%=F),r=It(10,F-t),l=o[n]%r|0,i==null?t<3?(t==0?l=l/100|0:t==1&&(l=l/10|0),a=e<4&&l==99999||e>3&&l==49999||l==5e4||l==0):a=(e<4&&l+1==r||e>3&&l+1==r/2)&&(o[n+1]/r/100|0)==It(10,t-2)-1||(l==r/2||l==0)&&(o[n+1]/r/100|0)==0:t<4?(t==0?l=l/1e3|0:t==1?l=l/100|0:t==2&&(l=l/10|0),a=(i||e<4)&&l==9999||!i&&e>3&&l==4999):a=((i||e<4)&&l+1==r||!i&&e>3&&l+1==r/2)&&(o[n+1]/r/1e3|0)==It(10,t-3)-1,a}function oi(o,t,e){for(var i,n=[0],r,a=0,l=o.length;a<l;){for(r=n.length;r--;)n[r]*=t;for(n[0]+=rn.indexOf(o.charAt(a++)),i=0;i<n.length;i++)n[i]>e-1&&(n[i+1]===void 0&&(n[i+1]=0),n[i+1]+=n[i]/e|0,n[i]%=e)}return n.reverse()}function Vr(o,t){var e,i,n;if(t.isZero())return t;i=t.d.length,i<32?(e=Math.ceil(i/3),n=(1/di(4,e)).toString()):(e=16,n="2.3283064365386962890625e-10"),o.precision+=e,t=Te(o,1,t.times(n),new o(1));for(var r=e;r--;){var a=t.times(t);t=a.times(a).minus(a).times(8).plus(1)}return o.precision-=e,t}var W=function(){function o(i,n,r){var a,l=0,d=i.length;for(i=i.slice();d--;)a=i[d]*n+l,i[d]=a%r|0,l=a/r|0;return l&&i.unshift(l),i}function t(i,n,r,a){var l,d;if(r!=a)d=r>a?1:-1;else for(l=d=0;l<r;l++)if(i[l]!=n[l]){d=i[l]>n[l]?1:-1;break}return d}function e(i,n,r,a){for(var l=0;r--;)i[r]-=l,l=i[r]<n[r]?1:0,i[r]=l*a+i[r]-n[r];for(;!i[0]&&i.length>1;)i.shift()}return function(i,n,r,a,l,d){var f,h,w,P,k,T,S,$,j,K,B,D,jt,Z,Lt,ne,Zt,oe,L,Bt,y=i.constructor,s=i.s==n.s?1:-1,u=i.d,_=n.d;if(!u||!u[0]||!_||!_[0])return new y(!i.s||!n.s||(u?_&&u[0]==_[0]:!_)?NaN:u&&u[0]==0||!_?s*0:s/0);for(d?(k=1,h=i.e-n.e):(d=Wt,k=F,h=Rt(i.e/k)-Rt(n.e/k)),L=_.length,Zt=u.length,j=new y(s),K=j.d=[],w=0;_[w]==(u[w]||0);w++);if(_[w]>(u[w]||0)&&h--,r==null?(Z=r=y.precision,a=y.rounding):l?Z=r+(i.e-n.e)+1:Z=r,Z<0)K.push(1),T=!0;else{if(Z=Z/k+2|0,w=0,L==1){for(P=0,_=_[0],Z++;(w<Zt||P)&&Z--;w++)Lt=P*d+(u[w]||0),K[w]=Lt/_|0,P=Lt%_|0;T=P||w<Zt}else{for(P=d/(_[0]+1)|0,P>1&&(_=o(_,P,d),u=o(u,P,d),L=_.length,Zt=u.length),ne=L,B=u.slice(0,L),D=B.length;D<L;)B[D++]=0;Bt=_.slice(),Bt.unshift(0),oe=_[0],_[1]>=d/2&&++oe;do P=0,f=t(_,B,L,D),f<0?(jt=B[0],L!=D&&(jt=jt*d+(B[1]||0)),P=jt/oe|0,P>1?(P>=d&&(P=d-1),S=o(_,P,d),$=S.length,D=B.length,f=t(S,B,$,D),f==1&&(P--,e(S,L<$?Bt:_,$,d))):(P==0&&(f=P=1),S=_.slice()),$=S.length,$<D&&S.unshift(0),e(B,S,D,d),f==-1&&(D=B.length,f=t(_,B,L,D),f<1&&(P++,e(B,L<D?Bt:_,D,d))),D=B.length):f===0&&(P++,B=[0]),K[w++]=P,f&&B[0]?B[D++]=u[ne]||0:(B=[u[ne]],D=1);while((ne++<Zt||B[0]!==void 0)&&Z--);T=B[0]!==void 0}K[0]||K.shift()}if(k==1)j.e=h,Co=T;else{for(w=1,P=K[0];P>=10;P/=10)w++;j.e=w+h*k-1,O(j,l?r+j.e+1:r,a,T)}return j}}();function O(o,t,e,i){var n,r,a,l,d,f,h,w,P,k=o.constructor;t:if(t!=null){if(w=o.d,!w)return o;for(n=1,l=w[0];l>=10;l/=10)n++;if(r=t-n,r<0)r+=F,a=t,h=w[P=0],d=h/It(10,n-a-1)%10|0;else if(P=Math.ceil((r+1)/F),l=w.length,P>=l)if(i){for(;l++<=P;)w.push(0);h=d=0,n=1,r%=F,a=r-F+1}else break t;else{for(h=l=w[P],n=1;l>=10;l/=10)n++;r%=F,a=r-F+n,d=a<0?0:h/It(10,n-a-1)%10|0}if(i=i||t<0||w[P+1]!==void 0||(a<0?h:h%It(10,n-a-1)),f=e<4?(d||i)&&(e==0||e==(o.s<0?3:2)):d>5||d==5&&(e==4||i||e==6&&(r>0?a>0?h/It(10,n-a):0:w[P-1])%10&1||e==(o.s<0?8:7)),t<1||!w[0])return w.length=0,f?(t-=o.e+1,w[0]=It(10,(F-t%F)%F),o.e=-t||0):w[0]=o.e=0,o;if(r==0?(w.length=P,l=1,P--):(w.length=P+1,l=It(10,F-r),w[P]=a>0?(h/It(10,n-a)%It(10,a)|0)*l:0),f)for(;;)if(P==0){for(r=1,a=w[0];a>=10;a/=10)r++;for(a=w[0]+=l,l=1;a>=10;a/=10)l++;r!=l&&(o.e++,w[0]==Wt&&(w[0]=1));break}else{if(w[P]+=l,w[P]!=Wt)break;w[P--]=0,l=1}for(r=w.length;w[--r]===0;)w.pop()}return N&&(o.e>k.maxE?(o.d=null,o.e=NaN):o.e<k.minE&&(o.e=0,o.d=[0])),o}function Yt(o,t,e){if(!o.isFinite())return Eo(o);var i,n=o.e,r=Ot(o.d),a=r.length;return t?(e&&(i=e-a)>0?r=r.charAt(0)+"."+r.slice(1)+ue(i):a>1&&(r=r.charAt(0)+"."+r.slice(1)),r=r+(o.e<0?"e":"e+")+o.e):n<0?(r="0."+ue(-n-1)+r,e&&(i=e-a)>0&&(r+=ue(i))):n>=a?(r+=ue(n+1-a),e&&(i=e-n-1)>0&&(r=r+"."+ue(i))):((i=n+1)<a&&(r=r.slice(0,i)+"."+r.slice(i)),e&&(i=e-a)>0&&(n+1===a&&(r+="."),r+=ue(i))),r}function ui(o,t){var e=o[0];for(t*=F;e>=10;e/=10)t++;return t}function ci(o,t,e){if(t>Kr)throw N=!0,e&&(o.precision=e),Error(ko);return O(new o(si),t,1,!0)}function Xt(o,t,e){if(t>an)throw Error(ko);return O(new o(ai),t,e,!0)}function So(o){var t=o.length-1,e=t*F+1;if(t=o[t],t){for(;t%10==0;t/=10)e--;for(t=o[0];t>=10;t/=10)e++}return e}function ue(o){for(var t="";o--;)t+="0";return t}function xo(o,t,e,i){var n,r=new o(1),a=Math.ceil(i/F+4);for(N=!1;;){if(e%2&&(r=r.times(t),vo(r.d,a)&&(n=!0)),e=Rt(e/2),e===0){e=r.d.length-1,n&&r.d[e]===0&&++r.d[e];break}t=t.times(t),vo(t.d,a)}return N=!0,r}function yo(o){return o.d[o.d.length-1]&1}function Io(o,t,e){for(var i,n,r=new o(t[0]),a=0;++a<t.length;){if(n=new o(t[a]),!n.s){r=n;break}i=r.cmp(n),(i===e||i===0&&r.s===e)&&(r=n)}return r}function cn(o,t){var e,i,n,r,a,l,d,f=0,h=0,w=0,P=o.constructor,k=P.rounding,T=P.precision;if(!o.d||!o.d[0]||o.e>17)return new P(o.d?o.d[0]?o.s<0?0:1/0:1:o.s?o.s<0?0:o:NaN);for(t==null?(N=!1,d=T):d=t,l=new P(.03125);o.e>-2;)o=o.times(l),w+=5;for(i=Math.log(It(2,w))/Math.LN10*2+5|0,d+=i,e=r=a=new P(1),P.precision=d;;){if(r=O(r.times(o),d,1),e=e.times(++h),l=a.plus(W(r,e,d,1)),Ot(l.d).slice(0,d)===Ot(a.d).slice(0,d)){for(n=w;n--;)a=O(a.times(a),d,1);if(t==null)if(f<3&&Ue(a.d,d-i,k,f))P.precision=d+=10,e=r=l=new P(1),h=0,f++;else return O(a,P.precision=T,k,N=!0);else return P.precision=T,a}a=l}}function de(o,t){var e,i,n,r,a,l,d,f,h,w,P,k=1,T=10,S=o,$=S.d,j=S.constructor,K=j.rounding,B=j.precision;if(S.s<0||!$||!$[0]||!S.e&&$[0]==1&&$.length==1)return new j($&&!$[0]?-1/0:S.s!=1?NaN:$?0:S);if(t==null?(N=!1,h=B):h=t,j.precision=h+=T,e=Ot($),i=e.charAt(0),Math.abs(r=S.e)<15e14){for(;i<7&&i!=1||i==1&&e.charAt(1)>3;)S=S.times(o),e=Ot(S.d),i=e.charAt(0),k++;r=S.e,i>1?(S=new j("0."+e),r++):S=new j(i+"."+e.slice(1))}else return f=ci(j,h+2,B).times(r+""),S=de(new j(i+"."+e.slice(1)),h-T).plus(f),j.precision=B,t==null?O(S,B,K,N=!0):S;for(w=S,d=a=S=W(S.minus(1),S.plus(1),h,1),P=O(S.times(S),h,1),n=3;;){if(a=O(a.times(P),h,1),f=d.plus(W(a,new j(n),h,1)),Ot(f.d).slice(0,h)===Ot(d.d).slice(0,h))if(d=d.times(2),r!==0&&(d=d.plus(ci(j,h+2,B).times(r+""))),d=W(d,new j(k),h,1),t==null)if(Ue(d.d,h-T,K,l))j.precision=h+=T,f=a=S=W(w.minus(1),w.plus(1),h,1),P=O(S.times(S),h,1),n=l=1;else return O(d,j.precision=B,K,N=!0);else return j.precision=B,d;d=f,n+=2}}function Eo(o){return String(o.s*o.s/0)}function ri(o,t){var e,i,n;for((e=t.indexOf("."))>-1&&(t=t.replace(".","")),(i=t.search(/e/i))>0?(e<0&&(e=i),e+=+t.slice(i+1),t=t.substring(0,i)):e<0&&(e=t.length),i=0;t.charCodeAt(i)===48;i++);for(n=t.length;t.charCodeAt(n-1)===48;--n);if(t=t.slice(i,n),t){if(n-=i,o.e=e=e-i-1,o.d=[],i=(e+1)%F,e<0&&(i+=F),i<n){for(i&&o.d.push(+t.slice(0,i)),n-=F;i<n;)o.d.push(+t.slice(i,i+=F));t=t.slice(i),i=F-t.length}else i-=n;for(;i--;)t+="0";o.d.push(+t),N&&(o.e>o.constructor.maxE?(o.d=null,o.e=NaN):o.e<o.constructor.minE&&(o.e=0,o.d=[0]))}else o.e=0,o.d=[0];return o}function Hr(o,t){var e,i,n,r,a,l,d,f,h;if(t.indexOf("_")>-1){if(t=t.replace(/(\d)_(?=\d)/g,"$1"),Ao.test(t))return ri(o,t)}else if(t==="Infinity"||t==="NaN")return+t||(o.s=NaN),o.e=NaN,o.d=null,o;if(Lr.test(t))e=16,t=t.toLowerCase();else if(Dr.test(t))e=2;else if($r.test(t))e=8;else throw Error(fe+t);for(r=t.search(/p/i),r>0?(d=+t.slice(r+1),t=t.substring(2,r)):t=t.slice(2),r=t.indexOf("."),a=r>=0,i=o.constructor,a&&(t=t.replace(".",""),l=t.length,r=l-r,n=xo(i,new i(e),r,r*2)),f=oi(t,e,Wt),h=f.length-1,r=h;f[r]===0;--r)f.pop();return r<0?new i(o.s*0):(o.e=ui(f,h),o.d=f,N=!1,a&&(o=W(o,n,l*4)),d&&(o=o.times(Math.abs(d)<54?It(2,d):Ke.pow(2,d))),N=!0,o)}function Wr(o,t){var e,i=t.d.length;if(i<3)return t.isZero()?t:Te(o,2,t,t);e=1.4*Math.sqrt(i),e=e>16?16:e|0,t=t.times(1/di(5,e)),t=Te(o,2,t,t);for(var n,r=new o(5),a=new o(16),l=new o(20);e--;)n=t.times(t),t=t.times(r.plus(n.times(a.times(n).minus(l))));return t}function Te(o,t,e,i,n){var r,a,l,d,f=1,h=o.precision,w=Math.ceil(h/F);for(N=!1,d=e.times(e),l=new o(i);;){if(a=W(l.times(d),new o(t++*t++),h,1),l=n?i.plus(a):i.minus(a),i=W(a.times(d),new o(t++*t++),h,1),a=l.plus(i),a.d[w]!==void 0){for(r=w;a.d[r]===l.d[r]&&r--;);if(r==-1)break}r=l,l=i,i=a,a=r,f++}return N=!0,a.d.length=w+1,a}function di(o,t){for(var e=o;--t;)e*=o;return e}function Oo(o,t){var e,i=t.s<0,n=Xt(o,o.precision,1),r=n.times(.5);if(t=t.abs(),t.lte(r))return se=i?4:1,t;if(e=t.divToInt(n),e.isZero())se=i?3:2;else{if(t=t.minus(e.times(n)),t.lte(r))return se=yo(e)?i?2:3:i?4:1,t;se=yo(e)?i?1:4:i?3:2}return t.minus(n).abs()}function ln(o,t,e,i){var n,r,a,l,d,f,h,w,P,k=o.constructor,T=e!==void 0;if(T?(Nt(e,1,_e),i===void 0?i=k.rounding:Nt(i,0,8)):(e=k.precision,i=k.rounding),!o.isFinite())h=Eo(o);else{for(h=Yt(o),a=h.indexOf("."),T?(n=2,t==16?e=e*4-3:t==8&&(e=e*3-2)):n=t,a>=0&&(h=h.replace(".",""),P=new k(1),P.e=h.length-a,P.d=oi(Yt(P),10,n),P.e=P.d.length),w=oi(h,10,n),r=d=w.length;w[--d]==0;)w.pop();if(!w[0])h=T?"0p+0":"0";else{if(a<0?r--:(o=new k(o),o.d=w,o.e=r,o=W(o,P,e,i,0,n),w=o.d,r=o.e,f=Co),a=w[e],l=n/2,f=f||w[e+1]!==void 0,f=i<4?(a!==void 0||f)&&(i===0||i===(o.s<0?3:2)):a>l||a===l&&(i===4||f||i===6&&w[e-1]&1||i===(o.s<0?8:7)),w.length=e,f)for(;++w[--e]>n-1;)w[e]=0,e||(++r,w.unshift(1));for(d=w.length;!w[d-1];--d);for(a=0,h="";a<d;a++)h+=rn.charAt(w[a]);if(T){if(d>1)if(t==16||t==8){for(a=t==16?4:3,--d;d%a;d++)h+="0";for(w=oi(h,n,t),d=w.length;!w[d-1];--d);for(a=1,h="1.";a<d;a++)h+=rn.charAt(w[a])}else h=h.charAt(0)+"."+h.slice(1);h=h+(r<0?"p":"p+")+r}else if(r<0){for(;++r;)h="0"+h;h="0."+h}else if(++r>d)for(r-=d;r--;)h+="0";else r<d&&(h=h.slice(0,r)+"."+h.slice(r))}h=(t==16?"0x":t==2?"0b":t==8?"0o":"")+h}return o.s<0?"-"+h:h}function vo(o,t){if(o.length>t)return o.length=t,!0}function Zr(o){return new this(o).abs()}function zr(o){return new this(o).acos()}function Gr(o){return new this(o).acosh()}function Qr(o,t){return new this(o).plus(t)}function Jr(o){return new this(o).asin()}function Xr(o){return new this(o).asinh()}function Yr(o){return new this(o).atan()}function ts(o){return new this(o).atanh()}function es(o,t){o=new this(o),t=new this(t);var e,i=this.precision,n=this.rounding,r=i+4;return!o.s||!t.s?e=new this(NaN):!o.d&&!t.d?(e=Xt(this,r,1).times(t.s>0?.25:.75),e.s=o.s):!t.d||o.isZero()?(e=t.s<0?Xt(this,i,n):new this(0),e.s=o.s):!o.d||t.isZero()?(e=Xt(this,r,1).times(.5),e.s=o.s):t.s<0?(this.precision=r,this.rounding=1,e=this.atan(W(o,t,r,1)),t=Xt(this,r,1),this.precision=i,this.rounding=n,e=o.s<0?e.minus(t):e.plus(t)):e=this.atan(W(o,t,r,1)),e}function is(o){return new this(o).cbrt()}function ns(o){return O(o=new this(o),o.e+1,2)}function os(o,t,e){return new this(o).clamp(t,e)}function rs(o){if(!o||typeof o!="object")throw Error(li+"Object expected");var t,e,i,n=o.defaults===!0,r=["precision",1,_e,"rounding",0,8,"toExpNeg",-Me,0,"toExpPos",0,Me,"maxE",0,Me,"minE",-Me,0,"modulo",0,9];for(t=0;t<r.length;t+=3)if(e=r[t],n&&(this[e]=sn[e]),(i=o[e])!==void 0)if(Rt(i)===i&&i>=r[t+1]&&i<=r[t+2])this[e]=i;else throw Error(fe+e+": "+i);if(e="crypto",n&&(this[e]=sn[e]),(i=o[e])!==void 0)if(i===!0||i===!1||i===0||i===1)if(i)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[e]=!0;else throw Error(Mo);else this[e]=!1;else throw Error(fe+e+": "+i);return this}function ss(o){return new this(o).cos()}function as(o){return new this(o).cosh()}function Ro(o){var t,e,i;function n(r){var a,l,d,f=this;if(!(f instanceof n))return new n(r);if(f.constructor=n,Po(r)){f.s=r.s,N?!r.d||r.e>n.maxE?(f.e=NaN,f.d=null):r.e<n.minE?(f.e=0,f.d=[0]):(f.e=r.e,f.d=r.d.slice()):(f.e=r.e,f.d=r.d?r.d.slice():r.d);return}if(d=typeof r,d==="number"){if(r===0){f.s=1/r<0?-1:1,f.e=0,f.d=[0];return}if(r<0?(r=-r,f.s=-1):f.s=1,r===~~r&&r<1e7){for(a=0,l=r;l>=10;l/=10)a++;N?a>n.maxE?(f.e=NaN,f.d=null):a<n.minE?(f.e=0,f.d=[0]):(f.e=a,f.d=[r]):(f.e=a,f.d=[r]);return}if(r*0!==0){r||(f.s=NaN),f.e=NaN,f.d=null;return}return ri(f,r.toString())}if(d==="string")return(l=r.charCodeAt(0))===45?(r=r.slice(1),f.s=-1):(l===43&&(r=r.slice(1)),f.s=1),Ao.test(r)?ri(f,r):Hr(f,r);if(d==="bigint")return r<0?(r=-r,f.s=-1):f.s=1,ri(f,r.toString());throw Error(fe+r)}if(n.prototype=M,n.ROUND_UP=0,n.ROUND_DOWN=1,n.ROUND_CEIL=2,n.ROUND_FLOOR=3,n.ROUND_HALF_UP=4,n.ROUND_HALF_DOWN=5,n.ROUND_HALF_EVEN=6,n.ROUND_HALF_CEIL=7,n.ROUND_HALF_FLOOR=8,n.EUCLID=9,n.config=n.set=rs,n.clone=Ro,n.isDecimal=Po,n.abs=Zr,n.acos=zr,n.acosh=Gr,n.add=Qr,n.asin=Jr,n.asinh=Xr,n.atan=Yr,n.atanh=ts,n.atan2=es,n.cbrt=is,n.ceil=ns,n.clamp=os,n.cos=ss,n.cosh=as,n.div=cs,n.exp=ls,n.floor=us,n.hypot=ds,n.ln=fs,n.log=_s,n.log10=ps,n.log2=hs,n.max=ms,n.min=gs,n.mod=bs,n.mul=ws,n.pow=ys,n.random=vs,n.round=Ps,n.sign=Cs,n.sin=ks,n.sinh=Ms,n.sqrt=Ts,n.sub=As,n.sum=Ss,n.tan=xs,n.tanh=Is,n.trunc=Es,o===void 0&&(o={}),o&&o.defaults!==!0)for(i=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],t=0;t<i.length;)o.hasOwnProperty(e=i[t++])||(o[e]=this[e]);return n.config(o),n}function cs(o,t){return new this(o).div(t)}function ls(o){return new this(o).exp()}function us(o){return O(o=new this(o),o.e+1,3)}function ds(){var o,t,e=new this(0);for(N=!1,o=0;o<arguments.length;)if(t=new this(arguments[o++]),t.d)e.d&&(e=e.plus(t.times(t)));else{if(t.s)return N=!0,new this(1/0);e=t}return N=!0,e.sqrt()}function Po(o){return o instanceof Ke||o&&o.toStringTag===To||!1}function fs(o){return new this(o).ln()}function _s(o,t){return new this(o).log(t)}function hs(o){return new this(o).log(2)}function ps(o){return new this(o).log(10)}function ms(){return Io(this,arguments,-1)}function gs(){return Io(this,arguments,1)}function bs(o,t){return new this(o).mod(t)}function ws(o,t){return new this(o).mul(t)}function ys(o,t){return new this(o).pow(t)}function vs(o){var t,e,i,n,r=0,a=new this(1),l=[];if(o===void 0?o=this.precision:Nt(o,1,_e),i=Math.ceil(o/F),this.crypto)if(crypto.getRandomValues)for(t=crypto.getRandomValues(new Uint32Array(i));r<i;)n=t[r],n>=429e7?t[r]=crypto.getRandomValues(new Uint32Array(1))[0]:l[r++]=n%1e7;else if(crypto.randomBytes){for(t=crypto.randomBytes(i*=4);r<i;)n=t[r]+(t[r+1]<<8)+(t[r+2]<<16)+((t[r+3]&127)<<24),n>=214e7?crypto.randomBytes(4).copy(t,r):(l.push(n%1e7),r+=4);r=i/4}else throw Error(Mo);else for(;r<i;)l[r++]=Math.random()*1e7|0;for(i=l[--r],o%=F,i&&o&&(n=It(10,F-o),l[r]=(i/n|0)*n);l[r]===0;r--)l.pop();if(r<0)e=0,l=[0];else{for(e=-1;l[0]===0;e-=F)l.shift();for(i=1,n=l[0];n>=10;n/=10)i++;i<F&&(e-=F-i)}return a.e=e,a.d=l,a}function Ps(o){return O(o=new this(o),o.e+1,this.rounding)}function Cs(o){return o=new this(o),o.d?o.d[0]?o.s:0*o.s:o.s||NaN}function ks(o){return new this(o).sin()}function Ms(o){return new this(o).sinh()}function Ts(o){return new this(o).sqrt()}function As(o,t){return new this(o).sub(t)}function Ss(){var o=0,t=arguments,e=new this(t[o]);for(N=!1;e.s&&++o<t.length;)e=e.plus(t[o]);return N=!0,O(e,this.precision,this.rounding)}function xs(o){return new this(o).tan()}function Is(o){return new this(o).tanh()}function Es(o){return O(o=new this(o),o.e+1,1)}M[Symbol.for("nodejs.util.inspect.custom")]=M.toString;M[Symbol.toStringTag]="Decimal";var Ke=M.constructor=Ro(sn);si=new Ke(si);ai=new Ke(ai);var I=Ke;function Ae(o){let t=qt.isSuiCoin(o.coin_type_a),e=qt.isSuiCoin(o.coin_type_b);return{is_adjust_coin_a:t,is_adjust_coin_b:e}}function Fs(o,t){return I.ceil(I(o).div(1+t)).toString()}var Ut=class{static createCollectRewarderAndFeeParams(t,e,i,n,r,a){r===void 0&&(r=[...n]),a===void 0&&(a=[...n]);let l=dn(i.coin_type_a),d=dn(i.coin_type_b);if(i.collect_fee){let h=qt.buildCoinForAmount(e,r,BigInt(0),l,!1);r=h.remain_coins;let w=qt.buildCoinForAmount(e,a,BigInt(0),d,!1);a=w.remain_coins,e=t.Position.createCollectFeePayload({pool_id:i.pool_id,pos_id:i.pos_id,coin_type_a:i.coin_type_a,coin_type_b:i.coin_type_b},e,h.target_coin,w.target_coin)}let f=[];return i.rewarder_coin_types.forEach(h=>{switch(dn(h)){case l:f.push(qt.buildCoinForAmount(e,r,BigInt(0),h,!1).target_coin);break;case d:f.push(qt.buildCoinForAmount(e,a,BigInt(0),h,!1).target_coin);break;default:f.push(qt.buildCoinForAmount(e,n,BigInt(0),h,!1).target_coin);break}}),e=t.Rewarder.createCollectRewarderPayload(i,e,f),e}static async adjustTransactionForGas(t,e,i,n){n.setSender(t.getSenderAddress());let r=qt.selectCoinAssetGreaterThanOrEqual(e,i).selected_coins,a=qt.calculateTotalBalance(e);if(r.length===0&&R("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let l=await t.FullClient.calculationTxGas(un.from(n));if(qt.selectCoinAssetGreaterThanOrEqual(e,BigInt(l),r.map(f=>f.coin_object_id)).selected_coins.length===0){let f=BigInt(l)+BigInt(500);if(a-i<f){i-=f,i<0&&R("InsufficientBalance","gas Insufficient balance");let h=new un;return{fixAmount:i,newTx:h}}}return{fixAmount:i}}static async buildAddLiquidityFixTokenForGas(t,e,i,n,r,a,l){r=await this.buildAddLiquidityFixToken(t,e,i,r,a,l);let{is_adjust_coin_a:d}=Ae(i),f=d?i.amount_a:i.amount_b,h=await this.adjustTransactionForGas(t,qt.getCoinAssets(d?i.coin_type_a:i.coin_type_b,e),BigInt(f),r),{fixAmount:w}=h,{newTx:P}=h;if(P!=null){let k,T;if(d)i.amount_a=Number(w),k=this.buildAddLiquidityFixTokenCoinInput(P,!i.fix_amount_a,w.toString(),i.slippage,i.coin_type_a,e,!1,!0),T=this.buildAddLiquidityFixTokenCoinInput(P,i.fix_amount_a,i.amount_b,i.slippage,i.coin_type_b,e,!1,!0);else return i.amount_b=Number(w),k=this.buildAddLiquidityFixTokenCoinInput(P,!i.fix_amount_a,i.amount_a,i.slippage,i.coin_type_a,e,!1,!0),T=this.buildAddLiquidityFixTokenCoinInput(P,i.fix_amount_a,w.toString(),i.slippage,i.coin_type_b,e,!1,!0),i=this.fixAddLiquidityFixTokenParams(i,n.slippage,n.cur_sqrt_price),r=await this.buildAddLiquidityFixTokenArgs(P,t,e,i,k,T),r}return r}static async buildAddLiquidityFixToken(t,e,i,n,r,a){n=n||new un;let l,d;return r==null||a==null?(l=this.buildAddLiquidityFixTokenCoinInput(n,!i.fix_amount_a,i.amount_a,i.slippage,i.coin_type_a,e,!1,!0),d=this.buildAddLiquidityFixTokenCoinInput(n,i.fix_amount_a,i.amount_b,i.slippage,i.coin_type_b,e,!1,!0)):(l={target_coin:r,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},d={target_coin:a,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]}),n=this.buildAddLiquidityFixTokenArgs(n,t,e,i,l,d),n}static buildAddLiquidityFixTokenCoinInput(t,e,i,n,r,a,l=!0,d=!0){return e?qt.buildCoinForAmountInterval(t,a,{amount_second:BigInt(Fs(i,n)),amount_first:BigInt(i)},r,l,d):qt.buildCoinForAmount(t,a,BigInt(i),r,l,d)}static fixAddLiquidityFixTokenParams(t,e,i){let n=t.fix_amount_a?t.amount_a:t.amount_b,r=Os.estLiquidityAndCoinAmountFromOneAmounts(Number(t.tick_lower),Number(t.tick_upper),new No.default(n),t.fix_amount_a,!0,e,i);return t.amount_a=t.fix_amount_a?t.amount_a:r.coin_amount_limit_a,t.amount_b=t.fix_amount_a?r.coin_amount_limit_b:t.amount_b,t}static buildAddLiquidityFixTokenArgs(t,e,i,n,r,a){let l=[n.coin_type_a,n.coin_type_b],d=n.is_open?"open_position_with_liquidity_by_fix_coin":"add_liquidity_by_fix_coin",{clmm_pool:f,integrate:h}=e.sdkOptions;n.is_open||(t=this.createCollectRewarderAndFeeParams(e,t,n,i,r.remain_coins,a.remain_coins));let w=Rs(f),P=n.is_open?[t.object(w.global_config_id),t.object(n.pool_id),t.pure.u32(Number(Fo(BigInt(n.tick_lower)).toString())),t.pure.u32(Number(Fo(BigInt(n.tick_upper)).toString())),r.target_coin,a.target_coin,t.pure.u64(n.amount_a),t.pure.u64(n.amount_b),t.pure.bool(n.fix_amount_a),t.object(Bo)]:[t.object(w.global_config_id),t.object(n.pool_id),t.object(n.pos_id),r.target_coin,a.target_coin,t.pure.u64(n.amount_a),t.pure.u64(n.amount_b),t.pure.bool(n.fix_amount_a),t.object(Bo)];return t.moveCall({target:`${h.published_at}::${Qt}::${d}`,typeArguments:l,arguments:P}),t}static checkCoinThreshold(t,e,i,n,r,a){e&&i.moveCall({target:`${t.sdkOptions.integrate.published_at}::${ni}::check_coin_threshold`,typeArguments:[a],arguments:[n,i.pure.u64(r)]})}};var te=class o{static getDefaultSqrtPriceLimit(t){return new qo.default(t?js:Ns)}static getDefaultOtherAmountThreshold(t){return t?Ds:qs}static async buildSwapTransactionForGas(t,e,i,n){let r=this.buildSwapTransaction(t,e,i);r.setSender(t.getSenderAddress());let a=await this.adjustTransactionForGas(t,Dt.getCoinAssets(e.a2b?e.coin_type_a:e.coin_type_b,i),BigInt(e.by_amount_in?e.amount:e.amount_limit),r),{fixAmount:l,newTx:d}=a;if(d!==void 0){d.setSender(t.getSenderAddress()),e.by_amount_in?e.amount=l.toString():e.amount_limit=l.toString(),e=await this.fixSwapParams(t,e,n);let f=Dt.buildCoinForAmount(r,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a),h=Dt.buildCoinForAmount(r,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b);r=this.buildSwapTransactionArgs(d,e,t.sdkOptions,f,h)}return r}static buildSwapTransactionArgs(t,e,i,n,r){let{clmm_pool:a,integrate:l}=i,d=o.getDefaultSqrtPriceLimit(e.a2b),f=[e.coin_type_a,e.coin_type_b],{global_config_id:h}=jo(a);h===void 0&&R("InvalidConfig","clmm.config.global_config_id is undefined");let w=e.swap_partner!==void 0,P=w?e.a2b?"swap_a2b_with_partner":"swap_b2a_with_partner":e.a2b?"swap_a2b":"swap_b2a",k=w?[t.object(h),t.object(e.pool_id),t.object(e.swap_partner),n.target_coin,r.target_coin,t.pure.bool(e.by_amount_in),t.pure.u64(e.amount),t.pure.u64(e.amount_limit),t.pure.u128(d.toString()),t.object(fi)]:[t.object(h),t.object(e.pool_id),n.target_coin,r.target_coin,t.pure.bool(e.by_amount_in),t.pure.u64(e.amount),t.pure.u64(e.amount_limit),t.pure.u128(d.toString()),t.object(fi)];return t.moveCall({target:`${l.published_at}::${Qt}::${P}`,typeArguments:f,arguments:k}),t}static async adjustTransactionForGas(t,e,i,n){n.setSender(t.getSenderAddress());let r=Dt.selectCoinAssetGreaterThanOrEqual(e,i).selected_coins,a=Dt.calculateTotalBalance(e);if(r.length===0&&R("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let l=await t.FullClient.calculationTxGas(n);if(Dt.selectCoinAssetGreaterThanOrEqual(e,BigInt(l),r.map(f=>f.coin_object_id)).selected_coins.length===0){let f=BigInt(l)+BigInt(500);if(a-i<f){i-=f,i<0&&R("InsufficientBalance","gas Insufficient balance");let h=new fn;return{fixAmount:i,newTx:h}}}return{fixAmount:i}}static buildSwapTransaction(t,e,i){let n=new fn;n.setSender(t.getSenderAddress());let r=Dt.buildCoinForAmount(n,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1),a=Dt.buildCoinForAmount(n,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1);return n=this.buildSwapTransactionArgs(n,e,t.sdkOptions,r,a),n}static async fixSwapParams(t,e,i){let{current_pool:n}=i;try{let r=await t.Swap.preSwap({decimals_a:i.decimals_a,decimals_b:i.decimals_b,a2b:e.a2b,by_amount_in:e.by_amount_in,amount:e.amount,pool:n,current_sqrt_price:n.current_sqrt_price,coin_type_a:n.coin_type_a,coin_type_b:n.coin_type_b}),a=i.by_amount_in?r.estimated_amount_out:r.estimated_amount_in,l=Bs(a,i.slippage,!i.by_amount_in);e.amount_limit=l.toString()}catch(r){$t("InvalidConfig",r)}return e}static async buildSwapTransactionWithoutTransferCoinsForGas(t,e,i,n){let{tx:r,coin_ab_s:a}=o.buildSwapTransactionWithoutTransferCoins(t,e,i);r.setSender(t.getSenderAddress());let l=await o.adjustTransactionForGas(t,Dt.getCoinAssets(e.a2b?e.coin_type_a:e.coin_type_b,i),BigInt(e.by_amount_in?e.amount:e.amount_limit),r),{fixAmount:d,newTx:f}=l;if(f!==void 0){f.setSender(t.getSenderAddress()),e.by_amount_in?e.amount=d.toString():e.amount_limit=d.toString(),e=await o.fixSwapParams(t,e,n);let h=Dt.buildCoinForAmount(r,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1,!0),w=Dt.buildCoinForAmount(r,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1,!0),P=o.buildSwapTransactionWithoutTransferCoinArgs(t,f,e,t.sdkOptions,h,w);r=P.tx,a=P.txRes}return{tx:r,coin_ab_s:a}}static buildSwapTransactionWithoutTransferCoins(t,e,i){let n=new fn;n.setSender(t.getSenderAddress());let r=Dt.buildCoinForAmount(n,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1,!0),a=Dt.buildCoinForAmount(n,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1,!0),l=o.buildSwapTransactionWithoutTransferCoinArgs(t,n,e,t.sdkOptions,r,a);return{tx:l.tx,coin_ab_s:l.txRes}}static buildSwapTransactionWithoutTransferCoinArgs(t,e,i,n,r,a){let{clmm_pool:l,integrate:d}=n,f=o.getDefaultSqrtPriceLimit(i.a2b),{global_config_id:h}=jo(l);h===void 0&&R("InvalidConfig","clmm.config.global_config_id is undefined");let w=i.swap_partner!==void 0,P=w?"swap_with_partner":"swap",k=w?bo:ni,T=w?[e.object(h),e.object(i.pool_id),e.object(i.swap_partner),r.target_coin,a.target_coin,e.pure.bool(i.a2b),e.pure.bool(i.by_amount_in),e.pure.u64(i.amount),e.pure.u128(f.toString()),e.pure.bool(!1),e.object(fi)]:[e.object(h),e.object(i.pool_id),r.target_coin,a.target_coin,e.pure.bool(i.a2b),e.pure.bool(i.by_amount_in),e.pure.u64(i.amount),e.pure.u128(f.toString()),e.pure.bool(!1),e.object(fi)],S=[i.coin_type_a,i.coin_type_b],$=e.moveCall({target:`${d.published_at}::${k}::${P}`,typeArguments:S,arguments:T});if(i.by_amount_in){let j=i.a2b?i.coin_type_b:i.coin_type_a,K=i.a2b?$[1]:$[0],B=Number(i.amount_limit);Ut.checkCoinThreshold(t,i.by_amount_in,e,K,B,j)}return{tx:e,txRes:$}}};function sc(o,t,e){let i=t.mul(e).shln(64),n=t.shln(64).add(o.mul(e));return _n.divRoundUp(i,n)}function ac(o,t,e){let i=t.mul(e).shln(64),n=t.shln(64).sub(o.mul(e));return _n.divRoundUp(i,n)}function cc(o,t,e){return e.sub(_n.divRoundUp(o.shln(64),t))}function lc(o,t,e){return e.add(o.shln(64).div(t))}function hc(o){let t=new Ft.default(o);return t.lt(Kt)?{bits:t.neg().xor(new Ft.default(2).pow(new Ft.default(64)).sub(new Ft.default(1))).add(new Ft.default(1)).toString()}:{bits:t.toString()}}function Uo(o){return{coin_type_a:o.coin_type_a,coin_type_b:o.coin_type_b,current_sqrt_price:new Ft.default(o.current_sqrt_price),current_tick_index:o.current_tick_index,fee_growth_global_a:new Ft.default(o.fee_growth_global_a),fee_growth_global_b:new Ft.default(o.fee_growth_global_b),fee_protocol_coin_a:new Ft.default(o.fee_protocol_coin_a),fee_protocol_coin_b:new Ft.default(o.fee_protocol_coin_b),fee_rate:new Ft.default(o.fee_rate),liquidity:new Ft.default(o.liquidity),tick_indexes:[],tick_spacing:Number(o.tick_spacing),ticks:[],collection_name:""}}function Us(o,t,e,i,n,r){if(e===Kt)return{amount_in:Kt,amount_out:Kt,next_sqrt_price:t,fee_amount:Kt};let a=o.gte(t),l,d,f,h;if(r){let w=me.checkMulDivFloor(i,me.checkUnsignedSub(_i,n),_i,64),P=$o(o,t,e,a);P.gt(w)?(l=w,h=me.checkUnsignedSub(i,w),f=Ls(o,e,w,a)):(l=P,h=me.checkMulDivCeil(l,n,_i.sub(n),64),f=t),d=Lo(o,f,e,a)}else{let w=Lo(o,t,e,a);w.gt(i)?(d=i,f=$s(o,e,i,a)):(d=w,f=t),l=$o(o,f,e,a),h=me.checkMulDivCeil(l,n,_i.sub(n),64)}return{amount_in:l,amount_out:d,next_sqrt_price:f,fee_amount:h}}function Ko(o,t,e,i,n){let r=e,a=i.liquidity,{current_sqrt_price:l}=i,d={amount_in:Kt,amount_out:Kt,fee_amount:Kt,ref_amount:Kt,next_sqrt_price:Kt,cross_tick_num:0},f,h,w=te.getDefaultSqrtPriceLimit(o);for(let P of n){if(o&&i.current_tick_index<P.index||!o&&i.current_tick_index>=P.index||P===null)continue;o&&w.gt(P.sqrt_price)||!o&&w.lt(P.sqrt_price)?f=w:f=P.sqrt_price;let k=Us(l,f,a,r,i.fee_rate,t);if(k.amount_in.eq(Kt)||(r=t?r.sub(k.amount_in.add(k.fee_amount)):r.sub(k.amount_out)),d.amount_in=d.amount_in.add(k.amount_in),d.amount_out=d.amount_out.add(k.amount_out),d.fee_amount=d.fee_amount.add(k.fee_amount),k.next_sqrt_price.eq(P.sqrt_price)?(h=P.liquidity_net.mul(new Ft.default(-1)),o?me.is_neg(h)?a=a.add(new Ft.default(Do(BigInt(h.toString()),128))):a=a.add(h):me.is_neg(h)?a=a.sub(new Ft.default(Do(BigInt(h.toString()),128))):a=a.sub(h),l=P.sqrt_price):l=k.next_sqrt_price,d.cross_tick_num+=1,r.eq(Kt))break}return d.amount_in=d.amount_in.add(d.fee_amount),d.next_sqrt_price=l,d}var Vt=re(Gt());import{asIntN as ge,buildNFT as Ks,d as Ve,DETAILS_KEYS as hi,extractStructTagFromType as Se,getMoveObjectType as Vo,getObjectDeletedResponse as Vs,getObjectFields as pn,getObjectId as hn,getObjectNotExistsResponse as Hs,getObjectOwner as Ws,MathUtil as Zs}from"@cetusprotocol/common-sdk";function zs(o,t,e){let i=Se(o).name,n=Se(t).name;return`${i}-${n}[${e}]`}function pi(o){let t=Vo(o),e=Se(t),i=pn(o);i==null&&R("InvalidPoolObject",`Pool id ${hn(o)} not exists.`,{[hi.METHOD_NAME]:"buildPool"});let n=[];i.rewarder_manager.fields.rewarders.forEach(a=>{let{emissions_per_second:l}=a.fields,d=Zs.fromX64(new Vt.default(l)),f=Math.floor(d.toNumber()*60*60*24);n.push({emissions_per_second:l,coin_type:Se(a.fields.reward_coin.fields.name).source_address,growth_global:a.fields.growth_global,emissions_every_day:f})});let r={id:hn(o),pool_type:t,coin_type_a:e.type_arguments[0],coin_type_b:e.type_arguments[1],coin_amount_a:i.coin_a,coin_amount_b:i.coin_b,current_sqrt_price:i.current_sqrt_price,current_tick_index:ge(BigInt(i.current_tick_index.fields.bits)),fee_growth_global_a:i.fee_growth_global_a,fee_growth_global_b:i.fee_growth_global_b,fee_protocol_coin_a:i.fee_protocol_coin_a,fee_protocol_coin_b:i.fee_protocol_coin_b,fee_rate:i.fee_rate,is_pause:i.is_pause,liquidity:i.liquidity,position_manager:{positions_handle:i.position_manager.fields.positions.fields.id.id,size:i.position_manager.fields.positions.fields.size},rewarder_infos:n,rewarder_last_updated_time:i.rewarder_manager.fields.last_updated_time,tick_spacing:i.tick_spacing,ticks_handle:i.tick_manager.fields.ticks.fields.id.id,uri:i.url,index:Number(i.index),name:""};return r.name=zs(r.coin_type_a,r.coin_type_b,r.tick_spacing),r}function mi(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&R("InvalidPositionObject",`Position not exists. Get Position error:${o.error}`,{[hi.METHOD_NAME]:"buildPosition"});let t={creator:"",description:"",image_url:"",link:"",name:"",project_url:""},e={...t,pos_object_id:"",owner:"",type:"",coin_type_a:"",coin_type_b:"",liquidity:"",tick_lower_index:0,tick_upper_index:0,index:0,pool:"",reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0",fee_growth_inside_a:"0",fee_owned_a:"0",fee_growth_inside_b:"0",fee_owned_b:"0",position_status:"Exists"},i=pn(o);if(i){let a=Vo(o),l=Ws(o);"nft"in i?(i=i.nft.fields,t.description=i.description,t.name=i.name,t.link=i.url):t=Ks(o),e={...t,pos_object_id:i.id.id,owner:l.AddressOwner,type:a,liquidity:i.liquidity,coin_type_a:i.coin_type_a.fields.name,coin_type_b:i.coin_type_b.fields.name,tick_lower_index:ge(BigInt(i.tick_lower_index.fields.bits)),tick_upper_index:ge(BigInt(i.tick_upper_index.fields.bits)),index:i.index,pool:i.pool,reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0",fee_growth_inside_a:"0",fee_owned_a:"0",fee_growth_inside_b:"0",fee_owned_b:"0",position_status:"Exists"}}let n=Vs(o);n&&(e.pos_object_id=n.objectId,e.position_status="Deleted");let r=Hs(o);return r&&(e.pos_object_id=r,e.position_status="NotExists"),e}function He(o){let t={reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0"};o="fields"in o?o.fields:o,o.rewards.forEach((r,a)=>{let{amount_owned:l,growth_inside:d}="fields"in r?r.fields:r;a===0?(t.reward_amount_owned_0=l,t.reward_growth_inside_0=d):a===1?(t.reward_amount_owned_1=l,t.reward_growth_inside_1=d):a===2&&(t.reward_amount_owned_2=l,t.reward_growth_inside_2=d)});let e="fields"in o.tick_lower_index?o.tick_lower_index.fields.bits:o.tick_lower_index.bits,i="fields"in o.tick_upper_index?o.tick_upper_index.fields.bits:o.tick_upper_index.bits;return{liquidity:o.liquidity,tick_lower_index:ge(BigInt(e)),tick_upper_index:ge(BigInt(i)),...t,fee_growth_inside_a:o.fee_growth_inside_a,fee_owned_a:o.fee_owned_a,fee_growth_inside_b:o.fee_growth_inside_b,fee_owned_b:o.fee_owned_b,pos_object_id:o.position_id}}function gi(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&R("InvalidTickObject",`Tick not exists. Get tick data error:${o.error}`,{[hi.METHOD_NAME]:"buildTickData"});let e=pn(o).value.fields.value.fields;return{object_id:hn(o),index:ge(BigInt(e.index.fields.bits)),sqrt_price:new Vt.default(e.sqrt_price),liquidity_net:new Vt.default(e.liquidity_net.fields.bits),liquidity_gross:new Vt.default(e.liquidity_gross),fee_growth_outside_a:new Vt.default(e.fee_growth_outside_a),fee_growth_outside_b:new Vt.default(e.fee_growth_outside_b),rewarders_growth_outside:e.rewards_growth_outside}}function Ho(o){(!o||!o.index||!o.sqrt_price||!o.liquidity_net||!o.liquidity_gross||!o.fee_growth_outside_a||!o.fee_growth_outside_b)&&R("InvalidTickFields","Invalid tick fields.",{[hi.METHOD_NAME]:"buildTickDataByEvent"});let t=ge(BigInt(o.index.bits)),e=new Vt.default(o.sqrt_price),i=new Vt.default(o.liquidity_net.bits),n=new Vt.default(o.liquidity_gross),r=new Vt.default(o.fee_growth_outside_a),a=new Vt.default(o.fee_growth_outside_b),l=o.rewards_growth_outside||[];return{object_id:"",index:t,sqrt_price:e,liquidity_net:i,liquidity_gross:n,fee_growth_outside_a:r,fee_growth_outside_b:a,rewarders_growth_outside:l}}function Mc(o,t){return`Cetus LP | Pool${o}-${t}`}function Wo(o,t,e){let i=[],{timestampMs:n,events:r}=o,a=["AddLiquidityEvent","RemoveLiquidityEvent","CollectFeeEvent","CollectRewardEvent","CollectRewardV2Event","HarvestEvent"];return r?.forEach((l,d)=>{let f=Se(l.type).name;if(a.includes(f)){let h={tx_digest:l.id.txDigest,package_id:l.packageId,transaction_module:l.transactionModule,sender:l.sender,type:l.type,timestamp_ms:n||"0",parsed_json:l.parsedJson,index:`${t}_${d}`};switch(f){case"CollectFeeEvent":e.includes(h.parsed_json.position)&&(Ve(h.parsed_json.amount_a).gt(0)||Ve(h.parsed_json.amount_b).gt(0))&&i.push(h);break;case"RemoveLiquidityEvent":case"AddLiquidityEvent":(Ve(h.parsed_json.amount_a).gt(0)||Ve(h.parsed_json.amount_b).gt(0))&&i.push(h);break;case"CollectRewardEvent":case"HarvestEvent":case"CollectRewardV2Event":(e.includes(h.parsed_json.position)||e.includes(h.parsed_json.wrapped_position_id))&&Ve(h.parsed_json.amount).gt(0)&&i.push(h);break;default:break}}}),i}function Zo(o,t,e,i){let n=[],{timestampMs:r,events:a}=o;return a?.forEach((l,d)=>{let{name:f,address:h}=Se(l.type);if(mo.includes(f)&&h===e&&i===l.parsedJson.pool){let w={tx:l.id.txDigest,sender:l.sender,type:l.type,block_time:r||"0",index:`${t}_${d}`,parsed_json:l.parsedJson};n.push(w)}}),n}function bi(o,t,e,i){t.moveCall({target:`${o.sdkOptions.integrate.published_at}::${wo}::transfer_coin_to_sender`,typeArguments:[i],arguments:[e]})}function zo(o,t,e,i,n){n!=null?t.transferObjects([e],t.pure.address(n)):bi(o,t,e,i)}import{asUintN as Ie,CACHE_TIME_24H as wi,ClmmPoolUtil as Ys,CLOCK_ADDRESS as Xo,CoinAssist as yi,createFullClient as ta,d as Ee,DETAILS_KEYS as G,extractStructTagFromType as Oe,getObjectPreviousTransactionDigest as ea,getPackagerConfigs as gn,isSortedSymbols as Yo,TickMath as bn,tickScore as ia,TickUtil as tr}from"@cetusprotocol/common-sdk";import{asIntN as Go,d as Qo,extractStructTagFromType as Gs,fixCoinType as Jo,getObjectFields as mn,getObjectType as Qs}from"@cetusprotocol/common-sdk";var Js=1e4,ee=class{static parseClmmVestInfo(t){let e=mn(t),i=Qs(t),n=Gs(i),r=e.global_vesting_periods.map(l=>({period:l.fields.period,release_time:l.fields.release_time,redeemed_amount:l.fields.redeemed_amount,percentage:Qo(l.fields.percentage).div(Js).toNumber()}));return{id:e.id.id,balance:e.balance,global_vesting_periods:r,total_value:e.total_value,total_cetus_amount:e.total_cetus_amount,redeemed_amount:e.redeemed_amount,start_time:e.start_time,type:n.full_address,positions:{id:e.positions.fields.id,size:e.positions.fields.size}}}static parsePositionVesting(t){return{position_id:t.position_id,cetus_amount:t.cetus_amount,redeemed_amount:t.redeemed_amount,is_paused:t.is_paused,impaired_a:t.impaired_a,impaired_b:t.impaired_b,period_details:t.period_details,coin_type_a:Jo(t.coin_a.name,!1),coin_type_b:Jo(t.coin_b.name,!1)}}static parsePoolLiquiditySnapshot(t){let e=mn(t);return{current_sqrt_price:e.current_sqrt_price,remove_percent:Qo(e.remove_percent).div(1e6).toString(),snapshots:{id:e.snapshots.fields.id.id,size:e.snapshots.fields.size}}}static parsePositionSnapshot(t){let e=mn(t),i=e.value.fields.value.fields;return{position_id:e.name,liquidity:i.liquidity,tick_lower_index:Go(BigInt(i.tick_lower_index.fields.bits)),tick_upper_index:Go(BigInt(i.tick_upper_index.fields.bits)),fee_owned_a:i.fee_owned_a,fee_owned_b:i.fee_owned_b,value_cutted:i.value_cutted,rewards:i.rewards}}};var vi=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}async getPositionList(t,e="all"){let i={data:[],has_next_page:!0},n=await this._sdk.FullClient.getDynamicFieldsByPage(t,e);i.has_next_page=n.has_next_page,i.next_cursor=n.next_cursor;let r=n.data.map(l=>(l.error!=null&&R("InvalidConfig",`when getPositionList get position objects error: ${l.error}, please check the rpc, contracts address config and position id.`,{[G.METHOD_NAME]:"getPositionList"}),l.name.value)),a=await this._sdk.Position.getSimplePositionList(r);return i.data=a,i}async getPoolImmutablesWithPage(t="all",e=!1){let{package_id:i}=this._sdk.sdkOptions.clmm_pool,n=[],r={data:[],has_next_page:!1},a=t==="all",l=`${i}_getPoolImmutables`;if(a){let d=this._sdk.getCache(l,e);d&&n.push(...d)}if(n.length===0)try{let d=`${i}::factory::CreatePoolEvent`,f=await this._sdk.FullClient.queryEventsByPage({MoveEventType:d},t);r.has_next_page=f.has_next_page,r.next_cursor=f.next_cursor,f.data.forEach(h=>{let w=h.parsedJson;w&&n.push({id:w.pool_id,tick_spacing:w.tick_spacing,coin_type_a:Oe(w.coin_type_a).full_address,coin_type_b:Oe(w.coin_type_b).full_address})})}catch(d){console.log("getPoolImmutables",d)}return r.data=n,a&&this._sdk.updateCache(`${i}_getPoolImmutables`,n,wi),r}async getPoolsWithPage(t="all",e=!1){let i={data:[],has_next_page:!1},n=await this.getPoolImmutablesWithPage(t,e),r=await this._sdk.FullClient.batchGetObjects(n.data.map(a=>a.id),{showContent:!0,showType:!0});for(let a of r){(a.error!=null||a.data?.content?.dataType!=="moveObject")&&R("InvalidPoolObject",`getPoolWithPages error code: ${a.error?.code??"unknown error"}, please check config and object ids`,{[G.METHOD_NAME]:"getPoolsWithPage"});let l=pi(a);i.data.push(l);let d=`${l.id}_getPoolObject`;this._sdk.updateCache(d,l,wi)}return i.has_next_page=n.has_next_page,i.next_cursor=n.next_cursor,i}async getPoolLiquiditySnapshot(t,e=!1){try{let i=await this._sdk.FullClient.getDynamicFieldObject({parentId:t,name:{type:"0x1::string::String",value:ho}}),n=ee.parsePoolLiquiditySnapshot(i);if(e){let a=(await this._sdk.FullClient.getDynamicFieldsByPage(n.snapshots.id)).data.map(l=>l.objectId);if(a.length>0){let l=await this._sdk.FullClient.batchGetObjects(a,{showContent:!0,showType:!0}),d=[];l.forEach(f=>{let h=ee.parsePositionSnapshot(f);d.push(h)}),n.position_snapshots=d}}return n}catch(i){return $t("InvalidPoolObject",i,{[G.METHOD_NAME]:"getPoolLiquiditySnapshot",[G.REQUEST_PARAMS]:t})}}async getPositionSnapshot(t,e){let i=await this._sdk.FullClient.getDynamicFieldObjects(t,e,"0x2::object::ID","address"),n=[];return i.forEach(r=>{try{let a=ee.parsePositionSnapshot(r);n.push(a)}catch(a){console.log("getPositionSnapshot error: ",a)}}),n}async getAssignPools(t){if(t.length===0)return[];let e=[],i=await this._sdk.FullClient.batchGetObjects(t,{showContent:!0,showType:!0});for(let n of i){(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidPoolObject",`getPools error code: ${n.error?.code??"unknown error"}, please check config and object ids`,{[G.METHOD_NAME]:"getAssignPools"});let r=pi(n);e.push(r);let a=`${r.id}_getPoolObject`;this._sdk.updateCache(a,r,wi)}return e}async getPool(t,e=!0){let i=`${t}_getPoolObject`,n=this._sdk.getCache(i,e);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:t,options:{showType:!0,showContent:!0}});(r.error!=null||r.data?.content?.dataType!=="moveObject")&&R("InvalidPoolObject",`getPool error code: ${r.error?.code??"unknown error"}, please check config and object id`,{[G.METHOD_NAME]:"getPool"});let a=pi(r);return this._sdk.updateCache(i,a),a}async getPoolByCoins(t,e){if(t.length===0)return[];for(let d=0;d<t.length;d++)t[d]==="0x2::sui::SUI"&&(t[d]="0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI");let i=this._sdk.sdkOptions.stats_pools_url;i||R("StatsPoolsUrlNotSet","statsPoolsUrl is not set in the sdk options.",{[G.METHOD_NAME]:"getPoolByCoins"}),i+=`?order_by=-fees&limit=100&has_mining=true&has_farming=true&no_incentives=true&display_all_pools=true&coin_type=${t.join(",")}`;let n=await fetch(i),r;try{r=await n.json()}catch(d){$t("FetchError",d,{[G.METHOD_NAME]:"getPoolByCoins",[G.REQUEST_PARAMS]:i})}let a=r.data.lp_list,l=[];for(let d of a)t.includes(d.coin_a_address)&&t.includes(d.coin_b_address)&&(e!=null?d.object.feeRate===e&&l.push(d.address):l.push(d.address));return l.length>0?await this.getAssignPools(l):[]}async createPoolPayload(t,e){if(Yo(xe(t.coin_type_a),xe(t.coin_type_b))){let i=t.coin_type_b;t.coin_type_b=t.coin_type_a,t.coin_type_a=i;let n=t.metadata_b;t.metadata_b=t.metadata_a,t.metadata_a=n}return await this.createPoolAndAddLiquidity(t,e)}async createPoolRowPayload(t,e){if(Yo(xe(t.coin_type_a),xe(t.coin_type_b))){let i=t.coin_type_b;t.coin_type_b=t.coin_type_a,t.coin_type_a=i;let n=t.metadata_b;t.metadata_b=t.metadata_a,t.metadata_a=n}return await this.createPoolAndAddLiquidityRow(t,e)}async getClmmConfigs(t=!1){let{package_id:e}=this._sdk.sdkOptions.clmm_pool,i=`${e}_getInitEvent`,n=this._sdk.getCache(i,t);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:e,options:{showPreviousTransaction:!0}}),a=ea(r),l=(await this._sdk.FullClient.queryEventsByPage({Transaction:a})).data,d={pools_id:"",global_config_id:"",global_vault_id:"",admin_cap_id:""};return l.length>0&&(l.forEach(f=>{let h=f.parsedJson;if(f.type)switch(Oe(f.type).full_address){case`${e}::config::InitConfigEvent`:d.global_config_id=h.global_config_id,d.admin_cap_id=h.admin_cap_id;break;case`${e}::factory::InitFactoryEvent`:d.pools_id=h.pools_id;break;case`${e}::rewarder::RewarderInitEvent`:d.global_vault_id=h.global_vault_id;break;case`${e}::partner::InitPartnerEvent`:d.partners_id=h.partners_id;break;default:break}}),this._sdk.updateCache(i,d,wi)),d}async getPoolTransactionList({pool_id:t,pagination_args:e,order:i="descending",full_rpc_url:n}){let{FullClient:r,sdkOptions:a}=this._sdk,l;n?l=ta(new Xs({url:n})):l=r;let d={data:[],has_next_page:!1},f=50,h=e,w=e.limit||10;do{let P=await l.queryTransactionBlocksByPage({ChangedObject:t},{...h,limit:50},i);P.data.forEach((k,T)=>{d.next_cursor=P.next_cursor;let S=Zo(k,T,a.clmm_pool.package_id,t);d.data=[...d.data,...S]}),d.has_next_page=P.has_next_page,d.next_cursor=P.next_cursor,h.cursor=P.next_cursor}while(d.data.length<w&&d.has_next_page);return d.data.length>w&&(d.data=d.data.slice(0,w),d.next_cursor=d.data[d.data.length-1].tx),d}async calculateCreatePoolWithPrice(t){let{current_price:e,slippage:i,tick_spacing:n,add_mode_params:r,price_base_coin:a,coin_decimals_a:l,coin_decimals_b:d}=t,f=bn.priceToSqrtPriceX64(a==="coin_a"?Ee(e):Ee(1).div(e),l,d),h=0,w=0;if(r.is_full_range)h=tr.getMinIndex(n),w=tr.getMaxIndex(n);else{let{min_price:B,max_price:D}=r;h=bn.priceToInitializeTickIndex(a==="coin_a"?Ee(B):Ee(1).div(D),l,d,n),w=bn.priceToInitializeTickIndex(a==="coin_a"?Ee(D):Ee(1).div(B),l,d,n)}let{coin_amount:P,fix_amount_a:k}=t,{coin_amount_limit_a:T,coin_amount_limit_b:S,liquidity_amount:$,coin_amount_a:j,coin_amount_b:K}=Ys.estLiquidityAndCoinAmountFromOneAmounts(h,w,new er.default(P),k,!0,i,f);return{coin_amount_a:j,coin_amount_b:K,coin_amount_limit_a:T,coin_amount_limit_b:S,liquidity:$,initialize_sqrt_price:f.toString(),tick_lower:h,tick_upper:w,fix_amount_a:k}}async createPoolAndAddLiquidity(t,e){e=e||new We,e.setSender(this.sdk.getSenderAddress());let{integrate:i,clmm_pool:n}=this.sdk.sdkOptions,r=gn(n),a=r.global_config_id,l=r.pools_id,d=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress()),f=yi.buildCoinForAmount(e,d,BigInt(t.amount_a),t.coin_type_a,!1,!0),h=yi.buildCoinForAmount(e,d,BigInt(t.amount_b),t.coin_type_b,!1,!0),w=[e.object(a),e.object(l),e.pure.u32(t.tick_spacing),e.pure.u128(t.initialize_sqrt_price),e.pure.string(t.uri),e.pure.u32(Number(Ie(BigInt(t.tick_lower)).toString())),e.pure.u32(Number(Ie(BigInt(t.tick_upper)).toString())),f.target_coin,h.target_coin,e.object(t.metadata_a),e.object(t.metadata_b),e.pure.bool(t.fix_amount_a),e.object(Xo)];return e.moveCall({target:`${i.published_at}::pool_creator_v2::create_pool_v2`,typeArguments:[t.coin_type_a,t.coin_type_b],arguments:w}),bi(this._sdk,e,f.target_coin,t.coin_type_a),bi(this._sdk,e,h.target_coin,t.coin_type_b),e}async createPoolAndAddLiquidityRow(t,e){e=e||new We;let{clmm_pool:i}=this.sdk.sdkOptions,n=gn(i),r=n.global_config_id,a=n.pools_id,l=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress()),d=yi.buildCoinForAmount(e,l,BigInt(t.amount_a),t.coin_type_a,!1,!0),f=yi.buildCoinForAmount(e,l,BigInt(t.amount_b),t.coin_type_b,!1,!0),h=[e.object(r),e.object(a),e.pure.u32(t.tick_spacing),e.pure.u128(t.initialize_sqrt_price),e.pure.string(t.uri),e.pure.u32(Number(Ie(BigInt(t.tick_lower)).toString())),e.pure.u32(Number(Ie(BigInt(t.tick_upper)).toString())),d.target_coin,f.target_coin,e.object(t.metadata_a),e.object(t.metadata_b),e.pure.bool(t.fix_amount_a),e.object(Xo)],w=e.moveCall({target:`${i.published_at}::pool_creator::create_pool_v2`,typeArguments:[t.coin_type_a,t.coin_type_b],arguments:h});return{tx:e,pos_id:w[0],remain_coin_a:w[1],remain_coin_b:w[2],remain_coin_type_a:t.coin_type_a,remain_coin_type_b:t.coin_type_b}}async createPoolWithPriceReturnPositionPayload(t,e){let{coin_type_a:i,coin_type_b:n,tick_spacing:r,uri:a,calculate_result:l}=t,{initialize_sqrt_price:d,tick_lower:f,tick_upper:h,liquidity:w,coin_amount_a:P,coin_amount_b:k,fix_amount_a:T,coin_amount_limit_a:S,coin_amount_limit_b:$}=l,j=await this._sdk.FullClient.fetchCoinMetadata(i),K=await this._sdk.FullClient.fetchCoinMetadata(n);if(j===null)return R("FetchError",`fetch coin ${i} metadata failed`,{[G.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[G.REQUEST_PARAMS]:t});if(K===null)return R("FetchError",`fetch coin ${n} metadata failed`,{[G.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[G.REQUEST_PARAMS]:t});let B=j.id,D=K.id;return this.createPoolRowPayload({tick_spacing:r,initialize_sqrt_price:d,uri:a||"",coin_type_a:i,coin_type_b:n,amount_a:T?P:S,amount_b:T?$:k,fix_amount_a:T,tick_lower:f,tick_upper:h,metadata_a:B,metadata_b:D},e)}async createPoolWithPricePayload(t){let{coin_type_a:e,coin_type_b:i,tick_spacing:n,uri:r,calculate_result:a}=t,{initialize_sqrt_price:l,tick_lower:d,tick_upper:f,liquidity:h,coin_amount_a:w,coin_amount_b:P,fix_amount_a:k,coin_amount_limit_a:T,coin_amount_limit_b:S}=a,$=await this._sdk.FullClient.fetchCoinMetadata(e),j=await this._sdk.FullClient.fetchCoinMetadata(i);if($===null)return R("FetchError",`fetch coin ${e} metadata failed`,{[G.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[G.REQUEST_PARAMS]:t});if(j===null)return R("FetchError",`fetch coin ${i} metadata failed`,{[G.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[G.REQUEST_PARAMS]:t});let K=$.id,B=j.id;return this.createPoolPayload({tick_spacing:n,initialize_sqrt_price:l,uri:r||"",coin_type_a:e,coin_type_b:i,amount_a:k?w:T,amount_b:k?S:P,fix_amount_a:k,tick_lower:d,tick_upper:f,metadata_a:K,metadata_b:B})}async fetchTicks(t){let e=[],i=[];for(;;){let r=await this.getTicks({pool_id:t.pool_id,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b,start:i,limit:512});if(e=[...e,...r],r.length<512)break;i=[Number(Ie(BigInt(r[r.length-1].index)))]}return e}async getTicks(t){let{integrate:e}=this.sdk.sdkOptions,i=[],n=[t.coin_type_a,t.coin_type_b],r=new We,a=r.makeMoveVec({elements:t.start.map(f=>r.pure.u32(f)),type:"u32"}),l=[r.object(t.pool_id),a,r.pure.u64(t.limit.toString())];r.moveCall({target:`${e.published_at}::${Jt}::fetch_ticks`,arguments:l,typeArguments:n});let d=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:r,sender:xe("0x0")});return d.error!=null&&R("InvalidTickObjectId",`getTicks error code: ${d.error??"unknown error"}, please check config and tick object ids`,{[G.METHOD_NAME]:"getTicks",[G.REQUEST_PARAMS]:t}),d.events?.forEach(f=>{Oe(f.type).name==="FetchTicksResultEvent"&&f.parsedJson.ticks.forEach(h=>{i.push(Ho(h))})}),i}async fetchPoolPositionInfoList(t){let{integrate:e}=this.sdk.sdkOptions,i=[],n=[],r=512;for(;;){let a=[t.coin_type_a,t.coin_type_b],l=new We,d=l.pure.vector("id",n.map(P=>P)),f=[l.object(t.pool_id),d,l.pure.u64(r)];l.moveCall({target:`${e.published_at}::${Jt}::fetch_positions`,arguments:f,typeArguments:a});let h=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:l,sender:xe("0x0")});h.error!=null&&R("InvalidPositionRewardObject",`fetch position info error code: ${h.error??"unknown error"}, please check config and tick object ids`,{[G.METHOD_NAME]:"fetchPoolPositionInfoList",[G.REQUEST_PARAMS]:t});let w=[];if(h?.events?.forEach(P=>{Oe(P.type).name==="FetchPositionsEvent"&&P.parsedJson.positions.forEach(k=>{let T=He(k);w.push(T)})}),i.push(...w),w.length<r)break;n=[w[w.length-1].pos_object_id]}return i}async fetchTicksByRpc(t){let e=[],i=null,n=50;for(;;){let r=[],a=await this.sdk.FullClient.getDynamicFields({parentId:t,cursor:i,limit:n});if(i=a.nextCursor,a.data.forEach(l=>{Oe(l.objectType).module==="skip_list"&&r.push(l.objectId)}),e=[...e,...await this.getTicksByRpc(r)],!a.hasNextPage)break}return e}async getTicksByRpc(t){let e=[],i=await this.sdk.FullClient.batchGetObjects(t,{showContent:!0,showType:!0});for(let n of i){(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidTickObjectId",`getTicksByRpc error code: ${n.error?.code??"unknown error"}, please check config and tick object ids`,{[G.METHOD_NAME]:"getTicksByRpc"});let r=gi(n);r!=null&&e.push(r)}return e}async getTickDataByIndex(t,e){let i={type:"u64",value:Ie(BigInt(ia(e).toString())).toString()},n=await this.sdk.FullClient.getDynamicFieldObject({parentId:t,name:i});return(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidTickIndex",`get tick by index: ${e} error: ${n.error}`,{[G.METHOD_NAME]:"getTickDataByIndex"}),gi(n)}async getTickDataByObjectId(t){let e=await this.sdk.FullClient.getObject({id:t,options:{showContent:!0}});return(e.error!=null||e.data?.content?.dataType!=="moveObject")&&R("InvalidTickObjectId",`getTicksByRpc error code: ${e.error?.code??"unknown error"}, please check config and tick object ids`,{[G.METHOD_NAME]:"getTickDataByObjectId"}),gi(e)}async getPartnerRefFeeAmount(t,e=!0){let i=await this._sdk.FullClient.batchGetObjects([t],{showOwner:!0,showContent:!0,showDisplay:e,showType:!0});i[0].data?.content?.dataType!=="moveObject"&&R("NotFoundPartnerObject",`get partner by object id: ${t} error: ${i[0].error}`,{[G.METHOD_NAME]:"getPartnerRefFeeAmount"});let n=i[0].data.content.fields.balances,r=await this._sdk.FullClient.getDynamicFieldsByPage(n.fields.id.id),a=[];r.data.forEach(f=>{f.objectId!=null&&a.push(f.objectId)});let l=[];return(await this._sdk.FullClient.batchGetObjects(a,{showOwner:!0,showContent:!0,showDisplay:e,showType:!0})).forEach(f=>{(f.error!=null||f.data?.content?.dataType!=="moveObject")&&R("InvalidPartnerRefFeeFields",`get coin by object id: ${f.data.objectId} error: ${f.error}`,{[G.METHOD_NAME]:"getPartnerRefFeeAmount"});let h={coin_type:f.data.content.fields.name,coin_object_id:f.data.objectId,balance:BigInt(f.data.content.fields.value)};l.push(h)}),l}async claimPartnerRefFeePayload(t,e,i){let n=new We,{clmm_pool:r}=this.sdk.sdkOptions,{global_config_id:a}=gn(r),l=[i],d=[n.object(a),n.object(t),n.object(e)];return n.moveCall({target:`${r.published_at}::${go}::claim_ref_fee`,arguments:d,typeArguments:l}),n}};var Ze=re(Gt());import{SuiClient as na}from"@mysten/sui/client";import{Transaction as ae}from"@mysten/sui/transactions";import{isValidSuiObjectId as oa,normalizeSuiAddress as ra}from"@mysten/sui/utils";import{asUintN as Re,ClmmPoolUtil as ir,CLOCK_ADDRESS as Pi,CoinAssist as Ci,d as ie,DETAILS_KEYS as he,extractStructTagFromType as sa,getObjectFields as nr,getPackagerConfigs as ce,TickMath as Fe,TickUtil as ki,createFullClient as aa,deriveDynamicFieldIdByType as ca}from"@cetusprotocol/common-sdk";var Mi=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}buildPositionType(){return`${this._sdk.sdkOptions.clmm_pool.package_id}::position::Position`}async getPositionTransactionList({pos_id:t,origin_pos_id:e,full_rpc_url:i,pagination_args:n="all",order:r="ascending"}){let{FullClient:a}=this._sdk,l=[t];e&&l.push(e);let d;i?d=aa(new na({url:i})):d=a;let f={data:[],has_next_page:!1};try{let h=await d.queryTransactionBlocksByPage({ChangedObject:t},n,r);return h.data.forEach((w,P)=>{let k=Wo(w,P,l);f.data=[...f.data,...k]}),f.has_next_page=h.has_next_page,f.next_cursor=h.next_cursor,f}catch(h){$t("FetchError",h,{[he.METHOD_NAME]:"getPositionTransactionList"})}return f}async getPositionList(t,e=[],i=!0){let n=[],r=await this._sdk.FullClient.getOwnedObjectsByPage(t,{options:{showType:!0,showContent:!0,showDisplay:i,showOwner:!0},filter:{Package:this._sdk.sdkOptions.clmm_pool.package_id}}),a=e.length>0;for(let l of r.data)if(sa(l.data.type).full_address===this.buildPositionType()){let f=mi(l),h=`${f.pos_object_id}_getPositionList`;this._sdk.updateCache(h,f),a?e.includes(f.pool)&&n.push(f):n.push(f)}return n}async getPosition(t,e,i=!0,n=!0){let r=await this.getSimplePosition(e,n);return i&&(r=await this.updatePositionInfo(t,r)),r}async getPositionById(t,e=!0,i=!0){let n=await this.getSimplePosition(t,i);if(e){let r=await this._sdk.Pool.getPool(n.pool,!1);return await this.updatePositionInfo(r.position_manager.positions_handle,n)}return n}async getSimplePosition(t,e=!0){let i=`${t}_getPositionList`,n=this.getSimplePositionByCache(t);if(n===void 0){let r=await this.sdk.FullClient.getObject({id:t,options:{showContent:!0,showType:!0,showDisplay:e,showOwner:!0}});n=mi(r),this._sdk.updateCache(i,n)}return n}getSimplePositionByCache(t){let e=`${t}_getPositionList`;return this._sdk.getCache(e)}async getSimplePositionList(t,e=!0){let i=[],n=[];return t.forEach(r=>{let a=this.getSimplePositionByCache(r);a?i.push(a):n.push(r)}),n.length>0&&(await this._sdk.FullClient.batchGetObjects(n,{showOwner:!0,showContent:!0,showDisplay:e,showType:!0})).forEach(a=>{if(a.error==null){let l=mi(a);i.push(l);let d=`${l.pos_object_id}_getPositionList`;this._sdk.updateCache(d,l)}}),i}async updatePositionInfo(t,e){let i=await this.getPositionInfo(t,e.pos_object_id);return{...e,...i}}async getPositionInfo(t,e){try{let i=await this._sdk.FullClient.getDynamicFieldObject({parentId:t,name:{type:"0x2::object::ID",value:e}}),r=nr(i.data).value.fields.value;return He(r)}catch(i){return $t("FetchError",i,{[he.METHOD_NAME]:"getPositionInfo",[he.REQUEST_PARAMS]:{position_handle:t,position_id:e}})}}async getPositionInfoList(t){try{let e=[],i=[];return t.forEach(async r=>{let{position_handle:a,position_ids:l}=r;l.forEach(d=>{let f=ca(a,d,"0x2::object::ID","address");i.push(f)})}),i.length===0?[]:((await this._sdk.FullClient.batchGetObjects(i,{showContent:!0,showType:!0,showOwner:!0})).forEach(r=>{try{let l=nr(r.data).value.fields.value,d=He(l);e.push(d)}catch(a){console.log("getPositionInfoList error",a)}}),e)}catch(e){return $t("FetchError",e,{[he.METHOD_NAME]:"getPositionInfoList",[he.REQUEST_PARAMS]:{options:t}})}}buildFetchPosFee(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r=[t.coin_type_a,t.coin_type_b],a=[e.object(ce(i).global_config_id),e.object(t.pool_id),e.pure.address(t.position_id)];e.moveCall({target:`${n.published_at}::${Jt}::fetch_position_fees`,arguments:a,typeArguments:r})}parsedPosFeeData(t){let e={},i=t.events?.filter(n=>n.type.includes("fetcher_script::FetchPositionFeesEvent"));for(let n=0;n<i.length;n+=1){let{parsedJson:r}=i[n],a={position_id:r.position_id,fee_owned_a:r.fee_owned_a,fee_owned_b:r.fee_owned_b};e[r.position_id]=a}return e}async fetchPosFeeAmount(t){let{clmm_pool:e,integrate:i}=this.sdk.sdkOptions,n=new ae;for(let d of t)this.buildFetchPosFee(d,n);let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:ra("0x0")});r.error!=null&&R("InvalidPoolObject",`fetch position fee error code: ${r.error??"unknown error"}, please check config and position and pool object ids`,{[he.METHOD_NAME]:"fetchPosFeeAmount"});let a=[],l=this.parsedPosFeeData(r);for(let d=0;d<t.length;d+=1){let f=l[t[d].position_id];if(f){let h={fee_owned_a:f.fee_owned_a,fee_owned_b:f.fee_owned_b,position_id:t[d].position_id};a.push(h)}}return a}async batchFetchPositionFees(t){let e=[];for(let n of t){let r=await this._sdk.Position.getPositionById(n,!1),a=await this._sdk.Pool.getPool(r.pool,!1);e.push({pool_id:a.id,position_id:r.pos_object_id,coin_type_a:a.coin_type_a,coin_type_b:a.coin_type_b})}let i={};if(e.length>0){let n=await this.fetchPosFeeAmount(e);for(let r of n)i[r.position_id]=r;return i}return i}async createAddLiquidityFixTokenPayload(t,e,i,n,r){let a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:l,is_adjust_coin_b:d}=Ae(t);if(t=t,t.fix_amount_a&&l||!t.fix_amount_a&&d)return i=await Ut.buildAddLiquidityFixTokenForGas(this._sdk,a,t,e,i,n,r),i}return Ut.buildAddLiquidityFixToken(this._sdk,a,t,i,n,r)}async createAddLiquidityPayload(t,e,i,n){let{integrate:r,clmm_pool:a}=this._sdk.sdkOptions,l=Re(BigInt(t.tick_lower)).toString(),d=Re(BigInt(t.tick_upper)).toString(),f=[t.coin_type_a,t.coin_type_b];e=e||new ae;let h=!oa(t.pos_id),w=BigInt(t.max_amount_a),P=BigInt(t.max_amount_b),k,T;if(i==null||n==null){let S=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());k=Ci.buildCoinForAmount(e,S,w,t.coin_type_a,!1,!0),T=Ci.buildCoinForAmount(e,S,P,t.coin_type_b,!1,!0)}else k={target_coin:i,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},T={target_coin:n,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]};if(h)e.moveCall({target:`${r.published_at}::${Qt}::open_position_with_liquidity`,typeArguments:f,arguments:[e.object(ce(a).global_config_id),e.object(t.pool_id),e.pure.u32(Number(l)),e.pure.u32(Number(d)),k.target_coin,T.target_coin,e.pure.u64(t.max_amount_a),e.pure.u64(t.max_amount_b),e.pure.u128(t.delta_liquidity),e.object(Pi)]});else{let S=await this._sdk.FullClient.getOwnerCoinAssets(this._sdk.getSenderAddress());e=Ut.createCollectRewarderAndFeeParams(this._sdk,e,t,S,k.remain_coins,T.remain_coins),e.moveCall({target:`${r.published_at}::${Qt}::add_liquidity`,typeArguments:f,arguments:[e.object(ce(a).global_config_id),e.object(t.pool_id),e.object(t.pos_id),k.target_coin,T.target_coin,e.pure.u64(t.max_amount_a),e.pure.u64(t.max_amount_b),e.pure.u128(t.delta_liquidity),e.object(Pi)]})}return e}async removeLiquidityPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r="remove_liquidity";e=e||new ae;let a=[t.coin_type_a,t.coin_type_b],l=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());e=Ut.createCollectRewarderAndFeeParams(this._sdk,e,t,l);let d=[e.object(ce(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.pure.u128(t.delta_liquidity),e.pure.u64(t.min_amount_a),e.pure.u64(t.min_amount_b),e.object(Pi)];return e.moveCall({target:`${n.published_at}::${$e}::${r}`,typeArguments:a,arguments:d}),e}async closePositionPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new ae;let r=[t.coin_type_a,t.coin_type_b],a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());return e=Ut.createCollectRewarderAndFeeParams(this._sdk,e,t,a),e.moveCall({target:`${n.published_at}::${$e}::close_position`,typeArguments:r,arguments:[e.object(ce(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.pure.u64(t.min_amount_a),e.pure.u64(t.min_amount_b),e.object(Pi)]}),e}openPositionPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new ae;let r=[t.coin_type_a,t.coin_type_b],a=Re(BigInt(t.tick_lower)).toString(),l=Re(BigInt(t.tick_upper)).toString(),d=[e.object(ce(i).global_config_id),e.object(t.pool_id),e.pure.u32(Number(a)),e.pure.u32(Number(l))];return e.moveCall({target:`${n.published_at}::${$e}::open_position`,typeArguments:r,arguments:d}),e}async openPositionWithPricePayload(t,e){let{pool_id:i}=t,{clmm_pool:n,integrate:r}=this.sdk.sdkOptions;e=e||new ae;let a=await this.sdk.Pool.getPool(i,!1),l=Number(a.tick_spacing),d=0,f=0;if(t.is_full_range)d=ki.getMinIndex(l),f=ki.getMaxIndex(l);else{let{price_base_coin:P,min_price:k,max_price:T}=t;d=Fe.priceToInitializeTickIndex(P==="coin_a"?ie(k):ie(1).div(T),t.coin_decimals_a,t.coin_decimals_b,l),f=Fe.priceToInitializeTickIndex(P==="coin_a"?ie(T):ie(1).div(k),t.coin_decimals_a,t.coin_decimals_b,l)}let h=[a.coin_type_a,a.coin_type_b],w=[e.object(ce(n).global_config_id),e.object(t.pool_id),e.pure.u32(Number(Re(BigInt(d)))),e.pure.u32(Number(Re(BigInt(f))))];return e.moveCall({target:`${r.published_at}::${$e}::open_position`,typeArguments:h,arguments:w}),e}async collectFeePayload(t,e,i,n){e=e||new ae;let r=i||Ci.buildCoinWithBalance(BigInt(0),t.coin_type_a,e),a=n||Ci.buildCoinWithBalance(BigInt(0),t.coin_type_b,e);return this.createCollectFeePayload(t,e,r,a),e}createCollectFeePayload(t,e,i,n){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,l=[t.coin_type_a,t.coin_type_b],d=[e.object(ce(r).global_config_id),e.object(t.pool_id),e.object(t.pos_id),i,n];return e.moveCall({target:`${a.published_at}::${Qt}::collect_fee`,typeArguments:l,arguments:d}),e}async calculateAddLiquidityResultWithPrice(t){let{pool_id:e,slippage:i,refresh_pool_price:n,add_mode_params:r}=t,a=await this.sdk.Pool.getPool(e,n),l=Number(a.tick_spacing),d=0,f=0;if(r.is_full_range)d=ki.getMinIndex(l),f=ki.getMaxIndex(l);else{let{price_base_coin:j,min_price:K,max_price:B}=r;d=Fe.priceToInitializeTickIndex(j==="coin_a"?ie(K):ie(1).div(B),r.coin_decimals_a,r.coin_decimals_b,l),f=Fe.priceToInitializeTickIndex(j==="coin_a"?ie(B):ie(1).div(K),r.coin_decimals_a,r.coin_decimals_b,l)}if("liquidity"in t){let{liquidity:j}=t,K=Fe.tickIndexToSqrtPriceX64(d),B=Fe.tickIndexToSqrtPriceX64(f),{coin_amount_a:D,coin_amount_b:jt}=ir.getCoinAmountFromLiquidity(new Ze.default(j),new Ze.default(a.current_sqrt_price),K,B,!1),Z=ie(D).mul(1+i).toFixed(0,I.ROUND_UP),Lt=ie(jt).mul(1+i).toFixed(0,I.ROUND_UP);return{coin_amount_a:D,coin_amount_b:jt,coin_amount_limit_a:Z,coin_amount_limit_b:Lt,liquidity:j,tick_lower:d,tick_upper:f}}let{coin_amount:h,fix_amount_a:w}=t,{coin_amount_limit_a:P,coin_amount_limit_b:k,liquidity_amount:T,coin_amount_a:S,coin_amount_b:$}=ir.estLiquidityAndCoinAmountFromOneAmounts(d,f,new Ze.default(h),w,!0,i,new Ze.default(a.current_sqrt_price));return{coin_amount_a:S,coin_amount_b:$,coin_amount_limit_a:P,coin_amount_limit_b:k,liquidity:T,tick_lower:d,tick_upper:f,fix_amount_a:w}}async addLiquidityWithPricePayload(t,e,i,n){let{pool_id:r,calculate_result:a}=t,{coin_amount_limit_a:l,coin_amount_limit_b:d,liquidity:f,tick_lower:h,tick_upper:w}=a;e=e||new ae;let P=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityPayload({delta_liquidity:f,max_amount_a:l,max_amount_b:d,tick_lower:h,tick_upper:w,collect_fee:!1,rewarder_coin_types:[],coin_type_a:P.coin_type_a,coin_type_b:P.coin_type_b,pool_id:r,pos_id:""},e,i,n),e}async createAddLiquidityFixCoinWithPricePayload(t,e,i,n){let{pool_id:r,calculate_result:a}=t,{coin_amount_limit_a:l,coin_amount_limit_b:d,liquidity:f,tick_lower:h,tick_upper:w,fix_amount_a:P,coin_amount_a:k,coin_amount_b:T}=a;if(P===void 0)throw R("InvalidParams","fix_amount_a is undefined",{[he.METHOD_NAME]:"addLiquidityFixCoinCoinWithPricePayload",[he.REQUEST_PARAMS]:t});e=e||new ae;let S=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityFixTokenPayload({amount_a:P?k:l,amount_b:P?d:T,slippage:0,fix_amount_a:P,is_open:!0,tick_lower:h,tick_upper:w,collect_fee:!1,rewarder_coin_types:[],coin_type_a:S.coin_type_a,coin_type_b:S.coin_type_b,pool_id:r,pos_id:""},void 0,e,i,n),e}createCollectFeeNoSendPayload(t,e,i,n){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,l=[t.coin_type_a,t.coin_type_b],d=[e.object(ce(r).global_config_id),e.object(t.pool_id),e.object(t.pos_id),i,n];return e.moveCall({target:`${a.published_at}::${ii}::collect_fee`,typeArguments:l,arguments:d}),e}};var kn=re(Gt());import{Transaction as wn}from"@mysten/sui/transactions";import{normalizeSuiAddress as la}from"@mysten/sui/utils";import{CLOCK_ADDRESS as yn,CoinAssist as vn,DETAILS_KEYS as or,getPackagerConfigs as Pn,MathUtil as ua,normalizeCoinType as Cn,ZERO as Be}from"@cetusprotocol/common-sdk";var Ti=class{constructor(t){this._sdk=t,this.growthGlobal=[Be,Be,Be]}get sdk(){return this._sdk}async emissionsEveryDay(t){let i=(await this.sdk.Pool.getPool(t)).rewarder_infos;if(!i)return null;let n=[];for(let r of i){let a=ua.fromX64(new kn.default(r.emissions_per_second));n.push({emissions:Math.floor(a.toNumber()*60*60*24),coin_type:r.coin_type})}return n}async batchFetchPositionRewarders(t){let e=[];for(let n of t){let r=await this._sdk.Position.getPositionById(n,!1),a=await this._sdk.Pool.getPool(r.pool,!1);e.push({pool_id:a.id,position_id:r.pos_object_id,coin_type_a:a.coin_type_a,coin_type_b:a.coin_type_b,rewarder_types:a.rewarder_infos.map(l=>l.coin_type)})}let i={};if(e.length>0){let n=await this.fetchPosRewardersAmount(e);for(let r of n)i[r.position_id]=r.rewarder_amounts;return i}return i}async fetchPositionRewarders(t,e){let i={pool_id:t.id,position_id:e,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b,rewarder_types:t.rewarder_infos.map(r=>r.coin_type)};return(await this.fetchPosRewardersAmount([i]))[0].rewarder_amounts}async batchFetchPositionFees(t){return await this._sdk.Position.batchFetchPositionFees(t)}async fetchPosFeeAmount(t){return await this._sdk.Position.fetchPosFeeAmount(t)}buildFetchPosReward(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r=[t.coin_type_a,t.coin_type_b],a=[e.object(Pn(i).global_config_id),e.object(t.pool_id),e.pure.address(t.position_id),e.object(yn)];e.moveCall({target:`${n.published_at}::${Jt}::fetch_position_rewards`,arguments:a,typeArguments:r})}async fetchPosRewardersAmount(t){let e=new wn;for(let a of t)this.buildFetchPosReward(a,e);let i=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:e,sender:la("0x")});i.error!=null&&R("InvalidConfig",`fetch position rewards error code: ${i.error??"unknown error"}, please check config and params`,{[or.METHOD_NAME]:"fetchPosRewardersAmount",[or.REQUEST_PARAMS]:{params:t}});let n=this.parsedPosRewardData(i),r=[];for(let a=0;a<t.length;a+=1){let l=n[t[a].position_id];if(l){let d={pool_id:t[a].pool_id,position_id:t[a].position_id,rewarder_amounts:l.rewarder_amount.map((f,h)=>({amount_owned:f,coin_type:t[a].rewarder_types[h]}))};r.push(d)}}return r}parsedPosRewardData(t){let e={},i=t.events?.filter(n=>n.type.includes("fetcher_script::FetchPositionRewardsEvent"));for(let n=0;n<i.length;n+=1){let{parsedJson:r}=i[n],a={position_id:r.position_id,rewarder_amount:r.data};e[r.position_id]=a}return e}async fetchPoolRewardersAmount(t,e){let i=await this.sdk.Pool.getPool(e),n=await this.sdk.Position.getPositionList(t,[e]),r=[];for(let d of n)r.push({pool_id:i.id,position_id:d.pos_object_id,rewarder_types:i.rewarder_infos.map(f=>f.coin_type),coin_type_a:i.coin_type_a,coin_type_b:i.coin_type_b});let a=await this.fetchPosRewardersAmount(r),l=[Be,Be,Be];if(a!=null)for(let d of a)for(let f=0;f<d.rewarder_amounts.length;f+=1)l[f]=l[f].add(new kn.default(d.rewarder_amounts[f].amount_owned));return l}async getPoolLowerAndUpperTicks(t,e){let i=[],n=[];for(let r of e){let a=await this.sdk.Pool.getTickDataByIndex(t,r.tick_lower_index),l=await this.sdk.Pool.getTickDataByIndex(t,r.tick_upper_index);i.push(a),n.push(l)}return[i,n]}async collectRewarderPayload(t){let e=await this.sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress(),null),i=new wn;return i=Ut.createCollectRewarderAndFeeParams(this._sdk,i,t,e),i}async batchCollectRewardsPayload(t,e,i,n){let r=await this.sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress(),null);e=e||new wn;let a={};return t.forEach(l=>{let d=Cn(l.coin_type_a),f=Cn(l.coin_type_b);if(l.collect_fee){let w=a[d];w==null&&(i==null?w=vn.buildCoinForAmount(e,r,BigInt(0),d,!1):w={target_coin:i,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},a[d]=w);let P=a[f];P==null&&(n==null?P=vn.buildCoinForAmount(e,r,BigInt(0),f,!1):P={target_coin:n,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},a[f]=P),e=this._sdk.Position.createCollectFeeNoSendPayload({pool_id:l.pool_id,pos_id:l.pos_id,coin_type_a:l.coin_type_a,coin_type_b:l.coin_type_b},e,w.target_coin,P.target_coin)}let h=[];l.rewarder_coin_types.forEach(w=>{let P=Cn(w),k=a[w];k===void 0&&(k=vn.buildCoinForAmount(e,r,BigInt(0),P,!1),a[P]=k),h.push(k.target_coin)}),e=this.createCollectRewarderNoSendPayload(l,e,h)}),Object.keys(a).forEach(l=>{let d=a[l];d.is_mint_zero_coin&&zo(this.sdk,e,d.target_coin,l,this.sdk.getSenderAddress())}),e}createCollectRewarderPayload(t,e,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=Pn(n),l=[t.coin_type_a,t.coin_type_b];return t.rewarder_coin_types.forEach((d,f)=>{e&&e.moveCall({target:`${r.published_at}::${Qt}::collect_reward`,typeArguments:[...l,d],arguments:[e.object(a.global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.object(a.global_vault_id),i[f],e.object(yn)]})}),e}createCollectRewarderNoSendPayload(t,e,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=Pn(n),l=[t.coin_type_a,t.coin_type_b];return t.rewarder_coin_types.forEach((d,f)=>{e&&e.moveCall({target:`${r.published_at}::${ii}::collect_reward`,typeArguments:[...l,d],arguments:[e.object(a.global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.object(a.global_vault_id),i[f],e.object(yn)]})}),e}};var be=re(Gt());import{Transaction as rr}from"@mysten/sui/transactions";import{normalizeSuiAddress as sr}from"@mysten/sui/utils";import{d as Ne,DETAILS_KEYS as pe,extractStructTagFromType as ar,TickMath as cr,U64_MAX as da,ZERO as fa}from"@cetusprotocol/common-sdk";var El="amm_swap",Ol="Pool",Ai=class{constructor(t){this.calculateSingleImpact=(t,e)=>e.minus(t).div(e).mul(100);this._sdk=t}get sdk(){return this._sdk}calculateSwapFee(t){let e=Ne(0);return t.forEach(i=>{let n=i.base_paths.length;if(n>0){let r=i.base_paths[0],a=r.label==="Cetus"?new I(r.fee_rate).div(10**6):new I(r.fee_rate).div(10**9),l=Ne(r.input_amount).div(10**r.from_decimal).mul(a);if(e=e.add(l),n>1){let d=i.base_paths[1],f=r.direction?r.current_price:new I(1).div(r.current_price),h=d.direction?d.current_price:new I(1).div(d.current_price),w=d.label==="Cetus"?new I(d.fee_rate).div(10**6):new I(d.fee_rate).div(10**9),k=Ne(d.output_amount).div(10**d.to_decimal).mul(w).div(f.mul(h));e=e.add(k)}}}),e.toString()}calculateSwapPriceImpact(t){let e=Ne(0);return t.forEach(i=>{let n=i.base_paths.length;if(n===1){let r=i.base_paths[0],a=Ne(r.output_amount).div(10**r.to_decimal),l=Ne(r.input_amount).div(10**r.from_decimal),d=a.div(l),f=r.direction?new I(r.current_price):new I(1).div(r.current_price);e=e.add(this.calculateSingleImpact(d,f))}if(n===2){let r=i.base_paths[0],a=i.base_paths[1],l=r.direction?new I(r.current_price):new I(1).div(r.current_price),d=a.direction?new I(a.current_price):new I(1).div(a.current_price),f=l.mul(d),h=new I(a.output_amount).div(10**a.to_decimal),w=new I(r.input_amount).div(10**r.from_decimal),P=h.div(w);e=e.add(this.calculateSingleImpact(P,f))}}),e.toString()}async preSwapWithMultiPool(t){let{integrate:e}=this.sdk.sdkOptions,i=new rr,n=[t.coin_type_a,t.coin_type_b];for(let f=0;f<t.pool_ids.length;f+=1){let h=[i.object(t.pool_ids[f]),i.pure.bool(t.a2b),i.pure.bool(t.by_amount_in),i.pure.u64(t.amount)];i.moveCall({target:`${e.published_at}::${Jt}::calculate_swap_result`,arguments:h,typeArguments:n})}let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:sr("0x0")});r.error!=null&&R("InvalidConfig",`pre swap with multi pools error code: ${r.error??"unknown error"}, please check config and params`,{[pe.METHOD_NAME]:"preSwapWithMultiPool",[pe.REQUEST_PARAMS]:{params:t}});let a=r.events?.filter(f=>ar(f.type).name==="CalculatedSwapResultEvent");if(a.length===0)return null;a.length!==t.pool_ids.length&&R("ParamsLengthNotEqual","valueData.length !== params.pools.length",{[pe.METHOD_NAME]:"preSwapWithMultiPool",[pe.REQUEST_PARAMS]:{params:t}});let l=t.by_amount_in?fa:da,d=0;for(let f=0;f<a.length;f+=1)if(!a[f].parsedJson.data.is_exceed)if(t.by_amount_in){let h=new be.default(a[f].parsedJson.data.amount_out);h.gt(l)&&(d=f,l=h)}else{let h=new be.default(a[f].parsedJson.data.amount_out);h.lt(l)&&(d=f,l=h)}return this.transformSwapWithMultiPoolData({pool_address:t.pool_ids[d],a2b:t.a2b,by_amount_in:t.by_amount_in,amount:t.amount,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b},a[d].parsedJson)}async preSwap(t){let{integrate:e}=this.sdk.sdkOptions,i=new rr,n=[t.coin_type_a,t.coin_type_b],r=[i.object(t.pool.id),i.pure.bool(t.a2b),i.pure.bool(t.by_amount_in),i.pure.u64(t.amount)];i.moveCall({target:`${e.published_at}::${Jt}::calculate_swap_result`,arguments:r,typeArguments:n});let a=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:sr("0x0")});if(a.error!=null)return R("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[pe.METHOD_NAME]:"preSwap",[pe.REQUEST_PARAMS]:{params:t}});let l=a.events?.filter(d=>ar(d.type).name==="CalculatedSwapResultEvent");return l.length===0?R("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[pe.METHOD_NAME]:"preSwap",[pe.REQUEST_PARAMS]:{params:t}}):this.transformSwapData(t,l[0].parsedJson.data)}transformSwapData(t,e){let i=e.amount_in&&e.fee_amount?new be.default(e.amount_in).add(new be.default(e.fee_amount)).toString():"";return{pool_address:t.pool.id,current_sqrt_price:t.current_sqrt_price,estimated_amount_in:i,estimated_amount_out:e.amount_out,estimated_end_sqrt_price:e.after_sqrt_price,estimated_fee_amount:e.fee_amount,is_exceed:e.is_exceed,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in}}transformSwapWithMultiPoolData(t,e){let{data:i}=e;console.log("json data. ",i);let n=i.amount_in&&i.fee_amount?new be.default(i.amount_in).add(new be.default(i.fee_amount)).toString():"";return{pool_address:t.pool_address,estimated_amount_in:n,estimated_amount_out:i.amount_out,estimated_end_sqrt_price:i.after_sqrt_price,estimated_start_sqrt_price:i.step_results[0].current_sqrt_price,estimated_fee_amount:i.fee_amount,is_exceed:i.is_exceed,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in}}calculateRates(t){let{current_pool:e}=t,i=Uo(e),n;t.a2b?n=t.swap_ticks.sort((P,k)=>k.index-P.index):n=t.swap_ticks.sort((P,k)=>P.index-k.index);let r=Ko(t.a2b,t.by_amount_in,t.amount,i,n),a=!1;t.by_amount_in?a=r.amount_in.lt(t.amount):a=r.amount_out.lt(t.amount);let l=te.getDefaultSqrtPriceLimit(t.a2b);t.a2b&&r.next_sqrt_price.lt(l)&&(a=!0),!t.a2b&&r.next_sqrt_price.gt(l)&&(a=!0);let d=0;r.cross_tick_num>6&&r.cross_tick_num<40&&(d=22e3*(r.cross_tick_num-6)),r.cross_tick_num>40&&(a=!0);let f=cr.sqrtPriceX64ToPrice(i.current_sqrt_price,t.decimals_a,t.decimals_b).toNumber(),h=cr.sqrtPriceX64ToPrice(r.next_sqrt_price,t.decimals_a,t.decimals_b).toNumber(),w=Math.abs(f-h)/f*100;return{estimated_amount_in:r.amount_in,estimated_amount_out:r.amount_out,estimated_end_sqrt_price:r.next_sqrt_price,estimated_fee_amount:r.fee_amount,is_exceed:a,extra_compute_limit:d,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in,price_impact_pct:w}}async createSwapPayload(t,e){let i=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:n,is_adjust_coin_b:r}=Ae(t);if(t.a2b&&n||!t.a2b&&r)return await te.buildSwapTransactionForGas(this._sdk,t,i,e)}return te.buildSwapTransaction(this.sdk,t,i)}async createSwapWithoutTransferCoinsPayload(t,e){let i=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:n,is_adjust_coin_b:r}=Ae(t);if(t.a2b&&n||!t.a2b&&r)return await te.buildSwapTransactionWithoutTransferCoinsForGas(this._sdk,t,i,e)}return te.buildSwapTransactionWithoutTransferCoins(this.sdk,t,i)}};import{CLOCK_ADDRESS as _a,CoinAssist as ha,DETAILS_KEYS as we,getPackagerConfigs as Mn}from"@cetusprotocol/common-sdk";import{Transaction as lr}from"@mysten/sui/transactions";import{normalizeSuiAddress as pa}from"@mysten/sui/utils";var Si=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}async getClmmVestInfoList(){let{clmm_vest:t}=this._sdk.sdkOptions;if(t===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"getClmmVestInfo"});let e=[];try{let i=`${t.package_id}::clmm_vester::CreateEvent`,r=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:i})).data.map(a=>a.parsedJson.clmm_vester_id);return r.length>0&&(await this._sdk.FullClient.batchGetObjects(r,{showContent:!0,showType:!0})).forEach(l=>{let d=ee.parseClmmVestInfo(l),f=`${d.id}-ClmmVestInfo`;this._sdk.updateCache(f,d),e.push(d)}),e}catch(i){$t("ClmmVestFetchError",i,{[we.METHOD_NAME]:"getClmmVestInfoList"})}return[]}async getClmmVestInfo(t=!0){let{clmm_vest:e}=this._sdk.sdkOptions;if(e===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"getClmmVestInfo"});let{clmm_vest_id:i}=Mn(e),n=`${i}-ClmmVestInfo`,r=this._sdk.getCache(n,t);if(r)return r;try{let a=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0,showType:!0}}),l=ee.parseClmmVestInfo(a);return this._sdk.updateCache(n,l),l}catch(a){return $t("ClmmVestFetchError",a,{[we.METHOD_NAME]:"getClmmVestInfo",[we.REQUEST_PARAMS]:{clmm_vest_id:i}})}}async getPositionVesting(t){let{clmm_vest:e}=this._sdk.sdkOptions;if(e===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"getPositionVesting"});if(t.length===0)return[];let{clmm_vest_id:i}=Mn(e),n=new lr;t.forEach(l=>{let{clmm_pool_id:d,coin_type_a:f,coin_type_b:h,clmm_position_ids:w}=l;n.moveCall({package:e.published_at,module:"clmm_vester",function:"get_positions_vesting",typeArguments:[f,h],arguments:[n.object(i),n.object(d),n.pure.vector("id",w)]})});let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:pa("0x0")}),a=[];return r.events?.forEach(l=>{if(l.type.includes("clmm_vester::GetPositionsVestingEvent")){let{parsedJson:d}=l;a.push(...d.position_vestings.map(f=>ee.parsePositionVesting(f)))}}),a}buildRedeemPayload(t,e){let{clmm_vest:i}=this._sdk.sdkOptions;if(i===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"buildRedeemVestPayload"});let{versioned_id:n,clmm_vest_id:r,cetus_coin_type:a}=Mn(i);return e=e||new lr,t.forEach(l=>{let{clmm_pool_id:d,clmm_position_id:f,coin_type_a:h,coin_type_b:w,period:P}=l,k=e.moveCall({package:i.published_at,module:"clmm_vester",function:"redeem",arguments:[e.object(n),e.object(r),e.object(d),e.object(f),e.pure.u16(P),e.object(_a)],typeArguments:[h,w]}),T=ha.fromBalance(k,a,e);e.transferObjects([T],this._sdk.getSenderAddress())}),e}};import{FullRpcUrlMainnet as ma}from"@cetusprotocol/common-sdk";var ur={clmmConfig:{pools_id:"0xf699e7f2276f5c9a75944b37a0c5b5d9ddfd2471bf6242483b03ab2887d198d0",global_config_id:"0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f",global_vault_id:"0xce7bceef26d3ad1f6d9b6f13a953f053e6ed3ca77907516481ce99ae8e588f2b",admin_cap_id:"0x89c1a321291d15ddae5a086c9abc533dff697fde3d89e0ca836c41af73e36a75"},cetusConfig:{coin_list_id:"0x8cbc11d9e10140db3d230f50b4d30e9b721201c0083615441707ffec1ef77b23",launchpad_pools_id:"0x1098fac992eab3a0ab7acf15bb654fc1cf29b5a6142c4ef1058e6c408dd15115",clmm_pools_id:"0x15b6a27dd9ae03eb455aba03b39e29aad74abd3757b8e18c0755651b2ae5b71e",admin_cap_id:"0x39d78781750e193ce35c45ff32c6c0c3f2941fa3ddaf8595c90c555589ddb113",global_config_id:"0x0408fa4e4a4c03cc0de8f23d0c2bbfe8913d178713c9a271ed4080973fe42d8f",coin_list_handle:"0x49136005e90e28c4695419ed4194cc240603f1ea8eb84e62275eaff088a71063",launchpad_pools_handle:"0x5e194a8efcf653830daf85a85b52e3ae8f65dc39481d54b2382acda25068375c",clmm_pools_handle:"0x37f60eb2d9d227949b95da8fea810db3c32d1e1fa8ed87434fc51664f87d83cb"}},dr={env:"mainnet",full_rpc_url:ma,cetus_config:{package_id:"0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f",published_at:"0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f",config:ur.cetusConfig},clmm_pool:{package_id:"0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb",published_at:"0x75b2e9ecad34944b8d0c874e568c90db0cf9437f0d7392abfd4cb902972f3e40",version:12,config:ur.clmmConfig},integrate:{package_id:"0x996c4d9480708fb8b92aa7acf819fb0497b5ec8e65ba06601cae2fb6db3312c3",published_at:"0x2d8c2e0fc6dd25b0214b3fa747e0fd27fd54608142cd2e4f64c1cd350cc4add4"},stats_pools_url:"https://api-sui.cetus.zone/v2/sui/stats_pools"};import{FullRpcUrlTestnet as ga}from"@cetusprotocol/common-sdk";var fr={env:"testnet",full_rpc_url:ga,cetus_config:{package_id:"0x2933975c3f74ef7c31f512edead6c6ce3f58f8e8fdbea78770ec8d5abd8ff700",published_at:"0xb50a626294f743b40ea51c9cb75190f0e38c71f580981b5613aef910b67a2691",config:{coin_list_id:"",launchpad_pools_id:"",clmm_pools_id:"",admin_cap_id:"0x774656a83f4f625fcc4e4dbf103eb77caf2d8b8f114ad33f55b848be068267b9",global_config_id:"0x95275a022123c66682278e9df6b5bac4da9abcc29ab698b7b2a6213262a592fe",coin_list_handle:"",launchpad_pools_handle:"",clmm_pools_handle:""}},clmm_pool:{package_id:"0xf57e7759f82e09a61e1b6fbbc171b413a06b99e212c36c94757af88425766ab3",published_at:"0xd03a086cff7db0328b65f8592b5439e0dadb43dd9a04c1d725d79294cd044e32",config:{pools_id:"0x98b3667696684057c4637df3e76873d64ed6dd561b5fc4eae2f63cddbeab9749",global_config_id:"0x170708f8b3fdb938835171ea730916b4b4e920c74f2cf7cd986d2c7ad02ebd5e",global_vault_id:"0x51b48da7fd31af581a0e02cb9e1b33129c8a66d5e98577d5540c24b23a3050a6",admin_cap_id:"0x6a172bd35fb4cb6e7026530f6addb7e75cf5a6dd289f353a854cbfab4b3680cd",partners_id:"0xfd1e0c739cdbdbac7b2ee00c68a92bd6aa139a75545fc37f562434867b001542"}},integrate:{package_id:"0xdc286a7c1ca76d401e5789142e0b662e286a7551c689566c71396ed028a0963f",published_at:"0xdc286a7c1ca76d401e5789142e0b662e286a7551c689566c71396ed028a0963f",version:4},stats_pools_url:"https://api-sui.devcetus.com/v2/sui/stats_pools",clmm_vest:{package_id:"0x6c4d444678edcc7877b0e75500b5150372d08057620103fd9bd104d8dbf674ab",published_at:"0x6c4d444678edcc7877b0e75500b5150372d08057620103fd9bd104d8dbf674ab",config:{clmm_vest_id:"0x0ab5b2b043fab143a5352d9ff9f769e47e1616107222f90bc4ed61663edcf9eb",versioned_id:"0x173b642cf27fe7ee36cd5568f8fbf7a651b6776e2fe7916d11e899bd3585ee8c",cetus_coin_type:"0xc6c51938da9a5cf6d6dca692783ea7bdf4478f7b1fef693f58947848f84bcf89::cetus::CETUS"}}};var xi=class o extends ba{constructor(t){super(t),this._swap=new Ai(this),this._pool=new vi(this),this._position=new Mi(this),this._config=new ei(this),this._rewarder=new Ti(this),this._vest=new Si(this)}get Pool(){return this._pool}get Position(){return this._position}get CetusConfig(){return this._config}get Rewarder(){return this._rewarder}get Swap(){return this._swap}get Vest(){return this._vest}static createSDK(t){let{env:e="mainnet"}=t;return e==="mainnet"?o.createCustomSDK({...dr,...t}):o.createCustomSDK({...fr,...t})}static createCustomSDK(t){return new o(t)}};var ze=re(Gt());import{MathUtil as je,TickMath as _r}from"@cetusprotocol/common-sdk";var wa=new ze.default(365),ya=new ze.default(24),va=new ze.default(3600),Pa=new ze.default(.5);function uu(o,t,e,i){return wa.mul(ya).mul(va).mul(Pa).mul(o.mul(t).add(e).div(i))}function Ca(o,t,e,i,n,r){let a=new I(o.toString()).div(new I(10**e)),l=new I(t.toString()).div(new I(10**i));return a.mul(n).add(l.mul(r))}function du(o,t,e,i,n,r,a,l,d,f,h,w,P,k,T,S,$,j,K,B,D,jt,Z,Lt){let ne=new I(w),Zt=new I(P),oe=new I(S),L=new I($),Bt=new I(j),y=new I(K),s=new I(B),u=new I(D),_=new I(jt),p=new I(Z),g=new I(Lt),v=_r.tickIndexToSqrtPriceX64(t),C=_r.tickIndexToSqrtPriceX64(e),m=je.toX64Decimal(je.fromX64(v)).round(),c=je.toX64Decimal(je.fromX64(C)).round(),b=je.toX64Decimal(je.fromX64(i)).round(),A,x=ne.mul(new I(10**r)).mul(c.mul(m)).div(c.sub(m)).round(),E=Zt.mul(new I(10**a)).div(c.sub(m)).round();o<t?A=x:o>e?A=E:A=I.min(x,E);let q=A.mul(b.sub(m)),V=A.mul(c.sub(b)).div(b.mul(c)).div(new I(10**r)).mul(s).add(q.div(new I(10**a).mul(u))),zt=Ca(k,T,r,a,s,u),H=V.div(zt),z=A.eq(new I(0))?new I(0):new I(h/1e4).mul(oe).mul(new I(A.toString()).div(new I(n.toString()).add(new I(A.toString())))).div(V),ye=H.eq(new I(0))?new I(0):H.mul(new I(36500/7)).div(V),Q=L.div(new I(10**l)).mul(_).mul(ye),J=Bt.div(new I(10**d)).mul(p).mul(ye),Ge=y.div(new I(10**f)).mul(g).mul(ye);return{fee_apr:z,pos_rewarder_0_apr:Q,pos_rewarder_1_apr:J,pos_rewarder_2_apr:Ge}}function fu(o,t,e,i){let n=Math.max(o,e),a=Math.min(t,i)-n,l=t-o,d=i-e,f=new I(l.toString()),h=new I(d.toString()),w=new I(a.toString()),P=new I("0");return a<0?P=new I("0"):l===a?P=h.div(w):d===a?P=w.div(f):P=w.mul(w).div(h).div(f),P}var vu=xi;export{El as AMM_SWAP_MODULE,Js as BPS,xi as CetusClmmSDK,Ce as ClmmError,Na as ClmmExpectSwapModule,Jt as ClmmFetcherModule,$e as ClmmIntegratePoolModule,Qt as ClmmIntegratePoolV2Module,ii as ClmmIntegratePoolV3Module,ni as ClmmIntegrateRouterModule,bo as ClmmIntegrateRouterWithPartnerModule,wo as ClmmIntegrateUtilsModule,go as ClmmPartnerModule,po as ClmmPositionStatus,ke as ConfigErrorCode,ei as ConfigModule,qa as DeepbookClobV2Module,ja as DeepbookCustodianV2Module,Da as DeepbookEndpointsV2Module,Ol as POOL_STRUCT,co as PartnerErrorCode,De as PoolErrorCode,vi as PoolModule,Ye as PositionErrorCode,Mi as PositionModule,Ut as PositionUtils,Ti as RewarderModule,Rr as RouterErrorCode,so as SwapErrorCode,Ai as SwapModule,te as SwapUtils,lo as TypesErrorCode,Yi as UtilsErrorCode,ao as VestErrorCode,Si as VestModule,ee as VestUtils,Mc as buildClmmPositionName,pi as buildPool,Zo as buildPoolTransactionInfo,mi as buildPosition,He as buildPositionInfo,Wo as buildPositionTransactionInfo,gi as buildTickData,Ho as buildTickDataByEvent,zo as buildTransferCoin,bi as buildTransferCoinToSender,dr as clmmMainnet,fr as clmmTestnet,Ko as computeSwap,Us as computeSwapStep,vu as default,uu as estPoolAPR,du as estPositionAPRWithDeltaMethod,fu as estPositionAPRWithMultiMethod,Ae as findAdjustCoin,La as getDefaultSuiInputType,sc as getLowerSqrtPriceFromCoinA,cc as getLowerSqrtPriceFromCoinB,ac as getUpperSqrtPriceFromCoinA,lc as getUpperSqrtPriceFromCoinB,$t as handleError,R as handleMessageError,hc as newBits,mo as poolFilterEvenTypes,ho as poolLiquiditySnapshotType,Uo as transClmmpoolDataWithoutTicks};
2
2
  /*! Bundled license information:
3
3
 
4
4
  decimal.js/decimal.mjs: