@cetusprotocol/sui-clmm-sdk 1.4.1 → 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/README.md +4 -0
- package/dist/index.d.ts +304 -23
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var fr=Object.create;var Fn=Object.defineProperty;var _r=Object.getOwnPropertyDescriptor;var pr=Object.getOwnPropertyNames;var hr=Object.getPrototypeOf,mr=Object.prototype.hasOwnProperty;var gr=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var br=(o,e)=>()=>(e||o((e={exports:{}}).exports,e),e.exports);var yr=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of pr(e))!mr.call(o,n)&&n!==t&&Fn(o,n,{get:()=>e[n],enumerable:!(i=_r(e,n))||i.enumerable});return o};var re=(o,e,t)=>(t=o!=null?fr(hr(o)):{},yr(e||!o||!o.__esModule?Fn(t,"default",{value:o,enumerable:!0}):t,o));var ie=br((uo,rn)=>{"use strict";(function(o,e){"use strict";function t(y,s){if(!y)throw new Error(s||"Assertion failed")}function i(y,s){y.super_=s;var u=function(){};u.prototype=s.prototype,y.prototype=new u,y.prototype.constructor=y}function n(y,s,u){if(n.isBN(y))return y;this.negative=0,this.words=null,this.length=0,this.red=null,y!==null&&((s==="le"||s==="be")&&(u=s,s=10),this._init(y||0,s||10,u||"be"))}typeof o=="object"?o.exports=n:e.BN=n,n.BN=n,n.wordSize=26;var r;try{typeof window<"u"&&typeof window.Buffer<"u"?r=window.Buffer:r=gr("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),t(u===(u|0)&&u>=2&&u<=36),s=s.toString().replace(/\s+/g,"");var h=0;s[0]==="-"&&(h++,this.negative=1),h<s.length&&(u===16?this._parseHex(s,h,_):(this._parseBase(s,u,h),_==="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):(t(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(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 h=0;h<this.length;h++)this.words[h]=0;var g,v,C=0;if(_==="be")for(h=s.length-1,g=0;h>=0;h-=3)v=s[h]|s[h-1]<<8|s[h-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(h=0,g=0;h<s.length;h+=3)v=s[h]|s[h+1]<<8|s[h+2]<<16,this.words[g]|=v<<C&67108863,this.words[g+1]=v>>>26-C&67108863,C+=24,C>=26&&(C-=26,g++);return this._strip()};function a(y,s){var u=y.charCodeAt(s);if(u>=48&&u<=57)return u-48;if(u>=65&&u<=70)return u-55;if(u>=97&&u<=102)return u-87;t(!1,"Invalid character in "+y)}function l(y,s,u){var _=a(y,u);return u-1>=s&&(_|=a(y,u-1)<<4),_}n.prototype._parseHex=function(s,u,_){this.length=Math.ceil((s.length-u)/6),this.words=new Array(this.length);for(var h=0;h<this.length;h++)this.words[h]=0;var g=0,v=0,C;if(_==="be")for(h=s.length-1;h>=u;h-=2)C=l(s,u,h)<<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(h=m%2===0?u+1:u;h<s.length;h+=2)C=l(s,u,h)<<g,this.words[v]|=C&67108863,g>=18?(g-=18,v+=1,this.words[v]|=C>>>26):g+=8}this._strip()};function d(y,s,u,_){for(var h=0,g=0,v=Math.min(y.length,u),C=s;C<v;C++){var m=y.charCodeAt(C)-48;h*=_,m>=49?g=m-49+10:m>=17?g=m-17+10:g=m,t(m>=0&&g<_,"Invalid character"),h+=g}return h}n.prototype._parseBase=function(s,u,_){this.words=[0],this.length=1;for(var h=0,g=1;g<=67108863;g*=u)h++;h--,g=g/u|0;for(var v=s.length-_,C=v%h,m=Math.min(v,v-C)+_,c=0,b=_;b<m;b+=h)c=d(s,b,b+h,u),this.imuln(g),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c);if(C!==0){var T=1;for(c=d(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()},n.prototype.copy=function(s){s.words=new Array(this.length);for(var u=0;u<this.length;u++)s.words[u]=this.words[u];s.length=this.length,s.negative=this.negative,s.red=this.red};function f(y,s){y.words=s.words,y.length=s.length,y.negative=s.negative,y.red=s.red}if(n.prototype._move=function(s){f(s,this)},n.prototype.clone=function(){var s=new n(null);return this.copy(s),s},n.prototype._expand=function(s){for(;this.length<s;)this.words[this.length++]=0;return this},n.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},n.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{n.prototype[Symbol.for("nodejs.util.inspect.custom")]=p}catch{n.prototype.inspect=p}else n.prototype.inspect=p;function p(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var w=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],P=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],M=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];n.prototype.toString=function(s,u){s=s||10,u=u|0||1;var _;if(s===16||s==="hex"){_="";for(var h=0,g=0,v=0;v<this.length;v++){var C=this.words[v],m=((C<<h|g)&16777215).toString(16);g=C>>>24-h&16777215,h+=2,h>=26&&(h-=26,v--),g!==0||v!==this.length-1?_=w[6-m.length]+m+_:_=m+_}for(g!==0&&(_=g.toString(16)+_);_.length%u!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}if(s===(s|0)&&s>=2&&s<=36){var c=P[s],b=M[s];_="";var T=this.clone();for(T.negative=0;!T.isZero();){var x=T.modrn(b).toString(s);T=T.idivn(b),T.isZero()?_=x+_:_=w[c-x.length]+x+_}for(this.isZero()&&(_="0"+_);_.length%u!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}t(!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&&t(!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 A=function(s,u){return s.allocUnsafe?s.allocUnsafe(u):new s(u)};n.prototype.toArrayLike=function(s,u,_){this._strip();var h=this.byteLength(),g=_||Math.max(1,h);t(h<=g,"byte array longer than desired length"),t(g>0,"Requested array length <= 0");var v=A(s,g),C=u==="le"?"LE":"BE";return this["_toArrayLike"+C](v,h),v},n.prototype._toArrayLikeLE=function(s,u){for(var _=0,h=0,g=0,v=0;g<this.length;g++){var C=this.words[g]<<v|h;s[_++]=C&255,_<s.length&&(s[_++]=C>>8&255),_<s.length&&(s[_++]=C>>16&255),v===6?(_<s.length&&(s[_++]=C>>24&255),h=0,v=0):(h=C>>>24,v+=2)}if(_<s.length)for(s[_++]=h;_<s.length;)s[_++]=0},n.prototype._toArrayLikeBE=function(s,u){for(var _=s.length-1,h=0,g=0,v=0;g<this.length;g++){var C=this.words[g]<<v|h;s[_--]=C&255,_>=0&&(s[_--]=C>>8&255),_>=0&&(s[_--]=C>>16&255),v===6?(_>=0&&(s[_--]=C>>24&255),h=0,v=0):(h=C>>>24,v+=2)}if(_>=0)for(s[_--]=h;_>=0;)s[_--]=0},Math.clz32?n.prototype._countBits=function(s){return 32-Math.clz32(s)}:n.prototype._countBits=function(s){var u=s,_=0;return u>=4096&&(_+=13,u>>>=13),u>=64&&(_+=7,u>>>=7),u>=8&&(_+=4,u>>>=4),u>=2&&(_+=2,u>>>=2),_+u},n.prototype._zeroBits=function(s){if(s===0)return 26;var u=s,_=0;return(u&8191)===0&&(_+=13,u>>>=13),(u&127)===0&&(_+=7,u>>>=7),(u&15)===0&&(_+=4,u>>>=4),(u&3)===0&&(_+=2,u>>>=2),(u&1)===0&&_++,_},n.prototype.bitLength=function(){var s=this.words[this.length-1],u=this._countBits(s);return(this.length-1)*26+u};function S(y){for(var s=new Array(y.bitLength()),u=0;u<s.length;u++){var _=u/26|0,h=u%26;s[u]=y.words[_]>>>h&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 t((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 t((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 h=0;h<_.length;h++)this.words[h]=u.words[h]^_.words[h];if(this!==u)for(;h<u.length;h++)this.words[h]=u.words[h];return this.length=u.length,this._strip()},n.prototype.ixor=function(s){return t((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){t(typeof s=="number"&&s>=0);var u=Math.ceil(s/26)|0,_=s%26;this._expand(u),_>0&&u--;for(var h=0;h<u;h++)this.words[h]=~this.words[h]&67108863;return _>0&&(this.words[h]=~this.words[h]&67108863>>26-_),this._strip()},n.prototype.notn=function(s){return this.clone().inotn(s)},n.prototype.setn=function(s,u){t(typeof s=="number"&&s>=0);var _=s/26|0,h=s%26;return this._expand(_+1),u?this.words[_]=this.words[_]|1<<h:this.words[_]=this.words[_]&~(1<<h),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 _,h;this.length>s.length?(_=this,h=s):(_=s,h=this);for(var g=0,v=0;v<h.length;v++)u=(_.words[v]|0)+(h.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 h,g;_>0?(h=this,g=s):(h=s,g=this);for(var v=0,C=0;C<g.length;C++)u=(h.words[C]|0)-(g.words[C]|0)+v,v=u>>26,this.words[C]=u&67108863;for(;v!==0&&C<h.length;C++)u=(h.words[C]|0)+v,v=u>>26,this.words[C]=u&67108863;if(v===0&&C<h.length&&h!==this)for(;C<h.length;C++)this.words[C]=h.words[C];return this.length=Math.max(this.length,C),h!==this&&(this.negative=1),this._strip()},n.prototype.sub=function(s){return this.clone().isub(s)};function $(y,s,u){u.negative=s.negative^y.negative;var _=y.length+s.length|0;u.length=_,_=_-1|0;var h=y.words[0]|0,g=s.words[0]|0,v=h*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,x=Math.min(c,s.length-1),I=Math.max(0,c-y.length+1);I<=x;I++){var B=c-I|0;h=y.words[B]|0,g=s.words[I]|0,v=h*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 F=function(s,u,_){var h=s.words,g=u.words,v=_.words,C=0,m,c,b,T=h[0]|0,x=T&8191,I=T>>>13,B=h[1]|0,U=B&8191,V=B>>>13,Ht=h[2]|0,K=Ht&8191,Z=Ht>>>13,ve=h[3]|0,G=ve&8191,Q=ve>>>13,Qe=h[4]|0,X=Qe&8191,Y=Qe>>>13,Cn=h[5]|0,tt=Cn&8191,et=Cn>>>13,Mn=h[6]|0,it=Mn&8191,nt=Mn>>>13,kn=h[7]|0,ot=kn&8191,rt=kn>>>13,Tn=h[8]|0,st=Tn&8191,at=Tn>>>13,An=h[9]|0,ct=An&8191,lt=An>>>13,Sn=g[0]|0,ut=Sn&8191,dt=Sn>>>13,xn=g[1]|0,ft=xn&8191,_t=xn>>>13,In=g[2]|0,pt=In&8191,ht=In>>>13,On=g[3]|0,mt=On&8191,gt=On>>>13,En=g[4]|0,bt=En&8191,yt=En>>>13,Rn=g[5]|0,wt=Rn&8191,vt=Rn>>>13,jn=g[6]|0,Pt=jn&8191,Ct=jn>>>13,Nn=g[7]|0,Mt=Nn&8191,kt=Nn>>>13,Bn=g[8]|0,Tt=Bn&8191,At=Bn>>>13,Dn=g[9]|0,St=Dn&8191,xt=Dn>>>13;_.negative=s.negative^u.negative,_.length=19,m=Math.imul(x,ut),c=Math.imul(x,dt),c=c+Math.imul(I,ut)|0,b=Math.imul(I,dt);var Ei=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ei>>>26)|0,Ei&=67108863,m=Math.imul(U,ut),c=Math.imul(U,dt),c=c+Math.imul(V,ut)|0,b=Math.imul(V,dt),m=m+Math.imul(x,ft)|0,c=c+Math.imul(x,_t)|0,c=c+Math.imul(I,ft)|0,b=b+Math.imul(I,_t)|0;var Ri=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ri>>>26)|0,Ri&=67108863,m=Math.imul(K,ut),c=Math.imul(K,dt),c=c+Math.imul(Z,ut)|0,b=Math.imul(Z,dt),m=m+Math.imul(U,ft)|0,c=c+Math.imul(U,_t)|0,c=c+Math.imul(V,ft)|0,b=b+Math.imul(V,_t)|0,m=m+Math.imul(x,pt)|0,c=c+Math.imul(x,ht)|0,c=c+Math.imul(I,pt)|0,b=b+Math.imul(I,ht)|0;var ji=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(ji>>>26)|0,ji&=67108863,m=Math.imul(G,ut),c=Math.imul(G,dt),c=c+Math.imul(Q,ut)|0,b=Math.imul(Q,dt),m=m+Math.imul(K,ft)|0,c=c+Math.imul(K,_t)|0,c=c+Math.imul(Z,ft)|0,b=b+Math.imul(Z,_t)|0,m=m+Math.imul(U,pt)|0,c=c+Math.imul(U,ht)|0,c=c+Math.imul(V,pt)|0,b=b+Math.imul(V,ht)|0,m=m+Math.imul(x,mt)|0,c=c+Math.imul(x,gt)|0,c=c+Math.imul(I,mt)|0,b=b+Math.imul(I,gt)|0;var Ni=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ni>>>26)|0,Ni&=67108863,m=Math.imul(X,ut),c=Math.imul(X,dt),c=c+Math.imul(Y,ut)|0,b=Math.imul(Y,dt),m=m+Math.imul(G,ft)|0,c=c+Math.imul(G,_t)|0,c=c+Math.imul(Q,ft)|0,b=b+Math.imul(Q,_t)|0,m=m+Math.imul(K,pt)|0,c=c+Math.imul(K,ht)|0,c=c+Math.imul(Z,pt)|0,b=b+Math.imul(Z,ht)|0,m=m+Math.imul(U,mt)|0,c=c+Math.imul(U,gt)|0,c=c+Math.imul(V,mt)|0,b=b+Math.imul(V,gt)|0,m=m+Math.imul(x,bt)|0,c=c+Math.imul(x,yt)|0,c=c+Math.imul(I,bt)|0,b=b+Math.imul(I,yt)|0;var Bi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Bi>>>26)|0,Bi&=67108863,m=Math.imul(tt,ut),c=Math.imul(tt,dt),c=c+Math.imul(et,ut)|0,b=Math.imul(et,dt),m=m+Math.imul(X,ft)|0,c=c+Math.imul(X,_t)|0,c=c+Math.imul(Y,ft)|0,b=b+Math.imul(Y,_t)|0,m=m+Math.imul(G,pt)|0,c=c+Math.imul(G,ht)|0,c=c+Math.imul(Q,pt)|0,b=b+Math.imul(Q,ht)|0,m=m+Math.imul(K,mt)|0,c=c+Math.imul(K,gt)|0,c=c+Math.imul(Z,mt)|0,b=b+Math.imul(Z,gt)|0,m=m+Math.imul(U,bt)|0,c=c+Math.imul(U,yt)|0,c=c+Math.imul(V,bt)|0,b=b+Math.imul(V,yt)|0,m=m+Math.imul(x,wt)|0,c=c+Math.imul(x,vt)|0,c=c+Math.imul(I,wt)|0,b=b+Math.imul(I,vt)|0;var Di=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Di>>>26)|0,Di&=67108863,m=Math.imul(it,ut),c=Math.imul(it,dt),c=c+Math.imul(nt,ut)|0,b=Math.imul(nt,dt),m=m+Math.imul(tt,ft)|0,c=c+Math.imul(tt,_t)|0,c=c+Math.imul(et,ft)|0,b=b+Math.imul(et,_t)|0,m=m+Math.imul(X,pt)|0,c=c+Math.imul(X,ht)|0,c=c+Math.imul(Y,pt)|0,b=b+Math.imul(Y,ht)|0,m=m+Math.imul(G,mt)|0,c=c+Math.imul(G,gt)|0,c=c+Math.imul(Q,mt)|0,b=b+Math.imul(Q,gt)|0,m=m+Math.imul(K,bt)|0,c=c+Math.imul(K,yt)|0,c=c+Math.imul(Z,bt)|0,b=b+Math.imul(Z,yt)|0,m=m+Math.imul(U,wt)|0,c=c+Math.imul(U,vt)|0,c=c+Math.imul(V,wt)|0,b=b+Math.imul(V,vt)|0,m=m+Math.imul(x,Pt)|0,c=c+Math.imul(x,Ct)|0,c=c+Math.imul(I,Pt)|0,b=b+Math.imul(I,Ct)|0;var Fi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Fi>>>26)|0,Fi&=67108863,m=Math.imul(ot,ut),c=Math.imul(ot,dt),c=c+Math.imul(rt,ut)|0,b=Math.imul(rt,dt),m=m+Math.imul(it,ft)|0,c=c+Math.imul(it,_t)|0,c=c+Math.imul(nt,ft)|0,b=b+Math.imul(nt,_t)|0,m=m+Math.imul(tt,pt)|0,c=c+Math.imul(tt,ht)|0,c=c+Math.imul(et,pt)|0,b=b+Math.imul(et,ht)|0,m=m+Math.imul(X,mt)|0,c=c+Math.imul(X,gt)|0,c=c+Math.imul(Y,mt)|0,b=b+Math.imul(Y,gt)|0,m=m+Math.imul(G,bt)|0,c=c+Math.imul(G,yt)|0,c=c+Math.imul(Q,bt)|0,b=b+Math.imul(Q,yt)|0,m=m+Math.imul(K,wt)|0,c=c+Math.imul(K,vt)|0,c=c+Math.imul(Z,wt)|0,b=b+Math.imul(Z,vt)|0,m=m+Math.imul(U,Pt)|0,c=c+Math.imul(U,Ct)|0,c=c+Math.imul(V,Pt)|0,b=b+Math.imul(V,Ct)|0,m=m+Math.imul(x,Mt)|0,c=c+Math.imul(x,kt)|0,c=c+Math.imul(I,Mt)|0,b=b+Math.imul(I,kt)|0;var qi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(qi>>>26)|0,qi&=67108863,m=Math.imul(st,ut),c=Math.imul(st,dt),c=c+Math.imul(at,ut)|0,b=Math.imul(at,dt),m=m+Math.imul(ot,ft)|0,c=c+Math.imul(ot,_t)|0,c=c+Math.imul(rt,ft)|0,b=b+Math.imul(rt,_t)|0,m=m+Math.imul(it,pt)|0,c=c+Math.imul(it,ht)|0,c=c+Math.imul(nt,pt)|0,b=b+Math.imul(nt,ht)|0,m=m+Math.imul(tt,mt)|0,c=c+Math.imul(tt,gt)|0,c=c+Math.imul(et,mt)|0,b=b+Math.imul(et,gt)|0,m=m+Math.imul(X,bt)|0,c=c+Math.imul(X,yt)|0,c=c+Math.imul(Y,bt)|0,b=b+Math.imul(Y,yt)|0,m=m+Math.imul(G,wt)|0,c=c+Math.imul(G,vt)|0,c=c+Math.imul(Q,wt)|0,b=b+Math.imul(Q,vt)|0,m=m+Math.imul(K,Pt)|0,c=c+Math.imul(K,Ct)|0,c=c+Math.imul(Z,Pt)|0,b=b+Math.imul(Z,Ct)|0,m=m+Math.imul(U,Mt)|0,c=c+Math.imul(U,kt)|0,c=c+Math.imul(V,Mt)|0,b=b+Math.imul(V,kt)|0,m=m+Math.imul(x,Tt)|0,c=c+Math.imul(x,At)|0,c=c+Math.imul(I,Tt)|0,b=b+Math.imul(I,At)|0;var Li=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Li>>>26)|0,Li&=67108863,m=Math.imul(ct,ut),c=Math.imul(ct,dt),c=c+Math.imul(lt,ut)|0,b=Math.imul(lt,dt),m=m+Math.imul(st,ft)|0,c=c+Math.imul(st,_t)|0,c=c+Math.imul(at,ft)|0,b=b+Math.imul(at,_t)|0,m=m+Math.imul(ot,pt)|0,c=c+Math.imul(ot,ht)|0,c=c+Math.imul(rt,pt)|0,b=b+Math.imul(rt,ht)|0,m=m+Math.imul(it,mt)|0,c=c+Math.imul(it,gt)|0,c=c+Math.imul(nt,mt)|0,b=b+Math.imul(nt,gt)|0,m=m+Math.imul(tt,bt)|0,c=c+Math.imul(tt,yt)|0,c=c+Math.imul(et,bt)|0,b=b+Math.imul(et,yt)|0,m=m+Math.imul(X,wt)|0,c=c+Math.imul(X,vt)|0,c=c+Math.imul(Y,wt)|0,b=b+Math.imul(Y,vt)|0,m=m+Math.imul(G,Pt)|0,c=c+Math.imul(G,Ct)|0,c=c+Math.imul(Q,Pt)|0,b=b+Math.imul(Q,Ct)|0,m=m+Math.imul(K,Mt)|0,c=c+Math.imul(K,kt)|0,c=c+Math.imul(Z,Mt)|0,b=b+Math.imul(Z,kt)|0,m=m+Math.imul(U,Tt)|0,c=c+Math.imul(U,At)|0,c=c+Math.imul(V,Tt)|0,b=b+Math.imul(V,At)|0,m=m+Math.imul(x,St)|0,c=c+Math.imul(x,xt)|0,c=c+Math.imul(I,St)|0,b=b+Math.imul(I,xt)|0;var $i=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+($i>>>26)|0,$i&=67108863,m=Math.imul(ct,ft),c=Math.imul(ct,_t),c=c+Math.imul(lt,ft)|0,b=Math.imul(lt,_t),m=m+Math.imul(st,pt)|0,c=c+Math.imul(st,ht)|0,c=c+Math.imul(at,pt)|0,b=b+Math.imul(at,ht)|0,m=m+Math.imul(ot,mt)|0,c=c+Math.imul(ot,gt)|0,c=c+Math.imul(rt,mt)|0,b=b+Math.imul(rt,gt)|0,m=m+Math.imul(it,bt)|0,c=c+Math.imul(it,yt)|0,c=c+Math.imul(nt,bt)|0,b=b+Math.imul(nt,yt)|0,m=m+Math.imul(tt,wt)|0,c=c+Math.imul(tt,vt)|0,c=c+Math.imul(et,wt)|0,b=b+Math.imul(et,vt)|0,m=m+Math.imul(X,Pt)|0,c=c+Math.imul(X,Ct)|0,c=c+Math.imul(Y,Pt)|0,b=b+Math.imul(Y,Ct)|0,m=m+Math.imul(G,Mt)|0,c=c+Math.imul(G,kt)|0,c=c+Math.imul(Q,Mt)|0,b=b+Math.imul(Q,kt)|0,m=m+Math.imul(K,Tt)|0,c=c+Math.imul(K,At)|0,c=c+Math.imul(Z,Tt)|0,b=b+Math.imul(Z,At)|0,m=m+Math.imul(U,St)|0,c=c+Math.imul(U,xt)|0,c=c+Math.imul(V,St)|0,b=b+Math.imul(V,xt)|0;var Ui=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ui>>>26)|0,Ui&=67108863,m=Math.imul(ct,pt),c=Math.imul(ct,ht),c=c+Math.imul(lt,pt)|0,b=Math.imul(lt,ht),m=m+Math.imul(st,mt)|0,c=c+Math.imul(st,gt)|0,c=c+Math.imul(at,mt)|0,b=b+Math.imul(at,gt)|0,m=m+Math.imul(ot,bt)|0,c=c+Math.imul(ot,yt)|0,c=c+Math.imul(rt,bt)|0,b=b+Math.imul(rt,yt)|0,m=m+Math.imul(it,wt)|0,c=c+Math.imul(it,vt)|0,c=c+Math.imul(nt,wt)|0,b=b+Math.imul(nt,vt)|0,m=m+Math.imul(tt,Pt)|0,c=c+Math.imul(tt,Ct)|0,c=c+Math.imul(et,Pt)|0,b=b+Math.imul(et,Ct)|0,m=m+Math.imul(X,Mt)|0,c=c+Math.imul(X,kt)|0,c=c+Math.imul(Y,Mt)|0,b=b+Math.imul(Y,kt)|0,m=m+Math.imul(G,Tt)|0,c=c+Math.imul(G,At)|0,c=c+Math.imul(Q,Tt)|0,b=b+Math.imul(Q,At)|0,m=m+Math.imul(K,St)|0,c=c+Math.imul(K,xt)|0,c=c+Math.imul(Z,St)|0,b=b+Math.imul(Z,xt)|0;var Vi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Vi>>>26)|0,Vi&=67108863,m=Math.imul(ct,mt),c=Math.imul(ct,gt),c=c+Math.imul(lt,mt)|0,b=Math.imul(lt,gt),m=m+Math.imul(st,bt)|0,c=c+Math.imul(st,yt)|0,c=c+Math.imul(at,bt)|0,b=b+Math.imul(at,yt)|0,m=m+Math.imul(ot,wt)|0,c=c+Math.imul(ot,vt)|0,c=c+Math.imul(rt,wt)|0,b=b+Math.imul(rt,vt)|0,m=m+Math.imul(it,Pt)|0,c=c+Math.imul(it,Ct)|0,c=c+Math.imul(nt,Pt)|0,b=b+Math.imul(nt,Ct)|0,m=m+Math.imul(tt,Mt)|0,c=c+Math.imul(tt,kt)|0,c=c+Math.imul(et,Mt)|0,b=b+Math.imul(et,kt)|0,m=m+Math.imul(X,Tt)|0,c=c+Math.imul(X,At)|0,c=c+Math.imul(Y,Tt)|0,b=b+Math.imul(Y,At)|0,m=m+Math.imul(G,St)|0,c=c+Math.imul(G,xt)|0,c=c+Math.imul(Q,St)|0,b=b+Math.imul(Q,xt)|0;var Ki=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ki>>>26)|0,Ki&=67108863,m=Math.imul(ct,bt),c=Math.imul(ct,yt),c=c+Math.imul(lt,bt)|0,b=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,b=b+Math.imul(at,vt)|0,m=m+Math.imul(ot,Pt)|0,c=c+Math.imul(ot,Ct)|0,c=c+Math.imul(rt,Pt)|0,b=b+Math.imul(rt,Ct)|0,m=m+Math.imul(it,Mt)|0,c=c+Math.imul(it,kt)|0,c=c+Math.imul(nt,Mt)|0,b=b+Math.imul(nt,kt)|0,m=m+Math.imul(tt,Tt)|0,c=c+Math.imul(tt,At)|0,c=c+Math.imul(et,Tt)|0,b=b+Math.imul(et,At)|0,m=m+Math.imul(X,St)|0,c=c+Math.imul(X,xt)|0,c=c+Math.imul(Y,St)|0,b=b+Math.imul(Y,xt)|0;var Wi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Wi>>>26)|0,Wi&=67108863,m=Math.imul(ct,wt),c=Math.imul(ct,vt),c=c+Math.imul(lt,wt)|0,b=Math.imul(lt,vt),m=m+Math.imul(st,Pt)|0,c=c+Math.imul(st,Ct)|0,c=c+Math.imul(at,Pt)|0,b=b+Math.imul(at,Ct)|0,m=m+Math.imul(ot,Mt)|0,c=c+Math.imul(ot,kt)|0,c=c+Math.imul(rt,Mt)|0,b=b+Math.imul(rt,kt)|0,m=m+Math.imul(it,Tt)|0,c=c+Math.imul(it,At)|0,c=c+Math.imul(nt,Tt)|0,b=b+Math.imul(nt,At)|0,m=m+Math.imul(tt,St)|0,c=c+Math.imul(tt,xt)|0,c=c+Math.imul(et,St)|0,b=b+Math.imul(et,xt)|0;var Hi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Hi>>>26)|0,Hi&=67108863,m=Math.imul(ct,Pt),c=Math.imul(ct,Ct),c=c+Math.imul(lt,Pt)|0,b=Math.imul(lt,Ct),m=m+Math.imul(st,Mt)|0,c=c+Math.imul(st,kt)|0,c=c+Math.imul(at,Mt)|0,b=b+Math.imul(at,kt)|0,m=m+Math.imul(ot,Tt)|0,c=c+Math.imul(ot,At)|0,c=c+Math.imul(rt,Tt)|0,b=b+Math.imul(rt,At)|0,m=m+Math.imul(it,St)|0,c=c+Math.imul(it,xt)|0,c=c+Math.imul(nt,St)|0,b=b+Math.imul(nt,xt)|0;var Zi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Zi>>>26)|0,Zi&=67108863,m=Math.imul(ct,Mt),c=Math.imul(ct,kt),c=c+Math.imul(lt,Mt)|0,b=Math.imul(lt,kt),m=m+Math.imul(st,Tt)|0,c=c+Math.imul(st,At)|0,c=c+Math.imul(at,Tt)|0,b=b+Math.imul(at,At)|0,m=m+Math.imul(ot,St)|0,c=c+Math.imul(ot,xt)|0,c=c+Math.imul(rt,St)|0,b=b+Math.imul(rt,xt)|0;var zi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(zi>>>26)|0,zi&=67108863,m=Math.imul(ct,Tt),c=Math.imul(ct,At),c=c+Math.imul(lt,Tt)|0,b=Math.imul(lt,At),m=m+Math.imul(st,St)|0,c=c+Math.imul(st,xt)|0,c=c+Math.imul(at,St)|0,b=b+Math.imul(at,xt)|0;var Gi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Gi>>>26)|0,Gi&=67108863,m=Math.imul(ct,St),c=Math.imul(ct,xt),c=c+Math.imul(lt,St)|0,b=Math.imul(lt,xt);var Qi=(C+m|0)+((c&8191)<<13)|0;return C=(b+(c>>>13)|0)+(Qi>>>26)|0,Qi&=67108863,v[0]=Ei,v[1]=Ri,v[2]=ji,v[3]=Ni,v[4]=Bi,v[5]=Di,v[6]=Fi,v[7]=qi,v[8]=Li,v[9]=$i,v[10]=Ui,v[11]=Vi,v[12]=Ki,v[13]=Wi,v[14]=Hi,v[15]=Zi,v[16]=zi,v[17]=Gi,v[18]=Qi,C!==0&&(v[19]=C,_.length++),_};Math.imul||(F=$);function z(y,s,u){u.negative=s.negative^y.negative,u.length=y.length+s.length;for(var _=0,h=0,g=0;g<u.length-1;g++){var v=h;h=0;for(var C=_&67108863,m=Math.min(g,s.length-1),c=Math.max(0,g-y.length+1);c<=m;c++){var b=g-c,T=y.words[b]|0,x=s.words[c]|0,I=T*x,B=I&67108863;v=v+(I/67108864|0)|0,B=B+C|0,C=B&67108863,v=v+(B>>>26)|0,h+=v>>>26,v&=67108863}u.words[g]=C,_=v,v=h}return _!==0?u.words[g]=_:u.length--,u._strip()}function D(y,s,u){return z(y,s,u)}n.prototype.mulTo=function(s,u){var _,h=this.length+s.length;return this.length===10&&s.length===10?_=F(this,s,u):h<63?_=$(this,s,u):h<1024?_=z(this,s,u):_=D(this,s,u),_};function q(y,s){this.x=y,this.y=s}q.prototype.makeRBT=function(s){for(var u=new Array(s),_=n.prototype._countBits(s)-1,h=0;h<s;h++)u[h]=this.revBin(h,_,s);return u},q.prototype.revBin=function(s,u,_){if(s===0||s===_-1)return s;for(var h=0,g=0;g<u;g++)h|=(s&1)<<u-g-1,s>>=1;return h},q.prototype.permute=function(s,u,_,h,g,v){for(var C=0;C<v;C++)h[C]=u[s[C]],g[C]=_[s[C]]},q.prototype.transform=function(s,u,_,h,g,v){this.permute(v,s,u,_,h,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 x=c,I=b,B=0;B<C;B++){var U=_[T+B],V=h[T+B],Ht=_[T+B+C],K=h[T+B+C],Z=x*Ht-I*K;K=x*K+I*Ht,Ht=Z,_[T+B]=U+Ht,h[T+B]=V+K,_[T+B+C]=U-Ht,h[T+B+C]=V-K,B!==m&&(Z=c*x-b*I,I=c*I+b*x,x=Z)}},q.prototype.guessLen13b=function(s,u){var _=Math.max(u,s)|1,h=_&1,g=0;for(_=_/2|0;_;_=_>>>1)g++;return 1<<g+1+h},q.prototype.conjugate=function(s,u,_){if(!(_<=1))for(var h=0;h<_/2;h++){var g=s[h];s[h]=s[_-h-1],s[_-h-1]=g,g=u[h],u[h]=-u[_-h-1],u[_-h-1]=-g}},q.prototype.normalize13b=function(s,u){for(var _=0,h=0;h<u/2;h++){var g=Math.round(s[2*h+1]/u)*8192+Math.round(s[2*h]/u)+_;s[h]=g&67108863,g<67108864?_=0:_=g/67108864|0}return s},q.prototype.convert13b=function(s,u,_,h){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<h;++v)_[v]=0;t(g===0),t((g&-8192)===0)},q.prototype.stub=function(s){for(var u=new Array(s),_=0;_<s;_++)u[_]=0;return u},q.prototype.mulp=function(s,u,_){var h=2*this.guessLen13b(s.length,u.length),g=this.makeRBT(h),v=this.stub(h),C=new Array(h),m=new Array(h),c=new Array(h),b=new Array(h),T=new Array(h),x=new Array(h),I=_.words;I.length=h,this.convert13b(s.words,s.length,C,h),this.convert13b(u.words,u.length,b,h),this.transform(C,v,m,c,h,g),this.transform(b,v,T,x,h,g);for(var B=0;B<h;B++){var U=m[B]*T[B]-c[B]*x[B];c[B]=m[B]*x[B]+c[B]*T[B],m[B]=U}return this.conjugate(m,c,h),this.transform(m,c,I,v,h,g),this.conjugate(I,v,h),this.normalize13b(I,h),_.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),D(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),t(typeof s=="number"),t(s<67108864);for(var _=0,h=0;h<this.length;h++){var g=(this.words[h]|0)*s,v=(g&67108863)+(_&67108863);_>>=26,_+=g/67108864|0,_+=v>>>26,this.words[h]=v&67108863}return _!==0&&(this.words[h]=_,this.length++),this.length=s===0?1:this.length,u?this.ineg():this},n.prototype.muln=function(s){return this.clone().imuln(s)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(s){var u=S(s);if(u.length===0)return new n(1);for(var _=this,h=0;h<u.length&&u[h]===0;h++,_=_.sqr());if(++h<u.length)for(var g=_.sqr();h<u.length;h++,g=g.sqr())u[h]!==0&&(_=_.mul(g));return _},n.prototype.iushln=function(s){t(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26,h=67108863>>>26-u<<26-u,g;if(u!==0){var v=0;for(g=0;g<this.length;g++){var C=this.words[g]&h,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 t(this.negative===0),this.iushln(s)},n.prototype.iushrn=function(s,u,_){t(typeof s=="number"&&s>=0);var h;u?h=(u-u%26)/26:h=0;var g=s%26,v=Math.min((s-g)/26,this.length),C=67108863^67108863>>>g<<g,m=_;if(h-=v,h=Math.max(0,h),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>=h);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()},n.prototype.ishrn=function(s,u,_){return t(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){t(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26,h=1<<u;if(this.length<=_)return!1;var g=this.words[_];return!!(g&h)},n.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 h=67108863^67108863>>>u<<u;this.words[this.length-1]&=h}return this.length===0&&(this.words[0]=0,this.length=1),this._strip()},n.prototype.maskn=function(s){return this.clone().imaskn(s)},n.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)},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(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()},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 h=s.length+_,g;this._expand(h);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()},n.prototype._wordDiv=function(s,u){var _=this.length-s.length,h=this.clone(),g=s,v=g.words[g.length-1]|0,C=this._countBits(v);_=26-C,_!==0&&(g=g.ushln(_),h.iushln(_),v=g.words[g.length-1]|0);var m=h.length-g.length,c;if(u!=="mod"){c=new n(null),c.length=m+1,c.words=new Array(c.length);for(var b=0;b<c.length;b++)c.words[b]=0}var T=h.clone()._ishlnsubmul(g,1,m);T.negative===0&&(h=T,c&&(c.words[m]=1));for(var x=m-1;x>=0;x--){var I=(h.words[g.length+x]|0)*67108864+(h.words[g.length+x-1]|0);for(I=Math.min(I/v|0,67108863),h._ishlnsubmul(g,I,x);h.negative!==0;)I--,h.negative=0,h._ishlnsubmul(g,1,x),h.isZero()||(h.negative^=1);c&&(c.words[x]=I)}return c&&c._strip(),h._strip(),u!=="div"&&_!==0&&h.iushrn(_),{div:c||null,mod:h}},n.prototype.divmod=function(s,u,_){if(t(!s.isZero()),this.isZero())return{div:new n(0),mod:new n(0)};var h,g,v;return this.negative!==0&&s.negative===0?(v=this.neg().divmod(s,u),u!=="mod"&&(h=v.div.neg()),u!=="div"&&(g=v.mod.neg(),_&&g.negative!==0&&g.iadd(s)),{div:h,mod:g}):this.negative===0&&s.negative!==0?(v=this.divmod(s.neg(),u),u!=="mod"&&(h=v.div.neg()),{div:h,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,h=s.ushrn(1),g=s.andln(1),v=_.cmp(h);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),t(s<=67108863);for(var _=(1<<26)%s,h=0,g=this.length-1;g>=0;g--)h=(_*h+(this.words[g]|0))%s;return u?-h:h},n.prototype.modn=function(s){return this.modrn(s)},n.prototype.idivn=function(s){var u=s<0;u&&(s=-s),t(s<=67108863);for(var _=0,h=this.length-1;h>=0;h--){var g=(this.words[h]|0)+_*67108864;this.words[h]=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){t(s.negative===0),t(!s.isZero());var u=this,_=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var h=new n(1),g=new n(0),v=new n(0),C=new n(1),m=0;u.isEven()&&_.isEven();)u.iushrn(1),_.iushrn(1),++m;for(var c=_.clone(),b=u.clone();!u.isZero();){for(var T=0,x=1;(u.words[0]&x)===0&&T<26;++T,x<<=1);if(T>0)for(u.iushrn(T);T-- >0;)(h.isOdd()||g.isOdd())&&(h.iadd(c),g.isub(b)),h.iushrn(1),g.iushrn(1);for(var I=0,B=1;(_.words[0]&B)===0&&I<26;++I,B<<=1);if(I>0)for(_.iushrn(I);I-- >0;)(v.isOdd()||C.isOdd())&&(v.iadd(c),C.isub(b)),v.iushrn(1),C.iushrn(1);u.cmp(_)>=0?(u.isub(_),h.isub(v),g.isub(C)):(_.isub(u),v.isub(h),C.isub(g))}return{a:v,b:C,gcd:_.iushln(m)}},n.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 h=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;)h.isOdd()&&h.iadd(v),h.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(_),h.isub(g)):(_.isub(u),g.isub(h))}var T;return u.cmpn(1)===0?T=h:T=g,T.cmpn(0)<0&&T.iadd(s),T},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 h=0;u.isEven()&&_.isEven();h++)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(h)},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){t(typeof s=="number");var u=s%26,_=(s-u)/26,h=1<<u;if(this.length<=_)return this._expand(_+1),this.words[_]|=h,this;for(var g=h,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),t(s<=67108863,"Number is too big");var h=this.words[0]|0;_=h===s?0:h<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 h=this.words[_]|0,g=s.words[_]|0;if(h!==g){h<g?u=-1:h>g&&(u=1);break}}return u},n.prototype.gtn=function(s){return this.cmpn(s)===1},n.prototype.gt=function(s){return this.cmp(s)===1},n.prototype.gten=function(s){return this.cmpn(s)>=0},n.prototype.gte=function(s){return this.cmp(s)>=0},n.prototype.ltn=function(s){return this.cmpn(s)===-1},n.prototype.lt=function(s){return this.cmp(s)===-1},n.prototype.lten=function(s){return this.cmpn(s)<=0},n.prototype.lte=function(s){return this.cmp(s)<=0},n.prototype.eqn=function(s){return this.cmpn(s)===0},n.prototype.eq=function(s){return this.cmp(s)===0},n.red=function(s){return new L(s)},n.prototype.toRed=function(s){return t(!this.red,"Already a number in reduction context"),t(this.negative===0,"red works only with positives"),s.convertTo(this)._forceRed(s)},n.prototype.fromRed=function(){return t(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 t(!this.red,"Already a number in reduction context"),this._forceRed(s)},n.prototype.redAdd=function(s){return t(this.red,"redAdd works only with red numbers"),this.red.add(this,s)},n.prototype.redIAdd=function(s){return t(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,s)},n.prototype.redSub=function(s){return t(this.red,"redSub works only with red numbers"),this.red.sub(this,s)},n.prototype.redISub=function(s){return t(this.red,"redISub works only with red numbers"),this.red.isub(this,s)},n.prototype.redShl=function(s){return t(this.red,"redShl works only with red numbers"),this.red.shl(this,s)},n.prototype.redMul=function(s){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.mul(this,s)},n.prototype.redIMul=function(s){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.imul(this,s)},n.prototype.redSqr=function(){return t(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return t(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return t(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return t(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return t(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(s){return t(this.red&&!s.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,s)};var Ft={k256:null,p224:null,p192:null,p25519:null};function H(y,s){this.name=y,this.p=new n(s,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}H.prototype._tmp=function(){var s=new n(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 h=_<this.n?-1:u.ucmp(this.p);return h===0?(u.words[0]=0,u.length=1):h>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 qt(){H.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}i(qt,H),qt.prototype.split=function(s,u){for(var _=4194303,h=Math.min(s.length,9),g=0;g<h;g++)u.words[g]=s.words[g];if(u.length=h,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 h=s.words[_]|0;u+=h*977,s.words[_]=u&67108863,u=h*64+(u/67108864|0)}return s.words[s.length-1]===0&&(s.length--,s.words[s.length-1]===0&&s.length--),s};function te(){H.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}i(te,H);function Wt(){H.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}i(Wt,H);function ee(){H.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}i(ee,H),ee.prototype.imulK=function(s){for(var u=0,_=0;_<s.length;_++){var h=(s.words[_]|0)*19+u,g=h&67108863;h>>>=26,s.words[_]=g,u=h}return u!==0&&(s.words[s.length++]=u),s},n._prime=function(s){if(Ft[s])return Ft[s];var u;if(s==="k256")u=new qt;else if(s==="p224")u=new te;else if(s==="p192")u=new Wt;else if(s==="p25519")u=new ee;else throw new Error("Unknown prime "+s);return Ft[s]=u,u};function L(y){if(typeof y=="string"){var s=n._prime(y);this.m=s.p,this.prime=s}else t(y.gtn(1),"modulus must be greater than 1"),this.m=y,this.prime=null}L.prototype._verify1=function(s){t(s.negative===0,"red works only with positives"),t(s.red,"red works only with red numbers")},L.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")},L.prototype.imod=function(s){return this.prime?this.prime.ireduce(s)._forceRed(this):(f(s,s.umod(this.m)._forceRed(this)),s)},L.prototype.neg=function(s){return s.isZero()?s.clone():this.m.sub(s)._forceRed(this)},L.prototype.add=function(s,u){this._verify2(s,u);var _=s.add(u);return _.cmp(this.m)>=0&&_.isub(this.m),_._forceRed(this)},L.prototype.iadd=function(s,u){this._verify2(s,u);var _=s.iadd(u);return _.cmp(this.m)>=0&&_.isub(this.m),_},L.prototype.sub=function(s,u){this._verify2(s,u);var _=s.sub(u);return _.cmpn(0)<0&&_.iadd(this.m),_._forceRed(this)},L.prototype.isub=function(s,u){this._verify2(s,u);var _=s.isub(u);return _.cmpn(0)<0&&_.iadd(this.m),_},L.prototype.shl=function(s,u){return this._verify1(s),this.imod(s.ushln(u))},L.prototype.imul=function(s,u){return this._verify2(s,u),this.imod(s.imul(u))},L.prototype.mul=function(s,u){return this._verify2(s,u),this.imod(s.mul(u))},L.prototype.isqr=function(s){return this.imul(s,s.clone())},L.prototype.sqr=function(s){return this.mul(s,s)},L.prototype.sqrt=function(s){if(s.isZero())return s.clone();var u=this.m.andln(3);if(t(u%2===1),u===3){var _=this.m.add(new n(1)).iushrn(2);return this.pow(s,_)}for(var h=this.m.subn(1),g=0;!h.isZero()&&h.andln(1)===0;)g++,h.iushrn(1);t(!h.isZero());var v=new n(1).toRed(this),C=v.redNeg(),m=this.m.subn(1).iushrn(1),c=this.m.bitLength();for(c=new n(2*c*c).toRed(this);this.pow(c,m).cmp(C)!==0;)c.redIAdd(C);for(var b=this.pow(c,h),T=this.pow(s,h.addn(1).iushrn(1)),x=this.pow(s,h),I=g;x.cmp(v)!==0;){for(var B=x,U=0;B.cmp(v)!==0;U++)B=B.redSqr();t(U<I);var V=this.pow(b,new n(1).iushln(I-U-1));T=T.redMul(V),b=V.redSqr(),x=x.redMul(b),I=U}return T},L.prototype.invm=function(s){var u=s._invmp(this.m);return u.negative!==0?(u.negative=0,this.imod(u).redNeg()):this.imod(u)},L.prototype.pow=function(s,u){if(u.isZero())return new n(1).toRed(this);if(u.cmpn(1)===0)return s.clone();var _=4,h=new Array(1<<_);h[0]=new n(1).toRed(this),h[1]=s;for(var g=2;g<h.length;g++)h[g]=this.mul(h[g-1],s);var v=h[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 x=b>>T&1;if(v!==h[0]&&(v=this.sqr(v)),x===0&&C===0){m=0;continue}C<<=1,C|=x,m++,!(m!==_&&(g!==0||T!==0))&&(v=this.mul(v,h[C]),m=0,C=0)}c=26}return v},L.prototype.convertTo=function(s){var u=s.umod(this.m);return u===s?u.clone():u},L.prototype.convertFrom=function(s){var u=s.clone();return u.red=null,u},n.mont=function(s){return new Nt(s)};function Nt(y){L.call(this,y),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new n(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}i(Nt,L),Nt.prototype.convertTo=function(s){return this.imod(s.ushln(this.shift))},Nt.prototype.convertFrom=function(s){var u=this.imod(s.mul(this.rinv));return u.red=null,u},Nt.prototype.imul=function(s,u){if(s.isZero()||u.isZero())return s.words[0]=0,s.length=1,s;var _=s.imul(u),h=_.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=_.isub(h).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)},Nt.prototype.mul=function(s,u){if(s.isZero()||u.isZero())return new n(0)._forceRed(this);var _=s.mul(u),h=_.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=_.isub(h).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)},Nt.prototype.invm=function(s){var u=this.imod(s._invmp(this.m).mul(this.r2));return u._forceRed(this)}})(typeof rn>"u"||rn,uo)});import{SdkWrapper as ba}from"@cetusprotocol/common-sdk";import{normalizeSuiObjectId as en}from"@mysten/sui/utils";import{CACHE_TIME_24H as Ut,CACHE_TIME_5MIN as Er,CachedContent as Rr,DETAILS_KEYS as Ue,extractStructTagFromType as nn,fixCoinType as jr,getFutureTime as co,getObjectFields as ei,getObjectId as on,getObjectPreviousTransactionDigest as Nr,getObjectType as Br,getPackagerConfigs as se,normalizeCoinType as lo}from"@cetusprotocol/common-sdk";var Kn="3.7.8",wr=Kn,Ce=typeof Buffer=="function",qn=typeof TextDecoder=="function"?new TextDecoder:void 0,Ln=typeof TextEncoder=="function"?new TextEncoder:void 0,vr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",Le=Array.prototype.slice.call(vr),Je=(o=>{let e={};return o.forEach((t,i)=>e[t]=i),e})(Le),Pr=/^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/,Ot=String.fromCharCode.bind(String),$n=typeof Uint8Array.from=="function"?Uint8Array.from.bind(Uint8Array):o=>new Uint8Array(Array.prototype.slice.call(o,0)),Wn=o=>o.replace(/=/g,"").replace(/[+\/]/g,e=>e=="+"?"-":"_"),Hn=o=>o.replace(/[^A-Za-z0-9\+\/]/g,""),Zn=o=>{let e,t,i,n,r="",a=o.length%3;for(let l=0;l<o.length;){if((t=o.charCodeAt(l++))>255||(i=o.charCodeAt(l++))>255||(n=o.charCodeAt(l++))>255)throw new TypeError("invalid character found");e=t<<16|i<<8|n,r+=Le[e>>18&63]+Le[e>>12&63]+Le[e>>6&63]+Le[e&63]}return a?r.slice(0,a-3)+"===".substring(a):r},Yi=typeof btoa=="function"?o=>btoa(o):Ce?o=>Buffer.from(o,"binary").toString("base64"):Zn,Ji=Ce?o=>Buffer.from(o).toString("base64"):o=>{let t=[];for(let i=0,n=o.length;i<n;i+=4096)t.push(Ot.apply(null,o.subarray(i,i+4096)));return Yi(t.join(""))},Xe=(o,e=!1)=>e?Wn(Ji(o)):Ji(o),Cr=o=>{if(o.length<2){var e=o.charCodeAt(0);return e<128?o:e<2048?Ot(192|e>>>6)+Ot(128|e&63):Ot(224|e>>>12&15)+Ot(128|e>>>6&63)+Ot(128|e&63)}else{var e=65536+(o.charCodeAt(0)-55296)*1024+(o.charCodeAt(1)-56320);return Ot(240|e>>>18&7)+Ot(128|e>>>12&63)+Ot(128|e>>>6&63)+Ot(128|e&63)}},Mr=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g,zn=o=>o.replace(Mr,Cr),Un=Ce?o=>Buffer.from(o,"utf8").toString("base64"):Ln?o=>Ji(Ln.encode(o)):o=>Yi(zn(o)),Pe=(o,e=!1)=>e?Wn(Un(o)):Un(o),Vn=o=>Pe(o,!0),kr=/[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g,Tr=o=>{switch(o.length){case 4:var e=(7&o.charCodeAt(0))<<18|(63&o.charCodeAt(1))<<12|(63&o.charCodeAt(2))<<6|63&o.charCodeAt(3),t=e-65536;return Ot((t>>>10)+55296)+Ot((t&1023)+56320);case 3:return Ot((15&o.charCodeAt(0))<<12|(63&o.charCodeAt(1))<<6|63&o.charCodeAt(2));default:return Ot((31&o.charCodeAt(0))<<6|63&o.charCodeAt(1))}},Gn=o=>o.replace(kr,Tr),Qn=o=>{if(o=o.replace(/\s+/g,""),!Pr.test(o))throw new TypeError("malformed base64.");o+="==".slice(2-(o.length&3));let e,t,i,n=[];for(let r=0;r<o.length;)e=Je[o.charAt(r++)]<<18|Je[o.charAt(r++)]<<12|(t=Je[o.charAt(r++)])<<6|(i=Je[o.charAt(r++)]),t===64?n.push(Ot(e>>16&255)):i===64?n.push(Ot(e>>16&255,e>>8&255)):n.push(Ot(e>>16&255,e>>8&255,e&255));return n.join("")},tn=typeof atob=="function"?o=>atob(Hn(o)):Ce?o=>Buffer.from(o,"base64").toString("binary"):Qn,Jn=Ce?o=>$n(Buffer.from(o,"base64")):o=>$n(tn(o).split("").map(e=>e.charCodeAt(0))),Xn=o=>Jn(Yn(o)),Ar=Ce?o=>Buffer.from(o,"base64").toString("utf8"):qn?o=>qn.decode(Jn(o)):o=>Gn(tn(o)),Yn=o=>Hn(o.replace(/[-_]/g,e=>e=="-"?"+":"/")),Xi=o=>Ar(Yn(o)),Sr=o=>{if(typeof o!="string")return!1;let e=o.replace(/\s+/g,"").replace(/={0,2}$/,"");return!/[^\s0-9a-zA-Z\+/]/.test(e)||!/[^\s0-9a-zA-Z\-_]/.test(e)},to=o=>({value:o,enumerable:!1,writable:!0,configurable:!0}),eo=function(){let o=(e,t)=>Object.defineProperty(String.prototype,e,to(t));o("fromBase64",function(){return Xi(this)}),o("toBase64",function(e){return Pe(this,e)}),o("toBase64URI",function(){return Pe(this,!0)}),o("toBase64URL",function(){return Pe(this,!0)}),o("toUint8Array",function(){return Xn(this)})},io=function(){let o=(e,t)=>Object.defineProperty(Uint8Array.prototype,e,to(t));o("toBase64",function(e){return Xe(this,e)}),o("toBase64URI",function(){return Xe(this,!0)}),o("toBase64URL",function(){return Xe(this,!0)})},xr=()=>{eo(),io()},Ye={version:Kn,VERSION:wr,atob:tn,atobPolyfill:Qn,btoa:Yi,btoaPolyfill:Zn,fromBase64:Xi,toBase64:Pe,encode:Pe,encodeURI:Vn,encodeURL:Vn,utob:zn,btou:Gn,decode:Xi,isValid:Sr,fromUint8Array:Xe,toUint8Array:Xn,extendString:eo,extendUint8Array:io,extendBuiltins:xr};import{BaseError as Ir}from"@cetusprotocol/common-sdk";var no=(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))(no||{}),ti=(r=>(r.InvalidTickEvent="InvalidTickEvent",r.InvalidPositionObject="InvalidPositionObject",r.InvalidPositionRewardObject="InvalidPositionRewardObject",r.InvalidParams="InvalidParams",r.FetchError="FetchError",r))(ti||{}),$e=(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))($e||{}),oo=(t=>(t.ClmmVestNotSet="ClmmVestNotSet",t.ClmmVestFetchError="ClmmVestFetchError",t))(oo||{}),ro=(t=>(t.NotFoundPartnerObject="NotFoundPartnerObject",t.InvalidPartnerRefFeeFields="InvalidPartnerRefFeeFields",t))(ro||{}),ke=(i=>(i.InvalidConfig="InvalidConfig",i.InvalidConfigHandle="InvalidConfigHandle",i.InvalidSimulateAccount="InvalidSimulateAccount",i))(ke||{}),so=(a=>(a.InvalidSendAddress="InvalidSendAddress",a.InvalidRecipientAddress="InvalidRecipientAddress",a.InvalidRecipientAndAmountLength="InvalidRecipientAndAmountLength",a.InsufficientBalance="InsufficientBalance",a.InvalidTarget="InvalidTarget",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a))(so||{}),Or=(a=>(a.InvalidCoin="InvalidCoin",a.NotFoundPath="NotFoundPath",a.NoDowngradeNeedParams="NoDowngradeNeedParams",a.InvalidSwapCountUrl="InvalidSwapCountUrl",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a.InvalidServerResponse="InvalidServerResponse",a))(Or||{}),ao=(e=>(e.InvalidType="InvalidType",e))(ao||{}),Me=class extends Ir{constructor(e,t,i){super(e,t||"UnknownError",i)}static isVaultsErrorCode(e,t){return this.isErrorCode(e,t)}},Bt=(o,e,t)=>{throw new Me(e.message,o,t)},N=(o,e,t)=>{throw new Me(e,o,t)};var ii=class{constructor(e){this._cache={};this._sdk=e}get sdk(){return this._sdk}setTokenListCache(e){let{coin_list_handle:t}=se(this.sdk.sdkOptions.cetus_config),i=`${t}_getCoinConfigs`,n=this.getCache(i),r=n?[...n,...e]:e;this.updateCache(i,r)}async getTokenListByCoinTypes(e){let t={},{coin_list_handle:i}=se(this.sdk.sdkOptions.cetus_config),n=`${i}_getCoinConfigs`,r=this.getCache(n);if(r!==void 0){let l=r;for(let d of e)for(let f of l)if(lo(d)===lo(f.address)){t[d]=f;continue}}let a=e.filter(l=>t[l]===void 0);for(let l of a){let d=`${l}_metadata`,f=this.getCache(d);if(f!==void 0)t[l]=f;else{let p=await this._sdk.FullClient.getCoinMetadata({coinType:l});if(p){let w={id:p.id,pyth_id:"",name:p.name,symbol:p.symbol,official_symbol:p.symbol,coingecko_id:"",decimals:p.decimals,project_url:"",logo_url:p.iconUrl,address:l};t[l]=w,this.updateCache(d,w,Ut)}else console.log(`not found ${l}`)}}return t}async getCoinConfigs(e=!1,t=!0){let{coin_list_handle:i}=se(this.sdk.sdkOptions.cetus_config),n=`${i}_getCoinConfigs`,r=this.getCache(n,e);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(p=>p.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(p=>{(p.error!=null||p.data?.content?.dataType!=="moveObject")&&N("FetchError",`when getCoinConfigs get objects error: ${p.error}, please check the rpc and contracts address config.`,{[Ue.METHOD_NAME]:"getCoinConfigs"});let w=this.buildCoinConfig(p,t);this.updateCache(`${i}_${w.address}_getCoinConfig`,w,Ut),f.push({...w})}),this.updateCache(n,f,Ut),f}async getCoinConfig(e,t=!1,i=!0){let{coin_list_handle:n}=se(this.sdk.sdkOptions.cetus_config),r=`${n}_${e}_getCoinConfig`,a=this.getCache(r,t);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"0x1::type_name::TypeName",value:{name:jr(e)}}});(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.`,{[Ue.METHOD_NAME]:"getCoinConfig"});let d=this.buildCoinConfig(l,i);return this.updateCache(r,d,Ut),d}buildCoinConfig(e,t=!0){let i=ei(e);i=i.value.fields;let n={...i};return n.id=on(e),n.address=nn(i.coin_type.fields.name).full_address,i.pyth_id&&(n.pyth_id=en(i.pyth_id)),this.transformExtensions(n,i.extension_fields.fields.contents,t),delete n.coin_type,n}async getClmmPoolConfigs(e=!1,t=!0){let{clmm_pools_handle:i}=se(this.sdk.sdkOptions.cetus_config),n=`${i}_getClmmPoolConfigs`,r=this.getCache(n,e);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(p=>p.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(p=>{(p.error!=null||p.data?.content?.dataType!=="moveObject")&&N("FetchError",`when getClmmPoolsConfigs get objects error: ${p.error}, please check the rpc and contracts address config.`,{[Ue.METHOD_NAME]:"getClmmPoolConfigs"});let w=this.buildClmmPoolConfig(p,t);this.updateCache(`${w.pool_address}_getClmmPoolConfig`,w,Ut),f.push({...w})}),this.updateCache(n,f,Ut),f}async getClmmPoolConfig(e,t=!1,i=!0){let{clmm_pools_handle:n}=se(this.sdk.sdkOptions.cetus_config),r=`${e}_getClmmPoolConfig`,a=this.getCache(r,t);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"address",value:e}}),d=this.buildClmmPoolConfig(l,i);return this.updateCache(r,d,Ut),d}buildClmmPoolConfig(e,t=!0){let i=ei(e);i=i.value.fields;let n={...i};return n.id=on(e),n.pool_address=en(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,t),n}async getLaunchpadPoolConfigs(e=!1,t=!0){let{launchpad_pools_handle:i}=se(this.sdk.sdkOptions.cetus_config),n=`${i}_getLaunchpadPoolConfigs`,r=this.getCache(n,e);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(p=>p.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(p=>{(p.error!=null||p.data?.content?.dataType!=="moveObject")&&N("FetchError",`when getCoinConfigs get objects error: ${p.error}, please check the rpc and contracts address config.`,{[Ue.METHOD_NAME]:"getLaunchpadPoolConfigs"});let w=this.buildLaunchpadPoolConfig(p,t);this.updateCache(`${w.pool_address}_getLaunchpadPoolConfig`,w,Ut),f.push({...w})}),this.updateCache(n,f,Ut),f}async getLaunchpadPoolConfig(e,t=!1,i=!0){let{launchpad_pools_handle:n}=se(this.sdk.sdkOptions.cetus_config),r=`${e}_getLaunchpadPoolConfig`,a=this.getCache(r,t);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"address",value:e}}),d=this.buildLaunchpadPoolConfig(l,i);return this.updateCache(r,d,Ut),d}buildLaunchpadPoolConfig(e,t=!0){let i=ei(e);i=i.value.fields;let n={...i};n.id=on(e),n.pool_address=en(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,t);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(Ye.decode(n.regulation).replace(/%/g,"%25"))}catch{n.regulation=Ye.decode(n.regulation)}return n}transformExtensions(e,t,i=!0){let n=[];for(let r of t){let{key:a}=r.fields,{value:l}=r.fields;if(a==="labels")try{let d=decodeURIComponent(Ye.decode(l));try{l=JSON.parse(d)}catch{l=d}}catch{}i&&(e[a]=l),n.push({key:a,value:l})}delete e.extension_fields,i||(e.extensions=n)}async getCetusConfig(e=!1){let t=this._sdk.sdkOptions.cetus_config.package_id,i=`${t}_getCetusConfig`,n=this.getCache(i,e);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:t,options:{showPreviousTransaction:!0}}),a=Nr(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(nn(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,Ut),d}async getCetusConfigHandle(e){let t=[e.clmm_pools_id,e.coin_list_id,e.launchpad_pools_id];return(await this._sdk.FullClient.multiGetObjects({ids:t,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.`,{[Ue.METHOD_NAME]:"getCetusConfigHandle"});let r=ei(n),a=Br(n);switch(nn(a).name){case"ClmmPools":e.clmm_pools_handle=r.pools.fields.id.id;break;case"CoinList":e.coin_list_handle=r.coins.fields.id.id;break;case"LaunchpadPools":e.launchpad_pools_handle=r.pools.fields.id.id;break;default:break}}),e}updateCache(e,t,i=Er){let n=this._cache[e];n?(n.overdue_time=co(i),n.value=t):n=new Rr(t,co(i)),this._cache[e]=n}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 Uo=re(ie(),1);import{SuiJsonRpcClient as ss}from"@mysten/sui/jsonRpc";import{Transaction as Ae}from"@mysten/sui/transactions";import{normalizeSuiAddress as Se}from"@mysten/sui/utils";var fo="position_liquidity_snapshot",_o=(i=>(i.Deleted="Deleted",i.Exists="Exists",i.NotExists="NotExists",i))(_o||{}),po=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","AddLiquidityV2Event","RemoveLiquidityV2Event"];var Rt=re(ie(),1);import{asUintN as vo,FEE_RATE_DENOMINATOR as si,getDeltaDownFromOutput as Po,getDeltaUpFromInput as Co,getNextSqrtPriceFromInput as Kr,getNextSqrtPriceFromOutput as Wr,MathUtil as ge,ZERO as Lt}from"@cetusprotocol/common-sdk";var wo=re(ie(),1);import{Transaction as an}from"@mysten/sui/transactions";import{adjustForSlippage as qr,CLOCK_ADDRESS as ri,CoinAssist as me,getPackagerConfigs as yo,MathUtil as cn,MAX_SQRT_PRICE as Lr,MIN_SQRT_PRICE as $r,U64_MAX as Ur,ZERO as Vr}from"@cetusprotocol/common-sdk";var sn="partner",ni="pool_script",Zt="pool_script_v2",oi="pool_script_v3",pe="router",ho="router_with_partner",zt="fetcher_script",Ba="expect_swap",mo="utils",Da="custodian_v2",Fa="clob_v2",qa="endpoints_v2",La=o=>{if(typeof o=="string"&&o.startsWith("0x"))return"object";if(typeof o=="number"||typeof o=="bigint")return"u64";if(typeof o=="boolean")return"bool";throw new Me(`Unknown type for value: ${o}`,"InvalidType")};import{Transaction as Dr}from"@mysten/sui/transactions";import{asUintN as go,CLOCK_ADDRESS as bo,CoinAssist as he,getPackagerConfigs as Fr}from"@cetusprotocol/common-sdk";function Qa(o){let e=he.isSuiCoin(o.coin_type_a),t=he.isSuiCoin(o.coin_type_b);return{is_adjust_coin_a:e,is_adjust_coin_b:t}}var Vt=class{static createCollectRewarderAndFeeParams(e,t,i){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,he.buildCoinWithBalance(BigInt(0),i.coin_type_a,t),he.buildCoinWithBalance(BigInt(0),i.coin_type_b,t)));let n=[];return i.rewarder_coin_types.forEach(r=>{n.push(he.buildCoinWithBalance(BigInt(0),r,t))}),t=e.Rewarder.createCollectRewarderPayload(i,t,n),t}static async buildAddLiquidityFixToken(e,t,i,n,r){i=i||new Dr;let a=n||he.buildCoinWithBalance(BigInt(t.amount_a),t.coin_type_a,i),l=r||he.buildCoinWithBalance(BigInt(t.amount_b),t.coin_type_b,i),d=[t.coin_type_a,t.coin_type_b],f=t.is_open?"open_position_with_liquidity_by_fix_coin":"add_liquidity_by_fix_coin",{clmm_pool:p,integrate:w}=e.sdkOptions;t.is_open||(i=this.createCollectRewarderAndFeeParams(e,i,t));let P=Fr(p),M=t.is_open?[i.object(P.global_config_id),i.object(t.pool_id),i.pure.u32(Number(go(BigInt(t.tick_lower)).toString())),i.pure.u32(Number(go(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(bo)]:[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(bo)];return i.moveCall({target:`${w.published_at}::${Zt}::${f}`,typeArguments:d,arguments:M}),i}static checkCoinThreshold(e,t,i,n,r,a){t&&i.moveCall({target:`${e.sdkOptions.integrate.published_at}::${pe}::check_coin_threshold`,typeArguments:[a],arguments:[n,i.pure.u64(r)]})}};var ae=class o{static getDefaultSqrtPriceLimit(e){return new wo.default(e?$r:Lr)}static getDefaultOtherAmountThreshold(e){return e?Vr:Ur}static buildSwapTransactionArgs(e,t,i,n,r){let{clmm_pool:a,integrate:l}=i,d=o.getDefaultSqrtPriceLimit(t.a2b),f=[t.coin_type_a,t.coin_type_b],{global_config_id:p}=yo(a);p===void 0&&N("InvalidConfig","clmm.config.global_config_id is undefined");let w=t.swap_partner!==void 0,P=w?t.a2b?"swap_a2b_with_partner":"swap_b2a_with_partner":t.a2b?"swap_a2b":"swap_b2a",M=w?[e.object(p),e.object(t.pool_id),e.object(t.swap_partner),n,r,e.pure.bool(t.by_amount_in),e.pure.u64(t.amount),e.pure.u64(t.amount_limit),e.pure.u128(d.toString()),e.object(ri)]:[e.object(p),e.object(t.pool_id),n,r,e.pure.bool(t.by_amount_in),e.pure.u64(t.amount),e.pure.u64(t.amount_limit),e.pure.u128(d.toString()),e.object(ri)];return e.moveCall({target:`${l.published_at}::${Zt}::${P}`,typeArguments:f,arguments:M}),e}static async adjustTransactionForGas(e,t,i,n){n.setSender(e.getSenderAddress());let r=me.selectCoinAssetGreaterThanOrEqual(t,i).selected_coins,a=me.calculateTotalBalance(t);if(r.length===0&&N("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let l=await e.FullClient.calculationTxGas(n);if(me.selectCoinAssetGreaterThanOrEqual(t,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 p=new an;return{fixAmount:i,newTx:p}}}return{fixAmount:i}}static buildSwapTransaction(e,t){let i=new an;i.setSender(e.getSenderAddress());let n=me.buildCoinWithBalance(t.a2b?BigInt(t.by_amount_in?t.amount:t.amount_limit):BigInt(0),t.coin_type_a,i),r=me.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,n,r),i}static async fixSwapParams(e,t,i){let{current_pool:n}=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: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=qr(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 n=me.buildCoinWithBalance(t.a2b?BigInt(t.by_amount_in?t.amount:t.amount_limit):BigInt(0),t.coin_type_a,i),r=me.buildCoinWithBalance(t.a2b?BigInt(0):BigInt(t.by_amount_in?t.amount:t.amount_limit),t.coin_type_b,i),a=o.buildSwapTransactionWithoutTransferCoinArgs(e,i,t,e.sdkOptions,n,r);return{tx:a.tx,coin_ab_s:a.txRes}}static buildSwapTransactionWithoutTransferCoinArgs(e,t,i,n,r,a){let{clmm_pool:l,integrate:d}=n,f=o.getDefaultSqrtPriceLimit(i.a2b),{global_config_id:p}=yo(l);p===void 0&&N("InvalidConfig","clmm.config.global_config_id is undefined");let w=i.swap_partner!==void 0,P=w?"swap_with_partner":"swap",M=w?ho:pe,A=w?[t.object(p),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(f.toString()),t.pure.bool(!1),t.object(ri)]:[t.object(p),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(f.toString()),t.pure.bool(!1),t.object(ri)],S=[i.coin_type_a,i.coin_type_b],$=t.moveCall({target:`${d.published_at}::${M}::${P}`,typeArguments:S,arguments:A});if(i.by_amount_in){let F=i.a2b?i.coin_type_b:i.coin_type_a,z=i.a2b?$[1]:$[0],D=Number(i.amount_limit);Vt.checkCoinThreshold(e,i.by_amount_in,t,z,D,F)}return{tx:t,txRes:$}}};function sc(o,e,t){let i=e.mul(t).shln(64),n=e.shln(64).add(o.mul(t));return cn.divRoundUp(i,n)}function ac(o,e,t){let i=e.mul(t).shln(64),n=e.shln(64).sub(o.mul(t));return cn.divRoundUp(i,n)}function cc(o,e,t){return t.sub(cn.divRoundUp(o.shln(64),e))}function lc(o,e,t){return t.add(o.shln(64).div(e))}function pc(o){let e=new Rt.default(o);return e.lt(Lt)?{bits:e.neg().xor(new Rt.default(2).pow(new Rt.default(64)).sub(new Rt.default(1))).add(new Rt.default(1)).toString()}:{bits:e.toString()}}function Mo(o){return{coin_type_a:o.coin_type_a,coin_type_b:o.coin_type_b,current_sqrt_price:new Rt.default(o.current_sqrt_price),current_tick_index:o.current_tick_index,fee_growth_global_a:new Rt.default(o.fee_growth_global_a),fee_growth_global_b:new Rt.default(o.fee_growth_global_b),fee_protocol_coin_a:new Rt.default(o.fee_protocol_coin_a),fee_protocol_coin_b:new Rt.default(o.fee_protocol_coin_b),fee_rate:new Rt.default(o.fee_rate),liquidity:new Rt.default(o.liquidity),tick_indexes:[],tick_spacing:Number(o.tick_spacing),ticks:[],collection_name:""}}function Hr(o,e,t,i,n,r){if(t===Lt)return{amount_in:Lt,amount_out:Lt,next_sqrt_price:e,fee_amount:Lt};let a=o.gte(e),l,d,f,p;if(r){let w=ge.checkMulDivFloor(i,ge.checkUnsignedSub(si,n),si,64),P=Co(o,e,t,a);P.gt(w)?(l=w,p=ge.checkUnsignedSub(i,w),f=Kr(o,t,w,a)):(l=P,p=ge.checkMulDivCeil(l,n,si.sub(n),64),f=e),d=Po(o,f,t,a)}else{let w=Po(o,e,t,a);w.gt(i)?(d=i,f=Wr(o,t,i,a)):(d=w,f=e),l=Co(o,f,t,a),p=ge.checkMulDivCeil(l,n,si.sub(n),64)}return{amount_in:l,amount_out:d,next_sqrt_price:f,fee_amount:p}}function ko(o,e,t,i,n){let r=t,a=i.liquidity,{current_sqrt_price:l}=i,d={amount_in:Lt,amount_out:Lt,fee_amount:Lt,ref_amount:Lt,next_sqrt_price:Lt,cross_tick_num:0},f,p,w=ae.getDefaultSqrtPriceLimit(o);for(let P of n){if(o&&i.current_tick_index<P.index||!o&&i.current_tick_index>=P.index||P===null)continue;o&&w.gt(P.sqrt_price)||!o&&w.lt(P.sqrt_price)?f=w:f=P.sqrt_price;let M=Hr(l,f,a,r,i.fee_rate,e);if(M.amount_in.eq(Lt)||(r=e?r.sub(M.amount_in.add(M.fee_amount)):r.sub(M.amount_out)),d.amount_in=d.amount_in.add(M.amount_in),d.amount_out=d.amount_out.add(M.amount_out),d.fee_amount=d.fee_amount.add(M.fee_amount),M.next_sqrt_price.eq(P.sqrt_price)?(p=P.liquidity_net.mul(new Rt.default(-1)),o?ge.is_neg(p)?a=a.add(new Rt.default(vo(BigInt(p.toString()),128))):a=a.add(p):ge.is_neg(p)?a=a.sub(new Rt.default(vo(BigInt(p.toString()),128))):a=a.sub(p),l=P.sqrt_price):l=M.next_sqrt_price,d.cross_tick_num+=1,r.eq(Lt))break}return d.amount_in=d.amount_in.add(d.fee_amount),d.next_sqrt_price=l,d}var $t=re(ie(),1);import{asIntN as be,buildNFT as Zr,d as Ve,DETAILS_KEYS as ai,extractStructTagFromType as Te,getMoveObjectType as To,getObjectDeletedResponse as zr,getObjectFields as un,getObjectId as ln,getObjectNotExistsResponse as Gr,getObjectOwner as Qr,MathUtil as Jr}from"@cetusprotocol/common-sdk";function Xr(o,e,t){let i=Te(o).name,n=Te(e).name;return`${i}-${n}[${t}]`}function ci(o){let e=To(o),t=Te(e),i=un(o);i==null&&N("InvalidPoolObject",`Pool id ${ln(o)} not exists.`,{[ai.METHOD_NAME]:"buildPool"});let n=[];i.rewarder_manager.fields.rewarders.forEach(d=>{let{emissions_per_second:f}=d.fields,p=Jr.fromX64(new $t.default(f)),w=Math.floor(p.toNumber()*60*60*24);n.push({emissions_per_second:f,coin_type:Te(d.fields.reward_coin.fields.name).source_address,growth_global:d.fields.growth_global,emissions_every_day:w})});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:ln(o),pool_type:e,coin_type_a:t.type_arguments[0],coin_type_b:t.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:be(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=Xr(l.coin_type_a,l.coin_type_b,l.tick_spacing),l}function li(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&N("InvalidPositionObject",`Position not exists. Get Position error:${o.error}`,{[ai.METHOD_NAME]:"buildPosition"});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=un(o);if(i){let a=To(o),l=Qr(o);"nft"in i?(i=i.nft.fields,e.description=i.description,e.name=i.name,e.link=i.url):e=Zr(o),t={...e,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:be(BigInt(i.tick_lower_index.fields.bits)),tick_upper_index:be(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=zr(o);n&&(t.pos_object_id=n.objectId,t.position_status="Deleted");let r=Gr(o);return r&&(t.pos_object_id=r,t.position_status="NotExists"),t}function Ke(o){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"};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?(e.reward_amount_owned_0=l,e.reward_growth_inside_0=d):a===1?(e.reward_amount_owned_1=l,e.reward_growth_inside_1=d):a===2&&(e.reward_amount_owned_2=l,e.reward_growth_inside_2=d)});let t="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:be(BigInt(t)),tick_upper_index:be(BigInt(i)),...e,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 ui(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&N("InvalidTickObject",`Tick not exists. Get tick data error:${o.error}`,{[ai.METHOD_NAME]:"buildTickData"});let t=un(o).value.fields.value.fields;return{object_id:ln(o),index:be(BigInt(t.index.fields.bits)),sqrt_price:new $t.default(t.sqrt_price),liquidity_net:new $t.default(t.liquidity_net.fields.bits),liquidity_gross:new $t.default(t.liquidity_gross),fee_growth_outside_a:new $t.default(t.fee_growth_outside_a),fee_growth_outside_b:new $t.default(t.fee_growth_outside_b),rewarders_growth_outside:t.rewards_growth_outside}}function Ao(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.",{[ai.METHOD_NAME]:"buildTickDataByEvent"});let e=be(BigInt(o.index.bits)),t=new $t.default(o.sqrt_price),i=new $t.default(o.liquidity_net.bits),n=new $t.default(o.liquidity_gross),r=new $t.default(o.fee_growth_outside_a),a=new $t.default(o.fee_growth_outside_b),l=o.rewards_growth_outside||[];return{object_id:"",index:e,sqrt_price:t,liquidity_net:i,liquidity_gross:n,fee_growth_outside_a:r,fee_growth_outside_b:a,rewarders_growth_outside:l}}function kc(o,e){return`Cetus LP | Pool${o}-${e}`}function So(o,e,t){let i=[],{timestampMs:n,events:r}=o,a=["AddLiquidityEvent","RemoveLiquidityEvent","CollectFeeEvent","CollectRewardEvent","CollectRewardV2Event","HarvestEvent","AddLiquidityV2Event","RemoveLiquidityV2Event"];return r?.forEach((l,d)=>{let f=Te(l.type).name;if(a.includes(f)){let p={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:`${e}_${d}`};switch(f){case"CollectFeeEvent":t.includes(p.parsed_json.position)&&(Ve(p.parsed_json.amount_a).gt(0)||Ve(p.parsed_json.amount_b).gt(0))&&i.push(p);break;case"RemoveLiquidityEvent":case"AddLiquidityEvent":case"AddLiquidityV2Event":case"RemoveLiquidityV2Event":(Ve(p.parsed_json.amount_a).gt(0)||Ve(p.parsed_json.amount_b).gt(0))&&i.push(p);break;case"CollectRewardEvent":case"HarvestEvent":case"CollectRewardV2Event":(t.includes(p.parsed_json.position)||t.includes(p.parsed_json.wrapped_position_id))&&Ve(p.parsed_json.amount).gt(0)&&i.push(p);break;default:break}}}),i}function xo(o,e,t,i){let n=[],{timestampMs:r,events:a}=o;return a?.forEach((l,d)=>{let{name:f,address:p}=Te(l.type);if(po.includes(f)&&t.includes(p)&&i===l.parsedJson.pool){let w={tx:l.id.txDigest,sender:l.sender,type:l.type,block_time:r||"0",index:`${e}_${d}`,parsed_json:l.parsedJson};n.push(w)}}),n}function di(o,e,t,i){e.moveCall({target:`${o.sdkOptions.integrate.published_at}::${mo}::transfer_coin_to_sender`,typeArguments:[i],arguments:[t]})}function Io(o,e,t,i,n){n!=null?e.transferObjects([t],e.pure.address(n)):di(o,e,t,i)}import{asUintN as xe,CACHE_TIME_24H as fi,ClmmPoolUtil as as,CLOCK_ADDRESS as qo,CoinAssist as _i,createFullClient as cs,d as Ie,DETAILS_KEYS as J,extractStructTagFromType as Oe,getObjectFields as ls,getObjectPreviousTransactionDigest as us,getPackagerConfigs as pi,isSortedSymbols as Lo,TickMath as fn,tickScore as ds,TickUtil as $o}from"@cetusprotocol/common-sdk";import{asIntN as Oo,d as Eo,extractStructTagFromType as Yr,fixCoinType as Ro,getObjectFields as dn,getObjectType as ts}from"@cetusprotocol/common-sdk";var es=1e4,Gt=class{static parseClmmVestInfo(e){let t=dn(e),i=ts(e),n=Yr(i),r=t.global_vesting_periods.map(l=>({period:l.fields.period,release_time:l.fields.release_time,redeemed_amount:l.fields.redeemed_amount,percentage:Eo(l.fields.percentage).div(es).toNumber()}));return{id:t.id.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:n.full_address,positions:{id:t.positions.fields.id,size:t.positions.fields.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:Ro(e.coin_a.name,!1),coin_type_b:Ro(e.coin_b.name,!1)}}static parsePoolLiquiditySnapshot(e){let t=dn(e);return{current_sqrt_price:t.current_sqrt_price,remove_percent:Eo(t.remove_percent).div(1e6).toString(),snapshots:{id:t.snapshots.fields.id.id,size:t.snapshots.fields.size}}}static parsePositionSnapshot(e){let t=dn(e),i=t.value.fields.value.fields;return{position_id:t.name,liquidity:i.liquidity,tick_lower_index:Oo(BigInt(i.tick_lower_index.fields.bits)),tick_upper_index:Oo(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}}};import{FullRpcUrlMainnet as is,GraphRpcUrlMainnet as ns}from"@cetusprotocol/common-sdk";var jo={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"}},No={env:"mainnet",full_rpc_url:is,graph_rpc_url:ns,cetus_config:{package_id:"0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f",published_at:"0xba7e740c3c002673dbe69ad5fbdb0691ec260170e141297cefb982e7081fde52",version:2,config:jo.cetusConfig},clmm_pool:{package_id:"0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb",published_at:"0x25ebb9a7c50eb17b3fa9c5a30fb8b5ad8f97caaf4928943acbcff7153dfee5e3",version:14,config:jo.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"}}},Bo=["0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb","0xdb5cd62a06c79695bfc9982eb08534706d3752fe123b48e0144f480209b3117f"];import{FullRpcUrlTestnet as os,GraphRpcUrlTestnet as rs}from"@cetusprotocol/common-sdk";var Do={env:"testnet",full_rpc_url:os,graph_rpc_url:rs,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"}}},Fo=["0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8","0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8"];var hi=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getPositionList(e,t="all"){let i={data:[],has_next_page:!0},n=await this._sdk.FullClient.getDynamicFieldsByPage(e,t);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.`,{[J.METHOD_NAME]:"getPositionList"}),l.name.value)),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,n=[],r={data:[],has_next_page:!1},a=e==="all",l=`${i}_getPoolImmutables`;if(a){let d=this._sdk.getCache(l,t);d&&n.push(...d)}if(n.length===0)try{let d=`${i}::factory::CreatePoolEvent`,f=await this._sdk.FullClient.queryEventsByPage({MoveEventType:d},e);r.has_next_page=f.has_next_page,r.next_cursor=f.next_cursor,f.data.forEach(p=>{let w=p.parsedJson;w&&n.push({id:w.pool_id,tick_spacing:w.tick_spacing,coin_type_a:Oe(w.coin_type_a).full_address,coin_type_b:Oe(w.coin_type_b).full_address})})}catch(d){return Bt("FetchError",d,{[J.METHOD_NAME]:"getPoolImmutables"})}return r.data=n,a&&this._sdk.updateCache(`${i}_getPoolImmutables`,n,fi),r}async getPoolsWithPage(e="all",t=!1){let i={data:[],has_next_page:!1},n=await this.getPoolImmutablesWithPage(e,t),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`,{[J.METHOD_NAME]:"getPoolsWithPage"});let l=ci(a);i.data.push(l);let d=`${l.id}_getPoolObject`;this._sdk.updateCache(d,l,fi)}return i.has_next_page=n.has_next_page,i.next_cursor=n.next_cursor,i}async getPoolLiquiditySnapshot(e,t=!1){try{let i=await this._sdk.FullClient.getDynamicFieldObject({parentId:e,name:{type:"0x1::string::String",value:fo}}),n=Gt.parsePoolLiquiditySnapshot(i);if(t){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 p=Gt.parsePositionSnapshot(f);d.push(p)}),n.position_snapshots=d}}return n}catch(i){return Bt("InvalidPoolObject",i,{[J.METHOD_NAME]:"getPoolLiquiditySnapshot",[J.REQUEST_PARAMS]:e})}}async getPositionSnapshot(e,t){let i=await this._sdk.FullClient.getDynamicFieldObjects(e,t,"0x2::object::ID","address"),n=[];return i.forEach(r=>{try{let a=Gt.parsePositionSnapshot(r);n.push(a)}catch(a){console.log("getPositionSnapshot error: ",a)}}),n}async getAssignPools(e){if(e.length===0)return[];let t=[],i=await this._sdk.FullClient.batchGetObjects(e,{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`,{[J.METHOD_NAME]:"getAssignPools"});let r=ci(n);t.push(r);let a=`${r.id}_getPoolObject`;this._sdk.updateCache(a,r,fi)}return t}async getPool(e,t=!0,i=!1){let n=`${e}_getPoolObject`,r=this._sdk.getCache(n,t);if(r!==void 0)return r;let a=await this._sdk.FullClient.getObject({id:e,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`,{[J.METHOD_NAME]:"getPool"});let l=ci(a);if(i){let d=await this.getPoolStatus(e);d&&(l.pool_status=d)}return this._sdk.updateCache(n,l),l}async getPoolByCoins(e,t){if(e.length===0)return[];for(let d=0;d<e.length;d++)e[d]==="0x2::sui::SUI"&&(e[d]="0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI");let i=this._sdk.sdkOptions.stats_pools_url;i||N("StatsPoolsUrlNotSet","statsPoolsUrl is not set in the sdk options.",{[J.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 n=await fetch(i),r;try{r=await n.json()}catch(d){Bt("FetchError",d,{[J.METHOD_NAME]:"getPoolByCoins",[J.REQUEST_PARAMS]:i})}let a=r.data.lp_list,l=[];for(let d of a)e.includes(d.coin_a_address)&&e.includes(d.coin_b_address)&&(t!=null?d.object.feeRate===t&&l.push(d.address):l.push(d.address));return l.length>0?await this.getAssignPools(l):[]}async createPoolPayload(e,t){if(Lo(Se(e.coin_type_a),Se(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(Lo(Se(e.coin_type_a),Se(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`,n=this._sdk.getCache(i,e);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:t,options:{showPreviousTransaction:!0}}),a=us(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 p=f.parsedJson;if(f.type)switch(Oe(f.type).full_address){case`${t}::config::InitConfigEvent`:d.global_config_id=p.global_config_id,d.admin_cap_id=p.admin_cap_id;break;case`${t}::factory::InitFactoryEvent`:d.pools_id=p.pools_id;break;case`${t}::rewarder::RewarderInitEvent`:d.global_vault_id=p.global_vault_id;break;case`${t}::partner::InitPartnerEvent`:d.partners_id=p.partners_id;break;default:break}}),this._sdk.updateCache(i,d,fi)),d}async getPoolTransactionList({pool_id:e,pagination_args:t,order:i="descending",full_rpc_url:n}){let{FullClient:r,sdkOptions:a}=this._sdk,l;n?l=cs(new ss({url:n,network:this._sdk.sdkOptions.env==="testnet"?"testnet":"mainnet"})):l=r;let d={data:[],has_next_page:!1},f=50,p=t,w=t.limit||10,P=a.env==="testnet"?Fo:Bo;do{let M=await l.queryTransactionBlocksByPage({ChangedObject:e},{...p,limit:50},i);M.data.forEach((A,S)=>{d.next_cursor=M.next_cursor;let $=xo(A,S,P,e);d.data=[...d.data,...$]}),d.has_next_page=M.has_next_page,d.next_cursor=M.next_cursor,p.cursor=M.next_cursor}while(d.data.length<w&&d.has_next_page);return d.data.length>w&&(d.data=d.data.slice(0,w),d.next_cursor=d.data[d.data.length-1].tx),d}async calculateCreatePoolWithPrice(e){let{current_price:t,slippage:i,tick_spacing:n,add_mode_params:r,price_base_coin:a,coin_decimals_a:l,coin_decimals_b:d}=e,f=fn.priceToSqrtPriceX64(a==="coin_a"?Ie(t):Ie(1).div(t),l,d),p=0,w=0;if(r.is_full_range)p=$o.getMinIndex(n),w=$o.getMaxIndex(n);else{let{min_price:D,max_price:q}=r;p=fn.priceToInitializeTickIndex(a==="coin_a"?Ie(D):Ie(1).div(q),l,d,n),w=fn.priceToInitializeTickIndex(a==="coin_a"?Ie(q):Ie(1).div(D),l,d,n)}let{coin_amount:P,fix_amount_a:M}=e,{coin_amount_limit_a:A,coin_amount_limit_b:S,liquidity_amount:$,coin_amount_a:F,coin_amount_b:z}=as.estLiquidityAndCoinAmountFromOneAmounts(p,w,new Uo.default(P),M,!0,i,f);return{coin_amount_a:F,coin_amount_b:z,coin_amount_limit_a:A,coin_amount_limit_b:S,liquidity:$,initialize_sqrt_price:f.toString(),tick_lower:p,tick_upper:w,fix_amount_a:M}}createPoolAndAddLiquidity(e,t){t=t||new Ae,t.setSender(this.sdk.getSenderAddress());let{integrate:i,clmm_pool:n}=this.sdk.sdkOptions,r=pi(n),a=r.global_config_id,l=r.pools_id,d=_i.buildCoinWithBalance(BigInt(e.amount_a),e.coin_type_a,t),f=_i.buildCoinWithBalance(BigInt(e.amount_b),e.coin_type_b,t),p=[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(xe(BigInt(e.tick_lower)).toString())),t.pure.u32(Number(xe(BigInt(e.tick_upper)).toString())),d,f,t.pure.bool(e.fix_amount_a),t.object(qo)];return t.moveCall({target:`${i.published_at}::pool_creator_v3::create_pool_v3`,typeArguments:[e.coin_type_a,e.coin_type_b],arguments:p}),di(this._sdk,t,d,e.coin_type_a),di(this._sdk,t,f,e.coin_type_b),t}createPoolAndAddLiquidityRow(e,t){t=t||new Ae;let{clmm_pool:i}=this.sdk.sdkOptions,n=pi(i),r=n.global_config_id,a=n.pools_id,l=_i.buildCoinWithBalance(BigInt(e.amount_a),e.coin_type_a,t),d=_i.buildCoinWithBalance(BigInt(e.amount_b),e.coin_type_b,t),f=[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(xe(BigInt(e.tick_lower)).toString())),t.pure.u32(Number(xe(BigInt(e.tick_upper)).toString())),l,d,t.pure.bool(e.fix_amount_a),t.object(qo)],p=t.moveCall({target:`${i.published_at}::pool_creator::create_pool_v3`,typeArguments:[e.coin_type_a,e.coin_type_b],arguments:f});return{tx:t,pos_id:p[0],remain_coin_a:p[1],remain_coin_b:p[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:n,tick_spacing:r,uri:a,calculate_result:l}=e,{initialize_sqrt_price:d,tick_lower:f,tick_upper:p,liquidity:w,coin_amount_a:P,coin_amount_b:M,fix_amount_a:A,coin_amount_limit_a:S,coin_amount_limit_b:$}=l;return this.createPoolRowPayload({tick_spacing:r,initialize_sqrt_price:d,uri:a||"",coin_type_a:i,coin_type_b:n,amount_a:A?P:S,amount_b:A?$:M,fix_amount_a:A,tick_lower:f,tick_upper:p},t)}async createPoolWithPricePayload(e){let{coin_type_a:t,coin_type_b:i,tick_spacing:n,uri:r,calculate_result:a}=e,{initialize_sqrt_price:l,tick_lower:d,tick_upper:f,liquidity:p,coin_amount_a:w,coin_amount_b:P,fix_amount_a:M,coin_amount_limit_a:A,coin_amount_limit_b:S}=a;return this.createPoolPayload({tick_spacing:n,initialize_sqrt_price:l,uri:r||"",coin_type_a:t,coin_type_b:i,amount_a:M?w:A,amount_b:M?S:P,fix_amount_a:M,tick_lower:d,tick_upper:f})}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(xe(BigInt(r[r.length-1].index)))]}return t}async getTicks(e){let{integrate:t}=this.sdk.sdkOptions,i=[],n=[e.coin_type_a,e.coin_type_b],r=new Ae,a=r.makeMoveVec({elements:e.start.map(f=>r.pure.u32(f)),type:"u32"}),l=[r.object(e.pool_id),a,r.pure.u64(e.limit.toString())];r.moveCall({target:`${t.published_at}::${zt}::fetch_ticks`,arguments:l,typeArguments:n});let d=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:r,sender:Se("0x0")});return d.error!=null&&N("InvalidTickObjectId",`getTicks error code: ${d.error??"unknown error"}, please check config and tick object ids`,{[J.METHOD_NAME]:"getTicks",[J.REQUEST_PARAMS]:e}),d.events?.forEach(f=>{Oe(f.type).name==="FetchTicksResultEvent"&&f.parsedJson.ticks.forEach(p=>{i.push(Ao(p))})}),i}async fetchPoolPositionInfoList(e){let{integrate:t}=this.sdk.sdkOptions,i=[],n=[],r=512;for(;;){let a=[e.coin_type_a,e.coin_type_b],l=new Ae,d=l.pure.vector("id",n.map(P=>P)),f=[l.object(e.pool_id),d,l.pure.u64(r)];l.moveCall({target:`${t.published_at}::${zt}::fetch_positions`,arguments:f,typeArguments:a});let p=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:l,sender:Se("0x0")});p.error!=null&&N("InvalidPositionRewardObject",`fetch position info error code: ${p.error??"unknown error"}, please check config and tick object ids`,{[J.METHOD_NAME]:"fetchPoolPositionInfoList",[J.REQUEST_PARAMS]:e});let w=[];if(p?.events?.forEach(P=>{Oe(P.type).name==="FetchPositionsEvent"&&P.parsedJson.positions.forEach(M=>{let A=Ke(M);w.push(A)})}),i.push(...w),w.length<r)break;n=[w[w.length-1].pos_object_id]}return i}async fetchTicksByRpc(e){let t=[],i=null,n=50;for(;;){let r=[],a=await this.sdk.FullClient.getDynamicFields({parentId:e,cursor:i,limit:n});if(i=a.nextCursor,a.data.forEach(l=>{Oe(l.objectType).module==="skip_list"&&r.push(l.objectId)}),t=[...t,...await this.getTicksByRpc(r)],!a.hasNextPage)break}return t}async getTicksByRpc(e){let t=[],i=await this.sdk.FullClient.batchGetObjects(e,{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`,{[J.METHOD_NAME]:"getTicksByRpc"});let r=ui(n);r!=null&&t.push(r)}return t}async getTickDataByIndex(e,t){let i={type:"u64",value:xe(BigInt(ds(t).toString())).toString()},n=await this.sdk.FullClient.getDynamicFieldObject({parentId:e,name:i});return(n.error!=null||n.data?.content?.dataType!=="moveObject")&&N("InvalidTickIndex",`get tick by index: ${t} error: ${n.error}`,{[J.METHOD_NAME]:"getTickDataByIndex"}),ui(n)}async getTickDataByObjectId(e){let t=await this.sdk.FullClient.getObject({id:e,options:{showContent:!0}});return(t.error!=null||t.data?.content?.dataType!=="moveObject")&&N("InvalidTickObjectId",`getTicksByRpc error code: ${t.error?.code??"unknown error"}, please check config and tick object ids`,{[J.METHOD_NAME]:"getTickDataByObjectId"}),ui(t)}async getPartnerRefFeeAmount(e,t=!0){let i=await this._sdk.FullClient.batchGetObjects([e],{showOwner:!0,showContent:!0,showDisplay:t,showType:!0});i[0].data?.content?.dataType!=="moveObject"&&N("NotFoundPartnerObject",`get partner by object id: ${e} error: ${i[0].error}`,{[J.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:t,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}`,{[J.METHOD_NAME]:"getPartnerRefFeeAmount"});let p={coin_type:f.data.content.fields.name,coin_object_id:f.data.objectId,balance:BigInt(f.data.content.fields.value)};l.push(p)}),l}async claimPartnerRefFeePayload(e,t,i){let n=new Ae,{clmm_pool:r}=this.sdk.sdkOptions,{global_config_id:a}=pi(r),l=[i],d=[n.object(a),n.object(e),n.object(t)];return n.moveCall({target:`${r.published_at}::${sn}::claim_ref_fee`,arguments:d,typeArguments:l}),n}async getPoolStatus(e){try{let t=await this._sdk.FullClient.getDynamicFieldObject({parentId:e,name:{type:"0x1::string::String",value:"pool_status"}}),n=ls(t).position.fields.status;if(n)return n}catch(t){console.log("\u{1F680} ~ file: poolModule.ts:1093 ~ PoolModule ~ getPoolStatus ~ error:",t)}}async claimAllPartnerRefFeesPayload(e,t,i){try{let{clmm_pool:n}=this._sdk.sdkOptions,{global_config_id:r}=pi(n),a=await this.getPartnerRefFeeAmount(e,!0),l=new Ae;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}::${sn}::claim_ref_fee`,arguments:[l.object(r),l.object(t),l.object(e)],typeArguments:[f]}))}return{ref_fees:a,tx:l}}catch(n){return Bt("InvalidPartnerRefFeeFields",n,{[J.METHOD_NAME]:"claimAllPartnerRefFeesPayload",[J.REQUEST_PARAMS]:{partner:e,partner_cap:t,specific_coin_types:i}})}}};var Ze=re(ie(),1);import{SuiJsonRpcClient as ra}from"@mysten/sui/jsonRpc";import{Transaction as oe}from"@mysten/sui/transactions";import{isValidSuiObjectId as sa,normalizeSuiAddress as aa}from"@mysten/sui/utils";import{asUintN as je,ClmmPoolUtil as nr,CLOCK_ADDRESS as Mi,CoinAssist as Ne,d as Xt,DETAILS_KEYS as fe,extractStructTagFromType as ca,getObjectFields as or,getPackagerConfigs as Yt,TickMath as Be,TickUtil as ki,createFullClient as la,deriveDynamicFieldIdByType as ua}from"@cetusprotocol/common-sdk";var Ee=9e15,de=1e9,_n="0123456789abcdef",bi="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",yi="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",pn={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Ee,maxE:Ee,crypto:!1},Ho,ne,j=!0,vi="[DecimalError] ",ue=vi+"Invalid argument: ",Zo=vi+"Precision limit exceeded",zo=vi+"crypto unavailable",Go="[object Decimal]",jt=Math.floor,It=Math.pow,fs=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,_s=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,ps=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,Qo=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,Kt=1e7,R=7,hs=9007199254740991,ms=bi.length-1,hn=yi.length-1,k={toStringTag:Go};k.absoluteValue=k.abs=function(){var o=new this.constructor(this);return o.s<0&&(o.s=1),O(o)};k.ceil=function(){return O(new this.constructor(this),this.e+1,2)};k.clampedTo=k.clamp=function(o,e){var t,i=this,n=i.constructor;if(o=new n(o),e=new n(e),!o.s||!e.s)return new n(NaN);if(o.gt(e))throw Error(ue+e);return t=i.cmp(o),t<0?o:i.cmp(e)>0?e:new n(i)};k.comparedTo=k.cmp=function(o){var e,t,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,e=0,t=i<n?i:n;e<t;++e)if(a[e]!==l[e])return a[e]>l[e]^d<0?1:-1;return i===n?0:i>n^d<0?1:-1};k.cosine=k.cos=function(){var o,e,t=this,i=t.constructor;return t.d?t.d[0]?(o=i.precision,e=i.rounding,i.precision=o+Math.max(t.e,t.sd())+R,i.rounding=1,t=gs(i,er(i,t)),i.precision=o,i.rounding=e,O(ne==2||ne==3?t.neg():t,o,e,!0)):new i(1):new i(NaN)};k.cubeRoot=k.cbrt=function(){var o,e,t,i,n,r,a,l,d,f,p=this,w=p.constructor;if(!p.isFinite()||p.isZero())return new w(p);for(j=!1,r=p.s*It(p.s*p,1/3),!r||Math.abs(r)==1/0?(t=Et(p.d),o=p.e,(r=(o-t.length+1)%3)&&(t+=r==1||r==-2?"0":"00"),r=It(t,1/3),o=jt((o+1)/3)-(o%3==(o<0?-1:2)),r==1/0?t="5e"+o:(t=r.toExponential(),t=t.slice(0,t.indexOf("e")+1)+o),i=new w(t),i.s=p.s):i=new w(r.toString()),a=(o=w.precision)+3;;)if(l=i,d=l.times(l).times(l),f=d.plus(p),i=W(f.plus(p).times(l),f.plus(d),a+2,1),Et(l.d).slice(0,a)===(t=Et(i.d)).slice(0,a))if(t=t.slice(a-3,a+1),t=="9999"||!n&&t=="4999"){if(!n&&(O(l,o+1,0),l.times(l).times(l).eq(p))){i=l;break}a+=4,n=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(O(i,o+1,1),e=!i.times(i).times(i).eq(p));break}return j=!0,O(i,o,w.rounding,e)};k.decimalPlaces=k.dp=function(){var o,e=this.d,t=NaN;if(e){if(o=e.length-1,t=(o-jt(this.e/R))*R,o=e[o],o)for(;o%10==0;o/=10)t--;t<0&&(t=0)}return t};k.dividedBy=k.div=function(o){return W(this,new this.constructor(o))};k.dividedToIntegerBy=k.divToInt=function(o){var e=this,t=e.constructor;return O(W(e,new t(o),0,1,1),t.precision,t.rounding)};k.equals=k.eq=function(o){return this.cmp(o)===0};k.floor=function(){return O(new this.constructor(this),this.e+1,3)};k.greaterThan=k.gt=function(o){return this.cmp(o)>0};k.greaterThanOrEqualTo=k.gte=function(o){var e=this.cmp(o);return e==1||e===0};k.hyperbolicCosine=k.cosh=function(){var o,e,t,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;t=a.precision,i=a.rounding,a.precision=t+Math.max(r.e,r.sd())+4,a.rounding=1,n=r.d.length,n<32?(o=Math.ceil(n/3),e=(1/Ci(4,o)).toString()):(o=16,e="2.3283064365386962890625e-10"),r=Re(a,1,r.times(e),new a(1),!0);for(var d,f=o,p=new a(8);f--;)d=r.times(r),r=l.minus(d.times(p.minus(d.times(p))));return O(r,a.precision=t,a.rounding=i,!0)};k.hyperbolicSine=k.sinh=function(){var o,e,t,i,n=this,r=n.constructor;if(!n.isFinite()||n.isZero())return new r(n);if(e=r.precision,t=r.rounding,r.precision=e+Math.max(n.e,n.sd())+4,r.rounding=1,i=n.d.length,i<3)n=Re(r,2,n,n,!0);else{o=1.4*Math.sqrt(i),o=o>16?16:o|0,n=n.times(1/Ci(5,o)),n=Re(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=e,r.rounding=t,O(n,e,t,!0)};k.hyperbolicTangent=k.tanh=function(){var o,e,t=this,i=t.constructor;return t.isFinite()?t.isZero()?new i(t):(o=i.precision,e=i.rounding,i.precision=o+7,i.rounding=1,W(t.sinh(),t.cosh(),i.precision=o,i.rounding=e)):new i(t.s)};k.inverseCosine=k.acos=function(){var o=this,e=o.constructor,t=o.abs().cmp(1),i=e.precision,n=e.rounding;return t!==-1?t===0?o.isNeg()?Qt(e,i,n):new e(0):new e(NaN):o.isZero()?Qt(e,i+4,n).times(.5):(e.precision=i+6,e.rounding=1,o=new e(1).minus(o).div(o.plus(1)).sqrt().atan(),e.precision=i,e.rounding=n,o.times(2))};k.inverseHyperbolicCosine=k.acosh=function(){var o,e,t=this,i=t.constructor;return t.lte(1)?new i(t.eq(1)?0:NaN):t.isFinite()?(o=i.precision,e=i.rounding,i.precision=o+Math.max(Math.abs(t.e),t.sd())+4,i.rounding=1,j=!1,t=t.times(t).minus(1).sqrt().plus(t),j=!0,i.precision=o,i.rounding=e,t.ln()):new i(t)};k.inverseHyperbolicSine=k.asinh=function(){var o,e,t=this,i=t.constructor;return!t.isFinite()||t.isZero()?new i(t):(o=i.precision,e=i.rounding,i.precision=o+2*Math.max(Math.abs(t.e),t.sd())+6,i.rounding=1,j=!1,t=t.times(t).plus(1).sqrt().plus(t),j=!0,i.precision=o,i.rounding=e,t.ln())};k.inverseHyperbolicTangent=k.atanh=function(){var o,e,t,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,e=r.rounding,i=n.sd(),Math.max(i,o)<2*-n.e-1?O(new r(n),o,e,!0):(r.precision=t=i-n.e,n=W(n.plus(1),new r(1).minus(n),t+o,1),r.precision=o+4,r.rounding=1,n=n.ln(),r.precision=o,r.rounding=e,n.times(.5))):new r(NaN)};k.inverseSine=k.asin=function(){var o,e,t,i,n=this,r=n.constructor;return n.isZero()?new r(n):(e=n.abs().cmp(1),t=r.precision,i=r.rounding,e!==-1?e===0?(o=Qt(r,t+4,i).times(.5),o.s=n.s,o):new r(NaN):(r.precision=t+6,r.rounding=1,n=n.div(new r(1).minus(n.times(n)).sqrt().plus(1)).atan(),r.precision=t,r.rounding=i,n.times(2)))};k.inverseTangent=k.atan=function(){var o,e,t,i,n,r,a,l,d,f=this,p=f.constructor,w=p.precision,P=p.rounding;if(f.isFinite()){if(f.isZero())return new p(f);if(f.abs().eq(1)&&w+4<=hn)return a=Qt(p,w+4,P).times(.25),a.s=f.s,a}else{if(!f.s)return new p(NaN);if(w+4<=hn)return a=Qt(p,w+4,P).times(.5),a.s=f.s,a}for(p.precision=l=w+10,p.rounding=1,t=Math.min(28,l/R+2|0),o=t;o;--o)f=f.div(f.times(f).plus(1).sqrt().plus(1));for(j=!1,e=Math.ceil(l/R),i=1,d=f.times(f),a=new p(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[e]!==void 0)for(o=e;a.d[o]===r.d[o]&&o--;);return t&&(a=a.times(2<<t-1)),j=!0,O(a,p.precision=w,p.rounding=P,!0)};k.isFinite=function(){return!!this.d};k.isInteger=k.isInt=function(){return!!this.d&&jt(this.e/R)>this.d.length-2};k.isNaN=function(){return!this.s};k.isNegative=k.isNeg=function(){return this.s<0};k.isPositive=k.isPos=function(){return this.s>0};k.isZero=function(){return!!this.d&&this.d[0]===0};k.lessThan=k.lt=function(o){return this.cmp(o)<0};k.lessThanOrEqualTo=k.lte=function(o){return this.cmp(o)<1};k.logarithm=k.log=function(o){var e,t,i,n,r,a,l,d,f=this,p=f.constructor,w=p.precision,P=p.rounding,M=5;if(o==null)o=new p(10),e=!0;else{if(o=new p(o),t=o.d,o.s<0||!t||!t[0]||o.eq(1))return new p(NaN);e=o.eq(10)}if(t=f.d,f.s<0||!t||!t[0]||f.eq(1))return new p(t&&!t[0]?-1/0:f.s!=1?NaN:t?0:1/0);if(e)if(t.length>1)r=!0;else{for(n=t[0];n%10===0;)n/=10;r=n!==1}if(j=!1,l=w+M,a=le(f,l),i=e?wi(p,l+10):le(o,l),d=W(a,i,l,1),We(d.d,n=w,P))do if(l+=10,a=le(f,l),i=e?wi(p,l+10):le(o,l),d=W(a,i,l,1),!r){+Et(d.d).slice(n+1,n+15)+1==1e14&&(d=O(d,w+1,0));break}while(We(d.d,n+=10,P));return j=!0,O(d,w,P)};k.minus=k.sub=function(o){var e,t,i,n,r,a,l,d,f,p,w,P,M=this,A=M.constructor;if(o=new A(o),!M.d||!o.d)return!M.s||!o.s?o=new A(NaN):M.d?o.s=-o.s:o=new A(o.d||M.s!==o.s?M:NaN),o;if(M.s!=o.s)return o.s=-o.s,M.plus(o);if(f=M.d,P=o.d,l=A.precision,d=A.rounding,!f[0]||!P[0]){if(P[0])o.s=-o.s;else if(f[0])o=new A(M);else return new A(d===3?-0:0);return j?O(o,l,d):o}if(t=jt(o.e/R),p=jt(M.e/R),f=f.slice(),r=p-t,r){for(w=r<0,w?(e=f,r=-r,a=P.length):(e=P,t=p,a=f.length),i=Math.max(Math.ceil(l/R),a)+2,r>i&&(r=i,e.length=1),e.reverse(),i=r;i--;)e.push(0);e.reverse()}else{for(i=f.length,a=P.length,w=i<a,w&&(a=i),i=0;i<a;i++)if(f[i]!=P[i]){w=f[i]<P[i];break}r=0}for(w&&(e=f,f=P,P=e,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]=Kt-1;--f[n],f[i]+=Kt}f[i]-=P[i]}for(;f[--a]===0;)f.pop();for(;f[0]===0;f.shift())--t;return f[0]?(o.d=f,o.e=Pi(f,t),j?O(o,l,d):o):new A(d===3?-0:0)};k.modulo=k.mod=function(o){var e,t=this,i=t.constructor;return o=new i(o),!t.d||!o.s||o.d&&!o.d[0]?new i(NaN):!o.d||t.d&&!t.d[0]?O(new i(t),i.precision,i.rounding):(j=!1,i.modulo==9?(e=W(t,o.abs(),0,3,1),e.s*=o.s):e=W(t,o,0,i.modulo,1),e=e.times(o),j=!0,t.minus(e))};k.naturalExponential=k.exp=function(){return mn(this)};k.naturalLogarithm=k.ln=function(){return le(this)};k.negated=k.neg=function(){var o=new this.constructor(this);return o.s=-o.s,O(o)};k.plus=k.add=function(o){var e,t,i,n,r,a,l,d,f,p,w=this,P=w.constructor;if(o=new P(o),!w.d||!o.d)return!w.s||!o.s?o=new P(NaN):w.d||(o=new P(o.d||w.s===o.s?w:NaN)),o;if(w.s!=o.s)return o.s=-o.s,w.minus(o);if(f=w.d,p=o.d,l=P.precision,d=P.rounding,!f[0]||!p[0])return p[0]||(o=new P(w)),j?O(o,l,d):o;if(r=jt(w.e/R),i=jt(o.e/R),f=f.slice(),n=r-i,n){for(n<0?(t=f,n=-n,a=p.length):(t=p,i=r,a=f.length),r=Math.ceil(l/R),a=r>a?r+1:a+1,n>a&&(n=a,t.length=1),t.reverse();n--;)t.push(0);t.reverse()}for(a=f.length,n=p.length,a-n<0&&(n=a,t=p,p=f,f=t),e=0;n;)e=(f[--n]=f[n]+p[n]+e)/Kt|0,f[n]%=Kt;for(e&&(f.unshift(e),++i),a=f.length;f[--a]==0;)f.pop();return o.d=f,o.e=Pi(f,i),j?O(o,l,d):o};k.precision=k.sd=function(o){var e,t=this;if(o!==void 0&&o!==!!o&&o!==1&&o!==0)throw Error(ue+o);return t.d?(e=Jo(t.d),o&&t.e+1>e&&(e=t.e+1)):e=NaN,e};k.round=function(){var o=this,e=o.constructor;return O(new e(o),o.e+1,e.rounding)};k.sine=k.sin=function(){var o,e,t=this,i=t.constructor;return t.isFinite()?t.isZero()?new i(t):(o=i.precision,e=i.rounding,i.precision=o+Math.max(t.e,t.sd())+R,i.rounding=1,t=ys(i,er(i,t)),i.precision=o,i.rounding=e,O(ne>2?t.neg():t,o,e,!0)):new i(NaN)};k.squareRoot=k.sqrt=function(){var o,e,t,i,n,r,a=this,l=a.d,d=a.e,f=a.s,p=a.constructor;if(f!==1||!l||!l[0])return new p(!f||f<0&&(!l||l[0])?NaN:l?a:1/0);for(j=!1,f=Math.sqrt(+a),f==0||f==1/0?(e=Et(l),(e.length+d)%2==0&&(e+="0"),f=Math.sqrt(e),d=jt((d+1)/2)-(d<0||d%2),f==1/0?e="5e"+d:(e=f.toExponential(),e=e.slice(0,e.indexOf("e")+1)+d),i=new p(e)):i=new p(f.toString()),t=(d=p.precision)+3;;)if(r=i,i=r.plus(W(a,r,t+2,1)).times(.5),Et(r.d).slice(0,t)===(e=Et(i.d)).slice(0,t))if(e=e.slice(t-3,t+1),e=="9999"||!n&&e=="4999"){if(!n&&(O(r,d+1,0),r.times(r).eq(a))){i=r;break}t+=4,n=1}else{(!+e||!+e.slice(1)&&e.charAt(0)=="5")&&(O(i,d+1,1),o=!i.times(i).eq(a));break}return j=!0,O(i,d,p.rounding,o)};k.tangent=k.tan=function(){var o,e,t=this,i=t.constructor;return t.isFinite()?t.isZero()?new i(t):(o=i.precision,e=i.rounding,i.precision=o+10,i.rounding=1,t=t.sin(),t.s=1,t=W(t,new i(1).minus(t.times(t)).sqrt(),o+10,0),i.precision=o,i.rounding=e,O(ne==2||ne==4?t.neg():t,o,e,!0)):new i(NaN)};k.times=k.mul=function(o){var e,t,i,n,r,a,l,d,f,p=this,w=p.constructor,P=p.d,M=(o=new w(o)).d;if(o.s*=p.s,!P||!P[0]||!M||!M[0])return new w(!o.s||P&&!P[0]&&!M||M&&!M[0]&&!P?NaN:!P||!M?o.s/0:o.s*0);for(t=jt(p.e/R)+jt(o.e/R),d=P.length,f=M.length,d<f&&(r=P,P=M,M=r,a=d,d=f,f=a),r=[],a=d+f,i=a;i--;)r.push(0);for(i=f;--i>=0;){for(e=0,n=d+i;n>i;)l=r[n]+M[i]*P[n-i-1]+e,r[n--]=l%Kt|0,e=l/Kt|0;r[n]=(r[n]+e)%Kt|0}for(;!r[--a];)r.pop();return e?++t:r.shift(),o.d=r,o.e=Pi(r,t),j?O(o,w.precision,w.rounding):o};k.toBinary=function(o,e){return gn(this,2,o,e)};k.toDecimalPlaces=k.toDP=function(o,e){var t=this,i=t.constructor;return t=new i(t),o===void 0?t:(Dt(o,0,de),e===void 0?e=i.rounding:Dt(e,0,8),O(t,o+t.e+1,e))};k.toExponential=function(o,e){var t,i=this,n=i.constructor;return o===void 0?t=Jt(i,!0):(Dt(o,0,de),e===void 0?e=n.rounding:Dt(e,0,8),i=O(new n(i),o+1,e),t=Jt(i,!0,o+1)),i.isNeg()&&!i.isZero()?"-"+t:t};k.toFixed=function(o,e){var t,i,n=this,r=n.constructor;return o===void 0?t=Jt(n):(Dt(o,0,de),e===void 0?e=r.rounding:Dt(e,0,8),i=O(new r(n),o+n.e+1,e),t=Jt(i,!1,o+i.e+1)),n.isNeg()&&!n.isZero()?"-"+t:t};k.toFraction=function(o){var e,t,i,n,r,a,l,d,f,p,w,P,M=this,A=M.d,S=M.constructor;if(!A)return new S(M);if(f=t=new S(1),i=d=new S(0),e=new S(i),r=e.e=Jo(A)-M.e-1,a=r%R,e.d[0]=It(10,a<0?R+a:a),o==null)o=r>0?e:f;else{if(l=new S(o),!l.isInt()||l.lt(f))throw Error(ue+l);o=l.gt(e)?r>0?e:f:l}for(j=!1,l=new S(Et(A)),p=S.precision,S.precision=r=A.length*R*2;w=W(l,e,0,1,1),n=t.plus(w.times(i)),n.cmp(o)!=1;)t=i,i=n,n=f,f=d.plus(w.times(n)),d=n,n=e,e=l.minus(w.times(n)),l=n;return n=W(o.minus(t),i,0,1,1),d=d.plus(n.times(f)),t=t.plus(n.times(i)),d.s=f.s=M.s,P=W(f,i,r,1).minus(M).abs().cmp(W(d,t,r,1).minus(M).abs())<1?[f,i]:[d,t],S.precision=p,j=!0,P};k.toHexadecimal=k.toHex=function(o,e){return gn(this,16,o,e)};k.toNearest=function(o,e){var t=this,i=t.constructor;if(t=new i(t),o==null){if(!t.d)return t;o=new i(1),e=i.rounding}else{if(o=new i(o),e===void 0?e=i.rounding:Dt(e,0,8),!t.d)return o.s?t:o;if(!o.d)return o.s&&(o.s=t.s),o}return o.d[0]?(j=!1,t=W(t,o,0,e,1).times(o),j=!0,O(t)):(o.s=t.s,t=o),t};k.toNumber=function(){return+this};k.toOctal=function(o,e){return gn(this,8,o,e)};k.toPower=k.pow=function(o){var e,t,i,n,r,a,l=this,d=l.constructor,f=+(o=new d(o));if(!l.d||!o.d||!l.d[0]||!o.d[0])return new d(It(+l,f));if(l=new d(l),l.eq(1))return l;if(i=d.precision,r=d.rounding,o.eq(1))return O(l,i,r);if(e=jt(o.e/R),e>=o.d.length-1&&(t=f<0?-f:f)<=hs)return n=Xo(d,l,t,i),o.s<0?new d(1).div(n):O(n,i,r);if(a=l.s,a<0){if(e<o.d.length-1)return new d(NaN);if((o.d[e]&1)==0&&(a=1),l.e==0&&l.d[0]==1&&l.d.length==1)return l.s=a,l}return t=It(+l,f),e=t==0||!isFinite(t)?jt(f*(Math.log("0."+Et(l.d))/Math.LN10+l.e+1)):new d(t+"").e,e>d.maxE+1||e<d.minE-1?new d(e>0?a/0:0):(j=!1,d.rounding=l.s=1,t=Math.min(12,(e+"").length),n=mn(o.times(le(l,i+t)),i),n.d&&(n=O(n,i+5,1),We(n.d,i,r)&&(e=i+10,n=O(mn(o.times(le(l,e+t)),e),e+5,1),+Et(n.d).slice(i+1,i+15)+1==1e14&&(n=O(n,i+1,0)))),n.s=a,j=!0,d.rounding=r,O(n,i,r))};k.toPrecision=function(o,e){var t,i=this,n=i.constructor;return o===void 0?t=Jt(i,i.e<=n.toExpNeg||i.e>=n.toExpPos):(Dt(o,1,de),e===void 0?e=n.rounding:Dt(e,0,8),i=O(new n(i),o,e),t=Jt(i,o<=i.e||i.e<=n.toExpNeg,o)),i.isNeg()&&!i.isZero()?"-"+t:t};k.toSignificantDigits=k.toSD=function(o,e){var t=this,i=t.constructor;return o===void 0?(o=i.precision,e=i.rounding):(Dt(o,1,de),e===void 0?e=i.rounding:Dt(e,0,8)),O(new i(t),o,e)};k.toString=function(){var o=this,e=o.constructor,t=Jt(o,o.e<=e.toExpNeg||o.e>=e.toExpPos);return o.isNeg()&&!o.isZero()?"-"+t:t};k.truncated=k.trunc=function(){return O(new this.constructor(this),this.e+1,1)};k.valueOf=k.toJSON=function(){var o=this,e=o.constructor,t=Jt(o,o.e<=e.toExpNeg||o.e>=e.toExpPos);return o.isNeg()?"-"+t:t};function Et(o){var e,t,i,n=o.length-1,r="",a=o[0];if(n>0){for(r+=a,e=1;e<n;e++)i=o[e]+"",t=R-i.length,t&&(r+=ce(t)),r+=i;a=o[e],i=a+"",t=R-i.length,t&&(r+=ce(t))}else if(a===0)return"0";for(;a%10===0;)a/=10;return r+a}function Dt(o,e,t){if(o!==~~o||o<e||o>t)throw Error(ue+o)}function We(o,e,t,i){var n,r,a,l;for(r=o[0];r>=10;r/=10)--e;return--e<0?(e+=R,n=0):(n=Math.ceil((e+1)/R),e%=R),r=It(10,R-e),l=o[n]%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)&&(o[n+1]/r/100|0)==It(10,e-2)-1||(l==r/2||l==0)&&(o[n+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)&&(o[n+1]/r/1e3|0)==It(10,e-3)-1,a}function mi(o,e,t){for(var i,n=[0],r,a=0,l=o.length;a<l;){for(r=n.length;r--;)n[r]*=e;for(n[0]+=_n.indexOf(o.charAt(a++)),i=0;i<n.length;i++)n[i]>t-1&&(n[i+1]===void 0&&(n[i+1]=0),n[i+1]+=n[i]/t|0,n[i]%=t)}return n.reverse()}function gs(o,e){var t,i,n;if(e.isZero())return e;i=e.d.length,i<32?(t=Math.ceil(i/3),n=(1/Ci(4,t)).toString()):(t=16,n="2.3283064365386962890625e-10"),o.precision+=t,e=Re(o,1,e.times(n),new o(1));for(var r=t;r--;){var a=e.times(e);e=a.times(a).minus(a).times(8).plus(1)}return o.precision-=t,e}var W=(function(){function o(i,n,r){var a,l=0,d=i.length;for(i=i.slice();d--;)a=i[d]*n+l,i[d]=a%r|0,l=a/r|0;return l&&i.unshift(l),i}function e(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 t(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,p,w,P,M,A,S,$,F,z,D,q,Ft,H,qt,te,Wt,ee,L,Nt,y=i.constructor,s=i.s==n.s?1:-1,u=i.d,_=n.d;if(!u||!u[0]||!_||!_[0])return new y(!i.s||!n.s||(u?_&&u[0]==_[0]:!_)?NaN:u&&u[0]==0||!_?s*0:s/0);for(d?(M=1,p=i.e-n.e):(d=Kt,M=R,p=jt(i.e/M)-jt(n.e/M)),L=_.length,Wt=u.length,F=new y(s),z=F.d=[],w=0;_[w]==(u[w]||0);w++);if(_[w]>(u[w]||0)&&p--,r==null?(H=r=y.precision,a=y.rounding):l?H=r+(i.e-n.e)+1:H=r,H<0)z.push(1),A=!0;else{if(H=H/M+2|0,w=0,L==1){for(P=0,_=_[0],H++;(w<Wt||P)&&H--;w++)qt=P*d+(u[w]||0),z[w]=qt/_|0,P=qt%_|0;A=P||w<Wt}else{for(P=d/(_[0]+1)|0,P>1&&(_=o(_,P,d),u=o(u,P,d),L=_.length,Wt=u.length),te=L,D=u.slice(0,L),q=D.length;q<L;)D[q++]=0;Nt=_.slice(),Nt.unshift(0),ee=_[0],_[1]>=d/2&&++ee;do P=0,f=e(_,D,L,q),f<0?(Ft=D[0],L!=q&&(Ft=Ft*d+(D[1]||0)),P=Ft/ee|0,P>1?(P>=d&&(P=d-1),S=o(_,P,d),$=S.length,q=D.length,f=e(S,D,$,q),f==1&&(P--,t(S,L<$?Nt:_,$,d))):(P==0&&(f=P=1),S=_.slice()),$=S.length,$<q&&S.unshift(0),t(D,S,q,d),f==-1&&(q=D.length,f=e(_,D,L,q),f<1&&(P++,t(D,L<q?Nt:_,q,d))),q=D.length):f===0&&(P++,D=[0]),z[w++]=P,f&&D[0]?D[q++]=u[te]||0:(D=[u[te]],q=1);while((te++<Wt||D[0]!==void 0)&&H--);A=D[0]!==void 0}z[0]||z.shift()}if(M==1)F.e=p,Ho=A;else{for(w=1,P=z[0];P>=10;P/=10)w++;F.e=w+p*M-1,O(F,l?r+F.e+1:r,a,A)}return F}})();function O(o,e,t,i){var n,r,a,l,d,f,p,w,P,M=o.constructor;t:if(e!=null){if(w=o.d,!w)return o;for(n=1,l=w[0];l>=10;l/=10)n++;if(r=e-n,r<0)r+=R,a=e,p=w[P=0],d=p/It(10,n-a-1)%10|0;else if(P=Math.ceil((r+1)/R),l=w.length,P>=l)if(i){for(;l++<=P;)w.push(0);p=d=0,n=1,r%=R,a=r-R+1}else break t;else{for(p=l=w[P],n=1;l>=10;l/=10)n++;r%=R,a=r-R+n,d=a<0?0:p/It(10,n-a-1)%10|0}if(i=i||e<0||w[P+1]!==void 0||(a<0?p:p%It(10,n-a-1)),f=t<4?(d||i)&&(t==0||t==(o.s<0?3:2)):d>5||d==5&&(t==4||i||t==6&&(r>0?a>0?p/It(10,n-a):0:w[P-1])%10&1||t==(o.s<0?8:7)),e<1||!w[0])return w.length=0,f?(e-=o.e+1,w[0]=It(10,(R-e%R)%R),o.e=-e||0):w[0]=o.e=0,o;if(r==0?(w.length=P,l=1,P--):(w.length=P+1,l=It(10,R-r),w[P]=a>0?(p/It(10,n-a)%It(10,a)|0)*l:0),f)for(;;)if(P==0){for(r=1,a=w[0];a>=10;a/=10)r++;for(a=w[0]+=l,l=1;a>=10;a/=10)l++;r!=l&&(o.e++,w[0]==Kt&&(w[0]=1));break}else{if(w[P]+=l,w[P]!=Kt)break;w[P--]=0,l=1}for(r=w.length;w[--r]===0;)w.pop()}return j&&(o.e>M.maxE?(o.d=null,o.e=NaN):o.e<M.minE&&(o.e=0,o.d=[0])),o}function Jt(o,e,t){if(!o.isFinite())return tr(o);var i,n=o.e,r=Et(o.d),a=r.length;return e?(t&&(i=t-a)>0?r=r.charAt(0)+"."+r.slice(1)+ce(i):a>1&&(r=r.charAt(0)+"."+r.slice(1)),r=r+(o.e<0?"e":"e+")+o.e):n<0?(r="0."+ce(-n-1)+r,t&&(i=t-a)>0&&(r+=ce(i))):n>=a?(r+=ce(n+1-a),t&&(i=t-n-1)>0&&(r=r+"."+ce(i))):((i=n+1)<a&&(r=r.slice(0,i)+"."+r.slice(i)),t&&(i=t-a)>0&&(n+1===a&&(r+="."),r+=ce(i))),r}function Pi(o,e){var t=o[0];for(e*=R;t>=10;t/=10)e++;return e}function wi(o,e,t){if(e>ms)throw j=!0,t&&(o.precision=t),Error(Zo);return O(new o(bi),e,1,!0)}function Qt(o,e,t){if(e>hn)throw Error(Zo);return O(new o(yi),e,t,!0)}function Jo(o){var e=o.length-1,t=e*R+1;if(e=o[e],e){for(;e%10==0;e/=10)t--;for(e=o[0];e>=10;e/=10)t++}return t}function ce(o){for(var e="";o--;)e+="0";return e}function Xo(o,e,t,i){var n,r=new o(1),a=Math.ceil(i/R+4);for(j=!1;;){if(t%2&&(r=r.times(e),Ko(r.d,a)&&(n=!0)),t=jt(t/2),t===0){t=r.d.length-1,n&&r.d[t]===0&&++r.d[t];break}e=e.times(e),Ko(e.d,a)}return j=!0,r}function Vo(o){return o.d[o.d.length-1]&1}function Yo(o,e,t){for(var i,n,r=new o(e[0]),a=0;++a<e.length;){if(n=new o(e[a]),!n.s){r=n;break}i=r.cmp(n),(i===t||i===0&&r.s===t)&&(r=n)}return r}function mn(o,e){var t,i,n,r,a,l,d,f=0,p=0,w=0,P=o.constructor,M=P.rounding,A=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(e==null?(j=!1,d=A):d=e,l=new P(.03125);o.e>-2;)o=o.times(l),w+=5;for(i=Math.log(It(2,w))/Math.LN10*2+5|0,d+=i,t=r=a=new P(1),P.precision=d;;){if(r=O(r.times(o),d,1),t=t.times(++p),l=a.plus(W(r,t,d,1)),Et(l.d).slice(0,d)===Et(a.d).slice(0,d)){for(n=w;n--;)a=O(a.times(a),d,1);if(e==null)if(f<3&&We(a.d,d-i,M,f))P.precision=d+=10,t=r=l=new P(1),p=0,f++;else return O(a,P.precision=A,M,j=!0);else return P.precision=A,a}a=l}}function le(o,e){var t,i,n,r,a,l,d,f,p,w,P,M=1,A=10,S=o,$=S.d,F=S.constructor,z=F.rounding,D=F.precision;if(S.s<0||!$||!$[0]||!S.e&&$[0]==1&&$.length==1)return new F($&&!$[0]?-1/0:S.s!=1?NaN:$?0:S);if(e==null?(j=!1,p=D):p=e,F.precision=p+=A,t=Et($),i=t.charAt(0),Math.abs(r=S.e)<15e14){for(;i<7&&i!=1||i==1&&t.charAt(1)>3;)S=S.times(o),t=Et(S.d),i=t.charAt(0),M++;r=S.e,i>1?(S=new F("0."+t),r++):S=new F(i+"."+t.slice(1))}else return f=wi(F,p+2,D).times(r+""),S=le(new F(i+"."+t.slice(1)),p-A).plus(f),F.precision=D,e==null?O(S,D,z,j=!0):S;for(w=S,d=a=S=W(S.minus(1),S.plus(1),p,1),P=O(S.times(S),p,1),n=3;;){if(a=O(a.times(P),p,1),f=d.plus(W(a,new F(n),p,1)),Et(f.d).slice(0,p)===Et(d.d).slice(0,p))if(d=d.times(2),r!==0&&(d=d.plus(wi(F,p+2,D).times(r+""))),d=W(d,new F(M),p,1),e==null)if(We(d.d,p-A,z,l))F.precision=p+=A,f=a=S=W(w.minus(1),w.plus(1),p,1),P=O(S.times(S),p,1),n=l=1;else return O(d,F.precision=D,z,j=!0);else return F.precision=D,d;d=f,n+=2}}function tr(o){return String(o.s*o.s/0)}function gi(o,e){var t,i,n;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(n=e.length;e.charCodeAt(n-1)===48;--n);if(e=e.slice(i,n),e){if(n-=i,o.e=t=t-i-1,o.d=[],i=(t+1)%R,t<0&&(i+=R),i<n){for(i&&o.d.push(+e.slice(0,i)),n-=R;i<n;)o.d.push(+e.slice(i,i+=R));e=e.slice(i),i=R-e.length}else i-=n;for(;i--;)e+="0";o.d.push(+e),j&&(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 bs(o,e){var t,i,n,r,a,l,d,f,p;if(e.indexOf("_")>-1){if(e=e.replace(/(\d)_(?=\d)/g,"$1"),Qo.test(e))return gi(o,e)}else if(e==="Infinity"||e==="NaN")return+e||(o.s=NaN),o.e=NaN,o.d=null,o;if(_s.test(e))t=16,e=e.toLowerCase();else if(fs.test(e))t=2;else if(ps.test(e))t=8;else throw Error(ue+e);for(r=e.search(/p/i),r>0?(d=+e.slice(r+1),e=e.substring(2,r)):e=e.slice(2),r=e.indexOf("."),a=r>=0,i=o.constructor,a&&(e=e.replace(".",""),l=e.length,r=l-r,n=Xo(i,new i(t),r,r*2)),f=mi(e,t,Kt),p=f.length-1,r=p;f[r]===0;--r)f.pop();return r<0?new i(o.s*0):(o.e=Pi(f,p),o.d=f,j=!1,a&&(o=W(o,n,l*4)),d&&(o=o.times(Math.abs(d)<54?It(2,d):He.pow(2,d))),j=!0,o)}function ys(o,e){var t,i=e.d.length;if(i<3)return e.isZero()?e:Re(o,2,e,e);t=1.4*Math.sqrt(i),t=t>16?16:t|0,e=e.times(1/Ci(5,t)),e=Re(o,2,e,e);for(var n,r=new o(5),a=new o(16),l=new o(20);t--;)n=e.times(e),e=e.times(r.plus(n.times(a.times(n).minus(l))));return e}function Re(o,e,t,i,n){var r,a,l,d,f=1,p=o.precision,w=Math.ceil(p/R);for(j=!1,d=t.times(t),l=new o(i);;){if(a=W(l.times(d),new o(e++*e++),p,1),l=n?i.plus(a):i.minus(a),i=W(a.times(d),new o(e++*e++),p,1),a=l.plus(i),a.d[w]!==void 0){for(r=w;a.d[r]===l.d[r]&&r--;);if(r==-1)break}r=l,l=i,i=a,a=r,f++}return j=!0,a.d.length=w+1,a}function Ci(o,e){for(var t=o;--e;)t*=o;return t}function er(o,e){var t,i=e.s<0,n=Qt(o,o.precision,1),r=n.times(.5);if(e=e.abs(),e.lte(r))return ne=i?4:1,e;if(t=e.divToInt(n),t.isZero())ne=i?3:2;else{if(e=e.minus(t.times(n)),e.lte(r))return ne=Vo(t)?i?2:3:i?4:1,e;ne=Vo(t)?i?1:4:i?3:2}return e.minus(n).abs()}function gn(o,e,t,i){var n,r,a,l,d,f,p,w,P,M=o.constructor,A=t!==void 0;if(A?(Dt(t,1,de),i===void 0?i=M.rounding:Dt(i,0,8)):(t=M.precision,i=M.rounding),!o.isFinite())p=tr(o);else{for(p=Jt(o),a=p.indexOf("."),A?(n=2,e==16?t=t*4-3:e==8&&(t=t*3-2)):n=e,a>=0&&(p=p.replace(".",""),P=new M(1),P.e=p.length-a,P.d=mi(Jt(P),10,n),P.e=P.d.length),w=mi(p,10,n),r=d=w.length;w[--d]==0;)w.pop();if(!w[0])p=A?"0p+0":"0";else{if(a<0?r--:(o=new M(o),o.d=w,o.e=r,o=W(o,P,t,i,0,n),w=o.d,r=o.e,f=Ho),a=w[t],l=n/2,f=f||w[t+1]!==void 0,f=i<4?(a!==void 0||f)&&(i===0||i===(o.s<0?3:2)):a>l||a===l&&(i===4||f||i===6&&w[t-1]&1||i===(o.s<0?8:7)),w.length=t,f)for(;++w[--t]>n-1;)w[t]=0,t||(++r,w.unshift(1));for(d=w.length;!w[d-1];--d);for(a=0,p="";a<d;a++)p+=_n.charAt(w[a]);if(A){if(d>1)if(e==16||e==8){for(a=e==16?4:3,--d;d%a;d++)p+="0";for(w=mi(p,n,e),d=w.length;!w[d-1];--d);for(a=1,p="1.";a<d;a++)p+=_n.charAt(w[a])}else p=p.charAt(0)+"."+p.slice(1);p=p+(r<0?"p":"p+")+r}else if(r<0){for(;++r;)p="0"+p;p="0."+p}else if(++r>d)for(r-=d;r--;)p+="0";else r<d&&(p=p.slice(0,r)+"."+p.slice(r))}p=(e==16?"0x":e==2?"0b":e==8?"0o":"")+p}return o.s<0?"-"+p:p}function Ko(o,e){if(o.length>e)return o.length=e,!0}function ws(o){return new this(o).abs()}function vs(o){return new this(o).acos()}function Ps(o){return new this(o).acosh()}function Cs(o,e){return new this(o).plus(e)}function Ms(o){return new this(o).asin()}function ks(o){return new this(o).asinh()}function Ts(o){return new this(o).atan()}function As(o){return new this(o).atanh()}function Ss(o,e){o=new this(o),e=new this(e);var t,i=this.precision,n=this.rounding,r=i+4;return!o.s||!e.s?t=new this(NaN):!o.d&&!e.d?(t=Qt(this,r,1).times(e.s>0?.25:.75),t.s=o.s):!e.d||o.isZero()?(t=e.s<0?Qt(this,i,n):new this(0),t.s=o.s):!o.d||e.isZero()?(t=Qt(this,r,1).times(.5),t.s=o.s):e.s<0?(this.precision=r,this.rounding=1,t=this.atan(W(o,e,r,1)),e=Qt(this,r,1),this.precision=i,this.rounding=n,t=o.s<0?t.minus(e):t.plus(e)):t=this.atan(W(o,e,r,1)),t}function xs(o){return new this(o).cbrt()}function Is(o){return O(o=new this(o),o.e+1,2)}function Os(o,e,t){return new this(o).clamp(e,t)}function Es(o){if(!o||typeof o!="object")throw Error(vi+"Object expected");var e,t,i,n=o.defaults===!0,r=["precision",1,de,"rounding",0,8,"toExpNeg",-Ee,0,"toExpPos",0,Ee,"maxE",0,Ee,"minE",-Ee,0,"modulo",0,9];for(e=0;e<r.length;e+=3)if(t=r[e],n&&(this[t]=pn[t]),(i=o[t])!==void 0)if(jt(i)===i&&i>=r[e+1]&&i<=r[e+2])this[t]=i;else throw Error(ue+t+": "+i);if(t="crypto",n&&(this[t]=pn[t]),(i=o[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(zo);else this[t]=!1;else throw Error(ue+t+": "+i);return this}function Rs(o){return new this(o).cos()}function js(o){return new this(o).cosh()}function ir(o){var e,t,i;function n(r){var a,l,d,f=this;if(!(f instanceof n))return new n(r);if(f.constructor=n,Wo(r)){f.s=r.s,j?!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++;j?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 gi(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),Qo.test(r)?gi(f,r):bs(f,r);if(d==="bigint")return r<0?(r=-r,f.s=-1):f.s=1,gi(f,r.toString());throw Error(ue+r)}if(n.prototype=k,n.ROUND_UP=0,n.ROUND_DOWN=1,n.ROUND_CEIL=2,n.ROUND_FLOOR=3,n.ROUND_HALF_UP=4,n.ROUND_HALF_DOWN=5,n.ROUND_HALF_EVEN=6,n.ROUND_HALF_CEIL=7,n.ROUND_HALF_FLOOR=8,n.EUCLID=9,n.config=n.set=Es,n.clone=ir,n.isDecimal=Wo,n.abs=ws,n.acos=vs,n.acosh=Ps,n.add=Cs,n.asin=Ms,n.asinh=ks,n.atan=Ts,n.atanh=As,n.atan2=Ss,n.cbrt=xs,n.ceil=Is,n.clamp=Os,n.cos=Rs,n.cosh=js,n.div=Ns,n.exp=Bs,n.floor=Ds,n.hypot=Fs,n.ln=qs,n.log=Ls,n.log10=Us,n.log2=$s,n.max=Vs,n.min=Ks,n.mod=Ws,n.mul=Hs,n.pow=Zs,n.random=zs,n.round=Gs,n.sign=Qs,n.sin=Js,n.sinh=Xs,n.sqrt=Ys,n.sub=ta,n.sum=ea,n.tan=ia,n.tanh=na,n.trunc=oa,o===void 0&&(o={}),o&&o.defaults!==!0)for(i=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],e=0;e<i.length;)o.hasOwnProperty(t=i[e++])||(o[t]=this[t]);return n.config(o),n}function Ns(o,e){return new this(o).div(e)}function Bs(o){return new this(o).exp()}function Ds(o){return O(o=new this(o),o.e+1,3)}function Fs(){var o,e,t=new this(0);for(j=!1,o=0;o<arguments.length;)if(e=new this(arguments[o++]),e.d)t.d&&(t=t.plus(e.times(e)));else{if(e.s)return j=!0,new this(1/0);t=e}return j=!0,t.sqrt()}function Wo(o){return o instanceof He||o&&o.toStringTag===Go||!1}function qs(o){return new this(o).ln()}function Ls(o,e){return new this(o).log(e)}function $s(o){return new this(o).log(2)}function Us(o){return new this(o).log(10)}function Vs(){return Yo(this,arguments,-1)}function Ks(){return Yo(this,arguments,1)}function Ws(o,e){return new this(o).mod(e)}function Hs(o,e){return new this(o).mul(e)}function Zs(o,e){return new this(o).pow(e)}function zs(o){var e,t,i,n,r=0,a=new this(1),l=[];if(o===void 0?o=this.precision:Dt(o,1,de),i=Math.ceil(o/R),this.crypto)if(crypto.getRandomValues)for(e=crypto.getRandomValues(new Uint32Array(i));r<i;)n=e[r],n>=429e7?e[r]=crypto.getRandomValues(new Uint32Array(1))[0]:l[r++]=n%1e7;else if(crypto.randomBytes){for(e=crypto.randomBytes(i*=4);r<i;)n=e[r]+(e[r+1]<<8)+(e[r+2]<<16)+((e[r+3]&127)<<24),n>=214e7?crypto.randomBytes(4).copy(e,r):(l.push(n%1e7),r+=4);r=i/4}else throw Error(zo);else for(;r<i;)l[r++]=Math.random()*1e7|0;for(i=l[--r],o%=R,i&&o&&(n=It(10,R-o),l[r]=(i/n|0)*n);l[r]===0;r--)l.pop();if(r<0)t=0,l=[0];else{for(t=-1;l[0]===0;t-=R)l.shift();for(i=1,n=l[0];n>=10;n/=10)i++;i<R&&(t-=R-i)}return a.e=t,a.d=l,a}function Gs(o){return O(o=new this(o),o.e+1,this.rounding)}function Qs(o){return o=new this(o),o.d?o.d[0]?o.s:0*o.s:o.s||NaN}function Js(o){return new this(o).sin()}function Xs(o){return new this(o).sinh()}function Ys(o){return new this(o).sqrt()}function ta(o,e){return new this(o).sub(e)}function ea(){var o=0,e=arguments,t=new this(e[o]);for(j=!1;t.s&&++o<e.length;)t=t.plus(e[o]);return j=!0,O(t,this.precision,this.rounding)}function ia(o){return new this(o).tan()}function na(o){return new this(o).tanh()}function oa(o){return O(o=new this(o),o.e+1,1)}k[Symbol.for("nodejs.util.inspect.custom")]=k.toString;k[Symbol.toStringTag]="Decimal";var He=k.constructor=ir(pn);bi=new He(bi);yi=new He(yi);var E=He;var Ti=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:n="all",order:r="ascending"}){let{FullClient:a}=this._sdk,l=[e];t&&l.push(t);let d;i?d=la(new ra({url:i,network:this._sdk.sdkOptions.env==="testnet"?"testnet":"mainnet"})):d=a;let f={data:[],has_next_page:!1};try{let p=await d.queryTransactionBlocksByPage({ChangedObject:e},n,r);return p.data.forEach((w,P)=>{let M=So(w,P,l);f.data=[...f.data,...M]}),f.has_next_page=p.has_next_page,f.next_cursor=p.next_cursor,f}catch(p){Bt("FetchError",p,{[fe.METHOD_NAME]:"getPositionTransactionList"})}return f}async getPositionList(e,t=[],i=!0){let n=[],r=await this._sdk.FullClient.getOwnedObjectsByPage(e,{options:{showType:!0,showContent:!0,showDisplay:i,showOwner:!0},filter:{Package:this._sdk.sdkOptions.clmm_pool.package_id}}),a=t.length>0;for(let l of r.data)if(ca(l.data.type).full_address===this.buildPositionType()){let f=li(l),p=`${f.pos_object_id}_getPositionList`;this._sdk.updateCache(p,f),a?t.includes(f.pool)&&n.push(f):n.push(f)}return n}async getPosition(e,t,i=!0,n=!0){let r=await this.getSimplePosition(t,n);return i&&(r=await this.updatePositionInfo(e,r)),r}async getPositionById(e,t=!0,i=!0){let n=await this.getSimplePosition(e,i);if(t){let r=await this._sdk.Pool.getPool(n.pool,!1);return await this.updatePositionInfo(r.position_manager.positions_handle,n)}return n}async getSimplePosition(e,t=!0){let i=`${e}_getPositionList`,n=this.getSimplePositionByCache(e);if(n===void 0){let r=await this.sdk.FullClient.getObject({id:e,options:{showContent:!0,showType:!0,showDisplay:t,showOwner:!0}});n=li(r),this._sdk.updateCache(i,n)}return n}getSimplePositionByCache(e){let t=`${e}_getPositionList`;return this._sdk.getCache(t)}async getSimplePositionList(e,t=!0){let i=[],n=[];return e.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:t,showType:!0})).forEach(a=>{if(a.error==null){let l=li(a);i.push(l);let d=`${l.pos_object_id}_getPositionList`;this._sdk.updateCache(d,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.getDynamicFieldObject({parentId:e,name:{type:"0x2::object::ID",value:t}}),r=or(i.data).value.fields.value;return Ke(r)}catch(i){return Bt("FetchError",i,{[fe.METHOD_NAME]:"getPositionInfo",[fe.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(d=>{let f=ua(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=or(r.data).value.fields.value,d=Ke(l);t.push(d)}catch(a){console.log("getPositionInfoList error",a)}}),t)}catch(t){return Bt("FetchError",t,{[fe.METHOD_NAME]:"getPositionInfoList",[fe.REQUEST_PARAMS]:{options:e}})}}buildFetchPosFee(e,t){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r=[e.coin_type_a,e.coin_type_b],a=[t.object(Yt(i).global_config_id),t.object(e.pool_id),t.pure.address(e.position_id)];t.moveCall({target:`${n.published_at}::${zt}::fetch_position_fees`,arguments:a,typeArguments:r})}parsedPosFeeData(e){let t={},i=e.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};t[r.position_id]=a}return t}async fetchPosFeeAmount(e){let{clmm_pool:t,integrate:i}=this.sdk.sdkOptions,n=new oe;for(let d of e)this.buildFetchPosFee(d,n);let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:aa("0x0")});r.error!=null&&N("InvalidPoolObject",`fetch position fee error code: ${r.error??"unknown error"}, please check config and position and pool object ids`,{[fe.METHOD_NAME]:"fetchPosFeeAmount"});let a=[],l=this.parsedPosFeeData(r);for(let d=0;d<e.length;d+=1){let f=l[e[d].position_id];if(f){let p={fee_owned_a:f.fee_owned_a,fee_owned_b:f.fee_owned_b,position_id:e[d].position_id};a.push(p)}}return a}async batchFetchPositionFees(e){let t=[];for(let n of e){let r=await this._sdk.Position.getPositionById(n,!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 n=await this.fetchPosFeeAmount(t);for(let r of n)i[r.position_id]=r;return i}return i}async createAddLiquidityFixTokenPayload(e,t,i,n){return Vt.buildAddLiquidityFixToken(this._sdk,e,t,i,n)}async createAddLiquidityPayload(e,t,i,n){let{integrate:r,clmm_pool:a}=this._sdk.sdkOptions,l=je(BigInt(e.tick_lower)).toString(),d=je(BigInt(e.tick_upper)).toString(),f=[e.coin_type_a,e.coin_type_b];t=t||new oe;let p=!sa(e.pos_id),w=BigInt(e.max_amount_a),P=BigInt(e.max_amount_b),M=i||Ne.buildCoinWithBalance(w,e.coin_type_a,t),A=n||Ne.buildCoinWithBalance(P,e.coin_type_b,t);return p?t.moveCall({target:`${r.published_at}::${Zt}::open_position_with_liquidity`,typeArguments:f,arguments:[t.object(Yt(a).global_config_id),t.object(e.pool_id),t.pure.u32(Number(l)),t.pure.u32(Number(d)),M,A,t.pure.u64(e.max_amount_a),t.pure.u64(e.max_amount_b),t.pure.u128(e.delta_liquidity),t.object(Mi)]}):(t=Vt.createCollectRewarderAndFeeParams(this._sdk,t,e),t.moveCall({target:`${r.published_at}::${Zt}::add_liquidity`,typeArguments:f,arguments:[t.object(Yt(a).global_config_id),t.object(e.pool_id),t.object(e.pos_id),M,A,t.pure.u64(e.max_amount_a),t.pure.u64(e.max_amount_b),t.pure.u128(e.delta_liquidity),t.object(Mi)]})),t}async removeLiquidityPayload(e,t){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;t=t||new oe;let r=[e.coin_type_a,e.coin_type_b];t=Vt.createCollectRewarderAndFeeParams(this._sdk,t,e);let a=[t.object(Yt(i).global_config_id),t.object(e.pool_id),t.object(e.pos_id),t.pure.u128(e.delta_liquidity),t.object(Mi)],[l,d]=t.moveCall({target:`${i.published_at}::pool::remove_liquidity`,typeArguments:r,arguments:a}),f=Ne.fromBalance(l,e.coin_type_a,t),p=Ne.fromBalance(d,e.coin_type_b,t);return t.moveCall({target:`${n.published_at}::${pe}::check_coin_threshold`,typeArguments:[e.coin_type_a],arguments:[f,t.pure.u64(e.min_amount_a)]}),t.moveCall({target:`${n.published_at}::${pe}::check_coin_threshold`,typeArguments:[e.coin_type_b],arguments:[p,t.pure.u64(e.min_amount_b)]}),e.is_return_coins?{coin_a:f,coin_b:p}:(t.transferObjects([f,p],this.sdk.getSenderAddress()),t)}async closePositionPayload(e,t){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;t=t||new oe;let r=[e.coin_type_a,e.coin_type_b];return t=Vt.createCollectRewarderAndFeeParams(this._sdk,t,e),t.moveCall({target:`${n.published_at}::${ni}::close_position`,typeArguments:r,arguments:[t.object(Yt(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(Mi)]}),t}openPositionPayload(e,t){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;t=t||new oe;let r=[e.coin_type_a,e.coin_type_b],a=je(BigInt(e.tick_lower)).toString(),l=je(BigInt(e.tick_upper)).toString(),d=[t.object(Yt(i).global_config_id),t.object(e.pool_id),t.pure.u32(Number(a)),t.pure.u32(Number(l))];return t.moveCall({target:`${n.published_at}::${ni}::open_position`,typeArguments:r,arguments:d}),t}async openPositionWithPricePayload(e,t){let{pool_id:i}=e,{clmm_pool:n,integrate:r}=this.sdk.sdkOptions;t=t||new oe;let a=await this.sdk.Pool.getPool(i,!1),l=Number(a.tick_spacing),d=0,f=0;if(e.is_full_range)d=ki.getMinIndex(l),f=ki.getMaxIndex(l);else{let{price_base_coin:P,min_price:M,max_price:A}=e;d=Be.priceToInitializeTickIndex(P==="coin_a"?Xt(M):Xt(1).div(A),e.coin_decimals_a,e.coin_decimals_b,l),f=Be.priceToInitializeTickIndex(P==="coin_a"?Xt(A):Xt(1).div(M),e.coin_decimals_a,e.coin_decimals_b,l)}let p=[a.coin_type_a,a.coin_type_b],w=[t.object(Yt(n).global_config_id),t.object(e.pool_id),t.pure.u32(Number(je(BigInt(d)))),t.pure.u32(Number(je(BigInt(f))))];return t.moveCall({target:`${r.published_at}::${ni}::open_position`,typeArguments:p,arguments:w}),t}async collectFeePayload(e,t,i,n){t=t||new oe;let r=i||Ne.buildCoinWithBalance(BigInt(0),e.coin_type_a,t),a=n||Ne.buildCoinWithBalance(BigInt(0),e.coin_type_b,t);return this.createCollectFeePayload(e,t,r,a),t}createCollectFeePayload(e,t,i,n){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,l=[e.coin_type_a,e.coin_type_b],d=[t.object(Yt(r).global_config_id),t.object(e.pool_id),t.object(e.pos_id),i,n];return t.moveCall({target:`${a.published_at}::${Zt}::collect_fee`,typeArguments:l,arguments:d}),t}async calculateAddLiquidityResultWithPrice(e){let{pool_id:t,slippage:i,refresh_pool_price:n,add_mode_params:r}=e,a=await this.sdk.Pool.getPool(t,n),l=Number(a.tick_spacing),d=0,f=0;if(r.is_full_range)d=ki.getMinIndex(l),f=ki.getMaxIndex(l);else{let{price_base_coin:F,min_price:z,max_price:D}=r;d=Be.priceToInitializeTickIndex(F==="coin_a"?Xt(z):Xt(1).div(D),r.coin_decimals_a,r.coin_decimals_b,l),f=Be.priceToInitializeTickIndex(F==="coin_a"?Xt(D):Xt(1).div(z),r.coin_decimals_a,r.coin_decimals_b,l)}if("liquidity"in e){let{liquidity:F}=e,z=Be.tickIndexToSqrtPriceX64(d),D=Be.tickIndexToSqrtPriceX64(f),{coin_amount_a:q,coin_amount_b:Ft}=nr.getCoinAmountFromLiquidity(new Ze.default(F),new Ze.default(a.current_sqrt_price),z,D,!1),H=Xt(q).mul(1+i).toFixed(0,E.ROUND_UP),qt=Xt(Ft).mul(1+i).toFixed(0,E.ROUND_UP);return{coin_amount_a:q,coin_amount_b:Ft,coin_amount_limit_a:H,coin_amount_limit_b:qt,liquidity:F,tick_lower:d,tick_upper:f}}let{coin_amount:p,fix_amount_a:w}=e,{coin_amount_limit_a:P,coin_amount_limit_b:M,liquidity_amount:A,coin_amount_a:S,coin_amount_b:$}=nr.estLiquidityAndCoinAmountFromOneAmounts(d,f,new Ze.default(p),w,!0,i,new Ze.default(a.current_sqrt_price));return{coin_amount_a:S,coin_amount_b:$,coin_amount_limit_a:P,coin_amount_limit_b:M,liquidity:A,tick_lower:d,tick_upper:f,fix_amount_a:w}}async addLiquidityWithPricePayload(e,t,i,n){let{pool_id:r,calculate_result:a}=e,{coin_amount_limit_a:l,coin_amount_limit_b:d,liquidity:f,tick_lower:p,tick_upper:w}=a;t=t||new oe;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:p,tick_upper:w,collect_fee:!1,rewarder_coin_types:[],coin_type_a:P.coin_type_a,coin_type_b:P.coin_type_b,pool_id:r,pos_id:""},t,i,n),t}async createAddLiquidityFixCoinWithPricePayload(e,t,i,n){let{pool_id:r,calculate_result:a}=e,{coin_amount_limit_a:l,coin_amount_limit_b:d,liquidity:f,tick_lower:p,tick_upper:w,fix_amount_a:P,coin_amount_a:M,coin_amount_b:A}=a;if(P===void 0)throw N("InvalidParams","fix_amount_a is undefined",{[fe.METHOD_NAME]:"addLiquidityFixCoinCoinWithPricePayload",[fe.REQUEST_PARAMS]:e});t=t||new oe;let S=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityFixTokenPayload({amount_a:P?M:l,amount_b:P?d:A,slippage:0,fix_amount_a:P,is_open:!0,tick_lower:p,tick_upper:w,collect_fee:!1,rewarder_coin_types:[],coin_type_a:S.coin_type_a,coin_type_b:S.coin_type_b,pool_id:r,pos_id:""},t,i,n),t}createCollectFeeNoSendPayload(e,t,i,n){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,l=[e.coin_type_a,e.coin_type_b],d=[t.object(Yt(r).global_config_id),t.object(e.pool_id),t.object(e.pos_id),i,n];return t.moveCall({target:`${a.published_at}::${oi}::collect_fee`,typeArguments:l,arguments:d}),t}createCollectFeeAndReturnCoinsPayload(e,t){let{clmm_pool:i}=this.sdk.sdkOptions,n=[e.coin_type_a,e.coin_type_b],r=[t.object(Yt(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:n,arguments:r});return{fee_a:a,fee_b:l}}};var vn=re(ie(),1);import{Transaction as bn}from"@mysten/sui/transactions";import{normalizeSuiAddress as da}from"@mysten/sui/utils";import{CLOCK_ADDRESS as Ai,CoinAssist as yn,DETAILS_KEYS as rr,getPackagerConfigs as ze,MathUtil as fa,normalizeCoinType as wn,ZERO as De}from"@cetusprotocol/common-sdk";var Si=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 n=[];for(let r of i){let a=fa.fromX64(new vn.default(r.emissions_per_second));n.push({emissions:Math.floor(a.toNumber()*60*60*24),coin_type:r.coin_type})}return n}async batchFetchPositionRewarders(e){let t=[];for(let n of e){let r=await this._sdk.Position.getPositionById(n,!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 n=await this.fetchPosRewardersAmount(t);for(let r of n)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:n}=this.sdk.sdkOptions,r=[e.coin_type_a,e.coin_type_b],a=[t.object(ze(i).global_config_id),t.object(e.pool_id),t.pure.address(e.position_id),t.object(Ai)];t.moveCall({target:`${n.published_at}::${zt}::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.devInspectTransactionBlock({transactionBlock:t,sender:da("0x")});i.error!=null&&N("InvalidConfig",`fetch position rewards error code: ${i.error??"unknown error"}, please check config and params`,{[rr.METHOD_NAME]:"fetchPosRewardersAmount",[rr.REQUEST_PARAMS]:{params:e}});let n=this.parsedPosRewardData(i),r=[];for(let a=0;a<e.length;a+=1){let l=n[e[a].position_id];if(l){let d={pool_id:e[a].pool_id,position_id:e[a].position_id,rewarder_amounts:l.rewarder_amount.map((f,p)=>({amount_owned:f,coin_type:e[a].rewarder_types[p]}))};r.push(d)}}return r}parsedPosRewardData(e){let t={},i=e.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};t[r.position_id]=a}return t}async fetchPoolRewardersAmount(e,t){let i=await this.sdk.Pool.getPool(t),n=await this.sdk.Position.getPositionList(e,[t]),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=[De,De,De];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 vn.default(d.rewarder_amounts[f].amount_owned));return l}async getPoolLowerAndUpperTicks(e,t){let i=[],n=[];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),n.push(l)}return[i,n]}async collectRewarderPayload(e){let t=new bn;return t=Vt.createCollectRewarderAndFeeParams(this._sdk,t,e),t}async batchCollectRewardsPayload(e,t,i,n){t=t||new bn;let r={};return e.forEach(a=>{let l=wn(a.coin_type_a),d=wn(a.coin_type_b);if(a.collect_fee){let p=r[l];p==null&&(p=p||yn.buildCoinWithBalance(BigInt(0),l,t),r[l]=p);let w=r[d];w==null&&(w=w||yn.buildCoinWithBalance(BigInt(0),d,t),r[d]=w),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,p,w)}let f=[];a.rewarder_coin_types.forEach(p=>{let w=wn(p),P=r[p];P===void 0&&(P=yn.buildCoinWithBalance(BigInt(0),w,t),r[w]=P),f.push(P)}),t=this.createCollectRewarderNoSendPayload(a,t,f)}),Object.keys(r).forEach(a=>{let l=r[a];Io(this.sdk,t,l,a,this.sdk.getSenderAddress())}),t}createCollectRewarderPayload(e,t,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=ze(n),l=[e.coin_type_a,e.coin_type_b];return e.rewarder_coin_types.forEach((d,f)=>{t&&t.moveCall({target:`${r.published_at}::${Zt}::collect_reward`,typeArguments:[...l,d],arguments:[t.object(a.global_config_id),t.object(e.pool_id),t.object(e.pos_id),t.object(a.global_vault_id),i[f],t.object(Ai)]})}),t}createCollectRewarderNoSendPayload(e,t,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=ze(n),l=[e.coin_type_a,e.coin_type_b];return e.rewarder_coin_types.forEach((d,f)=>{t&&t.moveCall({target:`${r.published_at}::${oi}::collect_reward`,typeArguments:[...l,d],arguments:[t.object(a.global_config_id),t.object(e.pool_id),t.object(e.pos_id),t.object(a.global_vault_id),i[f],t.object(Ai)]})}),t}createCollectRewarderAndReturnCoinPayload(e,t){let{clmm_pool:i}=this.sdk.sdkOptions,n=ze(i),r=[e.coin_type_a,e.coin_type_b],a=[t.object(ze(i).global_config_id),t.object(e.pool_id),t.object(e.pos_id),t.object(n.global_vault_id),t.pure.bool(!0),t.object(Ai)];return t.moveCall({target:`${i.published_at}::pool::collect_reward`,typeArguments:[...r,e.rewarder_coin_type],arguments:a})}};var ye=re(ie(),1);import{Transaction as sr}from"@mysten/sui/transactions";import{normalizeSuiAddress as ar}from"@mysten/sui/utils";import{d as Fe,DETAILS_KEYS as _e,extractStructTagFromType as cr,TickMath as lr,U64_MAX as _a,ZERO as pa}from"@cetusprotocol/common-sdk";var Ul="amm_swap",Vl="Pool",xi=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=Fe(0);return e.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=Fe(r.input_amount).div(10**r.from_decimal).mul(a);if(t=t.add(l),n>1){let d=i.base_paths[1],f=r.direction?r.current_price:new E(1).div(r.current_price),p=d.direction?d.current_price:new E(1).div(d.current_price),w=d.label==="Cetus"?new E(d.fee_rate).div(10**6):new E(d.fee_rate).div(10**9),M=Fe(d.output_amount).div(10**d.to_decimal).mul(w).div(f.mul(p));t=t.add(M)}}}),t.toString()}calculateSwapPriceImpact(e){let t=Fe(0);return e.forEach(i=>{let n=i.base_paths.length;if(n===1){let r=i.base_paths[0],a=Fe(r.output_amount).div(10**r.to_decimal),l=Fe(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);t=t.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),p=new E(a.output_amount).div(10**a.to_decimal),w=new E(r.input_amount).div(10**r.from_decimal),P=p.div(w);t=t.add(this.calculateSingleImpact(P,f))}}),t.toString()}async preSwapWithMultiPool(e){let{integrate:t}=this.sdk.sdkOptions,i=new sr,n=[e.coin_type_a,e.coin_type_b];for(let f=0;f<e.pool_ids.length;f+=1){let p=[i.object(e.pool_ids[f]),i.pure.bool(e.a2b),i.pure.bool(e.by_amount_in),i.pure.u64(e.amount)];i.moveCall({target:`${t.published_at}::${zt}::calculate_swap_result`,arguments:p,typeArguments:n})}let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:ar("0x0")});r.error!=null&&N("InvalidConfig",`pre swap with multi pools error code: ${r.error??"unknown error"}, please check config and params`,{[_e.METHOD_NAME]:"preSwapWithMultiPool",[_e.REQUEST_PARAMS]:{params:e}});let a=r.events?.filter(f=>cr(f.type).name==="CalculatedSwapResultEvent");if(a.length===0)return null;a.length!==e.pool_ids.length&&N("ParamsLengthNotEqual","valueData.length !== params.pools.length",{[_e.METHOD_NAME]:"preSwapWithMultiPool",[_e.REQUEST_PARAMS]:{params:e}});let l=e.by_amount_in?pa:_a,d=0;for(let f=0;f<a.length;f+=1)if(!a[f].parsedJson.data.is_exceed)if(e.by_amount_in){let p=new ye.default(a[f].parsedJson.data.amount_out);p.gt(l)&&(d=f,l=p)}else{let p=new ye.default(a[f].parsedJson.data.amount_out);p.lt(l)&&(d=f,l=p)}return this.transformSwapWithMultiPoolData({pool_address:e.pool_ids[d],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},a[d].parsedJson)}async preSwap(e){let{integrate:t}=this.sdk.sdkOptions,i=new sr,n=[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}::${zt}::calculate_swap_result`,arguments:r,typeArguments:n});let a=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:ar("0x0")});if(a.error!=null)return N("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[_e.METHOD_NAME]:"preSwap",[_e.REQUEST_PARAMS]:{params:e}});let l=a.events?.filter(d=>cr(d.type).name==="CalculatedSwapResultEvent");return l.length===0?N("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[_e.METHOD_NAME]:"preSwap",[_e.REQUEST_PARAMS]:{params:e}}):this.transformSwapData(e,l[0].parsedJson.data)}transformSwapData(e,t){let i=t.amount_in&&t.fee_amount?new ye.default(t.amount_in).add(new ye.default(t.fee_amount)).toString():"";return{pool_address:e.pool.id,current_sqrt_price:e.current_sqrt_price,estimated_amount_in:i,estimated_amount_out:t.amount_out,estimated_end_sqrt_price:t.after_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}}transformSwapWithMultiPoolData(e,t){let{data:i}=t;console.log("json data. ",i);let n=i.amount_in&&i.fee_amount?new ye.default(i.amount_in).add(new ye.default(i.fee_amount)).toString():"";return{pool_address:e.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:e.amount,a2b:e.a2b,by_amount_in:e.by_amount_in}}calculateRates(e){let{current_pool:t}=e,i=Mo(t),n;e.a2b?n=e.swap_ticks.sort((P,M)=>M.index-P.index):n=e.swap_ticks.sort((P,M)=>P.index-M.index);let r=ko(e.a2b,e.by_amount_in,e.amount,i,n),a=!1;e.by_amount_in?a=r.amount_in.lt(e.amount):a=r.amount_out.lt(e.amount);let l=ae.getDefaultSqrtPriceLimit(e.a2b);e.a2b&&r.next_sqrt_price.lt(l)&&(a=!0),!e.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=lr.sqrtPriceX64ToPrice(i.current_sqrt_price,e.decimals_a,e.decimals_b).toNumber(),p=lr.sqrtPriceX64ToPrice(r.next_sqrt_price,e.decimals_a,e.decimals_b).toNumber(),w=Math.abs(f-p)/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:e.amount,a2b:e.a2b,by_amount_in:e.by_amount_in,price_impact_pct:w}}async createSwapPayload(e){return ae.buildSwapTransaction(this.sdk,e)}async createSwapWithoutTransferCoinsPayload(e){return ae.buildSwapTransactionWithoutTransferCoins(this.sdk,e)}};import{CLOCK_ADDRESS as ha,CoinAssist as ma,DETAILS_KEYS as we,getPackagerConfigs as Pn}from"@cetusprotocol/common-sdk";import{Transaction as ur}from"@mysten/sui/transactions";import{normalizeSuiAddress as ga}from"@mysten/sui/utils";var Ii=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 N("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.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,{showContent:!0,showType:!0})).forEach(l=>{let d=Gt.parseClmmVestInfo(l),f=`${d.id}-ClmmVestInfo`;this._sdk.updateCache(f,d),t.push(d)}),t}catch(i){Bt("ClmmVestFetchError",i,{[we.METHOD_NAME]:"getClmmVestInfoList"})}return[]}async getClmmVestInfo(e=!0){let{clmm_vest:t}=this._sdk.sdkOptions;if(t===void 0)return N("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"getClmmVestInfo"});let{clmm_vest_id:i}=Pn(t),n=`${i}-ClmmVestInfo`,r=this._sdk.getCache(n,e);if(r)return r;try{let a=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0,showType:!0}}),l=Gt.parseClmmVestInfo(a);return this._sdk.updateCache(n,l),l}catch(a){return Bt("ClmmVestFetchError",a,{[we.METHOD_NAME]:"getClmmVestInfo",[we.REQUEST_PARAMS]:{clmm_vest_id:i}})}}async getPositionVesting(e){let{clmm_vest:t}=this._sdk.sdkOptions;if(t===void 0)return N("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"getPositionVesting"});if(e.length===0)return[];let{clmm_vest_id:i}=Pn(t),n=new ur;e.forEach(l=>{let{clmm_pool_id:d,coin_type_a:f,coin_type_b:p,clmm_position_ids:w}=l;n.moveCall({package:t.published_at,module:"clmm_vester",function:"get_positions_vesting",typeArguments:[f,p],arguments:[n.object(i),n.object(d),n.pure.vector("id",w)]})});let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:ga("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=>Gt.parsePositionVesting(f)))}}),a}buildRedeemPayload(e,t){let{clmm_vest:i}=this._sdk.sdkOptions;if(i===void 0)return N("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"buildRedeemVestPayload"});let{versioned_id:n,clmm_vest_id:r,cetus_coin_type:a}=Pn(i);return t=t||new ur,e.forEach(l=>{let{clmm_pool_id:d,clmm_position_id:f,coin_type_a:p,coin_type_b:w,period:P}=l,M=t.moveCall({package:i.published_at,module:"clmm_vester",function:"redeem",arguments:[t.object(n),t.object(r),t.object(d),typeof f=="string"?t.object(f):f,t.pure.u16(P),t.object(ha)],typeArguments:[p,w]}),A=ma.fromBalance(M,a,t);t.transferObjects([A],this._sdk.getSenderAddress())}),t}};var Oi=class o extends ba{constructor(e){super(e),this._swap=new xi(this),this._pool=new hi(this),this._position=new Ti(this),this._config=new ii(this),this._rewarder=new Si(this),this._vest=new Ii(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"?o.createCustomSDK({...No,...e}):o.createCustomSDK({...Do,...e})}static createCustomSDK(e){return new o(e)}};var Ge=re(ie(),1);import{MathUtil as qe,TickMath as dr}from"@cetusprotocol/common-sdk";var ya=new Ge.default(365),wa=new Ge.default(24),va=new Ge.default(3600),Pa=new Ge.default(.5);function gu(o,e,t,i){return ya.mul(wa).mul(va).mul(Pa).mul(o.mul(e).add(t).div(i))}function Ca(o,e,t,i,n,r){let a=new E(o.toString()).div(new E(10**t)),l=new E(e.toString()).div(new E(10**i));return a.mul(n).add(l.mul(r))}function bu(o,e,t,i,n,r,a,l,d,f,p,w,P,M,A,S,$,F,z,D,q,Ft,H,qt){let te=new E(w),Wt=new E(P),ee=new E(S),L=new E($),Nt=new E(F),y=new E(z),s=new E(D),u=new E(q),_=new E(Ft),h=new E(H),g=new E(qt),v=dr.tickIndexToSqrtPriceX64(e),C=dr.tickIndexToSqrtPriceX64(t),m=qe.toX64Decimal(qe.fromX64(v)).round(),c=qe.toX64Decimal(qe.fromX64(C)).round(),b=qe.toX64Decimal(qe.fromX64(i)).round(),T,x=te.mul(new E(10**r)).mul(c.mul(m)).div(c.sub(m)).round(),I=Wt.mul(new E(10**a)).div(c.sub(m)).round();o<e?T=x:o>t?T=I:T=E.min(x,I);let B=T.mul(b.sub(m)),V=T.mul(c.sub(b)).div(b.mul(c)).div(new E(10**r)).mul(s).add(B.div(new E(10**a).mul(u))),Ht=Ca(M,A,r,a,s,u),K=V.div(Ht),Z=T.eq(new E(0))?new E(0):new E(p/1e4).mul(ee).mul(new E(T.toString()).div(new E(n.toString()).add(new E(T.toString())))).div(V),ve=K.eq(new E(0))?new E(0):K.mul(new E(36500/7)).div(V),G=L.div(new E(10**l)).mul(_).mul(ve),Q=Nt.div(new E(10**d)).mul(h).mul(ve),Qe=y.div(new E(10**f)).mul(g).mul(ve);return{fee_apr:Z,pos_rewarder_0_apr:G,pos_rewarder_1_apr:Q,pos_rewarder_2_apr:Qe}}function yu(o,e,t,i){let n=Math.max(o,t),a=Math.min(e,i)-n,l=e-o,d=i-t,f=new E(l.toString()),p=new E(d.toString()),w=new E(a.toString()),P=new E("0");return a<0?P=new E("0"):l===a?P=p.div(w):d===a?P=w.div(f):P=w.mul(w).div(p).div(f),P}var ku=Oi;export{Ul as AMM_SWAP_MODULE,es as BPS,Oi as CetusClmmSDK,Me as ClmmError,Ba as ClmmExpectSwapModule,zt as ClmmFetcherModule,ni as ClmmIntegratePoolModule,Zt as ClmmIntegratePoolV2Module,oi as ClmmIntegratePoolV3Module,pe as ClmmIntegrateRouterModule,ho as ClmmIntegrateRouterWithPartnerModule,mo as ClmmIntegrateUtilsModule,sn as ClmmPartnerModule,_o as ClmmPositionStatus,ke as ConfigErrorCode,ii as ConfigModule,Fa as DeepbookClobV2Module,Da as DeepbookCustodianV2Module,qa as DeepbookEndpointsV2Module,Vl as POOL_STRUCT,ro as PartnerErrorCode,$e as PoolErrorCode,hi as PoolModule,ti as PositionErrorCode,Ti as PositionModule,Vt as PositionUtils,Si as RewarderModule,Or as RouterErrorCode,no as SwapErrorCode,xi as SwapModule,ae as SwapUtils,ao as TypesErrorCode,so as UtilsErrorCode,oo as VestErrorCode,Ii as VestModule,Gt as VestUtils,kc as buildClmmPositionName,ci as buildPool,xo as buildPoolTransactionInfo,li as buildPosition,Ke as buildPositionInfo,So as buildPositionTransactionInfo,ui as buildTickData,Ao as buildTickDataByEvent,Io as buildTransferCoin,di as buildTransferCoinToSender,No as clmmMainnet,Do as clmmTestnet,ko as computeSwap,Hr as computeSwapStep,ku as default,gu as estPoolAPR,bu as estPositionAPRWithDeltaMethod,yu as estPositionAPRWithMultiMethod,Bo as eventMainnetContractMaps,Fo as eventTestnetContractMaps,Qa as findAdjustCoin,La as getDefaultSuiInputType,sc as getLowerSqrtPriceFromCoinA,cc as getLowerSqrtPriceFromCoinB,ac as getUpperSqrtPriceFromCoinA,lc as getUpperSqrtPriceFromCoinB,Bt as handleError,N as handleMessageError,pc as newBits,po as poolFilterEvenTypes,fo as poolLiquiditySnapshotType,Mo as 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:
|