@cetusprotocol/margin-sdk 0.0.2 → 0.0.3

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.mjs DELETED
@@ -1,13 +0,0 @@
1
- var jr=Object.create;var rr=Object.defineProperty;var $r=Object.getOwnPropertyDescriptor;var Wr=Object.getOwnPropertyNames;var Zr=Object.getPrototypeOf,Vr=Object.prototype.hasOwnProperty;var Kr=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(r,i)=>(typeof require<"u"?require:r)[i]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var zr=(n,r)=>()=>(r||n((r={exports:{}}).exports,r),r.exports);var Gr=(n,r,i,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of Wr(r))!Vr.call(n,t)&&t!==i&&rr(n,t,{get:()=>r[t],enumerable:!(o=$r(r,t))||o.enumerable});return n};var Hr=(n,r,i)=>(i=n!=null?jr(Zr(n)):{},Gr(r||!n||!n.__esModule?rr(i,"default",{value:n,enumerable:!0}):i,n));var Br=zr((Ir,$i)=>{"use strict";(function(n,r){"use strict";function i(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:r.BN=t,t.BN=t,t.wordSize=26;var a;try{typeof window<"u"&&typeof window.Buffer<"u"?a=window.Buffer:a=Kr("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,u){if(typeof e=="number")return this._initNumber(e,s,u);if(typeof e=="object")return this._initArray(e,s,u);s==="hex"&&(s=16),i(s===(s|0)&&s>=2&&s<=36),e=e.toString().replace(/\s+/g,"");var d=0;e[0]==="-"&&(d++,this.negative=1),d<e.length&&(s===16?this._parseHex(e,d,u):(this._parseBase(e,s,d),u==="le"&&this._initArray(this.toArray(),s,u)))},t.prototype._initNumber=function(e,s,u){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):(i(e<9007199254740992),this.words=[e&67108863,e/67108864&67108863,1],this.length=3),u==="le"&&this._initArray(this.toArray(),s,u)},t.prototype._initArray=function(e,s,u){if(i(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 d=0;d<this.length;d++)this.words[d]=0;var h,M,P=0;if(u==="be")for(d=e.length-1,h=0;d>=0;d-=3)M=e[d]|e[d-1]<<8|e[d-2]<<16,this.words[h]|=M<<P&67108863,this.words[h+1]=M>>>26-P&67108863,P+=24,P>=26&&(P-=26,h++);else if(u==="le")for(d=0,h=0;d<e.length;d+=3)M=e[d]|e[d+1]<<8|e[d+2]<<16,this.words[h]|=M<<P&67108863,this.words[h+1]=M>>>26-P&67108863,P+=24,P>=26&&(P-=26,h++);return this._strip()};function f(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;i(!1,"Invalid character in "+p)}function c(p,e,s){var u=f(p,s);return s-1>=e&&(u|=f(p,s-1)<<4),u}t.prototype._parseHex=function(e,s,u){this.length=Math.ceil((e.length-s)/6),this.words=new Array(this.length);for(var d=0;d<this.length;d++)this.words[d]=0;var h=0,M=0,P;if(u==="be")for(d=e.length-1;d>=s;d-=2)P=c(e,s,d)<<h,this.words[M]|=P&67108863,h>=18?(h-=18,M+=1,this.words[M]|=P>>>26):h+=8;else{var w=e.length-s;for(d=w%2===0?s+1:s;d<e.length;d+=2)P=c(e,s,d)<<h,this.words[M]|=P&67108863,h>=18?(h-=18,M+=1,this.words[M]|=P>>>26):h+=8}this._strip()};function m(p,e,s,u){for(var d=0,h=0,M=Math.min(p.length,s),P=e;P<M;P++){var w=p.charCodeAt(P)-48;d*=u,w>=49?h=w-49+10:w>=17?h=w-17+10:h=w,i(w>=0&&h<u,"Invalid character"),d+=h}return d}t.prototype._parseBase=function(e,s,u){this.words=[0],this.length=1;for(var d=0,h=1;h<=67108863;h*=s)d++;d--,h=h/s|0;for(var M=e.length-u,P=M%d,w=Math.min(M,M-P)+u,l=0,y=u;y<w;y+=d)l=m(e,y,y+d,s),this.imuln(h),this.words[0]+l<67108864?this.words[0]+=l:this._iaddn(l);if(P!==0){var $=1;for(l=m(e,y,e.length,s),y=0;y<P;y++)$*=s;this.imuln($),this.words[0]+l<67108864?this.words[0]+=l:this._iaddn(l)}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 v(p,e){p.words=e.words,p.length=e.length,p.negative=e.negative,p.red=e.red}if(t.prototype._move=function(e){v(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")]=_}catch{t.prototype.inspect=_}else t.prototype.inspect=_;function _(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var k=["","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"],S=[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],D=[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 u;if(e===16||e==="hex"){u="";for(var d=0,h=0,M=0;M<this.length;M++){var P=this.words[M],w=((P<<d|h)&16777215).toString(16);h=P>>>24-d&16777215,d+=2,d>=26&&(d-=26,M--),h!==0||M!==this.length-1?u=k[6-w.length]+w+u:u=w+u}for(h!==0&&(u=h.toString(16)+u);u.length%s!==0;)u="0"+u;return this.negative!==0&&(u="-"+u),u}if(e===(e|0)&&e>=2&&e<=36){var l=S[e],y=D[e];u="";var $=this.clone();for($.negative=0;!$.isZero();){var q=$.modrn(y).toString(e);$=$.idivn(y),$.isZero()?u=q+u:u=k[l-q.length]+q+u}for(this.isZero()&&(u="0"+u);u.length%s!==0;)u="0"+u;return this.negative!==0&&(u="-"+u),u}i(!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&&i(!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,u){this._strip();var d=this.byteLength(),h=u||Math.max(1,d);i(d<=h,"byte array longer than desired length"),i(h>0,"Requested array length <= 0");var M=O(e,h),P=s==="le"?"LE":"BE";return this["_toArrayLike"+P](M,d),M},t.prototype._toArrayLikeLE=function(e,s){for(var u=0,d=0,h=0,M=0;h<this.length;h++){var P=this.words[h]<<M|d;e[u++]=P&255,u<e.length&&(e[u++]=P>>8&255),u<e.length&&(e[u++]=P>>16&255),M===6?(u<e.length&&(e[u++]=P>>24&255),d=0,M=0):(d=P>>>24,M+=2)}if(u<e.length)for(e[u++]=d;u<e.length;)e[u++]=0},t.prototype._toArrayLikeBE=function(e,s){for(var u=e.length-1,d=0,h=0,M=0;h<this.length;h++){var P=this.words[h]<<M|d;e[u--]=P&255,u>=0&&(e[u--]=P>>8&255),u>=0&&(e[u--]=P>>16&255),M===6?(u>=0&&(e[u--]=P>>24&255),d=0,M=0):(d=P>>>24,M+=2)}if(u>=0)for(e[u--]=d;u>=0;)e[u--]=0},Math.clz32?t.prototype._countBits=function(e){return 32-Math.clz32(e)}:t.prototype._countBits=function(e){var s=e,u=0;return s>=4096&&(u+=13,s>>>=13),s>=64&&(u+=7,s>>>=7),s>=8&&(u+=4,s>>>=4),s>=2&&(u+=2,s>>>=2),u+s},t.prototype._zeroBits=function(e){if(e===0)return 26;var s=e,u=0;return(s&8191)===0&&(u+=13,s>>>=13),(s&127)===0&&(u+=7,s>>>=7),(s&15)===0&&(u+=4,s>>>=4),(s&3)===0&&(u+=2,s>>>=2),(s&1)===0&&u++,u},t.prototype.bitLength=function(){var e=this.words[this.length-1],s=this._countBits(e);return(this.length-1)*26+s};function R(p){for(var e=new Array(p.bitLength()),s=0;s<e.length;s++){var u=s/26|0,d=s%26;e[s]=p.words[u]>>>d&1}return e}t.prototype.zeroBits=function(){if(this.isZero())return 0;for(var e=0,s=0;s<this.length;s++){var u=this._zeroBits(this.words[s]);if(e+=u,u!==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 i((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 u=0;u<s.length;u++)this.words[u]=this.words[u]&e.words[u];return this.length=s.length,this._strip()},t.prototype.iand=function(e){return i((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,u;this.length>e.length?(s=this,u=e):(s=e,u=this);for(var d=0;d<u.length;d++)this.words[d]=s.words[d]^u.words[d];if(this!==s)for(;d<s.length;d++)this.words[d]=s.words[d];return this.length=s.length,this._strip()},t.prototype.ixor=function(e){return i((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){i(typeof e=="number"&&e>=0);var s=Math.ceil(e/26)|0,u=e%26;this._expand(s),u>0&&s--;for(var d=0;d<s;d++)this.words[d]=~this.words[d]&67108863;return u>0&&(this.words[d]=~this.words[d]&67108863>>26-u),this._strip()},t.prototype.notn=function(e){return this.clone().inotn(e)},t.prototype.setn=function(e,s){i(typeof e=="number"&&e>=0);var u=e/26|0,d=e%26;return this._expand(u+1),s?this.words[u]=this.words[u]|1<<d:this.words[u]=this.words[u]&~(1<<d),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 u,d;this.length>e.length?(u=this,d=e):(u=e,d=this);for(var h=0,M=0;M<d.length;M++)s=(u.words[M]|0)+(d.words[M]|0)+h,this.words[M]=s&67108863,h=s>>>26;for(;h!==0&&M<u.length;M++)s=(u.words[M]|0)+h,this.words[M]=s&67108863,h=s>>>26;if(this.length=u.length,h!==0)this.words[this.length]=h,this.length++;else if(u!==this)for(;M<u.length;M++)this.words[M]=u.words[M];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 u=this.cmp(e);if(u===0)return this.negative=0,this.length=1,this.words[0]=0,this;var d,h;u>0?(d=this,h=e):(d=e,h=this);for(var M=0,P=0;P<h.length;P++)s=(d.words[P]|0)-(h.words[P]|0)+M,M=s>>26,this.words[P]=s&67108863;for(;M!==0&&P<d.length;P++)s=(d.words[P]|0)+M,M=s>>26,this.words[P]=s&67108863;if(M===0&&P<d.length&&d!==this)for(;P<d.length;P++)this.words[P]=d.words[P];return this.length=Math.max(this.length,P),d!==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 u=p.length+e.length|0;s.length=u,u=u-1|0;var d=p.words[0]|0,h=e.words[0]|0,M=d*h,P=M&67108863,w=M/67108864|0;s.words[0]=P;for(var l=1;l<u;l++){for(var y=w>>>26,$=w&67108863,q=Math.min(l,e.length-1),j=Math.max(0,l-p.length+1);j<=q;j++){var V=l-j|0;d=p.words[V]|0,h=e.words[j]|0,M=d*h+$,y+=M/67108864|0,$=M&67108863}s.words[l]=$|0,w=y|0}return w!==0?s.words[l]=w|0:s.length--,s._strip()}var L=function(e,s,u){var d=e.words,h=s.words,M=u.words,P=0,w,l,y,$=d[0]|0,q=$&8191,j=$>>>13,V=d[1]|0,z=V&8191,G=V>>>13,rt=d[2]|0,X=rt&8191,it=rt>>>13,st=d[3]|0,J=st&8191,H=st>>>13,ne=d[4]|0,at=ne&8191,ct=ne>>>13,ke=d[5]|0,ft=ke&8191,dt=ke>>>13,Jt=d[6]|0,ot=Jt&8191,lt=Jt>>>13,Re=d[7]|0,gt=Re&8191,_t=Re>>>13,Vi=d[8]|0,wt=Vi&8191,vt=Vi>>>13,Ki=d[9]|0,Mt=Ki&8191,bt=Ki>>>13,zi=h[0]|0,yt=zi&8191,kt=zi>>>13,Gi=h[1]|0,St=Gi&8191,At=Gi>>>13,Hi=h[2]|0,Pt=Hi&8191,Ct=Hi>>>13,Xi=h[3]|0,xt=Xi&8191,Dt=Xi>>>13,Yi=h[4]|0,Tt=Yi&8191,Ot=Yi>>>13,Ji=h[5]|0,Rt=Ji&8191,Lt=Ji>>>13,Qi=h[6]|0,Et=Qi&8191,Nt=Qi>>>13,tr=h[7]|0,It=tr&8191,Bt=tr>>>13,er=h[8]|0,qt=er&8191,Ft=er>>>13,ir=h[9]|0,Ut=ir&8191,jt=ir>>>13;u.negative=e.negative^s.negative,u.length=19,w=Math.imul(q,yt),l=Math.imul(q,kt),l=l+Math.imul(j,yt)|0,y=Math.imul(j,kt);var oi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(oi>>>26)|0,oi&=67108863,w=Math.imul(z,yt),l=Math.imul(z,kt),l=l+Math.imul(G,yt)|0,y=Math.imul(G,kt),w=w+Math.imul(q,St)|0,l=l+Math.imul(q,At)|0,l=l+Math.imul(j,St)|0,y=y+Math.imul(j,At)|0;var si=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(si>>>26)|0,si&=67108863,w=Math.imul(X,yt),l=Math.imul(X,kt),l=l+Math.imul(it,yt)|0,y=Math.imul(it,kt),w=w+Math.imul(z,St)|0,l=l+Math.imul(z,At)|0,l=l+Math.imul(G,St)|0,y=y+Math.imul(G,At)|0,w=w+Math.imul(q,Pt)|0,l=l+Math.imul(q,Ct)|0,l=l+Math.imul(j,Pt)|0,y=y+Math.imul(j,Ct)|0;var ai=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(ai>>>26)|0,ai&=67108863,w=Math.imul(J,yt),l=Math.imul(J,kt),l=l+Math.imul(H,yt)|0,y=Math.imul(H,kt),w=w+Math.imul(X,St)|0,l=l+Math.imul(X,At)|0,l=l+Math.imul(it,St)|0,y=y+Math.imul(it,At)|0,w=w+Math.imul(z,Pt)|0,l=l+Math.imul(z,Ct)|0,l=l+Math.imul(G,Pt)|0,y=y+Math.imul(G,Ct)|0,w=w+Math.imul(q,xt)|0,l=l+Math.imul(q,Dt)|0,l=l+Math.imul(j,xt)|0,y=y+Math.imul(j,Dt)|0;var li=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(li>>>26)|0,li&=67108863,w=Math.imul(at,yt),l=Math.imul(at,kt),l=l+Math.imul(ct,yt)|0,y=Math.imul(ct,kt),w=w+Math.imul(J,St)|0,l=l+Math.imul(J,At)|0,l=l+Math.imul(H,St)|0,y=y+Math.imul(H,At)|0,w=w+Math.imul(X,Pt)|0,l=l+Math.imul(X,Ct)|0,l=l+Math.imul(it,Pt)|0,y=y+Math.imul(it,Ct)|0,w=w+Math.imul(z,xt)|0,l=l+Math.imul(z,Dt)|0,l=l+Math.imul(G,xt)|0,y=y+Math.imul(G,Dt)|0,w=w+Math.imul(q,Tt)|0,l=l+Math.imul(q,Ot)|0,l=l+Math.imul(j,Tt)|0,y=y+Math.imul(j,Ot)|0;var ui=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(ui>>>26)|0,ui&=67108863,w=Math.imul(ft,yt),l=Math.imul(ft,kt),l=l+Math.imul(dt,yt)|0,y=Math.imul(dt,kt),w=w+Math.imul(at,St)|0,l=l+Math.imul(at,At)|0,l=l+Math.imul(ct,St)|0,y=y+Math.imul(ct,At)|0,w=w+Math.imul(J,Pt)|0,l=l+Math.imul(J,Ct)|0,l=l+Math.imul(H,Pt)|0,y=y+Math.imul(H,Ct)|0,w=w+Math.imul(X,xt)|0,l=l+Math.imul(X,Dt)|0,l=l+Math.imul(it,xt)|0,y=y+Math.imul(it,Dt)|0,w=w+Math.imul(z,Tt)|0,l=l+Math.imul(z,Ot)|0,l=l+Math.imul(G,Tt)|0,y=y+Math.imul(G,Ot)|0,w=w+Math.imul(q,Rt)|0,l=l+Math.imul(q,Lt)|0,l=l+Math.imul(j,Rt)|0,y=y+Math.imul(j,Lt)|0;var ci=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(ci>>>26)|0,ci&=67108863,w=Math.imul(ot,yt),l=Math.imul(ot,kt),l=l+Math.imul(lt,yt)|0,y=Math.imul(lt,kt),w=w+Math.imul(ft,St)|0,l=l+Math.imul(ft,At)|0,l=l+Math.imul(dt,St)|0,y=y+Math.imul(dt,At)|0,w=w+Math.imul(at,Pt)|0,l=l+Math.imul(at,Ct)|0,l=l+Math.imul(ct,Pt)|0,y=y+Math.imul(ct,Ct)|0,w=w+Math.imul(J,xt)|0,l=l+Math.imul(J,Dt)|0,l=l+Math.imul(H,xt)|0,y=y+Math.imul(H,Dt)|0,w=w+Math.imul(X,Tt)|0,l=l+Math.imul(X,Ot)|0,l=l+Math.imul(it,Tt)|0,y=y+Math.imul(it,Ot)|0,w=w+Math.imul(z,Rt)|0,l=l+Math.imul(z,Lt)|0,l=l+Math.imul(G,Rt)|0,y=y+Math.imul(G,Lt)|0,w=w+Math.imul(q,Et)|0,l=l+Math.imul(q,Nt)|0,l=l+Math.imul(j,Et)|0,y=y+Math.imul(j,Nt)|0;var di=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(di>>>26)|0,di&=67108863,w=Math.imul(gt,yt),l=Math.imul(gt,kt),l=l+Math.imul(_t,yt)|0,y=Math.imul(_t,kt),w=w+Math.imul(ot,St)|0,l=l+Math.imul(ot,At)|0,l=l+Math.imul(lt,St)|0,y=y+Math.imul(lt,At)|0,w=w+Math.imul(ft,Pt)|0,l=l+Math.imul(ft,Ct)|0,l=l+Math.imul(dt,Pt)|0,y=y+Math.imul(dt,Ct)|0,w=w+Math.imul(at,xt)|0,l=l+Math.imul(at,Dt)|0,l=l+Math.imul(ct,xt)|0,y=y+Math.imul(ct,Dt)|0,w=w+Math.imul(J,Tt)|0,l=l+Math.imul(J,Ot)|0,l=l+Math.imul(H,Tt)|0,y=y+Math.imul(H,Ot)|0,w=w+Math.imul(X,Rt)|0,l=l+Math.imul(X,Lt)|0,l=l+Math.imul(it,Rt)|0,y=y+Math.imul(it,Lt)|0,w=w+Math.imul(z,Et)|0,l=l+Math.imul(z,Nt)|0,l=l+Math.imul(G,Et)|0,y=y+Math.imul(G,Nt)|0,w=w+Math.imul(q,It)|0,l=l+Math.imul(q,Bt)|0,l=l+Math.imul(j,It)|0,y=y+Math.imul(j,Bt)|0;var fi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(fi>>>26)|0,fi&=67108863,w=Math.imul(wt,yt),l=Math.imul(wt,kt),l=l+Math.imul(vt,yt)|0,y=Math.imul(vt,kt),w=w+Math.imul(gt,St)|0,l=l+Math.imul(gt,At)|0,l=l+Math.imul(_t,St)|0,y=y+Math.imul(_t,At)|0,w=w+Math.imul(ot,Pt)|0,l=l+Math.imul(ot,Ct)|0,l=l+Math.imul(lt,Pt)|0,y=y+Math.imul(lt,Ct)|0,w=w+Math.imul(ft,xt)|0,l=l+Math.imul(ft,Dt)|0,l=l+Math.imul(dt,xt)|0,y=y+Math.imul(dt,Dt)|0,w=w+Math.imul(at,Tt)|0,l=l+Math.imul(at,Ot)|0,l=l+Math.imul(ct,Tt)|0,y=y+Math.imul(ct,Ot)|0,w=w+Math.imul(J,Rt)|0,l=l+Math.imul(J,Lt)|0,l=l+Math.imul(H,Rt)|0,y=y+Math.imul(H,Lt)|0,w=w+Math.imul(X,Et)|0,l=l+Math.imul(X,Nt)|0,l=l+Math.imul(it,Et)|0,y=y+Math.imul(it,Nt)|0,w=w+Math.imul(z,It)|0,l=l+Math.imul(z,Bt)|0,l=l+Math.imul(G,It)|0,y=y+Math.imul(G,Bt)|0,w=w+Math.imul(q,qt)|0,l=l+Math.imul(q,Ft)|0,l=l+Math.imul(j,qt)|0,y=y+Math.imul(j,Ft)|0;var hi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(hi>>>26)|0,hi&=67108863,w=Math.imul(Mt,yt),l=Math.imul(Mt,kt),l=l+Math.imul(bt,yt)|0,y=Math.imul(bt,kt),w=w+Math.imul(wt,St)|0,l=l+Math.imul(wt,At)|0,l=l+Math.imul(vt,St)|0,y=y+Math.imul(vt,At)|0,w=w+Math.imul(gt,Pt)|0,l=l+Math.imul(gt,Ct)|0,l=l+Math.imul(_t,Pt)|0,y=y+Math.imul(_t,Ct)|0,w=w+Math.imul(ot,xt)|0,l=l+Math.imul(ot,Dt)|0,l=l+Math.imul(lt,xt)|0,y=y+Math.imul(lt,Dt)|0,w=w+Math.imul(ft,Tt)|0,l=l+Math.imul(ft,Ot)|0,l=l+Math.imul(dt,Tt)|0,y=y+Math.imul(dt,Ot)|0,w=w+Math.imul(at,Rt)|0,l=l+Math.imul(at,Lt)|0,l=l+Math.imul(ct,Rt)|0,y=y+Math.imul(ct,Lt)|0,w=w+Math.imul(J,Et)|0,l=l+Math.imul(J,Nt)|0,l=l+Math.imul(H,Et)|0,y=y+Math.imul(H,Nt)|0,w=w+Math.imul(X,It)|0,l=l+Math.imul(X,Bt)|0,l=l+Math.imul(it,It)|0,y=y+Math.imul(it,Bt)|0,w=w+Math.imul(z,qt)|0,l=l+Math.imul(z,Ft)|0,l=l+Math.imul(G,qt)|0,y=y+Math.imul(G,Ft)|0,w=w+Math.imul(q,Ut)|0,l=l+Math.imul(q,jt)|0,l=l+Math.imul(j,Ut)|0,y=y+Math.imul(j,jt)|0;var pi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(pi>>>26)|0,pi&=67108863,w=Math.imul(Mt,St),l=Math.imul(Mt,At),l=l+Math.imul(bt,St)|0,y=Math.imul(bt,At),w=w+Math.imul(wt,Pt)|0,l=l+Math.imul(wt,Ct)|0,l=l+Math.imul(vt,Pt)|0,y=y+Math.imul(vt,Ct)|0,w=w+Math.imul(gt,xt)|0,l=l+Math.imul(gt,Dt)|0,l=l+Math.imul(_t,xt)|0,y=y+Math.imul(_t,Dt)|0,w=w+Math.imul(ot,Tt)|0,l=l+Math.imul(ot,Ot)|0,l=l+Math.imul(lt,Tt)|0,y=y+Math.imul(lt,Ot)|0,w=w+Math.imul(ft,Rt)|0,l=l+Math.imul(ft,Lt)|0,l=l+Math.imul(dt,Rt)|0,y=y+Math.imul(dt,Lt)|0,w=w+Math.imul(at,Et)|0,l=l+Math.imul(at,Nt)|0,l=l+Math.imul(ct,Et)|0,y=y+Math.imul(ct,Nt)|0,w=w+Math.imul(J,It)|0,l=l+Math.imul(J,Bt)|0,l=l+Math.imul(H,It)|0,y=y+Math.imul(H,Bt)|0,w=w+Math.imul(X,qt)|0,l=l+Math.imul(X,Ft)|0,l=l+Math.imul(it,qt)|0,y=y+Math.imul(it,Ft)|0,w=w+Math.imul(z,Ut)|0,l=l+Math.imul(z,jt)|0,l=l+Math.imul(G,Ut)|0,y=y+Math.imul(G,jt)|0;var mi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(mi>>>26)|0,mi&=67108863,w=Math.imul(Mt,Pt),l=Math.imul(Mt,Ct),l=l+Math.imul(bt,Pt)|0,y=Math.imul(bt,Ct),w=w+Math.imul(wt,xt)|0,l=l+Math.imul(wt,Dt)|0,l=l+Math.imul(vt,xt)|0,y=y+Math.imul(vt,Dt)|0,w=w+Math.imul(gt,Tt)|0,l=l+Math.imul(gt,Ot)|0,l=l+Math.imul(_t,Tt)|0,y=y+Math.imul(_t,Ot)|0,w=w+Math.imul(ot,Rt)|0,l=l+Math.imul(ot,Lt)|0,l=l+Math.imul(lt,Rt)|0,y=y+Math.imul(lt,Lt)|0,w=w+Math.imul(ft,Et)|0,l=l+Math.imul(ft,Nt)|0,l=l+Math.imul(dt,Et)|0,y=y+Math.imul(dt,Nt)|0,w=w+Math.imul(at,It)|0,l=l+Math.imul(at,Bt)|0,l=l+Math.imul(ct,It)|0,y=y+Math.imul(ct,Bt)|0,w=w+Math.imul(J,qt)|0,l=l+Math.imul(J,Ft)|0,l=l+Math.imul(H,qt)|0,y=y+Math.imul(H,Ft)|0,w=w+Math.imul(X,Ut)|0,l=l+Math.imul(X,jt)|0,l=l+Math.imul(it,Ut)|0,y=y+Math.imul(it,jt)|0;var gi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(gi>>>26)|0,gi&=67108863,w=Math.imul(Mt,xt),l=Math.imul(Mt,Dt),l=l+Math.imul(bt,xt)|0,y=Math.imul(bt,Dt),w=w+Math.imul(wt,Tt)|0,l=l+Math.imul(wt,Ot)|0,l=l+Math.imul(vt,Tt)|0,y=y+Math.imul(vt,Ot)|0,w=w+Math.imul(gt,Rt)|0,l=l+Math.imul(gt,Lt)|0,l=l+Math.imul(_t,Rt)|0,y=y+Math.imul(_t,Lt)|0,w=w+Math.imul(ot,Et)|0,l=l+Math.imul(ot,Nt)|0,l=l+Math.imul(lt,Et)|0,y=y+Math.imul(lt,Nt)|0,w=w+Math.imul(ft,It)|0,l=l+Math.imul(ft,Bt)|0,l=l+Math.imul(dt,It)|0,y=y+Math.imul(dt,Bt)|0,w=w+Math.imul(at,qt)|0,l=l+Math.imul(at,Ft)|0,l=l+Math.imul(ct,qt)|0,y=y+Math.imul(ct,Ft)|0,w=w+Math.imul(J,Ut)|0,l=l+Math.imul(J,jt)|0,l=l+Math.imul(H,Ut)|0,y=y+Math.imul(H,jt)|0;var _i=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(_i>>>26)|0,_i&=67108863,w=Math.imul(Mt,Tt),l=Math.imul(Mt,Ot),l=l+Math.imul(bt,Tt)|0,y=Math.imul(bt,Ot),w=w+Math.imul(wt,Rt)|0,l=l+Math.imul(wt,Lt)|0,l=l+Math.imul(vt,Rt)|0,y=y+Math.imul(vt,Lt)|0,w=w+Math.imul(gt,Et)|0,l=l+Math.imul(gt,Nt)|0,l=l+Math.imul(_t,Et)|0,y=y+Math.imul(_t,Nt)|0,w=w+Math.imul(ot,It)|0,l=l+Math.imul(ot,Bt)|0,l=l+Math.imul(lt,It)|0,y=y+Math.imul(lt,Bt)|0,w=w+Math.imul(ft,qt)|0,l=l+Math.imul(ft,Ft)|0,l=l+Math.imul(dt,qt)|0,y=y+Math.imul(dt,Ft)|0,w=w+Math.imul(at,Ut)|0,l=l+Math.imul(at,jt)|0,l=l+Math.imul(ct,Ut)|0,y=y+Math.imul(ct,jt)|0;var wi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(wi>>>26)|0,wi&=67108863,w=Math.imul(Mt,Rt),l=Math.imul(Mt,Lt),l=l+Math.imul(bt,Rt)|0,y=Math.imul(bt,Lt),w=w+Math.imul(wt,Et)|0,l=l+Math.imul(wt,Nt)|0,l=l+Math.imul(vt,Et)|0,y=y+Math.imul(vt,Nt)|0,w=w+Math.imul(gt,It)|0,l=l+Math.imul(gt,Bt)|0,l=l+Math.imul(_t,It)|0,y=y+Math.imul(_t,Bt)|0,w=w+Math.imul(ot,qt)|0,l=l+Math.imul(ot,Ft)|0,l=l+Math.imul(lt,qt)|0,y=y+Math.imul(lt,Ft)|0,w=w+Math.imul(ft,Ut)|0,l=l+Math.imul(ft,jt)|0,l=l+Math.imul(dt,Ut)|0,y=y+Math.imul(dt,jt)|0;var vi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(vi>>>26)|0,vi&=67108863,w=Math.imul(Mt,Et),l=Math.imul(Mt,Nt),l=l+Math.imul(bt,Et)|0,y=Math.imul(bt,Nt),w=w+Math.imul(wt,It)|0,l=l+Math.imul(wt,Bt)|0,l=l+Math.imul(vt,It)|0,y=y+Math.imul(vt,Bt)|0,w=w+Math.imul(gt,qt)|0,l=l+Math.imul(gt,Ft)|0,l=l+Math.imul(_t,qt)|0,y=y+Math.imul(_t,Ft)|0,w=w+Math.imul(ot,Ut)|0,l=l+Math.imul(ot,jt)|0,l=l+Math.imul(lt,Ut)|0,y=y+Math.imul(lt,jt)|0;var Mi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(Mi>>>26)|0,Mi&=67108863,w=Math.imul(Mt,It),l=Math.imul(Mt,Bt),l=l+Math.imul(bt,It)|0,y=Math.imul(bt,Bt),w=w+Math.imul(wt,qt)|0,l=l+Math.imul(wt,Ft)|0,l=l+Math.imul(vt,qt)|0,y=y+Math.imul(vt,Ft)|0,w=w+Math.imul(gt,Ut)|0,l=l+Math.imul(gt,jt)|0,l=l+Math.imul(_t,Ut)|0,y=y+Math.imul(_t,jt)|0;var bi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(bi>>>26)|0,bi&=67108863,w=Math.imul(Mt,qt),l=Math.imul(Mt,Ft),l=l+Math.imul(bt,qt)|0,y=Math.imul(bt,Ft),w=w+Math.imul(wt,Ut)|0,l=l+Math.imul(wt,jt)|0,l=l+Math.imul(vt,Ut)|0,y=y+Math.imul(vt,jt)|0;var yi=(P+w|0)+((l&8191)<<13)|0;P=(y+(l>>>13)|0)+(yi>>>26)|0,yi&=67108863,w=Math.imul(Mt,Ut),l=Math.imul(Mt,jt),l=l+Math.imul(bt,Ut)|0,y=Math.imul(bt,jt);var ki=(P+w|0)+((l&8191)<<13)|0;return P=(y+(l>>>13)|0)+(ki>>>26)|0,ki&=67108863,M[0]=oi,M[1]=si,M[2]=ai,M[3]=li,M[4]=ui,M[5]=ci,M[6]=di,M[7]=fi,M[8]=hi,M[9]=pi,M[10]=mi,M[11]=gi,M[12]=_i,M[13]=wi,M[14]=vi,M[15]=Mi,M[16]=bi,M[17]=yi,M[18]=ki,P!==0&&(M[19]=P,u.length++),u};Math.imul||(L=E);function T(p,e,s){s.negative=e.negative^p.negative,s.length=p.length+e.length;for(var u=0,d=0,h=0;h<s.length-1;h++){var M=d;d=0;for(var P=u&67108863,w=Math.min(h,e.length-1),l=Math.max(0,h-p.length+1);l<=w;l++){var y=h-l,$=p.words[y]|0,q=e.words[l]|0,j=$*q,V=j&67108863;M=M+(j/67108864|0)|0,V=V+P|0,P=V&67108863,M=M+(V>>>26)|0,d+=M>>>26,M&=67108863}s.words[h]=P,u=M,M=d}return u!==0?s.words[h]=u:s.length--,s._strip()}function I(p,e,s){return T(p,e,s)}t.prototype.mulTo=function(e,s){var u,d=this.length+e.length;return this.length===10&&e.length===10?u=L(this,e,s):d<63?u=E(this,e,s):d<1024?u=T(this,e,s):u=I(this,e,s),u};function B(p,e){this.x=p,this.y=e}B.prototype.makeRBT=function(e){for(var s=new Array(e),u=t.prototype._countBits(e)-1,d=0;d<e;d++)s[d]=this.revBin(d,u,e);return s},B.prototype.revBin=function(e,s,u){if(e===0||e===u-1)return e;for(var d=0,h=0;h<s;h++)d|=(e&1)<<s-h-1,e>>=1;return d},B.prototype.permute=function(e,s,u,d,h,M){for(var P=0;P<M;P++)d[P]=s[e[P]],h[P]=u[e[P]]},B.prototype.transform=function(e,s,u,d,h,M){this.permute(M,e,s,u,d,h);for(var P=1;P<h;P<<=1)for(var w=P<<1,l=Math.cos(2*Math.PI/w),y=Math.sin(2*Math.PI/w),$=0;$<h;$+=w)for(var q=l,j=y,V=0;V<P;V++){var z=u[$+V],G=d[$+V],rt=u[$+V+P],X=d[$+V+P],it=q*rt-j*X;X=q*X+j*rt,rt=it,u[$+V]=z+rt,d[$+V]=G+X,u[$+V+P]=z-rt,d[$+V+P]=G-X,V!==w&&(it=l*q-y*j,j=l*j+y*q,q=it)}},B.prototype.guessLen13b=function(e,s){var u=Math.max(s,e)|1,d=u&1,h=0;for(u=u/2|0;u;u=u>>>1)h++;return 1<<h+1+d},B.prototype.conjugate=function(e,s,u){if(!(u<=1))for(var d=0;d<u/2;d++){var h=e[d];e[d]=e[u-d-1],e[u-d-1]=h,h=s[d],s[d]=-s[u-d-1],s[u-d-1]=-h}},B.prototype.normalize13b=function(e,s){for(var u=0,d=0;d<s/2;d++){var h=Math.round(e[2*d+1]/s)*8192+Math.round(e[2*d]/s)+u;e[d]=h&67108863,h<67108864?u=0:u=h/67108864|0}return e},B.prototype.convert13b=function(e,s,u,d){for(var h=0,M=0;M<s;M++)h=h+(e[M]|0),u[2*M]=h&8191,h=h>>>13,u[2*M+1]=h&8191,h=h>>>13;for(M=2*s;M<d;++M)u[M]=0;i(h===0),i((h&-8192)===0)},B.prototype.stub=function(e){for(var s=new Array(e),u=0;u<e;u++)s[u]=0;return s},B.prototype.mulp=function(e,s,u){var d=2*this.guessLen13b(e.length,s.length),h=this.makeRBT(d),M=this.stub(d),P=new Array(d),w=new Array(d),l=new Array(d),y=new Array(d),$=new Array(d),q=new Array(d),j=u.words;j.length=d,this.convert13b(e.words,e.length,P,d),this.convert13b(s.words,s.length,y,d),this.transform(P,M,w,l,d,h),this.transform(y,M,$,q,d,h);for(var V=0;V<d;V++){var z=w[V]*$[V]-l[V]*q[V];l[V]=w[V]*q[V]+l[V]*$[V],w[V]=z}return this.conjugate(w,l,d),this.transform(w,l,j,M,d,h),this.conjugate(j,M,d),this.normalize13b(j,d),u.negative=e.negative^s.negative,u.length=e.length+s.length,u._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),i(typeof e=="number"),i(e<67108864);for(var u=0,d=0;d<this.length;d++){var h=(this.words[d]|0)*e,M=(h&67108863)+(u&67108863);u>>=26,u+=h/67108864|0,u+=M>>>26,this.words[d]=M&67108863}return u!==0&&(this.words[d]=u,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=R(e);if(s.length===0)return new t(1);for(var u=this,d=0;d<s.length&&s[d]===0;d++,u=u.sqr());if(++d<s.length)for(var h=u.sqr();d<s.length;d++,h=h.sqr())s[d]!==0&&(u=u.mul(h));return u},t.prototype.iushln=function(e){i(typeof e=="number"&&e>=0);var s=e%26,u=(e-s)/26,d=67108863>>>26-s<<26-s,h;if(s!==0){var M=0;for(h=0;h<this.length;h++){var P=this.words[h]&d,w=(this.words[h]|0)-P<<s;this.words[h]=w|M,M=P>>>26-s}M&&(this.words[h]=M,this.length++)}if(u!==0){for(h=this.length-1;h>=0;h--)this.words[h+u]=this.words[h];for(h=0;h<u;h++)this.words[h]=0;this.length+=u}return this._strip()},t.prototype.ishln=function(e){return i(this.negative===0),this.iushln(e)},t.prototype.iushrn=function(e,s,u){i(typeof e=="number"&&e>=0);var d;s?d=(s-s%26)/26:d=0;var h=e%26,M=Math.min((e-h)/26,this.length),P=67108863^67108863>>>h<<h,w=u;if(d-=M,d=Math.max(0,d),w){for(var l=0;l<M;l++)w.words[l]=this.words[l];w.length=M}if(M!==0)if(this.length>M)for(this.length-=M,l=0;l<this.length;l++)this.words[l]=this.words[l+M];else this.words[0]=0,this.length=1;var y=0;for(l=this.length-1;l>=0&&(y!==0||l>=d);l--){var $=this.words[l]|0;this.words[l]=y<<26-h|$>>>h,y=$&P}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,u){return i(this.negative===0),this.iushrn(e,s,u)},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){i(typeof e=="number"&&e>=0);var s=e%26,u=(e-s)/26,d=1<<s;if(this.length<=u)return!1;var h=this.words[u];return!!(h&d)},t.prototype.imaskn=function(e){i(typeof e=="number"&&e>=0);var s=e%26,u=(e-s)/26;if(i(this.negative===0,"imaskn works only with positive numbers"),this.length<=u)return this;if(s!==0&&u++,this.length=Math.min(u,this.length),s!==0){var d=67108863^67108863>>>s<<s;this.words[this.length-1]&=d}return this._strip()},t.prototype.maskn=function(e){return this.clone().imaskn(e)},t.prototype.iaddn=function(e){return i(typeof e=="number"),i(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(i(typeof e=="number"),i(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,u){var d=e.length+u,h;this._expand(d);var M,P=0;for(h=0;h<e.length;h++){M=(this.words[h+u]|0)+P;var w=(e.words[h]|0)*s;M-=w&67108863,P=(M>>26)-(w/67108864|0),this.words[h+u]=M&67108863}for(;h<this.length-u;h++)M=(this.words[h+u]|0)+P,P=M>>26,this.words[h+u]=M&67108863;if(P===0)return this._strip();for(i(P===-1),P=0,h=0;h<this.length;h++)M=-(this.words[h]|0)+P,P=M>>26,this.words[h]=M&67108863;return this.negative=1,this._strip()},t.prototype._wordDiv=function(e,s){var u=this.length-e.length,d=this.clone(),h=e,M=h.words[h.length-1]|0,P=this._countBits(M);u=26-P,u!==0&&(h=h.ushln(u),d.iushln(u),M=h.words[h.length-1]|0);var w=d.length-h.length,l;if(s!=="mod"){l=new t(null),l.length=w+1,l.words=new Array(l.length);for(var y=0;y<l.length;y++)l.words[y]=0}var $=d.clone()._ishlnsubmul(h,1,w);$.negative===0&&(d=$,l&&(l.words[w]=1));for(var q=w-1;q>=0;q--){var j=(d.words[h.length+q]|0)*67108864+(d.words[h.length+q-1]|0);for(j=Math.min(j/M|0,67108863),d._ishlnsubmul(h,j,q);d.negative!==0;)j--,d.negative=0,d._ishlnsubmul(h,1,q),d.isZero()||(d.negative^=1);l&&(l.words[q]=j)}return l&&l._strip(),d._strip(),s!=="div"&&u!==0&&d.iushrn(u),{div:l||null,mod:d}},t.prototype.divmod=function(e,s,u){if(i(!e.isZero()),this.isZero())return{div:new t(0),mod:new t(0)};var d,h,M;return this.negative!==0&&e.negative===0?(M=this.neg().divmod(e,s),s!=="mod"&&(d=M.div.neg()),s!=="div"&&(h=M.mod.neg(),u&&h.negative!==0&&h.iadd(e)),{div:d,mod:h}):this.negative===0&&e.negative!==0?(M=this.divmod(e.neg(),s),s!=="mod"&&(d=M.div.neg()),{div:d,mod:M.mod}):(this.negative&e.negative)!==0?(M=this.neg().divmod(e.neg(),s),s!=="div"&&(h=M.mod.neg(),u&&h.negative!==0&&h.isub(e)),{div:M.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 u=s.div.negative!==0?s.mod.isub(e):s.mod,d=e.ushrn(1),h=e.andln(1),M=u.cmp(d);return M<0||h===1&&M===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),i(e<=67108863);for(var u=(1<<26)%e,d=0,h=this.length-1;h>=0;h--)d=(u*d+(this.words[h]|0))%e;return s?-d:d},t.prototype.modn=function(e){return this.modrn(e)},t.prototype.idivn=function(e){var s=e<0;s&&(e=-e),i(e<=67108863);for(var u=0,d=this.length-1;d>=0;d--){var h=(this.words[d]|0)+u*67108864;this.words[d]=h/e|0,u=h%e}return this._strip(),s?this.ineg():this},t.prototype.divn=function(e){return this.clone().idivn(e)},t.prototype.egcd=function(e){i(e.negative===0),i(!e.isZero());var s=this,u=e.clone();s.negative!==0?s=s.umod(e):s=s.clone();for(var d=new t(1),h=new t(0),M=new t(0),P=new t(1),w=0;s.isEven()&&u.isEven();)s.iushrn(1),u.iushrn(1),++w;for(var l=u.clone(),y=s.clone();!s.isZero();){for(var $=0,q=1;(s.words[0]&q)===0&&$<26;++$,q<<=1);if($>0)for(s.iushrn($);$-- >0;)(d.isOdd()||h.isOdd())&&(d.iadd(l),h.isub(y)),d.iushrn(1),h.iushrn(1);for(var j=0,V=1;(u.words[0]&V)===0&&j<26;++j,V<<=1);if(j>0)for(u.iushrn(j);j-- >0;)(M.isOdd()||P.isOdd())&&(M.iadd(l),P.isub(y)),M.iushrn(1),P.iushrn(1);s.cmp(u)>=0?(s.isub(u),d.isub(M),h.isub(P)):(u.isub(s),M.isub(d),P.isub(h))}return{a:M,b:P,gcd:u.iushln(w)}},t.prototype._invmp=function(e){i(e.negative===0),i(!e.isZero());var s=this,u=e.clone();s.negative!==0?s=s.umod(e):s=s.clone();for(var d=new t(1),h=new t(0),M=u.clone();s.cmpn(1)>0&&u.cmpn(1)>0;){for(var P=0,w=1;(s.words[0]&w)===0&&P<26;++P,w<<=1);if(P>0)for(s.iushrn(P);P-- >0;)d.isOdd()&&d.iadd(M),d.iushrn(1);for(var l=0,y=1;(u.words[0]&y)===0&&l<26;++l,y<<=1);if(l>0)for(u.iushrn(l);l-- >0;)h.isOdd()&&h.iadd(M),h.iushrn(1);s.cmp(u)>=0?(s.isub(u),d.isub(h)):(u.isub(s),h.isub(d))}var $;return s.cmpn(1)===0?$=d:$=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(),u=e.clone();s.negative=0,u.negative=0;for(var d=0;s.isEven()&&u.isEven();d++)s.iushrn(1),u.iushrn(1);do{for(;s.isEven();)s.iushrn(1);for(;u.isEven();)u.iushrn(1);var h=s.cmp(u);if(h<0){var M=s;s=u,u=M}else if(h===0||u.cmpn(1)===0)break;s.isub(u)}while(!0);return u.iushln(d)},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){i(typeof e=="number");var s=e%26,u=(e-s)/26,d=1<<s;if(this.length<=u)return this._expand(u+1),this.words[u]|=d,this;for(var h=d,M=u;h!==0&&M<this.length;M++){var P=this.words[M]|0;P+=h,h=P>>>26,P&=67108863,this.words[M]=P}return h!==0&&(this.words[M]=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 u;if(this.length>1)u=1;else{s&&(e=-e),i(e<=67108863,"Number is too big");var d=this.words[0]|0;u=d===e?0:d<e?-1:1}return this.negative!==0?-u|0:u},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,u=this.length-1;u>=0;u--){var d=this.words[u]|0,h=e.words[u]|0;if(d!==h){d<h?s=-1:d>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 x(e)},t.prototype.toRed=function(e){return i(!this.red,"Already a number in reduction context"),i(this.negative===0,"red works only with positives"),e.convertTo(this)._forceRed(e)},t.prototype.fromRed=function(){return i(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 i(!this.red,"Already a number in reduction context"),this._forceRed(e)},t.prototype.redAdd=function(e){return i(this.red,"redAdd works only with red numbers"),this.red.add(this,e)},t.prototype.redIAdd=function(e){return i(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,e)},t.prototype.redSub=function(e){return i(this.red,"redSub works only with red numbers"),this.red.sub(this,e)},t.prototype.redISub=function(e){return i(this.red,"redISub works only with red numbers"),this.red.isub(this,e)},t.prototype.redShl=function(e){return i(this.red,"redShl works only with red numbers"),this.red.shl(this,e)},t.prototype.redMul=function(e){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.mul(this,e)},t.prototype.redIMul=function(e){return i(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.imul(this,e)},t.prototype.redSqr=function(){return i(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},t.prototype.redISqr=function(){return i(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},t.prototype.redSqrt=function(){return i(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},t.prototype.redInvm=function(){return i(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},t.prototype.redNeg=function(){return i(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},t.prototype.redPow=function(e){return i(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 U(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()}U.prototype._tmp=function(){var e=new t(null);return e.words=new Array(Math.ceil(this.n/13)),e},U.prototype.ireduce=function(e){var s=e,u;do this.split(s,this.tmp),s=this.imulK(s),s=s.iadd(this.tmp),u=s.bitLength();while(u>this.n);var d=u<this.n?-1:s.ucmp(this.p);return d===0?(s.words[0]=0,s.length=1):d>0?s.isub(this.p):s.strip!==void 0?s.strip():s._strip(),s},U.prototype.split=function(e,s){e.iushrn(this.n,0,s)},U.prototype.imulK=function(e){return e.imul(this.k)};function Z(){U.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}o(Z,U),Z.prototype.split=function(e,s){for(var u=4194303,d=Math.min(e.length,9),h=0;h<d;h++)s.words[h]=e.words[h];if(s.length=d,e.length<=9){e.words[0]=0,e.length=1;return}var M=e.words[9];for(s.words[s.length++]=M&u,h=10;h<e.length;h++){var P=e.words[h]|0;e.words[h-10]=(P&u)<<4|M>>>22,M=P}M>>>=22,e.words[h-10]=M,M===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,u=0;u<e.length;u++){var d=e.words[u]|0;s+=d*977,e.words[u]=s&67108863,s=d*64+(s/67108864|0)}return e.words[e.length-1]===0&&(e.length--,e.words[e.length-1]===0&&e.length--),e};function g(){U.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}o(g,U);function b(){U.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}o(b,U);function A(){U.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}o(A,U),A.prototype.imulK=function(e){for(var s=0,u=0;u<e.length;u++){var d=(e.words[u]|0)*19+s,h=d&67108863;d>>>=26,e.words[u]=h,s=d}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 g;else if(e==="p192")s=new b;else if(e==="p25519")s=new A;else throw new Error("Unknown prime "+e);return W[e]=s,s};function x(p){if(typeof p=="string"){var e=t._prime(p);this.m=e.p,this.prime=e}else i(p.gtn(1),"modulus must be greater than 1"),this.m=p,this.prime=null}x.prototype._verify1=function(e){i(e.negative===0,"red works only with positives"),i(e.red,"red works only with red numbers")},x.prototype._verify2=function(e,s){i((e.negative|s.negative)===0,"red works only with positives"),i(e.red&&e.red===s.red,"red works only with red numbers")},x.prototype.imod=function(e){return this.prime?this.prime.ireduce(e)._forceRed(this):(v(e,e.umod(this.m)._forceRed(this)),e)},x.prototype.neg=function(e){return e.isZero()?e.clone():this.m.sub(e)._forceRed(this)},x.prototype.add=function(e,s){this._verify2(e,s);var u=e.add(s);return u.cmp(this.m)>=0&&u.isub(this.m),u._forceRed(this)},x.prototype.iadd=function(e,s){this._verify2(e,s);var u=e.iadd(s);return u.cmp(this.m)>=0&&u.isub(this.m),u},x.prototype.sub=function(e,s){this._verify2(e,s);var u=e.sub(s);return u.cmpn(0)<0&&u.iadd(this.m),u._forceRed(this)},x.prototype.isub=function(e,s){this._verify2(e,s);var u=e.isub(s);return u.cmpn(0)<0&&u.iadd(this.m),u},x.prototype.shl=function(e,s){return this._verify1(e),this.imod(e.ushln(s))},x.prototype.imul=function(e,s){return this._verify2(e,s),this.imod(e.imul(s))},x.prototype.mul=function(e,s){return this._verify2(e,s),this.imod(e.mul(s))},x.prototype.isqr=function(e){return this.imul(e,e.clone())},x.prototype.sqr=function(e){return this.mul(e,e)},x.prototype.sqrt=function(e){if(e.isZero())return e.clone();var s=this.m.andln(3);if(i(s%2===1),s===3){var u=this.m.add(new t(1)).iushrn(2);return this.pow(e,u)}for(var d=this.m.subn(1),h=0;!d.isZero()&&d.andln(1)===0;)h++,d.iushrn(1);i(!d.isZero());var M=new t(1).toRed(this),P=M.redNeg(),w=this.m.subn(1).iushrn(1),l=this.m.bitLength();for(l=new t(2*l*l).toRed(this);this.pow(l,w).cmp(P)!==0;)l.redIAdd(P);for(var y=this.pow(l,d),$=this.pow(e,d.addn(1).iushrn(1)),q=this.pow(e,d),j=h;q.cmp(M)!==0;){for(var V=q,z=0;V.cmp(M)!==0;z++)V=V.redSqr();i(z<j);var G=this.pow(y,new t(1).iushln(j-z-1));$=$.redMul(G),y=G.redSqr(),q=q.redMul(y),j=z}return $},x.prototype.invm=function(e){var s=e._invmp(this.m);return s.negative!==0?(s.negative=0,this.imod(s).redNeg()):this.imod(s)},x.prototype.pow=function(e,s){if(s.isZero())return new t(1).toRed(this);if(s.cmpn(1)===0)return e.clone();var u=4,d=new Array(1<<u);d[0]=new t(1).toRed(this),d[1]=e;for(var h=2;h<d.length;h++)d[h]=this.mul(d[h-1],e);var M=d[0],P=0,w=0,l=s.bitLength()%26;for(l===0&&(l=26),h=s.length-1;h>=0;h--){for(var y=s.words[h],$=l-1;$>=0;$--){var q=y>>$&1;if(M!==d[0]&&(M=this.sqr(M)),q===0&&P===0){w=0;continue}P<<=1,P|=q,w++,!(w!==u&&(h!==0||$!==0))&&(M=this.mul(M,d[P]),w=0,P=0)}l=26}return M},x.prototype.convertTo=function(e){var s=e.umod(this.m);return s===e?s.clone():s},x.prototype.convertFrom=function(e){var s=e.clone();return s.red=null,s},t.mont=function(e){return new C(e)};function C(p){x.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(C,x),C.prototype.convertTo=function(e){return this.imod(e.ushln(this.shift))},C.prototype.convertFrom=function(e){var s=this.imod(e.mul(this.rinv));return s.red=null,s},C.prototype.imul=function(e,s){if(e.isZero()||s.isZero())return e.words[0]=0,e.length=1,e;var u=e.imul(s),d=u.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),h=u.isub(d).iushrn(this.shift),M=h;return h.cmp(this.m)>=0?M=h.isub(this.m):h.cmpn(0)<0&&(M=h.iadd(this.m)),M._forceRed(this)},C.prototype.mul=function(e,s){if(e.isZero()||s.isZero())return new t(0)._forceRed(this);var u=e.mul(s),d=u.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),h=u.isub(d).iushrn(this.shift),M=h;return h.cmp(this.m)>=0?M=h.isub(this.m):h.cmpn(0)<0&&(M=h.iadd(this.m)),M._forceRed(this)},C.prototype.invm=function(e){var s=this.imod(e._invmp(this.m).mul(this.r2));return s._forceRed(this)}})(typeof $i>"u"||$i,Ir)});import{SuiClient as yo}from"@mysten/sui/client";import{normalizeSuiAddress as ko}from"@mysten/sui/utils";import{SuiPriceServiceConnection as So}from"@pythnetwork/pyth-sui-js";import{SdkWrapper as Ao}from"@cetusprotocol/common-sdk";import{CetusClmmSDK as Po}from"@cetusprotocol/sui-clmm-sdk";import{AggregatorClient as Co,Env as Ur}from"@cetusprotocol/aggregator-sdk";import{FullRpcUrlMainnet as Xr}from"@cetusprotocol/common-sdk";var nr={full_rpc_url:Xr,env:"mainnet",aggregator_url:"https://api-sui.cetus.zone/router_v3",margin_trading:{package_id:"0x51bfedf86b5e18076f844bf0e1f82b14667bce59e2c38a8b5c8752ba3314bbd3",published_at:"0x51bfedf86b5e18076f844bf0e1f82b14667bce59e2c38a8b5c8752ba3314bbd3",version:0,config:{versioned_id:"0xdf21deea40ae40b2df5ef4b89a6d2d4bb487433b8fbc5af4f013c5d72e6fdc5f",admin_cap_id:"0x37727c2a5135bdf5b0d02f58dc119d3f3f3a778603690f8ecbb9ca9ecb2f0699",global_config_id:"0x3f0c7d3295ec74b465e944f9bad4310a8ce7f7fbeb76abf11933e43bb708bcd1",markets:"0x7d8797b6f177a8820e8b03ca81edf0818bc3ab2739df6800011b79c02d93f3ef",markets_table_id:"0xaf0d0a53740d02f6ace10a07a60763f8e079ee646913062e8392da3a96af3500"}},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"}}};import{CACHE_TIME_5MIN as tn,d as le,getPackagerConfigs as Ee}from"@cetusprotocol/common-sdk";import{Transaction as Ne}from"@mysten/sui/transactions";import{addHexPrefix as or,d as ae,getObjectFields as ur}from"@cetusprotocol/common-sdk";import{parseObligation as Yr}from"@suilend/sdk/parsers/obligation";import{getNetAprPercent as Jr,getTotalAprPercent as sr,Side as ar,getFilteredRewards as lr}from"@suilend/sdk";var Si=n=>{let r=ur(n),{open_permissions_pause:i,close_permissions_pause:o,deposit_permissions_pause:t,withdraw_permissions_pause:a,borrow_permissions_pause:f,repay_permissions_pause:c}=Ci(r.permissions.toString(2));return{market_id:r.id.id,base_token:or(r.base_token.fields.name),quote_token:or(r.quote_token.fields.name),max_long_leverage:ae(r.max_long_leverage).div(10**6).toString(),max_short_leverage:ae(r.max_short_leverage).div(10**6).toString(),open_fee_rate:ae(r.open_fee_rate).div(10**6).toString(),close_fee_rate:ae(r.close_fee_rate).div(10**6).toString(),open_permissions_pause:i,close_permissions_pause:o,deposit_permissions_pause:t,withdraw_permissions_pause:a,borrow_permissions_pause:f,repay_permissions_pause:c}},Ai=(n,r)=>{let i=ur(n);return{created_at:i.created_ts,position_id:i.id.id,init_deposit_amount:i.init_deposit_amount,is_long:i.is_long,lending_market_id:i.lending_market_id,obligation_owner_cap:i.obligation_owner_cap.fields.obligation_id,market_id:i.market_id,position_cap_id:r}},Pi=(n,r,i,o,t)=>{let a=Yr(r,i),f=Jr(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=ae(c.reserve.config.closeLtvPct).div(100).toString(),v=ae(c.reserve.config.openLtvPct).div(100).toString(),_=a.depositedAmountUsd.gt(0)?a.borrowedAmountUsd.div(a.depositedAmountUsd).toString():"0",k=ae(1).div(m).toString(),S=ae(m).sub(_).toString(),D="0",O="0",R="0",E="0";if(a.borrows&&a.borrows.length>0){let L=a.borrows[0],T=t[L?.coinType];T&&(D=sr(ar.BORROW,L?.reserve.borrowAprPercent,lr(T.borrow)).toString()),R=L.borrowedAmountUsd.toString(),E=L.borrowedAmount.toString()}if(a.deposits&&a.deposits.length>0){let L=a.deposits[0],T=t[L?.coinType];T&&(O=sr(ar.DEPOSIT,L?.reserve.depositAprPercent,lr(T.deposit)).toString())}return{...n,...a,origin_obligation:r,net_apr_percent:f.toString(),supplied_apr:ae(O).toString(),borrowed_apr:ae(D).toString(),close_ltv:m,open_ltv:v,cr:_,ltr:k,liquidation_buffer:S,deposited_amount_usd:c.depositedAmountUsd.toString(),borrowed_amount_usd:R,deposited_amount:c.depositedAmount.toString(),borrowed_amount:E}},Ci=n=>{let r=n.length,i=n.substring(r-1,r)==="0",o=n.substring(r-2,r-1)==="0",t=n.substring(r-3,r-2)==="0",a=n.substring(r-4,r-3)==="0",f=n.substring(r-5,r-4)==="0",c=n.substring(r-6,r-5)==="0";return{open_permissions_pause:i,close_permissions_pause:o,deposit_permissions_pause:t,withdraw_permissions_pause:f,borrow_permissions_pause:a,repay_permissions_pause:c}};import{getFilteredRewards as he,getStakingYieldAprPercent as cr,getTotalAprPercent as Ie,Side as Se}from"@suilend/sdk";import{BaseError as Qr}from"@cetusprotocol/common-sdk";var Le=class extends Qr{constructor(r,i,o){super(r,i||"UnknownError",o)}static isCrossSwapErrorCode(r,i){return this.isErrorCode(r,i)}},Zt=(n,r,i)=>{let o={...i,stack:r instanceof Error?r.stack:void 0};throw r instanceof Error?new Le(r.message,n,o):new Le(r,n,o)};var Be=class{constructor(r){this.createMarket=async r=>{let{package_id:i}=this._sdk.sdkOptions.margin_trading,{versioned_id:o,admin_cap_id:t,markets:a,global_config_id:f}=Ee(this._sdk.sdkOptions.margin_trading),c=new Ne;return c.moveCall({target:`${i}::market::create_market`,arguments:[c.object(t),c.object(f),c.object(a),c.pure.u64(r.open_fee_rate),c.pure.u64(r.close_fee_rate),c.object(o)],typeArguments:[r.base_token,r.quote_token]}),c};this.updateMarketFeeRate=async r=>{let{package_id:i}=this._sdk.sdkOptions.margin_trading,{versioned_id:o,admin_cap_id:t}=Ee(this._sdk.sdkOptions.margin_trading),{market_id:a,open_fee_rate:f,close_fee_rate:c}=r,m=new Ne;return m.moveCall({target:`${i}::market::set_fee_rate`,arguments:[m.object(t),m.object(a),m.pure.u64(f),m.pure.u64(c),m.object(o)],typeArguments:[]}),m};this.claimMarketFees=async(r,i)=>{let{package_id:o}=this._sdk.sdkOptions.margin_trading,{versioned_id:t,admin_cap_id:a}=Ee(this._sdk.sdkOptions.margin_trading),f=i||new Ne,{base_token:c,quote_token:m}=await this._sdk.MarketModules.getMarketInfo(r);return f.moveCall({target:`${o}::market::claim_fees`,arguments:[f.object(a),f.object(r),f.object(t)],typeArguments:[c,m]}),f};this.claimAllMarketFees=async r=>{let i=new Ne;for(let o=0;o<r.length;o++){let t=r[o];this.claimMarketFees(t,i)}return i};this.getMarginTradingConfig=async()=>{let{package_id:r}=this._sdk.sdkOptions.margin_trading;try{let i=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${r}::versioned::InitEvent`}),o=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${r}::admin_cap::InitEvent`}),t=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${r}::config::InitEvent`}),f=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${r}::market::InitEvent`})).data[0].parsedJson.markets_id,m=(await this._sdk.FullClient.getObject({id:f,options:{showContent:!0}})).data.content.fields.list.fields.id.id;return{versioned_id:i.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:f,markets_table_id:m}}catch(i){return Zt("FetchError",i),{versioned_id:"",admin_cap_id:"",global_config_id:"",markets_table_id:"",markets:""}}};this.getMarketList=async(r=!1)=>{let o=this._sdk.getCache("margin_trading_markets_list",r);if(o)return o;let{package_id:t}=this._sdk.sdkOptions.margin_trading,a=[];try{let f=`${t}::market::CreateMarketEvent`,m=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:f})).data.map(v=>v.parsedJson.market_id);return m.length>0?((await this._sdk.FullClient.batchGetObjects(m,{showContent:!0,showType:!0})).forEach(_=>{let k=Si(_),S=`margin_trading_market_info_${k.market_id}`;this._sdk.updateCache(S,k),a.push(k)}),a):(Zt("MarketNotFound","Market not found"),[])}catch(f){return Zt("FetchError",f),[]}};this.getMarketInfo=async(r,i=!1)=>{let o=`margin_trading_market_info_${r}`,t=this._sdk.getCache(o,i);if(t)return t;try{let a=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}}),f=Si(a);return this._sdk.updateCache(o,f,tn),f}catch(a){return Zt("FetchError",a),{}}};this.getMarketSuilendInfo=async r=>{try{let{base_token:i,quote_token:o}=await this.getMarketInfo(r),{lending_market_id:t}=Ee(this._sdk.sdkOptions.suilend),{allLendingMarketData:a,lstAprPercentMap:f}=await this._sdk.SuiLendModule.getLendingMarketData(!0),{base_reserve_map_info:c,quote_reserve_map_info:m}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(i,o,a);console.log("\u{1F680}\u{1F680}\u{1F680} ~ marketModules.ts:188 ~ MarketModules ~ base_reserve_map_info:",c);let v=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:a,lstAprPercentMap:f},!0),_=v[t].rewardMap[i],k=v[t].rewardMap[o],{depositedAmountUsd:S,borrowedAmountUsd:D,depositAprPercent:O,borrowAprPercent:R,depositedAmount:E,borrowedAmount:L}=c,{depositLimit:T,borrowLimit:I}=c.config,{depositedAmountUsd:B,borrowedAmountUsd:W,depositAprPercent:U,borrowAprPercent:Z,depositedAmount:g,borrowedAmount:b}=m,{depositLimit:A,borrowLimit:x}=m.config,C=Ie(Se.DEPOSIT,O,he(_.deposit),cr(Se.DEPOSIT,i,f)),p=Ie(Se.BORROW,R,he(_.borrow)),e=Ie(Se.DEPOSIT,U,he(k.deposit),cr(Se.DEPOSIT,i,f)),s=Ie(Se.BORROW,Z,he(k.borrow)),u=le(B.toString()).sub(W.toString()).toString(),d=le(S.toString()).sub(D.toString()).toString(),h=le(T.toString()).sub(le(E.toString())).toString(),M=le(I.toString()).sub(le(L.toString())).toString(),P=le(A.toString()).sub(le(g.toString())).toString(),w=le(x.toString()).sub(le(b.toString())).toString();return{long_liquidity:u,short_liquidity:d,base_total_deposit_apr_percent:C.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:M,quote_token_available_deposit_amount:P,quote_token_available_borrow_amount:w,base_deposit_rewards:he(_.deposit),quote_deposit_rewards:he(k.deposit),base_borrow_rewards:he(_.borrow),quote_borrow_rewards:he(k.borrow),base_deposit_apr_percent:O.toString(),base_borrow_apr_percent:R.toString(),quote_deposit_apr_percent:U.toString(),quote_borrow_apr_percent:Z.toString(),obligations:v[t],base_reserve_map_info:c,quote_reserve_map_info:m}}catch(i){return Zt("FetchError",i),{}}};this._sdk=r}};import{CLOCK_ADDRESS as Ae,getPackagerConfigs as Yt}from"@cetusprotocol/common-sdk";import{Transaction as Gt}from"@mysten/sui/transactions";var qe=class{constructor(r){this.queryGlobalPermissions=async()=>{let r=this._sdk.sdkOptions.margin_trading,{global_config_id:i}=Yt(r),o=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0}});if(o){let t=o.data.content.fields.permissions.toString(2),{open_permissions_pause:a,close_permissions_pause:f,deposit_permissions_pause:c,withdraw_permissions_pause:m,borrow_permissions_pause:v,repay_permissions_pause:_}=Ci(t);return{open_permissions_pause:a,close_permissions_pause:f,deposit_permissions_pause:c,withdraw_permissions_pause:m,borrow_permissions_pause:v,repay_permissions_pause:_}}};this.updateGlobalPermissions=async r=>{let i=new Gt;return this.updateGlobalOpenPositionPermissions(r,i),this.updateGlobalClosePositionPermissions(r,i),this.updateGlobalDepositPermissions(r,i),this.updateGlobalBorrowPermissions(r,i),this.updateGlobalWithdrawPermissions(r,i),this.updateGlobalRepayPermissions(r,i),i};this.updateGlobalOpenPositionPermissions=async(r,i)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:f}=Yt(o),c=i||new Gt;return c.moveCall({target:`${o.published_at}::config::update_global_open_position_permissions`,arguments:[c.object(f),c.object(t),c.pure.bool(r),c.object(Ae),c.object(a)],typeArguments:[]}),c};this.updateGlobalClosePositionPermissions=async(r,i)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:f}=Yt(o),c=i||new Gt;return c.moveCall({target:`${o.published_at}::config::update_global_close_position_permissions`,arguments:[c.object(f),c.object(t),c.pure.bool(r),c.object(Ae),c.object(a)],typeArguments:[]}),c};this.updateGlobalDepositPermissions=async(r,i)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:f}=Yt(o),c=i||new Gt;return c.moveCall({target:`${o.published_at}::config::update_global_deposit_permissions`,arguments:[c.object(f),c.object(t),c.pure.bool(r),c.object(Ae),c.object(a)],typeArguments:[]}),c};this.updateGlobalBorrowPermissions=async(r,i)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:f}=Yt(o),c=i||new Gt;return c.moveCall({target:`${o.published_at}::config::update_global_borrow_permissions`,arguments:[c.object(f),c.object(t),c.pure.bool(r),c.object(Ae),c.object(a)],typeArguments:[]}),c};this.updateGlobalWithdrawPermissions=async(r,i)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:f}=Yt(o),c=i||new Gt;return c.moveCall({target:`${o.published_at}::config::update_global_withdraw_permissions`,arguments:[c.object(f),c.object(t),c.pure.bool(r),c.object(Ae),c.object(a)],typeArguments:[]}),c};this.updateGlobalRepayPermissions=async(r,i)=>{let o=this._sdk.sdkOptions.margin_trading,{global_config_id:t,versioned_id:a,admin_cap_id:f}=Yt(o),c=i||new Gt;return c.moveCall({target:`${o.published_at}::config::update_global_repay_permissions`,arguments:[c.object(f),c.object(t),c.pure.bool(r),c.object(Ae),c.object(a)],typeArguments:[]}),c};this.updateMarketCreatePositionPermissions=async(r,i,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:f}=Yt(t),c=o||new Gt;return c.moveCall({target:`${t.published_at}::market::update_market_open_position_permission`,arguments:[c.object(f),c.object(r),c.pure.bool(i),c.object(a)],typeArguments:[]}),c};this.updateMarketClosePositionPermissions=async(r,i,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:f}=Yt(t),c=o||new Gt;return c.moveCall({target:`${t.published_at}::market::update_market_close_position_permission`,arguments:[c.object(f),c.object(r),c.pure.bool(i),c.object(a)],typeArguments:[]}),c};this.updateMarketDepositPermissions=async(r,i,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:f}=Yt(t),c=o||new Gt;return c.moveCall({target:`${t.published_at}::market::update_market_deposit_permission`,arguments:[c.object(f),c.object(r),c.pure.bool(i),c.object(a)],typeArguments:[]}),c};this.updateMarketWithdrawPermissions=async(r,i,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:f}=Yt(t),c=o||new Gt;return c.moveCall({target:`${t.published_at}::market::update_market_withdraw_permission`,arguments:[c.object(f),c.object(r),c.pure.bool(i),c.object(a)],typeArguments:[]}),c};this.updateMarketBorrowPermissions=async(r,i,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:f}=Yt(t),c=o||new Gt;return c.moveCall({target:`${t.published_at}::market::update_market_borrow_permission`,arguments:[c.object(f),c.object(r),c.pure.bool(i),c.object(a)],typeArguments:[]}),c};this.updateMarketRepayPermissions=async(r,i,o)=>{let t=this._sdk.sdkOptions.margin_trading,{versioned_id:a,admin_cap_id:f}=Yt(t),c=o||new Gt;return c.moveCall({target:`${t.published_at}::market::update_market_repay_permission`,arguments:[c.object(f),c.object(r),c.pure.bool(i),c.object(a)],typeArguments:[]}),c};this.updateMarketPermissions=async(r,i,o)=>{let t=o||new Gt;return this.updateMarketCreatePositionPermissions(r,i,t),this.updateMarketClosePositionPermissions(r,i,t),this.updateMarketDepositPermissions(r,i,t),this.updateMarketWithdrawPermissions(r,i,t),this.updateMarketBorrowPermissions(r,i,t),this.updateMarketRepayPermissions(r,i,t),t};this._sdk=r}};import{CACHE_TIME_24H as so,CLOCK_ADDRESS as ye,CoinAssist as Q,d as N,DETAILS_KEYS as Je,getObjectFields as Rr,getPackagerConfigs as mt,removeHexPrefix as xe,SUI_SYSTEM_STATE_OBJECT_ID as Lr,U64_MAX as Qe}from"@cetusprotocol/common-sdk";var en=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,Di=Math.ceil,te=Math.floor,Ht="[BigNumber Error] ",dr=Ht+"Number primitive has more than 15 significant digits: ",oe=1e14,et=14,Ti=9007199254740991,Oi=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],pe=1e7,Wt=1e9;function fr(n){var r,i,o,t=T.prototype={constructor:T,toString:null,valueOf:null},a=new T(1),f=20,c=4,m=-7,v=21,_=-1e7,k=1e7,S=!1,D=1,O=0,R={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:"\xA0",suffix:""},E="0123456789abcdefghijklmnopqrstuvwxyz",L=!0;function T(g,b){var A,x,C,p,e,s,u,d,h=this;if(!(h instanceof T))return new T(g,b);if(b==null){if(g&&g._isBigNumber===!0){h.s=g.s,!g.c||g.e>k?h.c=h.e=null:g.e<_?h.c=[h.e=0]:(h.e=g.e,h.c=g.c.slice());return}if((s=typeof g=="number")&&g*0==0){if(h.s=1/g<0?(g=-g,-1):1,g===~~g){for(p=0,e=g;e>=10;e/=10,p++);p>k?h.c=h.e=null:(h.e=p,h.c=[g]);return}d=String(g)}else{if(!en.test(d=String(g)))return o(h,d,s);h.s=d.charCodeAt(0)==45?(d=d.slice(1),-1):1}(p=d.indexOf("."))>-1&&(d=d.replace(".","")),(e=d.search(/e/i))>0?(p<0&&(p=e),p+=+d.slice(e+1),d=d.substring(0,e)):p<0&&(p=d.length)}else{if(ht(b,2,E.length,"Base"),b==10&&L)return h=new T(g),U(h,f+h.e+1,c);if(d=String(g),s=typeof g=="number"){if(g*0!=0)return o(h,d,s,b);if(h.s=1/g<0?(d=d.slice(1),-1):1,T.DEBUG&&d.replace(/^0\.0*|\./,"").length>15)throw Error(dr+g)}else h.s=d.charCodeAt(0)===45?(d=d.slice(1),-1):1;for(A=E.slice(0,b),p=e=0,u=d.length;e<u;e++)if(A.indexOf(x=d.charAt(e))<0){if(x=="."){if(e>p){p=u;continue}}else if(!C&&(d==d.toUpperCase()&&(d=d.toLowerCase())||d==d.toLowerCase()&&(d=d.toUpperCase()))){C=!0,e=-1,p=0;continue}return o(h,String(g),s,b)}s=!1,d=i(d,b,10,h.s),(p=d.indexOf("."))>-1?d=d.replace(".",""):p=d.length}for(e=0;d.charCodeAt(e)===48;e++);for(u=d.length;d.charCodeAt(--u)===48;);if(d=d.slice(e,++u)){if(u-=e,s&&T.DEBUG&&u>15&&(g>Ti||g!==te(g)))throw Error(dr+h.s*g);if((p=p-e-1)>k)h.c=h.e=null;else if(p<_)h.c=[h.e=0];else{if(h.e=p,h.c=[],e=(p+1)%et,p<0&&(e+=et),e<u){for(e&&h.c.push(+d.slice(0,e)),u-=et;e<u;)h.c.push(+d.slice(e,e+=et));e=et-(d=d.slice(e)).length}else e-=u;for(;e--;d+="0");h.c.push(+d)}}else h.c=[h.e=0]}T.clone=fr,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=function(g){var b,A;if(g!=null)if(typeof g=="object"){if(g.hasOwnProperty(b="DECIMAL_PLACES")&&(A=g[b],ht(A,0,Wt,b),f=A),g.hasOwnProperty(b="ROUNDING_MODE")&&(A=g[b],ht(A,0,8,b),c=A),g.hasOwnProperty(b="EXPONENTIAL_AT")&&(A=g[b],A&&A.pop?(ht(A[0],-Wt,0,b),ht(A[1],0,Wt,b),m=A[0],v=A[1]):(ht(A,-Wt,Wt,b),m=-(v=A<0?-A:A))),g.hasOwnProperty(b="RANGE"))if(A=g[b],A&&A.pop)ht(A[0],-Wt,-1,b),ht(A[1],1,Wt,b),_=A[0],k=A[1];else if(ht(A,-Wt,Wt,b),A)_=-(k=A<0?-A:A);else throw Error(Ht+b+" cannot be zero: "+A);if(g.hasOwnProperty(b="CRYPTO"))if(A=g[b],A===!!A)if(A)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))S=A;else throw S=!A,Error(Ht+"crypto unavailable");else S=A;else throw Error(Ht+b+" not true or false: "+A);if(g.hasOwnProperty(b="MODULO_MODE")&&(A=g[b],ht(A,0,9,b),D=A),g.hasOwnProperty(b="POW_PRECISION")&&(A=g[b],ht(A,0,Wt,b),O=A),g.hasOwnProperty(b="FORMAT"))if(A=g[b],typeof A=="object")R=A;else throw Error(Ht+b+" not an object: "+A);if(g.hasOwnProperty(b="ALPHABET"))if(A=g[b],typeof A=="string"&&!/^.?$|[+\-.\s]|(.).*\1/.test(A))L=A.slice(0,10)=="0123456789",E=A;else throw Error(Ht+b+" invalid: "+A)}else throw Error(Ht+"Object expected: "+g);return{DECIMAL_PLACES:f,ROUNDING_MODE:c,EXPONENTIAL_AT:[m,v],RANGE:[_,k],CRYPTO:S,MODULO_MODE:D,POW_PRECISION:O,FORMAT:R,ALPHABET:E}},T.isBigNumber=function(g){if(!g||g._isBigNumber!==!0)return!1;if(!T.DEBUG)return!0;var b,A,x=g.c,C=g.e,p=g.s;t:if({}.toString.call(x)=="[object Array]"){if((p===1||p===-1)&&C>=-Wt&&C<=Wt&&C===te(C)){if(x[0]===0){if(C===0&&x.length===1)return!0;break t}if(b=(C+1)%et,b<1&&(b+=et),String(x[0]).length==b){for(b=0;b<x.length;b++)if(A=x[b],A<0||A>=oe||A!==te(A))break t;if(A!==0)return!0}}}else if(x===null&&C===null&&(p===null||p===1||p===-1))return!0;throw Error(Ht+"Invalid BigNumber: "+g)},T.maximum=T.max=function(){return B(arguments,-1)},T.minimum=T.min=function(){return B(arguments,1)},T.random=(function(){var g=9007199254740992,b=Math.random()*g&2097151?function(){return te(Math.random()*g)}:function(){return(Math.random()*1073741824|0)*8388608+(Math.random()*8388608|0)};return function(A){var x,C,p,e,s,u=0,d=[],h=new T(a);if(A==null?A=f:ht(A,0,Wt),e=Di(A/et),S)if(crypto.getRandomValues){for(x=crypto.getRandomValues(new Uint32Array(e*=2));u<e;)s=x[u]*131072+(x[u+1]>>>11),s>=9e15?(C=crypto.getRandomValues(new Uint32Array(2)),x[u]=C[0],x[u+1]=C[1]):(d.push(s%1e14),u+=2);u=e/2}else if(crypto.randomBytes){for(x=crypto.randomBytes(e*=7);u<e;)s=(x[u]&31)*281474976710656+x[u+1]*1099511627776+x[u+2]*4294967296+x[u+3]*16777216+(x[u+4]<<16)+(x[u+5]<<8)+x[u+6],s>=9e15?crypto.randomBytes(7).copy(x,u):(d.push(s%1e14),u+=7);u=e/7}else throw S=!1,Error(Ht+"crypto unavailable");if(!S)for(;u<e;)s=b(),s<9e15&&(d[u++]=s%1e14);for(e=d[--u],A%=et,e&&A&&(s=Oi[et-A],d[u]=te(e/s)*s);d[u]===0;d.pop(),u--);if(u<0)d=[p=0];else{for(p=-1;d[0]===0;d.splice(0,1),p-=et);for(u=1,s=d[0];s>=10;s/=10,u++);u<et&&(p-=et-u)}return h.e=p,h.c=d,h}})(),T.sum=function(){for(var g=1,b=arguments,A=new T(b[0]);g<b.length;)A=A.plus(b[g++]);return A},i=(function(){var g="0123456789";function b(A,x,C,p){for(var e,s=[0],u,d=0,h=A.length;d<h;){for(u=s.length;u--;s[u]*=x);for(s[0]+=p.indexOf(A.charAt(d++)),e=0;e<s.length;e++)s[e]>C-1&&(s[e+1]==null&&(s[e+1]=0),s[e+1]+=s[e]/C|0,s[e]%=C)}return s.reverse()}return function(A,x,C,p,e){var s,u,d,h,M,P,w,l,y=A.indexOf("."),$=f,q=c;for(y>=0&&(h=O,O=0,A=A.replace(".",""),l=new T(x),P=l.pow(A.length-y),O=h,l.c=b(de(Qt(P.c),P.e,"0"),10,C,g),l.e=l.c.length),w=b(A,x,C,e?(s=E,g):(s=g,E)),d=h=w.length;w[--h]==0;w.pop());if(!w[0])return s.charAt(0);if(y<0?--d:(P.c=w,P.e=d,P.s=p,P=r(P,l,$,q,C),w=P.c,M=P.r,d=P.e),u=d+$+1,y=w[u],h=C/2,M=M||u<0||w[u+1]!=null,M=q<4?(y!=null||M)&&(q==0||q==(P.s<0?3:2)):y>h||y==h&&(q==4||M||q==6&&w[u-1]&1||q==(P.s<0?8:7)),u<1||!w[0])A=M?de(s.charAt(1),-$,s.charAt(0)):s.charAt(0);else{if(w.length=u,M)for(--C;++w[--u]>C;)w[u]=0,u||(++d,w=[1].concat(w));for(h=w.length;!w[--h];);for(y=0,A="";y<=h;A+=s.charAt(w[y++]));A=de(A,d,s.charAt(0))}return A}})(),r=(function(){function g(x,C,p){var e,s,u,d,h=0,M=x.length,P=C%pe,w=C/pe|0;for(x=x.slice();M--;)u=x[M]%pe,d=x[M]/pe|0,e=w*u+d*P,s=P*u+e%pe*pe+h,h=(s/p|0)+(e/pe|0)+w*d,x[M]=s%p;return h&&(x=[h].concat(x)),x}function b(x,C,p,e){var s,u;if(p!=e)u=p>e?1:-1;else for(s=u=0;s<p;s++)if(x[s]!=C[s]){u=x[s]>C[s]?1:-1;break}return u}function A(x,C,p,e){for(var s=0;p--;)x[p]-=s,s=x[p]<C[p]?1:0,x[p]=s*e+x[p]-C[p];for(;!x[0]&&x.length>1;x.splice(0,1));}return function(x,C,p,e,s){var u,d,h,M,P,w,l,y,$,q,j,V,z,G,rt,X,it,st=x.s==C.s?1:-1,J=x.c,H=C.c;if(!J||!J[0]||!H||!H[0])return new T(!x.s||!C.s||(J?H&&J[0]==H[0]:!H)?NaN:J&&J[0]==0||!H?st*0:st/0);for(y=new T(st),$=y.c=[],d=x.e-C.e,st=p+d+1,s||(s=oe,d=ee(x.e/et)-ee(C.e/et),st=st/et|0),h=0;H[h]==(J[h]||0);h++);if(H[h]>(J[h]||0)&&d--,st<0)$.push(1),M=!0;else{for(G=J.length,X=H.length,h=0,st+=2,P=te(s/(H[0]+1)),P>1&&(H=g(H,P,s),J=g(J,P,s),X=H.length,G=J.length),z=X,q=J.slice(0,X),j=q.length;j<X;q[j++]=0);it=H.slice(),it=[0].concat(it),rt=H[0],H[1]>=s/2&&rt++;do{if(P=0,u=b(H,q,X,j),u<0){if(V=q[0],X!=j&&(V=V*s+(q[1]||0)),P=te(V/rt),P>1)for(P>=s&&(P=s-1),w=g(H,P,s),l=w.length,j=q.length;b(w,q,l,j)==1;)P--,A(w,X<l?it:H,l,s),l=w.length,u=1;else P==0&&(u=P=1),w=H.slice(),l=w.length;if(l<j&&(w=[0].concat(w)),A(q,w,j,s),j=q.length,u==-1)for(;b(H,q,X,j)<1;)P++,A(q,X<j?it:H,j,s),j=q.length}else u===0&&(P++,q=[0]);$[h++]=P,q[0]?q[j++]=J[z]||0:(q=[J[z]],j=1)}while((z++<G||q[0]!=null)&&st--);M=q[0]!=null,$[0]||$.splice(0,1)}if(s==oe){for(h=1,st=$[0];st>=10;st/=10,h++);U(y,p+(y.e=h+d*et-1)+1,e,M)}else y.e=d,y.r=+M;return y}})();function I(g,b,A,x){var C,p,e,s,u;if(A==null?A=c:ht(A,0,8),!g.c)return g.toString();if(C=g.c[0],e=g.e,b==null)u=Qt(g.c),u=x==1||x==2&&(e<=m||e>=v)?Ue(u,e):de(u,e,"0");else if(g=U(new T(g),b,A),p=g.e,u=Qt(g.c),s=u.length,x==1||x==2&&(b<=p||p<=m)){for(;s<b;u+="0",s++);u=Ue(u,p)}else if(b-=e+(x===2&&p>e),u=de(u,p,"0"),p+1>s){if(--b>0)for(u+=".";b--;u+="0");}else if(b+=p-s,b>0)for(p+1==s&&(u+=".");b--;u+="0");return g.s<0&&C?"-"+u:u}function B(g,b){for(var A,x,C=1,p=new T(g[0]);C<g.length;C++)x=new T(g[C]),(!x.s||(A=Me(p,x))===b||A===0&&p.s===b)&&(p=x);return p}function W(g,b,A){for(var x=1,C=b.length;!b[--C];b.pop());for(C=b[0];C>=10;C/=10,x++);return(A=x+A*et-1)>k?g.c=g.e=null:A<_?g.c=[g.e=0]:(g.e=A,g.c=b),g}o=(function(){var g=/^(-?)0([xbo])(?=\w[\w.]*$)/i,b=/^([^.]+)\.$/,A=/^\.([^.]+)$/,x=/^-?(Infinity|NaN)$/,C=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(p,e,s,u){var d,h=s?e:e.replace(C,"");if(x.test(h))p.s=isNaN(h)?null:h<0?-1:1;else{if(!s&&(h=h.replace(g,function(M,P,w){return d=(w=w.toLowerCase())=="x"?16:w=="b"?2:8,!u||u==d?P:M}),u&&(d=u,h=h.replace(b,"$1").replace(A,"0.$1")),e!=h))return new T(h,d);if(T.DEBUG)throw Error(Ht+"Not a"+(u?" base "+u:"")+" number: "+e);p.s=null}p.c=p.e=null}})();function U(g,b,A,x){var C,p,e,s,u,d,h,M=g.c,P=Oi;if(M){t:{for(C=1,s=M[0];s>=10;s/=10,C++);if(p=b-C,p<0)p+=et,e=b,u=M[d=0],h=te(u/P[C-e-1]%10);else if(d=Di((p+1)/et),d>=M.length)if(x){for(;M.length<=d;M.push(0));u=h=0,C=1,p%=et,e=p-et+1}else break t;else{for(u=s=M[d],C=1;s>=10;s/=10,C++);p%=et,e=p-et+C,h=e<0?0:te(u/P[C-e-1]%10)}if(x=x||b<0||M[d+1]!=null||(e<0?u:u%P[C-e-1]),x=A<4?(h||x)&&(A==0||A==(g.s<0?3:2)):h>5||h==5&&(A==4||x||A==6&&(p>0?e>0?u/P[C-e]:0:M[d-1])%10&1||A==(g.s<0?8:7)),b<1||!M[0])return M.length=0,x?(b-=g.e+1,M[0]=P[(et-b%et)%et],g.e=-b||0):M[0]=g.e=0,g;if(p==0?(M.length=d,s=1,d--):(M.length=d+1,s=P[et-p],M[d]=e>0?te(u/P[C-e]%P[e])*s:0),x)for(;;)if(d==0){for(p=1,e=M[0];e>=10;e/=10,p++);for(e=M[0]+=s,s=1;e>=10;e/=10,s++);p!=s&&(g.e++,M[0]==oe&&(M[0]=1));break}else{if(M[d]+=s,M[d]!=oe)break;M[d--]=0,s=1}for(p=M.length;M[--p]===0;M.pop());}g.e>k?g.c=g.e=null:g.e<_&&(g.c=[g.e=0])}return g}function Z(g){var b,A=g.e;return A===null?g.toString():(b=Qt(g.c),b=A<=m||A>=v?Ue(b,A):de(b,A,"0"),g.s<0?"-"+b:b)}return t.absoluteValue=t.abs=function(){var g=new T(this);return g.s<0&&(g.s=1),g},t.comparedTo=function(g,b){return Me(this,new T(g,b))},t.decimalPlaces=t.dp=function(g,b){var A,x,C,p=this;if(g!=null)return ht(g,0,Wt),b==null?b=c:ht(b,0,8),U(new T(p),g+p.e+1,b);if(!(A=p.c))return null;if(x=((C=A.length-1)-ee(this.e/et))*et,C=A[C])for(;C%10==0;C/=10,x--);return x<0&&(x=0),x},t.dividedBy=t.div=function(g,b){return r(this,new T(g,b),f,c)},t.dividedToIntegerBy=t.idiv=function(g,b){return r(this,new T(g,b),0,1)},t.exponentiatedBy=t.pow=function(g,b){var A,x,C,p,e,s,u,d,h,M=this;if(g=new T(g),g.c&&!g.isInteger())throw Error(Ht+"Exponent not an integer: "+Z(g));if(b!=null&&(b=new T(b)),s=g.e>14,!M.c||!M.c[0]||M.c[0]==1&&!M.e&&M.c.length==1||!g.c||!g.c[0])return h=new T(Math.pow(+Z(M),s?g.s*(2-Fe(g)):+Z(g))),b?h.mod(b):h;if(u=g.s<0,b){if(b.c?!b.c[0]:!b.s)return new T(NaN);x=!u&&M.isInteger()&&b.isInteger(),x&&(M=M.mod(b))}else{if(g.e>9&&(M.e>0||M.e<-1||(M.e==0?M.c[0]>1||s&&M.c[1]>=24e7:M.c[0]<8e13||s&&M.c[0]<=9999975e7)))return p=M.s<0&&Fe(g)?-0:0,M.e>-1&&(p=1/p),new T(u?1/p:p);O&&(p=Di(O/et+2))}for(s?(A=new T(.5),u&&(g.s=1),d=Fe(g)):(C=Math.abs(+Z(g)),d=C%2),h=new T(a);;){if(d){if(h=h.times(M),!h.c)break;p?h.c.length>p&&(h.c.length=p):x&&(h=h.mod(b))}if(C){if(C=te(C/2),C===0)break;d=C%2}else if(g=g.times(A),U(g,g.e+1,1),g.e>14)d=Fe(g);else{if(C=+Z(g),C===0)break;d=C%2}M=M.times(M),p?M.c&&M.c.length>p&&(M.c.length=p):x&&(M=M.mod(b))}return x?h:(u&&(h=a.div(h)),b?h.mod(b):p?U(h,O,c,e):h)},t.integerValue=function(g){var b=new T(this);return g==null?g=c:ht(g,0,8),U(b,b.e+1,g)},t.isEqualTo=t.eq=function(g,b){return Me(this,new T(g,b))===0},t.isFinite=function(){return!!this.c},t.isGreaterThan=t.gt=function(g,b){return Me(this,new T(g,b))>0},t.isGreaterThanOrEqualTo=t.gte=function(g,b){return(b=Me(this,new T(g,b)))===1||b===0},t.isInteger=function(){return!!this.c&&ee(this.e/et)>this.c.length-2},t.isLessThan=t.lt=function(g,b){return Me(this,new T(g,b))<0},t.isLessThanOrEqualTo=t.lte=function(g,b){return(b=Me(this,new T(g,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(g,b){var A,x,C,p,e=this,s=e.s;if(g=new T(g,b),b=g.s,!s||!b)return new T(NaN);if(s!=b)return g.s=-b,e.plus(g);var u=e.e/et,d=g.e/et,h=e.c,M=g.c;if(!u||!d){if(!h||!M)return h?(g.s=-b,g):new T(M?e:NaN);if(!h[0]||!M[0])return M[0]?(g.s=-b,g):new T(h[0]?e:c==3?-0:0)}if(u=ee(u),d=ee(d),h=h.slice(),s=u-d){for((p=s<0)?(s=-s,C=h):(d=u,C=M),C.reverse(),b=s;b--;C.push(0));C.reverse()}else for(x=(p=(s=h.length)<(b=M.length))?s:b,s=b=0;b<x;b++)if(h[b]!=M[b]){p=h[b]<M[b];break}if(p&&(C=h,h=M,M=C,g.s=-g.s),b=(x=M.length)-(A=h.length),b>0)for(;b--;h[A++]=0);for(b=oe-1;x>s;){if(h[--x]<M[x]){for(A=x;A&&!h[--A];h[A]=b);--h[A],h[x]+=oe}h[x]-=M[x]}for(;h[0]==0;h.splice(0,1),--d);return h[0]?W(g,h,d):(g.s=c==3?-1:1,g.c=[g.e=0],g)},t.modulo=t.mod=function(g,b){var A,x,C=this;return g=new T(g,b),!C.c||!g.s||g.c&&!g.c[0]?new T(NaN):!g.c||C.c&&!C.c[0]?new T(C):(D==9?(x=g.s,g.s=1,A=r(C,g,0,3),g.s=x,A.s*=x):A=r(C,g,0,D),g=C.minus(A.times(g)),!g.c[0]&&D==1&&(g.s=C.s),g)},t.multipliedBy=t.times=function(g,b){var A,x,C,p,e,s,u,d,h,M,P,w,l,y,$,q=this,j=q.c,V=(g=new T(g,b)).c;if(!j||!V||!j[0]||!V[0])return!q.s||!g.s||j&&!j[0]&&!V||V&&!V[0]&&!j?g.c=g.e=g.s=null:(g.s*=q.s,!j||!V?g.c=g.e=null:(g.c=[0],g.e=0)),g;for(x=ee(q.e/et)+ee(g.e/et),g.s*=q.s,u=j.length,M=V.length,u<M&&(l=j,j=V,V=l,C=u,u=M,M=C),C=u+M,l=[];C--;l.push(0));for(y=oe,$=pe,C=M;--C>=0;){for(A=0,P=V[C]%$,w=V[C]/$|0,e=u,p=C+e;p>C;)d=j[--e]%$,h=j[e]/$|0,s=w*d+h*P,d=P*d+s%$*$+l[p]+A,A=(d/y|0)+(s/$|0)+w*h,l[p--]=d%y;l[p]=A}return A?++x:l.splice(0,1),W(g,l,x)},t.negated=function(){var g=new T(this);return g.s=-g.s||null,g},t.plus=function(g,b){var A,x=this,C=x.s;if(g=new T(g,b),b=g.s,!C||!b)return new T(NaN);if(C!=b)return g.s=-b,x.minus(g);var p=x.e/et,e=g.e/et,s=x.c,u=g.c;if(!p||!e){if(!s||!u)return new T(C/0);if(!s[0]||!u[0])return u[0]?g:new T(s[0]?x:C*0)}if(p=ee(p),e=ee(e),s=s.slice(),C=p-e){for(C>0?(e=p,A=u):(C=-C,A=s),A.reverse();C--;A.push(0));A.reverse()}for(C=s.length,b=u.length,C-b<0&&(A=u,u=s,s=A,b=C),C=0;b;)C=(s[--b]=s[b]+u[b]+C)/oe|0,s[b]=oe===s[b]?0:s[b]%oe;return C&&(s=[C].concat(s),++e),W(g,s,e)},t.precision=t.sd=function(g,b){var A,x,C,p=this;if(g!=null&&g!==!!g)return ht(g,1,Wt),b==null?b=c:ht(b,0,8),U(new T(p),g,b);if(!(A=p.c))return null;if(C=A.length-1,x=C*et+1,C=A[C]){for(;C%10==0;C/=10,x--);for(C=A[0];C>=10;C/=10,x++);}return g&&p.e+1>x&&(x=p.e+1),x},t.shiftedBy=function(g){return ht(g,-Ti,Ti),this.times("1e"+g)},t.squareRoot=t.sqrt=function(){var g,b,A,x,C,p=this,e=p.c,s=p.s,u=p.e,d=f+4,h=new T("0.5");if(s!==1||!e||!e[0])return new T(!s||s<0&&(!e||e[0])?NaN:e?p:1/0);if(s=Math.sqrt(+Z(p)),s==0||s==1/0?(b=Qt(e),(b.length+u)%2==0&&(b+="0"),s=Math.sqrt(+b),u=ee((u+1)/2)-(u<0||u%2),s==1/0?b="5e"+u:(b=s.toExponential(),b=b.slice(0,b.indexOf("e")+1)+u),A=new T(b)):A=new T(s+""),A.c[0]){for(u=A.e,s=u+d,s<3&&(s=0);;)if(C=A,A=h.times(C.plus(r(p,C,d,1))),Qt(C.c).slice(0,s)===(b=Qt(A.c)).slice(0,s))if(A.e<u&&--s,b=b.slice(s-3,s+1),b=="9999"||!x&&b=="4999"){if(!x&&(U(C,C.e+f+2,0),C.times(C).eq(p))){A=C;break}d+=4,s+=4,x=1}else{(!+b||!+b.slice(1)&&b.charAt(0)=="5")&&(U(A,A.e+f+2,1),g=!A.times(A).eq(p));break}}return U(A,A.e+f+1,c,g)},t.toExponential=function(g,b){return g!=null&&(ht(g,0,Wt),g++),I(this,g,b,1)},t.toFixed=function(g,b){return g!=null&&(ht(g,0,Wt),g=g+this.e+1),I(this,g,b)},t.toFormat=function(g,b,A){var x,C=this;if(A==null)g!=null&&b&&typeof b=="object"?(A=b,b=null):g&&typeof g=="object"?(A=g,g=b=null):A=R;else if(typeof A!="object")throw Error(Ht+"Argument not an object: "+A);if(x=C.toFixed(g,b),C.c){var p,e=x.split("."),s=+A.groupSize,u=+A.secondaryGroupSize,d=A.groupSeparator||"",h=e[0],M=e[1],P=C.s<0,w=P?h.slice(1):h,l=w.length;if(u&&(p=s,s=u,u=p,l-=p),s>0&&l>0){for(p=l%s||s,h=w.substr(0,p);p<l;p+=s)h+=d+w.substr(p,s);u>0&&(h+=d+w.slice(p)),P&&(h="-"+h)}x=M?h+(A.decimalSeparator||"")+((u=+A.fractionGroupSize)?M.replace(new RegExp("\\d{"+u+"}\\B","g"),"$&"+(A.fractionGroupSeparator||"")):M):h}return(A.prefix||"")+x+(A.suffix||"")},t.toFraction=function(g){var b,A,x,C,p,e,s,u,d,h,M,P,w=this,l=w.c;if(g!=null&&(s=new T(g),!s.isInteger()&&(s.c||s.s!==1)||s.lt(a)))throw Error(Ht+"Argument "+(s.isInteger()?"out of range: ":"not an integer: ")+Z(s));if(!l)return new T(w);for(b=new T(a),d=A=new T(a),x=u=new T(a),P=Qt(l),p=b.e=P.length-w.e-1,b.c[0]=Oi[(e=p%et)<0?et+e:e],g=!g||s.comparedTo(b)>0?p>0?b:d:s,e=k,k=1/0,s=new T(P),u.c[0]=0;h=r(s,b,0,1),C=A.plus(h.times(x)),C.comparedTo(g)!=1;)A=x,x=C,d=u.plus(h.times(C=d)),u=C,b=s.minus(h.times(C=b)),s=C;return C=r(g.minus(A),x,0,1),u=u.plus(C.times(d)),A=A.plus(C.times(x)),u.s=d.s=w.s,p=p*2,M=r(d,x,p,c).minus(w).abs().comparedTo(r(u,A,p,c).minus(w).abs())<1?[d,x]:[u,A],k=e,M},t.toNumber=function(){return+Z(this)},t.toPrecision=function(g,b){return g!=null&&ht(g,1,Wt),I(this,g,b,2)},t.toString=function(g){var b,A=this,x=A.s,C=A.e;return C===null?x?(b="Infinity",x<0&&(b="-"+b)):b="NaN":(g==null?b=C<=m||C>=v?Ue(Qt(A.c),C):de(Qt(A.c),C,"0"):g===10&&L?(A=U(new T(A),f+C+1,c),b=de(Qt(A.c),A.e,"0")):(ht(g,2,E.length,"Base"),b=i(de(Qt(A.c),C,"0"),10,g,x,!0)),x<0&&A.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&&T.set(n),T}function ee(n){var r=n|0;return n>0||n===r?r:r-1}function Qt(n){for(var r,i,o=1,t=n.length,a=n[0]+"";o<t;){for(r=n[o++]+"",i=et-r.length;i--;r="0"+r);a+=r}for(t=a.length;a.charCodeAt(--t)===48;);return a.slice(0,t+1||1)}function Me(n,r){var i,o,t=n.c,a=r.c,f=n.s,c=r.s,m=n.e,v=r.e;if(!f||!c)return null;if(i=t&&!t[0],o=a&&!a[0],i||o)return i?o?0:-c:f;if(f!=c)return f;if(i=f<0,o=m==v,!t||!a)return o?0:!t^i?1:-1;if(!o)return m>v^i?1:-1;for(c=(m=t.length)<(v=a.length)?m:v,f=0;f<c;f++)if(t[f]!=a[f])return t[f]>a[f]^i?1:-1;return m==v?0:m>v^i?1:-1}function ht(n,r,i,o){if(n<r||n>i||n!==te(n))throw Error(Ht+(o||"Argument")+(typeof n=="number"?n<r||n>i?" out of range: ":" not an integer: ":" not a primitive number: ")+String(n))}function Fe(n){var r=n.c.length-1;return ee(n.e/et)==r&&n.c[r]%2!=0}function Ue(n,r){return(n.length>1?n.charAt(0)+"."+n.slice(1):n)+(r<0?"e":"e+")+r}function de(n,r,i){var o,t;if(r<0){for(t=i+".";++r;t+=i);n=t+n}else if(o=n.length,++r>o){for(t=i,r-=o;--r;t+=i);n+=t}else r<o&&(n=n.slice(0,r)+"."+n.slice(r));return n}var rn=fr(),nt=rn;import{Transaction as ie}from"@mysten/sui/transactions";import{bcs as ao}from"@mysten/sui/bcs";var Pe=9e15,we=1e9,Ri="0123456789abcdef",We="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",Ze="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Li={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Pe,maxE:Pe,crypto:!1},gr,fe,tt=!0,Ke="[DecimalError] ",_e=Ke+"Invalid argument: ",_r=Ke+"Precision limit exceeded",wr=Ke+"crypto unavailable",vr="[object Decimal]",zt=Math.floor,$t=Math.pow,nn=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,on=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,sn=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,Mr=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,se=1e7,Y=7,an=9007199254740991,ln=We.length-1,Ei=Ze.length-1,F={toStringTag:vr};F.absoluteValue=F.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),K(n)};F.ceil=function(){return K(new this.constructor(this),this.e+1,2)};F.clampedTo=F.clamp=function(n,r){var i,o=this,t=o.constructor;if(n=new t(n),r=new t(r),!n.s||!r.s)return new t(NaN);if(n.gt(r))throw Error(_e+r);return i=o.cmp(n),i<0?n:o.cmp(r)>0?r:new t(o)};F.comparedTo=F.cmp=function(n){var r,i,o,t,a=this,f=a.d,c=(n=new a.constructor(n)).d,m=a.s,v=n.s;if(!f||!c)return!m||!v?NaN:m!==v?m:f===c?0:!f^m<0?1:-1;if(!f[0]||!c[0])return f[0]?m:c[0]?-v:0;if(m!==v)return m;if(a.e!==n.e)return a.e>n.e^m<0?1:-1;for(o=f.length,t=c.length,r=0,i=o<t?o:t;r<i;++r)if(f[r]!==c[r])return f[r]>c[r]^m<0?1:-1;return o===t?0:o>t^m<0?1:-1};F.cosine=F.cos=function(){var n,r,i=this,o=i.constructor;return i.d?i.d[0]?(n=o.precision,r=o.rounding,o.precision=n+Math.max(i.e,i.sd())+Y,o.rounding=1,i=un(o,Ar(o,i)),o.precision=n,o.rounding=r,K(fe==2||fe==3?i.neg():i,n,r,!0)):new o(1):new o(NaN)};F.cubeRoot=F.cbrt=function(){var n,r,i,o,t,a,f,c,m,v,_=this,k=_.constructor;if(!_.isFinite()||_.isZero())return new k(_);for(tt=!1,a=_.s*$t(_.s*_,1/3),!a||Math.abs(a)==1/0?(i=Vt(_.d),n=_.e,(a=(n-i.length+1)%3)&&(i+=a==1||a==-2?"0":"00"),a=$t(i,1/3),n=zt((n+1)/3)-(n%3==(n<0?-1:2)),a==1/0?i="5e"+n:(i=a.toExponential(),i=i.slice(0,i.indexOf("e")+1)+n),o=new k(i),o.s=_.s):o=new k(a.toString()),f=(n=k.precision)+3;;)if(c=o,m=c.times(c).times(c),v=m.plus(_),o=ut(v.plus(_).times(c),v.plus(m),f+2,1),Vt(c.d).slice(0,f)===(i=Vt(o.d)).slice(0,f))if(i=i.slice(f-3,f+1),i=="9999"||!t&&i=="4999"){if(!t&&(K(c,n+1,0),c.times(c).times(c).eq(_))){o=c;break}f+=4,t=1}else{(!+i||!+i.slice(1)&&i.charAt(0)=="5")&&(K(o,n+1,1),r=!o.times(o).times(o).eq(_));break}return tt=!0,K(o,n,k.rounding,r)};F.decimalPlaces=F.dp=function(){var n,r=this.d,i=NaN;if(r){if(n=r.length-1,i=(n-zt(this.e/Y))*Y,n=r[n],n)for(;n%10==0;n/=10)i--;i<0&&(i=0)}return i};F.dividedBy=F.div=function(n){return ut(this,new this.constructor(n))};F.dividedToIntegerBy=F.divToInt=function(n){var r=this,i=r.constructor;return K(ut(r,new i(n),0,1,1),i.precision,i.rounding)};F.equals=F.eq=function(n){return this.cmp(n)===0};F.floor=function(){return K(new this.constructor(this),this.e+1,3)};F.greaterThan=F.gt=function(n){return this.cmp(n)>0};F.greaterThanOrEqualTo=F.gte=function(n){var r=this.cmp(n);return r==1||r===0};F.hyperbolicCosine=F.cosh=function(){var n,r,i,o,t,a=this,f=a.constructor,c=new f(1);if(!a.isFinite())return new f(a.s?1/0:NaN);if(a.isZero())return c;i=f.precision,o=f.rounding,f.precision=i+Math.max(a.e,a.sd())+4,f.rounding=1,t=a.d.length,t<32?(n=Math.ceil(t/3),r=(1/Ge(4,n)).toString()):(n=16,r="2.3283064365386962890625e-10"),a=Ce(f,1,a.times(r),new f(1),!0);for(var m,v=n,_=new f(8);v--;)m=a.times(a),a=c.minus(m.times(_.minus(m.times(_))));return K(a,f.precision=i,f.rounding=o,!0)};F.hyperbolicSine=F.sinh=function(){var n,r,i,o,t=this,a=t.constructor;if(!t.isFinite()||t.isZero())return new a(t);if(r=a.precision,i=a.rounding,a.precision=r+Math.max(t.e,t.sd())+4,a.rounding=1,o=t.d.length,o<3)t=Ce(a,2,t,t,!0);else{n=1.4*Math.sqrt(o),n=n>16?16:n|0,t=t.times(1/Ge(5,n)),t=Ce(a,2,t,t,!0);for(var f,c=new a(5),m=new a(16),v=new a(20);n--;)f=t.times(t),t=t.times(c.plus(f.times(m.times(f).plus(v))))}return a.precision=r,a.rounding=i,K(t,r,i,!0)};F.hyperbolicTangent=F.tanh=function(){var n,r,i=this,o=i.constructor;return i.isFinite()?i.isZero()?new o(i):(n=o.precision,r=o.rounding,o.precision=n+7,o.rounding=1,ut(i.sinh(),i.cosh(),o.precision=n,o.rounding=r)):new o(i.s)};F.inverseCosine=F.acos=function(){var n=this,r=n.constructor,i=n.abs().cmp(1),o=r.precision,t=r.rounding;return i!==-1?i===0?n.isNeg()?ue(r,o,t):new r(0):new r(NaN):n.isZero()?ue(r,o+4,t).times(.5):(r.precision=o+6,r.rounding=1,n=new r(1).minus(n).div(n.plus(1)).sqrt().atan(),r.precision=o,r.rounding=t,n.times(2))};F.inverseHyperbolicCosine=F.acosh=function(){var n,r,i=this,o=i.constructor;return i.lte(1)?new o(i.eq(1)?0:NaN):i.isFinite()?(n=o.precision,r=o.rounding,o.precision=n+Math.max(Math.abs(i.e),i.sd())+4,o.rounding=1,tt=!1,i=i.times(i).minus(1).sqrt().plus(i),tt=!0,o.precision=n,o.rounding=r,i.ln()):new o(i)};F.inverseHyperbolicSine=F.asinh=function(){var n,r,i=this,o=i.constructor;return!i.isFinite()||i.isZero()?new o(i):(n=o.precision,r=o.rounding,o.precision=n+2*Math.max(Math.abs(i.e),i.sd())+6,o.rounding=1,tt=!1,i=i.times(i).plus(1).sqrt().plus(i),tt=!0,o.precision=n,o.rounding=r,i.ln())};F.inverseHyperbolicTangent=F.atanh=function(){var n,r,i,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,r=a.rounding,o=t.sd(),Math.max(o,n)<2*-t.e-1?K(new a(t),n,r,!0):(a.precision=i=o-t.e,t=ut(t.plus(1),new a(1).minus(t),i+n,1),a.precision=n+4,a.rounding=1,t=t.ln(),a.precision=n,a.rounding=r,t.times(.5))):new a(NaN)};F.inverseSine=F.asin=function(){var n,r,i,o,t=this,a=t.constructor;return t.isZero()?new a(t):(r=t.abs().cmp(1),i=a.precision,o=a.rounding,r!==-1?r===0?(n=ue(a,i+4,o).times(.5),n.s=t.s,n):new a(NaN):(a.precision=i+6,a.rounding=1,t=t.div(new a(1).minus(t.times(t)).sqrt().plus(1)).atan(),a.precision=i,a.rounding=o,t.times(2)))};F.inverseTangent=F.atan=function(){var n,r,i,o,t,a,f,c,m,v=this,_=v.constructor,k=_.precision,S=_.rounding;if(v.isFinite()){if(v.isZero())return new _(v);if(v.abs().eq(1)&&k+4<=Ei)return f=ue(_,k+4,S).times(.25),f.s=v.s,f}else{if(!v.s)return new _(NaN);if(k+4<=Ei)return f=ue(_,k+4,S).times(.5),f.s=v.s,f}for(_.precision=c=k+10,_.rounding=1,i=Math.min(28,c/Y+2|0),n=i;n;--n)v=v.div(v.times(v).plus(1).sqrt().plus(1));for(tt=!1,r=Math.ceil(c/Y),o=1,m=v.times(v),f=new _(v),t=v;n!==-1;)if(t=t.times(m),a=f.minus(t.div(o+=2)),t=t.times(m),f=a.plus(t.div(o+=2)),f.d[r]!==void 0)for(n=r;f.d[n]===a.d[n]&&n--;);return i&&(f=f.times(2<<i-1)),tt=!0,K(f,_.precision=k,_.rounding=S,!0)};F.isFinite=function(){return!!this.d};F.isInteger=F.isInt=function(){return!!this.d&&zt(this.e/Y)>this.d.length-2};F.isNaN=function(){return!this.s};F.isNegative=F.isNeg=function(){return this.s<0};F.isPositive=F.isPos=function(){return this.s>0};F.isZero=function(){return!!this.d&&this.d[0]===0};F.lessThan=F.lt=function(n){return this.cmp(n)<0};F.lessThanOrEqualTo=F.lte=function(n){return this.cmp(n)<1};F.logarithm=F.log=function(n){var r,i,o,t,a,f,c,m,v=this,_=v.constructor,k=_.precision,S=_.rounding,D=5;if(n==null)n=new _(10),r=!0;else{if(n=new _(n),i=n.d,n.s<0||!i||!i[0]||n.eq(1))return new _(NaN);r=n.eq(10)}if(i=v.d,v.s<0||!i||!i[0]||v.eq(1))return new _(i&&!i[0]?-1/0:v.s!=1?NaN:i?0:1/0);if(r)if(i.length>1)a=!0;else{for(t=i[0];t%10===0;)t/=10;a=t!==1}if(tt=!1,c=k+D,f=ge(v,c),o=r?Ve(_,c+10):ge(n,c),m=ut(f,o,c,1),Te(m.d,t=k,S))do if(c+=10,f=ge(v,c),o=r?Ve(_,c+10):ge(n,c),m=ut(f,o,c,1),!a){+Vt(m.d).slice(t+1,t+15)+1==1e14&&(m=K(m,k+1,0));break}while(Te(m.d,t+=10,S));return tt=!0,K(m,k,S)};F.minus=F.sub=function(n){var r,i,o,t,a,f,c,m,v,_,k,S,D=this,O=D.constructor;if(n=new O(n),!D.d||!n.d)return!D.s||!n.s?n=new O(NaN):D.d?n.s=-n.s:n=new O(n.d||D.s!==n.s?D:NaN),n;if(D.s!=n.s)return n.s=-n.s,D.plus(n);if(v=D.d,S=n.d,c=O.precision,m=O.rounding,!v[0]||!S[0]){if(S[0])n.s=-n.s;else if(v[0])n=new O(D);else return new O(m===3?-0:0);return tt?K(n,c,m):n}if(i=zt(n.e/Y),_=zt(D.e/Y),v=v.slice(),a=_-i,a){for(k=a<0,k?(r=v,a=-a,f=S.length):(r=S,i=_,f=v.length),o=Math.max(Math.ceil(c/Y),f)+2,a>o&&(a=o,r.length=1),r.reverse(),o=a;o--;)r.push(0);r.reverse()}else{for(o=v.length,f=S.length,k=o<f,k&&(f=o),o=0;o<f;o++)if(v[o]!=S[o]){k=v[o]<S[o];break}a=0}for(k&&(r=v,v=S,S=r,n.s=-n.s),f=v.length,o=S.length-f;o>0;--o)v[f++]=0;for(o=S.length;o>a;){if(v[--o]<S[o]){for(t=o;t&&v[--t]===0;)v[t]=se-1;--v[t],v[o]+=se}v[o]-=S[o]}for(;v[--f]===0;)v.pop();for(;v[0]===0;v.shift())--i;return v[0]?(n.d=v,n.e=ze(v,i),tt?K(n,c,m):n):new O(m===3?-0:0)};F.modulo=F.mod=function(n){var r,i=this,o=i.constructor;return n=new o(n),!i.d||!n.s||n.d&&!n.d[0]?new o(NaN):!n.d||i.d&&!i.d[0]?K(new o(i),o.precision,o.rounding):(tt=!1,o.modulo==9?(r=ut(i,n.abs(),0,3,1),r.s*=n.s):r=ut(i,n,0,o.modulo,1),r=r.times(n),tt=!0,i.minus(r))};F.naturalExponential=F.exp=function(){return Ni(this)};F.naturalLogarithm=F.ln=function(){return ge(this)};F.negated=F.neg=function(){var n=new this.constructor(this);return n.s=-n.s,K(n)};F.plus=F.add=function(n){var r,i,o,t,a,f,c,m,v,_,k=this,S=k.constructor;if(n=new S(n),!k.d||!n.d)return!k.s||!n.s?n=new S(NaN):k.d||(n=new S(n.d||k.s===n.s?k:NaN)),n;if(k.s!=n.s)return n.s=-n.s,k.minus(n);if(v=k.d,_=n.d,c=S.precision,m=S.rounding,!v[0]||!_[0])return _[0]||(n=new S(k)),tt?K(n,c,m):n;if(a=zt(k.e/Y),o=zt(n.e/Y),v=v.slice(),t=a-o,t){for(t<0?(i=v,t=-t,f=_.length):(i=_,o=a,f=v.length),a=Math.ceil(c/Y),f=a>f?a+1:f+1,t>f&&(t=f,i.length=1),i.reverse();t--;)i.push(0);i.reverse()}for(f=v.length,t=_.length,f-t<0&&(t=f,i=_,_=v,v=i),r=0;t;)r=(v[--t]=v[t]+_[t]+r)/se|0,v[t]%=se;for(r&&(v.unshift(r),++o),f=v.length;v[--f]==0;)v.pop();return n.d=v,n.e=ze(v,o),tt?K(n,c,m):n};F.precision=F.sd=function(n){var r,i=this;if(n!==void 0&&n!==!!n&&n!==1&&n!==0)throw Error(_e+n);return i.d?(r=br(i.d),n&&i.e+1>r&&(r=i.e+1)):r=NaN,r};F.round=function(){var n=this,r=n.constructor;return K(new r(n),n.e+1,r.rounding)};F.sine=F.sin=function(){var n,r,i=this,o=i.constructor;return i.isFinite()?i.isZero()?new o(i):(n=o.precision,r=o.rounding,o.precision=n+Math.max(i.e,i.sd())+Y,o.rounding=1,i=dn(o,Ar(o,i)),o.precision=n,o.rounding=r,K(fe>2?i.neg():i,n,r,!0)):new o(NaN)};F.squareRoot=F.sqrt=function(){var n,r,i,o,t,a,f=this,c=f.d,m=f.e,v=f.s,_=f.constructor;if(v!==1||!c||!c[0])return new _(!v||v<0&&(!c||c[0])?NaN:c?f:1/0);for(tt=!1,v=Math.sqrt(+f),v==0||v==1/0?(r=Vt(c),(r.length+m)%2==0&&(r+="0"),v=Math.sqrt(r),m=zt((m+1)/2)-(m<0||m%2),v==1/0?r="5e"+m:(r=v.toExponential(),r=r.slice(0,r.indexOf("e")+1)+m),o=new _(r)):o=new _(v.toString()),i=(m=_.precision)+3;;)if(a=o,o=a.plus(ut(f,a,i+2,1)).times(.5),Vt(a.d).slice(0,i)===(r=Vt(o.d)).slice(0,i))if(r=r.slice(i-3,i+1),r=="9999"||!t&&r=="4999"){if(!t&&(K(a,m+1,0),a.times(a).eq(f))){o=a;break}i+=4,t=1}else{(!+r||!+r.slice(1)&&r.charAt(0)=="5")&&(K(o,m+1,1),n=!o.times(o).eq(f));break}return tt=!0,K(o,m,_.rounding,n)};F.tangent=F.tan=function(){var n,r,i=this,o=i.constructor;return i.isFinite()?i.isZero()?new o(i):(n=o.precision,r=o.rounding,o.precision=n+10,o.rounding=1,i=i.sin(),i.s=1,i=ut(i,new o(1).minus(i.times(i)).sqrt(),n+10,0),o.precision=n,o.rounding=r,K(fe==2||fe==4?i.neg():i,n,r,!0)):new o(NaN)};F.times=F.mul=function(n){var r,i,o,t,a,f,c,m,v,_=this,k=_.constructor,S=_.d,D=(n=new k(n)).d;if(n.s*=_.s,!S||!S[0]||!D||!D[0])return new k(!n.s||S&&!S[0]&&!D||D&&!D[0]&&!S?NaN:!S||!D?n.s/0:n.s*0);for(i=zt(_.e/Y)+zt(n.e/Y),m=S.length,v=D.length,m<v&&(a=S,S=D,D=a,f=m,m=v,v=f),a=[],f=m+v,o=f;o--;)a.push(0);for(o=v;--o>=0;){for(r=0,t=m+o;t>o;)c=a[t]+D[o]*S[t-o-1]+r,a[t--]=c%se|0,r=c/se|0;a[t]=(a[t]+r)%se|0}for(;!a[--f];)a.pop();return r?++i:a.shift(),n.d=a,n.e=ze(a,i),tt?K(n,k.precision,k.rounding):n};F.toBinary=function(n,r){return Ii(this,2,n,r)};F.toDecimalPlaces=F.toDP=function(n,r){var i=this,o=i.constructor;return i=new o(i),n===void 0?i:(Xt(n,0,we),r===void 0?r=o.rounding:Xt(r,0,8),K(i,n+i.e+1,r))};F.toExponential=function(n,r){var i,o=this,t=o.constructor;return n===void 0?i=ce(o,!0):(Xt(n,0,we),r===void 0?r=t.rounding:Xt(r,0,8),o=K(new t(o),n+1,r),i=ce(o,!0,n+1)),o.isNeg()&&!o.isZero()?"-"+i:i};F.toFixed=function(n,r){var i,o,t=this,a=t.constructor;return n===void 0?i=ce(t):(Xt(n,0,we),r===void 0?r=a.rounding:Xt(r,0,8),o=K(new a(t),n+t.e+1,r),i=ce(o,!1,n+o.e+1)),t.isNeg()&&!t.isZero()?"-"+i:i};F.toFraction=function(n){var r,i,o,t,a,f,c,m,v,_,k,S,D=this,O=D.d,R=D.constructor;if(!O)return new R(D);if(v=i=new R(1),o=m=new R(0),r=new R(o),a=r.e=br(O)-D.e-1,f=a%Y,r.d[0]=$t(10,f<0?Y+f:f),n==null)n=a>0?r:v;else{if(c=new R(n),!c.isInt()||c.lt(v))throw Error(_e+c);n=c.gt(r)?a>0?r:v:c}for(tt=!1,c=new R(Vt(O)),_=R.precision,R.precision=a=O.length*Y*2;k=ut(c,r,0,1,1),t=i.plus(k.times(o)),t.cmp(n)!=1;)i=o,o=t,t=v,v=m.plus(k.times(t)),m=t,t=r,r=c.minus(k.times(t)),c=t;return t=ut(n.minus(i),o,0,1,1),m=m.plus(t.times(v)),i=i.plus(t.times(o)),m.s=v.s=D.s,S=ut(v,o,a,1).minus(D).abs().cmp(ut(m,i,a,1).minus(D).abs())<1?[v,o]:[m,i],R.precision=_,tt=!0,S};F.toHexadecimal=F.toHex=function(n,r){return Ii(this,16,n,r)};F.toNearest=function(n,r){var i=this,o=i.constructor;if(i=new o(i),n==null){if(!i.d)return i;n=new o(1),r=o.rounding}else{if(n=new o(n),r===void 0?r=o.rounding:Xt(r,0,8),!i.d)return n.s?i:n;if(!n.d)return n.s&&(n.s=i.s),n}return n.d[0]?(tt=!1,i=ut(i,n,0,r,1).times(n),tt=!0,K(i)):(n.s=i.s,i=n),i};F.toNumber=function(){return+this};F.toOctal=function(n,r){return Ii(this,8,n,r)};F.toPower=F.pow=function(n){var r,i,o,t,a,f,c=this,m=c.constructor,v=+(n=new m(n));if(!c.d||!n.d||!c.d[0]||!n.d[0])return new m($t(+c,v));if(c=new m(c),c.eq(1))return c;if(o=m.precision,a=m.rounding,n.eq(1))return K(c,o,a);if(r=zt(n.e/Y),r>=n.d.length-1&&(i=v<0?-v:v)<=an)return t=yr(m,c,i,o),n.s<0?new m(1).div(t):K(t,o,a);if(f=c.s,f<0){if(r<n.d.length-1)return new m(NaN);if((n.d[r]&1)==0&&(f=1),c.e==0&&c.d[0]==1&&c.d.length==1)return c.s=f,c}return i=$t(+c,v),r=i==0||!isFinite(i)?zt(v*(Math.log("0."+Vt(c.d))/Math.LN10+c.e+1)):new m(i+"").e,r>m.maxE+1||r<m.minE-1?new m(r>0?f/0:0):(tt=!1,m.rounding=c.s=1,i=Math.min(12,(r+"").length),t=Ni(n.times(ge(c,o+i)),o),t.d&&(t=K(t,o+5,1),Te(t.d,o,a)&&(r=o+10,t=K(Ni(n.times(ge(c,r+i)),r),r+5,1),+Vt(t.d).slice(o+1,o+15)+1==1e14&&(t=K(t,o+1,0)))),t.s=f,tt=!0,m.rounding=a,K(t,o,a))};F.toPrecision=function(n,r){var i,o=this,t=o.constructor;return n===void 0?i=ce(o,o.e<=t.toExpNeg||o.e>=t.toExpPos):(Xt(n,1,we),r===void 0?r=t.rounding:Xt(r,0,8),o=K(new t(o),n,r),i=ce(o,n<=o.e||o.e<=t.toExpNeg,n)),o.isNeg()&&!o.isZero()?"-"+i:i};F.toSignificantDigits=F.toSD=function(n,r){var i=this,o=i.constructor;return n===void 0?(n=o.precision,r=o.rounding):(Xt(n,1,we),r===void 0?r=o.rounding:Xt(r,0,8)),K(new o(i),n,r)};F.toString=function(){var n=this,r=n.constructor,i=ce(n,n.e<=r.toExpNeg||n.e>=r.toExpPos);return n.isNeg()&&!n.isZero()?"-"+i:i};F.truncated=F.trunc=function(){return K(new this.constructor(this),this.e+1,1)};F.valueOf=F.toJSON=function(){var n=this,r=n.constructor,i=ce(n,n.e<=r.toExpNeg||n.e>=r.toExpPos);return n.isNeg()?"-"+i:i};function Vt(n){var r,i,o,t=n.length-1,a="",f=n[0];if(t>0){for(a+=f,r=1;r<t;r++)o=n[r]+"",i=Y-o.length,i&&(a+=me(i)),a+=o;f=n[r],o=f+"",i=Y-o.length,i&&(a+=me(i))}else if(f===0)return"0";for(;f%10===0;)f/=10;return a+f}function Xt(n,r,i){if(n!==~~n||n<r||n>i)throw Error(_e+n)}function Te(n,r,i,o){var t,a,f,c;for(a=n[0];a>=10;a/=10)--r;return--r<0?(r+=Y,t=0):(t=Math.ceil((r+1)/Y),r%=Y),a=$t(10,Y-r),c=n[t]%a|0,o==null?r<3?(r==0?c=c/100|0:r==1&&(c=c/10|0),f=i<4&&c==99999||i>3&&c==49999||c==5e4||c==0):f=(i<4&&c+1==a||i>3&&c+1==a/2)&&(n[t+1]/a/100|0)==$t(10,r-2)-1||(c==a/2||c==0)&&(n[t+1]/a/100|0)==0:r<4?(r==0?c=c/1e3|0:r==1?c=c/100|0:r==2&&(c=c/10|0),f=(o||i<4)&&c==9999||!o&&i>3&&c==4999):f=((o||i<4)&&c+1==a||!o&&i>3&&c+1==a/2)&&(n[t+1]/a/1e3|0)==$t(10,r-3)-1,f}function je(n,r,i){for(var o,t=[0],a,f=0,c=n.length;f<c;){for(a=t.length;a--;)t[a]*=r;for(t[0]+=Ri.indexOf(n.charAt(f++)),o=0;o<t.length;o++)t[o]>i-1&&(t[o+1]===void 0&&(t[o+1]=0),t[o+1]+=t[o]/i|0,t[o]%=i)}return t.reverse()}function un(n,r){var i,o,t;if(r.isZero())return r;o=r.d.length,o<32?(i=Math.ceil(o/3),t=(1/Ge(4,i)).toString()):(i=16,t="2.3283064365386962890625e-10"),n.precision+=i,r=Ce(n,1,r.times(t),new n(1));for(var a=i;a--;){var f=r.times(r);r=f.times(f).minus(f).times(8).plus(1)}return n.precision-=i,r}var ut=(function(){function n(o,t,a){var f,c=0,m=o.length;for(o=o.slice();m--;)f=o[m]*t+c,o[m]=f%a|0,c=f/a|0;return c&&o.unshift(c),o}function r(o,t,a,f){var c,m;if(a!=f)m=a>f?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 i(o,t,a,f){for(var c=0;a--;)o[a]-=c,c=o[a]<t[a]?1:0,o[a]=c*f+o[a]-t[a];for(;!o[0]&&o.length>1;)o.shift()}return function(o,t,a,f,c,m){var v,_,k,S,D,O,R,E,L,T,I,B,W,U,Z,g,b,A,x,C,p=o.constructor,e=o.s==t.s?1:-1,s=o.d,u=t.d;if(!s||!s[0]||!u||!u[0])return new p(!o.s||!t.s||(s?u&&s[0]==u[0]:!u)?NaN:s&&s[0]==0||!u?e*0:e/0);for(m?(D=1,_=o.e-t.e):(m=se,D=Y,_=zt(o.e/D)-zt(t.e/D)),x=u.length,b=s.length,L=new p(e),T=L.d=[],k=0;u[k]==(s[k]||0);k++);if(u[k]>(s[k]||0)&&_--,a==null?(U=a=p.precision,f=p.rounding):c?U=a+(o.e-t.e)+1:U=a,U<0)T.push(1),O=!0;else{if(U=U/D+2|0,k=0,x==1){for(S=0,u=u[0],U++;(k<b||S)&&U--;k++)Z=S*m+(s[k]||0),T[k]=Z/u|0,S=Z%u|0;O=S||k<b}else{for(S=m/(u[0]+1)|0,S>1&&(u=n(u,S,m),s=n(s,S,m),x=u.length,b=s.length),g=x,I=s.slice(0,x),B=I.length;B<x;)I[B++]=0;C=u.slice(),C.unshift(0),A=u[0],u[1]>=m/2&&++A;do S=0,v=r(u,I,x,B),v<0?(W=I[0],x!=B&&(W=W*m+(I[1]||0)),S=W/A|0,S>1?(S>=m&&(S=m-1),R=n(u,S,m),E=R.length,B=I.length,v=r(R,I,E,B),v==1&&(S--,i(R,x<E?C:u,E,m))):(S==0&&(v=S=1),R=u.slice()),E=R.length,E<B&&R.unshift(0),i(I,R,B,m),v==-1&&(B=I.length,v=r(u,I,x,B),v<1&&(S++,i(I,x<B?C:u,B,m))),B=I.length):v===0&&(S++,I=[0]),T[k++]=S,v&&I[0]?I[B++]=s[g]||0:(I=[s[g]],B=1);while((g++<b||I[0]!==void 0)&&U--);O=I[0]!==void 0}T[0]||T.shift()}if(D==1)L.e=_,gr=O;else{for(k=1,S=T[0];S>=10;S/=10)k++;L.e=k+_*D-1,K(L,c?a+L.e+1:a,f,O)}return L}})();function K(n,r,i,o){var t,a,f,c,m,v,_,k,S,D=n.constructor;t:if(r!=null){if(k=n.d,!k)return n;for(t=1,c=k[0];c>=10;c/=10)t++;if(a=r-t,a<0)a+=Y,f=r,_=k[S=0],m=_/$t(10,t-f-1)%10|0;else if(S=Math.ceil((a+1)/Y),c=k.length,S>=c)if(o){for(;c++<=S;)k.push(0);_=m=0,t=1,a%=Y,f=a-Y+1}else break t;else{for(_=c=k[S],t=1;c>=10;c/=10)t++;a%=Y,f=a-Y+t,m=f<0?0:_/$t(10,t-f-1)%10|0}if(o=o||r<0||k[S+1]!==void 0||(f<0?_:_%$t(10,t-f-1)),v=i<4?(m||o)&&(i==0||i==(n.s<0?3:2)):m>5||m==5&&(i==4||o||i==6&&(a>0?f>0?_/$t(10,t-f):0:k[S-1])%10&1||i==(n.s<0?8:7)),r<1||!k[0])return k.length=0,v?(r-=n.e+1,k[0]=$t(10,(Y-r%Y)%Y),n.e=-r||0):k[0]=n.e=0,n;if(a==0?(k.length=S,c=1,S--):(k.length=S+1,c=$t(10,Y-a),k[S]=f>0?(_/$t(10,t-f)%$t(10,f)|0)*c:0),v)for(;;)if(S==0){for(a=1,f=k[0];f>=10;f/=10)a++;for(f=k[0]+=c,c=1;f>=10;f/=10)c++;a!=c&&(n.e++,k[0]==se&&(k[0]=1));break}else{if(k[S]+=c,k[S]!=se)break;k[S--]=0,c=1}for(a=k.length;k[--a]===0;)k.pop()}return tt&&(n.e>D.maxE?(n.d=null,n.e=NaN):n.e<D.minE&&(n.e=0,n.d=[0])),n}function ce(n,r,i){if(!n.isFinite())return Sr(n);var o,t=n.e,a=Vt(n.d),f=a.length;return r?(i&&(o=i-f)>0?a=a.charAt(0)+"."+a.slice(1)+me(o):f>1&&(a=a.charAt(0)+"."+a.slice(1)),a=a+(n.e<0?"e":"e+")+n.e):t<0?(a="0."+me(-t-1)+a,i&&(o=i-f)>0&&(a+=me(o))):t>=f?(a+=me(t+1-f),i&&(o=i-t-1)>0&&(a=a+"."+me(o))):((o=t+1)<f&&(a=a.slice(0,o)+"."+a.slice(o)),i&&(o=i-f)>0&&(t+1===f&&(a+="."),a+=me(o))),a}function ze(n,r){var i=n[0];for(r*=Y;i>=10;i/=10)r++;return r}function Ve(n,r,i){if(r>ln)throw tt=!0,i&&(n.precision=i),Error(_r);return K(new n(We),r,1,!0)}function ue(n,r,i){if(r>Ei)throw Error(_r);return K(new n(Ze),r,i,!0)}function br(n){var r=n.length-1,i=r*Y+1;if(r=n[r],r){for(;r%10==0;r/=10)i--;for(r=n[0];r>=10;r/=10)i++}return i}function me(n){for(var r="";n--;)r+="0";return r}function yr(n,r,i,o){var t,a=new n(1),f=Math.ceil(o/Y+4);for(tt=!1;;){if(i%2&&(a=a.times(r),pr(a.d,f)&&(t=!0)),i=zt(i/2),i===0){i=a.d.length-1,t&&a.d[i]===0&&++a.d[i];break}r=r.times(r),pr(r.d,f)}return tt=!0,a}function hr(n){return n.d[n.d.length-1]&1}function kr(n,r,i){for(var o,t,a=new n(r[0]),f=0;++f<r.length;){if(t=new n(r[f]),!t.s){a=t;break}o=a.cmp(t),(o===i||o===0&&a.s===i)&&(a=t)}return a}function Ni(n,r){var i,o,t,a,f,c,m,v=0,_=0,k=0,S=n.constructor,D=S.rounding,O=S.precision;if(!n.d||!n.d[0]||n.e>17)return new S(n.d?n.d[0]?n.s<0?0:1/0:1:n.s?n.s<0?0:n:NaN);for(r==null?(tt=!1,m=O):m=r,c=new S(.03125);n.e>-2;)n=n.times(c),k+=5;for(o=Math.log($t(2,k))/Math.LN10*2+5|0,m+=o,i=a=f=new S(1),S.precision=m;;){if(a=K(a.times(n),m,1),i=i.times(++_),c=f.plus(ut(a,i,m,1)),Vt(c.d).slice(0,m)===Vt(f.d).slice(0,m)){for(t=k;t--;)f=K(f.times(f),m,1);if(r==null)if(v<3&&Te(f.d,m-o,D,v))S.precision=m+=10,i=a=c=new S(1),_=0,v++;else return K(f,S.precision=O,D,tt=!0);else return S.precision=O,f}f=c}}function ge(n,r){var i,o,t,a,f,c,m,v,_,k,S,D=1,O=10,R=n,E=R.d,L=R.constructor,T=L.rounding,I=L.precision;if(R.s<0||!E||!E[0]||!R.e&&E[0]==1&&E.length==1)return new L(E&&!E[0]?-1/0:R.s!=1?NaN:E?0:R);if(r==null?(tt=!1,_=I):_=r,L.precision=_+=O,i=Vt(E),o=i.charAt(0),Math.abs(a=R.e)<15e14){for(;o<7&&o!=1||o==1&&i.charAt(1)>3;)R=R.times(n),i=Vt(R.d),o=i.charAt(0),D++;a=R.e,o>1?(R=new L("0."+i),a++):R=new L(o+"."+i.slice(1))}else return v=Ve(L,_+2,I).times(a+""),R=ge(new L(o+"."+i.slice(1)),_-O).plus(v),L.precision=I,r==null?K(R,I,T,tt=!0):R;for(k=R,m=f=R=ut(R.minus(1),R.plus(1),_,1),S=K(R.times(R),_,1),t=3;;){if(f=K(f.times(S),_,1),v=m.plus(ut(f,new L(t),_,1)),Vt(v.d).slice(0,_)===Vt(m.d).slice(0,_))if(m=m.times(2),a!==0&&(m=m.plus(Ve(L,_+2,I).times(a+""))),m=ut(m,new L(D),_,1),r==null)if(Te(m.d,_-O,T,c))L.precision=_+=O,v=f=R=ut(k.minus(1),k.plus(1),_,1),S=K(R.times(R),_,1),t=c=1;else return K(m,L.precision=I,T,tt=!0);else return L.precision=I,m;m=v,t+=2}}function Sr(n){return String(n.s*n.s/0)}function $e(n,r){var i,o,t;for((i=r.indexOf("."))>-1&&(r=r.replace(".","")),(o=r.search(/e/i))>0?(i<0&&(i=o),i+=+r.slice(o+1),r=r.substring(0,o)):i<0&&(i=r.length),o=0;r.charCodeAt(o)===48;o++);for(t=r.length;r.charCodeAt(t-1)===48;--t);if(r=r.slice(o,t),r){if(t-=o,n.e=i=i-o-1,n.d=[],o=(i+1)%Y,i<0&&(o+=Y),o<t){for(o&&n.d.push(+r.slice(0,o)),t-=Y;o<t;)n.d.push(+r.slice(o,o+=Y));r=r.slice(o),o=Y-r.length}else o-=t;for(;o--;)r+="0";n.d.push(+r),tt&&(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 cn(n,r){var i,o,t,a,f,c,m,v,_;if(r.indexOf("_")>-1){if(r=r.replace(/(\d)_(?=\d)/g,"$1"),Mr.test(r))return $e(n,r)}else if(r==="Infinity"||r==="NaN")return+r||(n.s=NaN),n.e=NaN,n.d=null,n;if(on.test(r))i=16,r=r.toLowerCase();else if(nn.test(r))i=2;else if(sn.test(r))i=8;else throw Error(_e+r);for(a=r.search(/p/i),a>0?(m=+r.slice(a+1),r=r.substring(2,a)):r=r.slice(2),a=r.indexOf("."),f=a>=0,o=n.constructor,f&&(r=r.replace(".",""),c=r.length,a=c-a,t=yr(o,new o(i),a,a*2)),v=je(r,i,se),_=v.length-1,a=_;v[a]===0;--a)v.pop();return a<0?new o(n.s*0):(n.e=ze(v,_),n.d=v,tt=!1,f&&(n=ut(n,t,c*4)),m&&(n=n.times(Math.abs(m)<54?$t(2,m):Oe.pow(2,m))),tt=!0,n)}function dn(n,r){var i,o=r.d.length;if(o<3)return r.isZero()?r:Ce(n,2,r,r);i=1.4*Math.sqrt(o),i=i>16?16:i|0,r=r.times(1/Ge(5,i)),r=Ce(n,2,r,r);for(var t,a=new n(5),f=new n(16),c=new n(20);i--;)t=r.times(r),r=r.times(a.plus(t.times(f.times(t).minus(c))));return r}function Ce(n,r,i,o,t){var a,f,c,m,v=1,_=n.precision,k=Math.ceil(_/Y);for(tt=!1,m=i.times(i),c=new n(o);;){if(f=ut(c.times(m),new n(r++*r++),_,1),c=t?o.plus(f):o.minus(f),o=ut(f.times(m),new n(r++*r++),_,1),f=c.plus(o),f.d[k]!==void 0){for(a=k;f.d[a]===c.d[a]&&a--;);if(a==-1)break}a=c,c=o,o=f,f=a,v++}return tt=!0,f.d.length=k+1,f}function Ge(n,r){for(var i=n;--r;)i*=n;return i}function Ar(n,r){var i,o=r.s<0,t=ue(n,n.precision,1),a=t.times(.5);if(r=r.abs(),r.lte(a))return fe=o?4:1,r;if(i=r.divToInt(t),i.isZero())fe=o?3:2;else{if(r=r.minus(i.times(t)),r.lte(a))return fe=hr(i)?o?2:3:o?4:1,r;fe=hr(i)?o?1:4:o?3:2}return r.minus(t).abs()}function Ii(n,r,i,o){var t,a,f,c,m,v,_,k,S,D=n.constructor,O=i!==void 0;if(O?(Xt(i,1,we),o===void 0?o=D.rounding:Xt(o,0,8)):(i=D.precision,o=D.rounding),!n.isFinite())_=Sr(n);else{for(_=ce(n),f=_.indexOf("."),O?(t=2,r==16?i=i*4-3:r==8&&(i=i*3-2)):t=r,f>=0&&(_=_.replace(".",""),S=new D(1),S.e=_.length-f,S.d=je(ce(S),10,t),S.e=S.d.length),k=je(_,10,t),a=m=k.length;k[--m]==0;)k.pop();if(!k[0])_=O?"0p+0":"0";else{if(f<0?a--:(n=new D(n),n.d=k,n.e=a,n=ut(n,S,i,o,0,t),k=n.d,a=n.e,v=gr),f=k[i],c=t/2,v=v||k[i+1]!==void 0,v=o<4?(f!==void 0||v)&&(o===0||o===(n.s<0?3:2)):f>c||f===c&&(o===4||v||o===6&&k[i-1]&1||o===(n.s<0?8:7)),k.length=i,v)for(;++k[--i]>t-1;)k[i]=0,i||(++a,k.unshift(1));for(m=k.length;!k[m-1];--m);for(f=0,_="";f<m;f++)_+=Ri.charAt(k[f]);if(O){if(m>1)if(r==16||r==8){for(f=r==16?4:3,--m;m%f;m++)_+="0";for(k=je(_,t,r),m=k.length;!k[m-1];--m);for(f=1,_="1.";f<m;f++)_+=Ri.charAt(k[f])}else _=_.charAt(0)+"."+_.slice(1);_=_+(a<0?"p":"p+")+a}else if(a<0){for(;++a;)_="0"+_;_="0."+_}else if(++a>m)for(a-=m;a--;)_+="0";else a<m&&(_=_.slice(0,a)+"."+_.slice(a))}_=(r==16?"0x":r==2?"0b":r==8?"0o":"")+_}return n.s<0?"-"+_:_}function pr(n,r){if(n.length>r)return n.length=r,!0}function fn(n){return new this(n).abs()}function hn(n){return new this(n).acos()}function pn(n){return new this(n).acosh()}function mn(n,r){return new this(n).plus(r)}function gn(n){return new this(n).asin()}function _n(n){return new this(n).asinh()}function wn(n){return new this(n).atan()}function vn(n){return new this(n).atanh()}function Mn(n,r){n=new this(n),r=new this(r);var i,o=this.precision,t=this.rounding,a=o+4;return!n.s||!r.s?i=new this(NaN):!n.d&&!r.d?(i=ue(this,a,1).times(r.s>0?.25:.75),i.s=n.s):!r.d||n.isZero()?(i=r.s<0?ue(this,o,t):new this(0),i.s=n.s):!n.d||r.isZero()?(i=ue(this,a,1).times(.5),i.s=n.s):r.s<0?(this.precision=a,this.rounding=1,i=this.atan(ut(n,r,a,1)),r=ue(this,a,1),this.precision=o,this.rounding=t,i=n.s<0?i.minus(r):i.plus(r)):i=this.atan(ut(n,r,a,1)),i}function bn(n){return new this(n).cbrt()}function yn(n){return K(n=new this(n),n.e+1,2)}function kn(n,r,i){return new this(n).clamp(r,i)}function Sn(n){if(!n||typeof n!="object")throw Error(Ke+"Object expected");var r,i,o,t=n.defaults===!0,a=["precision",1,we,"rounding",0,8,"toExpNeg",-Pe,0,"toExpPos",0,Pe,"maxE",0,Pe,"minE",-Pe,0,"modulo",0,9];for(r=0;r<a.length;r+=3)if(i=a[r],t&&(this[i]=Li[i]),(o=n[i])!==void 0)if(zt(o)===o&&o>=a[r+1]&&o<=a[r+2])this[i]=o;else throw Error(_e+i+": "+o);if(i="crypto",t&&(this[i]=Li[i]),(o=n[i])!==void 0)if(o===!0||o===!1||o===0||o===1)if(o)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[i]=!0;else throw Error(wr);else this[i]=!1;else throw Error(_e+i+": "+o);return this}function An(n){return new this(n).cos()}function Pn(n){return new this(n).cosh()}function Pr(n){var r,i,o;function t(a){var f,c,m,v=this;if(!(v instanceof t))return new t(a);if(v.constructor=t,mr(a)){v.s=a.s,tt?!a.d||a.e>t.maxE?(v.e=NaN,v.d=null):a.e<t.minE?(v.e=0,v.d=[0]):(v.e=a.e,v.d=a.d.slice()):(v.e=a.e,v.d=a.d?a.d.slice():a.d);return}if(m=typeof a,m==="number"){if(a===0){v.s=1/a<0?-1:1,v.e=0,v.d=[0];return}if(a<0?(a=-a,v.s=-1):v.s=1,a===~~a&&a<1e7){for(f=0,c=a;c>=10;c/=10)f++;tt?f>t.maxE?(v.e=NaN,v.d=null):f<t.minE?(v.e=0,v.d=[0]):(v.e=f,v.d=[a]):(v.e=f,v.d=[a]);return}if(a*0!==0){a||(v.s=NaN),v.e=NaN,v.d=null;return}return $e(v,a.toString())}if(m==="string")return(c=a.charCodeAt(0))===45?(a=a.slice(1),v.s=-1):(c===43&&(a=a.slice(1)),v.s=1),Mr.test(a)?$e(v,a):cn(v,a);if(m==="bigint")return a<0?(a=-a,v.s=-1):v.s=1,$e(v,a.toString());throw Error(_e+a)}if(t.prototype=F,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=Sn,t.clone=Pr,t.isDecimal=mr,t.abs=fn,t.acos=hn,t.acosh=pn,t.add=mn,t.asin=gn,t.asinh=_n,t.atan=wn,t.atanh=vn,t.atan2=Mn,t.cbrt=bn,t.ceil=yn,t.clamp=kn,t.cos=An,t.cosh=Pn,t.div=Cn,t.exp=xn,t.floor=Dn,t.hypot=Tn,t.ln=On,t.log=Rn,t.log10=En,t.log2=Ln,t.max=Nn,t.min=In,t.mod=Bn,t.mul=qn,t.pow=Fn,t.random=Un,t.round=jn,t.sign=$n,t.sin=Wn,t.sinh=Zn,t.sqrt=Vn,t.sub=Kn,t.sum=zn,t.tan=Gn,t.tanh=Hn,t.trunc=Xn,n===void 0&&(n={}),n&&n.defaults!==!0)for(o=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],r=0;r<o.length;)n.hasOwnProperty(i=o[r++])||(n[i]=this[i]);return t.config(n),t}function Cn(n,r){return new this(n).div(r)}function xn(n){return new this(n).exp()}function Dn(n){return K(n=new this(n),n.e+1,3)}function Tn(){var n,r,i=new this(0);for(tt=!1,n=0;n<arguments.length;)if(r=new this(arguments[n++]),r.d)i.d&&(i=i.plus(r.times(r)));else{if(r.s)return tt=!0,new this(1/0);i=r}return tt=!0,i.sqrt()}function mr(n){return n instanceof Oe||n&&n.toStringTag===vr||!1}function On(n){return new this(n).ln()}function Rn(n,r){return new this(n).log(r)}function Ln(n){return new this(n).log(2)}function En(n){return new this(n).log(10)}function Nn(){return kr(this,arguments,-1)}function In(){return kr(this,arguments,1)}function Bn(n,r){return new this(n).mod(r)}function qn(n,r){return new this(n).mul(r)}function Fn(n,r){return new this(n).pow(r)}function Un(n){var r,i,o,t,a=0,f=new this(1),c=[];if(n===void 0?n=this.precision:Xt(n,1,we),o=Math.ceil(n/Y),this.crypto)if(crypto.getRandomValues)for(r=crypto.getRandomValues(new Uint32Array(o));a<o;)t=r[a],t>=429e7?r[a]=crypto.getRandomValues(new Uint32Array(1))[0]:c[a++]=t%1e7;else if(crypto.randomBytes){for(r=crypto.randomBytes(o*=4);a<o;)t=r[a]+(r[a+1]<<8)+(r[a+2]<<16)+((r[a+3]&127)<<24),t>=214e7?crypto.randomBytes(4).copy(r,a):(c.push(t%1e7),a+=4);a=o/4}else throw Error(wr);else for(;a<o;)c[a++]=Math.random()*1e7|0;for(o=c[--a],n%=Y,o&&n&&(t=$t(10,Y-n),c[a]=(o/t|0)*t);c[a]===0;a--)c.pop();if(a<0)i=0,c=[0];else{for(i=-1;c[0]===0;i-=Y)c.shift();for(o=1,t=c[0];t>=10;t/=10)o++;o<Y&&(i-=Y-o)}return f.e=i,f.d=c,f}function jn(n){return K(n=new this(n),n.e+1,this.rounding)}function $n(n){return n=new this(n),n.d?n.d[0]?n.s:0*n.s:n.s||NaN}function Wn(n){return new this(n).sin()}function Zn(n){return new this(n).sinh()}function Vn(n){return new this(n).sqrt()}function Kn(n,r){return new this(n).sub(r)}function zn(){var n=0,r=arguments,i=new this(r[n]);for(tt=!1;i.s&&++n<r.length;)i=i.plus(r[n]);return tt=!0,K(i,this.precision,this.rounding)}function Gn(n){return new this(n).tan()}function Hn(n){return new this(n).tanh()}function Xn(n){return K(n=new this(n),n.e+1,1)}F[Symbol.for("nodejs.util.inspect.custom")]=F.toString;F[Symbol.toStringTag]="Decimal";var Oe=F.constructor=Pr(Li);We=new Oe(We);Ze=new Oe(Ze);var pt=Oe;import{compoundDebt as lo}from"@suilend/sdk";import{linearlyInterpolate as to,Side as be}from"@suilend/sdk";import{NORMALIZED_flSUI_COINTYPE as eo,NORMALIZED_jugSUI_COINTYPE as io,NORMALIZED_LBTC_COINTYPE as ro,formatLtvPercent as no}from"@suilend/sui-fe";import Yn from"crypto";var Xe=new Uint8Array(256),He=Xe.length;function Bi(){return He>Xe.length-16&&(Yn.randomFillSync(Xe),He=0),Xe.slice(He,He+=16)}var Kt=[];for(let n=0;n<256;++n)Kt.push((n+256).toString(16).slice(1));function Cr(n,r=0){return Kt[n[r+0]]+Kt[n[r+1]]+Kt[n[r+2]]+Kt[n[r+3]]+"-"+Kt[n[r+4]]+Kt[n[r+5]]+"-"+Kt[n[r+6]]+Kt[n[r+7]]+"-"+Kt[n[r+8]]+Kt[n[r+9]]+"-"+Kt[n[r+10]]+Kt[n[r+11]]+Kt[n[r+12]]+Kt[n[r+13]]+Kt[n[r+14]]+Kt[n[r+15]]}import Jn from"crypto";var qi={randomUUID:Jn.randomUUID};function Qn(n,r,i){if(qi.randomUUID&&!r&&!n)return qi.randomUUID();n=n||{};let o=n.random||(n.rng||Bi)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,r){i=i||0;for(let t=0;t<16;++t)r[i+t]=o[t];return r}return Cr(o)}var Ye=Qn;import{getPackagerConfigs as oo}from"@cetusprotocol/common-sdk";var Fi=10n;function xr(n){return n.depositedAmount.eq(0)?new nt(0):n.borrowedAmount.div(n.depositedAmount).times(100)}function Ui(n){let r=xr(n);if(!r.gt(100))return to(n.config.interestRate,"utilPercent","aprPercent",r)}function Dr(n){let r=xr(n),i=Ui(n);if(!(i===void 0||r.gt(100)))return new nt(r.div(100)).times(i.div(100)).times(1-n.config.spreadFeeBps/1e4).times(100)}function ms(n,r,i,o){let{api_url:t}=oo(o.sdkOptions.suilend);n===be.DEPOSIT&&([eo,io].includes(r.coinType)&&i.push({stats:{id:Ye(),isActive:!0,rewardIndex:-1,reserve:r,rewardCoinType:"LIQ_AG",mintDecimals:0,symbol:"LiqAg Points",iconUrl:`${t}/partners/LiqAg.png`,perDay:new nt(.036),side:be.DEPOSIT},obligationClaims:{}}),r.coinType===ro&&i.push({stats:{id:Ye(),isActive:!0,rewardIndex:-1,reserve:r,rewardCoinType:"LOMBARD",mintDecimals:0,symbol:"3x Lombard Lux",iconUrl:`${t}/partners/Lombard Lux.png`,perDay:new nt(0),side:be.DEPOSIT},obligationClaims:{}}))}function gs(n,r,i){return{...n,depositedAmount:r===be.DEPOSIT?nt.max(n.depositedAmount.plus(i),0):n.depositedAmount,borrowedAmount:r===be.BORROW?nt.max(n.borrowedAmount.plus(i),0):n.borrowedAmount}}function _s(n,r,i){let o=n===be.DEPOSIT?r.depositedAmount:r.borrowedAmount,t=n===be.DEPOSIT?i.depositedAmount:i.borrowedAmount,a=t.eq(0)?new nt(-1):o.div(t);return{multiplier:a,isValid:!a.eq(-1)}}function ws(n,r,i,o){let t=n.map(f=>({...f,stats:{...f.stats,perDay:o?f.stats.perDay.times(i):void 0}})),a=r.map(f=>({...f,stats:{...f.stats,aprPercent:o?f.stats.aprPercent.times(i):void 0}}));return{newPerDayRewards:t,newAprRewards:a}}function Tr(n,r){if(!Fi)throw new Error("oraclePriceMultiplierDecimal is required");if(n===0n)throw new Error("Invalid oracle price");if(r<0n){let i=10n**(Fi- -r);return n*i}return n/10n**(r+Fi)}function vs(n){return no(new nt(n))}function Or(n,r,i,o,t,a){let f=new nt(n),c=new nt(r),m=new nt(i),v=new nt(o),_=new nt(t),k=new nt(a),S=f.times(v),D=c.times(_),O=S.minus(D),R=m.times(v),E=O.plus(R),I=E.times(k).minus(E).minus(D);if(I.lt(0))throw new Error("Target leverage lower than current leverage");let B=I.div(_),W=I.div(v),U=f.plus(m).plus(W),Z=c.plus(B),g=U.times(v),b=Z.times(_),A=g.div(g.minus(b)),x=m.plus(W),C=R.plus(W.times(v));return{addBorrowAmount:B.toString(),addBorrowValue:I.toString(),addCollateralFromBorrow:W.toString(),addCollateralAmount:x.toString(),addCollateralValue:C.toString(),finalCollateralAmount:U.toString(),finalCollateralValue:g.toString(),finalDebtAmount:Z.toString(),finalDebtValue:b.toString(),leverage:A.toString()}}var ti=class{constructor(r){this.getPositionList=async(r=this._sdk.getSenderAddress(),i=!1)=>{let{lending_market_id:o,lending_market_type:t}=mt(this._sdk.sdkOptions.suilend);try{let a=`cap_to_position_mapping_${r}`,f=this._sdk.getCache(a,i),c=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`}]}});if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:42 ~ PositionModules ~ ownerRes.data.length:",c.data.length),c.data.length===0)return Zt("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:v,lstAprPercentMap:_}=await this._sdk.SuiLendModule.getLendingMarketData();console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:50 ~ PositionModules ~ allLendingMarketData:",v);let k=v[o].reserveMap;f||(f=new Map);let S=[];for(let E=0;E<c.data.length;E++){let L=Rr(c.data[E]);L.position_id&&S.push(L.position_id),L.id.id&&f.set(L.id.id,L.position_id)}await this._sdk.updateCache(a,f,so);let D=await this._sdk.FullClient.batchGetObjects(S,{showContent:!0});console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:73 ~ PositionModules ~ positionRes:",D);let O=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:v,lstAprPercentMap:_},!0);console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:75 ~ PositionModules ~ obligations:",O);let R=[];for(let E=0;E<D.length;E++){let L=S[E],T="";for(let[Z,g]of f.entries())if(g===L){T=Z;break}let I=Ai(D[E],T),B=await m.getObligation(I.obligation_owner_cap),W=Pi(I,B,k||{},_||{},O[o].rewardMap),U=this.getObligationRewardsInfo(B,Object.values(k));R.push({...W,claimable_rewards:U})}return R}catch(a){return Zt("FetchError",a),[]}};this.getPositionInfo=async(r,i=this._sdk.getSenderAddress(),o=!0)=>{let t=`cap_to_position_mapping_${i}`,a=this._sdk.getCache(t,o);a||(a=new Map);let f=this.getCapIdByPositionId(r,i)||"";if(!f){let B=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`}]}});for(let W=0;W<B.data.length;W++){let U=Rr(B.data[W]);U.id.id&&a.set(U.id.id,U.position_id),U.position_id===r&&(f=U.id.id)}}let c=await this._sdk.FullClient.batchGetObjects([r],{showContent:!0}),m=Ai(c[0],f||""),{lending_market_id:v,lending_market_type:_}=mt(this._sdk.sdkOptions.suilend),k=await this._sdk.SuiLendModule.getSuilendClient(v,_),{allLendingMarketData:S,lstAprPercentMap:D}=await this._sdk.SuiLendModule.getLendingMarketData(),O=S[v].reserveMap,R=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:S,lstAprPercentMap:D},!0),E=await k.getObligation(m.obligation_owner_cap),L=this.getObligationRewardsInfo(E,Object.values(O));return{...Pi(m,E,O||{},D||{},R[v].rewardMap),claimable_rewards:L}};this.getCapIdByPositionId=(r,i=this._sdk.getSenderAddress())=>{let o=`cap_to_position_mapping_${i}`,t=this._sdk.getCache(o,!1);if(!t)return"";for(let[a,f]of t.entries())if(f===r)return a;return""};this.repay=r=>{let{margin_trading:i}=this._sdk.sdkOptions,{lending_market_id:o,lending_market_type:t}=mt(this._sdk.sdkOptions.suilend),{global_config_id:a,versioned_id:f}=mt(this._sdk.sdkOptions.margin_trading),{txb:c,repay_amount:m,repay_coin_type:v,repay_coin:_,repay_reserve_array_index:k,market_id:S,position_cap_id:D}=r,O=c||new ie,R=_||Q.buildCoinWithBalance(BigInt(m.toString()),v,O);O.moveCall({target:`${i.published_at}::router::repay`,typeArguments:[t,v],arguments:[O.object(a),O.object(o),O.object(S),O.object(D),R,O.pure.u64(k.toString()),O.object(ye),O.object(f)]})};this.withdrawAsset=(r,i)=>{let{margin_trading:o}=this._sdk.sdkOptions,{lending_market_id:t,lending_market_type:a}=mt(this._sdk.sdkOptions.suilend),{global_config_id:f,versioned_id:c}=mt(this._sdk.sdkOptions.margin_trading),{package_id:m}=this._sdk.sdkOptions.suilend,{market_id:v,withdraw_amount:_,withdraw_reserve_array_index:k,withdraw_coin_type:S,position_cap_id:D}=r,[O]=i.moveCall({target:"0x1::option::none",typeArguments:[`${m}::lending_market::RateLimiterExemption<${a}, ${S}>`],arguments:[]});return i.moveCall({target:`${o.published_at}::router::withdraw`,typeArguments:[a,S],arguments:[i.object(f),i.object(t),i.object(v),i.object(D),i.object(O),i.pure.u64(_),i.pure.u64(k),i.object(Lr),i.object(ye),i.object(c)]})};this.borrowAsset=(r,i)=>{let{reserve_array_index:o,borrow_amount:t,base_token:a,quote_token:f,lending_market_id:c,is_long:m,market_id:v,position_cap_id:_,position_cap:k}=r,{margin_trading:S}=this._sdk.sdkOptions,{lending_market_type:D}=mt(this._sdk.sdkOptions.suilend),{global_config_id:O,versioned_id:R}=mt(this._sdk.sdkOptions.margin_trading);return i.moveCall({target:`${S.published_at}::router::borrow`,typeArguments:[D,m?f:a],arguments:[i.object(O),i.object(c),i.object(v),_?i.object(_):k,i.pure.u64(o),i.pure.u64(t.toString()),i.object(Lr),i.object(ye),i.object(R)]})};this.openPosition=async r=>{let{is_quote:i,is_long:o,amount:t,swap_clmm_pool:a="",slippage:f,leverage:c,market_id:m}=r,{lending_market_id:v}=mt(this._sdk.sdkOptions.suilend),_=new ie,{base_token:k,quote_token:S}=await this._sdk.MarketModules.getMarketInfo(m),{borrow_amount:D,base_reserve_array_index:O,quote_reserve_array_index:R,reserve:E,has_swap:L,flash_loan_objs:T}=await this.calculatePositionDeposit({is_long:o,is_quote:i,amount:t,swap_clmm_pool:a,leverage:c,slippage:f,market_id:m,base_token:k,quote_token:S,is_submit:!0});await this._sdk.SuiLendModule.refreshReservePrices(_,E);let I=this.createLeveragePosition({leverage:c,market_id:m,base_token:k,quote_token:S,is_long:o,init_deposit_amount:N(t).toString(),init_coin_type:i?S:k,tx:_}),B=o?O:R;if(T){let{is_flash_a:W,flash_amount:U,clmm_pool:Z,clmm_pool_coin_type_a:g,clmm_pool_coin_type_b:b,flash_loan_coin:A}=T,{balance_a:x,balance_b:C,receipt:p}=this._sdk.SwapModules.flashLoan({amount:U,clmm_pool:Z,clmm_pool_coin_type_a:g,clmm_pool_coin_type_b:b,flash_loan_coin:A,tx:_}),e=o?S:k,s=o?k:S,u=Q.buildCoinWithBalance(BigInt(t),i?S:k,_),d=Q.fromBalance(x,g,_),h=Q.fromBalance(C,b,_);L&&(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:515 ~ PositionModules ~ is_flash_a:",W),_.mergeCoins(u,[W?d:h]));let M=await this._sdk.SwapModules.handleSwap({from:e,to:s,amount:L?N(U).add(t).toString():U,input_coin:L?u:W?d:h,swap_clmm_pool:a,slippage:f,tx:_});M.swap_out_coin&&!L&&_.mergeCoins(M.swap_out_coin,[u]),await this.depositToLeveragePosition({is_long:o,market_id:m,position_cap:I,deposit_reserve_array_index:B,input_coin:M.swap_out_coin,base_token:k,quote_token:S},_);let P=this.borrowAsset({position_cap:I,reserve_array_index:o?R:O,borrow_amount:D,base_token:k,quote_token:S,is_long:o,lending_market_id:v,market_id:m},_),w=Q.mintBalanceZero(W?b:g,_);this._sdk.SwapModules.repayFlashLoan({tx:_,clmm_pool:Z,repay_base:W?Q.intoBalance(P,g,_):w,repay_quote:W?w:Q.intoBalance(P,b,_),receipt:p,clmm_pool_coin_type_a:g,clmm_pool_coin_type_b:b}),_.transferObjects([I],_.pure.address(this._sdk.getSenderAddress())),Q.destroyBalanceZero(W?Q.intoBalance(h,b,_):Q.intoBalance(d,g,_),W?b:g,_)}else{let W=Q.buildCoinWithBalance(BigInt(t),i?S:k,_),U=o?k:S,Z=o?S:k;L&&(W=(await this._sdk.SwapModules.handleSwap({from:Z,to:U,amount:t,input_coin:W,swap_clmm_pool:a,slippage:f,tx:_})).swap_out_coin),await this.depositToLeveragePosition({is_long:o,market_id:m,position_cap:I,deposit_reserve_array_index:B,input_coin:W,base_token:k,quote_token:S},_);let g=this.borrowAsset({position_cap:I,reserve_array_index:o?R:O,borrow_amount:D,base_token:k,quote_token:S,is_long:o,lending_market_id:v,market_id:m},_),b=await this._sdk.SwapModules.handleSwap({from:Z,to:U,amount:D,input_coin:g,swap_clmm_pool:a,slippage:f,tx:_});await this.depositToLeveragePosition({is_long:o,market_id:m,position_cap:I,deposit_reserve_array_index:B,input_coin:b.swap_out_coin,base_token:k,quote_token:S},_),_.transferObjects([I],_.pure.address(this._sdk.getSenderAddress()))}return _};this.positionDeposit=async r=>{let{position_id:i,swap_clmm_pool:o="",is_quote:t,amount:a,slippage:f,leverage:c,txb:m}=r,{lending_market_id:v}=mt(this._sdk.sdkOptions.suilend),{is_long:_,deposits:k,borrows:S,position_cap_id:D,market_id:O}=await this.getPositionInfo(i),{base_token:R,quote_token:E}=await this._sdk.MarketModules.getMarketInfo(O),L=m||new ie,T=k[0].reserveArrayIndex.toString(),I=S[0].reserveArrayIndex.toString(),{borrow_amount:B,flash_loan_objs:W,reserve:U,has_swap:Z}=await this.calculatePositionDeposit({market_id:O,is_long:_,is_quote:t,amount:a,swap_clmm_pool:o,leverage:c,slippage:f,base_token:R,quote_token:E,position_id:i,is_open:!1});if(await this._sdk.SuiLendModule.refreshReservePrices(L,U),W){let{is_flash_a:g,flash_amount:b,clmm_pool:A,clmm_pool_coin_type_a:x,clmm_pool_coin_type_b:C,flash_loan_coin:p}=W,{balance_a:e,balance_b:s,receipt:u}=this._sdk.SwapModules.flashLoan({amount:b,clmm_pool:A,clmm_pool_coin_type_a:x,clmm_pool_coin_type_b:C,flash_loan_coin:p,tx:L}),d=_?E:R,h=_?R:E,M=Q.buildCoinWithBalance(BigInt(a),t?E:R,L),P=Q.fromBalance(e,x,L),w=Q.fromBalance(s,C,L);Z&&L.mergeCoins(M,[g?P:w]);let l=await this._sdk.SwapModules.handleSwap({from:d,to:h,amount:Z?N(b).add(a).toString():b,input_coin:Z?M:g?P:w,swap_clmm_pool:o,slippage:f,tx:L});l.swap_out_coin&&!Z&&L.mergeCoins(l.swap_out_coin,[M]),await this.depositToLeveragePosition({is_long:_,market_id:O,position_cap_id:D,deposit_reserve_array_index:T,input_coin:l.swap_out_coin,base_token:R,quote_token:E},L);let y=await this.borrowAsset({position_cap_id:D,reserve_array_index:I,borrow_amount:B,base_token:R,quote_token:E,is_long:_,lending_market_id:v,market_id:O},L),$=Q.mintBalanceZero(g?C:x,L);this._sdk.SwapModules.repayFlashLoan({tx:L,clmm_pool:A,repay_base:g?Q.intoBalance(y,x,L):$,repay_quote:g?$:Q.intoBalance(y,C,L),receipt:u,clmm_pool_coin_type_a:x,clmm_pool_coin_type_b:C}),Q.destroyBalanceZero(g?Q.intoBalance(w,C,L):Q.intoBalance(P,x,L),g?C:x,L)}else{let g=Q.buildCoinWithBalance(BigInt(a),t?E:R,L),b=_?R:E,A=_?E:R;Z&&(g=(await this._sdk.SwapModules.handleSwap({from:A,to:b,amount:a,input_coin:g,swap_clmm_pool:o,slippage:f,tx:L})).swap_out_coin),await this.depositToLeveragePosition({is_long:_,market_id:O,position_cap_id:D,deposit_reserve_array_index:T,input_coin:g,base_token:R,quote_token:E},L);let x=this.borrowAsset({position_cap_id:D,reserve_array_index:I,borrow_amount:B,base_token:R,quote_token:E,is_long:_,lending_market_id:v,market_id:O},L),C=await this._sdk.SwapModules.handleSwap({from:A,to:b,amount:B,input_coin:x,swap_clmm_pool:o,slippage:f,tx:L});await this.depositToLeveragePosition({is_long:_,market_id:O,position_cap_id:D,deposit_reserve_array_index:T,input_coin:C.swap_out_coin,base_token:R,quote_token:E},L)}return L};this.positionWithdraw=async r=>{let{amount:i,is_quote:o,txb:t,swap_clmm_pool:a="",slippage:f,position_id:c,leverage:m,withdraw_max:v}=r,_=t||new ie,{withdraw_ctoken_amount:k,repay_amount:S,is_close:D,deposits:O,borrows:R,reserve:E,base_token:L,quote_token:T,clmm_pool:I,clmm_pool_coin_type_a:B,clmm_pool_coin_type_b:W,position_cap_id:U,market_id:Z,is_long:g,swap_convert_all:b,flash_loan_amount:A,routers:x,repay_flash_loan_amount:C="0",partial_amount_in:p,withdraw_amount:e,has_flash_loan:s,compoundDebtU64:u}=await this.calculatePositionWithdraw({position_id:c,is_quote:o,swap_clmm_pool:a,amount:i,leverage:m,slippage:f,withdraw_max:v});if(D)return this.positionClose({position_id:c,is_quote:o,leverage:m,slippage:f,swap_clmm_pool:a});await this._sdk.SuiLendModule.refreshReservePrices(_,E);let d,h;if(s){let{balance_a:l,balance_b:y,receipt:$,is_loan_a:q}=this._sdk.SwapModules.flashLoan({tx:_,amount:A,amount_u64:u,flash_loan_coin:R[0].reserve.coinType,clmm_pool:I,clmm_pool_coin_type_a:B,clmm_pool_coin_type_b:W});d=$,h=q,Q.destroyBalanceZero(h?y:l,h?W:B,_);let j=q?Q.fromBalance(l,B,_):Q.fromBalance(y,W,_);this.repay({txb:_,position_cap_id:U,repay_reserve_array_index:R[0].reserveArrayIndex.toString(),repay_coin:j,repay_coin_type:g?T:L,repay_amount:"0",market_id:Z}),_.transferObjects([j],this._sdk.getSenderAddress())}let M=this.withdrawAsset({market_id:Z,position_cap_id:U,withdraw_amount:k.toString(),withdraw_reserve_array_index:O[0].reserveArrayIndex.toString(),withdraw_coin_type:g?L:T},_);for(let l=1;l<O.length;l++){let y=this.withdrawAsset({market_id:Z,position_cap_id:U,withdraw_amount:Qe.toString(),withdraw_reserve_array_index:O[l].reserveArrayIndex.toString(),withdraw_coin_type:O[l].coinType},_);_.transferObjects([y],this._sdk.getSenderAddress())}let P;if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:778 ~ PositionModules ~ swap_convert_all:",{swap_convert_all:b,has_flash_loan:s}),b)P=await this._sdk.SwapModules.routerSwap({router:x?.route_obj,input_coin:M,slippage:f,txb:_});else{let l=_.splitCoins(M,[_.pure.u64(p?.toString()||"0")]);P=await this._sdk.SwapModules.routerSwap({router:x?.route_obj,input_coin:l,slippage:f,txb:_})}let w;if(P&&s&&(w=_.splitCoins(P,[_.pure.u64(C.toString())])),w&&s){let l=Q.mintBalanceZero(h?W:B,_);this._sdk.SwapModules.repayFlashLoan({tx:_,clmm_pool:I,repay_base:h?Q.intoBalance(w,B,_):l,repay_quote:h?l:Q.intoBalance(w,W,_),clmm_pool_coin_type_a:B,clmm_pool_coin_type_b:W,receipt:d})}else if(P){let l=_.splitCoins(P,[_.pure.u64(C.toString())]);this.repay({txb:_,position_cap_id:U,repay_reserve_array_index:R[0].reserveArrayIndex.toString(),repay_coin:l,repay_coin_type:g?T:L,repay_amount:"0",market_id:Z}),_.transferObjects([l],this._sdk.getSenderAddress())}return P&&_.transferObjects([P],this._sdk.getSenderAddress()),b||_.transferObjects([M],this._sdk.getSenderAddress()),_};this.positionClose=async r=>{let{position_id:i,is_quote:o,slippage:t,leverage:a,swap_clmm_pool:f=""}=r,{margin_trading:c}=this._sdk.sdkOptions,{global_config_id:m,versioned_id:v}=mt(this._sdk.sdkOptions.margin_trading),{lending_market_type:_,lending_market_id:k}=mt(this._sdk.sdkOptions.suilend),S=new ie,{deposits:D,borrows:O,base_token:R,quote_token:E,clmm_pool:L,clmm_pool_coin_type_a:T,clmm_pool_coin_type_b:I,position_cap_id:B,market_id:W,swap_convert_all:U,routers:Z,partial_amount_in:g,is_long:b,flash_loan_amount:A,reserve:x,repay_flash_loan_amount:C="0",claimable_rewards:p,compoundDebtU64:e}=await this.calculatePositionWithdraw({tx:S,position_id:i,is_quote:o,swap_clmm_pool:f,amount:Qe.toString(),leverage:a,slippage:t,withdraw_max:!0});await this._sdk.SuiLendModule.refreshReservePrices(S,x);let s,u;if(N(A).gt(0)){let{balance_a:M,balance_b:P,receipt:w,is_loan_a:l}=this._sdk.SwapModules.flashLoan({tx:S,amount:A,amount_u64:e,flash_loan_coin:b?E:R,clmm_pool:L,clmm_pool_coin_type_a:T,clmm_pool_coin_type_b:I});s=w,u=l,Q.destroyBalanceZero(u?P:M,u?I:T,S);let y=u?Q.fromBalance(M,T,S):Q.fromBalance(P,I,S);this.repay({txb:S,position_cap_id:B,repay_reserve_array_index:O[0].reserveArrayIndex.toString(),repay_coin:y,repay_coin_type:O[0].reserve.coinType,repay_amount:"0",market_id:W}),S.transferObjects([y],this._sdk.getSenderAddress())}let d=this.withdrawAsset({market_id:W,position_cap_id:B,withdraw_amount:Qe.toString(),withdraw_reserve_array_index:D[0].reserveArrayIndex.toString(),withdraw_coin_type:b?R:E},S);await this.buildClaimRewardsMoveCall(p,W,B,S);for(let M=1;M<D.length;M++){let P=this.withdrawAsset({market_id:W,position_cap_id:B,withdraw_amount:Qe.toString(),withdraw_reserve_array_index:D[M].reserveArrayIndex.toString(),withdraw_coin_type:D[M].coinType},S);S.transferObjects([P],this._sdk.getSenderAddress())}let h;if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:895 ~ PositionModules ~ swap_convert_all:",U),U)h=await this._sdk.SwapModules.routerSwap({router:Z?.route_obj,input_coin:d,slippage:t,txb:S});else if(g&&N(g).gt(0)){let M=S.splitCoins(d,[S.pure.u64(g?.toString()||"0")]);h=await this._sdk.SwapModules.routerSwap({router:Z?.route_obj,input_coin:M,slippage:t,txb:S})}if(h&&s){let M=Q.mintBalanceZero(u?I:T,S),P=S.splitCoins(h,[S.pure.u64(C?.toString()||"0")]);this._sdk.SwapModules.repayFlashLoan({tx:S,clmm_pool:L,repay_base:u?Q.intoBalance(P,T,S):M,repay_quote:u?M:Q.intoBalance(P,I,S),clmm_pool_coin_type_a:T,clmm_pool_coin_type_b:I,receipt:s})}return S.moveCall({target:`${c.published_at}::router::close_position`,typeArguments:[_],arguments:[S.object(m),S.object(k),S.object(W),S.object(B),S.object(ye),S.object(v)]}),h&&S.transferObjects([h],this._sdk.getSenderAddress()),U||S.transferObjects([d],this._sdk.getSenderAddress()),console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:951 ~ PositionModules ~ tx:",S),S};this.positionLeverageIncrease=async r=>{let{current_leverage:i,target_leverage:o,swap_clmm_pool:t="",slippage:a,position_id:f}=r,{lending_market_id:c}=mt(this._sdk.sdkOptions.suilend),m=new ie,{flash_loan_amount:v,quote_token:_,base_token:k,reserve:S,deposits:D,borrow_amount:O,routers:R,position_cap_id:E,is_long:L,market_id:T,is_flash_loan:I}=await this.calculatePositionLeverage({position_id:f,current_leverage:i,target_leverage:o,swap_clmm_pool:t,slippage:a}),B=L?_:k;if(await this._sdk.SuiLendModule.refreshReservePrices(m,S),I){let{clmm_pool_coin_type_a:W,clmm_pool_coin_type_b:U,clmm_pool:Z}=await this._sdk.SwapModules.getFlashLoanPool(B,v?.toString()||""),{balance_a:g,balance_b:b,receipt:A,is_loan_a:x}=this._sdk.SwapModules.flashLoan({tx:m,amount:v?.toString()||"0",clmm_pool:Z,clmm_pool_coin_type_a:W,clmm_pool_coin_type_b:U,flash_loan_coin:B}),C=await this._sdk.SwapModules.routerSwap({router:R?.route_obj,input_coin:x?Q.fromBalance(g,W,m):Q.fromBalance(b,U,m),slippage:a,txb:m});this.depositToLeveragePosition({is_long:L,market_id:T,position_cap_id:E,deposit_reserve_array_index:D[0].reserveArrayIndex.toString(),input_coin:C,base_token:k,quote_token:_},m);let p=this.borrowAsset({position_cap_id:E,reserve_array_index:L?S[1].arrayIndex.toString():S[0].arrayIndex.toString(),borrow_amount:O,base_token:k,quote_token:_,is_long:L,lending_market_id:c,market_id:T},m),e=Q.mintBalanceZero(x?U:W,m);this._sdk.SwapModules.repayFlashLoan({tx:m,clmm_pool:Z,repay_base:x?Q.intoBalance(p,W,m):e,repay_quote:x?e:Q.intoBalance(p,U,m),receipt:A,clmm_pool_coin_type_a:W,clmm_pool_coin_type_b:U}),Q.destroyBalanceZero(x?b:g,x?U:W,m)}else{let W=this.borrowAsset({position_cap_id:E,reserve_array_index:L?S[1].arrayIndex.toString():S[0].arrayIndex.toString(),borrow_amount:O,base_token:k,quote_token:_,is_long:L,lending_market_id:c,market_id:T},m),U=await this._sdk.SwapModules.routerSwap({router:R?.route_obj,input_coin:W,slippage:a,txb:m});this.depositToLeveragePosition({is_long:L,market_id:T,position_cap_id:E,deposit_reserve_array_index:D[0].reserveArrayIndex.toString(),input_coin:U,base_token:k,quote_token:_},m)}return m};this.positionLeverageDecrease=async r=>{let{current_leverage:i,target_leverage:o,swap_clmm_pool:t="",slippage:a,position_id:f}=r,{deposits:c,borrows:m,position_cap_id:v,market_id:_,is_long:k}=await this.getPositionInfo(f),{base_token:S,quote_token:D}=await this._sdk.MarketModules.getMarketInfo(_),O=new ie,{withdraw_ctoken_amount:R,routers:E,reserve:L,is_flash_loan:T}=await this.calculatePositionLeverage({position_id:f,current_leverage:i,target_leverage:o,swap_clmm_pool:t,slippage:a});if(await this._sdk.SuiLendModule.refreshReservePrices(O,L),T){let{clmm_pool_coin_type_a:I,clmm_pool_coin_type_b:B,clmm_pool:W,clmm_fee_tier:U}=await this._sdk.SwapModules.getFlashLoanPool(c[0].reserve.coinType,R.toString()),{balance_a:Z,balance_b:g,receipt:b,is_loan_a:A}=this._sdk.SwapModules.flashLoan({tx:O,amount:R,clmm_pool:W,clmm_pool_coin_type_a:I,clmm_pool_coin_type_b:B,flash_loan_coin:c[0].reserve.coinType}),x=await this._sdk.SwapModules.routerSwap({router:E?.route_obj,input_coin:A?Q.fromBalance(Z,I,O):Q.fromBalance(g,B,O),slippage:a,txb:O});this.repay({txb:O,market_id:_,position_cap_id:v,repay_reserve_array_index:m[0].reserveArrayIndex.toString(),repay_coin:x,repay_coin_type:k?D:S,repay_amount:"0"}),x&&O.transferObjects([x],this._sdk.getSenderAddress());let C=N(R).mul(U).toString(),p=N(R).add(C).toDP(0,pt.ROUND_UP),e=this.withdrawAsset({market_id:_,position_cap_id:v,withdraw_amount:p.toString(),withdraw_reserve_array_index:c[0].reserveArrayIndex.toString(),withdraw_coin_type:k?S:D},O),s=O.splitCoins(e,[p.toString()]);this._sdk.SwapModules.repayFlashLoan({tx:O,clmm_pool:W,repay_base:A?Q.intoBalance(s,I,O):Z,repay_quote:A?g:Q.intoBalance(s,B,O),receipt:b,clmm_pool_coin_type_a:I,clmm_pool_coin_type_b:B}),O.transferObjects([e],this._sdk.getSenderAddress())}else{let I=this.withdrawAsset({market_id:_,position_cap_id:v,withdraw_amount:R.toString(),withdraw_reserve_array_index:c[0].reserveArrayIndex.toString(),withdraw_coin_type:k?S:D},O),B=await this._sdk.SwapModules.routerSwap({router:E?.route_obj,input_coin:I,slippage:a,txb:O});this.repay({txb:O,market_id:_,position_cap_id:v,repay_reserve_array_index:m[0].reserveArrayIndex.toString(),repay_coin:B,repay_coin_type:k?D:S,repay_amount:"0"}),O.transferObjects([B],this._sdk.getSenderAddress())}return O};this.positionRepay=async r=>{let{position_id:i,amount:o,is_quote:t,slippage:a}=r,{routers:f,repay_coin_type:c,base_token:m,quote_token:v,borrows:_,deposits:k,market_id:S,position_cap_id:D}=await this.calculatePositionRepay({position_id:i,amount:o,is_quote:t}),O=this.extractOtherTokenTypes(k,_,m,v),{reserve:R}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(m,v,void 0,O),E=new ie;await this._sdk.SuiLendModule.refreshReservePrices(E,R);let L=t?v!==c:m!==c,T;if(L){let I=Q.buildCoinWithBalance(BigInt(o.toString()),t?v:m,E);T=await this._sdk.SwapModules.routerSwap({router:f?.route_obj,input_coin:I,slippage:a,txb:E})}else T=Q.buildCoinWithBalance(BigInt(o.toString()),t?v:m,E);return this.repay({txb:E,position_cap_id:D,repay_reserve_array_index:_[0].reserveArrayIndex.toString(),repay_coin:T,repay_coin_type:c,repay_amount:"0",market_id:S}),E.transferObjects([T],this._sdk.getSenderAddress()),E};this.positionTopUpCToken=async r=>{let{position_id:i,amount:o,is_quote:t,swap_clmm_pool:a="",slippage:f}=r,c=new ie,{is_long:m,market_id:v,position_cap_id:_,deposits:k,borrows:S}=await this.getPositionInfo(i),{base_token:D,quote_token:O}=await this._sdk.MarketModules.getMarketInfo(v),R=m&&t||!m&&!t,E=this.extractOtherTokenTypes(k,S,D,O),{base_reserve_array_index:L,quote_reserve_array_index:T,reserve:I}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(D,O,void 0,E);await this._sdk.SuiLendModule.refreshReservePrices(c,I);let B;if(R){let W=await this._sdk.SwapModules.findRouters(t?O:D,t?D:O,o,!0,[a]),U=Q.buildCoinWithBalance(BigInt(o.toString()),t?O:D,c);B=await this._sdk.SwapModules.routerSwap({router:W?.route_obj,input_coin:U,slippage:f,txb:c})}else B=Q.buildCoinWithBalance(BigInt(o.toString()),t?O:D,c);return await this.depositToLeveragePosition({is_long:m,market_id:v,position_cap_id:_,deposit_reserve_array_index:m?L:T,input_coin:B,base_token:D,quote_token:O},c),c};this.positionWithdrawCToken=async r=>{let{position_id:i,amount:o,is_quote:t,swap_clmm_pool:a="",slippage:f}=r,c=new ie,{is_long:m,market_id:v,position_cap_id:_,deposits:k,borrows:S}=await this.getPositionInfo(i),{base_token:D,quote_token:O}=await this._sdk.MarketModules.getMarketInfo(v),R=m&&t||!m&&!t,E=this.extractOtherTokenTypes(k,S,D,O),{base_reserve_array_index:L,quote_reserve_array_index:T,reserve:I}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(D,O,void 0,E);await this._sdk.SuiLendModule.refreshReservePrices(c,I);let B=await this._sdk.SuiLendModule.getLatestPriceFeeds(I,!0),W=B[xe(O)]?.price,U=B[xe(D)]?.price,Z=m?N(W).div(N(U)).toString():N(U).div(N(W)).toString(),g=I[0].mintDecimals,b=I[1].mintDecimals,A=R?N(o).mul(Z).div(10**(t?b:g)).mul(10**(m?g:b)).toDP(0,pt.ROUND_DOWN).toString():o.toString(),x=this.withdrawAsset({market_id:v,position_cap_id:_,withdraw_amount:A,withdraw_reserve_array_index:m?L:T,withdraw_coin_type:m?D:O},c);if(R){let C=await this._sdk.SwapModules.findRouters(m?D:O,t?O:D,A,!0,[a]),p=await this._sdk.SwapModules.routerSwap({router:C?.route_obj,input_coin:x,slippage:f,txb:c});p&&c.transferObjects([p],this._sdk.getSenderAddress())}else c.transferObjects([x],this._sdk.getSenderAddress());return c};this.positionClaim=async r=>{let i=new ie,{position_cap_id:o,obligation_owner_cap:t,market_id:a,claimable_rewards:f}=await this.getPositionInfo(r);return console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:1650 ~ PositionModules ~ claimable_rewards:",f),await this.buildClaimRewardsMoveCall(f,a,o,i),i};this.calculatePositionDeposit=async r=>{let{is_quote:i,is_long:o,amount:t,swap_clmm_pool:a="",leverage:f,by_amount_in:c=!0,base_token:m,quote_token:v,is_submit:_=!1,is_open:k=!0,position_id:S,market_id:D}=r,O=[],R=[],E=[],L=this._sdk.getLeverageThresholdForMarket(D),T;if(!k&&S){let{deposits:V,borrows:z}=await this.getPositionInfo(S);if(R=V,E=z,O=this.extractOtherTokenTypes(R,E,m,v),E.length>0&&R.length>0){let G=E[0].borrowedAmountUsd.toString(),rt=R[0].depositedAmountUsd.toString();rt&&G&&N(G).gt(0)&&(T=N(rt).div(N(rt).sub(G)).toString())}}let{reserve:I,base_reserve_array_index:B,quote_reserve_array_index:W}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(m,v,void 0,O),U=I[0].mintDecimals,Z=I[1].mintDecimals,g=t.toString(),b=o&&i||!o&&!i,A=await this._sdk.SuiLendModule.getLatestPriceFeedsByCoinTypes([m,v]),x=A&&A[v]?.price,C=A&&A[m]?.price;if(!x||!C)throw Zt("PriceNotFound","Price not found");if(b&&_){let V=o?N(x).div(N(C)).toString():N(C).div(N(x)).toString();g=N(t).mul(V).div(10**(i?Z:U)).mul(10**(o?U:Z)).toString(),console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:492 ~ PositionModules ~ initDepositAmount:",{initDepositAmount:g,rate:V})}else if(b&&!_){let V=o?v:m,z=o?m:v;g=(await this._sdk.SwapModules.findRouters(V,z,g,!0,[a]))?.amount_out.toString()}else g=t;let p=!1;L?p=o?f>L.long_threshold:f>L.short_threshold:p=!0,T&&N(T).gt(f)&&(p=!0);let e,s,u,d,h,M,P,w;if(p){let{base_price:V,quote_price:z,flash_amount:G,flash_loan_fee:rt,flash_loan_coin:X,is_flash_a:it,clmm_pool:st,clmm_fee_tier:J,clmm_pool_coin_type_a:H,clmm_pool_coin_type_b:ne}=await this._sdk.SwapModules.calculateFlashLoan({is_long:o,leverage:f,base_token:m,quote_token:v,deposit_amount:g,reserve:I,base_token_decimal:U,quote_token_decimal:Z}),at=await this._sdk.SwapModules.findRouters(o?v:m,o?m:v,G.toString(),!0,[a]);e=N(g).add(N(at?.amount_out.toString())).toDP(0,pt.ROUND_DOWN).toString(),s=N(e).mul(o?V:z).toString(),u=N(G).add(rt).toDP(0,pt.ROUND_UP).toString(),d=N(u).mul(o?z:V).toString(),w=at,h={clmm_fee_tier:J,flash_loan_fee:rt,flash_loan_coin:X,flash_amount:G,is_flash_a:it,clmm_pool_coin_type_a:H,clmm_pool_coin_type_b:ne,clmm_pool:st}}else{let V=o?U:Z,z=o?Z:U,G=R.length>0?R[0].depositedAmount.toString():"0",rt=E.length>0?E[0].borrowedAmount.toString():"0",X=o?C:x,it=o?x:C,st=N(g).div(10**V).toFixed(V),{addBorrowAmount:J,addBorrowValue:H,addCollateralFromBorrow:ne,finalCollateralAmount:at,finalCollateralValue:ct,finalDebtAmount:ke,finalDebtValue:ft,addCollateralAmount:dt,addCollateralValue:Jt}=Or(G,rt,st,X,it,f.toString());e=N(dt).mul(10**V).toFixed(0,pt.ROUND_DOWN).toString(),s=N(Jt).mul(10**V).toString(),u=N(J).mul(10**z).toFixed(0,pt.ROUND_UP).toString(),d=N(H).mul(10**z).toString(),w=await this._sdk.SwapModules.findRouters(o?v:m,o?m:v,e,!0,[])}let l=i?N(t).div(10**(i?Z:U)).mul(10**U).mul(f).div(C).toDP(0,pt.ROUND_DOWN).toString():N(t).mul(f).toDP(0,pt.ROUND_DOWN).toString(),y="0",$="0",q="0",j="0";return!k&&S&&(E&&E.length>0&&(y=N(E[0].borrowedAmount.toString()).mul(o?10**Z:10**U).add(N(u)).toString(),$=N(y).mul(o?x:C).toString()),q=N(R[0].depositedAmount.toString()).mul(o?10**U:10**Z).add(N(e)).toString(),j=N(q).mul(o?C:x).toString()),{routers:w,flash_loan_objs:h,quote_price:M,leverageDepositAmount:l,base_price:P,deposit_amount:e,borrow_amount:u,deposit_amount_usd:s,borrow_amount_usd:d,after_borrow_amount:y,after_borrow_amount_usd:$,after_deposit_amount:q,after_deposit_amount_usd:j,init_deposit_amount:g,base_reserve_array_index:B,quote_reserve_array_index:W,reserve:I,has_swap:b}};this.calculateCompoundDebt=async(r,i)=>{let{market_id:o,position_cap_id:t,borrow_reserve_array_index:a,borrow_index:f}=r,{margin_trading:c}=this._sdk.sdkOptions,{lending_market_type:m,lending_market_id:v}=mt(this._sdk.sdkOptions.suilend),_=new ie;_.moveCall({target:`${c.published_at}::router::compound_debt`,typeArguments:[m],arguments:[_.object(v),_.object(o),_.object(t),_.pure.u64(a),_.pure.u64(f)]});let k=i.moveCall({target:`${c.published_at}::router::compound_debt`,typeArguments:[m],arguments:[i.object(v),i.object(o),i.object(t),i.pure.u64(a),i.pure.u64(f)]});try{let S=await this._sdk.FullClient.devInspectTransactionBlock({transactionBlock:_,sender:this._sdk.getSenderAddress()});S.error!=null&&Zt("FetchError",new Error(S.error),{[Je.METHOD_NAME]:"calculateCompoundDebt",[Je.REQUEST_PARAMS]:r}),(!S.results||S.results.length===0||!S.results[0].returnValues||S.results[0].returnValues.length===0)&&Zt("FetchError",new Error("No return values from compound_debt"),{[Je.METHOD_NAME]:"calculateCompoundDebt",[Je.REQUEST_PARAMS]:r});let D=ao.u64().parse(Uint8Array.from(S?.results?.[0]?.returnValues?.[0]?.[0]||[]));return{compoundDebtU64:k,compoundDebtAmount:D.toString()}}catch{return}};this.calculatePositionWithdraw=async r=>{let{position_id:i,is_quote:o,swap_clmm_pool:t="",amount:a,leverage:f,slippage:c,withdraw_max:m,tx:v}=r,{deposits:_,borrows:k,origin_obligation:S,position_cap_id:D,market_id:O,is_long:R,claimable_rewards:E}=await this.getPositionInfo(i),{base_token:L,quote_token:T}=await this._sdk.MarketModules.getMarketInfo(O),I=i?this.extractOtherTokenTypes(_,k,L,T):[],{reserve:B,base_reserve_map_info:W,quote_reserve_map_info:U}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(L,T,void 0,I),Z=B[0].mintDecimals,g=B[1].mintDecimals,b=R?W.cTokenExchangeRate:U.cTokenExchangeRate,A=await this._sdk.SuiLendModule.getLatestPriceFeeds(B,!0),x=A[xe(T)]?.price,C=A[xe(L)]?.price,p=N(0),e;R?(p=N(o?N(a).mul(x).toString():N(a).mul(C).toString()).div(o?10**g:10**Z),e=o?N(a).div(C).div(10**g).mul(10**Z).toString():N(a)):(p=N(o?N(a).mul(x).toString():N(a).mul(C).toString()).div(o?10**g:10**Z),e=o?N(a):N(a).mul(C).div(10**Z).mul(10**g).toString());let s=N(_[0].depositedAmountUsd.toString()).sub(N(k&&k.length>0?k[0].borrowedAmountUsd.toString():"0")).toString(),u=p.div(N(s)),d=m||N(u).gte(1),h,M=new pt(0);S&&S.borrows&&S.borrows.length>0&&(h=lo(S.borrows[0],R?B[1]:B[0]),M=N(h.borrowedAmount.value.toString()).div(10**18).toDP(0,pt.ROUND_UP));let P,w=M.mul(d?1.001:u).toDP(0,pt.ROUND_UP).toString(),l=w.toString(),y,$,q,j,V,z;if(N(w).gt(0)){let{clmm_pool_coin_type_a:Jt,clmm_pool_coin_type_b:ot,clmm_fee_tier:lt,clmm_pool:Re}=await this._sdk.SwapModules.getFlashLoanPool(R?T:L,l);V=Jt||"",z=ot||"",q=lt||0,j=Re||"",y=N(l).mul(lt).toString(),$=N(l).mul(N(1).add(lt)).toDP(0,pt.ROUND_UP).toString()}let G=R&&o||!R&&!o,rt,X;if(G){let Jt=N(_[0].depositedAmount.toString()).mul(10**(R?Z:g)).mul(d?1:u).toDP(0,pt.ROUND_DOWN).toString();rt=await this._sdk.SwapModules.findRouters(_[0].reserve.coinType,R?T:L,Jt,!0,[t])}else{let ot=L==="0x0041f9f9344cac094454cd574e333c4fdb132d7bcc9379bcd4aab485b2a63942::wbtc::WBTC"||T==="0x0041f9f9344cac094454cd574e333c4fdb132d7bcc9379bcd4aab485b2a63942::wbtc::WBTC"?.01:.0025;X=N(w).mul(R?x:C).div(10**(R?g:Z)).mul(10**(R?Z:g)).div(R?C:x).mul(N(1).add(ot)).toDP(0,pt.ROUND_DOWN).toString(),rt=await this._sdk.SwapModules.findRouters(_[0].reserve.coinType,R?T:L,X.toString(),!0,[t]),d||(w=N(rt?.amount_out.toString()).mul(N(1).sub(c)).toDP(0,pt.ROUND_DOWN).toString())}e=N(e).mul(f).toDP(0,pt.ROUND_DOWN).toString(),p=N(p).mul(f);let it=M.mul(N(1).sub(u)).toString(),st=N(it).mul(R?x:C).toString(),J=N(_[0].depositedAmount.toString()).mul(R?10**Z:10**g).sub(N(e)).toString(),H=N(J).mul(R?C:x).toString(),ne=N(rt?.amount_in.toString()).toString(),at=N(rt?.amount_out.toString()).toString(),ct=N(e).div(b.toString()).toDP(0,pt.ROUND_DOWN).toString(),ke=N(st).div(N(10**(R?g:Z))),ft=N(H).div(N(10**(R?Z:g))),dt=!0;if(k.length>0){let Jt=N(k[0].borrowedAmountUsd.toString()),ot=Jt.div(ft).toString(),lt=N(_[0].reserve.config.openLtvPct).mul(N(.95)).div(100).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ positionModules.ts:1459 ~ PositionModules ~ afterLTV:",{afterLTV:ot,ltv:lt,afterDepositAmount:J,afterDepositAmountUSD:ft,afterBorrowAmount:it,afterBorrowAmountUSD:ke,withdrawAmount:e,currentBorrowAmountUSD:Jt}),N(ot).lte(lt)&&(dt=!1)}return{compoundDebtU64:P,amount_in:ne,amount_out:at,from:_[0].reserve.coinType,to:R?L:T,after_borrow_amount:it,after_borrow_amount_usd:st,after_deposit_amount:J,after_deposit_amount_usd:H,flash_loan_fee:y,clmm_fee_tier:q,withdraw_amount:e,withdraw_ctoken_amount:ct,repay_amount:w,ratio:u,is_close:d,is_long:R,deposits:_,borrows:k,reserve:B,base_token:L,quote_token:T,clmm_pool:j,clmm_pool_coin_type_a:V,clmm_pool_coin_type_b:z,position_cap_id:D,market_id:O,swap_convert_all:G,flash_loan_amount:l,routers:rt,repay_flash_loan_amount:$,partial_amount_in:X,has_flash_loan:dt,claimable_rewards:E}};this.calculatePositionLeverage=async r=>{let{position_id:i,current_leverage:o,target_leverage:t,swap_clmm_pool:a=""}=r,f=N(t).sub(N(o)),c=f.gt(0),{borrows:m,deposits:v,is_long:_,position_cap_id:k,market_id:S}=await this.getPositionInfo(i),{base_token:D,quote_token:O}=await this._sdk.MarketModules.getMarketInfo(S),R=this.extractOtherTokenTypes(v,m,D,O),{reserve:E}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(D,O,void 0,R),L=E[0].mintDecimals,T=E[1].mintDecimals,I=await this._sdk.SuiLendModule.getLatestPriceFeeds(E,!0),B=I[xe(O)]?.price,W=I[xe(D)]?.price,U=m.length>0,Z=v[0].depositedAmountUsd.toString(),g=v[0].depositedAmount.toString(),b=U?N(m[0].borrowedAmount.toString()).mul(10**(_?T:L)):N(0),A=U?m[0].borrowedAmountUsd.toString():N(0),x=U?N(m[0].reserve.config.borrowWeightBps.toString()).div(1e4).toString():"1",C=N(v[0].reserve.config.openLtvPct).mul(N(.95)).div(100).div(x).toString(),p=N(Z).sub(N(A)).mul(C).toString();if(c){let e=N(Z).div(o).mul(f),s=e.add(A).div(Z).toString(),u=e.div(_?B:W).mul(10**(_?T:L)).toDP(0,pt.ROUND_UP),d=N(s).gt(C),h=u,M=_?O:D,P=_?D:O,w=await this._sdk.SwapModules.findRouters(M,P,h.toString(),!0,[a]),l=N(w?.amount_out.toString()).toString(),y=N(l).mul(_?W:B).toString(),$,q,j,V,z,G="0",rt="0";if(d){let{clmm_pool_coin_type_a:H,clmm_pool_coin_type_b:ne,clmm_pool:at,clmm_fee_tier:ct}=await this._sdk.SwapModules.getFlashLoanPool(M,h.toString());j=ct,V=H,z=ne,$=h.mul(j).toString(),q=_?V==O:z==D,G=N(h).add($).toDP(0,pt.ROUND_UP).toString(),rt=N(G).mul(_?B:W).toString()}else G=u.toString(),rt=e.toString();let X=N(g).add(N(l).div(10**(_?L:T))).toString(),it=N(X).mul(_?W:B).toString(),st=N(b).add(N(G)).div(10**(_?T:L)).toString(),J=N(st).mul(_?B:W).toString();return{amount_in:w?.amount_in.toString(),amount_out:w?.amount_out.toString(),flash_loan_fee:$,deposit_amount:l,borrow_amount:G,deposit_amount_usd:y,borrow_amount_usd:rt,flash_loan_amount:h,base_token:D,quote_token:O,reserve:E,deposits:v,borrows:m,clmm_fee_tier:j,is_flash_loan:d,is_loan_a:q,clmm_pool_coin_type_a:V,clmm_pool_coin_type_b:z,after_deposit_amount:X,after_borrow_amount:st,after_deposit_amount_usd:it,after_borrow_amount_usd:J,position_cap_id:k,market_id:S,is_long:_,routers:w}}else{let s=N(Z).div(o).mul(f.abs()).div(_?B:W).mul(10**(_?T:L)).toDP(0,pt.ROUND_UP),u=await this._sdk.SwapModules.findRouters(v[0].reserve.coinType,m[0].reserve.coinType,s.toString(),!1,[a]),d=u?.amount_in.toString(),h=N(d).mul(_?W:B).div(10**(_?L:T)).toString(),M=N(Z).mul(C).toString(),P=N(A).div(N(Z).sub(N(h))).toString(),w=N(P).gt(C),l=s.toString(),y=N(l).mul(_?B:W).toString(),$=d,q=N($).mul(_?W:B).toString(),j,V="",z="";if(w){let{clmm_pool_coin_type_a:st,clmm_pool_coin_type_b:J,clmm_pool:H,clmm_fee_tier:ne}=await this._sdk.SwapModules.getFlashLoanPool(m[0].reserve.coinType,l.toString());j=ne,V=st,z=J}let G=N(g).sub(N($).div(10**(_?L:T))).toString(),rt=N(G).mul(_?W:B).toString(),X=N(b).sub(N(l)).div(10**(_?T:L)).toString(),it=N(X).mul(_?B:W).toString();return{amount_in:u?.amount_in.toString(),amount_out:u?.amount_out.toString(),deposit_amount:$,borrow_amount:l,deposit_amount_usd:q,borrow_amount_usd:y,base_token:D,quote_token:O,reserve:E,deposits:v,borrows:m,clmm_fee_tier:j,withdraw_ctoken_amount:d,is_flash_loan:w,clmm_pool_coin_type_a:V,clmm_pool_coin_type_b:z,after_deposit_amount:G,after_borrow_amount:X,after_deposit_amount_usd:rt,after_borrow_amount_usd:it,position_cap_id:k,market_id:S,is_long:_,routers:u}}};this.calculatePositionRepay=async r=>{let{position_id:i,amount:o,is_quote:t}=r,{borrows:a,deposits:f,is_long:c,market_id:m,position_cap_id:v}=await this._sdk.PositionModules.getPositionInfo(i),_=a[0].reserve.coinType,k=c?f[0].reserve.coinType:a[0].reserve.coinType,S=c?a[0].reserve.coinType:f[0].reserve.coinType,D,O=c?!t:t;return O&&(D=await this._sdk.SwapModules.findRouters(t?S:k,_,o,!0,[])),{routers:D,repay_coin_type:_,base_token:k,quote_token:S,is_long:c,borrows:a,deposits:f,market_id:m,position_cap_id:v,has_swap:O}};this._sdk=r}getObligationRewardsInfo(r,i){let o=[],t=new nt(10).pow(18),a={},f=Date.now();for(let m of i){let v=m.coinType,_=m.arrayIndex.toString(),k=m.depositsPoolRewardManager.poolRewards||[];for(let D of k){if(!D)continue;let O=D.id,R=D.coinType,E=parseInt(D.startTimeMs),L=parseInt(D.endTimeMs),T=f>=E&&f<L,I=D.mintDecimals,B=nt(D.cumulativeRewardsPerShare).times(10**I);a[O]={coinType:R,mintDecimals:I,isActive:T,reserveType:"deposit",reserveArrayIndex:_,reserveCoinType:v,globalCumulativePerShare:B,rewardIndex:D.rewardIndex,reserve:m}}let S=m.borrowsPoolRewardManager.poolRewards||[];for(let D of S){if(!D)continue;let O=D.id,R=D.coinType,E=parseInt(D.startTimeMs),L=parseInt(D.endTimeMs),T=f>=E&&f<L,I=D.mintDecimals,B=nt(D.cumulativeRewardsPerShare).times(10**I);a[O]={coinType:R,mintDecimals:I,isActive:T,reserveType:"borrow",reserveCoinType:v,reserveArrayIndex:_,rewardIndex:D.rewardIndex,globalCumulativePerShare:B,reserve:m}}}let c=r.userRewardManagers;for(let m of c)for(let v=0;v<m.rewards.length;v++){let _=m.rewards[v];if(!_)continue;let k=a[_.poolRewardId];if(!k)continue;let S=k.globalCumulativePerShare,D=new nt(_.cumulativeRewardsPerShare.value.toString()).div(t),O=new nt(m.share).times(S.minus(D)),R=new nt(_.earnedRewards.value).div(t),E=new nt(R).plus(O),L=k.reserve.price,T=E.div(10**k.mintDecimals),I=L?T.times(L).toFixed(12):0;N(I).gt(0)&&o.push({poolRewardId:_.poolRewardId,coinType:k.coinType,earnedRewards:E.toString(),earnedRewardsUsd:I,earnedRewardsFormatted:T.toFixed(12),isActive:k.isActive,reserveType:k.reserveType,reserveCoinType:k.reserveCoinType,reserveArrayIndex:k.reserveArrayIndex,rewardIndex:k.rewardIndex,rewardInfo:k})}return o}createLeveragePosition(r){let{lending_market_id:i,lending_market_type:o}=mt(this._sdk.sdkOptions.suilend),{global_config_id:t,versioned_id:a}=mt(this._sdk.sdkOptions.margin_trading),f=this._sdk.sdkOptions.margin_trading,{leverage:c,market_id:m,base_token:v,quote_token:_,is_long:k,init_deposit_amount:S,tx:D,init_coin_type:O}=r;return D.moveCall({target:`${f.published_at}::router::open_position`,arguments:[D.object(t),D.object(m),D.object(i),D.pure.u64(N(c).mul(1e4).toString()),D.object(ye),D.object(a)],typeArguments:[o,k?v:_]})}async depositToLeveragePosition(r,i){let{margin_trading:o}=this._sdk.sdkOptions,{lending_market_type:t,lending_market_id:a}=mt(this._sdk.sdkOptions.suilend),{is_long:f,market_id:c,position_cap_id:m,position_cap:v,deposit_reserve_array_index:_,input_coin:k,base_token:S,quote_token:D}=r,{global_config_id:O,versioned_id:R}=mt(this._sdk.sdkOptions.margin_trading);return i.moveCall({target:`${o.published_at}::router::deposit`,typeArguments:[t,f?S:D],arguments:[i.object(O),i.object(a),i.object(c),m?i.object(m):v,k,i.pure.u64(_),i.object(ye),i.object(R)]}),i}extractOtherTokenTypes(r,i,o,t){let a=[];return r.forEach(f=>{f.reserve.coinType!==o&&f.reserve.coinType!==t&&a.push(f.reserve.coinType)}),i.forEach(f=>{f.reserve.coinType!==o&&f.reserve.coinType!==t&&a.push(f.reserve.coinType)}),a}async buildClaimRewardsMoveCall(r,i,o,t){let{suilend:a,margin_trading:f}=this._sdk.sdkOptions,c=mt(a).lending_market_id,m=mt(a).lending_market_type,{global_config_id:v,versioned_id:_}=mt(this._sdk.sdkOptions.margin_trading);for(let k=0;k<r.length;k++){let S=t.moveCall({target:`${f.published_at}::router::claim_rewards`,arguments:[t.object(v),t.object(c),t.object(i),t.object(o),t.pure.u64(r[k].reserveArrayIndex.toString()),t.pure.u64(r[k].rewardIndex.toString()),t.pure.bool(r[k].reserveType==="deposit"),t.object(ye),t.object(_)],typeArguments:[m,r[k].coinType]});t.transferObjects([S],this._sdk.getSenderAddress())}}};import{SuilendClient as uo}from"@suilend/sdk/client";import{initializeSuilend as co,initializeSuilendRewards as fo,initializeObligations as ho,formatRewards as po,Side as ve,getFilteredRewards as mo,getStakingYieldAprPercent as go,getDedupedPerDayRewards as _o,getDedupedAprRewards as wo,getTotalAprPercent as Er,getNetAprPercent as vo}from"@suilend/sdk";import{d as ji,getPackagerConfigs as De,removeHexPrefix as ei}from"@cetusprotocol/common-sdk";import{toHEX as Nr}from"@mysten/sui/utils";var ii=class{constructor(r){this.getCoinAprByAmount=async(r,i,o)=>{let t=new nt(i.toString()),{allLendingMarketData:a,lstAprPercentMap:f}=await this.getLendingMarketData(!1);if(await this.getInitializeObligations({allLendingMarketData:a,lstAprPercentMap:f},!0),!a||!f)throw new Error("lendingMarketCache is not initialized");let c=Object.values(a??{})[0],m=c.lendingMarket.reserves.find(g=>g.coinType===r);if(!m)throw new Error("reserve not found");let _=(this.obligationsCache?.[c.lendingMarket.id]).rewardMap[m.token.coinType]?.[o]??[],k=mo(_),S=go(o,m.coinType,f),D=o===ve.DEPOSIT?m.depositAprPercent:m.borrowAprPercent,O=D,R=new nt(1),E=!0;if(o!==void 0&&t!==void 0&&t.gt(0)){let g={...m,depositedAmount:o===ve.DEPOSIT?nt.max(m.depositedAmount.plus(o===ve.DEPOSIT?t:t.negated()),0):m.depositedAmount,borrowedAmount:o===ve.BORROW?nt.max(m.borrowedAmount.plus(o===ve.BORROW?t:t.negated()),0):m.borrowedAmount};O=o===ve.DEPOSIT?Dr(g):Ui(g);let b=o===ve.DEPOSIT?m.depositedAmount:m.borrowedAmount,A=o===ve.DEPOSIT?g.depositedAmount:g.borrowedAmount;R=A.eq(0)?new nt(-1):b.div(A),E=!R.eq(-1)}let I=_o(k).map(g=>({...g,stats:{...g.stats,perDay:E?g.stats.perDay.times(R):void 0}})),W=wo(k).map(g=>({...g,stats:{...g.stats,aprPercent:E?g.stats.aprPercent.times(R):void 0}})),U=Er(o,D,k,S),Z=O===void 0||W.some(g=>g.stats.aprPercent===void 0)?void 0:Er(o,O,W,S);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:U.toString()}};this.refreshReservePrice=async(r,i,o)=>{let{lending_market_id:t,lending_market_type:a}=De(this.sdk.sdkOptions?.suilend),f=`${t}_${a}`;if(!this.suilendClientCache)throw new Error("suilendClientCache is not initialized");let c=await this.suilendClientCache[f].pythClient.getPriceFeedObjectId(i);this.suilendClientCache[f].refreshReservePrices(r,c,o)};this.getLatestPriceFeeds=async(r,i=!1)=>{let{lending_market_id:o,lending_market_type:t}=De(this.sdk.sdkOptions?.suilend),a={},f=[];if(r.forEach(_=>{let k=this.sdk.getCache(`getLatestPrice_${_.coinType.name}`,i);k&&this.priceCheck(k,60)?a[_.coinType.name]=k:f.push(_.coinType.name)}),f.length==0)return a;let c=`${o}_${t}`;if(!this.suilendClientCache)throw new Error("suilendClient is not initialized");let m=Array.from(new Set(r.map(_=>_?.priceIdentifier?.bytes?Nr(new Uint8Array(_.priceIdentifier.bytes)):ei(_.priceIdentifier.toString()))));return(await this.suilendClientCache[c].pythConnection.getLatestPriceFeeds(m))?.forEach((_,k)=>{let S=_.getPriceUnchecked(),D=r[k];if(S){let{price:O,expo:R}=S,E=ji(O).mul(ji(10).pow(ji(R))).toString(),L={coin_type:D.coinType.name||D.coinType,price:E,oracle_price:0n,last_update_time:S.publishTime};L.oracle_price=Tr(BigInt(O),BigInt(R)),a[D.coinType.name||D.coinType]=L,this.sdk.updateCache(`getLatestPrice_${L.coin_type}`,L)}}),a};this.getLatestPriceFeedsByCoinTypes=async(r,i=!1)=>{try{let{lending_market_id:o}=De(this.sdk.sdkOptions?.suilend),{allLendingMarketData:t}=await this.getLendingMarketData(!1),a=[];return r.forEach(c=>{let m=t[o].reserveMap[c];a.push(m)}),await this.getLatestPriceFeeds(a,i)}catch(o){Zt("FetchError",o);return}};this.getSuiLendReserveInfo=async(r,i,o,t)=>{let{lending_market_id:a}=De(this.sdk.sdkOptions?.suilend);o||(o=(await this.getLendingMarketData(!1)).allLendingMarketData);let f=o[a].reserveMap[r],c=o[a].reserveMap[i],m=o[a].refreshedRawReserves.filter(D=>D.coinType.name==ei(r)),v=o[a].refreshedRawReserves.filter(D=>D.coinType.name==ei(i)),_=o[a].reserveMap[r],k=o[a].reserveMap[i],S=[];return t&&t.forEach(D=>{let O=o[a].refreshedRawReserves.filter(R=>R.coinType.name==ei(D));S.push(...O)}),{base_reserve_array_index:f.arrayIndex.toString(),quote_reserve_array_index:c.arrayIndex.toString(),reserve:[m[0],v[0],...S],base_reserve_map_info:_,quote_reserve_map_info:k}};this.refreshReservePrices=async(r,i)=>{for(let o=0;o<i.length;o++){let t=Nr(new Uint8Array(i[o].priceIdentifier.bytes));await this.refreshReservePrice(r,t,BigInt(i[o].arrayIndex))}};this.refreshReservePricesV2=async(r,i)=>{for(let o=0;o<i.length;o++){let t=i[o].priceIdentifier.toString();await this.refreshReservePrice(r,t,BigInt(i[o].arrayIndex))}};this.sdk=r}async getSuilendClient(r,i){let o=`${r}_${i}`;return this.suilendClientCache||(this.suilendClientCache={}),this.suilendClientCache[o]||(this.suilendClientCache[o]=await uo.initialize(r,i,this.sdk.FullClient,!0)),this.suilendClientCache[o]}async getLendingMarketData(r=!0){let i="lendingMarketData",o=this.sdk.getCache(i,r);if(o)return o;let t=De(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,f]=await Promise.all([(async()=>Object.fromEntries(await Promise.all(t.map(async m=>{let v=await this.getSuilendClient(m.id,m.type),{lendingMarket:_,coinMetadataMap:k,refreshedRawReserves:S,reserveMap:D,reserveCoinTypes:O,reserveCoinMetadataMap:R,rewardCoinTypes:E,activeRewardCoinTypes:L,rewardCoinMetadataMap:T}=await co(this.sdk.FullClient,v),{rewardPriceMap:I}=await fo(D,L);return[m.id,{suilendClient:v,lendingMarket:_,coinMetadataMap:k,refreshedRawReserves:S,reserveMap:D,reserveCoinTypes:O,reserveCoinMetadataMap:R,rewardPriceMap:I,rewardCoinTypes:E,activeRewardCoinTypes:L,rewardCoinMetadataMap:T}]}))))(),(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(([v,_])=>[v,new nt(_)]))}catch(c){return console.error(c),{}}})()]);return this.sdk.updateCache(i,{allLendingMarketData:a,lstAprPercentMap:f},30*1e3),{allLendingMarketData:a,lstAprPercentMap:f}}catch(a){throw console.log("\u{1F680} getLendingMarketData error:",a),a}}async getInitializeObligations(r,i=!1){if(!i&&this.obligationsCache)return this.obligationsCache;r||(r=await this.getLendingMarketData(!1));let o={};for(let t of Object.values(r.allLendingMarketData)){let{obligationOwnerCaps:a,obligations:f}=await ho(this.sdk.FullClient,t.suilendClient,t.refreshedRawReserves,t.reserveMap,this.sdk.senderAddress);f.length>0&&console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:181 ~ SuiLendModule ~ getInitializeObligations ~ obligations:",f[0].original.allowedBorrowValueUsd.value.toString());let c=po(t.reserveMap,t.rewardCoinMetadataMap,t.rewardPriceMap,f);o[t.lendingMarket.id]={obligationOwnerCaps:a,obligations:f,rewardMap:c}}return this.obligationsCache=o,this.obligationsCache}async getSuiLendUserData(r,i=!1){let{allLendingMarketData:o,lstAprPercentMap:t}=await this.getLendingMarketData(!1),a=await this.getInitializeObligations({allLendingMarketData:o,lstAprPercentMap:t},!1),{lending_market_id:f}=De(this.sdk.sdkOptions?.suilend),c=a[f];if(!c)throw new Error(`No user data found for lending market: ${f}`);let m=c.obligations.find(_=>_.id===r);if(!m)throw new Error(`Obligation not found: ${r}`);let v=vo(m,c.rewardMap,t??{});return{obligation:m,netAprPercent:v,deposits:m.deposits,borrowedAmount:m.borrowedAmount,depositedAmount:m.depositedAmount,netValueUsd:m.netValueUsd,rewardMap:c.rewardMap}}priceCheck(r,i=60){let o=Math.floor(Date.now()/1e3);if(!(Math.abs(o-r.last_update_time)>i))return r}};var Zi=Hr(Br());import{Transaction as Mo}from"@mysten/sui/transactions";import{addHexPrefix as bo,CoinAssist as Wi,d as re,getPackagerConfigs as qr,removeHexPrefix as Fr}from"@cetusprotocol/common-sdk";var ri=class{constructor(r){this.routerSwap=async r=>{let{slippage:i,txb:o,input_coin:t,router:a}=r,f=o||new Mo;if(a)return await this._sdk.AggregatorClient.routerSwap({router:a,inputCoin:t,slippage:i,txb:f})};this.flashLoan=r=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:183 ~ SwapModules ~ params:",r);let{clmm_pool:i}=this._sdk.ClmmSDK.sdkOptions,{global_config_id:o}=qr(this._sdk.ClmmSDK.sdkOptions.clmm_pool),{amount:t,clmm_pool:a,clmm_pool_coin_type_a:f,clmm_pool_coin_type_b:c,flash_loan_coin:m,tx:v,amount_u64:_}=r,k=bo(f)===m;console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:188 ~ SwapModules ~ isLoanA:",k);let[S,D,O]=v.moveCall({target:`${i.published_at}::pool::flash_loan`,arguments:[v.object(o),v.object(a),v.pure.bool(k),_||v.pure.u64(t.toString())],typeArguments:[f,c]});return{balance_a:S,balance_b:D,receipt:O,is_loan_a:k,loan_coin_type:k?f:c}};this.getFlashLoanPool=async(r,i)=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:201 ~ SwapModules ~ flash_amount:",i,r);try{let o=await fetch(`https://api-sui.cetus.zone/v3/sui/margin_trading/pools?coin_type=${r}&min_amount=${i}`),{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:re(t[0].fee_rate).div(1e6).toString()}:(Zt("FlashLoanPoolNotFound","FlashLoanPoolNotFound"),{clmm_pool_coin_type_a:"",clmm_pool_coin_type_b:"",clmm_pool:"",clmm_fee_tier:""})}catch(o){return Zt("FetchError",o),{clmm_pool_coin_type_a:"",clmm_pool_coin_type_b:"",clmm_pool:"",clmm_fee_tier:""}}};this.repayFlashLoan=r=>{let{clmm_pool:i}=this._sdk.ClmmSDK.sdkOptions,{global_config_id:o}=qr(this._sdk.ClmmSDK.sdkOptions.clmm_pool),{tx:t,repay_base:a,repay_quote:f,receipt:c,clmm_pool:m,clmm_pool_coin_type_a:v,clmm_pool_coin_type_b:_}=r;return t.moveCall({target:`${i.published_at}::pool::repay_flash_loan`,arguments:[t.object(o),t.object(m),a,f,c],typeArguments:[v,_]}),t};this._sdk=r}async findRouters(r,i,o,t,a){try{let f={from:r,target:i,amount:new Zi.default(re(o).toFixed(0).toString()),byAmountIn:t,depth:3},c=await this._sdk.AggregatorClient.findRouters(f);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:r,target:i,amount:new Zi.default(re(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(r){let{is_long:i,is_quote:o,amount:t,base_token:a,quote_token:f,swap_clmm_pool:c="",slippage:m,tx:v}=r,_=i&&o||!i&&!o,k,S,D=t;if(_){let O=i&&o?f:a,R=i&&o?a:f,E=await this.findRouters(O,R,t,!0,[c]);if(D=E?.amount_out.toString(),E){let L=Wi.buildCoinWithBalance(BigInt(t),O,v);S=await this.routerSwap({router:E.route_obj,slippage:m,input_coin:L,txb:v}),k=S}}else k=Wi.buildCoinWithBalance(BigInt(t.toString()),o?f:a,v);return{has_swap:_,input_coin:k,init_deposit_amount:D}}async handleSwap(r){let{swap_clmm_pool:i="",slippage:o,amount:t,from:a,to:f,tx:c,input_coin:m}=r,v=await this.findRouters(a,f,t,!0,[i]),_=v.amount_out.toString();return{swap_out_coin:await this.routerSwap({router:v.route_obj,slippage:o,input_coin:m||Wi.buildCoinWithBalance(BigInt(t),a,c),txb:c}),amount_out:_}}async calculateFlashLoan(r){let{is_long:i,leverage:o,base_token:t,quote_token:a,deposit_amount:f,reserve:c,base_token_decimal:m,quote_token_decimal:v}=r,_=await this._sdk.SuiLendModule.getLatestPriceFeeds(c),k=_&&_[Fr(a)]?.price,S=_&&_[Fr(t)]?.price,D=i?re(S).div(re(k)).toString():re(k).div(re(S)).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:267 ~ SwapModules ~ calculateFlashLoan ~ deposit_amount:",f,D,m,v);let O=re(f).mul(re(o).sub(1)).mul(D).div(10**(i?m:v)).mul(10**(i?v:m)).toDP(0,pt.ROUND_UP).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ swapModules.ts:274 ~ SwapModules ~ calculateFlashLoan ~ flashAmount:",re(f).mul(re(o).sub(1)).mul(D).div(10**(i?m:v)).mul(10**(i?v:m)).toString(),O);let R=i?a:t,{clmm_pool_coin_type_a:E,clmm_fee_tier:L,clmm_pool:T,clmm_pool_coin_type_b:I}=await this.getFlashLoanPool(R,O),B=E===R,W=re(O).mul(L).toString();return{flash_amount:O,rate:D,quote_price:k,base_price:S,flash_loan_coin:R,is_flash_a:B,flash_loan_fee:W,clmm_pool:T,clmm_pool_coin_type_a:E,clmm_pool_coin_type_b:I,clmm_fee_tier:L}}};var ni=class n extends Ao{constructor(i,o){super(i);this._marketLeverageThresholds={};this._suilendModule=new ii(this),this._permissionModules=new qe(this),this._marketModules=new Be(this),this._positionModules=new ti(this),this._swapModules=new ri(this),this._aggregatorClient=new Co({signer:ko("0x0"),client:i.sui_client||new yo({url:i.full_rpc_url}),env:i.env==="testnet"?Ur.Testnet:Ur.Mainnet}),this._clmmSDK=o||Po.createSDK({env:i.env,full_rpc_url:i.full_rpc_url}),this._pythConnection=new So("https://hermes.pyth.network",{timeout:30*1e3}),this.setMarketLeverageThresholds({"0x7c62cbfa1884c02eec32cfa6a1e4325550fb6dda9579b030c3bae3031b80e0e4":{long_threshold:1.65,short_threshold:1.7},"0xe41b1cc8154300743bfd7bb9e6325edc2beea98e957dbe4c68790e7a92a13922":{long_threshold:1.55,short_threshold:1.7},"0x71bc9710e59c3f2c385e1530e16e9aecbcd02a1bab8a557825c73760efd9d0ec":{long_threshold:1.65,short_threshold:1.7}})}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}getMarketLeverageThresholds(){return{...this._marketLeverageThresholds}}getLeverageThresholdForMarket(i){return this._marketLeverageThresholds[i]}setLeverageThresholdForMarket(i,o){this._marketLeverageThresholds[i]=o}setMarketLeverageThresholds(i){this._marketLeverageThresholds={...i}}static createSDK(i){return n.createCustomSDK({...nr,...i})}static createCustomSDK(i){return new n(i)}};var va=ni;export{ni as CetusMarginTradingSDK,ms as appendExtraRewards,Or as calcIncrementalLeverage,Ui as calculateBorrowAprPercent,Dr as calculateDepositAprPercent,xr as calculateUtilizationPercent,va as default,vs as formatLtvPercentValue,Tr as getPriceWithFormattedDecimals,_s as getRewardsAprMultiplier,gs as getUpdatedReserve,nr as margin_trading_mainnet,Pi as mergePositionData,Fi as oraclePriceMultiplierDecimal,ws as updateRewardsWithMultiplier,Si as wrapMarketInfo,Ci as wrapMarketPermissions,Ai as wrapPosition};
2
- /*! Bundled license information:
3
-
4
- decimal.js/decimal.mjs:
5
- (*!
6
- * decimal.js v10.6.0
7
- * An arbitrary-precision Decimal type for JavaScript.
8
- * https://github.com/MikeMcl/decimal.js
9
- * Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
10
- * MIT Licence
11
- *)
12
- */
13
- //# sourceMappingURL=index.mjs.map