llll-sdk 0.0.0-experimental-20260120162634 → 0.0.0-experimental-20260121164846

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var Nr=Object.create;var Oe=Object.defineProperty;var Er=Object.getOwnPropertyDescriptor;var Ir=Object.getOwnPropertyNames;var Br=Object.getPrototypeOf,qr=Object.prototype.hasOwnProperty;var Fr=(n,i)=>()=>(i||n((i={exports:{}}).exports,i),i.exports),Ur=(n,i)=>{for(var r in i)Oe(n,r,{get:i[r],enumerable:!0})},tr=(n,i,r,o)=>{if(i&&typeof i=="object"||typeof i=="function")for(let t of Ir(i))!qr.call(n,t)&&t!==r&&Oe(n,t,{get:()=>i[t],enumerable:!(o=Er(i,t))||o.enumerable});return n};var yi=(n,i,r)=>(r=n!=null?Nr(Br(n)):{},tr(i||!n||!n.__esModule?Oe(r,"default",{value:n,enumerable:!0}):r,n)),jr=n=>tr(Oe({},"__esModule",{value:!0}),n);var Cr=Fr((Ar,Fi)=>{"use strict";(function(n,i){"use strict";function r(p,e){if(!p)throw new Error(e||"Assertion failed")}function o(p,e){p.super_=e;var s=function(){};s.prototype=e.prototype,p.prototype=new s,p.prototype.constructor=p}function t(p,e,s){if(t.isBN(p))return p;this.negative=0,this.words=null,this.length=0,this.red=null,p!==null&&((e==="le"||e==="be")&&(s=e,e=10),this._init(p||0,e||10,s||"be"))}typeof n=="object"?n.exports=t:i.BN=t,t.BN=t,t.wordSize=26;var a;try{typeof window<"u"&&typeof window.Buffer<"u"?a=window.Buffer:a=require("buffer").Buffer}catch{}t.isBN=function(e){return e instanceof t?!0:e!==null&&typeof e=="object"&&e.constructor.wordSize===t.wordSize&&Array.isArray(e.words)},t.max=function(e,s){return e.cmp(s)>0?e:s},t.min=function(e,s){return e.cmp(s)<0?e:s},t.prototype._init=function(e,s,l){if(typeof e=="number")return this._initNumber(e,s,l);if(typeof e=="object")return this._initArray(e,s,l);s==="hex"&&(s=16),r(s===(s|0)&&s>=2&&s<=36),e=e.toString().replace(/\s+/g,"");var f=0;e[0]==="-"&&(f++,this.negative=1),f<e.length&&(s===16?this._parseHex(e,f,l):(this._parseBase(e,s,f),l==="le"&&this._initArray(this.toArray(),s,l)))},t.prototype._initNumber=function(e,s,l){e<0&&(this.negative=1,e=-e),e<67108864?(this.words=[e&67108863],this.length=1):e<4503599627370496?(this.words=[e&67108863,e/67108864&67108863],this.length=2):(r(e<9007199254740992),this.words=[e&67108863,e/67108864&67108863,1],this.length=3),l==="le"&&this._initArray(this.toArray(),s,l)},t.prototype._initArray=function(e,s,l){if(r(typeof e.length=="number"),e.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(e.length/3),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var h,v,k=0;if(l==="be")for(f=e.length-1,h=0;f>=0;f-=3)v=e[f]|e[f-1]<<8|e[f-2]<<16,this.words[h]|=v<<k&67108863,this.words[h+1]=v>>>26-k&67108863,k+=24,k>=26&&(k-=26,h++);else if(l==="le")for(f=0,h=0;f<e.length;f+=3)v=e[f]|e[f+1]<<8|e[f+2]<<16,this.words[h]|=v<<k&67108863,this.words[h+1]=v>>>26-k&67108863,k+=24,k>=26&&(k-=26,h++);return this._strip()};function d(p,e){var s=p.charCodeAt(e);if(s>=48&&s<=57)return s-48;if(s>=65&&s<=70)return s-55;if(s>=97&&s<=102)return s-87;r(!1,"Invalid character in "+p)}function c(p,e,s){var l=d(p,s);return s-1>=e&&(l|=d(p,s-1)<<4),l}t.prototype._parseHex=function(e,s,l){this.length=Math.ceil((e.length-s)/6),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var h=0,v=0,k;if(l==="be")for(f=e.length-1;f>=s;f-=2)k=c(e,s,f)<<h,this.words[v]|=k&67108863,h>=18?(h-=18,v+=1,this.words[v]|=k>>>26):h+=8;else{var w=e.length-s;for(f=w%2===0?s+1:s;f<e.length;f+=2)k=c(e,s,f)<<h,this.words[v]|=k&67108863,h>=18?(h-=18,v+=1,this.words[v]|=k>>>26):h+=8}this._strip()};function m(p,e,s,l){for(var f=0,h=0,v=Math.min(p.length,s),k=e;k<v;k++){var w=p.charCodeAt(k)-48;f*=l,w>=49?h=w-49+10:w>=17?h=w-17+10:h=w,r(w>=0&&h<l,"Invalid character"),f+=h}return f}t.prototype._parseBase=function(e,s,l){this.words=[0],this.length=1;for(var f=0,h=1;h<=67108863;h*=s)f++;f--,h=h/s|0;for(var v=e.length-l,k=v%f,w=Math.min(v,v-k)+l,u=0,y=l;y<w;y+=f)u=m(e,y,y+f,s),this.imuln(h),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u);if(k!==0){var $=1;for(u=m(e,y,e.length,s),y=0;y<k;y++)$*=s;this.imuln($),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u)}this._strip()},t.prototype.copy=function(e){e.words=new Array(this.length);for(var s=0;s<this.length;s++)e.words[s]=this.words[s];e.length=this.length,e.negative=this.negative,e.red=this.red};function g(p,e){p.words=e.words,p.length=e.length,p.negative=e.negative,p.red=e.red}if(t.prototype._move=function(e){g(e,this)},t.prototype.clone=function(){var e=new t(null);return this.copy(e),e},t.prototype._expand=function(e){for(;this.length<e;)this.words[this.length++]=0;return this},t.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},t.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{t.prototype[Symbol.for("nodejs.util.inspect.custom")]=M}catch{t.prototype.inspect=M}else t.prototype.inspect=M;function M(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var P=["","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"],A=[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],T=[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];t.prototype.toString=function(e,s){e=e||10,s=s|0||1;var l;if(e===16||e==="hex"){l="";for(var f=0,h=0,v=0;v<this.length;v++){var k=this.words[v],w=((k<<f|h)&16777215).toString(16);h=k>>>24-f&16777215,f+=2,f>=26&&(f-=26,v--),h!==0||v!==this.length-1?l=P[6-w.length]+w+l:l=w+l}for(h!==0&&(l=h.toString(16)+l);l.length%s!==0;)l="0"+l;return this.negative!==0&&(l="-"+l),l}if(e===(e|0)&&e>=2&&e<=36){var u=A[e],y=T[e];l="";var $=this.clone();for($.negative=0;!$.isZero();){var F=$.modrn(y).toString(e);$=$.idivn(y),$.isZero()?l=F+l:l=P[u-F.length]+F+l}for(this.isZero()&&(l="0"+l);l.length%s!==0;)l="0"+l;return this.negative!==0&&(l="-"+l),l}r(!1,"Base should be between 2 and 36")},t.prototype.toNumber=function(){var e=this.words[0];return this.length===2?e+=this.words[1]*67108864:this.length===3&&this.words[2]===1?e+=4503599627370496+this.words[1]*67108864:this.length>2&&r(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-e:e},t.prototype.toJSON=function(){return this.toString(16,2)},a&&(t.prototype.toBuffer=function(e,s){return this.toArrayLike(a,e,s)}),t.prototype.toArray=function(e,s){return this.toArrayLike(Array,e,s)};var O=function(e,s){return e.allocUnsafe?e.allocUnsafe(s):new e(s)};t.prototype.toArrayLike=function(e,s,l){this._strip();var f=this.byteLength(),h=l||Math.max(1,f);r(f<=h,"byte array longer than desired length"),r(h>0,"Requested array length <= 0");var v=O(e,h),k=s==="le"?"LE":"BE";return this["_toArrayLike"+k](v,f),v},t.prototype._toArrayLikeLE=function(e,s){for(var l=0,f=0,h=0,v=0;h<this.length;h++){var k=this.words[h]<<v|f;e[l++]=k&255,l<e.length&&(e[l++]=k>>8&255),l<e.length&&(e[l++]=k>>16&255),v===6?(l<e.length&&(e[l++]=k>>24&255),f=0,v=0):(f=k>>>24,v+=2)}if(l<e.length)for(e[l++]=f;l<e.length;)e[l++]=0},t.prototype._toArrayLikeBE=function(e,s){for(var l=e.length-1,f=0,h=0,v=0;h<this.length;h++){var k=this.words[h]<<v|f;e[l--]=k&255,l>=0&&(e[l--]=k>>8&255),l>=0&&(e[l--]=k>>16&255),v===6?(l>=0&&(e[l--]=k>>24&255),f=0,v=0):(f=k>>>24,v+=2)}if(l>=0)for(e[l--]=f;l>=0;)e[l--]=0},Math.clz32?t.prototype._countBits=function(e){return 32-Math.clz32(e)}:t.prototype._countBits=function(e){var s=e,l=0;return s>=4096&&(l+=13,s>>>=13),s>=64&&(l+=7,s>>>=7),s>=8&&(l+=4,s>>>=4),s>=2&&(l+=2,s>>>=2),l+s},t.prototype._zeroBits=function(e){if(e===0)return 26;var s=e,l=0;return(s&8191)===0&&(l+=13,s>>>=13),(s&127)===0&&(l+=7,s>>>=7),(s&15)===0&&(l+=4,s>>>=4),(s&3)===0&&(l+=2,s>>>=2),(s&1)===0&&l++,l},t.prototype.bitLength=function(){var e=this.words[this.length-1],s=this._countBits(e);return(this.length-1)*26+s};function N(p){for(var e=new Array(p.bitLength()),s=0;s<e.length;s++){var l=s/26|0,f=s%26;e[s]=p.words[l]>>>f&1}return e}t.prototype.zeroBits=function(){if(this.isZero())return 0;for(var e=0,s=0;s<this.length;s++){var l=this._zeroBits(this.words[s]);if(e+=l,l!==26)break}return e},t.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},t.prototype.toTwos=function(e){return this.negative!==0?this.abs().inotn(e).iaddn(1):this.clone()},t.prototype.fromTwos=function(e){return this.testn(e-1)?this.notn(e).iaddn(1).ineg():this.clone()},t.prototype.isNeg=function(){return this.negative!==0},t.prototype.neg=function(){return this.clone().ineg()},t.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},t.prototype.iuor=function(e){for(;this.length<e.length;)this.words[this.length++]=0;for(var s=0;s<e.length;s++)this.words[s]=this.words[s]|e.words[s];return this._strip()},t.prototype.ior=function(e){return r((this.negative|e.negative)===0),this.iuor(e)},t.prototype.or=function(e){return this.length>e.length?this.clone().ior(e):e.clone().ior(this)},t.prototype.uor=function(e){return this.length>e.length?this.clone().iuor(e):e.clone().iuor(this)},t.prototype.iuand=function(e){var s;this.length>e.length?s=e:s=this;for(var l=0;l<s.length;l++)this.words[l]=this.words[l]&e.words[l];return this.length=s.length,this._strip()},t.prototype.iand=function(e){return r((this.negative|e.negative)===0),this.iuand(e)},t.prototype.and=function(e){return this.length>e.length?this.clone().iand(e):e.clone().iand(this)},t.prototype.uand=function(e){return this.length>e.length?this.clone().iuand(e):e.clone().iuand(this)},t.prototype.iuxor=function(e){var s,l;this.length>e.length?(s=this,l=e):(s=e,l=this);for(var f=0;f<l.length;f++)this.words[f]=s.words[f]^l.words[f];if(this!==s)for(;f<s.length;f++)this.words[f]=s.words[f];return this.length=s.length,this._strip()},t.prototype.ixor=function(e){return r((this.negative|e.negative)===0),this.iuxor(e)},t.prototype.xor=function(e){return this.length>e.length?this.clone().ixor(e):e.clone().ixor(this)},t.prototype.uxor=function(e){return this.length>e.length?this.clone().iuxor(e):e.clone().iuxor(this)},t.prototype.inotn=function(e){r(typeof e=="number"&&e>=0);var s=Math.ceil(e/26)|0,l=e%26;this._expand(s),l>0&&s--;for(var f=0;f<s;f++)this.words[f]=~this.words[f]&67108863;return l>0&&(this.words[f]=~this.words[f]&67108863>>26-l),this._strip()},t.prototype.notn=function(e){return this.clone().inotn(e)},t.prototype.setn=function(e,s){r(typeof e=="number"&&e>=0);var l=e/26|0,f=e%26;return this._expand(l+1),s?this.words[l]=this.words[l]|1<<f:this.words[l]=this.words[l]&~(1<<f),this._strip()},t.prototype.iadd=function(e){var s;if(this.negative!==0&&e.negative===0)return this.negative=0,s=this.isub(e),this.negative^=1,this._normSign();if(this.negative===0&&e.negative!==0)return e.negative=0,s=this.isub(e),e.negative=1,s._normSign();var l,f;this.length>e.length?(l=this,f=e):(l=e,f=this);for(var h=0,v=0;v<f.length;v++)s=(l.words[v]|0)+(f.words[v]|0)+h,this.words[v]=s&67108863,h=s>>>26;for(;h!==0&&v<l.length;v++)s=(l.words[v]|0)+h,this.words[v]=s&67108863,h=s>>>26;if(this.length=l.length,h!==0)this.words[this.length]=h,this.length++;else if(l!==this)for(;v<l.length;v++)this.words[v]=l.words[v];return this},t.prototype.add=function(e){var s;return e.negative!==0&&this.negative===0?(e.negative=0,s=this.sub(e),e.negative^=1,s):e.negative===0&&this.negative!==0?(this.negative=0,s=e.sub(this),this.negative=1,s):this.length>e.length?this.clone().iadd(e):e.clone().iadd(this)},t.prototype.isub=function(e){if(e.negative!==0){e.negative=0;var s=this.iadd(e);return e.negative=1,s._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(e),this.negative=1,this._normSign();var l=this.cmp(e);if(l===0)return this.negative=0,this.length=1,this.words[0]=0,this;var f,h;l>0?(f=this,h=e):(f=e,h=this);for(var v=0,k=0;k<h.length;k++)s=(f.words[k]|0)-(h.words[k]|0)+v,v=s>>26,this.words[k]=s&67108863;for(;v!==0&&k<f.length;k++)s=(f.words[k]|0)+v,v=s>>26,this.words[k]=s&67108863;if(v===0&&k<f.length&&f!==this)for(;k<f.length;k++)this.words[k]=f.words[k];return this.length=Math.max(this.length,k),f!==this&&(this.negative=1),this._strip()},t.prototype.sub=function(e){return this.clone().isub(e)};function E(p,e,s){s.negative=e.negative^p.negative;var l=p.length+e.length|0;s.length=l,l=l-1|0;var f=p.words[0]|0,h=e.words[0]|0,v=f*h,k=v&67108863,w=v/67108864|0;s.words[0]=k;for(var u=1;u<l;u++){for(var y=w>>>26,$=w&67108863,F=Math.min(u,e.length-1),j=Math.max(0,u-p.length+1);j<=F;j++){var K=u-j|0;f=p.words[K]|0,h=e.words[j]|0,v=f*h+$,y+=v/67108864|0,$=v&67108863}s.words[u]=$|0,w=y|0}return w!==0?s.words[u]=w|0:s.length--,s._strip()}var L=function(e,s,l){var f=e.words,h=s.words,v=l.words,k=0,w,u,y,$=f[0]|0,F=$&8191,j=$>>>13,K=f[1]|0,G=K&8191,J=K>>>13,rt=f[2]|0,V=rt&8191,et=rt>>>13,ft=f[3]|0,tt=ft&8191,X=ft>>>13,me=f[4]|0,dt=me&8191,st=me>>>13,ke=f[5]|0,nt=ke&8191,at=ke>>>13,de=f[6]|0,pt=de&8191,_t=de>>>13,ji=f[7]|0,wt=ji&8191,vt=ji>>>13,$i=f[8]|0,Mt=$i&8191,bt=$i>>>13,Wi=f[9]|0,yt=Wi&8191,kt=Wi>>>13,Zi=h[0]|0,St=Zi&8191,Pt=Zi>>>13,Ki=h[1]|0,At=Ki&8191,Ct=Ki>>>13,zi=h[2]|0,xt=zi&8191,Dt=zi>>>13,Gi=h[3]|0,Tt=Gi&8191,Ot=Gi>>>13,Hi=h[4]|0,Rt=Hi&8191,Lt=Hi>>>13,Vi=h[5]|0,Nt=Vi&8191,Et=Vi>>>13,Xi=h[6]|0,It=Xi&8191,Bt=Xi>>>13,Ji=h[7]|0,qt=Ji&8191,Ft=Ji>>>13,Yi=h[8]|0,Ut=Yi&8191,jt=Yi>>>13,Qi=h[9]|0,$t=Qi&8191,Wt=Qi>>>13;l.negative=e.negative^s.negative,l.length=19,w=Math.imul(F,St),u=Math.imul(F,Pt),u=u+Math.imul(j,St)|0,y=Math.imul(j,Pt);var ri=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(ri>>>26)|0,ri&=67108863,w=Math.imul(G,St),u=Math.imul(G,Pt),u=u+Math.imul(J,St)|0,y=Math.imul(J,Pt),w=w+Math.imul(F,At)|0,u=u+Math.imul(F,Ct)|0,u=u+Math.imul(j,At)|0,y=y+Math.imul(j,Ct)|0;var ni=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(ni>>>26)|0,ni&=67108863,w=Math.imul(V,St),u=Math.imul(V,Pt),u=u+Math.imul(et,St)|0,y=Math.imul(et,Pt),w=w+Math.imul(G,At)|0,u=u+Math.imul(G,Ct)|0,u=u+Math.imul(J,At)|0,y=y+Math.imul(J,Ct)|0,w=w+Math.imul(F,xt)|0,u=u+Math.imul(F,Dt)|0,u=u+Math.imul(j,xt)|0,y=y+Math.imul(j,Dt)|0;var oi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(oi>>>26)|0,oi&=67108863,w=Math.imul(tt,St),u=Math.imul(tt,Pt),u=u+Math.imul(X,St)|0,y=Math.imul(X,Pt),w=w+Math.imul(V,At)|0,u=u+Math.imul(V,Ct)|0,u=u+Math.imul(et,At)|0,y=y+Math.imul(et,Ct)|0,w=w+Math.imul(G,xt)|0,u=u+Math.imul(G,Dt)|0,u=u+Math.imul(J,xt)|0,y=y+Math.imul(J,Dt)|0,w=w+Math.imul(F,Tt)|0,u=u+Math.imul(F,Ot)|0,u=u+Math.imul(j,Tt)|0,y=y+Math.imul(j,Ot)|0;var si=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(si>>>26)|0,si&=67108863,w=Math.imul(dt,St),u=Math.imul(dt,Pt),u=u+Math.imul(st,St)|0,y=Math.imul(st,Pt),w=w+Math.imul(tt,At)|0,u=u+Math.imul(tt,Ct)|0,u=u+Math.imul(X,At)|0,y=y+Math.imul(X,Ct)|0,w=w+Math.imul(V,xt)|0,u=u+Math.imul(V,Dt)|0,u=u+Math.imul(et,xt)|0,y=y+Math.imul(et,Dt)|0,w=w+Math.imul(G,Tt)|0,u=u+Math.imul(G,Ot)|0,u=u+Math.imul(J,Tt)|0,y=y+Math.imul(J,Ot)|0,w=w+Math.imul(F,Rt)|0,u=u+Math.imul(F,Lt)|0,u=u+Math.imul(j,Rt)|0,y=y+Math.imul(j,Lt)|0;var ai=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(ai>>>26)|0,ai&=67108863,w=Math.imul(nt,St),u=Math.imul(nt,Pt),u=u+Math.imul(at,St)|0,y=Math.imul(at,Pt),w=w+Math.imul(dt,At)|0,u=u+Math.imul(dt,Ct)|0,u=u+Math.imul(st,At)|0,y=y+Math.imul(st,Ct)|0,w=w+Math.imul(tt,xt)|0,u=u+Math.imul(tt,Dt)|0,u=u+Math.imul(X,xt)|0,y=y+Math.imul(X,Dt)|0,w=w+Math.imul(V,Tt)|0,u=u+Math.imul(V,Ot)|0,u=u+Math.imul(et,Tt)|0,y=y+Math.imul(et,Ot)|0,w=w+Math.imul(G,Rt)|0,u=u+Math.imul(G,Lt)|0,u=u+Math.imul(J,Rt)|0,y=y+Math.imul(J,Lt)|0,w=w+Math.imul(F,Nt)|0,u=u+Math.imul(F,Et)|0,u=u+Math.imul(j,Nt)|0,y=y+Math.imul(j,Et)|0;var li=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(li>>>26)|0,li&=67108863,w=Math.imul(pt,St),u=Math.imul(pt,Pt),u=u+Math.imul(_t,St)|0,y=Math.imul(_t,Pt),w=w+Math.imul(nt,At)|0,u=u+Math.imul(nt,Ct)|0,u=u+Math.imul(at,At)|0,y=y+Math.imul(at,Ct)|0,w=w+Math.imul(dt,xt)|0,u=u+Math.imul(dt,Dt)|0,u=u+Math.imul(st,xt)|0,y=y+Math.imul(st,Dt)|0,w=w+Math.imul(tt,Tt)|0,u=u+Math.imul(tt,Ot)|0,u=u+Math.imul(X,Tt)|0,y=y+Math.imul(X,Ot)|0,w=w+Math.imul(V,Rt)|0,u=u+Math.imul(V,Lt)|0,u=u+Math.imul(et,Rt)|0,y=y+Math.imul(et,Lt)|0,w=w+Math.imul(G,Nt)|0,u=u+Math.imul(G,Et)|0,u=u+Math.imul(J,Nt)|0,y=y+Math.imul(J,Et)|0,w=w+Math.imul(F,It)|0,u=u+Math.imul(F,Bt)|0,u=u+Math.imul(j,It)|0,y=y+Math.imul(j,Bt)|0;var ui=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(ui>>>26)|0,ui&=67108863,w=Math.imul(wt,St),u=Math.imul(wt,Pt),u=u+Math.imul(vt,St)|0,y=Math.imul(vt,Pt),w=w+Math.imul(pt,At)|0,u=u+Math.imul(pt,Ct)|0,u=u+Math.imul(_t,At)|0,y=y+Math.imul(_t,Ct)|0,w=w+Math.imul(nt,xt)|0,u=u+Math.imul(nt,Dt)|0,u=u+Math.imul(at,xt)|0,y=y+Math.imul(at,Dt)|0,w=w+Math.imul(dt,Tt)|0,u=u+Math.imul(dt,Ot)|0,u=u+Math.imul(st,Tt)|0,y=y+Math.imul(st,Ot)|0,w=w+Math.imul(tt,Rt)|0,u=u+Math.imul(tt,Lt)|0,u=u+Math.imul(X,Rt)|0,y=y+Math.imul(X,Lt)|0,w=w+Math.imul(V,Nt)|0,u=u+Math.imul(V,Et)|0,u=u+Math.imul(et,Nt)|0,y=y+Math.imul(et,Et)|0,w=w+Math.imul(G,It)|0,u=u+Math.imul(G,Bt)|0,u=u+Math.imul(J,It)|0,y=y+Math.imul(J,Bt)|0,w=w+Math.imul(F,qt)|0,u=u+Math.imul(F,Ft)|0,u=u+Math.imul(j,qt)|0,y=y+Math.imul(j,Ft)|0;var ci=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(ci>>>26)|0,ci&=67108863,w=Math.imul(Mt,St),u=Math.imul(Mt,Pt),u=u+Math.imul(bt,St)|0,y=Math.imul(bt,Pt),w=w+Math.imul(wt,At)|0,u=u+Math.imul(wt,Ct)|0,u=u+Math.imul(vt,At)|0,y=y+Math.imul(vt,Ct)|0,w=w+Math.imul(pt,xt)|0,u=u+Math.imul(pt,Dt)|0,u=u+Math.imul(_t,xt)|0,y=y+Math.imul(_t,Dt)|0,w=w+Math.imul(nt,Tt)|0,u=u+Math.imul(nt,Ot)|0,u=u+Math.imul(at,Tt)|0,y=y+Math.imul(at,Ot)|0,w=w+Math.imul(dt,Rt)|0,u=u+Math.imul(dt,Lt)|0,u=u+Math.imul(st,Rt)|0,y=y+Math.imul(st,Lt)|0,w=w+Math.imul(tt,Nt)|0,u=u+Math.imul(tt,Et)|0,u=u+Math.imul(X,Nt)|0,y=y+Math.imul(X,Et)|0,w=w+Math.imul(V,It)|0,u=u+Math.imul(V,Bt)|0,u=u+Math.imul(et,It)|0,y=y+Math.imul(et,Bt)|0,w=w+Math.imul(G,qt)|0,u=u+Math.imul(G,Ft)|0,u=u+Math.imul(J,qt)|0,y=y+Math.imul(J,Ft)|0,w=w+Math.imul(F,Ut)|0,u=u+Math.imul(F,jt)|0,u=u+Math.imul(j,Ut)|0,y=y+Math.imul(j,jt)|0;var fi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(fi>>>26)|0,fi&=67108863,w=Math.imul(yt,St),u=Math.imul(yt,Pt),u=u+Math.imul(kt,St)|0,y=Math.imul(kt,Pt),w=w+Math.imul(Mt,At)|0,u=u+Math.imul(Mt,Ct)|0,u=u+Math.imul(bt,At)|0,y=y+Math.imul(bt,Ct)|0,w=w+Math.imul(wt,xt)|0,u=u+Math.imul(wt,Dt)|0,u=u+Math.imul(vt,xt)|0,y=y+Math.imul(vt,Dt)|0,w=w+Math.imul(pt,Tt)|0,u=u+Math.imul(pt,Ot)|0,u=u+Math.imul(_t,Tt)|0,y=y+Math.imul(_t,Ot)|0,w=w+Math.imul(nt,Rt)|0,u=u+Math.imul(nt,Lt)|0,u=u+Math.imul(at,Rt)|0,y=y+Math.imul(at,Lt)|0,w=w+Math.imul(dt,Nt)|0,u=u+Math.imul(dt,Et)|0,u=u+Math.imul(st,Nt)|0,y=y+Math.imul(st,Et)|0,w=w+Math.imul(tt,It)|0,u=u+Math.imul(tt,Bt)|0,u=u+Math.imul(X,It)|0,y=y+Math.imul(X,Bt)|0,w=w+Math.imul(V,qt)|0,u=u+Math.imul(V,Ft)|0,u=u+Math.imul(et,qt)|0,y=y+Math.imul(et,Ft)|0,w=w+Math.imul(G,Ut)|0,u=u+Math.imul(G,jt)|0,u=u+Math.imul(J,Ut)|0,y=y+Math.imul(J,jt)|0,w=w+Math.imul(F,$t)|0,u=u+Math.imul(F,Wt)|0,u=u+Math.imul(j,$t)|0,y=y+Math.imul(j,Wt)|0;var di=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(di>>>26)|0,di&=67108863,w=Math.imul(yt,At),u=Math.imul(yt,Ct),u=u+Math.imul(kt,At)|0,y=Math.imul(kt,Ct),w=w+Math.imul(Mt,xt)|0,u=u+Math.imul(Mt,Dt)|0,u=u+Math.imul(bt,xt)|0,y=y+Math.imul(bt,Dt)|0,w=w+Math.imul(wt,Tt)|0,u=u+Math.imul(wt,Ot)|0,u=u+Math.imul(vt,Tt)|0,y=y+Math.imul(vt,Ot)|0,w=w+Math.imul(pt,Rt)|0,u=u+Math.imul(pt,Lt)|0,u=u+Math.imul(_t,Rt)|0,y=y+Math.imul(_t,Lt)|0,w=w+Math.imul(nt,Nt)|0,u=u+Math.imul(nt,Et)|0,u=u+Math.imul(at,Nt)|0,y=y+Math.imul(at,Et)|0,w=w+Math.imul(dt,It)|0,u=u+Math.imul(dt,Bt)|0,u=u+Math.imul(st,It)|0,y=y+Math.imul(st,Bt)|0,w=w+Math.imul(tt,qt)|0,u=u+Math.imul(tt,Ft)|0,u=u+Math.imul(X,qt)|0,y=y+Math.imul(X,Ft)|0,w=w+Math.imul(V,Ut)|0,u=u+Math.imul(V,jt)|0,u=u+Math.imul(et,Ut)|0,y=y+Math.imul(et,jt)|0,w=w+Math.imul(G,$t)|0,u=u+Math.imul(G,Wt)|0,u=u+Math.imul(J,$t)|0,y=y+Math.imul(J,Wt)|0;var hi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(hi>>>26)|0,hi&=67108863,w=Math.imul(yt,xt),u=Math.imul(yt,Dt),u=u+Math.imul(kt,xt)|0,y=Math.imul(kt,Dt),w=w+Math.imul(Mt,Tt)|0,u=u+Math.imul(Mt,Ot)|0,u=u+Math.imul(bt,Tt)|0,y=y+Math.imul(bt,Ot)|0,w=w+Math.imul(wt,Rt)|0,u=u+Math.imul(wt,Lt)|0,u=u+Math.imul(vt,Rt)|0,y=y+Math.imul(vt,Lt)|0,w=w+Math.imul(pt,Nt)|0,u=u+Math.imul(pt,Et)|0,u=u+Math.imul(_t,Nt)|0,y=y+Math.imul(_t,Et)|0,w=w+Math.imul(nt,It)|0,u=u+Math.imul(nt,Bt)|0,u=u+Math.imul(at,It)|0,y=y+Math.imul(at,Bt)|0,w=w+Math.imul(dt,qt)|0,u=u+Math.imul(dt,Ft)|0,u=u+Math.imul(st,qt)|0,y=y+Math.imul(st,Ft)|0,w=w+Math.imul(tt,Ut)|0,u=u+Math.imul(tt,jt)|0,u=u+Math.imul(X,Ut)|0,y=y+Math.imul(X,jt)|0,w=w+Math.imul(V,$t)|0,u=u+Math.imul(V,Wt)|0,u=u+Math.imul(et,$t)|0,y=y+Math.imul(et,Wt)|0;var pi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(pi>>>26)|0,pi&=67108863,w=Math.imul(yt,Tt),u=Math.imul(yt,Ot),u=u+Math.imul(kt,Tt)|0,y=Math.imul(kt,Ot),w=w+Math.imul(Mt,Rt)|0,u=u+Math.imul(Mt,Lt)|0,u=u+Math.imul(bt,Rt)|0,y=y+Math.imul(bt,Lt)|0,w=w+Math.imul(wt,Nt)|0,u=u+Math.imul(wt,Et)|0,u=u+Math.imul(vt,Nt)|0,y=y+Math.imul(vt,Et)|0,w=w+Math.imul(pt,It)|0,u=u+Math.imul(pt,Bt)|0,u=u+Math.imul(_t,It)|0,y=y+Math.imul(_t,Bt)|0,w=w+Math.imul(nt,qt)|0,u=u+Math.imul(nt,Ft)|0,u=u+Math.imul(at,qt)|0,y=y+Math.imul(at,Ft)|0,w=w+Math.imul(dt,Ut)|0,u=u+Math.imul(dt,jt)|0,u=u+Math.imul(st,Ut)|0,y=y+Math.imul(st,jt)|0,w=w+Math.imul(tt,$t)|0,u=u+Math.imul(tt,Wt)|0,u=u+Math.imul(X,$t)|0,y=y+Math.imul(X,Wt)|0;var mi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(mi>>>26)|0,mi&=67108863,w=Math.imul(yt,Rt),u=Math.imul(yt,Lt),u=u+Math.imul(kt,Rt)|0,y=Math.imul(kt,Lt),w=w+Math.imul(Mt,Nt)|0,u=u+Math.imul(Mt,Et)|0,u=u+Math.imul(bt,Nt)|0,y=y+Math.imul(bt,Et)|0,w=w+Math.imul(wt,It)|0,u=u+Math.imul(wt,Bt)|0,u=u+Math.imul(vt,It)|0,y=y+Math.imul(vt,Bt)|0,w=w+Math.imul(pt,qt)|0,u=u+Math.imul(pt,Ft)|0,u=u+Math.imul(_t,qt)|0,y=y+Math.imul(_t,Ft)|0,w=w+Math.imul(nt,Ut)|0,u=u+Math.imul(nt,jt)|0,u=u+Math.imul(at,Ut)|0,y=y+Math.imul(at,jt)|0,w=w+Math.imul(dt,$t)|0,u=u+Math.imul(dt,Wt)|0,u=u+Math.imul(st,$t)|0,y=y+Math.imul(st,Wt)|0;var gi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(gi>>>26)|0,gi&=67108863,w=Math.imul(yt,Nt),u=Math.imul(yt,Et),u=u+Math.imul(kt,Nt)|0,y=Math.imul(kt,Et),w=w+Math.imul(Mt,It)|0,u=u+Math.imul(Mt,Bt)|0,u=u+Math.imul(bt,It)|0,y=y+Math.imul(bt,Bt)|0,w=w+Math.imul(wt,qt)|0,u=u+Math.imul(wt,Ft)|0,u=u+Math.imul(vt,qt)|0,y=y+Math.imul(vt,Ft)|0,w=w+Math.imul(pt,Ut)|0,u=u+Math.imul(pt,jt)|0,u=u+Math.imul(_t,Ut)|0,y=y+Math.imul(_t,jt)|0,w=w+Math.imul(nt,$t)|0,u=u+Math.imul(nt,Wt)|0,u=u+Math.imul(at,$t)|0,y=y+Math.imul(at,Wt)|0;var _i=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(_i>>>26)|0,_i&=67108863,w=Math.imul(yt,It),u=Math.imul(yt,Bt),u=u+Math.imul(kt,It)|0,y=Math.imul(kt,Bt),w=w+Math.imul(Mt,qt)|0,u=u+Math.imul(Mt,Ft)|0,u=u+Math.imul(bt,qt)|0,y=y+Math.imul(bt,Ft)|0,w=w+Math.imul(wt,Ut)|0,u=u+Math.imul(wt,jt)|0,u=u+Math.imul(vt,Ut)|0,y=y+Math.imul(vt,jt)|0,w=w+Math.imul(pt,$t)|0,u=u+Math.imul(pt,Wt)|0,u=u+Math.imul(_t,$t)|0,y=y+Math.imul(_t,Wt)|0;var wi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(wi>>>26)|0,wi&=67108863,w=Math.imul(yt,qt),u=Math.imul(yt,Ft),u=u+Math.imul(kt,qt)|0,y=Math.imul(kt,Ft),w=w+Math.imul(Mt,Ut)|0,u=u+Math.imul(Mt,jt)|0,u=u+Math.imul(bt,Ut)|0,y=y+Math.imul(bt,jt)|0,w=w+Math.imul(wt,$t)|0,u=u+Math.imul(wt,Wt)|0,u=u+Math.imul(vt,$t)|0,y=y+Math.imul(vt,Wt)|0;var vi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(vi>>>26)|0,vi&=67108863,w=Math.imul(yt,Ut),u=Math.imul(yt,jt),u=u+Math.imul(kt,Ut)|0,y=Math.imul(kt,jt),w=w+Math.imul(Mt,$t)|0,u=u+Math.imul(Mt,Wt)|0,u=u+Math.imul(bt,$t)|0,y=y+Math.imul(bt,Wt)|0;var Mi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(Mi>>>26)|0,Mi&=67108863,w=Math.imul(yt,$t),u=Math.imul(yt,Wt),u=u+Math.imul(kt,$t)|0,y=Math.imul(kt,Wt);var bi=(k+w|0)+((u&8191)<<13)|0;return k=(y+(u>>>13)|0)+(bi>>>26)|0,bi&=67108863,v[0]=ri,v[1]=ni,v[2]=oi,v[3]=si,v[4]=ai,v[5]=li,v[6]=ui,v[7]=ci,v[8]=fi,v[9]=di,v[10]=hi,v[11]=pi,v[12]=mi,v[13]=gi,v[14]=_i,v[15]=wi,v[16]=vi,v[17]=Mi,v[18]=bi,k!==0&&(v[19]=k,l.length++),l};Math.imul||(L=E);function R(p,e,s){s.negative=e.negative^p.negative,s.length=p.length+e.length;for(var l=0,f=0,h=0;h<s.length-1;h++){var v=f;f=0;for(var k=l&67108863,w=Math.min(h,e.length-1),u=Math.max(0,h-p.length+1);u<=w;u++){var y=h-u,$=p.words[y]|0,F=e.words[u]|0,j=$*F,K=j&67108863;v=v+(j/67108864|0)|0,K=K+k|0,k=K&67108863,v=v+(K>>>26)|0,f+=v>>>26,v&=67108863}s.words[h]=k,l=v,v=f}return l!==0?s.words[h]=l:s.length--,s._strip()}function I(p,e,s){return R(p,e,s)}t.prototype.mulTo=function(e,s){var l,f=this.length+e.length;return this.length===10&&e.length===10?l=L(this,e,s):f<63?l=E(this,e,s):f<1024?l=R(this,e,s):l=I(this,e,s),l};function U(p,e){this.x=p,this.y=e}U.prototype.makeRBT=function(e){for(var s=new Array(e),l=t.prototype._countBits(e)-1,f=0;f<e;f++)s[f]=this.revBin(f,l,e);return s},U.prototype.revBin=function(e,s,l){if(e===0||e===l-1)return e;for(var f=0,h=0;h<s;h++)f|=(e&1)<<s-h-1,e>>=1;return f},U.prototype.permute=function(e,s,l,f,h,v){for(var k=0;k<v;k++)f[k]=s[e[k]],h[k]=l[e[k]]},U.prototype.transform=function(e,s,l,f,h,v){this.permute(v,e,s,l,f,h);for(var k=1;k<h;k<<=1)for(var w=k<<1,u=Math.cos(2*Math.PI/w),y=Math.sin(2*Math.PI/w),$=0;$<h;$+=w)for(var F=u,j=y,K=0;K<k;K++){var G=l[$+K],J=f[$+K],rt=l[$+K+k],V=f[$+K+k],et=F*rt-j*V;V=F*V+j*rt,rt=et,l[$+K]=G+rt,f[$+K]=J+V,l[$+K+k]=G-rt,f[$+K+k]=J-V,K!==w&&(et=u*F-y*j,j=u*j+y*F,F=et)}},U.prototype.guessLen13b=function(e,s){var l=Math.max(s,e)|1,f=l&1,h=0;for(l=l/2|0;l;l=l>>>1)h++;return 1<<h+1+f},U.prototype.conjugate=function(e,s,l){if(!(l<=1))for(var f=0;f<l/2;f++){var h=e[f];e[f]=e[l-f-1],e[l-f-1]=h,h=s[f],s[f]=-s[l-f-1],s[l-f-1]=-h}},U.prototype.normalize13b=function(e,s){for(var l=0,f=0;f<s/2;f++){var h=Math.round(e[2*f+1]/s)*8192+Math.round(e[2*f]/s)+l;e[f]=h&67108863,h<67108864?l=0:l=h/67108864|0}return e},U.prototype.convert13b=function(e,s,l,f){for(var h=0,v=0;v<s;v++)h=h+(e[v]|0),l[2*v]=h&8191,h=h>>>13,l[2*v+1]=h&8191,h=h>>>13;for(v=2*s;v<f;++v)l[v]=0;r(h===0),r((h&-8192)===0)},U.prototype.stub=function(e){for(var s=new Array(e),l=0;l<e;l++)s[l]=0;return s},U.prototype.mulp=function(e,s,l){var f=2*this.guessLen13b(e.length,s.length),h=this.makeRBT(f),v=this.stub(f),k=new Array(f),w=new Array(f),u=new Array(f),y=new Array(f),$=new Array(f),F=new Array(f),j=l.words;j.length=f,this.convert13b(e.words,e.length,k,f),this.convert13b(s.words,s.length,y,f),this.transform(k,v,w,u,f,h),this.transform(y,v,$,F,f,h);for(var K=0;K<f;K++){var G=w[K]*$[K]-u[K]*F[K];u[K]=w[K]*F[K]+u[K]*$[K],w[K]=G}return this.conjugate(w,u,f),this.transform(w,u,j,v,f,h),this.conjugate(j,v,f),this.normalize13b(j,f),l.negative=e.negative^s.negative,l.length=e.length+s.length,l._strip()},t.prototype.mul=function(e){var s=new t(null);return s.words=new Array(this.length+e.length),this.mulTo(e,s)},t.prototype.mulf=function(e){var s=new t(null);return s.words=new Array(this.length+e.length),I(this,e,s)},t.prototype.imul=function(e){return this.clone().mulTo(e,this)},t.prototype.imuln=function(e){var s=e<0;s&&(e=-e),r(typeof e=="number"),r(e<67108864);for(var l=0,f=0;f<this.length;f++){var h=(this.words[f]|0)*e,v=(h&67108863)+(l&67108863);l>>=26,l+=h/67108864|0,l+=v>>>26,this.words[f]=v&67108863}return l!==0&&(this.words[f]=l,this.length++),this.length=e===0?1:this.length,s?this.ineg():this},t.prototype.muln=function(e){return this.clone().imuln(e)},t.prototype.sqr=function(){return this.mul(this)},t.prototype.isqr=function(){return this.imul(this.clone())},t.prototype.pow=function(e){var s=N(e);if(s.length===0)return new t(1);for(var l=this,f=0;f<s.length&&s[f]===0;f++,l=l.sqr());if(++f<s.length)for(var h=l.sqr();f<s.length;f++,h=h.sqr())s[f]!==0&&(l=l.mul(h));return l},t.prototype.iushln=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26,f=67108863>>>26-s<<26-s,h;if(s!==0){var v=0;for(h=0;h<this.length;h++){var k=this.words[h]&f,w=(this.words[h]|0)-k<<s;this.words[h]=w|v,v=k>>>26-s}v&&(this.words[h]=v,this.length++)}if(l!==0){for(h=this.length-1;h>=0;h--)this.words[h+l]=this.words[h];for(h=0;h<l;h++)this.words[h]=0;this.length+=l}return this._strip()},t.prototype.ishln=function(e){return r(this.negative===0),this.iushln(e)},t.prototype.iushrn=function(e,s,l){r(typeof e=="number"&&e>=0);var f;s?f=(s-s%26)/26:f=0;var h=e%26,v=Math.min((e-h)/26,this.length),k=67108863^67108863>>>h<<h,w=l;if(f-=v,f=Math.max(0,f),w){for(var u=0;u<v;u++)w.words[u]=this.words[u];w.length=v}if(v!==0)if(this.length>v)for(this.length-=v,u=0;u<this.length;u++)this.words[u]=this.words[u+v];else this.words[0]=0,this.length=1;var y=0;for(u=this.length-1;u>=0&&(y!==0||u>=f);u--){var $=this.words[u]|0;this.words[u]=y<<26-h|$>>>h,y=$&k}return w&&y!==0&&(w.words[w.length++]=y),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},t.prototype.ishrn=function(e,s,l){return r(this.negative===0),this.iushrn(e,s,l)},t.prototype.shln=function(e){return this.clone().ishln(e)},t.prototype.ushln=function(e){return this.clone().iushln(e)},t.prototype.shrn=function(e){return this.clone().ishrn(e)},t.prototype.ushrn=function(e){return this.clone().iushrn(e)},t.prototype.testn=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26,f=1<<s;if(this.length<=l)return!1;var h=this.words[l];return!!(h&f)},t.prototype.imaskn=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26;if(r(this.negative===0,"imaskn works only with positive numbers"),this.length<=l)return this;if(s!==0&&l++,this.length=Math.min(l,this.length),s!==0){var f=67108863^67108863>>>s<<s;this.words[this.length-1]&=f}return this._strip()},t.prototype.maskn=function(e){return this.clone().imaskn(e)},t.prototype.iaddn=function(e){return r(typeof e=="number"),r(e<67108864),e<0?this.isubn(-e):this.negative!==0?this.length===1&&(this.words[0]|0)<=e?(this.words[0]=e-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(e),this.negative=1,this):this._iaddn(e)},t.prototype._iaddn=function(e){this.words[0]+=e;for(var s=0;s<this.length&&this.words[s]>=67108864;s++)this.words[s]-=67108864,s===this.length-1?this.words[s+1]=1:this.words[s+1]++;return this.length=Math.max(this.length,s+1),this},t.prototype.isubn=function(e){if(r(typeof e=="number"),r(e<67108864),e<0)return this.iaddn(-e);if(this.negative!==0)return this.negative=0,this.iaddn(e),this.negative=1,this;if(this.words[0]-=e,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var s=0;s<this.length&&this.words[s]<0;s++)this.words[s]+=67108864,this.words[s+1]-=1;return this._strip()},t.prototype.addn=function(e){return this.clone().iaddn(e)},t.prototype.subn=function(e){return this.clone().isubn(e)},t.prototype.iabs=function(){return this.negative=0,this},t.prototype.abs=function(){return this.clone().iabs()},t.prototype._ishlnsubmul=function(e,s,l){var f=e.length+l,h;this._expand(f);var v,k=0;for(h=0;h<e.length;h++){v=(this.words[h+l]|0)+k;var w=(e.words[h]|0)*s;v-=w&67108863,k=(v>>26)-(w/67108864|0),this.words[h+l]=v&67108863}for(;h<this.length-l;h++)v=(this.words[h+l]|0)+k,k=v>>26,this.words[h+l]=v&67108863;if(k===0)return this._strip();for(r(k===-1),k=0,h=0;h<this.length;h++)v=-(this.words[h]|0)+k,k=v>>26,this.words[h]=v&67108863;return this.negative=1,this._strip()},t.prototype._wordDiv=function(e,s){var l=this.length-e.length,f=this.clone(),h=e,v=h.words[h.length-1]|0,k=this._countBits(v);l=26-k,l!==0&&(h=h.ushln(l),f.iushln(l),v=h.words[h.length-1]|0);var w=f.length-h.length,u;if(s!=="mod"){u=new t(null),u.length=w+1,u.words=new Array(u.length);for(var y=0;y<u.length;y++)u.words[y]=0}var $=f.clone()._ishlnsubmul(h,1,w);$.negative===0&&(f=$,u&&(u.words[w]=1));for(var F=w-1;F>=0;F--){var j=(f.words[h.length+F]|0)*67108864+(f.words[h.length+F-1]|0);for(j=Math.min(j/v|0,67108863),f._ishlnsubmul(h,j,F);f.negative!==0;)j--,f.negative=0,f._ishlnsubmul(h,1,F),f.isZero()||(f.negative^=1);u&&(u.words[F]=j)}return u&&u._strip(),f._strip(),s!=="div"&&l!==0&&f.iushrn(l),{div:u||null,mod:f}},t.prototype.divmod=function(e,s,l){if(r(!e.isZero()),this.isZero())return{div:new t(0),mod:new t(0)};var f,h,v;return this.negative!==0&&e.negative===0?(v=this.neg().divmod(e,s),s!=="mod"&&(f=v.div.neg()),s!=="div"&&(h=v.mod.neg(),l&&h.negative!==0&&h.iadd(e)),{div:f,mod:h}):this.negative===0&&e.negative!==0?(v=this.divmod(e.neg(),s),s!=="mod"&&(f=v.div.neg()),{div:f,mod:v.mod}):(this.negative&e.negative)!==0?(v=this.neg().divmod(e.neg(),s),s!=="div"&&(h=v.mod.neg(),l&&h.negative!==0&&h.isub(e)),{div:v.div,mod:h}):e.length>this.length||this.cmp(e)<0?{div:new t(0),mod:this}:e.length===1?s==="div"?{div:this.divn(e.words[0]),mod:null}:s==="mod"?{div:null,mod:new t(this.modrn(e.words[0]))}:{div:this.divn(e.words[0]),mod:new t(this.modrn(e.words[0]))}:this._wordDiv(e,s)},t.prototype.div=function(e){return this.divmod(e,"div",!1).div},t.prototype.mod=function(e){return this.divmod(e,"mod",!1).mod},t.prototype.umod=function(e){return this.divmod(e,"mod",!0).mod},t.prototype.divRound=function(e){var s=this.divmod(e);if(s.mod.isZero())return s.div;var l=s.div.negative!==0?s.mod.isub(e):s.mod,f=e.ushrn(1),h=e.andln(1),v=l.cmp(f);return v<0||h===1&&v===0?s.div:s.div.negative!==0?s.div.isubn(1):s.div.iaddn(1)},t.prototype.modrn=function(e){var s=e<0;s&&(e=-e),r(e<=67108863);for(var l=(1<<26)%e,f=0,h=this.length-1;h>=0;h--)f=(l*f+(this.words[h]|0))%e;return s?-f:f},t.prototype.modn=function(e){return this.modrn(e)},t.prototype.idivn=function(e){var s=e<0;s&&(e=-e),r(e<=67108863);for(var l=0,f=this.length-1;f>=0;f--){var h=(this.words[f]|0)+l*67108864;this.words[f]=h/e|0,l=h%e}return this._strip(),s?this.ineg():this},t.prototype.divn=function(e){return this.clone().idivn(e)},t.prototype.egcd=function(e){r(e.negative===0),r(!e.isZero());var s=this,l=e.clone();s.negative!==0?s=s.umod(e):s=s.clone();for(var f=new t(1),h=new t(0),v=new t(0),k=new t(1),w=0;s.isEven()&&l.isEven();)s.iushrn(1),l.iushrn(1),++w;for(var u=l.clone(),y=s.clone();!s.isZero();){for(var $=0,F=1;(s.words[0]&F)===0&&$<26;++$,F<<=1);if($>0)for(s.iushrn($);$-- >0;)(f.isOdd()||h.isOdd())&&(f.iadd(u),h.isub(y)),f.iushrn(1),h.iushrn(1);for(var j=0,K=1;(l.words[0]&K)===0&&j<26;++j,K<<=1);if(j>0)for(l.iushrn(j);j-- >0;)(v.isOdd()||k.isOdd())&&(v.iadd(u),k.isub(y)),v.iushrn(1),k.iushrn(1);s.cmp(l)>=0?(s.isub(l),f.isub(v),h.isub(k)):(l.isub(s),v.isub(f),k.isub(h))}return{a:v,b:k,gcd:l.iushln(w)}},t.prototype._invmp=function(e){r(e.negative===0),r(!e.isZero());var s=this,l=e.clone();s.negative!==0?s=s.umod(e):s=s.clone();for(var f=new t(1),h=new t(0),v=l.clone();s.cmpn(1)>0&&l.cmpn(1)>0;){for(var k=0,w=1;(s.words[0]&w)===0&&k<26;++k,w<<=1);if(k>0)for(s.iushrn(k);k-- >0;)f.isOdd()&&f.iadd(v),f.iushrn(1);for(var u=0,y=1;(l.words[0]&y)===0&&u<26;++u,y<<=1);if(u>0)for(l.iushrn(u);u-- >0;)h.isOdd()&&h.iadd(v),h.iushrn(1);s.cmp(l)>=0?(s.isub(l),f.isub(h)):(l.isub(s),h.isub(f))}var $;return s.cmpn(1)===0?$=f:$=h,$.cmpn(0)<0&&$.iadd(e),$},t.prototype.gcd=function(e){if(this.isZero())return e.abs();if(e.isZero())return this.abs();var s=this.clone(),l=e.clone();s.negative=0,l.negative=0;for(var f=0;s.isEven()&&l.isEven();f++)s.iushrn(1),l.iushrn(1);do{for(;s.isEven();)s.iushrn(1);for(;l.isEven();)l.iushrn(1);var h=s.cmp(l);if(h<0){var v=s;s=l,l=v}else if(h===0||l.cmpn(1)===0)break;s.isub(l)}while(!0);return l.iushln(f)},t.prototype.invm=function(e){return this.egcd(e).a.umod(e)},t.prototype.isEven=function(){return(this.words[0]&1)===0},t.prototype.isOdd=function(){return(this.words[0]&1)===1},t.prototype.andln=function(e){return this.words[0]&e},t.prototype.bincn=function(e){r(typeof e=="number");var s=e%26,l=(e-s)/26,f=1<<s;if(this.length<=l)return this._expand(l+1),this.words[l]|=f,this;for(var h=f,v=l;h!==0&&v<this.length;v++){var k=this.words[v]|0;k+=h,h=k>>>26,k&=67108863,this.words[v]=k}return h!==0&&(this.words[v]=h,this.length++),this},t.prototype.isZero=function(){return this.length===1&&this.words[0]===0},t.prototype.cmpn=function(e){var s=e<0;if(this.negative!==0&&!s)return-1;if(this.negative===0&&s)return 1;this._strip();var l;if(this.length>1)l=1;else{s&&(e=-e),r(e<=67108863,"Number is too big");var f=this.words[0]|0;l=f===e?0:f<e?-1:1}return this.negative!==0?-l|0:l},t.prototype.cmp=function(e){if(this.negative!==0&&e.negative===0)return-1;if(this.negative===0&&e.negative!==0)return 1;var s=this.ucmp(e);return this.negative!==0?-s|0:s},t.prototype.ucmp=function(e){if(this.length>e.length)return 1;if(this.length<e.length)return-1;for(var s=0,l=this.length-1;l>=0;l--){var f=this.words[l]|0,h=e.words[l]|0;if(f!==h){f<h?s=-1:f>h&&(s=1);break}}return s},t.prototype.gtn=function(e){return this.cmpn(e)===1},t.prototype.gt=function(e){return this.cmp(e)===1},t.prototype.gten=function(e){return this.cmpn(e)>=0},t.prototype.gte=function(e){return this.cmp(e)>=0},t.prototype.ltn=function(e){return this.cmpn(e)===-1},t.prototype.lt=function(e){return this.cmp(e)===-1},t.prototype.lten=function(e){return this.cmpn(e)<=0},t.prototype.lte=function(e){return this.cmp(e)<=0},t.prototype.eqn=function(e){return this.cmpn(e)===0},t.prototype.eq=function(e){return this.cmp(e)===0},t.red=function(e){return new C(e)},t.prototype.toRed=function(e){return r(!this.red,"Already a number in reduction context"),r(this.negative===0,"red works only with positives"),e.convertTo(this)._forceRed(e)},t.prototype.fromRed=function(){return r(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},t.prototype._forceRed=function(e){return this.red=e,this},t.prototype.forceRed=function(e){return r(!this.red,"Already a number in reduction context"),this._forceRed(e)},t.prototype.redAdd=function(e){return r(this.red,"redAdd works only with red numbers"),this.red.add(this,e)},t.prototype.redIAdd=function(e){return r(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,e)},t.prototype.redSub=function(e){return r(this.red,"redSub works only with red numbers"),this.red.sub(this,e)},t.prototype.redISub=function(e){return r(this.red,"redISub works only with red numbers"),this.red.isub(this,e)},t.prototype.redShl=function(e){return r(this.red,"redShl works only with red numbers"),this.red.shl(this,e)},t.prototype.redMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.mul(this,e)},t.prototype.redIMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.imul(this,e)},t.prototype.redSqr=function(){return r(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},t.prototype.redISqr=function(){return r(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},t.prototype.redSqrt=function(){return r(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},t.prototype.redInvm=function(){return r(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},t.prototype.redNeg=function(){return r(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},t.prototype.redPow=function(e){return r(this.red&&!e.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,e)};var W={k256:null,p224:null,p192:null,p25519:null};function q(p,e){this.name=p,this.p=new t(e,16),this.n=this.p.bitLength(),this.k=new t(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}q.prototype._tmp=function(){var e=new t(null);return e.words=new Array(Math.ceil(this.n/13)),e},q.prototype.ireduce=function(e){var s=e,l;do this.split(s,this.tmp),s=this.imulK(s),s=s.iadd(this.tmp),l=s.bitLength();while(l>this.n);var f=l<this.n?-1:s.ucmp(this.p);return f===0?(s.words[0]=0,s.length=1):f>0?s.isub(this.p):s.strip!==void 0?s.strip():s._strip(),s},q.prototype.split=function(e,s){e.iushrn(this.n,0,s)},q.prototype.imulK=function(e){return e.imul(this.k)};function Z(){q.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}o(Z,q),Z.prototype.split=function(e,s){for(var l=4194303,f=Math.min(e.length,9),h=0;h<f;h++)s.words[h]=e.words[h];if(s.length=f,e.length<=9){e.words[0]=0,e.length=1;return}var v=e.words[9];for(s.words[s.length++]=v&l,h=10;h<e.length;h++){var k=e.words[h]|0;e.words[h-10]=(k&l)<<4|v>>>22,v=k}v>>>=22,e.words[h-10]=v,v===0&&e.length>10?e.length-=10:e.length-=9},Z.prototype.imulK=function(e){e.words[e.length]=0,e.words[e.length+1]=0,e.length+=2;for(var s=0,l=0;l<e.length;l++){var f=e.words[l]|0;s+=f*977,e.words[l]=s&67108863,s=f*64+(s/67108864|0)}return e.words[e.length-1]===0&&(e.length--,e.words[e.length-1]===0&&e.length--),e};function _(){q.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}o(_,q);function b(){q.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}o(b,q);function S(){q.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}o(S,q),S.prototype.imulK=function(e){for(var s=0,l=0;l<e.length;l++){var f=(e.words[l]|0)*19+s,h=f&67108863;f>>>=26,e.words[l]=h,s=f}return s!==0&&(e.words[e.length++]=s),e},t._prime=function(e){if(W[e])return W[e];var s;if(e==="k256")s=new Z;else if(e==="p224")s=new _;else if(e==="p192")s=new b;else if(e==="p25519")s=new S;else throw new Error("Unknown prime "+e);return W[e]=s,s};function C(p){if(typeof p=="string"){var e=t._prime(p);this.m=e.p,this.prime=e}else r(p.gtn(1),"modulus must be greater than 1"),this.m=p,this.prime=null}C.prototype._verify1=function(e){r(e.negative===0,"red works only with positives"),r(e.red,"red works only with red numbers")},C.prototype._verify2=function(e,s){r((e.negative|s.negative)===0,"red works only with positives"),r(e.red&&e.red===s.red,"red works only with red numbers")},C.prototype.imod=function(e){return this.prime?this.prime.ireduce(e)._forceRed(this):(g(e,e.umod(this.m)._forceRed(this)),e)},C.prototype.neg=function(e){return e.isZero()?e.clone():this.m.sub(e)._forceRed(this)},C.prototype.add=function(e,s){this._verify2(e,s);var l=e.add(s);return l.cmp(this.m)>=0&&l.isub(this.m),l._forceRed(this)},C.prototype.iadd=function(e,s){this._verify2(e,s);var l=e.iadd(s);return l.cmp(this.m)>=0&&l.isub(this.m),l},C.prototype.sub=function(e,s){this._verify2(e,s);var l=e.sub(s);return l.cmpn(0)<0&&l.iadd(this.m),l._forceRed(this)},C.prototype.isub=function(e,s){this._verify2(e,s);var l=e.isub(s);return l.cmpn(0)<0&&l.iadd(this.m),l},C.prototype.shl=function(e,s){return this._verify1(e),this.imod(e.ushln(s))},C.prototype.imul=function(e,s){return this._verify2(e,s),this.imod(e.imul(s))},C.prototype.mul=function(e,s){return this._verify2(e,s),this.imod(e.mul(s))},C.prototype.isqr=function(e){return this.imul(e,e.clone())},C.prototype.sqr=function(e){return this.mul(e,e)},C.prototype.sqrt=function(e){if(e.isZero())return e.clone();var s=this.m.andln(3);if(r(s%2===1),s===3){var l=this.m.add(new t(1)).iushrn(2);return this.pow(e,l)}for(var f=this.m.subn(1),h=0;!f.isZero()&&f.andln(1)===0;)h++,f.iushrn(1);r(!f.isZero());var v=new t(1).toRed(this),k=v.redNeg(),w=this.m.subn(1).iushrn(1),u=this.m.bitLength();for(u=new t(2*u*u).toRed(this);this.pow(u,w).cmp(k)!==0;)u.redIAdd(k);for(var y=this.pow(u,f),$=this.pow(e,f.addn(1).iushrn(1)),F=this.pow(e,f),j=h;F.cmp(v)!==0;){for(var K=F,G=0;K.cmp(v)!==0;G++)K=K.redSqr();r(G<j);var J=this.pow(y,new t(1).iushln(j-G-1));$=$.redMul(J),y=J.redSqr(),F=F.redMul(y),j=G}return $},C.prototype.invm=function(e){var s=e._invmp(this.m);return s.negative!==0?(s.negative=0,this.imod(s).redNeg()):this.imod(s)},C.prototype.pow=function(e,s){if(s.isZero())return new t(1).toRed(this);if(s.cmpn(1)===0)return e.clone();var l=4,f=new Array(1<<l);f[0]=new t(1).toRed(this),f[1]=e;for(var h=2;h<f.length;h++)f[h]=this.mul(f[h-1],e);var v=f[0],k=0,w=0,u=s.bitLength()%26;for(u===0&&(u=26),h=s.length-1;h>=0;h--){for(var y=s.words[h],$=u-1;$>=0;$--){var F=y>>$&1;if(v!==f[0]&&(v=this.sqr(v)),F===0&&k===0){w=0;continue}k<<=1,k|=F,w++,!(w!==l&&(h!==0||$!==0))&&(v=this.mul(v,f[k]),w=0,k=0)}u=26}return v},C.prototype.convertTo=function(e){var s=e.umod(this.m);return s===e?s.clone():s},C.prototype.convertFrom=function(e){var s=e.clone();return s.red=null,s},t.mont=function(e){return new x(e)};function x(p){C.call(this,p),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new t(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)}o(x,C),x.prototype.convertTo=function(e){return this.imod(e.ushln(this.shift))},x.prototype.convertFrom=function(e){var s=this.imod(e.mul(this.rinv));return s.red=null,s},x.prototype.imul=function(e,s){if(e.isZero()||s.isZero())return e.words[0]=0,e.length=1,e;var l=e.imul(s),f=l.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),h=l.isub(f).iushrn(this.shift),v=h;return h.cmp(this.m)>=0?v=h.isub(this.m):h.cmpn(0)<0&&(v=h.iadd(this.m)),v._forceRed(this)},x.prototype.mul=function(e,s){if(e.isZero()||s.isZero())return new t(0)._forceRed(this);var l=e.mul(s),f=l.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),h=l.isub(f).iushrn(this.shift),v=h;return h.cmp(this.m)>=0?v=h.isub(this.m):h.cmpn(0)<0&&(v=h.iadd(this.m)),v._forceRed(this)},x.prototype.invm=function(e){var s=this.imod(e._invmp(this.m).mul(this.r2));return s._forceRed(this)}})(typeof Fi>"u"||Fi,Ar)});var Kn={};Ur(Kn,{CetusMarginTradingSDK:()=>Te,appendExtraRewards:()=>Fn,calculateBorrowAprPercent:()=>ti,calculateDepositAprPercent:()=>Ii,calculateUtilizationPercent:()=>Ei,default:()=>Zn,formatLtvPercentValue:()=>Wn,getPriceWithFormattedDecimals:()=>Bi,getRewardsAprMultiplier:()=>jn,getUpdatedReserve:()=>Un,margin_trading_mainnet:()=>ki,mergePositionData:()=>Ne,oraclePriceMultiplierDecimal:()=>Qe,updateRewardsWithMultiplier:()=>$n,wrapMarketInfo:()=>Re,wrapMarketPermissions:()=>Ee,wrapPosition:()=>Le});module.exports=jr(Kn);var Dr=require("@mysten/sui/client"),Tr=require("@mysten/sui/utils"),Or=require("@pythnetwork/pyth-sui-js"),Rr=require("@cetusprotocol/common-sdk"),Lr=require("@cetusprotocol/sui-clmm-sdk"),De=require("@cetusprotocol/aggregator-sdk");var er=require("@cetusprotocol/common-sdk"),ki={full_rpc_url:er.FullRpcUrlMainnet,env:"mainnet",aggregator_url:"https://api-sui.cetus.zone/router_v3",margin_trading:{package_id:"0xd36d24553943160f960d3c0065cb6b77910328d23669233d304290b84e101400",published_at:"0xd36d24553943160f960d3c0065cb6b77910328d23669233d304290b84e101400",version:0,config:{versioned_id:"0x7408869c4ef21ea5e7027e2a49933954669862b707d5bec5c2741bf1297a6cc9",admin_cap_id:"0x621d93c68cc44043acea6c95dfbe1176ffb710e84ad78fe7e3fc4057f64596a2",global_config_id:"0x0627a2c9ddc7c8e39ae2946e0d51d80554d9e3857f3a7e0efafcc60e2ee10096",markets:"0x01f934d67fbd524ff1b9570531fecf198dcda1b41996c3ac8752cf7f1cc66420",markets_table_id:"0x999a28a63aab15342799909fb352d2a8c7a92adf061dbb14339cdd7ff6e23184"}},suilend:{package_id:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf",published_at:"0xe37cc7bb50fd9b6dbd3873df66fa2c554e973697f50ef97707311dc78bd08444",config:{lending_market:[{name:"Main market",slug:"main",id:"0x84030d26d85eaa7035084a057f2f11f701b7e2e4eda87551becbc7c97505ece1",type:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf::suilend::MAIN_POOL",owner_cap_id:"0xf7a4defe0b6566b6a2674a02a0c61c9f99bd012eed21bc741a069eaa82d35927"},{name:"STEAMM LM",slug:"steamm-lm",id:"0xc1888ec1b81a414e427a44829310508352aec38252ee0daa9f8b181b6947de9f",type:"0x0a071f4976abae1a7f722199cf0bfcbe695ef9408a878e7d12a7ca87b7e582a6::lp_rewards::LP_REWARDS",owner_cap_id:"0x55a0f33b24e091830302726c8cfbff8cf8abd2ec1f83a4e6f4bf51c7ba3ad5ab",is_hidden:!0}],lending_market_id:"0x84030d26d85eaa7035084a057f2f11f701b7e2e4eda87551becbc7c97505ece1",lending_market_type:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf::suilend::MAIN_POOL",api_url:"https://d10td5ybgrf39v.cloudfront.net"}}};var Kt=require("@cetusprotocol/common-sdk"),Ae=require("@mysten/sui/transactions");var Gt=require("@cetusprotocol/common-sdk"),ir=require("@suilend/sdk/parsers/obligation"),se=require("@suilend/sdk"),Re=n=>{let i=(0,Gt.getObjectFields)(n),{open_permissions_pause:r,close_permissions_pause:o,deposit_permissions_pause:t,withdraw_permissions_pause:a,borrow_permissions_pause:d,repay_permissions_pause:c}=Ee(i.permissions.toString(2));return{market_id:i.id.id,base_token:(0,Gt.addHexPrefix)(i.base_token.fields.name),quote_token:(0,Gt.addHexPrefix)(i.quote_token.fields.name),max_long_leverage:(0,Gt.d)(i.max_long_leverage).div(10**6).toString(),max_short_leverage:(0,Gt.d)(i.max_short_leverage).div(10**6).toString(),open_fee_rate:(0,Gt.d)(i.open_fee_rate).div(10**6).toString(),close_fee_rate:(0,Gt.d)(i.close_fee_rate).div(10**6).toString(),open_permissions_pause:r,close_permissions_pause:o,deposit_permissions_pause:t,withdraw_permissions_pause:a,borrow_permissions_pause:d,repay_permissions_pause:c}},Le=(n,i)=>{let r=(0,Gt.getObjectFields)(n);return{created_at:r.created_ts,position_id:r.id.id,init_deposit_amount:r.init_deposit_amount,is_long:r.is_long,lending_market_id:r.lending_market_id,obligation_owner_cap:r.obligation_owner_cap.fields.obligation_id,market_id:r.market_id,position_cap_id:i}},Ne=(n,i,r,o,t)=>{let a=(0,ir.parseObligation)(i,r),d=(0,se.getNetAprPercent)(a,t,o);if(!a.deposits||a.deposits.length===0)throw new Error("No deposits found in obligation data");let c=a.deposits[0],m=(0,Gt.d)(c.reserve.config.closeLtvPct).div(100).toString(),g=(0,Gt.d)(c.reserve.config.openLtvPct).div(100).toString(),M=a.depositedAmountUsd.gt(0)?a.borrowedAmountUsd.div(a.depositedAmountUsd).toString():"0",P=(0,Gt.d)(1).div(m).toString(),A=(0,Gt.d)(m).sub(M).toString(),T="0",O="0",N="0",E="0";if(a.borrows&&a.borrows.length>0){let L=a.borrows[0],R=t[L?.coinType];R&&(T=(0,se.getTotalAprPercent)(se.Side.BORROW,L?.reserve.borrowAprPercent,(0,se.getFilteredRewards)(R.borrow)).toString()),N=L.borrowedAmountUsd.toString(),E=L.borrowedAmount.toString()}if(a.deposits&&a.deposits.length>0){let L=a.deposits[0],R=t[L?.coinType];R&&(O=(0,se.getTotalAprPercent)(se.Side.DEPOSIT,L?.reserve.depositAprPercent,(0,se.getFilteredRewards)(R.deposit)).toString())}return{...n,...a,origin_obligation:i,net_apr_percent:d.toString(),supplied_apr:(0,Gt.d)(O).toString(),borrowed_apr:(0,Gt.d)(T).toString(),close_ltv:m,open_ltv:g,cr:M,ltr:P,liquidation_buffer:A,deposited_amount_usd:c.depositedAmountUsd.toString(),borrowed_amount_usd:N,deposited_amount:c.depositedAmount.toString(),borrowed_amount:E}},Ee=n=>{let i=n.length,r=n.substring(i-1,i)==="0",o=n.substring(i-2,i-1)==="0",t=n.substring(i-3,i-2)==="0",a=n.substring(i-4,i-3)==="0",d=n.substring(i-5,i-4)==="0",c=n.substring(i-6,i-5)==="0";return{open_permissions_pause:r,close_permissions_pause:o,deposit_permissions_pause:t,withdraw_permissions_pause:d,borrow_permissions_pause:a,repay_permissions_pause:c}};var ut=require("@suilend/sdk");var rr=require("@cetusprotocol/common-sdk");var Ie=class extends rr.BaseError{constructor(i,r,o){super(i,r||"UnknownError",o)}static isCrossSwapErrorCode(i,r){return this.isErrorCode(i,r)}},Yt=(n,i,r)=>{let o={...r,stack:i instanceof Error?i.stack:void 0};throw i instanceof Error?new Ie(i.message,n,o):new Ie(i,n,o)};var Be=class{constructor(i){this.createMarket=async i=>{let{package_id:r}=this._sdk.sdkOptions.margin_trading,{versioned_id:o,admin_cap_id:t,markets:a,global_config_id:d}=(0,Kt.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),c=new Ae.Transaction;return c.moveCall({target:`${r}::market::create_market`,arguments:[c.object(t),c.object(d),c.object(a),c.pure.u64(i.open_fee_rate),c.pure.u64(i.close_fee_rate),c.object(o)],typeArguments:[i.base_token,i.quote_token]}),c};this.updateMarketFeeRate=async i=>{let{package_id:r}=this._sdk.sdkOptions.margin_trading,{versioned_id:o,admin_cap_id:t}=(0,Kt.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),{market_id:a,open_fee_rate:d,close_fee_rate:c}=i,m=new Ae.Transaction;return m.moveCall({target:`${r}::market::set_fee_rate`,arguments:[m.object(t),m.object(a),m.pure.u64(d),m.pure.u64(c),m.object(o)],typeArguments:[]}),m};this.claimMarketFees=async(i,r)=>{let{package_id:o}=this._sdk.sdkOptions.margin_trading,{versioned_id:t,admin_cap_id:a}=(0,Kt.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),d=r||new Ae.Transaction,{base_token:c,quote_token:m}=await this._sdk.MarketModules.getMarketInfo(i);return d.moveCall({target:`${o}::market::claim_fees`,arguments:[d.object(a),d.object(i),d.object(t)],typeArguments:[c,m]}),d};this.claimAllMarketFees=async i=>{let r=new Ae.Transaction;for(let o=0;o<i.length;o++){let t=i[o];this.claimMarketFees(t,r)}return r};this.getMarginTradingConfig=async()=>{let{package_id:i}=this._sdk.sdkOptions.margin_trading;try{let r=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::versioned::InitEvent`}),o=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::admin_cap::InitEvent`}),t=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::config::InitEvent`}),d=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::market::InitEvent`})).data[0].parsedJson.markets_id,m=(await this._sdk.FullClient.getObject({id:d,options:{showContent:!0}})).data.content.fields.list.fields.id.id;return{versioned_id:r.data[0].parsedJson.versioned_id,admin_cap_id:o.data[0].parsedJson.admin_cap_id,global_config_id:t.data[0].parsedJson.global_config_id,markets:d,markets_table_id:m}}catch(r){return Yt("FetchError",r),{versioned_id:"",admin_cap_id:"",global_config_id:"",markets_table_id:"",markets:""}}};this.getMarketList=async(i=!1)=>{let o=this._sdk.getCache("margin_trading_markets_list",i);if(o)return o;let{package_id:t}=this._sdk.sdkOptions.margin_trading,a=[];try{let d=`${t}::market::CreateMarketEvent`,m=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:d})).data.map(g=>g.parsedJson.market_id);return m.length>0?((await this._sdk.FullClient.batchGetObjects(m,{showContent:!0,showType:!0})).forEach(M=>{let P=Re(M),A=`margin_trading_market_info_${P.market_id}`;this._sdk.updateCache(A,P),a.push(P)}),a):(Yt("MarketNotFound","Market not found"),[])}catch(d){return Yt("FetchError",d),[]}};this.getMarketInfo=async(i,r=!1)=>{let o=`margin_trading_market_info_${i}`,t=this._sdk.getCache(o,r);if(t)return t;try{let a=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0}}),d=Re(a);return this._sdk.updateCache(o,d,Kt.CACHE_TIME_5MIN),d}catch(a){return Yt("FetchError",a),{}}};this.getMarketSuilendInfo=async i=>{try{let{base_token:r,quote_token:o}=await this.getMarketInfo(i),{lending_market_id:t}=(0,Kt.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{allLendingMarketData:a,lstAprPercentMap:d}=await this._sdk.SuiLendModule.getLendingMarketData(!0),{base_reserve_map_info:c,quote_reserve_map_info:m}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(r,o,a);console.log("\u{1F680}\u{1F680}\u{1F680} ~ marketModules.ts:188 ~ MarketModules ~ base_reserve_map_info:",c);let g=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:a,lstAprPercentMap:d},!0),M=g[t].rewardMap[r],P=g[t].rewardMap[o],{depositedAmountUsd:A,borrowedAmountUsd:T,depositAprPercent:O,borrowAprPercent:N,depositedAmount:E,borrowedAmount:L}=c,{depositLimit:R,borrowLimit:I}=c.config,{depositedAmountUsd:U,borrowedAmountUsd:W,depositAprPercent:q,borrowAprPercent:Z,depositedAmount:_,borrowedAmount:b}=m,{depositLimit:S,borrowLimit:C}=m.config,x=(0,ut.getTotalAprPercent)(ut.Side.DEPOSIT,O,(0,ut.getFilteredRewards)(M.deposit),(0,ut.getStakingYieldAprPercent)(ut.Side.DEPOSIT,r,d)),p=(0,ut.getTotalAprPercent)(ut.Side.BORROW,N,(0,ut.getFilteredRewards)(M.borrow)),e=(0,ut.getTotalAprPercent)(ut.Side.DEPOSIT,q,(0,ut.getFilteredRewards)(P.deposit),(0,ut.getStakingYieldAprPercent)(ut.Side.DEPOSIT,r,d)),s=(0,ut.getTotalAprPercent)(ut.Side.BORROW,Z,(0,ut.getFilteredRewards)(P.borrow)),l=(0,Kt.d)(U.toString()).sub(W.toString()).toString(),f=(0,Kt.d)(A.toString()).sub(T.toString()).toString(),h=(0,Kt.d)(R.toString()).sub((0,Kt.d)(E.toString())).toString(),v=(0,Kt.d)(I.toString()).sub((0,Kt.d)(L.toString())).toString(),k=(0,Kt.d)(S.toString()).sub((0,Kt.d)(_.toString())).toString(),w=(0,Kt.d)(C.toString()).sub((0,Kt.d)(b.toString())).toString();return{long_liquidity:l,short_liquidity:f,base_total_deposit_apr_percent:x.toString(),base_total_borrow_apr_percent:p.toString(),quote_total_deposit_apr_percent:e.toString(),quote_total_borrow_apr_percent:s.toString(),base_token_available_deposit_amount:h,base_token_available_borrow_amount:v,quote_token_available_deposit_amount:k,quote_token_available_borrow_amount:w,base_deposit_rewards:(0,ut.getFilteredRewards)(M.deposit),quote_deposit_rewards:(0,ut.getFilteredRewards)(P.deposit),base_borrow_rewards:(0,ut.getFilteredRewards)(M.borrow),quote_borrow_rewards:(0,ut.getFilteredRewards)(P.borrow),base_deposit_apr_percent:O.toString(),base_borrow_apr_percent:N.toString(),quote_deposit_apr_percent:q.toString(),quote_borrow_apr_percent:Z.toString(),obligations:g[t],base_reserve_map_info:c,quote_reserve_map_info:m}}catch(r){return Yt("FetchError",r),{}}};this._sdk=i}};var mt=require("@cetusprotocol/common-sdk"),Qt=require("@mysten/sui/transactions");var qe=class{constructor(i){this.queryGlobalPermissions=async()=>{let i=this._sdk.sdkOptions.margin_trading,{global_config_id:r}=(0,mt.getPackagerConfigs)(i),o=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}});if(o){let t=o.data.content.fields.permissions.toString(2),{open_permissions_pause:a,close_permissions_pause:d,deposit_permissions_pause:c,withdraw_permissions_pause:m,borrow_permissions_pause:g,repay_permissions_pause:M}=Ee(t);return{open_permissions_pause:a,close_permissions_pause:d,deposit_permissions_pause:c,withdraw_permissions_pause:m,borrow_permissions_pause:g,repay_permissions_pause:M}}};this.updateGlobalPermissions=async i=>{let r=new Qt.Transaction;return this.updateGlobalOpenPositionPermissions(i,r),this.updateGlobalClosePositionPermissions(i,r),this.updateGlobalDepositPermissions(i,r),this.updateGlobalBorrowPermissions(i,r),this.updateGlobalWithdrawPermissions(i,r),this.updateGlobalRepayPermissions(i,r),r};this.updateGlobalOpenPositionPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_open_position_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateGlobalClosePositionPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_close_position_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateGlobalDepositPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_deposit_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateGlobalBorrowPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_borrow_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateGlobalWithdrawPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_withdraw_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateGlobalRepayPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_repay_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateMarketCreatePositionPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_open_position_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketClosePositionPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_close_position_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketDepositPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_deposit_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketWithdrawPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_withdraw_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketBorrowPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_borrow_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketRepayPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_repay_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketPermissions=async(i,r,o)=>{let t=o||new Qt.Transaction;return this.updateMarketCreatePositionPermissions(i,r,t),this.updateMarketClosePositionPermissions(i,r,t),this.updateMarketDepositPermissions(i,r,t),this.updateMarketWithdrawPermissions(i,r,t),this.updateMarketBorrowPermissions(i,r,t),this.updateMarketRepayPermissions(i,r,t),t};this._sdk=i}};var D=require("@cetusprotocol/common-sdk");var $r=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,Pi=Math.ceil,re=Math.floor,te="[BigNumber Error] ",nr=te+"Number primitive has more than 15 significant digits: ",ae=1e14,Q=14,Ai=9007199254740991,Ci=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],ge=1e7,zt=1e9;function or(n){var i,r,o,t=R.prototype={constructor:R,toString:null,valueOf:null},a=new R(1),d=20,c=4,m=-7,g=21,M=-1e7,P=1e7,A=!1,T=1,O=0,N={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:"\xA0",suffix:""},E="0123456789abcdefghijklmnopqrstuvwxyz",L=!0;function R(_,b){var S,C,x,p,e,s,l,f,h=this;if(!(h instanceof R))return new R(_,b);if(b==null){if(_&&_._isBigNumber===!0){h.s=_.s,!_.c||_.e>P?h.c=h.e=null:_.e<M?h.c=[h.e=0]:(h.e=_.e,h.c=_.c.slice());return}if((s=typeof _=="number")&&_*0==0){if(h.s=1/_<0?(_=-_,-1):1,_===~~_){for(p=0,e=_;e>=10;e/=10,p++);p>P?h.c=h.e=null:(h.e=p,h.c=[_]);return}f=String(_)}else{if(!$r.test(f=String(_)))return o(h,f,s);h.s=f.charCodeAt(0)==45?(f=f.slice(1),-1):1}(p=f.indexOf("."))>-1&&(f=f.replace(".","")),(e=f.search(/e/i))>0?(p<0&&(p=e),p+=+f.slice(e+1),f=f.substring(0,e)):p<0&&(p=f.length)}else{if(ht(b,2,E.length,"Base"),b==10&&L)return h=new R(_),q(h,d+h.e+1,c);if(f=String(_),s=typeof _=="number"){if(_*0!=0)return o(h,f,s,b);if(h.s=1/_<0?(f=f.slice(1),-1):1,R.DEBUG&&f.replace(/^0\.0*|\./,"").length>15)throw Error(nr+_)}else h.s=f.charCodeAt(0)===45?(f=f.slice(1),-1):1;for(S=E.slice(0,b),p=e=0,l=f.length;e<l;e++)if(S.indexOf(C=f.charAt(e))<0){if(C=="."){if(e>p){p=l;continue}}else if(!x&&(f==f.toUpperCase()&&(f=f.toLowerCase())||f==f.toLowerCase()&&(f=f.toUpperCase()))){x=!0,e=-1,p=0;continue}return o(h,String(_),s,b)}s=!1,f=r(f,b,10,h.s),(p=f.indexOf("."))>-1?f=f.replace(".",""):p=f.length}for(e=0;f.charCodeAt(e)===48;e++);for(l=f.length;f.charCodeAt(--l)===48;);if(f=f.slice(e,++l)){if(l-=e,s&&R.DEBUG&&l>15&&(_>Ai||_!==re(_)))throw Error(nr+h.s*_);if((p=p-e-1)>P)h.c=h.e=null;else if(p<M)h.c=[h.e=0];else{if(h.e=p,h.c=[],e=(p+1)%Q,p<0&&(e+=Q),e<l){for(e&&h.c.push(+f.slice(0,e)),l-=Q;e<l;)h.c.push(+f.slice(e,e+=Q));e=Q-(f=f.slice(e)).length}else e-=l;for(;e--;f+="0");h.c.push(+f)}}else h.c=[h.e=0]}R.clone=or,R.ROUND_UP=0,R.ROUND_DOWN=1,R.ROUND_CEIL=2,R.ROUND_FLOOR=3,R.ROUND_HALF_UP=4,R.ROUND_HALF_DOWN=5,R.ROUND_HALF_EVEN=6,R.ROUND_HALF_CEIL=7,R.ROUND_HALF_FLOOR=8,R.EUCLID=9,R.config=R.set=function(_){var b,S;if(_!=null)if(typeof _=="object"){if(_.hasOwnProperty(b="DECIMAL_PLACES")&&(S=_[b],ht(S,0,zt,b),d=S),_.hasOwnProperty(b="ROUNDING_MODE")&&(S=_[b],ht(S,0,8,b),c=S),_.hasOwnProperty(b="EXPONENTIAL_AT")&&(S=_[b],S&&S.pop?(ht(S[0],-zt,0,b),ht(S[1],0,zt,b),m=S[0],g=S[1]):(ht(S,-zt,zt,b),m=-(g=S<0?-S:S))),_.hasOwnProperty(b="RANGE"))if(S=_[b],S&&S.pop)ht(S[0],-zt,-1,b),ht(S[1],1,zt,b),M=S[0],P=S[1];else if(ht(S,-zt,zt,b),S)M=-(P=S<0?-S:S);else throw Error(te+b+" cannot be zero: "+S);if(_.hasOwnProperty(b="CRYPTO"))if(S=_[b],S===!!S)if(S)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))A=S;else throw A=!S,Error(te+"crypto unavailable");else A=S;else throw Error(te+b+" not true or false: "+S);if(_.hasOwnProperty(b="MODULO_MODE")&&(S=_[b],ht(S,0,9,b),T=S),_.hasOwnProperty(b="POW_PRECISION")&&(S=_[b],ht(S,0,zt,b),O=S),_.hasOwnProperty(b="FORMAT"))if(S=_[b],typeof S=="object")N=S;else throw Error(te+b+" not an object: "+S);if(_.hasOwnProperty(b="ALPHABET"))if(S=_[b],typeof S=="string"&&!/^.?$|[+\-.\s]|(.).*\1/.test(S))L=S.slice(0,10)=="0123456789",E=S;else throw Error(te+b+" invalid: "+S)}else throw Error(te+"Object expected: "+_);return{DECIMAL_PLACES:d,ROUNDING_MODE:c,EXPONENTIAL_AT:[m,g],RANGE:[M,P],CRYPTO:A,MODULO_MODE:T,POW_PRECISION:O,FORMAT:N,ALPHABET:E}},R.isBigNumber=function(_){if(!_||_._isBigNumber!==!0)return!1;if(!R.DEBUG)return!0;var b,S,C=_.c,x=_.e,p=_.s;t:if({}.toString.call(C)=="[object Array]"){if((p===1||p===-1)&&x>=-zt&&x<=zt&&x===re(x)){if(C[0]===0){if(x===0&&C.length===1)return!0;break t}if(b=(x+1)%Q,b<1&&(b+=Q),String(C[0]).length==b){for(b=0;b<C.length;b++)if(S=C[b],S<0||S>=ae||S!==re(S))break t;if(S!==0)return!0}}}else if(C===null&&x===null&&(p===null||p===1||p===-1))return!0;throw Error(te+"Invalid BigNumber: "+_)},R.maximum=R.max=function(){return U(arguments,-1)},R.minimum=R.min=function(){return U(arguments,1)},R.random=(function(){var _=9007199254740992,b=Math.random()*_&2097151?function(){return re(Math.random()*_)}:function(){return(Math.random()*1073741824|0)*8388608+(Math.random()*8388608|0)};return function(S){var C,x,p,e,s,l=0,f=[],h=new R(a);if(S==null?S=d:ht(S,0,zt),e=Pi(S/Q),A)if(crypto.getRandomValues){for(C=crypto.getRandomValues(new Uint32Array(e*=2));l<e;)s=C[l]*131072+(C[l+1]>>>11),s>=9e15?(x=crypto.getRandomValues(new Uint32Array(2)),C[l]=x[0],C[l+1]=x[1]):(f.push(s%1e14),l+=2);l=e/2}else if(crypto.randomBytes){for(C=crypto.randomBytes(e*=7);l<e;)s=(C[l]&31)*281474976710656+C[l+1]*1099511627776+C[l+2]*4294967296+C[l+3]*16777216+(C[l+4]<<16)+(C[l+5]<<8)+C[l+6],s>=9e15?crypto.randomBytes(7).copy(C,l):(f.push(s%1e14),l+=7);l=e/7}else throw A=!1,Error(te+"crypto unavailable");if(!A)for(;l<e;)s=b(),s<9e15&&(f[l++]=s%1e14);for(e=f[--l],S%=Q,e&&S&&(s=Ci[Q-S],f[l]=re(e/s)*s);f[l]===0;f.pop(),l--);if(l<0)f=[p=0];else{for(p=-1;f[0]===0;f.splice(0,1),p-=Q);for(l=1,s=f[0];s>=10;s/=10,l++);l<Q&&(p-=Q-l)}return h.e=p,h.c=f,h}})(),R.sum=function(){for(var _=1,b=arguments,S=new R(b[0]);_<b.length;)S=S.plus(b[_++]);return S},r=(function(){var _="0123456789";function b(S,C,x,p){for(var e,s=[0],l,f=0,h=S.length;f<h;){for(l=s.length;l--;s[l]*=C);for(s[0]+=p.indexOf(S.charAt(f++)),e=0;e<s.length;e++)s[e]>x-1&&(s[e+1]==null&&(s[e+1]=0),s[e+1]+=s[e]/x|0,s[e]%=x)}return s.reverse()}return function(S,C,x,p,e){var s,l,f,h,v,k,w,u,y=S.indexOf("."),$=d,F=c;for(y>=0&&(h=O,O=0,S=S.replace(".",""),u=new R(C),k=u.pow(S.length-y),O=h,u.c=b(he(ie(k.c),k.e,"0"),10,x,_),u.e=u.c.length),w=b(S,C,x,e?(s=E,_):(s=_,E)),f=h=w.length;w[--h]==0;w.pop());if(!w[0])return s.charAt(0);if(y<0?--f:(k.c=w,k.e=f,k.s=p,k=i(k,u,$,F,x),w=k.c,v=k.r,f=k.e),l=f+$+1,y=w[l],h=x/2,v=v||l<0||w[l+1]!=null,v=F<4?(y!=null||v)&&(F==0||F==(k.s<0?3:2)):y>h||y==h&&(F==4||v||F==6&&w[l-1]&1||F==(k.s<0?8:7)),l<1||!w[0])S=v?he(s.charAt(1),-$,s.charAt(0)):s.charAt(0);else{if(w.length=l,v)for(--x;++w[--l]>x;)w[l]=0,l||(++f,w=[1].concat(w));for(h=w.length;!w[--h];);for(y=0,S="";y<=h;S+=s.charAt(w[y++]));S=he(S,f,s.charAt(0))}return S}})(),i=(function(){function _(C,x,p){var e,s,l,f,h=0,v=C.length,k=x%ge,w=x/ge|0;for(C=C.slice();v--;)l=C[v]%ge,f=C[v]/ge|0,e=w*l+f*k,s=k*l+e%ge*ge+h,h=(s/p|0)+(e/ge|0)+w*f,C[v]=s%p;return h&&(C=[h].concat(C)),C}function b(C,x,p,e){var s,l;if(p!=e)l=p>e?1:-1;else for(s=l=0;s<p;s++)if(C[s]!=x[s]){l=C[s]>x[s]?1:-1;break}return l}function S(C,x,p,e){for(var s=0;p--;)C[p]-=s,s=C[p]<x[p]?1:0,C[p]=s*e+C[p]-x[p];for(;!C[0]&&C.length>1;C.splice(0,1));}return function(C,x,p,e,s){var l,f,h,v,k,w,u,y,$,F,j,K,G,J,rt,V,et,ft=C.s==x.s?1:-1,tt=C.c,X=x.c;if(!tt||!tt[0]||!X||!X[0])return new R(!C.s||!x.s||(tt?X&&tt[0]==X[0]:!X)?NaN:tt&&tt[0]==0||!X?ft*0:ft/0);for(y=new R(ft),$=y.c=[],f=C.e-x.e,ft=p+f+1,s||(s=ae,f=ne(C.e/Q)-ne(x.e/Q),ft=ft/Q|0),h=0;X[h]==(tt[h]||0);h++);if(X[h]>(tt[h]||0)&&f--,ft<0)$.push(1),v=!0;else{for(J=tt.length,V=X.length,h=0,ft+=2,k=re(s/(X[0]+1)),k>1&&(X=_(X,k,s),tt=_(tt,k,s),V=X.length,J=tt.length),G=V,F=tt.slice(0,V),j=F.length;j<V;F[j++]=0);et=X.slice(),et=[0].concat(et),rt=X[0],X[1]>=s/2&&rt++;do{if(k=0,l=b(X,F,V,j),l<0){if(K=F[0],V!=j&&(K=K*s+(F[1]||0)),k=re(K/rt),k>1)for(k>=s&&(k=s-1),w=_(X,k,s),u=w.length,j=F.length;b(w,F,u,j)==1;)k--,S(w,V<u?et:X,u,s),u=w.length,l=1;else k==0&&(l=k=1),w=X.slice(),u=w.length;if(u<j&&(w=[0].concat(w)),S(F,w,j,s),j=F.length,l==-1)for(;b(X,F,V,j)<1;)k++,S(F,V<j?et:X,j,s),j=F.length}else l===0&&(k++,F=[0]);$[h++]=k,F[0]?F[j++]=tt[G]||0:(F=[tt[G]],j=1)}while((G++<J||F[0]!=null)&&ft--);v=F[0]!=null,$[0]||$.splice(0,1)}if(s==ae){for(h=1,ft=$[0];ft>=10;ft/=10,h++);q(y,p+(y.e=h+f*Q-1)+1,e,v)}else y.e=f,y.r=+v;return y}})();function I(_,b,S,C){var x,p,e,s,l;if(S==null?S=c:ht(S,0,8),!_.c)return _.toString();if(x=_.c[0],e=_.e,b==null)l=ie(_.c),l=C==1||C==2&&(e<=m||e>=g)?Ue(l,e):he(l,e,"0");else if(_=q(new R(_),b,S),p=_.e,l=ie(_.c),s=l.length,C==1||C==2&&(b<=p||p<=m)){for(;s<b;l+="0",s++);l=Ue(l,p)}else if(b-=e+(C===2&&p>e),l=he(l,p,"0"),p+1>s){if(--b>0)for(l+=".";b--;l+="0");}else if(b+=p-s,b>0)for(p+1==s&&(l+=".");b--;l+="0");return _.s<0&&x?"-"+l:l}function U(_,b){for(var S,C,x=1,p=new R(_[0]);x<_.length;x++)C=new R(_[x]),(!C.s||(S=ye(p,C))===b||S===0&&p.s===b)&&(p=C);return p}function W(_,b,S){for(var C=1,x=b.length;!b[--x];b.pop());for(x=b[0];x>=10;x/=10,C++);return(S=C+S*Q-1)>P?_.c=_.e=null:S<M?_.c=[_.e=0]:(_.e=S,_.c=b),_}o=(function(){var _=/^(-?)0([xbo])(?=\w[\w.]*$)/i,b=/^([^.]+)\.$/,S=/^\.([^.]+)$/,C=/^-?(Infinity|NaN)$/,x=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(p,e,s,l){var f,h=s?e:e.replace(x,"");if(C.test(h))p.s=isNaN(h)?null:h<0?-1:1;else{if(!s&&(h=h.replace(_,function(v,k,w){return f=(w=w.toLowerCase())=="x"?16:w=="b"?2:8,!l||l==f?k:v}),l&&(f=l,h=h.replace(b,"$1").replace(S,"0.$1")),e!=h))return new R(h,f);if(R.DEBUG)throw Error(te+"Not a"+(l?" base "+l:"")+" number: "+e);p.s=null}p.c=p.e=null}})();function q(_,b,S,C){var x,p,e,s,l,f,h,v=_.c,k=Ci;if(v){t:{for(x=1,s=v[0];s>=10;s/=10,x++);if(p=b-x,p<0)p+=Q,e=b,l=v[f=0],h=re(l/k[x-e-1]%10);else if(f=Pi((p+1)/Q),f>=v.length)if(C){for(;v.length<=f;v.push(0));l=h=0,x=1,p%=Q,e=p-Q+1}else break t;else{for(l=s=v[f],x=1;s>=10;s/=10,x++);p%=Q,e=p-Q+x,h=e<0?0:re(l/k[x-e-1]%10)}if(C=C||b<0||v[f+1]!=null||(e<0?l:l%k[x-e-1]),C=S<4?(h||C)&&(S==0||S==(_.s<0?3:2)):h>5||h==5&&(S==4||C||S==6&&(p>0?e>0?l/k[x-e]:0:v[f-1])%10&1||S==(_.s<0?8:7)),b<1||!v[0])return v.length=0,C?(b-=_.e+1,v[0]=k[(Q-b%Q)%Q],_.e=-b||0):v[0]=_.e=0,_;if(p==0?(v.length=f,s=1,f--):(v.length=f+1,s=k[Q-p],v[f]=e>0?re(l/k[x-e]%k[e])*s:0),C)for(;;)if(f==0){for(p=1,e=v[0];e>=10;e/=10,p++);for(e=v[0]+=s,s=1;e>=10;e/=10,s++);p!=s&&(_.e++,v[0]==ae&&(v[0]=1));break}else{if(v[f]+=s,v[f]!=ae)break;v[f--]=0,s=1}for(p=v.length;v[--p]===0;v.pop());}_.e>P?_.c=_.e=null:_.e<M&&(_.c=[_.e=0])}return _}function Z(_){var b,S=_.e;return S===null?_.toString():(b=ie(_.c),b=S<=m||S>=g?Ue(b,S):he(b,S,"0"),_.s<0?"-"+b:b)}return t.absoluteValue=t.abs=function(){var _=new R(this);return _.s<0&&(_.s=1),_},t.comparedTo=function(_,b){return ye(this,new R(_,b))},t.decimalPlaces=t.dp=function(_,b){var S,C,x,p=this;if(_!=null)return ht(_,0,zt),b==null?b=c:ht(b,0,8),q(new R(p),_+p.e+1,b);if(!(S=p.c))return null;if(C=((x=S.length-1)-ne(this.e/Q))*Q,x=S[x])for(;x%10==0;x/=10,C--);return C<0&&(C=0),C},t.dividedBy=t.div=function(_,b){return i(this,new R(_,b),d,c)},t.dividedToIntegerBy=t.idiv=function(_,b){return i(this,new R(_,b),0,1)},t.exponentiatedBy=t.pow=function(_,b){var S,C,x,p,e,s,l,f,h,v=this;if(_=new R(_),_.c&&!_.isInteger())throw Error(te+"Exponent not an integer: "+Z(_));if(b!=null&&(b=new R(b)),s=_.e>14,!v.c||!v.c[0]||v.c[0]==1&&!v.e&&v.c.length==1||!_.c||!_.c[0])return h=new R(Math.pow(+Z(v),s?_.s*(2-Fe(_)):+Z(_))),b?h.mod(b):h;if(l=_.s<0,b){if(b.c?!b.c[0]:!b.s)return new R(NaN);C=!l&&v.isInteger()&&b.isInteger(),C&&(v=v.mod(b))}else{if(_.e>9&&(v.e>0||v.e<-1||(v.e==0?v.c[0]>1||s&&v.c[1]>=24e7:v.c[0]<8e13||s&&v.c[0]<=9999975e7)))return p=v.s<0&&Fe(_)?-0:0,v.e>-1&&(p=1/p),new R(l?1/p:p);O&&(p=Pi(O/Q+2))}for(s?(S=new R(.5),l&&(_.s=1),f=Fe(_)):(x=Math.abs(+Z(_)),f=x%2),h=new R(a);;){if(f){if(h=h.times(v),!h.c)break;p?h.c.length>p&&(h.c.length=p):C&&(h=h.mod(b))}if(x){if(x=re(x/2),x===0)break;f=x%2}else if(_=_.times(S),q(_,_.e+1,1),_.e>14)f=Fe(_);else{if(x=+Z(_),x===0)break;f=x%2}v=v.times(v),p?v.c&&v.c.length>p&&(v.c.length=p):C&&(v=v.mod(b))}return C?h:(l&&(h=a.div(h)),b?h.mod(b):p?q(h,O,c,e):h)},t.integerValue=function(_){var b=new R(this);return _==null?_=c:ht(_,0,8),q(b,b.e+1,_)},t.isEqualTo=t.eq=function(_,b){return ye(this,new R(_,b))===0},t.isFinite=function(){return!!this.c},t.isGreaterThan=t.gt=function(_,b){return ye(this,new R(_,b))>0},t.isGreaterThanOrEqualTo=t.gte=function(_,b){return(b=ye(this,new R(_,b)))===1||b===0},t.isInteger=function(){return!!this.c&&ne(this.e/Q)>this.c.length-2},t.isLessThan=t.lt=function(_,b){return ye(this,new R(_,b))<0},t.isLessThanOrEqualTo=t.lte=function(_,b){return(b=ye(this,new R(_,b)))===-1||b===0},t.isNaN=function(){return!this.s},t.isNegative=function(){return this.s<0},t.isPositive=function(){return this.s>0},t.isZero=function(){return!!this.c&&this.c[0]==0},t.minus=function(_,b){var S,C,x,p,e=this,s=e.s;if(_=new R(_,b),b=_.s,!s||!b)return new R(NaN);if(s!=b)return _.s=-b,e.plus(_);var l=e.e/Q,f=_.e/Q,h=e.c,v=_.c;if(!l||!f){if(!h||!v)return h?(_.s=-b,_):new R(v?e:NaN);if(!h[0]||!v[0])return v[0]?(_.s=-b,_):new R(h[0]?e:c==3?-0:0)}if(l=ne(l),f=ne(f),h=h.slice(),s=l-f){for((p=s<0)?(s=-s,x=h):(f=l,x=v),x.reverse(),b=s;b--;x.push(0));x.reverse()}else for(C=(p=(s=h.length)<(b=v.length))?s:b,s=b=0;b<C;b++)if(h[b]!=v[b]){p=h[b]<v[b];break}if(p&&(x=h,h=v,v=x,_.s=-_.s),b=(C=v.length)-(S=h.length),b>0)for(;b--;h[S++]=0);for(b=ae-1;C>s;){if(h[--C]<v[C]){for(S=C;S&&!h[--S];h[S]=b);--h[S],h[C]+=ae}h[C]-=v[C]}for(;h[0]==0;h.splice(0,1),--f);return h[0]?W(_,h,f):(_.s=c==3?-1:1,_.c=[_.e=0],_)},t.modulo=t.mod=function(_,b){var S,C,x=this;return _=new R(_,b),!x.c||!_.s||_.c&&!_.c[0]?new R(NaN):!_.c||x.c&&!x.c[0]?new R(x):(T==9?(C=_.s,_.s=1,S=i(x,_,0,3),_.s=C,S.s*=C):S=i(x,_,0,T),_=x.minus(S.times(_)),!_.c[0]&&T==1&&(_.s=x.s),_)},t.multipliedBy=t.times=function(_,b){var S,C,x,p,e,s,l,f,h,v,k,w,u,y,$,F=this,j=F.c,K=(_=new R(_,b)).c;if(!j||!K||!j[0]||!K[0])return!F.s||!_.s||j&&!j[0]&&!K||K&&!K[0]&&!j?_.c=_.e=_.s=null:(_.s*=F.s,!j||!K?_.c=_.e=null:(_.c=[0],_.e=0)),_;for(C=ne(F.e/Q)+ne(_.e/Q),_.s*=F.s,l=j.length,v=K.length,l<v&&(u=j,j=K,K=u,x=l,l=v,v=x),x=l+v,u=[];x--;u.push(0));for(y=ae,$=ge,x=v;--x>=0;){for(S=0,k=K[x]%$,w=K[x]/$|0,e=l,p=x+e;p>x;)f=j[--e]%$,h=j[e]/$|0,s=w*f+h*k,f=k*f+s%$*$+u[p]+S,S=(f/y|0)+(s/$|0)+w*h,u[p--]=f%y;u[p]=S}return S?++C:u.splice(0,1),W(_,u,C)},t.negated=function(){var _=new R(this);return _.s=-_.s||null,_},t.plus=function(_,b){var S,C=this,x=C.s;if(_=new R(_,b),b=_.s,!x||!b)return new R(NaN);if(x!=b)return _.s=-b,C.minus(_);var p=C.e/Q,e=_.e/Q,s=C.c,l=_.c;if(!p||!e){if(!s||!l)return new R(x/0);if(!s[0]||!l[0])return l[0]?_:new R(s[0]?C:x*0)}if(p=ne(p),e=ne(e),s=s.slice(),x=p-e){for(x>0?(e=p,S=l):(x=-x,S=s),S.reverse();x--;S.push(0));S.reverse()}for(x=s.length,b=l.length,x-b<0&&(S=l,l=s,s=S,b=x),x=0;b;)x=(s[--b]=s[b]+l[b]+x)/ae|0,s[b]=ae===s[b]?0:s[b]%ae;return x&&(s=[x].concat(s),++e),W(_,s,e)},t.precision=t.sd=function(_,b){var S,C,x,p=this;if(_!=null&&_!==!!_)return ht(_,1,zt),b==null?b=c:ht(b,0,8),q(new R(p),_,b);if(!(S=p.c))return null;if(x=S.length-1,C=x*Q+1,x=S[x]){for(;x%10==0;x/=10,C--);for(x=S[0];x>=10;x/=10,C++);}return _&&p.e+1>C&&(C=p.e+1),C},t.shiftedBy=function(_){return ht(_,-Ai,Ai),this.times("1e"+_)},t.squareRoot=t.sqrt=function(){var _,b,S,C,x,p=this,e=p.c,s=p.s,l=p.e,f=d+4,h=new R("0.5");if(s!==1||!e||!e[0])return new R(!s||s<0&&(!e||e[0])?NaN:e?p:1/0);if(s=Math.sqrt(+Z(p)),s==0||s==1/0?(b=ie(e),(b.length+l)%2==0&&(b+="0"),s=Math.sqrt(+b),l=ne((l+1)/2)-(l<0||l%2),s==1/0?b="5e"+l:(b=s.toExponential(),b=b.slice(0,b.indexOf("e")+1)+l),S=new R(b)):S=new R(s+""),S.c[0]){for(l=S.e,s=l+f,s<3&&(s=0);;)if(x=S,S=h.times(x.plus(i(p,x,f,1))),ie(x.c).slice(0,s)===(b=ie(S.c)).slice(0,s))if(S.e<l&&--s,b=b.slice(s-3,s+1),b=="9999"||!C&&b=="4999"){if(!C&&(q(x,x.e+d+2,0),x.times(x).eq(p))){S=x;break}f+=4,s+=4,C=1}else{(!+b||!+b.slice(1)&&b.charAt(0)=="5")&&(q(S,S.e+d+2,1),_=!S.times(S).eq(p));break}}return q(S,S.e+d+1,c,_)},t.toExponential=function(_,b){return _!=null&&(ht(_,0,zt),_++),I(this,_,b,1)},t.toFixed=function(_,b){return _!=null&&(ht(_,0,zt),_=_+this.e+1),I(this,_,b)},t.toFormat=function(_,b,S){var C,x=this;if(S==null)_!=null&&b&&typeof b=="object"?(S=b,b=null):_&&typeof _=="object"?(S=_,_=b=null):S=N;else if(typeof S!="object")throw Error(te+"Argument not an object: "+S);if(C=x.toFixed(_,b),x.c){var p,e=C.split("."),s=+S.groupSize,l=+S.secondaryGroupSize,f=S.groupSeparator||"",h=e[0],v=e[1],k=x.s<0,w=k?h.slice(1):h,u=w.length;if(l&&(p=s,s=l,l=p,u-=p),s>0&&u>0){for(p=u%s||s,h=w.substr(0,p);p<u;p+=s)h+=f+w.substr(p,s);l>0&&(h+=f+w.slice(p)),k&&(h="-"+h)}C=v?h+(S.decimalSeparator||"")+((l=+S.fractionGroupSize)?v.replace(new RegExp("\\d{"+l+"}\\B","g"),"$&"+(S.fractionGroupSeparator||"")):v):h}return(S.prefix||"")+C+(S.suffix||"")},t.toFraction=function(_){var b,S,C,x,p,e,s,l,f,h,v,k,w=this,u=w.c;if(_!=null&&(s=new R(_),!s.isInteger()&&(s.c||s.s!==1)||s.lt(a)))throw Error(te+"Argument "+(s.isInteger()?"out of range: ":"not an integer: ")+Z(s));if(!u)return new R(w);for(b=new R(a),f=S=new R(a),C=l=new R(a),k=ie(u),p=b.e=k.length-w.e-1,b.c[0]=Ci[(e=p%Q)<0?Q+e:e],_=!_||s.comparedTo(b)>0?p>0?b:f:s,e=P,P=1/0,s=new R(k),l.c[0]=0;h=i(s,b,0,1),x=S.plus(h.times(C)),x.comparedTo(_)!=1;)S=C,C=x,f=l.plus(h.times(x=f)),l=x,b=s.minus(h.times(x=b)),s=x;return x=i(_.minus(S),C,0,1),l=l.plus(x.times(f)),S=S.plus(x.times(C)),l.s=f.s=w.s,p=p*2,v=i(f,C,p,c).minus(w).abs().comparedTo(i(l,S,p,c).minus(w).abs())<1?[f,C]:[l,S],P=e,v},t.toNumber=function(){return+Z(this)},t.toPrecision=function(_,b){return _!=null&&ht(_,1,zt),I(this,_,b,2)},t.toString=function(_){var b,S=this,C=S.s,x=S.e;return x===null?C?(b="Infinity",C<0&&(b="-"+b)):b="NaN":(_==null?b=x<=m||x>=g?Ue(ie(S.c),x):he(ie(S.c),x,"0"):_===10&&L?(S=q(new R(S),d+x+1,c),b=he(ie(S.c),S.e,"0")):(ht(_,2,E.length,"Base"),b=r(he(ie(S.c),x,"0"),10,_,C,!0)),C<0&&S.c[0]&&(b="-"+b)),b},t.valueOf=t.toJSON=function(){return Z(this)},t._isBigNumber=!0,t[Symbol.toStringTag]="BigNumber",t[Symbol.for("nodejs.util.inspect.custom")]=t.valueOf,n!=null&&R.set(n),R}function ne(n){var i=n|0;return n>0||n===i?i:i-1}function ie(n){for(var i,r,o=1,t=n.length,a=n[0]+"";o<t;){for(i=n[o++]+"",r=Q-i.length;r--;i="0"+i);a+=i}for(t=a.length;a.charCodeAt(--t)===48;);return a.slice(0,t+1||1)}function ye(n,i){var r,o,t=n.c,a=i.c,d=n.s,c=i.s,m=n.e,g=i.e;if(!d||!c)return null;if(r=t&&!t[0],o=a&&!a[0],r||o)return r?o?0:-c:d;if(d!=c)return d;if(r=d<0,o=m==g,!t||!a)return o?0:!t^r?1:-1;if(!o)return m>g^r?1:-1;for(c=(m=t.length)<(g=a.length)?m:g,d=0;d<c;d++)if(t[d]!=a[d])return t[d]>a[d]^r?1:-1;return m==g?0:m>g^r?1:-1}function ht(n,i,r,o){if(n<i||n>r||n!==re(n))throw Error(te+(o||"Argument")+(typeof n=="number"?n<i||n>r?" out of range: ":" not an integer: ":" not a primitive number: ")+String(n))}function Fe(n){var i=n.c.length-1;return ne(n.e/Q)==i&&n.c[i]%2!=0}function Ue(n,i){return(n.length>1?n.charAt(0)+"."+n.slice(1):n)+(i<0?"e":"e+")+i}function he(n,i,r){var o,t;if(i<0){for(t=r+".";++i;t+=r);n=t+n}else if(o=n.length,++i>o){for(t=r,i-=o;--i;t+=r);n+=t}else i<o&&(n=n.slice(0,i)+"."+n.slice(i));return n}var Wr=or(),ct=Wr;var oe=require("@mysten/sui/transactions");var Se=9e15,Me=1e9,xi="0123456789abcdef",We="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",Ze="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Di={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Se,maxE:Se,crypto:!1},ur,pe,Y=!0,ze="[DecimalError] ",ve=ze+"Invalid argument: ",cr=ze+"Precision limit exceeded",fr=ze+"crypto unavailable",dr="[object Decimal]",Xt=Math.floor,Zt=Math.pow,Zr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Kr=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,zr=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,hr=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,le=1e7,H=7,Gr=9007199254740991,Hr=We.length-1,Ti=Ze.length-1,B={toStringTag:dr};B.absoluteValue=B.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),z(n)};B.ceil=function(){return z(new this.constructor(this),this.e+1,2)};B.clampedTo=B.clamp=function(n,i){var r,o=this,t=o.constructor;if(n=new t(n),i=new t(i),!n.s||!i.s)return new t(NaN);if(n.gt(i))throw Error(ve+i);return r=o.cmp(n),r<0?n:o.cmp(i)>0?i:new t(o)};B.comparedTo=B.cmp=function(n){var i,r,o,t,a=this,d=a.d,c=(n=new a.constructor(n)).d,m=a.s,g=n.s;if(!d||!c)return!m||!g?NaN:m!==g?m:d===c?0:!d^m<0?1:-1;if(!d[0]||!c[0])return d[0]?m:c[0]?-g:0;if(m!==g)return m;if(a.e!==n.e)return a.e>n.e^m<0?1:-1;for(o=d.length,t=c.length,i=0,r=o<t?o:t;i<r;++i)if(d[i]!==c[i])return d[i]>c[i]^m<0?1:-1;return o===t?0:o>t^m<0?1:-1};B.cosine=B.cos=function(){var n,i,r=this,o=r.constructor;return r.d?r.d[0]?(n=o.precision,i=o.rounding,o.precision=n+Math.max(r.e,r.sd())+H,o.rounding=1,r=Vr(o,wr(o,r)),o.precision=n,o.rounding=i,z(pe==2||pe==3?r.neg():r,n,i,!0)):new o(1):new o(NaN)};B.cubeRoot=B.cbrt=function(){var n,i,r,o,t,a,d,c,m,g,M=this,P=M.constructor;if(!M.isFinite()||M.isZero())return new P(M);for(Y=!1,a=M.s*Zt(M.s*M,1/3),!a||Math.abs(a)==1/0?(r=Ht(M.d),n=M.e,(a=(n-r.length+1)%3)&&(r+=a==1||a==-2?"0":"00"),a=Zt(r,1/3),n=Xt((n+1)/3)-(n%3==(n<0?-1:2)),a==1/0?r="5e"+n:(r=a.toExponential(),r=r.slice(0,r.indexOf("e")+1)+n),o=new P(r),o.s=M.s):o=new P(a.toString()),d=(n=P.precision)+3;;)if(c=o,m=c.times(c).times(c),g=m.plus(M),o=ot(g.plus(M).times(c),g.plus(m),d+2,1),Ht(c.d).slice(0,d)===(r=Ht(o.d)).slice(0,d))if(r=r.slice(d-3,d+1),r=="9999"||!t&&r=="4999"){if(!t&&(z(c,n+1,0),c.times(c).times(c).eq(M))){o=c;break}d+=4,t=1}else{(!+r||!+r.slice(1)&&r.charAt(0)=="5")&&(z(o,n+1,1),i=!o.times(o).times(o).eq(M));break}return Y=!0,z(o,n,P.rounding,i)};B.decimalPlaces=B.dp=function(){var n,i=this.d,r=NaN;if(i){if(n=i.length-1,r=(n-Xt(this.e/H))*H,n=i[n],n)for(;n%10==0;n/=10)r--;r<0&&(r=0)}return r};B.dividedBy=B.div=function(n){return ot(this,new this.constructor(n))};B.dividedToIntegerBy=B.divToInt=function(n){var i=this,r=i.constructor;return z(ot(i,new r(n),0,1,1),r.precision,r.rounding)};B.equals=B.eq=function(n){return this.cmp(n)===0};B.floor=function(){return z(new this.constructor(this),this.e+1,3)};B.greaterThan=B.gt=function(n){return this.cmp(n)>0};B.greaterThanOrEqualTo=B.gte=function(n){var i=this.cmp(n);return i==1||i===0};B.hyperbolicCosine=B.cosh=function(){var n,i,r,o,t,a=this,d=a.constructor,c=new d(1);if(!a.isFinite())return new d(a.s?1/0:NaN);if(a.isZero())return c;r=d.precision,o=d.rounding,d.precision=r+Math.max(a.e,a.sd())+4,d.rounding=1,t=a.d.length,t<32?(n=Math.ceil(t/3),i=(1/He(4,n)).toString()):(n=16,i="2.3283064365386962890625e-10"),a=Pe(d,1,a.times(i),new d(1),!0);for(var m,g=n,M=new d(8);g--;)m=a.times(a),a=c.minus(m.times(M.minus(m.times(M))));return z(a,d.precision=r,d.rounding=o,!0)};B.hyperbolicSine=B.sinh=function(){var n,i,r,o,t=this,a=t.constructor;if(!t.isFinite()||t.isZero())return new a(t);if(i=a.precision,r=a.rounding,a.precision=i+Math.max(t.e,t.sd())+4,a.rounding=1,o=t.d.length,o<3)t=Pe(a,2,t,t,!0);else{n=1.4*Math.sqrt(o),n=n>16?16:n|0,t=t.times(1/He(5,n)),t=Pe(a,2,t,t,!0);for(var d,c=new a(5),m=new a(16),g=new a(20);n--;)d=t.times(t),t=t.times(c.plus(d.times(m.times(d).plus(g))))}return a.precision=i,a.rounding=r,z(t,i,r,!0)};B.hyperbolicTangent=B.tanh=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+7,o.rounding=1,ot(r.sinh(),r.cosh(),o.precision=n,o.rounding=i)):new o(r.s)};B.inverseCosine=B.acos=function(){var n=this,i=n.constructor,r=n.abs().cmp(1),o=i.precision,t=i.rounding;return r!==-1?r===0?n.isNeg()?ue(i,o,t):new i(0):new i(NaN):n.isZero()?ue(i,o+4,t).times(.5):(i.precision=o+6,i.rounding=1,n=new i(1).minus(n).div(n.plus(1)).sqrt().atan(),i.precision=o,i.rounding=t,n.times(2))};B.inverseHyperbolicCosine=B.acosh=function(){var n,i,r=this,o=r.constructor;return r.lte(1)?new o(r.eq(1)?0:NaN):r.isFinite()?(n=o.precision,i=o.rounding,o.precision=n+Math.max(Math.abs(r.e),r.sd())+4,o.rounding=1,Y=!1,r=r.times(r).minus(1).sqrt().plus(r),Y=!0,o.precision=n,o.rounding=i,r.ln()):new o(r)};B.inverseHyperbolicSine=B.asinh=function(){var n,i,r=this,o=r.constructor;return!r.isFinite()||r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+2*Math.max(Math.abs(r.e),r.sd())+6,o.rounding=1,Y=!1,r=r.times(r).plus(1).sqrt().plus(r),Y=!0,o.precision=n,o.rounding=i,r.ln())};B.inverseHyperbolicTangent=B.atanh=function(){var n,i,r,o,t=this,a=t.constructor;return t.isFinite()?t.e>=0?new a(t.abs().eq(1)?t.s/0:t.isZero()?t:NaN):(n=a.precision,i=a.rounding,o=t.sd(),Math.max(o,n)<2*-t.e-1?z(new a(t),n,i,!0):(a.precision=r=o-t.e,t=ot(t.plus(1),new a(1).minus(t),r+n,1),a.precision=n+4,a.rounding=1,t=t.ln(),a.precision=n,a.rounding=i,t.times(.5))):new a(NaN)};B.inverseSine=B.asin=function(){var n,i,r,o,t=this,a=t.constructor;return t.isZero()?new a(t):(i=t.abs().cmp(1),r=a.precision,o=a.rounding,i!==-1?i===0?(n=ue(a,r+4,o).times(.5),n.s=t.s,n):new a(NaN):(a.precision=r+6,a.rounding=1,t=t.div(new a(1).minus(t.times(t)).sqrt().plus(1)).atan(),a.precision=r,a.rounding=o,t.times(2)))};B.inverseTangent=B.atan=function(){var n,i,r,o,t,a,d,c,m,g=this,M=g.constructor,P=M.precision,A=M.rounding;if(g.isFinite()){if(g.isZero())return new M(g);if(g.abs().eq(1)&&P+4<=Ti)return d=ue(M,P+4,A).times(.25),d.s=g.s,d}else{if(!g.s)return new M(NaN);if(P+4<=Ti)return d=ue(M,P+4,A).times(.5),d.s=g.s,d}for(M.precision=c=P+10,M.rounding=1,r=Math.min(28,c/H+2|0),n=r;n;--n)g=g.div(g.times(g).plus(1).sqrt().plus(1));for(Y=!1,i=Math.ceil(c/H),o=1,m=g.times(g),d=new M(g),t=g;n!==-1;)if(t=t.times(m),a=d.minus(t.div(o+=2)),t=t.times(m),d=a.plus(t.div(o+=2)),d.d[i]!==void 0)for(n=i;d.d[n]===a.d[n]&&n--;);return r&&(d=d.times(2<<r-1)),Y=!0,z(d,M.precision=P,M.rounding=A,!0)};B.isFinite=function(){return!!this.d};B.isInteger=B.isInt=function(){return!!this.d&&Xt(this.e/H)>this.d.length-2};B.isNaN=function(){return!this.s};B.isNegative=B.isNeg=function(){return this.s<0};B.isPositive=B.isPos=function(){return this.s>0};B.isZero=function(){return!!this.d&&this.d[0]===0};B.lessThan=B.lt=function(n){return this.cmp(n)<0};B.lessThanOrEqualTo=B.lte=function(n){return this.cmp(n)<1};B.logarithm=B.log=function(n){var i,r,o,t,a,d,c,m,g=this,M=g.constructor,P=M.precision,A=M.rounding,T=5;if(n==null)n=new M(10),i=!0;else{if(n=new M(n),r=n.d,n.s<0||!r||!r[0]||n.eq(1))return new M(NaN);i=n.eq(10)}if(r=g.d,g.s<0||!r||!r[0]||g.eq(1))return new M(r&&!r[0]?-1/0:g.s!=1?NaN:r?0:1/0);if(i)if(r.length>1)a=!0;else{for(t=r[0];t%10===0;)t/=10;a=t!==1}if(Y=!1,c=P+T,d=we(g,c),o=i?Ke(M,c+10):we(n,c),m=ot(d,o,c,1),Ce(m.d,t=P,A))do if(c+=10,d=we(g,c),o=i?Ke(M,c+10):we(n,c),m=ot(d,o,c,1),!a){+Ht(m.d).slice(t+1,t+15)+1==1e14&&(m=z(m,P+1,0));break}while(Ce(m.d,t+=10,A));return Y=!0,z(m,P,A)};B.minus=B.sub=function(n){var i,r,o,t,a,d,c,m,g,M,P,A,T=this,O=T.constructor;if(n=new O(n),!T.d||!n.d)return!T.s||!n.s?n=new O(NaN):T.d?n.s=-n.s:n=new O(n.d||T.s!==n.s?T:NaN),n;if(T.s!=n.s)return n.s=-n.s,T.plus(n);if(g=T.d,A=n.d,c=O.precision,m=O.rounding,!g[0]||!A[0]){if(A[0])n.s=-n.s;else if(g[0])n=new O(T);else return new O(m===3?-0:0);return Y?z(n,c,m):n}if(r=Xt(n.e/H),M=Xt(T.e/H),g=g.slice(),a=M-r,a){for(P=a<0,P?(i=g,a=-a,d=A.length):(i=A,r=M,d=g.length),o=Math.max(Math.ceil(c/H),d)+2,a>o&&(a=o,i.length=1),i.reverse(),o=a;o--;)i.push(0);i.reverse()}else{for(o=g.length,d=A.length,P=o<d,P&&(d=o),o=0;o<d;o++)if(g[o]!=A[o]){P=g[o]<A[o];break}a=0}for(P&&(i=g,g=A,A=i,n.s=-n.s),d=g.length,o=A.length-d;o>0;--o)g[d++]=0;for(o=A.length;o>a;){if(g[--o]<A[o]){for(t=o;t&&g[--t]===0;)g[t]=le-1;--g[t],g[o]+=le}g[o]-=A[o]}for(;g[--d]===0;)g.pop();for(;g[0]===0;g.shift())--r;return g[0]?(n.d=g,n.e=Ge(g,r),Y?z(n,c,m):n):new O(m===3?-0:0)};B.modulo=B.mod=function(n){var i,r=this,o=r.constructor;return n=new o(n),!r.d||!n.s||n.d&&!n.d[0]?new o(NaN):!n.d||r.d&&!r.d[0]?z(new o(r),o.precision,o.rounding):(Y=!1,o.modulo==9?(i=ot(r,n.abs(),0,3,1),i.s*=n.s):i=ot(r,n,0,o.modulo,1),i=i.times(n),Y=!0,r.minus(i))};B.naturalExponential=B.exp=function(){return Oi(this)};B.naturalLogarithm=B.ln=function(){return we(this)};B.negated=B.neg=function(){var n=new this.constructor(this);return n.s=-n.s,z(n)};B.plus=B.add=function(n){var i,r,o,t,a,d,c,m,g,M,P=this,A=P.constructor;if(n=new A(n),!P.d||!n.d)return!P.s||!n.s?n=new A(NaN):P.d||(n=new A(n.d||P.s===n.s?P:NaN)),n;if(P.s!=n.s)return n.s=-n.s,P.minus(n);if(g=P.d,M=n.d,c=A.precision,m=A.rounding,!g[0]||!M[0])return M[0]||(n=new A(P)),Y?z(n,c,m):n;if(a=Xt(P.e/H),o=Xt(n.e/H),g=g.slice(),t=a-o,t){for(t<0?(r=g,t=-t,d=M.length):(r=M,o=a,d=g.length),a=Math.ceil(c/H),d=a>d?a+1:d+1,t>d&&(t=d,r.length=1),r.reverse();t--;)r.push(0);r.reverse()}for(d=g.length,t=M.length,d-t<0&&(t=d,r=M,M=g,g=r),i=0;t;)i=(g[--t]=g[t]+M[t]+i)/le|0,g[t]%=le;for(i&&(g.unshift(i),++o),d=g.length;g[--d]==0;)g.pop();return n.d=g,n.e=Ge(g,o),Y?z(n,c,m):n};B.precision=B.sd=function(n){var i,r=this;if(n!==void 0&&n!==!!n&&n!==1&&n!==0)throw Error(ve+n);return r.d?(i=pr(r.d),n&&r.e+1>i&&(i=r.e+1)):i=NaN,i};B.round=function(){var n=this,i=n.constructor;return z(new i(n),n.e+1,i.rounding)};B.sine=B.sin=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+Math.max(r.e,r.sd())+H,o.rounding=1,r=Jr(o,wr(o,r)),o.precision=n,o.rounding=i,z(pe>2?r.neg():r,n,i,!0)):new o(NaN)};B.squareRoot=B.sqrt=function(){var n,i,r,o,t,a,d=this,c=d.d,m=d.e,g=d.s,M=d.constructor;if(g!==1||!c||!c[0])return new M(!g||g<0&&(!c||c[0])?NaN:c?d:1/0);for(Y=!1,g=Math.sqrt(+d),g==0||g==1/0?(i=Ht(c),(i.length+m)%2==0&&(i+="0"),g=Math.sqrt(i),m=Xt((m+1)/2)-(m<0||m%2),g==1/0?i="5e"+m:(i=g.toExponential(),i=i.slice(0,i.indexOf("e")+1)+m),o=new M(i)):o=new M(g.toString()),r=(m=M.precision)+3;;)if(a=o,o=a.plus(ot(d,a,r+2,1)).times(.5),Ht(a.d).slice(0,r)===(i=Ht(o.d)).slice(0,r))if(i=i.slice(r-3,r+1),i=="9999"||!t&&i=="4999"){if(!t&&(z(a,m+1,0),a.times(a).eq(d))){o=a;break}r+=4,t=1}else{(!+i||!+i.slice(1)&&i.charAt(0)=="5")&&(z(o,m+1,1),n=!o.times(o).eq(d));break}return Y=!0,z(o,m,M.rounding,n)};B.tangent=B.tan=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+10,o.rounding=1,r=r.sin(),r.s=1,r=ot(r,new o(1).minus(r.times(r)).sqrt(),n+10,0),o.precision=n,o.rounding=i,z(pe==2||pe==4?r.neg():r,n,i,!0)):new o(NaN)};B.times=B.mul=function(n){var i,r,o,t,a,d,c,m,g,M=this,P=M.constructor,A=M.d,T=(n=new P(n)).d;if(n.s*=M.s,!A||!A[0]||!T||!T[0])return new P(!n.s||A&&!A[0]&&!T||T&&!T[0]&&!A?NaN:!A||!T?n.s/0:n.s*0);for(r=Xt(M.e/H)+Xt(n.e/H),m=A.length,g=T.length,m<g&&(a=A,A=T,T=a,d=m,m=g,g=d),a=[],d=m+g,o=d;o--;)a.push(0);for(o=g;--o>=0;){for(i=0,t=m+o;t>o;)c=a[t]+T[o]*A[t-o-1]+i,a[t--]=c%le|0,i=c/le|0;a[t]=(a[t]+i)%le|0}for(;!a[--d];)a.pop();return i?++r:a.shift(),n.d=a,n.e=Ge(a,r),Y?z(n,P.precision,P.rounding):n};B.toBinary=function(n,i){return Ri(this,2,n,i)};B.toDecimalPlaces=B.toDP=function(n,i){var r=this,o=r.constructor;return r=new o(r),n===void 0?r:(ee(n,0,Me),i===void 0?i=o.rounding:ee(i,0,8),z(r,n+r.e+1,i))};B.toExponential=function(n,i){var r,o=this,t=o.constructor;return n===void 0?r=ce(o,!0):(ee(n,0,Me),i===void 0?i=t.rounding:ee(i,0,8),o=z(new t(o),n+1,i),r=ce(o,!0,n+1)),o.isNeg()&&!o.isZero()?"-"+r:r};B.toFixed=function(n,i){var r,o,t=this,a=t.constructor;return n===void 0?r=ce(t):(ee(n,0,Me),i===void 0?i=a.rounding:ee(i,0,8),o=z(new a(t),n+t.e+1,i),r=ce(o,!1,n+o.e+1)),t.isNeg()&&!t.isZero()?"-"+r:r};B.toFraction=function(n){var i,r,o,t,a,d,c,m,g,M,P,A,T=this,O=T.d,N=T.constructor;if(!O)return new N(T);if(g=r=new N(1),o=m=new N(0),i=new N(o),a=i.e=pr(O)-T.e-1,d=a%H,i.d[0]=Zt(10,d<0?H+d:d),n==null)n=a>0?i:g;else{if(c=new N(n),!c.isInt()||c.lt(g))throw Error(ve+c);n=c.gt(i)?a>0?i:g:c}for(Y=!1,c=new N(Ht(O)),M=N.precision,N.precision=a=O.length*H*2;P=ot(c,i,0,1,1),t=r.plus(P.times(o)),t.cmp(n)!=1;)r=o,o=t,t=g,g=m.plus(P.times(t)),m=t,t=i,i=c.minus(P.times(t)),c=t;return t=ot(n.minus(r),o,0,1,1),m=m.plus(t.times(g)),r=r.plus(t.times(o)),m.s=g.s=T.s,A=ot(g,o,a,1).minus(T).abs().cmp(ot(m,r,a,1).minus(T).abs())<1?[g,o]:[m,r],N.precision=M,Y=!0,A};B.toHexadecimal=B.toHex=function(n,i){return Ri(this,16,n,i)};B.toNearest=function(n,i){var r=this,o=r.constructor;if(r=new o(r),n==null){if(!r.d)return r;n=new o(1),i=o.rounding}else{if(n=new o(n),i===void 0?i=o.rounding:ee(i,0,8),!r.d)return n.s?r:n;if(!n.d)return n.s&&(n.s=r.s),n}return n.d[0]?(Y=!1,r=ot(r,n,0,i,1).times(n),Y=!0,z(r)):(n.s=r.s,r=n),r};B.toNumber=function(){return+this};B.toOctal=function(n,i){return Ri(this,8,n,i)};B.toPower=B.pow=function(n){var i,r,o,t,a,d,c=this,m=c.constructor,g=+(n=new m(n));if(!c.d||!n.d||!c.d[0]||!n.d[0])return new m(Zt(+c,g));if(c=new m(c),c.eq(1))return c;if(o=m.precision,a=m.rounding,n.eq(1))return z(c,o,a);if(i=Xt(n.e/H),i>=n.d.length-1&&(r=g<0?-g:g)<=Gr)return t=mr(m,c,r,o),n.s<0?new m(1).div(t):z(t,o,a);if(d=c.s,d<0){if(i<n.d.length-1)return new m(NaN);if((n.d[i]&1)==0&&(d=1),c.e==0&&c.d[0]==1&&c.d.length==1)return c.s=d,c}return r=Zt(+c,g),i=r==0||!isFinite(r)?Xt(g*(Math.log("0."+Ht(c.d))/Math.LN10+c.e+1)):new m(r+"").e,i>m.maxE+1||i<m.minE-1?new m(i>0?d/0:0):(Y=!1,m.rounding=c.s=1,r=Math.min(12,(i+"").length),t=Oi(n.times(we(c,o+r)),o),t.d&&(t=z(t,o+5,1),Ce(t.d,o,a)&&(i=o+10,t=z(Oi(n.times(we(c,i+r)),i),i+5,1),+Ht(t.d).slice(o+1,o+15)+1==1e14&&(t=z(t,o+1,0)))),t.s=d,Y=!0,m.rounding=a,z(t,o,a))};B.toPrecision=function(n,i){var r,o=this,t=o.constructor;return n===void 0?r=ce(o,o.e<=t.toExpNeg||o.e>=t.toExpPos):(ee(n,1,Me),i===void 0?i=t.rounding:ee(i,0,8),o=z(new t(o),n,i),r=ce(o,n<=o.e||o.e<=t.toExpNeg,n)),o.isNeg()&&!o.isZero()?"-"+r:r};B.toSignificantDigits=B.toSD=function(n,i){var r=this,o=r.constructor;return n===void 0?(n=o.precision,i=o.rounding):(ee(n,1,Me),i===void 0?i=o.rounding:ee(i,0,8)),z(new o(r),n,i)};B.toString=function(){var n=this,i=n.constructor,r=ce(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()&&!n.isZero()?"-"+r:r};B.truncated=B.trunc=function(){return z(new this.constructor(this),this.e+1,1)};B.valueOf=B.toJSON=function(){var n=this,i=n.constructor,r=ce(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()?"-"+r:r};function Ht(n){var i,r,o,t=n.length-1,a="",d=n[0];if(t>0){for(a+=d,i=1;i<t;i++)o=n[i]+"",r=H-o.length,r&&(a+=_e(r)),a+=o;d=n[i],o=d+"",r=H-o.length,r&&(a+=_e(r))}else if(d===0)return"0";for(;d%10===0;)d/=10;return a+d}function ee(n,i,r){if(n!==~~n||n<i||n>r)throw Error(ve+n)}function Ce(n,i,r,o){var t,a,d,c;for(a=n[0];a>=10;a/=10)--i;return--i<0?(i+=H,t=0):(t=Math.ceil((i+1)/H),i%=H),a=Zt(10,H-i),c=n[t]%a|0,o==null?i<3?(i==0?c=c/100|0:i==1&&(c=c/10|0),d=r<4&&c==99999||r>3&&c==49999||c==5e4||c==0):d=(r<4&&c+1==a||r>3&&c+1==a/2)&&(n[t+1]/a/100|0)==Zt(10,i-2)-1||(c==a/2||c==0)&&(n[t+1]/a/100|0)==0:i<4?(i==0?c=c/1e3|0:i==1?c=c/100|0:i==2&&(c=c/10|0),d=(o||r<4)&&c==9999||!o&&r>3&&c==4999):d=((o||r<4)&&c+1==a||!o&&r>3&&c+1==a/2)&&(n[t+1]/a/1e3|0)==Zt(10,i-3)-1,d}function je(n,i,r){for(var o,t=[0],a,d=0,c=n.length;d<c;){for(a=t.length;a--;)t[a]*=i;for(t[0]+=xi.indexOf(n.charAt(d++)),o=0;o<t.length;o++)t[o]>r-1&&(t[o+1]===void 0&&(t[o+1]=0),t[o+1]+=t[o]/r|0,t[o]%=r)}return t.reverse()}function Vr(n,i){var r,o,t;if(i.isZero())return i;o=i.d.length,o<32?(r=Math.ceil(o/3),t=(1/He(4,r)).toString()):(r=16,t="2.3283064365386962890625e-10"),n.precision+=r,i=Pe(n,1,i.times(t),new n(1));for(var a=r;a--;){var d=i.times(i);i=d.times(d).minus(d).times(8).plus(1)}return n.precision-=r,i}var ot=(function(){function n(o,t,a){var d,c=0,m=o.length;for(o=o.slice();m--;)d=o[m]*t+c,o[m]=d%a|0,c=d/a|0;return c&&o.unshift(c),o}function i(o,t,a,d){var c,m;if(a!=d)m=a>d?1:-1;else for(c=m=0;c<a;c++)if(o[c]!=t[c]){m=o[c]>t[c]?1:-1;break}return m}function r(o,t,a,d){for(var c=0;a--;)o[a]-=c,c=o[a]<t[a]?1:0,o[a]=c*d+o[a]-t[a];for(;!o[0]&&o.length>1;)o.shift()}return function(o,t,a,d,c,m){var g,M,P,A,T,O,N,E,L,R,I,U,W,q,Z,_,b,S,C,x,p=o.constructor,e=o.s==t.s?1:-1,s=o.d,l=t.d;if(!s||!s[0]||!l||!l[0])return new p(!o.s||!t.s||(s?l&&s[0]==l[0]:!l)?NaN:s&&s[0]==0||!l?e*0:e/0);for(m?(T=1,M=o.e-t.e):(m=le,T=H,M=Xt(o.e/T)-Xt(t.e/T)),C=l.length,b=s.length,L=new p(e),R=L.d=[],P=0;l[P]==(s[P]||0);P++);if(l[P]>(s[P]||0)&&M--,a==null?(q=a=p.precision,d=p.rounding):c?q=a+(o.e-t.e)+1:q=a,q<0)R.push(1),O=!0;else{if(q=q/T+2|0,P=0,C==1){for(A=0,l=l[0],q++;(P<b||A)&&q--;P++)Z=A*m+(s[P]||0),R[P]=Z/l|0,A=Z%l|0;O=A||P<b}else{for(A=m/(l[0]+1)|0,A>1&&(l=n(l,A,m),s=n(s,A,m),C=l.length,b=s.length),_=C,I=s.slice(0,C),U=I.length;U<C;)I[U++]=0;x=l.slice(),x.unshift(0),S=l[0],l[1]>=m/2&&++S;do A=0,g=i(l,I,C,U),g<0?(W=I[0],C!=U&&(W=W*m+(I[1]||0)),A=W/S|0,A>1?(A>=m&&(A=m-1),N=n(l,A,m),E=N.length,U=I.length,g=i(N,I,E,U),g==1&&(A--,r(N,C<E?x:l,E,m))):(A==0&&(g=A=1),N=l.slice()),E=N.length,E<U&&N.unshift(0),r(I,N,U,m),g==-1&&(U=I.length,g=i(l,I,C,U),g<1&&(A++,r(I,C<U?x:l,U,m))),U=I.length):g===0&&(A++,I=[0]),R[P++]=A,g&&I[0]?I[U++]=s[_]||0:(I=[s[_]],U=1);while((_++<b||I[0]!==void 0)&&q--);O=I[0]!==void 0}R[0]||R.shift()}if(T==1)L.e=M,ur=O;else{for(P=1,A=R[0];A>=10;A/=10)P++;L.e=P+M*T-1,z(L,c?a+L.e+1:a,d,O)}return L}})();function z(n,i,r,o){var t,a,d,c,m,g,M,P,A,T=n.constructor;t:if(i!=null){if(P=n.d,!P)return n;for(t=1,c=P[0];c>=10;c/=10)t++;if(a=i-t,a<0)a+=H,d=i,M=P[A=0],m=M/Zt(10,t-d-1)%10|0;else if(A=Math.ceil((a+1)/H),c=P.length,A>=c)if(o){for(;c++<=A;)P.push(0);M=m=0,t=1,a%=H,d=a-H+1}else break t;else{for(M=c=P[A],t=1;c>=10;c/=10)t++;a%=H,d=a-H+t,m=d<0?0:M/Zt(10,t-d-1)%10|0}if(o=o||i<0||P[A+1]!==void 0||(d<0?M:M%Zt(10,t-d-1)),g=r<4?(m||o)&&(r==0||r==(n.s<0?3:2)):m>5||m==5&&(r==4||o||r==6&&(a>0?d>0?M/Zt(10,t-d):0:P[A-1])%10&1||r==(n.s<0?8:7)),i<1||!P[0])return P.length=0,g?(i-=n.e+1,P[0]=Zt(10,(H-i%H)%H),n.e=-i||0):P[0]=n.e=0,n;if(a==0?(P.length=A,c=1,A--):(P.length=A+1,c=Zt(10,H-a),P[A]=d>0?(M/Zt(10,t-d)%Zt(10,d)|0)*c:0),g)for(;;)if(A==0){for(a=1,d=P[0];d>=10;d/=10)a++;for(d=P[0]+=c,c=1;d>=10;d/=10)c++;a!=c&&(n.e++,P[0]==le&&(P[0]=1));break}else{if(P[A]+=c,P[A]!=le)break;P[A--]=0,c=1}for(a=P.length;P[--a]===0;)P.pop()}return Y&&(n.e>T.maxE?(n.d=null,n.e=NaN):n.e<T.minE&&(n.e=0,n.d=[0])),n}function ce(n,i,r){if(!n.isFinite())return _r(n);var o,t=n.e,a=Ht(n.d),d=a.length;return i?(r&&(o=r-d)>0?a=a.charAt(0)+"."+a.slice(1)+_e(o):d>1&&(a=a.charAt(0)+"."+a.slice(1)),a=a+(n.e<0?"e":"e+")+n.e):t<0?(a="0."+_e(-t-1)+a,r&&(o=r-d)>0&&(a+=_e(o))):t>=d?(a+=_e(t+1-d),r&&(o=r-t-1)>0&&(a=a+"."+_e(o))):((o=t+1)<d&&(a=a.slice(0,o)+"."+a.slice(o)),r&&(o=r-d)>0&&(t+1===d&&(a+="."),a+=_e(o))),a}function Ge(n,i){var r=n[0];for(i*=H;r>=10;r/=10)i++;return i}function Ke(n,i,r){if(i>Hr)throw Y=!0,r&&(n.precision=r),Error(cr);return z(new n(We),i,1,!0)}function ue(n,i,r){if(i>Ti)throw Error(cr);return z(new n(Ze),i,r,!0)}function pr(n){var i=n.length-1,r=i*H+1;if(i=n[i],i){for(;i%10==0;i/=10)r--;for(i=n[0];i>=10;i/=10)r++}return r}function _e(n){for(var i="";n--;)i+="0";return i}function mr(n,i,r,o){var t,a=new n(1),d=Math.ceil(o/H+4);for(Y=!1;;){if(r%2&&(a=a.times(i),ar(a.d,d)&&(t=!0)),r=Xt(r/2),r===0){r=a.d.length-1,t&&a.d[r]===0&&++a.d[r];break}i=i.times(i),ar(i.d,d)}return Y=!0,a}function sr(n){return n.d[n.d.length-1]&1}function gr(n,i,r){for(var o,t,a=new n(i[0]),d=0;++d<i.length;){if(t=new n(i[d]),!t.s){a=t;break}o=a.cmp(t),(o===r||o===0&&a.s===r)&&(a=t)}return a}function Oi(n,i){var r,o,t,a,d,c,m,g=0,M=0,P=0,A=n.constructor,T=A.rounding,O=A.precision;if(!n.d||!n.d[0]||n.e>17)return new A(n.d?n.d[0]?n.s<0?0:1/0:1:n.s?n.s<0?0:n:NaN);for(i==null?(Y=!1,m=O):m=i,c=new A(.03125);n.e>-2;)n=n.times(c),P+=5;for(o=Math.log(Zt(2,P))/Math.LN10*2+5|0,m+=o,r=a=d=new A(1),A.precision=m;;){if(a=z(a.times(n),m,1),r=r.times(++M),c=d.plus(ot(a,r,m,1)),Ht(c.d).slice(0,m)===Ht(d.d).slice(0,m)){for(t=P;t--;)d=z(d.times(d),m,1);if(i==null)if(g<3&&Ce(d.d,m-o,T,g))A.precision=m+=10,r=a=c=new A(1),M=0,g++;else return z(d,A.precision=O,T,Y=!0);else return A.precision=O,d}d=c}}function we(n,i){var r,o,t,a,d,c,m,g,M,P,A,T=1,O=10,N=n,E=N.d,L=N.constructor,R=L.rounding,I=L.precision;if(N.s<0||!E||!E[0]||!N.e&&E[0]==1&&E.length==1)return new L(E&&!E[0]?-1/0:N.s!=1?NaN:E?0:N);if(i==null?(Y=!1,M=I):M=i,L.precision=M+=O,r=Ht(E),o=r.charAt(0),Math.abs(a=N.e)<15e14){for(;o<7&&o!=1||o==1&&r.charAt(1)>3;)N=N.times(n),r=Ht(N.d),o=r.charAt(0),T++;a=N.e,o>1?(N=new L("0."+r),a++):N=new L(o+"."+r.slice(1))}else return g=Ke(L,M+2,I).times(a+""),N=we(new L(o+"."+r.slice(1)),M-O).plus(g),L.precision=I,i==null?z(N,I,R,Y=!0):N;for(P=N,m=d=N=ot(N.minus(1),N.plus(1),M,1),A=z(N.times(N),M,1),t=3;;){if(d=z(d.times(A),M,1),g=m.plus(ot(d,new L(t),M,1)),Ht(g.d).slice(0,M)===Ht(m.d).slice(0,M))if(m=m.times(2),a!==0&&(m=m.plus(Ke(L,M+2,I).times(a+""))),m=ot(m,new L(T),M,1),i==null)if(Ce(m.d,M-O,R,c))L.precision=M+=O,g=d=N=ot(P.minus(1),P.plus(1),M,1),A=z(N.times(N),M,1),t=c=1;else return z(m,L.precision=I,R,Y=!0);else return L.precision=I,m;m=g,t+=2}}function _r(n){return String(n.s*n.s/0)}function $e(n,i){var r,o,t;for((r=i.indexOf("."))>-1&&(i=i.replace(".","")),(o=i.search(/e/i))>0?(r<0&&(r=o),r+=+i.slice(o+1),i=i.substring(0,o)):r<0&&(r=i.length),o=0;i.charCodeAt(o)===48;o++);for(t=i.length;i.charCodeAt(t-1)===48;--t);if(i=i.slice(o,t),i){if(t-=o,n.e=r=r-o-1,n.d=[],o=(r+1)%H,r<0&&(o+=H),o<t){for(o&&n.d.push(+i.slice(0,o)),t-=H;o<t;)n.d.push(+i.slice(o,o+=H));i=i.slice(o),o=H-i.length}else o-=t;for(;o--;)i+="0";n.d.push(+i),Y&&(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 Xr(n,i){var r,o,t,a,d,c,m,g,M;if(i.indexOf("_")>-1){if(i=i.replace(/(\d)_(?=\d)/g,"$1"),hr.test(i))return $e(n,i)}else if(i==="Infinity"||i==="NaN")return+i||(n.s=NaN),n.e=NaN,n.d=null,n;if(Kr.test(i))r=16,i=i.toLowerCase();else if(Zr.test(i))r=2;else if(zr.test(i))r=8;else throw Error(ve+i);for(a=i.search(/p/i),a>0?(m=+i.slice(a+1),i=i.substring(2,a)):i=i.slice(2),a=i.indexOf("."),d=a>=0,o=n.constructor,d&&(i=i.replace(".",""),c=i.length,a=c-a,t=mr(o,new o(r),a,a*2)),g=je(i,r,le),M=g.length-1,a=M;g[a]===0;--a)g.pop();return a<0?new o(n.s*0):(n.e=Ge(g,M),n.d=g,Y=!1,d&&(n=ot(n,t,c*4)),m&&(n=n.times(Math.abs(m)<54?Zt(2,m):xe.pow(2,m))),Y=!0,n)}function Jr(n,i){var r,o=i.d.length;if(o<3)return i.isZero()?i:Pe(n,2,i,i);r=1.4*Math.sqrt(o),r=r>16?16:r|0,i=i.times(1/He(5,r)),i=Pe(n,2,i,i);for(var t,a=new n(5),d=new n(16),c=new n(20);r--;)t=i.times(i),i=i.times(a.plus(t.times(d.times(t).minus(c))));return i}function Pe(n,i,r,o,t){var a,d,c,m,g=1,M=n.precision,P=Math.ceil(M/H);for(Y=!1,m=r.times(r),c=new n(o);;){if(d=ot(c.times(m),new n(i++*i++),M,1),c=t?o.plus(d):o.minus(d),o=ot(d.times(m),new n(i++*i++),M,1),d=c.plus(o),d.d[P]!==void 0){for(a=P;d.d[a]===c.d[a]&&a--;);if(a==-1)break}a=c,c=o,o=d,d=a,g++}return Y=!0,d.d.length=P+1,d}function He(n,i){for(var r=n;--i;)r*=n;return r}function wr(n,i){var r,o=i.s<0,t=ue(n,n.precision,1),a=t.times(.5);if(i=i.abs(),i.lte(a))return pe=o?4:1,i;if(r=i.divToInt(t),r.isZero())pe=o?3:2;else{if(i=i.minus(r.times(t)),i.lte(a))return pe=sr(r)?o?2:3:o?4:1,i;pe=sr(r)?o?1:4:o?3:2}return i.minus(t).abs()}function Ri(n,i,r,o){var t,a,d,c,m,g,M,P,A,T=n.constructor,O=r!==void 0;if(O?(ee(r,1,Me),o===void 0?o=T.rounding:ee(o,0,8)):(r=T.precision,o=T.rounding),!n.isFinite())M=_r(n);else{for(M=ce(n),d=M.indexOf("."),O?(t=2,i==16?r=r*4-3:i==8&&(r=r*3-2)):t=i,d>=0&&(M=M.replace(".",""),A=new T(1),A.e=M.length-d,A.d=je(ce(A),10,t),A.e=A.d.length),P=je(M,10,t),a=m=P.length;P[--m]==0;)P.pop();if(!P[0])M=O?"0p+0":"0";else{if(d<0?a--:(n=new T(n),n.d=P,n.e=a,n=ot(n,A,r,o,0,t),P=n.d,a=n.e,g=ur),d=P[r],c=t/2,g=g||P[r+1]!==void 0,g=o<4?(d!==void 0||g)&&(o===0||o===(n.s<0?3:2)):d>c||d===c&&(o===4||g||o===6&&P[r-1]&1||o===(n.s<0?8:7)),P.length=r,g)for(;++P[--r]>t-1;)P[r]=0,r||(++a,P.unshift(1));for(m=P.length;!P[m-1];--m);for(d=0,M="";d<m;d++)M+=xi.charAt(P[d]);if(O){if(m>1)if(i==16||i==8){for(d=i==16?4:3,--m;m%d;m++)M+="0";for(P=je(M,t,i),m=P.length;!P[m-1];--m);for(d=1,M="1.";d<m;d++)M+=xi.charAt(P[d])}else M=M.charAt(0)+"."+M.slice(1);M=M+(a<0?"p":"p+")+a}else if(a<0){for(;++a;)M="0"+M;M="0."+M}else if(++a>m)for(a-=m;a--;)M+="0";else a<m&&(M=M.slice(0,a)+"."+M.slice(a))}M=(i==16?"0x":i==2?"0b":i==8?"0o":"")+M}return n.s<0?"-"+M:M}function ar(n,i){if(n.length>i)return n.length=i,!0}function Yr(n){return new this(n).abs()}function Qr(n){return new this(n).acos()}function tn(n){return new this(n).acosh()}function en(n,i){return new this(n).plus(i)}function rn(n){return new this(n).asin()}function nn(n){return new this(n).asinh()}function on(n){return new this(n).atan()}function sn(n){return new this(n).atanh()}function an(n,i){n=new this(n),i=new this(i);var r,o=this.precision,t=this.rounding,a=o+4;return!n.s||!i.s?r=new this(NaN):!n.d&&!i.d?(r=ue(this,a,1).times(i.s>0?.25:.75),r.s=n.s):!i.d||n.isZero()?(r=i.s<0?ue(this,o,t):new this(0),r.s=n.s):!n.d||i.isZero()?(r=ue(this,a,1).times(.5),r.s=n.s):i.s<0?(this.precision=a,this.rounding=1,r=this.atan(ot(n,i,a,1)),i=ue(this,a,1),this.precision=o,this.rounding=t,r=n.s<0?r.minus(i):r.plus(i)):r=this.atan(ot(n,i,a,1)),r}function ln(n){return new this(n).cbrt()}function un(n){return z(n=new this(n),n.e+1,2)}function cn(n,i,r){return new this(n).clamp(i,r)}function fn(n){if(!n||typeof n!="object")throw Error(ze+"Object expected");var i,r,o,t=n.defaults===!0,a=["precision",1,Me,"rounding",0,8,"toExpNeg",-Se,0,"toExpPos",0,Se,"maxE",0,Se,"minE",-Se,0,"modulo",0,9];for(i=0;i<a.length;i+=3)if(r=a[i],t&&(this[r]=Di[r]),(o=n[r])!==void 0)if(Xt(o)===o&&o>=a[i+1]&&o<=a[i+2])this[r]=o;else throw Error(ve+r+": "+o);if(r="crypto",t&&(this[r]=Di[r]),(o=n[r])!==void 0)if(o===!0||o===!1||o===0||o===1)if(o)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[r]=!0;else throw Error(fr);else this[r]=!1;else throw Error(ve+r+": "+o);return this}function dn(n){return new this(n).cos()}function hn(n){return new this(n).cosh()}function vr(n){var i,r,o;function t(a){var d,c,m,g=this;if(!(g instanceof t))return new t(a);if(g.constructor=t,lr(a)){g.s=a.s,Y?!a.d||a.e>t.maxE?(g.e=NaN,g.d=null):a.e<t.minE?(g.e=0,g.d=[0]):(g.e=a.e,g.d=a.d.slice()):(g.e=a.e,g.d=a.d?a.d.slice():a.d);return}if(m=typeof a,m==="number"){if(a===0){g.s=1/a<0?-1:1,g.e=0,g.d=[0];return}if(a<0?(a=-a,g.s=-1):g.s=1,a===~~a&&a<1e7){for(d=0,c=a;c>=10;c/=10)d++;Y?d>t.maxE?(g.e=NaN,g.d=null):d<t.minE?(g.e=0,g.d=[0]):(g.e=d,g.d=[a]):(g.e=d,g.d=[a]);return}if(a*0!==0){a||(g.s=NaN),g.e=NaN,g.d=null;return}return $e(g,a.toString())}if(m==="string")return(c=a.charCodeAt(0))===45?(a=a.slice(1),g.s=-1):(c===43&&(a=a.slice(1)),g.s=1),hr.test(a)?$e(g,a):Xr(g,a);if(m==="bigint")return a<0?(a=-a,g.s=-1):g.s=1,$e(g,a.toString());throw Error(ve+a)}if(t.prototype=B,t.ROUND_UP=0,t.ROUND_DOWN=1,t.ROUND_CEIL=2,t.ROUND_FLOOR=3,t.ROUND_HALF_UP=4,t.ROUND_HALF_DOWN=5,t.ROUND_HALF_EVEN=6,t.ROUND_HALF_CEIL=7,t.ROUND_HALF_FLOOR=8,t.EUCLID=9,t.config=t.set=fn,t.clone=vr,t.isDecimal=lr,t.abs=Yr,t.acos=Qr,t.acosh=tn,t.add=en,t.asin=rn,t.asinh=nn,t.atan=on,t.atanh=sn,t.atan2=an,t.cbrt=ln,t.ceil=un,t.clamp=cn,t.cos=dn,t.cosh=hn,t.div=pn,t.exp=mn,t.floor=gn,t.hypot=_n,t.ln=wn,t.log=vn,t.log10=bn,t.log2=Mn,t.max=yn,t.min=kn,t.mod=Sn,t.mul=Pn,t.pow=An,t.random=Cn,t.round=xn,t.sign=Dn,t.sin=Tn,t.sinh=On,t.sqrt=Rn,t.sub=Ln,t.sum=Nn,t.tan=En,t.tanh=In,t.trunc=Bn,n===void 0&&(n={}),n&&n.defaults!==!0)for(o=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],i=0;i<o.length;)n.hasOwnProperty(r=o[i++])||(n[r]=this[r]);return t.config(n),t}function pn(n,i){return new this(n).div(i)}function mn(n){return new this(n).exp()}function gn(n){return z(n=new this(n),n.e+1,3)}function _n(){var n,i,r=new this(0);for(Y=!1,n=0;n<arguments.length;)if(i=new this(arguments[n++]),i.d)r.d&&(r=r.plus(i.times(i)));else{if(i.s)return Y=!0,new this(1/0);r=i}return Y=!0,r.sqrt()}function lr(n){return n instanceof xe||n&&n.toStringTag===dr||!1}function wn(n){return new this(n).ln()}function vn(n,i){return new this(n).log(i)}function Mn(n){return new this(n).log(2)}function bn(n){return new this(n).log(10)}function yn(){return gr(this,arguments,-1)}function kn(){return gr(this,arguments,1)}function Sn(n,i){return new this(n).mod(i)}function Pn(n,i){return new this(n).mul(i)}function An(n,i){return new this(n).pow(i)}function Cn(n){var i,r,o,t,a=0,d=new this(1),c=[];if(n===void 0?n=this.precision:ee(n,1,Me),o=Math.ceil(n/H),this.crypto)if(crypto.getRandomValues)for(i=crypto.getRandomValues(new Uint32Array(o));a<o;)t=i[a],t>=429e7?i[a]=crypto.getRandomValues(new Uint32Array(1))[0]:c[a++]=t%1e7;else if(crypto.randomBytes){for(i=crypto.randomBytes(o*=4);a<o;)t=i[a]+(i[a+1]<<8)+(i[a+2]<<16)+((i[a+3]&127)<<24),t>=214e7?crypto.randomBytes(4).copy(i,a):(c.push(t%1e7),a+=4);a=o/4}else throw Error(fr);else for(;a<o;)c[a++]=Math.random()*1e7|0;for(o=c[--a],n%=H,o&&n&&(t=Zt(10,H-n),c[a]=(o/t|0)*t);c[a]===0;a--)c.pop();if(a<0)r=0,c=[0];else{for(r=-1;c[0]===0;r-=H)c.shift();for(o=1,t=c[0];t>=10;t/=10)o++;o<H&&(r-=H-o)}return d.e=r,d.d=c,d}function xn(n){return z(n=new this(n),n.e+1,this.rounding)}function Dn(n){return n=new this(n),n.d?n.d[0]?n.s:0*n.s:n.s||NaN}function Tn(n){return new this(n).sin()}function On(n){return new this(n).sinh()}function Rn(n){return new this(n).sqrt()}function Ln(n,i){return new this(n).sub(i)}function Nn(){var n=0,i=arguments,r=new this(i[n]);for(Y=!1;r.s&&++n<i.length;)r=r.plus(i[n]);return Y=!0,z(r,this.precision,this.rounding)}function En(n){return new this(n).tan()}function In(n){return new this(n).tanh()}function Bn(n){return z(n=new this(n),n.e+1,1)}B[Symbol.for("nodejs.util.inspect.custom")]=B.toString;B[Symbol.toStringTag]="Decimal";var xe=B.constructor=vr(Di);We=new xe(We);Ze=new xe(Ze);var gt=xe;var Mr=require("@suilend/sdk");var Ve=class{constructor(i){this.getPositionList=async(i=this._sdk.getSenderAddress(),r=!1)=>{let{lending_market_id:o,lending_market_type:t}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend);try{let a=`cap_to_position_mapping_${i}`,d=this._sdk.getCache(a,r),c=await this._sdk.FullClient.getOwnedObjectsByPage(i,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{MatchAny:[{StructType:`${this._sdk.sdkOptions.margin_trading.package_id}::position::PositionCap`}]}});if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:42 ~ PositionModules ~ ownerRes.data.length:",c.data.length),c.data.length===0)return Yt("PositionNotFound","Position not found"),[];let m=await this._sdk.SuiLendModule.getSuilendClient(o,t);console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:48 ~ PositionModules ~ suiLendClient:",m);let{allLendingMarketData:g,lstAprPercentMap:M}=await this._sdk.SuiLendModule.getLendingMarketData();console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:50 ~ PositionModules ~ allLendingMarketData:",g);let P=g[o].reserveMap;d||(d=new Map);let A=[];for(let E=0;E<c.data.length;E++){let L=(0,D.getObjectFields)(c.data[E]);L.position_id&&A.push(L.position_id),L.id.id&&d.set(L.id.id,L.position_id)}await this._sdk.updateCache(a,d,D.CACHE_TIME_24H);let T=await this._sdk.FullClient.batchGetObjects(A,{showContent:!0});console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:73 ~ PositionModules ~ positionRes:",T);let O=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:g,lstAprPercentMap:M},!0);console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:75 ~ PositionModules ~ obligations:",O);let N=[];for(let E=0;E<T.length;E++){let L=A[E],R="";for(let[Z,_]of d.entries())if(_===L){R=Z;break}let I=Le(T[E],R),U=await m.getObligation(I.obligation_owner_cap),W=Ne(I,U,P||{},M||{},O[o].rewardMap),q=this.getObligationRewardsInfo(U,Object.values(P));N.push({...W,claimable_rewards:q})}return N}catch(a){return Yt("FetchError",a),[]}};this.getPositionInfo=async(i,r=this._sdk.getSenderAddress(),o=!0)=>{let t=`cap_to_position_mapping_${r}`,a=this._sdk.getCache(t,o);a||(a=new Map);let d=this.getCapIdByPositionId(i,r)||"";if(!d){let U=await this._sdk.FullClient.getOwnedObjectsByPage(r,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{MatchAny:[{StructType:`${this._sdk.sdkOptions.margin_trading.package_id}::position::PositionCap`}]}});for(let W=0;W<U.data.length;W++){let q=(0,D.getObjectFields)(U.data[W]);q.id.id&&a.set(q.id.id,q.position_id),q.position_id===i&&(d=q.id.id)}}let c=await this._sdk.FullClient.batchGetObjects([i],{showContent:!0}),m=Le(c[0],d||""),{lending_market_id:g,lending_market_type:M}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),P=await this._sdk.SuiLendModule.getSuilendClient(g,M),{allLendingMarketData:A,lstAprPercentMap:T}=await this._sdk.SuiLendModule.getLendingMarketData(),O=A[g].reserveMap,N=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:A,lstAprPercentMap:T},!0),E=await P.getObligation(m.obligation_owner_cap),L=this.getObligationRewardsInfo(E,Object.values(O));return{...Ne(m,E,O||{},T||{},N[g].rewardMap),claimable_rewards:L}};this.getCapIdByPositionId=(i,r=this._sdk.getSenderAddress())=>{let o=`cap_to_position_mapping_${r}`,t=this._sdk.getCache(o,!1);if(!t)return"";for(let[a,d]of t.entries())if(d===i)return a;return""};this.repay=i=>{let{margin_trading:r}=this._sdk.sdkOptions,{lending_market_id:o,lending_market_type:t}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{global_config_id:a,versioned_id:d}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),{txb:c,repay_amount:m,repay_coin_type:g,repay_coin:M,repay_reserve_array_index:P,market_id:A,position_cap_id:T}=i,O=c||new oe.Transaction,N=M||D.CoinAssist.buildCoinWithBalance(BigInt(m.toString()),g,O);O.moveCall({target:`${r.published_at}::router::repay`,typeArguments:[t,g],arguments:[O.object(a),O.object(o),O.object(A),O.object(T),N,O.pure.u64(P.toString()),O.object(D.CLOCK_ADDRESS),O.object(d)]})};this.withdrawAsset=(i,r)=>{let{margin_trading:o}=this._sdk.sdkOptions,{lending_market_id:t,lending_market_type:a}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{global_config_id:d,versioned_id:c}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),{package_id:m}=this._sdk.sdkOptions.suilend,{market_id:g,withdraw_amount:M,withdraw_reserve_array_index:P,withdraw_coin_type:A,position_cap_id:T}=i,[O]=r.moveCall({target:"0x1::option::none",typeArguments:[`${m}::lending_market::RateLimiterExemption<${a}, ${A}>`],arguments:[]});return r.moveCall({target:`${o.published_at}::router::withdraw`,typeArguments:[a,A],arguments:[r.object(d),r.object(t),r.object(g),r.object(T),r.object(O),r.pure.u64(M),r.pure.u64(P),r.object(D.SUI_SYSTEM_STATE_OBJECT_ID),r.object(D.CLOCK_ADDRESS),r.object(c)]})};this.borrowAsset=(i,r)=>{let{reserve_array_index:o,borrow_amount:t,base_token:a,quote_token:d,lending_market_id:c,is_long:m,market_id:g,position_cap_id:M,position_cap:P}=i,{margin_trading:A}=this._sdk.sdkOptions,{lending_market_type:T}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{global_config_id:O,versioned_id:N}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading);return r.moveCall({target:`${A.published_at}::router::borrow`,typeArguments:[T,m?d:a],arguments:[r.object(O),r.object(c),r.object(g),M?r.object(M):P,r.pure.u64(o),r.pure.u64(t.toString()),r.object(D.SUI_SYSTEM_STATE_OBJECT_ID),r.object(D.CLOCK_ADDRESS),r.object(N)]})};this.openPosition=async i=>{let{is_quote:r,is_long:o,amount:t,swap_clmm_pool:a="",slippage:d,leverage:c,market_id:m}=i,{lending_market_id:g}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),M=new oe.Transaction,{base_token:P,quote_token:A}=await this._sdk.MarketModules.getMarketInfo(m),{flash_amount:T,flash_loan_coin:O,is_flash_a:N,borrow_amount:E,clmm_pool_coin_type_a:L,clmm_pool_coin_type_b:R,clmm_pool:I,base_reserve_array_index:U,quote_reserve_array_index:W,reserve:q,has_swap:Z,routers:_}=await this.calculatePositionDeposit({is_long:o,is_quote:r,amount:t,swap_clmm_pool:a,leverage:c,slippage:d,market_id:m,base_token:P,quote_token:A,is_submit:!0});console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:492 ~ PositionModules ~ routers:",_),await this._sdk.SuiLendModule.refreshReservePrices(M,q);let{balance_a:b,balance_b:S,receipt:C}=this._sdk.SwapModules.flashLoan({amount:T,clmm_pool:I,clmm_pool_coin_type_a:L,clmm_pool_coin_type_b:R,flash_loan_coin:O,tx:M}),x=o?A:P,p=o?P:A,e=D.CoinAssist.buildCoinWithBalance(BigInt(t),r?A:P,M),s=D.CoinAssist.fromBalance(b,L,M),l=D.CoinAssist.fromBalance(S,R,M);Z&&(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:515 ~ PositionModules ~ is_flash_a:",N),M.mergeCoins(e,[N?s:l])),console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:521 ~ PositionModules ~ d(flash_amount).add(amount).toString():",(0,D.d)(T).add(t).toString(),x,p);let f=await this._sdk.SwapModules.handleSwap({from:x,to:p,amount:Z?(0,D.d)(T).add(t).toString():T,input_coin:Z?e:N?s:l,swap_clmm_pool:a,slippage:d,tx:M}),h=this.createLeveragePosition({leverage:c,market_id:m,base_token:P,quote_token:A,is_long:o,init_deposit_amount:(0,D.d)(t).toString(),init_coin_type:r?A:P,tx:M}),v=o?U:W;f.swap_out_coin&&!Z&&M.mergeCoins(f.swap_out_coin,[e]),await this.depositToLeveragePosition({is_long:o,market_id:m,position_cap:h,deposit_reserve_array_index:v,input_coin:f.swap_out_coin,base_token:P,quote_token:A},M),console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:580 ~ PositionModules ~ borrow_amount:",E);let k=this.borrowAsset({position_cap:h,reserve_array_index:o?W:U,borrow_amount:E,base_token:P,quote_token:A,is_long:o,lending_market_id:g,market_id:m},M),w=D.CoinAssist.mintBalanceZero(N?R:L,M);return this._sdk.SwapModules.repayFlashLoan({tx:M,clmm_pool:I,repay_base:N?D.CoinAssist.intoBalance(k,L,M):w,repay_quote:N?w:D.CoinAssist.intoBalance(k,R,M),receipt:C,clmm_pool_coin_type_a:L,clmm_pool_coin_type_b:R}),M.transferObjects([h],M.pure.address(this._sdk.getSenderAddress())),D.CoinAssist.destroyBalanceZero(N?D.CoinAssist.intoBalance(l,R,M):D.CoinAssist.intoBalance(s,L,M),N?R:L,M),M};this.positionDeposit=async i=>{let{position_id:r,swap_clmm_pool:o="",is_quote:t,amount:a,slippage:d,leverage:c,txb:m}=i,{lending_market_id:g}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{is_long:M,deposits:P,borrows:A,position_cap_id:T,market_id:O}=await this.getPositionInfo(r),{base_token:N,quote_token:E}=await this._sdk.MarketModules.getMarketInfo(O),L=m||new oe.Transaction,R=P[0].reserveArrayIndex.toString(),I=A[0].reserveArrayIndex.toString(),{flash_amount:U,flash_loan_coin:W,is_flash_a:q,borrow_amount:Z,clmm_pool_coin_type_a:_,clmm_pool_coin_type_b:b,clmm_pool:S,reserve:C,has_swap:x}=await this.calculatePositionDeposit({market_id:O,is_long:M,is_quote:t,amount:a,swap_clmm_pool:o,leverage:c,slippage:d,base_token:N,quote_token:E,position_id:r,is_open:!1});console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:603 ~ PositionModules ~ flash_amount:",U),await this._sdk.SuiLendModule.refreshReservePrices(L,C);let{balance_a:p,balance_b:e,receipt:s}=this._sdk.SwapModules.flashLoan({amount:U,clmm_pool:S,clmm_pool_coin_type_a:_,clmm_pool_coin_type_b:b,flash_loan_coin:W,tx:L}),l=M?E:N,f=M?N:E,h=D.CoinAssist.buildCoinWithBalance(BigInt(a),t?E:N,L),v=D.CoinAssist.fromBalance(p,_,L),k=D.CoinAssist.fromBalance(e,b,L);x&&L.mergeCoins(h,[q?v:k]);let w=await this._sdk.SwapModules.handleSwap({from:l,to:f,amount:x?(0,D.d)(U).add(a).toString():U,input_coin:x?h:q?v:k,swap_clmm_pool:o,slippage:d,tx:L});w.swap_out_coin&&!x&&L.mergeCoins(w.swap_out_coin,[h]),await this.depositToLeveragePosition({is_long:M,market_id:O,position_cap_id:T,deposit_reserve_array_index:R,input_coin:w.swap_out_coin,base_token:N,quote_token:E},L);let u=await this.borrowAsset({position_cap_id:T,reserve_array_index:I,borrow_amount:Z,base_token:N,quote_token:E,is_long:M,lending_market_id:g,market_id:O},L),y=D.CoinAssist.mintBalanceZero(q?b:_,L);return this._sdk.SwapModules.repayFlashLoan({tx:L,clmm_pool:S,repay_base:q?D.CoinAssist.intoBalance(u,_,L):y,repay_quote:q?y:D.CoinAssist.intoBalance(u,b,L),receipt:s,clmm_pool_coin_type_a:_,clmm_pool_coin_type_b:b}),D.CoinAssist.destroyBalanceZero(q?D.CoinAssist.intoBalance(k,b,L):D.CoinAssist.intoBalance(v,_,L),q?b:_,L),L};this.positionWithdraw=async i=>{let{amount:r,is_quote:o,txb:t,swap_clmm_pool:a="",slippage:d,position_id:c,leverage:m}=i,g=t||new oe.Transaction,{withdraw_ctoken_amount:M,repay_amount:P,is_close:A,deposits:T,borrows:O,reserve:N,base_token:E,quote_token:L,clmm_pool:R,clmm_pool_coin_type_a:I,clmm_pool_coin_type_b:U,position_cap_id:W,market_id:q,is_long:Z,swap_convert_all:_,flash_loan_amount:b,routers:S,repay_flash_loan_amount:C="0",partial_amount_in:x,withdraw_amount:p,has_flash_loan:e}=await this.calculatePositionWithdraw({position_id:c,is_quote:o,swap_clmm_pool:a,amount:r,leverage:m,slippage:d});if(A)return this.positionClose({position_id:c,is_quote:o,leverage:m,slippage:d,swap_clmm_pool:a});await this._sdk.SuiLendModule.refreshReservePrices(g,N);let s,l;if(e){let{balance_a:k,balance_b:w,receipt:u,is_loan_a:y}=this._sdk.SwapModules.flashLoan({tx:g,amount:b,flash_loan_coin:O[0].reserve.coinType,clmm_pool:R,clmm_pool_coin_type_a:I,clmm_pool_coin_type_b:U});s=u,l=y,D.CoinAssist.destroyBalanceZero(l?w:k,l?U:I,g);let $=y?D.CoinAssist.fromBalance(k,I,g):D.CoinAssist.fromBalance(w,U,g);this.repay({txb:g,position_cap_id:W,repay_reserve_array_index:O[0].reserveArrayIndex.toString(),repay_coin:$,repay_coin_type:Z?L:E,repay_amount:"0",market_id:q}),g.transferObjects([$],this._sdk.getSenderAddress())}let f=this.withdrawAsset({market_id:q,position_cap_id:W,withdraw_amount:M.toString(),withdraw_reserve_array_index:T[0].reserveArrayIndex.toString(),withdraw_coin_type:Z?E:L},g);for(let k=1;k<T.length;k++){let w=this.withdrawAsset({market_id:q,position_cap_id:W,withdraw_amount:D.U64_MAX.toString(),withdraw_reserve_array_index:T[k].reserveArrayIndex.toString(),withdraw_coin_type:T[k].coinType},g);g.transferObjects([w],this._sdk.getSenderAddress())}let h;if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:778 ~ PositionModules ~ swap_convert_all:",{swap_convert_all:_,has_flash_loan:e}),_)h=await this._sdk.SwapModules.routerSwap({router:S?.route_obj,input_coin:f,slippage:d,txb:g});else{let k=g.splitCoins(f,[g.pure.u64(x?.toString()||"0")]);h=await this._sdk.SwapModules.routerSwap({router:S?.route_obj,input_coin:k,slippage:d,txb:g})}let v;if(h&&e&&(v=g.splitCoins(h,[g.pure.u64(C.toString())])),v&&e){let k=D.CoinAssist.mintBalanceZero(l?U:I,g);this._sdk.SwapModules.repayFlashLoan({tx:g,clmm_pool:R,repay_base:l?D.CoinAssist.intoBalance(v,I,g):k,repay_quote:l?k:D.CoinAssist.intoBalance(v,U,g),clmm_pool_coin_type_a:I,clmm_pool_coin_type_b:U,receipt:s})}else if(h){let k=g.splitCoins(h,[g.pure.u64(C.toString())]);this.repay({txb:g,position_cap_id:W,repay_reserve_array_index:O[0].reserveArrayIndex.toString(),repay_coin:k,repay_coin_type:Z?L:E,repay_amount:"0",market_id:q}),g.transferObjects([k],this._sdk.getSenderAddress())}return h&&g.transferObjects([h],this._sdk.getSenderAddress()),_||g.transferObjects([f],this._sdk.getSenderAddress()),g};this.positionClose=async i=>{let{position_id:r,is_quote:o,slippage:t,leverage:a,swap_clmm_pool:d=""}=i,{margin_trading:c}=this._sdk.sdkOptions,{global_config_id:m,versioned_id:g}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),{lending_market_type:M,lending_market_id:P}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),A=new oe.Transaction,{deposits:T,borrows:O,base_token:N,quote_token:E,clmm_pool:L,clmm_pool_coin_type_a:R,clmm_pool_coin_type_b:I,position_cap_id:U,market_id:W,swap_convert_all:q,routers:Z,partial_amount_in:_,is_long:b,flash_loan_amount:S,reserve:C,repay_flash_loan_amount:x="0",claimable_rewards:p}=await this.calculatePositionWithdraw({position_id:r,is_quote:o,swap_clmm_pool:d,amount:D.U64_MAX.toString(),leverage:a,slippage:t});await this._sdk.SuiLendModule.refreshReservePrices(A,C);let e,s;if((0,D.d)(S).gt(0)){let{balance_a:h,balance_b:v,receipt:k,is_loan_a:w}=this._sdk.SwapModules.flashLoan({tx:A,amount:S,flash_loan_coin:b?E:N,clmm_pool:L,clmm_pool_coin_type_a:R,clmm_pool_coin_type_b:I});e=k,s=w,D.CoinAssist.destroyBalanceZero(s?v:h,s?I:R,A);let u=s?D.CoinAssist.fromBalance(h,R,A):D.CoinAssist.fromBalance(v,I,A);this.repay({txb:A,position_cap_id:U,repay_reserve_array_index:O[0].reserveArrayIndex.toString(),repay_coin:u,repay_coin_type:O[0].reserve.coinType,repay_amount:"0",market_id:W}),A.transferObjects([u],this._sdk.getSenderAddress())}let l=this.withdrawAsset({market_id:W,position_cap_id:U,withdraw_amount:D.U64_MAX.toString(),withdraw_reserve_array_index:T[0].reserveArrayIndex.toString(),withdraw_coin_type:b?N:E},A);await this.buildClaimRewardsMoveCall(p,W,U,A);let f;if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:895 ~ PositionModules ~ swap_convert_all:",q),q)f=await this._sdk.SwapModules.routerSwap({router:Z?.route_obj,input_coin:l,slippage:t,txb:A});else if(_&&(0,D.d)(_).gt(0)){let h=A.splitCoins(l,[A.pure.u64(_?.toString()||"0")]);f=await this._sdk.SwapModules.routerSwap({router:Z?.route_obj,input_coin:h,slippage:t,txb:A})}if(f&&e){let h=D.CoinAssist.mintBalanceZero(s?I:R,A),v=A.splitCoins(f,[A.pure.u64(x?.toString()||"0")]);this._sdk.SwapModules.repayFlashLoan({tx:A,clmm_pool:L,repay_base:s?D.CoinAssist.intoBalance(v,R,A):h,repay_quote:s?h:D.CoinAssist.intoBalance(v,I,A),clmm_pool_coin_type_a:R,clmm_pool_coin_type_b:I,receipt:e})}return A.moveCall({target:`${c.published_at}::router::close_position`,typeArguments:[M],arguments:[A.object(m),A.object(P),A.object(W),A.object(U),A.object(D.CLOCK_ADDRESS),A.object(g)]}),f&&A.transferObjects([f],this._sdk.getSenderAddress()),q||A.transferObjects([l],this._sdk.getSenderAddress()),console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:951 ~ PositionModules ~ tx:",A),A};this.positionLeverageIncrease=async i=>{let{current_leverage:r,target_leverage:o,swap_clmm_pool:t="",slippage:a,position_id:d}=i,{lending_market_id:c}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),m=new oe.Transaction,{flash_loan_amount:g,quote_token:M,base_token:P,reserve:A,deposits:T,borrow_amount:O,routers:N,position_cap_id:E,is_long:L,market_id:R,is_flash_loan:I}=await this.calculatePositionLeverage({position_id:d,current_leverage:r,target_leverage:o,swap_clmm_pool:t,slippage:a}),U=L?M:P;if(await this._sdk.SuiLendModule.refreshReservePrices(m,A),I){let{clmm_pool_coin_type_a:W,clmm_pool_coin_type_b:q,clmm_pool:Z}=await this._sdk.SwapModules.getFlashLoanPool(U,g?.toString()||""),{balance_a:_,balance_b:b,receipt:S,is_loan_a:C}=this._sdk.SwapModules.flashLoan({tx:m,amount:g?.toString()||"0",clmm_pool:Z,clmm_pool_coin_type_a:W,clmm_pool_coin_type_b:q,flash_loan_coin:U}),x=await this._sdk.SwapModules.routerSwap({router:N?.route_obj,input_coin:C?D.CoinAssist.fromBalance(_,W,m):D.CoinAssist.fromBalance(b,q,m),slippage:a,txb:m});this.depositToLeveragePosition({is_long:L,market_id:R,position_cap_id:E,deposit_reserve_array_index:T[0].reserveArrayIndex.toString(),input_coin:x,base_token:P,quote_token:M},m);let p=this.borrowAsset({position_cap_id:E,reserve_array_index:L?A[1].arrayIndex.toString():A[0].arrayIndex.toString(),borrow_amount:O,base_token:P,quote_token:M,is_long:L,lending_market_id:c,market_id:R},m),e=D.CoinAssist.mintBalanceZero(C?q:W,m);this._sdk.SwapModules.repayFlashLoan({tx:m,clmm_pool:Z,repay_base:C?D.CoinAssist.intoBalance(p,W,m):e,repay_quote:C?e:D.CoinAssist.intoBalance(p,q,m),receipt:S,clmm_pool_coin_type_a:W,clmm_pool_coin_type_b:q}),D.CoinAssist.destroyBalanceZero(C?b:_,C?q:W,m)}else{let W=this.borrowAsset({position_cap_id:E,reserve_array_index:L?A[1].arrayIndex.toString():A[0].arrayIndex.toString(),borrow_amount:O,base_token:P,quote_token:M,is_long:L,lending_market_id:c,market_id:R},m),q=await this._sdk.SwapModules.routerSwap({router:N?.route_obj,input_coin:W,slippage:a,txb:m});this.depositToLeveragePosition({is_long:L,market_id:R,position_cap_id:E,deposit_reserve_array_index:T[0].reserveArrayIndex.toString(),input_coin:q,base_token:P,quote_token:M},m)}return m};this.positionLeverageDecrease=async i=>{let{current_leverage:r,target_leverage:o,swap_clmm_pool:t="",slippage:a,position_id:d}=i,{deposits:c,borrows:m,position_cap_id:g,market_id:M,is_long:P}=await this.getPositionInfo(d),{base_token:A,quote_token:T}=await this._sdk.MarketModules.getMarketInfo(M),O=new oe.Transaction,{withdraw_ctoken_amount:N,routers:E,reserve:L,is_flash_loan:R}=await this.calculatePositionLeverage({position_id:d,current_leverage:r,target_leverage:o,swap_clmm_pool:t,slippage:a});if(await this._sdk.SuiLendModule.refreshReservePrices(O,L),R){let{clmm_pool_coin_type_a:I,clmm_pool_coin_type_b:U,clmm_pool:W,clmm_fee_tier:q}=await this._sdk.SwapModules.getFlashLoanPool(c[0].reserve.coinType,N.toString()),{balance_a:Z,balance_b:_,receipt:b,is_loan_a:S}=this._sdk.SwapModules.flashLoan({tx:O,amount:N,clmm_pool:W,clmm_pool_coin_type_a:I,clmm_pool_coin_type_b:U,flash_loan_coin:c[0].reserve.coinType}),C=await this._sdk.SwapModules.routerSwap({router:E?.route_obj,input_coin:S?D.CoinAssist.fromBalance(Z,I,O):D.CoinAssist.fromBalance(_,U,O),slippage:a,txb:O});this.repay({txb:O,market_id:M,position_cap_id:g,repay_reserve_array_index:m[0].reserveArrayIndex.toString(),repay_coin:C,repay_coin_type:P?T:A,repay_amount:"0"}),C&&O.transferObjects([C],this._sdk.getSenderAddress());let x=(0,D.d)(N).mul(q).toString(),p=(0,D.d)(N).add(x).toDP(0,gt.ROUND_UP),e=this.withdrawAsset({market_id:M,position_cap_id:g,withdraw_amount:p.toString(),withdraw_reserve_array_index:c[0].reserveArrayIndex.toString(),withdraw_coin_type:P?A:T},O),s=O.splitCoins(e,[p.toString()]);this._sdk.SwapModules.repayFlashLoan({tx:O,clmm_pool:W,repay_base:S?D.CoinAssist.intoBalance(s,I,O):Z,repay_quote:S?_:D.CoinAssist.intoBalance(s,U,O),receipt:b,clmm_pool_coin_type_a:I,clmm_pool_coin_type_b:U}),O.transferObjects([e],this._sdk.getSenderAddress())}else{let I=this.withdrawAsset({market_id:M,position_cap_id:g,withdraw_amount:N.toString(),withdraw_reserve_array_index:c[0].reserveArrayIndex.toString(),withdraw_coin_type:P?A:T},O);O.transferObjects([I],this._sdk.getSenderAddress())}return O};this.positionRepay=async i=>{let{position_id:r,amount:o,is_quote:t,slippage:a}=i,{routers:d,repay_coin_type:c,base_token:m,quote_token:g,borrows:M,deposits:P,market_id:A,position_cap_id:T}=await this.calculatePositionRepay({position_id:r,amount:o,is_quote:t}),O=this.extractOtherTokenTypes(P,M,m,g),{reserve:N}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(m,g,void 0,O),E=new oe.Transaction;await this._sdk.SuiLendModule.refreshReservePrices(E,N);let L=t?g!==c:m!==c,R;if(L){let I=D.CoinAssist.buildCoinWithBalance(BigInt(o.toString()),t?g:m,E);R=await this._sdk.SwapModules.routerSwap({router:d?.route_obj,input_coin:I,slippage:a,txb:E})}else R=D.CoinAssist.buildCoinWithBalance(BigInt(o.toString()),t?g:m,E);return this.repay({txb:E,position_cap_id:T,repay_reserve_array_index:M[0].reserveArrayIndex.toString(),repay_coin:R,repay_coin_type:c,repay_amount:"0",market_id:A}),E.transferObjects([R],this._sdk.getSenderAddress()),E};this.positionTopUpCToken=async i=>{let{position_id:r,amount:o,is_quote:t,swap_clmm_pool:a="",slippage:d}=i,c=new oe.Transaction,{is_long:m,market_id:g,position_cap_id:M,deposits:P,borrows:A}=await this.getPositionInfo(r),{base_token:T,quote_token:O}=await this._sdk.MarketModules.getMarketInfo(g),N=m&&t||!m&&!t,E=this.extractOtherTokenTypes(P,A,T,O),{base_reserve_array_index:L,quote_reserve_array_index:R,reserve:I}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(T,O,void 0,E);await this._sdk.SuiLendModule.refreshReservePrices(c,I);let U;if(N){let W=await this._sdk.SwapModules.findRouters(t?O:T,t?T:O,o,!0,[a]),q=D.CoinAssist.buildCoinWithBalance(BigInt(o.toString()),t?O:T,c);U=await this._sdk.SwapModules.routerSwap({router:W?.route_obj,input_coin:q,slippage:d,txb:c})}else U=D.CoinAssist.buildCoinWithBalance(BigInt(o.toString()),t?O:T,c);return await this.depositToLeveragePosition({is_long:m,market_id:g,position_cap_id:M,deposit_reserve_array_index:m?L:R,input_coin:U,base_token:T,quote_token:O},c),c};this.positionWithdrawCToken=async i=>{let{position_id:r,amount:o,is_quote:t,swap_clmm_pool:a="",slippage:d}=i,c=new oe.Transaction,{is_long:m,market_id:g,position_cap_id:M,deposits:P,borrows:A}=await this.getPositionInfo(r),{base_token:T,quote_token:O}=await this._sdk.MarketModules.getMarketInfo(g),N=m&&t||!m&&!t,E=this.extractOtherTokenTypes(P,A,T,O),{base_reserve_array_index:L,quote_reserve_array_index:R,reserve:I}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(T,O,void 0,E);await this._sdk.SuiLendModule.refreshReservePrices(c,I);let U=await this._sdk.SuiLendModule.getLatestPriceFeeds(I,!0),W=U[(0,D.removeHexPrefix)(O)]?.price,q=U[(0,D.removeHexPrefix)(T)]?.price,Z=m?(0,D.d)(W).div((0,D.d)(q)).toString():(0,D.d)(q).div((0,D.d)(W)).toString(),_=I[0].mintDecimals,b=I[1].mintDecimals,S=N?(0,D.d)(o).mul(Z).div(10**(t?b:_)).mul(10**(m?_:b)).toDP(0,gt.ROUND_DOWN).toString():o.toString(),C=this.withdrawAsset({market_id:g,position_cap_id:M,withdraw_amount:S,withdraw_reserve_array_index:m?L:R,withdraw_coin_type:m?T:O},c);if(N){let x=await this._sdk.SwapModules.findRouters(m?T:O,t?O:T,S,!0,[a]),p=await this._sdk.SwapModules.routerSwap({router:x?.route_obj,input_coin:C,slippage:d,txb:c});p&&c.transferObjects([p],this._sdk.getSenderAddress())}else c.transferObjects([C],this._sdk.getSenderAddress());return c};this.positionClaim=async i=>{let r=new oe.Transaction,{position_cap_id:o,obligation_owner_cap:t,market_id:a,claimable_rewards:d}=await this.getPositionInfo(i);return console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:1650 ~ PositionModules ~ claimable_rewards:",d),await this.buildClaimRewardsMoveCall(d,a,o,r),r};this.calculatePositionDeposit=async i=>{let{is_quote:r,is_long:o,amount:t,swap_clmm_pool:a="",leverage:d,by_amount_in:c=!0,base_token:m,quote_token:g,is_submit:M=!1,is_open:P=!0,position_id:A}=i,T=[],O=[],N=[];if(!P&&A){let{deposits:rt,borrows:V}=await this.getPositionInfo(A);O=rt,N=V,T=this.extractOtherTokenTypes(O,N,m,g)}let{reserve:E,base_reserve_array_index:L,quote_reserve_array_index:R}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(m,g,void 0,T),I=E[0].mintDecimals,U=E[1].mintDecimals,W=t.toString(),q=o&&r||!o&&!r,Z=await this._sdk.SuiLendModule.getLatestPriceFeedsByCoinTypes([m,g]),_=Z&&Z[g]?.price,b=Z&&Z[m]?.price;if(!_||!b)throw Yt("PriceNotFound","Price not found");if(q&&M){let rt=o?(0,D.d)(_).div((0,D.d)(b)).toString():(0,D.d)(b).div((0,D.d)(_)).toString();W=(0,D.d)(t).mul(rt).div(10**(r?U:I)).mul(10**(o?I:U)).toString(),console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:492 ~ PositionModules ~ initDepositAmount:",{initDepositAmount:W,rate:rt})}else if(q&&!M){let rt=o?g:m,V=o?m:g;W=(await this._sdk.SwapModules.findRouters(rt,V,W,!0,[a]))?.amount_out.toString()}else W=t;let{base_price:S,quote_price:C,flash_amount:x,flash_loan_fee:p,flash_loan_coin:e,is_flash_a:s,clmm_pool:l,clmm_fee_tier:f,clmm_pool_coin_type_a:h,clmm_pool_coin_type_b:v}=await this._sdk.SwapModules.calculateFlashLoan({is_long:o,leverage:d,base_token:m,quote_token:g,deposit_amount:W,reserve:E,base_token_decimal:I,quote_token_decimal:U}),k=await this._sdk.SwapModules.findRouters(o?g:m,o?m:g,x.toString(),!0,[a]),w=(0,D.d)(W).add((0,D.d)(k?.amount_out.toString())).toDP(0,gt.ROUND_DOWN).toString(),u=(0,D.d)(w).mul(o?S:C).toString(),y=(0,D.d)(x).add(p).toDP(0,gt.ROUND_UP).toString(),$=(0,D.d)(y).mul(o?C:S).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:1575 ~ PositionModules ~ depositAmount:",{depositAmount:w,borrowAmount:y});let F=r?(0,D.d)(t).div(10**(r?U:I)).mul(10**I).mul(d).div(S).toDP(0,gt.ROUND_DOWN).toString():(0,D.d)(t).mul(d).toDP(0,gt.ROUND_DOWN).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:1583 ~ PositionModules ~ leverageDepositAmount:",F);let j="0",K="0",G="0",J="0";return!P&&A&&(N&&N.length>0&&(j=(0,D.d)(N[0].borrowedAmount.toString()).mul(o?10**U:10**I).add((0,D.d)(y)).toString(),K=(0,D.d)(j).mul(o?_:b).toString()),G=(0,D.d)(O[0].depositedAmount.toString()).mul(o?10**I:10**U).add((0,D.d)(w)).toString(),J=(0,D.d)(G).mul(o?b:_).toString()),{routers:k,leverage_deposit_amount:F,quote_price:C,base_price:S,clmm_fee_tier:f,flash_loan_fee:p,flash_loan_coin:e,deposit_amount:w,borrow_amount:y,deposit_amount_usd:u,borrow_amount_usd:$,after_borrow_amount:j,after_borrow_amount_usd:K,after_deposit_amount:G,after_deposit_amount_usd:J,flash_amount:x,is_flash_a:s,init_deposit_amount:W,clmm_pool_coin_type_a:h,clmm_pool_coin_type_b:v,clmm_pool:l,base_reserve_array_index:L,quote_reserve_array_index:R,reserve:E,has_swap:q}};this.calculatePositionWithdraw=async i=>{let{position_id:r,is_quote:o,swap_clmm_pool:t="",amount:a,leverage:d,slippage:c,withdraw_max:m}=i,{deposits:g,borrows:M,origin_obligation:P,position_cap_id:A,market_id:T,is_long:O,claimable_rewards:N}=await this.getPositionInfo(r),{base_token:E,quote_token:L}=await this._sdk.MarketModules.getMarketInfo(T),R=r?this.extractOtherTokenTypes(g,M,E,L):[],{reserve:I,base_reserve_map_info:U,quote_reserve_map_info:W}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(E,L,void 0,R),q=I[0].mintDecimals,Z=I[1].mintDecimals,_=O?U.cTokenExchangeRate:W.cTokenExchangeRate,b=await this._sdk.SuiLendModule.getLatestPriceFeeds(I,!0),S=b[(0,D.removeHexPrefix)(L)]?.price,C=b[(0,D.removeHexPrefix)(E)]?.price,x=(0,D.d)(0),p;O?(x=(0,D.d)(o?(0,D.d)(a).mul(S).toString():(0,D.d)(a).mul(C).toString()).div(o?10**Z:10**q),p=o?(0,D.d)(a).div(C).div(10**Z).mul(10**q).toString():(0,D.d)(a)):(x=(0,D.d)(o?(0,D.d)(a).mul(S).toString():(0,D.d)(a).mul(C).toString()).div(o?10**Z:10**q),p=o?(0,D.d)(a):(0,D.d)(a).mul(C).div(10**q).mul(10**Z).toString());let e=(0,D.d)(g[0].depositedAmountUsd.toString()).sub((0,D.d)(M&&M.length>0?M[0].borrowedAmountUsd.toString():"0")).toString(),s=x.div((0,D.d)(e)),l=m||(0,D.d)(s).gte(1),f,h=new gt(0);P&&P.borrows&&P.borrows.length>0&&(f=(0,Mr.compoundDebt)(P.borrows[0],O?I[1]:I[0]),h=(0,D.d)(f.borrowedAmount.value.toString()).div(10**18).toDP(0,gt.ROUND_UP));let v=h.mul(l?1:s).toDP(0,gt.ROUND_UP).toString(),k=v.toString(),w,u,y,$,F,j;if((0,D.d)(v).gt(0)){let{clmm_pool_coin_type_a:nt,clmm_pool_coin_type_b:at,clmm_fee_tier:de,clmm_pool:pt}=await this._sdk.SwapModules.getFlashLoanPool(O?L:E,k);F=nt||"",j=at||"",y=de||0,$=pt||"",w=(0,D.d)(k).mul(de).toString(),u=(0,D.d)(k).mul((0,D.d)(1).add(de)).toDP(0,gt.ROUND_UP).toString()}let K=O&&o||!O&&!o,G,J;if(K){let nt=(0,D.d)(g[0].depositedAmount.toString()).mul(10**(O?q:Z)).mul(l?1:s).toDP(0,gt.ROUND_DOWN).toString();G=await this._sdk.SwapModules.findRouters(g[0].reserve.coinType,O?L:E,nt,!0,[t])}else{let nt=g[0].reserve.coinType==="0x0041f9f9344cac094454cd574e333c4fdb132d7bcc9379bcd4aab485b2a63942::wbtc::WBTC"?.01:.0025;J=(0,D.d)(v).mul(O?S:C).div(10**(O?Z:q)).mul(10**(O?q:Z)).div(O?C:S).mul((0,D.d)(1).add(nt)).toDP(0,gt.ROUND_DOWN).toString(),G=await this._sdk.SwapModules.findRouters(g[0].reserve.coinType,O?L:E,J.toString(),!0,[t]),l||(v=(0,D.d)(G?.amount_out.toString()).mul((0,D.d)(1).sub(c)).toDP(0,gt.ROUND_DOWN).toString())}p=(0,D.d)(p).mul(d).toDP(0,gt.ROUND_DOWN).toString(),x=(0,D.d)(x).mul(d);let rt=h.mul((0,D.d)(1).sub(s)).toString(),V=(0,D.d)(rt).mul(O?S:C).toString(),et=(0,D.d)(g[0].depositedAmount.toString()).mul(O?10**q:10**Z).sub((0,D.d)(p)).toString(),ft=(0,D.d)(et).mul(O?C:S).toString(),tt=(0,D.d)(G?.amount_in.toString()).toString(),X=(0,D.d)(G?.amount_out.toString()).toString(),me=(0,D.d)(p).div(_.toString()).toDP(0,gt.ROUND_DOWN).toString(),dt=(0,D.d)(V).div((0,D.d)(10**(O?Z:q))),st=(0,D.d)(ft).div((0,D.d)(10**(O?q:Z))),ke=!0;if(M.length>0){let nt=(0,D.d)(M[0].borrowedAmountUsd.toString()),at=nt.div(st).toString(),de=(0,D.d)(g[0].reserve.config.openLtvPct).mul((0,D.d)(.95)).div(100).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:1459 ~ PositionModules ~ afterLTV:",{afterLTV:at,ltv:de,afterDepositAmount:et,afterDepositAmountUSD:st,afterBorrowAmount:rt,afterBorrowAmountUSD:dt,withdrawAmount:p,currentBorrowAmountUSD:nt}),(0,D.d)(at).lte(de)&&(ke=!1)}return{amount_in:tt,amount_out:X,from:g[0].reserve.coinType,to:O?E:L,after_borrow_amount:rt,after_borrow_amount_usd:V,after_deposit_amount:et,after_deposit_amount_usd:ft,flash_loan_fee:w,clmm_fee_tier:y,withdraw_amount:p,withdraw_ctoken_amount:me,repay_amount:v,ratio:s,is_close:l,is_long:O,deposits:g,borrows:M,reserve:I,base_token:E,quote_token:L,clmm_pool:$,clmm_pool_coin_type_a:F,clmm_pool_coin_type_b:j,position_cap_id:A,market_id:T,swap_convert_all:K,flash_loan_amount:k,routers:G,repay_flash_loan_amount:u,partial_amount_in:J,has_flash_loan:ke,claimable_rewards:N}};this.calculatePositionLeverage=async i=>{let{position_id:r,current_leverage:o,target_leverage:t,swap_clmm_pool:a=""}=i,d=(0,D.d)(t).sub((0,D.d)(o)),c=d.gt(0),{borrows:m,deposits:g,is_long:M,position_cap_id:P,market_id:A}=await this.getPositionInfo(r),{base_token:T,quote_token:O}=await this._sdk.MarketModules.getMarketInfo(A),N=this.extractOtherTokenTypes(g,m,T,O),{reserve:E}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(T,O,void 0,N),L=E[0].mintDecimals,R=E[1].mintDecimals,I=await this._sdk.SuiLendModule.getLatestPriceFeeds(E,!0),U=I[(0,D.removeHexPrefix)(O)]?.price,W=I[(0,D.removeHexPrefix)(T)]?.price,q=m.length>0,Z=g[0].depositedAmountUsd.toString(),_=g[0].depositedAmount.toString(),b=q?(0,D.d)(m[0].borrowedAmount.toString()).mul(10**(M?R:L)):(0,D.d)(0),S=q?m[0].borrowedAmountUsd.toString():(0,D.d)(0),C=q?(0,D.d)(m[0].reserve.config.borrowWeightBps.toString()).div(1e4).toString():"1",x=(0,D.d)(g[0].reserve.config.openLtvPct).mul((0,D.d)(.95)).div(100).div(C).toString(),p=(0,D.d)(Z).sub((0,D.d)(S)).mul(x).toString();if(c){let e=(0,D.d)(Z).div(o).mul(d),s=e.add(S).div(Z).toString(),l=e.div(M?U:W).mul(10**(M?R:L)).toDP(0,gt.ROUND_UP),f=(0,D.d)(s).gt(x),h=l,v=M?O:T,k=M?T:O,w=await this._sdk.SwapModules.findRouters(v,k,h.toString(),!0,[a]),u=(0,D.d)(w?.amount_out.toString()).toString(),y=(0,D.d)(u).mul(M?W:U).toString(),$,F,j,K,G,J="0",rt="0";if(f){let{clmm_pool_coin_type_a:X,clmm_pool_coin_type_b:me,clmm_pool:dt,clmm_fee_tier:st}=await this._sdk.SwapModules.getFlashLoanPool(v,h.toString());j=st,K=X,G=me,$=h.mul(j).toString(),F=M?K==O:G==T,J=(0,D.d)(h).add($).toDP(0,gt.ROUND_UP).toString(),rt=(0,D.d)(J).mul(M?U:W).toString()}else J=l.toString(),rt=e.toString();let V=(0,D.d)(_).add((0,D.d)(u).div(10**(M?L:R))).toString(),et=(0,D.d)(V).mul(M?W:U).toString(),ft=(0,D.d)(b).add((0,D.d)(J)).div(10**(M?R:L)).toString(),tt=(0,D.d)(ft).mul(M?U:W).toString();return{amount_in:w?.amount_in.toString(),amount_out:w?.amount_out.toString(),flash_loan_fee:$,deposit_amount:u,borrow_amount:J,deposit_amount_usd:y,borrow_amount_usd:rt,flash_loan_amount:h,base_token:T,quote_token:O,reserve:E,deposits:g,borrows:m,clmm_fee_tier:j,is_flash_loan:f,is_loan_a:F,clmm_pool_coin_type_a:K,clmm_pool_coin_type_b:G,after_deposit_amount:V,after_borrow_amount:ft,after_deposit_amount_usd:et,after_borrow_amount_usd:tt,position_cap_id:P,market_id:A,is_long:M,routers:w}}else{let s=(0,D.d)(Z).div(o).mul(d.abs()).div(M?U:W).mul(10**(M?R:L)).toDP(0,gt.ROUND_UP),l=await this._sdk.SwapModules.findRouters(g[0].reserve.coinType,m[0].reserve.coinType,s.toString(),!1,[a]),f=l?.amount_in.toString(),h=(0,D.d)(f).mul(M?W:U).div(10**(M?L:R)).toString(),v=(0,D.d)(Z).mul(x).toString(),k=(0,D.d)(S).div((0,D.d)(Z).sub((0,D.d)(h))).toString(),w=(0,D.d)(k).gt(x),u=s.toString(),y=(0,D.d)(u).mul(M?U:W).toString(),$=f,F=(0,D.d)($).mul(M?W:U).toString(),j,K="",G="";if(w){let{clmm_pool_coin_type_a:ft,clmm_pool_coin_type_b:tt,clmm_pool:X,clmm_fee_tier:me}=await this._sdk.SwapModules.getFlashLoanPool(m[0].reserve.coinType,u.toString());j=me,K=ft,G=tt}let J=(0,D.d)(_).sub((0,D.d)($).div(10**(M?L:R))).toString(),rt=(0,D.d)(J).mul(M?W:U).toString(),V=(0,D.d)(b).sub((0,D.d)(u)).div(10**(M?R:L)).toString(),et=(0,D.d)(V).mul(M?U:W).toString();return{amount_in:l?.amount_in.toString(),amount_out:l?.amount_out.toString(),deposit_amount:$,borrow_amount:u,deposit_amount_usd:F,borrow_amount_usd:y,base_token:T,quote_token:O,reserve:E,deposits:g,borrows:m,clmm_fee_tier:j,withdraw_ctoken_amount:f,is_flash_loan:w,clmm_pool_coin_type_a:K,clmm_pool_coin_type_b:G,after_deposit_amount:J,after_borrow_amount:V,after_deposit_amount_usd:rt,after_borrow_amount_usd:et,position_cap_id:P,market_id:A,is_long:M,routers:l}}};this.calculatePositionRepay=async i=>{let{position_id:r,amount:o,is_quote:t}=i,{borrows:a,deposits:d,is_long:c,market_id:m,position_cap_id:g}=await this._sdk.PositionModules.getPositionInfo(r),M=a[0].reserve.coinType,P=c?d[0].reserve.coinType:a[0].reserve.coinType,A=c?a[0].reserve.coinType:d[0].reserve.coinType,T,O=c?!t:t;return O&&(T=await this._sdk.SwapModules.findRouters(t?A:P,M,o,!0,[])),{routers:T,repay_coin_type:M,base_token:P,quote_token:A,is_long:c,borrows:a,deposits:d,market_id:m,position_cap_id:g,has_swap:O}};this._sdk=i}getObligationRewardsInfo(i,r){let o=[],t=new ct(10).pow(18),a={},d=Date.now();for(let m of r){let g=m.coinType,M=m.arrayIndex.toString(),P=m.depositsPoolRewardManager.poolRewards||[];for(let T of P){if(!T)continue;let O=T.id,N=T.coinType,E=parseInt(T.startTimeMs),L=parseInt(T.endTimeMs),R=d>=E&&d<L,I=T.mintDecimals,U=ct(T.cumulativeRewardsPerShare).times(10**I);a[O]={coinType:N,mintDecimals:I,isActive:R,reserveType:"deposit",reserveArrayIndex:M,reserveCoinType:g,globalCumulativePerShare:U,rewardIndex:T.rewardIndex,reserve:m}}let A=m.borrowsPoolRewardManager.poolRewards||[];for(let T of A){if(!T)continue;let O=T.id,N=T.coinType,E=parseInt(T.startTimeMs),L=parseInt(T.endTimeMs),R=d>=E&&d<L,I=T.mintDecimals,U=ct(T.cumulativeRewardsPerShare).times(10**I);a[O]={coinType:N,mintDecimals:I,isActive:R,reserveType:"borrow",reserveCoinType:g,reserveArrayIndex:M,rewardIndex:T.rewardIndex,globalCumulativePerShare:U,reserve:m}}}let c=i.userRewardManagers;for(let m of c)for(let g=0;g<m.rewards.length;g++){let M=m.rewards[g];if(!M)continue;let P=a[M.poolRewardId];if(!P)continue;let A=P.globalCumulativePerShare,T=new ct(M.cumulativeRewardsPerShare.value.toString()).div(t),O=new ct(m.share).times(A.minus(T)),N=new ct(M.earnedRewards.value).div(t),E=new ct(N).plus(O),L=P.reserve.price,R=E.div(10**P.mintDecimals),I=L?R.times(L).toFixed(12):0;(0,D.d)(I).gt(0)&&o.push({poolRewardId:M.poolRewardId,coinType:P.coinType,earnedRewards:E.toString(),earnedRewardsUsd:I,earnedRewardsFormatted:R.toFixed(12),isActive:P.isActive,reserveType:P.reserveType,reserveCoinType:P.reserveCoinType,reserveArrayIndex:P.reserveArrayIndex,rewardIndex:P.rewardIndex,rewardInfo:P})}return o}createLeveragePosition(i){let{lending_market_id:r,lending_market_type:o}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{global_config_id:t,versioned_id:a}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),d=this._sdk.sdkOptions.margin_trading,{leverage:c,market_id:m,base_token:g,quote_token:M,is_long:P,init_deposit_amount:A,tx:T,init_coin_type:O}=i;return T.moveCall({target:`${d.published_at}::router::open_position`,arguments:[T.object(t),T.object(m),T.object(r),T.pure.u64(A),T.pure.u64((0,D.d)(c).mul(1e4).toString()),T.object(D.CLOCK_ADDRESS),T.object(a)],typeArguments:[o,P?g:M,O]})}async depositToLeveragePosition(i,r){let{margin_trading:o}=this._sdk.sdkOptions,{lending_market_type:t,lending_market_id:a}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{is_long:d,market_id:c,position_cap_id:m,position_cap:g,deposit_reserve_array_index:M,input_coin:P,base_token:A,quote_token:T}=i,{global_config_id:O,versioned_id:N}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading);return r.moveCall({target:`${o.published_at}::router::deposit`,typeArguments:[t,d?A:T],arguments:[r.object(O),r.object(a),r.object(c),m?r.object(m):g,P,r.pure.u64(M),r.object(D.CLOCK_ADDRESS),r.object(N)]}),r}extractOtherTokenTypes(i,r,o,t){let a=[];return i.forEach(d=>{d.reserve.coinType!==o&&d.reserve.coinType!==t&&a.push(d.reserve.coinType)}),r.forEach(d=>{d.reserve.coinType!==o&&d.reserve.coinType!==t&&a.push(d.reserve.coinType)}),a}async buildClaimRewardsMoveCall(i,r,o,t){let{suilend:a,margin_trading:d}=this._sdk.sdkOptions,c=(0,D.getPackagerConfigs)(a).lending_market_id,m=(0,D.getPackagerConfigs)(a).lending_market_type,{global_config_id:g,versioned_id:M}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading);for(let P=0;P<i.length;P++){let A=t.moveCall({target:`${d.published_at}::router::claim_rewards`,arguments:[t.object(g),t.object(c),t.object(r),t.object(o),t.pure.u64(i[P].reserveArrayIndex.toString()),t.pure.u64(i[P].rewardIndex.toString()),t.pure.bool(i[P].reserveType==="deposit"),t.object(D.CLOCK_ADDRESS),t.object(M)],typeArguments:[m,i[P].coinType]});t.transferObjects([A],this._sdk.getSenderAddress())}}};var Pr=require("@suilend/sdk/client"),it=require("@suilend/sdk");var fe=require("@suilend/sdk"),be=require("@suilend/sui-fe");var br=yi(require("crypto")),Je=new Uint8Array(256),Xe=Je.length;function Li(){return Xe>Je.length-16&&(br.default.randomFillSync(Je),Xe=0),Je.slice(Xe,Xe+=16)}var Vt=[];for(let n=0;n<256;++n)Vt.push((n+256).toString(16).slice(1));function yr(n,i=0){return Vt[n[i+0]]+Vt[n[i+1]]+Vt[n[i+2]]+Vt[n[i+3]]+"-"+Vt[n[i+4]]+Vt[n[i+5]]+"-"+Vt[n[i+6]]+Vt[n[i+7]]+"-"+Vt[n[i+8]]+Vt[n[i+9]]+"-"+Vt[n[i+10]]+Vt[n[i+11]]+Vt[n[i+12]]+Vt[n[i+13]]+Vt[n[i+14]]+Vt[n[i+15]]}var kr=yi(require("crypto")),Ni={randomUUID:kr.default.randomUUID};function qn(n,i,r){if(Ni.randomUUID&&!i&&!n)return Ni.randomUUID();n=n||{};let o=n.random||(n.rng||Li)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,i){r=r||0;for(let t=0;t<16;++t)i[r+t]=o[t];return i}return yr(o)}var Ye=qn;var Sr=require("@cetusprotocol/common-sdk"),Qe=10n;function Ei(n){return n.depositedAmount.eq(0)?new ct(0):n.borrowedAmount.div(n.depositedAmount).times(100)}function ti(n){let i=Ei(n);if(!i.gt(100))return(0,fe.linearlyInterpolate)(n.config.interestRate,"utilPercent","aprPercent",i)}function Ii(n){let i=Ei(n),r=ti(n);if(!(r===void 0||i.gt(100)))return new ct(i.div(100)).times(r.div(100)).times(1-n.config.spreadFeeBps/1e4).times(100)}function Fn(n,i,r,o){let{api_url:t}=(0,Sr.getPackagerConfigs)(o.sdkOptions.suilend);n===fe.Side.DEPOSIT&&([be.NORMALIZED_flSUI_COINTYPE,be.NORMALIZED_jugSUI_COINTYPE].includes(i.coinType)&&r.push({stats:{id:Ye(),isActive:!0,rewardIndex:-1,reserve:i,rewardCoinType:"LIQ_AG",mintDecimals:0,symbol:"LiqAg Points",iconUrl:`${t}/partners/LiqAg.png`,perDay:new ct(.036),side:fe.Side.DEPOSIT},obligationClaims:{}}),i.coinType===be.NORMALIZED_LBTC_COINTYPE&&r.push({stats:{id:Ye(),isActive:!0,rewardIndex:-1,reserve:i,rewardCoinType:"LOMBARD",mintDecimals:0,symbol:"3x Lombard Lux",iconUrl:`${t}/partners/Lombard Lux.png`,perDay:new ct(0),side:fe.Side.DEPOSIT},obligationClaims:{}}))}function Un(n,i,r){return{...n,depositedAmount:i===fe.Side.DEPOSIT?ct.max(n.depositedAmount.plus(r),0):n.depositedAmount,borrowedAmount:i===fe.Side.BORROW?ct.max(n.borrowedAmount.plus(r),0):n.borrowedAmount}}function jn(n,i,r){let o=n===fe.Side.DEPOSIT?i.depositedAmount:i.borrowedAmount,t=n===fe.Side.DEPOSIT?r.depositedAmount:r.borrowedAmount,a=t.eq(0)?new ct(-1):o.div(t);return{multiplier:a,isValid:!a.eq(-1)}}function $n(n,i,r,o){let t=n.map(d=>({...d,stats:{...d.stats,perDay:o?d.stats.perDay.times(r):void 0}})),a=i.map(d=>({...d,stats:{...d.stats,aprPercent:o?d.stats.aprPercent.times(r):void 0}}));return{newPerDayRewards:t,newAprRewards:a}}function Bi(n,i){if(!Qe)throw new Error("oraclePriceMultiplierDecimal is required");if(n===0n)throw new Error("Invalid oracle price");if(i<0n){let r=10n**(Qe- -i);return n*r}return n/10n**(i+Qe)}function Wn(n){return(0,be.formatLtvPercent)(new ct(n))}var Jt=require("@cetusprotocol/common-sdk"),qi=require("@mysten/sui/utils");var ei=class{constructor(i){this.getCoinAprByAmount=async(i,r,o)=>{let t=new ct(r.toString()),{allLendingMarketData:a,lstAprPercentMap:d}=await this.getLendingMarketData(!1);if(await this.getInitializeObligations({allLendingMarketData:a,lstAprPercentMap:d},!0),!a||!d)throw new Error("lendingMarketCache is not initialized");let c=Object.values(a??{})[0],m=c.lendingMarket.reserves.find(_=>_.coinType===i);if(!m)throw new Error("reserve not found");let M=(this.obligationsCache?.[c.lendingMarket.id]).rewardMap[m.token.coinType]?.[o]??[],P=(0,it.getFilteredRewards)(M),A=(0,it.getStakingYieldAprPercent)(o,m.coinType,d),T=o===it.Side.DEPOSIT?m.depositAprPercent:m.borrowAprPercent,O=T,N=new ct(1),E=!0;if(o!==void 0&&t!==void 0&&t.gt(0)){let _={...m,depositedAmount:o===it.Side.DEPOSIT?ct.max(m.depositedAmount.plus(o===it.Side.DEPOSIT?t:t.negated()),0):m.depositedAmount,borrowedAmount:o===it.Side.BORROW?ct.max(m.borrowedAmount.plus(o===it.Side.BORROW?t:t.negated()),0):m.borrowedAmount};O=o===it.Side.DEPOSIT?Ii(_):ti(_);let b=o===it.Side.DEPOSIT?m.depositedAmount:m.borrowedAmount,S=o===it.Side.DEPOSIT?_.depositedAmount:_.borrowedAmount;N=S.eq(0)?new ct(-1):b.div(S),E=!N.eq(-1)}let I=(0,it.getDedupedPerDayRewards)(P).map(_=>({..._,stats:{..._.stats,perDay:E?_.stats.perDay.times(N):void 0}})),W=(0,it.getDedupedAprRewards)(P).map(_=>({..._,stats:{..._.stats,aprPercent:E?_.stats.aprPercent.times(N):void 0}})),q=(0,it.getTotalAprPercent)(o,T,P,A),Z=O===void 0||W.some(_=>_.stats.aprPercent===void 0)?void 0:(0,it.getTotalAprPercent)(o,O,W,A);return console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:315 ~ SuiLendModule ~ newTotalAprPercent:",Z?.toString()),{new_total_apr_percent:Z?.toString(),total_apr_percent:q.toString()}};this.refreshReservePrice=async(i,r,o)=>{let{lending_market_id:t,lending_market_type:a}=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend),d=`${t}_${a}`;if(!this.suilendClientCache)throw new Error("suilendClientCache is not initialized");let c=await this.suilendClientCache[d].pythClient.getPriceFeedObjectId(r);this.suilendClientCache[d].refreshReservePrices(i,c,o)};this.getLatestPriceFeeds=async(i,r=!1)=>{let{lending_market_id:o,lending_market_type:t}=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend),a={},d=[];if(i.forEach(M=>{let P=this.sdk.getCache(`getLatestPrice_${M.coinType.name}`,r);P&&this.priceCheck(P,60)?a[M.coinType.name]=P:d.push(M.coinType.name)}),d.length==0)return a;let c=`${o}_${t}`;if(!this.suilendClientCache)throw new Error("suilendClient is not initialized");let m=Array.from(new Set(i.map(M=>M?.priceIdentifier?.bytes?(0,qi.toHEX)(new Uint8Array(M.priceIdentifier.bytes)):(0,Jt.removeHexPrefix)(M.priceIdentifier.toString()))));return(await this.suilendClientCache[c].pythConnection.getLatestPriceFeeds(m))?.forEach((M,P)=>{let A=M.getPriceUnchecked(),T=i[P];if(A){let{price:O,expo:N}=A,E=(0,Jt.d)(O).mul((0,Jt.d)(10).pow((0,Jt.d)(N))).toString(),L={coin_type:T.coinType.name||T.coinType,price:E,oracle_price:0n,last_update_time:A.publishTime};L.oracle_price=Bi(BigInt(O),BigInt(N)),a[T.coinType.name||T.coinType]=L,this.sdk.updateCache(`getLatestPrice_${L.coin_type}`,L)}}),a};this.getLatestPriceFeedsByCoinTypes=async(i,r=!1)=>{try{let{lending_market_id:o}=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend),{allLendingMarketData:t}=await this.getLendingMarketData(!1),a=[];return i.forEach(c=>{let m=t[o].reserveMap[c];a.push(m)}),await this.getLatestPriceFeeds(a,r)}catch(o){Yt("FetchError",o);return}};this.getSuiLendReserveInfo=async(i,r,o,t)=>{let{lending_market_id:a}=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend);o||(o=(await this.getLendingMarketData(!1)).allLendingMarketData);let d=o[a].reserveMap[i],c=o[a].reserveMap[r],m=o[a].refreshedRawReserves.filter(T=>T.coinType.name==(0,Jt.removeHexPrefix)(i)),g=o[a].refreshedRawReserves.filter(T=>T.coinType.name==(0,Jt.removeHexPrefix)(r)),M=o[a].reserveMap[i],P=o[a].reserveMap[r],A=[];return t&&t.forEach(T=>{let O=o[a].refreshedRawReserves.filter(N=>N.coinType.name==(0,Jt.removeHexPrefix)(T));A.push(...O)}),{base_reserve_array_index:d.arrayIndex.toString(),quote_reserve_array_index:c.arrayIndex.toString(),reserve:[m[0],g[0],...A],base_reserve_map_info:M,quote_reserve_map_info:P}};this.refreshReservePrices=async(i,r)=>{for(let o=0;o<r.length;o++){let t=(0,qi.toHEX)(new Uint8Array(r[o].priceIdentifier.bytes));await this.refreshReservePrice(i,t,BigInt(r[o].arrayIndex))}};this.refreshReservePricesV2=async(i,r)=>{for(let o=0;o<r.length;o++){let t=r[o].priceIdentifier.toString();await this.refreshReservePrice(i,t,BigInt(r[o].arrayIndex))}};this.sdk=i}async getSuilendClient(i,r){let o=`${i}_${r}`;return this.suilendClientCache||(this.suilendClientCache={}),this.suilendClientCache[o]||(this.suilendClientCache[o]=await Pr.SuilendClient.initialize(i,r,this.sdk.FullClient,!0)),this.suilendClientCache[o]}async getLendingMarketData(i=!0){let r="lendingMarketData",o=this.sdk.getCache(r,i);if(o)return o;let t=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend)?.lending_market||[];if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:67 ~ SuiLendModule ~ getLendingMarketData ~ lendingMarkets:",t),!t.length)return console.warn("\u26A0\uFE0F No lending markets configured."),{};try{let[a,d]=await Promise.all([(async()=>Object.fromEntries(await Promise.all(t.map(async m=>{let g=await this.getSuilendClient(m.id,m.type),{lendingMarket:M,coinMetadataMap:P,refreshedRawReserves:A,reserveMap:T,reserveCoinTypes:O,reserveCoinMetadataMap:N,rewardCoinTypes:E,activeRewardCoinTypes:L,rewardCoinMetadataMap:R}=await(0,it.initializeSuilend)(this.sdk.FullClient,g),{rewardPriceMap:I}=await(0,it.initializeSuilendRewards)(T,L);return[m.id,{suilendClient:g,lendingMarket:M,coinMetadataMap:P,refreshedRawReserves:A,reserveMap:T,reserveCoinTypes:O,reserveCoinMetadataMap:N,rewardPriceMap:I,rewardCoinTypes:E,activeRewardCoinTypes:L,rewardCoinMetadataMap:R}]}))))(),(async()=>{try{let c=await fetch(`${this.sdk.sdkOptions?.suilend?.config?.api_url}/springsui/apy`),m=await c.json();if(c?.statusCode===500)throw new Error("Failed to fetch SpringSui LST APRs");return Object.fromEntries(Object.entries(m).map(([g,M])=>[g,new ct(M)]))}catch(c){return console.error(c),{}}})()]);return this.sdk.updateCache(r,{allLendingMarketData:a,lstAprPercentMap:d},30*1e3),{allLendingMarketData:a,lstAprPercentMap:d}}catch(a){throw console.log("\u{1F680} getLendingMarketData error:",a),a}}async getInitializeObligations(i,r=!1){if(!r&&this.obligationsCache)return this.obligationsCache;i||(i=await this.getLendingMarketData(!1));let o={};for(let t of Object.values(i.allLendingMarketData)){let{obligationOwnerCaps:a,obligations:d}=await(0,it.initializeObligations)(this.sdk.FullClient,t.suilendClient,t.refreshedRawReserves,t.reserveMap,this.sdk.senderAddress);d.length>0&&console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:181 ~ SuiLendModule ~ getInitializeObligations ~ obligations:",d[0].original.allowedBorrowValueUsd.value.toString());let c=(0,it.formatRewards)(t.reserveMap,t.rewardCoinMetadataMap,t.rewardPriceMap,d);o[t.lendingMarket.id]={obligationOwnerCaps:a,obligations:d,rewardMap:c}}return this.obligationsCache=o,this.obligationsCache}async getSuiLendUserData(i,r=!1){let{allLendingMarketData:o,lstAprPercentMap:t}=await this.getLendingMarketData(!1),a=await this.getInitializeObligations({allLendingMarketData:o,lstAprPercentMap:t},!1),{lending_market_id:d}=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend),c=a[d];if(!c)throw new Error(`No user data found for lending market: ${d}`);let m=c.obligations.find(M=>M.id===i);if(!m)throw new Error(`Obligation not found: ${i}`);let g=(0,it.getNetAprPercent)(m,c.rewardMap,t??{});return{obligation:m,netAprPercent:g,deposits:m.deposits,borrowedAmount:m.borrowedAmount,depositedAmount:m.depositedAmount,netValueUsd:m.netValueUsd,rewardMap:c.rewardMap}}priceCheck(i,r=60){let o=Math.floor(Date.now()/1e3);if(!(Math.abs(o-i.last_update_time)>r))return i}};var xr=require("@mysten/sui/transactions"),Ui=yi(Cr()),lt=require("@cetusprotocol/common-sdk");var ii=class{constructor(i){this.routerSwap=async i=>{let{slippage:r,txb:o,input_coin:t,router:a}=i,d=o||new xr.Transaction;if(a)return await this._sdk.AggregatorClient.routerSwap({router:a,inputCoin:t,slippage:r,txb:d})};this.flashLoan=i=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:183 ~ SwapModules ~ params:",i);let{clmm_pool:r}=this._sdk.ClmmSDK.sdkOptions,{global_config_id:o}=(0,lt.getPackagerConfigs)(this._sdk.ClmmSDK.sdkOptions.clmm_pool),{amount:t,clmm_pool:a,clmm_pool_coin_type_a:d,clmm_pool_coin_type_b:c,flash_loan_coin:m,tx:g}=i,M=(0,lt.addHexPrefix)(d)===m;console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:188 ~ SwapModules ~ isLoanA:",M);let[P,A,T]=g.moveCall({target:`${r.published_at}::pool::flash_loan`,arguments:[g.object(o),g.object(a),g.pure.bool(M),g.pure.u64(t.toString())],typeArguments:[d,c]});return{balance_a:P,balance_b:A,receipt:T,is_loan_a:M,loan_coin_type:M?d:c}};this.getFlashLoanPool=async(i,r)=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:201 ~ SwapModules ~ flash_amount:",r,i);try{let o=await fetch(`https://api-sui.cetus.zone/v3/sui/margin_trading/pools?coin_type=${i}&min_amount=${r}`),{data:t}=await o.json();return console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:206 ~ SwapModules ~ data:",t),t&&t.length>0?{clmm_pool_coin_type_a:t[0].coin_type_a,clmm_pool_coin_type_b:t[0].coin_type_b,clmm_pool:t[0].pool_id,clmm_fee_tier:(0,lt.d)(t[0].fee_rate).div(1e6).toString()}:(Yt("FlashLoanPoolNotFound","FlashLoanPoolNotFound"),{clmm_pool_coin_type_a:"",clmm_pool_coin_type_b:"",clmm_pool:"",clmm_fee_tier:""})}catch(o){return Yt("FetchError",o),{clmm_pool_coin_type_a:"",clmm_pool_coin_type_b:"",clmm_pool:"",clmm_fee_tier:""}}};this.repayFlashLoan=i=>{let{clmm_pool:r}=this._sdk.ClmmSDK.sdkOptions,{global_config_id:o}=(0,lt.getPackagerConfigs)(this._sdk.ClmmSDK.sdkOptions.clmm_pool),{tx:t,repay_base:a,repay_quote:d,receipt:c,clmm_pool:m,clmm_pool_coin_type_a:g,clmm_pool_coin_type_b:M}=i;return t.moveCall({target:`${r.published_at}::pool::repay_flash_loan`,arguments:[t.object(o),t.object(m),a,d,c],typeArguments:[g,M]}),t};this._sdk=i}async findRouters(i,r,o,t,a){try{let d={from:i,target:r,amount:new Ui.default((0,lt.d)(o).toFixed(0).toString()),byAmountIn:t,depth:3},c=await this._sdk.AggregatorClient.findRouters(d);if(c?.error?.code===10001)return{...c,is_exceed:c.insufficientLiquidity};if(c?.insufficientLiquidity)return{...c,is_exceed:c.insufficientLiquidity};if(!c?.paths||c?.paths?.length===0)throw Error("Aggregator no router");return{amount_in:c.amountIn.toString(),amount_out:c.amountOut.toString(),is_exceed:c.insufficientLiquidity,route_obj:c,by_amount_in:!0,origin_res:c}}catch{try{if(a){let c=await this._sdk.AggregatorClient.swapInPools({from:i,target:r,amount:new Ui.default((0,lt.d)(o).toFixed(0).toString()),byAmountIn:t,pools:a});return c?{amount_in:c.routeData.amountIn.toString(),amount_out:c.routeData.amountOut.toString(),is_exceed:c.isExceed,route_obj:c.routeData,by_amount_in:!0,origin_res:c}:null}return null}catch{return null}}}async handleSwapLogic(i){let{is_long:r,is_quote:o,amount:t,base_token:a,quote_token:d,swap_clmm_pool:c="",slippage:m,tx:g}=i,M=r&&o||!r&&!o,P,A,T=t;if(M){let O=r&&o?d:a,N=r&&o?a:d,E=await this.findRouters(O,N,t,!0,[c]);if(T=E?.amount_out.toString(),E){let L=lt.CoinAssist.buildCoinWithBalance(BigInt(t),O,g);A=await this.routerSwap({router:E.route_obj,slippage:m,input_coin:L,txb:g}),P=A}}else P=lt.CoinAssist.buildCoinWithBalance(BigInt(t.toString()),o?d:a,g);return{has_swap:M,input_coin:P,init_deposit_amount:T}}async handleSwap(i){let{swap_clmm_pool:r="",slippage:o,amount:t,from:a,to:d,tx:c,input_coin:m}=i,g=await this.findRouters(a,d,t,!0,[r]),M=g.amount_out.toString();return{swap_out_coin:await this.routerSwap({router:g.route_obj,slippage:o,input_coin:m||lt.CoinAssist.buildCoinWithBalance(BigInt(t),a,c),txb:c}),amount_out:M}}async calculateFlashLoan(i){let{is_long:r,leverage:o,base_token:t,quote_token:a,deposit_amount:d,reserve:c,base_token_decimal:m,quote_token_decimal:g}=i,M=await this._sdk.SuiLendModule.getLatestPriceFeeds(c),P=M&&M[(0,lt.removeHexPrefix)(a)]?.price,A=M&&M[(0,lt.removeHexPrefix)(t)]?.price,T=r?(0,lt.d)(A).div((0,lt.d)(P)).toString():(0,lt.d)(P).div((0,lt.d)(A)).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:267 ~ SwapModules ~ calculateFlashLoan ~ deposit_amount:",d,T,m,g);let O=(0,lt.d)(d).mul((0,lt.d)(o).sub(1)).mul(T).div(10**(r?m:g)).mul(10**(r?g:m)).toDP(0,gt.ROUND_UP).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:274 ~ SwapModules ~ calculateFlashLoan ~ flashAmount:",(0,lt.d)(d).mul((0,lt.d)(o).sub(1)).mul(T).div(10**(r?m:g)).mul(10**(r?g:m)).toString(),O);let N=r?a:t,{clmm_pool_coin_type_a:E,clmm_fee_tier:L,clmm_pool:R,clmm_pool_coin_type_b:I}=await this.getFlashLoanPool(N,O),U=E===N,W=(0,lt.d)(O).mul(L).toString();return{flash_amount:O,rate:T,quote_price:P,base_price:A,flash_loan_coin:N,is_flash_a:U,flash_loan_fee:W,clmm_pool:R,clmm_pool_coin_type_a:E,clmm_pool_coin_type_b:I,clmm_fee_tier:L}}};var Te=class n extends Rr.SdkWrapper{constructor(i,r){super(i),this._suilendModule=new ei(this),this._permissionModules=new qe(this),this._marketModules=new Be(this),this._positionModules=new Ve(this),this._swapModules=new ii(this),this._aggregatorClient=new De.AggregatorClient({signer:(0,Tr.normalizeSuiAddress)("0x0"),client:i.sui_client||new Dr.SuiClient({url:i.full_rpc_url}),env:i.env==="testnet"?De.Env.Testnet:De.Env.Mainnet}),this._clmmSDK=r||Lr.CetusClmmSDK.createSDK({env:i.env,full_rpc_url:i.full_rpc_url}),this._pythConnection=new Or.SuiPriceServiceConnection("https://hermes.pyth.network",{timeout:30*1e3})}get SuiLendModule(){return this._suilendModule}get PermissionModules(){return this._permissionModules}get MarketModules(){return this._marketModules}get PositionModules(){return this._positionModules}get SwapModules(){return this._swapModules}get AggregatorClient(){return this._aggregatorClient}get ClmmSDK(){return this._clmmSDK}get PythConnection(){return this._pythConnection}static createSDK(i){return n.createCustomSDK({...ki,...i})}static createCustomSDK(i){return new n(i)}};var Zn=Te;0&&(module.exports={CetusMarginTradingSDK,appendExtraRewards,calculateBorrowAprPercent,calculateDepositAprPercent,calculateUtilizationPercent,formatLtvPercentValue,getPriceWithFormattedDecimals,getRewardsAprMultiplier,getUpdatedReserve,margin_trading_mainnet,mergePositionData,oraclePriceMultiplierDecimal,updateRewardsWithMultiplier,wrapMarketInfo,wrapMarketPermissions,wrapPosition});
1
+ "use strict";var Nr=Object.create;var Oe=Object.defineProperty;var Ir=Object.getOwnPropertyDescriptor;var Er=Object.getOwnPropertyNames;var Br=Object.getPrototypeOf,qr=Object.prototype.hasOwnProperty;var Fr=(n,i)=>()=>(i||n((i={exports:{}}).exports,i),i.exports),Ur=(n,i)=>{for(var r in i)Oe(n,r,{get:i[r],enumerable:!0})},tr=(n,i,r,o)=>{if(i&&typeof i=="object"||typeof i=="function")for(let t of Er(i))!qr.call(n,t)&&t!==r&&Oe(n,t,{get:()=>i[t],enumerable:!(o=Ir(i,t))||o.enumerable});return n};var yi=(n,i,r)=>(r=n!=null?Nr(Br(n)):{},tr(i||!n||!n.__esModule?Oe(r,"default",{value:n,enumerable:!0}):r,n)),jr=n=>tr(Oe({},"__esModule",{value:!0}),n);var Cr=Fr((Ar,Fi)=>{"use strict";(function(n,i){"use strict";function r(p,e){if(!p)throw new Error(e||"Assertion failed")}function o(p,e){p.super_=e;var s=function(){};s.prototype=e.prototype,p.prototype=new s,p.prototype.constructor=p}function t(p,e,s){if(t.isBN(p))return p;this.negative=0,this.words=null,this.length=0,this.red=null,p!==null&&((e==="le"||e==="be")&&(s=e,e=10),this._init(p||0,e||10,s||"be"))}typeof n=="object"?n.exports=t:i.BN=t,t.BN=t,t.wordSize=26;var a;try{typeof window<"u"&&typeof window.Buffer<"u"?a=window.Buffer:a=require("buffer").Buffer}catch{}t.isBN=function(e){return e instanceof t?!0:e!==null&&typeof e=="object"&&e.constructor.wordSize===t.wordSize&&Array.isArray(e.words)},t.max=function(e,s){return e.cmp(s)>0?e:s},t.min=function(e,s){return e.cmp(s)<0?e:s},t.prototype._init=function(e,s,l){if(typeof e=="number")return this._initNumber(e,s,l);if(typeof e=="object")return this._initArray(e,s,l);s==="hex"&&(s=16),r(s===(s|0)&&s>=2&&s<=36),e=e.toString().replace(/\s+/g,"");var f=0;e[0]==="-"&&(f++,this.negative=1),f<e.length&&(s===16?this._parseHex(e,f,l):(this._parseBase(e,s,f),l==="le"&&this._initArray(this.toArray(),s,l)))},t.prototype._initNumber=function(e,s,l){e<0&&(this.negative=1,e=-e),e<67108864?(this.words=[e&67108863],this.length=1):e<4503599627370496?(this.words=[e&67108863,e/67108864&67108863],this.length=2):(r(e<9007199254740992),this.words=[e&67108863,e/67108864&67108863,1],this.length=3),l==="le"&&this._initArray(this.toArray(),s,l)},t.prototype._initArray=function(e,s,l){if(r(typeof e.length=="number"),e.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(e.length/3),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var h,v,k=0;if(l==="be")for(f=e.length-1,h=0;f>=0;f-=3)v=e[f]|e[f-1]<<8|e[f-2]<<16,this.words[h]|=v<<k&67108863,this.words[h+1]=v>>>26-k&67108863,k+=24,k>=26&&(k-=26,h++);else if(l==="le")for(f=0,h=0;f<e.length;f+=3)v=e[f]|e[f+1]<<8|e[f+2]<<16,this.words[h]|=v<<k&67108863,this.words[h+1]=v>>>26-k&67108863,k+=24,k>=26&&(k-=26,h++);return this._strip()};function d(p,e){var s=p.charCodeAt(e);if(s>=48&&s<=57)return s-48;if(s>=65&&s<=70)return s-55;if(s>=97&&s<=102)return s-87;r(!1,"Invalid character in "+p)}function c(p,e,s){var l=d(p,s);return s-1>=e&&(l|=d(p,s-1)<<4),l}t.prototype._parseHex=function(e,s,l){this.length=Math.ceil((e.length-s)/6),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var h=0,v=0,k;if(l==="be")for(f=e.length-1;f>=s;f-=2)k=c(e,s,f)<<h,this.words[v]|=k&67108863,h>=18?(h-=18,v+=1,this.words[v]|=k>>>26):h+=8;else{var w=e.length-s;for(f=w%2===0?s+1:s;f<e.length;f+=2)k=c(e,s,f)<<h,this.words[v]|=k&67108863,h>=18?(h-=18,v+=1,this.words[v]|=k>>>26):h+=8}this._strip()};function m(p,e,s,l){for(var f=0,h=0,v=Math.min(p.length,s),k=e;k<v;k++){var w=p.charCodeAt(k)-48;f*=l,w>=49?h=w-49+10:w>=17?h=w-17+10:h=w,r(w>=0&&h<l,"Invalid character"),f+=h}return f}t.prototype._parseBase=function(e,s,l){this.words=[0],this.length=1;for(var f=0,h=1;h<=67108863;h*=s)f++;f--,h=h/s|0;for(var v=e.length-l,k=v%f,w=Math.min(v,v-k)+l,u=0,y=l;y<w;y+=f)u=m(e,y,y+f,s),this.imuln(h),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u);if(k!==0){var $=1;for(u=m(e,y,e.length,s),y=0;y<k;y++)$*=s;this.imuln($),this.words[0]+u<67108864?this.words[0]+=u:this._iaddn(u)}this._strip()},t.prototype.copy=function(e){e.words=new Array(this.length);for(var s=0;s<this.length;s++)e.words[s]=this.words[s];e.length=this.length,e.negative=this.negative,e.red=this.red};function g(p,e){p.words=e.words,p.length=e.length,p.negative=e.negative,p.red=e.red}if(t.prototype._move=function(e){g(e,this)},t.prototype.clone=function(){var e=new t(null);return this.copy(e),e},t.prototype._expand=function(e){for(;this.length<e;)this.words[this.length++]=0;return this},t.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},t.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{t.prototype[Symbol.for("nodejs.util.inspect.custom")]=M}catch{t.prototype.inspect=M}else t.prototype.inspect=M;function M(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var P=["","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"],A=[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],T=[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];t.prototype.toString=function(e,s){e=e||10,s=s|0||1;var l;if(e===16||e==="hex"){l="";for(var f=0,h=0,v=0;v<this.length;v++){var k=this.words[v],w=((k<<f|h)&16777215).toString(16);h=k>>>24-f&16777215,f+=2,f>=26&&(f-=26,v--),h!==0||v!==this.length-1?l=P[6-w.length]+w+l:l=w+l}for(h!==0&&(l=h.toString(16)+l);l.length%s!==0;)l="0"+l;return this.negative!==0&&(l="-"+l),l}if(e===(e|0)&&e>=2&&e<=36){var u=A[e],y=T[e];l="";var $=this.clone();for($.negative=0;!$.isZero();){var U=$.modrn(y).toString(e);$=$.idivn(y),$.isZero()?l=U+l:l=P[u-U.length]+U+l}for(this.isZero()&&(l="0"+l);l.length%s!==0;)l="0"+l;return this.negative!==0&&(l="-"+l),l}r(!1,"Base should be between 2 and 36")},t.prototype.toNumber=function(){var e=this.words[0];return this.length===2?e+=this.words[1]*67108864:this.length===3&&this.words[2]===1?e+=4503599627370496+this.words[1]*67108864:this.length>2&&r(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-e:e},t.prototype.toJSON=function(){return this.toString(16,2)},a&&(t.prototype.toBuffer=function(e,s){return this.toArrayLike(a,e,s)}),t.prototype.toArray=function(e,s){return this.toArrayLike(Array,e,s)};var O=function(e,s){return e.allocUnsafe?e.allocUnsafe(s):new e(s)};t.prototype.toArrayLike=function(e,s,l){this._strip();var f=this.byteLength(),h=l||Math.max(1,f);r(f<=h,"byte array longer than desired length"),r(h>0,"Requested array length <= 0");var v=O(e,h),k=s==="le"?"LE":"BE";return this["_toArrayLike"+k](v,f),v},t.prototype._toArrayLikeLE=function(e,s){for(var l=0,f=0,h=0,v=0;h<this.length;h++){var k=this.words[h]<<v|f;e[l++]=k&255,l<e.length&&(e[l++]=k>>8&255),l<e.length&&(e[l++]=k>>16&255),v===6?(l<e.length&&(e[l++]=k>>24&255),f=0,v=0):(f=k>>>24,v+=2)}if(l<e.length)for(e[l++]=f;l<e.length;)e[l++]=0},t.prototype._toArrayLikeBE=function(e,s){for(var l=e.length-1,f=0,h=0,v=0;h<this.length;h++){var k=this.words[h]<<v|f;e[l--]=k&255,l>=0&&(e[l--]=k>>8&255),l>=0&&(e[l--]=k>>16&255),v===6?(l>=0&&(e[l--]=k>>24&255),f=0,v=0):(f=k>>>24,v+=2)}if(l>=0)for(e[l--]=f;l>=0;)e[l--]=0},Math.clz32?t.prototype._countBits=function(e){return 32-Math.clz32(e)}:t.prototype._countBits=function(e){var s=e,l=0;return s>=4096&&(l+=13,s>>>=13),s>=64&&(l+=7,s>>>=7),s>=8&&(l+=4,s>>>=4),s>=2&&(l+=2,s>>>=2),l+s},t.prototype._zeroBits=function(e){if(e===0)return 26;var s=e,l=0;return(s&8191)===0&&(l+=13,s>>>=13),(s&127)===0&&(l+=7,s>>>=7),(s&15)===0&&(l+=4,s>>>=4),(s&3)===0&&(l+=2,s>>>=2),(s&1)===0&&l++,l},t.prototype.bitLength=function(){var e=this.words[this.length-1],s=this._countBits(e);return(this.length-1)*26+s};function N(p){for(var e=new Array(p.bitLength()),s=0;s<e.length;s++){var l=s/26|0,f=s%26;e[s]=p.words[l]>>>f&1}return e}t.prototype.zeroBits=function(){if(this.isZero())return 0;for(var e=0,s=0;s<this.length;s++){var l=this._zeroBits(this.words[s]);if(e+=l,l!==26)break}return e},t.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},t.prototype.toTwos=function(e){return this.negative!==0?this.abs().inotn(e).iaddn(1):this.clone()},t.prototype.fromTwos=function(e){return this.testn(e-1)?this.notn(e).iaddn(1).ineg():this.clone()},t.prototype.isNeg=function(){return this.negative!==0},t.prototype.neg=function(){return this.clone().ineg()},t.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},t.prototype.iuor=function(e){for(;this.length<e.length;)this.words[this.length++]=0;for(var s=0;s<e.length;s++)this.words[s]=this.words[s]|e.words[s];return this._strip()},t.prototype.ior=function(e){return r((this.negative|e.negative)===0),this.iuor(e)},t.prototype.or=function(e){return this.length>e.length?this.clone().ior(e):e.clone().ior(this)},t.prototype.uor=function(e){return this.length>e.length?this.clone().iuor(e):e.clone().iuor(this)},t.prototype.iuand=function(e){var s;this.length>e.length?s=e:s=this;for(var l=0;l<s.length;l++)this.words[l]=this.words[l]&e.words[l];return this.length=s.length,this._strip()},t.prototype.iand=function(e){return r((this.negative|e.negative)===0),this.iuand(e)},t.prototype.and=function(e){return this.length>e.length?this.clone().iand(e):e.clone().iand(this)},t.prototype.uand=function(e){return this.length>e.length?this.clone().iuand(e):e.clone().iuand(this)},t.prototype.iuxor=function(e){var s,l;this.length>e.length?(s=this,l=e):(s=e,l=this);for(var f=0;f<l.length;f++)this.words[f]=s.words[f]^l.words[f];if(this!==s)for(;f<s.length;f++)this.words[f]=s.words[f];return this.length=s.length,this._strip()},t.prototype.ixor=function(e){return r((this.negative|e.negative)===0),this.iuxor(e)},t.prototype.xor=function(e){return this.length>e.length?this.clone().ixor(e):e.clone().ixor(this)},t.prototype.uxor=function(e){return this.length>e.length?this.clone().iuxor(e):e.clone().iuxor(this)},t.prototype.inotn=function(e){r(typeof e=="number"&&e>=0);var s=Math.ceil(e/26)|0,l=e%26;this._expand(s),l>0&&s--;for(var f=0;f<s;f++)this.words[f]=~this.words[f]&67108863;return l>0&&(this.words[f]=~this.words[f]&67108863>>26-l),this._strip()},t.prototype.notn=function(e){return this.clone().inotn(e)},t.prototype.setn=function(e,s){r(typeof e=="number"&&e>=0);var l=e/26|0,f=e%26;return this._expand(l+1),s?this.words[l]=this.words[l]|1<<f:this.words[l]=this.words[l]&~(1<<f),this._strip()},t.prototype.iadd=function(e){var s;if(this.negative!==0&&e.negative===0)return this.negative=0,s=this.isub(e),this.negative^=1,this._normSign();if(this.negative===0&&e.negative!==0)return e.negative=0,s=this.isub(e),e.negative=1,s._normSign();var l,f;this.length>e.length?(l=this,f=e):(l=e,f=this);for(var h=0,v=0;v<f.length;v++)s=(l.words[v]|0)+(f.words[v]|0)+h,this.words[v]=s&67108863,h=s>>>26;for(;h!==0&&v<l.length;v++)s=(l.words[v]|0)+h,this.words[v]=s&67108863,h=s>>>26;if(this.length=l.length,h!==0)this.words[this.length]=h,this.length++;else if(l!==this)for(;v<l.length;v++)this.words[v]=l.words[v];return this},t.prototype.add=function(e){var s;return e.negative!==0&&this.negative===0?(e.negative=0,s=this.sub(e),e.negative^=1,s):e.negative===0&&this.negative!==0?(this.negative=0,s=e.sub(this),this.negative=1,s):this.length>e.length?this.clone().iadd(e):e.clone().iadd(this)},t.prototype.isub=function(e){if(e.negative!==0){e.negative=0;var s=this.iadd(e);return e.negative=1,s._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(e),this.negative=1,this._normSign();var l=this.cmp(e);if(l===0)return this.negative=0,this.length=1,this.words[0]=0,this;var f,h;l>0?(f=this,h=e):(f=e,h=this);for(var v=0,k=0;k<h.length;k++)s=(f.words[k]|0)-(h.words[k]|0)+v,v=s>>26,this.words[k]=s&67108863;for(;v!==0&&k<f.length;k++)s=(f.words[k]|0)+v,v=s>>26,this.words[k]=s&67108863;if(v===0&&k<f.length&&f!==this)for(;k<f.length;k++)this.words[k]=f.words[k];return this.length=Math.max(this.length,k),f!==this&&(this.negative=1),this._strip()},t.prototype.sub=function(e){return this.clone().isub(e)};function I(p,e,s){s.negative=e.negative^p.negative;var l=p.length+e.length|0;s.length=l,l=l-1|0;var f=p.words[0]|0,h=e.words[0]|0,v=f*h,k=v&67108863,w=v/67108864|0;s.words[0]=k;for(var u=1;u<l;u++){for(var y=w>>>26,$=w&67108863,U=Math.min(u,e.length-1),j=Math.max(0,u-p.length+1);j<=U;j++){var K=u-j|0;f=p.words[K]|0,h=e.words[j]|0,v=f*h+$,y+=v/67108864|0,$=v&67108863}s.words[u]=$|0,w=y|0}return w!==0?s.words[u]=w|0:s.length--,s._strip()}var L=function(e,s,l){var f=e.words,h=s.words,v=l.words,k=0,w,u,y,$=f[0]|0,U=$&8191,j=$>>>13,K=f[1]|0,G=K&8191,J=K>>>13,rt=f[2]|0,V=rt&8191,et=rt>>>13,ft=f[3]|0,tt=ft&8191,X=ft>>>13,me=f[4]|0,dt=me&8191,at=me>>>13,ke=f[5]|0,nt=ke&8191,ot=ke>>>13,de=f[6]|0,pt=de&8191,_t=de>>>13,ji=f[7]|0,wt=ji&8191,vt=ji>>>13,$i=f[8]|0,Mt=$i&8191,bt=$i>>>13,Wi=f[9]|0,yt=Wi&8191,kt=Wi>>>13,Zi=h[0]|0,St=Zi&8191,Pt=Zi>>>13,Ki=h[1]|0,At=Ki&8191,Ct=Ki>>>13,zi=h[2]|0,xt=zi&8191,Dt=zi>>>13,Gi=h[3]|0,Tt=Gi&8191,Ot=Gi>>>13,Hi=h[4]|0,Rt=Hi&8191,Lt=Hi>>>13,Vi=h[5]|0,Nt=Vi&8191,It=Vi>>>13,Xi=h[6]|0,Et=Xi&8191,Bt=Xi>>>13,Ji=h[7]|0,qt=Ji&8191,Ft=Ji>>>13,Yi=h[8]|0,Ut=Yi&8191,jt=Yi>>>13,Qi=h[9]|0,$t=Qi&8191,Wt=Qi>>>13;l.negative=e.negative^s.negative,l.length=19,w=Math.imul(U,St),u=Math.imul(U,Pt),u=u+Math.imul(j,St)|0,y=Math.imul(j,Pt);var ri=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(ri>>>26)|0,ri&=67108863,w=Math.imul(G,St),u=Math.imul(G,Pt),u=u+Math.imul(J,St)|0,y=Math.imul(J,Pt),w=w+Math.imul(U,At)|0,u=u+Math.imul(U,Ct)|0,u=u+Math.imul(j,At)|0,y=y+Math.imul(j,Ct)|0;var ni=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(ni>>>26)|0,ni&=67108863,w=Math.imul(V,St),u=Math.imul(V,Pt),u=u+Math.imul(et,St)|0,y=Math.imul(et,Pt),w=w+Math.imul(G,At)|0,u=u+Math.imul(G,Ct)|0,u=u+Math.imul(J,At)|0,y=y+Math.imul(J,Ct)|0,w=w+Math.imul(U,xt)|0,u=u+Math.imul(U,Dt)|0,u=u+Math.imul(j,xt)|0,y=y+Math.imul(j,Dt)|0;var oi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(oi>>>26)|0,oi&=67108863,w=Math.imul(tt,St),u=Math.imul(tt,Pt),u=u+Math.imul(X,St)|0,y=Math.imul(X,Pt),w=w+Math.imul(V,At)|0,u=u+Math.imul(V,Ct)|0,u=u+Math.imul(et,At)|0,y=y+Math.imul(et,Ct)|0,w=w+Math.imul(G,xt)|0,u=u+Math.imul(G,Dt)|0,u=u+Math.imul(J,xt)|0,y=y+Math.imul(J,Dt)|0,w=w+Math.imul(U,Tt)|0,u=u+Math.imul(U,Ot)|0,u=u+Math.imul(j,Tt)|0,y=y+Math.imul(j,Ot)|0;var si=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(si>>>26)|0,si&=67108863,w=Math.imul(dt,St),u=Math.imul(dt,Pt),u=u+Math.imul(at,St)|0,y=Math.imul(at,Pt),w=w+Math.imul(tt,At)|0,u=u+Math.imul(tt,Ct)|0,u=u+Math.imul(X,At)|0,y=y+Math.imul(X,Ct)|0,w=w+Math.imul(V,xt)|0,u=u+Math.imul(V,Dt)|0,u=u+Math.imul(et,xt)|0,y=y+Math.imul(et,Dt)|0,w=w+Math.imul(G,Tt)|0,u=u+Math.imul(G,Ot)|0,u=u+Math.imul(J,Tt)|0,y=y+Math.imul(J,Ot)|0,w=w+Math.imul(U,Rt)|0,u=u+Math.imul(U,Lt)|0,u=u+Math.imul(j,Rt)|0,y=y+Math.imul(j,Lt)|0;var ai=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(ai>>>26)|0,ai&=67108863,w=Math.imul(nt,St),u=Math.imul(nt,Pt),u=u+Math.imul(ot,St)|0,y=Math.imul(ot,Pt),w=w+Math.imul(dt,At)|0,u=u+Math.imul(dt,Ct)|0,u=u+Math.imul(at,At)|0,y=y+Math.imul(at,Ct)|0,w=w+Math.imul(tt,xt)|0,u=u+Math.imul(tt,Dt)|0,u=u+Math.imul(X,xt)|0,y=y+Math.imul(X,Dt)|0,w=w+Math.imul(V,Tt)|0,u=u+Math.imul(V,Ot)|0,u=u+Math.imul(et,Tt)|0,y=y+Math.imul(et,Ot)|0,w=w+Math.imul(G,Rt)|0,u=u+Math.imul(G,Lt)|0,u=u+Math.imul(J,Rt)|0,y=y+Math.imul(J,Lt)|0,w=w+Math.imul(U,Nt)|0,u=u+Math.imul(U,It)|0,u=u+Math.imul(j,Nt)|0,y=y+Math.imul(j,It)|0;var li=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(li>>>26)|0,li&=67108863,w=Math.imul(pt,St),u=Math.imul(pt,Pt),u=u+Math.imul(_t,St)|0,y=Math.imul(_t,Pt),w=w+Math.imul(nt,At)|0,u=u+Math.imul(nt,Ct)|0,u=u+Math.imul(ot,At)|0,y=y+Math.imul(ot,Ct)|0,w=w+Math.imul(dt,xt)|0,u=u+Math.imul(dt,Dt)|0,u=u+Math.imul(at,xt)|0,y=y+Math.imul(at,Dt)|0,w=w+Math.imul(tt,Tt)|0,u=u+Math.imul(tt,Ot)|0,u=u+Math.imul(X,Tt)|0,y=y+Math.imul(X,Ot)|0,w=w+Math.imul(V,Rt)|0,u=u+Math.imul(V,Lt)|0,u=u+Math.imul(et,Rt)|0,y=y+Math.imul(et,Lt)|0,w=w+Math.imul(G,Nt)|0,u=u+Math.imul(G,It)|0,u=u+Math.imul(J,Nt)|0,y=y+Math.imul(J,It)|0,w=w+Math.imul(U,Et)|0,u=u+Math.imul(U,Bt)|0,u=u+Math.imul(j,Et)|0,y=y+Math.imul(j,Bt)|0;var ui=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(ui>>>26)|0,ui&=67108863,w=Math.imul(wt,St),u=Math.imul(wt,Pt),u=u+Math.imul(vt,St)|0,y=Math.imul(vt,Pt),w=w+Math.imul(pt,At)|0,u=u+Math.imul(pt,Ct)|0,u=u+Math.imul(_t,At)|0,y=y+Math.imul(_t,Ct)|0,w=w+Math.imul(nt,xt)|0,u=u+Math.imul(nt,Dt)|0,u=u+Math.imul(ot,xt)|0,y=y+Math.imul(ot,Dt)|0,w=w+Math.imul(dt,Tt)|0,u=u+Math.imul(dt,Ot)|0,u=u+Math.imul(at,Tt)|0,y=y+Math.imul(at,Ot)|0,w=w+Math.imul(tt,Rt)|0,u=u+Math.imul(tt,Lt)|0,u=u+Math.imul(X,Rt)|0,y=y+Math.imul(X,Lt)|0,w=w+Math.imul(V,Nt)|0,u=u+Math.imul(V,It)|0,u=u+Math.imul(et,Nt)|0,y=y+Math.imul(et,It)|0,w=w+Math.imul(G,Et)|0,u=u+Math.imul(G,Bt)|0,u=u+Math.imul(J,Et)|0,y=y+Math.imul(J,Bt)|0,w=w+Math.imul(U,qt)|0,u=u+Math.imul(U,Ft)|0,u=u+Math.imul(j,qt)|0,y=y+Math.imul(j,Ft)|0;var ci=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(ci>>>26)|0,ci&=67108863,w=Math.imul(Mt,St),u=Math.imul(Mt,Pt),u=u+Math.imul(bt,St)|0,y=Math.imul(bt,Pt),w=w+Math.imul(wt,At)|0,u=u+Math.imul(wt,Ct)|0,u=u+Math.imul(vt,At)|0,y=y+Math.imul(vt,Ct)|0,w=w+Math.imul(pt,xt)|0,u=u+Math.imul(pt,Dt)|0,u=u+Math.imul(_t,xt)|0,y=y+Math.imul(_t,Dt)|0,w=w+Math.imul(nt,Tt)|0,u=u+Math.imul(nt,Ot)|0,u=u+Math.imul(ot,Tt)|0,y=y+Math.imul(ot,Ot)|0,w=w+Math.imul(dt,Rt)|0,u=u+Math.imul(dt,Lt)|0,u=u+Math.imul(at,Rt)|0,y=y+Math.imul(at,Lt)|0,w=w+Math.imul(tt,Nt)|0,u=u+Math.imul(tt,It)|0,u=u+Math.imul(X,Nt)|0,y=y+Math.imul(X,It)|0,w=w+Math.imul(V,Et)|0,u=u+Math.imul(V,Bt)|0,u=u+Math.imul(et,Et)|0,y=y+Math.imul(et,Bt)|0,w=w+Math.imul(G,qt)|0,u=u+Math.imul(G,Ft)|0,u=u+Math.imul(J,qt)|0,y=y+Math.imul(J,Ft)|0,w=w+Math.imul(U,Ut)|0,u=u+Math.imul(U,jt)|0,u=u+Math.imul(j,Ut)|0,y=y+Math.imul(j,jt)|0;var fi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(fi>>>26)|0,fi&=67108863,w=Math.imul(yt,St),u=Math.imul(yt,Pt),u=u+Math.imul(kt,St)|0,y=Math.imul(kt,Pt),w=w+Math.imul(Mt,At)|0,u=u+Math.imul(Mt,Ct)|0,u=u+Math.imul(bt,At)|0,y=y+Math.imul(bt,Ct)|0,w=w+Math.imul(wt,xt)|0,u=u+Math.imul(wt,Dt)|0,u=u+Math.imul(vt,xt)|0,y=y+Math.imul(vt,Dt)|0,w=w+Math.imul(pt,Tt)|0,u=u+Math.imul(pt,Ot)|0,u=u+Math.imul(_t,Tt)|0,y=y+Math.imul(_t,Ot)|0,w=w+Math.imul(nt,Rt)|0,u=u+Math.imul(nt,Lt)|0,u=u+Math.imul(ot,Rt)|0,y=y+Math.imul(ot,Lt)|0,w=w+Math.imul(dt,Nt)|0,u=u+Math.imul(dt,It)|0,u=u+Math.imul(at,Nt)|0,y=y+Math.imul(at,It)|0,w=w+Math.imul(tt,Et)|0,u=u+Math.imul(tt,Bt)|0,u=u+Math.imul(X,Et)|0,y=y+Math.imul(X,Bt)|0,w=w+Math.imul(V,qt)|0,u=u+Math.imul(V,Ft)|0,u=u+Math.imul(et,qt)|0,y=y+Math.imul(et,Ft)|0,w=w+Math.imul(G,Ut)|0,u=u+Math.imul(G,jt)|0,u=u+Math.imul(J,Ut)|0,y=y+Math.imul(J,jt)|0,w=w+Math.imul(U,$t)|0,u=u+Math.imul(U,Wt)|0,u=u+Math.imul(j,$t)|0,y=y+Math.imul(j,Wt)|0;var di=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(di>>>26)|0,di&=67108863,w=Math.imul(yt,At),u=Math.imul(yt,Ct),u=u+Math.imul(kt,At)|0,y=Math.imul(kt,Ct),w=w+Math.imul(Mt,xt)|0,u=u+Math.imul(Mt,Dt)|0,u=u+Math.imul(bt,xt)|0,y=y+Math.imul(bt,Dt)|0,w=w+Math.imul(wt,Tt)|0,u=u+Math.imul(wt,Ot)|0,u=u+Math.imul(vt,Tt)|0,y=y+Math.imul(vt,Ot)|0,w=w+Math.imul(pt,Rt)|0,u=u+Math.imul(pt,Lt)|0,u=u+Math.imul(_t,Rt)|0,y=y+Math.imul(_t,Lt)|0,w=w+Math.imul(nt,Nt)|0,u=u+Math.imul(nt,It)|0,u=u+Math.imul(ot,Nt)|0,y=y+Math.imul(ot,It)|0,w=w+Math.imul(dt,Et)|0,u=u+Math.imul(dt,Bt)|0,u=u+Math.imul(at,Et)|0,y=y+Math.imul(at,Bt)|0,w=w+Math.imul(tt,qt)|0,u=u+Math.imul(tt,Ft)|0,u=u+Math.imul(X,qt)|0,y=y+Math.imul(X,Ft)|0,w=w+Math.imul(V,Ut)|0,u=u+Math.imul(V,jt)|0,u=u+Math.imul(et,Ut)|0,y=y+Math.imul(et,jt)|0,w=w+Math.imul(G,$t)|0,u=u+Math.imul(G,Wt)|0,u=u+Math.imul(J,$t)|0,y=y+Math.imul(J,Wt)|0;var hi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(hi>>>26)|0,hi&=67108863,w=Math.imul(yt,xt),u=Math.imul(yt,Dt),u=u+Math.imul(kt,xt)|0,y=Math.imul(kt,Dt),w=w+Math.imul(Mt,Tt)|0,u=u+Math.imul(Mt,Ot)|0,u=u+Math.imul(bt,Tt)|0,y=y+Math.imul(bt,Ot)|0,w=w+Math.imul(wt,Rt)|0,u=u+Math.imul(wt,Lt)|0,u=u+Math.imul(vt,Rt)|0,y=y+Math.imul(vt,Lt)|0,w=w+Math.imul(pt,Nt)|0,u=u+Math.imul(pt,It)|0,u=u+Math.imul(_t,Nt)|0,y=y+Math.imul(_t,It)|0,w=w+Math.imul(nt,Et)|0,u=u+Math.imul(nt,Bt)|0,u=u+Math.imul(ot,Et)|0,y=y+Math.imul(ot,Bt)|0,w=w+Math.imul(dt,qt)|0,u=u+Math.imul(dt,Ft)|0,u=u+Math.imul(at,qt)|0,y=y+Math.imul(at,Ft)|0,w=w+Math.imul(tt,Ut)|0,u=u+Math.imul(tt,jt)|0,u=u+Math.imul(X,Ut)|0,y=y+Math.imul(X,jt)|0,w=w+Math.imul(V,$t)|0,u=u+Math.imul(V,Wt)|0,u=u+Math.imul(et,$t)|0,y=y+Math.imul(et,Wt)|0;var pi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(pi>>>26)|0,pi&=67108863,w=Math.imul(yt,Tt),u=Math.imul(yt,Ot),u=u+Math.imul(kt,Tt)|0,y=Math.imul(kt,Ot),w=w+Math.imul(Mt,Rt)|0,u=u+Math.imul(Mt,Lt)|0,u=u+Math.imul(bt,Rt)|0,y=y+Math.imul(bt,Lt)|0,w=w+Math.imul(wt,Nt)|0,u=u+Math.imul(wt,It)|0,u=u+Math.imul(vt,Nt)|0,y=y+Math.imul(vt,It)|0,w=w+Math.imul(pt,Et)|0,u=u+Math.imul(pt,Bt)|0,u=u+Math.imul(_t,Et)|0,y=y+Math.imul(_t,Bt)|0,w=w+Math.imul(nt,qt)|0,u=u+Math.imul(nt,Ft)|0,u=u+Math.imul(ot,qt)|0,y=y+Math.imul(ot,Ft)|0,w=w+Math.imul(dt,Ut)|0,u=u+Math.imul(dt,jt)|0,u=u+Math.imul(at,Ut)|0,y=y+Math.imul(at,jt)|0,w=w+Math.imul(tt,$t)|0,u=u+Math.imul(tt,Wt)|0,u=u+Math.imul(X,$t)|0,y=y+Math.imul(X,Wt)|0;var mi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(mi>>>26)|0,mi&=67108863,w=Math.imul(yt,Rt),u=Math.imul(yt,Lt),u=u+Math.imul(kt,Rt)|0,y=Math.imul(kt,Lt),w=w+Math.imul(Mt,Nt)|0,u=u+Math.imul(Mt,It)|0,u=u+Math.imul(bt,Nt)|0,y=y+Math.imul(bt,It)|0,w=w+Math.imul(wt,Et)|0,u=u+Math.imul(wt,Bt)|0,u=u+Math.imul(vt,Et)|0,y=y+Math.imul(vt,Bt)|0,w=w+Math.imul(pt,qt)|0,u=u+Math.imul(pt,Ft)|0,u=u+Math.imul(_t,qt)|0,y=y+Math.imul(_t,Ft)|0,w=w+Math.imul(nt,Ut)|0,u=u+Math.imul(nt,jt)|0,u=u+Math.imul(ot,Ut)|0,y=y+Math.imul(ot,jt)|0,w=w+Math.imul(dt,$t)|0,u=u+Math.imul(dt,Wt)|0,u=u+Math.imul(at,$t)|0,y=y+Math.imul(at,Wt)|0;var gi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(gi>>>26)|0,gi&=67108863,w=Math.imul(yt,Nt),u=Math.imul(yt,It),u=u+Math.imul(kt,Nt)|0,y=Math.imul(kt,It),w=w+Math.imul(Mt,Et)|0,u=u+Math.imul(Mt,Bt)|0,u=u+Math.imul(bt,Et)|0,y=y+Math.imul(bt,Bt)|0,w=w+Math.imul(wt,qt)|0,u=u+Math.imul(wt,Ft)|0,u=u+Math.imul(vt,qt)|0,y=y+Math.imul(vt,Ft)|0,w=w+Math.imul(pt,Ut)|0,u=u+Math.imul(pt,jt)|0,u=u+Math.imul(_t,Ut)|0,y=y+Math.imul(_t,jt)|0,w=w+Math.imul(nt,$t)|0,u=u+Math.imul(nt,Wt)|0,u=u+Math.imul(ot,$t)|0,y=y+Math.imul(ot,Wt)|0;var _i=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(_i>>>26)|0,_i&=67108863,w=Math.imul(yt,Et),u=Math.imul(yt,Bt),u=u+Math.imul(kt,Et)|0,y=Math.imul(kt,Bt),w=w+Math.imul(Mt,qt)|0,u=u+Math.imul(Mt,Ft)|0,u=u+Math.imul(bt,qt)|0,y=y+Math.imul(bt,Ft)|0,w=w+Math.imul(wt,Ut)|0,u=u+Math.imul(wt,jt)|0,u=u+Math.imul(vt,Ut)|0,y=y+Math.imul(vt,jt)|0,w=w+Math.imul(pt,$t)|0,u=u+Math.imul(pt,Wt)|0,u=u+Math.imul(_t,$t)|0,y=y+Math.imul(_t,Wt)|0;var wi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(wi>>>26)|0,wi&=67108863,w=Math.imul(yt,qt),u=Math.imul(yt,Ft),u=u+Math.imul(kt,qt)|0,y=Math.imul(kt,Ft),w=w+Math.imul(Mt,Ut)|0,u=u+Math.imul(Mt,jt)|0,u=u+Math.imul(bt,Ut)|0,y=y+Math.imul(bt,jt)|0,w=w+Math.imul(wt,$t)|0,u=u+Math.imul(wt,Wt)|0,u=u+Math.imul(vt,$t)|0,y=y+Math.imul(vt,Wt)|0;var vi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(vi>>>26)|0,vi&=67108863,w=Math.imul(yt,Ut),u=Math.imul(yt,jt),u=u+Math.imul(kt,Ut)|0,y=Math.imul(kt,jt),w=w+Math.imul(Mt,$t)|0,u=u+Math.imul(Mt,Wt)|0,u=u+Math.imul(bt,$t)|0,y=y+Math.imul(bt,Wt)|0;var Mi=(k+w|0)+((u&8191)<<13)|0;k=(y+(u>>>13)|0)+(Mi>>>26)|0,Mi&=67108863,w=Math.imul(yt,$t),u=Math.imul(yt,Wt),u=u+Math.imul(kt,$t)|0,y=Math.imul(kt,Wt);var bi=(k+w|0)+((u&8191)<<13)|0;return k=(y+(u>>>13)|0)+(bi>>>26)|0,bi&=67108863,v[0]=ri,v[1]=ni,v[2]=oi,v[3]=si,v[4]=ai,v[5]=li,v[6]=ui,v[7]=ci,v[8]=fi,v[9]=di,v[10]=hi,v[11]=pi,v[12]=mi,v[13]=gi,v[14]=_i,v[15]=wi,v[16]=vi,v[17]=Mi,v[18]=bi,k!==0&&(v[19]=k,l.length++),l};Math.imul||(L=I);function R(p,e,s){s.negative=e.negative^p.negative,s.length=p.length+e.length;for(var l=0,f=0,h=0;h<s.length-1;h++){var v=f;f=0;for(var k=l&67108863,w=Math.min(h,e.length-1),u=Math.max(0,h-p.length+1);u<=w;u++){var y=h-u,$=p.words[y]|0,U=e.words[u]|0,j=$*U,K=j&67108863;v=v+(j/67108864|0)|0,K=K+k|0,k=K&67108863,v=v+(K>>>26)|0,f+=v>>>26,v&=67108863}s.words[h]=k,l=v,v=f}return l!==0?s.words[h]=l:s.length--,s._strip()}function E(p,e,s){return R(p,e,s)}t.prototype.mulTo=function(e,s){var l,f=this.length+e.length;return this.length===10&&e.length===10?l=L(this,e,s):f<63?l=I(this,e,s):f<1024?l=R(this,e,s):l=E(this,e,s),l};function q(p,e){this.x=p,this.y=e}q.prototype.makeRBT=function(e){for(var s=new Array(e),l=t.prototype._countBits(e)-1,f=0;f<e;f++)s[f]=this.revBin(f,l,e);return s},q.prototype.revBin=function(e,s,l){if(e===0||e===l-1)return e;for(var f=0,h=0;h<s;h++)f|=(e&1)<<s-h-1,e>>=1;return f},q.prototype.permute=function(e,s,l,f,h,v){for(var k=0;k<v;k++)f[k]=s[e[k]],h[k]=l[e[k]]},q.prototype.transform=function(e,s,l,f,h,v){this.permute(v,e,s,l,f,h);for(var k=1;k<h;k<<=1)for(var w=k<<1,u=Math.cos(2*Math.PI/w),y=Math.sin(2*Math.PI/w),$=0;$<h;$+=w)for(var U=u,j=y,K=0;K<k;K++){var G=l[$+K],J=f[$+K],rt=l[$+K+k],V=f[$+K+k],et=U*rt-j*V;V=U*V+j*rt,rt=et,l[$+K]=G+rt,f[$+K]=J+V,l[$+K+k]=G-rt,f[$+K+k]=J-V,K!==w&&(et=u*U-y*j,j=u*j+y*U,U=et)}},q.prototype.guessLen13b=function(e,s){var l=Math.max(s,e)|1,f=l&1,h=0;for(l=l/2|0;l;l=l>>>1)h++;return 1<<h+1+f},q.prototype.conjugate=function(e,s,l){if(!(l<=1))for(var f=0;f<l/2;f++){var h=e[f];e[f]=e[l-f-1],e[l-f-1]=h,h=s[f],s[f]=-s[l-f-1],s[l-f-1]=-h}},q.prototype.normalize13b=function(e,s){for(var l=0,f=0;f<s/2;f++){var h=Math.round(e[2*f+1]/s)*8192+Math.round(e[2*f]/s)+l;e[f]=h&67108863,h<67108864?l=0:l=h/67108864|0}return e},q.prototype.convert13b=function(e,s,l,f){for(var h=0,v=0;v<s;v++)h=h+(e[v]|0),l[2*v]=h&8191,h=h>>>13,l[2*v+1]=h&8191,h=h>>>13;for(v=2*s;v<f;++v)l[v]=0;r(h===0),r((h&-8192)===0)},q.prototype.stub=function(e){for(var s=new Array(e),l=0;l<e;l++)s[l]=0;return s},q.prototype.mulp=function(e,s,l){var f=2*this.guessLen13b(e.length,s.length),h=this.makeRBT(f),v=this.stub(f),k=new Array(f),w=new Array(f),u=new Array(f),y=new Array(f),$=new Array(f),U=new Array(f),j=l.words;j.length=f,this.convert13b(e.words,e.length,k,f),this.convert13b(s.words,s.length,y,f),this.transform(k,v,w,u,f,h),this.transform(y,v,$,U,f,h);for(var K=0;K<f;K++){var G=w[K]*$[K]-u[K]*U[K];u[K]=w[K]*U[K]+u[K]*$[K],w[K]=G}return this.conjugate(w,u,f),this.transform(w,u,j,v,f,h),this.conjugate(j,v,f),this.normalize13b(j,f),l.negative=e.negative^s.negative,l.length=e.length+s.length,l._strip()},t.prototype.mul=function(e){var s=new t(null);return s.words=new Array(this.length+e.length),this.mulTo(e,s)},t.prototype.mulf=function(e){var s=new t(null);return s.words=new Array(this.length+e.length),E(this,e,s)},t.prototype.imul=function(e){return this.clone().mulTo(e,this)},t.prototype.imuln=function(e){var s=e<0;s&&(e=-e),r(typeof e=="number"),r(e<67108864);for(var l=0,f=0;f<this.length;f++){var h=(this.words[f]|0)*e,v=(h&67108863)+(l&67108863);l>>=26,l+=h/67108864|0,l+=v>>>26,this.words[f]=v&67108863}return l!==0&&(this.words[f]=l,this.length++),this.length=e===0?1:this.length,s?this.ineg():this},t.prototype.muln=function(e){return this.clone().imuln(e)},t.prototype.sqr=function(){return this.mul(this)},t.prototype.isqr=function(){return this.imul(this.clone())},t.prototype.pow=function(e){var s=N(e);if(s.length===0)return new t(1);for(var l=this,f=0;f<s.length&&s[f]===0;f++,l=l.sqr());if(++f<s.length)for(var h=l.sqr();f<s.length;f++,h=h.sqr())s[f]!==0&&(l=l.mul(h));return l},t.prototype.iushln=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26,f=67108863>>>26-s<<26-s,h;if(s!==0){var v=0;for(h=0;h<this.length;h++){var k=this.words[h]&f,w=(this.words[h]|0)-k<<s;this.words[h]=w|v,v=k>>>26-s}v&&(this.words[h]=v,this.length++)}if(l!==0){for(h=this.length-1;h>=0;h--)this.words[h+l]=this.words[h];for(h=0;h<l;h++)this.words[h]=0;this.length+=l}return this._strip()},t.prototype.ishln=function(e){return r(this.negative===0),this.iushln(e)},t.prototype.iushrn=function(e,s,l){r(typeof e=="number"&&e>=0);var f;s?f=(s-s%26)/26:f=0;var h=e%26,v=Math.min((e-h)/26,this.length),k=67108863^67108863>>>h<<h,w=l;if(f-=v,f=Math.max(0,f),w){for(var u=0;u<v;u++)w.words[u]=this.words[u];w.length=v}if(v!==0)if(this.length>v)for(this.length-=v,u=0;u<this.length;u++)this.words[u]=this.words[u+v];else this.words[0]=0,this.length=1;var y=0;for(u=this.length-1;u>=0&&(y!==0||u>=f);u--){var $=this.words[u]|0;this.words[u]=y<<26-h|$>>>h,y=$&k}return w&&y!==0&&(w.words[w.length++]=y),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},t.prototype.ishrn=function(e,s,l){return r(this.negative===0),this.iushrn(e,s,l)},t.prototype.shln=function(e){return this.clone().ishln(e)},t.prototype.ushln=function(e){return this.clone().iushln(e)},t.prototype.shrn=function(e){return this.clone().ishrn(e)},t.prototype.ushrn=function(e){return this.clone().iushrn(e)},t.prototype.testn=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26,f=1<<s;if(this.length<=l)return!1;var h=this.words[l];return!!(h&f)},t.prototype.imaskn=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26;if(r(this.negative===0,"imaskn works only with positive numbers"),this.length<=l)return this;if(s!==0&&l++,this.length=Math.min(l,this.length),s!==0){var f=67108863^67108863>>>s<<s;this.words[this.length-1]&=f}return this._strip()},t.prototype.maskn=function(e){return this.clone().imaskn(e)},t.prototype.iaddn=function(e){return r(typeof e=="number"),r(e<67108864),e<0?this.isubn(-e):this.negative!==0?this.length===1&&(this.words[0]|0)<=e?(this.words[0]=e-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(e),this.negative=1,this):this._iaddn(e)},t.prototype._iaddn=function(e){this.words[0]+=e;for(var s=0;s<this.length&&this.words[s]>=67108864;s++)this.words[s]-=67108864,s===this.length-1?this.words[s+1]=1:this.words[s+1]++;return this.length=Math.max(this.length,s+1),this},t.prototype.isubn=function(e){if(r(typeof e=="number"),r(e<67108864),e<0)return this.iaddn(-e);if(this.negative!==0)return this.negative=0,this.iaddn(e),this.negative=1,this;if(this.words[0]-=e,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var s=0;s<this.length&&this.words[s]<0;s++)this.words[s]+=67108864,this.words[s+1]-=1;return this._strip()},t.prototype.addn=function(e){return this.clone().iaddn(e)},t.prototype.subn=function(e){return this.clone().isubn(e)},t.prototype.iabs=function(){return this.negative=0,this},t.prototype.abs=function(){return this.clone().iabs()},t.prototype._ishlnsubmul=function(e,s,l){var f=e.length+l,h;this._expand(f);var v,k=0;for(h=0;h<e.length;h++){v=(this.words[h+l]|0)+k;var w=(e.words[h]|0)*s;v-=w&67108863,k=(v>>26)-(w/67108864|0),this.words[h+l]=v&67108863}for(;h<this.length-l;h++)v=(this.words[h+l]|0)+k,k=v>>26,this.words[h+l]=v&67108863;if(k===0)return this._strip();for(r(k===-1),k=0,h=0;h<this.length;h++)v=-(this.words[h]|0)+k,k=v>>26,this.words[h]=v&67108863;return this.negative=1,this._strip()},t.prototype._wordDiv=function(e,s){var l=this.length-e.length,f=this.clone(),h=e,v=h.words[h.length-1]|0,k=this._countBits(v);l=26-k,l!==0&&(h=h.ushln(l),f.iushln(l),v=h.words[h.length-1]|0);var w=f.length-h.length,u;if(s!=="mod"){u=new t(null),u.length=w+1,u.words=new Array(u.length);for(var y=0;y<u.length;y++)u.words[y]=0}var $=f.clone()._ishlnsubmul(h,1,w);$.negative===0&&(f=$,u&&(u.words[w]=1));for(var U=w-1;U>=0;U--){var j=(f.words[h.length+U]|0)*67108864+(f.words[h.length+U-1]|0);for(j=Math.min(j/v|0,67108863),f._ishlnsubmul(h,j,U);f.negative!==0;)j--,f.negative=0,f._ishlnsubmul(h,1,U),f.isZero()||(f.negative^=1);u&&(u.words[U]=j)}return u&&u._strip(),f._strip(),s!=="div"&&l!==0&&f.iushrn(l),{div:u||null,mod:f}},t.prototype.divmod=function(e,s,l){if(r(!e.isZero()),this.isZero())return{div:new t(0),mod:new t(0)};var f,h,v;return this.negative!==0&&e.negative===0?(v=this.neg().divmod(e,s),s!=="mod"&&(f=v.div.neg()),s!=="div"&&(h=v.mod.neg(),l&&h.negative!==0&&h.iadd(e)),{div:f,mod:h}):this.negative===0&&e.negative!==0?(v=this.divmod(e.neg(),s),s!=="mod"&&(f=v.div.neg()),{div:f,mod:v.mod}):(this.negative&e.negative)!==0?(v=this.neg().divmod(e.neg(),s),s!=="div"&&(h=v.mod.neg(),l&&h.negative!==0&&h.isub(e)),{div:v.div,mod:h}):e.length>this.length||this.cmp(e)<0?{div:new t(0),mod:this}:e.length===1?s==="div"?{div:this.divn(e.words[0]),mod:null}:s==="mod"?{div:null,mod:new t(this.modrn(e.words[0]))}:{div:this.divn(e.words[0]),mod:new t(this.modrn(e.words[0]))}:this._wordDiv(e,s)},t.prototype.div=function(e){return this.divmod(e,"div",!1).div},t.prototype.mod=function(e){return this.divmod(e,"mod",!1).mod},t.prototype.umod=function(e){return this.divmod(e,"mod",!0).mod},t.prototype.divRound=function(e){var s=this.divmod(e);if(s.mod.isZero())return s.div;var l=s.div.negative!==0?s.mod.isub(e):s.mod,f=e.ushrn(1),h=e.andln(1),v=l.cmp(f);return v<0||h===1&&v===0?s.div:s.div.negative!==0?s.div.isubn(1):s.div.iaddn(1)},t.prototype.modrn=function(e){var s=e<0;s&&(e=-e),r(e<=67108863);for(var l=(1<<26)%e,f=0,h=this.length-1;h>=0;h--)f=(l*f+(this.words[h]|0))%e;return s?-f:f},t.prototype.modn=function(e){return this.modrn(e)},t.prototype.idivn=function(e){var s=e<0;s&&(e=-e),r(e<=67108863);for(var l=0,f=this.length-1;f>=0;f--){var h=(this.words[f]|0)+l*67108864;this.words[f]=h/e|0,l=h%e}return this._strip(),s?this.ineg():this},t.prototype.divn=function(e){return this.clone().idivn(e)},t.prototype.egcd=function(e){r(e.negative===0),r(!e.isZero());var s=this,l=e.clone();s.negative!==0?s=s.umod(e):s=s.clone();for(var f=new t(1),h=new t(0),v=new t(0),k=new t(1),w=0;s.isEven()&&l.isEven();)s.iushrn(1),l.iushrn(1),++w;for(var u=l.clone(),y=s.clone();!s.isZero();){for(var $=0,U=1;(s.words[0]&U)===0&&$<26;++$,U<<=1);if($>0)for(s.iushrn($);$-- >0;)(f.isOdd()||h.isOdd())&&(f.iadd(u),h.isub(y)),f.iushrn(1),h.iushrn(1);for(var j=0,K=1;(l.words[0]&K)===0&&j<26;++j,K<<=1);if(j>0)for(l.iushrn(j);j-- >0;)(v.isOdd()||k.isOdd())&&(v.iadd(u),k.isub(y)),v.iushrn(1),k.iushrn(1);s.cmp(l)>=0?(s.isub(l),f.isub(v),h.isub(k)):(l.isub(s),v.isub(f),k.isub(h))}return{a:v,b:k,gcd:l.iushln(w)}},t.prototype._invmp=function(e){r(e.negative===0),r(!e.isZero());var s=this,l=e.clone();s.negative!==0?s=s.umod(e):s=s.clone();for(var f=new t(1),h=new t(0),v=l.clone();s.cmpn(1)>0&&l.cmpn(1)>0;){for(var k=0,w=1;(s.words[0]&w)===0&&k<26;++k,w<<=1);if(k>0)for(s.iushrn(k);k-- >0;)f.isOdd()&&f.iadd(v),f.iushrn(1);for(var u=0,y=1;(l.words[0]&y)===0&&u<26;++u,y<<=1);if(u>0)for(l.iushrn(u);u-- >0;)h.isOdd()&&h.iadd(v),h.iushrn(1);s.cmp(l)>=0?(s.isub(l),f.isub(h)):(l.isub(s),h.isub(f))}var $;return s.cmpn(1)===0?$=f:$=h,$.cmpn(0)<0&&$.iadd(e),$},t.prototype.gcd=function(e){if(this.isZero())return e.abs();if(e.isZero())return this.abs();var s=this.clone(),l=e.clone();s.negative=0,l.negative=0;for(var f=0;s.isEven()&&l.isEven();f++)s.iushrn(1),l.iushrn(1);do{for(;s.isEven();)s.iushrn(1);for(;l.isEven();)l.iushrn(1);var h=s.cmp(l);if(h<0){var v=s;s=l,l=v}else if(h===0||l.cmpn(1)===0)break;s.isub(l)}while(!0);return l.iushln(f)},t.prototype.invm=function(e){return this.egcd(e).a.umod(e)},t.prototype.isEven=function(){return(this.words[0]&1)===0},t.prototype.isOdd=function(){return(this.words[0]&1)===1},t.prototype.andln=function(e){return this.words[0]&e},t.prototype.bincn=function(e){r(typeof e=="number");var s=e%26,l=(e-s)/26,f=1<<s;if(this.length<=l)return this._expand(l+1),this.words[l]|=f,this;for(var h=f,v=l;h!==0&&v<this.length;v++){var k=this.words[v]|0;k+=h,h=k>>>26,k&=67108863,this.words[v]=k}return h!==0&&(this.words[v]=h,this.length++),this},t.prototype.isZero=function(){return this.length===1&&this.words[0]===0},t.prototype.cmpn=function(e){var s=e<0;if(this.negative!==0&&!s)return-1;if(this.negative===0&&s)return 1;this._strip();var l;if(this.length>1)l=1;else{s&&(e=-e),r(e<=67108863,"Number is too big");var f=this.words[0]|0;l=f===e?0:f<e?-1:1}return this.negative!==0?-l|0:l},t.prototype.cmp=function(e){if(this.negative!==0&&e.negative===0)return-1;if(this.negative===0&&e.negative!==0)return 1;var s=this.ucmp(e);return this.negative!==0?-s|0:s},t.prototype.ucmp=function(e){if(this.length>e.length)return 1;if(this.length<e.length)return-1;for(var s=0,l=this.length-1;l>=0;l--){var f=this.words[l]|0,h=e.words[l]|0;if(f!==h){f<h?s=-1:f>h&&(s=1);break}}return s},t.prototype.gtn=function(e){return this.cmpn(e)===1},t.prototype.gt=function(e){return this.cmp(e)===1},t.prototype.gten=function(e){return this.cmpn(e)>=0},t.prototype.gte=function(e){return this.cmp(e)>=0},t.prototype.ltn=function(e){return this.cmpn(e)===-1},t.prototype.lt=function(e){return this.cmp(e)===-1},t.prototype.lten=function(e){return this.cmpn(e)<=0},t.prototype.lte=function(e){return this.cmp(e)<=0},t.prototype.eqn=function(e){return this.cmpn(e)===0},t.prototype.eq=function(e){return this.cmp(e)===0},t.red=function(e){return new C(e)},t.prototype.toRed=function(e){return r(!this.red,"Already a number in reduction context"),r(this.negative===0,"red works only with positives"),e.convertTo(this)._forceRed(e)},t.prototype.fromRed=function(){return r(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},t.prototype._forceRed=function(e){return this.red=e,this},t.prototype.forceRed=function(e){return r(!this.red,"Already a number in reduction context"),this._forceRed(e)},t.prototype.redAdd=function(e){return r(this.red,"redAdd works only with red numbers"),this.red.add(this,e)},t.prototype.redIAdd=function(e){return r(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,e)},t.prototype.redSub=function(e){return r(this.red,"redSub works only with red numbers"),this.red.sub(this,e)},t.prototype.redISub=function(e){return r(this.red,"redISub works only with red numbers"),this.red.isub(this,e)},t.prototype.redShl=function(e){return r(this.red,"redShl works only with red numbers"),this.red.shl(this,e)},t.prototype.redMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.mul(this,e)},t.prototype.redIMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.imul(this,e)},t.prototype.redSqr=function(){return r(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},t.prototype.redISqr=function(){return r(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},t.prototype.redSqrt=function(){return r(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},t.prototype.redInvm=function(){return r(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},t.prototype.redNeg=function(){return r(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},t.prototype.redPow=function(e){return r(this.red&&!e.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,e)};var W={k256:null,p224:null,p192:null,p25519:null};function F(p,e){this.name=p,this.p=new t(e,16),this.n=this.p.bitLength(),this.k=new t(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}F.prototype._tmp=function(){var e=new t(null);return e.words=new Array(Math.ceil(this.n/13)),e},F.prototype.ireduce=function(e){var s=e,l;do this.split(s,this.tmp),s=this.imulK(s),s=s.iadd(this.tmp),l=s.bitLength();while(l>this.n);var f=l<this.n?-1:s.ucmp(this.p);return f===0?(s.words[0]=0,s.length=1):f>0?s.isub(this.p):s.strip!==void 0?s.strip():s._strip(),s},F.prototype.split=function(e,s){e.iushrn(this.n,0,s)},F.prototype.imulK=function(e){return e.imul(this.k)};function Z(){F.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}o(Z,F),Z.prototype.split=function(e,s){for(var l=4194303,f=Math.min(e.length,9),h=0;h<f;h++)s.words[h]=e.words[h];if(s.length=f,e.length<=9){e.words[0]=0,e.length=1;return}var v=e.words[9];for(s.words[s.length++]=v&l,h=10;h<e.length;h++){var k=e.words[h]|0;e.words[h-10]=(k&l)<<4|v>>>22,v=k}v>>>=22,e.words[h-10]=v,v===0&&e.length>10?e.length-=10:e.length-=9},Z.prototype.imulK=function(e){e.words[e.length]=0,e.words[e.length+1]=0,e.length+=2;for(var s=0,l=0;l<e.length;l++){var f=e.words[l]|0;s+=f*977,e.words[l]=s&67108863,s=f*64+(s/67108864|0)}return e.words[e.length-1]===0&&(e.length--,e.words[e.length-1]===0&&e.length--),e};function _(){F.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}o(_,F);function b(){F.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}o(b,F);function S(){F.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}o(S,F),S.prototype.imulK=function(e){for(var s=0,l=0;l<e.length;l++){var f=(e.words[l]|0)*19+s,h=f&67108863;f>>>=26,e.words[l]=h,s=f}return s!==0&&(e.words[e.length++]=s),e},t._prime=function(e){if(W[e])return W[e];var s;if(e==="k256")s=new Z;else if(e==="p224")s=new _;else if(e==="p192")s=new b;else if(e==="p25519")s=new S;else throw new Error("Unknown prime "+e);return W[e]=s,s};function C(p){if(typeof p=="string"){var e=t._prime(p);this.m=e.p,this.prime=e}else r(p.gtn(1),"modulus must be greater than 1"),this.m=p,this.prime=null}C.prototype._verify1=function(e){r(e.negative===0,"red works only with positives"),r(e.red,"red works only with red numbers")},C.prototype._verify2=function(e,s){r((e.negative|s.negative)===0,"red works only with positives"),r(e.red&&e.red===s.red,"red works only with red numbers")},C.prototype.imod=function(e){return this.prime?this.prime.ireduce(e)._forceRed(this):(g(e,e.umod(this.m)._forceRed(this)),e)},C.prototype.neg=function(e){return e.isZero()?e.clone():this.m.sub(e)._forceRed(this)},C.prototype.add=function(e,s){this._verify2(e,s);var l=e.add(s);return l.cmp(this.m)>=0&&l.isub(this.m),l._forceRed(this)},C.prototype.iadd=function(e,s){this._verify2(e,s);var l=e.iadd(s);return l.cmp(this.m)>=0&&l.isub(this.m),l},C.prototype.sub=function(e,s){this._verify2(e,s);var l=e.sub(s);return l.cmpn(0)<0&&l.iadd(this.m),l._forceRed(this)},C.prototype.isub=function(e,s){this._verify2(e,s);var l=e.isub(s);return l.cmpn(0)<0&&l.iadd(this.m),l},C.prototype.shl=function(e,s){return this._verify1(e),this.imod(e.ushln(s))},C.prototype.imul=function(e,s){return this._verify2(e,s),this.imod(e.imul(s))},C.prototype.mul=function(e,s){return this._verify2(e,s),this.imod(e.mul(s))},C.prototype.isqr=function(e){return this.imul(e,e.clone())},C.prototype.sqr=function(e){return this.mul(e,e)},C.prototype.sqrt=function(e){if(e.isZero())return e.clone();var s=this.m.andln(3);if(r(s%2===1),s===3){var l=this.m.add(new t(1)).iushrn(2);return this.pow(e,l)}for(var f=this.m.subn(1),h=0;!f.isZero()&&f.andln(1)===0;)h++,f.iushrn(1);r(!f.isZero());var v=new t(1).toRed(this),k=v.redNeg(),w=this.m.subn(1).iushrn(1),u=this.m.bitLength();for(u=new t(2*u*u).toRed(this);this.pow(u,w).cmp(k)!==0;)u.redIAdd(k);for(var y=this.pow(u,f),$=this.pow(e,f.addn(1).iushrn(1)),U=this.pow(e,f),j=h;U.cmp(v)!==0;){for(var K=U,G=0;K.cmp(v)!==0;G++)K=K.redSqr();r(G<j);var J=this.pow(y,new t(1).iushln(j-G-1));$=$.redMul(J),y=J.redSqr(),U=U.redMul(y),j=G}return $},C.prototype.invm=function(e){var s=e._invmp(this.m);return s.negative!==0?(s.negative=0,this.imod(s).redNeg()):this.imod(s)},C.prototype.pow=function(e,s){if(s.isZero())return new t(1).toRed(this);if(s.cmpn(1)===0)return e.clone();var l=4,f=new Array(1<<l);f[0]=new t(1).toRed(this),f[1]=e;for(var h=2;h<f.length;h++)f[h]=this.mul(f[h-1],e);var v=f[0],k=0,w=0,u=s.bitLength()%26;for(u===0&&(u=26),h=s.length-1;h>=0;h--){for(var y=s.words[h],$=u-1;$>=0;$--){var U=y>>$&1;if(v!==f[0]&&(v=this.sqr(v)),U===0&&k===0){w=0;continue}k<<=1,k|=U,w++,!(w!==l&&(h!==0||$!==0))&&(v=this.mul(v,f[k]),w=0,k=0)}u=26}return v},C.prototype.convertTo=function(e){var s=e.umod(this.m);return s===e?s.clone():s},C.prototype.convertFrom=function(e){var s=e.clone();return s.red=null,s},t.mont=function(e){return new x(e)};function x(p){C.call(this,p),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new t(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)}o(x,C),x.prototype.convertTo=function(e){return this.imod(e.ushln(this.shift))},x.prototype.convertFrom=function(e){var s=this.imod(e.mul(this.rinv));return s.red=null,s},x.prototype.imul=function(e,s){if(e.isZero()||s.isZero())return e.words[0]=0,e.length=1,e;var l=e.imul(s),f=l.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),h=l.isub(f).iushrn(this.shift),v=h;return h.cmp(this.m)>=0?v=h.isub(this.m):h.cmpn(0)<0&&(v=h.iadd(this.m)),v._forceRed(this)},x.prototype.mul=function(e,s){if(e.isZero()||s.isZero())return new t(0)._forceRed(this);var l=e.mul(s),f=l.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),h=l.isub(f).iushrn(this.shift),v=h;return h.cmp(this.m)>=0?v=h.isub(this.m):h.cmpn(0)<0&&(v=h.iadd(this.m)),v._forceRed(this)},x.prototype.invm=function(e){var s=this.imod(e._invmp(this.m).mul(this.r2));return s._forceRed(this)}})(typeof Fi>"u"||Fi,Ar)});var Kn={};Ur(Kn,{CetusMarginTradingSDK:()=>Te,appendExtraRewards:()=>Fn,calculateBorrowAprPercent:()=>ti,calculateDepositAprPercent:()=>Ei,calculateUtilizationPercent:()=>Ii,default:()=>Zn,formatLtvPercentValue:()=>Wn,getPriceWithFormattedDecimals:()=>Bi,getRewardsAprMultiplier:()=>jn,getUpdatedReserve:()=>Un,margin_trading_mainnet:()=>ki,mergePositionData:()=>Ne,oraclePriceMultiplierDecimal:()=>Qe,updateRewardsWithMultiplier:()=>$n,wrapMarketInfo:()=>Re,wrapMarketPermissions:()=>Ie,wrapPosition:()=>Le});module.exports=jr(Kn);var Dr=require("@mysten/sui/client"),Tr=require("@mysten/sui/utils"),Or=require("@pythnetwork/pyth-sui-js"),Rr=require("@cetusprotocol/common-sdk"),Lr=require("@cetusprotocol/sui-clmm-sdk"),De=require("@cetusprotocol/aggregator-sdk");var er=require("@cetusprotocol/common-sdk"),ki={full_rpc_url:er.FullRpcUrlMainnet,env:"mainnet",aggregator_url:"https://api-sui.cetus.zone/router_v3",margin_trading:{package_id:"0xd36d24553943160f960d3c0065cb6b77910328d23669233d304290b84e101400",published_at:"0xd36d24553943160f960d3c0065cb6b77910328d23669233d304290b84e101400",version:0,config:{versioned_id:"0x7408869c4ef21ea5e7027e2a49933954669862b707d5bec5c2741bf1297a6cc9",admin_cap_id:"0x621d93c68cc44043acea6c95dfbe1176ffb710e84ad78fe7e3fc4057f64596a2",global_config_id:"0x0627a2c9ddc7c8e39ae2946e0d51d80554d9e3857f3a7e0efafcc60e2ee10096",markets:"0x01f934d67fbd524ff1b9570531fecf198dcda1b41996c3ac8752cf7f1cc66420",markets_table_id:"0x999a28a63aab15342799909fb352d2a8c7a92adf061dbb14339cdd7ff6e23184"}},suilend:{package_id:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf",published_at:"0xe37cc7bb50fd9b6dbd3873df66fa2c554e973697f50ef97707311dc78bd08444",config:{lending_market:[{name:"Main market",slug:"main",id:"0x84030d26d85eaa7035084a057f2f11f701b7e2e4eda87551becbc7c97505ece1",type:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf::suilend::MAIN_POOL",owner_cap_id:"0xf7a4defe0b6566b6a2674a02a0c61c9f99bd012eed21bc741a069eaa82d35927"},{name:"STEAMM LM",slug:"steamm-lm",id:"0xc1888ec1b81a414e427a44829310508352aec38252ee0daa9f8b181b6947de9f",type:"0x0a071f4976abae1a7f722199cf0bfcbe695ef9408a878e7d12a7ca87b7e582a6::lp_rewards::LP_REWARDS",owner_cap_id:"0x55a0f33b24e091830302726c8cfbff8cf8abd2ec1f83a4e6f4bf51c7ba3ad5ab",is_hidden:!0}],lending_market_id:"0x84030d26d85eaa7035084a057f2f11f701b7e2e4eda87551becbc7c97505ece1",lending_market_type:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf::suilend::MAIN_POOL",api_url:"https://d10td5ybgrf39v.cloudfront.net"}}};var Kt=require("@cetusprotocol/common-sdk"),Ae=require("@mysten/sui/transactions");var Gt=require("@cetusprotocol/common-sdk"),ir=require("@suilend/sdk/parsers/obligation"),se=require("@suilend/sdk"),Re=n=>{let i=(0,Gt.getObjectFields)(n),{open_permissions_pause:r,close_permissions_pause:o,deposit_permissions_pause:t,withdraw_permissions_pause:a,borrow_permissions_pause:d,repay_permissions_pause:c}=Ie(i.permissions.toString(2));return{market_id:i.id.id,base_token:(0,Gt.addHexPrefix)(i.base_token.fields.name),quote_token:(0,Gt.addHexPrefix)(i.quote_token.fields.name),max_long_leverage:(0,Gt.d)(i.max_long_leverage).div(10**6).toString(),max_short_leverage:(0,Gt.d)(i.max_short_leverage).div(10**6).toString(),open_fee_rate:(0,Gt.d)(i.open_fee_rate).div(10**6).toString(),close_fee_rate:(0,Gt.d)(i.close_fee_rate).div(10**6).toString(),open_permissions_pause:r,close_permissions_pause:o,deposit_permissions_pause:t,withdraw_permissions_pause:a,borrow_permissions_pause:d,repay_permissions_pause:c}},Le=(n,i)=>{let r=(0,Gt.getObjectFields)(n);return{created_at:r.created_ts,position_id:r.id.id,init_deposit_amount:r.init_deposit_amount,is_long:r.is_long,lending_market_id:r.lending_market_id,obligation_owner_cap:r.obligation_owner_cap.fields.obligation_id,market_id:r.market_id,position_cap_id:i}},Ne=(n,i,r,o,t)=>{let a=(0,ir.parseObligation)(i,r),d=(0,se.getNetAprPercent)(a,t,o);if(!a.deposits||a.deposits.length===0)throw new Error("No deposits found in obligation data");let c=a.deposits[0],m=(0,Gt.d)(c.reserve.config.closeLtvPct).div(100).toString(),g=(0,Gt.d)(c.reserve.config.openLtvPct).div(100).toString(),M=a.depositedAmountUsd.gt(0)?a.borrowedAmountUsd.div(a.depositedAmountUsd).toString():"0",P=(0,Gt.d)(1).div(m).toString(),A=(0,Gt.d)(m).sub(M).toString(),T="0",O="0",N="0",I="0";if(a.borrows&&a.borrows.length>0){let L=a.borrows[0],R=t[L?.coinType];R&&(T=(0,se.getTotalAprPercent)(se.Side.BORROW,L?.reserve.borrowAprPercent,(0,se.getFilteredRewards)(R.borrow)).toString()),N=L.borrowedAmountUsd.toString(),I=L.borrowedAmount.toString()}if(a.deposits&&a.deposits.length>0){let L=a.deposits[0],R=t[L?.coinType];R&&(O=(0,se.getTotalAprPercent)(se.Side.DEPOSIT,L?.reserve.depositAprPercent,(0,se.getFilteredRewards)(R.deposit)).toString())}return{...n,...a,origin_obligation:i,net_apr_percent:d.toString(),supplied_apr:(0,Gt.d)(O).toString(),borrowed_apr:(0,Gt.d)(T).toString(),close_ltv:m,open_ltv:g,cr:M,ltr:P,liquidation_buffer:A,deposited_amount_usd:c.depositedAmountUsd.toString(),borrowed_amount_usd:N,deposited_amount:c.depositedAmount.toString(),borrowed_amount:I}},Ie=n=>{let i=n.length,r=n.substring(i-1,i)==="0",o=n.substring(i-2,i-1)==="0",t=n.substring(i-3,i-2)==="0",a=n.substring(i-4,i-3)==="0",d=n.substring(i-5,i-4)==="0",c=n.substring(i-6,i-5)==="0";return{open_permissions_pause:r,close_permissions_pause:o,deposit_permissions_pause:t,withdraw_permissions_pause:d,borrow_permissions_pause:a,repay_permissions_pause:c}};var ut=require("@suilend/sdk");var rr=require("@cetusprotocol/common-sdk");var Ee=class extends rr.BaseError{constructor(i,r,o){super(i,r||"UnknownError",o)}static isCrossSwapErrorCode(i,r){return this.isErrorCode(i,r)}},Yt=(n,i,r)=>{let o={...r,stack:i instanceof Error?i.stack:void 0};throw i instanceof Error?new Ee(i.message,n,o):new Ee(i,n,o)};var Be=class{constructor(i){this.createMarket=async i=>{let{package_id:r}=this._sdk.sdkOptions.margin_trading,{versioned_id:o,admin_cap_id:t,markets:a,global_config_id:d}=(0,Kt.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),c=new Ae.Transaction;return c.moveCall({target:`${r}::market::create_market`,arguments:[c.object(t),c.object(d),c.object(a),c.pure.u64(i.open_fee_rate),c.pure.u64(i.close_fee_rate),c.object(o)],typeArguments:[i.base_token,i.quote_token]}),c};this.updateMarketFeeRate=async i=>{let{package_id:r}=this._sdk.sdkOptions.margin_trading,{versioned_id:o,admin_cap_id:t}=(0,Kt.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),{market_id:a,open_fee_rate:d,close_fee_rate:c}=i,m=new Ae.Transaction;return m.moveCall({target:`${r}::market::set_fee_rate`,arguments:[m.object(t),m.object(a),m.pure.u64(d),m.pure.u64(c),m.object(o)],typeArguments:[]}),m};this.claimMarketFees=async(i,r)=>{let{package_id:o}=this._sdk.sdkOptions.margin_trading,{versioned_id:t,admin_cap_id:a}=(0,Kt.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),d=r||new Ae.Transaction,{base_token:c,quote_token:m}=await this._sdk.MarketModules.getMarketInfo(i);return d.moveCall({target:`${o}::market::claim_fees`,arguments:[d.object(a),d.object(i),d.object(t)],typeArguments:[c,m]}),d};this.claimAllMarketFees=async i=>{let r=new Ae.Transaction;for(let o=0;o<i.length;o++){let t=i[o];this.claimMarketFees(t,r)}return r};this.getMarginTradingConfig=async()=>{let{package_id:i}=this._sdk.sdkOptions.margin_trading;try{let r=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::versioned::InitEvent`}),o=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::admin_cap::InitEvent`}),t=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::config::InitEvent`}),d=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::market::InitEvent`})).data[0].parsedJson.markets_id,m=(await this._sdk.FullClient.getObject({id:d,options:{showContent:!0}})).data.content.fields.list.fields.id.id;return{versioned_id:r.data[0].parsedJson.versioned_id,admin_cap_id:o.data[0].parsedJson.admin_cap_id,global_config_id:t.data[0].parsedJson.global_config_id,markets:d,markets_table_id:m}}catch(r){return Yt("FetchError",r),{versioned_id:"",admin_cap_id:"",global_config_id:"",markets_table_id:"",markets:""}}};this.getMarketList=async(i=!1)=>{let o=this._sdk.getCache("margin_trading_markets_list",i);if(o)return o;let{package_id:t}=this._sdk.sdkOptions.margin_trading,a=[];try{let d=`${t}::market::CreateMarketEvent`,m=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:d})).data.map(g=>g.parsedJson.market_id);return m.length>0?((await this._sdk.FullClient.batchGetObjects(m,{showContent:!0,showType:!0})).forEach(M=>{let P=Re(M),A=`margin_trading_market_info_${P.market_id}`;this._sdk.updateCache(A,P),a.push(P)}),a):(Yt("MarketNotFound","Market not found"),[])}catch(d){return Yt("FetchError",d),[]}};this.getMarketInfo=async(i,r=!1)=>{let o=`margin_trading_market_info_${i}`,t=this._sdk.getCache(o,r);if(t)return t;try{let a=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0}}),d=Re(a);return this._sdk.updateCache(o,d,Kt.CACHE_TIME_5MIN),d}catch(a){return Yt("FetchError",a),{}}};this.getMarketSuilendInfo=async i=>{try{let{base_token:r,quote_token:o}=await this.getMarketInfo(i),{lending_market_id:t}=(0,Kt.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{allLendingMarketData:a,lstAprPercentMap:d}=await this._sdk.SuiLendModule.getLendingMarketData(!0),{base_reserve_map_info:c,quote_reserve_map_info:m}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(r,o,a);console.log("\u{1F680}\u{1F680}\u{1F680} ~ marketModules.ts:188 ~ MarketModules ~ base_reserve_map_info:",c);let g=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:a,lstAprPercentMap:d},!0),M=g[t].rewardMap[r],P=g[t].rewardMap[o],{depositedAmountUsd:A,borrowedAmountUsd:T,depositAprPercent:O,borrowAprPercent:N,depositedAmount:I,borrowedAmount:L}=c,{depositLimit:R,borrowLimit:E}=c.config,{depositedAmountUsd:q,borrowedAmountUsd:W,depositAprPercent:F,borrowAprPercent:Z,depositedAmount:_,borrowedAmount:b}=m,{depositLimit:S,borrowLimit:C}=m.config,x=(0,ut.getTotalAprPercent)(ut.Side.DEPOSIT,O,(0,ut.getFilteredRewards)(M.deposit),(0,ut.getStakingYieldAprPercent)(ut.Side.DEPOSIT,r,d)),p=(0,ut.getTotalAprPercent)(ut.Side.BORROW,N,(0,ut.getFilteredRewards)(M.borrow)),e=(0,ut.getTotalAprPercent)(ut.Side.DEPOSIT,F,(0,ut.getFilteredRewards)(P.deposit),(0,ut.getStakingYieldAprPercent)(ut.Side.DEPOSIT,r,d)),s=(0,ut.getTotalAprPercent)(ut.Side.BORROW,Z,(0,ut.getFilteredRewards)(P.borrow)),l=(0,Kt.d)(q.toString()).sub(W.toString()).toString(),f=(0,Kt.d)(A.toString()).sub(T.toString()).toString(),h=(0,Kt.d)(R.toString()).sub((0,Kt.d)(I.toString())).toString(),v=(0,Kt.d)(E.toString()).sub((0,Kt.d)(L.toString())).toString(),k=(0,Kt.d)(S.toString()).sub((0,Kt.d)(_.toString())).toString(),w=(0,Kt.d)(C.toString()).sub((0,Kt.d)(b.toString())).toString();return{long_liquidity:l,short_liquidity:f,base_total_deposit_apr_percent:x.toString(),base_total_borrow_apr_percent:p.toString(),quote_total_deposit_apr_percent:e.toString(),quote_total_borrow_apr_percent:s.toString(),base_token_available_deposit_amount:h,base_token_available_borrow_amount:v,quote_token_available_deposit_amount:k,quote_token_available_borrow_amount:w,base_deposit_rewards:(0,ut.getFilteredRewards)(M.deposit),quote_deposit_rewards:(0,ut.getFilteredRewards)(P.deposit),base_borrow_rewards:(0,ut.getFilteredRewards)(M.borrow),quote_borrow_rewards:(0,ut.getFilteredRewards)(P.borrow),base_deposit_apr_percent:O.toString(),base_borrow_apr_percent:N.toString(),quote_deposit_apr_percent:F.toString(),quote_borrow_apr_percent:Z.toString(),obligations:g[t],base_reserve_map_info:c,quote_reserve_map_info:m}}catch(r){return Yt("FetchError",r),{}}};this._sdk=i}};var mt=require("@cetusprotocol/common-sdk"),Qt=require("@mysten/sui/transactions");var qe=class{constructor(i){this.queryGlobalPermissions=async()=>{let i=this._sdk.sdkOptions.margin_trading,{global_config_id:r}=(0,mt.getPackagerConfigs)(i),o=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}});if(o){let t=o.data.content.fields.permissions.toString(2),{open_permissions_pause:a,close_permissions_pause:d,deposit_permissions_pause:c,withdraw_permissions_pause:m,borrow_permissions_pause:g,repay_permissions_pause:M}=Ie(t);return{open_permissions_pause:a,close_permissions_pause:d,deposit_permissions_pause:c,withdraw_permissions_pause:m,borrow_permissions_pause:g,repay_permissions_pause:M}}};this.updateGlobalPermissions=async i=>{let r=new Qt.Transaction;return this.updateGlobalOpenPositionPermissions(i,r),this.updateGlobalClosePositionPermissions(i,r),this.updateGlobalDepositPermissions(i,r),this.updateGlobalBorrowPermissions(i,r),this.updateGlobalWithdrawPermissions(i,r),this.updateGlobalRepayPermissions(i,r),r};this.updateGlobalOpenPositionPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_open_position_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateGlobalClosePositionPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_close_position_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateGlobalDepositPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_deposit_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateGlobalBorrowPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_borrow_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateGlobalWithdrawPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_withdraw_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateGlobalRepayPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(o),c=r||new Qt.Transaction;return c.moveCall({target:`${o.published_at}::config::update_global_repay_permissions`,arguments:[c.object(d),c.object(t),c.pure.bool(i),c.object(mt.CLOCK_ADDRESS),c.object(a)],typeArguments:[]}),c};this.updateMarketCreatePositionPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_open_position_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketClosePositionPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_close_position_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketDepositPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_deposit_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketWithdrawPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_withdraw_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketBorrowPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_borrow_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketRepayPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:d}=(0,mt.getPackagerConfigs)(t),c=o||new Qt.Transaction;return c.moveCall({target:`${t.published_at}::market::update_market_repay_permission`,arguments:[c.object(d),c.object(i),c.pure.bool(r),c.object(a)],typeArguments:[]}),c};this.updateMarketPermissions=async(i,r,o)=>{let t=o||new Qt.Transaction;return this.updateMarketCreatePositionPermissions(i,r,t),this.updateMarketClosePositionPermissions(i,r,t),this.updateMarketDepositPermissions(i,r,t),this.updateMarketWithdrawPermissions(i,r,t),this.updateMarketBorrowPermissions(i,r,t),this.updateMarketRepayPermissions(i,r,t),t};this._sdk=i}};var D=require("@cetusprotocol/common-sdk");var $r=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,Pi=Math.ceil,re=Math.floor,te="[BigNumber Error] ",nr=te+"Number primitive has more than 15 significant digits: ",ae=1e14,Q=14,Ai=9007199254740991,Ci=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],ge=1e7,zt=1e9;function or(n){var i,r,o,t=R.prototype={constructor:R,toString:null,valueOf:null},a=new R(1),d=20,c=4,m=-7,g=21,M=-1e7,P=1e7,A=!1,T=1,O=0,N={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:"\xA0",suffix:""},I="0123456789abcdefghijklmnopqrstuvwxyz",L=!0;function R(_,b){var S,C,x,p,e,s,l,f,h=this;if(!(h instanceof R))return new R(_,b);if(b==null){if(_&&_._isBigNumber===!0){h.s=_.s,!_.c||_.e>P?h.c=h.e=null:_.e<M?h.c=[h.e=0]:(h.e=_.e,h.c=_.c.slice());return}if((s=typeof _=="number")&&_*0==0){if(h.s=1/_<0?(_=-_,-1):1,_===~~_){for(p=0,e=_;e>=10;e/=10,p++);p>P?h.c=h.e=null:(h.e=p,h.c=[_]);return}f=String(_)}else{if(!$r.test(f=String(_)))return o(h,f,s);h.s=f.charCodeAt(0)==45?(f=f.slice(1),-1):1}(p=f.indexOf("."))>-1&&(f=f.replace(".","")),(e=f.search(/e/i))>0?(p<0&&(p=e),p+=+f.slice(e+1),f=f.substring(0,e)):p<0&&(p=f.length)}else{if(ht(b,2,I.length,"Base"),b==10&&L)return h=new R(_),F(h,d+h.e+1,c);if(f=String(_),s=typeof _=="number"){if(_*0!=0)return o(h,f,s,b);if(h.s=1/_<0?(f=f.slice(1),-1):1,R.DEBUG&&f.replace(/^0\.0*|\./,"").length>15)throw Error(nr+_)}else h.s=f.charCodeAt(0)===45?(f=f.slice(1),-1):1;for(S=I.slice(0,b),p=e=0,l=f.length;e<l;e++)if(S.indexOf(C=f.charAt(e))<0){if(C=="."){if(e>p){p=l;continue}}else if(!x&&(f==f.toUpperCase()&&(f=f.toLowerCase())||f==f.toLowerCase()&&(f=f.toUpperCase()))){x=!0,e=-1,p=0;continue}return o(h,String(_),s,b)}s=!1,f=r(f,b,10,h.s),(p=f.indexOf("."))>-1?f=f.replace(".",""):p=f.length}for(e=0;f.charCodeAt(e)===48;e++);for(l=f.length;f.charCodeAt(--l)===48;);if(f=f.slice(e,++l)){if(l-=e,s&&R.DEBUG&&l>15&&(_>Ai||_!==re(_)))throw Error(nr+h.s*_);if((p=p-e-1)>P)h.c=h.e=null;else if(p<M)h.c=[h.e=0];else{if(h.e=p,h.c=[],e=(p+1)%Q,p<0&&(e+=Q),e<l){for(e&&h.c.push(+f.slice(0,e)),l-=Q;e<l;)h.c.push(+f.slice(e,e+=Q));e=Q-(f=f.slice(e)).length}else e-=l;for(;e--;f+="0");h.c.push(+f)}}else h.c=[h.e=0]}R.clone=or,R.ROUND_UP=0,R.ROUND_DOWN=1,R.ROUND_CEIL=2,R.ROUND_FLOOR=3,R.ROUND_HALF_UP=4,R.ROUND_HALF_DOWN=5,R.ROUND_HALF_EVEN=6,R.ROUND_HALF_CEIL=7,R.ROUND_HALF_FLOOR=8,R.EUCLID=9,R.config=R.set=function(_){var b,S;if(_!=null)if(typeof _=="object"){if(_.hasOwnProperty(b="DECIMAL_PLACES")&&(S=_[b],ht(S,0,zt,b),d=S),_.hasOwnProperty(b="ROUNDING_MODE")&&(S=_[b],ht(S,0,8,b),c=S),_.hasOwnProperty(b="EXPONENTIAL_AT")&&(S=_[b],S&&S.pop?(ht(S[0],-zt,0,b),ht(S[1],0,zt,b),m=S[0],g=S[1]):(ht(S,-zt,zt,b),m=-(g=S<0?-S:S))),_.hasOwnProperty(b="RANGE"))if(S=_[b],S&&S.pop)ht(S[0],-zt,-1,b),ht(S[1],1,zt,b),M=S[0],P=S[1];else if(ht(S,-zt,zt,b),S)M=-(P=S<0?-S:S);else throw Error(te+b+" cannot be zero: "+S);if(_.hasOwnProperty(b="CRYPTO"))if(S=_[b],S===!!S)if(S)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))A=S;else throw A=!S,Error(te+"crypto unavailable");else A=S;else throw Error(te+b+" not true or false: "+S);if(_.hasOwnProperty(b="MODULO_MODE")&&(S=_[b],ht(S,0,9,b),T=S),_.hasOwnProperty(b="POW_PRECISION")&&(S=_[b],ht(S,0,zt,b),O=S),_.hasOwnProperty(b="FORMAT"))if(S=_[b],typeof S=="object")N=S;else throw Error(te+b+" not an object: "+S);if(_.hasOwnProperty(b="ALPHABET"))if(S=_[b],typeof S=="string"&&!/^.?$|[+\-.\s]|(.).*\1/.test(S))L=S.slice(0,10)=="0123456789",I=S;else throw Error(te+b+" invalid: "+S)}else throw Error(te+"Object expected: "+_);return{DECIMAL_PLACES:d,ROUNDING_MODE:c,EXPONENTIAL_AT:[m,g],RANGE:[M,P],CRYPTO:A,MODULO_MODE:T,POW_PRECISION:O,FORMAT:N,ALPHABET:I}},R.isBigNumber=function(_){if(!_||_._isBigNumber!==!0)return!1;if(!R.DEBUG)return!0;var b,S,C=_.c,x=_.e,p=_.s;t:if({}.toString.call(C)=="[object Array]"){if((p===1||p===-1)&&x>=-zt&&x<=zt&&x===re(x)){if(C[0]===0){if(x===0&&C.length===1)return!0;break t}if(b=(x+1)%Q,b<1&&(b+=Q),String(C[0]).length==b){for(b=0;b<C.length;b++)if(S=C[b],S<0||S>=ae||S!==re(S))break t;if(S!==0)return!0}}}else if(C===null&&x===null&&(p===null||p===1||p===-1))return!0;throw Error(te+"Invalid BigNumber: "+_)},R.maximum=R.max=function(){return q(arguments,-1)},R.minimum=R.min=function(){return q(arguments,1)},R.random=(function(){var _=9007199254740992,b=Math.random()*_&2097151?function(){return re(Math.random()*_)}:function(){return(Math.random()*1073741824|0)*8388608+(Math.random()*8388608|0)};return function(S){var C,x,p,e,s,l=0,f=[],h=new R(a);if(S==null?S=d:ht(S,0,zt),e=Pi(S/Q),A)if(crypto.getRandomValues){for(C=crypto.getRandomValues(new Uint32Array(e*=2));l<e;)s=C[l]*131072+(C[l+1]>>>11),s>=9e15?(x=crypto.getRandomValues(new Uint32Array(2)),C[l]=x[0],C[l+1]=x[1]):(f.push(s%1e14),l+=2);l=e/2}else if(crypto.randomBytes){for(C=crypto.randomBytes(e*=7);l<e;)s=(C[l]&31)*281474976710656+C[l+1]*1099511627776+C[l+2]*4294967296+C[l+3]*16777216+(C[l+4]<<16)+(C[l+5]<<8)+C[l+6],s>=9e15?crypto.randomBytes(7).copy(C,l):(f.push(s%1e14),l+=7);l=e/7}else throw A=!1,Error(te+"crypto unavailable");if(!A)for(;l<e;)s=b(),s<9e15&&(f[l++]=s%1e14);for(e=f[--l],S%=Q,e&&S&&(s=Ci[Q-S],f[l]=re(e/s)*s);f[l]===0;f.pop(),l--);if(l<0)f=[p=0];else{for(p=-1;f[0]===0;f.splice(0,1),p-=Q);for(l=1,s=f[0];s>=10;s/=10,l++);l<Q&&(p-=Q-l)}return h.e=p,h.c=f,h}})(),R.sum=function(){for(var _=1,b=arguments,S=new R(b[0]);_<b.length;)S=S.plus(b[_++]);return S},r=(function(){var _="0123456789";function b(S,C,x,p){for(var e,s=[0],l,f=0,h=S.length;f<h;){for(l=s.length;l--;s[l]*=C);for(s[0]+=p.indexOf(S.charAt(f++)),e=0;e<s.length;e++)s[e]>x-1&&(s[e+1]==null&&(s[e+1]=0),s[e+1]+=s[e]/x|0,s[e]%=x)}return s.reverse()}return function(S,C,x,p,e){var s,l,f,h,v,k,w,u,y=S.indexOf("."),$=d,U=c;for(y>=0&&(h=O,O=0,S=S.replace(".",""),u=new R(C),k=u.pow(S.length-y),O=h,u.c=b(he(ie(k.c),k.e,"0"),10,x,_),u.e=u.c.length),w=b(S,C,x,e?(s=I,_):(s=_,I)),f=h=w.length;w[--h]==0;w.pop());if(!w[0])return s.charAt(0);if(y<0?--f:(k.c=w,k.e=f,k.s=p,k=i(k,u,$,U,x),w=k.c,v=k.r,f=k.e),l=f+$+1,y=w[l],h=x/2,v=v||l<0||w[l+1]!=null,v=U<4?(y!=null||v)&&(U==0||U==(k.s<0?3:2)):y>h||y==h&&(U==4||v||U==6&&w[l-1]&1||U==(k.s<0?8:7)),l<1||!w[0])S=v?he(s.charAt(1),-$,s.charAt(0)):s.charAt(0);else{if(w.length=l,v)for(--x;++w[--l]>x;)w[l]=0,l||(++f,w=[1].concat(w));for(h=w.length;!w[--h];);for(y=0,S="";y<=h;S+=s.charAt(w[y++]));S=he(S,f,s.charAt(0))}return S}})(),i=(function(){function _(C,x,p){var e,s,l,f,h=0,v=C.length,k=x%ge,w=x/ge|0;for(C=C.slice();v--;)l=C[v]%ge,f=C[v]/ge|0,e=w*l+f*k,s=k*l+e%ge*ge+h,h=(s/p|0)+(e/ge|0)+w*f,C[v]=s%p;return h&&(C=[h].concat(C)),C}function b(C,x,p,e){var s,l;if(p!=e)l=p>e?1:-1;else for(s=l=0;s<p;s++)if(C[s]!=x[s]){l=C[s]>x[s]?1:-1;break}return l}function S(C,x,p,e){for(var s=0;p--;)C[p]-=s,s=C[p]<x[p]?1:0,C[p]=s*e+C[p]-x[p];for(;!C[0]&&C.length>1;C.splice(0,1));}return function(C,x,p,e,s){var l,f,h,v,k,w,u,y,$,U,j,K,G,J,rt,V,et,ft=C.s==x.s?1:-1,tt=C.c,X=x.c;if(!tt||!tt[0]||!X||!X[0])return new R(!C.s||!x.s||(tt?X&&tt[0]==X[0]:!X)?NaN:tt&&tt[0]==0||!X?ft*0:ft/0);for(y=new R(ft),$=y.c=[],f=C.e-x.e,ft=p+f+1,s||(s=ae,f=ne(C.e/Q)-ne(x.e/Q),ft=ft/Q|0),h=0;X[h]==(tt[h]||0);h++);if(X[h]>(tt[h]||0)&&f--,ft<0)$.push(1),v=!0;else{for(J=tt.length,V=X.length,h=0,ft+=2,k=re(s/(X[0]+1)),k>1&&(X=_(X,k,s),tt=_(tt,k,s),V=X.length,J=tt.length),G=V,U=tt.slice(0,V),j=U.length;j<V;U[j++]=0);et=X.slice(),et=[0].concat(et),rt=X[0],X[1]>=s/2&&rt++;do{if(k=0,l=b(X,U,V,j),l<0){if(K=U[0],V!=j&&(K=K*s+(U[1]||0)),k=re(K/rt),k>1)for(k>=s&&(k=s-1),w=_(X,k,s),u=w.length,j=U.length;b(w,U,u,j)==1;)k--,S(w,V<u?et:X,u,s),u=w.length,l=1;else k==0&&(l=k=1),w=X.slice(),u=w.length;if(u<j&&(w=[0].concat(w)),S(U,w,j,s),j=U.length,l==-1)for(;b(X,U,V,j)<1;)k++,S(U,V<j?et:X,j,s),j=U.length}else l===0&&(k++,U=[0]);$[h++]=k,U[0]?U[j++]=tt[G]||0:(U=[tt[G]],j=1)}while((G++<J||U[0]!=null)&&ft--);v=U[0]!=null,$[0]||$.splice(0,1)}if(s==ae){for(h=1,ft=$[0];ft>=10;ft/=10,h++);F(y,p+(y.e=h+f*Q-1)+1,e,v)}else y.e=f,y.r=+v;return y}})();function E(_,b,S,C){var x,p,e,s,l;if(S==null?S=c:ht(S,0,8),!_.c)return _.toString();if(x=_.c[0],e=_.e,b==null)l=ie(_.c),l=C==1||C==2&&(e<=m||e>=g)?Ue(l,e):he(l,e,"0");else if(_=F(new R(_),b,S),p=_.e,l=ie(_.c),s=l.length,C==1||C==2&&(b<=p||p<=m)){for(;s<b;l+="0",s++);l=Ue(l,p)}else if(b-=e+(C===2&&p>e),l=he(l,p,"0"),p+1>s){if(--b>0)for(l+=".";b--;l+="0");}else if(b+=p-s,b>0)for(p+1==s&&(l+=".");b--;l+="0");return _.s<0&&x?"-"+l:l}function q(_,b){for(var S,C,x=1,p=new R(_[0]);x<_.length;x++)C=new R(_[x]),(!C.s||(S=ye(p,C))===b||S===0&&p.s===b)&&(p=C);return p}function W(_,b,S){for(var C=1,x=b.length;!b[--x];b.pop());for(x=b[0];x>=10;x/=10,C++);return(S=C+S*Q-1)>P?_.c=_.e=null:S<M?_.c=[_.e=0]:(_.e=S,_.c=b),_}o=(function(){var _=/^(-?)0([xbo])(?=\w[\w.]*$)/i,b=/^([^.]+)\.$/,S=/^\.([^.]+)$/,C=/^-?(Infinity|NaN)$/,x=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(p,e,s,l){var f,h=s?e:e.replace(x,"");if(C.test(h))p.s=isNaN(h)?null:h<0?-1:1;else{if(!s&&(h=h.replace(_,function(v,k,w){return f=(w=w.toLowerCase())=="x"?16:w=="b"?2:8,!l||l==f?k:v}),l&&(f=l,h=h.replace(b,"$1").replace(S,"0.$1")),e!=h))return new R(h,f);if(R.DEBUG)throw Error(te+"Not a"+(l?" base "+l:"")+" number: "+e);p.s=null}p.c=p.e=null}})();function F(_,b,S,C){var x,p,e,s,l,f,h,v=_.c,k=Ci;if(v){t:{for(x=1,s=v[0];s>=10;s/=10,x++);if(p=b-x,p<0)p+=Q,e=b,l=v[f=0],h=re(l/k[x-e-1]%10);else if(f=Pi((p+1)/Q),f>=v.length)if(C){for(;v.length<=f;v.push(0));l=h=0,x=1,p%=Q,e=p-Q+1}else break t;else{for(l=s=v[f],x=1;s>=10;s/=10,x++);p%=Q,e=p-Q+x,h=e<0?0:re(l/k[x-e-1]%10)}if(C=C||b<0||v[f+1]!=null||(e<0?l:l%k[x-e-1]),C=S<4?(h||C)&&(S==0||S==(_.s<0?3:2)):h>5||h==5&&(S==4||C||S==6&&(p>0?e>0?l/k[x-e]:0:v[f-1])%10&1||S==(_.s<0?8:7)),b<1||!v[0])return v.length=0,C?(b-=_.e+1,v[0]=k[(Q-b%Q)%Q],_.e=-b||0):v[0]=_.e=0,_;if(p==0?(v.length=f,s=1,f--):(v.length=f+1,s=k[Q-p],v[f]=e>0?re(l/k[x-e]%k[e])*s:0),C)for(;;)if(f==0){for(p=1,e=v[0];e>=10;e/=10,p++);for(e=v[0]+=s,s=1;e>=10;e/=10,s++);p!=s&&(_.e++,v[0]==ae&&(v[0]=1));break}else{if(v[f]+=s,v[f]!=ae)break;v[f--]=0,s=1}for(p=v.length;v[--p]===0;v.pop());}_.e>P?_.c=_.e=null:_.e<M&&(_.c=[_.e=0])}return _}function Z(_){var b,S=_.e;return S===null?_.toString():(b=ie(_.c),b=S<=m||S>=g?Ue(b,S):he(b,S,"0"),_.s<0?"-"+b:b)}return t.absoluteValue=t.abs=function(){var _=new R(this);return _.s<0&&(_.s=1),_},t.comparedTo=function(_,b){return ye(this,new R(_,b))},t.decimalPlaces=t.dp=function(_,b){var S,C,x,p=this;if(_!=null)return ht(_,0,zt),b==null?b=c:ht(b,0,8),F(new R(p),_+p.e+1,b);if(!(S=p.c))return null;if(C=((x=S.length-1)-ne(this.e/Q))*Q,x=S[x])for(;x%10==0;x/=10,C--);return C<0&&(C=0),C},t.dividedBy=t.div=function(_,b){return i(this,new R(_,b),d,c)},t.dividedToIntegerBy=t.idiv=function(_,b){return i(this,new R(_,b),0,1)},t.exponentiatedBy=t.pow=function(_,b){var S,C,x,p,e,s,l,f,h,v=this;if(_=new R(_),_.c&&!_.isInteger())throw Error(te+"Exponent not an integer: "+Z(_));if(b!=null&&(b=new R(b)),s=_.e>14,!v.c||!v.c[0]||v.c[0]==1&&!v.e&&v.c.length==1||!_.c||!_.c[0])return h=new R(Math.pow(+Z(v),s?_.s*(2-Fe(_)):+Z(_))),b?h.mod(b):h;if(l=_.s<0,b){if(b.c?!b.c[0]:!b.s)return new R(NaN);C=!l&&v.isInteger()&&b.isInteger(),C&&(v=v.mod(b))}else{if(_.e>9&&(v.e>0||v.e<-1||(v.e==0?v.c[0]>1||s&&v.c[1]>=24e7:v.c[0]<8e13||s&&v.c[0]<=9999975e7)))return p=v.s<0&&Fe(_)?-0:0,v.e>-1&&(p=1/p),new R(l?1/p:p);O&&(p=Pi(O/Q+2))}for(s?(S=new R(.5),l&&(_.s=1),f=Fe(_)):(x=Math.abs(+Z(_)),f=x%2),h=new R(a);;){if(f){if(h=h.times(v),!h.c)break;p?h.c.length>p&&(h.c.length=p):C&&(h=h.mod(b))}if(x){if(x=re(x/2),x===0)break;f=x%2}else if(_=_.times(S),F(_,_.e+1,1),_.e>14)f=Fe(_);else{if(x=+Z(_),x===0)break;f=x%2}v=v.times(v),p?v.c&&v.c.length>p&&(v.c.length=p):C&&(v=v.mod(b))}return C?h:(l&&(h=a.div(h)),b?h.mod(b):p?F(h,O,c,e):h)},t.integerValue=function(_){var b=new R(this);return _==null?_=c:ht(_,0,8),F(b,b.e+1,_)},t.isEqualTo=t.eq=function(_,b){return ye(this,new R(_,b))===0},t.isFinite=function(){return!!this.c},t.isGreaterThan=t.gt=function(_,b){return ye(this,new R(_,b))>0},t.isGreaterThanOrEqualTo=t.gte=function(_,b){return(b=ye(this,new R(_,b)))===1||b===0},t.isInteger=function(){return!!this.c&&ne(this.e/Q)>this.c.length-2},t.isLessThan=t.lt=function(_,b){return ye(this,new R(_,b))<0},t.isLessThanOrEqualTo=t.lte=function(_,b){return(b=ye(this,new R(_,b)))===-1||b===0},t.isNaN=function(){return!this.s},t.isNegative=function(){return this.s<0},t.isPositive=function(){return this.s>0},t.isZero=function(){return!!this.c&&this.c[0]==0},t.minus=function(_,b){var S,C,x,p,e=this,s=e.s;if(_=new R(_,b),b=_.s,!s||!b)return new R(NaN);if(s!=b)return _.s=-b,e.plus(_);var l=e.e/Q,f=_.e/Q,h=e.c,v=_.c;if(!l||!f){if(!h||!v)return h?(_.s=-b,_):new R(v?e:NaN);if(!h[0]||!v[0])return v[0]?(_.s=-b,_):new R(h[0]?e:c==3?-0:0)}if(l=ne(l),f=ne(f),h=h.slice(),s=l-f){for((p=s<0)?(s=-s,x=h):(f=l,x=v),x.reverse(),b=s;b--;x.push(0));x.reverse()}else for(C=(p=(s=h.length)<(b=v.length))?s:b,s=b=0;b<C;b++)if(h[b]!=v[b]){p=h[b]<v[b];break}if(p&&(x=h,h=v,v=x,_.s=-_.s),b=(C=v.length)-(S=h.length),b>0)for(;b--;h[S++]=0);for(b=ae-1;C>s;){if(h[--C]<v[C]){for(S=C;S&&!h[--S];h[S]=b);--h[S],h[C]+=ae}h[C]-=v[C]}for(;h[0]==0;h.splice(0,1),--f);return h[0]?W(_,h,f):(_.s=c==3?-1:1,_.c=[_.e=0],_)},t.modulo=t.mod=function(_,b){var S,C,x=this;return _=new R(_,b),!x.c||!_.s||_.c&&!_.c[0]?new R(NaN):!_.c||x.c&&!x.c[0]?new R(x):(T==9?(C=_.s,_.s=1,S=i(x,_,0,3),_.s=C,S.s*=C):S=i(x,_,0,T),_=x.minus(S.times(_)),!_.c[0]&&T==1&&(_.s=x.s),_)},t.multipliedBy=t.times=function(_,b){var S,C,x,p,e,s,l,f,h,v,k,w,u,y,$,U=this,j=U.c,K=(_=new R(_,b)).c;if(!j||!K||!j[0]||!K[0])return!U.s||!_.s||j&&!j[0]&&!K||K&&!K[0]&&!j?_.c=_.e=_.s=null:(_.s*=U.s,!j||!K?_.c=_.e=null:(_.c=[0],_.e=0)),_;for(C=ne(U.e/Q)+ne(_.e/Q),_.s*=U.s,l=j.length,v=K.length,l<v&&(u=j,j=K,K=u,x=l,l=v,v=x),x=l+v,u=[];x--;u.push(0));for(y=ae,$=ge,x=v;--x>=0;){for(S=0,k=K[x]%$,w=K[x]/$|0,e=l,p=x+e;p>x;)f=j[--e]%$,h=j[e]/$|0,s=w*f+h*k,f=k*f+s%$*$+u[p]+S,S=(f/y|0)+(s/$|0)+w*h,u[p--]=f%y;u[p]=S}return S?++C:u.splice(0,1),W(_,u,C)},t.negated=function(){var _=new R(this);return _.s=-_.s||null,_},t.plus=function(_,b){var S,C=this,x=C.s;if(_=new R(_,b),b=_.s,!x||!b)return new R(NaN);if(x!=b)return _.s=-b,C.minus(_);var p=C.e/Q,e=_.e/Q,s=C.c,l=_.c;if(!p||!e){if(!s||!l)return new R(x/0);if(!s[0]||!l[0])return l[0]?_:new R(s[0]?C:x*0)}if(p=ne(p),e=ne(e),s=s.slice(),x=p-e){for(x>0?(e=p,S=l):(x=-x,S=s),S.reverse();x--;S.push(0));S.reverse()}for(x=s.length,b=l.length,x-b<0&&(S=l,l=s,s=S,b=x),x=0;b;)x=(s[--b]=s[b]+l[b]+x)/ae|0,s[b]=ae===s[b]?0:s[b]%ae;return x&&(s=[x].concat(s),++e),W(_,s,e)},t.precision=t.sd=function(_,b){var S,C,x,p=this;if(_!=null&&_!==!!_)return ht(_,1,zt),b==null?b=c:ht(b,0,8),F(new R(p),_,b);if(!(S=p.c))return null;if(x=S.length-1,C=x*Q+1,x=S[x]){for(;x%10==0;x/=10,C--);for(x=S[0];x>=10;x/=10,C++);}return _&&p.e+1>C&&(C=p.e+1),C},t.shiftedBy=function(_){return ht(_,-Ai,Ai),this.times("1e"+_)},t.squareRoot=t.sqrt=function(){var _,b,S,C,x,p=this,e=p.c,s=p.s,l=p.e,f=d+4,h=new R("0.5");if(s!==1||!e||!e[0])return new R(!s||s<0&&(!e||e[0])?NaN:e?p:1/0);if(s=Math.sqrt(+Z(p)),s==0||s==1/0?(b=ie(e),(b.length+l)%2==0&&(b+="0"),s=Math.sqrt(+b),l=ne((l+1)/2)-(l<0||l%2),s==1/0?b="5e"+l:(b=s.toExponential(),b=b.slice(0,b.indexOf("e")+1)+l),S=new R(b)):S=new R(s+""),S.c[0]){for(l=S.e,s=l+f,s<3&&(s=0);;)if(x=S,S=h.times(x.plus(i(p,x,f,1))),ie(x.c).slice(0,s)===(b=ie(S.c)).slice(0,s))if(S.e<l&&--s,b=b.slice(s-3,s+1),b=="9999"||!C&&b=="4999"){if(!C&&(F(x,x.e+d+2,0),x.times(x).eq(p))){S=x;break}f+=4,s+=4,C=1}else{(!+b||!+b.slice(1)&&b.charAt(0)=="5")&&(F(S,S.e+d+2,1),_=!S.times(S).eq(p));break}}return F(S,S.e+d+1,c,_)},t.toExponential=function(_,b){return _!=null&&(ht(_,0,zt),_++),E(this,_,b,1)},t.toFixed=function(_,b){return _!=null&&(ht(_,0,zt),_=_+this.e+1),E(this,_,b)},t.toFormat=function(_,b,S){var C,x=this;if(S==null)_!=null&&b&&typeof b=="object"?(S=b,b=null):_&&typeof _=="object"?(S=_,_=b=null):S=N;else if(typeof S!="object")throw Error(te+"Argument not an object: "+S);if(C=x.toFixed(_,b),x.c){var p,e=C.split("."),s=+S.groupSize,l=+S.secondaryGroupSize,f=S.groupSeparator||"",h=e[0],v=e[1],k=x.s<0,w=k?h.slice(1):h,u=w.length;if(l&&(p=s,s=l,l=p,u-=p),s>0&&u>0){for(p=u%s||s,h=w.substr(0,p);p<u;p+=s)h+=f+w.substr(p,s);l>0&&(h+=f+w.slice(p)),k&&(h="-"+h)}C=v?h+(S.decimalSeparator||"")+((l=+S.fractionGroupSize)?v.replace(new RegExp("\\d{"+l+"}\\B","g"),"$&"+(S.fractionGroupSeparator||"")):v):h}return(S.prefix||"")+C+(S.suffix||"")},t.toFraction=function(_){var b,S,C,x,p,e,s,l,f,h,v,k,w=this,u=w.c;if(_!=null&&(s=new R(_),!s.isInteger()&&(s.c||s.s!==1)||s.lt(a)))throw Error(te+"Argument "+(s.isInteger()?"out of range: ":"not an integer: ")+Z(s));if(!u)return new R(w);for(b=new R(a),f=S=new R(a),C=l=new R(a),k=ie(u),p=b.e=k.length-w.e-1,b.c[0]=Ci[(e=p%Q)<0?Q+e:e],_=!_||s.comparedTo(b)>0?p>0?b:f:s,e=P,P=1/0,s=new R(k),l.c[0]=0;h=i(s,b,0,1),x=S.plus(h.times(C)),x.comparedTo(_)!=1;)S=C,C=x,f=l.plus(h.times(x=f)),l=x,b=s.minus(h.times(x=b)),s=x;return x=i(_.minus(S),C,0,1),l=l.plus(x.times(f)),S=S.plus(x.times(C)),l.s=f.s=w.s,p=p*2,v=i(f,C,p,c).minus(w).abs().comparedTo(i(l,S,p,c).minus(w).abs())<1?[f,C]:[l,S],P=e,v},t.toNumber=function(){return+Z(this)},t.toPrecision=function(_,b){return _!=null&&ht(_,1,zt),E(this,_,b,2)},t.toString=function(_){var b,S=this,C=S.s,x=S.e;return x===null?C?(b="Infinity",C<0&&(b="-"+b)):b="NaN":(_==null?b=x<=m||x>=g?Ue(ie(S.c),x):he(ie(S.c),x,"0"):_===10&&L?(S=F(new R(S),d+x+1,c),b=he(ie(S.c),S.e,"0")):(ht(_,2,I.length,"Base"),b=r(he(ie(S.c),x,"0"),10,_,C,!0)),C<0&&S.c[0]&&(b="-"+b)),b},t.valueOf=t.toJSON=function(){return Z(this)},t._isBigNumber=!0,t[Symbol.toStringTag]="BigNumber",t[Symbol.for("nodejs.util.inspect.custom")]=t.valueOf,n!=null&&R.set(n),R}function ne(n){var i=n|0;return n>0||n===i?i:i-1}function ie(n){for(var i,r,o=1,t=n.length,a=n[0]+"";o<t;){for(i=n[o++]+"",r=Q-i.length;r--;i="0"+i);a+=i}for(t=a.length;a.charCodeAt(--t)===48;);return a.slice(0,t+1||1)}function ye(n,i){var r,o,t=n.c,a=i.c,d=n.s,c=i.s,m=n.e,g=i.e;if(!d||!c)return null;if(r=t&&!t[0],o=a&&!a[0],r||o)return r?o?0:-c:d;if(d!=c)return d;if(r=d<0,o=m==g,!t||!a)return o?0:!t^r?1:-1;if(!o)return m>g^r?1:-1;for(c=(m=t.length)<(g=a.length)?m:g,d=0;d<c;d++)if(t[d]!=a[d])return t[d]>a[d]^r?1:-1;return m==g?0:m>g^r?1:-1}function ht(n,i,r,o){if(n<i||n>r||n!==re(n))throw Error(te+(o||"Argument")+(typeof n=="number"?n<i||n>r?" out of range: ":" not an integer: ":" not a primitive number: ")+String(n))}function Fe(n){var i=n.c.length-1;return ne(n.e/Q)==i&&n.c[i]%2!=0}function Ue(n,i){return(n.length>1?n.charAt(0)+"."+n.slice(1):n)+(i<0?"e":"e+")+i}function he(n,i,r){var o,t;if(i<0){for(t=r+".";++i;t+=r);n=t+n}else if(o=n.length,++i>o){for(t=r,i-=o;--i;t+=r);n+=t}else i<o&&(n=n.slice(0,i)+"."+n.slice(i));return n}var Wr=or(),ct=Wr;var oe=require("@mysten/sui/transactions");var Se=9e15,Me=1e9,xi="0123456789abcdef",We="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",Ze="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Di={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Se,maxE:Se,crypto:!1},ur,pe,Y=!0,ze="[DecimalError] ",ve=ze+"Invalid argument: ",cr=ze+"Precision limit exceeded",fr=ze+"crypto unavailable",dr="[object Decimal]",Xt=Math.floor,Zt=Math.pow,Zr=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Kr=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,zr=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,hr=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,le=1e7,H=7,Gr=9007199254740991,Hr=We.length-1,Ti=Ze.length-1,B={toStringTag:dr};B.absoluteValue=B.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),z(n)};B.ceil=function(){return z(new this.constructor(this),this.e+1,2)};B.clampedTo=B.clamp=function(n,i){var r,o=this,t=o.constructor;if(n=new t(n),i=new t(i),!n.s||!i.s)return new t(NaN);if(n.gt(i))throw Error(ve+i);return r=o.cmp(n),r<0?n:o.cmp(i)>0?i:new t(o)};B.comparedTo=B.cmp=function(n){var i,r,o,t,a=this,d=a.d,c=(n=new a.constructor(n)).d,m=a.s,g=n.s;if(!d||!c)return!m||!g?NaN:m!==g?m:d===c?0:!d^m<0?1:-1;if(!d[0]||!c[0])return d[0]?m:c[0]?-g:0;if(m!==g)return m;if(a.e!==n.e)return a.e>n.e^m<0?1:-1;for(o=d.length,t=c.length,i=0,r=o<t?o:t;i<r;++i)if(d[i]!==c[i])return d[i]>c[i]^m<0?1:-1;return o===t?0:o>t^m<0?1:-1};B.cosine=B.cos=function(){var n,i,r=this,o=r.constructor;return r.d?r.d[0]?(n=o.precision,i=o.rounding,o.precision=n+Math.max(r.e,r.sd())+H,o.rounding=1,r=Vr(o,wr(o,r)),o.precision=n,o.rounding=i,z(pe==2||pe==3?r.neg():r,n,i,!0)):new o(1):new o(NaN)};B.cubeRoot=B.cbrt=function(){var n,i,r,o,t,a,d,c,m,g,M=this,P=M.constructor;if(!M.isFinite()||M.isZero())return new P(M);for(Y=!1,a=M.s*Zt(M.s*M,1/3),!a||Math.abs(a)==1/0?(r=Ht(M.d),n=M.e,(a=(n-r.length+1)%3)&&(r+=a==1||a==-2?"0":"00"),a=Zt(r,1/3),n=Xt((n+1)/3)-(n%3==(n<0?-1:2)),a==1/0?r="5e"+n:(r=a.toExponential(),r=r.slice(0,r.indexOf("e")+1)+n),o=new P(r),o.s=M.s):o=new P(a.toString()),d=(n=P.precision)+3;;)if(c=o,m=c.times(c).times(c),g=m.plus(M),o=st(g.plus(M).times(c),g.plus(m),d+2,1),Ht(c.d).slice(0,d)===(r=Ht(o.d)).slice(0,d))if(r=r.slice(d-3,d+1),r=="9999"||!t&&r=="4999"){if(!t&&(z(c,n+1,0),c.times(c).times(c).eq(M))){o=c;break}d+=4,t=1}else{(!+r||!+r.slice(1)&&r.charAt(0)=="5")&&(z(o,n+1,1),i=!o.times(o).times(o).eq(M));break}return Y=!0,z(o,n,P.rounding,i)};B.decimalPlaces=B.dp=function(){var n,i=this.d,r=NaN;if(i){if(n=i.length-1,r=(n-Xt(this.e/H))*H,n=i[n],n)for(;n%10==0;n/=10)r--;r<0&&(r=0)}return r};B.dividedBy=B.div=function(n){return st(this,new this.constructor(n))};B.dividedToIntegerBy=B.divToInt=function(n){var i=this,r=i.constructor;return z(st(i,new r(n),0,1,1),r.precision,r.rounding)};B.equals=B.eq=function(n){return this.cmp(n)===0};B.floor=function(){return z(new this.constructor(this),this.e+1,3)};B.greaterThan=B.gt=function(n){return this.cmp(n)>0};B.greaterThanOrEqualTo=B.gte=function(n){var i=this.cmp(n);return i==1||i===0};B.hyperbolicCosine=B.cosh=function(){var n,i,r,o,t,a=this,d=a.constructor,c=new d(1);if(!a.isFinite())return new d(a.s?1/0:NaN);if(a.isZero())return c;r=d.precision,o=d.rounding,d.precision=r+Math.max(a.e,a.sd())+4,d.rounding=1,t=a.d.length,t<32?(n=Math.ceil(t/3),i=(1/He(4,n)).toString()):(n=16,i="2.3283064365386962890625e-10"),a=Pe(d,1,a.times(i),new d(1),!0);for(var m,g=n,M=new d(8);g--;)m=a.times(a),a=c.minus(m.times(M.minus(m.times(M))));return z(a,d.precision=r,d.rounding=o,!0)};B.hyperbolicSine=B.sinh=function(){var n,i,r,o,t=this,a=t.constructor;if(!t.isFinite()||t.isZero())return new a(t);if(i=a.precision,r=a.rounding,a.precision=i+Math.max(t.e,t.sd())+4,a.rounding=1,o=t.d.length,o<3)t=Pe(a,2,t,t,!0);else{n=1.4*Math.sqrt(o),n=n>16?16:n|0,t=t.times(1/He(5,n)),t=Pe(a,2,t,t,!0);for(var d,c=new a(5),m=new a(16),g=new a(20);n--;)d=t.times(t),t=t.times(c.plus(d.times(m.times(d).plus(g))))}return a.precision=i,a.rounding=r,z(t,i,r,!0)};B.hyperbolicTangent=B.tanh=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+7,o.rounding=1,st(r.sinh(),r.cosh(),o.precision=n,o.rounding=i)):new o(r.s)};B.inverseCosine=B.acos=function(){var n=this,i=n.constructor,r=n.abs().cmp(1),o=i.precision,t=i.rounding;return r!==-1?r===0?n.isNeg()?ue(i,o,t):new i(0):new i(NaN):n.isZero()?ue(i,o+4,t).times(.5):(i.precision=o+6,i.rounding=1,n=new i(1).minus(n).div(n.plus(1)).sqrt().atan(),i.precision=o,i.rounding=t,n.times(2))};B.inverseHyperbolicCosine=B.acosh=function(){var n,i,r=this,o=r.constructor;return r.lte(1)?new o(r.eq(1)?0:NaN):r.isFinite()?(n=o.precision,i=o.rounding,o.precision=n+Math.max(Math.abs(r.e),r.sd())+4,o.rounding=1,Y=!1,r=r.times(r).minus(1).sqrt().plus(r),Y=!0,o.precision=n,o.rounding=i,r.ln()):new o(r)};B.inverseHyperbolicSine=B.asinh=function(){var n,i,r=this,o=r.constructor;return!r.isFinite()||r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+2*Math.max(Math.abs(r.e),r.sd())+6,o.rounding=1,Y=!1,r=r.times(r).plus(1).sqrt().plus(r),Y=!0,o.precision=n,o.rounding=i,r.ln())};B.inverseHyperbolicTangent=B.atanh=function(){var n,i,r,o,t=this,a=t.constructor;return t.isFinite()?t.e>=0?new a(t.abs().eq(1)?t.s/0:t.isZero()?t:NaN):(n=a.precision,i=a.rounding,o=t.sd(),Math.max(o,n)<2*-t.e-1?z(new a(t),n,i,!0):(a.precision=r=o-t.e,t=st(t.plus(1),new a(1).minus(t),r+n,1),a.precision=n+4,a.rounding=1,t=t.ln(),a.precision=n,a.rounding=i,t.times(.5))):new a(NaN)};B.inverseSine=B.asin=function(){var n,i,r,o,t=this,a=t.constructor;return t.isZero()?new a(t):(i=t.abs().cmp(1),r=a.precision,o=a.rounding,i!==-1?i===0?(n=ue(a,r+4,o).times(.5),n.s=t.s,n):new a(NaN):(a.precision=r+6,a.rounding=1,t=t.div(new a(1).minus(t.times(t)).sqrt().plus(1)).atan(),a.precision=r,a.rounding=o,t.times(2)))};B.inverseTangent=B.atan=function(){var n,i,r,o,t,a,d,c,m,g=this,M=g.constructor,P=M.precision,A=M.rounding;if(g.isFinite()){if(g.isZero())return new M(g);if(g.abs().eq(1)&&P+4<=Ti)return d=ue(M,P+4,A).times(.25),d.s=g.s,d}else{if(!g.s)return new M(NaN);if(P+4<=Ti)return d=ue(M,P+4,A).times(.5),d.s=g.s,d}for(M.precision=c=P+10,M.rounding=1,r=Math.min(28,c/H+2|0),n=r;n;--n)g=g.div(g.times(g).plus(1).sqrt().plus(1));for(Y=!1,i=Math.ceil(c/H),o=1,m=g.times(g),d=new M(g),t=g;n!==-1;)if(t=t.times(m),a=d.minus(t.div(o+=2)),t=t.times(m),d=a.plus(t.div(o+=2)),d.d[i]!==void 0)for(n=i;d.d[n]===a.d[n]&&n--;);return r&&(d=d.times(2<<r-1)),Y=!0,z(d,M.precision=P,M.rounding=A,!0)};B.isFinite=function(){return!!this.d};B.isInteger=B.isInt=function(){return!!this.d&&Xt(this.e/H)>this.d.length-2};B.isNaN=function(){return!this.s};B.isNegative=B.isNeg=function(){return this.s<0};B.isPositive=B.isPos=function(){return this.s>0};B.isZero=function(){return!!this.d&&this.d[0]===0};B.lessThan=B.lt=function(n){return this.cmp(n)<0};B.lessThanOrEqualTo=B.lte=function(n){return this.cmp(n)<1};B.logarithm=B.log=function(n){var i,r,o,t,a,d,c,m,g=this,M=g.constructor,P=M.precision,A=M.rounding,T=5;if(n==null)n=new M(10),i=!0;else{if(n=new M(n),r=n.d,n.s<0||!r||!r[0]||n.eq(1))return new M(NaN);i=n.eq(10)}if(r=g.d,g.s<0||!r||!r[0]||g.eq(1))return new M(r&&!r[0]?-1/0:g.s!=1?NaN:r?0:1/0);if(i)if(r.length>1)a=!0;else{for(t=r[0];t%10===0;)t/=10;a=t!==1}if(Y=!1,c=P+T,d=we(g,c),o=i?Ke(M,c+10):we(n,c),m=st(d,o,c,1),Ce(m.d,t=P,A))do if(c+=10,d=we(g,c),o=i?Ke(M,c+10):we(n,c),m=st(d,o,c,1),!a){+Ht(m.d).slice(t+1,t+15)+1==1e14&&(m=z(m,P+1,0));break}while(Ce(m.d,t+=10,A));return Y=!0,z(m,P,A)};B.minus=B.sub=function(n){var i,r,o,t,a,d,c,m,g,M,P,A,T=this,O=T.constructor;if(n=new O(n),!T.d||!n.d)return!T.s||!n.s?n=new O(NaN):T.d?n.s=-n.s:n=new O(n.d||T.s!==n.s?T:NaN),n;if(T.s!=n.s)return n.s=-n.s,T.plus(n);if(g=T.d,A=n.d,c=O.precision,m=O.rounding,!g[0]||!A[0]){if(A[0])n.s=-n.s;else if(g[0])n=new O(T);else return new O(m===3?-0:0);return Y?z(n,c,m):n}if(r=Xt(n.e/H),M=Xt(T.e/H),g=g.slice(),a=M-r,a){for(P=a<0,P?(i=g,a=-a,d=A.length):(i=A,r=M,d=g.length),o=Math.max(Math.ceil(c/H),d)+2,a>o&&(a=o,i.length=1),i.reverse(),o=a;o--;)i.push(0);i.reverse()}else{for(o=g.length,d=A.length,P=o<d,P&&(d=o),o=0;o<d;o++)if(g[o]!=A[o]){P=g[o]<A[o];break}a=0}for(P&&(i=g,g=A,A=i,n.s=-n.s),d=g.length,o=A.length-d;o>0;--o)g[d++]=0;for(o=A.length;o>a;){if(g[--o]<A[o]){for(t=o;t&&g[--t]===0;)g[t]=le-1;--g[t],g[o]+=le}g[o]-=A[o]}for(;g[--d]===0;)g.pop();for(;g[0]===0;g.shift())--r;return g[0]?(n.d=g,n.e=Ge(g,r),Y?z(n,c,m):n):new O(m===3?-0:0)};B.modulo=B.mod=function(n){var i,r=this,o=r.constructor;return n=new o(n),!r.d||!n.s||n.d&&!n.d[0]?new o(NaN):!n.d||r.d&&!r.d[0]?z(new o(r),o.precision,o.rounding):(Y=!1,o.modulo==9?(i=st(r,n.abs(),0,3,1),i.s*=n.s):i=st(r,n,0,o.modulo,1),i=i.times(n),Y=!0,r.minus(i))};B.naturalExponential=B.exp=function(){return Oi(this)};B.naturalLogarithm=B.ln=function(){return we(this)};B.negated=B.neg=function(){var n=new this.constructor(this);return n.s=-n.s,z(n)};B.plus=B.add=function(n){var i,r,o,t,a,d,c,m,g,M,P=this,A=P.constructor;if(n=new A(n),!P.d||!n.d)return!P.s||!n.s?n=new A(NaN):P.d||(n=new A(n.d||P.s===n.s?P:NaN)),n;if(P.s!=n.s)return n.s=-n.s,P.minus(n);if(g=P.d,M=n.d,c=A.precision,m=A.rounding,!g[0]||!M[0])return M[0]||(n=new A(P)),Y?z(n,c,m):n;if(a=Xt(P.e/H),o=Xt(n.e/H),g=g.slice(),t=a-o,t){for(t<0?(r=g,t=-t,d=M.length):(r=M,o=a,d=g.length),a=Math.ceil(c/H),d=a>d?a+1:d+1,t>d&&(t=d,r.length=1),r.reverse();t--;)r.push(0);r.reverse()}for(d=g.length,t=M.length,d-t<0&&(t=d,r=M,M=g,g=r),i=0;t;)i=(g[--t]=g[t]+M[t]+i)/le|0,g[t]%=le;for(i&&(g.unshift(i),++o),d=g.length;g[--d]==0;)g.pop();return n.d=g,n.e=Ge(g,o),Y?z(n,c,m):n};B.precision=B.sd=function(n){var i,r=this;if(n!==void 0&&n!==!!n&&n!==1&&n!==0)throw Error(ve+n);return r.d?(i=pr(r.d),n&&r.e+1>i&&(i=r.e+1)):i=NaN,i};B.round=function(){var n=this,i=n.constructor;return z(new i(n),n.e+1,i.rounding)};B.sine=B.sin=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+Math.max(r.e,r.sd())+H,o.rounding=1,r=Jr(o,wr(o,r)),o.precision=n,o.rounding=i,z(pe>2?r.neg():r,n,i,!0)):new o(NaN)};B.squareRoot=B.sqrt=function(){var n,i,r,o,t,a,d=this,c=d.d,m=d.e,g=d.s,M=d.constructor;if(g!==1||!c||!c[0])return new M(!g||g<0&&(!c||c[0])?NaN:c?d:1/0);for(Y=!1,g=Math.sqrt(+d),g==0||g==1/0?(i=Ht(c),(i.length+m)%2==0&&(i+="0"),g=Math.sqrt(i),m=Xt((m+1)/2)-(m<0||m%2),g==1/0?i="5e"+m:(i=g.toExponential(),i=i.slice(0,i.indexOf("e")+1)+m),o=new M(i)):o=new M(g.toString()),r=(m=M.precision)+3;;)if(a=o,o=a.plus(st(d,a,r+2,1)).times(.5),Ht(a.d).slice(0,r)===(i=Ht(o.d)).slice(0,r))if(i=i.slice(r-3,r+1),i=="9999"||!t&&i=="4999"){if(!t&&(z(a,m+1,0),a.times(a).eq(d))){o=a;break}r+=4,t=1}else{(!+i||!+i.slice(1)&&i.charAt(0)=="5")&&(z(o,m+1,1),n=!o.times(o).eq(d));break}return Y=!0,z(o,m,M.rounding,n)};B.tangent=B.tan=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+10,o.rounding=1,r=r.sin(),r.s=1,r=st(r,new o(1).minus(r.times(r)).sqrt(),n+10,0),o.precision=n,o.rounding=i,z(pe==2||pe==4?r.neg():r,n,i,!0)):new o(NaN)};B.times=B.mul=function(n){var i,r,o,t,a,d,c,m,g,M=this,P=M.constructor,A=M.d,T=(n=new P(n)).d;if(n.s*=M.s,!A||!A[0]||!T||!T[0])return new P(!n.s||A&&!A[0]&&!T||T&&!T[0]&&!A?NaN:!A||!T?n.s/0:n.s*0);for(r=Xt(M.e/H)+Xt(n.e/H),m=A.length,g=T.length,m<g&&(a=A,A=T,T=a,d=m,m=g,g=d),a=[],d=m+g,o=d;o--;)a.push(0);for(o=g;--o>=0;){for(i=0,t=m+o;t>o;)c=a[t]+T[o]*A[t-o-1]+i,a[t--]=c%le|0,i=c/le|0;a[t]=(a[t]+i)%le|0}for(;!a[--d];)a.pop();return i?++r:a.shift(),n.d=a,n.e=Ge(a,r),Y?z(n,P.precision,P.rounding):n};B.toBinary=function(n,i){return Ri(this,2,n,i)};B.toDecimalPlaces=B.toDP=function(n,i){var r=this,o=r.constructor;return r=new o(r),n===void 0?r:(ee(n,0,Me),i===void 0?i=o.rounding:ee(i,0,8),z(r,n+r.e+1,i))};B.toExponential=function(n,i){var r,o=this,t=o.constructor;return n===void 0?r=ce(o,!0):(ee(n,0,Me),i===void 0?i=t.rounding:ee(i,0,8),o=z(new t(o),n+1,i),r=ce(o,!0,n+1)),o.isNeg()&&!o.isZero()?"-"+r:r};B.toFixed=function(n,i){var r,o,t=this,a=t.constructor;return n===void 0?r=ce(t):(ee(n,0,Me),i===void 0?i=a.rounding:ee(i,0,8),o=z(new a(t),n+t.e+1,i),r=ce(o,!1,n+o.e+1)),t.isNeg()&&!t.isZero()?"-"+r:r};B.toFraction=function(n){var i,r,o,t,a,d,c,m,g,M,P,A,T=this,O=T.d,N=T.constructor;if(!O)return new N(T);if(g=r=new N(1),o=m=new N(0),i=new N(o),a=i.e=pr(O)-T.e-1,d=a%H,i.d[0]=Zt(10,d<0?H+d:d),n==null)n=a>0?i:g;else{if(c=new N(n),!c.isInt()||c.lt(g))throw Error(ve+c);n=c.gt(i)?a>0?i:g:c}for(Y=!1,c=new N(Ht(O)),M=N.precision,N.precision=a=O.length*H*2;P=st(c,i,0,1,1),t=r.plus(P.times(o)),t.cmp(n)!=1;)r=o,o=t,t=g,g=m.plus(P.times(t)),m=t,t=i,i=c.minus(P.times(t)),c=t;return t=st(n.minus(r),o,0,1,1),m=m.plus(t.times(g)),r=r.plus(t.times(o)),m.s=g.s=T.s,A=st(g,o,a,1).minus(T).abs().cmp(st(m,r,a,1).minus(T).abs())<1?[g,o]:[m,r],N.precision=M,Y=!0,A};B.toHexadecimal=B.toHex=function(n,i){return Ri(this,16,n,i)};B.toNearest=function(n,i){var r=this,o=r.constructor;if(r=new o(r),n==null){if(!r.d)return r;n=new o(1),i=o.rounding}else{if(n=new o(n),i===void 0?i=o.rounding:ee(i,0,8),!r.d)return n.s?r:n;if(!n.d)return n.s&&(n.s=r.s),n}return n.d[0]?(Y=!1,r=st(r,n,0,i,1).times(n),Y=!0,z(r)):(n.s=r.s,r=n),r};B.toNumber=function(){return+this};B.toOctal=function(n,i){return Ri(this,8,n,i)};B.toPower=B.pow=function(n){var i,r,o,t,a,d,c=this,m=c.constructor,g=+(n=new m(n));if(!c.d||!n.d||!c.d[0]||!n.d[0])return new m(Zt(+c,g));if(c=new m(c),c.eq(1))return c;if(o=m.precision,a=m.rounding,n.eq(1))return z(c,o,a);if(i=Xt(n.e/H),i>=n.d.length-1&&(r=g<0?-g:g)<=Gr)return t=mr(m,c,r,o),n.s<0?new m(1).div(t):z(t,o,a);if(d=c.s,d<0){if(i<n.d.length-1)return new m(NaN);if((n.d[i]&1)==0&&(d=1),c.e==0&&c.d[0]==1&&c.d.length==1)return c.s=d,c}return r=Zt(+c,g),i=r==0||!isFinite(r)?Xt(g*(Math.log("0."+Ht(c.d))/Math.LN10+c.e+1)):new m(r+"").e,i>m.maxE+1||i<m.minE-1?new m(i>0?d/0:0):(Y=!1,m.rounding=c.s=1,r=Math.min(12,(i+"").length),t=Oi(n.times(we(c,o+r)),o),t.d&&(t=z(t,o+5,1),Ce(t.d,o,a)&&(i=o+10,t=z(Oi(n.times(we(c,i+r)),i),i+5,1),+Ht(t.d).slice(o+1,o+15)+1==1e14&&(t=z(t,o+1,0)))),t.s=d,Y=!0,m.rounding=a,z(t,o,a))};B.toPrecision=function(n,i){var r,o=this,t=o.constructor;return n===void 0?r=ce(o,o.e<=t.toExpNeg||o.e>=t.toExpPos):(ee(n,1,Me),i===void 0?i=t.rounding:ee(i,0,8),o=z(new t(o),n,i),r=ce(o,n<=o.e||o.e<=t.toExpNeg,n)),o.isNeg()&&!o.isZero()?"-"+r:r};B.toSignificantDigits=B.toSD=function(n,i){var r=this,o=r.constructor;return n===void 0?(n=o.precision,i=o.rounding):(ee(n,1,Me),i===void 0?i=o.rounding:ee(i,0,8)),z(new o(r),n,i)};B.toString=function(){var n=this,i=n.constructor,r=ce(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()&&!n.isZero()?"-"+r:r};B.truncated=B.trunc=function(){return z(new this.constructor(this),this.e+1,1)};B.valueOf=B.toJSON=function(){var n=this,i=n.constructor,r=ce(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()?"-"+r:r};function Ht(n){var i,r,o,t=n.length-1,a="",d=n[0];if(t>0){for(a+=d,i=1;i<t;i++)o=n[i]+"",r=H-o.length,r&&(a+=_e(r)),a+=o;d=n[i],o=d+"",r=H-o.length,r&&(a+=_e(r))}else if(d===0)return"0";for(;d%10===0;)d/=10;return a+d}function ee(n,i,r){if(n!==~~n||n<i||n>r)throw Error(ve+n)}function Ce(n,i,r,o){var t,a,d,c;for(a=n[0];a>=10;a/=10)--i;return--i<0?(i+=H,t=0):(t=Math.ceil((i+1)/H),i%=H),a=Zt(10,H-i),c=n[t]%a|0,o==null?i<3?(i==0?c=c/100|0:i==1&&(c=c/10|0),d=r<4&&c==99999||r>3&&c==49999||c==5e4||c==0):d=(r<4&&c+1==a||r>3&&c+1==a/2)&&(n[t+1]/a/100|0)==Zt(10,i-2)-1||(c==a/2||c==0)&&(n[t+1]/a/100|0)==0:i<4?(i==0?c=c/1e3|0:i==1?c=c/100|0:i==2&&(c=c/10|0),d=(o||r<4)&&c==9999||!o&&r>3&&c==4999):d=((o||r<4)&&c+1==a||!o&&r>3&&c+1==a/2)&&(n[t+1]/a/1e3|0)==Zt(10,i-3)-1,d}function je(n,i,r){for(var o,t=[0],a,d=0,c=n.length;d<c;){for(a=t.length;a--;)t[a]*=i;for(t[0]+=xi.indexOf(n.charAt(d++)),o=0;o<t.length;o++)t[o]>r-1&&(t[o+1]===void 0&&(t[o+1]=0),t[o+1]+=t[o]/r|0,t[o]%=r)}return t.reverse()}function Vr(n,i){var r,o,t;if(i.isZero())return i;o=i.d.length,o<32?(r=Math.ceil(o/3),t=(1/He(4,r)).toString()):(r=16,t="2.3283064365386962890625e-10"),n.precision+=r,i=Pe(n,1,i.times(t),new n(1));for(var a=r;a--;){var d=i.times(i);i=d.times(d).minus(d).times(8).plus(1)}return n.precision-=r,i}var st=(function(){function n(o,t,a){var d,c=0,m=o.length;for(o=o.slice();m--;)d=o[m]*t+c,o[m]=d%a|0,c=d/a|0;return c&&o.unshift(c),o}function i(o,t,a,d){var c,m;if(a!=d)m=a>d?1:-1;else for(c=m=0;c<a;c++)if(o[c]!=t[c]){m=o[c]>t[c]?1:-1;break}return m}function r(o,t,a,d){for(var c=0;a--;)o[a]-=c,c=o[a]<t[a]?1:0,o[a]=c*d+o[a]-t[a];for(;!o[0]&&o.length>1;)o.shift()}return function(o,t,a,d,c,m){var g,M,P,A,T,O,N,I,L,R,E,q,W,F,Z,_,b,S,C,x,p=o.constructor,e=o.s==t.s?1:-1,s=o.d,l=t.d;if(!s||!s[0]||!l||!l[0])return new p(!o.s||!t.s||(s?l&&s[0]==l[0]:!l)?NaN:s&&s[0]==0||!l?e*0:e/0);for(m?(T=1,M=o.e-t.e):(m=le,T=H,M=Xt(o.e/T)-Xt(t.e/T)),C=l.length,b=s.length,L=new p(e),R=L.d=[],P=0;l[P]==(s[P]||0);P++);if(l[P]>(s[P]||0)&&M--,a==null?(F=a=p.precision,d=p.rounding):c?F=a+(o.e-t.e)+1:F=a,F<0)R.push(1),O=!0;else{if(F=F/T+2|0,P=0,C==1){for(A=0,l=l[0],F++;(P<b||A)&&F--;P++)Z=A*m+(s[P]||0),R[P]=Z/l|0,A=Z%l|0;O=A||P<b}else{for(A=m/(l[0]+1)|0,A>1&&(l=n(l,A,m),s=n(s,A,m),C=l.length,b=s.length),_=C,E=s.slice(0,C),q=E.length;q<C;)E[q++]=0;x=l.slice(),x.unshift(0),S=l[0],l[1]>=m/2&&++S;do A=0,g=i(l,E,C,q),g<0?(W=E[0],C!=q&&(W=W*m+(E[1]||0)),A=W/S|0,A>1?(A>=m&&(A=m-1),N=n(l,A,m),I=N.length,q=E.length,g=i(N,E,I,q),g==1&&(A--,r(N,C<I?x:l,I,m))):(A==0&&(g=A=1),N=l.slice()),I=N.length,I<q&&N.unshift(0),r(E,N,q,m),g==-1&&(q=E.length,g=i(l,E,C,q),g<1&&(A++,r(E,C<q?x:l,q,m))),q=E.length):g===0&&(A++,E=[0]),R[P++]=A,g&&E[0]?E[q++]=s[_]||0:(E=[s[_]],q=1);while((_++<b||E[0]!==void 0)&&F--);O=E[0]!==void 0}R[0]||R.shift()}if(T==1)L.e=M,ur=O;else{for(P=1,A=R[0];A>=10;A/=10)P++;L.e=P+M*T-1,z(L,c?a+L.e+1:a,d,O)}return L}})();function z(n,i,r,o){var t,a,d,c,m,g,M,P,A,T=n.constructor;t:if(i!=null){if(P=n.d,!P)return n;for(t=1,c=P[0];c>=10;c/=10)t++;if(a=i-t,a<0)a+=H,d=i,M=P[A=0],m=M/Zt(10,t-d-1)%10|0;else if(A=Math.ceil((a+1)/H),c=P.length,A>=c)if(o){for(;c++<=A;)P.push(0);M=m=0,t=1,a%=H,d=a-H+1}else break t;else{for(M=c=P[A],t=1;c>=10;c/=10)t++;a%=H,d=a-H+t,m=d<0?0:M/Zt(10,t-d-1)%10|0}if(o=o||i<0||P[A+1]!==void 0||(d<0?M:M%Zt(10,t-d-1)),g=r<4?(m||o)&&(r==0||r==(n.s<0?3:2)):m>5||m==5&&(r==4||o||r==6&&(a>0?d>0?M/Zt(10,t-d):0:P[A-1])%10&1||r==(n.s<0?8:7)),i<1||!P[0])return P.length=0,g?(i-=n.e+1,P[0]=Zt(10,(H-i%H)%H),n.e=-i||0):P[0]=n.e=0,n;if(a==0?(P.length=A,c=1,A--):(P.length=A+1,c=Zt(10,H-a),P[A]=d>0?(M/Zt(10,t-d)%Zt(10,d)|0)*c:0),g)for(;;)if(A==0){for(a=1,d=P[0];d>=10;d/=10)a++;for(d=P[0]+=c,c=1;d>=10;d/=10)c++;a!=c&&(n.e++,P[0]==le&&(P[0]=1));break}else{if(P[A]+=c,P[A]!=le)break;P[A--]=0,c=1}for(a=P.length;P[--a]===0;)P.pop()}return Y&&(n.e>T.maxE?(n.d=null,n.e=NaN):n.e<T.minE&&(n.e=0,n.d=[0])),n}function ce(n,i,r){if(!n.isFinite())return _r(n);var o,t=n.e,a=Ht(n.d),d=a.length;return i?(r&&(o=r-d)>0?a=a.charAt(0)+"."+a.slice(1)+_e(o):d>1&&(a=a.charAt(0)+"."+a.slice(1)),a=a+(n.e<0?"e":"e+")+n.e):t<0?(a="0."+_e(-t-1)+a,r&&(o=r-d)>0&&(a+=_e(o))):t>=d?(a+=_e(t+1-d),r&&(o=r-t-1)>0&&(a=a+"."+_e(o))):((o=t+1)<d&&(a=a.slice(0,o)+"."+a.slice(o)),r&&(o=r-d)>0&&(t+1===d&&(a+="."),a+=_e(o))),a}function Ge(n,i){var r=n[0];for(i*=H;r>=10;r/=10)i++;return i}function Ke(n,i,r){if(i>Hr)throw Y=!0,r&&(n.precision=r),Error(cr);return z(new n(We),i,1,!0)}function ue(n,i,r){if(i>Ti)throw Error(cr);return z(new n(Ze),i,r,!0)}function pr(n){var i=n.length-1,r=i*H+1;if(i=n[i],i){for(;i%10==0;i/=10)r--;for(i=n[0];i>=10;i/=10)r++}return r}function _e(n){for(var i="";n--;)i+="0";return i}function mr(n,i,r,o){var t,a=new n(1),d=Math.ceil(o/H+4);for(Y=!1;;){if(r%2&&(a=a.times(i),ar(a.d,d)&&(t=!0)),r=Xt(r/2),r===0){r=a.d.length-1,t&&a.d[r]===0&&++a.d[r];break}i=i.times(i),ar(i.d,d)}return Y=!0,a}function sr(n){return n.d[n.d.length-1]&1}function gr(n,i,r){for(var o,t,a=new n(i[0]),d=0;++d<i.length;){if(t=new n(i[d]),!t.s){a=t;break}o=a.cmp(t),(o===r||o===0&&a.s===r)&&(a=t)}return a}function Oi(n,i){var r,o,t,a,d,c,m,g=0,M=0,P=0,A=n.constructor,T=A.rounding,O=A.precision;if(!n.d||!n.d[0]||n.e>17)return new A(n.d?n.d[0]?n.s<0?0:1/0:1:n.s?n.s<0?0:n:NaN);for(i==null?(Y=!1,m=O):m=i,c=new A(.03125);n.e>-2;)n=n.times(c),P+=5;for(o=Math.log(Zt(2,P))/Math.LN10*2+5|0,m+=o,r=a=d=new A(1),A.precision=m;;){if(a=z(a.times(n),m,1),r=r.times(++M),c=d.plus(st(a,r,m,1)),Ht(c.d).slice(0,m)===Ht(d.d).slice(0,m)){for(t=P;t--;)d=z(d.times(d),m,1);if(i==null)if(g<3&&Ce(d.d,m-o,T,g))A.precision=m+=10,r=a=c=new A(1),M=0,g++;else return z(d,A.precision=O,T,Y=!0);else return A.precision=O,d}d=c}}function we(n,i){var r,o,t,a,d,c,m,g,M,P,A,T=1,O=10,N=n,I=N.d,L=N.constructor,R=L.rounding,E=L.precision;if(N.s<0||!I||!I[0]||!N.e&&I[0]==1&&I.length==1)return new L(I&&!I[0]?-1/0:N.s!=1?NaN:I?0:N);if(i==null?(Y=!1,M=E):M=i,L.precision=M+=O,r=Ht(I),o=r.charAt(0),Math.abs(a=N.e)<15e14){for(;o<7&&o!=1||o==1&&r.charAt(1)>3;)N=N.times(n),r=Ht(N.d),o=r.charAt(0),T++;a=N.e,o>1?(N=new L("0."+r),a++):N=new L(o+"."+r.slice(1))}else return g=Ke(L,M+2,E).times(a+""),N=we(new L(o+"."+r.slice(1)),M-O).plus(g),L.precision=E,i==null?z(N,E,R,Y=!0):N;for(P=N,m=d=N=st(N.minus(1),N.plus(1),M,1),A=z(N.times(N),M,1),t=3;;){if(d=z(d.times(A),M,1),g=m.plus(st(d,new L(t),M,1)),Ht(g.d).slice(0,M)===Ht(m.d).slice(0,M))if(m=m.times(2),a!==0&&(m=m.plus(Ke(L,M+2,E).times(a+""))),m=st(m,new L(T),M,1),i==null)if(Ce(m.d,M-O,R,c))L.precision=M+=O,g=d=N=st(P.minus(1),P.plus(1),M,1),A=z(N.times(N),M,1),t=c=1;else return z(m,L.precision=E,R,Y=!0);else return L.precision=E,m;m=g,t+=2}}function _r(n){return String(n.s*n.s/0)}function $e(n,i){var r,o,t;for((r=i.indexOf("."))>-1&&(i=i.replace(".","")),(o=i.search(/e/i))>0?(r<0&&(r=o),r+=+i.slice(o+1),i=i.substring(0,o)):r<0&&(r=i.length),o=0;i.charCodeAt(o)===48;o++);for(t=i.length;i.charCodeAt(t-1)===48;--t);if(i=i.slice(o,t),i){if(t-=o,n.e=r=r-o-1,n.d=[],o=(r+1)%H,r<0&&(o+=H),o<t){for(o&&n.d.push(+i.slice(0,o)),t-=H;o<t;)n.d.push(+i.slice(o,o+=H));i=i.slice(o),o=H-i.length}else o-=t;for(;o--;)i+="0";n.d.push(+i),Y&&(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 Xr(n,i){var r,o,t,a,d,c,m,g,M;if(i.indexOf("_")>-1){if(i=i.replace(/(\d)_(?=\d)/g,"$1"),hr.test(i))return $e(n,i)}else if(i==="Infinity"||i==="NaN")return+i||(n.s=NaN),n.e=NaN,n.d=null,n;if(Kr.test(i))r=16,i=i.toLowerCase();else if(Zr.test(i))r=2;else if(zr.test(i))r=8;else throw Error(ve+i);for(a=i.search(/p/i),a>0?(m=+i.slice(a+1),i=i.substring(2,a)):i=i.slice(2),a=i.indexOf("."),d=a>=0,o=n.constructor,d&&(i=i.replace(".",""),c=i.length,a=c-a,t=mr(o,new o(r),a,a*2)),g=je(i,r,le),M=g.length-1,a=M;g[a]===0;--a)g.pop();return a<0?new o(n.s*0):(n.e=Ge(g,M),n.d=g,Y=!1,d&&(n=st(n,t,c*4)),m&&(n=n.times(Math.abs(m)<54?Zt(2,m):xe.pow(2,m))),Y=!0,n)}function Jr(n,i){var r,o=i.d.length;if(o<3)return i.isZero()?i:Pe(n,2,i,i);r=1.4*Math.sqrt(o),r=r>16?16:r|0,i=i.times(1/He(5,r)),i=Pe(n,2,i,i);for(var t,a=new n(5),d=new n(16),c=new n(20);r--;)t=i.times(i),i=i.times(a.plus(t.times(d.times(t).minus(c))));return i}function Pe(n,i,r,o,t){var a,d,c,m,g=1,M=n.precision,P=Math.ceil(M/H);for(Y=!1,m=r.times(r),c=new n(o);;){if(d=st(c.times(m),new n(i++*i++),M,1),c=t?o.plus(d):o.minus(d),o=st(d.times(m),new n(i++*i++),M,1),d=c.plus(o),d.d[P]!==void 0){for(a=P;d.d[a]===c.d[a]&&a--;);if(a==-1)break}a=c,c=o,o=d,d=a,g++}return Y=!0,d.d.length=P+1,d}function He(n,i){for(var r=n;--i;)r*=n;return r}function wr(n,i){var r,o=i.s<0,t=ue(n,n.precision,1),a=t.times(.5);if(i=i.abs(),i.lte(a))return pe=o?4:1,i;if(r=i.divToInt(t),r.isZero())pe=o?3:2;else{if(i=i.minus(r.times(t)),i.lte(a))return pe=sr(r)?o?2:3:o?4:1,i;pe=sr(r)?o?1:4:o?3:2}return i.minus(t).abs()}function Ri(n,i,r,o){var t,a,d,c,m,g,M,P,A,T=n.constructor,O=r!==void 0;if(O?(ee(r,1,Me),o===void 0?o=T.rounding:ee(o,0,8)):(r=T.precision,o=T.rounding),!n.isFinite())M=_r(n);else{for(M=ce(n),d=M.indexOf("."),O?(t=2,i==16?r=r*4-3:i==8&&(r=r*3-2)):t=i,d>=0&&(M=M.replace(".",""),A=new T(1),A.e=M.length-d,A.d=je(ce(A),10,t),A.e=A.d.length),P=je(M,10,t),a=m=P.length;P[--m]==0;)P.pop();if(!P[0])M=O?"0p+0":"0";else{if(d<0?a--:(n=new T(n),n.d=P,n.e=a,n=st(n,A,r,o,0,t),P=n.d,a=n.e,g=ur),d=P[r],c=t/2,g=g||P[r+1]!==void 0,g=o<4?(d!==void 0||g)&&(o===0||o===(n.s<0?3:2)):d>c||d===c&&(o===4||g||o===6&&P[r-1]&1||o===(n.s<0?8:7)),P.length=r,g)for(;++P[--r]>t-1;)P[r]=0,r||(++a,P.unshift(1));for(m=P.length;!P[m-1];--m);for(d=0,M="";d<m;d++)M+=xi.charAt(P[d]);if(O){if(m>1)if(i==16||i==8){for(d=i==16?4:3,--m;m%d;m++)M+="0";for(P=je(M,t,i),m=P.length;!P[m-1];--m);for(d=1,M="1.";d<m;d++)M+=xi.charAt(P[d])}else M=M.charAt(0)+"."+M.slice(1);M=M+(a<0?"p":"p+")+a}else if(a<0){for(;++a;)M="0"+M;M="0."+M}else if(++a>m)for(a-=m;a--;)M+="0";else a<m&&(M=M.slice(0,a)+"."+M.slice(a))}M=(i==16?"0x":i==2?"0b":i==8?"0o":"")+M}return n.s<0?"-"+M:M}function ar(n,i){if(n.length>i)return n.length=i,!0}function Yr(n){return new this(n).abs()}function Qr(n){return new this(n).acos()}function tn(n){return new this(n).acosh()}function en(n,i){return new this(n).plus(i)}function rn(n){return new this(n).asin()}function nn(n){return new this(n).asinh()}function on(n){return new this(n).atan()}function sn(n){return new this(n).atanh()}function an(n,i){n=new this(n),i=new this(i);var r,o=this.precision,t=this.rounding,a=o+4;return!n.s||!i.s?r=new this(NaN):!n.d&&!i.d?(r=ue(this,a,1).times(i.s>0?.25:.75),r.s=n.s):!i.d||n.isZero()?(r=i.s<0?ue(this,o,t):new this(0),r.s=n.s):!n.d||i.isZero()?(r=ue(this,a,1).times(.5),r.s=n.s):i.s<0?(this.precision=a,this.rounding=1,r=this.atan(st(n,i,a,1)),i=ue(this,a,1),this.precision=o,this.rounding=t,r=n.s<0?r.minus(i):r.plus(i)):r=this.atan(st(n,i,a,1)),r}function ln(n){return new this(n).cbrt()}function un(n){return z(n=new this(n),n.e+1,2)}function cn(n,i,r){return new this(n).clamp(i,r)}function fn(n){if(!n||typeof n!="object")throw Error(ze+"Object expected");var i,r,o,t=n.defaults===!0,a=["precision",1,Me,"rounding",0,8,"toExpNeg",-Se,0,"toExpPos",0,Se,"maxE",0,Se,"minE",-Se,0,"modulo",0,9];for(i=0;i<a.length;i+=3)if(r=a[i],t&&(this[r]=Di[r]),(o=n[r])!==void 0)if(Xt(o)===o&&o>=a[i+1]&&o<=a[i+2])this[r]=o;else throw Error(ve+r+": "+o);if(r="crypto",t&&(this[r]=Di[r]),(o=n[r])!==void 0)if(o===!0||o===!1||o===0||o===1)if(o)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[r]=!0;else throw Error(fr);else this[r]=!1;else throw Error(ve+r+": "+o);return this}function dn(n){return new this(n).cos()}function hn(n){return new this(n).cosh()}function vr(n){var i,r,o;function t(a){var d,c,m,g=this;if(!(g instanceof t))return new t(a);if(g.constructor=t,lr(a)){g.s=a.s,Y?!a.d||a.e>t.maxE?(g.e=NaN,g.d=null):a.e<t.minE?(g.e=0,g.d=[0]):(g.e=a.e,g.d=a.d.slice()):(g.e=a.e,g.d=a.d?a.d.slice():a.d);return}if(m=typeof a,m==="number"){if(a===0){g.s=1/a<0?-1:1,g.e=0,g.d=[0];return}if(a<0?(a=-a,g.s=-1):g.s=1,a===~~a&&a<1e7){for(d=0,c=a;c>=10;c/=10)d++;Y?d>t.maxE?(g.e=NaN,g.d=null):d<t.minE?(g.e=0,g.d=[0]):(g.e=d,g.d=[a]):(g.e=d,g.d=[a]);return}if(a*0!==0){a||(g.s=NaN),g.e=NaN,g.d=null;return}return $e(g,a.toString())}if(m==="string")return(c=a.charCodeAt(0))===45?(a=a.slice(1),g.s=-1):(c===43&&(a=a.slice(1)),g.s=1),hr.test(a)?$e(g,a):Xr(g,a);if(m==="bigint")return a<0?(a=-a,g.s=-1):g.s=1,$e(g,a.toString());throw Error(ve+a)}if(t.prototype=B,t.ROUND_UP=0,t.ROUND_DOWN=1,t.ROUND_CEIL=2,t.ROUND_FLOOR=3,t.ROUND_HALF_UP=4,t.ROUND_HALF_DOWN=5,t.ROUND_HALF_EVEN=6,t.ROUND_HALF_CEIL=7,t.ROUND_HALF_FLOOR=8,t.EUCLID=9,t.config=t.set=fn,t.clone=vr,t.isDecimal=lr,t.abs=Yr,t.acos=Qr,t.acosh=tn,t.add=en,t.asin=rn,t.asinh=nn,t.atan=on,t.atanh=sn,t.atan2=an,t.cbrt=ln,t.ceil=un,t.clamp=cn,t.cos=dn,t.cosh=hn,t.div=pn,t.exp=mn,t.floor=gn,t.hypot=_n,t.ln=wn,t.log=vn,t.log10=bn,t.log2=Mn,t.max=yn,t.min=kn,t.mod=Sn,t.mul=Pn,t.pow=An,t.random=Cn,t.round=xn,t.sign=Dn,t.sin=Tn,t.sinh=On,t.sqrt=Rn,t.sub=Ln,t.sum=Nn,t.tan=In,t.tanh=En,t.trunc=Bn,n===void 0&&(n={}),n&&n.defaults!==!0)for(o=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],i=0;i<o.length;)n.hasOwnProperty(r=o[i++])||(n[r]=this[r]);return t.config(n),t}function pn(n,i){return new this(n).div(i)}function mn(n){return new this(n).exp()}function gn(n){return z(n=new this(n),n.e+1,3)}function _n(){var n,i,r=new this(0);for(Y=!1,n=0;n<arguments.length;)if(i=new this(arguments[n++]),i.d)r.d&&(r=r.plus(i.times(i)));else{if(i.s)return Y=!0,new this(1/0);r=i}return Y=!0,r.sqrt()}function lr(n){return n instanceof xe||n&&n.toStringTag===dr||!1}function wn(n){return new this(n).ln()}function vn(n,i){return new this(n).log(i)}function Mn(n){return new this(n).log(2)}function bn(n){return new this(n).log(10)}function yn(){return gr(this,arguments,-1)}function kn(){return gr(this,arguments,1)}function Sn(n,i){return new this(n).mod(i)}function Pn(n,i){return new this(n).mul(i)}function An(n,i){return new this(n).pow(i)}function Cn(n){var i,r,o,t,a=0,d=new this(1),c=[];if(n===void 0?n=this.precision:ee(n,1,Me),o=Math.ceil(n/H),this.crypto)if(crypto.getRandomValues)for(i=crypto.getRandomValues(new Uint32Array(o));a<o;)t=i[a],t>=429e7?i[a]=crypto.getRandomValues(new Uint32Array(1))[0]:c[a++]=t%1e7;else if(crypto.randomBytes){for(i=crypto.randomBytes(o*=4);a<o;)t=i[a]+(i[a+1]<<8)+(i[a+2]<<16)+((i[a+3]&127)<<24),t>=214e7?crypto.randomBytes(4).copy(i,a):(c.push(t%1e7),a+=4);a=o/4}else throw Error(fr);else for(;a<o;)c[a++]=Math.random()*1e7|0;for(o=c[--a],n%=H,o&&n&&(t=Zt(10,H-n),c[a]=(o/t|0)*t);c[a]===0;a--)c.pop();if(a<0)r=0,c=[0];else{for(r=-1;c[0]===0;r-=H)c.shift();for(o=1,t=c[0];t>=10;t/=10)o++;o<H&&(r-=H-o)}return d.e=r,d.d=c,d}function xn(n){return z(n=new this(n),n.e+1,this.rounding)}function Dn(n){return n=new this(n),n.d?n.d[0]?n.s:0*n.s:n.s||NaN}function Tn(n){return new this(n).sin()}function On(n){return new this(n).sinh()}function Rn(n){return new this(n).sqrt()}function Ln(n,i){return new this(n).sub(i)}function Nn(){var n=0,i=arguments,r=new this(i[n]);for(Y=!1;r.s&&++n<i.length;)r=r.plus(i[n]);return Y=!0,z(r,this.precision,this.rounding)}function In(n){return new this(n).tan()}function En(n){return new this(n).tanh()}function Bn(n){return z(n=new this(n),n.e+1,1)}B[Symbol.for("nodejs.util.inspect.custom")]=B.toString;B[Symbol.toStringTag]="Decimal";var xe=B.constructor=vr(Di);We=new xe(We);Ze=new xe(Ze);var gt=xe;var Mr=require("@suilend/sdk");var Ve=class{constructor(i){this.getPositionList=async(i=this._sdk.getSenderAddress(),r=!1)=>{let{lending_market_id:o,lending_market_type:t}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend);try{let a=`cap_to_position_mapping_${i}`,d=this._sdk.getCache(a,r),c=await this._sdk.FullClient.getOwnedObjectsByPage(i,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{MatchAny:[{StructType:`${this._sdk.sdkOptions.margin_trading.package_id}::position::PositionCap`}]}});if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:42 ~ PositionModules ~ ownerRes.data.length:",c.data.length),c.data.length===0)return Yt("PositionNotFound","Position not found"),[];let m=await this._sdk.SuiLendModule.getSuilendClient(o,t);console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:48 ~ PositionModules ~ suiLendClient:",m);let{allLendingMarketData:g,lstAprPercentMap:M}=await this._sdk.SuiLendModule.getLendingMarketData();console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:50 ~ PositionModules ~ allLendingMarketData:",g);let P=g[o].reserveMap;d||(d=new Map);let A=[];for(let I=0;I<c.data.length;I++){let L=(0,D.getObjectFields)(c.data[I]);L.position_id&&A.push(L.position_id),L.id.id&&d.set(L.id.id,L.position_id)}await this._sdk.updateCache(a,d,D.CACHE_TIME_24H);let T=await this._sdk.FullClient.batchGetObjects(A,{showContent:!0});console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:73 ~ PositionModules ~ positionRes:",T);let O=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:g,lstAprPercentMap:M},!0);console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:75 ~ PositionModules ~ obligations:",O);let N=[];for(let I=0;I<T.length;I++){let L=A[I],R="";for(let[Z,_]of d.entries())if(_===L){R=Z;break}let E=Le(T[I],R),q=await m.getObligation(E.obligation_owner_cap),W=Ne(E,q,P||{},M||{},O[o].rewardMap),F=this.getObligationRewardsInfo(q,Object.values(P));N.push({...W,claimable_rewards:F})}return N}catch(a){return Yt("FetchError",a),[]}};this.getPositionInfo=async(i,r=this._sdk.getSenderAddress(),o=!0)=>{let t=`cap_to_position_mapping_${r}`,a=this._sdk.getCache(t,o);a||(a=new Map);let d=this.getCapIdByPositionId(i,r)||"";if(!d){let q=await this._sdk.FullClient.getOwnedObjectsByPage(r,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{MatchAny:[{StructType:`${this._sdk.sdkOptions.margin_trading.package_id}::position::PositionCap`}]}});for(let W=0;W<q.data.length;W++){let F=(0,D.getObjectFields)(q.data[W]);F.id.id&&a.set(F.id.id,F.position_id),F.position_id===i&&(d=F.id.id)}}let c=await this._sdk.FullClient.batchGetObjects([i],{showContent:!0}),m=Le(c[0],d||""),{lending_market_id:g,lending_market_type:M}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),P=await this._sdk.SuiLendModule.getSuilendClient(g,M),{allLendingMarketData:A,lstAprPercentMap:T}=await this._sdk.SuiLendModule.getLendingMarketData(),O=A[g].reserveMap,N=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:A,lstAprPercentMap:T},!0),I=await P.getObligation(m.obligation_owner_cap),L=this.getObligationRewardsInfo(I,Object.values(O));return{...Ne(m,I,O||{},T||{},N[g].rewardMap),claimable_rewards:L}};this.getCapIdByPositionId=(i,r=this._sdk.getSenderAddress())=>{let o=`cap_to_position_mapping_${r}`,t=this._sdk.getCache(o,!1);if(!t)return"";for(let[a,d]of t.entries())if(d===i)return a;return""};this.repay=i=>{let{margin_trading:r}=this._sdk.sdkOptions,{lending_market_id:o,lending_market_type:t}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{global_config_id:a,versioned_id:d}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),{txb:c,repay_amount:m,repay_coin_type:g,repay_coin:M,repay_reserve_array_index:P,market_id:A,position_cap_id:T}=i,O=c||new oe.Transaction,N=M||D.CoinAssist.buildCoinWithBalance(BigInt(m.toString()),g,O);O.moveCall({target:`${r.published_at}::router::repay`,typeArguments:[t,g],arguments:[O.object(a),O.object(o),O.object(A),O.object(T),N,O.pure.u64(P.toString()),O.object(D.CLOCK_ADDRESS),O.object(d)]})};this.withdrawAsset=(i,r)=>{let{margin_trading:o}=this._sdk.sdkOptions,{lending_market_id:t,lending_market_type:a}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{global_config_id:d,versioned_id:c}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),{package_id:m}=this._sdk.sdkOptions.suilend,{market_id:g,withdraw_amount:M,withdraw_reserve_array_index:P,withdraw_coin_type:A,position_cap_id:T}=i,[O]=r.moveCall({target:"0x1::option::none",typeArguments:[`${m}::lending_market::RateLimiterExemption<${a}, ${A}>`],arguments:[]});return r.moveCall({target:`${o.published_at}::router::withdraw`,typeArguments:[a,A],arguments:[r.object(d),r.object(t),r.object(g),r.object(T),r.object(O),r.pure.u64(M),r.pure.u64(P),r.object(D.SUI_SYSTEM_STATE_OBJECT_ID),r.object(D.CLOCK_ADDRESS),r.object(c)]})};this.borrowAsset=(i,r)=>{let{reserve_array_index:o,borrow_amount:t,base_token:a,quote_token:d,lending_market_id:c,is_long:m,market_id:g,position_cap_id:M,position_cap:P}=i,{margin_trading:A}=this._sdk.sdkOptions,{lending_market_type:T}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{global_config_id:O,versioned_id:N}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading);return r.moveCall({target:`${A.published_at}::router::borrow`,typeArguments:[T,m?d:a],arguments:[r.object(O),r.object(c),r.object(g),M?r.object(M):P,r.pure.u64(o),r.pure.u64(t.toString()),r.object(D.SUI_SYSTEM_STATE_OBJECT_ID),r.object(D.CLOCK_ADDRESS),r.object(N)]})};this.openPosition=async i=>{let{is_quote:r,is_long:o,amount:t,swap_clmm_pool:a="",slippage:d,leverage:c,market_id:m}=i,{lending_market_id:g}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),M=new oe.Transaction,{base_token:P,quote_token:A}=await this._sdk.MarketModules.getMarketInfo(m),{flash_amount:T,flash_loan_coin:O,is_flash_a:N,borrow_amount:I,clmm_pool_coin_type_a:L,clmm_pool_coin_type_b:R,clmm_pool:E,base_reserve_array_index:q,quote_reserve_array_index:W,reserve:F,has_swap:Z,routers:_}=await this.calculatePositionDeposit({is_long:o,is_quote:r,amount:t,swap_clmm_pool:a,leverage:c,slippage:d,market_id:m,base_token:P,quote_token:A,is_submit:!0});console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:492 ~ PositionModules ~ routers:",_),await this._sdk.SuiLendModule.refreshReservePrices(M,F);let{balance_a:b,balance_b:S,receipt:C}=this._sdk.SwapModules.flashLoan({amount:T,clmm_pool:E,clmm_pool_coin_type_a:L,clmm_pool_coin_type_b:R,flash_loan_coin:O,tx:M}),x=o?A:P,p=o?P:A,e=D.CoinAssist.buildCoinWithBalance(BigInt(t),r?A:P,M),s=D.CoinAssist.fromBalance(b,L,M),l=D.CoinAssist.fromBalance(S,R,M);Z&&(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:515 ~ PositionModules ~ is_flash_a:",N),M.mergeCoins(e,[N?s:l])),console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:521 ~ PositionModules ~ d(flash_amount).add(amount).toString():",(0,D.d)(T).add(t).toString(),x,p);let f=await this._sdk.SwapModules.handleSwap({from:x,to:p,amount:Z?(0,D.d)(T).add(t).toString():T,input_coin:Z?e:N?s:l,swap_clmm_pool:a,slippage:d,tx:M}),h=this.createLeveragePosition({leverage:c,market_id:m,base_token:P,quote_token:A,is_long:o,init_deposit_amount:(0,D.d)(t).toString(),init_coin_type:r?A:P,tx:M}),v=o?q:W;f.swap_out_coin&&!Z&&M.mergeCoins(f.swap_out_coin,[e]),await this.depositToLeveragePosition({is_long:o,market_id:m,position_cap:h,deposit_reserve_array_index:v,input_coin:f.swap_out_coin,base_token:P,quote_token:A},M),console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:580 ~ PositionModules ~ borrow_amount:",I);let k=this.borrowAsset({position_cap:h,reserve_array_index:o?W:q,borrow_amount:I,base_token:P,quote_token:A,is_long:o,lending_market_id:g,market_id:m},M),w=D.CoinAssist.mintBalanceZero(N?R:L,M);return this._sdk.SwapModules.repayFlashLoan({tx:M,clmm_pool:E,repay_base:N?D.CoinAssist.intoBalance(k,L,M):w,repay_quote:N?w:D.CoinAssist.intoBalance(k,R,M),receipt:C,clmm_pool_coin_type_a:L,clmm_pool_coin_type_b:R}),M.transferObjects([h],M.pure.address(this._sdk.getSenderAddress())),D.CoinAssist.destroyBalanceZero(N?D.CoinAssist.intoBalance(l,R,M):D.CoinAssist.intoBalance(s,L,M),N?R:L,M),M};this.positionDeposit=async i=>{let{position_id:r,swap_clmm_pool:o="",is_quote:t,amount:a,slippage:d,leverage:c,txb:m}=i,{lending_market_id:g}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{is_long:M,deposits:P,borrows:A,position_cap_id:T,market_id:O}=await this.getPositionInfo(r),{base_token:N,quote_token:I}=await this._sdk.MarketModules.getMarketInfo(O),L=m||new oe.Transaction,R=P[0].reserveArrayIndex.toString(),E=A[0].reserveArrayIndex.toString(),{flash_amount:q,flash_loan_coin:W,is_flash_a:F,borrow_amount:Z,clmm_pool_coin_type_a:_,clmm_pool_coin_type_b:b,clmm_pool:S,reserve:C,has_swap:x}=await this.calculatePositionDeposit({market_id:O,is_long:M,is_quote:t,amount:a,swap_clmm_pool:o,leverage:c,slippage:d,base_token:N,quote_token:I,position_id:r,is_open:!1});console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:603 ~ PositionModules ~ flash_amount:",q),await this._sdk.SuiLendModule.refreshReservePrices(L,C);let{balance_a:p,balance_b:e,receipt:s}=this._sdk.SwapModules.flashLoan({amount:q,clmm_pool:S,clmm_pool_coin_type_a:_,clmm_pool_coin_type_b:b,flash_loan_coin:W,tx:L}),l=M?I:N,f=M?N:I,h=D.CoinAssist.buildCoinWithBalance(BigInt(a),t?I:N,L),v=D.CoinAssist.fromBalance(p,_,L),k=D.CoinAssist.fromBalance(e,b,L);x&&L.mergeCoins(h,[F?v:k]);let w=await this._sdk.SwapModules.handleSwap({from:l,to:f,amount:x?(0,D.d)(q).add(a).toString():q,input_coin:x?h:F?v:k,swap_clmm_pool:o,slippage:d,tx:L});w.swap_out_coin&&!x&&L.mergeCoins(w.swap_out_coin,[h]),await this.depositToLeveragePosition({is_long:M,market_id:O,position_cap_id:T,deposit_reserve_array_index:R,input_coin:w.swap_out_coin,base_token:N,quote_token:I},L);let u=await this.borrowAsset({position_cap_id:T,reserve_array_index:E,borrow_amount:Z,base_token:N,quote_token:I,is_long:M,lending_market_id:g,market_id:O},L),y=D.CoinAssist.mintBalanceZero(F?b:_,L);return this._sdk.SwapModules.repayFlashLoan({tx:L,clmm_pool:S,repay_base:F?D.CoinAssist.intoBalance(u,_,L):y,repay_quote:F?y:D.CoinAssist.intoBalance(u,b,L),receipt:s,clmm_pool_coin_type_a:_,clmm_pool_coin_type_b:b}),D.CoinAssist.destroyBalanceZero(F?D.CoinAssist.intoBalance(k,b,L):D.CoinAssist.intoBalance(v,_,L),F?b:_,L),L};this.positionWithdraw=async i=>{let{amount:r,is_quote:o,txb:t,swap_clmm_pool:a="",slippage:d,position_id:c,leverage:m}=i,g=t||new oe.Transaction,{withdraw_ctoken_amount:M,repay_amount:P,is_close:A,deposits:T,borrows:O,reserve:N,base_token:I,quote_token:L,clmm_pool:R,clmm_pool_coin_type_a:E,clmm_pool_coin_type_b:q,position_cap_id:W,market_id:F,is_long:Z,swap_convert_all:_,flash_loan_amount:b,routers:S,repay_flash_loan_amount:C="0",partial_amount_in:x,withdraw_amount:p,has_flash_loan:e}=await this.calculatePositionWithdraw({position_id:c,is_quote:o,swap_clmm_pool:a,amount:r,leverage:m,slippage:d});if(A)return this.positionClose({position_id:c,is_quote:o,leverage:m,slippage:d,swap_clmm_pool:a});await this._sdk.SuiLendModule.refreshReservePrices(g,N);let s,l;if(e){let{balance_a:k,balance_b:w,receipt:u,is_loan_a:y}=this._sdk.SwapModules.flashLoan({tx:g,amount:b,flash_loan_coin:O[0].reserve.coinType,clmm_pool:R,clmm_pool_coin_type_a:E,clmm_pool_coin_type_b:q});s=u,l=y,D.CoinAssist.destroyBalanceZero(l?w:k,l?q:E,g);let $=y?D.CoinAssist.fromBalance(k,E,g):D.CoinAssist.fromBalance(w,q,g);this.repay({txb:g,position_cap_id:W,repay_reserve_array_index:O[0].reserveArrayIndex.toString(),repay_coin:$,repay_coin_type:Z?L:I,repay_amount:"0",market_id:F}),g.transferObjects([$],this._sdk.getSenderAddress())}let f=this.withdrawAsset({market_id:F,position_cap_id:W,withdraw_amount:M.toString(),withdraw_reserve_array_index:T[0].reserveArrayIndex.toString(),withdraw_coin_type:Z?I:L},g);for(let k=1;k<T.length;k++){let w=this.withdrawAsset({market_id:F,position_cap_id:W,withdraw_amount:D.U64_MAX.toString(),withdraw_reserve_array_index:T[k].reserveArrayIndex.toString(),withdraw_coin_type:T[k].coinType},g);g.transferObjects([w],this._sdk.getSenderAddress())}let h;if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:778 ~ PositionModules ~ swap_convert_all:",{swap_convert_all:_,has_flash_loan:e}),_)h=await this._sdk.SwapModules.routerSwap({router:S?.route_obj,input_coin:f,slippage:d,txb:g});else{let k=g.splitCoins(f,[g.pure.u64(x?.toString()||"0")]);h=await this._sdk.SwapModules.routerSwap({router:S?.route_obj,input_coin:k,slippage:d,txb:g})}let v;if(h&&e&&(v=g.splitCoins(h,[g.pure.u64(C.toString())])),v&&e){let k=D.CoinAssist.mintBalanceZero(l?q:E,g);this._sdk.SwapModules.repayFlashLoan({tx:g,clmm_pool:R,repay_base:l?D.CoinAssist.intoBalance(v,E,g):k,repay_quote:l?k:D.CoinAssist.intoBalance(v,q,g),clmm_pool_coin_type_a:E,clmm_pool_coin_type_b:q,receipt:s})}else if(h){let k=g.splitCoins(h,[g.pure.u64(C.toString())]);this.repay({txb:g,position_cap_id:W,repay_reserve_array_index:O[0].reserveArrayIndex.toString(),repay_coin:k,repay_coin_type:Z?L:I,repay_amount:"0",market_id:F}),g.transferObjects([k],this._sdk.getSenderAddress())}return h&&g.transferObjects([h],this._sdk.getSenderAddress()),_||g.transferObjects([f],this._sdk.getSenderAddress()),g};this.positionClose=async i=>{let{position_id:r,is_quote:o,slippage:t,leverage:a,swap_clmm_pool:d=""}=i,{margin_trading:c}=this._sdk.sdkOptions,{global_config_id:m,versioned_id:g}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),{lending_market_type:M,lending_market_id:P}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),A=new oe.Transaction,{deposits:T,borrows:O,base_token:N,quote_token:I,clmm_pool:L,clmm_pool_coin_type_a:R,clmm_pool_coin_type_b:E,position_cap_id:q,market_id:W,swap_convert_all:F,routers:Z,partial_amount_in:_,is_long:b,flash_loan_amount:S,reserve:C,repay_flash_loan_amount:x="0",claimable_rewards:p}=await this.calculatePositionWithdraw({position_id:r,is_quote:o,swap_clmm_pool:d,amount:D.U64_MAX.toString(),leverage:a,slippage:t});await this._sdk.SuiLendModule.refreshReservePrices(A,C);let e,s;if((0,D.d)(S).gt(0)){let{balance_a:h,balance_b:v,receipt:k,is_loan_a:w}=this._sdk.SwapModules.flashLoan({tx:A,amount:S,flash_loan_coin:b?I:N,clmm_pool:L,clmm_pool_coin_type_a:R,clmm_pool_coin_type_b:E});e=k,s=w,D.CoinAssist.destroyBalanceZero(s?v:h,s?E:R,A);let u=s?D.CoinAssist.fromBalance(h,R,A):D.CoinAssist.fromBalance(v,E,A);this.repay({txb:A,position_cap_id:q,repay_reserve_array_index:O[0].reserveArrayIndex.toString(),repay_coin:u,repay_coin_type:O[0].reserve.coinType,repay_amount:"0",market_id:W}),A.transferObjects([u],this._sdk.getSenderAddress())}let l=this.withdrawAsset({market_id:W,position_cap_id:q,withdraw_amount:D.U64_MAX.toString(),withdraw_reserve_array_index:T[0].reserveArrayIndex.toString(),withdraw_coin_type:b?N:I},A);await this.buildClaimRewardsMoveCall(p,W,q,A);let f;if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:895 ~ PositionModules ~ swap_convert_all:",F),F)f=await this._sdk.SwapModules.routerSwap({router:Z?.route_obj,input_coin:l,slippage:t,txb:A});else if(_&&(0,D.d)(_).gt(0)){let h=A.splitCoins(l,[A.pure.u64(_?.toString()||"0")]);f=await this._sdk.SwapModules.routerSwap({router:Z?.route_obj,input_coin:h,slippage:t,txb:A})}if(f&&e){let h=D.CoinAssist.mintBalanceZero(s?E:R,A),v=A.splitCoins(f,[A.pure.u64(x?.toString()||"0")]);this._sdk.SwapModules.repayFlashLoan({tx:A,clmm_pool:L,repay_base:s?D.CoinAssist.intoBalance(v,R,A):h,repay_quote:s?h:D.CoinAssist.intoBalance(v,E,A),clmm_pool_coin_type_a:R,clmm_pool_coin_type_b:E,receipt:e})}return A.moveCall({target:`${c.published_at}::router::close_position`,typeArguments:[M],arguments:[A.object(m),A.object(P),A.object(W),A.object(q),A.object(D.CLOCK_ADDRESS),A.object(g)]}),f&&A.transferObjects([f],this._sdk.getSenderAddress()),F||A.transferObjects([l],this._sdk.getSenderAddress()),console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:951 ~ PositionModules ~ tx:",A),A};this.positionLeverageIncrease=async i=>{let{current_leverage:r,target_leverage:o,swap_clmm_pool:t="",slippage:a,position_id:d}=i,{lending_market_id:c}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),m=new oe.Transaction,{flash_loan_amount:g,quote_token:M,base_token:P,reserve:A,deposits:T,borrow_amount:O,routers:N,position_cap_id:I,is_long:L,market_id:R,is_flash_loan:E}=await this.calculatePositionLeverage({position_id:d,current_leverage:r,target_leverage:o,swap_clmm_pool:t,slippage:a}),q=L?M:P;if(await this._sdk.SuiLendModule.refreshReservePrices(m,A),E){let{clmm_pool_coin_type_a:W,clmm_pool_coin_type_b:F,clmm_pool:Z}=await this._sdk.SwapModules.getFlashLoanPool(q,g?.toString()||""),{balance_a:_,balance_b:b,receipt:S,is_loan_a:C}=this._sdk.SwapModules.flashLoan({tx:m,amount:g?.toString()||"0",clmm_pool:Z,clmm_pool_coin_type_a:W,clmm_pool_coin_type_b:F,flash_loan_coin:q}),x=await this._sdk.SwapModules.routerSwap({router:N?.route_obj,input_coin:C?D.CoinAssist.fromBalance(_,W,m):D.CoinAssist.fromBalance(b,F,m),slippage:a,txb:m});this.depositToLeveragePosition({is_long:L,market_id:R,position_cap_id:I,deposit_reserve_array_index:T[0].reserveArrayIndex.toString(),input_coin:x,base_token:P,quote_token:M},m);let p=this.borrowAsset({position_cap_id:I,reserve_array_index:L?A[1].arrayIndex.toString():A[0].arrayIndex.toString(),borrow_amount:O,base_token:P,quote_token:M,is_long:L,lending_market_id:c,market_id:R},m),e=D.CoinAssist.mintBalanceZero(C?F:W,m);this._sdk.SwapModules.repayFlashLoan({tx:m,clmm_pool:Z,repay_base:C?D.CoinAssist.intoBalance(p,W,m):e,repay_quote:C?e:D.CoinAssist.intoBalance(p,F,m),receipt:S,clmm_pool_coin_type_a:W,clmm_pool_coin_type_b:F}),D.CoinAssist.destroyBalanceZero(C?b:_,C?F:W,m)}else{let W=this.borrowAsset({position_cap_id:I,reserve_array_index:L?A[1].arrayIndex.toString():A[0].arrayIndex.toString(),borrow_amount:O,base_token:P,quote_token:M,is_long:L,lending_market_id:c,market_id:R},m),F=await this._sdk.SwapModules.routerSwap({router:N?.route_obj,input_coin:W,slippage:a,txb:m});this.depositToLeveragePosition({is_long:L,market_id:R,position_cap_id:I,deposit_reserve_array_index:T[0].reserveArrayIndex.toString(),input_coin:F,base_token:P,quote_token:M},m)}return m};this.positionLeverageDecrease=async i=>{let{current_leverage:r,target_leverage:o,swap_clmm_pool:t="",slippage:a,position_id:d}=i,{deposits:c,borrows:m,position_cap_id:g,market_id:M,is_long:P}=await this.getPositionInfo(d),{base_token:A,quote_token:T}=await this._sdk.MarketModules.getMarketInfo(M),O=new oe.Transaction,{withdraw_ctoken_amount:N,routers:I,reserve:L,is_flash_loan:R}=await this.calculatePositionLeverage({position_id:d,current_leverage:r,target_leverage:o,swap_clmm_pool:t,slippage:a});if(await this._sdk.SuiLendModule.refreshReservePrices(O,L),R){let{clmm_pool_coin_type_a:E,clmm_pool_coin_type_b:q,clmm_pool:W,clmm_fee_tier:F}=await this._sdk.SwapModules.getFlashLoanPool(c[0].reserve.coinType,N.toString()),{balance_a:Z,balance_b:_,receipt:b,is_loan_a:S}=this._sdk.SwapModules.flashLoan({tx:O,amount:N,clmm_pool:W,clmm_pool_coin_type_a:E,clmm_pool_coin_type_b:q,flash_loan_coin:c[0].reserve.coinType}),C=await this._sdk.SwapModules.routerSwap({router:I?.route_obj,input_coin:S?D.CoinAssist.fromBalance(Z,E,O):D.CoinAssist.fromBalance(_,q,O),slippage:a,txb:O});this.repay({txb:O,market_id:M,position_cap_id:g,repay_reserve_array_index:m[0].reserveArrayIndex.toString(),repay_coin:C,repay_coin_type:P?T:A,repay_amount:"0"}),C&&O.transferObjects([C],this._sdk.getSenderAddress());let x=(0,D.d)(N).mul(F).toString(),p=(0,D.d)(N).add(x).toDP(0,gt.ROUND_UP),e=this.withdrawAsset({market_id:M,position_cap_id:g,withdraw_amount:p.toString(),withdraw_reserve_array_index:c[0].reserveArrayIndex.toString(),withdraw_coin_type:P?A:T},O),s=O.splitCoins(e,[p.toString()]);this._sdk.SwapModules.repayFlashLoan({tx:O,clmm_pool:W,repay_base:S?D.CoinAssist.intoBalance(s,E,O):Z,repay_quote:S?_:D.CoinAssist.intoBalance(s,q,O),receipt:b,clmm_pool_coin_type_a:E,clmm_pool_coin_type_b:q}),O.transferObjects([e],this._sdk.getSenderAddress())}else{let E=this.withdrawAsset({market_id:M,position_cap_id:g,withdraw_amount:N.toString(),withdraw_reserve_array_index:c[0].reserveArrayIndex.toString(),withdraw_coin_type:P?A:T},O),q=await this._sdk.SwapModules.routerSwap({router:I?.route_obj,input_coin:E,slippage:a,txb:O});this.repay({txb:O,market_id:M,position_cap_id:g,repay_reserve_array_index:m[0].reserveArrayIndex.toString(),repay_coin:q,repay_coin_type:P?T:A,repay_amount:"0"}),O.transferObjects([q],this._sdk.getSenderAddress())}return O};this.positionRepay=async i=>{let{position_id:r,amount:o,is_quote:t,slippage:a}=i,{routers:d,repay_coin_type:c,base_token:m,quote_token:g,borrows:M,deposits:P,market_id:A,position_cap_id:T}=await this.calculatePositionRepay({position_id:r,amount:o,is_quote:t}),O=this.extractOtherTokenTypes(P,M,m,g),{reserve:N}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(m,g,void 0,O),I=new oe.Transaction;await this._sdk.SuiLendModule.refreshReservePrices(I,N);let L=t?g!==c:m!==c,R;if(L){let E=D.CoinAssist.buildCoinWithBalance(BigInt(o.toString()),t?g:m,I);R=await this._sdk.SwapModules.routerSwap({router:d?.route_obj,input_coin:E,slippage:a,txb:I})}else R=D.CoinAssist.buildCoinWithBalance(BigInt(o.toString()),t?g:m,I);return this.repay({txb:I,position_cap_id:T,repay_reserve_array_index:M[0].reserveArrayIndex.toString(),repay_coin:R,repay_coin_type:c,repay_amount:"0",market_id:A}),I.transferObjects([R],this._sdk.getSenderAddress()),I};this.positionTopUpCToken=async i=>{let{position_id:r,amount:o,is_quote:t,swap_clmm_pool:a="",slippage:d}=i,c=new oe.Transaction,{is_long:m,market_id:g,position_cap_id:M,deposits:P,borrows:A}=await this.getPositionInfo(r),{base_token:T,quote_token:O}=await this._sdk.MarketModules.getMarketInfo(g),N=m&&t||!m&&!t,I=this.extractOtherTokenTypes(P,A,T,O),{base_reserve_array_index:L,quote_reserve_array_index:R,reserve:E}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(T,O,void 0,I);await this._sdk.SuiLendModule.refreshReservePrices(c,E);let q;if(N){let W=await this._sdk.SwapModules.findRouters(t?O:T,t?T:O,o,!0,[a]),F=D.CoinAssist.buildCoinWithBalance(BigInt(o.toString()),t?O:T,c);q=await this._sdk.SwapModules.routerSwap({router:W?.route_obj,input_coin:F,slippage:d,txb:c})}else q=D.CoinAssist.buildCoinWithBalance(BigInt(o.toString()),t?O:T,c);return await this.depositToLeveragePosition({is_long:m,market_id:g,position_cap_id:M,deposit_reserve_array_index:m?L:R,input_coin:q,base_token:T,quote_token:O},c),c};this.positionWithdrawCToken=async i=>{let{position_id:r,amount:o,is_quote:t,swap_clmm_pool:a="",slippage:d}=i,c=new oe.Transaction,{is_long:m,market_id:g,position_cap_id:M,deposits:P,borrows:A}=await this.getPositionInfo(r),{base_token:T,quote_token:O}=await this._sdk.MarketModules.getMarketInfo(g),N=m&&t||!m&&!t,I=this.extractOtherTokenTypes(P,A,T,O),{base_reserve_array_index:L,quote_reserve_array_index:R,reserve:E}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(T,O,void 0,I);await this._sdk.SuiLendModule.refreshReservePrices(c,E);let q=await this._sdk.SuiLendModule.getLatestPriceFeeds(E,!0),W=q[(0,D.removeHexPrefix)(O)]?.price,F=q[(0,D.removeHexPrefix)(T)]?.price,Z=m?(0,D.d)(W).div((0,D.d)(F)).toString():(0,D.d)(F).div((0,D.d)(W)).toString(),_=E[0].mintDecimals,b=E[1].mintDecimals,S=N?(0,D.d)(o).mul(Z).div(10**(t?b:_)).mul(10**(m?_:b)).toDP(0,gt.ROUND_DOWN).toString():o.toString(),C=this.withdrawAsset({market_id:g,position_cap_id:M,withdraw_amount:S,withdraw_reserve_array_index:m?L:R,withdraw_coin_type:m?T:O},c);if(N){let x=await this._sdk.SwapModules.findRouters(m?T:O,t?O:T,S,!0,[a]),p=await this._sdk.SwapModules.routerSwap({router:x?.route_obj,input_coin:C,slippage:d,txb:c});p&&c.transferObjects([p],this._sdk.getSenderAddress())}else c.transferObjects([C],this._sdk.getSenderAddress());return c};this.positionClaim=async i=>{let r=new oe.Transaction,{position_cap_id:o,obligation_owner_cap:t,market_id:a,claimable_rewards:d}=await this.getPositionInfo(i);return console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:1650 ~ PositionModules ~ claimable_rewards:",d),await this.buildClaimRewardsMoveCall(d,a,o,r),r};this.calculatePositionDeposit=async i=>{let{is_quote:r,is_long:o,amount:t,swap_clmm_pool:a="",leverage:d,by_amount_in:c=!0,base_token:m,quote_token:g,is_submit:M=!1,is_open:P=!0,position_id:A}=i,T=[],O=[],N=[];if(!P&&A){let{deposits:rt,borrows:V}=await this.getPositionInfo(A);O=rt,N=V,T=this.extractOtherTokenTypes(O,N,m,g)}let{reserve:I,base_reserve_array_index:L,quote_reserve_array_index:R}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(m,g,void 0,T),E=I[0].mintDecimals,q=I[1].mintDecimals,W=t.toString(),F=o&&r||!o&&!r,Z=await this._sdk.SuiLendModule.getLatestPriceFeedsByCoinTypes([m,g]),_=Z&&Z[g]?.price,b=Z&&Z[m]?.price;if(!_||!b)throw Yt("PriceNotFound","Price not found");if(F&&M){let rt=o?(0,D.d)(_).div((0,D.d)(b)).toString():(0,D.d)(b).div((0,D.d)(_)).toString();W=(0,D.d)(t).mul(rt).div(10**(r?q:E)).mul(10**(o?E:q)).toString(),console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:492 ~ PositionModules ~ initDepositAmount:",{initDepositAmount:W,rate:rt})}else if(F&&!M){let rt=o?g:m,V=o?m:g;W=(await this._sdk.SwapModules.findRouters(rt,V,W,!0,[a]))?.amount_out.toString()}else W=t;let{base_price:S,quote_price:C,flash_amount:x,flash_loan_fee:p,flash_loan_coin:e,is_flash_a:s,clmm_pool:l,clmm_fee_tier:f,clmm_pool_coin_type_a:h,clmm_pool_coin_type_b:v}=await this._sdk.SwapModules.calculateFlashLoan({is_long:o,leverage:d,base_token:m,quote_token:g,deposit_amount:W,reserve:I,base_token_decimal:E,quote_token_decimal:q}),k=await this._sdk.SwapModules.findRouters(o?g:m,o?m:g,x.toString(),!0,[a]),w=(0,D.d)(W).add((0,D.d)(k?.amount_out.toString())).toDP(0,gt.ROUND_DOWN).toString(),u=(0,D.d)(w).mul(o?S:C).toString(),y=(0,D.d)(x).add(p).toDP(0,gt.ROUND_UP).toString(),$=(0,D.d)(y).mul(o?C:S).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:1575 ~ PositionModules ~ depositAmount:",{depositAmount:w,borrowAmount:y});let U=r?(0,D.d)(t).div(10**(r?q:E)).mul(10**E).mul(d).div(S).toDP(0,gt.ROUND_DOWN).toString():(0,D.d)(t).mul(d).toDP(0,gt.ROUND_DOWN).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:1583 ~ PositionModules ~ leverageDepositAmount:",U);let j="0",K="0",G="0",J="0";return!P&&A&&(N&&N.length>0&&(j=(0,D.d)(N[0].borrowedAmount.toString()).mul(o?10**q:10**E).add((0,D.d)(y)).toString(),K=(0,D.d)(j).mul(o?_:b).toString()),G=(0,D.d)(O[0].depositedAmount.toString()).mul(o?10**E:10**q).add((0,D.d)(w)).toString(),J=(0,D.d)(G).mul(o?b:_).toString()),{routers:k,leverage_deposit_amount:U,quote_price:C,base_price:S,clmm_fee_tier:f,flash_loan_fee:p,flash_loan_coin:e,deposit_amount:w,borrow_amount:y,deposit_amount_usd:u,borrow_amount_usd:$,after_borrow_amount:j,after_borrow_amount_usd:K,after_deposit_amount:G,after_deposit_amount_usd:J,flash_amount:x,is_flash_a:s,init_deposit_amount:W,clmm_pool_coin_type_a:h,clmm_pool_coin_type_b:v,clmm_pool:l,base_reserve_array_index:L,quote_reserve_array_index:R,reserve:I,has_swap:F}};this.calculatePositionWithdraw=async i=>{let{position_id:r,is_quote:o,swap_clmm_pool:t="",amount:a,leverage:d,slippage:c,withdraw_max:m}=i,{deposits:g,borrows:M,origin_obligation:P,position_cap_id:A,market_id:T,is_long:O,claimable_rewards:N}=await this.getPositionInfo(r),{base_token:I,quote_token:L}=await this._sdk.MarketModules.getMarketInfo(T),R=r?this.extractOtherTokenTypes(g,M,I,L):[],{reserve:E,base_reserve_map_info:q,quote_reserve_map_info:W}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(I,L,void 0,R),F=E[0].mintDecimals,Z=E[1].mintDecimals,_=O?q.cTokenExchangeRate:W.cTokenExchangeRate,b=await this._sdk.SuiLendModule.getLatestPriceFeeds(E,!0),S=b[(0,D.removeHexPrefix)(L)]?.price,C=b[(0,D.removeHexPrefix)(I)]?.price,x=(0,D.d)(0),p;O?(x=(0,D.d)(o?(0,D.d)(a).mul(S).toString():(0,D.d)(a).mul(C).toString()).div(o?10**Z:10**F),p=o?(0,D.d)(a).div(C).div(10**Z).mul(10**F).toString():(0,D.d)(a)):(x=(0,D.d)(o?(0,D.d)(a).mul(S).toString():(0,D.d)(a).mul(C).toString()).div(o?10**Z:10**F),p=o?(0,D.d)(a):(0,D.d)(a).mul(C).div(10**F).mul(10**Z).toString());let e=(0,D.d)(g[0].depositedAmountUsd.toString()).sub((0,D.d)(M&&M.length>0?M[0].borrowedAmountUsd.toString():"0")).toString(),s=x.div((0,D.d)(e)),l=m||(0,D.d)(s).gte(1),f,h=new gt(0);P&&P.borrows&&P.borrows.length>0&&(f=(0,Mr.compoundDebt)(P.borrows[0],O?E[1]:E[0]),h=(0,D.d)(f.borrowedAmount.value.toString()).div(10**18).toDP(0,gt.ROUND_UP));let v=h.mul(l?1:s).toDP(0,gt.ROUND_UP).toString(),k=v.toString(),w,u,y,$,U,j;if((0,D.d)(v).gt(0)){let{clmm_pool_coin_type_a:nt,clmm_pool_coin_type_b:ot,clmm_fee_tier:de,clmm_pool:pt}=await this._sdk.SwapModules.getFlashLoanPool(O?L:I,k);U=nt||"",j=ot||"",y=de||0,$=pt||"",w=(0,D.d)(k).mul(de).toString(),u=(0,D.d)(k).mul((0,D.d)(1).add(de)).toDP(0,gt.ROUND_UP).toString()}let K=O&&o||!O&&!o,G,J;if(K){let nt=(0,D.d)(g[0].depositedAmount.toString()).mul(10**(O?F:Z)).mul(l?1:s).toDP(0,gt.ROUND_DOWN).toString();G=await this._sdk.SwapModules.findRouters(g[0].reserve.coinType,O?L:I,nt,!0,[t])}else{let ot=I==="0x0041f9f9344cac094454cd574e333c4fdb132d7bcc9379bcd4aab485b2a63942::wbtc::WBTC"||L==="0x0041f9f9344cac094454cd574e333c4fdb132d7bcc9379bcd4aab485b2a63942::wbtc::WBTC"?.01:.0025;J=(0,D.d)(v).mul(O?S:C).div(10**(O?Z:F)).mul(10**(O?F:Z)).div(O?C:S).mul((0,D.d)(1).add(ot)).toDP(0,gt.ROUND_DOWN).toString(),G=await this._sdk.SwapModules.findRouters(g[0].reserve.coinType,O?L:I,J.toString(),!0,[t]),l||(v=(0,D.d)(G?.amount_out.toString()).mul((0,D.d)(1).sub(c)).toDP(0,gt.ROUND_DOWN).toString())}p=(0,D.d)(p).mul(d).toDP(0,gt.ROUND_DOWN).toString(),x=(0,D.d)(x).mul(d);let rt=h.mul((0,D.d)(1).sub(s)).toString(),V=(0,D.d)(rt).mul(O?S:C).toString(),et=(0,D.d)(g[0].depositedAmount.toString()).mul(O?10**F:10**Z).sub((0,D.d)(p)).toString(),ft=(0,D.d)(et).mul(O?C:S).toString(),tt=(0,D.d)(G?.amount_in.toString()).toString(),X=(0,D.d)(G?.amount_out.toString()).toString(),me=(0,D.d)(p).div(_.toString()).toDP(0,gt.ROUND_DOWN).toString(),dt=(0,D.d)(V).div((0,D.d)(10**(O?Z:F))),at=(0,D.d)(ft).div((0,D.d)(10**(O?F:Z))),ke=!0;if(M.length>0){let nt=(0,D.d)(M[0].borrowedAmountUsd.toString()),ot=nt.div(at).toString(),de=(0,D.d)(g[0].reserve.config.openLtvPct).mul((0,D.d)(.95)).div(100).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:1459 ~ PositionModules ~ afterLTV:",{afterLTV:ot,ltv:de,afterDepositAmount:et,afterDepositAmountUSD:at,afterBorrowAmount:rt,afterBorrowAmountUSD:dt,withdrawAmount:p,currentBorrowAmountUSD:nt}),(0,D.d)(ot).lte(de)&&(ke=!1)}return{amount_in:tt,amount_out:X,from:g[0].reserve.coinType,to:O?I:L,after_borrow_amount:rt,after_borrow_amount_usd:V,after_deposit_amount:et,after_deposit_amount_usd:ft,flash_loan_fee:w,clmm_fee_tier:y,withdraw_amount:p,withdraw_ctoken_amount:me,repay_amount:v,ratio:s,is_close:l,is_long:O,deposits:g,borrows:M,reserve:E,base_token:I,quote_token:L,clmm_pool:$,clmm_pool_coin_type_a:U,clmm_pool_coin_type_b:j,position_cap_id:A,market_id:T,swap_convert_all:K,flash_loan_amount:k,routers:G,repay_flash_loan_amount:u,partial_amount_in:J,has_flash_loan:ke,claimable_rewards:N}};this.calculatePositionLeverage=async i=>{let{position_id:r,current_leverage:o,target_leverage:t,swap_clmm_pool:a=""}=i,d=(0,D.d)(t).sub((0,D.d)(o)),c=d.gt(0),{borrows:m,deposits:g,is_long:M,position_cap_id:P,market_id:A}=await this.getPositionInfo(r),{base_token:T,quote_token:O}=await this._sdk.MarketModules.getMarketInfo(A),N=this.extractOtherTokenTypes(g,m,T,O),{reserve:I}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(T,O,void 0,N),L=I[0].mintDecimals,R=I[1].mintDecimals,E=await this._sdk.SuiLendModule.getLatestPriceFeeds(I,!0),q=E[(0,D.removeHexPrefix)(O)]?.price,W=E[(0,D.removeHexPrefix)(T)]?.price,F=m.length>0,Z=g[0].depositedAmountUsd.toString(),_=g[0].depositedAmount.toString(),b=F?(0,D.d)(m[0].borrowedAmount.toString()).mul(10**(M?R:L)):(0,D.d)(0),S=F?m[0].borrowedAmountUsd.toString():(0,D.d)(0),C=F?(0,D.d)(m[0].reserve.config.borrowWeightBps.toString()).div(1e4).toString():"1",x=(0,D.d)(g[0].reserve.config.openLtvPct).mul((0,D.d)(.95)).div(100).div(C).toString(),p=(0,D.d)(Z).sub((0,D.d)(S)).mul(x).toString();if(c){let e=(0,D.d)(Z).div(o).mul(d),s=e.add(S).div(Z).toString(),l=e.div(M?q:W).mul(10**(M?R:L)).toDP(0,gt.ROUND_UP),f=(0,D.d)(s).gt(x),h=l,v=M?O:T,k=M?T:O,w=await this._sdk.SwapModules.findRouters(v,k,h.toString(),!0,[a]),u=(0,D.d)(w?.amount_out.toString()).toString(),y=(0,D.d)(u).mul(M?W:q).toString(),$,U,j,K,G,J="0",rt="0";if(f){let{clmm_pool_coin_type_a:X,clmm_pool_coin_type_b:me,clmm_pool:dt,clmm_fee_tier:at}=await this._sdk.SwapModules.getFlashLoanPool(v,h.toString());j=at,K=X,G=me,$=h.mul(j).toString(),U=M?K==O:G==T,J=(0,D.d)(h).add($).toDP(0,gt.ROUND_UP).toString(),rt=(0,D.d)(J).mul(M?q:W).toString()}else J=l.toString(),rt=e.toString();let V=(0,D.d)(_).add((0,D.d)(u).div(10**(M?L:R))).toString(),et=(0,D.d)(V).mul(M?W:q).toString(),ft=(0,D.d)(b).add((0,D.d)(J)).div(10**(M?R:L)).toString(),tt=(0,D.d)(ft).mul(M?q:W).toString();return{amount_in:w?.amount_in.toString(),amount_out:w?.amount_out.toString(),flash_loan_fee:$,deposit_amount:u,borrow_amount:J,deposit_amount_usd:y,borrow_amount_usd:rt,flash_loan_amount:h,base_token:T,quote_token:O,reserve:I,deposits:g,borrows:m,clmm_fee_tier:j,is_flash_loan:f,is_loan_a:U,clmm_pool_coin_type_a:K,clmm_pool_coin_type_b:G,after_deposit_amount:V,after_borrow_amount:ft,after_deposit_amount_usd:et,after_borrow_amount_usd:tt,position_cap_id:P,market_id:A,is_long:M,routers:w}}else{let s=(0,D.d)(Z).div(o).mul(d.abs()).div(M?q:W).mul(10**(M?R:L)).toDP(0,gt.ROUND_UP),l=await this._sdk.SwapModules.findRouters(g[0].reserve.coinType,m[0].reserve.coinType,s.toString(),!1,[a]),f=l?.amount_in.toString(),h=(0,D.d)(f).mul(M?W:q).div(10**(M?L:R)).toString(),v=(0,D.d)(Z).mul(x).toString(),k=(0,D.d)(S).div((0,D.d)(Z).sub((0,D.d)(h))).toString(),w=(0,D.d)(k).gt(x),u=s.toString(),y=(0,D.d)(u).mul(M?q:W).toString(),$=f,U=(0,D.d)($).mul(M?W:q).toString(),j,K="",G="";if(w){let{clmm_pool_coin_type_a:ft,clmm_pool_coin_type_b:tt,clmm_pool:X,clmm_fee_tier:me}=await this._sdk.SwapModules.getFlashLoanPool(m[0].reserve.coinType,u.toString());j=me,K=ft,G=tt}let J=(0,D.d)(_).sub((0,D.d)($).div(10**(M?L:R))).toString(),rt=(0,D.d)(J).mul(M?W:q).toString(),V=(0,D.d)(b).sub((0,D.d)(u)).div(10**(M?R:L)).toString(),et=(0,D.d)(V).mul(M?q:W).toString();return{amount_in:l?.amount_in.toString(),amount_out:l?.amount_out.toString(),deposit_amount:$,borrow_amount:u,deposit_amount_usd:U,borrow_amount_usd:y,base_token:T,quote_token:O,reserve:I,deposits:g,borrows:m,clmm_fee_tier:j,withdraw_ctoken_amount:f,is_flash_loan:w,clmm_pool_coin_type_a:K,clmm_pool_coin_type_b:G,after_deposit_amount:J,after_borrow_amount:V,after_deposit_amount_usd:rt,after_borrow_amount_usd:et,position_cap_id:P,market_id:A,is_long:M,routers:l}}};this.calculatePositionRepay=async i=>{let{position_id:r,amount:o,is_quote:t}=i,{borrows:a,deposits:d,is_long:c,market_id:m,position_cap_id:g}=await this._sdk.PositionModules.getPositionInfo(r),M=a[0].reserve.coinType,P=c?d[0].reserve.coinType:a[0].reserve.coinType,A=c?a[0].reserve.coinType:d[0].reserve.coinType,T,O=c?!t:t;return O&&(T=await this._sdk.SwapModules.findRouters(t?A:P,M,o,!0,[])),{routers:T,repay_coin_type:M,base_token:P,quote_token:A,is_long:c,borrows:a,deposits:d,market_id:m,position_cap_id:g,has_swap:O}};this._sdk=i}getObligationRewardsInfo(i,r){let o=[],t=new ct(10).pow(18),a={},d=Date.now();for(let m of r){let g=m.coinType,M=m.arrayIndex.toString(),P=m.depositsPoolRewardManager.poolRewards||[];for(let T of P){if(!T)continue;let O=T.id,N=T.coinType,I=parseInt(T.startTimeMs),L=parseInt(T.endTimeMs),R=d>=I&&d<L,E=T.mintDecimals,q=ct(T.cumulativeRewardsPerShare).times(10**E);a[O]={coinType:N,mintDecimals:E,isActive:R,reserveType:"deposit",reserveArrayIndex:M,reserveCoinType:g,globalCumulativePerShare:q,rewardIndex:T.rewardIndex,reserve:m}}let A=m.borrowsPoolRewardManager.poolRewards||[];for(let T of A){if(!T)continue;let O=T.id,N=T.coinType,I=parseInt(T.startTimeMs),L=parseInt(T.endTimeMs),R=d>=I&&d<L,E=T.mintDecimals,q=ct(T.cumulativeRewardsPerShare).times(10**E);a[O]={coinType:N,mintDecimals:E,isActive:R,reserveType:"borrow",reserveCoinType:g,reserveArrayIndex:M,rewardIndex:T.rewardIndex,globalCumulativePerShare:q,reserve:m}}}let c=i.userRewardManagers;for(let m of c)for(let g=0;g<m.rewards.length;g++){let M=m.rewards[g];if(!M)continue;let P=a[M.poolRewardId];if(!P)continue;let A=P.globalCumulativePerShare,T=new ct(M.cumulativeRewardsPerShare.value.toString()).div(t),O=new ct(m.share).times(A.minus(T)),N=new ct(M.earnedRewards.value).div(t),I=new ct(N).plus(O),L=P.reserve.price,R=I.div(10**P.mintDecimals),E=L?R.times(L).toFixed(12):0;(0,D.d)(E).gt(0)&&o.push({poolRewardId:M.poolRewardId,coinType:P.coinType,earnedRewards:I.toString(),earnedRewardsUsd:E,earnedRewardsFormatted:R.toFixed(12),isActive:P.isActive,reserveType:P.reserveType,reserveCoinType:P.reserveCoinType,reserveArrayIndex:P.reserveArrayIndex,rewardIndex:P.rewardIndex,rewardInfo:P})}return o}createLeveragePosition(i){let{lending_market_id:r,lending_market_type:o}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{global_config_id:t,versioned_id:a}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading),d=this._sdk.sdkOptions.margin_trading,{leverage:c,market_id:m,base_token:g,quote_token:M,is_long:P,init_deposit_amount:A,tx:T,init_coin_type:O}=i;return T.moveCall({target:`${d.published_at}::router::open_position`,arguments:[T.object(t),T.object(m),T.object(r),T.pure.u64(A),T.pure.u64((0,D.d)(c).mul(1e4).toString()),T.object(D.CLOCK_ADDRESS),T.object(a)],typeArguments:[o,P?g:M,O]})}async depositToLeveragePosition(i,r){let{margin_trading:o}=this._sdk.sdkOptions,{lending_market_type:t,lending_market_id:a}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.suilend),{is_long:d,market_id:c,position_cap_id:m,position_cap:g,deposit_reserve_array_index:M,input_coin:P,base_token:A,quote_token:T}=i,{global_config_id:O,versioned_id:N}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading);return r.moveCall({target:`${o.published_at}::router::deposit`,typeArguments:[t,d?A:T],arguments:[r.object(O),r.object(a),r.object(c),m?r.object(m):g,P,r.pure.u64(M),r.object(D.CLOCK_ADDRESS),r.object(N)]}),r}extractOtherTokenTypes(i,r,o,t){let a=[];return i.forEach(d=>{d.reserve.coinType!==o&&d.reserve.coinType!==t&&a.push(d.reserve.coinType)}),r.forEach(d=>{d.reserve.coinType!==o&&d.reserve.coinType!==t&&a.push(d.reserve.coinType)}),a}async buildClaimRewardsMoveCall(i,r,o,t){let{suilend:a,margin_trading:d}=this._sdk.sdkOptions,c=(0,D.getPackagerConfigs)(a).lending_market_id,m=(0,D.getPackagerConfigs)(a).lending_market_type,{global_config_id:g,versioned_id:M}=(0,D.getPackagerConfigs)(this._sdk.sdkOptions.margin_trading);for(let P=0;P<i.length;P++){let A=t.moveCall({target:`${d.published_at}::router::claim_rewards`,arguments:[t.object(g),t.object(c),t.object(r),t.object(o),t.pure.u64(i[P].reserveArrayIndex.toString()),t.pure.u64(i[P].rewardIndex.toString()),t.pure.bool(i[P].reserveType==="deposit"),t.object(D.CLOCK_ADDRESS),t.object(M)],typeArguments:[m,i[P].coinType]});t.transferObjects([A],this._sdk.getSenderAddress())}}};var Pr=require("@suilend/sdk/client"),it=require("@suilend/sdk");var fe=require("@suilend/sdk"),be=require("@suilend/sui-fe");var br=yi(require("crypto")),Je=new Uint8Array(256),Xe=Je.length;function Li(){return Xe>Je.length-16&&(br.default.randomFillSync(Je),Xe=0),Je.slice(Xe,Xe+=16)}var Vt=[];for(let n=0;n<256;++n)Vt.push((n+256).toString(16).slice(1));function yr(n,i=0){return Vt[n[i+0]]+Vt[n[i+1]]+Vt[n[i+2]]+Vt[n[i+3]]+"-"+Vt[n[i+4]]+Vt[n[i+5]]+"-"+Vt[n[i+6]]+Vt[n[i+7]]+"-"+Vt[n[i+8]]+Vt[n[i+9]]+"-"+Vt[n[i+10]]+Vt[n[i+11]]+Vt[n[i+12]]+Vt[n[i+13]]+Vt[n[i+14]]+Vt[n[i+15]]}var kr=yi(require("crypto")),Ni={randomUUID:kr.default.randomUUID};function qn(n,i,r){if(Ni.randomUUID&&!i&&!n)return Ni.randomUUID();n=n||{};let o=n.random||(n.rng||Li)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,i){r=r||0;for(let t=0;t<16;++t)i[r+t]=o[t];return i}return yr(o)}var Ye=qn;var Sr=require("@cetusprotocol/common-sdk"),Qe=10n;function Ii(n){return n.depositedAmount.eq(0)?new ct(0):n.borrowedAmount.div(n.depositedAmount).times(100)}function ti(n){let i=Ii(n);if(!i.gt(100))return(0,fe.linearlyInterpolate)(n.config.interestRate,"utilPercent","aprPercent",i)}function Ei(n){let i=Ii(n),r=ti(n);if(!(r===void 0||i.gt(100)))return new ct(i.div(100)).times(r.div(100)).times(1-n.config.spreadFeeBps/1e4).times(100)}function Fn(n,i,r,o){let{api_url:t}=(0,Sr.getPackagerConfigs)(o.sdkOptions.suilend);n===fe.Side.DEPOSIT&&([be.NORMALIZED_flSUI_COINTYPE,be.NORMALIZED_jugSUI_COINTYPE].includes(i.coinType)&&r.push({stats:{id:Ye(),isActive:!0,rewardIndex:-1,reserve:i,rewardCoinType:"LIQ_AG",mintDecimals:0,symbol:"LiqAg Points",iconUrl:`${t}/partners/LiqAg.png`,perDay:new ct(.036),side:fe.Side.DEPOSIT},obligationClaims:{}}),i.coinType===be.NORMALIZED_LBTC_COINTYPE&&r.push({stats:{id:Ye(),isActive:!0,rewardIndex:-1,reserve:i,rewardCoinType:"LOMBARD",mintDecimals:0,symbol:"3x Lombard Lux",iconUrl:`${t}/partners/Lombard Lux.png`,perDay:new ct(0),side:fe.Side.DEPOSIT},obligationClaims:{}}))}function Un(n,i,r){return{...n,depositedAmount:i===fe.Side.DEPOSIT?ct.max(n.depositedAmount.plus(r),0):n.depositedAmount,borrowedAmount:i===fe.Side.BORROW?ct.max(n.borrowedAmount.plus(r),0):n.borrowedAmount}}function jn(n,i,r){let o=n===fe.Side.DEPOSIT?i.depositedAmount:i.borrowedAmount,t=n===fe.Side.DEPOSIT?r.depositedAmount:r.borrowedAmount,a=t.eq(0)?new ct(-1):o.div(t);return{multiplier:a,isValid:!a.eq(-1)}}function $n(n,i,r,o){let t=n.map(d=>({...d,stats:{...d.stats,perDay:o?d.stats.perDay.times(r):void 0}})),a=i.map(d=>({...d,stats:{...d.stats,aprPercent:o?d.stats.aprPercent.times(r):void 0}}));return{newPerDayRewards:t,newAprRewards:a}}function Bi(n,i){if(!Qe)throw new Error("oraclePriceMultiplierDecimal is required");if(n===0n)throw new Error("Invalid oracle price");if(i<0n){let r=10n**(Qe- -i);return n*r}return n/10n**(i+Qe)}function Wn(n){return(0,be.formatLtvPercent)(new ct(n))}var Jt=require("@cetusprotocol/common-sdk"),qi=require("@mysten/sui/utils");var ei=class{constructor(i){this.getCoinAprByAmount=async(i,r,o)=>{let t=new ct(r.toString()),{allLendingMarketData:a,lstAprPercentMap:d}=await this.getLendingMarketData(!1);if(await this.getInitializeObligations({allLendingMarketData:a,lstAprPercentMap:d},!0),!a||!d)throw new Error("lendingMarketCache is not initialized");let c=Object.values(a??{})[0],m=c.lendingMarket.reserves.find(_=>_.coinType===i);if(!m)throw new Error("reserve not found");let M=(this.obligationsCache?.[c.lendingMarket.id]).rewardMap[m.token.coinType]?.[o]??[],P=(0,it.getFilteredRewards)(M),A=(0,it.getStakingYieldAprPercent)(o,m.coinType,d),T=o===it.Side.DEPOSIT?m.depositAprPercent:m.borrowAprPercent,O=T,N=new ct(1),I=!0;if(o!==void 0&&t!==void 0&&t.gt(0)){let _={...m,depositedAmount:o===it.Side.DEPOSIT?ct.max(m.depositedAmount.plus(o===it.Side.DEPOSIT?t:t.negated()),0):m.depositedAmount,borrowedAmount:o===it.Side.BORROW?ct.max(m.borrowedAmount.plus(o===it.Side.BORROW?t:t.negated()),0):m.borrowedAmount};O=o===it.Side.DEPOSIT?Ei(_):ti(_);let b=o===it.Side.DEPOSIT?m.depositedAmount:m.borrowedAmount,S=o===it.Side.DEPOSIT?_.depositedAmount:_.borrowedAmount;N=S.eq(0)?new ct(-1):b.div(S),I=!N.eq(-1)}let E=(0,it.getDedupedPerDayRewards)(P).map(_=>({..._,stats:{..._.stats,perDay:I?_.stats.perDay.times(N):void 0}})),W=(0,it.getDedupedAprRewards)(P).map(_=>({..._,stats:{..._.stats,aprPercent:I?_.stats.aprPercent.times(N):void 0}})),F=(0,it.getTotalAprPercent)(o,T,P,A),Z=O===void 0||W.some(_=>_.stats.aprPercent===void 0)?void 0:(0,it.getTotalAprPercent)(o,O,W,A);return console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:315 ~ SuiLendModule ~ newTotalAprPercent:",Z?.toString()),{new_total_apr_percent:Z?.toString(),total_apr_percent:F.toString()}};this.refreshReservePrice=async(i,r,o)=>{let{lending_market_id:t,lending_market_type:a}=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend),d=`${t}_${a}`;if(!this.suilendClientCache)throw new Error("suilendClientCache is not initialized");let c=await this.suilendClientCache[d].pythClient.getPriceFeedObjectId(r);this.suilendClientCache[d].refreshReservePrices(i,c,o)};this.getLatestPriceFeeds=async(i,r=!1)=>{let{lending_market_id:o,lending_market_type:t}=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend),a={},d=[];if(i.forEach(M=>{let P=this.sdk.getCache(`getLatestPrice_${M.coinType.name}`,r);P&&this.priceCheck(P,60)?a[M.coinType.name]=P:d.push(M.coinType.name)}),d.length==0)return a;let c=`${o}_${t}`;if(!this.suilendClientCache)throw new Error("suilendClient is not initialized");let m=Array.from(new Set(i.map(M=>M?.priceIdentifier?.bytes?(0,qi.toHEX)(new Uint8Array(M.priceIdentifier.bytes)):(0,Jt.removeHexPrefix)(M.priceIdentifier.toString()))));return(await this.suilendClientCache[c].pythConnection.getLatestPriceFeeds(m))?.forEach((M,P)=>{let A=M.getPriceUnchecked(),T=i[P];if(A){let{price:O,expo:N}=A,I=(0,Jt.d)(O).mul((0,Jt.d)(10).pow((0,Jt.d)(N))).toString(),L={coin_type:T.coinType.name||T.coinType,price:I,oracle_price:0n,last_update_time:A.publishTime};L.oracle_price=Bi(BigInt(O),BigInt(N)),a[T.coinType.name||T.coinType]=L,this.sdk.updateCache(`getLatestPrice_${L.coin_type}`,L)}}),a};this.getLatestPriceFeedsByCoinTypes=async(i,r=!1)=>{try{let{lending_market_id:o}=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend),{allLendingMarketData:t}=await this.getLendingMarketData(!1),a=[];return i.forEach(c=>{let m=t[o].reserveMap[c];a.push(m)}),await this.getLatestPriceFeeds(a,r)}catch(o){Yt("FetchError",o);return}};this.getSuiLendReserveInfo=async(i,r,o,t)=>{let{lending_market_id:a}=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend);o||(o=(await this.getLendingMarketData(!1)).allLendingMarketData);let d=o[a].reserveMap[i],c=o[a].reserveMap[r],m=o[a].refreshedRawReserves.filter(T=>T.coinType.name==(0,Jt.removeHexPrefix)(i)),g=o[a].refreshedRawReserves.filter(T=>T.coinType.name==(0,Jt.removeHexPrefix)(r)),M=o[a].reserveMap[i],P=o[a].reserveMap[r],A=[];return t&&t.forEach(T=>{let O=o[a].refreshedRawReserves.filter(N=>N.coinType.name==(0,Jt.removeHexPrefix)(T));A.push(...O)}),{base_reserve_array_index:d.arrayIndex.toString(),quote_reserve_array_index:c.arrayIndex.toString(),reserve:[m[0],g[0],...A],base_reserve_map_info:M,quote_reserve_map_info:P}};this.refreshReservePrices=async(i,r)=>{for(let o=0;o<r.length;o++){let t=(0,qi.toHEX)(new Uint8Array(r[o].priceIdentifier.bytes));await this.refreshReservePrice(i,t,BigInt(r[o].arrayIndex))}};this.refreshReservePricesV2=async(i,r)=>{for(let o=0;o<r.length;o++){let t=r[o].priceIdentifier.toString();await this.refreshReservePrice(i,t,BigInt(r[o].arrayIndex))}};this.sdk=i}async getSuilendClient(i,r){let o=`${i}_${r}`;return this.suilendClientCache||(this.suilendClientCache={}),this.suilendClientCache[o]||(this.suilendClientCache[o]=await Pr.SuilendClient.initialize(i,r,this.sdk.FullClient,!0)),this.suilendClientCache[o]}async getLendingMarketData(i=!0){let r="lendingMarketData",o=this.sdk.getCache(r,i);if(o)return o;let t=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend)?.lending_market||[];if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:67 ~ SuiLendModule ~ getLendingMarketData ~ lendingMarkets:",t),!t.length)return console.warn("\u26A0\uFE0F No lending markets configured."),{};try{let[a,d]=await Promise.all([(async()=>Object.fromEntries(await Promise.all(t.map(async m=>{let g=await this.getSuilendClient(m.id,m.type),{lendingMarket:M,coinMetadataMap:P,refreshedRawReserves:A,reserveMap:T,reserveCoinTypes:O,reserveCoinMetadataMap:N,rewardCoinTypes:I,activeRewardCoinTypes:L,rewardCoinMetadataMap:R}=await(0,it.initializeSuilend)(this.sdk.FullClient,g),{rewardPriceMap:E}=await(0,it.initializeSuilendRewards)(T,L);return[m.id,{suilendClient:g,lendingMarket:M,coinMetadataMap:P,refreshedRawReserves:A,reserveMap:T,reserveCoinTypes:O,reserveCoinMetadataMap:N,rewardPriceMap:E,rewardCoinTypes:I,activeRewardCoinTypes:L,rewardCoinMetadataMap:R}]}))))(),(async()=>{try{let c=await fetch(`${this.sdk.sdkOptions?.suilend?.config?.api_url}/springsui/apy`),m=await c.json();if(c?.statusCode===500)throw new Error("Failed to fetch SpringSui LST APRs");return Object.fromEntries(Object.entries(m).map(([g,M])=>[g,new ct(M)]))}catch(c){return console.error(c),{}}})()]);return this.sdk.updateCache(r,{allLendingMarketData:a,lstAprPercentMap:d},30*1e3),{allLendingMarketData:a,lstAprPercentMap:d}}catch(a){throw console.log("\u{1F680} getLendingMarketData error:",a),a}}async getInitializeObligations(i,r=!1){if(!r&&this.obligationsCache)return this.obligationsCache;i||(i=await this.getLendingMarketData(!1));let o={};for(let t of Object.values(i.allLendingMarketData)){let{obligationOwnerCaps:a,obligations:d}=await(0,it.initializeObligations)(this.sdk.FullClient,t.suilendClient,t.refreshedRawReserves,t.reserveMap,this.sdk.senderAddress);d.length>0&&console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:181 ~ SuiLendModule ~ getInitializeObligations ~ obligations:",d[0].original.allowedBorrowValueUsd.value.toString());let c=(0,it.formatRewards)(t.reserveMap,t.rewardCoinMetadataMap,t.rewardPriceMap,d);o[t.lendingMarket.id]={obligationOwnerCaps:a,obligations:d,rewardMap:c}}return this.obligationsCache=o,this.obligationsCache}async getSuiLendUserData(i,r=!1){let{allLendingMarketData:o,lstAprPercentMap:t}=await this.getLendingMarketData(!1),a=await this.getInitializeObligations({allLendingMarketData:o,lstAprPercentMap:t},!1),{lending_market_id:d}=(0,Jt.getPackagerConfigs)(this.sdk.sdkOptions?.suilend),c=a[d];if(!c)throw new Error(`No user data found for lending market: ${d}`);let m=c.obligations.find(M=>M.id===i);if(!m)throw new Error(`Obligation not found: ${i}`);let g=(0,it.getNetAprPercent)(m,c.rewardMap,t??{});return{obligation:m,netAprPercent:g,deposits:m.deposits,borrowedAmount:m.borrowedAmount,depositedAmount:m.depositedAmount,netValueUsd:m.netValueUsd,rewardMap:c.rewardMap}}priceCheck(i,r=60){let o=Math.floor(Date.now()/1e3);if(!(Math.abs(o-i.last_update_time)>r))return i}};var xr=require("@mysten/sui/transactions"),Ui=yi(Cr()),lt=require("@cetusprotocol/common-sdk");var ii=class{constructor(i){this.routerSwap=async i=>{let{slippage:r,txb:o,input_coin:t,router:a}=i,d=o||new xr.Transaction;if(a)return await this._sdk.AggregatorClient.routerSwap({router:a,inputCoin:t,slippage:r,txb:d})};this.flashLoan=i=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:183 ~ SwapModules ~ params:",i);let{clmm_pool:r}=this._sdk.ClmmSDK.sdkOptions,{global_config_id:o}=(0,lt.getPackagerConfigs)(this._sdk.ClmmSDK.sdkOptions.clmm_pool),{amount:t,clmm_pool:a,clmm_pool_coin_type_a:d,clmm_pool_coin_type_b:c,flash_loan_coin:m,tx:g}=i,M=(0,lt.addHexPrefix)(d)===m;console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:188 ~ SwapModules ~ isLoanA:",M);let[P,A,T]=g.moveCall({target:`${r.published_at}::pool::flash_loan`,arguments:[g.object(o),g.object(a),g.pure.bool(M),g.pure.u64(t.toString())],typeArguments:[d,c]});return{balance_a:P,balance_b:A,receipt:T,is_loan_a:M,loan_coin_type:M?d:c}};this.getFlashLoanPool=async(i,r)=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:201 ~ SwapModules ~ flash_amount:",r,i);try{let o=await fetch(`https://api-sui.cetus.zone/v3/sui/margin_trading/pools?coin_type=${i}&min_amount=${r}`),{data:t}=await o.json();return console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:206 ~ SwapModules ~ data:",t),t&&t.length>0?{clmm_pool_coin_type_a:t[0].coin_type_a,clmm_pool_coin_type_b:t[0].coin_type_b,clmm_pool:t[0].pool_id,clmm_fee_tier:(0,lt.d)(t[0].fee_rate).div(1e6).toString()}:(Yt("FlashLoanPoolNotFound","FlashLoanPoolNotFound"),{clmm_pool_coin_type_a:"",clmm_pool_coin_type_b:"",clmm_pool:"",clmm_fee_tier:""})}catch(o){return Yt("FetchError",o),{clmm_pool_coin_type_a:"",clmm_pool_coin_type_b:"",clmm_pool:"",clmm_fee_tier:""}}};this.repayFlashLoan=i=>{let{clmm_pool:r}=this._sdk.ClmmSDK.sdkOptions,{global_config_id:o}=(0,lt.getPackagerConfigs)(this._sdk.ClmmSDK.sdkOptions.clmm_pool),{tx:t,repay_base:a,repay_quote:d,receipt:c,clmm_pool:m,clmm_pool_coin_type_a:g,clmm_pool_coin_type_b:M}=i;return t.moveCall({target:`${r.published_at}::pool::repay_flash_loan`,arguments:[t.object(o),t.object(m),a,d,c],typeArguments:[g,M]}),t};this._sdk=i}async findRouters(i,r,o,t,a){try{let d={from:i,target:r,amount:new Ui.default((0,lt.d)(o).toFixed(0).toString()),byAmountIn:t,depth:3},c=await this._sdk.AggregatorClient.findRouters(d);if(c?.error?.code===10001)return{...c,is_exceed:c.insufficientLiquidity};if(c?.insufficientLiquidity)return{...c,is_exceed:c.insufficientLiquidity};if(!c?.paths||c?.paths?.length===0)throw Error("Aggregator no router");return{amount_in:c.amountIn.toString(),amount_out:c.amountOut.toString(),is_exceed:c.insufficientLiquidity,route_obj:c,by_amount_in:!0,origin_res:c}}catch{try{if(a){let c=await this._sdk.AggregatorClient.swapInPools({from:i,target:r,amount:new Ui.default((0,lt.d)(o).toFixed(0).toString()),byAmountIn:t,pools:a});return c?{amount_in:c.routeData.amountIn.toString(),amount_out:c.routeData.amountOut.toString(),is_exceed:c.isExceed,route_obj:c.routeData,by_amount_in:!0,origin_res:c}:null}return null}catch{return null}}}async handleSwapLogic(i){let{is_long:r,is_quote:o,amount:t,base_token:a,quote_token:d,swap_clmm_pool:c="",slippage:m,tx:g}=i,M=r&&o||!r&&!o,P,A,T=t;if(M){let O=r&&o?d:a,N=r&&o?a:d,I=await this.findRouters(O,N,t,!0,[c]);if(T=I?.amount_out.toString(),I){let L=lt.CoinAssist.buildCoinWithBalance(BigInt(t),O,g);A=await this.routerSwap({router:I.route_obj,slippage:m,input_coin:L,txb:g}),P=A}}else P=lt.CoinAssist.buildCoinWithBalance(BigInt(t.toString()),o?d:a,g);return{has_swap:M,input_coin:P,init_deposit_amount:T}}async handleSwap(i){let{swap_clmm_pool:r="",slippage:o,amount:t,from:a,to:d,tx:c,input_coin:m}=i,g=await this.findRouters(a,d,t,!0,[r]),M=g.amount_out.toString();return{swap_out_coin:await this.routerSwap({router:g.route_obj,slippage:o,input_coin:m||lt.CoinAssist.buildCoinWithBalance(BigInt(t),a,c),txb:c}),amount_out:M}}async calculateFlashLoan(i){let{is_long:r,leverage:o,base_token:t,quote_token:a,deposit_amount:d,reserve:c,base_token_decimal:m,quote_token_decimal:g}=i,M=await this._sdk.SuiLendModule.getLatestPriceFeeds(c),P=M&&M[(0,lt.removeHexPrefix)(a)]?.price,A=M&&M[(0,lt.removeHexPrefix)(t)]?.price,T=r?(0,lt.d)(A).div((0,lt.d)(P)).toString():(0,lt.d)(P).div((0,lt.d)(A)).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:267 ~ SwapModules ~ calculateFlashLoan ~ deposit_amount:",d,T,m,g);let O=(0,lt.d)(d).mul((0,lt.d)(o).sub(1)).mul(T).div(10**(r?m:g)).mul(10**(r?g:m)).toDP(0,gt.ROUND_UP).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:274 ~ SwapModules ~ calculateFlashLoan ~ flashAmount:",(0,lt.d)(d).mul((0,lt.d)(o).sub(1)).mul(T).div(10**(r?m:g)).mul(10**(r?g:m)).toString(),O);let N=r?a:t,{clmm_pool_coin_type_a:I,clmm_fee_tier:L,clmm_pool:R,clmm_pool_coin_type_b:E}=await this.getFlashLoanPool(N,O),q=I===N,W=(0,lt.d)(O).mul(L).toString();return{flash_amount:O,rate:T,quote_price:P,base_price:A,flash_loan_coin:N,is_flash_a:q,flash_loan_fee:W,clmm_pool:R,clmm_pool_coin_type_a:I,clmm_pool_coin_type_b:E,clmm_fee_tier:L}}};var Te=class n extends Rr.SdkWrapper{constructor(i,r){super(i),this._suilendModule=new ei(this),this._permissionModules=new qe(this),this._marketModules=new Be(this),this._positionModules=new Ve(this),this._swapModules=new ii(this),this._aggregatorClient=new De.AggregatorClient({signer:(0,Tr.normalizeSuiAddress)("0x0"),client:i.sui_client||new Dr.SuiClient({url:i.full_rpc_url}),env:i.env==="testnet"?De.Env.Testnet:De.Env.Mainnet}),this._clmmSDK=r||Lr.CetusClmmSDK.createSDK({env:i.env,full_rpc_url:i.full_rpc_url}),this._pythConnection=new Or.SuiPriceServiceConnection("https://hermes.pyth.network",{timeout:30*1e3})}get SuiLendModule(){return this._suilendModule}get PermissionModules(){return this._permissionModules}get MarketModules(){return this._marketModules}get PositionModules(){return this._positionModules}get SwapModules(){return this._swapModules}get AggregatorClient(){return this._aggregatorClient}get ClmmSDK(){return this._clmmSDK}get PythConnection(){return this._pythConnection}static createSDK(i){return n.createCustomSDK({...ki,...i})}static createCustomSDK(i){return new n(i)}};var Zn=Te;0&&(module.exports={CetusMarginTradingSDK,appendExtraRewards,calculateBorrowAprPercent,calculateDepositAprPercent,calculateUtilizationPercent,formatLtvPercentValue,getPriceWithFormattedDecimals,getRewardsAprMultiplier,getUpdatedReserve,margin_trading_mainnet,mergePositionData,oraclePriceMultiplierDecimal,updateRewardsWithMultiplier,wrapMarketInfo,wrapMarketPermissions,wrapPosition});
2
2
  /*! Bundled license information:
3
3
 
4
4
  decimal.js/decimal.mjs: