@cetusprotocol/dlmm-sdk 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +32 -32
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/config/mainnet.ts +1 -1
- package/src/utils/weightUtils.ts +7 -4
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var kr=Object.create;var Re=Object.defineProperty;var xr=Object.getOwnPropertyDescriptor;var Rr=Object.getOwnPropertyNames;var Dr=Object.getPrototypeOf,Fr=Object.prototype.hasOwnProperty;var Nr=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports),Lr=(n,e)=>{for(var t in e)Re(n,t,{get:e[t],enumerable:!0})},nr=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of Rr(e))!Fr.call(n,i)&&i!==t&&Re(n,i,{get:()=>e[i],enumerable:!(r=xr(e,i))||r.enumerable});return n};var wi=(n,e,t)=>(t=n!=null?kr(Dr(n)):{},nr(e||!n||!n.__esModule?Re(t,"default",{value:n,enumerable:!0}):t,n)),qr=n=>nr(Re({},"__esModule",{value:!0}),n);var De=Nr((ar,yi)=>{"use strict";(function(n,e){"use strict";function t(v,o){if(!v)throw new Error(o||"Assertion failed")}function r(v,o){v.super_=o;var l=function(){};l.prototype=o.prototype,v.prototype=new l,v.prototype.constructor=v}function i(v,o,l){if(i.isBN(v))return v;this.negative=0,this.words=null,this.length=0,this.red=null,v!==null&&((o==="le"||o==="be")&&(l=o,o=10),this._init(v||0,o||10,l||"be"))}typeof n=="object"?n.exports=i:e.BN=i,i.BN=i,i.wordSize=26;var s;try{typeof window<"u"&&typeof window.Buffer<"u"?s=window.Buffer:s=require("buffer").Buffer}catch{}i.isBN=function(o){return o instanceof i?!0:o!==null&&typeof o=="object"&&o.constructor.wordSize===i.wordSize&&Array.isArray(o.words)},i.max=function(o,l){return o.cmp(l)>0?o:l},i.min=function(o,l){return o.cmp(l)<0?o:l},i.prototype._init=function(o,l,h){if(typeof o=="number")return this._initNumber(o,l,h);if(typeof o=="object")return this._initArray(o,l,h);l==="hex"&&(l=16),t(l===(l|0)&&l>=2&&l<=36),o=o.toString().replace(/\s+/g,"");var m=0;o[0]==="-"&&(m++,this.negative=1),m<o.length&&(l===16?this._parseHex(o,m,h):(this._parseBase(o,l,m),h==="le"&&this._initArray(this.toArray(),l,h)))},i.prototype._initNumber=function(o,l,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(),l,h)},i.prototype._initArray=function(o,l,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,A=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<<A&67108863,this.words[b+1]=M>>>26-A&67108863,A+=24,A>=26&&(A-=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<<A&67108863,this.words[b+1]=M>>>26-A&67108863,A+=24,A>=26&&(A-=26,b++);return this._strip()};function u(v,o){var l=v.charCodeAt(o);if(l>=48&&l<=57)return l-48;if(l>=65&&l<=70)return l-55;if(l>=97&&l<=102)return l-87;t(!1,"Invalid character in "+v)}function d(v,o,l){var h=u(v,l);return l-1>=o&&(h|=u(v,l-1)<<4),h}i.prototype._parseHex=function(o,l,h){this.length=Math.ceil((o.length-l)/6),this.words=new Array(this.length);for(var m=0;m<this.length;m++)this.words[m]=0;var b=0,M=0,A;if(h==="be")for(m=o.length-1;m>=l;m-=2)A=d(o,l,m)<<b,this.words[M]|=A&67108863,b>=18?(b-=18,M+=1,this.words[M]|=A>>>26):b+=8;else{var g=o.length-l;for(m=g%2===0?l+1:l;m<o.length;m+=2)A=d(o,l,m)<<b,this.words[M]|=A&67108863,b>=18?(b-=18,M+=1,this.words[M]|=A>>>26):b+=8}this._strip()};function f(v,o,l,h){for(var m=0,b=0,M=Math.min(v.length,l),A=o;A<M;A++){var g=v.charCodeAt(A)-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}i.prototype._parseBase=function(o,l,h){this.words=[0],this.length=1;for(var m=0,b=1;b<=67108863;b*=l)m++;m--,b=b/l|0;for(var M=o.length-h,A=M%m,g=Math.min(M,M-A)+h,a=0,w=h;w<g;w+=m)a=f(o,w,w+m,l),this.imuln(b),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a);if(A!==0){var N=1;for(a=f(o,w,o.length,l),w=0;w<A;w++)N*=l;this.imuln(N),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a)}this._strip()},i.prototype.copy=function(o){o.words=new Array(this.length);for(var l=0;l<this.length;l++)o.words[l]=this.words[l];o.length=this.length,o.negative=this.negative,o.red=this.red};function c(v,o){v.words=o.words,v.length=o.length,v.negative=o.negative,v.red=o.red}if(i.prototype._move=function(o){c(o,this)},i.prototype.clone=function(){var o=new i(null);return this.copy(o),o},i.prototype._expand=function(o){for(;this.length<o;)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"],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],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(o,l){o=o||10,l=l|0||1;var h;if(o===16||o==="hex"){h="";for(var m=0,b=0,M=0;M<this.length;M++){var A=this.words[M],g=((A<<m|b)&16777215).toString(16);b=A>>>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%l!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}if(o===(o|0)&&o>=2&&o<=36){var a=y[o],w=P[o];h="";var N=this.clone();for(N.negative=0;!N.isZero();){var q=N.modrn(w).toString(o);N=N.idivn(w),N.isZero()?h=q+h:h=_[a-q.length]+q+h}for(this.isZero()&&(h="0"+h);h.length%l!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}t(!1,"Base should be between 2 and 36")},i.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},i.prototype.toJSON=function(){return this.toString(16,2)},s&&(i.prototype.toBuffer=function(o,l){return this.toArrayLike(s,o,l)}),i.prototype.toArray=function(o,l){return this.toArrayLike(Array,o,l)};var S=function(o,l){return o.allocUnsafe?o.allocUnsafe(l):new o(l)};i.prototype.toArrayLike=function(o,l,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=S(o,b),A=l==="le"?"LE":"BE";return this["_toArrayLike"+A](M,m),M},i.prototype._toArrayLikeLE=function(o,l){for(var h=0,m=0,b=0,M=0;b<this.length;b++){var A=this.words[b]<<M|m;o[h++]=A&255,h<o.length&&(o[h++]=A>>8&255),h<o.length&&(o[h++]=A>>16&255),M===6?(h<o.length&&(o[h++]=A>>24&255),m=0,M=0):(m=A>>>24,M+=2)}if(h<o.length)for(o[h++]=m;h<o.length;)o[h++]=0},i.prototype._toArrayLikeBE=function(o,l){for(var h=o.length-1,m=0,b=0,M=0;b<this.length;b++){var A=this.words[b]<<M|m;o[h--]=A&255,h>=0&&(o[h--]=A>>8&255),h>=0&&(o[h--]=A>>16&255),M===6?(h>=0&&(o[h--]=A>>24&255),m=0,M=0):(m=A>>>24,M+=2)}if(h>=0)for(o[h--]=m;h>=0;)o[h--]=0},Math.clz32?i.prototype._countBits=function(o){return 32-Math.clz32(o)}:i.prototype._countBits=function(o){var l=o,h=0;return l>=4096&&(h+=13,l>>>=13),l>=64&&(h+=7,l>>>=7),l>=8&&(h+=4,l>>>=4),l>=2&&(h+=2,l>>>=2),h+l},i.prototype._zeroBits=function(o){if(o===0)return 26;var l=o,h=0;return(l&8191)===0&&(h+=13,l>>>=13),(l&127)===0&&(h+=7,l>>>=7),(l&15)===0&&(h+=4,l>>>=4),(l&3)===0&&(h+=2,l>>>=2),(l&1)===0&&h++,h},i.prototype.bitLength=function(){var o=this.words[this.length-1],l=this._countBits(o);return(this.length-1)*26+l};function B(v){for(var o=new Array(v.bitLength()),l=0;l<o.length;l++){var h=l/26|0,m=l%26;o[l]=v.words[h]>>>m&1}return o}i.prototype.zeroBits=function(){if(this.isZero())return 0;for(var o=0,l=0;l<this.length;l++){var h=this._zeroBits(this.words[l]);if(o+=h,h!==26)break}return o},i.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},i.prototype.toTwos=function(o){return this.negative!==0?this.abs().inotn(o).iaddn(1):this.clone()},i.prototype.fromTwos=function(o){return this.testn(o-1)?this.notn(o).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(o){for(;this.length<o.length;)this.words[this.length++]=0;for(var l=0;l<o.length;l++)this.words[l]=this.words[l]|o.words[l];return this._strip()},i.prototype.ior=function(o){return t((this.negative|o.negative)===0),this.iuor(o)},i.prototype.or=function(o){return this.length>o.length?this.clone().ior(o):o.clone().ior(this)},i.prototype.uor=function(o){return this.length>o.length?this.clone().iuor(o):o.clone().iuor(this)},i.prototype.iuand=function(o){var l;this.length>o.length?l=o:l=this;for(var h=0;h<l.length;h++)this.words[h]=this.words[h]&o.words[h];return this.length=l.length,this._strip()},i.prototype.iand=function(o){return t((this.negative|o.negative)===0),this.iuand(o)},i.prototype.and=function(o){return this.length>o.length?this.clone().iand(o):o.clone().iand(this)},i.prototype.uand=function(o){return this.length>o.length?this.clone().iuand(o):o.clone().iuand(this)},i.prototype.iuxor=function(o){var l,h;this.length>o.length?(l=this,h=o):(l=o,h=this);for(var m=0;m<h.length;m++)this.words[m]=l.words[m]^h.words[m];if(this!==l)for(;m<l.length;m++)this.words[m]=l.words[m];return this.length=l.length,this._strip()},i.prototype.ixor=function(o){return t((this.negative|o.negative)===0),this.iuxor(o)},i.prototype.xor=function(o){return this.length>o.length?this.clone().ixor(o):o.clone().ixor(this)},i.prototype.uxor=function(o){return this.length>o.length?this.clone().iuxor(o):o.clone().iuxor(this)},i.prototype.inotn=function(o){t(typeof o=="number"&&o>=0);var l=Math.ceil(o/26)|0,h=o%26;this._expand(l),h>0&&l--;for(var m=0;m<l;m++)this.words[m]=~this.words[m]&67108863;return h>0&&(this.words[m]=~this.words[m]&67108863>>26-h),this._strip()},i.prototype.notn=function(o){return this.clone().inotn(o)},i.prototype.setn=function(o,l){t(typeof o=="number"&&o>=0);var h=o/26|0,m=o%26;return this._expand(h+1),l?this.words[h]=this.words[h]|1<<m:this.words[h]=this.words[h]&~(1<<m),this._strip()},i.prototype.iadd=function(o){var l;if(this.negative!==0&&o.negative===0)return this.negative=0,l=this.isub(o),this.negative^=1,this._normSign();if(this.negative===0&&o.negative!==0)return o.negative=0,l=this.isub(o),o.negative=1,l._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++)l=(h.words[M]|0)+(m.words[M]|0)+b,this.words[M]=l&67108863,b=l>>>26;for(;b!==0&&M<h.length;M++)l=(h.words[M]|0)+b,this.words[M]=l&67108863,b=l>>>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},i.prototype.add=function(o){var l;return o.negative!==0&&this.negative===0?(o.negative=0,l=this.sub(o),o.negative^=1,l):o.negative===0&&this.negative!==0?(this.negative=0,l=o.sub(this),this.negative=1,l):this.length>o.length?this.clone().iadd(o):o.clone().iadd(this)},i.prototype.isub=function(o){if(o.negative!==0){o.negative=0;var l=this.iadd(o);return o.negative=1,l._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,A=0;A<b.length;A++)l=(m.words[A]|0)-(b.words[A]|0)+M,M=l>>26,this.words[A]=l&67108863;for(;M!==0&&A<m.length;A++)l=(m.words[A]|0)+M,M=l>>26,this.words[A]=l&67108863;if(M===0&&A<m.length&&m!==this)for(;A<m.length;A++)this.words[A]=m.words[A];return this.length=Math.max(this.length,A),m!==this&&(this.negative=1),this._strip()},i.prototype.sub=function(o){return this.clone().isub(o)};function k(v,o,l){l.negative=o.negative^v.negative;var h=v.length+o.length|0;l.length=h,h=h-1|0;var m=v.words[0]|0,b=o.words[0]|0,M=m*b,A=M&67108863,g=M/67108864|0;l.words[0]=A;for(var a=1;a<h;a++){for(var w=g>>>26,N=g&67108863,q=Math.min(a,o.length-1),W=Math.max(0,a-v.length+1);W<=q;W++){var X=a-W|0;m=v.words[X]|0,b=o.words[W]|0,M=m*b+N,w+=M/67108864|0,N=M&67108863}l.words[a]=N|0,g=w|0}return g!==0?l.words[a]=g|0:l.length--,l._strip()}var x=function(o,l,h){var m=o.words,b=l.words,M=h.words,A=0,g,a,w,N=m[0]|0,q=N&8191,W=N>>>13,X=m[1]|0,it=X&8191,nt=X>>>13,le=m[2]|0,ot=le&8191,at=le>>>13,Wi=m[3]|0,dt=Wi&8191,ft=Wi>>>13,Zi=m[4]|0,ct=Zi&8191,ht=Zi>>>13,Ui=m[5]|0,pt=Ui&8191,mt=Ui>>>13,Hi=m[6]|0,_t=Hi&8191,gt=Hi>>>13,Qi=m[7]|0,bt=Qi&8191,wt=Qi>>>13,Ki=m[8]|0,vt=Ki&8191,yt=Ki>>>13,$i=m[9]|0,Mt=$i&8191,Pt=$i>>>13,zi=b[0]|0,At=zi&8191,St=zi>>>13,Gi=b[1]|0,Bt=Gi&8191,Et=Gi>>>13,Vi=b[2]|0,Ot=Vi&8191,It=Vi>>>13,Xi=b[3]|0,Ct=Xi&8191,Tt=Xi>>>13,Ji=b[4]|0,kt=Ji&8191,xt=Ji>>>13,Yi=b[5]|0,Rt=Yi&8191,Dt=Yi>>>13,tr=b[6]|0,Ft=tr&8191,Nt=tr>>>13,er=b[7]|0,Lt=er&8191,qt=er>>>13,ir=b[8]|0,jt=ir&8191,Wt=ir>>>13,rr=b[9]|0,Zt=rr&8191,Ut=rr>>>13;h.negative=o.negative^l.negative,h.length=19,g=Math.imul(q,At),a=Math.imul(q,St),a=a+Math.imul(W,At)|0,w=Math.imul(W,St);var ti=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ti>>>26)|0,ti&=67108863,g=Math.imul(it,At),a=Math.imul(it,St),a=a+Math.imul(nt,At)|0,w=Math.imul(nt,St),g=g+Math.imul(q,Bt)|0,a=a+Math.imul(q,Et)|0,a=a+Math.imul(W,Bt)|0,w=w+Math.imul(W,Et)|0;var ei=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ei>>>26)|0,ei&=67108863,g=Math.imul(ot,At),a=Math.imul(ot,St),a=a+Math.imul(at,At)|0,w=Math.imul(at,St),g=g+Math.imul(it,Bt)|0,a=a+Math.imul(it,Et)|0,a=a+Math.imul(nt,Bt)|0,w=w+Math.imul(nt,Et)|0,g=g+Math.imul(q,Ot)|0,a=a+Math.imul(q,It)|0,a=a+Math.imul(W,Ot)|0,w=w+Math.imul(W,It)|0;var ii=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ii>>>26)|0,ii&=67108863,g=Math.imul(dt,At),a=Math.imul(dt,St),a=a+Math.imul(ft,At)|0,w=Math.imul(ft,St),g=g+Math.imul(ot,Bt)|0,a=a+Math.imul(ot,Et)|0,a=a+Math.imul(at,Bt)|0,w=w+Math.imul(at,Et)|0,g=g+Math.imul(it,Ot)|0,a=a+Math.imul(it,It)|0,a=a+Math.imul(nt,Ot)|0,w=w+Math.imul(nt,It)|0,g=g+Math.imul(q,Ct)|0,a=a+Math.imul(q,Tt)|0,a=a+Math.imul(W,Ct)|0,w=w+Math.imul(W,Tt)|0;var ri=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ri>>>26)|0,ri&=67108863,g=Math.imul(ct,At),a=Math.imul(ct,St),a=a+Math.imul(ht,At)|0,w=Math.imul(ht,St),g=g+Math.imul(dt,Bt)|0,a=a+Math.imul(dt,Et)|0,a=a+Math.imul(ft,Bt)|0,w=w+Math.imul(ft,Et)|0,g=g+Math.imul(ot,Ot)|0,a=a+Math.imul(ot,It)|0,a=a+Math.imul(at,Ot)|0,w=w+Math.imul(at,It)|0,g=g+Math.imul(it,Ct)|0,a=a+Math.imul(it,Tt)|0,a=a+Math.imul(nt,Ct)|0,w=w+Math.imul(nt,Tt)|0,g=g+Math.imul(q,kt)|0,a=a+Math.imul(q,xt)|0,a=a+Math.imul(W,kt)|0,w=w+Math.imul(W,xt)|0;var ni=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ni>>>26)|0,ni&=67108863,g=Math.imul(pt,At),a=Math.imul(pt,St),a=a+Math.imul(mt,At)|0,w=Math.imul(mt,St),g=g+Math.imul(ct,Bt)|0,a=a+Math.imul(ct,Et)|0,a=a+Math.imul(ht,Bt)|0,w=w+Math.imul(ht,Et)|0,g=g+Math.imul(dt,Ot)|0,a=a+Math.imul(dt,It)|0,a=a+Math.imul(ft,Ot)|0,w=w+Math.imul(ft,It)|0,g=g+Math.imul(ot,Ct)|0,a=a+Math.imul(ot,Tt)|0,a=a+Math.imul(at,Ct)|0,w=w+Math.imul(at,Tt)|0,g=g+Math.imul(it,kt)|0,a=a+Math.imul(it,xt)|0,a=a+Math.imul(nt,kt)|0,w=w+Math.imul(nt,xt)|0,g=g+Math.imul(q,Rt)|0,a=a+Math.imul(q,Dt)|0,a=a+Math.imul(W,Rt)|0,w=w+Math.imul(W,Dt)|0;var oi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(oi>>>26)|0,oi&=67108863,g=Math.imul(_t,At),a=Math.imul(_t,St),a=a+Math.imul(gt,At)|0,w=Math.imul(gt,St),g=g+Math.imul(pt,Bt)|0,a=a+Math.imul(pt,Et)|0,a=a+Math.imul(mt,Bt)|0,w=w+Math.imul(mt,Et)|0,g=g+Math.imul(ct,Ot)|0,a=a+Math.imul(ct,It)|0,a=a+Math.imul(ht,Ot)|0,w=w+Math.imul(ht,It)|0,g=g+Math.imul(dt,Ct)|0,a=a+Math.imul(dt,Tt)|0,a=a+Math.imul(ft,Ct)|0,w=w+Math.imul(ft,Tt)|0,g=g+Math.imul(ot,kt)|0,a=a+Math.imul(ot,xt)|0,a=a+Math.imul(at,kt)|0,w=w+Math.imul(at,xt)|0,g=g+Math.imul(it,Rt)|0,a=a+Math.imul(it,Dt)|0,a=a+Math.imul(nt,Rt)|0,w=w+Math.imul(nt,Dt)|0,g=g+Math.imul(q,Ft)|0,a=a+Math.imul(q,Nt)|0,a=a+Math.imul(W,Ft)|0,w=w+Math.imul(W,Nt)|0;var si=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(si>>>26)|0,si&=67108863,g=Math.imul(bt,At),a=Math.imul(bt,St),a=a+Math.imul(wt,At)|0,w=Math.imul(wt,St),g=g+Math.imul(_t,Bt)|0,a=a+Math.imul(_t,Et)|0,a=a+Math.imul(gt,Bt)|0,w=w+Math.imul(gt,Et)|0,g=g+Math.imul(pt,Ot)|0,a=a+Math.imul(pt,It)|0,a=a+Math.imul(mt,Ot)|0,w=w+Math.imul(mt,It)|0,g=g+Math.imul(ct,Ct)|0,a=a+Math.imul(ct,Tt)|0,a=a+Math.imul(ht,Ct)|0,w=w+Math.imul(ht,Tt)|0,g=g+Math.imul(dt,kt)|0,a=a+Math.imul(dt,xt)|0,a=a+Math.imul(ft,kt)|0,w=w+Math.imul(ft,xt)|0,g=g+Math.imul(ot,Rt)|0,a=a+Math.imul(ot,Dt)|0,a=a+Math.imul(at,Rt)|0,w=w+Math.imul(at,Dt)|0,g=g+Math.imul(it,Ft)|0,a=a+Math.imul(it,Nt)|0,a=a+Math.imul(nt,Ft)|0,w=w+Math.imul(nt,Nt)|0,g=g+Math.imul(q,Lt)|0,a=a+Math.imul(q,qt)|0,a=a+Math.imul(W,Lt)|0,w=w+Math.imul(W,qt)|0;var ai=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ai>>>26)|0,ai&=67108863,g=Math.imul(vt,At),a=Math.imul(vt,St),a=a+Math.imul(yt,At)|0,w=Math.imul(yt,St),g=g+Math.imul(bt,Bt)|0,a=a+Math.imul(bt,Et)|0,a=a+Math.imul(wt,Bt)|0,w=w+Math.imul(wt,Et)|0,g=g+Math.imul(_t,Ot)|0,a=a+Math.imul(_t,It)|0,a=a+Math.imul(gt,Ot)|0,w=w+Math.imul(gt,It)|0,g=g+Math.imul(pt,Ct)|0,a=a+Math.imul(pt,Tt)|0,a=a+Math.imul(mt,Ct)|0,w=w+Math.imul(mt,Tt)|0,g=g+Math.imul(ct,kt)|0,a=a+Math.imul(ct,xt)|0,a=a+Math.imul(ht,kt)|0,w=w+Math.imul(ht,xt)|0,g=g+Math.imul(dt,Rt)|0,a=a+Math.imul(dt,Dt)|0,a=a+Math.imul(ft,Rt)|0,w=w+Math.imul(ft,Dt)|0,g=g+Math.imul(ot,Ft)|0,a=a+Math.imul(ot,Nt)|0,a=a+Math.imul(at,Ft)|0,w=w+Math.imul(at,Nt)|0,g=g+Math.imul(it,Lt)|0,a=a+Math.imul(it,qt)|0,a=a+Math.imul(nt,Lt)|0,w=w+Math.imul(nt,qt)|0,g=g+Math.imul(q,jt)|0,a=a+Math.imul(q,Wt)|0,a=a+Math.imul(W,jt)|0,w=w+Math.imul(W,Wt)|0;var li=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(li>>>26)|0,li&=67108863,g=Math.imul(Mt,At),a=Math.imul(Mt,St),a=a+Math.imul(Pt,At)|0,w=Math.imul(Pt,St),g=g+Math.imul(vt,Bt)|0,a=a+Math.imul(vt,Et)|0,a=a+Math.imul(yt,Bt)|0,w=w+Math.imul(yt,Et)|0,g=g+Math.imul(bt,Ot)|0,a=a+Math.imul(bt,It)|0,a=a+Math.imul(wt,Ot)|0,w=w+Math.imul(wt,It)|0,g=g+Math.imul(_t,Ct)|0,a=a+Math.imul(_t,Tt)|0,a=a+Math.imul(gt,Ct)|0,w=w+Math.imul(gt,Tt)|0,g=g+Math.imul(pt,kt)|0,a=a+Math.imul(pt,xt)|0,a=a+Math.imul(mt,kt)|0,w=w+Math.imul(mt,xt)|0,g=g+Math.imul(ct,Rt)|0,a=a+Math.imul(ct,Dt)|0,a=a+Math.imul(ht,Rt)|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(ft,Ft)|0,w=w+Math.imul(ft,Nt)|0,g=g+Math.imul(ot,Lt)|0,a=a+Math.imul(ot,qt)|0,a=a+Math.imul(at,Lt)|0,w=w+Math.imul(at,qt)|0,g=g+Math.imul(it,jt)|0,a=a+Math.imul(it,Wt)|0,a=a+Math.imul(nt,jt)|0,w=w+Math.imul(nt,Wt)|0,g=g+Math.imul(q,Zt)|0,a=a+Math.imul(q,Ut)|0,a=a+Math.imul(W,Zt)|0,w=w+Math.imul(W,Ut)|0;var ui=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ui>>>26)|0,ui&=67108863,g=Math.imul(Mt,Bt),a=Math.imul(Mt,Et),a=a+Math.imul(Pt,Bt)|0,w=Math.imul(Pt,Et),g=g+Math.imul(vt,Ot)|0,a=a+Math.imul(vt,It)|0,a=a+Math.imul(yt,Ot)|0,w=w+Math.imul(yt,It)|0,g=g+Math.imul(bt,Ct)|0,a=a+Math.imul(bt,Tt)|0,a=a+Math.imul(wt,Ct)|0,w=w+Math.imul(wt,Tt)|0,g=g+Math.imul(_t,kt)|0,a=a+Math.imul(_t,xt)|0,a=a+Math.imul(gt,kt)|0,w=w+Math.imul(gt,xt)|0,g=g+Math.imul(pt,Rt)|0,a=a+Math.imul(pt,Dt)|0,a=a+Math.imul(mt,Rt)|0,w=w+Math.imul(mt,Dt)|0,g=g+Math.imul(ct,Ft)|0,a=a+Math.imul(ct,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(ft,Lt)|0,w=w+Math.imul(ft,qt)|0,g=g+Math.imul(ot,jt)|0,a=a+Math.imul(ot,Wt)|0,a=a+Math.imul(at,jt)|0,w=w+Math.imul(at,Wt)|0,g=g+Math.imul(it,Zt)|0,a=a+Math.imul(it,Ut)|0,a=a+Math.imul(nt,Zt)|0,w=w+Math.imul(nt,Ut)|0;var di=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(di>>>26)|0,di&=67108863,g=Math.imul(Mt,Ot),a=Math.imul(Mt,It),a=a+Math.imul(Pt,Ot)|0,w=Math.imul(Pt,It),g=g+Math.imul(vt,Ct)|0,a=a+Math.imul(vt,Tt)|0,a=a+Math.imul(yt,Ct)|0,w=w+Math.imul(yt,Tt)|0,g=g+Math.imul(bt,kt)|0,a=a+Math.imul(bt,xt)|0,a=a+Math.imul(wt,kt)|0,w=w+Math.imul(wt,xt)|0,g=g+Math.imul(_t,Rt)|0,a=a+Math.imul(_t,Dt)|0,a=a+Math.imul(gt,Rt)|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(ct,Lt)|0,a=a+Math.imul(ct,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(ft,jt)|0,w=w+Math.imul(ft,Wt)|0,g=g+Math.imul(ot,Zt)|0,a=a+Math.imul(ot,Ut)|0,a=a+Math.imul(at,Zt)|0,w=w+Math.imul(at,Ut)|0;var fi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(fi>>>26)|0,fi&=67108863,g=Math.imul(Mt,Ct),a=Math.imul(Mt,Tt),a=a+Math.imul(Pt,Ct)|0,w=Math.imul(Pt,Tt),g=g+Math.imul(vt,kt)|0,a=a+Math.imul(vt,xt)|0,a=a+Math.imul(yt,kt)|0,w=w+Math.imul(yt,xt)|0,g=g+Math.imul(bt,Rt)|0,a=a+Math.imul(bt,Dt)|0,a=a+Math.imul(wt,Rt)|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(ct,jt)|0,a=a+Math.imul(ct,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(ft,Zt)|0,w=w+Math.imul(ft,Ut)|0;var ci=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ci>>>26)|0,ci&=67108863,g=Math.imul(Mt,kt),a=Math.imul(Mt,xt),a=a+Math.imul(Pt,kt)|0,w=Math.imul(Pt,xt),g=g+Math.imul(vt,Rt)|0,a=a+Math.imul(vt,Dt)|0,a=a+Math.imul(yt,Rt)|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(ct,Zt)|0,a=a+Math.imul(ct,Ut)|0,a=a+Math.imul(ht,Zt)|0,w=w+Math.imul(ht,Ut)|0;var hi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(hi>>>26)|0,hi&=67108863,g=Math.imul(Mt,Rt),a=Math.imul(Mt,Dt),a=a+Math.imul(Pt,Rt)|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 pi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(pi>>>26)|0,pi&=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 mi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(mi>>>26)|0,mi&=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 _i=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(_i>>>26)|0,_i&=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 gi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(gi>>>26)|0,gi&=67108863,g=Math.imul(Mt,Zt),a=Math.imul(Mt,Ut),a=a+Math.imul(Pt,Zt)|0,w=Math.imul(Pt,Ut);var bi=(A+g|0)+((a&8191)<<13)|0;return A=(w+(a>>>13)|0)+(bi>>>26)|0,bi&=67108863,M[0]=ti,M[1]=ei,M[2]=ii,M[3]=ri,M[4]=ni,M[5]=oi,M[6]=si,M[7]=ai,M[8]=li,M[9]=ui,M[10]=di,M[11]=fi,M[12]=ci,M[13]=hi,M[14]=pi,M[15]=mi,M[16]=_i,M[17]=gi,M[18]=bi,A!==0&&(M[19]=A,h.length++),h};Math.imul||(x=k);function R(v,o,l){l.negative=o.negative^v.negative,l.length=v.length+o.length;for(var h=0,m=0,b=0;b<l.length-1;b++){var M=m;m=0;for(var A=h&67108863,g=Math.min(b,o.length-1),a=Math.max(0,b-v.length+1);a<=g;a++){var w=b-a,N=v.words[w]|0,q=o.words[a]|0,W=N*q,X=W&67108863;M=M+(W/67108864|0)|0,X=X+A|0,A=X&67108863,M=M+(X>>>26)|0,m+=M>>>26,M&=67108863}l.words[b]=A,h=M,M=m}return h!==0?l.words[b]=h:l.length--,l._strip()}function T(v,o,l){return R(v,o,l)}i.prototype.mulTo=function(o,l){var h,m=this.length+o.length;return this.length===10&&o.length===10?h=x(this,o,l):m<63?h=k(this,o,l):m<1024?h=R(this,o,l):h=T(this,o,l),h};function I(v,o){this.x=v,this.y=o}I.prototype.makeRBT=function(o){for(var l=new Array(o),h=i.prototype._countBits(o)-1,m=0;m<o;m++)l[m]=this.revBin(m,h,o);return l},I.prototype.revBin=function(o,l,h){if(o===0||o===h-1)return o;for(var m=0,b=0;b<l;b++)m|=(o&1)<<l-b-1,o>>=1;return m},I.prototype.permute=function(o,l,h,m,b,M){for(var A=0;A<M;A++)m[A]=l[o[A]],b[A]=h[o[A]]},I.prototype.transform=function(o,l,h,m,b,M){this.permute(M,o,l,h,m,b);for(var A=1;A<b;A<<=1)for(var g=A<<1,a=Math.cos(2*Math.PI/g),w=Math.sin(2*Math.PI/g),N=0;N<b;N+=g)for(var q=a,W=w,X=0;X<A;X++){var it=h[N+X],nt=m[N+X],le=h[N+X+A],ot=m[N+X+A],at=q*le-W*ot;ot=q*ot+W*le,le=at,h[N+X]=it+le,m[N+X]=nt+ot,h[N+X+A]=it-le,m[N+X+A]=nt-ot,X!==g&&(at=a*q-w*W,W=a*W+w*q,q=at)}},I.prototype.guessLen13b=function(o,l){var h=Math.max(l,o)|1,m=h&1,b=0;for(h=h/2|0;h;h=h>>>1)b++;return 1<<b+1+m},I.prototype.conjugate=function(o,l,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=l[m],l[m]=-l[h-m-1],l[h-m-1]=-b}},I.prototype.normalize13b=function(o,l){for(var h=0,m=0;m<l/2;m++){var b=Math.round(o[2*m+1]/l)*8192+Math.round(o[2*m]/l)+h;o[m]=b&67108863,b<67108864?h=0:h=b/67108864|0}return o},I.prototype.convert13b=function(o,l,h,m){for(var b=0,M=0;M<l;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*l;M<m;++M)h[M]=0;t(b===0),t((b&-8192)===0)},I.prototype.stub=function(o){for(var l=new Array(o),h=0;h<o;h++)l[h]=0;return l},I.prototype.mulp=function(o,l,h){var m=2*this.guessLen13b(o.length,l.length),b=this.makeRBT(m),M=this.stub(m),A=new Array(m),g=new Array(m),a=new Array(m),w=new Array(m),N=new Array(m),q=new Array(m),W=h.words;W.length=m,this.convert13b(o.words,o.length,A,m),this.convert13b(l.words,l.length,w,m),this.transform(A,M,g,a,m,b),this.transform(w,M,N,q,m,b);for(var X=0;X<m;X++){var it=g[X]*N[X]-a[X]*q[X];a[X]=g[X]*q[X]+a[X]*N[X],g[X]=it}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^l.negative,h.length=o.length+l.length,h._strip()},i.prototype.mul=function(o){var l=new i(null);return l.words=new Array(this.length+o.length),this.mulTo(o,l)},i.prototype.mulf=function(o){var l=new i(null);return l.words=new Array(this.length+o.length),T(this,o,l)},i.prototype.imul=function(o){return this.clone().mulTo(o,this)},i.prototype.imuln=function(o){var l=o<0;l&&(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,l?this.ineg():this},i.prototype.muln=function(o){return this.clone().imuln(o)},i.prototype.sqr=function(){return this.mul(this)},i.prototype.isqr=function(){return this.imul(this.clone())},i.prototype.pow=function(o){var l=B(o);if(l.length===0)return new i(1);for(var h=this,m=0;m<l.length&&l[m]===0;m++,h=h.sqr());if(++m<l.length)for(var b=h.sqr();m<l.length;m++,b=b.sqr())l[m]!==0&&(h=h.mul(b));return h},i.prototype.iushln=function(o){t(typeof o=="number"&&o>=0);var l=o%26,h=(o-l)/26,m=67108863>>>26-l<<26-l,b;if(l!==0){var M=0;for(b=0;b<this.length;b++){var A=this.words[b]&m,g=(this.words[b]|0)-A<<l;this.words[b]=g|M,M=A>>>26-l}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()},i.prototype.ishln=function(o){return t(this.negative===0),this.iushln(o)},i.prototype.iushrn=function(o,l,h){t(typeof o=="number"&&o>=0);var m;l?m=(l-l%26)/26:m=0;var b=o%26,M=Math.min((o-b)/26,this.length),A=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 N=this.words[a]|0;this.words[a]=w<<26-b|N>>>b,w=N&A}return g&&w!==0&&(g.words[g.length++]=w),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},i.prototype.ishrn=function(o,l,h){return t(this.negative===0),this.iushrn(o,l,h)},i.prototype.shln=function(o){return this.clone().ishln(o)},i.prototype.ushln=function(o){return this.clone().iushln(o)},i.prototype.shrn=function(o){return this.clone().ishrn(o)},i.prototype.ushrn=function(o){return this.clone().iushrn(o)},i.prototype.testn=function(o){t(typeof o=="number"&&o>=0);var l=o%26,h=(o-l)/26,m=1<<l;if(this.length<=h)return!1;var b=this.words[h];return!!(b&m)},i.prototype.imaskn=function(o){t(typeof o=="number"&&o>=0);var l=o%26,h=(o-l)/26;if(t(this.negative===0,"imaskn works only with positive numbers"),this.length<=h)return this;if(l!==0&&h++,this.length=Math.min(h,this.length),l!==0){var m=67108863^67108863>>>l<<l;this.words[this.length-1]&=m}return this._strip()},i.prototype.maskn=function(o){return this.clone().imaskn(o)},i.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)},i.prototype._iaddn=function(o){this.words[0]+=o;for(var l=0;l<this.length&&this.words[l]>=67108864;l++)this.words[l]-=67108864,l===this.length-1?this.words[l+1]=1:this.words[l+1]++;return this.length=Math.max(this.length,l+1),this},i.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 l=0;l<this.length&&this.words[l]<0;l++)this.words[l]+=67108864,this.words[l+1]-=1;return this._strip()},i.prototype.addn=function(o){return this.clone().iaddn(o)},i.prototype.subn=function(o){return this.clone().isubn(o)},i.prototype.iabs=function(){return this.negative=0,this},i.prototype.abs=function(){return this.clone().iabs()},i.prototype._ishlnsubmul=function(o,l,h){var m=o.length+h,b;this._expand(m);var M,A=0;for(b=0;b<o.length;b++){M=(this.words[b+h]|0)+A;var g=(o.words[b]|0)*l;M-=g&67108863,A=(M>>26)-(g/67108864|0),this.words[b+h]=M&67108863}for(;b<this.length-h;b++)M=(this.words[b+h]|0)+A,A=M>>26,this.words[b+h]=M&67108863;if(A===0)return this._strip();for(t(A===-1),A=0,b=0;b<this.length;b++)M=-(this.words[b]|0)+A,A=M>>26,this.words[b]=M&67108863;return this.negative=1,this._strip()},i.prototype._wordDiv=function(o,l){var h=this.length-o.length,m=this.clone(),b=o,M=b.words[b.length-1]|0,A=this._countBits(M);h=26-A,h!==0&&(b=b.ushln(h),m.iushln(h),M=b.words[b.length-1]|0);var g=m.length-b.length,a;if(l!=="mod"){a=new i(null),a.length=g+1,a.words=new Array(a.length);for(var w=0;w<a.length;w++)a.words[w]=0}var N=m.clone()._ishlnsubmul(b,1,g);N.negative===0&&(m=N,a&&(a.words[g]=1));for(var q=g-1;q>=0;q--){var W=(m.words[b.length+q]|0)*67108864+(m.words[b.length+q-1]|0);for(W=Math.min(W/M|0,67108863),m._ishlnsubmul(b,W,q);m.negative!==0;)W--,m.negative=0,m._ishlnsubmul(b,1,q),m.isZero()||(m.negative^=1);a&&(a.words[q]=W)}return a&&a._strip(),m._strip(),l!=="div"&&h!==0&&m.iushrn(h),{div:a||null,mod:m}},i.prototype.divmod=function(o,l,h){if(t(!o.isZero()),this.isZero())return{div:new i(0),mod:new i(0)};var m,b,M;return this.negative!==0&&o.negative===0?(M=this.neg().divmod(o,l),l!=="mod"&&(m=M.div.neg()),l!=="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(),l),l!=="mod"&&(m=M.div.neg()),{div:m,mod:M.mod}):(this.negative&o.negative)!==0?(M=this.neg().divmod(o.neg(),l),l!=="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 i(0),mod:this}:o.length===1?l==="div"?{div:this.divn(o.words[0]),mod:null}:l==="mod"?{div:null,mod:new i(this.modrn(o.words[0]))}:{div:this.divn(o.words[0]),mod:new i(this.modrn(o.words[0]))}:this._wordDiv(o,l)},i.prototype.div=function(o){return this.divmod(o,"div",!1).div},i.prototype.mod=function(o){return this.divmod(o,"mod",!1).mod},i.prototype.umod=function(o){return this.divmod(o,"mod",!0).mod},i.prototype.divRound=function(o){var l=this.divmod(o);if(l.mod.isZero())return l.div;var h=l.div.negative!==0?l.mod.isub(o):l.mod,m=o.ushrn(1),b=o.andln(1),M=h.cmp(m);return M<0||b===1&&M===0?l.div:l.div.negative!==0?l.div.isubn(1):l.div.iaddn(1)},i.prototype.modrn=function(o){var l=o<0;l&&(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 l?-m:m},i.prototype.modn=function(o){return this.modrn(o)},i.prototype.idivn=function(o){var l=o<0;l&&(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(),l?this.ineg():this},i.prototype.divn=function(o){return this.clone().idivn(o)},i.prototype.egcd=function(o){t(o.negative===0),t(!o.isZero());var l=this,h=o.clone();l.negative!==0?l=l.umod(o):l=l.clone();for(var m=new i(1),b=new i(0),M=new i(0),A=new i(1),g=0;l.isEven()&&h.isEven();)l.iushrn(1),h.iushrn(1),++g;for(var a=h.clone(),w=l.clone();!l.isZero();){for(var N=0,q=1;(l.words[0]&q)===0&&N<26;++N,q<<=1);if(N>0)for(l.iushrn(N);N-- >0;)(m.isOdd()||b.isOdd())&&(m.iadd(a),b.isub(w)),m.iushrn(1),b.iushrn(1);for(var W=0,X=1;(h.words[0]&X)===0&&W<26;++W,X<<=1);if(W>0)for(h.iushrn(W);W-- >0;)(M.isOdd()||A.isOdd())&&(M.iadd(a),A.isub(w)),M.iushrn(1),A.iushrn(1);l.cmp(h)>=0?(l.isub(h),m.isub(M),b.isub(A)):(h.isub(l),M.isub(m),A.isub(b))}return{a:M,b:A,gcd:h.iushln(g)}},i.prototype._invmp=function(o){t(o.negative===0),t(!o.isZero());var l=this,h=o.clone();l.negative!==0?l=l.umod(o):l=l.clone();for(var m=new i(1),b=new i(0),M=h.clone();l.cmpn(1)>0&&h.cmpn(1)>0;){for(var A=0,g=1;(l.words[0]&g)===0&&A<26;++A,g<<=1);if(A>0)for(l.iushrn(A);A-- >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);l.cmp(h)>=0?(l.isub(h),m.isub(b)):(h.isub(l),b.isub(m))}var N;return l.cmpn(1)===0?N=m:N=b,N.cmpn(0)<0&&N.iadd(o),N},i.prototype.gcd=function(o){if(this.isZero())return o.abs();if(o.isZero())return this.abs();var l=this.clone(),h=o.clone();l.negative=0,h.negative=0;for(var m=0;l.isEven()&&h.isEven();m++)l.iushrn(1),h.iushrn(1);do{for(;l.isEven();)l.iushrn(1);for(;h.isEven();)h.iushrn(1);var b=l.cmp(h);if(b<0){var M=l;l=h,h=M}else if(b===0||h.cmpn(1)===0)break;l.isub(h)}while(!0);return h.iushln(m)},i.prototype.invm=function(o){return this.egcd(o).a.umod(o)},i.prototype.isEven=function(){return(this.words[0]&1)===0},i.prototype.isOdd=function(){return(this.words[0]&1)===1},i.prototype.andln=function(o){return this.words[0]&o},i.prototype.bincn=function(o){t(typeof o=="number");var l=o%26,h=(o-l)/26,m=1<<l;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 A=this.words[M]|0;A+=b,b=A>>>26,A&=67108863,this.words[M]=A}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(o){var l=o<0;if(this.negative!==0&&!l)return-1;if(this.negative===0&&l)return 1;this._strip();var h;if(this.length>1)h=1;else{l&&(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},i.prototype.cmp=function(o){if(this.negative!==0&&o.negative===0)return-1;if(this.negative===0&&o.negative!==0)return 1;var l=this.ucmp(o);return this.negative!==0?-l|0:l},i.prototype.ucmp=function(o){if(this.length>o.length)return 1;if(this.length<o.length)return-1;for(var l=0,h=this.length-1;h>=0;h--){var m=this.words[h]|0,b=o.words[h]|0;if(m!==b){m<b?l=-1:m>b&&(l=1);break}}return l},i.prototype.gtn=function(o){return this.cmpn(o)===1},i.prototype.gt=function(o){return this.cmp(o)===1},i.prototype.gten=function(o){return this.cmpn(o)>=0},i.prototype.gte=function(o){return this.cmp(o)>=0},i.prototype.ltn=function(o){return this.cmpn(o)===-1},i.prototype.lt=function(o){return this.cmp(o)===-1},i.prototype.lten=function(o){return this.cmpn(o)<=0},i.prototype.lte=function(o){return this.cmp(o)<=0},i.prototype.eqn=function(o){return this.cmpn(o)===0},i.prototype.eq=function(o){return this.cmp(o)===0},i.red=function(o){return new J(o)},i.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)},i.prototype.fromRed=function(){return t(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},i.prototype._forceRed=function(o){return this.red=o,this},i.prototype.forceRed=function(o){return t(!this.red,"Already a number in reduction context"),this._forceRed(o)},i.prototype.redAdd=function(o){return t(this.red,"redAdd works only with red numbers"),this.red.add(this,o)},i.prototype.redIAdd=function(o){return t(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,o)},i.prototype.redSub=function(o){return t(this.red,"redSub works only with red numbers"),this.red.sub(this,o)},i.prototype.redISub=function(o){return t(this.red,"redISub works only with red numbers"),this.red.isub(this,o)},i.prototype.redShl=function(o){return t(this.red,"redShl works only with red numbers"),this.red.shl(this,o)},i.prototype.redMul=function(o){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,o),this.red.mul(this,o)},i.prototype.redIMul=function(o){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,o),this.red.imul(this,o)},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(o){return t(this.red&&!o.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,o)};var G={k256:null,p224:null,p192:null,p25519:null};function H(v,o){this.name=v,this.p=new i(o,16),this.n=this.p.bitLength(),this.k=new i(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}H.prototype._tmp=function(){var o=new i(null);return o.words=new Array(Math.ceil(this.n/13)),o},H.prototype.ireduce=function(o){var l=o,h;do this.split(l,this.tmp),l=this.imulK(l),l=l.iadd(this.tmp),h=l.bitLength();while(h>this.n);var m=h<this.n?-1:l.ucmp(this.p);return m===0?(l.words[0]=0,l.length=1):m>0?l.isub(this.p):l.strip!==void 0?l.strip():l._strip(),l},H.prototype.split=function(o,l){o.iushrn(this.n,0,l)},H.prototype.imulK=function(o){return o.imul(this.k)};function Q(){H.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}r(Q,H),Q.prototype.split=function(o,l){for(var h=4194303,m=Math.min(o.length,9),b=0;b<m;b++)l.words[b]=o.words[b];if(l.length=m,o.length<=9){o.words[0]=0,o.length=1;return}var M=o.words[9];for(l.words[l.length++]=M&h,b=10;b<o.length;b++){var A=o.words[b]|0;o.words[b-10]=(A&h)<<4|M>>>22,M=A}M>>>=22,o.words[b-10]=M,M===0&&o.length>10?o.length-=10:o.length-=9},Q.prototype.imulK=function(o){o.words[o.length]=0,o.words[o.length+1]=0,o.length+=2;for(var l=0,h=0;h<o.length;h++){var m=o.words[h]|0;l+=m*977,o.words[h]=l&67108863,l=m*64+(l/67108864|0)}return o.words[o.length-1]===0&&(o.length--,o.words[o.length-1]===0&&o.length--),o};function tt(){H.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}r(tt,H);function $t(){H.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}r($t,H);function Vt(){H.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}r(Vt,H),Vt.prototype.imulK=function(o){for(var l=0,h=0;h<o.length;h++){var m=(o.words[h]|0)*19+l,b=m&67108863;m>>>=26,o.words[h]=b,l=m}return l!==0&&(o.words[o.length++]=l),o},i._prime=function(o){if(G[o])return G[o];var l;if(o==="k256")l=new Q;else if(o==="p224")l=new tt;else if(o==="p192")l=new $t;else if(o==="p25519")l=new Vt;else throw new Error("Unknown prime "+o);return G[o]=l,l};function J(v){if(typeof v=="string"){var o=i._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}J.prototype._verify1=function(o){t(o.negative===0,"red works only with positives"),t(o.red,"red works only with red numbers")},J.prototype._verify2=function(o,l){t((o.negative|l.negative)===0,"red works only with positives"),t(o.red&&o.red===l.red,"red works only with red numbers")},J.prototype.imod=function(o){return this.prime?this.prime.ireduce(o)._forceRed(this):(c(o,o.umod(this.m)._forceRed(this)),o)},J.prototype.neg=function(o){return o.isZero()?o.clone():this.m.sub(o)._forceRed(this)},J.prototype.add=function(o,l){this._verify2(o,l);var h=o.add(l);return h.cmp(this.m)>=0&&h.isub(this.m),h._forceRed(this)},J.prototype.iadd=function(o,l){this._verify2(o,l);var h=o.iadd(l);return h.cmp(this.m)>=0&&h.isub(this.m),h},J.prototype.sub=function(o,l){this._verify2(o,l);var h=o.sub(l);return h.cmpn(0)<0&&h.iadd(this.m),h._forceRed(this)},J.prototype.isub=function(o,l){this._verify2(o,l);var h=o.isub(l);return h.cmpn(0)<0&&h.iadd(this.m),h},J.prototype.shl=function(o,l){return this._verify1(o),this.imod(o.ushln(l))},J.prototype.imul=function(o,l){return this._verify2(o,l),this.imod(o.imul(l))},J.prototype.mul=function(o,l){return this._verify2(o,l),this.imod(o.mul(l))},J.prototype.isqr=function(o){return this.imul(o,o.clone())},J.prototype.sqr=function(o){return this.mul(o,o)},J.prototype.sqrt=function(o){if(o.isZero())return o.clone();var l=this.m.andln(3);if(t(l%2===1),l===3){var h=this.m.add(new i(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 i(1).toRed(this),A=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(A)!==0;)a.redIAdd(A);for(var w=this.pow(a,m),N=this.pow(o,m.addn(1).iushrn(1)),q=this.pow(o,m),W=b;q.cmp(M)!==0;){for(var X=q,it=0;X.cmp(M)!==0;it++)X=X.redSqr();t(it<W);var nt=this.pow(w,new i(1).iushln(W-it-1));N=N.redMul(nt),w=nt.redSqr(),q=q.redMul(w),W=it}return N},J.prototype.invm=function(o){var l=o._invmp(this.m);return l.negative!==0?(l.negative=0,this.imod(l).redNeg()):this.imod(l)},J.prototype.pow=function(o,l){if(l.isZero())return new i(1).toRed(this);if(l.cmpn(1)===0)return o.clone();var h=4,m=new Array(1<<h);m[0]=new i(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],A=0,g=0,a=l.bitLength()%26;for(a===0&&(a=26),b=l.length-1;b>=0;b--){for(var w=l.words[b],N=a-1;N>=0;N--){var q=w>>N&1;if(M!==m[0]&&(M=this.sqr(M)),q===0&&A===0){g=0;continue}A<<=1,A|=q,g++,!(g!==h&&(b!==0||N!==0))&&(M=this.mul(M,m[A]),g=0,A=0)}a=26}return M},J.prototype.convertTo=function(o){var l=o.umod(this.m);return l===o?l.clone():l},J.prototype.convertFrom=function(o){var l=o.clone();return l.red=null,l},i.mont=function(o){return new st(o)};function st(v){J.call(this,v),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)}r(st,J),st.prototype.convertTo=function(o){return this.imod(o.ushln(this.shift))},st.prototype.convertFrom=function(o){var l=this.imod(o.mul(this.rinv));return l.red=null,l},st.prototype.imul=function(o,l){if(o.isZero()||l.isZero())return o.words[0]=0,o.length=1,o;var h=o.imul(l),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)},st.prototype.mul=function(o,l){if(o.isZero()||l.isZero())return new i(0)._forceRed(this);var h=o.mul(l),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)},st.prototype.invm=function(o){var l=this.imod(o._invmp(this.m).mul(this.r2));return l._forceRed(this)}})(typeof yi>"u"||yi,ar)});var Qn={};Lr(Qn,{BASIS_POINT:()=>Yt,BASIS_POINT_MAX:()=>we,BIN_BOUND:()=>_e,BinUtils:()=>F,CetusDlmmSDK:()=>xe,DEFAULT_MAX_WEIGHT:()=>ve,DEFAULT_MIN_WEIGHT:()=>me,FEE_PRECISION:()=>Ei,FeeUtils:()=>Oe,MAX_BIN_ID:()=>Dn,MAX_BIN_PER_POSITION:()=>ae,MAX_FEE_RATE:()=>He,MIN_BIN_ID:()=>Rn,ONE:()=>Qe,PoolModule:()=>Te,REWARD_PERIOD:()=>Fn,REWARD_PERIOD_START_AT:()=>Nn,SCALE_OFFSET:()=>D,StrategyType:()=>Be,StrategyUtils:()=>Pe,WeightUtils:()=>te,buildPoolKey:()=>Fi,default:()=>Hn,dlmmMainnet:()=>vi,dlmmTestnet:()=>Li,generateRewardSchedule:()=>Wn,getRouterModule:()=>Di,parseBinInfo:()=>Ci,parseBinInfoList:()=>Ii,parseCurrentRewardPeriodEmission:()=>Un,parseDlmmBasePool:()=>Oi,parseDlmmPool:()=>Ee,parseDlmmPosition:()=>$e,parseLiquidityShares:()=>qn,parsePartner:()=>Ke,parsePoolTransactionInfo:()=>Ri,parseRewardPeriodEmission:()=>Zn,parseStrategyType:()=>jn,parsedDlmmPosFeeData:()=>Ti,parsedDlmmPosRewardData:()=>ki,parsedSwapQuoteData:()=>xi,poolFilterEvenTypes:()=>Sr,safeAmount:()=>Me,safeMulAmount:()=>re});module.exports=qr(Qn);var Tr=require("@cetusprotocol/common-sdk");var or=require("@cetusprotocol/common-sdk"),vi={env:"mainnet",full_rpc_url:or.FullRpcUrlMainnet,dlmm_pool:{package_id:"0x5664f9d3fd82c84023870cfbda8ea84e14c8dd56ce557ad2116e0668581a682b",published_at:"0x5664f9d3fd82c84023870cfbda8ea84e14c8dd56ce557ad2116e0668581a682b",version:1,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:"0xc65d59c4aa7c9d53836bdbbb2a1418b02be6a351a767c64d6e4b7e96d26356d2",published_at:"0xda28da84b5b1fef0ca053a2373e80ddb3a75f45e891e825fb06c2a53f838067d",version:1}};var Ie=require("@mysten/sui/transactions"),V=require("@cetusprotocol/common-sdk");var sr=require("@cetusprotocol/common-sdk");var pe=class extends sr.BaseError{constructor(e,t,r){super(e,t||"UnknownError",r)}static isDlmmErrorCode(e,t){return this.isErrorCode(e,t)}},$=(n,e,t)=>{let r={...t,stack:e instanceof Error?e.stack:void 0};throw e instanceof Error?new pe(e.message,n,r):new pe(e,n,r)};var z=require("@cetusprotocol/common-sdk"),Pr=wi(De());var ge=9e15,ce=1e9,Mi="0123456789abcdef",Le="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",qe="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Pi={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-ge,maxE:ge,crypto:!1},fr,se,K=!0,We="[DecimalError] ",fe=We+"Invalid argument: ",cr=We+"Precision limit exceeded",hr=We+"crypto unavailable",pr="[object Decimal]",zt=Math.floor,Ht=Math.pow,jr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Wr=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Zr=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,mr=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,ie=1e7,U=7,Ur=9007199254740991,Hr=Le.length-1,Ai=qe.length-1,E={toStringTag:pr};E.absoluteValue=E.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),j(n)};E.ceil=function(){return j(new this.constructor(this),this.e+1,2)};E.clampedTo=E.clamp=function(n,e){var t,r=this,i=r.constructor;if(n=new i(n),e=new i(e),!n.s||!e.s)return new i(NaN);if(n.gt(e))throw Error(fe+e);return t=r.cmp(n),t<0?n:r.cmp(e)>0?e:new i(r)};E.comparedTo=E.cmp=function(n){var e,t,r,i,s=this,u=s.d,d=(n=new s.constructor(n)).d,f=s.s,c=n.s;if(!u||!d)return!f||!c?NaN:f!==c?f:u===d?0:!u^f<0?1:-1;if(!u[0]||!d[0])return u[0]?f:d[0]?-c:0;if(f!==c)return f;if(s.e!==n.e)return s.e>n.e^f<0?1:-1;for(r=u.length,i=d.length,e=0,t=r<i?r:i;e<t;++e)if(u[e]!==d[e])return u[e]>d[e]^f<0?1:-1;return r===i?0:r>i^f<0?1:-1};E.cosine=E.cos=function(){var n,e,t=this,r=t.constructor;return t.d?t.d[0]?(n=r.precision,e=r.rounding,r.precision=n+Math.max(t.e,t.sd())+U,r.rounding=1,t=Qr(r,vr(r,t)),r.precision=n,r.rounding=e,j(se==2||se==3?t.neg():t,n,e,!0)):new r(1):new r(NaN)};E.cubeRoot=E.cbrt=function(){var n,e,t,r,i,s,u,d,f,c,p=this,_=p.constructor;if(!p.isFinite()||p.isZero())return new _(p);for(K=!1,s=p.s*Ht(p.s*p,1/3),!s||Math.abs(s)==1/0?(t=Qt(p.d),n=p.e,(s=(n-t.length+1)%3)&&(t+=s==1||s==-2?"0":"00"),s=Ht(t,1/3),n=zt((n+1)/3)-(n%3==(n<0?-1:2)),s==1/0?t="5e"+n:(t=s.toExponential(),t=t.slice(0,t.indexOf("e")+1)+n),r=new _(t),r.s=p.s):r=new _(s.toString()),u=(n=_.precision)+3;;)if(d=r,f=d.times(d).times(d),c=f.plus(p),r=rt(c.plus(p).times(d),c.plus(f),u+2,1),Qt(d.d).slice(0,u)===(t=Qt(r.d)).slice(0,u))if(t=t.slice(u-3,u+1),t=="9999"||!i&&t=="4999"){if(!i&&(j(d,n+1,0),d.times(d).times(d).eq(p))){r=d;break}u+=4,i=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(j(r,n+1,1),e=!r.times(r).times(r).eq(p));break}return K=!0,j(r,n,_.rounding,e)};E.decimalPlaces=E.dp=function(){var n,e=this.d,t=NaN;if(e){if(n=e.length-1,t=(n-zt(this.e/U))*U,n=e[n],n)for(;n%10==0;n/=10)t--;t<0&&(t=0)}return t};E.dividedBy=E.div=function(n){return rt(this,new this.constructor(n))};E.dividedToIntegerBy=E.divToInt=function(n){var e=this,t=e.constructor;return j(rt(e,new t(n),0,1,1),t.precision,t.rounding)};E.equals=E.eq=function(n){return this.cmp(n)===0};E.floor=function(){return j(new this.constructor(this),this.e+1,3)};E.greaterThan=E.gt=function(n){return this.cmp(n)>0};E.greaterThanOrEqualTo=E.gte=function(n){var e=this.cmp(n);return e==1||e===0};E.hyperbolicCosine=E.cosh=function(){var n,e,t,r,i,s=this,u=s.constructor,d=new u(1);if(!s.isFinite())return new u(s.s?1/0:NaN);if(s.isZero())return d;t=u.precision,r=u.rounding,u.precision=t+Math.max(s.e,s.sd())+4,u.rounding=1,i=s.d.length,i<32?(n=Math.ceil(i/3),e=(1/Ue(4,n)).toString()):(n=16,e="2.3283064365386962890625e-10"),s=be(u,1,s.times(e),new u(1),!0);for(var f,c=n,p=new u(8);c--;)f=s.times(s),s=d.minus(f.times(p.minus(f.times(p))));return j(s,u.precision=t,u.rounding=r,!0)};E.hyperbolicSine=E.sinh=function(){var n,e,t,r,i=this,s=i.constructor;if(!i.isFinite()||i.isZero())return new s(i);if(e=s.precision,t=s.rounding,s.precision=e+Math.max(i.e,i.sd())+4,s.rounding=1,r=i.d.length,r<3)i=be(s,2,i,i,!0);else{n=1.4*Math.sqrt(r),n=n>16?16:n|0,i=i.times(1/Ue(5,n)),i=be(s,2,i,i,!0);for(var u,d=new s(5),f=new s(16),c=new s(20);n--;)u=i.times(i),i=i.times(d.plus(u.times(f.times(u).plus(c))))}return s.precision=e,s.rounding=t,j(i,e,t,!0)};E.hyperbolicTangent=E.tanh=function(){var n,e,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,e=r.rounding,r.precision=n+7,r.rounding=1,rt(t.sinh(),t.cosh(),r.precision=n,r.rounding=e)):new r(t.s)};E.inverseCosine=E.acos=function(){var n=this,e=n.constructor,t=n.abs().cmp(1),r=e.precision,i=e.rounding;return t!==-1?t===0?n.isNeg()?ne(e,r,i):new e(0):new e(NaN):n.isZero()?ne(e,r+4,i).times(.5):(e.precision=r+6,e.rounding=1,n=new e(1).minus(n).div(n.plus(1)).sqrt().atan(),e.precision=r,e.rounding=i,n.times(2))};E.inverseHyperbolicCosine=E.acosh=function(){var n,e,t=this,r=t.constructor;return t.lte(1)?new r(t.eq(1)?0:NaN):t.isFinite()?(n=r.precision,e=r.rounding,r.precision=n+Math.max(Math.abs(t.e),t.sd())+4,r.rounding=1,K=!1,t=t.times(t).minus(1).sqrt().plus(t),K=!0,r.precision=n,r.rounding=e,t.ln()):new r(t)};E.inverseHyperbolicSine=E.asinh=function(){var n,e,t=this,r=t.constructor;return!t.isFinite()||t.isZero()?new r(t):(n=r.precision,e=r.rounding,r.precision=n+2*Math.max(Math.abs(t.e),t.sd())+6,r.rounding=1,K=!1,t=t.times(t).plus(1).sqrt().plus(t),K=!0,r.precision=n,r.rounding=e,t.ln())};E.inverseHyperbolicTangent=E.atanh=function(){var n,e,t,r,i=this,s=i.constructor;return i.isFinite()?i.e>=0?new s(i.abs().eq(1)?i.s/0:i.isZero()?i:NaN):(n=s.precision,e=s.rounding,r=i.sd(),Math.max(r,n)<2*-i.e-1?j(new s(i),n,e,!0):(s.precision=t=r-i.e,i=rt(i.plus(1),new s(1).minus(i),t+n,1),s.precision=n+4,s.rounding=1,i=i.ln(),s.precision=n,s.rounding=e,i.times(.5))):new s(NaN)};E.inverseSine=E.asin=function(){var n,e,t,r,i=this,s=i.constructor;return i.isZero()?new s(i):(e=i.abs().cmp(1),t=s.precision,r=s.rounding,e!==-1?e===0?(n=ne(s,t+4,r).times(.5),n.s=i.s,n):new s(NaN):(s.precision=t+6,s.rounding=1,i=i.div(new s(1).minus(i.times(i)).sqrt().plus(1)).atan(),s.precision=t,s.rounding=r,i.times(2)))};E.inverseTangent=E.atan=function(){var n,e,t,r,i,s,u,d,f,c=this,p=c.constructor,_=p.precision,y=p.rounding;if(c.isFinite()){if(c.isZero())return new p(c);if(c.abs().eq(1)&&_+4<=Ai)return u=ne(p,_+4,y).times(.25),u.s=c.s,u}else{if(!c.s)return new p(NaN);if(_+4<=Ai)return u=ne(p,_+4,y).times(.5),u.s=c.s,u}for(p.precision=d=_+10,p.rounding=1,t=Math.min(28,d/U+2|0),n=t;n;--n)c=c.div(c.times(c).plus(1).sqrt().plus(1));for(K=!1,e=Math.ceil(d/U),r=1,f=c.times(c),u=new p(c),i=c;n!==-1;)if(i=i.times(f),s=u.minus(i.div(r+=2)),i=i.times(f),u=s.plus(i.div(r+=2)),u.d[e]!==void 0)for(n=e;u.d[n]===s.d[n]&&n--;);return t&&(u=u.times(2<<t-1)),K=!0,j(u,p.precision=_,p.rounding=y,!0)};E.isFinite=function(){return!!this.d};E.isInteger=E.isInt=function(){return!!this.d&&zt(this.e/U)>this.d.length-2};E.isNaN=function(){return!this.s};E.isNegative=E.isNeg=function(){return this.s<0};E.isPositive=E.isPos=function(){return this.s>0};E.isZero=function(){return!!this.d&&this.d[0]===0};E.lessThan=E.lt=function(n){return this.cmp(n)<0};E.lessThanOrEqualTo=E.lte=function(n){return this.cmp(n)<1};E.logarithm=E.log=function(n){var e,t,r,i,s,u,d,f,c=this,p=c.constructor,_=p.precision,y=p.rounding,P=5;if(n==null)n=new p(10),e=!0;else{if(n=new p(n),t=n.d,n.s<0||!t||!t[0]||n.eq(1))return new p(NaN);e=n.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)s=!0;else{for(i=t[0];i%10===0;)i/=10;s=i!==1}if(K=!1,d=_+P,u=de(c,d),r=e?je(p,d+10):de(n,d),f=rt(u,r,d,1),Ae(f.d,i=_,y))do if(d+=10,u=de(c,d),r=e?je(p,d+10):de(n,d),f=rt(u,r,d,1),!s){+Qt(f.d).slice(i+1,i+15)+1==1e14&&(f=j(f,_+1,0));break}while(Ae(f.d,i+=10,y));return K=!0,j(f,_,y)};E.minus=E.sub=function(n){var e,t,r,i,s,u,d,f,c,p,_,y,P=this,S=P.constructor;if(n=new S(n),!P.d||!n.d)return!P.s||!n.s?n=new S(NaN):P.d?n.s=-n.s:n=new S(n.d||P.s!==n.s?P:NaN),n;if(P.s!=n.s)return n.s=-n.s,P.plus(n);if(c=P.d,y=n.d,d=S.precision,f=S.rounding,!c[0]||!y[0]){if(y[0])n.s=-n.s;else if(c[0])n=new S(P);else return new S(f===3?-0:0);return K?j(n,d,f):n}if(t=zt(n.e/U),p=zt(P.e/U),c=c.slice(),s=p-t,s){for(_=s<0,_?(e=c,s=-s,u=y.length):(e=y,t=p,u=c.length),r=Math.max(Math.ceil(d/U),u)+2,s>r&&(s=r,e.length=1),e.reverse(),r=s;r--;)e.push(0);e.reverse()}else{for(r=c.length,u=y.length,_=r<u,_&&(u=r),r=0;r<u;r++)if(c[r]!=y[r]){_=c[r]<y[r];break}s=0}for(_&&(e=c,c=y,y=e,n.s=-n.s),u=c.length,r=y.length-u;r>0;--r)c[u++]=0;for(r=y.length;r>s;){if(c[--r]<y[r]){for(i=r;i&&c[--i]===0;)c[i]=ie-1;--c[i],c[r]+=ie}c[r]-=y[r]}for(;c[--u]===0;)c.pop();for(;c[0]===0;c.shift())--t;return c[0]?(n.d=c,n.e=Ze(c,t),K?j(n,d,f):n):new S(f===3?-0:0)};E.modulo=E.mod=function(n){var e,t=this,r=t.constructor;return n=new r(n),!t.d||!n.s||n.d&&!n.d[0]?new r(NaN):!n.d||t.d&&!t.d[0]?j(new r(t),r.precision,r.rounding):(K=!1,r.modulo==9?(e=rt(t,n.abs(),0,3,1),e.s*=n.s):e=rt(t,n,0,r.modulo,1),e=e.times(n),K=!0,t.minus(e))};E.naturalExponential=E.exp=function(){return Si(this)};E.naturalLogarithm=E.ln=function(){return de(this)};E.negated=E.neg=function(){var n=new this.constructor(this);return n.s=-n.s,j(n)};E.plus=E.add=function(n){var e,t,r,i,s,u,d,f,c,p,_=this,y=_.constructor;if(n=new y(n),!_.d||!n.d)return!_.s||!n.s?n=new y(NaN):_.d||(n=new y(n.d||_.s===n.s?_:NaN)),n;if(_.s!=n.s)return n.s=-n.s,_.minus(n);if(c=_.d,p=n.d,d=y.precision,f=y.rounding,!c[0]||!p[0])return p[0]||(n=new y(_)),K?j(n,d,f):n;if(s=zt(_.e/U),r=zt(n.e/U),c=c.slice(),i=s-r,i){for(i<0?(t=c,i=-i,u=p.length):(t=p,r=s,u=c.length),s=Math.ceil(d/U),u=s>u?s+1:u+1,i>u&&(i=u,t.length=1),t.reverse();i--;)t.push(0);t.reverse()}for(u=c.length,i=p.length,u-i<0&&(i=u,t=p,p=c,c=t),e=0;i;)e=(c[--i]=c[i]+p[i]+e)/ie|0,c[i]%=ie;for(e&&(c.unshift(e),++r),u=c.length;c[--u]==0;)c.pop();return n.d=c,n.e=Ze(c,r),K?j(n,d,f):n};E.precision=E.sd=function(n){var e,t=this;if(n!==void 0&&n!==!!n&&n!==1&&n!==0)throw Error(fe+n);return t.d?(e=_r(t.d),n&&t.e+1>e&&(e=t.e+1)):e=NaN,e};E.round=function(){var n=this,e=n.constructor;return j(new e(n),n.e+1,e.rounding)};E.sine=E.sin=function(){var n,e,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,e=r.rounding,r.precision=n+Math.max(t.e,t.sd())+U,r.rounding=1,t=$r(r,vr(r,t)),r.precision=n,r.rounding=e,j(se>2?t.neg():t,n,e,!0)):new r(NaN)};E.squareRoot=E.sqrt=function(){var n,e,t,r,i,s,u=this,d=u.d,f=u.e,c=u.s,p=u.constructor;if(c!==1||!d||!d[0])return new p(!c||c<0&&(!d||d[0])?NaN:d?u:1/0);for(K=!1,c=Math.sqrt(+u),c==0||c==1/0?(e=Qt(d),(e.length+f)%2==0&&(e+="0"),c=Math.sqrt(e),f=zt((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),r=new p(e)):r=new p(c.toString()),t=(f=p.precision)+3;;)if(s=r,r=s.plus(rt(u,s,t+2,1)).times(.5),Qt(s.d).slice(0,t)===(e=Qt(r.d)).slice(0,t))if(e=e.slice(t-3,t+1),e=="9999"||!i&&e=="4999"){if(!i&&(j(s,f+1,0),s.times(s).eq(u))){r=s;break}t+=4,i=1}else{(!+e||!+e.slice(1)&&e.charAt(0)=="5")&&(j(r,f+1,1),n=!r.times(r).eq(u));break}return K=!0,j(r,f,p.rounding,n)};E.tangent=E.tan=function(){var n,e,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,e=r.rounding,r.precision=n+10,r.rounding=1,t=t.sin(),t.s=1,t=rt(t,new r(1).minus(t.times(t)).sqrt(),n+10,0),r.precision=n,r.rounding=e,j(se==2||se==4?t.neg():t,n,e,!0)):new r(NaN)};E.times=E.mul=function(n){var e,t,r,i,s,u,d,f,c,p=this,_=p.constructor,y=p.d,P=(n=new _(n)).d;if(n.s*=p.s,!y||!y[0]||!P||!P[0])return new _(!n.s||y&&!y[0]&&!P||P&&!P[0]&&!y?NaN:!y||!P?n.s/0:n.s*0);for(t=zt(p.e/U)+zt(n.e/U),f=y.length,c=P.length,f<c&&(s=y,y=P,P=s,u=f,f=c,c=u),s=[],u=f+c,r=u;r--;)s.push(0);for(r=c;--r>=0;){for(e=0,i=f+r;i>r;)d=s[i]+P[r]*y[i-r-1]+e,s[i--]=d%ie|0,e=d/ie|0;s[i]=(s[i]+e)%ie|0}for(;!s[--u];)s.pop();return e?++t:s.shift(),n.d=s,n.e=Ze(s,t),K?j(n,_.precision,_.rounding):n};E.toBinary=function(n,e){return Bi(this,2,n,e)};E.toDecimalPlaces=E.toDP=function(n,e){var t=this,r=t.constructor;return t=new r(t),n===void 0?t:(Xt(n,0,ce),e===void 0?e=r.rounding:Xt(e,0,8),j(t,n+t.e+1,e))};E.toExponential=function(n,e){var t,r=this,i=r.constructor;return n===void 0?t=oe(r,!0):(Xt(n,0,ce),e===void 0?e=i.rounding:Xt(e,0,8),r=j(new i(r),n+1,e),t=oe(r,!0,n+1)),r.isNeg()&&!r.isZero()?"-"+t:t};E.toFixed=function(n,e){var t,r,i=this,s=i.constructor;return n===void 0?t=oe(i):(Xt(n,0,ce),e===void 0?e=s.rounding:Xt(e,0,8),r=j(new s(i),n+i.e+1,e),t=oe(r,!1,n+r.e+1)),i.isNeg()&&!i.isZero()?"-"+t:t};E.toFraction=function(n){var e,t,r,i,s,u,d,f,c,p,_,y,P=this,S=P.d,B=P.constructor;if(!S)return new B(P);if(c=t=new B(1),r=f=new B(0),e=new B(r),s=e.e=_r(S)-P.e-1,u=s%U,e.d[0]=Ht(10,u<0?U+u:u),n==null)n=s>0?e:c;else{if(d=new B(n),!d.isInt()||d.lt(c))throw Error(fe+d);n=d.gt(e)?s>0?e:c:d}for(K=!1,d=new B(Qt(S)),p=B.precision,B.precision=s=S.length*U*2;_=rt(d,e,0,1,1),i=t.plus(_.times(r)),i.cmp(n)!=1;)t=r,r=i,i=c,c=f.plus(_.times(i)),f=i,i=e,e=d.minus(_.times(i)),d=i;return i=rt(n.minus(t),r,0,1,1),f=f.plus(i.times(c)),t=t.plus(i.times(r)),f.s=c.s=P.s,y=rt(c,r,s,1).minus(P).abs().cmp(rt(f,t,s,1).minus(P).abs())<1?[c,r]:[f,t],B.precision=p,K=!0,y};E.toHexadecimal=E.toHex=function(n,e){return Bi(this,16,n,e)};E.toNearest=function(n,e){var t=this,r=t.constructor;if(t=new r(t),n==null){if(!t.d)return t;n=new r(1),e=r.rounding}else{if(n=new r(n),e===void 0?e=r.rounding:Xt(e,0,8),!t.d)return n.s?t:n;if(!n.d)return n.s&&(n.s=t.s),n}return n.d[0]?(K=!1,t=rt(t,n,0,e,1).times(n),K=!0,j(t)):(n.s=t.s,t=n),t};E.toNumber=function(){return+this};E.toOctal=function(n,e){return Bi(this,8,n,e)};E.toPower=E.pow=function(n){var e,t,r,i,s,u,d=this,f=d.constructor,c=+(n=new f(n));if(!d.d||!n.d||!d.d[0]||!n.d[0])return new f(Ht(+d,c));if(d=new f(d),d.eq(1))return d;if(r=f.precision,s=f.rounding,n.eq(1))return j(d,r,s);if(e=zt(n.e/U),e>=n.d.length-1&&(t=c<0?-c:c)<=Ur)return i=gr(f,d,t,r),n.s<0?new f(1).div(i):j(i,r,s);if(u=d.s,u<0){if(e<n.d.length-1)return new f(NaN);if((n.d[e]&1)==0&&(u=1),d.e==0&&d.d[0]==1&&d.d.length==1)return d.s=u,d}return t=Ht(+d,c),e=t==0||!isFinite(t)?zt(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?u/0:0):(K=!1,f.rounding=d.s=1,t=Math.min(12,(e+"").length),i=Si(n.times(de(d,r+t)),r),i.d&&(i=j(i,r+5,1),Ae(i.d,r,s)&&(e=r+10,i=j(Si(n.times(de(d,e+t)),e),e+5,1),+Qt(i.d).slice(r+1,r+15)+1==1e14&&(i=j(i,r+1,0)))),i.s=u,K=!0,f.rounding=s,j(i,r,s))};E.toPrecision=function(n,e){var t,r=this,i=r.constructor;return n===void 0?t=oe(r,r.e<=i.toExpNeg||r.e>=i.toExpPos):(Xt(n,1,ce),e===void 0?e=i.rounding:Xt(e,0,8),r=j(new i(r),n,e),t=oe(r,n<=r.e||r.e<=i.toExpNeg,n)),r.isNeg()&&!r.isZero()?"-"+t:t};E.toSignificantDigits=E.toSD=function(n,e){var t=this,r=t.constructor;return n===void 0?(n=r.precision,e=r.rounding):(Xt(n,1,ce),e===void 0?e=r.rounding:Xt(e,0,8)),j(new r(t),n,e)};E.toString=function(){var n=this,e=n.constructor,t=oe(n,n.e<=e.toExpNeg||n.e>=e.toExpPos);return n.isNeg()&&!n.isZero()?"-"+t:t};E.truncated=E.trunc=function(){return j(new this.constructor(this),this.e+1,1)};E.valueOf=E.toJSON=function(){var n=this,e=n.constructor,t=oe(n,n.e<=e.toExpNeg||n.e>=e.toExpPos);return n.isNeg()?"-"+t:t};function Qt(n){var e,t,r,i=n.length-1,s="",u=n[0];if(i>0){for(s+=u,e=1;e<i;e++)r=n[e]+"",t=U-r.length,t&&(s+=ue(t)),s+=r;u=n[e],r=u+"",t=U-r.length,t&&(s+=ue(t))}else if(u===0)return"0";for(;u%10===0;)u/=10;return s+u}function Xt(n,e,t){if(n!==~~n||n<e||n>t)throw Error(fe+n)}function Ae(n,e,t,r){var i,s,u,d;for(s=n[0];s>=10;s/=10)--e;return--e<0?(e+=U,i=0):(i=Math.ceil((e+1)/U),e%=U),s=Ht(10,U-e),d=n[i]%s|0,r==null?e<3?(e==0?d=d/100|0:e==1&&(d=d/10|0),u=t<4&&d==99999||t>3&&d==49999||d==5e4||d==0):u=(t<4&&d+1==s||t>3&&d+1==s/2)&&(n[i+1]/s/100|0)==Ht(10,e-2)-1||(d==s/2||d==0)&&(n[i+1]/s/100|0)==0:e<4?(e==0?d=d/1e3|0:e==1?d=d/100|0:e==2&&(d=d/10|0),u=(r||t<4)&&d==9999||!r&&t>3&&d==4999):u=((r||t<4)&&d+1==s||!r&&t>3&&d+1==s/2)&&(n[i+1]/s/1e3|0)==Ht(10,e-3)-1,u}function Fe(n,e,t){for(var r,i=[0],s,u=0,d=n.length;u<d;){for(s=i.length;s--;)i[s]*=e;for(i[0]+=Mi.indexOf(n.charAt(u++)),r=0;r<i.length;r++)i[r]>t-1&&(i[r+1]===void 0&&(i[r+1]=0),i[r+1]+=i[r]/t|0,i[r]%=t)}return i.reverse()}function Qr(n,e){var t,r,i;if(e.isZero())return e;r=e.d.length,r<32?(t=Math.ceil(r/3),i=(1/Ue(4,t)).toString()):(t=16,i="2.3283064365386962890625e-10"),n.precision+=t,e=be(n,1,e.times(i),new n(1));for(var s=t;s--;){var u=e.times(e);e=u.times(u).minus(u).times(8).plus(1)}return n.precision-=t,e}var rt=(function(){function n(r,i,s){var u,d=0,f=r.length;for(r=r.slice();f--;)u=r[f]*i+d,r[f]=u%s|0,d=u/s|0;return d&&r.unshift(d),r}function e(r,i,s,u){var d,f;if(s!=u)f=s>u?1:-1;else for(d=f=0;d<s;d++)if(r[d]!=i[d]){f=r[d]>i[d]?1:-1;break}return f}function t(r,i,s,u){for(var d=0;s--;)r[s]-=d,d=r[s]<i[s]?1:0,r[s]=d*u+r[s]-i[s];for(;!r[0]&&r.length>1;)r.shift()}return function(r,i,s,u,d,f){var c,p,_,y,P,S,B,k,x,R,T,I,G,H,Q,tt,$t,Vt,J,st,v=r.constructor,o=r.s==i.s?1:-1,l=r.d,h=i.d;if(!l||!l[0]||!h||!h[0])return new v(!r.s||!i.s||(l?h&&l[0]==h[0]:!h)?NaN:l&&l[0]==0||!h?o*0:o/0);for(f?(P=1,p=r.e-i.e):(f=ie,P=U,p=zt(r.e/P)-zt(i.e/P)),J=h.length,$t=l.length,x=new v(o),R=x.d=[],_=0;h[_]==(l[_]||0);_++);if(h[_]>(l[_]||0)&&p--,s==null?(H=s=v.precision,u=v.rounding):d?H=s+(r.e-i.e)+1:H=s,H<0)R.push(1),S=!0;else{if(H=H/P+2|0,_=0,J==1){for(y=0,h=h[0],H++;(_<$t||y)&&H--;_++)Q=y*f+(l[_]||0),R[_]=Q/h|0,y=Q%h|0;S=y||_<$t}else{for(y=f/(h[0]+1)|0,y>1&&(h=n(h,y,f),l=n(l,y,f),J=h.length,$t=l.length),tt=J,T=l.slice(0,J),I=T.length;I<J;)T[I++]=0;st=h.slice(),st.unshift(0),Vt=h[0],h[1]>=f/2&&++Vt;do y=0,c=e(h,T,J,I),c<0?(G=T[0],J!=I&&(G=G*f+(T[1]||0)),y=G/Vt|0,y>1?(y>=f&&(y=f-1),B=n(h,y,f),k=B.length,I=T.length,c=e(B,T,k,I),c==1&&(y--,t(B,J<k?st:h,k,f))):(y==0&&(c=y=1),B=h.slice()),k=B.length,k<I&&B.unshift(0),t(T,B,I,f),c==-1&&(I=T.length,c=e(h,T,J,I),c<1&&(y++,t(T,J<I?st:h,I,f))),I=T.length):c===0&&(y++,T=[0]),R[_++]=y,c&&T[0]?T[I++]=l[tt]||0:(T=[l[tt]],I=1);while((tt++<$t||T[0]!==void 0)&&H--);S=T[0]!==void 0}R[0]||R.shift()}if(P==1)x.e=p,fr=S;else{for(_=1,y=R[0];y>=10;y/=10)_++;x.e=_+p*P-1,j(x,d?s+x.e+1:s,u,S)}return x}})();function j(n,e,t,r){var i,s,u,d,f,c,p,_,y,P=n.constructor;t:if(e!=null){if(_=n.d,!_)return n;for(i=1,d=_[0];d>=10;d/=10)i++;if(s=e-i,s<0)s+=U,u=e,p=_[y=0],f=p/Ht(10,i-u-1)%10|0;else if(y=Math.ceil((s+1)/U),d=_.length,y>=d)if(r){for(;d++<=y;)_.push(0);p=f=0,i=1,s%=U,u=s-U+1}else break t;else{for(p=d=_[y],i=1;d>=10;d/=10)i++;s%=U,u=s-U+i,f=u<0?0:p/Ht(10,i-u-1)%10|0}if(r=r||e<0||_[y+1]!==void 0||(u<0?p:p%Ht(10,i-u-1)),c=t<4?(f||r)&&(t==0||t==(n.s<0?3:2)):f>5||f==5&&(t==4||r||t==6&&(s>0?u>0?p/Ht(10,i-u):0:_[y-1])%10&1||t==(n.s<0?8:7)),e<1||!_[0])return _.length=0,c?(e-=n.e+1,_[0]=Ht(10,(U-e%U)%U),n.e=-e||0):_[0]=n.e=0,n;if(s==0?(_.length=y,d=1,y--):(_.length=y+1,d=Ht(10,U-s),_[y]=u>0?(p/Ht(10,i-u)%Ht(10,u)|0)*d:0),c)for(;;)if(y==0){for(s=1,u=_[0];u>=10;u/=10)s++;for(u=_[0]+=d,d=1;u>=10;u/=10)d++;s!=d&&(n.e++,_[0]==ie&&(_[0]=1));break}else{if(_[y]+=d,_[y]!=ie)break;_[y--]=0,d=1}for(s=_.length;_[--s]===0;)_.pop()}return K&&(n.e>P.maxE?(n.d=null,n.e=NaN):n.e<P.minE&&(n.e=0,n.d=[0])),n}function oe(n,e,t){if(!n.isFinite())return wr(n);var r,i=n.e,s=Qt(n.d),u=s.length;return e?(t&&(r=t-u)>0?s=s.charAt(0)+"."+s.slice(1)+ue(r):u>1&&(s=s.charAt(0)+"."+s.slice(1)),s=s+(n.e<0?"e":"e+")+n.e):i<0?(s="0."+ue(-i-1)+s,t&&(r=t-u)>0&&(s+=ue(r))):i>=u?(s+=ue(i+1-u),t&&(r=t-i-1)>0&&(s=s+"."+ue(r))):((r=i+1)<u&&(s=s.slice(0,r)+"."+s.slice(r)),t&&(r=t-u)>0&&(i+1===u&&(s+="."),s+=ue(r))),s}function Ze(n,e){var t=n[0];for(e*=U;t>=10;t/=10)e++;return e}function je(n,e,t){if(e>Hr)throw K=!0,t&&(n.precision=t),Error(cr);return j(new n(Le),e,1,!0)}function ne(n,e,t){if(e>Ai)throw Error(cr);return j(new n(qe),e,t,!0)}function _r(n){var e=n.length-1,t=e*U+1;if(e=n[e],e){for(;e%10==0;e/=10)t--;for(e=n[0];e>=10;e/=10)t++}return t}function ue(n){for(var e="";n--;)e+="0";return e}function gr(n,e,t,r){var i,s=new n(1),u=Math.ceil(r/U+4);for(K=!1;;){if(t%2&&(s=s.times(e),ur(s.d,u)&&(i=!0)),t=zt(t/2),t===0){t=s.d.length-1,i&&s.d[t]===0&&++s.d[t];break}e=e.times(e),ur(e.d,u)}return K=!0,s}function lr(n){return n.d[n.d.length-1]&1}function br(n,e,t){for(var r,i,s=new n(e[0]),u=0;++u<e.length;){if(i=new n(e[u]),!i.s){s=i;break}r=s.cmp(i),(r===t||r===0&&s.s===t)&&(s=i)}return s}function Si(n,e){var t,r,i,s,u,d,f,c=0,p=0,_=0,y=n.constructor,P=y.rounding,S=y.precision;if(!n.d||!n.d[0]||n.e>17)return new y(n.d?n.d[0]?n.s<0?0:1/0:1:n.s?n.s<0?0:n:NaN);for(e==null?(K=!1,f=S):f=e,d=new y(.03125);n.e>-2;)n=n.times(d),_+=5;for(r=Math.log(Ht(2,_))/Math.LN10*2+5|0,f+=r,t=s=u=new y(1),y.precision=f;;){if(s=j(s.times(n),f,1),t=t.times(++p),d=u.plus(rt(s,t,f,1)),Qt(d.d).slice(0,f)===Qt(u.d).slice(0,f)){for(i=_;i--;)u=j(u.times(u),f,1);if(e==null)if(c<3&&Ae(u.d,f-r,P,c))y.precision=f+=10,t=s=d=new y(1),p=0,c++;else return j(u,y.precision=S,P,K=!0);else return y.precision=S,u}u=d}}function de(n,e){var t,r,i,s,u,d,f,c,p,_,y,P=1,S=10,B=n,k=B.d,x=B.constructor,R=x.rounding,T=x.precision;if(B.s<0||!k||!k[0]||!B.e&&k[0]==1&&k.length==1)return new x(k&&!k[0]?-1/0:B.s!=1?NaN:k?0:B);if(e==null?(K=!1,p=T):p=e,x.precision=p+=S,t=Qt(k),r=t.charAt(0),Math.abs(s=B.e)<15e14){for(;r<7&&r!=1||r==1&&t.charAt(1)>3;)B=B.times(n),t=Qt(B.d),r=t.charAt(0),P++;s=B.e,r>1?(B=new x("0."+t),s++):B=new x(r+"."+t.slice(1))}else return c=je(x,p+2,T).times(s+""),B=de(new x(r+"."+t.slice(1)),p-S).plus(c),x.precision=T,e==null?j(B,T,R,K=!0):B;for(_=B,f=u=B=rt(B.minus(1),B.plus(1),p,1),y=j(B.times(B),p,1),i=3;;){if(u=j(u.times(y),p,1),c=f.plus(rt(u,new x(i),p,1)),Qt(c.d).slice(0,p)===Qt(f.d).slice(0,p))if(f=f.times(2),s!==0&&(f=f.plus(je(x,p+2,T).times(s+""))),f=rt(f,new x(P),p,1),e==null)if(Ae(f.d,p-S,R,d))x.precision=p+=S,c=u=B=rt(_.minus(1),_.plus(1),p,1),y=j(B.times(B),p,1),i=d=1;else return j(f,x.precision=T,R,K=!0);else return x.precision=T,f;f=c,i+=2}}function wr(n){return String(n.s*n.s/0)}function Ne(n,e){var t,r,i;for((t=e.indexOf("."))>-1&&(e=e.replace(".","")),(r=e.search(/e/i))>0?(t<0&&(t=r),t+=+e.slice(r+1),e=e.substring(0,r)):t<0&&(t=e.length),r=0;e.charCodeAt(r)===48;r++);for(i=e.length;e.charCodeAt(i-1)===48;--i);if(e=e.slice(r,i),e){if(i-=r,n.e=t=t-r-1,n.d=[],r=(t+1)%U,t<0&&(r+=U),r<i){for(r&&n.d.push(+e.slice(0,r)),i-=U;r<i;)n.d.push(+e.slice(r,r+=U));e=e.slice(r),r=U-e.length}else r-=i;for(;r--;)e+="0";n.d.push(+e),K&&(n.e>n.constructor.maxE?(n.d=null,n.e=NaN):n.e<n.constructor.minE&&(n.e=0,n.d=[0]))}else n.e=0,n.d=[0];return n}function Kr(n,e){var t,r,i,s,u,d,f,c,p;if(e.indexOf("_")>-1){if(e=e.replace(/(\d)_(?=\d)/g,"$1"),mr.test(e))return Ne(n,e)}else if(e==="Infinity"||e==="NaN")return+e||(n.s=NaN),n.e=NaN,n.d=null,n;if(Wr.test(e))t=16,e=e.toLowerCase();else if(jr.test(e))t=2;else if(Zr.test(e))t=8;else throw Error(fe+e);for(s=e.search(/p/i),s>0?(f=+e.slice(s+1),e=e.substring(2,s)):e=e.slice(2),s=e.indexOf("."),u=s>=0,r=n.constructor,u&&(e=e.replace(".",""),d=e.length,s=d-s,i=gr(r,new r(t),s,s*2)),c=Fe(e,t,ie),p=c.length-1,s=p;c[s]===0;--s)c.pop();return s<0?new r(n.s*0):(n.e=Ze(c,p),n.d=c,K=!1,u&&(n=rt(n,i,d*4)),f&&(n=n.times(Math.abs(f)<54?Ht(2,f):Se.pow(2,f))),K=!0,n)}function $r(n,e){var t,r=e.d.length;if(r<3)return e.isZero()?e:be(n,2,e,e);t=1.4*Math.sqrt(r),t=t>16?16:t|0,e=e.times(1/Ue(5,t)),e=be(n,2,e,e);for(var i,s=new n(5),u=new n(16),d=new n(20);t--;)i=e.times(e),e=e.times(s.plus(i.times(u.times(i).minus(d))));return e}function be(n,e,t,r,i){var s,u,d,f,c=1,p=n.precision,_=Math.ceil(p/U);for(K=!1,f=t.times(t),d=new n(r);;){if(u=rt(d.times(f),new n(e++*e++),p,1),d=i?r.plus(u):r.minus(u),r=rt(u.times(f),new n(e++*e++),p,1),u=d.plus(r),u.d[_]!==void 0){for(s=_;u.d[s]===d.d[s]&&s--;);if(s==-1)break}s=d,d=r,r=u,u=s,c++}return K=!0,u.d.length=_+1,u}function Ue(n,e){for(var t=n;--e;)t*=n;return t}function vr(n,e){var t,r=e.s<0,i=ne(n,n.precision,1),s=i.times(.5);if(e=e.abs(),e.lte(s))return se=r?4:1,e;if(t=e.divToInt(i),t.isZero())se=r?3:2;else{if(e=e.minus(t.times(i)),e.lte(s))return se=lr(t)?r?2:3:r?4:1,e;se=lr(t)?r?1:4:r?3:2}return e.minus(i).abs()}function Bi(n,e,t,r){var i,s,u,d,f,c,p,_,y,P=n.constructor,S=t!==void 0;if(S?(Xt(t,1,ce),r===void 0?r=P.rounding:Xt(r,0,8)):(t=P.precision,r=P.rounding),!n.isFinite())p=wr(n);else{for(p=oe(n),u=p.indexOf("."),S?(i=2,e==16?t=t*4-3:e==8&&(t=t*3-2)):i=e,u>=0&&(p=p.replace(".",""),y=new P(1),y.e=p.length-u,y.d=Fe(oe(y),10,i),y.e=y.d.length),_=Fe(p,10,i),s=f=_.length;_[--f]==0;)_.pop();if(!_[0])p=S?"0p+0":"0";else{if(u<0?s--:(n=new P(n),n.d=_,n.e=s,n=rt(n,y,t,r,0,i),_=n.d,s=n.e,c=fr),u=_[t],d=i/2,c=c||_[t+1]!==void 0,c=r<4?(u!==void 0||c)&&(r===0||r===(n.s<0?3:2)):u>d||u===d&&(r===4||c||r===6&&_[t-1]&1||r===(n.s<0?8:7)),_.length=t,c)for(;++_[--t]>i-1;)_[t]=0,t||(++s,_.unshift(1));for(f=_.length;!_[f-1];--f);for(u=0,p="";u<f;u++)p+=Mi.charAt(_[u]);if(S){if(f>1)if(e==16||e==8){for(u=e==16?4:3,--f;f%u;f++)p+="0";for(_=Fe(p,i,e),f=_.length;!_[f-1];--f);for(u=1,p="1.";u<f;u++)p+=Mi.charAt(_[u])}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>f)for(s-=f;s--;)p+="0";else s<f&&(p=p.slice(0,s)+"."+p.slice(s))}p=(e==16?"0x":e==2?"0b":e==8?"0o":"")+p}return n.s<0?"-"+p:p}function ur(n,e){if(n.length>e)return n.length=e,!0}function zr(n){return new this(n).abs()}function Gr(n){return new this(n).acos()}function Vr(n){return new this(n).acosh()}function Xr(n,e){return new this(n).plus(e)}function Jr(n){return new this(n).asin()}function Yr(n){return new this(n).asinh()}function tn(n){return new this(n).atan()}function en(n){return new this(n).atanh()}function rn(n,e){n=new this(n),e=new this(e);var t,r=this.precision,i=this.rounding,s=r+4;return!n.s||!e.s?t=new this(NaN):!n.d&&!e.d?(t=ne(this,s,1).times(e.s>0?.25:.75),t.s=n.s):!e.d||n.isZero()?(t=e.s<0?ne(this,r,i):new this(0),t.s=n.s):!n.d||e.isZero()?(t=ne(this,s,1).times(.5),t.s=n.s):e.s<0?(this.precision=s,this.rounding=1,t=this.atan(rt(n,e,s,1)),e=ne(this,s,1),this.precision=r,this.rounding=i,t=n.s<0?t.minus(e):t.plus(e)):t=this.atan(rt(n,e,s,1)),t}function nn(n){return new this(n).cbrt()}function on(n){return j(n=new this(n),n.e+1,2)}function sn(n,e,t){return new this(n).clamp(e,t)}function an(n){if(!n||typeof n!="object")throw Error(We+"Object expected");var e,t,r,i=n.defaults===!0,s=["precision",1,ce,"rounding",0,8,"toExpNeg",-ge,0,"toExpPos",0,ge,"maxE",0,ge,"minE",-ge,0,"modulo",0,9];for(e=0;e<s.length;e+=3)if(t=s[e],i&&(this[t]=Pi[t]),(r=n[t])!==void 0)if(zt(r)===r&&r>=s[e+1]&&r<=s[e+2])this[t]=r;else throw Error(fe+t+": "+r);if(t="crypto",i&&(this[t]=Pi[t]),(r=n[t])!==void 0)if(r===!0||r===!1||r===0||r===1)if(r)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[t]=!0;else throw Error(hr);else this[t]=!1;else throw Error(fe+t+": "+r);return this}function ln(n){return new this(n).cos()}function un(n){return new this(n).cosh()}function yr(n){var e,t,r;function i(s){var u,d,f,c=this;if(!(c instanceof i))return new i(s);if(c.constructor=i,dr(s)){c.s=s.s,K?!s.d||s.e>i.maxE?(c.e=NaN,c.d=null):s.e<i.minE?(c.e=0,c.d=[0]):(c.e=s.e,c.d=s.d.slice()):(c.e=s.e,c.d=s.d?s.d.slice():s.d);return}if(f=typeof s,f==="number"){if(s===0){c.s=1/s<0?-1:1,c.e=0,c.d=[0];return}if(s<0?(s=-s,c.s=-1):c.s=1,s===~~s&&s<1e7){for(u=0,d=s;d>=10;d/=10)u++;K?u>i.maxE?(c.e=NaN,c.d=null):u<i.minE?(c.e=0,c.d=[0]):(c.e=u,c.d=[s]):(c.e=u,c.d=[s]);return}if(s*0!==0){s||(c.s=NaN),c.e=NaN,c.d=null;return}return Ne(c,s.toString())}if(f==="string")return(d=s.charCodeAt(0))===45?(s=s.slice(1),c.s=-1):(d===43&&(s=s.slice(1)),c.s=1),mr.test(s)?Ne(c,s):Kr(c,s);if(f==="bigint")return s<0?(s=-s,c.s=-1):c.s=1,Ne(c,s.toString());throw Error(fe+s)}if(i.prototype=E,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=an,i.clone=yr,i.isDecimal=dr,i.abs=zr,i.acos=Gr,i.acosh=Vr,i.add=Xr,i.asin=Jr,i.asinh=Yr,i.atan=tn,i.atanh=en,i.atan2=rn,i.cbrt=nn,i.ceil=on,i.clamp=sn,i.cos=ln,i.cosh=un,i.div=dn,i.exp=fn,i.floor=cn,i.hypot=hn,i.ln=pn,i.log=mn,i.log10=gn,i.log2=_n,i.max=bn,i.min=wn,i.mod=vn,i.mul=yn,i.pow=Mn,i.random=Pn,i.round=An,i.sign=Sn,i.sin=Bn,i.sinh=En,i.sqrt=On,i.sub=In,i.sum=Cn,i.tan=Tn,i.tanh=kn,i.trunc=xn,n===void 0&&(n={}),n&&n.defaults!==!0)for(r=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],e=0;e<r.length;)n.hasOwnProperty(t=r[e++])||(n[t]=this[t]);return i.config(n),i}function dn(n,e){return new this(n).div(e)}function fn(n){return new this(n).exp()}function cn(n){return j(n=new this(n),n.e+1,3)}function hn(){var n,e,t=new this(0);for(K=!1,n=0;n<arguments.length;)if(e=new this(arguments[n++]),e.d)t.d&&(t=t.plus(e.times(e)));else{if(e.s)return K=!0,new this(1/0);t=e}return K=!0,t.sqrt()}function dr(n){return n instanceof Se||n&&n.toStringTag===pr||!1}function pn(n){return new this(n).ln()}function mn(n,e){return new this(n).log(e)}function _n(n){return new this(n).log(2)}function gn(n){return new this(n).log(10)}function bn(){return br(this,arguments,-1)}function wn(){return br(this,arguments,1)}function vn(n,e){return new this(n).mod(e)}function yn(n,e){return new this(n).mul(e)}function Mn(n,e){return new this(n).pow(e)}function Pn(n){var e,t,r,i,s=0,u=new this(1),d=[];if(n===void 0?n=this.precision:Xt(n,1,ce),r=Math.ceil(n/U),this.crypto)if(crypto.getRandomValues)for(e=crypto.getRandomValues(new Uint32Array(r));s<r;)i=e[s],i>=429e7?e[s]=crypto.getRandomValues(new Uint32Array(1))[0]:d[s++]=i%1e7;else if(crypto.randomBytes){for(e=crypto.randomBytes(r*=4);s<r;)i=e[s]+(e[s+1]<<8)+(e[s+2]<<16)+((e[s+3]&127)<<24),i>=214e7?crypto.randomBytes(4).copy(e,s):(d.push(i%1e7),s+=4);s=r/4}else throw Error(hr);else for(;s<r;)d[s++]=Math.random()*1e7|0;for(r=d[--s],n%=U,r&&n&&(i=Ht(10,U-n),d[s]=(r/i|0)*i);d[s]===0;s--)d.pop();if(s<0)t=0,d=[0];else{for(t=-1;d[0]===0;t-=U)d.shift();for(r=1,i=d[0];i>=10;i/=10)r++;r<U&&(t-=U-r)}return u.e=t,u.d=d,u}function An(n){return j(n=new this(n),n.e+1,this.rounding)}function Sn(n){return n=new this(n),n.d?n.d[0]?n.s:0*n.s:n.s||NaN}function Bn(n){return new this(n).sin()}function En(n){return new this(n).sinh()}function On(n){return new this(n).sqrt()}function In(n,e){return new this(n).sub(e)}function Cn(){var n=0,e=arguments,t=new this(e[n]);for(K=!1;t.s&&++n<e.length;)t=t.plus(e[n]);return K=!0,j(t,this.precision,this.rounding)}function Tn(n){return new this(n).tan()}function kn(n){return new this(n).tanh()}function xn(n){return j(n=new this(n),n.e+1,1)}E[Symbol.for("nodejs.util.inspect.custom")]=E.toString;E[Symbol.toStringTag]="Decimal";var Se=E.constructor=yr(Pi);Le=new Se(Le);qe=new Se(qe);var L=Se;var Be=(r=>(r[r.Spot=0]="Spot",r[r.Curve=1]="Curve",r[r.BidAsk=2]="BidAsk",r))(Be||{});var Z=require("@cetusprotocol/common-sdk");var ae=1e3,Rn=-443636,Dn=443636,we=1e4,ve=2e3,me=200,_e=443636n,He=1e8,Ei=1e9,Yt=1e4,Fn=604800,Nn=1747627200;var et=wi(De());var Ln=new et.default(524288),D=64,Qe=new et.default(1).shln(D),Mr=new et.default(2).pow(new et.default(128)).sub(new et.default(1)),F=class n{static splitBinLiquidityInfo(e,t,r){let i=n.getPositionCount(t,r);if(i<=1)return[e];let s=[],u=t;for(let d=0;d<i;d++){let f=Math.min(u+1e3-2,r),c=e.bins.filter(p=>p.bin_id>=u&&p.bin_id<=f);s.push({bins:c,amount_a:c.reduce((p,_)=>(0,Z.d)(p).plus(_.amount_a),(0,Z.d)(0)).toFixed(0),amount_b:c.reduce((p,_)=>(0,Z.d)(p).plus(_.amount_b),(0,Z.d)(0)).toFixed(0)}),u=f+1}return s}static processBinsByRate(e,t){let r=[],i=(0,Z.d)(0),s=(0,Z.d)(0),u=!1;return e.forEach(d=>{let{amount_a:f,amount_b:c,liquidity:p="0"}=d,_=(0,Z.d)(t).mul(p).toFixed(0),y=(0,Z.d)(f).mul(t),P=(0,Z.d)(c).mul(t);i=(0,Z.d)(i).plus(y),s=(0,Z.d)(s).plus(P),((0,Z.d)(y).lt(1)&&(0,Z.d)(y).gt(0)||(0,Z.d)(P).lt(1)&&(0,Z.d)(P).gt(0))&&(u=!0),r.push({bin_id:d.bin_id,amount_a:y.toFixed(0),amount_b:P.toFixed(0),price_per_lamport:d.price_per_lamport,liquidity:_})}),{bins:{bins:r,amount_a:i.toFixed(0),amount_b:s.toFixed(0)},has_invalid_amount:u}}static calculateOutByShare(e,t){let{amount_a:r,amount_b:i,liquidity:s="0"}=e;if(s==="0")return{amount_a:"0",amount_b:"0"};if((0,Z.d)(t).gte((0,Z.d)(s)))return{amount_a:r,amount_b:i};let u=(0,Z.d)(t).div(s).mul(r).toFixed(0,L.ROUND_FLOOR),d=(0,Z.d)(t).div(s).mul(i).toFixed(0,L.ROUND_FLOOR);return{amount_a:u,amount_b:d}}static getPositionCount(e,t){let i=(0,Z.d)(t).sub(e).add(1).div(1e3);return Number(i.toFixed(0,L.ROUND_UP))}static getLiquidity(e,t,r){return(0,Z.d)(r).mul(e).add((0,Z.d)(t).mul((0,Z.d)(2).pow(D))).toFixed(0)}static getAmountAFromLiquidity(e,t){return(0,Z.d)(e).div((0,Z.d)(t)).toFixed(0)}static getAmountBFromLiquidity(e){return(0,Z.d)(e).div((0,Z.d)(2).pow(D)).toFixed(0)}static getAmountsFromLiquidity(e,t,r,i){if((0,Z.d)(i).isZero()&&$("LiquiditySupplyIsZero","Liquidity supply is zero"),(0,Z.d)(r).gt((0,Z.d)(i))&&$("InvalidDeltaLiquidity","Invalid delta liquidity"),(0,Z.d)(r).isZero())return["0","0"];let s;(0,Z.d)(e).isZero()?s="0":s=(0,Z.d)(e).mul((0,Z.d)(r)).div((0,Z.d)(i)).toFixed(0,L.ROUND_FLOOR);let u;return(0,Z.d)(t).isZero()?u="0":u=(0,Z.d)(t).mul((0,Z.d)(r)).div((0,Z.d)(i)).toFixed(0,L.ROUND_FLOOR),[s,u]}static getPriceFromBinId(e,t,r,i){let s=n.getPricePerLamportFromBinId(e,t);return n.getPriceFromLamport(r,i,s).toString()}static getPricePerLamportFromBinId(e,t){let r=new L(t).div(new L(1e4));return new L(1).add(new L(r)).pow(new L(e)).toString()}static getBinIdFromPrice(e,t,r,i,s){let u=n.getPricePerLamport(i,s,e);return n.getBinIdFromLamportPrice(u,t,r)}static getBinIdFromLamportPrice(e,t,r){let i=new L(t).div(new L(1e4)),s=new L(e).log().dividedBy(new L(1).add(i).log());return(r?s.floor():s.ceil()).toNumber()}static getPricePerLamport(e,t,r){return new L(r).mul(new L(10**(t-e))).toString()}static getPriceFromLamport(e,t,r){return new L(r).div(new L(10**(t-e))).toString()}static getReversePrice(e){return new L(1).div(e).toString()}static getQPriceFromId(e,t){let r=new et.default(t).shln(D).div(new et.default(1e4)),i=Qe.add(r);return n.pow(i,new et.default(e)).toString()}static getPricePerLamportFromQPrice(e){return Z.MathUtil.fromX64(new et.default(e)).toString()}static pow(e,t){let r=t.isNeg();if(t.isZero())return Qe;if(t=r?t.abs():t,t.gt(Ln))return new et.default(0);let i=e,s=Qe;return i.gte(s)&&(i=Mr.div(i),r=!r),t.and(new et.default(1)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(2)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(4)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(8)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(16)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(32)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(64)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(128)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(256)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(512)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(1024)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(2048)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(4096)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(8192)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(16384)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(32768)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(65536)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(131072)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new et.default(262144)).isZero()||(s=s.mul(i).shrn(D)),s.isZero()?new et.default(0):(r&&(s=Mr.div(s)),s)}static binScore(e){let t=BigInt(e)+_e;return(t<0n||t>_e*2n)&&$("InvalidBinId",new Error("Invalid bin ID"),{[Z.DETAILS_KEYS.METHOD_NAME]:"binScore",[Z.DETAILS_KEYS.REQUEST_PARAMS]:{binId:e}}),t.toString()}static scoreToBinId(e){let t=BigInt(e)-_e;return(t<-_e||t>_e)&&$("InvalidBinId",new Error("Invalid score"),{[Z.DETAILS_KEYS.METHOD_NAME]:"scoreToBinId",[Z.DETAILS_KEYS.REQUEST_PARAMS]:{score:e}}),Number(t)}static resolveBinPosition(e){let t=BigInt(e),r=t>>4n,i=Number(t&0xfn);return[r.toString(),i]}static findMinMaxBinId(e){let t=1+e/1e4,i=new L("18446744073709551615").log(10).div(new L(t).log(10)).floor(),s=i.neg(),u=i,d=(0,Z.d)(1),f=(0,Z.d)("340282366920938463463374607431768211455");for(;;){let c=(0,Z.d)(n.getQPriceFromId(s.toNumber(),e));if(c.gt(d)&&!c.isZero())break;s=s.add(1)}for(;;){let c=(0,Z.d)(n.getQPriceFromId(u.toNumber(),e));if(c.lt(f)&&!c.isZero())break;u=u.sub(1)}return{minBinId:s.toNumber(),maxBinId:u.toNumber()}}static getBinShift(e,t,r){let i=n.getPricePerLamportFromBinId(e,t),s=(0,Z.d)(i).mul(1+r).toString(),u=n.getBinIdFromLamportPrice(s,t,!0),d=(0,Z.d)(u).sub(e).abs().toFixed(0,L.ROUND_UP);return console.log("getBinShift Options:",{active_id:e,bin_shift:d}),Number(d)}};var Gt=require("@mysten/sui/bcs"),Ar=require("blakejs");function Oi(n){try{let e=n.parsedJson;return{id:e.pool_id,bin_step:Number(e.bin_step),coin_type_a:(0,z.fixCoinType)(e.coin_type_a,!1),coin_type_b:(0,z.fixCoinType)(e.coin_type_b,!1)}}catch(e){return $("ParseError",e,{[z.DETAILS_KEYS.METHOD_NAME]:"parseDlmmBasePool",[z.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function Ee(n){try{let e=(0,z.getObjectFields)(n),t=(0,z.getObjectType)(n),r=(0,z.extractStructTagFromType)(t),i={bin_step:e.bin_manager.fields.bin_step,bin_manager_handle:e.bin_manager.fields.bins.fields.id.id,size:e.bin_manager.fields.bins.fields.size},s={bin_step:e.position_manager.fields.bin_step,position_index:e.position_manager.fields.position_index,position_handle:e.position_manager.fields.positions.fields.id.id,size:e.position_manager.fields.positions.fields.size},u=e.reward_manager.fields,d=u.rewards.map(_=>{let y=_.fields.current_emission_rate,P=z.MathUtil.fromX64(new Pr.default(y)),S=Math.floor(P.toNumber()*60*60*24).toString();return{reward_coin:(0,z.fixCoinType)(_.fields.reward_coin.fields.name,!1),emissions_per_second:P.toString(),emissions_per_day:S,period_emission_rates:{id:_.fields.period_emission_rates.fields.id.id,size:_.fields.period_emission_rates.fields.size}}}),f={is_public:u.is_public,emergency_reward_pause:u.emergency_reward_pause,vault:{id:u.vault.fields.id.id,size:u.vault.fields.size},rewards:d,last_updated_time:u.last_updated_time},c={volatility_accumulator:e.v_parameters.fields.volatility_accumulator,volatility_reference:e.v_parameters.fields.volatility_reference,index_reference:(0,z.asIntN)(BigInt(e.v_parameters.fields.index_reference.fields.bits)),last_update_timestamp:e.v_parameters.fields.last_update_timestamp,bin_step_config:e.v_parameters.fields.bin_step_config.fields},p={id:e.id.id,bin_step:Number(e.bin_step),coin_type_a:(0,z.fixCoinType)(r.type_arguments[0],!1),coin_type_b:(0,z.fixCoinType)(r.type_arguments[1],!1),pool_type:t,index:Number(e.index),bin_manager:i,variable_parameters:c,active_id:(0,z.asIntN)(BigInt(e.active_id.fields.bits)),permissions:e.permissions.fields,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:s};return p.bin_step=p.bin_manager.bin_step,p}catch(e){return console.log("\u{1F680} ~ parseDlmmPool ~ error:",e),$("ParseError",e,{[z.DETAILS_KEYS.METHOD_NAME]:"parseDlmmPool",[z.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function Ke(n){let e=(0,z.getObjectFields)(n),t=(0,z.getObjectType)(n),r=(0,z.extractStructTagFromType)(t);return{id:e.id.id,name:e.name,ref_fee_rate:(0,z.d)(e.ref_fee_rate).div(1e4).toNumber(),start_time:Number(e.start_time),end_time:Number(e.end_time),balances:{id:e.balances.fields.id.id,size:e.balances.fields.size},type:r.full_address}}function $e(n){try{let e=(0,z.getObjectFields)(n);return{uri:e.uri,index:e.index,id:e.id.id,name:e.name,pool_id:e.pool_id,lower_bin_id:(0,z.asIntN)(BigInt(e.lower_bin_id.fields.bits)),upper_bin_id:(0,z.asIntN)(BigInt(e.upper_bin_id.fields.bits)),liquidity_shares:e.liquidity_shares,description:e.description,coin_type_a:(0,z.fixCoinType)(e.coin_type_a,!1),coin_type_b:(0,z.fixCoinType)(e.coin_type_b,!1)}}catch(e){return console.log("\u{1F680} ~ parseDlmmPosition ~ error:",e),$("ParseError",e,{[z.DETAILS_KEYS.METHOD_NAME]:"parseDlmmPosition",[z.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function qn(n,e,t,r){let i=n.map((d,f)=>{let c=t+f,p=F.getPricePerLamportFromBinId(c,e);if(c===r.bin_id){let{amount_a:P,amount_b:S}=F.calculateOutByShare(r,d);return{bin_id:c,amount_a:P,amount_b:S,liquidity:d,price_per_lamport:p}}if(c<r.bin_id){let P=F.getAmountBFromLiquidity(d);return{bin_id:c,amount_a:"0",amount_b:P,liquidity:d,price_per_lamport:p}}let _=F.getQPriceFromId(c,e),y=F.getAmountAFromLiquidity(d,_);return{bin_id:c,amount_a:y,amount_b:"0",liquidity:d,price_per_lamport:p}}),s=i.reduce((d,f)=>d.add(new L(f.amount_a)),new L(0)).toFixed(0),u=i.reduce((d,f)=>d.add(new L(f.amount_b)),new L(0)).toFixed(0);return{bins:i,amount_a:s,amount_b:u}}function Ii(n){try{let e=Gt.bcs.struct("BinAmount",{id:Gt.bcs.struct("I32",{bits:Gt.bcs.u32()}),amount_a:Gt.bcs.u64(),amount_b:Gt.bcs.u64(),price:Gt.bcs.u128(),liquidity_supply:Gt.bcs.u128(),rewards_growth_global:Gt.bcs.vector(Gt.bcs.u128()),fee_a_growth_global:Gt.bcs.u128(),fee_b_growth_global:Gt.bcs.u128()});return Gt.bcs.vector(e).parse(Uint8Array.from(n.results[1].returnValues[0][0])).map(r=>({bin_id:(0,z.asIntN)(BigInt(r.id.bits)),amount_a:r.amount_a,amount_b:r.amount_b,liquidity:r.liquidity_supply,price_per_lamport:F.getPricePerLamportFromQPrice(r.price)}))}catch(e){return console.log("\u{1F680} ~ parseBinInfo ~ error:",e),[]}}function Ci(n){try{return{bin_id:(0,z.asIntN)(BigInt(n.id.fields.bits)),amount_a:n.amount_a,amount_b:n.amount_b,liquidity:n.liquidity_share,price_per_lamport:F.getPricePerLamportFromQPrice(n.price)}}catch(e){return console.log("\u{1F680} ~ parseBinInfo ~ error:",e),$("ParseError",e,{[z.DETAILS_KEYS.METHOD_NAME]:"parseBinInfo",[z.DETAILS_KEYS.REQUEST_PARAMS]:{fields:n}})}}function Ti(n){let e={},t=n.events?.filter(r=>r.type.includes("pool::CollectFeeEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:i}=t[r],s={position_id:i.position,fee_owned_a:i.fee_a,fee_owned_b:i.fee_b};e[i.position]=s}return e}function ki(n){let e={},t=n.events?.filter(r=>r.type.includes("pool::CollectRewardEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:i}=t[r],s=i.position,u=i.reward,d=i.amount,f={coin_type:(0,z.fixCoinType)(u.name,!1),reward_owned:d},c=e[s];c?c.rewards.push(f):c={position_id:s,rewards:[f]},e[s]=c}return e}function xi(n,e){let t=n.events?.filter(r=>r.type.includes("pool::SwapEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:i}=t[r],{partner:s,pool:u,amount_in:d,amount_out:f,fee:c,ref_fee:p,bin_swaps:_,from:y,target:P}=i,S=_.map(k=>({bin_id:k.bin_id.bits,in_amount:k.amount_in,out_amount:k.amount_out,fee:k.fee,var_fee_rate:k.var_fee_rate}));return{pool_id:u,a2b:e,in_amount:d,out_amount:f,ref_fee_amount:p,fee_amount:c,bin_swaps:S,partner:s,from_coin_type:(0,z.fixCoinType)(y.name,!1),to_coin_type:(0,z.fixCoinType)(P.name,!1)}}}function jn(n){switch(n){case 0:return 0;case 1:return 1;case 2:return 2}}var Sr=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","ClosePositionEvent"];function Ri(n,e,t,r){let i=[],{timestampMs:s,events:u}=n;return u?.forEach((d,f)=>{let{name:c,address:p}=(0,z.extractStructTagFromType)(d.type);if(Sr.includes(c)&&p===t&&r===d.parsedJson.pool){let _={tx:d.id.txDigest,sender:d.sender,type:d.type,block_time:s||"0",index:`${e}_${f}`,parsed_json:d.parsedJson};i.push(_)}}),i}function Wn(n,e,t){let r=[],i=0,s=new Date(n*1e3).getTime(),u=new Date().getTime();for(;;){let d=s+i*t;if(d>=u&&r.push(d),i>=e)break;i+=1}return r}function Zn(n,e,t,r){let i=[];for(let s=e;s<=t;s+=r){let u=n.findLast(d=>(0,z.d)(s).gte(d.time));u?i.push({time:s.toString(),emissions_per_second:u.emissions_per_second,emissions_per_day:u.emissions_per_day,visualized_time:new Date(s*1e3).toLocaleString()}):i.push({emissions_per_day:"0",time:s.toString(),emissions_per_second:"0",visualized_time:new Date(s*1e3).toLocaleString()})}return i}function Un(n){if(n.length===0)return;let e=new Date().getTime()/1e3,t=n.findLast(r=>(0,z.d)(e).gte(r.time));return t||n[n.length-1]}function re(n,e){let t=n.mul(e);if(t.gt(0)&&t.lt(1))throw new pe(`Multiplication ${t} is less than 1`,"AmountTooSmall");return t.floor()}function Me(n){if(n.gt(0)&&n.lt(1))throw new pe(`Multiplication ${n.toString()} is less than 1`,"AmountTooSmall");return n.floor()}function Di(n){switch(n){case 0:return"spot";case 1:return"curve";case 2:return"bid_ask"}}function Fi(n,e,t,r){let i=Buffer.from(n,"utf8"),s=Buffer.from(e,"utf8"),u=s.length,d=0;for(;d<u;){let y=s[d];i=Buffer.concat([i,Buffer.from([y])]),d++}let f=Gt.bcs.u16().serialize(t).toBytes(),c=Gt.bcs.u16().serialize(r).toBytes(),p=Buffer.concat([i,f,c]),_=(0,Ar.blake2b)(p,void 0,32);return`0x${Buffer.from(_).toString("hex")}`}var O=require("@cetusprotocol/common-sdk");var te=class n{static toWeight(e){console.log("\u{1F680} ~ WeightUtils ~ toWeight ~ options:",e);let{strategy_type:t,active_id:r,bin_step:i,lower_bin_id:s,upper_bin_id:u,total_amount_a:d,total_amount_b:f,active_bin_of_pool:c}=e,p=r<s||r>u,_=F.getQPriceFromId(r,i),y=(0,O.d)(0),P=(0,O.d)(0),S=(0,O.d)(200);if(t===2?S=(0,O.d)(200):t===1?S=(0,O.d)(2e3):t===0&&(S=(0,O.d)(1)),!p&&c){let Q=this.calculateActiveWeights(c.amount_a,c.amount_b,_,S);y=Q.active_weight_a,P=Q.active_weight_b}r===s&&(0,O.d)(f).isZero()&&(y=(0,O.d)(S).mul((0,O.d)(2).pow(D*2)).div((0,O.d)(_)).floor(),P=(0,O.d)(0)),r===u&&(0,O.d)(d).isZero()&&(P=(0,O.d)(S).mul((0,O.d)(2).pow(D)).floor(),y=(0,O.d)(0)),r>s&&r<u&&((0,O.d)(d).isZero()&&(P=(0,O.d)(S).mul((0,O.d)(2).pow(D)).floor(),y=(0,O.d)(0)),(0,O.d)(f).isZero()&&(y=(0,O.d)(S).mul((0,O.d)(2).pow(D*2)).div((0,O.d)(_)).floor(),P=(0,O.d)(0)));let B=p?(0,O.d)(0):y,k=p?(0,O.d)(0):P,x=(0,O.d)(2e3).sub((0,O.d)(200)).floor(),R=r>s?x.div((0,O.d)(r-s)).floor():(0,O.d)(0),T=u>r?x.div((0,O.d)(u-r)).floor():(0,O.d)(0),I=s,G=[],H=[];for(;I<=u;){let Q=(0,O.d)(0);if(I<r){let tt=r-I;t===0?Q=(0,O.d)(1):t===2?Q=(0,O.d)(S).add(R.mul(tt)).floor():t===1&&(Q=(0,O.d)(S).sub(R.mul(tt)).floor())}else if(I>r){let tt=I-r;t===0?Q=(0,O.d)(1):t===2?Q=(0,O.d)(S).add(T.mul(tt)).floor():t===1&&(Q=(0,O.d)(S).sub(T.mul(tt)).floor())}else Q=S;if(G.push(Q),I<r)k=k.add(Q.mul((0,O.d)(2).pow(D))).floor(),H.push((0,O.d)(0));else if(I>r){let tt=Q.mul((0,O.d)(2).pow(D*2)).div(F.getQPriceFromId(I,i)).floor();H.push(tt),B=B.add(tt).floor()}else H.push((0,O.d)(0));I+=1}return{...e,total_weight_a:B,total_weight_b:k,active_weight_a:y,active_weight_b:P,weights:G,weight_per_prices:H}}static toWeightSpotBalanced(e,t){let r=[];for(let i=e;i<=t;i++)r.push({bin_id:i,weight:1});return r}static toWeightDescendingOrder(e,t){let r=[];for(let i=e;i<=t;i++)r.push({bin_id:i,weight:t-i+1});return r}static toWeightAscendingOrder(e,t){let r=[];for(let i=e;i<=t;i++)r.push({bin_id:i,weight:i-e+1});return r}static toWeightCurve(e,t,r){if(r<e)return n.toWeightDescendingOrder(e,t);if(r>t)return n.toWeightAscendingOrder(e,t);let i=2e3,u=i-200,d=r>e?Math.floor(u/(r-e)):0,f=t>r?Math.floor(u/(t-r)):0,c=[];for(let p=e;p<=t;p++)p<r?c.push({bin_id:p,weight:i-(r-p)*d}):p>r?c.push({bin_id:p,weight:i-(p-r)*f}):c.push({bin_id:p,weight:i});return c}static toWeightBidAsk(e,t,r){if(r>t)return n.toWeightDescendingOrder(e,t);if(r<e)return n.toWeightAscendingOrder(e,t);let i=2e3,s=200,u=i-s,d=r>e?Math.floor(u/(r-e)):0,f=t>r?Math.floor(u/(t-r)):0,c=[];for(let p=e;p<=t;p++)p<r?c.push({bin_id:p,weight:s+(r-p)*d}):p>r?c.push({bin_id:p,weight:s+(p-r)*f}):c.push({bin_id:p,weight:s});return c}static toAmountBidSide(e,t,r,i,s=!1){let u=i.filter(f=>f.bin_id<=e).reduce(function(f,c){return s?c.bin_id>e?f:f.add(c.weight):c.bin_id>=e?f:f.add(c.weight)},(0,O.d)(0));if(u.cmp((0,O.d)(0))!=1)throw Error("Invalid parameters");return{bins:i.map(f=>{let c=F.getPricePerLamportFromBinId(f.bin_id,r);if(!(f.bin_id<=e)||f.bin_id>=e&&!s)return{bin_id:f.bin_id,amount_a:"0",amount_b:"0",price_per_lamport:c,liquidity:"0"};{let _=(0,O.d)(f.weight).div(u),y=re((0,O.d)(t),_).toString(),P="0",S=F.getQPriceFromId(f.bin_id,r),B=F.getLiquidity(P,y,S);return{bin_id:f.bin_id,amount_b:y,amount_a:P,price_per_lamport:c,liquidity:B}}}),amount_a:"0",amount_b:t}}static toAmountAskSide(e,t,r,i,s=!1){let u=i.filter(f=>f.bin_id>=e).reduce(function(f,c){if(c.bin_id<=e&&!s)return f;{let p=F.getPricePerLamportFromBinId(c.bin_id,t),_=new L(c.weight).div(p);return f.add(_)}},new L(0));if(u.cmp(new L(0))!=1)throw Error("Invalid parameters");return{bins:i.map(f=>{let c=F.getPricePerLamportFromBinId(f.bin_id,t);if(!(f.bin_id>=e)||f.bin_id<=e&&!s)return{bin_id:f.bin_id,amount_a:"0",amount_b:"0",price_per_lamport:c,liquidity:"0"};{let y=new L(f.weight).div(c).div(u),P=re((0,O.d)(r),y).toString(),S="0",B=F.getQPriceFromId(f.bin_id,t),k=F.getLiquidity(P,S,B);return{bin_id:f.bin_id,amount_a:P,amount_b:S,price_per_lamport:c,liquidity:k}}}),amount_a:r,amount_b:"0"}}static toAmountBothSide(e,t,r,i,s,u,d){let f=!(0,O.d)(r).isZero()&&(0,O.d)(i).isZero(),c=(0,O.d)(r).isZero()&&!(0,O.d)(i).isZero();if(e>d[d.length-1].bin_id)return n.toAmountBidSide(e,i,t,d);if(c&&e!==d[d.length-1].bin_id)return n.toAmountBidSide(e,i,t,d,!0);if(e<d[0].bin_id)return n.toAmountAskSide(e,t,r,d);if(f&&e!==d[0].bin_id)return n.toAmountAskSide(e,t,r,d,!0);let p=d.filter(_=>_.bin_id===e);if(p.length===1){let{totalWeightA:_,totalWeightB:y,activeWeightA:P,activeWeightB:S}=n.calculateTotalWeights(t,d,e,p[0],s,u,f?"a":c?"b":void 0),B=new L(r.toString()).div(_),k=new L(i.toString()).div(y),x=d.map(I=>{let G=F.getPricePerLamportFromBinId(I.bin_id,t);if(I.bin_id<e||I.bin_id===e&&c){let st=re(k,new L(I.weight)),v="0",o=F.getQPriceFromId(I.bin_id,t),l=F.getLiquidity(v,st.toString(),o);return{bin_id:I.bin_id,amount_a:"0",amount_b:st.toString(),price_per_lamport:G,liquidity:l}}if(I.bin_id>e||I.bin_id===e&&f){let st=new L(I.weight).div(G),v=re(B,new L(st)),o="0",l=F.getQPriceFromId(I.bin_id,t),h=F.getLiquidity(v.toString(),o,l);return{bin_id:I.bin_id,amount_a:v.toString(),amount_b:"0",price_per_lamport:G,liquidity:h}}let H=re(B,P),Q=re(k,S),tt=H.toString(),$t=Q.toString(),Vt=F.getQPriceFromId(I.bin_id,t),J=F.getLiquidity(tt,$t,Vt);return{bin_id:I.bin_id,amount_a:tt,amount_b:$t,price_per_lamport:G,liquidity:J}}),R=x.reduce((I,G)=>(0,O.d)(I).add((0,O.d)(G.amount_a)),(0,O.d)(0)).toString(),T=x.reduce((I,G)=>(0,O.d)(I).add((0,O.d)(G.amount_b)),(0,O.d)(0)).toString();return{bins:x,amount_a:R,amount_b:T}}else{let{totalWeightA:_,totalWeightB:y}=n.calculateTotalWeights(t,d,e),P=new L(r.toString()).div(_),S=new L(i.toString()).div(y),B=d.map(R=>{let T=F.getPricePerLamportFromBinId(R.bin_id,t);if(R.bin_id<e){let I=re(S,new L(R.weight));return{bin_id:R.bin_id,amount_a:"0",amount_b:I.toString(),price_per_lamport:T}}else{let I=new L(R.weight).div(T),G=re(P,I);return{bin_id:R.bin_id,amount_a:G.toString(),amount_b:"0",price_per_lamport:T}}}),k=B.reduce((R,T)=>(0,O.d)(R).add((0,O.d)(T.amount_a)),(0,O.d)(0)).toString(),x=B.reduce((R,T)=>(0,O.d)(R).add((0,O.d)(T.amount_b)),(0,O.d)(0)).toString();return{bins:B,amount_a:k,amount_b:x}}}static autoFillCoinByWeight(e,t,r,i,s,u,d){if(e>d[d.length-1].bin_id)return n.toAmountBidSide(e,r,t,d);if(e<d[0].bin_id)return n.toAmountAskSide(e,t,r,d);let f=d.filter(P=>P.bin_id===e),{totalWeightA:c,totalWeightB:p}=n.calculateTotalWeights(t,d,e,f.length===1?f[0]:void 0,f.length===1?s:void 0,f.length===1?u:void 0),_=(0,O.d)(0);i?_=c.isZero()?new L(0):new L(r).div(c):_=p.isZero()?new L(0):new L(r).div(p);let y=re(_,i?p:c).toString();return n.toAmountBothSide(e,t,i?r:y,i?y:r,s,u,d)}static calculateActiveWeights(e,t,r,i){let s=(0,O.d)(r),u=(0,O.d)(e),d=(0,O.d)(t),f=(0,O.d)(0),c=(0,O.d)(0);if(u.isZero()&&d.isZero())f=(0,O.d)(i).mul((0,O.d)(2).pow(D*2)).div(s.mul(2)).floor(),c=(0,O.d)(i).mul((0,O.d)(2).pow(D)).div(2).floor();else{if(u.isZero())f=(0,O.d)(0);else{let p=d.mul((0,O.d)(2).pow(D)).div(u);f=(0,O.d)(i).mul((0,O.d)(2).pow(D*2)).div(s.add(p)).floor()}if(d.isZero())c=(0,O.d)(0);else{let p=(0,O.d)(2).pow(D).add(s.mul(u).div(d)).floor();c=(0,O.d)(i).mul((0,O.d)(2).pow(D*2)).div(p).floor()}}return{active_weight_a:f,active_weight_b:c}}static calculateTotalWeights(e,t,r,i,s,u,d){let f=(0,O.d)(F.getPricePerLamportFromBinId(r,e)),c=(0,O.d)(0),p=(0,O.d)(0);if(s&&u&&i&&!d)if((0,O.d)(s).isZero()&&(0,O.d)(u).isZero())c=new L(i.weight).div(f.mul(new L(2))),p=new L(i.weight).div(new L(2));else{let P=new L(s.toString()),S=new L(u.toString());(0,O.d)(s).isZero()||(c=new L(i.weight).div(f.add(S.div(P)))),(0,O.d)(u).isZero()||(p=new L(i.weight).div(new L(1).add(f.mul(P).div(S))))}let _=c,y=p;return t.forEach(P=>{if((P.bin_id<r||d==="b")&&(y=y.add(new L(P.weight))),P.bin_id>r||d==="a"){let S=F.getPricePerLamportFromBinId(P.bin_id,e),B=new L(P.weight).div(S);_=_.add(B)}}),{totalWeightA:_,totalWeightB:y,activeWeightA:c,activeWeightB:p}}};var Jt=require("@cetusprotocol/common-sdk");var Pe=class{static toAmountsByWeights(e){let{total_weight_a:t,total_weight_b:r,weights:i,weight_per_prices:s,total_amount_a:u,total_amount_b:d,active_weight_a:f,active_weight_b:c,lower_bin_id:p,upper_bin_id:_,active_id:y,bin_step:P}=e,S=r.isZero()?(0,Jt.d)(0):(0,Jt.d)(d).mul((0,Jt.d)(2).pow(D*2)).div(r).floor(),B=t.isZero()?(0,Jt.d)(0):(0,Jt.d)(u).mul((0,Jt.d)(2).pow(D*2)).div(t).floor(),k=Me(B.mul(f).div((0,Jt.d)(2).pow(D*2))),x=Me(S.mul(c).div((0,Jt.d)(2).pow(D*2))),R=_-p+1,T=p,I=0,G=[];for(;I<R;){let Q=(0,Jt.d)(0),tt=(0,Jt.d)(0),$t=F.getQPriceFromId(T,P);T<y?tt=Me(S.mul(i[I]).div((0,Jt.d)(2).pow(D))):T>y?Q=Me(B.mul(s[I]).div((0,Jt.d)(2).pow(D*2))):(Q=k,tt=x);let Vt=F.getLiquidity(Q.toString(),tt.toString(),$t);G.push({bin_id:T,amount_a:Q.toString(),amount_b:tt.toString(),price_per_lamport:F.getPricePerLamportFromBinId(T,P),liquidity:Vt}),T+=1,I+=1}return{bins:G,amount_a:u.toString(),amount_b:d.toString()}}static toAmountsBothSideByStrategy(e,t,r,i,s,u,d,f){let c=te.toWeight({strategy_type:d,active_id:e,bin_step:t,lower_bin_id:r,upper_bin_id:i,total_amount_a:s,total_amount_b:u,active_bin_of_pool:f});return this.toAmountsByWeights(c)}static autoFillCoinByStrategy(e,t,r,i,s,u,d,f){switch(d){case 0:{let c=te.toWeightSpotBalanced(s,u);return te.autoFillCoinByWeight(e,t,r,i,f?.amount_a||"0",f?.amount_b||"0",c)}case 1:{let c=te.toWeightCurve(s,u,e);return te.autoFillCoinByWeight(e,t,r,i,f?.amount_a||"0",f?.amount_b||"0",c)}case 2:{let c=te.toWeightBidAsk(s,u,e);return te.autoFillCoinByWeight(e,t,r,i,f?.amount_a||"0",f?.amount_b||"0",c)}}}static autoFillCoinByStrategyV2(e,t,r,i,s,u,d,f){let c=this.autoFillCoinByStrategy(e,t,r,i,s,u,d,f),p=te.toWeight({strategy_type:d,active_id:e,bin_step:t,lower_bin_id:s,upper_bin_id:u,total_amount_a:i?r:c.amount_a,total_amount_b:i?c.amount_b:r,active_bin_of_pool:f});return this.toAmountsByWeights(p)}};var lt=require("@cetusprotocol/common-sdk");var Oe=class n{static getVariableFee(e){let{volatility_accumulator:t,bin_step_config:r}=e,{variable_fee_control:i,bin_step:s}=r;return(0,lt.d)(i).gt(0)?(0,lt.d)(t).mul(s).pow(2).mul(i).add(99999999999).div(1e11).toFixed(0):"0"}static calculateCompositionFee(e,t){let r=(0,lt.d)(e).mul(t);return(0,lt.d)(r).mul((0,lt.d)(1e9).add(t)).div(1e18).toFixed(0)}static calculateProtocolFee(e,t){return(0,lt.d)(e).mul(t).div(1e4).ceil().toFixed(0)}static getProtocolFees(e,t,r){let i=n.calculateProtocolFee(e,r),s=n.calculateProtocolFee(t,r);return{protocol_fee_a:i,protocol_fee_b:s}}static getCompositionFees(e,t,r){let{bin_step_config:i}=r;if((0,lt.d)(e.liquidity||"0").eq((0,lt.d)(0)))return{fees_a:"0",fees_b:"0"};let{bin_step:s,base_factor:u}=i,d=F.getQPriceFromId(e.bin_id,s),f=F.getLiquidity(e.amount_a,e.amount_b,d),c=F.getLiquidity(t.amount_a,t.amount_b,d),p=(0,lt.d)(e.liquidity).mul(c).div(f).toFixed(0),{amount_a:_,amount_b:y}=F.calculateOutByShare({bin_id:e.bin_id,liquidity:(0,lt.d)(e.liquidity).add(p).toFixed(0),amount_a:(0,lt.d)(e.amount_a).add(t.amount_a).toFixed(0),amount_b:(0,lt.d)(e.amount_b).add(t.amount_b).toFixed(0),price_per_lamport:e.price_per_lamport},p),P=(0,lt.d)(s).mul(u).mul(10),S=n.getVariableFee(r),B=(0,lt.d)(P).add(S).toFixed(0);(0,lt.d)(B).gt(1e8)&&(B=1e8.toString());let k="0",x="0";return(0,lt.d)(_).gt(t.amount_a)&&(0,lt.d)(t.amount_b).gt(y)?x=n.calculateCompositionFee((0,lt.d)(t.amount_b).sub(y).toFixed(0),B):(0,lt.d)(y).gt(t.amount_b)&&(0,lt.d)(t.amount_a).gt(_)&&(k=n.calculateCompositionFee((0,lt.d)(t.amount_a).sub(_).toFixed(0),B)),{fees_a:k,fees_b:x}}};var Br=require("@mysten/sui/client"),Ni=require("@mysten/sui/utils"),Ce=require("@mysten/sui/bcs"),Te=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getPoolAddress(e,t,r,i){try{let s=Fi(e,t,r,i),{dlmm_pool:u}=this._sdk.sdkOptions,{pools_id:d}=(0,V.getPackagerConfigs)(u),f=await this._sdk.FullClient.getDynamicFieldObject({parentId:d,name:{type:"0x2::object::ID",value:s}});return(0,V.getObjectFields)(f).value.fields.value.fields.pool_id}catch(s){return $("FetchError",s,{[V.DETAILS_KEYS.METHOD_NAME]:"getPoolAddress",[V.DETAILS_KEYS.REQUEST_PARAMS]:{coin_type_a:e,coin_type_b:t,bin_step:r,base_factor:i}})}}async getBasePoolList(e="all",t=!1){let{dlmm_pool:r}=this._sdk.sdkOptions,i={data:[],has_next_page:!1},s=e==="all",u=`${r.package_id}_getBasePoolList`;if(s){let d=this._sdk.getCache(u,t);if(d&&d.length>0)return i.data.push(...d),i}try{let d=`${r.package_id}::registry::CreatePoolEvent`,f=await this._sdk.FullClient.queryEventsByPage({MoveEventType:d},e);i.has_next_page=f.has_next_page,i.next_cursor=f.next_cursor,f.data.forEach(c=>{let p=Oi(c);i.data.push(p)}),s&&this._sdk.updateCache(`${r.package_id}_getPoolImmutables`,i.data)}catch(d){return $("FetchError",d,{[V.DETAILS_KEYS.METHOD_NAME]:"getBasePoolList"})}return i}async getPools(e="all",t=!1){let r={data:[],has_next_page:!1},i=await this.getBasePoolList(e,t);if(i.data.length===0)return r;try{let s=await this._sdk.FullClient.batchGetObjects(i.data.map(u=>u.id),{showContent:!0,showType:!0});r.has_next_page=i.has_next_page,r.next_cursor=i.next_cursor;for(let u of s){let d=Ee(u),f=`${d.id}_getDlmmPool`;this._sdk.updateCache(f,d),r.data.push(d)}}catch(s){return $("FetchError",s,{[V.DETAILS_KEYS.METHOD_NAME]:"getPools"})}return r}async getBinInfo(e,t,r,i=!0){try{let s=`${e}_getBinInfo_${t}`,u=this._sdk.getCache(s,i);if(u!==void 0)return u;let d=F.binScore(t),[f,c]=F.resolveBinPosition(d),_=(await this._sdk.FullClient.getDynamicFieldObject({parentId:e,name:{type:"u64",value:f}})).data.content.fields.value.fields.value.fields.group.fields.bins[c].fields,y=Ci(_);return this._sdk.updateCache(s,y),y}catch{return{bin_id:t,amount_a:"0",amount_b:"0",liquidity:"0",price_per_lamport:F.getPricePerLamportFromBinId(t,r)}}}async getTotalFeeRate(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:i,coin_type_b:s}=e,u=new Ie.Transaction;u.moveCall({target:`${t.published_at}::pool::get_total_fee_rate`,arguments:[u.object(r)],typeArguments:[i,s]});let d=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:u,sender:(0,Ni.normalizeSuiAddress)("0x0")});return Ce.bcs.struct("FeeRate",{base_fee_rate:Ce.bcs.u64(),var_fee_rate:Ce.bcs.u64(),total_fee_rate:Ce.bcs.u64()}).parse(Uint8Array.from(d.results[0].returnValues[0][0]))}async getPoolBinInfo(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:i,coin_type_b:s}=e,u=1e3,d=[],f,c=!0;for(;c;){let p=new Ie.Transaction,_;f!==void 0?_=p.moveCall({target:"0x1::option::some",arguments:[p.pure.u32(Number((0,V.asUintN)(BigInt(f))))],typeArguments:["u32"]}):_=p.moveCall({target:"0x1::option::none",typeArguments:["u32"]}),p.moveCall({target:`${t.published_at}::pool::fetch_bins`,arguments:[p.object(r),_,p.pure.u64(u)],typeArguments:[i,s]});let y=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:p,sender:(0,Ni.normalizeSuiAddress)("0x0")}),P=Ii(y);d.push(...P),f=P.length>0?P[P.length-1].bin_id+1:void 0,c=P.length===u}return d.sort((p,_)=>p.bin_id-_.bin_id)}async getPoolTransactionList({pool_id:e,pagination_args:t,order:r="descending",full_rpc_url:i}){let{FullClient:s,sdkOptions:u}=this._sdk,d;i?d=(0,V.createFullClient)(new Br.SuiClient({url:i})):d=s;let f={data:[],has_next_page:!1},c=50,p=t,_=t.limit||10;do{let y=await d.queryTransactionBlocksByPage({ChangedObject:e},{...p,limit:50},r);y.data.forEach((P,S)=>{let B=Ri(P,S,u.dlmm_pool.package_id,e);f.data=[...f.data,...B]}),f.has_next_page=y.has_next_page,f.next_cursor=y.next_cursor,p.cursor=y.next_cursor}while(f.data.length<_&&f.has_next_page);return f.data.length>_&&(f.data=f.data.slice(0,_),f.has_next_page=!0),f.data.length>0&&(f.next_cursor=f.data[f.data.length-1].tx),f}async getRangeBinInfo(e,t,r,i){let s=[];for(let u=t;u<=r;u++){let d=await Promise.all([this.getBinInfo(e,u,i)]);s.push(...d)}return s}async getAssignPoolList(e){if(e.length===0)return[];let t=[];try{let r=await this._sdk.FullClient.batchGetObjects(e,{showContent:!0,showType:!0});for(let i of r){let s=Ee(i),u=`${s.id}_getDlmmPool`;this._sdk.updateCache(u,s),t.push(s)}}catch(r){return $("FetchError",r,{[V.DETAILS_KEYS.METHOD_NAME]:"getAssignPoolList"})}return t}async getPool(e,t=!0){try{let r=`${e}_getDlmmPool`,i=this._sdk.getCache(r,t);if(i!==void 0)return i;let s=await this._sdk.FullClient.getObject({id:e,options:{showType:!0,showContent:!0}}),u=Ee(s);return this._sdk.updateCache(r,u),u}catch(r){return $("FetchError",r,{[V.DETAILS_KEYS.METHOD_NAME]:"getPool",[V.DETAILS_KEYS.REQUEST_PARAMS]:e})}}async createPoolAndAddWithPricePayload(e){let{bin_step:t,url:r,coin_type_a:i,coin_type_b:s,bin_infos:u,price_base_coin:d,price:f,lower_price:c,upper_price:p,decimals_a:_,decimals_b:y,strategy_type:P,use_bin_infos:S,base_factor:B,pool_id:k}=e,x,R,T,I=u;if(d==="coin_a")x=F.getBinIdFromPrice(c,t,!1,_,y),R=F.getBinIdFromPrice(p,t,!0,_,y),T=F.getBinIdFromPrice(f,t,!0,_,y);else{x=F.getBinIdFromPrice((0,V.d)(1).div(p).toString(),t,!1,_,y),R=F.getBinIdFromPrice((0,V.d)(1).div(c).toString(),t,!0,_,y),T=F.getBinIdFromPrice((0,V.d)(1).div(f).toString(),t,!1,_,y);let Q={pool_id:k,amount_a:u.amount_b,amount_b:u.amount_a,active_id:T,bin_step:t,lower_bin_id:x,upper_bin_id:R,active_bin_of_pool:void 0,strategy_type:e.strategy_type};I=await this.sdk.Position.calculateAddLiquidityInfo(Q)}let H={bin_step:t,url:r,coin_type_a:i,coin_type_b:s,bin_infos:I,lower_bin_id:x,upper_bin_id:R,active_id:T,strategy_type:P,use_bin_infos:S,base_factor:B};return this.createPoolAndAddLiquidityPayload(H)}async createPoolPayload(e,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{bin_step:i,base_factor:s,url:u,coin_type_a:d,coin_type_b:f,active_id:c}=e;t=t||new Ie.Transaction;let{registry_id:p,global_config_id:_,versioned_id:y}=(0,V.getPackagerConfigs)(r),P=await this._sdk.FullClient.fetchCoinMetadata(d);P?.id||$("FetchError",new Error(`coin_a_metadata not found: ${d}`),{[V.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[V.DETAILS_KEYS.REQUEST_PARAMS]:e});let S=await this._sdk.FullClient.fetchCoinMetadata(f);S?.id||$("FetchError",new Error(`coin_b_metadata not found: ${f}`),{[V.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[V.DETAILS_KEYS.REQUEST_PARAMS]:e});let[B,k]=t.moveCall({target:`${r.published_at}::registry::create_pool`,arguments:[t.object(p),t.object(P.id),t.object(S.id),t.pure.u16(i),t.pure.u16(s),t.pure.u32(Number((0,V.asUintN)(BigInt(c)))),t.pure.string(u||""),t.object(_),t.object(y),t.object(V.CLOCK_ADDRESS)],typeArguments:[d,f]});return t.moveCall({target:`${r.published_at}::registry::destroy_receipt`,arguments:[t.object(B),k,t.object(y)],typeArguments:[d,f]}),k}async createPoolAndAddLiquidityPayload(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{bin_step:r,base_factor:i,url:s,active_id:u,coin_type_a:d,coin_type_b:f,bin_infos:c,lower_bin_id:p,upper_bin_id:_,strategy_type:y,use_bin_infos:P}=e,{registry_id:S,global_config_id:B,versioned_id:k}=(0,V.getPackagerConfigs)(t),x=new Ie.Transaction;(0,V.isSortedSymbols)((0,V.fixCoinType)(d,!1),(0,V.fixCoinType)(f,!1))&&$("InvalidCoinTypeSequence",new Error("invalid coin type sequence"),{[V.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[V.DETAILS_KEYS.REQUEST_PARAMS]:e});let R=_-p+1;R>1e3&&$("InvalidBinWidth",new Error("Width is too large"),{[V.DETAILS_KEYS.METHOD_NAME]:"openPosition",[V.DETAILS_KEYS.REQUEST_PARAMS]:e}),(await this._sdk.FullClient.fetchCoinMetadata(d))?.id||$("FetchError",new Error(`coin_a_metadata not found: ${d}`),{[V.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[V.DETAILS_KEYS.REQUEST_PARAMS]:e}),(await this._sdk.FullClient.fetchCoinMetadata(f))?.id||$("FetchError",new Error(`coin_b_metadata not found: ${f}`),{[V.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[V.DETAILS_KEYS.REQUEST_PARAMS]:e}),console.log("\u{1F680} ~ createPoolAndAddLiquidityPayload ~ option:",{...e,width:R});let H={pool_id:await this.createPoolPayload({active_id:u,bin_step:r,base_factor:i,coin_type_a:d,coin_type_b:f},x),bin_infos:c,coin_type_a:d,coin_type_b:f,lower_bin_id:p,upper_bin_id:_,active_id:u,strategy_type:y,use_bin_infos:P,max_price_slippage:0,bin_step:r};return this.sdk.Position.addLiquidityPayload(H,x),x}};var Er=require("@cetusprotocol/common-sdk"),Li={env:"testnet",full_rpc_url:Er.FullRpcUrlTestnet,dlmm_pool:{package_id:"0x17a1f5a8779461ff44e942adf33325cce112c693d6a177ed77f035ca86d1fdb6",published_at:"0x6d32c1be32eefcea933c03dd5cb7c783d1d83f6b30c4d1131d955933747b1701",version:1,config:{registry_id:"0x319070e26a6809f439d3c4a45e63bf74939c5fe3165de7b65968ee8547f71bd0",pools_id:"0x505fcde74ab557d553832a87f169a0408ad3507ca4e84b25f7d32c2c1535765c",global_config_id:"0x88bb33e9eff2fccab980a0e4b43fc4572abd08f08304d47a20d3e4e99d94d159",versioned_id:"0xa710caae87b2129acc97fbb98ea7011e3137c3291b02c0fcce866d67d5d9e8d0",admin_cap_id:"0x6fc908894ad7c2ff16cca07a05af6760831a8b5e5dc34e40470dce6ee1760155",partners_id:"0xc5c31fe1550e39c9890e0fe3d2608dd9b408a10d74020e5ff72ccfffe4c9c879"}},dlmm_router:{package_id:"0xba3059875c8980ac171fc2bac81b9df172fb77fa0cb5a267636df701225b93ef",published_at:"0x59b7a2da6db8f9245a1db6169018af7124c0714fa77a84224967ead6be125127",version:1},faucet:{package_id:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",published_at:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",version:1}};var C=require("@cetusprotocol/common-sdk"),he=require("@mysten/sui/transactions"),Or=require("@mysten/sui/utils");var ze=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){let t=[];try{(await this._sdk.FullClient.getOwnedObjectsByPage(e,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{StructType:this.buildPositionType()}})).data.forEach(i=>{t.push($e(i))})}catch(r){console.log("\u{1F680} ~ PositionModule ~ getOwnerPositionList ~ error:",r),$("GetObjectError",r,{[C.DETAILS_KEYS.METHOD_NAME]:"getOwnerPositionList",[C.DETAILS_KEYS.REQUEST_PARAMS]:e})}return t}async getPosition(e){try{let t=await this._sdk.FullClient.getObject({id:e,options:{showType:!0,showContent:!0,showOwner:!0}});return $e(t)}catch(t){return console.log("\u{1F680} ~ PositionModule ~ getPosition ~ error:",t),$("GetObjectError",t,{[C.DETAILS_KEYS.METHOD_NAME]:"getPosition",[C.DETAILS_KEYS.REQUEST_PARAMS]:e})}}collectFeePayload(e,t){let{pool_id:r,position_id:i,coin_type_a:s,coin_type_b:u}=e,{dlmm_pool:d}=this.sdk.sdkOptions,{versioned_id:f,global_config_id:c}=(0,C.getPackagerConfigs)(d);t=t||new he.Transaction;let[p,_]=t.moveCall({target:`${d.published_at}::pool::collect_position_fee`,arguments:[t.object(r),t.object(i),t.object(c),t.object(f)],typeArguments:[s,u]}),y=C.CoinAssist.fromBalance(p,s,t),P=C.CoinAssist.fromBalance(_,u,t);return t.transferObjects([y,P],this.sdk.getSenderAddress()),t}updatePositionFeeAndRewards(e,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:i}=(0,C.getPackagerConfigs)(r),{pool_id:s,position_id:u,coin_type_a:d,coin_type_b:f}=e;return t.moveCall({target:`${r.published_at}::pool::update_position_fee_and_rewards`,arguments:[t.object(s),t.pure.id(u),t.object(i),t.object(C.CLOCK_ADDRESS)],typeArguments:[d,f]}),t}collectRewardPayload(e,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:i,global_config_id:s}=(0,C.getPackagerConfigs)(r);return t=t||new he.Transaction,e.forEach(u=>{let{pool_id:d,position_id:f,reward_coins:c,coin_type_a:p,coin_type_b:_}=u;c.forEach(y=>{let P=t.moveCall({target:`${r.published_at}::pool::collect_position_reward`,arguments:[t.object(d),t.object(f),t.object(s),t.object(i)],typeArguments:[p,_,y]}),S=C.CoinAssist.fromBalance(P,y,t);t.transferObjects([S],this.sdk.getSenderAddress())})}),t}collectRewardAndFeePayload(e,t){return t=t||new he.Transaction,e.forEach(r=>{let{pool_id:i,position_id:s,reward_coins:u,coin_type_a:d,coin_type_b:f}=r;this.updatePositionFeeAndRewards({pool_id:i,position_id:s,coin_type_a:d,coin_type_b:f},t),this.collectFeePayload({pool_id:i,position_id:s,coin_type_a:d,coin_type_b:f},t),this.collectRewardPayload([{pool_id:i,position_id:s,reward_coins:u,coin_type_a:d,coin_type_b:f}],t)}),t}validateActiveIdSlippage(e,t){let{pool_id:r,active_id:i,max_price_slippage:s,bin_step:u,coin_type_a:d,coin_type_b:f}=e,{dlmm_router:c}=this.sdk.sdkOptions,p=F.getBinShift(i,u,s),_=Number((0,C.asUintN)(BigInt(i)));return t.moveCall({target:`${c.published_at}::utils::validate_active_id_slippage`,arguments:[typeof r=="string"?t.object(r):r,t.pure.u32(Number(_)),t.pure.u32(Number(p))],typeArguments:[d,f]}),t}closePositionPayload(e,t){let{pool_id:r,position_id:i,reward_coins:s,coin_type_a:u,coin_type_b:d}=e,{dlmm_pool:f}=this.sdk.sdkOptions,{versioned_id:c,global_config_id:p}=(0,C.getPackagerConfigs)(f);t=t||new he.Transaction,this.updatePositionFeeAndRewards({pool_id:r,position_id:i,coin_type_a:u,coin_type_b:d},t),this.collectRewardPayload([{pool_id:r,position_id:i,reward_coins:s,coin_type_a:u,coin_type_b:d}],t);let[_,y,P]=t.moveCall({target:`${f.published_at}::pool::close_position`,arguments:[t.object(r),t.object(i),t.object(p),t.object(c),t.object(C.CLOCK_ADDRESS)],typeArguments:[u,d]}),S=C.CoinAssist.fromBalance(y,u,t),B=C.CoinAssist.fromBalance(P,d,t);return t.moveCall({target:`${f.published_at}::pool::destroy_close_position_cert`,arguments:[_,t.object(c)],typeArguments:[]}),t.transferObjects([S,B],this.sdk.getSenderAddress()),t}async getActiveBinIfInRange(e,t,r,i,s,u=!1){if(i<=r&&i>=t)return await this._sdk.Pool.getBinInfo(e,i,s,u)}calculateRemoveLiquidityInfo(e){let{bins:t,active_id:r,coin_amount:i}=e,s="fix_amount_a"in e,u=t.filter(y=>y.bin_id<r),d=t.filter(y=>y.bin_id>r),f=(0,C.d)(0),c=(0,C.d)(0),p=[];if(s){p=[...t];let{fix_amount_a:y}=e,P=t.find(k=>k.bin_id===r),S=d.reduce((k,x)=>(0,C.d)(k).plus(x.amount_a),(0,C.d)(0)).add(P?.amount_a||"0"),B=u.reduce((k,x)=>(0,C.d)(k).plus(x.amount_b),(0,C.d)(0)).add(P?.amount_b||"0");f=y?S:B,c=(0,C.d)(i).gte(f)?(0,C.d)(1):(0,C.d)(i).div(f)}else{let{is_only_a:y}=e;p=y?d:u,f=p.reduce((P,S)=>(0,C.d)(P).plus(y?S.amount_a:S.amount_b),(0,C.d)(0)),c=(0,C.d)(i).gte(f)?(0,C.d)(1):(0,C.d)(i).div(f)}if((0,C.d)(f).isZero())return $("InsufficientLiquidity",new Error("Insufficient liquidity"),{[C.DETAILS_KEYS.METHOD_NAME]:"calculateRemoveLiquidityResult",[C.DETAILS_KEYS.REQUEST_PARAMS]:e});let _=F.processBinsByRate([...p],c.toFixed());if(_.has_invalid_amount){let y=(0,C.d)(i),P=[],S=s?e.fix_amount_a:e.is_only_a;for(let B of p){if(y.lte(0))break;let k=S?B.amount_a:B.amount_b,x=S?B.amount_b:B.amount_a,R=(0,C.d)((0,C.d)(y).div(k).toFixed(9,L.ROUND_UP));R=R.gt(1)?(0,C.d)(1):R;let T=(0,C.d)(k).mul(R);y=y.minus(T);let I=(0,C.d)(x).mul(R),G=(0,C.d)(B.liquidity).mul(R);P.push({...B,amount_a:S?T.toFixed(0):I.toFixed(0),amount_b:S?I.toFixed(0):T.toFixed(0),liquidity:G.toFixed(0)})}return{bins:P,amount_a:P.reduce((B,k)=>(0,C.d)(B).plus(k.amount_a),(0,C.d)(0)).toFixed(0),amount_b:P.reduce((B,k)=>(0,C.d)(B).plus(k.amount_b),(0,C.d)(0)).toFixed(0)}}return _.bins}async calculateAddLiquidityInfo(e){let t="fix_amount_a"in e,{active_id:r,bin_step:i,lower_bin_id:s,upper_bin_id:u,active_bin_of_pool:d,strategy_type:f,pool_id:c}=e,p;if(t){let{coin_amount:_,fix_amount_a:y}=e;p=Pe.autoFillCoinByStrategyV2(r,i,_,y,s,u,f,d)}else p=Pe.toAmountsBothSideByStrategy(r,i,s,u,e.amount_a,e.amount_b,f,d);if(d&&c){let _=p.bins.findIndex(y=>y.bin_id===r);if(_!==-1){let y=await this._sdk.Pool.getPool(c,!1);if(y){let{fees_a:P,fees_b:S}=Oe.getCompositionFees(d,p.bins[_],y.variable_parameters),B=p.bins[_];B.amount_a=(0,C.d)(B.amount_a).sub(P).toFixed(0),B.amount_b=(0,C.d)(B.amount_b).sub(S).toFixed(0),p.bins[_]=B,console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_a:",P),console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_b:",S)}}}return p}removeLiquidityPayload(e){let{pool_id:t,position_id:r,bin_infos:i,reward_coins:s,slippage:u,coin_type_a:d,coin_type_b:f,active_id:c,collect_fee:p,bin_step:_,remove_percent:y}=e,{dlmm_pool:P}=this.sdk.sdkOptions,{bins:S}=i,B=new he.Transaction;(p||s.length>0)&&this.updatePositionFeeAndRewards({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},B),p&&this.collectFeePayload({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},B),this.collectRewardPayload([{pool_id:t,position_id:r,reward_coins:s,coin_type_a:d,coin_type_b:f}],B);let{versioned_id:k,global_config_id:x}=(0,C.getPackagerConfigs)(P);if(y){let R=(0,C.asUintN)(BigInt(S[0].bin_id)),T=(0,C.asUintN)(BigInt(S[S.length-1].bin_id)),I=Number((0,C.d)(y).mul(1e4).toFixed(0)),[G,H]=B.moveCall({target:`${P.published_at}::pool::remove_liquidity_by_percent`,arguments:[B.object(t),B.object(r),B.pure.u32(Number(R)),B.pure.u32(Number(T)),B.pure.u16(I),B.object(x),B.object(k),B.object(C.CLOCK_ADDRESS)],typeArguments:[d,f]}),Q=C.CoinAssist.fromBalance(G,d,B),tt=C.CoinAssist.fromBalance(H,f,B);B.transferObjects([Q,tt],this.sdk.getSenderAddress())}else{let R=B.pure.vector("u32",S.map(tt=>Number((0,C.asUintN)(BigInt(tt.bin_id))))),T=B.pure.vector("u128",S.map(tt=>tt.liquidity)),[I,G]=B.moveCall({target:`${P.published_at}::pool::remove_liquidity`,arguments:[B.object(t),B.object(r),R,T,B.object(x),B.object(k),B.object(C.CLOCK_ADDRESS)],typeArguments:[d,f]}),H=C.CoinAssist.fromBalance(I,d,B),Q=C.CoinAssist.fromBalance(G,f,B);B.transferObjects([H,Q],this.sdk.getSenderAddress())}return this.validateActiveIdSlippage({pool_id:t,active_id:c,max_price_slippage:u,bin_step:_,coin_type_a:d,coin_type_b:f},B),B}async addLiquidityWithPricePayload(e){let{pool_id:t,bin_infos:r,coin_type_a:i,coin_type_b:s,price_base_coin:u,price:d,lower_price:f,upper_price:c,bin_step:p,strategy_type:_,active_bin_of_pool:y,decimals_a:P,decimals_b:S,use_bin_infos:B,max_price_slippage:k}=e,x,R,T,I=r;if(u==="coin_a")x=F.getBinIdFromPrice(f,p,!1,P,S),R=F.getBinIdFromPrice(c,p,!0,P,S),T=F.getBinIdFromPrice(d,p,!0,P,S);else{x=F.getBinIdFromPrice((0,C.d)(1).div(c).toString(),p,!1,P,S),R=F.getBinIdFromPrice((0,C.d)(1).div(f).toString(),p,!0,P,S),T=F.getBinIdFromPrice((0,C.d)(1).div(d).toString(),p,!1,P,S);let Q={amount_a:r.amount_b,amount_b:r.amount_a,active_id:T,bin_step:p,lower_bin_id:x,upper_bin_id:R,active_bin_of_pool:y,strategy_type:_};I=await this.sdk.Position.calculateAddLiquidityInfo(Q)}let H={pool_id:t,active_id:T,bin_infos:I,coin_type_a:i,coin_type_b:s,lower_bin_id:x,upper_bin_id:R,strategy_type:_,use_bin_infos:B,max_price_slippage:k,bin_step:p};return this.addLiquidityPayload(H)}addLiquidityPayload(e,t){let{pool_id:r,bin_infos:i,coin_type_a:s,coin_type_b:u,active_id:d,strategy_type:f,max_price_slippage:c,bin_step:p,use_bin_infos:_=!1}=e;t=t||new he.Transaction;let y="lower_bin_id"in e,P=[];if(y){let S=F.splitBinLiquidityInfo(i,e.lower_bin_id,e.upper_bin_id);P.push(...S)}else{let S=e.position_id;P.push(i),(e.collect_fee||e.reward_coins.length>0)&&this.updatePositionFeeAndRewards({pool_id:r,position_id:S,coin_type_a:s,coin_type_b:u},t),e.collect_fee&&this.collectFeePayload({pool_id:r,position_id:S,coin_type_a:s,coin_type_b:u},t),e.reward_coins.length>0&&this.collectRewardPayload([{pool_id:r,position_id:S,reward_coins:e.reward_coins,coin_type_a:s,coin_type_b:u}],t)}return P.forEach((S,B)=>{console.log("\u{1F680} ~ PositionModule ~ addLiquidityPayload ~ liquidity_bin:",B,S);let{amount_a:k,amount_b:x,bins:R}=S,T=C.CoinAssist.buildCoinWithBalance(BigInt(k),s,t),I=C.CoinAssist.buildCoinWithBalance(BigInt(x),u,t);_?this.addLiquidityInternal({pool_id:r,coin_type_a:s,coin_type_b:u,active_id:d,liquidity_bin:S,tx:t,coin_a_obj_id:T,coin_b_obj_id:I,position_id:y?void 0:e.position_id,max_price_slippage:c,bin_step:p}):this.addLiquidityStrategyInternal({pool_id:r,coin_type_a:s,coin_type_b:u,active_id:d,liquidity_bin:S,tx:t,max_price_slippage:c,bin_step:p,coin_a_obj_id:T,coin_b_obj_id:I,strategy_type:f,position_id:y?void 0:e.position_id})}),t}addLiquidityStrategyInternal(e){let{max_price_slippage:t,bin_step:r,position_id:i,pool_id:s,coin_type_a:u,coin_type_b:d,active_id:f,liquidity_bin:c,tx:p,coin_a_obj_id:_,coin_b_obj_id:y,strategy_type:P}=e,{dlmm_pool:S,dlmm_router:B}=this.sdk.sdkOptions,{versioned_id:k,global_config_id:x}=(0,C.getPackagerConfigs)(S),{bins:R,amount_a:T,amount_b:I}=c,G=i,H=R[0].bin_id,Q=R[R.length-1].bin_id,tt=(0,C.asUintN)(BigInt(H)),$t=Number((0,C.asUintN)(BigInt(f))),Vt=F.getBinShift(f,r,t),J=Di(P);if(i===void 0){let st=Q-H+1;st>1e3&&$("InvalidBinWidth",new Error("Width is too large"),{[C.DETAILS_KEYS.METHOD_NAME]:"openPosition"}),G=p.moveCall({target:`${B.published_at}::${J}::open_position`,arguments:[typeof s=="string"?p.object(s):s,_,y,p.pure.u64(T),p.pure.u64(I),p.pure.u32(Number(tt)),p.pure.u16(Number(st)),p.pure.u32(Number($t)),p.pure.u32(Number(Vt)),p.object(x),p.object(k),p.object(C.CLOCK_ADDRESS)],typeArguments:[u,d]})}else{let st=R.filter(l=>l.amount_a!=="0"||l.amount_b!=="0");if(st.length===0)return $("InvalidParams",new Error("No bins to add liquidity"),{[C.DETAILS_KEYS.METHOD_NAME]:"addLiquidityStrategyInternal",[C.DETAILS_KEYS.REQUEST_PARAMS]:e});let v=(0,C.asUintN)(BigInt(st[0].bin_id)),o=(0,C.asUintN)(BigInt(st[st.length-1].bin_id));p.moveCall({target:`${B.published_at}::${J}::add_liquidity`,arguments:[typeof s=="string"?p.object(s):s,p.object(i),_,y,p.pure.u64(T),p.pure.u64(I),p.pure.u32(Number(v)),p.pure.u32(Number(o)),p.pure.u32(Number($t)),p.pure.u32(Number(Vt)),p.object(x),p.object(k),p.object(C.CLOCK_ADDRESS)],typeArguments:[u,d]})}return G?p.transferObjects([G,_,y],this.sdk.getSenderAddress()):p.transferObjects([_,y],this.sdk.getSenderAddress()),p}addLiquidityInternal(e){let{position_id:t,pool_id:r,coin_type_a:i,coin_type_b:s,active_id:u,liquidity_bin:d,tx:f,coin_a_obj_id:c,coin_b_obj_id:p,max_price_slippage:_,bin_step:y}=e,{bins:P}=d,{dlmm_pool:S,dlmm_router:B}=this.sdk.sdkOptions,{versioned_id:k,global_config_id:x}=(0,C.getPackagerConfigs)(S),R=f.pure.vector("u64",P.map(Q=>Q.amount_a)),T=f.pure.vector("u64",P.map(Q=>Q.amount_b)),I=f.makeMoveVec({elements:P.map(Q=>f.pure.u32(Number((0,C.asUintN)(BigInt(Q.bin_id))))),type:"u32"}),G=d.bins[0].bin_id,H=d.bins[d.bins.length-1].bin_id;if(t===void 0){H-G+1>1e3&&$("InvalidBinWidth",new Error("Width is too large"),{[C.DETAILS_KEYS.METHOD_NAME]:"openPosition"});let tt=f.moveCall({target:`${B.published_at}::add_liquidity::open_position`,arguments:[typeof r=="string"?f.object(r):r,c,p,I,R,T,f.object(x),f.object(k),f.object(C.CLOCK_ADDRESS)],typeArguments:[i,s]});u>=G&&u<=H&&this.validateActiveIdSlippage({pool_id:r,active_id:u,max_price_slippage:_,bin_step:y,coin_type_a:i,coin_type_b:s},f),f.transferObjects([c,p,tt],this.sdk.getSenderAddress())}else f.moveCall({target:`${B.published_at}::add_liquidity::add_liquidity`,arguments:[typeof r=="string"?f.object(r):r,f.object(t),c,p,I,R,T,f.object(x),f.object(k),f.object(C.CLOCK_ADDRESS)],typeArguments:[i,s]}),u>=G&&u<=H&&this.validateActiveIdSlippage({pool_id:r,active_id:u,max_price_slippage:_,bin_step:y,coin_type_a:i,coin_type_b:s},f),f.transferObjects([c,p],this.sdk.getSenderAddress());return f}async fetchPositionFeeAndReward(e){let t=new he.Transaction;this.collectRewardAndFeePayload(e,t);let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:t,sender:(0,Or.normalizeSuiAddress)("0x0")});if(r.error!=null)return $("FetchError",new Error(r.error),{[C.DETAILS_KEYS.METHOD_NAME]:"fetchPositionFeeAndReward",[C.DETAILS_KEYS.REQUEST_PARAMS]:{options:e,totalOptions:e.length}});let i=Ti(r),s=ki(r);return{feeData:i,rewardData:s}}};var qi=require("@mysten/sui/transactions"),Kt=require("@cetusprotocol/common-sdk");var Ir=require("@mysten/sui/utils");var Ge=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async preSwapQuote(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:i,coin_type_b:s,a2b:u,by_amount_in:d,in_amount:f}=e,{versioned_id:c,global_config_id:p}=(0,Kt.getPackagerConfigs)(t),_=new qi.Transaction;_.moveCall({target:`${t.published_at}::pool::flash_swap`,arguments:[_.object(r),_.pure.bool(u),_.pure.bool(d),_.pure.u64(f),_.object(p),_.object(c),_.object(Kt.CLOCK_ADDRESS)],typeArguments:[i,s]});let y=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:_,sender:(0,Ir.normalizeSuiAddress)("0x0")});if(y.error!=null)return $("FetchError",new Error(y.error),{[Kt.DETAILS_KEYS.METHOD_NAME]:"fetchSwapQuote",[Kt.DETAILS_KEYS.REQUEST_PARAMS]:e});let P=xi(y,u);return P==null?$("FetchError",new Error("No quote info"),{[Kt.DETAILS_KEYS.METHOD_NAME]:"preSwapQuote",[Kt.DETAILS_KEYS.REQUEST_PARAMS]:e}):(P.a2b=u,P)}swapPayload(e){let{dlmm_pool:t,dlmm_router:r}=this._sdk.sdkOptions,{quote_obj:i,coin_type_a:s,coin_type_b:u,by_amount_in:d,slippage:f,partner:c}=e,{pool_id:p,in_amount:_,out_amount:y,a2b:P}=i,S=new qi.Transaction,B=d?_:(0,Kt.d)(_).mul(1+f).toFixed(0),k=d?(0,Kt.d)(y).mul(1-f).toFixed(0):y,x=Kt.CoinAssist.buildCoinWithBalance(BigInt(B),P?s:u,S),{versioned_id:R,global_config_id:T}=(0,Kt.getPackagerConfigs)(t);return console.log("\u{1F680} ~ SwapModule ~ option:",{...e,in_amount_limit:B,out_amount_limit:k}),c?S.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b_with_partner":"swap_b2a_with_partner"}`,arguments:[S.object(p),S.object(c),x,S.pure.bool(d),S.pure.u64(BigInt(d?_:y)),S.pure.u64(BigInt(d?k:B)),S.object(T),S.object(R),S.object(Kt.CLOCK_ADDRESS)],typeArguments:[s,u]}):S.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b":"swap_b2a"}`,arguments:[S.object(p),x,S.pure.bool(d),S.pure.u64(BigInt(d?_:y)),S.pure.u64(BigInt(d?k:B)),S.object(T),S.object(R),S.object(Kt.CLOCK_ADDRESS)],typeArguments:[s,u]}),S.transferObjects([x],this.sdk.getSenderAddress()),S}};var ke=require("@mysten/sui/transactions"),Y=require("@cetusprotocol/common-sdk");var Ve=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getPartnerList(){let e=[],{dlmm_pool:t}=this._sdk.sdkOptions,{partners_id:r}=(0,Y.getPackagerConfigs)(t);try{let i=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}}),u=(0,Y.getObjectFields)(i).partners.fields.contents.map(d=>d.fields.value);u.length>0&&(await this._sdk.FullClient.batchGetObjects(u,{showContent:!0,showType:!0})).forEach(f=>{let c=Ke(f);e.push(c)})}catch(i){return $("FetchError",i,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerList",[Y.DETAILS_KEYS.REQUEST_PARAMS]:r})}return e}async getPartnerCapId(e,t){let{dlmm_pool:r}=this._sdk.sdkOptions;try{let i=`partner_cap_id_${e}_${t}`,s=this._sdk.getCache(i);if(s)return s;let u=await this._sdk.FullClient.getOwnedObjects({owner:e,options:{showContent:!0,showType:!0},filter:{StructType:`${r.package_id}::partner::PartnerCap`}}),d;return u.data.forEach(f=>{let c=(0,Y.getObjectFields)(f);c.partner_id===t&&(d=c.id.id,this._sdk.updateCache(i,d))}),d||$("NotFound",new Error("Partner cap not found"),{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[Y.DETAILS_KEYS.REQUEST_PARAMS]:{owner:e,partner_id:t}})}catch(i){return $("FetchError",i,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[Y.DETAILS_KEYS.REQUEST_PARAMS]:{owner:e,partner_id:t}})}}async getPartnerBalance(e){try{let t=await this._sdk.FullClient.getDynamicFieldsByPage(e),r=[],i=t.data.map(s=>s.objectId);return i.length>0&&(await this._sdk.FullClient.batchGetObjects(i,{showContent:!0,showType:!0})).forEach(u=>{let d=(0,Y.getObjectFields)(u);console.log(d),r.push({coin_type:(0,Y.fixCoinType)(d.name,!1),balance:d.value})}),r}catch(t){return $("FetchError",t,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartnerBalance",[Y.DETAILS_KEYS.REQUEST_PARAMS]:e})}}async getPartner(e){try{let t=await this._sdk.FullClient.getObject({id:e,options:{showContent:!0}});return Ke(t)}catch(t){return $("FetchError",t,{[Y.DETAILS_KEYS.METHOD_NAME]:"getPartner",[Y.DETAILS_KEYS.REQUEST_PARAMS]:e})}}updateRefFeeRatePayload(e,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{partner_id:i,ref_fee_rate:s}=e,{global_config_id:u,versioned_id:d}=(0,Y.getPackagerConfigs)(r);t=t||new ke.Transaction;let f=(0,Y.d)(s).mul(1e4);return f.gt(1e4)?$("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[Y.DETAILS_KEYS.METHOD_NAME]:"updateRefFeeRatePayload",[Y.DETAILS_KEYS.REQUEST_PARAMS]:e}):(t.moveCall({target:`${r.published_at}::partner::update_ref_fee_rate`,arguments:[t.object(i),t.pure.u64(f.toNumber()),t.object(u),t.object(d)],typeArguments:[]}),t)}async claimRefFeePayload(e){let{partner_id:t,partner_cap_id:r,fee_coin_types:i}=e,{dlmm_pool:s}=this._sdk.sdkOptions,{versioned_id:u}=(0,Y.getPackagerConfigs)(s),d=new ke.Transaction,f=r;return f||(f=await this.getPartnerCapId(this._sdk.getSenderAddress(),t)),i.forEach(c=>{d.moveCall({target:`${s.published_at}::partner::claim_ref_fee`,arguments:[d.object(t),d.object(f),d.object(u)],typeArguments:[c]})}),d}updateTimeRangePayload(e,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{partner_id:i,start_time:s,end_time:u}=e,{global_config_id:d,versioned_id:f}=(0,Y.getPackagerConfigs)(r);t=t||new ke.Transaction;let c=s>1e12?Math.floor(s/1e3):s,p=u>1e12?Math.floor(u/1e3):u;return p<=c?$("InvalidParams",new Error("end_time must be greater than start_time"),{[Y.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[Y.DETAILS_KEYS.REQUEST_PARAMS]:e}):(t.moveCall({target:`${r.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(Y.CLOCK_ADDRESS)],typeArguments:[]}),t)}createPartnerPayload(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{name:r,ref_fee_rate:i,start_time:s,end_time:u,recipient:d}=e,{partners_id:f,global_config_id:c,versioned_id:p}=(0,Y.getPackagerConfigs)(t),_=new ke.Transaction,y=(0,Y.d)(i).mul(1e4);if(y.gt(1e4))return $("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[Y.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[Y.DETAILS_KEYS.REQUEST_PARAMS]:e});let P=s>1e12?Math.floor(s/1e3):s,S=u>1e12?Math.floor(u/1e3):u;return S<=P?$("InvalidParams",new Error("end_time must be greater than start_time"),{[Y.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[Y.DETAILS_KEYS.REQUEST_PARAMS]:e}):(_.moveCall({target:`${t.published_at}::partner::create_partner`,arguments:[_.object(f),_.pure.string(r),_.pure.u64(y.toNumber()),_.pure.u64(P),_.pure.u64(S),_.pure.address(d),_.object(c),_.object(p)],typeArguments:[]}),_)}};var Xe=require("@mysten/sui/transactions"),ut=require("@cetusprotocol/common-sdk"),ji=wi(De()),Je=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getRewardPeriodEmission(e,t,r){let i=await this._sdk.FullClient.getDynamicFieldsByPage(e),s=[],u=i.data.map(p=>p.objectId);u.length>0&&(await this._sdk.FullClient.batchGetObjects(u,{showContent:!0})).forEach(_=>{let y=(0,ut.getObjectFields)(_),P=ut.MathUtil.u128ToI128(new ji.default(y.value.fields.value.fields.bits)).toString(),S=y.name,B=new Date(Number(S)*1e3).toLocaleString(),x={emissions_per_second:"0",emissions_per_day:"0",emissions_per:ut.MathUtil.fromX64(new ji.default(P)).toString(),time:S,visualized_time:B};s.push(x)});let d=s.sort((p,_)=>Number(p.time)-Number(_.time)),f=[];f.push({emissions_per_second:t,emissions_per_day:(0,ut.d)(t).mul(3600*24).toString(),emissions_per:"0",time:r.toString(),visualized_time:new Date(r*1e3).toLocaleString()});let c=t;for(let p=0;p<d.length;p++){let _=d[p];if((0,ut.d)(_.time).lte(r))continue;c=(0,ut.d)(c).add((0,ut.d)(_.emissions_per)).toString();let y=(0,ut.d)(c).mul(3600*24).toString();(0,ut.d)(c).lt(0)?(_.emissions_per_second="0",_.emissions_per_day="0"):(_.emissions_per_second=c,_.emissions_per_day=y),f.push(_)}return f}addRewardPayload(e,t){t=t||new Xe.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:s}=(0,ut.getPackagerConfigs)(r),{pool_id:u,reward_coin_type:d,reward_amount:f,start_time_seconds:c,end_time_seconds:p,coin_type_a:_,coin_type_b:y}=e,P=ut.CoinAssist.buildCoinWithBalance(BigInt(f),d,t);return t.pure.option("u64",c),t.moveCall({target:`${r.published_at}::pool::add_reward`,arguments:[t.object(u),P,t.pure.option("u64",c),t.pure.u64(p),t.object(i),t.object(s),t.object(ut.CLOCK_ADDRESS)],typeArguments:[_,y,d]}),t}initRewardPayload(e,t){t=t||new Xe.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:s}=(0,ut.getPackagerConfigs)(r),{pool_id:u,reward_coin_types:d,coin_type_a:f,coin_type_b:c}=e;return d.forEach(p=>{t.moveCall({target:`${r.published_at}::pool::initialize_reward`,arguments:[t.object(u),t.object(i),t.object(s),t.object(ut.CLOCK_ADDRESS)],typeArguments:[f,c,p]})}),t}buildRewardAccessPayload(e,t){t=t||new Xe.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:s}=(0,ut.getPackagerConfigs)(r),{pool_id:u,type:d,coin_type_a:f,coin_type_b:c}=e;return t.moveCall({target:`${r.published_at}::pool::${d==="to_public"?"make_reward_public":"make_reward_private"}`,arguments:[t.object(u),t.object(i),t.object(s)],typeArguments:[f,c]}),t}};var Cr=require("@mysten/sui/transactions"),ee=require("@cetusprotocol/common-sdk");var Ye=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}buildRewardWhiteListPayload(e,t){t=t||new Cr.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:s}=(0,ee.getPackagerConfigs)(r),{reward_coin_types:u,type:d}=e;return u.forEach(f=>{t.moveCall({target:`${r.published_at}::config::${d==="add"?"add_reward_whitelist":"remove_reward_whitelist"}`,arguments:[t.object(i),t.object(s)],typeArguments:[f]})}),t}async getBinStepConfigList(e){let t=await this._sdk.FullClient.getDynamicFields({parentId:e}),r=[],i=t.data.map(s=>s.objectId);return i.length>0&&(await this._sdk.FullClient.batchGetObjects(i,{showContent:!0})).forEach(u=>{let f={...(0,ee.getObjectFields)(u).value.fields};r.push(f)}),r}async getDlmmGlobalConfig(){let{dlmm_pool:e}=this._sdk.sdkOptions,{global_config_id:t}=(0,ee.getPackagerConfigs)(e);try{let r=await this._sdk.FullClient.getObject({id:t,options:{showContent:!0}}),i=(0,ee.getObjectFields)(r),s=i.reward_config.fields,u=s.reward_white_list?.fields?.contents?.map(f=>(0,ee.fixCoinType)(f.fields.key.fields.name,!1))||[];return{id:i.id.id,acl:{id:i.acl.fields.permissions.fields.id.id,size:i.acl.fields.permissions.fields.size},allowed_list:{id:i.allowed_list.fields.id.id,size:i.allowed_list.fields.size},denied_list:{id:i.denied_list.fields.id.id,size:i.denied_list.fields.size},bin_steps:{id:i.bin_steps.fields.id.id,size:i.bin_steps.fields.size},reward_white_list:u,blocked_position:{id:i.restriction.fields.blocked_position.fields.permissions.fields.id.id,size:i.restriction.fields.blocked_position.fields.permissions.fields.size},blocked_user:{id:i.restriction.fields.blocked_user.fields.permissions.fields.id.id,size:i.restriction.fields.blocked_user.fields.permissions.fields.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(r){return console.log("fetchGlobalConfig error: ",r),$("FetchError",r,{[ee.DETAILS_KEYS.METHOD_NAME]:"getBinStepConfigs",[ee.DETAILS_KEYS.REQUEST_PARAMS]:t})}}async fetchDlmmSdkConfigs(){let{dlmm_pool:e}=this._sdk.sdkOptions,t={registry_id:"",pools_id:"",global_config_id:"",versioned_id:"",admin_cap_id:"",partners_id:""},i=(await this._sdk.FullClient.getObject({id:e.package_id,options:{showContent:!0,showPreviousTransaction:!0}})).data?.previousTransaction;(await this._sdk.FullClient.getTransactionBlock({digest:i,options:{showEvents:!0}})).events?.forEach(f=>{let c=f.type,p=f.parsedJson;c.includes("versioned::InitEvent")&&(t.versioned_id=p.versioned),c.includes("partner::InitPartnerEvent")&&(t.partners_id=p.partners_id),c.includes("config::InitEvent")&&(t.global_config_id=p.config_id),c.includes("admin_cap::InitEvent")&&(t.admin_cap_id=p.admin_cap_id),c.includes("registry::RegistryEvent")&&(t.registry_id=p.pools_id)});let u=await this._sdk.FullClient.getObject({id:t.registry_id,options:{showContent:!0}}),d=(0,ee.getObjectFields)(u);return t.pools_id=d.pools.fields.id.id,t}};var xe=class n extends Tr.SdkWrapper{constructor(e){super(e),this._pool=new Te(this),this._position=new ze(this),this._swap=new Ge(this),this._partner=new Ve(this),this._reward=new Je(this),this._config=new Ye(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"?n.createCustomSDK({...vi,...e}):n.createCustomSDK({...Li,...e})}static createCustomSDK(e){return new n(e)}};var Hn=xe;0&&(module.exports={BASIS_POINT,BASIS_POINT_MAX,BIN_BOUND,BinUtils,CetusDlmmSDK,DEFAULT_MAX_WEIGHT,DEFAULT_MIN_WEIGHT,FEE_PRECISION,FeeUtils,MAX_BIN_ID,MAX_BIN_PER_POSITION,MAX_FEE_RATE,MIN_BIN_ID,ONE,PoolModule,REWARD_PERIOD,REWARD_PERIOD_START_AT,SCALE_OFFSET,StrategyType,StrategyUtils,WeightUtils,buildPoolKey,dlmmMainnet,dlmmTestnet,generateRewardSchedule,getRouterModule,parseBinInfo,parseBinInfoList,parseCurrentRewardPeriodEmission,parseDlmmBasePool,parseDlmmPool,parseDlmmPosition,parseLiquidityShares,parsePartner,parsePoolTransactionInfo,parseRewardPeriodEmission,parseStrategyType,parsedDlmmPosFeeData,parsedDlmmPosRewardData,parsedSwapQuoteData,poolFilterEvenTypes,safeAmount,safeMulAmount});
|
|
1
|
+
"use strict";var kr=Object.create;var Re=Object.defineProperty;var xr=Object.getOwnPropertyDescriptor;var Rr=Object.getOwnPropertyNames;var Dr=Object.getPrototypeOf,Fr=Object.prototype.hasOwnProperty;var Nr=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports),Lr=(n,e)=>{for(var t in e)Re(n,t,{get:e[t],enumerable:!0})},nr=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of Rr(e))!Fr.call(n,i)&&i!==t&&Re(n,i,{get:()=>e[i],enumerable:!(r=xr(e,i))||r.enumerable});return n};var wi=(n,e,t)=>(t=n!=null?kr(Dr(n)):{},nr(e||!n||!n.__esModule?Re(t,"default",{value:n,enumerable:!0}):t,n)),qr=n=>nr(Re({},"__esModule",{value:!0}),n);var De=Nr((ar,yi)=>{"use strict";(function(n,e){"use strict";function t(v,o){if(!v)throw new Error(o||"Assertion failed")}function r(v,o){v.super_=o;var l=function(){};l.prototype=o.prototype,v.prototype=new l,v.prototype.constructor=v}function i(v,o,l){if(i.isBN(v))return v;this.negative=0,this.words=null,this.length=0,this.red=null,v!==null&&((o==="le"||o==="be")&&(l=o,o=10),this._init(v||0,o||10,l||"be"))}typeof n=="object"?n.exports=i:e.BN=i,i.BN=i,i.wordSize=26;var s;try{typeof window<"u"&&typeof window.Buffer<"u"?s=window.Buffer:s=require("buffer").Buffer}catch{}i.isBN=function(o){return o instanceof i?!0:o!==null&&typeof o=="object"&&o.constructor.wordSize===i.wordSize&&Array.isArray(o.words)},i.max=function(o,l){return o.cmp(l)>0?o:l},i.min=function(o,l){return o.cmp(l)<0?o:l},i.prototype._init=function(o,l,h){if(typeof o=="number")return this._initNumber(o,l,h);if(typeof o=="object")return this._initArray(o,l,h);l==="hex"&&(l=16),t(l===(l|0)&&l>=2&&l<=36),o=o.toString().replace(/\s+/g,"");var m=0;o[0]==="-"&&(m++,this.negative=1),m<o.length&&(l===16?this._parseHex(o,m,h):(this._parseBase(o,l,m),h==="le"&&this._initArray(this.toArray(),l,h)))},i.prototype._initNumber=function(o,l,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(),l,h)},i.prototype._initArray=function(o,l,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,A=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<<A&67108863,this.words[b+1]=M>>>26-A&67108863,A+=24,A>=26&&(A-=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<<A&67108863,this.words[b+1]=M>>>26-A&67108863,A+=24,A>=26&&(A-=26,b++);return this._strip()};function u(v,o){var l=v.charCodeAt(o);if(l>=48&&l<=57)return l-48;if(l>=65&&l<=70)return l-55;if(l>=97&&l<=102)return l-87;t(!1,"Invalid character in "+v)}function d(v,o,l){var h=u(v,l);return l-1>=o&&(h|=u(v,l-1)<<4),h}i.prototype._parseHex=function(o,l,h){this.length=Math.ceil((o.length-l)/6),this.words=new Array(this.length);for(var m=0;m<this.length;m++)this.words[m]=0;var b=0,M=0,A;if(h==="be")for(m=o.length-1;m>=l;m-=2)A=d(o,l,m)<<b,this.words[M]|=A&67108863,b>=18?(b-=18,M+=1,this.words[M]|=A>>>26):b+=8;else{var g=o.length-l;for(m=g%2===0?l+1:l;m<o.length;m+=2)A=d(o,l,m)<<b,this.words[M]|=A&67108863,b>=18?(b-=18,M+=1,this.words[M]|=A>>>26):b+=8}this._strip()};function f(v,o,l,h){for(var m=0,b=0,M=Math.min(v.length,l),A=o;A<M;A++){var g=v.charCodeAt(A)-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}i.prototype._parseBase=function(o,l,h){this.words=[0],this.length=1;for(var m=0,b=1;b<=67108863;b*=l)m++;m--,b=b/l|0;for(var M=o.length-h,A=M%m,g=Math.min(M,M-A)+h,a=0,w=h;w<g;w+=m)a=f(o,w,w+m,l),this.imuln(b),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a);if(A!==0){var N=1;for(a=f(o,w,o.length,l),w=0;w<A;w++)N*=l;this.imuln(N),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a)}this._strip()},i.prototype.copy=function(o){o.words=new Array(this.length);for(var l=0;l<this.length;l++)o.words[l]=this.words[l];o.length=this.length,o.negative=this.negative,o.red=this.red};function c(v,o){v.words=o.words,v.length=o.length,v.negative=o.negative,v.red=o.red}if(i.prototype._move=function(o){c(o,this)},i.prototype.clone=function(){var o=new i(null);return this.copy(o),o},i.prototype._expand=function(o){for(;this.length<o;)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"],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],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(o,l){o=o||10,l=l|0||1;var h;if(o===16||o==="hex"){h="";for(var m=0,b=0,M=0;M<this.length;M++){var A=this.words[M],g=((A<<m|b)&16777215).toString(16);b=A>>>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%l!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}if(o===(o|0)&&o>=2&&o<=36){var a=y[o],w=P[o];h="";var N=this.clone();for(N.negative=0;!N.isZero();){var q=N.modrn(w).toString(o);N=N.idivn(w),N.isZero()?h=q+h:h=_[a-q.length]+q+h}for(this.isZero()&&(h="0"+h);h.length%l!==0;)h="0"+h;return this.negative!==0&&(h="-"+h),h}t(!1,"Base should be between 2 and 36")},i.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},i.prototype.toJSON=function(){return this.toString(16,2)},s&&(i.prototype.toBuffer=function(o,l){return this.toArrayLike(s,o,l)}),i.prototype.toArray=function(o,l){return this.toArrayLike(Array,o,l)};var S=function(o,l){return o.allocUnsafe?o.allocUnsafe(l):new o(l)};i.prototype.toArrayLike=function(o,l,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=S(o,b),A=l==="le"?"LE":"BE";return this["_toArrayLike"+A](M,m),M},i.prototype._toArrayLikeLE=function(o,l){for(var h=0,m=0,b=0,M=0;b<this.length;b++){var A=this.words[b]<<M|m;o[h++]=A&255,h<o.length&&(o[h++]=A>>8&255),h<o.length&&(o[h++]=A>>16&255),M===6?(h<o.length&&(o[h++]=A>>24&255),m=0,M=0):(m=A>>>24,M+=2)}if(h<o.length)for(o[h++]=m;h<o.length;)o[h++]=0},i.prototype._toArrayLikeBE=function(o,l){for(var h=o.length-1,m=0,b=0,M=0;b<this.length;b++){var A=this.words[b]<<M|m;o[h--]=A&255,h>=0&&(o[h--]=A>>8&255),h>=0&&(o[h--]=A>>16&255),M===6?(h>=0&&(o[h--]=A>>24&255),m=0,M=0):(m=A>>>24,M+=2)}if(h>=0)for(o[h--]=m;h>=0;)o[h--]=0},Math.clz32?i.prototype._countBits=function(o){return 32-Math.clz32(o)}:i.prototype._countBits=function(o){var l=o,h=0;return l>=4096&&(h+=13,l>>>=13),l>=64&&(h+=7,l>>>=7),l>=8&&(h+=4,l>>>=4),l>=2&&(h+=2,l>>>=2),h+l},i.prototype._zeroBits=function(o){if(o===0)return 26;var l=o,h=0;return(l&8191)===0&&(h+=13,l>>>=13),(l&127)===0&&(h+=7,l>>>=7),(l&15)===0&&(h+=4,l>>>=4),(l&3)===0&&(h+=2,l>>>=2),(l&1)===0&&h++,h},i.prototype.bitLength=function(){var o=this.words[this.length-1],l=this._countBits(o);return(this.length-1)*26+l};function B(v){for(var o=new Array(v.bitLength()),l=0;l<o.length;l++){var h=l/26|0,m=l%26;o[l]=v.words[h]>>>m&1}return o}i.prototype.zeroBits=function(){if(this.isZero())return 0;for(var o=0,l=0;l<this.length;l++){var h=this._zeroBits(this.words[l]);if(o+=h,h!==26)break}return o},i.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},i.prototype.toTwos=function(o){return this.negative!==0?this.abs().inotn(o).iaddn(1):this.clone()},i.prototype.fromTwos=function(o){return this.testn(o-1)?this.notn(o).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(o){for(;this.length<o.length;)this.words[this.length++]=0;for(var l=0;l<o.length;l++)this.words[l]=this.words[l]|o.words[l];return this._strip()},i.prototype.ior=function(o){return t((this.negative|o.negative)===0),this.iuor(o)},i.prototype.or=function(o){return this.length>o.length?this.clone().ior(o):o.clone().ior(this)},i.prototype.uor=function(o){return this.length>o.length?this.clone().iuor(o):o.clone().iuor(this)},i.prototype.iuand=function(o){var l;this.length>o.length?l=o:l=this;for(var h=0;h<l.length;h++)this.words[h]=this.words[h]&o.words[h];return this.length=l.length,this._strip()},i.prototype.iand=function(o){return t((this.negative|o.negative)===0),this.iuand(o)},i.prototype.and=function(o){return this.length>o.length?this.clone().iand(o):o.clone().iand(this)},i.prototype.uand=function(o){return this.length>o.length?this.clone().iuand(o):o.clone().iuand(this)},i.prototype.iuxor=function(o){var l,h;this.length>o.length?(l=this,h=o):(l=o,h=this);for(var m=0;m<h.length;m++)this.words[m]=l.words[m]^h.words[m];if(this!==l)for(;m<l.length;m++)this.words[m]=l.words[m];return this.length=l.length,this._strip()},i.prototype.ixor=function(o){return t((this.negative|o.negative)===0),this.iuxor(o)},i.prototype.xor=function(o){return this.length>o.length?this.clone().ixor(o):o.clone().ixor(this)},i.prototype.uxor=function(o){return this.length>o.length?this.clone().iuxor(o):o.clone().iuxor(this)},i.prototype.inotn=function(o){t(typeof o=="number"&&o>=0);var l=Math.ceil(o/26)|0,h=o%26;this._expand(l),h>0&&l--;for(var m=0;m<l;m++)this.words[m]=~this.words[m]&67108863;return h>0&&(this.words[m]=~this.words[m]&67108863>>26-h),this._strip()},i.prototype.notn=function(o){return this.clone().inotn(o)},i.prototype.setn=function(o,l){t(typeof o=="number"&&o>=0);var h=o/26|0,m=o%26;return this._expand(h+1),l?this.words[h]=this.words[h]|1<<m:this.words[h]=this.words[h]&~(1<<m),this._strip()},i.prototype.iadd=function(o){var l;if(this.negative!==0&&o.negative===0)return this.negative=0,l=this.isub(o),this.negative^=1,this._normSign();if(this.negative===0&&o.negative!==0)return o.negative=0,l=this.isub(o),o.negative=1,l._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++)l=(h.words[M]|0)+(m.words[M]|0)+b,this.words[M]=l&67108863,b=l>>>26;for(;b!==0&&M<h.length;M++)l=(h.words[M]|0)+b,this.words[M]=l&67108863,b=l>>>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},i.prototype.add=function(o){var l;return o.negative!==0&&this.negative===0?(o.negative=0,l=this.sub(o),o.negative^=1,l):o.negative===0&&this.negative!==0?(this.negative=0,l=o.sub(this),this.negative=1,l):this.length>o.length?this.clone().iadd(o):o.clone().iadd(this)},i.prototype.isub=function(o){if(o.negative!==0){o.negative=0;var l=this.iadd(o);return o.negative=1,l._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,A=0;A<b.length;A++)l=(m.words[A]|0)-(b.words[A]|0)+M,M=l>>26,this.words[A]=l&67108863;for(;M!==0&&A<m.length;A++)l=(m.words[A]|0)+M,M=l>>26,this.words[A]=l&67108863;if(M===0&&A<m.length&&m!==this)for(;A<m.length;A++)this.words[A]=m.words[A];return this.length=Math.max(this.length,A),m!==this&&(this.negative=1),this._strip()},i.prototype.sub=function(o){return this.clone().isub(o)};function k(v,o,l){l.negative=o.negative^v.negative;var h=v.length+o.length|0;l.length=h,h=h-1|0;var m=v.words[0]|0,b=o.words[0]|0,M=m*b,A=M&67108863,g=M/67108864|0;l.words[0]=A;for(var a=1;a<h;a++){for(var w=g>>>26,N=g&67108863,q=Math.min(a,o.length-1),W=Math.max(0,a-v.length+1);W<=q;W++){var V=a-W|0;m=v.words[V]|0,b=o.words[W]|0,M=m*b+N,w+=M/67108864|0,N=M&67108863}l.words[a]=N|0,g=w|0}return g!==0?l.words[a]=g|0:l.length--,l._strip()}var x=function(o,l,h){var m=o.words,b=l.words,M=h.words,A=0,g,a,w,N=m[0]|0,q=N&8191,W=N>>>13,V=m[1]|0,rt=V&8191,ot=V>>>13,le=m[2]|0,st=le&8191,lt=le>>>13,Wi=m[3]|0,ct=Wi&8191,ht=Wi>>>13,Zi=m[4]|0,pt=Zi&8191,mt=Zi>>>13,Ui=m[5]|0,_t=Ui&8191,gt=Ui>>>13,Hi=m[6]|0,bt=Hi&8191,wt=Hi>>>13,Qi=m[7]|0,vt=Qi&8191,yt=Qi>>>13,Ki=m[8]|0,Mt=Ki&8191,Pt=Ki>>>13,$i=m[9]|0,At=$i&8191,St=$i>>>13,zi=b[0]|0,Bt=zi&8191,Et=zi>>>13,Gi=b[1]|0,Ot=Gi&8191,It=Gi>>>13,Vi=b[2]|0,Ct=Vi&8191,Tt=Vi>>>13,Xi=b[3]|0,kt=Xi&8191,xt=Xi>>>13,Ji=b[4]|0,Rt=Ji&8191,Dt=Ji>>>13,Yi=b[5]|0,Ft=Yi&8191,Nt=Yi>>>13,tr=b[6]|0,Lt=tr&8191,qt=tr>>>13,er=b[7]|0,jt=er&8191,Wt=er>>>13,ir=b[8]|0,Zt=ir&8191,Ut=ir>>>13,rr=b[9]|0,Ht=rr&8191,Qt=rr>>>13;h.negative=o.negative^l.negative,h.length=19,g=Math.imul(q,Bt),a=Math.imul(q,Et),a=a+Math.imul(W,Bt)|0,w=Math.imul(W,Et);var ti=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ti>>>26)|0,ti&=67108863,g=Math.imul(rt,Bt),a=Math.imul(rt,Et),a=a+Math.imul(ot,Bt)|0,w=Math.imul(ot,Et),g=g+Math.imul(q,Ot)|0,a=a+Math.imul(q,It)|0,a=a+Math.imul(W,Ot)|0,w=w+Math.imul(W,It)|0;var ei=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ei>>>26)|0,ei&=67108863,g=Math.imul(st,Bt),a=Math.imul(st,Et),a=a+Math.imul(lt,Bt)|0,w=Math.imul(lt,Et),g=g+Math.imul(rt,Ot)|0,a=a+Math.imul(rt,It)|0,a=a+Math.imul(ot,Ot)|0,w=w+Math.imul(ot,It)|0,g=g+Math.imul(q,Ct)|0,a=a+Math.imul(q,Tt)|0,a=a+Math.imul(W,Ct)|0,w=w+Math.imul(W,Tt)|0;var ii=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ii>>>26)|0,ii&=67108863,g=Math.imul(ct,Bt),a=Math.imul(ct,Et),a=a+Math.imul(ht,Bt)|0,w=Math.imul(ht,Et),g=g+Math.imul(st,Ot)|0,a=a+Math.imul(st,It)|0,a=a+Math.imul(lt,Ot)|0,w=w+Math.imul(lt,It)|0,g=g+Math.imul(rt,Ct)|0,a=a+Math.imul(rt,Tt)|0,a=a+Math.imul(ot,Ct)|0,w=w+Math.imul(ot,Tt)|0,g=g+Math.imul(q,kt)|0,a=a+Math.imul(q,xt)|0,a=a+Math.imul(W,kt)|0,w=w+Math.imul(W,xt)|0;var ri=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ri>>>26)|0,ri&=67108863,g=Math.imul(pt,Bt),a=Math.imul(pt,Et),a=a+Math.imul(mt,Bt)|0,w=Math.imul(mt,Et),g=g+Math.imul(ct,Ot)|0,a=a+Math.imul(ct,It)|0,a=a+Math.imul(ht,Ot)|0,w=w+Math.imul(ht,It)|0,g=g+Math.imul(st,Ct)|0,a=a+Math.imul(st,Tt)|0,a=a+Math.imul(lt,Ct)|0,w=w+Math.imul(lt,Tt)|0,g=g+Math.imul(rt,kt)|0,a=a+Math.imul(rt,xt)|0,a=a+Math.imul(ot,kt)|0,w=w+Math.imul(ot,xt)|0,g=g+Math.imul(q,Rt)|0,a=a+Math.imul(q,Dt)|0,a=a+Math.imul(W,Rt)|0,w=w+Math.imul(W,Dt)|0;var ni=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ni>>>26)|0,ni&=67108863,g=Math.imul(_t,Bt),a=Math.imul(_t,Et),a=a+Math.imul(gt,Bt)|0,w=Math.imul(gt,Et),g=g+Math.imul(pt,Ot)|0,a=a+Math.imul(pt,It)|0,a=a+Math.imul(mt,Ot)|0,w=w+Math.imul(mt,It)|0,g=g+Math.imul(ct,Ct)|0,a=a+Math.imul(ct,Tt)|0,a=a+Math.imul(ht,Ct)|0,w=w+Math.imul(ht,Tt)|0,g=g+Math.imul(st,kt)|0,a=a+Math.imul(st,xt)|0,a=a+Math.imul(lt,kt)|0,w=w+Math.imul(lt,xt)|0,g=g+Math.imul(rt,Rt)|0,a=a+Math.imul(rt,Dt)|0,a=a+Math.imul(ot,Rt)|0,w=w+Math.imul(ot,Dt)|0,g=g+Math.imul(q,Ft)|0,a=a+Math.imul(q,Nt)|0,a=a+Math.imul(W,Ft)|0,w=w+Math.imul(W,Nt)|0;var oi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(oi>>>26)|0,oi&=67108863,g=Math.imul(bt,Bt),a=Math.imul(bt,Et),a=a+Math.imul(wt,Bt)|0,w=Math.imul(wt,Et),g=g+Math.imul(_t,Ot)|0,a=a+Math.imul(_t,It)|0,a=a+Math.imul(gt,Ot)|0,w=w+Math.imul(gt,It)|0,g=g+Math.imul(pt,Ct)|0,a=a+Math.imul(pt,Tt)|0,a=a+Math.imul(mt,Ct)|0,w=w+Math.imul(mt,Tt)|0,g=g+Math.imul(ct,kt)|0,a=a+Math.imul(ct,xt)|0,a=a+Math.imul(ht,kt)|0,w=w+Math.imul(ht,xt)|0,g=g+Math.imul(st,Rt)|0,a=a+Math.imul(st,Dt)|0,a=a+Math.imul(lt,Rt)|0,w=w+Math.imul(lt,Dt)|0,g=g+Math.imul(rt,Ft)|0,a=a+Math.imul(rt,Nt)|0,a=a+Math.imul(ot,Ft)|0,w=w+Math.imul(ot,Nt)|0,g=g+Math.imul(q,Lt)|0,a=a+Math.imul(q,qt)|0,a=a+Math.imul(W,Lt)|0,w=w+Math.imul(W,qt)|0;var si=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(si>>>26)|0,si&=67108863,g=Math.imul(vt,Bt),a=Math.imul(vt,Et),a=a+Math.imul(yt,Bt)|0,w=Math.imul(yt,Et),g=g+Math.imul(bt,Ot)|0,a=a+Math.imul(bt,It)|0,a=a+Math.imul(wt,Ot)|0,w=w+Math.imul(wt,It)|0,g=g+Math.imul(_t,Ct)|0,a=a+Math.imul(_t,Tt)|0,a=a+Math.imul(gt,Ct)|0,w=w+Math.imul(gt,Tt)|0,g=g+Math.imul(pt,kt)|0,a=a+Math.imul(pt,xt)|0,a=a+Math.imul(mt,kt)|0,w=w+Math.imul(mt,xt)|0,g=g+Math.imul(ct,Rt)|0,a=a+Math.imul(ct,Dt)|0,a=a+Math.imul(ht,Rt)|0,w=w+Math.imul(ht,Dt)|0,g=g+Math.imul(st,Ft)|0,a=a+Math.imul(st,Nt)|0,a=a+Math.imul(lt,Ft)|0,w=w+Math.imul(lt,Nt)|0,g=g+Math.imul(rt,Lt)|0,a=a+Math.imul(rt,qt)|0,a=a+Math.imul(ot,Lt)|0,w=w+Math.imul(ot,qt)|0,g=g+Math.imul(q,jt)|0,a=a+Math.imul(q,Wt)|0,a=a+Math.imul(W,jt)|0,w=w+Math.imul(W,Wt)|0;var ai=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ai>>>26)|0,ai&=67108863,g=Math.imul(Mt,Bt),a=Math.imul(Mt,Et),a=a+Math.imul(Pt,Bt)|0,w=Math.imul(Pt,Et),g=g+Math.imul(vt,Ot)|0,a=a+Math.imul(vt,It)|0,a=a+Math.imul(yt,Ot)|0,w=w+Math.imul(yt,It)|0,g=g+Math.imul(bt,Ct)|0,a=a+Math.imul(bt,Tt)|0,a=a+Math.imul(wt,Ct)|0,w=w+Math.imul(wt,Tt)|0,g=g+Math.imul(_t,kt)|0,a=a+Math.imul(_t,xt)|0,a=a+Math.imul(gt,kt)|0,w=w+Math.imul(gt,xt)|0,g=g+Math.imul(pt,Rt)|0,a=a+Math.imul(pt,Dt)|0,a=a+Math.imul(mt,Rt)|0,w=w+Math.imul(mt,Dt)|0,g=g+Math.imul(ct,Ft)|0,a=a+Math.imul(ct,Nt)|0,a=a+Math.imul(ht,Ft)|0,w=w+Math.imul(ht,Nt)|0,g=g+Math.imul(st,Lt)|0,a=a+Math.imul(st,qt)|0,a=a+Math.imul(lt,Lt)|0,w=w+Math.imul(lt,qt)|0,g=g+Math.imul(rt,jt)|0,a=a+Math.imul(rt,Wt)|0,a=a+Math.imul(ot,jt)|0,w=w+Math.imul(ot,Wt)|0,g=g+Math.imul(q,Zt)|0,a=a+Math.imul(q,Ut)|0,a=a+Math.imul(W,Zt)|0,w=w+Math.imul(W,Ut)|0;var li=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(li>>>26)|0,li&=67108863,g=Math.imul(At,Bt),a=Math.imul(At,Et),a=a+Math.imul(St,Bt)|0,w=Math.imul(St,Et),g=g+Math.imul(Mt,Ot)|0,a=a+Math.imul(Mt,It)|0,a=a+Math.imul(Pt,Ot)|0,w=w+Math.imul(Pt,It)|0,g=g+Math.imul(vt,Ct)|0,a=a+Math.imul(vt,Tt)|0,a=a+Math.imul(yt,Ct)|0,w=w+Math.imul(yt,Tt)|0,g=g+Math.imul(bt,kt)|0,a=a+Math.imul(bt,xt)|0,a=a+Math.imul(wt,kt)|0,w=w+Math.imul(wt,xt)|0,g=g+Math.imul(_t,Rt)|0,a=a+Math.imul(_t,Dt)|0,a=a+Math.imul(gt,Rt)|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(ct,Lt)|0,a=a+Math.imul(ct,qt)|0,a=a+Math.imul(ht,Lt)|0,w=w+Math.imul(ht,qt)|0,g=g+Math.imul(st,jt)|0,a=a+Math.imul(st,Wt)|0,a=a+Math.imul(lt,jt)|0,w=w+Math.imul(lt,Wt)|0,g=g+Math.imul(rt,Zt)|0,a=a+Math.imul(rt,Ut)|0,a=a+Math.imul(ot,Zt)|0,w=w+Math.imul(ot,Ut)|0,g=g+Math.imul(q,Ht)|0,a=a+Math.imul(q,Qt)|0,a=a+Math.imul(W,Ht)|0,w=w+Math.imul(W,Qt)|0;var ui=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ui>>>26)|0,ui&=67108863,g=Math.imul(At,Ot),a=Math.imul(At,It),a=a+Math.imul(St,Ot)|0,w=Math.imul(St,It),g=g+Math.imul(Mt,Ct)|0,a=a+Math.imul(Mt,Tt)|0,a=a+Math.imul(Pt,Ct)|0,w=w+Math.imul(Pt,Tt)|0,g=g+Math.imul(vt,kt)|0,a=a+Math.imul(vt,xt)|0,a=a+Math.imul(yt,kt)|0,w=w+Math.imul(yt,xt)|0,g=g+Math.imul(bt,Rt)|0,a=a+Math.imul(bt,Dt)|0,a=a+Math.imul(wt,Rt)|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(ct,jt)|0,a=a+Math.imul(ct,Wt)|0,a=a+Math.imul(ht,jt)|0,w=w+Math.imul(ht,Wt)|0,g=g+Math.imul(st,Zt)|0,a=a+Math.imul(st,Ut)|0,a=a+Math.imul(lt,Zt)|0,w=w+Math.imul(lt,Ut)|0,g=g+Math.imul(rt,Ht)|0,a=a+Math.imul(rt,Qt)|0,a=a+Math.imul(ot,Ht)|0,w=w+Math.imul(ot,Qt)|0;var di=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(di>>>26)|0,di&=67108863,g=Math.imul(At,Ct),a=Math.imul(At,Tt),a=a+Math.imul(St,Ct)|0,w=Math.imul(St,Tt),g=g+Math.imul(Mt,kt)|0,a=a+Math.imul(Mt,xt)|0,a=a+Math.imul(Pt,kt)|0,w=w+Math.imul(Pt,xt)|0,g=g+Math.imul(vt,Rt)|0,a=a+Math.imul(vt,Dt)|0,a=a+Math.imul(yt,Rt)|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(ct,Zt)|0,a=a+Math.imul(ct,Ut)|0,a=a+Math.imul(ht,Zt)|0,w=w+Math.imul(ht,Ut)|0,g=g+Math.imul(st,Ht)|0,a=a+Math.imul(st,Qt)|0,a=a+Math.imul(lt,Ht)|0,w=w+Math.imul(lt,Qt)|0;var fi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(fi>>>26)|0,fi&=67108863,g=Math.imul(At,kt),a=Math.imul(At,xt),a=a+Math.imul(St,kt)|0,w=Math.imul(St,xt),g=g+Math.imul(Mt,Rt)|0,a=a+Math.imul(Mt,Dt)|0,a=a+Math.imul(Pt,Rt)|0,w=w+Math.imul(Pt,Dt)|0,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,g=g+Math.imul(ct,Ht)|0,a=a+Math.imul(ct,Qt)|0,a=a+Math.imul(ht,Ht)|0,w=w+Math.imul(ht,Qt)|0;var ci=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(ci>>>26)|0,ci&=67108863,g=Math.imul(At,Rt),a=Math.imul(At,Dt),a=a+Math.imul(St,Rt)|0,w=Math.imul(St,Dt),g=g+Math.imul(Mt,Ft)|0,a=a+Math.imul(Mt,Nt)|0,a=a+Math.imul(Pt,Ft)|0,w=w+Math.imul(Pt,Nt)|0,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,g=g+Math.imul(pt,Ht)|0,a=a+Math.imul(pt,Qt)|0,a=a+Math.imul(mt,Ht)|0,w=w+Math.imul(mt,Qt)|0;var hi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(hi>>>26)|0,hi&=67108863,g=Math.imul(At,Ft),a=Math.imul(At,Nt),a=a+Math.imul(St,Ft)|0,w=Math.imul(St,Nt),g=g+Math.imul(Mt,Lt)|0,a=a+Math.imul(Mt,qt)|0,a=a+Math.imul(Pt,Lt)|0,w=w+Math.imul(Pt,qt)|0,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,g=g+Math.imul(_t,Ht)|0,a=a+Math.imul(_t,Qt)|0,a=a+Math.imul(gt,Ht)|0,w=w+Math.imul(gt,Qt)|0;var pi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(pi>>>26)|0,pi&=67108863,g=Math.imul(At,Lt),a=Math.imul(At,qt),a=a+Math.imul(St,Lt)|0,w=Math.imul(St,qt),g=g+Math.imul(Mt,jt)|0,a=a+Math.imul(Mt,Wt)|0,a=a+Math.imul(Pt,jt)|0,w=w+Math.imul(Pt,Wt)|0,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,g=g+Math.imul(bt,Ht)|0,a=a+Math.imul(bt,Qt)|0,a=a+Math.imul(wt,Ht)|0,w=w+Math.imul(wt,Qt)|0;var mi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(mi>>>26)|0,mi&=67108863,g=Math.imul(At,jt),a=Math.imul(At,Wt),a=a+Math.imul(St,jt)|0,w=Math.imul(St,Wt),g=g+Math.imul(Mt,Zt)|0,a=a+Math.imul(Mt,Ut)|0,a=a+Math.imul(Pt,Zt)|0,w=w+Math.imul(Pt,Ut)|0,g=g+Math.imul(vt,Ht)|0,a=a+Math.imul(vt,Qt)|0,a=a+Math.imul(yt,Ht)|0,w=w+Math.imul(yt,Qt)|0;var _i=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(_i>>>26)|0,_i&=67108863,g=Math.imul(At,Zt),a=Math.imul(At,Ut),a=a+Math.imul(St,Zt)|0,w=Math.imul(St,Ut),g=g+Math.imul(Mt,Ht)|0,a=a+Math.imul(Mt,Qt)|0,a=a+Math.imul(Pt,Ht)|0,w=w+Math.imul(Pt,Qt)|0;var gi=(A+g|0)+((a&8191)<<13)|0;A=(w+(a>>>13)|0)+(gi>>>26)|0,gi&=67108863,g=Math.imul(At,Ht),a=Math.imul(At,Qt),a=a+Math.imul(St,Ht)|0,w=Math.imul(St,Qt);var bi=(A+g|0)+((a&8191)<<13)|0;return A=(w+(a>>>13)|0)+(bi>>>26)|0,bi&=67108863,M[0]=ti,M[1]=ei,M[2]=ii,M[3]=ri,M[4]=ni,M[5]=oi,M[6]=si,M[7]=ai,M[8]=li,M[9]=ui,M[10]=di,M[11]=fi,M[12]=ci,M[13]=hi,M[14]=pi,M[15]=mi,M[16]=_i,M[17]=gi,M[18]=bi,A!==0&&(M[19]=A,h.length++),h};Math.imul||(x=k);function R(v,o,l){l.negative=o.negative^v.negative,l.length=v.length+o.length;for(var h=0,m=0,b=0;b<l.length-1;b++){var M=m;m=0;for(var A=h&67108863,g=Math.min(b,o.length-1),a=Math.max(0,b-v.length+1);a<=g;a++){var w=b-a,N=v.words[w]|0,q=o.words[a]|0,W=N*q,V=W&67108863;M=M+(W/67108864|0)|0,V=V+A|0,A=V&67108863,M=M+(V>>>26)|0,m+=M>>>26,M&=67108863}l.words[b]=A,h=M,M=m}return h!==0?l.words[b]=h:l.length--,l._strip()}function C(v,o,l){return R(v,o,l)}i.prototype.mulTo=function(o,l){var h,m=this.length+o.length;return this.length===10&&o.length===10?h=x(this,o,l):m<63?h=k(this,o,l):m<1024?h=R(this,o,l):h=C(this,o,l),h};function T(v,o){this.x=v,this.y=o}T.prototype.makeRBT=function(o){for(var l=new Array(o),h=i.prototype._countBits(o)-1,m=0;m<o;m++)l[m]=this.revBin(m,h,o);return l},T.prototype.revBin=function(o,l,h){if(o===0||o===h-1)return o;for(var m=0,b=0;b<l;b++)m|=(o&1)<<l-b-1,o>>=1;return m},T.prototype.permute=function(o,l,h,m,b,M){for(var A=0;A<M;A++)m[A]=l[o[A]],b[A]=h[o[A]]},T.prototype.transform=function(o,l,h,m,b,M){this.permute(M,o,l,h,m,b);for(var A=1;A<b;A<<=1)for(var g=A<<1,a=Math.cos(2*Math.PI/g),w=Math.sin(2*Math.PI/g),N=0;N<b;N+=g)for(var q=a,W=w,V=0;V<A;V++){var rt=h[N+V],ot=m[N+V],le=h[N+V+A],st=m[N+V+A],lt=q*le-W*st;st=q*st+W*le,le=lt,h[N+V]=rt+le,m[N+V]=ot+st,h[N+V+A]=rt-le,m[N+V+A]=ot-st,V!==g&&(lt=a*q-w*W,W=a*W+w*q,q=lt)}},T.prototype.guessLen13b=function(o,l){var h=Math.max(l,o)|1,m=h&1,b=0;for(h=h/2|0;h;h=h>>>1)b++;return 1<<b+1+m},T.prototype.conjugate=function(o,l,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=l[m],l[m]=-l[h-m-1],l[h-m-1]=-b}},T.prototype.normalize13b=function(o,l){for(var h=0,m=0;m<l/2;m++){var b=Math.round(o[2*m+1]/l)*8192+Math.round(o[2*m]/l)+h;o[m]=b&67108863,b<67108864?h=0:h=b/67108864|0}return o},T.prototype.convert13b=function(o,l,h,m){for(var b=0,M=0;M<l;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*l;M<m;++M)h[M]=0;t(b===0),t((b&-8192)===0)},T.prototype.stub=function(o){for(var l=new Array(o),h=0;h<o;h++)l[h]=0;return l},T.prototype.mulp=function(o,l,h){var m=2*this.guessLen13b(o.length,l.length),b=this.makeRBT(m),M=this.stub(m),A=new Array(m),g=new Array(m),a=new Array(m),w=new Array(m),N=new Array(m),q=new Array(m),W=h.words;W.length=m,this.convert13b(o.words,o.length,A,m),this.convert13b(l.words,l.length,w,m),this.transform(A,M,g,a,m,b),this.transform(w,M,N,q,m,b);for(var V=0;V<m;V++){var rt=g[V]*N[V]-a[V]*q[V];a[V]=g[V]*q[V]+a[V]*N[V],g[V]=rt}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^l.negative,h.length=o.length+l.length,h._strip()},i.prototype.mul=function(o){var l=new i(null);return l.words=new Array(this.length+o.length),this.mulTo(o,l)},i.prototype.mulf=function(o){var l=new i(null);return l.words=new Array(this.length+o.length),C(this,o,l)},i.prototype.imul=function(o){return this.clone().mulTo(o,this)},i.prototype.imuln=function(o){var l=o<0;l&&(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,l?this.ineg():this},i.prototype.muln=function(o){return this.clone().imuln(o)},i.prototype.sqr=function(){return this.mul(this)},i.prototype.isqr=function(){return this.imul(this.clone())},i.prototype.pow=function(o){var l=B(o);if(l.length===0)return new i(1);for(var h=this,m=0;m<l.length&&l[m]===0;m++,h=h.sqr());if(++m<l.length)for(var b=h.sqr();m<l.length;m++,b=b.sqr())l[m]!==0&&(h=h.mul(b));return h},i.prototype.iushln=function(o){t(typeof o=="number"&&o>=0);var l=o%26,h=(o-l)/26,m=67108863>>>26-l<<26-l,b;if(l!==0){var M=0;for(b=0;b<this.length;b++){var A=this.words[b]&m,g=(this.words[b]|0)-A<<l;this.words[b]=g|M,M=A>>>26-l}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()},i.prototype.ishln=function(o){return t(this.negative===0),this.iushln(o)},i.prototype.iushrn=function(o,l,h){t(typeof o=="number"&&o>=0);var m;l?m=(l-l%26)/26:m=0;var b=o%26,M=Math.min((o-b)/26,this.length),A=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 N=this.words[a]|0;this.words[a]=w<<26-b|N>>>b,w=N&A}return g&&w!==0&&(g.words[g.length++]=w),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},i.prototype.ishrn=function(o,l,h){return t(this.negative===0),this.iushrn(o,l,h)},i.prototype.shln=function(o){return this.clone().ishln(o)},i.prototype.ushln=function(o){return this.clone().iushln(o)},i.prototype.shrn=function(o){return this.clone().ishrn(o)},i.prototype.ushrn=function(o){return this.clone().iushrn(o)},i.prototype.testn=function(o){t(typeof o=="number"&&o>=0);var l=o%26,h=(o-l)/26,m=1<<l;if(this.length<=h)return!1;var b=this.words[h];return!!(b&m)},i.prototype.imaskn=function(o){t(typeof o=="number"&&o>=0);var l=o%26,h=(o-l)/26;if(t(this.negative===0,"imaskn works only with positive numbers"),this.length<=h)return this;if(l!==0&&h++,this.length=Math.min(h,this.length),l!==0){var m=67108863^67108863>>>l<<l;this.words[this.length-1]&=m}return this._strip()},i.prototype.maskn=function(o){return this.clone().imaskn(o)},i.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)},i.prototype._iaddn=function(o){this.words[0]+=o;for(var l=0;l<this.length&&this.words[l]>=67108864;l++)this.words[l]-=67108864,l===this.length-1?this.words[l+1]=1:this.words[l+1]++;return this.length=Math.max(this.length,l+1),this},i.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 l=0;l<this.length&&this.words[l]<0;l++)this.words[l]+=67108864,this.words[l+1]-=1;return this._strip()},i.prototype.addn=function(o){return this.clone().iaddn(o)},i.prototype.subn=function(o){return this.clone().isubn(o)},i.prototype.iabs=function(){return this.negative=0,this},i.prototype.abs=function(){return this.clone().iabs()},i.prototype._ishlnsubmul=function(o,l,h){var m=o.length+h,b;this._expand(m);var M,A=0;for(b=0;b<o.length;b++){M=(this.words[b+h]|0)+A;var g=(o.words[b]|0)*l;M-=g&67108863,A=(M>>26)-(g/67108864|0),this.words[b+h]=M&67108863}for(;b<this.length-h;b++)M=(this.words[b+h]|0)+A,A=M>>26,this.words[b+h]=M&67108863;if(A===0)return this._strip();for(t(A===-1),A=0,b=0;b<this.length;b++)M=-(this.words[b]|0)+A,A=M>>26,this.words[b]=M&67108863;return this.negative=1,this._strip()},i.prototype._wordDiv=function(o,l){var h=this.length-o.length,m=this.clone(),b=o,M=b.words[b.length-1]|0,A=this._countBits(M);h=26-A,h!==0&&(b=b.ushln(h),m.iushln(h),M=b.words[b.length-1]|0);var g=m.length-b.length,a;if(l!=="mod"){a=new i(null),a.length=g+1,a.words=new Array(a.length);for(var w=0;w<a.length;w++)a.words[w]=0}var N=m.clone()._ishlnsubmul(b,1,g);N.negative===0&&(m=N,a&&(a.words[g]=1));for(var q=g-1;q>=0;q--){var W=(m.words[b.length+q]|0)*67108864+(m.words[b.length+q-1]|0);for(W=Math.min(W/M|0,67108863),m._ishlnsubmul(b,W,q);m.negative!==0;)W--,m.negative=0,m._ishlnsubmul(b,1,q),m.isZero()||(m.negative^=1);a&&(a.words[q]=W)}return a&&a._strip(),m._strip(),l!=="div"&&h!==0&&m.iushrn(h),{div:a||null,mod:m}},i.prototype.divmod=function(o,l,h){if(t(!o.isZero()),this.isZero())return{div:new i(0),mod:new i(0)};var m,b,M;return this.negative!==0&&o.negative===0?(M=this.neg().divmod(o,l),l!=="mod"&&(m=M.div.neg()),l!=="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(),l),l!=="mod"&&(m=M.div.neg()),{div:m,mod:M.mod}):(this.negative&o.negative)!==0?(M=this.neg().divmod(o.neg(),l),l!=="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 i(0),mod:this}:o.length===1?l==="div"?{div:this.divn(o.words[0]),mod:null}:l==="mod"?{div:null,mod:new i(this.modrn(o.words[0]))}:{div:this.divn(o.words[0]),mod:new i(this.modrn(o.words[0]))}:this._wordDiv(o,l)},i.prototype.div=function(o){return this.divmod(o,"div",!1).div},i.prototype.mod=function(o){return this.divmod(o,"mod",!1).mod},i.prototype.umod=function(o){return this.divmod(o,"mod",!0).mod},i.prototype.divRound=function(o){var l=this.divmod(o);if(l.mod.isZero())return l.div;var h=l.div.negative!==0?l.mod.isub(o):l.mod,m=o.ushrn(1),b=o.andln(1),M=h.cmp(m);return M<0||b===1&&M===0?l.div:l.div.negative!==0?l.div.isubn(1):l.div.iaddn(1)},i.prototype.modrn=function(o){var l=o<0;l&&(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 l?-m:m},i.prototype.modn=function(o){return this.modrn(o)},i.prototype.idivn=function(o){var l=o<0;l&&(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(),l?this.ineg():this},i.prototype.divn=function(o){return this.clone().idivn(o)},i.prototype.egcd=function(o){t(o.negative===0),t(!o.isZero());var l=this,h=o.clone();l.negative!==0?l=l.umod(o):l=l.clone();for(var m=new i(1),b=new i(0),M=new i(0),A=new i(1),g=0;l.isEven()&&h.isEven();)l.iushrn(1),h.iushrn(1),++g;for(var a=h.clone(),w=l.clone();!l.isZero();){for(var N=0,q=1;(l.words[0]&q)===0&&N<26;++N,q<<=1);if(N>0)for(l.iushrn(N);N-- >0;)(m.isOdd()||b.isOdd())&&(m.iadd(a),b.isub(w)),m.iushrn(1),b.iushrn(1);for(var W=0,V=1;(h.words[0]&V)===0&&W<26;++W,V<<=1);if(W>0)for(h.iushrn(W);W-- >0;)(M.isOdd()||A.isOdd())&&(M.iadd(a),A.isub(w)),M.iushrn(1),A.iushrn(1);l.cmp(h)>=0?(l.isub(h),m.isub(M),b.isub(A)):(h.isub(l),M.isub(m),A.isub(b))}return{a:M,b:A,gcd:h.iushln(g)}},i.prototype._invmp=function(o){t(o.negative===0),t(!o.isZero());var l=this,h=o.clone();l.negative!==0?l=l.umod(o):l=l.clone();for(var m=new i(1),b=new i(0),M=h.clone();l.cmpn(1)>0&&h.cmpn(1)>0;){for(var A=0,g=1;(l.words[0]&g)===0&&A<26;++A,g<<=1);if(A>0)for(l.iushrn(A);A-- >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);l.cmp(h)>=0?(l.isub(h),m.isub(b)):(h.isub(l),b.isub(m))}var N;return l.cmpn(1)===0?N=m:N=b,N.cmpn(0)<0&&N.iadd(o),N},i.prototype.gcd=function(o){if(this.isZero())return o.abs();if(o.isZero())return this.abs();var l=this.clone(),h=o.clone();l.negative=0,h.negative=0;for(var m=0;l.isEven()&&h.isEven();m++)l.iushrn(1),h.iushrn(1);do{for(;l.isEven();)l.iushrn(1);for(;h.isEven();)h.iushrn(1);var b=l.cmp(h);if(b<0){var M=l;l=h,h=M}else if(b===0||h.cmpn(1)===0)break;l.isub(h)}while(!0);return h.iushln(m)},i.prototype.invm=function(o){return this.egcd(o).a.umod(o)},i.prototype.isEven=function(){return(this.words[0]&1)===0},i.prototype.isOdd=function(){return(this.words[0]&1)===1},i.prototype.andln=function(o){return this.words[0]&o},i.prototype.bincn=function(o){t(typeof o=="number");var l=o%26,h=(o-l)/26,m=1<<l;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 A=this.words[M]|0;A+=b,b=A>>>26,A&=67108863,this.words[M]=A}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(o){var l=o<0;if(this.negative!==0&&!l)return-1;if(this.negative===0&&l)return 1;this._strip();var h;if(this.length>1)h=1;else{l&&(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},i.prototype.cmp=function(o){if(this.negative!==0&&o.negative===0)return-1;if(this.negative===0&&o.negative!==0)return 1;var l=this.ucmp(o);return this.negative!==0?-l|0:l},i.prototype.ucmp=function(o){if(this.length>o.length)return 1;if(this.length<o.length)return-1;for(var l=0,h=this.length-1;h>=0;h--){var m=this.words[h]|0,b=o.words[h]|0;if(m!==b){m<b?l=-1:m>b&&(l=1);break}}return l},i.prototype.gtn=function(o){return this.cmpn(o)===1},i.prototype.gt=function(o){return this.cmp(o)===1},i.prototype.gten=function(o){return this.cmpn(o)>=0},i.prototype.gte=function(o){return this.cmp(o)>=0},i.prototype.ltn=function(o){return this.cmpn(o)===-1},i.prototype.lt=function(o){return this.cmp(o)===-1},i.prototype.lten=function(o){return this.cmpn(o)<=0},i.prototype.lte=function(o){return this.cmp(o)<=0},i.prototype.eqn=function(o){return this.cmpn(o)===0},i.prototype.eq=function(o){return this.cmp(o)===0},i.red=function(o){return new X(o)},i.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)},i.prototype.fromRed=function(){return t(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},i.prototype._forceRed=function(o){return this.red=o,this},i.prototype.forceRed=function(o){return t(!this.red,"Already a number in reduction context"),this._forceRed(o)},i.prototype.redAdd=function(o){return t(this.red,"redAdd works only with red numbers"),this.red.add(this,o)},i.prototype.redIAdd=function(o){return t(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,o)},i.prototype.redSub=function(o){return t(this.red,"redSub works only with red numbers"),this.red.sub(this,o)},i.prototype.redISub=function(o){return t(this.red,"redISub works only with red numbers"),this.red.isub(this,o)},i.prototype.redShl=function(o){return t(this.red,"redShl works only with red numbers"),this.red.shl(this,o)},i.prototype.redMul=function(o){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,o),this.red.mul(this,o)},i.prototype.redIMul=function(o){return t(this.red,"redMul works only with red numbers"),this.red._verify2(this,o),this.red.imul(this,o)},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(o){return t(this.red&&!o.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,o)};var z={k256:null,p224:null,p192:null,p25519:null};function U(v,o){this.name=v,this.p=new i(o,16),this.n=this.p.bitLength(),this.k=new i(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}U.prototype._tmp=function(){var o=new i(null);return o.words=new Array(Math.ceil(this.n/13)),o},U.prototype.ireduce=function(o){var l=o,h;do this.split(l,this.tmp),l=this.imulK(l),l=l.iadd(this.tmp),h=l.bitLength();while(h>this.n);var m=h<this.n?-1:l.ucmp(this.p);return m===0?(l.words[0]=0,l.length=1):m>0?l.isub(this.p):l.strip!==void 0?l.strip():l._strip(),l},U.prototype.split=function(o,l){o.iushrn(this.n,0,l)},U.prototype.imulK=function(o){return o.imul(this.k)};function Y(){U.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}r(Y,U),Y.prototype.split=function(o,l){for(var h=4194303,m=Math.min(o.length,9),b=0;b<m;b++)l.words[b]=o.words[b];if(l.length=m,o.length<=9){o.words[0]=0,o.length=1;return}var M=o.words[9];for(l.words[l.length++]=M&h,b=10;b<o.length;b++){var A=o.words[b]|0;o.words[b-10]=(A&h)<<4|M>>>22,M=A}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 l=0,h=0;h<o.length;h++){var m=o.words[h]|0;l+=m*977,o.words[h]=l&67108863,l=m*64+(l/67108864|0)}return o.words[o.length-1]===0&&(o.length--,o.words[o.length-1]===0&&o.length--),o};function et(){U.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}r(et,U);function it(){U.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}r(it,U);function ut(){U.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}r(ut,U),ut.prototype.imulK=function(o){for(var l=0,h=0;h<o.length;h++){var m=(o.words[h]|0)*19+l,b=m&67108863;m>>>=26,o.words[h]=b,l=m}return l!==0&&(o.words[o.length++]=l),o},i._prime=function(o){if(z[o])return z[o];var l;if(o==="k256")l=new Y;else if(o==="p224")l=new et;else if(o==="p192")l=new it;else if(o==="p25519")l=new ut;else throw new Error("Unknown prime "+o);return z[o]=l,l};function X(v){if(typeof v=="string"){var o=i._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,l){t((o.negative|l.negative)===0,"red works only with positives"),t(o.red&&o.red===l.red,"red works only with red numbers")},X.prototype.imod=function(o){return this.prime?this.prime.ireduce(o)._forceRed(this):(c(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,l){this._verify2(o,l);var h=o.add(l);return h.cmp(this.m)>=0&&h.isub(this.m),h._forceRed(this)},X.prototype.iadd=function(o,l){this._verify2(o,l);var h=o.iadd(l);return h.cmp(this.m)>=0&&h.isub(this.m),h},X.prototype.sub=function(o,l){this._verify2(o,l);var h=o.sub(l);return h.cmpn(0)<0&&h.iadd(this.m),h._forceRed(this)},X.prototype.isub=function(o,l){this._verify2(o,l);var h=o.isub(l);return h.cmpn(0)<0&&h.iadd(this.m),h},X.prototype.shl=function(o,l){return this._verify1(o),this.imod(o.ushln(l))},X.prototype.imul=function(o,l){return this._verify2(o,l),this.imod(o.imul(l))},X.prototype.mul=function(o,l){return this._verify2(o,l),this.imod(o.mul(l))},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 l=this.m.andln(3);if(t(l%2===1),l===3){var h=this.m.add(new i(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 i(1).toRed(this),A=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(A)!==0;)a.redIAdd(A);for(var w=this.pow(a,m),N=this.pow(o,m.addn(1).iushrn(1)),q=this.pow(o,m),W=b;q.cmp(M)!==0;){for(var V=q,rt=0;V.cmp(M)!==0;rt++)V=V.redSqr();t(rt<W);var ot=this.pow(w,new i(1).iushln(W-rt-1));N=N.redMul(ot),w=ot.redSqr(),q=q.redMul(w),W=rt}return N},X.prototype.invm=function(o){var l=o._invmp(this.m);return l.negative!==0?(l.negative=0,this.imod(l).redNeg()):this.imod(l)},X.prototype.pow=function(o,l){if(l.isZero())return new i(1).toRed(this);if(l.cmpn(1)===0)return o.clone();var h=4,m=new Array(1<<h);m[0]=new i(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],A=0,g=0,a=l.bitLength()%26;for(a===0&&(a=26),b=l.length-1;b>=0;b--){for(var w=l.words[b],N=a-1;N>=0;N--){var q=w>>N&1;if(M!==m[0]&&(M=this.sqr(M)),q===0&&A===0){g=0;continue}A<<=1,A|=q,g++,!(g!==h&&(b!==0||N!==0))&&(M=this.mul(M,m[A]),g=0,A=0)}a=26}return M},X.prototype.convertTo=function(o){var l=o.umod(this.m);return l===o?l.clone():l},X.prototype.convertFrom=function(o){var l=o.clone();return l.red=null,l},i.mont=function(o){return new at(o)};function at(v){X.call(this,v),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)}r(at,X),at.prototype.convertTo=function(o){return this.imod(o.ushln(this.shift))},at.prototype.convertFrom=function(o){var l=this.imod(o.mul(this.rinv));return l.red=null,l},at.prototype.imul=function(o,l){if(o.isZero()||l.isZero())return o.words[0]=0,o.length=1,o;var h=o.imul(l),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)},at.prototype.mul=function(o,l){if(o.isZero()||l.isZero())return new i(0)._forceRed(this);var h=o.mul(l),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)},at.prototype.invm=function(o){var l=this.imod(o._invmp(this.m).mul(this.r2));return l._forceRed(this)}})(typeof yi>"u"||yi,ar)});var Qn={};Lr(Qn,{BASIS_POINT:()=>Yt,BASIS_POINT_MAX:()=>we,BIN_BOUND:()=>_e,BinUtils:()=>F,CetusDlmmSDK:()=>xe,DEFAULT_MAX_WEIGHT:()=>ve,DEFAULT_MIN_WEIGHT:()=>me,FEE_PRECISION:()=>Ei,FeeUtils:()=>Oe,MAX_BIN_ID:()=>Dn,MAX_BIN_PER_POSITION:()=>ae,MAX_FEE_RATE:()=>He,MIN_BIN_ID:()=>Rn,ONE:()=>Qe,PoolModule:()=>Te,REWARD_PERIOD:()=>Fn,REWARD_PERIOD_START_AT:()=>Nn,SCALE_OFFSET:()=>D,StrategyType:()=>Be,StrategyUtils:()=>Pe,WeightUtils:()=>te,buildPoolKey:()=>Fi,default:()=>Hn,dlmmMainnet:()=>vi,dlmmTestnet:()=>Li,generateRewardSchedule:()=>Wn,getRouterModule:()=>Di,parseBinInfo:()=>Ci,parseBinInfoList:()=>Ii,parseCurrentRewardPeriodEmission:()=>Un,parseDlmmBasePool:()=>Oi,parseDlmmPool:()=>Ee,parseDlmmPosition:()=>$e,parseLiquidityShares:()=>qn,parsePartner:()=>Ke,parsePoolTransactionInfo:()=>Ri,parseRewardPeriodEmission:()=>Zn,parseStrategyType:()=>jn,parsedDlmmPosFeeData:()=>Ti,parsedDlmmPosRewardData:()=>ki,parsedSwapQuoteData:()=>xi,poolFilterEvenTypes:()=>Sr,safeAmount:()=>Me,safeMulAmount:()=>re});module.exports=qr(Qn);var Tr=require("@cetusprotocol/common-sdk");var or=require("@cetusprotocol/common-sdk"),vi={env:"mainnet",full_rpc_url:or.FullRpcUrlMainnet,dlmm_pool:{package_id:"0x5664f9d3fd82c84023870cfbda8ea84e14c8dd56ce557ad2116e0668581a682b",published_at:"0x5664f9d3fd82c84023870cfbda8ea84e14c8dd56ce557ad2116e0668581a682b",version:1,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:"0xc65d59c4aa7c9d53836bdbbb2a1418b02be6a351a767c64d6e4b7e96d26356d2",published_at:"0xab76281e46956e18ce7ae3ab876da0b769e21afca90d2dc00b74bb9161720d28",version:1}};var Ie=require("@mysten/sui/transactions"),G=require("@cetusprotocol/common-sdk");var sr=require("@cetusprotocol/common-sdk");var pe=class extends sr.BaseError{constructor(e,t,r){super(e,t||"UnknownError",r)}static isDlmmErrorCode(e,t){return this.isErrorCode(e,t)}},K=(n,e,t)=>{let r={...t,stack:e instanceof Error?e.stack:void 0};throw e instanceof Error?new pe(e.message,n,r):new pe(e,n,r)};var $=require("@cetusprotocol/common-sdk"),Pr=wi(De());var ge=9e15,ce=1e9,Mi="0123456789abcdef",Le="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",qe="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Pi={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-ge,maxE:ge,crypto:!1},fr,se,Q=!0,We="[DecimalError] ",fe=We+"Invalid argument: ",cr=We+"Precision limit exceeded",hr=We+"crypto unavailable",pr="[object Decimal]",Gt=Math.floor,Kt=Math.pow,jr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Wr=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Zr=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,mr=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,ie=1e7,H=7,Ur=9007199254740991,Hr=Le.length-1,Ai=qe.length-1,E={toStringTag:pr};E.absoluteValue=E.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),j(n)};E.ceil=function(){return j(new this.constructor(this),this.e+1,2)};E.clampedTo=E.clamp=function(n,e){var t,r=this,i=r.constructor;if(n=new i(n),e=new i(e),!n.s||!e.s)return new i(NaN);if(n.gt(e))throw Error(fe+e);return t=r.cmp(n),t<0?n:r.cmp(e)>0?e:new i(r)};E.comparedTo=E.cmp=function(n){var e,t,r,i,s=this,u=s.d,d=(n=new s.constructor(n)).d,f=s.s,c=n.s;if(!u||!d)return!f||!c?NaN:f!==c?f:u===d?0:!u^f<0?1:-1;if(!u[0]||!d[0])return u[0]?f:d[0]?-c:0;if(f!==c)return f;if(s.e!==n.e)return s.e>n.e^f<0?1:-1;for(r=u.length,i=d.length,e=0,t=r<i?r:i;e<t;++e)if(u[e]!==d[e])return u[e]>d[e]^f<0?1:-1;return r===i?0:r>i^f<0?1:-1};E.cosine=E.cos=function(){var n,e,t=this,r=t.constructor;return t.d?t.d[0]?(n=r.precision,e=r.rounding,r.precision=n+Math.max(t.e,t.sd())+H,r.rounding=1,t=Qr(r,vr(r,t)),r.precision=n,r.rounding=e,j(se==2||se==3?t.neg():t,n,e,!0)):new r(1):new r(NaN)};E.cubeRoot=E.cbrt=function(){var n,e,t,r,i,s,u,d,f,c,p=this,_=p.constructor;if(!p.isFinite()||p.isZero())return new _(p);for(Q=!1,s=p.s*Kt(p.s*p,1/3),!s||Math.abs(s)==1/0?(t=$t(p.d),n=p.e,(s=(n-t.length+1)%3)&&(t+=s==1||s==-2?"0":"00"),s=Kt(t,1/3),n=Gt((n+1)/3)-(n%3==(n<0?-1:2)),s==1/0?t="5e"+n:(t=s.toExponential(),t=t.slice(0,t.indexOf("e")+1)+n),r=new _(t),r.s=p.s):r=new _(s.toString()),u=(n=_.precision)+3;;)if(d=r,f=d.times(d).times(d),c=f.plus(p),r=nt(c.plus(p).times(d),c.plus(f),u+2,1),$t(d.d).slice(0,u)===(t=$t(r.d)).slice(0,u))if(t=t.slice(u-3,u+1),t=="9999"||!i&&t=="4999"){if(!i&&(j(d,n+1,0),d.times(d).times(d).eq(p))){r=d;break}u+=4,i=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(j(r,n+1,1),e=!r.times(r).times(r).eq(p));break}return Q=!0,j(r,n,_.rounding,e)};E.decimalPlaces=E.dp=function(){var n,e=this.d,t=NaN;if(e){if(n=e.length-1,t=(n-Gt(this.e/H))*H,n=e[n],n)for(;n%10==0;n/=10)t--;t<0&&(t=0)}return t};E.dividedBy=E.div=function(n){return nt(this,new this.constructor(n))};E.dividedToIntegerBy=E.divToInt=function(n){var e=this,t=e.constructor;return j(nt(e,new t(n),0,1,1),t.precision,t.rounding)};E.equals=E.eq=function(n){return this.cmp(n)===0};E.floor=function(){return j(new this.constructor(this),this.e+1,3)};E.greaterThan=E.gt=function(n){return this.cmp(n)>0};E.greaterThanOrEqualTo=E.gte=function(n){var e=this.cmp(n);return e==1||e===0};E.hyperbolicCosine=E.cosh=function(){var n,e,t,r,i,s=this,u=s.constructor,d=new u(1);if(!s.isFinite())return new u(s.s?1/0:NaN);if(s.isZero())return d;t=u.precision,r=u.rounding,u.precision=t+Math.max(s.e,s.sd())+4,u.rounding=1,i=s.d.length,i<32?(n=Math.ceil(i/3),e=(1/Ue(4,n)).toString()):(n=16,e="2.3283064365386962890625e-10"),s=be(u,1,s.times(e),new u(1),!0);for(var f,c=n,p=new u(8);c--;)f=s.times(s),s=d.minus(f.times(p.minus(f.times(p))));return j(s,u.precision=t,u.rounding=r,!0)};E.hyperbolicSine=E.sinh=function(){var n,e,t,r,i=this,s=i.constructor;if(!i.isFinite()||i.isZero())return new s(i);if(e=s.precision,t=s.rounding,s.precision=e+Math.max(i.e,i.sd())+4,s.rounding=1,r=i.d.length,r<3)i=be(s,2,i,i,!0);else{n=1.4*Math.sqrt(r),n=n>16?16:n|0,i=i.times(1/Ue(5,n)),i=be(s,2,i,i,!0);for(var u,d=new s(5),f=new s(16),c=new s(20);n--;)u=i.times(i),i=i.times(d.plus(u.times(f.times(u).plus(c))))}return s.precision=e,s.rounding=t,j(i,e,t,!0)};E.hyperbolicTangent=E.tanh=function(){var n,e,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,e=r.rounding,r.precision=n+7,r.rounding=1,nt(t.sinh(),t.cosh(),r.precision=n,r.rounding=e)):new r(t.s)};E.inverseCosine=E.acos=function(){var n=this,e=n.constructor,t=n.abs().cmp(1),r=e.precision,i=e.rounding;return t!==-1?t===0?n.isNeg()?ne(e,r,i):new e(0):new e(NaN):n.isZero()?ne(e,r+4,i).times(.5):(e.precision=r+6,e.rounding=1,n=new e(1).minus(n).div(n.plus(1)).sqrt().atan(),e.precision=r,e.rounding=i,n.times(2))};E.inverseHyperbolicCosine=E.acosh=function(){var n,e,t=this,r=t.constructor;return t.lte(1)?new r(t.eq(1)?0:NaN):t.isFinite()?(n=r.precision,e=r.rounding,r.precision=n+Math.max(Math.abs(t.e),t.sd())+4,r.rounding=1,Q=!1,t=t.times(t).minus(1).sqrt().plus(t),Q=!0,r.precision=n,r.rounding=e,t.ln()):new r(t)};E.inverseHyperbolicSine=E.asinh=function(){var n,e,t=this,r=t.constructor;return!t.isFinite()||t.isZero()?new r(t):(n=r.precision,e=r.rounding,r.precision=n+2*Math.max(Math.abs(t.e),t.sd())+6,r.rounding=1,Q=!1,t=t.times(t).plus(1).sqrt().plus(t),Q=!0,r.precision=n,r.rounding=e,t.ln())};E.inverseHyperbolicTangent=E.atanh=function(){var n,e,t,r,i=this,s=i.constructor;return i.isFinite()?i.e>=0?new s(i.abs().eq(1)?i.s/0:i.isZero()?i:NaN):(n=s.precision,e=s.rounding,r=i.sd(),Math.max(r,n)<2*-i.e-1?j(new s(i),n,e,!0):(s.precision=t=r-i.e,i=nt(i.plus(1),new s(1).minus(i),t+n,1),s.precision=n+4,s.rounding=1,i=i.ln(),s.precision=n,s.rounding=e,i.times(.5))):new s(NaN)};E.inverseSine=E.asin=function(){var n,e,t,r,i=this,s=i.constructor;return i.isZero()?new s(i):(e=i.abs().cmp(1),t=s.precision,r=s.rounding,e!==-1?e===0?(n=ne(s,t+4,r).times(.5),n.s=i.s,n):new s(NaN):(s.precision=t+6,s.rounding=1,i=i.div(new s(1).minus(i.times(i)).sqrt().plus(1)).atan(),s.precision=t,s.rounding=r,i.times(2)))};E.inverseTangent=E.atan=function(){var n,e,t,r,i,s,u,d,f,c=this,p=c.constructor,_=p.precision,y=p.rounding;if(c.isFinite()){if(c.isZero())return new p(c);if(c.abs().eq(1)&&_+4<=Ai)return u=ne(p,_+4,y).times(.25),u.s=c.s,u}else{if(!c.s)return new p(NaN);if(_+4<=Ai)return u=ne(p,_+4,y).times(.5),u.s=c.s,u}for(p.precision=d=_+10,p.rounding=1,t=Math.min(28,d/H+2|0),n=t;n;--n)c=c.div(c.times(c).plus(1).sqrt().plus(1));for(Q=!1,e=Math.ceil(d/H),r=1,f=c.times(c),u=new p(c),i=c;n!==-1;)if(i=i.times(f),s=u.minus(i.div(r+=2)),i=i.times(f),u=s.plus(i.div(r+=2)),u.d[e]!==void 0)for(n=e;u.d[n]===s.d[n]&&n--;);return t&&(u=u.times(2<<t-1)),Q=!0,j(u,p.precision=_,p.rounding=y,!0)};E.isFinite=function(){return!!this.d};E.isInteger=E.isInt=function(){return!!this.d&&Gt(this.e/H)>this.d.length-2};E.isNaN=function(){return!this.s};E.isNegative=E.isNeg=function(){return this.s<0};E.isPositive=E.isPos=function(){return this.s>0};E.isZero=function(){return!!this.d&&this.d[0]===0};E.lessThan=E.lt=function(n){return this.cmp(n)<0};E.lessThanOrEqualTo=E.lte=function(n){return this.cmp(n)<1};E.logarithm=E.log=function(n){var e,t,r,i,s,u,d,f,c=this,p=c.constructor,_=p.precision,y=p.rounding,P=5;if(n==null)n=new p(10),e=!0;else{if(n=new p(n),t=n.d,n.s<0||!t||!t[0]||n.eq(1))return new p(NaN);e=n.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)s=!0;else{for(i=t[0];i%10===0;)i/=10;s=i!==1}if(Q=!1,d=_+P,u=de(c,d),r=e?je(p,d+10):de(n,d),f=nt(u,r,d,1),Ae(f.d,i=_,y))do if(d+=10,u=de(c,d),r=e?je(p,d+10):de(n,d),f=nt(u,r,d,1),!s){+$t(f.d).slice(i+1,i+15)+1==1e14&&(f=j(f,_+1,0));break}while(Ae(f.d,i+=10,y));return Q=!0,j(f,_,y)};E.minus=E.sub=function(n){var e,t,r,i,s,u,d,f,c,p,_,y,P=this,S=P.constructor;if(n=new S(n),!P.d||!n.d)return!P.s||!n.s?n=new S(NaN):P.d?n.s=-n.s:n=new S(n.d||P.s!==n.s?P:NaN),n;if(P.s!=n.s)return n.s=-n.s,P.plus(n);if(c=P.d,y=n.d,d=S.precision,f=S.rounding,!c[0]||!y[0]){if(y[0])n.s=-n.s;else if(c[0])n=new S(P);else return new S(f===3?-0:0);return Q?j(n,d,f):n}if(t=Gt(n.e/H),p=Gt(P.e/H),c=c.slice(),s=p-t,s){for(_=s<0,_?(e=c,s=-s,u=y.length):(e=y,t=p,u=c.length),r=Math.max(Math.ceil(d/H),u)+2,s>r&&(s=r,e.length=1),e.reverse(),r=s;r--;)e.push(0);e.reverse()}else{for(r=c.length,u=y.length,_=r<u,_&&(u=r),r=0;r<u;r++)if(c[r]!=y[r]){_=c[r]<y[r];break}s=0}for(_&&(e=c,c=y,y=e,n.s=-n.s),u=c.length,r=y.length-u;r>0;--r)c[u++]=0;for(r=y.length;r>s;){if(c[--r]<y[r]){for(i=r;i&&c[--i]===0;)c[i]=ie-1;--c[i],c[r]+=ie}c[r]-=y[r]}for(;c[--u]===0;)c.pop();for(;c[0]===0;c.shift())--t;return c[0]?(n.d=c,n.e=Ze(c,t),Q?j(n,d,f):n):new S(f===3?-0:0)};E.modulo=E.mod=function(n){var e,t=this,r=t.constructor;return n=new r(n),!t.d||!n.s||n.d&&!n.d[0]?new r(NaN):!n.d||t.d&&!t.d[0]?j(new r(t),r.precision,r.rounding):(Q=!1,r.modulo==9?(e=nt(t,n.abs(),0,3,1),e.s*=n.s):e=nt(t,n,0,r.modulo,1),e=e.times(n),Q=!0,t.minus(e))};E.naturalExponential=E.exp=function(){return Si(this)};E.naturalLogarithm=E.ln=function(){return de(this)};E.negated=E.neg=function(){var n=new this.constructor(this);return n.s=-n.s,j(n)};E.plus=E.add=function(n){var e,t,r,i,s,u,d,f,c,p,_=this,y=_.constructor;if(n=new y(n),!_.d||!n.d)return!_.s||!n.s?n=new y(NaN):_.d||(n=new y(n.d||_.s===n.s?_:NaN)),n;if(_.s!=n.s)return n.s=-n.s,_.minus(n);if(c=_.d,p=n.d,d=y.precision,f=y.rounding,!c[0]||!p[0])return p[0]||(n=new y(_)),Q?j(n,d,f):n;if(s=Gt(_.e/H),r=Gt(n.e/H),c=c.slice(),i=s-r,i){for(i<0?(t=c,i=-i,u=p.length):(t=p,r=s,u=c.length),s=Math.ceil(d/H),u=s>u?s+1:u+1,i>u&&(i=u,t.length=1),t.reverse();i--;)t.push(0);t.reverse()}for(u=c.length,i=p.length,u-i<0&&(i=u,t=p,p=c,c=t),e=0;i;)e=(c[--i]=c[i]+p[i]+e)/ie|0,c[i]%=ie;for(e&&(c.unshift(e),++r),u=c.length;c[--u]==0;)c.pop();return n.d=c,n.e=Ze(c,r),Q?j(n,d,f):n};E.precision=E.sd=function(n){var e,t=this;if(n!==void 0&&n!==!!n&&n!==1&&n!==0)throw Error(fe+n);return t.d?(e=_r(t.d),n&&t.e+1>e&&(e=t.e+1)):e=NaN,e};E.round=function(){var n=this,e=n.constructor;return j(new e(n),n.e+1,e.rounding)};E.sine=E.sin=function(){var n,e,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,e=r.rounding,r.precision=n+Math.max(t.e,t.sd())+H,r.rounding=1,t=$r(r,vr(r,t)),r.precision=n,r.rounding=e,j(se>2?t.neg():t,n,e,!0)):new r(NaN)};E.squareRoot=E.sqrt=function(){var n,e,t,r,i,s,u=this,d=u.d,f=u.e,c=u.s,p=u.constructor;if(c!==1||!d||!d[0])return new p(!c||c<0&&(!d||d[0])?NaN:d?u:1/0);for(Q=!1,c=Math.sqrt(+u),c==0||c==1/0?(e=$t(d),(e.length+f)%2==0&&(e+="0"),c=Math.sqrt(e),f=Gt((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),r=new p(e)):r=new p(c.toString()),t=(f=p.precision)+3;;)if(s=r,r=s.plus(nt(u,s,t+2,1)).times(.5),$t(s.d).slice(0,t)===(e=$t(r.d)).slice(0,t))if(e=e.slice(t-3,t+1),e=="9999"||!i&&e=="4999"){if(!i&&(j(s,f+1,0),s.times(s).eq(u))){r=s;break}t+=4,i=1}else{(!+e||!+e.slice(1)&&e.charAt(0)=="5")&&(j(r,f+1,1),n=!r.times(r).eq(u));break}return Q=!0,j(r,f,p.rounding,n)};E.tangent=E.tan=function(){var n,e,t=this,r=t.constructor;return t.isFinite()?t.isZero()?new r(t):(n=r.precision,e=r.rounding,r.precision=n+10,r.rounding=1,t=t.sin(),t.s=1,t=nt(t,new r(1).minus(t.times(t)).sqrt(),n+10,0),r.precision=n,r.rounding=e,j(se==2||se==4?t.neg():t,n,e,!0)):new r(NaN)};E.times=E.mul=function(n){var e,t,r,i,s,u,d,f,c,p=this,_=p.constructor,y=p.d,P=(n=new _(n)).d;if(n.s*=p.s,!y||!y[0]||!P||!P[0])return new _(!n.s||y&&!y[0]&&!P||P&&!P[0]&&!y?NaN:!y||!P?n.s/0:n.s*0);for(t=Gt(p.e/H)+Gt(n.e/H),f=y.length,c=P.length,f<c&&(s=y,y=P,P=s,u=f,f=c,c=u),s=[],u=f+c,r=u;r--;)s.push(0);for(r=c;--r>=0;){for(e=0,i=f+r;i>r;)d=s[i]+P[r]*y[i-r-1]+e,s[i--]=d%ie|0,e=d/ie|0;s[i]=(s[i]+e)%ie|0}for(;!s[--u];)s.pop();return e?++t:s.shift(),n.d=s,n.e=Ze(s,t),Q?j(n,_.precision,_.rounding):n};E.toBinary=function(n,e){return Bi(this,2,n,e)};E.toDecimalPlaces=E.toDP=function(n,e){var t=this,r=t.constructor;return t=new r(t),n===void 0?t:(Xt(n,0,ce),e===void 0?e=r.rounding:Xt(e,0,8),j(t,n+t.e+1,e))};E.toExponential=function(n,e){var t,r=this,i=r.constructor;return n===void 0?t=oe(r,!0):(Xt(n,0,ce),e===void 0?e=i.rounding:Xt(e,0,8),r=j(new i(r),n+1,e),t=oe(r,!0,n+1)),r.isNeg()&&!r.isZero()?"-"+t:t};E.toFixed=function(n,e){var t,r,i=this,s=i.constructor;return n===void 0?t=oe(i):(Xt(n,0,ce),e===void 0?e=s.rounding:Xt(e,0,8),r=j(new s(i),n+i.e+1,e),t=oe(r,!1,n+r.e+1)),i.isNeg()&&!i.isZero()?"-"+t:t};E.toFraction=function(n){var e,t,r,i,s,u,d,f,c,p,_,y,P=this,S=P.d,B=P.constructor;if(!S)return new B(P);if(c=t=new B(1),r=f=new B(0),e=new B(r),s=e.e=_r(S)-P.e-1,u=s%H,e.d[0]=Kt(10,u<0?H+u:u),n==null)n=s>0?e:c;else{if(d=new B(n),!d.isInt()||d.lt(c))throw Error(fe+d);n=d.gt(e)?s>0?e:c:d}for(Q=!1,d=new B($t(S)),p=B.precision,B.precision=s=S.length*H*2;_=nt(d,e,0,1,1),i=t.plus(_.times(r)),i.cmp(n)!=1;)t=r,r=i,i=c,c=f.plus(_.times(i)),f=i,i=e,e=d.minus(_.times(i)),d=i;return i=nt(n.minus(t),r,0,1,1),f=f.plus(i.times(c)),t=t.plus(i.times(r)),f.s=c.s=P.s,y=nt(c,r,s,1).minus(P).abs().cmp(nt(f,t,s,1).minus(P).abs())<1?[c,r]:[f,t],B.precision=p,Q=!0,y};E.toHexadecimal=E.toHex=function(n,e){return Bi(this,16,n,e)};E.toNearest=function(n,e){var t=this,r=t.constructor;if(t=new r(t),n==null){if(!t.d)return t;n=new r(1),e=r.rounding}else{if(n=new r(n),e===void 0?e=r.rounding:Xt(e,0,8),!t.d)return n.s?t:n;if(!n.d)return n.s&&(n.s=t.s),n}return n.d[0]?(Q=!1,t=nt(t,n,0,e,1).times(n),Q=!0,j(t)):(n.s=t.s,t=n),t};E.toNumber=function(){return+this};E.toOctal=function(n,e){return Bi(this,8,n,e)};E.toPower=E.pow=function(n){var e,t,r,i,s,u,d=this,f=d.constructor,c=+(n=new f(n));if(!d.d||!n.d||!d.d[0]||!n.d[0])return new f(Kt(+d,c));if(d=new f(d),d.eq(1))return d;if(r=f.precision,s=f.rounding,n.eq(1))return j(d,r,s);if(e=Gt(n.e/H),e>=n.d.length-1&&(t=c<0?-c:c)<=Ur)return i=gr(f,d,t,r),n.s<0?new f(1).div(i):j(i,r,s);if(u=d.s,u<0){if(e<n.d.length-1)return new f(NaN);if((n.d[e]&1)==0&&(u=1),d.e==0&&d.d[0]==1&&d.d.length==1)return d.s=u,d}return t=Kt(+d,c),e=t==0||!isFinite(t)?Gt(c*(Math.log("0."+$t(d.d))/Math.LN10+d.e+1)):new f(t+"").e,e>f.maxE+1||e<f.minE-1?new f(e>0?u/0:0):(Q=!1,f.rounding=d.s=1,t=Math.min(12,(e+"").length),i=Si(n.times(de(d,r+t)),r),i.d&&(i=j(i,r+5,1),Ae(i.d,r,s)&&(e=r+10,i=j(Si(n.times(de(d,e+t)),e),e+5,1),+$t(i.d).slice(r+1,r+15)+1==1e14&&(i=j(i,r+1,0)))),i.s=u,Q=!0,f.rounding=s,j(i,r,s))};E.toPrecision=function(n,e){var t,r=this,i=r.constructor;return n===void 0?t=oe(r,r.e<=i.toExpNeg||r.e>=i.toExpPos):(Xt(n,1,ce),e===void 0?e=i.rounding:Xt(e,0,8),r=j(new i(r),n,e),t=oe(r,n<=r.e||r.e<=i.toExpNeg,n)),r.isNeg()&&!r.isZero()?"-"+t:t};E.toSignificantDigits=E.toSD=function(n,e){var t=this,r=t.constructor;return n===void 0?(n=r.precision,e=r.rounding):(Xt(n,1,ce),e===void 0?e=r.rounding:Xt(e,0,8)),j(new r(t),n,e)};E.toString=function(){var n=this,e=n.constructor,t=oe(n,n.e<=e.toExpNeg||n.e>=e.toExpPos);return n.isNeg()&&!n.isZero()?"-"+t:t};E.truncated=E.trunc=function(){return j(new this.constructor(this),this.e+1,1)};E.valueOf=E.toJSON=function(){var n=this,e=n.constructor,t=oe(n,n.e<=e.toExpNeg||n.e>=e.toExpPos);return n.isNeg()?"-"+t:t};function $t(n){var e,t,r,i=n.length-1,s="",u=n[0];if(i>0){for(s+=u,e=1;e<i;e++)r=n[e]+"",t=H-r.length,t&&(s+=ue(t)),s+=r;u=n[e],r=u+"",t=H-r.length,t&&(s+=ue(t))}else if(u===0)return"0";for(;u%10===0;)u/=10;return s+u}function Xt(n,e,t){if(n!==~~n||n<e||n>t)throw Error(fe+n)}function Ae(n,e,t,r){var i,s,u,d;for(s=n[0];s>=10;s/=10)--e;return--e<0?(e+=H,i=0):(i=Math.ceil((e+1)/H),e%=H),s=Kt(10,H-e),d=n[i]%s|0,r==null?e<3?(e==0?d=d/100|0:e==1&&(d=d/10|0),u=t<4&&d==99999||t>3&&d==49999||d==5e4||d==0):u=(t<4&&d+1==s||t>3&&d+1==s/2)&&(n[i+1]/s/100|0)==Kt(10,e-2)-1||(d==s/2||d==0)&&(n[i+1]/s/100|0)==0:e<4?(e==0?d=d/1e3|0:e==1?d=d/100|0:e==2&&(d=d/10|0),u=(r||t<4)&&d==9999||!r&&t>3&&d==4999):u=((r||t<4)&&d+1==s||!r&&t>3&&d+1==s/2)&&(n[i+1]/s/1e3|0)==Kt(10,e-3)-1,u}function Fe(n,e,t){for(var r,i=[0],s,u=0,d=n.length;u<d;){for(s=i.length;s--;)i[s]*=e;for(i[0]+=Mi.indexOf(n.charAt(u++)),r=0;r<i.length;r++)i[r]>t-1&&(i[r+1]===void 0&&(i[r+1]=0),i[r+1]+=i[r]/t|0,i[r]%=t)}return i.reverse()}function Qr(n,e){var t,r,i;if(e.isZero())return e;r=e.d.length,r<32?(t=Math.ceil(r/3),i=(1/Ue(4,t)).toString()):(t=16,i="2.3283064365386962890625e-10"),n.precision+=t,e=be(n,1,e.times(i),new n(1));for(var s=t;s--;){var u=e.times(e);e=u.times(u).minus(u).times(8).plus(1)}return n.precision-=t,e}var nt=(function(){function n(r,i,s){var u,d=0,f=r.length;for(r=r.slice();f--;)u=r[f]*i+d,r[f]=u%s|0,d=u/s|0;return d&&r.unshift(d),r}function e(r,i,s,u){var d,f;if(s!=u)f=s>u?1:-1;else for(d=f=0;d<s;d++)if(r[d]!=i[d]){f=r[d]>i[d]?1:-1;break}return f}function t(r,i,s,u){for(var d=0;s--;)r[s]-=d,d=r[s]<i[s]?1:0,r[s]=d*u+r[s]-i[s];for(;!r[0]&&r.length>1;)r.shift()}return function(r,i,s,u,d,f){var c,p,_,y,P,S,B,k,x,R,C,T,z,U,Y,et,it,ut,X,at,v=r.constructor,o=r.s==i.s?1:-1,l=r.d,h=i.d;if(!l||!l[0]||!h||!h[0])return new v(!r.s||!i.s||(l?h&&l[0]==h[0]:!h)?NaN:l&&l[0]==0||!h?o*0:o/0);for(f?(P=1,p=r.e-i.e):(f=ie,P=H,p=Gt(r.e/P)-Gt(i.e/P)),X=h.length,it=l.length,x=new v(o),R=x.d=[],_=0;h[_]==(l[_]||0);_++);if(h[_]>(l[_]||0)&&p--,s==null?(U=s=v.precision,u=v.rounding):d?U=s+(r.e-i.e)+1:U=s,U<0)R.push(1),S=!0;else{if(U=U/P+2|0,_=0,X==1){for(y=0,h=h[0],U++;(_<it||y)&&U--;_++)Y=y*f+(l[_]||0),R[_]=Y/h|0,y=Y%h|0;S=y||_<it}else{for(y=f/(h[0]+1)|0,y>1&&(h=n(h,y,f),l=n(l,y,f),X=h.length,it=l.length),et=X,C=l.slice(0,X),T=C.length;T<X;)C[T++]=0;at=h.slice(),at.unshift(0),ut=h[0],h[1]>=f/2&&++ut;do y=0,c=e(h,C,X,T),c<0?(z=C[0],X!=T&&(z=z*f+(C[1]||0)),y=z/ut|0,y>1?(y>=f&&(y=f-1),B=n(h,y,f),k=B.length,T=C.length,c=e(B,C,k,T),c==1&&(y--,t(B,X<k?at:h,k,f))):(y==0&&(c=y=1),B=h.slice()),k=B.length,k<T&&B.unshift(0),t(C,B,T,f),c==-1&&(T=C.length,c=e(h,C,X,T),c<1&&(y++,t(C,X<T?at:h,T,f))),T=C.length):c===0&&(y++,C=[0]),R[_++]=y,c&&C[0]?C[T++]=l[et]||0:(C=[l[et]],T=1);while((et++<it||C[0]!==void 0)&&U--);S=C[0]!==void 0}R[0]||R.shift()}if(P==1)x.e=p,fr=S;else{for(_=1,y=R[0];y>=10;y/=10)_++;x.e=_+p*P-1,j(x,d?s+x.e+1:s,u,S)}return x}})();function j(n,e,t,r){var i,s,u,d,f,c,p,_,y,P=n.constructor;t:if(e!=null){if(_=n.d,!_)return n;for(i=1,d=_[0];d>=10;d/=10)i++;if(s=e-i,s<0)s+=H,u=e,p=_[y=0],f=p/Kt(10,i-u-1)%10|0;else if(y=Math.ceil((s+1)/H),d=_.length,y>=d)if(r){for(;d++<=y;)_.push(0);p=f=0,i=1,s%=H,u=s-H+1}else break t;else{for(p=d=_[y],i=1;d>=10;d/=10)i++;s%=H,u=s-H+i,f=u<0?0:p/Kt(10,i-u-1)%10|0}if(r=r||e<0||_[y+1]!==void 0||(u<0?p:p%Kt(10,i-u-1)),c=t<4?(f||r)&&(t==0||t==(n.s<0?3:2)):f>5||f==5&&(t==4||r||t==6&&(s>0?u>0?p/Kt(10,i-u):0:_[y-1])%10&1||t==(n.s<0?8:7)),e<1||!_[0])return _.length=0,c?(e-=n.e+1,_[0]=Kt(10,(H-e%H)%H),n.e=-e||0):_[0]=n.e=0,n;if(s==0?(_.length=y,d=1,y--):(_.length=y+1,d=Kt(10,H-s),_[y]=u>0?(p/Kt(10,i-u)%Kt(10,u)|0)*d:0),c)for(;;)if(y==0){for(s=1,u=_[0];u>=10;u/=10)s++;for(u=_[0]+=d,d=1;u>=10;u/=10)d++;s!=d&&(n.e++,_[0]==ie&&(_[0]=1));break}else{if(_[y]+=d,_[y]!=ie)break;_[y--]=0,d=1}for(s=_.length;_[--s]===0;)_.pop()}return Q&&(n.e>P.maxE?(n.d=null,n.e=NaN):n.e<P.minE&&(n.e=0,n.d=[0])),n}function oe(n,e,t){if(!n.isFinite())return wr(n);var r,i=n.e,s=$t(n.d),u=s.length;return e?(t&&(r=t-u)>0?s=s.charAt(0)+"."+s.slice(1)+ue(r):u>1&&(s=s.charAt(0)+"."+s.slice(1)),s=s+(n.e<0?"e":"e+")+n.e):i<0?(s="0."+ue(-i-1)+s,t&&(r=t-u)>0&&(s+=ue(r))):i>=u?(s+=ue(i+1-u),t&&(r=t-i-1)>0&&(s=s+"."+ue(r))):((r=i+1)<u&&(s=s.slice(0,r)+"."+s.slice(r)),t&&(r=t-u)>0&&(i+1===u&&(s+="."),s+=ue(r))),s}function Ze(n,e){var t=n[0];for(e*=H;t>=10;t/=10)e++;return e}function je(n,e,t){if(e>Hr)throw Q=!0,t&&(n.precision=t),Error(cr);return j(new n(Le),e,1,!0)}function ne(n,e,t){if(e>Ai)throw Error(cr);return j(new n(qe),e,t,!0)}function _r(n){var e=n.length-1,t=e*H+1;if(e=n[e],e){for(;e%10==0;e/=10)t--;for(e=n[0];e>=10;e/=10)t++}return t}function ue(n){for(var e="";n--;)e+="0";return e}function gr(n,e,t,r){var i,s=new n(1),u=Math.ceil(r/H+4);for(Q=!1;;){if(t%2&&(s=s.times(e),ur(s.d,u)&&(i=!0)),t=Gt(t/2),t===0){t=s.d.length-1,i&&s.d[t]===0&&++s.d[t];break}e=e.times(e),ur(e.d,u)}return Q=!0,s}function lr(n){return n.d[n.d.length-1]&1}function br(n,e,t){for(var r,i,s=new n(e[0]),u=0;++u<e.length;){if(i=new n(e[u]),!i.s){s=i;break}r=s.cmp(i),(r===t||r===0&&s.s===t)&&(s=i)}return s}function Si(n,e){var t,r,i,s,u,d,f,c=0,p=0,_=0,y=n.constructor,P=y.rounding,S=y.precision;if(!n.d||!n.d[0]||n.e>17)return new y(n.d?n.d[0]?n.s<0?0:1/0:1:n.s?n.s<0?0:n:NaN);for(e==null?(Q=!1,f=S):f=e,d=new y(.03125);n.e>-2;)n=n.times(d),_+=5;for(r=Math.log(Kt(2,_))/Math.LN10*2+5|0,f+=r,t=s=u=new y(1),y.precision=f;;){if(s=j(s.times(n),f,1),t=t.times(++p),d=u.plus(nt(s,t,f,1)),$t(d.d).slice(0,f)===$t(u.d).slice(0,f)){for(i=_;i--;)u=j(u.times(u),f,1);if(e==null)if(c<3&&Ae(u.d,f-r,P,c))y.precision=f+=10,t=s=d=new y(1),p=0,c++;else return j(u,y.precision=S,P,Q=!0);else return y.precision=S,u}u=d}}function de(n,e){var t,r,i,s,u,d,f,c,p,_,y,P=1,S=10,B=n,k=B.d,x=B.constructor,R=x.rounding,C=x.precision;if(B.s<0||!k||!k[0]||!B.e&&k[0]==1&&k.length==1)return new x(k&&!k[0]?-1/0:B.s!=1?NaN:k?0:B);if(e==null?(Q=!1,p=C):p=e,x.precision=p+=S,t=$t(k),r=t.charAt(0),Math.abs(s=B.e)<15e14){for(;r<7&&r!=1||r==1&&t.charAt(1)>3;)B=B.times(n),t=$t(B.d),r=t.charAt(0),P++;s=B.e,r>1?(B=new x("0."+t),s++):B=new x(r+"."+t.slice(1))}else return c=je(x,p+2,C).times(s+""),B=de(new x(r+"."+t.slice(1)),p-S).plus(c),x.precision=C,e==null?j(B,C,R,Q=!0):B;for(_=B,f=u=B=nt(B.minus(1),B.plus(1),p,1),y=j(B.times(B),p,1),i=3;;){if(u=j(u.times(y),p,1),c=f.plus(nt(u,new x(i),p,1)),$t(c.d).slice(0,p)===$t(f.d).slice(0,p))if(f=f.times(2),s!==0&&(f=f.plus(je(x,p+2,C).times(s+""))),f=nt(f,new x(P),p,1),e==null)if(Ae(f.d,p-S,R,d))x.precision=p+=S,c=u=B=nt(_.minus(1),_.plus(1),p,1),y=j(B.times(B),p,1),i=d=1;else return j(f,x.precision=C,R,Q=!0);else return x.precision=C,f;f=c,i+=2}}function wr(n){return String(n.s*n.s/0)}function Ne(n,e){var t,r,i;for((t=e.indexOf("."))>-1&&(e=e.replace(".","")),(r=e.search(/e/i))>0?(t<0&&(t=r),t+=+e.slice(r+1),e=e.substring(0,r)):t<0&&(t=e.length),r=0;e.charCodeAt(r)===48;r++);for(i=e.length;e.charCodeAt(i-1)===48;--i);if(e=e.slice(r,i),e){if(i-=r,n.e=t=t-r-1,n.d=[],r=(t+1)%H,t<0&&(r+=H),r<i){for(r&&n.d.push(+e.slice(0,r)),i-=H;r<i;)n.d.push(+e.slice(r,r+=H));e=e.slice(r),r=H-e.length}else r-=i;for(;r--;)e+="0";n.d.push(+e),Q&&(n.e>n.constructor.maxE?(n.d=null,n.e=NaN):n.e<n.constructor.minE&&(n.e=0,n.d=[0]))}else n.e=0,n.d=[0];return n}function Kr(n,e){var t,r,i,s,u,d,f,c,p;if(e.indexOf("_")>-1){if(e=e.replace(/(\d)_(?=\d)/g,"$1"),mr.test(e))return Ne(n,e)}else if(e==="Infinity"||e==="NaN")return+e||(n.s=NaN),n.e=NaN,n.d=null,n;if(Wr.test(e))t=16,e=e.toLowerCase();else if(jr.test(e))t=2;else if(Zr.test(e))t=8;else throw Error(fe+e);for(s=e.search(/p/i),s>0?(f=+e.slice(s+1),e=e.substring(2,s)):e=e.slice(2),s=e.indexOf("."),u=s>=0,r=n.constructor,u&&(e=e.replace(".",""),d=e.length,s=d-s,i=gr(r,new r(t),s,s*2)),c=Fe(e,t,ie),p=c.length-1,s=p;c[s]===0;--s)c.pop();return s<0?new r(n.s*0):(n.e=Ze(c,p),n.d=c,Q=!1,u&&(n=nt(n,i,d*4)),f&&(n=n.times(Math.abs(f)<54?Kt(2,f):Se.pow(2,f))),Q=!0,n)}function $r(n,e){var t,r=e.d.length;if(r<3)return e.isZero()?e:be(n,2,e,e);t=1.4*Math.sqrt(r),t=t>16?16:t|0,e=e.times(1/Ue(5,t)),e=be(n,2,e,e);for(var i,s=new n(5),u=new n(16),d=new n(20);t--;)i=e.times(e),e=e.times(s.plus(i.times(u.times(i).minus(d))));return e}function be(n,e,t,r,i){var s,u,d,f,c=1,p=n.precision,_=Math.ceil(p/H);for(Q=!1,f=t.times(t),d=new n(r);;){if(u=nt(d.times(f),new n(e++*e++),p,1),d=i?r.plus(u):r.minus(u),r=nt(u.times(f),new n(e++*e++),p,1),u=d.plus(r),u.d[_]!==void 0){for(s=_;u.d[s]===d.d[s]&&s--;);if(s==-1)break}s=d,d=r,r=u,u=s,c++}return Q=!0,u.d.length=_+1,u}function Ue(n,e){for(var t=n;--e;)t*=n;return t}function vr(n,e){var t,r=e.s<0,i=ne(n,n.precision,1),s=i.times(.5);if(e=e.abs(),e.lte(s))return se=r?4:1,e;if(t=e.divToInt(i),t.isZero())se=r?3:2;else{if(e=e.minus(t.times(i)),e.lte(s))return se=lr(t)?r?2:3:r?4:1,e;se=lr(t)?r?1:4:r?3:2}return e.minus(i).abs()}function Bi(n,e,t,r){var i,s,u,d,f,c,p,_,y,P=n.constructor,S=t!==void 0;if(S?(Xt(t,1,ce),r===void 0?r=P.rounding:Xt(r,0,8)):(t=P.precision,r=P.rounding),!n.isFinite())p=wr(n);else{for(p=oe(n),u=p.indexOf("."),S?(i=2,e==16?t=t*4-3:e==8&&(t=t*3-2)):i=e,u>=0&&(p=p.replace(".",""),y=new P(1),y.e=p.length-u,y.d=Fe(oe(y),10,i),y.e=y.d.length),_=Fe(p,10,i),s=f=_.length;_[--f]==0;)_.pop();if(!_[0])p=S?"0p+0":"0";else{if(u<0?s--:(n=new P(n),n.d=_,n.e=s,n=nt(n,y,t,r,0,i),_=n.d,s=n.e,c=fr),u=_[t],d=i/2,c=c||_[t+1]!==void 0,c=r<4?(u!==void 0||c)&&(r===0||r===(n.s<0?3:2)):u>d||u===d&&(r===4||c||r===6&&_[t-1]&1||r===(n.s<0?8:7)),_.length=t,c)for(;++_[--t]>i-1;)_[t]=0,t||(++s,_.unshift(1));for(f=_.length;!_[f-1];--f);for(u=0,p="";u<f;u++)p+=Mi.charAt(_[u]);if(S){if(f>1)if(e==16||e==8){for(u=e==16?4:3,--f;f%u;f++)p+="0";for(_=Fe(p,i,e),f=_.length;!_[f-1];--f);for(u=1,p="1.";u<f;u++)p+=Mi.charAt(_[u])}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>f)for(s-=f;s--;)p+="0";else s<f&&(p=p.slice(0,s)+"."+p.slice(s))}p=(e==16?"0x":e==2?"0b":e==8?"0o":"")+p}return n.s<0?"-"+p:p}function ur(n,e){if(n.length>e)return n.length=e,!0}function zr(n){return new this(n).abs()}function Gr(n){return new this(n).acos()}function Vr(n){return new this(n).acosh()}function Xr(n,e){return new this(n).plus(e)}function Jr(n){return new this(n).asin()}function Yr(n){return new this(n).asinh()}function tn(n){return new this(n).atan()}function en(n){return new this(n).atanh()}function rn(n,e){n=new this(n),e=new this(e);var t,r=this.precision,i=this.rounding,s=r+4;return!n.s||!e.s?t=new this(NaN):!n.d&&!e.d?(t=ne(this,s,1).times(e.s>0?.25:.75),t.s=n.s):!e.d||n.isZero()?(t=e.s<0?ne(this,r,i):new this(0),t.s=n.s):!n.d||e.isZero()?(t=ne(this,s,1).times(.5),t.s=n.s):e.s<0?(this.precision=s,this.rounding=1,t=this.atan(nt(n,e,s,1)),e=ne(this,s,1),this.precision=r,this.rounding=i,t=n.s<0?t.minus(e):t.plus(e)):t=this.atan(nt(n,e,s,1)),t}function nn(n){return new this(n).cbrt()}function on(n){return j(n=new this(n),n.e+1,2)}function sn(n,e,t){return new this(n).clamp(e,t)}function an(n){if(!n||typeof n!="object")throw Error(We+"Object expected");var e,t,r,i=n.defaults===!0,s=["precision",1,ce,"rounding",0,8,"toExpNeg",-ge,0,"toExpPos",0,ge,"maxE",0,ge,"minE",-ge,0,"modulo",0,9];for(e=0;e<s.length;e+=3)if(t=s[e],i&&(this[t]=Pi[t]),(r=n[t])!==void 0)if(Gt(r)===r&&r>=s[e+1]&&r<=s[e+2])this[t]=r;else throw Error(fe+t+": "+r);if(t="crypto",i&&(this[t]=Pi[t]),(r=n[t])!==void 0)if(r===!0||r===!1||r===0||r===1)if(r)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[t]=!0;else throw Error(hr);else this[t]=!1;else throw Error(fe+t+": "+r);return this}function ln(n){return new this(n).cos()}function un(n){return new this(n).cosh()}function yr(n){var e,t,r;function i(s){var u,d,f,c=this;if(!(c instanceof i))return new i(s);if(c.constructor=i,dr(s)){c.s=s.s,Q?!s.d||s.e>i.maxE?(c.e=NaN,c.d=null):s.e<i.minE?(c.e=0,c.d=[0]):(c.e=s.e,c.d=s.d.slice()):(c.e=s.e,c.d=s.d?s.d.slice():s.d);return}if(f=typeof s,f==="number"){if(s===0){c.s=1/s<0?-1:1,c.e=0,c.d=[0];return}if(s<0?(s=-s,c.s=-1):c.s=1,s===~~s&&s<1e7){for(u=0,d=s;d>=10;d/=10)u++;Q?u>i.maxE?(c.e=NaN,c.d=null):u<i.minE?(c.e=0,c.d=[0]):(c.e=u,c.d=[s]):(c.e=u,c.d=[s]);return}if(s*0!==0){s||(c.s=NaN),c.e=NaN,c.d=null;return}return Ne(c,s.toString())}if(f==="string")return(d=s.charCodeAt(0))===45?(s=s.slice(1),c.s=-1):(d===43&&(s=s.slice(1)),c.s=1),mr.test(s)?Ne(c,s):Kr(c,s);if(f==="bigint")return s<0?(s=-s,c.s=-1):c.s=1,Ne(c,s.toString());throw Error(fe+s)}if(i.prototype=E,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=an,i.clone=yr,i.isDecimal=dr,i.abs=zr,i.acos=Gr,i.acosh=Vr,i.add=Xr,i.asin=Jr,i.asinh=Yr,i.atan=tn,i.atanh=en,i.atan2=rn,i.cbrt=nn,i.ceil=on,i.clamp=sn,i.cos=ln,i.cosh=un,i.div=dn,i.exp=fn,i.floor=cn,i.hypot=hn,i.ln=pn,i.log=mn,i.log10=gn,i.log2=_n,i.max=bn,i.min=wn,i.mod=vn,i.mul=yn,i.pow=Mn,i.random=Pn,i.round=An,i.sign=Sn,i.sin=Bn,i.sinh=En,i.sqrt=On,i.sub=In,i.sum=Cn,i.tan=Tn,i.tanh=kn,i.trunc=xn,n===void 0&&(n={}),n&&n.defaults!==!0)for(r=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],e=0;e<r.length;)n.hasOwnProperty(t=r[e++])||(n[t]=this[t]);return i.config(n),i}function dn(n,e){return new this(n).div(e)}function fn(n){return new this(n).exp()}function cn(n){return j(n=new this(n),n.e+1,3)}function hn(){var n,e,t=new this(0);for(Q=!1,n=0;n<arguments.length;)if(e=new this(arguments[n++]),e.d)t.d&&(t=t.plus(e.times(e)));else{if(e.s)return Q=!0,new this(1/0);t=e}return Q=!0,t.sqrt()}function dr(n){return n instanceof Se||n&&n.toStringTag===pr||!1}function pn(n){return new this(n).ln()}function mn(n,e){return new this(n).log(e)}function _n(n){return new this(n).log(2)}function gn(n){return new this(n).log(10)}function bn(){return br(this,arguments,-1)}function wn(){return br(this,arguments,1)}function vn(n,e){return new this(n).mod(e)}function yn(n,e){return new this(n).mul(e)}function Mn(n,e){return new this(n).pow(e)}function Pn(n){var e,t,r,i,s=0,u=new this(1),d=[];if(n===void 0?n=this.precision:Xt(n,1,ce),r=Math.ceil(n/H),this.crypto)if(crypto.getRandomValues)for(e=crypto.getRandomValues(new Uint32Array(r));s<r;)i=e[s],i>=429e7?e[s]=crypto.getRandomValues(new Uint32Array(1))[0]:d[s++]=i%1e7;else if(crypto.randomBytes){for(e=crypto.randomBytes(r*=4);s<r;)i=e[s]+(e[s+1]<<8)+(e[s+2]<<16)+((e[s+3]&127)<<24),i>=214e7?crypto.randomBytes(4).copy(e,s):(d.push(i%1e7),s+=4);s=r/4}else throw Error(hr);else for(;s<r;)d[s++]=Math.random()*1e7|0;for(r=d[--s],n%=H,r&&n&&(i=Kt(10,H-n),d[s]=(r/i|0)*i);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(r=1,i=d[0];i>=10;i/=10)r++;r<H&&(t-=H-r)}return u.e=t,u.d=d,u}function An(n){return j(n=new this(n),n.e+1,this.rounding)}function Sn(n){return n=new this(n),n.d?n.d[0]?n.s:0*n.s:n.s||NaN}function Bn(n){return new this(n).sin()}function En(n){return new this(n).sinh()}function On(n){return new this(n).sqrt()}function In(n,e){return new this(n).sub(e)}function Cn(){var n=0,e=arguments,t=new this(e[n]);for(Q=!1;t.s&&++n<e.length;)t=t.plus(e[n]);return Q=!0,j(t,this.precision,this.rounding)}function Tn(n){return new this(n).tan()}function kn(n){return new this(n).tanh()}function xn(n){return j(n=new this(n),n.e+1,1)}E[Symbol.for("nodejs.util.inspect.custom")]=E.toString;E[Symbol.toStringTag]="Decimal";var Se=E.constructor=yr(Pi);Le=new Se(Le);qe=new Se(qe);var L=Se;var Be=(r=>(r[r.Spot=0]="Spot",r[r.Curve=1]="Curve",r[r.BidAsk=2]="BidAsk",r))(Be||{});var Z=require("@cetusprotocol/common-sdk");var ae=1e3,Rn=-443636,Dn=443636,we=1e4,ve=2e3,me=200,_e=443636n,He=1e8,Ei=1e9,Yt=1e4,Fn=604800,Nn=1747627200;var tt=wi(De());var Ln=new tt.default(524288),D=64,Qe=new tt.default(1).shln(D),Mr=new tt.default(2).pow(new tt.default(128)).sub(new tt.default(1)),F=class n{static splitBinLiquidityInfo(e,t,r){let i=n.getPositionCount(t,r);if(i<=1)return[e];let s=[],u=t;for(let d=0;d<i;d++){let f=Math.min(u+1e3-2,r),c=e.bins.filter(p=>p.bin_id>=u&&p.bin_id<=f);s.push({bins:c,amount_a:c.reduce((p,_)=>(0,Z.d)(p).plus(_.amount_a),(0,Z.d)(0)).toFixed(0),amount_b:c.reduce((p,_)=>(0,Z.d)(p).plus(_.amount_b),(0,Z.d)(0)).toFixed(0)}),u=f+1}return s}static processBinsByRate(e,t){let r=[],i=(0,Z.d)(0),s=(0,Z.d)(0),u=!1;return e.forEach(d=>{let{amount_a:f,amount_b:c,liquidity:p="0"}=d,_=(0,Z.d)(t).mul(p).toFixed(0),y=(0,Z.d)(f).mul(t),P=(0,Z.d)(c).mul(t);i=(0,Z.d)(i).plus(y),s=(0,Z.d)(s).plus(P),((0,Z.d)(y).lt(1)&&(0,Z.d)(y).gt(0)||(0,Z.d)(P).lt(1)&&(0,Z.d)(P).gt(0))&&(u=!0),r.push({bin_id:d.bin_id,amount_a:y.toFixed(0),amount_b:P.toFixed(0),price_per_lamport:d.price_per_lamport,liquidity:_})}),{bins:{bins:r,amount_a:i.toFixed(0),amount_b:s.toFixed(0)},has_invalid_amount:u}}static calculateOutByShare(e,t){let{amount_a:r,amount_b:i,liquidity:s="0"}=e;if(s==="0")return{amount_a:"0",amount_b:"0"};if((0,Z.d)(t).gte((0,Z.d)(s)))return{amount_a:r,amount_b:i};let u=(0,Z.d)(t).div(s).mul(r).toFixed(0,L.ROUND_FLOOR),d=(0,Z.d)(t).div(s).mul(i).toFixed(0,L.ROUND_FLOOR);return{amount_a:u,amount_b:d}}static getPositionCount(e,t){let i=(0,Z.d)(t).sub(e).add(1).div(1e3);return Number(i.toFixed(0,L.ROUND_UP))}static getLiquidity(e,t,r){return(0,Z.d)(r).mul(e).add((0,Z.d)(t).mul((0,Z.d)(2).pow(D))).toFixed(0)}static getAmountAFromLiquidity(e,t){return(0,Z.d)(e).div((0,Z.d)(t)).toFixed(0)}static getAmountBFromLiquidity(e){return(0,Z.d)(e).div((0,Z.d)(2).pow(D)).toFixed(0)}static getAmountsFromLiquidity(e,t,r,i){if((0,Z.d)(i).isZero()&&K("LiquiditySupplyIsZero","Liquidity supply is zero"),(0,Z.d)(r).gt((0,Z.d)(i))&&K("InvalidDeltaLiquidity","Invalid delta liquidity"),(0,Z.d)(r).isZero())return["0","0"];let s;(0,Z.d)(e).isZero()?s="0":s=(0,Z.d)(e).mul((0,Z.d)(r)).div((0,Z.d)(i)).toFixed(0,L.ROUND_FLOOR);let u;return(0,Z.d)(t).isZero()?u="0":u=(0,Z.d)(t).mul((0,Z.d)(r)).div((0,Z.d)(i)).toFixed(0,L.ROUND_FLOOR),[s,u]}static getPriceFromBinId(e,t,r,i){let s=n.getPricePerLamportFromBinId(e,t);return n.getPriceFromLamport(r,i,s).toString()}static getPricePerLamportFromBinId(e,t){let r=new L(t).div(new L(1e4));return new L(1).add(new L(r)).pow(new L(e)).toString()}static getBinIdFromPrice(e,t,r,i,s){let u=n.getPricePerLamport(i,s,e);return n.getBinIdFromLamportPrice(u,t,r)}static getBinIdFromLamportPrice(e,t,r){let i=new L(t).div(new L(1e4)),s=new L(e).log().dividedBy(new L(1).add(i).log());return(r?s.floor():s.ceil()).toNumber()}static getPricePerLamport(e,t,r){return new L(r).mul(new L(10**(t-e))).toString()}static getPriceFromLamport(e,t,r){return new L(r).div(new L(10**(t-e))).toString()}static getReversePrice(e){return new L(1).div(e).toString()}static getQPriceFromId(e,t){let r=new tt.default(t).shln(D).div(new tt.default(1e4)),i=Qe.add(r);return n.pow(i,new tt.default(e)).toString()}static getPricePerLamportFromQPrice(e){return Z.MathUtil.fromX64(new tt.default(e)).toString()}static pow(e,t){let r=t.isNeg();if(t.isZero())return Qe;if(t=r?t.abs():t,t.gt(Ln))return new tt.default(0);let i=e,s=Qe;return i.gte(s)&&(i=Mr.div(i),r=!r),t.and(new tt.default(1)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(2)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(4)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(8)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(16)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(32)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(64)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(128)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(256)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(512)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(1024)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(2048)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(4096)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(8192)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(16384)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(32768)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(65536)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(131072)).isZero()||(s=s.mul(i).shrn(D)),i=i.mul(i).shrn(D),t.and(new tt.default(262144)).isZero()||(s=s.mul(i).shrn(D)),s.isZero()?new tt.default(0):(r&&(s=Mr.div(s)),s)}static binScore(e){let t=BigInt(e)+_e;return(t<0n||t>_e*2n)&&K("InvalidBinId",new Error("Invalid bin ID"),{[Z.DETAILS_KEYS.METHOD_NAME]:"binScore",[Z.DETAILS_KEYS.REQUEST_PARAMS]:{binId:e}}),t.toString()}static scoreToBinId(e){let t=BigInt(e)-_e;return(t<-_e||t>_e)&&K("InvalidBinId",new Error("Invalid score"),{[Z.DETAILS_KEYS.METHOD_NAME]:"scoreToBinId",[Z.DETAILS_KEYS.REQUEST_PARAMS]:{score:e}}),Number(t)}static resolveBinPosition(e){let t=BigInt(e),r=t>>4n,i=Number(t&0xfn);return[r.toString(),i]}static findMinMaxBinId(e){let t=1+e/1e4,i=new L("18446744073709551615").log(10).div(new L(t).log(10)).floor(),s=i.neg(),u=i,d=(0,Z.d)(1),f=(0,Z.d)("340282366920938463463374607431768211455");for(;;){let c=(0,Z.d)(n.getQPriceFromId(s.toNumber(),e));if(c.gt(d)&&!c.isZero())break;s=s.add(1)}for(;;){let c=(0,Z.d)(n.getQPriceFromId(u.toNumber(),e));if(c.lt(f)&&!c.isZero())break;u=u.sub(1)}return{minBinId:s.toNumber(),maxBinId:u.toNumber()}}static getBinShift(e,t,r){let i=n.getPricePerLamportFromBinId(e,t),s=(0,Z.d)(i).mul(1+r).toString(),u=n.getBinIdFromLamportPrice(s,t,!0),d=(0,Z.d)(u).sub(e).abs().toFixed(0,L.ROUND_UP);return console.log("getBinShift Options:",{active_id:e,bin_shift:d}),Number(d)}};var Vt=require("@mysten/sui/bcs"),Ar=require("blakejs");function Oi(n){try{let e=n.parsedJson;return{id:e.pool_id,bin_step:Number(e.bin_step),coin_type_a:(0,$.fixCoinType)(e.coin_type_a,!1),coin_type_b:(0,$.fixCoinType)(e.coin_type_b,!1)}}catch(e){return K("ParseError",e,{[$.DETAILS_KEYS.METHOD_NAME]:"parseDlmmBasePool",[$.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function Ee(n){try{let e=(0,$.getObjectFields)(n),t=(0,$.getObjectType)(n),r=(0,$.extractStructTagFromType)(t),i={bin_step:e.bin_manager.fields.bin_step,bin_manager_handle:e.bin_manager.fields.bins.fields.id.id,size:e.bin_manager.fields.bins.fields.size},s={bin_step:e.position_manager.fields.bin_step,position_index:e.position_manager.fields.position_index,position_handle:e.position_manager.fields.positions.fields.id.id,size:e.position_manager.fields.positions.fields.size},u=e.reward_manager.fields,d=u.rewards.map(_=>{let y=_.fields.current_emission_rate,P=$.MathUtil.fromX64(new Pr.default(y)),S=Math.floor(P.toNumber()*60*60*24).toString();return{reward_coin:(0,$.fixCoinType)(_.fields.reward_coin.fields.name,!1),emissions_per_second:P.toString(),emissions_per_day:S,period_emission_rates:{id:_.fields.period_emission_rates.fields.id.id,size:_.fields.period_emission_rates.fields.size}}}),f={is_public:u.is_public,emergency_reward_pause:u.emergency_reward_pause,vault:{id:u.vault.fields.id.id,size:u.vault.fields.size},rewards:d,last_updated_time:u.last_updated_time},c={volatility_accumulator:e.v_parameters.fields.volatility_accumulator,volatility_reference:e.v_parameters.fields.volatility_reference,index_reference:(0,$.asIntN)(BigInt(e.v_parameters.fields.index_reference.fields.bits)),last_update_timestamp:e.v_parameters.fields.last_update_timestamp,bin_step_config:e.v_parameters.fields.bin_step_config.fields},p={id:e.id.id,bin_step:Number(e.bin_step),coin_type_a:(0,$.fixCoinType)(r.type_arguments[0],!1),coin_type_b:(0,$.fixCoinType)(r.type_arguments[1],!1),pool_type:t,index:Number(e.index),bin_manager:i,variable_parameters:c,active_id:(0,$.asIntN)(BigInt(e.active_id.fields.bits)),permissions:e.permissions.fields,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:s};return p.bin_step=p.bin_manager.bin_step,p}catch(e){return console.log("\u{1F680} ~ parseDlmmPool ~ error:",e),K("ParseError",e,{[$.DETAILS_KEYS.METHOD_NAME]:"parseDlmmPool",[$.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function Ke(n){let e=(0,$.getObjectFields)(n),t=(0,$.getObjectType)(n),r=(0,$.extractStructTagFromType)(t);return{id:e.id.id,name:e.name,ref_fee_rate:(0,$.d)(e.ref_fee_rate).div(1e4).toNumber(),start_time:Number(e.start_time),end_time:Number(e.end_time),balances:{id:e.balances.fields.id.id,size:e.balances.fields.size},type:r.full_address}}function $e(n){try{let e=(0,$.getObjectFields)(n);return{uri:e.uri,index:e.index,id:e.id.id,name:e.name,pool_id:e.pool_id,lower_bin_id:(0,$.asIntN)(BigInt(e.lower_bin_id.fields.bits)),upper_bin_id:(0,$.asIntN)(BigInt(e.upper_bin_id.fields.bits)),liquidity_shares:e.liquidity_shares,description:e.description,coin_type_a:(0,$.fixCoinType)(e.coin_type_a,!1),coin_type_b:(0,$.fixCoinType)(e.coin_type_b,!1)}}catch(e){return console.log("\u{1F680} ~ parseDlmmPosition ~ error:",e),K("ParseError",e,{[$.DETAILS_KEYS.METHOD_NAME]:"parseDlmmPosition",[$.DETAILS_KEYS.REQUEST_PARAMS]:n})}}function qn(n,e,t,r){let i=n.map((d,f)=>{let c=t+f,p=F.getPricePerLamportFromBinId(c,e);if(c===r.bin_id){let{amount_a:P,amount_b:S}=F.calculateOutByShare(r,d);return{bin_id:c,amount_a:P,amount_b:S,liquidity:d,price_per_lamport:p}}if(c<r.bin_id){let P=F.getAmountBFromLiquidity(d);return{bin_id:c,amount_a:"0",amount_b:P,liquidity:d,price_per_lamport:p}}let _=F.getQPriceFromId(c,e),y=F.getAmountAFromLiquidity(d,_);return{bin_id:c,amount_a:y,amount_b:"0",liquidity:d,price_per_lamport:p}}),s=i.reduce((d,f)=>d.add(new L(f.amount_a)),new L(0)).toFixed(0),u=i.reduce((d,f)=>d.add(new L(f.amount_b)),new L(0)).toFixed(0);return{bins:i,amount_a:s,amount_b:u}}function Ii(n){try{let e=Vt.bcs.struct("BinAmount",{id:Vt.bcs.struct("I32",{bits:Vt.bcs.u32()}),amount_a:Vt.bcs.u64(),amount_b:Vt.bcs.u64(),price:Vt.bcs.u128(),liquidity_supply:Vt.bcs.u128(),rewards_growth_global:Vt.bcs.vector(Vt.bcs.u128()),fee_a_growth_global:Vt.bcs.u128(),fee_b_growth_global:Vt.bcs.u128()});return Vt.bcs.vector(e).parse(Uint8Array.from(n.results[1].returnValues[0][0])).map(r=>({bin_id:(0,$.asIntN)(BigInt(r.id.bits)),amount_a:r.amount_a,amount_b:r.amount_b,liquidity:r.liquidity_supply,price_per_lamport:F.getPricePerLamportFromQPrice(r.price)}))}catch(e){return console.log("\u{1F680} ~ parseBinInfo ~ error:",e),[]}}function Ci(n){try{return{bin_id:(0,$.asIntN)(BigInt(n.id.fields.bits)),amount_a:n.amount_a,amount_b:n.amount_b,liquidity:n.liquidity_share,price_per_lamport:F.getPricePerLamportFromQPrice(n.price)}}catch(e){return console.log("\u{1F680} ~ parseBinInfo ~ error:",e),K("ParseError",e,{[$.DETAILS_KEYS.METHOD_NAME]:"parseBinInfo",[$.DETAILS_KEYS.REQUEST_PARAMS]:{fields:n}})}}function Ti(n){let e={},t=n.events?.filter(r=>r.type.includes("pool::CollectFeeEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:i}=t[r],s={position_id:i.position,fee_owned_a:i.fee_a,fee_owned_b:i.fee_b};e[i.position]=s}return e}function ki(n){let e={},t=n.events?.filter(r=>r.type.includes("pool::CollectRewardEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:i}=t[r],s=i.position,u=i.reward,d=i.amount,f={coin_type:(0,$.fixCoinType)(u.name,!1),reward_owned:d},c=e[s];c?c.rewards.push(f):c={position_id:s,rewards:[f]},e[s]=c}return e}function xi(n,e){let t=n.events?.filter(r=>r.type.includes("pool::SwapEvent"));for(let r=0;r<t.length;r+=1){let{parsedJson:i}=t[r],{partner:s,pool:u,amount_in:d,amount_out:f,fee:c,ref_fee:p,bin_swaps:_,from:y,target:P}=i,S=_.map(k=>({bin_id:k.bin_id.bits,in_amount:k.amount_in,out_amount:k.amount_out,fee:k.fee,var_fee_rate:k.var_fee_rate}));return{pool_id:u,a2b:e,in_amount:d,out_amount:f,ref_fee_amount:p,fee_amount:c,bin_swaps:S,partner:s,from_coin_type:(0,$.fixCoinType)(y.name,!1),to_coin_type:(0,$.fixCoinType)(P.name,!1)}}}function jn(n){switch(n){case 0:return 0;case 1:return 1;case 2:return 2}}var Sr=["RemoveLiquidityEvent","SwapEvent","AddLiquidityEvent","ClosePositionEvent"];function Ri(n,e,t,r){let i=[],{timestampMs:s,events:u}=n;return u?.forEach((d,f)=>{let{name:c,address:p}=(0,$.extractStructTagFromType)(d.type);if(Sr.includes(c)&&p===t&&r===d.parsedJson.pool){let _={tx:d.id.txDigest,sender:d.sender,type:d.type,block_time:s||"0",index:`${e}_${f}`,parsed_json:d.parsedJson};i.push(_)}}),i}function Wn(n,e,t){let r=[],i=0,s=new Date(n*1e3).getTime(),u=new Date().getTime();for(;;){let d=s+i*t;if(d>=u&&r.push(d),i>=e)break;i+=1}return r}function Zn(n,e,t,r){let i=[];for(let s=e;s<=t;s+=r){let u=n.findLast(d=>(0,$.d)(s).gte(d.time));u?i.push({time:s.toString(),emissions_per_second:u.emissions_per_second,emissions_per_day:u.emissions_per_day,visualized_time:new Date(s*1e3).toLocaleString()}):i.push({emissions_per_day:"0",time:s.toString(),emissions_per_second:"0",visualized_time:new Date(s*1e3).toLocaleString()})}return i}function Un(n){if(n.length===0)return;let e=new Date().getTime()/1e3,t=n.findLast(r=>(0,$.d)(e).gte(r.time));return t||n[n.length-1]}function re(n,e){let t=n.mul(e);if(t.gt(0)&&t.lt(1))throw new pe(`Multiplication ${t} is less than 1`,"AmountTooSmall");return t.floor()}function Me(n){if(n.gt(0)&&n.lt(1))throw new pe(`Multiplication ${n.toString()} is less than 1`,"AmountTooSmall");return n.floor()}function Di(n){switch(n){case 0:return"spot";case 1:return"curve";case 2:return"bid_ask"}}function Fi(n,e,t,r){let i=Buffer.from(n,"utf8"),s=Buffer.from(e,"utf8"),u=s.length,d=0;for(;d<u;){let y=s[d];i=Buffer.concat([i,Buffer.from([y])]),d++}let f=Vt.bcs.u16().serialize(t).toBytes(),c=Vt.bcs.u16().serialize(r).toBytes(),p=Buffer.concat([i,f,c]),_=(0,Ar.blake2b)(p,void 0,32);return`0x${Buffer.from(_).toString("hex")}`}var O=require("@cetusprotocol/common-sdk");var te=class n{static toWeight(e){console.log("\u{1F680} ~ WeightUtils ~ toWeight ~ options:",e);let{strategy_type:t,active_id:r,bin_step:i,lower_bin_id:s,upper_bin_id:u,total_amount_a:d,total_amount_b:f,active_bin_of_pool:c}=e,p=r<s||r>u,_=F.getQPriceFromId(r,i),y=(0,O.d)(0),P=(0,O.d)(0),S=(0,O.d)(200);if(t===2?S=(0,O.d)(200):t===1?S=(0,O.d)(2e3):t===0&&(S=(0,O.d)(1)),!p&&c){let it=this.calculateActiveWeights(c.amount_a,c.amount_b,_,S);y=it.active_weight_a,P=it.active_weight_b}r===s&&(0,O.d)(f).isZero()&&(y=(0,O.d)(S).mul((0,O.d)(2).pow(D*2)).div((0,O.d)(_)).floor(),P=(0,O.d)(0)),r===u&&(0,O.d)(d).isZero()&&(P=(0,O.d)(S).mul((0,O.d)(2).pow(D)).floor(),y=(0,O.d)(0)),r>s&&r<u&&((0,O.d)(d).isZero()&&(P=(0,O.d)(S).mul((0,O.d)(2).pow(D)).floor(),y=(0,O.d)(0)),(0,O.d)(f).isZero()&&(y=(0,O.d)(S).mul((0,O.d)(2).pow(D*2)).div((0,O.d)(_)).floor(),P=(0,O.d)(0)));let B=p?(0,O.d)(0):y,k=p?(0,O.d)(0):P,x=(0,O.d)(2e3).sub((0,O.d)(200)).floor(),R=r>u?u:r,C=r<s?s:r,T=r>s?x.div((0,O.d)(R-s)).floor():(0,O.d)(0),z=u>r?x.div((0,O.d)(u-C)).floor():(0,O.d)(0),U=s,Y=[],et=[];for(;U<=u;){let it=(0,O.d)(0);if(U<r){let ut=R-U;t===0?it=(0,O.d)(1):t===2?it=(0,O.d)(S).add(T.mul(ut)).floor():t===1&&(it=(0,O.d)(S).sub(T.mul(ut)).floor())}else if(U>r){let ut=U-C;t===0?it=(0,O.d)(1):t===2?it=(0,O.d)(S).add(z.mul(ut)).floor():t===1&&(it=(0,O.d)(S).sub(z.mul(ut)).floor())}else it=S;if(Y.push(it),U<r)k=k.add(it.mul((0,O.d)(2).pow(D))).floor(),et.push((0,O.d)(0));else if(U>r){let ut=it.mul((0,O.d)(2).pow(D*2)).div(F.getQPriceFromId(U,i)).floor();et.push(ut),B=B.add(ut).floor()}else et.push((0,O.d)(0));U+=1}return{...e,total_weight_a:B,total_weight_b:k,active_weight_a:y,active_weight_b:P,weights:Y,weight_per_prices:et}}static toWeightSpotBalanced(e,t){let r=[];for(let i=e;i<=t;i++)r.push({bin_id:i,weight:1});return r}static toWeightDescendingOrder(e,t){let r=[];for(let i=e;i<=t;i++)r.push({bin_id:i,weight:t-i+1});return r}static toWeightAscendingOrder(e,t){let r=[];for(let i=e;i<=t;i++)r.push({bin_id:i,weight:i-e+1});return r}static toWeightCurve(e,t,r){if(r<e)return n.toWeightDescendingOrder(e,t);if(r>t)return n.toWeightAscendingOrder(e,t);let i=2e3,u=i-200,d=r>e?Math.floor(u/(r-e)):0,f=t>r?Math.floor(u/(t-r)):0,c=[];for(let p=e;p<=t;p++)p<r?c.push({bin_id:p,weight:i-(r-p)*d}):p>r?c.push({bin_id:p,weight:i-(p-r)*f}):c.push({bin_id:p,weight:i});return c}static toWeightBidAsk(e,t,r){if(r>t)return n.toWeightDescendingOrder(e,t);if(r<e)return n.toWeightAscendingOrder(e,t);let i=2e3,s=200,u=i-s,d=r>e?Math.floor(u/(r-e)):0,f=t>r?Math.floor(u/(t-r)):0,c=[];for(let p=e;p<=t;p++)p<r?c.push({bin_id:p,weight:s+(r-p)*d}):p>r?c.push({bin_id:p,weight:s+(p-r)*f}):c.push({bin_id:p,weight:s});return c}static toAmountBidSide(e,t,r,i,s=!1){let u=i.filter(f=>f.bin_id<=e).reduce(function(f,c){return s?c.bin_id>e?f:f.add(c.weight):c.bin_id>=e?f:f.add(c.weight)},(0,O.d)(0));if(u.cmp((0,O.d)(0))!=1)throw Error("Invalid parameters");return{bins:i.map(f=>{let c=F.getPricePerLamportFromBinId(f.bin_id,r);if(!(f.bin_id<=e)||f.bin_id>=e&&!s)return{bin_id:f.bin_id,amount_a:"0",amount_b:"0",price_per_lamport:c,liquidity:"0"};{let _=(0,O.d)(f.weight).div(u),y=re((0,O.d)(t),_).toString(),P="0",S=F.getQPriceFromId(f.bin_id,r),B=F.getLiquidity(P,y,S);return{bin_id:f.bin_id,amount_b:y,amount_a:P,price_per_lamport:c,liquidity:B}}}),amount_a:"0",amount_b:t}}static toAmountAskSide(e,t,r,i,s=!1){let u=i.filter(f=>f.bin_id>=e).reduce(function(f,c){if(c.bin_id<=e&&!s)return f;{let p=F.getPricePerLamportFromBinId(c.bin_id,t),_=new L(c.weight).div(p);return f.add(_)}},new L(0));if(u.cmp(new L(0))!=1)throw Error("Invalid parameters");return{bins:i.map(f=>{let c=F.getPricePerLamportFromBinId(f.bin_id,t);if(!(f.bin_id>=e)||f.bin_id<=e&&!s)return{bin_id:f.bin_id,amount_a:"0",amount_b:"0",price_per_lamport:c,liquidity:"0"};{let y=new L(f.weight).div(c).div(u),P=re((0,O.d)(r),y).toString(),S="0",B=F.getQPriceFromId(f.bin_id,t),k=F.getLiquidity(P,S,B);return{bin_id:f.bin_id,amount_a:P,amount_b:S,price_per_lamport:c,liquidity:k}}}),amount_a:r,amount_b:"0"}}static toAmountBothSide(e,t,r,i,s,u,d){let f=!(0,O.d)(r).isZero()&&(0,O.d)(i).isZero(),c=(0,O.d)(r).isZero()&&!(0,O.d)(i).isZero();if(e>d[d.length-1].bin_id)return n.toAmountBidSide(e,i,t,d);if(c&&e!==d[d.length-1].bin_id)return n.toAmountBidSide(e,i,t,d,!0);if(e<d[0].bin_id)return n.toAmountAskSide(e,t,r,d);if(f&&e!==d[0].bin_id)return n.toAmountAskSide(e,t,r,d,!0);let p=d.filter(_=>_.bin_id===e);if(p.length===1){let{totalWeightA:_,totalWeightB:y,activeWeightA:P,activeWeightB:S}=n.calculateTotalWeights(t,d,e,p[0],s,u,f?"a":c?"b":void 0),B=new L(r.toString()).div(_),k=new L(i.toString()).div(y),x=d.map(T=>{let z=F.getPricePerLamportFromBinId(T.bin_id,t);if(T.bin_id<e||T.bin_id===e&&c){let at=re(k,new L(T.weight)),v="0",o=F.getQPriceFromId(T.bin_id,t),l=F.getLiquidity(v,at.toString(),o);return{bin_id:T.bin_id,amount_a:"0",amount_b:at.toString(),price_per_lamport:z,liquidity:l}}if(T.bin_id>e||T.bin_id===e&&f){let at=new L(T.weight).div(z),v=re(B,new L(at)),o="0",l=F.getQPriceFromId(T.bin_id,t),h=F.getLiquidity(v.toString(),o,l);return{bin_id:T.bin_id,amount_a:v.toString(),amount_b:"0",price_per_lamport:z,liquidity:h}}let U=re(B,P),Y=re(k,S),et=U.toString(),it=Y.toString(),ut=F.getQPriceFromId(T.bin_id,t),X=F.getLiquidity(et,it,ut);return{bin_id:T.bin_id,amount_a:et,amount_b:it,price_per_lamport:z,liquidity:X}}),R=x.reduce((T,z)=>(0,O.d)(T).add((0,O.d)(z.amount_a)),(0,O.d)(0)).toString(),C=x.reduce((T,z)=>(0,O.d)(T).add((0,O.d)(z.amount_b)),(0,O.d)(0)).toString();return{bins:x,amount_a:R,amount_b:C}}else{let{totalWeightA:_,totalWeightB:y}=n.calculateTotalWeights(t,d,e),P=new L(r.toString()).div(_),S=new L(i.toString()).div(y),B=d.map(R=>{let C=F.getPricePerLamportFromBinId(R.bin_id,t);if(R.bin_id<e){let T=re(S,new L(R.weight));return{bin_id:R.bin_id,amount_a:"0",amount_b:T.toString(),price_per_lamport:C}}else{let T=new L(R.weight).div(C),z=re(P,T);return{bin_id:R.bin_id,amount_a:z.toString(),amount_b:"0",price_per_lamport:C}}}),k=B.reduce((R,C)=>(0,O.d)(R).add((0,O.d)(C.amount_a)),(0,O.d)(0)).toString(),x=B.reduce((R,C)=>(0,O.d)(R).add((0,O.d)(C.amount_b)),(0,O.d)(0)).toString();return{bins:B,amount_a:k,amount_b:x}}}static autoFillCoinByWeight(e,t,r,i,s,u,d){if(e>d[d.length-1].bin_id)return n.toAmountBidSide(e,r,t,d);if(e<d[0].bin_id)return n.toAmountAskSide(e,t,r,d);let f=d.filter(P=>P.bin_id===e),{totalWeightA:c,totalWeightB:p}=n.calculateTotalWeights(t,d,e,f.length===1?f[0]:void 0,f.length===1?s:void 0,f.length===1?u:void 0),_=(0,O.d)(0);i?_=c.isZero()?new L(0):new L(r).div(c):_=p.isZero()?new L(0):new L(r).div(p);let y=re(_,i?p:c).toString();return n.toAmountBothSide(e,t,i?r:y,i?y:r,s,u,d)}static calculateActiveWeights(e,t,r,i){let s=(0,O.d)(r),u=(0,O.d)(e),d=(0,O.d)(t),f=(0,O.d)(0),c=(0,O.d)(0);if(u.isZero()&&d.isZero())f=(0,O.d)(i).mul((0,O.d)(2).pow(D*2)).div(s.mul(2)).floor(),c=(0,O.d)(i).mul((0,O.d)(2).pow(D)).div(2).floor();else{if(u.isZero())f=(0,O.d)(0);else{let p=d.mul((0,O.d)(2).pow(D)).div(u);f=(0,O.d)(i).mul((0,O.d)(2).pow(D*2)).div(s.add(p)).floor()}if(d.isZero())c=(0,O.d)(0);else{let p=(0,O.d)(2).pow(D).add(s.mul(u).div(d)).floor();c=(0,O.d)(i).mul((0,O.d)(2).pow(D*2)).div(p).floor()}}return{active_weight_a:f,active_weight_b:c}}static calculateTotalWeights(e,t,r,i,s,u,d){let f=(0,O.d)(F.getPricePerLamportFromBinId(r,e)),c=(0,O.d)(0),p=(0,O.d)(0);if(s&&u&&i&&!d)if((0,O.d)(s).isZero()&&(0,O.d)(u).isZero())c=new L(i.weight).div(f.mul(new L(2))),p=new L(i.weight).div(new L(2));else{let P=new L(s.toString()),S=new L(u.toString());(0,O.d)(s).isZero()||(c=new L(i.weight).div(f.add(S.div(P)))),(0,O.d)(u).isZero()||(p=new L(i.weight).div(new L(1).add(f.mul(P).div(S))))}let _=c,y=p;return t.forEach(P=>{if((P.bin_id<r||d==="b")&&(y=y.add(new L(P.weight))),P.bin_id>r||d==="a"){let S=F.getPricePerLamportFromBinId(P.bin_id,e),B=new L(P.weight).div(S);_=_.add(B)}}),{totalWeightA:_,totalWeightB:y,activeWeightA:c,activeWeightB:p}}};var Jt=require("@cetusprotocol/common-sdk");var Pe=class{static toAmountsByWeights(e){let{total_weight_a:t,total_weight_b:r,weights:i,weight_per_prices:s,total_amount_a:u,total_amount_b:d,active_weight_a:f,active_weight_b:c,lower_bin_id:p,upper_bin_id:_,active_id:y,bin_step:P}=e,S=r.isZero()?(0,Jt.d)(0):(0,Jt.d)(d).mul((0,Jt.d)(2).pow(D*2)).div(r).floor(),B=t.isZero()?(0,Jt.d)(0):(0,Jt.d)(u).mul((0,Jt.d)(2).pow(D*2)).div(t).floor(),k=Me(B.mul(f).div((0,Jt.d)(2).pow(D*2))),x=Me(S.mul(c).div((0,Jt.d)(2).pow(D*2))),R=_-p+1,C=p,T=0,z=[];for(;T<R;){let Y=(0,Jt.d)(0),et=(0,Jt.d)(0),it=F.getQPriceFromId(C,P);C<y?et=Me(S.mul(i[T]).div((0,Jt.d)(2).pow(D))):C>y?Y=Me(B.mul(s[T]).div((0,Jt.d)(2).pow(D*2))):(Y=k,et=x);let ut=F.getLiquidity(Y.toString(),et.toString(),it);z.push({bin_id:C,amount_a:Y.toString(),amount_b:et.toString(),price_per_lamport:F.getPricePerLamportFromBinId(C,P),liquidity:ut}),C+=1,T+=1}return{bins:z,amount_a:u.toString(),amount_b:d.toString()}}static toAmountsBothSideByStrategy(e,t,r,i,s,u,d,f){let c=te.toWeight({strategy_type:d,active_id:e,bin_step:t,lower_bin_id:r,upper_bin_id:i,total_amount_a:s,total_amount_b:u,active_bin_of_pool:f});return this.toAmountsByWeights(c)}static autoFillCoinByStrategy(e,t,r,i,s,u,d,f){switch(d){case 0:{let c=te.toWeightSpotBalanced(s,u);return te.autoFillCoinByWeight(e,t,r,i,f?.amount_a||"0",f?.amount_b||"0",c)}case 1:{let c=te.toWeightCurve(s,u,e);return te.autoFillCoinByWeight(e,t,r,i,f?.amount_a||"0",f?.amount_b||"0",c)}case 2:{let c=te.toWeightBidAsk(s,u,e);return te.autoFillCoinByWeight(e,t,r,i,f?.amount_a||"0",f?.amount_b||"0",c)}}}static autoFillCoinByStrategyV2(e,t,r,i,s,u,d,f){let c=this.autoFillCoinByStrategy(e,t,r,i,s,u,d,f),p=te.toWeight({strategy_type:d,active_id:e,bin_step:t,lower_bin_id:s,upper_bin_id:u,total_amount_a:i?r:c.amount_a,total_amount_b:i?c.amount_b:r,active_bin_of_pool:f});return this.toAmountsByWeights(p)}};var dt=require("@cetusprotocol/common-sdk");var Oe=class n{static getVariableFee(e){let{volatility_accumulator:t,bin_step_config:r}=e,{variable_fee_control:i,bin_step:s}=r;return(0,dt.d)(i).gt(0)?(0,dt.d)(t).mul(s).pow(2).mul(i).add(99999999999).div(1e11).toFixed(0):"0"}static calculateCompositionFee(e,t){let r=(0,dt.d)(e).mul(t);return(0,dt.d)(r).mul((0,dt.d)(1e9).add(t)).div(1e18).toFixed(0)}static calculateProtocolFee(e,t){return(0,dt.d)(e).mul(t).div(1e4).ceil().toFixed(0)}static getProtocolFees(e,t,r){let i=n.calculateProtocolFee(e,r),s=n.calculateProtocolFee(t,r);return{protocol_fee_a:i,protocol_fee_b:s}}static getCompositionFees(e,t,r){let{bin_step_config:i}=r;if((0,dt.d)(e.liquidity||"0").eq((0,dt.d)(0)))return{fees_a:"0",fees_b:"0"};let{bin_step:s,base_factor:u}=i,d=F.getQPriceFromId(e.bin_id,s),f=F.getLiquidity(e.amount_a,e.amount_b,d),c=F.getLiquidity(t.amount_a,t.amount_b,d),p=(0,dt.d)(e.liquidity).mul(c).div(f).toFixed(0),{amount_a:_,amount_b:y}=F.calculateOutByShare({bin_id:e.bin_id,liquidity:(0,dt.d)(e.liquidity).add(p).toFixed(0),amount_a:(0,dt.d)(e.amount_a).add(t.amount_a).toFixed(0),amount_b:(0,dt.d)(e.amount_b).add(t.amount_b).toFixed(0),price_per_lamport:e.price_per_lamport},p),P=(0,dt.d)(s).mul(u).mul(10),S=n.getVariableFee(r),B=(0,dt.d)(P).add(S).toFixed(0);(0,dt.d)(B).gt(1e8)&&(B=1e8.toString());let k="0",x="0";return(0,dt.d)(_).gt(t.amount_a)&&(0,dt.d)(t.amount_b).gt(y)?x=n.calculateCompositionFee((0,dt.d)(t.amount_b).sub(y).toFixed(0),B):(0,dt.d)(y).gt(t.amount_b)&&(0,dt.d)(t.amount_a).gt(_)&&(k=n.calculateCompositionFee((0,dt.d)(t.amount_a).sub(_).toFixed(0),B)),{fees_a:k,fees_b:x}}};var Br=require("@mysten/sui/client"),Ni=require("@mysten/sui/utils"),Ce=require("@mysten/sui/bcs"),Te=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getPoolAddress(e,t,r,i){try{let s=Fi(e,t,r,i),{dlmm_pool:u}=this._sdk.sdkOptions,{pools_id:d}=(0,G.getPackagerConfigs)(u),f=await this._sdk.FullClient.getDynamicFieldObject({parentId:d,name:{type:"0x2::object::ID",value:s}});return(0,G.getObjectFields)(f).value.fields.value.fields.pool_id}catch(s){return K("FetchError",s,{[G.DETAILS_KEYS.METHOD_NAME]:"getPoolAddress",[G.DETAILS_KEYS.REQUEST_PARAMS]:{coin_type_a:e,coin_type_b:t,bin_step:r,base_factor:i}})}}async getBasePoolList(e="all",t=!1){let{dlmm_pool:r}=this._sdk.sdkOptions,i={data:[],has_next_page:!1},s=e==="all",u=`${r.package_id}_getBasePoolList`;if(s){let d=this._sdk.getCache(u,t);if(d&&d.length>0)return i.data.push(...d),i}try{let d=`${r.package_id}::registry::CreatePoolEvent`,f=await this._sdk.FullClient.queryEventsByPage({MoveEventType:d},e);i.has_next_page=f.has_next_page,i.next_cursor=f.next_cursor,f.data.forEach(c=>{let p=Oi(c);i.data.push(p)}),s&&this._sdk.updateCache(`${r.package_id}_getPoolImmutables`,i.data)}catch(d){return K("FetchError",d,{[G.DETAILS_KEYS.METHOD_NAME]:"getBasePoolList"})}return i}async getPools(e="all",t=!1){let r={data:[],has_next_page:!1},i=await this.getBasePoolList(e,t);if(i.data.length===0)return r;try{let s=await this._sdk.FullClient.batchGetObjects(i.data.map(u=>u.id),{showContent:!0,showType:!0});r.has_next_page=i.has_next_page,r.next_cursor=i.next_cursor;for(let u of s){let d=Ee(u),f=`${d.id}_getDlmmPool`;this._sdk.updateCache(f,d),r.data.push(d)}}catch(s){return K("FetchError",s,{[G.DETAILS_KEYS.METHOD_NAME]:"getPools"})}return r}async getBinInfo(e,t,r,i=!0){try{let s=`${e}_getBinInfo_${t}`,u=this._sdk.getCache(s,i);if(u!==void 0)return u;let d=F.binScore(t),[f,c]=F.resolveBinPosition(d),_=(await this._sdk.FullClient.getDynamicFieldObject({parentId:e,name:{type:"u64",value:f}})).data.content.fields.value.fields.value.fields.group.fields.bins[c].fields,y=Ci(_);return this._sdk.updateCache(s,y),y}catch{return{bin_id:t,amount_a:"0",amount_b:"0",liquidity:"0",price_per_lamport:F.getPricePerLamportFromBinId(t,r)}}}async getTotalFeeRate(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:i,coin_type_b:s}=e,u=new Ie.Transaction;u.moveCall({target:`${t.published_at}::pool::get_total_fee_rate`,arguments:[u.object(r)],typeArguments:[i,s]});let d=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:u,sender:(0,Ni.normalizeSuiAddress)("0x0")});return Ce.bcs.struct("FeeRate",{base_fee_rate:Ce.bcs.u64(),var_fee_rate:Ce.bcs.u64(),total_fee_rate:Ce.bcs.u64()}).parse(Uint8Array.from(d.results[0].returnValues[0][0]))}async getPoolBinInfo(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:i,coin_type_b:s}=e,u=1e3,d=[],f,c=!0;for(;c;){let p=new Ie.Transaction,_;f!==void 0?_=p.moveCall({target:"0x1::option::some",arguments:[p.pure.u32(Number((0,G.asUintN)(BigInt(f))))],typeArguments:["u32"]}):_=p.moveCall({target:"0x1::option::none",typeArguments:["u32"]}),p.moveCall({target:`${t.published_at}::pool::fetch_bins`,arguments:[p.object(r),_,p.pure.u64(u)],typeArguments:[i,s]});let y=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:p,sender:(0,Ni.normalizeSuiAddress)("0x0")}),P=Ii(y);d.push(...P),f=P.length>0?P[P.length-1].bin_id+1:void 0,c=P.length===u}return d.sort((p,_)=>p.bin_id-_.bin_id)}async getPoolTransactionList({pool_id:e,pagination_args:t,order:r="descending",full_rpc_url:i}){let{FullClient:s,sdkOptions:u}=this._sdk,d;i?d=(0,G.createFullClient)(new Br.SuiClient({url:i})):d=s;let f={data:[],has_next_page:!1},c=50,p=t,_=t.limit||10;do{let y=await d.queryTransactionBlocksByPage({ChangedObject:e},{...p,limit:50},r);y.data.forEach((P,S)=>{let B=Ri(P,S,u.dlmm_pool.package_id,e);f.data=[...f.data,...B]}),f.has_next_page=y.has_next_page,f.next_cursor=y.next_cursor,p.cursor=y.next_cursor}while(f.data.length<_&&f.has_next_page);return f.data.length>_&&(f.data=f.data.slice(0,_),f.has_next_page=!0),f.data.length>0&&(f.next_cursor=f.data[f.data.length-1].tx),f}async getRangeBinInfo(e,t,r,i){let s=[];for(let u=t;u<=r;u++){let d=await Promise.all([this.getBinInfo(e,u,i)]);s.push(...d)}return s}async getAssignPoolList(e){if(e.length===0)return[];let t=[];try{let r=await this._sdk.FullClient.batchGetObjects(e,{showContent:!0,showType:!0});for(let i of r){let s=Ee(i),u=`${s.id}_getDlmmPool`;this._sdk.updateCache(u,s),t.push(s)}}catch(r){return K("FetchError",r,{[G.DETAILS_KEYS.METHOD_NAME]:"getAssignPoolList"})}return t}async getPool(e,t=!0){try{let r=`${e}_getDlmmPool`,i=this._sdk.getCache(r,t);if(i!==void 0)return i;let s=await this._sdk.FullClient.getObject({id:e,options:{showType:!0,showContent:!0}}),u=Ee(s);return this._sdk.updateCache(r,u),u}catch(r){return K("FetchError",r,{[G.DETAILS_KEYS.METHOD_NAME]:"getPool",[G.DETAILS_KEYS.REQUEST_PARAMS]:e})}}async createPoolAndAddWithPricePayload(e){let{bin_step:t,url:r,coin_type_a:i,coin_type_b:s,bin_infos:u,price_base_coin:d,price:f,lower_price:c,upper_price:p,decimals_a:_,decimals_b:y,strategy_type:P,use_bin_infos:S,base_factor:B,pool_id:k}=e,x,R,C,T=u;if(d==="coin_a")x=F.getBinIdFromPrice(c,t,!1,_,y),R=F.getBinIdFromPrice(p,t,!0,_,y),C=F.getBinIdFromPrice(f,t,!0,_,y);else{x=F.getBinIdFromPrice((0,G.d)(1).div(p).toString(),t,!1,_,y),R=F.getBinIdFromPrice((0,G.d)(1).div(c).toString(),t,!0,_,y),C=F.getBinIdFromPrice((0,G.d)(1).div(f).toString(),t,!1,_,y);let Y={pool_id:k,amount_a:u.amount_b,amount_b:u.amount_a,active_id:C,bin_step:t,lower_bin_id:x,upper_bin_id:R,active_bin_of_pool:void 0,strategy_type:e.strategy_type};T=await this.sdk.Position.calculateAddLiquidityInfo(Y)}let U={bin_step:t,url:r,coin_type_a:i,coin_type_b:s,bin_infos:T,lower_bin_id:x,upper_bin_id:R,active_id:C,strategy_type:P,use_bin_infos:S,base_factor:B};return this.createPoolAndAddLiquidityPayload(U)}async createPoolPayload(e,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{bin_step:i,base_factor:s,url:u,coin_type_a:d,coin_type_b:f,active_id:c}=e;t=t||new Ie.Transaction;let{registry_id:p,global_config_id:_,versioned_id:y}=(0,G.getPackagerConfigs)(r),P=await this._sdk.FullClient.fetchCoinMetadata(d);P?.id||K("FetchError",new Error(`coin_a_metadata not found: ${d}`),{[G.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[G.DETAILS_KEYS.REQUEST_PARAMS]:e});let S=await this._sdk.FullClient.fetchCoinMetadata(f);S?.id||K("FetchError",new Error(`coin_b_metadata not found: ${f}`),{[G.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[G.DETAILS_KEYS.REQUEST_PARAMS]:e});let[B,k]=t.moveCall({target:`${r.published_at}::registry::create_pool`,arguments:[t.object(p),t.object(P.id),t.object(S.id),t.pure.u16(i),t.pure.u16(s),t.pure.u32(Number((0,G.asUintN)(BigInt(c)))),t.pure.string(u||""),t.object(_),t.object(y),t.object(G.CLOCK_ADDRESS)],typeArguments:[d,f]});return t.moveCall({target:`${r.published_at}::registry::destroy_receipt`,arguments:[t.object(B),k,t.object(y)],typeArguments:[d,f]}),k}async createPoolAndAddLiquidityPayload(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{bin_step:r,base_factor:i,url:s,active_id:u,coin_type_a:d,coin_type_b:f,bin_infos:c,lower_bin_id:p,upper_bin_id:_,strategy_type:y,use_bin_infos:P}=e,{registry_id:S,global_config_id:B,versioned_id:k}=(0,G.getPackagerConfigs)(t),x=new Ie.Transaction;(0,G.isSortedSymbols)((0,G.fixCoinType)(d,!1),(0,G.fixCoinType)(f,!1))&&K("InvalidCoinTypeSequence",new Error("invalid coin type sequence"),{[G.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[G.DETAILS_KEYS.REQUEST_PARAMS]:e});let R=_-p+1;R>1e3&&K("InvalidBinWidth",new Error("Width is too large"),{[G.DETAILS_KEYS.METHOD_NAME]:"openPosition",[G.DETAILS_KEYS.REQUEST_PARAMS]:e}),(await this._sdk.FullClient.fetchCoinMetadata(d))?.id||K("FetchError",new Error(`coin_a_metadata not found: ${d}`),{[G.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[G.DETAILS_KEYS.REQUEST_PARAMS]:e}),(await this._sdk.FullClient.fetchCoinMetadata(f))?.id||K("FetchError",new Error(`coin_b_metadata not found: ${f}`),{[G.DETAILS_KEYS.METHOD_NAME]:"createPoolAndAddLiquidityPayload",[G.DETAILS_KEYS.REQUEST_PARAMS]:e}),console.log("\u{1F680} ~ createPoolAndAddLiquidityPayload ~ option:",{...e,width:R});let U={pool_id:await this.createPoolPayload({active_id:u,bin_step:r,base_factor:i,coin_type_a:d,coin_type_b:f},x),bin_infos:c,coin_type_a:d,coin_type_b:f,lower_bin_id:p,upper_bin_id:_,active_id:u,strategy_type:y,use_bin_infos:P,max_price_slippage:0,bin_step:r};return this.sdk.Position.addLiquidityPayload(U,x),x}};var Er=require("@cetusprotocol/common-sdk"),Li={env:"testnet",full_rpc_url:Er.FullRpcUrlTestnet,dlmm_pool:{package_id:"0x17a1f5a8779461ff44e942adf33325cce112c693d6a177ed77f035ca86d1fdb6",published_at:"0x6d32c1be32eefcea933c03dd5cb7c783d1d83f6b30c4d1131d955933747b1701",version:1,config:{registry_id:"0x319070e26a6809f439d3c4a45e63bf74939c5fe3165de7b65968ee8547f71bd0",pools_id:"0x505fcde74ab557d553832a87f169a0408ad3507ca4e84b25f7d32c2c1535765c",global_config_id:"0x88bb33e9eff2fccab980a0e4b43fc4572abd08f08304d47a20d3e4e99d94d159",versioned_id:"0xa710caae87b2129acc97fbb98ea7011e3137c3291b02c0fcce866d67d5d9e8d0",admin_cap_id:"0x6fc908894ad7c2ff16cca07a05af6760831a8b5e5dc34e40470dce6ee1760155",partners_id:"0xc5c31fe1550e39c9890e0fe3d2608dd9b408a10d74020e5ff72ccfffe4c9c879"}},dlmm_router:{package_id:"0xba3059875c8980ac171fc2bac81b9df172fb77fa0cb5a267636df701225b93ef",published_at:"0x59b7a2da6db8f9245a1db6169018af7124c0714fa77a84224967ead6be125127",version:1},faucet:{package_id:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",published_at:"0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48",version:1}};var I=require("@cetusprotocol/common-sdk"),he=require("@mysten/sui/transactions"),Or=require("@mysten/sui/utils");var ze=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){let t=[];try{(await this._sdk.FullClient.getOwnedObjectsByPage(e,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{StructType:this.buildPositionType()}})).data.forEach(i=>{t.push($e(i))})}catch(r){console.log("\u{1F680} ~ PositionModule ~ getOwnerPositionList ~ error:",r),K("GetObjectError",r,{[I.DETAILS_KEYS.METHOD_NAME]:"getOwnerPositionList",[I.DETAILS_KEYS.REQUEST_PARAMS]:e})}return t}async getPosition(e){try{let t=await this._sdk.FullClient.getObject({id:e,options:{showType:!0,showContent:!0,showOwner:!0}});return $e(t)}catch(t){return console.log("\u{1F680} ~ PositionModule ~ getPosition ~ error:",t),K("GetObjectError",t,{[I.DETAILS_KEYS.METHOD_NAME]:"getPosition",[I.DETAILS_KEYS.REQUEST_PARAMS]:e})}}collectFeePayload(e,t){let{pool_id:r,position_id:i,coin_type_a:s,coin_type_b:u}=e,{dlmm_pool:d}=this.sdk.sdkOptions,{versioned_id:f,global_config_id:c}=(0,I.getPackagerConfigs)(d);t=t||new he.Transaction;let[p,_]=t.moveCall({target:`${d.published_at}::pool::collect_position_fee`,arguments:[t.object(r),t.object(i),t.object(c),t.object(f)],typeArguments:[s,u]}),y=I.CoinAssist.fromBalance(p,s,t),P=I.CoinAssist.fromBalance(_,u,t);return t.transferObjects([y,P],this.sdk.getSenderAddress()),t}updatePositionFeeAndRewards(e,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:i}=(0,I.getPackagerConfigs)(r),{pool_id:s,position_id:u,coin_type_a:d,coin_type_b:f}=e;return t.moveCall({target:`${r.published_at}::pool::update_position_fee_and_rewards`,arguments:[t.object(s),t.pure.id(u),t.object(i),t.object(I.CLOCK_ADDRESS)],typeArguments:[d,f]}),t}collectRewardPayload(e,t){let{dlmm_pool:r}=this.sdk.sdkOptions,{versioned_id:i,global_config_id:s}=(0,I.getPackagerConfigs)(r);return t=t||new he.Transaction,e.forEach(u=>{let{pool_id:d,position_id:f,reward_coins:c,coin_type_a:p,coin_type_b:_}=u;c.forEach(y=>{let P=t.moveCall({target:`${r.published_at}::pool::collect_position_reward`,arguments:[t.object(d),t.object(f),t.object(s),t.object(i)],typeArguments:[p,_,y]}),S=I.CoinAssist.fromBalance(P,y,t);t.transferObjects([S],this.sdk.getSenderAddress())})}),t}collectRewardAndFeePayload(e,t){return t=t||new he.Transaction,e.forEach(r=>{let{pool_id:i,position_id:s,reward_coins:u,coin_type_a:d,coin_type_b:f}=r;this.updatePositionFeeAndRewards({pool_id:i,position_id:s,coin_type_a:d,coin_type_b:f},t),this.collectFeePayload({pool_id:i,position_id:s,coin_type_a:d,coin_type_b:f},t),this.collectRewardPayload([{pool_id:i,position_id:s,reward_coins:u,coin_type_a:d,coin_type_b:f}],t)}),t}validateActiveIdSlippage(e,t){let{pool_id:r,active_id:i,max_price_slippage:s,bin_step:u,coin_type_a:d,coin_type_b:f}=e,{dlmm_router:c}=this.sdk.sdkOptions,p=F.getBinShift(i,u,s),_=Number((0,I.asUintN)(BigInt(i)));return t.moveCall({target:`${c.published_at}::utils::validate_active_id_slippage`,arguments:[typeof r=="string"?t.object(r):r,t.pure.u32(Number(_)),t.pure.u32(Number(p))],typeArguments:[d,f]}),t}closePositionPayload(e,t){let{pool_id:r,position_id:i,reward_coins:s,coin_type_a:u,coin_type_b:d}=e,{dlmm_pool:f}=this.sdk.sdkOptions,{versioned_id:c,global_config_id:p}=(0,I.getPackagerConfigs)(f);t=t||new he.Transaction,this.updatePositionFeeAndRewards({pool_id:r,position_id:i,coin_type_a:u,coin_type_b:d},t),this.collectRewardPayload([{pool_id:r,position_id:i,reward_coins:s,coin_type_a:u,coin_type_b:d}],t);let[_,y,P]=t.moveCall({target:`${f.published_at}::pool::close_position`,arguments:[t.object(r),t.object(i),t.object(p),t.object(c),t.object(I.CLOCK_ADDRESS)],typeArguments:[u,d]}),S=I.CoinAssist.fromBalance(y,u,t),B=I.CoinAssist.fromBalance(P,d,t);return t.moveCall({target:`${f.published_at}::pool::destroy_close_position_cert`,arguments:[_,t.object(c)],typeArguments:[]}),t.transferObjects([S,B],this.sdk.getSenderAddress()),t}async getActiveBinIfInRange(e,t,r,i,s,u=!1){if(i<=r&&i>=t)return await this._sdk.Pool.getBinInfo(e,i,s,u)}calculateRemoveLiquidityInfo(e){let{bins:t,active_id:r,coin_amount:i}=e,s="fix_amount_a"in e,u=t.filter(y=>y.bin_id<r),d=t.filter(y=>y.bin_id>r),f=(0,I.d)(0),c=(0,I.d)(0),p=[];if(s){p=[...t];let{fix_amount_a:y}=e,P=t.find(k=>k.bin_id===r),S=d.reduce((k,x)=>(0,I.d)(k).plus(x.amount_a),(0,I.d)(0)).add(P?.amount_a||"0"),B=u.reduce((k,x)=>(0,I.d)(k).plus(x.amount_b),(0,I.d)(0)).add(P?.amount_b||"0");f=y?S:B,c=(0,I.d)(i).gte(f)?(0,I.d)(1):(0,I.d)(i).div(f)}else{let{is_only_a:y}=e;p=y?d:u,f=p.reduce((P,S)=>(0,I.d)(P).plus(y?S.amount_a:S.amount_b),(0,I.d)(0)),c=(0,I.d)(i).gte(f)?(0,I.d)(1):(0,I.d)(i).div(f)}if((0,I.d)(f).isZero())return K("InsufficientLiquidity",new Error("Insufficient liquidity"),{[I.DETAILS_KEYS.METHOD_NAME]:"calculateRemoveLiquidityResult",[I.DETAILS_KEYS.REQUEST_PARAMS]:e});let _=F.processBinsByRate([...p],c.toFixed());if(_.has_invalid_amount){let y=(0,I.d)(i),P=[],S=s?e.fix_amount_a:e.is_only_a;for(let B of p){if(y.lte(0))break;let k=S?B.amount_a:B.amount_b,x=S?B.amount_b:B.amount_a,R=(0,I.d)((0,I.d)(y).div(k).toFixed(9,L.ROUND_UP));R=R.gt(1)?(0,I.d)(1):R;let C=(0,I.d)(k).mul(R);y=y.minus(C);let T=(0,I.d)(x).mul(R),z=(0,I.d)(B.liquidity).mul(R);P.push({...B,amount_a:S?C.toFixed(0):T.toFixed(0),amount_b:S?T.toFixed(0):C.toFixed(0),liquidity:z.toFixed(0)})}return{bins:P,amount_a:P.reduce((B,k)=>(0,I.d)(B).plus(k.amount_a),(0,I.d)(0)).toFixed(0),amount_b:P.reduce((B,k)=>(0,I.d)(B).plus(k.amount_b),(0,I.d)(0)).toFixed(0)}}return _.bins}async calculateAddLiquidityInfo(e){let t="fix_amount_a"in e,{active_id:r,bin_step:i,lower_bin_id:s,upper_bin_id:u,active_bin_of_pool:d,strategy_type:f,pool_id:c}=e,p;if(t){let{coin_amount:_,fix_amount_a:y}=e;p=Pe.autoFillCoinByStrategyV2(r,i,_,y,s,u,f,d)}else p=Pe.toAmountsBothSideByStrategy(r,i,s,u,e.amount_a,e.amount_b,f,d);if(d&&c){let _=p.bins.findIndex(y=>y.bin_id===r);if(_!==-1){let y=await this._sdk.Pool.getPool(c,!1);if(y){let{fees_a:P,fees_b:S}=Oe.getCompositionFees(d,p.bins[_],y.variable_parameters),B=p.bins[_];B.amount_a=(0,I.d)(B.amount_a).sub(P).toFixed(0),B.amount_b=(0,I.d)(B.amount_b).sub(S).toFixed(0),p.bins[_]=B,console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_a:",P),console.log("\u{1F680} ~ PositionModule ~ calculateAddLiquidityInfo ~ fees_b:",S)}}}return p}removeLiquidityPayload(e){let{pool_id:t,position_id:r,bin_infos:i,reward_coins:s,slippage:u,coin_type_a:d,coin_type_b:f,active_id:c,collect_fee:p,bin_step:_,remove_percent:y}=e,{dlmm_pool:P}=this.sdk.sdkOptions,{bins:S}=i,B=new he.Transaction;(p||s.length>0)&&this.updatePositionFeeAndRewards({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},B),p&&this.collectFeePayload({pool_id:t,position_id:r,coin_type_a:d,coin_type_b:f},B),this.collectRewardPayload([{pool_id:t,position_id:r,reward_coins:s,coin_type_a:d,coin_type_b:f}],B);let{versioned_id:k,global_config_id:x}=(0,I.getPackagerConfigs)(P);if(y){let R=(0,I.asUintN)(BigInt(S[0].bin_id)),C=(0,I.asUintN)(BigInt(S[S.length-1].bin_id)),T=Number((0,I.d)(y).mul(1e4).toFixed(0)),[z,U]=B.moveCall({target:`${P.published_at}::pool::remove_liquidity_by_percent`,arguments:[B.object(t),B.object(r),B.pure.u32(Number(R)),B.pure.u32(Number(C)),B.pure.u16(T),B.object(x),B.object(k),B.object(I.CLOCK_ADDRESS)],typeArguments:[d,f]}),Y=I.CoinAssist.fromBalance(z,d,B),et=I.CoinAssist.fromBalance(U,f,B);B.transferObjects([Y,et],this.sdk.getSenderAddress())}else{let R=B.pure.vector("u32",S.map(et=>Number((0,I.asUintN)(BigInt(et.bin_id))))),C=B.pure.vector("u128",S.map(et=>et.liquidity)),[T,z]=B.moveCall({target:`${P.published_at}::pool::remove_liquidity`,arguments:[B.object(t),B.object(r),R,C,B.object(x),B.object(k),B.object(I.CLOCK_ADDRESS)],typeArguments:[d,f]}),U=I.CoinAssist.fromBalance(T,d,B),Y=I.CoinAssist.fromBalance(z,f,B);B.transferObjects([U,Y],this.sdk.getSenderAddress())}return this.validateActiveIdSlippage({pool_id:t,active_id:c,max_price_slippage:u,bin_step:_,coin_type_a:d,coin_type_b:f},B),B}async addLiquidityWithPricePayload(e){let{pool_id:t,bin_infos:r,coin_type_a:i,coin_type_b:s,price_base_coin:u,price:d,lower_price:f,upper_price:c,bin_step:p,strategy_type:_,active_bin_of_pool:y,decimals_a:P,decimals_b:S,use_bin_infos:B,max_price_slippage:k}=e,x,R,C,T=r;if(u==="coin_a")x=F.getBinIdFromPrice(f,p,!1,P,S),R=F.getBinIdFromPrice(c,p,!0,P,S),C=F.getBinIdFromPrice(d,p,!0,P,S);else{x=F.getBinIdFromPrice((0,I.d)(1).div(c).toString(),p,!1,P,S),R=F.getBinIdFromPrice((0,I.d)(1).div(f).toString(),p,!0,P,S),C=F.getBinIdFromPrice((0,I.d)(1).div(d).toString(),p,!1,P,S);let Y={amount_a:r.amount_b,amount_b:r.amount_a,active_id:C,bin_step:p,lower_bin_id:x,upper_bin_id:R,active_bin_of_pool:y,strategy_type:_};T=await this.sdk.Position.calculateAddLiquidityInfo(Y)}let U={pool_id:t,active_id:C,bin_infos:T,coin_type_a:i,coin_type_b:s,lower_bin_id:x,upper_bin_id:R,strategy_type:_,use_bin_infos:B,max_price_slippage:k,bin_step:p};return this.addLiquidityPayload(U)}addLiquidityPayload(e,t){let{pool_id:r,bin_infos:i,coin_type_a:s,coin_type_b:u,active_id:d,strategy_type:f,max_price_slippage:c,bin_step:p,use_bin_infos:_=!1}=e;t=t||new he.Transaction;let y="lower_bin_id"in e,P=[];if(y){let S=F.splitBinLiquidityInfo(i,e.lower_bin_id,e.upper_bin_id);P.push(...S)}else{let S=e.position_id;P.push(i),(e.collect_fee||e.reward_coins.length>0)&&this.updatePositionFeeAndRewards({pool_id:r,position_id:S,coin_type_a:s,coin_type_b:u},t),e.collect_fee&&this.collectFeePayload({pool_id:r,position_id:S,coin_type_a:s,coin_type_b:u},t),e.reward_coins.length>0&&this.collectRewardPayload([{pool_id:r,position_id:S,reward_coins:e.reward_coins,coin_type_a:s,coin_type_b:u}],t)}return P.forEach((S,B)=>{console.log("\u{1F680} ~ PositionModule ~ addLiquidityPayload ~ liquidity_bin:",B,S);let{amount_a:k,amount_b:x,bins:R}=S,C=I.CoinAssist.buildCoinWithBalance(BigInt(k),s,t),T=I.CoinAssist.buildCoinWithBalance(BigInt(x),u,t);_?this.addLiquidityInternal({pool_id:r,coin_type_a:s,coin_type_b:u,active_id:d,liquidity_bin:S,tx:t,coin_a_obj_id:C,coin_b_obj_id:T,position_id:y?void 0:e.position_id,max_price_slippage:c,bin_step:p}):this.addLiquidityStrategyInternal({pool_id:r,coin_type_a:s,coin_type_b:u,active_id:d,liquidity_bin:S,tx:t,max_price_slippage:c,bin_step:p,coin_a_obj_id:C,coin_b_obj_id:T,strategy_type:f,position_id:y?void 0:e.position_id})}),t}addLiquidityStrategyInternal(e){let{max_price_slippage:t,bin_step:r,position_id:i,pool_id:s,coin_type_a:u,coin_type_b:d,active_id:f,liquidity_bin:c,tx:p,coin_a_obj_id:_,coin_b_obj_id:y,strategy_type:P}=e,{dlmm_pool:S,dlmm_router:B}=this.sdk.sdkOptions,{versioned_id:k,global_config_id:x}=(0,I.getPackagerConfigs)(S),{bins:R,amount_a:C,amount_b:T}=c,z=i,U=R[0].bin_id,Y=R[R.length-1].bin_id,et=(0,I.asUintN)(BigInt(U)),it=Number((0,I.asUintN)(BigInt(f))),ut=F.getBinShift(f,r,t),X=Di(P);if(i===void 0){let at=Y-U+1;at>1e3&&K("InvalidBinWidth",new Error("Width is too large"),{[I.DETAILS_KEYS.METHOD_NAME]:"openPosition"}),z=p.moveCall({target:`${B.published_at}::${X}::open_position`,arguments:[typeof s=="string"?p.object(s):s,_,y,p.pure.u64(C),p.pure.u64(T),p.pure.u32(Number(et)),p.pure.u16(Number(at)),p.pure.u32(Number(it)),p.pure.u32(Number(ut)),p.object(x),p.object(k),p.object(I.CLOCK_ADDRESS)],typeArguments:[u,d]})}else{let at=R.filter(l=>l.amount_a!=="0"||l.amount_b!=="0");if(at.length===0)return K("InvalidParams",new Error("No bins to add liquidity"),{[I.DETAILS_KEYS.METHOD_NAME]:"addLiquidityStrategyInternal",[I.DETAILS_KEYS.REQUEST_PARAMS]:e});let v=(0,I.asUintN)(BigInt(at[0].bin_id)),o=(0,I.asUintN)(BigInt(at[at.length-1].bin_id));p.moveCall({target:`${B.published_at}::${X}::add_liquidity`,arguments:[typeof s=="string"?p.object(s):s,p.object(i),_,y,p.pure.u64(C),p.pure.u64(T),p.pure.u32(Number(v)),p.pure.u32(Number(o)),p.pure.u32(Number(it)),p.pure.u32(Number(ut)),p.object(x),p.object(k),p.object(I.CLOCK_ADDRESS)],typeArguments:[u,d]})}return z?p.transferObjects([z,_,y],this.sdk.getSenderAddress()):p.transferObjects([_,y],this.sdk.getSenderAddress()),p}addLiquidityInternal(e){let{position_id:t,pool_id:r,coin_type_a:i,coin_type_b:s,active_id:u,liquidity_bin:d,tx:f,coin_a_obj_id:c,coin_b_obj_id:p,max_price_slippage:_,bin_step:y}=e,{bins:P}=d,{dlmm_pool:S,dlmm_router:B}=this.sdk.sdkOptions,{versioned_id:k,global_config_id:x}=(0,I.getPackagerConfigs)(S),R=f.pure.vector("u64",P.map(Y=>Y.amount_a)),C=f.pure.vector("u64",P.map(Y=>Y.amount_b)),T=f.makeMoveVec({elements:P.map(Y=>f.pure.u32(Number((0,I.asUintN)(BigInt(Y.bin_id))))),type:"u32"}),z=d.bins[0].bin_id,U=d.bins[d.bins.length-1].bin_id;if(t===void 0){U-z+1>1e3&&K("InvalidBinWidth",new Error("Width is too large"),{[I.DETAILS_KEYS.METHOD_NAME]:"openPosition"});let et=f.moveCall({target:`${B.published_at}::add_liquidity::open_position`,arguments:[typeof r=="string"?f.object(r):r,c,p,T,R,C,f.object(x),f.object(k),f.object(I.CLOCK_ADDRESS)],typeArguments:[i,s]});u>=z&&u<=U&&this.validateActiveIdSlippage({pool_id:r,active_id:u,max_price_slippage:_,bin_step:y,coin_type_a:i,coin_type_b:s},f),f.transferObjects([c,p,et],this.sdk.getSenderAddress())}else f.moveCall({target:`${B.published_at}::add_liquidity::add_liquidity`,arguments:[typeof r=="string"?f.object(r):r,f.object(t),c,p,T,R,C,f.object(x),f.object(k),f.object(I.CLOCK_ADDRESS)],typeArguments:[i,s]}),u>=z&&u<=U&&this.validateActiveIdSlippage({pool_id:r,active_id:u,max_price_slippage:_,bin_step:y,coin_type_a:i,coin_type_b:s},f),f.transferObjects([c,p],this.sdk.getSenderAddress());return f}async fetchPositionFeeAndReward(e){let t=new he.Transaction;this.collectRewardAndFeePayload(e,t);let r=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:t,sender:(0,Or.normalizeSuiAddress)("0x0")});if(r.error!=null)return K("FetchError",new Error(r.error),{[I.DETAILS_KEYS.METHOD_NAME]:"fetchPositionFeeAndReward",[I.DETAILS_KEYS.REQUEST_PARAMS]:{options:e,totalOptions:e.length}});let i=Ti(r),s=ki(r);return{feeData:i,rewardData:s}}};var qi=require("@mysten/sui/transactions"),zt=require("@cetusprotocol/common-sdk");var Ir=require("@mysten/sui/utils");var Ge=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async preSwapQuote(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{pool_id:r,coin_type_a:i,coin_type_b:s,a2b:u,by_amount_in:d,in_amount:f}=e,{versioned_id:c,global_config_id:p}=(0,zt.getPackagerConfigs)(t),_=new qi.Transaction;_.moveCall({target:`${t.published_at}::pool::flash_swap`,arguments:[_.object(r),_.pure.bool(u),_.pure.bool(d),_.pure.u64(f),_.object(p),_.object(c),_.object(zt.CLOCK_ADDRESS)],typeArguments:[i,s]});let y=await this.sdk.FullClient.devInspectTransactionBlock({transactionBlock:_,sender:(0,Ir.normalizeSuiAddress)("0x0")});if(y.error!=null)return K("FetchError",new Error(y.error),{[zt.DETAILS_KEYS.METHOD_NAME]:"fetchSwapQuote",[zt.DETAILS_KEYS.REQUEST_PARAMS]:e});let P=xi(y,u);return P==null?K("FetchError",new Error("No quote info"),{[zt.DETAILS_KEYS.METHOD_NAME]:"preSwapQuote",[zt.DETAILS_KEYS.REQUEST_PARAMS]:e}):(P.a2b=u,P)}swapPayload(e){let{dlmm_pool:t,dlmm_router:r}=this._sdk.sdkOptions,{quote_obj:i,coin_type_a:s,coin_type_b:u,by_amount_in:d,slippage:f,partner:c}=e,{pool_id:p,in_amount:_,out_amount:y,a2b:P}=i,S=new qi.Transaction,B=d?_:(0,zt.d)(_).mul(1+f).toFixed(0),k=d?(0,zt.d)(y).mul(1-f).toFixed(0):y,x=zt.CoinAssist.buildCoinWithBalance(BigInt(B),P?s:u,S),{versioned_id:R,global_config_id:C}=(0,zt.getPackagerConfigs)(t);return console.log("\u{1F680} ~ SwapModule ~ option:",{...e,in_amount_limit:B,out_amount_limit:k}),c?S.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b_with_partner":"swap_b2a_with_partner"}`,arguments:[S.object(p),S.object(c),x,S.pure.bool(d),S.pure.u64(BigInt(d?_:y)),S.pure.u64(BigInt(d?k:B)),S.object(C),S.object(R),S.object(zt.CLOCK_ADDRESS)],typeArguments:[s,u]}):S.moveCall({target:`${r.published_at}::swap::${P?"swap_a2b":"swap_b2a"}`,arguments:[S.object(p),x,S.pure.bool(d),S.pure.u64(BigInt(d?_:y)),S.pure.u64(BigInt(d?k:B)),S.object(C),S.object(R),S.object(zt.CLOCK_ADDRESS)],typeArguments:[s,u]}),S.transferObjects([x],this.sdk.getSenderAddress()),S}};var ke=require("@mysten/sui/transactions"),J=require("@cetusprotocol/common-sdk");var Ve=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getPartnerList(){let e=[],{dlmm_pool:t}=this._sdk.sdkOptions,{partners_id:r}=(0,J.getPackagerConfigs)(t);try{let i=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}}),u=(0,J.getObjectFields)(i).partners.fields.contents.map(d=>d.fields.value);u.length>0&&(await this._sdk.FullClient.batchGetObjects(u,{showContent:!0,showType:!0})).forEach(f=>{let c=Ke(f);e.push(c)})}catch(i){return K("FetchError",i,{[J.DETAILS_KEYS.METHOD_NAME]:"getPartnerList",[J.DETAILS_KEYS.REQUEST_PARAMS]:r})}return e}async getPartnerCapId(e,t){let{dlmm_pool:r}=this._sdk.sdkOptions;try{let i=`partner_cap_id_${e}_${t}`,s=this._sdk.getCache(i);if(s)return s;let u=await this._sdk.FullClient.getOwnedObjects({owner:e,options:{showContent:!0,showType:!0},filter:{StructType:`${r.package_id}::partner::PartnerCap`}}),d;return u.data.forEach(f=>{let c=(0,J.getObjectFields)(f);c.partner_id===t&&(d=c.id.id,this._sdk.updateCache(i,d))}),d||K("NotFound",new Error("Partner cap not found"),{[J.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[J.DETAILS_KEYS.REQUEST_PARAMS]:{owner:e,partner_id:t}})}catch(i){return K("FetchError",i,{[J.DETAILS_KEYS.METHOD_NAME]:"getPartnerCapId",[J.DETAILS_KEYS.REQUEST_PARAMS]:{owner:e,partner_id:t}})}}async getPartnerBalance(e){try{let t=await this._sdk.FullClient.getDynamicFieldsByPage(e),r=[],i=t.data.map(s=>s.objectId);return i.length>0&&(await this._sdk.FullClient.batchGetObjects(i,{showContent:!0,showType:!0})).forEach(u=>{let d=(0,J.getObjectFields)(u);console.log(d),r.push({coin_type:(0,J.fixCoinType)(d.name,!1),balance:d.value})}),r}catch(t){return K("FetchError",t,{[J.DETAILS_KEYS.METHOD_NAME]:"getPartnerBalance",[J.DETAILS_KEYS.REQUEST_PARAMS]:e})}}async getPartner(e){try{let t=await this._sdk.FullClient.getObject({id:e,options:{showContent:!0}});return Ke(t)}catch(t){return K("FetchError",t,{[J.DETAILS_KEYS.METHOD_NAME]:"getPartner",[J.DETAILS_KEYS.REQUEST_PARAMS]:e})}}updateRefFeeRatePayload(e,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{partner_id:i,ref_fee_rate:s}=e,{global_config_id:u,versioned_id:d}=(0,J.getPackagerConfigs)(r);t=t||new ke.Transaction;let f=(0,J.d)(s).mul(1e4);return f.gt(1e4)?K("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[J.DETAILS_KEYS.METHOD_NAME]:"updateRefFeeRatePayload",[J.DETAILS_KEYS.REQUEST_PARAMS]:e}):(t.moveCall({target:`${r.published_at}::partner::update_ref_fee_rate`,arguments:[t.object(i),t.pure.u64(f.toNumber()),t.object(u),t.object(d)],typeArguments:[]}),t)}async claimRefFeePayload(e){let{partner_id:t,partner_cap_id:r,fee_coin_types:i}=e,{dlmm_pool:s}=this._sdk.sdkOptions,{versioned_id:u}=(0,J.getPackagerConfigs)(s),d=new ke.Transaction,f=r;return f||(f=await this.getPartnerCapId(this._sdk.getSenderAddress(),t)),i.forEach(c=>{d.moveCall({target:`${s.published_at}::partner::claim_ref_fee`,arguments:[d.object(t),d.object(f),d.object(u)],typeArguments:[c]})}),d}updateTimeRangePayload(e,t){let{dlmm_pool:r}=this._sdk.sdkOptions,{partner_id:i,start_time:s,end_time:u}=e,{global_config_id:d,versioned_id:f}=(0,J.getPackagerConfigs)(r);t=t||new ke.Transaction;let c=s>1e12?Math.floor(s/1e3):s,p=u>1e12?Math.floor(u/1e3):u;return p<=c?K("InvalidParams",new Error("end_time must be greater than start_time"),{[J.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[J.DETAILS_KEYS.REQUEST_PARAMS]:e}):(t.moveCall({target:`${r.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(J.CLOCK_ADDRESS)],typeArguments:[]}),t)}createPartnerPayload(e){let{dlmm_pool:t}=this._sdk.sdkOptions,{name:r,ref_fee_rate:i,start_time:s,end_time:u,recipient:d}=e,{partners_id:f,global_config_id:c,versioned_id:p}=(0,J.getPackagerConfigs)(t),_=new ke.Transaction,y=(0,J.d)(i).mul(1e4);if(y.gt(1e4))return K("InvalidParams",new Error("ref_fee_rate is cannot be greater than 1"),{[J.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[J.DETAILS_KEYS.REQUEST_PARAMS]:e});let P=s>1e12?Math.floor(s/1e3):s,S=u>1e12?Math.floor(u/1e3):u;return S<=P?K("InvalidParams",new Error("end_time must be greater than start_time"),{[J.DETAILS_KEYS.METHOD_NAME]:"createPartnerPayload",[J.DETAILS_KEYS.REQUEST_PARAMS]:e}):(_.moveCall({target:`${t.published_at}::partner::create_partner`,arguments:[_.object(f),_.pure.string(r),_.pure.u64(y.toNumber()),_.pure.u64(P),_.pure.u64(S),_.pure.address(d),_.object(c),_.object(p)],typeArguments:[]}),_)}};var Xe=require("@mysten/sui/transactions"),ft=require("@cetusprotocol/common-sdk"),ji=wi(De()),Je=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}async getRewardPeriodEmission(e,t,r){let i=await this._sdk.FullClient.getDynamicFieldsByPage(e),s=[],u=i.data.map(p=>p.objectId);u.length>0&&(await this._sdk.FullClient.batchGetObjects(u,{showContent:!0})).forEach(_=>{let y=(0,ft.getObjectFields)(_),P=ft.MathUtil.u128ToI128(new ji.default(y.value.fields.value.fields.bits)).toString(),S=y.name,B=new Date(Number(S)*1e3).toLocaleString(),x={emissions_per_second:"0",emissions_per_day:"0",emissions_per:ft.MathUtil.fromX64(new ji.default(P)).toString(),time:S,visualized_time:B};s.push(x)});let d=s.sort((p,_)=>Number(p.time)-Number(_.time)),f=[];f.push({emissions_per_second:t,emissions_per_day:(0,ft.d)(t).mul(3600*24).toString(),emissions_per:"0",time:r.toString(),visualized_time:new Date(r*1e3).toLocaleString()});let c=t;for(let p=0;p<d.length;p++){let _=d[p];if((0,ft.d)(_.time).lte(r))continue;c=(0,ft.d)(c).add((0,ft.d)(_.emissions_per)).toString();let y=(0,ft.d)(c).mul(3600*24).toString();(0,ft.d)(c).lt(0)?(_.emissions_per_second="0",_.emissions_per_day="0"):(_.emissions_per_second=c,_.emissions_per_day=y),f.push(_)}return f}addRewardPayload(e,t){t=t||new Xe.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:s}=(0,ft.getPackagerConfigs)(r),{pool_id:u,reward_coin_type:d,reward_amount:f,start_time_seconds:c,end_time_seconds:p,coin_type_a:_,coin_type_b:y}=e,P=ft.CoinAssist.buildCoinWithBalance(BigInt(f),d,t);return t.pure.option("u64",c),t.moveCall({target:`${r.published_at}::pool::add_reward`,arguments:[t.object(u),P,t.pure.option("u64",c),t.pure.u64(p),t.object(i),t.object(s),t.object(ft.CLOCK_ADDRESS)],typeArguments:[_,y,d]}),t}initRewardPayload(e,t){t=t||new Xe.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:s}=(0,ft.getPackagerConfigs)(r),{pool_id:u,reward_coin_types:d,coin_type_a:f,coin_type_b:c}=e;return d.forEach(p=>{t.moveCall({target:`${r.published_at}::pool::initialize_reward`,arguments:[t.object(u),t.object(i),t.object(s),t.object(ft.CLOCK_ADDRESS)],typeArguments:[f,c,p]})}),t}buildRewardAccessPayload(e,t){t=t||new Xe.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:s}=(0,ft.getPackagerConfigs)(r),{pool_id:u,type:d,coin_type_a:f,coin_type_b:c}=e;return t.moveCall({target:`${r.published_at}::pool::${d==="to_public"?"make_reward_public":"make_reward_private"}`,arguments:[t.object(u),t.object(i),t.object(s)],typeArguments:[f,c]}),t}};var Cr=require("@mysten/sui/transactions"),ee=require("@cetusprotocol/common-sdk");var Ye=class{constructor(e){this._sdk=e}get sdk(){return this._sdk}buildRewardWhiteListPayload(e,t){t=t||new Cr.Transaction;let{dlmm_pool:r}=this._sdk.sdkOptions,{global_config_id:i,versioned_id:s}=(0,ee.getPackagerConfigs)(r),{reward_coin_types:u,type:d}=e;return u.forEach(f=>{t.moveCall({target:`${r.published_at}::config::${d==="add"?"add_reward_whitelist":"remove_reward_whitelist"}`,arguments:[t.object(i),t.object(s)],typeArguments:[f]})}),t}async getBinStepConfigList(e){let t=await this._sdk.FullClient.getDynamicFields({parentId:e}),r=[],i=t.data.map(s=>s.objectId);return i.length>0&&(await this._sdk.FullClient.batchGetObjects(i,{showContent:!0})).forEach(u=>{let f={...(0,ee.getObjectFields)(u).value.fields};r.push(f)}),r}async getDlmmGlobalConfig(){let{dlmm_pool:e}=this._sdk.sdkOptions,{global_config_id:t}=(0,ee.getPackagerConfigs)(e);try{let r=await this._sdk.FullClient.getObject({id:t,options:{showContent:!0}}),i=(0,ee.getObjectFields)(r),s=i.reward_config.fields,u=s.reward_white_list?.fields?.contents?.map(f=>(0,ee.fixCoinType)(f.fields.key.fields.name,!1))||[];return{id:i.id.id,acl:{id:i.acl.fields.permissions.fields.id.id,size:i.acl.fields.permissions.fields.size},allowed_list:{id:i.allowed_list.fields.id.id,size:i.allowed_list.fields.size},denied_list:{id:i.denied_list.fields.id.id,size:i.denied_list.fields.size},bin_steps:{id:i.bin_steps.fields.id.id,size:i.bin_steps.fields.size},reward_white_list:u,blocked_position:{id:i.restriction.fields.blocked_position.fields.permissions.fields.id.id,size:i.restriction.fields.blocked_position.fields.permissions.fields.size},blocked_user:{id:i.restriction.fields.blocked_user.fields.permissions.fields.id.id,size:i.restriction.fields.blocked_user.fields.permissions.fields.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(r){return console.log("fetchGlobalConfig error: ",r),K("FetchError",r,{[ee.DETAILS_KEYS.METHOD_NAME]:"getBinStepConfigs",[ee.DETAILS_KEYS.REQUEST_PARAMS]:t})}}async fetchDlmmSdkConfigs(){let{dlmm_pool:e}=this._sdk.sdkOptions,t={registry_id:"",pools_id:"",global_config_id:"",versioned_id:"",admin_cap_id:"",partners_id:""},i=(await this._sdk.FullClient.getObject({id:e.package_id,options:{showContent:!0,showPreviousTransaction:!0}})).data?.previousTransaction;(await this._sdk.FullClient.getTransactionBlock({digest:i,options:{showEvents:!0}})).events?.forEach(f=>{let c=f.type,p=f.parsedJson;c.includes("versioned::InitEvent")&&(t.versioned_id=p.versioned),c.includes("partner::InitPartnerEvent")&&(t.partners_id=p.partners_id),c.includes("config::InitEvent")&&(t.global_config_id=p.config_id),c.includes("admin_cap::InitEvent")&&(t.admin_cap_id=p.admin_cap_id),c.includes("registry::RegistryEvent")&&(t.registry_id=p.pools_id)});let u=await this._sdk.FullClient.getObject({id:t.registry_id,options:{showContent:!0}}),d=(0,ee.getObjectFields)(u);return t.pools_id=d.pools.fields.id.id,t}};var xe=class n extends Tr.SdkWrapper{constructor(e){super(e),this._pool=new Te(this),this._position=new ze(this),this._swap=new Ge(this),this._partner=new Ve(this),this._reward=new Je(this),this._config=new Ye(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"?n.createCustomSDK({...vi,...e}):n.createCustomSDK({...Li,...e})}static createCustomSDK(e){return new n(e)}};var Hn=xe;0&&(module.exports={BASIS_POINT,BASIS_POINT_MAX,BIN_BOUND,BinUtils,CetusDlmmSDK,DEFAULT_MAX_WEIGHT,DEFAULT_MIN_WEIGHT,FEE_PRECISION,FeeUtils,MAX_BIN_ID,MAX_BIN_PER_POSITION,MAX_FEE_RATE,MIN_BIN_ID,ONE,PoolModule,REWARD_PERIOD,REWARD_PERIOD_START_AT,SCALE_OFFSET,StrategyType,StrategyUtils,WeightUtils,buildPoolKey,dlmmMainnet,dlmmTestnet,generateRewardSchedule,getRouterModule,parseBinInfo,parseBinInfoList,parseCurrentRewardPeriodEmission,parseDlmmBasePool,parseDlmmPool,parseDlmmPosition,parseLiquidityShares,parsePartner,parsePoolTransactionInfo,parseRewardPeriodEmission,parseStrategyType,parsedDlmmPosFeeData,parsedDlmmPosRewardData,parsedSwapQuoteData,poolFilterEvenTypes,safeAmount,safeMulAmount});
|
|
2
2
|
/*! Bundled license information:
|
|
3
3
|
|
|
4
4
|
decimal.js/decimal.mjs:
|