@cetusprotocol/sui-clmm-sdk 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/docs/create_clmm_pool.md +2 -2
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var mr=Object.create;var $n=Object.defineProperty;var gr=Object.getOwnPropertyDescriptor;var br=Object.getOwnPropertyNames;var wr=Object.getPrototypeOf,yr=Object.prototype.hasOwnProperty;var vr=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var Pr=(o,t)=>()=>(t||o((t={exports:{}}).exports,t),t.exports);var Cr=(o,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of br(t))!yr.call(o,n)&&n!==e&&$n(o,n,{get:()=>t[n],enumerable:!(i=gr(t,n))||i.enumerable});return o};var re=(o,t,e)=>(e=o!=null?mr(wr(o)):{},Cr(t||!o||!o.__esModule?$n(e,"default",{value:o,enumerable:!0}):e,o));var Gt=Pr((_o,on)=>{"use strict";(function(o,t){"use strict";function e(y,s){if(!y)throw new Error(s||"Assertion failed")}function i(y,s){y.super_=s;var u=function(){};u.prototype=s.prototype,y.prototype=new u,y.prototype.constructor=y}function n(y,s,u){if(n.isBN(y))return y;this.negative=0,this.words=null,this.length=0,this.red=null,y!==null&&((s==="le"||s==="be")&&(u=s,s=10),this._init(y||0,s||10,u||"be"))}typeof o=="object"?o.exports=n:t.BN=n,n.BN=n,n.wordSize=26;var r;try{typeof window<"u"&&typeof window.Buffer<"u"?r=window.Buffer:r=vr("buffer").Buffer}catch{}n.isBN=function(s){return s instanceof n?!0:s!==null&&typeof s=="object"&&s.constructor.wordSize===n.wordSize&&Array.isArray(s.words)},n.max=function(s,u){return s.cmp(u)>0?s:u},n.min=function(s,u){return s.cmp(u)<0?s:u},n.prototype._init=function(s,u,_){if(typeof s=="number")return this._initNumber(s,u,_);if(typeof s=="object")return this._initArray(s,u,_);u==="hex"&&(u=16),e(u===(u|0)&&u>=2&&u<=36),s=s.toString().replace(/\s+/g,"");var p=0;s[0]==="-"&&(p++,this.negative=1),p<s.length&&(u===16?this._parseHex(s,p,_):(this._parseBase(s,u,p),_==="le"&&this._initArray(this.toArray(),u,_)))},n.prototype._initNumber=function(s,u,_){s<0&&(this.negative=1,s=-s),s<67108864?(this.words=[s&67108863],this.length=1):s<4503599627370496?(this.words=[s&67108863,s/67108864&67108863],this.length=2):(e(s<9007199254740992),this.words=[s&67108863,s/67108864&67108863,1],this.length=3),_==="le"&&this._initArray(this.toArray(),u,_)},n.prototype._initArray=function(s,u,_){if(e(typeof s.length=="number"),s.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(s.length/3),this.words=new Array(this.length);for(var p=0;p<this.length;p++)this.words[p]=0;var g,v,C=0;if(_==="be")for(p=s.length-1,g=0;p>=0;p-=3)v=s[p]|s[p-1]<<8|s[p-2]<<16,this.words[g]|=v<<C&67108863,this.words[g+1]=v>>>26-C&67108863,C+=24,C>=26&&(C-=26,g++);else if(_==="le")for(p=0,g=0;p<s.length;p+=3)v=s[p]|s[p+1]<<8|s[p+2]<<16,this.words[g]|=v<<C&67108863,this.words[g+1]=v>>>26-C&67108863,C+=24,C>=26&&(C-=26,g++);return this._strip()};function a(y,s){var u=y.charCodeAt(s);if(u>=48&&u<=57)return u-48;if(u>=65&&u<=70)return u-55;if(u>=97&&u<=102)return u-87;e(!1,"Invalid character in "+y)}function l(y,s,u){var _=a(y,u);return u-1>=s&&(_|=a(y,u-1)<<4),_}n.prototype._parseHex=function(s,u,_){this.length=Math.ceil((s.length-u)/6),this.words=new Array(this.length);for(var p=0;p<this.length;p++)this.words[p]=0;var g=0,v=0,C;if(_==="be")for(p=s.length-1;p>=u;p-=2)C=l(s,u,p)<<g,this.words[v]|=C&67108863,g>=18?(g-=18,v+=1,this.words[v]|=C>>>26):g+=8;else{var m=s.length-u;for(p=m%2===0?u+1:u;p<s.length;p+=2)C=l(s,u,p)<<g,this.words[v]|=C&67108863,g>=18?(g-=18,v+=1,this.words[v]|=C>>>26):g+=8}this._strip()};function d(y,s,u,_){for(var p=0,g=0,v=Math.min(y.length,u),C=s;C<v;C++){var m=y.charCodeAt(C)-48;p*=_,m>=49?g=m-49+10:m>=17?g=m-17+10:g=m,e(m>=0&&g<_,"Invalid character"),p+=g}return p}n.prototype._parseBase=function(s,u,_){this.words=[0],this.length=1;for(var p=0,g=1;g<=67108863;g*=u)p++;p--,g=g/u|0;for(var v=s.length-_,C=v%p,m=Math.min(v,v-C)+_,c=0,b=_;b<m;b+=p)c=d(s,b,b+p,u),this.imuln(g),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c);if(C!==0){var A=1;for(c=d(s,b,s.length,u),b=0;b<C;b++)A*=u;this.imuln(A),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c)}this._strip()},n.prototype.copy=function(s){s.words=new Array(this.length);for(var u=0;u<this.length;u++)s.words[u]=this.words[u];s.length=this.length,s.negative=this.negative,s.red=this.red};function f(y,s){y.words=s.words,y.length=s.length,y.negative=s.negative,y.red=s.red}if(n.prototype._move=function(s){f(s,this)},n.prototype.clone=function(){var s=new n(null);return this.copy(s),s},n.prototype._expand=function(s){for(;this.length<s;)this.words[this.length++]=0;return this},n.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},n.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{n.prototype[Symbol.for("nodejs.util.inspect.custom")]=h}catch{n.prototype.inspect=h}else n.prototype.inspect=h;function h(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var w=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],P=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],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 p=0,g=0,v=0;v<this.length;v++){var C=this.words[v],m=((C<<p|g)&16777215).toString(16);g=C>>>24-p&16777215,p+=2,p>=26&&(p-=26,v--),g!==0||v!==this.length-1?_=w[6-m.length]+m+_:_=m+_}for(g!==0&&(_=g.toString(16)+_);_.length%u!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}if(s===(s|0)&&s>=2&&s<=36){var c=P[s],b=M[s];_="";var A=this.clone();for(A.negative=0;!A.isZero();){var x=A.modrn(b).toString(s);A=A.idivn(b),A.isZero()?_=x+_:_=w[c-x.length]+x+_}for(this.isZero()&&(_="0"+_);_.length%u!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}e(!1,"Base should be between 2 and 36")},n.prototype.toNumber=function(){var s=this.words[0];return this.length===2?s+=this.words[1]*67108864:this.length===3&&this.words[2]===1?s+=4503599627370496+this.words[1]*67108864:this.length>2&&e(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-s:s},n.prototype.toJSON=function(){return this.toString(16,2)},r&&(n.prototype.toBuffer=function(s,u){return this.toArrayLike(r,s,u)}),n.prototype.toArray=function(s,u){return this.toArrayLike(Array,s,u)};var T=function(s,u){return s.allocUnsafe?s.allocUnsafe(u):new s(u)};n.prototype.toArrayLike=function(s,u,_){this._strip();var p=this.byteLength(),g=_||Math.max(1,p);e(p<=g,"byte array longer than desired length"),e(g>0,"Requested array length <= 0");var v=T(s,g),C=u==="le"?"LE":"BE";return this["_toArrayLike"+C](v,p),v},n.prototype._toArrayLikeLE=function(s,u){for(var _=0,p=0,g=0,v=0;g<this.length;g++){var C=this.words[g]<<v|p;s[_++]=C&255,_<s.length&&(s[_++]=C>>8&255),_<s.length&&(s[_++]=C>>16&255),v===6?(_<s.length&&(s[_++]=C>>24&255),p=0,v=0):(p=C>>>24,v+=2)}if(_<s.length)for(s[_++]=p;_<s.length;)s[_++]=0},n.prototype._toArrayLikeBE=function(s,u){for(var _=s.length-1,p=0,g=0,v=0;g<this.length;g++){var C=this.words[g]<<v|p;s[_--]=C&255,_>=0&&(s[_--]=C>>8&255),_>=0&&(s[_--]=C>>16&255),v===6?(_>=0&&(s[_--]=C>>24&255),p=0,v=0):(p=C>>>24,v+=2)}if(_>=0)for(s[_--]=p;_>=0;)s[_--]=0},Math.clz32?n.prototype._countBits=function(s){return 32-Math.clz32(s)}:n.prototype._countBits=function(s){var u=s,_=0;return u>=4096&&(_+=13,u>>>=13),u>=64&&(_+=7,u>>>=7),u>=8&&(_+=4,u>>>=4),u>=2&&(_+=2,u>>>=2),_+u},n.prototype._zeroBits=function(s){if(s===0)return 26;var u=s,_=0;return(u&8191)===0&&(_+=13,u>>>=13),(u&127)===0&&(_+=7,u>>>=7),(u&15)===0&&(_+=4,u>>>=4),(u&3)===0&&(_+=2,u>>>=2),(u&1)===0&&_++,_},n.prototype.bitLength=function(){var s=this.words[this.length-1],u=this._countBits(s);return(this.length-1)*26+u};function S(y){for(var s=new Array(y.bitLength()),u=0;u<s.length;u++){var _=u/26|0,p=u%26;s[u]=y.words[_]>>>p&1}return s}n.prototype.zeroBits=function(){if(this.isZero())return 0;for(var s=0,u=0;u<this.length;u++){var _=this._zeroBits(this.words[u]);if(s+=_,_!==26)break}return s},n.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},n.prototype.toTwos=function(s){return this.negative!==0?this.abs().inotn(s).iaddn(1):this.clone()},n.prototype.fromTwos=function(s){return this.testn(s-1)?this.notn(s).iaddn(1).ineg():this.clone()},n.prototype.isNeg=function(){return this.negative!==0},n.prototype.neg=function(){return this.clone().ineg()},n.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},n.prototype.iuor=function(s){for(;this.length<s.length;)this.words[this.length++]=0;for(var u=0;u<s.length;u++)this.words[u]=this.words[u]|s.words[u];return this._strip()},n.prototype.ior=function(s){return e((this.negative|s.negative)===0),this.iuor(s)},n.prototype.or=function(s){return this.length>s.length?this.clone().ior(s):s.clone().ior(this)},n.prototype.uor=function(s){return this.length>s.length?this.clone().iuor(s):s.clone().iuor(this)},n.prototype.iuand=function(s){var u;this.length>s.length?u=s:u=this;for(var _=0;_<u.length;_++)this.words[_]=this.words[_]&s.words[_];return this.length=u.length,this._strip()},n.prototype.iand=function(s){return e((this.negative|s.negative)===0),this.iuand(s)},n.prototype.and=function(s){return this.length>s.length?this.clone().iand(s):s.clone().iand(this)},n.prototype.uand=function(s){return this.length>s.length?this.clone().iuand(s):s.clone().iuand(this)},n.prototype.iuxor=function(s){var u,_;this.length>s.length?(u=this,_=s):(u=s,_=this);for(var p=0;p<_.length;p++)this.words[p]=u.words[p]^_.words[p];if(this!==u)for(;p<u.length;p++)this.words[p]=u.words[p];return this.length=u.length,this._strip()},n.prototype.ixor=function(s){return e((this.negative|s.negative)===0),this.iuxor(s)},n.prototype.xor=function(s){return this.length>s.length?this.clone().ixor(s):s.clone().ixor(this)},n.prototype.uxor=function(s){return this.length>s.length?this.clone().iuxor(s):s.clone().iuxor(this)},n.prototype.inotn=function(s){e(typeof s=="number"&&s>=0);var u=Math.ceil(s/26)|0,_=s%26;this._expand(u),_>0&&u--;for(var p=0;p<u;p++)this.words[p]=~this.words[p]&67108863;return _>0&&(this.words[p]=~this.words[p]&67108863>>26-_),this._strip()},n.prototype.notn=function(s){return this.clone().inotn(s)},n.prototype.setn=function(s,u){e(typeof s=="number"&&s>=0);var _=s/26|0,p=s%26;return this._expand(_+1),u?this.words[_]=this.words[_]|1<<p:this.words[_]=this.words[_]&~(1<<p),this._strip()},n.prototype.iadd=function(s){var u;if(this.negative!==0&&s.negative===0)return this.negative=0,u=this.isub(s),this.negative^=1,this._normSign();if(this.negative===0&&s.negative!==0)return s.negative=0,u=this.isub(s),s.negative=1,u._normSign();var _,p;this.length>s.length?(_=this,p=s):(_=s,p=this);for(var g=0,v=0;v<p.length;v++)u=(_.words[v]|0)+(p.words[v]|0)+g,this.words[v]=u&67108863,g=u>>>26;for(;g!==0&&v<_.length;v++)u=(_.words[v]|0)+g,this.words[v]=u&67108863,g=u>>>26;if(this.length=_.length,g!==0)this.words[this.length]=g,this.length++;else if(_!==this)for(;v<_.length;v++)this.words[v]=_.words[v];return this},n.prototype.add=function(s){var u;return s.negative!==0&&this.negative===0?(s.negative=0,u=this.sub(s),s.negative^=1,u):s.negative===0&&this.negative!==0?(this.negative=0,u=s.sub(this),this.negative=1,u):this.length>s.length?this.clone().iadd(s):s.clone().iadd(this)},n.prototype.isub=function(s){if(s.negative!==0){s.negative=0;var u=this.iadd(s);return s.negative=1,u._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(s),this.negative=1,this._normSign();var _=this.cmp(s);if(_===0)return this.negative=0,this.length=1,this.words[0]=0,this;var p,g;_>0?(p=this,g=s):(p=s,g=this);for(var v=0,C=0;C<g.length;C++)u=(p.words[C]|0)-(g.words[C]|0)+v,v=u>>26,this.words[C]=u&67108863;for(;v!==0&&C<p.length;C++)u=(p.words[C]|0)+v,v=u>>26,this.words[C]=u&67108863;if(v===0&&C<p.length&&p!==this)for(;C<p.length;C++)this.words[C]=p.words[C];return this.length=Math.max(this.length,C),p!==this&&(this.negative=1),this._strip()},n.prototype.sub=function(s){return this.clone().isub(s)};function L(y,s,u){u.negative=s.negative^y.negative;var _=y.length+s.length|0;u.length=_,_=_-1|0;var p=y.words[0]|0,g=s.words[0]|0,v=p*g,C=v&67108863,m=v/67108864|0;u.words[0]=C;for(var c=1;c<_;c++){for(var b=m>>>26,A=m&67108863,x=Math.min(c,s.length-1),E=Math.max(0,c-y.length+1);E<=x;E++){var q=c-E|0;p=y.words[q]|0,g=s.words[E]|0,v=p*g+A,b+=v/67108864|0,A=v&67108863}u.words[c]=A|0,m=b|0}return m!==0?u.words[c]=m|0:u.length--,u._strip()}var j=function(s,u,_){var p=s.words,g=u.words,v=_.words,C=0,m,c,b,A=p[0]|0,x=A&8191,E=A>>>13,q=p[1]|0,U=q&8191,K=q>>>13,zt=p[2]|0,H=zt&8191,G=zt>>>13,ye=p[3]|0,Q=ye&8191,J=ye>>>13,Ge=p[4]|0,X=Ge&8191,Y=Ge>>>13,Tn=p[5]|0,tt=Tn&8191,et=Tn>>>13,An=p[6]|0,it=An&8191,nt=An>>>13,Sn=p[7]|0,ot=Sn&8191,rt=Sn>>>13,xn=p[8]|0,st=xn&8191,at=xn>>>13,In=p[9]|0,ct=In&8191,lt=In>>>13,En=g[0]|0,ut=En&8191,dt=En>>>13,On=g[1]|0,ft=On&8191,_t=On>>>13,Rn=g[2]|0,ht=Rn&8191,pt=Rn>>>13,Fn=g[3]|0,mt=Fn&8191,gt=Fn>>>13,Nn=g[4]|0,bt=Nn&8191,wt=Nn>>>13,Bn=g[5]|0,yt=Bn&8191,vt=Bn>>>13,jn=g[6]|0,Pt=jn&8191,Ct=jn>>>13,qn=g[7]|0,Mt=qn&8191,kt=qn>>>13,Dn=g[8]|0,Tt=Dn&8191,At=Dn>>>13,Ln=g[9]|0,St=Ln&8191,xt=Ln>>>13;_.negative=s.negative^u.negative,_.length=19,m=Math.imul(x,ut),c=Math.imul(x,dt),c=c+Math.imul(E,ut)|0,b=Math.imul(E,dt);var Ii=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ii>>>26)|0,Ii&=67108863,m=Math.imul(U,ut),c=Math.imul(U,dt),c=c+Math.imul(K,ut)|0,b=Math.imul(K,dt),m=m+Math.imul(x,ft)|0,c=c+Math.imul(x,_t)|0,c=c+Math.imul(E,ft)|0,b=b+Math.imul(E,_t)|0;var Ei=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ei>>>26)|0,Ei&=67108863,m=Math.imul(H,ut),c=Math.imul(H,dt),c=c+Math.imul(G,ut)|0,b=Math.imul(G,dt),m=m+Math.imul(U,ft)|0,c=c+Math.imul(U,_t)|0,c=c+Math.imul(K,ft)|0,b=b+Math.imul(K,_t)|0,m=m+Math.imul(x,ht)|0,c=c+Math.imul(x,pt)|0,c=c+Math.imul(E,ht)|0,b=b+Math.imul(E,pt)|0;var Oi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Oi>>>26)|0,Oi&=67108863,m=Math.imul(Q,ut),c=Math.imul(Q,dt),c=c+Math.imul(J,ut)|0,b=Math.imul(J,dt),m=m+Math.imul(H,ft)|0,c=c+Math.imul(H,_t)|0,c=c+Math.imul(G,ft)|0,b=b+Math.imul(G,_t)|0,m=m+Math.imul(U,ht)|0,c=c+Math.imul(U,pt)|0,c=c+Math.imul(K,ht)|0,b=b+Math.imul(K,pt)|0,m=m+Math.imul(x,mt)|0,c=c+Math.imul(x,gt)|0,c=c+Math.imul(E,mt)|0,b=b+Math.imul(E,gt)|0;var Ri=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ri>>>26)|0,Ri&=67108863,m=Math.imul(X,ut),c=Math.imul(X,dt),c=c+Math.imul(Y,ut)|0,b=Math.imul(Y,dt),m=m+Math.imul(Q,ft)|0,c=c+Math.imul(Q,_t)|0,c=c+Math.imul(J,ft)|0,b=b+Math.imul(J,_t)|0,m=m+Math.imul(H,ht)|0,c=c+Math.imul(H,pt)|0,c=c+Math.imul(G,ht)|0,b=b+Math.imul(G,pt)|0,m=m+Math.imul(U,mt)|0,c=c+Math.imul(U,gt)|0,c=c+Math.imul(K,mt)|0,b=b+Math.imul(K,gt)|0,m=m+Math.imul(x,bt)|0,c=c+Math.imul(x,wt)|0,c=c+Math.imul(E,bt)|0,b=b+Math.imul(E,wt)|0;var Fi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Fi>>>26)|0,Fi&=67108863,m=Math.imul(tt,ut),c=Math.imul(tt,dt),c=c+Math.imul(et,ut)|0,b=Math.imul(et,dt),m=m+Math.imul(X,ft)|0,c=c+Math.imul(X,_t)|0,c=c+Math.imul(Y,ft)|0,b=b+Math.imul(Y,_t)|0,m=m+Math.imul(Q,ht)|0,c=c+Math.imul(Q,pt)|0,c=c+Math.imul(J,ht)|0,b=b+Math.imul(J,pt)|0,m=m+Math.imul(H,mt)|0,c=c+Math.imul(H,gt)|0,c=c+Math.imul(G,mt)|0,b=b+Math.imul(G,gt)|0,m=m+Math.imul(U,bt)|0,c=c+Math.imul(U,wt)|0,c=c+Math.imul(K,bt)|0,b=b+Math.imul(K,wt)|0,m=m+Math.imul(x,yt)|0,c=c+Math.imul(x,vt)|0,c=c+Math.imul(E,yt)|0,b=b+Math.imul(E,vt)|0;var Ni=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ni>>>26)|0,Ni&=67108863,m=Math.imul(it,ut),c=Math.imul(it,dt),c=c+Math.imul(nt,ut)|0,b=Math.imul(nt,dt),m=m+Math.imul(tt,ft)|0,c=c+Math.imul(tt,_t)|0,c=c+Math.imul(et,ft)|0,b=b+Math.imul(et,_t)|0,m=m+Math.imul(X,ht)|0,c=c+Math.imul(X,pt)|0,c=c+Math.imul(Y,ht)|0,b=b+Math.imul(Y,pt)|0,m=m+Math.imul(Q,mt)|0,c=c+Math.imul(Q,gt)|0,c=c+Math.imul(J,mt)|0,b=b+Math.imul(J,gt)|0,m=m+Math.imul(H,bt)|0,c=c+Math.imul(H,wt)|0,c=c+Math.imul(G,bt)|0,b=b+Math.imul(G,wt)|0,m=m+Math.imul(U,yt)|0,c=c+Math.imul(U,vt)|0,c=c+Math.imul(K,yt)|0,b=b+Math.imul(K,vt)|0,m=m+Math.imul(x,Pt)|0,c=c+Math.imul(x,Ct)|0,c=c+Math.imul(E,Pt)|0,b=b+Math.imul(E,Ct)|0;var Bi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Bi>>>26)|0,Bi&=67108863,m=Math.imul(ot,ut),c=Math.imul(ot,dt),c=c+Math.imul(rt,ut)|0,b=Math.imul(rt,dt),m=m+Math.imul(it,ft)|0,c=c+Math.imul(it,_t)|0,c=c+Math.imul(nt,ft)|0,b=b+Math.imul(nt,_t)|0,m=m+Math.imul(tt,ht)|0,c=c+Math.imul(tt,pt)|0,c=c+Math.imul(et,ht)|0,b=b+Math.imul(et,pt)|0,m=m+Math.imul(X,mt)|0,c=c+Math.imul(X,gt)|0,c=c+Math.imul(Y,mt)|0,b=b+Math.imul(Y,gt)|0,m=m+Math.imul(Q,bt)|0,c=c+Math.imul(Q,wt)|0,c=c+Math.imul(J,bt)|0,b=b+Math.imul(J,wt)|0,m=m+Math.imul(H,yt)|0,c=c+Math.imul(H,vt)|0,c=c+Math.imul(G,yt)|0,b=b+Math.imul(G,vt)|0,m=m+Math.imul(U,Pt)|0,c=c+Math.imul(U,Ct)|0,c=c+Math.imul(K,Pt)|0,b=b+Math.imul(K,Ct)|0,m=m+Math.imul(x,Mt)|0,c=c+Math.imul(x,kt)|0,c=c+Math.imul(E,Mt)|0,b=b+Math.imul(E,kt)|0;var ji=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(ji>>>26)|0,ji&=67108863,m=Math.imul(st,ut),c=Math.imul(st,dt),c=c+Math.imul(at,ut)|0,b=Math.imul(at,dt),m=m+Math.imul(ot,ft)|0,c=c+Math.imul(ot,_t)|0,c=c+Math.imul(rt,ft)|0,b=b+Math.imul(rt,_t)|0,m=m+Math.imul(it,ht)|0,c=c+Math.imul(it,pt)|0,c=c+Math.imul(nt,ht)|0,b=b+Math.imul(nt,pt)|0,m=m+Math.imul(tt,mt)|0,c=c+Math.imul(tt,gt)|0,c=c+Math.imul(et,mt)|0,b=b+Math.imul(et,gt)|0,m=m+Math.imul(X,bt)|0,c=c+Math.imul(X,wt)|0,c=c+Math.imul(Y,bt)|0,b=b+Math.imul(Y,wt)|0,m=m+Math.imul(Q,yt)|0,c=c+Math.imul(Q,vt)|0,c=c+Math.imul(J,yt)|0,b=b+Math.imul(J,vt)|0,m=m+Math.imul(H,Pt)|0,c=c+Math.imul(H,Ct)|0,c=c+Math.imul(G,Pt)|0,b=b+Math.imul(G,Ct)|0,m=m+Math.imul(U,Mt)|0,c=c+Math.imul(U,kt)|0,c=c+Math.imul(K,Mt)|0,b=b+Math.imul(K,kt)|0,m=m+Math.imul(x,Tt)|0,c=c+Math.imul(x,At)|0,c=c+Math.imul(E,Tt)|0,b=b+Math.imul(E,At)|0;var qi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(qi>>>26)|0,qi&=67108863,m=Math.imul(ct,ut),c=Math.imul(ct,dt),c=c+Math.imul(lt,ut)|0,b=Math.imul(lt,dt),m=m+Math.imul(st,ft)|0,c=c+Math.imul(st,_t)|0,c=c+Math.imul(at,ft)|0,b=b+Math.imul(at,_t)|0,m=m+Math.imul(ot,ht)|0,c=c+Math.imul(ot,pt)|0,c=c+Math.imul(rt,ht)|0,b=b+Math.imul(rt,pt)|0,m=m+Math.imul(it,mt)|0,c=c+Math.imul(it,gt)|0,c=c+Math.imul(nt,mt)|0,b=b+Math.imul(nt,gt)|0,m=m+Math.imul(tt,bt)|0,c=c+Math.imul(tt,wt)|0,c=c+Math.imul(et,bt)|0,b=b+Math.imul(et,wt)|0,m=m+Math.imul(X,yt)|0,c=c+Math.imul(X,vt)|0,c=c+Math.imul(Y,yt)|0,b=b+Math.imul(Y,vt)|0,m=m+Math.imul(Q,Pt)|0,c=c+Math.imul(Q,Ct)|0,c=c+Math.imul(J,Pt)|0,b=b+Math.imul(J,Ct)|0,m=m+Math.imul(H,Mt)|0,c=c+Math.imul(H,kt)|0,c=c+Math.imul(G,Mt)|0,b=b+Math.imul(G,kt)|0,m=m+Math.imul(U,Tt)|0,c=c+Math.imul(U,At)|0,c=c+Math.imul(K,Tt)|0,b=b+Math.imul(K,At)|0,m=m+Math.imul(x,St)|0,c=c+Math.imul(x,xt)|0,c=c+Math.imul(E,St)|0,b=b+Math.imul(E,xt)|0;var Di=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Di>>>26)|0,Di&=67108863,m=Math.imul(ct,ft),c=Math.imul(ct,_t),c=c+Math.imul(lt,ft)|0,b=Math.imul(lt,_t),m=m+Math.imul(st,ht)|0,c=c+Math.imul(st,pt)|0,c=c+Math.imul(at,ht)|0,b=b+Math.imul(at,pt)|0,m=m+Math.imul(ot,mt)|0,c=c+Math.imul(ot,gt)|0,c=c+Math.imul(rt,mt)|0,b=b+Math.imul(rt,gt)|0,m=m+Math.imul(it,bt)|0,c=c+Math.imul(it,wt)|0,c=c+Math.imul(nt,bt)|0,b=b+Math.imul(nt,wt)|0,m=m+Math.imul(tt,yt)|0,c=c+Math.imul(tt,vt)|0,c=c+Math.imul(et,yt)|0,b=b+Math.imul(et,vt)|0,m=m+Math.imul(X,Pt)|0,c=c+Math.imul(X,Ct)|0,c=c+Math.imul(Y,Pt)|0,b=b+Math.imul(Y,Ct)|0,m=m+Math.imul(Q,Mt)|0,c=c+Math.imul(Q,kt)|0,c=c+Math.imul(J,Mt)|0,b=b+Math.imul(J,kt)|0,m=m+Math.imul(H,Tt)|0,c=c+Math.imul(H,At)|0,c=c+Math.imul(G,Tt)|0,b=b+Math.imul(G,At)|0,m=m+Math.imul(U,St)|0,c=c+Math.imul(U,xt)|0,c=c+Math.imul(K,St)|0,b=b+Math.imul(K,xt)|0;var Li=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Li>>>26)|0,Li&=67108863,m=Math.imul(ct,ht),c=Math.imul(ct,pt),c=c+Math.imul(lt,ht)|0,b=Math.imul(lt,pt),m=m+Math.imul(st,mt)|0,c=c+Math.imul(st,gt)|0,c=c+Math.imul(at,mt)|0,b=b+Math.imul(at,gt)|0,m=m+Math.imul(ot,bt)|0,c=c+Math.imul(ot,wt)|0,c=c+Math.imul(rt,bt)|0,b=b+Math.imul(rt,wt)|0,m=m+Math.imul(it,yt)|0,c=c+Math.imul(it,vt)|0,c=c+Math.imul(nt,yt)|0,b=b+Math.imul(nt,vt)|0,m=m+Math.imul(tt,Pt)|0,c=c+Math.imul(tt,Ct)|0,c=c+Math.imul(et,Pt)|0,b=b+Math.imul(et,Ct)|0,m=m+Math.imul(X,Mt)|0,c=c+Math.imul(X,kt)|0,c=c+Math.imul(Y,Mt)|0,b=b+Math.imul(Y,kt)|0,m=m+Math.imul(Q,Tt)|0,c=c+Math.imul(Q,At)|0,c=c+Math.imul(J,Tt)|0,b=b+Math.imul(J,At)|0,m=m+Math.imul(H,St)|0,c=c+Math.imul(H,xt)|0,c=c+Math.imul(G,St)|0,b=b+Math.imul(G,xt)|0;var $i=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+($i>>>26)|0,$i&=67108863,m=Math.imul(ct,mt),c=Math.imul(ct,gt),c=c+Math.imul(lt,mt)|0,b=Math.imul(lt,gt),m=m+Math.imul(st,bt)|0,c=c+Math.imul(st,wt)|0,c=c+Math.imul(at,bt)|0,b=b+Math.imul(at,wt)|0,m=m+Math.imul(ot,yt)|0,c=c+Math.imul(ot,vt)|0,c=c+Math.imul(rt,yt)|0,b=b+Math.imul(rt,vt)|0,m=m+Math.imul(it,Pt)|0,c=c+Math.imul(it,Ct)|0,c=c+Math.imul(nt,Pt)|0,b=b+Math.imul(nt,Ct)|0,m=m+Math.imul(tt,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(Q,St)|0,c=c+Math.imul(Q,xt)|0,c=c+Math.imul(J,St)|0,b=b+Math.imul(J,xt)|0;var Ui=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ui>>>26)|0,Ui&=67108863,m=Math.imul(ct,bt),c=Math.imul(ct,wt),c=c+Math.imul(lt,bt)|0,b=Math.imul(lt,wt),m=m+Math.imul(st,yt)|0,c=c+Math.imul(st,vt)|0,c=c+Math.imul(at,yt)|0,b=b+Math.imul(at,vt)|0,m=m+Math.imul(ot,Pt)|0,c=c+Math.imul(ot,Ct)|0,c=c+Math.imul(rt,Pt)|0,b=b+Math.imul(rt,Ct)|0,m=m+Math.imul(it,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 Vi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Vi>>>26)|0,Vi&=67108863,m=Math.imul(ct,yt),c=Math.imul(ct,vt),c=c+Math.imul(lt,yt)|0,b=Math.imul(lt,vt),m=m+Math.imul(st,Pt)|0,c=c+Math.imul(st,Ct)|0,c=c+Math.imul(at,Pt)|0,b=b+Math.imul(at,Ct)|0,m=m+Math.imul(ot,Mt)|0,c=c+Math.imul(ot,kt)|0,c=c+Math.imul(rt,Mt)|0,b=b+Math.imul(rt,kt)|0,m=m+Math.imul(it,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 Ki=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Ki>>>26)|0,Ki&=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 Hi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Hi>>>26)|0,Hi&=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 Wi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Wi>>>26)|0,Wi&=67108863,m=Math.imul(ct,Tt),c=Math.imul(ct,At),c=c+Math.imul(lt,Tt)|0,b=Math.imul(lt,At),m=m+Math.imul(st,St)|0,c=c+Math.imul(st,xt)|0,c=c+Math.imul(at,St)|0,b=b+Math.imul(at,xt)|0;var Zi=(C+m|0)+((c&8191)<<13)|0;C=(b+(c>>>13)|0)+(Zi>>>26)|0,Zi&=67108863,m=Math.imul(ct,St),c=Math.imul(ct,xt),c=c+Math.imul(lt,St)|0,b=Math.imul(lt,xt);var zi=(C+m|0)+((c&8191)<<13)|0;return C=(b+(c>>>13)|0)+(zi>>>26)|0,zi&=67108863,v[0]=Ii,v[1]=Ei,v[2]=Oi,v[3]=Ri,v[4]=Fi,v[5]=Ni,v[6]=Bi,v[7]=ji,v[8]=qi,v[9]=Di,v[10]=Li,v[11]=$i,v[12]=Ui,v[13]=Vi,v[14]=Ki,v[15]=Hi,v[16]=Wi,v[17]=Zi,v[18]=zi,C!==0&&(v[19]=C,_.length++),_};Math.imul||(j=L);function V(y,s,u){u.negative=s.negative^y.negative,u.length=y.length+s.length;for(var _=0,p=0,g=0;g<u.length-1;g++){var v=p;p=0;for(var C=_&67108863,m=Math.min(g,s.length-1),c=Math.max(0,g-y.length+1);c<=m;c++){var b=g-c,A=y.words[b]|0,x=s.words[c]|0,E=A*x,q=E&67108863;v=v+(E/67108864|0)|0,q=q+C|0,C=q&67108863,v=v+(q>>>26)|0,p+=v>>>26,v&=67108863}u.words[g]=C,_=v,v=p}return _!==0?u.words[g]=_:u.length--,u._strip()}function N(y,s,u){return V(y,s,u)}n.prototype.mulTo=function(s,u){var _,p=this.length+s.length;return this.length===10&&s.length===10?_=j(this,s,u):p<63?_=L(this,s,u):p<1024?_=V(this,s,u):_=N(this,s,u),_};function D(y,s){this.x=y,this.y=s}D.prototype.makeRBT=function(s){for(var u=new Array(s),_=n.prototype._countBits(s)-1,p=0;p<s;p++)u[p]=this.revBin(p,_,s);return u},D.prototype.revBin=function(s,u,_){if(s===0||s===_-1)return s;for(var p=0,g=0;g<u;g++)p|=(s&1)<<u-g-1,s>>=1;return p},D.prototype.permute=function(s,u,_,p,g,v){for(var C=0;C<v;C++)p[C]=u[s[C]],g[C]=_[s[C]]},D.prototype.transform=function(s,u,_,p,g,v){this.permute(v,s,u,_,p,g);for(var C=1;C<g;C<<=1)for(var m=C<<1,c=Math.cos(2*Math.PI/m),b=Math.sin(2*Math.PI/m),A=0;A<g;A+=m)for(var x=c,E=b,q=0;q<C;q++){var U=_[A+q],K=p[A+q],zt=_[A+q+C],H=p[A+q+C],G=x*zt-E*H;H=x*H+E*zt,zt=G,_[A+q]=U+zt,p[A+q]=K+H,_[A+q+C]=U-zt,p[A+q+C]=K-H,q!==m&&(G=c*x-b*E,E=c*E+b*x,x=G)}},D.prototype.guessLen13b=function(s,u){var _=Math.max(u,s)|1,p=_&1,g=0;for(_=_/2|0;_;_=_>>>1)g++;return 1<<g+1+p},D.prototype.conjugate=function(s,u,_){if(!(_<=1))for(var p=0;p<_/2;p++){var g=s[p];s[p]=s[_-p-1],s[_-p-1]=g,g=u[p],u[p]=-u[_-p-1],u[_-p-1]=-g}},D.prototype.normalize13b=function(s,u){for(var _=0,p=0;p<u/2;p++){var g=Math.round(s[2*p+1]/u)*8192+Math.round(s[2*p]/u)+_;s[p]=g&67108863,g<67108864?_=0:_=g/67108864|0}return s},D.prototype.convert13b=function(s,u,_,p){for(var g=0,v=0;v<u;v++)g=g+(s[v]|0),_[2*v]=g&8191,g=g>>>13,_[2*v+1]=g&8191,g=g>>>13;for(v=2*u;v<p;++v)_[v]=0;e(g===0),e((g&-8192)===0)},D.prototype.stub=function(s){for(var u=new Array(s),_=0;_<s;_++)u[_]=0;return u},D.prototype.mulp=function(s,u,_){var p=2*this.guessLen13b(s.length,u.length),g=this.makeRBT(p),v=this.stub(p),C=new Array(p),m=new Array(p),c=new Array(p),b=new Array(p),A=new Array(p),x=new Array(p),E=_.words;E.length=p,this.convert13b(s.words,s.length,C,p),this.convert13b(u.words,u.length,b,p),this.transform(C,v,m,c,p,g),this.transform(b,v,A,x,p,g);for(var q=0;q<p;q++){var U=m[q]*A[q]-c[q]*x[q];c[q]=m[q]*x[q]+c[q]*A[q],m[q]=U}return this.conjugate(m,c,p),this.transform(m,c,E,v,p,g),this.conjugate(E,v,p),this.normalize13b(E,p),_.negative=s.negative^u.negative,_.length=s.length+u.length,_._strip()},n.prototype.mul=function(s){var u=new n(null);return u.words=new Array(this.length+s.length),this.mulTo(s,u)},n.prototype.mulf=function(s){var u=new n(null);return u.words=new Array(this.length+s.length),N(this,s,u)},n.prototype.imul=function(s){return this.clone().mulTo(s,this)},n.prototype.imuln=function(s){var u=s<0;u&&(s=-s),e(typeof s=="number"),e(s<67108864);for(var _=0,p=0;p<this.length;p++){var g=(this.words[p]|0)*s,v=(g&67108863)+(_&67108863);_>>=26,_+=g/67108864|0,_+=v>>>26,this.words[p]=v&67108863}return _!==0&&(this.words[p]=_,this.length++),this.length=s===0?1:this.length,u?this.ineg():this},n.prototype.muln=function(s){return this.clone().imuln(s)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(s){var u=S(s);if(u.length===0)return new n(1);for(var _=this,p=0;p<u.length&&u[p]===0;p++,_=_.sqr());if(++p<u.length)for(var g=_.sqr();p<u.length;p++,g=g.sqr())u[p]!==0&&(_=_.mul(g));return _},n.prototype.iushln=function(s){e(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26,p=67108863>>>26-u<<26-u,g;if(u!==0){var v=0;for(g=0;g<this.length;g++){var C=this.words[g]&p,m=(this.words[g]|0)-C<<u;this.words[g]=m|v,v=C>>>26-u}v&&(this.words[g]=v,this.length++)}if(_!==0){for(g=this.length-1;g>=0;g--)this.words[g+_]=this.words[g];for(g=0;g<_;g++)this.words[g]=0;this.length+=_}return this._strip()},n.prototype.ishln=function(s){return e(this.negative===0),this.iushln(s)},n.prototype.iushrn=function(s,u,_){e(typeof s=="number"&&s>=0);var p;u?p=(u-u%26)/26:p=0;var g=s%26,v=Math.min((s-g)/26,this.length),C=67108863^67108863>>>g<<g,m=_;if(p-=v,p=Math.max(0,p),m){for(var c=0;c<v;c++)m.words[c]=this.words[c];m.length=v}if(v!==0)if(this.length>v)for(this.length-=v,c=0;c<this.length;c++)this.words[c]=this.words[c+v];else this.words[0]=0,this.length=1;var b=0;for(c=this.length-1;c>=0&&(b!==0||c>=p);c--){var A=this.words[c]|0;this.words[c]=b<<26-g|A>>>g,b=A&C}return m&&b!==0&&(m.words[m.length++]=b),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},n.prototype.ishrn=function(s,u,_){return e(this.negative===0),this.iushrn(s,u,_)},n.prototype.shln=function(s){return this.clone().ishln(s)},n.prototype.ushln=function(s){return this.clone().iushln(s)},n.prototype.shrn=function(s){return this.clone().ishrn(s)},n.prototype.ushrn=function(s){return this.clone().iushrn(s)},n.prototype.testn=function(s){e(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26,p=1<<u;if(this.length<=_)return!1;var g=this.words[_];return!!(g&p)},n.prototype.imaskn=function(s){e(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26;if(e(this.negative===0,"imaskn works only with positive numbers"),this.length<=_)return this;if(u!==0&&_++,this.length=Math.min(_,this.length),u!==0){var p=67108863^67108863>>>u<<u;this.words[this.length-1]&=p}return this._strip()},n.prototype.maskn=function(s){return this.clone().imaskn(s)},n.prototype.iaddn=function(s){return e(typeof s=="number"),e(s<67108864),s<0?this.isubn(-s):this.negative!==0?this.length===1&&(this.words[0]|0)<=s?(this.words[0]=s-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(s),this.negative=1,this):this._iaddn(s)},n.prototype._iaddn=function(s){this.words[0]+=s;for(var u=0;u<this.length&&this.words[u]>=67108864;u++)this.words[u]-=67108864,u===this.length-1?this.words[u+1]=1:this.words[u+1]++;return this.length=Math.max(this.length,u+1),this},n.prototype.isubn=function(s){if(e(typeof s=="number"),e(s<67108864),s<0)return this.iaddn(-s);if(this.negative!==0)return this.negative=0,this.iaddn(s),this.negative=1,this;if(this.words[0]-=s,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var u=0;u<this.length&&this.words[u]<0;u++)this.words[u]+=67108864,this.words[u+1]-=1;return this._strip()},n.prototype.addn=function(s){return this.clone().iaddn(s)},n.prototype.subn=function(s){return this.clone().isubn(s)},n.prototype.iabs=function(){return this.negative=0,this},n.prototype.abs=function(){return this.clone().iabs()},n.prototype._ishlnsubmul=function(s,u,_){var p=s.length+_,g;this._expand(p);var v,C=0;for(g=0;g<s.length;g++){v=(this.words[g+_]|0)+C;var m=(s.words[g]|0)*u;v-=m&67108863,C=(v>>26)-(m/67108864|0),this.words[g+_]=v&67108863}for(;g<this.length-_;g++)v=(this.words[g+_]|0)+C,C=v>>26,this.words[g+_]=v&67108863;if(C===0)return this._strip();for(e(C===-1),C=0,g=0;g<this.length;g++)v=-(this.words[g]|0)+C,C=v>>26,this.words[g]=v&67108863;return this.negative=1,this._strip()},n.prototype._wordDiv=function(s,u){var _=this.length-s.length,p=this.clone(),g=s,v=g.words[g.length-1]|0,C=this._countBits(v);_=26-C,_!==0&&(g=g.ushln(_),p.iushln(_),v=g.words[g.length-1]|0);var m=p.length-g.length,c;if(u!=="mod"){c=new n(null),c.length=m+1,c.words=new Array(c.length);for(var b=0;b<c.length;b++)c.words[b]=0}var A=p.clone()._ishlnsubmul(g,1,m);A.negative===0&&(p=A,c&&(c.words[m]=1));for(var x=m-1;x>=0;x--){var E=(p.words[g.length+x]|0)*67108864+(p.words[g.length+x-1]|0);for(E=Math.min(E/v|0,67108863),p._ishlnsubmul(g,E,x);p.negative!==0;)E--,p.negative=0,p._ishlnsubmul(g,1,x),p.isZero()||(p.negative^=1);c&&(c.words[x]=E)}return c&&c._strip(),p._strip(),u!=="div"&&_!==0&&p.iushrn(_),{div:c||null,mod:p}},n.prototype.divmod=function(s,u,_){if(e(!s.isZero()),this.isZero())return{div:new n(0),mod:new n(0)};var p,g,v;return this.negative!==0&&s.negative===0?(v=this.neg().divmod(s,u),u!=="mod"&&(p=v.div.neg()),u!=="div"&&(g=v.mod.neg(),_&&g.negative!==0&&g.iadd(s)),{div:p,mod:g}):this.negative===0&&s.negative!==0?(v=this.divmod(s.neg(),u),u!=="mod"&&(p=v.div.neg()),{div:p,mod:v.mod}):(this.negative&s.negative)!==0?(v=this.neg().divmod(s.neg(),u),u!=="div"&&(g=v.mod.neg(),_&&g.negative!==0&&g.isub(s)),{div:v.div,mod:g}):s.length>this.length||this.cmp(s)<0?{div:new n(0),mod:this}:s.length===1?u==="div"?{div:this.divn(s.words[0]),mod:null}:u==="mod"?{div:null,mod:new n(this.modrn(s.words[0]))}:{div:this.divn(s.words[0]),mod:new n(this.modrn(s.words[0]))}:this._wordDiv(s,u)},n.prototype.div=function(s){return this.divmod(s,"div",!1).div},n.prototype.mod=function(s){return this.divmod(s,"mod",!1).mod},n.prototype.umod=function(s){return this.divmod(s,"mod",!0).mod},n.prototype.divRound=function(s){var u=this.divmod(s);if(u.mod.isZero())return u.div;var _=u.div.negative!==0?u.mod.isub(s):u.mod,p=s.ushrn(1),g=s.andln(1),v=_.cmp(p);return v<0||g===1&&v===0?u.div:u.div.negative!==0?u.div.isubn(1):u.div.iaddn(1)},n.prototype.modrn=function(s){var u=s<0;u&&(s=-s),e(s<=67108863);for(var _=(1<<26)%s,p=0,g=this.length-1;g>=0;g--)p=(_*p+(this.words[g]|0))%s;return u?-p:p},n.prototype.modn=function(s){return this.modrn(s)},n.prototype.idivn=function(s){var u=s<0;u&&(s=-s),e(s<=67108863);for(var _=0,p=this.length-1;p>=0;p--){var g=(this.words[p]|0)+_*67108864;this.words[p]=g/s|0,_=g%s}return this._strip(),u?this.ineg():this},n.prototype.divn=function(s){return this.clone().idivn(s)},n.prototype.egcd=function(s){e(s.negative===0),e(!s.isZero());var u=this,_=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var p=new n(1),g=new n(0),v=new n(0),C=new n(1),m=0;u.isEven()&&_.isEven();)u.iushrn(1),_.iushrn(1),++m;for(var c=_.clone(),b=u.clone();!u.isZero();){for(var A=0,x=1;(u.words[0]&x)===0&&A<26;++A,x<<=1);if(A>0)for(u.iushrn(A);A-- >0;)(p.isOdd()||g.isOdd())&&(p.iadd(c),g.isub(b)),p.iushrn(1),g.iushrn(1);for(var E=0,q=1;(_.words[0]&q)===0&&E<26;++E,q<<=1);if(E>0)for(_.iushrn(E);E-- >0;)(v.isOdd()||C.isOdd())&&(v.iadd(c),C.isub(b)),v.iushrn(1),C.iushrn(1);u.cmp(_)>=0?(u.isub(_),p.isub(v),g.isub(C)):(_.isub(u),v.isub(p),C.isub(g))}return{a:v,b:C,gcd:_.iushln(m)}},n.prototype._invmp=function(s){e(s.negative===0),e(!s.isZero());var u=this,_=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var p=new n(1),g=new n(0),v=_.clone();u.cmpn(1)>0&&_.cmpn(1)>0;){for(var C=0,m=1;(u.words[0]&m)===0&&C<26;++C,m<<=1);if(C>0)for(u.iushrn(C);C-- >0;)p.isOdd()&&p.iadd(v),p.iushrn(1);for(var c=0,b=1;(_.words[0]&b)===0&&c<26;++c,b<<=1);if(c>0)for(_.iushrn(c);c-- >0;)g.isOdd()&&g.iadd(v),g.iushrn(1);u.cmp(_)>=0?(u.isub(_),p.isub(g)):(_.isub(u),g.isub(p))}var A;return u.cmpn(1)===0?A=p:A=g,A.cmpn(0)<0&&A.iadd(s),A},n.prototype.gcd=function(s){if(this.isZero())return s.abs();if(s.isZero())return this.abs();var u=this.clone(),_=s.clone();u.negative=0,_.negative=0;for(var p=0;u.isEven()&&_.isEven();p++)u.iushrn(1),_.iushrn(1);do{for(;u.isEven();)u.iushrn(1);for(;_.isEven();)_.iushrn(1);var g=u.cmp(_);if(g<0){var v=u;u=_,_=v}else if(g===0||_.cmpn(1)===0)break;u.isub(_)}while(!0);return _.iushln(p)},n.prototype.invm=function(s){return this.egcd(s).a.umod(s)},n.prototype.isEven=function(){return(this.words[0]&1)===0},n.prototype.isOdd=function(){return(this.words[0]&1)===1},n.prototype.andln=function(s){return this.words[0]&s},n.prototype.bincn=function(s){e(typeof s=="number");var u=s%26,_=(s-u)/26,p=1<<u;if(this.length<=_)return this._expand(_+1),this.words[_]|=p,this;for(var g=p,v=_;g!==0&&v<this.length;v++){var C=this.words[v]|0;C+=g,g=C>>>26,C&=67108863,this.words[v]=C}return g!==0&&(this.words[v]=g,this.length++),this},n.prototype.isZero=function(){return this.length===1&&this.words[0]===0},n.prototype.cmpn=function(s){var u=s<0;if(this.negative!==0&&!u)return-1;if(this.negative===0&&u)return 1;this._strip();var _;if(this.length>1)_=1;else{u&&(s=-s),e(s<=67108863,"Number is too big");var p=this.words[0]|0;_=p===s?0:p<s?-1:1}return this.negative!==0?-_|0:_},n.prototype.cmp=function(s){if(this.negative!==0&&s.negative===0)return-1;if(this.negative===0&&s.negative!==0)return 1;var u=this.ucmp(s);return this.negative!==0?-u|0:u},n.prototype.ucmp=function(s){if(this.length>s.length)return 1;if(this.length<s.length)return-1;for(var u=0,_=this.length-1;_>=0;_--){var p=this.words[_]|0,g=s.words[_]|0;if(p!==g){p<g?u=-1:p>g&&(u=1);break}}return u},n.prototype.gtn=function(s){return this.cmpn(s)===1},n.prototype.gt=function(s){return this.cmp(s)===1},n.prototype.gten=function(s){return this.cmpn(s)>=0},n.prototype.gte=function(s){return this.cmp(s)>=0},n.prototype.ltn=function(s){return this.cmpn(s)===-1},n.prototype.lt=function(s){return this.cmp(s)===-1},n.prototype.lten=function(s){return this.cmpn(s)<=0},n.prototype.lte=function(s){return this.cmp(s)<=0},n.prototype.eqn=function(s){return this.cmpn(s)===0},n.prototype.eq=function(s){return this.cmp(s)===0},n.red=function(s){return new $(s)},n.prototype.toRed=function(s){return e(!this.red,"Already a number in reduction context"),e(this.negative===0,"red works only with positives"),s.convertTo(this)._forceRed(s)},n.prototype.fromRed=function(){return e(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},n.prototype._forceRed=function(s){return this.red=s,this},n.prototype.forceRed=function(s){return e(!this.red,"Already a number in reduction context"),this._forceRed(s)},n.prototype.redAdd=function(s){return e(this.red,"redAdd works only with red numbers"),this.red.add(this,s)},n.prototype.redIAdd=function(s){return e(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,s)},n.prototype.redSub=function(s){return e(this.red,"redSub works only with red numbers"),this.red.sub(this,s)},n.prototype.redISub=function(s){return e(this.red,"redISub works only with red numbers"),this.red.isub(this,s)},n.prototype.redShl=function(s){return e(this.red,"redShl works only with red numbers"),this.red.shl(this,s)},n.prototype.redMul=function(s){return e(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.mul(this,s)},n.prototype.redIMul=function(s){return e(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.imul(this,s)},n.prototype.redSqr=function(){return e(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return e(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return e(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return e(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return e(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(s){return e(this.red&&!s.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,s)};var jt={k256:null,p224:null,p192:null,p25519:null};function z(y,s){this.name=y,this.p=new n(s,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}z.prototype._tmp=function(){var s=new n(null);return s.words=new Array(Math.ceil(this.n/13)),s},z.prototype.ireduce=function(s){var u=s,_;do this.split(u,this.tmp),u=this.imulK(u),u=u.iadd(this.tmp),_=u.bitLength();while(_>this.n);var p=_<this.n?-1:u.ucmp(this.p);return p===0?(u.words[0]=0,u.length=1):p>0?u.isub(this.p):u.strip!==void 0?u.strip():u._strip(),u},z.prototype.split=function(s,u){s.iushrn(this.n,0,u)},z.prototype.imulK=function(s){return s.imul(this.k)};function $t(){z.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}i($t,z),$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 ne(){z.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}i(ne,z);function Zt(){z.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}i(Zt,z);function oe(){z.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}i(oe,z),oe.prototype.imulK=function(s){for(var u=0,_=0;_<s.length;_++){var p=(s.words[_]|0)*19+u,g=p&67108863;p>>>=26,s.words[_]=g,u=p}return u!==0&&(s.words[s.length++]=u),s},n._prime=function(s){if(jt[s])return jt[s];var u;if(s==="k256")u=new $t;else if(s==="p224")u=new ne;else if(s==="p192")u=new Zt;else if(s==="p25519")u=new oe;else throw new Error("Unknown prime "+s);return jt[s]=u,u};function $(y){if(typeof y=="string"){var s=n._prime(y);this.m=s.p,this.prime=s}else e(y.gtn(1),"modulus must be greater than 1"),this.m=y,this.prime=null}$.prototype._verify1=function(s){e(s.negative===0,"red works only with positives"),e(s.red,"red works only with red numbers")},$.prototype._verify2=function(s,u){e((s.negative|u.negative)===0,"red works only with positives"),e(s.red&&s.red===u.red,"red works only with red numbers")},$.prototype.imod=function(s){return this.prime?this.prime.ireduce(s)._forceRed(this):(f(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(e(u%2===1),u===3){var _=this.m.add(new n(1)).iushrn(2);return this.pow(s,_)}for(var p=this.m.subn(1),g=0;!p.isZero()&&p.andln(1)===0;)g++,p.iushrn(1);e(!p.isZero());var v=new n(1).toRed(this),C=v.redNeg(),m=this.m.subn(1).iushrn(1),c=this.m.bitLength();for(c=new n(2*c*c).toRed(this);this.pow(c,m).cmp(C)!==0;)c.redIAdd(C);for(var b=this.pow(c,p),A=this.pow(s,p.addn(1).iushrn(1)),x=this.pow(s,p),E=g;x.cmp(v)!==0;){for(var q=x,U=0;q.cmp(v)!==0;U++)q=q.redSqr();e(U<E);var K=this.pow(b,new n(1).iushln(E-U-1));A=A.redMul(K),b=K.redSqr(),x=x.redMul(b),E=U}return A},$.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 n(1).toRed(this);if(u.cmpn(1)===0)return s.clone();var _=4,p=new Array(1<<_);p[0]=new n(1).toRed(this),p[1]=s;for(var g=2;g<p.length;g++)p[g]=this.mul(p[g-1],s);var v=p[0],C=0,m=0,c=u.bitLength()%26;for(c===0&&(c=26),g=u.length-1;g>=0;g--){for(var b=u.words[g],A=c-1;A>=0;A--){var x=b>>A&1;if(v!==p[0]&&(v=this.sqr(v)),x===0&&C===0){m=0;continue}C<<=1,C|=x,m++,!(m!==_&&(g!==0||A!==0))&&(v=this.mul(v,p[C]),m=0,C=0)}c=26}return v},$.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},n.mont=function(s){return new Nt(s)};function Nt(y){$.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,$),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),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)},Nt.prototype.mul=function(s,u){if(s.isZero()||u.isZero())return new n(0)._forceRed(this);var _=s.mul(u),p=_.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=_.isub(p).iushrn(this.shift),v=g;return g.cmp(this.m)>=0?v=g.isub(this.m):g.cmpn(0)<0&&(v=g.iadd(this.m)),v._forceRed(this)},Nt.prototype.invm=function(s){var u=this.imod(s._invmp(this.m).mul(this.r2));return u._forceRed(this)}})(typeof on>"u"||on,_o)});import{SdkWrapper as va}from"@cetusprotocol/common-sdk";import{normalizeSuiObjectId as tn}from"@mysten/sui/utils";import{CACHE_TIME_24H as Ht,CACHE_TIME_5MIN as Br,CachedContent as jr,DETAILS_KEYS as Le,extractStructTagFromType as en,fixCoinType as qr,getFutureTime as uo,getObjectFields as ti,getObjectId as nn,getObjectPreviousTransactionDigest as Dr,getObjectType as Lr,getPackagerConfigs as le,normalizeCoinType as fo}from"@cetusprotocol/common-sdk";var Zn="3.7.8",Mr=Zn,Pe=typeof Buffer=="function",Un=typeof TextDecoder=="function"?new TextDecoder:void 0,Vn=typeof TextEncoder=="function"?new TextEncoder:void 0,kr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",qe=Array.prototype.slice.call(kr),Qe=(o=>{let t={};return o.forEach((e,i)=>t[e]=i),t})(qe),Tr=/^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/,Et=String.fromCharCode.bind(String),Kn=typeof Uint8Array.from=="function"?Uint8Array.from.bind(Uint8Array):o=>new Uint8Array(Array.prototype.slice.call(o,0)),zn=o=>o.replace(/=/g,"").replace(/[+\/]/g,t=>t=="+"?"-":"_"),Gn=o=>o.replace(/[^A-Za-z0-9\+\/]/g,""),Qn=o=>{let t,e,i,n,r="",a=o.length%3;for(let l=0;l<o.length;){if((e=o.charCodeAt(l++))>255||(i=o.charCodeAt(l++))>255||(n=o.charCodeAt(l++))>255)throw new TypeError("invalid character found");t=e<<16|i<<8|n,r+=qe[t>>18&63]+qe[t>>12&63]+qe[t>>6&63]+qe[t&63]}return a?r.slice(0,a-3)+"===".substring(a):r},Ji=typeof btoa=="function"?o=>btoa(o):Pe?o=>Buffer.from(o,"binary").toString("base64"):Qn,Gi=Pe?o=>Buffer.from(o).toString("base64"):o=>{let e=[];for(let i=0,n=o.length;i<n;i+=4096)e.push(Et.apply(null,o.subarray(i,i+4096)));return Ji(e.join(""))},Je=(o,t=!1)=>t?zn(Gi(o)):Gi(o),Ar=o=>{if(o.length<2){var t=o.charCodeAt(0);return t<128?o:t<2048?Et(192|t>>>6)+Et(128|t&63):Et(224|t>>>12&15)+Et(128|t>>>6&63)+Et(128|t&63)}else{var t=65536+(o.charCodeAt(0)-55296)*1024+(o.charCodeAt(1)-56320);return Et(240|t>>>18&7)+Et(128|t>>>12&63)+Et(128|t>>>6&63)+Et(128|t&63)}},Sr=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g,Jn=o=>o.replace(Sr,Ar),Hn=Pe?o=>Buffer.from(o,"utf8").toString("base64"):Vn?o=>Gi(Vn.encode(o)):o=>Ji(Jn(o)),ve=(o,t=!1)=>t?zn(Hn(o)):Hn(o),Wn=o=>ve(o,!0),xr=/[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g,Ir=o=>{switch(o.length){case 4:var t=(7&o.charCodeAt(0))<<18|(63&o.charCodeAt(1))<<12|(63&o.charCodeAt(2))<<6|63&o.charCodeAt(3),e=t-65536;return Et((e>>>10)+55296)+Et((e&1023)+56320);case 3:return Et((15&o.charCodeAt(0))<<12|(63&o.charCodeAt(1))<<6|63&o.charCodeAt(2));default:return Et((31&o.charCodeAt(0))<<6|63&o.charCodeAt(1))}},Xn=o=>o.replace(xr,Ir),Yn=o=>{if(o=o.replace(/\s+/g,""),!Tr.test(o))throw new TypeError("malformed base64.");o+="==".slice(2-(o.length&3));let t,e,i,n=[];for(let r=0;r<o.length;)t=Qe[o.charAt(r++)]<<18|Qe[o.charAt(r++)]<<12|(e=Qe[o.charAt(r++)])<<6|(i=Qe[o.charAt(r++)]),e===64?n.push(Et(t>>16&255)):i===64?n.push(Et(t>>16&255,t>>8&255)):n.push(Et(t>>16&255,t>>8&255,t&255));return n.join("")},Xi=typeof atob=="function"?o=>atob(Gn(o)):Pe?o=>Buffer.from(o,"base64").toString("binary"):Yn,to=Pe?o=>Kn(Buffer.from(o,"base64")):o=>Kn(Xi(o).split("").map(t=>t.charCodeAt(0))),eo=o=>to(io(o)),Er=Pe?o=>Buffer.from(o,"base64").toString("utf8"):Un?o=>Un.decode(to(o)):o=>Xn(Xi(o)),io=o=>Gn(o.replace(/[-_]/g,t=>t=="-"?"+":"/")),Qi=o=>Er(io(o)),Or=o=>{if(typeof o!="string")return!1;let t=o.replace(/\s+/g,"").replace(/={0,2}$/,"");return!/[^\s0-9a-zA-Z\+/]/.test(t)||!/[^\s0-9a-zA-Z\-_]/.test(t)},no=o=>({value:o,enumerable:!1,writable:!0,configurable:!0}),oo=function(){let o=(t,e)=>Object.defineProperty(String.prototype,t,no(e));o("fromBase64",function(){return Qi(this)}),o("toBase64",function(t){return ve(this,t)}),o("toBase64URI",function(){return ve(this,!0)}),o("toBase64URL",function(){return ve(this,!0)}),o("toUint8Array",function(){return eo(this)})},ro=function(){let o=(t,e)=>Object.defineProperty(Uint8Array.prototype,t,no(e));o("toBase64",function(t){return Je(this,t)}),o("toBase64URI",function(){return Je(this,!0)}),o("toBase64URL",function(){return Je(this,!0)})},Rr=()=>{oo(),ro()},Xe={version:Zn,VERSION:Mr,atob:Xi,atobPolyfill:Yn,btoa:Ji,btoaPolyfill:Qn,fromBase64:Qi,toBase64:ve,encode:ve,encodeURI:Wn,encodeURL:Wn,utob:Jn,btou:Xn,decode:Qi,isValid:Or,fromUint8Array:Je,toUint8Array:eo,extendString:oo,extendUint8Array:ro,extendBuiltins:Rr};import{BaseError as Fr}from"@cetusprotocol/common-sdk";var so=(f=>(f.InvalidSqrtPriceLimitDirection="InvalidSqrtPriceLimitDirection",f.ZeroTradableAmount="ZeroTradableAmount",f.AmountOutBelowMinimum="AmountOutBelowMinimum",f.AmountInAboveMaximum="AmountInAboveMaximum",f.NextTickNotFound="NextTickNotFound",f.TickArraySequenceInvalid="TickArraySequenceInvalid",f.TickArrayCrossingAboveMax="TickArrayCrossingAboveMax",f.TickArrayIndexNotInitialized="TickArrayIndexNotInitialized",f.ParamsLengthNotEqual="ParamsLengthNotEqual",f))(so||{}),Ye=(r=>(r.InvalidTickEvent="InvalidTickEvent",r.InvalidPositionObject="InvalidPositionObject",r.InvalidPositionRewardObject="InvalidPositionRewardObject",r.InvalidParams="InvalidParams",r.FetchError="FetchError",r))(Ye||{}),De=(f=>(f.InvalidCoinTypeSequence="InvalidCoinTypeSequence",f.InvalidTickIndex="InvalidTickIndex",f.InvalidPoolObject="InvalidPoolObject",f.InvalidTickObjectId="InvalidTickObjectId",f.InvalidTickObject="InvalidTickObject",f.InvalidTickFields="InvalidTickFields",f.PoolsNotFound="PoolsNotFound",f.StatsPoolsUrlNotSet="StatsPoolsUrlNotSet",f.FetchError="FetchError",f))(De||{}),ao=(e=>(e.ClmmVestNotSet="ClmmVestNotSet",e.ClmmVestFetchError="ClmmVestFetchError",e))(ao||{}),co=(e=>(e.NotFoundPartnerObject="NotFoundPartnerObject",e.InvalidPartnerRefFeeFields="InvalidPartnerRefFeeFields",e))(co||{}),Me=(i=>(i.InvalidConfig="InvalidConfig",i.InvalidConfigHandle="InvalidConfigHandle",i.InvalidSimulateAccount="InvalidSimulateAccount",i))(Me||{}),Yi=(a=>(a.InvalidSendAddress="InvalidSendAddress",a.InvalidRecipientAddress="InvalidRecipientAddress",a.InvalidRecipientAndAmountLength="InvalidRecipientAndAmountLength",a.InsufficientBalance="InsufficientBalance",a.InvalidTarget="InvalidTarget",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a))(Yi||{}),Nr=(a=>(a.InvalidCoin="InvalidCoin",a.NotFoundPath="NotFoundPath",a.NoDowngradeNeedParams="NoDowngradeNeedParams",a.InvalidSwapCountUrl="InvalidSwapCountUrl",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a.InvalidServerResponse="InvalidServerResponse",a))(Nr||{}),lo=(t=>(t.InvalidType="InvalidType",t))(lo||{}),Ce=class extends Fr{constructor(t,e,i){super(t,e||"UnknownError",i)}static isVaultsErrorCode(t,e){return this.isErrorCode(t,e)}},qt=(o,t,e)=>{throw new Ce(t.message,o,e)},R=(o,t,e)=>{throw new Ce(t,o,e)};var ei=class{constructor(t){this._cache={};this._sdk=t}get sdk(){return this._sdk}setTokenListCache(t){let{coin_list_handle:e}=le(this.sdk.sdkOptions.cetus_config),i=`${e}_getCoinConfigs`,n=this.getCache(i),r=n?[...n,...t]:t;this.updateCache(i,r)}async getTokenListByCoinTypes(t){let e={},{coin_list_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getCoinConfigs`,r=this.getCache(n);if(r!==void 0){let l=r;for(let d of t)for(let f of l)if(fo(d)===fo(f.address)){e[d]=f;continue}}let a=t.filter(l=>e[l]===void 0);for(let l of a){let d=`${l}_metadata`,f=this.getCache(d);if(f!==void 0)e[l]=f;else{let h=await this._sdk.FullClient.getCoinMetadata({coinType:l});if(h){let w={id:h.id,pyth_id:"",name:h.name,symbol:h.symbol,official_symbol:h.symbol,coingecko_id:"",decimals:h.decimals,project_url:"",logo_url:h.iconUrl,address:l};e[l]=w,this.updateCache(d,w,Ht)}else console.log(`not found ${l}`)}}return e}async getCoinConfigs(t=!1,e=!0){let{coin_list_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getCoinConfigs`,r=this.getCache(n,t);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getCoinConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[Le.METHOD_NAME]:"getCoinConfigs"});let w=this.buildCoinConfig(h,e);this.updateCache(`${i}_${w.address}_getCoinConfig`,w,Ht),f.push({...w})}),this.updateCache(n,f,Ht),f}async getCoinConfig(t,e=!1,i=!0){let{coin_list_handle:n}=le(this.sdk.sdkOptions.cetus_config),r=`${n}_${t}_getCoinConfig`,a=this.getCache(r,e);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"0x1::type_name::TypeName",value:{name:qr(t)}}});(l.error!=null||l.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getCoinConfig get object error: ${l.error}, please check the rpc and contracts address config.`,{[Le.METHOD_NAME]:"getCoinConfig"});let d=this.buildCoinConfig(l,i);return this.updateCache(r,d,Ht),d}buildCoinConfig(t,e=!0){let i=ti(t);i=i.value.fields;let n={...i};return n.id=nn(t),n.address=en(i.coin_type.fields.name).full_address,i.pyth_id&&(n.pyth_id=tn(i.pyth_id)),this.transformExtensions(n,i.extension_fields.fields.contents,e),delete n.coin_type,n}async getClmmPoolConfigs(t=!1,e=!0){let{clmm_pools_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getClmmPoolConfigs`,r=this.getCache(n,t);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getClmmPoolsConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[Le.METHOD_NAME]:"getClmmPoolConfigs"});let w=this.buildClmmPoolConfig(h,e);this.updateCache(`${w.pool_address}_getClmmPoolConfig`,w,Ht),f.push({...w})}),this.updateCache(n,f,Ht),f}async getClmmPoolConfig(t,e=!1,i=!0){let{clmm_pools_handle:n}=le(this.sdk.sdkOptions.cetus_config),r=`${t}_getClmmPoolConfig`,a=this.getCache(r,e);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"address",value:t}}),d=this.buildClmmPoolConfig(l,i);return this.updateCache(r,d,Ht),d}buildClmmPoolConfig(t,e=!0){let i=ti(t);i=i.value.fields;let n={...i};return n.id=nn(t),n.pool_address=tn(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,e),n}async getLaunchpadPoolConfigs(t=!1,e=!0){let{launchpad_pools_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getLaunchpadPoolConfigs`,r=this.getCache(n,t);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getCoinConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[Le.METHOD_NAME]:"getLaunchpadPoolConfigs"});let w=this.buildLaunchpadPoolConfig(h,e);this.updateCache(`${w.pool_address}_getLaunchpadPoolConfig`,w,Ht),f.push({...w})}),this.updateCache(n,f,Ht),f}async getLaunchpadPoolConfig(t,e=!1,i=!0){let{launchpad_pools_handle:n}=le(this.sdk.sdkOptions.cetus_config),r=`${t}_getLaunchpadPoolConfig`,a=this.getCache(r,e);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"address",value:t}}),d=this.buildLaunchpadPoolConfig(l,i);return this.updateCache(r,d,Ht),d}buildLaunchpadPoolConfig(t,e=!0){let i=ti(t);i=i.value.fields;let n={...i};n.id=nn(t),n.pool_address=tn(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,e);let r=[];i.social_media.fields.contents.forEach(a=>{r.push({name:a.fields.value.fields.name,link:a.fields.value.fields.link})}),n.social_media=r;try{n.regulation=decodeURIComponent(Xe.decode(n.regulation).replace(/%/g,"%25"))}catch{n.regulation=Xe.decode(n.regulation)}return n}transformExtensions(t,e,i=!0){let n=[];for(let r of e){let{key:a}=r.fields,{value:l}=r.fields;if(a==="labels")try{let d=decodeURIComponent(Xe.decode(l));try{l=JSON.parse(d)}catch{l=d}}catch{}i&&(t[a]=l),n.push({key:a,value:l})}delete t.extension_fields,i||(t.extensions=n)}async getCetusConfig(t=!1){let e=this._sdk.sdkOptions.cetus_config.package_id,i=`${e}_getCetusConfig`,n=this.getCache(i,t);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:e,options:{showPreviousTransaction:!0}}),a=Dr(r),l=await this._sdk.FullClient.queryEventsByPage({Transaction:a}),d={coin_list_id:"",launchpad_pools_id:"",clmm_pools_id:"",admin_cap_id:"",global_config_id:"",coin_list_handle:"",launchpad_pools_handle:"",clmm_pools_handle:""};if(l.data.length>0)for(let f of l.data)switch(en(f.type).name){case"InitCoinListEvent":d.coin_list_id=f.parsedJson.coin_list_id;break;case"InitLaunchpadPoolsEvent":d.launchpad_pools_id=f.parsedJson.launchpad_pools_id;break;case"InitClmmPoolsEvent":d.clmm_pools_id=f.parsedJson.pools_id;break;case"InitConfigEvent":d.global_config_id=f.parsedJson.global_config_id,d.admin_cap_id=f.parsedJson.admin_cap_id;break;default:break}return d=await this.getCetusConfigHandle(d),d.clmm_pools_id.length>0&&this.updateCache(i,d,Ht),d}async getCetusConfigHandle(t){let e=[t.clmm_pools_id,t.coin_list_id,t.launchpad_pools_id];return(await this._sdk.FullClient.multiGetObjects({ids:e,options:{showContent:!0}})).forEach(n=>{(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidConfigHandle",`when getCetusConfigHandle get objects error: ${n.error}, please check the rpc and contracts address config.`,{[Le.METHOD_NAME]:"getCetusConfigHandle"});let r=ti(n),a=Lr(n);switch(en(a).name){case"ClmmPools":t.clmm_pools_handle=r.pools.fields.id.id;break;case"CoinList":t.coin_list_handle=r.coins.fields.id.id;break;case"LaunchpadPools":t.launchpad_pools_handle=r.pools.fields.id.id;break;default:break}}),t}updateCache(t,e,i=Br){let n=this._cache[t];n?(n.overdue_time=uo(i),n.value=e):n=new jr(e,uo(i)),this._cache[t]=n}getCache(t,e=!1){try{let i=this._cache[t];if(!i)return;if(e||!i.isValid()){delete this._cache[t];return}return i.value}catch(i){console.error(`Error accessing cache for key ${t}:`,i);return}}};var sr=re(Gt());import{SuiClient as ia}from"@mysten/sui/client";import{Transaction as We}from"@mysten/sui/transactions";import{normalizeSuiAddress as xe}from"@mysten/sui/utils";var ho="position_liquidity_snapshot",po=(i=>(i.Deleted="Deleted",i.Exists="Exists",i.NotExists="NotExists",i))(po||{}),mo=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","AddLiquidityV2Event","RemoveLiquidityV2Event"];var Ft=re(Gt());import{asUintN as Do,FEE_RATE_DENOMINATOR as _i,getDeltaDownFromOutput as Lo,getDeltaUpFromInput as $o,getNextSqrtPriceFromInput as Us,getNextSqrtPriceFromOutput as Vs,MathUtil as me,ZERO as Vt}from"@cetusprotocol/common-sdk";var qo=re(Gt());import{Transaction as fn}from"@mysten/sui/transactions";import{adjustForSlippage as js,CLOCK_ADDRESS as fi,CoinAssist as Lt,getPackagerConfigs as jo,MathUtil as _n,MAX_SQRT_PRICE as qs,MIN_SQRT_PRICE as Ds,U64_MAX as Ls,ZERO as $s}from"@cetusprotocol/common-sdk";var go="partner",$e="pool_script",Qt="pool_script_v2",ii="pool_script_v3",ni="router",bo="router_with_partner",Jt="fetcher_script",Da="expect_swap",wo="utils",La="custodian_v2",$a="clob_v2",Ua="endpoints_v2",Va=o=>{if(typeof o=="string"&&o.startsWith("0x"))return"object";if(typeof o=="number"||typeof o=="bigint")return"u64";if(typeof o=="boolean")return"bool";throw new Ce(`Unknown type for value: ${o}`,"InvalidType")};var Bo=re(Gt());import{Transaction as un}from"@mysten/sui/transactions";import{asUintN as Fo,ClmmPoolUtil as Fs,CLOCK_ADDRESS as No,CoinAssist as Dt,getPackagerConfigs as Ns,normalizeCoinType as dn}from"@cetusprotocol/common-sdk";var ke=9e15,_e=1e9,rn="0123456789abcdef",si="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",ai="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",sn={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-ke,maxE:ke,crypto:!1},Co,se,B=!0,li="[DecimalError] ",fe=li+"Invalid argument: ",Mo=li+"Precision limit exceeded",ko=li+"crypto unavailable",To="[object Decimal]",Rt=Math.floor,It=Math.pow,$r=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Ur=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Vr=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,Ao=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,Wt=1e7,F=7,Kr=9007199254740991,Hr=si.length-1,an=ai.length-1,k={toStringTag:To};k.absoluteValue=k.abs=function(){var o=new this.constructor(this);return o.s<0&&(o.s=1),O(o)};k.ceil=function(){return O(new this.constructor(this),this.e+1,2)};k.clampedTo=k.clamp=function(o,t){var e,i=this,n=i.constructor;if(o=new n(o),t=new n(t),!o.s||!t.s)return new n(NaN);if(o.gt(t))throw Error(fe+t);return e=i.cmp(o),e<0?o:i.cmp(t)>0?t:new n(i)};k.comparedTo=k.cmp=function(o){var t,e,i,n,r=this,a=r.d,l=(o=new r.constructor(o)).d,d=r.s,f=o.s;if(!a||!l)return!d||!f?NaN:d!==f?d:a===l?0:!a^d<0?1:-1;if(!a[0]||!l[0])return a[0]?d:l[0]?-f:0;if(d!==f)return d;if(r.e!==o.e)return r.e>o.e^d<0?1:-1;for(i=a.length,n=l.length,t=0,e=i<n?i:n;t<e;++t)if(a[t]!==l[t])return a[t]>l[t]^d<0?1:-1;return i===n?0:i>n^d<0?1:-1};k.cosine=k.cos=function(){var o,t,e=this,i=e.constructor;return e.d?e.d[0]?(o=i.precision,t=i.rounding,i.precision=o+Math.max(e.e,e.sd())+F,i.rounding=1,e=Wr(i,Oo(i,e)),i.precision=o,i.rounding=t,O(se==2||se==3?e.neg():e,o,t,!0)):new i(1):new i(NaN)};k.cubeRoot=k.cbrt=function(){var o,t,e,i,n,r,a,l,d,f,h=this,w=h.constructor;if(!h.isFinite()||h.isZero())return new w(h);for(B=!1,r=h.s*It(h.s*h,1/3),!r||Math.abs(r)==1/0?(e=Ot(h.d),o=h.e,(r=(o-e.length+1)%3)&&(e+=r==1||r==-2?"0":"00"),r=It(e,1/3),o=Rt((o+1)/3)-(o%3==(o<0?-1:2)),r==1/0?e="5e"+o:(e=r.toExponential(),e=e.slice(0,e.indexOf("e")+1)+o),i=new w(e),i.s=h.s):i=new w(r.toString()),a=(o=w.precision)+3;;)if(l=i,d=l.times(l).times(l),f=d.plus(h),i=W(f.plus(h).times(l),f.plus(d),a+2,1),Ot(l.d).slice(0,a)===(e=Ot(i.d)).slice(0,a))if(e=e.slice(a-3,a+1),e=="9999"||!n&&e=="4999"){if(!n&&(O(l,o+1,0),l.times(l).times(l).eq(h))){i=l;break}a+=4,n=1}else{(!+e||!+e.slice(1)&&e.charAt(0)=="5")&&(O(i,o+1,1),t=!i.times(i).times(i).eq(h));break}return B=!0,O(i,o,w.rounding,t)};k.decimalPlaces=k.dp=function(){var o,t=this.d,e=NaN;if(t){if(o=t.length-1,e=(o-Rt(this.e/F))*F,o=t[o],o)for(;o%10==0;o/=10)e--;e<0&&(e=0)}return e};k.dividedBy=k.div=function(o){return W(this,new this.constructor(o))};k.dividedToIntegerBy=k.divToInt=function(o){var t=this,e=t.constructor;return O(W(t,new e(o),0,1,1),e.precision,e.rounding)};k.equals=k.eq=function(o){return this.cmp(o)===0};k.floor=function(){return O(new this.constructor(this),this.e+1,3)};k.greaterThan=k.gt=function(o){return this.cmp(o)>0};k.greaterThanOrEqualTo=k.gte=function(o){var t=this.cmp(o);return t==1||t===0};k.hyperbolicCosine=k.cosh=function(){var o,t,e,i,n,r=this,a=r.constructor,l=new a(1);if(!r.isFinite())return new a(r.s?1/0:NaN);if(r.isZero())return l;e=a.precision,i=a.rounding,a.precision=e+Math.max(r.e,r.sd())+4,a.rounding=1,n=r.d.length,n<32?(o=Math.ceil(n/3),t=(1/di(4,o)).toString()):(o=16,t="2.3283064365386962890625e-10"),r=Te(a,1,r.times(t),new a(1),!0);for(var d,f=o,h=new a(8);f--;)d=r.times(r),r=l.minus(d.times(h.minus(d.times(h))));return O(r,a.precision=e,a.rounding=i,!0)};k.hyperbolicSine=k.sinh=function(){var o,t,e,i,n=this,r=n.constructor;if(!n.isFinite()||n.isZero())return new r(n);if(t=r.precision,e=r.rounding,r.precision=t+Math.max(n.e,n.sd())+4,r.rounding=1,i=n.d.length,i<3)n=Te(r,2,n,n,!0);else{o=1.4*Math.sqrt(i),o=o>16?16:o|0,n=n.times(1/di(5,o)),n=Te(r,2,n,n,!0);for(var a,l=new r(5),d=new r(16),f=new r(20);o--;)a=n.times(n),n=n.times(l.plus(a.times(d.times(a).plus(f))))}return r.precision=t,r.rounding=e,O(n,t,e,!0)};k.hyperbolicTangent=k.tanh=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+7,i.rounding=1,W(e.sinh(),e.cosh(),i.precision=o,i.rounding=t)):new i(e.s)};k.inverseCosine=k.acos=function(){var o=this,t=o.constructor,e=o.abs().cmp(1),i=t.precision,n=t.rounding;return e!==-1?e===0?o.isNeg()?Xt(t,i,n):new t(0):new t(NaN):o.isZero()?Xt(t,i+4,n).times(.5):(t.precision=i+6,t.rounding=1,o=new t(1).minus(o).div(o.plus(1)).sqrt().atan(),t.precision=i,t.rounding=n,o.times(2))};k.inverseHyperbolicCosine=k.acosh=function(){var o,t,e=this,i=e.constructor;return e.lte(1)?new i(e.eq(1)?0:NaN):e.isFinite()?(o=i.precision,t=i.rounding,i.precision=o+Math.max(Math.abs(e.e),e.sd())+4,i.rounding=1,B=!1,e=e.times(e).minus(1).sqrt().plus(e),B=!0,i.precision=o,i.rounding=t,e.ln()):new i(e)};k.inverseHyperbolicSine=k.asinh=function(){var o,t,e=this,i=e.constructor;return!e.isFinite()||e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+2*Math.max(Math.abs(e.e),e.sd())+6,i.rounding=1,B=!1,e=e.times(e).plus(1).sqrt().plus(e),B=!0,i.precision=o,i.rounding=t,e.ln())};k.inverseHyperbolicTangent=k.atanh=function(){var o,t,e,i,n=this,r=n.constructor;return n.isFinite()?n.e>=0?new r(n.abs().eq(1)?n.s/0:n.isZero()?n:NaN):(o=r.precision,t=r.rounding,i=n.sd(),Math.max(i,o)<2*-n.e-1?O(new r(n),o,t,!0):(r.precision=e=i-n.e,n=W(n.plus(1),new r(1).minus(n),e+o,1),r.precision=o+4,r.rounding=1,n=n.ln(),r.precision=o,r.rounding=t,n.times(.5))):new r(NaN)};k.inverseSine=k.asin=function(){var o,t,e,i,n=this,r=n.constructor;return n.isZero()?new r(n):(t=n.abs().cmp(1),e=r.precision,i=r.rounding,t!==-1?t===0?(o=Xt(r,e+4,i).times(.5),o.s=n.s,o):new r(NaN):(r.precision=e+6,r.rounding=1,n=n.div(new r(1).minus(n.times(n)).sqrt().plus(1)).atan(),r.precision=e,r.rounding=i,n.times(2)))};k.inverseTangent=k.atan=function(){var o,t,e,i,n,r,a,l,d,f=this,h=f.constructor,w=h.precision,P=h.rounding;if(f.isFinite()){if(f.isZero())return new h(f);if(f.abs().eq(1)&&w+4<=an)return a=Xt(h,w+4,P).times(.25),a.s=f.s,a}else{if(!f.s)return new h(NaN);if(w+4<=an)return a=Xt(h,w+4,P).times(.5),a.s=f.s,a}for(h.precision=l=w+10,h.rounding=1,e=Math.min(28,l/F+2|0),o=e;o;--o)f=f.div(f.times(f).plus(1).sqrt().plus(1));for(B=!1,t=Math.ceil(l/F),i=1,d=f.times(f),a=new h(f),n=f;o!==-1;)if(n=n.times(d),r=a.minus(n.div(i+=2)),n=n.times(d),a=r.plus(n.div(i+=2)),a.d[t]!==void 0)for(o=t;a.d[o]===r.d[o]&&o--;);return e&&(a=a.times(2<<e-1)),B=!0,O(a,h.precision=w,h.rounding=P,!0)};k.isFinite=function(){return!!this.d};k.isInteger=k.isInt=function(){return!!this.d&&Rt(this.e/F)>this.d.length-2};k.isNaN=function(){return!this.s};k.isNegative=k.isNeg=function(){return this.s<0};k.isPositive=k.isPos=function(){return this.s>0};k.isZero=function(){return!!this.d&&this.d[0]===0};k.lessThan=k.lt=function(o){return this.cmp(o)<0};k.lessThanOrEqualTo=k.lte=function(o){return this.cmp(o)<1};k.logarithm=k.log=function(o){var t,e,i,n,r,a,l,d,f=this,h=f.constructor,w=h.precision,P=h.rounding,M=5;if(o==null)o=new h(10),t=!0;else{if(o=new h(o),e=o.d,o.s<0||!e||!e[0]||o.eq(1))return new h(NaN);t=o.eq(10)}if(e=f.d,f.s<0||!e||!e[0]||f.eq(1))return new h(e&&!e[0]?-1/0:f.s!=1?NaN:e?0:1/0);if(t)if(e.length>1)r=!0;else{for(n=e[0];n%10===0;)n/=10;r=n!==1}if(B=!1,l=w+M,a=de(f,l),i=t?ci(h,l+10):de(o,l),d=W(a,i,l,1),Ue(d.d,n=w,P))do if(l+=10,a=de(f,l),i=t?ci(h,l+10):de(o,l),d=W(a,i,l,1),!r){+Ot(d.d).slice(n+1,n+15)+1==1e14&&(d=O(d,w+1,0));break}while(Ue(d.d,n+=10,P));return B=!0,O(d,w,P)};k.minus=k.sub=function(o){var t,e,i,n,r,a,l,d,f,h,w,P,M=this,T=M.constructor;if(o=new T(o),!M.d||!o.d)return!M.s||!o.s?o=new T(NaN):M.d?o.s=-o.s:o=new T(o.d||M.s!==o.s?M:NaN),o;if(M.s!=o.s)return o.s=-o.s,M.plus(o);if(f=M.d,P=o.d,l=T.precision,d=T.rounding,!f[0]||!P[0]){if(P[0])o.s=-o.s;else if(f[0])o=new T(M);else return new T(d===3?-0:0);return B?O(o,l,d):o}if(e=Rt(o.e/F),h=Rt(M.e/F),f=f.slice(),r=h-e,r){for(w=r<0,w?(t=f,r=-r,a=P.length):(t=P,e=h,a=f.length),i=Math.max(Math.ceil(l/F),a)+2,r>i&&(r=i,t.length=1),t.reverse(),i=r;i--;)t.push(0);t.reverse()}else{for(i=f.length,a=P.length,w=i<a,w&&(a=i),i=0;i<a;i++)if(f[i]!=P[i]){w=f[i]<P[i];break}r=0}for(w&&(t=f,f=P,P=t,o.s=-o.s),a=f.length,i=P.length-a;i>0;--i)f[a++]=0;for(i=P.length;i>r;){if(f[--i]<P[i]){for(n=i;n&&f[--n]===0;)f[n]=Wt-1;--f[n],f[i]+=Wt}f[i]-=P[i]}for(;f[--a]===0;)f.pop();for(;f[0]===0;f.shift())--e;return f[0]?(o.d=f,o.e=ui(f,e),B?O(o,l,d):o):new T(d===3?-0:0)};k.modulo=k.mod=function(o){var t,e=this,i=e.constructor;return o=new i(o),!e.d||!o.s||o.d&&!o.d[0]?new i(NaN):!o.d||e.d&&!e.d[0]?O(new i(e),i.precision,i.rounding):(B=!1,i.modulo==9?(t=W(e,o.abs(),0,3,1),t.s*=o.s):t=W(e,o,0,i.modulo,1),t=t.times(o),B=!0,e.minus(t))};k.naturalExponential=k.exp=function(){return cn(this)};k.naturalLogarithm=k.ln=function(){return de(this)};k.negated=k.neg=function(){var o=new this.constructor(this);return o.s=-o.s,O(o)};k.plus=k.add=function(o){var t,e,i,n,r,a,l,d,f,h,w=this,P=w.constructor;if(o=new P(o),!w.d||!o.d)return!w.s||!o.s?o=new P(NaN):w.d||(o=new P(o.d||w.s===o.s?w:NaN)),o;if(w.s!=o.s)return o.s=-o.s,w.minus(o);if(f=w.d,h=o.d,l=P.precision,d=P.rounding,!f[0]||!h[0])return h[0]||(o=new P(w)),B?O(o,l,d):o;if(r=Rt(w.e/F),i=Rt(o.e/F),f=f.slice(),n=r-i,n){for(n<0?(e=f,n=-n,a=h.length):(e=h,i=r,a=f.length),r=Math.ceil(l/F),a=r>a?r+1:a+1,n>a&&(n=a,e.length=1),e.reverse();n--;)e.push(0);e.reverse()}for(a=f.length,n=h.length,a-n<0&&(n=a,e=h,h=f,f=e),t=0;n;)t=(f[--n]=f[n]+h[n]+t)/Wt|0,f[n]%=Wt;for(t&&(f.unshift(t),++i),a=f.length;f[--a]==0;)f.pop();return o.d=f,o.e=ui(f,i),B?O(o,l,d):o};k.precision=k.sd=function(o){var t,e=this;if(o!==void 0&&o!==!!o&&o!==1&&o!==0)throw Error(fe+o);return e.d?(t=So(e.d),o&&e.e+1>t&&(t=e.e+1)):t=NaN,t};k.round=function(){var o=this,t=o.constructor;return O(new t(o),o.e+1,t.rounding)};k.sine=k.sin=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+Math.max(e.e,e.sd())+F,i.rounding=1,e=zr(i,Oo(i,e)),i.precision=o,i.rounding=t,O(se>2?e.neg():e,o,t,!0)):new i(NaN)};k.squareRoot=k.sqrt=function(){var o,t,e,i,n,r,a=this,l=a.d,d=a.e,f=a.s,h=a.constructor;if(f!==1||!l||!l[0])return new h(!f||f<0&&(!l||l[0])?NaN:l?a:1/0);for(B=!1,f=Math.sqrt(+a),f==0||f==1/0?(t=Ot(l),(t.length+d)%2==0&&(t+="0"),f=Math.sqrt(t),d=Rt((d+1)/2)-(d<0||d%2),f==1/0?t="5e"+d:(t=f.toExponential(),t=t.slice(0,t.indexOf("e")+1)+d),i=new h(t)):i=new h(f.toString()),e=(d=h.precision)+3;;)if(r=i,i=r.plus(W(a,r,e+2,1)).times(.5),Ot(r.d).slice(0,e)===(t=Ot(i.d)).slice(0,e))if(t=t.slice(e-3,e+1),t=="9999"||!n&&t=="4999"){if(!n&&(O(r,d+1,0),r.times(r).eq(a))){i=r;break}e+=4,n=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(O(i,d+1,1),o=!i.times(i).eq(a));break}return B=!0,O(i,d,h.rounding,o)};k.tangent=k.tan=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+10,i.rounding=1,e=e.sin(),e.s=1,e=W(e,new i(1).minus(e.times(e)).sqrt(),o+10,0),i.precision=o,i.rounding=t,O(se==2||se==4?e.neg():e,o,t,!0)):new i(NaN)};k.times=k.mul=function(o){var t,e,i,n,r,a,l,d,f,h=this,w=h.constructor,P=h.d,M=(o=new w(o)).d;if(o.s*=h.s,!P||!P[0]||!M||!M[0])return new w(!o.s||P&&!P[0]&&!M||M&&!M[0]&&!P?NaN:!P||!M?o.s/0:o.s*0);for(e=Rt(h.e/F)+Rt(o.e/F),d=P.length,f=M.length,d<f&&(r=P,P=M,M=r,a=d,d=f,f=a),r=[],a=d+f,i=a;i--;)r.push(0);for(i=f;--i>=0;){for(t=0,n=d+i;n>i;)l=r[n]+M[i]*P[n-i-1]+t,r[n--]=l%Wt|0,t=l/Wt|0;r[n]=(r[n]+t)%Wt|0}for(;!r[--a];)r.pop();return t?++e:r.shift(),o.d=r,o.e=ui(r,e),B?O(o,w.precision,w.rounding):o};k.toBinary=function(o,t){return ln(this,2,o,t)};k.toDecimalPlaces=k.toDP=function(o,t){var e=this,i=e.constructor;return e=new i(e),o===void 0?e:(Bt(o,0,_e),t===void 0?t=i.rounding:Bt(t,0,8),O(e,o+e.e+1,t))};k.toExponential=function(o,t){var e,i=this,n=i.constructor;return o===void 0?e=Yt(i,!0):(Bt(o,0,_e),t===void 0?t=n.rounding:Bt(t,0,8),i=O(new n(i),o+1,t),e=Yt(i,!0,o+1)),i.isNeg()&&!i.isZero()?"-"+e:e};k.toFixed=function(o,t){var e,i,n=this,r=n.constructor;return o===void 0?e=Yt(n):(Bt(o,0,_e),t===void 0?t=r.rounding:Bt(t,0,8),i=O(new r(n),o+n.e+1,t),e=Yt(i,!1,o+i.e+1)),n.isNeg()&&!n.isZero()?"-"+e:e};k.toFraction=function(o){var t,e,i,n,r,a,l,d,f,h,w,P,M=this,T=M.d,S=M.constructor;if(!T)return new S(M);if(f=e=new S(1),i=d=new S(0),t=new S(i),r=t.e=So(T)-M.e-1,a=r%F,t.d[0]=It(10,a<0?F+a:a),o==null)o=r>0?t:f;else{if(l=new S(o),!l.isInt()||l.lt(f))throw Error(fe+l);o=l.gt(t)?r>0?t:f:l}for(B=!1,l=new S(Ot(T)),h=S.precision,S.precision=r=T.length*F*2;w=W(l,t,0,1,1),n=e.plus(w.times(i)),n.cmp(o)!=1;)e=i,i=n,n=f,f=d.plus(w.times(n)),d=n,n=t,t=l.minus(w.times(n)),l=n;return n=W(o.minus(e),i,0,1,1),d=d.plus(n.times(f)),e=e.plus(n.times(i)),d.s=f.s=M.s,P=W(f,i,r,1).minus(M).abs().cmp(W(d,e,r,1).minus(M).abs())<1?[f,i]:[d,e],S.precision=h,B=!0,P};k.toHexadecimal=k.toHex=function(o,t){return ln(this,16,o,t)};k.toNearest=function(o,t){var e=this,i=e.constructor;if(e=new i(e),o==null){if(!e.d)return e;o=new i(1),t=i.rounding}else{if(o=new i(o),t===void 0?t=i.rounding:Bt(t,0,8),!e.d)return o.s?e:o;if(!o.d)return o.s&&(o.s=e.s),o}return o.d[0]?(B=!1,e=W(e,o,0,t,1).times(o),B=!0,O(e)):(o.s=e.s,e=o),e};k.toNumber=function(){return+this};k.toOctal=function(o,t){return ln(this,8,o,t)};k.toPower=k.pow=function(o){var t,e,i,n,r,a,l=this,d=l.constructor,f=+(o=new d(o));if(!l.d||!o.d||!l.d[0]||!o.d[0])return new d(It(+l,f));if(l=new d(l),l.eq(1))return l;if(i=d.precision,r=d.rounding,o.eq(1))return O(l,i,r);if(t=Rt(o.e/F),t>=o.d.length-1&&(e=f<0?-f:f)<=Kr)return n=xo(d,l,e,i),o.s<0?new d(1).div(n):O(n,i,r);if(a=l.s,a<0){if(t<o.d.length-1)return new d(NaN);if((o.d[t]&1)==0&&(a=1),l.e==0&&l.d[0]==1&&l.d.length==1)return l.s=a,l}return e=It(+l,f),t=e==0||!isFinite(e)?Rt(f*(Math.log("0."+Ot(l.d))/Math.LN10+l.e+1)):new d(e+"").e,t>d.maxE+1||t<d.minE-1?new d(t>0?a/0:0):(B=!1,d.rounding=l.s=1,e=Math.min(12,(t+"").length),n=cn(o.times(de(l,i+e)),i),n.d&&(n=O(n,i+5,1),Ue(n.d,i,r)&&(t=i+10,n=O(cn(o.times(de(l,t+e)),t),t+5,1),+Ot(n.d).slice(i+1,i+15)+1==1e14&&(n=O(n,i+1,0)))),n.s=a,B=!0,d.rounding=r,O(n,i,r))};k.toPrecision=function(o,t){var e,i=this,n=i.constructor;return o===void 0?e=Yt(i,i.e<=n.toExpNeg||i.e>=n.toExpPos):(Bt(o,1,_e),t===void 0?t=n.rounding:Bt(t,0,8),i=O(new n(i),o,t),e=Yt(i,o<=i.e||i.e<=n.toExpNeg,o)),i.isNeg()&&!i.isZero()?"-"+e:e};k.toSignificantDigits=k.toSD=function(o,t){var e=this,i=e.constructor;return o===void 0?(o=i.precision,t=i.rounding):(Bt(o,1,_e),t===void 0?t=i.rounding:Bt(t,0,8)),O(new i(e),o,t)};k.toString=function(){var o=this,t=o.constructor,e=Yt(o,o.e<=t.toExpNeg||o.e>=t.toExpPos);return o.isNeg()&&!o.isZero()?"-"+e:e};k.truncated=k.trunc=function(){return O(new this.constructor(this),this.e+1,1)};k.valueOf=k.toJSON=function(){var o=this,t=o.constructor,e=Yt(o,o.e<=t.toExpNeg||o.e>=t.toExpPos);return o.isNeg()?"-"+e:e};function Ot(o){var t,e,i,n=o.length-1,r="",a=o[0];if(n>0){for(r+=a,t=1;t<n;t++)i=o[t]+"",e=F-i.length,e&&(r+=ue(e)),r+=i;a=o[t],i=a+"",e=F-i.length,e&&(r+=ue(e))}else if(a===0)return"0";for(;a%10===0;)a/=10;return r+a}function Bt(o,t,e){if(o!==~~o||o<t||o>e)throw Error(fe+o)}function Ue(o,t,e,i){var n,r,a,l;for(r=o[0];r>=10;r/=10)--t;return--t<0?(t+=F,n=0):(n=Math.ceil((t+1)/F),t%=F),r=It(10,F-t),l=o[n]%r|0,i==null?t<3?(t==0?l=l/100|0:t==1&&(l=l/10|0),a=e<4&&l==99999||e>3&&l==49999||l==5e4||l==0):a=(e<4&&l+1==r||e>3&&l+1==r/2)&&(o[n+1]/r/100|0)==It(10,t-2)-1||(l==r/2||l==0)&&(o[n+1]/r/100|0)==0:t<4?(t==0?l=l/1e3|0:t==1?l=l/100|0:t==2&&(l=l/10|0),a=(i||e<4)&&l==9999||!i&&e>3&&l==4999):a=((i||e<4)&&l+1==r||!i&&e>3&&l+1==r/2)&&(o[n+1]/r/1e3|0)==It(10,t-3)-1,a}function oi(o,t,e){for(var i,n=[0],r,a=0,l=o.length;a<l;){for(r=n.length;r--;)n[r]*=t;for(n[0]+=rn.indexOf(o.charAt(a++)),i=0;i<n.length;i++)n[i]>e-1&&(n[i+1]===void 0&&(n[i+1]=0),n[i+1]+=n[i]/e|0,n[i]%=e)}return n.reverse()}function Wr(o,t){var e,i,n;if(t.isZero())return t;i=t.d.length,i<32?(e=Math.ceil(i/3),n=(1/di(4,e)).toString()):(e=16,n="2.3283064365386962890625e-10"),o.precision+=e,t=Te(o,1,t.times(n),new o(1));for(var r=e;r--;){var a=t.times(t);t=a.times(a).minus(a).times(8).plus(1)}return o.precision-=e,t}var W=(function(){function o(i,n,r){var a,l=0,d=i.length;for(i=i.slice();d--;)a=i[d]*n+l,i[d]=a%r|0,l=a/r|0;return l&&i.unshift(l),i}function t(i,n,r,a){var l,d;if(r!=a)d=r>a?1:-1;else for(l=d=0;l<r;l++)if(i[l]!=n[l]){d=i[l]>n[l]?1:-1;break}return d}function e(i,n,r,a){for(var l=0;r--;)i[r]-=l,l=i[r]<n[r]?1:0,i[r]=l*a+i[r]-n[r];for(;!i[0]&&i.length>1;)i.shift()}return function(i,n,r,a,l,d){var f,h,w,P,M,T,S,L,j,V,N,D,jt,z,$t,ne,Zt,oe,$,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,h=i.e-n.e):(d=Wt,M=F,h=Rt(i.e/M)-Rt(n.e/M)),$=_.length,Zt=u.length,j=new y(s),V=j.d=[],w=0;_[w]==(u[w]||0);w++);if(_[w]>(u[w]||0)&&h--,r==null?(z=r=y.precision,a=y.rounding):l?z=r+(i.e-n.e)+1:z=r,z<0)V.push(1),T=!0;else{if(z=z/M+2|0,w=0,$==1){for(P=0,_=_[0],z++;(w<Zt||P)&&z--;w++)$t=P*d+(u[w]||0),V[w]=$t/_|0,P=$t%_|0;T=P||w<Zt}else{for(P=d/(_[0]+1)|0,P>1&&(_=o(_,P,d),u=o(u,P,d),$=_.length,Zt=u.length),ne=$,N=u.slice(0,$),D=N.length;D<$;)N[D++]=0;Nt=_.slice(),Nt.unshift(0),oe=_[0],_[1]>=d/2&&++oe;do P=0,f=t(_,N,$,D),f<0?(jt=N[0],$!=D&&(jt=jt*d+(N[1]||0)),P=jt/oe|0,P>1?(P>=d&&(P=d-1),S=o(_,P,d),L=S.length,D=N.length,f=t(S,N,L,D),f==1&&(P--,e(S,$<L?Nt:_,L,d))):(P==0&&(f=P=1),S=_.slice()),L=S.length,L<D&&S.unshift(0),e(N,S,D,d),f==-1&&(D=N.length,f=t(_,N,$,D),f<1&&(P++,e(N,$<D?Nt:_,D,d))),D=N.length):f===0&&(P++,N=[0]),V[w++]=P,f&&N[0]?N[D++]=u[ne]||0:(N=[u[ne]],D=1);while((ne++<Zt||N[0]!==void 0)&&z--);T=N[0]!==void 0}V[0]||V.shift()}if(M==1)j.e=h,Co=T;else{for(w=1,P=V[0];P>=10;P/=10)w++;j.e=w+h*M-1,O(j,l?r+j.e+1:r,a,T)}return j}})();function O(o,t,e,i){var n,r,a,l,d,f,h,w,P,M=o.constructor;t:if(t!=null){if(w=o.d,!w)return o;for(n=1,l=w[0];l>=10;l/=10)n++;if(r=t-n,r<0)r+=F,a=t,h=w[P=0],d=h/It(10,n-a-1)%10|0;else if(P=Math.ceil((r+1)/F),l=w.length,P>=l)if(i){for(;l++<=P;)w.push(0);h=d=0,n=1,r%=F,a=r-F+1}else break t;else{for(h=l=w[P],n=1;l>=10;l/=10)n++;r%=F,a=r-F+n,d=a<0?0:h/It(10,n-a-1)%10|0}if(i=i||t<0||w[P+1]!==void 0||(a<0?h:h%It(10,n-a-1)),f=e<4?(d||i)&&(e==0||e==(o.s<0?3:2)):d>5||d==5&&(e==4||i||e==6&&(r>0?a>0?h/It(10,n-a):0:w[P-1])%10&1||e==(o.s<0?8:7)),t<1||!w[0])return w.length=0,f?(t-=o.e+1,w[0]=It(10,(F-t%F)%F),o.e=-t||0):w[0]=o.e=0,o;if(r==0?(w.length=P,l=1,P--):(w.length=P+1,l=It(10,F-r),w[P]=a>0?(h/It(10,n-a)%It(10,a)|0)*l:0),f)for(;;)if(P==0){for(r=1,a=w[0];a>=10;a/=10)r++;for(a=w[0]+=l,l=1;a>=10;a/=10)l++;r!=l&&(o.e++,w[0]==Wt&&(w[0]=1));break}else{if(w[P]+=l,w[P]!=Wt)break;w[P--]=0,l=1}for(r=w.length;w[--r]===0;)w.pop()}return B&&(o.e>M.maxE?(o.d=null,o.e=NaN):o.e<M.minE&&(o.e=0,o.d=[0])),o}function Yt(o,t,e){if(!o.isFinite())return Eo(o);var i,n=o.e,r=Ot(o.d),a=r.length;return t?(e&&(i=e-a)>0?r=r.charAt(0)+"."+r.slice(1)+ue(i):a>1&&(r=r.charAt(0)+"."+r.slice(1)),r=r+(o.e<0?"e":"e+")+o.e):n<0?(r="0."+ue(-n-1)+r,e&&(i=e-a)>0&&(r+=ue(i))):n>=a?(r+=ue(n+1-a),e&&(i=e-n-1)>0&&(r=r+"."+ue(i))):((i=n+1)<a&&(r=r.slice(0,i)+"."+r.slice(i)),e&&(i=e-a)>0&&(n+1===a&&(r+="."),r+=ue(i))),r}function ui(o,t){var e=o[0];for(t*=F;e>=10;e/=10)t++;return t}function ci(o,t,e){if(t>Hr)throw B=!0,e&&(o.precision=e),Error(Mo);return O(new o(si),t,1,!0)}function Xt(o,t,e){if(t>an)throw Error(Mo);return O(new o(ai),t,e,!0)}function So(o){var t=o.length-1,e=t*F+1;if(t=o[t],t){for(;t%10==0;t/=10)e--;for(t=o[0];t>=10;t/=10)e++}return e}function ue(o){for(var t="";o--;)t+="0";return t}function xo(o,t,e,i){var n,r=new o(1),a=Math.ceil(i/F+4);for(B=!1;;){if(e%2&&(r=r.times(t),vo(r.d,a)&&(n=!0)),e=Rt(e/2),e===0){e=r.d.length-1,n&&r.d[e]===0&&++r.d[e];break}t=t.times(t),vo(t.d,a)}return B=!0,r}function yo(o){return o.d[o.d.length-1]&1}function Io(o,t,e){for(var i,n,r=new o(t[0]),a=0;++a<t.length;){if(n=new o(t[a]),!n.s){r=n;break}i=r.cmp(n),(i===e||i===0&&r.s===e)&&(r=n)}return r}function cn(o,t){var e,i,n,r,a,l,d,f=0,h=0,w=0,P=o.constructor,M=P.rounding,T=P.precision;if(!o.d||!o.d[0]||o.e>17)return new P(o.d?o.d[0]?o.s<0?0:1/0:1:o.s?o.s<0?0:o:NaN);for(t==null?(B=!1,d=T):d=t,l=new P(.03125);o.e>-2;)o=o.times(l),w+=5;for(i=Math.log(It(2,w))/Math.LN10*2+5|0,d+=i,e=r=a=new P(1),P.precision=d;;){if(r=O(r.times(o),d,1),e=e.times(++h),l=a.plus(W(r,e,d,1)),Ot(l.d).slice(0,d)===Ot(a.d).slice(0,d)){for(n=w;n--;)a=O(a.times(a),d,1);if(t==null)if(f<3&&Ue(a.d,d-i,M,f))P.precision=d+=10,e=r=l=new P(1),h=0,f++;else return O(a,P.precision=T,M,B=!0);else return P.precision=T,a}a=l}}function de(o,t){var e,i,n,r,a,l,d,f,h,w,P,M=1,T=10,S=o,L=S.d,j=S.constructor,V=j.rounding,N=j.precision;if(S.s<0||!L||!L[0]||!S.e&&L[0]==1&&L.length==1)return new j(L&&!L[0]?-1/0:S.s!=1?NaN:L?0:S);if(t==null?(B=!1,h=N):h=t,j.precision=h+=T,e=Ot(L),i=e.charAt(0),Math.abs(r=S.e)<15e14){for(;i<7&&i!=1||i==1&&e.charAt(1)>3;)S=S.times(o),e=Ot(S.d),i=e.charAt(0),M++;r=S.e,i>1?(S=new j("0."+e),r++):S=new j(i+"."+e.slice(1))}else return f=ci(j,h+2,N).times(r+""),S=de(new j(i+"."+e.slice(1)),h-T).plus(f),j.precision=N,t==null?O(S,N,V,B=!0):S;for(w=S,d=a=S=W(S.minus(1),S.plus(1),h,1),P=O(S.times(S),h,1),n=3;;){if(a=O(a.times(P),h,1),f=d.plus(W(a,new j(n),h,1)),Ot(f.d).slice(0,h)===Ot(d.d).slice(0,h))if(d=d.times(2),r!==0&&(d=d.plus(ci(j,h+2,N).times(r+""))),d=W(d,new j(M),h,1),t==null)if(Ue(d.d,h-T,V,l))j.precision=h+=T,f=a=S=W(w.minus(1),w.plus(1),h,1),P=O(S.times(S),h,1),n=l=1;else return O(d,j.precision=N,V,B=!0);else return j.precision=N,d;d=f,n+=2}}function Eo(o){return String(o.s*o.s/0)}function ri(o,t){var e,i,n;for((e=t.indexOf("."))>-1&&(t=t.replace(".","")),(i=t.search(/e/i))>0?(e<0&&(e=i),e+=+t.slice(i+1),t=t.substring(0,i)):e<0&&(e=t.length),i=0;t.charCodeAt(i)===48;i++);for(n=t.length;t.charCodeAt(n-1)===48;--n);if(t=t.slice(i,n),t){if(n-=i,o.e=e=e-i-1,o.d=[],i=(e+1)%F,e<0&&(i+=F),i<n){for(i&&o.d.push(+t.slice(0,i)),n-=F;i<n;)o.d.push(+t.slice(i,i+=F));t=t.slice(i),i=F-t.length}else i-=n;for(;i--;)t+="0";o.d.push(+t),B&&(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 Zr(o,t){var e,i,n,r,a,l,d,f,h;if(t.indexOf("_")>-1){if(t=t.replace(/(\d)_(?=\d)/g,"$1"),Ao.test(t))return ri(o,t)}else if(t==="Infinity"||t==="NaN")return+t||(o.s=NaN),o.e=NaN,o.d=null,o;if(Ur.test(t))e=16,t=t.toLowerCase();else if($r.test(t))e=2;else if(Vr.test(t))e=8;else throw Error(fe+t);for(r=t.search(/p/i),r>0?(d=+t.slice(r+1),t=t.substring(2,r)):t=t.slice(2),r=t.indexOf("."),a=r>=0,i=o.constructor,a&&(t=t.replace(".",""),l=t.length,r=l-r,n=xo(i,new i(e),r,r*2)),f=oi(t,e,Wt),h=f.length-1,r=h;f[r]===0;--r)f.pop();return r<0?new i(o.s*0):(o.e=ui(f,h),o.d=f,B=!1,a&&(o=W(o,n,l*4)),d&&(o=o.times(Math.abs(d)<54?It(2,d):Ve.pow(2,d))),B=!0,o)}function zr(o,t){var e,i=t.d.length;if(i<3)return t.isZero()?t:Te(o,2,t,t);e=1.4*Math.sqrt(i),e=e>16?16:e|0,t=t.times(1/di(5,e)),t=Te(o,2,t,t);for(var n,r=new o(5),a=new o(16),l=new o(20);e--;)n=t.times(t),t=t.times(r.plus(n.times(a.times(n).minus(l))));return t}function Te(o,t,e,i,n){var r,a,l,d,f=1,h=o.precision,w=Math.ceil(h/F);for(B=!1,d=e.times(e),l=new o(i);;){if(a=W(l.times(d),new o(t++*t++),h,1),l=n?i.plus(a):i.minus(a),i=W(a.times(d),new o(t++*t++),h,1),a=l.plus(i),a.d[w]!==void 0){for(r=w;a.d[r]===l.d[r]&&r--;);if(r==-1)break}r=l,l=i,i=a,a=r,f++}return B=!0,a.d.length=w+1,a}function di(o,t){for(var e=o;--t;)e*=o;return e}function Oo(o,t){var e,i=t.s<0,n=Xt(o,o.precision,1),r=n.times(.5);if(t=t.abs(),t.lte(r))return se=i?4:1,t;if(e=t.divToInt(n),e.isZero())se=i?3:2;else{if(t=t.minus(e.times(n)),t.lte(r))return se=yo(e)?i?2:3:i?4:1,t;se=yo(e)?i?1:4:i?3:2}return t.minus(n).abs()}function ln(o,t,e,i){var n,r,a,l,d,f,h,w,P,M=o.constructor,T=e!==void 0;if(T?(Bt(e,1,_e),i===void 0?i=M.rounding:Bt(i,0,8)):(e=M.precision,i=M.rounding),!o.isFinite())h=Eo(o);else{for(h=Yt(o),a=h.indexOf("."),T?(n=2,t==16?e=e*4-3:t==8&&(e=e*3-2)):n=t,a>=0&&(h=h.replace(".",""),P=new M(1),P.e=h.length-a,P.d=oi(Yt(P),10,n),P.e=P.d.length),w=oi(h,10,n),r=d=w.length;w[--d]==0;)w.pop();if(!w[0])h=T?"0p+0":"0";else{if(a<0?r--:(o=new M(o),o.d=w,o.e=r,o=W(o,P,e,i,0,n),w=o.d,r=o.e,f=Co),a=w[e],l=n/2,f=f||w[e+1]!==void 0,f=i<4?(a!==void 0||f)&&(i===0||i===(o.s<0?3:2)):a>l||a===l&&(i===4||f||i===6&&w[e-1]&1||i===(o.s<0?8:7)),w.length=e,f)for(;++w[--e]>n-1;)w[e]=0,e||(++r,w.unshift(1));for(d=w.length;!w[d-1];--d);for(a=0,h="";a<d;a++)h+=rn.charAt(w[a]);if(T){if(d>1)if(t==16||t==8){for(a=t==16?4:3,--d;d%a;d++)h+="0";for(w=oi(h,n,t),d=w.length;!w[d-1];--d);for(a=1,h="1.";a<d;a++)h+=rn.charAt(w[a])}else h=h.charAt(0)+"."+h.slice(1);h=h+(r<0?"p":"p+")+r}else if(r<0){for(;++r;)h="0"+h;h="0."+h}else if(++r>d)for(r-=d;r--;)h+="0";else r<d&&(h=h.slice(0,r)+"."+h.slice(r))}h=(t==16?"0x":t==2?"0b":t==8?"0o":"")+h}return o.s<0?"-"+h:h}function vo(o,t){if(o.length>t)return o.length=t,!0}function Gr(o){return new this(o).abs()}function Qr(o){return new this(o).acos()}function Jr(o){return new this(o).acosh()}function Xr(o,t){return new this(o).plus(t)}function Yr(o){return new this(o).asin()}function ts(o){return new this(o).asinh()}function es(o){return new this(o).atan()}function is(o){return new this(o).atanh()}function ns(o,t){o=new this(o),t=new this(t);var e,i=this.precision,n=this.rounding,r=i+4;return!o.s||!t.s?e=new this(NaN):!o.d&&!t.d?(e=Xt(this,r,1).times(t.s>0?.25:.75),e.s=o.s):!t.d||o.isZero()?(e=t.s<0?Xt(this,i,n):new this(0),e.s=o.s):!o.d||t.isZero()?(e=Xt(this,r,1).times(.5),e.s=o.s):t.s<0?(this.precision=r,this.rounding=1,e=this.atan(W(o,t,r,1)),t=Xt(this,r,1),this.precision=i,this.rounding=n,e=o.s<0?e.minus(t):e.plus(t)):e=this.atan(W(o,t,r,1)),e}function os(o){return new this(o).cbrt()}function rs(o){return O(o=new this(o),o.e+1,2)}function ss(o,t,e){return new this(o).clamp(t,e)}function as(o){if(!o||typeof o!="object")throw Error(li+"Object expected");var t,e,i,n=o.defaults===!0,r=["precision",1,_e,"rounding",0,8,"toExpNeg",-ke,0,"toExpPos",0,ke,"maxE",0,ke,"minE",-ke,0,"modulo",0,9];for(t=0;t<r.length;t+=3)if(e=r[t],n&&(this[e]=sn[e]),(i=o[e])!==void 0)if(Rt(i)===i&&i>=r[t+1]&&i<=r[t+2])this[e]=i;else throw Error(fe+e+": "+i);if(e="crypto",n&&(this[e]=sn[e]),(i=o[e])!==void 0)if(i===!0||i===!1||i===0||i===1)if(i)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[e]=!0;else throw Error(ko);else this[e]=!1;else throw Error(fe+e+": "+i);return this}function cs(o){return new this(o).cos()}function ls(o){return new this(o).cosh()}function Ro(o){var t,e,i;function n(r){var a,l,d,f=this;if(!(f instanceof n))return new n(r);if(f.constructor=n,Po(r)){f.s=r.s,B?!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++;B?a>n.maxE?(f.e=NaN,f.d=null):a<n.minE?(f.e=0,f.d=[0]):(f.e=a,f.d=[r]):(f.e=a,f.d=[r]);return}if(r*0!==0){r||(f.s=NaN),f.e=NaN,f.d=null;return}return ri(f,r.toString())}if(d==="string")return(l=r.charCodeAt(0))===45?(r=r.slice(1),f.s=-1):(l===43&&(r=r.slice(1)),f.s=1),Ao.test(r)?ri(f,r):Zr(f,r);if(d==="bigint")return r<0?(r=-r,f.s=-1):f.s=1,ri(f,r.toString());throw Error(fe+r)}if(n.prototype=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=as,n.clone=Ro,n.isDecimal=Po,n.abs=Gr,n.acos=Qr,n.acosh=Jr,n.add=Xr,n.asin=Yr,n.asinh=ts,n.atan=es,n.atanh=is,n.atan2=ns,n.cbrt=os,n.ceil=rs,n.clamp=ss,n.cos=cs,n.cosh=ls,n.div=us,n.exp=ds,n.floor=fs,n.hypot=_s,n.ln=hs,n.log=ps,n.log10=gs,n.log2=ms,n.max=bs,n.min=ws,n.mod=ys,n.mul=vs,n.pow=Ps,n.random=Cs,n.round=Ms,n.sign=ks,n.sin=Ts,n.sinh=As,n.sqrt=Ss,n.sub=xs,n.sum=Is,n.tan=Es,n.tanh=Os,n.trunc=Rs,o===void 0&&(o={}),o&&o.defaults!==!0)for(i=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],t=0;t<i.length;)o.hasOwnProperty(e=i[t++])||(o[e]=this[e]);return n.config(o),n}function us(o,t){return new this(o).div(t)}function ds(o){return new this(o).exp()}function fs(o){return O(o=new this(o),o.e+1,3)}function _s(){var o,t,e=new this(0);for(B=!1,o=0;o<arguments.length;)if(t=new this(arguments[o++]),t.d)e.d&&(e=e.plus(t.times(t)));else{if(t.s)return B=!0,new this(1/0);e=t}return B=!0,e.sqrt()}function Po(o){return o instanceof Ve||o&&o.toStringTag===To||!1}function hs(o){return new this(o).ln()}function ps(o,t){return new this(o).log(t)}function ms(o){return new this(o).log(2)}function gs(o){return new this(o).log(10)}function bs(){return Io(this,arguments,-1)}function ws(){return Io(this,arguments,1)}function ys(o,t){return new this(o).mod(t)}function vs(o,t){return new this(o).mul(t)}function Ps(o,t){return new this(o).pow(t)}function Cs(o){var t,e,i,n,r=0,a=new this(1),l=[];if(o===void 0?o=this.precision:Bt(o,1,_e),i=Math.ceil(o/F),this.crypto)if(crypto.getRandomValues)for(t=crypto.getRandomValues(new Uint32Array(i));r<i;)n=t[r],n>=429e7?t[r]=crypto.getRandomValues(new Uint32Array(1))[0]:l[r++]=n%1e7;else if(crypto.randomBytes){for(t=crypto.randomBytes(i*=4);r<i;)n=t[r]+(t[r+1]<<8)+(t[r+2]<<16)+((t[r+3]&127)<<24),n>=214e7?crypto.randomBytes(4).copy(t,r):(l.push(n%1e7),r+=4);r=i/4}else throw Error(ko);else for(;r<i;)l[r++]=Math.random()*1e7|0;for(i=l[--r],o%=F,i&&o&&(n=It(10,F-o),l[r]=(i/n|0)*n);l[r]===0;r--)l.pop();if(r<0)e=0,l=[0];else{for(e=-1;l[0]===0;e-=F)l.shift();for(i=1,n=l[0];n>=10;n/=10)i++;i<F&&(e-=F-i)}return a.e=e,a.d=l,a}function Ms(o){return O(o=new this(o),o.e+1,this.rounding)}function ks(o){return o=new this(o),o.d?o.d[0]?o.s:0*o.s:o.s||NaN}function Ts(o){return new this(o).sin()}function As(o){return new this(o).sinh()}function Ss(o){return new this(o).sqrt()}function xs(o,t){return new this(o).sub(t)}function Is(){var o=0,t=arguments,e=new this(t[o]);for(B=!1;e.s&&++o<t.length;)e=e.plus(t[o]);return B=!0,O(e,this.precision,this.rounding)}function Es(o){return new this(o).tan()}function Os(o){return new this(o).tanh()}function Rs(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 Ve=k.constructor=Ro(sn);si=new Ve(si);ai=new Ve(ai);var I=Ve;function Ae(o){let t=Dt.isSuiCoin(o.coin_type_a),e=Dt.isSuiCoin(o.coin_type_b);return{is_adjust_coin_a:t,is_adjust_coin_b:e}}function Bs(o,t){return I.ceil(I(o).div(1+t)).toString()}var Ut=class{static createCollectRewarderAndFeeParams(t,e,i,n,r,a){r===void 0&&(r=[...n]),a===void 0&&(a=[...n]);let l=dn(i.coin_type_a),d=dn(i.coin_type_b);if(i.collect_fee){let h=Dt.buildCoinForAmount(e,r,BigInt(0),l,!1);r=h.remain_coins;let w=Dt.buildCoinForAmount(e,a,BigInt(0),d,!1);a=w.remain_coins,e=t.Position.createCollectFeePayload({pool_id:i.pool_id,pos_id:i.pos_id,coin_type_a:i.coin_type_a,coin_type_b:i.coin_type_b},e,h.target_coin,w.target_coin)}let f=[];return i.rewarder_coin_types.forEach(h=>{switch(dn(h)){case l:f.push(Dt.buildCoinForAmount(e,r,BigInt(0),h,!1).target_coin);break;case d:f.push(Dt.buildCoinForAmount(e,a,BigInt(0),h,!1).target_coin);break;default:f.push(Dt.buildCoinForAmount(e,n,BigInt(0),h,!1).target_coin);break}}),e=t.Rewarder.createCollectRewarderPayload(i,e,f),e}static async adjustTransactionForGas(t,e,i,n){n.setSender(t.getSenderAddress());let r=Dt.selectCoinAssetGreaterThanOrEqual(e,i).selected_coins,a=Dt.calculateTotalBalance(e);if(r.length===0&&R("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let l=await t.FullClient.calculationTxGas(un.from(n));if(Dt.selectCoinAssetGreaterThanOrEqual(e,BigInt(l),r.map(f=>f.coin_object_id)).selected_coins.length===0){let f=BigInt(l)+BigInt(500);if(a-i<f){i-=f,i<0&&R("InsufficientBalance","gas Insufficient balance");let h=new un;return{fixAmount:i,newTx:h}}}return{fixAmount:i}}static async buildAddLiquidityFixTokenForGas(t,e,i,n,r,a,l){r=await this.buildAddLiquidityFixToken(t,e,i,r,a,l);let{is_adjust_coin_a:d}=Ae(i),f=d?i.amount_a:i.amount_b,h=await this.adjustTransactionForGas(t,Dt.getCoinAssets(d?i.coin_type_a:i.coin_type_b,e),BigInt(f),r),{fixAmount:w}=h,P=h.newTx;if(P!=null){let M,T;if(d)i.amount_a=Number(w),M=this.buildAddLiquidityFixTokenCoinInput(P,!i.fix_amount_a,w.toString(),i.slippage,i.coin_type_a,e,!1,!0),T=this.buildAddLiquidityFixTokenCoinInput(P,i.fix_amount_a,i.amount_b,i.slippage,i.coin_type_b,e,!1,!0);else return i.amount_b=Number(w),M=this.buildAddLiquidityFixTokenCoinInput(P,!i.fix_amount_a,i.amount_a,i.slippage,i.coin_type_a,e,!1,!0),T=this.buildAddLiquidityFixTokenCoinInput(P,i.fix_amount_a,w.toString(),i.slippage,i.coin_type_b,e,!1,!0),i=this.fixAddLiquidityFixTokenParams(i,n.slippage,n.cur_sqrt_price),this.buildAddLiquidityFixTokenArgs(P,t,e,i,M,T)}return r}static async buildAddLiquidityFixToken(t,e,i,n,r,a){n=n||new un;let l,d;return r==null||a==null?(l=this.buildAddLiquidityFixTokenCoinInput(n,!i.fix_amount_a,i.amount_a,i.slippage,i.coin_type_a,e,!1,!0),d=this.buildAddLiquidityFixTokenCoinInput(n,i.fix_amount_a,i.amount_b,i.slippage,i.coin_type_b,e,!1,!0)):(l={target_coin:r,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},d={target_coin:a,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]}),n=this.buildAddLiquidityFixTokenArgs(n,t,e,i,l,d),n}static buildAddLiquidityFixTokenCoinInput(t,e,i,n,r,a,l=!0,d=!0){return e?Dt.buildCoinForAmountInterval(t,a,{amount_second:BigInt(Bs(i,n)),amount_first:BigInt(i)},r,l,d):Dt.buildCoinForAmount(t,a,BigInt(i),r,l,d)}static fixAddLiquidityFixTokenParams(t,e,i){let n=t.fix_amount_a?t.amount_a:t.amount_b,r=Fs.estLiquidityAndCoinAmountFromOneAmounts(Number(t.tick_lower),Number(t.tick_upper),new Bo.default(n),t.fix_amount_a,!0,e,i);return t.amount_a=t.fix_amount_a?t.amount_a:r.coin_amount_limit_a,t.amount_b=t.fix_amount_a?r.coin_amount_limit_b:t.amount_b,t}static buildAddLiquidityFixTokenArgs(t,e,i,n,r,a){let l=[n.coin_type_a,n.coin_type_b],d=n.is_open?"open_position_with_liquidity_by_fix_coin":"add_liquidity_by_fix_coin",{clmm_pool:f,integrate:h}=e.sdkOptions;n.is_open||(t=this.createCollectRewarderAndFeeParams(e,t,n,i,r.remain_coins,a.remain_coins));let w=Ns(f),P=n.is_open?[t.object(w.global_config_id),t.object(n.pool_id),t.pure.u32(Number(Fo(BigInt(n.tick_lower)).toString())),t.pure.u32(Number(Fo(BigInt(n.tick_upper)).toString())),r.target_coin,a.target_coin,t.pure.u64(n.amount_a),t.pure.u64(n.amount_b),t.pure.bool(n.fix_amount_a),t.object(No)]:[t.object(w.global_config_id),t.object(n.pool_id),t.object(n.pos_id),r.target_coin,a.target_coin,t.pure.u64(n.amount_a),t.pure.u64(n.amount_b),t.pure.bool(n.fix_amount_a),t.object(No)];return t.moveCall({target:`${h.published_at}::${Qt}::${d}`,typeArguments:l,arguments:P}),t}static checkCoinThreshold(t,e,i,n,r,a){e&&i.moveCall({target:`${t.sdkOptions.integrate.published_at}::${ni}::check_coin_threshold`,typeArguments:[a],arguments:[n,i.pure.u64(r)]})}};var te=class o{static getDefaultSqrtPriceLimit(t){return new qo.default(t?Ds:qs)}static getDefaultOtherAmountThreshold(t){return t?$s:Ls}static async buildSwapTransactionForGas(t,e,i,n){let r=this.buildSwapTransaction(t,e,i);r.setSender(t.getSenderAddress());let a=await this.adjustTransactionForGas(t,Lt.getCoinAssets(e.a2b?e.coin_type_a:e.coin_type_b,i),BigInt(e.by_amount_in?e.amount:e.amount_limit),r),{fixAmount:l,newTx:d}=a;if(d!==void 0){d.setSender(t.getSenderAddress()),e.by_amount_in?e.amount=l.toString():e.amount_limit=l.toString(),e=await this.fixSwapParams(t,e,n);let f=Lt.buildCoinForAmount(r,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a),h=Lt.buildCoinForAmount(r,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b);r=this.buildSwapTransactionArgs(d,e,t.sdkOptions,f,h)}return r}static buildSwapTransactionArgs(t,e,i,n,r){let{clmm_pool:a,integrate:l}=i,d=o.getDefaultSqrtPriceLimit(e.a2b),f=[e.coin_type_a,e.coin_type_b],{global_config_id:h}=jo(a);h===void 0&&R("InvalidConfig","clmm.config.global_config_id is undefined");let w=e.swap_partner!==void 0,P=w?e.a2b?"swap_a2b_with_partner":"swap_b2a_with_partner":e.a2b?"swap_a2b":"swap_b2a",M=w?[t.object(h),t.object(e.pool_id),t.object(e.swap_partner),n.target_coin,r.target_coin,t.pure.bool(e.by_amount_in),t.pure.u64(e.amount),t.pure.u64(e.amount_limit),t.pure.u128(d.toString()),t.object(fi)]:[t.object(h),t.object(e.pool_id),n.target_coin,r.target_coin,t.pure.bool(e.by_amount_in),t.pure.u64(e.amount),t.pure.u64(e.amount_limit),t.pure.u128(d.toString()),t.object(fi)];return t.moveCall({target:`${l.published_at}::${Qt}::${P}`,typeArguments:f,arguments:M}),t}static async adjustTransactionForGas(t,e,i,n){n.setSender(t.getSenderAddress());let r=Lt.selectCoinAssetGreaterThanOrEqual(e,i).selected_coins,a=Lt.calculateTotalBalance(e);if(r.length===0&&R("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let l=await t.FullClient.calculationTxGas(n);if(Lt.selectCoinAssetGreaterThanOrEqual(e,BigInt(l),r.map(f=>f.coin_object_id)).selected_coins.length===0){let f=BigInt(l)+BigInt(500);if(a-i<f){i-=f,i<0&&R("InsufficientBalance","gas Insufficient balance");let h=new fn;return{fixAmount:i,newTx:h}}}return{fixAmount:i}}static buildSwapTransaction(t,e,i){let n=new fn;n.setSender(t.getSenderAddress());let r=Lt.buildCoinForAmount(n,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1),a=Lt.buildCoinForAmount(n,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1);return n=this.buildSwapTransactionArgs(n,e,t.sdkOptions,r,a),n}static async fixSwapParams(t,e,i){let{current_pool:n}=i;try{let r=await t.Swap.preSwap({decimals_a:i.decimals_a,decimals_b:i.decimals_b,a2b:e.a2b,by_amount_in:e.by_amount_in,amount:e.amount,pool:n,current_sqrt_price:n.current_sqrt_price,coin_type_a:n.coin_type_a,coin_type_b:n.coin_type_b}),a=i.by_amount_in?r.estimated_amount_out:r.estimated_amount_in,l=js(a,i.slippage,!i.by_amount_in);e.amount_limit=l.toString()}catch(r){qt("InvalidConfig",r)}return e}static async buildSwapTransactionWithoutTransferCoinsForGas(t,e,i,n){let{tx:r,coin_ab_s:a}=o.buildSwapTransactionWithoutTransferCoins(t,e,i);r.setSender(t.getSenderAddress());let l=await o.adjustTransactionForGas(t,Lt.getCoinAssets(e.a2b?e.coin_type_a:e.coin_type_b,i),BigInt(e.by_amount_in?e.amount:e.amount_limit),r),{fixAmount:d,newTx:f}=l;if(f!==void 0){f.setSender(t.getSenderAddress()),e.by_amount_in?e.amount=d.toString():e.amount_limit=d.toString(),e=await o.fixSwapParams(t,e,n);let h=Lt.buildCoinForAmount(r,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1,!0),w=Lt.buildCoinForAmount(r,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1,!0),P=o.buildSwapTransactionWithoutTransferCoinArgs(t,f,e,t.sdkOptions,h,w);r=P.tx,a=P.txRes}return{tx:r,coin_ab_s:a}}static buildSwapTransactionWithoutTransferCoins(t,e,i){let n=new fn;n.setSender(t.getSenderAddress());let r=Lt.buildCoinForAmount(n,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1,!0),a=Lt.buildCoinForAmount(n,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1,!0),l=o.buildSwapTransactionWithoutTransferCoinArgs(t,n,e,t.sdkOptions,r,a);return{tx:l.tx,coin_ab_s:l.txRes}}static buildSwapTransactionWithoutTransferCoinArgs(t,e,i,n,r,a){let{clmm_pool:l,integrate:d}=n,f=o.getDefaultSqrtPriceLimit(i.a2b),{global_config_id:h}=jo(l);h===void 0&&R("InvalidConfig","clmm.config.global_config_id is undefined");let w=i.swap_partner!==void 0,P=w?"swap_with_partner":"swap",M=w?bo:ni,T=w?[e.object(h),e.object(i.pool_id),e.object(i.swap_partner),r.target_coin,a.target_coin,e.pure.bool(i.a2b),e.pure.bool(i.by_amount_in),e.pure.u64(i.amount),e.pure.u128(f.toString()),e.pure.bool(!1),e.object(fi)]:[e.object(h),e.object(i.pool_id),r.target_coin,a.target_coin,e.pure.bool(i.a2b),e.pure.bool(i.by_amount_in),e.pure.u64(i.amount),e.pure.u128(f.toString()),e.pure.bool(!1),e.object(fi)],S=[i.coin_type_a,i.coin_type_b],L=e.moveCall({target:`${d.published_at}::${M}::${P}`,typeArguments:S,arguments:T});if(i.by_amount_in){let j=i.a2b?i.coin_type_b:i.coin_type_a,V=i.a2b?L[1]:L[0],N=Number(i.amount_limit);Ut.checkCoinThreshold(t,i.by_amount_in,e,V,N,j)}return{tx:e,txRes:L}}};function lc(o,t,e){let i=t.mul(e).shln(64),n=t.shln(64).add(o.mul(e));return _n.divRoundUp(i,n)}function uc(o,t,e){let i=t.mul(e).shln(64),n=t.shln(64).sub(o.mul(e));return _n.divRoundUp(i,n)}function dc(o,t,e){return e.sub(_n.divRoundUp(o.shln(64),t))}function fc(o,t,e){return e.add(o.shln(64).div(t))}function gc(o){let t=new Ft.default(o);return t.lt(Vt)?{bits:t.neg().xor(new Ft.default(2).pow(new Ft.default(64)).sub(new Ft.default(1))).add(new Ft.default(1)).toString()}:{bits:t.toString()}}function Uo(o){return{coin_type_a:o.coin_type_a,coin_type_b:o.coin_type_b,current_sqrt_price:new Ft.default(o.current_sqrt_price),current_tick_index:o.current_tick_index,fee_growth_global_a:new Ft.default(o.fee_growth_global_a),fee_growth_global_b:new Ft.default(o.fee_growth_global_b),fee_protocol_coin_a:new Ft.default(o.fee_protocol_coin_a),fee_protocol_coin_b:new Ft.default(o.fee_protocol_coin_b),fee_rate:new Ft.default(o.fee_rate),liquidity:new Ft.default(o.liquidity),tick_indexes:[],tick_spacing:Number(o.tick_spacing),ticks:[],collection_name:""}}function Ks(o,t,e,i,n,r){if(e===Vt)return{amount_in:Vt,amount_out:Vt,next_sqrt_price:t,fee_amount:Vt};let a=o.gte(t),l,d,f,h;if(r){let w=me.checkMulDivFloor(i,me.checkUnsignedSub(_i,n),_i,64),P=$o(o,t,e,a);P.gt(w)?(l=w,h=me.checkUnsignedSub(i,w),f=Us(o,e,w,a)):(l=P,h=me.checkMulDivCeil(l,n,_i.sub(n),64),f=t),d=Lo(o,f,e,a)}else{let w=Lo(o,t,e,a);w.gt(i)?(d=i,f=Vs(o,e,i,a)):(d=w,f=t),l=$o(o,f,e,a),h=me.checkMulDivCeil(l,n,_i.sub(n),64)}return{amount_in:l,amount_out:d,next_sqrt_price:f,fee_amount:h}}function Vo(o,t,e,i,n){let r=e,a=i.liquidity,{current_sqrt_price:l}=i,d={amount_in:Vt,amount_out:Vt,fee_amount:Vt,ref_amount:Vt,next_sqrt_price:Vt,cross_tick_num:0},f,h,w=te.getDefaultSqrtPriceLimit(o);for(let P of n){if(o&&i.current_tick_index<P.index||!o&&i.current_tick_index>=P.index||P===null)continue;o&&w.gt(P.sqrt_price)||!o&&w.lt(P.sqrt_price)?f=w:f=P.sqrt_price;let M=Ks(l,f,a,r,i.fee_rate,t);if(M.amount_in.eq(Vt)||(r=t?r.sub(M.amount_in.add(M.fee_amount)):r.sub(M.amount_out)),d.amount_in=d.amount_in.add(M.amount_in),d.amount_out=d.amount_out.add(M.amount_out),d.fee_amount=d.fee_amount.add(M.fee_amount),M.next_sqrt_price.eq(P.sqrt_price)?(h=P.liquidity_net.mul(new Ft.default(-1)),o?me.is_neg(h)?a=a.add(new Ft.default(Do(BigInt(h.toString()),128))):a=a.add(h):me.is_neg(h)?a=a.sub(new Ft.default(Do(BigInt(h.toString()),128))):a=a.sub(h),l=P.sqrt_price):l=M.next_sqrt_price,d.cross_tick_num+=1,r.eq(Vt))break}return d.amount_in=d.amount_in.add(d.fee_amount),d.next_sqrt_price=l,d}var Kt=re(Gt());import{asIntN as ge,buildNFT as Hs,d as Ke,DETAILS_KEYS as hi,extractStructTagFromType as Se,getMoveObjectType as Ko,getObjectDeletedResponse as Ws,getObjectFields as pn,getObjectId as hn,getObjectNotExistsResponse as Zs,getObjectOwner as zs,MathUtil as Gs}from"@cetusprotocol/common-sdk";function Qs(o,t,e){let i=Se(o).name,n=Se(t).name;return`${i}-${n}[${e}]`}function pi(o){let t=Ko(o),e=Se(t),i=pn(o);i==null&&R("InvalidPoolObject",`Pool id ${hn(o)} not exists.`,{[hi.METHOD_NAME]:"buildPool"});let n=[];i.rewarder_manager.fields.rewarders.forEach(d=>{let{emissions_per_second:f}=d.fields,h=Gs.fromX64(new Kt.default(f)),w=Math.floor(h.toNumber()*60*60*24);n.push({emissions_per_second:f,coin_type:Se(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:hn(o),pool_type:t,coin_type_a:e.type_arguments[0],coin_type_b:e.type_arguments[1],coin_amount_a:i.coin_a,coin_amount_b:i.coin_b,current_sqrt_price:i.current_sqrt_price,current_tick_index:ge(BigInt(i.current_tick_index.fields.bits)),fee_growth_global_a:i.fee_growth_global_a,fee_growth_global_b:i.fee_growth_global_b,fee_protocol_coin_a:i.fee_protocol_coin_a,fee_protocol_coin_b:i.fee_protocol_coin_b,fee_rate:i.fee_rate,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=Qs(l.coin_type_a,l.coin_type_b,l.tick_spacing),l}function mi(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&R("InvalidPositionObject",`Position not exists. Get Position error:${o.error}`,{[hi.METHOD_NAME]:"buildPosition"});let t={creator:"",description:"",image_url:"",link:"",name:"",project_url:""},e={...t,pos_object_id:"",owner:"",type:"",coin_type_a:"",coin_type_b:"",liquidity:"",tick_lower_index:0,tick_upper_index:0,index:0,pool:"",reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0",fee_growth_inside_a:"0",fee_owned_a:"0",fee_growth_inside_b:"0",fee_owned_b:"0",position_status:"Exists"},i=pn(o);if(i){let a=Ko(o),l=zs(o);"nft"in i?(i=i.nft.fields,t.description=i.description,t.name=i.name,t.link=i.url):t=Hs(o),e={...t,pos_object_id:i.id.id,owner:l.AddressOwner,type:a,liquidity:i.liquidity,coin_type_a:i.coin_type_a.fields.name,coin_type_b:i.coin_type_b.fields.name,tick_lower_index:ge(BigInt(i.tick_lower_index.fields.bits)),tick_upper_index:ge(BigInt(i.tick_upper_index.fields.bits)),index:i.index,pool:i.pool,reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0",fee_growth_inside_a:"0",fee_owned_a:"0",fee_growth_inside_b:"0",fee_owned_b:"0",position_status:"Exists"}}let n=Ws(o);n&&(e.pos_object_id=n.objectId,e.position_status="Deleted");let r=Zs(o);return r&&(e.pos_object_id=r,e.position_status="NotExists"),e}function He(o){let t={reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0"};o="fields"in o?o.fields:o,o.rewards.forEach((r,a)=>{let{amount_owned:l,growth_inside:d}="fields"in r?r.fields:r;a===0?(t.reward_amount_owned_0=l,t.reward_growth_inside_0=d):a===1?(t.reward_amount_owned_1=l,t.reward_growth_inside_1=d):a===2&&(t.reward_amount_owned_2=l,t.reward_growth_inside_2=d)});let e="fields"in o.tick_lower_index?o.tick_lower_index.fields.bits:o.tick_lower_index.bits,i="fields"in o.tick_upper_index?o.tick_upper_index.fields.bits:o.tick_upper_index.bits;return{liquidity:o.liquidity,tick_lower_index:ge(BigInt(e)),tick_upper_index:ge(BigInt(i)),...t,fee_growth_inside_a:o.fee_growth_inside_a,fee_owned_a:o.fee_owned_a,fee_growth_inside_b:o.fee_growth_inside_b,fee_owned_b:o.fee_owned_b,pos_object_id:o.position_id}}function gi(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&R("InvalidTickObject",`Tick not exists. Get tick data error:${o.error}`,{[hi.METHOD_NAME]:"buildTickData"});let e=pn(o).value.fields.value.fields;return{object_id:hn(o),index:ge(BigInt(e.index.fields.bits)),sqrt_price:new Kt.default(e.sqrt_price),liquidity_net:new Kt.default(e.liquidity_net.fields.bits),liquidity_gross:new Kt.default(e.liquidity_gross),fee_growth_outside_a:new Kt.default(e.fee_growth_outside_a),fee_growth_outside_b:new Kt.default(e.fee_growth_outside_b),rewarders_growth_outside:e.rewards_growth_outside}}function Ho(o){(!o||!o.index||!o.sqrt_price||!o.liquidity_net||!o.liquidity_gross||!o.fee_growth_outside_a||!o.fee_growth_outside_b)&&R("InvalidTickFields","Invalid tick fields.",{[hi.METHOD_NAME]:"buildTickDataByEvent"});let t=ge(BigInt(o.index.bits)),e=new Kt.default(o.sqrt_price),i=new Kt.default(o.liquidity_net.bits),n=new Kt.default(o.liquidity_gross),r=new Kt.default(o.fee_growth_outside_a),a=new Kt.default(o.fee_growth_outside_b),l=o.rewards_growth_outside||[];return{object_id:"",index:t,sqrt_price:e,liquidity_net:i,liquidity_gross:n,fee_growth_outside_a:r,fee_growth_outside_b:a,rewarders_growth_outside:l}}function Sc(o,t){return`Cetus LP | Pool${o}-${t}`}function Wo(o,t,e){let i=[],{timestampMs:n,events:r}=o,a=["AddLiquidityEvent","RemoveLiquidityEvent","CollectFeeEvent","CollectRewardEvent","CollectRewardV2Event","HarvestEvent","AddLiquidityV2Event","RemoveLiquidityV2Event"];return r?.forEach((l,d)=>{let f=Se(l.type).name;if(a.includes(f)){let h={tx_digest:l.id.txDigest,package_id:l.packageId,transaction_module:l.transactionModule,sender:l.sender,type:l.type,timestamp_ms:n||"0",parsed_json:l.parsedJson,index:`${t}_${d}`};switch(f){case"CollectFeeEvent":e.includes(h.parsed_json.position)&&(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":(e.includes(h.parsed_json.position)||e.includes(h.parsed_json.wrapped_position_id))&&Ke(h.parsed_json.amount).gt(0)&&i.push(h);break;default:break}}}),i}function Zo(o,t,e,i){let n=[],{timestampMs:r,events:a}=o;return a?.forEach((l,d)=>{let{name:f,address:h}=Se(l.type);if(mo.includes(f)&&e.includes(h)&&i===l.parsedJson.pool){let w={tx:l.id.txDigest,sender:l.sender,type:l.type,block_time:r||"0",index:`${t}_${d}`,parsed_json:l.parsedJson};n.push(w)}}),n}function bi(o,t,e,i){t.moveCall({target:`${o.sdkOptions.integrate.published_at}::${wo}::transfer_coin_to_sender`,typeArguments:[i],arguments:[e]})}function zo(o,t,e,i,n){n!=null?t.transferObjects([e],t.pure.address(n)):bi(o,t,e,i)}import{asUintN as Ie,CACHE_TIME_24H as wi,ClmmPoolUtil as na,CLOCK_ADDRESS as nr,CoinAssist as yi,createFullClient as oa,d as Ee,DETAILS_KEYS as Z,extractStructTagFromType as Oe,getObjectFields as ra,getObjectPreviousTransactionDigest as sa,getPackagerConfigs as gn,isSortedSymbols as or,TickMath as bn,tickScore as aa,TickUtil as rr}from"@cetusprotocol/common-sdk";import{asIntN as Go,d as Qo,extractStructTagFromType as Js,fixCoinType as Jo,getObjectFields as mn,getObjectType as Xs}from"@cetusprotocol/common-sdk";var Ys=1e4,ee=class{static parseClmmVestInfo(t){let e=mn(t),i=Xs(t),n=Js(i),r=e.global_vesting_periods.map(l=>({period:l.fields.period,release_time:l.fields.release_time,redeemed_amount:l.fields.redeemed_amount,percentage:Qo(l.fields.percentage).div(Ys).toNumber()}));return{id:e.id.id,balance:e.balance,global_vesting_periods:r,total_value:e.total_value,total_cetus_amount:e.total_cetus_amount,redeemed_amount:e.redeemed_amount,start_time:e.start_time,type:n.full_address,positions:{id:e.positions.fields.id,size:e.positions.fields.size}}}static parsePositionVesting(t){return{position_id:t.position_id,cetus_amount:t.cetus_amount,redeemed_amount:t.redeemed_amount,is_paused:t.is_paused,impaired_a:t.impaired_a,impaired_b:t.impaired_b,period_details:t.period_details,coin_type_a:Jo(t.coin_a.name,!1),coin_type_b:Jo(t.coin_b.name,!1)}}static parsePoolLiquiditySnapshot(t){let e=mn(t);return{current_sqrt_price:e.current_sqrt_price,remove_percent:Qo(e.remove_percent).div(1e6).toString(),snapshots:{id:e.snapshots.fields.id.id,size:e.snapshots.fields.size}}}static parsePositionSnapshot(t){let e=mn(t),i=e.value.fields.value.fields;return{position_id:e.name,liquidity:i.liquidity,tick_lower_index:Go(BigInt(i.tick_lower_index.fields.bits)),tick_upper_index:Go(BigInt(i.tick_upper_index.fields.bits)),fee_owned_a:i.fee_owned_a,fee_owned_b:i.fee_owned_b,value_cut:i.value_cut,rewards:i.rewards}}};import{FullRpcUrlMainnet as ta}from"@cetusprotocol/common-sdk";var Xo={clmmConfig:{pools_id:"0xf699e7f2276f5c9a75944b37a0c5b5d9ddfd2471bf6242483b03ab2887d198d0",global_config_id:"0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f",global_vault_id:"0xce7bceef26d3ad1f6d9b6f13a953f053e6ed3ca77907516481ce99ae8e588f2b",admin_cap_id:"0x89c1a321291d15ddae5a086c9abc533dff697fde3d89e0ca836c41af73e36a75"},cetusConfig:{coin_list_id:"0x8cbc11d9e10140db3d230f50b4d30e9b721201c0083615441707ffec1ef77b23",launchpad_pools_id:"0x1098fac992eab3a0ab7acf15bb654fc1cf29b5a6142c4ef1058e6c408dd15115",clmm_pools_id:"0x15b6a27dd9ae03eb455aba03b39e29aad74abd3757b8e18c0755651b2ae5b71e",admin_cap_id:"0x39d78781750e193ce35c45ff32c6c0c3f2941fa3ddaf8595c90c555589ddb113",global_config_id:"0x0408fa4e4a4c03cc0de8f23d0c2bbfe8913d178713c9a271ed4080973fe42d8f",coin_list_handle:"0x49136005e90e28c4695419ed4194cc240603f1ea8eb84e62275eaff088a71063",launchpad_pools_handle:"0x5e194a8efcf653830daf85a85b52e3ae8f65dc39481d54b2382acda25068375c",clmm_pools_handle:"0x37f60eb2d9d227949b95da8fea810db3c32d1e1fa8ed87434fc51664f87d83cb"}},Yo={env:"mainnet",full_rpc_url:ta,cetus_config:{package_id:"0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f",published_at:"0xba7e740c3c002673dbe69ad5fbdb0691ec260170e141297cefb982e7081fde52",version:2,config:Xo.cetusConfig},clmm_pool:{package_id:"0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb",published_at:"0xdb5cd62a06c79695bfc9982eb08534706d3752fe123b48e0144f480209b3117f",version:13,config:Xo.clmmConfig},integrate:{package_id:"0x996c4d9480708fb8b92aa7acf819fb0497b5ec8e65ba06601cae2fb6db3312c3",published_at:"0x6219e52e9a9d2cce4d0e53f27a9152a827f3572678e5bdec85f528e3e175e479",version:14},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"}}},tr=["0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb","0xdb5cd62a06c79695bfc9982eb08534706d3752fe123b48e0144f480209b3117f"];import{FullRpcUrlTestnet as ea}from"@cetusprotocol/common-sdk";var er={env:"testnet",full_rpc_url:ea,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:"0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8",config:{pools_id:"0x20a086e6fa0741b3ca77d033a65faf0871349b986ddbdde6fa1d85d78a5f4222",global_config_id:"0xc6273f844b4bc258952c4e477697aa12c918c8e08106fac6b934811298c9820a",global_vault_id:"0x71e74a999dd7959e483f758ddf573e85fa4c24944db33ff6763c9d85a9c045fe",admin_cap_id:"0xbf4c48590f403c38351de0e8aa13d6d91bf78fd8c04e93ac1d0269c44d70ae02",partners_id:"0xb5ae5ed3f403654ae1307aadc0140f746db41efb7bda92235257c84d90a1397e"}},integrate:{package_id:"0x36187418dd79415d50e2e5903f9b3caca582052005f062959c86da64e82107a9",published_at:"0x36187418dd79415d50e2e5903f9b3caca582052005f062959c86da64e82107a9",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"}}},ir=["0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8","0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8"];var vi=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}async getPositionList(t,e="all"){let i={data:[],has_next_page:!0},n=await this._sdk.FullClient.getDynamicFieldsByPage(t,e);i.has_next_page=n.has_next_page,i.next_cursor=n.next_cursor;let r=n.data.map(l=>(l.error!=null&&R("InvalidConfig",`when getPositionList get position objects error: ${l.error}, please check the rpc, contracts address config and position id.`,{[Z.METHOD_NAME]:"getPositionList"}),l.name.value)),a=await this._sdk.Position.getSimplePositionList(r);return i.data=a,i}async getPoolImmutablesWithPage(t="all",e=!1){let{package_id:i}=this._sdk.sdkOptions.clmm_pool,n=[],r={data:[],has_next_page:!1},a=t==="all",l=`${i}_getPoolImmutables`;if(a){let d=this._sdk.getCache(l,e);d&&n.push(...d)}if(n.length===0)try{let d=`${i}::factory::CreatePoolEvent`,f=await this._sdk.FullClient.queryEventsByPage({MoveEventType:d},t);r.has_next_page=f.has_next_page,r.next_cursor=f.next_cursor,f.data.forEach(h=>{let w=h.parsedJson;w&&n.push({id:w.pool_id,tick_spacing:w.tick_spacing,coin_type_a:Oe(w.coin_type_a).full_address,coin_type_b:Oe(w.coin_type_b).full_address})})}catch(d){return qt("FetchError",d,{[Z.METHOD_NAME]:"getPoolImmutables"})}return r.data=n,a&&this._sdk.updateCache(`${i}_getPoolImmutables`,n,wi),r}async getPoolsWithPage(t="all",e=!1){let i={data:[],has_next_page:!1},n=await this.getPoolImmutablesWithPage(t,e),r=await this._sdk.FullClient.batchGetObjects(n.data.map(a=>a.id),{showContent:!0,showType:!0});for(let a of r){(a.error!=null||a.data?.content?.dataType!=="moveObject")&&R("InvalidPoolObject",`getPoolWithPages error code: ${a.error?.code??"unknown error"}, please check config and object ids`,{[Z.METHOD_NAME]:"getPoolsWithPage"});let l=pi(a);i.data.push(l);let d=`${l.id}_getPoolObject`;this._sdk.updateCache(d,l,wi)}return i.has_next_page=n.has_next_page,i.next_cursor=n.next_cursor,i}async getPoolLiquiditySnapshot(t,e=!1){try{let i=await this._sdk.FullClient.getDynamicFieldObject({parentId:t,name:{type:"0x1::string::String",value:ho}}),n=ee.parsePoolLiquiditySnapshot(i);if(e){let a=(await this._sdk.FullClient.getDynamicFieldsByPage(n.snapshots.id)).data.map(l=>l.objectId);if(a.length>0){let l=await this._sdk.FullClient.batchGetObjects(a,{showContent:!0,showType:!0}),d=[];l.forEach(f=>{let h=ee.parsePositionSnapshot(f);d.push(h)}),n.position_snapshots=d}}return n}catch(i){return qt("InvalidPoolObject",i,{[Z.METHOD_NAME]:"getPoolLiquiditySnapshot",[Z.REQUEST_PARAMS]:t})}}async getPositionSnapshot(t,e){let i=await this._sdk.FullClient.getDynamicFieldObjects(t,e,"0x2::object::ID","address"),n=[];return i.forEach(r=>{try{let a=ee.parsePositionSnapshot(r);n.push(a)}catch(a){console.log("getPositionSnapshot error: ",a)}}),n}async getAssignPools(t){if(t.length===0)return[];let e=[],i=await this._sdk.FullClient.batchGetObjects(t,{showContent:!0,showType:!0});for(let n of i){(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidPoolObject",`getPools error code: ${n.error?.code??"unknown error"}, please check config and object ids`,{[Z.METHOD_NAME]:"getAssignPools"});let r=pi(n);e.push(r);let a=`${r.id}_getPoolObject`;this._sdk.updateCache(a,r,wi)}return e}async getPool(t,e=!0,i=!1){let n=`${t}_getPoolObject`,r=this._sdk.getCache(n,e);if(r!==void 0)return r;let a=await this._sdk.FullClient.getObject({id:t,options:{showType:!0,showContent:!0}});(a.error!=null||a.data?.content?.dataType!=="moveObject")&&R("InvalidPoolObject",`getPool error code: ${a.error?.code??"unknown error"}, please check config and object id`,{[Z.METHOD_NAME]:"getPool"});let l=pi(a);if(i){let d=await this.getPoolStatus(t);d&&(l.pool_status=d)}return this._sdk.updateCache(n,l),l}async getPoolByCoins(t,e){if(t.length===0)return[];for(let d=0;d<t.length;d++)t[d]==="0x2::sui::SUI"&&(t[d]="0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI");let i=this._sdk.sdkOptions.stats_pools_url;i||R("StatsPoolsUrlNotSet","statsPoolsUrl is not set in the sdk options.",{[Z.METHOD_NAME]:"getPoolByCoins"}),i+=`?order_by=-fees&limit=100&has_mining=true&has_farming=true&no_incentives=true&display_all_pools=true&coin_type=${t.join(",")}`;let n=await fetch(i),r;try{r=await n.json()}catch(d){qt("FetchError",d,{[Z.METHOD_NAME]:"getPoolByCoins",[Z.REQUEST_PARAMS]:i})}let a=r.data.lp_list,l=[];for(let d of a)t.includes(d.coin_a_address)&&t.includes(d.coin_b_address)&&(e!=null?d.object.feeRate===e&&l.push(d.address):l.push(d.address));return l.length>0?await this.getAssignPools(l):[]}async createPoolPayload(t,e){if(or(xe(t.coin_type_a),xe(t.coin_type_b))){let i=t.coin_type_b;t.coin_type_b=t.coin_type_a,t.coin_type_a=i;let n=t.metadata_b;t.metadata_b=t.metadata_a,t.metadata_a=n}return await this.createPoolAndAddLiquidity(t,e)}async createPoolRowPayload(t,e){if(or(xe(t.coin_type_a),xe(t.coin_type_b))){let i=t.coin_type_b;t.coin_type_b=t.coin_type_a,t.coin_type_a=i;let n=t.metadata_b;t.metadata_b=t.metadata_a,t.metadata_a=n}return await this.createPoolAndAddLiquidityRow(t,e)}async getClmmConfigs(t=!1){let{package_id:e}=this._sdk.sdkOptions.clmm_pool,i=`${e}_getInitEvent`,n=this._sdk.getCache(i,t);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:e,options:{showPreviousTransaction:!0}}),a=sa(r),l=(await this._sdk.FullClient.queryEventsByPage({Transaction:a})).data,d={pools_id:"",global_config_id:"",global_vault_id:"",admin_cap_id:""};return l.length>0&&(l.forEach(f=>{let h=f.parsedJson;if(f.type)switch(Oe(f.type).full_address){case`${e}::config::InitConfigEvent`:d.global_config_id=h.global_config_id,d.admin_cap_id=h.admin_cap_id;break;case`${e}::factory::InitFactoryEvent`:d.pools_id=h.pools_id;break;case`${e}::rewarder::RewarderInitEvent`:d.global_vault_id=h.global_vault_id;break;case`${e}::partner::InitPartnerEvent`:d.partners_id=h.partners_id;break;default:break}}),this._sdk.updateCache(i,d,wi)),d}async getPoolTransactionList({pool_id:t,pagination_args:e,order:i="descending",full_rpc_url:n}){let{FullClient:r,sdkOptions:a}=this._sdk,l;n?l=oa(new ia({url:n})):l=r;let d={data:[],has_next_page:!1},f=50,h=e,w=e.limit||10,P=a.env==="testnet"?ir:tr;do{let M=await l.queryTransactionBlocksByPage({ChangedObject:t},{...h,limit:50},i);M.data.forEach((T,S)=>{d.next_cursor=M.next_cursor;let L=Zo(T,S,P,t);d.data=[...d.data,...L]}),d.has_next_page=M.has_next_page,d.next_cursor=M.next_cursor,h.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(t){let{current_price:e,slippage:i,tick_spacing:n,add_mode_params:r,price_base_coin:a,coin_decimals_a:l,coin_decimals_b:d}=t,f=bn.priceToSqrtPriceX64(a==="coin_a"?Ee(e):Ee(1).div(e),l,d),h=0,w=0;if(r.is_full_range)h=rr.getMinIndex(n),w=rr.getMaxIndex(n);else{let{min_price:N,max_price:D}=r;h=bn.priceToInitializeTickIndex(a==="coin_a"?Ee(N):Ee(1).div(D),l,d,n),w=bn.priceToInitializeTickIndex(a==="coin_a"?Ee(D):Ee(1).div(N),l,d,n)}let{coin_amount:P,fix_amount_a:M}=t,{coin_amount_limit_a:T,coin_amount_limit_b:S,liquidity_amount:L,coin_amount_a:j,coin_amount_b:V}=na.estLiquidityAndCoinAmountFromOneAmounts(h,w,new sr.default(P),M,!0,i,f);return{coin_amount_a:j,coin_amount_b:V,coin_amount_limit_a:T,coin_amount_limit_b:S,liquidity:L,initialize_sqrt_price:f.toString(),tick_lower:h,tick_upper:w,fix_amount_a:M}}async createPoolAndAddLiquidity(t,e){e=e||new We,e.setSender(this.sdk.getSenderAddress());let{integrate:i,clmm_pool:n}=this.sdk.sdkOptions,r=gn(n),a=r.global_config_id,l=r.pools_id,d=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress()),f=yi.buildCoinForAmount(e,d,BigInt(t.amount_a),t.coin_type_a,!1,!0),h=yi.buildCoinForAmount(e,d,BigInt(t.amount_b),t.coin_type_b,!1,!0),w=[e.object(a),e.object(l),e.pure.u32(t.tick_spacing),e.pure.u128(t.initialize_sqrt_price),e.pure.string(t.uri),e.pure.u32(Number(Ie(BigInt(t.tick_lower)).toString())),e.pure.u32(Number(Ie(BigInt(t.tick_upper)).toString())),f.target_coin,h.target_coin,e.object(t.metadata_a),e.object(t.metadata_b),e.pure.bool(t.fix_amount_a),e.object(nr)];return e.moveCall({target:`${i.published_at}::pool_creator_v2::create_pool_v2`,typeArguments:[t.coin_type_a,t.coin_type_b],arguments:w}),bi(this._sdk,e,f.target_coin,t.coin_type_a),bi(this._sdk,e,h.target_coin,t.coin_type_b),e}async createPoolAndAddLiquidityRow(t,e){e=e||new We;let{clmm_pool:i}=this.sdk.sdkOptions,n=gn(i),r=n.global_config_id,a=n.pools_id,l=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress()),d=yi.buildCoinForAmount(e,l,BigInt(t.amount_a),t.coin_type_a,!1,!0),f=yi.buildCoinForAmount(e,l,BigInt(t.amount_b),t.coin_type_b,!1,!0),h=[e.object(r),e.object(a),e.pure.u32(t.tick_spacing),e.pure.u128(t.initialize_sqrt_price),e.pure.string(t.uri),e.pure.u32(Number(Ie(BigInt(t.tick_lower)).toString())),e.pure.u32(Number(Ie(BigInt(t.tick_upper)).toString())),d.target_coin,f.target_coin,e.object(t.metadata_a),e.object(t.metadata_b),e.pure.bool(t.fix_amount_a),e.object(nr)],w=e.moveCall({target:`${i.published_at}::pool_creator::create_pool_v2`,typeArguments:[t.coin_type_a,t.coin_type_b],arguments:h});return{tx:e,pos_id:w[0],remain_coin_a:w[1],remain_coin_b:w[2],remain_coin_type_a:t.coin_type_a,remain_coin_type_b:t.coin_type_b}}async createPoolWithPriceReturnPositionPayload(t,e){let{coin_type_a:i,coin_type_b:n,tick_spacing:r,uri:a,calculate_result:l}=t,{initialize_sqrt_price:d,tick_lower:f,tick_upper:h,liquidity:w,coin_amount_a:P,coin_amount_b:M,fix_amount_a:T,coin_amount_limit_a:S,coin_amount_limit_b:L}=l,j=await this._sdk.FullClient.fetchCoinMetadata(i),V=await this._sdk.FullClient.fetchCoinMetadata(n);if(j===null)return R("FetchError",`fetch coin ${i} metadata failed`,{[Z.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[Z.REQUEST_PARAMS]:t});if(V===null)return R("FetchError",`fetch coin ${n} metadata failed`,{[Z.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[Z.REQUEST_PARAMS]:t});let N=j.id,D=V.id;return this.createPoolRowPayload({tick_spacing:r,initialize_sqrt_price:d,uri:a||"",coin_type_a:i,coin_type_b:n,amount_a:T?P:S,amount_b:T?L:M,fix_amount_a:T,tick_lower:f,tick_upper:h,metadata_a:N,metadata_b:D},e)}async createPoolWithPricePayload(t){let{coin_type_a:e,coin_type_b:i,tick_spacing:n,uri:r,calculate_result:a}=t,{initialize_sqrt_price:l,tick_lower:d,tick_upper:f,liquidity:h,coin_amount_a:w,coin_amount_b:P,fix_amount_a:M,coin_amount_limit_a:T,coin_amount_limit_b:S}=a,L=await this._sdk.FullClient.fetchCoinMetadata(e),j=await this._sdk.FullClient.fetchCoinMetadata(i);if(L===null)return R("FetchError",`fetch coin ${e} metadata failed`,{[Z.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[Z.REQUEST_PARAMS]:t});if(j===null)return R("FetchError",`fetch coin ${i} metadata failed`,{[Z.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[Z.REQUEST_PARAMS]:t});let V=L.id,N=j.id;return this.createPoolPayload({tick_spacing:n,initialize_sqrt_price:l,uri:r||"",coin_type_a:e,coin_type_b:i,amount_a:M?w:T,amount_b:M?S:P,fix_amount_a:M,tick_lower:d,tick_upper:f,metadata_a:V,metadata_b:N})}async fetchTicks(t){let e=[],i=[];for(;;){let r=await this.getTicks({pool_id:t.pool_id,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b,start:i,limit:512});if(e=[...e,...r],r.length<512)break;i=[Number(Ie(BigInt(r[r.length-1].index)))]}return e}async getTicks(t){let{integrate:e}=this.sdk.sdkOptions,i=[],n=[t.coin_type_a,t.coin_type_b],r=new We,a=r.makeMoveVec({elements:t.start.map(f=>r.pure.u32(f)),type:"u32"}),l=[r.object(t.pool_id),a,r.pure.u64(t.limit.toString())];r.moveCall({target:`${e.published_at}::${Jt}::fetch_ticks`,arguments:l,typeArguments:n});let d=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:r,sender:xe("0x0")});return d.error!=null&&R("InvalidTickObjectId",`getTicks error code: ${d.error??"unknown error"}, please check config and tick object ids`,{[Z.METHOD_NAME]:"getTicks",[Z.REQUEST_PARAMS]:t}),d.events?.forEach(f=>{Oe(f.type).name==="FetchTicksResultEvent"&&f.parsedJson.ticks.forEach(h=>{i.push(Ho(h))})}),i}async fetchPoolPositionInfoList(t){let{integrate:e}=this.sdk.sdkOptions,i=[],n=[],r=512;for(;;){let a=[t.coin_type_a,t.coin_type_b],l=new We,d=l.pure.vector("id",n.map(P=>P)),f=[l.object(t.pool_id),d,l.pure.u64(r)];l.moveCall({target:`${e.published_at}::${Jt}::fetch_positions`,arguments:f,typeArguments:a});let h=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:l,sender:xe("0x0")});h.error!=null&&R("InvalidPositionRewardObject",`fetch position info error code: ${h.error??"unknown error"}, please check config and tick object ids`,{[Z.METHOD_NAME]:"fetchPoolPositionInfoList",[Z.REQUEST_PARAMS]:t});let w=[];if(h?.events?.forEach(P=>{Oe(P.type).name==="FetchPositionsEvent"&&P.parsedJson.positions.forEach(M=>{let T=He(M);w.push(T)})}),i.push(...w),w.length<r)break;n=[w[w.length-1].pos_object_id]}return i}async fetchTicksByRpc(t){let e=[],i=null,n=50;for(;;){let r=[],a=await this.sdk.FullClient.getDynamicFields({parentId:t,cursor:i,limit:n});if(i=a.nextCursor,a.data.forEach(l=>{Oe(l.objectType).module==="skip_list"&&r.push(l.objectId)}),e=[...e,...await this.getTicksByRpc(r)],!a.hasNextPage)break}return e}async getTicksByRpc(t){let e=[],i=await this.sdk.FullClient.batchGetObjects(t,{showContent:!0,showType:!0});for(let n of i){(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidTickObjectId",`getTicksByRpc error code: ${n.error?.code??"unknown error"}, please check config and tick object ids`,{[Z.METHOD_NAME]:"getTicksByRpc"});let r=gi(n);r!=null&&e.push(r)}return e}async getTickDataByIndex(t,e){let i={type:"u64",value:Ie(BigInt(aa(e).toString())).toString()},n=await this.sdk.FullClient.getDynamicFieldObject({parentId:t,name:i});return(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidTickIndex",`get tick by index: ${e} error: ${n.error}`,{[Z.METHOD_NAME]:"getTickDataByIndex"}),gi(n)}async getTickDataByObjectId(t){let e=await this.sdk.FullClient.getObject({id:t,options:{showContent:!0}});return(e.error!=null||e.data?.content?.dataType!=="moveObject")&&R("InvalidTickObjectId",`getTicksByRpc error code: ${e.error?.code??"unknown error"}, please check config and tick object ids`,{[Z.METHOD_NAME]:"getTickDataByObjectId"}),gi(e)}async getPartnerRefFeeAmount(t,e=!0){let i=await this._sdk.FullClient.batchGetObjects([t],{showOwner:!0,showContent:!0,showDisplay:e,showType:!0});i[0].data?.content?.dataType!=="moveObject"&&R("NotFoundPartnerObject",`get partner by object id: ${t} error: ${i[0].error}`,{[Z.METHOD_NAME]:"getPartnerRefFeeAmount"});let n=i[0].data.content.fields.balances,r=await this._sdk.FullClient.getDynamicFieldsByPage(n.fields.id.id),a=[];r.data.forEach(f=>{f.objectId!=null&&a.push(f.objectId)});let l=[];return(await this._sdk.FullClient.batchGetObjects(a,{showOwner:!0,showContent:!0,showDisplay:e,showType:!0})).forEach(f=>{(f.error!=null||f.data?.content?.dataType!=="moveObject")&&R("InvalidPartnerRefFeeFields",`get coin by object id: ${f.data.objectId} error: ${f.error}`,{[Z.METHOD_NAME]:"getPartnerRefFeeAmount"});let h={coin_type:f.data.content.fields.name,coin_object_id:f.data.objectId,balance:BigInt(f.data.content.fields.value)};l.push(h)}),l}async claimPartnerRefFeePayload(t,e,i){let n=new We,{clmm_pool:r}=this.sdk.sdkOptions,{global_config_id:a}=gn(r),l=[i],d=[n.object(a),n.object(t),n.object(e)];return n.moveCall({target:`${r.published_at}::${go}::claim_ref_fee`,arguments:d,typeArguments:l}),n}async getPoolStatus(t){try{let e=await this._sdk.FullClient.getDynamicFieldObject({parentId:t,name:{type:"0x1::string::String",value:"pool_status"}}),n=ra(e).position.fields.status;if(n)return n}catch(e){console.log("\u{1F680} ~ file: poolModule.ts:1093 ~ PoolModule ~ getPoolStatus ~ error:",e)}}};var Ze=re(Gt());import{SuiClient as ca}from"@mysten/sui/client";import{Transaction as ae}from"@mysten/sui/transactions";import{isValidSuiObjectId as la,normalizeSuiAddress as ua}from"@mysten/sui/utils";import{asUintN as Re,ClmmPoolUtil as ar,CLOCK_ADDRESS as Pi,CoinAssist as Ci,d as ie,DETAILS_KEYS as he,extractStructTagFromType as da,getObjectFields as cr,getPackagerConfigs as ce,TickMath as Fe,TickUtil as Mi,createFullClient as fa,deriveDynamicFieldIdByType as _a}from"@cetusprotocol/common-sdk";var ki=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}buildPositionType(){return`${this._sdk.sdkOptions.clmm_pool.package_id}::position::Position`}async getPositionTransactionList({pos_id:t,origin_pos_id:e,full_rpc_url:i,pagination_args:n="all",order:r="ascending"}){let{FullClient:a}=this._sdk,l=[t];e&&l.push(e);let d;i?d=fa(new ca({url:i})):d=a;let f={data:[],has_next_page:!1};try{let h=await d.queryTransactionBlocksByPage({ChangedObject:t},n,r);return h.data.forEach((w,P)=>{let M=Wo(w,P,l);f.data=[...f.data,...M]}),f.has_next_page=h.has_next_page,f.next_cursor=h.next_cursor,f}catch(h){qt("FetchError",h,{[he.METHOD_NAME]:"getPositionTransactionList"})}return f}async getPositionList(t,e=[],i=!0){let n=[],r=await this._sdk.FullClient.getOwnedObjectsByPage(t,{options:{showType:!0,showContent:!0,showDisplay:i,showOwner:!0},filter:{Package:this._sdk.sdkOptions.clmm_pool.package_id}}),a=e.length>0;for(let l of r.data)if(da(l.data.type).full_address===this.buildPositionType()){let f=mi(l),h=`${f.pos_object_id}_getPositionList`;this._sdk.updateCache(h,f),a?e.includes(f.pool)&&n.push(f):n.push(f)}return n}async getPosition(t,e,i=!0,n=!0){let r=await this.getSimplePosition(e,n);return i&&(r=await this.updatePositionInfo(t,r)),r}async getPositionById(t,e=!0,i=!0){let n=await this.getSimplePosition(t,i);if(e){let r=await this._sdk.Pool.getPool(n.pool,!1);return await this.updatePositionInfo(r.position_manager.positions_handle,n)}return n}async getSimplePosition(t,e=!0){let i=`${t}_getPositionList`,n=this.getSimplePositionByCache(t);if(n===void 0){let r=await this.sdk.FullClient.getObject({id:t,options:{showContent:!0,showType:!0,showDisplay:e,showOwner:!0}});n=mi(r),this._sdk.updateCache(i,n)}return n}getSimplePositionByCache(t){let e=`${t}_getPositionList`;return this._sdk.getCache(e)}async getSimplePositionList(t,e=!0){let i=[],n=[];return t.forEach(r=>{let a=this.getSimplePositionByCache(r);a?i.push(a):n.push(r)}),n.length>0&&(await this._sdk.FullClient.batchGetObjects(n,{showOwner:!0,showContent:!0,showDisplay:e,showType:!0})).forEach(a=>{if(a.error==null){let l=mi(a);i.push(l);let d=`${l.pos_object_id}_getPositionList`;this._sdk.updateCache(d,l)}}),i}async updatePositionInfo(t,e){let i=await this.getPositionInfo(t,e.pos_object_id);return{...e,...i}}async getPositionInfo(t,e){try{let i=await this._sdk.FullClient.getDynamicFieldObject({parentId:t,name:{type:"0x2::object::ID",value:e}}),r=cr(i.data).value.fields.value;return He(r)}catch(i){return qt("FetchError",i,{[he.METHOD_NAME]:"getPositionInfo",[he.REQUEST_PARAMS]:{position_handle:t,position_id:e}})}}async getPositionInfoList(t){try{let e=[],i=[];return t.forEach(async r=>{let{position_handle:a,position_ids:l}=r;l.forEach(d=>{let f=_a(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=cr(r.data).value.fields.value,d=He(l);e.push(d)}catch(a){console.log("getPositionInfoList error",a)}}),e)}catch(e){return qt("FetchError",e,{[he.METHOD_NAME]:"getPositionInfoList",[he.REQUEST_PARAMS]:{options:t}})}}buildFetchPosFee(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r=[t.coin_type_a,t.coin_type_b],a=[e.object(ce(i).global_config_id),e.object(t.pool_id),e.pure.address(t.position_id)];e.moveCall({target:`${n.published_at}::${Jt}::fetch_position_fees`,arguments:a,typeArguments:r})}parsedPosFeeData(t){let e={},i=t.events?.filter(n=>n.type.includes("fetcher_script::FetchPositionFeesEvent"));for(let n=0;n<i.length;n+=1){let{parsedJson:r}=i[n],a={position_id:r.position_id,fee_owned_a:r.fee_owned_a,fee_owned_b:r.fee_owned_b};e[r.position_id]=a}return e}async fetchPosFeeAmount(t){let{clmm_pool:e,integrate:i}=this.sdk.sdkOptions,n=new ae;for(let d of t)this.buildFetchPosFee(d,n);let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:ua("0x0")});r.error!=null&&R("InvalidPoolObject",`fetch position fee error code: ${r.error??"unknown error"}, please check config and position and pool object ids`,{[he.METHOD_NAME]:"fetchPosFeeAmount"});let a=[],l=this.parsedPosFeeData(r);for(let d=0;d<t.length;d+=1){let f=l[t[d].position_id];if(f){let h={fee_owned_a:f.fee_owned_a,fee_owned_b:f.fee_owned_b,position_id:t[d].position_id};a.push(h)}}return a}async batchFetchPositionFees(t){let e=[];for(let n of t){let r=await this._sdk.Position.getPositionById(n,!1),a=await this._sdk.Pool.getPool(r.pool,!1);e.push({pool_id:a.id,position_id:r.pos_object_id,coin_type_a:a.coin_type_a,coin_type_b:a.coin_type_b})}let i={};if(e.length>0){let n=await this.fetchPosFeeAmount(e);for(let r of n)i[r.position_id]=r;return i}return i}async createAddLiquidityFixTokenPayload(t,e,i,n,r){let a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:l,is_adjust_coin_b:d}=Ae(t);if(t=t,t.fix_amount_a&&l||!t.fix_amount_a&&d)return i=await Ut.buildAddLiquidityFixTokenForGas(this._sdk,a,t,e,i,n,r),i}return Ut.buildAddLiquidityFixToken(this._sdk,a,t,i,n,r)}async createAddLiquidityPayload(t,e,i,n){let{integrate:r,clmm_pool:a}=this._sdk.sdkOptions,l=Re(BigInt(t.tick_lower)).toString(),d=Re(BigInt(t.tick_upper)).toString(),f=[t.coin_type_a,t.coin_type_b];e=e||new ae;let h=!la(t.pos_id),w=BigInt(t.max_amount_a),P=BigInt(t.max_amount_b),M,T;if(i==null||n==null){let S=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());M=Ci.buildCoinForAmount(e,S,w,t.coin_type_a,!1,!0),T=Ci.buildCoinForAmount(e,S,P,t.coin_type_b,!1,!0)}else M={target_coin:i,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},T={target_coin:n,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]};if(h)e.moveCall({target:`${r.published_at}::${Qt}::open_position_with_liquidity`,typeArguments:f,arguments:[e.object(ce(a).global_config_id),e.object(t.pool_id),e.pure.u32(Number(l)),e.pure.u32(Number(d)),M.target_coin,T.target_coin,e.pure.u64(t.max_amount_a),e.pure.u64(t.max_amount_b),e.pure.u128(t.delta_liquidity),e.object(Pi)]});else{let S=await this._sdk.FullClient.getOwnerCoinAssets(this._sdk.getSenderAddress());e=Ut.createCollectRewarderAndFeeParams(this._sdk,e,t,S,M.remain_coins,T.remain_coins),e.moveCall({target:`${r.published_at}::${Qt}::add_liquidity`,typeArguments:f,arguments:[e.object(ce(a).global_config_id),e.object(t.pool_id),e.object(t.pos_id),M.target_coin,T.target_coin,e.pure.u64(t.max_amount_a),e.pure.u64(t.max_amount_b),e.pure.u128(t.delta_liquidity),e.object(Pi)]})}return e}async removeLiquidityPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r="remove_liquidity";e=e||new ae;let a=[t.coin_type_a,t.coin_type_b],l=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());e=Ut.createCollectRewarderAndFeeParams(this._sdk,e,t,l);let d=[e.object(ce(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.pure.u128(t.delta_liquidity),e.pure.u64(t.min_amount_a),e.pure.u64(t.min_amount_b),e.object(Pi)];return e.moveCall({target:`${n.published_at}::${$e}::${r}`,typeArguments:a,arguments:d}),e}async closePositionPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new ae;let r=[t.coin_type_a,t.coin_type_b],a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());return e=Ut.createCollectRewarderAndFeeParams(this._sdk,e,t,a),e.moveCall({target:`${n.published_at}::${$e}::close_position`,typeArguments:r,arguments:[e.object(ce(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.pure.u64(t.min_amount_a),e.pure.u64(t.min_amount_b),e.object(Pi)]}),e}openPositionPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new ae;let r=[t.coin_type_a,t.coin_type_b],a=Re(BigInt(t.tick_lower)).toString(),l=Re(BigInt(t.tick_upper)).toString(),d=[e.object(ce(i).global_config_id),e.object(t.pool_id),e.pure.u32(Number(a)),e.pure.u32(Number(l))];return e.moveCall({target:`${n.published_at}::${$e}::open_position`,typeArguments:r,arguments:d}),e}async openPositionWithPricePayload(t,e){let{pool_id:i}=t,{clmm_pool:n,integrate:r}=this.sdk.sdkOptions;e=e||new ae;let a=await this.sdk.Pool.getPool(i,!1),l=Number(a.tick_spacing),d=0,f=0;if(t.is_full_range)d=Mi.getMinIndex(l),f=Mi.getMaxIndex(l);else{let{price_base_coin:P,min_price:M,max_price:T}=t;d=Fe.priceToInitializeTickIndex(P==="coin_a"?ie(M):ie(1).div(T),t.coin_decimals_a,t.coin_decimals_b,l),f=Fe.priceToInitializeTickIndex(P==="coin_a"?ie(T):ie(1).div(M),t.coin_decimals_a,t.coin_decimals_b,l)}let h=[a.coin_type_a,a.coin_type_b],w=[e.object(ce(n).global_config_id),e.object(t.pool_id),e.pure.u32(Number(Re(BigInt(d)))),e.pure.u32(Number(Re(BigInt(f))))];return e.moveCall({target:`${r.published_at}::${$e}::open_position`,typeArguments:h,arguments:w}),e}async collectFeePayload(t,e,i,n){e=e||new ae;let r=i||Ci.buildCoinWithBalance(BigInt(0),t.coin_type_a,e),a=n||Ci.buildCoinWithBalance(BigInt(0),t.coin_type_b,e);return this.createCollectFeePayload(t,e,r,a),e}createCollectFeePayload(t,e,i,n){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,l=[t.coin_type_a,t.coin_type_b],d=[e.object(ce(r).global_config_id),e.object(t.pool_id),e.object(t.pos_id),i,n];return e.moveCall({target:`${a.published_at}::${Qt}::collect_fee`,typeArguments:l,arguments:d}),e}async calculateAddLiquidityResultWithPrice(t){let{pool_id:e,slippage:i,refresh_pool_price:n,add_mode_params:r}=t,a=await this.sdk.Pool.getPool(e,n),l=Number(a.tick_spacing),d=0,f=0;if(r.is_full_range)d=Mi.getMinIndex(l),f=Mi.getMaxIndex(l);else{let{price_base_coin:j,min_price:V,max_price:N}=r;d=Fe.priceToInitializeTickIndex(j==="coin_a"?ie(V):ie(1).div(N),r.coin_decimals_a,r.coin_decimals_b,l),f=Fe.priceToInitializeTickIndex(j==="coin_a"?ie(N):ie(1).div(V),r.coin_decimals_a,r.coin_decimals_b,l)}if("liquidity"in t){let{liquidity:j}=t,V=Fe.tickIndexToSqrtPriceX64(d),N=Fe.tickIndexToSqrtPriceX64(f),{coin_amount_a:D,coin_amount_b:jt}=ar.getCoinAmountFromLiquidity(new Ze.default(j),new Ze.default(a.current_sqrt_price),V,N,!1),z=ie(D).mul(1+i).toFixed(0,I.ROUND_UP),$t=ie(jt).mul(1+i).toFixed(0,I.ROUND_UP);return{coin_amount_a:D,coin_amount_b:jt,coin_amount_limit_a:z,coin_amount_limit_b:$t,liquidity:j,tick_lower:d,tick_upper:f}}let{coin_amount:h,fix_amount_a:w}=t,{coin_amount_limit_a:P,coin_amount_limit_b:M,liquidity_amount:T,coin_amount_a:S,coin_amount_b:L}=ar.estLiquidityAndCoinAmountFromOneAmounts(d,f,new Ze.default(h),w,!0,i,new Ze.default(a.current_sqrt_price));return{coin_amount_a:S,coin_amount_b:L,coin_amount_limit_a:P,coin_amount_limit_b:M,liquidity:T,tick_lower:d,tick_upper:f,fix_amount_a:w}}async addLiquidityWithPricePayload(t,e,i,n){let{pool_id:r,calculate_result:a}=t,{coin_amount_limit_a:l,coin_amount_limit_b:d,liquidity:f,tick_lower:h,tick_upper:w}=a;e=e||new ae;let P=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityPayload({delta_liquidity:f,max_amount_a:l,max_amount_b:d,tick_lower:h,tick_upper:w,collect_fee:!1,rewarder_coin_types:[],coin_type_a:P.coin_type_a,coin_type_b:P.coin_type_b,pool_id:r,pos_id:""},e,i,n),e}async createAddLiquidityFixCoinWithPricePayload(t,e,i,n){let{pool_id:r,calculate_result:a}=t,{coin_amount_limit_a:l,coin_amount_limit_b:d,liquidity:f,tick_lower:h,tick_upper:w,fix_amount_a:P,coin_amount_a:M,coin_amount_b:T}=a;if(P===void 0)throw R("InvalidParams","fix_amount_a is undefined",{[he.METHOD_NAME]:"addLiquidityFixCoinCoinWithPricePayload",[he.REQUEST_PARAMS]:t});e=e||new ae;let S=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityFixTokenPayload({amount_a:P?M:l,amount_b:P?d:T,slippage:0,fix_amount_a:P,is_open:!0,tick_lower:h,tick_upper:w,collect_fee:!1,rewarder_coin_types:[],coin_type_a:S.coin_type_a,coin_type_b:S.coin_type_b,pool_id:r,pos_id:""},void 0,e,i,n),e}createCollectFeeNoSendPayload(t,e,i,n){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,l=[t.coin_type_a,t.coin_type_b],d=[e.object(ce(r).global_config_id),e.object(t.pool_id),e.object(t.pos_id),i,n];return e.moveCall({target:`${a.published_at}::${ii}::collect_fee`,typeArguments:l,arguments:d}),e}};var Mn=re(Gt());import{Transaction as wn}from"@mysten/sui/transactions";import{normalizeSuiAddress as ha}from"@mysten/sui/utils";import{CLOCK_ADDRESS as yn,CoinAssist as vn,DETAILS_KEYS as lr,getPackagerConfigs as Pn,MathUtil as pa,normalizeCoinType as Cn,ZERO as Ne}from"@cetusprotocol/common-sdk";var Ti=class{constructor(t){this._sdk=t,this.growthGlobal=[Ne,Ne,Ne]}get sdk(){return this._sdk}async emissionsEveryDay(t){let i=(await this.sdk.Pool.getPool(t)).rewarder_infos;if(!i)return null;let n=[];for(let r of i){let a=pa.fromX64(new Mn.default(r.emissions_per_second));n.push({emissions:Math.floor(a.toNumber()*60*60*24),coin_type:r.coin_type})}return n}async batchFetchPositionRewarders(t){let e=[];for(let n of t){let r=await this._sdk.Position.getPositionById(n,!1),a=await this._sdk.Pool.getPool(r.pool,!1);e.push({pool_id:a.id,position_id:r.pos_object_id,coin_type_a:a.coin_type_a,coin_type_b:a.coin_type_b,rewarder_types:a.rewarder_infos.map(l=>l.coin_type)})}let i={};if(e.length>0){let n=await this.fetchPosRewardersAmount(e);for(let r of n)i[r.position_id]=r.rewarder_amounts;return i}return i}async fetchPositionRewarders(t,e){let i={pool_id:t.id,position_id:e,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b,rewarder_types:t.rewarder_infos.map(r=>r.coin_type)};return(await this.fetchPosRewardersAmount([i]))[0].rewarder_amounts}async batchFetchPositionFees(t){return await this._sdk.Position.batchFetchPositionFees(t)}async fetchPosFeeAmount(t){return await this._sdk.Position.fetchPosFeeAmount(t)}buildFetchPosReward(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r=[t.coin_type_a,t.coin_type_b],a=[e.object(Pn(i).global_config_id),e.object(t.pool_id),e.pure.address(t.position_id),e.object(yn)];e.moveCall({target:`${n.published_at}::${Jt}::fetch_position_rewards`,arguments:a,typeArguments:r})}async fetchPosRewardersAmount(t){let e=new wn;for(let a of t)this.buildFetchPosReward(a,e);let i=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:e,sender:ha("0x")});i.error!=null&&R("InvalidConfig",`fetch position rewards error code: ${i.error??"unknown error"}, please check config and params`,{[lr.METHOD_NAME]:"fetchPosRewardersAmount",[lr.REQUEST_PARAMS]:{params:t}});let n=this.parsedPosRewardData(i),r=[];for(let a=0;a<t.length;a+=1){let l=n[t[a].position_id];if(l){let d={pool_id:t[a].pool_id,position_id:t[a].position_id,rewarder_amounts:l.rewarder_amount.map((f,h)=>({amount_owned:f,coin_type:t[a].rewarder_types[h]}))};r.push(d)}}return r}parsedPosRewardData(t){let e={},i=t.events?.filter(n=>n.type.includes("fetcher_script::FetchPositionRewardsEvent"));for(let n=0;n<i.length;n+=1){let{parsedJson:r}=i[n],a={position_id:r.position_id,rewarder_amount:r.data};e[r.position_id]=a}return e}async fetchPoolRewardersAmount(t,e){let i=await this.sdk.Pool.getPool(e),n=await this.sdk.Position.getPositionList(t,[e]),r=[];for(let d of n)r.push({pool_id:i.id,position_id:d.pos_object_id,rewarder_types:i.rewarder_infos.map(f=>f.coin_type),coin_type_a:i.coin_type_a,coin_type_b:i.coin_type_b});let a=await this.fetchPosRewardersAmount(r),l=[Ne,Ne,Ne];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 Mn.default(d.rewarder_amounts[f].amount_owned));return l}async getPoolLowerAndUpperTicks(t,e){let i=[],n=[];for(let r of e){let a=await this.sdk.Pool.getTickDataByIndex(t,r.tick_lower_index),l=await this.sdk.Pool.getTickDataByIndex(t,r.tick_upper_index);i.push(a),n.push(l)}return[i,n]}async collectRewarderPayload(t){let e=await this.sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress(),null),i=new wn;return i=Ut.createCollectRewarderAndFeeParams(this._sdk,i,t,e),i}async batchCollectRewardsPayload(t,e,i,n){let r=await this.sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress(),null);e=e||new wn;let a={};return t.forEach(l=>{let d=Cn(l.coin_type_a),f=Cn(l.coin_type_b);if(l.collect_fee){let w=a[d];w==null&&(i==null?w=vn.buildCoinForAmount(e,r,BigInt(0),d,!1):w={target_coin:i,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},a[d]=w);let P=a[f];P==null&&(n==null?P=vn.buildCoinForAmount(e,r,BigInt(0),f,!1):P={target_coin:n,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},a[f]=P),e=this._sdk.Position.createCollectFeeNoSendPayload({pool_id:l.pool_id,pos_id:l.pos_id,coin_type_a:l.coin_type_a,coin_type_b:l.coin_type_b},e,w.target_coin,P.target_coin)}let h=[];l.rewarder_coin_types.forEach(w=>{let P=Cn(w),M=a[w];M===void 0&&(M=vn.buildCoinForAmount(e,r,BigInt(0),P,!1),a[P]=M),h.push(M.target_coin)}),e=this.createCollectRewarderNoSendPayload(l,e,h)}),Object.keys(a).forEach(l=>{let d=a[l];d.is_mint_zero_coin&&zo(this.sdk,e,d.target_coin,l,this.sdk.getSenderAddress())}),e}createCollectRewarderPayload(t,e,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=Pn(n),l=[t.coin_type_a,t.coin_type_b];return t.rewarder_coin_types.forEach((d,f)=>{e&&e.moveCall({target:`${r.published_at}::${Qt}::collect_reward`,typeArguments:[...l,d],arguments:[e.object(a.global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.object(a.global_vault_id),i[f],e.object(yn)]})}),e}createCollectRewarderNoSendPayload(t,e,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=Pn(n),l=[t.coin_type_a,t.coin_type_b];return t.rewarder_coin_types.forEach((d,f)=>{e&&e.moveCall({target:`${r.published_at}::${ii}::collect_reward`,typeArguments:[...l,d],arguments:[e.object(a.global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.object(a.global_vault_id),i[f],e.object(yn)]})}),e}};var be=re(Gt());import{Transaction as ur}from"@mysten/sui/transactions";import{normalizeSuiAddress as dr}from"@mysten/sui/utils";import{d as Be,DETAILS_KEYS as pe,extractStructTagFromType as fr,TickMath as _r,U64_MAX as ma,ZERO as ga}from"@cetusprotocol/common-sdk";var Kl="amm_swap",Hl="Pool",Ai=class{constructor(t){this.calculateSingleImpact=(t,e)=>e.minus(t).div(e).mul(100);this._sdk=t}get sdk(){return this._sdk}calculateSwapFee(t){let e=Be(0);return t.forEach(i=>{let n=i.base_paths.length;if(n>0){let r=i.base_paths[0],a=r.label==="Cetus"?new I(r.fee_rate).div(10**6):new I(r.fee_rate).div(10**9),l=Be(r.input_amount).div(10**r.from_decimal).mul(a);if(e=e.add(l),n>1){let d=i.base_paths[1],f=r.direction?r.current_price:new I(1).div(r.current_price),h=d.direction?d.current_price:new I(1).div(d.current_price),w=d.label==="Cetus"?new I(d.fee_rate).div(10**6):new I(d.fee_rate).div(10**9),M=Be(d.output_amount).div(10**d.to_decimal).mul(w).div(f.mul(h));e=e.add(M)}}}),e.toString()}calculateSwapPriceImpact(t){let e=Be(0);return t.forEach(i=>{let n=i.base_paths.length;if(n===1){let r=i.base_paths[0],a=Be(r.output_amount).div(10**r.to_decimal),l=Be(r.input_amount).div(10**r.from_decimal),d=a.div(l),f=r.direction?new I(r.current_price):new I(1).div(r.current_price);e=e.add(this.calculateSingleImpact(d,f))}if(n===2){let r=i.base_paths[0],a=i.base_paths[1],l=r.direction?new I(r.current_price):new I(1).div(r.current_price),d=a.direction?new I(a.current_price):new I(1).div(a.current_price),f=l.mul(d),h=new I(a.output_amount).div(10**a.to_decimal),w=new I(r.input_amount).div(10**r.from_decimal),P=h.div(w);e=e.add(this.calculateSingleImpact(P,f))}}),e.toString()}async preSwapWithMultiPool(t){let{integrate:e}=this.sdk.sdkOptions,i=new ur,n=[t.coin_type_a,t.coin_type_b];for(let f=0;f<t.pool_ids.length;f+=1){let h=[i.object(t.pool_ids[f]),i.pure.bool(t.a2b),i.pure.bool(t.by_amount_in),i.pure.u64(t.amount)];i.moveCall({target:`${e.published_at}::${Jt}::calculate_swap_result`,arguments:h,typeArguments:n})}let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:dr("0x0")});r.error!=null&&R("InvalidConfig",`pre swap with multi pools error code: ${r.error??"unknown error"}, please check config and params`,{[pe.METHOD_NAME]:"preSwapWithMultiPool",[pe.REQUEST_PARAMS]:{params:t}});let a=r.events?.filter(f=>fr(f.type).name==="CalculatedSwapResultEvent");if(a.length===0)return null;a.length!==t.pool_ids.length&&R("ParamsLengthNotEqual","valueData.length !== params.pools.length",{[pe.METHOD_NAME]:"preSwapWithMultiPool",[pe.REQUEST_PARAMS]:{params:t}});let l=t.by_amount_in?ga:ma,d=0;for(let f=0;f<a.length;f+=1)if(!a[f].parsedJson.data.is_exceed)if(t.by_amount_in){let h=new be.default(a[f].parsedJson.data.amount_out);h.gt(l)&&(d=f,l=h)}else{let h=new be.default(a[f].parsedJson.data.amount_out);h.lt(l)&&(d=f,l=h)}return this.transformSwapWithMultiPoolData({pool_address:t.pool_ids[d],a2b:t.a2b,by_amount_in:t.by_amount_in,amount:t.amount,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b},a[d].parsedJson)}async preSwap(t){let{integrate:e}=this.sdk.sdkOptions,i=new ur,n=[t.coin_type_a,t.coin_type_b],r=[i.object(t.pool.id),i.pure.bool(t.a2b),i.pure.bool(t.by_amount_in),i.pure.u64(t.amount)];i.moveCall({target:`${e.published_at}::${Jt}::calculate_swap_result`,arguments:r,typeArguments:n});let a=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:dr("0x0")});if(a.error!=null)return R("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[pe.METHOD_NAME]:"preSwap",[pe.REQUEST_PARAMS]:{params:t}});let l=a.events?.filter(d=>fr(d.type).name==="CalculatedSwapResultEvent");return l.length===0?R("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[pe.METHOD_NAME]:"preSwap",[pe.REQUEST_PARAMS]:{params:t}}):this.transformSwapData(t,l[0].parsedJson.data)}transformSwapData(t,e){let i=e.amount_in&&e.fee_amount?new be.default(e.amount_in).add(new be.default(e.fee_amount)).toString():"";return{pool_address:t.pool.id,current_sqrt_price:t.current_sqrt_price,estimated_amount_in:i,estimated_amount_out:e.amount_out,estimated_end_sqrt_price:e.after_sqrt_price,estimated_fee_amount:e.fee_amount,is_exceed:e.is_exceed,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in}}transformSwapWithMultiPoolData(t,e){let{data:i}=e;console.log("json data. ",i);let n=i.amount_in&&i.fee_amount?new be.default(i.amount_in).add(new be.default(i.fee_amount)).toString():"";return{pool_address:t.pool_address,estimated_amount_in:n,estimated_amount_out:i.amount_out,estimated_end_sqrt_price:i.after_sqrt_price,estimated_start_sqrt_price:i.step_results[0].current_sqrt_price,estimated_fee_amount:i.fee_amount,is_exceed:i.is_exceed,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in}}calculateRates(t){let{current_pool:e}=t,i=Uo(e),n;t.a2b?n=t.swap_ticks.sort((P,M)=>M.index-P.index):n=t.swap_ticks.sort((P,M)=>P.index-M.index);let r=Vo(t.a2b,t.by_amount_in,t.amount,i,n),a=!1;t.by_amount_in?a=r.amount_in.lt(t.amount):a=r.amount_out.lt(t.amount);let l=te.getDefaultSqrtPriceLimit(t.a2b);t.a2b&&r.next_sqrt_price.lt(l)&&(a=!0),!t.a2b&&r.next_sqrt_price.gt(l)&&(a=!0);let d=0;r.cross_tick_num>6&&r.cross_tick_num<40&&(d=22e3*(r.cross_tick_num-6)),r.cross_tick_num>40&&(a=!0);let f=_r.sqrtPriceX64ToPrice(i.current_sqrt_price,t.decimals_a,t.decimals_b).toNumber(),h=_r.sqrtPriceX64ToPrice(r.next_sqrt_price,t.decimals_a,t.decimals_b).toNumber(),w=Math.abs(f-h)/f*100;return{estimated_amount_in:r.amount_in,estimated_amount_out:r.amount_out,estimated_end_sqrt_price:r.next_sqrt_price,estimated_fee_amount:r.fee_amount,is_exceed:a,extra_compute_limit:d,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in,price_impact_pct:w}}async createSwapPayload(t,e){let i=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:n,is_adjust_coin_b:r}=Ae(t);if(t.a2b&&n||!t.a2b&&r)return await te.buildSwapTransactionForGas(this._sdk,t,i,e)}return te.buildSwapTransaction(this.sdk,t,i)}async createSwapWithoutTransferCoinsPayload(t,e){let i=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:n,is_adjust_coin_b:r}=Ae(t);if(t.a2b&&n||!t.a2b&&r)return await te.buildSwapTransactionWithoutTransferCoinsForGas(this._sdk,t,i,e)}return te.buildSwapTransactionWithoutTransferCoins(this.sdk,t,i)}};import{CLOCK_ADDRESS as ba,CoinAssist as wa,DETAILS_KEYS as we,getPackagerConfigs as kn}from"@cetusprotocol/common-sdk";import{Transaction as hr}from"@mysten/sui/transactions";import{normalizeSuiAddress as ya}from"@mysten/sui/utils";var Si=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}async getClmmVestInfoList(){let{clmm_vest:t}=this._sdk.sdkOptions;if(t===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"getClmmVestInfo"});let e=[];try{let i=`${t.package_id}::clmm_vester::CreateEvent`,r=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:i})).data.map(a=>a.parsedJson.clmm_vester_id);return r.length>0&&(await this._sdk.FullClient.batchGetObjects(r,{showContent:!0,showType:!0})).forEach(l=>{let d=ee.parseClmmVestInfo(l),f=`${d.id}-ClmmVestInfo`;this._sdk.updateCache(f,d),e.push(d)}),e}catch(i){qt("ClmmVestFetchError",i,{[we.METHOD_NAME]:"getClmmVestInfoList"})}return[]}async getClmmVestInfo(t=!0){let{clmm_vest:e}=this._sdk.sdkOptions;if(e===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"getClmmVestInfo"});let{clmm_vest_id:i}=kn(e),n=`${i}-ClmmVestInfo`,r=this._sdk.getCache(n,t);if(r)return r;try{let a=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0,showType:!0}}),l=ee.parseClmmVestInfo(a);return this._sdk.updateCache(n,l),l}catch(a){return qt("ClmmVestFetchError",a,{[we.METHOD_NAME]:"getClmmVestInfo",[we.REQUEST_PARAMS]:{clmm_vest_id:i}})}}async getPositionVesting(t){let{clmm_vest:e}=this._sdk.sdkOptions;if(e===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"getPositionVesting"});if(t.length===0)return[];let{clmm_vest_id:i}=kn(e),n=new hr;t.forEach(l=>{let{clmm_pool_id:d,coin_type_a:f,coin_type_b:h,clmm_position_ids:w}=l;n.moveCall({package:e.published_at,module:"clmm_vester",function:"get_positions_vesting",typeArguments:[f,h],arguments:[n.object(i),n.object(d),n.pure.vector("id",w)]})});let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:ya("0x0")}),a=[];return r.events?.forEach(l=>{if(l.type.includes("clmm_vester::GetPositionsVestingEvent")){let{parsedJson:d}=l;a.push(...d.position_vestings.map(f=>ee.parsePositionVesting(f)))}}),a}buildRedeemPayload(t,e){let{clmm_vest:i}=this._sdk.sdkOptions;if(i===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[we.METHOD_NAME]:"buildRedeemVestPayload"});let{versioned_id:n,clmm_vest_id:r,cetus_coin_type:a}=kn(i);return e=e||new hr,t.forEach(l=>{let{clmm_pool_id:d,clmm_position_id:f,coin_type_a:h,coin_type_b:w,period:P}=l,M=e.moveCall({package:i.published_at,module:"clmm_vester",function:"redeem",arguments:[e.object(n),e.object(r),e.object(d),typeof f=="string"?e.object(f):f,e.pure.u16(P),e.object(ba)],typeArguments:[h,w]}),T=wa.fromBalance(M,a,e);e.transferObjects([T],this._sdk.getSenderAddress())}),e}};var xi=class o extends va{constructor(t){super(t),this._swap=new Ai(this),this._pool=new vi(this),this._position=new ki(this),this._config=new ei(this),this._rewarder=new Ti(this),this._vest=new Si(this)}get Pool(){return this._pool}get Position(){return this._position}get CetusConfig(){return this._config}get Rewarder(){return this._rewarder}get Swap(){return this._swap}get Vest(){return this._vest}static createSDK(t){let{env:e="mainnet"}=t;return e==="mainnet"?o.createCustomSDK({...Yo,...t}):o.createCustomSDK({...er,...t})}static createCustomSDK(t){return new o(t)}};var ze=re(Gt());import{MathUtil as je,TickMath as pr}from"@cetusprotocol/common-sdk";var Pa=new ze.default(365),Ca=new ze.default(24),Ma=new ze.default(3600),ka=new ze.default(.5);function wu(o,t,e,i){return Pa.mul(Ca).mul(Ma).mul(ka).mul(o.mul(t).add(e).div(i))}function Ta(o,t,e,i,n,r){let a=new I(o.toString()).div(new I(10**e)),l=new I(t.toString()).div(new I(10**i));return a.mul(n).add(l.mul(r))}function yu(o,t,e,i,n,r,a,l,d,f,h,w,P,M,T,S,L,j,V,N,D,jt,z,$t){let ne=new I(w),Zt=new I(P),oe=new I(S),$=new I(L),Nt=new I(j),y=new I(V),s=new I(N),u=new I(D),_=new I(jt),p=new I(z),g=new I($t),v=pr.tickIndexToSqrtPriceX64(t),C=pr.tickIndexToSqrtPriceX64(e),m=je.toX64Decimal(je.fromX64(v)).round(),c=je.toX64Decimal(je.fromX64(C)).round(),b=je.toX64Decimal(je.fromX64(i)).round(),A,x=ne.mul(new I(10**r)).mul(c.mul(m)).div(c.sub(m)).round(),E=Zt.mul(new I(10**a)).div(c.sub(m)).round();o<t?A=x:o>e?A=E:A=I.min(x,E);let q=A.mul(b.sub(m)),K=A.mul(c.sub(b)).div(b.mul(c)).div(new I(10**r)).mul(s).add(q.div(new I(10**a).mul(u))),zt=Ta(M,T,r,a,s,u),H=K.div(zt),G=A.eq(new I(0))?new I(0):new I(h/1e4).mul(oe).mul(new I(A.toString()).div(new I(n.toString()).add(new I(A.toString())))).div(K),ye=H.eq(new I(0))?new I(0):H.mul(new I(36500/7)).div(K),Q=$.div(new I(10**l)).mul(_).mul(ye),J=Nt.div(new I(10**d)).mul(p).mul(ye),Ge=y.div(new I(10**f)).mul(g).mul(ye);return{fee_apr:G,pos_rewarder_0_apr:Q,pos_rewarder_1_apr:J,pos_rewarder_2_apr:Ge}}function vu(o,t,e,i){let n=Math.max(o,e),a=Math.min(t,i)-n,l=t-o,d=i-e,f=new I(l.toString()),h=new I(d.toString()),w=new I(a.toString()),P=new I("0");return a<0?P=new I("0"):l===a?P=h.div(w):d===a?P=w.div(f):P=w.mul(w).div(h).div(f),P}var Au=xi;export{Kl as AMM_SWAP_MODULE,Ys as BPS,xi as CetusClmmSDK,Ce as ClmmError,Da as ClmmExpectSwapModule,Jt as ClmmFetcherModule,$e as ClmmIntegratePoolModule,Qt as ClmmIntegratePoolV2Module,ii as ClmmIntegratePoolV3Module,ni as ClmmIntegrateRouterModule,bo as ClmmIntegrateRouterWithPartnerModule,wo as ClmmIntegrateUtilsModule,go as ClmmPartnerModule,po as ClmmPositionStatus,Me as ConfigErrorCode,ei as ConfigModule,$a as DeepbookClobV2Module,La as DeepbookCustodianV2Module,Ua as DeepbookEndpointsV2Module,Hl as POOL_STRUCT,co as PartnerErrorCode,De as PoolErrorCode,vi as PoolModule,Ye as PositionErrorCode,ki as PositionModule,Ut as PositionUtils,Ti as RewarderModule,Nr as RouterErrorCode,so as SwapErrorCode,Ai as SwapModule,te as SwapUtils,lo as TypesErrorCode,Yi as UtilsErrorCode,ao as VestErrorCode,Si as VestModule,ee as VestUtils,Sc as buildClmmPositionName,pi as buildPool,Zo as buildPoolTransactionInfo,mi as buildPosition,He as buildPositionInfo,Wo as buildPositionTransactionInfo,gi as buildTickData,Ho as buildTickDataByEvent,zo as buildTransferCoin,bi as buildTransferCoinToSender,Yo as clmmMainnet,er as clmmTestnet,Vo as computeSwap,Ks as computeSwapStep,Au as default,wu as estPoolAPR,yu as estPositionAPRWithDeltaMethod,vu as estPositionAPRWithMultiMethod,tr as eventMainnetContractMaps,ir as eventTestnetContractMaps,Ae as findAdjustCoin,Va as getDefaultSuiInputType,lc as getLowerSqrtPriceFromCoinA,dc as getLowerSqrtPriceFromCoinB,uc as getUpperSqrtPriceFromCoinA,fc as getUpperSqrtPriceFromCoinB,qt as handleError,R as handleMessageError,gc as newBits,mo as poolFilterEvenTypes,ho as poolLiquiditySnapshotType,Uo as transClmmpoolDataWithoutTicks};
|
|
1
|
+
var mr=Object.create;var $n=Object.defineProperty;var gr=Object.getOwnPropertyDescriptor;var br=Object.getOwnPropertyNames;var wr=Object.getPrototypeOf,yr=Object.prototype.hasOwnProperty;var vr=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(t,e)=>(typeof require<"u"?require:t)[e]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var Pr=(o,t)=>()=>(t||o((t={exports:{}}).exports,t),t.exports);var Cr=(o,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of br(t))!yr.call(o,n)&&n!==e&&$n(o,n,{get:()=>t[n],enumerable:!(i=gr(t,n))||i.enumerable});return o};var re=(o,t,e)=>(e=o!=null?mr(wr(o)):{},Cr(t||!o||!o.__esModule?$n(e,"default",{value:o,enumerable:!0}):e,o));var Gt=Pr((_o,on)=>{"use strict";(function(o,t){"use strict";function e(y,s){if(!y)throw new Error(s||"Assertion failed")}function i(y,s){y.super_=s;var u=function(){};u.prototype=s.prototype,y.prototype=new u,y.prototype.constructor=y}function n(y,s,u){if(n.isBN(y))return y;this.negative=0,this.words=null,this.length=0,this.red=null,y!==null&&((s==="le"||s==="be")&&(u=s,s=10),this._init(y||0,s||10,u||"be"))}typeof o=="object"?o.exports=n:t.BN=n,n.BN=n,n.wordSize=26;var r;try{typeof window<"u"&&typeof window.Buffer<"u"?r=window.Buffer:r=vr("buffer").Buffer}catch{}n.isBN=function(s){return s instanceof n?!0:s!==null&&typeof s=="object"&&s.constructor.wordSize===n.wordSize&&Array.isArray(s.words)},n.max=function(s,u){return s.cmp(u)>0?s:u},n.min=function(s,u){return s.cmp(u)<0?s:u},n.prototype._init=function(s,u,_){if(typeof s=="number")return this._initNumber(s,u,_);if(typeof s=="object")return this._initArray(s,u,_);u==="hex"&&(u=16),e(u===(u|0)&&u>=2&&u<=36),s=s.toString().replace(/\s+/g,"");var p=0;s[0]==="-"&&(p++,this.negative=1),p<s.length&&(u===16?this._parseHex(s,p,_):(this._parseBase(s,u,p),_==="le"&&this._initArray(this.toArray(),u,_)))},n.prototype._initNumber=function(s,u,_){s<0&&(this.negative=1,s=-s),s<67108864?(this.words=[s&67108863],this.length=1):s<4503599627370496?(this.words=[s&67108863,s/67108864&67108863],this.length=2):(e(s<9007199254740992),this.words=[s&67108863,s/67108864&67108863,1],this.length=3),_==="le"&&this._initArray(this.toArray(),u,_)},n.prototype._initArray=function(s,u,_){if(e(typeof s.length=="number"),s.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(s.length/3),this.words=new Array(this.length);for(var p=0;p<this.length;p++)this.words[p]=0;var g,v,C=0;if(_==="be")for(p=s.length-1,g=0;p>=0;p-=3)v=s[p]|s[p-1]<<8|s[p-2]<<16,this.words[g]|=v<<C&67108863,this.words[g+1]=v>>>26-C&67108863,C+=24,C>=26&&(C-=26,g++);else if(_==="le")for(p=0,g=0;p<s.length;p+=3)v=s[p]|s[p+1]<<8|s[p+2]<<16,this.words[g]|=v<<C&67108863,this.words[g+1]=v>>>26-C&67108863,C+=24,C>=26&&(C-=26,g++);return this._strip()};function a(y,s){var u=y.charCodeAt(s);if(u>=48&&u<=57)return u-48;if(u>=65&&u<=70)return u-55;if(u>=97&&u<=102)return u-87;e(!1,"Invalid character in "+y)}function l(y,s,u){var _=a(y,u);return u-1>=s&&(_|=a(y,u-1)<<4),_}n.prototype._parseHex=function(s,u,_){this.length=Math.ceil((s.length-u)/6),this.words=new Array(this.length);for(var p=0;p<this.length;p++)this.words[p]=0;var g=0,v=0,C;if(_==="be")for(p=s.length-1;p>=u;p-=2)C=l(s,u,p)<<g,this.words[v]|=C&67108863,g>=18?(g-=18,v+=1,this.words[v]|=C>>>26):g+=8;else{var m=s.length-u;for(p=m%2===0?u+1:u;p<s.length;p+=2)C=l(s,u,p)<<g,this.words[v]|=C&67108863,g>=18?(g-=18,v+=1,this.words[v]|=C>>>26):g+=8}this._strip()};function d(y,s,u,_){for(var p=0,g=0,v=Math.min(y.length,u),C=s;C<v;C++){var m=y.charCodeAt(C)-48;p*=_,m>=49?g=m-49+10:m>=17?g=m-17+10:g=m,e(m>=0&&g<_,"Invalid character"),p+=g}return p}n.prototype._parseBase=function(s,u,_){this.words=[0],this.length=1;for(var p=0,g=1;g<=67108863;g*=u)p++;p--,g=g/u|0;for(var v=s.length-_,C=v%p,m=Math.min(v,v-C)+_,c=0,w=_;w<m;w+=p)c=d(s,w,w+p,u),this.imuln(g),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c);if(C!==0){var A=1;for(c=d(s,w,s.length,u),w=0;w<C;w++)A*=u;this.imuln(A),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c)}this._strip()},n.prototype.copy=function(s){s.words=new Array(this.length);for(var u=0;u<this.length;u++)s.words[u]=this.words[u];s.length=this.length,s.negative=this.negative,s.red=this.red};function f(y,s){y.words=s.words,y.length=s.length,y.negative=s.negative,y.red=s.red}if(n.prototype._move=function(s){f(s,this)},n.prototype.clone=function(){var s=new n(null);return this.copy(s),s},n.prototype._expand=function(s){for(;this.length<s;)this.words[this.length++]=0;return this},n.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},n.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{n.prototype[Symbol.for("nodejs.util.inspect.custom")]=h}catch{n.prototype.inspect=h}else n.prototype.inspect=h;function h(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var b=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],P=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],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 p=0,g=0,v=0;v<this.length;v++){var C=this.words[v],m=((C<<p|g)&16777215).toString(16);g=C>>>24-p&16777215,p+=2,p>=26&&(p-=26,v--),g!==0||v!==this.length-1?_=b[6-m.length]+m+_:_=m+_}for(g!==0&&(_=g.toString(16)+_);_.length%u!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}if(s===(s|0)&&s>=2&&s<=36){var c=P[s],w=M[s];_="";var A=this.clone();for(A.negative=0;!A.isZero();){var x=A.modrn(w).toString(s);A=A.idivn(w),A.isZero()?_=x+_:_=b[c-x.length]+x+_}for(this.isZero()&&(_="0"+_);_.length%u!==0;)_="0"+_;return this.negative!==0&&(_="-"+_),_}e(!1,"Base should be between 2 and 36")},n.prototype.toNumber=function(){var s=this.words[0];return this.length===2?s+=this.words[1]*67108864:this.length===3&&this.words[2]===1?s+=4503599627370496+this.words[1]*67108864:this.length>2&&e(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-s:s},n.prototype.toJSON=function(){return this.toString(16,2)},r&&(n.prototype.toBuffer=function(s,u){return this.toArrayLike(r,s,u)}),n.prototype.toArray=function(s,u){return this.toArrayLike(Array,s,u)};var T=function(s,u){return s.allocUnsafe?s.allocUnsafe(u):new s(u)};n.prototype.toArrayLike=function(s,u,_){this._strip();var p=this.byteLength(),g=_||Math.max(1,p);e(p<=g,"byte array longer than desired length"),e(g>0,"Requested array length <= 0");var v=T(s,g),C=u==="le"?"LE":"BE";return this["_toArrayLike"+C](v,p),v},n.prototype._toArrayLikeLE=function(s,u){for(var _=0,p=0,g=0,v=0;g<this.length;g++){var C=this.words[g]<<v|p;s[_++]=C&255,_<s.length&&(s[_++]=C>>8&255),_<s.length&&(s[_++]=C>>16&255),v===6?(_<s.length&&(s[_++]=C>>24&255),p=0,v=0):(p=C>>>24,v+=2)}if(_<s.length)for(s[_++]=p;_<s.length;)s[_++]=0},n.prototype._toArrayLikeBE=function(s,u){for(var _=s.length-1,p=0,g=0,v=0;g<this.length;g++){var C=this.words[g]<<v|p;s[_--]=C&255,_>=0&&(s[_--]=C>>8&255),_>=0&&(s[_--]=C>>16&255),v===6?(_>=0&&(s[_--]=C>>24&255),p=0,v=0):(p=C>>>24,v+=2)}if(_>=0)for(s[_--]=p;_>=0;)s[_--]=0},Math.clz32?n.prototype._countBits=function(s){return 32-Math.clz32(s)}:n.prototype._countBits=function(s){var u=s,_=0;return u>=4096&&(_+=13,u>>>=13),u>=64&&(_+=7,u>>>=7),u>=8&&(_+=4,u>>>=4),u>=2&&(_+=2,u>>>=2),_+u},n.prototype._zeroBits=function(s){if(s===0)return 26;var u=s,_=0;return(u&8191)===0&&(_+=13,u>>>=13),(u&127)===0&&(_+=7,u>>>=7),(u&15)===0&&(_+=4,u>>>=4),(u&3)===0&&(_+=2,u>>>=2),(u&1)===0&&_++,_},n.prototype.bitLength=function(){var s=this.words[this.length-1],u=this._countBits(s);return(this.length-1)*26+u};function S(y){for(var s=new Array(y.bitLength()),u=0;u<s.length;u++){var _=u/26|0,p=u%26;s[u]=y.words[_]>>>p&1}return s}n.prototype.zeroBits=function(){if(this.isZero())return 0;for(var s=0,u=0;u<this.length;u++){var _=this._zeroBits(this.words[u]);if(s+=_,_!==26)break}return s},n.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},n.prototype.toTwos=function(s){return this.negative!==0?this.abs().inotn(s).iaddn(1):this.clone()},n.prototype.fromTwos=function(s){return this.testn(s-1)?this.notn(s).iaddn(1).ineg():this.clone()},n.prototype.isNeg=function(){return this.negative!==0},n.prototype.neg=function(){return this.clone().ineg()},n.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},n.prototype.iuor=function(s){for(;this.length<s.length;)this.words[this.length++]=0;for(var u=0;u<s.length;u++)this.words[u]=this.words[u]|s.words[u];return this._strip()},n.prototype.ior=function(s){return e((this.negative|s.negative)===0),this.iuor(s)},n.prototype.or=function(s){return this.length>s.length?this.clone().ior(s):s.clone().ior(this)},n.prototype.uor=function(s){return this.length>s.length?this.clone().iuor(s):s.clone().iuor(this)},n.prototype.iuand=function(s){var u;this.length>s.length?u=s:u=this;for(var _=0;_<u.length;_++)this.words[_]=this.words[_]&s.words[_];return this.length=u.length,this._strip()},n.prototype.iand=function(s){return e((this.negative|s.negative)===0),this.iuand(s)},n.prototype.and=function(s){return this.length>s.length?this.clone().iand(s):s.clone().iand(this)},n.prototype.uand=function(s){return this.length>s.length?this.clone().iuand(s):s.clone().iuand(this)},n.prototype.iuxor=function(s){var u,_;this.length>s.length?(u=this,_=s):(u=s,_=this);for(var p=0;p<_.length;p++)this.words[p]=u.words[p]^_.words[p];if(this!==u)for(;p<u.length;p++)this.words[p]=u.words[p];return this.length=u.length,this._strip()},n.prototype.ixor=function(s){return e((this.negative|s.negative)===0),this.iuxor(s)},n.prototype.xor=function(s){return this.length>s.length?this.clone().ixor(s):s.clone().ixor(this)},n.prototype.uxor=function(s){return this.length>s.length?this.clone().iuxor(s):s.clone().iuxor(this)},n.prototype.inotn=function(s){e(typeof s=="number"&&s>=0);var u=Math.ceil(s/26)|0,_=s%26;this._expand(u),_>0&&u--;for(var p=0;p<u;p++)this.words[p]=~this.words[p]&67108863;return _>0&&(this.words[p]=~this.words[p]&67108863>>26-_),this._strip()},n.prototype.notn=function(s){return this.clone().inotn(s)},n.prototype.setn=function(s,u){e(typeof s=="number"&&s>=0);var _=s/26|0,p=s%26;return this._expand(_+1),u?this.words[_]=this.words[_]|1<<p:this.words[_]=this.words[_]&~(1<<p),this._strip()},n.prototype.iadd=function(s){var u;if(this.negative!==0&&s.negative===0)return this.negative=0,u=this.isub(s),this.negative^=1,this._normSign();if(this.negative===0&&s.negative!==0)return s.negative=0,u=this.isub(s),s.negative=1,u._normSign();var _,p;this.length>s.length?(_=this,p=s):(_=s,p=this);for(var g=0,v=0;v<p.length;v++)u=(_.words[v]|0)+(p.words[v]|0)+g,this.words[v]=u&67108863,g=u>>>26;for(;g!==0&&v<_.length;v++)u=(_.words[v]|0)+g,this.words[v]=u&67108863,g=u>>>26;if(this.length=_.length,g!==0)this.words[this.length]=g,this.length++;else if(_!==this)for(;v<_.length;v++)this.words[v]=_.words[v];return this},n.prototype.add=function(s){var u;return s.negative!==0&&this.negative===0?(s.negative=0,u=this.sub(s),s.negative^=1,u):s.negative===0&&this.negative!==0?(this.negative=0,u=s.sub(this),this.negative=1,u):this.length>s.length?this.clone().iadd(s):s.clone().iadd(this)},n.prototype.isub=function(s){if(s.negative!==0){s.negative=0;var u=this.iadd(s);return s.negative=1,u._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(s),this.negative=1,this._normSign();var _=this.cmp(s);if(_===0)return this.negative=0,this.length=1,this.words[0]=0,this;var p,g;_>0?(p=this,g=s):(p=s,g=this);for(var v=0,C=0;C<g.length;C++)u=(p.words[C]|0)-(g.words[C]|0)+v,v=u>>26,this.words[C]=u&67108863;for(;v!==0&&C<p.length;C++)u=(p.words[C]|0)+v,v=u>>26,this.words[C]=u&67108863;if(v===0&&C<p.length&&p!==this)for(;C<p.length;C++)this.words[C]=p.words[C];return this.length=Math.max(this.length,C),p!==this&&(this.negative=1),this._strip()},n.prototype.sub=function(s){return this.clone().isub(s)};function L(y,s,u){u.negative=s.negative^y.negative;var _=y.length+s.length|0;u.length=_,_=_-1|0;var p=y.words[0]|0,g=s.words[0]|0,v=p*g,C=v&67108863,m=v/67108864|0;u.words[0]=C;for(var c=1;c<_;c++){for(var w=m>>>26,A=m&67108863,x=Math.min(c,s.length-1),E=Math.max(0,c-y.length+1);E<=x;E++){var q=c-E|0;p=y.words[q]|0,g=s.words[E]|0,v=p*g+A,w+=v/67108864|0,A=v&67108863}u.words[c]=A|0,m=w|0}return m!==0?u.words[c]=m|0:u.length--,u._strip()}var j=function(s,u,_){var p=s.words,g=u.words,v=_.words,C=0,m,c,w,A=p[0]|0,x=A&8191,E=A>>>13,q=p[1]|0,U=q&8191,K=q>>>13,zt=p[2]|0,H=zt&8191,G=zt>>>13,ve=p[3]|0,Q=ve&8191,J=ve>>>13,Qe=p[4]|0,X=Qe&8191,Y=Qe>>>13,Tn=p[5]|0,tt=Tn&8191,et=Tn>>>13,An=p[6]|0,it=An&8191,nt=An>>>13,Sn=p[7]|0,ot=Sn&8191,rt=Sn>>>13,xn=p[8]|0,st=xn&8191,at=xn>>>13,In=p[9]|0,ct=In&8191,lt=In>>>13,En=g[0]|0,ut=En&8191,dt=En>>>13,On=g[1]|0,ft=On&8191,_t=On>>>13,Rn=g[2]|0,ht=Rn&8191,pt=Rn>>>13,Fn=g[3]|0,mt=Fn&8191,gt=Fn>>>13,Bn=g[4]|0,bt=Bn&8191,wt=Bn>>>13,Nn=g[5]|0,yt=Nn&8191,vt=Nn>>>13,jn=g[6]|0,Pt=jn&8191,Ct=jn>>>13,qn=g[7]|0,Mt=qn&8191,kt=qn>>>13,Dn=g[8]|0,Tt=Dn&8191,At=Dn>>>13,Ln=g[9]|0,St=Ln&8191,xt=Ln>>>13;_.negative=s.negative^u.negative,_.length=19,m=Math.imul(x,ut),c=Math.imul(x,dt),c=c+Math.imul(E,ut)|0,w=Math.imul(E,dt);var Ii=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Ii>>>26)|0,Ii&=67108863,m=Math.imul(U,ut),c=Math.imul(U,dt),c=c+Math.imul(K,ut)|0,w=Math.imul(K,dt),m=m+Math.imul(x,ft)|0,c=c+Math.imul(x,_t)|0,c=c+Math.imul(E,ft)|0,w=w+Math.imul(E,_t)|0;var Ei=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Ei>>>26)|0,Ei&=67108863,m=Math.imul(H,ut),c=Math.imul(H,dt),c=c+Math.imul(G,ut)|0,w=Math.imul(G,dt),m=m+Math.imul(U,ft)|0,c=c+Math.imul(U,_t)|0,c=c+Math.imul(K,ft)|0,w=w+Math.imul(K,_t)|0,m=m+Math.imul(x,ht)|0,c=c+Math.imul(x,pt)|0,c=c+Math.imul(E,ht)|0,w=w+Math.imul(E,pt)|0;var Oi=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Oi>>>26)|0,Oi&=67108863,m=Math.imul(Q,ut),c=Math.imul(Q,dt),c=c+Math.imul(J,ut)|0,w=Math.imul(J,dt),m=m+Math.imul(H,ft)|0,c=c+Math.imul(H,_t)|0,c=c+Math.imul(G,ft)|0,w=w+Math.imul(G,_t)|0,m=m+Math.imul(U,ht)|0,c=c+Math.imul(U,pt)|0,c=c+Math.imul(K,ht)|0,w=w+Math.imul(K,pt)|0,m=m+Math.imul(x,mt)|0,c=c+Math.imul(x,gt)|0,c=c+Math.imul(E,mt)|0,w=w+Math.imul(E,gt)|0;var Ri=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Ri>>>26)|0,Ri&=67108863,m=Math.imul(X,ut),c=Math.imul(X,dt),c=c+Math.imul(Y,ut)|0,w=Math.imul(Y,dt),m=m+Math.imul(Q,ft)|0,c=c+Math.imul(Q,_t)|0,c=c+Math.imul(J,ft)|0,w=w+Math.imul(J,_t)|0,m=m+Math.imul(H,ht)|0,c=c+Math.imul(H,pt)|0,c=c+Math.imul(G,ht)|0,w=w+Math.imul(G,pt)|0,m=m+Math.imul(U,mt)|0,c=c+Math.imul(U,gt)|0,c=c+Math.imul(K,mt)|0,w=w+Math.imul(K,gt)|0,m=m+Math.imul(x,bt)|0,c=c+Math.imul(x,wt)|0,c=c+Math.imul(E,bt)|0,w=w+Math.imul(E,wt)|0;var Fi=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Fi>>>26)|0,Fi&=67108863,m=Math.imul(tt,ut),c=Math.imul(tt,dt),c=c+Math.imul(et,ut)|0,w=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,w=w+Math.imul(Y,_t)|0,m=m+Math.imul(Q,ht)|0,c=c+Math.imul(Q,pt)|0,c=c+Math.imul(J,ht)|0,w=w+Math.imul(J,pt)|0,m=m+Math.imul(H,mt)|0,c=c+Math.imul(H,gt)|0,c=c+Math.imul(G,mt)|0,w=w+Math.imul(G,gt)|0,m=m+Math.imul(U,bt)|0,c=c+Math.imul(U,wt)|0,c=c+Math.imul(K,bt)|0,w=w+Math.imul(K,wt)|0,m=m+Math.imul(x,yt)|0,c=c+Math.imul(x,vt)|0,c=c+Math.imul(E,yt)|0,w=w+Math.imul(E,vt)|0;var Bi=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Bi>>>26)|0,Bi&=67108863,m=Math.imul(it,ut),c=Math.imul(it,dt),c=c+Math.imul(nt,ut)|0,w=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,w=w+Math.imul(et,_t)|0,m=m+Math.imul(X,ht)|0,c=c+Math.imul(X,pt)|0,c=c+Math.imul(Y,ht)|0,w=w+Math.imul(Y,pt)|0,m=m+Math.imul(Q,mt)|0,c=c+Math.imul(Q,gt)|0,c=c+Math.imul(J,mt)|0,w=w+Math.imul(J,gt)|0,m=m+Math.imul(H,bt)|0,c=c+Math.imul(H,wt)|0,c=c+Math.imul(G,bt)|0,w=w+Math.imul(G,wt)|0,m=m+Math.imul(U,yt)|0,c=c+Math.imul(U,vt)|0,c=c+Math.imul(K,yt)|0,w=w+Math.imul(K,vt)|0,m=m+Math.imul(x,Pt)|0,c=c+Math.imul(x,Ct)|0,c=c+Math.imul(E,Pt)|0,w=w+Math.imul(E,Ct)|0;var Ni=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Ni>>>26)|0,Ni&=67108863,m=Math.imul(ot,ut),c=Math.imul(ot,dt),c=c+Math.imul(rt,ut)|0,w=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,w=w+Math.imul(nt,_t)|0,m=m+Math.imul(tt,ht)|0,c=c+Math.imul(tt,pt)|0,c=c+Math.imul(et,ht)|0,w=w+Math.imul(et,pt)|0,m=m+Math.imul(X,mt)|0,c=c+Math.imul(X,gt)|0,c=c+Math.imul(Y,mt)|0,w=w+Math.imul(Y,gt)|0,m=m+Math.imul(Q,bt)|0,c=c+Math.imul(Q,wt)|0,c=c+Math.imul(J,bt)|0,w=w+Math.imul(J,wt)|0,m=m+Math.imul(H,yt)|0,c=c+Math.imul(H,vt)|0,c=c+Math.imul(G,yt)|0,w=w+Math.imul(G,vt)|0,m=m+Math.imul(U,Pt)|0,c=c+Math.imul(U,Ct)|0,c=c+Math.imul(K,Pt)|0,w=w+Math.imul(K,Ct)|0,m=m+Math.imul(x,Mt)|0,c=c+Math.imul(x,kt)|0,c=c+Math.imul(E,Mt)|0,w=w+Math.imul(E,kt)|0;var ji=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(ji>>>26)|0,ji&=67108863,m=Math.imul(st,ut),c=Math.imul(st,dt),c=c+Math.imul(at,ut)|0,w=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,w=w+Math.imul(rt,_t)|0,m=m+Math.imul(it,ht)|0,c=c+Math.imul(it,pt)|0,c=c+Math.imul(nt,ht)|0,w=w+Math.imul(nt,pt)|0,m=m+Math.imul(tt,mt)|0,c=c+Math.imul(tt,gt)|0,c=c+Math.imul(et,mt)|0,w=w+Math.imul(et,gt)|0,m=m+Math.imul(X,bt)|0,c=c+Math.imul(X,wt)|0,c=c+Math.imul(Y,bt)|0,w=w+Math.imul(Y,wt)|0,m=m+Math.imul(Q,yt)|0,c=c+Math.imul(Q,vt)|0,c=c+Math.imul(J,yt)|0,w=w+Math.imul(J,vt)|0,m=m+Math.imul(H,Pt)|0,c=c+Math.imul(H,Ct)|0,c=c+Math.imul(G,Pt)|0,w=w+Math.imul(G,Ct)|0,m=m+Math.imul(U,Mt)|0,c=c+Math.imul(U,kt)|0,c=c+Math.imul(K,Mt)|0,w=w+Math.imul(K,kt)|0,m=m+Math.imul(x,Tt)|0,c=c+Math.imul(x,At)|0,c=c+Math.imul(E,Tt)|0,w=w+Math.imul(E,At)|0;var qi=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(qi>>>26)|0,qi&=67108863,m=Math.imul(ct,ut),c=Math.imul(ct,dt),c=c+Math.imul(lt,ut)|0,w=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,w=w+Math.imul(at,_t)|0,m=m+Math.imul(ot,ht)|0,c=c+Math.imul(ot,pt)|0,c=c+Math.imul(rt,ht)|0,w=w+Math.imul(rt,pt)|0,m=m+Math.imul(it,mt)|0,c=c+Math.imul(it,gt)|0,c=c+Math.imul(nt,mt)|0,w=w+Math.imul(nt,gt)|0,m=m+Math.imul(tt,bt)|0,c=c+Math.imul(tt,wt)|0,c=c+Math.imul(et,bt)|0,w=w+Math.imul(et,wt)|0,m=m+Math.imul(X,yt)|0,c=c+Math.imul(X,vt)|0,c=c+Math.imul(Y,yt)|0,w=w+Math.imul(Y,vt)|0,m=m+Math.imul(Q,Pt)|0,c=c+Math.imul(Q,Ct)|0,c=c+Math.imul(J,Pt)|0,w=w+Math.imul(J,Ct)|0,m=m+Math.imul(H,Mt)|0,c=c+Math.imul(H,kt)|0,c=c+Math.imul(G,Mt)|0,w=w+Math.imul(G,kt)|0,m=m+Math.imul(U,Tt)|0,c=c+Math.imul(U,At)|0,c=c+Math.imul(K,Tt)|0,w=w+Math.imul(K,At)|0,m=m+Math.imul(x,St)|0,c=c+Math.imul(x,xt)|0,c=c+Math.imul(E,St)|0,w=w+Math.imul(E,xt)|0;var Di=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Di>>>26)|0,Di&=67108863,m=Math.imul(ct,ft),c=Math.imul(ct,_t),c=c+Math.imul(lt,ft)|0,w=Math.imul(lt,_t),m=m+Math.imul(st,ht)|0,c=c+Math.imul(st,pt)|0,c=c+Math.imul(at,ht)|0,w=w+Math.imul(at,pt)|0,m=m+Math.imul(ot,mt)|0,c=c+Math.imul(ot,gt)|0,c=c+Math.imul(rt,mt)|0,w=w+Math.imul(rt,gt)|0,m=m+Math.imul(it,bt)|0,c=c+Math.imul(it,wt)|0,c=c+Math.imul(nt,bt)|0,w=w+Math.imul(nt,wt)|0,m=m+Math.imul(tt,yt)|0,c=c+Math.imul(tt,vt)|0,c=c+Math.imul(et,yt)|0,w=w+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,w=w+Math.imul(Y,Ct)|0,m=m+Math.imul(Q,Mt)|0,c=c+Math.imul(Q,kt)|0,c=c+Math.imul(J,Mt)|0,w=w+Math.imul(J,kt)|0,m=m+Math.imul(H,Tt)|0,c=c+Math.imul(H,At)|0,c=c+Math.imul(G,Tt)|0,w=w+Math.imul(G,At)|0,m=m+Math.imul(U,St)|0,c=c+Math.imul(U,xt)|0,c=c+Math.imul(K,St)|0,w=w+Math.imul(K,xt)|0;var Li=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Li>>>26)|0,Li&=67108863,m=Math.imul(ct,ht),c=Math.imul(ct,pt),c=c+Math.imul(lt,ht)|0,w=Math.imul(lt,pt),m=m+Math.imul(st,mt)|0,c=c+Math.imul(st,gt)|0,c=c+Math.imul(at,mt)|0,w=w+Math.imul(at,gt)|0,m=m+Math.imul(ot,bt)|0,c=c+Math.imul(ot,wt)|0,c=c+Math.imul(rt,bt)|0,w=w+Math.imul(rt,wt)|0,m=m+Math.imul(it,yt)|0,c=c+Math.imul(it,vt)|0,c=c+Math.imul(nt,yt)|0,w=w+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,w=w+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,w=w+Math.imul(Y,kt)|0,m=m+Math.imul(Q,Tt)|0,c=c+Math.imul(Q,At)|0,c=c+Math.imul(J,Tt)|0,w=w+Math.imul(J,At)|0,m=m+Math.imul(H,St)|0,c=c+Math.imul(H,xt)|0,c=c+Math.imul(G,St)|0,w=w+Math.imul(G,xt)|0;var $i=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+($i>>>26)|0,$i&=67108863,m=Math.imul(ct,mt),c=Math.imul(ct,gt),c=c+Math.imul(lt,mt)|0,w=Math.imul(lt,gt),m=m+Math.imul(st,bt)|0,c=c+Math.imul(st,wt)|0,c=c+Math.imul(at,bt)|0,w=w+Math.imul(at,wt)|0,m=m+Math.imul(ot,yt)|0,c=c+Math.imul(ot,vt)|0,c=c+Math.imul(rt,yt)|0,w=w+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,w=w+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,w=w+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,w=w+Math.imul(Y,At)|0,m=m+Math.imul(Q,St)|0,c=c+Math.imul(Q,xt)|0,c=c+Math.imul(J,St)|0,w=w+Math.imul(J,xt)|0;var Ui=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Ui>>>26)|0,Ui&=67108863,m=Math.imul(ct,bt),c=Math.imul(ct,wt),c=c+Math.imul(lt,bt)|0,w=Math.imul(lt,wt),m=m+Math.imul(st,yt)|0,c=c+Math.imul(st,vt)|0,c=c+Math.imul(at,yt)|0,w=w+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,w=w+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,w=w+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,w=w+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,w=w+Math.imul(Y,xt)|0;var Vi=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Vi>>>26)|0,Vi&=67108863,m=Math.imul(ct,yt),c=Math.imul(ct,vt),c=c+Math.imul(lt,yt)|0,w=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,w=w+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,w=w+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,w=w+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,w=w+Math.imul(et,xt)|0;var Ki=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Ki>>>26)|0,Ki&=67108863,m=Math.imul(ct,Pt),c=Math.imul(ct,Ct),c=c+Math.imul(lt,Pt)|0,w=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,w=w+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,w=w+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,w=w+Math.imul(nt,xt)|0;var Hi=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Hi>>>26)|0,Hi&=67108863,m=Math.imul(ct,Mt),c=Math.imul(ct,kt),c=c+Math.imul(lt,Mt)|0,w=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,w=w+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,w=w+Math.imul(rt,xt)|0;var Wi=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Wi>>>26)|0,Wi&=67108863,m=Math.imul(ct,Tt),c=Math.imul(ct,At),c=c+Math.imul(lt,Tt)|0,w=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,w=w+Math.imul(at,xt)|0;var Zi=(C+m|0)+((c&8191)<<13)|0;C=(w+(c>>>13)|0)+(Zi>>>26)|0,Zi&=67108863,m=Math.imul(ct,St),c=Math.imul(ct,xt),c=c+Math.imul(lt,St)|0,w=Math.imul(lt,xt);var zi=(C+m|0)+((c&8191)<<13)|0;return C=(w+(c>>>13)|0)+(zi>>>26)|0,zi&=67108863,v[0]=Ii,v[1]=Ei,v[2]=Oi,v[3]=Ri,v[4]=Fi,v[5]=Bi,v[6]=Ni,v[7]=ji,v[8]=qi,v[9]=Di,v[10]=Li,v[11]=$i,v[12]=Ui,v[13]=Vi,v[14]=Ki,v[15]=Hi,v[16]=Wi,v[17]=Zi,v[18]=zi,C!==0&&(v[19]=C,_.length++),_};Math.imul||(j=L);function V(y,s,u){u.negative=s.negative^y.negative,u.length=y.length+s.length;for(var _=0,p=0,g=0;g<u.length-1;g++){var v=p;p=0;for(var C=_&67108863,m=Math.min(g,s.length-1),c=Math.max(0,g-y.length+1);c<=m;c++){var w=g-c,A=y.words[w]|0,x=s.words[c]|0,E=A*x,q=E&67108863;v=v+(E/67108864|0)|0,q=q+C|0,C=q&67108863,v=v+(q>>>26)|0,p+=v>>>26,v&=67108863}u.words[g]=C,_=v,v=p}return _!==0?u.words[g]=_:u.length--,u._strip()}function B(y,s,u){return V(y,s,u)}n.prototype.mulTo=function(s,u){var _,p=this.length+s.length;return this.length===10&&s.length===10?_=j(this,s,u):p<63?_=L(this,s,u):p<1024?_=V(this,s,u):_=B(this,s,u),_};function D(y,s){this.x=y,this.y=s}D.prototype.makeRBT=function(s){for(var u=new Array(s),_=n.prototype._countBits(s)-1,p=0;p<s;p++)u[p]=this.revBin(p,_,s);return u},D.prototype.revBin=function(s,u,_){if(s===0||s===_-1)return s;for(var p=0,g=0;g<u;g++)p|=(s&1)<<u-g-1,s>>=1;return p},D.prototype.permute=function(s,u,_,p,g,v){for(var C=0;C<v;C++)p[C]=u[s[C]],g[C]=_[s[C]]},D.prototype.transform=function(s,u,_,p,g,v){this.permute(v,s,u,_,p,g);for(var C=1;C<g;C<<=1)for(var m=C<<1,c=Math.cos(2*Math.PI/m),w=Math.sin(2*Math.PI/m),A=0;A<g;A+=m)for(var x=c,E=w,q=0;q<C;q++){var U=_[A+q],K=p[A+q],zt=_[A+q+C],H=p[A+q+C],G=x*zt-E*H;H=x*H+E*zt,zt=G,_[A+q]=U+zt,p[A+q]=K+H,_[A+q+C]=U-zt,p[A+q+C]=K-H,q!==m&&(G=c*x-w*E,E=c*E+w*x,x=G)}},D.prototype.guessLen13b=function(s,u){var _=Math.max(u,s)|1,p=_&1,g=0;for(_=_/2|0;_;_=_>>>1)g++;return 1<<g+1+p},D.prototype.conjugate=function(s,u,_){if(!(_<=1))for(var p=0;p<_/2;p++){var g=s[p];s[p]=s[_-p-1],s[_-p-1]=g,g=u[p],u[p]=-u[_-p-1],u[_-p-1]=-g}},D.prototype.normalize13b=function(s,u){for(var _=0,p=0;p<u/2;p++){var g=Math.round(s[2*p+1]/u)*8192+Math.round(s[2*p]/u)+_;s[p]=g&67108863,g<67108864?_=0:_=g/67108864|0}return s},D.prototype.convert13b=function(s,u,_,p){for(var g=0,v=0;v<u;v++)g=g+(s[v]|0),_[2*v]=g&8191,g=g>>>13,_[2*v+1]=g&8191,g=g>>>13;for(v=2*u;v<p;++v)_[v]=0;e(g===0),e((g&-8192)===0)},D.prototype.stub=function(s){for(var u=new Array(s),_=0;_<s;_++)u[_]=0;return u},D.prototype.mulp=function(s,u,_){var p=2*this.guessLen13b(s.length,u.length),g=this.makeRBT(p),v=this.stub(p),C=new Array(p),m=new Array(p),c=new Array(p),w=new Array(p),A=new Array(p),x=new Array(p),E=_.words;E.length=p,this.convert13b(s.words,s.length,C,p),this.convert13b(u.words,u.length,w,p),this.transform(C,v,m,c,p,g),this.transform(w,v,A,x,p,g);for(var q=0;q<p;q++){var U=m[q]*A[q]-c[q]*x[q];c[q]=m[q]*x[q]+c[q]*A[q],m[q]=U}return this.conjugate(m,c,p),this.transform(m,c,E,v,p,g),this.conjugate(E,v,p),this.normalize13b(E,p),_.negative=s.negative^u.negative,_.length=s.length+u.length,_._strip()},n.prototype.mul=function(s){var u=new n(null);return u.words=new Array(this.length+s.length),this.mulTo(s,u)},n.prototype.mulf=function(s){var u=new n(null);return u.words=new Array(this.length+s.length),B(this,s,u)},n.prototype.imul=function(s){return this.clone().mulTo(s,this)},n.prototype.imuln=function(s){var u=s<0;u&&(s=-s),e(typeof s=="number"),e(s<67108864);for(var _=0,p=0;p<this.length;p++){var g=(this.words[p]|0)*s,v=(g&67108863)+(_&67108863);_>>=26,_+=g/67108864|0,_+=v>>>26,this.words[p]=v&67108863}return _!==0&&(this.words[p]=_,this.length++),this.length=s===0?1:this.length,u?this.ineg():this},n.prototype.muln=function(s){return this.clone().imuln(s)},n.prototype.sqr=function(){return this.mul(this)},n.prototype.isqr=function(){return this.imul(this.clone())},n.prototype.pow=function(s){var u=S(s);if(u.length===0)return new n(1);for(var _=this,p=0;p<u.length&&u[p]===0;p++,_=_.sqr());if(++p<u.length)for(var g=_.sqr();p<u.length;p++,g=g.sqr())u[p]!==0&&(_=_.mul(g));return _},n.prototype.iushln=function(s){e(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26,p=67108863>>>26-u<<26-u,g;if(u!==0){var v=0;for(g=0;g<this.length;g++){var C=this.words[g]&p,m=(this.words[g]|0)-C<<u;this.words[g]=m|v,v=C>>>26-u}v&&(this.words[g]=v,this.length++)}if(_!==0){for(g=this.length-1;g>=0;g--)this.words[g+_]=this.words[g];for(g=0;g<_;g++)this.words[g]=0;this.length+=_}return this._strip()},n.prototype.ishln=function(s){return e(this.negative===0),this.iushln(s)},n.prototype.iushrn=function(s,u,_){e(typeof s=="number"&&s>=0);var p;u?p=(u-u%26)/26:p=0;var g=s%26,v=Math.min((s-g)/26,this.length),C=67108863^67108863>>>g<<g,m=_;if(p-=v,p=Math.max(0,p),m){for(var c=0;c<v;c++)m.words[c]=this.words[c];m.length=v}if(v!==0)if(this.length>v)for(this.length-=v,c=0;c<this.length;c++)this.words[c]=this.words[c+v];else this.words[0]=0,this.length=1;var w=0;for(c=this.length-1;c>=0&&(w!==0||c>=p);c--){var A=this.words[c]|0;this.words[c]=w<<26-g|A>>>g,w=A&C}return m&&w!==0&&(m.words[m.length++]=w),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},n.prototype.ishrn=function(s,u,_){return e(this.negative===0),this.iushrn(s,u,_)},n.prototype.shln=function(s){return this.clone().ishln(s)},n.prototype.ushln=function(s){return this.clone().iushln(s)},n.prototype.shrn=function(s){return this.clone().ishrn(s)},n.prototype.ushrn=function(s){return this.clone().iushrn(s)},n.prototype.testn=function(s){e(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26,p=1<<u;if(this.length<=_)return!1;var g=this.words[_];return!!(g&p)},n.prototype.imaskn=function(s){e(typeof s=="number"&&s>=0);var u=s%26,_=(s-u)/26;if(e(this.negative===0,"imaskn works only with positive numbers"),this.length<=_)return this;if(u!==0&&_++,this.length=Math.min(_,this.length),u!==0){var p=67108863^67108863>>>u<<u;this.words[this.length-1]&=p}return this._strip()},n.prototype.maskn=function(s){return this.clone().imaskn(s)},n.prototype.iaddn=function(s){return e(typeof s=="number"),e(s<67108864),s<0?this.isubn(-s):this.negative!==0?this.length===1&&(this.words[0]|0)<=s?(this.words[0]=s-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(s),this.negative=1,this):this._iaddn(s)},n.prototype._iaddn=function(s){this.words[0]+=s;for(var u=0;u<this.length&&this.words[u]>=67108864;u++)this.words[u]-=67108864,u===this.length-1?this.words[u+1]=1:this.words[u+1]++;return this.length=Math.max(this.length,u+1),this},n.prototype.isubn=function(s){if(e(typeof s=="number"),e(s<67108864),s<0)return this.iaddn(-s);if(this.negative!==0)return this.negative=0,this.iaddn(s),this.negative=1,this;if(this.words[0]-=s,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var u=0;u<this.length&&this.words[u]<0;u++)this.words[u]+=67108864,this.words[u+1]-=1;return this._strip()},n.prototype.addn=function(s){return this.clone().iaddn(s)},n.prototype.subn=function(s){return this.clone().isubn(s)},n.prototype.iabs=function(){return this.negative=0,this},n.prototype.abs=function(){return this.clone().iabs()},n.prototype._ishlnsubmul=function(s,u,_){var p=s.length+_,g;this._expand(p);var v,C=0;for(g=0;g<s.length;g++){v=(this.words[g+_]|0)+C;var m=(s.words[g]|0)*u;v-=m&67108863,C=(v>>26)-(m/67108864|0),this.words[g+_]=v&67108863}for(;g<this.length-_;g++)v=(this.words[g+_]|0)+C,C=v>>26,this.words[g+_]=v&67108863;if(C===0)return this._strip();for(e(C===-1),C=0,g=0;g<this.length;g++)v=-(this.words[g]|0)+C,C=v>>26,this.words[g]=v&67108863;return this.negative=1,this._strip()},n.prototype._wordDiv=function(s,u){var _=this.length-s.length,p=this.clone(),g=s,v=g.words[g.length-1]|0,C=this._countBits(v);_=26-C,_!==0&&(g=g.ushln(_),p.iushln(_),v=g.words[g.length-1]|0);var m=p.length-g.length,c;if(u!=="mod"){c=new n(null),c.length=m+1,c.words=new Array(c.length);for(var w=0;w<c.length;w++)c.words[w]=0}var A=p.clone()._ishlnsubmul(g,1,m);A.negative===0&&(p=A,c&&(c.words[m]=1));for(var x=m-1;x>=0;x--){var E=(p.words[g.length+x]|0)*67108864+(p.words[g.length+x-1]|0);for(E=Math.min(E/v|0,67108863),p._ishlnsubmul(g,E,x);p.negative!==0;)E--,p.negative=0,p._ishlnsubmul(g,1,x),p.isZero()||(p.negative^=1);c&&(c.words[x]=E)}return c&&c._strip(),p._strip(),u!=="div"&&_!==0&&p.iushrn(_),{div:c||null,mod:p}},n.prototype.divmod=function(s,u,_){if(e(!s.isZero()),this.isZero())return{div:new n(0),mod:new n(0)};var p,g,v;return this.negative!==0&&s.negative===0?(v=this.neg().divmod(s,u),u!=="mod"&&(p=v.div.neg()),u!=="div"&&(g=v.mod.neg(),_&&g.negative!==0&&g.iadd(s)),{div:p,mod:g}):this.negative===0&&s.negative!==0?(v=this.divmod(s.neg(),u),u!=="mod"&&(p=v.div.neg()),{div:p,mod:v.mod}):(this.negative&s.negative)!==0?(v=this.neg().divmod(s.neg(),u),u!=="div"&&(g=v.mod.neg(),_&&g.negative!==0&&g.isub(s)),{div:v.div,mod:g}):s.length>this.length||this.cmp(s)<0?{div:new n(0),mod:this}:s.length===1?u==="div"?{div:this.divn(s.words[0]),mod:null}:u==="mod"?{div:null,mod:new n(this.modrn(s.words[0]))}:{div:this.divn(s.words[0]),mod:new n(this.modrn(s.words[0]))}:this._wordDiv(s,u)},n.prototype.div=function(s){return this.divmod(s,"div",!1).div},n.prototype.mod=function(s){return this.divmod(s,"mod",!1).mod},n.prototype.umod=function(s){return this.divmod(s,"mod",!0).mod},n.prototype.divRound=function(s){var u=this.divmod(s);if(u.mod.isZero())return u.div;var _=u.div.negative!==0?u.mod.isub(s):u.mod,p=s.ushrn(1),g=s.andln(1),v=_.cmp(p);return v<0||g===1&&v===0?u.div:u.div.negative!==0?u.div.isubn(1):u.div.iaddn(1)},n.prototype.modrn=function(s){var u=s<0;u&&(s=-s),e(s<=67108863);for(var _=(1<<26)%s,p=0,g=this.length-1;g>=0;g--)p=(_*p+(this.words[g]|0))%s;return u?-p:p},n.prototype.modn=function(s){return this.modrn(s)},n.prototype.idivn=function(s){var u=s<0;u&&(s=-s),e(s<=67108863);for(var _=0,p=this.length-1;p>=0;p--){var g=(this.words[p]|0)+_*67108864;this.words[p]=g/s|0,_=g%s}return this._strip(),u?this.ineg():this},n.prototype.divn=function(s){return this.clone().idivn(s)},n.prototype.egcd=function(s){e(s.negative===0),e(!s.isZero());var u=this,_=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var p=new n(1),g=new n(0),v=new n(0),C=new n(1),m=0;u.isEven()&&_.isEven();)u.iushrn(1),_.iushrn(1),++m;for(var c=_.clone(),w=u.clone();!u.isZero();){for(var A=0,x=1;(u.words[0]&x)===0&&A<26;++A,x<<=1);if(A>0)for(u.iushrn(A);A-- >0;)(p.isOdd()||g.isOdd())&&(p.iadd(c),g.isub(w)),p.iushrn(1),g.iushrn(1);for(var E=0,q=1;(_.words[0]&q)===0&&E<26;++E,q<<=1);if(E>0)for(_.iushrn(E);E-- >0;)(v.isOdd()||C.isOdd())&&(v.iadd(c),C.isub(w)),v.iushrn(1),C.iushrn(1);u.cmp(_)>=0?(u.isub(_),p.isub(v),g.isub(C)):(_.isub(u),v.isub(p),C.isub(g))}return{a:v,b:C,gcd:_.iushln(m)}},n.prototype._invmp=function(s){e(s.negative===0),e(!s.isZero());var u=this,_=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var p=new n(1),g=new n(0),v=_.clone();u.cmpn(1)>0&&_.cmpn(1)>0;){for(var C=0,m=1;(u.words[0]&m)===0&&C<26;++C,m<<=1);if(C>0)for(u.iushrn(C);C-- >0;)p.isOdd()&&p.iadd(v),p.iushrn(1);for(var c=0,w=1;(_.words[0]&w)===0&&c<26;++c,w<<=1);if(c>0)for(_.iushrn(c);c-- >0;)g.isOdd()&&g.iadd(v),g.iushrn(1);u.cmp(_)>=0?(u.isub(_),p.isub(g)):(_.isub(u),g.isub(p))}var A;return u.cmpn(1)===0?A=p:A=g,A.cmpn(0)<0&&A.iadd(s),A},n.prototype.gcd=function(s){if(this.isZero())return s.abs();if(s.isZero())return this.abs();var u=this.clone(),_=s.clone();u.negative=0,_.negative=0;for(var p=0;u.isEven()&&_.isEven();p++)u.iushrn(1),_.iushrn(1);do{for(;u.isEven();)u.iushrn(1);for(;_.isEven();)_.iushrn(1);var g=u.cmp(_);if(g<0){var v=u;u=_,_=v}else if(g===0||_.cmpn(1)===0)break;u.isub(_)}while(!0);return _.iushln(p)},n.prototype.invm=function(s){return this.egcd(s).a.umod(s)},n.prototype.isEven=function(){return(this.words[0]&1)===0},n.prototype.isOdd=function(){return(this.words[0]&1)===1},n.prototype.andln=function(s){return this.words[0]&s},n.prototype.bincn=function(s){e(typeof s=="number");var u=s%26,_=(s-u)/26,p=1<<u;if(this.length<=_)return this._expand(_+1),this.words[_]|=p,this;for(var g=p,v=_;g!==0&&v<this.length;v++){var C=this.words[v]|0;C+=g,g=C>>>26,C&=67108863,this.words[v]=C}return g!==0&&(this.words[v]=g,this.length++),this},n.prototype.isZero=function(){return this.length===1&&this.words[0]===0},n.prototype.cmpn=function(s){var u=s<0;if(this.negative!==0&&!u)return-1;if(this.negative===0&&u)return 1;this._strip();var _;if(this.length>1)_=1;else{u&&(s=-s),e(s<=67108863,"Number is too big");var p=this.words[0]|0;_=p===s?0:p<s?-1:1}return this.negative!==0?-_|0:_},n.prototype.cmp=function(s){if(this.negative!==0&&s.negative===0)return-1;if(this.negative===0&&s.negative!==0)return 1;var u=this.ucmp(s);return this.negative!==0?-u|0:u},n.prototype.ucmp=function(s){if(this.length>s.length)return 1;if(this.length<s.length)return-1;for(var u=0,_=this.length-1;_>=0;_--){var p=this.words[_]|0,g=s.words[_]|0;if(p!==g){p<g?u=-1:p>g&&(u=1);break}}return u},n.prototype.gtn=function(s){return this.cmpn(s)===1},n.prototype.gt=function(s){return this.cmp(s)===1},n.prototype.gten=function(s){return this.cmpn(s)>=0},n.prototype.gte=function(s){return this.cmp(s)>=0},n.prototype.ltn=function(s){return this.cmpn(s)===-1},n.prototype.lt=function(s){return this.cmp(s)===-1},n.prototype.lten=function(s){return this.cmpn(s)<=0},n.prototype.lte=function(s){return this.cmp(s)<=0},n.prototype.eqn=function(s){return this.cmpn(s)===0},n.prototype.eq=function(s){return this.cmp(s)===0},n.red=function(s){return new $(s)},n.prototype.toRed=function(s){return e(!this.red,"Already a number in reduction context"),e(this.negative===0,"red works only with positives"),s.convertTo(this)._forceRed(s)},n.prototype.fromRed=function(){return e(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},n.prototype._forceRed=function(s){return this.red=s,this},n.prototype.forceRed=function(s){return e(!this.red,"Already a number in reduction context"),this._forceRed(s)},n.prototype.redAdd=function(s){return e(this.red,"redAdd works only with red numbers"),this.red.add(this,s)},n.prototype.redIAdd=function(s){return e(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,s)},n.prototype.redSub=function(s){return e(this.red,"redSub works only with red numbers"),this.red.sub(this,s)},n.prototype.redISub=function(s){return e(this.red,"redISub works only with red numbers"),this.red.isub(this,s)},n.prototype.redShl=function(s){return e(this.red,"redShl works only with red numbers"),this.red.shl(this,s)},n.prototype.redMul=function(s){return e(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.mul(this,s)},n.prototype.redIMul=function(s){return e(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.imul(this,s)},n.prototype.redSqr=function(){return e(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},n.prototype.redISqr=function(){return e(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},n.prototype.redSqrt=function(){return e(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},n.prototype.redInvm=function(){return e(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},n.prototype.redNeg=function(){return e(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},n.prototype.redPow=function(s){return e(this.red&&!s.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,s)};var jt={k256:null,p224:null,p192:null,p25519:null};function z(y,s){this.name=y,this.p=new n(s,16),this.n=this.p.bitLength(),this.k=new n(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}z.prototype._tmp=function(){var s=new n(null);return s.words=new Array(Math.ceil(this.n/13)),s},z.prototype.ireduce=function(s){var u=s,_;do this.split(u,this.tmp),u=this.imulK(u),u=u.iadd(this.tmp),_=u.bitLength();while(_>this.n);var p=_<this.n?-1:u.ucmp(this.p);return p===0?(u.words[0]=0,u.length=1):p>0?u.isub(this.p):u.strip!==void 0?u.strip():u._strip(),u},z.prototype.split=function(s,u){s.iushrn(this.n,0,u)},z.prototype.imulK=function(s){return s.imul(this.k)};function $t(){z.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}i($t,z),$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 ne(){z.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}i(ne,z);function Zt(){z.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}i(Zt,z);function oe(){z.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}i(oe,z),oe.prototype.imulK=function(s){for(var u=0,_=0;_<s.length;_++){var p=(s.words[_]|0)*19+u,g=p&67108863;p>>>=26,s.words[_]=g,u=p}return u!==0&&(s.words[s.length++]=u),s},n._prime=function(s){if(jt[s])return jt[s];var u;if(s==="k256")u=new $t;else if(s==="p224")u=new ne;else if(s==="p192")u=new Zt;else if(s==="p25519")u=new oe;else throw new Error("Unknown prime "+s);return jt[s]=u,u};function $(y){if(typeof y=="string"){var s=n._prime(y);this.m=s.p,this.prime=s}else e(y.gtn(1),"modulus must be greater than 1"),this.m=y,this.prime=null}$.prototype._verify1=function(s){e(s.negative===0,"red works only with positives"),e(s.red,"red works only with red numbers")},$.prototype._verify2=function(s,u){e((s.negative|u.negative)===0,"red works only with positives"),e(s.red&&s.red===u.red,"red works only with red numbers")},$.prototype.imod=function(s){return this.prime?this.prime.ireduce(s)._forceRed(this):(f(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(e(u%2===1),u===3){var _=this.m.add(new n(1)).iushrn(2);return this.pow(s,_)}for(var p=this.m.subn(1),g=0;!p.isZero()&&p.andln(1)===0;)g++,p.iushrn(1);e(!p.isZero());var v=new n(1).toRed(this),C=v.redNeg(),m=this.m.subn(1).iushrn(1),c=this.m.bitLength();for(c=new n(2*c*c).toRed(this);this.pow(c,m).cmp(C)!==0;)c.redIAdd(C);for(var w=this.pow(c,p),A=this.pow(s,p.addn(1).iushrn(1)),x=this.pow(s,p),E=g;x.cmp(v)!==0;){for(var q=x,U=0;q.cmp(v)!==0;U++)q=q.redSqr();e(U<E);var K=this.pow(w,new n(1).iushln(E-U-1));A=A.redMul(K),w=K.redSqr(),x=x.redMul(w),E=U}return A},$.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 n(1).toRed(this);if(u.cmpn(1)===0)return s.clone();var _=4,p=new Array(1<<_);p[0]=new n(1).toRed(this),p[1]=s;for(var g=2;g<p.length;g++)p[g]=this.mul(p[g-1],s);var v=p[0],C=0,m=0,c=u.bitLength()%26;for(c===0&&(c=26),g=u.length-1;g>=0;g--){for(var w=u.words[g],A=c-1;A>=0;A--){var x=w>>A&1;if(v!==p[0]&&(v=this.sqr(v)),x===0&&C===0){m=0;continue}C<<=1,C|=x,m++,!(m!==_&&(g!==0||A!==0))&&(v=this.mul(v,p[C]),m=0,C=0)}c=26}return v},$.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},n.mont=function(s){return new Bt(s)};function Bt(y){$.call(this,y),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new n(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}i(Bt,$),Bt.prototype.convertTo=function(s){return this.imod(s.ushln(this.shift))},Bt.prototype.convertFrom=function(s){var u=this.imod(s.mul(this.rinv));return u.red=null,u},Bt.prototype.imul=function(s,u){if(s.isZero()||u.isZero())return s.words[0]=0,s.length=1,s;var _=s.imul(u),p=_.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=_.isub(p).iushrn(this.shift),v=g;return g.cmp(this.m)>=0?v=g.isub(this.m):g.cmpn(0)<0&&(v=g.iadd(this.m)),v._forceRed(this)},Bt.prototype.mul=function(s,u){if(s.isZero()||u.isZero())return new n(0)._forceRed(this);var _=s.mul(u),p=_.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),g=_.isub(p).iushrn(this.shift),v=g;return g.cmp(this.m)>=0?v=g.isub(this.m):g.cmpn(0)<0&&(v=g.iadd(this.m)),v._forceRed(this)},Bt.prototype.invm=function(s){var u=this.imod(s._invmp(this.m).mul(this.r2));return u._forceRed(this)}})(typeof on>"u"||on,_o)});import{SdkWrapper as va}from"@cetusprotocol/common-sdk";import{normalizeSuiObjectId as tn}from"@mysten/sui/utils";import{CACHE_TIME_24H as Ht,CACHE_TIME_5MIN as Nr,CachedContent as jr,DETAILS_KEYS as Ue,extractStructTagFromType as en,fixCoinType as qr,getFutureTime as uo,getObjectFields as ei,getObjectId as nn,getObjectPreviousTransactionDigest as Dr,getObjectType as Lr,getPackagerConfigs as le,normalizeCoinType as fo}from"@cetusprotocol/common-sdk";var Zn="3.7.8",Mr=Zn,Ce=typeof Buffer=="function",Un=typeof TextDecoder=="function"?new TextDecoder:void 0,Vn=typeof TextEncoder=="function"?new TextEncoder:void 0,kr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",Le=Array.prototype.slice.call(kr),Je=(o=>{let t={};return o.forEach((e,i)=>t[e]=i),t})(Le),Tr=/^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/,Et=String.fromCharCode.bind(String),Kn=typeof Uint8Array.from=="function"?Uint8Array.from.bind(Uint8Array):o=>new Uint8Array(Array.prototype.slice.call(o,0)),zn=o=>o.replace(/=/g,"").replace(/[+\/]/g,t=>t=="+"?"-":"_"),Gn=o=>o.replace(/[^A-Za-z0-9\+\/]/g,""),Qn=o=>{let t,e,i,n,r="",a=o.length%3;for(let l=0;l<o.length;){if((e=o.charCodeAt(l++))>255||(i=o.charCodeAt(l++))>255||(n=o.charCodeAt(l++))>255)throw new TypeError("invalid character found");t=e<<16|i<<8|n,r+=Le[t>>18&63]+Le[t>>12&63]+Le[t>>6&63]+Le[t&63]}return a?r.slice(0,a-3)+"===".substring(a):r},Ji=typeof btoa=="function"?o=>btoa(o):Ce?o=>Buffer.from(o,"binary").toString("base64"):Qn,Gi=Ce?o=>Buffer.from(o).toString("base64"):o=>{let e=[];for(let i=0,n=o.length;i<n;i+=4096)e.push(Et.apply(null,o.subarray(i,i+4096)));return Ji(e.join(""))},Xe=(o,t=!1)=>t?zn(Gi(o)):Gi(o),Ar=o=>{if(o.length<2){var t=o.charCodeAt(0);return t<128?o:t<2048?Et(192|t>>>6)+Et(128|t&63):Et(224|t>>>12&15)+Et(128|t>>>6&63)+Et(128|t&63)}else{var t=65536+(o.charCodeAt(0)-55296)*1024+(o.charCodeAt(1)-56320);return Et(240|t>>>18&7)+Et(128|t>>>12&63)+Et(128|t>>>6&63)+Et(128|t&63)}},Sr=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g,Jn=o=>o.replace(Sr,Ar),Hn=Ce?o=>Buffer.from(o,"utf8").toString("base64"):Vn?o=>Gi(Vn.encode(o)):o=>Ji(Jn(o)),Pe=(o,t=!1)=>t?zn(Hn(o)):Hn(o),Wn=o=>Pe(o,!0),xr=/[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g,Ir=o=>{switch(o.length){case 4:var t=(7&o.charCodeAt(0))<<18|(63&o.charCodeAt(1))<<12|(63&o.charCodeAt(2))<<6|63&o.charCodeAt(3),e=t-65536;return Et((e>>>10)+55296)+Et((e&1023)+56320);case 3:return Et((15&o.charCodeAt(0))<<12|(63&o.charCodeAt(1))<<6|63&o.charCodeAt(2));default:return Et((31&o.charCodeAt(0))<<6|63&o.charCodeAt(1))}},Xn=o=>o.replace(xr,Ir),Yn=o=>{if(o=o.replace(/\s+/g,""),!Tr.test(o))throw new TypeError("malformed base64.");o+="==".slice(2-(o.length&3));let t,e,i,n=[];for(let r=0;r<o.length;)t=Je[o.charAt(r++)]<<18|Je[o.charAt(r++)]<<12|(e=Je[o.charAt(r++)])<<6|(i=Je[o.charAt(r++)]),e===64?n.push(Et(t>>16&255)):i===64?n.push(Et(t>>16&255,t>>8&255)):n.push(Et(t>>16&255,t>>8&255,t&255));return n.join("")},Xi=typeof atob=="function"?o=>atob(Gn(o)):Ce?o=>Buffer.from(o,"base64").toString("binary"):Yn,to=Ce?o=>Kn(Buffer.from(o,"base64")):o=>Kn(Xi(o).split("").map(t=>t.charCodeAt(0))),eo=o=>to(io(o)),Er=Ce?o=>Buffer.from(o,"base64").toString("utf8"):Un?o=>Un.decode(to(o)):o=>Xn(Xi(o)),io=o=>Gn(o.replace(/[-_]/g,t=>t=="-"?"+":"/")),Qi=o=>Er(io(o)),Or=o=>{if(typeof o!="string")return!1;let t=o.replace(/\s+/g,"").replace(/={0,2}$/,"");return!/[^\s0-9a-zA-Z\+/]/.test(t)||!/[^\s0-9a-zA-Z\-_]/.test(t)},no=o=>({value:o,enumerable:!1,writable:!0,configurable:!0}),oo=function(){let o=(t,e)=>Object.defineProperty(String.prototype,t,no(e));o("fromBase64",function(){return Qi(this)}),o("toBase64",function(t){return Pe(this,t)}),o("toBase64URI",function(){return Pe(this,!0)}),o("toBase64URL",function(){return Pe(this,!0)}),o("toUint8Array",function(){return eo(this)})},ro=function(){let o=(t,e)=>Object.defineProperty(Uint8Array.prototype,t,no(e));o("toBase64",function(t){return Xe(this,t)}),o("toBase64URI",function(){return Xe(this,!0)}),o("toBase64URL",function(){return Xe(this,!0)})},Rr=()=>{oo(),ro()},Ye={version:Zn,VERSION:Mr,atob:Xi,atobPolyfill:Yn,btoa:Ji,btoaPolyfill:Qn,fromBase64:Qi,toBase64:Pe,encode:Pe,encodeURI:Wn,encodeURL:Wn,utob:Jn,btou:Xn,decode:Qi,isValid:Or,fromUint8Array:Xe,toUint8Array:eo,extendString:oo,extendUint8Array:ro,extendBuiltins:Rr};import{BaseError as Fr}from"@cetusprotocol/common-sdk";var so=(f=>(f.InvalidSqrtPriceLimitDirection="InvalidSqrtPriceLimitDirection",f.ZeroTradableAmount="ZeroTradableAmount",f.AmountOutBelowMinimum="AmountOutBelowMinimum",f.AmountInAboveMaximum="AmountInAboveMaximum",f.NextTickNotFound="NextTickNotFound",f.TickArraySequenceInvalid="TickArraySequenceInvalid",f.TickArrayCrossingAboveMax="TickArrayCrossingAboveMax",f.TickArrayIndexNotInitialized="TickArrayIndexNotInitialized",f.ParamsLengthNotEqual="ParamsLengthNotEqual",f))(so||{}),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||{}),ao=(e=>(e.ClmmVestNotSet="ClmmVestNotSet",e.ClmmVestFetchError="ClmmVestFetchError",e))(ao||{}),co=(e=>(e.NotFoundPartnerObject="NotFoundPartnerObject",e.InvalidPartnerRefFeeFields="InvalidPartnerRefFeeFields",e))(co||{}),ke=(i=>(i.InvalidConfig="InvalidConfig",i.InvalidConfigHandle="InvalidConfigHandle",i.InvalidSimulateAccount="InvalidSimulateAccount",i))(ke||{}),Yi=(a=>(a.InvalidSendAddress="InvalidSendAddress",a.InvalidRecipientAddress="InvalidRecipientAddress",a.InvalidRecipientAndAmountLength="InvalidRecipientAndAmountLength",a.InsufficientBalance="InsufficientBalance",a.InvalidTarget="InvalidTarget",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a))(Yi||{}),Br=(a=>(a.InvalidCoin="InvalidCoin",a.NotFoundPath="NotFoundPath",a.NoDowngradeNeedParams="NoDowngradeNeedParams",a.InvalidSwapCountUrl="InvalidSwapCountUrl",a.InvalidTransactionBuilder="InvalidTransactionBuilder",a.InvalidServerResponse="InvalidServerResponse",a))(Br||{}),lo=(t=>(t.InvalidType="InvalidType",t))(lo||{}),Me=class extends Fr{constructor(t,e,i){super(t,e||"UnknownError",i)}static isVaultsErrorCode(t,e){return this.isErrorCode(t,e)}},qt=(o,t,e)=>{throw new Me(t.message,o,e)},R=(o,t,e)=>{throw new Me(t,o,e)};var ii=class{constructor(t){this._cache={};this._sdk=t}get sdk(){return this._sdk}setTokenListCache(t){let{coin_list_handle:e}=le(this.sdk.sdkOptions.cetus_config),i=`${e}_getCoinConfigs`,n=this.getCache(i),r=n?[...n,...t]:t;this.updateCache(i,r)}async getTokenListByCoinTypes(t){let e={},{coin_list_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getCoinConfigs`,r=this.getCache(n);if(r!==void 0){let l=r;for(let d of t)for(let f of l)if(fo(d)===fo(f.address)){e[d]=f;continue}}let a=t.filter(l=>e[l]===void 0);for(let l of a){let d=`${l}_metadata`,f=this.getCache(d);if(f!==void 0)e[l]=f;else{let h=await this._sdk.FullClient.getCoinMetadata({coinType:l});if(h){let b={id:h.id,pyth_id:"",name:h.name,symbol:h.symbol,official_symbol:h.symbol,coingecko_id:"",decimals:h.decimals,project_url:"",logo_url:h.iconUrl,address:l};e[l]=b,this.updateCache(d,b,Ht)}else console.log(`not found ${l}`)}}return e}async getCoinConfigs(t=!1,e=!0){let{coin_list_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getCoinConfigs`,r=this.getCache(n,t);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getCoinConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[Ue.METHOD_NAME]:"getCoinConfigs"});let b=this.buildCoinConfig(h,e);this.updateCache(`${i}_${b.address}_getCoinConfig`,b,Ht),f.push({...b})}),this.updateCache(n,f,Ht),f}async getCoinConfig(t,e=!1,i=!0){let{coin_list_handle:n}=le(this.sdk.sdkOptions.cetus_config),r=`${n}_${t}_getCoinConfig`,a=this.getCache(r,e);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"0x1::type_name::TypeName",value:{name:qr(t)}}});(l.error!=null||l.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getCoinConfig get object error: ${l.error}, please check the rpc and contracts address config.`,{[Ue.METHOD_NAME]:"getCoinConfig"});let d=this.buildCoinConfig(l,i);return this.updateCache(r,d,Ht),d}buildCoinConfig(t,e=!0){let i=ei(t);i=i.value.fields;let n={...i};return n.id=nn(t),n.address=en(i.coin_type.fields.name).full_address,i.pyth_id&&(n.pyth_id=tn(i.pyth_id)),this.transformExtensions(n,i.extension_fields.fields.contents,e),delete n.coin_type,n}async getClmmPoolConfigs(t=!1,e=!0){let{clmm_pools_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getClmmPoolConfigs`,r=this.getCache(n,t);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getClmmPoolsConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[Ue.METHOD_NAME]:"getClmmPoolConfigs"});let b=this.buildClmmPoolConfig(h,e);this.updateCache(`${b.pool_address}_getClmmPoolConfig`,b,Ht),f.push({...b})}),this.updateCache(n,f,Ht),f}async getClmmPoolConfig(t,e=!1,i=!0){let{clmm_pools_handle:n}=le(this.sdk.sdkOptions.cetus_config),r=`${t}_getClmmPoolConfig`,a=this.getCache(r,e);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"address",value:t}}),d=this.buildClmmPoolConfig(l,i);return this.updateCache(r,d,Ht),d}buildClmmPoolConfig(t,e=!0){let i=ei(t);i=i.value.fields;let n={...i};return n.id=nn(t),n.pool_address=tn(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,e),n}async getLaunchpadPoolConfigs(t=!1,e=!0){let{launchpad_pools_handle:i}=le(this.sdk.sdkOptions.cetus_config),n=`${i}_getLaunchpadPoolConfigs`,r=this.getCache(n,t);if(r)return r;let l=(await this._sdk.FullClient.getDynamicFieldsByPage(i)).data.map(h=>h.objectId),d=await this._sdk.FullClient.batchGetObjects(l,{showContent:!0}),f=[];return d.forEach(h=>{(h.error!=null||h.data?.content?.dataType!=="moveObject")&&R("FetchError",`when getCoinConfigs get objects error: ${h.error}, please check the rpc and contracts address config.`,{[Ue.METHOD_NAME]:"getLaunchpadPoolConfigs"});let b=this.buildLaunchpadPoolConfig(h,e);this.updateCache(`${b.pool_address}_getLaunchpadPoolConfig`,b,Ht),f.push({...b})}),this.updateCache(n,f,Ht),f}async getLaunchpadPoolConfig(t,e=!1,i=!0){let{launchpad_pools_handle:n}=le(this.sdk.sdkOptions.cetus_config),r=`${t}_getLaunchpadPoolConfig`,a=this.getCache(r,e);if(a)return a;let l=await this._sdk.FullClient.getDynamicFieldObject({parentId:n,name:{type:"address",value:t}}),d=this.buildLaunchpadPoolConfig(l,i);return this.updateCache(r,d,Ht),d}buildLaunchpadPoolConfig(t,e=!0){let i=ei(t);i=i.value.fields;let n={...i};n.id=nn(t),n.pool_address=tn(i.pool_address),this.transformExtensions(n,i.extension_fields.fields.contents,e);let r=[];i.social_media.fields.contents.forEach(a=>{r.push({name:a.fields.value.fields.name,link:a.fields.value.fields.link})}),n.social_media=r;try{n.regulation=decodeURIComponent(Ye.decode(n.regulation).replace(/%/g,"%25"))}catch{n.regulation=Ye.decode(n.regulation)}return n}transformExtensions(t,e,i=!0){let n=[];for(let r of e){let{key:a}=r.fields,{value:l}=r.fields;if(a==="labels")try{let d=decodeURIComponent(Ye.decode(l));try{l=JSON.parse(d)}catch{l=d}}catch{}i&&(t[a]=l),n.push({key:a,value:l})}delete t.extension_fields,i||(t.extensions=n)}async getCetusConfig(t=!1){let e=this._sdk.sdkOptions.cetus_config.package_id,i=`${e}_getCetusConfig`,n=this.getCache(i,t);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:e,options:{showPreviousTransaction:!0}}),a=Dr(r),l=await this._sdk.FullClient.queryEventsByPage({Transaction:a}),d={coin_list_id:"",launchpad_pools_id:"",clmm_pools_id:"",admin_cap_id:"",global_config_id:"",coin_list_handle:"",launchpad_pools_handle:"",clmm_pools_handle:""};if(l.data.length>0)for(let f of l.data)switch(en(f.type).name){case"InitCoinListEvent":d.coin_list_id=f.parsedJson.coin_list_id;break;case"InitLaunchpadPoolsEvent":d.launchpad_pools_id=f.parsedJson.launchpad_pools_id;break;case"InitClmmPoolsEvent":d.clmm_pools_id=f.parsedJson.pools_id;break;case"InitConfigEvent":d.global_config_id=f.parsedJson.global_config_id,d.admin_cap_id=f.parsedJson.admin_cap_id;break;default:break}return d=await this.getCetusConfigHandle(d),d.clmm_pools_id.length>0&&this.updateCache(i,d,Ht),d}async getCetusConfigHandle(t){let e=[t.clmm_pools_id,t.coin_list_id,t.launchpad_pools_id];return(await this._sdk.FullClient.multiGetObjects({ids:e,options:{showContent:!0}})).forEach(n=>{(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidConfigHandle",`when getCetusConfigHandle get objects error: ${n.error}, please check the rpc and contracts address config.`,{[Ue.METHOD_NAME]:"getCetusConfigHandle"});let r=ei(n),a=Lr(n);switch(en(a).name){case"ClmmPools":t.clmm_pools_handle=r.pools.fields.id.id;break;case"CoinList":t.coin_list_handle=r.coins.fields.id.id;break;case"LaunchpadPools":t.launchpad_pools_handle=r.pools.fields.id.id;break;default:break}}),t}updateCache(t,e,i=Nr){let n=this._cache[t];n?(n.overdue_time=uo(i),n.value=e):n=new jr(e,uo(i)),this._cache[t]=n}getCache(t,e=!1){try{let i=this._cache[t];if(!i)return;if(e||!i.isValid()){delete this._cache[t];return}return i.value}catch(i){console.error(`Error accessing cache for key ${t}:`,i);return}}};var sr=re(Gt());import{SuiClient as ia}from"@mysten/sui/client";import{Transaction as Ze}from"@mysten/sui/transactions";import{normalizeSuiAddress as Ie}from"@mysten/sui/utils";var ho="position_liquidity_snapshot",po=(i=>(i.Deleted="Deleted",i.Exists="Exists",i.NotExists="NotExists",i))(po||{}),mo=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","AddLiquidityV2Event","RemoveLiquidityV2Event"];var Ft=re(Gt());import{asUintN as Do,FEE_RATE_DENOMINATOR as hi,getDeltaDownFromOutput as Lo,getDeltaUpFromInput as $o,getNextSqrtPriceFromInput as Us,getNextSqrtPriceFromOutput as Vs,MathUtil as ge,ZERO as Vt}from"@cetusprotocol/common-sdk";var qo=re(Gt());import{Transaction as fn}from"@mysten/sui/transactions";import{adjustForSlippage as js,CLOCK_ADDRESS as _i,CoinAssist as Lt,getPackagerConfigs as jo,MathUtil as _n,MAX_SQRT_PRICE as qs,MIN_SQRT_PRICE as Ds,U64_MAX as Ls,ZERO as $s}from"@cetusprotocol/common-sdk";var go="partner",ni="pool_script",Qt="pool_script_v2",oi="pool_script_v3",me="router",bo="router_with_partner",Jt="fetcher_script",Da="expect_swap",wo="utils",La="custodian_v2",$a="clob_v2",Ua="endpoints_v2",Va=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")};var No=re(Gt());import{Transaction as un}from"@mysten/sui/transactions";import{asUintN as Fo,ClmmPoolUtil as Fs,CLOCK_ADDRESS as Bo,CoinAssist as Dt,getPackagerConfigs as Bs,normalizeCoinType as dn}from"@cetusprotocol/common-sdk";var Te=9e15,_e=1e9,rn="0123456789abcdef",ai="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",ci="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",sn={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Te,maxE:Te,crypto:!1},Co,se,N=!0,ui="[DecimalError] ",fe=ui+"Invalid argument: ",Mo=ui+"Precision limit exceeded",ko=ui+"crypto unavailable",To="[object Decimal]",Rt=Math.floor,It=Math.pow,$r=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Ur=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Vr=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,Ao=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,Wt=1e7,F=7,Kr=9007199254740991,Hr=ai.length-1,an=ci.length-1,k={toStringTag:To};k.absoluteValue=k.abs=function(){var o=new this.constructor(this);return o.s<0&&(o.s=1),O(o)};k.ceil=function(){return O(new this.constructor(this),this.e+1,2)};k.clampedTo=k.clamp=function(o,t){var e,i=this,n=i.constructor;if(o=new n(o),t=new n(t),!o.s||!t.s)return new n(NaN);if(o.gt(t))throw Error(fe+t);return e=i.cmp(o),e<0?o:i.cmp(t)>0?t:new n(i)};k.comparedTo=k.cmp=function(o){var t,e,i,n,r=this,a=r.d,l=(o=new r.constructor(o)).d,d=r.s,f=o.s;if(!a||!l)return!d||!f?NaN:d!==f?d:a===l?0:!a^d<0?1:-1;if(!a[0]||!l[0])return a[0]?d:l[0]?-f:0;if(d!==f)return d;if(r.e!==o.e)return r.e>o.e^d<0?1:-1;for(i=a.length,n=l.length,t=0,e=i<n?i:n;t<e;++t)if(a[t]!==l[t])return a[t]>l[t]^d<0?1:-1;return i===n?0:i>n^d<0?1:-1};k.cosine=k.cos=function(){var o,t,e=this,i=e.constructor;return e.d?e.d[0]?(o=i.precision,t=i.rounding,i.precision=o+Math.max(e.e,e.sd())+F,i.rounding=1,e=Wr(i,Oo(i,e)),i.precision=o,i.rounding=t,O(se==2||se==3?e.neg():e,o,t,!0)):new i(1):new i(NaN)};k.cubeRoot=k.cbrt=function(){var o,t,e,i,n,r,a,l,d,f,h=this,b=h.constructor;if(!h.isFinite()||h.isZero())return new b(h);for(N=!1,r=h.s*It(h.s*h,1/3),!r||Math.abs(r)==1/0?(e=Ot(h.d),o=h.e,(r=(o-e.length+1)%3)&&(e+=r==1||r==-2?"0":"00"),r=It(e,1/3),o=Rt((o+1)/3)-(o%3==(o<0?-1:2)),r==1/0?e="5e"+o:(e=r.toExponential(),e=e.slice(0,e.indexOf("e")+1)+o),i=new b(e),i.s=h.s):i=new b(r.toString()),a=(o=b.precision)+3;;)if(l=i,d=l.times(l).times(l),f=d.plus(h),i=W(f.plus(h).times(l),f.plus(d),a+2,1),Ot(l.d).slice(0,a)===(e=Ot(i.d)).slice(0,a))if(e=e.slice(a-3,a+1),e=="9999"||!n&&e=="4999"){if(!n&&(O(l,o+1,0),l.times(l).times(l).eq(h))){i=l;break}a+=4,n=1}else{(!+e||!+e.slice(1)&&e.charAt(0)=="5")&&(O(i,o+1,1),t=!i.times(i).times(i).eq(h));break}return N=!0,O(i,o,b.rounding,t)};k.decimalPlaces=k.dp=function(){var o,t=this.d,e=NaN;if(t){if(o=t.length-1,e=(o-Rt(this.e/F))*F,o=t[o],o)for(;o%10==0;o/=10)e--;e<0&&(e=0)}return e};k.dividedBy=k.div=function(o){return W(this,new this.constructor(o))};k.dividedToIntegerBy=k.divToInt=function(o){var t=this,e=t.constructor;return O(W(t,new e(o),0,1,1),e.precision,e.rounding)};k.equals=k.eq=function(o){return this.cmp(o)===0};k.floor=function(){return O(new this.constructor(this),this.e+1,3)};k.greaterThan=k.gt=function(o){return this.cmp(o)>0};k.greaterThanOrEqualTo=k.gte=function(o){var t=this.cmp(o);return t==1||t===0};k.hyperbolicCosine=k.cosh=function(){var o,t,e,i,n,r=this,a=r.constructor,l=new a(1);if(!r.isFinite())return new a(r.s?1/0:NaN);if(r.isZero())return l;e=a.precision,i=a.rounding,a.precision=e+Math.max(r.e,r.sd())+4,a.rounding=1,n=r.d.length,n<32?(o=Math.ceil(n/3),t=(1/fi(4,o)).toString()):(o=16,t="2.3283064365386962890625e-10"),r=Ae(a,1,r.times(t),new a(1),!0);for(var d,f=o,h=new a(8);f--;)d=r.times(r),r=l.minus(d.times(h.minus(d.times(h))));return O(r,a.precision=e,a.rounding=i,!0)};k.hyperbolicSine=k.sinh=function(){var o,t,e,i,n=this,r=n.constructor;if(!n.isFinite()||n.isZero())return new r(n);if(t=r.precision,e=r.rounding,r.precision=t+Math.max(n.e,n.sd())+4,r.rounding=1,i=n.d.length,i<3)n=Ae(r,2,n,n,!0);else{o=1.4*Math.sqrt(i),o=o>16?16:o|0,n=n.times(1/fi(5,o)),n=Ae(r,2,n,n,!0);for(var a,l=new r(5),d=new r(16),f=new r(20);o--;)a=n.times(n),n=n.times(l.plus(a.times(d.times(a).plus(f))))}return r.precision=t,r.rounding=e,O(n,t,e,!0)};k.hyperbolicTangent=k.tanh=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+7,i.rounding=1,W(e.sinh(),e.cosh(),i.precision=o,i.rounding=t)):new i(e.s)};k.inverseCosine=k.acos=function(){var o=this,t=o.constructor,e=o.abs().cmp(1),i=t.precision,n=t.rounding;return e!==-1?e===0?o.isNeg()?Xt(t,i,n):new t(0):new t(NaN):o.isZero()?Xt(t,i+4,n).times(.5):(t.precision=i+6,t.rounding=1,o=new t(1).minus(o).div(o.plus(1)).sqrt().atan(),t.precision=i,t.rounding=n,o.times(2))};k.inverseHyperbolicCosine=k.acosh=function(){var o,t,e=this,i=e.constructor;return e.lte(1)?new i(e.eq(1)?0:NaN):e.isFinite()?(o=i.precision,t=i.rounding,i.precision=o+Math.max(Math.abs(e.e),e.sd())+4,i.rounding=1,N=!1,e=e.times(e).minus(1).sqrt().plus(e),N=!0,i.precision=o,i.rounding=t,e.ln()):new i(e)};k.inverseHyperbolicSine=k.asinh=function(){var o,t,e=this,i=e.constructor;return!e.isFinite()||e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+2*Math.max(Math.abs(e.e),e.sd())+6,i.rounding=1,N=!1,e=e.times(e).plus(1).sqrt().plus(e),N=!0,i.precision=o,i.rounding=t,e.ln())};k.inverseHyperbolicTangent=k.atanh=function(){var o,t,e,i,n=this,r=n.constructor;return n.isFinite()?n.e>=0?new r(n.abs().eq(1)?n.s/0:n.isZero()?n:NaN):(o=r.precision,t=r.rounding,i=n.sd(),Math.max(i,o)<2*-n.e-1?O(new r(n),o,t,!0):(r.precision=e=i-n.e,n=W(n.plus(1),new r(1).minus(n),e+o,1),r.precision=o+4,r.rounding=1,n=n.ln(),r.precision=o,r.rounding=t,n.times(.5))):new r(NaN)};k.inverseSine=k.asin=function(){var o,t,e,i,n=this,r=n.constructor;return n.isZero()?new r(n):(t=n.abs().cmp(1),e=r.precision,i=r.rounding,t!==-1?t===0?(o=Xt(r,e+4,i).times(.5),o.s=n.s,o):new r(NaN):(r.precision=e+6,r.rounding=1,n=n.div(new r(1).minus(n.times(n)).sqrt().plus(1)).atan(),r.precision=e,r.rounding=i,n.times(2)))};k.inverseTangent=k.atan=function(){var o,t,e,i,n,r,a,l,d,f=this,h=f.constructor,b=h.precision,P=h.rounding;if(f.isFinite()){if(f.isZero())return new h(f);if(f.abs().eq(1)&&b+4<=an)return a=Xt(h,b+4,P).times(.25),a.s=f.s,a}else{if(!f.s)return new h(NaN);if(b+4<=an)return a=Xt(h,b+4,P).times(.5),a.s=f.s,a}for(h.precision=l=b+10,h.rounding=1,e=Math.min(28,l/F+2|0),o=e;o;--o)f=f.div(f.times(f).plus(1).sqrt().plus(1));for(N=!1,t=Math.ceil(l/F),i=1,d=f.times(f),a=new h(f),n=f;o!==-1;)if(n=n.times(d),r=a.minus(n.div(i+=2)),n=n.times(d),a=r.plus(n.div(i+=2)),a.d[t]!==void 0)for(o=t;a.d[o]===r.d[o]&&o--;);return e&&(a=a.times(2<<e-1)),N=!0,O(a,h.precision=b,h.rounding=P,!0)};k.isFinite=function(){return!!this.d};k.isInteger=k.isInt=function(){return!!this.d&&Rt(this.e/F)>this.d.length-2};k.isNaN=function(){return!this.s};k.isNegative=k.isNeg=function(){return this.s<0};k.isPositive=k.isPos=function(){return this.s>0};k.isZero=function(){return!!this.d&&this.d[0]===0};k.lessThan=k.lt=function(o){return this.cmp(o)<0};k.lessThanOrEqualTo=k.lte=function(o){return this.cmp(o)<1};k.logarithm=k.log=function(o){var t,e,i,n,r,a,l,d,f=this,h=f.constructor,b=h.precision,P=h.rounding,M=5;if(o==null)o=new h(10),t=!0;else{if(o=new h(o),e=o.d,o.s<0||!e||!e[0]||o.eq(1))return new h(NaN);t=o.eq(10)}if(e=f.d,f.s<0||!e||!e[0]||f.eq(1))return new h(e&&!e[0]?-1/0:f.s!=1?NaN:e?0:1/0);if(t)if(e.length>1)r=!0;else{for(n=e[0];n%10===0;)n/=10;r=n!==1}if(N=!1,l=b+M,a=de(f,l),i=t?li(h,l+10):de(o,l),d=W(a,i,l,1),Ve(d.d,n=b,P))do if(l+=10,a=de(f,l),i=t?li(h,l+10):de(o,l),d=W(a,i,l,1),!r){+Ot(d.d).slice(n+1,n+15)+1==1e14&&(d=O(d,b+1,0));break}while(Ve(d.d,n+=10,P));return N=!0,O(d,b,P)};k.minus=k.sub=function(o){var t,e,i,n,r,a,l,d,f,h,b,P,M=this,T=M.constructor;if(o=new T(o),!M.d||!o.d)return!M.s||!o.s?o=new T(NaN):M.d?o.s=-o.s:o=new T(o.d||M.s!==o.s?M:NaN),o;if(M.s!=o.s)return o.s=-o.s,M.plus(o);if(f=M.d,P=o.d,l=T.precision,d=T.rounding,!f[0]||!P[0]){if(P[0])o.s=-o.s;else if(f[0])o=new T(M);else return new T(d===3?-0:0);return N?O(o,l,d):o}if(e=Rt(o.e/F),h=Rt(M.e/F),f=f.slice(),r=h-e,r){for(b=r<0,b?(t=f,r=-r,a=P.length):(t=P,e=h,a=f.length),i=Math.max(Math.ceil(l/F),a)+2,r>i&&(r=i,t.length=1),t.reverse(),i=r;i--;)t.push(0);t.reverse()}else{for(i=f.length,a=P.length,b=i<a,b&&(a=i),i=0;i<a;i++)if(f[i]!=P[i]){b=f[i]<P[i];break}r=0}for(b&&(t=f,f=P,P=t,o.s=-o.s),a=f.length,i=P.length-a;i>0;--i)f[a++]=0;for(i=P.length;i>r;){if(f[--i]<P[i]){for(n=i;n&&f[--n]===0;)f[n]=Wt-1;--f[n],f[i]+=Wt}f[i]-=P[i]}for(;f[--a]===0;)f.pop();for(;f[0]===0;f.shift())--e;return f[0]?(o.d=f,o.e=di(f,e),N?O(o,l,d):o):new T(d===3?-0:0)};k.modulo=k.mod=function(o){var t,e=this,i=e.constructor;return o=new i(o),!e.d||!o.s||o.d&&!o.d[0]?new i(NaN):!o.d||e.d&&!e.d[0]?O(new i(e),i.precision,i.rounding):(N=!1,i.modulo==9?(t=W(e,o.abs(),0,3,1),t.s*=o.s):t=W(e,o,0,i.modulo,1),t=t.times(o),N=!0,e.minus(t))};k.naturalExponential=k.exp=function(){return cn(this)};k.naturalLogarithm=k.ln=function(){return de(this)};k.negated=k.neg=function(){var o=new this.constructor(this);return o.s=-o.s,O(o)};k.plus=k.add=function(o){var t,e,i,n,r,a,l,d,f,h,b=this,P=b.constructor;if(o=new P(o),!b.d||!o.d)return!b.s||!o.s?o=new P(NaN):b.d||(o=new P(o.d||b.s===o.s?b:NaN)),o;if(b.s!=o.s)return o.s=-o.s,b.minus(o);if(f=b.d,h=o.d,l=P.precision,d=P.rounding,!f[0]||!h[0])return h[0]||(o=new P(b)),N?O(o,l,d):o;if(r=Rt(b.e/F),i=Rt(o.e/F),f=f.slice(),n=r-i,n){for(n<0?(e=f,n=-n,a=h.length):(e=h,i=r,a=f.length),r=Math.ceil(l/F),a=r>a?r+1:a+1,n>a&&(n=a,e.length=1),e.reverse();n--;)e.push(0);e.reverse()}for(a=f.length,n=h.length,a-n<0&&(n=a,e=h,h=f,f=e),t=0;n;)t=(f[--n]=f[n]+h[n]+t)/Wt|0,f[n]%=Wt;for(t&&(f.unshift(t),++i),a=f.length;f[--a]==0;)f.pop();return o.d=f,o.e=di(f,i),N?O(o,l,d):o};k.precision=k.sd=function(o){var t,e=this;if(o!==void 0&&o!==!!o&&o!==1&&o!==0)throw Error(fe+o);return e.d?(t=So(e.d),o&&e.e+1>t&&(t=e.e+1)):t=NaN,t};k.round=function(){var o=this,t=o.constructor;return O(new t(o),o.e+1,t.rounding)};k.sine=k.sin=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+Math.max(e.e,e.sd())+F,i.rounding=1,e=zr(i,Oo(i,e)),i.precision=o,i.rounding=t,O(se>2?e.neg():e,o,t,!0)):new i(NaN)};k.squareRoot=k.sqrt=function(){var o,t,e,i,n,r,a=this,l=a.d,d=a.e,f=a.s,h=a.constructor;if(f!==1||!l||!l[0])return new h(!f||f<0&&(!l||l[0])?NaN:l?a:1/0);for(N=!1,f=Math.sqrt(+a),f==0||f==1/0?(t=Ot(l),(t.length+d)%2==0&&(t+="0"),f=Math.sqrt(t),d=Rt((d+1)/2)-(d<0||d%2),f==1/0?t="5e"+d:(t=f.toExponential(),t=t.slice(0,t.indexOf("e")+1)+d),i=new h(t)):i=new h(f.toString()),e=(d=h.precision)+3;;)if(r=i,i=r.plus(W(a,r,e+2,1)).times(.5),Ot(r.d).slice(0,e)===(t=Ot(i.d)).slice(0,e))if(t=t.slice(e-3,e+1),t=="9999"||!n&&t=="4999"){if(!n&&(O(r,d+1,0),r.times(r).eq(a))){i=r;break}e+=4,n=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(O(i,d+1,1),o=!i.times(i).eq(a));break}return N=!0,O(i,d,h.rounding,o)};k.tangent=k.tan=function(){var o,t,e=this,i=e.constructor;return e.isFinite()?e.isZero()?new i(e):(o=i.precision,t=i.rounding,i.precision=o+10,i.rounding=1,e=e.sin(),e.s=1,e=W(e,new i(1).minus(e.times(e)).sqrt(),o+10,0),i.precision=o,i.rounding=t,O(se==2||se==4?e.neg():e,o,t,!0)):new i(NaN)};k.times=k.mul=function(o){var t,e,i,n,r,a,l,d,f,h=this,b=h.constructor,P=h.d,M=(o=new b(o)).d;if(o.s*=h.s,!P||!P[0]||!M||!M[0])return new b(!o.s||P&&!P[0]&&!M||M&&!M[0]&&!P?NaN:!P||!M?o.s/0:o.s*0);for(e=Rt(h.e/F)+Rt(o.e/F),d=P.length,f=M.length,d<f&&(r=P,P=M,M=r,a=d,d=f,f=a),r=[],a=d+f,i=a;i--;)r.push(0);for(i=f;--i>=0;){for(t=0,n=d+i;n>i;)l=r[n]+M[i]*P[n-i-1]+t,r[n--]=l%Wt|0,t=l/Wt|0;r[n]=(r[n]+t)%Wt|0}for(;!r[--a];)r.pop();return t?++e:r.shift(),o.d=r,o.e=di(r,e),N?O(o,b.precision,b.rounding):o};k.toBinary=function(o,t){return ln(this,2,o,t)};k.toDecimalPlaces=k.toDP=function(o,t){var e=this,i=e.constructor;return e=new i(e),o===void 0?e:(Nt(o,0,_e),t===void 0?t=i.rounding:Nt(t,0,8),O(e,o+e.e+1,t))};k.toExponential=function(o,t){var e,i=this,n=i.constructor;return o===void 0?e=Yt(i,!0):(Nt(o,0,_e),t===void 0?t=n.rounding:Nt(t,0,8),i=O(new n(i),o+1,t),e=Yt(i,!0,o+1)),i.isNeg()&&!i.isZero()?"-"+e:e};k.toFixed=function(o,t){var e,i,n=this,r=n.constructor;return o===void 0?e=Yt(n):(Nt(o,0,_e),t===void 0?t=r.rounding:Nt(t,0,8),i=O(new r(n),o+n.e+1,t),e=Yt(i,!1,o+i.e+1)),n.isNeg()&&!n.isZero()?"-"+e:e};k.toFraction=function(o){var t,e,i,n,r,a,l,d,f,h,b,P,M=this,T=M.d,S=M.constructor;if(!T)return new S(M);if(f=e=new S(1),i=d=new S(0),t=new S(i),r=t.e=So(T)-M.e-1,a=r%F,t.d[0]=It(10,a<0?F+a:a),o==null)o=r>0?t:f;else{if(l=new S(o),!l.isInt()||l.lt(f))throw Error(fe+l);o=l.gt(t)?r>0?t:f:l}for(N=!1,l=new S(Ot(T)),h=S.precision,S.precision=r=T.length*F*2;b=W(l,t,0,1,1),n=e.plus(b.times(i)),n.cmp(o)!=1;)e=i,i=n,n=f,f=d.plus(b.times(n)),d=n,n=t,t=l.minus(b.times(n)),l=n;return n=W(o.minus(e),i,0,1,1),d=d.plus(n.times(f)),e=e.plus(n.times(i)),d.s=f.s=M.s,P=W(f,i,r,1).minus(M).abs().cmp(W(d,e,r,1).minus(M).abs())<1?[f,i]:[d,e],S.precision=h,N=!0,P};k.toHexadecimal=k.toHex=function(o,t){return ln(this,16,o,t)};k.toNearest=function(o,t){var e=this,i=e.constructor;if(e=new i(e),o==null){if(!e.d)return e;o=new i(1),t=i.rounding}else{if(o=new i(o),t===void 0?t=i.rounding:Nt(t,0,8),!e.d)return o.s?e:o;if(!o.d)return o.s&&(o.s=e.s),o}return o.d[0]?(N=!1,e=W(e,o,0,t,1).times(o),N=!0,O(e)):(o.s=e.s,e=o),e};k.toNumber=function(){return+this};k.toOctal=function(o,t){return ln(this,8,o,t)};k.toPower=k.pow=function(o){var t,e,i,n,r,a,l=this,d=l.constructor,f=+(o=new d(o));if(!l.d||!o.d||!l.d[0]||!o.d[0])return new d(It(+l,f));if(l=new d(l),l.eq(1))return l;if(i=d.precision,r=d.rounding,o.eq(1))return O(l,i,r);if(t=Rt(o.e/F),t>=o.d.length-1&&(e=f<0?-f:f)<=Kr)return n=xo(d,l,e,i),o.s<0?new d(1).div(n):O(n,i,r);if(a=l.s,a<0){if(t<o.d.length-1)return new d(NaN);if((o.d[t]&1)==0&&(a=1),l.e==0&&l.d[0]==1&&l.d.length==1)return l.s=a,l}return e=It(+l,f),t=e==0||!isFinite(e)?Rt(f*(Math.log("0."+Ot(l.d))/Math.LN10+l.e+1)):new d(e+"").e,t>d.maxE+1||t<d.minE-1?new d(t>0?a/0:0):(N=!1,d.rounding=l.s=1,e=Math.min(12,(t+"").length),n=cn(o.times(de(l,i+e)),i),n.d&&(n=O(n,i+5,1),Ve(n.d,i,r)&&(t=i+10,n=O(cn(o.times(de(l,t+e)),t),t+5,1),+Ot(n.d).slice(i+1,i+15)+1==1e14&&(n=O(n,i+1,0)))),n.s=a,N=!0,d.rounding=r,O(n,i,r))};k.toPrecision=function(o,t){var e,i=this,n=i.constructor;return o===void 0?e=Yt(i,i.e<=n.toExpNeg||i.e>=n.toExpPos):(Nt(o,1,_e),t===void 0?t=n.rounding:Nt(t,0,8),i=O(new n(i),o,t),e=Yt(i,o<=i.e||i.e<=n.toExpNeg,o)),i.isNeg()&&!i.isZero()?"-"+e:e};k.toSignificantDigits=k.toSD=function(o,t){var e=this,i=e.constructor;return o===void 0?(o=i.precision,t=i.rounding):(Nt(o,1,_e),t===void 0?t=i.rounding:Nt(t,0,8)),O(new i(e),o,t)};k.toString=function(){var o=this,t=o.constructor,e=Yt(o,o.e<=t.toExpNeg||o.e>=t.toExpPos);return o.isNeg()&&!o.isZero()?"-"+e:e};k.truncated=k.trunc=function(){return O(new this.constructor(this),this.e+1,1)};k.valueOf=k.toJSON=function(){var o=this,t=o.constructor,e=Yt(o,o.e<=t.toExpNeg||o.e>=t.toExpPos);return o.isNeg()?"-"+e:e};function Ot(o){var t,e,i,n=o.length-1,r="",a=o[0];if(n>0){for(r+=a,t=1;t<n;t++)i=o[t]+"",e=F-i.length,e&&(r+=ue(e)),r+=i;a=o[t],i=a+"",e=F-i.length,e&&(r+=ue(e))}else if(a===0)return"0";for(;a%10===0;)a/=10;return r+a}function Nt(o,t,e){if(o!==~~o||o<t||o>e)throw Error(fe+o)}function Ve(o,t,e,i){var n,r,a,l;for(r=o[0];r>=10;r/=10)--t;return--t<0?(t+=F,n=0):(n=Math.ceil((t+1)/F),t%=F),r=It(10,F-t),l=o[n]%r|0,i==null?t<3?(t==0?l=l/100|0:t==1&&(l=l/10|0),a=e<4&&l==99999||e>3&&l==49999||l==5e4||l==0):a=(e<4&&l+1==r||e>3&&l+1==r/2)&&(o[n+1]/r/100|0)==It(10,t-2)-1||(l==r/2||l==0)&&(o[n+1]/r/100|0)==0:t<4?(t==0?l=l/1e3|0:t==1?l=l/100|0:t==2&&(l=l/10|0),a=(i||e<4)&&l==9999||!i&&e>3&&l==4999):a=((i||e<4)&&l+1==r||!i&&e>3&&l+1==r/2)&&(o[n+1]/r/1e3|0)==It(10,t-3)-1,a}function ri(o,t,e){for(var i,n=[0],r,a=0,l=o.length;a<l;){for(r=n.length;r--;)n[r]*=t;for(n[0]+=rn.indexOf(o.charAt(a++)),i=0;i<n.length;i++)n[i]>e-1&&(n[i+1]===void 0&&(n[i+1]=0),n[i+1]+=n[i]/e|0,n[i]%=e)}return n.reverse()}function Wr(o,t){var e,i,n;if(t.isZero())return t;i=t.d.length,i<32?(e=Math.ceil(i/3),n=(1/fi(4,e)).toString()):(e=16,n="2.3283064365386962890625e-10"),o.precision+=e,t=Ae(o,1,t.times(n),new o(1));for(var r=e;r--;){var a=t.times(t);t=a.times(a).minus(a).times(8).plus(1)}return o.precision-=e,t}var W=(function(){function o(i,n,r){var a,l=0,d=i.length;for(i=i.slice();d--;)a=i[d]*n+l,i[d]=a%r|0,l=a/r|0;return l&&i.unshift(l),i}function t(i,n,r,a){var l,d;if(r!=a)d=r>a?1:-1;else for(l=d=0;l<r;l++)if(i[l]!=n[l]){d=i[l]>n[l]?1:-1;break}return d}function e(i,n,r,a){for(var l=0;r--;)i[r]-=l,l=i[r]<n[r]?1:0,i[r]=l*a+i[r]-n[r];for(;!i[0]&&i.length>1;)i.shift()}return function(i,n,r,a,l,d){var f,h,b,P,M,T,S,L,j,V,B,D,jt,z,$t,ne,Zt,oe,$,Bt,y=i.constructor,s=i.s==n.s?1:-1,u=i.d,_=n.d;if(!u||!u[0]||!_||!_[0])return new y(!i.s||!n.s||(u?_&&u[0]==_[0]:!_)?NaN:u&&u[0]==0||!_?s*0:s/0);for(d?(M=1,h=i.e-n.e):(d=Wt,M=F,h=Rt(i.e/M)-Rt(n.e/M)),$=_.length,Zt=u.length,j=new y(s),V=j.d=[],b=0;_[b]==(u[b]||0);b++);if(_[b]>(u[b]||0)&&h--,r==null?(z=r=y.precision,a=y.rounding):l?z=r+(i.e-n.e)+1:z=r,z<0)V.push(1),T=!0;else{if(z=z/M+2|0,b=0,$==1){for(P=0,_=_[0],z++;(b<Zt||P)&&z--;b++)$t=P*d+(u[b]||0),V[b]=$t/_|0,P=$t%_|0;T=P||b<Zt}else{for(P=d/(_[0]+1)|0,P>1&&(_=o(_,P,d),u=o(u,P,d),$=_.length,Zt=u.length),ne=$,B=u.slice(0,$),D=B.length;D<$;)B[D++]=0;Bt=_.slice(),Bt.unshift(0),oe=_[0],_[1]>=d/2&&++oe;do P=0,f=t(_,B,$,D),f<0?(jt=B[0],$!=D&&(jt=jt*d+(B[1]||0)),P=jt/oe|0,P>1?(P>=d&&(P=d-1),S=o(_,P,d),L=S.length,D=B.length,f=t(S,B,L,D),f==1&&(P--,e(S,$<L?Bt:_,L,d))):(P==0&&(f=P=1),S=_.slice()),L=S.length,L<D&&S.unshift(0),e(B,S,D,d),f==-1&&(D=B.length,f=t(_,B,$,D),f<1&&(P++,e(B,$<D?Bt:_,D,d))),D=B.length):f===0&&(P++,B=[0]),V[b++]=P,f&&B[0]?B[D++]=u[ne]||0:(B=[u[ne]],D=1);while((ne++<Zt||B[0]!==void 0)&&z--);T=B[0]!==void 0}V[0]||V.shift()}if(M==1)j.e=h,Co=T;else{for(b=1,P=V[0];P>=10;P/=10)b++;j.e=b+h*M-1,O(j,l?r+j.e+1:r,a,T)}return j}})();function O(o,t,e,i){var n,r,a,l,d,f,h,b,P,M=o.constructor;t:if(t!=null){if(b=o.d,!b)return o;for(n=1,l=b[0];l>=10;l/=10)n++;if(r=t-n,r<0)r+=F,a=t,h=b[P=0],d=h/It(10,n-a-1)%10|0;else if(P=Math.ceil((r+1)/F),l=b.length,P>=l)if(i){for(;l++<=P;)b.push(0);h=d=0,n=1,r%=F,a=r-F+1}else break t;else{for(h=l=b[P],n=1;l>=10;l/=10)n++;r%=F,a=r-F+n,d=a<0?0:h/It(10,n-a-1)%10|0}if(i=i||t<0||b[P+1]!==void 0||(a<0?h:h%It(10,n-a-1)),f=e<4?(d||i)&&(e==0||e==(o.s<0?3:2)):d>5||d==5&&(e==4||i||e==6&&(r>0?a>0?h/It(10,n-a):0:b[P-1])%10&1||e==(o.s<0?8:7)),t<1||!b[0])return b.length=0,f?(t-=o.e+1,b[0]=It(10,(F-t%F)%F),o.e=-t||0):b[0]=o.e=0,o;if(r==0?(b.length=P,l=1,P--):(b.length=P+1,l=It(10,F-r),b[P]=a>0?(h/It(10,n-a)%It(10,a)|0)*l:0),f)for(;;)if(P==0){for(r=1,a=b[0];a>=10;a/=10)r++;for(a=b[0]+=l,l=1;a>=10;a/=10)l++;r!=l&&(o.e++,b[0]==Wt&&(b[0]=1));break}else{if(b[P]+=l,b[P]!=Wt)break;b[P--]=0,l=1}for(r=b.length;b[--r]===0;)b.pop()}return N&&(o.e>M.maxE?(o.d=null,o.e=NaN):o.e<M.minE&&(o.e=0,o.d=[0])),o}function Yt(o,t,e){if(!o.isFinite())return Eo(o);var i,n=o.e,r=Ot(o.d),a=r.length;return t?(e&&(i=e-a)>0?r=r.charAt(0)+"."+r.slice(1)+ue(i):a>1&&(r=r.charAt(0)+"."+r.slice(1)),r=r+(o.e<0?"e":"e+")+o.e):n<0?(r="0."+ue(-n-1)+r,e&&(i=e-a)>0&&(r+=ue(i))):n>=a?(r+=ue(n+1-a),e&&(i=e-n-1)>0&&(r=r+"."+ue(i))):((i=n+1)<a&&(r=r.slice(0,i)+"."+r.slice(i)),e&&(i=e-a)>0&&(n+1===a&&(r+="."),r+=ue(i))),r}function di(o,t){var e=o[0];for(t*=F;e>=10;e/=10)t++;return t}function li(o,t,e){if(t>Hr)throw N=!0,e&&(o.precision=e),Error(Mo);return O(new o(ai),t,1,!0)}function Xt(o,t,e){if(t>an)throw Error(Mo);return O(new o(ci),t,e,!0)}function So(o){var t=o.length-1,e=t*F+1;if(t=o[t],t){for(;t%10==0;t/=10)e--;for(t=o[0];t>=10;t/=10)e++}return e}function ue(o){for(var t="";o--;)t+="0";return t}function xo(o,t,e,i){var n,r=new o(1),a=Math.ceil(i/F+4);for(N=!1;;){if(e%2&&(r=r.times(t),vo(r.d,a)&&(n=!0)),e=Rt(e/2),e===0){e=r.d.length-1,n&&r.d[e]===0&&++r.d[e];break}t=t.times(t),vo(t.d,a)}return N=!0,r}function yo(o){return o.d[o.d.length-1]&1}function Io(o,t,e){for(var i,n,r=new o(t[0]),a=0;++a<t.length;){if(n=new o(t[a]),!n.s){r=n;break}i=r.cmp(n),(i===e||i===0&&r.s===e)&&(r=n)}return r}function cn(o,t){var e,i,n,r,a,l,d,f=0,h=0,b=0,P=o.constructor,M=P.rounding,T=P.precision;if(!o.d||!o.d[0]||o.e>17)return new P(o.d?o.d[0]?o.s<0?0:1/0:1:o.s?o.s<0?0:o:NaN);for(t==null?(N=!1,d=T):d=t,l=new P(.03125);o.e>-2;)o=o.times(l),b+=5;for(i=Math.log(It(2,b))/Math.LN10*2+5|0,d+=i,e=r=a=new P(1),P.precision=d;;){if(r=O(r.times(o),d,1),e=e.times(++h),l=a.plus(W(r,e,d,1)),Ot(l.d).slice(0,d)===Ot(a.d).slice(0,d)){for(n=b;n--;)a=O(a.times(a),d,1);if(t==null)if(f<3&&Ve(a.d,d-i,M,f))P.precision=d+=10,e=r=l=new P(1),h=0,f++;else return O(a,P.precision=T,M,N=!0);else return P.precision=T,a}a=l}}function de(o,t){var e,i,n,r,a,l,d,f,h,b,P,M=1,T=10,S=o,L=S.d,j=S.constructor,V=j.rounding,B=j.precision;if(S.s<0||!L||!L[0]||!S.e&&L[0]==1&&L.length==1)return new j(L&&!L[0]?-1/0:S.s!=1?NaN:L?0:S);if(t==null?(N=!1,h=B):h=t,j.precision=h+=T,e=Ot(L),i=e.charAt(0),Math.abs(r=S.e)<15e14){for(;i<7&&i!=1||i==1&&e.charAt(1)>3;)S=S.times(o),e=Ot(S.d),i=e.charAt(0),M++;r=S.e,i>1?(S=new j("0."+e),r++):S=new j(i+"."+e.slice(1))}else return f=li(j,h+2,B).times(r+""),S=de(new j(i+"."+e.slice(1)),h-T).plus(f),j.precision=B,t==null?O(S,B,V,N=!0):S;for(b=S,d=a=S=W(S.minus(1),S.plus(1),h,1),P=O(S.times(S),h,1),n=3;;){if(a=O(a.times(P),h,1),f=d.plus(W(a,new j(n),h,1)),Ot(f.d).slice(0,h)===Ot(d.d).slice(0,h))if(d=d.times(2),r!==0&&(d=d.plus(li(j,h+2,B).times(r+""))),d=W(d,new j(M),h,1),t==null)if(Ve(d.d,h-T,V,l))j.precision=h+=T,f=a=S=W(b.minus(1),b.plus(1),h,1),P=O(S.times(S),h,1),n=l=1;else return O(d,j.precision=B,V,N=!0);else return j.precision=B,d;d=f,n+=2}}function Eo(o){return String(o.s*o.s/0)}function si(o,t){var e,i,n;for((e=t.indexOf("."))>-1&&(t=t.replace(".","")),(i=t.search(/e/i))>0?(e<0&&(e=i),e+=+t.slice(i+1),t=t.substring(0,i)):e<0&&(e=t.length),i=0;t.charCodeAt(i)===48;i++);for(n=t.length;t.charCodeAt(n-1)===48;--n);if(t=t.slice(i,n),t){if(n-=i,o.e=e=e-i-1,o.d=[],i=(e+1)%F,e<0&&(i+=F),i<n){for(i&&o.d.push(+t.slice(0,i)),n-=F;i<n;)o.d.push(+t.slice(i,i+=F));t=t.slice(i),i=F-t.length}else i-=n;for(;i--;)t+="0";o.d.push(+t),N&&(o.e>o.constructor.maxE?(o.d=null,o.e=NaN):o.e<o.constructor.minE&&(o.e=0,o.d=[0]))}else o.e=0,o.d=[0];return o}function Zr(o,t){var e,i,n,r,a,l,d,f,h;if(t.indexOf("_")>-1){if(t=t.replace(/(\d)_(?=\d)/g,"$1"),Ao.test(t))return si(o,t)}else if(t==="Infinity"||t==="NaN")return+t||(o.s=NaN),o.e=NaN,o.d=null,o;if(Ur.test(t))e=16,t=t.toLowerCase();else if($r.test(t))e=2;else if(Vr.test(t))e=8;else throw Error(fe+t);for(r=t.search(/p/i),r>0?(d=+t.slice(r+1),t=t.substring(2,r)):t=t.slice(2),r=t.indexOf("."),a=r>=0,i=o.constructor,a&&(t=t.replace(".",""),l=t.length,r=l-r,n=xo(i,new i(e),r,r*2)),f=ri(t,e,Wt),h=f.length-1,r=h;f[r]===0;--r)f.pop();return r<0?new i(o.s*0):(o.e=di(f,h),o.d=f,N=!1,a&&(o=W(o,n,l*4)),d&&(o=o.times(Math.abs(d)<54?It(2,d):Ke.pow(2,d))),N=!0,o)}function zr(o,t){var e,i=t.d.length;if(i<3)return t.isZero()?t:Ae(o,2,t,t);e=1.4*Math.sqrt(i),e=e>16?16:e|0,t=t.times(1/fi(5,e)),t=Ae(o,2,t,t);for(var n,r=new o(5),a=new o(16),l=new o(20);e--;)n=t.times(t),t=t.times(r.plus(n.times(a.times(n).minus(l))));return t}function Ae(o,t,e,i,n){var r,a,l,d,f=1,h=o.precision,b=Math.ceil(h/F);for(N=!1,d=e.times(e),l=new o(i);;){if(a=W(l.times(d),new o(t++*t++),h,1),l=n?i.plus(a):i.minus(a),i=W(a.times(d),new o(t++*t++),h,1),a=l.plus(i),a.d[b]!==void 0){for(r=b;a.d[r]===l.d[r]&&r--;);if(r==-1)break}r=l,l=i,i=a,a=r,f++}return N=!0,a.d.length=b+1,a}function fi(o,t){for(var e=o;--t;)e*=o;return e}function Oo(o,t){var e,i=t.s<0,n=Xt(o,o.precision,1),r=n.times(.5);if(t=t.abs(),t.lte(r))return se=i?4:1,t;if(e=t.divToInt(n),e.isZero())se=i?3:2;else{if(t=t.minus(e.times(n)),t.lte(r))return se=yo(e)?i?2:3:i?4:1,t;se=yo(e)?i?1:4:i?3:2}return t.minus(n).abs()}function ln(o,t,e,i){var n,r,a,l,d,f,h,b,P,M=o.constructor,T=e!==void 0;if(T?(Nt(e,1,_e),i===void 0?i=M.rounding:Nt(i,0,8)):(e=M.precision,i=M.rounding),!o.isFinite())h=Eo(o);else{for(h=Yt(o),a=h.indexOf("."),T?(n=2,t==16?e=e*4-3:t==8&&(e=e*3-2)):n=t,a>=0&&(h=h.replace(".",""),P=new M(1),P.e=h.length-a,P.d=ri(Yt(P),10,n),P.e=P.d.length),b=ri(h,10,n),r=d=b.length;b[--d]==0;)b.pop();if(!b[0])h=T?"0p+0":"0";else{if(a<0?r--:(o=new M(o),o.d=b,o.e=r,o=W(o,P,e,i,0,n),b=o.d,r=o.e,f=Co),a=b[e],l=n/2,f=f||b[e+1]!==void 0,f=i<4?(a!==void 0||f)&&(i===0||i===(o.s<0?3:2)):a>l||a===l&&(i===4||f||i===6&&b[e-1]&1||i===(o.s<0?8:7)),b.length=e,f)for(;++b[--e]>n-1;)b[e]=0,e||(++r,b.unshift(1));for(d=b.length;!b[d-1];--d);for(a=0,h="";a<d;a++)h+=rn.charAt(b[a]);if(T){if(d>1)if(t==16||t==8){for(a=t==16?4:3,--d;d%a;d++)h+="0";for(b=ri(h,n,t),d=b.length;!b[d-1];--d);for(a=1,h="1.";a<d;a++)h+=rn.charAt(b[a])}else h=h.charAt(0)+"."+h.slice(1);h=h+(r<0?"p":"p+")+r}else if(r<0){for(;++r;)h="0"+h;h="0."+h}else if(++r>d)for(r-=d;r--;)h+="0";else r<d&&(h=h.slice(0,r)+"."+h.slice(r))}h=(t==16?"0x":t==2?"0b":t==8?"0o":"")+h}return o.s<0?"-"+h:h}function vo(o,t){if(o.length>t)return o.length=t,!0}function Gr(o){return new this(o).abs()}function Qr(o){return new this(o).acos()}function Jr(o){return new this(o).acosh()}function Xr(o,t){return new this(o).plus(t)}function Yr(o){return new this(o).asin()}function ts(o){return new this(o).asinh()}function es(o){return new this(o).atan()}function is(o){return new this(o).atanh()}function ns(o,t){o=new this(o),t=new this(t);var e,i=this.precision,n=this.rounding,r=i+4;return!o.s||!t.s?e=new this(NaN):!o.d&&!t.d?(e=Xt(this,r,1).times(t.s>0?.25:.75),e.s=o.s):!t.d||o.isZero()?(e=t.s<0?Xt(this,i,n):new this(0),e.s=o.s):!o.d||t.isZero()?(e=Xt(this,r,1).times(.5),e.s=o.s):t.s<0?(this.precision=r,this.rounding=1,e=this.atan(W(o,t,r,1)),t=Xt(this,r,1),this.precision=i,this.rounding=n,e=o.s<0?e.minus(t):e.plus(t)):e=this.atan(W(o,t,r,1)),e}function os(o){return new this(o).cbrt()}function rs(o){return O(o=new this(o),o.e+1,2)}function ss(o,t,e){return new this(o).clamp(t,e)}function as(o){if(!o||typeof o!="object")throw Error(ui+"Object expected");var t,e,i,n=o.defaults===!0,r=["precision",1,_e,"rounding",0,8,"toExpNeg",-Te,0,"toExpPos",0,Te,"maxE",0,Te,"minE",-Te,0,"modulo",0,9];for(t=0;t<r.length;t+=3)if(e=r[t],n&&(this[e]=sn[e]),(i=o[e])!==void 0)if(Rt(i)===i&&i>=r[t+1]&&i<=r[t+2])this[e]=i;else throw Error(fe+e+": "+i);if(e="crypto",n&&(this[e]=sn[e]),(i=o[e])!==void 0)if(i===!0||i===!1||i===0||i===1)if(i)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[e]=!0;else throw Error(ko);else this[e]=!1;else throw Error(fe+e+": "+i);return this}function cs(o){return new this(o).cos()}function ls(o){return new this(o).cosh()}function Ro(o){var t,e,i;function n(r){var a,l,d,f=this;if(!(f instanceof n))return new n(r);if(f.constructor=n,Po(r)){f.s=r.s,N?!r.d||r.e>n.maxE?(f.e=NaN,f.d=null):r.e<n.minE?(f.e=0,f.d=[0]):(f.e=r.e,f.d=r.d.slice()):(f.e=r.e,f.d=r.d?r.d.slice():r.d);return}if(d=typeof r,d==="number"){if(r===0){f.s=1/r<0?-1:1,f.e=0,f.d=[0];return}if(r<0?(r=-r,f.s=-1):f.s=1,r===~~r&&r<1e7){for(a=0,l=r;l>=10;l/=10)a++;N?a>n.maxE?(f.e=NaN,f.d=null):a<n.minE?(f.e=0,f.d=[0]):(f.e=a,f.d=[r]):(f.e=a,f.d=[r]);return}if(r*0!==0){r||(f.s=NaN),f.e=NaN,f.d=null;return}return si(f,r.toString())}if(d==="string")return(l=r.charCodeAt(0))===45?(r=r.slice(1),f.s=-1):(l===43&&(r=r.slice(1)),f.s=1),Ao.test(r)?si(f,r):Zr(f,r);if(d==="bigint")return r<0?(r=-r,f.s=-1):f.s=1,si(f,r.toString());throw Error(fe+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=as,n.clone=Ro,n.isDecimal=Po,n.abs=Gr,n.acos=Qr,n.acosh=Jr,n.add=Xr,n.asin=Yr,n.asinh=ts,n.atan=es,n.atanh=is,n.atan2=ns,n.cbrt=os,n.ceil=rs,n.clamp=ss,n.cos=cs,n.cosh=ls,n.div=us,n.exp=ds,n.floor=fs,n.hypot=_s,n.ln=hs,n.log=ps,n.log10=gs,n.log2=ms,n.max=bs,n.min=ws,n.mod=ys,n.mul=vs,n.pow=Ps,n.random=Cs,n.round=Ms,n.sign=ks,n.sin=Ts,n.sinh=As,n.sqrt=Ss,n.sub=xs,n.sum=Is,n.tan=Es,n.tanh=Os,n.trunc=Rs,o===void 0&&(o={}),o&&o.defaults!==!0)for(i=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],t=0;t<i.length;)o.hasOwnProperty(e=i[t++])||(o[e]=this[e]);return n.config(o),n}function us(o,t){return new this(o).div(t)}function ds(o){return new this(o).exp()}function fs(o){return O(o=new this(o),o.e+1,3)}function _s(){var o,t,e=new this(0);for(N=!1,o=0;o<arguments.length;)if(t=new this(arguments[o++]),t.d)e.d&&(e=e.plus(t.times(t)));else{if(t.s)return N=!0,new this(1/0);e=t}return N=!0,e.sqrt()}function Po(o){return o instanceof Ke||o&&o.toStringTag===To||!1}function hs(o){return new this(o).ln()}function ps(o,t){return new this(o).log(t)}function ms(o){return new this(o).log(2)}function gs(o){return new this(o).log(10)}function bs(){return Io(this,arguments,-1)}function ws(){return Io(this,arguments,1)}function ys(o,t){return new this(o).mod(t)}function vs(o,t){return new this(o).mul(t)}function Ps(o,t){return new this(o).pow(t)}function Cs(o){var t,e,i,n,r=0,a=new this(1),l=[];if(o===void 0?o=this.precision:Nt(o,1,_e),i=Math.ceil(o/F),this.crypto)if(crypto.getRandomValues)for(t=crypto.getRandomValues(new Uint32Array(i));r<i;)n=t[r],n>=429e7?t[r]=crypto.getRandomValues(new Uint32Array(1))[0]:l[r++]=n%1e7;else if(crypto.randomBytes){for(t=crypto.randomBytes(i*=4);r<i;)n=t[r]+(t[r+1]<<8)+(t[r+2]<<16)+((t[r+3]&127)<<24),n>=214e7?crypto.randomBytes(4).copy(t,r):(l.push(n%1e7),r+=4);r=i/4}else throw Error(ko);else for(;r<i;)l[r++]=Math.random()*1e7|0;for(i=l[--r],o%=F,i&&o&&(n=It(10,F-o),l[r]=(i/n|0)*n);l[r]===0;r--)l.pop();if(r<0)e=0,l=[0];else{for(e=-1;l[0]===0;e-=F)l.shift();for(i=1,n=l[0];n>=10;n/=10)i++;i<F&&(e-=F-i)}return a.e=e,a.d=l,a}function Ms(o){return O(o=new this(o),o.e+1,this.rounding)}function ks(o){return o=new this(o),o.d?o.d[0]?o.s:0*o.s:o.s||NaN}function Ts(o){return new this(o).sin()}function As(o){return new this(o).sinh()}function Ss(o){return new this(o).sqrt()}function xs(o,t){return new this(o).sub(t)}function Is(){var o=0,t=arguments,e=new this(t[o]);for(N=!1;e.s&&++o<t.length;)e=e.plus(t[o]);return N=!0,O(e,this.precision,this.rounding)}function Es(o){return new this(o).tan()}function Os(o){return new this(o).tanh()}function Rs(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 Ke=k.constructor=Ro(sn);ai=new Ke(ai);ci=new Ke(ci);var I=Ke;function Se(o){let t=Dt.isSuiCoin(o.coin_type_a),e=Dt.isSuiCoin(o.coin_type_b);return{is_adjust_coin_a:t,is_adjust_coin_b:e}}function Ns(o,t){return I.ceil(I(o).div(1+t)).toString()}var Ut=class{static createCollectRewarderAndFeeParams(t,e,i,n,r,a){r===void 0&&(r=[...n]),a===void 0&&(a=[...n]);let l=dn(i.coin_type_a),d=dn(i.coin_type_b);if(i.collect_fee){let h=Dt.buildCoinForAmount(e,r,BigInt(0),l,!1);r=h.remain_coins;let b=Dt.buildCoinForAmount(e,a,BigInt(0),d,!1);a=b.remain_coins,e=t.Position.createCollectFeePayload({pool_id:i.pool_id,pos_id:i.pos_id,coin_type_a:i.coin_type_a,coin_type_b:i.coin_type_b},e,h.target_coin,b.target_coin)}let f=[];return i.rewarder_coin_types.forEach(h=>{switch(dn(h)){case l:f.push(Dt.buildCoinForAmount(e,r,BigInt(0),h,!1).target_coin);break;case d:f.push(Dt.buildCoinForAmount(e,a,BigInt(0),h,!1).target_coin);break;default:f.push(Dt.buildCoinForAmount(e,n,BigInt(0),h,!1).target_coin);break}}),e=t.Rewarder.createCollectRewarderPayload(i,e,f),e}static async adjustTransactionForGas(t,e,i,n){n.setSender(t.getSenderAddress());let r=Dt.selectCoinAssetGreaterThanOrEqual(e,i).selected_coins,a=Dt.calculateTotalBalance(e);if(r.length===0&&R("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let l=await t.FullClient.calculationTxGas(un.from(n));if(Dt.selectCoinAssetGreaterThanOrEqual(e,BigInt(l),r.map(f=>f.coin_object_id)).selected_coins.length===0){let f=BigInt(l)+BigInt(500);if(a-i<f){i-=f,i<0&&R("InsufficientBalance","gas Insufficient balance");let h=new un;return{fixAmount:i,newTx:h}}}return{fixAmount:i}}static async buildAddLiquidityFixTokenForGas(t,e,i,n,r,a,l){r=await this.buildAddLiquidityFixToken(t,e,i,r,a,l);let{is_adjust_coin_a:d}=Se(i),f=d?i.amount_a:i.amount_b,h=await this.adjustTransactionForGas(t,Dt.getCoinAssets(d?i.coin_type_a:i.coin_type_b,e),BigInt(f),r),{fixAmount:b}=h,P=h.newTx;if(P!=null){let M,T;if(d)i.amount_a=Number(b),M=this.buildAddLiquidityFixTokenCoinInput(P,!i.fix_amount_a,b.toString(),i.slippage,i.coin_type_a,e,!1,!0),T=this.buildAddLiquidityFixTokenCoinInput(P,i.fix_amount_a,i.amount_b,i.slippage,i.coin_type_b,e,!1,!0);else return i.amount_b=Number(b),M=this.buildAddLiquidityFixTokenCoinInput(P,!i.fix_amount_a,i.amount_a,i.slippage,i.coin_type_a,e,!1,!0),T=this.buildAddLiquidityFixTokenCoinInput(P,i.fix_amount_a,b.toString(),i.slippage,i.coin_type_b,e,!1,!0),i=this.fixAddLiquidityFixTokenParams(i,n.slippage,n.cur_sqrt_price),this.buildAddLiquidityFixTokenArgs(P,t,e,i,M,T)}return r}static async buildAddLiquidityFixToken(t,e,i,n,r,a){n=n||new un;let l,d;return r==null||a==null?(l=this.buildAddLiquidityFixTokenCoinInput(n,!i.fix_amount_a,i.amount_a,i.slippage,i.coin_type_a,e,!1,!0),d=this.buildAddLiquidityFixTokenCoinInput(n,i.fix_amount_a,i.amount_b,i.slippage,i.coin_type_b,e,!1,!0)):(l={target_coin:r,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},d={target_coin:a,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]}),n=this.buildAddLiquidityFixTokenArgs(n,t,e,i,l,d),n}static buildAddLiquidityFixTokenCoinInput(t,e,i,n,r,a,l=!0,d=!0){return e?Dt.buildCoinForAmountInterval(t,a,{amount_second:BigInt(Ns(i,n)),amount_first:BigInt(i)},r,l,d):Dt.buildCoinForAmount(t,a,BigInt(i),r,l,d)}static fixAddLiquidityFixTokenParams(t,e,i){let n=t.fix_amount_a?t.amount_a:t.amount_b,r=Fs.estLiquidityAndCoinAmountFromOneAmounts(Number(t.tick_lower),Number(t.tick_upper),new No.default(n),t.fix_amount_a,!0,e,i);return t.amount_a=t.fix_amount_a?t.amount_a:r.coin_amount_limit_a,t.amount_b=t.fix_amount_a?r.coin_amount_limit_b:t.amount_b,t}static buildAddLiquidityFixTokenArgs(t,e,i,n,r,a){let l=[n.coin_type_a,n.coin_type_b],d=n.is_open?"open_position_with_liquidity_by_fix_coin":"add_liquidity_by_fix_coin",{clmm_pool:f,integrate:h}=e.sdkOptions;n.is_open||(t=this.createCollectRewarderAndFeeParams(e,t,n,i,r.remain_coins,a.remain_coins));let b=Bs(f),P=n.is_open?[t.object(b.global_config_id),t.object(n.pool_id),t.pure.u32(Number(Fo(BigInt(n.tick_lower)).toString())),t.pure.u32(Number(Fo(BigInt(n.tick_upper)).toString())),r.target_coin,a.target_coin,t.pure.u64(n.amount_a),t.pure.u64(n.amount_b),t.pure.bool(n.fix_amount_a),t.object(Bo)]:[t.object(b.global_config_id),t.object(n.pool_id),t.object(n.pos_id),r.target_coin,a.target_coin,t.pure.u64(n.amount_a),t.pure.u64(n.amount_b),t.pure.bool(n.fix_amount_a),t.object(Bo)];return t.moveCall({target:`${h.published_at}::${Qt}::${d}`,typeArguments:l,arguments:P}),t}static checkCoinThreshold(t,e,i,n,r,a){e&&i.moveCall({target:`${t.sdkOptions.integrate.published_at}::${me}::check_coin_threshold`,typeArguments:[a],arguments:[n,i.pure.u64(r)]})}};var te=class o{static getDefaultSqrtPriceLimit(t){return new qo.default(t?Ds:qs)}static getDefaultOtherAmountThreshold(t){return t?$s:Ls}static async buildSwapTransactionForGas(t,e,i,n){let r=this.buildSwapTransaction(t,e,i);r.setSender(t.getSenderAddress());let a=await this.adjustTransactionForGas(t,Lt.getCoinAssets(e.a2b?e.coin_type_a:e.coin_type_b,i),BigInt(e.by_amount_in?e.amount:e.amount_limit),r),{fixAmount:l,newTx:d}=a;if(d!==void 0){d.setSender(t.getSenderAddress()),e.by_amount_in?e.amount=l.toString():e.amount_limit=l.toString(),e=await this.fixSwapParams(t,e,n);let f=Lt.buildCoinForAmount(r,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a),h=Lt.buildCoinForAmount(r,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b);r=this.buildSwapTransactionArgs(d,e,t.sdkOptions,f,h)}return r}static buildSwapTransactionArgs(t,e,i,n,r){let{clmm_pool:a,integrate:l}=i,d=o.getDefaultSqrtPriceLimit(e.a2b),f=[e.coin_type_a,e.coin_type_b],{global_config_id:h}=jo(a);h===void 0&&R("InvalidConfig","clmm.config.global_config_id is undefined");let b=e.swap_partner!==void 0,P=b?e.a2b?"swap_a2b_with_partner":"swap_b2a_with_partner":e.a2b?"swap_a2b":"swap_b2a",M=b?[t.object(h),t.object(e.pool_id),t.object(e.swap_partner),n.target_coin,r.target_coin,t.pure.bool(e.by_amount_in),t.pure.u64(e.amount),t.pure.u64(e.amount_limit),t.pure.u128(d.toString()),t.object(_i)]:[t.object(h),t.object(e.pool_id),n.target_coin,r.target_coin,t.pure.bool(e.by_amount_in),t.pure.u64(e.amount),t.pure.u64(e.amount_limit),t.pure.u128(d.toString()),t.object(_i)];return t.moveCall({target:`${l.published_at}::${Qt}::${P}`,typeArguments:f,arguments:M}),t}static async adjustTransactionForGas(t,e,i,n){n.setSender(t.getSenderAddress());let r=Lt.selectCoinAssetGreaterThanOrEqual(e,i).selected_coins,a=Lt.calculateTotalBalance(e);if(r.length===0&&R("InsufficientBalance",`Insufficient balance exceed amount ${i} real amount ${a}`),a-i>1e9)return{fixAmount:i};let l=await t.FullClient.calculationTxGas(n);if(Lt.selectCoinAssetGreaterThanOrEqual(e,BigInt(l),r.map(f=>f.coin_object_id)).selected_coins.length===0){let f=BigInt(l)+BigInt(500);if(a-i<f){i-=f,i<0&&R("InsufficientBalance","gas Insufficient balance");let h=new fn;return{fixAmount:i,newTx:h}}}return{fixAmount:i}}static buildSwapTransaction(t,e,i){let n=new fn;n.setSender(t.getSenderAddress());let r=Lt.buildCoinForAmount(n,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1),a=Lt.buildCoinForAmount(n,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1);return n=this.buildSwapTransactionArgs(n,e,t.sdkOptions,r,a),n}static async fixSwapParams(t,e,i){let{current_pool:n}=i;try{let r=await t.Swap.preSwap({decimals_a:i.decimals_a,decimals_b:i.decimals_b,a2b:e.a2b,by_amount_in:e.by_amount_in,amount:e.amount,pool:n,current_sqrt_price:n.current_sqrt_price,coin_type_a:n.coin_type_a,coin_type_b:n.coin_type_b}),a=i.by_amount_in?r.estimated_amount_out:r.estimated_amount_in,l=js(a,i.slippage,!i.by_amount_in);e.amount_limit=l.toString()}catch(r){qt("InvalidConfig",r)}return e}static async buildSwapTransactionWithoutTransferCoinsForGas(t,e,i,n){let{tx:r,coin_ab_s:a}=o.buildSwapTransactionWithoutTransferCoins(t,e,i);r.setSender(t.getSenderAddress());let l=await o.adjustTransactionForGas(t,Lt.getCoinAssets(e.a2b?e.coin_type_a:e.coin_type_b,i),BigInt(e.by_amount_in?e.amount:e.amount_limit),r),{fixAmount:d,newTx:f}=l;if(f!==void 0){f.setSender(t.getSenderAddress()),e.by_amount_in?e.amount=d.toString():e.amount_limit=d.toString(),e=await o.fixSwapParams(t,e,n);let h=Lt.buildCoinForAmount(r,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1,!0),b=Lt.buildCoinForAmount(r,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1,!0),P=o.buildSwapTransactionWithoutTransferCoinArgs(t,f,e,t.sdkOptions,h,b);r=P.tx,a=P.txRes}return{tx:r,coin_ab_s:a}}static buildSwapTransactionWithoutTransferCoins(t,e,i){let n=new fn;n.setSender(t.getSenderAddress());let r=Lt.buildCoinForAmount(n,i,e.a2b?BigInt(e.by_amount_in?e.amount:e.amount_limit):BigInt(0),e.coin_type_a,!1,!0),a=Lt.buildCoinForAmount(n,i,e.a2b?BigInt(0):BigInt(e.by_amount_in?e.amount:e.amount_limit),e.coin_type_b,!1,!0),l=o.buildSwapTransactionWithoutTransferCoinArgs(t,n,e,t.sdkOptions,r,a);return{tx:l.tx,coin_ab_s:l.txRes}}static buildSwapTransactionWithoutTransferCoinArgs(t,e,i,n,r,a){let{clmm_pool:l,integrate:d}=n,f=o.getDefaultSqrtPriceLimit(i.a2b),{global_config_id:h}=jo(l);h===void 0&&R("InvalidConfig","clmm.config.global_config_id is undefined");let b=i.swap_partner!==void 0,P=b?"swap_with_partner":"swap",M=b?bo:me,T=b?[e.object(h),e.object(i.pool_id),e.object(i.swap_partner),r.target_coin,a.target_coin,e.pure.bool(i.a2b),e.pure.bool(i.by_amount_in),e.pure.u64(i.amount),e.pure.u128(f.toString()),e.pure.bool(!1),e.object(_i)]:[e.object(h),e.object(i.pool_id),r.target_coin,a.target_coin,e.pure.bool(i.a2b),e.pure.bool(i.by_amount_in),e.pure.u64(i.amount),e.pure.u128(f.toString()),e.pure.bool(!1),e.object(_i)],S=[i.coin_type_a,i.coin_type_b],L=e.moveCall({target:`${d.published_at}::${M}::${P}`,typeArguments:S,arguments:T});if(i.by_amount_in){let j=i.a2b?i.coin_type_b:i.coin_type_a,V=i.a2b?L[1]:L[0],B=Number(i.amount_limit);Ut.checkCoinThreshold(t,i.by_amount_in,e,V,B,j)}return{tx:e,txRes:L}}};function lc(o,t,e){let i=t.mul(e).shln(64),n=t.shln(64).add(o.mul(e));return _n.divRoundUp(i,n)}function uc(o,t,e){let i=t.mul(e).shln(64),n=t.shln(64).sub(o.mul(e));return _n.divRoundUp(i,n)}function dc(o,t,e){return e.sub(_n.divRoundUp(o.shln(64),t))}function fc(o,t,e){return e.add(o.shln(64).div(t))}function gc(o){let t=new Ft.default(o);return t.lt(Vt)?{bits:t.neg().xor(new Ft.default(2).pow(new Ft.default(64)).sub(new Ft.default(1))).add(new Ft.default(1)).toString()}:{bits:t.toString()}}function Uo(o){return{coin_type_a:o.coin_type_a,coin_type_b:o.coin_type_b,current_sqrt_price:new Ft.default(o.current_sqrt_price),current_tick_index:o.current_tick_index,fee_growth_global_a:new Ft.default(o.fee_growth_global_a),fee_growth_global_b:new Ft.default(o.fee_growth_global_b),fee_protocol_coin_a:new Ft.default(o.fee_protocol_coin_a),fee_protocol_coin_b:new Ft.default(o.fee_protocol_coin_b),fee_rate:new Ft.default(o.fee_rate),liquidity:new Ft.default(o.liquidity),tick_indexes:[],tick_spacing:Number(o.tick_spacing),ticks:[],collection_name:""}}function Ks(o,t,e,i,n,r){if(e===Vt)return{amount_in:Vt,amount_out:Vt,next_sqrt_price:t,fee_amount:Vt};let a=o.gte(t),l,d,f,h;if(r){let b=ge.checkMulDivFloor(i,ge.checkUnsignedSub(hi,n),hi,64),P=$o(o,t,e,a);P.gt(b)?(l=b,h=ge.checkUnsignedSub(i,b),f=Us(o,e,b,a)):(l=P,h=ge.checkMulDivCeil(l,n,hi.sub(n),64),f=t),d=Lo(o,f,e,a)}else{let b=Lo(o,t,e,a);b.gt(i)?(d=i,f=Vs(o,e,i,a)):(d=b,f=t),l=$o(o,f,e,a),h=ge.checkMulDivCeil(l,n,hi.sub(n),64)}return{amount_in:l,amount_out:d,next_sqrt_price:f,fee_amount:h}}function Vo(o,t,e,i,n){let r=e,a=i.liquidity,{current_sqrt_price:l}=i,d={amount_in:Vt,amount_out:Vt,fee_amount:Vt,ref_amount:Vt,next_sqrt_price:Vt,cross_tick_num:0},f,h,b=te.getDefaultSqrtPriceLimit(o);for(let P of n){if(o&&i.current_tick_index<P.index||!o&&i.current_tick_index>=P.index||P===null)continue;o&&b.gt(P.sqrt_price)||!o&&b.lt(P.sqrt_price)?f=b:f=P.sqrt_price;let M=Ks(l,f,a,r,i.fee_rate,t);if(M.amount_in.eq(Vt)||(r=t?r.sub(M.amount_in.add(M.fee_amount)):r.sub(M.amount_out)),d.amount_in=d.amount_in.add(M.amount_in),d.amount_out=d.amount_out.add(M.amount_out),d.fee_amount=d.fee_amount.add(M.fee_amount),M.next_sqrt_price.eq(P.sqrt_price)?(h=P.liquidity_net.mul(new Ft.default(-1)),o?ge.is_neg(h)?a=a.add(new Ft.default(Do(BigInt(h.toString()),128))):a=a.add(h):ge.is_neg(h)?a=a.sub(new Ft.default(Do(BigInt(h.toString()),128))):a=a.sub(h),l=P.sqrt_price):l=M.next_sqrt_price,d.cross_tick_num+=1,r.eq(Vt))break}return d.amount_in=d.amount_in.add(d.fee_amount),d.next_sqrt_price=l,d}var Kt=re(Gt());import{asIntN as be,buildNFT as Hs,d as He,DETAILS_KEYS as pi,extractStructTagFromType as xe,getMoveObjectType as Ko,getObjectDeletedResponse as Ws,getObjectFields as pn,getObjectId as hn,getObjectNotExistsResponse as Zs,getObjectOwner as zs,MathUtil as Gs}from"@cetusprotocol/common-sdk";function Qs(o,t,e){let i=xe(o).name,n=xe(t).name;return`${i}-${n}[${e}]`}function mi(o){let t=Ko(o),e=xe(t),i=pn(o);i==null&&R("InvalidPoolObject",`Pool id ${hn(o)} not exists.`,{[pi.METHOD_NAME]:"buildPool"});let n=[];i.rewarder_manager.fields.rewarders.forEach(d=>{let{emissions_per_second:f}=d.fields,h=Gs.fromX64(new Kt.default(f)),b=Math.floor(h.toNumber()*60*60*24);n.push({emissions_per_second:f,coin_type:xe(d.fields.reward_coin.fields.name).source_address,growth_global:d.fields.growth_global,emissions_every_day:b})});let r=i.is_pause,a={disable_add_liquidity:r,disable_remove_liquidity:r,disable_swap:r,disable_flash_loan:r,disable_collect_fee:r,disable_collect_reward:r},l={id:hn(o),pool_type:t,coin_type_a:e.type_arguments[0],coin_type_b:e.type_arguments[1],coin_amount_a:i.coin_a,coin_amount_b:i.coin_b,current_sqrt_price:i.current_sqrt_price,current_tick_index: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=Qs(l.coin_type_a,l.coin_type_b,l.tick_spacing),l}function gi(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&R("InvalidPositionObject",`Position not exists. Get Position error:${o.error}`,{[pi.METHOD_NAME]:"buildPosition"});let t={creator:"",description:"",image_url:"",link:"",name:"",project_url:""},e={...t,pos_object_id:"",owner:"",type:"",coin_type_a:"",coin_type_b:"",liquidity:"",tick_lower_index:0,tick_upper_index:0,index:0,pool:"",reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0",fee_growth_inside_a:"0",fee_owned_a:"0",fee_growth_inside_b:"0",fee_owned_b:"0",position_status:"Exists"},i=pn(o);if(i){let a=Ko(o),l=zs(o);"nft"in i?(i=i.nft.fields,t.description=i.description,t.name=i.name,t.link=i.url):t=Hs(o),e={...t,pos_object_id:i.id.id,owner:l.AddressOwner,type:a,liquidity:i.liquidity,coin_type_a:i.coin_type_a.fields.name,coin_type_b:i.coin_type_b.fields.name,tick_lower_index: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=Ws(o);n&&(e.pos_object_id=n.objectId,e.position_status="Deleted");let r=Zs(o);return r&&(e.pos_object_id=r,e.position_status="NotExists"),e}function We(o){let t={reward_amount_owned_0:"0",reward_amount_owned_1:"0",reward_amount_owned_2:"0",reward_growth_inside_0:"0",reward_growth_inside_1:"0",reward_growth_inside_2:"0"};o="fields"in o?o.fields:o,o.rewards.forEach((r,a)=>{let{amount_owned:l,growth_inside:d}="fields"in r?r.fields:r;a===0?(t.reward_amount_owned_0=l,t.reward_growth_inside_0=d):a===1?(t.reward_amount_owned_1=l,t.reward_growth_inside_1=d):a===2&&(t.reward_amount_owned_2=l,t.reward_growth_inside_2=d)});let e="fields"in o.tick_lower_index?o.tick_lower_index.fields.bits:o.tick_lower_index.bits,i="fields"in o.tick_upper_index?o.tick_upper_index.fields.bits:o.tick_upper_index.bits;return{liquidity:o.liquidity,tick_lower_index:be(BigInt(e)),tick_upper_index:be(BigInt(i)),...t,fee_growth_inside_a:o.fee_growth_inside_a,fee_owned_a:o.fee_owned_a,fee_growth_inside_b:o.fee_growth_inside_b,fee_owned_b:o.fee_owned_b,pos_object_id:o.position_id}}function bi(o){(o.error!=null||o.data?.content?.dataType!=="moveObject")&&R("InvalidTickObject",`Tick not exists. Get tick data error:${o.error}`,{[pi.METHOD_NAME]:"buildTickData"});let e=pn(o).value.fields.value.fields;return{object_id:hn(o),index:be(BigInt(e.index.fields.bits)),sqrt_price:new Kt.default(e.sqrt_price),liquidity_net:new Kt.default(e.liquidity_net.fields.bits),liquidity_gross:new Kt.default(e.liquidity_gross),fee_growth_outside_a:new Kt.default(e.fee_growth_outside_a),fee_growth_outside_b:new Kt.default(e.fee_growth_outside_b),rewarders_growth_outside:e.rewards_growth_outside}}function Ho(o){(!o||!o.index||!o.sqrt_price||!o.liquidity_net||!o.liquidity_gross||!o.fee_growth_outside_a||!o.fee_growth_outside_b)&&R("InvalidTickFields","Invalid tick fields.",{[pi.METHOD_NAME]:"buildTickDataByEvent"});let t=be(BigInt(o.index.bits)),e=new Kt.default(o.sqrt_price),i=new Kt.default(o.liquidity_net.bits),n=new Kt.default(o.liquidity_gross),r=new Kt.default(o.fee_growth_outside_a),a=new Kt.default(o.fee_growth_outside_b),l=o.rewards_growth_outside||[];return{object_id:"",index:t,sqrt_price:e,liquidity_net:i,liquidity_gross:n,fee_growth_outside_a:r,fee_growth_outside_b:a,rewarders_growth_outside:l}}function Sc(o,t){return`Cetus LP | Pool${o}-${t}`}function Wo(o,t,e){let i=[],{timestampMs:n,events:r}=o,a=["AddLiquidityEvent","RemoveLiquidityEvent","CollectFeeEvent","CollectRewardEvent","CollectRewardV2Event","HarvestEvent","AddLiquidityV2Event","RemoveLiquidityV2Event"];return r?.forEach((l,d)=>{let f=xe(l.type).name;if(a.includes(f)){let h={tx_digest:l.id.txDigest,package_id:l.packageId,transaction_module:l.transactionModule,sender:l.sender,type:l.type,timestamp_ms:n||"0",parsed_json:l.parsedJson,index:`${t}_${d}`};switch(f){case"CollectFeeEvent":e.includes(h.parsed_json.position)&&(He(h.parsed_json.amount_a).gt(0)||He(h.parsed_json.amount_b).gt(0))&&i.push(h);break;case"RemoveLiquidityEvent":case"AddLiquidityEvent":case"AddLiquidityV2Event":case"RemoveLiquidityV2Event":(He(h.parsed_json.amount_a).gt(0)||He(h.parsed_json.amount_b).gt(0))&&i.push(h);break;case"CollectRewardEvent":case"HarvestEvent":case"CollectRewardV2Event":(e.includes(h.parsed_json.position)||e.includes(h.parsed_json.wrapped_position_id))&&He(h.parsed_json.amount).gt(0)&&i.push(h);break;default:break}}}),i}function Zo(o,t,e,i){let n=[],{timestampMs:r,events:a}=o;return a?.forEach((l,d)=>{let{name:f,address:h}=xe(l.type);if(mo.includes(f)&&e.includes(h)&&i===l.parsedJson.pool){let b={tx:l.id.txDigest,sender:l.sender,type:l.type,block_time:r||"0",index:`${t}_${d}`,parsed_json:l.parsedJson};n.push(b)}}),n}function wi(o,t,e,i){t.moveCall({target:`${o.sdkOptions.integrate.published_at}::${wo}::transfer_coin_to_sender`,typeArguments:[i],arguments:[e]})}function zo(o,t,e,i,n){n!=null?t.transferObjects([e],t.pure.address(n)):wi(o,t,e,i)}import{asUintN as Ee,CACHE_TIME_24H as yi,ClmmPoolUtil as na,CLOCK_ADDRESS as nr,CoinAssist as vi,createFullClient as oa,d as Oe,DETAILS_KEYS as Z,extractStructTagFromType as Re,getObjectFields as ra,getObjectPreviousTransactionDigest as sa,getPackagerConfigs as gn,isSortedSymbols as or,TickMath as bn,tickScore as aa,TickUtil as rr}from"@cetusprotocol/common-sdk";import{asIntN as Go,d as Qo,extractStructTagFromType as Js,fixCoinType as Jo,getObjectFields as mn,getObjectType as Xs}from"@cetusprotocol/common-sdk";var Ys=1e4,ee=class{static parseClmmVestInfo(t){let e=mn(t),i=Xs(t),n=Js(i),r=e.global_vesting_periods.map(l=>({period:l.fields.period,release_time:l.fields.release_time,redeemed_amount:l.fields.redeemed_amount,percentage:Qo(l.fields.percentage).div(Ys).toNumber()}));return{id:e.id.id,balance:e.balance,global_vesting_periods:r,total_value:e.total_value,total_cetus_amount:e.total_cetus_amount,redeemed_amount:e.redeemed_amount,start_time:e.start_time,type:n.full_address,positions:{id:e.positions.fields.id,size:e.positions.fields.size}}}static parsePositionVesting(t){return{position_id:t.position_id,cetus_amount:t.cetus_amount,redeemed_amount:t.redeemed_amount,is_paused:t.is_paused,impaired_a:t.impaired_a,impaired_b:t.impaired_b,period_details:t.period_details,coin_type_a:Jo(t.coin_a.name,!1),coin_type_b:Jo(t.coin_b.name,!1)}}static parsePoolLiquiditySnapshot(t){let e=mn(t);return{current_sqrt_price:e.current_sqrt_price,remove_percent:Qo(e.remove_percent).div(1e6).toString(),snapshots:{id:e.snapshots.fields.id.id,size:e.snapshots.fields.size}}}static parsePositionSnapshot(t){let e=mn(t),i=e.value.fields.value.fields;return{position_id:e.name,liquidity:i.liquidity,tick_lower_index:Go(BigInt(i.tick_lower_index.fields.bits)),tick_upper_index:Go(BigInt(i.tick_upper_index.fields.bits)),fee_owned_a:i.fee_owned_a,fee_owned_b:i.fee_owned_b,value_cut:i.value_cut,rewards:i.rewards}}};import{FullRpcUrlMainnet as ta}from"@cetusprotocol/common-sdk";var Xo={clmmConfig:{pools_id:"0xf699e7f2276f5c9a75944b37a0c5b5d9ddfd2471bf6242483b03ab2887d198d0",global_config_id:"0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f",global_vault_id:"0xce7bceef26d3ad1f6d9b6f13a953f053e6ed3ca77907516481ce99ae8e588f2b",admin_cap_id:"0x89c1a321291d15ddae5a086c9abc533dff697fde3d89e0ca836c41af73e36a75"},cetusConfig:{coin_list_id:"0x8cbc11d9e10140db3d230f50b4d30e9b721201c0083615441707ffec1ef77b23",launchpad_pools_id:"0x1098fac992eab3a0ab7acf15bb654fc1cf29b5a6142c4ef1058e6c408dd15115",clmm_pools_id:"0x15b6a27dd9ae03eb455aba03b39e29aad74abd3757b8e18c0755651b2ae5b71e",admin_cap_id:"0x39d78781750e193ce35c45ff32c6c0c3f2941fa3ddaf8595c90c555589ddb113",global_config_id:"0x0408fa4e4a4c03cc0de8f23d0c2bbfe8913d178713c9a271ed4080973fe42d8f",coin_list_handle:"0x49136005e90e28c4695419ed4194cc240603f1ea8eb84e62275eaff088a71063",launchpad_pools_handle:"0x5e194a8efcf653830daf85a85b52e3ae8f65dc39481d54b2382acda25068375c",clmm_pools_handle:"0x37f60eb2d9d227949b95da8fea810db3c32d1e1fa8ed87434fc51664f87d83cb"}},Yo={env:"mainnet",full_rpc_url:ta,cetus_config:{package_id:"0x95b8d278b876cae22206131fb9724f701c9444515813042f54f0a426c9a3bc2f",published_at:"0xba7e740c3c002673dbe69ad5fbdb0691ec260170e141297cefb982e7081fde52",version:2,config:Xo.cetusConfig},clmm_pool:{package_id:"0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb",published_at:"0xdb5cd62a06c79695bfc9982eb08534706d3752fe123b48e0144f480209b3117f",version:13,config:Xo.clmmConfig},integrate:{package_id:"0x996c4d9480708fb8b92aa7acf819fb0497b5ec8e65ba06601cae2fb6db3312c3",published_at:"0x6219e52e9a9d2cce4d0e53f27a9152a827f3572678e5bdec85f528e3e175e479",version:14},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"}}},tr=["0x1eabed72c53feb3805120a081dc15963c204dc8d091542592abaf7a35689b2fb","0xdb5cd62a06c79695bfc9982eb08534706d3752fe123b48e0144f480209b3117f"];import{FullRpcUrlTestnet as ea}from"@cetusprotocol/common-sdk";var er={env:"testnet",full_rpc_url:ea,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:"0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8",config:{pools_id:"0x20a086e6fa0741b3ca77d033a65faf0871349b986ddbdde6fa1d85d78a5f4222",global_config_id:"0xc6273f844b4bc258952c4e477697aa12c918c8e08106fac6b934811298c9820a",global_vault_id:"0x71e74a999dd7959e483f758ddf573e85fa4c24944db33ff6763c9d85a9c045fe",admin_cap_id:"0xbf4c48590f403c38351de0e8aa13d6d91bf78fd8c04e93ac1d0269c44d70ae02",partners_id:"0xb5ae5ed3f403654ae1307aadc0140f746db41efb7bda92235257c84d90a1397e"}},integrate:{package_id:"0x36187418dd79415d50e2e5903f9b3caca582052005f062959c86da64e82107a9",published_at:"0x36187418dd79415d50e2e5903f9b3caca582052005f062959c86da64e82107a9",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"}}},ir=["0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8","0x5372d555ac734e272659136c2a0cd3227f9b92de67c80dc11250307268af2db8"];var Pi=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}async getPositionList(t,e="all"){let i={data:[],has_next_page:!0},n=await this._sdk.FullClient.getDynamicFieldsByPage(t,e);i.has_next_page=n.has_next_page,i.next_cursor=n.next_cursor;let r=n.data.map(l=>(l.error!=null&&R("InvalidConfig",`when getPositionList get position objects error: ${l.error}, please check the rpc, contracts address config and position id.`,{[Z.METHOD_NAME]:"getPositionList"}),l.name.value)),a=await this._sdk.Position.getSimplePositionList(r);return i.data=a,i}async getPoolImmutablesWithPage(t="all",e=!1){let{package_id:i}=this._sdk.sdkOptions.clmm_pool,n=[],r={data:[],has_next_page:!1},a=t==="all",l=`${i}_getPoolImmutables`;if(a){let d=this._sdk.getCache(l,e);d&&n.push(...d)}if(n.length===0)try{let d=`${i}::factory::CreatePoolEvent`,f=await this._sdk.FullClient.queryEventsByPage({MoveEventType:d},t);r.has_next_page=f.has_next_page,r.next_cursor=f.next_cursor,f.data.forEach(h=>{let b=h.parsedJson;b&&n.push({id:b.pool_id,tick_spacing:b.tick_spacing,coin_type_a:Re(b.coin_type_a).full_address,coin_type_b:Re(b.coin_type_b).full_address})})}catch(d){return qt("FetchError",d,{[Z.METHOD_NAME]:"getPoolImmutables"})}return r.data=n,a&&this._sdk.updateCache(`${i}_getPoolImmutables`,n,yi),r}async getPoolsWithPage(t="all",e=!1){let i={data:[],has_next_page:!1},n=await this.getPoolImmutablesWithPage(t,e),r=await this._sdk.FullClient.batchGetObjects(n.data.map(a=>a.id),{showContent:!0,showType:!0});for(let a of r){(a.error!=null||a.data?.content?.dataType!=="moveObject")&&R("InvalidPoolObject",`getPoolWithPages error code: ${a.error?.code??"unknown error"}, please check config and object ids`,{[Z.METHOD_NAME]:"getPoolsWithPage"});let l=mi(a);i.data.push(l);let d=`${l.id}_getPoolObject`;this._sdk.updateCache(d,l,yi)}return i.has_next_page=n.has_next_page,i.next_cursor=n.next_cursor,i}async getPoolLiquiditySnapshot(t,e=!1){try{let i=await this._sdk.FullClient.getDynamicFieldObject({parentId:t,name:{type:"0x1::string::String",value:ho}}),n=ee.parsePoolLiquiditySnapshot(i);if(e){let a=(await this._sdk.FullClient.getDynamicFieldsByPage(n.snapshots.id)).data.map(l=>l.objectId);if(a.length>0){let l=await this._sdk.FullClient.batchGetObjects(a,{showContent:!0,showType:!0}),d=[];l.forEach(f=>{let h=ee.parsePositionSnapshot(f);d.push(h)}),n.position_snapshots=d}}return n}catch(i){return qt("InvalidPoolObject",i,{[Z.METHOD_NAME]:"getPoolLiquiditySnapshot",[Z.REQUEST_PARAMS]:t})}}async getPositionSnapshot(t,e){let i=await this._sdk.FullClient.getDynamicFieldObjects(t,e,"0x2::object::ID","address"),n=[];return i.forEach(r=>{try{let a=ee.parsePositionSnapshot(r);n.push(a)}catch(a){console.log("getPositionSnapshot error: ",a)}}),n}async getAssignPools(t){if(t.length===0)return[];let e=[],i=await this._sdk.FullClient.batchGetObjects(t,{showContent:!0,showType:!0});for(let n of i){(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidPoolObject",`getPools error code: ${n.error?.code??"unknown error"}, please check config and object ids`,{[Z.METHOD_NAME]:"getAssignPools"});let r=mi(n);e.push(r);let a=`${r.id}_getPoolObject`;this._sdk.updateCache(a,r,yi)}return e}async getPool(t,e=!0,i=!1){let n=`${t}_getPoolObject`,r=this._sdk.getCache(n,e);if(r!==void 0)return r;let a=await this._sdk.FullClient.getObject({id:t,options:{showType:!0,showContent:!0}});(a.error!=null||a.data?.content?.dataType!=="moveObject")&&R("InvalidPoolObject",`getPool error code: ${a.error?.code??"unknown error"}, please check config and object id`,{[Z.METHOD_NAME]:"getPool"});let l=mi(a);if(i){let d=await this.getPoolStatus(t);d&&(l.pool_status=d)}return this._sdk.updateCache(n,l),l}async getPoolByCoins(t,e){if(t.length===0)return[];for(let d=0;d<t.length;d++)t[d]==="0x2::sui::SUI"&&(t[d]="0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI");let i=this._sdk.sdkOptions.stats_pools_url;i||R("StatsPoolsUrlNotSet","statsPoolsUrl is not set in the sdk options.",{[Z.METHOD_NAME]:"getPoolByCoins"}),i+=`?order_by=-fees&limit=100&has_mining=true&has_farming=true&no_incentives=true&display_all_pools=true&coin_type=${t.join(",")}`;let n=await fetch(i),r;try{r=await n.json()}catch(d){qt("FetchError",d,{[Z.METHOD_NAME]:"getPoolByCoins",[Z.REQUEST_PARAMS]:i})}let a=r.data.lp_list,l=[];for(let d of a)t.includes(d.coin_a_address)&&t.includes(d.coin_b_address)&&(e!=null?d.object.feeRate===e&&l.push(d.address):l.push(d.address));return l.length>0?await this.getAssignPools(l):[]}async createPoolPayload(t,e){if(or(Ie(t.coin_type_a),Ie(t.coin_type_b))){let i=t.coin_type_b;t.coin_type_b=t.coin_type_a,t.coin_type_a=i;let n=t.metadata_b;t.metadata_b=t.metadata_a,t.metadata_a=n}return await this.createPoolAndAddLiquidity(t,e)}async createPoolRowPayload(t,e){if(or(Ie(t.coin_type_a),Ie(t.coin_type_b))){let i=t.coin_type_b;t.coin_type_b=t.coin_type_a,t.coin_type_a=i;let n=t.metadata_b;t.metadata_b=t.metadata_a,t.metadata_a=n}return await this.createPoolAndAddLiquidityRow(t,e)}async getClmmConfigs(t=!1){let{package_id:e}=this._sdk.sdkOptions.clmm_pool,i=`${e}_getInitEvent`,n=this._sdk.getCache(i,t);if(n!==void 0)return n;let r=await this._sdk.FullClient.getObject({id:e,options:{showPreviousTransaction:!0}}),a=sa(r),l=(await this._sdk.FullClient.queryEventsByPage({Transaction:a})).data,d={pools_id:"",global_config_id:"",global_vault_id:"",admin_cap_id:""};return l.length>0&&(l.forEach(f=>{let h=f.parsedJson;if(f.type)switch(Re(f.type).full_address){case`${e}::config::InitConfigEvent`:d.global_config_id=h.global_config_id,d.admin_cap_id=h.admin_cap_id;break;case`${e}::factory::InitFactoryEvent`:d.pools_id=h.pools_id;break;case`${e}::rewarder::RewarderInitEvent`:d.global_vault_id=h.global_vault_id;break;case`${e}::partner::InitPartnerEvent`:d.partners_id=h.partners_id;break;default:break}}),this._sdk.updateCache(i,d,yi)),d}async getPoolTransactionList({pool_id:t,pagination_args:e,order:i="descending",full_rpc_url:n}){let{FullClient:r,sdkOptions:a}=this._sdk,l;n?l=oa(new ia({url:n})):l=r;let d={data:[],has_next_page:!1},f=50,h=e,b=e.limit||10,P=a.env==="testnet"?ir:tr;do{let M=await l.queryTransactionBlocksByPage({ChangedObject:t},{...h,limit:50},i);M.data.forEach((T,S)=>{d.next_cursor=M.next_cursor;let L=Zo(T,S,P,t);d.data=[...d.data,...L]}),d.has_next_page=M.has_next_page,d.next_cursor=M.next_cursor,h.cursor=M.next_cursor}while(d.data.length<b&&d.has_next_page);return d.data.length>b&&(d.data=d.data.slice(0,b),d.next_cursor=d.data[d.data.length-1].tx),d}async calculateCreatePoolWithPrice(t){let{current_price:e,slippage:i,tick_spacing:n,add_mode_params:r,price_base_coin:a,coin_decimals_a:l,coin_decimals_b:d}=t,f=bn.priceToSqrtPriceX64(a==="coin_a"?Oe(e):Oe(1).div(e),l,d),h=0,b=0;if(r.is_full_range)h=rr.getMinIndex(n),b=rr.getMaxIndex(n);else{let{min_price:B,max_price:D}=r;h=bn.priceToInitializeTickIndex(a==="coin_a"?Oe(B):Oe(1).div(D),l,d,n),b=bn.priceToInitializeTickIndex(a==="coin_a"?Oe(D):Oe(1).div(B),l,d,n)}let{coin_amount:P,fix_amount_a:M}=t,{coin_amount_limit_a:T,coin_amount_limit_b:S,liquidity_amount:L,coin_amount_a:j,coin_amount_b:V}=na.estLiquidityAndCoinAmountFromOneAmounts(h,b,new sr.default(P),M,!0,i,f);return{coin_amount_a:j,coin_amount_b:V,coin_amount_limit_a:T,coin_amount_limit_b:S,liquidity:L,initialize_sqrt_price:f.toString(),tick_lower:h,tick_upper:b,fix_amount_a:M}}async createPoolAndAddLiquidity(t,e){e=e||new Ze,e.setSender(this.sdk.getSenderAddress());let{integrate:i,clmm_pool:n}=this.sdk.sdkOptions,r=gn(n),a=r.global_config_id,l=r.pools_id,d=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress()),f=vi.buildCoinForAmount(e,d,BigInt(t.amount_a),t.coin_type_a,!1,!0),h=vi.buildCoinForAmount(e,d,BigInt(t.amount_b),t.coin_type_b,!1,!0),b=[e.object(a),e.object(l),e.pure.u32(t.tick_spacing),e.pure.u128(t.initialize_sqrt_price),e.pure.string(t.uri),e.pure.u32(Number(Ee(BigInt(t.tick_lower)).toString())),e.pure.u32(Number(Ee(BigInt(t.tick_upper)).toString())),f.target_coin,h.target_coin,e.object(t.metadata_a),e.object(t.metadata_b),e.pure.bool(t.fix_amount_a),e.object(nr)];return e.moveCall({target:`${i.published_at}::pool_creator_v2::create_pool_v2`,typeArguments:[t.coin_type_a,t.coin_type_b],arguments:b}),wi(this._sdk,e,f.target_coin,t.coin_type_a),wi(this._sdk,e,h.target_coin,t.coin_type_b),e}async createPoolAndAddLiquidityRow(t,e){e=e||new Ze;let{clmm_pool:i}=this.sdk.sdkOptions,n=gn(i),r=n.global_config_id,a=n.pools_id,l=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress()),d=vi.buildCoinForAmount(e,l,BigInt(t.amount_a),t.coin_type_a,!1,!0),f=vi.buildCoinForAmount(e,l,BigInt(t.amount_b),t.coin_type_b,!1,!0),h=[e.object(r),e.object(a),e.pure.u32(t.tick_spacing),e.pure.u128(t.initialize_sqrt_price),e.pure.string(t.uri),e.pure.u32(Number(Ee(BigInt(t.tick_lower)).toString())),e.pure.u32(Number(Ee(BigInt(t.tick_upper)).toString())),d.target_coin,f.target_coin,e.object(t.metadata_a),e.object(t.metadata_b),e.pure.bool(t.fix_amount_a),e.object(nr)],b=e.moveCall({target:`${i.published_at}::pool_creator::create_pool_v2`,typeArguments:[t.coin_type_a,t.coin_type_b],arguments:h});return{tx:e,pos_id:b[0],remain_coin_a:b[1],remain_coin_b:b[2],remain_coin_type_a:t.coin_type_a,remain_coin_type_b:t.coin_type_b}}async createPoolWithPriceReturnPositionPayload(t,e){let{coin_type_a:i,coin_type_b:n,tick_spacing:r,uri:a,calculate_result:l}=t,{initialize_sqrt_price:d,tick_lower:f,tick_upper:h,liquidity:b,coin_amount_a:P,coin_amount_b:M,fix_amount_a:T,coin_amount_limit_a:S,coin_amount_limit_b:L}=l,j=await this._sdk.FullClient.fetchCoinMetadataId(i),V=await this._sdk.FullClient.fetchCoinMetadataId(n);if(j===null)return R("FetchError",`fetch coin ${i} metadata failed`,{[Z.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[Z.REQUEST_PARAMS]:t});if(V===null)return R("FetchError",`fetch coin ${n} metadata failed`,{[Z.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[Z.REQUEST_PARAMS]:t});let B=j,D=V;return this.createPoolRowPayload({tick_spacing:r,initialize_sqrt_price:d,uri:a||"",coin_type_a:i,coin_type_b:n,amount_a:T?P:S,amount_b:T?L:M,fix_amount_a:T,tick_lower:f,tick_upper:h,metadata_a:B,metadata_b:D},e)}async createPoolWithPricePayload(t){let{coin_type_a:e,coin_type_b:i,tick_spacing:n,uri:r,calculate_result:a}=t,{initialize_sqrt_price:l,tick_lower:d,tick_upper:f,liquidity:h,coin_amount_a:b,coin_amount_b:P,fix_amount_a:M,coin_amount_limit_a:T,coin_amount_limit_b:S}=a,L=await this._sdk.FullClient.fetchCoinMetadataId(e),j=await this._sdk.FullClient.fetchCoinMetadataId(i);if(L===null)return R("FetchError",`fetch coin ${e} metadata failed`,{[Z.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[Z.REQUEST_PARAMS]:t});if(j===null)return R("FetchError",`fetch coin ${i} metadata failed`,{[Z.METHOD_NAME]:"createPoolAndAddLiquidityWithPrice",[Z.REQUEST_PARAMS]:t});let V=L,B=j;return this.createPoolPayload({tick_spacing:n,initialize_sqrt_price:l,uri:r||"",coin_type_a:e,coin_type_b:i,amount_a:M?b:T,amount_b:M?S:P,fix_amount_a:M,tick_lower:d,tick_upper:f,metadata_a:V,metadata_b:B})}async fetchTicks(t){let e=[],i=[];for(;;){let r=await this.getTicks({pool_id:t.pool_id,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b,start:i,limit:512});if(e=[...e,...r],r.length<512)break;i=[Number(Ee(BigInt(r[r.length-1].index)))]}return e}async getTicks(t){let{integrate:e}=this.sdk.sdkOptions,i=[],n=[t.coin_type_a,t.coin_type_b],r=new Ze,a=r.makeMoveVec({elements:t.start.map(f=>r.pure.u32(f)),type:"u32"}),l=[r.object(t.pool_id),a,r.pure.u64(t.limit.toString())];r.moveCall({target:`${e.published_at}::${Jt}::fetch_ticks`,arguments:l,typeArguments:n});let d=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:r,sender:Ie("0x0")});return d.error!=null&&R("InvalidTickObjectId",`getTicks error code: ${d.error??"unknown error"}, please check config and tick object ids`,{[Z.METHOD_NAME]:"getTicks",[Z.REQUEST_PARAMS]:t}),d.events?.forEach(f=>{Re(f.type).name==="FetchTicksResultEvent"&&f.parsedJson.ticks.forEach(h=>{i.push(Ho(h))})}),i}async fetchPoolPositionInfoList(t){let{integrate:e}=this.sdk.sdkOptions,i=[],n=[],r=512;for(;;){let a=[t.coin_type_a,t.coin_type_b],l=new Ze,d=l.pure.vector("id",n.map(P=>P)),f=[l.object(t.pool_id),d,l.pure.u64(r)];l.moveCall({target:`${e.published_at}::${Jt}::fetch_positions`,arguments:f,typeArguments:a});let h=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:l,sender:Ie("0x0")});h.error!=null&&R("InvalidPositionRewardObject",`fetch position info error code: ${h.error??"unknown error"}, please check config and tick object ids`,{[Z.METHOD_NAME]:"fetchPoolPositionInfoList",[Z.REQUEST_PARAMS]:t});let b=[];if(h?.events?.forEach(P=>{Re(P.type).name==="FetchPositionsEvent"&&P.parsedJson.positions.forEach(M=>{let T=We(M);b.push(T)})}),i.push(...b),b.length<r)break;n=[b[b.length-1].pos_object_id]}return i}async fetchTicksByRpc(t){let e=[],i=null,n=50;for(;;){let r=[],a=await this.sdk.FullClient.getDynamicFields({parentId:t,cursor:i,limit:n});if(i=a.nextCursor,a.data.forEach(l=>{Re(l.objectType).module==="skip_list"&&r.push(l.objectId)}),e=[...e,...await this.getTicksByRpc(r)],!a.hasNextPage)break}return e}async getTicksByRpc(t){let e=[],i=await this.sdk.FullClient.batchGetObjects(t,{showContent:!0,showType:!0});for(let n of i){(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidTickObjectId",`getTicksByRpc error code: ${n.error?.code??"unknown error"}, please check config and tick object ids`,{[Z.METHOD_NAME]:"getTicksByRpc"});let r=bi(n);r!=null&&e.push(r)}return e}async getTickDataByIndex(t,e){let i={type:"u64",value:Ee(BigInt(aa(e).toString())).toString()},n=await this.sdk.FullClient.getDynamicFieldObject({parentId:t,name:i});return(n.error!=null||n.data?.content?.dataType!=="moveObject")&&R("InvalidTickIndex",`get tick by index: ${e} error: ${n.error}`,{[Z.METHOD_NAME]:"getTickDataByIndex"}),bi(n)}async getTickDataByObjectId(t){let e=await this.sdk.FullClient.getObject({id:t,options:{showContent:!0}});return(e.error!=null||e.data?.content?.dataType!=="moveObject")&&R("InvalidTickObjectId",`getTicksByRpc error code: ${e.error?.code??"unknown error"}, please check config and tick object ids`,{[Z.METHOD_NAME]:"getTickDataByObjectId"}),bi(e)}async getPartnerRefFeeAmount(t,e=!0){let i=await this._sdk.FullClient.batchGetObjects([t],{showOwner:!0,showContent:!0,showDisplay:e,showType:!0});i[0].data?.content?.dataType!=="moveObject"&&R("NotFoundPartnerObject",`get partner by object id: ${t} error: ${i[0].error}`,{[Z.METHOD_NAME]:"getPartnerRefFeeAmount"});let n=i[0].data.content.fields.balances,r=await this._sdk.FullClient.getDynamicFieldsByPage(n.fields.id.id),a=[];r.data.forEach(f=>{f.objectId!=null&&a.push(f.objectId)});let l=[];return(await this._sdk.FullClient.batchGetObjects(a,{showOwner:!0,showContent:!0,showDisplay:e,showType:!0})).forEach(f=>{(f.error!=null||f.data?.content?.dataType!=="moveObject")&&R("InvalidPartnerRefFeeFields",`get coin by object id: ${f.data.objectId} error: ${f.error}`,{[Z.METHOD_NAME]:"getPartnerRefFeeAmount"});let h={coin_type:f.data.content.fields.name,coin_object_id:f.data.objectId,balance:BigInt(f.data.content.fields.value)};l.push(h)}),l}async claimPartnerRefFeePayload(t,e,i){let n=new Ze,{clmm_pool:r}=this.sdk.sdkOptions,{global_config_id:a}=gn(r),l=[i],d=[n.object(a),n.object(t),n.object(e)];return n.moveCall({target:`${r.published_at}::${go}::claim_ref_fee`,arguments:d,typeArguments:l}),n}async getPoolStatus(t){try{let e=await this._sdk.FullClient.getDynamicFieldObject({parentId:t,name:{type:"0x1::string::String",value:"pool_status"}}),n=ra(e).position.fields.status;if(n)return n}catch(e){console.log("\u{1F680} ~ file: poolModule.ts:1093 ~ PoolModule ~ getPoolStatus ~ error:",e)}}};var ze=re(Gt());import{SuiClient as ca}from"@mysten/sui/client";import{Transaction as ae}from"@mysten/sui/transactions";import{isValidSuiObjectId as la,normalizeSuiAddress as ua}from"@mysten/sui/utils";import{asUintN as Fe,ClmmPoolUtil as ar,CLOCK_ADDRESS as Ci,CoinAssist as Be,d as ie,DETAILS_KEYS as he,extractStructTagFromType as da,getObjectFields as cr,getPackagerConfigs as ce,TickMath as Ne,TickUtil as Mi,createFullClient as fa,deriveDynamicFieldIdByType as _a}from"@cetusprotocol/common-sdk";var ki=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}buildPositionType(){return`${this._sdk.sdkOptions.clmm_pool.package_id}::position::Position`}async getPositionTransactionList({pos_id:t,origin_pos_id:e,full_rpc_url:i,pagination_args:n="all",order:r="ascending"}){let{FullClient:a}=this._sdk,l=[t];e&&l.push(e);let d;i?d=fa(new ca({url:i})):d=a;let f={data:[],has_next_page:!1};try{let h=await d.queryTransactionBlocksByPage({ChangedObject:t},n,r);return h.data.forEach((b,P)=>{let M=Wo(b,P,l);f.data=[...f.data,...M]}),f.has_next_page=h.has_next_page,f.next_cursor=h.next_cursor,f}catch(h){qt("FetchError",h,{[he.METHOD_NAME]:"getPositionTransactionList"})}return f}async getPositionList(t,e=[],i=!0){let n=[],r=await this._sdk.FullClient.getOwnedObjectsByPage(t,{options:{showType:!0,showContent:!0,showDisplay:i,showOwner:!0},filter:{Package:this._sdk.sdkOptions.clmm_pool.package_id}}),a=e.length>0;for(let l of r.data)if(da(l.data.type).full_address===this.buildPositionType()){let f=gi(l),h=`${f.pos_object_id}_getPositionList`;this._sdk.updateCache(h,f),a?e.includes(f.pool)&&n.push(f):n.push(f)}return n}async getPosition(t,e,i=!0,n=!0){let r=await this.getSimplePosition(e,n);return i&&(r=await this.updatePositionInfo(t,r)),r}async getPositionById(t,e=!0,i=!0){let n=await this.getSimplePosition(t,i);if(e){let r=await this._sdk.Pool.getPool(n.pool,!1);return await this.updatePositionInfo(r.position_manager.positions_handle,n)}return n}async getSimplePosition(t,e=!0){let i=`${t}_getPositionList`,n=this.getSimplePositionByCache(t);if(n===void 0){let r=await this.sdk.FullClient.getObject({id:t,options:{showContent:!0,showType:!0,showDisplay:e,showOwner:!0}});n=gi(r),this._sdk.updateCache(i,n)}return n}getSimplePositionByCache(t){let e=`${t}_getPositionList`;return this._sdk.getCache(e)}async getSimplePositionList(t,e=!0){let i=[],n=[];return t.forEach(r=>{let a=this.getSimplePositionByCache(r);a?i.push(a):n.push(r)}),n.length>0&&(await this._sdk.FullClient.batchGetObjects(n,{showOwner:!0,showContent:!0,showDisplay:e,showType:!0})).forEach(a=>{if(a.error==null){let l=gi(a);i.push(l);let d=`${l.pos_object_id}_getPositionList`;this._sdk.updateCache(d,l)}}),i}async updatePositionInfo(t,e){let i=await this.getPositionInfo(t,e.pos_object_id);return{...e,...i}}async getPositionInfo(t,e){try{let i=await this._sdk.FullClient.getDynamicFieldObject({parentId:t,name:{type:"0x2::object::ID",value:e}}),r=cr(i.data).value.fields.value;return We(r)}catch(i){return qt("FetchError",i,{[he.METHOD_NAME]:"getPositionInfo",[he.REQUEST_PARAMS]:{position_handle:t,position_id:e}})}}async getPositionInfoList(t){try{let e=[],i=[];return t.forEach(async r=>{let{position_handle:a,position_ids:l}=r;l.forEach(d=>{let f=_a(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=cr(r.data).value.fields.value,d=We(l);e.push(d)}catch(a){console.log("getPositionInfoList error",a)}}),e)}catch(e){return qt("FetchError",e,{[he.METHOD_NAME]:"getPositionInfoList",[he.REQUEST_PARAMS]:{options:t}})}}buildFetchPosFee(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r=[t.coin_type_a,t.coin_type_b],a=[e.object(ce(i).global_config_id),e.object(t.pool_id),e.pure.address(t.position_id)];e.moveCall({target:`${n.published_at}::${Jt}::fetch_position_fees`,arguments:a,typeArguments:r})}parsedPosFeeData(t){let e={},i=t.events?.filter(n=>n.type.includes("fetcher_script::FetchPositionFeesEvent"));for(let n=0;n<i.length;n+=1){let{parsedJson:r}=i[n],a={position_id:r.position_id,fee_owned_a:r.fee_owned_a,fee_owned_b:r.fee_owned_b};e[r.position_id]=a}return e}async fetchPosFeeAmount(t){let{clmm_pool:e,integrate:i}=this.sdk.sdkOptions,n=new ae;for(let d of t)this.buildFetchPosFee(d,n);let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:ua("0x0")});r.error!=null&&R("InvalidPoolObject",`fetch position fee error code: ${r.error??"unknown error"}, please check config and position and pool object ids`,{[he.METHOD_NAME]:"fetchPosFeeAmount"});let a=[],l=this.parsedPosFeeData(r);for(let d=0;d<t.length;d+=1){let f=l[t[d].position_id];if(f){let h={fee_owned_a:f.fee_owned_a,fee_owned_b:f.fee_owned_b,position_id:t[d].position_id};a.push(h)}}return a}async batchFetchPositionFees(t){let e=[];for(let n of t){let r=await this._sdk.Position.getPositionById(n,!1),a=await this._sdk.Pool.getPool(r.pool,!1);e.push({pool_id:a.id,position_id:r.pos_object_id,coin_type_a:a.coin_type_a,coin_type_b:a.coin_type_b})}let i={};if(e.length>0){let n=await this.fetchPosFeeAmount(e);for(let r of n)i[r.position_id]=r;return i}return i}async createAddLiquidityFixTokenPayload(t,e,i,n,r){let a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:l,is_adjust_coin_b:d}=Se(t);if(t=t,t.fix_amount_a&&l||!t.fix_amount_a&&d)return i=await Ut.buildAddLiquidityFixTokenForGas(this._sdk,a,t,e,i,n,r),i}return Ut.buildAddLiquidityFixToken(this._sdk,a,t,i,n,r)}async createAddLiquidityPayload(t,e,i,n){let{integrate:r,clmm_pool:a}=this._sdk.sdkOptions,l=Fe(BigInt(t.tick_lower)).toString(),d=Fe(BigInt(t.tick_upper)).toString(),f=[t.coin_type_a,t.coin_type_b];e=e||new ae;let h=!la(t.pos_id),b=BigInt(t.max_amount_a),P=BigInt(t.max_amount_b),M,T;if(i==null||n==null){let S=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());M=Be.buildCoinForAmount(e,S,b,t.coin_type_a,!1,!0),T=Be.buildCoinForAmount(e,S,P,t.coin_type_b,!1,!0)}else M={target_coin:i,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},T={target_coin:n,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]};if(h)e.moveCall({target:`${r.published_at}::${Qt}::open_position_with_liquidity`,typeArguments:f,arguments:[e.object(ce(a).global_config_id),e.object(t.pool_id),e.pure.u32(Number(l)),e.pure.u32(Number(d)),M.target_coin,T.target_coin,e.pure.u64(t.max_amount_a),e.pure.u64(t.max_amount_b),e.pure.u128(t.delta_liquidity),e.object(Ci)]});else{let S=await this._sdk.FullClient.getOwnerCoinAssets(this._sdk.getSenderAddress());e=Ut.createCollectRewarderAndFeeParams(this._sdk,e,t,S,M.remain_coins,T.remain_coins),e.moveCall({target:`${r.published_at}::${Qt}::add_liquidity`,typeArguments:f,arguments:[e.object(ce(a).global_config_id),e.object(t.pool_id),e.object(t.pos_id),M.target_coin,T.target_coin,e.pure.u64(t.max_amount_a),e.pure.u64(t.max_amount_b),e.pure.u128(t.delta_liquidity),e.object(Ci)]})}return e}async removeLiquidityPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new ae;let r=[t.coin_type_a,t.coin_type_b],a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());e=Ut.createCollectRewarderAndFeeParams(this._sdk,e,t,a);let l=[e.object(ce(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.pure.u128(t.delta_liquidity),e.object(Ci)],[d,f]=e.moveCall({target:`${i.published_at}::pool::remove_liquidity`,typeArguments:r,arguments:l}),h=Be.fromBalance(d,t.coin_type_a,e),b=Be.fromBalance(f,t.coin_type_b,e);return e.moveCall({target:`${n.published_at}::${me}::check_coin_threshold`,typeArguments:[t.coin_type_a],arguments:[h,e.pure.u64(t.min_amount_a)]}),e.moveCall({target:`${n.published_at}::${me}::check_coin_threshold`,typeArguments:[t.coin_type_b],arguments:[b,e.pure.u64(t.min_amount_b)]}),e.transferObjects([h,b],this.sdk.getSenderAddress()),e}async closePositionPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new ae;let r=[t.coin_type_a,t.coin_type_b],a=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());return e=Ut.createCollectRewarderAndFeeParams(this._sdk,e,t,a),e.moveCall({target:`${n.published_at}::${ni}::close_position`,typeArguments:r,arguments:[e.object(ce(i).global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.pure.u64(t.min_amount_a),e.pure.u64(t.min_amount_b),e.object(Ci)]}),e}openPositionPayload(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions;e=e||new ae;let r=[t.coin_type_a,t.coin_type_b],a=Fe(BigInt(t.tick_lower)).toString(),l=Fe(BigInt(t.tick_upper)).toString(),d=[e.object(ce(i).global_config_id),e.object(t.pool_id),e.pure.u32(Number(a)),e.pure.u32(Number(l))];return e.moveCall({target:`${n.published_at}::${ni}::open_position`,typeArguments:r,arguments:d}),e}async openPositionWithPricePayload(t,e){let{pool_id:i}=t,{clmm_pool:n,integrate:r}=this.sdk.sdkOptions;e=e||new ae;let a=await this.sdk.Pool.getPool(i,!1),l=Number(a.tick_spacing),d=0,f=0;if(t.is_full_range)d=Mi.getMinIndex(l),f=Mi.getMaxIndex(l);else{let{price_base_coin:P,min_price:M,max_price:T}=t;d=Ne.priceToInitializeTickIndex(P==="coin_a"?ie(M):ie(1).div(T),t.coin_decimals_a,t.coin_decimals_b,l),f=Ne.priceToInitializeTickIndex(P==="coin_a"?ie(T):ie(1).div(M),t.coin_decimals_a,t.coin_decimals_b,l)}let h=[a.coin_type_a,a.coin_type_b],b=[e.object(ce(n).global_config_id),e.object(t.pool_id),e.pure.u32(Number(Fe(BigInt(d)))),e.pure.u32(Number(Fe(BigInt(f))))];return e.moveCall({target:`${r.published_at}::${ni}::open_position`,typeArguments:h,arguments:b}),e}async collectFeePayload(t,e,i,n){e=e||new ae;let r=i||Be.buildCoinWithBalance(BigInt(0),t.coin_type_a,e),a=n||Be.buildCoinWithBalance(BigInt(0),t.coin_type_b,e);return this.createCollectFeePayload(t,e,r,a),e}createCollectFeePayload(t,e,i,n){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,l=[t.coin_type_a,t.coin_type_b],d=[e.object(ce(r).global_config_id),e.object(t.pool_id),e.object(t.pos_id),i,n];return e.moveCall({target:`${a.published_at}::${Qt}::collect_fee`,typeArguments:l,arguments:d}),e}async calculateAddLiquidityResultWithPrice(t){let{pool_id:e,slippage:i,refresh_pool_price:n,add_mode_params:r}=t,a=await this.sdk.Pool.getPool(e,n),l=Number(a.tick_spacing),d=0,f=0;if(r.is_full_range)d=Mi.getMinIndex(l),f=Mi.getMaxIndex(l);else{let{price_base_coin:j,min_price:V,max_price:B}=r;d=Ne.priceToInitializeTickIndex(j==="coin_a"?ie(V):ie(1).div(B),r.coin_decimals_a,r.coin_decimals_b,l),f=Ne.priceToInitializeTickIndex(j==="coin_a"?ie(B):ie(1).div(V),r.coin_decimals_a,r.coin_decimals_b,l)}if("liquidity"in t){let{liquidity:j}=t,V=Ne.tickIndexToSqrtPriceX64(d),B=Ne.tickIndexToSqrtPriceX64(f),{coin_amount_a:D,coin_amount_b:jt}=ar.getCoinAmountFromLiquidity(new ze.default(j),new ze.default(a.current_sqrt_price),V,B,!1),z=ie(D).mul(1+i).toFixed(0,I.ROUND_UP),$t=ie(jt).mul(1+i).toFixed(0,I.ROUND_UP);return{coin_amount_a:D,coin_amount_b:jt,coin_amount_limit_a:z,coin_amount_limit_b:$t,liquidity:j,tick_lower:d,tick_upper:f}}let{coin_amount:h,fix_amount_a:b}=t,{coin_amount_limit_a:P,coin_amount_limit_b:M,liquidity_amount:T,coin_amount_a:S,coin_amount_b:L}=ar.estLiquidityAndCoinAmountFromOneAmounts(d,f,new ze.default(h),b,!0,i,new ze.default(a.current_sqrt_price));return{coin_amount_a:S,coin_amount_b:L,coin_amount_limit_a:P,coin_amount_limit_b:M,liquidity:T,tick_lower:d,tick_upper:f,fix_amount_a:b}}async addLiquidityWithPricePayload(t,e,i,n){let{pool_id:r,calculate_result:a}=t,{coin_amount_limit_a:l,coin_amount_limit_b:d,liquidity:f,tick_lower:h,tick_upper:b}=a;e=e||new ae;let P=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityPayload({delta_liquidity:f,max_amount_a:l,max_amount_b:d,tick_lower:h,tick_upper:b,collect_fee:!1,rewarder_coin_types:[],coin_type_a:P.coin_type_a,coin_type_b:P.coin_type_b,pool_id:r,pos_id:""},e,i,n),e}async createAddLiquidityFixCoinWithPricePayload(t,e,i,n){let{pool_id:r,calculate_result:a}=t,{coin_amount_limit_a:l,coin_amount_limit_b:d,liquidity:f,tick_lower:h,tick_upper:b,fix_amount_a:P,coin_amount_a:M,coin_amount_b:T}=a;if(P===void 0)throw R("InvalidParams","fix_amount_a is undefined",{[he.METHOD_NAME]:"addLiquidityFixCoinCoinWithPricePayload",[he.REQUEST_PARAMS]:t});e=e||new ae;let S=await this.sdk.Pool.getPool(r,!1);return await this.createAddLiquidityFixTokenPayload({amount_a:P?M:l,amount_b:P?d:T,slippage:0,fix_amount_a:P,is_open:!0,tick_lower:h,tick_upper:b,collect_fee:!1,rewarder_coin_types:[],coin_type_a:S.coin_type_a,coin_type_b:S.coin_type_b,pool_id:r,pos_id:""},void 0,e,i,n),e}createCollectFeeNoSendPayload(t,e,i,n){let{clmm_pool:r,integrate:a}=this.sdk.sdkOptions,l=[t.coin_type_a,t.coin_type_b],d=[e.object(ce(r).global_config_id),e.object(t.pool_id),e.object(t.pos_id),i,n];return e.moveCall({target:`${a.published_at}::${oi}::collect_fee`,typeArguments:l,arguments:d}),e}};var Mn=re(Gt());import{Transaction as wn}from"@mysten/sui/transactions";import{normalizeSuiAddress as ha}from"@mysten/sui/utils";import{CLOCK_ADDRESS as yn,CoinAssist as vn,DETAILS_KEYS as lr,getPackagerConfigs as Pn,MathUtil as pa,normalizeCoinType as Cn,ZERO as je}from"@cetusprotocol/common-sdk";var Ti=class{constructor(t){this._sdk=t,this.growthGlobal=[je,je,je]}get sdk(){return this._sdk}async emissionsEveryDay(t){let i=(await this.sdk.Pool.getPool(t)).rewarder_infos;if(!i)return null;let n=[];for(let r of i){let a=pa.fromX64(new Mn.default(r.emissions_per_second));n.push({emissions:Math.floor(a.toNumber()*60*60*24),coin_type:r.coin_type})}return n}async batchFetchPositionRewarders(t){let e=[];for(let n of t){let r=await this._sdk.Position.getPositionById(n,!1),a=await this._sdk.Pool.getPool(r.pool,!1);e.push({pool_id:a.id,position_id:r.pos_object_id,coin_type_a:a.coin_type_a,coin_type_b:a.coin_type_b,rewarder_types:a.rewarder_infos.map(l=>l.coin_type)})}let i={};if(e.length>0){let n=await this.fetchPosRewardersAmount(e);for(let r of n)i[r.position_id]=r.rewarder_amounts;return i}return i}async fetchPositionRewarders(t,e){let i={pool_id:t.id,position_id:e,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b,rewarder_types:t.rewarder_infos.map(r=>r.coin_type)};return(await this.fetchPosRewardersAmount([i]))[0].rewarder_amounts}async batchFetchPositionFees(t){return await this._sdk.Position.batchFetchPositionFees(t)}async fetchPosFeeAmount(t){return await this._sdk.Position.fetchPosFeeAmount(t)}buildFetchPosReward(t,e){let{clmm_pool:i,integrate:n}=this.sdk.sdkOptions,r=[t.coin_type_a,t.coin_type_b],a=[e.object(Pn(i).global_config_id),e.object(t.pool_id),e.pure.address(t.position_id),e.object(yn)];e.moveCall({target:`${n.published_at}::${Jt}::fetch_position_rewards`,arguments:a,typeArguments:r})}async fetchPosRewardersAmount(t){let e=new wn;for(let a of t)this.buildFetchPosReward(a,e);let i=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:e,sender:ha("0x")});i.error!=null&&R("InvalidConfig",`fetch position rewards error code: ${i.error??"unknown error"}, please check config and params`,{[lr.METHOD_NAME]:"fetchPosRewardersAmount",[lr.REQUEST_PARAMS]:{params:t}});let n=this.parsedPosRewardData(i),r=[];for(let a=0;a<t.length;a+=1){let l=n[t[a].position_id];if(l){let d={pool_id:t[a].pool_id,position_id:t[a].position_id,rewarder_amounts:l.rewarder_amount.map((f,h)=>({amount_owned:f,coin_type:t[a].rewarder_types[h]}))};r.push(d)}}return r}parsedPosRewardData(t){let e={},i=t.events?.filter(n=>n.type.includes("fetcher_script::FetchPositionRewardsEvent"));for(let n=0;n<i.length;n+=1){let{parsedJson:r}=i[n],a={position_id:r.position_id,rewarder_amount:r.data};e[r.position_id]=a}return e}async fetchPoolRewardersAmount(t,e){let i=await this.sdk.Pool.getPool(e),n=await this.sdk.Position.getPositionList(t,[e]),r=[];for(let d of n)r.push({pool_id:i.id,position_id:d.pos_object_id,rewarder_types:i.rewarder_infos.map(f=>f.coin_type),coin_type_a:i.coin_type_a,coin_type_b:i.coin_type_b});let a=await this.fetchPosRewardersAmount(r),l=[je,je,je];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 Mn.default(d.rewarder_amounts[f].amount_owned));return l}async getPoolLowerAndUpperTicks(t,e){let i=[],n=[];for(let r of e){let a=await this.sdk.Pool.getTickDataByIndex(t,r.tick_lower_index),l=await this.sdk.Pool.getTickDataByIndex(t,r.tick_upper_index);i.push(a),n.push(l)}return[i,n]}async collectRewarderPayload(t){let e=await this.sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress(),null),i=new wn;return i=Ut.createCollectRewarderAndFeeParams(this._sdk,i,t,e),i}async batchCollectRewardsPayload(t,e,i,n){let r=await this.sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress(),null);e=e||new wn;let a={};return t.forEach(l=>{let d=Cn(l.coin_type_a),f=Cn(l.coin_type_b);if(l.collect_fee){let b=a[d];b==null&&(i==null?b=vn.buildCoinForAmount(e,r,BigInt(0),d,!1):b={target_coin:i,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},a[d]=b);let P=a[f];P==null&&(n==null?P=vn.buildCoinForAmount(e,r,BigInt(0),f,!1):P={target_coin:n,remain_coins:[],is_mint_zero_coin:!1,target_coin_amount:"0",selected_coins:[]},a[f]=P),e=this._sdk.Position.createCollectFeeNoSendPayload({pool_id:l.pool_id,pos_id:l.pos_id,coin_type_a:l.coin_type_a,coin_type_b:l.coin_type_b},e,b.target_coin,P.target_coin)}let h=[];l.rewarder_coin_types.forEach(b=>{let P=Cn(b),M=a[b];M===void 0&&(M=vn.buildCoinForAmount(e,r,BigInt(0),P,!1),a[P]=M),h.push(M.target_coin)}),e=this.createCollectRewarderNoSendPayload(l,e,h)}),Object.keys(a).forEach(l=>{let d=a[l];d.is_mint_zero_coin&&zo(this.sdk,e,d.target_coin,l,this.sdk.getSenderAddress())}),e}createCollectRewarderPayload(t,e,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=Pn(n),l=[t.coin_type_a,t.coin_type_b];return t.rewarder_coin_types.forEach((d,f)=>{e&&e.moveCall({target:`${r.published_at}::${Qt}::collect_reward`,typeArguments:[...l,d],arguments:[e.object(a.global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.object(a.global_vault_id),i[f],e.object(yn)]})}),e}createCollectRewarderNoSendPayload(t,e,i){let{clmm_pool:n,integrate:r}=this.sdk.sdkOptions,a=Pn(n),l=[t.coin_type_a,t.coin_type_b];return t.rewarder_coin_types.forEach((d,f)=>{e&&e.moveCall({target:`${r.published_at}::${oi}::collect_reward`,typeArguments:[...l,d],arguments:[e.object(a.global_config_id),e.object(t.pool_id),e.object(t.pos_id),e.object(a.global_vault_id),i[f],e.object(yn)]})}),e}};var we=re(Gt());import{Transaction as ur}from"@mysten/sui/transactions";import{normalizeSuiAddress as dr}from"@mysten/sui/utils";import{d as qe,DETAILS_KEYS as pe,extractStructTagFromType as fr,TickMath as _r,U64_MAX as ma,ZERO as ga}from"@cetusprotocol/common-sdk";var Kl="amm_swap",Hl="Pool",Ai=class{constructor(t){this.calculateSingleImpact=(t,e)=>e.minus(t).div(e).mul(100);this._sdk=t}get sdk(){return this._sdk}calculateSwapFee(t){let e=qe(0);return t.forEach(i=>{let n=i.base_paths.length;if(n>0){let r=i.base_paths[0],a=r.label==="Cetus"?new I(r.fee_rate).div(10**6):new I(r.fee_rate).div(10**9),l=qe(r.input_amount).div(10**r.from_decimal).mul(a);if(e=e.add(l),n>1){let d=i.base_paths[1],f=r.direction?r.current_price:new I(1).div(r.current_price),h=d.direction?d.current_price:new I(1).div(d.current_price),b=d.label==="Cetus"?new I(d.fee_rate).div(10**6):new I(d.fee_rate).div(10**9),M=qe(d.output_amount).div(10**d.to_decimal).mul(b).div(f.mul(h));e=e.add(M)}}}),e.toString()}calculateSwapPriceImpact(t){let e=qe(0);return t.forEach(i=>{let n=i.base_paths.length;if(n===1){let r=i.base_paths[0],a=qe(r.output_amount).div(10**r.to_decimal),l=qe(r.input_amount).div(10**r.from_decimal),d=a.div(l),f=r.direction?new I(r.current_price):new I(1).div(r.current_price);e=e.add(this.calculateSingleImpact(d,f))}if(n===2){let r=i.base_paths[0],a=i.base_paths[1],l=r.direction?new I(r.current_price):new I(1).div(r.current_price),d=a.direction?new I(a.current_price):new I(1).div(a.current_price),f=l.mul(d),h=new I(a.output_amount).div(10**a.to_decimal),b=new I(r.input_amount).div(10**r.from_decimal),P=h.div(b);e=e.add(this.calculateSingleImpact(P,f))}}),e.toString()}async preSwapWithMultiPool(t){let{integrate:e}=this.sdk.sdkOptions,i=new ur,n=[t.coin_type_a,t.coin_type_b];for(let f=0;f<t.pool_ids.length;f+=1){let h=[i.object(t.pool_ids[f]),i.pure.bool(t.a2b),i.pure.bool(t.by_amount_in),i.pure.u64(t.amount)];i.moveCall({target:`${e.published_at}::${Jt}::calculate_swap_result`,arguments:h,typeArguments:n})}let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:dr("0x0")});r.error!=null&&R("InvalidConfig",`pre swap with multi pools error code: ${r.error??"unknown error"}, please check config and params`,{[pe.METHOD_NAME]:"preSwapWithMultiPool",[pe.REQUEST_PARAMS]:{params:t}});let a=r.events?.filter(f=>fr(f.type).name==="CalculatedSwapResultEvent");if(a.length===0)return null;a.length!==t.pool_ids.length&&R("ParamsLengthNotEqual","valueData.length !== params.pools.length",{[pe.METHOD_NAME]:"preSwapWithMultiPool",[pe.REQUEST_PARAMS]:{params:t}});let l=t.by_amount_in?ga:ma,d=0;for(let f=0;f<a.length;f+=1)if(!a[f].parsedJson.data.is_exceed)if(t.by_amount_in){let h=new we.default(a[f].parsedJson.data.amount_out);h.gt(l)&&(d=f,l=h)}else{let h=new we.default(a[f].parsedJson.data.amount_out);h.lt(l)&&(d=f,l=h)}return this.transformSwapWithMultiPoolData({pool_address:t.pool_ids[d],a2b:t.a2b,by_amount_in:t.by_amount_in,amount:t.amount,coin_type_a:t.coin_type_a,coin_type_b:t.coin_type_b},a[d].parsedJson)}async preSwap(t){let{integrate:e}=this.sdk.sdkOptions,i=new ur,n=[t.coin_type_a,t.coin_type_b],r=[i.object(t.pool.id),i.pure.bool(t.a2b),i.pure.bool(t.by_amount_in),i.pure.u64(t.amount)];i.moveCall({target:`${e.published_at}::${Jt}::calculate_swap_result`,arguments:r,typeArguments:n});let a=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:i,sender:dr("0x0")});if(a.error!=null)return R("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[pe.METHOD_NAME]:"preSwap",[pe.REQUEST_PARAMS]:{params:t}});let l=a.events?.filter(d=>fr(d.type).name==="CalculatedSwapResultEvent");return l.length===0?R("InvalidConfig",`preSwap error code: ${a.error??"unknown error"}, please check config and params`,{[pe.METHOD_NAME]:"preSwap",[pe.REQUEST_PARAMS]:{params:t}}):this.transformSwapData(t,l[0].parsedJson.data)}transformSwapData(t,e){let i=e.amount_in&&e.fee_amount?new we.default(e.amount_in).add(new we.default(e.fee_amount)).toString():"";return{pool_address:t.pool.id,current_sqrt_price:t.current_sqrt_price,estimated_amount_in:i,estimated_amount_out:e.amount_out,estimated_end_sqrt_price:e.after_sqrt_price,estimated_fee_amount:e.fee_amount,is_exceed:e.is_exceed,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in}}transformSwapWithMultiPoolData(t,e){let{data:i}=e;console.log("json data. ",i);let n=i.amount_in&&i.fee_amount?new we.default(i.amount_in).add(new we.default(i.fee_amount)).toString():"";return{pool_address:t.pool_address,estimated_amount_in:n,estimated_amount_out:i.amount_out,estimated_end_sqrt_price:i.after_sqrt_price,estimated_start_sqrt_price:i.step_results[0].current_sqrt_price,estimated_fee_amount:i.fee_amount,is_exceed:i.is_exceed,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in}}calculateRates(t){let{current_pool:e}=t,i=Uo(e),n;t.a2b?n=t.swap_ticks.sort((P,M)=>M.index-P.index):n=t.swap_ticks.sort((P,M)=>P.index-M.index);let r=Vo(t.a2b,t.by_amount_in,t.amount,i,n),a=!1;t.by_amount_in?a=r.amount_in.lt(t.amount):a=r.amount_out.lt(t.amount);let l=te.getDefaultSqrtPriceLimit(t.a2b);t.a2b&&r.next_sqrt_price.lt(l)&&(a=!0),!t.a2b&&r.next_sqrt_price.gt(l)&&(a=!0);let d=0;r.cross_tick_num>6&&r.cross_tick_num<40&&(d=22e3*(r.cross_tick_num-6)),r.cross_tick_num>40&&(a=!0);let f=_r.sqrtPriceX64ToPrice(i.current_sqrt_price,t.decimals_a,t.decimals_b).toNumber(),h=_r.sqrtPriceX64ToPrice(r.next_sqrt_price,t.decimals_a,t.decimals_b).toNumber(),b=Math.abs(f-h)/f*100;return{estimated_amount_in:r.amount_in,estimated_amount_out:r.amount_out,estimated_end_sqrt_price:r.next_sqrt_price,estimated_fee_amount:r.fee_amount,is_exceed:a,extra_compute_limit:d,amount:t.amount,a2b:t.a2b,by_amount_in:t.by_amount_in,price_impact_pct:b}}async createSwapPayload(t,e){let i=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:n,is_adjust_coin_b:r}=Se(t);if(t.a2b&&n||!t.a2b&&r)return await te.buildSwapTransactionForGas(this._sdk,t,i,e)}return te.buildSwapTransaction(this.sdk,t,i)}async createSwapWithoutTransferCoinsPayload(t,e){let i=await this._sdk.FullClient.getOwnerCoinAssets(this.sdk.getSenderAddress());if(e){let{is_adjust_coin_a:n,is_adjust_coin_b:r}=Se(t);if(t.a2b&&n||!t.a2b&&r)return await te.buildSwapTransactionWithoutTransferCoinsForGas(this._sdk,t,i,e)}return te.buildSwapTransactionWithoutTransferCoins(this.sdk,t,i)}};import{CLOCK_ADDRESS as ba,CoinAssist as wa,DETAILS_KEYS as ye,getPackagerConfigs as kn}from"@cetusprotocol/common-sdk";import{Transaction as hr}from"@mysten/sui/transactions";import{normalizeSuiAddress as ya}from"@mysten/sui/utils";var Si=class{constructor(t){this._sdk=t}get sdk(){return this._sdk}async getClmmVestInfoList(){let{clmm_vest:t}=this._sdk.sdkOptions;if(t===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[ye.METHOD_NAME]:"getClmmVestInfo"});let e=[];try{let i=`${t.package_id}::clmm_vester::CreateEvent`,r=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:i})).data.map(a=>a.parsedJson.clmm_vester_id);return r.length>0&&(await this._sdk.FullClient.batchGetObjects(r,{showContent:!0,showType:!0})).forEach(l=>{let d=ee.parseClmmVestInfo(l),f=`${d.id}-ClmmVestInfo`;this._sdk.updateCache(f,d),e.push(d)}),e}catch(i){qt("ClmmVestFetchError",i,{[ye.METHOD_NAME]:"getClmmVestInfoList"})}return[]}async getClmmVestInfo(t=!0){let{clmm_vest:e}=this._sdk.sdkOptions;if(e===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[ye.METHOD_NAME]:"getClmmVestInfo"});let{clmm_vest_id:i}=kn(e),n=`${i}-ClmmVestInfo`,r=this._sdk.getCache(n,t);if(r)return r;try{let a=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0,showType:!0}}),l=ee.parseClmmVestInfo(a);return this._sdk.updateCache(n,l),l}catch(a){return qt("ClmmVestFetchError",a,{[ye.METHOD_NAME]:"getClmmVestInfo",[ye.REQUEST_PARAMS]:{clmm_vest_id:i}})}}async getPositionVesting(t){let{clmm_vest:e}=this._sdk.sdkOptions;if(e===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[ye.METHOD_NAME]:"getPositionVesting"});if(t.length===0)return[];let{clmm_vest_id:i}=kn(e),n=new hr;t.forEach(l=>{let{clmm_pool_id:d,coin_type_a:f,coin_type_b:h,clmm_position_ids:b}=l;n.moveCall({package:e.published_at,module:"clmm_vester",function:"get_positions_vesting",typeArguments:[f,h],arguments:[n.object(i),n.object(d),n.pure.vector("id",b)]})});let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:n,sender:ya("0x0")}),a=[];return r.events?.forEach(l=>{if(l.type.includes("clmm_vester::GetPositionsVestingEvent")){let{parsedJson:d}=l;a.push(...d.position_vestings.map(f=>ee.parsePositionVesting(f)))}}),a}buildRedeemPayload(t,e){let{clmm_vest:i}=this._sdk.sdkOptions;if(i===void 0)return R("ClmmVestNotSet","clmm_vest is not set config in sdk options",{[ye.METHOD_NAME]:"buildRedeemVestPayload"});let{versioned_id:n,clmm_vest_id:r,cetus_coin_type:a}=kn(i);return e=e||new hr,t.forEach(l=>{let{clmm_pool_id:d,clmm_position_id:f,coin_type_a:h,coin_type_b:b,period:P}=l,M=e.moveCall({package:i.published_at,module:"clmm_vester",function:"redeem",arguments:[e.object(n),e.object(r),e.object(d),typeof f=="string"?e.object(f):f,e.pure.u16(P),e.object(ba)],typeArguments:[h,b]}),T=wa.fromBalance(M,a,e);e.transferObjects([T],this._sdk.getSenderAddress())}),e}};var xi=class o extends va{constructor(t){super(t),this._swap=new Ai(this),this._pool=new Pi(this),this._position=new ki(this),this._config=new ii(this),this._rewarder=new Ti(this),this._vest=new Si(this)}get Pool(){return this._pool}get Position(){return this._position}get CetusConfig(){return this._config}get Rewarder(){return this._rewarder}get Swap(){return this._swap}get Vest(){return this._vest}static createSDK(t){let{env:e="mainnet"}=t;return e==="mainnet"?o.createCustomSDK({...Yo,...t}):o.createCustomSDK({...er,...t})}static createCustomSDK(t){return new o(t)}};var Ge=re(Gt());import{MathUtil as De,TickMath as pr}from"@cetusprotocol/common-sdk";var Pa=new Ge.default(365),Ca=new Ge.default(24),Ma=new Ge.default(3600),ka=new Ge.default(.5);function wu(o,t,e,i){return Pa.mul(Ca).mul(Ma).mul(ka).mul(o.mul(t).add(e).div(i))}function Ta(o,t,e,i,n,r){let a=new I(o.toString()).div(new I(10**e)),l=new I(t.toString()).div(new I(10**i));return a.mul(n).add(l.mul(r))}function yu(o,t,e,i,n,r,a,l,d,f,h,b,P,M,T,S,L,j,V,B,D,jt,z,$t){let ne=new I(b),Zt=new I(P),oe=new I(S),$=new I(L),Bt=new I(j),y=new I(V),s=new I(B),u=new I(D),_=new I(jt),p=new I(z),g=new I($t),v=pr.tickIndexToSqrtPriceX64(t),C=pr.tickIndexToSqrtPriceX64(e),m=De.toX64Decimal(De.fromX64(v)).round(),c=De.toX64Decimal(De.fromX64(C)).round(),w=De.toX64Decimal(De.fromX64(i)).round(),A,x=ne.mul(new I(10**r)).mul(c.mul(m)).div(c.sub(m)).round(),E=Zt.mul(new I(10**a)).div(c.sub(m)).round();o<t?A=x:o>e?A=E:A=I.min(x,E);let q=A.mul(w.sub(m)),K=A.mul(c.sub(w)).div(w.mul(c)).div(new I(10**r)).mul(s).add(q.div(new I(10**a).mul(u))),zt=Ta(M,T,r,a,s,u),H=K.div(zt),G=A.eq(new I(0))?new I(0):new I(h/1e4).mul(oe).mul(new I(A.toString()).div(new I(n.toString()).add(new I(A.toString())))).div(K),ve=H.eq(new I(0))?new I(0):H.mul(new I(36500/7)).div(K),Q=$.div(new I(10**l)).mul(_).mul(ve),J=Bt.div(new I(10**d)).mul(p).mul(ve),Qe=y.div(new I(10**f)).mul(g).mul(ve);return{fee_apr:G,pos_rewarder_0_apr:Q,pos_rewarder_1_apr:J,pos_rewarder_2_apr:Qe}}function vu(o,t,e,i){let n=Math.max(o,e),a=Math.min(t,i)-n,l=t-o,d=i-e,f=new I(l.toString()),h=new I(d.toString()),b=new I(a.toString()),P=new I("0");return a<0?P=new I("0"):l===a?P=h.div(b):d===a?P=b.div(f):P=b.mul(b).div(h).div(f),P}var Au=xi;export{Kl as AMM_SWAP_MODULE,Ys as BPS,xi as CetusClmmSDK,Me as ClmmError,Da as ClmmExpectSwapModule,Jt as ClmmFetcherModule,ni as ClmmIntegratePoolModule,Qt as ClmmIntegratePoolV2Module,oi as ClmmIntegratePoolV3Module,me as ClmmIntegrateRouterModule,bo as ClmmIntegrateRouterWithPartnerModule,wo as ClmmIntegrateUtilsModule,go as ClmmPartnerModule,po as ClmmPositionStatus,ke as ConfigErrorCode,ii as ConfigModule,$a as DeepbookClobV2Module,La as DeepbookCustodianV2Module,Ua as DeepbookEndpointsV2Module,Hl as POOL_STRUCT,co as PartnerErrorCode,$e as PoolErrorCode,Pi as PoolModule,ti as PositionErrorCode,ki as PositionModule,Ut as PositionUtils,Ti as RewarderModule,Br as RouterErrorCode,so as SwapErrorCode,Ai as SwapModule,te as SwapUtils,lo as TypesErrorCode,Yi as UtilsErrorCode,ao as VestErrorCode,Si as VestModule,ee as VestUtils,Sc as buildClmmPositionName,mi as buildPool,Zo as buildPoolTransactionInfo,gi as buildPosition,We as buildPositionInfo,Wo as buildPositionTransactionInfo,bi as buildTickData,Ho as buildTickDataByEvent,zo as buildTransferCoin,wi as buildTransferCoinToSender,Yo as clmmMainnet,er as clmmTestnet,Vo as computeSwap,Ks as computeSwapStep,Au as default,wu as estPoolAPR,yu as estPositionAPRWithDeltaMethod,vu as estPositionAPRWithMultiMethod,tr as eventMainnetContractMaps,ir as eventTestnetContractMaps,Se as findAdjustCoin,Va as getDefaultSuiInputType,lc as getLowerSqrtPriceFromCoinA,dc as getLowerSqrtPriceFromCoinB,uc as getUpperSqrtPriceFromCoinA,fc as getUpperSqrtPriceFromCoinB,qt as handleError,R as handleMessageError,gc as newBits,mo as poolFilterEvenTypes,ho as poolLiquiditySnapshotType,Uo as transClmmpoolDataWithoutTicks};
|
|
2
2
|
/*! Bundled license information:
|
|
3
3
|
|
|
4
4
|
decimal.js/decimal.mjs:
|