@cetusprotocol/dlmm-sdk 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- var Yn=Object.create;var an=Object.defineProperty;var Jn=Object.getOwnPropertyDescriptor;var tr=Object.getOwnPropertyNames;var er=Object.getPrototypeOf,ir=Object.prototype.hasOwnProperty;var nr=(r=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(r,{get:(i,t)=>(typeof require<"u"?require:i)[t]}):r)(function(r){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});var rr=(r,i)=>()=>(i||r((i={exports:{}}).exports,i),i.exports);var or=(r,i,t,n)=>{if(i&&typeof i=="object"||typeof i=="function")for(let e of tr(i))!ir.call(r,e)&&e!==t&&an(r,e,{get:()=>i[e],enumerable:!(n=Jn(i,e))||n.enumerable});return r};var Ai=(r,i,t)=>(t=r!=null?Yn(er(r)):{},or(i||!r||!r.__esModule?an(t,"default",{value:r,enumerable:!0}):t,r));var De=rr((un,Bi)=>{"use strict";(function(r,i){"use strict";function t(v,o){if(!v)throw new Error(o||"Assertion failed")}function n(v,o){v.super_=o;var u=function(){};u.prototype=o.prototype,v.prototype=new u,v.prototype.constructor=v}function e(v,o,u){if(e.isBN(v))return v;this.negative=0,this.words=null,this.length=0,this.red=null,v!==null&&((o==="le"||o==="be")&&(u=o,o=10),this._init(v||0,o||10,u||"be"))}typeof r=="object"?r.exports=e:i.BN=e,e.BN=e,e.wordSize=26;var s;try{typeof window<"u"&&typeof window.Buffer<"u"?s=window.Buffer:s=nr("buffer").Buffer}catch{}e.isBN=function(o){return o instanceof e?!0:o!==null&&typeof o=="object"&&o.constructor.wordSize===e.wordSize&&Array.isArray(o.words)},e.max=function(o,u){return o.cmp(u)>0?o:u},e.min=function(o,u){return o.cmp(u)<0?o:u},e.prototype._init=function(o,u,h){if(typeof o=="number")return this._initNumber(o,u,h);if(typeof o=="object")return this._initArray(o,u,h);u==="hex"&&(u=16),t(u===(u|0)&&u>=2&&u<=36),o=o.toString().replace(/\s+/g,"");var m=0;o[0]==="-"&&(m++,this.negative=1),m<o.length&&(u===16?this._parseHex(o,m,h):(this._parseBase(o,u,m),h==="le"&&this._initArray(this.toArray(),u,h)))},e.prototype._initNumber=function(o,u,h){o<0&&(this.negative=1,o=-o),o<67108864?(this.words=[o&67108863],this.length=1):o<4503599627370496?(this.words=[o&67108863,o/67108864&67108863],this.length=2):(t(o<9007199254740992),this.words=[o&67108863,o/67108864&67108863,1],this.length=3),h==="le"&&this._initArray(this.toArray(),u,h)},e.prototype._initArray=function(o,u,h){if(t(typeof o.length=="number"),o.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(o.length/3),this.words=new Array(this.length);for(var m=0;m<this.length;m++)this.words[m]=0;var b,M,P=0;if(h==="be")for(m=o.length-1,b=0;m>=0;m-=3)M=o[m]|o[m-1]<<8|o[m-2]<<16,this.words[b]|=M<<P&67108863,this.words[b+1]=M>>>26-P&67108863,P+=24,P>=26&&(P-=26,b++);else if(h==="le")for(m=0,b=0;m<o.length;m+=3)M=o[m]|o[m+1]<<8|o[m+2]<<16,this.words[b]|=M<<P&67108863,this.words[b+1]=M>>>26-P&67108863,P+=24,P>=26&&(P-=26,b++);return this._strip()};function l(v,o){var u=v.charCodeAt(o);if(u>=48&&u<=57)return u-48;if(u>=65&&u<=70)return u-55;if(u>=97&&u<=102)return u-87;t(!1,"Invalid character in "+v)}function d(v,o,u){var h=l(v,u);return u-1>=o&&(h|=l(v,u-1)<<4),h}e.prototype._parseHex=function(o,u,h){this.length=Math.ceil((o.length-u)/6),this.words=new Array(this.length);for(var m=0;m<this.length;m++)this.words[m]=0;var b=0,M=0,P;if(h==="be")for(m=o.length-1;m>=u;m-=2)P=d(o,u,m)<<b,this.words[M]|=P&67108863,b>=18?(b-=18,M+=1,this.words[M]|=P>>>26):b+=8;else{var g=o.length-u;for(m=g%2===0?u+1:u;m<o.length;m+=2)P=d(o,u,m)<<b,this.words[M]|=P&67108863,b>=18?(b-=18,M+=1,this.words[M]|=P>>>26):b+=8}this._strip()};function c(v,o,u,h){for(var m=0,b=0,M=Math.min(v.length,u),P=o;P<M;P++){var g=v.charCodeAt(P)-48;m*=h,g>=49?b=g-49+10:g>=17?b=g-17+10:b=g,t(g>=0&&b<h,"Invalid character"),m+=b}return m}e.prototype._parseBase=function(o,u,h){this.words=[0],this.length=1;for(var m=0,b=1;b<=67108863;b*=u)m++;m--,b=b/u|0;for(var M=o.length-h,P=M%m,g=Math.min(M,M-P)+h,a=0,w=h;w<g;w+=m)a=c(o,w,w+m,u),this.imuln(b),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a);if(P!==0){var C=1;for(a=c(o,w,o.length,u),w=0;w<P;w++)C*=u;this.imuln(C),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a)}this._strip()},e.prototype.copy=function(o){o.words=new Array(this.length);for(var u=0;u<this.length;u++)o.words[u]=this.words[u];o.length=this.length,o.negative=this.negative,o.red=this.red};function f(v,o){v.words=o.words,v.length=o.length,v.negative=o.negative,v.red=o.red}if(e.prototype._move=function(o){f(o,this)},e.prototype.clone=function(){var o=new e(null);return this.copy(o),o},e.prototype._expand=function(o){for(;this.length<o;)this.words[this.length++]=0;return this},e.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},e.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{e.prototype[Symbol.for("nodejs.util.inspect.custom")]=p}catch{e.prototype.inspect=p}else e.prototype.inspect=p;function p(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var _=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],y=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],A=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];e.prototype.toString=function(o,u){o=o||10,u=u|0||1;var h;if(o===16||o==="hex"){h="";for(var m=0,b=0,M=0;M<this.length;M++){var P=this.words[M],g=((P<<m|b)&16777215).toString(16);b=P>>>24-m&16777215,m+=2,m>=26&&(m-=26,M--),b!==0||M!==this.length-1?h=_[6-g.length]+g+h:h=g+h}for(b!==0&&(h=b.toString(16)+h);h.length%u!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}if(o===(o|0)&&o>=2&&o<=36){var a=y[o],w=A[o];h="";var C=this.clone();for(C.negative=0;!C.isZero();){var D=C.modrn(w).toString(o);C=C.idivn(w),C.isZero()?h=D+h:h=_[a-D.length]+D+h}for(this.isZero()&&(h="0"+h);h.length%u!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}t(!1,"Base should be between 2 and 36")},e.prototype.toNumber=function(){var o=this.words[0];return this.length===2?o+=this.words[1]*67108864:this.length===3&&this.words[2]===1?o+=4503599627370496+this.words[1]*67108864:this.length>2&&t(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-o:o},e.prototype.toJSON=function(){return this.toString(16,2)},s&&(e.prototype.toBuffer=function(o,u){return this.toArrayLike(s,o,u)}),e.prototype.toArray=function(o,u){return this.toArrayLike(Array,o,u)};var B=function(o,u){return o.allocUnsafe?o.allocUnsafe(u):new o(u)};e.prototype.toArrayLike=function(o,u,h){this._strip();var m=this.byteLength(),b=h||Math.max(1,m);t(m<=b,"byte array longer than desired length"),t(b>0,"Requested array length <= 0");var M=B(o,b),P=u==="le"?"LE":"BE";return this["_toArrayLike"+P](M,m),M},e.prototype._toArrayLikeLE=function(o,u){for(var h=0,m=0,b=0,M=0;b<this.length;b++){var P=this.words[b]<<M|m;o[h++]=P&255,h<o.length&&(o[h++]=P>>8&255),h<o.length&&(o[h++]=P>>16&255),M===6?(h<o.length&&(o[h++]=P>>24&255),m=0,M=0):(m=P>>>24,M+=2)}if(h<o.length)for(o[h++]=m;h<o.length;)o[h++]=0},e.prototype._toArrayLikeBE=function(o,u){for(var h=o.length-1,m=0,b=0,M=0;b<this.length;b++){var P=this.words[b]<<M|m;o[h--]=P&255,h>=0&&(o[h--]=P>>8&255),h>=0&&(o[h--]=P>>16&255),M===6?(h>=0&&(o[h--]=P>>24&255),m=0,M=0):(m=P>>>24,M+=2)}if(h>=0)for(o[h--]=m;h>=0;)o[h--]=0},Math.clz32?e.prototype._countBits=function(o){return 32-Math.clz32(o)}:e.prototype._countBits=function(o){var u=o,h=0;return u>=4096&&(h+=13,u>>>=13),u>=64&&(h+=7,u>>>=7),u>=8&&(h+=4,u>>>=4),u>=2&&(h+=2,u>>>=2),h+u},e.prototype._zeroBits=function(o){if(o===0)return 26;var u=o,h=0;return(u&8191)===0&&(h+=13,u>>>=13),(u&127)===0&&(h+=7,u>>>=7),(u&15)===0&&(h+=4,u>>>=4),(u&3)===0&&(h+=2,u>>>=2),(u&1)===0&&h++,h},e.prototype.bitLength=function(){var o=this.words[this.length-1],u=this._countBits(o);return(this.length-1)*26+u};function S(v){for(var o=new Array(v.bitLength()),u=0;u<o.length;u++){var h=u/26|0,m=u%26;o[u]=v.words[h]>>>m&1}return o}e.prototype.zeroBits=function(){if(this.isZero())return 0;for(var o=0,u=0;u<this.length;u++){var h=this._zeroBits(this.words[u]);if(o+=h,h!==26)break}return o},e.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},e.prototype.toTwos=function(o){return this.negative!==0?this.abs().inotn(o).iaddn(1):this.clone()},e.prototype.fromTwos=function(o){return this.testn(o-1)?this.notn(o).iaddn(1).ineg():this.clone()},e.prototype.isNeg=function(){return this.negative!==0},e.prototype.neg=function(){return this.clone().ineg()},e.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},e.prototype.iuor=function(o){for(;this.length<o.length;)this.words[this.length++]=0;for(var u=0;u<o.length;u++)this.words[u]=this.words[u]|o.words[u];return this._strip()},e.prototype.ior=function(o){return t((this.negative|o.negative)===0),this.iuor(o)},e.prototype.or=function(o){return this.length>o.length?this.clone().ior(o):o.clone().ior(this)},e.prototype.uor=function(o){return this.length>o.length?this.clone().iuor(o):o.clone().iuor(this)},e.prototype.iuand=function(o){var u;this.length>o.length?u=o:u=this;for(var h=0;h<u.length;h++)this.words[h]=this.words[h]&o.words[h];return this.length=u.length,this._strip()},e.prototype.iand=function(o){return t((this.negative|o.negative)===0),this.iuand(o)},e.prototype.and=function(o){return this.length>o.length?this.clone().iand(o):o.clone().iand(this)},e.prototype.uand=function(o){return this.length>o.length?this.clone().iuand(o):o.clone().iuand(this)},e.prototype.iuxor=function(o){var u,h;this.length>o.length?(u=this,h=o):(u=o,h=this);for(var m=0;m<h.length;m++)this.words[m]=u.words[m]^h.words[m];if(this!==u)for(;m<u.length;m++)this.words[m]=u.words[m];return this.length=u.length,this._strip()},e.prototype.ixor=function(o){return t((this.negative|o.negative)===0),this.iuxor(o)},e.prototype.xor=function(o){return this.length>o.length?this.clone().ixor(o):o.clone().ixor(this)},e.prototype.uxor=function(o){return this.length>o.length?this.clone().iuxor(o):o.clone().iuxor(this)},e.prototype.inotn=function(o){t(typeof o=="number"&&o>=0);var u=Math.ceil(o/26)|0,h=o%26;this._expand(u),h>0&&u--;for(var m=0;m<u;m++)this.words[m]=~this.words[m]&67108863;return h>0&&(this.words[m]=~this.words[m]&67108863>>26-h),this._strip()},e.prototype.notn=function(o){return this.clone().inotn(o)},e.prototype.setn=function(o,u){t(typeof o=="number"&&o>=0);var h=o/26|0,m=o%26;return this._expand(h+1),u?this.words[h]=this.words[h]|1<<m:this.words[h]=this.words[h]&~(1<<m),this._strip()},e.prototype.iadd=function(o){var u;if(this.negative!==0&&o.negative===0)return this.negative=0,u=this.isub(o),this.negative^=1,this._normSign();if(this.negative===0&&o.negative!==0)return o.negative=0,u=this.isub(o),o.negative=1,u._normSign();var h,m;this.length>o.length?(h=this,m=o):(h=o,m=this);for(var b=0,M=0;M<m.length;M++)u=(h.words[M]|0)+(m.words[M]|0)+b,this.words[M]=u&67108863,b=u>>>26;for(;b!==0&&M<h.length;M++)u=(h.words[M]|0)+b,this.words[M]=u&67108863,b=u>>>26;if(this.length=h.length,b!==0)this.words[this.length]=b,this.length++;else if(h!==this)for(;M<h.length;M++)this.words[M]=h.words[M];return this},e.prototype.add=function(o){var u;return o.negative!==0&&this.negative===0?(o.negative=0,u=this.sub(o),o.negative^=1,u):o.negative===0&&this.negative!==0?(this.negative=0,u=o.sub(this),this.negative=1,u):this.length>o.length?this.clone().iadd(o):o.clone().iadd(this)},e.prototype.isub=function(o){if(o.negative!==0){o.negative=0;var u=this.iadd(o);return o.negative=1,u._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(o),this.negative=1,this._normSign();var h=this.cmp(o);if(h===0)return this.negative=0,this.length=1,this.words[0]=0,this;var m,b;h>0?(m=this,b=o):(m=o,b=this);for(var M=0,P=0;P<b.length;P++)u=(m.words[P]|0)-(b.words[P]|0)+M,M=u>>26,this.words[P]=u&67108863;for(;M!==0&&P<m.length;P++)u=(m.words[P]|0)+M,M=u>>26,this.words[P]=u&67108863;if(M===0&&P<m.length&&m!==this)for(;P<m.length;P++)this.words[P]=m.words[P];return this.length=Math.max(this.length,P),m!==this&&(this.negative=1),this._strip()},e.prototype.sub=function(o){return this.clone().isub(o)};function O(v,o,u){u.negative=o.negative^v.negative;var h=v.length+o.length|0;u.length=h,h=h-1|0;var m=v.words[0]|0,b=o.words[0]|0,M=m*b,P=M&67108863,g=M/67108864|0;u.words[0]=P;for(var a=1;a<h;a++){for(var w=g>>>26,C=g&67108863,D=Math.min(a,o.length-1),W=Math.max(0,a-v.length+1);W<=D;W++){var z=a-W|0;m=v.words[z]|0,b=o.words[W]|0,M=m*b+C,w+=M/67108864|0,C=M&67108863}u.words[a]=C|0,g=w|0}return g!==0?u.words[a]=g|0:u.length--,u._strip()}var x=function(o,u,h){var m=o.words,b=u.words,M=h.words,P=0,g,a,w,C=m[0]|0,D=C&8191,W=C>>>13,z=m[1]|0,J=z&8191,L=z>>>13,nt=m[2]|0,Q=nt&8191,tt=nt>>>13,Ie=m[3]|0,dt=Ie&8191,ct=Ie>>>13,Qi=m[4]|0,ft=Qi&8191,ht=Qi>>>13,Hi=m[5]|0,pt=Hi&8191,mt=Hi>>>13,Ki=m[6]|0,_t=Ki&8191,gt=Ki>>>13,Gi=m[7]|0,bt=Gi&8191,wt=Gi>>>13,$i=m[8]|0,vt=$i&8191,yt=$i>>>13,zi=m[9]|0,Mt=zi&8191,Pt=zi>>>13,Vi=b[0]|0,At=Vi&8191,Bt=Vi>>>13,Xi=b[1]|0,St=Xi&8191,It=Xi>>>13,Yi=b[2]|0,Et=Yi&8191,Ot=Yi>>>13,Ji=b[3]|0,Tt=Ji&8191,kt=Ji>>>13,tn=b[4]|0,xt=tn&8191,Rt=tn>>>13,en=b[5]|0,Ct=en&8191,Dt=en>>>13,nn=b[6]|0,Ft=nn&8191,Nt=nn>>>13,rn=b[7]|0,Lt=rn&8191,qt=rn>>>13,on=b[8]|0,jt=on&8191,Wt=on>>>13,sn=b[9]|0,Zt=sn&8191,Ut=sn>>>13;h.negative=o.negative^u.negative,h.length=19,g=Math.imul(D,At),a=Math.imul(D,Bt),a=a+Math.imul(W,At)|0,w=Math.imul(W,Bt);var oi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(oi>>>26)|0,oi&=67108863,g=Math.imul(J,At),a=Math.imul(J,Bt),a=a+Math.imul(L,At)|0,w=Math.imul(L,Bt),g=g+Math.imul(D,St)|0,a=a+Math.imul(D,It)|0,a=a+Math.imul(W,St)|0,w=w+Math.imul(W,It)|0;var si=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(si>>>26)|0,si&=67108863,g=Math.imul(Q,At),a=Math.imul(Q,Bt),a=a+Math.imul(tt,At)|0,w=Math.imul(tt,Bt),g=g+Math.imul(J,St)|0,a=a+Math.imul(J,It)|0,a=a+Math.imul(L,St)|0,w=w+Math.imul(L,It)|0,g=g+Math.imul(D,Et)|0,a=a+Math.imul(D,Ot)|0,a=a+Math.imul(W,Et)|0,w=w+Math.imul(W,Ot)|0;var ai=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(ai>>>26)|0,ai&=67108863,g=Math.imul(dt,At),a=Math.imul(dt,Bt),a=a+Math.imul(ct,At)|0,w=Math.imul(ct,Bt),g=g+Math.imul(Q,St)|0,a=a+Math.imul(Q,It)|0,a=a+Math.imul(tt,St)|0,w=w+Math.imul(tt,It)|0,g=g+Math.imul(J,Et)|0,a=a+Math.imul(J,Ot)|0,a=a+Math.imul(L,Et)|0,w=w+Math.imul(L,Ot)|0,g=g+Math.imul(D,Tt)|0,a=a+Math.imul(D,kt)|0,a=a+Math.imul(W,Tt)|0,w=w+Math.imul(W,kt)|0;var li=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(li>>>26)|0,li&=67108863,g=Math.imul(ft,At),a=Math.imul(ft,Bt),a=a+Math.imul(ht,At)|0,w=Math.imul(ht,Bt),g=g+Math.imul(dt,St)|0,a=a+Math.imul(dt,It)|0,a=a+Math.imul(ct,St)|0,w=w+Math.imul(ct,It)|0,g=g+Math.imul(Q,Et)|0,a=a+Math.imul(Q,Ot)|0,a=a+Math.imul(tt,Et)|0,w=w+Math.imul(tt,Ot)|0,g=g+Math.imul(J,Tt)|0,a=a+Math.imul(J,kt)|0,a=a+Math.imul(L,Tt)|0,w=w+Math.imul(L,kt)|0,g=g+Math.imul(D,xt)|0,a=a+Math.imul(D,Rt)|0,a=a+Math.imul(W,xt)|0,w=w+Math.imul(W,Rt)|0;var ui=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(ui>>>26)|0,ui&=67108863,g=Math.imul(pt,At),a=Math.imul(pt,Bt),a=a+Math.imul(mt,At)|0,w=Math.imul(mt,Bt),g=g+Math.imul(ft,St)|0,a=a+Math.imul(ft,It)|0,a=a+Math.imul(ht,St)|0,w=w+Math.imul(ht,It)|0,g=g+Math.imul(dt,Et)|0,a=a+Math.imul(dt,Ot)|0,a=a+Math.imul(ct,Et)|0,w=w+Math.imul(ct,Ot)|0,g=g+Math.imul(Q,Tt)|0,a=a+Math.imul(Q,kt)|0,a=a+Math.imul(tt,Tt)|0,w=w+Math.imul(tt,kt)|0,g=g+Math.imul(J,xt)|0,a=a+Math.imul(J,Rt)|0,a=a+Math.imul(L,xt)|0,w=w+Math.imul(L,Rt)|0,g=g+Math.imul(D,Ct)|0,a=a+Math.imul(D,Dt)|0,a=a+Math.imul(W,Ct)|0,w=w+Math.imul(W,Dt)|0;var di=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(di>>>26)|0,di&=67108863,g=Math.imul(_t,At),a=Math.imul(_t,Bt),a=a+Math.imul(gt,At)|0,w=Math.imul(gt,Bt),g=g+Math.imul(pt,St)|0,a=a+Math.imul(pt,It)|0,a=a+Math.imul(mt,St)|0,w=w+Math.imul(mt,It)|0,g=g+Math.imul(ft,Et)|0,a=a+Math.imul(ft,Ot)|0,a=a+Math.imul(ht,Et)|0,w=w+Math.imul(ht,Ot)|0,g=g+Math.imul(dt,Tt)|0,a=a+Math.imul(dt,kt)|0,a=a+Math.imul(ct,Tt)|0,w=w+Math.imul(ct,kt)|0,g=g+Math.imul(Q,xt)|0,a=a+Math.imul(Q,Rt)|0,a=a+Math.imul(tt,xt)|0,w=w+Math.imul(tt,Rt)|0,g=g+Math.imul(J,Ct)|0,a=a+Math.imul(J,Dt)|0,a=a+Math.imul(L,Ct)|0,w=w+Math.imul(L,Dt)|0,g=g+Math.imul(D,Ft)|0,a=a+Math.imul(D,Nt)|0,a=a+Math.imul(W,Ft)|0,w=w+Math.imul(W,Nt)|0;var ci=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(ci>>>26)|0,ci&=67108863,g=Math.imul(bt,At),a=Math.imul(bt,Bt),a=a+Math.imul(wt,At)|0,w=Math.imul(wt,Bt),g=g+Math.imul(_t,St)|0,a=a+Math.imul(_t,It)|0,a=a+Math.imul(gt,St)|0,w=w+Math.imul(gt,It)|0,g=g+Math.imul(pt,Et)|0,a=a+Math.imul(pt,Ot)|0,a=a+Math.imul(mt,Et)|0,w=w+Math.imul(mt,Ot)|0,g=g+Math.imul(ft,Tt)|0,a=a+Math.imul(ft,kt)|0,a=a+Math.imul(ht,Tt)|0,w=w+Math.imul(ht,kt)|0,g=g+Math.imul(dt,xt)|0,a=a+Math.imul(dt,Rt)|0,a=a+Math.imul(ct,xt)|0,w=w+Math.imul(ct,Rt)|0,g=g+Math.imul(Q,Ct)|0,a=a+Math.imul(Q,Dt)|0,a=a+Math.imul(tt,Ct)|0,w=w+Math.imul(tt,Dt)|0,g=g+Math.imul(J,Ft)|0,a=a+Math.imul(J,Nt)|0,a=a+Math.imul(L,Ft)|0,w=w+Math.imul(L,Nt)|0,g=g+Math.imul(D,Lt)|0,a=a+Math.imul(D,qt)|0,a=a+Math.imul(W,Lt)|0,w=w+Math.imul(W,qt)|0;var fi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(fi>>>26)|0,fi&=67108863,g=Math.imul(vt,At),a=Math.imul(vt,Bt),a=a+Math.imul(yt,At)|0,w=Math.imul(yt,Bt),g=g+Math.imul(bt,St)|0,a=a+Math.imul(bt,It)|0,a=a+Math.imul(wt,St)|0,w=w+Math.imul(wt,It)|0,g=g+Math.imul(_t,Et)|0,a=a+Math.imul(_t,Ot)|0,a=a+Math.imul(gt,Et)|0,w=w+Math.imul(gt,Ot)|0,g=g+Math.imul(pt,Tt)|0,a=a+Math.imul(pt,kt)|0,a=a+Math.imul(mt,Tt)|0,w=w+Math.imul(mt,kt)|0,g=g+Math.imul(ft,xt)|0,a=a+Math.imul(ft,Rt)|0,a=a+Math.imul(ht,xt)|0,w=w+Math.imul(ht,Rt)|0,g=g+Math.imul(dt,Ct)|0,a=a+Math.imul(dt,Dt)|0,a=a+Math.imul(ct,Ct)|0,w=w+Math.imul(ct,Dt)|0,g=g+Math.imul(Q,Ft)|0,a=a+Math.imul(Q,Nt)|0,a=a+Math.imul(tt,Ft)|0,w=w+Math.imul(tt,Nt)|0,g=g+Math.imul(J,Lt)|0,a=a+Math.imul(J,qt)|0,a=a+Math.imul(L,Lt)|0,w=w+Math.imul(L,qt)|0,g=g+Math.imul(D,jt)|0,a=a+Math.imul(D,Wt)|0,a=a+Math.imul(W,jt)|0,w=w+Math.imul(W,Wt)|0;var hi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(hi>>>26)|0,hi&=67108863,g=Math.imul(Mt,At),a=Math.imul(Mt,Bt),a=a+Math.imul(Pt,At)|0,w=Math.imul(Pt,Bt),g=g+Math.imul(vt,St)|0,a=a+Math.imul(vt,It)|0,a=a+Math.imul(yt,St)|0,w=w+Math.imul(yt,It)|0,g=g+Math.imul(bt,Et)|0,a=a+Math.imul(bt,Ot)|0,a=a+Math.imul(wt,Et)|0,w=w+Math.imul(wt,Ot)|0,g=g+Math.imul(_t,Tt)|0,a=a+Math.imul(_t,kt)|0,a=a+Math.imul(gt,Tt)|0,w=w+Math.imul(gt,kt)|0,g=g+Math.imul(pt,xt)|0,a=a+Math.imul(pt,Rt)|0,a=a+Math.imul(mt,xt)|0,w=w+Math.imul(mt,Rt)|0,g=g+Math.imul(ft,Ct)|0,a=a+Math.imul(ft,Dt)|0,a=a+Math.imul(ht,Ct)|0,w=w+Math.imul(ht,Dt)|0,g=g+Math.imul(dt,Ft)|0,a=a+Math.imul(dt,Nt)|0,a=a+Math.imul(ct,Ft)|0,w=w+Math.imul(ct,Nt)|0,g=g+Math.imul(Q,Lt)|0,a=a+Math.imul(Q,qt)|0,a=a+Math.imul(tt,Lt)|0,w=w+Math.imul(tt,qt)|0,g=g+Math.imul(J,jt)|0,a=a+Math.imul(J,Wt)|0,a=a+Math.imul(L,jt)|0,w=w+Math.imul(L,Wt)|0,g=g+Math.imul(D,Zt)|0,a=a+Math.imul(D,Ut)|0,a=a+Math.imul(W,Zt)|0,w=w+Math.imul(W,Ut)|0;var pi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(pi>>>26)|0,pi&=67108863,g=Math.imul(Mt,St),a=Math.imul(Mt,It),a=a+Math.imul(Pt,St)|0,w=Math.imul(Pt,It),g=g+Math.imul(vt,Et)|0,a=a+Math.imul(vt,Ot)|0,a=a+Math.imul(yt,Et)|0,w=w+Math.imul(yt,Ot)|0,g=g+Math.imul(bt,Tt)|0,a=a+Math.imul(bt,kt)|0,a=a+Math.imul(wt,Tt)|0,w=w+Math.imul(wt,kt)|0,g=g+Math.imul(_t,xt)|0,a=a+Math.imul(_t,Rt)|0,a=a+Math.imul(gt,xt)|0,w=w+Math.imul(gt,Rt)|0,g=g+Math.imul(pt,Ct)|0,a=a+Math.imul(pt,Dt)|0,a=a+Math.imul(mt,Ct)|0,w=w+Math.imul(mt,Dt)|0,g=g+Math.imul(ft,Ft)|0,a=a+Math.imul(ft,Nt)|0,a=a+Math.imul(ht,Ft)|0,w=w+Math.imul(ht,Nt)|0,g=g+Math.imul(dt,Lt)|0,a=a+Math.imul(dt,qt)|0,a=a+Math.imul(ct,Lt)|0,w=w+Math.imul(ct,qt)|0,g=g+Math.imul(Q,jt)|0,a=a+Math.imul(Q,Wt)|0,a=a+Math.imul(tt,jt)|0,w=w+Math.imul(tt,Wt)|0,g=g+Math.imul(J,Zt)|0,a=a+Math.imul(J,Ut)|0,a=a+Math.imul(L,Zt)|0,w=w+Math.imul(L,Ut)|0;var mi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(mi>>>26)|0,mi&=67108863,g=Math.imul(Mt,Et),a=Math.imul(Mt,Ot),a=a+Math.imul(Pt,Et)|0,w=Math.imul(Pt,Ot),g=g+Math.imul(vt,Tt)|0,a=a+Math.imul(vt,kt)|0,a=a+Math.imul(yt,Tt)|0,w=w+Math.imul(yt,kt)|0,g=g+Math.imul(bt,xt)|0,a=a+Math.imul(bt,Rt)|0,a=a+Math.imul(wt,xt)|0,w=w+Math.imul(wt,Rt)|0,g=g+Math.imul(_t,Ct)|0,a=a+Math.imul(_t,Dt)|0,a=a+Math.imul(gt,Ct)|0,w=w+Math.imul(gt,Dt)|0,g=g+Math.imul(pt,Ft)|0,a=a+Math.imul(pt,Nt)|0,a=a+Math.imul(mt,Ft)|0,w=w+Math.imul(mt,Nt)|0,g=g+Math.imul(ft,Lt)|0,a=a+Math.imul(ft,qt)|0,a=a+Math.imul(ht,Lt)|0,w=w+Math.imul(ht,qt)|0,g=g+Math.imul(dt,jt)|0,a=a+Math.imul(dt,Wt)|0,a=a+Math.imul(ct,jt)|0,w=w+Math.imul(ct,Wt)|0,g=g+Math.imul(Q,Zt)|0,a=a+Math.imul(Q,Ut)|0,a=a+Math.imul(tt,Zt)|0,w=w+Math.imul(tt,Ut)|0;var _i=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(_i>>>26)|0,_i&=67108863,g=Math.imul(Mt,Tt),a=Math.imul(Mt,kt),a=a+Math.imul(Pt,Tt)|0,w=Math.imul(Pt,kt),g=g+Math.imul(vt,xt)|0,a=a+Math.imul(vt,Rt)|0,a=a+Math.imul(yt,xt)|0,w=w+Math.imul(yt,Rt)|0,g=g+Math.imul(bt,Ct)|0,a=a+Math.imul(bt,Dt)|0,a=a+Math.imul(wt,Ct)|0,w=w+Math.imul(wt,Dt)|0,g=g+Math.imul(_t,Ft)|0,a=a+Math.imul(_t,Nt)|0,a=a+Math.imul(gt,Ft)|0,w=w+Math.imul(gt,Nt)|0,g=g+Math.imul(pt,Lt)|0,a=a+Math.imul(pt,qt)|0,a=a+Math.imul(mt,Lt)|0,w=w+Math.imul(mt,qt)|0,g=g+Math.imul(ft,jt)|0,a=a+Math.imul(ft,Wt)|0,a=a+Math.imul(ht,jt)|0,w=w+Math.imul(ht,Wt)|0,g=g+Math.imul(dt,Zt)|0,a=a+Math.imul(dt,Ut)|0,a=a+Math.imul(ct,Zt)|0,w=w+Math.imul(ct,Ut)|0;var gi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(gi>>>26)|0,gi&=67108863,g=Math.imul(Mt,xt),a=Math.imul(Mt,Rt),a=a+Math.imul(Pt,xt)|0,w=Math.imul(Pt,Rt),g=g+Math.imul(vt,Ct)|0,a=a+Math.imul(vt,Dt)|0,a=a+Math.imul(yt,Ct)|0,w=w+Math.imul(yt,Dt)|0,g=g+Math.imul(bt,Ft)|0,a=a+Math.imul(bt,Nt)|0,a=a+Math.imul(wt,Ft)|0,w=w+Math.imul(wt,Nt)|0,g=g+Math.imul(_t,Lt)|0,a=a+Math.imul(_t,qt)|0,a=a+Math.imul(gt,Lt)|0,w=w+Math.imul(gt,qt)|0,g=g+Math.imul(pt,jt)|0,a=a+Math.imul(pt,Wt)|0,a=a+Math.imul(mt,jt)|0,w=w+Math.imul(mt,Wt)|0,g=g+Math.imul(ft,Zt)|0,a=a+Math.imul(ft,Ut)|0,a=a+Math.imul(ht,Zt)|0,w=w+Math.imul(ht,Ut)|0;var bi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(bi>>>26)|0,bi&=67108863,g=Math.imul(Mt,Ct),a=Math.imul(Mt,Dt),a=a+Math.imul(Pt,Ct)|0,w=Math.imul(Pt,Dt),g=g+Math.imul(vt,Ft)|0,a=a+Math.imul(vt,Nt)|0,a=a+Math.imul(yt,Ft)|0,w=w+Math.imul(yt,Nt)|0,g=g+Math.imul(bt,Lt)|0,a=a+Math.imul(bt,qt)|0,a=a+Math.imul(wt,Lt)|0,w=w+Math.imul(wt,qt)|0,g=g+Math.imul(_t,jt)|0,a=a+Math.imul(_t,Wt)|0,a=a+Math.imul(gt,jt)|0,w=w+Math.imul(gt,Wt)|0,g=g+Math.imul(pt,Zt)|0,a=a+Math.imul(pt,Ut)|0,a=a+Math.imul(mt,Zt)|0,w=w+Math.imul(mt,Ut)|0;var wi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(wi>>>26)|0,wi&=67108863,g=Math.imul(Mt,Ft),a=Math.imul(Mt,Nt),a=a+Math.imul(Pt,Ft)|0,w=Math.imul(Pt,Nt),g=g+Math.imul(vt,Lt)|0,a=a+Math.imul(vt,qt)|0,a=a+Math.imul(yt,Lt)|0,w=w+Math.imul(yt,qt)|0,g=g+Math.imul(bt,jt)|0,a=a+Math.imul(bt,Wt)|0,a=a+Math.imul(wt,jt)|0,w=w+Math.imul(wt,Wt)|0,g=g+Math.imul(_t,Zt)|0,a=a+Math.imul(_t,Ut)|0,a=a+Math.imul(gt,Zt)|0,w=w+Math.imul(gt,Ut)|0;var vi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(vi>>>26)|0,vi&=67108863,g=Math.imul(Mt,Lt),a=Math.imul(Mt,qt),a=a+Math.imul(Pt,Lt)|0,w=Math.imul(Pt,qt),g=g+Math.imul(vt,jt)|0,a=a+Math.imul(vt,Wt)|0,a=a+Math.imul(yt,jt)|0,w=w+Math.imul(yt,Wt)|0,g=g+Math.imul(bt,Zt)|0,a=a+Math.imul(bt,Ut)|0,a=a+Math.imul(wt,Zt)|0,w=w+Math.imul(wt,Ut)|0;var yi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(yi>>>26)|0,yi&=67108863,g=Math.imul(Mt,jt),a=Math.imul(Mt,Wt),a=a+Math.imul(Pt,jt)|0,w=Math.imul(Pt,Wt),g=g+Math.imul(vt,Zt)|0,a=a+Math.imul(vt,Ut)|0,a=a+Math.imul(yt,Zt)|0,w=w+Math.imul(yt,Ut)|0;var Mi=(P+g|0)+((a&8191)<<13)|0;P=(w+(a>>>13)|0)+(Mi>>>26)|0,Mi&=67108863,g=Math.imul(Mt,Zt),a=Math.imul(Mt,Ut),a=a+Math.imul(Pt,Zt)|0,w=Math.imul(Pt,Ut);var Pi=(P+g|0)+((a&8191)<<13)|0;return P=(w+(a>>>13)|0)+(Pi>>>26)|0,Pi&=67108863,M[0]=oi,M[1]=si,M[2]=ai,M[3]=li,M[4]=ui,M[5]=di,M[6]=ci,M[7]=fi,M[8]=hi,M[9]=pi,M[10]=mi,M[11]=_i,M[12]=gi,M[13]=bi,M[14]=wi,M[15]=vi,M[16]=yi,M[17]=Mi,M[18]=Pi,P!==0&&(M[19]=P,h.length++),h};Math.imul||(x=O);function R(v,o,u){u.negative=o.negative^v.negative,u.length=v.length+o.length;for(var h=0,m=0,b=0;b<u.length-1;b++){var M=m;m=0;for(var P=h&67108863,g=Math.min(b,o.length-1),a=Math.max(0,b-v.length+1);a<=g;a++){var w=b-a,C=v.words[w]|0,D=o.words[a]|0,W=C*D,z=W&67108863;M=M+(W/67108864|0)|0,z=z+P|0,P=z&67108863,M=M+(z>>>26)|0,m+=M>>>26,M&=67108863}u.words[b]=P,h=M,M=m}return h!==0?u.words[b]=h:u.length--,u._strip()}function T(v,o,u){return R(v,o,u)}e.prototype.mulTo=function(o,u){var h,m=this.length+o.length;return this.length===10&&o.length===10?h=x(this,o,u):m<63?h=O(this,o,u):m<1024?h=R(this,o,u):h=T(this,o,u),h};function k(v,o){this.x=v,this.y=o}k.prototype.makeRBT=function(o){for(var u=new Array(o),h=e.prototype._countBits(o)-1,m=0;m<o;m++)u[m]=this.revBin(m,h,o);return u},k.prototype.revBin=function(o,u,h){if(o===0||o===h-1)return o;for(var m=0,b=0;b<u;b++)m|=(o&1)<<u-b-1,o>>=1;return m},k.prototype.permute=function(o,u,h,m,b,M){for(var P=0;P<M;P++)m[P]=u[o[P]],b[P]=h[o[P]]},k.prototype.transform=function(o,u,h,m,b,M){this.permute(M,o,u,h,m,b);for(var P=1;P<b;P<<=1)for(var g=P<<1,a=Math.cos(2*Math.PI/g),w=Math.sin(2*Math.PI/g),C=0;C<b;C+=g)for(var D=a,W=w,z=0;z<P;z++){var J=h[C+z],L=m[C+z],nt=h[C+z+P],Q=m[C+z+P],tt=D*nt-W*Q;Q=D*Q+W*nt,nt=tt,h[C+z]=J+nt,m[C+z]=L+Q,h[C+z+P]=J-nt,m[C+z+P]=L-Q,z!==g&&(tt=a*D-w*W,W=a*W+w*D,D=tt)}},k.prototype.guessLen13b=function(o,u){var h=Math.max(u,o)|1,m=h&1,b=0;for(h=h/2|0;h;h=h>>>1)b++;return 1<<b+1+m},k.prototype.conjugate=function(o,u,h){if(!(h<=1))for(var m=0;m<h/2;m++){var b=o[m];o[m]=o[h-m-1],o[h-m-1]=b,b=u[m],u[m]=-u[h-m-1],u[h-m-1]=-b}},k.prototype.normalize13b=function(o,u){for(var h=0,m=0;m<u/2;m++){var b=Math.round(o[2*m+1]/u)*8192+Math.round(o[2*m]/u)+h;o[m]=b&67108863,b<67108864?h=0:h=b/67108864|0}return o},k.prototype.convert13b=function(o,u,h,m){for(var b=0,M=0;M<u;M++)b=b+(o[M]|0),h[2*M]=b&8191,b=b>>>13,h[2*M+1]=b&8191,b=b>>>13;for(M=2*u;M<m;++M)h[M]=0;t(b===0),t((b&-8192)===0)},k.prototype.stub=function(o){for(var u=new Array(o),h=0;h<o;h++)u[h]=0;return u},k.prototype.mulp=function(o,u,h){var m=2*this.guessLen13b(o.length,u.length),b=this.makeRBT(m),M=this.stub(m),P=new Array(m),g=new Array(m),a=new Array(m),w=new Array(m),C=new Array(m),D=new Array(m),W=h.words;W.length=m,this.convert13b(o.words,o.length,P,m),this.convert13b(u.words,u.length,w,m),this.transform(P,M,g,a,m,b),this.transform(w,M,C,D,m,b);for(var z=0;z<m;z++){var J=g[z]*C[z]-a[z]*D[z];a[z]=g[z]*D[z]+a[z]*C[z],g[z]=J}return this.conjugate(g,a,m),this.transform(g,a,W,M,m,b),this.conjugate(W,M,m),this.normalize13b(W,m),h.negative=o.negative^u.negative,h.length=o.length+u.length,h._strip()},e.prototype.mul=function(o){var u=new e(null);return u.words=new Array(this.length+o.length),this.mulTo(o,u)},e.prototype.mulf=function(o){var u=new e(null);return u.words=new Array(this.length+o.length),T(this,o,u)},e.prototype.imul=function(o){return this.clone().mulTo(o,this)},e.prototype.imuln=function(o){var u=o<0;u&&(o=-o),t(typeof o=="number"),t(o<67108864);for(var h=0,m=0;m<this.length;m++){var b=(this.words[m]|0)*o,M=(b&67108863)+(h&67108863);h>>=26,h+=b/67108864|0,h+=M>>>26,this.words[m]=M&67108863}return h!==0&&(this.words[m]=h,this.length++),this.length=o===0?1:this.length,u?this.ineg():this},e.prototype.muln=function(o){return this.clone().imuln(o)},e.prototype.sqr=function(){return this.mul(this)},e.prototype.isqr=function(){return this.imul(this.clone())},e.prototype.pow=function(o){var u=S(o);if(u.length===0)return new e(1);for(var h=this,m=0;m<u.length&&u[m]===0;m++,h=h.sqr());if(++m<u.length)for(var b=h.sqr();m<u.length;m++,b=b.sqr())u[m]!==0&&(h=h.mul(b));return h},e.prototype.iushln=function(o){t(typeof o=="number"&&o>=0);var u=o%26,h=(o-u)/26,m=67108863>>>26-u<<26-u,b;if(u!==0){var M=0;for(b=0;b<this.length;b++){var P=this.words[b]&m,g=(this.words[b]|0)-P<<u;this.words[b]=g|M,M=P>>>26-u}M&&(this.words[b]=M,this.length++)}if(h!==0){for(b=this.length-1;b>=0;b--)this.words[b+h]=this.words[b];for(b=0;b<h;b++)this.words[b]=0;this.length+=h}return this._strip()},e.prototype.ishln=function(o){return t(this.negative===0),this.iushln(o)},e.prototype.iushrn=function(o,u,h){t(typeof o=="number"&&o>=0);var m;u?m=(u-u%26)/26:m=0;var b=o%26,M=Math.min((o-b)/26,this.length),P=67108863^67108863>>>b<<b,g=h;if(m-=M,m=Math.max(0,m),g){for(var a=0;a<M;a++)g.words[a]=this.words[a];g.length=M}if(M!==0)if(this.length>M)for(this.length-=M,a=0;a<this.length;a++)this.words[a]=this.words[a+M];else this.words[0]=0,this.length=1;var w=0;for(a=this.length-1;a>=0&&(w!==0||a>=m);a--){var C=this.words[a]|0;this.words[a]=w<<26-b|C>>>b,w=C&P}return g&&w!==0&&(g.words[g.length++]=w),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},e.prototype.ishrn=function(o,u,h){return t(this.negative===0),this.iushrn(o,u,h)},e.prototype.shln=function(o){return this.clone().ishln(o)},e.prototype.ushln=function(o){return this.clone().iushln(o)},e.prototype.shrn=function(o){return this.clone().ishrn(o)},e.prototype.ushrn=function(o){return this.clone().iushrn(o)},e.prototype.testn=function(o){t(typeof o=="number"&&o>=0);var u=o%26,h=(o-u)/26,m=1<<u;if(this.length<=h)return!1;var b=this.words[h];return!!(b&m)},e.prototype.imaskn=function(o){t(typeof o=="number"&&o>=0);var u=o%26,h=(o-u)/26;if(t(this.negative===0,"imaskn works only with positive numbers"),this.length<=h)return this;if(u!==0&&h++,this.length=Math.min(h,this.length),u!==0){var m=67108863^67108863>>>u<<u;this.words[this.length-1]&=m}return this.length===0&&(this.words[0]=0,this.length=1),this._strip()},e.prototype.maskn=function(o){return this.clone().imaskn(o)},e.prototype.iaddn=function(o){return t(typeof o=="number"),t(o<67108864),o<0?this.isubn(-o):this.negative!==0?this.length===1&&(this.words[0]|0)<=o?(this.words[0]=o-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(o),this.negative=1,this):this._iaddn(o)},e.prototype._iaddn=function(o){this.words[0]+=o;for(var 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},e.prototype.isubn=function(o){if(t(typeof o=="number"),t(o<67108864),o<0)return this.iaddn(-o);if(this.negative!==0)return this.negative=0,this.iaddn(o),this.negative=1,this;if(this.words[0]-=o,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var u=0;u<this.length&&this.words[u]<0;u++)this.words[u]+=67108864,this.words[u+1]-=1;return this._strip()},e.prototype.addn=function(o){return this.clone().iaddn(o)},e.prototype.subn=function(o){return this.clone().isubn(o)},e.prototype.iabs=function(){return this.negative=0,this},e.prototype.abs=function(){return this.clone().iabs()},e.prototype._ishlnsubmul=function(o,u,h){var m=o.length+h,b;this._expand(m);var M,P=0;for(b=0;b<o.length;b++){M=(this.words[b+h]|0)+P;var g=(o.words[b]|0)*u;M-=g&67108863,P=(M>>26)-(g/67108864|0),this.words[b+h]=M&67108863}for(;b<this.length-h;b++)M=(this.words[b+h]|0)+P,P=M>>26,this.words[b+h]=M&67108863;if(P===0)return this._strip();for(t(P===-1),P=0,b=0;b<this.length;b++)M=-(this.words[b]|0)+P,P=M>>26,this.words[b]=M&67108863;return this.negative=1,this._strip()},e.prototype._wordDiv=function(o,u){var h=this.length-o.length,m=this.clone(),b=o,M=b.words[b.length-1]|0,P=this._countBits(M);h=26-P,h!==0&&(b=b.ushln(h),m.iushln(h),M=b.words[b.length-1]|0);var g=m.length-b.length,a;if(u!=="mod"){a=new e(null),a.length=g+1,a.words=new Array(a.length);for(var w=0;w<a.length;w++)a.words[w]=0}var C=m.clone()._ishlnsubmul(b,1,g);C.negative===0&&(m=C,a&&(a.words[g]=1));for(var D=g-1;D>=0;D--){var W=(m.words[b.length+D]|0)*67108864+(m.words[b.length+D-1]|0);for(W=Math.min(W/M|0,67108863),m._ishlnsubmul(b,W,D);m.negative!==0;)W--,m.negative=0,m._ishlnsubmul(b,1,D),m.isZero()||(m.negative^=1);a&&(a.words[D]=W)}return a&&a._strip(),m._strip(),u!=="div"&&h!==0&&m.iushrn(h),{div:a||null,mod:m}},e.prototype.divmod=function(o,u,h){if(t(!o.isZero()),this.isZero())return{div:new e(0),mod:new e(0)};var m,b,M;return this.negative!==0&&o.negative===0?(M=this.neg().divmod(o,u),u!=="mod"&&(m=M.div.neg()),u!=="div"&&(b=M.mod.neg(),h&&b.negative!==0&&b.iadd(o)),{div:m,mod:b}):this.negative===0&&o.negative!==0?(M=this.divmod(o.neg(),u),u!=="mod"&&(m=M.div.neg()),{div:m,mod:M.mod}):(this.negative&o.negative)!==0?(M=this.neg().divmod(o.neg(),u),u!=="div"&&(b=M.mod.neg(),h&&b.negative!==0&&b.isub(o)),{div:M.div,mod:b}):o.length>this.length||this.cmp(o)<0?{div:new e(0),mod:this}:o.length===1?u==="div"?{div:this.divn(o.words[0]),mod:null}:u==="mod"?{div:null,mod:new e(this.modrn(o.words[0]))}:{div:this.divn(o.words[0]),mod:new e(this.modrn(o.words[0]))}:this._wordDiv(o,u)},e.prototype.div=function(o){return this.divmod(o,"div",!1).div},e.prototype.mod=function(o){return this.divmod(o,"mod",!1).mod},e.prototype.umod=function(o){return this.divmod(o,"mod",!0).mod},e.prototype.divRound=function(o){var u=this.divmod(o);if(u.mod.isZero())return u.div;var h=u.div.negative!==0?u.mod.isub(o):u.mod,m=o.ushrn(1),b=o.andln(1),M=h.cmp(m);return M<0||b===1&&M===0?u.div:u.div.negative!==0?u.div.isubn(1):u.div.iaddn(1)},e.prototype.modrn=function(o){var u=o<0;u&&(o=-o),t(o<=67108863);for(var h=(1<<26)%o,m=0,b=this.length-1;b>=0;b--)m=(h*m+(this.words[b]|0))%o;return u?-m:m},e.prototype.modn=function(o){return this.modrn(o)},e.prototype.idivn=function(o){var u=o<0;u&&(o=-o),t(o<=67108863);for(var h=0,m=this.length-1;m>=0;m--){var b=(this.words[m]|0)+h*67108864;this.words[m]=b/o|0,h=b%o}return this._strip(),u?this.ineg():this},e.prototype.divn=function(o){return this.clone().idivn(o)},e.prototype.egcd=function(o){t(o.negative===0),t(!o.isZero());var u=this,h=o.clone();u.negative!==0?u=u.umod(o):u=u.clone();for(var m=new e(1),b=new e(0),M=new e(0),P=new e(1),g=0;u.isEven()&&h.isEven();)u.iushrn(1),h.iushrn(1),++g;for(var a=h.clone(),w=u.clone();!u.isZero();){for(var C=0,D=1;(u.words[0]&D)===0&&C<26;++C,D<<=1);if(C>0)for(u.iushrn(C);C-- >0;)(m.isOdd()||b.isOdd())&&(m.iadd(a),b.isub(w)),m.iushrn(1),b.iushrn(1);for(var W=0,z=1;(h.words[0]&z)===0&&W<26;++W,z<<=1);if(W>0)for(h.iushrn(W);W-- >0;)(M.isOdd()||P.isOdd())&&(M.iadd(a),P.isub(w)),M.iushrn(1),P.iushrn(1);u.cmp(h)>=0?(u.isub(h),m.isub(M),b.isub(P)):(h.isub(u),M.isub(m),P.isub(b))}return{a:M,b:P,gcd:h.iushln(g)}},e.prototype._invmp=function(o){t(o.negative===0),t(!o.isZero());var u=this,h=o.clone();u.negative!==0?u=u.umod(o):u=u.clone();for(var m=new e(1),b=new e(0),M=h.clone();u.cmpn(1)>0&&h.cmpn(1)>0;){for(var P=0,g=1;(u.words[0]&g)===0&&P<26;++P,g<<=1);if(P>0)for(u.iushrn(P);P-- >0;)m.isOdd()&&m.iadd(M),m.iushrn(1);for(var a=0,w=1;(h.words[0]&w)===0&&a<26;++a,w<<=1);if(a>0)for(h.iushrn(a);a-- >0;)b.isOdd()&&b.iadd(M),b.iushrn(1);u.cmp(h)>=0?(u.isub(h),m.isub(b)):(h.isub(u),b.isub(m))}var C;return u.cmpn(1)===0?C=m:C=b,C.cmpn(0)<0&&C.iadd(o),C},e.prototype.gcd=function(o){if(this.isZero())return o.abs();if(o.isZero())return this.abs();var u=this.clone(),h=o.clone();u.negative=0,h.negative=0;for(var m=0;u.isEven()&&h.isEven();m++)u.iushrn(1),h.iushrn(1);do{for(;u.isEven();)u.iushrn(1);for(;h.isEven();)h.iushrn(1);var b=u.cmp(h);if(b<0){var M=u;u=h,h=M}else if(b===0||h.cmpn(1)===0)break;u.isub(h)}while(!0);return h.iushln(m)},e.prototype.invm=function(o){return this.egcd(o).a.umod(o)},e.prototype.isEven=function(){return(this.words[0]&1)===0},e.prototype.isOdd=function(){return(this.words[0]&1)===1},e.prototype.andln=function(o){return this.words[0]&o},e.prototype.bincn=function(o){t(typeof o=="number");var u=o%26,h=(o-u)/26,m=1<<u;if(this.length<=h)return this._expand(h+1),this.words[h]|=m,this;for(var b=m,M=h;b!==0&&M<this.length;M++){var P=this.words[M]|0;P+=b,b=P>>>26,P&=67108863,this.words[M]=P}return b!==0&&(this.words[M]=b,this.length++),this},e.prototype.isZero=function(){return this.length===1&&this.words[0]===0},e.prototype.cmpn=function(o){var u=o<0;if(this.negative!==0&&!u)return-1;if(this.negative===0&&u)return 1;this._strip();var h;if(this.length>1)h=1;else{u&&(o=-o),t(o<=67108863,"Number is too big");var m=this.words[0]|0;h=m===o?0:m<o?-1:1}return this.negative!==0?-h|0:h},e.prototype.cmp=function(o){if(this.negative!==0&&o.negative===0)return-1;if(this.negative===0&&o.negative!==0)return 1;var u=this.ucmp(o);return this.negative!==0?-u|0:u},e.prototype.ucmp=function(o){if(this.length>o.length)return 1;if(this.length<o.length)return-1;for(var u=0,h=this.length-1;h>=0;h--){var m=this.words[h]|0,b=o.words[h]|0;if(m!==b){m<b?u=-1:m>b&&(u=1);break}}return u},e.prototype.gtn=function(o){return this.cmpn(o)===1},e.prototype.gt=function(o){return this.cmp(o)===1},e.prototype.gten=function(o){return this.cmpn(o)>=0},e.prototype.gte=function(o){return this.cmp(o)>=0},e.prototype.ltn=function(o){return this.cmpn(o)===-1},e.prototype.lt=function(o){return this.cmp(o)===-1},e.prototype.lten=function(o){return this.cmpn(o)<=0},e.prototype.lte=function(o){return this.cmp(o)<=0},e.prototype.eqn=function(o){return this.cmpn(o)===0},e.prototype.eq=function(o){return this.cmp(o)===0},e.red=function(o){return new X(o)},e.prototype.toRed=function(o){return t(!this.red,"Already a number in reduction context"),t(this.negative===0,"red works only with positives"),o.convertTo(this)._forceRed(o)},e.prototype.fromRed=function(){return t(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},e.prototype._forceRed=function(o){return this.red=o,this},e.prototype.forceRed=function(o){return t(!this.red,"Already a number in reduction context"),this._forceRed(o)},e.prototype.redAdd=function(o){return t(this.red,"redAdd works only with red numbers"),this.red.add(this,o)},e.prototype.redIAdd=function(o){return t(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,o)},e.prototype.redSub=function(o){return t(this.red,"redSub works only with red numbers"),this.red.sub(this,o)},e.prototype.redISub=function(o){return t(this.red,"redISub works only with red numbers"),this.red.isub(this,o)},e.prototype.redShl=function(o){return t(this.red,"redShl works only with red numbers"),this.red.shl(this,o)},e.prototype.redMul=function(o){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,o),this.red.mul(this,o)},e.prototype.redIMul=function(o){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,o),this.red.imul(this,o)},e.prototype.redSqr=function(){return t(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},e.prototype.redISqr=function(){return t(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},e.prototype.redSqrt=function(){return t(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},e.prototype.redInvm=function(){return t(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},e.prototype.redNeg=function(){return t(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},e.prototype.redPow=function(o){return t(this.red&&!o.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,o)};var j={k256:null,p224:null,p192:null,p25519:null};function N(v,o){this.name=v,this.p=new e(o,16),this.n=this.p.bitLength(),this.k=new e(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}N.prototype._tmp=function(){var o=new e(null);return o.words=new Array(Math.ceil(this.n/13)),o},N.prototype.ireduce=function(o){var u=o,h;do this.split(u,this.tmp),u=this.imulK(u),u=u.iadd(this.tmp),h=u.bitLength();while(h>this.n);var m=h<this.n?-1:u.ucmp(this.p);return m===0?(u.words[0]=0,u.length=1):m>0?u.isub(this.p):u.strip!==void 0?u.strip():u._strip(),u},N.prototype.split=function(o,u){o.iushrn(this.n,0,u)},N.prototype.imulK=function(o){return o.imul(this.k)};function Y(){N.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}n(Y,N),Y.prototype.split=function(o,u){for(var h=4194303,m=Math.min(o.length,9),b=0;b<m;b++)u.words[b]=o.words[b];if(u.length=m,o.length<=9){o.words[0]=0,o.length=1;return}var M=o.words[9];for(u.words[u.length++]=M&h,b=10;b<o.length;b++){var P=o.words[b]|0;o.words[b-10]=(P&h)<<4|M>>>22,M=P}M>>>=22,o.words[b-10]=M,M===0&&o.length>10?o.length-=10:o.length-=9},Y.prototype.imulK=function(o){o.words[o.length]=0,o.words[o.length+1]=0,o.length+=2;for(var u=0,h=0;h<o.length;h++){var m=o.words[h]|0;u+=m*977,o.words[h]=u&67108863,u=m*64+(u/67108864|0)}return o.words[o.length-1]===0&&(o.length--,o.words[o.length-1]===0&&o.length--),o};function et(){N.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}n(et,N);function it(){N.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}n(it,N);function at(){N.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}n(at,N),at.prototype.imulK=function(o){for(var u=0,h=0;h<o.length;h++){var m=(o.words[h]|0)*19+u,b=m&67108863;m>>>=26,o.words[h]=b,u=m}return u!==0&&(o.words[o.length++]=u),o},e._prime=function(o){if(j[o])return j[o];var u;if(o==="k256")u=new Y;else if(o==="p224")u=new et;else if(o==="p192")u=new it;else if(o==="p25519")u=new at;else throw new Error("Unknown prime "+o);return j[o]=u,u};function X(v){if(typeof v=="string"){var o=e._prime(v);this.m=o.p,this.prime=o}else t(v.gtn(1),"modulus must be greater than 1"),this.m=v,this.prime=null}X.prototype._verify1=function(o){t(o.negative===0,"red works only with positives"),t(o.red,"red works only with red numbers")},X.prototype._verify2=function(o,u){t((o.negative|u.negative)===0,"red works only with positives"),t(o.red&&o.red===u.red,"red works only with red numbers")},X.prototype.imod=function(o){return this.prime?this.prime.ireduce(o)._forceRed(this):(f(o,o.umod(this.m)._forceRed(this)),o)},X.prototype.neg=function(o){return o.isZero()?o.clone():this.m.sub(o)._forceRed(this)},X.prototype.add=function(o,u){this._verify2(o,u);var h=o.add(u);return h.cmp(this.m)>=0&&h.isub(this.m),h._forceRed(this)},X.prototype.iadd=function(o,u){this._verify2(o,u);var h=o.iadd(u);return h.cmp(this.m)>=0&&h.isub(this.m),h},X.prototype.sub=function(o,u){this._verify2(o,u);var h=o.sub(u);return h.cmpn(0)<0&&h.iadd(this.m),h._forceRed(this)},X.prototype.isub=function(o,u){this._verify2(o,u);var h=o.isub(u);return h.cmpn(0)<0&&h.iadd(this.m),h},X.prototype.shl=function(o,u){return this._verify1(o),this.imod(o.ushln(u))},X.prototype.imul=function(o,u){return this._verify2(o,u),this.imod(o.imul(u))},X.prototype.mul=function(o,u){return this._verify2(o,u),this.imod(o.mul(u))},X.prototype.isqr=function(o){return this.imul(o,o.clone())},X.prototype.sqr=function(o){return this.mul(o,o)},X.prototype.sqrt=function(o){if(o.isZero())return o.clone();var u=this.m.andln(3);if(t(u%2===1),u===3){var h=this.m.add(new e(1)).iushrn(2);return this.pow(o,h)}for(var m=this.m.subn(1),b=0;!m.isZero()&&m.andln(1)===0;)b++,m.iushrn(1);t(!m.isZero());var M=new e(1).toRed(this),P=M.redNeg(),g=this.m.subn(1).iushrn(1),a=this.m.bitLength();for(a=new e(2*a*a).toRed(this);this.pow(a,g).cmp(P)!==0;)a.redIAdd(P);for(var w=this.pow(a,m),C=this.pow(o,m.addn(1).iushrn(1)),D=this.pow(o,m),W=b;D.cmp(M)!==0;){for(var z=D,J=0;z.cmp(M)!==0;J++)z=z.redSqr();t(J<W);var L=this.pow(w,new e(1).iushln(W-J-1));C=C.redMul(L),w=L.redSqr(),D=D.redMul(w),W=J}return C},X.prototype.invm=function(o){var u=o._invmp(this.m);return u.negative!==0?(u.negative=0,this.imod(u).redNeg()):this.imod(u)},X.prototype.pow=function(o,u){if(u.isZero())return new e(1).toRed(this);if(u.cmpn(1)===0)return o.clone();var h=4,m=new Array(1<<h);m[0]=new e(1).toRed(this),m[1]=o;for(var b=2;b<m.length;b++)m[b]=this.mul(m[b-1],o);var M=m[0],P=0,g=0,a=u.bitLength()%26;for(a===0&&(a=26),b=u.length-1;b>=0;b--){for(var w=u.words[b],C=a-1;C>=0;C--){var D=w>>C&1;if(M!==m[0]&&(M=this.sqr(M)),D===0&&P===0){g=0;continue}P<<=1,P|=D,g++,!(g!==h&&(b!==0||C!==0))&&(M=this.mul(M,m[P]),g=0,P=0)}a=26}return M},X.prototype.convertTo=function(o){var u=o.umod(this.m);return u===o?u.clone():u},X.prototype.convertFrom=function(o){var u=o.clone();return u.red=null,u},e.mont=function(o){return new rt(o)};function rt(v){X.call(this,v),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new e(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}n(rt,X),rt.prototype.convertTo=function(o){return this.imod(o.ushln(this.shift))},rt.prototype.convertFrom=function(o){var u=this.imod(o.mul(this.rinv));return u.red=null,u},rt.prototype.imul=function(o,u){if(o.isZero()||u.isZero())return o.words[0]=0,o.length=1,o;var h=o.imul(u),m=h.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),b=h.isub(m).iushrn(this.shift),M=b;return b.cmp(this.m)>=0?M=b.isub(this.m):b.cmpn(0)<0&&(M=b.iadd(this.m)),M._forceRed(this)},rt.prototype.mul=function(o,u){if(o.isZero()||u.isZero())return new e(0)._forceRed(this);var h=o.mul(u),m=h.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),b=h.isub(m).iushrn(this.shift),M=b;return b.cmp(this.m)>=0?M=b.isub(this.m):b.cmpn(0)<0&&(M=b.iadd(this.m)),M._forceRed(this)},rt.prototype.invm=function(o){var u=this.imod(o._invmp(this.m).mul(this.r2));return u._forceRed(this)}})(typeof Bi>"u"||Bi,un)});import{SdkWrapper as Bo}from"@cetusprotocol/common-sdk";import{FullRpcUrlMainnet as sr,GraphRpcUrlMainnet as ar}from"@cetusprotocol/common-sdk";var ln={env:"mainnet",full_rpc_url:sr,graph_rpc_url:ar,dlmm_pool:{package_id:"0x5664f9d3fd82c84023870cfbda8ea84e14c8dd56ce557ad2116e0668581a682b",published_at:"0x0489a4b326c17428d9ae6f10023468109b097f10e705af30ccc27bbb18ead065",version:9,config:{registry_id:"0xb1d55e7d895823c65f98d99b81a69436cf7d1638629c9ccb921326039cda1f1b",pools_id:"0xc3683b2356cac6423e9ecaea20955c7cc193998b016e5b884730ed1192174991",global_config_id:"0xf31b605d117f959b9730e8c07b08b856cb05143c5e81d5751c90d2979e82f599",versioned_id:"0x05370b2d656612dd5759cbe80463de301e3b94a921dfc72dd9daa2ecdeb2d0a8",admin_cap_id:"0xc4c42bc31cb54beb679dccd547f8bdb970cb6dc989bd1f85a4fed4812ed95d6e",partners_id:"0x5c0affc8d363b6abb1f32790c229165215f4edead89a9bc7cd95dad717b4296a"}},dlmm_router:{package_id:"0x8d389fa25cb08ebc5e520bc520ed394eed9e62b56b7868acb398bf298b8a76f3",published_at:"0x7fc44c7ed805d52d8e40379f5a730125c97b8a5edc43744c121f1d2236da139e",version:7}};import{Transaction as $e}from"@mysten/sui/transactions";import{asUintN as jn,buildSuiGrpcClient as co,CLOCK_ADDRESS as fo,createFullClient as ho,d as qi,deriveDynamicFieldIdByType as po,DETAILS_KEYS as te,fixCoinType as Wn,getPackagerConfigs as Zn,isSortedSymbols as mo}from"@cetusprotocol/common-sdk";import{BaseError as lr}from"@cetusprotocol/common-sdk";var be=class extends lr{constructor(i,t,n){super(i,t||"UnknownError",n)}static isDlmmErrorCode(i,t){return this.isErrorCode(i,t)}},V=(r,i,t)=>{let n={...t,stack:i instanceof Error?i.stack:void 0};throw i instanceof Error?new be(i.message,r,n):new be(i,r,n)};var En=Ai(De(),1);import{asIntN as Ae,d as ke,DETAILS_KEYS as pe,extractStructTagFromType as Ci,fixCoinType as re,MathUtil as ro,OptionU64Raw as Sn,TypeNameRaw as Ri}from"@cetusprotocol/common-sdk";var we=9e15,ce=1e9,Si="0123456789abcdef",Le="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",qe="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Ii={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-we,maxE:we,crypto:!1},hn,se,$=!0,We="[DecimalError] ",de=We+"Invalid argument: ",pn=We+"Precision limit exceeded",mn=We+"crypto unavailable",_n="[object Decimal]",Gt=Math.floor,Qt=Math.pow,ur=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,dr=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,cr=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,gn=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,Xt=1e7,H=7,fr=9007199254740991,hr=Le.length-1,Ei=qe.length-1,I={toStringTag:_n};I.absoluteValue=I.abs=function(){var r=new this.constructor(this);return r.s<0&&(r.s=1),U(r)};I.ceil=function(){return U(new this.constructor(this),this.e+1,2)};I.clampedTo=I.clamp=function(r,i){var t,n=this,e=n.constructor;if(r=new e(r),i=new e(i),!r.s||!i.s)return new e(NaN);if(r.gt(i))throw Error(de+i);return t=n.cmp(r),t<0?r:n.cmp(i)>0?i:new e(n)};I.comparedTo=I.cmp=function(r){var i,t,n,e,s=this,l=s.d,d=(r=new s.constructor(r)).d,c=s.s,f=r.s;if(!l||!d)return!c||!f?NaN:c!==f?c:l===d?0:!l^c<0?1:-1;if(!l[0]||!d[0])return l[0]?c:d[0]?-f:0;if(c!==f)return c;if(s.e!==r.e)return s.e>r.e^c<0?1:-1;for(n=l.length,e=d.length,i=0,t=n<e?n:e;i<t;++i)if(l[i]!==d[i])return l[i]>d[i]^c<0?1:-1;return n===e?0:n>e^c<0?1:-1};I.cosine=I.cos=function(){var r,i,t=this,n=t.constructor;return t.d?t.d[0]?(r=n.precision,i=n.rounding,n.precision=r+Math.max(t.e,t.sd())+H,n.rounding=1,t=pr(n,Mn(n,t)),n.precision=r,n.rounding=i,U(se==2||se==3?t.neg():t,r,i,!0)):new n(1):new n(NaN)};I.cubeRoot=I.cbrt=function(){var r,i,t,n,e,s,l,d,c,f,p=this,_=p.constructor;if(!p.isFinite()||p.isZero())return new _(p);for($=!1,s=p.s*Qt(p.s*p,1/3),!s||Math.abs(s)==1/0?(t=Kt(p.d),r=p.e,(s=(r-t.length+1)%3)&&(t+=s==1||s==-2?"0":"00"),s=Qt(t,1/3),r=Gt((r+1)/3)-(r%3==(r<0?-1:2)),s==1/0?t="5e"+r:(t=s.toExponential(),t=t.slice(0,t.indexOf("e")+1)+r),n=new _(t),n.s=p.s):n=new _(s.toString()),l=(r=_.precision)+3;;)if(d=n,c=d.times(d).times(d),f=c.plus(p),n=st(f.plus(p).times(d),f.plus(c),l+2,1),Kt(d.d).slice(0,l)===(t=Kt(n.d)).slice(0,l))if(t=t.slice(l-3,l+1),t=="9999"||!e&&t=="4999"){if(!e&&(U(d,r+1,0),d.times(d).times(d).eq(p))){n=d;break}l+=4,e=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(U(n,r+1,1),i=!n.times(n).times(n).eq(p));break}return $=!0,U(n,r,_.rounding,i)};I.decimalPlaces=I.dp=function(){var r,i=this.d,t=NaN;if(i){if(r=i.length-1,t=(r-Gt(this.e/H))*H,r=i[r],r)for(;r%10==0;r/=10)t--;t<0&&(t=0)}return t};I.dividedBy=I.div=function(r){return st(this,new this.constructor(r))};I.dividedToIntegerBy=I.divToInt=function(r){var i=this,t=i.constructor;return U(st(i,new t(r),0,1,1),t.precision,t.rounding)};I.equals=I.eq=function(r){return this.cmp(r)===0};I.floor=function(){return U(new this.constructor(this),this.e+1,3)};I.greaterThan=I.gt=function(r){return this.cmp(r)>0};I.greaterThanOrEqualTo=I.gte=function(r){var i=this.cmp(r);return i==1||i===0};I.hyperbolicCosine=I.cosh=function(){var r,i,t,n,e,s=this,l=s.constructor,d=new l(1);if(!s.isFinite())return new l(s.s?1/0:NaN);if(s.isZero())return d;t=l.precision,n=l.rounding,l.precision=t+Math.max(s.e,s.sd())+4,l.rounding=1,e=s.d.length,e<32?(r=Math.ceil(e/3),i=(1/Ue(4,r)).toString()):(r=16,i="2.3283064365386962890625e-10"),s=ve(l,1,s.times(i),new l(1),!0);for(var c,f=r,p=new l(8);f--;)c=s.times(s),s=d.minus(c.times(p.minus(c.times(p))));return U(s,l.precision=t,l.rounding=n,!0)};I.hyperbolicSine=I.sinh=function(){var r,i,t,n,e=this,s=e.constructor;if(!e.isFinite()||e.isZero())return new s(e);if(i=s.precision,t=s.rounding,s.precision=i+Math.max(e.e,e.sd())+4,s.rounding=1,n=e.d.length,n<3)e=ve(s,2,e,e,!0);else{r=1.4*Math.sqrt(n),r=r>16?16:r|0,e=e.times(1/Ue(5,r)),e=ve(s,2,e,e,!0);for(var l,d=new s(5),c=new s(16),f=new s(20);r--;)l=e.times(e),e=e.times(d.plus(l.times(c.times(l).plus(f))))}return s.precision=i,s.rounding=t,U(e,i,t,!0)};I.hyperbolicTangent=I.tanh=function(){var r,i,t=this,n=t.constructor;return t.isFinite()?t.isZero()?new n(t):(r=n.precision,i=n.rounding,n.precision=r+7,n.rounding=1,st(t.sinh(),t.cosh(),n.precision=r,n.rounding=i)):new n(t.s)};I.inverseCosine=I.acos=function(){var r=this,i=r.constructor,t=r.abs().cmp(1),n=i.precision,e=i.rounding;return t!==-1?t===0?r.isNeg()?ie(i,n,e):new i(0):new i(NaN):r.isZero()?ie(i,n+4,e).times(.5):(i.precision=n+6,i.rounding=1,r=new i(1).minus(r).div(r.plus(1)).sqrt().atan(),i.precision=n,i.rounding=e,r.times(2))};I.inverseHyperbolicCosine=I.acosh=function(){var r,i,t=this,n=t.constructor;return t.lte(1)?new n(t.eq(1)?0:NaN):t.isFinite()?(r=n.precision,i=n.rounding,n.precision=r+Math.max(Math.abs(t.e),t.sd())+4,n.rounding=1,$=!1,t=t.times(t).minus(1).sqrt().plus(t),$=!0,n.precision=r,n.rounding=i,t.ln()):new n(t)};I.inverseHyperbolicSine=I.asinh=function(){var r,i,t=this,n=t.constructor;return!t.isFinite()||t.isZero()?new n(t):(r=n.precision,i=n.rounding,n.precision=r+2*Math.max(Math.abs(t.e),t.sd())+6,n.rounding=1,$=!1,t=t.times(t).plus(1).sqrt().plus(t),$=!0,n.precision=r,n.rounding=i,t.ln())};I.inverseHyperbolicTangent=I.atanh=function(){var r,i,t,n,e=this,s=e.constructor;return e.isFinite()?e.e>=0?new s(e.abs().eq(1)?e.s/0:e.isZero()?e:NaN):(r=s.precision,i=s.rounding,n=e.sd(),Math.max(n,r)<2*-e.e-1?U(new s(e),r,i,!0):(s.precision=t=n-e.e,e=st(e.plus(1),new s(1).minus(e),t+r,1),s.precision=r+4,s.rounding=1,e=e.ln(),s.precision=r,s.rounding=i,e.times(.5))):new s(NaN)};I.inverseSine=I.asin=function(){var r,i,t,n,e=this,s=e.constructor;return e.isZero()?new s(e):(i=e.abs().cmp(1),t=s.precision,n=s.rounding,i!==-1?i===0?(r=ie(s,t+4,n).times(.5),r.s=e.s,r):new s(NaN):(s.precision=t+6,s.rounding=1,e=e.div(new s(1).minus(e.times(e)).sqrt().plus(1)).atan(),s.precision=t,s.rounding=n,e.times(2)))};I.inverseTangent=I.atan=function(){var r,i,t,n,e,s,l,d,c,f=this,p=f.constructor,_=p.precision,y=p.rounding;if(f.isFinite()){if(f.isZero())return new p(f);if(f.abs().eq(1)&&_+4<=Ei)return l=ie(p,_+4,y).times(.25),l.s=f.s,l}else{if(!f.s)return new p(NaN);if(_+4<=Ei)return l=ie(p,_+4,y).times(.5),l.s=f.s,l}for(p.precision=d=_+10,p.rounding=1,t=Math.min(28,d/H+2|0),r=t;r;--r)f=f.div(f.times(f).plus(1).sqrt().plus(1));for($=!1,i=Math.ceil(d/H),n=1,c=f.times(f),l=new p(f),e=f;r!==-1;)if(e=e.times(c),s=l.minus(e.div(n+=2)),e=e.times(c),l=s.plus(e.div(n+=2)),l.d[i]!==void 0)for(r=i;l.d[r]===s.d[r]&&r--;);return t&&(l=l.times(2<<t-1)),$=!0,U(l,p.precision=_,p.rounding=y,!0)};I.isFinite=function(){return!!this.d};I.isInteger=I.isInt=function(){return!!this.d&&Gt(this.e/H)>this.d.length-2};I.isNaN=function(){return!this.s};I.isNegative=I.isNeg=function(){return this.s<0};I.isPositive=I.isPos=function(){return this.s>0};I.isZero=function(){return!!this.d&&this.d[0]===0};I.lessThan=I.lt=function(r){return this.cmp(r)<0};I.lessThanOrEqualTo=I.lte=function(r){return this.cmp(r)<1};I.logarithm=I.log=function(r){var i,t,n,e,s,l,d,c,f=this,p=f.constructor,_=p.precision,y=p.rounding,A=5;if(r==null)r=new p(10),i=!0;else{if(r=new p(r),t=r.d,r.s<0||!t||!t[0]||r.eq(1))return new p(NaN);i=r.eq(10)}if(t=f.d,f.s<0||!t||!t[0]||f.eq(1))return new p(t&&!t[0]?-1/0:f.s!=1?NaN:t?0:1/0);if(i)if(t.length>1)s=!0;else{for(e=t[0];e%10===0;)e/=10;s=e!==1}if($=!1,d=_+A,l=ue(f,d),n=i?je(p,d+10):ue(r,d),c=st(l,n,d,1),Ee(c.d,e=_,y))do if(d+=10,l=ue(f,d),n=i?je(p,d+10):ue(r,d),c=st(l,n,d,1),!s){+Kt(c.d).slice(e+1,e+15)+1==1e14&&(c=U(c,_+1,0));break}while(Ee(c.d,e+=10,y));return $=!0,U(c,_,y)};I.minus=I.sub=function(r){var i,t,n,e,s,l,d,c,f,p,_,y,A=this,B=A.constructor;if(r=new B(r),!A.d||!r.d)return!A.s||!r.s?r=new B(NaN):A.d?r.s=-r.s:r=new B(r.d||A.s!==r.s?A:NaN),r;if(A.s!=r.s)return r.s=-r.s,A.plus(r);if(f=A.d,y=r.d,d=B.precision,c=B.rounding,!f[0]||!y[0]){if(y[0])r.s=-r.s;else if(f[0])r=new B(A);else return new B(c===3?-0:0);return $?U(r,d,c):r}if(t=Gt(r.e/H),p=Gt(A.e/H),f=f.slice(),s=p-t,s){for(_=s<0,_?(i=f,s=-s,l=y.length):(i=y,t=p,l=f.length),n=Math.max(Math.ceil(d/H),l)+2,s>n&&(s=n,i.length=1),i.reverse(),n=s;n--;)i.push(0);i.reverse()}else{for(n=f.length,l=y.length,_=n<l,_&&(l=n),n=0;n<l;n++)if(f[n]!=y[n]){_=f[n]<y[n];break}s=0}for(_&&(i=f,f=y,y=i,r.s=-r.s),l=f.length,n=y.length-l;n>0;--n)f[l++]=0;for(n=y.length;n>s;){if(f[--n]<y[n]){for(e=n;e&&f[--e]===0;)f[e]=Xt-1;--f[e],f[n]+=Xt}f[n]-=y[n]}for(;f[--l]===0;)f.pop();for(;f[0]===0;f.shift())--t;return f[0]?(r.d=f,r.e=Ze(f,t),$?U(r,d,c):r):new B(c===3?-0:0)};I.modulo=I.mod=function(r){var i,t=this,n=t.constructor;return r=new n(r),!t.d||!r.s||r.d&&!r.d[0]?new n(NaN):!r.d||t.d&&!t.d[0]?U(new n(t),n.precision,n.rounding):($=!1,n.modulo==9?(i=st(t,r.abs(),0,3,1),i.s*=r.s):i=st(t,r,0,n.modulo,1),i=i.times(r),$=!0,t.minus(i))};I.naturalExponential=I.exp=function(){return Oi(this)};I.naturalLogarithm=I.ln=function(){return ue(this)};I.negated=I.neg=function(){var r=new this.constructor(this);return r.s=-r.s,U(r)};I.plus=I.add=function(r){var i,t,n,e,s,l,d,c,f,p,_=this,y=_.constructor;if(r=new y(r),!_.d||!r.d)return!_.s||!r.s?r=new y(NaN):_.d||(r=new y(r.d||_.s===r.s?_:NaN)),r;if(_.s!=r.s)return r.s=-r.s,_.minus(r);if(f=_.d,p=r.d,d=y.precision,c=y.rounding,!f[0]||!p[0])return p[0]||(r=new y(_)),$?U(r,d,c):r;if(s=Gt(_.e/H),n=Gt(r.e/H),f=f.slice(),e=s-n,e){for(e<0?(t=f,e=-e,l=p.length):(t=p,n=s,l=f.length),s=Math.ceil(d/H),l=s>l?s+1:l+1,e>l&&(e=l,t.length=1),t.reverse();e--;)t.push(0);t.reverse()}for(l=f.length,e=p.length,l-e<0&&(e=l,t=p,p=f,f=t),i=0;e;)i=(f[--e]=f[e]+p[e]+i)/Xt|0,f[e]%=Xt;for(i&&(f.unshift(i),++n),l=f.length;f[--l]==0;)f.pop();return r.d=f,r.e=Ze(f,n),$?U(r,d,c):r};I.precision=I.sd=function(r){var i,t=this;if(r!==void 0&&r!==!!r&&r!==1&&r!==0)throw Error(de+r);return t.d?(i=bn(t.d),r&&t.e+1>i&&(i=t.e+1)):i=NaN,i};I.round=function(){var r=this,i=r.constructor;return U(new i(r),r.e+1,i.rounding)};I.sine=I.sin=function(){var r,i,t=this,n=t.constructor;return t.isFinite()?t.isZero()?new n(t):(r=n.precision,i=n.rounding,n.precision=r+Math.max(t.e,t.sd())+H,n.rounding=1,t=_r(n,Mn(n,t)),n.precision=r,n.rounding=i,U(se>2?t.neg():t,r,i,!0)):new n(NaN)};I.squareRoot=I.sqrt=function(){var r,i,t,n,e,s,l=this,d=l.d,c=l.e,f=l.s,p=l.constructor;if(f!==1||!d||!d[0])return new p(!f||f<0&&(!d||d[0])?NaN:d?l:1/0);for($=!1,f=Math.sqrt(+l),f==0||f==1/0?(i=Kt(d),(i.length+c)%2==0&&(i+="0"),f=Math.sqrt(i),c=Gt((c+1)/2)-(c<0||c%2),f==1/0?i="5e"+c:(i=f.toExponential(),i=i.slice(0,i.indexOf("e")+1)+c),n=new p(i)):n=new p(f.toString()),t=(c=p.precision)+3;;)if(s=n,n=s.plus(st(l,s,t+2,1)).times(.5),Kt(s.d).slice(0,t)===(i=Kt(n.d)).slice(0,t))if(i=i.slice(t-3,t+1),i=="9999"||!e&&i=="4999"){if(!e&&(U(s,c+1,0),s.times(s).eq(l))){n=s;break}t+=4,e=1}else{(!+i||!+i.slice(1)&&i.charAt(0)=="5")&&(U(n,c+1,1),r=!n.times(n).eq(l));break}return $=!0,U(n,c,p.rounding,r)};I.tangent=I.tan=function(){var r,i,t=this,n=t.constructor;return t.isFinite()?t.isZero()?new n(t):(r=n.precision,i=n.rounding,n.precision=r+10,n.rounding=1,t=t.sin(),t.s=1,t=st(t,new n(1).minus(t.times(t)).sqrt(),r+10,0),n.precision=r,n.rounding=i,U(se==2||se==4?t.neg():t,r,i,!0)):new n(NaN)};I.times=I.mul=function(r){var i,t,n,e,s,l,d,c,f,p=this,_=p.constructor,y=p.d,A=(r=new _(r)).d;if(r.s*=p.s,!y||!y[0]||!A||!A[0])return new _(!r.s||y&&!y[0]&&!A||A&&!A[0]&&!y?NaN:!y||!A?r.s/0:r.s*0);for(t=Gt(p.e/H)+Gt(r.e/H),c=y.length,f=A.length,c<f&&(s=y,y=A,A=s,l=c,c=f,f=l),s=[],l=c+f,n=l;n--;)s.push(0);for(n=f;--n>=0;){for(i=0,e=c+n;e>n;)d=s[e]+A[n]*y[e-n-1]+i,s[e--]=d%Xt|0,i=d/Xt|0;s[e]=(s[e]+i)%Xt|0}for(;!s[--l];)s.pop();return i?++t:s.shift(),r.d=s,r.e=Ze(s,t),$?U(r,_.precision,_.rounding):r};I.toBinary=function(r,i){return Ti(this,2,r,i)};I.toDecimalPlaces=I.toDP=function(r,i){var t=this,n=t.constructor;return t=new n(t),r===void 0?t:($t(r,0,ce),i===void 0?i=n.rounding:$t(i,0,8),U(t,r+t.e+1,i))};I.toExponential=function(r,i){var t,n=this,e=n.constructor;return r===void 0?t=ne(n,!0):($t(r,0,ce),i===void 0?i=e.rounding:$t(i,0,8),n=U(new e(n),r+1,i),t=ne(n,!0,r+1)),n.isNeg()&&!n.isZero()?"-"+t:t};I.toFixed=function(r,i){var t,n,e=this,s=e.constructor;return r===void 0?t=ne(e):($t(r,0,ce),i===void 0?i=s.rounding:$t(i,0,8),n=U(new s(e),r+e.e+1,i),t=ne(n,!1,r+n.e+1)),e.isNeg()&&!e.isZero()?"-"+t:t};I.toFraction=function(r){var i,t,n,e,s,l,d,c,f,p,_,y,A=this,B=A.d,S=A.constructor;if(!B)return new S(A);if(f=t=new S(1),n=c=new S(0),i=new S(n),s=i.e=bn(B)-A.e-1,l=s%H,i.d[0]=Qt(10,l<0?H+l:l),r==null)r=s>0?i:f;else{if(d=new S(r),!d.isInt()||d.lt(f))throw Error(de+d);r=d.gt(i)?s>0?i:f:d}for($=!1,d=new S(Kt(B)),p=S.precision,S.precision=s=B.length*H*2;_=st(d,i,0,1,1),e=t.plus(_.times(n)),e.cmp(r)!=1;)t=n,n=e,e=f,f=c.plus(_.times(e)),c=e,e=i,i=d.minus(_.times(e)),d=e;return e=st(r.minus(t),n,0,1,1),c=c.plus(e.times(f)),t=t.plus(e.times(n)),c.s=f.s=A.s,y=st(f,n,s,1).minus(A).abs().cmp(st(c,t,s,1).minus(A).abs())<1?[f,n]:[c,t],S.precision=p,$=!0,y};I.toHexadecimal=I.toHex=function(r,i){return Ti(this,16,r,i)};I.toNearest=function(r,i){var t=this,n=t.constructor;if(t=new n(t),r==null){if(!t.d)return t;r=new n(1),i=n.rounding}else{if(r=new n(r),i===void 0?i=n.rounding:$t(i,0,8),!t.d)return r.s?t:r;if(!r.d)return r.s&&(r.s=t.s),r}return r.d[0]?($=!1,t=st(t,r,0,i,1).times(r),$=!0,U(t)):(r.s=t.s,t=r),t};I.toNumber=function(){return+this};I.toOctal=function(r,i){return Ti(this,8,r,i)};I.toPower=I.pow=function(r){var i,t,n,e,s,l,d=this,c=d.constructor,f=+(r=new c(r));if(!d.d||!r.d||!d.d[0]||!r.d[0])return new c(Qt(+d,f));if(d=new c(d),d.eq(1))return d;if(n=c.precision,s=c.rounding,r.eq(1))return U(d,n,s);if(i=Gt(r.e/H),i>=r.d.length-1&&(t=f<0?-f:f)<=fr)return e=wn(c,d,t,n),r.s<0?new c(1).div(e):U(e,n,s);if(l=d.s,l<0){if(i<r.d.length-1)return new c(NaN);if((r.d[i]&1)==0&&(l=1),d.e==0&&d.d[0]==1&&d.d.length==1)return d.s=l,d}return t=Qt(+d,f),i=t==0||!isFinite(t)?Gt(f*(Math.log("0."+Kt(d.d))/Math.LN10+d.e+1)):new c(t+"").e,i>c.maxE+1||i<c.minE-1?new c(i>0?l/0:0):($=!1,c.rounding=d.s=1,t=Math.min(12,(i+"").length),e=Oi(r.times(ue(d,n+t)),n),e.d&&(e=U(e,n+5,1),Ee(e.d,n,s)&&(i=n+10,e=U(Oi(r.times(ue(d,i+t)),i),i+5,1),+Kt(e.d).slice(n+1,n+15)+1==1e14&&(e=U(e,n+1,0)))),e.s=l,$=!0,c.rounding=s,U(e,n,s))};I.toPrecision=function(r,i){var t,n=this,e=n.constructor;return r===void 0?t=ne(n,n.e<=e.toExpNeg||n.e>=e.toExpPos):($t(r,1,ce),i===void 0?i=e.rounding:$t(i,0,8),n=U(new e(n),r,i),t=ne(n,r<=n.e||n.e<=e.toExpNeg,r)),n.isNeg()&&!n.isZero()?"-"+t:t};I.toSignificantDigits=I.toSD=function(r,i){var t=this,n=t.constructor;return r===void 0?(r=n.precision,i=n.rounding):($t(r,1,ce),i===void 0?i=n.rounding:$t(i,0,8)),U(new n(t),r,i)};I.toString=function(){var r=this,i=r.constructor,t=ne(r,r.e<=i.toExpNeg||r.e>=i.toExpPos);return r.isNeg()&&!r.isZero()?"-"+t:t};I.truncated=I.trunc=function(){return U(new this.constructor(this),this.e+1,1)};I.valueOf=I.toJSON=function(){var r=this,i=r.constructor,t=ne(r,r.e<=i.toExpNeg||r.e>=i.toExpPos);return r.isNeg()?"-"+t:t};function Kt(r){var i,t,n,e=r.length-1,s="",l=r[0];if(e>0){for(s+=l,i=1;i<e;i++)n=r[i]+"",t=H-n.length,t&&(s+=le(t)),s+=n;l=r[i],n=l+"",t=H-n.length,t&&(s+=le(t))}else if(l===0)return"0";for(;l%10===0;)l/=10;return s+l}function $t(r,i,t){if(r!==~~r||r<i||r>t)throw Error(de+r)}function Ee(r,i,t,n){var e,s,l,d;for(s=r[0];s>=10;s/=10)--i;return--i<0?(i+=H,e=0):(e=Math.ceil((i+1)/H),i%=H),s=Qt(10,H-i),d=r[e]%s|0,n==null?i<3?(i==0?d=d/100|0:i==1&&(d=d/10|0),l=t<4&&d==99999||t>3&&d==49999||d==5e4||d==0):l=(t<4&&d+1==s||t>3&&d+1==s/2)&&(r[e+1]/s/100|0)==Qt(10,i-2)-1||(d==s/2||d==0)&&(r[e+1]/s/100|0)==0:i<4?(i==0?d=d/1e3|0:i==1?d=d/100|0:i==2&&(d=d/10|0),l=(n||t<4)&&d==9999||!n&&t>3&&d==4999):l=((n||t<4)&&d+1==s||!n&&t>3&&d+1==s/2)&&(r[e+1]/s/1e3|0)==Qt(10,i-3)-1,l}function Fe(r,i,t){for(var n,e=[0],s,l=0,d=r.length;l<d;){for(s=e.length;s--;)e[s]*=i;for(e[0]+=Si.indexOf(r.charAt(l++)),n=0;n<e.length;n++)e[n]>t-1&&(e[n+1]===void 0&&(e[n+1]=0),e[n+1]+=e[n]/t|0,e[n]%=t)}return e.reverse()}function pr(r,i){var t,n,e;if(i.isZero())return i;n=i.d.length,n<32?(t=Math.ceil(n/3),e=(1/Ue(4,t)).toString()):(t=16,e="2.3283064365386962890625e-10"),r.precision+=t,i=ve(r,1,i.times(e),new r(1));for(var s=t;s--;){var l=i.times(i);i=l.times(l).minus(l).times(8).plus(1)}return r.precision-=t,i}var st=(function(){function r(n,e,s){var l,d=0,c=n.length;for(n=n.slice();c--;)l=n[c]*e+d,n[c]=l%s|0,d=l/s|0;return d&&n.unshift(d),n}function i(n,e,s,l){var d,c;if(s!=l)c=s>l?1:-1;else for(d=c=0;d<s;d++)if(n[d]!=e[d]){c=n[d]>e[d]?1:-1;break}return c}function t(n,e,s,l){for(var d=0;s--;)n[s]-=d,d=n[s]<e[s]?1:0,n[s]=d*l+n[s]-e[s];for(;!n[0]&&n.length>1;)n.shift()}return function(n,e,s,l,d,c){var f,p,_,y,A,B,S,O,x,R,T,k,j,N,Y,et,it,at,X,rt,v=n.constructor,o=n.s==e.s?1:-1,u=n.d,h=e.d;if(!u||!u[0]||!h||!h[0])return new v(!n.s||!e.s||(u?h&&u[0]==h[0]:!h)?NaN:u&&u[0]==0||!h?o*0:o/0);for(c?(A=1,p=n.e-e.e):(c=Xt,A=H,p=Gt(n.e/A)-Gt(e.e/A)),X=h.length,it=u.length,x=new v(o),R=x.d=[],_=0;h[_]==(u[_]||0);_++);if(h[_]>(u[_]||0)&&p--,s==null?(N=s=v.precision,l=v.rounding):d?N=s+(n.e-e.e)+1:N=s,N<0)R.push(1),B=!0;else{if(N=N/A+2|0,_=0,X==1){for(y=0,h=h[0],N++;(_<it||y)&&N--;_++)Y=y*c+(u[_]||0),R[_]=Y/h|0,y=Y%h|0;B=y||_<it}else{for(y=c/(h[0]+1)|0,y>1&&(h=r(h,y,c),u=r(u,y,c),X=h.length,it=u.length),et=X,T=u.slice(0,X),k=T.length;k<X;)T[k++]=0;rt=h.slice(),rt.unshift(0),at=h[0],h[1]>=c/2&&++at;do y=0,f=i(h,T,X,k),f<0?(j=T[0],X!=k&&(j=j*c+(T[1]||0)),y=j/at|0,y>1?(y>=c&&(y=c-1),S=r(h,y,c),O=S.length,k=T.length,f=i(S,T,O,k),f==1&&(y--,t(S,X<O?rt:h,O,c))):(y==0&&(f=y=1),S=h.slice()),O=S.length,O<k&&S.unshift(0),t(T,S,k,c),f==-1&&(k=T.length,f=i(h,T,X,k),f<1&&(y++,t(T,X<k?rt:h,k,c))),k=T.length):f===0&&(y++,T=[0]),R[_++]=y,f&&T[0]?T[k++]=u[et]||0:(T=[u[et]],k=1);while((et++<it||T[0]!==void 0)&&N--);B=T[0]!==void 0}R[0]||R.shift()}if(A==1)x.e=p,hn=B;else{for(_=1,y=R[0];y>=10;y/=10)_++;x.e=_+p*A-1,U(x,d?s+x.e+1:s,l,B)}return x}})();function U(r,i,t,n){var e,s,l,d,c,f,p,_,y,A=r.constructor;t:if(i!=null){if(_=r.d,!_)return r;for(e=1,d=_[0];d>=10;d/=10)e++;if(s=i-e,s<0)s+=H,l=i,p=_[y=0],c=p/Qt(10,e-l-1)%10|0;else if(y=Math.ceil((s+1)/H),d=_.length,y>=d)if(n){for(;d++<=y;)_.push(0);p=c=0,e=1,s%=H,l=s-H+1}else break t;else{for(p=d=_[y],e=1;d>=10;d/=10)e++;s%=H,l=s-H+e,c=l<0?0:p/Qt(10,e-l-1)%10|0}if(n=n||i<0||_[y+1]!==void 0||(l<0?p:p%Qt(10,e-l-1)),f=t<4?(c||n)&&(t==0||t==(r.s<0?3:2)):c>5||c==5&&(t==4||n||t==6&&(s>0?l>0?p/Qt(10,e-l):0:_[y-1])%10&1||t==(r.s<0?8:7)),i<1||!_[0])return _.length=0,f?(i-=r.e+1,_[0]=Qt(10,(H-i%H)%H),r.e=-i||0):_[0]=r.e=0,r;if(s==0?(_.length=y,d=1,y--):(_.length=y+1,d=Qt(10,H-s),_[y]=l>0?(p/Qt(10,e-l)%Qt(10,l)|0)*d:0),f)for(;;)if(y==0){for(s=1,l=_[0];l>=10;l/=10)s++;for(l=_[0]+=d,d=1;l>=10;l/=10)d++;s!=d&&(r.e++,_[0]==Xt&&(_[0]=1));break}else{if(_[y]+=d,_[y]!=Xt)break;_[y--]=0,d=1}for(s=_.length;_[--s]===0;)_.pop()}return $&&(r.e>A.maxE?(r.d=null,r.e=NaN):r.e<A.minE&&(r.e=0,r.d=[0])),r}function ne(r,i,t){if(!r.isFinite())return yn(r);var n,e=r.e,s=Kt(r.d),l=s.length;return i?(t&&(n=t-l)>0?s=s.charAt(0)+"."+s.slice(1)+le(n):l>1&&(s=s.charAt(0)+"."+s.slice(1)),s=s+(r.e<0?"e":"e+")+r.e):e<0?(s="0."+le(-e-1)+s,t&&(n=t-l)>0&&(s+=le(n))):e>=l?(s+=le(e+1-l),t&&(n=t-e-1)>0&&(s=s+"."+le(n))):((n=e+1)<l&&(s=s.slice(0,n)+"."+s.slice(n)),t&&(n=t-l)>0&&(e+1===l&&(s+="."),s+=le(n))),s}function Ze(r,i){var t=r[0];for(i*=H;t>=10;t/=10)i++;return i}function je(r,i,t){if(i>hr)throw $=!0,t&&(r.precision=t),Error(pn);return U(new r(Le),i,1,!0)}function ie(r,i,t){if(i>Ei)throw Error(pn);return U(new r(qe),i,t,!0)}function bn(r){var i=r.length-1,t=i*H+1;if(i=r[i],i){for(;i%10==0;i/=10)t--;for(i=r[0];i>=10;i/=10)t++}return t}function le(r){for(var i="";r--;)i+="0";return i}function wn(r,i,t,n){var e,s=new r(1),l=Math.ceil(n/H+4);for($=!1;;){if(t%2&&(s=s.times(i),cn(s.d,l)&&(e=!0)),t=Gt(t/2),t===0){t=s.d.length-1,e&&s.d[t]===0&&++s.d[t];break}i=i.times(i),cn(i.d,l)}return $=!0,s}function dn(r){return r.d[r.d.length-1]&1}function vn(r,i,t){for(var n,e,s=new r(i[0]),l=0;++l<i.length;){if(e=new r(i[l]),!e.s){s=e;break}n=s.cmp(e),(n===t||n===0&&s.s===t)&&(s=e)}return s}function Oi(r,i){var t,n,e,s,l,d,c,f=0,p=0,_=0,y=r.constructor,A=y.rounding,B=y.precision;if(!r.d||!r.d[0]||r.e>17)return new y(r.d?r.d[0]?r.s<0?0:1/0:1:r.s?r.s<0?0:r:NaN);for(i==null?($=!1,c=B):c=i,d=new y(.03125);r.e>-2;)r=r.times(d),_+=5;for(n=Math.log(Qt(2,_))/Math.LN10*2+5|0,c+=n,t=s=l=new y(1),y.precision=c;;){if(s=U(s.times(r),c,1),t=t.times(++p),d=l.plus(st(s,t,c,1)),Kt(d.d).slice(0,c)===Kt(l.d).slice(0,c)){for(e=_;e--;)l=U(l.times(l),c,1);if(i==null)if(f<3&&Ee(l.d,c-n,A,f))y.precision=c+=10,t=s=d=new y(1),p=0,f++;else return U(l,y.precision=B,A,$=!0);else return y.precision=B,l}l=d}}function ue(r,i){var t,n,e,s,l,d,c,f,p,_,y,A=1,B=10,S=r,O=S.d,x=S.constructor,R=x.rounding,T=x.precision;if(S.s<0||!O||!O[0]||!S.e&&O[0]==1&&O.length==1)return new x(O&&!O[0]?-1/0:S.s!=1?NaN:O?0:S);if(i==null?($=!1,p=T):p=i,x.precision=p+=B,t=Kt(O),n=t.charAt(0),Math.abs(s=S.e)<15e14){for(;n<7&&n!=1||n==1&&t.charAt(1)>3;)S=S.times(r),t=Kt(S.d),n=t.charAt(0),A++;s=S.e,n>1?(S=new x("0."+t),s++):S=new x(n+"."+t.slice(1))}else return f=je(x,p+2,T).times(s+""),S=ue(new x(n+"."+t.slice(1)),p-B).plus(f),x.precision=T,i==null?U(S,T,R,$=!0):S;for(_=S,c=l=S=st(S.minus(1),S.plus(1),p,1),y=U(S.times(S),p,1),e=3;;){if(l=U(l.times(y),p,1),f=c.plus(st(l,new x(e),p,1)),Kt(f.d).slice(0,p)===Kt(c.d).slice(0,p))if(c=c.times(2),s!==0&&(c=c.plus(je(x,p+2,T).times(s+""))),c=st(c,new x(A),p,1),i==null)if(Ee(c.d,p-B,R,d))x.precision=p+=B,f=l=S=st(_.minus(1),_.plus(1),p,1),y=U(S.times(S),p,1),e=d=1;else return U(c,x.precision=T,R,$=!0);else return x.precision=T,c;c=f,e+=2}}function yn(r){return String(r.s*r.s/0)}function Ne(r,i){var t,n,e;for((t=i.indexOf("."))>-1&&(i=i.replace(".","")),(n=i.search(/e/i))>0?(t<0&&(t=n),t+=+i.slice(n+1),i=i.substring(0,n)):t<0&&(t=i.length),n=0;i.charCodeAt(n)===48;n++);for(e=i.length;i.charCodeAt(e-1)===48;--e);if(i=i.slice(n,e),i){if(e-=n,r.e=t=t-n-1,r.d=[],n=(t+1)%H,t<0&&(n+=H),n<e){for(n&&r.d.push(+i.slice(0,n)),e-=H;n<e;)r.d.push(+i.slice(n,n+=H));i=i.slice(n),n=H-i.length}else n-=e;for(;n--;)i+="0";r.d.push(+i),$&&(r.e>r.constructor.maxE?(r.d=null,r.e=NaN):r.e<r.constructor.minE&&(r.e=0,r.d=[0]))}else r.e=0,r.d=[0];return r}function mr(r,i){var t,n,e,s,l,d,c,f,p;if(i.indexOf("_")>-1){if(i=i.replace(/(\d)_(?=\d)/g,"$1"),gn.test(i))return Ne(r,i)}else if(i==="Infinity"||i==="NaN")return+i||(r.s=NaN),r.e=NaN,r.d=null,r;if(dr.test(i))t=16,i=i.toLowerCase();else if(ur.test(i))t=2;else if(cr.test(i))t=8;else throw Error(de+i);for(s=i.search(/p/i),s>0?(c=+i.slice(s+1),i=i.substring(2,s)):i=i.slice(2),s=i.indexOf("."),l=s>=0,n=r.constructor,l&&(i=i.replace(".",""),d=i.length,s=d-s,e=wn(n,new n(t),s,s*2)),f=Fe(i,t,Xt),p=f.length-1,s=p;f[s]===0;--s)f.pop();return s<0?new n(r.s*0):(r.e=Ze(f,p),r.d=f,$=!1,l&&(r=st(r,e,d*4)),c&&(r=r.times(Math.abs(c)<54?Qt(2,c):Oe.pow(2,c))),$=!0,r)}function _r(r,i){var t,n=i.d.length;if(n<3)return i.isZero()?i:ve(r,2,i,i);t=1.4*Math.sqrt(n),t=t>16?16:t|0,i=i.times(1/Ue(5,t)),i=ve(r,2,i,i);for(var e,s=new r(5),l=new r(16),d=new r(20);t--;)e=i.times(i),i=i.times(s.plus(e.times(l.times(e).minus(d))));return i}function ve(r,i,t,n,e){var s,l,d,c,f=1,p=r.precision,_=Math.ceil(p/H);for($=!1,c=t.times(t),d=new r(n);;){if(l=st(d.times(c),new r(i++*i++),p,1),d=e?n.plus(l):n.minus(l),n=st(l.times(c),new r(i++*i++),p,1),l=d.plus(n),l.d[_]!==void 0){for(s=_;l.d[s]===d.d[s]&&s--;);if(s==-1)break}s=d,d=n,n=l,l=s,f++}return $=!0,l.d.length=_+1,l}function Ue(r,i){for(var t=r;--i;)t*=r;return t}function Mn(r,i){var t,n=i.s<0,e=ie(r,r.precision,1),s=e.times(.5);if(i=i.abs(),i.lte(s))return se=n?4:1,i;if(t=i.divToInt(e),t.isZero())se=n?3:2;else{if(i=i.minus(t.times(e)),i.lte(s))return se=dn(t)?n?2:3:n?4:1,i;se=dn(t)?n?1:4:n?3:2}return i.minus(e).abs()}function Ti(r,i,t,n){var e,s,l,d,c,f,p,_,y,A=r.constructor,B=t!==void 0;if(B?($t(t,1,ce),n===void 0?n=A.rounding:$t(n,0,8)):(t=A.precision,n=A.rounding),!r.isFinite())p=yn(r);else{for(p=ne(r),l=p.indexOf("."),B?(e=2,i==16?t=t*4-3:i==8&&(t=t*3-2)):e=i,l>=0&&(p=p.replace(".",""),y=new A(1),y.e=p.length-l,y.d=Fe(ne(y),10,e),y.e=y.d.length),_=Fe(p,10,e),s=c=_.length;_[--c]==0;)_.pop();if(!_[0])p=B?"0p+0":"0";else{if(l<0?s--:(r=new A(r),r.d=_,r.e=s,r=st(r,y,t,n,0,e),_=r.d,s=r.e,f=hn),l=_[t],d=e/2,f=f||_[t+1]!==void 0,f=n<4?(l!==void 0||f)&&(n===0||n===(r.s<0?3:2)):l>d||l===d&&(n===4||f||n===6&&_[t-1]&1||n===(r.s<0?8:7)),_.length=t,f)for(;++_[--t]>e-1;)_[t]=0,t||(++s,_.unshift(1));for(c=_.length;!_[c-1];--c);for(l=0,p="";l<c;l++)p+=Si.charAt(_[l]);if(B){if(c>1)if(i==16||i==8){for(l=i==16?4:3,--c;c%l;c++)p+="0";for(_=Fe(p,e,i),c=_.length;!_[c-1];--c);for(l=1,p="1.";l<c;l++)p+=Si.charAt(_[l])}else p=p.charAt(0)+"."+p.slice(1);p=p+(s<0?"p":"p+")+s}else if(s<0){for(;++s;)p="0"+p;p="0."+p}else if(++s>c)for(s-=c;s--;)p+="0";else s<c&&(p=p.slice(0,s)+"."+p.slice(s))}p=(i==16?"0x":i==2?"0b":i==8?"0o":"")+p}return r.s<0?"-"+p:p}function cn(r,i){if(r.length>i)return r.length=i,!0}function gr(r){return new this(r).abs()}function br(r){return new this(r).acos()}function wr(r){return new this(r).acosh()}function vr(r,i){return new this(r).plus(i)}function yr(r){return new this(r).asin()}function Mr(r){return new this(r).asinh()}function Pr(r){return new this(r).atan()}function Ar(r){return new this(r).atanh()}function Br(r,i){r=new this(r),i=new this(i);var t,n=this.precision,e=this.rounding,s=n+4;return!r.s||!i.s?t=new this(NaN):!r.d&&!i.d?(t=ie(this,s,1).times(i.s>0?.25:.75),t.s=r.s):!i.d||r.isZero()?(t=i.s<0?ie(this,n,e):new this(0),t.s=r.s):!r.d||i.isZero()?(t=ie(this,s,1).times(.5),t.s=r.s):i.s<0?(this.precision=s,this.rounding=1,t=this.atan(st(r,i,s,1)),i=ie(this,s,1),this.precision=n,this.rounding=e,t=r.s<0?t.minus(i):t.plus(i)):t=this.atan(st(r,i,s,1)),t}function Sr(r){return new this(r).cbrt()}function Ir(r){return U(r=new this(r),r.e+1,2)}function Er(r,i,t){return new this(r).clamp(i,t)}function Or(r){if(!r||typeof r!="object")throw Error(We+"Object expected");var i,t,n,e=r.defaults===!0,s=["precision",1,ce,"rounding",0,8,"toExpNeg",-we,0,"toExpPos",0,we,"maxE",0,we,"minE",-we,0,"modulo",0,9];for(i=0;i<s.length;i+=3)if(t=s[i],e&&(this[t]=Ii[t]),(n=r[t])!==void 0)if(Gt(n)===n&&n>=s[i+1]&&n<=s[i+2])this[t]=n;else throw Error(de+t+": "+n);if(t="crypto",e&&(this[t]=Ii[t]),(n=r[t])!==void 0)if(n===!0||n===!1||n===0||n===1)if(n)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[t]=!0;else throw Error(mn);else this[t]=!1;else throw Error(de+t+": "+n);return this}function Tr(r){return new this(r).cos()}function kr(r){return new this(r).cosh()}function Pn(r){var i,t,n;function e(s){var l,d,c,f=this;if(!(f instanceof e))return new e(s);if(f.constructor=e,fn(s)){f.s=s.s,$?!s.d||s.e>e.maxE?(f.e=NaN,f.d=null):s.e<e.minE?(f.e=0,f.d=[0]):(f.e=s.e,f.d=s.d.slice()):(f.e=s.e,f.d=s.d?s.d.slice():s.d);return}if(c=typeof s,c==="number"){if(s===0){f.s=1/s<0?-1:1,f.e=0,f.d=[0];return}if(s<0?(s=-s,f.s=-1):f.s=1,s===~~s&&s<1e7){for(l=0,d=s;d>=10;d/=10)l++;$?l>e.maxE?(f.e=NaN,f.d=null):l<e.minE?(f.e=0,f.d=[0]):(f.e=l,f.d=[s]):(f.e=l,f.d=[s]);return}if(s*0!==0){s||(f.s=NaN),f.e=NaN,f.d=null;return}return Ne(f,s.toString())}if(c==="string")return(d=s.charCodeAt(0))===45?(s=s.slice(1),f.s=-1):(d===43&&(s=s.slice(1)),f.s=1),gn.test(s)?Ne(f,s):mr(f,s);if(c==="bigint")return s<0?(s=-s,f.s=-1):f.s=1,Ne(f,s.toString());throw Error(de+s)}if(e.prototype=I,e.ROUND_UP=0,e.ROUND_DOWN=1,e.ROUND_CEIL=2,e.ROUND_FLOOR=3,e.ROUND_HALF_UP=4,e.ROUND_HALF_DOWN=5,e.ROUND_HALF_EVEN=6,e.ROUND_HALF_CEIL=7,e.ROUND_HALF_FLOOR=8,e.EUCLID=9,e.config=e.set=Or,e.clone=Pn,e.isDecimal=fn,e.abs=gr,e.acos=br,e.acosh=wr,e.add=vr,e.asin=yr,e.asinh=Mr,e.atan=Pr,e.atanh=Ar,e.atan2=Br,e.cbrt=Sr,e.ceil=Ir,e.clamp=Er,e.cos=Tr,e.cosh=kr,e.div=xr,e.exp=Rr,e.floor=Cr,e.hypot=Dr,e.ln=Fr,e.log=Nr,e.log10=qr,e.log2=Lr,e.max=jr,e.min=Wr,e.mod=Zr,e.mul=Ur,e.pow=Qr,e.random=Hr,e.round=Kr,e.sign=Gr,e.sin=$r,e.sinh=zr,e.sqrt=Vr,e.sub=Xr,e.sum=Yr,e.tan=Jr,e.tanh=to,e.trunc=eo,r===void 0&&(r={}),r&&r.defaults!==!0)for(n=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],i=0;i<n.length;)r.hasOwnProperty(t=n[i++])||(r[t]=this[t]);return e.config(r),e}function xr(r,i){return new this(r).div(i)}function Rr(r){return new this(r).exp()}function Cr(r){return U(r=new this(r),r.e+1,3)}function Dr(){var r,i,t=new this(0);for($=!1,r=0;r<arguments.length;)if(i=new this(arguments[r++]),i.d)t.d&&(t=t.plus(i.times(i)));else{if(i.s)return $=!0,new this(1/0);t=i}return $=!0,t.sqrt()}function fn(r){return r instanceof Oe||r&&r.toStringTag===_n||!1}function Fr(r){return new this(r).ln()}function Nr(r,i){return new this(r).log(i)}function Lr(r){return new this(r).log(2)}function qr(r){return new this(r).log(10)}function jr(){return vn(this,arguments,-1)}function Wr(){return vn(this,arguments,1)}function Zr(r,i){return new this(r).mod(i)}function Ur(r,i){return new this(r).mul(i)}function Qr(r,i){return new this(r).pow(i)}function Hr(r){var i,t,n,e,s=0,l=new this(1),d=[];if(r===void 0?r=this.precision:$t(r,1,ce),n=Math.ceil(r/H),this.crypto)if(crypto.getRandomValues)for(i=crypto.getRandomValues(new Uint32Array(n));s<n;)e=i[s],e>=429e7?i[s]=crypto.getRandomValues(new Uint32Array(1))[0]:d[s++]=e%1e7;else if(crypto.randomBytes){for(i=crypto.randomBytes(n*=4);s<n;)e=i[s]+(i[s+1]<<8)+(i[s+2]<<16)+((i[s+3]&127)<<24),e>=214e7?crypto.randomBytes(4).copy(i,s):(d.push(e%1e7),s+=4);s=n/4}else throw Error(mn);else for(;s<n;)d[s++]=Math.random()*1e7|0;for(n=d[--s],r%=H,n&&r&&(e=Qt(10,H-r),d[s]=(n/e|0)*e);d[s]===0;s--)d.pop();if(s<0)t=0,d=[0];else{for(t=-1;d[0]===0;t-=H)d.shift();for(n=1,e=d[0];e>=10;e/=10)n++;n<H&&(t-=H-n)}return l.e=t,l.d=d,l}function Kr(r){return U(r=new this(r),r.e+1,this.rounding)}function Gr(r){return r=new this(r),r.d?r.d[0]?r.s:0*r.s:r.s||NaN}function $r(r){return new this(r).sin()}function zr(r){return new this(r).sinh()}function Vr(r){return new this(r).sqrt()}function Xr(r,i){return new this(r).sub(i)}function Yr(){var r=0,i=arguments,t=new this(i[r]);for($=!1;t.s&&++r<i.length;)t=t.plus(i[r]);return $=!0,U(t,this.precision,this.rounding)}function Jr(r){return new this(r).tan()}function to(r){return new this(r).tanh()}function eo(r){return U(r=new this(r),r.e+1,1)}I[Symbol.for("nodejs.util.inspect.custom")]=I.toString;I[Symbol.toStringTag]="Decimal";var Oe=I.constructor=Pn(Ii);Le=new Oe(Le);qe=new Oe(qe);var Z=Oe;var Qe=(n=>(n[n.Spot=0]="Spot",n[n.Curve=1]="Curve",n[n.BidAsk=2]="BidAsk",n))(Qe||{});import{d as G,DETAILS_KEYS as He,MathUtil as io}from"@cetusprotocol/common-sdk";var fe=1e3,Ro=-443636,Co=443636,he=1e4,Te=2e3,ye=200,Me=443636n,ki=1e8,An=1e9,Yt=1e4,Do=604800,Fo=1747627200;var ot=Ai(De(),1);var no=new ot.default(524288),q=64,xi=new ot.default(1).shln(q),Bn=new ot.default(2).pow(new ot.default(128)).sub(new ot.default(1)),F=class r{static splitBinLiquidityInfo(i,t,n){let e=r.getPositionCount(t,n);if(e<=1)return[i];let s=[],l=t;for(let d=0;d<e;d++){let c=Math.min(l+1e3-2,n),f=i.bins.filter(p=>p.bin_id>=l&&p.bin_id<=c);s.push({bins:f,amount_a:f.reduce((p,_)=>G(p).plus(_.amount_a),G(0)).toFixed(0),amount_b:f.reduce((p,_)=>G(p).plus(_.amount_b),G(0)).toFixed(0)}),l=c+1}return s}static processBinsByRate(i,t){let n=[],e=G(0),s=G(0);return i.forEach(l=>{let{amount_a:d,amount_b:c,liquidity:f="0"}=l,p=G(t).mul(f).toFixed(0),_=G(d).mul(t),y=G(c).mul(t);G(_).lt(1)&&G(_).gt(0)&&(_=G(1)),G(y).lt(1)&&G(y).gt(0)&&(y=G(1)),e=G(e).plus(_),s=G(s).plus(y),n.push({bin_id:l.bin_id,amount_a:_.toFixed(0),amount_b:y.toFixed(0),price_per_lamport:l.price_per_lamport,liquidity:p})}),{bins:n,amount_a:e.toFixed(0),amount_b:s.toFixed(0)}}static calculateOutByShare(i,t){let{amount_a:n,amount_b:e,liquidity:s="0"}=i;if(s==="0")return{amount_a:"0",amount_b:"0"};if(G(t).gte(G(s)))return{amount_a:n,amount_b:e};let l=G(t).div(s).mul(n).toFixed(0,Z.ROUND_FLOOR),d=G(t).div(s).mul(e).toFixed(0,Z.ROUND_FLOOR);return{amount_a:l,amount_b:d}}static getPositionCount(i,t){let e=G(t).sub(i).add(1).div(1e3);return Number(e.toFixed(0,Z.ROUND_UP))}static getLiquidity(i,t,n){return G(n).mul(i).add(G(t).mul(G(2).pow(q))).toFixed(0)}static getAmountAFromLiquidity(i,t){return G(i).div(G(t)).toFixed(0)}static getAmountBFromLiquidity(i){return G(i).div(G(2).pow(q)).toFixed(0)}static getAmountsFromLiquidity(i,t,n,e){if(G(e).isZero()&&V("LiquiditySupplyIsZero","Liquidity supply is zero"),G(n).gt(G(e))&&V("InvalidDeltaLiquidity","Invalid delta liquidity"),G(n).isZero())return["0","0"];let s;G(i).isZero()?s="0":s=G(i).mul(G(n)).div(G(e)).toFixed(0,Z.ROUND_FLOOR);let l;return G(t).isZero()?l="0":l=G(t).mul(G(n)).div(G(e)).toFixed(0,Z.ROUND_FLOOR),[s,l]}static getPriceFromBinId(i,t,n,e){let s=r.getPricePerLamportFromBinId(i,t);return r.getPriceFromLamport(n,e,s).toString()}static getPricePerLamportFromBinId(i,t){let n=new Z(t).div(new Z(1e4));return new Z(1).add(new Z(n)).pow(new Z(i)).toString()}static getBinIdFromPrice(i,t,n,e,s){let l=r.getPricePerLamport(e,s,i);return r.getBinIdFromLamportPrice(l,t,n)}static getBinIdFromLamportPrice(i,t,n){let e=new Z(t).div(new Z(1e4)),s=new Z(i).log().dividedBy(new Z(1).add(e).log());return(n?s.floor():s.ceil()).toNumber()}static getPricePerLamport(i,t,n){return new Z(n).mul(new Z(10**(t-i))).toString()}static getPriceFromLamport(i,t,n){return new Z(n).div(new Z(10**(t-i))).toString()}static getReversePrice(i){return new Z(1).div(i).toString()}static getQPriceFromId(i,t){let n=new ot.default(t).shln(q).div(new ot.default(1e4)),e=xi.add(n);return r.pow(e,new ot.default(i)).toString()}static getPricePerLamportFromQPrice(i){return io.fromX64(new ot.default(i)).toString()}static pow(i,t){let n=t.isNeg();if(t.isZero())return xi;if(t=n?t.abs():t,t.gt(no))return new ot.default(0);let e=i,s=xi;return e.gte(s)&&(e=Bn.div(e),n=!n),t.and(new ot.default(1)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(2)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(4)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(8)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(16)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(32)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(64)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(128)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(256)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(512)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(1024)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(2048)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(4096)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(8192)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(16384)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(32768)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(65536)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(131072)).isZero()||(s=s.mul(e).shrn(q)),e=e.mul(e).shrn(q),t.and(new ot.default(262144)).isZero()||(s=s.mul(e).shrn(q)),s.isZero()?new ot.default(0):(n&&(s=Bn.div(s)),s)}static binScore(i){let t=BigInt(i)+Me;return(t<0n||t>Me*2n)&&V("InvalidBinId",new Error("Invalid bin ID"),{[He.METHOD_NAME]:"binScore",[He.REQUEST_PARAMS]:{binId:i}}),t.toString()}static scoreToBinId(i){let t=BigInt(i)-Me;return(t<-Me||t>Me)&&V("InvalidBinId",new Error("Invalid score"),{[He.METHOD_NAME]:"scoreToBinId",[He.REQUEST_PARAMS]:{score:i}}),Number(t)}static resolveBinPosition(i){let t=BigInt(i),n=t>>4n,e=Number(t&0xfn);return[n.toString(),e]}static findMinMaxBinId(i){let t=1+i/1e4,e=new Z("18446744073709551615").log(10).div(new Z(t).log(10)).floor(),s=e.neg(),l=e,d=G(1),c=G("340282366920938463463374607431768211455");for(;;){let f=G(r.getQPriceFromId(s.toNumber(),i));if(f.gt(d)&&!f.isZero())break;s=s.add(1)}for(;;){let f=G(r.getQPriceFromId(l.toNumber(),i));if(f.lt(c)&&!f.isZero())break;l=l.sub(1)}return{minBinId:s.toNumber(),maxBinId:l.toNumber()}}static getBinShift(i,t,n){let e=r.getPricePerLamportFromBinId(i,t),s=G(e).mul(1+n).toString(),l=r.getBinIdFromLamportPrice(s,t,!0),d=G(l).sub(i).abs().toFixed(0,Z.ROUND_UP);return console.log("getBinShift Options:",{active_id:i,bin_shift:d}),Number(d)}};import{bcs as K}from"@mysten/sui/bcs";import{blake2b as oo}from"blakejs";function On(r){try{let i=r.parsedJson;return{id:i.pool_id,bin_step:Number(i.bin_step),coin_type_a:re(i.coin_type_a,!1),coin_type_b:re(i.coin_type_b,!1)}}catch(i){return V("ParseError",i,{[pe.METHOD_NAME]:"parseDlmmBasePool",[pe.REQUEST_PARAMS]:r})}}function Ke(r){try{let i=r.json,t=r.type,n=Ci(t),e={bin_step:i.bin_manager.bin_step,bin_manager_handle:i.bin_manager.bins.id,size:i.bin_manager.bins.size},s={bin_step:i.position_manager.bin_step,position_index:i.position_manager.position_index,position_handle:i.position_manager.positions.id,size:i.position_manager.positions.size},l=i.reward_manager,d=l.rewards.map(_=>{let y=_.current_emission_rate,A=ro.fromX64(new En.default(y)),B=Math.floor(A.toNumber()*60*60*24).toString();return{reward_coin:re(_.reward_coin,!1),emissions_per_second:A.toString(),emissions_per_day:B,period_emission_rates:{id:_.period_emission_rates.id,size:_.period_emission_rates.size}}}),c={is_public:l.is_public,emergency_reward_pause:l.emergency_reward_pause,vault:{id:l.vault.id,size:l.vault.size},rewards:d,last_updated_time:l.last_updated_time},f={volatility_accumulator:i.v_parameters.volatility_accumulator,volatility_reference:i.v_parameters.volatility_reference,index_reference:Ae(BigInt(i.v_parameters.index_reference.bits)),last_update_timestamp:i.v_parameters.last_update_timestamp,bin_step_config:i.v_parameters.bin_step_config},p={id:i.id,bin_step:Number(i.bin_step),coin_type_a:re(n.type_arguments[0],!1),coin_type_b:re(n.type_arguments[1],!1),pool_type:t,index:Number(i.index),bin_manager:e,variable_parameters:f,active_id:Ae(BigInt(i.active_id.bits)),permissions:i.permissions,balance_a:i.balance_a,balance_b:i.balance_b,base_fee_rate:i.base_fee_rate,protocol_fee_a:i.protocol_fee_a,protocol_fee_b:i.protocol_fee_b,url:i.url,reward_manager:c,position_manager:s};return p.bin_step=p.bin_manager.bin_step,p}catch(i){return console.log("\u{1F680} ~ parseDlmmPool ~ error:",i),V("ParseError",i,{[pe.METHOD_NAME]:"parseDlmmPool",[pe.REQUEST_PARAMS]:r})}}function Di(r){let i=r.json,t=r.type,n=Ci(t);return{id:i.id,name:i.name,ref_fee_rate:ke(i.ref_fee_rate).div(1e4).toNumber(),start_time:Number(i.start_time),end_time:Number(i.end_time),balances:{id:i.balances.id,size:i.balances.size},type:n.full_address}}function Fi(r){try{let i=r.json;return{uri:i.uri,index:i.index,id:i.id,name:i.name,pool_id:i.pool_id,lower_bin_id:Ae(BigInt(i.lower_bin_id.bits)),upper_bin_id:Ae(BigInt(i.upper_bin_id.bits)),liquidity_shares:i.liquidity_shares,description:i.description,coin_type_a:re(i.coin_type_a,!1),coin_type_b:re(i.coin_type_b,!1)}}catch(i){return console.log("\u{1F680} ~ parseDlmmPosition ~ error:",i),V("ParseError",i,{[pe.METHOD_NAME]:"parseDlmmPosition",[pe.REQUEST_PARAMS]:r})}}function gs(r,i,t,n){let e=r.map((d,c)=>{let f=t+c,p=F.getPricePerLamportFromBinId(f,i);if(f===n.bin_id){let{amount_a:A,amount_b:B}=F.calculateOutByShare(n,d);return{bin_id:f,amount_a:A,amount_b:B,liquidity:d,price_per_lamport:p}}if(f<n.bin_id){let A=F.getAmountBFromLiquidity(d);return{bin_id:f,amount_a:"0",amount_b:A,liquidity:d,price_per_lamport:p}}let _=F.getQPriceFromId(f,i),y=F.getAmountAFromLiquidity(d,_);return{bin_id:f,amount_a:y,amount_b:"0",liquidity:d,price_per_lamport:p}}),s=e.reduce((d,c)=>d.add(new Z(c.amount_a)),new Z(0)).toFixed(0),l=e.reduce((d,c)=>d.add(new Z(c.amount_b)),new Z(0)).toFixed(0);return{bins:e,amount_a:s,amount_b:l}}function Tn(r){try{return K.vector(K.struct("BinAmount",{id:K.struct("I32",{bits:K.u32()}),amount_a:K.u64(),amount_b:K.u64(),price:K.u128(),liquidity_supply:K.u128(),rewards_growth_global:K.vector(K.u128()),fee_a_growth_global:K.u128(),fee_b_growth_global:K.u128()})).parse(r).map(n=>({bin_id:Ae(BigInt(n.id.bits)),amount_a:n.amount_a,amount_b:n.amount_b,liquidity:n.liquidity_supply,price_per_lamport:F.getPricePerLamportFromQPrice(n.price)}))}catch(i){return console.log("\u{1F680} ~ parseBinInfo ~ error:",i),[]}}function Ni(r){try{return{bin_id:Ae(BigInt(typeof r.id=="number"?r.id:r.id.bits)),amount_a:r.amount_a,amount_b:r.amount_b,liquidity:r.liquidity_share,price_per_lamport:F.getPricePerLamportFromQPrice(r.price)}}catch(i){return V("ParseError",i,{[pe.METHOD_NAME]:"parseBinInfo",[pe.REQUEST_PARAMS]:{fields:r}})}}function kn(r){let i={},t=r?.filter(n=>n.eventType.includes("pool::CollectFeeEvent"));for(let n=0;n<t.length;n+=1){let{bcs:e}=t[n],s=K.struct("CollectFeeEvent",{pool:K.Address,position:K.Address,fee_a:K.u64(),fee_b:K.u64()}).parse(e),l={position_id:s.position,fee_owned_a:s.fee_a,fee_owned_b:s.fee_b};i[s.position]=l}return i}function xn(r){let i={},t=r?.filter(n=>n.eventType.includes("pool::CollectRewardEvent"));for(let n=0;n<t.length;n+=1){let{bcs:e}=t[n],s=K.struct("CollectRewardEvent",{pool:K.Address,position:K.Address,reward:Ri,amount:K.u64()}).parse(e),l={coin_type:re(s.reward.name,!1),reward_owned:s.amount},d=i[s.position];d?d.rewards.push(l):d={position_id:s.position,rewards:[l]},i[s.position]=d}return i}function Rn(r,i){let t=r.events?.filter(n=>n.eventType.includes("pool::SwapEvent"));for(let n=0;n<t.length;n+=1){let{json:e}=t[n],{partner:s,pool:l,amount_in:d,amount_out:c,fee:f,ref_fee:p,bin_swaps:_,from:y,target:A}=e,B=_.map(O=>({bin_id:O.bin_id.bits,in_amount:O.amount_in,out_amount:O.amount_out,fee:O.fee,var_fee_rate:O.var_fee_rate}));return{pool_id:l,a2b:i,in_amount:d,out_amount:c,ref_fee_amount:p,fee_amount:f,bin_swaps:B,partner:s,from_coin_type:re(y,!1),to_coin_type:re(A,!1)}}}function bs(r){switch(r){case 0:return 0;case 1:return 1;case 2:return 2}}var Li=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","ClosePositionEvent"];function Cn(r,i,t,n,e){let s=e?.length?e:Li,l=[];return r.effects.events.nodes?.forEach((c,f)=>{let p=c.timestamp,{json:_,type:y}=c.contents,{name:A,address:B}=Ci(y.repr);if(s.includes(A)&&t===B&&n===_.pool){let S={tx:r.digest,sender:c.sender.address,type:y.repr,block_time:new Date(p).getTime().toString(),index:`${i}_${f}`,parsed_json:_};l.push(S)}}),l}function ws(r,i,t){let n=[],e=0,s=new Date(r*1e3).getTime(),l=new Date().getTime();for(;;){let d=s+e*t;if(d>=l&&n.push(d),e>=i)break;e+=1}return n}function vs(r,i,t,n){let e=[];for(let s=i;s<=t;s+=n){let l=r.findLast(d=>ke(s).gte(d.time));l?e.push({time:s.toString(),emissions_per_second:l.emissions_per_second,emissions_per_day:l.emissions_per_day,visualized_time:new Date(s*1e3).toLocaleString()}):e.push({emissions_per_day:"0",time:s.toString(),emissions_per_second:"0",visualized_time:new Date(s*1e3).toLocaleString()})}return e}function ys(r){if(r.length===0)return;let i=new Date().getTime()/1e3,t=r.findLast(n=>ke(i).gte(n.time));return t||r[r.length-1]}function oe(r,i,t=!1){let n=r.mul(i),e=n.gt(0)&&n.lt(1);if(t&&e)throw new be(`Multiplication ${n} is less than 1`,"AmountTooSmall");return e?ke(0):n.floor()}function xe(r,i=!1){let t=r.gt(0)&&r.lt(1);if(i&&t)throw new be(`Multiplication ${r.toString()} is less than 1`,"AmountTooSmall");return t?ke(0):r.floor()}function Dn(r){switch(r){case 0:return"spot";case 1:return"curve";case 2:return"bid_ask"}}function Fn(r,i,t,n){let e=Buffer.from(r,"utf8"),s=Buffer.from(i,"utf8"),l=s.length,d=0;for(;d<l;){let y=s[d];e=Buffer.concat([e,Buffer.from([y])]),d++}let c=K.u16().serialize(t).toBytes(),f=K.u16().serialize(n).toBytes(),p=Buffer.concat([e,c,f]),_=oo(p,void 0,32);return`0x${Buffer.from(_).toString("hex")}`}var so=K.struct("PoolSimpleInfo",{pool_id:K.Address,pool_key:K.Address,coin_type_a:Ri,coin_type_b:Ri,bin_step:K.u16(),base_factor:K.u16()}),In=K.bytes(32),Nn=K.struct("Node<ID, PoolSimpleInfo>",{prev:K.option(In),next:K.option(In),value:so}),ao=K.struct("Bin",{id:K.u32(),amount_a:K.u64(),amount_b:K.u64(),price:K.u128(),liquidity_share:K.u128(),rewards_growth_global:K.vector(K.u128()),fee_a_growth_global:K.u128(),fee_b_growth_global:K.u128()}),lo=K.struct("BinGroup",{idx:K.u32(),used_bins_mask:K.u16(),bins:K.vector(ao)}),uo=K.struct("BinGroupRef",{pool_id:K.Address,group:lo}),Ln=K.struct("SkipListNode<BinGroupRef>",{score:K.u64(),prev:K.vector(Sn),next:Sn,value:uo});import{d as E}from"@cetusprotocol/common-sdk";var Jt=class r{static toWeight(i){console.log("\u{1F680} ~ WeightUtils ~ toWeight ~ options:",i);let{strategy_type:t,active_id:n,bin_step:e,lower_bin_id:s,upper_bin_id:l,total_amount_a:d,total_amount_b:c,active_bin_of_pool:f}=i,p=n<s||n>l,_=F.getQPriceFromId(n,e),y=E(0),A=E(0),B=E(200);if(t===2?B=E(200):t===1?B=E(2e3):t===0&&(B=E(1)),!p&&f){let it=this.calculateActiveWeights(f.amount_a,f.amount_b,_,B);y=it.active_weight_a,A=it.active_weight_b}n===s&&E(c).isZero()&&(y=E(B).mul(E(2).pow(q*2)).div(E(_)).floor(),A=E(0)),n===l&&E(d).isZero()&&(A=E(B).mul(E(2).pow(q)).floor(),y=E(0)),n>s&&n<l&&(E(d).isZero()&&(A=E(B).mul(E(2).pow(q)).floor(),y=E(0)),E(c).isZero()&&(y=E(B).mul(E(2).pow(q*2)).div(E(_)).floor(),A=E(0)));let S=p?E(0):y,O=p?E(0):A,x=E(2e3).sub(E(200)).floor(),R=n>l?l:n,T=n<s?s:n,k=n>s?R===s?E(0):x.div(E(R-s)).floor():E(0),j=l>n?T===l?E(0):x.div(E(l-T)).floor():E(0),N=s,Y=[],et=[];for(;N<=l;){let it=E(0);if(N<n){let at=R-N;t===0?it=E(1):t===2?it=E(B).add(k.mul(at)).floor():t===1&&(it=E(B).sub(k.mul(at)).floor())}else if(N>n){let at=N-T;t===0?it=E(1):t===2?it=E(B).add(j.mul(at)).floor():t===1&&(it=E(B).sub(j.mul(at)).floor())}else it=B;if(Y.push(it),N<n)O=O.add(it.mul(E(2).pow(q))).floor(),et.push(E(0));else if(N>n){let at=it.mul(E(2).pow(q*2)).div(F.getQPriceFromId(N,e)).floor();et.push(at),S=S.add(at).floor()}else et.push(E(0));N+=1}return{...i,total_weight_a:S,total_weight_b:O,active_weight_a:y,active_weight_b:A,weights:Y,weight_per_prices:et}}static toWeightSpotBalanced(i,t){let n=[];for(let e=i;e<=t;e++)n.push({bin_id:e,weight:1});return n}static toWeightDescendingOrder(i,t){let n=[];for(let e=i;e<=t;e++)n.push({bin_id:e,weight:t-e+1});return n}static toWeightAscendingOrder(i,t){let n=[];for(let e=i;e<=t;e++)n.push({bin_id:e,weight:e-i+1});return n}static toWeightCurve(i,t,n){if(n<i)return r.toWeightDescendingOrder(i,t);if(n>t)return r.toWeightAscendingOrder(i,t);let e=2e3,l=e-200,d=n>i?Math.floor(l/(n-i)):0,c=t>n?Math.floor(l/(t-n)):0,f=[];for(let p=i;p<=t;p++)p<n?f.push({bin_id:p,weight:e-(n-p)*d}):p>n?f.push({bin_id:p,weight:e-(p-n)*c}):f.push({bin_id:p,weight:e});return f}static toWeightBidAsk(i,t,n){if(n>t)return r.toWeightDescendingOrder(i,t);if(n<i)return r.toWeightAscendingOrder(i,t);let e=2e3,s=200,l=e-s,d=n>i?Math.floor(l/(n-i)):0,c=t>n?Math.floor(l/(t-n)):0,f=[];for(let p=i;p<=t;p++)p<n?f.push({bin_id:p,weight:s+(n-p)*d}):p>n?f.push({bin_id:p,weight:s+(p-n)*c}):f.push({bin_id:p,weight:s});return f}static toAmountBidSide(i,t,n,e,s=!1){let l=e.filter(c=>c.bin_id<=i).reduce(function(c,f){return s?f.bin_id>i?c:c.add(f.weight):f.bin_id>=i?c:c.add(f.weight)},E(0));if(l.cmp(E(0))!=1)throw Error("Invalid parameters");return{bins:e.map(c=>{let f=F.getPricePerLamportFromBinId(c.bin_id,n);if(!(c.bin_id<=i)||c.bin_id>=i&&!s)return{bin_id:c.bin_id,amount_a:"0",amount_b:"0",price_per_lamport:f,liquidity:"0"};{let _=E(c.weight).div(l),y=oe(E(t),_).toString(),A="0",B=F.getQPriceFromId(c.bin_id,n),S=F.getLiquidity(A,y,B);return{bin_id:c.bin_id,amount_b:y,amount_a:A,price_per_lamport:f,liquidity:S}}}),amount_a:"0",amount_b:t}}static toAmountAskSide(i,t,n,e,s=!1){let l=e.filter(c=>c.bin_id>=i).reduce(function(c,f){if(f.bin_id<=i&&!s)return c;{let p=F.getPricePerLamportFromBinId(f.bin_id,t),_=new Z(f.weight).div(p);return c.add(_)}},new Z(0));if(l.cmp(new Z(0))!=1)throw Error("Invalid parameters");return{bins:e.map(c=>{let f=F.getPricePerLamportFromBinId(c.bin_id,t);if(!(c.bin_id>=i)||c.bin_id<=i&&!s)return{bin_id:c.bin_id,amount_a:"0",amount_b:"0",price_per_lamport:f,liquidity:"0"};{let y=new Z(c.weight).div(f).div(l),A=oe(E(n),y).toString(),B="0",S=F.getQPriceFromId(c.bin_id,t),O=F.getLiquidity(A,B,S);return{bin_id:c.bin_id,amount_a:A,amount_b:B,price_per_lamport:f,liquidity:O}}}),amount_a:n,amount_b:"0"}}static toAmountBothSide(i,t,n,e,s,l,d){let c=!E(n).isZero()&&E(e).isZero(),f=E(n).isZero()&&!E(e).isZero();if(i>d[d.length-1].bin_id)return r.toAmountBidSide(i,e,t,d);if(f&&i!==d[d.length-1].bin_id)return r.toAmountBidSide(i,e,t,d,!0);if(i<d[0].bin_id)return r.toAmountAskSide(i,t,n,d);if(c&&i!==d[0].bin_id)return r.toAmountAskSide(i,t,n,d,!0);let p=d.filter(_=>_.bin_id===i);if(p.length===1){let{totalWeightA:_,totalWeightB:y,activeWeightA:A,activeWeightB:B}=r.calculateTotalWeights(t,d,i,p[0],s,l,c?"a":f?"b":void 0),S=new Z(n.toString()).div(_),O=new Z(e.toString()).div(y),x=d.map(k=>{let j=F.getPricePerLamportFromBinId(k.bin_id,t);if(k.bin_id<i||k.bin_id===i&&f){let rt=oe(O,new Z(k.weight)),v="0",o=F.getQPriceFromId(k.bin_id,t),u=F.getLiquidity(v,rt.toString(),o);return{bin_id:k.bin_id,amount_a:"0",amount_b:rt.toString(),price_per_lamport:j,liquidity:u}}if(k.bin_id>i||k.bin_id===i&&c){let rt=new Z(k.weight).div(j),v=oe(S,new Z(rt)),o="0",u=F.getQPriceFromId(k.bin_id,t),h=F.getLiquidity(v.toString(),o,u);return{bin_id:k.bin_id,amount_a:v.toString(),amount_b:"0",price_per_lamport:j,liquidity:h}}let N=oe(S,A),Y=oe(O,B),et=N.toString(),it=Y.toString(),at=F.getQPriceFromId(k.bin_id,t),X=F.getLiquidity(et,it,at);return{bin_id:k.bin_id,amount_a:et,amount_b:it,price_per_lamport:j,liquidity:X}}),R=x.reduce((k,j)=>E(k).add(E(j.amount_a)),E(0)).toString(),T=x.reduce((k,j)=>E(k).add(E(j.amount_b)),E(0)).toString();return{bins:x,amount_a:R,amount_b:T}}else{let{totalWeightA:_,totalWeightB:y}=r.calculateTotalWeights(t,d,i),A=new Z(n.toString()).div(_),B=new Z(e.toString()).div(y),S=d.map(R=>{let T=F.getPricePerLamportFromBinId(R.bin_id,t);if(R.bin_id<i){let k=oe(B,new Z(R.weight));return{bin_id:R.bin_id,amount_a:"0",amount_b:k.toString(),price_per_lamport:T}}else{let k=new Z(R.weight).div(T),j=oe(A,k);return{bin_id:R.bin_id,amount_a:j.toString(),amount_b:"0",price_per_lamport:T}}}),O=S.reduce((R,T)=>E(R).add(E(T.amount_a)),E(0)).toString(),x=S.reduce((R,T)=>E(R).add(E(T.amount_b)),E(0)).toString();return{bins:S,amount_a:O,amount_b:x}}}static autoFillCoinByWeight(i,t,n,e,s,l,d){if(i>d[d.length-1].bin_id)return r.toAmountBidSide(i,n,t,d);if(i<d[0].bin_id)return r.toAmountAskSide(i,t,n,d);let c=d.filter(A=>A.bin_id===i),{totalWeightA:f,totalWeightB:p}=r.calculateTotalWeights(t,d,i,c.length===1?c[0]:void 0,c.length===1?s:void 0,c.length===1?l:void 0),_=E(0);e?_=f.isZero()?new Z(0):new Z(n).div(f):_=p.isZero()?new Z(0):new Z(n).div(p);let y=oe(_,e?p:f).toString();return r.toAmountBothSide(i,t,e?n:y,e?y:n,s,l,d)}static calculateActiveWeights(i,t,n,e){let s=E(n),l=E(i),d=E(t),c=E(0),f=E(0);if(l.isZero()&&d.isZero())c=E(e).mul(E(2).pow(q*2)).div(s.mul(2)).floor(),f=E(e).mul(E(2).pow(q)).div(2).floor();else{if(l.isZero())c=E(0);else{let p=d.mul(E(2).pow(q)).div(l);c=E(e).mul(E(2).pow(q*2)).div(s.add(p)).floor()}if(d.isZero())f=E(0);else{let p=E(2).pow(q).add(s.mul(l).div(d)).floor();f=E(e).mul(E(2).pow(q*2)).div(p).floor()}}return{active_weight_a:c,active_weight_b:f}}static calculateTotalWeights(i,t,n,e,s,l,d){let c=E(F.getPricePerLamportFromBinId(n,i)),f=E(0),p=E(0);if(s&&l&&e&&!d)if(E(s).isZero()&&E(l).isZero())f=new Z(e.weight).div(c.mul(new Z(2))),p=new Z(e.weight).div(new Z(2));else{let A=new Z(s.toString()),B=new Z(l.toString());E(s).isZero()||(f=new Z(e.weight).div(c.add(B.div(A)))),E(l).isZero()||(p=new Z(e.weight).div(new Z(1).add(c.mul(A).div(B))))}let _=f,y=p;return t.forEach(A=>{if((A.bin_id<n||d==="b")&&(y=y.add(new Z(A.weight))),A.bin_id>n||d==="a"){let B=F.getPricePerLamportFromBinId(A.bin_id,i),S=new Z(A.weight).div(B);_=_.add(S)}}),{totalWeightA:_,totalWeightB:y,activeWeightA:f,activeWeightB:p}}};import{d as zt}from"@cetusprotocol/common-sdk";var Re=class{static toAmountsByWeights(i){let{total_weight_a:t,total_weight_b:n,weights:e,weight_per_prices:s,total_amount_a:l,total_amount_b:d,active_weight_a:c,active_weight_b:f,lower_bin_id:p,upper_bin_id:_,active_id:y,bin_step:A}=i,B=n.isZero()?zt(0):zt(d).mul(zt(2).pow(q*2)).div(n).floor(),S=t.isZero()?zt(0):zt(l).mul(zt(2).pow(q*2)).div(t).floor(),O=xe(S.mul(c).div(zt(2).pow(q*2))),x=xe(B.mul(f).div(zt(2).pow(q*2))),R=_-p+1,T=p,k=0,j=[];for(;k<R;){let Y=zt(0),et=zt(0),it=F.getQPriceFromId(T,A);T<y?et=xe(B.mul(e[k]).div(zt(2).pow(q))):T>y?Y=xe(S.mul(s[k]).div(zt(2).pow(q*2))):(Y=O,et=x);let at=F.getLiquidity(Y.toString(),et.toString(),it);j.push({bin_id:T,amount_a:Y.toString(),amount_b:et.toString(),price_per_lamport:F.getPricePerLamportFromBinId(T,A),liquidity:at}),T+=1,k+=1}return{bins:j,amount_a:l.toString(),amount_b:d.toString()}}static toAmountsBothSideByStrategy(i,t,n,e,s,l,d,c){let f=Jt.toWeight({strategy_type:d,active_id:i,bin_step:t,lower_bin_id:n,upper_bin_id:e,total_amount_a:s,total_amount_b:l,active_bin_of_pool:c});return this.toAmountsByWeights(f)}static autoFillCoinByStrategy(i,t,n,e,s,l,d,c){switch(d){case 0:{let f=Jt.toWeightSpotBalanced(s,l);return Jt.autoFillCoinByWeight(i,t,n,e,c?.amount_a||"0",c?.amount_b||"0",f)}case 1:{let f=Jt.toWeightCurve(s,l,i);return Jt.autoFillCoinByWeight(i,t,n,e,c?.amount_a||"0",c?.amount_b||"0",f)}case 2:{let f=Jt.toWeightBidAsk(s,l,i);return Jt.autoFillCoinByWeight(i,t,n,e,c?.amount_a||"0",c?.amount_b||"0",f)}}}static autoFillCoinByStrategyV2(i,t,n,e,s,l,d,c){let f=this.autoFillCoinByStrategy(i,t,n,e,s,l,d,c),p=Jt.toWeight({strategy_type:d,active_id:i,bin_step:t,lower_bin_id:s,upper_bin_id:l,total_amount_a:f.amount_a,total_amount_b:f.amount_b,active_bin_of_pool:c});return this.toAmountsByWeights(p)}};import{d as lt}from"@cetusprotocol/common-sdk";var Ge=class r{static getVariableFee(i){let{volatility_accumulator:t,bin_step_config:n}=i,{variable_fee_control:e,bin_step:s}=n;return lt(e).gt(0)?lt(t).mul(s).pow(2).mul(e).add(99999999999).div(1e11).toFixed(0):"0"}static calculateCompositionFee(i,t){let n=lt(i).mul(t);return lt(n).mul(lt(1e9).add(t)).div(1e18).toFixed(0)}static calculateProtocolFee(i,t){return lt(i).mul(t).div(1e4).ceil().toFixed(0)}static getProtocolFees(i,t,n){let e=r.calculateProtocolFee(i,n),s=r.calculateProtocolFee(t,n);return{protocol_fee_a:e,protocol_fee_b:s}}static getCompositionFees(i,t,n){let{bin_step_config:e}=n;if(lt(i.liquidity||"0").eq(lt(0)))return{fees_a:"0",fees_b:"0"};let{bin_step:s,base_factor:l}=e,d=F.getQPriceFromId(i.bin_id,s),c=F.getLiquidity(i.amount_a,i.amount_b,d),f=F.getLiquidity(t.amount_a,t.amount_b,d),p=lt(i.liquidity).mul(f).div(c).toFixed(0),{amount_a:_,amount_b:y}=F.calculateOutByShare({bin_id:i.bin_id,liquidity:lt(i.liquidity).add(p).toFixed(0),amount_a:lt(i.amount_a).add(t.amount_a).toFixed(0),amount_b:lt(i.amount_b).add(t.amount_b).toFixed(0),price_per_lamport:i.price_per_lamport},p),A=lt(s).mul(l).mul(10),B=r.getVariableFee(n),S=lt(A).add(B).toFixed(0);lt(S).gt(1e8)&&(S=1e8.toString());let O="0",x="0";return lt(_).gt(t.amount_a)&&lt(t.amount_b).gt(y)?x=r.calculateCompositionFee(lt(t.amount_b).sub(y).toFixed(0),S):lt(y).gt(t.amount_b)&&lt(t.amount_a).gt(_)&&(O=r.calculateCompositionFee(lt(t.amount_a).sub(_).toFixed(0),S)),{fees_a:O,fees_b:x}}};var qn=class{static calculateIlm(i){let{curvature:t,initial_price:n,max_price:e,bin_step:s,total_supply:l,pool_share_percentage:d,config:c}=i,{price_curve_points_num:f,liquidity_distribution_num:p,tokens_table_num:_,price_table_num:y}=c;if(d<0||d>100)throw new Error("Pool Share Percentage must be greater than 0 and less than 100.");let A=!1;if(e<n)throw new Error("Maximum Price must be greater or equal to Initial Price.");if(e===n&&t!==0)throw new Error("Curvature must be 0 when Maximum and Initial Price are equal.");if(e!==n&&t===0)throw new Error("Maximum and Initial Price must be equal when Curvature is 0.");e===n&&t===0&&(A=!0);let B=L=>N*Math.pow(L/j,t)+n,S=(L,nt)=>{let Q=N*Math.pow(j,-t)*Math.pow(L,t+1)/(t+1)+n*L,tt=N*Math.pow(j,-t)*Math.pow(nt,t+1)/(t+1)+n*nt;return Q-tt},O=L=>j*Math.pow((L-n)/N,1/t),x=L=>j*Math.pow(L-n,1/t-1)/(t*Math.pow(N,1/t)),R=L=>Math.pow((L-n)/N,1/t)*j,T=(L,nt,Q)=>{let tt=nt/1e4,Ie=Math.log(L)/Math.log(1+tt);return Q?Math.floor(Ie):Math.ceil(Ie)},k=(L,nt)=>{let Q=nt/1e4;return Math.pow(1+Q,L)},j=l*d/100,N=e-n,Y=[],et=[],it=T(n,s,!1),at=T(e,s,!1),X=0,rt=it;if(A)Y.push(n),et.push(n*j);else for(;rt<=at;){let L=k(rt,s),nt=k(rt+1,s),Q=R(nt)-R(L);X=X+Q,et.push(Q*L),Y.push(L),rt++}let v={price_curve:{data:[],min_y:0,max_y:0},liquidity_curve:{data:[],min_y:0,max_y:0},dlmm_bins:{data:[],min_y:0,max_y:0},tokens_table:[],price_table:[],initial_fdv:n*l,final_fdv:e*l,usdc_in_pool:0};var o=N/100;let u=[],h=0,m=0;for(let L=0;L<f;L++){let nt=L*j/(f-1),Q=A?e:B(nt);h=h<Q?h:Q,m=m>Q?m:Q,u.push({x:nt,y:Q})}v.price_curve={data:u,min_y:h,max_y:m};let b=[],M=0,P=0;if(A)M=0,P=j,b=[{x:n,y:0},{x:n,y:j/2},{x:n,y:j}];else for(let L=0;L<p;L++){let nt=n+L*o,Q=x(nt);b.push({x:nt,y:Q}),M=M<Q?M:Q,P=P>Q?P:Q}v.liquidity_curve={data:b,min_y:M,max_y:P};let g=[],a=0,w=0;Y.forEach((L,nt)=>{let Q=L,tt=et[nt];g.push({x:Q,y:tt}),a=a<tt?a:tt,w=w>tt?w:tt}),v.dlmm_bins={data:g,min_y:a,max_y:w};let C=[],D=0,W=j/_;for(;D<=j;){let L=B(D),nt=D,Q=S(nt,0);C.push({withdrawn:nt,price:L,usdc_in_pool:Q}),D=D+W}v.tokens_table=C;let z=Math.floor(N*1e10)/1e11,J=[];if(A)J.push({withdrawn:j,price:n,usdc_in_pool:j*n});else{let L=0,nt=n;for(;L<=y;){let Q=O(nt),tt=S(Q,0);J.push({withdrawn:Q,price:nt,usdc_in_pool:tt}),nt=nt+z,L=L+1}}return v.price_table=J,v.usdc_in_pool=J[J.length-1].usdc_in_pool,v}};import{normalizeSuiAddress as Un}from"@mysten/sui/utils";import{bcs as Be}from"@mysten/sui/bcs";var ze=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async getPoolAddress(i,t,n,e){try{let s=Fn(i,t,n,e),{dlmm_pool:l}=this._sdk.sdkOptions,{pools_id:d}=Zn(l),c=await this._sdk.FullClient.getDynamicField({parentId:d,name:{type:"0x2::object::ID",bcs:Be.Address.serialize(s).toBytes()}});return Nn.parse(c.dynamicField.value.bcs).value.pool_id}catch(s){return V("FetchError",s,{[te.METHOD_NAME]:"getPoolAddress",[te.REQUEST_PARAMS]:{coin_type_a:i,coin_type_b:t,bin_step:n,base_factor:e}})}}async getBasePoolList(i="all",t=!1){let{dlmm_pool:n}=this._sdk.sdkOptions,e={data:[],has_next_page:!1},s=i==="all",l=`${n.package_id}_getBasePoolList`;if(s){let d=this._sdk.getCache(l,t);if(d&&d.length>0)return e.data.push(...d),e}try{let d=`${n.package_id}::registry::CreatePoolEvent`,c=await this._sdk.FullClient.queryEventsByGraphQL({MoveEventType:d},i);e.has_next_page=c.has_next_page,e.next_cursor=c.next_cursor,c.data.forEach(f=>{let p=On(f);e.data.push(p)}),s&&this._sdk.updateCache(`${n.package_id}_getPoolImmutables`,e.data)}catch(d){return V("FetchError",d,{[te.METHOD_NAME]:"getBasePoolList"})}return e}async getPools(i="all",t=!1){let n={data:[],has_next_page:!1},e=await this.getBasePoolList(i,t);if(e.data.length===0)return n;try{let s=await this._sdk.FullClient.batchGetObjects(e.data.map(l=>l.id),{json:!0});n.has_next_page=e.has_next_page,n.next_cursor=e.next_cursor;for(let l of s){let d=Ke(l),c=`${d.id}_getDlmmPool`;this._sdk.updateCache(c,d),n.data.push(d)}}catch(s){return V("FetchError",s,{[te.METHOD_NAME]:"getPools"})}return n}async getBinInfo(i,t,n,e=!0){try{let s=`${i}_getBinInfo_${t}`,l=this._sdk.getCache(s,e);if(l!==void 0)return l;let d=F.binScore(t),[c,f]=F.resolveBinPosition(d),p=await this._sdk.FullClient.getDynamicField({parentId:i,name:{type:"u64",bcs:Be.u64().serialize(c).toBytes()}}),y=Ln.parse(p.dynamicField.value.bcs).value.group.bins[f],A=Ni(y);return this._sdk.updateCache(s,A),A}catch{return{bin_id:t,amount_a:"0",amount_b:"0",liquidity:"0",price_per_lamport:F.getPricePerLamportFromBinId(t,n)}}}async getBinInfoList(i){let t=[],n=i.map(s=>{let{bin_manager_handle:l,bin_id:d,bin_step:c}=s,f=F.binScore(d),[p,_]=F.resolveBinPosition(f),y=po(l,p,"u64","u64");return{groupIndex:p,offsetInGroup:_,bin_id:d,bin_step:c,bin_manager_handle:l,dynamic_field_id:y}});return(await this._sdk.FullClient.batchGetObjects(n.map(s=>s.dynamic_field_id),{json:!0})).forEach((s,l)=>{let{offsetInGroup:d,bin_manager_handle:c,bin_step:f,bin_id:p}=n[l];try{let y=s.json.value.value.group.bins[d],A=Ni(y);t.push({...A,bin_manager_handle:c,bin_step:f})}catch{t.push({bin_id:p,amount_a:"0",amount_b:"0",liquidity:"0",price_per_lamport:F.getPricePerLamportFromBinId(p,f),bin_manager_handle:c,bin_step:f})}}),t}async getTotalFeeRate(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:n,coin_type_a:e,coin_type_b:s}=i,l=new $e;l.moveCall({target:`${t.published_at}::pool::get_total_fee_rate`,arguments:[l.object(n)],typeArguments:[e,s]});let d=await this._sdk.FullClient.sendSimulationTransaction(l,Un("0x0"));return Be.struct("FeeRate",{base_fee_rate:Be.u64(),var_fee_rate:Be.u64(),total_fee_rate:Be.u64()}).parse(d.commandResults[0].returnValues[0].bcs)}async getPoolBinInfo(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:n,coin_type_a:e,coin_type_b:s}=i,l=900,d=[],c,f=!0;for(;f;){let p=new $e,_;c!==void 0?_=p.moveCall({target:"0x1::option::some",arguments:[p.pure.u32(Number(jn(BigInt(c))))],typeArguments:["u32"]}):_=p.moveCall({target:"0x1::option::none",typeArguments:["u32"]});try{p.moveCall({target:`${t.published_at}::pool::fetch_bins`,arguments:[p.object(n),_,p.pure.u64(l)],typeArguments:[e,s]});let y=await this._sdk.FullClient.sendSimulationTransaction(p,Un("0x0")),A=Tn(y.commandResults[1].returnValues[0].bcs);d.push(...A),c=A.length>0?A[A.length-1].bin_id+1:void 0,f=A.length===l}catch{f=!1}}return d.sort((p,_)=>p.bin_id-_.bin_id)}async getPoolTransactionList({pool_id:i,pagination_args:t,order:n="descending",full_rpc_url:e,event_types:s}){let{FullClient:l,sdkOptions:d}=this._sdk,c;e?c=ho(co(e,this._sdk.sdkOptions.env),l._graphQLClient,this._sdk.sdkOptions.env):c=l;let f={data:[],has_next_page:!1},p=50,_=t,y=t.limit||10,A=s?.length?s:Li,B=10,S=0;do{S++;let O=await c.queryTransactionBlocksByPage({affectedObject:i},{..._,limit:50},n);O.data.forEach((x,R)=>{let T=Cn(x,R,d.dlmm_pool.package_id,i,A);f.data=[...f.data,...T]}),f.has_next_page=O.has_next_page,f.next_cursor=O.next_cursor,_.cursor=O.next_cursor}while(f.data.length<y&&f.has_next_page&&S<B);return S>=B&&f.data.length<y&&(f.has_next_page=!1),f.data.length>y&&(f.data=f.data.slice(0,y),f.has_next_page=!0),f}async getRangeBinInfo(i,t,n,e){let s=[];for(let l=t;l<=n;l++){let d=await Promise.all([this.getBinInfo(i,l,e)]);s.push(...d)}return s}async getAssignPoolList(i){if(i.length===0)return[];let t=[];try{let n=await this._sdk.FullClient.batchGetObjects(i,{json:!0});for(let e of n){let s=Ke(e),l=`${s.id}_getDlmmPool`;this._sdk.updateCache(l,s),t.push(s)}}catch(n){return V("FetchError",n,{[te.METHOD_NAME]:"getAssignPoolList"})}return t}async getPool(i,t=!0){try{let n=`${i}_getDlmmPool`,e=this._sdk.getCache(n,t);if(e!==void 0)return e;let s=await this._sdk.FullClient.getObject({objectId:i,include:{json:!0}}),l=Ke(s.object);return this._sdk.updateCache(n,l),l}catch(n){return V("FetchError",n,{[te.METHOD_NAME]:"getPool",[te.REQUEST_PARAMS]:i})}}async createPoolAndAddWithPricePayload(i){let{bin_step:t,url:n,coin_type_a:e,coin_type_b:s,bin_infos:l,price_base_coin:d,price:c,lower_price:f,upper_price:p,decimals_a:_,decimals_b:y,strategy_type:A,use_bin_infos:B,base_factor:S,pool_id:O}=i,x,R,T,k=l;if(d==="coin_a")x=F.getBinIdFromPrice(f,t,!1,_,y),R=F.getBinIdFromPrice(p,t,!0,_,y),T=F.getBinIdFromPrice(c,t,!0,_,y);else{x=F.getBinIdFromPrice(qi(1).div(p).toString(),t,!1,_,y),R=F.getBinIdFromPrice(qi(1).div(f).toString(),t,!0,_,y),T=F.getBinIdFromPrice(qi(1).div(c).toString(),t,!1,_,y);let Y={pool_id:O,amount_a:l.amount_b,amount_b:l.amount_a,active_id:T,bin_step:t,lower_bin_id:x,upper_bin_id:R,active_bin_of_pool:void 0,strategy_type:i.strategy_type};k=await this.sdk.Position.calculateAddLiquidityInfo(Y)}let N={bin_step:t,url:n,coin_type_a:e,coin_type_b:s,bin_infos:k,lower_bin_id:x,upper_bin_id:R,active_id:T,strategy_type:A,use_bin_infos:B,base_factor:S};return this.createPoolAndAddLiquidityPayload(N)}createPoolPayload(i,t){let{dlmm_pool:n}=this._sdk.sdkOptions,{bin_step:e,base_factor:s,url:l,coin_type_a:d,coin_type_b:c,active_id:f}=i;t=t||new $e;let{registry_id:p,global_config_id:_,versioned_id:y}=Zn(n),[A,B]=t.moveCall({target:`${n.published_at}::registry::create_pool_v3`,arguments:[t.object(p),t.pure.u16(e),t.pure.u16(s),t.pure.u32(Number(jn(BigInt(f)))),t.pure.string(l||""),t.object(_),t.object(y),t.object(fo)],typeArguments:[d,c]});return t.moveCall({target:`${n.published_at}::registry::destroy_receipt`,arguments:[t.object(A),B,t.object(y)],typeArguments:[d,c]}),B}createPoolAndAddLiquidityPayload(i){let{bin_step:t,base_factor:n,url:e,active_id:s,coin_type_a:l,coin_type_b:d,bin_infos:c,lower_bin_id:f,upper_bin_id:p,strategy_type:_,use_bin_infos:y}=i,A=new $e;mo(Wn(l,!1),Wn(d,!1))&&V("InvalidCoinTypeSequence",new Error("invalid coin type sequence"),{[te.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[te.REQUEST_PARAMS]:i});let B=p-f+1;B>1e3&&V("InvalidBinWidth",new Error("Width is too large"),{[te.METHOD_NAME]:"openPosition",[te.REQUEST_PARAMS]:i}),console.log("\u{1F680} ~ createPoolAndAddLiquidityPayload ~ option:",{...i,width:B});let O={pool_id:this.createPoolPayload({active_id:s,bin_step:t,base_factor:n,coin_type_a:l,coin_type_b:d},A),bin_infos:c,coin_type_a:l,coin_type_b:d,lower_bin_id:f,upper_bin_id:p,active_id:s,strategy_type:_,use_bin_infos:y,max_price_slippage:0,bin_step:t};return this.sdk.Position.addLiquidityPayload(O,A),A}};import{FullRpcUrlTestnet as _o,GraphRpcUrlTestnet as go}from"@cetusprotocol/common-sdk";var Qn={env:"testnet",full_rpc_url:_o,graph_rpc_url:go,dlmm_pool:{package_id:"0xb382224d12558da5f87624765065a8c7e8f5c899d0ee890610e2bb4e8c686be9",published_at:"0x1cdac9c678c5ec89c80409db8865c9c3f8e6207dfd988fb4ddd11e2806db0bf9",version:1,config:{registry_id:"0xdc91c4f094557b9d2a35fc6159ef32649a54c4aa9350860bf0d4b52b5f0a3990",pools_id:"0xb518b1de84a1ba1ab6c9a2d71fcdf382ef36045a4660497b3d77fb0a6df7709e",global_config_id:"0xe84ebca8d61cdd9312cec3787204d0ee1063d424a81dcda62cfb8b1887041a9f",versioned_id:"0xf8478d6dc081bc266229f25ac9c31a96b5e99ecbd82222e883368dda95829065",admin_cap_id:"0x273dbda2a1d62460a01a07831aec0fa8191a41341d634fb43f15acdb627edbce",partners_id:"0xd7ac594ed2e7756f0d4a98503dd970f314f2619a10c3613c705253a155cc9fea"}},dlmm_router:{package_id:"0xbc76d04e910452518efd5fd63d5fffe77c24855063c9cdb5501e06896bc34908",published_at:"0xe122ffbcfe5091398ba15a60e654e6589fd586b3865d5b4118292754f8cde301",version:1},faucet:{package_id:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",published_at:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",version:1}};import{asUintN as ae,CLOCK_ADDRESS as me,CoinAssist as ee,d as ut,DETAILS_KEYS as Vt,getPackagerConfigs as _e}from"@cetusprotocol/common-sdk";import{Transaction as ge}from"@mysten/sui/transactions";var Ve=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}buildPositionType(){return`${this._sdk.sdkOptions.dlmm_pool.package_id}::position::Position`}async getOwnerPositionList(i){let t=[];try{(await this._sdk.FullClient.getOwnedObjectsByPage(i,this.buildPositionType())).data.forEach(e=>{t.push(Fi(e))})}catch(n){console.log("\u{1F680} ~ PositionModule ~ getOwnerPositionList ~ error:",n),V("GetObjectError",n,{[Vt.METHOD_NAME]:"getOwnerPositionList",[Vt.REQUEST_PARAMS]:i})}return t}async getPosition(i){try{let t=await this._sdk.FullClient.getObject({objectId:i,include:{json:!0}});return Fi(t.object)}catch(t){return console.log("\u{1F680} ~ PositionModule ~ getPosition ~ error:",t),V("GetObjectError",t,{[Vt.METHOD_NAME]:"getPosition",[Vt.REQUEST_PARAMS]:i})}}collectFeePayload(i,t){t=t||new ge;let{fee_a_obj:n,fee_b_obj:e}=this.collectFeePayloadWithoutTransfer(i,t);return t.transferObjects([n,e],this.sdk.getSenderAddress()),t}collectFeePayloadWithoutTransfer(i,t){let{pool_id:n,position_id:e,coin_type_a:s,coin_type_b:l}=i,{dlmm_pool:d}=this.sdk.sdkOptions,{versioned_id:c,global_config_id:f}=_e(d),[p,_]=t.moveCall({target:`${d.published_at}::pool::collect_position_fee`,arguments:[t.object(n),t.object(e),t.object(f),t.object(c)],typeArguments:[s,l]}),y=ee.fromBalance(p,s,t),A=ee.fromBalance(_,l,t);return{fee_a_obj:y,fee_b_obj:A}}updatePositionFeeAndRewards(i,t){let{dlmm_pool:n}=this.sdk.sdkOptions,{versioned_id:e}=_e(n),{pool_id:s,position_id:l,coin_type_a:d,coin_type_b:c}=i;return t.moveCall({target:`${n.published_at}::pool::update_position_fee_and_rewards`,arguments:[t.object(s),t.pure.id(l),t.object(e),t.object(me)],typeArguments:[d,c]}),t}collectRewardPayload(i,t){let{dlmm_pool:n}=this.sdk.sdkOptions,{versioned_id:e,global_config_id:s}=_e(n);return t=t||new ge,i.forEach(l=>{let{pool_id:d,position_id:c,reward_coins:f,coin_type_a:p,coin_type_b:_}=l,{reward_coin_objs:y}=this.collectRewardPayloadWithoutTransfer({pool_id:d,position_id:c,reward_coins:f,coin_type_a:p,coin_type_b:_},t);y.length>0&&t.transferObjects(y,this.sdk.getSenderAddress())}),t}collectRewardPayloadWithoutTransfer(i,t){let{dlmm_pool:n}=this.sdk.sdkOptions,{versioned_id:e,global_config_id:s}=_e(n),l=[],{pool_id:d,position_id:c,reward_coins:f,coin_type_a:p,coin_type_b:_}=i;return f.forEach(y=>{let A=t.moveCall({target:`${n.published_at}::pool::collect_position_reward`,arguments:[t.object(d),t.object(c),t.object(s),t.object(e)],typeArguments:[p,_,y]}),B=ee.fromBalance(A,y,t);l.push(B)}),{reward_coin_objs:l}}collectRewardAndFeePayload(i,t){return t=t||new ge,i.forEach(n=>{let{pool_id:e,position_id:s,reward_coins:l,coin_type_a:d,coin_type_b:c}=n;this.updatePositionFeeAndRewards({pool_id:e,position_id:s,coin_type_a:d,coin_type_b:c},t),this.collectFeePayload({pool_id:e,position_id:s,coin_type_a:d,coin_type_b:c},t),this.collectRewardPayload([{pool_id:e,position_id:s,reward_coins:l,coin_type_a:d,coin_type_b:c}],t)}),t}validateActiveIdSlippage(i,t){let{pool_id:n,active_id:e,max_price_slippage:s,bin_step:l,coin_type_a:d,coin_type_b:c}=i,{dlmm_router:f}=this.sdk.sdkOptions,p=F.getBinShift(e,l,s),_=Number(ae(BigInt(e)));return t.moveCall({target:`${f.published_at}::utils::validate_active_id_slippage`,arguments:[typeof n=="string"?t.object(n):n,t.pure.u32(Number(_)),t.pure.u32(Number(p))],typeArguments:[d,c]}),t}closePositionPayload(i,t){t=t||new ge;let{coin_a_obj:n,coin_b_obj:e}=this.closePositionNoTransferPayload(i,t);return t.transferObjects([n,e],this.sdk.getSenderAddress()),t}closePositionNoTransferPayload(i,t){let{pool_id:n,position_id:e,reward_coins:s,coin_type_a:l,coin_type_b:d}=i,{dlmm_pool:c}=this.sdk.sdkOptions,{versioned_id:f,global_config_id:p}=_e(c);t=t||new ge,this.updatePositionFeeAndRewards({pool_id:n,position_id:e,coin_type_a:l,coin_type_b:d},t),this.collectRewardPayload([{pool_id:n,position_id:e,reward_coins:s,coin_type_a:l,coin_type_b:d}],t);let[_,y,A]=t.moveCall({target:`${c.published_at}::pool::close_position`,arguments:[t.object(n),t.object(e),t.object(p),t.object(f),t.object(me)],typeArguments:[l,d]}),B=ee.fromBalance(y,l,t),S=ee.fromBalance(A,d,t);return t.moveCall({target:`${c.published_at}::pool::destroy_close_position_cert`,arguments:[_,t.object(f)],typeArguments:[]}),{coin_a_obj:B,coin_b_obj:S}}async getActiveBinIfInRange(i,t,n,e,s,l=!1){if(e<=n&&e>=t)return await this._sdk.Pool.getBinInfo(i,e,s,l)}calculateRemoveLiquidityInfo(i){let{bins:t,active_id:n,coin_amount:e}=i,s="fix_amount_a"in i,l=t.filter(y=>y.bin_id<n),d=t.filter(y=>y.bin_id>n),c=ut(0),f=ut(0),p=[];if(s){p=[...t];let{fix_amount_a:y}=i,A=t.find(O=>O.bin_id===n),B=d.reduce((O,x)=>ut(O).plus(x.amount_a),ut(0)).add(A?.amount_a||"0"),S=l.reduce((O,x)=>ut(O).plus(x.amount_b),ut(0)).add(A?.amount_b||"0");c=y?B:S,f=ut(e).gte(c)?ut(1):ut(e).div(c)}else{let{is_only_a:y}=i;p=y?d:l,c=p.reduce((A,B)=>ut(A).plus(y?B.amount_a:B.amount_b),ut(0)),f=ut(e).gte(c)?ut(1):ut(e).div(c)}return ut(c).isZero()?V("InsufficientLiquidity",new Error("Insufficient liquidity"),{[Vt.METHOD_NAME]:"calculateRemoveLiquidityResult",[Vt.REQUEST_PARAMS]:i}):F.processBinsByRate([...p],f.toFixed())}async calculateAddLiquidityInfo(i){let t="fix_amount_a"in i,{active_id:n,bin_step:e,lower_bin_id:s,upper_bin_id:l,active_bin_of_pool:d,strategy_type:c,pool_id:f}=i,p;if(t){let{coin_amount:_,fix_amount_a:y}=i;p=Re.autoFillCoinByStrategyV2(n,e,_,y,s,l,c,d)}else p=Re.toAmountsBothSideByStrategy(n,e,s,l,i.amount_a,i.amount_b,c,d);if(d&&f){let _=p.bins.findIndex(y=>y.bin_id===n);if(_!==-1){let y=await this._sdk.Pool.getPool(f,!1);if(y){let{fees_a:A,fees_b:B}=Ge.getCompositionFees(d,p.bins[_],y.variable_parameters),S=p.bins[_];S.amount_a=ut(S.amount_a).sub(A).toFixed(0),S.amount_b=ut(S.amount_b).sub(B).toFixed(0),p.bins[_]=S}}}return p}removeLiquidityPayload(i){let t=new ge,{coin_a_obj:n,coin_b_obj:e}=this.removeLiquidityNoTransferPayload(i,t);return t.transferObjects([n,e],this.sdk.getSenderAddress()),t}removeLiquidityNoTransferPayload(i,t){let{pool_id:n,position_id:e,bin_infos:s,reward_coins:l,slippage:d,coin_type_a:c,coin_type_b:f,active_id:p,collect_fee:_,bin_step:y,remove_percent:A}=i,{dlmm_pool:B}=this.sdk.sdkOptions,{bins:S}=s;(_||l.length>0)&&this.updatePositionFeeAndRewards({pool_id:n,position_id:e,coin_type_a:c,coin_type_b:f},t),_&&this.collectFeePayload({pool_id:n,position_id:e,coin_type_a:c,coin_type_b:f},t),this.collectRewardPayload([{pool_id:n,position_id:e,reward_coins:l,coin_type_a:c,coin_type_b:f}],t);let{versioned_id:O,global_config_id:x}=_e(B);if(A){let R=ae(BigInt(S[0].bin_id)),T=ae(BigInt(S[S.length-1].bin_id)),k=Number(ut(A).mul(1e4).toFixed(0)),[j,N]=t.moveCall({target:`${B.published_at}::pool::remove_liquidity_by_percent`,arguments:[t.object(n),t.object(e),t.pure.u32(Number(R)),t.pure.u32(Number(T)),t.pure.u16(k),t.object(x),t.object(O),t.object(me)],typeArguments:[c,f]}),Y=ee.fromBalance(j,c,t),et=ee.fromBalance(N,f,t);return this.validateActiveIdSlippage({pool_id:n,active_id:p,max_price_slippage:d,bin_step:y,coin_type_a:c,coin_type_b:f},t),{coin_a_obj:Y,coin_b_obj:et}}else{let R=t.pure.vector("u32",S.map(et=>Number(ae(BigInt(et.bin_id))))),T=t.pure.vector("u128",S.map(et=>et.liquidity)),[k,j]=t.moveCall({target:`${B.published_at}::pool::remove_liquidity`,arguments:[t.object(n),t.object(e),R,T,t.object(x),t.object(O),t.object(me)],typeArguments:[c,f]});this.validateActiveIdSlippage({pool_id:n,active_id:p,max_price_slippage:d,bin_step:y,coin_type_a:c,coin_type_b:f},t);let N=ee.fromBalance(k,c,t),Y=ee.fromBalance(j,f,t);return{coin_a_obj:N,coin_b_obj:Y}}}async addLiquidityWithPricePayload(i){let{pool_id:t,bin_infos:n,coin_type_a:e,coin_type_b:s,price_base_coin:l,price:d,lower_price:c,upper_price:f,bin_step:p,strategy_type:_,active_bin_of_pool:y,decimals_a:A,decimals_b:B,use_bin_infos:S,max_price_slippage:O}=i,x,R,T,k=n;if(l==="coin_a")x=F.getBinIdFromPrice(c,p,!1,A,B),R=F.getBinIdFromPrice(f,p,!0,A,B),T=F.getBinIdFromPrice(d,p,!0,A,B);else{x=F.getBinIdFromPrice(ut(1).div(f).toString(),p,!1,A,B),R=F.getBinIdFromPrice(ut(1).div(c).toString(),p,!0,A,B),T=F.getBinIdFromPrice(ut(1).div(d).toString(),p,!1,A,B);let Y={amount_a:n.amount_b,amount_b:n.amount_a,active_id:T,bin_step:p,lower_bin_id:x,upper_bin_id:R,active_bin_of_pool:y,strategy_type:_};k=await this.sdk.Position.calculateAddLiquidityInfo(Y)}let N={pool_id:t,active_id:T,bin_infos:k,coin_type_a:e,coin_type_b:s,lower_bin_id:x,upper_bin_id:R,strategy_type:_,use_bin_infos:S,max_price_slippage:O,bin_step:p};return this.addLiquidityPayload(N)}addLiquidityPayload(i,t){let{pool_id:n,bin_infos:e,coin_type_a:s,coin_type_b:l,active_id:d,strategy_type:c,max_price_slippage:f,bin_step:p,use_bin_infos:_=!1,coin_object_id_a:y,coin_object_id_b:A}=i;t=t||new ge;let B="lower_bin_id"in i,S=[];if(B){let O=F.splitBinLiquidityInfo(e,i.lower_bin_id,i.upper_bin_id);S.push(...O)}else{let O=i.position_id;S.push(e),(i.collect_fee||i.reward_coins.length>0)&&this.updatePositionFeeAndRewards({pool_id:n,position_id:O,coin_type_a:s,coin_type_b:l},t),i.collect_fee&&this.collectFeePayload({pool_id:n,position_id:O,coin_type_a:s,coin_type_b:l},t),i.reward_coins.length>0&&this.collectRewardPayload([{pool_id:n,position_id:O,reward_coins:i.reward_coins,coin_type_a:s,coin_type_b:l}],t)}return S.forEach((O,x)=>{console.log("\u{1F680} ~ PositionModule ~ addLiquidityPayload ~ liquidity_bin:",x,O);let{amount_a:R,amount_b:T,bins:k}=O,j=y||ee.buildCoinWithBalance(BigInt(R),s,t),N=A||ee.buildCoinWithBalance(BigInt(T),l,t);_?this.addLiquidityInternal({pool_id:n,coin_type_a:s,coin_type_b:l,active_id:d,liquidity_bin:O,tx:t,coin_a_obj_id:j,coin_b_obj_id:N,position_id:B?void 0:i.position_id,max_price_slippage:f,bin_step:p}):this.addLiquidityStrategyInternal({pool_id:n,coin_type_a:s,coin_type_b:l,active_id:d,liquidity_bin:O,tx:t,max_price_slippage:f,bin_step:p,coin_a_obj_id:j,coin_b_obj_id:N,strategy_type:c,position_id:B?void 0:i.position_id})}),t}addLiquidityStrategyInternal(i){let{max_price_slippage:t,bin_step:n,position_id:e,pool_id:s,coin_type_a:l,coin_type_b:d,active_id:c,liquidity_bin:f,tx:p,coin_a_obj_id:_,coin_b_obj_id:y,strategy_type:A}=i,{dlmm_pool:B,dlmm_router:S}=this.sdk.sdkOptions,{versioned_id:O,global_config_id:x}=_e(B),{bins:R,amount_a:T,amount_b:k}=f,j=e,N=R[0].bin_id,Y=R[R.length-1].bin_id,et=ae(BigInt(N)),it=Number(ae(BigInt(c))),at=F.getBinShift(c,n,t),X=Dn(A);if(e===void 0){let rt=Y-N+1;rt>1e3&&V("InvalidBinWidth",new Error("Width is too large"),{[Vt.METHOD_NAME]:"openPosition"}),console.log("\u{1F680} ~ addLiquidityStrategyInternal ~ amount_a:",{amount_a:T,amount_b:k,lower_bin_id_u32:et,width:rt,active_id_u32:it,bin_shift:at}),j=p.moveCall({target:`${S.published_at}::${X}::open_position`,arguments:[typeof s=="string"?p.object(s):s,_,y,p.pure.u64(T),p.pure.u64(k),p.pure.u32(Number(et)),p.pure.u16(Number(rt)),p.pure.u32(Number(it)),p.pure.u32(Number(at)),p.object(x),p.object(O),p.object(me)],typeArguments:[l,d]})}else{let rt=R.filter(u=>u.amount_a!=="0"||u.amount_b!=="0");if(rt.length===0)return V("InvalidParams",new Error("No bins to add liquidity"),{[Vt.METHOD_NAME]:"addLiquidityStrategyInternal",[Vt.REQUEST_PARAMS]:i});let v=ae(BigInt(rt[0].bin_id)),o=ae(BigInt(rt[rt.length-1].bin_id));p.moveCall({target:`${S.published_at}::${X}::add_liquidity`,arguments:[typeof s=="string"?p.object(s):s,p.object(e),_,y,p.pure.u64(T),p.pure.u64(k),p.pure.u32(Number(v)),p.pure.u32(Number(o)),p.pure.u32(Number(it)),p.pure.u32(Number(at)),p.object(x),p.object(O),p.object(me)],typeArguments:[l,d]})}return j?p.transferObjects([j,_,y],this.sdk.getSenderAddress()):p.transferObjects([_,y],this.sdk.getSenderAddress()),p}addLiquidityInternal(i){let{position_id:t,pool_id:n,coin_type_a:e,coin_type_b:s,active_id:l,liquidity_bin:d,tx:c,coin_a_obj_id:f,coin_b_obj_id:p,max_price_slippage:_,bin_step:y}=i,{bins:A}=d,{dlmm_pool:B,dlmm_router:S}=this.sdk.sdkOptions,{versioned_id:O,global_config_id:x}=_e(B),R=c.pure.vector("u64",A.map(Y=>Y.amount_a)),T=c.pure.vector("u64",A.map(Y=>Y.amount_b)),k=c.makeMoveVec({elements:A.map(Y=>c.pure.u32(Number(ae(BigInt(Y.bin_id))))),type:"u32"}),j=d.bins[0].bin_id,N=d.bins[d.bins.length-1].bin_id;return t===void 0?(N-j+1>1e3&&V("InvalidBinWidth",new Error("Width is too large"),{[Vt.METHOD_NAME]:"openPosition"}),c.moveCall({target:`${S.published_at}::add_liquidity::open_position`,arguments:[typeof n=="string"?c.object(n):n,f,p,k,R,T,c.object(x),c.object(O),c.object(me)],typeArguments:[e,s]}),l>=j&&l<=N&&this.validateActiveIdSlippage({pool_id:n,active_id:l,max_price_slippage:_,bin_step:y,coin_type_a:e,coin_type_b:s},c),c.transferObjects([f,p],this.sdk.getSenderAddress())):(c.moveCall({target:`${S.published_at}::add_liquidity::add_liquidity`,arguments:[typeof n=="string"?c.object(n):n,c.object(t),f,p,k,R,T,c.object(x),c.object(O),c.object(me)],typeArguments:[e,s]}),l>=j&&l<=N&&this.validateActiveIdSlippage({pool_id:n,active_id:l,max_price_slippage:_,bin_step:y,coin_type_a:e,coin_type_b:s},c),c.transferObjects([f,p],this.sdk.getSenderAddress())),c}async fetchPositionFeeAndReward(i){let t=new ge;this.collectRewardAndFeePayload(i,t);let n=await this.sdk.FullClient.sendSimulationTransaction(t,this.sdk.getSenderAddress());if(n.error!=null)return V("FetchError",new Error(n.error),{[Vt.METHOD_NAME]:"fetchPositionFeeAndReward",[Vt.REQUEST_PARAMS]:{options:i,totalOptions:i.length}});let e=kn(n.Transaction.events),s=xn(n.Transaction.events);return{feeData:e,rewardData:s}}};import{Transaction as Hn}from"@mysten/sui/transactions";import{CLOCK_ADDRESS as ji,CoinAssist as bo,d as Kn,DETAILS_KEYS as Xe,getPackagerConfigs as Gn}from"@cetusprotocol/common-sdk";import{normalizeSuiAddress as wo}from"@mysten/sui/utils";var Ye=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async preSwapQuote(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:n,coin_type_a:e,coin_type_b:s,a2b:l,by_amount_in:d,in_amount:c}=i,{versioned_id:f,global_config_id:p}=Gn(t),_=new Hn;_.moveCall({target:`${t.published_at}::pool::flash_swap`,arguments:[_.object(n),_.pure.bool(l),_.pure.bool(d),_.pure.u64(c),_.object(p),_.object(f),_.object(ji)],typeArguments:[e,s]});let y=await this.sdk.FullClient.sendSimulationTransaction(_,wo("0x0"),!1);if(y.FailedTransaction!=null)return V("FetchError",new Error(y.FailedTransaction.status.error?.message??"unknown error"),{[Xe.METHOD_NAME]:"fetchSwapQuote",[Xe.REQUEST_PARAMS]:i});let A=Rn(y.Transaction,l);return A==null?V("FetchError",new Error("No quote info"),{[Xe.METHOD_NAME]:"preSwapQuote",[Xe.REQUEST_PARAMS]:i}):(A.a2b=l,A)}swapPayload(i){let{dlmm_pool:t,dlmm_router:n}=this._sdk.sdkOptions,{quote_obj:e,coin_type_a:s,coin_type_b:l,by_amount_in:d,slippage:c,partner:f}=i,{pool_id:p,in_amount:_,out_amount:y,a2b:A}=e,B=new Hn,S=d?_:Kn(_).mul(1+c).toFixed(0),O=d?Kn(y).mul(1-c).toFixed(0):y,x=bo.buildCoinWithBalance(BigInt(S),A?s:l,B),{versioned_id:R,global_config_id:T}=Gn(t);return console.log("\u{1F680} ~ SwapModule ~ option:",{...i,in_amount_limit:S,out_amount_limit:O}),f?B.moveCall({target:`${n.published_at}::swap::${A?"swap_a2b_with_partner":"swap_b2a_with_partner"}`,arguments:[B.object(p),B.object(f),x,B.pure.bool(d),B.pure.u64(BigInt(d?_:y)),B.pure.u64(BigInt(d?O:S)),B.object(T),B.object(R),B.object(ji)],typeArguments:[s,l]}):B.moveCall({target:`${n.published_at}::swap::${A?"swap_a2b":"swap_b2a"}`,arguments:[B.object(p),x,B.pure.bool(d),B.pure.u64(BigInt(d?_:y)),B.pure.u64(BigInt(d?O:S)),B.object(T),B.object(R),B.object(ji)],typeArguments:[s,l]}),B.transferObjects([x],this.sdk.getSenderAddress()),B}};import{Transaction as Je}from"@mysten/sui/transactions";import{CLOCK_ADDRESS as vo,d as $n,DETAILS_KEYS as Ht,fixCoinType as yo,getPackagerConfigs as Ce}from"@cetusprotocol/common-sdk";var ti=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async getPartnerList(){let i=[],{dlmm_pool:t}=this._sdk.sdkOptions,{partners_id:n}=Ce(t);try{let l=(await this._sdk.FullClient.getObject({objectId:n,include:{json:!0}})).object.json.partners.contents.map(d=>d.value);l.length>0&&(await this._sdk.FullClient.batchGetObjects(l,{json:!0})).forEach(c=>{let f=Di(c);i.push(f)})}catch(e){return V("FetchError",e,{[Ht.METHOD_NAME]:"getPartnerList",[Ht.REQUEST_PARAMS]:n})}return i}async getPartnerCapId(i,t){let{dlmm_pool:n}=this._sdk.sdkOptions;try{let e=`partner_cap_id_${i}_${t}`,s=this._sdk.getCache(e);if(s)return s;let l=await this._sdk.FullClient.listOwnedObjects({owner:i,include:{json:!0},type:`${n.package_id}::partner::PartnerCap`}),d;return l.objects.forEach(c=>{let f=c.json;f.partner_id===t&&(d=f.id,this._sdk.updateCache(e,d))}),d||V("NotFound",new Error("Partner cap not found"),{[Ht.METHOD_NAME]:"getPartnerCapId",[Ht.REQUEST_PARAMS]:{owner:i,partner_id:t}})}catch(e){return V("FetchError",e,{[Ht.METHOD_NAME]:"getPartnerCapId",[Ht.REQUEST_PARAMS]:{owner:i,partner_id:t}})}}async getPartnerBalance(i){try{let t=await this._sdk.FullClient.getDynamicFieldsByPage(i),n=[],e=t.data.map(s=>s.fieldId);return e.length>0&&(await this._sdk.FullClient.batchGetObjects(e,{json:!0})).forEach(l=>{n.push({coin_type:yo(l.json.name,!1),balance:l.json.value})}),n}catch(t){return V("FetchError",t,{[Ht.METHOD_NAME]:"getPartnerBalance",[Ht.REQUEST_PARAMS]:i})}}async getPartner(i){try{let t=await this._sdk.FullClient.getObject({objectId:i,include:{json:!0}});return Di(t.object)}catch(t){return V("FetchError",t,{[Ht.METHOD_NAME]:"getPartner",[Ht.REQUEST_PARAMS]:i})}}updateRefFeeRatePayload(i,t){let{dlmm_pool:n}=this._sdk.sdkOptions,{partner_id:e,ref_fee_rate:s}=i,{global_config_id:l,versioned_id:d}=Ce(n);t=t||new Je;let c=$n(s).mul(1e4);return c.gt(1e4)?V("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[Ht.METHOD_NAME]:"updateRefFeeRatePayload",[Ht.REQUEST_PARAMS]:i}):(t.moveCall({target:`${n.published_at}::partner::update_ref_fee_rate`,arguments:[t.object(e),t.pure.u64(c.toNumber()),t.object(l),t.object(d)],typeArguments:[]}),t)}async claimRefFeePayload(i){let{partner_id:t,partner_cap_id:n,fee_coin_types:e}=i,{dlmm_pool:s}=this._sdk.sdkOptions,{versioned_id:l}=Ce(s),d=new Je,c=n;return c||(c=await this.getPartnerCapId(this._sdk.getSenderAddress(),t)),e.forEach(f=>{d.moveCall({target:`${s.published_at}::partner::claim_ref_fee`,arguments:[d.object(t),d.object(c),d.object(l)],typeArguments:[f]})}),d}updateTimeRangePayload(i,t){let{dlmm_pool:n}=this._sdk.sdkOptions,{partner_id:e,start_time:s,end_time:l}=i,{global_config_id:d,versioned_id:c}=Ce(n);t=t||new Je;let f=s>1e12?Math.floor(s/1e3):s,p=l>1e12?Math.floor(l/1e3):l;return p<=f?V("InvalidParams",new Error("end_time must be greater than start_time"),{[Ht.METHOD_NAME]:"createPartnerPayload",[Ht.REQUEST_PARAMS]:i}):(t.moveCall({target:`${n.published_at}::partner::update_time_range`,arguments:[t.object(e),t.pure.u64(f),t.pure.u64(p),t.object(d),t.object(c),t.object(vo)],typeArguments:[]}),t)}createPartnerPayload(i){let{dlmm_pool:t}=this._sdk.sdkOptions,{name:n,ref_fee_rate:e,start_time:s,end_time:l,recipient:d}=i,{partners_id:c,global_config_id:f,versioned_id:p}=Ce(t),_=new Je,y=$n(e).mul(1e4);if(y.gt(1e4))return V("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[Ht.METHOD_NAME]:"createPartnerPayload",[Ht.REQUEST_PARAMS]:i});let A=s>1e12?Math.floor(s/1e3):s,B=l>1e12?Math.floor(l/1e3):l;return B<=A?V("InvalidParams",new Error("end_time must be greater than start_time"),{[Ht.METHOD_NAME]:"createPartnerPayload",[Ht.REQUEST_PARAMS]:i}):(_.moveCall({target:`${t.published_at}::partner::create_partner`,arguments:[_.object(c),_.pure.string(n),_.pure.u64(y.toNumber()),_.pure.u64(A),_.pure.u64(B),_.pure.address(d),_.object(f),_.object(p)],typeArguments:[]}),_)}};var Ui=Ai(De(),1);import{Transaction as Wi}from"@mysten/sui/transactions";import{CLOCK_ADDRESS as zn,CoinAssist as Mo,d as Se,getPackagerConfigs as Zi,MathUtil as Vn}from"@cetusprotocol/common-sdk";var ei=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}async getRewardPeriodEmission(i,t,n){let e=await this._sdk.FullClient.getDynamicFieldsByPage(i),s=[],l=e.data.map(p=>p.fieldId);l.length>0&&(await this._sdk.FullClient.batchGetObjects(l,{json:!0})).forEach(_=>{let y=_.json,A=Vn.u128ToI128(new Ui.default(y.value.value.bits)).toString(),B=y.name,S=new Date(Number(B)*1e3).toLocaleString(),x={emissions_per_second:"0",emissions_per_day:"0",emissions_per:Vn.fromX64(new Ui.default(A)).toString(),time:B,visualized_time:S};s.push(x)});let d=s.sort((p,_)=>Number(p.time)-Number(_.time)),c=[];c.push({emissions_per_second:t,emissions_per_day:Se(t).mul(3600*24).toString(),emissions_per:"0",time:n.toString(),visualized_time:new Date(n*1e3).toLocaleString()});let f=t;for(let p=0;p<d.length;p++){let _=d[p];if(Se(_.time).lte(n))continue;f=Se(f).add(Se(_.emissions_per)).toString();let y=Se(f).mul(3600*24).toString();Se(f).lt(0)?(_.emissions_per_second="0",_.emissions_per_day="0"):(_.emissions_per_second=f,_.emissions_per_day=y),c.push(_)}return c}addRewardPayload(i,t){t=t||new Wi;let{dlmm_pool:n}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=Zi(n),{pool_id:l,reward_coin_type:d,reward_amount:c,start_time_seconds:f,end_time_seconds:p,coin_type_a:_,coin_type_b:y}=i,A=Mo.buildCoinWithBalance(BigInt(c),d,t);return t.pure.option("u64",f),t.moveCall({target:`${n.published_at}::pool::add_reward`,arguments:[t.object(l),A,t.pure.option("u64",f),t.pure.u64(p),t.object(e),t.object(s),t.object(zn)],typeArguments:[_,y,d]}),t}initRewardPayload(i,t){t=t||new Wi;let{dlmm_pool:n}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=Zi(n),{pool_id:l,reward_coin_types:d,coin_type_a:c,coin_type_b:f}=i;return d.forEach(p=>{t.moveCall({target:`${n.published_at}::pool::initialize_reward`,arguments:[t.object(l),t.object(e),t.object(s),t.object(zn)],typeArguments:[c,f,p]})}),t}buildRewardAccessPayload(i,t){t=t||new Wi;let{dlmm_pool:n}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=Zi(n),{pool_id:l,type:d,coin_type_a:c,coin_type_b:f}=i;return t.moveCall({target:`${n.published_at}::pool::${d==="to_public"?"make_reward_public":"make_reward_private"}`,arguments:[t.object(l),t.object(e),t.object(s)],typeArguments:[c,f]}),t}};import{Transaction as Po}from"@mysten/sui/transactions";import{DETAILS_KEYS as ii,fixCoinType as Ao,getPackagerConfigs as Xn}from"@cetusprotocol/common-sdk";var ni=class{constructor(i){this._sdk=i}get sdk(){return this._sdk}buildRewardWhiteListPayload(i,t){t=t||new Po;let{dlmm_pool:n}=this._sdk.sdkOptions,{global_config_id:e,versioned_id:s}=Xn(n),{reward_coin_types:l,type:d}=i;return l.forEach(c=>{t.moveCall({target:`${n.published_at}::config::${d==="add"?"add_reward_whitelist":"remove_reward_whitelist"}`,arguments:[t.object(e),t.object(s)],typeArguments:[c]})}),t}async getBinStepConfigList(i){let t=await this._sdk.FullClient.getDynamicFieldsByPage(i),n=[],e=t.data.map(s=>s.fieldId);return e.length>0&&(await this._sdk.FullClient.batchGetObjects(e,{json:!0})).forEach(l=>{let c={...l.json.value};n.push(c)}),n}async getDlmmGlobalConfig(){let{dlmm_pool:i}=this._sdk.sdkOptions,{global_config_id:t}=Xn(i);try{let e=(await this._sdk.FullClient.getObject({objectId:t,include:{json:!0}})).object.json,s=e.reward_config,l=s.reward_white_list?.fields?.contents?.map(c=>Ao(c.fields.key.fields.name,!1))||[];return{id:e.id.id,acl:{id:e.acl.permissions.id,size:e.acl.permissions.size},allowed_list:{id:e.allowed_list.id,size:e.allowed_list.size},denied_list:{id:e.denied_list.id,size:e.denied_list.size},bin_steps:{id:e.bin_steps.id,size:e.bin_steps.size},reward_white_list:l,blocked_position:{id:e.restriction.blocked_position.permissions.id,size:e.restriction.blocked_position.permissions.size},blocked_user:{id:e.restriction.blocked_user.permissions.id,size:e.restriction.blocked_user.permissions.size},min_reward_duration:Number(s.min_reward_duration),non_manager_initialize_reward_cap:Number(s.manager_reserved_reward_init_slots),reward_public:s.reward_public}}catch(n){return console.log("fetchGlobalConfig error: ",n),V("FetchError",n,{[ii.METHOD_NAME]:"getBinStepConfigs",[ii.REQUEST_PARAMS]:t})}}async fetchDlmmSdkConfigs(){let{dlmm_pool:i}=this._sdk.sdkOptions,t={registry_id:"",pools_id:"",global_config_id:"",versioned_id:"",admin_cap_id:"",partners_id:""},e=(await this._sdk.FullClient.getObject({objectId:i.package_id,include:{json:!0,previousTransaction:!0}})).object?.previousTransaction;if(!e)return V("FetchError","Missing previous transaction digest for dlmm package object",{[ii.METHOD_NAME]:"fetchDlmmSdkConfigs",[ii.REQUEST_PARAMS]:i.package_id});(await this._sdk.FullClient.queryEventsByGraphQL({Transaction:e})).data?.forEach(c=>{let f=c.type,p=c.parsedJson;f.includes("versioned::InitEvent")&&(t.versioned_id=p.versioned),f.includes("partner::InitPartnerEvent")&&(t.partners_id=p.partners_id),f.includes("config::InitEvent")&&(t.global_config_id=p.config_id),f.includes("admin_cap::InitEvent")&&(t.admin_cap_id=p.admin_cap_id),f.includes("registry::RegistryEvent")&&(t.registry_id=p.pools_id)});let d=(await this._sdk.FullClient.getObject({objectId:t.registry_id,include:{json:!0}})).object.json;return t.pools_id=d.pools.id,t}};var ri=class r extends Bo{constructor(i){super(i),this._pool=new ze(this),this._position=new Ve(this),this._swap=new Ye(this),this._partner=new ti(this),this._reward=new ei(this),this._config=new ni(this)}get Pool(){return this._pool}get Position(){return this._position}get Swap(){return this._swap}get Partner(){return this._partner}get Reward(){return this._reward}get Config(){return this._config}static createSDK(i){let{env:t="mainnet"}=i;return t==="mainnet"?r.createCustomSDK({...ln,...i}):r.createCustomSDK({...Qn,...i})}static createCustomSDK(i){return new r(i)}};var wl=ri;export{Yt as BASIS_POINT,he as BASIS_POINT_MAX,Me as BIN_BOUND,ao as Bin,lo as BinGroup,uo as BinGroupRefRaw,F as BinUtils,ri as CetusDlmmSDK,Te as DEFAULT_MAX_WEIGHT,ye as DEFAULT_MIN_WEIGHT,An as FEE_PRECISION,Ge as FeeUtils,qn as IlmUtils,Co as MAX_BIN_ID,fe as MAX_BIN_PER_POSITION,ki as MAX_FEE_RATE,Ro as MIN_BIN_ID,Nn as NodeIDPoolSimpleInfo,xi as ONE,ze as PoolModule,so as PoolSimpleInfoRaw,Do as REWARD_PERIOD,Fo as REWARD_PERIOD_START_AT,q as SCALE_OFFSET,Ln as SkipListNodeBinGroupRefRaw,Qe as StrategyType,Re as StrategyUtils,Jt as WeightUtils,Fn as buildPoolKey,wl as default,ln as dlmmMainnet,Qn as dlmmTestnet,ws as generateRewardSchedule,Dn as getRouterModule,Ni as parseBinInfo,Tn as parseBinInfoList,ys as parseCurrentRewardPeriodEmission,On as parseDlmmBasePool,Ke as parseDlmmPool,Fi as parseDlmmPosition,gs as parseLiquidityShares,Di as parsePartner,Cn as parsePoolTransactionInfo,vs as parseRewardPeriodEmission,bs as parseStrategyType,kn as parsedDlmmPosFeeData,xn as parsedDlmmPosRewardData,Rn as parsedSwapQuoteData,Li as poolFilterEvenTypes,xe as safeAmount,oe as safeMulAmount};
1
+ var fr=Object.create;var pn=Object.defineProperty;var pr=Object.getOwnPropertyDescriptor;var mr=Object.getOwnPropertyNames;var hr=Object.getPrototypeOf,_r=Object.prototype.hasOwnProperty;var gr=(r=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(r,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):r)(function(r){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+r+'" is not supported')});var br=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports);var wr=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of mr(e))!_r.call(r,i)&&i!==t&&pn(r,i,{get:()=>e[i],enumerable:!(n=pr(e,i))||n.enumerable});return r};var Ri=(r,e,t)=>(t=r!=null?fr(hr(r)):{},wr(e||!r||!r.__esModule?pn(t,"default",{value:r,enumerable:!0}):t,r));var je=br((hn,Ti)=>{"use strict";(function(r,e){"use strict";function t(y,s){if(!y)throw new Error(s||"Assertion failed")}function n(y,s){y.super_=s;var u=function(){};u.prototype=s.prototype,y.prototype=new u,y.prototype.constructor=y}function i(y,s,u){if(i.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 r=="object"?r.exports=i:e.BN=i,i.BN=i,i.wordSize=26;var o;try{typeof window<"u"&&typeof window.Buffer<"u"?o=window.Buffer:o=gr("buffer").Buffer}catch{}i.isBN=function(s){return s instanceof i?!0:s!==null&&typeof s=="object"&&s.constructor.wordSize===i.wordSize&&Array.isArray(s.words)},i.max=function(s,u){return s.cmp(u)>0?s:u},i.min=function(s,u){return s.cmp(u)<0?s:u},i.prototype._init=function(s,u,m){if(typeof s=="number")return this._initNumber(s,u,m);if(typeof s=="object")return this._initArray(s,u,m);u==="hex"&&(u=16),t(u===(u|0)&&u>=2&&u<=36),s=s.toString().replace(/\s+/g,"");var h=0;s[0]==="-"&&(h++,this.negative=1),h<s.length&&(u===16?this._parseHex(s,h,m):(this._parseBase(s,u,h),m==="le"&&this._initArray(this.toArray(),u,m)))},i.prototype._initNumber=function(s,u,m){s<0&&(this.negative=1,s=-s),s<67108864?(this.words=[s&67108863],this.length=1):s<4503599627370496?(this.words=[s&67108863,s/67108864&67108863],this.length=2):(t(s<9007199254740992),this.words=[s&67108863,s/67108864&67108863,1],this.length=3),m==="le"&&this._initArray(this.toArray(),u,m)},i.prototype._initArray=function(s,u,m){if(t(typeof s.length=="number"),s.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(s.length/3),this.words=new Array(this.length);for(var h=0;h<this.length;h++)this.words[h]=0;var b,M,B=0;if(m==="be")for(h=s.length-1,b=0;h>=0;h-=3)M=s[h]|s[h-1]<<8|s[h-2]<<16,this.words[b]|=M<<B&67108863,this.words[b+1]=M>>>26-B&67108863,B+=24,B>=26&&(B-=26,b++);else if(m==="le")for(h=0,b=0;h<s.length;h+=3)M=s[h]|s[h+1]<<8|s[h+2]<<16,this.words[b]|=M<<B&67108863,this.words[b+1]=M>>>26-B&67108863,B+=24,B>=26&&(B-=26,b++);return this._strip()};function l(y,s){var u=y.charCodeAt(s);if(u>=48&&u<=57)return u-48;if(u>=65&&u<=70)return u-55;if(u>=97&&u<=102)return u-87;t(!1,"Invalid character in "+y)}function d(y,s,u){var m=l(y,u);return u-1>=s&&(m|=l(y,u-1)<<4),m}i.prototype._parseHex=function(s,u,m){this.length=Math.ceil((s.length-u)/6),this.words=new Array(this.length);for(var h=0;h<this.length;h++)this.words[h]=0;var b=0,M=0,B;if(m==="be")for(h=s.length-1;h>=u;h-=2)B=d(s,u,h)<<b,this.words[M]|=B&67108863,b>=18?(b-=18,M+=1,this.words[M]|=B>>>26):b+=8;else{var g=s.length-u;for(h=g%2===0?u+1:u;h<s.length;h+=2)B=d(s,u,h)<<b,this.words[M]|=B&67108863,b>=18?(b-=18,M+=1,this.words[M]|=B>>>26):b+=8}this._strip()};function f(y,s,u,m){for(var h=0,b=0,M=Math.min(y.length,u),B=s;B<M;B++){var g=y.charCodeAt(B)-48;h*=m,g>=49?b=g-49+10:g>=17?b=g-17+10:b=g,t(g>=0&&b<m,"Invalid character"),h+=b}return h}i.prototype._parseBase=function(s,u,m){this.words=[0],this.length=1;for(var h=0,b=1;b<=67108863;b*=u)h++;h--,b=b/u|0;for(var M=s.length-m,B=M%h,g=Math.min(M,M-B)+m,a=0,v=m;v<g;v+=h)a=f(s,v,v+h,u),this.imuln(b),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a);if(B!==0){var D=1;for(a=f(s,v,s.length,u),v=0;v<B;v++)D*=u;this.imuln(D),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a)}this._strip()},i.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 c(y,s){y.words=s.words,y.length=s.length,y.negative=s.negative,y.red=s.red}if(i.prototype._move=function(s){c(s,this)},i.prototype.clone=function(){var s=new i(null);return this.copy(s),s},i.prototype._expand=function(s){for(;this.length<s;)this.words[this.length++]=0;return this},i.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},i.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{i.prototype[Symbol.for("nodejs.util.inspect.custom")]=p}catch{i.prototype.inspect=p}else i.prototype.inspect=p;function p(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var _=["","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"],w=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],P=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];i.prototype.toString=function(s,u){s=s||10,u=u|0||1;var m;if(s===16||s==="hex"){m="";for(var h=0,b=0,M=0;M<this.length;M++){var B=this.words[M],g=((B<<h|b)&16777215).toString(16);b=B>>>24-h&16777215,h+=2,h>=26&&(h-=26,M--),b!==0||M!==this.length-1?m=_[6-g.length]+g+m:m=g+m}for(b!==0&&(m=b.toString(16)+m);m.length%u!==0;)m="0"+m;return this.negative!==0&&(m="-"+m),m}if(s===(s|0)&&s>=2&&s<=36){var a=w[s],v=P[s];m="";var D=this.clone();for(D.negative=0;!D.isZero();){var N=D.modrn(v).toString(s);D=D.idivn(v),D.isZero()?m=N+m:m=_[a-N.length]+N+m}for(this.isZero()&&(m="0"+m);m.length%u!==0;)m="0"+m;return this.negative!==0&&(m="-"+m),m}t(!1,"Base should be between 2 and 36")},i.prototype.toNumber=function(){var s=this.words[0];return this.length===2?s+=this.words[1]*67108864:this.length===3&&this.words[2]===1?s+=4503599627370496+this.words[1]*67108864:this.length>2&&t(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-s:s},i.prototype.toJSON=function(){return this.toString(16,2)},o&&(i.prototype.toBuffer=function(s,u){return this.toArrayLike(o,s,u)}),i.prototype.toArray=function(s,u){return this.toArrayLike(Array,s,u)};var A=function(s,u){return s.allocUnsafe?s.allocUnsafe(u):new s(u)};i.prototype.toArrayLike=function(s,u,m){this._strip();var h=this.byteLength(),b=m||Math.max(1,h);t(h<=b,"byte array longer than desired length"),t(b>0,"Requested array length <= 0");var M=A(s,b),B=u==="le"?"LE":"BE";return this["_toArrayLike"+B](M,h),M},i.prototype._toArrayLikeLE=function(s,u){for(var m=0,h=0,b=0,M=0;b<this.length;b++){var B=this.words[b]<<M|h;s[m++]=B&255,m<s.length&&(s[m++]=B>>8&255),m<s.length&&(s[m++]=B>>16&255),M===6?(m<s.length&&(s[m++]=B>>24&255),h=0,M=0):(h=B>>>24,M+=2)}if(m<s.length)for(s[m++]=h;m<s.length;)s[m++]=0},i.prototype._toArrayLikeBE=function(s,u){for(var m=s.length-1,h=0,b=0,M=0;b<this.length;b++){var B=this.words[b]<<M|h;s[m--]=B&255,m>=0&&(s[m--]=B>>8&255),m>=0&&(s[m--]=B>>16&255),M===6?(m>=0&&(s[m--]=B>>24&255),h=0,M=0):(h=B>>>24,M+=2)}if(m>=0)for(s[m--]=h;m>=0;)s[m--]=0},Math.clz32?i.prototype._countBits=function(s){return 32-Math.clz32(s)}:i.prototype._countBits=function(s){var u=s,m=0;return u>=4096&&(m+=13,u>>>=13),u>=64&&(m+=7,u>>>=7),u>=8&&(m+=4,u>>>=4),u>=2&&(m+=2,u>>>=2),m+u},i.prototype._zeroBits=function(s){if(s===0)return 26;var u=s,m=0;return(u&8191)===0&&(m+=13,u>>>=13),(u&127)===0&&(m+=7,u>>>=7),(u&15)===0&&(m+=4,u>>>=4),(u&3)===0&&(m+=2,u>>>=2),(u&1)===0&&m++,m},i.prototype.bitLength=function(){var s=this.words[this.length-1],u=this._countBits(s);return(this.length-1)*26+u};function I(y){for(var s=new Array(y.bitLength()),u=0;u<s.length;u++){var m=u/26|0,h=u%26;s[u]=y.words[m]>>>h&1}return s}i.prototype.zeroBits=function(){if(this.isZero())return 0;for(var s=0,u=0;u<this.length;u++){var m=this._zeroBits(this.words[u]);if(s+=m,m!==26)break}return s},i.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},i.prototype.toTwos=function(s){return this.negative!==0?this.abs().inotn(s).iaddn(1):this.clone()},i.prototype.fromTwos=function(s){return this.testn(s-1)?this.notn(s).iaddn(1).ineg():this.clone()},i.prototype.isNeg=function(){return this.negative!==0},i.prototype.neg=function(){return this.clone().ineg()},i.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},i.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()},i.prototype.ior=function(s){return t((this.negative|s.negative)===0),this.iuor(s)},i.prototype.or=function(s){return this.length>s.length?this.clone().ior(s):s.clone().ior(this)},i.prototype.uor=function(s){return this.length>s.length?this.clone().iuor(s):s.clone().iuor(this)},i.prototype.iuand=function(s){var u;this.length>s.length?u=s:u=this;for(var m=0;m<u.length;m++)this.words[m]=this.words[m]&s.words[m];return this.length=u.length,this._strip()},i.prototype.iand=function(s){return t((this.negative|s.negative)===0),this.iuand(s)},i.prototype.and=function(s){return this.length>s.length?this.clone().iand(s):s.clone().iand(this)},i.prototype.uand=function(s){return this.length>s.length?this.clone().iuand(s):s.clone().iuand(this)},i.prototype.iuxor=function(s){var u,m;this.length>s.length?(u=this,m=s):(u=s,m=this);for(var h=0;h<m.length;h++)this.words[h]=u.words[h]^m.words[h];if(this!==u)for(;h<u.length;h++)this.words[h]=u.words[h];return this.length=u.length,this._strip()},i.prototype.ixor=function(s){return t((this.negative|s.negative)===0),this.iuxor(s)},i.prototype.xor=function(s){return this.length>s.length?this.clone().ixor(s):s.clone().ixor(this)},i.prototype.uxor=function(s){return this.length>s.length?this.clone().iuxor(s):s.clone().iuxor(this)},i.prototype.inotn=function(s){t(typeof s=="number"&&s>=0);var u=Math.ceil(s/26)|0,m=s%26;this._expand(u),m>0&&u--;for(var h=0;h<u;h++)this.words[h]=~this.words[h]&67108863;return m>0&&(this.words[h]=~this.words[h]&67108863>>26-m),this._strip()},i.prototype.notn=function(s){return this.clone().inotn(s)},i.prototype.setn=function(s,u){t(typeof s=="number"&&s>=0);var m=s/26|0,h=s%26;return this._expand(m+1),u?this.words[m]=this.words[m]|1<<h:this.words[m]=this.words[m]&~(1<<h),this._strip()},i.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 m,h;this.length>s.length?(m=this,h=s):(m=s,h=this);for(var b=0,M=0;M<h.length;M++)u=(m.words[M]|0)+(h.words[M]|0)+b,this.words[M]=u&67108863,b=u>>>26;for(;b!==0&&M<m.length;M++)u=(m.words[M]|0)+b,this.words[M]=u&67108863,b=u>>>26;if(this.length=m.length,b!==0)this.words[this.length]=b,this.length++;else if(m!==this)for(;M<m.length;M++)this.words[M]=m.words[M];return this},i.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)},i.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 m=this.cmp(s);if(m===0)return this.negative=0,this.length=1,this.words[0]=0,this;var h,b;m>0?(h=this,b=s):(h=s,b=this);for(var M=0,B=0;B<b.length;B++)u=(h.words[B]|0)-(b.words[B]|0)+M,M=u>>26,this.words[B]=u&67108863;for(;M!==0&&B<h.length;B++)u=(h.words[B]|0)+M,M=u>>26,this.words[B]=u&67108863;if(M===0&&B<h.length&&h!==this)for(;B<h.length;B++)this.words[B]=h.words[B];return this.length=Math.max(this.length,B),h!==this&&(this.negative=1),this._strip()},i.prototype.sub=function(s){return this.clone().isub(s)};function E(y,s,u){u.negative=s.negative^y.negative;var m=y.length+s.length|0;u.length=m,m=m-1|0;var h=y.words[0]|0,b=s.words[0]|0,M=h*b,B=M&67108863,g=M/67108864|0;u.words[0]=B;for(var a=1;a<m;a++){for(var v=g>>>26,D=g&67108863,N=Math.min(a,s.length-1),j=Math.max(0,a-y.length+1);j<=N;j++){var X=a-j|0;h=y.words[X]|0,b=s.words[j]|0,M=h*b+D,v+=M/67108864|0,D=M&67108863}u.words[a]=D|0,g=v|0}return g!==0?u.words[a]=g|0:u.length--,u._strip()}var T=function(s,u,m){var h=s.words,b=u.words,M=m.words,B=0,g,a,v,D=h[0]|0,N=D&8191,j=D>>>13,X=h[1]|0,J=X&8191,F=X>>>13,it=h[2]|0,Z=it&8191,tt=it>>>13,xe=h[3]|0,dt=xe&8191,ct=xe>>>13,zi=h[4]|0,ft=zi&8191,pt=zi>>>13,Xi=h[5]|0,mt=Xi&8191,ht=Xi>>>13,Yi=h[6]|0,_t=Yi&8191,gt=Yi>>>13,Ji=h[7]|0,bt=Ji&8191,wt=Ji>>>13,tn=h[8]|0,vt=tn&8191,yt=tn>>>13,en=h[9]|0,Mt=en&8191,Pt=en>>>13,nn=b[0]|0,Bt=nn&8191,At=nn>>>13,rn=b[1]|0,It=rn&8191,St=rn>>>13,on=b[2]|0,Et=on&8191,Ot=on>>>13,sn=b[3]|0,Rt=sn&8191,Tt=sn>>>13,an=b[4]|0,xt=an&8191,Ct=an>>>13,ln=b[5]|0,kt=ln&8191,Dt=ln>>>13,un=b[6]|0,Nt=un&8191,Ft=un>>>13,dn=b[7]|0,qt=dn&8191,Lt=dn>>>13,cn=b[8]|0,jt=cn&8191,Wt=cn>>>13,fn=b[9]|0,Ut=fn&8191,Zt=fn>>>13;m.negative=s.negative^u.negative,m.length=19,g=Math.imul(N,Bt),a=Math.imul(N,At),a=a+Math.imul(j,Bt)|0,v=Math.imul(j,At);var ci=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(ci>>>26)|0,ci&=67108863,g=Math.imul(J,Bt),a=Math.imul(J,At),a=a+Math.imul(F,Bt)|0,v=Math.imul(F,At),g=g+Math.imul(N,It)|0,a=a+Math.imul(N,St)|0,a=a+Math.imul(j,It)|0,v=v+Math.imul(j,St)|0;var fi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(fi>>>26)|0,fi&=67108863,g=Math.imul(Z,Bt),a=Math.imul(Z,At),a=a+Math.imul(tt,Bt)|0,v=Math.imul(tt,At),g=g+Math.imul(J,It)|0,a=a+Math.imul(J,St)|0,a=a+Math.imul(F,It)|0,v=v+Math.imul(F,St)|0,g=g+Math.imul(N,Et)|0,a=a+Math.imul(N,Ot)|0,a=a+Math.imul(j,Et)|0,v=v+Math.imul(j,Ot)|0;var pi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(pi>>>26)|0,pi&=67108863,g=Math.imul(dt,Bt),a=Math.imul(dt,At),a=a+Math.imul(ct,Bt)|0,v=Math.imul(ct,At),g=g+Math.imul(Z,It)|0,a=a+Math.imul(Z,St)|0,a=a+Math.imul(tt,It)|0,v=v+Math.imul(tt,St)|0,g=g+Math.imul(J,Et)|0,a=a+Math.imul(J,Ot)|0,a=a+Math.imul(F,Et)|0,v=v+Math.imul(F,Ot)|0,g=g+Math.imul(N,Rt)|0,a=a+Math.imul(N,Tt)|0,a=a+Math.imul(j,Rt)|0,v=v+Math.imul(j,Tt)|0;var mi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(mi>>>26)|0,mi&=67108863,g=Math.imul(ft,Bt),a=Math.imul(ft,At),a=a+Math.imul(pt,Bt)|0,v=Math.imul(pt,At),g=g+Math.imul(dt,It)|0,a=a+Math.imul(dt,St)|0,a=a+Math.imul(ct,It)|0,v=v+Math.imul(ct,St)|0,g=g+Math.imul(Z,Et)|0,a=a+Math.imul(Z,Ot)|0,a=a+Math.imul(tt,Et)|0,v=v+Math.imul(tt,Ot)|0,g=g+Math.imul(J,Rt)|0,a=a+Math.imul(J,Tt)|0,a=a+Math.imul(F,Rt)|0,v=v+Math.imul(F,Tt)|0,g=g+Math.imul(N,xt)|0,a=a+Math.imul(N,Ct)|0,a=a+Math.imul(j,xt)|0,v=v+Math.imul(j,Ct)|0;var hi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(hi>>>26)|0,hi&=67108863,g=Math.imul(mt,Bt),a=Math.imul(mt,At),a=a+Math.imul(ht,Bt)|0,v=Math.imul(ht,At),g=g+Math.imul(ft,It)|0,a=a+Math.imul(ft,St)|0,a=a+Math.imul(pt,It)|0,v=v+Math.imul(pt,St)|0,g=g+Math.imul(dt,Et)|0,a=a+Math.imul(dt,Ot)|0,a=a+Math.imul(ct,Et)|0,v=v+Math.imul(ct,Ot)|0,g=g+Math.imul(Z,Rt)|0,a=a+Math.imul(Z,Tt)|0,a=a+Math.imul(tt,Rt)|0,v=v+Math.imul(tt,Tt)|0,g=g+Math.imul(J,xt)|0,a=a+Math.imul(J,Ct)|0,a=a+Math.imul(F,xt)|0,v=v+Math.imul(F,Ct)|0,g=g+Math.imul(N,kt)|0,a=a+Math.imul(N,Dt)|0,a=a+Math.imul(j,kt)|0,v=v+Math.imul(j,Dt)|0;var _i=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(_i>>>26)|0,_i&=67108863,g=Math.imul(_t,Bt),a=Math.imul(_t,At),a=a+Math.imul(gt,Bt)|0,v=Math.imul(gt,At),g=g+Math.imul(mt,It)|0,a=a+Math.imul(mt,St)|0,a=a+Math.imul(ht,It)|0,v=v+Math.imul(ht,St)|0,g=g+Math.imul(ft,Et)|0,a=a+Math.imul(ft,Ot)|0,a=a+Math.imul(pt,Et)|0,v=v+Math.imul(pt,Ot)|0,g=g+Math.imul(dt,Rt)|0,a=a+Math.imul(dt,Tt)|0,a=a+Math.imul(ct,Rt)|0,v=v+Math.imul(ct,Tt)|0,g=g+Math.imul(Z,xt)|0,a=a+Math.imul(Z,Ct)|0,a=a+Math.imul(tt,xt)|0,v=v+Math.imul(tt,Ct)|0,g=g+Math.imul(J,kt)|0,a=a+Math.imul(J,Dt)|0,a=a+Math.imul(F,kt)|0,v=v+Math.imul(F,Dt)|0,g=g+Math.imul(N,Nt)|0,a=a+Math.imul(N,Ft)|0,a=a+Math.imul(j,Nt)|0,v=v+Math.imul(j,Ft)|0;var gi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(gi>>>26)|0,gi&=67108863,g=Math.imul(bt,Bt),a=Math.imul(bt,At),a=a+Math.imul(wt,Bt)|0,v=Math.imul(wt,At),g=g+Math.imul(_t,It)|0,a=a+Math.imul(_t,St)|0,a=a+Math.imul(gt,It)|0,v=v+Math.imul(gt,St)|0,g=g+Math.imul(mt,Et)|0,a=a+Math.imul(mt,Ot)|0,a=a+Math.imul(ht,Et)|0,v=v+Math.imul(ht,Ot)|0,g=g+Math.imul(ft,Rt)|0,a=a+Math.imul(ft,Tt)|0,a=a+Math.imul(pt,Rt)|0,v=v+Math.imul(pt,Tt)|0,g=g+Math.imul(dt,xt)|0,a=a+Math.imul(dt,Ct)|0,a=a+Math.imul(ct,xt)|0,v=v+Math.imul(ct,Ct)|0,g=g+Math.imul(Z,kt)|0,a=a+Math.imul(Z,Dt)|0,a=a+Math.imul(tt,kt)|0,v=v+Math.imul(tt,Dt)|0,g=g+Math.imul(J,Nt)|0,a=a+Math.imul(J,Ft)|0,a=a+Math.imul(F,Nt)|0,v=v+Math.imul(F,Ft)|0,g=g+Math.imul(N,qt)|0,a=a+Math.imul(N,Lt)|0,a=a+Math.imul(j,qt)|0,v=v+Math.imul(j,Lt)|0;var bi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(bi>>>26)|0,bi&=67108863,g=Math.imul(vt,Bt),a=Math.imul(vt,At),a=a+Math.imul(yt,Bt)|0,v=Math.imul(yt,At),g=g+Math.imul(bt,It)|0,a=a+Math.imul(bt,St)|0,a=a+Math.imul(wt,It)|0,v=v+Math.imul(wt,St)|0,g=g+Math.imul(_t,Et)|0,a=a+Math.imul(_t,Ot)|0,a=a+Math.imul(gt,Et)|0,v=v+Math.imul(gt,Ot)|0,g=g+Math.imul(mt,Rt)|0,a=a+Math.imul(mt,Tt)|0,a=a+Math.imul(ht,Rt)|0,v=v+Math.imul(ht,Tt)|0,g=g+Math.imul(ft,xt)|0,a=a+Math.imul(ft,Ct)|0,a=a+Math.imul(pt,xt)|0,v=v+Math.imul(pt,Ct)|0,g=g+Math.imul(dt,kt)|0,a=a+Math.imul(dt,Dt)|0,a=a+Math.imul(ct,kt)|0,v=v+Math.imul(ct,Dt)|0,g=g+Math.imul(Z,Nt)|0,a=a+Math.imul(Z,Ft)|0,a=a+Math.imul(tt,Nt)|0,v=v+Math.imul(tt,Ft)|0,g=g+Math.imul(J,qt)|0,a=a+Math.imul(J,Lt)|0,a=a+Math.imul(F,qt)|0,v=v+Math.imul(F,Lt)|0,g=g+Math.imul(N,jt)|0,a=a+Math.imul(N,Wt)|0,a=a+Math.imul(j,jt)|0,v=v+Math.imul(j,Wt)|0;var wi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(wi>>>26)|0,wi&=67108863,g=Math.imul(Mt,Bt),a=Math.imul(Mt,At),a=a+Math.imul(Pt,Bt)|0,v=Math.imul(Pt,At),g=g+Math.imul(vt,It)|0,a=a+Math.imul(vt,St)|0,a=a+Math.imul(yt,It)|0,v=v+Math.imul(yt,St)|0,g=g+Math.imul(bt,Et)|0,a=a+Math.imul(bt,Ot)|0,a=a+Math.imul(wt,Et)|0,v=v+Math.imul(wt,Ot)|0,g=g+Math.imul(_t,Rt)|0,a=a+Math.imul(_t,Tt)|0,a=a+Math.imul(gt,Rt)|0,v=v+Math.imul(gt,Tt)|0,g=g+Math.imul(mt,xt)|0,a=a+Math.imul(mt,Ct)|0,a=a+Math.imul(ht,xt)|0,v=v+Math.imul(ht,Ct)|0,g=g+Math.imul(ft,kt)|0,a=a+Math.imul(ft,Dt)|0,a=a+Math.imul(pt,kt)|0,v=v+Math.imul(pt,Dt)|0,g=g+Math.imul(dt,Nt)|0,a=a+Math.imul(dt,Ft)|0,a=a+Math.imul(ct,Nt)|0,v=v+Math.imul(ct,Ft)|0,g=g+Math.imul(Z,qt)|0,a=a+Math.imul(Z,Lt)|0,a=a+Math.imul(tt,qt)|0,v=v+Math.imul(tt,Lt)|0,g=g+Math.imul(J,jt)|0,a=a+Math.imul(J,Wt)|0,a=a+Math.imul(F,jt)|0,v=v+Math.imul(F,Wt)|0,g=g+Math.imul(N,Ut)|0,a=a+Math.imul(N,Zt)|0,a=a+Math.imul(j,Ut)|0,v=v+Math.imul(j,Zt)|0;var vi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(vi>>>26)|0,vi&=67108863,g=Math.imul(Mt,It),a=Math.imul(Mt,St),a=a+Math.imul(Pt,It)|0,v=Math.imul(Pt,St),g=g+Math.imul(vt,Et)|0,a=a+Math.imul(vt,Ot)|0,a=a+Math.imul(yt,Et)|0,v=v+Math.imul(yt,Ot)|0,g=g+Math.imul(bt,Rt)|0,a=a+Math.imul(bt,Tt)|0,a=a+Math.imul(wt,Rt)|0,v=v+Math.imul(wt,Tt)|0,g=g+Math.imul(_t,xt)|0,a=a+Math.imul(_t,Ct)|0,a=a+Math.imul(gt,xt)|0,v=v+Math.imul(gt,Ct)|0,g=g+Math.imul(mt,kt)|0,a=a+Math.imul(mt,Dt)|0,a=a+Math.imul(ht,kt)|0,v=v+Math.imul(ht,Dt)|0,g=g+Math.imul(ft,Nt)|0,a=a+Math.imul(ft,Ft)|0,a=a+Math.imul(pt,Nt)|0,v=v+Math.imul(pt,Ft)|0,g=g+Math.imul(dt,qt)|0,a=a+Math.imul(dt,Lt)|0,a=a+Math.imul(ct,qt)|0,v=v+Math.imul(ct,Lt)|0,g=g+Math.imul(Z,jt)|0,a=a+Math.imul(Z,Wt)|0,a=a+Math.imul(tt,jt)|0,v=v+Math.imul(tt,Wt)|0,g=g+Math.imul(J,Ut)|0,a=a+Math.imul(J,Zt)|0,a=a+Math.imul(F,Ut)|0,v=v+Math.imul(F,Zt)|0;var yi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(yi>>>26)|0,yi&=67108863,g=Math.imul(Mt,Et),a=Math.imul(Mt,Ot),a=a+Math.imul(Pt,Et)|0,v=Math.imul(Pt,Ot),g=g+Math.imul(vt,Rt)|0,a=a+Math.imul(vt,Tt)|0,a=a+Math.imul(yt,Rt)|0,v=v+Math.imul(yt,Tt)|0,g=g+Math.imul(bt,xt)|0,a=a+Math.imul(bt,Ct)|0,a=a+Math.imul(wt,xt)|0,v=v+Math.imul(wt,Ct)|0,g=g+Math.imul(_t,kt)|0,a=a+Math.imul(_t,Dt)|0,a=a+Math.imul(gt,kt)|0,v=v+Math.imul(gt,Dt)|0,g=g+Math.imul(mt,Nt)|0,a=a+Math.imul(mt,Ft)|0,a=a+Math.imul(ht,Nt)|0,v=v+Math.imul(ht,Ft)|0,g=g+Math.imul(ft,qt)|0,a=a+Math.imul(ft,Lt)|0,a=a+Math.imul(pt,qt)|0,v=v+Math.imul(pt,Lt)|0,g=g+Math.imul(dt,jt)|0,a=a+Math.imul(dt,Wt)|0,a=a+Math.imul(ct,jt)|0,v=v+Math.imul(ct,Wt)|0,g=g+Math.imul(Z,Ut)|0,a=a+Math.imul(Z,Zt)|0,a=a+Math.imul(tt,Ut)|0,v=v+Math.imul(tt,Zt)|0;var Mi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(Mi>>>26)|0,Mi&=67108863,g=Math.imul(Mt,Rt),a=Math.imul(Mt,Tt),a=a+Math.imul(Pt,Rt)|0,v=Math.imul(Pt,Tt),g=g+Math.imul(vt,xt)|0,a=a+Math.imul(vt,Ct)|0,a=a+Math.imul(yt,xt)|0,v=v+Math.imul(yt,Ct)|0,g=g+Math.imul(bt,kt)|0,a=a+Math.imul(bt,Dt)|0,a=a+Math.imul(wt,kt)|0,v=v+Math.imul(wt,Dt)|0,g=g+Math.imul(_t,Nt)|0,a=a+Math.imul(_t,Ft)|0,a=a+Math.imul(gt,Nt)|0,v=v+Math.imul(gt,Ft)|0,g=g+Math.imul(mt,qt)|0,a=a+Math.imul(mt,Lt)|0,a=a+Math.imul(ht,qt)|0,v=v+Math.imul(ht,Lt)|0,g=g+Math.imul(ft,jt)|0,a=a+Math.imul(ft,Wt)|0,a=a+Math.imul(pt,jt)|0,v=v+Math.imul(pt,Wt)|0,g=g+Math.imul(dt,Ut)|0,a=a+Math.imul(dt,Zt)|0,a=a+Math.imul(ct,Ut)|0,v=v+Math.imul(ct,Zt)|0;var Pi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(Pi>>>26)|0,Pi&=67108863,g=Math.imul(Mt,xt),a=Math.imul(Mt,Ct),a=a+Math.imul(Pt,xt)|0,v=Math.imul(Pt,Ct),g=g+Math.imul(vt,kt)|0,a=a+Math.imul(vt,Dt)|0,a=a+Math.imul(yt,kt)|0,v=v+Math.imul(yt,Dt)|0,g=g+Math.imul(bt,Nt)|0,a=a+Math.imul(bt,Ft)|0,a=a+Math.imul(wt,Nt)|0,v=v+Math.imul(wt,Ft)|0,g=g+Math.imul(_t,qt)|0,a=a+Math.imul(_t,Lt)|0,a=a+Math.imul(gt,qt)|0,v=v+Math.imul(gt,Lt)|0,g=g+Math.imul(mt,jt)|0,a=a+Math.imul(mt,Wt)|0,a=a+Math.imul(ht,jt)|0,v=v+Math.imul(ht,Wt)|0,g=g+Math.imul(ft,Ut)|0,a=a+Math.imul(ft,Zt)|0,a=a+Math.imul(pt,Ut)|0,v=v+Math.imul(pt,Zt)|0;var Bi=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(Bi>>>26)|0,Bi&=67108863,g=Math.imul(Mt,kt),a=Math.imul(Mt,Dt),a=a+Math.imul(Pt,kt)|0,v=Math.imul(Pt,Dt),g=g+Math.imul(vt,Nt)|0,a=a+Math.imul(vt,Ft)|0,a=a+Math.imul(yt,Nt)|0,v=v+Math.imul(yt,Ft)|0,g=g+Math.imul(bt,qt)|0,a=a+Math.imul(bt,Lt)|0,a=a+Math.imul(wt,qt)|0,v=v+Math.imul(wt,Lt)|0,g=g+Math.imul(_t,jt)|0,a=a+Math.imul(_t,Wt)|0,a=a+Math.imul(gt,jt)|0,v=v+Math.imul(gt,Wt)|0,g=g+Math.imul(mt,Ut)|0,a=a+Math.imul(mt,Zt)|0,a=a+Math.imul(ht,Ut)|0,v=v+Math.imul(ht,Zt)|0;var Ai=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(Ai>>>26)|0,Ai&=67108863,g=Math.imul(Mt,Nt),a=Math.imul(Mt,Ft),a=a+Math.imul(Pt,Nt)|0,v=Math.imul(Pt,Ft),g=g+Math.imul(vt,qt)|0,a=a+Math.imul(vt,Lt)|0,a=a+Math.imul(yt,qt)|0,v=v+Math.imul(yt,Lt)|0,g=g+Math.imul(bt,jt)|0,a=a+Math.imul(bt,Wt)|0,a=a+Math.imul(wt,jt)|0,v=v+Math.imul(wt,Wt)|0,g=g+Math.imul(_t,Ut)|0,a=a+Math.imul(_t,Zt)|0,a=a+Math.imul(gt,Ut)|0,v=v+Math.imul(gt,Zt)|0;var Ii=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(Ii>>>26)|0,Ii&=67108863,g=Math.imul(Mt,qt),a=Math.imul(Mt,Lt),a=a+Math.imul(Pt,qt)|0,v=Math.imul(Pt,Lt),g=g+Math.imul(vt,jt)|0,a=a+Math.imul(vt,Wt)|0,a=a+Math.imul(yt,jt)|0,v=v+Math.imul(yt,Wt)|0,g=g+Math.imul(bt,Ut)|0,a=a+Math.imul(bt,Zt)|0,a=a+Math.imul(wt,Ut)|0,v=v+Math.imul(wt,Zt)|0;var Si=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(Si>>>26)|0,Si&=67108863,g=Math.imul(Mt,jt),a=Math.imul(Mt,Wt),a=a+Math.imul(Pt,jt)|0,v=Math.imul(Pt,Wt),g=g+Math.imul(vt,Ut)|0,a=a+Math.imul(vt,Zt)|0,a=a+Math.imul(yt,Ut)|0,v=v+Math.imul(yt,Zt)|0;var Ei=(B+g|0)+((a&8191)<<13)|0;B=(v+(a>>>13)|0)+(Ei>>>26)|0,Ei&=67108863,g=Math.imul(Mt,Ut),a=Math.imul(Mt,Zt),a=a+Math.imul(Pt,Ut)|0,v=Math.imul(Pt,Zt);var Oi=(B+g|0)+((a&8191)<<13)|0;return B=(v+(a>>>13)|0)+(Oi>>>26)|0,Oi&=67108863,M[0]=ci,M[1]=fi,M[2]=pi,M[3]=mi,M[4]=hi,M[5]=_i,M[6]=gi,M[7]=bi,M[8]=wi,M[9]=vi,M[10]=yi,M[11]=Mi,M[12]=Pi,M[13]=Bi,M[14]=Ai,M[15]=Ii,M[16]=Si,M[17]=Ei,M[18]=Oi,B!==0&&(M[19]=B,m.length++),m};Math.imul||(T=E);function L(y,s,u){u.negative=s.negative^y.negative,u.length=y.length+s.length;for(var m=0,h=0,b=0;b<u.length-1;b++){var M=h;h=0;for(var B=m&67108863,g=Math.min(b,s.length-1),a=Math.max(0,b-y.length+1);a<=g;a++){var v=b-a,D=y.words[v]|0,N=s.words[a]|0,j=D*N,X=j&67108863;M=M+(j/67108864|0)|0,X=X+B|0,B=X&67108863,M=M+(X>>>26)|0,h+=M>>>26,M&=67108863}u.words[b]=B,m=M,M=h}return m!==0?u.words[b]=m:u.length--,u._strip()}function R(y,s,u){return L(y,s,u)}i.prototype.mulTo=function(s,u){var m,h=this.length+s.length;return this.length===10&&s.length===10?m=T(this,s,u):h<63?m=E(this,s,u):h<1024?m=L(this,s,u):m=R(this,s,u),m};function C(y,s){this.x=y,this.y=s}C.prototype.makeRBT=function(s){for(var u=new Array(s),m=i.prototype._countBits(s)-1,h=0;h<s;h++)u[h]=this.revBin(h,m,s);return u},C.prototype.revBin=function(s,u,m){if(s===0||s===m-1)return s;for(var h=0,b=0;b<u;b++)h|=(s&1)<<u-b-1,s>>=1;return h},C.prototype.permute=function(s,u,m,h,b,M){for(var B=0;B<M;B++)h[B]=u[s[B]],b[B]=m[s[B]]},C.prototype.transform=function(s,u,m,h,b,M){this.permute(M,s,u,m,h,b);for(var B=1;B<b;B<<=1)for(var g=B<<1,a=Math.cos(2*Math.PI/g),v=Math.sin(2*Math.PI/g),D=0;D<b;D+=g)for(var N=a,j=v,X=0;X<B;X++){var J=m[D+X],F=h[D+X],it=m[D+X+B],Z=h[D+X+B],tt=N*it-j*Z;Z=N*Z+j*it,it=tt,m[D+X]=J+it,h[D+X]=F+Z,m[D+X+B]=J-it,h[D+X+B]=F-Z,X!==g&&(tt=a*N-v*j,j=a*j+v*N,N=tt)}},C.prototype.guessLen13b=function(s,u){var m=Math.max(u,s)|1,h=m&1,b=0;for(m=m/2|0;m;m=m>>>1)b++;return 1<<b+1+h},C.prototype.conjugate=function(s,u,m){if(!(m<=1))for(var h=0;h<m/2;h++){var b=s[h];s[h]=s[m-h-1],s[m-h-1]=b,b=u[h],u[h]=-u[m-h-1],u[m-h-1]=-b}},C.prototype.normalize13b=function(s,u){for(var m=0,h=0;h<u/2;h++){var b=Math.round(s[2*h+1]/u)*8192+Math.round(s[2*h]/u)+m;s[h]=b&67108863,b<67108864?m=0:m=b/67108864|0}return s},C.prototype.convert13b=function(s,u,m,h){for(var b=0,M=0;M<u;M++)b=b+(s[M]|0),m[2*M]=b&8191,b=b>>>13,m[2*M+1]=b&8191,b=b>>>13;for(M=2*u;M<h;++M)m[M]=0;t(b===0),t((b&-8192)===0)},C.prototype.stub=function(s){for(var u=new Array(s),m=0;m<s;m++)u[m]=0;return u},C.prototype.mulp=function(s,u,m){var h=2*this.guessLen13b(s.length,u.length),b=this.makeRBT(h),M=this.stub(h),B=new Array(h),g=new Array(h),a=new Array(h),v=new Array(h),D=new Array(h),N=new Array(h),j=m.words;j.length=h,this.convert13b(s.words,s.length,B,h),this.convert13b(u.words,u.length,v,h),this.transform(B,M,g,a,h,b),this.transform(v,M,D,N,h,b);for(var X=0;X<h;X++){var J=g[X]*D[X]-a[X]*N[X];a[X]=g[X]*N[X]+a[X]*D[X],g[X]=J}return this.conjugate(g,a,h),this.transform(g,a,j,M,h,b),this.conjugate(j,M,h),this.normalize13b(j,h),m.negative=s.negative^u.negative,m.length=s.length+u.length,m._strip()},i.prototype.mul=function(s){var u=new i(null);return u.words=new Array(this.length+s.length),this.mulTo(s,u)},i.prototype.mulf=function(s){var u=new i(null);return u.words=new Array(this.length+s.length),R(this,s,u)},i.prototype.imul=function(s){return this.clone().mulTo(s,this)},i.prototype.imuln=function(s){var u=s<0;u&&(s=-s),t(typeof s=="number"),t(s<67108864);for(var m=0,h=0;h<this.length;h++){var b=(this.words[h]|0)*s,M=(b&67108863)+(m&67108863);m>>=26,m+=b/67108864|0,m+=M>>>26,this.words[h]=M&67108863}return m!==0&&(this.words[h]=m,this.length++),this.length=s===0?1:this.length,u?this.ineg():this},i.prototype.muln=function(s){return this.clone().imuln(s)},i.prototype.sqr=function(){return this.mul(this)},i.prototype.isqr=function(){return this.imul(this.clone())},i.prototype.pow=function(s){var u=I(s);if(u.length===0)return new i(1);for(var m=this,h=0;h<u.length&&u[h]===0;h++,m=m.sqr());if(++h<u.length)for(var b=m.sqr();h<u.length;h++,b=b.sqr())u[h]!==0&&(m=m.mul(b));return m},i.prototype.iushln=function(s){t(typeof s=="number"&&s>=0);var u=s%26,m=(s-u)/26,h=67108863>>>26-u<<26-u,b;if(u!==0){var M=0;for(b=0;b<this.length;b++){var B=this.words[b]&h,g=(this.words[b]|0)-B<<u;this.words[b]=g|M,M=B>>>26-u}M&&(this.words[b]=M,this.length++)}if(m!==0){for(b=this.length-1;b>=0;b--)this.words[b+m]=this.words[b];for(b=0;b<m;b++)this.words[b]=0;this.length+=m}return this._strip()},i.prototype.ishln=function(s){return t(this.negative===0),this.iushln(s)},i.prototype.iushrn=function(s,u,m){t(typeof s=="number"&&s>=0);var h;u?h=(u-u%26)/26:h=0;var b=s%26,M=Math.min((s-b)/26,this.length),B=67108863^67108863>>>b<<b,g=m;if(h-=M,h=Math.max(0,h),g){for(var a=0;a<M;a++)g.words[a]=this.words[a];g.length=M}if(M!==0)if(this.length>M)for(this.length-=M,a=0;a<this.length;a++)this.words[a]=this.words[a+M];else this.words[0]=0,this.length=1;var v=0;for(a=this.length-1;a>=0&&(v!==0||a>=h);a--){var D=this.words[a]|0;this.words[a]=v<<26-b|D>>>b,v=D&B}return g&&v!==0&&(g.words[g.length++]=v),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},i.prototype.ishrn=function(s,u,m){return t(this.negative===0),this.iushrn(s,u,m)},i.prototype.shln=function(s){return this.clone().ishln(s)},i.prototype.ushln=function(s){return this.clone().iushln(s)},i.prototype.shrn=function(s){return this.clone().ishrn(s)},i.prototype.ushrn=function(s){return this.clone().iushrn(s)},i.prototype.testn=function(s){t(typeof s=="number"&&s>=0);var u=s%26,m=(s-u)/26,h=1<<u;if(this.length<=m)return!1;var b=this.words[m];return!!(b&h)},i.prototype.imaskn=function(s){t(typeof s=="number"&&s>=0);var u=s%26,m=(s-u)/26;if(t(this.negative===0,"imaskn works only with positive numbers"),this.length<=m)return this;if(u!==0&&m++,this.length=Math.min(m,this.length),u!==0){var h=67108863^67108863>>>u<<u;this.words[this.length-1]&=h}return this.length===0&&(this.words[0]=0,this.length=1),this._strip()},i.prototype.maskn=function(s){return this.clone().imaskn(s)},i.prototype.iaddn=function(s){return t(typeof s=="number"),t(s<67108864),s<0?this.isubn(-s):this.negative!==0?this.length===1&&(this.words[0]|0)<=s?(this.words[0]=s-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(s),this.negative=1,this):this._iaddn(s)},i.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},i.prototype.isubn=function(s){if(t(typeof s=="number"),t(s<67108864),s<0)return this.iaddn(-s);if(this.negative!==0)return this.negative=0,this.iaddn(s),this.negative=1,this;if(this.words[0]-=s,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var u=0;u<this.length&&this.words[u]<0;u++)this.words[u]+=67108864,this.words[u+1]-=1;return this._strip()},i.prototype.addn=function(s){return this.clone().iaddn(s)},i.prototype.subn=function(s){return this.clone().isubn(s)},i.prototype.iabs=function(){return this.negative=0,this},i.prototype.abs=function(){return this.clone().iabs()},i.prototype._ishlnsubmul=function(s,u,m){var h=s.length+m,b;this._expand(h);var M,B=0;for(b=0;b<s.length;b++){M=(this.words[b+m]|0)+B;var g=(s.words[b]|0)*u;M-=g&67108863,B=(M>>26)-(g/67108864|0),this.words[b+m]=M&67108863}for(;b<this.length-m;b++)M=(this.words[b+m]|0)+B,B=M>>26,this.words[b+m]=M&67108863;if(B===0)return this._strip();for(t(B===-1),B=0,b=0;b<this.length;b++)M=-(this.words[b]|0)+B,B=M>>26,this.words[b]=M&67108863;return this.negative=1,this._strip()},i.prototype._wordDiv=function(s,u){var m=this.length-s.length,h=this.clone(),b=s,M=b.words[b.length-1]|0,B=this._countBits(M);m=26-B,m!==0&&(b=b.ushln(m),h.iushln(m),M=b.words[b.length-1]|0);var g=h.length-b.length,a;if(u!=="mod"){a=new i(null),a.length=g+1,a.words=new Array(a.length);for(var v=0;v<a.length;v++)a.words[v]=0}var D=h.clone()._ishlnsubmul(b,1,g);D.negative===0&&(h=D,a&&(a.words[g]=1));for(var N=g-1;N>=0;N--){var j=(h.words[b.length+N]|0)*67108864+(h.words[b.length+N-1]|0);for(j=Math.min(j/M|0,67108863),h._ishlnsubmul(b,j,N);h.negative!==0;)j--,h.negative=0,h._ishlnsubmul(b,1,N),h.isZero()||(h.negative^=1);a&&(a.words[N]=j)}return a&&a._strip(),h._strip(),u!=="div"&&m!==0&&h.iushrn(m),{div:a||null,mod:h}},i.prototype.divmod=function(s,u,m){if(t(!s.isZero()),this.isZero())return{div:new i(0),mod:new i(0)};var h,b,M;return this.negative!==0&&s.negative===0?(M=this.neg().divmod(s,u),u!=="mod"&&(h=M.div.neg()),u!=="div"&&(b=M.mod.neg(),m&&b.negative!==0&&b.iadd(s)),{div:h,mod:b}):this.negative===0&&s.negative!==0?(M=this.divmod(s.neg(),u),u!=="mod"&&(h=M.div.neg()),{div:h,mod:M.mod}):(this.negative&s.negative)!==0?(M=this.neg().divmod(s.neg(),u),u!=="div"&&(b=M.mod.neg(),m&&b.negative!==0&&b.isub(s)),{div:M.div,mod:b}):s.length>this.length||this.cmp(s)<0?{div:new i(0),mod:this}:s.length===1?u==="div"?{div:this.divn(s.words[0]),mod:null}:u==="mod"?{div:null,mod:new i(this.modrn(s.words[0]))}:{div:this.divn(s.words[0]),mod:new i(this.modrn(s.words[0]))}:this._wordDiv(s,u)},i.prototype.div=function(s){return this.divmod(s,"div",!1).div},i.prototype.mod=function(s){return this.divmod(s,"mod",!1).mod},i.prototype.umod=function(s){return this.divmod(s,"mod",!0).mod},i.prototype.divRound=function(s){var u=this.divmod(s);if(u.mod.isZero())return u.div;var m=u.div.negative!==0?u.mod.isub(s):u.mod,h=s.ushrn(1),b=s.andln(1),M=m.cmp(h);return M<0||b===1&&M===0?u.div:u.div.negative!==0?u.div.isubn(1):u.div.iaddn(1)},i.prototype.modrn=function(s){var u=s<0;u&&(s=-s),t(s<=67108863);for(var m=(1<<26)%s,h=0,b=this.length-1;b>=0;b--)h=(m*h+(this.words[b]|0))%s;return u?-h:h},i.prototype.modn=function(s){return this.modrn(s)},i.prototype.idivn=function(s){var u=s<0;u&&(s=-s),t(s<=67108863);for(var m=0,h=this.length-1;h>=0;h--){var b=(this.words[h]|0)+m*67108864;this.words[h]=b/s|0,m=b%s}return this._strip(),u?this.ineg():this},i.prototype.divn=function(s){return this.clone().idivn(s)},i.prototype.egcd=function(s){t(s.negative===0),t(!s.isZero());var u=this,m=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var h=new i(1),b=new i(0),M=new i(0),B=new i(1),g=0;u.isEven()&&m.isEven();)u.iushrn(1),m.iushrn(1),++g;for(var a=m.clone(),v=u.clone();!u.isZero();){for(var D=0,N=1;(u.words[0]&N)===0&&D<26;++D,N<<=1);if(D>0)for(u.iushrn(D);D-- >0;)(h.isOdd()||b.isOdd())&&(h.iadd(a),b.isub(v)),h.iushrn(1),b.iushrn(1);for(var j=0,X=1;(m.words[0]&X)===0&&j<26;++j,X<<=1);if(j>0)for(m.iushrn(j);j-- >0;)(M.isOdd()||B.isOdd())&&(M.iadd(a),B.isub(v)),M.iushrn(1),B.iushrn(1);u.cmp(m)>=0?(u.isub(m),h.isub(M),b.isub(B)):(m.isub(u),M.isub(h),B.isub(b))}return{a:M,b:B,gcd:m.iushln(g)}},i.prototype._invmp=function(s){t(s.negative===0),t(!s.isZero());var u=this,m=s.clone();u.negative!==0?u=u.umod(s):u=u.clone();for(var h=new i(1),b=new i(0),M=m.clone();u.cmpn(1)>0&&m.cmpn(1)>0;){for(var B=0,g=1;(u.words[0]&g)===0&&B<26;++B,g<<=1);if(B>0)for(u.iushrn(B);B-- >0;)h.isOdd()&&h.iadd(M),h.iushrn(1);for(var a=0,v=1;(m.words[0]&v)===0&&a<26;++a,v<<=1);if(a>0)for(m.iushrn(a);a-- >0;)b.isOdd()&&b.iadd(M),b.iushrn(1);u.cmp(m)>=0?(u.isub(m),h.isub(b)):(m.isub(u),b.isub(h))}var D;return u.cmpn(1)===0?D=h:D=b,D.cmpn(0)<0&&D.iadd(s),D},i.prototype.gcd=function(s){if(this.isZero())return s.abs();if(s.isZero())return this.abs();var u=this.clone(),m=s.clone();u.negative=0,m.negative=0;for(var h=0;u.isEven()&&m.isEven();h++)u.iushrn(1),m.iushrn(1);do{for(;u.isEven();)u.iushrn(1);for(;m.isEven();)m.iushrn(1);var b=u.cmp(m);if(b<0){var M=u;u=m,m=M}else if(b===0||m.cmpn(1)===0)break;u.isub(m)}while(!0);return m.iushln(h)},i.prototype.invm=function(s){return this.egcd(s).a.umod(s)},i.prototype.isEven=function(){return(this.words[0]&1)===0},i.prototype.isOdd=function(){return(this.words[0]&1)===1},i.prototype.andln=function(s){return this.words[0]&s},i.prototype.bincn=function(s){t(typeof s=="number");var u=s%26,m=(s-u)/26,h=1<<u;if(this.length<=m)return this._expand(m+1),this.words[m]|=h,this;for(var b=h,M=m;b!==0&&M<this.length;M++){var B=this.words[M]|0;B+=b,b=B>>>26,B&=67108863,this.words[M]=B}return b!==0&&(this.words[M]=b,this.length++),this},i.prototype.isZero=function(){return this.length===1&&this.words[0]===0},i.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 m;if(this.length>1)m=1;else{u&&(s=-s),t(s<=67108863,"Number is too big");var h=this.words[0]|0;m=h===s?0:h<s?-1:1}return this.negative!==0?-m|0:m},i.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},i.prototype.ucmp=function(s){if(this.length>s.length)return 1;if(this.length<s.length)return-1;for(var u=0,m=this.length-1;m>=0;m--){var h=this.words[m]|0,b=s.words[m]|0;if(h!==b){h<b?u=-1:h>b&&(u=1);break}}return u},i.prototype.gtn=function(s){return this.cmpn(s)===1},i.prototype.gt=function(s){return this.cmp(s)===1},i.prototype.gten=function(s){return this.cmpn(s)>=0},i.prototype.gte=function(s){return this.cmp(s)>=0},i.prototype.ltn=function(s){return this.cmpn(s)===-1},i.prototype.lt=function(s){return this.cmp(s)===-1},i.prototype.lten=function(s){return this.cmpn(s)<=0},i.prototype.lte=function(s){return this.cmp(s)<=0},i.prototype.eqn=function(s){return this.cmpn(s)===0},i.prototype.eq=function(s){return this.cmp(s)===0},i.red=function(s){return new U(s)},i.prototype.toRed=function(s){return t(!this.red,"Already a number in reduction context"),t(this.negative===0,"red works only with positives"),s.convertTo(this)._forceRed(s)},i.prototype.fromRed=function(){return t(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},i.prototype._forceRed=function(s){return this.red=s,this},i.prototype.forceRed=function(s){return t(!this.red,"Already a number in reduction context"),this._forceRed(s)},i.prototype.redAdd=function(s){return t(this.red,"redAdd works only with red numbers"),this.red.add(this,s)},i.prototype.redIAdd=function(s){return t(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,s)},i.prototype.redSub=function(s){return t(this.red,"redSub works only with red numbers"),this.red.sub(this,s)},i.prototype.redISub=function(s){return t(this.red,"redISub works only with red numbers"),this.red.isub(this,s)},i.prototype.redShl=function(s){return t(this.red,"redShl works only with red numbers"),this.red.shl(this,s)},i.prototype.redMul=function(s){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.mul(this,s)},i.prototype.redIMul=function(s){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,s),this.red.imul(this,s)},i.prototype.redSqr=function(){return t(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},i.prototype.redISqr=function(){return t(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},i.prototype.redSqrt=function(){return t(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},i.prototype.redInvm=function(){return t(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},i.prototype.redNeg=function(){return t(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},i.prototype.redPow=function(s){return t(this.red&&!s.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,s)};var k={k256:null,p224:null,p192:null,p25519:null};function x(y,s){this.name=y,this.p=new i(s,16),this.n=this.p.bitLength(),this.k=new i(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}x.prototype._tmp=function(){var s=new i(null);return s.words=new Array(Math.ceil(this.n/13)),s},x.prototype.ireduce=function(s){var u=s,m;do this.split(u,this.tmp),u=this.imulK(u),u=u.iadd(this.tmp),m=u.bitLength();while(m>this.n);var h=m<this.n?-1:u.ucmp(this.p);return h===0?(u.words[0]=0,u.length=1):h>0?u.isub(this.p):u.strip!==void 0?u.strip():u._strip(),u},x.prototype.split=function(s,u){s.iushrn(this.n,0,u)},x.prototype.imulK=function(s){return s.imul(this.k)};function K(){x.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}n(K,x),K.prototype.split=function(s,u){for(var m=4194303,h=Math.min(s.length,9),b=0;b<h;b++)u.words[b]=s.words[b];if(u.length=h,s.length<=9){s.words[0]=0,s.length=1;return}var M=s.words[9];for(u.words[u.length++]=M&m,b=10;b<s.length;b++){var B=s.words[b]|0;s.words[b-10]=(B&m)<<4|M>>>22,M=B}M>>>=22,s.words[b-10]=M,M===0&&s.length>10?s.length-=10:s.length-=9},K.prototype.imulK=function(s){s.words[s.length]=0,s.words[s.length+1]=0,s.length+=2;for(var u=0,m=0;m<s.length;m++){var h=s.words[m]|0;u+=h*977,s.words[m]=u&67108863,u=h*64+(u/67108864|0)}return s.words[s.length-1]===0&&(s.length--,s.words[s.length-1]===0&&s.length--),s};function rt(){x.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}n(rt,x);function at(){x.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}n(at,x);function nt(){x.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}n(nt,x),nt.prototype.imulK=function(s){for(var u=0,m=0;m<s.length;m++){var h=(s.words[m]|0)*19+u,b=h&67108863;h>>>=26,s.words[m]=b,u=h}return u!==0&&(s.words[s.length++]=u),s},i._prime=function(s){if(k[s])return k[s];var u;if(s==="k256")u=new K;else if(s==="p224")u=new rt;else if(s==="p192")u=new at;else if(s==="p25519")u=new nt;else throw new Error("Unknown prime "+s);return k[s]=u,u};function U(y){if(typeof y=="string"){var s=i._prime(y);this.m=s.p,this.prime=s}else t(y.gtn(1),"modulus must be greater than 1"),this.m=y,this.prime=null}U.prototype._verify1=function(s){t(s.negative===0,"red works only with positives"),t(s.red,"red works only with red numbers")},U.prototype._verify2=function(s,u){t((s.negative|u.negative)===0,"red works only with positives"),t(s.red&&s.red===u.red,"red works only with red numbers")},U.prototype.imod=function(s){return this.prime?this.prime.ireduce(s)._forceRed(this):(c(s,s.umod(this.m)._forceRed(this)),s)},U.prototype.neg=function(s){return s.isZero()?s.clone():this.m.sub(s)._forceRed(this)},U.prototype.add=function(s,u){this._verify2(s,u);var m=s.add(u);return m.cmp(this.m)>=0&&m.isub(this.m),m._forceRed(this)},U.prototype.iadd=function(s,u){this._verify2(s,u);var m=s.iadd(u);return m.cmp(this.m)>=0&&m.isub(this.m),m},U.prototype.sub=function(s,u){this._verify2(s,u);var m=s.sub(u);return m.cmpn(0)<0&&m.iadd(this.m),m._forceRed(this)},U.prototype.isub=function(s,u){this._verify2(s,u);var m=s.isub(u);return m.cmpn(0)<0&&m.iadd(this.m),m},U.prototype.shl=function(s,u){return this._verify1(s),this.imod(s.ushln(u))},U.prototype.imul=function(s,u){return this._verify2(s,u),this.imod(s.imul(u))},U.prototype.mul=function(s,u){return this._verify2(s,u),this.imod(s.mul(u))},U.prototype.isqr=function(s){return this.imul(s,s.clone())},U.prototype.sqr=function(s){return this.mul(s,s)},U.prototype.sqrt=function(s){if(s.isZero())return s.clone();var u=this.m.andln(3);if(t(u%2===1),u===3){var m=this.m.add(new i(1)).iushrn(2);return this.pow(s,m)}for(var h=this.m.subn(1),b=0;!h.isZero()&&h.andln(1)===0;)b++,h.iushrn(1);t(!h.isZero());var M=new i(1).toRed(this),B=M.redNeg(),g=this.m.subn(1).iushrn(1),a=this.m.bitLength();for(a=new i(2*a*a).toRed(this);this.pow(a,g).cmp(B)!==0;)a.redIAdd(B);for(var v=this.pow(a,h),D=this.pow(s,h.addn(1).iushrn(1)),N=this.pow(s,h),j=b;N.cmp(M)!==0;){for(var X=N,J=0;X.cmp(M)!==0;J++)X=X.redSqr();t(J<j);var F=this.pow(v,new i(1).iushln(j-J-1));D=D.redMul(F),v=F.redSqr(),N=N.redMul(v),j=J}return D},U.prototype.invm=function(s){var u=s._invmp(this.m);return u.negative!==0?(u.negative=0,this.imod(u).redNeg()):this.imod(u)},U.prototype.pow=function(s,u){if(u.isZero())return new i(1).toRed(this);if(u.cmpn(1)===0)return s.clone();var m=4,h=new Array(1<<m);h[0]=new i(1).toRed(this),h[1]=s;for(var b=2;b<h.length;b++)h[b]=this.mul(h[b-1],s);var M=h[0],B=0,g=0,a=u.bitLength()%26;for(a===0&&(a=26),b=u.length-1;b>=0;b--){for(var v=u.words[b],D=a-1;D>=0;D--){var N=v>>D&1;if(M!==h[0]&&(M=this.sqr(M)),N===0&&B===0){g=0;continue}B<<=1,B|=N,g++,!(g!==m&&(b!==0||D!==0))&&(M=this.mul(M,h[B]),g=0,B=0)}a=26}return M},U.prototype.convertTo=function(s){var u=s.umod(this.m);return u===s?u.clone():u},U.prototype.convertFrom=function(s){var u=s.clone();return u.red=null,u},i.mont=function(s){return new ot(s)};function ot(y){U.call(this,y),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new i(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)}n(ot,U),ot.prototype.convertTo=function(s){return this.imod(s.ushln(this.shift))},ot.prototype.convertFrom=function(s){var u=this.imod(s.mul(this.rinv));return u.red=null,u},ot.prototype.imul=function(s,u){if(s.isZero()||u.isZero())return s.words[0]=0,s.length=1,s;var m=s.imul(u),h=m.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),b=m.isub(h).iushrn(this.shift),M=b;return b.cmp(this.m)>=0?M=b.isub(this.m):b.cmpn(0)<0&&(M=b.iadd(this.m)),M._forceRed(this)},ot.prototype.mul=function(s,u){if(s.isZero()||u.isZero())return new i(0)._forceRed(this);var m=s.mul(u),h=m.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),b=m.isub(h).iushrn(this.shift),M=b;return b.cmp(this.m)>=0?M=b.isub(this.m):b.cmpn(0)<0&&(M=b.iadd(this.m)),M._forceRed(this)},ot.prototype.invm=function(s){var u=this.imod(s._invmp(this.m).mul(this.r2));return u._forceRed(this)}})(typeof Ti>"u"||Ti,hn)});import{SdkWrapper as Wo}from"@cetusprotocol/common-sdk";import{FullRpcUrlMainnet as vr,GraphRpcUrlMainnet as yr}from"@cetusprotocol/common-sdk";var mn={env:"mainnet",full_rpc_url:vr,graph_rpc_url:yr,dlmm_pool:{package_id:"0x5664f9d3fd82c84023870cfbda8ea84e14c8dd56ce557ad2116e0668581a682b",published_at:"0x6b989f9b05bd11a00f1c7779443d66802c5e71631f59e87be5d2a616991f151e",version:10,config:{registry_id:"0xb1d55e7d895823c65f98d99b81a69436cf7d1638629c9ccb921326039cda1f1b",pools_id:"0xc3683b2356cac6423e9ecaea20955c7cc193998b016e5b884730ed1192174991",global_config_id:"0xf31b605d117f959b9730e8c07b08b856cb05143c5e81d5751c90d2979e82f599",versioned_id:"0x05370b2d656612dd5759cbe80463de301e3b94a921dfc72dd9daa2ecdeb2d0a8",admin_cap_id:"0xc4c42bc31cb54beb679dccd547f8bdb970cb6dc989bd1f85a4fed4812ed95d6e",partners_id:"0x5c0affc8d363b6abb1f32790c229165215f4edead89a9bc7cd95dad717b4296a"}},dlmm_router:{package_id:"0x8d389fa25cb08ebc5e520bc520ed394eed9e62b56b7868acb398bf298b8a76f3",published_at:"0x8937cf16447e9fd8f0db24f1e05364be0422585e0954b86afb18693510b6ca5b",version:7}};import{Transaction as ti}from"@mysten/sui/transactions";import{asUintN as tr,buildSuiGrpcClient as So,CLOCK_ADDRESS as Eo,createFullClient as Oo,d as Hi,deriveDynamicFieldIdByType as Ro,DETAILS_KEYS as ie,fixCoinType as er,getPackagerConfigs as ir,isSortedSymbols as To}from"@cetusprotocol/common-sdk";import{BaseError as Mr}from"@cetusprotocol/common-sdk";var ye=class extends Mr{constructor(e,t,n){super(e,t||"UnknownError",n)}static isDlmmErrorCode(e,t){return this.isErrorCode(e,t)}},z=(r,e,t)=>{let n={...t,stack:e instanceof Error?e.stack:void 0};throw e instanceof Error?new ye(e.message,r,n):new ye(e,r,n)};var Un=Ri(je(),1);import{asIntN as Oe,d as Ne,DETAILS_KEYS as ge,extractStructTagFromType as ji,fixCoinType as ae,MathUtil as vo,OptionU64Raw as jn,TypeNameRaw as Li}from"@cetusprotocol/common-sdk";var Be=9e15,me=1e9,xi="0123456789abcdef",Ze="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",Ge="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Ci={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Be,maxE:Be,crypto:!1},wn,ue,$=!0,Qe="[DecimalError] ",pe=Qe+"Invalid argument: ",vn=Qe+"Precision limit exceeded",yn=Qe+"crypto unavailable",Mn="[object Decimal]",Kt=Math.floor,Gt=Math.pow,Pr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Br=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Ar=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,Pn=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,te=1e7,H=7,Ir=9007199254740991,Sr=Ze.length-1,ki=Ge.length-1,S={toStringTag:Mn};S.absoluteValue=S.abs=function(){var r=new this.constructor(this);return r.s<0&&(r.s=1),W(r)};S.ceil=function(){return W(new this.constructor(this),this.e+1,2)};S.clampedTo=S.clamp=function(r,e){var t,n=this,i=n.constructor;if(r=new i(r),e=new i(e),!r.s||!e.s)return new i(NaN);if(r.gt(e))throw Error(pe+e);return t=n.cmp(r),t<0?r:n.cmp(e)>0?e:new i(n)};S.comparedTo=S.cmp=function(r){var e,t,n,i,o=this,l=o.d,d=(r=new o.constructor(r)).d,f=o.s,c=r.s;if(!l||!d)return!f||!c?NaN:f!==c?f:l===d?0:!l^f<0?1:-1;if(!l[0]||!d[0])return l[0]?f:d[0]?-c:0;if(f!==c)return f;if(o.e!==r.e)return o.e>r.e^f<0?1:-1;for(n=l.length,i=d.length,e=0,t=n<i?n:i;e<t;++e)if(l[e]!==d[e])return l[e]>d[e]^f<0?1:-1;return n===i?0:n>i^f<0?1:-1};S.cosine=S.cos=function(){var r,e,t=this,n=t.constructor;return t.d?t.d[0]?(r=n.precision,e=n.rounding,n.precision=r+Math.max(t.e,t.sd())+H,n.rounding=1,t=Er(n,En(n,t)),n.precision=r,n.rounding=e,W(ue==2||ue==3?t.neg():t,r,e,!0)):new n(1):new n(NaN)};S.cubeRoot=S.cbrt=function(){var r,e,t,n,i,o,l,d,f,c,p=this,_=p.constructor;if(!p.isFinite()||p.isZero())return new _(p);for($=!1,o=p.s*Gt(p.s*p,1/3),!o||Math.abs(o)==1/0?(t=Qt(p.d),r=p.e,(o=(r-t.length+1)%3)&&(t+=o==1||o==-2?"0":"00"),o=Gt(t,1/3),r=Kt((r+1)/3)-(r%3==(r<0?-1:2)),o==1/0?t="5e"+r:(t=o.toExponential(),t=t.slice(0,t.indexOf("e")+1)+r),n=new _(t),n.s=p.s):n=new _(o.toString()),l=(r=_.precision)+3;;)if(d=n,f=d.times(d).times(d),c=f.plus(p),n=st(c.plus(p).times(d),c.plus(f),l+2,1),Qt(d.d).slice(0,l)===(t=Qt(n.d)).slice(0,l))if(t=t.slice(l-3,l+1),t=="9999"||!i&&t=="4999"){if(!i&&(W(d,r+1,0),d.times(d).times(d).eq(p))){n=d;break}l+=4,i=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(W(n,r+1,1),e=!n.times(n).times(n).eq(p));break}return $=!0,W(n,r,_.rounding,e)};S.decimalPlaces=S.dp=function(){var r,e=this.d,t=NaN;if(e){if(r=e.length-1,t=(r-Kt(this.e/H))*H,r=e[r],r)for(;r%10==0;r/=10)t--;t<0&&(t=0)}return t};S.dividedBy=S.div=function(r){return st(this,new this.constructor(r))};S.dividedToIntegerBy=S.divToInt=function(r){var e=this,t=e.constructor;return W(st(e,new t(r),0,1,1),t.precision,t.rounding)};S.equals=S.eq=function(r){return this.cmp(r)===0};S.floor=function(){return W(new this.constructor(this),this.e+1,3)};S.greaterThan=S.gt=function(r){return this.cmp(r)>0};S.greaterThanOrEqualTo=S.gte=function(r){var e=this.cmp(r);return e==1||e===0};S.hyperbolicCosine=S.cosh=function(){var r,e,t,n,i,o=this,l=o.constructor,d=new l(1);if(!o.isFinite())return new l(o.s?1/0:NaN);if(o.isZero())return d;t=l.precision,n=l.rounding,l.precision=t+Math.max(o.e,o.sd())+4,l.rounding=1,i=o.d.length,i<32?(r=Math.ceil(i/3),e=(1/Ve(4,r)).toString()):(r=16,e="2.3283064365386962890625e-10"),o=Ae(l,1,o.times(e),new l(1),!0);for(var f,c=r,p=new l(8);c--;)f=o.times(o),o=d.minus(f.times(p.minus(f.times(p))));return W(o,l.precision=t,l.rounding=n,!0)};S.hyperbolicSine=S.sinh=function(){var r,e,t,n,i=this,o=i.constructor;if(!i.isFinite()||i.isZero())return new o(i);if(e=o.precision,t=o.rounding,o.precision=e+Math.max(i.e,i.sd())+4,o.rounding=1,n=i.d.length,n<3)i=Ae(o,2,i,i,!0);else{r=1.4*Math.sqrt(n),r=r>16?16:r|0,i=i.times(1/Ve(5,r)),i=Ae(o,2,i,i,!0);for(var l,d=new o(5),f=new o(16),c=new o(20);r--;)l=i.times(i),i=i.times(d.plus(l.times(f.times(l).plus(c))))}return o.precision=e,o.rounding=t,W(i,e,t,!0)};S.hyperbolicTangent=S.tanh=function(){var r,e,t=this,n=t.constructor;return t.isFinite()?t.isZero()?new n(t):(r=n.precision,e=n.rounding,n.precision=r+7,n.rounding=1,st(t.sinh(),t.cosh(),n.precision=r,n.rounding=e)):new n(t.s)};S.inverseCosine=S.acos=function(){var r=this,e=r.constructor,t=r.abs().cmp(1),n=e.precision,i=e.rounding;return t!==-1?t===0?r.isNeg()?re(e,n,i):new e(0):new e(NaN):r.isZero()?re(e,n+4,i).times(.5):(e.precision=n+6,e.rounding=1,r=new e(1).minus(r).div(r.plus(1)).sqrt().atan(),e.precision=n,e.rounding=i,r.times(2))};S.inverseHyperbolicCosine=S.acosh=function(){var r,e,t=this,n=t.constructor;return t.lte(1)?new n(t.eq(1)?0:NaN):t.isFinite()?(r=n.precision,e=n.rounding,n.precision=r+Math.max(Math.abs(t.e),t.sd())+4,n.rounding=1,$=!1,t=t.times(t).minus(1).sqrt().plus(t),$=!0,n.precision=r,n.rounding=e,t.ln()):new n(t)};S.inverseHyperbolicSine=S.asinh=function(){var r,e,t=this,n=t.constructor;return!t.isFinite()||t.isZero()?new n(t):(r=n.precision,e=n.rounding,n.precision=r+2*Math.max(Math.abs(t.e),t.sd())+6,n.rounding=1,$=!1,t=t.times(t).plus(1).sqrt().plus(t),$=!0,n.precision=r,n.rounding=e,t.ln())};S.inverseHyperbolicTangent=S.atanh=function(){var r,e,t,n,i=this,o=i.constructor;return i.isFinite()?i.e>=0?new o(i.abs().eq(1)?i.s/0:i.isZero()?i:NaN):(r=o.precision,e=o.rounding,n=i.sd(),Math.max(n,r)<2*-i.e-1?W(new o(i),r,e,!0):(o.precision=t=n-i.e,i=st(i.plus(1),new o(1).minus(i),t+r,1),o.precision=r+4,o.rounding=1,i=i.ln(),o.precision=r,o.rounding=e,i.times(.5))):new o(NaN)};S.inverseSine=S.asin=function(){var r,e,t,n,i=this,o=i.constructor;return i.isZero()?new o(i):(e=i.abs().cmp(1),t=o.precision,n=o.rounding,e!==-1?e===0?(r=re(o,t+4,n).times(.5),r.s=i.s,r):new o(NaN):(o.precision=t+6,o.rounding=1,i=i.div(new o(1).minus(i.times(i)).sqrt().plus(1)).atan(),o.precision=t,o.rounding=n,i.times(2)))};S.inverseTangent=S.atan=function(){var r,e,t,n,i,o,l,d,f,c=this,p=c.constructor,_=p.precision,w=p.rounding;if(c.isFinite()){if(c.isZero())return new p(c);if(c.abs().eq(1)&&_+4<=ki)return l=re(p,_+4,w).times(.25),l.s=c.s,l}else{if(!c.s)return new p(NaN);if(_+4<=ki)return l=re(p,_+4,w).times(.5),l.s=c.s,l}for(p.precision=d=_+10,p.rounding=1,t=Math.min(28,d/H+2|0),r=t;r;--r)c=c.div(c.times(c).plus(1).sqrt().plus(1));for($=!1,e=Math.ceil(d/H),n=1,f=c.times(c),l=new p(c),i=c;r!==-1;)if(i=i.times(f),o=l.minus(i.div(n+=2)),i=i.times(f),l=o.plus(i.div(n+=2)),l.d[e]!==void 0)for(r=e;l.d[r]===o.d[r]&&r--;);return t&&(l=l.times(2<<t-1)),$=!0,W(l,p.precision=_,p.rounding=w,!0)};S.isFinite=function(){return!!this.d};S.isInteger=S.isInt=function(){return!!this.d&&Kt(this.e/H)>this.d.length-2};S.isNaN=function(){return!this.s};S.isNegative=S.isNeg=function(){return this.s<0};S.isPositive=S.isPos=function(){return this.s>0};S.isZero=function(){return!!this.d&&this.d[0]===0};S.lessThan=S.lt=function(r){return this.cmp(r)<0};S.lessThanOrEqualTo=S.lte=function(r){return this.cmp(r)<1};S.logarithm=S.log=function(r){var e,t,n,i,o,l,d,f,c=this,p=c.constructor,_=p.precision,w=p.rounding,P=5;if(r==null)r=new p(10),e=!0;else{if(r=new p(r),t=r.d,r.s<0||!t||!t[0]||r.eq(1))return new p(NaN);e=r.eq(10)}if(t=c.d,c.s<0||!t||!t[0]||c.eq(1))return new p(t&&!t[0]?-1/0:c.s!=1?NaN:t?0:1/0);if(e)if(t.length>1)o=!0;else{for(i=t[0];i%10===0;)i/=10;o=i!==1}if($=!1,d=_+P,l=fe(c,d),n=e?He(p,d+10):fe(r,d),f=st(l,n,d,1),Ce(f.d,i=_,w))do if(d+=10,l=fe(c,d),n=e?He(p,d+10):fe(r,d),f=st(l,n,d,1),!o){+Qt(f.d).slice(i+1,i+15)+1==1e14&&(f=W(f,_+1,0));break}while(Ce(f.d,i+=10,w));return $=!0,W(f,_,w)};S.minus=S.sub=function(r){var e,t,n,i,o,l,d,f,c,p,_,w,P=this,A=P.constructor;if(r=new A(r),!P.d||!r.d)return!P.s||!r.s?r=new A(NaN):P.d?r.s=-r.s:r=new A(r.d||P.s!==r.s?P:NaN),r;if(P.s!=r.s)return r.s=-r.s,P.plus(r);if(c=P.d,w=r.d,d=A.precision,f=A.rounding,!c[0]||!w[0]){if(w[0])r.s=-r.s;else if(c[0])r=new A(P);else return new A(f===3?-0:0);return $?W(r,d,f):r}if(t=Kt(r.e/H),p=Kt(P.e/H),c=c.slice(),o=p-t,o){for(_=o<0,_?(e=c,o=-o,l=w.length):(e=w,t=p,l=c.length),n=Math.max(Math.ceil(d/H),l)+2,o>n&&(o=n,e.length=1),e.reverse(),n=o;n--;)e.push(0);e.reverse()}else{for(n=c.length,l=w.length,_=n<l,_&&(l=n),n=0;n<l;n++)if(c[n]!=w[n]){_=c[n]<w[n];break}o=0}for(_&&(e=c,c=w,w=e,r.s=-r.s),l=c.length,n=w.length-l;n>0;--n)c[l++]=0;for(n=w.length;n>o;){if(c[--n]<w[n]){for(i=n;i&&c[--i]===0;)c[i]=te-1;--c[i],c[n]+=te}c[n]-=w[n]}for(;c[--l]===0;)c.pop();for(;c[0]===0;c.shift())--t;return c[0]?(r.d=c,r.e=Ke(c,t),$?W(r,d,f):r):new A(f===3?-0:0)};S.modulo=S.mod=function(r){var e,t=this,n=t.constructor;return r=new n(r),!t.d||!r.s||r.d&&!r.d[0]?new n(NaN):!r.d||t.d&&!t.d[0]?W(new n(t),n.precision,n.rounding):($=!1,n.modulo==9?(e=st(t,r.abs(),0,3,1),e.s*=r.s):e=st(t,r,0,n.modulo,1),e=e.times(r),$=!0,t.minus(e))};S.naturalExponential=S.exp=function(){return Di(this)};S.naturalLogarithm=S.ln=function(){return fe(this)};S.negated=S.neg=function(){var r=new this.constructor(this);return r.s=-r.s,W(r)};S.plus=S.add=function(r){var e,t,n,i,o,l,d,f,c,p,_=this,w=_.constructor;if(r=new w(r),!_.d||!r.d)return!_.s||!r.s?r=new w(NaN):_.d||(r=new w(r.d||_.s===r.s?_:NaN)),r;if(_.s!=r.s)return r.s=-r.s,_.minus(r);if(c=_.d,p=r.d,d=w.precision,f=w.rounding,!c[0]||!p[0])return p[0]||(r=new w(_)),$?W(r,d,f):r;if(o=Kt(_.e/H),n=Kt(r.e/H),c=c.slice(),i=o-n,i){for(i<0?(t=c,i=-i,l=p.length):(t=p,n=o,l=c.length),o=Math.ceil(d/H),l=o>l?o+1:l+1,i>l&&(i=l,t.length=1),t.reverse();i--;)t.push(0);t.reverse()}for(l=c.length,i=p.length,l-i<0&&(i=l,t=p,p=c,c=t),e=0;i;)e=(c[--i]=c[i]+p[i]+e)/te|0,c[i]%=te;for(e&&(c.unshift(e),++n),l=c.length;c[--l]==0;)c.pop();return r.d=c,r.e=Ke(c,n),$?W(r,d,f):r};S.precision=S.sd=function(r){var e,t=this;if(r!==void 0&&r!==!!r&&r!==1&&r!==0)throw Error(pe+r);return t.d?(e=Bn(t.d),r&&t.e+1>e&&(e=t.e+1)):e=NaN,e};S.round=function(){var r=this,e=r.constructor;return W(new e(r),r.e+1,e.rounding)};S.sine=S.sin=function(){var r,e,t=this,n=t.constructor;return t.isFinite()?t.isZero()?new n(t):(r=n.precision,e=n.rounding,n.precision=r+Math.max(t.e,t.sd())+H,n.rounding=1,t=Rr(n,En(n,t)),n.precision=r,n.rounding=e,W(ue>2?t.neg():t,r,e,!0)):new n(NaN)};S.squareRoot=S.sqrt=function(){var r,e,t,n,i,o,l=this,d=l.d,f=l.e,c=l.s,p=l.constructor;if(c!==1||!d||!d[0])return new p(!c||c<0&&(!d||d[0])?NaN:d?l:1/0);for($=!1,c=Math.sqrt(+l),c==0||c==1/0?(e=Qt(d),(e.length+f)%2==0&&(e+="0"),c=Math.sqrt(e),f=Kt((f+1)/2)-(f<0||f%2),c==1/0?e="5e"+f:(e=c.toExponential(),e=e.slice(0,e.indexOf("e")+1)+f),n=new p(e)):n=new p(c.toString()),t=(f=p.precision)+3;;)if(o=n,n=o.plus(st(l,o,t+2,1)).times(.5),Qt(o.d).slice(0,t)===(e=Qt(n.d)).slice(0,t))if(e=e.slice(t-3,t+1),e=="9999"||!i&&e=="4999"){if(!i&&(W(o,f+1,0),o.times(o).eq(l))){n=o;break}t+=4,i=1}else{(!+e||!+e.slice(1)&&e.charAt(0)=="5")&&(W(n,f+1,1),r=!n.times(n).eq(l));break}return $=!0,W(n,f,p.rounding,r)};S.tangent=S.tan=function(){var r,e,t=this,n=t.constructor;return t.isFinite()?t.isZero()?new n(t):(r=n.precision,e=n.rounding,n.precision=r+10,n.rounding=1,t=t.sin(),t.s=1,t=st(t,new n(1).minus(t.times(t)).sqrt(),r+10,0),n.precision=r,n.rounding=e,W(ue==2||ue==4?t.neg():t,r,e,!0)):new n(NaN)};S.times=S.mul=function(r){var e,t,n,i,o,l,d,f,c,p=this,_=p.constructor,w=p.d,P=(r=new _(r)).d;if(r.s*=p.s,!w||!w[0]||!P||!P[0])return new _(!r.s||w&&!w[0]&&!P||P&&!P[0]&&!w?NaN:!w||!P?r.s/0:r.s*0);for(t=Kt(p.e/H)+Kt(r.e/H),f=w.length,c=P.length,f<c&&(o=w,w=P,P=o,l=f,f=c,c=l),o=[],l=f+c,n=l;n--;)o.push(0);for(n=c;--n>=0;){for(e=0,i=f+n;i>n;)d=o[i]+P[n]*w[i-n-1]+e,o[i--]=d%te|0,e=d/te|0;o[i]=(o[i]+e)%te|0}for(;!o[--l];)o.pop();return e?++t:o.shift(),r.d=o,r.e=Ke(o,t),$?W(r,_.precision,_.rounding):r};S.toBinary=function(r,e){return Ni(this,2,r,e)};S.toDecimalPlaces=S.toDP=function(r,e){var t=this,n=t.constructor;return t=new n(t),r===void 0?t:(Vt(r,0,me),e===void 0?e=n.rounding:Vt(e,0,8),W(t,r+t.e+1,e))};S.toExponential=function(r,e){var t,n=this,i=n.constructor;return r===void 0?t=oe(n,!0):(Vt(r,0,me),e===void 0?e=i.rounding:Vt(e,0,8),n=W(new i(n),r+1,e),t=oe(n,!0,r+1)),n.isNeg()&&!n.isZero()?"-"+t:t};S.toFixed=function(r,e){var t,n,i=this,o=i.constructor;return r===void 0?t=oe(i):(Vt(r,0,me),e===void 0?e=o.rounding:Vt(e,0,8),n=W(new o(i),r+i.e+1,e),t=oe(n,!1,r+n.e+1)),i.isNeg()&&!i.isZero()?"-"+t:t};S.toFraction=function(r){var e,t,n,i,o,l,d,f,c,p,_,w,P=this,A=P.d,I=P.constructor;if(!A)return new I(P);if(c=t=new I(1),n=f=new I(0),e=new I(n),o=e.e=Bn(A)-P.e-1,l=o%H,e.d[0]=Gt(10,l<0?H+l:l),r==null)r=o>0?e:c;else{if(d=new I(r),!d.isInt()||d.lt(c))throw Error(pe+d);r=d.gt(e)?o>0?e:c:d}for($=!1,d=new I(Qt(A)),p=I.precision,I.precision=o=A.length*H*2;_=st(d,e,0,1,1),i=t.plus(_.times(n)),i.cmp(r)!=1;)t=n,n=i,i=c,c=f.plus(_.times(i)),f=i,i=e,e=d.minus(_.times(i)),d=i;return i=st(r.minus(t),n,0,1,1),f=f.plus(i.times(c)),t=t.plus(i.times(n)),f.s=c.s=P.s,w=st(c,n,o,1).minus(P).abs().cmp(st(f,t,o,1).minus(P).abs())<1?[c,n]:[f,t],I.precision=p,$=!0,w};S.toHexadecimal=S.toHex=function(r,e){return Ni(this,16,r,e)};S.toNearest=function(r,e){var t=this,n=t.constructor;if(t=new n(t),r==null){if(!t.d)return t;r=new n(1),e=n.rounding}else{if(r=new n(r),e===void 0?e=n.rounding:Vt(e,0,8),!t.d)return r.s?t:r;if(!r.d)return r.s&&(r.s=t.s),r}return r.d[0]?($=!1,t=st(t,r,0,e,1).times(r),$=!0,W(t)):(r.s=t.s,t=r),t};S.toNumber=function(){return+this};S.toOctal=function(r,e){return Ni(this,8,r,e)};S.toPower=S.pow=function(r){var e,t,n,i,o,l,d=this,f=d.constructor,c=+(r=new f(r));if(!d.d||!r.d||!d.d[0]||!r.d[0])return new f(Gt(+d,c));if(d=new f(d),d.eq(1))return d;if(n=f.precision,o=f.rounding,r.eq(1))return W(d,n,o);if(e=Kt(r.e/H),e>=r.d.length-1&&(t=c<0?-c:c)<=Ir)return i=An(f,d,t,n),r.s<0?new f(1).div(i):W(i,n,o);if(l=d.s,l<0){if(e<r.d.length-1)return new f(NaN);if((r.d[e]&1)==0&&(l=1),d.e==0&&d.d[0]==1&&d.d.length==1)return d.s=l,d}return t=Gt(+d,c),e=t==0||!isFinite(t)?Kt(c*(Math.log("0."+Qt(d.d))/Math.LN10+d.e+1)):new f(t+"").e,e>f.maxE+1||e<f.minE-1?new f(e>0?l/0:0):($=!1,f.rounding=d.s=1,t=Math.min(12,(e+"").length),i=Di(r.times(fe(d,n+t)),n),i.d&&(i=W(i,n+5,1),Ce(i.d,n,o)&&(e=n+10,i=W(Di(r.times(fe(d,e+t)),e),e+5,1),+Qt(i.d).slice(n+1,n+15)+1==1e14&&(i=W(i,n+1,0)))),i.s=l,$=!0,f.rounding=o,W(i,n,o))};S.toPrecision=function(r,e){var t,n=this,i=n.constructor;return r===void 0?t=oe(n,n.e<=i.toExpNeg||n.e>=i.toExpPos):(Vt(r,1,me),e===void 0?e=i.rounding:Vt(e,0,8),n=W(new i(n),r,e),t=oe(n,r<=n.e||n.e<=i.toExpNeg,r)),n.isNeg()&&!n.isZero()?"-"+t:t};S.toSignificantDigits=S.toSD=function(r,e){var t=this,n=t.constructor;return r===void 0?(r=n.precision,e=n.rounding):(Vt(r,1,me),e===void 0?e=n.rounding:Vt(e,0,8)),W(new n(t),r,e)};S.toString=function(){var r=this,e=r.constructor,t=oe(r,r.e<=e.toExpNeg||r.e>=e.toExpPos);return r.isNeg()&&!r.isZero()?"-"+t:t};S.truncated=S.trunc=function(){return W(new this.constructor(this),this.e+1,1)};S.valueOf=S.toJSON=function(){var r=this,e=r.constructor,t=oe(r,r.e<=e.toExpNeg||r.e>=e.toExpPos);return r.isNeg()?"-"+t:t};function Qt(r){var e,t,n,i=r.length-1,o="",l=r[0];if(i>0){for(o+=l,e=1;e<i;e++)n=r[e]+"",t=H-n.length,t&&(o+=ce(t)),o+=n;l=r[e],n=l+"",t=H-n.length,t&&(o+=ce(t))}else if(l===0)return"0";for(;l%10===0;)l/=10;return o+l}function Vt(r,e,t){if(r!==~~r||r<e||r>t)throw Error(pe+r)}function Ce(r,e,t,n){var i,o,l,d;for(o=r[0];o>=10;o/=10)--e;return--e<0?(e+=H,i=0):(i=Math.ceil((e+1)/H),e%=H),o=Gt(10,H-e),d=r[i]%o|0,n==null?e<3?(e==0?d=d/100|0:e==1&&(d=d/10|0),l=t<4&&d==99999||t>3&&d==49999||d==5e4||d==0):l=(t<4&&d+1==o||t>3&&d+1==o/2)&&(r[i+1]/o/100|0)==Gt(10,e-2)-1||(d==o/2||d==0)&&(r[i+1]/o/100|0)==0:e<4?(e==0?d=d/1e3|0:e==1?d=d/100|0:e==2&&(d=d/10|0),l=(n||t<4)&&d==9999||!n&&t>3&&d==4999):l=((n||t<4)&&d+1==o||!n&&t>3&&d+1==o/2)&&(r[i+1]/o/1e3|0)==Gt(10,e-3)-1,l}function We(r,e,t){for(var n,i=[0],o,l=0,d=r.length;l<d;){for(o=i.length;o--;)i[o]*=e;for(i[0]+=xi.indexOf(r.charAt(l++)),n=0;n<i.length;n++)i[n]>t-1&&(i[n+1]===void 0&&(i[n+1]=0),i[n+1]+=i[n]/t|0,i[n]%=t)}return i.reverse()}function Er(r,e){var t,n,i;if(e.isZero())return e;n=e.d.length,n<32?(t=Math.ceil(n/3),i=(1/Ve(4,t)).toString()):(t=16,i="2.3283064365386962890625e-10"),r.precision+=t,e=Ae(r,1,e.times(i),new r(1));for(var o=t;o--;){var l=e.times(e);e=l.times(l).minus(l).times(8).plus(1)}return r.precision-=t,e}var st=(function(){function r(n,i,o){var l,d=0,f=n.length;for(n=n.slice();f--;)l=n[f]*i+d,n[f]=l%o|0,d=l/o|0;return d&&n.unshift(d),n}function e(n,i,o,l){var d,f;if(o!=l)f=o>l?1:-1;else for(d=f=0;d<o;d++)if(n[d]!=i[d]){f=n[d]>i[d]?1:-1;break}return f}function t(n,i,o,l){for(var d=0;o--;)n[o]-=d,d=n[o]<i[o]?1:0,n[o]=d*l+n[o]-i[o];for(;!n[0]&&n.length>1;)n.shift()}return function(n,i,o,l,d,f){var c,p,_,w,P,A,I,E,T,L,R,C,k,x,K,rt,at,nt,U,ot,y=n.constructor,s=n.s==i.s?1:-1,u=n.d,m=i.d;if(!u||!u[0]||!m||!m[0])return new y(!n.s||!i.s||(u?m&&u[0]==m[0]:!m)?NaN:u&&u[0]==0||!m?s*0:s/0);for(f?(P=1,p=n.e-i.e):(f=te,P=H,p=Kt(n.e/P)-Kt(i.e/P)),U=m.length,at=u.length,T=new y(s),L=T.d=[],_=0;m[_]==(u[_]||0);_++);if(m[_]>(u[_]||0)&&p--,o==null?(x=o=y.precision,l=y.rounding):d?x=o+(n.e-i.e)+1:x=o,x<0)L.push(1),A=!0;else{if(x=x/P+2|0,_=0,U==1){for(w=0,m=m[0],x++;(_<at||w)&&x--;_++)K=w*f+(u[_]||0),L[_]=K/m|0,w=K%m|0;A=w||_<at}else{for(w=f/(m[0]+1)|0,w>1&&(m=r(m,w,f),u=r(u,w,f),U=m.length,at=u.length),rt=U,R=u.slice(0,U),C=R.length;C<U;)R[C++]=0;ot=m.slice(),ot.unshift(0),nt=m[0],m[1]>=f/2&&++nt;do w=0,c=e(m,R,U,C),c<0?(k=R[0],U!=C&&(k=k*f+(R[1]||0)),w=k/nt|0,w>1?(w>=f&&(w=f-1),I=r(m,w,f),E=I.length,C=R.length,c=e(I,R,E,C),c==1&&(w--,t(I,U<E?ot:m,E,f))):(w==0&&(c=w=1),I=m.slice()),E=I.length,E<C&&I.unshift(0),t(R,I,C,f),c==-1&&(C=R.length,c=e(m,R,U,C),c<1&&(w++,t(R,U<C?ot:m,C,f))),C=R.length):c===0&&(w++,R=[0]),L[_++]=w,c&&R[0]?R[C++]=u[rt]||0:(R=[u[rt]],C=1);while((rt++<at||R[0]!==void 0)&&x--);A=R[0]!==void 0}L[0]||L.shift()}if(P==1)T.e=p,wn=A;else{for(_=1,w=L[0];w>=10;w/=10)_++;T.e=_+p*P-1,W(T,d?o+T.e+1:o,l,A)}return T}})();function W(r,e,t,n){var i,o,l,d,f,c,p,_,w,P=r.constructor;t:if(e!=null){if(_=r.d,!_)return r;for(i=1,d=_[0];d>=10;d/=10)i++;if(o=e-i,o<0)o+=H,l=e,p=_[w=0],f=p/Gt(10,i-l-1)%10|0;else if(w=Math.ceil((o+1)/H),d=_.length,w>=d)if(n){for(;d++<=w;)_.push(0);p=f=0,i=1,o%=H,l=o-H+1}else break t;else{for(p=d=_[w],i=1;d>=10;d/=10)i++;o%=H,l=o-H+i,f=l<0?0:p/Gt(10,i-l-1)%10|0}if(n=n||e<0||_[w+1]!==void 0||(l<0?p:p%Gt(10,i-l-1)),c=t<4?(f||n)&&(t==0||t==(r.s<0?3:2)):f>5||f==5&&(t==4||n||t==6&&(o>0?l>0?p/Gt(10,i-l):0:_[w-1])%10&1||t==(r.s<0?8:7)),e<1||!_[0])return _.length=0,c?(e-=r.e+1,_[0]=Gt(10,(H-e%H)%H),r.e=-e||0):_[0]=r.e=0,r;if(o==0?(_.length=w,d=1,w--):(_.length=w+1,d=Gt(10,H-o),_[w]=l>0?(p/Gt(10,i-l)%Gt(10,l)|0)*d:0),c)for(;;)if(w==0){for(o=1,l=_[0];l>=10;l/=10)o++;for(l=_[0]+=d,d=1;l>=10;l/=10)d++;o!=d&&(r.e++,_[0]==te&&(_[0]=1));break}else{if(_[w]+=d,_[w]!=te)break;_[w--]=0,d=1}for(o=_.length;_[--o]===0;)_.pop()}return $&&(r.e>P.maxE?(r.d=null,r.e=NaN):r.e<P.minE&&(r.e=0,r.d=[0])),r}function oe(r,e,t){if(!r.isFinite())return Sn(r);var n,i=r.e,o=Qt(r.d),l=o.length;return e?(t&&(n=t-l)>0?o=o.charAt(0)+"."+o.slice(1)+ce(n):l>1&&(o=o.charAt(0)+"."+o.slice(1)),o=o+(r.e<0?"e":"e+")+r.e):i<0?(o="0."+ce(-i-1)+o,t&&(n=t-l)>0&&(o+=ce(n))):i>=l?(o+=ce(i+1-l),t&&(n=t-i-1)>0&&(o=o+"."+ce(n))):((n=i+1)<l&&(o=o.slice(0,n)+"."+o.slice(n)),t&&(n=t-l)>0&&(i+1===l&&(o+="."),o+=ce(n))),o}function Ke(r,e){var t=r[0];for(e*=H;t>=10;t/=10)e++;return e}function He(r,e,t){if(e>Sr)throw $=!0,t&&(r.precision=t),Error(vn);return W(new r(Ze),e,1,!0)}function re(r,e,t){if(e>ki)throw Error(vn);return W(new r(Ge),e,t,!0)}function Bn(r){var e=r.length-1,t=e*H+1;if(e=r[e],e){for(;e%10==0;e/=10)t--;for(e=r[0];e>=10;e/=10)t++}return t}function ce(r){for(var e="";r--;)e+="0";return e}function An(r,e,t,n){var i,o=new r(1),l=Math.ceil(n/H+4);for($=!1;;){if(t%2&&(o=o.times(e),gn(o.d,l)&&(i=!0)),t=Kt(t/2),t===0){t=o.d.length-1,i&&o.d[t]===0&&++o.d[t];break}e=e.times(e),gn(e.d,l)}return $=!0,o}function _n(r){return r.d[r.d.length-1]&1}function In(r,e,t){for(var n,i,o=new r(e[0]),l=0;++l<e.length;){if(i=new r(e[l]),!i.s){o=i;break}n=o.cmp(i),(n===t||n===0&&o.s===t)&&(o=i)}return o}function Di(r,e){var t,n,i,o,l,d,f,c=0,p=0,_=0,w=r.constructor,P=w.rounding,A=w.precision;if(!r.d||!r.d[0]||r.e>17)return new w(r.d?r.d[0]?r.s<0?0:1/0:1:r.s?r.s<0?0:r:NaN);for(e==null?($=!1,f=A):f=e,d=new w(.03125);r.e>-2;)r=r.times(d),_+=5;for(n=Math.log(Gt(2,_))/Math.LN10*2+5|0,f+=n,t=o=l=new w(1),w.precision=f;;){if(o=W(o.times(r),f,1),t=t.times(++p),d=l.plus(st(o,t,f,1)),Qt(d.d).slice(0,f)===Qt(l.d).slice(0,f)){for(i=_;i--;)l=W(l.times(l),f,1);if(e==null)if(c<3&&Ce(l.d,f-n,P,c))w.precision=f+=10,t=o=d=new w(1),p=0,c++;else return W(l,w.precision=A,P,$=!0);else return w.precision=A,l}l=d}}function fe(r,e){var t,n,i,o,l,d,f,c,p,_,w,P=1,A=10,I=r,E=I.d,T=I.constructor,L=T.rounding,R=T.precision;if(I.s<0||!E||!E[0]||!I.e&&E[0]==1&&E.length==1)return new T(E&&!E[0]?-1/0:I.s!=1?NaN:E?0:I);if(e==null?($=!1,p=R):p=e,T.precision=p+=A,t=Qt(E),n=t.charAt(0),Math.abs(o=I.e)<15e14){for(;n<7&&n!=1||n==1&&t.charAt(1)>3;)I=I.times(r),t=Qt(I.d),n=t.charAt(0),P++;o=I.e,n>1?(I=new T("0."+t),o++):I=new T(n+"."+t.slice(1))}else return c=He(T,p+2,R).times(o+""),I=fe(new T(n+"."+t.slice(1)),p-A).plus(c),T.precision=R,e==null?W(I,R,L,$=!0):I;for(_=I,f=l=I=st(I.minus(1),I.plus(1),p,1),w=W(I.times(I),p,1),i=3;;){if(l=W(l.times(w),p,1),c=f.plus(st(l,new T(i),p,1)),Qt(c.d).slice(0,p)===Qt(f.d).slice(0,p))if(f=f.times(2),o!==0&&(f=f.plus(He(T,p+2,R).times(o+""))),f=st(f,new T(P),p,1),e==null)if(Ce(f.d,p-A,L,d))T.precision=p+=A,c=l=I=st(_.minus(1),_.plus(1),p,1),w=W(I.times(I),p,1),i=d=1;else return W(f,T.precision=R,L,$=!0);else return T.precision=R,f;f=c,i+=2}}function Sn(r){return String(r.s*r.s/0)}function Ue(r,e){var t,n,i;for((t=e.indexOf("."))>-1&&(e=e.replace(".","")),(n=e.search(/e/i))>0?(t<0&&(t=n),t+=+e.slice(n+1),e=e.substring(0,n)):t<0&&(t=e.length),n=0;e.charCodeAt(n)===48;n++);for(i=e.length;e.charCodeAt(i-1)===48;--i);if(e=e.slice(n,i),e){if(i-=n,r.e=t=t-n-1,r.d=[],n=(t+1)%H,t<0&&(n+=H),n<i){for(n&&r.d.push(+e.slice(0,n)),i-=H;n<i;)r.d.push(+e.slice(n,n+=H));e=e.slice(n),n=H-e.length}else n-=i;for(;n--;)e+="0";r.d.push(+e),$&&(r.e>r.constructor.maxE?(r.d=null,r.e=NaN):r.e<r.constructor.minE&&(r.e=0,r.d=[0]))}else r.e=0,r.d=[0];return r}function Or(r,e){var t,n,i,o,l,d,f,c,p;if(e.indexOf("_")>-1){if(e=e.replace(/(\d)_(?=\d)/g,"$1"),Pn.test(e))return Ue(r,e)}else if(e==="Infinity"||e==="NaN")return+e||(r.s=NaN),r.e=NaN,r.d=null,r;if(Br.test(e))t=16,e=e.toLowerCase();else if(Pr.test(e))t=2;else if(Ar.test(e))t=8;else throw Error(pe+e);for(o=e.search(/p/i),o>0?(f=+e.slice(o+1),e=e.substring(2,o)):e=e.slice(2),o=e.indexOf("."),l=o>=0,n=r.constructor,l&&(e=e.replace(".",""),d=e.length,o=d-o,i=An(n,new n(t),o,o*2)),c=We(e,t,te),p=c.length-1,o=p;c[o]===0;--o)c.pop();return o<0?new n(r.s*0):(r.e=Ke(c,p),r.d=c,$=!1,l&&(r=st(r,i,d*4)),f&&(r=r.times(Math.abs(f)<54?Gt(2,f):ke.pow(2,f))),$=!0,r)}function Rr(r,e){var t,n=e.d.length;if(n<3)return e.isZero()?e:Ae(r,2,e,e);t=1.4*Math.sqrt(n),t=t>16?16:t|0,e=e.times(1/Ve(5,t)),e=Ae(r,2,e,e);for(var i,o=new r(5),l=new r(16),d=new r(20);t--;)i=e.times(e),e=e.times(o.plus(i.times(l.times(i).minus(d))));return e}function Ae(r,e,t,n,i){var o,l,d,f,c=1,p=r.precision,_=Math.ceil(p/H);for($=!1,f=t.times(t),d=new r(n);;){if(l=st(d.times(f),new r(e++*e++),p,1),d=i?n.plus(l):n.minus(l),n=st(l.times(f),new r(e++*e++),p,1),l=d.plus(n),l.d[_]!==void 0){for(o=_;l.d[o]===d.d[o]&&o--;);if(o==-1)break}o=d,d=n,n=l,l=o,c++}return $=!0,l.d.length=_+1,l}function Ve(r,e){for(var t=r;--e;)t*=r;return t}function En(r,e){var t,n=e.s<0,i=re(r,r.precision,1),o=i.times(.5);if(e=e.abs(),e.lte(o))return ue=n?4:1,e;if(t=e.divToInt(i),t.isZero())ue=n?3:2;else{if(e=e.minus(t.times(i)),e.lte(o))return ue=_n(t)?n?2:3:n?4:1,e;ue=_n(t)?n?1:4:n?3:2}return e.minus(i).abs()}function Ni(r,e,t,n){var i,o,l,d,f,c,p,_,w,P=r.constructor,A=t!==void 0;if(A?(Vt(t,1,me),n===void 0?n=P.rounding:Vt(n,0,8)):(t=P.precision,n=P.rounding),!r.isFinite())p=Sn(r);else{for(p=oe(r),l=p.indexOf("."),A?(i=2,e==16?t=t*4-3:e==8&&(t=t*3-2)):i=e,l>=0&&(p=p.replace(".",""),w=new P(1),w.e=p.length-l,w.d=We(oe(w),10,i),w.e=w.d.length),_=We(p,10,i),o=f=_.length;_[--f]==0;)_.pop();if(!_[0])p=A?"0p+0":"0";else{if(l<0?o--:(r=new P(r),r.d=_,r.e=o,r=st(r,w,t,n,0,i),_=r.d,o=r.e,c=wn),l=_[t],d=i/2,c=c||_[t+1]!==void 0,c=n<4?(l!==void 0||c)&&(n===0||n===(r.s<0?3:2)):l>d||l===d&&(n===4||c||n===6&&_[t-1]&1||n===(r.s<0?8:7)),_.length=t,c)for(;++_[--t]>i-1;)_[t]=0,t||(++o,_.unshift(1));for(f=_.length;!_[f-1];--f);for(l=0,p="";l<f;l++)p+=xi.charAt(_[l]);if(A){if(f>1)if(e==16||e==8){for(l=e==16?4:3,--f;f%l;f++)p+="0";for(_=We(p,i,e),f=_.length;!_[f-1];--f);for(l=1,p="1.";l<f;l++)p+=xi.charAt(_[l])}else p=p.charAt(0)+"."+p.slice(1);p=p+(o<0?"p":"p+")+o}else if(o<0){for(;++o;)p="0"+p;p="0."+p}else if(++o>f)for(o-=f;o--;)p+="0";else o<f&&(p=p.slice(0,o)+"."+p.slice(o))}p=(e==16?"0x":e==2?"0b":e==8?"0o":"")+p}return r.s<0?"-"+p:p}function gn(r,e){if(r.length>e)return r.length=e,!0}function Tr(r){return new this(r).abs()}function xr(r){return new this(r).acos()}function Cr(r){return new this(r).acosh()}function kr(r,e){return new this(r).plus(e)}function Dr(r){return new this(r).asin()}function Nr(r){return new this(r).asinh()}function Fr(r){return new this(r).atan()}function qr(r){return new this(r).atanh()}function Lr(r,e){r=new this(r),e=new this(e);var t,n=this.precision,i=this.rounding,o=n+4;return!r.s||!e.s?t=new this(NaN):!r.d&&!e.d?(t=re(this,o,1).times(e.s>0?.25:.75),t.s=r.s):!e.d||r.isZero()?(t=e.s<0?re(this,n,i):new this(0),t.s=r.s):!r.d||e.isZero()?(t=re(this,o,1).times(.5),t.s=r.s):e.s<0?(this.precision=o,this.rounding=1,t=this.atan(st(r,e,o,1)),e=re(this,o,1),this.precision=n,this.rounding=i,t=r.s<0?t.minus(e):t.plus(e)):t=this.atan(st(r,e,o,1)),t}function jr(r){return new this(r).cbrt()}function Wr(r){return W(r=new this(r),r.e+1,2)}function Ur(r,e,t){return new this(r).clamp(e,t)}function Zr(r){if(!r||typeof r!="object")throw Error(Qe+"Object expected");var e,t,n,i=r.defaults===!0,o=["precision",1,me,"rounding",0,8,"toExpNeg",-Be,0,"toExpPos",0,Be,"maxE",0,Be,"minE",-Be,0,"modulo",0,9];for(e=0;e<o.length;e+=3)if(t=o[e],i&&(this[t]=Ci[t]),(n=r[t])!==void 0)if(Kt(n)===n&&n>=o[e+1]&&n<=o[e+2])this[t]=n;else throw Error(pe+t+": "+n);if(t="crypto",i&&(this[t]=Ci[t]),(n=r[t])!==void 0)if(n===!0||n===!1||n===0||n===1)if(n)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[t]=!0;else throw Error(yn);else this[t]=!1;else throw Error(pe+t+": "+n);return this}function Gr(r){return new this(r).cos()}function Hr(r){return new this(r).cosh()}function On(r){var e,t,n;function i(o){var l,d,f,c=this;if(!(c instanceof i))return new i(o);if(c.constructor=i,bn(o)){c.s=o.s,$?!o.d||o.e>i.maxE?(c.e=NaN,c.d=null):o.e<i.minE?(c.e=0,c.d=[0]):(c.e=o.e,c.d=o.d.slice()):(c.e=o.e,c.d=o.d?o.d.slice():o.d);return}if(f=typeof o,f==="number"){if(o===0){c.s=1/o<0?-1:1,c.e=0,c.d=[0];return}if(o<0?(o=-o,c.s=-1):c.s=1,o===~~o&&o<1e7){for(l=0,d=o;d>=10;d/=10)l++;$?l>i.maxE?(c.e=NaN,c.d=null):l<i.minE?(c.e=0,c.d=[0]):(c.e=l,c.d=[o]):(c.e=l,c.d=[o]);return}if(o*0!==0){o||(c.s=NaN),c.e=NaN,c.d=null;return}return Ue(c,o.toString())}if(f==="string")return(d=o.charCodeAt(0))===45?(o=o.slice(1),c.s=-1):(d===43&&(o=o.slice(1)),c.s=1),Pn.test(o)?Ue(c,o):Or(c,o);if(f==="bigint")return o<0?(o=-o,c.s=-1):c.s=1,Ue(c,o.toString());throw Error(pe+o)}if(i.prototype=S,i.ROUND_UP=0,i.ROUND_DOWN=1,i.ROUND_CEIL=2,i.ROUND_FLOOR=3,i.ROUND_HALF_UP=4,i.ROUND_HALF_DOWN=5,i.ROUND_HALF_EVEN=6,i.ROUND_HALF_CEIL=7,i.ROUND_HALF_FLOOR=8,i.EUCLID=9,i.config=i.set=Zr,i.clone=On,i.isDecimal=bn,i.abs=Tr,i.acos=xr,i.acosh=Cr,i.add=kr,i.asin=Dr,i.asinh=Nr,i.atan=Fr,i.atanh=qr,i.atan2=Lr,i.cbrt=jr,i.ceil=Wr,i.clamp=Ur,i.cos=Gr,i.cosh=Hr,i.div=Qr,i.exp=Kr,i.floor=Vr,i.hypot=$r,i.ln=zr,i.log=Xr,i.log10=Jr,i.log2=Yr,i.max=to,i.min=eo,i.mod=io,i.mul=no,i.pow=ro,i.random=oo,i.round=so,i.sign=ao,i.sin=lo,i.sinh=uo,i.sqrt=co,i.sub=fo,i.sum=po,i.tan=mo,i.tanh=ho,i.trunc=_o,r===void 0&&(r={}),r&&r.defaults!==!0)for(n=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],e=0;e<n.length;)r.hasOwnProperty(t=n[e++])||(r[t]=this[t]);return i.config(r),i}function Qr(r,e){return new this(r).div(e)}function Kr(r){return new this(r).exp()}function Vr(r){return W(r=new this(r),r.e+1,3)}function $r(){var r,e,t=new this(0);for($=!1,r=0;r<arguments.length;)if(e=new this(arguments[r++]),e.d)t.d&&(t=t.plus(e.times(e)));else{if(e.s)return $=!0,new this(1/0);t=e}return $=!0,t.sqrt()}function bn(r){return r instanceof ke||r&&r.toStringTag===Mn||!1}function zr(r){return new this(r).ln()}function Xr(r,e){return new this(r).log(e)}function Yr(r){return new this(r).log(2)}function Jr(r){return new this(r).log(10)}function to(){return In(this,arguments,-1)}function eo(){return In(this,arguments,1)}function io(r,e){return new this(r).mod(e)}function no(r,e){return new this(r).mul(e)}function ro(r,e){return new this(r).pow(e)}function oo(r){var e,t,n,i,o=0,l=new this(1),d=[];if(r===void 0?r=this.precision:Vt(r,1,me),n=Math.ceil(r/H),this.crypto)if(crypto.getRandomValues)for(e=crypto.getRandomValues(new Uint32Array(n));o<n;)i=e[o],i>=429e7?e[o]=crypto.getRandomValues(new Uint32Array(1))[0]:d[o++]=i%1e7;else if(crypto.randomBytes){for(e=crypto.randomBytes(n*=4);o<n;)i=e[o]+(e[o+1]<<8)+(e[o+2]<<16)+((e[o+3]&127)<<24),i>=214e7?crypto.randomBytes(4).copy(e,o):(d.push(i%1e7),o+=4);o=n/4}else throw Error(yn);else for(;o<n;)d[o++]=Math.random()*1e7|0;for(n=d[--o],r%=H,n&&r&&(i=Gt(10,H-r),d[o]=(n/i|0)*i);d[o]===0;o--)d.pop();if(o<0)t=0,d=[0];else{for(t=-1;d[0]===0;t-=H)d.shift();for(n=1,i=d[0];i>=10;i/=10)n++;n<H&&(t-=H-n)}return l.e=t,l.d=d,l}function so(r){return W(r=new this(r),r.e+1,this.rounding)}function ao(r){return r=new this(r),r.d?r.d[0]?r.s:0*r.s:r.s||NaN}function lo(r){return new this(r).sin()}function uo(r){return new this(r).sinh()}function co(r){return new this(r).sqrt()}function fo(r,e){return new this(r).sub(e)}function po(){var r=0,e=arguments,t=new this(e[r]);for($=!1;t.s&&++r<e.length;)t=t.plus(e[r]);return $=!0,W(t,this.precision,this.rounding)}function mo(r){return new this(r).tan()}function ho(r){return new this(r).tanh()}function _o(r){return W(r=new this(r),r.e+1,1)}S[Symbol.for("nodejs.util.inspect.custom")]=S.toString;S[Symbol.toStringTag]="Decimal";var ke=S.constructor=On(Ci);Ze=new ke(Ze);Ge=new ke(Ge);var V=ke;var $e=(n=>(n[n.Spot=0]="Spot",n[n.Curve=1]="Curve",n[n.BidAsk=2]="BidAsk",n))($e||{});import{d as Y,DETAILS_KEYS as _e}from"@cetusprotocol/common-sdk";var he=1e3,se=16,$o=-443636,zo=443636,Me=1e4,Rn=16e6,De=2e3,Ie=200,zt=443636n,Fi=1e8,Tn=1e9,$t=1e4,Xo=604800,Yo=1747627200;var et=Ri(je(),1);var go=new et.default(524288),bo=524288,q=64,qi=new et.default(1).shln(q),xn=new et.default(2).pow(new et.default(128)).sub(new et.default(1)),Se=1n<<64n,Fn=(1n<<128n)-1n,wo=BigInt(16e6),Xe=Number(zt),ze=V.clone({precision:80});function Xt(r,e){throw z("InvalidParams",new Error(r),{[_e.METHOD_NAME]:"priceMathV2",[_e.REQUEST_PARAMS]:e}),new Error(r)}function Cn(r,e){let t=r*e>>64n;return t===0n&&Xt("V2 price underflow"),t>Fn&&Xt("V2 price overflow"),t}function qn(r,e){if(e===0||r===Se)return Se;let t=Math.abs(e);t>=bo&&Xt("price exponent overflow",{exp:e});let n=e<0?Se*Se/r:r;(n===0n||n>Fn)&&Xt("Invalid reciprocal",{exp:e});let i=Se;for(;t>0;)t%2===1&&(i=Cn(i,n)),t=Math.floor(t/2),t>0&&(n=Cn(n,n));return i<wo&&Xt("V2 price below precision floor",{exp:e,result:i.toString()}),i}function Ln(r,e){(!Number.isInteger(e)||e<1||e>1e3)&&Xt("bin step outside configured range",{binStep:e}),(!Number.isInteger(r)||r<-Xe||r>Xe)&&Xt("Invalid binId",{binId:r});let t=(BigInt(e)<<64n)/10000n;return qn(Se+t,r)}function kn(r,e){try{return Ln(r,e),!0}catch{return!1}}function Dn(r,e,t){for(;r<e;){let n=Math.floor((r+e)/2);t(n)?e=n:r=n+1}return r}function Nn(r,e,t){for(;r<e;){let n=Math.floor((r+e+1)/2);t(n)?r=n:e=n-1}return r}var G=class r{static splitBinLiquidityInfo(e,t,n){let i=r.getPositionCount(t,n);if(i<=1)return[e];let o=[],l=t;for(let d=0;d<i;d++){let f=Math.min(l+1e3-2,n),c=e.bins.filter(p=>p.bin_id>=l&&p.bin_id<=f);o.push({bins:c,amount_a:c.reduce((p,_)=>Y(p).plus(_.amount_a),Y(0)).toFixed(0),amount_b:c.reduce((p,_)=>Y(p).plus(_.amount_b),Y(0)).toFixed(0)}),l=f+1}return o}static processBinsByRate(e,t){let n=[],i=Y(0),o=Y(0);return e.forEach(l=>{let{amount_a:d,amount_b:f,liquidity:c="0"}=l,p=Y(t).mul(c).toFixed(0),_=Y(d).mul(t),w=Y(f).mul(t);Y(_).lt(1)&&Y(_).gt(0)&&(_=Y(1)),Y(w).lt(1)&&Y(w).gt(0)&&(w=Y(1)),i=Y(i).plus(_),o=Y(o).plus(w),n.push({bin_id:l.bin_id,amount_a:_.toFixed(0),amount_b:w.toFixed(0),price:l.price,price_per_lamport:l.price_per_lamport,liquidity:p})}),{bins:n,amount_a:i.toFixed(0),amount_b:o.toFixed(0)}}static calculateOutByShare(e,t){let{amount_a:n,amount_b:i,liquidity:o="0"}=e;if(o==="0")return{amount_a:"0",amount_b:"0"};if(Y(t).gte(Y(o)))return{amount_a:n,amount_b:i};let l=Y(t).div(o).mul(n).toFixed(0,V.ROUND_FLOOR),d=Y(t).div(o).mul(i).toFixed(0,V.ROUND_FLOOR);return{amount_a:l,amount_b:d}}static getPositionCount(e,t){let i=Y(t).sub(e).add(1).div(1e3);return Number(i.toFixed(0,V.ROUND_UP))}static getLiquidity(e,t,n){return Y(n).mul(e).add(Y(t).mul(Y(2).pow(q))).toFixed(0)}static getAmountAFromLiquidity(e,t){return Y(e).div(Y(t)).toFixed(0)}static getAmountBFromLiquidity(e){return Y(e).div(Y(2).pow(q)).toFixed(0)}static getAmountsFromLiquidity(e,t,n,i){if(Y(i).isZero()&&z("LiquiditySupplyIsZero","Liquidity supply is zero"),Y(n).gt(Y(i))&&z("InvalidDeltaLiquidity","Invalid delta liquidity"),Y(n).isZero())return["0","0"];let o;Y(e).isZero()?o="0":o=Y(e).mul(Y(n)).div(Y(i)).toFixed(0,V.ROUND_FLOOR);let l;return Y(t).isZero()?l="0":l=Y(t).mul(Y(n)).div(Y(i)).toFixed(0,V.ROUND_FLOOR),[o,l]}static getPriceFromBinId(e,t,n,i){let o=r.getPricePerLamportFromBinId(e,t);return r.getPriceFromLamport(n,i,o).toString()}static getPricePerLamportFromBinId(e,t){return r.getPricePerLamportFromQPrice(r.predictBinPrice(e,t))}static getBinIdFromPrice(e,t,n,i,o){let l=r.getPricePerLamport(i,o,e);return r.getBinIdFromLamportPrice(l,t,n)}static getBinIdFromLamportPrice(e,t,n){let{minCreatableBinId:i,maxCreatableBinId:o}=r.getBinIdBoundary(t),l=BigInt(new ze(e).mul(new ze(2).pow(64)).toFixed(0,V.ROUND_HALF_UP)),d=BigInt(r.predictBinPrice(i,t)),f=BigInt(r.predictBinPrice(o,t));return l<=d?i:l>=f?o:n?Nn(i,o,c=>BigInt(r.predictBinPrice(c,t))<=l):Dn(i,o,c=>BigInt(r.predictBinPrice(c,t))>=l)}static getPricePerLamport(e,t,n){return new V(n).mul(new V(10**(t-e))).toString()}static getPriceFromLamport(e,t,n){return new V(n).div(new V(10**(t-e))).toString()}static getReversePrice(e){return new V(1).div(e).toString()}static getQPriceFromId(e,t){let n=new et.default(t).shln(q).div(new et.default(1e4)),i=qi.add(n);return r.pow(i,new et.default(e)).toString()}static getQPriceFromIdV2(e,t){return Ln(e,t).toString()}static powV2(e,t){return new et.default(qn(BigInt(e.toString()),t).toString())}static binIdFromGroupOffset(e,t){(t<0||t>=16)&&Xt("invalid bin offset",{groupIndex:e.toString(),offset:t});let n=e*BigInt(16)+BigInt(t);return n>zt*2n+BigInt(15)&&Xt("bin score outside structural range",{groupIndex:e.toString(),offset:t}),Number(n-zt)}static calculateGroupPrices(e,t){let n=BigInt(e);n*BigInt(16)>zt*2n&&Xt("group anchor outside structural range",{groupIndex:n.toString(),binStep:t});let o=r.binIdFromGroupOffset(n,0),l=r.binIdFromGroupOffset(n,15),d=new et.default(r.getQPriceFromIdV2(o,t));r.getQPriceFromIdV2(l,t);let f=[],c=new et.default(1e4+t),p=new et.default(1e4);for(let _=0;_<16;_++)f.push(d.toString()),_+1<16&&(d=d.mul(c).div(p));return f}static predictBinPrice(e,t){let[n,i]=r.resolveBinPosition(r.binScore(e));return r.calculateGroupPrices(n,t)[i]}static effectiveBinPrice(e,t,n){return n||r.predictBinPrice(e,t)}static resolveBinPrice(e,t,n){let i=r.effectiveBinPrice(e,t,n);return{price:i,price_per_lamport:r.getPricePerLamportFromQPrice(i)}}static validBinIdRangeV2(e){(!Number.isInteger(e)||e<1||e>1e3)&&Xt("bin step outside configured range",{binStep:e});let t=Dn(-Xe,0,i=>kn(i,e)),n=Nn(0,Xe,i=>kn(i,e));return{minBinId:t,maxBinId:n}}static getBinIdBoundary(e){let{minBinId:t,maxBinId:n}=r.validBinIdRangeV2(e),i=BigInt(t)+zt,o=BigInt(n)+zt,l=BigInt(16),d=Number((i+l-1n)/l);o<l-1n&&Xt("no complete valid group",{binStep:e});let f=Number((o-(l-1n))/l);return{minPriceBinId:t,maxPriceBinId:n,minGroupIndex:d,maxGroupIndex:f,minCreatableBinId:r.binIdFromGroupOffset(BigInt(d),0),maxCreatableBinId:r.binIdFromGroupOffset(BigInt(f),15)}}static validGroupIndexRangeV2(e){let{minGroupIndex:t,maxGroupIndex:n}=r.getBinIdBoundary(e);return{firstGroup:t.toString(),lastGroup:n.toString()}}static validGroupBinIdRangeV2(e){let{minCreatableBinId:t,maxCreatableBinId:n}=r.getBinIdBoundary(e);return{minBinId:t,maxBinId:n}}static assertCreatableBinIds(e,t,n="assertCreatableBinIds"){let{minCreatableBinId:i,maxCreatableBinId:o}=r.getBinIdBoundary(e);for(let l of t)(!Number.isInteger(l)||l<i||l>o)&&z("InvalidBinId",new Error("bin id outside creatable range"),{[_e.METHOD_NAME]:n,[_e.REQUEST_PARAMS]:{binStep:e,binId:l,minCreatableBinId:i,maxCreatableBinId:o}})}static getPricePerLamportFromQPrice(e){return new ze(e).div(new ze(2).pow(64)).toString()}static pow(e,t){let n=t.isNeg();if(t.isZero())return qi;if(t=n?t.abs():t,t.gt(go))return new et.default(0);let i=e,o=qi;return i.gte(o)&&(i=xn.div(i),n=!n),t.and(new et.default(1)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(2)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(4)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(8)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(16)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(32)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(64)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(128)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(256)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(512)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(1024)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(2048)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(4096)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(8192)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(16384)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(32768)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(65536)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(131072)).isZero()||(o=o.mul(i).shrn(q)),i=i.mul(i).shrn(q),t.and(new et.default(262144)).isZero()||(o=o.mul(i).shrn(q)),o.isZero()?new et.default(0):(n&&(o=xn.div(o)),o)}static binScore(e){let t=BigInt(e)+zt;return(t<0n||t>zt*2n)&&z("InvalidBinId",new Error("Invalid bin ID"),{[_e.METHOD_NAME]:"binScore",[_e.REQUEST_PARAMS]:{binId:e}}),t.toString()}static scoreToBinId(e){let t=BigInt(e)-zt;return(t<-zt||t>zt)&&z("InvalidBinId",new Error("Invalid score"),{[_e.METHOD_NAME]:"scoreToBinId",[_e.REQUEST_PARAMS]:{score:e}}),Number(t)}static resolveBinPosition(e){let t=BigInt(e),n=t>>4n,i=Number(t&0xfn);return[n.toString(),i]}static findMinMaxBinId(e){let{minCreatableBinId:t,maxCreatableBinId:n}=r.getBinIdBoundary(e);return{minBinId:t,maxBinId:n}}static getBinShift(e,t,n){let i=r.getPricePerLamportFromBinId(e,t),o=Y(i).mul(1+n).toString(),l=r.getBinIdFromLamportPrice(o,t,!0),d=Y(l).sub(e).abs().toFixed(0,V.ROUND_UP);return console.log("getBinShift Options:",{active_id:e,bin_shift:d}),Number(d)}};import{bcs as Q}from"@mysten/sui/bcs";import{blake2b as yo}from"blakejs";function Zn(r){try{let e=r.parsedJson;return{id:e.pool_id,bin_step:Number(e.bin_step),coin_type_a:ae(e.coin_type_a,!1),coin_type_b:ae(e.coin_type_b,!1)}}catch(e){return z("ParseError",e,{[ge.METHOD_NAME]:"parseDlmmBasePool",[ge.REQUEST_PARAMS]:r})}}function Ye(r){try{let e=r.json,t=r.type,n=ji(t),i={bin_step:e.bin_manager.bin_step,bin_manager_handle:e.bin_manager.bins.id,size:e.bin_manager.bins.size},o={bin_step:e.position_manager.bin_step,position_index:e.position_manager.position_index,position_handle:e.position_manager.positions.id,size:e.position_manager.positions.size},l=e.reward_manager,d=l.rewards.map(_=>{let w=_.current_emission_rate,P=vo.fromX64(new Un.default(w)),A=Math.floor(P.toNumber()*60*60*24).toString();return{reward_coin:ae(_.reward_coin,!1),emissions_per_second:P.toString(),emissions_per_day:A,period_emission_rates:{id:_.period_emission_rates.id,size:_.period_emission_rates.size}}}),f={is_public:l.is_public,emergency_reward_pause:l.emergency_reward_pause,vault:{id:l.vault.id,size:l.vault.size},rewards:d,last_updated_time:l.last_updated_time},c={volatility_accumulator:e.v_parameters.volatility_accumulator,volatility_reference:e.v_parameters.volatility_reference,index_reference:Oe(BigInt(e.v_parameters.index_reference.bits)),last_update_timestamp:e.v_parameters.last_update_timestamp,bin_step_config:e.v_parameters.bin_step_config},p={id:e.id,bin_step:Number(e.bin_step),coin_type_a:ae(n.type_arguments[0],!1),coin_type_b:ae(n.type_arguments[1],!1),pool_type:t,index:Number(e.index),bin_manager:i,variable_parameters:c,active_id:Oe(BigInt(e.active_id.bits)),permissions:e.permissions,balance_a:e.balance_a,balance_b:e.balance_b,base_fee_rate:e.base_fee_rate,protocol_fee_a:e.protocol_fee_a,protocol_fee_b:e.protocol_fee_b,url:e.url,reward_manager:f,position_manager:o};return p.bin_step=p.bin_manager.bin_step,p}catch(e){return console.log("\u{1F680} ~ parseDlmmPool ~ error:",e),z("ParseError",e,{[ge.METHOD_NAME]:"parseDlmmPool",[ge.REQUEST_PARAMS]:r})}}function Wi(r){let e=r.json,t=r.type,n=ji(t);return{id:e.id,name:e.name,ref_fee_rate:Ne(e.ref_fee_rate).div(1e4).toNumber(),start_time:Number(e.start_time),end_time:Number(e.end_time),balances:{id:e.balances.id,size:e.balances.size},type:n.full_address}}function Ui(r){try{let e=r.json;return{uri:e.uri,index:e.index,id:e.id,name:e.name,pool_id:e.pool_id,lower_bin_id:Oe(BigInt(e.lower_bin_id.bits)),upper_bin_id:Oe(BigInt(e.upper_bin_id.bits)),liquidity_shares:e.liquidity_shares,description:e.description,coin_type_a:ae(e.coin_type_a,!1),coin_type_b:ae(e.coin_type_b,!1)}}catch(e){return console.log("\u{1F680} ~ parseDlmmPosition ~ error:",e),z("ParseError",e,{[ge.METHOD_NAME]:"parseDlmmPosition",[ge.REQUEST_PARAMS]:r})}}function xs(r,e,t,n,i=[]){let o=new Map;for(let c of i)c.price&&o.set(c.bin_id,c.price);n.price&&o.set(n.bin_id,n.price);let l=r.map((c,p)=>{let _=t+p,{price:w,price_per_lamport:P}=G.resolveBinPrice(_,e,o.get(_));if(_===n.bin_id){let{amount_a:I,amount_b:E}=G.calculateOutByShare(n,c);return{bin_id:_,amount_a:I,amount_b:E,liquidity:c,price:w,price_per_lamport:P}}if(_<n.bin_id){let I=G.getAmountBFromLiquidity(c);return{bin_id:_,amount_a:"0",amount_b:I,liquidity:c,price:w,price_per_lamport:P}}let A=G.getAmountAFromLiquidity(c,w);return{bin_id:_,amount_a:A,amount_b:"0",liquidity:c,price:w,price_per_lamport:P}}),d=l.reduce((c,p)=>c.add(new V(p.amount_a)),new V(0)).toFixed(0),f=l.reduce((c,p)=>c.add(new V(p.amount_b)),new V(0)).toFixed(0);return{bins:l,amount_a:d,amount_b:f}}function Gn(r){try{return Q.vector(Q.struct("BinAmount",{id:Q.struct("I32",{bits:Q.u32()}),amount_a:Q.u64(),amount_b:Q.u64(),price:Q.u128(),liquidity_supply:Q.u128(),rewards_growth_global:Q.vector(Q.u128()),fee_a_growth_global:Q.u128(),fee_b_growth_global:Q.u128()})).parse(r).map(n=>{let i=Oe(BigInt(n.id.bits)),o=n.price.toString();return{bin_id:i,amount_a:n.amount_a,amount_b:n.amount_b,liquidity:n.liquidity_supply,price:o,price_per_lamport:G.getPricePerLamportFromQPrice(o)}})}catch(e){return console.log("\u{1F680} ~ parseBinInfo ~ error:",e),[]}}function Zi(r){try{let e=Oe(BigInt(typeof r.id=="number"?r.id:r.id.bits)),t=r.price.toString();return{bin_id:e,amount_a:r.amount_a,amount_b:r.amount_b,liquidity:r.liquidity_share,price:t,price_per_lamport:G.getPricePerLamportFromQPrice(t)}}catch(e){return z("ParseError",e,{[ge.METHOD_NAME]:"parseBinInfo",[ge.REQUEST_PARAMS]:{fields:r}})}}function Hn(r){let e={},t=r?.filter(n=>n.eventType.includes("pool::CollectFeeEvent"));for(let n=0;n<t.length;n+=1){let{bcs:i}=t[n],o=Q.struct("CollectFeeEvent",{pool:Q.Address,position:Q.Address,fee_a:Q.u64(),fee_b:Q.u64()}).parse(i),l={position_id:o.position,fee_owned_a:o.fee_a,fee_owned_b:o.fee_b};e[o.position]=l}return e}function Qn(r){let e={},t=r?.filter(n=>n.eventType.includes("pool::CollectRewardEvent"));for(let n=0;n<t.length;n+=1){let{bcs:i}=t[n],o=Q.struct("CollectRewardEvent",{pool:Q.Address,position:Q.Address,reward:Li,amount:Q.u64()}).parse(i),l={coin_type:ae(o.reward.name,!1),reward_owned:o.amount},d=e[o.position];d?d.rewards.push(l):d={position_id:o.position,rewards:[l]},e[o.position]=d}return e}function Kn(r,e){let t=r.events?.filter(n=>n.eventType.includes("pool::SwapEvent"));for(let n=0;n<t.length;n+=1){let{json:i}=t[n],{partner:o,pool:l,amount_in:d,amount_out:f,fee:c,ref_fee:p,bin_swaps:_,from:w,target:P}=i,A=_.map(E=>({bin_id:E.bin_id.bits,in_amount:E.amount_in,out_amount:E.amount_out,fee:E.fee,var_fee_rate:E.var_fee_rate}));return{pool_id:l,a2b:e,in_amount:d,out_amount:f,ref_fee_amount:p,fee_amount:c,bin_swaps:A,partner:o,from_coin_type:ae(w,!1),to_coin_type:ae(P,!1)}}}function Cs(r){switch(r){case 0:return 0;case 1:return 1;case 2:return 2}}var Gi=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","ClosePositionEvent"];function Vn(r,e,t,n,i){let o=i?.length?i:Gi,l=[];return r.effects.events.nodes?.forEach((f,c)=>{let p=f.timestamp,{json:_,type:w}=f.contents,{name:P,address:A}=ji(w.repr);if(o.includes(P)&&t===A&&n===_.pool){let I={tx:r.digest,sender:f.sender.address,type:w.repr,block_time:new Date(p).getTime().toString(),index:`${e}_${c}`,parsed_json:_};l.push(I)}}),l}function ks(r,e,t){let n=[],i=0,o=new Date(r*1e3).getTime(),l=new Date().getTime();for(;;){let d=o+i*t;if(d>=l&&n.push(d),i>=e)break;i+=1}return n}function Ds(r,e,t,n){let i=[];for(let o=e;o<=t;o+=n){let l=r.findLast(d=>Ne(o).gte(d.time));l?i.push({time:o.toString(),emissions_per_second:l.emissions_per_second,emissions_per_day:l.emissions_per_day,visualized_time:new Date(o*1e3).toLocaleString()}):i.push({emissions_per_day:"0",time:o.toString(),emissions_per_second:"0",visualized_time:new Date(o*1e3).toLocaleString()})}return i}function Ns(r){if(r.length===0)return;let e=new Date().getTime()/1e3,t=r.findLast(n=>Ne(e).gte(n.time));return t||r[r.length-1]}function le(r,e,t=!1){let n=r.mul(e),i=n.gt(0)&&n.lt(1);if(t&&i)throw new ye(`Multiplication ${n} is less than 1`,"AmountTooSmall");return i?Ne(0):n.floor()}function Fe(r,e=!1){let t=r.gt(0)&&r.lt(1);if(e&&t)throw new ye(`Multiplication ${r.toString()} is less than 1`,"AmountTooSmall");return t?Ne(0):r.floor()}function $n(r){switch(r){case 0:return"spot";case 1:return"curve";case 2:return"bid_ask"}}function zn(r,e,t,n){let i=Buffer.from(r,"utf8"),o=Buffer.from(e,"utf8"),l=o.length,d=0;for(;d<l;){let w=o[d];i=Buffer.concat([i,Buffer.from([w])]),d++}let f=Q.u16().serialize(t).toBytes(),c=Q.u16().serialize(n).toBytes(),p=Buffer.concat([i,f,c]),_=yo(p,void 0,32);return`0x${Buffer.from(_).toString("hex")}`}var Mo=Q.struct("PoolSimpleInfo",{pool_id:Q.Address,pool_key:Q.Address,coin_type_a:Li,coin_type_b:Li,bin_step:Q.u16(),base_factor:Q.u16()}),Wn=Q.bytes(32),Xn=Q.struct("Node<ID, PoolSimpleInfo>",{prev:Q.option(Wn),next:Q.option(Wn),value:Mo}),Po=Q.struct("Bin",{id:Q.u32(),amount_a:Q.u64(),amount_b:Q.u64(),price:Q.u128(),liquidity_share:Q.u128(),rewards_growth_global:Q.vector(Q.u128()),fee_a_growth_global:Q.u128(),fee_b_growth_global:Q.u128()}),Bo=Q.struct("BinGroup",{idx:Q.u32(),used_bins_mask:Q.u16(),bins:Q.vector(Po)}),Ao=Q.struct("BinGroupRef",{pool_id:Q.Address,group:Bo}),Yn=Q.struct("SkipListNode<BinGroupRef>",{score:Q.u64(),prev:Q.vector(jn),next:jn,value:Ao});import{d as O}from"@cetusprotocol/common-sdk";function Io(r,e,t){return G.resolveBinPrice(r,e,t)}function Pe(r,e,t,n){return Io(r,e,r===t?n:void 0)}var ee=class r{static toWeight(e){console.log("\u{1F680} ~ WeightUtils ~ toWeight ~ options:",e);let{strategy_type:t,active_id:n,bin_step:i,lower_bin_id:o,upper_bin_id:l,total_amount_a:d,total_amount_b:f,active_bin_of_pool:c}=e,p=n<o||n>l,_=c?.price,w=G.effectiveBinPrice(n,i,_),P=O(0),A=O(0),I=O(200);if(t===2?I=O(200):t===1?I=O(2e3):t===0&&(I=O(1)),!p&&c){let nt=this.calculateActiveWeights(c.amount_a,c.amount_b,w,I);P=nt.active_weight_a,A=nt.active_weight_b}n===o&&O(f).isZero()&&(P=O(I).mul(O(2).pow(q*2)).div(O(w)).floor(),A=O(0)),n===l&&O(d).isZero()&&(A=O(I).mul(O(2).pow(q)).floor(),P=O(0)),n>o&&n<l&&(O(d).isZero()&&(A=O(I).mul(O(2).pow(q)).floor(),P=O(0)),O(f).isZero()&&(P=O(I).mul(O(2).pow(q*2)).div(O(w)).floor(),A=O(0)));let E=p?O(0):P,T=p?O(0):A,L=O(2e3).sub(O(200)).floor(),R=n>l?l:n,C=n<o?o:n,k=n>o?R===o?O(0):L.div(O(R-o)).floor():O(0),x=l>n?C===l?O(0):L.div(O(l-C)).floor():O(0),K=o,rt=[],at=[];for(;K<=l;){let nt=O(0);if(K<n){let U=R-K;t===0?nt=O(1):t===2?nt=O(I).add(k.mul(U)).floor():t===1&&(nt=O(I).sub(k.mul(U)).floor())}else if(K>n){let U=K-C;t===0?nt=O(1):t===2?nt=O(I).add(x.mul(U)).floor():t===1&&(nt=O(I).sub(x.mul(U)).floor())}else nt=I;if(rt.push(nt),K<n)T=T.add(nt.mul(O(2).pow(q))).floor(),at.push(O(0));else if(K>n){let U=nt.mul(O(2).pow(q*2)).div(G.effectiveBinPrice(K,i,K===n?_:void 0)).floor();at.push(U),E=E.add(U).floor()}else at.push(O(0));K+=1}return{...e,total_weight_a:E,total_weight_b:T,active_weight_a:P,active_weight_b:A,weights:rt,weight_per_prices:at}}static toWeightSpotBalanced(e,t){let n=[];for(let i=e;i<=t;i++)n.push({bin_id:i,weight:1});return n}static toWeightDescendingOrder(e,t){let n=[];for(let i=e;i<=t;i++)n.push({bin_id:i,weight:t-i+1});return n}static toWeightAscendingOrder(e,t){let n=[];for(let i=e;i<=t;i++)n.push({bin_id:i,weight:i-e+1});return n}static toWeightCurve(e,t,n){if(n<e)return r.toWeightDescendingOrder(e,t);if(n>t)return r.toWeightAscendingOrder(e,t);let i=2e3,l=i-200,d=n>e?Math.floor(l/(n-e)):0,f=t>n?Math.floor(l/(t-n)):0,c=[];for(let p=e;p<=t;p++)p<n?c.push({bin_id:p,weight:i-(n-p)*d}):p>n?c.push({bin_id:p,weight:i-(p-n)*f}):c.push({bin_id:p,weight:i});return c}static toWeightBidAsk(e,t,n){if(n>t)return r.toWeightDescendingOrder(e,t);if(n<e)return r.toWeightAscendingOrder(e,t);let i=2e3,o=200,l=i-o,d=n>e?Math.floor(l/(n-e)):0,f=t>n?Math.floor(l/(t-n)):0,c=[];for(let p=e;p<=t;p++)p<n?c.push({bin_id:p,weight:o+(n-p)*d}):p>n?c.push({bin_id:p,weight:o+(p-n)*f}):c.push({bin_id:p,weight:o});return c}static toAmountBidSide(e,t,n,i,o=!1,l){let d=i.filter(c=>c.bin_id<=e).reduce(function(c,p){return o?p.bin_id>e?c:c.add(p.weight):p.bin_id>=e?c:c.add(p.weight)},O(0));if(d.cmp(O(0))!=1)throw Error("Invalid parameters");return{bins:i.map(c=>{let{price:p,price_per_lamport:_}=Pe(c.bin_id,n,e,l);if(!(c.bin_id<=e)||c.bin_id>=e&&!o)return{bin_id:c.bin_id,amount_a:"0",amount_b:"0",price:p,price_per_lamport:_,liquidity:"0"};{let P=O(c.weight).div(d),A=le(O(t),P).toString(),I="0",E=G.getLiquidity(I,A,p);return{bin_id:c.bin_id,amount_b:A,amount_a:I,price:p,price_per_lamport:_,liquidity:E}}}),amount_a:"0",amount_b:t}}static toAmountAskSide(e,t,n,i,o=!1,l){let d=i.filter(c=>c.bin_id>=e).reduce(function(c,p){if(p.bin_id<=e&&!o)return c;{let{price_per_lamport:_}=Pe(p.bin_id,t,e,l),w=new V(p.weight).div(_);return c.add(w)}},new V(0));if(d.cmp(new V(0))!=1)throw Error("Invalid parameters");return{bins:i.map(c=>{let{price:p,price_per_lamport:_}=Pe(c.bin_id,t,e,l);if(!(c.bin_id>=e)||c.bin_id<=e&&!o)return{bin_id:c.bin_id,amount_a:"0",amount_b:"0",price:p,price_per_lamport:_,liquidity:"0"};{let A=new V(c.weight).div(_).div(d),I=le(O(n),A).toString(),E="0",T=G.getLiquidity(I,E,p);return{bin_id:c.bin_id,amount_a:I,amount_b:E,price:p,price_per_lamport:_,liquidity:T}}}),amount_a:n,amount_b:"0"}}static toAmountBothSide(e,t,n,i,o,l,d,f){let c=!O(n).isZero()&&O(i).isZero(),p=O(n).isZero()&&!O(i).isZero();if(e>d[d.length-1].bin_id)return r.toAmountBidSide(e,i,t,d,!1,f);if(p&&e!==d[d.length-1].bin_id)return r.toAmountBidSide(e,i,t,d,!0,f);if(e<d[0].bin_id)return r.toAmountAskSide(e,t,n,d,!1,f);if(c&&e!==d[0].bin_id)return r.toAmountAskSide(e,t,n,d,!0,f);let _=d.filter(w=>w.bin_id===e);if(_.length===1){let{totalWeightA:w,totalWeightB:P,activeWeightA:A,activeWeightB:I}=r.calculateTotalWeights(t,d,e,_[0],o,l,c?"a":p?"b":void 0,f),E=new V(n.toString()).div(w),T=new V(i.toString()).div(P),L=d.map(k=>{let{price:x,price_per_lamport:K}=Pe(k.bin_id,t,e,f);if(k.bin_id<e||k.bin_id===e&&p){let y=le(T,new V(k.weight)),u=G.getLiquidity("0",y.toString(),x);return{bin_id:k.bin_id,amount_a:"0",amount_b:y.toString(),price:x,price_per_lamport:K,liquidity:u}}if(k.bin_id>e||k.bin_id===e&&c){let y=new V(k.weight).div(K),s=le(E,new V(y)),m=G.getLiquidity(s.toString(),"0",x);return{bin_id:k.bin_id,amount_a:s.toString(),amount_b:"0",price:x,price_per_lamport:K,liquidity:m}}let rt=le(E,A),at=le(T,I),nt=rt.toString(),U=at.toString(),ot=G.getLiquidity(nt,U,x);return{bin_id:k.bin_id,amount_a:nt,amount_b:U,price:x,price_per_lamport:K,liquidity:ot}}),R=L.reduce((k,x)=>O(k).add(O(x.amount_a)),O(0)).toString(),C=L.reduce((k,x)=>O(k).add(O(x.amount_b)),O(0)).toString();return{bins:L,amount_a:R,amount_b:C}}else{let{totalWeightA:w,totalWeightB:P}=r.calculateTotalWeights(t,d,e,void 0,void 0,void 0,void 0,f),A=new V(n.toString()).div(w),I=new V(i.toString()).div(P),E=d.map(R=>{let{price:C,price_per_lamport:k}=Pe(R.bin_id,t,e,f);if(R.bin_id<e){let x=le(I,new V(R.weight));return{bin_id:R.bin_id,amount_a:"0",amount_b:x.toString(),price:C,price_per_lamport:k}}else{let x=new V(R.weight).div(k),K=le(A,x);return{bin_id:R.bin_id,amount_a:K.toString(),amount_b:"0",price:C,price_per_lamport:k}}}),T=E.reduce((R,C)=>O(R).add(O(C.amount_a)),O(0)).toString(),L=E.reduce((R,C)=>O(R).add(O(C.amount_b)),O(0)).toString();return{bins:E,amount_a:T,amount_b:L}}}static autoFillCoinByWeight(e,t,n,i,o,l,d,f){if(e>d[d.length-1].bin_id)return r.toAmountBidSide(e,n,t,d,!1,f);if(e<d[0].bin_id)return r.toAmountAskSide(e,t,n,d,!1,f);let c=d.filter(A=>A.bin_id===e),{totalWeightA:p,totalWeightB:_}=r.calculateTotalWeights(t,d,e,c.length===1?c[0]:void 0,c.length===1?o:void 0,c.length===1?l:void 0,void 0,f),w=O(0);i?w=p.isZero()?new V(0):new V(n).div(p):w=_.isZero()?new V(0):new V(n).div(_);let P=le(w,i?_:p).toString();return r.toAmountBothSide(e,t,i?n:P,i?P:n,o,l,d,f)}static calculateActiveWeights(e,t,n,i){let o=O(n),l=O(e),d=O(t),f=O(0),c=O(0);if(l.isZero()&&d.isZero())f=O(i).mul(O(2).pow(q*2)).div(o.mul(2)).floor(),c=O(i).mul(O(2).pow(q)).div(2).floor();else{if(l.isZero())f=O(0);else{let p=d.mul(O(2).pow(q)).div(l);f=O(i).mul(O(2).pow(q*2)).div(o.add(p)).floor()}if(d.isZero())c=O(0);else{let p=O(2).pow(q).add(o.mul(l).div(d)).floor();c=O(i).mul(O(2).pow(q*2)).div(p).floor()}}return{active_weight_a:f,active_weight_b:c}}static calculateTotalWeights(e,t,n,i,o,l,d,f){let c=O(Pe(n,e,n,f).price_per_lamport),p=O(0),_=O(0);if(o&&l&&i&&!d)if(O(o).isZero()&&O(l).isZero())p=new V(i.weight).div(c.mul(new V(2))),_=new V(i.weight).div(new V(2));else{let A=new V(o.toString()),I=new V(l.toString());O(o).isZero()||(p=new V(i.weight).div(c.add(I.div(A)))),O(l).isZero()||(_=new V(i.weight).div(new V(1).add(c.mul(A).div(I))))}let w=p,P=_;return t.forEach(A=>{if((A.bin_id<n||d==="b")&&(P=P.add(new V(A.weight))),A.bin_id>n||d==="a"){let{price_per_lamport:I}=Pe(A.bin_id,e,n,f),E=new V(A.weight).div(I);w=w.add(E)}}),{totalWeightA:w,totalWeightB:P,activeWeightA:p,activeWeightB:_}}};import{d as Yt}from"@cetusprotocol/common-sdk";var qe=class{static toAmountsByWeights(e){let{total_weight_a:t,total_weight_b:n,weights:i,weight_per_prices:o,total_amount_a:l,total_amount_b:d,active_weight_a:f,active_weight_b:c,lower_bin_id:p,upper_bin_id:_,active_id:w,bin_step:P,active_bin_of_pool:A}=e,I=n.isZero()?Yt(0):Yt(d).mul(Yt(2).pow(q*2)).div(n).floor(),E=t.isZero()?Yt(0):Yt(l).mul(Yt(2).pow(q*2)).div(t).floor(),T=Fe(E.mul(f).div(Yt(2).pow(q*2))),L=Fe(I.mul(c).div(Yt(2).pow(q*2))),R=_-p+1,C=p,k=0,x=[];for(;k<R;){let rt=Yt(0),at=Yt(0),nt=C===w?A?.price:void 0,{price:U,price_per_lamport:ot}=G.resolveBinPrice(C,P,nt);C<w?at=Fe(I.mul(i[k]).div(Yt(2).pow(q))):C>w?rt=Fe(E.mul(o[k]).div(Yt(2).pow(q*2))):(rt=T,at=L);let y=G.getLiquidity(rt.toString(),at.toString(),U);x.push({bin_id:C,amount_a:rt.toString(),amount_b:at.toString(),price:U,price_per_lamport:ot,liquidity:y}),C+=1,k+=1}return{bins:x,amount_a:l.toString(),amount_b:d.toString()}}static toAmountsBothSideByStrategy(e,t,n,i,o,l,d,f){let c=ee.toWeight({strategy_type:d,active_id:e,bin_step:t,lower_bin_id:n,upper_bin_id:i,total_amount_a:o,total_amount_b:l,active_bin_of_pool:f});return this.toAmountsByWeights(c)}static autoFillCoinByStrategy(e,t,n,i,o,l,d,f){switch(d){case 0:{let c=ee.toWeightSpotBalanced(o,l);return ee.autoFillCoinByWeight(e,t,n,i,f?.amount_a||"0",f?.amount_b||"0",c,f?.price)}case 1:{let c=ee.toWeightCurve(o,l,e);return ee.autoFillCoinByWeight(e,t,n,i,f?.amount_a||"0",f?.amount_b||"0",c,f?.price)}case 2:{let c=ee.toWeightBidAsk(o,l,e);return ee.autoFillCoinByWeight(e,t,n,i,f?.amount_a||"0",f?.amount_b||"0",c,f?.price)}}}static autoFillCoinByStrategyV2(e,t,n,i,o,l,d,f){let c=this.autoFillCoinByStrategy(e,t,n,i,o,l,d,f),p=ee.toWeight({strategy_type:d,active_id:e,bin_step:t,lower_bin_id:o,upper_bin_id:l,total_amount_a:c.amount_a,total_amount_b:c.amount_b,active_bin_of_pool:f});return this.toAmountsByWeights(p)}};import{d as lt}from"@cetusprotocol/common-sdk";var Je=class r{static previewVolatilityParameter(e,t,n=Math.floor(Date.now()/1e3)){let i={...e,bin_step_config:{...e.bin_step_config}},o=n-Number(i.last_update_timestamp);if(o<0)return i;let{filter_period:l,decay_period:d,reduction_factor:f,max_volatility_accumulator:c}=i.bin_step_config;o>=l&&(i.index_reference=t,o<d?i.volatility_reference=lt(i.volatility_accumulator).mul(f).div(1e4).floor().toFixed(0):i.volatility_reference="0"),i.last_update_timestamp=n.toString();let p=Math.abs(i.index_reference-t),_=lt(i.volatility_reference).add(lt(p).mul(1e4));return _.gt(c)?i.volatility_accumulator=c:i.volatility_accumulator=_.toFixed(0),i}static getVariableFee(e){let{volatility_accumulator:t,bin_step_config:n}=e,{variable_fee_control:i,bin_step:o}=n;return lt(i).gt(0)?lt(t).mul(o).pow(2).mul(i).add(99999999999).div(1e11).toFixed(0):"0"}static calculateCompositionFee(e,t){let n=lt(e).mul(t);return lt(n).mul(lt(1e9).add(t)).div(1e18).toFixed(0)}static calculateProtocolFee(e,t){return lt(e).mul(t).div(1e4).ceil().toFixed(0)}static getProtocolFees(e,t,n){let i=r.calculateProtocolFee(e,n),o=r.calculateProtocolFee(t,n);return{protocol_fee_a:i,protocol_fee_b:o}}static getCompositionFees(e,t,n){if(lt(e.liquidity||"0").eq(lt(0)))return{fees_a:"0",fees_b:"0"};let i=r.previewVolatilityParameter(n,e.bin_id),{bin_step:o,base_factor:l}=i.bin_step_config,d=G.effectiveBinPrice(e.bin_id,o,e.price),f=G.getLiquidity(e.amount_a,e.amount_b,d),c=G.getLiquidity(t.amount_a,t.amount_b,d),p=lt(e.liquidity).mul(c).div(f).toFixed(0),{amount_a:_,amount_b:w}=G.calculateOutByShare({bin_id:e.bin_id,liquidity:lt(e.liquidity).add(p).toFixed(0),amount_a:lt(e.amount_a).add(t.amount_a).toFixed(0),amount_b:lt(e.amount_b).add(t.amount_b).toFixed(0),price_per_lamport:e.price_per_lamport},p),P=lt(o).mul(l).mul(10),A=r.getVariableFee(i),I=lt(P).add(A).toFixed(0);lt(I).gt(1e8)&&(I=1e8.toString());let E="0",T="0";return lt(_).gt(t.amount_a)&&lt(t.amount_b).gt(w)?T=r.calculateCompositionFee(lt(t.amount_b).sub(w).toFixed(0),I):lt(w).gt(t.amount_b)&&lt(t.amount_a).gt(_)&&(E=r.calculateCompositionFee(lt(t.amount_a).sub(_).toFixed(0),I)),{fees_a:E,fees_b:T}}};var Jn=class{static calculateIlm(e){let{curvature:t,initial_price:n,max_price:i,bin_step:o,total_supply:l,pool_share_percentage:d,config:f}=e,{price_curve_points_num:c,liquidity_distribution_num:p,tokens_table_num:_,price_table_num:w}=f;if(d<0||d>100)throw new Error("Pool Share Percentage must be greater than 0 and less than 100.");let P=!1;if(i<n)throw new Error("Maximum Price must be greater or equal to Initial Price.");if(i===n&&t!==0)throw new Error("Curvature must be 0 when Maximum and Initial Price are equal.");if(i!==n&&t===0)throw new Error("Maximum and Initial Price must be equal when Curvature is 0.");i===n&&t===0&&(P=!0);let A=F=>x*Math.pow(F/k,t)+n,I=(F,it)=>{let Z=x*Math.pow(k,-t)*Math.pow(F,t+1)/(t+1)+n*F,tt=x*Math.pow(k,-t)*Math.pow(it,t+1)/(t+1)+n*it;return Z-tt},E=F=>k*Math.pow((F-n)/x,1/t),T=F=>k*Math.pow(F-n,1/t-1)/(t*Math.pow(x,1/t)),L=F=>Math.pow((F-n)/x,1/t)*k,R=(F,it,Z)=>{let tt=it/1e4,xe=Math.log(F)/Math.log(1+tt);return Z?Math.floor(xe):Math.ceil(xe)},C=(F,it)=>{let Z=it/1e4;return Math.pow(1+Z,F)},k=l*d/100,x=i-n,K=[],rt=[],at=R(n,o,!1),nt=R(i,o,!1),U=0,ot=at;if(P)K.push(n),rt.push(n*k);else for(;ot<=nt;){let F=C(ot,o),it=C(ot+1,o),Z=L(it)-L(F);U=U+Z,rt.push(Z*F),K.push(F),ot++}let y={price_curve:{data:[],min_y:0,max_y:0},liquidity_curve:{data:[],min_y:0,max_y:0},dlmm_bins:{data:[],min_y:0,max_y:0},tokens_table:[],price_table:[],initial_fdv:n*l,final_fdv:i*l,usdc_in_pool:0};var s=x/100;let u=[],m=0,h=0;for(let F=0;F<c;F++){let it=F*k/(c-1),Z=P?i:A(it);m=m<Z?m:Z,h=h>Z?h:Z,u.push({x:it,y:Z})}y.price_curve={data:u,min_y:m,max_y:h};let b=[],M=0,B=0;if(P)M=0,B=k,b=[{x:n,y:0},{x:n,y:k/2},{x:n,y:k}];else for(let F=0;F<p;F++){let it=n+F*s,Z=T(it);b.push({x:it,y:Z}),M=M<Z?M:Z,B=B>Z?B:Z}y.liquidity_curve={data:b,min_y:M,max_y:B};let g=[],a=0,v=0;K.forEach((F,it)=>{let Z=F,tt=rt[it];g.push({x:Z,y:tt}),a=a<tt?a:tt,v=v>tt?v:tt}),y.dlmm_bins={data:g,min_y:a,max_y:v};let D=[],N=0,j=k/_;for(;N<=k;){let F=A(N),it=N,Z=I(it,0);D.push({withdrawn:it,price:F,usdc_in_pool:Z}),N=N+j}y.tokens_table=D;let X=Math.floor(x*1e10)/1e11,J=[];if(P)J.push({withdrawn:k,price:n,usdc_in_pool:k*n});else{let F=0,it=n;for(;F<=w;){let Z=E(it),tt=I(Z,0);J.push({withdrawn:Z,price:it,usdc_in_pool:tt}),it=it+X,F=F+1}}return y.price_table=J,y.usdc_in_pool=J[J.length-1].usdc_in_pool,y}};import{normalizeSuiAddress as nr}from"@mysten/sui/utils";import{bcs as Re}from"@mysten/sui/bcs";var ei=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getPoolAddress(e,t,n,i,o){try{let l=zn(e,t,n,i),{dlmm_pool:d}=this._sdk.sdkOptions,{pools_id:f}=ir(d),c=await this._sdk.FullClient.getDynamicField({parentId:f,name:{type:"0x2::object::ID",bcs:Re.Address.serialize(l).toBytes()},signal:o?.signal});return Xn.parse(c.dynamicField.value.bcs).value.pool_id}catch(l){return z("FetchError",l,{[ie.METHOD_NAME]:"getPoolAddress",[ie.REQUEST_PARAMS]:{coin_type_a:e,coin_type_b:t,bin_step:n,base_factor:i}})}}async getBasePoolList(e="all",t=!1,n){let{dlmm_pool:i}=this._sdk.sdkOptions,o={data:[],has_next_page:!1},l=e==="all",d=`${i.package_id}_getBasePoolList`;if(l){let f=this._sdk.getCache(d,t);if(f&&f.length>0)return o.data.push(...f),o}try{let f=`${i.package_id}::registry::CreatePoolEvent`,c=await this._sdk.FullClient.queryEventsByGraphQL({MoveEventType:f},e,n);o.has_next_page=c.has_next_page,o.next_cursor=c.next_cursor,c.data.forEach(p=>{let _=Zn(p);o.data.push(_)}),l&&this._sdk.updateCache(`${i.package_id}_getPoolImmutables`,o.data)}catch(f){return z("FetchError",f,{[ie.METHOD_NAME]:"getBasePoolList"})}return o}async getPools(e="all",t=!1,n){let i={data:[],has_next_page:!1},o=await this.getBasePoolList(e,t,n);if(o.data.length===0)return i;try{let l=await this._sdk.FullClient.batchGetObjects(o.data.map(d=>d.id),{json:!0},1e3,n);i.has_next_page=o.has_next_page,i.next_cursor=o.next_cursor;for(let d of l){let f=Ye(d),c=`${f.id}_getDlmmPool`;this._sdk.updateCache(c,f),i.data.push(f)}}catch(l){return z("FetchError",l,{[ie.METHOD_NAME]:"getPools"})}return i}async getBinInfo(e,t,n,i=!0,o){try{let l=`${e}_getBinInfo_${t}`,d=this._sdk.getCache(l,i);if(d!==void 0)return d;let f=G.binScore(t),[c,p]=G.resolveBinPosition(f),_=await this._sdk.FullClient.getDynamicField({parentId:e,name:{type:"u64",bcs:Re.u64().serialize(c).toBytes()},signal:o?.signal}),P=Yn.parse(_.dynamicField.value.bcs).value.group.bins[p],A=Zi(P);return this._sdk.updateCache(l,A),A}catch{return{bin_id:t,amount_a:"0",amount_b:"0",liquidity:"0",...G.resolveBinPrice(t,n)}}}async getBinInfoList(e,t){let n=[],i=e.map(l=>{let{bin_manager_handle:d,bin_id:f,bin_step:c}=l,p=G.binScore(f),[_,w]=G.resolveBinPosition(p),P=Ro(d,_,"u64","u64");return{groupIndex:_,offsetInGroup:w,bin_id:f,bin_step:c,bin_manager_handle:d,dynamic_field_id:P}});return(await this._sdk.FullClient.batchGetObjects(i.map(l=>l.dynamic_field_id),{json:!0},1e3,t)).forEach((l,d)=>{let{offsetInGroup:f,bin_manager_handle:c,bin_step:p,bin_id:_}=i[d];try{let P=l.json.value.value.group.bins[f],A=Zi(P);n.push({...A,bin_manager_handle:c,bin_step:p})}catch{n.push({bin_id:_,amount_a:"0",amount_b:"0",liquidity:"0",...G.resolveBinPrice(_,p),bin_manager_handle:c,bin_step:p})}}),n}async getTotalFeeRate(e,t){let{dlmm_pool:n}=this._sdk.sdkOptions,{pool_id:i,coin_type_a:o,coin_type_b:l}=e,d=new ti;d.moveCall({target:`${n.published_at}::pool::get_total_fee_rate`,arguments:[d.object(i)],typeArguments:[o,l]});let f=await this._sdk.FullClient.sendSimulationTransaction(d,nr("0x0"),!0,t);return Re.struct("FeeRate",{base_fee_rate:Re.u64(),var_fee_rate:Re.u64(),total_fee_rate:Re.u64()}).parse(f.commandResults[0].returnValues[0].bcs)}async getPoolBinInfo(e,t){let{dlmm_pool:n}=this._sdk.sdkOptions,{pool_id:i,coin_type_a:o,coin_type_b:l}=e,d=900,f=[],c,p=!0;for(;p;){let _=new ti,w;c!==void 0?w=_.moveCall({target:"0x1::option::some",arguments:[_.pure.u32(Number(tr(BigInt(c))))],typeArguments:["u32"]}):w=_.moveCall({target:"0x1::option::none",typeArguments:["u32"]});try{_.moveCall({target:`${n.published_at}::pool::fetch_bins`,arguments:[_.object(i),w,_.pure.u64(d)],typeArguments:[o,l]});let P=await this._sdk.FullClient.sendSimulationTransaction(_,nr("0x0"),!0,t),A=Gn(P.commandResults[1].returnValues[0].bcs);f.push(...A),c=A.length>0?A[A.length-1].bin_id+1:void 0,p=A.length===d}catch{p=!1}}return f.sort((_,w)=>_.bin_id-w.bin_id)}async getPoolTransactionList({pool_id:e,pagination_args:t,order:n="descending",full_rpc_url:i,event_types:o},l){let{FullClient:d,sdkOptions:f}=this._sdk,c;i?c=Oo(So(i,this._sdk.sdkOptions.env),d._graphQLClient,this._sdk.sdkOptions.env):c=d;let p={data:[],has_next_page:!1},_=50,w=t,P=t.limit||10,A=o?.length?o:Gi,I=10,E=0;do{E++;let T=await c.queryTransactionBlocksByPage({affectedObject:e},{...w,limit:50},n,l);T.data.forEach((L,R)=>{let C=Vn(L,R,f.dlmm_pool.package_id,e,A);p.data=[...p.data,...C]}),p.has_next_page=T.has_next_page,p.next_cursor=T.next_cursor,w.cursor=T.next_cursor}while(p.data.length<P&&p.has_next_page&&E<I);return E>=I&&p.data.length<P&&(p.has_next_page=!1),p.data.length>P&&(p.data=p.data.slice(0,P),p.has_next_page=!0),p}async getRangeBinInfo(e,t,n,i,o){let l=[];for(let d=t;d<=n;d++){let f=await Promise.all([this.getBinInfo(e,d,i,!0,o)]);l.push(...f)}return l}async getAssignPoolList(e,t){if(e.length===0)return[];let n=[];try{let i=await this._sdk.FullClient.batchGetObjects(e,{json:!0},1e3,t);for(let o of i){let l=Ye(o),d=`${l.id}_getDlmmPool`;this._sdk.updateCache(d,l),n.push(l)}}catch(i){return z("FetchError",i,{[ie.METHOD_NAME]:"getAssignPoolList"})}return n}async getPool(e,t=!0,n){try{let i=`${e}_getDlmmPool`,o=this._sdk.getCache(i,t);if(o!==void 0)return o;let l=await this._sdk.FullClient.getObject({objectId:e,include:{json:!0},signal:n?.signal}),d=Ye(l.object);return this._sdk.updateCache(i,d),d}catch(i){return z("FetchError",i,{[ie.METHOD_NAME]:"getPool",[ie.REQUEST_PARAMS]:e})}}async createPoolAndAddWithPricePayload(e){let{bin_step:t,url:n,coin_type_a:i,coin_type_b:o,bin_infos:l,price_base_coin:d,price:f,lower_price:c,upper_price:p,decimals_a:_,decimals_b:w,strategy_type:P,use_bin_infos:A,base_factor:I,pool_id:E}=e,T,L,R,C=l;if(d==="coin_a")T=G.getBinIdFromPrice(c,t,!1,_,w),L=G.getBinIdFromPrice(p,t,!0,_,w),R=G.getBinIdFromPrice(f,t,!0,_,w);else{T=G.getBinIdFromPrice(Hi(1).div(p).toString(),t,!1,_,w),L=G.getBinIdFromPrice(Hi(1).div(c).toString(),t,!0,_,w),R=G.getBinIdFromPrice(Hi(1).div(f).toString(),t,!1,_,w);let K={pool_id:E,amount_a:l.amount_b,amount_b:l.amount_a,active_id:R,bin_step:t,lower_bin_id:T,upper_bin_id:L,active_bin_of_pool:void 0,strategy_type:e.strategy_type};C=await this.sdk.Position.calculateAddLiquidityInfo(K)}let x={bin_step:t,url:n,coin_type_a:i,coin_type_b:o,bin_infos:C,lower_bin_id:T,upper_bin_id:L,active_id:R,strategy_type:P,use_bin_infos:A,base_factor:I};return this.createPoolAndAddLiquidityPayload(x)}createPoolPayload(e,t){let{dlmm_pool:n}=this._sdk.sdkOptions,{bin_step:i,base_factor:o,url:l,coin_type_a:d,coin_type_b:f,active_id:c}=e;t=t||new ti,G.assertCreatableBinIds(i,[c],"createPoolPayload");let{registry_id:p,global_config_id:_,versioned_id:w}=ir(n),[P,A]=t.moveCall({target:`${n.published_at}::registry::create_pool_v3`,arguments:[t.object(p),t.pure.u16(i),t.pure.u16(o),t.pure.u32(Number(tr(BigInt(c)))),t.pure.string(l||""),t.object(_),t.object(w),t.object(Eo)],typeArguments:[d,f]});return t.moveCall({target:`${n.published_at}::registry::destroy_receipt`,arguments:[t.object(P),A,t.object(w)],typeArguments:[d,f]}),A}createPoolAndAddLiquidityPayload(e){let{bin_step:t,base_factor:n,url:i,active_id:o,coin_type_a:l,coin_type_b:d,bin_infos:f,lower_bin_id:c,upper_bin_id:p,strategy_type:_,use_bin_infos:w}=e,P=new ti;To(er(l,!1),er(d,!1))&&z("InvalidCoinTypeSequence",new Error("invalid coin type sequence"),{[ie.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[ie.REQUEST_PARAMS]:e});let A=p-c+1;A>1e3&&z("InvalidBinWidth",new Error("Width is too large"),{[ie.METHOD_NAME]:"openPosition",[ie.REQUEST_PARAMS]:e}),console.log("\u{1F680} ~ createPoolAndAddLiquidityPayload ~ option:",{...e,width:A});let E={pool_id:this.createPoolPayload({active_id:o,bin_step:t,base_factor:n,coin_type_a:l,coin_type_b:d},P),bin_infos:f,coin_type_a:l,coin_type_b:d,lower_bin_id:c,upper_bin_id:p,active_id:o,strategy_type:_,use_bin_infos:w,max_price_slippage:0,bin_step:t};return this.sdk.Position.addLiquidityPayload(E,P),P}};import{FullRpcUrlTestnet as xo,GraphRpcUrlTestnet as Co}from"@cetusprotocol/common-sdk";var rr={env:"testnet",full_rpc_url:xo,graph_rpc_url:Co,dlmm_pool:{package_id:"0xb382224d12558da5f87624765065a8c7e8f5c899d0ee890610e2bb4e8c686be9",published_at:"0x1cdac9c678c5ec89c80409db8865c9c3f8e6207dfd988fb4ddd11e2806db0bf9",version:1,config:{registry_id:"0xdc91c4f094557b9d2a35fc6159ef32649a54c4aa9350860bf0d4b52b5f0a3990",pools_id:"0xb518b1de84a1ba1ab6c9a2d71fcdf382ef36045a4660497b3d77fb0a6df7709e",global_config_id:"0xe84ebca8d61cdd9312cec3787204d0ee1063d424a81dcda62cfb8b1887041a9f",versioned_id:"0xf8478d6dc081bc266229f25ac9c31a96b5e99ecbd82222e883368dda95829065",admin_cap_id:"0x273dbda2a1d62460a01a07831aec0fa8191a41341d634fb43f15acdb627edbce",partners_id:"0xd7ac594ed2e7756f0d4a98503dd970f314f2619a10c3613c705253a155cc9fea"}},dlmm_router:{package_id:"0xbc76d04e910452518efd5fd63d5fffe77c24855063c9cdb5501e06896bc34908",published_at:"0xe122ffbcfe5091398ba15a60e654e6589fd586b3865d5b4118292754f8cde301",version:1},faucet:{package_id:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",published_at:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",version:1}};import{asUintN as de,CLOCK_ADDRESS as be,CoinAssist as ne,d as ut,DETAILS_KEYS as Jt,getPackagerConfigs as we}from"@cetusprotocol/common-sdk";import{Transaction as ve}from"@mysten/sui/transactions";var ii=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}buildPositionType(){return`${this._sdk.sdkOptions.dlmm_pool.package_id}::position::Position`}async getOwnerPositionList(e,t){let n=[];try{(await this._sdk.FullClient.getOwnedObjectsByPage(e,this.buildPositionType(),"all",t)).data.forEach(o=>{n.push(Ui(o))})}catch(i){console.log("\u{1F680} ~ PositionModule ~ getOwnerPositionList ~ error:",i),z("GetObjectError",i,{[Jt.METHOD_NAME]:"getOwnerPositionList",[Jt.REQUEST_PARAMS]:e})}return n}async getPosition(e,t){try{let n=await this._sdk.FullClient.getObject({objectId:e,include:{json:!0},signal:t?.signal});return Ui(n.object)}catch(n){return console.log("\u{1F680} ~ PositionModule ~ getPosition ~ error:",n),z("GetObjectError",n,{[Jt.METHOD_NAME]:"getPosition",[Jt.REQUEST_PARAMS]:e})}}collectFeePayload(e,t){t=t||new ve;let{fee_a_obj:n,fee_b_obj:i}=this.collectFeePayloadWithoutTransfer(e,t);return t.transferObjects([n,i],this.sdk.getSenderAddress()),t}collectFeePayloadWithoutTransfer(e,t){let{pool_id:n,position_id:i,coin_type_a:o,coin_type_b:l}=e,{dlmm_pool:d}=this.sdk.sdkOptions,{versioned_id:f,global_config_id:c}=we(d),[p,_]=t.moveCall({target:`${d.published_at}::pool::collect_position_fee`,arguments:[t.object(n),t.object(i),t.object(c),t.object(f)],typeArguments:[o,l]}),w=ne.fromBalance(p,o,t),P=ne.fromBalance(_,l,t);return{fee_a_obj:w,fee_b_obj:P}}updatePositionFeeAndRewards(e,t){let{dlmm_pool:n}=this.sdk.sdkOptions,{versioned_id:i}=we(n),{pool_id:o,position_id:l,coin_type_a:d,coin_type_b:f}=e;return t.moveCall({target:`${n.published_at}::pool::update_position_fee_and_rewards`,arguments:[t.object(o),t.pure.id(l),t.object(i),t.object(be)],typeArguments:[d,f]}),t}collectRewardPayload(e,t){let{dlmm_pool:n}=this.sdk.sdkOptions,{versioned_id:i,global_config_id:o}=we(n);return t=t||new ve,e.forEach(l=>{let{pool_id:d,position_id:f,reward_coins:c,coin_type_a:p,coin_type_b:_}=l,{reward_coin_objs:w}=this.collectRewardPayloadWithoutTransfer({pool_id:d,position_id:f,reward_coins:c,coin_type_a:p,coin_type_b:_},t);w.length>0&&t.transferObjects(w,this.sdk.getSenderAddress())}),t}collectRewardPayloadWithoutTransfer(e,t){let{dlmm_pool:n}=this.sdk.sdkOptions,{versioned_id:i,global_config_id:o}=we(n),l=[],{pool_id:d,position_id:f,reward_coins:c,coin_type_a:p,coin_type_b:_}=e;return c.forEach(w=>{let P=t.moveCall({target:`${n.published_at}::pool::collect_position_reward`,arguments:[t.object(d),t.object(f),t.object(o),t.object(i)],typeArguments:[p,_,w]}),A=ne.fromBalance(P,w,t);l.push(A)}),{reward_coin_objs:l}}collectRewardAndFeePayload(e,t){return t=t||new ve,e.forEach(n=>{let{pool_id:i,position_id:o,reward_coins:l,coin_type_a:d,coin_type_b:f}=n;this.updatePositionFeeAndRewards({pool_id:i,position_id:o,coin_type_a:d,coin_type_b:f},t),this.collectFeePayload({pool_id:i,position_id:o,coin_type_a:d,coin_type_b:f},t),this.collectRewardPayload([{pool_id:i,position_id:o,reward_coins:l,coin_type_a:d,coin_type_b:f}],t)}),t}validateActiveIdSlippage(e,t){let{pool_id:n,active_id:i,max_price_slippage:o,bin_step:l,coin_type_a:d,coin_type_b:f}=e,{dlmm_router:c}=this.sdk.sdkOptions,p=G.getBinShift(i,l,o),_=Number(de(BigInt(i)));return t.moveCall({target:`${c.published_at}::utils::validate_active_id_slippage`,arguments:[typeof n=="string"?t.object(n):n,t.pure.u32(Number(_)),t.pure.u32(Number(p))],typeArguments:[d,f]}),t}closePositionPayload(e,t){t=t||new ve;let{coin_a_obj:n,coin_b_obj:i}=this.closePositionNoTransferPayload(e,t);return t.transferObjects([n,i],this.sdk.getSenderAddress()),t}closePositionNoTransferPayload(e,t){let{pool_id:n,position_id:i,reward_coins:o,coin_type_a:l,coin_type_b:d}=e,{dlmm_pool:f}=this.sdk.sdkOptions,{versioned_id:c,global_config_id:p}=we(f);t=t||new ve,this.updatePositionFeeAndRewards({pool_id:n,position_id:i,coin_type_a:l,coin_type_b:d},t),this.collectRewardPayload([{pool_id:n,position_id:i,reward_coins:o,coin_type_a:l,coin_type_b:d}],t);let[_,w,P]=t.moveCall({target:`${f.published_at}::pool::close_position`,arguments:[t.object(n),t.object(i),t.object(p),t.object(c),t.object(be)],typeArguments:[l,d]}),A=ne.fromBalance(w,l,t),I=ne.fromBalance(P,d,t);return t.moveCall({target:`${f.published_at}::pool::destroy_close_position_cert`,arguments:[_,t.object(c)],typeArguments:[]}),{coin_a_obj:A,coin_b_obj:I}}async getActiveBinIfInRange(e,t,n,i,o,l=!1,d){if(i<=n&&i>=t)return await this._sdk.Pool.getBinInfo(e,i,o,l,d)}calculateRemoveLiquidityInfo(e){let{bins:t,active_id:n,coin_amount:i}=e,o="fix_amount_a"in e,l=t.filter(w=>w.bin_id<n),d=t.filter(w=>w.bin_id>n),f=ut(0),c=ut(0),p=[];if(o){p=[...t];let{fix_amount_a:w}=e,P=t.find(E=>E.bin_id===n),A=d.reduce((E,T)=>ut(E).plus(T.amount_a),ut(0)).add(P?.amount_a||"0"),I=l.reduce((E,T)=>ut(E).plus(T.amount_b),ut(0)).add(P?.amount_b||"0");f=w?A:I,c=ut(i).gte(f)?ut(1):ut(i).div(f)}else{let{is_only_a:w}=e;p=w?d:l,f=p.reduce((P,A)=>ut(P).plus(w?A.amount_a:A.amount_b),ut(0)),c=ut(i).gte(f)?ut(1):ut(i).div(f)}return ut(f).isZero()?z("InsufficientLiquidity",new Error("Insufficient liquidity"),{[Jt.METHOD_NAME]:"calculateRemoveLiquidityResult",[Jt.REQUEST_PARAMS]:e}):G.processBinsByRate([...p],c.toFixed())}async calculateAddLiquidityInfo(e,t){let n="fix_amount_a"in e,{active_id:i,bin_step:o,lower_bin_id:l,upper_bin_id:d,active_bin_of_pool:f,strategy_type:c,pool_id:p}=e,_;if(n){let{coin_amount:w,fix_amount_a:P}=e;_=qe.autoFillCoinByStrategyV2(i,o,w,P,l,d,c,f)}else _=qe.toAmountsBothSideByStrategy(i,o,l,d,e.amount_a,e.amount_b,c,f);if(f&&p){let w=_.bins.findIndex(P=>P.bin_id===i);if(w!==-1){let P=await this._sdk.Pool.getPool(p,!1,t);if(P){let{fees_a:A,fees_b:I}=Je.getCompositionFees(f,_.bins[w],P.variable_parameters),E=_.bins[w];E.amount_a=ut(E.amount_a).sub(A).toFixed(0),E.amount_b=ut(E.amount_b).sub(I).toFixed(0),_.bins[w]=E}}}return _}removeLiquidityPayload(e){let t=new ve,{coin_a_obj:n,coin_b_obj:i}=this.removeLiquidityNoTransferPayload(e,t);return t.transferObjects([n,i],this.sdk.getSenderAddress()),t}removeLiquidityNoTransferPayload(e,t){let{pool_id:n,position_id:i,bin_infos:o,reward_coins:l,slippage:d,coin_type_a:f,coin_type_b:c,active_id:p,collect_fee:_,bin_step:w,remove_percent:P}=e,{dlmm_pool:A}=this.sdk.sdkOptions,{bins:I}=o;(_||l.length>0)&&this.updatePositionFeeAndRewards({pool_id:n,position_id:i,coin_type_a:f,coin_type_b:c},t),_&&this.collectFeePayload({pool_id:n,position_id:i,coin_type_a:f,coin_type_b:c},t),this.collectRewardPayload([{pool_id:n,position_id:i,reward_coins:l,coin_type_a:f,coin_type_b:c}],t);let{versioned_id:E,global_config_id:T}=we(A);if(P){let L=de(BigInt(I[0].bin_id)),R=de(BigInt(I[I.length-1].bin_id)),C=Number(ut(P).mul(1e4).toFixed(0)),[k,x]=t.moveCall({target:`${A.published_at}::pool::remove_liquidity_by_percent`,arguments:[t.object(n),t.object(i),t.pure.u32(Number(L)),t.pure.u32(Number(R)),t.pure.u16(C),t.object(T),t.object(E),t.object(be)],typeArguments:[f,c]}),K=ne.fromBalance(k,f,t),rt=ne.fromBalance(x,c,t);return this.validateActiveIdSlippage({pool_id:n,active_id:p,max_price_slippage:d,bin_step:w,coin_type_a:f,coin_type_b:c},t),{coin_a_obj:K,coin_b_obj:rt}}else{let L=t.pure.vector("u32",I.map(rt=>Number(de(BigInt(rt.bin_id))))),R=t.pure.vector("u128",I.map(rt=>rt.liquidity)),[C,k]=t.moveCall({target:`${A.published_at}::pool::remove_liquidity`,arguments:[t.object(n),t.object(i),L,R,t.object(T),t.object(E),t.object(be)],typeArguments:[f,c]});this.validateActiveIdSlippage({pool_id:n,active_id:p,max_price_slippage:d,bin_step:w,coin_type_a:f,coin_type_b:c},t);let x=ne.fromBalance(C,f,t),K=ne.fromBalance(k,c,t);return{coin_a_obj:x,coin_b_obj:K}}}async addLiquidityWithPricePayload(e){let{pool_id:t,bin_infos:n,coin_type_a:i,coin_type_b:o,price_base_coin:l,price:d,lower_price:f,upper_price:c,bin_step:p,strategy_type:_,active_bin_of_pool:w,decimals_a:P,decimals_b:A,use_bin_infos:I,max_price_slippage:E}=e,T,L,R,C=n;if(l==="coin_a")T=G.getBinIdFromPrice(f,p,!1,P,A),L=G.getBinIdFromPrice(c,p,!0,P,A),R=G.getBinIdFromPrice(d,p,!0,P,A);else{T=G.getBinIdFromPrice(ut(1).div(c).toString(),p,!1,P,A),L=G.getBinIdFromPrice(ut(1).div(f).toString(),p,!0,P,A),R=G.getBinIdFromPrice(ut(1).div(d).toString(),p,!1,P,A);let K={amount_a:n.amount_b,amount_b:n.amount_a,active_id:R,bin_step:p,lower_bin_id:T,upper_bin_id:L,active_bin_of_pool:w,strategy_type:_};C=await this.sdk.Position.calculateAddLiquidityInfo(K)}let x={pool_id:t,active_id:R,bin_infos:C,coin_type_a:i,coin_type_b:o,lower_bin_id:T,upper_bin_id:L,strategy_type:_,use_bin_infos:I,max_price_slippage:E,bin_step:p};return this.addLiquidityPayload(x)}addLiquidityPayload(e,t){let{pool_id:n,bin_infos:i,coin_type_a:o,coin_type_b:l,active_id:d,strategy_type:f,max_price_slippage:c,bin_step:p,use_bin_infos:_=!1,coin_object_id_a:w,coin_object_id_b:P}=e;t=t||new ve;let A="lower_bin_id"in e,I=[];if(A){let E=G.splitBinLiquidityInfo(i,e.lower_bin_id,e.upper_bin_id);I.push(...E)}else{let E=e.position_id;I.push(i),(e.collect_fee||e.reward_coins.length>0)&&this.updatePositionFeeAndRewards({pool_id:n,position_id:E,coin_type_a:o,coin_type_b:l},t),e.collect_fee&&this.collectFeePayload({pool_id:n,position_id:E,coin_type_a:o,coin_type_b:l},t),e.reward_coins.length>0&&this.collectRewardPayload([{pool_id:n,position_id:E,reward_coins:e.reward_coins,coin_type_a:o,coin_type_b:l}],t)}return I.forEach((E,T)=>{console.log("\u{1F680} ~ PositionModule ~ addLiquidityPayload ~ liquidity_bin:",T,E);let{amount_a:L,amount_b:R,bins:C}=E,k=w||ne.buildCoinWithBalance(BigInt(L),o,t),x=P||ne.buildCoinWithBalance(BigInt(R),l,t);_?this.addLiquidityInternal({pool_id:n,coin_type_a:o,coin_type_b:l,active_id:d,liquidity_bin:E,tx:t,coin_a_obj_id:k,coin_b_obj_id:x,position_id:A?void 0:e.position_id,max_price_slippage:c,bin_step:p}):this.addLiquidityStrategyInternal({pool_id:n,coin_type_a:o,coin_type_b:l,active_id:d,liquidity_bin:E,tx:t,max_price_slippage:c,bin_step:p,coin_a_obj_id:k,coin_b_obj_id:x,strategy_type:f,position_id:A?void 0:e.position_id})}),t}addLiquidityStrategyInternal(e){let{max_price_slippage:t,bin_step:n,position_id:i,pool_id:o,coin_type_a:l,coin_type_b:d,active_id:f,liquidity_bin:c,tx:p,coin_a_obj_id:_,coin_b_obj_id:w,strategy_type:P}=e,{dlmm_pool:A,dlmm_router:I}=this.sdk.sdkOptions,{versioned_id:E,global_config_id:T}=we(A),{bins:L,amount_a:R,amount_b:C}=c,k=i,x=L[0].bin_id,K=L[L.length-1].bin_id,rt=de(BigInt(x)),at=Number(de(BigInt(f))),nt=G.getBinShift(f,n,t),U=$n(P);if(i===void 0){G.assertCreatableBinIds(n,[x,K],"openPosition");let ot=K-x+1;ot>1e3&&z("InvalidBinWidth",new Error("Width is too large"),{[Jt.METHOD_NAME]:"openPosition"}),console.log("\u{1F680} ~ addLiquidityStrategyInternal ~ amount_a:",{amount_a:R,amount_b:C,lower_bin_id_u32:rt,width:ot,active_id_u32:at,bin_shift:nt}),k=p.moveCall({target:`${I.published_at}::${U}::open_position`,arguments:[typeof o=="string"?p.object(o):o,_,w,p.pure.u64(R),p.pure.u64(C),p.pure.u32(Number(rt)),p.pure.u16(Number(ot)),p.pure.u32(Number(at)),p.pure.u32(Number(nt)),p.object(T),p.object(E),p.object(be)],typeArguments:[l,d]})}else{let ot=L.filter(u=>u.amount_a!=="0"||u.amount_b!=="0");if(ot.length===0)return z("InvalidParams",new Error("No bins to add liquidity"),{[Jt.METHOD_NAME]:"addLiquidityStrategyInternal",[Jt.REQUEST_PARAMS]:e});let y=de(BigInt(ot[0].bin_id)),s=de(BigInt(ot[ot.length-1].bin_id));p.moveCall({target:`${I.published_at}::${U}::add_liquidity`,arguments:[typeof o=="string"?p.object(o):o,p.object(i),_,w,p.pure.u64(R),p.pure.u64(C),p.pure.u32(Number(y)),p.pure.u32(Number(s)),p.pure.u32(Number(at)),p.pure.u32(Number(nt)),p.object(T),p.object(E),p.object(be)],typeArguments:[l,d]})}return k?p.transferObjects([k,_,w],this.sdk.getSenderAddress()):p.transferObjects([_,w],this.sdk.getSenderAddress()),p}addLiquidityInternal(e){let{position_id:t,pool_id:n,coin_type_a:i,coin_type_b:o,active_id:l,liquidity_bin:d,tx:f,coin_a_obj_id:c,coin_b_obj_id:p,max_price_slippage:_,bin_step:w}=e,{bins:P}=d,{dlmm_pool:A,dlmm_router:I}=this.sdk.sdkOptions,{versioned_id:E,global_config_id:T}=we(A),L=f.pure.vector("u64",P.map(K=>K.amount_a)),R=f.pure.vector("u64",P.map(K=>K.amount_b)),C=f.makeMoveVec({elements:P.map(K=>f.pure.u32(Number(de(BigInt(K.bin_id))))),type:"u32"}),k=d.bins[0].bin_id,x=d.bins[d.bins.length-1].bin_id;return t===void 0?(G.assertCreatableBinIds(w,[k,x],"openPosition"),x-k+1>1e3&&z("InvalidBinWidth",new Error("Width is too large"),{[Jt.METHOD_NAME]:"openPosition"}),f.moveCall({target:`${I.published_at}::add_liquidity::open_position`,arguments:[typeof n=="string"?f.object(n):n,c,p,C,L,R,f.object(T),f.object(E),f.object(be)],typeArguments:[i,o]}),l>=k&&l<=x&&this.validateActiveIdSlippage({pool_id:n,active_id:l,max_price_slippage:_,bin_step:w,coin_type_a:i,coin_type_b:o},f),f.transferObjects([c,p],this.sdk.getSenderAddress())):(f.moveCall({target:`${I.published_at}::add_liquidity::add_liquidity`,arguments:[typeof n=="string"?f.object(n):n,f.object(t),c,p,C,L,R,f.object(T),f.object(E),f.object(be)],typeArguments:[i,o]}),l>=k&&l<=x&&this.validateActiveIdSlippage({pool_id:n,active_id:l,max_price_slippage:_,bin_step:w,coin_type_a:i,coin_type_b:o},f),f.transferObjects([c,p],this.sdk.getSenderAddress())),f}async fetchPositionFeeAndReward(e,t){let n=new ve;this.collectRewardAndFeePayload(e,n);let i=await this.sdk.FullClient.sendSimulationTransaction(n,this.sdk.getSenderAddress(),!0,t);if(i.error!=null)return z("FetchError",new Error(i.error),{[Jt.METHOD_NAME]:"fetchPositionFeeAndReward",[Jt.REQUEST_PARAMS]:{options:e,totalOptions:e.length}});let o=Hn(i.Transaction.events),l=Qn(i.Transaction.events);return{feeData:o,rewardData:l}}};import{Transaction as or}from"@mysten/sui/transactions";import{CLOCK_ADDRESS as Qi,CoinAssist as ko,d as sr,DETAILS_KEYS as ni,getPackagerConfigs as ar}from"@cetusprotocol/common-sdk";import{normalizeSuiAddress as Do}from"@mysten/sui/utils";var ri=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async preSwapQuote(e,t){let{dlmm_pool:n}=this._sdk.sdkOptions,{pool_id:i,coin_type_a:o,coin_type_b:l,a2b:d,by_amount_in:f,in_amount:c}=e,{versioned_id:p,global_config_id:_}=ar(n),w=new or;w.moveCall({target:`${n.published_at}::pool::flash_swap`,arguments:[w.object(i),w.pure.bool(d),w.pure.bool(f),w.pure.u64(c),w.object(_),w.object(p),w.object(Qi)],typeArguments:[o,l]});let P=await this.sdk.FullClient.sendSimulationTransaction(w,Do("0x0"),!1,t);if(P.FailedTransaction!=null)return z("FetchError",new Error(P.FailedTransaction.status.error?.message??"unknown error"),{[ni.METHOD_NAME]:"fetchSwapQuote",[ni.REQUEST_PARAMS]:e});let A=Kn(P.Transaction,d);return A==null?z("FetchError",new Error("No quote info"),{[ni.METHOD_NAME]:"preSwapQuote",[ni.REQUEST_PARAMS]:e}):(A.a2b=d,A)}swapPayload(e){let{dlmm_pool:t,dlmm_router:n}=this._sdk.sdkOptions,{quote_obj:i,coin_type_a:o,coin_type_b:l,by_amount_in:d,slippage:f,partner:c}=e,{pool_id:p,in_amount:_,out_amount:w,a2b:P}=i,A=new or,I=d?_:sr(_).mul(1+f).toFixed(0),E=d?sr(w).mul(1-f).toFixed(0):w,T=ko.buildCoinWithBalance(BigInt(I),P?o:l,A),{versioned_id:L,global_config_id:R}=ar(t);return console.log("\u{1F680} ~ SwapModule ~ option:",{...e,in_amount_limit:I,out_amount_limit:E}),c?A.moveCall({target:`${n.published_at}::swap::${P?"swap_a2b_with_partner":"swap_b2a_with_partner"}`,arguments:[A.object(p),A.object(c),T,A.pure.bool(d),A.pure.u64(BigInt(d?_:w)),A.pure.u64(BigInt(d?E:I)),A.object(R),A.object(L),A.object(Qi)],typeArguments:[o,l]}):A.moveCall({target:`${n.published_at}::swap::${P?"swap_a2b":"swap_b2a"}`,arguments:[A.object(p),T,A.pure.bool(d),A.pure.u64(BigInt(d?_:w)),A.pure.u64(BigInt(d?E:I)),A.object(R),A.object(L),A.object(Qi)],typeArguments:[o,l]}),A.transferObjects([T],this.sdk.getSenderAddress()),A}};import{Transaction as oi}from"@mysten/sui/transactions";import{CLOCK_ADDRESS as No,d as lr,DETAILS_KEYS as Ht,fixCoinType as Fo,getPackagerConfigs as Le}from"@cetusprotocol/common-sdk";var si=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getPartnerList(e){let t=[],{dlmm_pool:n}=this._sdk.sdkOptions,{partners_id:i}=Le(n);try{let d=(await this._sdk.FullClient.getObject({objectId:i,include:{json:!0},signal:e?.signal})).object.json.partners.contents.map(f=>f.value);d.length>0&&(await this._sdk.FullClient.batchGetObjects(d,{json:!0},1e3,e)).forEach(c=>{let p=Wi(c);t.push(p)})}catch(o){return z("FetchError",o,{[Ht.METHOD_NAME]:"getPartnerList",[Ht.REQUEST_PARAMS]:i})}return t}async getPartnerCapId(e,t,n){let{dlmm_pool:i}=this._sdk.sdkOptions;try{let o=`partner_cap_id_${e}_${t}`,l=this._sdk.getCache(o);if(l)return l;let d=await this._sdk.FullClient.listOwnedObjects({owner:e,include:{json:!0},type:`${i.package_id}::partner::PartnerCap`,signal:n?.signal}),f;return d.objects.forEach(c=>{let p=c.json;p.partner_id===t&&(f=p.id,this._sdk.updateCache(o,f))}),f||z("NotFound",new Error("Partner cap not found"),{[Ht.METHOD_NAME]:"getPartnerCapId",[Ht.REQUEST_PARAMS]:{owner:e,partner_id:t}})}catch(o){return z("FetchError",o,{[Ht.METHOD_NAME]:"getPartnerCapId",[Ht.REQUEST_PARAMS]:{owner:e,partner_id:t}})}}async getPartnerBalance(e,t){try{let n=await this._sdk.FullClient.getDynamicFieldsByPage(e,"all",t),i=[],o=n.data.map(l=>l.fieldId);return o.length>0&&(await this._sdk.FullClient.batchGetObjects(o,{json:!0},1e3,t)).forEach(d=>{i.push({coin_type:Fo(d.json.name,!1),balance:d.json.value})}),i}catch(n){return z("FetchError",n,{[Ht.METHOD_NAME]:"getPartnerBalance",[Ht.REQUEST_PARAMS]:e})}}async getPartner(e,t){try{let n=await this._sdk.FullClient.getObject({objectId:e,include:{json:!0},signal:t?.signal});return Wi(n.object)}catch(n){return z("FetchError",n,{[Ht.METHOD_NAME]:"getPartner",[Ht.REQUEST_PARAMS]:e})}}updateRefFeeRatePayload(e,t){let{dlmm_pool:n}=this._sdk.sdkOptions,{partner_id:i,ref_fee_rate:o}=e,{global_config_id:l,versioned_id:d}=Le(n);t=t||new oi;let f=lr(o).mul(1e4);return f.gt(1e4)?z("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[Ht.METHOD_NAME]:"updateRefFeeRatePayload",[Ht.REQUEST_PARAMS]:e}):(t.moveCall({target:`${n.published_at}::partner::update_ref_fee_rate`,arguments:[t.object(i),t.pure.u64(f.toNumber()),t.object(l),t.object(d)],typeArguments:[]}),t)}async claimRefFeePayload(e){let{partner_id:t,partner_cap_id:n,fee_coin_types:i}=e,{dlmm_pool:o}=this._sdk.sdkOptions,{versioned_id:l}=Le(o),d=new oi,f=n;return f||(f=await this.getPartnerCapId(this._sdk.getSenderAddress(),t)),i.forEach(c=>{d.moveCall({target:`${o.published_at}::partner::claim_ref_fee`,arguments:[d.object(t),d.object(f),d.object(l)],typeArguments:[c]})}),d}updateTimeRangePayload(e,t){let{dlmm_pool:n}=this._sdk.sdkOptions,{partner_id:i,start_time:o,end_time:l}=e,{global_config_id:d,versioned_id:f}=Le(n);t=t||new oi;let c=o>1e12?Math.floor(o/1e3):o,p=l>1e12?Math.floor(l/1e3):l;return p<=c?z("InvalidParams",new Error("end_time must be greater than start_time"),{[Ht.METHOD_NAME]:"createPartnerPayload",[Ht.REQUEST_PARAMS]:e}):(t.moveCall({target:`${n.published_at}::partner::update_time_range`,arguments:[t.object(i),t.pure.u64(c),t.pure.u64(p),t.object(d),t.object(f),t.object(No)],typeArguments:[]}),t)}createPartnerPayload(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{name:n,ref_fee_rate:i,start_time:o,end_time:l,recipient:d}=e,{partners_id:f,global_config_id:c,versioned_id:p}=Le(t),_=new oi,w=lr(i).mul(1e4);if(w.gt(1e4))return z("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[Ht.METHOD_NAME]:"createPartnerPayload",[Ht.REQUEST_PARAMS]:e});let P=o>1e12?Math.floor(o/1e3):o,A=l>1e12?Math.floor(l/1e3):l;return A<=P?z("InvalidParams",new Error("end_time must be greater than start_time"),{[Ht.METHOD_NAME]:"createPartnerPayload",[Ht.REQUEST_PARAMS]:e}):(_.moveCall({target:`${t.published_at}::partner::create_partner`,arguments:[_.object(f),_.pure.string(n),_.pure.u64(w.toNumber()),_.pure.u64(P),_.pure.u64(A),_.pure.address(d),_.object(c),_.object(p)],typeArguments:[]}),_)}};var $i=Ri(je(),1);import{Transaction as Ki}from"@mysten/sui/transactions";import{CLOCK_ADDRESS as ur,CoinAssist as qo,d as Te,getPackagerConfigs as Vi,MathUtil as dr}from"@cetusprotocol/common-sdk";var ai=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getRewardPeriodEmission(e,t,n,i){let o=await this._sdk.FullClient.getDynamicFieldsByPage(e,"all",i),l=[],d=o.data.map(_=>_.fieldId);d.length>0&&(await this._sdk.FullClient.batchGetObjects(d,{json:!0},1e3,i)).forEach(w=>{let P=w.json,A=dr.u128ToI128(new $i.default(P.value.value.bits)).toString(),I=P.name,E=new Date(Number(I)*1e3).toLocaleString(),L={emissions_per_second:"0",emissions_per_day:"0",emissions_per:dr.fromX64(new $i.default(A)).toString(),time:I,visualized_time:E};l.push(L)});let f=l.sort((_,w)=>Number(_.time)-Number(w.time)),c=[];c.push({emissions_per_second:t,emissions_per_day:Te(t).mul(3600*24).toString(),emissions_per:"0",time:n.toString(),visualized_time:new Date(n*1e3).toLocaleString()});let p=t;for(let _=0;_<f.length;_++){let w=f[_];if(Te(w.time).lte(n))continue;p=Te(p).add(Te(w.emissions_per)).toString();let P=Te(p).mul(3600*24).toString();Te(p).lt(0)?(w.emissions_per_second="0",w.emissions_per_day="0"):(w.emissions_per_second=p,w.emissions_per_day=P),c.push(w)}return c}addRewardPayload(e,t){t=t||new Ki;let{dlmm_pool:n}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:o}=Vi(n),{pool_id:l,reward_coin_type:d,reward_amount:f,start_time_seconds:c,end_time_seconds:p,coin_type_a:_,coin_type_b:w}=e,P=qo.buildCoinWithBalance(BigInt(f),d,t);return t.pure.option("u64",c),t.moveCall({target:`${n.published_at}::pool::add_reward`,arguments:[t.object(l),P,t.pure.option("u64",c),t.pure.u64(p),t.object(i),t.object(o),t.object(ur)],typeArguments:[_,w,d]}),t}initRewardPayload(e,t){t=t||new Ki;let{dlmm_pool:n}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:o}=Vi(n),{pool_id:l,reward_coin_types:d,coin_type_a:f,coin_type_b:c}=e;return d.forEach(p=>{t.moveCall({target:`${n.published_at}::pool::initialize_reward`,arguments:[t.object(l),t.object(i),t.object(o),t.object(ur)],typeArguments:[f,c,p]})}),t}buildRewardAccessPayload(e,t){t=t||new Ki;let{dlmm_pool:n}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:o}=Vi(n),{pool_id:l,type:d,coin_type_a:f,coin_type_b:c}=e;return t.moveCall({target:`${n.published_at}::pool::${d==="to_public"?"make_reward_public":"make_reward_private"}`,arguments:[t.object(l),t.object(i),t.object(o)],typeArguments:[f,c]}),t}};import{Transaction as Lo}from"@mysten/sui/transactions";import{DETAILS_KEYS as li,fixCoinType as jo,getPackagerConfigs as cr}from"@cetusprotocol/common-sdk";var ui=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}buildRewardWhiteListPayload(e,t){t=t||new Lo;let{dlmm_pool:n}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:o}=cr(n),{reward_coin_types:l,type:d}=e;return l.forEach(f=>{t.moveCall({target:`${n.published_at}::config::${d==="add"?"add_reward_whitelist":"remove_reward_whitelist"}`,arguments:[t.object(i),t.object(o)],typeArguments:[f]})}),t}async getBinStepConfigList(e,t){let n=await this._sdk.FullClient.getDynamicFieldsByPage(e,"all",t),i=[],o=n.data.map(l=>l.fieldId);return o.length>0&&(await this._sdk.FullClient.batchGetObjects(o,{json:!0},1e3,t)).forEach(d=>{let c={...d.json.value};i.push(c)}),i}async getDlmmGlobalConfig(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{global_config_id:n}=cr(t);try{let o=(await this._sdk.FullClient.getObject({objectId:n,include:{json:!0},signal:e?.signal})).object.json,l=o.reward_config,d=l.reward_white_list?.fields?.contents?.map(c=>jo(c.fields.key.fields.name,!1))||[];return{id:o.id.id,acl:{id:o.acl.permissions.id,size:o.acl.permissions.size},allowed_list:{id:o.allowed_list.id,size:o.allowed_list.size},denied_list:{id:o.denied_list.id,size:o.denied_list.size},bin_steps:{id:o.bin_steps.id,size:o.bin_steps.size},reward_white_list:d,blocked_position:{id:o.restriction.blocked_position.permissions.id,size:o.restriction.blocked_position.permissions.size},blocked_user:{id:o.restriction.blocked_user.permissions.id,size:o.restriction.blocked_user.permissions.size},min_reward_duration:Number(l.min_reward_duration),non_manager_initialize_reward_cap:Number(l.manager_reserved_reward_init_slots),reward_public:l.reward_public}}catch(i){return console.log("fetchGlobalConfig error: ",i),z("FetchError",i,{[li.METHOD_NAME]:"getBinStepConfigs",[li.REQUEST_PARAMS]:n})}}async fetchDlmmSdkConfigs(e){let{dlmm_pool:t}=this._sdk.sdkOptions,n={registry_id:"",pools_id:"",global_config_id:"",versioned_id:"",admin_cap_id:"",partners_id:""},o=(await this._sdk.FullClient.getObject({objectId:t.package_id,include:{json:!0,previousTransaction:!0},signal:e?.signal})).object?.previousTransaction;if(!o)return z("FetchError","Missing previous transaction digest for dlmm package object",{[li.METHOD_NAME]:"fetchDlmmSdkConfigs",[li.REQUEST_PARAMS]:t.package_id});(await this._sdk.FullClient.queryEventsByGraphQL({Transaction:o},"all",e)).data?.forEach(c=>{let p=c.type,_=c.parsedJson;p.includes("versioned::InitEvent")&&(n.versioned_id=_.versioned),p.includes("partner::InitPartnerEvent")&&(n.partners_id=_.partners_id),p.includes("config::InitEvent")&&(n.global_config_id=_.config_id),p.includes("admin_cap::InitEvent")&&(n.admin_cap_id=_.admin_cap_id),p.includes("registry::RegistryEvent")&&(n.registry_id=_.pools_id)});let f=(await this._sdk.FullClient.getObject({objectId:n.registry_id,include:{json:!0},signal:e?.signal})).object.json;return n.pools_id=f.pools.id,n}};var di=class r extends Wo{constructor(e){super(e),this._pool=new ei(this),this._position=new ii(this),this._swap=new ri(this),this._partner=new si(this),this._reward=new ai(this),this._config=new ui(this)}get Pool(){return this._pool}get Position(){return this._position}get Swap(){return this._swap}get Partner(){return this._partner}get Reward(){return this._reward}get Config(){return this._config}static createSDK(e){let{env:t="mainnet"}=e;return t==="mainnet"?r.createCustomSDK({...mn,...e}):r.createCustomSDK({...rr,...e})}static createCustomSDK(e){return new r(e)}};var Nl=di;export{$t as BASIS_POINT,Me as BASIS_POINT_MAX,zt as BIN_BOUND,Po as Bin,Bo as BinGroup,Ao as BinGroupRefRaw,G as BinUtils,di as CetusDlmmSDK,De as DEFAULT_MAX_WEIGHT,Ie as DEFAULT_MIN_WEIGHT,Tn as FEE_PRECISION,Je as FeeUtils,Jn as IlmUtils,zo as MAX_BIN_ID,se as MAX_BIN_PER_GROUP,he as MAX_BIN_PER_POSITION,Fi as MAX_FEE_RATE,$o as MIN_BIN_ID,Rn as MIN_PRICE_Q64_V2,Xn as NodeIDPoolSimpleInfo,qi as ONE,ei as PoolModule,Mo as PoolSimpleInfoRaw,Xo as REWARD_PERIOD,Yo as REWARD_PERIOD_START_AT,q as SCALE_OFFSET,Yn as SkipListNodeBinGroupRefRaw,$e as StrategyType,qe as StrategyUtils,ee as WeightUtils,zn as buildPoolKey,Nl as default,mn as dlmmMainnet,rr as dlmmTestnet,ks as generateRewardSchedule,$n as getRouterModule,Zi as parseBinInfo,Gn as parseBinInfoList,Ns as parseCurrentRewardPeriodEmission,Zn as parseDlmmBasePool,Ye as parseDlmmPool,Ui as parseDlmmPosition,xs as parseLiquidityShares,Wi as parsePartner,Vn as parsePoolTransactionInfo,Ds as parseRewardPeriodEmission,Cs as parseStrategyType,Hn as parsedDlmmPosFeeData,Qn as parsedDlmmPosRewardData,Kn as parsedSwapQuoteData,Gi as poolFilterEvenTypes,Fe as safeAmount,le as safeMulAmount};
2
2
  /*! Bundled license information:
3
3
 
4
4
  decimal.js/decimal.mjs: