@cetusprotocol/sui-clmm-sdk 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var Ao=Object.create;var Xe=Object.defineProperty;var So=Object.getOwnPropertyDescriptor;var xo=Object.getOwnPropertyNames;var Io=Object.getPrototypeOf,Oo=Object.prototype.hasOwnProperty;var Eo=(o,t)=>()=>(t||o((t={exports:{}}).exports,t),t.exports),Ro=(o,t)=>{for(var e in t)Xe(o,e,{get:t[e],enumerable:!0})},Rn=(o,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of xo(t))!Oo.call(o,n)&&n!==e&&Xe(o,n,{get:()=>t[n],enumerable:!(i=So(t,n))||i.enumerable});return o};var fe=(o,t,e)=>(e=o!=null?Ao(Io(o)):{},Rn(t||!o||!o.__esModule?Xe(e,"default",{value:o,enumerable:!0}):e,o)),Fo=o=>Rn(Xe({},"__esModule",{value:!0}),o);var re=Eo((to,Wi)=>{"use strict";(function(o,t){"use strict";function e(w,s){if(!w)throw new Error(s||"Assertion failed")}function i(w,s){w.super_=s;var u=function(){};u.prototype=s.prototype,w.prototype=new u,w.prototype.constructor=w}function n(w,s,u){if(n.isBN(w))return w;this.negative=0,this.words=null,this.length=0,this.red=null,w!==null&&((s==="le"||s==="be")&&(u=s,s=10),this._init(w||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=require("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(w,s){var u=w.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 "+w)}function l(w,s,u){var _=a(w,u);return u-1>=s&&(_|=a(w,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(w,s,u,_){for(var p=0,g=0,v=Math.min(w.length,u),C=s;C<v;C++){var m=w.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,y=_;y<m;y+=p)c=d(s,y,y+p,u),this.imuln(g),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c);if(C!==0){var x=1;for(c=d(s,y,s.length,u),y=0;y<C;y++)x*=u;this.imuln(x),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(w,s){w.words=s.words,w.length=s.length,w.negative=s.negative,w.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 b=["","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?_=b[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],y=k[s];_="";var x=this.clone();for(x.negative=0;!x.isZero();){var O=x.modrn(y).toString(s);x=x.idivn(y),x.isZero()?_=O+_:_=b[c-O.length]+O+_}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 S=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=S(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 I(w){for(var s=new Array(w.bitLength()),u=0;u<s.length;u++){var _=u/26|0,p=u%26;s[u]=w.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 Z(w,s,u){u.negative=s.negative^w.negative;var _=w.length+s.length|0;u.length=_,_=_-1|0;var p=w.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 y=m>>>26,x=m&67108863,O=Math.min(c,s.length-1),R=Math.max(0,c-w.length+1);R<=O;R++){var L=c-R|0;p=w.words[L]|0,g=s.words[R]|0,v=p*g+x,y+=v/67108864|0,x=v&67108863}u.words[c]=x|0,m=y|0}return m!==0?u.words[c]=m|0:u.length--,u._strip()}var V=function(s,u,_){var p=s.words,g=u.words,v=_.words,C=0,m,c,y,x=p[0]|0,O=x&8191,R=x>>>13,L=p[1]|0,z=L&8191,Q=L>>>13,oe=p[2]|0,J=oe&8191,et=oe>>>13,Me=p[3]|0,ot=Me&8191,rt=Me>>>13,Je=p[4]|0,st=Je&8191,at=Je>>>13,bn=p[5]|0,ct=bn&8191,lt=bn>>>13,yn=p[6]|0,ut=yn&8191,dt=yn>>>13,wn=p[7]|0,ft=wn&8191,_t=wn>>>13,vn=p[8]|0,ht=vn&8191,pt=vn>>>13,Pn=p[9]|0,mt=Pn&8191,gt=Pn>>>13,Cn=g[0]|0,bt=Cn&8191,yt=Cn>>>13,kn=g[1]|0,wt=kn&8191,vt=kn>>>13,Mn=g[2]|0,Pt=Mn&8191,Ct=Mn>>>13,Tn=g[3]|0,kt=Tn&8191,Mt=Tn>>>13,An=g[4]|0,Tt=An&8191,At=An>>>13,Sn=g[5]|0,St=Sn&8191,xt=Sn>>>13,xn=g[6]|0,It=xn&8191,Ot=xn>>>13,In=g[7]|0,Et=In&8191,Rt=In>>>13,On=g[8]|0,Ft=On&8191,Bt=On>>>13,En=g[9]|0,jt=En&8191,Nt=En>>>13;_.negative=s.negative^u.negative,_.length=19,m=Math.imul(O,bt),c=Math.imul(O,yt),c=c+Math.imul(R,bt)|0,y=Math.imul(R,yt);var yi=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(yi>>>26)|0,yi&=67108863,m=Math.imul(z,bt),c=Math.imul(z,yt),c=c+Math.imul(Q,bt)|0,y=Math.imul(Q,yt),m=m+Math.imul(O,wt)|0,c=c+Math.imul(O,vt)|0,c=c+Math.imul(R,wt)|0,y=y+Math.imul(R,vt)|0;var wi=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(wi>>>26)|0,wi&=67108863,m=Math.imul(J,bt),c=Math.imul(J,yt),c=c+Math.imul(et,bt)|0,y=Math.imul(et,yt),m=m+Math.imul(z,wt)|0,c=c+Math.imul(z,vt)|0,c=c+Math.imul(Q,wt)|0,y=y+Math.imul(Q,vt)|0,m=m+Math.imul(O,Pt)|0,c=c+Math.imul(O,Ct)|0,c=c+Math.imul(R,Pt)|0,y=y+Math.imul(R,Ct)|0;var vi=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(vi>>>26)|0,vi&=67108863,m=Math.imul(ot,bt),c=Math.imul(ot,yt),c=c+Math.imul(rt,bt)|0,y=Math.imul(rt,yt),m=m+Math.imul(J,wt)|0,c=c+Math.imul(J,vt)|0,c=c+Math.imul(et,wt)|0,y=y+Math.imul(et,vt)|0,m=m+Math.imul(z,Pt)|0,c=c+Math.imul(z,Ct)|0,c=c+Math.imul(Q,Pt)|0,y=y+Math.imul(Q,Ct)|0,m=m+Math.imul(O,kt)|0,c=c+Math.imul(O,Mt)|0,c=c+Math.imul(R,kt)|0,y=y+Math.imul(R,Mt)|0;var Pi=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Pi>>>26)|0,Pi&=67108863,m=Math.imul(st,bt),c=Math.imul(st,yt),c=c+Math.imul(at,bt)|0,y=Math.imul(at,yt),m=m+Math.imul(ot,wt)|0,c=c+Math.imul(ot,vt)|0,c=c+Math.imul(rt,wt)|0,y=y+Math.imul(rt,vt)|0,m=m+Math.imul(J,Pt)|0,c=c+Math.imul(J,Ct)|0,c=c+Math.imul(et,Pt)|0,y=y+Math.imul(et,Ct)|0,m=m+Math.imul(z,kt)|0,c=c+Math.imul(z,Mt)|0,c=c+Math.imul(Q,kt)|0,y=y+Math.imul(Q,Mt)|0,m=m+Math.imul(O,Tt)|0,c=c+Math.imul(O,At)|0,c=c+Math.imul(R,Tt)|0,y=y+Math.imul(R,At)|0;var Ci=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Ci>>>26)|0,Ci&=67108863,m=Math.imul(ct,bt),c=Math.imul(ct,yt),c=c+Math.imul(lt,bt)|0,y=Math.imul(lt,yt),m=m+Math.imul(st,wt)|0,c=c+Math.imul(st,vt)|0,c=c+Math.imul(at,wt)|0,y=y+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,y=y+Math.imul(rt,Ct)|0,m=m+Math.imul(J,kt)|0,c=c+Math.imul(J,Mt)|0,c=c+Math.imul(et,kt)|0,y=y+Math.imul(et,Mt)|0,m=m+Math.imul(z,Tt)|0,c=c+Math.imul(z,At)|0,c=c+Math.imul(Q,Tt)|0,y=y+Math.imul(Q,At)|0,m=m+Math.imul(O,St)|0,c=c+Math.imul(O,xt)|0,c=c+Math.imul(R,St)|0,y=y+Math.imul(R,xt)|0;var ki=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(ki>>>26)|0,ki&=67108863,m=Math.imul(ut,bt),c=Math.imul(ut,yt),c=c+Math.imul(dt,bt)|0,y=Math.imul(dt,yt),m=m+Math.imul(ct,wt)|0,c=c+Math.imul(ct,vt)|0,c=c+Math.imul(lt,wt)|0,y=y+Math.imul(lt,vt)|0,m=m+Math.imul(st,Pt)|0,c=c+Math.imul(st,Ct)|0,c=c+Math.imul(at,Pt)|0,y=y+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,y=y+Math.imul(rt,Mt)|0,m=m+Math.imul(J,Tt)|0,c=c+Math.imul(J,At)|0,c=c+Math.imul(et,Tt)|0,y=y+Math.imul(et,At)|0,m=m+Math.imul(z,St)|0,c=c+Math.imul(z,xt)|0,c=c+Math.imul(Q,St)|0,y=y+Math.imul(Q,xt)|0,m=m+Math.imul(O,It)|0,c=c+Math.imul(O,Ot)|0,c=c+Math.imul(R,It)|0,y=y+Math.imul(R,Ot)|0;var Mi=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Mi>>>26)|0,Mi&=67108863,m=Math.imul(ft,bt),c=Math.imul(ft,yt),c=c+Math.imul(_t,bt)|0,y=Math.imul(_t,yt),m=m+Math.imul(ut,wt)|0,c=c+Math.imul(ut,vt)|0,c=c+Math.imul(dt,wt)|0,y=y+Math.imul(dt,vt)|0,m=m+Math.imul(ct,Pt)|0,c=c+Math.imul(ct,Ct)|0,c=c+Math.imul(lt,Pt)|0,y=y+Math.imul(lt,Ct)|0,m=m+Math.imul(st,kt)|0,c=c+Math.imul(st,Mt)|0,c=c+Math.imul(at,kt)|0,y=y+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,y=y+Math.imul(rt,At)|0,m=m+Math.imul(J,St)|0,c=c+Math.imul(J,xt)|0,c=c+Math.imul(et,St)|0,y=y+Math.imul(et,xt)|0,m=m+Math.imul(z,It)|0,c=c+Math.imul(z,Ot)|0,c=c+Math.imul(Q,It)|0,y=y+Math.imul(Q,Ot)|0,m=m+Math.imul(O,Et)|0,c=c+Math.imul(O,Rt)|0,c=c+Math.imul(R,Et)|0,y=y+Math.imul(R,Rt)|0;var Ti=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Ti>>>26)|0,Ti&=67108863,m=Math.imul(ht,bt),c=Math.imul(ht,yt),c=c+Math.imul(pt,bt)|0,y=Math.imul(pt,yt),m=m+Math.imul(ft,wt)|0,c=c+Math.imul(ft,vt)|0,c=c+Math.imul(_t,wt)|0,y=y+Math.imul(_t,vt)|0,m=m+Math.imul(ut,Pt)|0,c=c+Math.imul(ut,Ct)|0,c=c+Math.imul(dt,Pt)|0,y=y+Math.imul(dt,Ct)|0,m=m+Math.imul(ct,kt)|0,c=c+Math.imul(ct,Mt)|0,c=c+Math.imul(lt,kt)|0,y=y+Math.imul(lt,Mt)|0,m=m+Math.imul(st,Tt)|0,c=c+Math.imul(st,At)|0,c=c+Math.imul(at,Tt)|0,y=y+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,y=y+Math.imul(rt,xt)|0,m=m+Math.imul(J,It)|0,c=c+Math.imul(J,Ot)|0,c=c+Math.imul(et,It)|0,y=y+Math.imul(et,Ot)|0,m=m+Math.imul(z,Et)|0,c=c+Math.imul(z,Rt)|0,c=c+Math.imul(Q,Et)|0,y=y+Math.imul(Q,Rt)|0,m=m+Math.imul(O,Ft)|0,c=c+Math.imul(O,Bt)|0,c=c+Math.imul(R,Ft)|0,y=y+Math.imul(R,Bt)|0;var Ai=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Ai>>>26)|0,Ai&=67108863,m=Math.imul(mt,bt),c=Math.imul(mt,yt),c=c+Math.imul(gt,bt)|0,y=Math.imul(gt,yt),m=m+Math.imul(ht,wt)|0,c=c+Math.imul(ht,vt)|0,c=c+Math.imul(pt,wt)|0,y=y+Math.imul(pt,vt)|0,m=m+Math.imul(ft,Pt)|0,c=c+Math.imul(ft,Ct)|0,c=c+Math.imul(_t,Pt)|0,y=y+Math.imul(_t,Ct)|0,m=m+Math.imul(ut,kt)|0,c=c+Math.imul(ut,Mt)|0,c=c+Math.imul(dt,kt)|0,y=y+Math.imul(dt,Mt)|0,m=m+Math.imul(ct,Tt)|0,c=c+Math.imul(ct,At)|0,c=c+Math.imul(lt,Tt)|0,y=y+Math.imul(lt,At)|0,m=m+Math.imul(st,St)|0,c=c+Math.imul(st,xt)|0,c=c+Math.imul(at,St)|0,y=y+Math.imul(at,xt)|0,m=m+Math.imul(ot,It)|0,c=c+Math.imul(ot,Ot)|0,c=c+Math.imul(rt,It)|0,y=y+Math.imul(rt,Ot)|0,m=m+Math.imul(J,Et)|0,c=c+Math.imul(J,Rt)|0,c=c+Math.imul(et,Et)|0,y=y+Math.imul(et,Rt)|0,m=m+Math.imul(z,Ft)|0,c=c+Math.imul(z,Bt)|0,c=c+Math.imul(Q,Ft)|0,y=y+Math.imul(Q,Bt)|0,m=m+Math.imul(O,jt)|0,c=c+Math.imul(O,Nt)|0,c=c+Math.imul(R,jt)|0,y=y+Math.imul(R,Nt)|0;var Si=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Si>>>26)|0,Si&=67108863,m=Math.imul(mt,wt),c=Math.imul(mt,vt),c=c+Math.imul(gt,wt)|0,y=Math.imul(gt,vt),m=m+Math.imul(ht,Pt)|0,c=c+Math.imul(ht,Ct)|0,c=c+Math.imul(pt,Pt)|0,y=y+Math.imul(pt,Ct)|0,m=m+Math.imul(ft,kt)|0,c=c+Math.imul(ft,Mt)|0,c=c+Math.imul(_t,kt)|0,y=y+Math.imul(_t,Mt)|0,m=m+Math.imul(ut,Tt)|0,c=c+Math.imul(ut,At)|0,c=c+Math.imul(dt,Tt)|0,y=y+Math.imul(dt,At)|0,m=m+Math.imul(ct,St)|0,c=c+Math.imul(ct,xt)|0,c=c+Math.imul(lt,St)|0,y=y+Math.imul(lt,xt)|0,m=m+Math.imul(st,It)|0,c=c+Math.imul(st,Ot)|0,c=c+Math.imul(at,It)|0,y=y+Math.imul(at,Ot)|0,m=m+Math.imul(ot,Et)|0,c=c+Math.imul(ot,Rt)|0,c=c+Math.imul(rt,Et)|0,y=y+Math.imul(rt,Rt)|0,m=m+Math.imul(J,Ft)|0,c=c+Math.imul(J,Bt)|0,c=c+Math.imul(et,Ft)|0,y=y+Math.imul(et,Bt)|0,m=m+Math.imul(z,jt)|0,c=c+Math.imul(z,Nt)|0,c=c+Math.imul(Q,jt)|0,y=y+Math.imul(Q,Nt)|0;var xi=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(xi>>>26)|0,xi&=67108863,m=Math.imul(mt,Pt),c=Math.imul(mt,Ct),c=c+Math.imul(gt,Pt)|0,y=Math.imul(gt,Ct),m=m+Math.imul(ht,kt)|0,c=c+Math.imul(ht,Mt)|0,c=c+Math.imul(pt,kt)|0,y=y+Math.imul(pt,Mt)|0,m=m+Math.imul(ft,Tt)|0,c=c+Math.imul(ft,At)|0,c=c+Math.imul(_t,Tt)|0,y=y+Math.imul(_t,At)|0,m=m+Math.imul(ut,St)|0,c=c+Math.imul(ut,xt)|0,c=c+Math.imul(dt,St)|0,y=y+Math.imul(dt,xt)|0,m=m+Math.imul(ct,It)|0,c=c+Math.imul(ct,Ot)|0,c=c+Math.imul(lt,It)|0,y=y+Math.imul(lt,Ot)|0,m=m+Math.imul(st,Et)|0,c=c+Math.imul(st,Rt)|0,c=c+Math.imul(at,Et)|0,y=y+Math.imul(at,Rt)|0,m=m+Math.imul(ot,Ft)|0,c=c+Math.imul(ot,Bt)|0,c=c+Math.imul(rt,Ft)|0,y=y+Math.imul(rt,Bt)|0,m=m+Math.imul(J,jt)|0,c=c+Math.imul(J,Nt)|0,c=c+Math.imul(et,jt)|0,y=y+Math.imul(et,Nt)|0;var Ii=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Ii>>>26)|0,Ii&=67108863,m=Math.imul(mt,kt),c=Math.imul(mt,Mt),c=c+Math.imul(gt,kt)|0,y=Math.imul(gt,Mt),m=m+Math.imul(ht,Tt)|0,c=c+Math.imul(ht,At)|0,c=c+Math.imul(pt,Tt)|0,y=y+Math.imul(pt,At)|0,m=m+Math.imul(ft,St)|0,c=c+Math.imul(ft,xt)|0,c=c+Math.imul(_t,St)|0,y=y+Math.imul(_t,xt)|0,m=m+Math.imul(ut,It)|0,c=c+Math.imul(ut,Ot)|0,c=c+Math.imul(dt,It)|0,y=y+Math.imul(dt,Ot)|0,m=m+Math.imul(ct,Et)|0,c=c+Math.imul(ct,Rt)|0,c=c+Math.imul(lt,Et)|0,y=y+Math.imul(lt,Rt)|0,m=m+Math.imul(st,Ft)|0,c=c+Math.imul(st,Bt)|0,c=c+Math.imul(at,Ft)|0,y=y+Math.imul(at,Bt)|0,m=m+Math.imul(ot,jt)|0,c=c+Math.imul(ot,Nt)|0,c=c+Math.imul(rt,jt)|0,y=y+Math.imul(rt,Nt)|0;var Oi=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Oi>>>26)|0,Oi&=67108863,m=Math.imul(mt,Tt),c=Math.imul(mt,At),c=c+Math.imul(gt,Tt)|0,y=Math.imul(gt,At),m=m+Math.imul(ht,St)|0,c=c+Math.imul(ht,xt)|0,c=c+Math.imul(pt,St)|0,y=y+Math.imul(pt,xt)|0,m=m+Math.imul(ft,It)|0,c=c+Math.imul(ft,Ot)|0,c=c+Math.imul(_t,It)|0,y=y+Math.imul(_t,Ot)|0,m=m+Math.imul(ut,Et)|0,c=c+Math.imul(ut,Rt)|0,c=c+Math.imul(dt,Et)|0,y=y+Math.imul(dt,Rt)|0,m=m+Math.imul(ct,Ft)|0,c=c+Math.imul(ct,Bt)|0,c=c+Math.imul(lt,Ft)|0,y=y+Math.imul(lt,Bt)|0,m=m+Math.imul(st,jt)|0,c=c+Math.imul(st,Nt)|0,c=c+Math.imul(at,jt)|0,y=y+Math.imul(at,Nt)|0;var Ei=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Ei>>>26)|0,Ei&=67108863,m=Math.imul(mt,St),c=Math.imul(mt,xt),c=c+Math.imul(gt,St)|0,y=Math.imul(gt,xt),m=m+Math.imul(ht,It)|0,c=c+Math.imul(ht,Ot)|0,c=c+Math.imul(pt,It)|0,y=y+Math.imul(pt,Ot)|0,m=m+Math.imul(ft,Et)|0,c=c+Math.imul(ft,Rt)|0,c=c+Math.imul(_t,Et)|0,y=y+Math.imul(_t,Rt)|0,m=m+Math.imul(ut,Ft)|0,c=c+Math.imul(ut,Bt)|0,c=c+Math.imul(dt,Ft)|0,y=y+Math.imul(dt,Bt)|0,m=m+Math.imul(ct,jt)|0,c=c+Math.imul(ct,Nt)|0,c=c+Math.imul(lt,jt)|0,y=y+Math.imul(lt,Nt)|0;var Ri=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Ri>>>26)|0,Ri&=67108863,m=Math.imul(mt,It),c=Math.imul(mt,Ot),c=c+Math.imul(gt,It)|0,y=Math.imul(gt,Ot),m=m+Math.imul(ht,Et)|0,c=c+Math.imul(ht,Rt)|0,c=c+Math.imul(pt,Et)|0,y=y+Math.imul(pt,Rt)|0,m=m+Math.imul(ft,Ft)|0,c=c+Math.imul(ft,Bt)|0,c=c+Math.imul(_t,Ft)|0,y=y+Math.imul(_t,Bt)|0,m=m+Math.imul(ut,jt)|0,c=c+Math.imul(ut,Nt)|0,c=c+Math.imul(dt,jt)|0,y=y+Math.imul(dt,Nt)|0;var Fi=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Fi>>>26)|0,Fi&=67108863,m=Math.imul(mt,Et),c=Math.imul(mt,Rt),c=c+Math.imul(gt,Et)|0,y=Math.imul(gt,Rt),m=m+Math.imul(ht,Ft)|0,c=c+Math.imul(ht,Bt)|0,c=c+Math.imul(pt,Ft)|0,y=y+Math.imul(pt,Bt)|0,m=m+Math.imul(ft,jt)|0,c=c+Math.imul(ft,Nt)|0,c=c+Math.imul(_t,jt)|0,y=y+Math.imul(_t,Nt)|0;var Bi=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(Bi>>>26)|0,Bi&=67108863,m=Math.imul(mt,Ft),c=Math.imul(mt,Bt),c=c+Math.imul(gt,Ft)|0,y=Math.imul(gt,Bt),m=m+Math.imul(ht,jt)|0,c=c+Math.imul(ht,Nt)|0,c=c+Math.imul(pt,jt)|0,y=y+Math.imul(pt,Nt)|0;var ji=(C+m|0)+((c&8191)<<13)|0;C=(y+(c>>>13)|0)+(ji>>>26)|0,ji&=67108863,m=Math.imul(mt,jt),c=Math.imul(mt,Nt),c=c+Math.imul(gt,jt)|0,y=Math.imul(gt,Nt);var Ni=(C+m|0)+((c&8191)<<13)|0;return C=(y+(c>>>13)|0)+(Ni>>>26)|0,Ni&=67108863,v[0]=yi,v[1]=wi,v[2]=vi,v[3]=Pi,v[4]=Ci,v[5]=ki,v[6]=Mi,v[7]=Ti,v[8]=Ai,v[9]=Si,v[10]=xi,v[11]=Ii,v[12]=Oi,v[13]=Ei,v[14]=Ri,v[15]=Fi,v[16]=Bi,v[17]=ji,v[18]=Ni,C!==0&&(v[19]=C,_.length++),_};Math.imul||(V=Z);function nt(w,s,u){u.negative=s.negative^w.negative,u.length=w.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-w.length+1);c<=m;c++){var y=g-c,x=w.words[y]|0,O=s.words[c]|0,R=x*O,L=R&67108863;v=v+(R/67108864|0)|0,L=L+C|0,C=L&67108863,v=v+(L>>>26)|0,p+=v>>>26,v&=67108863}u.words[g]=C,_=v,v=p}return _!==0?u.words[g]=_:u.length--,u._strip()}function $(w,s,u){return nt(w,s,u)}n.prototype.mulTo=function(s,u){var _,p=this.length+s.length;return this.length===10&&s.length===10?_=V(this,s,u):p<63?_=Z(this,s,u):p<1024?_=nt(this,s,u):_=$(this,s,u),_};function H(w,s){this.x=w,this.y=s}H.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},H.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},H.prototype.permute=function(s,u,_,p,g,v){for(var C=0;C<v;C++)p[C]=u[s[C]],g[C]=_[s[C]]},H.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),y=Math.sin(2*Math.PI/m),x=0;x<g;x+=m)for(var O=c,R=y,L=0;L<C;L++){var z=_[x+L],Q=p[x+L],oe=_[x+L+C],J=p[x+L+C],et=O*oe-R*J;J=O*J+R*oe,oe=et,_[x+L]=z+oe,p[x+L]=Q+J,_[x+L+C]=z-oe,p[x+L+C]=Q-J,L!==m&&(et=c*O-y*R,R=c*R+y*O,O=et)}},H.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},H.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}},H.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},H.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)},H.prototype.stub=function(s){for(var u=new Array(s),_=0;_<s;_++)u[_]=0;return u},H.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),y=new Array(p),x=new Array(p),O=new Array(p),R=_.words;R.length=p,this.convert13b(s.words,s.length,C,p),this.convert13b(u.words,u.length,y,p),this.transform(C,v,m,c,p,g),this.transform(y,v,x,O,p,g);for(var L=0;L<p;L++){var z=m[L]*x[L]-c[L]*O[L];c[L]=m[L]*O[L]+c[L]*x[L],m[L]=z}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^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),$(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=I(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 y=0;for(c=this.length-1;c>=0&&(y!==0||c>=p);c--){var x=this.words[c]|0;this.words[c]=y<<26-g|x>>>g,y=x&C}return m&&y!==0&&(m.words[m.length++]=y),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 y=0;y<c.length;y++)c.words[y]=0}var x=p.clone()._ishlnsubmul(g,1,m);x.negative===0&&(p=x,c&&(c.words[m]=1));for(var O=m-1;O>=0;O--){var R=(p.words[g.length+O]|0)*67108864+(p.words[g.length+O-1]|0);for(R=Math.min(R/v|0,67108863),p._ishlnsubmul(g,R,O);p.negative!==0;)R--,p.negative=0,p._ishlnsubmul(g,1,O),p.isZero()||(p.negative^=1);c&&(c.words[O]=R)}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(),y=u.clone();!u.isZero();){for(var x=0,O=1;(u.words[0]&O)===0&&x<26;++x,O<<=1);if(x>0)for(u.iushrn(x);x-- >0;)(p.isOdd()||g.isOdd())&&(p.iadd(c),g.isub(y)),p.iushrn(1),g.iushrn(1);for(var R=0,L=1;(_.words[0]&L)===0&&R<26;++R,L<<=1);if(R>0)for(_.iushrn(R);R-- >0;)(v.isOdd()||C.isOdd())&&(v.iadd(c),C.isub(y)),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,y=1;(_.words[0]&y)===0&&c<26;++c,y<<=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 x;return u.cmpn(1)===0?x=p:x=g,x.cmpn(0)<0&&x.iadd(s),x},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 W(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 zt={k256:null,p224:null,p192:null,p25519:null};function tt(w,s){this.name=w,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()}tt.prototype._tmp=function(){var s=new n(null);return s.words=new Array(Math.ceil(this.n/13)),s},tt.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},tt.prototype.split=function(s,u){s.iushrn(this.n,0,u)},tt.prototype.imulK=function(s){return s.imul(this.k)};function Qt(){tt.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}i(Qt,tt),Qt.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},Qt.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 ue(){tt.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}i(ue,tt);function ne(){tt.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}i(ne,tt);function de(){tt.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}i(de,tt),de.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(zt[s])return zt[s];var u;if(s==="k256")u=new Qt;else if(s==="p224")u=new ue;else if(s==="p192")u=new ne;else if(s==="p25519")u=new de;else throw new Error("Unknown prime "+s);return zt[s]=u,u};function W(w){if(typeof w=="string"){var s=n._prime(w);this.m=s.p,this.prime=s}else e(w.gtn(1),"modulus must be greater than 1"),this.m=w,this.prime=null}W.prototype._verify1=function(s){e(s.negative===0,"red works only with positives"),e(s.red,"red works only with red numbers")},W.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")},W.prototype.imod=function(s){return this.prime?this.prime.ireduce(s)._forceRed(this):(f(s,s.umod(this.m)._forceRed(this)),s)},W.prototype.neg=function(s){return s.isZero()?s.clone():this.m.sub(s)._forceRed(this)},W.prototype.add=function(s,u){this._verify2(s,u);var _=s.add(u);return _.cmp(this.m)>=0&&_.isub(this.m),_._forceRed(this)},W.prototype.iadd=function(s,u){this._verify2(s,u);var _=s.iadd(u);return _.cmp(this.m)>=0&&_.isub(this.m),_},W.prototype.sub=function(s,u){this._verify2(s,u);var _=s.sub(u);return _.cmpn(0)<0&&_.iadd(this.m),_._forceRed(this)},W.prototype.isub=function(s,u){this._verify2(s,u);var _=s.isub(u);return _.cmpn(0)<0&&_.iadd(this.m),_},W.prototype.shl=function(s,u){return this._verify1(s),this.imod(s.ushln(u))},W.prototype.imul=function(s,u){return this._verify2(s,u),this.imod(s.imul(u))},W.prototype.mul=function(s,u){return this._verify2(s,u),this.imod(s.mul(u))},W.prototype.isqr=function(s){return this.imul(s,s.clone())},W.prototype.sqr=function(s){return this.mul(s,s)},W.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 y=this.pow(c,p),x=this.pow(s,p.addn(1).iushrn(1)),O=this.pow(s,p),R=g;O.cmp(v)!==0;){for(var L=O,z=0;L.cmp(v)!==0;z++)L=L.redSqr();e(z<R);var Q=this.pow(y,new n(1).iushln(R-z-1));x=x.redMul(Q),y=Q.redSqr(),O=O.redMul(y),R=z}return x},W.prototype.invm=function(s){var u=s._invmp(this.m);return u.negative!==0?(u.negative=0,this.imod(u).redNeg()):this.imod(u)},W.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 y=u.words[g],x=c-1;x>=0;x--){var O=y>>x&1;if(v!==p[0]&&(v=this.sqr(v)),O===0&&C===0){m=0;continue}C<<=1,C|=O,m++,!(m!==_&&(g!==0||x!==0))&&(v=this.mul(v,p[C]),m=0,C=0)}c=26}return v},W.prototype.convertTo=function(s){var u=s.umod(this.m);return u===s?u.clone():u},W.prototype.convertFrom=function(s){var u=s.clone();return u.red=null,u},n.mont=function(s){return new Wt(s)};function Wt(w){W.call(this,w),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(Wt,W),Wt.prototype.convertTo=function(s){return this.imod(s.ushln(this.shift))},Wt.prototype.convertFrom=function(s){var u=this.imod(s.mul(this.rinv));return u.red=null,u},Wt.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)},Wt.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)},Wt.prototype.invm=function(s){var u=this.imod(s._invmp(this.m).mul(this.r2));return u._forceRed(this)}})(typeof Wi>"u"||Wi,to)});var cs={};Ro(cs,{AMM_SWAP_MODULE:()=>Jr,BPS:()=>yo,CetusClmmSDK:()=>Ge,ClmmError:()=>ye,ClmmExpectSwapModule:()=>Ho,ClmmFetcherModule:()=>Yt,ClmmIntegratePoolModule:()=>Be,ClmmIntegratePoolV2Module:()=>Xt,ClmmIntegratePoolV3Module:()=>je,ClmmIntegrateRouterModule:()=>he,ClmmIntegrateRouterWithPartnerModule:()=>Qi,ClmmIntegrateUtilsModule:()=>Ji,ClmmPartnerModule:()=>oi,ClmmPositionStatus:()=>zi,ConfigErrorCode:()=>we,ConfigModule:()=>Fe,DeepbookClobV2Module:()=>Zo,DeepbookCustodianV2Module:()=>Wo,DeepbookEndpointsV2Module:()=>zo,POOL_STRUCT:()=>Xr,PartnerErrorCode:()=>Ki,PoolErrorCode:()=>Se,PoolModule:()=>Ve,PositionErrorCode:()=>Re,PositionModule:()=>He,PositionUtils:()=>Gt,RewarderModule:()=>We,RouterErrorCode:()=>Yn,SwapErrorCode:()=>Ui,SwapModule:()=>Ze,SwapUtils:()=>ee,TypesErrorCode:()=>Hi,UtilsErrorCode:()=>ii,VestErrorCode:()=>Vi,VestModule:()=>ze,VestUtils:()=>ie,buildClmmPositionName:()=>Qr,buildPool:()=>De,buildPoolTransactionInfo:()=>cn,buildPosition:()=>Le,buildPositionInfo:()=>Oe,buildPositionTransactionInfo:()=>an,buildTickData:()=>$e,buildTickDataByEvent:()=>sn,buildTransferCoin:()=>ln,buildTransferCoinToSender:()=>Ue,clmmMainnet:()=>un,clmmTestnet:()=>fn,computeSwap:()=>rn,computeSwapStep:()=>bo,default:()=>as,estPoolAPR:()=>ns,estPositionAPRWithDeltaMethod:()=>rs,estPositionAPRWithMultiMethod:()=>ss,eventMainnetContractMaps:()=>dn,eventTestnetContractMaps:()=>_n,findAdjustCoin:()=>ve,getDefaultSuiInputType:()=>Go,getLowerSqrtPriceFromCoinA:()=>Kr,getLowerSqrtPriceFromCoinB:()=>Wr,getUpperSqrtPriceFromCoinA:()=>Hr,getUpperSqrtPriceFromCoinB:()=>Zr,handleError:()=>Ht,handleMessageError:()=>N,newBits:()=>zr,poolFilterEvenTypes:()=>Gi,poolLiquiditySnapshotType:()=>Zi,transClmmpoolDataWithoutTicks:()=>on});module.exports=Fo(cs);var To=require("@cetusprotocol/common-sdk");var ni=require("@mysten/sui/utils"),F=require("@cetusprotocol/common-sdk");var Dn="3.7.8",Bo=Dn,Ae=typeof Buffer=="function",Fn=typeof TextDecoder=="function"?new TextDecoder:void 0,Bn=typeof TextEncoder=="function"?new TextEncoder:void 0,jo="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",Ee=Array.prototype.slice.call(jo),Ye=(o=>{let t={};return o.forEach((e,i)=>t[e]=i),t})(Ee),No=/^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/,Dt=String.fromCharCode.bind(String),jn=typeof Uint8Array.from=="function"?Uint8Array.from.bind(Uint8Array):o=>new Uint8Array(Array.prototype.slice.call(o,0)),Ln=o=>o.replace(/=/g,"").replace(/[+\/]/g,t=>t=="+"?"-":"_"),$n=o=>o.replace(/[^A-Za-z0-9\+\/]/g,""),Un=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+=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},Li=typeof btoa=="function"?o=>btoa(o):Ae?o=>Buffer.from(o,"binary").toString("base64"):Un,qi=Ae?o=>Buffer.from(o).toString("base64"):o=>{let e=[];for(let i=0,n=o.length;i<n;i+=4096)e.push(Dt.apply(null,o.subarray(i,i+4096)));return Li(e.join(""))},ti=(o,t=!1)=>t?Ln(qi(o)):qi(o),qo=o=>{if(o.length<2){var t=o.charCodeAt(0);return t<128?o:t<2048?Dt(192|t>>>6)+Dt(128|t&63):Dt(224|t>>>12&15)+Dt(128|t>>>6&63)+Dt(128|t&63)}else{var t=65536+(o.charCodeAt(0)-55296)*1024+(o.charCodeAt(1)-56320);return Dt(240|t>>>18&7)+Dt(128|t>>>12&63)+Dt(128|t>>>6&63)+Dt(128|t&63)}},Do=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g,Vn=o=>o.replace(Do,qo),Nn=Ae?o=>Buffer.from(o,"utf8").toString("base64"):Bn?o=>qi(Bn.encode(o)):o=>Li(Vn(o)),Te=(o,t=!1)=>t?Ln(Nn(o)):Nn(o),qn=o=>Te(o,!0),Lo=/[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g,$o=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 Dt((e>>>10)+55296)+Dt((e&1023)+56320);case 3:return Dt((15&o.charCodeAt(0))<<12|(63&o.charCodeAt(1))<<6|63&o.charCodeAt(2));default:return Dt((31&o.charCodeAt(0))<<6|63&o.charCodeAt(1))}},Kn=o=>o.replace(Lo,$o),Hn=o=>{if(o=o.replace(/\s+/g,""),!No.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=Ye[o.charAt(r++)]<<18|Ye[o.charAt(r++)]<<12|(e=Ye[o.charAt(r++)])<<6|(i=Ye[o.charAt(r++)]),e===64?n.push(Dt(t>>16&255)):i===64?n.push(Dt(t>>16&255,t>>8&255)):n.push(Dt(t>>16&255,t>>8&255,t&255));return n.join("")},$i=typeof atob=="function"?o=>atob($n(o)):Ae?o=>Buffer.from(o,"base64").toString("binary"):Hn,Wn=Ae?o=>jn(Buffer.from(o,"base64")):o=>jn($i(o).split("").map(t=>t.charCodeAt(0))),Zn=o=>Wn(zn(o)),Uo=Ae?o=>Buffer.from(o,"base64").toString("utf8"):Fn?o=>Fn.decode(Wn(o)):o=>Kn($i(o)),zn=o=>$n(o.replace(/[-_]/g,t=>t=="-"?"+":"/")),Di=o=>Uo(zn(o)),Vo=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)},Gn=o=>({value:o,enumerable:!1,writable:!0,configurable:!0}),Qn=function(){let o=(t,e)=>Object.defineProperty(String.prototype,t,Gn(e));o("fromBase64",function(){return Di(this)}),o("toBase64",function(t){return Te(this,t)}),o("toBase64URI",function(){return Te(this,!0)}),o("toBase64URL",function(){return Te(this,!0)}),o("toUint8Array",function(){return Zn(this)})},Jn=function(){let o=(t,e)=>Object.defineProperty(Uint8Array.prototype,t,Gn(e));o("toBase64",function(t){return ti(this,t)}),o("toBase64URI",function(){return ti(this,!0)}),o("toBase64URL",function(){return ti(this,!0)})},Ko=()=>{Qn(),Jn()},ei={version:Dn,VERSION:Bo,atob:$i,atobPolyfill:Hn,btoa:Li,btoaPolyfill:Un,fromBase64:Di,toBase64:Te,encode:Te,encodeURI:qn,encodeURL:qn,utob:Vn,btou:Kn,decode:Di,isValid:Vo,fromUint8Array:ti,toUint8Array:Zn,extendString:Qn,extendUint8Array:Jn,extendBuiltins:Ko};var Xn=require("@cetusprotocol/common-sdk"),Ui=(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))(Ui||{}),Re=(r=>(r.InvalidTickEvent="InvalidTickEvent",r.InvalidPositionObject="InvalidPositionObject",r.InvalidPositionRewardObject="InvalidPositionRewardObject",r.InvalidParams="InvalidParams",r.FetchError="FetchError",r))(Re||{}),Se=(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))(Se||{}),Vi=(e=>(e.ClmmVestNotSet="ClmmVestNotSet",e.ClmmVestFetchError="ClmmVestFetchError",e))(Vi||{}),Ki=(e=>(e.NotFoundPartnerObject="NotFoundPartnerObject",e.InvalidPartnerRefFeeFields="InvalidPartnerRefFeeFields",e))(Ki||{}),we=(i=>(i.InvalidConfig="InvalidConfig",i.InvalidConfigHandle="InvalidConfigHandle",i.InvalidSimulateAccount="InvalidSimulateAccount",i))(we||{}),ii=(a=>(a.InvalidSendAddress="InvalidSendAddress",a.InvalidRecipientAddress="InvalidRecipientAddress",a.InvalidRecipientAndAmountLength="InvalidRecipientAndAmountLength",a.InsufficientBalance="InsufficientBalance",a.InvalidTarget="InvalidTarget",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a))(ii||{}),Yn=(a=>(a.InvalidCoin="InvalidCoin",a.NotFoundPath="NotFoundPath",a.NoDowngradeNeedParams="NoDowngradeNeedParams",a.InvalidSwapCountUrl="InvalidSwapCountUrl",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a.InvalidServerResponse="InvalidServerResponse",a))(Yn||{}),Hi=(t=>(t.InvalidType="InvalidType",t))(Hi||{}),ye=class extends Xn.BaseError{constructor(t,e,i){super(t,e||"UnknownError",i)}static isVaultsErrorCode(t,e){return this.isErrorCode(t,e)}},Ht=(o,t,e)=>{throw new ye(t.message,o,e)},N=(o,t,e)=>{throw new ye(t,o,e)};var Fe=class{constructor(t){this._cache={};this._sdk=t}get sdk(){return this._sdk}setTokenListCache(t){let{coin_list_handle:e}=(0,F.getPackagerConfigs)(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}=(0,F.getPackagerConfigs)(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((0,F.normalizeCoinType)(d)===(0,F.normalizeCoinType)(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 b={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]=b,this.updateCache(d,b,F.CACHE_TIME_24H)}else console.log(`not found ${l}`)}}return e}async getCoinConfigs(t=!1,e=!0){let{coin_list_handle:i}=(0,F.getPackagerConfigs)(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")&&N("FetchError",`when getCoinConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[F.DETAILS_KEYS.METHOD_NAME]:"getCoinConfigs"});let b=this.buildCoinConfig(h,e);this.updateCache(`${i}_${b.address}_getCoinConfig`,b,F.CACHE_TIME_24H),f.push({...b})}),this.updateCache(n,f,F.CACHE_TIME_24H),f}async getCoinConfig(t,e=!1,i=!0){let{coin_list_handle:n}=(0,F.getPackagerConfigs)(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:(0,F.fixCoinType)(t)}}});(l.error!=null||l.data?.content?.dataType!=="moveObject")&&N("FetchError",`when getCoinConfig get object error: ${l.error}, please check the rpc and contracts address config.`,{[F.DETAILS_KEYS.METHOD_NAME]:"getCoinConfig"});let d=this.buildCoinConfig(l,i);return this.updateCache(r,d,F.CACHE_TIME_24H),d}buildCoinConfig(t,e=!0){let i=(0,F.getObjectFields)(t);i=i.value.fields;let n={...i};return n.id=(0,F.getObjectId)(t),n.address=(0,F.extractStructTagFromType)(i.coin_type.fields.name).full_address,i.pyth_id&&(n.pyth_id=(0,ni.normalizeSuiObjectId)(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}=(0,F.getPackagerConfigs)(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")&&N("FetchError",`when getClmmPoolsConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[F.DETAILS_KEYS.METHOD_NAME]:"getClmmPoolConfigs"});let b=this.buildClmmPoolConfig(h,e);this.updateCache(`${b.pool_address}_getClmmPoolConfig`,b,F.CACHE_TIME_24H),f.push({...b})}),this.updateCache(n,f,F.CACHE_TIME_24H),f}async getClmmPoolConfig(t,e=!1,i=!0){let{clmm_pools_handle:n}=(0,F.getPackagerConfigs)(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,F.CACHE_TIME_24H),d}buildClmmPoolConfig(t,e=!0){let i=(0,F.getObjectFields)(t);i=i.value.fields;let n={...i};return n.id=(0,F.getObjectId)(t),n.pool_address=(0,ni.normalizeSuiObjectId)(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,e),n}async getLaunchpadPoolConfigs(t=!1,e=!0){let{launchpad_pools_handle:i}=(0,F.getPackagerConfigs)(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")&&N("FetchError",`when getCoinConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[F.DETAILS_KEYS.METHOD_NAME]:"getLaunchpadPoolConfigs"});let b=this.buildLaunchpadPoolConfig(h,e);this.updateCache(`${b.pool_address}_getLaunchpadPoolConfig`,b,F.CACHE_TIME_24H),f.push({...b})}),this.updateCache(n,f,F.CACHE_TIME_24H),f}async getLaunchpadPoolConfig(t,e=!1,i=!0){let{launchpad_pools_handle:n}=(0,F.getPackagerConfigs)(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,F.CACHE_TIME_24H),d}buildLaunchpadPoolConfig(t,e=!0){let i=(0,F.getObjectFields)(t);i=i.value.fields;let n={...i};n.id=(0,F.getObjectId)(t),n.pool_address=(0,ni.normalizeSuiObjectId)(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(ei.decode(n.regulation).replace(/%/g,"%25"))}catch{n.regulation=ei.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(ei.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=(0,F.getObjectPreviousTransactionDigest)(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((0,F.extractStructTagFromType)(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,F.CACHE_TIME_24H),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")&&N("InvalidConfigHandle",`when getCetusConfigHandle get objects error: ${n.error}, please check the rpc and contracts address config.`,{[F.DETAILS_KEYS.METHOD_NAME]:"getCetusConfigHandle"});let r=(0,F.getObjectFields)(n),a=(0,F.getObjectType)(n);switch((0,F.extractStructTagFromType)(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=F.CACHE_TIME_5MIN){let n=this._cache[t];n?(n.overdue_time=(0,F.getFutureTime)(i),n.value=e):n=new F.CachedContent(e,(0,F.getFutureTime)(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 vo=require("@mysten/sui/client"),Pe=require("@mysten/sui/transactions"),Ce=require("@mysten/sui/utils"),Po=fe(re());var Zi="position_liquidity_snapshot",zi=(i=>(i.Deleted="Deleted",i.Exists="Exists",i.NotExists="NotExists",i))(zi||{}),Gi=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","AddLiquidityV2Event","RemoveLiquidityV2Event"];var Vt=fe(re()),U=require("@cetusprotocol/common-sdk");var hi=require("@mysten/sui/transactions"),go=fe(re()),K=require("@cetusprotocol/common-sdk");var oi="partner",Be="pool_script",Xt="pool_script_v2",je="pool_script_v3",he="router",Qi="router_with_partner",Yt="fetcher_script",Ho="expect_swap",Ji="utils",Wo="custodian_v2",Zo="clob_v2",zo="endpoints_v2",Go=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 ye(`Unknown type for value: ${o}`,"InvalidType")};var _i=require("@mysten/sui/transactions"),mo=fe(re()),X=require("@cetusprotocol/common-sdk");var xe=9e15,be=1e9,Xi="0123456789abcdef",ai="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",ci="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Yi={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-xe,maxE:xe,crypto:!1},oo,_e,D=!0,ui="[DecimalError] ",ge=ui+"Invalid argument: ",ro=ui+"Precision limit exceeded",so=ui+"crypto unavailable",ao="[object Decimal]",Ut=Math.floor,qt=Math.pow,Qo=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Jo=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Xo=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,co=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,te=1e7,j=7,Yo=9007199254740991,tr=ai.length-1,tn=ci.length-1,M={toStringTag:ao};M.absoluteValue=M.abs=function(){var o=new this.constructor(this);return o.s<0&&(o.s=1),B(o)};M.ceil=function(){return B(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(ge+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())+j,i.rounding=1,e=er(i,ho(i,e)),i.precision=o,i.rounding=t,B(_e==2||_e==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,b=h.constructor;if(!h.isFinite()||h.isZero())return new b(h);for(D=!1,r=h.s*qt(h.s*h,1/3),!r||Math.abs(r)==1/0?(e=Lt(h.d),o=h.e,(r=(o-e.length+1)%3)&&(e+=r==1||r==-2?"0":"00"),r=qt(e,1/3),o=Ut((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 b(e),i.s=h.s):i=new b(r.toString()),a=(o=b.precision)+3;;)if(l=i,d=l.times(l).times(l),f=d.plus(h),i=Y(f.plus(h).times(l),f.plus(d),a+2,1),Lt(l.d).slice(0,a)===(e=Lt(i.d)).slice(0,a))if(e=e.slice(a-3,a+1),e=="9999"||!n&&e=="4999"){if(!n&&(B(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")&&(B(i,o+1,1),t=!i.times(i).times(i).eq(h));break}return D=!0,B(i,o,b.rounding,t)};M.decimalPlaces=M.dp=function(){var o,t=this.d,e=NaN;if(t){if(o=t.length-1,e=(o-Ut(this.e/j))*j,o=t[o],o)for(;o%10==0;o/=10)e--;e<0&&(e=0)}return e};M.dividedBy=M.div=function(o){return Y(this,new this.constructor(o))};M.dividedToIntegerBy=M.divToInt=function(o){var t=this,e=t.constructor;return B(Y(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 B(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/fi(4,o)).toString()):(o=16,t="2.3283064365386962890625e-10"),r=Ie(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 B(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=Ie(r,2,n,n,!0);else{o=1.4*Math.sqrt(i),o=o>16?16:o|0,n=n.times(1/fi(5,o)),n=Ie(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,B(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,Y(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()?se(t,i,n):new t(0):new t(NaN):o.isZero()?se(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,D=!1,e=e.times(e).minus(1).sqrt().plus(e),D=!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,D=!1,e=e.times(e).plus(1).sqrt().plus(e),D=!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?B(new r(n),o,t,!0):(r.precision=e=i-n.e,n=Y(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=se(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,b=h.precision,P=h.rounding;if(f.isFinite()){if(f.isZero())return new h(f);if(f.abs().eq(1)&&b+4<=tn)return a=se(h,b+4,P).times(.25),a.s=f.s,a}else{if(!f.s)return new h(NaN);if(b+4<=tn)return a=se(h,b+4,P).times(.5),a.s=f.s,a}for(h.precision=l=b+10,h.rounding=1,e=Math.min(28,l/j+2|0),o=e;o;--o)f=f.div(f.times(f).plus(1).sqrt().plus(1));for(D=!1,t=Math.ceil(l/j),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)),D=!0,B(a,h.precision=b,h.rounding=P,!0)};M.isFinite=function(){return!!this.d};M.isInteger=M.isInt=function(){return!!this.d&&Ut(this.e/j)>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,b=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(D=!1,l=b+k,a=me(f,l),i=t?li(h,l+10):me(o,l),d=Y(a,i,l,1),Ne(d.d,n=b,P))do if(l+=10,a=me(f,l),i=t?li(h,l+10):me(o,l),d=Y(a,i,l,1),!r){+Lt(d.d).slice(n+1,n+15)+1==1e14&&(d=B(d,b+1,0));break}while(Ne(d.d,n+=10,P));return D=!0,B(d,b,P)};M.minus=M.sub=function(o){var t,e,i,n,r,a,l,d,f,h,b,P,k=this,S=k.constructor;if(o=new S(o),!k.d||!o.d)return!k.s||!o.s?o=new S(NaN):k.d?o.s=-o.s:o=new S(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=S.precision,d=S.rounding,!f[0]||!P[0]){if(P[0])o.s=-o.s;else if(f[0])o=new S(k);else return new S(d===3?-0:0);return D?B(o,l,d):o}if(e=Ut(o.e/j),h=Ut(k.e/j),f=f.slice(),r=h-e,r){for(b=r<0,b?(t=f,r=-r,a=P.length):(t=P,e=h,a=f.length),i=Math.max(Math.ceil(l/j),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,b=i<a,b&&(a=i),i=0;i<a;i++)if(f[i]!=P[i]){b=f[i]<P[i];break}r=0}for(b&&(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]=te-1;--f[n],f[i]+=te}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=di(f,e),D?B(o,l,d):o):new S(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]?B(new i(e),i.precision,i.rounding):(D=!1,i.modulo==9?(t=Y(e,o.abs(),0,3,1),t.s*=o.s):t=Y(e,o,0,i.modulo,1),t=t.times(o),D=!0,e.minus(t))};M.naturalExponential=M.exp=function(){return en(this)};M.naturalLogarithm=M.ln=function(){return me(this)};M.negated=M.neg=function(){var o=new this.constructor(this);return o.s=-o.s,B(o)};M.plus=M.add=function(o){var t,e,i,n,r,a,l,d,f,h,b=this,P=b.constructor;if(o=new P(o),!b.d||!o.d)return!b.s||!o.s?o=new P(NaN):b.d||(o=new P(o.d||b.s===o.s?b:NaN)),o;if(b.s!=o.s)return o.s=-o.s,b.minus(o);if(f=b.d,h=o.d,l=P.precision,d=P.rounding,!f[0]||!h[0])return h[0]||(o=new P(b)),D?B(o,l,d):o;if(r=Ut(b.e/j),i=Ut(o.e/j),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/j),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)/te|0,f[n]%=te;for(t&&(f.unshift(t),++i),a=f.length;f[--a]==0;)f.pop();return o.d=f,o.e=di(f,i),D?B(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(ge+o);return e.d?(t=lo(e.d),o&&e.e+1>t&&(t=e.e+1)):t=NaN,t};M.round=function(){var o=this,t=o.constructor;return B(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())+j,i.rounding=1,e=nr(i,ho(i,e)),i.precision=o,i.rounding=t,B(_e>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(D=!1,f=Math.sqrt(+a),f==0||f==1/0?(t=Lt(l),(t.length+d)%2==0&&(t+="0"),f=Math.sqrt(t),d=Ut((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(Y(a,r,e+2,1)).times(.5),Lt(r.d).slice(0,e)===(t=Lt(i.d)).slice(0,e))if(t=t.slice(e-3,e+1),t=="9999"||!n&&t=="4999"){if(!n&&(B(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")&&(B(i,d+1,1),o=!i.times(i).eq(a));break}return D=!0,B(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=Y(e,new i(1).minus(e.times(e)).sqrt(),o+10,0),i.precision=o,i.rounding=t,B(_e==2||_e==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,b=h.constructor,P=h.d,k=(o=new b(o)).d;if(o.s*=h.s,!P||!P[0]||!k||!k[0])return new b(!o.s||P&&!P[0]&&!k||k&&!k[0]&&!P?NaN:!P||!k?o.s/0:o.s*0);for(e=Ut(h.e/j)+Ut(o.e/j),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%te|0,t=l/te|0;r[n]=(r[n]+t)%te|0}for(;!r[--a];)r.pop();return t?++e:r.shift(),o.d=r,o.e=di(r,e),D?B(o,b.precision,b.rounding):o};M.toBinary=function(o,t){return nn(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:(Zt(o,0,be),t===void 0?t=i.rounding:Zt(t,0,8),B(e,o+e.e+1,t))};M.toExponential=function(o,t){var e,i=this,n=i.constructor;return o===void 0?e=ae(i,!0):(Zt(o,0,be),t===void 0?t=n.rounding:Zt(t,0,8),i=B(new n(i),o+1,t),e=ae(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=ae(n):(Zt(o,0,be),t===void 0?t=r.rounding:Zt(t,0,8),i=B(new r(n),o+n.e+1,t),e=ae(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,b,P,k=this,S=k.d,I=k.constructor;if(!S)return new I(k);if(f=e=new I(1),i=d=new I(0),t=new I(i),r=t.e=lo(S)-k.e-1,a=r%j,t.d[0]=qt(10,a<0?j+a:a),o==null)o=r>0?t:f;else{if(l=new I(o),!l.isInt()||l.lt(f))throw Error(ge+l);o=l.gt(t)?r>0?t:f:l}for(D=!1,l=new I(Lt(S)),h=I.precision,I.precision=r=S.length*j*2;b=Y(l,t,0,1,1),n=e.plus(b.times(i)),n.cmp(o)!=1;)e=i,i=n,n=f,f=d.plus(b.times(n)),d=n,n=t,t=l.minus(b.times(n)),l=n;return n=Y(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=Y(f,i,r,1).minus(k).abs().cmp(Y(d,e,r,1).minus(k).abs())<1?[f,i]:[d,e],I.precision=h,D=!0,P};M.toHexadecimal=M.toHex=function(o,t){return nn(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:Zt(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]?(D=!1,e=Y(e,o,0,t,1).times(o),D=!0,B(e)):(o.s=e.s,e=o),e};M.toNumber=function(){return+this};M.toOctal=function(o,t){return nn(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(qt(+l,f));if(l=new d(l),l.eq(1))return l;if(i=d.precision,r=d.rounding,o.eq(1))return B(l,i,r);if(t=Ut(o.e/j),t>=o.d.length-1&&(e=f<0?-f:f)<=Yo)return n=uo(d,l,e,i),o.s<0?new d(1).div(n):B(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=qt(+l,f),t=e==0||!isFinite(e)?Ut(f*(Math.log("0."+Lt(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):(D=!1,d.rounding=l.s=1,e=Math.min(12,(t+"").length),n=en(o.times(me(l,i+e)),i),n.d&&(n=B(n,i+5,1),Ne(n.d,i,r)&&(t=i+10,n=B(en(o.times(me(l,t+e)),t),t+5,1),+Lt(n.d).slice(i+1,i+15)+1==1e14&&(n=B(n,i+1,0)))),n.s=a,D=!0,d.rounding=r,B(n,i,r))};M.toPrecision=function(o,t){var e,i=this,n=i.constructor;return o===void 0?e=ae(i,i.e<=n.toExpNeg||i.e>=n.toExpPos):(Zt(o,1,be),t===void 0?t=n.rounding:Zt(t,0,8),i=B(new n(i),o,t),e=ae(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):(Zt(o,1,be),t===void 0?t=i.rounding:Zt(t,0,8)),B(new i(e),o,t)};M.toString=function(){var o=this,t=o.constructor,e=ae(o,o.e<=t.toExpNeg||o.e>=t.toExpPos);return o.isNeg()&&!o.isZero()?"-"+e:e};M.truncated=M.trunc=function(){return B(new this.constructor(this),this.e+1,1)};M.valueOf=M.toJSON=function(){var o=this,t=o.constructor,e=ae(o,o.e<=t.toExpNeg||o.e>=t.toExpPos);return o.isNeg()?"-"+e:e};function Lt(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=j-i.length,e&&(r+=pe(e)),r+=i;a=o[t],i=a+"",e=j-i.length,e&&(r+=pe(e))}else if(a===0)return"0";for(;a%10===0;)a/=10;return r+a}function Zt(o,t,e){if(o!==~~o||o<t||o>e)throw Error(ge+o)}function Ne(o,t,e,i){var n,r,a,l;for(r=o[0];r>=10;r/=10)--t;return--t<0?(t+=j,n=0):(n=Math.ceil((t+1)/j),t%=j),r=qt(10,j-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)==qt(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)==qt(10,t-3)-1,a}function ri(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]+=Xi.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 er(o,t){var e,i,n;if(t.isZero())return t;i=t.d.length,i<32?(e=Math.ceil(i/3),n=(1/fi(4,e)).toString()):(e=16,n="2.3283064365386962890625e-10"),o.precision+=e,t=Ie(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 Y=(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,b,P,k,S,I,Z,V,nt,$,H,zt,tt,Qt,ue,ne,de,W,Wt,w=i.constructor,s=i.s==n.s?1:-1,u=i.d,_=n.d;if(!u||!u[0]||!_||!_[0])return new w(!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=te,k=j,h=Ut(i.e/k)-Ut(n.e/k)),W=_.length,ne=u.length,V=new w(s),nt=V.d=[],b=0;_[b]==(u[b]||0);b++);if(_[b]>(u[b]||0)&&h--,r==null?(tt=r=w.precision,a=w.rounding):l?tt=r+(i.e-n.e)+1:tt=r,tt<0)nt.push(1),S=!0;else{if(tt=tt/k+2|0,b=0,W==1){for(P=0,_=_[0],tt++;(b<ne||P)&&tt--;b++)Qt=P*d+(u[b]||0),nt[b]=Qt/_|0,P=Qt%_|0;S=P||b<ne}else{for(P=d/(_[0]+1)|0,P>1&&(_=o(_,P,d),u=o(u,P,d),W=_.length,ne=u.length),ue=W,$=u.slice(0,W),H=$.length;H<W;)$[H++]=0;Wt=_.slice(),Wt.unshift(0),de=_[0],_[1]>=d/2&&++de;do P=0,f=t(_,$,W,H),f<0?(zt=$[0],W!=H&&(zt=zt*d+($[1]||0)),P=zt/de|0,P>1?(P>=d&&(P=d-1),I=o(_,P,d),Z=I.length,H=$.length,f=t(I,$,Z,H),f==1&&(P--,e(I,W<Z?Wt:_,Z,d))):(P==0&&(f=P=1),I=_.slice()),Z=I.length,Z<H&&I.unshift(0),e($,I,H,d),f==-1&&(H=$.length,f=t(_,$,W,H),f<1&&(P++,e($,W<H?Wt:_,H,d))),H=$.length):f===0&&(P++,$=[0]),nt[b++]=P,f&&$[0]?$[H++]=u[ue]||0:($=[u[ue]],H=1);while((ue++<ne||$[0]!==void 0)&&tt--);S=$[0]!==void 0}nt[0]||nt.shift()}if(k==1)V.e=h,oo=S;else{for(b=1,P=nt[0];P>=10;P/=10)b++;V.e=b+h*k-1,B(V,l?r+V.e+1:r,a,S)}return V}})();function B(o,t,e,i){var n,r,a,l,d,f,h,b,P,k=o.constructor;t:if(t!=null){if(b=o.d,!b)return o;for(n=1,l=b[0];l>=10;l/=10)n++;if(r=t-n,r<0)r+=j,a=t,h=b[P=0],d=h/qt(10,n-a-1)%10|0;else if(P=Math.ceil((r+1)/j),l=b.length,P>=l)if(i){for(;l++<=P;)b.push(0);h=d=0,n=1,r%=j,a=r-j+1}else break t;else{for(h=l=b[P],n=1;l>=10;l/=10)n++;r%=j,a=r-j+n,d=a<0?0:h/qt(10,n-a-1)%10|0}if(i=i||t<0||b[P+1]!==void 0||(a<0?h:h%qt(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/qt(10,n-a):0:b[P-1])%10&1||e==(o.s<0?8:7)),t<1||!b[0])return b.length=0,f?(t-=o.e+1,b[0]=qt(10,(j-t%j)%j),o.e=-t||0):b[0]=o.e=0,o;if(r==0?(b.length=P,l=1,P--):(b.length=P+1,l=qt(10,j-r),b[P]=a>0?(h/qt(10,n-a)%qt(10,a)|0)*l:0),f)for(;;)if(P==0){for(r=1,a=b[0];a>=10;a/=10)r++;for(a=b[0]+=l,l=1;a>=10;a/=10)l++;r!=l&&(o.e++,b[0]==te&&(b[0]=1));break}else{if(b[P]+=l,b[P]!=te)break;b[P--]=0,l=1}for(r=b.length;b[--r]===0;)b.pop()}return D&&(o.e>k.maxE?(o.d=null,o.e=NaN):o.e<k.minE&&(o.e=0,o.d=[0])),o}function ae(o,t,e){if(!o.isFinite())return _o(o);var i,n=o.e,r=Lt(o.d),a=r.length;return t?(e&&(i=e-a)>0?r=r.charAt(0)+"."+r.slice(1)+pe(i):a>1&&(r=r.charAt(0)+"."+r.slice(1)),r=r+(o.e<0?"e":"e+")+o.e):n<0?(r="0."+pe(-n-1)+r,e&&(i=e-a)>0&&(r+=pe(i))):n>=a?(r+=pe(n+1-a),e&&(i=e-n-1)>0&&(r=r+"."+pe(i))):((i=n+1)<a&&(r=r.slice(0,i)+"."+r.slice(i)),e&&(i=e-a)>0&&(n+1===a&&(r+="."),r+=pe(i))),r}function di(o,t){var e=o[0];for(t*=j;e>=10;e/=10)t++;return t}function li(o,t,e){if(t>tr)throw D=!0,e&&(o.precision=e),Error(ro);return B(new o(ai),t,1,!0)}function se(o,t,e){if(t>tn)throw Error(ro);return B(new o(ci),t,e,!0)}function lo(o){var t=o.length-1,e=t*j+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 pe(o){for(var t="";o--;)t+="0";return t}function uo(o,t,e,i){var n,r=new o(1),a=Math.ceil(i/j+4);for(D=!1;;){if(e%2&&(r=r.times(t),io(r.d,a)&&(n=!0)),e=Ut(e/2),e===0){e=r.d.length-1,n&&r.d[e]===0&&++r.d[e];break}t=t.times(t),io(t.d,a)}return D=!0,r}function eo(o){return o.d[o.d.length-1]&1}function fo(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 en(o,t){var e,i,n,r,a,l,d,f=0,h=0,b=0,P=o.constructor,k=P.rounding,S=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?(D=!1,d=S):d=t,l=new P(.03125);o.e>-2;)o=o.times(l),b+=5;for(i=Math.log(qt(2,b))/Math.LN10*2+5|0,d+=i,e=r=a=new P(1),P.precision=d;;){if(r=B(r.times(o),d,1),e=e.times(++h),l=a.plus(Y(r,e,d,1)),Lt(l.d).slice(0,d)===Lt(a.d).slice(0,d)){for(n=b;n--;)a=B(a.times(a),d,1);if(t==null)if(f<3&&Ne(a.d,d-i,k,f))P.precision=d+=10,e=r=l=new P(1),h=0,f++;else return B(a,P.precision=S,k,D=!0);else return P.precision=S,a}a=l}}function me(o,t){var e,i,n,r,a,l,d,f,h,b,P,k=1,S=10,I=o,Z=I.d,V=I.constructor,nt=V.rounding,$=V.precision;if(I.s<0||!Z||!Z[0]||!I.e&&Z[0]==1&&Z.length==1)return new V(Z&&!Z[0]?-1/0:I.s!=1?NaN:Z?0:I);if(t==null?(D=!1,h=$):h=t,V.precision=h+=S,e=Lt(Z),i=e.charAt(0),Math.abs(r=I.e)<15e14){for(;i<7&&i!=1||i==1&&e.charAt(1)>3;)I=I.times(o),e=Lt(I.d),i=e.charAt(0),k++;r=I.e,i>1?(I=new V("0."+e),r++):I=new V(i+"."+e.slice(1))}else return f=li(V,h+2,$).times(r+""),I=me(new V(i+"."+e.slice(1)),h-S).plus(f),V.precision=$,t==null?B(I,$,nt,D=!0):I;for(b=I,d=a=I=Y(I.minus(1),I.plus(1),h,1),P=B(I.times(I),h,1),n=3;;){if(a=B(a.times(P),h,1),f=d.plus(Y(a,new V(n),h,1)),Lt(f.d).slice(0,h)===Lt(d.d).slice(0,h))if(d=d.times(2),r!==0&&(d=d.plus(li(V,h+2,$).times(r+""))),d=Y(d,new V(k),h,1),t==null)if(Ne(d.d,h-S,nt,l))V.precision=h+=S,f=a=I=Y(b.minus(1),b.plus(1),h,1),P=B(I.times(I),h,1),n=l=1;else return B(d,V.precision=$,nt,D=!0);else return V.precision=$,d;d=f,n+=2}}function _o(o){return String(o.s*o.s/0)}function si(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)%j,e<0&&(i+=j),i<n){for(i&&o.d.push(+t.slice(0,i)),n-=j;i<n;)o.d.push(+t.slice(i,i+=j));t=t.slice(i),i=j-t.length}else i-=n;for(;i--;)t+="0";o.d.push(+t),D&&(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 ir(o,t){var e,i,n,r,a,l,d,f,h;if(t.indexOf("_")>-1){if(t=t.replace(/(\d)_(?=\d)/g,"$1"),co.test(t))return si(o,t)}else if(t==="Infinity"||t==="NaN")return+t||(o.s=NaN),o.e=NaN,o.d=null,o;if(Jo.test(t))e=16,t=t.toLowerCase();else if(Qo.test(t))e=2;else if(Xo.test(t))e=8;else throw Error(ge+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=uo(i,new i(e),r,r*2)),f=ri(t,e,te),h=f.length-1,r=h;f[r]===0;--r)f.pop();return r<0?new i(o.s*0):(o.e=di(f,h),o.d=f,D=!1,a&&(o=Y(o,n,l*4)),d&&(o=o.times(Math.abs(d)<54?qt(2,d):qe.pow(2,d))),D=!0,o)}function nr(o,t){var e,i=t.d.length;if(i<3)return t.isZero()?t:Ie(o,2,t,t);e=1.4*Math.sqrt(i),e=e>16?16:e|0,t=t.times(1/fi(5,e)),t=Ie(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 Ie(o,t,e,i,n){var r,a,l,d,f=1,h=o.precision,b=Math.ceil(h/j);for(D=!1,d=e.times(e),l=new o(i);;){if(a=Y(l.times(d),new o(t++*t++),h,1),l=n?i.plus(a):i.minus(a),i=Y(a.times(d),new o(t++*t++),h,1),a=l.plus(i),a.d[b]!==void 0){for(r=b;a.d[r]===l.d[r]&&r--;);if(r==-1)break}r=l,l=i,i=a,a=r,f++}return D=!0,a.d.length=b+1,a}function fi(o,t){for(var e=o;--t;)e*=o;return e}function ho(o,t){var e,i=t.s<0,n=se(o,o.precision,1),r=n.times(.5);if(t=t.abs(),t.lte(r))return _e=i?4:1,t;if(e=t.divToInt(n),e.isZero())_e=i?3:2;else{if(t=t.minus(e.times(n)),t.lte(r))return _e=eo(e)?i?2:3:i?4:1,t;_e=eo(e)?i?1:4:i?3:2}return t.minus(n).abs()}function nn(o,t,e,i){var n,r,a,l,d,f,h,b,P,k=o.constructor,S=e!==void 0;if(S?(Zt(e,1,be),i===void 0?i=k.rounding:Zt(i,0,8)):(e=k.precision,i=k.rounding),!o.isFinite())h=_o(o);else{for(h=ae(o),a=h.indexOf("."),S?(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=ri(ae(P),10,n),P.e=P.d.length),b=ri(h,10,n),r=d=b.length;b[--d]==0;)b.pop();if(!b[0])h=S?"0p+0":"0";else{if(a<0?r--:(o=new k(o),o.d=b,o.e=r,o=Y(o,P,e,i,0,n),b=o.d,r=o.e,f=oo),a=b[e],l=n/2,f=f||b[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&&b[e-1]&1||i===(o.s<0?8:7)),b.length=e,f)for(;++b[--e]>n-1;)b[e]=0,e||(++r,b.unshift(1));for(d=b.length;!b[d-1];--d);for(a=0,h="";a<d;a++)h+=Xi.charAt(b[a]);if(S){if(d>1)if(t==16||t==8){for(a=t==16?4:3,--d;d%a;d++)h+="0";for(b=ri(h,n,t),d=b.length;!b[d-1];--d);for(a=1,h="1.";a<d;a++)h+=Xi.charAt(b[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 io(o,t){if(o.length>t)return o.length=t,!0}function or(o){return new this(o).abs()}function rr(o){return new this(o).acos()}function sr(o){return new this(o).acosh()}function ar(o,t){return new this(o).plus(t)}function cr(o){return new this(o).asin()}function lr(o){return new this(o).asinh()}function ur(o){return new this(o).atan()}function dr(o){return new this(o).atanh()}function fr(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=se(this,r,1).times(t.s>0?.25:.75),e.s=o.s):!t.d||o.isZero()?(e=t.s<0?se(this,i,n):new this(0),e.s=o.s):!o.d||t.isZero()?(e=se(this,r,1).times(.5),e.s=o.s):t.s<0?(this.precision=r,this.rounding=1,e=this.atan(Y(o,t,r,1)),t=se(this,r,1),this.precision=i,this.rounding=n,e=o.s<0?e.minus(t):e.plus(t)):e=this.atan(Y(o,t,r,1)),e}function _r(o){return new this(o).cbrt()}function hr(o){return B(o=new this(o),o.e+1,2)}function pr(o,t,e){return new this(o).clamp(t,e)}function mr(o){if(!o||typeof o!="object")throw Error(ui+"Object expected");var t,e,i,n=o.defaults===!0,r=["precision",1,be,"rounding",0,8,"toExpNeg",-xe,0,"toExpPos",0,xe,"maxE",0,xe,"minE",-xe,0,"modulo",0,9];for(t=0;t<r.length;t+=3)if(e=r[t],n&&(this[e]=Yi[e]),(i=o[e])!==void 0)if(Ut(i)===i&&i>=r[t+1]&&i<=r[t+2])this[e]=i;else throw Error(ge+e+": "+i);if(e="crypto",n&&(this[e]=Yi[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(so);else this[e]=!1;else throw Error(ge+e+": "+i);return this}function gr(o){return new this(o).cos()}function br(o){return new this(o).cosh()}function po(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,no(r)){f.s=r.s,D?!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++;D?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 si(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),co.test(r)?si(f,r):ir(f,r);if(d==="bigint")return r<0?(r=-r,f.s=-1):f.s=1,si(f,r.toString());throw Error(ge+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=mr,n.clone=po,n.isDecimal=no,n.abs=or,n.acos=rr,n.acosh=sr,n.add=ar,n.asin=cr,n.asinh=lr,n.atan=ur,n.atanh=dr,n.atan2=fr,n.cbrt=_r,n.ceil=hr,n.clamp=pr,n.cos=gr,n.cosh=br,n.div=yr,n.exp=wr,n.floor=vr,n.hypot=Pr,n.ln=Cr,n.log=kr,n.log10=Tr,n.log2=Mr,n.max=Ar,n.min=Sr,n.mod=xr,n.mul=Ir,n.pow=Or,n.random=Er,n.round=Rr,n.sign=Fr,n.sin=Br,n.sinh=jr,n.sqrt=Nr,n.sub=qr,n.sum=Dr,n.tan=Lr,n.tanh=$r,n.trunc=Ur,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 yr(o,t){return new this(o).div(t)}function wr(o){return new this(o).exp()}function vr(o){return B(o=new this(o),o.e+1,3)}function Pr(){var o,t,e=new this(0);for(D=!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 D=!0,new this(1/0);e=t}return D=!0,e.sqrt()}function no(o){return o instanceof qe||o&&o.toStringTag===ao||!1}function Cr(o){return new this(o).ln()}function kr(o,t){return new this(o).log(t)}function Mr(o){return new this(o).log(2)}function Tr(o){return new this(o).log(10)}function Ar(){return fo(this,arguments,-1)}function Sr(){return fo(this,arguments,1)}function xr(o,t){return new this(o).mod(t)}function Ir(o,t){return new this(o).mul(t)}function Or(o,t){return new this(o).pow(t)}function Er(o){var t,e,i,n,r=0,a=new this(1),l=[];if(o===void 0?o=this.precision:Zt(o,1,be),i=Math.ceil(o/j),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(so);else for(;r<i;)l[r++]=Math.random()*1e7|0;for(i=l[--r],o%=j,i&&o&&(n=qt(10,j-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-=j)l.shift();for(i=1,n=l[0];n>=10;n/=10)i++;i<j&&(e-=j-i)}return a.e=e,a.d=l,a}function Rr(o){return B(o=new this(o),o.e+1,this.rounding)}function Fr(o){return o=new this(o),o.d?o.d[0]?o.s:0*o.s:o.s||NaN}function Br(o){return new this(o).sin()}function jr(o){return new this(o).sinh()}function Nr(o){return new this(o).sqrt()}function qr(o,t){return new this(o).sub(t)}function Dr(){var o=0,t=arguments,e=new this(t[o]);for(D=!1;e.s&&++o<t.length;)e=e.plus(t[o]);return D=!0,B(e,this.precision,this.rounding)}function Lr(o){return new this(o).tan()}function $r(o){return new this(o).tanh()}function Ur(o){return B(o=new this(o),o.e+1,1)}M[Symbol.for("nodejs.util.inspect.custom")]=M.toString;M[Symbol.toStringTag]="Decimal";var qe=M.constructor=po(Yi);ai=new qe(ai);ci=new qe(ci);var E=qe;function ve(o){let t=X.CoinAssist.isSuiCoin(o.coin_type_a),e=X.CoinAssist.isSuiCoin(o.coin_type_b);return{is_adjust_coin_a:t,is_adjust_coin_b:e}}function Vr(o,t){return E.ceil(E(o).div(1+t)).toString()}var Gt=class{static createCollectRewarderAndFeeParams(t,e,i,n,r,a){r===void 0&&(r=[...n]),a===void 0&&(a=[...n]);let l=(0,X.normalizeCoinType)(i.coin_type_a),d=(0,X.normalizeCoinType)(i.coin_type_b);if(i.collect_fee){let h=X.CoinAssist.buildCoinForAmount(e,r,BigInt(0),l,!1);r=h.remain_coins;let b=X.CoinAssist.buildCoinForAmount(e,a,BigInt(0),d,!1);a=b.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,b.target_coin)}let f=[];return i.rewarder_coin_types.forEach(h=>{switch((0,X.normalizeCoinType)(h)){case l:f.push(X.CoinAssist.buildCoinForAmount(e,r,BigInt(0),h,!1).target_coin);break;case d:f.push(X.CoinAssist.buildCoinForAmount(e,a,BigInt(0),h,!1).target_coin);break;default:f.push(X.CoinAssist.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=X.CoinAssist.selectCoinAssetGreaterThanOrEqual(e,i).selected_coins,a=X.CoinAssist.calculateTotalBalance(e);if(r.length===0&&N("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let l=await t.FullClient.calculationTxGas(_i.Transaction.from(n));if(X.CoinAssist.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&&N("InsufficientBalance","gas Insufficient balance");let h=new _i.Transaction;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}=ve(i),f=d?i.amount_a:i.amount_b,h=await this.adjustTransactionForGas(t,X.CoinAssist.getCoinAssets(d?i.coin_type_a:i.coin_type_b,e),BigInt(f),r),{fixAmount:b}=h,P=h.newTx;if(P!=null){let k,S;if(d)i.amount_a=Number(b),k=this.buildAddLiquidityFixTokenCoinInput(P,!i.fix_amount_a,b.toString(),i.slippage,i.coin_type_a,e,!1,!0),S=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(b),k=this.buildAddLiquidityFixTokenCoinInput(P,!i.fix_amount_a,i.amount_a,i.slippage,i.coin_type_a,e,!1,!0),S=this.buildAddLiquidityFixTokenCoinInput(P,i.fix_amount_a,b.toString(),i.slippage,i.coin_type_b,e,!1,!0),i=this.fixAddLiquidityFixTokenParams(i,n.slippage,n.cur_sqrt_price),this.buildAddLiquidityFixTokenArgs(P,t,e,i,k,S)}return r}static async buildAddLiquidityFixToken(t,e,i,n,r,a){n=n||new _i.Transaction;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?X.CoinAssist.buildCoinForAmountInterval(t,a,{amount_second:BigInt(Vr(i,n)),amount_first:BigInt(i)},r,l,d):X.CoinAssist.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=X.ClmmPoolUtil.estLiquidityAndCoinAmountFromOneAmounts(Number(t.tick_lower),Number(t.tick_upper),new mo.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 b=(0,X.getPackagerConfigs)(f),P=n.is_open?[t.object(b.global_config_id),t.object(n.pool_id),t.pure.u32(Number((0,X.asUintN)(BigInt(n.tick_lower)).toString())),t.pure.u32(Number((0,X.asUintN)(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(X.CLOCK_ADDRESS)]:[t.object(b.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(X.CLOCK_ADDRESS)];return t.moveCall({target:`${h.published_at}::${Xt}::${d}`,typeArguments:l,arguments:P}),t}static checkCoinThreshold(t,e,i,n,r,a){e&&i.moveCall({target:`${t.sdkOptions.integrate.published_at}::${he}::check_coin_threshold`,typeArguments:[a],arguments:[n,i.pure.u64(r)]})}};var ee=class o{static getDefaultSqrtPriceLimit(t){return new go.default(t?K.MIN_SQRT_PRICE:K.MAX_SQRT_PRICE)}static getDefaultOtherAmountThreshold(t){return t?K.ZERO:K.U64_MAX}static async buildSwapTransactionForGas(t,e,i,n){let r=this.buildSwapTransaction(t,e,i);r.setSender(t.getSenderAddress());let a=await this.adjustTransactionForGas(t,K.CoinAssist.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=K.CoinAssist.buildCoinForAmount(r,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a),h=K.CoinAssist.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}=(0,K.getPackagerConfigs)(a);h===void 0&&N("InvalidConfig","clmm.config.global_config_id is undefined");let b=e.swap_partner!==void 0,P=b?e.a2b?"swap_a2b_with_partner":"swap_b2a_with_partner":e.a2b?"swap_a2b":"swap_b2a",k=b?[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(K.CLOCK_ADDRESS)]:[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(K.CLOCK_ADDRESS)];return t.moveCall({target:`${l.published_at}::${Xt}::${P}`,typeArguments:f,arguments:k}),t}static async adjustTransactionForGas(t,e,i,n){n.setSender(t.getSenderAddress());let r=K.CoinAssist.selectCoinAssetGreaterThanOrEqual(e,i).selected_coins,a=K.CoinAssist.calculateTotalBalance(e);if(r.length===0&&N("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let l=await t.FullClient.calculationTxGas(n);if(K.CoinAssist.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&&N("InsufficientBalance","gas Insufficient balance");let h=new hi.Transaction;return{fixAmount:i,newTx:h}}}return{fixAmount:i}}static buildSwapTransaction(t,e,i){let n=new hi.Transaction;n.setSender(t.getSenderAddress());let r=K.CoinAssist.buildCoinForAmount(n,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1),a=K.CoinAssist.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=(0,K.adjustForSlippage)(a,i.slippage,!i.by_amount_in);e.amount_limit=l.toString()}catch(r){Ht("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,K.CoinAssist.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=K.CoinAssist.buildCoinForAmount(r,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1,!0),b=K.CoinAssist.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,b);r=P.tx,a=P.txRes}return{tx:r,coin_ab_s:a}}static buildSwapTransactionWithoutTransferCoins(t,e,i){let n=new hi.Transaction;n.setSender(t.getSenderAddress());let r=K.CoinAssist.buildCoinForAmount(n,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1,!0),a=K.CoinAssist.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}=(0,K.getPackagerConfigs)(l);h===void 0&&N("InvalidConfig","clmm.config.global_config_id is undefined");let b=i.swap_partner!==void 0,P=b?"swap_with_partner":"swap",k=b?Qi:he,S=b?[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(K.CLOCK_ADDRESS)]:[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(K.CLOCK_ADDRESS)],I=[i.coin_type_a,i.coin_type_b],Z=e.moveCall({target:`${d.published_at}::${k}::${P}`,typeArguments:I,arguments:S});if(i.by_amount_in){let V=i.a2b?i.coin_type_b:i.coin_type_a,nt=i.a2b?Z[1]:Z[0],$=Number(i.amount_limit);Gt.checkCoinThreshold(t,i.by_amount_in,e,nt,$,V)}return{tx:e,txRes:Z}}};function Kr(o,t,e){let i=t.mul(e).shln(64),n=t.shln(64).add(o.mul(e));return K.MathUtil.divRoundUp(i,n)}function Hr(o,t,e){let i=t.mul(e).shln(64),n=t.shln(64).sub(o.mul(e));return K.MathUtil.divRoundUp(i,n)}function Wr(o,t,e){return e.sub(K.MathUtil.divRoundUp(o.shln(64),t))}function Zr(o,t,e){return e.add(o.shln(64).div(t))}function zr(o){let t=new Vt.default(o);return t.lt(U.ZERO)?{bits:t.neg().xor(new Vt.default(2).pow(new Vt.default(64)).sub(new Vt.default(1))).add(new Vt.default(1)).toString()}:{bits:t.toString()}}function on(o){return{coin_type_a:o.coin_type_a,coin_type_b:o.coin_type_b,current_sqrt_price:new Vt.default(o.current_sqrt_price),current_tick_index:o.current_tick_index,fee_growth_global_a:new Vt.default(o.fee_growth_global_a),fee_growth_global_b:new Vt.default(o.fee_growth_global_b),fee_protocol_coin_a:new Vt.default(o.fee_protocol_coin_a),fee_protocol_coin_b:new Vt.default(o.fee_protocol_coin_b),fee_rate:new Vt.default(o.fee_rate),liquidity:new Vt.default(o.liquidity),tick_indexes:[],tick_spacing:Number(o.tick_spacing),ticks:[],collection_name:""}}function bo(o,t,e,i,n,r){if(e===U.ZERO)return{amount_in:U.ZERO,amount_out:U.ZERO,next_sqrt_price:t,fee_amount:U.ZERO};let a=o.gte(t),l,d,f,h;if(r){let b=U.MathUtil.checkMulDivFloor(i,U.MathUtil.checkUnsignedSub(U.FEE_RATE_DENOMINATOR,n),U.FEE_RATE_DENOMINATOR,64),P=(0,U.getDeltaUpFromInput)(o,t,e,a);P.gt(b)?(l=b,h=U.MathUtil.checkUnsignedSub(i,b),f=(0,U.getNextSqrtPriceFromInput)(o,e,b,a)):(l=P,h=U.MathUtil.checkMulDivCeil(l,n,U.FEE_RATE_DENOMINATOR.sub(n),64),f=t),d=(0,U.getDeltaDownFromOutput)(o,f,e,a)}else{let b=(0,U.getDeltaDownFromOutput)(o,t,e,a);b.gt(i)?(d=i,f=(0,U.getNextSqrtPriceFromOutput)(o,e,i,a)):(d=b,f=t),l=(0,U.getDeltaUpFromInput)(o,f,e,a),h=U.MathUtil.checkMulDivCeil(l,n,U.FEE_RATE_DENOMINATOR.sub(n),64)}return{amount_in:l,amount_out:d,next_sqrt_price:f,fee_amount:h}}function rn(o,t,e,i,n){let r=e,a=i.liquidity,{current_sqrt_price:l}=i,d={amount_in:U.ZERO,amount_out:U.ZERO,fee_amount:U.ZERO,ref_amount:U.ZERO,next_sqrt_price:U.ZERO,cross_tick_num:0},f,h,b=ee.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&&b.gt(P.sqrt_price)||!o&&b.lt(P.sqrt_price)?f=b:f=P.sqrt_price;let k=bo(l,f,a,r,i.fee_rate,t);if(k.amount_in.eq(U.ZERO)||(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 Vt.default(-1)),o?U.MathUtil.is_neg(h)?a=a.add(new Vt.default((0,U.asUintN)(BigInt(h.toString()),128))):a=a.add(h):U.MathUtil.is_neg(h)?a=a.sub(new Vt.default((0,U.asUintN)(BigInt(h.toString()),128))):a=a.sub(h),l=P.sqrt_price):l=k.next_sqrt_price,d.cross_tick_num+=1,r.eq(U.ZERO))break}return d.amount_in=d.amount_in.add(d.fee_amount),d.next_sqrt_price=l,d}var Jt=fe(re()),q=require("@cetusprotocol/common-sdk");function Gr(o,t,e){let i=(0,q.extractStructTagFromType)(o).name,n=(0,q.extractStructTagFromType)(t).name;return`${i}-${n}[${e}]`}function De(o){let t=(0,q.getMoveObjectType)(o),e=(0,q.extractStructTagFromType)(t),i=(0,q.getObjectFields)(o);i==null&&N("InvalidPoolObject",`Pool id ${(0,q.getObjectId)(o)} not exists.`,{[q.DETAILS_KEYS.METHOD_NAME]:"buildPool"});let n=[];i.rewarder_manager.fields.rewarders.forEach(d=>{let{emissions_per_second:f}=d.fields,h=q.MathUtil.fromX64(new Jt.default(f)),b=Math.floor(h.toNumber()*60*60*24);n.push({emissions_per_second:f,coin_type:(0,q.extractStructTagFromType)(d.fields.reward_coin.fields.name).source_address,growth_global:d.fields.growth_global,emissions_every_day:b})});let r=i.is_pause,a={disable_add_liquidity:r,disable_remove_liquidity:r,disable_swap:r,disable_flash_loan:r,disable_collect_fee:r,disable_collect_reward:r},l={id:(0,q.getObjectId)(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:(0,q.asIntN)(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,pool_status:a,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 l.name=Gr(l.coin_type_a,l.coin_type_b,l.tick_spacing),l}function Le(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&N("InvalidPositionObject",`Position not exists. Get Position error:${o.error}`,{[q.DETAILS_KEYS.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=(0,q.getObjectFields)(o);if(i){let a=(0,q.getMoveObjectType)(o),l=(0,q.getObjectOwner)(o);"nft"in i?(i=i.nft.fields,t.description=i.description,t.name=i.name,t.link=i.url):t=(0,q.buildNFT)(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:(0,q.asIntN)(BigInt(i.tick_lower_index.fields.bits)),tick_upper_index:(0,q.asIntN)(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=(0,q.getObjectDeletedResponse)(o);n&&(e.pos_object_id=n.objectId,e.position_status="Deleted");let r=(0,q.getObjectNotExistsResponse)(o);return r&&(e.pos_object_id=r,e.position_status="NotExists"),e}function Oe(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:(0,q.asIntN)(BigInt(e)),tick_upper_index:(0,q.asIntN)(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 $e(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&N("InvalidTickObject",`Tick not exists. Get tick data error:${o.error}`,{[q.DETAILS_KEYS.METHOD_NAME]:"buildTickData"});let e=(0,q.getObjectFields)(o).value.fields.value.fields;return{object_id:(0,q.getObjectId)(o),index:(0,q.asIntN)(BigInt(e.index.fields.bits)),sqrt_price:new Jt.default(e.sqrt_price),liquidity_net:new Jt.default(e.liquidity_net.fields.bits),liquidity_gross:new Jt.default(e.liquidity_gross),fee_growth_outside_a:new Jt.default(e.fee_growth_outside_a),fee_growth_outside_b:new Jt.default(e.fee_growth_outside_b),rewarders_growth_outside:e.rewards_growth_outside}}function sn(o){(!o||!o.index||!o.sqrt_price||!o.liquidity_net||!o.liquidity_gross||!o.fee_growth_outside_a||!o.fee_growth_outside_b)&&N("InvalidTickFields","Invalid tick fields.",{[q.DETAILS_KEYS.METHOD_NAME]:"buildTickDataByEvent"});let t=(0,q.asIntN)(BigInt(o.index.bits)),e=new Jt.default(o.sqrt_price),i=new Jt.default(o.liquidity_net.bits),n=new Jt.default(o.liquidity_gross),r=new Jt.default(o.fee_growth_outside_a),a=new Jt.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 Qr(o,t){return`Cetus LP | Pool${o}-${t}`}function an(o,t,e){let i=[],{timestampMs:n,events:r}=o,a=["AddLiquidityEvent","RemoveLiquidityEvent","CollectFeeEvent","CollectRewardEvent","CollectRewardV2Event","HarvestEvent","AddLiquidityV2Event","RemoveLiquidityV2Event"];return r?.forEach((l,d)=>{let f=(0,q.extractStructTagFromType)(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)&&((0,q.d)(h.parsed_json.amount_a).gt(0)||(0,q.d)(h.parsed_json.amount_b).gt(0))&&i.push(h);break;case"RemoveLiquidityEvent":case"AddLiquidityEvent":case"AddLiquidityV2Event":case"RemoveLiquidityV2Event":((0,q.d)(h.parsed_json.amount_a).gt(0)||(0,q.d)(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))&&(0,q.d)(h.parsed_json.amount).gt(0)&&i.push(h);break;default:break}}}),i}function cn(o,t,e,i){let n=[],{timestampMs:r,events:a}=o;return a?.forEach((l,d)=>{let{name:f,address:h}=(0,q.extractStructTagFromType)(l.type);if(Gi.includes(f)&&e.includes(h)&&i===l.parsedJson.pool){let b={tx:l.id.txDigest,sender:l.sender,type:l.type,block_time:r||"0",index:`${t}_${d}`,parsed_json:l.parsedJson};n.push(b)}}),n}function Ue(o,t,e,i){t.moveCall({target:`${o.sdkOptions.integrate.published_at}::${Ji}::transfer_coin_to_sender`,typeArguments:[i],arguments:[e]})}function ln(o,t,e,i,n){n!=null?t.transferObjects([e],t.pure.address(n)):Ue(o,t,e,i)}var T=require("@cetusprotocol/common-sdk");var $t=require("@cetusprotocol/common-sdk"),yo=1e4,ie=class{static parseClmmVestInfo(t){let e=(0,$t.getObjectFields)(t),i=(0,$t.getObjectType)(t),n=(0,$t.extractStructTagFromType)(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:(0,$t.d)(l.fields.percentage).div(yo).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:(0,$t.fixCoinType)(t.coin_a.name,!1),coin_type_b:(0,$t.fixCoinType)(t.coin_b.name,!1)}}static parsePoolLiquiditySnapshot(t){let e=(0,$t.getObjectFields)(t);return{current_sqrt_price:e.current_sqrt_price,remove_percent:(0,$t.d)(e.remove_percent).div(1e6).toString(),snapshots:{id:e.snapshots.fields.id.id,size:e.snapshots.fields.size}}}static parsePositionSnapshot(t){let e=(0,$t.getObjectFields)(t),i=e.value.fields.value.fields;return{position_id:e.name,liquidity:i.liquidity,tick_lower_index:(0,$t.asIntN)(BigInt(i.tick_lower_index.fields.bits)),tick_upper_index:(0,$t.asIntN)(BigInt(i.tick_upper_index.fields.bits)),fee_owned_a:i.fee_owned_a,fee_owned_b:i.fee_owned_b,value_cut:i.value_cut,rewards:i.rewards}}};var pi=require("@cetusprotocol/common-sdk"),wo={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"}},un={env:"mainnet",full_rpc_url:pi.FullRpcUrlMainnet,graph_rpc_url:pi.GraphRpcUrlMainnet,cetus_config:{package_id:"0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f",published_at:"0xba7e740c3c002673dbe69ad5fbdb0691ec260170e141297cefb982e7081fde52",version:2,config:wo.cetusConfig},clmm_pool:{package_id:"0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb",published_at:"0x25ebb9a7c50eb17b3fa9c5a30fb8b5ad8f97caaf4928943acbcff7153dfee5e3",version:14,config:wo.clmmConfig},integrate:{package_id:"0x996c4d9480708fb8b92aa7acf819fb0497b5ec8e65ba06601cae2fb6db3312c3",published_at:"0xae9c208cf58fd5ba36737c9ee5dcfa7f152d0fb5a5a99eebb7c881ebc2fe59e0",version:16},stats_pools_url:"https://api-sui.cetus.zone/v2/sui/stats_pools",clmm_vest:{package_id:"0x9d2f067d3b9d19ac0f8d2e5c2c393b1760232083e42005b2e5df39c06064d522",published_at:"0x9d2f067d3b9d19ac0f8d2e5c2c393b1760232083e42005b2e5df39c06064d522",version:1,config:{clmm_vest_id:"0xe255c47472470c03bbefb1fc883459c2b978d3ad29aa8ee0c8c1ec9753fa7d01",versioned_id:"0x4f6f2f638362505836114f313809b834dafd58e3910df5110f6e54e4e35c929b",cetus_coin_type:"0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS"}}},dn=["0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb","0xdb5cd62a06c79695bfc9982eb08534706d3752fe123b48e0144f480209b3117f"];var mi=require("@cetusprotocol/common-sdk");var fn={env:"testnet",full_rpc_url:mi.FullRpcUrlTestnet,graph_rpc_url:mi.GraphRpcUrlTestnet,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:"0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8",published_at:"0x6bbdf09f9fa0baa1524080a5b8991042e95061c4e1206217279aec51ba08edf7",config:{pools_id:"0x20a086e6fa0741b3ca77d033a65faf0871349b986ddbdde6fa1d85d78a5f4222",global_config_id:"0xc6273f844b4bc258952c4e477697aa12c918c8e08106fac6b934811298c9820a",global_vault_id:"0x71e74a999dd7959e483f758ddf573e85fa4c24944db33ff6763c9d85a9c045fe",admin_cap_id:"0xbf4c48590f403c38351de0e8aa13d6d91bf78fd8c04e93ac1d0269c44d70ae02",partners_id:"0xb5ae5ed3f403654ae1307aadc0140f746db41efb7bda92235257c84d90a1397e"}},integrate:{package_id:"0xab2d58dd28ff0dc19b18ab2c634397b785a38c342a8f5065ade5f53f9dbffa1c",published_at:"0xab2d58dd28ff0dc19b18ab2c634397b785a38c342a8f5065ade5f53f9dbffa1c",version:1},stats_pools_url:"https://api-sui.devcetus.com/v2/sui/stats_pools",clmm_vest:{package_id:"0xa46d9c66e7b24ab14c5fc5f0d08fa257d833718f0295a6343556ea2f2fdfbd7f",published_at:"0xa46d9c66e7b24ab14c5fc5f0d08fa257d833718f0295a6343556ea2f2fdfbd7f",config:{clmm_vest_id:"0x308b24963e5992f699e32db2f7088b812566a0cae580317fd3b8bf61de7f5508",versioned_id:"0x1cfb684d8ff581416a56caba2aa419bee45fe98a23cbf28e2c6c1021b14cab7c",cetus_coin_type:"0xc6c51938da9a5cf6d6dca692783ea7bdf4478f7b1fef693f58947848f84bcf89::cetus::CETUS"}}},_n=["0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8","0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8"];var Ve=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&&N("InvalidConfig",`when getPositionList get position objects error: ${l.error}, please check the rpc, contracts address config and position id.`,{[T.DETAILS_KEYS.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 b=h.parsedJson;b&&n.push({id:b.pool_id,tick_spacing:b.tick_spacing,coin_type_a:(0,T.extractStructTagFromType)(b.coin_type_a).full_address,coin_type_b:(0,T.extractStructTagFromType)(b.coin_type_b).full_address})})}catch(d){return Ht("FetchError",d,{[T.DETAILS_KEYS.METHOD_NAME]:"getPoolImmutables"})}return r.data=n,a&&this._sdk.updateCache(`${i}_getPoolImmutables`,n,T.CACHE_TIME_24H),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")&&N("InvalidPoolObject",`getPoolWithPages error code: ${a.error?.code??"unknown error"}, please check config and object ids`,{[T.DETAILS_KEYS.METHOD_NAME]:"getPoolsWithPage"});let l=De(a);i.data.push(l);let d=`${l.id}_getPoolObject`;this._sdk.updateCache(d,l,T.CACHE_TIME_24H)}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:Zi}}),n=ie.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=ie.parsePositionSnapshot(f);d.push(h)}),n.position_snapshots=d}}return n}catch(i){return Ht("InvalidPoolObject",i,{[T.DETAILS_KEYS.METHOD_NAME]:"getPoolLiquiditySnapshot",[T.DETAILS_KEYS.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=ie.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")&&N("InvalidPoolObject",`getPools error code: ${n.error?.code??"unknown error"}, please check config and object ids`,{[T.DETAILS_KEYS.METHOD_NAME]:"getAssignPools"});let r=De(n);e.push(r);let a=`${r.id}_getPoolObject`;this._sdk.updateCache(a,r,T.CACHE_TIME_24H)}return e}async getPool(t,e=!0,i=!1){let n=`${t}_getPoolObject`,r=this._sdk.getCache(n,e);if(r!==void 0)return r;let a=await this._sdk.FullClient.getObject({id:t,options:{showType:!0,showContent:!0}});(a.error!=null||a.data?.content?.dataType!=="moveObject")&&N("InvalidPoolObject",`getPool error code: ${a.error?.code??"unknown error"}, please check config and object id`,{[T.DETAILS_KEYS.METHOD_NAME]:"getPool"});let l=De(a);if(i){let d=await this.getPoolStatus(t);d&&(l.pool_status=d)}return this._sdk.updateCache(n,l),l}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||N("StatsPoolsUrlNotSet","statsPoolsUrl is not set in the sdk options.",{[T.DETAILS_KEYS.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){Ht("FetchError",d,{[T.DETAILS_KEYS.METHOD_NAME]:"getPoolByCoins",[T.DETAILS_KEYS.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((0,T.isSortedSymbols)((0,Ce.normalizeSuiAddress)(t.coin_type_a),(0,Ce.normalizeSuiAddress)(t.coin_type_b))){let i=t.coin_type_b;t.coin_type_b=t.coin_type_a,t.coin_type_a=i}return await this.createPoolAndAddLiquidity(t,e)}async createPoolRowPayload(t,e){if((0,T.isSortedSymbols)((0,Ce.normalizeSuiAddress)(t.coin_type_a),(0,Ce.normalizeSuiAddress)(t.coin_type_b))){let i=t.coin_type_b;t.coin_type_b=t.coin_type_a,t.coin_type_a=i}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=(0,T.getObjectPreviousTransactionDigest)(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((0,T.extractStructTagFromType)(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,T.CACHE_TIME_24H)),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=(0,T.createFullClient)(new vo.SuiClient({url:n})):l=r;let d={data:[],has_next_page:!1},f=50,h=e,b=e.limit||10,P=a.env==="testnet"?_n:dn;do{let k=await l.queryTransactionBlocksByPage({ChangedObject:t},{...h,limit:50},i);k.data.forEach((S,I)=>{d.next_cursor=k.next_cursor;let Z=cn(S,I,P,t);d.data=[...d.data,...Z]}),d.has_next_page=k.has_next_page,d.next_cursor=k.next_cursor,h.cursor=k.next_cursor}while(d.data.length<b&&d.has_next_page);return d.data.length>b&&(d.data=d.data.slice(0,b),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=T.TickMath.priceToSqrtPriceX64(a==="coin_a"?(0,T.d)(e):(0,T.d)(1).div(e),l,d),h=0,b=0;if(r.is_full_range)h=T.TickUtil.getMinIndex(n),b=T.TickUtil.getMaxIndex(n);else{let{min_price:$,max_price:H}=r;h=T.TickMath.priceToInitializeTickIndex(a==="coin_a"?(0,T.d)($):(0,T.d)(1).div(H),l,d,n),b=T.TickMath.priceToInitializeTickIndex(a==="coin_a"?(0,T.d)(H):(0,T.d)(1).div($),l,d,n)}let{coin_amount:P,fix_amount_a:k}=t,{coin_amount_limit_a:S,coin_amount_limit_b:I,liquidity_amount:Z,coin_amount_a:V,coin_amount_b:nt}=T.ClmmPoolUtil.estLiquidityAndCoinAmountFromOneAmounts(h,b,new Po.default(P),k,!0,i,f);return{coin_amount_a:V,coin_amount_b:nt,coin_amount_limit_a:S,coin_amount_limit_b:I,liquidity:Z,initialize_sqrt_price:f.toString(),tick_lower:h,tick_upper:b,fix_amount_a:k}}async createPoolAndAddLiquidity(t,e){e=e||new Pe.Transaction,e.setSender(this.sdk.getSenderAddress());let{integrate:i,clmm_pool:n}=this.sdk.sdkOptions,r=(0,T.getPackagerConfigs)(n),a=r.global_config_id,l=r.pools_id,d=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress()),f=T.CoinAssist.buildCoinForAmount(e,d,BigInt(t.amount_a),t.coin_type_a,!1,!0),h=T.CoinAssist.buildCoinForAmount(e,d,BigInt(t.amount_b),t.coin_type_b,!1,!0),b=[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((0,T.asUintN)(BigInt(t.tick_lower)).toString())),e.pure.u32(Number((0,T.asUintN)(BigInt(t.tick_upper)).toString())),f.target_coin,h.target_coin,e.pure.bool(t.fix_amount_a),e.object(T.CLOCK_ADDRESS)];return e.moveCall({target:`${i.published_at}::pool_creator_v3::create_pool_v3`,typeArguments:[t.coin_type_a,t.coin_type_b],arguments:b}),Ue(this._sdk,e,f.target_coin,t.coin_type_a),Ue(this._sdk,e,h.target_coin,t.coin_type_b),e}async createPoolAndAddLiquidityRow(t,e){e=e||new Pe.Transaction;let{clmm_pool:i}=this.sdk.sdkOptions,n=(0,T.getPackagerConfigs)(i),r=n.global_config_id,a=n.pools_id,l=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress()),d=T.CoinAssist.buildCoinForAmount(e,l,BigInt(t.amount_a),t.coin_type_a,!1,!0),f=T.CoinAssist.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((0,T.asUintN)(BigInt(t.tick_lower)).toString())),e.pure.u32(Number((0,T.asUintN)(BigInt(t.tick_upper)).toString())),d.target_coin,f.target_coin,e.pure.bool(t.fix_amount_a),e.object(T.CLOCK_ADDRESS)],b=e.moveCall({target:`${i.published_at}::pool_creator::create_pool_v3`,typeArguments:[t.coin_type_a,t.coin_type_b],arguments:h});return{tx:e,pos_id:b[0],remain_coin_a:b[1],remain_coin_b:b[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:b,coin_amount_a:P,coin_amount_b:k,fix_amount_a:S,coin_amount_limit_a:I,coin_amount_limit_b:Z}=l;return this.createPoolRowPayload({tick_spacing:r,initialize_sqrt_price:d,uri:a||"",coin_type_a:i,coin_type_b:n,amount_a:S?P:I,amount_b:S?Z:k,fix_amount_a:S,tick_lower:f,tick_upper:h},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:b,coin_amount_b:P,fix_amount_a:k,coin_amount_limit_a:S,coin_amount_limit_b:I}=a;return this.createPoolPayload({tick_spacing:n,initialize_sqrt_price:l,uri:r||"",coin_type_a:e,coin_type_b:i,amount_a:k?b:S,amount_b:k?I:P,fix_amount_a:k,tick_lower:d,tick_upper:f})}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((0,T.asUintN)(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 Pe.Transaction,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}::${Yt}::fetch_ticks`,arguments:l,typeArguments:n});let d=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:r,sender:(0,Ce.normalizeSuiAddress)("0x0")});return d.error!=null&&N("InvalidTickObjectId",`getTicks error code: ${d.error??"unknown error"}, please check config and tick object ids`,{[T.DETAILS_KEYS.METHOD_NAME]:"getTicks",[T.DETAILS_KEYS.REQUEST_PARAMS]:t}),d.events?.forEach(f=>{(0,T.extractStructTagFromType)(f.type).name==="FetchTicksResultEvent"&&f.parsedJson.ticks.forEach(h=>{i.push(sn(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 Pe.Transaction,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}::${Yt}::fetch_positions`,arguments:f,typeArguments:a});let h=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:l,sender:(0,Ce.normalizeSuiAddress)("0x0")});h.error!=null&&N("InvalidPositionRewardObject",`fetch position info error code: ${h.error??"unknown error"}, please check config and tick object ids`,{[T.DETAILS_KEYS.METHOD_NAME]:"fetchPoolPositionInfoList",[T.DETAILS_KEYS.REQUEST_PARAMS]:t});let b=[];if(h?.events?.forEach(P=>{(0,T.extractStructTagFromType)(P.type).name==="FetchPositionsEvent"&&P.parsedJson.positions.forEach(k=>{let S=Oe(k);b.push(S)})}),i.push(...b),b.length<r)break;n=[b[b.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=>{(0,T.extractStructTagFromType)(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")&&N("InvalidTickObjectId",`getTicksByRpc error code: ${n.error?.code??"unknown error"}, please check config and tick object ids`,{[T.DETAILS_KEYS.METHOD_NAME]:"getTicksByRpc"});let r=$e(n);r!=null&&e.push(r)}return e}async getTickDataByIndex(t,e){let i={type:"u64",value:(0,T.asUintN)(BigInt((0,T.tickScore)(e).toString())).toString()},n=await this.sdk.FullClient.getDynamicFieldObject({parentId:t,name:i});return(n.error!=null||n.data?.content?.dataType!=="moveObject")&&N("InvalidTickIndex",`get tick by index: ${e} error: ${n.error}`,{[T.DETAILS_KEYS.METHOD_NAME]:"getTickDataByIndex"}),$e(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")&&N("InvalidTickObjectId",`getTicksByRpc error code: ${e.error?.code??"unknown error"}, please check config and tick object ids`,{[T.DETAILS_KEYS.METHOD_NAME]:"getTickDataByObjectId"}),$e(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"&&N("NotFoundPartnerObject",`get partner by object id: ${t} error: ${i[0].error}`,{[T.DETAILS_KEYS.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")&&N("InvalidPartnerRefFeeFields",`get coin by object id: ${f.data.objectId} error: ${f.error}`,{[T.DETAILS_KEYS.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 Pe.Transaction,{clmm_pool:r}=this.sdk.sdkOptions,{global_config_id:a}=(0,T.getPackagerConfigs)(r),l=[i],d=[n.object(a),n.object(t),n.object(e)];return n.moveCall({target:`${r.published_at}::${oi}::claim_ref_fee`,arguments:d,typeArguments:l}),n}async getPoolStatus(t){try{let e=await this._sdk.FullClient.getDynamicFieldObject({parentId:t,name:{type:"0x1::string::String",value:"pool_status"}}),n=(0,T.getObjectFields)(e).position.fields.status;if(n)return n}catch(e){console.log("\u{1F680} ~ file: poolModule.ts:1093 ~ PoolModule ~ getPoolStatus ~ error:",e)}}async claimAllPartnerRefFeesPayload(t,e,i){try{let{clmm_pool:n}=this._sdk.sdkOptions,{global_config_id:r}=(0,T.getPackagerConfigs)(n),a=await this.getPartnerRefFeeAmount(t,!0),l=new Pe.Transaction;if(a.length===0)return{ref_fees:[],tx:l};for(let d of a){let f=d.coin_type;d.balance!==BigInt(0)&&(i&&i.length>0&&!i.includes(f)||l.moveCall({target:`${n.published_at}::${oi}::claim_ref_fee`,arguments:[l.object(r),l.object(e),l.object(t)],typeArguments:[f]}))}return{ref_fees:a,tx:l}}catch(n){return Ht("InvalidPartnerRefFeeFields",n,{[T.DETAILS_KEYS.METHOD_NAME]:"claimAllPartnerRefFeesPayload",[T.DETAILS_KEYS.REQUEST_PARAMS]:{partner:t,partner_cap:e,specific_coin_types:i}})}}};var Co=require("@mysten/sui/client"),ce=require("@mysten/sui/transactions"),gi=require("@mysten/sui/utils"),Ke=fe(re()),A=require("@cetusprotocol/common-sdk");var He=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=(0,A.createFullClient)(new Co.SuiClient({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((b,P)=>{let k=an(b,P,l);f.data=[...f.data,...k]}),f.has_next_page=h.has_next_page,f.next_cursor=h.next_cursor,f}catch(h){Ht("FetchError",h,{[A.DETAILS_KEYS.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((0,A.extractStructTagFromType)(l.data.type).full_address===this.buildPositionType()){let f=Le(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=Le(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=Le(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=(0,A.getObjectFields)(i.data).value.fields.value;return Oe(r)}catch(i){return Ht("FetchError",i,{[A.DETAILS_KEYS.METHOD_NAME]:"getPositionInfo",[A.DETAILS_KEYS.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=(0,A.deriveDynamicFieldIdByType)(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=(0,A.getObjectFields)(r.data).value.fields.value,d=Oe(l);e.push(d)}catch(a){console.log("getPositionInfoList error",a)}}),e)}catch(e){return Ht("FetchError",e,{[A.DETAILS_KEYS.METHOD_NAME]:"getPositionInfoList",[A.DETAILS_KEYS.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((0,A.getPackagerConfigs)(i).global_config_id),e.object(t.pool_id),e.pure.address(t.position_id)];e.moveCall({target:`${n.published_at}::${Yt}::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 ce.Transaction;for(let d of t)this.buildFetchPosFee(d,n);let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:(0,gi.normalizeSuiAddress)("0x0")});r.error!=null&&N("InvalidPoolObject",`fetch position fee error code: ${r.error??"unknown error"}, please check config and position and pool object ids`,{[A.DETAILS_KEYS.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}=ve(t);if(t=t,t.fix_amount_a&&l||!t.fix_amount_a&&d)return i=await Gt.buildAddLiquidityFixTokenForGas(this._sdk,a,t,e,i,n,r),i}return Gt.buildAddLiquidityFixToken(this._sdk,a,t,i,n,r)}async createAddLiquidityPayload(t,e,i,n){let{integrate:r,clmm_pool:a}=this._sdk.sdkOptions,l=(0,A.asUintN)(BigInt(t.tick_lower)).toString(),d=(0,A.asUintN)(BigInt(t.tick_upper)).toString(),f=[t.coin_type_a,t.coin_type_b];e=e||new ce.Transaction;let h=!(0,gi.isValidSuiObjectId)(t.pos_id),b=BigInt(t.max_amount_a),P=BigInt(t.max_amount_b),k,S;if(i==null||n==null){let I=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());k=A.CoinAssist.buildCoinForAmount(e,I,b,t.coin_type_a,!1,!0),S=A.CoinAssist.buildCoinForAmount(e,I,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:[]},S={target_coin:n,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]};if(h)e.moveCall({target:`${r.published_at}::${Xt}::open_position_with_liquidity`,typeArguments:f,arguments:[e.object((0,A.getPackagerConfigs)(a).global_config_id),e.object(t.pool_id),e.pure.u32(Number(l)),e.pure.u32(Number(d)),k.target_coin,S.target_coin,e.pure.u64(t.max_amount_a),e.pure.u64(t.max_amount_b),e.pure.u128(t.delta_liquidity),e.object(A.CLOCK_ADDRESS)]});else{let I=await this._sdk.FullClient.getOwnerCoinAssets(this._sdk.getSenderAddress());e=Gt.createCollectRewarderAndFeeParams(this._sdk,e,t,I,k.remain_coins,S.remain_coins),e.moveCall({target:`${r.published_at}::${Xt}::add_liquidity`,typeArguments:f,arguments:[e.object((0,A.getPackagerConfigs)(a).global_config_id),e.object(t.pool_id),e.object(t.pos_id),k.target_coin,S.target_coin,e.pure.u64(t.max_amount_a),e.pure.u64(t.max_amount_b),e.pure.u128(t.delta_liquidity),e.object(A.CLOCK_ADDRESS)]})}return e}async removeLiquidityPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new ce.Transaction;let r=[t.coin_type_a,t.coin_type_b],a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());e=Gt.createCollectRewarderAndFeeParams(this._sdk,e,t,a);let l=[e.object((0,A.getPackagerConfigs)(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.pure.u128(t.delta_liquidity),e.object(A.CLOCK_ADDRESS)],[d,f]=e.moveCall({target:`${i.published_at}::pool::remove_liquidity`,typeArguments:r,arguments:l}),h=A.CoinAssist.fromBalance(d,t.coin_type_a,e),b=A.CoinAssist.fromBalance(f,t.coin_type_b,e);return e.moveCall({target:`${n.published_at}::${he}::check_coin_threshold`,typeArguments:[t.coin_type_a],arguments:[h,e.pure.u64(t.min_amount_a)]}),e.moveCall({target:`${n.published_at}::${he}::check_coin_threshold`,typeArguments:[t.coin_type_b],arguments:[b,e.pure.u64(t.min_amount_b)]}),t.is_return_coins?{coin_a:h,coin_b:b}:(e.transferObjects([h,b],this.sdk.getSenderAddress()),e)}async closePositionPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new ce.Transaction;let r=[t.coin_type_a,t.coin_type_b],a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());return e=Gt.createCollectRewarderAndFeeParams(this._sdk,e,t,a),e.moveCall({target:`${n.published_at}::${Be}::close_position`,typeArguments:r,arguments:[e.object((0,A.getPackagerConfigs)(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(A.CLOCK_ADDRESS)]}),e}openPositionPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new ce.Transaction;let r=[t.coin_type_a,t.coin_type_b],a=(0,A.asUintN)(BigInt(t.tick_lower)).toString(),l=(0,A.asUintN)(BigInt(t.tick_upper)).toString(),d=[e.object((0,A.getPackagerConfigs)(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}::${Be}::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 ce.Transaction;let a=await this.sdk.Pool.getPool(i,!1),l=Number(a.tick_spacing),d=0,f=0;if(t.is_full_range)d=A.TickUtil.getMinIndex(l),f=A.TickUtil.getMaxIndex(l);else{let{price_base_coin:P,min_price:k,max_price:S}=t;d=A.TickMath.priceToInitializeTickIndex(P==="coin_a"?(0,A.d)(k):(0,A.d)(1).div(S),t.coin_decimals_a,t.coin_decimals_b,l),f=A.TickMath.priceToInitializeTickIndex(P==="coin_a"?(0,A.d)(S):(0,A.d)(1).div(k),t.coin_decimals_a,t.coin_decimals_b,l)}let h=[a.coin_type_a,a.coin_type_b],b=[e.object((0,A.getPackagerConfigs)(n).global_config_id),e.object(t.pool_id),e.pure.u32(Number((0,A.asUintN)(BigInt(d)))),e.pure.u32(Number((0,A.asUintN)(BigInt(f))))];return e.moveCall({target:`${r.published_at}::${Be}::open_position`,typeArguments:h,arguments:b}),e}async collectFeePayload(t,e,i,n){e=e||new ce.Transaction;let r=i||A.CoinAssist.buildCoinWithBalance(BigInt(0),t.coin_type_a,e),a=n||A.CoinAssist.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((0,A.getPackagerConfigs)(r).global_config_id),e.object(t.pool_id),e.object(t.pos_id),i,n];return e.moveCall({target:`${a.published_at}::${Xt}::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=A.TickUtil.getMinIndex(l),f=A.TickUtil.getMaxIndex(l);else{let{price_base_coin:V,min_price:nt,max_price:$}=r;d=A.TickMath.priceToInitializeTickIndex(V==="coin_a"?(0,A.d)(nt):(0,A.d)(1).div($),r.coin_decimals_a,r.coin_decimals_b,l),f=A.TickMath.priceToInitializeTickIndex(V==="coin_a"?(0,A.d)($):(0,A.d)(1).div(nt),r.coin_decimals_a,r.coin_decimals_b,l)}if("liquidity"in t){let{liquidity:V}=t,nt=A.TickMath.tickIndexToSqrtPriceX64(d),$=A.TickMath.tickIndexToSqrtPriceX64(f),{coin_amount_a:H,coin_amount_b:zt}=A.ClmmPoolUtil.getCoinAmountFromLiquidity(new Ke.default(V),new Ke.default(a.current_sqrt_price),nt,$,!1),tt=(0,A.d)(H).mul(1+i).toFixed(0,E.ROUND_UP),Qt=(0,A.d)(zt).mul(1+i).toFixed(0,E.ROUND_UP);return{coin_amount_a:H,coin_amount_b:zt,coin_amount_limit_a:tt,coin_amount_limit_b:Qt,liquidity:V,tick_lower:d,tick_upper:f}}let{coin_amount:h,fix_amount_a:b}=t,{coin_amount_limit_a:P,coin_amount_limit_b:k,liquidity_amount:S,coin_amount_a:I,coin_amount_b:Z}=A.ClmmPoolUtil.estLiquidityAndCoinAmountFromOneAmounts(d,f,new Ke.default(h),b,!0,i,new Ke.default(a.current_sqrt_price));return{coin_amount_a:I,coin_amount_b:Z,coin_amount_limit_a:P,coin_amount_limit_b:k,liquidity:S,tick_lower:d,tick_upper:f,fix_amount_a:b}}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:b}=a;e=e||new ce.Transaction;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:b,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:b,fix_amount_a:P,coin_amount_a:k,coin_amount_b:S}=a;if(P===void 0)throw N("InvalidParams","fix_amount_a is undefined",{[A.DETAILS_KEYS.METHOD_NAME]:"addLiquidityFixCoinCoinWithPricePayload",[A.DETAILS_KEYS.REQUEST_PARAMS]:t});e=e||new ce.Transaction;let I=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityFixTokenPayload({amount_a:P?k:l,amount_b:P?d:S,slippage:0,fix_amount_a:P,is_open:!0,tick_lower:h,tick_upper:b,collect_fee:!1,rewarder_coin_types:[],coin_type_a:I.coin_type_a,coin_type_b:I.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((0,A.getPackagerConfigs)(r).global_config_id),e.object(t.pool_id),e.object(t.pos_id),i,n];return e.moveCall({target:`${a.published_at}::${je}::collect_fee`,typeArguments:l,arguments:d}),e}createCollectFeeAndReturnCoinsPayload(t,e){let{clmm_pool:i}=this.sdk.sdkOptions,n=[t.coin_type_a,t.coin_type_b],r=[e.object((0,A.getPackagerConfigs)(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.pure.bool(!0)],[a,l]=e.moveCall({target:`${i.published_at}::pool::collect_fee`,typeArguments:n,arguments:r});return{fee_a:a,fee_b:l}}};var bi=require("@mysten/sui/transactions"),ko=require("@mysten/sui/utils"),hn=fe(re()),G=require("@cetusprotocol/common-sdk");var We=class{constructor(t){this._sdk=t,this.growthGlobal=[G.ZERO,G.ZERO,G.ZERO]}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=G.MathUtil.fromX64(new hn.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((0,G.getPackagerConfigs)(i).global_config_id),e.object(t.pool_id),e.pure.address(t.position_id),e.object(G.CLOCK_ADDRESS)];e.moveCall({target:`${n.published_at}::${Yt}::fetch_position_rewards`,arguments:a,typeArguments:r})}async fetchPosRewardersAmount(t){let e=new bi.Transaction;for(let a of t)this.buildFetchPosReward(a,e);let i=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:e,sender:(0,ko.normalizeSuiAddress)("0x")});i.error!=null&&N("InvalidConfig",`fetch position rewards error code: ${i.error??"unknown error"}, please check config and params`,{[G.DETAILS_KEYS.METHOD_NAME]:"fetchPosRewardersAmount",[G.DETAILS_KEYS.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=[G.ZERO,G.ZERO,G.ZERO];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 hn.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 bi.Transaction;return i=Gt.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 bi.Transaction;let a={};return t.forEach(l=>{let d=(0,G.normalizeCoinType)(l.coin_type_a),f=(0,G.normalizeCoinType)(l.coin_type_b);if(l.collect_fee){let b=a[d];b==null&&(i==null?b=G.CoinAssist.buildCoinForAmount(e,r,BigInt(0),d,!1):b={target_coin:i,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},a[d]=b);let P=a[f];P==null&&(n==null?P=G.CoinAssist.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,b.target_coin,P.target_coin)}let h=[];l.rewarder_coin_types.forEach(b=>{let P=(0,G.normalizeCoinType)(b),k=a[b];k===void 0&&(k=G.CoinAssist.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&&ln(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=(0,G.getPackagerConfigs)(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}::${Xt}::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(G.CLOCK_ADDRESS)]})}),e}createCollectRewarderNoSendPayload(t,e,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=(0,G.getPackagerConfigs)(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}::${je}::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(G.CLOCK_ADDRESS)]})}),e}createCollectRewarderAndReturnCoinPayload(t,e){let{clmm_pool:i}=this.sdk.sdkOptions,n=(0,G.getPackagerConfigs)(i),r=[t.coin_type_a,t.coin_type_b],a=[e.object((0,G.getPackagerConfigs)(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.object(n.global_vault_id),e.pure.bool(!0),e.object(G.CLOCK_ADDRESS)];return e.moveCall({target:`${i.published_at}::pool::collect_reward`,typeArguments:[...r,t.rewarder_coin_type],arguments:a})}};var pn=require("@mysten/sui/transactions"),mn=require("@mysten/sui/utils"),ke=fe(re()),it=require("@cetusprotocol/common-sdk");var Jr="amm_swap",Xr="Pool",Ze=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=(0,it.d)(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 E(r.fee_rate).div(10**6):new E(r.fee_rate).div(10**9),l=(0,it.d)(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 E(1).div(r.current_price),h=d.direction?d.current_price:new E(1).div(d.current_price),b=d.label==="Cetus"?new E(d.fee_rate).div(10**6):new E(d.fee_rate).div(10**9),k=(0,it.d)(d.output_amount).div(10**d.to_decimal).mul(b).div(f.mul(h));e=e.add(k)}}}),e.toString()}calculateSwapPriceImpact(t){let e=(0,it.d)(0);return t.forEach(i=>{let n=i.base_paths.length;if(n===1){let r=i.base_paths[0],a=(0,it.d)(r.output_amount).div(10**r.to_decimal),l=(0,it.d)(r.input_amount).div(10**r.from_decimal),d=a.div(l),f=r.direction?new E(r.current_price):new E(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 E(r.current_price):new E(1).div(r.current_price),d=a.direction?new E(a.current_price):new E(1).div(a.current_price),f=l.mul(d),h=new E(a.output_amount).div(10**a.to_decimal),b=new E(r.input_amount).div(10**r.from_decimal),P=h.div(b);e=e.add(this.calculateSingleImpact(P,f))}}),e.toString()}async preSwapWithMultiPool(t){let{integrate:e}=this.sdk.sdkOptions,i=new pn.Transaction,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}::${Yt}::calculate_swap_result`,arguments:h,typeArguments:n})}let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:(0,mn.normalizeSuiAddress)("0x0")});r.error!=null&&N("InvalidConfig",`pre swap with multi pools error code: ${r.error??"unknown error"}, please check config and params`,{[it.DETAILS_KEYS.METHOD_NAME]:"preSwapWithMultiPool",[it.DETAILS_KEYS.REQUEST_PARAMS]:{params:t}});let a=r.events?.filter(f=>(0,it.extractStructTagFromType)(f.type).name==="CalculatedSwapResultEvent");if(a.length===0)return null;a.length!==t.pool_ids.length&&N("ParamsLengthNotEqual","valueData.length !== params.pools.length",{[it.DETAILS_KEYS.METHOD_NAME]:"preSwapWithMultiPool",[it.DETAILS_KEYS.REQUEST_PARAMS]:{params:t}});let l=t.by_amount_in?it.ZERO:it.U64_MAX,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 ke.default(a[f].parsedJson.data.amount_out);h.gt(l)&&(d=f,l=h)}else{let h=new ke.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 pn.Transaction,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}::${Yt}::calculate_swap_result`,arguments:r,typeArguments:n});let a=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:(0,mn.normalizeSuiAddress)("0x0")});if(a.error!=null)return N("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[it.DETAILS_KEYS.METHOD_NAME]:"preSwap",[it.DETAILS_KEYS.REQUEST_PARAMS]:{params:t}});let l=a.events?.filter(d=>(0,it.extractStructTagFromType)(d.type).name==="CalculatedSwapResultEvent");return l.length===0?N("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[it.DETAILS_KEYS.METHOD_NAME]:"preSwap",[it.DETAILS_KEYS.REQUEST_PARAMS]:{params:t}}):this.transformSwapData(t,l[0].parsedJson.data)}transformSwapData(t,e){let i=e.amount_in&&e.fee_amount?new ke.default(e.amount_in).add(new ke.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 ke.default(i.amount_in).add(new ke.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=on(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=rn(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=ee.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=it.TickMath.sqrtPriceX64ToPrice(i.current_sqrt_price,t.decimals_a,t.decimals_b).toNumber(),h=it.TickMath.sqrtPriceX64ToPrice(r.next_sqrt_price,t.decimals_a,t.decimals_b).toNumber(),b=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:b}}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}=ve(t);if(t.a2b&&n||!t.a2b&&r)return await ee.buildSwapTransactionForGas(this._sdk,t,i,e)}return ee.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}=ve(t);if(t.a2b&&n||!t.a2b&&r)return await ee.buildSwapTransactionWithoutTransferCoinsForGas(this._sdk,t,i,e)}return ee.buildSwapTransactionWithoutTransferCoins(this.sdk,t,i)}};var Kt=require("@cetusprotocol/common-sdk");var gn=require("@mysten/sui/transactions"),Mo=require("@mysten/sui/utils"),ze=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 N("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[Kt.DETAILS_KEYS.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=ie.parseClmmVestInfo(l),f=`${d.id}-ClmmVestInfo`;this._sdk.updateCache(f,d),e.push(d)}),e}catch(i){Ht("ClmmVestFetchError",i,{[Kt.DETAILS_KEYS.METHOD_NAME]:"getClmmVestInfoList"})}return[]}async getClmmVestInfo(t=!0){let{clmm_vest:e}=this._sdk.sdkOptions;if(e===void 0)return N("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[Kt.DETAILS_KEYS.METHOD_NAME]:"getClmmVestInfo"});let{clmm_vest_id:i}=(0,Kt.getPackagerConfigs)(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=ie.parseClmmVestInfo(a);return this._sdk.updateCache(n,l),l}catch(a){return Ht("ClmmVestFetchError",a,{[Kt.DETAILS_KEYS.METHOD_NAME]:"getClmmVestInfo",[Kt.DETAILS_KEYS.REQUEST_PARAMS]:{clmm_vest_id:i}})}}async getPositionVesting(t){let{clmm_vest:e}=this._sdk.sdkOptions;if(e===void 0)return N("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[Kt.DETAILS_KEYS.METHOD_NAME]:"getPositionVesting"});if(t.length===0)return[];let{clmm_vest_id:i}=(0,Kt.getPackagerConfigs)(e),n=new gn.Transaction;t.forEach(l=>{let{clmm_pool_id:d,coin_type_a:f,coin_type_b:h,clmm_position_ids:b}=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",b)]})});let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:(0,Mo.normalizeSuiAddress)("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=>ie.parsePositionVesting(f)))}}),a}buildRedeemPayload(t,e){let{clmm_vest:i}=this._sdk.sdkOptions;if(i===void 0)return N("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[Kt.DETAILS_KEYS.METHOD_NAME]:"buildRedeemVestPayload"});let{versioned_id:n,clmm_vest_id:r,cetus_coin_type:a}=(0,Kt.getPackagerConfigs)(i);return e=e||new gn.Transaction,t.forEach(l=>{let{clmm_pool_id:d,clmm_position_id:f,coin_type_a:h,coin_type_b:b,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),typeof f=="string"?e.object(f):f,e.pure.u16(P),e.object(Kt.CLOCK_ADDRESS)],typeArguments:[h,b]}),S=Kt.CoinAssist.fromBalance(k,a,e);e.transferObjects([S],this._sdk.getSenderAddress())}),e}};var Ge=class o extends To.SdkWrapper{constructor(t){super(t),this._swap=new Ze(this),this._pool=new Ve(this),this._position=new He(this),this._config=new Fe(this),this._rewarder=new We(this),this._vest=new ze(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({...un,...t}):o.createCustomSDK({...fn,...t})}static createCustomSDK(t){return new o(t)}};var Qe=fe(re()),le=require("@cetusprotocol/common-sdk");var Yr=new Qe.default(365),ts=new Qe.default(24),es=new Qe.default(3600),is=new Qe.default(.5);function ns(o,t,e,i){return Yr.mul(ts).mul(es).mul(is).mul(o.mul(t).add(e).div(i))}function os(o,t,e,i,n,r){let a=new E(o.toString()).div(new E(10**e)),l=new E(t.toString()).div(new E(10**i));return a.mul(n).add(l.mul(r))}function rs(o,t,e,i,n,r,a,l,d,f,h,b,P,k,S,I,Z,V,nt,$,H,zt,tt,Qt){let ue=new E(b),ne=new E(P),de=new E(I),W=new E(Z),Wt=new E(V),w=new E(nt),s=new E($),u=new E(H),_=new E(zt),p=new E(tt),g=new E(Qt),v=le.TickMath.tickIndexToSqrtPriceX64(t),C=le.TickMath.tickIndexToSqrtPriceX64(e),m=le.MathUtil.toX64Decimal(le.MathUtil.fromX64(v)).round(),c=le.MathUtil.toX64Decimal(le.MathUtil.fromX64(C)).round(),y=le.MathUtil.toX64Decimal(le.MathUtil.fromX64(i)).round(),x,O=ue.mul(new E(10**r)).mul(c.mul(m)).div(c.sub(m)).round(),R=ne.mul(new E(10**a)).div(c.sub(m)).round();o<t?x=O:o>e?x=R:x=E.min(O,R);let L=x.mul(y.sub(m)),Q=x.mul(c.sub(y)).div(y.mul(c)).div(new E(10**r)).mul(s).add(L.div(new E(10**a).mul(u))),oe=os(k,S,r,a,s,u),J=Q.div(oe),et=x.eq(new E(0))?new E(0):new E(h/1e4).mul(de).mul(new E(x.toString()).div(new E(n.toString()).add(new E(x.toString())))).div(Q),Me=J.eq(new E(0))?new E(0):J.mul(new E(36500/7)).div(Q),ot=W.div(new E(10**l)).mul(_).mul(Me),rt=Wt.div(new E(10**d)).mul(p).mul(Me),Je=w.div(new E(10**f)).mul(g).mul(Me);return{fee_apr:et,pos_rewarder_0_apr:ot,pos_rewarder_1_apr:rt,pos_rewarder_2_apr:Je}}function ss(o,t,e,i){let n=Math.max(o,e),a=Math.min(t,i)-n,l=t-o,d=i-e,f=new E(l.toString()),h=new E(d.toString()),b=new E(a.toString()),P=new E("0");return a<0?P=new E("0"):l===a?P=h.div(b):d===a?P=b.div(f):P=b.mul(b).div(h).div(f),P}var as=Ge;0&&(module.exports={AMM_SWAP_MODULE,BPS,CetusClmmSDK,ClmmError,ClmmExpectSwapModule,ClmmFetcherModule,ClmmIntegratePoolModule,ClmmIntegratePoolV2Module,ClmmIntegratePoolV3Module,ClmmIntegrateRouterModule,ClmmIntegrateRouterWithPartnerModule,ClmmIntegrateUtilsModule,ClmmPartnerModule,ClmmPositionStatus,ConfigErrorCode,ConfigModule,DeepbookClobV2Module,DeepbookCustodianV2Module,DeepbookEndpointsV2Module,POOL_STRUCT,PartnerErrorCode,PoolErrorCode,PoolModule,PositionErrorCode,PositionModule,PositionUtils,RewarderModule,RouterErrorCode,SwapErrorCode,SwapModule,SwapUtils,TypesErrorCode,UtilsErrorCode,VestErrorCode,VestModule,VestUtils,buildClmmPositionName,buildPool,buildPoolTransactionInfo,buildPosition,buildPositionInfo,buildPositionTransactionInfo,buildTickData,buildTickDataByEvent,buildTransferCoin,buildTransferCoinToSender,clmmMainnet,clmmTestnet,computeSwap,computeSwapStep,estPoolAPR,estPositionAPRWithDeltaMethod,estPositionAPRWithMultiMethod,eventMainnetContractMaps,eventTestnetContractMaps,findAdjustCoin,getDefaultSuiInputType,getLowerSqrtPriceFromCoinA,getLowerSqrtPriceFromCoinB,getUpperSqrtPriceFromCoinA,getUpperSqrtPriceFromCoinB,handleError,handleMessageError,newBits,poolFilterEvenTypes,poolLiquiditySnapshotType,transClmmpoolDataWithoutTicks});
1
+ var vr=Object.create;var qn=Object.defineProperty;var Pr=Object.getOwnPropertyDescriptor;var Cr=Object.getOwnPropertyNames;var Mr=Object.getPrototypeOf,kr=Object.prototype.hasOwnProperty;var Tr=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var xr=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports);var Sr=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Cr(e))!kr.call(n,o)&&o!==t&&qn(n,o,{get:()=>e[o],enumerable:!(i=Pr(e,o))||i.enumerable});return n};var ce=(n,e,t)=>(t=n!=null?vr(Mr(n)):{},Sr(e||!n||!n.__esModule?qn(t,"default",{value:n,enumerable:!0}):t,n));var oe=xr((so,on)=>{"use strict";(function(n,e){"use strict";function t(w,s){if(!w)throw new Error(s||"Assertion failed")}function i(w,s){w.super_=s;var u=function(){};u.prototype=s.prototype,w.prototype=new u,w.prototype.constructor=w}function o(w,s,u){if(o.isBN(w))return w;this.negative=0,this.words=null,this.length=0,this.red=null,w!==null&&((s==="le"||s==="be")&&(u=s,s=10),this._init(w||0,s||10,u||"be"))}typeof n=="object"?n.exports=o:e.BN=o,o.BN=o,o.wordSize=26;var r;try{typeof window<"u"&&typeof window.Buffer<"u"?r=window.Buffer:r=Tr("buffer").Buffer}catch{}o.isBN=function(s){return s instanceof o?!0:s!==null&&typeof s=="object"&&s.constructor.wordSize===o.wordSize&&Array.isArray(s.words)},o.max=function(s,u){return s.cmp(u)>0?s:u},o.min=function(s,u){return s.cmp(u)<0?s:u},o.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),t(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,_)))},o.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):(t(s<9007199254740992),this.words=[s&67108863,s/67108864&67108863,1],this.length=3),_==="le"&&this._initArray(this.toArray(),u,_)},o.prototype._initArray=function(s,u,_){if(t(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(w,s){var u=w.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;t(!1,"Invalid character in "+w)}function l(w,s,u){var _=a(w,u);return u-1>=s&&(_|=a(w,u-1)<<4),_}o.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 f(w,s,u,_){for(var p=0,g=0,v=Math.min(w.length,u),C=s;C<v;C++){var m=w.charCodeAt(C)-48;p*=_,m>=49?g=m-49+10:m>=17?g=m-17+10:g=m,t(m>=0&&g<_,"Invalid character"),p+=g}return p}o.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=f(s,b,b+p,u),this.imuln(g),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c);if(C!==0){var T=1;for(c=f(s,b,s.length,u),b=0;b<C;b++)T*=u;this.imuln(T),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c)}this._strip()},o.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 d(w,s){w.words=s.words,w.length=s.length,w.negative=s.negative,w.red=s.red}if(o.prototype._move=function(s){d(s,this)},o.prototype.clone=function(){var s=new o(null);return this.copy(s),s},o.prototype._expand=function(s){for(;this.length<s;)this.words[this.length++]=0;return this},o.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},o.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{o.prototype[Symbol.for("nodejs.util.inspect.custom")]=h}catch{o.prototype.inspect=h}else o.prototype.inspect=h;function h(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var y=["","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];o.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?_=y[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=M[s];_="";var T=this.clone();for(T.negative=0;!T.isZero();){var I=T.modrn(b).toString(s);T=T.idivn(b),T.isZero()?_=I+_:_=y[c-I.length]+I+_}for(this.isZero()&&(_="0"+_);_.length%u!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}t(!1,"Base should be between 2 and 36")},o.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&&t(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-s:s},o.prototype.toJSON=function(){return this.toString(16,2)},r&&(o.prototype.toBuffer=function(s,u){return this.toArrayLike(r,s,u)}),o.prototype.toArray=function(s,u){return this.toArrayLike(Array,s,u)};var S=function(s,u){return s.allocUnsafe?s.allocUnsafe(u):new s(u)};o.prototype.toArrayLike=function(s,u,_){this._strip();var p=this.byteLength(),g=_||Math.max(1,p);t(p<=g,"byte array longer than desired length"),t(g>0,"Requested array length <= 0");var v=S(s,g),C=u==="le"?"LE":"BE";return this["_toArrayLike"+C](v,p),v},o.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},o.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?o.prototype._countBits=function(s){return 32-Math.clz32(s)}:o.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},o.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&&_++,_},o.prototype.bitLength=function(){var s=this.words[this.length-1],u=this._countBits(s);return(this.length-1)*26+u};function x(w){for(var s=new Array(w.bitLength()),u=0;u<s.length;u++){var _=u/26|0,p=u%26;s[u]=w.words[_]>>>p&1}return s}o.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},o.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},o.prototype.toTwos=function(s){return this.negative!==0?this.abs().inotn(s).iaddn(1):this.clone()},o.prototype.fromTwos=function(s){return this.testn(s-1)?this.notn(s).iaddn(1).ineg():this.clone()},o.prototype.isNeg=function(){return this.negative!==0},o.prototype.neg=function(){return this.clone().ineg()},o.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},o.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()},o.prototype.ior=function(s){return t((this.negative|s.negative)===0),this.iuor(s)},o.prototype.or=function(s){return this.length>s.length?this.clone().ior(s):s.clone().ior(this)},o.prototype.uor=function(s){return this.length>s.length?this.clone().iuor(s):s.clone().iuor(this)},o.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()},o.prototype.iand=function(s){return t((this.negative|s.negative)===0),this.iuand(s)},o.prototype.and=function(s){return this.length>s.length?this.clone().iand(s):s.clone().iand(this)},o.prototype.uand=function(s){return this.length>s.length?this.clone().iuand(s):s.clone().iuand(this)},o.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()},o.prototype.ixor=function(s){return t((this.negative|s.negative)===0),this.iuxor(s)},o.prototype.xor=function(s){return this.length>s.length?this.clone().ixor(s):s.clone().ixor(this)},o.prototype.uxor=function(s){return this.length>s.length?this.clone().iuxor(s):s.clone().iuxor(this)},o.prototype.inotn=function(s){t(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()},o.prototype.notn=function(s){return this.clone().inotn(s)},o.prototype.setn=function(s,u){t(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()},o.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},o.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)},o.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()},o.prototype.sub=function(s){return this.clone().isub(s)};function U(w,s,u){u.negative=s.negative^w.negative;var _=w.length+s.length|0;u.length=_,_=_-1|0;var p=w.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,T=m&67108863,I=Math.min(c,s.length-1),R=Math.max(0,c-w.length+1);R<=I;R++){var O=c-R|0;p=w.words[O]|0,g=s.words[R]|0,v=p*g+T,b+=v/67108864|0,T=v&67108863}u.words[c]=T|0,m=b|0}return m!==0?u.words[c]=m|0:u.length--,u._strip()}var D=function(s,u,_){var p=s.words,g=u.words,v=_.words,C=0,m,c,b,T=p[0]|0,I=T&8191,R=T>>>13,O=p[1]|0,V=O&8191,K=O>>>13,Ht=p[2]|0,W=Ht&8191,G=Ht>>>13,Ce=p[3]|0,X=Ce&8191,J=Ce>>>13,Ye=p[4]|0,Y=Ye&8191,tt=Ye>>>13,Cn=p[5]|0,et=Cn&8191,it=Cn>>>13,Mn=p[6]|0,nt=Mn&8191,ot=Mn>>>13,kn=p[7]|0,rt=kn&8191,st=kn>>>13,Tn=p[8]|0,at=Tn&8191,ct=Tn>>>13,xn=p[9]|0,lt=xn&8191,ut=xn>>>13,Sn=g[0]|0,dt=Sn&8191,ft=Sn>>>13,An=g[1]|0,_t=An&8191,pt=An>>>13,In=g[2]|0,ht=In&8191,mt=In>>>13,Rn=g[3]|0,gt=Rn&8191,bt=Rn>>>13,En=g[4]|0,yt=En&8191,wt=En>>>13,jn=g[5]|0,vt=jn&8191,Pt=jn>>>13,Fn=g[6]|0,Ct=Fn&8191,Mt=Fn>>>13,Nn=g[7]|0,kt=Nn&8191,Tt=Nn>>>13,Bn=g[8]|0,xt=Bn&8191,St=Bn>>>13,On=g[9]|0,At=On&8191,It=On>>>13;_.negative=s.negative^u.negative,_.length=19,m=Math.imul(I,dt),c=Math.imul(I,ft),c=c+Math.imul(R,dt)|0,b=Math.imul(R,ft);var ji=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(ji>>>26)|0,ji&=67108863,m=Math.imul(V,dt),c=Math.imul(V,ft),c=c+Math.imul(K,dt)|0,b=Math.imul(K,ft),m=m+Math.imul(I,_t)|0,c=c+Math.imul(I,pt)|0,c=c+Math.imul(R,_t)|0,b=b+Math.imul(R,pt)|0;var Fi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Fi>>>26)|0,Fi&=67108863,m=Math.imul(W,dt),c=Math.imul(W,ft),c=c+Math.imul(G,dt)|0,b=Math.imul(G,ft),m=m+Math.imul(V,_t)|0,c=c+Math.imul(V,pt)|0,c=c+Math.imul(K,_t)|0,b=b+Math.imul(K,pt)|0,m=m+Math.imul(I,ht)|0,c=c+Math.imul(I,mt)|0,c=c+Math.imul(R,ht)|0,b=b+Math.imul(R,mt)|0;var Ni=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ni>>>26)|0,Ni&=67108863,m=Math.imul(X,dt),c=Math.imul(X,ft),c=c+Math.imul(J,dt)|0,b=Math.imul(J,ft),m=m+Math.imul(W,_t)|0,c=c+Math.imul(W,pt)|0,c=c+Math.imul(G,_t)|0,b=b+Math.imul(G,pt)|0,m=m+Math.imul(V,ht)|0,c=c+Math.imul(V,mt)|0,c=c+Math.imul(K,ht)|0,b=b+Math.imul(K,mt)|0,m=m+Math.imul(I,gt)|0,c=c+Math.imul(I,bt)|0,c=c+Math.imul(R,gt)|0,b=b+Math.imul(R,bt)|0;var Bi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Bi>>>26)|0,Bi&=67108863,m=Math.imul(Y,dt),c=Math.imul(Y,ft),c=c+Math.imul(tt,dt)|0,b=Math.imul(tt,ft),m=m+Math.imul(X,_t)|0,c=c+Math.imul(X,pt)|0,c=c+Math.imul(J,_t)|0,b=b+Math.imul(J,pt)|0,m=m+Math.imul(W,ht)|0,c=c+Math.imul(W,mt)|0,c=c+Math.imul(G,ht)|0,b=b+Math.imul(G,mt)|0,m=m+Math.imul(V,gt)|0,c=c+Math.imul(V,bt)|0,c=c+Math.imul(K,gt)|0,b=b+Math.imul(K,bt)|0,m=m+Math.imul(I,yt)|0,c=c+Math.imul(I,wt)|0,c=c+Math.imul(R,yt)|0,b=b+Math.imul(R,wt)|0;var Oi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Oi>>>26)|0,Oi&=67108863,m=Math.imul(et,dt),c=Math.imul(et,ft),c=c+Math.imul(it,dt)|0,b=Math.imul(it,ft),m=m+Math.imul(Y,_t)|0,c=c+Math.imul(Y,pt)|0,c=c+Math.imul(tt,_t)|0,b=b+Math.imul(tt,pt)|0,m=m+Math.imul(X,ht)|0,c=c+Math.imul(X,mt)|0,c=c+Math.imul(J,ht)|0,b=b+Math.imul(J,mt)|0,m=m+Math.imul(W,gt)|0,c=c+Math.imul(W,bt)|0,c=c+Math.imul(G,gt)|0,b=b+Math.imul(G,bt)|0,m=m+Math.imul(V,yt)|0,c=c+Math.imul(V,wt)|0,c=c+Math.imul(K,yt)|0,b=b+Math.imul(K,wt)|0,m=m+Math.imul(I,vt)|0,c=c+Math.imul(I,Pt)|0,c=c+Math.imul(R,vt)|0,b=b+Math.imul(R,Pt)|0;var qi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(qi>>>26)|0,qi&=67108863,m=Math.imul(nt,dt),c=Math.imul(nt,ft),c=c+Math.imul(ot,dt)|0,b=Math.imul(ot,ft),m=m+Math.imul(et,_t)|0,c=c+Math.imul(et,pt)|0,c=c+Math.imul(it,_t)|0,b=b+Math.imul(it,pt)|0,m=m+Math.imul(Y,ht)|0,c=c+Math.imul(Y,mt)|0,c=c+Math.imul(tt,ht)|0,b=b+Math.imul(tt,mt)|0,m=m+Math.imul(X,gt)|0,c=c+Math.imul(X,bt)|0,c=c+Math.imul(J,gt)|0,b=b+Math.imul(J,bt)|0,m=m+Math.imul(W,yt)|0,c=c+Math.imul(W,wt)|0,c=c+Math.imul(G,yt)|0,b=b+Math.imul(G,wt)|0,m=m+Math.imul(V,vt)|0,c=c+Math.imul(V,Pt)|0,c=c+Math.imul(K,vt)|0,b=b+Math.imul(K,Pt)|0,m=m+Math.imul(I,Ct)|0,c=c+Math.imul(I,Mt)|0,c=c+Math.imul(R,Ct)|0,b=b+Math.imul(R,Mt)|0;var Di=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Di>>>26)|0,Di&=67108863,m=Math.imul(rt,dt),c=Math.imul(rt,ft),c=c+Math.imul(st,dt)|0,b=Math.imul(st,ft),m=m+Math.imul(nt,_t)|0,c=c+Math.imul(nt,pt)|0,c=c+Math.imul(ot,_t)|0,b=b+Math.imul(ot,pt)|0,m=m+Math.imul(et,ht)|0,c=c+Math.imul(et,mt)|0,c=c+Math.imul(it,ht)|0,b=b+Math.imul(it,mt)|0,m=m+Math.imul(Y,gt)|0,c=c+Math.imul(Y,bt)|0,c=c+Math.imul(tt,gt)|0,b=b+Math.imul(tt,bt)|0,m=m+Math.imul(X,yt)|0,c=c+Math.imul(X,wt)|0,c=c+Math.imul(J,yt)|0,b=b+Math.imul(J,wt)|0,m=m+Math.imul(W,vt)|0,c=c+Math.imul(W,Pt)|0,c=c+Math.imul(G,vt)|0,b=b+Math.imul(G,Pt)|0,m=m+Math.imul(V,Ct)|0,c=c+Math.imul(V,Mt)|0,c=c+Math.imul(K,Ct)|0,b=b+Math.imul(K,Mt)|0,m=m+Math.imul(I,kt)|0,c=c+Math.imul(I,Tt)|0,c=c+Math.imul(R,kt)|0,b=b+Math.imul(R,Tt)|0;var Li=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Li>>>26)|0,Li&=67108863,m=Math.imul(at,dt),c=Math.imul(at,ft),c=c+Math.imul(ct,dt)|0,b=Math.imul(ct,ft),m=m+Math.imul(rt,_t)|0,c=c+Math.imul(rt,pt)|0,c=c+Math.imul(st,_t)|0,b=b+Math.imul(st,pt)|0,m=m+Math.imul(nt,ht)|0,c=c+Math.imul(nt,mt)|0,c=c+Math.imul(ot,ht)|0,b=b+Math.imul(ot,mt)|0,m=m+Math.imul(et,gt)|0,c=c+Math.imul(et,bt)|0,c=c+Math.imul(it,gt)|0,b=b+Math.imul(it,bt)|0,m=m+Math.imul(Y,yt)|0,c=c+Math.imul(Y,wt)|0,c=c+Math.imul(tt,yt)|0,b=b+Math.imul(tt,wt)|0,m=m+Math.imul(X,vt)|0,c=c+Math.imul(X,Pt)|0,c=c+Math.imul(J,vt)|0,b=b+Math.imul(J,Pt)|0,m=m+Math.imul(W,Ct)|0,c=c+Math.imul(W,Mt)|0,c=c+Math.imul(G,Ct)|0,b=b+Math.imul(G,Mt)|0,m=m+Math.imul(V,kt)|0,c=c+Math.imul(V,Tt)|0,c=c+Math.imul(K,kt)|0,b=b+Math.imul(K,Tt)|0,m=m+Math.imul(I,xt)|0,c=c+Math.imul(I,St)|0,c=c+Math.imul(R,xt)|0,b=b+Math.imul(R,St)|0;var $i=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+($i>>>26)|0,$i&=67108863,m=Math.imul(lt,dt),c=Math.imul(lt,ft),c=c+Math.imul(ut,dt)|0,b=Math.imul(ut,ft),m=m+Math.imul(at,_t)|0,c=c+Math.imul(at,pt)|0,c=c+Math.imul(ct,_t)|0,b=b+Math.imul(ct,pt)|0,m=m+Math.imul(rt,ht)|0,c=c+Math.imul(rt,mt)|0,c=c+Math.imul(st,ht)|0,b=b+Math.imul(st,mt)|0,m=m+Math.imul(nt,gt)|0,c=c+Math.imul(nt,bt)|0,c=c+Math.imul(ot,gt)|0,b=b+Math.imul(ot,bt)|0,m=m+Math.imul(et,yt)|0,c=c+Math.imul(et,wt)|0,c=c+Math.imul(it,yt)|0,b=b+Math.imul(it,wt)|0,m=m+Math.imul(Y,vt)|0,c=c+Math.imul(Y,Pt)|0,c=c+Math.imul(tt,vt)|0,b=b+Math.imul(tt,Pt)|0,m=m+Math.imul(X,Ct)|0,c=c+Math.imul(X,Mt)|0,c=c+Math.imul(J,Ct)|0,b=b+Math.imul(J,Mt)|0,m=m+Math.imul(W,kt)|0,c=c+Math.imul(W,Tt)|0,c=c+Math.imul(G,kt)|0,b=b+Math.imul(G,Tt)|0,m=m+Math.imul(V,xt)|0,c=c+Math.imul(V,St)|0,c=c+Math.imul(K,xt)|0,b=b+Math.imul(K,St)|0,m=m+Math.imul(I,At)|0,c=c+Math.imul(I,It)|0,c=c+Math.imul(R,At)|0,b=b+Math.imul(R,It)|0;var Ui=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ui>>>26)|0,Ui&=67108863,m=Math.imul(lt,_t),c=Math.imul(lt,pt),c=c+Math.imul(ut,_t)|0,b=Math.imul(ut,pt),m=m+Math.imul(at,ht)|0,c=c+Math.imul(at,mt)|0,c=c+Math.imul(ct,ht)|0,b=b+Math.imul(ct,mt)|0,m=m+Math.imul(rt,gt)|0,c=c+Math.imul(rt,bt)|0,c=c+Math.imul(st,gt)|0,b=b+Math.imul(st,bt)|0,m=m+Math.imul(nt,yt)|0,c=c+Math.imul(nt,wt)|0,c=c+Math.imul(ot,yt)|0,b=b+Math.imul(ot,wt)|0,m=m+Math.imul(et,vt)|0,c=c+Math.imul(et,Pt)|0,c=c+Math.imul(it,vt)|0,b=b+Math.imul(it,Pt)|0,m=m+Math.imul(Y,Ct)|0,c=c+Math.imul(Y,Mt)|0,c=c+Math.imul(tt,Ct)|0,b=b+Math.imul(tt,Mt)|0,m=m+Math.imul(X,kt)|0,c=c+Math.imul(X,Tt)|0,c=c+Math.imul(J,kt)|0,b=b+Math.imul(J,Tt)|0,m=m+Math.imul(W,xt)|0,c=c+Math.imul(W,St)|0,c=c+Math.imul(G,xt)|0,b=b+Math.imul(G,St)|0,m=m+Math.imul(V,At)|0,c=c+Math.imul(V,It)|0,c=c+Math.imul(K,At)|0,b=b+Math.imul(K,It)|0;var Vi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Vi>>>26)|0,Vi&=67108863,m=Math.imul(lt,ht),c=Math.imul(lt,mt),c=c+Math.imul(ut,ht)|0,b=Math.imul(ut,mt),m=m+Math.imul(at,gt)|0,c=c+Math.imul(at,bt)|0,c=c+Math.imul(ct,gt)|0,b=b+Math.imul(ct,bt)|0,m=m+Math.imul(rt,yt)|0,c=c+Math.imul(rt,wt)|0,c=c+Math.imul(st,yt)|0,b=b+Math.imul(st,wt)|0,m=m+Math.imul(nt,vt)|0,c=c+Math.imul(nt,Pt)|0,c=c+Math.imul(ot,vt)|0,b=b+Math.imul(ot,Pt)|0,m=m+Math.imul(et,Ct)|0,c=c+Math.imul(et,Mt)|0,c=c+Math.imul(it,Ct)|0,b=b+Math.imul(it,Mt)|0,m=m+Math.imul(Y,kt)|0,c=c+Math.imul(Y,Tt)|0,c=c+Math.imul(tt,kt)|0,b=b+Math.imul(tt,Tt)|0,m=m+Math.imul(X,xt)|0,c=c+Math.imul(X,St)|0,c=c+Math.imul(J,xt)|0,b=b+Math.imul(J,St)|0,m=m+Math.imul(W,At)|0,c=c+Math.imul(W,It)|0,c=c+Math.imul(G,At)|0,b=b+Math.imul(G,It)|0;var Ki=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ki>>>26)|0,Ki&=67108863,m=Math.imul(lt,gt),c=Math.imul(lt,bt),c=c+Math.imul(ut,gt)|0,b=Math.imul(ut,bt),m=m+Math.imul(at,yt)|0,c=c+Math.imul(at,wt)|0,c=c+Math.imul(ct,yt)|0,b=b+Math.imul(ct,wt)|0,m=m+Math.imul(rt,vt)|0,c=c+Math.imul(rt,Pt)|0,c=c+Math.imul(st,vt)|0,b=b+Math.imul(st,Pt)|0,m=m+Math.imul(nt,Ct)|0,c=c+Math.imul(nt,Mt)|0,c=c+Math.imul(ot,Ct)|0,b=b+Math.imul(ot,Mt)|0,m=m+Math.imul(et,kt)|0,c=c+Math.imul(et,Tt)|0,c=c+Math.imul(it,kt)|0,b=b+Math.imul(it,Tt)|0,m=m+Math.imul(Y,xt)|0,c=c+Math.imul(Y,St)|0,c=c+Math.imul(tt,xt)|0,b=b+Math.imul(tt,St)|0,m=m+Math.imul(X,At)|0,c=c+Math.imul(X,It)|0,c=c+Math.imul(J,At)|0,b=b+Math.imul(J,It)|0;var Wi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Wi>>>26)|0,Wi&=67108863,m=Math.imul(lt,yt),c=Math.imul(lt,wt),c=c+Math.imul(ut,yt)|0,b=Math.imul(ut,wt),m=m+Math.imul(at,vt)|0,c=c+Math.imul(at,Pt)|0,c=c+Math.imul(ct,vt)|0,b=b+Math.imul(ct,Pt)|0,m=m+Math.imul(rt,Ct)|0,c=c+Math.imul(rt,Mt)|0,c=c+Math.imul(st,Ct)|0,b=b+Math.imul(st,Mt)|0,m=m+Math.imul(nt,kt)|0,c=c+Math.imul(nt,Tt)|0,c=c+Math.imul(ot,kt)|0,b=b+Math.imul(ot,Tt)|0,m=m+Math.imul(et,xt)|0,c=c+Math.imul(et,St)|0,c=c+Math.imul(it,xt)|0,b=b+Math.imul(it,St)|0,m=m+Math.imul(Y,At)|0,c=c+Math.imul(Y,It)|0,c=c+Math.imul(tt,At)|0,b=b+Math.imul(tt,It)|0;var Zi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Zi>>>26)|0,Zi&=67108863,m=Math.imul(lt,vt),c=Math.imul(lt,Pt),c=c+Math.imul(ut,vt)|0,b=Math.imul(ut,Pt),m=m+Math.imul(at,Ct)|0,c=c+Math.imul(at,Mt)|0,c=c+Math.imul(ct,Ct)|0,b=b+Math.imul(ct,Mt)|0,m=m+Math.imul(rt,kt)|0,c=c+Math.imul(rt,Tt)|0,c=c+Math.imul(st,kt)|0,b=b+Math.imul(st,Tt)|0,m=m+Math.imul(nt,xt)|0,c=c+Math.imul(nt,St)|0,c=c+Math.imul(ot,xt)|0,b=b+Math.imul(ot,St)|0,m=m+Math.imul(et,At)|0,c=c+Math.imul(et,It)|0,c=c+Math.imul(it,At)|0,b=b+Math.imul(it,It)|0;var zi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(zi>>>26)|0,zi&=67108863,m=Math.imul(lt,Ct),c=Math.imul(lt,Mt),c=c+Math.imul(ut,Ct)|0,b=Math.imul(ut,Mt),m=m+Math.imul(at,kt)|0,c=c+Math.imul(at,Tt)|0,c=c+Math.imul(ct,kt)|0,b=b+Math.imul(ct,Tt)|0,m=m+Math.imul(rt,xt)|0,c=c+Math.imul(rt,St)|0,c=c+Math.imul(st,xt)|0,b=b+Math.imul(st,St)|0,m=m+Math.imul(nt,At)|0,c=c+Math.imul(nt,It)|0,c=c+Math.imul(ot,At)|0,b=b+Math.imul(ot,It)|0;var Hi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Hi>>>26)|0,Hi&=67108863,m=Math.imul(lt,kt),c=Math.imul(lt,Tt),c=c+Math.imul(ut,kt)|0,b=Math.imul(ut,Tt),m=m+Math.imul(at,xt)|0,c=c+Math.imul(at,St)|0,c=c+Math.imul(ct,xt)|0,b=b+Math.imul(ct,St)|0,m=m+Math.imul(rt,At)|0,c=c+Math.imul(rt,It)|0,c=c+Math.imul(st,At)|0,b=b+Math.imul(st,It)|0;var Gi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Gi>>>26)|0,Gi&=67108863,m=Math.imul(lt,xt),c=Math.imul(lt,St),c=c+Math.imul(ut,xt)|0,b=Math.imul(ut,St),m=m+Math.imul(at,At)|0,c=c+Math.imul(at,It)|0,c=c+Math.imul(ct,At)|0,b=b+Math.imul(ct,It)|0;var Qi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Qi>>>26)|0,Qi&=67108863,m=Math.imul(lt,At),c=Math.imul(lt,It),c=c+Math.imul(ut,At)|0,b=Math.imul(ut,It);var Xi=(C+m|0)+((c&8191)<<13)|0;return C=(b+(c>>>13)|0)+(Xi>>>26)|0,Xi&=67108863,v[0]=ji,v[1]=Fi,v[2]=Ni,v[3]=Bi,v[4]=Oi,v[5]=qi,v[6]=Di,v[7]=Li,v[8]=$i,v[9]=Ui,v[10]=Vi,v[11]=Ki,v[12]=Wi,v[13]=Zi,v[14]=zi,v[15]=Hi,v[16]=Gi,v[17]=Qi,v[18]=Xi,C!==0&&(v[19]=C,_.length++),_};Math.imul||(D=U);function Q(w,s,u){u.negative=s.negative^w.negative,u.length=w.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-w.length+1);c<=m;c++){var b=g-c,T=w.words[b]|0,I=s.words[c]|0,R=T*I,O=R&67108863;v=v+(R/67108864|0)|0,O=O+C|0,C=O&67108863,v=v+(O>>>26)|0,p+=v>>>26,v&=67108863}u.words[g]=C,_=v,v=p}return _!==0?u.words[g]=_:u.length--,u._strip()}function q(w,s,u){return Q(w,s,u)}o.prototype.mulTo=function(s,u){var _,p=this.length+s.length;return this.length===10&&s.length===10?_=D(this,s,u):p<63?_=U(this,s,u):p<1024?_=Q(this,s,u):_=q(this,s,u),_};function L(w,s){this.x=w,this.y=s}L.prototype.makeRBT=function(s){for(var u=new Array(s),_=o.prototype._countBits(s)-1,p=0;p<s;p++)u[p]=this.revBin(p,_,s);return u},L.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},L.prototype.permute=function(s,u,_,p,g,v){for(var C=0;C<v;C++)p[C]=u[s[C]],g[C]=_[s[C]]},L.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),T=0;T<g;T+=m)for(var I=c,R=b,O=0;O<C;O++){var V=_[T+O],K=p[T+O],Ht=_[T+O+C],W=p[T+O+C],G=I*Ht-R*W;W=I*W+R*Ht,Ht=G,_[T+O]=V+Ht,p[T+O]=K+W,_[T+O+C]=V-Ht,p[T+O+C]=K-W,O!==m&&(G=c*I-b*R,R=c*R+b*I,I=G)}},L.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},L.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}},L.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},L.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;t(g===0),t((g&-8192)===0)},L.prototype.stub=function(s){for(var u=new Array(s),_=0;_<s;_++)u[_]=0;return u},L.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),T=new Array(p),I=new Array(p),R=_.words;R.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,T,I,p,g);for(var O=0;O<p;O++){var V=m[O]*T[O]-c[O]*I[O];c[O]=m[O]*I[O]+c[O]*T[O],m[O]=V}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^u.negative,_.length=s.length+u.length,_._strip()},o.prototype.mul=function(s){var u=new o(null);return u.words=new Array(this.length+s.length),this.mulTo(s,u)},o.prototype.mulf=function(s){var u=new o(null);return u.words=new Array(this.length+s.length),q(this,s,u)},o.prototype.imul=function(s){return this.clone().mulTo(s,this)},o.prototype.imuln=function(s){var u=s<0;u&&(s=-s),t(typeof s=="number"),t(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},o.prototype.muln=function(s){return this.clone().imuln(s)},o.prototype.sqr=function(){return this.mul(this)},o.prototype.isqr=function(){return this.imul(this.clone())},o.prototype.pow=function(s){var u=x(s);if(u.length===0)return new o(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 _},o.prototype.iushln=function(s){t(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()},o.prototype.ishln=function(s){return t(this.negative===0),this.iushln(s)},o.prototype.iushrn=function(s,u,_){t(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 T=this.words[c]|0;this.words[c]=b<<26-g|T>>>g,b=T&C}return m&&b!==0&&(m.words[m.length++]=b),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},o.prototype.ishrn=function(s,u,_){return t(this.negative===0),this.iushrn(s,u,_)},o.prototype.shln=function(s){return this.clone().ishln(s)},o.prototype.ushln=function(s){return this.clone().iushln(s)},o.prototype.shrn=function(s){return this.clone().ishrn(s)},o.prototype.ushrn=function(s){return this.clone().iushrn(s)},o.prototype.testn=function(s){t(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)},o.prototype.imaskn=function(s){t(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26;if(t(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.length===0&&(this.words[0]=0,this.length=1),this._strip()},o.prototype.maskn=function(s){return this.clone().imaskn(s)},o.prototype.iaddn=function(s){return t(typeof s=="number"),t(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)},o.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},o.prototype.isubn=function(s){if(t(typeof s=="number"),t(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()},o.prototype.addn=function(s){return this.clone().iaddn(s)},o.prototype.subn=function(s){return this.clone().isubn(s)},o.prototype.iabs=function(){return this.negative=0,this},o.prototype.abs=function(){return this.clone().iabs()},o.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(t(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()},o.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 o(null),c.length=m+1,c.words=new Array(c.length);for(var b=0;b<c.length;b++)c.words[b]=0}var T=p.clone()._ishlnsubmul(g,1,m);T.negative===0&&(p=T,c&&(c.words[m]=1));for(var I=m-1;I>=0;I--){var R=(p.words[g.length+I]|0)*67108864+(p.words[g.length+I-1]|0);for(R=Math.min(R/v|0,67108863),p._ishlnsubmul(g,R,I);p.negative!==0;)R--,p.negative=0,p._ishlnsubmul(g,1,I),p.isZero()||(p.negative^=1);c&&(c.words[I]=R)}return c&&c._strip(),p._strip(),u!=="div"&&_!==0&&p.iushrn(_),{div:c||null,mod:p}},o.prototype.divmod=function(s,u,_){if(t(!s.isZero()),this.isZero())return{div:new o(0),mod:new o(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 o(0),mod:this}:s.length===1?u==="div"?{div:this.divn(s.words[0]),mod:null}:u==="mod"?{div:null,mod:new o(this.modrn(s.words[0]))}:{div:this.divn(s.words[0]),mod:new o(this.modrn(s.words[0]))}:this._wordDiv(s,u)},o.prototype.div=function(s){return this.divmod(s,"div",!1).div},o.prototype.mod=function(s){return this.divmod(s,"mod",!1).mod},o.prototype.umod=function(s){return this.divmod(s,"mod",!0).mod},o.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)},o.prototype.modrn=function(s){var u=s<0;u&&(s=-s),t(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},o.prototype.modn=function(s){return this.modrn(s)},o.prototype.idivn=function(s){var u=s<0;u&&(s=-s),t(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},o.prototype.divn=function(s){return this.clone().idivn(s)},o.prototype.egcd=function(s){t(s.negative===0),t(!s.isZero());var u=this,_=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var p=new o(1),g=new o(0),v=new o(0),C=new o(1),m=0;u.isEven()&&_.isEven();)u.iushrn(1),_.iushrn(1),++m;for(var c=_.clone(),b=u.clone();!u.isZero();){for(var T=0,I=1;(u.words[0]&I)===0&&T<26;++T,I<<=1);if(T>0)for(u.iushrn(T);T-- >0;)(p.isOdd()||g.isOdd())&&(p.iadd(c),g.isub(b)),p.iushrn(1),g.iushrn(1);for(var R=0,O=1;(_.words[0]&O)===0&&R<26;++R,O<<=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);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)}},o.prototype._invmp=function(s){t(s.negative===0),t(!s.isZero());var u=this,_=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var p=new o(1),g=new o(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 T;return u.cmpn(1)===0?T=p:T=g,T.cmpn(0)<0&&T.iadd(s),T},o.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)},o.prototype.invm=function(s){return this.egcd(s).a.umod(s)},o.prototype.isEven=function(){return(this.words[0]&1)===0},o.prototype.isOdd=function(){return(this.words[0]&1)===1},o.prototype.andln=function(s){return this.words[0]&s},o.prototype.bincn=function(s){t(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},o.prototype.isZero=function(){return this.length===1&&this.words[0]===0},o.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),t(s<=67108863,"Number is too big");var p=this.words[0]|0;_=p===s?0:p<s?-1:1}return this.negative!==0?-_|0:_},o.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},o.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},o.prototype.gtn=function(s){return this.cmpn(s)===1},o.prototype.gt=function(s){return this.cmp(s)===1},o.prototype.gten=function(s){return this.cmpn(s)>=0},o.prototype.gte=function(s){return this.cmp(s)>=0},o.prototype.ltn=function(s){return this.cmpn(s)===-1},o.prototype.lt=function(s){return this.cmp(s)===-1},o.prototype.lten=function(s){return this.cmpn(s)<=0},o.prototype.lte=function(s){return this.cmp(s)<=0},o.prototype.eqn=function(s){return this.cmpn(s)===0},o.prototype.eq=function(s){return this.cmp(s)===0},o.red=function(s){return new $(s)},o.prototype.toRed=function(s){return t(!this.red,"Already a number in reduction context"),t(this.negative===0,"red works only with positives"),s.convertTo(this)._forceRed(s)},o.prototype.fromRed=function(){return t(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},o.prototype._forceRed=function(s){return this.red=s,this},o.prototype.forceRed=function(s){return t(!this.red,"Already a number in reduction context"),this._forceRed(s)},o.prototype.redAdd=function(s){return t(this.red,"redAdd works only with red numbers"),this.red.add(this,s)},o.prototype.redIAdd=function(s){return t(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,s)},o.prototype.redSub=function(s){return t(this.red,"redSub works only with red numbers"),this.red.sub(this,s)},o.prototype.redISub=function(s){return t(this.red,"redISub works only with red numbers"),this.red.isub(this,s)},o.prototype.redShl=function(s){return t(this.red,"redShl works only with red numbers"),this.red.shl(this,s)},o.prototype.redMul=function(s){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.mul(this,s)},o.prototype.redIMul=function(s){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.imul(this,s)},o.prototype.redSqr=function(){return t(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},o.prototype.redISqr=function(){return t(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},o.prototype.redSqrt=function(){return t(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},o.prototype.redInvm=function(){return t(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},o.prototype.redNeg=function(){return t(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},o.prototype.redPow=function(s){return t(this.red&&!s.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,s)};var Lt={k256:null,p224:null,p192:null,p25519:null};function H(w,s){this.name=w,this.p=new o(s,16),this.n=this.p.bitLength(),this.k=new o(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}H.prototype._tmp=function(){var s=new o(null);return s.words=new Array(Math.ceil(this.n/13)),s},H.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},H.prototype.split=function(s,u){s.iushrn(this.n,0,u)},H.prototype.imulK=function(s){return s.imul(this.k)};function $t(){H.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}i($t,H),$t.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},$t.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 ie(){H.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}i(ie,H);function zt(){H.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}i(zt,H);function ne(){H.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}i(ne,H),ne.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},o._prime=function(s){if(Lt[s])return Lt[s];var u;if(s==="k256")u=new $t;else if(s==="p224")u=new ie;else if(s==="p192")u=new zt;else if(s==="p25519")u=new ne;else throw new Error("Unknown prime "+s);return Lt[s]=u,u};function $(w){if(typeof w=="string"){var s=o._prime(w);this.m=s.p,this.prime=s}else t(w.gtn(1),"modulus must be greater than 1"),this.m=w,this.prime=null}$.prototype._verify1=function(s){t(s.negative===0,"red works only with positives"),t(s.red,"red works only with red numbers")},$.prototype._verify2=function(s,u){t((s.negative|u.negative)===0,"red works only with positives"),t(s.red&&s.red===u.red,"red works only with red numbers")},$.prototype.imod=function(s){return this.prime?this.prime.ireduce(s)._forceRed(this):(d(s,s.umod(this.m)._forceRed(this)),s)},$.prototype.neg=function(s){return s.isZero()?s.clone():this.m.sub(s)._forceRed(this)},$.prototype.add=function(s,u){this._verify2(s,u);var _=s.add(u);return _.cmp(this.m)>=0&&_.isub(this.m),_._forceRed(this)},$.prototype.iadd=function(s,u){this._verify2(s,u);var _=s.iadd(u);return _.cmp(this.m)>=0&&_.isub(this.m),_},$.prototype.sub=function(s,u){this._verify2(s,u);var _=s.sub(u);return _.cmpn(0)<0&&_.iadd(this.m),_._forceRed(this)},$.prototype.isub=function(s,u){this._verify2(s,u);var _=s.isub(u);return _.cmpn(0)<0&&_.iadd(this.m),_},$.prototype.shl=function(s,u){return this._verify1(s),this.imod(s.ushln(u))},$.prototype.imul=function(s,u){return this._verify2(s,u),this.imod(s.imul(u))},$.prototype.mul=function(s,u){return this._verify2(s,u),this.imod(s.mul(u))},$.prototype.isqr=function(s){return this.imul(s,s.clone())},$.prototype.sqr=function(s){return this.mul(s,s)},$.prototype.sqrt=function(s){if(s.isZero())return s.clone();var u=this.m.andln(3);if(t(u%2===1),u===3){var _=this.m.add(new o(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);t(!p.isZero());var v=new o(1).toRed(this),C=v.redNeg(),m=this.m.subn(1).iushrn(1),c=this.m.bitLength();for(c=new o(2*c*c).toRed(this);this.pow(c,m).cmp(C)!==0;)c.redIAdd(C);for(var b=this.pow(c,p),T=this.pow(s,p.addn(1).iushrn(1)),I=this.pow(s,p),R=g;I.cmp(v)!==0;){for(var O=I,V=0;O.cmp(v)!==0;V++)O=O.redSqr();t(V<R);var K=this.pow(b,new o(1).iushln(R-V-1));T=T.redMul(K),b=K.redSqr(),I=I.redMul(b),R=V}return T},$.prototype.invm=function(s){var u=s._invmp(this.m);return u.negative!==0?(u.negative=0,this.imod(u).redNeg()):this.imod(u)},$.prototype.pow=function(s,u){if(u.isZero())return new o(1).toRed(this);if(u.cmpn(1)===0)return s.clone();var _=4,p=new Array(1<<_);p[0]=new o(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],T=c-1;T>=0;T--){var I=b>>T&1;if(v!==p[0]&&(v=this.sqr(v)),I===0&&C===0){m=0;continue}C<<=1,C|=I,m++,!(m!==_&&(g!==0||T!==0))&&(v=this.mul(v,p[C]),m=0,C=0)}c=26}return v},$.prototype.convertTo=function(s){var u=s.umod(this.m);return u===s?u.clone():u},$.prototype.convertFrom=function(s){var u=s.clone();return u.red=null,u},o.mont=function(s){return new Ot(s)};function Ot(w){$.call(this,w),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new o(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(Ot,$),Ot.prototype.convertTo=function(s){return this.imod(s.ushln(this.shift))},Ot.prototype.convertFrom=function(s){var u=this.imod(s.mul(this.rinv));return u.red=null,u},Ot.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)},Ot.prototype.mul=function(s,u){if(s.isZero()||u.isZero())return new o(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)},Ot.prototype.invm=function(s){var u=this.imod(s._invmp(this.m).mul(this.r2));return u._forceRed(this)}})(typeof on>"u"||on,so)});import{SdkWrapper as Fa}from"@cetusprotocol/common-sdk";import{fromHex as Dr,normalizeSuiObjectId as xe,toHex as Lr}from"@mysten/sui/utils";import{CACHE_TIME_24H as Kt,CACHE_TIME_5MIN as $r,CachedContent as Ur,extractStructTagFromType as ii,fixCoinType as Vr,getFutureTime as no,getPackagerConfigs as le,normalizeCoinType as oo}from"@cetusprotocol/common-sdk";var Kn="3.7.8",Ar=Kn,ke=typeof Buffer=="function",Dn=typeof TextDecoder=="function"?new TextDecoder:void 0,Ln=typeof TextEncoder=="function"?new TextEncoder:void 0,Ir="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",Ue=Array.prototype.slice.call(Ir),ti=(n=>{let e={};return n.forEach((t,i)=>e[t]=i),e})(Ue),Rr=/^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/,Et=String.fromCharCode.bind(String),$n=typeof Uint8Array.from=="function"?Uint8Array.from.bind(Uint8Array):n=>new Uint8Array(Array.prototype.slice.call(n,0)),Wn=n=>n.replace(/=/g,"").replace(/[+\/]/g,e=>e=="+"?"-":"_"),Zn=n=>n.replace(/[^A-Za-z0-9\+\/]/g,""),zn=n=>{let e,t,i,o,r="",a=n.length%3;for(let l=0;l<n.length;){if((t=n.charCodeAt(l++))>255||(i=n.charCodeAt(l++))>255||(o=n.charCodeAt(l++))>255)throw new TypeError("invalid character found");e=t<<16|i<<8|o,r+=Ue[e>>18&63]+Ue[e>>12&63]+Ue[e>>6&63]+Ue[e&63]}return a?r.slice(0,a-3)+"===".substring(a):r},tn=typeof btoa=="function"?n=>btoa(n):ke?n=>Buffer.from(n,"binary").toString("base64"):zn,Ji=ke?n=>Buffer.from(n).toString("base64"):n=>{let t=[];for(let i=0,o=n.length;i<o;i+=4096)t.push(Et.apply(null,n.subarray(i,i+4096)));return tn(t.join(""))},ei=(n,e=!1)=>e?Wn(Ji(n)):Ji(n),Er=n=>{if(n.length<2){var e=n.charCodeAt(0);return e<128?n:e<2048?Et(192|e>>>6)+Et(128|e&63):Et(224|e>>>12&15)+Et(128|e>>>6&63)+Et(128|e&63)}else{var e=65536+(n.charCodeAt(0)-55296)*1024+(n.charCodeAt(1)-56320);return Et(240|e>>>18&7)+Et(128|e>>>12&63)+Et(128|e>>>6&63)+Et(128|e&63)}},jr=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g,Hn=n=>n.replace(jr,Er),Un=ke?n=>Buffer.from(n,"utf8").toString("base64"):Ln?n=>Ji(Ln.encode(n)):n=>tn(Hn(n)),Me=(n,e=!1)=>e?Wn(Un(n)):Un(n),Vn=n=>Me(n,!0),Fr=/[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g,Nr=n=>{switch(n.length){case 4:var e=(7&n.charCodeAt(0))<<18|(63&n.charCodeAt(1))<<12|(63&n.charCodeAt(2))<<6|63&n.charCodeAt(3),t=e-65536;return Et((t>>>10)+55296)+Et((t&1023)+56320);case 3:return Et((15&n.charCodeAt(0))<<12|(63&n.charCodeAt(1))<<6|63&n.charCodeAt(2));default:return Et((31&n.charCodeAt(0))<<6|63&n.charCodeAt(1))}},Gn=n=>n.replace(Fr,Nr),Qn=n=>{if(n=n.replace(/\s+/g,""),!Rr.test(n))throw new TypeError("malformed base64.");n+="==".slice(2-(n.length&3));let e,t,i,o=[];for(let r=0;r<n.length;)e=ti[n.charAt(r++)]<<18|ti[n.charAt(r++)]<<12|(t=ti[n.charAt(r++)])<<6|(i=ti[n.charAt(r++)]),t===64?o.push(Et(e>>16&255)):i===64?o.push(Et(e>>16&255,e>>8&255)):o.push(Et(e>>16&255,e>>8&255,e&255));return o.join("")},en=typeof atob=="function"?n=>atob(Zn(n)):ke?n=>Buffer.from(n,"base64").toString("binary"):Qn,Xn=ke?n=>$n(Buffer.from(n,"base64")):n=>$n(en(n).split("").map(e=>e.charCodeAt(0))),Jn=n=>Xn(Yn(n)),Br=ke?n=>Buffer.from(n,"base64").toString("utf8"):Dn?n=>Dn.decode(Xn(n)):n=>Gn(en(n)),Yn=n=>Zn(n.replace(/[-_]/g,e=>e=="-"?"+":"/")),Yi=n=>Br(Yn(n)),Or=n=>{if(typeof n!="string")return!1;let e=n.replace(/\s+/g,"").replace(/={0,2}$/,"");return!/[^\s0-9a-zA-Z\+/]/.test(e)||!/[^\s0-9a-zA-Z\-_]/.test(e)},to=n=>({value:n,enumerable:!1,writable:!0,configurable:!0}),eo=function(){let n=(e,t)=>Object.defineProperty(String.prototype,e,to(t));n("fromBase64",function(){return Yi(this)}),n("toBase64",function(e){return Me(this,e)}),n("toBase64URI",function(){return Me(this,!0)}),n("toBase64URL",function(){return Me(this,!0)}),n("toUint8Array",function(){return Jn(this)})},io=function(){let n=(e,t)=>Object.defineProperty(Uint8Array.prototype,e,to(t));n("toBase64",function(e){return ei(this,e)}),n("toBase64URI",function(){return ei(this,!0)}),n("toBase64URL",function(){return ei(this,!0)})},qr=()=>{eo(),io()},Te={version:Kn,VERSION:Ar,atob:en,atobPolyfill:Qn,btoa:tn,btoaPolyfill:zn,fromBase64:Yi,toBase64:Me,encode:Me,encodeURI:Vn,encodeURL:Vn,utob:Hn,btou:Gn,decode:Yi,isValid:Or,fromUint8Array:ei,toUint8Array:Jn,extendString:eo,extendUint8Array:io,extendBuiltins:qr};import{bcs as N}from"@mysten/sui/bcs";var Kr=N.struct("TypeName",{name:N.string()}),Wr=N.struct("Entry<String, String>",{key:N.string(),value:N.string()}),nn=N.struct("VecMap<String, String>",{contents:N.vector(Wr)}),Zr=N.struct("Coin",{name:N.string(),symbol:N.string(),coingecko_id:N.string(),pyth_id:N.string(),decimals:N.u8(),logo_url:N.string(),project_url:N.string(),coin_type:Kr,extension_fields:nn}),ro=N.bytes(32).transform({input:n=>Dr(n),output:n=>Lr(n)}),zr=N.struct("Pool",{pool_address:ro,pool_type:N.string(),project_url:N.string(),is_closed:N.bool(),is_show_rewarder:N.bool(),show_rewarder_1:N.bool(),show_rewarder_2:N.bool(),show_rewarder_3:N.bool(),extension_fields:nn}),Hr=N.struct("MediaInfo",{name:N.string(),link:N.string()}),Gr=N.struct("Entry<String, MediaInfo>",{key:N.string(),value:Hr}),Qr=N.struct("VecMap<String, MediaInfo>",{contents:N.vector(Gr)}),Xr=N.struct("LaunchpadPool",{pool_address:ro,is_closed:N.bool(),show_settle:N.bool(),coin_symbol:N.string(),coin_name:N.string(),coin_icon:N.string(),banners:N.vector(N.string()),introduction:N.string(),website:N.string(),tokenomics:N.string(),social_media:Qr,terms:N.string(),white_list_terms:N.string(),regulation:N.string(),project_details:N.string(),extension_fields:nn}),ni=class{constructor(e){this._cache={};this._sdk=e}get sdk(){return this._sdk}setTokenListCache(e){let{coin_list_handle:t}=le(this.sdk.sdkOptions.cetus_config),i=`${t}_getCoinConfigs`,o=this.getCache(i),r=o?[...o,...e]:e;this.updateCache(i,r)}async getTokenListByCoinTypes(e){let t={},{coin_list_handle:i}=le(this.sdk.sdkOptions.cetus_config),o=`${i}_getCoinConfigs`,r=this.getCache(o);if(r!==void 0){let l=r;for(let f of e)for(let d of l)if(oo(f)===oo(d.address)){t[f]=d;continue}}let a=e.filter(l=>t[l]===void 0);for(let l of a){let f=`${l}_metadata`,d=this.getCache(f);if(d!==void 0)t[l]=d;else{let h=await this._sdk.FullClient.getCoinMetadata({coinType:l});if(h.coinMetadata){let{id:y,name:P,symbol:M,decimals:S,iconUrl:x}=h.coinMetadata,U={id:y,pyth_id:"",name:P,symbol:M,official_symbol:M,coingecko_id:"",decimals:S,project_url:"",logo_url:x,address:l};t[l]=U,this.updateCache(f,U,Kt)}else console.log(`not found ${l}`)}}return t}async getCoinConfigs(e=!1,t=!0){let{coin_list_handle:i}=le(this.sdk.sdkOptions.cetus_config),o=`${i}_getCoinConfigs`,r=this.getCache(o,e);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.fieldId),f=await this._sdk.FullClient.batchGetObjects(l,{json:!0}),d=[];return f.forEach(h=>{let y=this.buildCoinConfig(h,t);this.updateCache(`${i}_${y.address}_getCoinConfig`,y,Kt),d.push({...y})}),this.updateCache(o,d,Kt),d}async getCoinConfig(e,t=!1,i=!0){let{coin_list_handle:o}=le(this.sdk.sdkOptions.cetus_config),r=`${o}_${e}_getCoinConfig`,a=this.getCache(r,t);if(a)return a;let l=await this._sdk.FullClient.getDynamicField({parentId:o,name:{type:"0x1::type_name::TypeName",bcs:N.String.serialize(Vr(e,!0)).toBytes()}}),f=Zr.parse(l.dynamicField.value.bcs),d={id:l.dynamicField.fieldId,address:ii(f.coin_type.name).full_address,name:f.name,symbol:f.symbol,pyth_id:f.pyth_id,project_url:f.project_url,logo_url:f.logo_url,decimals:f.decimals};return d.pyth_id&&(d.pyth_id=xe(d.pyth_id)),this.transformExtensions(d,f.extension_fields.contents,i),delete d.coin_type,this.updateCache(r,d,Kt),d}buildCoinConfig(e,t=!0){let{json:i,objectId:o}=e,r=i.value,a={...r};return a.id=o,a.address=ii(r.coin_type.name).full_address,r.pyth_id&&(a.pyth_id=xe(r.pyth_id)),this.transformExtensions(a,r.extension_fields.contents,t),delete a.coin_type,a}async getClmmPoolConfigs(e=!1,t=!0){let{clmm_pools_handle:i}=le(this.sdk.sdkOptions.cetus_config),o=`${i}_getClmmPoolConfigs`,r=this.getCache(o,e);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.fieldId),f=await this._sdk.FullClient.batchGetObjects(l,{json:!0}),d=[];return f.forEach(h=>{let y=this.buildClmmPoolConfig(h,t);this.updateCache(`${y.pool_address}_getClmmPoolConfig`,y,Kt),d.push({...y})}),this.updateCache(o,d,Kt),d}async getClmmPoolConfig(e,t=!1,i=!0){let{clmm_pools_handle:o}=le(this.sdk.sdkOptions.cetus_config),r=`${e}_getClmmPoolConfig`,a=this.getCache(r,t);if(a)return a;let l=await this._sdk.FullClient.getDynamicField({parentId:o,name:{type:"address",bcs:N.Address.serialize(e).toBytes()}}),f=zr.parse(l.dynamicField.value.bcs),d={id:l.dynamicField.fieldId,pool_address:xe(f.pool_address),pool_type:f.pool_type,project_url:f.project_url,is_closed:f.is_closed,is_show_rewarder:f.is_show_rewarder,show_rewarder_1:f.show_rewarder_1,show_rewarder_2:f.show_rewarder_2,show_rewarder_3:f.show_rewarder_3};return this.transformExtensions(d,f.extension_fields.contents,i),this.updateCache(r,d,Kt),d}buildClmmPoolConfig(e,t=!0){let{json:i,objectId:o}=e,r={...i.value};return r.id=o,r.pool_address=xe(i.value.pool_address),this.transformExtensions(r,i.value.extension_fields.contents,t),r}async getLaunchpadPoolConfigs(e=!1,t=!0){let{launchpad_pools_handle:i}=le(this.sdk.sdkOptions.cetus_config),o=`${i}_getLaunchpadPoolConfigs`,r=this.getCache(o,e);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.fieldId),f=await this._sdk.FullClient.batchGetObjects(l,{json:!0}),d=[];return f.forEach(h=>{let y=this.buildLaunchpadPoolConfig(h,t);this.updateCache(`${y.pool_address}_getLaunchpadPoolConfig`,y,Kt),d.push({...y})}),this.updateCache(o,d,Kt),d}async getLaunchpadPoolConfig(e,t=!1,i=!0){let{launchpad_pools_handle:o}=le(this.sdk.sdkOptions.cetus_config),r=`${e}_getLaunchpadPoolConfig`,a=this.getCache(r,t);if(a)return a;let l=await this._sdk.FullClient.getDynamicField({parentId:o,name:{type:"address",bcs:N.Address.serialize(e).toBytes()}}),f=Xr.parse(l.dynamicField.value.bcs),d={...f,id:l.dynamicField.fieldId,pool_address:xe(f.pool_address),social_media:f.social_media.contents.map(h=>({name:h.value.name,link:h.value.link}))};this.transformExtensions(d,f.extension_fields.contents,i);try{d.regulation=decodeURIComponent(Te.decode(d.regulation).replace(/%/g,"%25"))}catch{d.regulation=Te.decode(d.regulation)}return this.updateCache(r,d,Kt),d}buildLaunchpadPoolConfig(e,t=!0){let{json:i,objectId:o}=e,r={...i.value};r.id=o,r.pool_address=xe(r.pool_address),this.transformExtensions(r,r.extension_fields.contents,t);let a=[];r.social_media.contents.forEach(l=>{a.push({name:l.value.name,link:l.value.link})}),r.social_media=a;try{r.regulation=decodeURIComponent(Te.decode(r.regulation).replace(/%/g,"%25"))}catch{r.regulation=Te.decode(r.regulation)}return r}transformExtensions(e,t,i=!0){let o=[];for(let r of t){let{key:a}=r,l=r.value;if(a==="labels")try{let f=decodeURIComponent(Te.decode(l));try{l=JSON.parse(f)}catch{l=f}}catch{}i&&(e[a]=l),o.push({key:a,value:l})}delete e.extension_fields,i||(e.extensions=o)}async getCetusConfig(e=!1){let t=this._sdk.sdkOptions.cetus_config.package_id,i=`${t}_getCetusConfig`,o=this.getCache(i,e);if(o!==void 0)return o;let a=(await this._sdk.FullClient.getObject({objectId:t,include:{effects:!0,previousTransaction:!0,previousTxDigest:!0}})).object.previousTransaction,l=await this._sdk.FullClient.queryEventsByPage({Transaction:a}),f={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 d of l.data)switch(ii(d.type).name){case"InitCoinListEvent":f.coin_list_id=d.parsedJson.coin_list_id;break;case"InitLaunchpadPoolsEvent":f.launchpad_pools_id=d.parsedJson.launchpad_pools_id;break;case"InitClmmPoolsEvent":f.clmm_pools_id=d.parsedJson.pools_id;break;case"InitConfigEvent":f.global_config_id=d.parsedJson.global_config_id,f.admin_cap_id=d.parsedJson.admin_cap_id;break;default:break}return f=await this.getCetusConfigHandle(f),f.clmm_pools_id.length>0&&this.updateCache(i,f,Kt),f}async getCetusConfigHandle(e){let t=[e.clmm_pools_id,e.coin_list_id,e.launchpad_pools_id];return(await this._sdk.FullClient.batchGetObjects(t,{json:!0})).forEach(o=>{let r=o.data?.json,a=o.data?.type;switch(ii(a).name){case"ClmmPools":e.clmm_pools_handle=r.pools.id;break;case"CoinList":e.coin_list_handle=r.coins.id;break;case"LaunchpadPools":e.launchpad_pools_handle=r.pools.id;break;default:break}}),e}updateCache(e,t,i=$r){let o=this._cache[e];o?(o.overdue_time=no(i),o.value=t):o=new Ur(t,no(i)),this._cache[e]=o}getCache(e,t=!1){try{let i=this._cache[e];if(!i)return;if(t||!i.isValid()){delete this._cache[e];return}return i.value}catch(i){console.error(`Error accessing cache for key ${e}:`,i);return}}};var Jo=ce(oe(),1);import{Transaction as Ie}from"@mysten/sui/transactions";import{normalizeSuiAddress as Re}from"@mysten/sui/utils";import{BaseError as Jr}from"@cetusprotocol/common-sdk";var ao=(d=>(d.InvalidSqrtPriceLimitDirection="InvalidSqrtPriceLimitDirection",d.ZeroTradableAmount="ZeroTradableAmount",d.AmountOutBelowMinimum="AmountOutBelowMinimum",d.AmountInAboveMaximum="AmountInAboveMaximum",d.NextTickNotFound="NextTickNotFound",d.TickArraySequenceInvalid="TickArraySequenceInvalid",d.TickArrayCrossingAboveMax="TickArrayCrossingAboveMax",d.TickArrayIndexNotInitialized="TickArrayIndexNotInitialized",d.ParamsLengthNotEqual="ParamsLengthNotEqual",d))(ao||{}),rn=(r=>(r.InvalidTickEvent="InvalidTickEvent",r.InvalidPositionObject="InvalidPositionObject",r.InvalidPositionRewardObject="InvalidPositionRewardObject",r.InvalidParams="InvalidParams",r.FetchError="FetchError",r))(rn||{}),oi=(d=>(d.InvalidCoinTypeSequence="InvalidCoinTypeSequence",d.InvalidTickIndex="InvalidTickIndex",d.InvalidPoolObject="InvalidPoolObject",d.InvalidTickObjectId="InvalidTickObjectId",d.InvalidTickObject="InvalidTickObject",d.InvalidTickFields="InvalidTickFields",d.PoolsNotFound="PoolsNotFound",d.StatsPoolsUrlNotSet="StatsPoolsUrlNotSet",d.FetchError="FetchError",d))(oi||{}),co=(t=>(t.ClmmVestNotSet="ClmmVestNotSet",t.ClmmVestFetchError="ClmmVestFetchError",t))(co||{}),lo=(t=>(t.NotFoundPartnerObject="NotFoundPartnerObject",t.InvalidPartnerRefFeeFields="InvalidPartnerRefFeeFields",t))(lo||{}),Ve=(i=>(i.InvalidConfig="InvalidConfig",i.InvalidConfigHandle="InvalidConfigHandle",i.InvalidSimulateAccount="InvalidSimulateAccount",i))(Ve||{}),uo=(a=>(a.InvalidSendAddress="InvalidSendAddress",a.InvalidRecipientAddress="InvalidRecipientAddress",a.InvalidRecipientAndAmountLength="InvalidRecipientAndAmountLength",a.InsufficientBalance="InsufficientBalance",a.InvalidTarget="InvalidTarget",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a))(uo||{}),Yr=(a=>(a.InvalidCoin="InvalidCoin",a.NotFoundPath="NotFoundPath",a.NoDowngradeNeedParams="NoDowngradeNeedParams",a.InvalidSwapCountUrl="InvalidSwapCountUrl",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a.InvalidServerResponse="InvalidServerResponse",a))(Yr||{}),fo=(e=>(e.InvalidType="InvalidType",e))(fo||{}),Se=class extends Jr{constructor(e,t,i){super(e,t||"UnknownError",i)}static isVaultsErrorCode(e,t){return this.isErrorCode(e,t)}},Bt=(n,e,t)=>{throw new Se(e.message,n,t)},Z=(n,e,t)=>{throw new Se(e,n,t)};var _o="position_liquidity_snapshot",po=(i=>(i.Deleted="Deleted",i.Exists="Exists",i.NotExists="NotExists",i))(po||{}),ho=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","AddLiquidityV2Event","RemoveLiquidityV2Event"];var Ft=ce(oe(),1);import{asUintN as Co,FEE_RATE_DENOMINATOR as ci,getDeltaDownFromOutput as Mo,getDeltaUpFromInput as ko,getNextSqrtPriceFromInput as as,getNextSqrtPriceFromOutput as cs,MathUtil as ye,ZERO as Ut}from"@cetusprotocol/common-sdk";var Po=ce(oe(),1);import{Transaction as an}from"@mysten/sui/transactions";import{adjustForSlippage as is,CLOCK_ADDRESS as ai,CoinAssist as be,getPackagerConfigs as vo,MathUtil as cn,MAX_SQRT_PRICE as ns,MIN_SQRT_PRICE as os,U64_MAX as rs,ZERO as ss}from"@cetusprotocol/common-sdk";var sn="partner",ri="pool_script",Gt="pool_script_v2",si="pool_script_v3",me="router",mo="router_with_partner",Qt="fetcher_script",Ya="expect_swap",go="utils",tc="custodian_v2",ec="clob_v2",ic="endpoints_v2",nc=n=>{if(typeof n=="string"&&n.startsWith("0x"))return"object";if(typeof n=="number"||typeof n=="bigint")return"u64";if(typeof n=="boolean")return"bool";throw new Se(`Unknown type for value: ${n}`,"InvalidType")};import{Transaction as ts}from"@mysten/sui/transactions";import{asUintN as bo,CLOCK_ADDRESS as yo,CoinAssist as ge,getPackagerConfigs as es,normalizeCoinType as wo}from"@cetusprotocol/common-sdk";function fc(n){let e=ge.isSuiCoin(n.coin_type_a),t=ge.isSuiCoin(n.coin_type_b);return{is_adjust_coin_a:e,is_adjust_coin_b:t}}var Wt=class{static createCollectRewarderAndFeeParams(e,t,i){let o=wo(i.coin_type_a),r=wo(i.coin_type_b);i.collect_fee&&(t=e.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},t,ge.buildCoinWithBalance(BigInt(0),i.coin_type_a,t),ge.buildCoinWithBalance(BigInt(0),i.coin_type_b,t)));let a=[];return i.rewarder_coin_types.forEach(l=>{a.push(ge.buildCoinWithBalance(BigInt(0),l,t))}),t=e.Rewarder.createCollectRewarderPayload(i,t,a),t}static async buildAddLiquidityFixToken(e,t,i,o,r){i=i||new ts;let a=o||ge.buildCoinWithBalance(BigInt(t.amount_a),t.coin_type_a,i),l=r||ge.buildCoinWithBalance(BigInt(t.amount_b),t.coin_type_b,i),f=[t.coin_type_a,t.coin_type_b],d=t.is_open?"open_position_with_liquidity_by_fix_coin":"add_liquidity_by_fix_coin",{clmm_pool:h,integrate:y}=e.sdkOptions;t.is_open||(i=this.createCollectRewarderAndFeeParams(e,i,t));let P=es(h),M=t.is_open?[i.object(P.global_config_id),i.object(t.pool_id),i.pure.u32(Number(bo(BigInt(t.tick_lower)).toString())),i.pure.u32(Number(bo(BigInt(t.tick_upper)).toString())),a,l,i.pure.u64(t.amount_a),i.pure.u64(t.amount_b),i.pure.bool(t.fix_amount_a),i.object(yo)]:[i.object(P.global_config_id),i.object(t.pool_id),i.object(t.pos_id),a,l,i.pure.u64(t.amount_a),i.pure.u64(t.amount_b),i.pure.bool(t.fix_amount_a),i.object(yo)];return i.moveCall({target:`${y.published_at}::${Gt}::${d}`,typeArguments:f,arguments:M}),i}static checkCoinThreshold(e,t,i,o,r,a){t&&i.moveCall({target:`${e.sdkOptions.integrate.published_at}::${me}::check_coin_threshold`,typeArguments:[a],arguments:[o,i.pure.u64(r)]})}};var ue=class n{static getDefaultSqrtPriceLimit(e){return new Po.default(e?os:ns)}static getDefaultOtherAmountThreshold(e){return e?ss:rs}static buildSwapTransactionArgs(e,t,i,o,r){let{clmm_pool:a,integrate:l}=i,f=n.getDefaultSqrtPriceLimit(t.a2b),d=[t.coin_type_a,t.coin_type_b],{global_config_id:h}=vo(a);h===void 0&&Z("InvalidConfig","clmm.config.global_config_id is undefined");let y=t.swap_partner!==void 0,P=y?t.a2b?"swap_a2b_with_partner":"swap_b2a_with_partner":t.a2b?"swap_a2b":"swap_b2a",M=y?[e.object(h),e.object(t.pool_id),e.object(t.swap_partner),o,r,e.pure.bool(t.by_amount_in),e.pure.u64(t.amount),e.pure.u64(t.amount_limit),e.pure.u128(f.toString()),e.object(ai)]:[e.object(h),e.object(t.pool_id),o,r,e.pure.bool(t.by_amount_in),e.pure.u64(t.amount),e.pure.u64(t.amount_limit),e.pure.u128(f.toString()),e.object(ai)];return e.moveCall({target:`${l.published_at}::${Gt}::${P}`,typeArguments:d,arguments:M}),e}static async adjustTransactionForGas(e,t,i,o){o.setSender(e.getSenderAddress());let r=be.selectCoinAssetGreaterThanOrEqual(t,i).selected_coins,a=be.calculateTotalBalance(t);if(r.length===0&&Z("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let l=await e.FullClient.calculationTxGas(o);if(be.selectCoinAssetGreaterThanOrEqual(t,BigInt(l),r.map(d=>d.coin_object_id)).selected_coins.length===0){let d=BigInt(l)+BigInt(500);if(a-i<d){i-=d,i<0&&Z("InsufficientBalance","gas Insufficient balance");let h=new an;return{fixAmount:i,newTx:h}}}return{fixAmount:i}}static buildSwapTransaction(e,t){let i=new an;i.setSender(e.getSenderAddress());let o=be.buildCoinWithBalance(t.a2b?BigInt(t.by_amount_in?t.amount:t.amount_limit):BigInt(0),t.coin_type_a,i),r=be.buildCoinWithBalance(t.a2b?BigInt(0):BigInt(t.by_amount_in?t.amount:t.amount_limit),t.coin_type_b,i);return i=this.buildSwapTransactionArgs(i,t,e.sdkOptions,o,r),i}static async fixSwapParams(e,t,i){let{current_pool:o}=i;try{let r=await e.Swap.preSwap({decimals_a:i.decimals_a,decimals_b:i.decimals_b,a2b:t.a2b,by_amount_in:t.by_amount_in,amount:t.amount,pool:o,current_sqrt_price:o.current_sqrt_price,coin_type_a:o.coin_type_a,coin_type_b:o.coin_type_b}),a=i.by_amount_in?r.estimated_amount_out:r.estimated_amount_in,l=is(a,i.slippage,!i.by_amount_in);t.amount_limit=l.toString()}catch(r){Bt("InvalidConfig",r)}return t}static buildSwapTransactionWithoutTransferCoins(e,t){let i=new an;i.setSender(e.getSenderAddress());let o=be.buildCoinWithBalance(t.a2b?BigInt(t.by_amount_in?t.amount:t.amount_limit):BigInt(0),t.coin_type_a,i),r=be.buildCoinWithBalance(t.a2b?BigInt(0):BigInt(t.by_amount_in?t.amount:t.amount_limit),t.coin_type_b,i),a=n.buildSwapTransactionWithoutTransferCoinArgs(e,i,t,e.sdkOptions,o,r);return{tx:a.tx,coin_ab_s:a.txRes}}static buildSwapTransactionWithoutTransferCoinArgs(e,t,i,o,r,a){let{clmm_pool:l,integrate:f}=o,d=n.getDefaultSqrtPriceLimit(i.a2b),{global_config_id:h}=vo(l);h===void 0&&Z("InvalidConfig","clmm.config.global_config_id is undefined");let y=i.swap_partner!==void 0,P=y?"swap_with_partner":"swap",M=y?mo:me,S=y?[t.object(h),t.object(i.pool_id),t.object(i.swap_partner),r,a,t.pure.bool(i.a2b),t.pure.bool(i.by_amount_in),t.pure.u64(i.amount),t.pure.u128(d.toString()),t.pure.bool(!1),t.object(ai)]:[t.object(h),t.object(i.pool_id),r,a,t.pure.bool(i.a2b),t.pure.bool(i.by_amount_in),t.pure.u64(i.amount),t.pure.u128(d.toString()),t.pure.bool(!1),t.object(ai)],x=[i.coin_type_a,i.coin_type_b],U=t.moveCall({target:`${f.published_at}::${M}::${P}`,typeArguments:x,arguments:S});if(i.by_amount_in){let D=i.a2b?i.coin_type_b:i.coin_type_a,Q=i.a2b?U[1]:U[0],q=Number(i.amount_limit);Wt.checkCoinThreshold(e,i.by_amount_in,t,Q,q,D)}return{tx:t,txRes:U}}};function Pc(n,e,t){let i=e.mul(t).shln(64),o=e.shln(64).add(n.mul(t));return cn.divRoundUp(i,o)}function Cc(n,e,t){let i=e.mul(t).shln(64),o=e.shln(64).sub(n.mul(t));return cn.divRoundUp(i,o)}function Mc(n,e,t){return t.sub(cn.divRoundUp(n.shln(64),e))}function kc(n,e,t){return t.add(n.shln(64).div(e))}function Ic(n){let e=new Ft.default(n);return e.lt(Ut)?{bits:e.neg().xor(new Ft.default(2).pow(new Ft.default(64)).sub(new Ft.default(1))).add(new Ft.default(1)).toString()}:{bits:e.toString()}}function To(n){return{coin_type_a:n.coin_type_a,coin_type_b:n.coin_type_b,current_sqrt_price:new Ft.default(n.current_sqrt_price),current_tick_index:n.current_tick_index,fee_growth_global_a:new Ft.default(n.fee_growth_global_a),fee_growth_global_b:new Ft.default(n.fee_growth_global_b),fee_protocol_coin_a:new Ft.default(n.fee_protocol_coin_a),fee_protocol_coin_b:new Ft.default(n.fee_protocol_coin_b),fee_rate:new Ft.default(n.fee_rate),liquidity:new Ft.default(n.liquidity),tick_indexes:[],tick_spacing:Number(n.tick_spacing),ticks:[],collection_name:""}}function ls(n,e,t,i,o,r){if(t===Ut)return{amount_in:Ut,amount_out:Ut,next_sqrt_price:e,fee_amount:Ut};let a=n.gte(e),l,f,d,h;if(r){let y=ye.checkMulDivFloor(i,ye.checkUnsignedSub(ci,o),ci,64),P=ko(n,e,t,a);P.gt(y)?(l=y,h=ye.checkUnsignedSub(i,y),d=as(n,t,y,a)):(l=P,h=ye.checkMulDivCeil(l,o,ci.sub(o),64),d=e),f=Mo(n,d,t,a)}else{let y=Mo(n,e,t,a);y.gt(i)?(f=i,d=cs(n,t,i,a)):(f=y,d=e),l=ko(n,d,t,a),h=ye.checkMulDivCeil(l,o,ci.sub(o),64)}return{amount_in:l,amount_out:f,next_sqrt_price:d,fee_amount:h}}function xo(n,e,t,i,o){let r=t,a=i.liquidity,{current_sqrt_price:l}=i,f={amount_in:Ut,amount_out:Ut,fee_amount:Ut,ref_amount:Ut,next_sqrt_price:Ut,cross_tick_num:0},d,h,y=ue.getDefaultSqrtPriceLimit(n);for(let P of o){if(n&&i.current_tick_index<P.index||!n&&i.current_tick_index>=P.index||P===null)continue;n&&y.gt(P.sqrt_price)||!n&&y.lt(P.sqrt_price)?d=y:d=P.sqrt_price;let M=ls(l,d,a,r,i.fee_rate,e);if(M.amount_in.eq(Ut)||(r=e?r.sub(M.amount_in.add(M.fee_amount)):r.sub(M.amount_out)),f.amount_in=f.amount_in.add(M.amount_in),f.amount_out=f.amount_out.add(M.amount_out),f.fee_amount=f.fee_amount.add(M.fee_amount),M.next_sqrt_price.eq(P.sqrt_price)?(h=P.liquidity_net.mul(new Ft.default(-1)),n?ye.is_neg(h)?a=a.add(new Ft.default(Co(BigInt(h.toString()),128))):a=a.add(h):ye.is_neg(h)?a=a.sub(new Ft.default(Co(BigInt(h.toString()),128))):a=a.sub(h),l=P.sqrt_price):l=M.next_sqrt_price,f.cross_tick_num+=1,r.eq(Ut))break}return f.amount_in=f.amount_in.add(f.fee_amount),f.next_sqrt_price=l,f}var Vt=ce(oe(),1);import{asIntN as we,buildNFT as us,d as Ke,DETAILS_KEYS as ds,extractStructTagFromType as Ae,fixCoinType as So,MathUtil as fs}from"@cetusprotocol/common-sdk";function _s(n,e,t){let i=Ae(n).name,o=Ae(e).name;return`${i}-${o}[${t}]`}function li(n){let e=Ae(n.type),t=n.json,i=[];t.rewarder_manager.rewarders.forEach(l=>{let{emissions_per_second:f}=l,d=fs.fromX64(new Vt.default(f)),h=Math.floor(d.toNumber()*60*60*24);i.push({emissions_per_second:f,coin_type:Ae(l.reward_coin.name).source_address,growth_global:l.growth_global,emissions_every_day:h})});let o=t.is_pause,r={disable_add_liquidity:o,disable_remove_liquidity:o,disable_swap:o,disable_flash_loan:o,disable_collect_fee:o,disable_collect_reward:o},a={id:n.objectId,pool_type:e.source_address,coin_type_a:e.type_arguments[0],coin_type_b:e.type_arguments[1],coin_amount_a:t.coin_a,coin_amount_b:t.coin_b,current_sqrt_price:t.current_sqrt_price,current_tick_index:we(BigInt(t.current_tick_index.bits)),fee_growth_global_a:t.fee_growth_global_a,fee_growth_global_b:t.fee_growth_global_b,fee_protocol_coin_a:t.fee_protocol_coin_a,fee_protocol_coin_b:t.fee_protocol_coin_b,fee_rate:t.fee_rate,pool_status:r,liquidity:t.liquidity,position_manager:{positions_handle:t.position_manager.positions.id,size:t.position_manager.positions.size},rewarder_infos:i,rewarder_last_updated_time:t.rewarder_manager.last_updated_time,tick_spacing:t.tick_spacing,ticks_handle:t.tick_manager.ticks.id,uri:t.url,index:Number(t.index),name:""};return a.name=_s(a.coin_type_a,a.coin_type_b,a.tick_spacing),a}function ui(n){let e={creator:"",description:"",image_url:"",link:"",name:"",project_url:""},t={...e,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=n.json;if(i){let o=n.type,r=n.owner;"nft"in i?(i=i.nft.fields,e.description=i.description,e.name=i.name,e.link=i.url):e=us(i),t={...e,pos_object_id:i.id,owner:r.AddressOwner,type:o,liquidity:i.liquidity,coin_type_a:So(i.coin_type_a.name,!1),coin_type_b:So(i.coin_type_b.name,!1),tick_lower_index:we(BigInt(i.tick_lower_index.bits)),tick_upper_index:we(BigInt(i.tick_upper_index.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",name:i.name,description:i.description,image_url:i.url,link:i.link}}return t}function We(n){let e={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"};n.rewards.forEach((r,a)=>{let{amount_owned:l,growth_inside:f}=r;a===0?(e.reward_amount_owned_0=l,e.reward_growth_inside_0=f):a===1?(e.reward_amount_owned_1=l,e.reward_growth_inside_1=f):a===2&&(e.reward_amount_owned_2=l,e.reward_growth_inside_2=f)});let t=typeof n.tick_lower_index=="object"?n.tick_lower_index.bits:n.tick_lower_index,i=typeof n.tick_upper_index=="object"?n.tick_upper_index.bits:n.tick_upper_index;return{liquidity:n.liquidity,tick_lower_index:we(BigInt(t)),tick_upper_index:we(BigInt(i)),...e,fee_growth_inside_a:n.fee_growth_inside_a,fee_owned_a:n.fee_owned_a,fee_growth_inside_b:n.fee_growth_inside_b,fee_owned_b:n.fee_owned_b,pos_object_id:n.position_id}}function ln(n){let t=n.json.value.value;return{object_id:n.objectId,index:we(BigInt(t.index.bits)),sqrt_price:new Vt.default(t.sqrt_price),liquidity_net:new Vt.default(t.liquidity_net.bits),liquidity_gross:new Vt.default(t.liquidity_gross),fee_growth_outside_a:new Vt.default(t.fee_growth_outside_a),fee_growth_outside_b:new Vt.default(t.fee_growth_outside_b),rewarders_growth_outside:t.rewards_growth_outside}}function un(n){(!n||!n.index||!n.sqrt_price||!n.liquidity_net||!n.liquidity_gross||!n.fee_growth_outside_a||!n.fee_growth_outside_b)&&Z("InvalidTickFields","Invalid tick fields.",{[ds.METHOD_NAME]:"buildTickDataByEvent"});let e=we(BigInt(n.index)),t=new Vt.default(n.sqrt_price),i=new Vt.default(n.liquidity_net),o=new Vt.default(n.liquidity_gross),r=new Vt.default(n.fee_growth_outside_a),a=new Vt.default(n.fee_growth_outside_b),l=n.rewards_growth_outside||[];return{object_id:"",index:e,sqrt_price:t,liquidity_net:i,liquidity_gross:o,fee_growth_outside_a:r,fee_growth_outside_b:a,rewarders_growth_outside:l}}function Uc(n,e){return`Cetus LP | Pool${n}-${e}`}function Ao(n,e,t){let i=[],{timestampMs:o,events:r}=n,a=["AddLiquidityEvent","RemoveLiquidityEvent","CollectFeeEvent","CollectRewardEvent","CollectRewardV2Event","HarvestEvent","AddLiquidityV2Event","RemoveLiquidityV2Event"];return r?.forEach((l,f)=>{let d=Ae(l.type).name;if(a.includes(d)){let h={tx_digest:l.id.txDigest,package_id:l.packageId,transaction_module:l.transactionModule,sender:l.sender,type:l.type,timestamp_ms:o||"0",parsed_json:l.parsedJson,index:`${e}_${f}`};switch(d){case"CollectFeeEvent":t.includes(h.parsed_json.position)&&(Ke(h.parsed_json.amount_a).gt(0)||Ke(h.parsed_json.amount_b).gt(0))&&i.push(h);break;case"RemoveLiquidityEvent":case"AddLiquidityEvent":case"AddLiquidityV2Event":case"RemoveLiquidityV2Event":(Ke(h.parsed_json.amount_a).gt(0)||Ke(h.parsed_json.amount_b).gt(0))&&i.push(h);break;case"CollectRewardEvent":case"HarvestEvent":case"CollectRewardV2Event":(t.includes(h.parsed_json.position)||t.includes(h.parsed_json.wrapped_position_id))&&Ke(h.parsed_json.amount).gt(0)&&i.push(h);break;default:break}}}),i}function Io(n,e,t,i){let o=[];return n.effects.events.nodes?.forEach((a,l)=>{let f=a.timestamp,{json:d,type:h}=a.contents,{name:y,address:P}=Ae(h.repr);if(ho.includes(y)&&t.includes(P)&&i===d.pool){let M={tx:n.digest,sender:a.sender.address,type:h.repr,block_time:new Date(f).getTime().toString(),index:`${e}_${l}`,parsed_json:d};o.push(M)}}),o}function di(n,e,t,i){e.moveCall({target:`${n.sdkOptions.integrate.published_at}::${go}::transfer_coin_to_sender`,typeArguments:[i],arguments:[t]})}function Ro(n,e,t,i,o){o!=null?e.transferObjects([t],e.pure.address(o)):di(n,e,t,i)}import{asUintN as Ee,buildSuiGrpcClient as vs,buildSuiJsonRpcClient as Ps,CACHE_TIME_24H as _i,ClmmPoolUtil as Cs,CLOCK_ADDRESS as Go,CoinAssist as pi,createFullClient as Ms,d as je,DETAILS_KEYS as qt,extractStructTagFromType as Ze,getPackagerConfigs as hi,isSortedSymbols as Qo,TickMath as fn,tickScore as ks,TickUtil as Xo}from"@cetusprotocol/common-sdk";import{asIntN as Eo,d as jo,extractStructTagFromType as ps,fixCoinType as Fo}from"@cetusprotocol/common-sdk";var hs=1e4,Xt=class{static parseClmmVestInfo(e){let t=e?.json,i=e.type,o=ps(i),r=t.global_vesting_periods.map(l=>({period:l.period,release_time:l.release_time,redeemed_amount:l.redeemed_amount,percentage:jo(l.percentage).div(hs).toNumber()}));return{id:t.id,balance:t.balance,global_vesting_periods:r,total_value:t.total_value,total_cetus_amount:t.total_cetus_amount,redeemed_amount:t.redeemed_amount,start_time:t.start_time,type:o.full_address,positions:{id:t.positions.id,size:t.positions.size}}}static parsePositionVesting(e){return{position_id:e.position_id,cetus_amount:e.cetus_amount,redeemed_amount:e.redeemed_amount,is_paused:e.is_paused,impaired_a:e.impaired_a,impaired_b:e.impaired_b,period_details:e.period_details,coin_type_a:Fo(e.coin_a.name,!1),coin_type_b:Fo(e.coin_b.name,!1)}}static parsePoolLiquiditySnapshot(e){let t=e.json;return{current_sqrt_price:t.current_sqrt_price,remove_percent:jo(t.remove_percent).div(1e6).toString(),snapshots:{id:t.snapshots.id,size:t.snapshots.size}}}static parsePositionSnapshot(e){let t=e.json,i=t.value.value;return{position_id:t.name,liquidity:i.liquidity,tick_lower_index:Eo(BigInt(i.tick_lower_index.bits)),tick_upper_index:Eo(BigInt(i.tick_upper_index.bits)),fee_owned_a:i.fee_owned_a,fee_owned_b:i.fee_owned_b,value_cut:i.value_cut,rewards:i.rewards}}};import{FullRpcUrlMainnet as ms,GraphRpcUrlMainnet as gs}from"@cetusprotocol/common-sdk";var No={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"}},Bo={env:"mainnet",full_rpc_url:ms,graph_rpc_url:gs,cetus_config:{package_id:"0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f",published_at:"0xba7e740c3c002673dbe69ad5fbdb0691ec260170e141297cefb982e7081fde52",version:2,config:No.cetusConfig},clmm_pool:{package_id:"0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb",published_at:"0x25ebb9a7c50eb17b3fa9c5a30fb8b5ad8f97caaf4928943acbcff7153dfee5e3",version:14,config:No.clmmConfig},integrate:{package_id:"0x996c4d9480708fb8b92aa7acf819fb0497b5ec8e65ba06601cae2fb6db3312c3",published_at:"0xae9c208cf58fd5ba36737c9ee5dcfa7f152d0fb5a5a99eebb7c881ebc2fe59e0",version:16},stats_pools_url:"https://api-sui.cetus.zone/v2/sui/stats_pools",clmm_vest:{package_id:"0x9d2f067d3b9d19ac0f8d2e5c2c393b1760232083e42005b2e5df39c06064d522",published_at:"0x9d2f067d3b9d19ac0f8d2e5c2c393b1760232083e42005b2e5df39c06064d522",version:1,config:{clmm_vest_id:"0xe255c47472470c03bbefb1fc883459c2b978d3ad29aa8ee0c8c1ec9753fa7d01",versioned_id:"0x4f6f2f638362505836114f313809b834dafd58e3910df5110f6e54e4e35c929b",cetus_coin_type:"0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS"}}},Oo=["0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb","0xdb5cd62a06c79695bfc9982eb08534706d3752fe123b48e0144f480209b3117f"];import{FullRpcUrlTestnet as bs,GraphRpcUrlTestnet as ys}from"@cetusprotocol/common-sdk";var qo={env:"testnet",full_rpc_url:bs,graph_rpc_url:ys,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:"0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8",published_at:"0x6bbdf09f9fa0baa1524080a5b8991042e95061c4e1206217279aec51ba08edf7",config:{pools_id:"0x20a086e6fa0741b3ca77d033a65faf0871349b986ddbdde6fa1d85d78a5f4222",global_config_id:"0xc6273f844b4bc258952c4e477697aa12c918c8e08106fac6b934811298c9820a",global_vault_id:"0x71e74a999dd7959e483f758ddf573e85fa4c24944db33ff6763c9d85a9c045fe",admin_cap_id:"0xbf4c48590f403c38351de0e8aa13d6d91bf78fd8c04e93ac1d0269c44d70ae02",partners_id:"0xb5ae5ed3f403654ae1307aadc0140f746db41efb7bda92235257c84d90a1397e"}},integrate:{package_id:"0xab2d58dd28ff0dc19b18ab2c634397b785a38c342a8f5065ade5f53f9dbffa1c",published_at:"0xab2d58dd28ff0dc19b18ab2c634397b785a38c342a8f5065ade5f53f9dbffa1c",version:1},stats_pools_url:"https://api-sui.devcetus.com/v2/sui/stats_pools",clmm_vest:{package_id:"0xa46d9c66e7b24ab14c5fc5f0d08fa257d833718f0295a6343556ea2f2fdfbd7f",published_at:"0xa46d9c66e7b24ab14c5fc5f0d08fa257d833718f0295a6343556ea2f2fdfbd7f",config:{clmm_vest_id:"0x308b24963e5992f699e32db2f7088b812566a0cae580317fd3b8bf61de7f5508",versioned_id:"0x1cfb684d8ff581416a56caba2aa419bee45fe98a23cbf28e2c6c1021b14cab7c",cetus_coin_type:"0xc6c51938da9a5cf6d6dca692783ea7bdf4478f7b1fef693f58947848f84bcf89::cetus::CETUS"}}},Do=["0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8","0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8"];import{bcs as ze}from"@mysten/sui/bcs";import{TypeNameRaw as Lo}from"@cetusprotocol/common-sdk";import{bcs as A}from"@mysten/sui/bcs";var ws=A.struct("PositionReward",{growth_inside:A.u128(),amount_owned:A.u64()}),Uo=A.struct("PositionInfo",{position_id:A.Address,liquidity:A.u128(),tick_lower_index:A.u32(),tick_upper_index:A.u32(),fee_growth_inside_a:A.u128(),fee_growth_inside_b:A.u128(),fee_owned_a:A.u64(),fee_owned_b:A.u64(),points_owned:A.u128(),points_growth_inside:A.u128(),rewards:A.vector(ws)}),$o=A.bytes(32),Vo=A.struct("Node<ID, PositionInfo>",{prev:A.option($o),next:A.option($o),value:Uo}),Ko=A.struct("FetchPositionsEvent",{positions:A.vector(Uo)}),dn=A.struct("Tick",{index:A.u32(),sqrt_price:A.u128(),liquidity_net:A.u128(),liquidity_gross:A.u128(),fee_growth_outside_a:A.u128(),fee_growth_outside_b:A.u128(),points_growth_outside:A.u128(),rewards_growth_outside:A.vector(A.u128())}),Wo=A.struct("FetchTicksResultEvent",{ticks:A.vector(dn)}),Zo=A.struct("FetchPositionRewardsEvent",{data:A.vector(A.u64()),position_id:A.Address}),zo=A.struct("FetchPositionFeesEvent",{position_id:A.Address,fee_owned_a:A.u64(),fee_owned_b:A.u64()}),fi=A.struct("CalculatedSwapResultEventData",{data:A.struct("FetchPositionFeesEvent",{amount_in:A.u64(),amount_out:A.u64(),fee_amount:A.u64(),fee_rate:A.u64(),after_sqrt_price:A.u128(),is_exceed:A.bool()})}),Ho=A.struct("GetPositionsVestingEventData",{data:A.vector(A.struct("PositionVesting",{position_id:A.Address,cetus_amount:A.u64(),redeemed_amount:A.u64(),coin_a:Lo,coin_b:Lo,impaired_a:A.u64(),impaired_b:A.u64(),period_details:A.vector(A.struct("PeriodDetail",{period:A.u64(),cetus_amount:A.u64(),is_redeemed:A.bool()})),is_paused:A.bool()}))});var mi=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getPositionList(e,t="all"){let i={data:[],has_next_page:!0},o=await this._sdk.FullClient.getDynamicFieldsByPage(e,t);i.has_next_page=o.has_next_page,i.next_cursor=o.next_cursor;let r=o.data.map(l=>(l.error!=null&&Z("InvalidConfig",`when getPositionList get position objects error: ${l.error}, please check the rpc, contracts address config and position id.`,{[qt.METHOD_NAME]:"getPositionList"}),ze.Address.parse(l.name.bcs))),a=await this._sdk.Position.getSimplePositionList(r);return i.data=a,i}async getPoolImmutablesWithPage(e="all",t=!1){let{package_id:i}=this._sdk.sdkOptions.clmm_pool,o=[],r={data:[],has_next_page:!1},a=e==="all",l=`${i}_getPoolImmutables`;if(a){let f=this._sdk.getCache(l,t);f&&o.push(...f)}if(o.length===0)try{let f=`${i}::factory::CreatePoolEvent`,d=await this._sdk.FullClient.queryEventsByPage({MoveEventType:f},e);r.has_next_page=d.has_next_page,r.next_cursor=d.next_cursor,d.data.forEach(h=>{let y=h.parsedJson;y&&o.push({id:y.pool_id,tick_spacing:y.tick_spacing,coin_type_a:Ze(y.coin_type_a).full_address,coin_type_b:Ze(y.coin_type_b).full_address})})}catch(f){return Bt("FetchError",f,{[qt.METHOD_NAME]:"getPoolImmutables"})}return r.data=o,a&&this._sdk.updateCache(`${i}_getPoolImmutables`,o,_i),r}async getPoolsWithPage(e="all",t=!1){let i={data:[],has_next_page:!1},o=await this.getPoolImmutablesWithPage(e,t),r=await this._sdk.FullClient.batchGetObjects(o.data.map(a=>a.id),{json:!0});for(let a of r){a instanceof Error&&Z("InvalidPoolObject",`getPoolWithPages error code: ${a??"unknown error"}, please check config and object ids`,{[qt.METHOD_NAME]:"getPoolsWithPage"});let l=li(a);i.data.push(l);let f=`${l.id}_getPoolObject`;this._sdk.updateCache(f,l,_i)}return i.has_next_page=o.has_next_page,i.next_cursor=o.next_cursor,i}async getPoolLiquiditySnapshot(e,t=!1){try{let i=ze.String.serialize(_o).toBytes(),o=await this._sdk.FullClient.getDynamicField({parentId:e,name:{type:"0x2::dynamic_object_field::Wrapper<0x1::string::String>",bcs:i}}),r=ze.Address.parse(o.dynamicField.value.bcs),a=await this._sdk.FullClient.getObject({objectId:r,include:{json:!0}}),l=Xt.parsePoolLiquiditySnapshot(a.object);if(t){let d=(await this._sdk.FullClient.getDynamicFieldsByPage(l.snapshots.id)).data.map(h=>h.fieldId);if(d.length>0){let h=await this._sdk.FullClient.batchGetObjects(d,{json:!0}),y=[];h.forEach(P=>{let M=Xt.parsePositionSnapshot(P);y.push(M)}),l.position_snapshots=y}}return l}catch(i){return Bt("InvalidPoolObject",i,{[qt.METHOD_NAME]:"getPoolLiquiditySnapshot",[qt.REQUEST_PARAMS]:e})}}async getPositionSnapshot(e,t){let i=await this._sdk.FullClient.getDynamicFieldObjects(e,t,"0x2::object::ID","address",{json:!0}),o=[];return i.forEach(r=>{try{let a=Xt.parsePositionSnapshot(r);o.push(a)}catch(a){console.log("getPositionSnapshot error: ",a)}}),o}async getAssignPools(e){if(e.length===0)return[];let t=[],i=await this._sdk.FullClient.batchGetObjects(e,{json:!0});for(let o of i){let r=li(o);t.push(r);let a=`${r.id}_getPoolObject`;this._sdk.updateCache(a,r,_i)}return t}async getPool(e,t=!0,i=!1){let o=`${e}_getPoolObject`,r=this._sdk.getCache(o,t);if(r!==void 0)return r;let a=await this._sdk.FullClient.getObject({objectId:e,include:{type:!0,json:!0}}),l=li(a.object);if(i){let f=await this.getPoolStatus(e);f&&(l.pool_status=f)}return this._sdk.updateCache(o,l),l}async getPoolByCoins(e,t){if(e.length===0)return[];for(let f=0;f<e.length;f++)e[f]==="0x2::sui::SUI"&&(e[f]="0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI");let i=this._sdk.sdkOptions.stats_pools_url;i||Z("StatsPoolsUrlNotSet","statsPoolsUrl is not set in the sdk options.",{[qt.METHOD_NAME]:"getPoolByCoins"}),i+=`?order_by=-fees&limit=100&has_mining=true&has_farming=true&no_incentives=true&display_all_pools=true&coin_type=${e.join(",")}`;let o=await fetch(i),r;try{r=await o.json()}catch(f){Bt("FetchError",f,{[qt.METHOD_NAME]:"getPoolByCoins",[qt.REQUEST_PARAMS]:i})}let a=r.data.lp_list,l=[];for(let f of a)e.includes(f.coin_a_address)&&e.includes(f.coin_b_address)&&(t!=null?f.object.feeRate===t&&l.push(f.address):l.push(f.address));return l.length>0?await this.getAssignPools(l):[]}async createPoolPayload(e,t){if(Qo(Re(e.coin_type_a),Re(e.coin_type_b))){let i=e.coin_type_b;e.coin_type_b=e.coin_type_a,e.coin_type_a=i}return await this.createPoolAndAddLiquidity(e,t)}async createPoolRowPayload(e,t){if(Qo(Re(e.coin_type_a),Re(e.coin_type_b))){let i=e.coin_type_b;e.coin_type_b=e.coin_type_a,e.coin_type_a=i}return await this.createPoolAndAddLiquidityRow(e,t)}async getClmmConfigs(e=!1){let{package_id:t}=this._sdk.sdkOptions.clmm_pool,i=`${t}_getInitEvent`,o=this._sdk.getCache(i,e);if(o!==void 0)return o;let a=(await this._sdk.FullClient.getObject({objectId:t,include:{previousTransaction:!0}})).object?.previousTransaction,l=(await this._sdk.FullClient.queryEventsByPage({Transaction:a})).data,f={pools_id:"",global_config_id:"",global_vault_id:"",admin_cap_id:""};return l.length>0&&(l.forEach(d=>{let h=d.parsedJson;if(d.type)switch(Ze(d.type).full_address){case`${t}::config::InitConfigEvent`:f.global_config_id=h.global_config_id,f.admin_cap_id=h.admin_cap_id;break;case`${t}::factory::InitFactoryEvent`:f.pools_id=h.pools_id;break;case`${t}::rewarder::RewarderInitEvent`:f.global_vault_id=h.global_vault_id;break;case`${t}::partner::InitPartnerEvent`:f.partners_id=h.partners_id;break;default:break}}),this._sdk.updateCache(i,f,_i)),f}async getPoolTransactionList({pool_id:e,pagination_args:t,full_rpc_url:i}){let{FullClient:o,sdkOptions:r}=this._sdk,a;i?a=Ms(vs(i,this._sdk.sdkOptions.env),o._graphQLClient,this._sdk.sdkOptions.env,Ps(i,this._sdk.sdkOptions.env)):a=o;let l={data:[],has_next_page:!1},f=50,d=t,h=t.limit||10,y=r.env==="testnet"?Do:Oo;do{let P=await a.queryTransactionBlocksByPage({affectedObject:e},{...d,limit:50});P.data.forEach((M,S)=>{l.next_cursor=P.next_cursor;let x=Io(M,S,y,e);l.data=[...l.data,...x]}),l.has_next_page=P.has_next_page,l.next_cursor=P.next_cursor,d.cursor=P.next_cursor}while(l.data.length<h&&l.has_next_page);return l.data.length>h&&(l.data=l.data.slice(0,h),l.next_cursor=l.data[l.data.length-1].tx),l}async calculateCreatePoolWithPrice(e){let{current_price:t,slippage:i,tick_spacing:o,add_mode_params:r,price_base_coin:a,coin_decimals_a:l,coin_decimals_b:f}=e,d=fn.priceToSqrtPriceX64(a==="coin_a"?je(t):je(1).div(t),l,f),h=0,y=0;if(r.is_full_range)h=Xo.getMinIndex(o),y=Xo.getMaxIndex(o);else{let{min_price:q,max_price:L}=r;h=fn.priceToInitializeTickIndex(a==="coin_a"?je(q):je(1).div(L),l,f,o),y=fn.priceToInitializeTickIndex(a==="coin_a"?je(L):je(1).div(q),l,f,o)}let{coin_amount:P,fix_amount_a:M}=e,{coin_amount_limit_a:S,coin_amount_limit_b:x,liquidity_amount:U,coin_amount_a:D,coin_amount_b:Q}=Cs.estLiquidityAndCoinAmountFromOneAmounts(h,y,new Jo.default(P),M,!0,i,d);return{coin_amount_a:D,coin_amount_b:Q,coin_amount_limit_a:S,coin_amount_limit_b:x,liquidity:U,initialize_sqrt_price:d.toString(),tick_lower:h,tick_upper:y,fix_amount_a:M}}createPoolAndAddLiquidity(e,t){t=t||new Ie,t.setSender(this.sdk.getSenderAddress());let{integrate:i,clmm_pool:o}=this.sdk.sdkOptions,r=hi(o),a=r.global_config_id,l=r.pools_id,f=pi.buildCoinWithBalance(BigInt(e.amount_a),e.coin_type_a,t),d=pi.buildCoinWithBalance(BigInt(e.amount_b),e.coin_type_b,t),h=[t.object(a),t.object(l),t.pure.u32(e.tick_spacing),t.pure.u128(e.initialize_sqrt_price),t.pure.string(e.uri),t.pure.u32(Number(Ee(BigInt(e.tick_lower)).toString())),t.pure.u32(Number(Ee(BigInt(e.tick_upper)).toString())),f,d,t.pure.bool(e.fix_amount_a),t.object(Go)];return t.moveCall({target:`${i.published_at}::pool_creator_v3::create_pool_v3`,typeArguments:[e.coin_type_a,e.coin_type_b],arguments:h}),di(this._sdk,t,f,e.coin_type_a),di(this._sdk,t,d,e.coin_type_b),t}createPoolAndAddLiquidityRow(e,t){t=t||new Ie;let{clmm_pool:i}=this.sdk.sdkOptions,o=hi(i),r=o.global_config_id,a=o.pools_id,l=pi.buildCoinWithBalance(BigInt(e.amount_a),e.coin_type_a,t),f=pi.buildCoinWithBalance(BigInt(e.amount_b),e.coin_type_b,t),d=[t.object(r),t.object(a),t.pure.u32(e.tick_spacing),t.pure.u128(e.initialize_sqrt_price),t.pure.string(e.uri),t.pure.u32(Number(Ee(BigInt(e.tick_lower)).toString())),t.pure.u32(Number(Ee(BigInt(e.tick_upper)).toString())),l,f,t.pure.bool(e.fix_amount_a),t.object(Go)],h=t.moveCall({target:`${i.published_at}::pool_creator::create_pool_v3`,typeArguments:[e.coin_type_a,e.coin_type_b],arguments:d});return{tx:t,pos_id:h[0],remain_coin_a:h[1],remain_coin_b:h[2],remain_coin_type_a:e.coin_type_a,remain_coin_type_b:e.coin_type_b}}async createPoolWithPriceReturnPositionPayload(e,t){let{coin_type_a:i,coin_type_b:o,tick_spacing:r,uri:a,calculate_result:l}=e,{initialize_sqrt_price:f,tick_lower:d,tick_upper:h,liquidity:y,coin_amount_a:P,coin_amount_b:M,fix_amount_a:S,coin_amount_limit_a:x,coin_amount_limit_b:U}=l;return this.createPoolRowPayload({tick_spacing:r,initialize_sqrt_price:f,uri:a||"",coin_type_a:i,coin_type_b:o,amount_a:S?P:x,amount_b:S?U:M,fix_amount_a:S,tick_lower:d,tick_upper:h},t)}async createPoolWithPricePayload(e){let{coin_type_a:t,coin_type_b:i,tick_spacing:o,uri:r,calculate_result:a}=e,{initialize_sqrt_price:l,tick_lower:f,tick_upper:d,liquidity:h,coin_amount_a:y,coin_amount_b:P,fix_amount_a:M,coin_amount_limit_a:S,coin_amount_limit_b:x}=a;return this.createPoolPayload({tick_spacing:o,initialize_sqrt_price:l,uri:r||"",coin_type_a:t,coin_type_b:i,amount_a:M?y:S,amount_b:M?x:P,fix_amount_a:M,tick_lower:f,tick_upper:d})}async fetchTicks(e){let t=[],i=[];for(;;){let r=await this.getTicks({pool_id:e.pool_id,coin_type_a:e.coin_type_a,coin_type_b:e.coin_type_b,start:i,limit:512});if(t=[...t,...r],r.length<512)break;i=[Number(Ee(BigInt(r[r.length-1].index)))]}return t}async getTicks(e){let{integrate:t}=this.sdk.sdkOptions,i=[],o=[e.coin_type_a,e.coin_type_b],r=new Ie,a=r.makeMoveVec({elements:e.start.map(d=>r.pure.u32(d)),type:"u32"}),l=[r.object(e.pool_id),a,r.pure.u64(e.limit.toString())];return r.moveCall({target:`${t.published_at}::${Qt}::fetch_ticks`,arguments:l,typeArguments:o}),(await this.sdk.FullClient.sendSimulationTransaction(r,Re("0x0"))).Transaction?.events?.forEach(d=>{Ze(d.eventType).name==="FetchTicksResultEvent"&&Wo.parse(d.bcs).ticks.forEach(y=>{i.push(un(y))})}),i}async fetchPoolPositionInfoList(e){let{integrate:t}=this.sdk.sdkOptions,i=[],o=[],r=512;for(;;){let a=[e.coin_type_a,e.coin_type_b],l=new Ie,f=l.pure.vector("id",o.map(P=>P)),d=[l.object(e.pool_id),f,l.pure.u64(r)];l.moveCall({target:`${t.published_at}::${Qt}::fetch_positions`,arguments:d,typeArguments:a});let h=await this.sdk.FullClient.sendSimulationTransaction(l,Re("0x0"));h.FailedTransaction!=null&&Z("InvalidPositionRewardObject",`fetch position info error code: ${h.FailedTransaction??"unknown error"}, please check config and tick object ids`,{[qt.METHOD_NAME]:"fetchPoolPositionInfoList",[qt.REQUEST_PARAMS]:e});let y=[];if(h?.Transaction?.events?.forEach(P=>{Ze(P.eventType).name==="FetchPositionsEvent"&&Ko.parse(P.bcs).positions.forEach(S=>{let x=We(S);y.push(x)})}),i.push(...y),y.length<r)break;o=[y[y.length-1].pos_object_id]}return i}async fetchTicksByRpc(e){let t=[],i=null,o=50;for(;;){let r=[],a=await this.sdk.FullClient.listDynamicFields({parentId:e,cursor:i,limit:o});if(i=a.cursor,a.dynamicFields.forEach(l=>{r.push(l.fieldId)}),t=[...t,...await this.getTicksByRpc(r)],!a.hasNextPage)break}return t}async getTicksByRpc(e){let t=[],i=await this.sdk.FullClient.batchGetObjects(e,{json:!0});for(let o of i){let r=ln(o);r!=null&&t.push(r)}return t}async getTickDataByIndex(e,t){try{let i={type:"u64",bcs:ze.u64().serialize(Ee(BigInt(ks(t).toString()))).toBytes()},o=await this.sdk.FullClient.getDynamicField({parentId:e,name:i}),r=dn.parse(o.dynamicField.value.bcs);return un(r)}catch{return Z("InvalidTickFields","Invalid tick fields.",{[qt.METHOD_NAME]:"getTickDataByIndex"})}}async getTickDataByObjectId(e){let t=await this.sdk.FullClient.getObject({objectId:e,include:{json:!0}});return ln(t.object)}async getPartnerRefFeeAmount(e){let t=await this._sdk.FullClient.getObject({objectId:e,include:{json:!0}});t instanceof Error&&Z("NotFoundPartnerObject",`get partner by object id: ${e} error: ${t.message}`,{[qt.METHOD_NAME]:"getPartnerRefFeeAmount"});let i=t.object.json.balances.id,o=await this._sdk.FullClient.getDynamicFieldsByPage(i),r=[];o.data.forEach(f=>{f.fieldId!=null&&r.push(f.fieldId)});let a=[];return(await this._sdk.FullClient.batchGetObjects(r,{json:!0})).forEach(f=>{let d={coin_type:f.json.name,coin_object_id:f.json.id,balance:BigInt(f.json.value)};a.push(d)}),a}async claimPartnerRefFeePayload(e,t,i){let o=new Ie,{clmm_pool:r}=this.sdk.sdkOptions,{global_config_id:a}=hi(r),l=[i],f=[o.object(a),o.object(e),o.object(t)];return o.moveCall({target:`${r.published_at}::${sn}::claim_ref_fee`,arguments:f,typeArguments:l}),o}async getPoolStatus(e){try{let o=((await this._sdk.FullClient.getDynamicField({parentId:e,name:{type:"0x2::dynamic_object_field::Wrapper<0x1::string::String>",bcs:ze.String.serialize("pool_status").toBytes()}})).data?.json).position.status;if(o)return o}catch(t){console.log("\u{1F680} ~ file: poolModule.ts:1093 ~ PoolModule ~ getPoolStatus ~ error:",t)}}async claimAllPartnerRefFeesPayload(e,t,i){try{let{clmm_pool:o}=this._sdk.sdkOptions,{global_config_id:r}=hi(o),a=await this.getPartnerRefFeeAmount(e),l=new Ie;if(a.length===0)return{ref_fees:[],tx:l};for(let f of a){let d=f.coin_type;f.balance!==BigInt(0)&&(i&&i.length>0&&!i.includes(d)||l.moveCall({target:`${o.published_at}::${sn}::claim_ref_fee`,arguments:[l.object(r),l.object(t),l.object(e)],typeArguments:[d]}))}return{ref_fees:a,tx:l}}catch(o){return Bt("InvalidPartnerRefFeeFields",o,{[qt.METHOD_NAME]:"claimAllPartnerRefFeesPayload",[qt.REQUEST_PARAMS]:{partner:e,partner_cap:t,specific_coin_types:i}})}}};var Qe=ce(oe(),1);import{Transaction as se}from"@mysten/sui/transactions";import{isValidSuiObjectId as ya,normalizeSuiAddress as wa}from"@mysten/sui/utils";import{asUintN as Be,ClmmPoolUtil as _r,CLOCK_ADDRESS as ki,CoinAssist as Oe,d as te,DETAILS_KEYS as ae,extractStructTagFromType as va,getPackagerConfigs as ee,TickMath as qe,TickUtil as Ti,createFullClient as Pa,deriveDynamicFieldIdByType as Ca,buildSuiGrpcClient as Ma,buildSuiJsonRpcClient as ka}from"@cetusprotocol/common-sdk";var Fe=9e15,pe=1e9,_n="0123456789abcdef",yi="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",wi="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",pn={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Fe,maxE:Fe,crypto:!1},ir,re,B=!0,Pi="[DecimalError] ",_e=Pi+"Invalid argument: ",nr=Pi+"Precision limit exceeded",or=Pi+"crypto unavailable",rr="[object Decimal]",Nt=Math.floor,Rt=Math.pow,Ts=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,xs=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Ss=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,sr=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,Zt=1e7,F=7,As=9007199254740991,Is=yi.length-1,hn=wi.length-1,k={toStringTag:rr};k.absoluteValue=k.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),E(n)};k.ceil=function(){return E(new this.constructor(this),this.e+1,2)};k.clampedTo=k.clamp=function(n,e){var t,i=this,o=i.constructor;if(n=new o(n),e=new o(e),!n.s||!e.s)return new o(NaN);if(n.gt(e))throw Error(_e+e);return t=i.cmp(n),t<0?n:i.cmp(e)>0?e:new o(i)};k.comparedTo=k.cmp=function(n){var e,t,i,o,r=this,a=r.d,l=(n=new r.constructor(n)).d,f=r.s,d=n.s;if(!a||!l)return!f||!d?NaN:f!==d?f:a===l?0:!a^f<0?1:-1;if(!a[0]||!l[0])return a[0]?f:l[0]?-d:0;if(f!==d)return f;if(r.e!==n.e)return r.e>n.e^f<0?1:-1;for(i=a.length,o=l.length,e=0,t=i<o?i:o;e<t;++e)if(a[e]!==l[e])return a[e]>l[e]^f<0?1:-1;return i===o?0:i>o^f<0?1:-1};k.cosine=k.cos=function(){var n,e,t=this,i=t.constructor;return t.d?t.d[0]?(n=i.precision,e=i.rounding,i.precision=n+Math.max(t.e,t.sd())+F,i.rounding=1,t=Rs(i,dr(i,t)),i.precision=n,i.rounding=e,E(re==2||re==3?t.neg():t,n,e,!0)):new i(1):new i(NaN)};k.cubeRoot=k.cbrt=function(){var n,e,t,i,o,r,a,l,f,d,h=this,y=h.constructor;if(!h.isFinite()||h.isZero())return new y(h);for(B=!1,r=h.s*Rt(h.s*h,1/3),!r||Math.abs(r)==1/0?(t=jt(h.d),n=h.e,(r=(n-t.length+1)%3)&&(t+=r==1||r==-2?"0":"00"),r=Rt(t,1/3),n=Nt((n+1)/3)-(n%3==(n<0?-1:2)),r==1/0?t="5e"+n:(t=r.toExponential(),t=t.slice(0,t.indexOf("e")+1)+n),i=new y(t),i.s=h.s):i=new y(r.toString()),a=(n=y.precision)+3;;)if(l=i,f=l.times(l).times(l),d=f.plus(h),i=z(d.plus(h).times(l),d.plus(f),a+2,1),jt(l.d).slice(0,a)===(t=jt(i.d)).slice(0,a))if(t=t.slice(a-3,a+1),t=="9999"||!o&&t=="4999"){if(!o&&(E(l,n+1,0),l.times(l).times(l).eq(h))){i=l;break}a+=4,o=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(E(i,n+1,1),e=!i.times(i).times(i).eq(h));break}return B=!0,E(i,n,y.rounding,e)};k.decimalPlaces=k.dp=function(){var n,e=this.d,t=NaN;if(e){if(n=e.length-1,t=(n-Nt(this.e/F))*F,n=e[n],n)for(;n%10==0;n/=10)t--;t<0&&(t=0)}return t};k.dividedBy=k.div=function(n){return z(this,new this.constructor(n))};k.dividedToIntegerBy=k.divToInt=function(n){var e=this,t=e.constructor;return E(z(e,new t(n),0,1,1),t.precision,t.rounding)};k.equals=k.eq=function(n){return this.cmp(n)===0};k.floor=function(){return E(new this.constructor(this),this.e+1,3)};k.greaterThan=k.gt=function(n){return this.cmp(n)>0};k.greaterThanOrEqualTo=k.gte=function(n){var e=this.cmp(n);return e==1||e===0};k.hyperbolicCosine=k.cosh=function(){var n,e,t,i,o,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;t=a.precision,i=a.rounding,a.precision=t+Math.max(r.e,r.sd())+4,a.rounding=1,o=r.d.length,o<32?(n=Math.ceil(o/3),e=(1/Mi(4,n)).toString()):(n=16,e="2.3283064365386962890625e-10"),r=Ne(a,1,r.times(e),new a(1),!0);for(var f,d=n,h=new a(8);d--;)f=r.times(r),r=l.minus(f.times(h.minus(f.times(h))));return E(r,a.precision=t,a.rounding=i,!0)};k.hyperbolicSine=k.sinh=function(){var n,e,t,i,o=this,r=o.constructor;if(!o.isFinite()||o.isZero())return new r(o);if(e=r.precision,t=r.rounding,r.precision=e+Math.max(o.e,o.sd())+4,r.rounding=1,i=o.d.length,i<3)o=Ne(r,2,o,o,!0);else{n=1.4*Math.sqrt(i),n=n>16?16:n|0,o=o.times(1/Mi(5,n)),o=Ne(r,2,o,o,!0);for(var a,l=new r(5),f=new r(16),d=new r(20);n--;)a=o.times(o),o=o.times(l.plus(a.times(f.times(a).plus(d))))}return r.precision=e,r.rounding=t,E(o,e,t,!0)};k.hyperbolicTangent=k.tanh=function(){var n,e,t=this,i=t.constructor;return t.isFinite()?t.isZero()?new i(t):(n=i.precision,e=i.rounding,i.precision=n+7,i.rounding=1,z(t.sinh(),t.cosh(),i.precision=n,i.rounding=e)):new i(t.s)};k.inverseCosine=k.acos=function(){var n=this,e=n.constructor,t=n.abs().cmp(1),i=e.precision,o=e.rounding;return t!==-1?t===0?n.isNeg()?Jt(e,i,o):new e(0):new e(NaN):n.isZero()?Jt(e,i+4,o).times(.5):(e.precision=i+6,e.rounding=1,n=new e(1).minus(n).div(n.plus(1)).sqrt().atan(),e.precision=i,e.rounding=o,n.times(2))};k.inverseHyperbolicCosine=k.acosh=function(){var n,e,t=this,i=t.constructor;return t.lte(1)?new i(t.eq(1)?0:NaN):t.isFinite()?(n=i.precision,e=i.rounding,i.precision=n+Math.max(Math.abs(t.e),t.sd())+4,i.rounding=1,B=!1,t=t.times(t).minus(1).sqrt().plus(t),B=!0,i.precision=n,i.rounding=e,t.ln()):new i(t)};k.inverseHyperbolicSine=k.asinh=function(){var n,e,t=this,i=t.constructor;return!t.isFinite()||t.isZero()?new i(t):(n=i.precision,e=i.rounding,i.precision=n+2*Math.max(Math.abs(t.e),t.sd())+6,i.rounding=1,B=!1,t=t.times(t).plus(1).sqrt().plus(t),B=!0,i.precision=n,i.rounding=e,t.ln())};k.inverseHyperbolicTangent=k.atanh=function(){var n,e,t,i,o=this,r=o.constructor;return o.isFinite()?o.e>=0?new r(o.abs().eq(1)?o.s/0:o.isZero()?o:NaN):(n=r.precision,e=r.rounding,i=o.sd(),Math.max(i,n)<2*-o.e-1?E(new r(o),n,e,!0):(r.precision=t=i-o.e,o=z(o.plus(1),new r(1).minus(o),t+n,1),r.precision=n+4,r.rounding=1,o=o.ln(),r.precision=n,r.rounding=e,o.times(.5))):new r(NaN)};k.inverseSine=k.asin=function(){var n,e,t,i,o=this,r=o.constructor;return o.isZero()?new r(o):(e=o.abs().cmp(1),t=r.precision,i=r.rounding,e!==-1?e===0?(n=Jt(r,t+4,i).times(.5),n.s=o.s,n):new r(NaN):(r.precision=t+6,r.rounding=1,o=o.div(new r(1).minus(o.times(o)).sqrt().plus(1)).atan(),r.precision=t,r.rounding=i,o.times(2)))};k.inverseTangent=k.atan=function(){var n,e,t,i,o,r,a,l,f,d=this,h=d.constructor,y=h.precision,P=h.rounding;if(d.isFinite()){if(d.isZero())return new h(d);if(d.abs().eq(1)&&y+4<=hn)return a=Jt(h,y+4,P).times(.25),a.s=d.s,a}else{if(!d.s)return new h(NaN);if(y+4<=hn)return a=Jt(h,y+4,P).times(.5),a.s=d.s,a}for(h.precision=l=y+10,h.rounding=1,t=Math.min(28,l/F+2|0),n=t;n;--n)d=d.div(d.times(d).plus(1).sqrt().plus(1));for(B=!1,e=Math.ceil(l/F),i=1,f=d.times(d),a=new h(d),o=d;n!==-1;)if(o=o.times(f),r=a.minus(o.div(i+=2)),o=o.times(f),a=r.plus(o.div(i+=2)),a.d[e]!==void 0)for(n=e;a.d[n]===r.d[n]&&n--;);return t&&(a=a.times(2<<t-1)),B=!0,E(a,h.precision=y,h.rounding=P,!0)};k.isFinite=function(){return!!this.d};k.isInteger=k.isInt=function(){return!!this.d&&Nt(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(n){return this.cmp(n)<0};k.lessThanOrEqualTo=k.lte=function(n){return this.cmp(n)<1};k.logarithm=k.log=function(n){var e,t,i,o,r,a,l,f,d=this,h=d.constructor,y=h.precision,P=h.rounding,M=5;if(n==null)n=new h(10),e=!0;else{if(n=new h(n),t=n.d,n.s<0||!t||!t[0]||n.eq(1))return new h(NaN);e=n.eq(10)}if(t=d.d,d.s<0||!t||!t[0]||d.eq(1))return new h(t&&!t[0]?-1/0:d.s!=1?NaN:t?0:1/0);if(e)if(t.length>1)r=!0;else{for(o=t[0];o%10===0;)o/=10;r=o!==1}if(B=!1,l=y+M,a=fe(d,l),i=e?vi(h,l+10):fe(n,l),f=z(a,i,l,1),He(f.d,o=y,P))do if(l+=10,a=fe(d,l),i=e?vi(h,l+10):fe(n,l),f=z(a,i,l,1),!r){+jt(f.d).slice(o+1,o+15)+1==1e14&&(f=E(f,y+1,0));break}while(He(f.d,o+=10,P));return B=!0,E(f,y,P)};k.minus=k.sub=function(n){var e,t,i,o,r,a,l,f,d,h,y,P,M=this,S=M.constructor;if(n=new S(n),!M.d||!n.d)return!M.s||!n.s?n=new S(NaN):M.d?n.s=-n.s:n=new S(n.d||M.s!==n.s?M:NaN),n;if(M.s!=n.s)return n.s=-n.s,M.plus(n);if(d=M.d,P=n.d,l=S.precision,f=S.rounding,!d[0]||!P[0]){if(P[0])n.s=-n.s;else if(d[0])n=new S(M);else return new S(f===3?-0:0);return B?E(n,l,f):n}if(t=Nt(n.e/F),h=Nt(M.e/F),d=d.slice(),r=h-t,r){for(y=r<0,y?(e=d,r=-r,a=P.length):(e=P,t=h,a=d.length),i=Math.max(Math.ceil(l/F),a)+2,r>i&&(r=i,e.length=1),e.reverse(),i=r;i--;)e.push(0);e.reverse()}else{for(i=d.length,a=P.length,y=i<a,y&&(a=i),i=0;i<a;i++)if(d[i]!=P[i]){y=d[i]<P[i];break}r=0}for(y&&(e=d,d=P,P=e,n.s=-n.s),a=d.length,i=P.length-a;i>0;--i)d[a++]=0;for(i=P.length;i>r;){if(d[--i]<P[i]){for(o=i;o&&d[--o]===0;)d[o]=Zt-1;--d[o],d[i]+=Zt}d[i]-=P[i]}for(;d[--a]===0;)d.pop();for(;d[0]===0;d.shift())--t;return d[0]?(n.d=d,n.e=Ci(d,t),B?E(n,l,f):n):new S(f===3?-0:0)};k.modulo=k.mod=function(n){var e,t=this,i=t.constructor;return n=new i(n),!t.d||!n.s||n.d&&!n.d[0]?new i(NaN):!n.d||t.d&&!t.d[0]?E(new i(t),i.precision,i.rounding):(B=!1,i.modulo==9?(e=z(t,n.abs(),0,3,1),e.s*=n.s):e=z(t,n,0,i.modulo,1),e=e.times(n),B=!0,t.minus(e))};k.naturalExponential=k.exp=function(){return mn(this)};k.naturalLogarithm=k.ln=function(){return fe(this)};k.negated=k.neg=function(){var n=new this.constructor(this);return n.s=-n.s,E(n)};k.plus=k.add=function(n){var e,t,i,o,r,a,l,f,d,h,y=this,P=y.constructor;if(n=new P(n),!y.d||!n.d)return!y.s||!n.s?n=new P(NaN):y.d||(n=new P(n.d||y.s===n.s?y:NaN)),n;if(y.s!=n.s)return n.s=-n.s,y.minus(n);if(d=y.d,h=n.d,l=P.precision,f=P.rounding,!d[0]||!h[0])return h[0]||(n=new P(y)),B?E(n,l,f):n;if(r=Nt(y.e/F),i=Nt(n.e/F),d=d.slice(),o=r-i,o){for(o<0?(t=d,o=-o,a=h.length):(t=h,i=r,a=d.length),r=Math.ceil(l/F),a=r>a?r+1:a+1,o>a&&(o=a,t.length=1),t.reverse();o--;)t.push(0);t.reverse()}for(a=d.length,o=h.length,a-o<0&&(o=a,t=h,h=d,d=t),e=0;o;)e=(d[--o]=d[o]+h[o]+e)/Zt|0,d[o]%=Zt;for(e&&(d.unshift(e),++i),a=d.length;d[--a]==0;)d.pop();return n.d=d,n.e=Ci(d,i),B?E(n,l,f):n};k.precision=k.sd=function(n){var e,t=this;if(n!==void 0&&n!==!!n&&n!==1&&n!==0)throw Error(_e+n);return t.d?(e=ar(t.d),n&&t.e+1>e&&(e=t.e+1)):e=NaN,e};k.round=function(){var n=this,e=n.constructor;return E(new e(n),n.e+1,e.rounding)};k.sine=k.sin=function(){var n,e,t=this,i=t.constructor;return t.isFinite()?t.isZero()?new i(t):(n=i.precision,e=i.rounding,i.precision=n+Math.max(t.e,t.sd())+F,i.rounding=1,t=js(i,dr(i,t)),i.precision=n,i.rounding=e,E(re>2?t.neg():t,n,e,!0)):new i(NaN)};k.squareRoot=k.sqrt=function(){var n,e,t,i,o,r,a=this,l=a.d,f=a.e,d=a.s,h=a.constructor;if(d!==1||!l||!l[0])return new h(!d||d<0&&(!l||l[0])?NaN:l?a:1/0);for(B=!1,d=Math.sqrt(+a),d==0||d==1/0?(e=jt(l),(e.length+f)%2==0&&(e+="0"),d=Math.sqrt(e),f=Nt((f+1)/2)-(f<0||f%2),d==1/0?e="5e"+f:(e=d.toExponential(),e=e.slice(0,e.indexOf("e")+1)+f),i=new h(e)):i=new h(d.toString()),t=(f=h.precision)+3;;)if(r=i,i=r.plus(z(a,r,t+2,1)).times(.5),jt(r.d).slice(0,t)===(e=jt(i.d)).slice(0,t))if(e=e.slice(t-3,t+1),e=="9999"||!o&&e=="4999"){if(!o&&(E(r,f+1,0),r.times(r).eq(a))){i=r;break}t+=4,o=1}else{(!+e||!+e.slice(1)&&e.charAt(0)=="5")&&(E(i,f+1,1),n=!i.times(i).eq(a));break}return B=!0,E(i,f,h.rounding,n)};k.tangent=k.tan=function(){var n,e,t=this,i=t.constructor;return t.isFinite()?t.isZero()?new i(t):(n=i.precision,e=i.rounding,i.precision=n+10,i.rounding=1,t=t.sin(),t.s=1,t=z(t,new i(1).minus(t.times(t)).sqrt(),n+10,0),i.precision=n,i.rounding=e,E(re==2||re==4?t.neg():t,n,e,!0)):new i(NaN)};k.times=k.mul=function(n){var e,t,i,o,r,a,l,f,d,h=this,y=h.constructor,P=h.d,M=(n=new y(n)).d;if(n.s*=h.s,!P||!P[0]||!M||!M[0])return new y(!n.s||P&&!P[0]&&!M||M&&!M[0]&&!P?NaN:!P||!M?n.s/0:n.s*0);for(t=Nt(h.e/F)+Nt(n.e/F),f=P.length,d=M.length,f<d&&(r=P,P=M,M=r,a=f,f=d,d=a),r=[],a=f+d,i=a;i--;)r.push(0);for(i=d;--i>=0;){for(e=0,o=f+i;o>i;)l=r[o]+M[i]*P[o-i-1]+e,r[o--]=l%Zt|0,e=l/Zt|0;r[o]=(r[o]+e)%Zt|0}for(;!r[--a];)r.pop();return e?++t:r.shift(),n.d=r,n.e=Ci(r,t),B?E(n,y.precision,y.rounding):n};k.toBinary=function(n,e){return gn(this,2,n,e)};k.toDecimalPlaces=k.toDP=function(n,e){var t=this,i=t.constructor;return t=new i(t),n===void 0?t:(Dt(n,0,pe),e===void 0?e=i.rounding:Dt(e,0,8),E(t,n+t.e+1,e))};k.toExponential=function(n,e){var t,i=this,o=i.constructor;return n===void 0?t=Yt(i,!0):(Dt(n,0,pe),e===void 0?e=o.rounding:Dt(e,0,8),i=E(new o(i),n+1,e),t=Yt(i,!0,n+1)),i.isNeg()&&!i.isZero()?"-"+t:t};k.toFixed=function(n,e){var t,i,o=this,r=o.constructor;return n===void 0?t=Yt(o):(Dt(n,0,pe),e===void 0?e=r.rounding:Dt(e,0,8),i=E(new r(o),n+o.e+1,e),t=Yt(i,!1,n+i.e+1)),o.isNeg()&&!o.isZero()?"-"+t:t};k.toFraction=function(n){var e,t,i,o,r,a,l,f,d,h,y,P,M=this,S=M.d,x=M.constructor;if(!S)return new x(M);if(d=t=new x(1),i=f=new x(0),e=new x(i),r=e.e=ar(S)-M.e-1,a=r%F,e.d[0]=Rt(10,a<0?F+a:a),n==null)n=r>0?e:d;else{if(l=new x(n),!l.isInt()||l.lt(d))throw Error(_e+l);n=l.gt(e)?r>0?e:d:l}for(B=!1,l=new x(jt(S)),h=x.precision,x.precision=r=S.length*F*2;y=z(l,e,0,1,1),o=t.plus(y.times(i)),o.cmp(n)!=1;)t=i,i=o,o=d,d=f.plus(y.times(o)),f=o,o=e,e=l.minus(y.times(o)),l=o;return o=z(n.minus(t),i,0,1,1),f=f.plus(o.times(d)),t=t.plus(o.times(i)),f.s=d.s=M.s,P=z(d,i,r,1).minus(M).abs().cmp(z(f,t,r,1).minus(M).abs())<1?[d,i]:[f,t],x.precision=h,B=!0,P};k.toHexadecimal=k.toHex=function(n,e){return gn(this,16,n,e)};k.toNearest=function(n,e){var t=this,i=t.constructor;if(t=new i(t),n==null){if(!t.d)return t;n=new i(1),e=i.rounding}else{if(n=new i(n),e===void 0?e=i.rounding:Dt(e,0,8),!t.d)return n.s?t:n;if(!n.d)return n.s&&(n.s=t.s),n}return n.d[0]?(B=!1,t=z(t,n,0,e,1).times(n),B=!0,E(t)):(n.s=t.s,t=n),t};k.toNumber=function(){return+this};k.toOctal=function(n,e){return gn(this,8,n,e)};k.toPower=k.pow=function(n){var e,t,i,o,r,a,l=this,f=l.constructor,d=+(n=new f(n));if(!l.d||!n.d||!l.d[0]||!n.d[0])return new f(Rt(+l,d));if(l=new f(l),l.eq(1))return l;if(i=f.precision,r=f.rounding,n.eq(1))return E(l,i,r);if(e=Nt(n.e/F),e>=n.d.length-1&&(t=d<0?-d:d)<=As)return o=cr(f,l,t,i),n.s<0?new f(1).div(o):E(o,i,r);if(a=l.s,a<0){if(e<n.d.length-1)return new f(NaN);if((n.d[e]&1)==0&&(a=1),l.e==0&&l.d[0]==1&&l.d.length==1)return l.s=a,l}return t=Rt(+l,d),e=t==0||!isFinite(t)?Nt(d*(Math.log("0."+jt(l.d))/Math.LN10+l.e+1)):new f(t+"").e,e>f.maxE+1||e<f.minE-1?new f(e>0?a/0:0):(B=!1,f.rounding=l.s=1,t=Math.min(12,(e+"").length),o=mn(n.times(fe(l,i+t)),i),o.d&&(o=E(o,i+5,1),He(o.d,i,r)&&(e=i+10,o=E(mn(n.times(fe(l,e+t)),e),e+5,1),+jt(o.d).slice(i+1,i+15)+1==1e14&&(o=E(o,i+1,0)))),o.s=a,B=!0,f.rounding=r,E(o,i,r))};k.toPrecision=function(n,e){var t,i=this,o=i.constructor;return n===void 0?t=Yt(i,i.e<=o.toExpNeg||i.e>=o.toExpPos):(Dt(n,1,pe),e===void 0?e=o.rounding:Dt(e,0,8),i=E(new o(i),n,e),t=Yt(i,n<=i.e||i.e<=o.toExpNeg,n)),i.isNeg()&&!i.isZero()?"-"+t:t};k.toSignificantDigits=k.toSD=function(n,e){var t=this,i=t.constructor;return n===void 0?(n=i.precision,e=i.rounding):(Dt(n,1,pe),e===void 0?e=i.rounding:Dt(e,0,8)),E(new i(t),n,e)};k.toString=function(){var n=this,e=n.constructor,t=Yt(n,n.e<=e.toExpNeg||n.e>=e.toExpPos);return n.isNeg()&&!n.isZero()?"-"+t:t};k.truncated=k.trunc=function(){return E(new this.constructor(this),this.e+1,1)};k.valueOf=k.toJSON=function(){var n=this,e=n.constructor,t=Yt(n,n.e<=e.toExpNeg||n.e>=e.toExpPos);return n.isNeg()?"-"+t:t};function jt(n){var e,t,i,o=n.length-1,r="",a=n[0];if(o>0){for(r+=a,e=1;e<o;e++)i=n[e]+"",t=F-i.length,t&&(r+=de(t)),r+=i;a=n[e],i=a+"",t=F-i.length,t&&(r+=de(t))}else if(a===0)return"0";for(;a%10===0;)a/=10;return r+a}function Dt(n,e,t){if(n!==~~n||n<e||n>t)throw Error(_e+n)}function He(n,e,t,i){var o,r,a,l;for(r=n[0];r>=10;r/=10)--e;return--e<0?(e+=F,o=0):(o=Math.ceil((e+1)/F),e%=F),r=Rt(10,F-e),l=n[o]%r|0,i==null?e<3?(e==0?l=l/100|0:e==1&&(l=l/10|0),a=t<4&&l==99999||t>3&&l==49999||l==5e4||l==0):a=(t<4&&l+1==r||t>3&&l+1==r/2)&&(n[o+1]/r/100|0)==Rt(10,e-2)-1||(l==r/2||l==0)&&(n[o+1]/r/100|0)==0:e<4?(e==0?l=l/1e3|0:e==1?l=l/100|0:e==2&&(l=l/10|0),a=(i||t<4)&&l==9999||!i&&t>3&&l==4999):a=((i||t<4)&&l+1==r||!i&&t>3&&l+1==r/2)&&(n[o+1]/r/1e3|0)==Rt(10,e-3)-1,a}function gi(n,e,t){for(var i,o=[0],r,a=0,l=n.length;a<l;){for(r=o.length;r--;)o[r]*=e;for(o[0]+=_n.indexOf(n.charAt(a++)),i=0;i<o.length;i++)o[i]>t-1&&(o[i+1]===void 0&&(o[i+1]=0),o[i+1]+=o[i]/t|0,o[i]%=t)}return o.reverse()}function Rs(n,e){var t,i,o;if(e.isZero())return e;i=e.d.length,i<32?(t=Math.ceil(i/3),o=(1/Mi(4,t)).toString()):(t=16,o="2.3283064365386962890625e-10"),n.precision+=t,e=Ne(n,1,e.times(o),new n(1));for(var r=t;r--;){var a=e.times(e);e=a.times(a).minus(a).times(8).plus(1)}return n.precision-=t,e}var z=(function(){function n(i,o,r){var a,l=0,f=i.length;for(i=i.slice();f--;)a=i[f]*o+l,i[f]=a%r|0,l=a/r|0;return l&&i.unshift(l),i}function e(i,o,r,a){var l,f;if(r!=a)f=r>a?1:-1;else for(l=f=0;l<r;l++)if(i[l]!=o[l]){f=i[l]>o[l]?1:-1;break}return f}function t(i,o,r,a){for(var l=0;r--;)i[r]-=l,l=i[r]<o[r]?1:0,i[r]=l*a+i[r]-o[r];for(;!i[0]&&i.length>1;)i.shift()}return function(i,o,r,a,l,f){var d,h,y,P,M,S,x,U,D,Q,q,L,Lt,H,$t,ie,zt,ne,$,Ot,w=i.constructor,s=i.s==o.s?1:-1,u=i.d,_=o.d;if(!u||!u[0]||!_||!_[0])return new w(!i.s||!o.s||(u?_&&u[0]==_[0]:!_)?NaN:u&&u[0]==0||!_?s*0:s/0);for(f?(M=1,h=i.e-o.e):(f=Zt,M=F,h=Nt(i.e/M)-Nt(o.e/M)),$=_.length,zt=u.length,D=new w(s),Q=D.d=[],y=0;_[y]==(u[y]||0);y++);if(_[y]>(u[y]||0)&&h--,r==null?(H=r=w.precision,a=w.rounding):l?H=r+(i.e-o.e)+1:H=r,H<0)Q.push(1),S=!0;else{if(H=H/M+2|0,y=0,$==1){for(P=0,_=_[0],H++;(y<zt||P)&&H--;y++)$t=P*f+(u[y]||0),Q[y]=$t/_|0,P=$t%_|0;S=P||y<zt}else{for(P=f/(_[0]+1)|0,P>1&&(_=n(_,P,f),u=n(u,P,f),$=_.length,zt=u.length),ie=$,q=u.slice(0,$),L=q.length;L<$;)q[L++]=0;Ot=_.slice(),Ot.unshift(0),ne=_[0],_[1]>=f/2&&++ne;do P=0,d=e(_,q,$,L),d<0?(Lt=q[0],$!=L&&(Lt=Lt*f+(q[1]||0)),P=Lt/ne|0,P>1?(P>=f&&(P=f-1),x=n(_,P,f),U=x.length,L=q.length,d=e(x,q,U,L),d==1&&(P--,t(x,$<U?Ot:_,U,f))):(P==0&&(d=P=1),x=_.slice()),U=x.length,U<L&&x.unshift(0),t(q,x,L,f),d==-1&&(L=q.length,d=e(_,q,$,L),d<1&&(P++,t(q,$<L?Ot:_,L,f))),L=q.length):d===0&&(P++,q=[0]),Q[y++]=P,d&&q[0]?q[L++]=u[ie]||0:(q=[u[ie]],L=1);while((ie++<zt||q[0]!==void 0)&&H--);S=q[0]!==void 0}Q[0]||Q.shift()}if(M==1)D.e=h,ir=S;else{for(y=1,P=Q[0];P>=10;P/=10)y++;D.e=y+h*M-1,E(D,l?r+D.e+1:r,a,S)}return D}})();function E(n,e,t,i){var o,r,a,l,f,d,h,y,P,M=n.constructor;t:if(e!=null){if(y=n.d,!y)return n;for(o=1,l=y[0];l>=10;l/=10)o++;if(r=e-o,r<0)r+=F,a=e,h=y[P=0],f=h/Rt(10,o-a-1)%10|0;else if(P=Math.ceil((r+1)/F),l=y.length,P>=l)if(i){for(;l++<=P;)y.push(0);h=f=0,o=1,r%=F,a=r-F+1}else break t;else{for(h=l=y[P],o=1;l>=10;l/=10)o++;r%=F,a=r-F+o,f=a<0?0:h/Rt(10,o-a-1)%10|0}if(i=i||e<0||y[P+1]!==void 0||(a<0?h:h%Rt(10,o-a-1)),d=t<4?(f||i)&&(t==0||t==(n.s<0?3:2)):f>5||f==5&&(t==4||i||t==6&&(r>0?a>0?h/Rt(10,o-a):0:y[P-1])%10&1||t==(n.s<0?8:7)),e<1||!y[0])return y.length=0,d?(e-=n.e+1,y[0]=Rt(10,(F-e%F)%F),n.e=-e||0):y[0]=n.e=0,n;if(r==0?(y.length=P,l=1,P--):(y.length=P+1,l=Rt(10,F-r),y[P]=a>0?(h/Rt(10,o-a)%Rt(10,a)|0)*l:0),d)for(;;)if(P==0){for(r=1,a=y[0];a>=10;a/=10)r++;for(a=y[0]+=l,l=1;a>=10;a/=10)l++;r!=l&&(n.e++,y[0]==Zt&&(y[0]=1));break}else{if(y[P]+=l,y[P]!=Zt)break;y[P--]=0,l=1}for(r=y.length;y[--r]===0;)y.pop()}return B&&(n.e>M.maxE?(n.d=null,n.e=NaN):n.e<M.minE&&(n.e=0,n.d=[0])),n}function Yt(n,e,t){if(!n.isFinite())return ur(n);var i,o=n.e,r=jt(n.d),a=r.length;return e?(t&&(i=t-a)>0?r=r.charAt(0)+"."+r.slice(1)+de(i):a>1&&(r=r.charAt(0)+"."+r.slice(1)),r=r+(n.e<0?"e":"e+")+n.e):o<0?(r="0."+de(-o-1)+r,t&&(i=t-a)>0&&(r+=de(i))):o>=a?(r+=de(o+1-a),t&&(i=t-o-1)>0&&(r=r+"."+de(i))):((i=o+1)<a&&(r=r.slice(0,i)+"."+r.slice(i)),t&&(i=t-a)>0&&(o+1===a&&(r+="."),r+=de(i))),r}function Ci(n,e){var t=n[0];for(e*=F;t>=10;t/=10)e++;return e}function vi(n,e,t){if(e>Is)throw B=!0,t&&(n.precision=t),Error(nr);return E(new n(yi),e,1,!0)}function Jt(n,e,t){if(e>hn)throw Error(nr);return E(new n(wi),e,t,!0)}function ar(n){var e=n.length-1,t=e*F+1;if(e=n[e],e){for(;e%10==0;e/=10)t--;for(e=n[0];e>=10;e/=10)t++}return t}function de(n){for(var e="";n--;)e+="0";return e}function cr(n,e,t,i){var o,r=new n(1),a=Math.ceil(i/F+4);for(B=!1;;){if(t%2&&(r=r.times(e),tr(r.d,a)&&(o=!0)),t=Nt(t/2),t===0){t=r.d.length-1,o&&r.d[t]===0&&++r.d[t];break}e=e.times(e),tr(e.d,a)}return B=!0,r}function Yo(n){return n.d[n.d.length-1]&1}function lr(n,e,t){for(var i,o,r=new n(e[0]),a=0;++a<e.length;){if(o=new n(e[a]),!o.s){r=o;break}i=r.cmp(o),(i===t||i===0&&r.s===t)&&(r=o)}return r}function mn(n,e){var t,i,o,r,a,l,f,d=0,h=0,y=0,P=n.constructor,M=P.rounding,S=P.precision;if(!n.d||!n.d[0]||n.e>17)return new P(n.d?n.d[0]?n.s<0?0:1/0:1:n.s?n.s<0?0:n:NaN);for(e==null?(B=!1,f=S):f=e,l=new P(.03125);n.e>-2;)n=n.times(l),y+=5;for(i=Math.log(Rt(2,y))/Math.LN10*2+5|0,f+=i,t=r=a=new P(1),P.precision=f;;){if(r=E(r.times(n),f,1),t=t.times(++h),l=a.plus(z(r,t,f,1)),jt(l.d).slice(0,f)===jt(a.d).slice(0,f)){for(o=y;o--;)a=E(a.times(a),f,1);if(e==null)if(d<3&&He(a.d,f-i,M,d))P.precision=f+=10,t=r=l=new P(1),h=0,d++;else return E(a,P.precision=S,M,B=!0);else return P.precision=S,a}a=l}}function fe(n,e){var t,i,o,r,a,l,f,d,h,y,P,M=1,S=10,x=n,U=x.d,D=x.constructor,Q=D.rounding,q=D.precision;if(x.s<0||!U||!U[0]||!x.e&&U[0]==1&&U.length==1)return new D(U&&!U[0]?-1/0:x.s!=1?NaN:U?0:x);if(e==null?(B=!1,h=q):h=e,D.precision=h+=S,t=jt(U),i=t.charAt(0),Math.abs(r=x.e)<15e14){for(;i<7&&i!=1||i==1&&t.charAt(1)>3;)x=x.times(n),t=jt(x.d),i=t.charAt(0),M++;r=x.e,i>1?(x=new D("0."+t),r++):x=new D(i+"."+t.slice(1))}else return d=vi(D,h+2,q).times(r+""),x=fe(new D(i+"."+t.slice(1)),h-S).plus(d),D.precision=q,e==null?E(x,q,Q,B=!0):x;for(y=x,f=a=x=z(x.minus(1),x.plus(1),h,1),P=E(x.times(x),h,1),o=3;;){if(a=E(a.times(P),h,1),d=f.plus(z(a,new D(o),h,1)),jt(d.d).slice(0,h)===jt(f.d).slice(0,h))if(f=f.times(2),r!==0&&(f=f.plus(vi(D,h+2,q).times(r+""))),f=z(f,new D(M),h,1),e==null)if(He(f.d,h-S,Q,l))D.precision=h+=S,d=a=x=z(y.minus(1),y.plus(1),h,1),P=E(x.times(x),h,1),o=l=1;else return E(f,D.precision=q,Q,B=!0);else return D.precision=q,f;f=d,o+=2}}function ur(n){return String(n.s*n.s/0)}function bi(n,e){var t,i,o;for((t=e.indexOf("."))>-1&&(e=e.replace(".","")),(i=e.search(/e/i))>0?(t<0&&(t=i),t+=+e.slice(i+1),e=e.substring(0,i)):t<0&&(t=e.length),i=0;e.charCodeAt(i)===48;i++);for(o=e.length;e.charCodeAt(o-1)===48;--o);if(e=e.slice(i,o),e){if(o-=i,n.e=t=t-i-1,n.d=[],i=(t+1)%F,t<0&&(i+=F),i<o){for(i&&n.d.push(+e.slice(0,i)),o-=F;i<o;)n.d.push(+e.slice(i,i+=F));e=e.slice(i),i=F-e.length}else i-=o;for(;i--;)e+="0";n.d.push(+e),B&&(n.e>n.constructor.maxE?(n.d=null,n.e=NaN):n.e<n.constructor.minE&&(n.e=0,n.d=[0]))}else n.e=0,n.d=[0];return n}function Es(n,e){var t,i,o,r,a,l,f,d,h;if(e.indexOf("_")>-1){if(e=e.replace(/(\d)_(?=\d)/g,"$1"),sr.test(e))return bi(n,e)}else if(e==="Infinity"||e==="NaN")return+e||(n.s=NaN),n.e=NaN,n.d=null,n;if(xs.test(e))t=16,e=e.toLowerCase();else if(Ts.test(e))t=2;else if(Ss.test(e))t=8;else throw Error(_e+e);for(r=e.search(/p/i),r>0?(f=+e.slice(r+1),e=e.substring(2,r)):e=e.slice(2),r=e.indexOf("."),a=r>=0,i=n.constructor,a&&(e=e.replace(".",""),l=e.length,r=l-r,o=cr(i,new i(t),r,r*2)),d=gi(e,t,Zt),h=d.length-1,r=h;d[r]===0;--r)d.pop();return r<0?new i(n.s*0):(n.e=Ci(d,h),n.d=d,B=!1,a&&(n=z(n,o,l*4)),f&&(n=n.times(Math.abs(f)<54?Rt(2,f):Ge.pow(2,f))),B=!0,n)}function js(n,e){var t,i=e.d.length;if(i<3)return e.isZero()?e:Ne(n,2,e,e);t=1.4*Math.sqrt(i),t=t>16?16:t|0,e=e.times(1/Mi(5,t)),e=Ne(n,2,e,e);for(var o,r=new n(5),a=new n(16),l=new n(20);t--;)o=e.times(e),e=e.times(r.plus(o.times(a.times(o).minus(l))));return e}function Ne(n,e,t,i,o){var r,a,l,f,d=1,h=n.precision,y=Math.ceil(h/F);for(B=!1,f=t.times(t),l=new n(i);;){if(a=z(l.times(f),new n(e++*e++),h,1),l=o?i.plus(a):i.minus(a),i=z(a.times(f),new n(e++*e++),h,1),a=l.plus(i),a.d[y]!==void 0){for(r=y;a.d[r]===l.d[r]&&r--;);if(r==-1)break}r=l,l=i,i=a,a=r,d++}return B=!0,a.d.length=y+1,a}function Mi(n,e){for(var t=n;--e;)t*=n;return t}function dr(n,e){var t,i=e.s<0,o=Jt(n,n.precision,1),r=o.times(.5);if(e=e.abs(),e.lte(r))return re=i?4:1,e;if(t=e.divToInt(o),t.isZero())re=i?3:2;else{if(e=e.minus(t.times(o)),e.lte(r))return re=Yo(t)?i?2:3:i?4:1,e;re=Yo(t)?i?1:4:i?3:2}return e.minus(o).abs()}function gn(n,e,t,i){var o,r,a,l,f,d,h,y,P,M=n.constructor,S=t!==void 0;if(S?(Dt(t,1,pe),i===void 0?i=M.rounding:Dt(i,0,8)):(t=M.precision,i=M.rounding),!n.isFinite())h=ur(n);else{for(h=Yt(n),a=h.indexOf("."),S?(o=2,e==16?t=t*4-3:e==8&&(t=t*3-2)):o=e,a>=0&&(h=h.replace(".",""),P=new M(1),P.e=h.length-a,P.d=gi(Yt(P),10,o),P.e=P.d.length),y=gi(h,10,o),r=f=y.length;y[--f]==0;)y.pop();if(!y[0])h=S?"0p+0":"0";else{if(a<0?r--:(n=new M(n),n.d=y,n.e=r,n=z(n,P,t,i,0,o),y=n.d,r=n.e,d=ir),a=y[t],l=o/2,d=d||y[t+1]!==void 0,d=i<4?(a!==void 0||d)&&(i===0||i===(n.s<0?3:2)):a>l||a===l&&(i===4||d||i===6&&y[t-1]&1||i===(n.s<0?8:7)),y.length=t,d)for(;++y[--t]>o-1;)y[t]=0,t||(++r,y.unshift(1));for(f=y.length;!y[f-1];--f);for(a=0,h="";a<f;a++)h+=_n.charAt(y[a]);if(S){if(f>1)if(e==16||e==8){for(a=e==16?4:3,--f;f%a;f++)h+="0";for(y=gi(h,o,e),f=y.length;!y[f-1];--f);for(a=1,h="1.";a<f;a++)h+=_n.charAt(y[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>f)for(r-=f;r--;)h+="0";else r<f&&(h=h.slice(0,r)+"."+h.slice(r))}h=(e==16?"0x":e==2?"0b":e==8?"0o":"")+h}return n.s<0?"-"+h:h}function tr(n,e){if(n.length>e)return n.length=e,!0}function Fs(n){return new this(n).abs()}function Ns(n){return new this(n).acos()}function Bs(n){return new this(n).acosh()}function Os(n,e){return new this(n).plus(e)}function qs(n){return new this(n).asin()}function Ds(n){return new this(n).asinh()}function Ls(n){return new this(n).atan()}function $s(n){return new this(n).atanh()}function Us(n,e){n=new this(n),e=new this(e);var t,i=this.precision,o=this.rounding,r=i+4;return!n.s||!e.s?t=new this(NaN):!n.d&&!e.d?(t=Jt(this,r,1).times(e.s>0?.25:.75),t.s=n.s):!e.d||n.isZero()?(t=e.s<0?Jt(this,i,o):new this(0),t.s=n.s):!n.d||e.isZero()?(t=Jt(this,r,1).times(.5),t.s=n.s):e.s<0?(this.precision=r,this.rounding=1,t=this.atan(z(n,e,r,1)),e=Jt(this,r,1),this.precision=i,this.rounding=o,t=n.s<0?t.minus(e):t.plus(e)):t=this.atan(z(n,e,r,1)),t}function Vs(n){return new this(n).cbrt()}function Ks(n){return E(n=new this(n),n.e+1,2)}function Ws(n,e,t){return new this(n).clamp(e,t)}function Zs(n){if(!n||typeof n!="object")throw Error(Pi+"Object expected");var e,t,i,o=n.defaults===!0,r=["precision",1,pe,"rounding",0,8,"toExpNeg",-Fe,0,"toExpPos",0,Fe,"maxE",0,Fe,"minE",-Fe,0,"modulo",0,9];for(e=0;e<r.length;e+=3)if(t=r[e],o&&(this[t]=pn[t]),(i=n[t])!==void 0)if(Nt(i)===i&&i>=r[e+1]&&i<=r[e+2])this[t]=i;else throw Error(_e+t+": "+i);if(t="crypto",o&&(this[t]=pn[t]),(i=n[t])!==void 0)if(i===!0||i===!1||i===0||i===1)if(i)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[t]=!0;else throw Error(or);else this[t]=!1;else throw Error(_e+t+": "+i);return this}function zs(n){return new this(n).cos()}function Hs(n){return new this(n).cosh()}function fr(n){var e,t,i;function o(r){var a,l,f,d=this;if(!(d instanceof o))return new o(r);if(d.constructor=o,er(r)){d.s=r.s,B?!r.d||r.e>o.maxE?(d.e=NaN,d.d=null):r.e<o.minE?(d.e=0,d.d=[0]):(d.e=r.e,d.d=r.d.slice()):(d.e=r.e,d.d=r.d?r.d.slice():r.d);return}if(f=typeof r,f==="number"){if(r===0){d.s=1/r<0?-1:1,d.e=0,d.d=[0];return}if(r<0?(r=-r,d.s=-1):d.s=1,r===~~r&&r<1e7){for(a=0,l=r;l>=10;l/=10)a++;B?a>o.maxE?(d.e=NaN,d.d=null):a<o.minE?(d.e=0,d.d=[0]):(d.e=a,d.d=[r]):(d.e=a,d.d=[r]);return}if(r*0!==0){r||(d.s=NaN),d.e=NaN,d.d=null;return}return bi(d,r.toString())}if(f==="string")return(l=r.charCodeAt(0))===45?(r=r.slice(1),d.s=-1):(l===43&&(r=r.slice(1)),d.s=1),sr.test(r)?bi(d,r):Es(d,r);if(f==="bigint")return r<0?(r=-r,d.s=-1):d.s=1,bi(d,r.toString());throw Error(_e+r)}if(o.prototype=k,o.ROUND_UP=0,o.ROUND_DOWN=1,o.ROUND_CEIL=2,o.ROUND_FLOOR=3,o.ROUND_HALF_UP=4,o.ROUND_HALF_DOWN=5,o.ROUND_HALF_EVEN=6,o.ROUND_HALF_CEIL=7,o.ROUND_HALF_FLOOR=8,o.EUCLID=9,o.config=o.set=Zs,o.clone=fr,o.isDecimal=er,o.abs=Fs,o.acos=Ns,o.acosh=Bs,o.add=Os,o.asin=qs,o.asinh=Ds,o.atan=Ls,o.atanh=$s,o.atan2=Us,o.cbrt=Vs,o.ceil=Ks,o.clamp=Ws,o.cos=zs,o.cosh=Hs,o.div=Gs,o.exp=Qs,o.floor=Xs,o.hypot=Js,o.ln=Ys,o.log=ta,o.log10=ia,o.log2=ea,o.max=na,o.min=oa,o.mod=ra,o.mul=sa,o.pow=aa,o.random=ca,o.round=la,o.sign=ua,o.sin=da,o.sinh=fa,o.sqrt=_a,o.sub=pa,o.sum=ha,o.tan=ma,o.tanh=ga,o.trunc=ba,n===void 0&&(n={}),n&&n.defaults!==!0)for(i=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],e=0;e<i.length;)n.hasOwnProperty(t=i[e++])||(n[t]=this[t]);return o.config(n),o}function Gs(n,e){return new this(n).div(e)}function Qs(n){return new this(n).exp()}function Xs(n){return E(n=new this(n),n.e+1,3)}function Js(){var n,e,t=new this(0);for(B=!1,n=0;n<arguments.length;)if(e=new this(arguments[n++]),e.d)t.d&&(t=t.plus(e.times(e)));else{if(e.s)return B=!0,new this(1/0);t=e}return B=!0,t.sqrt()}function er(n){return n instanceof Ge||n&&n.toStringTag===rr||!1}function Ys(n){return new this(n).ln()}function ta(n,e){return new this(n).log(e)}function ea(n){return new this(n).log(2)}function ia(n){return new this(n).log(10)}function na(){return lr(this,arguments,-1)}function oa(){return lr(this,arguments,1)}function ra(n,e){return new this(n).mod(e)}function sa(n,e){return new this(n).mul(e)}function aa(n,e){return new this(n).pow(e)}function ca(n){var e,t,i,o,r=0,a=new this(1),l=[];if(n===void 0?n=this.precision:Dt(n,1,pe),i=Math.ceil(n/F),this.crypto)if(crypto.getRandomValues)for(e=crypto.getRandomValues(new Uint32Array(i));r<i;)o=e[r],o>=429e7?e[r]=crypto.getRandomValues(new Uint32Array(1))[0]:l[r++]=o%1e7;else if(crypto.randomBytes){for(e=crypto.randomBytes(i*=4);r<i;)o=e[r]+(e[r+1]<<8)+(e[r+2]<<16)+((e[r+3]&127)<<24),o>=214e7?crypto.randomBytes(4).copy(e,r):(l.push(o%1e7),r+=4);r=i/4}else throw Error(or);else for(;r<i;)l[r++]=Math.random()*1e7|0;for(i=l[--r],n%=F,i&&n&&(o=Rt(10,F-n),l[r]=(i/o|0)*o);l[r]===0;r--)l.pop();if(r<0)t=0,l=[0];else{for(t=-1;l[0]===0;t-=F)l.shift();for(i=1,o=l[0];o>=10;o/=10)i++;i<F&&(t-=F-i)}return a.e=t,a.d=l,a}function la(n){return E(n=new this(n),n.e+1,this.rounding)}function ua(n){return n=new this(n),n.d?n.d[0]?n.s:0*n.s:n.s||NaN}function da(n){return new this(n).sin()}function fa(n){return new this(n).sinh()}function _a(n){return new this(n).sqrt()}function pa(n,e){return new this(n).sub(e)}function ha(){var n=0,e=arguments,t=new this(e[n]);for(B=!1;t.s&&++n<e.length;)t=t.plus(e[n]);return B=!0,E(t,this.precision,this.rounding)}function ma(n){return new this(n).tan()}function ga(n){return new this(n).tanh()}function ba(n){return E(n=new this(n),n.e+1,1)}k[Symbol.for("nodejs.util.inspect.custom")]=k.toString;k[Symbol.toStringTag]="Decimal";var Ge=k.constructor=fr(pn);yi=new Ge(yi);wi=new Ge(wi);var j=Ge;import{bcs as Ta}from"@mysten/sui/bcs";var xi=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}buildPositionType(){return`${this._sdk.sdkOptions.clmm_pool.package_id}::position::Position`}async getPositionTransactionList({pos_id:e,origin_pos_id:t,full_rpc_url:i,pagination_args:o="all",order:r="ascending"}){let{FullClient:a}=this._sdk,l=[e];t&&l.push(t);let f;i?f=Pa(Ma(i,this._sdk.sdkOptions.env),a._graphQLClient,this._sdk.sdkOptions.env,ka(i,this._sdk.sdkOptions.env)):f=a;let d={data:[],has_next_page:!1};try{let h=await f.queryTransactionBlocksByPage({affectedObject:e},o);return h.data.forEach((y,P)=>{let M=Ao(y,P,l);d.data=[...d.data,...M]}),d.has_next_page=h.has_next_page,d.next_cursor=h.next_cursor,d}catch(h){Bt("FetchError",h,{[ae.METHOD_NAME]:"getPositionTransactionList"})}return d}async getPositionList(e,t=[],i=!0){let o=[],r=await this._sdk.FullClient.getOwnedObjectsByPage(e,this.buildPositionType()),a=t.length>0;for(let l of r.data)if(va(l.type).full_address===this.buildPositionType()){let d=ui(l),h=`${d.pos_object_id}_getPositionList`;this._sdk.updateCache(h,d),a?t.includes(d.pool)&&o.push(d):o.push(d)}return o}async getPosition(e,t,i=!0){let o=await this.getSimplePosition(t);return i&&(o=await this.updatePositionInfo(e,o)),o}async getPositionById(e,t=!0){let i=await this.getSimplePosition(e);if(t){let o=await this._sdk.Pool.getPool(i.pool,!1);return await this.updatePositionInfo(o.position_manager.positions_handle,i)}return i}async getSimplePosition(e){let t=`${e}_getPositionList`,i=this.getSimplePositionByCache(e);if(i===void 0){let o=await this.sdk.FullClient.getObject({objectId:e,include:{json:!0}});i=ui(o.object),this._sdk.updateCache(t,i)}return i}getSimplePositionByCache(e){let t=`${e}_getPositionList`;return this._sdk.getCache(t)}async getSimplePositionList(e,t=!0){let i=[],o=[];return e.forEach(r=>{let a=this.getSimplePositionByCache(r);a?i.push(a):o.push(r)}),o.length>0&&(await this._sdk.FullClient.batchGetObjects(o,{json:!0})).forEach(a=>{let l=ui(a);if(l){i.push(l);let f=`${l.pos_object_id}_getPositionList`;this._sdk.updateCache(f,l)}}),i}async updatePositionInfo(e,t){let i=await this.getPositionInfo(e,t.pos_object_id);return{...t,...i}}async getPositionInfo(e,t){try{let i=await this._sdk.FullClient.getDynamicField({parentId:e,name:{type:"0x2::object::ID",bcs:Ta.Address.serialize(t).toBytes()}}),o=Vo.parse(i.dynamicField.value.bcs);return We(o.value)}catch(i){return Bt("FetchError",i,{[ae.METHOD_NAME]:"getPositionInfo",[ae.REQUEST_PARAMS]:{position_handle:e,position_id:t}})}}async getPositionInfoList(e){try{let t=[],i=[];return e.forEach(async r=>{let{position_handle:a,position_ids:l}=r;l.forEach(f=>{let d=Ca(a,f,"0x2::object::ID","address");i.push(d)})}),i.length===0?[]:((await this._sdk.FullClient.batchGetObjects(i,{json:!0})).forEach(r=>{try{let a=We(r.json.value.value);t.push(a)}catch(a){console.log("getPositionInfoList error",a)}}),t)}catch(t){return Bt("FetchError",t,{[ae.METHOD_NAME]:"getPositionInfoList",[ae.REQUEST_PARAMS]:{options:e}})}}buildFetchPosFee(e,t){let{clmm_pool:i,integrate:o}=this.sdk.sdkOptions,r=[e.coin_type_a,e.coin_type_b],a=[t.object(ee(i).global_config_id),t.object(e.pool_id),t.pure.address(e.position_id)];t.moveCall({target:`${o.published_at}::${Qt}::fetch_position_fees`,arguments:a,typeArguments:r})}parsedPosFeeData(e){let t={},i=e.events?.filter(o=>o.eventType.includes("fetcher_script::FetchPositionFeesEvent"));for(let o=0;o<i.length;o+=1){let{bcs:r}=i[o],a=zo.parse(r);t[a.position_id]={position_id:a.position_id,fee_owned_a:a.fee_owned_a.toString(),fee_owned_b:a.fee_owned_b.toString()}}return t}async fetchPosFeeAmount(e){let t=new se;for(let a of e)this.buildFetchPosFee(a,t);let i=await this.sdk.FullClient.sendSimulationTransaction(t,wa("0x0"));if(i.FailedTransaction!=null)return Bt("FetchError",new Error(i.FailedTransaction.status.error?.message??"unknown error"),{[ae.METHOD_NAME]:"fetchPosFeeAmount",[ae.REQUEST_PARAMS]:{params:e}});let o=[],r=this.parsedPosFeeData(i.Transaction);for(let a=0;a<e.length;a+=1){let l=r[e[a].position_id];if(l){let f={fee_owned_a:l.fee_owned_a,fee_owned_b:l.fee_owned_b,position_id:e[a].position_id};o.push(f)}}return o}async batchFetchPositionFees(e){let t=[];for(let o of e){let r=await this._sdk.Position.getPositionById(o,!1),a=await this._sdk.Pool.getPool(r.pool,!1);t.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(t.length>0){let o=await this.fetchPosFeeAmount(t);for(let r of o)i[r.position_id]=r;return i}return i}async createAddLiquidityFixTokenPayload(e,t,i,o){return Wt.buildAddLiquidityFixToken(this._sdk,e,t,i,o)}async createAddLiquidityPayload(e,t,i,o){let{integrate:r,clmm_pool:a}=this._sdk.sdkOptions,l=Be(BigInt(e.tick_lower)).toString(),f=Be(BigInt(e.tick_upper)).toString(),d=[e.coin_type_a,e.coin_type_b];t=t||new se;let h=!ya(e.pos_id),y=BigInt(e.max_amount_a),P=BigInt(e.max_amount_b),M=i||Oe.buildCoinWithBalance(y,e.coin_type_a,t),S=o||Oe.buildCoinWithBalance(P,e.coin_type_b,t);return h?t.moveCall({target:`${r.published_at}::${Gt}::open_position_with_liquidity`,typeArguments:d,arguments:[t.object(ee(a).global_config_id),t.object(e.pool_id),t.pure.u32(Number(l)),t.pure.u32(Number(f)),M,S,t.pure.u64(e.max_amount_a),t.pure.u64(e.max_amount_b),t.pure.u128(e.delta_liquidity),t.object(ki)]}):(t=Wt.createCollectRewarderAndFeeParams(this._sdk,t,e),t.moveCall({target:`${r.published_at}::${Gt}::add_liquidity`,typeArguments:d,arguments:[t.object(ee(a).global_config_id),t.object(e.pool_id),t.object(e.pos_id),M,S,t.pure.u64(e.max_amount_a),t.pure.u64(e.max_amount_b),t.pure.u128(e.delta_liquidity),t.object(ki)]})),t}async removeLiquidityPayload(e,t){let{clmm_pool:i,integrate:o}=this.sdk.sdkOptions;t=t||new se;let r=[e.coin_type_a,e.coin_type_b];t=Wt.createCollectRewarderAndFeeParams(this._sdk,t,e);let a=[t.object(ee(i).global_config_id),t.object(e.pool_id),t.object(e.pos_id),t.pure.u128(e.delta_liquidity),t.object(ki)],[l,f]=t.moveCall({target:`${i.published_at}::pool::remove_liquidity`,typeArguments:r,arguments:a}),d=Oe.fromBalance(l,e.coin_type_a,t),h=Oe.fromBalance(f,e.coin_type_b,t);return t.moveCall({target:`${o.published_at}::${me}::check_coin_threshold`,typeArguments:[e.coin_type_a],arguments:[d,t.pure.u64(e.min_amount_a)]}),t.moveCall({target:`${o.published_at}::${me}::check_coin_threshold`,typeArguments:[e.coin_type_b],arguments:[h,t.pure.u64(e.min_amount_b)]}),e.is_return_coins?{coin_a:d,coin_b:h}:(t.transferObjects([d,h],this.sdk.getSenderAddress()),t)}async closePositionPayload(e,t){let{clmm_pool:i,integrate:o}=this.sdk.sdkOptions;t=t||new se;let r=[e.coin_type_a,e.coin_type_b];return t=Wt.createCollectRewarderAndFeeParams(this._sdk,t,e),t.moveCall({target:`${o.published_at}::${ri}::close_position`,typeArguments:r,arguments:[t.object(ee(i).global_config_id),t.object(e.pool_id),t.object(e.pos_id),t.pure.u64(e.min_amount_a),t.pure.u64(e.min_amount_b),t.object(ki)]}),t}openPositionPayload(e,t){let{clmm_pool:i,integrate:o}=this.sdk.sdkOptions;t=t||new se;let r=[e.coin_type_a,e.coin_type_b],a=Be(BigInt(e.tick_lower)).toString(),l=Be(BigInt(e.tick_upper)).toString(),f=[t.object(ee(i).global_config_id),t.object(e.pool_id),t.pure.u32(Number(a)),t.pure.u32(Number(l))];return t.moveCall({target:`${o.published_at}::${ri}::open_position`,typeArguments:r,arguments:f}),t}async openPositionWithPricePayload(e,t){let{pool_id:i}=e,{clmm_pool:o,integrate:r}=this.sdk.sdkOptions;t=t||new se;let a=await this.sdk.Pool.getPool(i,!1),l=Number(a.tick_spacing),f=0,d=0;if(e.is_full_range)f=Ti.getMinIndex(l),d=Ti.getMaxIndex(l);else{let{price_base_coin:P,min_price:M,max_price:S}=e;f=qe.priceToInitializeTickIndex(P==="coin_a"?te(M):te(1).div(S),e.coin_decimals_a,e.coin_decimals_b,l),d=qe.priceToInitializeTickIndex(P==="coin_a"?te(S):te(1).div(M),e.coin_decimals_a,e.coin_decimals_b,l)}let h=[a.coin_type_a,a.coin_type_b],y=[t.object(ee(o).global_config_id),t.object(e.pool_id),t.pure.u32(Number(Be(BigInt(f)))),t.pure.u32(Number(Be(BigInt(d))))];return t.moveCall({target:`${r.published_at}::${ri}::open_position`,typeArguments:h,arguments:y}),t}async collectFeePayload(e,t,i,o){t=t||new se;let r=i||Oe.buildCoinWithBalance(BigInt(0),e.coin_type_a,t),a=o||Oe.buildCoinWithBalance(BigInt(0),e.coin_type_b,t);return this.createCollectFeePayload(e,t,r,a),t}createCollectFeePayload(e,t,i,o){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,l=[e.coin_type_a,e.coin_type_b],f=[t.object(ee(r).global_config_id),t.object(e.pool_id),t.object(e.pos_id),i,o];return t.moveCall({target:`${a.published_at}::${Gt}::collect_fee`,typeArguments:l,arguments:f}),t}async calculateAddLiquidityResultWithPrice(e){let{pool_id:t,slippage:i,refresh_pool_price:o,add_mode_params:r}=e,a=await this.sdk.Pool.getPool(t,o),l=Number(a.tick_spacing),f=0,d=0;if(r.is_full_range)f=Ti.getMinIndex(l),d=Ti.getMaxIndex(l);else{let{price_base_coin:D,min_price:Q,max_price:q}=r;f=qe.priceToInitializeTickIndex(D==="coin_a"?te(Q):te(1).div(q),r.coin_decimals_a,r.coin_decimals_b,l),d=qe.priceToInitializeTickIndex(D==="coin_a"?te(q):te(1).div(Q),r.coin_decimals_a,r.coin_decimals_b,l)}if("liquidity"in e){let{liquidity:D}=e,Q=qe.tickIndexToSqrtPriceX64(f),q=qe.tickIndexToSqrtPriceX64(d),{coin_amount_a:L,coin_amount_b:Lt}=_r.getCoinAmountFromLiquidity(new Qe.default(D),new Qe.default(a.current_sqrt_price),Q,q,!1),H=te(L).mul(1+i).toFixed(0,j.ROUND_UP),$t=te(Lt).mul(1+i).toFixed(0,j.ROUND_UP);return{coin_amount_a:L,coin_amount_b:Lt,coin_amount_limit_a:H,coin_amount_limit_b:$t,liquidity:D,tick_lower:f,tick_upper:d}}let{coin_amount:h,fix_amount_a:y}=e,{coin_amount_limit_a:P,coin_amount_limit_b:M,liquidity_amount:S,coin_amount_a:x,coin_amount_b:U}=_r.estLiquidityAndCoinAmountFromOneAmounts(f,d,new Qe.default(h),y,!0,i,new Qe.default(a.current_sqrt_price));return{coin_amount_a:x,coin_amount_b:U,coin_amount_limit_a:P,coin_amount_limit_b:M,liquidity:S,tick_lower:f,tick_upper:d,fix_amount_a:y}}async addLiquidityWithPricePayload(e,t,i,o){let{pool_id:r,calculate_result:a}=e,{coin_amount_limit_a:l,coin_amount_limit_b:f,liquidity:d,tick_lower:h,tick_upper:y}=a;t=t||new se;let P=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityPayload({delta_liquidity:d,max_amount_a:l,max_amount_b:f,tick_lower:h,tick_upper:y,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:""},t,i,o),t}async createAddLiquidityFixCoinWithPricePayload(e,t,i,o){let{pool_id:r,calculate_result:a}=e,{coin_amount_limit_a:l,coin_amount_limit_b:f,liquidity:d,tick_lower:h,tick_upper:y,fix_amount_a:P,coin_amount_a:M,coin_amount_b:S}=a;if(P===void 0)throw Z("InvalidParams","fix_amount_a is undefined",{[ae.METHOD_NAME]:"addLiquidityFixCoinCoinWithPricePayload",[ae.REQUEST_PARAMS]:e});t=t||new se;let x=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityFixTokenPayload({amount_a:P?M:l,amount_b:P?f:S,slippage:0,fix_amount_a:P,is_open:!0,tick_lower:h,tick_upper:y,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:""},t,i,o),t}createCollectFeeNoSendPayload(e,t,i,o){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,l=[e.coin_type_a,e.coin_type_b],f=[t.object(ee(r).global_config_id),t.object(e.pool_id),t.object(e.pos_id),i,o];return t.moveCall({target:`${a.published_at}::${si}::collect_fee`,typeArguments:l,arguments:f}),t}createCollectFeeAndReturnCoinsPayload(e,t){let{clmm_pool:i}=this.sdk.sdkOptions,o=[e.coin_type_a,e.coin_type_b],r=[t.object(ee(i).global_config_id),t.object(e.pool_id),t.object(e.pos_id),t.pure.bool(!0)],[a,l]=t.moveCall({target:`${i.published_at}::pool::collect_fee`,typeArguments:o,arguments:r});return{fee_a:a,fee_b:l}}};var vn=ce(oe(),1);import{Transaction as bn}from"@mysten/sui/transactions";import{normalizeSuiAddress as xa}from"@mysten/sui/utils";import{CLOCK_ADDRESS as Si,CoinAssist as yn,DETAILS_KEYS as pr,getPackagerConfigs as Xe,MathUtil as Sa,normalizeCoinType as wn,ZERO as De}from"@cetusprotocol/common-sdk";var Ai=class{constructor(e){this._sdk=e,this.growthGlobal=[De,De,De]}get sdk(){return this._sdk}async emissionsEveryDay(e){let i=(await this.sdk.Pool.getPool(e)).rewarder_infos;if(!i)return null;let o=[];for(let r of i){let a=Sa.fromX64(new vn.default(r.emissions_per_second));o.push({emissions:Math.floor(a.toNumber()*60*60*24),coin_type:r.coin_type})}return o}async batchFetchPositionRewarders(e){let t=[];for(let o of e){let r=await this._sdk.Position.getPositionById(o,!1),a=await this._sdk.Pool.getPool(r.pool,!1);t.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(t.length>0){let o=await this.fetchPosRewardersAmount(t);for(let r of o)i[r.position_id]=r.rewarder_amounts;return i}return i}async fetchPositionRewarders(e,t){let i={pool_id:e.id,position_id:t,coin_type_a:e.coin_type_a,coin_type_b:e.coin_type_b,rewarder_types:e.rewarder_infos.map(r=>r.coin_type)};return(await this.fetchPosRewardersAmount([i]))[0].rewarder_amounts}async batchFetchPositionFees(e){return await this._sdk.Position.batchFetchPositionFees(e)}async fetchPosFeeAmount(e){return await this._sdk.Position.fetchPosFeeAmount(e)}buildFetchPosReward(e,t){let{clmm_pool:i,integrate:o}=this.sdk.sdkOptions,r=[e.coin_type_a,e.coin_type_b],a=[t.object(Xe(i).global_config_id),t.object(e.pool_id),t.pure.address(e.position_id),t.object(Si)];t.moveCall({target:`${o.published_at}::${Qt}::fetch_position_rewards`,arguments:a,typeArguments:r})}async fetchPosRewardersAmount(e){let t=new bn;for(let a of e)this.buildFetchPosReward(a,t);let i=await this.sdk.FullClient.sendSimulationTransaction(t,xa("0x0"));i.FailedTransaction!=null&&Z("InvalidConfig",`fetch position rewards error code: ${i.FailedTransaction.status.error?.message??"unknown error"}, please check config and params`,{[pr.METHOD_NAME]:"fetchPosRewardersAmount",[pr.REQUEST_PARAMS]:{params:e}});let o=this.parsedPosRewardData(i.Transaction),r=[];for(let a=0;a<e.length;a+=1){let l=o[e[a].position_id];if(l){let f={pool_id:e[a].pool_id,position_id:e[a].position_id,rewarder_amounts:l.rewarder_amount.map((d,h)=>({amount_owned:d,coin_type:e[a].rewarder_types[h]}))};r.push(f)}}return r}parsedPosRewardData(e){let t={},i=e.events?.filter(o=>o.eventType.includes("fetcher_script::FetchPositionRewardsEvent"));for(let o=0;o<i.length;o+=1){let{bcs:r}=i[o],a=Zo.parse(r),l={position_id:a.position_id,rewarder_amount:a.data};t[a.position_id]=l}return t}async fetchPoolRewardersAmount(e,t){let i=await this.sdk.Pool.getPool(t),o=await this.sdk.Position.getPositionList(e,[t]),r=[];for(let f of o)r.push({pool_id:i.id,position_id:f.pos_object_id,rewarder_types:i.rewarder_infos.map(d=>d.coin_type),coin_type_a:i.coin_type_a,coin_type_b:i.coin_type_b});let a=await this.fetchPosRewardersAmount(r),l=[De,De,De];if(a!=null)for(let f of a)for(let d=0;d<f.rewarder_amounts.length;d+=1)l[d]=l[d].add(new vn.default(f.rewarder_amounts[d].amount_owned));return l}async getPoolLowerAndUpperTicks(e,t){let i=[],o=[];for(let r of t){let a=await this.sdk.Pool.getTickDataByIndex(e,r.tick_lower_index),l=await this.sdk.Pool.getTickDataByIndex(e,r.tick_upper_index);i.push(a),o.push(l)}return[i,o]}async collectRewarderPayload(e){let t=new bn;return t=Wt.createCollectRewarderAndFeeParams(this._sdk,t,e),t}async batchCollectRewardsPayload(e,t,i,o){t=t||new bn;let r={};return e.forEach(a=>{let l=wn(a.coin_type_a),f=wn(a.coin_type_b);if(a.collect_fee){let h=r[l];h==null&&(h=h||yn.buildCoinWithBalance(BigInt(0),l,t),r[l]=h);let y=r[f];y==null&&(y=y||yn.buildCoinWithBalance(BigInt(0),f,t),r[f]=y),t=this._sdk.Position.createCollectFeeNoSendPayload({pool_id:a.pool_id,pos_id:a.pos_id,coin_type_a:a.coin_type_a,coin_type_b:a.coin_type_b},t,h,y)}let d=[];a.rewarder_coin_types.forEach(h=>{let y=wn(h),P=r[h];P===void 0&&(P=yn.buildCoinWithBalance(BigInt(0),y,t),r[y]=P),d.push(P)}),t=this.createCollectRewarderNoSendPayload(a,t,d)}),Object.keys(r).forEach(a=>{let l=r[a];Ro(this.sdk,t,l,a,this.sdk.getSenderAddress())}),t}createCollectRewarderPayload(e,t,i){let{clmm_pool:o,integrate:r}=this.sdk.sdkOptions,a=Xe(o),l=[e.coin_type_a,e.coin_type_b];return e.rewarder_coin_types.forEach((f,d)=>{t&&t.moveCall({target:`${r.published_at}::${Gt}::collect_reward`,typeArguments:[...l,f],arguments:[t.object(a.global_config_id),t.object(e.pool_id),t.object(e.pos_id),t.object(a.global_vault_id),i[d],t.object(Si)]})}),t}createCollectRewarderNoSendPayload(e,t,i){let{clmm_pool:o,integrate:r}=this.sdk.sdkOptions,a=Xe(o),l=[e.coin_type_a,e.coin_type_b];return e.rewarder_coin_types.forEach((f,d)=>{t&&t.moveCall({target:`${r.published_at}::${si}::collect_reward`,typeArguments:[...l,f],arguments:[t.object(a.global_config_id),t.object(e.pool_id),t.object(e.pos_id),t.object(a.global_vault_id),i[d],t.object(Si)]})}),t}createCollectRewarderAndReturnCoinPayload(e,t){let{clmm_pool:i}=this.sdk.sdkOptions,o=Xe(i),r=[e.coin_type_a,e.coin_type_b],a=[t.object(Xe(i).global_config_id),t.object(e.pool_id),t.object(e.pos_id),t.object(o.global_vault_id),t.pure.bool(!0),t.object(Si)];return t.moveCall({target:`${i.published_at}::pool::collect_reward`,typeArguments:[...r,e.rewarder_coin_type],arguments:a})}};var ve=ce(oe(),1);import{Transaction as hr}from"@mysten/sui/transactions";import{normalizeSuiAddress as mr}from"@mysten/sui/utils";import{d as Le,DETAILS_KEYS as he,extractStructTagFromType as gr,TickMath as br,U64_MAX as Aa,ZERO as Ia}from"@cetusprotocol/common-sdk";var uu="amm_swap",du="Pool",Ii=class{constructor(e){this.calculateSingleImpact=(e,t)=>t.minus(e).div(t).mul(100);this._sdk=e}get sdk(){return this._sdk}calculateSwapFee(e){let t=Le(0);return e.forEach(i=>{let o=i.base_paths.length;if(o>0){let r=i.base_paths[0],a=r.label==="Cetus"?new j(r.fee_rate).div(10**6):new j(r.fee_rate).div(10**9),l=Le(r.input_amount).div(10**r.from_decimal).mul(a);if(t=t.add(l),o>1){let f=i.base_paths[1],d=r.direction?r.current_price:new j(1).div(r.current_price),h=f.direction?f.current_price:new j(1).div(f.current_price),y=f.label==="Cetus"?new j(f.fee_rate).div(10**6):new j(f.fee_rate).div(10**9),M=Le(f.output_amount).div(10**f.to_decimal).mul(y).div(d.mul(h));t=t.add(M)}}}),t.toString()}calculateSwapPriceImpact(e){let t=Le(0);return e.forEach(i=>{let o=i.base_paths.length;if(o===1){let r=i.base_paths[0],a=Le(r.output_amount).div(10**r.to_decimal),l=Le(r.input_amount).div(10**r.from_decimal),f=a.div(l),d=r.direction?new j(r.current_price):new j(1).div(r.current_price);t=t.add(this.calculateSingleImpact(f,d))}if(o===2){let r=i.base_paths[0],a=i.base_paths[1],l=r.direction?new j(r.current_price):new j(1).div(r.current_price),f=a.direction?new j(a.current_price):new j(1).div(a.current_price),d=l.mul(f),h=new j(a.output_amount).div(10**a.to_decimal),y=new j(r.input_amount).div(10**r.from_decimal),P=h.div(y);t=t.add(this.calculateSingleImpact(P,d))}}),t.toString()}async preSwapWithMultiPool(e){let{integrate:t}=this.sdk.sdkOptions,i=new hr,o=[e.coin_type_a,e.coin_type_b];for(let d=0;d<e.pool_ids.length;d+=1){let h=[i.object(e.pool_ids[d]),i.pure.bool(e.a2b),i.pure.bool(e.by_amount_in),i.pure.u64(e.amount)];i.moveCall({target:`${t.published_at}::${Qt}::calculate_swap_result`,arguments:h,typeArguments:o})}let r=await this.sdk.FullClient.sendSimulationTransaction(i,mr("0x0"));r.FailedTransaction!=null&&Z("InvalidConfig",`pre swap with multi pools error code: ${r.FailedTransaction.status.error?.message??"unknown error"}, please check config and params`,{[he.METHOD_NAME]:"preSwapWithMultiPool",[he.REQUEST_PARAMS]:{params:e}});let a=r.Transaction?.events?.filter(d=>gr(d.eventType).name==="CalculatedSwapResultEvent");if(a.length===0)return null;a.length!==e.pool_ids.length&&Z("ParamsLengthNotEqual","valueData.length !== params.pools.length",{[he.METHOD_NAME]:"preSwapWithMultiPool",[he.REQUEST_PARAMS]:{params:e}});let l=e.by_amount_in?Ia:Aa,f=0;for(let d=0;d<a.length;d+=1){let h=fi.parse(a[d].bcs).data;if(!h.is_exceed)if(e.by_amount_in){let y=new ve.default(h.amount_out);y.gt(l)&&(f=d,l=y)}else{let y=new ve.default(h.amount_out);y.lt(l)&&(f=d,l=y)}}return this.transformSwapWithMultiPoolData({pool_address:e.pool_ids[f],a2b:e.a2b,by_amount_in:e.by_amount_in,amount:e.amount,coin_type_a:e.coin_type_a,coin_type_b:e.coin_type_b},fi.parse(a[f].bcs).data)}async preSwap(e){let{integrate:t}=this.sdk.sdkOptions,i=new hr,o=[e.coin_type_a,e.coin_type_b],r=[i.object(e.pool.id),i.pure.bool(e.a2b),i.pure.bool(e.by_amount_in),i.pure.u64(e.amount)];i.moveCall({target:`${t.published_at}::${Qt}::calculate_swap_result`,arguments:r,typeArguments:o});let a=await this.sdk.FullClient.sendSimulationTransaction(i,mr("0x0"));if(a.FailedTransaction!=null)return Z("InvalidConfig",`preSwap error code: ${a.FailedTransaction.status.error?.message??"unknown error"}, please check config and params`,{[he.METHOD_NAME]:"preSwap",[he.REQUEST_PARAMS]:{params:e}});let l=a.Transaction?.events?.filter(f=>gr(f.eventType).name==="CalculatedSwapResultEvent");return l.length===0?Z("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[he.METHOD_NAME]:"preSwap",[he.REQUEST_PARAMS]:{params:e}}):this.transformSwapData(e,l[0].bcs)}transformSwapData(e,t){let i=fi.parse(t).data,o=i.amount_in&&i.fee_amount?new ve.default(i.amount_in).add(new ve.default(i.fee_amount)).toString():"";return{pool_address:e.pool.id,current_sqrt_price:e.current_sqrt_price,estimated_amount_in:o,estimated_amount_out:i.amount_out,estimated_end_sqrt_price:i.after_sqrt_price,estimated_fee_amount:i.fee_amount,is_exceed:i.is_exceed,amount:e.amount,a2b:e.a2b,by_amount_in:e.by_amount_in}}transformSwapWithMultiPoolData(e,t){let i=t.amount_in&&t.fee_amount?new ve.default(t.amount_in).add(new ve.default(t.fee_amount)).toString():"";return{pool_address:e.pool_address,estimated_amount_in:i,estimated_amount_out:t.amount_out,estimated_end_sqrt_price:t.after_sqrt_price,estimated_start_sqrt_price:t.step_results[0].current_sqrt_price,estimated_fee_amount:t.fee_amount,is_exceed:t.is_exceed,amount:e.amount,a2b:e.a2b,by_amount_in:e.by_amount_in}}calculateRates(e){let{current_pool:t}=e,i=To(t),o;e.a2b?o=e.swap_ticks.sort((P,M)=>M.index-P.index):o=e.swap_ticks.sort((P,M)=>P.index-M.index);let r=xo(e.a2b,e.by_amount_in,e.amount,i,o),a=!1;e.by_amount_in?a=r.amount_in.lt(e.amount):a=r.amount_out.lt(e.amount);let l=ue.getDefaultSqrtPriceLimit(e.a2b);e.a2b&&r.next_sqrt_price.lt(l)&&(a=!0),!e.a2b&&r.next_sqrt_price.gt(l)&&(a=!0);let f=0;r.cross_tick_num>6&&r.cross_tick_num<40&&(f=22e3*(r.cross_tick_num-6)),r.cross_tick_num>40&&(a=!0);let d=br.sqrtPriceX64ToPrice(i.current_sqrt_price,e.decimals_a,e.decimals_b).toNumber(),h=br.sqrtPriceX64ToPrice(r.next_sqrt_price,e.decimals_a,e.decimals_b).toNumber(),y=Math.abs(d-h)/d*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:f,amount:e.amount,a2b:e.a2b,by_amount_in:e.by_amount_in,price_impact_pct:y}}async createSwapPayload(e){return ue.buildSwapTransaction(this.sdk,e)}async createSwapWithoutTransferCoinsPayload(e){return ue.buildSwapTransactionWithoutTransferCoins(this.sdk,e)}};import{CLOCK_ADDRESS as Ra,CoinAssist as Ea,DETAILS_KEYS as Pe,getPackagerConfigs as Pn}from"@cetusprotocol/common-sdk";import{Transaction as yr}from"@mysten/sui/transactions";import{normalizeSuiAddress as ja}from"@mysten/sui/utils";var Ri=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getClmmVestInfoList(){let{clmm_vest:e}=this._sdk.sdkOptions;if(e===void 0)return Z("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[Pe.METHOD_NAME]:"getClmmVestInfo"});let t=[];try{let i=`${e.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,{json:!0})).forEach(l=>{let f=Xt.parseClmmVestInfo(l),d=`${f.id}-ClmmVestInfo`;this._sdk.updateCache(d,f),t.push(f)}),t}catch(i){Bt("ClmmVestFetchError",i,{[Pe.METHOD_NAME]:"getClmmVestInfoList"})}return[]}async getClmmVestInfo(e=!0){let{clmm_vest:t}=this._sdk.sdkOptions;if(t===void 0)return Z("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[Pe.METHOD_NAME]:"getClmmVestInfo"});let{clmm_vest_id:i}=Pn(t),o=`${i}-ClmmVestInfo`,r=this._sdk.getCache(o,e);if(r)return r;try{let a=await this._sdk.FullClient.getObject({objectId:i,include:{json:!0}}),l=Xt.parseClmmVestInfo(a.object);return this._sdk.updateCache(o,l),l}catch(a){return Bt("ClmmVestFetchError",a,{[Pe.METHOD_NAME]:"getClmmVestInfo",[Pe.REQUEST_PARAMS]:{clmm_vest_id:i}})}}async getPositionVesting(e){let{clmm_vest:t}=this._sdk.sdkOptions;if(t===void 0)return Z("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[Pe.METHOD_NAME]:"getPositionVesting"});if(e.length===0)return[];let{clmm_vest_id:i}=Pn(t),o=new yr;e.forEach(l=>{let{clmm_pool_id:f,coin_type_a:d,coin_type_b:h,clmm_position_ids:y}=l;o.moveCall({package:t.published_at,module:"clmm_vester",function:"get_positions_vesting",typeArguments:[d,h],arguments:[o.object(i),o.object(f),o.pure.vector("id",y)]})});let r=await this.sdk.FullClient.sendSimulationTransaction(o,ja("0x0")),a=[];return r.Transaction?.events?.forEach(l=>{if(l.eventType.includes("clmm_vester::GetPositionsVestingEvent")){let f=Ho.parse(l.bcs).data;a.push(...f.map(d=>Xt.parsePositionVesting(d)))}}),a}buildRedeemPayload(e,t){let{clmm_vest:i}=this._sdk.sdkOptions;if(i===void 0)return Z("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[Pe.METHOD_NAME]:"buildRedeemVestPayload"});let{versioned_id:o,clmm_vest_id:r,cetus_coin_type:a}=Pn(i);return t=t||new yr,e.forEach(l=>{let{clmm_pool_id:f,clmm_position_id:d,coin_type_a:h,coin_type_b:y,period:P}=l,M=t.moveCall({package:i.published_at,module:"clmm_vester",function:"redeem",arguments:[t.object(o),t.object(r),t.object(f),typeof d=="string"?t.object(d):d,t.pure.u16(P),t.object(Ra)],typeArguments:[h,y]}),S=Ea.fromBalance(M,a,t);t.transferObjects([S],this._sdk.getSenderAddress())}),t}};var Ei=class n extends Fa{constructor(e){super(e),this._swap=new Ii(this),this._pool=new mi(this),this._position=new xi(this),this._config=new ni(this),this._rewarder=new Ai(this),this._vest=new Ri(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(e){let{env:t="mainnet"}=e;return t==="mainnet"?n.createCustomSDK({...Bo,...e}):n.createCustomSDK({...qo,...e})}static createCustomSDK(e){return new n(e)}};var Je=ce(oe(),1);import{MathUtil as $e,TickMath as wr}from"@cetusprotocol/common-sdk";var Na=new Je.default(365),Ba=new Je.default(24),Oa=new Je.default(3600),qa=new Je.default(.5);function Lu(n,e,t,i){return Na.mul(Ba).mul(Oa).mul(qa).mul(n.mul(e).add(t).div(i))}function Da(n,e,t,i,o,r){let a=new j(n.toString()).div(new j(10**t)),l=new j(e.toString()).div(new j(10**i));return a.mul(o).add(l.mul(r))}function $u(n,e,t,i,o,r,a,l,f,d,h,y,P,M,S,x,U,D,Q,q,L,Lt,H,$t){let ie=new j(y),zt=new j(P),ne=new j(x),$=new j(U),Ot=new j(D),w=new j(Q),s=new j(q),u=new j(L),_=new j(Lt),p=new j(H),g=new j($t),v=wr.tickIndexToSqrtPriceX64(e),C=wr.tickIndexToSqrtPriceX64(t),m=$e.toX64Decimal($e.fromX64(v)).round(),c=$e.toX64Decimal($e.fromX64(C)).round(),b=$e.toX64Decimal($e.fromX64(i)).round(),T,I=ie.mul(new j(10**r)).mul(c.mul(m)).div(c.sub(m)).round(),R=zt.mul(new j(10**a)).div(c.sub(m)).round();n<e?T=I:n>t?T=R:T=j.min(I,R);let O=T.mul(b.sub(m)),K=T.mul(c.sub(b)).div(b.mul(c)).div(new j(10**r)).mul(s).add(O.div(new j(10**a).mul(u))),Ht=Da(M,S,r,a,s,u),W=K.div(Ht),G=T.eq(new j(0))?new j(0):new j(h/1e4).mul(ne).mul(new j(T.toString()).div(new j(o.toString()).add(new j(T.toString())))).div(K),Ce=W.eq(new j(0))?new j(0):W.mul(new j(36500/7)).div(K),X=$.div(new j(10**l)).mul(_).mul(Ce),J=Ot.div(new j(10**f)).mul(p).mul(Ce),Ye=w.div(new j(10**d)).mul(g).mul(Ce);return{fee_apr:G,pos_rewarder_0_apr:X,pos_rewarder_1_apr:J,pos_rewarder_2_apr:Ye}}function Uu(n,e,t,i){let o=Math.max(n,t),a=Math.min(e,i)-o,l=e-n,f=i-t,d=new j(l.toString()),h=new j(f.toString()),y=new j(a.toString()),P=new j("0");return a<0?P=new j("0"):l===a?P=h.div(y):f===a?P=y.div(d):P=y.mul(y).div(h).div(d),P}var Hu=Ei;export{uu as AMM_SWAP_MODULE,hs as BPS,fi as CalculatedSwapResultEventRaw,Ei as CetusClmmSDK,Se as ClmmError,Ya as ClmmExpectSwapModule,Qt as ClmmFetcherModule,ri as ClmmIntegratePoolModule,Gt as ClmmIntegratePoolV2Module,si as ClmmIntegratePoolV3Module,me as ClmmIntegrateRouterModule,mo as ClmmIntegrateRouterWithPartnerModule,go as ClmmIntegrateUtilsModule,sn as ClmmPartnerModule,zr as ClmmPool,po as ClmmPositionStatus,Zr as Coin,Ve as ConfigErrorCode,ni as ConfigModule,ec as DeepbookClobV2Module,tc as DeepbookCustodianV2Module,ic as DeepbookEndpointsV2Module,zo as FetchPositionFeesEventRaw,Zo as FetchPositionRewardsEventRaw,Ko as FetchPositionsEventRaw,Wo as FetchTicksResultEventRaw,Ho as GetPositionsVestingEventRaw,Xr as LaunchpadPool,Hr as MediaInfo,Vo as NodeIDPositionInfo,du as POOL_STRUCT,lo as PartnerErrorCode,oi as PoolErrorCode,mi as PoolModule,rn as PositionErrorCode,Uo as PositionInfoRaw,xi as PositionModule,ws as PositionRewardRaw,Wt as PositionUtils,Ai as RewarderModule,Yr as RouterErrorCode,ao as SwapErrorCode,Ii as SwapModule,ue as SwapUtils,dn as TickRaw,fo as TypesErrorCode,uo as UtilsErrorCode,co as VestErrorCode,Ri as VestModule,Xt as VestUtils,Uc as buildClmmPositionName,li as buildPool,Io as buildPoolTransactionInfo,ui as buildPosition,We as buildPositionInfo,Ao as buildPositionTransactionInfo,ln as buildTickData,un as buildTickDataByEvent,Ro as buildTransferCoin,di as buildTransferCoinToSender,Bo as clmmMainnet,qo as clmmTestnet,xo as computeSwap,ls as computeSwapStep,Hu as default,Lu as estPoolAPR,$u as estPositionAPRWithDeltaMethod,Uu as estPositionAPRWithMultiMethod,Oo as eventMainnetContractMaps,Do as eventTestnetContractMaps,fc as findAdjustCoin,nc as getDefaultSuiInputType,Pc as getLowerSqrtPriceFromCoinA,Mc as getLowerSqrtPriceFromCoinB,Cc as getUpperSqrtPriceFromCoinA,kc as getUpperSqrtPriceFromCoinB,Bt as handleError,Z as handleMessageError,Ic as newBits,ho as poolFilterEvenTypes,_o as poolLiquiditySnapshotType,To as transClmmpoolDataWithoutTicks};
2
2
  /*! Bundled license information:
3
3
 
4
4
  decimal.js/decimal.mjs: