lll-sdk 0.0.0-experimental-20251014152233 → 0.0.0-experimental-20251014153114
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var Ti=Object.create;var Hr=Object.defineProperty;var xi=Object.getOwnPropertyDescriptor;var Di=Object.getOwnPropertyNames;var Oi=Object.getPrototypeOf,Ii=Object.prototype.hasOwnProperty;var Ri=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(i,r)=>(typeof require<"u"?require:i)[r]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var Bi=(n,i)=>()=>(i||n((i={exports:{}}).exports,i),i.exports);var Ni=(n,i,r,o)=>{if(i&&typeof i=="object"||typeof i=="function")for(let t of Di(i))!Ii.call(n,t)&&t!==r&&Hr(n,t,{get:()=>i[t],enumerable:!(o=xi(i,t))||o.enumerable});return n};var Ei=(n,i,r)=>(r=n!=null?Ti(Oi(n)):{},Ni(i||!n||!n.__esModule?Hr(r,"default",{value:n,enumerable:!0}):r,n));var Mi=Bi((wi,Cr)=>{"use strict";(function(n,i){"use strict";function r(h,e){if(!h)throw new Error(e||"Assertion failed")}function o(h,e){h.super_=e;var s=function(){};s.prototype=e.prototype,h.prototype=new s,h.prototype.constructor=h}function t(h,e,s){if(t.isBN(h))return h;this.negative=0,this.words=null,this.length=0,this.red=null,h!==null&&((e==="le"||e==="be")&&(s=e,e=10),this._init(h||0,e||10,s||"be"))}typeof n=="object"?n.exports=t:i.BN=t,t.BN=t,t.wordSize=26;var u;try{typeof window<"u"&&typeof window.Buffer<"u"?u=window.Buffer:u=Ri("buffer").Buffer}catch{}t.isBN=function(e){return e instanceof t?!0:e!==null&&typeof e=="object"&&e.constructor.wordSize===t.wordSize&&Array.isArray(e.words)},t.max=function(e,s){return e.cmp(s)>0?e:s},t.min=function(e,s){return e.cmp(s)<0?e:s},t.prototype._init=function(e,s,l){if(typeof e=="number")return this._initNumber(e,s,l);if(typeof e=="object")return this._initArray(e,s,l);s==="hex"&&(s=16),r(s===(s|0)&&s>=2&&s<=36),e=e.toString().replace(/\s+/g,"");var f=0;e[0]==="-"&&(f++,this.negative=1),f<e.length&&(s===16?this._parseHex(e,f,l):(this._parseBase(e,s,f),l==="le"&&this._initArray(this.toArray(),s,l)))},t.prototype._initNumber=function(e,s,l){e<0&&(this.negative=1,e=-e),e<67108864?(this.words=[e&67108863],this.length=1):e<4503599627370496?(this.words=[e&67108863,e/67108864&67108863],this.length=2):(r(e<9007199254740992),this.words=[e&67108863,e/67108864&67108863,1],this.length=3),l==="le"&&this._initArray(this.toArray(),s,l)},t.prototype._initArray=function(e,s,l){if(r(typeof e.length=="number"),e.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(e.length/3),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var d,v,k=0;if(l==="be")for(f=e.length-1,d=0;f>=0;f-=3)v=e[f]|e[f-1]<<8|e[f-2]<<16,this.words[d]|=v<<k&67108863,this.words[d+1]=v>>>26-k&67108863,k+=24,k>=26&&(k-=26,d++);else if(l==="le")for(f=0,d=0;f<e.length;f+=3)v=e[f]|e[f+1]<<8|e[f+2]<<16,this.words[d]|=v<<k&67108863,this.words[d+1]=v>>>26-k&67108863,k+=24,k>=26&&(k-=26,d++);return this._strip()};function p(h,e){var s=h.charCodeAt(e);if(s>=48&&s<=57)return s-48;if(s>=65&&s<=70)return s-55;if(s>=97&&s<=102)return s-87;r(!1,"Invalid character in "+h)}function c(h,e,s){var l=p(h,s);return s-1>=e&&(l|=p(h,s-1)<<4),l}t.prototype._parseHex=function(e,s,l){this.length=Math.ceil((e.length-s)/6),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var d=0,v=0,k;if(l==="be")for(f=e.length-1;f>=s;f-=2)k=c(e,s,f)<<d,this.words[v]|=k&67108863,d>=18?(d-=18,v+=1,this.words[v]|=k>>>26):d+=8;else{var w=e.length-s;for(f=w%2===0?s+1:s;f<e.length;f+=2)k=c(e,s,f)<<d,this.words[v]|=k&67108863,d>=18?(d-=18,v+=1,this.words[v]|=k>>>26):d+=8}this._strip()};function g(h,e,s,l){for(var f=0,d=0,v=Math.min(h.length,s),k=e;k<v;k++){var w=h.charCodeAt(k)-48;f*=l,w>=49?d=w-49+10:w>=17?d=w-17+10:d=w,r(w>=0&&d<l,"Invalid character"),f+=d}return f}t.prototype._parseBase=function(e,s,l){this.words=[0],this.length=1;for(var f=0,d=1;d<=67108863;d*=s)f++;f--,d=d/s|0;for(var v=e.length-l,k=v%f,w=Math.min(v,v-k)+l,a=0,_=l;_<w;_+=f)a=g(e,_,_+f,s),this.imuln(d),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a);if(k!==0){var F=1;for(a=g(e,_,e.length,s),_=0;_<k;_++)F*=s;this.imuln(F),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a)}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 M(h,e){h.words=e.words,h.length=e.length,h.negative=e.negative,h.red=e.red}if(t.prototype._move=function(e){M(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")]=b}catch{t.prototype.inspect=b}else t.prototype.inspect=b;function b(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var S=["","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"],C=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],T=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];t.prototype.toString=function(e,s){e=e||10,s=s|0||1;var l;if(e===16||e==="hex"){l="";for(var f=0,d=0,v=0;v<this.length;v++){var k=this.words[v],w=((k<<f|d)&16777215).toString(16);d=k>>>24-f&16777215,f+=2,f>=26&&(f-=26,v--),d!==0||v!==this.length-1?l=S[6-w.length]+w+l:l=w+l}for(d!==0&&(l=d.toString(16)+l);l.length%s!==0;)l="0"+l;return this.negative!==0&&(l="-"+l),l}if(e===(e|0)&&e>=2&&e<=36){var a=C[e],_=T[e];l="";var F=this.clone();for(F.negative=0;!F.isZero();){var E=F.modrn(_).toString(e);F=F.idivn(_),F.isZero()?l=E+l:l=S[a-E.length]+E+l}for(this.isZero()&&(l="0"+l);l.length%s!==0;)l="0"+l;return this.negative!==0&&(l="-"+l),l}r(!1,"Base should be between 2 and 36")},t.prototype.toNumber=function(){var e=this.words[0];return this.length===2?e+=this.words[1]*67108864:this.length===3&&this.words[2]===1?e+=4503599627370496+this.words[1]*67108864:this.length>2&&r(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-e:e},t.prototype.toJSON=function(){return this.toString(16,2)},u&&(t.prototype.toBuffer=function(e,s){return this.toArrayLike(u,e,s)}),t.prototype.toArray=function(e,s){return this.toArrayLike(Array,e,s)};var D=function(e,s){return e.allocUnsafe?e.allocUnsafe(s):new e(s)};t.prototype.toArrayLike=function(e,s,l){this._strip();var f=this.byteLength(),d=l||Math.max(1,f);r(f<=d,"byte array longer than desired length"),r(d>0,"Requested array length <= 0");var v=D(e,d),k=s==="le"?"LE":"BE";return this["_toArrayLike"+k](v,f),v},t.prototype._toArrayLikeLE=function(e,s){for(var l=0,f=0,d=0,v=0;d<this.length;d++){var k=this.words[d]<<v|f;e[l++]=k&255,l<e.length&&(e[l++]=k>>8&255),l<e.length&&(e[l++]=k>>16&255),v===6?(l<e.length&&(e[l++]=k>>24&255),f=0,v=0):(f=k>>>24,v+=2)}if(l<e.length)for(e[l++]=f;l<e.length;)e[l++]=0},t.prototype._toArrayLikeBE=function(e,s){for(var l=e.length-1,f=0,d=0,v=0;d<this.length;d++){var k=this.words[d]<<v|f;e[l--]=k&255,l>=0&&(e[l--]=k>>8&255),l>=0&&(e[l--]=k>>16&255),v===6?(l>=0&&(e[l--]=k>>24&255),f=0,v=0):(f=k>>>24,v+=2)}if(l>=0)for(e[l--]=f;l>=0;)e[l--]=0},Math.clz32?t.prototype._countBits=function(e){return 32-Math.clz32(e)}:t.prototype._countBits=function(e){var s=e,l=0;return s>=4096&&(l+=13,s>>>=13),s>=64&&(l+=7,s>>>=7),s>=8&&(l+=4,s>>>=4),s>=2&&(l+=2,s>>>=2),l+s},t.prototype._zeroBits=function(e){if(e===0)return 26;var s=e,l=0;return(s&8191)===0&&(l+=13,s>>>=13),(s&127)===0&&(l+=7,s>>>=7),(s&15)===0&&(l+=4,s>>>=4),(s&3)===0&&(l+=2,s>>>=2),(s&1)===0&&l++,l},t.prototype.bitLength=function(){var e=this.words[this.length-1],s=this._countBits(e);return(this.length-1)*26+s};function O(h){for(var e=new Array(h.bitLength()),s=0;s<e.length;s++){var l=s/26|0,f=s%26;e[s]=h.words[l]>>>f&1}return e}t.prototype.zeroBits=function(){if(this.isZero())return 0;for(var e=0,s=0;s<this.length;s++){var l=this._zeroBits(this.words[s]);if(e+=l,l!==26)break}return e},t.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},t.prototype.toTwos=function(e){return this.negative!==0?this.abs().inotn(e).iaddn(1):this.clone()},t.prototype.fromTwos=function(e){return this.testn(e-1)?this.notn(e).iaddn(1).ineg():this.clone()},t.prototype.isNeg=function(){return this.negative!==0},t.prototype.neg=function(){return this.clone().ineg()},t.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},t.prototype.iuor=function(e){for(;this.length<e.length;)this.words[this.length++]=0;for(var s=0;s<e.length;s++)this.words[s]=this.words[s]|e.words[s];return this._strip()},t.prototype.ior=function(e){return r((this.negative|e.negative)===0),this.iuor(e)},t.prototype.or=function(e){return this.length>e.length?this.clone().ior(e):e.clone().ior(this)},t.prototype.uor=function(e){return this.length>e.length?this.clone().iuor(e):e.clone().iuor(this)},t.prototype.iuand=function(e){var s;this.length>e.length?s=e:s=this;for(var l=0;l<s.length;l++)this.words[l]=this.words[l]&e.words[l];return this.length=s.length,this._strip()},t.prototype.iand=function(e){return r((this.negative|e.negative)===0),this.iuand(e)},t.prototype.and=function(e){return this.length>e.length?this.clone().iand(e):e.clone().iand(this)},t.prototype.uand=function(e){return this.length>e.length?this.clone().iuand(e):e.clone().iuand(this)},t.prototype.iuxor=function(e){var s,l;this.length>e.length?(s=this,l=e):(s=e,l=this);for(var f=0;f<l.length;f++)this.words[f]=s.words[f]^l.words[f];if(this!==s)for(;f<s.length;f++)this.words[f]=s.words[f];return this.length=s.length,this._strip()},t.prototype.ixor=function(e){return r((this.negative|e.negative)===0),this.iuxor(e)},t.prototype.xor=function(e){return this.length>e.length?this.clone().ixor(e):e.clone().ixor(this)},t.prototype.uxor=function(e){return this.length>e.length?this.clone().iuxor(e):e.clone().iuxor(this)},t.prototype.inotn=function(e){r(typeof e=="number"&&e>=0);var s=Math.ceil(e/26)|0,l=e%26;this._expand(s),l>0&&s--;for(var f=0;f<s;f++)this.words[f]=~this.words[f]&67108863;return l>0&&(this.words[f]=~this.words[f]&67108863>>26-l),this._strip()},t.prototype.notn=function(e){return this.clone().inotn(e)},t.prototype.setn=function(e,s){r(typeof e=="number"&&e>=0);var l=e/26|0,f=e%26;return this._expand(l+1),s?this.words[l]=this.words[l]|1<<f:this.words[l]=this.words[l]&~(1<<f),this._strip()},t.prototype.iadd=function(e){var s;if(this.negative!==0&&e.negative===0)return this.negative=0,s=this.isub(e),this.negative^=1,this._normSign();if(this.negative===0&&e.negative!==0)return e.negative=0,s=this.isub(e),e.negative=1,s._normSign();var l,f;this.length>e.length?(l=this,f=e):(l=e,f=this);for(var d=0,v=0;v<f.length;v++)s=(l.words[v]|0)+(f.words[v]|0)+d,this.words[v]=s&67108863,d=s>>>26;for(;d!==0&&v<l.length;v++)s=(l.words[v]|0)+d,this.words[v]=s&67108863,d=s>>>26;if(this.length=l.length,d!==0)this.words[this.length]=d,this.length++;else if(l!==this)for(;v<l.length;v++)this.words[v]=l.words[v];return this},t.prototype.add=function(e){var s;return e.negative!==0&&this.negative===0?(e.negative=0,s=this.sub(e),e.negative^=1,s):e.negative===0&&this.negative!==0?(this.negative=0,s=e.sub(this),this.negative=1,s):this.length>e.length?this.clone().iadd(e):e.clone().iadd(this)},t.prototype.isub=function(e){if(e.negative!==0){e.negative=0;var s=this.iadd(e);return e.negative=1,s._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(e),this.negative=1,this._normSign();var l=this.cmp(e);if(l===0)return this.negative=0,this.length=1,this.words[0]=0,this;var f,d;l>0?(f=this,d=e):(f=e,d=this);for(var v=0,k=0;k<d.length;k++)s=(f.words[k]|0)-(d.words[k]|0)+v,v=s>>26,this.words[k]=s&67108863;for(;v!==0&&k<f.length;k++)s=(f.words[k]|0)+v,v=s>>26,this.words[k]=s&67108863;if(v===0&&k<f.length&&f!==this)for(;k<f.length;k++)this.words[k]=f.words[k];return this.length=Math.max(this.length,k),f!==this&&(this.negative=1),this._strip()},t.prototype.sub=function(e){return this.clone().isub(e)};function B(h,e,s){s.negative=e.negative^h.negative;var l=h.length+e.length|0;s.length=l,l=l-1|0;var f=h.words[0]|0,d=e.words[0]|0,v=f*d,k=v&67108863,w=v/67108864|0;s.words[0]=k;for(var a=1;a<l;a++){for(var _=w>>>26,F=w&67108863,E=Math.min(a,e.length-1),U=Math.max(0,a-h.length+1);U<=E;U++){var Z=a-U|0;f=h.words[Z]|0,d=e.words[U]|0,v=f*d+F,_+=v/67108864|0,F=v&67108863}s.words[a]=F|0,w=_|0}return w!==0?s.words[a]=w|0:s.length--,s._strip()}var R=function(e,s,l){var f=e.words,d=s.words,v=l.words,k=0,w,a,_,F=f[0]|0,E=F&8191,U=F>>>13,Z=f[1]|0,G=Z&8191,te=Z>>>13,Ze=f[2]|0,J=Ze&8191,re=Ze>>>13,Ke=f[3]|0,ee=Ke&8191,Q=Ke>>>13,Or=f[4]|0,se=Or&8191,ae=Or>>>13,Ir=f[5]|0,le=Ir&8191,ue=Ir>>>13,Rr=f[6]|0,fe=Rr&8191,ce=Rr>>>13,Br=f[7]|0,de=Br&8191,he=Br>>>13,Nr=f[8]|0,pe=Nr&8191,ge=Nr>>>13,Er=f[9]|0,me=Er&8191,ve=Er>>>13,Fr=d[0]|0,we=Fr&8191,Me=Fr>>>13,Ur=d[1]|0,be=Ur&8191,ye=Ur>>>13,qr=d[2]|0,_e=qr&8191,ke=qr>>>13,jr=d[3]|0,Ae=jr&8191,Se=jr>>>13,$r=d[4]|0,Ce=$r&8191,Pe=$r>>>13,Zr=d[5]|0,Le=Zr&8191,Te=Zr>>>13,Wr=d[6]|0,xe=Wr&8191,De=Wr>>>13,zr=d[7]|0,Oe=zr&8191,Ie=zr>>>13,Kr=d[8]|0,Re=Kr&8191,Be=Kr>>>13,Gr=d[9]|0,Ne=Gr&8191,Ee=Gr>>>13;l.negative=e.negative^s.negative,l.length=19,w=Math.imul(E,we),a=Math.imul(E,Me),a=a+Math.imul(U,we)|0,_=Math.imul(U,Me);var Gt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Gt>>>26)|0,Gt&=67108863,w=Math.imul(G,we),a=Math.imul(G,Me),a=a+Math.imul(te,we)|0,_=Math.imul(te,Me),w=w+Math.imul(E,be)|0,a=a+Math.imul(E,ye)|0,a=a+Math.imul(U,be)|0,_=_+Math.imul(U,ye)|0;var Ht=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Ht>>>26)|0,Ht&=67108863,w=Math.imul(J,we),a=Math.imul(J,Me),a=a+Math.imul(re,we)|0,_=Math.imul(re,Me),w=w+Math.imul(G,be)|0,a=a+Math.imul(G,ye)|0,a=a+Math.imul(te,be)|0,_=_+Math.imul(te,ye)|0,w=w+Math.imul(E,_e)|0,a=a+Math.imul(E,ke)|0,a=a+Math.imul(U,_e)|0,_=_+Math.imul(U,ke)|0;var Vt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Vt>>>26)|0,Vt&=67108863,w=Math.imul(ee,we),a=Math.imul(ee,Me),a=a+Math.imul(Q,we)|0,_=Math.imul(Q,Me),w=w+Math.imul(J,be)|0,a=a+Math.imul(J,ye)|0,a=a+Math.imul(re,be)|0,_=_+Math.imul(re,ye)|0,w=w+Math.imul(G,_e)|0,a=a+Math.imul(G,ke)|0,a=a+Math.imul(te,_e)|0,_=_+Math.imul(te,ke)|0,w=w+Math.imul(E,Ae)|0,a=a+Math.imul(E,Se)|0,a=a+Math.imul(U,Ae)|0,_=_+Math.imul(U,Se)|0;var Qt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Qt>>>26)|0,Qt&=67108863,w=Math.imul(se,we),a=Math.imul(se,Me),a=a+Math.imul(ae,we)|0,_=Math.imul(ae,Me),w=w+Math.imul(ee,be)|0,a=a+Math.imul(ee,ye)|0,a=a+Math.imul(Q,be)|0,_=_+Math.imul(Q,ye)|0,w=w+Math.imul(J,_e)|0,a=a+Math.imul(J,ke)|0,a=a+Math.imul(re,_e)|0,_=_+Math.imul(re,ke)|0,w=w+Math.imul(G,Ae)|0,a=a+Math.imul(G,Se)|0,a=a+Math.imul(te,Ae)|0,_=_+Math.imul(te,Se)|0,w=w+Math.imul(E,Ce)|0,a=a+Math.imul(E,Pe)|0,a=a+Math.imul(U,Ce)|0,_=_+Math.imul(U,Pe)|0;var Xt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Xt>>>26)|0,Xt&=67108863,w=Math.imul(le,we),a=Math.imul(le,Me),a=a+Math.imul(ue,we)|0,_=Math.imul(ue,Me),w=w+Math.imul(se,be)|0,a=a+Math.imul(se,ye)|0,a=a+Math.imul(ae,be)|0,_=_+Math.imul(ae,ye)|0,w=w+Math.imul(ee,_e)|0,a=a+Math.imul(ee,ke)|0,a=a+Math.imul(Q,_e)|0,_=_+Math.imul(Q,ke)|0,w=w+Math.imul(J,Ae)|0,a=a+Math.imul(J,Se)|0,a=a+Math.imul(re,Ae)|0,_=_+Math.imul(re,Se)|0,w=w+Math.imul(G,Ce)|0,a=a+Math.imul(G,Pe)|0,a=a+Math.imul(te,Ce)|0,_=_+Math.imul(te,Pe)|0,w=w+Math.imul(E,Le)|0,a=a+Math.imul(E,Te)|0,a=a+Math.imul(U,Le)|0,_=_+Math.imul(U,Te)|0;var Jt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Jt>>>26)|0,Jt&=67108863,w=Math.imul(fe,we),a=Math.imul(fe,Me),a=a+Math.imul(ce,we)|0,_=Math.imul(ce,Me),w=w+Math.imul(le,be)|0,a=a+Math.imul(le,ye)|0,a=a+Math.imul(ue,be)|0,_=_+Math.imul(ue,ye)|0,w=w+Math.imul(se,_e)|0,a=a+Math.imul(se,ke)|0,a=a+Math.imul(ae,_e)|0,_=_+Math.imul(ae,ke)|0,w=w+Math.imul(ee,Ae)|0,a=a+Math.imul(ee,Se)|0,a=a+Math.imul(Q,Ae)|0,_=_+Math.imul(Q,Se)|0,w=w+Math.imul(J,Ce)|0,a=a+Math.imul(J,Pe)|0,a=a+Math.imul(re,Ce)|0,_=_+Math.imul(re,Pe)|0,w=w+Math.imul(G,Le)|0,a=a+Math.imul(G,Te)|0,a=a+Math.imul(te,Le)|0,_=_+Math.imul(te,Te)|0,w=w+Math.imul(E,xe)|0,a=a+Math.imul(E,De)|0,a=a+Math.imul(U,xe)|0,_=_+Math.imul(U,De)|0;var Yt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Yt>>>26)|0,Yt&=67108863,w=Math.imul(de,we),a=Math.imul(de,Me),a=a+Math.imul(he,we)|0,_=Math.imul(he,Me),w=w+Math.imul(fe,be)|0,a=a+Math.imul(fe,ye)|0,a=a+Math.imul(ce,be)|0,_=_+Math.imul(ce,ye)|0,w=w+Math.imul(le,_e)|0,a=a+Math.imul(le,ke)|0,a=a+Math.imul(ue,_e)|0,_=_+Math.imul(ue,ke)|0,w=w+Math.imul(se,Ae)|0,a=a+Math.imul(se,Se)|0,a=a+Math.imul(ae,Ae)|0,_=_+Math.imul(ae,Se)|0,w=w+Math.imul(ee,Ce)|0,a=a+Math.imul(ee,Pe)|0,a=a+Math.imul(Q,Ce)|0,_=_+Math.imul(Q,Pe)|0,w=w+Math.imul(J,Le)|0,a=a+Math.imul(J,Te)|0,a=a+Math.imul(re,Le)|0,_=_+Math.imul(re,Te)|0,w=w+Math.imul(G,xe)|0,a=a+Math.imul(G,De)|0,a=a+Math.imul(te,xe)|0,_=_+Math.imul(te,De)|0,w=w+Math.imul(E,Oe)|0,a=a+Math.imul(E,Ie)|0,a=a+Math.imul(U,Oe)|0,_=_+Math.imul(U,Ie)|0;var er=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(er>>>26)|0,er&=67108863,w=Math.imul(pe,we),a=Math.imul(pe,Me),a=a+Math.imul(ge,we)|0,_=Math.imul(ge,Me),w=w+Math.imul(de,be)|0,a=a+Math.imul(de,ye)|0,a=a+Math.imul(he,be)|0,_=_+Math.imul(he,ye)|0,w=w+Math.imul(fe,_e)|0,a=a+Math.imul(fe,ke)|0,a=a+Math.imul(ce,_e)|0,_=_+Math.imul(ce,ke)|0,w=w+Math.imul(le,Ae)|0,a=a+Math.imul(le,Se)|0,a=a+Math.imul(ue,Ae)|0,_=_+Math.imul(ue,Se)|0,w=w+Math.imul(se,Ce)|0,a=a+Math.imul(se,Pe)|0,a=a+Math.imul(ae,Ce)|0,_=_+Math.imul(ae,Pe)|0,w=w+Math.imul(ee,Le)|0,a=a+Math.imul(ee,Te)|0,a=a+Math.imul(Q,Le)|0,_=_+Math.imul(Q,Te)|0,w=w+Math.imul(J,xe)|0,a=a+Math.imul(J,De)|0,a=a+Math.imul(re,xe)|0,_=_+Math.imul(re,De)|0,w=w+Math.imul(G,Oe)|0,a=a+Math.imul(G,Ie)|0,a=a+Math.imul(te,Oe)|0,_=_+Math.imul(te,Ie)|0,w=w+Math.imul(E,Re)|0,a=a+Math.imul(E,Be)|0,a=a+Math.imul(U,Re)|0,_=_+Math.imul(U,Be)|0;var tr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(tr>>>26)|0,tr&=67108863,w=Math.imul(me,we),a=Math.imul(me,Me),a=a+Math.imul(ve,we)|0,_=Math.imul(ve,Me),w=w+Math.imul(pe,be)|0,a=a+Math.imul(pe,ye)|0,a=a+Math.imul(ge,be)|0,_=_+Math.imul(ge,ye)|0,w=w+Math.imul(de,_e)|0,a=a+Math.imul(de,ke)|0,a=a+Math.imul(he,_e)|0,_=_+Math.imul(he,ke)|0,w=w+Math.imul(fe,Ae)|0,a=a+Math.imul(fe,Se)|0,a=a+Math.imul(ce,Ae)|0,_=_+Math.imul(ce,Se)|0,w=w+Math.imul(le,Ce)|0,a=a+Math.imul(le,Pe)|0,a=a+Math.imul(ue,Ce)|0,_=_+Math.imul(ue,Pe)|0,w=w+Math.imul(se,Le)|0,a=a+Math.imul(se,Te)|0,a=a+Math.imul(ae,Le)|0,_=_+Math.imul(ae,Te)|0,w=w+Math.imul(ee,xe)|0,a=a+Math.imul(ee,De)|0,a=a+Math.imul(Q,xe)|0,_=_+Math.imul(Q,De)|0,w=w+Math.imul(J,Oe)|0,a=a+Math.imul(J,Ie)|0,a=a+Math.imul(re,Oe)|0,_=_+Math.imul(re,Ie)|0,w=w+Math.imul(G,Re)|0,a=a+Math.imul(G,Be)|0,a=a+Math.imul(te,Re)|0,_=_+Math.imul(te,Be)|0,w=w+Math.imul(E,Ne)|0,a=a+Math.imul(E,Ee)|0,a=a+Math.imul(U,Ne)|0,_=_+Math.imul(U,Ee)|0;var rr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(rr>>>26)|0,rr&=67108863,w=Math.imul(me,be),a=Math.imul(me,ye),a=a+Math.imul(ve,be)|0,_=Math.imul(ve,ye),w=w+Math.imul(pe,_e)|0,a=a+Math.imul(pe,ke)|0,a=a+Math.imul(ge,_e)|0,_=_+Math.imul(ge,ke)|0,w=w+Math.imul(de,Ae)|0,a=a+Math.imul(de,Se)|0,a=a+Math.imul(he,Ae)|0,_=_+Math.imul(he,Se)|0,w=w+Math.imul(fe,Ce)|0,a=a+Math.imul(fe,Pe)|0,a=a+Math.imul(ce,Ce)|0,_=_+Math.imul(ce,Pe)|0,w=w+Math.imul(le,Le)|0,a=a+Math.imul(le,Te)|0,a=a+Math.imul(ue,Le)|0,_=_+Math.imul(ue,Te)|0,w=w+Math.imul(se,xe)|0,a=a+Math.imul(se,De)|0,a=a+Math.imul(ae,xe)|0,_=_+Math.imul(ae,De)|0,w=w+Math.imul(ee,Oe)|0,a=a+Math.imul(ee,Ie)|0,a=a+Math.imul(Q,Oe)|0,_=_+Math.imul(Q,Ie)|0,w=w+Math.imul(J,Re)|0,a=a+Math.imul(J,Be)|0,a=a+Math.imul(re,Re)|0,_=_+Math.imul(re,Be)|0,w=w+Math.imul(G,Ne)|0,a=a+Math.imul(G,Ee)|0,a=a+Math.imul(te,Ne)|0,_=_+Math.imul(te,Ee)|0;var ir=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(ir>>>26)|0,ir&=67108863,w=Math.imul(me,_e),a=Math.imul(me,ke),a=a+Math.imul(ve,_e)|0,_=Math.imul(ve,ke),w=w+Math.imul(pe,Ae)|0,a=a+Math.imul(pe,Se)|0,a=a+Math.imul(ge,Ae)|0,_=_+Math.imul(ge,Se)|0,w=w+Math.imul(de,Ce)|0,a=a+Math.imul(de,Pe)|0,a=a+Math.imul(he,Ce)|0,_=_+Math.imul(he,Pe)|0,w=w+Math.imul(fe,Le)|0,a=a+Math.imul(fe,Te)|0,a=a+Math.imul(ce,Le)|0,_=_+Math.imul(ce,Te)|0,w=w+Math.imul(le,xe)|0,a=a+Math.imul(le,De)|0,a=a+Math.imul(ue,xe)|0,_=_+Math.imul(ue,De)|0,w=w+Math.imul(se,Oe)|0,a=a+Math.imul(se,Ie)|0,a=a+Math.imul(ae,Oe)|0,_=_+Math.imul(ae,Ie)|0,w=w+Math.imul(ee,Re)|0,a=a+Math.imul(ee,Be)|0,a=a+Math.imul(Q,Re)|0,_=_+Math.imul(Q,Be)|0,w=w+Math.imul(J,Ne)|0,a=a+Math.imul(J,Ee)|0,a=a+Math.imul(re,Ne)|0,_=_+Math.imul(re,Ee)|0;var nr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(nr>>>26)|0,nr&=67108863,w=Math.imul(me,Ae),a=Math.imul(me,Se),a=a+Math.imul(ve,Ae)|0,_=Math.imul(ve,Se),w=w+Math.imul(pe,Ce)|0,a=a+Math.imul(pe,Pe)|0,a=a+Math.imul(ge,Ce)|0,_=_+Math.imul(ge,Pe)|0,w=w+Math.imul(de,Le)|0,a=a+Math.imul(de,Te)|0,a=a+Math.imul(he,Le)|0,_=_+Math.imul(he,Te)|0,w=w+Math.imul(fe,xe)|0,a=a+Math.imul(fe,De)|0,a=a+Math.imul(ce,xe)|0,_=_+Math.imul(ce,De)|0,w=w+Math.imul(le,Oe)|0,a=a+Math.imul(le,Ie)|0,a=a+Math.imul(ue,Oe)|0,_=_+Math.imul(ue,Ie)|0,w=w+Math.imul(se,Re)|0,a=a+Math.imul(se,Be)|0,a=a+Math.imul(ae,Re)|0,_=_+Math.imul(ae,Be)|0,w=w+Math.imul(ee,Ne)|0,a=a+Math.imul(ee,Ee)|0,a=a+Math.imul(Q,Ne)|0,_=_+Math.imul(Q,Ee)|0;var or=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(or>>>26)|0,or&=67108863,w=Math.imul(me,Ce),a=Math.imul(me,Pe),a=a+Math.imul(ve,Ce)|0,_=Math.imul(ve,Pe),w=w+Math.imul(pe,Le)|0,a=a+Math.imul(pe,Te)|0,a=a+Math.imul(ge,Le)|0,_=_+Math.imul(ge,Te)|0,w=w+Math.imul(de,xe)|0,a=a+Math.imul(de,De)|0,a=a+Math.imul(he,xe)|0,_=_+Math.imul(he,De)|0,w=w+Math.imul(fe,Oe)|0,a=a+Math.imul(fe,Ie)|0,a=a+Math.imul(ce,Oe)|0,_=_+Math.imul(ce,Ie)|0,w=w+Math.imul(le,Re)|0,a=a+Math.imul(le,Be)|0,a=a+Math.imul(ue,Re)|0,_=_+Math.imul(ue,Be)|0,w=w+Math.imul(se,Ne)|0,a=a+Math.imul(se,Ee)|0,a=a+Math.imul(ae,Ne)|0,_=_+Math.imul(ae,Ee)|0;var sr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(sr>>>26)|0,sr&=67108863,w=Math.imul(me,Le),a=Math.imul(me,Te),a=a+Math.imul(ve,Le)|0,_=Math.imul(ve,Te),w=w+Math.imul(pe,xe)|0,a=a+Math.imul(pe,De)|0,a=a+Math.imul(ge,xe)|0,_=_+Math.imul(ge,De)|0,w=w+Math.imul(de,Oe)|0,a=a+Math.imul(de,Ie)|0,a=a+Math.imul(he,Oe)|0,_=_+Math.imul(he,Ie)|0,w=w+Math.imul(fe,Re)|0,a=a+Math.imul(fe,Be)|0,a=a+Math.imul(ce,Re)|0,_=_+Math.imul(ce,Be)|0,w=w+Math.imul(le,Ne)|0,a=a+Math.imul(le,Ee)|0,a=a+Math.imul(ue,Ne)|0,_=_+Math.imul(ue,Ee)|0;var ar=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(ar>>>26)|0,ar&=67108863,w=Math.imul(me,xe),a=Math.imul(me,De),a=a+Math.imul(ve,xe)|0,_=Math.imul(ve,De),w=w+Math.imul(pe,Oe)|0,a=a+Math.imul(pe,Ie)|0,a=a+Math.imul(ge,Oe)|0,_=_+Math.imul(ge,Ie)|0,w=w+Math.imul(de,Re)|0,a=a+Math.imul(de,Be)|0,a=a+Math.imul(he,Re)|0,_=_+Math.imul(he,Be)|0,w=w+Math.imul(fe,Ne)|0,a=a+Math.imul(fe,Ee)|0,a=a+Math.imul(ce,Ne)|0,_=_+Math.imul(ce,Ee)|0;var lr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(lr>>>26)|0,lr&=67108863,w=Math.imul(me,Oe),a=Math.imul(me,Ie),a=a+Math.imul(ve,Oe)|0,_=Math.imul(ve,Ie),w=w+Math.imul(pe,Re)|0,a=a+Math.imul(pe,Be)|0,a=a+Math.imul(ge,Re)|0,_=_+Math.imul(ge,Be)|0,w=w+Math.imul(de,Ne)|0,a=a+Math.imul(de,Ee)|0,a=a+Math.imul(he,Ne)|0,_=_+Math.imul(he,Ee)|0;var ur=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(ur>>>26)|0,ur&=67108863,w=Math.imul(me,Re),a=Math.imul(me,Be),a=a+Math.imul(ve,Re)|0,_=Math.imul(ve,Be),w=w+Math.imul(pe,Ne)|0,a=a+Math.imul(pe,Ee)|0,a=a+Math.imul(ge,Ne)|0,_=_+Math.imul(ge,Ee)|0;var fr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(fr>>>26)|0,fr&=67108863,w=Math.imul(me,Ne),a=Math.imul(me,Ee),a=a+Math.imul(ve,Ne)|0,_=Math.imul(ve,Ee);var cr=(k+w|0)+((a&8191)<<13)|0;return k=(_+(a>>>13)|0)+(cr>>>26)|0,cr&=67108863,v[0]=Gt,v[1]=Ht,v[2]=Vt,v[3]=Qt,v[4]=Xt,v[5]=Jt,v[6]=Yt,v[7]=er,v[8]=tr,v[9]=rr,v[10]=ir,v[11]=nr,v[12]=or,v[13]=sr,v[14]=ar,v[15]=lr,v[16]=ur,v[17]=fr,v[18]=cr,k!==0&&(v[19]=k,l.length++),l};Math.imul||(R=B);function x(h,e,s){s.negative=e.negative^h.negative,s.length=h.length+e.length;for(var l=0,f=0,d=0;d<s.length-1;d++){var v=f;f=0;for(var k=l&67108863,w=Math.min(d,e.length-1),a=Math.max(0,d-h.length+1);a<=w;a++){var _=d-a,F=h.words[_]|0,E=e.words[a]|0,U=F*E,Z=U&67108863;v=v+(U/67108864|0)|0,Z=Z+k|0,k=Z&67108863,v=v+(Z>>>26)|0,f+=v>>>26,v&=67108863}s.words[d]=k,l=v,v=f}return l!==0?s.words[d]=l:s.length--,s._strip()}function q(h,e,s){return x(h,e,s)}t.prototype.mulTo=function(e,s){var l,f=this.length+e.length;return this.length===10&&e.length===10?l=R(this,e,s):f<63?l=B(this,e,s):f<1024?l=x(this,e,s):l=q(this,e,s),l};function j(h,e){this.x=h,this.y=e}j.prototype.makeRBT=function(e){for(var s=new Array(e),l=t.prototype._countBits(e)-1,f=0;f<e;f++)s[f]=this.revBin(f,l,e);return s},j.prototype.revBin=function(e,s,l){if(e===0||e===l-1)return e;for(var f=0,d=0;d<s;d++)f|=(e&1)<<s-d-1,e>>=1;return f},j.prototype.permute=function(e,s,l,f,d,v){for(var k=0;k<v;k++)f[k]=s[e[k]],d[k]=l[e[k]]},j.prototype.transform=function(e,s,l,f,d,v){this.permute(v,e,s,l,f,d);for(var k=1;k<d;k<<=1)for(var w=k<<1,a=Math.cos(2*Math.PI/w),_=Math.sin(2*Math.PI/w),F=0;F<d;F+=w)for(var E=a,U=_,Z=0;Z<k;Z++){var G=l[F+Z],te=f[F+Z],Ze=l[F+Z+k],J=f[F+Z+k],re=E*Ze-U*J;J=E*J+U*Ze,Ze=re,l[F+Z]=G+Ze,f[F+Z]=te+J,l[F+Z+k]=G-Ze,f[F+Z+k]=te-J,Z!==w&&(re=a*E-_*U,U=a*U+_*E,E=re)}},j.prototype.guessLen13b=function(e,s){var l=Math.max(s,e)|1,f=l&1,d=0;for(l=l/2|0;l;l=l>>>1)d++;return 1<<d+1+f},j.prototype.conjugate=function(e,s,l){if(!(l<=1))for(var f=0;f<l/2;f++){var d=e[f];e[f]=e[l-f-1],e[l-f-1]=d,d=s[f],s[f]=-s[l-f-1],s[l-f-1]=-d}},j.prototype.normalize13b=function(e,s){for(var l=0,f=0;f<s/2;f++){var d=Math.round(e[2*f+1]/s)*8192+Math.round(e[2*f]/s)+l;e[f]=d&67108863,d<67108864?l=0:l=d/67108864|0}return e},j.prototype.convert13b=function(e,s,l,f){for(var d=0,v=0;v<s;v++)d=d+(e[v]|0),l[2*v]=d&8191,d=d>>>13,l[2*v+1]=d&8191,d=d>>>13;for(v=2*s;v<f;++v)l[v]=0;r(d===0),r((d&-8192)===0)},j.prototype.stub=function(e){for(var s=new Array(e),l=0;l<e;l++)s[l]=0;return s},j.prototype.mulp=function(e,s,l){var f=2*this.guessLen13b(e.length,s.length),d=this.makeRBT(f),v=this.stub(f),k=new Array(f),w=new Array(f),a=new Array(f),_=new Array(f),F=new Array(f),E=new Array(f),U=l.words;U.length=f,this.convert13b(e.words,e.length,k,f),this.convert13b(s.words,s.length,_,f),this.transform(k,v,w,a,f,d),this.transform(_,v,F,E,f,d);for(var Z=0;Z<f;Z++){var G=w[Z]*F[Z]-a[Z]*E[Z];a[Z]=w[Z]*E[Z]+a[Z]*F[Z],w[Z]=G}return this.conjugate(w,a,f),this.transform(w,a,U,v,f,d),this.conjugate(U,v,f),this.normalize13b(U,f),l.negative=e.negative^s.negative,l.length=e.length+s.length,l._strip()},t.prototype.mul=function(e){var s=new t(null);return s.words=new Array(this.length+e.length),this.mulTo(e,s)},t.prototype.mulf=function(e){var s=new t(null);return s.words=new Array(this.length+e.length),q(this,e,s)},t.prototype.imul=function(e){return this.clone().mulTo(e,this)},t.prototype.imuln=function(e){var s=e<0;s&&(e=-e),r(typeof e=="number"),r(e<67108864);for(var l=0,f=0;f<this.length;f++){var d=(this.words[f]|0)*e,v=(d&67108863)+(l&67108863);l>>=26,l+=d/67108864|0,l+=v>>>26,this.words[f]=v&67108863}return l!==0&&(this.words[f]=l,this.length++),this.length=e===0?1:this.length,s?this.ineg():this},t.prototype.muln=function(e){return this.clone().imuln(e)},t.prototype.sqr=function(){return this.mul(this)},t.prototype.isqr=function(){return this.imul(this.clone())},t.prototype.pow=function(e){var s=O(e);if(s.length===0)return new t(1);for(var l=this,f=0;f<s.length&&s[f]===0;f++,l=l.sqr());if(++f<s.length)for(var d=l.sqr();f<s.length;f++,d=d.sqr())s[f]!==0&&(l=l.mul(d));return l},t.prototype.iushln=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26,f=67108863>>>26-s<<26-s,d;if(s!==0){var v=0;for(d=0;d<this.length;d++){var k=this.words[d]&f,w=(this.words[d]|0)-k<<s;this.words[d]=w|v,v=k>>>26-s}v&&(this.words[d]=v,this.length++)}if(l!==0){for(d=this.length-1;d>=0;d--)this.words[d+l]=this.words[d];for(d=0;d<l;d++)this.words[d]=0;this.length+=l}return this._strip()},t.prototype.ishln=function(e){return r(this.negative===0),this.iushln(e)},t.prototype.iushrn=function(e,s,l){r(typeof e=="number"&&e>=0);var f;s?f=(s-s%26)/26:f=0;var d=e%26,v=Math.min((e-d)/26,this.length),k=67108863^67108863>>>d<<d,w=l;if(f-=v,f=Math.max(0,f),w){for(var a=0;a<v;a++)w.words[a]=this.words[a];w.length=v}if(v!==0)if(this.length>v)for(this.length-=v,a=0;a<this.length;a++)this.words[a]=this.words[a+v];else this.words[0]=0,this.length=1;var _=0;for(a=this.length-1;a>=0&&(_!==0||a>=f);a--){var F=this.words[a]|0;this.words[a]=_<<26-d|F>>>d,_=F&k}return w&&_!==0&&(w.words[w.length++]=_),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},t.prototype.ishrn=function(e,s,l){return r(this.negative===0),this.iushrn(e,s,l)},t.prototype.shln=function(e){return this.clone().ishln(e)},t.prototype.ushln=function(e){return this.clone().iushln(e)},t.prototype.shrn=function(e){return this.clone().ishrn(e)},t.prototype.ushrn=function(e){return this.clone().iushrn(e)},t.prototype.testn=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26,f=1<<s;if(this.length<=l)return!1;var d=this.words[l];return!!(d&f)},t.prototype.imaskn=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26;if(r(this.negative===0,"imaskn works only with positive numbers"),this.length<=l)return this;if(s!==0&&l++,this.length=Math.min(l,this.length),s!==0){var f=67108863^67108863>>>s<<s;this.words[this.length-1]&=f}return this._strip()},t.prototype.maskn=function(e){return this.clone().imaskn(e)},t.prototype.iaddn=function(e){return r(typeof e=="number"),r(e<67108864),e<0?this.isubn(-e):this.negative!==0?this.length===1&&(this.words[0]|0)<=e?(this.words[0]=e-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(e),this.negative=1,this):this._iaddn(e)},t.prototype._iaddn=function(e){this.words[0]+=e;for(var s=0;s<this.length&&this.words[s]>=67108864;s++)this.words[s]-=67108864,s===this.length-1?this.words[s+1]=1:this.words[s+1]++;return this.length=Math.max(this.length,s+1),this},t.prototype.isubn=function(e){if(r(typeof e=="number"),r(e<67108864),e<0)return this.iaddn(-e);if(this.negative!==0)return this.negative=0,this.iaddn(e),this.negative=1,this;if(this.words[0]-=e,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var s=0;s<this.length&&this.words[s]<0;s++)this.words[s]+=67108864,this.words[s+1]-=1;return this._strip()},t.prototype.addn=function(e){return this.clone().iaddn(e)},t.prototype.subn=function(e){return this.clone().isubn(e)},t.prototype.iabs=function(){return this.negative=0,this},t.prototype.abs=function(){return this.clone().iabs()},t.prototype._ishlnsubmul=function(e,s,l){var f=e.length+l,d;this._expand(f);var v,k=0;for(d=0;d<e.length;d++){v=(this.words[d+l]|0)+k;var w=(e.words[d]|0)*s;v-=w&67108863,k=(v>>26)-(w/67108864|0),this.words[d+l]=v&67108863}for(;d<this.length-l;d++)v=(this.words[d+l]|0)+k,k=v>>26,this.words[d+l]=v&67108863;if(k===0)return this._strip();for(r(k===-1),k=0,d=0;d<this.length;d++)v=-(this.words[d]|0)+k,k=v>>26,this.words[d]=v&67108863;return this.negative=1,this._strip()},t.prototype._wordDiv=function(e,s){var l=this.length-e.length,f=this.clone(),d=e,v=d.words[d.length-1]|0,k=this._countBits(v);l=26-k,l!==0&&(d=d.ushln(l),f.iushln(l),v=d.words[d.length-1]|0);var w=f.length-d.length,a;if(s!=="mod"){a=new t(null),a.length=w+1,a.words=new Array(a.length);for(var _=0;_<a.length;_++)a.words[_]=0}var F=f.clone()._ishlnsubmul(d,1,w);F.negative===0&&(f=F,a&&(a.words[w]=1));for(var E=w-1;E>=0;E--){var U=(f.words[d.length+E]|0)*67108864+(f.words[d.length+E-1]|0);for(U=Math.min(U/v|0,67108863),f._ishlnsubmul(d,U,E);f.negative!==0;)U--,f.negative=0,f._ishlnsubmul(d,1,E),f.isZero()||(f.negative^=1);a&&(a.words[E]=U)}return a&&a._strip(),f._strip(),s!=="div"&&l!==0&&f.iushrn(l),{div:a||null,mod:f}},t.prototype.divmod=function(e,s,l){if(r(!e.isZero()),this.isZero())return{div:new t(0),mod:new t(0)};var f,d,v;return this.negative!==0&&e.negative===0?(v=this.neg().divmod(e,s),s!=="mod"&&(f=v.div.neg()),s!=="div"&&(d=v.mod.neg(),l&&d.negative!==0&&d.iadd(e)),{div:f,mod:d}):this.negative===0&&e.negative!==0?(v=this.divmod(e.neg(),s),s!=="mod"&&(f=v.div.neg()),{div:f,mod:v.mod}):(this.negative&e.negative)!==0?(v=this.neg().divmod(e.neg(),s),s!=="div"&&(d=v.mod.neg(),l&&d.negative!==0&&d.isub(e)),{div:v.div,mod:d}):e.length>this.length||this.cmp(e)<0?{div:new t(0),mod:this}:e.length===1?s==="div"?{div:this.divn(e.words[0]),mod:null}:s==="mod"?{div:null,mod:new t(this.modrn(e.words[0]))}:{div:this.divn(e.words[0]),mod:new t(this.modrn(e.words[0]))}:this._wordDiv(e,s)},t.prototype.div=function(e){return this.divmod(e,"div",!1).div},t.prototype.mod=function(e){return this.divmod(e,"mod",!1).mod},t.prototype.umod=function(e){return this.divmod(e,"mod",!0).mod},t.prototype.divRound=function(e){var s=this.divmod(e);if(s.mod.isZero())return s.div;var l=s.div.negative!==0?s.mod.isub(e):s.mod,f=e.ushrn(1),d=e.andln(1),v=l.cmp(f);return v<0||d===1&&v===0?s.div:s.div.negative!==0?s.div.isubn(1):s.div.iaddn(1)},t.prototype.modrn=function(e){var s=e<0;s&&(e=-e),r(e<=67108863);for(var l=(1<<26)%e,f=0,d=this.length-1;d>=0;d--)f=(l*f+(this.words[d]|0))%e;return s?-f:f},t.prototype.modn=function(e){return this.modrn(e)},t.prototype.idivn=function(e){var s=e<0;s&&(e=-e),r(e<=67108863);for(var l=0,f=this.length-1;f>=0;f--){var d=(this.words[f]|0)+l*67108864;this.words[f]=d/e|0,l=d%e}return this._strip(),s?this.ineg():this},t.prototype.divn=function(e){return this.clone().idivn(e)},t.prototype.egcd=function(e){r(e.negative===0),r(!e.isZero());var s=this,l=e.clone();s.negative!==0?s=s.umod(e):s=s.clone();for(var f=new t(1),d=new t(0),v=new t(0),k=new t(1),w=0;s.isEven()&&l.isEven();)s.iushrn(1),l.iushrn(1),++w;for(var a=l.clone(),_=s.clone();!s.isZero();){for(var F=0,E=1;(s.words[0]&E)===0&&F<26;++F,E<<=1);if(F>0)for(s.iushrn(F);F-- >0;)(f.isOdd()||d.isOdd())&&(f.iadd(a),d.isub(_)),f.iushrn(1),d.iushrn(1);for(var U=0,Z=1;(l.words[0]&Z)===0&&U<26;++U,Z<<=1);if(U>0)for(l.iushrn(U);U-- >0;)(v.isOdd()||k.isOdd())&&(v.iadd(a),k.isub(_)),v.iushrn(1),k.iushrn(1);s.cmp(l)>=0?(s.isub(l),f.isub(v),d.isub(k)):(l.isub(s),v.isub(f),k.isub(d))}return{a:v,b:k,gcd:l.iushln(w)}},t.prototype._invmp=function(e){r(e.negative===0),r(!e.isZero());var s=this,l=e.clone();s.negative!==0?s=s.umod(e):s=s.clone();for(var f=new t(1),d=new t(0),v=l.clone();s.cmpn(1)>0&&l.cmpn(1)>0;){for(var k=0,w=1;(s.words[0]&w)===0&&k<26;++k,w<<=1);if(k>0)for(s.iushrn(k);k-- >0;)f.isOdd()&&f.iadd(v),f.iushrn(1);for(var a=0,_=1;(l.words[0]&_)===0&&a<26;++a,_<<=1);if(a>0)for(l.iushrn(a);a-- >0;)d.isOdd()&&d.iadd(v),d.iushrn(1);s.cmp(l)>=0?(s.isub(l),f.isub(d)):(l.isub(s),d.isub(f))}var F;return s.cmpn(1)===0?F=f:F=d,F.cmpn(0)<0&&F.iadd(e),F},t.prototype.gcd=function(e){if(this.isZero())return e.abs();if(e.isZero())return this.abs();var s=this.clone(),l=e.clone();s.negative=0,l.negative=0;for(var f=0;s.isEven()&&l.isEven();f++)s.iushrn(1),l.iushrn(1);do{for(;s.isEven();)s.iushrn(1);for(;l.isEven();)l.iushrn(1);var d=s.cmp(l);if(d<0){var v=s;s=l,l=v}else if(d===0||l.cmpn(1)===0)break;s.isub(l)}while(!0);return l.iushln(f)},t.prototype.invm=function(e){return this.egcd(e).a.umod(e)},t.prototype.isEven=function(){return(this.words[0]&1)===0},t.prototype.isOdd=function(){return(this.words[0]&1)===1},t.prototype.andln=function(e){return this.words[0]&e},t.prototype.bincn=function(e){r(typeof e=="number");var s=e%26,l=(e-s)/26,f=1<<s;if(this.length<=l)return this._expand(l+1),this.words[l]|=f,this;for(var d=f,v=l;d!==0&&v<this.length;v++){var k=this.words[v]|0;k+=d,d=k>>>26,k&=67108863,this.words[v]=k}return d!==0&&(this.words[v]=d,this.length++),this},t.prototype.isZero=function(){return this.length===1&&this.words[0]===0},t.prototype.cmpn=function(e){var s=e<0;if(this.negative!==0&&!s)return-1;if(this.negative===0&&s)return 1;this._strip();var l;if(this.length>1)l=1;else{s&&(e=-e),r(e<=67108863,"Number is too big");var f=this.words[0]|0;l=f===e?0:f<e?-1:1}return this.negative!==0?-l|0:l},t.prototype.cmp=function(e){if(this.negative!==0&&e.negative===0)return-1;if(this.negative===0&&e.negative!==0)return 1;var s=this.ucmp(e);return this.negative!==0?-s|0:s},t.prototype.ucmp=function(e){if(this.length>e.length)return 1;if(this.length<e.length)return-1;for(var s=0,l=this.length-1;l>=0;l--){var f=this.words[l]|0,d=e.words[l]|0;if(f!==d){f<d?s=-1:f>d&&(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 P(e)},t.prototype.toRed=function(e){return r(!this.red,"Already a number in reduction context"),r(this.negative===0,"red works only with positives"),e.convertTo(this)._forceRed(e)},t.prototype.fromRed=function(){return r(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},t.prototype._forceRed=function(e){return this.red=e,this},t.prototype.forceRed=function(e){return r(!this.red,"Already a number in reduction context"),this._forceRed(e)},t.prototype.redAdd=function(e){return r(this.red,"redAdd works only with red numbers"),this.red.add(this,e)},t.prototype.redIAdd=function(e){return r(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,e)},t.prototype.redSub=function(e){return r(this.red,"redSub works only with red numbers"),this.red.sub(this,e)},t.prototype.redISub=function(e){return r(this.red,"redISub works only with red numbers"),this.red.isub(this,e)},t.prototype.redShl=function(e){return r(this.red,"redShl works only with red numbers"),this.red.shl(this,e)},t.prototype.redMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.mul(this,e)},t.prototype.redIMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.imul(this,e)},t.prototype.redSqr=function(){return r(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},t.prototype.redISqr=function(){return r(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},t.prototype.redSqrt=function(){return r(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},t.prototype.redInvm=function(){return r(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},t.prototype.redNeg=function(){return r(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},t.prototype.redPow=function(e){return r(this.red&&!e.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,e)};var W={k256:null,p224:null,p192:null,p25519:null};function $(h,e){this.name=h,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()}$.prototype._tmp=function(){var e=new t(null);return e.words=new Array(Math.ceil(this.n/13)),e},$.prototype.ireduce=function(e){var s=e,l;do this.split(s,this.tmp),s=this.imulK(s),s=s.iadd(this.tmp),l=s.bitLength();while(l>this.n);var f=l<this.n?-1:s.ucmp(this.p);return f===0?(s.words[0]=0,s.length=1):f>0?s.isub(this.p):s.strip!==void 0?s.strip():s._strip(),s},$.prototype.split=function(e,s){e.iushrn(this.n,0,s)},$.prototype.imulK=function(e){return e.imul(this.k)};function z(){$.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}o(z,$),z.prototype.split=function(e,s){for(var l=4194303,f=Math.min(e.length,9),d=0;d<f;d++)s.words[d]=e.words[d];if(s.length=f,e.length<=9){e.words[0]=0,e.length=1;return}var v=e.words[9];for(s.words[s.length++]=v&l,d=10;d<e.length;d++){var k=e.words[d]|0;e.words[d-10]=(k&l)<<4|v>>>22,v=k}v>>>=22,e.words[d-10]=v,v===0&&e.length>10?e.length-=10:e.length-=9},z.prototype.imulK=function(e){e.words[e.length]=0,e.words[e.length+1]=0,e.length+=2;for(var s=0,l=0;l<e.length;l++){var f=e.words[l]|0;s+=f*977,e.words[l]=s&67108863,s=f*64+(s/67108864|0)}return e.words[e.length-1]===0&&(e.length--,e.words[e.length-1]===0&&e.length--),e};function m(){$.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}o(m,$);function y(){$.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}o(y,$);function A(){$.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}o(A,$),A.prototype.imulK=function(e){for(var s=0,l=0;l<e.length;l++){var f=(e.words[l]|0)*19+s,d=f&67108863;f>>>=26,e.words[l]=d,s=f}return s!==0&&(e.words[e.length++]=s),e},t._prime=function(e){if(W[e])return W[e];var s;if(e==="k256")s=new z;else if(e==="p224")s=new m;else if(e==="p192")s=new y;else if(e==="p25519")s=new A;else throw new Error("Unknown prime "+e);return W[e]=s,s};function P(h){if(typeof h=="string"){var e=t._prime(h);this.m=e.p,this.prime=e}else r(h.gtn(1),"modulus must be greater than 1"),this.m=h,this.prime=null}P.prototype._verify1=function(e){r(e.negative===0,"red works only with positives"),r(e.red,"red works only with red numbers")},P.prototype._verify2=function(e,s){r((e.negative|s.negative)===0,"red works only with positives"),r(e.red&&e.red===s.red,"red works only with red numbers")},P.prototype.imod=function(e){return this.prime?this.prime.ireduce(e)._forceRed(this):(M(e,e.umod(this.m)._forceRed(this)),e)},P.prototype.neg=function(e){return e.isZero()?e.clone():this.m.sub(e)._forceRed(this)},P.prototype.add=function(e,s){this._verify2(e,s);var l=e.add(s);return l.cmp(this.m)>=0&&l.isub(this.m),l._forceRed(this)},P.prototype.iadd=function(e,s){this._verify2(e,s);var l=e.iadd(s);return l.cmp(this.m)>=0&&l.isub(this.m),l},P.prototype.sub=function(e,s){this._verify2(e,s);var l=e.sub(s);return l.cmpn(0)<0&&l.iadd(this.m),l._forceRed(this)},P.prototype.isub=function(e,s){this._verify2(e,s);var l=e.isub(s);return l.cmpn(0)<0&&l.iadd(this.m),l},P.prototype.shl=function(e,s){return this._verify1(e),this.imod(e.ushln(s))},P.prototype.imul=function(e,s){return this._verify2(e,s),this.imod(e.imul(s))},P.prototype.mul=function(e,s){return this._verify2(e,s),this.imod(e.mul(s))},P.prototype.isqr=function(e){return this.imul(e,e.clone())},P.prototype.sqr=function(e){return this.mul(e,e)},P.prototype.sqrt=function(e){if(e.isZero())return e.clone();var s=this.m.andln(3);if(r(s%2===1),s===3){var l=this.m.add(new t(1)).iushrn(2);return this.pow(e,l)}for(var f=this.m.subn(1),d=0;!f.isZero()&&f.andln(1)===0;)d++,f.iushrn(1);r(!f.isZero());var v=new t(1).toRed(this),k=v.redNeg(),w=this.m.subn(1).iushrn(1),a=this.m.bitLength();for(a=new t(2*a*a).toRed(this);this.pow(a,w).cmp(k)!==0;)a.redIAdd(k);for(var _=this.pow(a,f),F=this.pow(e,f.addn(1).iushrn(1)),E=this.pow(e,f),U=d;E.cmp(v)!==0;){for(var Z=E,G=0;Z.cmp(v)!==0;G++)Z=Z.redSqr();r(G<U);var te=this.pow(_,new t(1).iushln(U-G-1));F=F.redMul(te),_=te.redSqr(),E=E.redMul(_),U=G}return F},P.prototype.invm=function(e){var s=e._invmp(this.m);return s.negative!==0?(s.negative=0,this.imod(s).redNeg()):this.imod(s)},P.prototype.pow=function(e,s){if(s.isZero())return new t(1).toRed(this);if(s.cmpn(1)===0)return e.clone();var l=4,f=new Array(1<<l);f[0]=new t(1).toRed(this),f[1]=e;for(var d=2;d<f.length;d++)f[d]=this.mul(f[d-1],e);var v=f[0],k=0,w=0,a=s.bitLength()%26;for(a===0&&(a=26),d=s.length-1;d>=0;d--){for(var _=s.words[d],F=a-1;F>=0;F--){var E=_>>F&1;if(v!==f[0]&&(v=this.sqr(v)),E===0&&k===0){w=0;continue}k<<=1,k|=E,w++,!(w!==l&&(d!==0||F!==0))&&(v=this.mul(v,f[k]),w=0,k=0)}a=26}return v},P.prototype.convertTo=function(e){var s=e.umod(this.m);return s===e?s.clone():s},P.prototype.convertFrom=function(e){var s=e.clone();return s.red=null,s},t.mont=function(e){return new L(e)};function L(h){P.call(this,h),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(L,P),L.prototype.convertTo=function(e){return this.imod(e.ushln(this.shift))},L.prototype.convertFrom=function(e){var s=this.imod(e.mul(this.rinv));return s.red=null,s},L.prototype.imul=function(e,s){if(e.isZero()||s.isZero())return e.words[0]=0,e.length=1,e;var l=e.imul(s),f=l.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),d=l.isub(f).iushrn(this.shift),v=d;return d.cmp(this.m)>=0?v=d.isub(this.m):d.cmpn(0)<0&&(v=d.iadd(this.m)),v._forceRed(this)},L.prototype.mul=function(e,s){if(e.isZero()||s.isZero())return new t(0)._forceRed(this);var l=e.mul(s),f=l.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),d=l.isub(f).iushrn(this.shift),v=d;return d.cmp(this.m)>=0?v=d.isub(this.m):d.cmpn(0)<0&&(v=d.iadd(this.m)),v._forceRed(this)},L.prototype.invm=function(e){var s=this.imod(e._invmp(this.m).mul(this.r2));return s._forceRed(this)}})(typeof Cr>"u"||Cr,wi)});import{SdkWrapper as lo}from"@cetusprotocol/common-sdk";import{DefaultProviders as Fi,FullRpcUrlMainnet as Ui}from"@cetusprotocol/common-sdk";var Vr={full_rpc_url:Ui,env:"mainnet",aggregator_url:"https://api-sui.cetus.zone/router_v3",providers:Fi,leverage:{package_id:"0xc41dca0f7de9e155862521e4d73386e2a45049c5c3c3fa6033525473fa6c634d",published_at:"0xc41dca0f7de9e155862521e4d73386e2a45049c5c3c3fa6033525473fa6c634d",version:0,config:{versioned:"0x16e25bbbb9b06db1ae9c3ebcb381f670ee3ca3b2647981e9cc0afbbfadb0e9e5",admin_cap_id:"0x5056062528bfac02d34f25b623ed6e34b4cd36fdaf6608e4bd593a13207f9b59",global_config_id:"0xeb7d17f241a94c8f9c7f6f363926d63a0bc608616adb409ab1e3abbc3e4d8ea0",markets:"0x2ed42decf1d64d0eb4af233e827a09c8d653d7fb1a40295cf818022abbc26914",markets_table_id:"0xb4aad4cb3690a5fc2033422fce919c9b8eb3c90102e9501b01a9fcf9dc7ea162"}},suilend:{package_id:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf",published_at:"0xe37cc7bb50fd9b6dbd3873df66fa2c554e973697f50ef97707311dc78bd08444",config:{lendingMarket:[{name:"Main market",slug:"main",id:"0x84030d26d85eaa7035084a057f2f11f701b7e2e4eda87551becbc7c97505ece1",type:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf::suilend::MAIN_POOL",ownerCapId:"0xf7a4defe0b6566b6a2674a02a0c61c9f99bd012eed21bc741a069eaa82d35927"},{name:"STEAMM LM",slug:"steamm-lm",id:"0xc1888ec1b81a414e427a44829310508352aec38252ee0daa9f8b181b6947de9f",type:"0x0a071f4976abae1a7f722199cf0bfcbe695ef9408a878e7d12a7ca87b7e582a6::lp_rewards::LP_REWARDS",ownerCapId:"0x55a0f33b24e091830302726c8cfbff8cf8abd2ec1f83a4e6f4bf51c7ba3ad5ab",isHidden:!0}],lendingMarketId:"0x84030d26d85eaa7035084a057f2f11f701b7e2e4eda87551becbc7c97505ece1",lendingMarketType:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf::suilend::MAIN_POOL",apiUrl:"https://d10td5ybgrf39v.cloudfront.net"}}};import{SuilendClient as Xi}from"@suilend/sdk/client";import{initializeSuilend as Ji,initializeSuilendRewards as Yi,initializeObligations as en,formatRewards as tn,Side as lt,getFilteredRewards as rn,getStakingYieldAprPercent as nn,getDedupedPerDayRewards as on,getDedupedAprRewards as sn,getTotalAprPercent as ri,getNetAprPercent as an}from"@suilend/sdk";var qi=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,dr=Math.ceil,Je=Math.floor,Ge="[BigNumber Error] ",Qr=Ge+"Number primitive has more than 15 significant digits: ",et=1e14,X=14,hr=9007199254740991,pr=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],at=1e7,qe=1e9;function Xr(n){var i,r,o,t=x.prototype={constructor:x,toString:null,valueOf:null},u=new x(1),p=20,c=4,g=-7,M=21,b=-1e7,S=1e7,C=!1,T=1,D=0,O={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:"\xA0",suffix:""},B="0123456789abcdefghijklmnopqrstuvwxyz",R=!0;function x(m,y){var A,P,L,h,e,s,l,f,d=this;if(!(d instanceof x))return new x(m,y);if(y==null){if(m&&m._isBigNumber===!0){d.s=m.s,!m.c||m.e>S?d.c=d.e=null:m.e<b?d.c=[d.e=0]:(d.e=m.e,d.c=m.c.slice());return}if((s=typeof m=="number")&&m*0==0){if(d.s=1/m<0?(m=-m,-1):1,m===~~m){for(h=0,e=m;e>=10;e/=10,h++);h>S?d.c=d.e=null:(d.e=h,d.c=[m]);return}f=String(m)}else{if(!qi.test(f=String(m)))return o(d,f,s);d.s=f.charCodeAt(0)==45?(f=f.slice(1),-1):1}(h=f.indexOf("."))>-1&&(f=f.replace(".","")),(e=f.search(/e/i))>0?(h<0&&(h=e),h+=+f.slice(e+1),f=f.substring(0,e)):h<0&&(h=f.length)}else{if(oe(y,2,B.length,"Base"),y==10&&R)return d=new x(m),$(d,p+d.e+1,c);if(f=String(m),s=typeof m=="number"){if(m*0!=0)return o(d,f,s,y);if(d.s=1/m<0?(f=f.slice(1),-1):1,x.DEBUG&&f.replace(/^0\.0*|\./,"").length>15)throw Error(Qr+m)}else d.s=f.charCodeAt(0)===45?(f=f.slice(1),-1):1;for(A=B.slice(0,y),h=e=0,l=f.length;e<l;e++)if(A.indexOf(P=f.charAt(e))<0){if(P=="."){if(e>h){h=l;continue}}else if(!L&&(f==f.toUpperCase()&&(f=f.toLowerCase())||f==f.toLowerCase()&&(f=f.toUpperCase()))){L=!0,e=-1,h=0;continue}return o(d,String(m),s,y)}s=!1,f=r(f,y,10,d.s),(h=f.indexOf("."))>-1?f=f.replace(".",""):h=f.length}for(e=0;f.charCodeAt(e)===48;e++);for(l=f.length;f.charCodeAt(--l)===48;);if(f=f.slice(e,++l)){if(l-=e,s&&x.DEBUG&&l>15&&(m>hr||m!==Je(m)))throw Error(Qr+d.s*m);if((h=h-e-1)>S)d.c=d.e=null;else if(h<b)d.c=[d.e=0];else{if(d.e=h,d.c=[],e=(h+1)%X,h<0&&(e+=X),e<l){for(e&&d.c.push(+f.slice(0,e)),l-=X;e<l;)d.c.push(+f.slice(e,e+=X));e=X-(f=f.slice(e)).length}else e-=l;for(;e--;f+="0");d.c.push(+f)}}else d.c=[d.e=0]}x.clone=Xr,x.ROUND_UP=0,x.ROUND_DOWN=1,x.ROUND_CEIL=2,x.ROUND_FLOOR=3,x.ROUND_HALF_UP=4,x.ROUND_HALF_DOWN=5,x.ROUND_HALF_EVEN=6,x.ROUND_HALF_CEIL=7,x.ROUND_HALF_FLOOR=8,x.EUCLID=9,x.config=x.set=function(m){var y,A;if(m!=null)if(typeof m=="object"){if(m.hasOwnProperty(y="DECIMAL_PLACES")&&(A=m[y],oe(A,0,qe,y),p=A),m.hasOwnProperty(y="ROUNDING_MODE")&&(A=m[y],oe(A,0,8,y),c=A),m.hasOwnProperty(y="EXPONENTIAL_AT")&&(A=m[y],A&&A.pop?(oe(A[0],-qe,0,y),oe(A[1],0,qe,y),g=A[0],M=A[1]):(oe(A,-qe,qe,y),g=-(M=A<0?-A:A))),m.hasOwnProperty(y="RANGE"))if(A=m[y],A&&A.pop)oe(A[0],-qe,-1,y),oe(A[1],1,qe,y),b=A[0],S=A[1];else if(oe(A,-qe,qe,y),A)b=-(S=A<0?-A:A);else throw Error(Ge+y+" cannot be zero: "+A);if(m.hasOwnProperty(y="CRYPTO"))if(A=m[y],A===!!A)if(A)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))C=A;else throw C=!A,Error(Ge+"crypto unavailable");else C=A;else throw Error(Ge+y+" not true or false: "+A);if(m.hasOwnProperty(y="MODULO_MODE")&&(A=m[y],oe(A,0,9,y),T=A),m.hasOwnProperty(y="POW_PRECISION")&&(A=m[y],oe(A,0,qe,y),D=A),m.hasOwnProperty(y="FORMAT"))if(A=m[y],typeof A=="object")O=A;else throw Error(Ge+y+" not an object: "+A);if(m.hasOwnProperty(y="ALPHABET"))if(A=m[y],typeof A=="string"&&!/^.?$|[+\-.\s]|(.).*\1/.test(A))R=A.slice(0,10)=="0123456789",B=A;else throw Error(Ge+y+" invalid: "+A)}else throw Error(Ge+"Object expected: "+m);return{DECIMAL_PLACES:p,ROUNDING_MODE:c,EXPONENTIAL_AT:[g,M],RANGE:[b,S],CRYPTO:C,MODULO_MODE:T,POW_PRECISION:D,FORMAT:O,ALPHABET:B}},x.isBigNumber=function(m){if(!m||m._isBigNumber!==!0)return!1;if(!x.DEBUG)return!0;var y,A,P=m.c,L=m.e,h=m.s;e:if({}.toString.call(P)=="[object Array]"){if((h===1||h===-1)&&L>=-qe&&L<=qe&&L===Je(L)){if(P[0]===0){if(L===0&&P.length===1)return!0;break e}if(y=(L+1)%X,y<1&&(y+=X),String(P[0]).length==y){for(y=0;y<P.length;y++)if(A=P[y],A<0||A>=et||A!==Je(A))break e;if(A!==0)return!0}}}else if(P===null&&L===null&&(h===null||h===1||h===-1))return!0;throw Error(Ge+"Invalid BigNumber: "+m)},x.maximum=x.max=function(){return j(arguments,-1)},x.minimum=x.min=function(){return j(arguments,1)},x.random=(function(){var m=9007199254740992,y=Math.random()*m&2097151?function(){return Je(Math.random()*m)}:function(){return(Math.random()*1073741824|0)*8388608+(Math.random()*8388608|0)};return function(A){var P,L,h,e,s,l=0,f=[],d=new x(u);if(A==null?A=p:oe(A,0,qe),e=dr(A/X),C)if(crypto.getRandomValues){for(P=crypto.getRandomValues(new Uint32Array(e*=2));l<e;)s=P[l]*131072+(P[l+1]>>>11),s>=9e15?(L=crypto.getRandomValues(new Uint32Array(2)),P[l]=L[0],P[l+1]=L[1]):(f.push(s%1e14),l+=2);l=e/2}else if(crypto.randomBytes){for(P=crypto.randomBytes(e*=7);l<e;)s=(P[l]&31)*281474976710656+P[l+1]*1099511627776+P[l+2]*4294967296+P[l+3]*16777216+(P[l+4]<<16)+(P[l+5]<<8)+P[l+6],s>=9e15?crypto.randomBytes(7).copy(P,l):(f.push(s%1e14),l+=7);l=e/7}else throw C=!1,Error(Ge+"crypto unavailable");if(!C)for(;l<e;)s=y(),s<9e15&&(f[l++]=s%1e14);for(e=f[--l],A%=X,e&&A&&(s=pr[X-A],f[l]=Je(e/s)*s);f[l]===0;f.pop(),l--);if(l<0)f=[h=0];else{for(h=-1;f[0]===0;f.splice(0,1),h-=X);for(l=1,s=f[0];s>=10;s/=10,l++);l<X&&(h-=X-l)}return d.e=h,d.c=f,d}})(),x.sum=function(){for(var m=1,y=arguments,A=new x(y[0]);m<y.length;)A=A.plus(y[m++]);return A},r=(function(){var m="0123456789";function y(A,P,L,h){for(var e,s=[0],l,f=0,d=A.length;f<d;){for(l=s.length;l--;s[l]*=P);for(s[0]+=h.indexOf(A.charAt(f++)),e=0;e<s.length;e++)s[e]>L-1&&(s[e+1]==null&&(s[e+1]=0),s[e+1]+=s[e]/L|0,s[e]%=L)}return s.reverse()}return function(A,P,L,h,e){var s,l,f,d,v,k,w,a,_=A.indexOf("."),F=p,E=c;for(_>=0&&(d=D,D=0,A=A.replace(".",""),a=new x(P),k=a.pow(A.length-_),D=d,a.c=y(ot(Xe(k.c),k.e,"0"),10,L,m),a.e=a.c.length),w=y(A,P,L,e?(s=B,m):(s=m,B)),f=d=w.length;w[--d]==0;w.pop());if(!w[0])return s.charAt(0);if(_<0?--f:(k.c=w,k.e=f,k.s=h,k=i(k,a,F,E,L),w=k.c,v=k.r,f=k.e),l=f+F+1,_=w[l],d=L/2,v=v||l<0||w[l+1]!=null,v=E<4?(_!=null||v)&&(E==0||E==(k.s<0?3:2)):_>d||_==d&&(E==4||v||E==6&&w[l-1]&1||E==(k.s<0?8:7)),l<1||!w[0])A=v?ot(s.charAt(1),-F,s.charAt(0)):s.charAt(0);else{if(w.length=l,v)for(--L;++w[--l]>L;)w[l]=0,l||(++f,w=[1].concat(w));for(d=w.length;!w[--d];);for(_=0,A="";_<=d;A+=s.charAt(w[_++]));A=ot(A,f,s.charAt(0))}return A}})(),i=(function(){function m(P,L,h){var e,s,l,f,d=0,v=P.length,k=L%at,w=L/at|0;for(P=P.slice();v--;)l=P[v]%at,f=P[v]/at|0,e=w*l+f*k,s=k*l+e%at*at+d,d=(s/h|0)+(e/at|0)+w*f,P[v]=s%h;return d&&(P=[d].concat(P)),P}function y(P,L,h,e){var s,l;if(h!=e)l=h>e?1:-1;else for(s=l=0;s<h;s++)if(P[s]!=L[s]){l=P[s]>L[s]?1:-1;break}return l}function A(P,L,h,e){for(var s=0;h--;)P[h]-=s,s=P[h]<L[h]?1:0,P[h]=s*e+P[h]-L[h];for(;!P[0]&&P.length>1;P.splice(0,1));}return function(P,L,h,e,s){var l,f,d,v,k,w,a,_,F,E,U,Z,G,te,Ze,J,re,Ke=P.s==L.s?1:-1,ee=P.c,Q=L.c;if(!ee||!ee[0]||!Q||!Q[0])return new x(!P.s||!L.s||(ee?Q&&ee[0]==Q[0]:!Q)?NaN:ee&&ee[0]==0||!Q?Ke*0:Ke/0);for(_=new x(Ke),F=_.c=[],f=P.e-L.e,Ke=h+f+1,s||(s=et,f=Ye(P.e/X)-Ye(L.e/X),Ke=Ke/X|0),d=0;Q[d]==(ee[d]||0);d++);if(Q[d]>(ee[d]||0)&&f--,Ke<0)F.push(1),v=!0;else{for(te=ee.length,J=Q.length,d=0,Ke+=2,k=Je(s/(Q[0]+1)),k>1&&(Q=m(Q,k,s),ee=m(ee,k,s),J=Q.length,te=ee.length),G=J,E=ee.slice(0,J),U=E.length;U<J;E[U++]=0);re=Q.slice(),re=[0].concat(re),Ze=Q[0],Q[1]>=s/2&&Ze++;do{if(k=0,l=y(Q,E,J,U),l<0){if(Z=E[0],J!=U&&(Z=Z*s+(E[1]||0)),k=Je(Z/Ze),k>1)for(k>=s&&(k=s-1),w=m(Q,k,s),a=w.length,U=E.length;y(w,E,a,U)==1;)k--,A(w,J<a?re:Q,a,s),a=w.length,l=1;else k==0&&(l=k=1),w=Q.slice(),a=w.length;if(a<U&&(w=[0].concat(w)),A(E,w,U,s),U=E.length,l==-1)for(;y(Q,E,J,U)<1;)k++,A(E,J<U?re:Q,U,s),U=E.length}else l===0&&(k++,E=[0]);F[d++]=k,E[0]?E[U++]=ee[G]||0:(E=[ee[G]],U=1)}while((G++<te||E[0]!=null)&&Ke--);v=E[0]!=null,F[0]||F.splice(0,1)}if(s==et){for(d=1,Ke=F[0];Ke>=10;Ke/=10,d++);$(_,h+(_.e=d+f*X-1)+1,e,v)}else _.e=f,_.r=+v;return _}})();function q(m,y,A,P){var L,h,e,s,l;if(A==null?A=c:oe(A,0,8),!m.c)return m.toString();if(L=m.c[0],e=m.e,y==null)l=Xe(m.c),l=P==1||P==2&&(e<=g||e>=M)?Lt(l,e):ot(l,e,"0");else if(m=$(new x(m),y,A),h=m.e,l=Xe(m.c),s=l.length,P==1||P==2&&(y<=h||h<=g)){for(;s<y;l+="0",s++);l=Lt(l,h)}else if(y-=e+(P===2&&h>e),l=ot(l,h,"0"),h+1>s){if(--y>0)for(l+=".";y--;l+="0");}else if(y+=h-s,y>0)for(h+1==s&&(l+=".");y--;l+="0");return m.s<0&&L?"-"+l:l}function j(m,y){for(var A,P,L=1,h=new x(m[0]);L<m.length;L++)P=new x(m[L]),(!P.s||(A=gt(h,P))===y||A===0&&h.s===y)&&(h=P);return h}function W(m,y,A){for(var P=1,L=y.length;!y[--L];y.pop());for(L=y[0];L>=10;L/=10,P++);return(A=P+A*X-1)>S?m.c=m.e=null:A<b?m.c=[m.e=0]:(m.e=A,m.c=y),m}o=(function(){var m=/^(-?)0([xbo])(?=\w[\w.]*$)/i,y=/^([^.]+)\.$/,A=/^\.([^.]+)$/,P=/^-?(Infinity|NaN)$/,L=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(h,e,s,l){var f,d=s?e:e.replace(L,"");if(P.test(d))h.s=isNaN(d)?null:d<0?-1:1;else{if(!s&&(d=d.replace(m,function(v,k,w){return f=(w=w.toLowerCase())=="x"?16:w=="b"?2:8,!l||l==f?k:v}),l&&(f=l,d=d.replace(y,"$1").replace(A,"0.$1")),e!=d))return new x(d,f);if(x.DEBUG)throw Error(Ge+"Not a"+(l?" base "+l:"")+" number: "+e);h.s=null}h.c=h.e=null}})();function $(m,y,A,P){var L,h,e,s,l,f,d,v=m.c,k=pr;if(v){e:{for(L=1,s=v[0];s>=10;s/=10,L++);if(h=y-L,h<0)h+=X,e=y,l=v[f=0],d=Je(l/k[L-e-1]%10);else if(f=dr((h+1)/X),f>=v.length)if(P){for(;v.length<=f;v.push(0));l=d=0,L=1,h%=X,e=h-X+1}else break e;else{for(l=s=v[f],L=1;s>=10;s/=10,L++);h%=X,e=h-X+L,d=e<0?0:Je(l/k[L-e-1]%10)}if(P=P||y<0||v[f+1]!=null||(e<0?l:l%k[L-e-1]),P=A<4?(d||P)&&(A==0||A==(m.s<0?3:2)):d>5||d==5&&(A==4||P||A==6&&(h>0?e>0?l/k[L-e]:0:v[f-1])%10&1||A==(m.s<0?8:7)),y<1||!v[0])return v.length=0,P?(y-=m.e+1,v[0]=k[(X-y%X)%X],m.e=-y||0):v[0]=m.e=0,m;if(h==0?(v.length=f,s=1,f--):(v.length=f+1,s=k[X-h],v[f]=e>0?Je(l/k[L-e]%k[e])*s:0),P)for(;;)if(f==0){for(h=1,e=v[0];e>=10;e/=10,h++);for(e=v[0]+=s,s=1;e>=10;e/=10,s++);h!=s&&(m.e++,v[0]==et&&(v[0]=1));break}else{if(v[f]+=s,v[f]!=et)break;v[f--]=0,s=1}for(h=v.length;v[--h]===0;v.pop());}m.e>S?m.c=m.e=null:m.e<b&&(m.c=[m.e=0])}return m}function z(m){var y,A=m.e;return A===null?m.toString():(y=Xe(m.c),y=A<=g||A>=M?Lt(y,A):ot(y,A,"0"),m.s<0?"-"+y:y)}return t.absoluteValue=t.abs=function(){var m=new x(this);return m.s<0&&(m.s=1),m},t.comparedTo=function(m,y){return gt(this,new x(m,y))},t.decimalPlaces=t.dp=function(m,y){var A,P,L,h=this;if(m!=null)return oe(m,0,qe),y==null?y=c:oe(y,0,8),$(new x(h),m+h.e+1,y);if(!(A=h.c))return null;if(P=((L=A.length-1)-Ye(this.e/X))*X,L=A[L])for(;L%10==0;L/=10,P--);return P<0&&(P=0),P},t.dividedBy=t.div=function(m,y){return i(this,new x(m,y),p,c)},t.dividedToIntegerBy=t.idiv=function(m,y){return i(this,new x(m,y),0,1)},t.exponentiatedBy=t.pow=function(m,y){var A,P,L,h,e,s,l,f,d,v=this;if(m=new x(m),m.c&&!m.isInteger())throw Error(Ge+"Exponent not an integer: "+z(m));if(y!=null&&(y=new x(y)),s=m.e>14,!v.c||!v.c[0]||v.c[0]==1&&!v.e&&v.c.length==1||!m.c||!m.c[0])return d=new x(Math.pow(+z(v),s?m.s*(2-Pt(m)):+z(m))),y?d.mod(y):d;if(l=m.s<0,y){if(y.c?!y.c[0]:!y.s)return new x(NaN);P=!l&&v.isInteger()&&y.isInteger(),P&&(v=v.mod(y))}else{if(m.e>9&&(v.e>0||v.e<-1||(v.e==0?v.c[0]>1||s&&v.c[1]>=24e7:v.c[0]<8e13||s&&v.c[0]<=9999975e7)))return h=v.s<0&&Pt(m)?-0:0,v.e>-1&&(h=1/h),new x(l?1/h:h);D&&(h=dr(D/X+2))}for(s?(A=new x(.5),l&&(m.s=1),f=Pt(m)):(L=Math.abs(+z(m)),f=L%2),d=new x(u);;){if(f){if(d=d.times(v),!d.c)break;h?d.c.length>h&&(d.c.length=h):P&&(d=d.mod(y))}if(L){if(L=Je(L/2),L===0)break;f=L%2}else if(m=m.times(A),$(m,m.e+1,1),m.e>14)f=Pt(m);else{if(L=+z(m),L===0)break;f=L%2}v=v.times(v),h?v.c&&v.c.length>h&&(v.c.length=h):P&&(v=v.mod(y))}return P?d:(l&&(d=u.div(d)),y?d.mod(y):h?$(d,D,c,e):d)},t.integerValue=function(m){var y=new x(this);return m==null?m=c:oe(m,0,8),$(y,y.e+1,m)},t.isEqualTo=t.eq=function(m,y){return gt(this,new x(m,y))===0},t.isFinite=function(){return!!this.c},t.isGreaterThan=t.gt=function(m,y){return gt(this,new x(m,y))>0},t.isGreaterThanOrEqualTo=t.gte=function(m,y){return(y=gt(this,new x(m,y)))===1||y===0},t.isInteger=function(){return!!this.c&&Ye(this.e/X)>this.c.length-2},t.isLessThan=t.lt=function(m,y){return gt(this,new x(m,y))<0},t.isLessThanOrEqualTo=t.lte=function(m,y){return(y=gt(this,new x(m,y)))===-1||y===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(m,y){var A,P,L,h,e=this,s=e.s;if(m=new x(m,y),y=m.s,!s||!y)return new x(NaN);if(s!=y)return m.s=-y,e.plus(m);var l=e.e/X,f=m.e/X,d=e.c,v=m.c;if(!l||!f){if(!d||!v)return d?(m.s=-y,m):new x(v?e:NaN);if(!d[0]||!v[0])return v[0]?(m.s=-y,m):new x(d[0]?e:c==3?-0:0)}if(l=Ye(l),f=Ye(f),d=d.slice(),s=l-f){for((h=s<0)?(s=-s,L=d):(f=l,L=v),L.reverse(),y=s;y--;L.push(0));L.reverse()}else for(P=(h=(s=d.length)<(y=v.length))?s:y,s=y=0;y<P;y++)if(d[y]!=v[y]){h=d[y]<v[y];break}if(h&&(L=d,d=v,v=L,m.s=-m.s),y=(P=v.length)-(A=d.length),y>0)for(;y--;d[A++]=0);for(y=et-1;P>s;){if(d[--P]<v[P]){for(A=P;A&&!d[--A];d[A]=y);--d[A],d[P]+=et}d[P]-=v[P]}for(;d[0]==0;d.splice(0,1),--f);return d[0]?W(m,d,f):(m.s=c==3?-1:1,m.c=[m.e=0],m)},t.modulo=t.mod=function(m,y){var A,P,L=this;return m=new x(m,y),!L.c||!m.s||m.c&&!m.c[0]?new x(NaN):!m.c||L.c&&!L.c[0]?new x(L):(T==9?(P=m.s,m.s=1,A=i(L,m,0,3),m.s=P,A.s*=P):A=i(L,m,0,T),m=L.minus(A.times(m)),!m.c[0]&&T==1&&(m.s=L.s),m)},t.multipliedBy=t.times=function(m,y){var A,P,L,h,e,s,l,f,d,v,k,w,a,_,F,E=this,U=E.c,Z=(m=new x(m,y)).c;if(!U||!Z||!U[0]||!Z[0])return!E.s||!m.s||U&&!U[0]&&!Z||Z&&!Z[0]&&!U?m.c=m.e=m.s=null:(m.s*=E.s,!U||!Z?m.c=m.e=null:(m.c=[0],m.e=0)),m;for(P=Ye(E.e/X)+Ye(m.e/X),m.s*=E.s,l=U.length,v=Z.length,l<v&&(a=U,U=Z,Z=a,L=l,l=v,v=L),L=l+v,a=[];L--;a.push(0));for(_=et,F=at,L=v;--L>=0;){for(A=0,k=Z[L]%F,w=Z[L]/F|0,e=l,h=L+e;h>L;)f=U[--e]%F,d=U[e]/F|0,s=w*f+d*k,f=k*f+s%F*F+a[h]+A,A=(f/_|0)+(s/F|0)+w*d,a[h--]=f%_;a[h]=A}return A?++P:a.splice(0,1),W(m,a,P)},t.negated=function(){var m=new x(this);return m.s=-m.s||null,m},t.plus=function(m,y){var A,P=this,L=P.s;if(m=new x(m,y),y=m.s,!L||!y)return new x(NaN);if(L!=y)return m.s=-y,P.minus(m);var h=P.e/X,e=m.e/X,s=P.c,l=m.c;if(!h||!e){if(!s||!l)return new x(L/0);if(!s[0]||!l[0])return l[0]?m:new x(s[0]?P:L*0)}if(h=Ye(h),e=Ye(e),s=s.slice(),L=h-e){for(L>0?(e=h,A=l):(L=-L,A=s),A.reverse();L--;A.push(0));A.reverse()}for(L=s.length,y=l.length,L-y<0&&(A=l,l=s,s=A,y=L),L=0;y;)L=(s[--y]=s[y]+l[y]+L)/et|0,s[y]=et===s[y]?0:s[y]%et;return L&&(s=[L].concat(s),++e),W(m,s,e)},t.precision=t.sd=function(m,y){var A,P,L,h=this;if(m!=null&&m!==!!m)return oe(m,1,qe),y==null?y=c:oe(y,0,8),$(new x(h),m,y);if(!(A=h.c))return null;if(L=A.length-1,P=L*X+1,L=A[L]){for(;L%10==0;L/=10,P--);for(L=A[0];L>=10;L/=10,P++);}return m&&h.e+1>P&&(P=h.e+1),P},t.shiftedBy=function(m){return oe(m,-hr,hr),this.times("1e"+m)},t.squareRoot=t.sqrt=function(){var m,y,A,P,L,h=this,e=h.c,s=h.s,l=h.e,f=p+4,d=new x("0.5");if(s!==1||!e||!e[0])return new x(!s||s<0&&(!e||e[0])?NaN:e?h:1/0);if(s=Math.sqrt(+z(h)),s==0||s==1/0?(y=Xe(e),(y.length+l)%2==0&&(y+="0"),s=Math.sqrt(+y),l=Ye((l+1)/2)-(l<0||l%2),s==1/0?y="5e"+l:(y=s.toExponential(),y=y.slice(0,y.indexOf("e")+1)+l),A=new x(y)):A=new x(s+""),A.c[0]){for(l=A.e,s=l+f,s<3&&(s=0);;)if(L=A,A=d.times(L.plus(i(h,L,f,1))),Xe(L.c).slice(0,s)===(y=Xe(A.c)).slice(0,s))if(A.e<l&&--s,y=y.slice(s-3,s+1),y=="9999"||!P&&y=="4999"){if(!P&&($(L,L.e+p+2,0),L.times(L).eq(h))){A=L;break}f+=4,s+=4,P=1}else{(!+y||!+y.slice(1)&&y.charAt(0)=="5")&&($(A,A.e+p+2,1),m=!A.times(A).eq(h));break}}return $(A,A.e+p+1,c,m)},t.toExponential=function(m,y){return m!=null&&(oe(m,0,qe),m++),q(this,m,y,1)},t.toFixed=function(m,y){return m!=null&&(oe(m,0,qe),m=m+this.e+1),q(this,m,y)},t.toFormat=function(m,y,A){var P,L=this;if(A==null)m!=null&&y&&typeof y=="object"?(A=y,y=null):m&&typeof m=="object"?(A=m,m=y=null):A=O;else if(typeof A!="object")throw Error(Ge+"Argument not an object: "+A);if(P=L.toFixed(m,y),L.c){var h,e=P.split("."),s=+A.groupSize,l=+A.secondaryGroupSize,f=A.groupSeparator||"",d=e[0],v=e[1],k=L.s<0,w=k?d.slice(1):d,a=w.length;if(l&&(h=s,s=l,l=h,a-=h),s>0&&a>0){for(h=a%s||s,d=w.substr(0,h);h<a;h+=s)d+=f+w.substr(h,s);l>0&&(d+=f+w.slice(h)),k&&(d="-"+d)}P=v?d+(A.decimalSeparator||"")+((l=+A.fractionGroupSize)?v.replace(new RegExp("\\d{"+l+"}\\B","g"),"$&"+(A.fractionGroupSeparator||"")):v):d}return(A.prefix||"")+P+(A.suffix||"")},t.toFraction=function(m){var y,A,P,L,h,e,s,l,f,d,v,k,w=this,a=w.c;if(m!=null&&(s=new x(m),!s.isInteger()&&(s.c||s.s!==1)||s.lt(u)))throw Error(Ge+"Argument "+(s.isInteger()?"out of range: ":"not an integer: ")+z(s));if(!a)return new x(w);for(y=new x(u),f=A=new x(u),P=l=new x(u),k=Xe(a),h=y.e=k.length-w.e-1,y.c[0]=pr[(e=h%X)<0?X+e:e],m=!m||s.comparedTo(y)>0?h>0?y:f:s,e=S,S=1/0,s=new x(k),l.c[0]=0;d=i(s,y,0,1),L=A.plus(d.times(P)),L.comparedTo(m)!=1;)A=P,P=L,f=l.plus(d.times(L=f)),l=L,y=s.minus(d.times(L=y)),s=L;return L=i(m.minus(A),P,0,1),l=l.plus(L.times(f)),A=A.plus(L.times(P)),l.s=f.s=w.s,h=h*2,v=i(f,P,h,c).minus(w).abs().comparedTo(i(l,A,h,c).minus(w).abs())<1?[f,P]:[l,A],S=e,v},t.toNumber=function(){return+z(this)},t.toPrecision=function(m,y){return m!=null&&oe(m,1,qe),q(this,m,y,2)},t.toString=function(m){var y,A=this,P=A.s,L=A.e;return L===null?P?(y="Infinity",P<0&&(y="-"+y)):y="NaN":(m==null?y=L<=g||L>=M?Lt(Xe(A.c),L):ot(Xe(A.c),L,"0"):m===10&&R?(A=$(new x(A),p+L+1,c),y=ot(Xe(A.c),A.e,"0")):(oe(m,2,B.length,"Base"),y=r(ot(Xe(A.c),L,"0"),10,m,P,!0)),P<0&&A.c[0]&&(y="-"+y)),y},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&&x.set(n),x}function Ye(n){var i=n|0;return n>0||n===i?i:i-1}function Xe(n){for(var i,r,o=1,t=n.length,u=n[0]+"";o<t;){for(i=n[o++]+"",r=X-i.length;r--;i="0"+i);u+=i}for(t=u.length;u.charCodeAt(--t)===48;);return u.slice(0,t+1||1)}function gt(n,i){var r,o,t=n.c,u=i.c,p=n.s,c=i.s,g=n.e,M=i.e;if(!p||!c)return null;if(r=t&&!t[0],o=u&&!u[0],r||o)return r?o?0:-c:p;if(p!=c)return p;if(r=p<0,o=g==M,!t||!u)return o?0:!t^r?1:-1;if(!o)return g>M^r?1:-1;for(c=(g=t.length)<(M=u.length)?g:M,p=0;p<c;p++)if(t[p]!=u[p])return t[p]>u[p]^r?1:-1;return g==M?0:g>M^r?1:-1}function oe(n,i,r,o){if(n<i||n>r||n!==Je(n))throw Error(Ge+(o||"Argument")+(typeof n=="number"?n<i||n>r?" out of range: ":" not an integer: ":" not a primitive number: ")+String(n))}function Pt(n){var i=n.c.length-1;return Ye(n.e/X)==i&&n.c[i]%2!=0}function Lt(n,i){return(n.length>1?n.charAt(0)+"."+n.slice(1):n)+(i<0?"e":"e+")+i}function ot(n,i,r){var o,t;if(i<0){for(t=r+".";++i;t+=r);n=t+n}else if(o=n.length,++i>o){for(t=r,i-=o;--i;t+=r);n+=t}else i<o&&(n=n.slice(0,i)+"."+n.slice(i));return n}var ji=Xr(),We=ji;import{linearlyInterpolate as zi,Side as vt}from"@suilend/sdk";import{NORMALIZED_flSUI_COINTYPE as Ki,NORMALIZED_jugSUI_COINTYPE as Gi,NORMALIZED_LBTC_COINTYPE as Hi,formatLtvPercent as Vi}from"@suilend/sui-fe";import $i from"crypto";var xt=new Uint8Array(256),Tt=xt.length;function gr(){return Tt>xt.length-16&&($i.randomFillSync(xt),Tt=0),xt.slice(Tt,Tt+=16)}var je=[];for(let n=0;n<256;++n)je.push((n+256).toString(16).slice(1));function Jr(n,i=0){return je[n[i+0]]+je[n[i+1]]+je[n[i+2]]+je[n[i+3]]+"-"+je[n[i+4]]+je[n[i+5]]+"-"+je[n[i+6]]+je[n[i+7]]+"-"+je[n[i+8]]+je[n[i+9]]+"-"+je[n[i+10]]+je[n[i+11]]+je[n[i+12]]+je[n[i+13]]+je[n[i+14]]+je[n[i+15]]}import Zi from"crypto";var mr={randomUUID:Zi.randomUUID};function Wi(n,i,r){if(mr.randomUUID&&!i&&!n)return mr.randomUUID();n=n||{};let o=n.random||(n.rng||gr)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,i){r=r||0;for(let t=0;t<16;++t)i[r+t]=o[t];return i}return Jr(o)}var mt=Wi;import{getPackagerConfigs as Qi}from"@cetusprotocol/common-sdk";var vr=10n;function Yr(n){return n.depositedAmount.eq(0)?new We(0):n.borrowedAmount.div(n.depositedAmount).times(100)}function wr(n){let i=Yr(n);if(!i.gt(100))return zi(n.config.interestRate,"utilPercent","aprPercent",i)}function ei(n){let i=Yr(n),r=wr(n);if(!(r===void 0||i.gt(100)))return new We(i.div(100)).times(r.div(100)).times(1-n.config.spreadFeeBps/1e4).times(100)}function qo(n,i,r,o){let{apiUrl:t}=Qi(o.sdkOptions.suilend);n===vt.DEPOSIT&&([Ki,Gi].includes(i.coinType)&&r.push({stats:{id:mt(),isActive:!0,rewardIndex:-1,reserve:i,rewardCoinType:"LIQ_AG",mintDecimals:0,symbol:"LiqAg Points",iconUrl:`${t}/partners/LiqAg.png`,perDay:new We(.036),side:vt.DEPOSIT},obligationClaims:{}}),i.coinType===Hi&&r.push({stats:{id:mt(),isActive:!0,rewardIndex:-1,reserve:i,rewardCoinType:"LOMBARD",mintDecimals:0,symbol:"3x Lombard Lux",iconUrl:`${t}/partners/Lombard Lux.png`,perDay:new We(0),side:vt.DEPOSIT},obligationClaims:{}}))}function jo(n,i,r){return{...n,depositedAmount:i===vt.DEPOSIT?We.max(n.depositedAmount.plus(r),0):n.depositedAmount,borrowedAmount:i===vt.BORROW?We.max(n.borrowedAmount.plus(r),0):n.borrowedAmount}}function $o(n,i,r){let o=n===vt.DEPOSIT?i.depositedAmount:i.borrowedAmount,t=n===vt.DEPOSIT?r.depositedAmount:r.borrowedAmount,u=t.eq(0)?new We(-1):o.div(t);return{multiplier:u,isValid:!u.eq(-1)}}function Zo(n,i,r,o){let t=n.map(p=>({...p,stats:{...p.stats,perDay:o?p.stats.perDay.times(r):void 0}})),u=i.map(p=>({...p,stats:{...p.stats,aprPercent:o?p.stats.aprPercent.times(r):void 0}}));return{newPerDayRewards:t,newAprRewards:u}}function ti(n,i){if(!vr)throw new Error("oraclePriceMultiplierDecimal is required");if(n===0n)throw new Error("Invalid oracle price");if(i<0n){let r=10n**(vr- -i);return n*r}return n/10n**(i+vr)}function Wo(n){return Vi(new We(n))}import{CACHE_TIME_5MIN as ln,d as Mr,getPackagerConfigs as wt,removeHexPrefix as br}from"@cetusprotocol/common-sdk";import{toHEX as ii}from"@mysten/sui/utils";var Dt=class{constructor(i){this.getSuiLendCoinInfo=async(i,r,o)=>{let t=new We(r.toString()),{allLendingMarketData:u,lstAprPercentMap:p}=await this.getLendingMarketData(!1);if(await this.getInitializeObligations({allLendingMarketData:u,lstAprPercentMap:p},!0),!u||!p)throw new Error("lendingMarketCache is not initialized");let c=Object.values(u??{})[0],g=c.lendingMarket.reserves.find(m=>m.coinType===i);if(!g)throw new Error("reserve not found");let b=(this.obligationsCache?.[c.lendingMarket.id]).rewardMap[g.token.coinType]?.[o]??[],S=rn(b),C=nn(o,g.coinType,p),T=o===lt.DEPOSIT?g.depositAprPercent:g.borrowAprPercent,D=T,O=new We(1),B=!0;if(o!==void 0&&t!==void 0&&t.gt(0)){let m={...g,depositedAmount:o===lt.DEPOSIT?We.max(g.depositedAmount.plus(o===lt.DEPOSIT?t:t.negated()),0):g.depositedAmount,borrowedAmount:o===lt.BORROW?We.max(g.borrowedAmount.plus(o===lt.BORROW?t:t.negated()),0):g.borrowedAmount};D=o===lt.DEPOSIT?ei(m):wr(m);let y=o===lt.DEPOSIT?g.depositedAmount:g.borrowedAmount,A=o===lt.DEPOSIT?m.depositedAmount:m.borrowedAmount;O=A.eq(0)?new We(-1):y.div(A),B=!O.eq(-1)}let q=on(S).map(m=>({...m,stats:{...m.stats,perDay:B?m.stats.perDay.times(O):void 0}})),W=sn(S).map(m=>({...m,stats:{...m.stats,aprPercent:B?m.stats.aprPercent.times(O):void 0}})),$=ri(o,T,S,C),z=D===void 0||W.some(m=>m.stats.aprPercent===void 0)?void 0:ri(o,D,W,C);return console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:315 ~ SuiLendModule ~ newTotalAprPercent:",z?.toString()),{newTotalAprPercent:z?.toString(),totalAprPercent:$.toString()}};this.refreshReservePrice=async(i,r,o)=>{let{lendingMarketId:t,lendingMarketType:u}=wt(this.sdk.sdkOptions?.suilend),p=`${t}_${u}`;if(!this.suilendClientCache)throw new Error("suilendClientCache is not initialized");let c=await this.suilendClientCache[p].pythClient.getPriceFeedObjectId(r);this.suilendClientCache[p].refreshReservePrices(i,c,o)};this.getLatestPriceFeeds=async(i,r=!1)=>{let{lendingMarketId:o,lendingMarketType:t}=wt(this.sdk.sdkOptions?.suilend),u={},p=[];if(i.forEach(b=>{let S=this.sdk.getCache(`getLatestPrice_${b.coinType.name}`,r);S&&this.priceCheck(S,60)?u[b.coinType.name]=S:p.push(b.coinType.name)}),p.length==0)return u;let c=`${o}_${t}`;if(!this.suilendClientCache)throw new Error("suilendClient is not initialized");let g=Array.from(new Set(i.map(b=>b?.priceIdentifier?.bytes?ii(new Uint8Array(b.priceIdentifier.bytes)):br(b.priceIdentifier.toString()))));return(await this.suilendClientCache[c].pythConnection.getLatestPriceFeeds(g))?.forEach((b,S)=>{let C=b.getPriceUnchecked(),T=i[S];if(C){let{price:D,expo:O}=C,B=Mr(D).mul(Mr(10).pow(Mr(O))).toString(),R={coin_type:T.coinType.name||T.coinType,price:B,oracle_price:0n,last_update_time:C.publishTime};R.oracle_price=ti(BigInt(D),BigInt(O)),u[T.coinType.name||T.coinType]=R,this.sdk.updateCache(`getLatestPrice_${R.coin_type}`,R)}}),u};this.getLatestPriceFeedsByCoinTypes=async(i,r=!1)=>{let{lendingMarketId:o}=wt(this.sdk.sdkOptions?.suilend),{allLendingMarketData:t}=await this.getLendingMarketData(!1),u=[];return i.forEach(c=>{let g=t[o].reserveMap[c];u.push(g)}),await this.getLatestPriceFeeds(u,r)};this.getSuiLendReserveInfo=async(i,r,o)=>{let{lendingMarketId:t}=wt(this.sdk.sdkOptions?.suilend);o||(o=(await this.getLendingMarketData(!1)).allLendingMarketData);let u=o[t].reserveMap[i],p=o[t].reserveMap[r],c=o[t].refreshedRawReserves.filter(S=>S.coinType.name==br(i)),g=o[t].refreshedRawReserves.filter(S=>S.coinType.name==br(r)),M=o[t].reserveMap[i],b=o[t].reserveMap[r];return{baseReserveArrayIndex:u.arrayIndex.toString(),quoteReserveArrayIndex:p.arrayIndex.toString(),reserve:[c[0],g[0]],baseReserveMapInfo:M,quoteReserveMapInfo:b}};this.refreshReservePrices=async(i,r)=>{for(let o=0;o<r.length;o++){let t=ii(new Uint8Array(r[o].priceIdentifier.bytes));await this.refreshReservePrice(i,t,BigInt(r[o].arrayIndex))}};this.sdk=i}async getSuilendClient(i,r){let o=`${i}_${r}`;return this.suilendClientCache||(this.suilendClientCache={}),this.suilendClientCache[o]||(this.suilendClientCache[o]=await Xi.initialize(i,r,this.sdk.FullClient,!0)),this.suilendClientCache[o]}async getLendingMarketData(i=!0){let r="lendingMarketData",o=this.sdk.getCache(r,i);if(o)return o;let t=wt(this.sdk.sdkOptions?.suilend)?.lendingMarket||[];if(!t.length)return console.warn("\u26A0\uFE0F No lending markets configured."),{};try{let[u,p]=await Promise.all([(async()=>Object.fromEntries(await Promise.all(t.map(async g=>{let M=await this.getSuilendClient(g.id,g.type),{lendingMarket:b,coinMetadataMap:S,refreshedRawReserves:C,reserveMap:T,reserveCoinTypes:D,reserveCoinMetadataMap:O,rewardCoinTypes:B,activeRewardCoinTypes:R,rewardCoinMetadataMap:x}=await Ji(this.sdk.FullClient,M),{rewardPriceMap:q}=await Yi(T,R);return[g.id,{suilendClient:M,lendingMarket:b,coinMetadataMap:S,refreshedRawReserves:C,reserveMap:T,reserveCoinTypes:D,reserveCoinMetadataMap:O,rewardPriceMap:q,rewardCoinTypes:B,activeRewardCoinTypes:R,rewardCoinMetadataMap:x}]}))))(),(async()=>{try{let c=await fetch(`${this.sdk.sdkOptions?.suilend?.config?.apiUrl}/springsui/apy`),g=await c.json();if(c?.statusCode===500)throw new Error("Failed to fetch SpringSui LST APRs");return Object.fromEntries(Object.entries(g).map(([M,b])=>[M,new We(b)]))}catch(c){return console.error(c),{}}})()]);return this.sdk.updateCache(r,{allLendingMarketData:u,lstAprPercentMap:p},ln),{allLendingMarketData:u,lstAprPercentMap:p}}catch(u){throw console.log("\u{1F680} getLendingMarketData error:",u),u}}async getInitializeObligations(i,r=!1){if(!r&&this.obligationsCache)return this.obligationsCache;i||(i=await this.getLendingMarketData(!1));let o={};for(let t of Object.values(i.allLendingMarketData)){let{obligationOwnerCaps:u,obligations:p}=await en(this.sdk.FullClient,t.suilendClient,t.refreshedRawReserves,t.reserveMap,this.sdk.senderAddress),c=tn(t.reserveMap,t.rewardCoinMetadataMap,t.rewardPriceMap,p);o[t.lendingMarket.id]={obligationOwnerCaps:u,obligations:p,rewardMap:c}}return this.obligationsCache=o,this.obligationsCache}async getSuiLendUserData(i,r=!1){let{allLendingMarketData:o,lstAprPercentMap:t}=await this.getLendingMarketData(!1),u=await this.getInitializeObligations({allLendingMarketData:o,lstAprPercentMap:t},!1),{lendingMarketId:p}=wt(this.sdk.sdkOptions?.suilend),c=u[p];if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:275 ~ SuiLendModule ~ getSuiLendUserData ~ userData:",c),!c)throw new Error(`No user data found for lending market: ${p}`);if(i){let g=c.obligations.find(b=>b.id===i);if(!g)throw new Error(`Obligation not found: ${i}`);let M=an(g,c.rewardMap,t??{});return{obligation:g,netAprPercent:M,deposits:g.deposits,borrowedAmount:g.borrowedAmount,depositedAmount:g.depositedAmount,netValueUsd:g.netValueUsd,rewardMap:c.rewardMap}}}priceCheck(i,r=60){let o=Math.floor(Date.now()/1e3);if(!(Math.abs(o-i.last_update_time)>r))return i}};import{getPackagerConfigs as ne,CACHE_TIME_5MIN as _i,CoinAssist as Y,CLOCK_ADDRESS as pt,d as I,removeHexPrefix as yt,getObjectFields as ki,addHexPrefix as ao,U64_MAX as Ai}from"@cetusprotocol/common-sdk";var Mt=9e15,dt=1e9,yr="0123456789abcdef",Rt="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",Bt="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",_r={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Mt,maxE:Mt,crypto:!1},ai,st,V=!0,Et="[DecimalError] ",ct=Et+"Invalid argument: ",li=Et+"Precision limit exceeded",ui=Et+"crypto unavailable",fi="[object Decimal]",ze=Math.floor,Fe=Math.pow,un=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,fn=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,cn=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,ci=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,tt=1e7,H=7,dn=9007199254740991,hn=Rt.length-1,kr=Bt.length-1,N={toStringTag:fi};N.absoluteValue=N.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),K(n)};N.ceil=function(){return K(new this.constructor(this),this.e+1,2)};N.clampedTo=N.clamp=function(n,i){var r,o=this,t=o.constructor;if(n=new t(n),i=new t(i),!n.s||!i.s)return new t(NaN);if(n.gt(i))throw Error(ct+i);return r=o.cmp(n),r<0?n:o.cmp(i)>0?i:new t(o)};N.comparedTo=N.cmp=function(n){var i,r,o,t,u=this,p=u.d,c=(n=new u.constructor(n)).d,g=u.s,M=n.s;if(!p||!c)return!g||!M?NaN:g!==M?g:p===c?0:!p^g<0?1:-1;if(!p[0]||!c[0])return p[0]?g:c[0]?-M:0;if(g!==M)return g;if(u.e!==n.e)return u.e>n.e^g<0?1:-1;for(o=p.length,t=c.length,i=0,r=o<t?o:t;i<r;++i)if(p[i]!==c[i])return p[i]>c[i]^g<0?1:-1;return o===t?0:o>t^g<0?1:-1};N.cosine=N.cos=function(){var n,i,r=this,o=r.constructor;return r.d?r.d[0]?(n=o.precision,i=o.rounding,o.precision=n+Math.max(r.e,r.sd())+H,o.rounding=1,r=pn(o,mi(o,r)),o.precision=n,o.rounding=i,K(st==2||st==3?r.neg():r,n,i,!0)):new o(1):new o(NaN)};N.cubeRoot=N.cbrt=function(){var n,i,r,o,t,u,p,c,g,M,b=this,S=b.constructor;if(!b.isFinite()||b.isZero())return new S(b);for(V=!1,u=b.s*Fe(b.s*b,1/3),!u||Math.abs(u)==1/0?(r=$e(b.d),n=b.e,(u=(n-r.length+1)%3)&&(r+=u==1||u==-2?"0":"00"),u=Fe(r,1/3),n=ze((n+1)/3)-(n%3==(n<0?-1:2)),u==1/0?r="5e"+n:(r=u.toExponential(),r=r.slice(0,r.indexOf("e")+1)+n),o=new S(r),o.s=b.s):o=new S(u.toString()),p=(n=S.precision)+3;;)if(c=o,g=c.times(c).times(c),M=g.plus(b),o=ie(M.plus(b).times(c),M.plus(g),p+2,1),$e(c.d).slice(0,p)===(r=$e(o.d)).slice(0,p))if(r=r.slice(p-3,p+1),r=="9999"||!t&&r=="4999"){if(!t&&(K(c,n+1,0),c.times(c).times(c).eq(b))){o=c;break}p+=4,t=1}else{(!+r||!+r.slice(1)&&r.charAt(0)=="5")&&(K(o,n+1,1),i=!o.times(o).times(o).eq(b));break}return V=!0,K(o,n,S.rounding,i)};N.decimalPlaces=N.dp=function(){var n,i=this.d,r=NaN;if(i){if(n=i.length-1,r=(n-ze(this.e/H))*H,n=i[n],n)for(;n%10==0;n/=10)r--;r<0&&(r=0)}return r};N.dividedBy=N.div=function(n){return ie(this,new this.constructor(n))};N.dividedToIntegerBy=N.divToInt=function(n){var i=this,r=i.constructor;return K(ie(i,new r(n),0,1,1),r.precision,r.rounding)};N.equals=N.eq=function(n){return this.cmp(n)===0};N.floor=function(){return K(new this.constructor(this),this.e+1,3)};N.greaterThan=N.gt=function(n){return this.cmp(n)>0};N.greaterThanOrEqualTo=N.gte=function(n){var i=this.cmp(n);return i==1||i===0};N.hyperbolicCosine=N.cosh=function(){var n,i,r,o,t,u=this,p=u.constructor,c=new p(1);if(!u.isFinite())return new p(u.s?1/0:NaN);if(u.isZero())return c;r=p.precision,o=p.rounding,p.precision=r+Math.max(u.e,u.sd())+4,p.rounding=1,t=u.d.length,t<32?(n=Math.ceil(t/3),i=(1/Ut(4,n)).toString()):(n=16,i="2.3283064365386962890625e-10"),u=bt(p,1,u.times(i),new p(1),!0);for(var g,M=n,b=new p(8);M--;)g=u.times(u),u=c.minus(g.times(b.minus(g.times(b))));return K(u,p.precision=r,p.rounding=o,!0)};N.hyperbolicSine=N.sinh=function(){var n,i,r,o,t=this,u=t.constructor;if(!t.isFinite()||t.isZero())return new u(t);if(i=u.precision,r=u.rounding,u.precision=i+Math.max(t.e,t.sd())+4,u.rounding=1,o=t.d.length,o<3)t=bt(u,2,t,t,!0);else{n=1.4*Math.sqrt(o),n=n>16?16:n|0,t=t.times(1/Ut(5,n)),t=bt(u,2,t,t,!0);for(var p,c=new u(5),g=new u(16),M=new u(20);n--;)p=t.times(t),t=t.times(c.plus(p.times(g.times(p).plus(M))))}return u.precision=i,u.rounding=r,K(t,i,r,!0)};N.hyperbolicTangent=N.tanh=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+7,o.rounding=1,ie(r.sinh(),r.cosh(),o.precision=n,o.rounding=i)):new o(r.s)};N.inverseCosine=N.acos=function(){var n=this,i=n.constructor,r=n.abs().cmp(1),o=i.precision,t=i.rounding;return r!==-1?r===0?n.isNeg()?it(i,o,t):new i(0):new i(NaN):n.isZero()?it(i,o+4,t).times(.5):(i.precision=o+6,i.rounding=1,n=new i(1).minus(n).div(n.plus(1)).sqrt().atan(),i.precision=o,i.rounding=t,n.times(2))};N.inverseHyperbolicCosine=N.acosh=function(){var n,i,r=this,o=r.constructor;return r.lte(1)?new o(r.eq(1)?0:NaN):r.isFinite()?(n=o.precision,i=o.rounding,o.precision=n+Math.max(Math.abs(r.e),r.sd())+4,o.rounding=1,V=!1,r=r.times(r).minus(1).sqrt().plus(r),V=!0,o.precision=n,o.rounding=i,r.ln()):new o(r)};N.inverseHyperbolicSine=N.asinh=function(){var n,i,r=this,o=r.constructor;return!r.isFinite()||r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+2*Math.max(Math.abs(r.e),r.sd())+6,o.rounding=1,V=!1,r=r.times(r).plus(1).sqrt().plus(r),V=!0,o.precision=n,o.rounding=i,r.ln())};N.inverseHyperbolicTangent=N.atanh=function(){var n,i,r,o,t=this,u=t.constructor;return t.isFinite()?t.e>=0?new u(t.abs().eq(1)?t.s/0:t.isZero()?t:NaN):(n=u.precision,i=u.rounding,o=t.sd(),Math.max(o,n)<2*-t.e-1?K(new u(t),n,i,!0):(u.precision=r=o-t.e,t=ie(t.plus(1),new u(1).minus(t),r+n,1),u.precision=n+4,u.rounding=1,t=t.ln(),u.precision=n,u.rounding=i,t.times(.5))):new u(NaN)};N.inverseSine=N.asin=function(){var n,i,r,o,t=this,u=t.constructor;return t.isZero()?new u(t):(i=t.abs().cmp(1),r=u.precision,o=u.rounding,i!==-1?i===0?(n=it(u,r+4,o).times(.5),n.s=t.s,n):new u(NaN):(u.precision=r+6,u.rounding=1,t=t.div(new u(1).minus(t.times(t)).sqrt().plus(1)).atan(),u.precision=r,u.rounding=o,t.times(2)))};N.inverseTangent=N.atan=function(){var n,i,r,o,t,u,p,c,g,M=this,b=M.constructor,S=b.precision,C=b.rounding;if(M.isFinite()){if(M.isZero())return new b(M);if(M.abs().eq(1)&&S+4<=kr)return p=it(b,S+4,C).times(.25),p.s=M.s,p}else{if(!M.s)return new b(NaN);if(S+4<=kr)return p=it(b,S+4,C).times(.5),p.s=M.s,p}for(b.precision=c=S+10,b.rounding=1,r=Math.min(28,c/H+2|0),n=r;n;--n)M=M.div(M.times(M).plus(1).sqrt().plus(1));for(V=!1,i=Math.ceil(c/H),o=1,g=M.times(M),p=new b(M),t=M;n!==-1;)if(t=t.times(g),u=p.minus(t.div(o+=2)),t=t.times(g),p=u.plus(t.div(o+=2)),p.d[i]!==void 0)for(n=i;p.d[n]===u.d[n]&&n--;);return r&&(p=p.times(2<<r-1)),V=!0,K(p,b.precision=S,b.rounding=C,!0)};N.isFinite=function(){return!!this.d};N.isInteger=N.isInt=function(){return!!this.d&&ze(this.e/H)>this.d.length-2};N.isNaN=function(){return!this.s};N.isNegative=N.isNeg=function(){return this.s<0};N.isPositive=N.isPos=function(){return this.s>0};N.isZero=function(){return!!this.d&&this.d[0]===0};N.lessThan=N.lt=function(n){return this.cmp(n)<0};N.lessThanOrEqualTo=N.lte=function(n){return this.cmp(n)<1};N.logarithm=N.log=function(n){var i,r,o,t,u,p,c,g,M=this,b=M.constructor,S=b.precision,C=b.rounding,T=5;if(n==null)n=new b(10),i=!0;else{if(n=new b(n),r=n.d,n.s<0||!r||!r[0]||n.eq(1))return new b(NaN);i=n.eq(10)}if(r=M.d,M.s<0||!r||!r[0]||M.eq(1))return new b(r&&!r[0]?-1/0:M.s!=1?NaN:r?0:1/0);if(i)if(r.length>1)u=!0;else{for(t=r[0];t%10===0;)t/=10;u=t!==1}if(V=!1,c=S+T,p=ft(M,c),o=i?Nt(b,c+10):ft(n,c),g=ie(p,o,c,1),At(g.d,t=S,C))do if(c+=10,p=ft(M,c),o=i?Nt(b,c+10):ft(n,c),g=ie(p,o,c,1),!u){+$e(g.d).slice(t+1,t+15)+1==1e14&&(g=K(g,S+1,0));break}while(At(g.d,t+=10,C));return V=!0,K(g,S,C)};N.minus=N.sub=function(n){var i,r,o,t,u,p,c,g,M,b,S,C,T=this,D=T.constructor;if(n=new D(n),!T.d||!n.d)return!T.s||!n.s?n=new D(NaN):T.d?n.s=-n.s:n=new D(n.d||T.s!==n.s?T:NaN),n;if(T.s!=n.s)return n.s=-n.s,T.plus(n);if(M=T.d,C=n.d,c=D.precision,g=D.rounding,!M[0]||!C[0]){if(C[0])n.s=-n.s;else if(M[0])n=new D(T);else return new D(g===3?-0:0);return V?K(n,c,g):n}if(r=ze(n.e/H),b=ze(T.e/H),M=M.slice(),u=b-r,u){for(S=u<0,S?(i=M,u=-u,p=C.length):(i=C,r=b,p=M.length),o=Math.max(Math.ceil(c/H),p)+2,u>o&&(u=o,i.length=1),i.reverse(),o=u;o--;)i.push(0);i.reverse()}else{for(o=M.length,p=C.length,S=o<p,S&&(p=o),o=0;o<p;o++)if(M[o]!=C[o]){S=M[o]<C[o];break}u=0}for(S&&(i=M,M=C,C=i,n.s=-n.s),p=M.length,o=C.length-p;o>0;--o)M[p++]=0;for(o=C.length;o>u;){if(M[--o]<C[o]){for(t=o;t&&M[--t]===0;)M[t]=tt-1;--M[t],M[o]+=tt}M[o]-=C[o]}for(;M[--p]===0;)M.pop();for(;M[0]===0;M.shift())--r;return M[0]?(n.d=M,n.e=Ft(M,r),V?K(n,c,g):n):new D(g===3?-0:0)};N.modulo=N.mod=function(n){var i,r=this,o=r.constructor;return n=new o(n),!r.d||!n.s||n.d&&!n.d[0]?new o(NaN):!n.d||r.d&&!r.d[0]?K(new o(r),o.precision,o.rounding):(V=!1,o.modulo==9?(i=ie(r,n.abs(),0,3,1),i.s*=n.s):i=ie(r,n,0,o.modulo,1),i=i.times(n),V=!0,r.minus(i))};N.naturalExponential=N.exp=function(){return Ar(this)};N.naturalLogarithm=N.ln=function(){return ft(this)};N.negated=N.neg=function(){var n=new this.constructor(this);return n.s=-n.s,K(n)};N.plus=N.add=function(n){var i,r,o,t,u,p,c,g,M,b,S=this,C=S.constructor;if(n=new C(n),!S.d||!n.d)return!S.s||!n.s?n=new C(NaN):S.d||(n=new C(n.d||S.s===n.s?S:NaN)),n;if(S.s!=n.s)return n.s=-n.s,S.minus(n);if(M=S.d,b=n.d,c=C.precision,g=C.rounding,!M[0]||!b[0])return b[0]||(n=new C(S)),V?K(n,c,g):n;if(u=ze(S.e/H),o=ze(n.e/H),M=M.slice(),t=u-o,t){for(t<0?(r=M,t=-t,p=b.length):(r=b,o=u,p=M.length),u=Math.ceil(c/H),p=u>p?u+1:p+1,t>p&&(t=p,r.length=1),r.reverse();t--;)r.push(0);r.reverse()}for(p=M.length,t=b.length,p-t<0&&(t=p,r=b,b=M,M=r),i=0;t;)i=(M[--t]=M[t]+b[t]+i)/tt|0,M[t]%=tt;for(i&&(M.unshift(i),++o),p=M.length;M[--p]==0;)M.pop();return n.d=M,n.e=Ft(M,o),V?K(n,c,g):n};N.precision=N.sd=function(n){var i,r=this;if(n!==void 0&&n!==!!n&&n!==1&&n!==0)throw Error(ct+n);return r.d?(i=di(r.d),n&&r.e+1>i&&(i=r.e+1)):i=NaN,i};N.round=function(){var n=this,i=n.constructor;return K(new i(n),n.e+1,i.rounding)};N.sine=N.sin=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+Math.max(r.e,r.sd())+H,o.rounding=1,r=mn(o,mi(o,r)),o.precision=n,o.rounding=i,K(st>2?r.neg():r,n,i,!0)):new o(NaN)};N.squareRoot=N.sqrt=function(){var n,i,r,o,t,u,p=this,c=p.d,g=p.e,M=p.s,b=p.constructor;if(M!==1||!c||!c[0])return new b(!M||M<0&&(!c||c[0])?NaN:c?p:1/0);for(V=!1,M=Math.sqrt(+p),M==0||M==1/0?(i=$e(c),(i.length+g)%2==0&&(i+="0"),M=Math.sqrt(i),g=ze((g+1)/2)-(g<0||g%2),M==1/0?i="5e"+g:(i=M.toExponential(),i=i.slice(0,i.indexOf("e")+1)+g),o=new b(i)):o=new b(M.toString()),r=(g=b.precision)+3;;)if(u=o,o=u.plus(ie(p,u,r+2,1)).times(.5),$e(u.d).slice(0,r)===(i=$e(o.d)).slice(0,r))if(i=i.slice(r-3,r+1),i=="9999"||!t&&i=="4999"){if(!t&&(K(u,g+1,0),u.times(u).eq(p))){o=u;break}r+=4,t=1}else{(!+i||!+i.slice(1)&&i.charAt(0)=="5")&&(K(o,g+1,1),n=!o.times(o).eq(p));break}return V=!0,K(o,g,b.rounding,n)};N.tangent=N.tan=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+10,o.rounding=1,r=r.sin(),r.s=1,r=ie(r,new o(1).minus(r.times(r)).sqrt(),n+10,0),o.precision=n,o.rounding=i,K(st==2||st==4?r.neg():r,n,i,!0)):new o(NaN)};N.times=N.mul=function(n){var i,r,o,t,u,p,c,g,M,b=this,S=b.constructor,C=b.d,T=(n=new S(n)).d;if(n.s*=b.s,!C||!C[0]||!T||!T[0])return new S(!n.s||C&&!C[0]&&!T||T&&!T[0]&&!C?NaN:!C||!T?n.s/0:n.s*0);for(r=ze(b.e/H)+ze(n.e/H),g=C.length,M=T.length,g<M&&(u=C,C=T,T=u,p=g,g=M,M=p),u=[],p=g+M,o=p;o--;)u.push(0);for(o=M;--o>=0;){for(i=0,t=g+o;t>o;)c=u[t]+T[o]*C[t-o-1]+i,u[t--]=c%tt|0,i=c/tt|0;u[t]=(u[t]+i)%tt|0}for(;!u[--p];)u.pop();return i?++r:u.shift(),n.d=u,n.e=Ft(u,r),V?K(n,S.precision,S.rounding):n};N.toBinary=function(n,i){return Sr(this,2,n,i)};N.toDecimalPlaces=N.toDP=function(n,i){var r=this,o=r.constructor;return r=new o(r),n===void 0?r:(He(n,0,dt),i===void 0?i=o.rounding:He(i,0,8),K(r,n+r.e+1,i))};N.toExponential=function(n,i){var r,o=this,t=o.constructor;return n===void 0?r=nt(o,!0):(He(n,0,dt),i===void 0?i=t.rounding:He(i,0,8),o=K(new t(o),n+1,i),r=nt(o,!0,n+1)),o.isNeg()&&!o.isZero()?"-"+r:r};N.toFixed=function(n,i){var r,o,t=this,u=t.constructor;return n===void 0?r=nt(t):(He(n,0,dt),i===void 0?i=u.rounding:He(i,0,8),o=K(new u(t),n+t.e+1,i),r=nt(o,!1,n+o.e+1)),t.isNeg()&&!t.isZero()?"-"+r:r};N.toFraction=function(n){var i,r,o,t,u,p,c,g,M,b,S,C,T=this,D=T.d,O=T.constructor;if(!D)return new O(T);if(M=r=new O(1),o=g=new O(0),i=new O(o),u=i.e=di(D)-T.e-1,p=u%H,i.d[0]=Fe(10,p<0?H+p:p),n==null)n=u>0?i:M;else{if(c=new O(n),!c.isInt()||c.lt(M))throw Error(ct+c);n=c.gt(i)?u>0?i:M:c}for(V=!1,c=new O($e(D)),b=O.precision,O.precision=u=D.length*H*2;S=ie(c,i,0,1,1),t=r.plus(S.times(o)),t.cmp(n)!=1;)r=o,o=t,t=M,M=g.plus(S.times(t)),g=t,t=i,i=c.minus(S.times(t)),c=t;return t=ie(n.minus(r),o,0,1,1),g=g.plus(t.times(M)),r=r.plus(t.times(o)),g.s=M.s=T.s,C=ie(M,o,u,1).minus(T).abs().cmp(ie(g,r,u,1).minus(T).abs())<1?[M,o]:[g,r],O.precision=b,V=!0,C};N.toHexadecimal=N.toHex=function(n,i){return Sr(this,16,n,i)};N.toNearest=function(n,i){var r=this,o=r.constructor;if(r=new o(r),n==null){if(!r.d)return r;n=new o(1),i=o.rounding}else{if(n=new o(n),i===void 0?i=o.rounding:He(i,0,8),!r.d)return n.s?r:n;if(!n.d)return n.s&&(n.s=r.s),n}return n.d[0]?(V=!1,r=ie(r,n,0,i,1).times(n),V=!0,K(r)):(n.s=r.s,r=n),r};N.toNumber=function(){return+this};N.toOctal=function(n,i){return Sr(this,8,n,i)};N.toPower=N.pow=function(n){var i,r,o,t,u,p,c=this,g=c.constructor,M=+(n=new g(n));if(!c.d||!n.d||!c.d[0]||!n.d[0])return new g(Fe(+c,M));if(c=new g(c),c.eq(1))return c;if(o=g.precision,u=g.rounding,n.eq(1))return K(c,o,u);if(i=ze(n.e/H),i>=n.d.length-1&&(r=M<0?-M:M)<=dn)return t=hi(g,c,r,o),n.s<0?new g(1).div(t):K(t,o,u);if(p=c.s,p<0){if(i<n.d.length-1)return new g(NaN);if((n.d[i]&1)==0&&(p=1),c.e==0&&c.d[0]==1&&c.d.length==1)return c.s=p,c}return r=Fe(+c,M),i=r==0||!isFinite(r)?ze(M*(Math.log("0."+$e(c.d))/Math.LN10+c.e+1)):new g(r+"").e,i>g.maxE+1||i<g.minE-1?new g(i>0?p/0:0):(V=!1,g.rounding=c.s=1,r=Math.min(12,(i+"").length),t=Ar(n.times(ft(c,o+r)),o),t.d&&(t=K(t,o+5,1),At(t.d,o,u)&&(i=o+10,t=K(Ar(n.times(ft(c,i+r)),i),i+5,1),+$e(t.d).slice(o+1,o+15)+1==1e14&&(t=K(t,o+1,0)))),t.s=p,V=!0,g.rounding=u,K(t,o,u))};N.toPrecision=function(n,i){var r,o=this,t=o.constructor;return n===void 0?r=nt(o,o.e<=t.toExpNeg||o.e>=t.toExpPos):(He(n,1,dt),i===void 0?i=t.rounding:He(i,0,8),o=K(new t(o),n,i),r=nt(o,n<=o.e||o.e<=t.toExpNeg,n)),o.isNeg()&&!o.isZero()?"-"+r:r};N.toSignificantDigits=N.toSD=function(n,i){var r=this,o=r.constructor;return n===void 0?(n=o.precision,i=o.rounding):(He(n,1,dt),i===void 0?i=o.rounding:He(i,0,8)),K(new o(r),n,i)};N.toString=function(){var n=this,i=n.constructor,r=nt(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()&&!n.isZero()?"-"+r:r};N.truncated=N.trunc=function(){return K(new this.constructor(this),this.e+1,1)};N.valueOf=N.toJSON=function(){var n=this,i=n.constructor,r=nt(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()?"-"+r:r};function $e(n){var i,r,o,t=n.length-1,u="",p=n[0];if(t>0){for(u+=p,i=1;i<t;i++)o=n[i]+"",r=H-o.length,r&&(u+=ut(r)),u+=o;p=n[i],o=p+"",r=H-o.length,r&&(u+=ut(r))}else if(p===0)return"0";for(;p%10===0;)p/=10;return u+p}function He(n,i,r){if(n!==~~n||n<i||n>r)throw Error(ct+n)}function At(n,i,r,o){var t,u,p,c;for(u=n[0];u>=10;u/=10)--i;return--i<0?(i+=H,t=0):(t=Math.ceil((i+1)/H),i%=H),u=Fe(10,H-i),c=n[t]%u|0,o==null?i<3?(i==0?c=c/100|0:i==1&&(c=c/10|0),p=r<4&&c==99999||r>3&&c==49999||c==5e4||c==0):p=(r<4&&c+1==u||r>3&&c+1==u/2)&&(n[t+1]/u/100|0)==Fe(10,i-2)-1||(c==u/2||c==0)&&(n[t+1]/u/100|0)==0:i<4?(i==0?c=c/1e3|0:i==1?c=c/100|0:i==2&&(c=c/10|0),p=(o||r<4)&&c==9999||!o&&r>3&&c==4999):p=((o||r<4)&&c+1==u||!o&&r>3&&c+1==u/2)&&(n[t+1]/u/1e3|0)==Fe(10,i-3)-1,p}function Ot(n,i,r){for(var o,t=[0],u,p=0,c=n.length;p<c;){for(u=t.length;u--;)t[u]*=i;for(t[0]+=yr.indexOf(n.charAt(p++)),o=0;o<t.length;o++)t[o]>r-1&&(t[o+1]===void 0&&(t[o+1]=0),t[o+1]+=t[o]/r|0,t[o]%=r)}return t.reverse()}function pn(n,i){var r,o,t;if(i.isZero())return i;o=i.d.length,o<32?(r=Math.ceil(o/3),t=(1/Ut(4,r)).toString()):(r=16,t="2.3283064365386962890625e-10"),n.precision+=r,i=bt(n,1,i.times(t),new n(1));for(var u=r;u--;){var p=i.times(i);i=p.times(p).minus(p).times(8).plus(1)}return n.precision-=r,i}var ie=(function(){function n(o,t,u){var p,c=0,g=o.length;for(o=o.slice();g--;)p=o[g]*t+c,o[g]=p%u|0,c=p/u|0;return c&&o.unshift(c),o}function i(o,t,u,p){var c,g;if(u!=p)g=u>p?1:-1;else for(c=g=0;c<u;c++)if(o[c]!=t[c]){g=o[c]>t[c]?1:-1;break}return g}function r(o,t,u,p){for(var c=0;u--;)o[u]-=c,c=o[u]<t[u]?1:0,o[u]=c*p+o[u]-t[u];for(;!o[0]&&o.length>1;)o.shift()}return function(o,t,u,p,c,g){var M,b,S,C,T,D,O,B,R,x,q,j,W,$,z,m,y,A,P,L,h=o.constructor,e=o.s==t.s?1:-1,s=o.d,l=t.d;if(!s||!s[0]||!l||!l[0])return new h(!o.s||!t.s||(s?l&&s[0]==l[0]:!l)?NaN:s&&s[0]==0||!l?e*0:e/0);for(g?(T=1,b=o.e-t.e):(g=tt,T=H,b=ze(o.e/T)-ze(t.e/T)),P=l.length,y=s.length,R=new h(e),x=R.d=[],S=0;l[S]==(s[S]||0);S++);if(l[S]>(s[S]||0)&&b--,u==null?($=u=h.precision,p=h.rounding):c?$=u+(o.e-t.e)+1:$=u,$<0)x.push(1),D=!0;else{if($=$/T+2|0,S=0,P==1){for(C=0,l=l[0],$++;(S<y||C)&&$--;S++)z=C*g+(s[S]||0),x[S]=z/l|0,C=z%l|0;D=C||S<y}else{for(C=g/(l[0]+1)|0,C>1&&(l=n(l,C,g),s=n(s,C,g),P=l.length,y=s.length),m=P,q=s.slice(0,P),j=q.length;j<P;)q[j++]=0;L=l.slice(),L.unshift(0),A=l[0],l[1]>=g/2&&++A;do C=0,M=i(l,q,P,j),M<0?(W=q[0],P!=j&&(W=W*g+(q[1]||0)),C=W/A|0,C>1?(C>=g&&(C=g-1),O=n(l,C,g),B=O.length,j=q.length,M=i(O,q,B,j),M==1&&(C--,r(O,P<B?L:l,B,g))):(C==0&&(M=C=1),O=l.slice()),B=O.length,B<j&&O.unshift(0),r(q,O,j,g),M==-1&&(j=q.length,M=i(l,q,P,j),M<1&&(C++,r(q,P<j?L:l,j,g))),j=q.length):M===0&&(C++,q=[0]),x[S++]=C,M&&q[0]?q[j++]=s[m]||0:(q=[s[m]],j=1);while((m++<y||q[0]!==void 0)&&$--);D=q[0]!==void 0}x[0]||x.shift()}if(T==1)R.e=b,ai=D;else{for(S=1,C=x[0];C>=10;C/=10)S++;R.e=S+b*T-1,K(R,c?u+R.e+1:u,p,D)}return R}})();function K(n,i,r,o){var t,u,p,c,g,M,b,S,C,T=n.constructor;e:if(i!=null){if(S=n.d,!S)return n;for(t=1,c=S[0];c>=10;c/=10)t++;if(u=i-t,u<0)u+=H,p=i,b=S[C=0],g=b/Fe(10,t-p-1)%10|0;else if(C=Math.ceil((u+1)/H),c=S.length,C>=c)if(o){for(;c++<=C;)S.push(0);b=g=0,t=1,u%=H,p=u-H+1}else break e;else{for(b=c=S[C],t=1;c>=10;c/=10)t++;u%=H,p=u-H+t,g=p<0?0:b/Fe(10,t-p-1)%10|0}if(o=o||i<0||S[C+1]!==void 0||(p<0?b:b%Fe(10,t-p-1)),M=r<4?(g||o)&&(r==0||r==(n.s<0?3:2)):g>5||g==5&&(r==4||o||r==6&&(u>0?p>0?b/Fe(10,t-p):0:S[C-1])%10&1||r==(n.s<0?8:7)),i<1||!S[0])return S.length=0,M?(i-=n.e+1,S[0]=Fe(10,(H-i%H)%H),n.e=-i||0):S[0]=n.e=0,n;if(u==0?(S.length=C,c=1,C--):(S.length=C+1,c=Fe(10,H-u),S[C]=p>0?(b/Fe(10,t-p)%Fe(10,p)|0)*c:0),M)for(;;)if(C==0){for(u=1,p=S[0];p>=10;p/=10)u++;for(p=S[0]+=c,c=1;p>=10;p/=10)c++;u!=c&&(n.e++,S[0]==tt&&(S[0]=1));break}else{if(S[C]+=c,S[C]!=tt)break;S[C--]=0,c=1}for(u=S.length;S[--u]===0;)S.pop()}return V&&(n.e>T.maxE?(n.d=null,n.e=NaN):n.e<T.minE&&(n.e=0,n.d=[0])),n}function nt(n,i,r){if(!n.isFinite())return gi(n);var o,t=n.e,u=$e(n.d),p=u.length;return i?(r&&(o=r-p)>0?u=u.charAt(0)+"."+u.slice(1)+ut(o):p>1&&(u=u.charAt(0)+"."+u.slice(1)),u=u+(n.e<0?"e":"e+")+n.e):t<0?(u="0."+ut(-t-1)+u,r&&(o=r-p)>0&&(u+=ut(o))):t>=p?(u+=ut(t+1-p),r&&(o=r-t-1)>0&&(u=u+"."+ut(o))):((o=t+1)<p&&(u=u.slice(0,o)+"."+u.slice(o)),r&&(o=r-p)>0&&(t+1===p&&(u+="."),u+=ut(o))),u}function Ft(n,i){var r=n[0];for(i*=H;r>=10;r/=10)i++;return i}function Nt(n,i,r){if(i>hn)throw V=!0,r&&(n.precision=r),Error(li);return K(new n(Rt),i,1,!0)}function it(n,i,r){if(i>kr)throw Error(li);return K(new n(Bt),i,r,!0)}function di(n){var i=n.length-1,r=i*H+1;if(i=n[i],i){for(;i%10==0;i/=10)r--;for(i=n[0];i>=10;i/=10)r++}return r}function ut(n){for(var i="";n--;)i+="0";return i}function hi(n,i,r,o){var t,u=new n(1),p=Math.ceil(o/H+4);for(V=!1;;){if(r%2&&(u=u.times(i),oi(u.d,p)&&(t=!0)),r=ze(r/2),r===0){r=u.d.length-1,t&&u.d[r]===0&&++u.d[r];break}i=i.times(i),oi(i.d,p)}return V=!0,u}function ni(n){return n.d[n.d.length-1]&1}function pi(n,i,r){for(var o,t,u=new n(i[0]),p=0;++p<i.length;){if(t=new n(i[p]),!t.s){u=t;break}o=u.cmp(t),(o===r||o===0&&u.s===r)&&(u=t)}return u}function Ar(n,i){var r,o,t,u,p,c,g,M=0,b=0,S=0,C=n.constructor,T=C.rounding,D=C.precision;if(!n.d||!n.d[0]||n.e>17)return new C(n.d?n.d[0]?n.s<0?0:1/0:1:n.s?n.s<0?0:n:NaN);for(i==null?(V=!1,g=D):g=i,c=new C(.03125);n.e>-2;)n=n.times(c),S+=5;for(o=Math.log(Fe(2,S))/Math.LN10*2+5|0,g+=o,r=u=p=new C(1),C.precision=g;;){if(u=K(u.times(n),g,1),r=r.times(++b),c=p.plus(ie(u,r,g,1)),$e(c.d).slice(0,g)===$e(p.d).slice(0,g)){for(t=S;t--;)p=K(p.times(p),g,1);if(i==null)if(M<3&&At(p.d,g-o,T,M))C.precision=g+=10,r=u=c=new C(1),b=0,M++;else return K(p,C.precision=D,T,V=!0);else return C.precision=D,p}p=c}}function ft(n,i){var r,o,t,u,p,c,g,M,b,S,C,T=1,D=10,O=n,B=O.d,R=O.constructor,x=R.rounding,q=R.precision;if(O.s<0||!B||!B[0]||!O.e&&B[0]==1&&B.length==1)return new R(B&&!B[0]?-1/0:O.s!=1?NaN:B?0:O);if(i==null?(V=!1,b=q):b=i,R.precision=b+=D,r=$e(B),o=r.charAt(0),Math.abs(u=O.e)<15e14){for(;o<7&&o!=1||o==1&&r.charAt(1)>3;)O=O.times(n),r=$e(O.d),o=r.charAt(0),T++;u=O.e,o>1?(O=new R("0."+r),u++):O=new R(o+"."+r.slice(1))}else return M=Nt(R,b+2,q).times(u+""),O=ft(new R(o+"."+r.slice(1)),b-D).plus(M),R.precision=q,i==null?K(O,q,x,V=!0):O;for(S=O,g=p=O=ie(O.minus(1),O.plus(1),b,1),C=K(O.times(O),b,1),t=3;;){if(p=K(p.times(C),b,1),M=g.plus(ie(p,new R(t),b,1)),$e(M.d).slice(0,b)===$e(g.d).slice(0,b))if(g=g.times(2),u!==0&&(g=g.plus(Nt(R,b+2,q).times(u+""))),g=ie(g,new R(T),b,1),i==null)if(At(g.d,b-D,x,c))R.precision=b+=D,M=p=O=ie(S.minus(1),S.plus(1),b,1),C=K(O.times(O),b,1),t=c=1;else return K(g,R.precision=q,x,V=!0);else return R.precision=q,g;g=M,t+=2}}function gi(n){return String(n.s*n.s/0)}function It(n,i){var r,o,t;for((r=i.indexOf("."))>-1&&(i=i.replace(".","")),(o=i.search(/e/i))>0?(r<0&&(r=o),r+=+i.slice(o+1),i=i.substring(0,o)):r<0&&(r=i.length),o=0;i.charCodeAt(o)===48;o++);for(t=i.length;i.charCodeAt(t-1)===48;--t);if(i=i.slice(o,t),i){if(t-=o,n.e=r=r-o-1,n.d=[],o=(r+1)%H,r<0&&(o+=H),o<t){for(o&&n.d.push(+i.slice(0,o)),t-=H;o<t;)n.d.push(+i.slice(o,o+=H));i=i.slice(o),o=H-i.length}else o-=t;for(;o--;)i+="0";n.d.push(+i),V&&(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 gn(n,i){var r,o,t,u,p,c,g,M,b;if(i.indexOf("_")>-1){if(i=i.replace(/(\d)_(?=\d)/g,"$1"),ci.test(i))return It(n,i)}else if(i==="Infinity"||i==="NaN")return+i||(n.s=NaN),n.e=NaN,n.d=null,n;if(fn.test(i))r=16,i=i.toLowerCase();else if(un.test(i))r=2;else if(cn.test(i))r=8;else throw Error(ct+i);for(u=i.search(/p/i),u>0?(g=+i.slice(u+1),i=i.substring(2,u)):i=i.slice(2),u=i.indexOf("."),p=u>=0,o=n.constructor,p&&(i=i.replace(".",""),c=i.length,u=c-u,t=hi(o,new o(r),u,u*2)),M=Ot(i,r,tt),b=M.length-1,u=b;M[u]===0;--u)M.pop();return u<0?new o(n.s*0):(n.e=Ft(M,b),n.d=M,V=!1,p&&(n=ie(n,t,c*4)),g&&(n=n.times(Math.abs(g)<54?Fe(2,g):St.pow(2,g))),V=!0,n)}function mn(n,i){var r,o=i.d.length;if(o<3)return i.isZero()?i:bt(n,2,i,i);r=1.4*Math.sqrt(o),r=r>16?16:r|0,i=i.times(1/Ut(5,r)),i=bt(n,2,i,i);for(var t,u=new n(5),p=new n(16),c=new n(20);r--;)t=i.times(i),i=i.times(u.plus(t.times(p.times(t).minus(c))));return i}function bt(n,i,r,o,t){var u,p,c,g,M=1,b=n.precision,S=Math.ceil(b/H);for(V=!1,g=r.times(r),c=new n(o);;){if(p=ie(c.times(g),new n(i++*i++),b,1),c=t?o.plus(p):o.minus(p),o=ie(p.times(g),new n(i++*i++),b,1),p=c.plus(o),p.d[S]!==void 0){for(u=S;p.d[u]===c.d[u]&&u--;);if(u==-1)break}u=c,c=o,o=p,p=u,M++}return V=!0,p.d.length=S+1,p}function Ut(n,i){for(var r=n;--i;)r*=n;return r}function mi(n,i){var r,o=i.s<0,t=it(n,n.precision,1),u=t.times(.5);if(i=i.abs(),i.lte(u))return st=o?4:1,i;if(r=i.divToInt(t),r.isZero())st=o?3:2;else{if(i=i.minus(r.times(t)),i.lte(u))return st=ni(r)?o?2:3:o?4:1,i;st=ni(r)?o?1:4:o?3:2}return i.minus(t).abs()}function Sr(n,i,r,o){var t,u,p,c,g,M,b,S,C,T=n.constructor,D=r!==void 0;if(D?(He(r,1,dt),o===void 0?o=T.rounding:He(o,0,8)):(r=T.precision,o=T.rounding),!n.isFinite())b=gi(n);else{for(b=nt(n),p=b.indexOf("."),D?(t=2,i==16?r=r*4-3:i==8&&(r=r*3-2)):t=i,p>=0&&(b=b.replace(".",""),C=new T(1),C.e=b.length-p,C.d=Ot(nt(C),10,t),C.e=C.d.length),S=Ot(b,10,t),u=g=S.length;S[--g]==0;)S.pop();if(!S[0])b=D?"0p+0":"0";else{if(p<0?u--:(n=new T(n),n.d=S,n.e=u,n=ie(n,C,r,o,0,t),S=n.d,u=n.e,M=ai),p=S[r],c=t/2,M=M||S[r+1]!==void 0,M=o<4?(p!==void 0||M)&&(o===0||o===(n.s<0?3:2)):p>c||p===c&&(o===4||M||o===6&&S[r-1]&1||o===(n.s<0?8:7)),S.length=r,M)for(;++S[--r]>t-1;)S[r]=0,r||(++u,S.unshift(1));for(g=S.length;!S[g-1];--g);for(p=0,b="";p<g;p++)b+=yr.charAt(S[p]);if(D){if(g>1)if(i==16||i==8){for(p=i==16?4:3,--g;g%p;g++)b+="0";for(S=Ot(b,t,i),g=S.length;!S[g-1];--g);for(p=1,b="1.";p<g;p++)b+=yr.charAt(S[p])}else b=b.charAt(0)+"."+b.slice(1);b=b+(u<0?"p":"p+")+u}else if(u<0){for(;++u;)b="0"+b;b="0."+b}else if(++u>g)for(u-=g;u--;)b+="0";else u<g&&(b=b.slice(0,u)+"."+b.slice(u))}b=(i==16?"0x":i==2?"0b":i==8?"0o":"")+b}return n.s<0?"-"+b:b}function oi(n,i){if(n.length>i)return n.length=i,!0}function vn(n){return new this(n).abs()}function wn(n){return new this(n).acos()}function Mn(n){return new this(n).acosh()}function bn(n,i){return new this(n).plus(i)}function yn(n){return new this(n).asin()}function _n(n){return new this(n).asinh()}function kn(n){return new this(n).atan()}function An(n){return new this(n).atanh()}function Sn(n,i){n=new this(n),i=new this(i);var r,o=this.precision,t=this.rounding,u=o+4;return!n.s||!i.s?r=new this(NaN):!n.d&&!i.d?(r=it(this,u,1).times(i.s>0?.25:.75),r.s=n.s):!i.d||n.isZero()?(r=i.s<0?it(this,o,t):new this(0),r.s=n.s):!n.d||i.isZero()?(r=it(this,u,1).times(.5),r.s=n.s):i.s<0?(this.precision=u,this.rounding=1,r=this.atan(ie(n,i,u,1)),i=it(this,u,1),this.precision=o,this.rounding=t,r=n.s<0?r.minus(i):r.plus(i)):r=this.atan(ie(n,i,u,1)),r}function Cn(n){return new this(n).cbrt()}function Pn(n){return K(n=new this(n),n.e+1,2)}function Ln(n,i,r){return new this(n).clamp(i,r)}function Tn(n){if(!n||typeof n!="object")throw Error(Et+"Object expected");var i,r,o,t=n.defaults===!0,u=["precision",1,dt,"rounding",0,8,"toExpNeg",-Mt,0,"toExpPos",0,Mt,"maxE",0,Mt,"minE",-Mt,0,"modulo",0,9];for(i=0;i<u.length;i+=3)if(r=u[i],t&&(this[r]=_r[r]),(o=n[r])!==void 0)if(ze(o)===o&&o>=u[i+1]&&o<=u[i+2])this[r]=o;else throw Error(ct+r+": "+o);if(r="crypto",t&&(this[r]=_r[r]),(o=n[r])!==void 0)if(o===!0||o===!1||o===0||o===1)if(o)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[r]=!0;else throw Error(ui);else this[r]=!1;else throw Error(ct+r+": "+o);return this}function xn(n){return new this(n).cos()}function Dn(n){return new this(n).cosh()}function vi(n){var i,r,o;function t(u){var p,c,g,M=this;if(!(M instanceof t))return new t(u);if(M.constructor=t,si(u)){M.s=u.s,V?!u.d||u.e>t.maxE?(M.e=NaN,M.d=null):u.e<t.minE?(M.e=0,M.d=[0]):(M.e=u.e,M.d=u.d.slice()):(M.e=u.e,M.d=u.d?u.d.slice():u.d);return}if(g=typeof u,g==="number"){if(u===0){M.s=1/u<0?-1:1,M.e=0,M.d=[0];return}if(u<0?(u=-u,M.s=-1):M.s=1,u===~~u&&u<1e7){for(p=0,c=u;c>=10;c/=10)p++;V?p>t.maxE?(M.e=NaN,M.d=null):p<t.minE?(M.e=0,M.d=[0]):(M.e=p,M.d=[u]):(M.e=p,M.d=[u]);return}if(u*0!==0){u||(M.s=NaN),M.e=NaN,M.d=null;return}return It(M,u.toString())}if(g==="string")return(c=u.charCodeAt(0))===45?(u=u.slice(1),M.s=-1):(c===43&&(u=u.slice(1)),M.s=1),ci.test(u)?It(M,u):gn(M,u);if(g==="bigint")return u<0?(u=-u,M.s=-1):M.s=1,It(M,u.toString());throw Error(ct+u)}if(t.prototype=N,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=Tn,t.clone=vi,t.isDecimal=si,t.abs=vn,t.acos=wn,t.acosh=Mn,t.add=bn,t.asin=yn,t.asinh=_n,t.atan=kn,t.atanh=An,t.atan2=Sn,t.cbrt=Cn,t.ceil=Pn,t.clamp=Ln,t.cos=xn,t.cosh=Dn,t.div=On,t.exp=In,t.floor=Rn,t.hypot=Bn,t.ln=Nn,t.log=En,t.log10=Un,t.log2=Fn,t.max=qn,t.min=jn,t.mod=$n,t.mul=Zn,t.pow=Wn,t.random=zn,t.round=Kn,t.sign=Gn,t.sin=Hn,t.sinh=Vn,t.sqrt=Qn,t.sub=Xn,t.sum=Jn,t.tan=Yn,t.tanh=eo,t.trunc=to,n===void 0&&(n={}),n&&n.defaults!==!0)for(o=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],i=0;i<o.length;)n.hasOwnProperty(r=o[i++])||(n[r]=this[r]);return t.config(n),t}function On(n,i){return new this(n).div(i)}function In(n){return new this(n).exp()}function Rn(n){return K(n=new this(n),n.e+1,3)}function Bn(){var n,i,r=new this(0);for(V=!1,n=0;n<arguments.length;)if(i=new this(arguments[n++]),i.d)r.d&&(r=r.plus(i.times(i)));else{if(i.s)return V=!0,new this(1/0);r=i}return V=!0,r.sqrt()}function si(n){return n instanceof St||n&&n.toStringTag===fi||!1}function Nn(n){return new this(n).ln()}function En(n,i){return new this(n).log(i)}function Fn(n){return new this(n).log(2)}function Un(n){return new this(n).log(10)}function qn(){return pi(this,arguments,-1)}function jn(){return pi(this,arguments,1)}function $n(n,i){return new this(n).mod(i)}function Zn(n,i){return new this(n).mul(i)}function Wn(n,i){return new this(n).pow(i)}function zn(n){var i,r,o,t,u=0,p=new this(1),c=[];if(n===void 0?n=this.precision:He(n,1,dt),o=Math.ceil(n/H),this.crypto)if(crypto.getRandomValues)for(i=crypto.getRandomValues(new Uint32Array(o));u<o;)t=i[u],t>=429e7?i[u]=crypto.getRandomValues(new Uint32Array(1))[0]:c[u++]=t%1e7;else if(crypto.randomBytes){for(i=crypto.randomBytes(o*=4);u<o;)t=i[u]+(i[u+1]<<8)+(i[u+2]<<16)+((i[u+3]&127)<<24),t>=214e7?crypto.randomBytes(4).copy(i,u):(c.push(t%1e7),u+=4);u=o/4}else throw Error(ui);else for(;u<o;)c[u++]=Math.random()*1e7|0;for(o=c[--u],n%=H,o&&n&&(t=Fe(10,H-n),c[u]=(o/t|0)*t);c[u]===0;u--)c.pop();if(u<0)r=0,c=[0];else{for(r=-1;c[0]===0;r-=H)c.shift();for(o=1,t=c[0];t>=10;t/=10)o++;o<H&&(r-=H-o)}return p.e=r,p.d=c,p}function Kn(n){return K(n=new this(n),n.e+1,this.rounding)}function Gn(n){return n=new this(n),n.d?n.d[0]?n.s:0*n.s:n.s||NaN}function Hn(n){return new this(n).sin()}function Vn(n){return new this(n).sinh()}function Qn(n){return new this(n).sqrt()}function Xn(n,i){return new this(n).sub(i)}function Jn(){var n=0,i=arguments,r=new this(i[n]);for(V=!1;r.s&&++n<i.length;)r=r.plus(i[n]);return V=!0,K(r,this.precision,this.rounding)}function Yn(n){return new this(n).tan()}function eo(n){return new this(n).tanh()}function to(n){return K(n=new this(n),n.e+1,1)}N[Symbol.for("nodejs.util.inspect.custom")]=N.toString;N[Symbol.toStringTag]="Decimal";var St=N.constructor=vi(_r);Rt=new St(Rt);Bt=new St(Bt);var Ue=St;var Dr=Ei(Mi());import{Transaction as rt}from"@mysten/sui/transactions";import{addHexPrefix as bi,d as ht,getObjectFields as yi}from"@cetusprotocol/common-sdk";import{parseObligation as ro}from"@suilend/sdk/parsers/obligation";import{getNetAprPercent as io,getTotalAprPercent as no,Side as oo,getFilteredRewards as so}from"@suilend/sdk";var Pr=n=>{let i=yi(n),{openPermissionsPause:r,closePermissionsPause:o,depositPermissionsPause:t,withdrawPermissionsPause:u,borrowPermissionsPause:p,repayPermissionsPause:c}=xr(i.permissions.toString(2));return{marketId:i.id.id,baseToken:bi(i.base_token.fields.name),quoteToken:bi(i.quote_token.fields.name),maxLongLeverage:ht(i.max_long_leverage).div(10**6).toString(),maxShortLeverage:ht(i.max_short_leverage).div(10**6).toString(),openFeeRate:ht(i.open_fee_rate).div(10**6).toString(),closeFeeRate:ht(i.close_fee_rate).div(10**6).toString(),openPermissionsPause:r,closePermissionsPause:o,depositPermissionsPause:t,withdrawPermissionsPause:u,borrowPermissionsPause:p,repayPermissionsPause:c}},Lr=(n,i)=>{let r=yi(n);return{createdAt:r.created_ts,positionId:r.id.id,initDepositAmount:r.init_deposit_amount,isLong:r.is_long,lendingMarketId:r.lending_market_id,obligationOwnerCap:r.obligation_owner_cap.fields.obligation_id,marketId:r.market_id,positionCapId:i}},Tr=(n,i,r,o,t)=>{let u=ro(i,r),p=io(u,t,o);if(!u.deposits||u.deposits.length===0)throw new Error("No deposits found in obligation data");let c=u.deposits[0],g=ht(c.reserve.config.closeLtvPct).div(100).toString(),M=ht(c.reserve.config.openLtvPct).div(100).toString(),b=u.depositedAmountUsd.gt(0)?u.borrowedAmountUsd.div(u.depositedAmountUsd).toString():"0",S=ht(1).div(g).toString(),C=ht(g).sub(b).toString(),T="0",D="0",O="0";if(u.borrows&&u.borrows.length>0){let B=u.borrows[0],R=t[B?.coinType];R&&(T=no(oo.BORROW,B?.reserve.borrowAprPercent,so(R.borrow)).toString()),D=B.borrowedAmountUsd.toString(),O=B.borrowedAmount.toString()}return{...n,...u,originObligation:i,netAprPercent:p.toString(),suppliedAPR:c.reserve.depositAprPercent.toString(),borrowedAPR:T,closeLTV:g,openLTV:M,cr:b,ltr:S,liquidationBuffer:C,depositedAmountUsd:c.depositedAmountUsd.toString(),borrowedAmountUsd:D,depositedAmount:c.depositedAmount.toString(),borrowedAmount:O}},xr=n=>{let i=n.length,r=n.substring(i-1,i)==="0",o=n.substring(i-2,i-1)==="0",t=n.substring(i-3,i-2)==="0",u=n.substring(i-4,i-3)==="0",p=n.substring(i-5,i-4)==="0",c=n.substring(i-6,i-5)==="0";return{openPermissionsPause:r,closePermissionsPause:o,depositPermissionsPause:t,withdrawPermissionsPause:u,borrowPermissionsPause:p,repayPermissionsPause:c}};import{SUI_SYSTEM_STATE_OBJECT_ID as Si}from"@mysten/sui/utils";import{compoundDebt as Ci,getFilteredRewards as qt,getStakingYieldAprPercent as Pi,getTotalAprPercent as jt,Side as _t}from"@suilend/sdk";var $t=class{constructor(i){this.getLeverageConfig=async()=>{let{package_id:i}=this._sdk.sdkOptions.leverage,r=ne(this._sdk.sdkOptions.leverage);try{let o=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::versioned::InitEvent`}),t=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::admin_cap::InitEvent`}),u=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::config::InitEvent`}),c=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::market::InitEvent`})).data[0].parsedJson.markets_id,M=(await this._sdk.FullClient.getObject({id:c,options:{showContent:!0}})).data.content.fields.list.fields.id.id;return{versioned:o.data[0].parsedJson.versioned,admin_cap_id:t.data[0].parsedJson.admin_cap_id,global_config_id:u.data[0].parsedJson.global_config_id,markets:c,markets_table_id:M}}catch(o){return console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:78 ~ LeverageModules ~ error:",o),{versioned:"",admin_cap_id:"",global_config_id:"",markets_table_id:"",markets:""}}};this.getLeverageMarketsList=async(i=!1)=>{let o=this._sdk.getCache("leverage_markets_list",i);if(o)return o;let{package_id:t}=this._sdk.sdkOptions.leverage,u=[];try{let p=`${t}::market::CreateMarketEvent`,c=await this._sdk.FullClient.queryEventsByPage({MoveEventType:p});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:98 ~ LeverageModules ~ objects:",c);let g=c.data.map(M=>M.parsedJson.market_id);if(g.length>0)return(await this._sdk.FullClient.batchGetObjects(g,{showContent:!0,showType:!0})).forEach(b=>{let S=Pr(b),C=`leverage_market_info_${S.marketId}`;this._sdk.updateCache(C,S),u.push(S)}),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:106 ~ LeverageModules ~ marketList:",u),u}catch(p){return console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:108 ~ LeverageModules ~ error:",p),[]}};this.getLeverageMarketSuilendInfo=async i=>{let{baseToken:r,quoteToken:o}=await this.getLeverageMarketInfo(i),{lendingMarketId:t}=ne(this._sdk.sdkOptions.suilend),{allLendingMarketData:u,lstAprPercentMap:p}=await this._sdk.SuiLendModule.getLendingMarketData(!0),{baseReserveMapInfo:c,quoteReserveMapInfo:g}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(r,o,u);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:140 ~ LeverageModules ~ baseReserveMapInfo:",c);let M=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:u,lstAprPercentMap:p},!0);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:100 ~ LeverageModules ~ obligations:",M);let b=M[t].rewardMap[r],S=M[t].rewardMap[o],{depositedAmountUsd:C,borrowedAmountUsd:T,depositAprPercent:D,borrowAprPercent:O,depositedAmount:B,borrowedAmount:R}=c,{depositLimit:x,borrowLimit:q}=c.config,{depositedAmountUsd:j,borrowedAmountUsd:W,depositAprPercent:$,borrowAprPercent:z,depositedAmount:m,borrowedAmount:y}=g,{depositLimit:A,borrowLimit:P}=g.config,L=jt(_t.DEPOSIT,D,qt(b.deposit),Pi(_t.DEPOSIT,r,p)),h=jt(_t.BORROW,O,qt(b.borrow)),e=jt(_t.DEPOSIT,$,qt(S.deposit),Pi(_t.DEPOSIT,r,p)),s=jt(_t.BORROW,z,qt(S.borrow)),l=c.config.borrowLimitUsd,f=g.config.borrowLimitUsd,d=I(C.toString()).sub(T.toString()).toString(),v=I(j.toString()).sub(W.toString()).toString(),k=I(x.toString()).sub(I(B.toString())).toString(),w=I(q.toString()).sub(I(R.toString())).toString(),a=I(A.toString()).sub(I(m.toString())).toString(),_=I(P.toString()).sub(I(y.toString())).toString();return{longLiquidity:d,shortLiquidity:v,baseTotalDepositAprPercent:L.toString(),baseTotalBorrowAprPercent:h.toString(),quoteTotalDepositAprPercent:e.toString(),quoteTotalBorrowAprPercent:s.toString(),baseTokenAvailableDepositAmount:k,baseTokenAvailableBorrowAmount:w,quoteTokenAvailableDepositAmount:a,quoteTokenAvailableBorrowAmount:_}};this.getLeverageMarketInfo=async(i,r=!1)=>{let o=`leverage_market_info_${i}`,t=this._sdk.getCache(o,r);if(t)return t;let u=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0}}),p=Pr(u);return this._sdk.updateCache(o,p,_i),p};this.getLeveragePositionList=async(i=this._sdk.getSenderAddress())=>{let r=await this._sdk.FullClient.getOwnedObjectsByPage(i,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{MatchAny:[{Package:this._sdk.sdkOptions.leverage.package_id}]}});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:126 ~ LeverageModules ~ ownerRes:",r);let{lendingMarketId:o,lendingMarketType:t}=ne(this._sdk.sdkOptions.suilend),u=await this._sdk.SuiLendModule.getSuilendClient(o,t),{allLendingMarketData:p,lstAprPercentMap:c}=await this._sdk.SuiLendModule.getLendingMarketData(),g=p[o].reserveMap,M=[],b=[],S=[];for(let D=0;D<r.data.length;D++){let O=ki(r.data[D]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:259 ~ LeverageModules ~ fields:",O),O.position_id&&(b.push(O.position_id),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:260 ~ LeverageModules ~ positionIdList:",b),M.push(O.id.id))}let C=await this._sdk.FullClient.batchGetObjects(b,{showContent:!0}),T=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:p,lstAprPercentMap:c},!0);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:269 ~ LeverageModules ~ obligations:",T);for(let D=0;D<C.length;D++){let O=Lr(C[D],M[D]),B=await u.getObligation(O.obligationOwnerCap);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:250 ~ LeverageModules ~ rawObligation:",B);let R=Tr(O,B,g||{},c||{},T[o].rewardMap);S.push({...R})}return S};this.getLeveragePosition=async(i,r=this._sdk.getSenderAddress(),o=!0)=>{console.log("\u{1F680} ~ LeverageModules ~ walletAddress:",r);let t=`leverage_position_${i}`,u=this._sdk.getCache(t,o);if(u)return u;let p=await this._sdk.FullClient.getOwnedObjectsByPage(r,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{ObjectId:i}}),c=ki(p.data[0]),g=await this._sdk.FullClient.batchGetObjects([c.position_id],{showContent:!0}),M=Lr(g[0],i),{lendingMarketId:b,lendingMarketType:S}=ne(this._sdk.sdkOptions.suilend),C=await this._sdk.SuiLendModule.getSuilendClient(b,S),{allLendingMarketData:T,lstAprPercentMap:D}=await this._sdk.SuiLendModule.getLendingMarketData(),O=T[b].reserveMap,B=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:T,lstAprPercentMap:D},!0),R=await C.getObligation(M.obligationOwnerCap),q={...Tr(M,R,O||{},D||{},B[b].rewardMap)};return this._sdk.updateCache(t,q,_i),q};this.getClmmPoolInfo=async i=>{let r=await this._sdk.ClmmSDK.Pool.getPool(i,!1),{coin_type_a:o,coin_type_b:t,fee_rate:u}=r;return{clmmFeeTier:u/1e6,clmmPoolCoinTypeA:o,clmmPoolCoinTypeB:t}};this.openLeveragePosition=async i=>{let{isQuote:r,isLong:o,amount:t,swapClmmPool:u="",slippage:p,leverage:c,marketId:g}=i,{lendingMarketId:M}=ne(this._sdk.sdkOptions.suilend),b=new rt,{openFeeRate:S,baseToken:C,quoteToken:T}=await this.getLeverageMarketInfo(g),{baseReserveArrayIndex:D,quoteReserveArrayIndex:O,reserve:B}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(C,T),R=B[0].mintDecimals,x=B[1].mintDecimals,{inputCoin:q,initDepositAmount:j}=await this.handleSwapLogic({isLong:o,isQuote:r,amount:t,baseToken:C,quoteToken:T,swapClmmPool:u,slippage:p,tx:b}),{flashAmount:W,flashLoanCoin:$,isFlashA:z,flashLoanFee:m,clmmPoolCoinTypeA:y,clmmPoolCoinTypeB:A,clmmPool:P}=await this.calculateFlashLoanParams({isLong:o,leverage:c,baseToken:C,quoteToken:T,initDepositAmount:j,reserve:B,baseTokenDecimal:R,quoteTokenDecimal:x,openFeeRate:S});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:489 ~ LeverageModules ~ flashAmount:",W);let{balanceA:L,balanceB:h,receipt:e}=this.flashLoan({amount:I(W),clmmPool:P,clmmPoolCoinTypeA:y,clmmPoolCoinTypeB:A,flashLoanCoin:$,tx:b}),{debtSwapAmountOut:s,debtSwapOutCoin:l}=await this.handleDebtSwapLogic({isLong:o,flashAmount:W,baseToken:C,quoteToken:T,swapClmmPool:u,isFlashA:z,balanceA:L,balanceB:h,slippage:p,clmmPoolCoinTypeA:y,clmmPoolCoinTypeB:A,tx:b});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:528 ~ LeverageModules ~ debtSwapAmountOut:",s.toString());let f=I(s).add(j).mul(1-S).toDP(0,Ue.ROUND_DOWN).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:550 ~ LeverageModules ~ totalDepositAmount:",f),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:528 ~ LeverageModules ~ debtSwapAmountOut:",s);let{positionCap:d,marginTradingContext:v}=this.createLeveragePosition({leverage:c,marketId:g,baseToken:C,quoteToken:T,isLong:o,initDepositAmount:I(t).toString(),initCoinType:r?T:C,tx:b});await this._sdk.SuiLendModule.refreshReservePrices(b,B);let k=o?D:O;await b.mergeCoins(l,[q]),await this.depositToLeveragePosition({isLong:o,marketId:g,positionCap:d,depositReserveArrayIndex:k,inputCoin:l,marginTradingContext:v},b);let w=I(W).add(m).toDP(0,Ue.ROUND_UP).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:407 ~ LeverageModules ~ borrowAmount:",{flashAmount:W,flashLoanFee:m,clmmPool:P});let a=this.borrowAsset({positionCap:d,reserveArrayIndex:o?O:D,borrowAmount:I(w),baseToken:C,quoteToken:T,isLong:o,lendingMarketId:M,marketId:g,marginTradingContext:v},b),_=Y.mintBalanceZero(z?A:y,b);return this.repayFlashLoan({tx:b,clmmPool:P,repayBase:z?Y.intoBalance(a,y,b):_,repayQuote:z?_:Y.intoBalance(a,A,b),receipt:e,clmmPoolCoinTypeA:y,clmmPoolCoinTypeB:A}),b.transferObjects([d],b.pure.address(this._sdk.getSenderAddress())),Y.destroyBalanceZero(z?h:L,z?A:y,b),await this.marginTradingContextConfirm(v,b),b};this.positionManageSizeDeposit=async i=>{let{marketId:r,positionCapId:o,swapClmmPool:t="",isQuote:u,amount:p,slippage:c,leverage:g,txb:M}=i,{lendingMarketId:b}=ne(this._sdk.sdkOptions.suilend),{baseToken:S,quoteToken:C}=await this.getLeverageMarketInfo(r),{isLong:T,deposits:D,borrows:O}=await this.getLeveragePosition(o),{reserve:B}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(S,C),R=M||new rt,x=this.createMarginTradingContext({marketId:r,positionCapId:o,action:"increase_size",coinType:u?C:S,amount:p.toString()},R),q=D[0].reserveArrayIndex.toString(),j=O[0].reserveArrayIndex.toString(),{inputCoin:W,initDepositAmount:$}=await this.handleSwapLogic({isLong:T,isQuote:u,amount:p,baseToken:S,quoteToken:C,swapClmmPool:t,slippage:c,tx:R}),{flashAmount:z,flashLoanFee:m,flashLoanCoin:y,isFlashA:A,borrowAmount:P}=await this.calculatePositionDeposit({marketId:r,isLong:T,isQuote:u,amount:I(p),swapClmmPool:t,leverage:g,slippage:c}),{clmmPoolCoinTypeA:L,clmmPoolCoinTypeB:h,clmmPool:e}=await this.getFlashLoanPool(y,z);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:492 ~ LeverageModules ~ flashLoanCoin:",y),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:492 ~ LeverageModules ~ borrowAmount:",P);let{balanceA:s,balanceB:l,receipt:f}=this.flashLoan({amount:I(z),clmmPool:e,clmmPoolCoinTypeA:L,clmmPoolCoinTypeB:h,flashLoanCoin:y,tx:R}),{debtSwapAmountOut:d,debtSwapOutCoin:v}=await this.handleDebtSwapLogic({isLong:T,flashAmount:z,baseToken:S,quoteToken:C,swapClmmPool:t,isFlashA:A,balanceA:s,balanceB:l,slippage:c,clmmPoolCoinTypeA:L,clmmPoolCoinTypeB:h,tx:R});R.mergeCoins(v,[W]),await this._sdk.SuiLendModule.refreshReservePrices(R,B),await this.depositToLeveragePosition({isLong:T,marketId:r,positionCapId:o,depositReserveArrayIndex:q,inputCoin:v,marginTradingContext:x},R),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:531 ~ LeverageModules ~ borrowAmount:",P);let k=this.borrowAsset({positionCapId:o,reserveArrayIndex:j,borrowAmount:I(P),baseToken:S,quoteToken:C,isLong:T,lendingMarketId:b,marketId:r,marginTradingContext:x},R),w=Y.mintBalanceZero(A?h:L,R);return this.repayFlashLoan({tx:R,clmmPool:e,repayBase:A?Y.intoBalance(k,L,R):w,repayQuote:A?w:Y.intoBalance(k,h,R),receipt:f,clmmPoolCoinTypeA:L,clmmPoolCoinTypeB:h}),Y.destroyBalanceZero(A?l:s,A?h:L,R),await this.marginTradingContextConfirm(x,R),R};this.positionManageSizeWithdraw=async i=>{let{marketId:r,amount:o,isQuote:t,txb:u,swapClmmPool:p="",slippage:c,positionCapId:g,isLong:M,leverage:b}=i,{lendingMarketType:S,lendingMarketId:C}=ne(this._sdk.sdkOptions.suilend),T=u||new rt,{withdrawCtokenAmount:D,repayAmount:O,isClose:B,deposits:R,borrows:x,reserve:q,baseToken:j,quoteToken:W,clmmPool:$,clmmPoolCoinTypeA:z,clmmPoolCoinTypeB:m,clmmFeeTier:y}=await this.calculatePositionWithdraw({marketId:r,positionCapId:g,isLong:M,isQuote:t,swapClmmPool:p,amount:o,leverage:b});if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:575 ~ LeverageModules ~ withdrawAmount:",D),B)return this.leveragePositionCloseWithCoin({marketId:r,positionCapId:g,isQuote:t,slippage:c,swapClmmPool:p});let A=this.createMarginTradingContext({marketId:r,positionCapId:g,action:"decrease_size",coinType:t?W:j,amount:o.toString()},T);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:669 ~ LeverageModules ~ repayAmount:",O),await this._sdk.SuiLendModule.refreshReservePrices(T,q);let{balanceA:P,balanceB:L,receipt:h,isLoanA:e,loanCoinType:s}=this.flashLoan({tx:T,amount:I(O),flashLoanCoin:x[0].reserve.coinType,clmmPool:$,clmmPoolCoinTypeA:z,clmmPoolCoinTypeB:m}),l=e?Y.fromBalance(P,z,T):Y.fromBalance(L,m,T);this.repay({txb:T,positionCapId:g,repayReserveArrayIndex:x[0].reserveArrayIndex.toString(),repayCoin:l,repayCoinType:M?W:j,repayAmount:I(0),marketId:r,marginTradingContext:A}),T.transferObjects([l],this._sdk.getSenderAddress());let f=await this._sdk.SuiLendModule.getSuilendClient(C,S),d=await this._sdk.PythConnection.getPriceFeedsUpdateData([R[0].reserve.priceIdentifier]),v=await f.pythClient.updatePriceFeeds(T,d,[R[0].reserve.priceIdentifier]),k=this.withdrawAsset({marketId:r,positionCapId:g,withdrawAmount:D.toString(),withdrawReserveArrayIndex:R[0].reserveArrayIndex.toString(),withdrawCoinType:M?j:W,baseToken:j,quoteToken:W,priceObjectId:v&&v[0],marginTradingContext:A},T),w=M?j:W,a,_;if(w!==s){_=I(O).add(I(O).mul(y)).toDP(0,Ue.ROUND_UP).toString();let G=await this.findRouters(w,s,I(_),!1,[p]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:748 ~ LeverageModules ~ routers:",{amountIn:G?.amount_in.toString(),amountOut:G?.amount_out.toString(),withdrawAmount:D}),_=G?.amount_in.toString();let te=T.splitCoins(k,[G?.amount_in.toString()]);a=await this.routerSwap({router:G?.route_obj,inputCoin:te,slippage:c,txb:T})}else a=T.splitCoins(k,[O]),_=O;let F=Y.mintBalanceZero(e?m:z,T);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:761 ~ LeverageModules ~ clmmPoolCoinTypeA:",z),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:761 ~ LeverageModules ~ clmmPoolCoinTypeB:",m),a&&this.repayFlashLoan({tx:T,clmmPool:$,repayBase:e?Y.intoBalance(a,z,T):F,repayQuote:e?F:Y.intoBalance(a,m,T),clmmPoolCoinTypeA:z,clmmPoolCoinTypeB:m,receipt:h});let E=t?W:j,U=I(D).sub(I(_)).toString(),Z;if(E!==w){let G=await this.findRouters(w,E,I(U),!0,[p]);Z=await this.routerSwap({router:G?.route_obj,inputCoin:k,slippage:c,txb:T})}else Z=k;return Z&&T.transferObjects([Z],this._sdk.getSenderAddress()),Y.destroyBalanceZero(e?L:P,e?m:z,T),await this.marginTradingContextConfirm(A,T),T};this.positionManageLeverageIncrease=async i=>{let{marketId:r,positionCapId:o,isLong:t,currentLeverage:u,targetLeverage:p,swapClmmPool:c="",slippage:g}=i,{lendingMarketId:M}=ne(this._sdk.sdkOptions.suilend),b=new rt,{flashLoanAmount:S,quoteToken:C,baseToken:T,reserve:D,deposits:O,borrows:B,borrowAmount:R,isFlashLoan:x}=await this.calculatePositionLeverage({marketId:r,positionCapId:o,currentLeverage:u,targetLeverage:p,swapClmmPool:c,slippage:g}),q=t?C:T,{clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W,clmmPool:$}=await this.getFlashLoanPool(q,S?.toString()||"");console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:896 ~ LeverageModules ~ clmmPoolCoinTypeA:",{clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W});let z=this.createMarginTradingContext({marketId:r,positionCapId:o,action:"increase_leverage",coinType:t?T:C,amount:"0"},b);if(D&&await this._sdk.SuiLendModule.refreshReservePrices(b,D),x){let{balanceA:m,balanceB:y,receipt:A,isLoanA:P}=this.flashLoan({tx:b,amount:I(S),clmmPool:$,clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W,flashLoanCoin:q}),L=t?C:T,h=t?T:C,e=await this.findRouters(L,h,I(S),!0,[c]),s=await this.routerSwap({router:e?.route_obj,inputCoin:P?Y.fromBalance(m,j,b):Y.fromBalance(y,W,b),slippage:g,txb:b});await this.depositToLeveragePosition({isLong:t,marketId:r,positionCapId:o,depositReserveArrayIndex:O[0].reserveArrayIndex.toString(),inputCoin:s,marginTradingContext:z},b);let l=await this.borrowAsset({positionCapId:o,reserveArrayIndex:t?D[1].arrayIndex.toString():D[0].arrayIndex.toString(),borrowAmount:I(R),baseToken:T,quoteToken:C,isLong:t,lendingMarketId:M,marketId:r,marginTradingContext:z},b),f=Y.mintBalanceZero(P?W:j,b);this.repayFlashLoan({tx:b,clmmPool:$,repayBase:P?Y.intoBalance(l,j,b):f,repayQuote:P?f:Y.intoBalance(l,W,b),receipt:A,clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W}),Y.destroyBalanceZero(P?y:m,P?W:j,b)}else{let m=await this.borrowAsset({positionCapId:o,reserveArrayIndex:t?D[1].arrayIndex.toString():D[0].arrayIndex.toString(),borrowAmount:I(R),baseToken:T,quoteToken:C,isLong:t,lendingMarketId:M,marketId:r,marginTradingContext:z},b),y=await this.findRouters(t?C:T,t?T:C,I(R),!0,[c]),A=await this.routerSwap({router:y?.route_obj,inputCoin:m,slippage:g,txb:b});await this.depositToLeveragePosition({isLong:t,marketId:r,positionCapId:o,depositReserveArrayIndex:O[0].reserveArrayIndex.toString(),inputCoin:A,marginTradingContext:z},b)}return await this.marginTradingContextConfirm(z,b),b};this.positionManageLeverageDecrease=async i=>{let{marketId:r,positionCapId:o,isLong:t,currentLeverage:u,targetLeverage:p,swapClmmPool:c="",slippage:g}=i,{baseToken:M,quoteToken:b,closeFeeRate:S}=await this.getLeverageMarketInfo(r),{deposits:C,borrows:T}=await this.getLeveragePosition(o),{reserve:D}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(M,b),{lendingMarketId:O,lendingMarketType:B}=ne(this._sdk.sdkOptions.suilend),R=new rt,{withdrawCtokenAmount:x,isFlashLoan:q}=await this.calculatePositionLeverage({marketId:r,positionCapId:o,currentLeverage:u,targetLeverage:p,swapClmmPool:c,slippage:g});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:654 ~ LeverageModules ~ withdrawCtokenAmount:",x,q);let j=this.createMarginTradingContext({marketId:r,positionCapId:o,action:"decrease_leverage",coinType:t?M:b,amount:"0"},R);await this._sdk.SuiLendModule.refreshReservePrices(R,D);let W=await this._sdk.SuiLendModule.getSuilendClient(O,B),$=await this._sdk.PythConnection.getPriceFeedsUpdateData([C[0].reserve.priceIdentifier]),z=await W.pythClient.updatePriceFeeds(R,$,[C[0].reserve.priceIdentifier]),{clmmPoolCoinTypeA:m,clmmPoolCoinTypeB:y,clmmPool:A,clmmFeeTier:P}=await this.getFlashLoanPool(C[0].reserve.coinType,x.toString());if(q){let{balanceA:L,balanceB:h,receipt:e,isLoanA:s}=this.flashLoan({tx:R,amount:I(x),clmmPool:A,clmmPoolCoinTypeA:m,clmmPoolCoinTypeB:y,flashLoanCoin:C[0].reserve.coinType});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1019 ~ LeverageModules ~ isLoanA:",s);let l=t?M:b,f=t?b:M,d=await this.findRouters(l,f,I(x),!0,[c]),v=await this.routerSwap({router:d?.route_obj,inputCoin:s?Y.fromBalance(L,m,R):Y.fromBalance(h,y,R),slippage:g,txb:R});await this.repay({txb:R,marketId:r,positionCapId:o,repayReserveArrayIndex:T[0].reserveArrayIndex.toString(),repayCoin:v,repayCoinType:t?b:M,repayAmount:I(0),marginTradingContext:j}),v&&R.transferObjects([v],this._sdk.getSenderAddress());let k=this.withdrawAsset({marketId:r,positionCapId:o,withdrawAmount:x.toString(),withdrawReserveArrayIndex:C[0].reserveArrayIndex.toString(),withdrawCoinType:t?M:b,baseToken:M,quoteToken:b,priceObjectId:z&&z[0],marginTradingContext:j},R),w=I(x).mul(I(1).add(P)).toDP(0,Ue.ROUND_UP),a=R.splitCoins(k,[w.toString()]);this.repayFlashLoan({tx:R,clmmPool:A,repayBase:s?Y.intoBalance(a,m,R):L,repayQuote:s?h:Y.intoBalance(a,y,R),receipt:e,clmmPoolCoinTypeA:m,clmmPoolCoinTypeB:y}),R.transferObjects([k],this._sdk.getSenderAddress())}else{let L=this.withdrawAsset({marketId:r,positionCapId:o,withdrawAmount:x.toString(),withdrawReserveArrayIndex:C[0].reserveArrayIndex.toString(),withdrawCoinType:t?M:b,baseToken:M,quoteToken:b,priceObjectId:z&&z[0],marginTradingContext:j},R),h=t?M:b,e=t?b:M,s=await this.findRouters(h,e,I(x),!0,[c]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:681 ~ LeverageModules ~ routers:",s,s?.route_obj.paths,h,e);let l=await this.routerSwap({router:s?.route_obj,inputCoin:L,slippage:g,txb:R});this.repay({txb:R,positionCapId:o,repayReserveArrayIndex:T[0].reserveArrayIndex.toString(),repayCoin:l,repayCoinType:t?b:M,repayAmount:I(0),marketId:r,marginTradingContext:j}),l&&R.transferObjects([l],this._sdk.getSenderAddress())}return await this.marginTradingContextConfirm(j,R),R};this.leveragePositionCloseWithCoin=async i=>{let{marketId:r,positionCapId:o,isQuote:t,slippage:u,swapClmmPool:p=""}=i,{baseToken:c,quoteToken:g,closeFeeRate:M}=await this.getLeverageMarketInfo(r),{deposits:b,borrows:S,isLong:C,originObligation:T}=await this.getLeveragePosition(o);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:847 ~ LeverageModules ~ borrows:",S);let D=new rt,O=t?g:c,B=C?c!==O:g!==O,{reserve:R}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(c,g),x=this.createMarginTradingContext({marketId:r,positionCapId:o,action:"close_position",coinType:t?g:c,amount:"0"},D);await this._sdk.SuiLendModule.refreshReservePrices(D,R);let q=T.borrows.length>0,j,W,$,z,m=I(0),y,A,P,L;if(q){let v=Ci(T.borrows[0],C?R[1]:R[0]),k=I(v.borrowedAmount.value.toString()).div(10**18).toDP(0,Ue.ROUND_UP).add(100),w=k,a=await this.getFlashLoanPool(S[0].reserve.coinType,w.toString());j=a.clmmPoolCoinTypeA,W=a.clmmPoolCoinTypeB,$=a.clmmPool,z=a.clmmFeeTier;let _=w.mul(z);m=w.add(_).toDP(0,Ue.ROUND_UP);let F=this.flashLoan({tx:D,amount:k,clmmPool:$,clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W,flashLoanCoin:S[0].reserve.coinType});A=F.balanceA,P=F.balanceB,L=F.receipt,y=F.isLoanA;let E=y?Y.fromBalance(A,y?j:W,D):Y.fromBalance(P,y?j:W,D);this.repay({txb:D,positionCapId:o,repayReserveArrayIndex:S[0].reserveArrayIndex.toString(),repayCoin:E,repayCoinType:y?j:W,repayAmount:I(0),marketId:r,marginTradingContext:x}),D.transferObjects([E],this._sdk.getSenderAddress())}let{lendingMarketType:h,lendingMarketId:e}=ne(this._sdk.sdkOptions.suilend),s=await this._sdk.SuiLendModule.getSuilendClient(e,h),l=[];for(let v=0;v<b.length;v++){let k=await this._sdk.PythConnection.getPriceFeedsUpdateData([b[v].reserve.priceIdentifier]);l=await s.pythClient.updatePriceFeeds(D,k,[b[v].reserve.priceIdentifier])}let f=this.withdrawAsset({marketId:r,positionCapId:o,withdrawAmount:Ai.toString(),withdrawReserveArrayIndex:b[0].reserveArrayIndex.toString(),withdrawCoinType:C?c:g,baseToken:c,quoteToken:g,priceObjectId:l&&l[0],marginTradingContext:x},D);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1217 ~ LeverageModules ~ deposits:",b);for(let v=1;v<b.length;v++){let k=this.withdrawAsset({marketId:r,positionCapId:o,withdrawAmount:Ai.toString(),withdrawReserveArrayIndex:b[v].reserveArrayIndex.toString(),withdrawCoinType:b[v].coinType,baseToken:c,quoteToken:g,priceObjectId:l&&l[v],marginTradingContext:x},D);D.transferObjects([k],this._sdk.getSenderAddress())}let d=b[0].depositedCtokenAmount.toString();if(q){let v=y?j!==b[0].reserve.coinType:W!==b[0].reserve.coinType,k;if(v){let a=await this.findRouters(C?c:g,y?j:W,m,!1,[p]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:902 ~ LeverageModules ~ isLong ? baseToken : quoteToken:",{from:C?c:g,to:y?j:W,amountIn:a?.amount_in.toString(),amountOut:m.toString()});let _=D.splitCoins(f,[a?.amount_in.toString()]);k=await this.routerSwap({router:a?.route_obj,inputCoin:_,slippage:u,txb:D}),d=I(b[0].depositedCtokenAmount.toString()).sub(a?.amount_in.toString()).toString()}else k=D.splitCoins(f,[m.toString()]),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:928 ~ LeverageModules ~ flashLoanAmountTotal.toString():",m.toString(),b[0].depositedCtokenAmount.toString()),d=I(b[0].depositedCtokenAmount.toString()).sub(m).toString();let w=y?P:A;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:921 ~ LeverageModules ~ isLoanA:",y),k&&this.repayFlashLoan({tx:D,clmmPool:$,repayBase:y?Y.intoBalance(k,j,D):w,repayQuote:y?w:Y.intoBalance(k,W,D),receipt:L,clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W})}if(B){let v=await this.findRouters(C?c:g,O,I(d),!0,[p]),k=await this.routerSwap({router:v?.route_obj,inputCoin:f,slippage:u,txb:D});k&&D.transferObjects([k],this._sdk.getSenderAddress())}else D.transferObjects([f],this._sdk.getSenderAddress());return this.leveragePositionClose({marketId:r,positionCapId:o,marginTradingContext:x},D),await this.marginTradingContextConfirm(x,D),D};this.leveragePositionClose=async(i,r)=>{let o=r||new rt,{marketId:t,positionCapId:u,marginTradingContext:p}=i,{leverage:c}=this._sdk.sdkOptions,{global_config_id:g,versioned:M}=ne(this._sdk.sdkOptions.leverage),{lendingMarketType:b,lendingMarketId:S}=ne(this._sdk.sdkOptions.suilend);return o.moveCall({target:`${c.published_at}::router::close_position`,typeArguments:[b],arguments:[p,o.object(g),o.object(S),o.object(t),o.object(u),o.object(pt),o.object(M)]}),o};this.leveragePositionRepay=async i=>{let{marketId:r,positionCapId:o,amount:t,isQuote:u,swapClmmPool:p="",slippage:c}=i,{baseToken:g,quoteToken:M}=await this.getLeverageMarketInfo(r),{borrows:b}=await this.getLeveragePosition(o),S=new rt,C=b[0].reserve.coinType,T=u?M!==C:g!==C,D,O=this.createMarginTradingContext({marketId:r,positionCapId:o,action:"repay_debt",coinType:u?M:g,amount:t.toString()},S);if(T){let B=await this.findRouters(u?M:g,C,t,!0,[p]),R=Y.buildCoinWithBalance(BigInt(t.toString()),u?M:g,S);D=await this.routerSwap({router:B?.route_obj,inputCoin:R,slippage:c,txb:S})}else D=Y.buildCoinWithBalance(BigInt(t.toString()),u?M:g,S);return this.repay({txb:S,positionCapId:o,repayReserveArrayIndex:b[0].reserveArrayIndex.toString(),repayCoin:D,repayCoinType:C,repayAmount:I(0),marketId:r,marginTradingContext:O}),S.transferObjects([D],this._sdk.getSenderAddress()),await this.marginTradingContextConfirm(O,S),S};this.positionTopUpCoken=async i=>{let{marketId:r,positionCapId:o,amount:t,isQuote:u,swapClmmPool:p="",slippage:c}=i,g=new rt,{baseToken:M,quoteToken:b}=await this.getLeverageMarketInfo(r),{isLong:S}=await this.getLeveragePosition(o),C=S&&u||!S&&!u,T=await this.createMarginTradingContext({marketId:r,positionCapId:o,action:"top_up_collateral",coinType:u?b:M,amount:t.toString()},g),D;if(C){let x=await this.findRouters(u?b:M,u?M:b,t,!0,[p]),q=Y.buildCoinWithBalance(BigInt(t.toString()),u?b:M,g);D=await this.routerSwap({router:x?.route_obj,inputCoin:q,slippage:c,txb:g})}else D=Y.buildCoinWithBalance(BigInt(t.toString()),u?b:M,g);let{baseReserveArrayIndex:O,quoteReserveArrayIndex:B,reserve:R}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(M,b);return await this.depositToLeveragePosition({isLong:S,marketId:r,positionCapId:o,depositReserveArrayIndex:S?O:B,inputCoin:D,marginTradingContext:T},g),await this.marginTradingContextConfirm(T,g),g};this.flashLoan=i=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:698 ~ LeverageModules ~ params:",i);let{clmm_pool:r}=this._sdk.ClmmSDK.sdkOptions,{global_config_id:o}=ne(this._sdk.ClmmSDK.sdkOptions.clmm_pool),{amount:t,clmmPool:u,clmmPoolCoinTypeA:p,clmmPoolCoinTypeB:c,flashLoanCoin:g,tx:M}=i,b=ao(p)===g;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:983 ~ LeverageModules ~ isLoanA:",b);let[S,C,T]=M.moveCall({target:`${r.published_at}::pool::flash_loan`,arguments:[M.object(o),M.object(u),M.pure.bool(b),M.pure.u64(t.toString())],typeArguments:[p,c]});return{balanceA:S,balanceB:C,receipt:T,isLoanA:b,loanCoinType:b?p:c}};this.repayFlashLoan=i=>{let{clmm_pool:r}=this._sdk.ClmmSDK.sdkOptions,{global_config_id:o}=ne(this._sdk.ClmmSDK.sdkOptions.clmm_pool),{tx:t,repayBase:u,repayQuote:p,receipt:c,clmmPool:g,clmmPoolCoinTypeA:M,clmmPoolCoinTypeB:b}=i;return console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:612 ~ LeverageModules ~ params:",i),t.moveCall({target:`${r.published_at}::pool::repay_flash_loan`,arguments:[t.object(o),t.object(g),u,p,c],typeArguments:[M,b]}),t};this.borrowAsset=(i,r)=>{let{reserveArrayIndex:o,borrowAmount:t,baseToken:u,quoteToken:p,lendingMarketId:c,isLong:g,marketId:M,positionCapId:b,positionCap:S,marginTradingContext:C}=i;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:677 ~ LeverageModules ~ params:",i);let{leverage:T}=this._sdk.sdkOptions,{lendingMarketType:D}=ne(this._sdk.sdkOptions.suilend),{global_config_id:O,versioned:B}=ne(this._sdk.sdkOptions.leverage);return Y.isSuiCoin(g?p:u)?r.moveCall({target:`${T.published_at}::router::borrow_sui`,typeArguments:[D],arguments:[C,r.object(O),r.object(c),r.object(M),b?r.object(b):S,r.pure.u64(o),r.pure.u64(t.toString()),r.object(Si),r.object(pt),r.object(B)]}):r.moveCall({target:`${T.published_at}::router::borrow_not_sui`,typeArguments:[D,g?p:u],arguments:[C,r.object(O),r.object(c),r.object(M),b?r.object(b):S,r.pure.u64(o),r.pure.u64(t.toString()),r.object(pt),r.object(B)]})};this.withdrawAsset=(i,r)=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:749 ~ LeverageModules ~ params:",i);let{leverage:o}=this._sdk.sdkOptions,{lendingMarketId:t,lendingMarketType:u}=ne(this._sdk.sdkOptions.suilend),{global_config_id:p,versioned:c}=ne(this._sdk.sdkOptions.leverage),{package_id:g}=this._sdk.sdkOptions.suilend,{marketId:M,withdrawAmount:b,withdrawReserveArrayIndex:S,withdrawCoinType:C,baseToken:T,quoteToken:D,priceObjectId:O,positionCapId:B,marginTradingContext:R}=i;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1596 ~ LeverageModules ~ withdrawAmount:",b);let[x]=r.moveCall({target:"0x1::option::none",typeArguments:[`${g}::lending_market::RateLimiterExemption<${u}, ${C}>`],arguments:[]});return Y.isSuiCoin(C)?r.moveCall({target:`${o.published_at}::router::withdraw_sui`,typeArguments:[u],arguments:[R,r.object(p),r.object(t),r.object(M),r.object(B),r.object(x),r.pure.u64(b),r.pure.u64(S),r.object(Si),r.object(pt),r.object(c)]}):r.moveCall({target:`${o.published_at}::router::withdraw_not_sui`,typeArguments:[u,C],arguments:[R,r.object(p),r.object(t),r.object(M),r.object(B),r.object(x),r.pure.u64(b),r.pure.u64(S),r.object(pt),r.object(c)]})};this.repay=i=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1333 ~ LeverageModules ~ params:",i);let{leverage:r}=this._sdk.sdkOptions,{lendingMarketId:o,lendingMarketType:t}=ne(this._sdk.sdkOptions.suilend),{global_config_id:u,versioned:p}=ne(this._sdk.sdkOptions.leverage),{txb:c,repayAmount:g,repayCoinType:M,repayCoin:b,repayReserveArrayIndex:S,marketId:C,positionCapId:T,marginTradingContext:D}=i,O=c||new rt,B=b||Y.buildCoinWithBalance(BigInt(g.toString()),M,O);O.moveCall({target:`${r.published_at}::router::repay`,typeArguments:[t,M],arguments:[D,O.object(u),O.object(o),O.object(C),O.object(T),B,O.pure.u64(S.toString()),O.object(pt),O.object(p)]})};this.routerSwap=async i=>{let{slippage:r,txb:o,inputCoin:t,router:u}=i,p=o||new rt;if(u)return await this._sdk.AggregatorClient.routerSwap({router:u,inputCoin:t,slippage:r,txb:p})};this.calculatePositionDeposit=async i=>{let{isQuote:r,isLong:o,amount:t,swapClmmPool:u="",leverage:p,byAmountIn:c=!0,marketId:g}=i;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1514 ~ LeverageModules ~ amount:",t.toString());let{baseToken:M,quoteToken:b,openFeeRate:S}=await this.getLeverageMarketInfo(g),{reserve:C}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(M,b),T=C[0].mintDecimals,D=C[1].mintDecimals,O=t.toString();if(o&&r||!o&&!r){let s=o?b:M,l=o?M:b;O=(await this.findRouters(s,l,I(O),!0,[u]))?.amount_out.toString()}let{basePrice:R,quotePrice:x,flashAmount:q,flashLoanFee:j,flashLoanCoin:W,isFlashA:$,clmmPool:z,clmmFeeTier:m}=await this.calculateFlashLoanParams({isLong:o,leverage:p,baseToken:M,quoteToken:b,initDepositAmount:O,reserve:C,baseTokenDecimal:T,quoteTokenDecimal:D,openFeeRate:S});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1533 ~ LeverageModules ~ flashAmount:",q.toString(),O);let y=await this.findRouters(o?b:M,o?M:b,I(q),!0,[u]),A=I(O).add(I(y?.amount_out.toString())).toDP(0,Ue.ROUND_DOWN).toString(),P=I(A).mul(o?R:x).toString(),L=I(q).add(j).toDP(0,Ue.ROUND_UP).toString(),h=I(L).mul(o?x:R).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1541 ~ LeverageModules ~ isQuote:",r);let e=r?I(t).div(10**(r?D:T)).mul(10**T).mul(p).div(R).toDP(0,Ue.ROUND_DOWN).toString():I(t).mul(p).toDP(0,Ue.ROUND_DOWN).toString();return{routers:y,leverageDepositAmount:e,quotePrice:x,basePrice:R,clmmFeeTier:m,flashLoanFee:j,flashLoanCoin:W,depositAmount:A,borrowAmount:L,depositAmountUSD:P,borrowAmountUSD:h,flashAmount:q,isFlashA:$,initDepositAmount:O}};this.calculatePositionWithdraw=async i=>{let{marketId:r,positionCapId:o,isLong:t,isQuote:u,swapClmmPool:p="",amount:c,leverage:g}=i,{baseToken:M,quoteToken:b}=await this.getLeverageMarketInfo(r),{deposits:S,borrows:C,originObligation:T}=await this.getLeveragePosition(o),{reserve:D,baseReserveMapInfo:O,quoteReserveMapInfo:B}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(M,b);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1951 ~ LeverageModules ~ reserve:",D);let R=D[0].mintDecimals,x=D[1].mintDecimals,q=t?O.cTokenExchangeRate:B.cTokenExchangeRate;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1955 ~ LeverageModules ~ ctokenExchangeRate:",q.toString());let j=await this._sdk.SuiLendModule.getLatestPriceFeeds(D,!0),W=j[yt(b)]?.price,$=j[yt(M)]?.price,z=I(0),m;t?(z=I(u?c.mul(W).toString():c.mul($).toString()).div(u?10**x:10**R),m=u?c.div($).div(10**x).mul(10**R).toString():c):(z=I(u?c.mul(W).toString():c.mul($).toString()).div(u?10**x:10**R),m=u?c:c.mul($).div(10**R).mul(10**x).toString()),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1973 ~ LeverageModules ~ withdrawAmount:",m);let y=I(S[0].depositedAmountUsd.toString()).sub(I(C[0].borrowedAmountUsd.toString())).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1887 ~ LeverageModules ~ availableWithdrawAmountUSD:",y);let A=I(y).div(t?$:W).mul(10**(t?R:x)).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1893 ~ LeverageModules ~ availableWithdrawAmount:",A);let P=z.div(I(y));console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1625 ~ LeverageModules ~ ratio:",P),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1899 ~ LeverageModules ~ borrows[0].borrowedAmount.toString():",C[0].borrowedAmount.toString()),m=I(m).mul(g).toDP(0,Ue.ROUND_DOWN).toString(),z=I(z).mul(g);let L=Ci(T.borrows[0],t?D[1]:D[0]),h=I(L.borrowedAmount.value.toString()).div(10**18).toDP(0,Ue.ROUND_UP),e=h.mul(P).toDP(0,Ue.ROUND_UP).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1901 ~ LeverageModules ~ repayAmount:",{repayAmount:e,ratio:P,withdrawAmount:m,borrowAmount:h,withdrawAmountUSD:z,quotePrice:W,basePrice:$});let{clmmPoolCoinTypeA:s,clmmPoolCoinTypeB:l,clmmFeeTier:f,clmmPool:d}=await this.getFlashLoanPool(C[0].reserve.coinType,e),v=await this.findRouters(S[0].reserve.coinType,C[0].reserve.coinType,I(e),!1,[p]),k=h.mul(I(1).sub(P)).toString(),w=I(k).mul(t?W:$).toString(),a=I(S[0].depositedAmount.toString()).mul(t?10**R:10**x).sub(I(m)).toString(),_=I(a).mul(t?$:W).toString(),F=I(e).mul(f).toString(),E=I(v?.amount_in.toString()).toString(),U=I(v?.amount_out.toString()).toString(),Z=I(P).gte(1);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1927 ~ LeverageModules ~ deposits[0].depositedAmount.toString():",{afterBorrowAmount:k,afterBorrowAmountUSD:w,afterDepositAmount:a,afterDepositAmountUSD:_});let G=I(m).div(q.toString()).toDP(0,Ue.ROUND_DOWN).toString();return console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1956 ~ LeverageModules ~ withdrawCtokenAmount:",G),{amountIn:E,amountOut:U,from:S[0].reserve.coinType,to:C[0].reserve.coinType,afterBorrowAmount:k,afterBorrowAmountUSD:w,afterDepositAmount:a,afterDepositAmountUSD:_,flashLoanFee:F,clmmFeeTier:f,withdrawAmount:m,withdrawCtokenAmount:G,repayAmount:e,ratio:P,isClose:Z,isLong:t,deposits:S,borrows:C,reserve:D,baseToken:M,quoteToken:b,clmmPool:d,clmmPoolCoinTypeA:s,clmmPoolCoinTypeB:l}};this.calculatePositionLeverage=async i=>{let{marketId:r,positionCapId:o,currentLeverage:t,targetLeverage:u,swapClmmPool:p=""}=i,c=I(u).sub(I(t)),g=c.gt(0),{baseToken:M,quoteToken:b}=await this.getLeverageMarketInfo(r),{borrows:S,deposits:C,isLong:T}=await this.getLeveragePosition(o);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1540 ~ LeverageModules ~ deposits:",C[0].reserve.config);let{reserve:D}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(M,b),O=D[0].mintDecimals,B=D[1].mintDecimals,R=await this._sdk.SuiLendModule.getLatestPriceFeeds(D,!0),x=R[yt(b)]?.price,q=R[yt(M)]?.price,j=S.length>0,W=C[0].depositedAmountUsd.toString(),$=C[0].depositedAmount.toString(),z=j?I(S[0].borrowedAmount.toString()).mul(10**(T?B:O)):I(0),m=j?S[0].borrowedAmountUsd.toString():I(0),y=I(C[0].reserve.config.openLtvPct).div(100).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1552 ~ LeverageModules ~ openLTV:",y);let A=I(W).sub(I(m)).mul(y).toString();if(g){let P=I(W).div(t).mul(c).div(T?x:q).mul(10**(T?B:O)).toDP(0,Ue.ROUND_UP),L=P.gt(A),h=P;if(L){let e=T?b:M,s=T?M:b,l=await this.findRouters(e,s,h,!0,[p]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:2106 ~ LeverageModules ~ routers:",{routers:l,from:e,to:s});let f=I(l?.amount_out.toString()).toString(),d=I(f).mul(T?q:x).toString(),{clmmPoolCoinTypeA:v,clmmPoolCoinTypeB:k,clmmPool:w,clmmFeeTier:a}=await this.getFlashLoanPool(e,h.toString()),_=h.mul(a).toString(),F=T?v==b:k==M,U=I(h).add(_).toDP(0,Ue.ROUND_UP).toString(),Z=I(U).mul(T?x:q).toString(),G=I($).add(I(f).div(10**(T?O:B))).toString(),te=I(G).mul(T?q:x).toString(),Ze=I(z).add(I(U)).div(10**(T?B:O)).toString(),J=I(Ze).mul(T?x:q).toString();return{amountIn:l?.amount_in.toString(),amountOut:l?.amount_out.toString(),flashLoanFee:_,depositAmount:f,borrowAmount:U,depositAmountUSD:d,borrowAmountUSD:Z,flashLoanAmount:h,baseToken:M,quoteToken:b,reserve:D,deposits:C,borrows:S,clmmFeeTier:a,isFlashLoan:L,isLoanA:F,clmmPoolCoinTypeA:v,clmmPoolCoinTypeB:k,afterDepositAmount:G,afterBorrowAmount:Ze,afterDepositAmountUSD:te,afterBorrowAmountUSD:J}}else{let e=h.toString(),s=I(e).mul(T?x:q).toString(),l=await this.findRouters(T?b:M,T?M:b,I(e),!0,[p]),f=I(l?.amount_out.toString()).toString(),d=I(f).mul(T?q:x).toString(),v=$.toString(),k=I(v).mul(T?q:x).toString(),w=I(z).add(I(e)).div(10**(T?B:O)).toString(),a=I(w).mul(T?x:q).toString();return{amountIn:l?.amount_in.toString(),amountOut:l?.amount_out.toString(),depositAmount:f,borrowAmount:e,depositAmountUSD:d,borrowAmountUSD:s,flashLoanAmount:h,baseToken:M,quoteToken:b,reserve:D,deposits:C,borrows:S,isFlashLoan:L,afterDepositAmount:v,afterBorrowAmount:w,afterDepositAmountUSD:k,afterBorrowAmountUSD:a}}}else{let P=I(W).div(t);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1598 ~ LeverageModules ~ netValue:",P.toString());let L=P.mul(c.abs()).div(T?x:q).mul(10**(T?B:O)).toDP(0,Ue.ROUND_UP);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1596 ~ LeverageModules ~ repayAmount:",L.toString());let h=await this.findRouters(C[0].reserve.coinType,S[0].reserve.coinType,L,!1,[p]),e=h?.amount_in.toString(),s=I(W).mul(y).toString(),l=I(s).lt(I(e));if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:2223 ~ LeverageModules ~ isFlashLoan:",l),l){console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1606 ~ LeverageModules ~ withdrawCtokenAmount:",e);let f=L.toString(),d=I(f).mul(T?x:q).toString(),v=e,k=I(v).mul(T?q:x).toString(),{clmmPoolCoinTypeA:w,clmmPoolCoinTypeB:a,clmmPool:_,clmmFeeTier:F}=await this.getFlashLoanPool(S[0].reserve.coinType,f.toString()),E=I($).sub(I(v).div(10**(T?O:B))).toString(),U=I(E).mul(T?q:x).toString(),Z=I(z).sub(I(f)).div(10**(T?B:O)).toString(),G=I(Z).mul(T?x:q).toString();return{amountIn:h?.amount_in.toString(),amountOut:h?.amount_out.toString(),depositAmount:v,borrowAmount:f,depositAmountUSD:k,borrowAmountUSD:d,baseToken:M,quoteToken:b,reserve:D,deposits:C,borrows:S,clmmFeeTier:F,withdrawCtokenAmount:e,isFlashLoan:l,clmmPoolCoinTypeA:w,clmmPoolCoinTypeB:a,afterDepositAmount:E,afterBorrowAmount:Z,afterDepositAmountUSD:U,afterBorrowAmountUSD:G}}else{let d=(await this.findRouters(C[0].reserve.coinType,S[0].reserve.coinType,e,!0,[p]))?.amount_out.toString(),v=I(d).mul(T?x:q).toString(),k=e.toString(),w=I(k).mul(T?q:x).toString(),a=I($).sub(I(k).div(10**(T?O:B))).toString(),_=I(a).mul(T?q:x).toString(),F=I(z).sub(I(d)).div(10**(T?B:O)).toString(),E=I(F).mul(T?x:q).toString();return{amountIn:h?.amount_in.toString(),amountOut:h?.amount_out.toString(),depositAmount:k,borrowAmount:d,depositAmountUSD:w,borrowAmountUSD:v,baseToken:M,quoteToken:b,reserve:D,deposits:C,borrows:S,withdrawCtokenAmount:e,isFlashLoan:l,afterDepositAmount:a,afterBorrowAmount:F,afterDepositAmountUSD:_,afterBorrowAmountUSD:E}}}};this.getFlashLoanPool=async(i,r)=>{let o=await fetch(`https://api-sui.cetus.zone/v3/sui/margin_trading/pools?coin_type=${i}&min_amount=${r}`);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:2151 ~ LeverageModules ~ res:",o);let{data:t}=await o.json();return t&&t.length>0?{clmmPoolCoinTypeA:t[0].coin_type_a,clmmPoolCoinTypeB:t[0].coin_type_b,clmmPool:t[0].pool_id,clmmFeeTier:I(t[0].fee_rate).div(1e6).toString()}:{clmmPoolCoinTypeA:"",clmmPoolCoinTypeB:"",clmmPool:"",clmmFeeTier:""}};this.createMarginTradingContext=(i,r)=>{let{published_at:o}=this._sdk.sdkOptions.leverage,t=mt(),{marketId:u,positionCapId:p,action:c,coinType:g,amount:M}=i;return r.moveCall({target:`${o}::router::create_margin_trading_context`,arguments:[r.pure.string(t),r.object(u),r.object(p),r.pure.string(c),r.pure.string(g),r.pure.u64(M)],typeArguments:[]})};this.marginTradingContextConfirm=(i,r)=>{let{published_at:o}=this._sdk.sdkOptions.leverage;return r.moveCall({target:`${o}::router::confirm`,arguments:[i],typeArguments:[]}),r};this._sdk=i}async handleSwapLogic(i){let{isLong:r,isQuote:o,amount:t,baseToken:u,quoteToken:p,swapClmmPool:c="",slippage:g,tx:M}=i,b=r&&o||!r&&!o;console.log("\u{1F680}\u{1F680}\u{1F680} ~ LeverageModules ~ hasSwap:",b);let S,C,T=t.toString();if(b){let D=r&&o?p:u,O=r&&o?u:p,B=await this.findRouters(D,O,t,!0,[c]);if(T=B?.amount_out.toString(),B){let R=Y.buildCoinWithBalance(BigInt(t.toString()),D,M);C=await this.routerSwap({router:B.route_obj,slippage:g,inputCoin:R,txb:M}),S=C}}else S=Y.buildCoinWithBalance(BigInt(t.toString()),o?p:u,M);return{hasSwap:b,inputCoin:S,initDepositAmount:T}}async handleDebtSwapLogic(i){let{isLong:r,flashAmount:o,baseToken:t,quoteToken:u,swapClmmPool:p="",isFlashA:c,balanceA:g,balanceB:M,slippage:b,clmmPoolCoinTypeA:S,clmmPoolCoinTypeB:C,tx:T}=i,D=r?u:t,O=r?t:u,B=await this.findRouters(D,O,I(o),!0,[p]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:311 ~ LeverageModules ~ debtRouters:",{debtRouters:B,debtFrom:D,debtTo:O,clmmPoolCoinTypeA:S,clmmPoolCoinTypeB:C});let R=I(B?.amount_out||0),x;return B&&(x=await this.routerSwap({router:B.route_obj,inputCoin:c?Y.fromBalance(g,S,T):Y.fromBalance(M,C,T),slippage:b,txb:T})),{debtFrom:D,debtTo:O,debtRouters:B,debtSwapAmountOut:R,debtSwapOutCoin:x}}async calculateFlashLoanParams(i){let{isLong:r,leverage:o,baseToken:t,quoteToken:u,initDepositAmount:p,reserve:c,baseTokenDecimal:g,quoteTokenDecimal:M,openFeeRate:b}=i;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1403 ~ LeverageModules ~ calculateFlashLoanParams ~ params:",i),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1079 ~ LeverageModules ~ calculateFlashLoanParams ~ initDepositAmount:",p);let S=await this._sdk.SuiLendModule.getLatestPriceFeeds(c);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:587 ~ LeverageModules ~ calculateFlashLoanParams ~ priceUpdateData:",S,u,t);let C=S[yt(u)]?.price,T=S[yt(t)]?.price,D=r?I(T).div(I(C)).toString():I(C).div(I(T)).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1057 ~ LeverageModules ~ calculateFlashLoanParams ~ rate:",{rate:D}),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1424 ~ LeverageModules ~ calculateFlashLoanParams ~ initDepositAmount:",p);let O=I(p).mul(1-b).mul(o-1).mul(D).div(10**(r?g:M)).mul(10**(r?M:g)).toDP(0,Ue.ROUND_UP).toString(),B=r?u:t;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1457 ~ LeverageModules ~ flashLoanCoin:",B),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1457 ~ LeverageModules ~ flashLoanCoin:",B);let{clmmPoolCoinTypeA:R,clmmFeeTier:x,clmmPool:q,clmmPoolCoinTypeB:j}=await this.getFlashLoanPool(B,O);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1094 ~ LeverageModules ~ calculateFlashLoanParams ~ flashAmount:",O,x,B);let W=R===B,$=I(O).mul(x).toString();return{flashAmount:O,rate:D,quotePrice:C,basePrice:T,flashLoanCoin:B,isFlashA:W,flashLoanFee:$,clmmPool:q,clmmPoolCoinTypeA:R,clmmPoolCoinTypeB:j,clmmFeeTier:x}}createLeveragePosition(i){console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1493 ~ LeverageModules ~ createLeveragePosition ~ params:",i);let{lendingMarketId:r,lendingMarketType:o}=ne(this._sdk.sdkOptions.suilend),{global_config_id:t,versioned:u}=ne(this._sdk.sdkOptions.leverage),p=this._sdk.sdkOptions.leverage,{leverage:c,marketId:g,baseToken:M,quoteToken:b,isLong:S,initDepositAmount:C,tx:T,initCoinType:D}=i,O=mt(),[B,R]=T.moveCall({target:`${p.published_at}::router::open_position`,arguments:[T.object(t),T.object(g),T.object(r),T.pure.u64(C),T.pure.string(D),T.pure.u64(I(c).mul(1e4).toString()),T.pure.string(O),T.object(pt),T.object(u)],typeArguments:[o,S?M:b]});return{positionCap:B,marginTradingContext:R}}async depositToLeveragePosition(i,r){console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:651 ~ LeverageModules ~ depositToLeveragePosition ~ params:",i);let{leverage:o}=this._sdk.sdkOptions,{lendingMarketType:t,lendingMarketId:u}=ne(this._sdk.sdkOptions.suilend),{isLong:p,marketId:c,positionCapId:g,positionCap:M,depositReserveArrayIndex:b,inputCoin:S,marginTradingContext:C}=i,{global_config_id:T,versioned:D}=ne(this._sdk.sdkOptions.leverage),{baseToken:O,quoteToken:B}=await this.getLeverageMarketInfo(c);return r.moveCall({target:`${o.published_at}::router::deposit`,typeArguments:[t,p?O:B],arguments:[C,r.object(T),r.object(u),r.object(c),g?r.object(g):M,S,r.pure.u64(b),r.object(pt),r.object(D)]}),r}async findRouters(i,r,o,t,u){let{providers:p}=this._sdk.sdkOptions;try{let c={from:i,target:r,amount:new Dr.default(o.toFixed(0).toString()),byAmountIn:t,depth:3,providers:p},g=await this._sdk.AggregatorClient.findRouters(c);if(g?.error?.code===10001)return{...g,is_exceed:g.insufficientLiquidity};if(g?.insufficientLiquidity)return{...g,is_exceed:g.insufficientLiquidity};if(!g?.paths||g?.paths?.length===0)throw Error("Aggregator no router");return{amount_in:g.amountIn.toString(),amount_out:g.amountOut.toString(),is_exceed:g.insufficientLiquidity,route_obj:g,byAmountIn:!0,originRes:g}}catch{try{if(u){let g=await this._sdk.AggregatorClient.swapInPools({from:i,target:r,amount:new Dr.default(o.toFixed(0).toString()),byAmountIn:t,pools:u});return g?{amount_in:g.routeData.amountIn.toString(),amount_out:g.routeData.amountOut.toString(),is_exceed:g.isExceed,route_obj:g.routeData,byAmountIn:!0,originRes:g}:null}return null}catch{return null}}}};import{AggregatorClient as uo,Env as Li}from"@cetusprotocol/aggregator-sdk";import{normalizeSuiAddress as fo}from"@mysten/sui/utils";import{SuiClient as co}from"@mysten/sui/client";import{CetusClmmSDK as ho}from"@cetusprotocol/sui-clmm-sdk";import{SuiPriceServiceConnection as po}from"@pythnetwork/pyth-sui-js";import{CLOCK_ADDRESS as kt,getPackagerConfigs as Qe}from"@cetusprotocol/common-sdk";import{Transaction as Ve}from"@mysten/sui/transactions";var Zt=class{constructor(i){this.queryGlobalPermissions=async()=>{let i=this._sdk.sdkOptions.leverage,{global_config_id:r}=Qe(i),o=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}});if(o){let t=o.data.content.fields.permissions.toString(2),{openPermissionsPause:u,closePermissionsPause:p,depositPermissionsPause:c,withdrawPermissionsPause:g,borrowPermissionsPause:M,repayPermissionsPause:b}=xr(t);return{openPermissionsPause:u,closePermissionsPause:p,depositPermissionsPause:c,withdrawPermissionsPause:g,borrowPermissionsPause:M,repayPermissionsPause:b}}};this.updateGlobalPermissions=async i=>{let r=new Ve;return this.updateGlobalOpenPositionPermissions(i,r),this.updateGlobalClosePositionPermissions(i,r),this.updateGlobalDepositPermissions(i,r),this.updateGlobalBorrowPermissions(i,r),this.updateGlobalWithdrawPermissions(i,r),this.updateGlobalRepayPermissions(i,r),r};this.updateGlobalOpenPositionPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_open_position_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateGlobalClosePositionPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_close_position_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateGlobalDepositPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_deposit_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateGlobalBorrowPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_borrow_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateGlobalWithdrawPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_withdraw_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateGlobalRepayPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_repay_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateMarketCreatePositionPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_open_position_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketClosePositionPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_close_position_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketDepositPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_deposit_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketWithdrawPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_withdraw_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketBorrowPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_borrow_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketRepayPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_repay_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketPermissions=async(i,r,o)=>{let t=o||new Ve;return this.updateMarketCreatePositionPermissions(i,r,t),this.updateMarketClosePositionPermissions(i,r,t),this.updateMarketDepositPermissions(i,r,t),this.updateMarketWithdrawPermissions(i,r,t),this.updateMarketBorrowPermissions(i,r,t),this.updateMarketRepayPermissions(i,r,t),t};this._sdk=i}};import{getPackagerConfigs as Wt}from"@cetusprotocol/common-sdk";import{Transaction as Ct}from"@mysten/sui/transactions";var zt=class{constructor(i){this.createMarket=async i=>{let{package_id:r}=this._sdk.sdkOptions.leverage,{versioned:o,admin_cap_id:t,markets:u,global_config_id:p}=Wt(this._sdk.sdkOptions.leverage),c=new Ct;return c.moveCall({target:`${r}::market::create_market`,arguments:[c.object(t),c.object(p),c.object(u),c.pure.u64(i.maxLongLeverage),c.pure.u64(i.maxShortLeverage),c.pure.u64(i.openFeeRate),c.pure.u64(i.closeFeeRate),c.object(o)],typeArguments:[i.baseToken,i.quoteToken]}),c};this.updateMarketMaxLeverage=async i=>{let{package_id:r}=this._sdk.sdkOptions.leverage,{versioned:o,admin_cap_id:t}=Wt(this._sdk.sdkOptions.leverage),{marketId:u,maxLongLeverage:p,maxShortLeverage:c}=i,g=new Ct;return g.moveCall({target:`${r}::market::set_max_leverage`,arguments:[g.object(t),g.object(u),g.pure.u64(p),g.pure.u64(c),g.object(o)],typeArguments:[]}),g};this.updateMarketFeeRate=async i=>{let{package_id:r}=this._sdk.sdkOptions.leverage,{versioned:o,admin_cap_id:t}=Wt(this._sdk.sdkOptions.leverage),{marketId:u,openFeeRate:p,closeFeeRate:c}=i,g=new Ct;return g.moveCall({target:`${r}::market::set_fee_rate`,arguments:[g.object(t),g.object(u),g.pure.u64(p),g.pure.u64(c),g.object(o)],typeArguments:[]}),g};this.claimMarketFees=async(i,r)=>{let{package_id:o}=this._sdk.sdkOptions.leverage,{versioned:t,admin_cap_id:u}=Wt(this._sdk.sdkOptions.leverage),p=r||new Ct,{baseToken:c,quoteToken:g}=await this._sdk.LeverageModules.getLeverageMarketInfo(i);return p.moveCall({target:`${o}::market::claim_fees`,arguments:[p.object(u),p.object(i),p.object(t)],typeArguments:[c,g]}),p};this.claimAllMarketFees=async i=>{let r=new Ct;for(let o=0;o<i.length;o++){let t=i[o];this.claimMarketFees(t,r)}return r};this._sdk=i}};var Kt=class n extends lo{constructor(i,r){super(i),this._suilendModule=new Dt(this),this._leverageModules=new $t(this),this._permissionModules=new Zt(this),this._marketModules=new zt(this),this._aggregatorClient=new uo({signer:fo("0x0"),client:i.sui_client||new co({url:i.full_rpc_url}),env:i.env==="testnet"?Li.Testnet:Li.Mainnet,pythUrls:i.pyth_urls}),this._clmmSDK=r||ho.createSDK({env:i.env,full_rpc_url:i.full_rpc_url}),this._pythConnection=new po("https://hermes.pyth.network",{timeout:30*1e3})}get SuiLendModule(){return this._suilendModule}get LeverageModules(){return this._leverageModules}get AggregatorClient(){return this._aggregatorClient}get ClmmSDK(){return this._clmmSDK}get PythConnection(){return this._pythConnection}get PermissionModules(){return this._permissionModules}get MarketModules(){return this._marketModules}static createSDK(i){return n.createCustomSDK({...Vr,...i})}static createCustomSDK(i){return new n(i)}};var Ks=Kt;export{Kt as CetusLeverageSDK,qo as appendExtraRewards,wr as calculateBorrowAprPercent,ei as calculateDepositAprPercent,Yr as calculateUtilizationPercent,Ks as default,Wo as formatLtvPercentValue,ti as getPriceWithFormattedDecimals,$o as getRewardsAprMultiplier,jo as getUpdatedReserve,Vr as leverage_mainnet,Tr as mergePositionData,vr as oraclePriceMultiplierDecimal,Zo as updateRewardsWithMultiplier,Pr as wrapMarketInfo,xr as wrapMarketPermissions,Lr as wrapPosition};
|
|
1
|
+
var Ti=Object.create;var Hr=Object.defineProperty;var xi=Object.getOwnPropertyDescriptor;var Di=Object.getOwnPropertyNames;var Oi=Object.getPrototypeOf,Ii=Object.prototype.hasOwnProperty;var Ri=(n=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(n,{get:(i,r)=>(typeof require<"u"?require:i)[r]}):n)(function(n){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+n+'" is not supported')});var Bi=(n,i)=>()=>(i||n((i={exports:{}}).exports,i),i.exports);var Ni=(n,i,r,o)=>{if(i&&typeof i=="object"||typeof i=="function")for(let t of Di(i))!Ii.call(n,t)&&t!==r&&Hr(n,t,{get:()=>i[t],enumerable:!(o=xi(i,t))||o.enumerable});return n};var Ei=(n,i,r)=>(r=n!=null?Ti(Oi(n)):{},Ni(i||!n||!n.__esModule?Hr(r,"default",{value:n,enumerable:!0}):r,n));var Mi=Bi((wi,Cr)=>{"use strict";(function(n,i){"use strict";function r(h,e){if(!h)throw new Error(e||"Assertion failed")}function o(h,e){h.super_=e;var s=function(){};s.prototype=e.prototype,h.prototype=new s,h.prototype.constructor=h}function t(h,e,s){if(t.isBN(h))return h;this.negative=0,this.words=null,this.length=0,this.red=null,h!==null&&((e==="le"||e==="be")&&(s=e,e=10),this._init(h||0,e||10,s||"be"))}typeof n=="object"?n.exports=t:i.BN=t,t.BN=t,t.wordSize=26;var u;try{typeof window<"u"&&typeof window.Buffer<"u"?u=window.Buffer:u=Ri("buffer").Buffer}catch{}t.isBN=function(e){return e instanceof t?!0:e!==null&&typeof e=="object"&&e.constructor.wordSize===t.wordSize&&Array.isArray(e.words)},t.max=function(e,s){return e.cmp(s)>0?e:s},t.min=function(e,s){return e.cmp(s)<0?e:s},t.prototype._init=function(e,s,l){if(typeof e=="number")return this._initNumber(e,s,l);if(typeof e=="object")return this._initArray(e,s,l);s==="hex"&&(s=16),r(s===(s|0)&&s>=2&&s<=36),e=e.toString().replace(/\s+/g,"");var f=0;e[0]==="-"&&(f++,this.negative=1),f<e.length&&(s===16?this._parseHex(e,f,l):(this._parseBase(e,s,f),l==="le"&&this._initArray(this.toArray(),s,l)))},t.prototype._initNumber=function(e,s,l){e<0&&(this.negative=1,e=-e),e<67108864?(this.words=[e&67108863],this.length=1):e<4503599627370496?(this.words=[e&67108863,e/67108864&67108863],this.length=2):(r(e<9007199254740992),this.words=[e&67108863,e/67108864&67108863,1],this.length=3),l==="le"&&this._initArray(this.toArray(),s,l)},t.prototype._initArray=function(e,s,l){if(r(typeof e.length=="number"),e.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(e.length/3),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var d,v,k=0;if(l==="be")for(f=e.length-1,d=0;f>=0;f-=3)v=e[f]|e[f-1]<<8|e[f-2]<<16,this.words[d]|=v<<k&67108863,this.words[d+1]=v>>>26-k&67108863,k+=24,k>=26&&(k-=26,d++);else if(l==="le")for(f=0,d=0;f<e.length;f+=3)v=e[f]|e[f+1]<<8|e[f+2]<<16,this.words[d]|=v<<k&67108863,this.words[d+1]=v>>>26-k&67108863,k+=24,k>=26&&(k-=26,d++);return this._strip()};function p(h,e){var s=h.charCodeAt(e);if(s>=48&&s<=57)return s-48;if(s>=65&&s<=70)return s-55;if(s>=97&&s<=102)return s-87;r(!1,"Invalid character in "+h)}function c(h,e,s){var l=p(h,s);return s-1>=e&&(l|=p(h,s-1)<<4),l}t.prototype._parseHex=function(e,s,l){this.length=Math.ceil((e.length-s)/6),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var d=0,v=0,k;if(l==="be")for(f=e.length-1;f>=s;f-=2)k=c(e,s,f)<<d,this.words[v]|=k&67108863,d>=18?(d-=18,v+=1,this.words[v]|=k>>>26):d+=8;else{var w=e.length-s;for(f=w%2===0?s+1:s;f<e.length;f+=2)k=c(e,s,f)<<d,this.words[v]|=k&67108863,d>=18?(d-=18,v+=1,this.words[v]|=k>>>26):d+=8}this._strip()};function g(h,e,s,l){for(var f=0,d=0,v=Math.min(h.length,s),k=e;k<v;k++){var w=h.charCodeAt(k)-48;f*=l,w>=49?d=w-49+10:w>=17?d=w-17+10:d=w,r(w>=0&&d<l,"Invalid character"),f+=d}return f}t.prototype._parseBase=function(e,s,l){this.words=[0],this.length=1;for(var f=0,d=1;d<=67108863;d*=s)f++;f--,d=d/s|0;for(var v=e.length-l,k=v%f,w=Math.min(v,v-k)+l,a=0,_=l;_<w;_+=f)a=g(e,_,_+f,s),this.imuln(d),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a);if(k!==0){var F=1;for(a=g(e,_,e.length,s),_=0;_<k;_++)F*=s;this.imuln(F),this.words[0]+a<67108864?this.words[0]+=a:this._iaddn(a)}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 M(h,e){h.words=e.words,h.length=e.length,h.negative=e.negative,h.red=e.red}if(t.prototype._move=function(e){M(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")]=b}catch{t.prototype.inspect=b}else t.prototype.inspect=b;function b(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var S=["","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"],C=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],T=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];t.prototype.toString=function(e,s){e=e||10,s=s|0||1;var l;if(e===16||e==="hex"){l="";for(var f=0,d=0,v=0;v<this.length;v++){var k=this.words[v],w=((k<<f|d)&16777215).toString(16);d=k>>>24-f&16777215,f+=2,f>=26&&(f-=26,v--),d!==0||v!==this.length-1?l=S[6-w.length]+w+l:l=w+l}for(d!==0&&(l=d.toString(16)+l);l.length%s!==0;)l="0"+l;return this.negative!==0&&(l="-"+l),l}if(e===(e|0)&&e>=2&&e<=36){var a=C[e],_=T[e];l="";var F=this.clone();for(F.negative=0;!F.isZero();){var E=F.modrn(_).toString(e);F=F.idivn(_),F.isZero()?l=E+l:l=S[a-E.length]+E+l}for(this.isZero()&&(l="0"+l);l.length%s!==0;)l="0"+l;return this.negative!==0&&(l="-"+l),l}r(!1,"Base should be between 2 and 36")},t.prototype.toNumber=function(){var e=this.words[0];return this.length===2?e+=this.words[1]*67108864:this.length===3&&this.words[2]===1?e+=4503599627370496+this.words[1]*67108864:this.length>2&&r(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-e:e},t.prototype.toJSON=function(){return this.toString(16,2)},u&&(t.prototype.toBuffer=function(e,s){return this.toArrayLike(u,e,s)}),t.prototype.toArray=function(e,s){return this.toArrayLike(Array,e,s)};var D=function(e,s){return e.allocUnsafe?e.allocUnsafe(s):new e(s)};t.prototype.toArrayLike=function(e,s,l){this._strip();var f=this.byteLength(),d=l||Math.max(1,f);r(f<=d,"byte array longer than desired length"),r(d>0,"Requested array length <= 0");var v=D(e,d),k=s==="le"?"LE":"BE";return this["_toArrayLike"+k](v,f),v},t.prototype._toArrayLikeLE=function(e,s){for(var l=0,f=0,d=0,v=0;d<this.length;d++){var k=this.words[d]<<v|f;e[l++]=k&255,l<e.length&&(e[l++]=k>>8&255),l<e.length&&(e[l++]=k>>16&255),v===6?(l<e.length&&(e[l++]=k>>24&255),f=0,v=0):(f=k>>>24,v+=2)}if(l<e.length)for(e[l++]=f;l<e.length;)e[l++]=0},t.prototype._toArrayLikeBE=function(e,s){for(var l=e.length-1,f=0,d=0,v=0;d<this.length;d++){var k=this.words[d]<<v|f;e[l--]=k&255,l>=0&&(e[l--]=k>>8&255),l>=0&&(e[l--]=k>>16&255),v===6?(l>=0&&(e[l--]=k>>24&255),f=0,v=0):(f=k>>>24,v+=2)}if(l>=0)for(e[l--]=f;l>=0;)e[l--]=0},Math.clz32?t.prototype._countBits=function(e){return 32-Math.clz32(e)}:t.prototype._countBits=function(e){var s=e,l=0;return s>=4096&&(l+=13,s>>>=13),s>=64&&(l+=7,s>>>=7),s>=8&&(l+=4,s>>>=4),s>=2&&(l+=2,s>>>=2),l+s},t.prototype._zeroBits=function(e){if(e===0)return 26;var s=e,l=0;return(s&8191)===0&&(l+=13,s>>>=13),(s&127)===0&&(l+=7,s>>>=7),(s&15)===0&&(l+=4,s>>>=4),(s&3)===0&&(l+=2,s>>>=2),(s&1)===0&&l++,l},t.prototype.bitLength=function(){var e=this.words[this.length-1],s=this._countBits(e);return(this.length-1)*26+s};function O(h){for(var e=new Array(h.bitLength()),s=0;s<e.length;s++){var l=s/26|0,f=s%26;e[s]=h.words[l]>>>f&1}return e}t.prototype.zeroBits=function(){if(this.isZero())return 0;for(var e=0,s=0;s<this.length;s++){var l=this._zeroBits(this.words[s]);if(e+=l,l!==26)break}return e},t.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},t.prototype.toTwos=function(e){return this.negative!==0?this.abs().inotn(e).iaddn(1):this.clone()},t.prototype.fromTwos=function(e){return this.testn(e-1)?this.notn(e).iaddn(1).ineg():this.clone()},t.prototype.isNeg=function(){return this.negative!==0},t.prototype.neg=function(){return this.clone().ineg()},t.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},t.prototype.iuor=function(e){for(;this.length<e.length;)this.words[this.length++]=0;for(var s=0;s<e.length;s++)this.words[s]=this.words[s]|e.words[s];return this._strip()},t.prototype.ior=function(e){return r((this.negative|e.negative)===0),this.iuor(e)},t.prototype.or=function(e){return this.length>e.length?this.clone().ior(e):e.clone().ior(this)},t.prototype.uor=function(e){return this.length>e.length?this.clone().iuor(e):e.clone().iuor(this)},t.prototype.iuand=function(e){var s;this.length>e.length?s=e:s=this;for(var l=0;l<s.length;l++)this.words[l]=this.words[l]&e.words[l];return this.length=s.length,this._strip()},t.prototype.iand=function(e){return r((this.negative|e.negative)===0),this.iuand(e)},t.prototype.and=function(e){return this.length>e.length?this.clone().iand(e):e.clone().iand(this)},t.prototype.uand=function(e){return this.length>e.length?this.clone().iuand(e):e.clone().iuand(this)},t.prototype.iuxor=function(e){var s,l;this.length>e.length?(s=this,l=e):(s=e,l=this);for(var f=0;f<l.length;f++)this.words[f]=s.words[f]^l.words[f];if(this!==s)for(;f<s.length;f++)this.words[f]=s.words[f];return this.length=s.length,this._strip()},t.prototype.ixor=function(e){return r((this.negative|e.negative)===0),this.iuxor(e)},t.prototype.xor=function(e){return this.length>e.length?this.clone().ixor(e):e.clone().ixor(this)},t.prototype.uxor=function(e){return this.length>e.length?this.clone().iuxor(e):e.clone().iuxor(this)},t.prototype.inotn=function(e){r(typeof e=="number"&&e>=0);var s=Math.ceil(e/26)|0,l=e%26;this._expand(s),l>0&&s--;for(var f=0;f<s;f++)this.words[f]=~this.words[f]&67108863;return l>0&&(this.words[f]=~this.words[f]&67108863>>26-l),this._strip()},t.prototype.notn=function(e){return this.clone().inotn(e)},t.prototype.setn=function(e,s){r(typeof e=="number"&&e>=0);var l=e/26|0,f=e%26;return this._expand(l+1),s?this.words[l]=this.words[l]|1<<f:this.words[l]=this.words[l]&~(1<<f),this._strip()},t.prototype.iadd=function(e){var s;if(this.negative!==0&&e.negative===0)return this.negative=0,s=this.isub(e),this.negative^=1,this._normSign();if(this.negative===0&&e.negative!==0)return e.negative=0,s=this.isub(e),e.negative=1,s._normSign();var l,f;this.length>e.length?(l=this,f=e):(l=e,f=this);for(var d=0,v=0;v<f.length;v++)s=(l.words[v]|0)+(f.words[v]|0)+d,this.words[v]=s&67108863,d=s>>>26;for(;d!==0&&v<l.length;v++)s=(l.words[v]|0)+d,this.words[v]=s&67108863,d=s>>>26;if(this.length=l.length,d!==0)this.words[this.length]=d,this.length++;else if(l!==this)for(;v<l.length;v++)this.words[v]=l.words[v];return this},t.prototype.add=function(e){var s;return e.negative!==0&&this.negative===0?(e.negative=0,s=this.sub(e),e.negative^=1,s):e.negative===0&&this.negative!==0?(this.negative=0,s=e.sub(this),this.negative=1,s):this.length>e.length?this.clone().iadd(e):e.clone().iadd(this)},t.prototype.isub=function(e){if(e.negative!==0){e.negative=0;var s=this.iadd(e);return e.negative=1,s._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(e),this.negative=1,this._normSign();var l=this.cmp(e);if(l===0)return this.negative=0,this.length=1,this.words[0]=0,this;var f,d;l>0?(f=this,d=e):(f=e,d=this);for(var v=0,k=0;k<d.length;k++)s=(f.words[k]|0)-(d.words[k]|0)+v,v=s>>26,this.words[k]=s&67108863;for(;v!==0&&k<f.length;k++)s=(f.words[k]|0)+v,v=s>>26,this.words[k]=s&67108863;if(v===0&&k<f.length&&f!==this)for(;k<f.length;k++)this.words[k]=f.words[k];return this.length=Math.max(this.length,k),f!==this&&(this.negative=1),this._strip()},t.prototype.sub=function(e){return this.clone().isub(e)};function B(h,e,s){s.negative=e.negative^h.negative;var l=h.length+e.length|0;s.length=l,l=l-1|0;var f=h.words[0]|0,d=e.words[0]|0,v=f*d,k=v&67108863,w=v/67108864|0;s.words[0]=k;for(var a=1;a<l;a++){for(var _=w>>>26,F=w&67108863,E=Math.min(a,e.length-1),U=Math.max(0,a-h.length+1);U<=E;U++){var Z=a-U|0;f=h.words[Z]|0,d=e.words[U]|0,v=f*d+F,_+=v/67108864|0,F=v&67108863}s.words[a]=F|0,w=_|0}return w!==0?s.words[a]=w|0:s.length--,s._strip()}var R=function(e,s,l){var f=e.words,d=s.words,v=l.words,k=0,w,a,_,F=f[0]|0,E=F&8191,U=F>>>13,Z=f[1]|0,G=Z&8191,te=Z>>>13,Ze=f[2]|0,J=Ze&8191,re=Ze>>>13,Ke=f[3]|0,ee=Ke&8191,Q=Ke>>>13,Or=f[4]|0,se=Or&8191,ae=Or>>>13,Ir=f[5]|0,le=Ir&8191,ue=Ir>>>13,Rr=f[6]|0,fe=Rr&8191,ce=Rr>>>13,Br=f[7]|0,de=Br&8191,he=Br>>>13,Nr=f[8]|0,pe=Nr&8191,ge=Nr>>>13,Er=f[9]|0,me=Er&8191,ve=Er>>>13,Fr=d[0]|0,we=Fr&8191,Me=Fr>>>13,Ur=d[1]|0,be=Ur&8191,ye=Ur>>>13,qr=d[2]|0,_e=qr&8191,ke=qr>>>13,jr=d[3]|0,Ae=jr&8191,Se=jr>>>13,$r=d[4]|0,Ce=$r&8191,Pe=$r>>>13,Zr=d[5]|0,Le=Zr&8191,Te=Zr>>>13,Wr=d[6]|0,xe=Wr&8191,De=Wr>>>13,zr=d[7]|0,Oe=zr&8191,Ie=zr>>>13,Kr=d[8]|0,Re=Kr&8191,Be=Kr>>>13,Gr=d[9]|0,Ne=Gr&8191,Ee=Gr>>>13;l.negative=e.negative^s.negative,l.length=19,w=Math.imul(E,we),a=Math.imul(E,Me),a=a+Math.imul(U,we)|0,_=Math.imul(U,Me);var Gt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Gt>>>26)|0,Gt&=67108863,w=Math.imul(G,we),a=Math.imul(G,Me),a=a+Math.imul(te,we)|0,_=Math.imul(te,Me),w=w+Math.imul(E,be)|0,a=a+Math.imul(E,ye)|0,a=a+Math.imul(U,be)|0,_=_+Math.imul(U,ye)|0;var Ht=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Ht>>>26)|0,Ht&=67108863,w=Math.imul(J,we),a=Math.imul(J,Me),a=a+Math.imul(re,we)|0,_=Math.imul(re,Me),w=w+Math.imul(G,be)|0,a=a+Math.imul(G,ye)|0,a=a+Math.imul(te,be)|0,_=_+Math.imul(te,ye)|0,w=w+Math.imul(E,_e)|0,a=a+Math.imul(E,ke)|0,a=a+Math.imul(U,_e)|0,_=_+Math.imul(U,ke)|0;var Vt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Vt>>>26)|0,Vt&=67108863,w=Math.imul(ee,we),a=Math.imul(ee,Me),a=a+Math.imul(Q,we)|0,_=Math.imul(Q,Me),w=w+Math.imul(J,be)|0,a=a+Math.imul(J,ye)|0,a=a+Math.imul(re,be)|0,_=_+Math.imul(re,ye)|0,w=w+Math.imul(G,_e)|0,a=a+Math.imul(G,ke)|0,a=a+Math.imul(te,_e)|0,_=_+Math.imul(te,ke)|0,w=w+Math.imul(E,Ae)|0,a=a+Math.imul(E,Se)|0,a=a+Math.imul(U,Ae)|0,_=_+Math.imul(U,Se)|0;var Qt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Qt>>>26)|0,Qt&=67108863,w=Math.imul(se,we),a=Math.imul(se,Me),a=a+Math.imul(ae,we)|0,_=Math.imul(ae,Me),w=w+Math.imul(ee,be)|0,a=a+Math.imul(ee,ye)|0,a=a+Math.imul(Q,be)|0,_=_+Math.imul(Q,ye)|0,w=w+Math.imul(J,_e)|0,a=a+Math.imul(J,ke)|0,a=a+Math.imul(re,_e)|0,_=_+Math.imul(re,ke)|0,w=w+Math.imul(G,Ae)|0,a=a+Math.imul(G,Se)|0,a=a+Math.imul(te,Ae)|0,_=_+Math.imul(te,Se)|0,w=w+Math.imul(E,Ce)|0,a=a+Math.imul(E,Pe)|0,a=a+Math.imul(U,Ce)|0,_=_+Math.imul(U,Pe)|0;var Xt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Xt>>>26)|0,Xt&=67108863,w=Math.imul(le,we),a=Math.imul(le,Me),a=a+Math.imul(ue,we)|0,_=Math.imul(ue,Me),w=w+Math.imul(se,be)|0,a=a+Math.imul(se,ye)|0,a=a+Math.imul(ae,be)|0,_=_+Math.imul(ae,ye)|0,w=w+Math.imul(ee,_e)|0,a=a+Math.imul(ee,ke)|0,a=a+Math.imul(Q,_e)|0,_=_+Math.imul(Q,ke)|0,w=w+Math.imul(J,Ae)|0,a=a+Math.imul(J,Se)|0,a=a+Math.imul(re,Ae)|0,_=_+Math.imul(re,Se)|0,w=w+Math.imul(G,Ce)|0,a=a+Math.imul(G,Pe)|0,a=a+Math.imul(te,Ce)|0,_=_+Math.imul(te,Pe)|0,w=w+Math.imul(E,Le)|0,a=a+Math.imul(E,Te)|0,a=a+Math.imul(U,Le)|0,_=_+Math.imul(U,Te)|0;var Jt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Jt>>>26)|0,Jt&=67108863,w=Math.imul(fe,we),a=Math.imul(fe,Me),a=a+Math.imul(ce,we)|0,_=Math.imul(ce,Me),w=w+Math.imul(le,be)|0,a=a+Math.imul(le,ye)|0,a=a+Math.imul(ue,be)|0,_=_+Math.imul(ue,ye)|0,w=w+Math.imul(se,_e)|0,a=a+Math.imul(se,ke)|0,a=a+Math.imul(ae,_e)|0,_=_+Math.imul(ae,ke)|0,w=w+Math.imul(ee,Ae)|0,a=a+Math.imul(ee,Se)|0,a=a+Math.imul(Q,Ae)|0,_=_+Math.imul(Q,Se)|0,w=w+Math.imul(J,Ce)|0,a=a+Math.imul(J,Pe)|0,a=a+Math.imul(re,Ce)|0,_=_+Math.imul(re,Pe)|0,w=w+Math.imul(G,Le)|0,a=a+Math.imul(G,Te)|0,a=a+Math.imul(te,Le)|0,_=_+Math.imul(te,Te)|0,w=w+Math.imul(E,xe)|0,a=a+Math.imul(E,De)|0,a=a+Math.imul(U,xe)|0,_=_+Math.imul(U,De)|0;var Yt=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(Yt>>>26)|0,Yt&=67108863,w=Math.imul(de,we),a=Math.imul(de,Me),a=a+Math.imul(he,we)|0,_=Math.imul(he,Me),w=w+Math.imul(fe,be)|0,a=a+Math.imul(fe,ye)|0,a=a+Math.imul(ce,be)|0,_=_+Math.imul(ce,ye)|0,w=w+Math.imul(le,_e)|0,a=a+Math.imul(le,ke)|0,a=a+Math.imul(ue,_e)|0,_=_+Math.imul(ue,ke)|0,w=w+Math.imul(se,Ae)|0,a=a+Math.imul(se,Se)|0,a=a+Math.imul(ae,Ae)|0,_=_+Math.imul(ae,Se)|0,w=w+Math.imul(ee,Ce)|0,a=a+Math.imul(ee,Pe)|0,a=a+Math.imul(Q,Ce)|0,_=_+Math.imul(Q,Pe)|0,w=w+Math.imul(J,Le)|0,a=a+Math.imul(J,Te)|0,a=a+Math.imul(re,Le)|0,_=_+Math.imul(re,Te)|0,w=w+Math.imul(G,xe)|0,a=a+Math.imul(G,De)|0,a=a+Math.imul(te,xe)|0,_=_+Math.imul(te,De)|0,w=w+Math.imul(E,Oe)|0,a=a+Math.imul(E,Ie)|0,a=a+Math.imul(U,Oe)|0,_=_+Math.imul(U,Ie)|0;var er=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(er>>>26)|0,er&=67108863,w=Math.imul(pe,we),a=Math.imul(pe,Me),a=a+Math.imul(ge,we)|0,_=Math.imul(ge,Me),w=w+Math.imul(de,be)|0,a=a+Math.imul(de,ye)|0,a=a+Math.imul(he,be)|0,_=_+Math.imul(he,ye)|0,w=w+Math.imul(fe,_e)|0,a=a+Math.imul(fe,ke)|0,a=a+Math.imul(ce,_e)|0,_=_+Math.imul(ce,ke)|0,w=w+Math.imul(le,Ae)|0,a=a+Math.imul(le,Se)|0,a=a+Math.imul(ue,Ae)|0,_=_+Math.imul(ue,Se)|0,w=w+Math.imul(se,Ce)|0,a=a+Math.imul(se,Pe)|0,a=a+Math.imul(ae,Ce)|0,_=_+Math.imul(ae,Pe)|0,w=w+Math.imul(ee,Le)|0,a=a+Math.imul(ee,Te)|0,a=a+Math.imul(Q,Le)|0,_=_+Math.imul(Q,Te)|0,w=w+Math.imul(J,xe)|0,a=a+Math.imul(J,De)|0,a=a+Math.imul(re,xe)|0,_=_+Math.imul(re,De)|0,w=w+Math.imul(G,Oe)|0,a=a+Math.imul(G,Ie)|0,a=a+Math.imul(te,Oe)|0,_=_+Math.imul(te,Ie)|0,w=w+Math.imul(E,Re)|0,a=a+Math.imul(E,Be)|0,a=a+Math.imul(U,Re)|0,_=_+Math.imul(U,Be)|0;var tr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(tr>>>26)|0,tr&=67108863,w=Math.imul(me,we),a=Math.imul(me,Me),a=a+Math.imul(ve,we)|0,_=Math.imul(ve,Me),w=w+Math.imul(pe,be)|0,a=a+Math.imul(pe,ye)|0,a=a+Math.imul(ge,be)|0,_=_+Math.imul(ge,ye)|0,w=w+Math.imul(de,_e)|0,a=a+Math.imul(de,ke)|0,a=a+Math.imul(he,_e)|0,_=_+Math.imul(he,ke)|0,w=w+Math.imul(fe,Ae)|0,a=a+Math.imul(fe,Se)|0,a=a+Math.imul(ce,Ae)|0,_=_+Math.imul(ce,Se)|0,w=w+Math.imul(le,Ce)|0,a=a+Math.imul(le,Pe)|0,a=a+Math.imul(ue,Ce)|0,_=_+Math.imul(ue,Pe)|0,w=w+Math.imul(se,Le)|0,a=a+Math.imul(se,Te)|0,a=a+Math.imul(ae,Le)|0,_=_+Math.imul(ae,Te)|0,w=w+Math.imul(ee,xe)|0,a=a+Math.imul(ee,De)|0,a=a+Math.imul(Q,xe)|0,_=_+Math.imul(Q,De)|0,w=w+Math.imul(J,Oe)|0,a=a+Math.imul(J,Ie)|0,a=a+Math.imul(re,Oe)|0,_=_+Math.imul(re,Ie)|0,w=w+Math.imul(G,Re)|0,a=a+Math.imul(G,Be)|0,a=a+Math.imul(te,Re)|0,_=_+Math.imul(te,Be)|0,w=w+Math.imul(E,Ne)|0,a=a+Math.imul(E,Ee)|0,a=a+Math.imul(U,Ne)|0,_=_+Math.imul(U,Ee)|0;var rr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(rr>>>26)|0,rr&=67108863,w=Math.imul(me,be),a=Math.imul(me,ye),a=a+Math.imul(ve,be)|0,_=Math.imul(ve,ye),w=w+Math.imul(pe,_e)|0,a=a+Math.imul(pe,ke)|0,a=a+Math.imul(ge,_e)|0,_=_+Math.imul(ge,ke)|0,w=w+Math.imul(de,Ae)|0,a=a+Math.imul(de,Se)|0,a=a+Math.imul(he,Ae)|0,_=_+Math.imul(he,Se)|0,w=w+Math.imul(fe,Ce)|0,a=a+Math.imul(fe,Pe)|0,a=a+Math.imul(ce,Ce)|0,_=_+Math.imul(ce,Pe)|0,w=w+Math.imul(le,Le)|0,a=a+Math.imul(le,Te)|0,a=a+Math.imul(ue,Le)|0,_=_+Math.imul(ue,Te)|0,w=w+Math.imul(se,xe)|0,a=a+Math.imul(se,De)|0,a=a+Math.imul(ae,xe)|0,_=_+Math.imul(ae,De)|0,w=w+Math.imul(ee,Oe)|0,a=a+Math.imul(ee,Ie)|0,a=a+Math.imul(Q,Oe)|0,_=_+Math.imul(Q,Ie)|0,w=w+Math.imul(J,Re)|0,a=a+Math.imul(J,Be)|0,a=a+Math.imul(re,Re)|0,_=_+Math.imul(re,Be)|0,w=w+Math.imul(G,Ne)|0,a=a+Math.imul(G,Ee)|0,a=a+Math.imul(te,Ne)|0,_=_+Math.imul(te,Ee)|0;var ir=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(ir>>>26)|0,ir&=67108863,w=Math.imul(me,_e),a=Math.imul(me,ke),a=a+Math.imul(ve,_e)|0,_=Math.imul(ve,ke),w=w+Math.imul(pe,Ae)|0,a=a+Math.imul(pe,Se)|0,a=a+Math.imul(ge,Ae)|0,_=_+Math.imul(ge,Se)|0,w=w+Math.imul(de,Ce)|0,a=a+Math.imul(de,Pe)|0,a=a+Math.imul(he,Ce)|0,_=_+Math.imul(he,Pe)|0,w=w+Math.imul(fe,Le)|0,a=a+Math.imul(fe,Te)|0,a=a+Math.imul(ce,Le)|0,_=_+Math.imul(ce,Te)|0,w=w+Math.imul(le,xe)|0,a=a+Math.imul(le,De)|0,a=a+Math.imul(ue,xe)|0,_=_+Math.imul(ue,De)|0,w=w+Math.imul(se,Oe)|0,a=a+Math.imul(se,Ie)|0,a=a+Math.imul(ae,Oe)|0,_=_+Math.imul(ae,Ie)|0,w=w+Math.imul(ee,Re)|0,a=a+Math.imul(ee,Be)|0,a=a+Math.imul(Q,Re)|0,_=_+Math.imul(Q,Be)|0,w=w+Math.imul(J,Ne)|0,a=a+Math.imul(J,Ee)|0,a=a+Math.imul(re,Ne)|0,_=_+Math.imul(re,Ee)|0;var nr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(nr>>>26)|0,nr&=67108863,w=Math.imul(me,Ae),a=Math.imul(me,Se),a=a+Math.imul(ve,Ae)|0,_=Math.imul(ve,Se),w=w+Math.imul(pe,Ce)|0,a=a+Math.imul(pe,Pe)|0,a=a+Math.imul(ge,Ce)|0,_=_+Math.imul(ge,Pe)|0,w=w+Math.imul(de,Le)|0,a=a+Math.imul(de,Te)|0,a=a+Math.imul(he,Le)|0,_=_+Math.imul(he,Te)|0,w=w+Math.imul(fe,xe)|0,a=a+Math.imul(fe,De)|0,a=a+Math.imul(ce,xe)|0,_=_+Math.imul(ce,De)|0,w=w+Math.imul(le,Oe)|0,a=a+Math.imul(le,Ie)|0,a=a+Math.imul(ue,Oe)|0,_=_+Math.imul(ue,Ie)|0,w=w+Math.imul(se,Re)|0,a=a+Math.imul(se,Be)|0,a=a+Math.imul(ae,Re)|0,_=_+Math.imul(ae,Be)|0,w=w+Math.imul(ee,Ne)|0,a=a+Math.imul(ee,Ee)|0,a=a+Math.imul(Q,Ne)|0,_=_+Math.imul(Q,Ee)|0;var or=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(or>>>26)|0,or&=67108863,w=Math.imul(me,Ce),a=Math.imul(me,Pe),a=a+Math.imul(ve,Ce)|0,_=Math.imul(ve,Pe),w=w+Math.imul(pe,Le)|0,a=a+Math.imul(pe,Te)|0,a=a+Math.imul(ge,Le)|0,_=_+Math.imul(ge,Te)|0,w=w+Math.imul(de,xe)|0,a=a+Math.imul(de,De)|0,a=a+Math.imul(he,xe)|0,_=_+Math.imul(he,De)|0,w=w+Math.imul(fe,Oe)|0,a=a+Math.imul(fe,Ie)|0,a=a+Math.imul(ce,Oe)|0,_=_+Math.imul(ce,Ie)|0,w=w+Math.imul(le,Re)|0,a=a+Math.imul(le,Be)|0,a=a+Math.imul(ue,Re)|0,_=_+Math.imul(ue,Be)|0,w=w+Math.imul(se,Ne)|0,a=a+Math.imul(se,Ee)|0,a=a+Math.imul(ae,Ne)|0,_=_+Math.imul(ae,Ee)|0;var sr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(sr>>>26)|0,sr&=67108863,w=Math.imul(me,Le),a=Math.imul(me,Te),a=a+Math.imul(ve,Le)|0,_=Math.imul(ve,Te),w=w+Math.imul(pe,xe)|0,a=a+Math.imul(pe,De)|0,a=a+Math.imul(ge,xe)|0,_=_+Math.imul(ge,De)|0,w=w+Math.imul(de,Oe)|0,a=a+Math.imul(de,Ie)|0,a=a+Math.imul(he,Oe)|0,_=_+Math.imul(he,Ie)|0,w=w+Math.imul(fe,Re)|0,a=a+Math.imul(fe,Be)|0,a=a+Math.imul(ce,Re)|0,_=_+Math.imul(ce,Be)|0,w=w+Math.imul(le,Ne)|0,a=a+Math.imul(le,Ee)|0,a=a+Math.imul(ue,Ne)|0,_=_+Math.imul(ue,Ee)|0;var ar=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(ar>>>26)|0,ar&=67108863,w=Math.imul(me,xe),a=Math.imul(me,De),a=a+Math.imul(ve,xe)|0,_=Math.imul(ve,De),w=w+Math.imul(pe,Oe)|0,a=a+Math.imul(pe,Ie)|0,a=a+Math.imul(ge,Oe)|0,_=_+Math.imul(ge,Ie)|0,w=w+Math.imul(de,Re)|0,a=a+Math.imul(de,Be)|0,a=a+Math.imul(he,Re)|0,_=_+Math.imul(he,Be)|0,w=w+Math.imul(fe,Ne)|0,a=a+Math.imul(fe,Ee)|0,a=a+Math.imul(ce,Ne)|0,_=_+Math.imul(ce,Ee)|0;var lr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(lr>>>26)|0,lr&=67108863,w=Math.imul(me,Oe),a=Math.imul(me,Ie),a=a+Math.imul(ve,Oe)|0,_=Math.imul(ve,Ie),w=w+Math.imul(pe,Re)|0,a=a+Math.imul(pe,Be)|0,a=a+Math.imul(ge,Re)|0,_=_+Math.imul(ge,Be)|0,w=w+Math.imul(de,Ne)|0,a=a+Math.imul(de,Ee)|0,a=a+Math.imul(he,Ne)|0,_=_+Math.imul(he,Ee)|0;var ur=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(ur>>>26)|0,ur&=67108863,w=Math.imul(me,Re),a=Math.imul(me,Be),a=a+Math.imul(ve,Re)|0,_=Math.imul(ve,Be),w=w+Math.imul(pe,Ne)|0,a=a+Math.imul(pe,Ee)|0,a=a+Math.imul(ge,Ne)|0,_=_+Math.imul(ge,Ee)|0;var fr=(k+w|0)+((a&8191)<<13)|0;k=(_+(a>>>13)|0)+(fr>>>26)|0,fr&=67108863,w=Math.imul(me,Ne),a=Math.imul(me,Ee),a=a+Math.imul(ve,Ne)|0,_=Math.imul(ve,Ee);var cr=(k+w|0)+((a&8191)<<13)|0;return k=(_+(a>>>13)|0)+(cr>>>26)|0,cr&=67108863,v[0]=Gt,v[1]=Ht,v[2]=Vt,v[3]=Qt,v[4]=Xt,v[5]=Jt,v[6]=Yt,v[7]=er,v[8]=tr,v[9]=rr,v[10]=ir,v[11]=nr,v[12]=or,v[13]=sr,v[14]=ar,v[15]=lr,v[16]=ur,v[17]=fr,v[18]=cr,k!==0&&(v[19]=k,l.length++),l};Math.imul||(R=B);function x(h,e,s){s.negative=e.negative^h.negative,s.length=h.length+e.length;for(var l=0,f=0,d=0;d<s.length-1;d++){var v=f;f=0;for(var k=l&67108863,w=Math.min(d,e.length-1),a=Math.max(0,d-h.length+1);a<=w;a++){var _=d-a,F=h.words[_]|0,E=e.words[a]|0,U=F*E,Z=U&67108863;v=v+(U/67108864|0)|0,Z=Z+k|0,k=Z&67108863,v=v+(Z>>>26)|0,f+=v>>>26,v&=67108863}s.words[d]=k,l=v,v=f}return l!==0?s.words[d]=l:s.length--,s._strip()}function q(h,e,s){return x(h,e,s)}t.prototype.mulTo=function(e,s){var l,f=this.length+e.length;return this.length===10&&e.length===10?l=R(this,e,s):f<63?l=B(this,e,s):f<1024?l=x(this,e,s):l=q(this,e,s),l};function j(h,e){this.x=h,this.y=e}j.prototype.makeRBT=function(e){for(var s=new Array(e),l=t.prototype._countBits(e)-1,f=0;f<e;f++)s[f]=this.revBin(f,l,e);return s},j.prototype.revBin=function(e,s,l){if(e===0||e===l-1)return e;for(var f=0,d=0;d<s;d++)f|=(e&1)<<s-d-1,e>>=1;return f},j.prototype.permute=function(e,s,l,f,d,v){for(var k=0;k<v;k++)f[k]=s[e[k]],d[k]=l[e[k]]},j.prototype.transform=function(e,s,l,f,d,v){this.permute(v,e,s,l,f,d);for(var k=1;k<d;k<<=1)for(var w=k<<1,a=Math.cos(2*Math.PI/w),_=Math.sin(2*Math.PI/w),F=0;F<d;F+=w)for(var E=a,U=_,Z=0;Z<k;Z++){var G=l[F+Z],te=f[F+Z],Ze=l[F+Z+k],J=f[F+Z+k],re=E*Ze-U*J;J=E*J+U*Ze,Ze=re,l[F+Z]=G+Ze,f[F+Z]=te+J,l[F+Z+k]=G-Ze,f[F+Z+k]=te-J,Z!==w&&(re=a*E-_*U,U=a*U+_*E,E=re)}},j.prototype.guessLen13b=function(e,s){var l=Math.max(s,e)|1,f=l&1,d=0;for(l=l/2|0;l;l=l>>>1)d++;return 1<<d+1+f},j.prototype.conjugate=function(e,s,l){if(!(l<=1))for(var f=0;f<l/2;f++){var d=e[f];e[f]=e[l-f-1],e[l-f-1]=d,d=s[f],s[f]=-s[l-f-1],s[l-f-1]=-d}},j.prototype.normalize13b=function(e,s){for(var l=0,f=0;f<s/2;f++){var d=Math.round(e[2*f+1]/s)*8192+Math.round(e[2*f]/s)+l;e[f]=d&67108863,d<67108864?l=0:l=d/67108864|0}return e},j.prototype.convert13b=function(e,s,l,f){for(var d=0,v=0;v<s;v++)d=d+(e[v]|0),l[2*v]=d&8191,d=d>>>13,l[2*v+1]=d&8191,d=d>>>13;for(v=2*s;v<f;++v)l[v]=0;r(d===0),r((d&-8192)===0)},j.prototype.stub=function(e){for(var s=new Array(e),l=0;l<e;l++)s[l]=0;return s},j.prototype.mulp=function(e,s,l){var f=2*this.guessLen13b(e.length,s.length),d=this.makeRBT(f),v=this.stub(f),k=new Array(f),w=new Array(f),a=new Array(f),_=new Array(f),F=new Array(f),E=new Array(f),U=l.words;U.length=f,this.convert13b(e.words,e.length,k,f),this.convert13b(s.words,s.length,_,f),this.transform(k,v,w,a,f,d),this.transform(_,v,F,E,f,d);for(var Z=0;Z<f;Z++){var G=w[Z]*F[Z]-a[Z]*E[Z];a[Z]=w[Z]*E[Z]+a[Z]*F[Z],w[Z]=G}return this.conjugate(w,a,f),this.transform(w,a,U,v,f,d),this.conjugate(U,v,f),this.normalize13b(U,f),l.negative=e.negative^s.negative,l.length=e.length+s.length,l._strip()},t.prototype.mul=function(e){var s=new t(null);return s.words=new Array(this.length+e.length),this.mulTo(e,s)},t.prototype.mulf=function(e){var s=new t(null);return s.words=new Array(this.length+e.length),q(this,e,s)},t.prototype.imul=function(e){return this.clone().mulTo(e,this)},t.prototype.imuln=function(e){var s=e<0;s&&(e=-e),r(typeof e=="number"),r(e<67108864);for(var l=0,f=0;f<this.length;f++){var d=(this.words[f]|0)*e,v=(d&67108863)+(l&67108863);l>>=26,l+=d/67108864|0,l+=v>>>26,this.words[f]=v&67108863}return l!==0&&(this.words[f]=l,this.length++),this.length=e===0?1:this.length,s?this.ineg():this},t.prototype.muln=function(e){return this.clone().imuln(e)},t.prototype.sqr=function(){return this.mul(this)},t.prototype.isqr=function(){return this.imul(this.clone())},t.prototype.pow=function(e){var s=O(e);if(s.length===0)return new t(1);for(var l=this,f=0;f<s.length&&s[f]===0;f++,l=l.sqr());if(++f<s.length)for(var d=l.sqr();f<s.length;f++,d=d.sqr())s[f]!==0&&(l=l.mul(d));return l},t.prototype.iushln=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26,f=67108863>>>26-s<<26-s,d;if(s!==0){var v=0;for(d=0;d<this.length;d++){var k=this.words[d]&f,w=(this.words[d]|0)-k<<s;this.words[d]=w|v,v=k>>>26-s}v&&(this.words[d]=v,this.length++)}if(l!==0){for(d=this.length-1;d>=0;d--)this.words[d+l]=this.words[d];for(d=0;d<l;d++)this.words[d]=0;this.length+=l}return this._strip()},t.prototype.ishln=function(e){return r(this.negative===0),this.iushln(e)},t.prototype.iushrn=function(e,s,l){r(typeof e=="number"&&e>=0);var f;s?f=(s-s%26)/26:f=0;var d=e%26,v=Math.min((e-d)/26,this.length),k=67108863^67108863>>>d<<d,w=l;if(f-=v,f=Math.max(0,f),w){for(var a=0;a<v;a++)w.words[a]=this.words[a];w.length=v}if(v!==0)if(this.length>v)for(this.length-=v,a=0;a<this.length;a++)this.words[a]=this.words[a+v];else this.words[0]=0,this.length=1;var _=0;for(a=this.length-1;a>=0&&(_!==0||a>=f);a--){var F=this.words[a]|0;this.words[a]=_<<26-d|F>>>d,_=F&k}return w&&_!==0&&(w.words[w.length++]=_),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},t.prototype.ishrn=function(e,s,l){return r(this.negative===0),this.iushrn(e,s,l)},t.prototype.shln=function(e){return this.clone().ishln(e)},t.prototype.ushln=function(e){return this.clone().iushln(e)},t.prototype.shrn=function(e){return this.clone().ishrn(e)},t.prototype.ushrn=function(e){return this.clone().iushrn(e)},t.prototype.testn=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26,f=1<<s;if(this.length<=l)return!1;var d=this.words[l];return!!(d&f)},t.prototype.imaskn=function(e){r(typeof e=="number"&&e>=0);var s=e%26,l=(e-s)/26;if(r(this.negative===0,"imaskn works only with positive numbers"),this.length<=l)return this;if(s!==0&&l++,this.length=Math.min(l,this.length),s!==0){var f=67108863^67108863>>>s<<s;this.words[this.length-1]&=f}return this._strip()},t.prototype.maskn=function(e){return this.clone().imaskn(e)},t.prototype.iaddn=function(e){return r(typeof e=="number"),r(e<67108864),e<0?this.isubn(-e):this.negative!==0?this.length===1&&(this.words[0]|0)<=e?(this.words[0]=e-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(e),this.negative=1,this):this._iaddn(e)},t.prototype._iaddn=function(e){this.words[0]+=e;for(var s=0;s<this.length&&this.words[s]>=67108864;s++)this.words[s]-=67108864,s===this.length-1?this.words[s+1]=1:this.words[s+1]++;return this.length=Math.max(this.length,s+1),this},t.prototype.isubn=function(e){if(r(typeof e=="number"),r(e<67108864),e<0)return this.iaddn(-e);if(this.negative!==0)return this.negative=0,this.iaddn(e),this.negative=1,this;if(this.words[0]-=e,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var s=0;s<this.length&&this.words[s]<0;s++)this.words[s]+=67108864,this.words[s+1]-=1;return this._strip()},t.prototype.addn=function(e){return this.clone().iaddn(e)},t.prototype.subn=function(e){return this.clone().isubn(e)},t.prototype.iabs=function(){return this.negative=0,this},t.prototype.abs=function(){return this.clone().iabs()},t.prototype._ishlnsubmul=function(e,s,l){var f=e.length+l,d;this._expand(f);var v,k=0;for(d=0;d<e.length;d++){v=(this.words[d+l]|0)+k;var w=(e.words[d]|0)*s;v-=w&67108863,k=(v>>26)-(w/67108864|0),this.words[d+l]=v&67108863}for(;d<this.length-l;d++)v=(this.words[d+l]|0)+k,k=v>>26,this.words[d+l]=v&67108863;if(k===0)return this._strip();for(r(k===-1),k=0,d=0;d<this.length;d++)v=-(this.words[d]|0)+k,k=v>>26,this.words[d]=v&67108863;return this.negative=1,this._strip()},t.prototype._wordDiv=function(e,s){var l=this.length-e.length,f=this.clone(),d=e,v=d.words[d.length-1]|0,k=this._countBits(v);l=26-k,l!==0&&(d=d.ushln(l),f.iushln(l),v=d.words[d.length-1]|0);var w=f.length-d.length,a;if(s!=="mod"){a=new t(null),a.length=w+1,a.words=new Array(a.length);for(var _=0;_<a.length;_++)a.words[_]=0}var F=f.clone()._ishlnsubmul(d,1,w);F.negative===0&&(f=F,a&&(a.words[w]=1));for(var E=w-1;E>=0;E--){var U=(f.words[d.length+E]|0)*67108864+(f.words[d.length+E-1]|0);for(U=Math.min(U/v|0,67108863),f._ishlnsubmul(d,U,E);f.negative!==0;)U--,f.negative=0,f._ishlnsubmul(d,1,E),f.isZero()||(f.negative^=1);a&&(a.words[E]=U)}return a&&a._strip(),f._strip(),s!=="div"&&l!==0&&f.iushrn(l),{div:a||null,mod:f}},t.prototype.divmod=function(e,s,l){if(r(!e.isZero()),this.isZero())return{div:new t(0),mod:new t(0)};var f,d,v;return this.negative!==0&&e.negative===0?(v=this.neg().divmod(e,s),s!=="mod"&&(f=v.div.neg()),s!=="div"&&(d=v.mod.neg(),l&&d.negative!==0&&d.iadd(e)),{div:f,mod:d}):this.negative===0&&e.negative!==0?(v=this.divmod(e.neg(),s),s!=="mod"&&(f=v.div.neg()),{div:f,mod:v.mod}):(this.negative&e.negative)!==0?(v=this.neg().divmod(e.neg(),s),s!=="div"&&(d=v.mod.neg(),l&&d.negative!==0&&d.isub(e)),{div:v.div,mod:d}):e.length>this.length||this.cmp(e)<0?{div:new t(0),mod:this}:e.length===1?s==="div"?{div:this.divn(e.words[0]),mod:null}:s==="mod"?{div:null,mod:new t(this.modrn(e.words[0]))}:{div:this.divn(e.words[0]),mod:new t(this.modrn(e.words[0]))}:this._wordDiv(e,s)},t.prototype.div=function(e){return this.divmod(e,"div",!1).div},t.prototype.mod=function(e){return this.divmod(e,"mod",!1).mod},t.prototype.umod=function(e){return this.divmod(e,"mod",!0).mod},t.prototype.divRound=function(e){var s=this.divmod(e);if(s.mod.isZero())return s.div;var l=s.div.negative!==0?s.mod.isub(e):s.mod,f=e.ushrn(1),d=e.andln(1),v=l.cmp(f);return v<0||d===1&&v===0?s.div:s.div.negative!==0?s.div.isubn(1):s.div.iaddn(1)},t.prototype.modrn=function(e){var s=e<0;s&&(e=-e),r(e<=67108863);for(var l=(1<<26)%e,f=0,d=this.length-1;d>=0;d--)f=(l*f+(this.words[d]|0))%e;return s?-f:f},t.prototype.modn=function(e){return this.modrn(e)},t.prototype.idivn=function(e){var s=e<0;s&&(e=-e),r(e<=67108863);for(var l=0,f=this.length-1;f>=0;f--){var d=(this.words[f]|0)+l*67108864;this.words[f]=d/e|0,l=d%e}return this._strip(),s?this.ineg():this},t.prototype.divn=function(e){return this.clone().idivn(e)},t.prototype.egcd=function(e){r(e.negative===0),r(!e.isZero());var s=this,l=e.clone();s.negative!==0?s=s.umod(e):s=s.clone();for(var f=new t(1),d=new t(0),v=new t(0),k=new t(1),w=0;s.isEven()&&l.isEven();)s.iushrn(1),l.iushrn(1),++w;for(var a=l.clone(),_=s.clone();!s.isZero();){for(var F=0,E=1;(s.words[0]&E)===0&&F<26;++F,E<<=1);if(F>0)for(s.iushrn(F);F-- >0;)(f.isOdd()||d.isOdd())&&(f.iadd(a),d.isub(_)),f.iushrn(1),d.iushrn(1);for(var U=0,Z=1;(l.words[0]&Z)===0&&U<26;++U,Z<<=1);if(U>0)for(l.iushrn(U);U-- >0;)(v.isOdd()||k.isOdd())&&(v.iadd(a),k.isub(_)),v.iushrn(1),k.iushrn(1);s.cmp(l)>=0?(s.isub(l),f.isub(v),d.isub(k)):(l.isub(s),v.isub(f),k.isub(d))}return{a:v,b:k,gcd:l.iushln(w)}},t.prototype._invmp=function(e){r(e.negative===0),r(!e.isZero());var s=this,l=e.clone();s.negative!==0?s=s.umod(e):s=s.clone();for(var f=new t(1),d=new t(0),v=l.clone();s.cmpn(1)>0&&l.cmpn(1)>0;){for(var k=0,w=1;(s.words[0]&w)===0&&k<26;++k,w<<=1);if(k>0)for(s.iushrn(k);k-- >0;)f.isOdd()&&f.iadd(v),f.iushrn(1);for(var a=0,_=1;(l.words[0]&_)===0&&a<26;++a,_<<=1);if(a>0)for(l.iushrn(a);a-- >0;)d.isOdd()&&d.iadd(v),d.iushrn(1);s.cmp(l)>=0?(s.isub(l),f.isub(d)):(l.isub(s),d.isub(f))}var F;return s.cmpn(1)===0?F=f:F=d,F.cmpn(0)<0&&F.iadd(e),F},t.prototype.gcd=function(e){if(this.isZero())return e.abs();if(e.isZero())return this.abs();var s=this.clone(),l=e.clone();s.negative=0,l.negative=0;for(var f=0;s.isEven()&&l.isEven();f++)s.iushrn(1),l.iushrn(1);do{for(;s.isEven();)s.iushrn(1);for(;l.isEven();)l.iushrn(1);var d=s.cmp(l);if(d<0){var v=s;s=l,l=v}else if(d===0||l.cmpn(1)===0)break;s.isub(l)}while(!0);return l.iushln(f)},t.prototype.invm=function(e){return this.egcd(e).a.umod(e)},t.prototype.isEven=function(){return(this.words[0]&1)===0},t.prototype.isOdd=function(){return(this.words[0]&1)===1},t.prototype.andln=function(e){return this.words[0]&e},t.prototype.bincn=function(e){r(typeof e=="number");var s=e%26,l=(e-s)/26,f=1<<s;if(this.length<=l)return this._expand(l+1),this.words[l]|=f,this;for(var d=f,v=l;d!==0&&v<this.length;v++){var k=this.words[v]|0;k+=d,d=k>>>26,k&=67108863,this.words[v]=k}return d!==0&&(this.words[v]=d,this.length++),this},t.prototype.isZero=function(){return this.length===1&&this.words[0]===0},t.prototype.cmpn=function(e){var s=e<0;if(this.negative!==0&&!s)return-1;if(this.negative===0&&s)return 1;this._strip();var l;if(this.length>1)l=1;else{s&&(e=-e),r(e<=67108863,"Number is too big");var f=this.words[0]|0;l=f===e?0:f<e?-1:1}return this.negative!==0?-l|0:l},t.prototype.cmp=function(e){if(this.negative!==0&&e.negative===0)return-1;if(this.negative===0&&e.negative!==0)return 1;var s=this.ucmp(e);return this.negative!==0?-s|0:s},t.prototype.ucmp=function(e){if(this.length>e.length)return 1;if(this.length<e.length)return-1;for(var s=0,l=this.length-1;l>=0;l--){var f=this.words[l]|0,d=e.words[l]|0;if(f!==d){f<d?s=-1:f>d&&(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 P(e)},t.prototype.toRed=function(e){return r(!this.red,"Already a number in reduction context"),r(this.negative===0,"red works only with positives"),e.convertTo(this)._forceRed(e)},t.prototype.fromRed=function(){return r(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},t.prototype._forceRed=function(e){return this.red=e,this},t.prototype.forceRed=function(e){return r(!this.red,"Already a number in reduction context"),this._forceRed(e)},t.prototype.redAdd=function(e){return r(this.red,"redAdd works only with red numbers"),this.red.add(this,e)},t.prototype.redIAdd=function(e){return r(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,e)},t.prototype.redSub=function(e){return r(this.red,"redSub works only with red numbers"),this.red.sub(this,e)},t.prototype.redISub=function(e){return r(this.red,"redISub works only with red numbers"),this.red.isub(this,e)},t.prototype.redShl=function(e){return r(this.red,"redShl works only with red numbers"),this.red.shl(this,e)},t.prototype.redMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.mul(this,e)},t.prototype.redIMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.imul(this,e)},t.prototype.redSqr=function(){return r(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},t.prototype.redISqr=function(){return r(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},t.prototype.redSqrt=function(){return r(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},t.prototype.redInvm=function(){return r(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},t.prototype.redNeg=function(){return r(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},t.prototype.redPow=function(e){return r(this.red&&!e.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,e)};var W={k256:null,p224:null,p192:null,p25519:null};function $(h,e){this.name=h,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()}$.prototype._tmp=function(){var e=new t(null);return e.words=new Array(Math.ceil(this.n/13)),e},$.prototype.ireduce=function(e){var s=e,l;do this.split(s,this.tmp),s=this.imulK(s),s=s.iadd(this.tmp),l=s.bitLength();while(l>this.n);var f=l<this.n?-1:s.ucmp(this.p);return f===0?(s.words[0]=0,s.length=1):f>0?s.isub(this.p):s.strip!==void 0?s.strip():s._strip(),s},$.prototype.split=function(e,s){e.iushrn(this.n,0,s)},$.prototype.imulK=function(e){return e.imul(this.k)};function z(){$.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}o(z,$),z.prototype.split=function(e,s){for(var l=4194303,f=Math.min(e.length,9),d=0;d<f;d++)s.words[d]=e.words[d];if(s.length=f,e.length<=9){e.words[0]=0,e.length=1;return}var v=e.words[9];for(s.words[s.length++]=v&l,d=10;d<e.length;d++){var k=e.words[d]|0;e.words[d-10]=(k&l)<<4|v>>>22,v=k}v>>>=22,e.words[d-10]=v,v===0&&e.length>10?e.length-=10:e.length-=9},z.prototype.imulK=function(e){e.words[e.length]=0,e.words[e.length+1]=0,e.length+=2;for(var s=0,l=0;l<e.length;l++){var f=e.words[l]|0;s+=f*977,e.words[l]=s&67108863,s=f*64+(s/67108864|0)}return e.words[e.length-1]===0&&(e.length--,e.words[e.length-1]===0&&e.length--),e};function m(){$.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}o(m,$);function y(){$.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}o(y,$);function A(){$.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}o(A,$),A.prototype.imulK=function(e){for(var s=0,l=0;l<e.length;l++){var f=(e.words[l]|0)*19+s,d=f&67108863;f>>>=26,e.words[l]=d,s=f}return s!==0&&(e.words[e.length++]=s),e},t._prime=function(e){if(W[e])return W[e];var s;if(e==="k256")s=new z;else if(e==="p224")s=new m;else if(e==="p192")s=new y;else if(e==="p25519")s=new A;else throw new Error("Unknown prime "+e);return W[e]=s,s};function P(h){if(typeof h=="string"){var e=t._prime(h);this.m=e.p,this.prime=e}else r(h.gtn(1),"modulus must be greater than 1"),this.m=h,this.prime=null}P.prototype._verify1=function(e){r(e.negative===0,"red works only with positives"),r(e.red,"red works only with red numbers")},P.prototype._verify2=function(e,s){r((e.negative|s.negative)===0,"red works only with positives"),r(e.red&&e.red===s.red,"red works only with red numbers")},P.prototype.imod=function(e){return this.prime?this.prime.ireduce(e)._forceRed(this):(M(e,e.umod(this.m)._forceRed(this)),e)},P.prototype.neg=function(e){return e.isZero()?e.clone():this.m.sub(e)._forceRed(this)},P.prototype.add=function(e,s){this._verify2(e,s);var l=e.add(s);return l.cmp(this.m)>=0&&l.isub(this.m),l._forceRed(this)},P.prototype.iadd=function(e,s){this._verify2(e,s);var l=e.iadd(s);return l.cmp(this.m)>=0&&l.isub(this.m),l},P.prototype.sub=function(e,s){this._verify2(e,s);var l=e.sub(s);return l.cmpn(0)<0&&l.iadd(this.m),l._forceRed(this)},P.prototype.isub=function(e,s){this._verify2(e,s);var l=e.isub(s);return l.cmpn(0)<0&&l.iadd(this.m),l},P.prototype.shl=function(e,s){return this._verify1(e),this.imod(e.ushln(s))},P.prototype.imul=function(e,s){return this._verify2(e,s),this.imod(e.imul(s))},P.prototype.mul=function(e,s){return this._verify2(e,s),this.imod(e.mul(s))},P.prototype.isqr=function(e){return this.imul(e,e.clone())},P.prototype.sqr=function(e){return this.mul(e,e)},P.prototype.sqrt=function(e){if(e.isZero())return e.clone();var s=this.m.andln(3);if(r(s%2===1),s===3){var l=this.m.add(new t(1)).iushrn(2);return this.pow(e,l)}for(var f=this.m.subn(1),d=0;!f.isZero()&&f.andln(1)===0;)d++,f.iushrn(1);r(!f.isZero());var v=new t(1).toRed(this),k=v.redNeg(),w=this.m.subn(1).iushrn(1),a=this.m.bitLength();for(a=new t(2*a*a).toRed(this);this.pow(a,w).cmp(k)!==0;)a.redIAdd(k);for(var _=this.pow(a,f),F=this.pow(e,f.addn(1).iushrn(1)),E=this.pow(e,f),U=d;E.cmp(v)!==0;){for(var Z=E,G=0;Z.cmp(v)!==0;G++)Z=Z.redSqr();r(G<U);var te=this.pow(_,new t(1).iushln(U-G-1));F=F.redMul(te),_=te.redSqr(),E=E.redMul(_),U=G}return F},P.prototype.invm=function(e){var s=e._invmp(this.m);return s.negative!==0?(s.negative=0,this.imod(s).redNeg()):this.imod(s)},P.prototype.pow=function(e,s){if(s.isZero())return new t(1).toRed(this);if(s.cmpn(1)===0)return e.clone();var l=4,f=new Array(1<<l);f[0]=new t(1).toRed(this),f[1]=e;for(var d=2;d<f.length;d++)f[d]=this.mul(f[d-1],e);var v=f[0],k=0,w=0,a=s.bitLength()%26;for(a===0&&(a=26),d=s.length-1;d>=0;d--){for(var _=s.words[d],F=a-1;F>=0;F--){var E=_>>F&1;if(v!==f[0]&&(v=this.sqr(v)),E===0&&k===0){w=0;continue}k<<=1,k|=E,w++,!(w!==l&&(d!==0||F!==0))&&(v=this.mul(v,f[k]),w=0,k=0)}a=26}return v},P.prototype.convertTo=function(e){var s=e.umod(this.m);return s===e?s.clone():s},P.prototype.convertFrom=function(e){var s=e.clone();return s.red=null,s},t.mont=function(e){return new L(e)};function L(h){P.call(this,h),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(L,P),L.prototype.convertTo=function(e){return this.imod(e.ushln(this.shift))},L.prototype.convertFrom=function(e){var s=this.imod(e.mul(this.rinv));return s.red=null,s},L.prototype.imul=function(e,s){if(e.isZero()||s.isZero())return e.words[0]=0,e.length=1,e;var l=e.imul(s),f=l.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),d=l.isub(f).iushrn(this.shift),v=d;return d.cmp(this.m)>=0?v=d.isub(this.m):d.cmpn(0)<0&&(v=d.iadd(this.m)),v._forceRed(this)},L.prototype.mul=function(e,s){if(e.isZero()||s.isZero())return new t(0)._forceRed(this);var l=e.mul(s),f=l.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),d=l.isub(f).iushrn(this.shift),v=d;return d.cmp(this.m)>=0?v=d.isub(this.m):d.cmpn(0)<0&&(v=d.iadd(this.m)),v._forceRed(this)},L.prototype.invm=function(e){var s=this.imod(e._invmp(this.m).mul(this.r2));return s._forceRed(this)}})(typeof Cr>"u"||Cr,wi)});import{SdkWrapper as lo}from"@cetusprotocol/common-sdk";import{DefaultProviders as Fi,FullRpcUrlMainnet as Ui}from"@cetusprotocol/common-sdk";var Vr={full_rpc_url:Ui,env:"mainnet",aggregator_url:"https://api-sui.cetus.zone/router_v3",providers:Fi,leverage:{package_id:"0xc41dca0f7de9e155862521e4d73386e2a45049c5c3c3fa6033525473fa6c634d",published_at:"0xc41dca0f7de9e155862521e4d73386e2a45049c5c3c3fa6033525473fa6c634d",version:0,config:{versioned:"0x16e25bbbb9b06db1ae9c3ebcb381f670ee3ca3b2647981e9cc0afbbfadb0e9e5",admin_cap_id:"0x5056062528bfac02d34f25b623ed6e34b4cd36fdaf6608e4bd593a13207f9b59",global_config_id:"0xeb7d17f241a94c8f9c7f6f363926d63a0bc608616adb409ab1e3abbc3e4d8ea0",markets:"0x2ed42decf1d64d0eb4af233e827a09c8d653d7fb1a40295cf818022abbc26914",markets_table_id:"0xb4aad4cb3690a5fc2033422fce919c9b8eb3c90102e9501b01a9fcf9dc7ea162"}},suilend:{package_id:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf",published_at:"0xe37cc7bb50fd9b6dbd3873df66fa2c554e973697f50ef97707311dc78bd08444",config:{lendingMarket:[{name:"Main market",slug:"main",id:"0x84030d26d85eaa7035084a057f2f11f701b7e2e4eda87551becbc7c97505ece1",type:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf::suilend::MAIN_POOL",ownerCapId:"0xf7a4defe0b6566b6a2674a02a0c61c9f99bd012eed21bc741a069eaa82d35927"},{name:"STEAMM LM",slug:"steamm-lm",id:"0xc1888ec1b81a414e427a44829310508352aec38252ee0daa9f8b181b6947de9f",type:"0x0a071f4976abae1a7f722199cf0bfcbe695ef9408a878e7d12a7ca87b7e582a6::lp_rewards::LP_REWARDS",ownerCapId:"0x55a0f33b24e091830302726c8cfbff8cf8abd2ec1f83a4e6f4bf51c7ba3ad5ab",isHidden:!0}],lendingMarketId:"0x84030d26d85eaa7035084a057f2f11f701b7e2e4eda87551becbc7c97505ece1",lendingMarketType:"0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf::suilend::MAIN_POOL",apiUrl:"https://d10td5ybgrf39v.cloudfront.net"}}};import{SuilendClient as Xi}from"@suilend/sdk/client";import{initializeSuilend as Ji,initializeSuilendRewards as Yi,initializeObligations as en,formatRewards as tn,Side as lt,getFilteredRewards as rn,getStakingYieldAprPercent as nn,getDedupedPerDayRewards as on,getDedupedAprRewards as sn,getTotalAprPercent as ri,getNetAprPercent as an}from"@suilend/sdk";var qi=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,dr=Math.ceil,Je=Math.floor,Ge="[BigNumber Error] ",Qr=Ge+"Number primitive has more than 15 significant digits: ",et=1e14,X=14,hr=9007199254740991,pr=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],at=1e7,qe=1e9;function Xr(n){var i,r,o,t=x.prototype={constructor:x,toString:null,valueOf:null},u=new x(1),p=20,c=4,g=-7,M=21,b=-1e7,S=1e7,C=!1,T=1,D=0,O={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:"\xA0",suffix:""},B="0123456789abcdefghijklmnopqrstuvwxyz",R=!0;function x(m,y){var A,P,L,h,e,s,l,f,d=this;if(!(d instanceof x))return new x(m,y);if(y==null){if(m&&m._isBigNumber===!0){d.s=m.s,!m.c||m.e>S?d.c=d.e=null:m.e<b?d.c=[d.e=0]:(d.e=m.e,d.c=m.c.slice());return}if((s=typeof m=="number")&&m*0==0){if(d.s=1/m<0?(m=-m,-1):1,m===~~m){for(h=0,e=m;e>=10;e/=10,h++);h>S?d.c=d.e=null:(d.e=h,d.c=[m]);return}f=String(m)}else{if(!qi.test(f=String(m)))return o(d,f,s);d.s=f.charCodeAt(0)==45?(f=f.slice(1),-1):1}(h=f.indexOf("."))>-1&&(f=f.replace(".","")),(e=f.search(/e/i))>0?(h<0&&(h=e),h+=+f.slice(e+1),f=f.substring(0,e)):h<0&&(h=f.length)}else{if(oe(y,2,B.length,"Base"),y==10&&R)return d=new x(m),$(d,p+d.e+1,c);if(f=String(m),s=typeof m=="number"){if(m*0!=0)return o(d,f,s,y);if(d.s=1/m<0?(f=f.slice(1),-1):1,x.DEBUG&&f.replace(/^0\.0*|\./,"").length>15)throw Error(Qr+m)}else d.s=f.charCodeAt(0)===45?(f=f.slice(1),-1):1;for(A=B.slice(0,y),h=e=0,l=f.length;e<l;e++)if(A.indexOf(P=f.charAt(e))<0){if(P=="."){if(e>h){h=l;continue}}else if(!L&&(f==f.toUpperCase()&&(f=f.toLowerCase())||f==f.toLowerCase()&&(f=f.toUpperCase()))){L=!0,e=-1,h=0;continue}return o(d,String(m),s,y)}s=!1,f=r(f,y,10,d.s),(h=f.indexOf("."))>-1?f=f.replace(".",""):h=f.length}for(e=0;f.charCodeAt(e)===48;e++);for(l=f.length;f.charCodeAt(--l)===48;);if(f=f.slice(e,++l)){if(l-=e,s&&x.DEBUG&&l>15&&(m>hr||m!==Je(m)))throw Error(Qr+d.s*m);if((h=h-e-1)>S)d.c=d.e=null;else if(h<b)d.c=[d.e=0];else{if(d.e=h,d.c=[],e=(h+1)%X,h<0&&(e+=X),e<l){for(e&&d.c.push(+f.slice(0,e)),l-=X;e<l;)d.c.push(+f.slice(e,e+=X));e=X-(f=f.slice(e)).length}else e-=l;for(;e--;f+="0");d.c.push(+f)}}else d.c=[d.e=0]}x.clone=Xr,x.ROUND_UP=0,x.ROUND_DOWN=1,x.ROUND_CEIL=2,x.ROUND_FLOOR=3,x.ROUND_HALF_UP=4,x.ROUND_HALF_DOWN=5,x.ROUND_HALF_EVEN=6,x.ROUND_HALF_CEIL=7,x.ROUND_HALF_FLOOR=8,x.EUCLID=9,x.config=x.set=function(m){var y,A;if(m!=null)if(typeof m=="object"){if(m.hasOwnProperty(y="DECIMAL_PLACES")&&(A=m[y],oe(A,0,qe,y),p=A),m.hasOwnProperty(y="ROUNDING_MODE")&&(A=m[y],oe(A,0,8,y),c=A),m.hasOwnProperty(y="EXPONENTIAL_AT")&&(A=m[y],A&&A.pop?(oe(A[0],-qe,0,y),oe(A[1],0,qe,y),g=A[0],M=A[1]):(oe(A,-qe,qe,y),g=-(M=A<0?-A:A))),m.hasOwnProperty(y="RANGE"))if(A=m[y],A&&A.pop)oe(A[0],-qe,-1,y),oe(A[1],1,qe,y),b=A[0],S=A[1];else if(oe(A,-qe,qe,y),A)b=-(S=A<0?-A:A);else throw Error(Ge+y+" cannot be zero: "+A);if(m.hasOwnProperty(y="CRYPTO"))if(A=m[y],A===!!A)if(A)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))C=A;else throw C=!A,Error(Ge+"crypto unavailable");else C=A;else throw Error(Ge+y+" not true or false: "+A);if(m.hasOwnProperty(y="MODULO_MODE")&&(A=m[y],oe(A,0,9,y),T=A),m.hasOwnProperty(y="POW_PRECISION")&&(A=m[y],oe(A,0,qe,y),D=A),m.hasOwnProperty(y="FORMAT"))if(A=m[y],typeof A=="object")O=A;else throw Error(Ge+y+" not an object: "+A);if(m.hasOwnProperty(y="ALPHABET"))if(A=m[y],typeof A=="string"&&!/^.?$|[+\-.\s]|(.).*\1/.test(A))R=A.slice(0,10)=="0123456789",B=A;else throw Error(Ge+y+" invalid: "+A)}else throw Error(Ge+"Object expected: "+m);return{DECIMAL_PLACES:p,ROUNDING_MODE:c,EXPONENTIAL_AT:[g,M],RANGE:[b,S],CRYPTO:C,MODULO_MODE:T,POW_PRECISION:D,FORMAT:O,ALPHABET:B}},x.isBigNumber=function(m){if(!m||m._isBigNumber!==!0)return!1;if(!x.DEBUG)return!0;var y,A,P=m.c,L=m.e,h=m.s;e:if({}.toString.call(P)=="[object Array]"){if((h===1||h===-1)&&L>=-qe&&L<=qe&&L===Je(L)){if(P[0]===0){if(L===0&&P.length===1)return!0;break e}if(y=(L+1)%X,y<1&&(y+=X),String(P[0]).length==y){for(y=0;y<P.length;y++)if(A=P[y],A<0||A>=et||A!==Je(A))break e;if(A!==0)return!0}}}else if(P===null&&L===null&&(h===null||h===1||h===-1))return!0;throw Error(Ge+"Invalid BigNumber: "+m)},x.maximum=x.max=function(){return j(arguments,-1)},x.minimum=x.min=function(){return j(arguments,1)},x.random=(function(){var m=9007199254740992,y=Math.random()*m&2097151?function(){return Je(Math.random()*m)}:function(){return(Math.random()*1073741824|0)*8388608+(Math.random()*8388608|0)};return function(A){var P,L,h,e,s,l=0,f=[],d=new x(u);if(A==null?A=p:oe(A,0,qe),e=dr(A/X),C)if(crypto.getRandomValues){for(P=crypto.getRandomValues(new Uint32Array(e*=2));l<e;)s=P[l]*131072+(P[l+1]>>>11),s>=9e15?(L=crypto.getRandomValues(new Uint32Array(2)),P[l]=L[0],P[l+1]=L[1]):(f.push(s%1e14),l+=2);l=e/2}else if(crypto.randomBytes){for(P=crypto.randomBytes(e*=7);l<e;)s=(P[l]&31)*281474976710656+P[l+1]*1099511627776+P[l+2]*4294967296+P[l+3]*16777216+(P[l+4]<<16)+(P[l+5]<<8)+P[l+6],s>=9e15?crypto.randomBytes(7).copy(P,l):(f.push(s%1e14),l+=7);l=e/7}else throw C=!1,Error(Ge+"crypto unavailable");if(!C)for(;l<e;)s=y(),s<9e15&&(f[l++]=s%1e14);for(e=f[--l],A%=X,e&&A&&(s=pr[X-A],f[l]=Je(e/s)*s);f[l]===0;f.pop(),l--);if(l<0)f=[h=0];else{for(h=-1;f[0]===0;f.splice(0,1),h-=X);for(l=1,s=f[0];s>=10;s/=10,l++);l<X&&(h-=X-l)}return d.e=h,d.c=f,d}})(),x.sum=function(){for(var m=1,y=arguments,A=new x(y[0]);m<y.length;)A=A.plus(y[m++]);return A},r=(function(){var m="0123456789";function y(A,P,L,h){for(var e,s=[0],l,f=0,d=A.length;f<d;){for(l=s.length;l--;s[l]*=P);for(s[0]+=h.indexOf(A.charAt(f++)),e=0;e<s.length;e++)s[e]>L-1&&(s[e+1]==null&&(s[e+1]=0),s[e+1]+=s[e]/L|0,s[e]%=L)}return s.reverse()}return function(A,P,L,h,e){var s,l,f,d,v,k,w,a,_=A.indexOf("."),F=p,E=c;for(_>=0&&(d=D,D=0,A=A.replace(".",""),a=new x(P),k=a.pow(A.length-_),D=d,a.c=y(ot(Xe(k.c),k.e,"0"),10,L,m),a.e=a.c.length),w=y(A,P,L,e?(s=B,m):(s=m,B)),f=d=w.length;w[--d]==0;w.pop());if(!w[0])return s.charAt(0);if(_<0?--f:(k.c=w,k.e=f,k.s=h,k=i(k,a,F,E,L),w=k.c,v=k.r,f=k.e),l=f+F+1,_=w[l],d=L/2,v=v||l<0||w[l+1]!=null,v=E<4?(_!=null||v)&&(E==0||E==(k.s<0?3:2)):_>d||_==d&&(E==4||v||E==6&&w[l-1]&1||E==(k.s<0?8:7)),l<1||!w[0])A=v?ot(s.charAt(1),-F,s.charAt(0)):s.charAt(0);else{if(w.length=l,v)for(--L;++w[--l]>L;)w[l]=0,l||(++f,w=[1].concat(w));for(d=w.length;!w[--d];);for(_=0,A="";_<=d;A+=s.charAt(w[_++]));A=ot(A,f,s.charAt(0))}return A}})(),i=(function(){function m(P,L,h){var e,s,l,f,d=0,v=P.length,k=L%at,w=L/at|0;for(P=P.slice();v--;)l=P[v]%at,f=P[v]/at|0,e=w*l+f*k,s=k*l+e%at*at+d,d=(s/h|0)+(e/at|0)+w*f,P[v]=s%h;return d&&(P=[d].concat(P)),P}function y(P,L,h,e){var s,l;if(h!=e)l=h>e?1:-1;else for(s=l=0;s<h;s++)if(P[s]!=L[s]){l=P[s]>L[s]?1:-1;break}return l}function A(P,L,h,e){for(var s=0;h--;)P[h]-=s,s=P[h]<L[h]?1:0,P[h]=s*e+P[h]-L[h];for(;!P[0]&&P.length>1;P.splice(0,1));}return function(P,L,h,e,s){var l,f,d,v,k,w,a,_,F,E,U,Z,G,te,Ze,J,re,Ke=P.s==L.s?1:-1,ee=P.c,Q=L.c;if(!ee||!ee[0]||!Q||!Q[0])return new x(!P.s||!L.s||(ee?Q&&ee[0]==Q[0]:!Q)?NaN:ee&&ee[0]==0||!Q?Ke*0:Ke/0);for(_=new x(Ke),F=_.c=[],f=P.e-L.e,Ke=h+f+1,s||(s=et,f=Ye(P.e/X)-Ye(L.e/X),Ke=Ke/X|0),d=0;Q[d]==(ee[d]||0);d++);if(Q[d]>(ee[d]||0)&&f--,Ke<0)F.push(1),v=!0;else{for(te=ee.length,J=Q.length,d=0,Ke+=2,k=Je(s/(Q[0]+1)),k>1&&(Q=m(Q,k,s),ee=m(ee,k,s),J=Q.length,te=ee.length),G=J,E=ee.slice(0,J),U=E.length;U<J;E[U++]=0);re=Q.slice(),re=[0].concat(re),Ze=Q[0],Q[1]>=s/2&&Ze++;do{if(k=0,l=y(Q,E,J,U),l<0){if(Z=E[0],J!=U&&(Z=Z*s+(E[1]||0)),k=Je(Z/Ze),k>1)for(k>=s&&(k=s-1),w=m(Q,k,s),a=w.length,U=E.length;y(w,E,a,U)==1;)k--,A(w,J<a?re:Q,a,s),a=w.length,l=1;else k==0&&(l=k=1),w=Q.slice(),a=w.length;if(a<U&&(w=[0].concat(w)),A(E,w,U,s),U=E.length,l==-1)for(;y(Q,E,J,U)<1;)k++,A(E,J<U?re:Q,U,s),U=E.length}else l===0&&(k++,E=[0]);F[d++]=k,E[0]?E[U++]=ee[G]||0:(E=[ee[G]],U=1)}while((G++<te||E[0]!=null)&&Ke--);v=E[0]!=null,F[0]||F.splice(0,1)}if(s==et){for(d=1,Ke=F[0];Ke>=10;Ke/=10,d++);$(_,h+(_.e=d+f*X-1)+1,e,v)}else _.e=f,_.r=+v;return _}})();function q(m,y,A,P){var L,h,e,s,l;if(A==null?A=c:oe(A,0,8),!m.c)return m.toString();if(L=m.c[0],e=m.e,y==null)l=Xe(m.c),l=P==1||P==2&&(e<=g||e>=M)?Lt(l,e):ot(l,e,"0");else if(m=$(new x(m),y,A),h=m.e,l=Xe(m.c),s=l.length,P==1||P==2&&(y<=h||h<=g)){for(;s<y;l+="0",s++);l=Lt(l,h)}else if(y-=e+(P===2&&h>e),l=ot(l,h,"0"),h+1>s){if(--y>0)for(l+=".";y--;l+="0");}else if(y+=h-s,y>0)for(h+1==s&&(l+=".");y--;l+="0");return m.s<0&&L?"-"+l:l}function j(m,y){for(var A,P,L=1,h=new x(m[0]);L<m.length;L++)P=new x(m[L]),(!P.s||(A=gt(h,P))===y||A===0&&h.s===y)&&(h=P);return h}function W(m,y,A){for(var P=1,L=y.length;!y[--L];y.pop());for(L=y[0];L>=10;L/=10,P++);return(A=P+A*X-1)>S?m.c=m.e=null:A<b?m.c=[m.e=0]:(m.e=A,m.c=y),m}o=(function(){var m=/^(-?)0([xbo])(?=\w[\w.]*$)/i,y=/^([^.]+)\.$/,A=/^\.([^.]+)$/,P=/^-?(Infinity|NaN)$/,L=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(h,e,s,l){var f,d=s?e:e.replace(L,"");if(P.test(d))h.s=isNaN(d)?null:d<0?-1:1;else{if(!s&&(d=d.replace(m,function(v,k,w){return f=(w=w.toLowerCase())=="x"?16:w=="b"?2:8,!l||l==f?k:v}),l&&(f=l,d=d.replace(y,"$1").replace(A,"0.$1")),e!=d))return new x(d,f);if(x.DEBUG)throw Error(Ge+"Not a"+(l?" base "+l:"")+" number: "+e);h.s=null}h.c=h.e=null}})();function $(m,y,A,P){var L,h,e,s,l,f,d,v=m.c,k=pr;if(v){e:{for(L=1,s=v[0];s>=10;s/=10,L++);if(h=y-L,h<0)h+=X,e=y,l=v[f=0],d=Je(l/k[L-e-1]%10);else if(f=dr((h+1)/X),f>=v.length)if(P){for(;v.length<=f;v.push(0));l=d=0,L=1,h%=X,e=h-X+1}else break e;else{for(l=s=v[f],L=1;s>=10;s/=10,L++);h%=X,e=h-X+L,d=e<0?0:Je(l/k[L-e-1]%10)}if(P=P||y<0||v[f+1]!=null||(e<0?l:l%k[L-e-1]),P=A<4?(d||P)&&(A==0||A==(m.s<0?3:2)):d>5||d==5&&(A==4||P||A==6&&(h>0?e>0?l/k[L-e]:0:v[f-1])%10&1||A==(m.s<0?8:7)),y<1||!v[0])return v.length=0,P?(y-=m.e+1,v[0]=k[(X-y%X)%X],m.e=-y||0):v[0]=m.e=0,m;if(h==0?(v.length=f,s=1,f--):(v.length=f+1,s=k[X-h],v[f]=e>0?Je(l/k[L-e]%k[e])*s:0),P)for(;;)if(f==0){for(h=1,e=v[0];e>=10;e/=10,h++);for(e=v[0]+=s,s=1;e>=10;e/=10,s++);h!=s&&(m.e++,v[0]==et&&(v[0]=1));break}else{if(v[f]+=s,v[f]!=et)break;v[f--]=0,s=1}for(h=v.length;v[--h]===0;v.pop());}m.e>S?m.c=m.e=null:m.e<b&&(m.c=[m.e=0])}return m}function z(m){var y,A=m.e;return A===null?m.toString():(y=Xe(m.c),y=A<=g||A>=M?Lt(y,A):ot(y,A,"0"),m.s<0?"-"+y:y)}return t.absoluteValue=t.abs=function(){var m=new x(this);return m.s<0&&(m.s=1),m},t.comparedTo=function(m,y){return gt(this,new x(m,y))},t.decimalPlaces=t.dp=function(m,y){var A,P,L,h=this;if(m!=null)return oe(m,0,qe),y==null?y=c:oe(y,0,8),$(new x(h),m+h.e+1,y);if(!(A=h.c))return null;if(P=((L=A.length-1)-Ye(this.e/X))*X,L=A[L])for(;L%10==0;L/=10,P--);return P<0&&(P=0),P},t.dividedBy=t.div=function(m,y){return i(this,new x(m,y),p,c)},t.dividedToIntegerBy=t.idiv=function(m,y){return i(this,new x(m,y),0,1)},t.exponentiatedBy=t.pow=function(m,y){var A,P,L,h,e,s,l,f,d,v=this;if(m=new x(m),m.c&&!m.isInteger())throw Error(Ge+"Exponent not an integer: "+z(m));if(y!=null&&(y=new x(y)),s=m.e>14,!v.c||!v.c[0]||v.c[0]==1&&!v.e&&v.c.length==1||!m.c||!m.c[0])return d=new x(Math.pow(+z(v),s?m.s*(2-Pt(m)):+z(m))),y?d.mod(y):d;if(l=m.s<0,y){if(y.c?!y.c[0]:!y.s)return new x(NaN);P=!l&&v.isInteger()&&y.isInteger(),P&&(v=v.mod(y))}else{if(m.e>9&&(v.e>0||v.e<-1||(v.e==0?v.c[0]>1||s&&v.c[1]>=24e7:v.c[0]<8e13||s&&v.c[0]<=9999975e7)))return h=v.s<0&&Pt(m)?-0:0,v.e>-1&&(h=1/h),new x(l?1/h:h);D&&(h=dr(D/X+2))}for(s?(A=new x(.5),l&&(m.s=1),f=Pt(m)):(L=Math.abs(+z(m)),f=L%2),d=new x(u);;){if(f){if(d=d.times(v),!d.c)break;h?d.c.length>h&&(d.c.length=h):P&&(d=d.mod(y))}if(L){if(L=Je(L/2),L===0)break;f=L%2}else if(m=m.times(A),$(m,m.e+1,1),m.e>14)f=Pt(m);else{if(L=+z(m),L===0)break;f=L%2}v=v.times(v),h?v.c&&v.c.length>h&&(v.c.length=h):P&&(v=v.mod(y))}return P?d:(l&&(d=u.div(d)),y?d.mod(y):h?$(d,D,c,e):d)},t.integerValue=function(m){var y=new x(this);return m==null?m=c:oe(m,0,8),$(y,y.e+1,m)},t.isEqualTo=t.eq=function(m,y){return gt(this,new x(m,y))===0},t.isFinite=function(){return!!this.c},t.isGreaterThan=t.gt=function(m,y){return gt(this,new x(m,y))>0},t.isGreaterThanOrEqualTo=t.gte=function(m,y){return(y=gt(this,new x(m,y)))===1||y===0},t.isInteger=function(){return!!this.c&&Ye(this.e/X)>this.c.length-2},t.isLessThan=t.lt=function(m,y){return gt(this,new x(m,y))<0},t.isLessThanOrEqualTo=t.lte=function(m,y){return(y=gt(this,new x(m,y)))===-1||y===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(m,y){var A,P,L,h,e=this,s=e.s;if(m=new x(m,y),y=m.s,!s||!y)return new x(NaN);if(s!=y)return m.s=-y,e.plus(m);var l=e.e/X,f=m.e/X,d=e.c,v=m.c;if(!l||!f){if(!d||!v)return d?(m.s=-y,m):new x(v?e:NaN);if(!d[0]||!v[0])return v[0]?(m.s=-y,m):new x(d[0]?e:c==3?-0:0)}if(l=Ye(l),f=Ye(f),d=d.slice(),s=l-f){for((h=s<0)?(s=-s,L=d):(f=l,L=v),L.reverse(),y=s;y--;L.push(0));L.reverse()}else for(P=(h=(s=d.length)<(y=v.length))?s:y,s=y=0;y<P;y++)if(d[y]!=v[y]){h=d[y]<v[y];break}if(h&&(L=d,d=v,v=L,m.s=-m.s),y=(P=v.length)-(A=d.length),y>0)for(;y--;d[A++]=0);for(y=et-1;P>s;){if(d[--P]<v[P]){for(A=P;A&&!d[--A];d[A]=y);--d[A],d[P]+=et}d[P]-=v[P]}for(;d[0]==0;d.splice(0,1),--f);return d[0]?W(m,d,f):(m.s=c==3?-1:1,m.c=[m.e=0],m)},t.modulo=t.mod=function(m,y){var A,P,L=this;return m=new x(m,y),!L.c||!m.s||m.c&&!m.c[0]?new x(NaN):!m.c||L.c&&!L.c[0]?new x(L):(T==9?(P=m.s,m.s=1,A=i(L,m,0,3),m.s=P,A.s*=P):A=i(L,m,0,T),m=L.minus(A.times(m)),!m.c[0]&&T==1&&(m.s=L.s),m)},t.multipliedBy=t.times=function(m,y){var A,P,L,h,e,s,l,f,d,v,k,w,a,_,F,E=this,U=E.c,Z=(m=new x(m,y)).c;if(!U||!Z||!U[0]||!Z[0])return!E.s||!m.s||U&&!U[0]&&!Z||Z&&!Z[0]&&!U?m.c=m.e=m.s=null:(m.s*=E.s,!U||!Z?m.c=m.e=null:(m.c=[0],m.e=0)),m;for(P=Ye(E.e/X)+Ye(m.e/X),m.s*=E.s,l=U.length,v=Z.length,l<v&&(a=U,U=Z,Z=a,L=l,l=v,v=L),L=l+v,a=[];L--;a.push(0));for(_=et,F=at,L=v;--L>=0;){for(A=0,k=Z[L]%F,w=Z[L]/F|0,e=l,h=L+e;h>L;)f=U[--e]%F,d=U[e]/F|0,s=w*f+d*k,f=k*f+s%F*F+a[h]+A,A=(f/_|0)+(s/F|0)+w*d,a[h--]=f%_;a[h]=A}return A?++P:a.splice(0,1),W(m,a,P)},t.negated=function(){var m=new x(this);return m.s=-m.s||null,m},t.plus=function(m,y){var A,P=this,L=P.s;if(m=new x(m,y),y=m.s,!L||!y)return new x(NaN);if(L!=y)return m.s=-y,P.minus(m);var h=P.e/X,e=m.e/X,s=P.c,l=m.c;if(!h||!e){if(!s||!l)return new x(L/0);if(!s[0]||!l[0])return l[0]?m:new x(s[0]?P:L*0)}if(h=Ye(h),e=Ye(e),s=s.slice(),L=h-e){for(L>0?(e=h,A=l):(L=-L,A=s),A.reverse();L--;A.push(0));A.reverse()}for(L=s.length,y=l.length,L-y<0&&(A=l,l=s,s=A,y=L),L=0;y;)L=(s[--y]=s[y]+l[y]+L)/et|0,s[y]=et===s[y]?0:s[y]%et;return L&&(s=[L].concat(s),++e),W(m,s,e)},t.precision=t.sd=function(m,y){var A,P,L,h=this;if(m!=null&&m!==!!m)return oe(m,1,qe),y==null?y=c:oe(y,0,8),$(new x(h),m,y);if(!(A=h.c))return null;if(L=A.length-1,P=L*X+1,L=A[L]){for(;L%10==0;L/=10,P--);for(L=A[0];L>=10;L/=10,P++);}return m&&h.e+1>P&&(P=h.e+1),P},t.shiftedBy=function(m){return oe(m,-hr,hr),this.times("1e"+m)},t.squareRoot=t.sqrt=function(){var m,y,A,P,L,h=this,e=h.c,s=h.s,l=h.e,f=p+4,d=new x("0.5");if(s!==1||!e||!e[0])return new x(!s||s<0&&(!e||e[0])?NaN:e?h:1/0);if(s=Math.sqrt(+z(h)),s==0||s==1/0?(y=Xe(e),(y.length+l)%2==0&&(y+="0"),s=Math.sqrt(+y),l=Ye((l+1)/2)-(l<0||l%2),s==1/0?y="5e"+l:(y=s.toExponential(),y=y.slice(0,y.indexOf("e")+1)+l),A=new x(y)):A=new x(s+""),A.c[0]){for(l=A.e,s=l+f,s<3&&(s=0);;)if(L=A,A=d.times(L.plus(i(h,L,f,1))),Xe(L.c).slice(0,s)===(y=Xe(A.c)).slice(0,s))if(A.e<l&&--s,y=y.slice(s-3,s+1),y=="9999"||!P&&y=="4999"){if(!P&&($(L,L.e+p+2,0),L.times(L).eq(h))){A=L;break}f+=4,s+=4,P=1}else{(!+y||!+y.slice(1)&&y.charAt(0)=="5")&&($(A,A.e+p+2,1),m=!A.times(A).eq(h));break}}return $(A,A.e+p+1,c,m)},t.toExponential=function(m,y){return m!=null&&(oe(m,0,qe),m++),q(this,m,y,1)},t.toFixed=function(m,y){return m!=null&&(oe(m,0,qe),m=m+this.e+1),q(this,m,y)},t.toFormat=function(m,y,A){var P,L=this;if(A==null)m!=null&&y&&typeof y=="object"?(A=y,y=null):m&&typeof m=="object"?(A=m,m=y=null):A=O;else if(typeof A!="object")throw Error(Ge+"Argument not an object: "+A);if(P=L.toFixed(m,y),L.c){var h,e=P.split("."),s=+A.groupSize,l=+A.secondaryGroupSize,f=A.groupSeparator||"",d=e[0],v=e[1],k=L.s<0,w=k?d.slice(1):d,a=w.length;if(l&&(h=s,s=l,l=h,a-=h),s>0&&a>0){for(h=a%s||s,d=w.substr(0,h);h<a;h+=s)d+=f+w.substr(h,s);l>0&&(d+=f+w.slice(h)),k&&(d="-"+d)}P=v?d+(A.decimalSeparator||"")+((l=+A.fractionGroupSize)?v.replace(new RegExp("\\d{"+l+"}\\B","g"),"$&"+(A.fractionGroupSeparator||"")):v):d}return(A.prefix||"")+P+(A.suffix||"")},t.toFraction=function(m){var y,A,P,L,h,e,s,l,f,d,v,k,w=this,a=w.c;if(m!=null&&(s=new x(m),!s.isInteger()&&(s.c||s.s!==1)||s.lt(u)))throw Error(Ge+"Argument "+(s.isInteger()?"out of range: ":"not an integer: ")+z(s));if(!a)return new x(w);for(y=new x(u),f=A=new x(u),P=l=new x(u),k=Xe(a),h=y.e=k.length-w.e-1,y.c[0]=pr[(e=h%X)<0?X+e:e],m=!m||s.comparedTo(y)>0?h>0?y:f:s,e=S,S=1/0,s=new x(k),l.c[0]=0;d=i(s,y,0,1),L=A.plus(d.times(P)),L.comparedTo(m)!=1;)A=P,P=L,f=l.plus(d.times(L=f)),l=L,y=s.minus(d.times(L=y)),s=L;return L=i(m.minus(A),P,0,1),l=l.plus(L.times(f)),A=A.plus(L.times(P)),l.s=f.s=w.s,h=h*2,v=i(f,P,h,c).minus(w).abs().comparedTo(i(l,A,h,c).minus(w).abs())<1?[f,P]:[l,A],S=e,v},t.toNumber=function(){return+z(this)},t.toPrecision=function(m,y){return m!=null&&oe(m,1,qe),q(this,m,y,2)},t.toString=function(m){var y,A=this,P=A.s,L=A.e;return L===null?P?(y="Infinity",P<0&&(y="-"+y)):y="NaN":(m==null?y=L<=g||L>=M?Lt(Xe(A.c),L):ot(Xe(A.c),L,"0"):m===10&&R?(A=$(new x(A),p+L+1,c),y=ot(Xe(A.c),A.e,"0")):(oe(m,2,B.length,"Base"),y=r(ot(Xe(A.c),L,"0"),10,m,P,!0)),P<0&&A.c[0]&&(y="-"+y)),y},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&&x.set(n),x}function Ye(n){var i=n|0;return n>0||n===i?i:i-1}function Xe(n){for(var i,r,o=1,t=n.length,u=n[0]+"";o<t;){for(i=n[o++]+"",r=X-i.length;r--;i="0"+i);u+=i}for(t=u.length;u.charCodeAt(--t)===48;);return u.slice(0,t+1||1)}function gt(n,i){var r,o,t=n.c,u=i.c,p=n.s,c=i.s,g=n.e,M=i.e;if(!p||!c)return null;if(r=t&&!t[0],o=u&&!u[0],r||o)return r?o?0:-c:p;if(p!=c)return p;if(r=p<0,o=g==M,!t||!u)return o?0:!t^r?1:-1;if(!o)return g>M^r?1:-1;for(c=(g=t.length)<(M=u.length)?g:M,p=0;p<c;p++)if(t[p]!=u[p])return t[p]>u[p]^r?1:-1;return g==M?0:g>M^r?1:-1}function oe(n,i,r,o){if(n<i||n>r||n!==Je(n))throw Error(Ge+(o||"Argument")+(typeof n=="number"?n<i||n>r?" out of range: ":" not an integer: ":" not a primitive number: ")+String(n))}function Pt(n){var i=n.c.length-1;return Ye(n.e/X)==i&&n.c[i]%2!=0}function Lt(n,i){return(n.length>1?n.charAt(0)+"."+n.slice(1):n)+(i<0?"e":"e+")+i}function ot(n,i,r){var o,t;if(i<0){for(t=r+".";++i;t+=r);n=t+n}else if(o=n.length,++i>o){for(t=r,i-=o;--i;t+=r);n+=t}else i<o&&(n=n.slice(0,i)+"."+n.slice(i));return n}var ji=Xr(),We=ji;import{linearlyInterpolate as zi,Side as vt}from"@suilend/sdk";import{NORMALIZED_flSUI_COINTYPE as Ki,NORMALIZED_jugSUI_COINTYPE as Gi,NORMALIZED_LBTC_COINTYPE as Hi,formatLtvPercent as Vi}from"@suilend/sui-fe";import $i from"crypto";var xt=new Uint8Array(256),Tt=xt.length;function gr(){return Tt>xt.length-16&&($i.randomFillSync(xt),Tt=0),xt.slice(Tt,Tt+=16)}var je=[];for(let n=0;n<256;++n)je.push((n+256).toString(16).slice(1));function Jr(n,i=0){return je[n[i+0]]+je[n[i+1]]+je[n[i+2]]+je[n[i+3]]+"-"+je[n[i+4]]+je[n[i+5]]+"-"+je[n[i+6]]+je[n[i+7]]+"-"+je[n[i+8]]+je[n[i+9]]+"-"+je[n[i+10]]+je[n[i+11]]+je[n[i+12]]+je[n[i+13]]+je[n[i+14]]+je[n[i+15]]}import Zi from"crypto";var mr={randomUUID:Zi.randomUUID};function Wi(n,i,r){if(mr.randomUUID&&!i&&!n)return mr.randomUUID();n=n||{};let o=n.random||(n.rng||gr)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,i){r=r||0;for(let t=0;t<16;++t)i[r+t]=o[t];return i}return Jr(o)}var mt=Wi;import{getPackagerConfigs as Qi}from"@cetusprotocol/common-sdk";var vr=10n;function Yr(n){return n.depositedAmount.eq(0)?new We(0):n.borrowedAmount.div(n.depositedAmount).times(100)}function wr(n){let i=Yr(n);if(!i.gt(100))return zi(n.config.interestRate,"utilPercent","aprPercent",i)}function ei(n){let i=Yr(n),r=wr(n);if(!(r===void 0||i.gt(100)))return new We(i.div(100)).times(r.div(100)).times(1-n.config.spreadFeeBps/1e4).times(100)}function qo(n,i,r,o){let{apiUrl:t}=Qi(o.sdkOptions.suilend);n===vt.DEPOSIT&&([Ki,Gi].includes(i.coinType)&&r.push({stats:{id:mt(),isActive:!0,rewardIndex:-1,reserve:i,rewardCoinType:"LIQ_AG",mintDecimals:0,symbol:"LiqAg Points",iconUrl:`${t}/partners/LiqAg.png`,perDay:new We(.036),side:vt.DEPOSIT},obligationClaims:{}}),i.coinType===Hi&&r.push({stats:{id:mt(),isActive:!0,rewardIndex:-1,reserve:i,rewardCoinType:"LOMBARD",mintDecimals:0,symbol:"3x Lombard Lux",iconUrl:`${t}/partners/Lombard Lux.png`,perDay:new We(0),side:vt.DEPOSIT},obligationClaims:{}}))}function jo(n,i,r){return{...n,depositedAmount:i===vt.DEPOSIT?We.max(n.depositedAmount.plus(r),0):n.depositedAmount,borrowedAmount:i===vt.BORROW?We.max(n.borrowedAmount.plus(r),0):n.borrowedAmount}}function $o(n,i,r){let o=n===vt.DEPOSIT?i.depositedAmount:i.borrowedAmount,t=n===vt.DEPOSIT?r.depositedAmount:r.borrowedAmount,u=t.eq(0)?new We(-1):o.div(t);return{multiplier:u,isValid:!u.eq(-1)}}function Zo(n,i,r,o){let t=n.map(p=>({...p,stats:{...p.stats,perDay:o?p.stats.perDay.times(r):void 0}})),u=i.map(p=>({...p,stats:{...p.stats,aprPercent:o?p.stats.aprPercent.times(r):void 0}}));return{newPerDayRewards:t,newAprRewards:u}}function ti(n,i){if(!vr)throw new Error("oraclePriceMultiplierDecimal is required");if(n===0n)throw new Error("Invalid oracle price");if(i<0n){let r=10n**(vr- -i);return n*r}return n/10n**(i+vr)}function Wo(n){return Vi(new We(n))}import{CACHE_TIME_5MIN as ln,d as Mr,getPackagerConfigs as wt,removeHexPrefix as br}from"@cetusprotocol/common-sdk";import{toHEX as ii}from"@mysten/sui/utils";var Dt=class{constructor(i){this.getSuiLendCoinInfo=async(i,r,o)=>{let t=new We(r.toString()),{allLendingMarketData:u,lstAprPercentMap:p}=await this.getLendingMarketData(!1);if(await this.getInitializeObligations({allLendingMarketData:u,lstAprPercentMap:p},!0),!u||!p)throw new Error("lendingMarketCache is not initialized");let c=Object.values(u??{})[0],g=c.lendingMarket.reserves.find(m=>m.coinType===i);if(!g)throw new Error("reserve not found");let b=(this.obligationsCache?.[c.lendingMarket.id]).rewardMap[g.token.coinType]?.[o]??[],S=rn(b),C=nn(o,g.coinType,p),T=o===lt.DEPOSIT?g.depositAprPercent:g.borrowAprPercent,D=T,O=new We(1),B=!0;if(o!==void 0&&t!==void 0&&t.gt(0)){let m={...g,depositedAmount:o===lt.DEPOSIT?We.max(g.depositedAmount.plus(o===lt.DEPOSIT?t:t.negated()),0):g.depositedAmount,borrowedAmount:o===lt.BORROW?We.max(g.borrowedAmount.plus(o===lt.BORROW?t:t.negated()),0):g.borrowedAmount};D=o===lt.DEPOSIT?ei(m):wr(m);let y=o===lt.DEPOSIT?g.depositedAmount:g.borrowedAmount,A=o===lt.DEPOSIT?m.depositedAmount:m.borrowedAmount;O=A.eq(0)?new We(-1):y.div(A),B=!O.eq(-1)}let q=on(S).map(m=>({...m,stats:{...m.stats,perDay:B?m.stats.perDay.times(O):void 0}})),W=sn(S).map(m=>({...m,stats:{...m.stats,aprPercent:B?m.stats.aprPercent.times(O):void 0}})),$=ri(o,T,S,C),z=D===void 0||W.some(m=>m.stats.aprPercent===void 0)?void 0:ri(o,D,W,C);return console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:315 ~ SuiLendModule ~ newTotalAprPercent:",z?.toString()),{newTotalAprPercent:z?.toString(),totalAprPercent:$.toString()}};this.refreshReservePrice=async(i,r,o)=>{let{lendingMarketId:t,lendingMarketType:u}=wt(this.sdk.sdkOptions?.suilend),p=`${t}_${u}`;if(!this.suilendClientCache)throw new Error("suilendClientCache is not initialized");let c=await this.suilendClientCache[p].pythClient.getPriceFeedObjectId(r);this.suilendClientCache[p].refreshReservePrices(i,c,o)};this.getLatestPriceFeeds=async(i,r=!1)=>{let{lendingMarketId:o,lendingMarketType:t}=wt(this.sdk.sdkOptions?.suilend),u={},p=[];if(i.forEach(b=>{let S=this.sdk.getCache(`getLatestPrice_${b.coinType.name}`,r);S&&this.priceCheck(S,60)?u[b.coinType.name]=S:p.push(b.coinType.name)}),p.length==0)return u;let c=`${o}_${t}`;if(!this.suilendClientCache)throw new Error("suilendClient is not initialized");let g=Array.from(new Set(i.map(b=>b?.priceIdentifier?.bytes?ii(new Uint8Array(b.priceIdentifier.bytes)):br(b.priceIdentifier.toString()))));return(await this.suilendClientCache[c].pythConnection.getLatestPriceFeeds(g))?.forEach((b,S)=>{let C=b.getPriceUnchecked(),T=i[S];if(C){let{price:D,expo:O}=C,B=Mr(D).mul(Mr(10).pow(Mr(O))).toString(),R={coin_type:T.coinType.name||T.coinType,price:B,oracle_price:0n,last_update_time:C.publishTime};R.oracle_price=ti(BigInt(D),BigInt(O)),u[T.coinType.name||T.coinType]=R,this.sdk.updateCache(`getLatestPrice_${R.coin_type}`,R)}}),u};this.getLatestPriceFeedsByCoinTypes=async(i,r=!1)=>{let{lendingMarketId:o}=wt(this.sdk.sdkOptions?.suilend),{allLendingMarketData:t}=await this.getLendingMarketData(!1),u=[];return i.forEach(c=>{let g=t[o].reserveMap[c];u.push(g)}),await this.getLatestPriceFeeds(u,r)};this.getSuiLendReserveInfo=async(i,r,o)=>{let{lendingMarketId:t}=wt(this.sdk.sdkOptions?.suilend);o||(o=(await this.getLendingMarketData(!1)).allLendingMarketData);let u=o[t].reserveMap[i],p=o[t].reserveMap[r],c=o[t].refreshedRawReserves.filter(S=>S.coinType.name==br(i)),g=o[t].refreshedRawReserves.filter(S=>S.coinType.name==br(r)),M=o[t].reserveMap[i],b=o[t].reserveMap[r];return{baseReserveArrayIndex:u.arrayIndex.toString(),quoteReserveArrayIndex:p.arrayIndex.toString(),reserve:[c[0],g[0]],baseReserveMapInfo:M,quoteReserveMapInfo:b}};this.refreshReservePrices=async(i,r)=>{for(let o=0;o<r.length;o++){let t=ii(new Uint8Array(r[o].priceIdentifier.bytes));await this.refreshReservePrice(i,t,BigInt(r[o].arrayIndex))}};this.sdk=i}async getSuilendClient(i,r){let o=`${i}_${r}`;return this.suilendClientCache||(this.suilendClientCache={}),this.suilendClientCache[o]||(this.suilendClientCache[o]=await Xi.initialize(i,r,this.sdk.FullClient,!0)),this.suilendClientCache[o]}async getLendingMarketData(i=!0){let r="lendingMarketData",o=this.sdk.getCache(r,i);if(o)return o;let t=wt(this.sdk.sdkOptions?.suilend)?.lendingMarket||[];if(!t.length)return console.warn("\u26A0\uFE0F No lending markets configured."),{};try{let[u,p]=await Promise.all([(async()=>Object.fromEntries(await Promise.all(t.map(async g=>{let M=await this.getSuilendClient(g.id,g.type),{lendingMarket:b,coinMetadataMap:S,refreshedRawReserves:C,reserveMap:T,reserveCoinTypes:D,reserveCoinMetadataMap:O,rewardCoinTypes:B,activeRewardCoinTypes:R,rewardCoinMetadataMap:x}=await Ji(this.sdk.FullClient,M),{rewardPriceMap:q}=await Yi(T,R);return[g.id,{suilendClient:M,lendingMarket:b,coinMetadataMap:S,refreshedRawReserves:C,reserveMap:T,reserveCoinTypes:D,reserveCoinMetadataMap:O,rewardPriceMap:q,rewardCoinTypes:B,activeRewardCoinTypes:R,rewardCoinMetadataMap:x}]}))))(),(async()=>{try{let c=await fetch(`${this.sdk.sdkOptions?.suilend?.config?.apiUrl}/springsui/apy`),g=await c.json();if(c?.statusCode===500)throw new Error("Failed to fetch SpringSui LST APRs");return Object.fromEntries(Object.entries(g).map(([M,b])=>[M,new We(b)]))}catch(c){return console.error(c),{}}})()]);return this.sdk.updateCache(r,{allLendingMarketData:u,lstAprPercentMap:p},ln),{allLendingMarketData:u,lstAprPercentMap:p}}catch(u){throw console.log("\u{1F680} getLendingMarketData error:",u),u}}async getInitializeObligations(i,r=!1){if(!r&&this.obligationsCache)return this.obligationsCache;i||(i=await this.getLendingMarketData(!1));let o={};for(let t of Object.values(i.allLendingMarketData)){let{obligationOwnerCaps:u,obligations:p}=await en(this.sdk.FullClient,t.suilendClient,t.refreshedRawReserves,t.reserveMap,this.sdk.senderAddress),c=tn(t.reserveMap,t.rewardCoinMetadataMap,t.rewardPriceMap,p);o[t.lendingMarket.id]={obligationOwnerCaps:u,obligations:p,rewardMap:c}}return this.obligationsCache=o,this.obligationsCache}async getSuiLendUserData(i,r=!1){let{allLendingMarketData:o,lstAprPercentMap:t}=await this.getLendingMarketData(!1),u=await this.getInitializeObligations({allLendingMarketData:o,lstAprPercentMap:t},!1),{lendingMarketId:p}=wt(this.sdk.sdkOptions?.suilend),c=u[p];if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ suilendModule.ts:275 ~ SuiLendModule ~ getSuiLendUserData ~ userData:",c),!c)throw new Error(`No user data found for lending market: ${p}`);if(i){let g=c.obligations.find(b=>b.id===i);if(!g)throw new Error(`Obligation not found: ${i}`);let M=an(g,c.rewardMap,t??{});return{obligation:g,netAprPercent:M,deposits:g.deposits,borrowedAmount:g.borrowedAmount,depositedAmount:g.depositedAmount,netValueUsd:g.netValueUsd,rewardMap:c.rewardMap}}}priceCheck(i,r=60){let o=Math.floor(Date.now()/1e3);if(!(Math.abs(o-i.last_update_time)>r))return i}};import{getPackagerConfigs as ne,CACHE_TIME_5MIN as _i,CoinAssist as Y,CLOCK_ADDRESS as pt,d as I,removeHexPrefix as yt,getObjectFields as ki,addHexPrefix as ao,U64_MAX as Ai}from"@cetusprotocol/common-sdk";var Mt=9e15,dt=1e9,yr="0123456789abcdef",Rt="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",Bt="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",_r={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-Mt,maxE:Mt,crypto:!1},ai,st,V=!0,Et="[DecimalError] ",ct=Et+"Invalid argument: ",li=Et+"Precision limit exceeded",ui=Et+"crypto unavailable",fi="[object Decimal]",ze=Math.floor,Fe=Math.pow,un=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,fn=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,cn=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,ci=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,tt=1e7,H=7,dn=9007199254740991,hn=Rt.length-1,kr=Bt.length-1,N={toStringTag:fi};N.absoluteValue=N.abs=function(){var n=new this.constructor(this);return n.s<0&&(n.s=1),K(n)};N.ceil=function(){return K(new this.constructor(this),this.e+1,2)};N.clampedTo=N.clamp=function(n,i){var r,o=this,t=o.constructor;if(n=new t(n),i=new t(i),!n.s||!i.s)return new t(NaN);if(n.gt(i))throw Error(ct+i);return r=o.cmp(n),r<0?n:o.cmp(i)>0?i:new t(o)};N.comparedTo=N.cmp=function(n){var i,r,o,t,u=this,p=u.d,c=(n=new u.constructor(n)).d,g=u.s,M=n.s;if(!p||!c)return!g||!M?NaN:g!==M?g:p===c?0:!p^g<0?1:-1;if(!p[0]||!c[0])return p[0]?g:c[0]?-M:0;if(g!==M)return g;if(u.e!==n.e)return u.e>n.e^g<0?1:-1;for(o=p.length,t=c.length,i=0,r=o<t?o:t;i<r;++i)if(p[i]!==c[i])return p[i]>c[i]^g<0?1:-1;return o===t?0:o>t^g<0?1:-1};N.cosine=N.cos=function(){var n,i,r=this,o=r.constructor;return r.d?r.d[0]?(n=o.precision,i=o.rounding,o.precision=n+Math.max(r.e,r.sd())+H,o.rounding=1,r=pn(o,mi(o,r)),o.precision=n,o.rounding=i,K(st==2||st==3?r.neg():r,n,i,!0)):new o(1):new o(NaN)};N.cubeRoot=N.cbrt=function(){var n,i,r,o,t,u,p,c,g,M,b=this,S=b.constructor;if(!b.isFinite()||b.isZero())return new S(b);for(V=!1,u=b.s*Fe(b.s*b,1/3),!u||Math.abs(u)==1/0?(r=$e(b.d),n=b.e,(u=(n-r.length+1)%3)&&(r+=u==1||u==-2?"0":"00"),u=Fe(r,1/3),n=ze((n+1)/3)-(n%3==(n<0?-1:2)),u==1/0?r="5e"+n:(r=u.toExponential(),r=r.slice(0,r.indexOf("e")+1)+n),o=new S(r),o.s=b.s):o=new S(u.toString()),p=(n=S.precision)+3;;)if(c=o,g=c.times(c).times(c),M=g.plus(b),o=ie(M.plus(b).times(c),M.plus(g),p+2,1),$e(c.d).slice(0,p)===(r=$e(o.d)).slice(0,p))if(r=r.slice(p-3,p+1),r=="9999"||!t&&r=="4999"){if(!t&&(K(c,n+1,0),c.times(c).times(c).eq(b))){o=c;break}p+=4,t=1}else{(!+r||!+r.slice(1)&&r.charAt(0)=="5")&&(K(o,n+1,1),i=!o.times(o).times(o).eq(b));break}return V=!0,K(o,n,S.rounding,i)};N.decimalPlaces=N.dp=function(){var n,i=this.d,r=NaN;if(i){if(n=i.length-1,r=(n-ze(this.e/H))*H,n=i[n],n)for(;n%10==0;n/=10)r--;r<0&&(r=0)}return r};N.dividedBy=N.div=function(n){return ie(this,new this.constructor(n))};N.dividedToIntegerBy=N.divToInt=function(n){var i=this,r=i.constructor;return K(ie(i,new r(n),0,1,1),r.precision,r.rounding)};N.equals=N.eq=function(n){return this.cmp(n)===0};N.floor=function(){return K(new this.constructor(this),this.e+1,3)};N.greaterThan=N.gt=function(n){return this.cmp(n)>0};N.greaterThanOrEqualTo=N.gte=function(n){var i=this.cmp(n);return i==1||i===0};N.hyperbolicCosine=N.cosh=function(){var n,i,r,o,t,u=this,p=u.constructor,c=new p(1);if(!u.isFinite())return new p(u.s?1/0:NaN);if(u.isZero())return c;r=p.precision,o=p.rounding,p.precision=r+Math.max(u.e,u.sd())+4,p.rounding=1,t=u.d.length,t<32?(n=Math.ceil(t/3),i=(1/Ut(4,n)).toString()):(n=16,i="2.3283064365386962890625e-10"),u=bt(p,1,u.times(i),new p(1),!0);for(var g,M=n,b=new p(8);M--;)g=u.times(u),u=c.minus(g.times(b.minus(g.times(b))));return K(u,p.precision=r,p.rounding=o,!0)};N.hyperbolicSine=N.sinh=function(){var n,i,r,o,t=this,u=t.constructor;if(!t.isFinite()||t.isZero())return new u(t);if(i=u.precision,r=u.rounding,u.precision=i+Math.max(t.e,t.sd())+4,u.rounding=1,o=t.d.length,o<3)t=bt(u,2,t,t,!0);else{n=1.4*Math.sqrt(o),n=n>16?16:n|0,t=t.times(1/Ut(5,n)),t=bt(u,2,t,t,!0);for(var p,c=new u(5),g=new u(16),M=new u(20);n--;)p=t.times(t),t=t.times(c.plus(p.times(g.times(p).plus(M))))}return u.precision=i,u.rounding=r,K(t,i,r,!0)};N.hyperbolicTangent=N.tanh=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+7,o.rounding=1,ie(r.sinh(),r.cosh(),o.precision=n,o.rounding=i)):new o(r.s)};N.inverseCosine=N.acos=function(){var n=this,i=n.constructor,r=n.abs().cmp(1),o=i.precision,t=i.rounding;return r!==-1?r===0?n.isNeg()?it(i,o,t):new i(0):new i(NaN):n.isZero()?it(i,o+4,t).times(.5):(i.precision=o+6,i.rounding=1,n=new i(1).minus(n).div(n.plus(1)).sqrt().atan(),i.precision=o,i.rounding=t,n.times(2))};N.inverseHyperbolicCosine=N.acosh=function(){var n,i,r=this,o=r.constructor;return r.lte(1)?new o(r.eq(1)?0:NaN):r.isFinite()?(n=o.precision,i=o.rounding,o.precision=n+Math.max(Math.abs(r.e),r.sd())+4,o.rounding=1,V=!1,r=r.times(r).minus(1).sqrt().plus(r),V=!0,o.precision=n,o.rounding=i,r.ln()):new o(r)};N.inverseHyperbolicSine=N.asinh=function(){var n,i,r=this,o=r.constructor;return!r.isFinite()||r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+2*Math.max(Math.abs(r.e),r.sd())+6,o.rounding=1,V=!1,r=r.times(r).plus(1).sqrt().plus(r),V=!0,o.precision=n,o.rounding=i,r.ln())};N.inverseHyperbolicTangent=N.atanh=function(){var n,i,r,o,t=this,u=t.constructor;return t.isFinite()?t.e>=0?new u(t.abs().eq(1)?t.s/0:t.isZero()?t:NaN):(n=u.precision,i=u.rounding,o=t.sd(),Math.max(o,n)<2*-t.e-1?K(new u(t),n,i,!0):(u.precision=r=o-t.e,t=ie(t.plus(1),new u(1).minus(t),r+n,1),u.precision=n+4,u.rounding=1,t=t.ln(),u.precision=n,u.rounding=i,t.times(.5))):new u(NaN)};N.inverseSine=N.asin=function(){var n,i,r,o,t=this,u=t.constructor;return t.isZero()?new u(t):(i=t.abs().cmp(1),r=u.precision,o=u.rounding,i!==-1?i===0?(n=it(u,r+4,o).times(.5),n.s=t.s,n):new u(NaN):(u.precision=r+6,u.rounding=1,t=t.div(new u(1).minus(t.times(t)).sqrt().plus(1)).atan(),u.precision=r,u.rounding=o,t.times(2)))};N.inverseTangent=N.atan=function(){var n,i,r,o,t,u,p,c,g,M=this,b=M.constructor,S=b.precision,C=b.rounding;if(M.isFinite()){if(M.isZero())return new b(M);if(M.abs().eq(1)&&S+4<=kr)return p=it(b,S+4,C).times(.25),p.s=M.s,p}else{if(!M.s)return new b(NaN);if(S+4<=kr)return p=it(b,S+4,C).times(.5),p.s=M.s,p}for(b.precision=c=S+10,b.rounding=1,r=Math.min(28,c/H+2|0),n=r;n;--n)M=M.div(M.times(M).plus(1).sqrt().plus(1));for(V=!1,i=Math.ceil(c/H),o=1,g=M.times(M),p=new b(M),t=M;n!==-1;)if(t=t.times(g),u=p.minus(t.div(o+=2)),t=t.times(g),p=u.plus(t.div(o+=2)),p.d[i]!==void 0)for(n=i;p.d[n]===u.d[n]&&n--;);return r&&(p=p.times(2<<r-1)),V=!0,K(p,b.precision=S,b.rounding=C,!0)};N.isFinite=function(){return!!this.d};N.isInteger=N.isInt=function(){return!!this.d&&ze(this.e/H)>this.d.length-2};N.isNaN=function(){return!this.s};N.isNegative=N.isNeg=function(){return this.s<0};N.isPositive=N.isPos=function(){return this.s>0};N.isZero=function(){return!!this.d&&this.d[0]===0};N.lessThan=N.lt=function(n){return this.cmp(n)<0};N.lessThanOrEqualTo=N.lte=function(n){return this.cmp(n)<1};N.logarithm=N.log=function(n){var i,r,o,t,u,p,c,g,M=this,b=M.constructor,S=b.precision,C=b.rounding,T=5;if(n==null)n=new b(10),i=!0;else{if(n=new b(n),r=n.d,n.s<0||!r||!r[0]||n.eq(1))return new b(NaN);i=n.eq(10)}if(r=M.d,M.s<0||!r||!r[0]||M.eq(1))return new b(r&&!r[0]?-1/0:M.s!=1?NaN:r?0:1/0);if(i)if(r.length>1)u=!0;else{for(t=r[0];t%10===0;)t/=10;u=t!==1}if(V=!1,c=S+T,p=ft(M,c),o=i?Nt(b,c+10):ft(n,c),g=ie(p,o,c,1),At(g.d,t=S,C))do if(c+=10,p=ft(M,c),o=i?Nt(b,c+10):ft(n,c),g=ie(p,o,c,1),!u){+$e(g.d).slice(t+1,t+15)+1==1e14&&(g=K(g,S+1,0));break}while(At(g.d,t+=10,C));return V=!0,K(g,S,C)};N.minus=N.sub=function(n){var i,r,o,t,u,p,c,g,M,b,S,C,T=this,D=T.constructor;if(n=new D(n),!T.d||!n.d)return!T.s||!n.s?n=new D(NaN):T.d?n.s=-n.s:n=new D(n.d||T.s!==n.s?T:NaN),n;if(T.s!=n.s)return n.s=-n.s,T.plus(n);if(M=T.d,C=n.d,c=D.precision,g=D.rounding,!M[0]||!C[0]){if(C[0])n.s=-n.s;else if(M[0])n=new D(T);else return new D(g===3?-0:0);return V?K(n,c,g):n}if(r=ze(n.e/H),b=ze(T.e/H),M=M.slice(),u=b-r,u){for(S=u<0,S?(i=M,u=-u,p=C.length):(i=C,r=b,p=M.length),o=Math.max(Math.ceil(c/H),p)+2,u>o&&(u=o,i.length=1),i.reverse(),o=u;o--;)i.push(0);i.reverse()}else{for(o=M.length,p=C.length,S=o<p,S&&(p=o),o=0;o<p;o++)if(M[o]!=C[o]){S=M[o]<C[o];break}u=0}for(S&&(i=M,M=C,C=i,n.s=-n.s),p=M.length,o=C.length-p;o>0;--o)M[p++]=0;for(o=C.length;o>u;){if(M[--o]<C[o]){for(t=o;t&&M[--t]===0;)M[t]=tt-1;--M[t],M[o]+=tt}M[o]-=C[o]}for(;M[--p]===0;)M.pop();for(;M[0]===0;M.shift())--r;return M[0]?(n.d=M,n.e=Ft(M,r),V?K(n,c,g):n):new D(g===3?-0:0)};N.modulo=N.mod=function(n){var i,r=this,o=r.constructor;return n=new o(n),!r.d||!n.s||n.d&&!n.d[0]?new o(NaN):!n.d||r.d&&!r.d[0]?K(new o(r),o.precision,o.rounding):(V=!1,o.modulo==9?(i=ie(r,n.abs(),0,3,1),i.s*=n.s):i=ie(r,n,0,o.modulo,1),i=i.times(n),V=!0,r.minus(i))};N.naturalExponential=N.exp=function(){return Ar(this)};N.naturalLogarithm=N.ln=function(){return ft(this)};N.negated=N.neg=function(){var n=new this.constructor(this);return n.s=-n.s,K(n)};N.plus=N.add=function(n){var i,r,o,t,u,p,c,g,M,b,S=this,C=S.constructor;if(n=new C(n),!S.d||!n.d)return!S.s||!n.s?n=new C(NaN):S.d||(n=new C(n.d||S.s===n.s?S:NaN)),n;if(S.s!=n.s)return n.s=-n.s,S.minus(n);if(M=S.d,b=n.d,c=C.precision,g=C.rounding,!M[0]||!b[0])return b[0]||(n=new C(S)),V?K(n,c,g):n;if(u=ze(S.e/H),o=ze(n.e/H),M=M.slice(),t=u-o,t){for(t<0?(r=M,t=-t,p=b.length):(r=b,o=u,p=M.length),u=Math.ceil(c/H),p=u>p?u+1:p+1,t>p&&(t=p,r.length=1),r.reverse();t--;)r.push(0);r.reverse()}for(p=M.length,t=b.length,p-t<0&&(t=p,r=b,b=M,M=r),i=0;t;)i=(M[--t]=M[t]+b[t]+i)/tt|0,M[t]%=tt;for(i&&(M.unshift(i),++o),p=M.length;M[--p]==0;)M.pop();return n.d=M,n.e=Ft(M,o),V?K(n,c,g):n};N.precision=N.sd=function(n){var i,r=this;if(n!==void 0&&n!==!!n&&n!==1&&n!==0)throw Error(ct+n);return r.d?(i=di(r.d),n&&r.e+1>i&&(i=r.e+1)):i=NaN,i};N.round=function(){var n=this,i=n.constructor;return K(new i(n),n.e+1,i.rounding)};N.sine=N.sin=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+Math.max(r.e,r.sd())+H,o.rounding=1,r=mn(o,mi(o,r)),o.precision=n,o.rounding=i,K(st>2?r.neg():r,n,i,!0)):new o(NaN)};N.squareRoot=N.sqrt=function(){var n,i,r,o,t,u,p=this,c=p.d,g=p.e,M=p.s,b=p.constructor;if(M!==1||!c||!c[0])return new b(!M||M<0&&(!c||c[0])?NaN:c?p:1/0);for(V=!1,M=Math.sqrt(+p),M==0||M==1/0?(i=$e(c),(i.length+g)%2==0&&(i+="0"),M=Math.sqrt(i),g=ze((g+1)/2)-(g<0||g%2),M==1/0?i="5e"+g:(i=M.toExponential(),i=i.slice(0,i.indexOf("e")+1)+g),o=new b(i)):o=new b(M.toString()),r=(g=b.precision)+3;;)if(u=o,o=u.plus(ie(p,u,r+2,1)).times(.5),$e(u.d).slice(0,r)===(i=$e(o.d)).slice(0,r))if(i=i.slice(r-3,r+1),i=="9999"||!t&&i=="4999"){if(!t&&(K(u,g+1,0),u.times(u).eq(p))){o=u;break}r+=4,t=1}else{(!+i||!+i.slice(1)&&i.charAt(0)=="5")&&(K(o,g+1,1),n=!o.times(o).eq(p));break}return V=!0,K(o,g,b.rounding,n)};N.tangent=N.tan=function(){var n,i,r=this,o=r.constructor;return r.isFinite()?r.isZero()?new o(r):(n=o.precision,i=o.rounding,o.precision=n+10,o.rounding=1,r=r.sin(),r.s=1,r=ie(r,new o(1).minus(r.times(r)).sqrt(),n+10,0),o.precision=n,o.rounding=i,K(st==2||st==4?r.neg():r,n,i,!0)):new o(NaN)};N.times=N.mul=function(n){var i,r,o,t,u,p,c,g,M,b=this,S=b.constructor,C=b.d,T=(n=new S(n)).d;if(n.s*=b.s,!C||!C[0]||!T||!T[0])return new S(!n.s||C&&!C[0]&&!T||T&&!T[0]&&!C?NaN:!C||!T?n.s/0:n.s*0);for(r=ze(b.e/H)+ze(n.e/H),g=C.length,M=T.length,g<M&&(u=C,C=T,T=u,p=g,g=M,M=p),u=[],p=g+M,o=p;o--;)u.push(0);for(o=M;--o>=0;){for(i=0,t=g+o;t>o;)c=u[t]+T[o]*C[t-o-1]+i,u[t--]=c%tt|0,i=c/tt|0;u[t]=(u[t]+i)%tt|0}for(;!u[--p];)u.pop();return i?++r:u.shift(),n.d=u,n.e=Ft(u,r),V?K(n,S.precision,S.rounding):n};N.toBinary=function(n,i){return Sr(this,2,n,i)};N.toDecimalPlaces=N.toDP=function(n,i){var r=this,o=r.constructor;return r=new o(r),n===void 0?r:(He(n,0,dt),i===void 0?i=o.rounding:He(i,0,8),K(r,n+r.e+1,i))};N.toExponential=function(n,i){var r,o=this,t=o.constructor;return n===void 0?r=nt(o,!0):(He(n,0,dt),i===void 0?i=t.rounding:He(i,0,8),o=K(new t(o),n+1,i),r=nt(o,!0,n+1)),o.isNeg()&&!o.isZero()?"-"+r:r};N.toFixed=function(n,i){var r,o,t=this,u=t.constructor;return n===void 0?r=nt(t):(He(n,0,dt),i===void 0?i=u.rounding:He(i,0,8),o=K(new u(t),n+t.e+1,i),r=nt(o,!1,n+o.e+1)),t.isNeg()&&!t.isZero()?"-"+r:r};N.toFraction=function(n){var i,r,o,t,u,p,c,g,M,b,S,C,T=this,D=T.d,O=T.constructor;if(!D)return new O(T);if(M=r=new O(1),o=g=new O(0),i=new O(o),u=i.e=di(D)-T.e-1,p=u%H,i.d[0]=Fe(10,p<0?H+p:p),n==null)n=u>0?i:M;else{if(c=new O(n),!c.isInt()||c.lt(M))throw Error(ct+c);n=c.gt(i)?u>0?i:M:c}for(V=!1,c=new O($e(D)),b=O.precision,O.precision=u=D.length*H*2;S=ie(c,i,0,1,1),t=r.plus(S.times(o)),t.cmp(n)!=1;)r=o,o=t,t=M,M=g.plus(S.times(t)),g=t,t=i,i=c.minus(S.times(t)),c=t;return t=ie(n.minus(r),o,0,1,1),g=g.plus(t.times(M)),r=r.plus(t.times(o)),g.s=M.s=T.s,C=ie(M,o,u,1).minus(T).abs().cmp(ie(g,r,u,1).minus(T).abs())<1?[M,o]:[g,r],O.precision=b,V=!0,C};N.toHexadecimal=N.toHex=function(n,i){return Sr(this,16,n,i)};N.toNearest=function(n,i){var r=this,o=r.constructor;if(r=new o(r),n==null){if(!r.d)return r;n=new o(1),i=o.rounding}else{if(n=new o(n),i===void 0?i=o.rounding:He(i,0,8),!r.d)return n.s?r:n;if(!n.d)return n.s&&(n.s=r.s),n}return n.d[0]?(V=!1,r=ie(r,n,0,i,1).times(n),V=!0,K(r)):(n.s=r.s,r=n),r};N.toNumber=function(){return+this};N.toOctal=function(n,i){return Sr(this,8,n,i)};N.toPower=N.pow=function(n){var i,r,o,t,u,p,c=this,g=c.constructor,M=+(n=new g(n));if(!c.d||!n.d||!c.d[0]||!n.d[0])return new g(Fe(+c,M));if(c=new g(c),c.eq(1))return c;if(o=g.precision,u=g.rounding,n.eq(1))return K(c,o,u);if(i=ze(n.e/H),i>=n.d.length-1&&(r=M<0?-M:M)<=dn)return t=hi(g,c,r,o),n.s<0?new g(1).div(t):K(t,o,u);if(p=c.s,p<0){if(i<n.d.length-1)return new g(NaN);if((n.d[i]&1)==0&&(p=1),c.e==0&&c.d[0]==1&&c.d.length==1)return c.s=p,c}return r=Fe(+c,M),i=r==0||!isFinite(r)?ze(M*(Math.log("0."+$e(c.d))/Math.LN10+c.e+1)):new g(r+"").e,i>g.maxE+1||i<g.minE-1?new g(i>0?p/0:0):(V=!1,g.rounding=c.s=1,r=Math.min(12,(i+"").length),t=Ar(n.times(ft(c,o+r)),o),t.d&&(t=K(t,o+5,1),At(t.d,o,u)&&(i=o+10,t=K(Ar(n.times(ft(c,i+r)),i),i+5,1),+$e(t.d).slice(o+1,o+15)+1==1e14&&(t=K(t,o+1,0)))),t.s=p,V=!0,g.rounding=u,K(t,o,u))};N.toPrecision=function(n,i){var r,o=this,t=o.constructor;return n===void 0?r=nt(o,o.e<=t.toExpNeg||o.e>=t.toExpPos):(He(n,1,dt),i===void 0?i=t.rounding:He(i,0,8),o=K(new t(o),n,i),r=nt(o,n<=o.e||o.e<=t.toExpNeg,n)),o.isNeg()&&!o.isZero()?"-"+r:r};N.toSignificantDigits=N.toSD=function(n,i){var r=this,o=r.constructor;return n===void 0?(n=o.precision,i=o.rounding):(He(n,1,dt),i===void 0?i=o.rounding:He(i,0,8)),K(new o(r),n,i)};N.toString=function(){var n=this,i=n.constructor,r=nt(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()&&!n.isZero()?"-"+r:r};N.truncated=N.trunc=function(){return K(new this.constructor(this),this.e+1,1)};N.valueOf=N.toJSON=function(){var n=this,i=n.constructor,r=nt(n,n.e<=i.toExpNeg||n.e>=i.toExpPos);return n.isNeg()?"-"+r:r};function $e(n){var i,r,o,t=n.length-1,u="",p=n[0];if(t>0){for(u+=p,i=1;i<t;i++)o=n[i]+"",r=H-o.length,r&&(u+=ut(r)),u+=o;p=n[i],o=p+"",r=H-o.length,r&&(u+=ut(r))}else if(p===0)return"0";for(;p%10===0;)p/=10;return u+p}function He(n,i,r){if(n!==~~n||n<i||n>r)throw Error(ct+n)}function At(n,i,r,o){var t,u,p,c;for(u=n[0];u>=10;u/=10)--i;return--i<0?(i+=H,t=0):(t=Math.ceil((i+1)/H),i%=H),u=Fe(10,H-i),c=n[t]%u|0,o==null?i<3?(i==0?c=c/100|0:i==1&&(c=c/10|0),p=r<4&&c==99999||r>3&&c==49999||c==5e4||c==0):p=(r<4&&c+1==u||r>3&&c+1==u/2)&&(n[t+1]/u/100|0)==Fe(10,i-2)-1||(c==u/2||c==0)&&(n[t+1]/u/100|0)==0:i<4?(i==0?c=c/1e3|0:i==1?c=c/100|0:i==2&&(c=c/10|0),p=(o||r<4)&&c==9999||!o&&r>3&&c==4999):p=((o||r<4)&&c+1==u||!o&&r>3&&c+1==u/2)&&(n[t+1]/u/1e3|0)==Fe(10,i-3)-1,p}function Ot(n,i,r){for(var o,t=[0],u,p=0,c=n.length;p<c;){for(u=t.length;u--;)t[u]*=i;for(t[0]+=yr.indexOf(n.charAt(p++)),o=0;o<t.length;o++)t[o]>r-1&&(t[o+1]===void 0&&(t[o+1]=0),t[o+1]+=t[o]/r|0,t[o]%=r)}return t.reverse()}function pn(n,i){var r,o,t;if(i.isZero())return i;o=i.d.length,o<32?(r=Math.ceil(o/3),t=(1/Ut(4,r)).toString()):(r=16,t="2.3283064365386962890625e-10"),n.precision+=r,i=bt(n,1,i.times(t),new n(1));for(var u=r;u--;){var p=i.times(i);i=p.times(p).minus(p).times(8).plus(1)}return n.precision-=r,i}var ie=(function(){function n(o,t,u){var p,c=0,g=o.length;for(o=o.slice();g--;)p=o[g]*t+c,o[g]=p%u|0,c=p/u|0;return c&&o.unshift(c),o}function i(o,t,u,p){var c,g;if(u!=p)g=u>p?1:-1;else for(c=g=0;c<u;c++)if(o[c]!=t[c]){g=o[c]>t[c]?1:-1;break}return g}function r(o,t,u,p){for(var c=0;u--;)o[u]-=c,c=o[u]<t[u]?1:0,o[u]=c*p+o[u]-t[u];for(;!o[0]&&o.length>1;)o.shift()}return function(o,t,u,p,c,g){var M,b,S,C,T,D,O,B,R,x,q,j,W,$,z,m,y,A,P,L,h=o.constructor,e=o.s==t.s?1:-1,s=o.d,l=t.d;if(!s||!s[0]||!l||!l[0])return new h(!o.s||!t.s||(s?l&&s[0]==l[0]:!l)?NaN:s&&s[0]==0||!l?e*0:e/0);for(g?(T=1,b=o.e-t.e):(g=tt,T=H,b=ze(o.e/T)-ze(t.e/T)),P=l.length,y=s.length,R=new h(e),x=R.d=[],S=0;l[S]==(s[S]||0);S++);if(l[S]>(s[S]||0)&&b--,u==null?($=u=h.precision,p=h.rounding):c?$=u+(o.e-t.e)+1:$=u,$<0)x.push(1),D=!0;else{if($=$/T+2|0,S=0,P==1){for(C=0,l=l[0],$++;(S<y||C)&&$--;S++)z=C*g+(s[S]||0),x[S]=z/l|0,C=z%l|0;D=C||S<y}else{for(C=g/(l[0]+1)|0,C>1&&(l=n(l,C,g),s=n(s,C,g),P=l.length,y=s.length),m=P,q=s.slice(0,P),j=q.length;j<P;)q[j++]=0;L=l.slice(),L.unshift(0),A=l[0],l[1]>=g/2&&++A;do C=0,M=i(l,q,P,j),M<0?(W=q[0],P!=j&&(W=W*g+(q[1]||0)),C=W/A|0,C>1?(C>=g&&(C=g-1),O=n(l,C,g),B=O.length,j=q.length,M=i(O,q,B,j),M==1&&(C--,r(O,P<B?L:l,B,g))):(C==0&&(M=C=1),O=l.slice()),B=O.length,B<j&&O.unshift(0),r(q,O,j,g),M==-1&&(j=q.length,M=i(l,q,P,j),M<1&&(C++,r(q,P<j?L:l,j,g))),j=q.length):M===0&&(C++,q=[0]),x[S++]=C,M&&q[0]?q[j++]=s[m]||0:(q=[s[m]],j=1);while((m++<y||q[0]!==void 0)&&$--);D=q[0]!==void 0}x[0]||x.shift()}if(T==1)R.e=b,ai=D;else{for(S=1,C=x[0];C>=10;C/=10)S++;R.e=S+b*T-1,K(R,c?u+R.e+1:u,p,D)}return R}})();function K(n,i,r,o){var t,u,p,c,g,M,b,S,C,T=n.constructor;e:if(i!=null){if(S=n.d,!S)return n;for(t=1,c=S[0];c>=10;c/=10)t++;if(u=i-t,u<0)u+=H,p=i,b=S[C=0],g=b/Fe(10,t-p-1)%10|0;else if(C=Math.ceil((u+1)/H),c=S.length,C>=c)if(o){for(;c++<=C;)S.push(0);b=g=0,t=1,u%=H,p=u-H+1}else break e;else{for(b=c=S[C],t=1;c>=10;c/=10)t++;u%=H,p=u-H+t,g=p<0?0:b/Fe(10,t-p-1)%10|0}if(o=o||i<0||S[C+1]!==void 0||(p<0?b:b%Fe(10,t-p-1)),M=r<4?(g||o)&&(r==0||r==(n.s<0?3:2)):g>5||g==5&&(r==4||o||r==6&&(u>0?p>0?b/Fe(10,t-p):0:S[C-1])%10&1||r==(n.s<0?8:7)),i<1||!S[0])return S.length=0,M?(i-=n.e+1,S[0]=Fe(10,(H-i%H)%H),n.e=-i||0):S[0]=n.e=0,n;if(u==0?(S.length=C,c=1,C--):(S.length=C+1,c=Fe(10,H-u),S[C]=p>0?(b/Fe(10,t-p)%Fe(10,p)|0)*c:0),M)for(;;)if(C==0){for(u=1,p=S[0];p>=10;p/=10)u++;for(p=S[0]+=c,c=1;p>=10;p/=10)c++;u!=c&&(n.e++,S[0]==tt&&(S[0]=1));break}else{if(S[C]+=c,S[C]!=tt)break;S[C--]=0,c=1}for(u=S.length;S[--u]===0;)S.pop()}return V&&(n.e>T.maxE?(n.d=null,n.e=NaN):n.e<T.minE&&(n.e=0,n.d=[0])),n}function nt(n,i,r){if(!n.isFinite())return gi(n);var o,t=n.e,u=$e(n.d),p=u.length;return i?(r&&(o=r-p)>0?u=u.charAt(0)+"."+u.slice(1)+ut(o):p>1&&(u=u.charAt(0)+"."+u.slice(1)),u=u+(n.e<0?"e":"e+")+n.e):t<0?(u="0."+ut(-t-1)+u,r&&(o=r-p)>0&&(u+=ut(o))):t>=p?(u+=ut(t+1-p),r&&(o=r-t-1)>0&&(u=u+"."+ut(o))):((o=t+1)<p&&(u=u.slice(0,o)+"."+u.slice(o)),r&&(o=r-p)>0&&(t+1===p&&(u+="."),u+=ut(o))),u}function Ft(n,i){var r=n[0];for(i*=H;r>=10;r/=10)i++;return i}function Nt(n,i,r){if(i>hn)throw V=!0,r&&(n.precision=r),Error(li);return K(new n(Rt),i,1,!0)}function it(n,i,r){if(i>kr)throw Error(li);return K(new n(Bt),i,r,!0)}function di(n){var i=n.length-1,r=i*H+1;if(i=n[i],i){for(;i%10==0;i/=10)r--;for(i=n[0];i>=10;i/=10)r++}return r}function ut(n){for(var i="";n--;)i+="0";return i}function hi(n,i,r,o){var t,u=new n(1),p=Math.ceil(o/H+4);for(V=!1;;){if(r%2&&(u=u.times(i),oi(u.d,p)&&(t=!0)),r=ze(r/2),r===0){r=u.d.length-1,t&&u.d[r]===0&&++u.d[r];break}i=i.times(i),oi(i.d,p)}return V=!0,u}function ni(n){return n.d[n.d.length-1]&1}function pi(n,i,r){for(var o,t,u=new n(i[0]),p=0;++p<i.length;){if(t=new n(i[p]),!t.s){u=t;break}o=u.cmp(t),(o===r||o===0&&u.s===r)&&(u=t)}return u}function Ar(n,i){var r,o,t,u,p,c,g,M=0,b=0,S=0,C=n.constructor,T=C.rounding,D=C.precision;if(!n.d||!n.d[0]||n.e>17)return new C(n.d?n.d[0]?n.s<0?0:1/0:1:n.s?n.s<0?0:n:NaN);for(i==null?(V=!1,g=D):g=i,c=new C(.03125);n.e>-2;)n=n.times(c),S+=5;for(o=Math.log(Fe(2,S))/Math.LN10*2+5|0,g+=o,r=u=p=new C(1),C.precision=g;;){if(u=K(u.times(n),g,1),r=r.times(++b),c=p.plus(ie(u,r,g,1)),$e(c.d).slice(0,g)===$e(p.d).slice(0,g)){for(t=S;t--;)p=K(p.times(p),g,1);if(i==null)if(M<3&&At(p.d,g-o,T,M))C.precision=g+=10,r=u=c=new C(1),b=0,M++;else return K(p,C.precision=D,T,V=!0);else return C.precision=D,p}p=c}}function ft(n,i){var r,o,t,u,p,c,g,M,b,S,C,T=1,D=10,O=n,B=O.d,R=O.constructor,x=R.rounding,q=R.precision;if(O.s<0||!B||!B[0]||!O.e&&B[0]==1&&B.length==1)return new R(B&&!B[0]?-1/0:O.s!=1?NaN:B?0:O);if(i==null?(V=!1,b=q):b=i,R.precision=b+=D,r=$e(B),o=r.charAt(0),Math.abs(u=O.e)<15e14){for(;o<7&&o!=1||o==1&&r.charAt(1)>3;)O=O.times(n),r=$e(O.d),o=r.charAt(0),T++;u=O.e,o>1?(O=new R("0."+r),u++):O=new R(o+"."+r.slice(1))}else return M=Nt(R,b+2,q).times(u+""),O=ft(new R(o+"."+r.slice(1)),b-D).plus(M),R.precision=q,i==null?K(O,q,x,V=!0):O;for(S=O,g=p=O=ie(O.minus(1),O.plus(1),b,1),C=K(O.times(O),b,1),t=3;;){if(p=K(p.times(C),b,1),M=g.plus(ie(p,new R(t),b,1)),$e(M.d).slice(0,b)===$e(g.d).slice(0,b))if(g=g.times(2),u!==0&&(g=g.plus(Nt(R,b+2,q).times(u+""))),g=ie(g,new R(T),b,1),i==null)if(At(g.d,b-D,x,c))R.precision=b+=D,M=p=O=ie(S.minus(1),S.plus(1),b,1),C=K(O.times(O),b,1),t=c=1;else return K(g,R.precision=q,x,V=!0);else return R.precision=q,g;g=M,t+=2}}function gi(n){return String(n.s*n.s/0)}function It(n,i){var r,o,t;for((r=i.indexOf("."))>-1&&(i=i.replace(".","")),(o=i.search(/e/i))>0?(r<0&&(r=o),r+=+i.slice(o+1),i=i.substring(0,o)):r<0&&(r=i.length),o=0;i.charCodeAt(o)===48;o++);for(t=i.length;i.charCodeAt(t-1)===48;--t);if(i=i.slice(o,t),i){if(t-=o,n.e=r=r-o-1,n.d=[],o=(r+1)%H,r<0&&(o+=H),o<t){for(o&&n.d.push(+i.slice(0,o)),t-=H;o<t;)n.d.push(+i.slice(o,o+=H));i=i.slice(o),o=H-i.length}else o-=t;for(;o--;)i+="0";n.d.push(+i),V&&(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 gn(n,i){var r,o,t,u,p,c,g,M,b;if(i.indexOf("_")>-1){if(i=i.replace(/(\d)_(?=\d)/g,"$1"),ci.test(i))return It(n,i)}else if(i==="Infinity"||i==="NaN")return+i||(n.s=NaN),n.e=NaN,n.d=null,n;if(fn.test(i))r=16,i=i.toLowerCase();else if(un.test(i))r=2;else if(cn.test(i))r=8;else throw Error(ct+i);for(u=i.search(/p/i),u>0?(g=+i.slice(u+1),i=i.substring(2,u)):i=i.slice(2),u=i.indexOf("."),p=u>=0,o=n.constructor,p&&(i=i.replace(".",""),c=i.length,u=c-u,t=hi(o,new o(r),u,u*2)),M=Ot(i,r,tt),b=M.length-1,u=b;M[u]===0;--u)M.pop();return u<0?new o(n.s*0):(n.e=Ft(M,b),n.d=M,V=!1,p&&(n=ie(n,t,c*4)),g&&(n=n.times(Math.abs(g)<54?Fe(2,g):St.pow(2,g))),V=!0,n)}function mn(n,i){var r,o=i.d.length;if(o<3)return i.isZero()?i:bt(n,2,i,i);r=1.4*Math.sqrt(o),r=r>16?16:r|0,i=i.times(1/Ut(5,r)),i=bt(n,2,i,i);for(var t,u=new n(5),p=new n(16),c=new n(20);r--;)t=i.times(i),i=i.times(u.plus(t.times(p.times(t).minus(c))));return i}function bt(n,i,r,o,t){var u,p,c,g,M=1,b=n.precision,S=Math.ceil(b/H);for(V=!1,g=r.times(r),c=new n(o);;){if(p=ie(c.times(g),new n(i++*i++),b,1),c=t?o.plus(p):o.minus(p),o=ie(p.times(g),new n(i++*i++),b,1),p=c.plus(o),p.d[S]!==void 0){for(u=S;p.d[u]===c.d[u]&&u--;);if(u==-1)break}u=c,c=o,o=p,p=u,M++}return V=!0,p.d.length=S+1,p}function Ut(n,i){for(var r=n;--i;)r*=n;return r}function mi(n,i){var r,o=i.s<0,t=it(n,n.precision,1),u=t.times(.5);if(i=i.abs(),i.lte(u))return st=o?4:1,i;if(r=i.divToInt(t),r.isZero())st=o?3:2;else{if(i=i.minus(r.times(t)),i.lte(u))return st=ni(r)?o?2:3:o?4:1,i;st=ni(r)?o?1:4:o?3:2}return i.minus(t).abs()}function Sr(n,i,r,o){var t,u,p,c,g,M,b,S,C,T=n.constructor,D=r!==void 0;if(D?(He(r,1,dt),o===void 0?o=T.rounding:He(o,0,8)):(r=T.precision,o=T.rounding),!n.isFinite())b=gi(n);else{for(b=nt(n),p=b.indexOf("."),D?(t=2,i==16?r=r*4-3:i==8&&(r=r*3-2)):t=i,p>=0&&(b=b.replace(".",""),C=new T(1),C.e=b.length-p,C.d=Ot(nt(C),10,t),C.e=C.d.length),S=Ot(b,10,t),u=g=S.length;S[--g]==0;)S.pop();if(!S[0])b=D?"0p+0":"0";else{if(p<0?u--:(n=new T(n),n.d=S,n.e=u,n=ie(n,C,r,o,0,t),S=n.d,u=n.e,M=ai),p=S[r],c=t/2,M=M||S[r+1]!==void 0,M=o<4?(p!==void 0||M)&&(o===0||o===(n.s<0?3:2)):p>c||p===c&&(o===4||M||o===6&&S[r-1]&1||o===(n.s<0?8:7)),S.length=r,M)for(;++S[--r]>t-1;)S[r]=0,r||(++u,S.unshift(1));for(g=S.length;!S[g-1];--g);for(p=0,b="";p<g;p++)b+=yr.charAt(S[p]);if(D){if(g>1)if(i==16||i==8){for(p=i==16?4:3,--g;g%p;g++)b+="0";for(S=Ot(b,t,i),g=S.length;!S[g-1];--g);for(p=1,b="1.";p<g;p++)b+=yr.charAt(S[p])}else b=b.charAt(0)+"."+b.slice(1);b=b+(u<0?"p":"p+")+u}else if(u<0){for(;++u;)b="0"+b;b="0."+b}else if(++u>g)for(u-=g;u--;)b+="0";else u<g&&(b=b.slice(0,u)+"."+b.slice(u))}b=(i==16?"0x":i==2?"0b":i==8?"0o":"")+b}return n.s<0?"-"+b:b}function oi(n,i){if(n.length>i)return n.length=i,!0}function vn(n){return new this(n).abs()}function wn(n){return new this(n).acos()}function Mn(n){return new this(n).acosh()}function bn(n,i){return new this(n).plus(i)}function yn(n){return new this(n).asin()}function _n(n){return new this(n).asinh()}function kn(n){return new this(n).atan()}function An(n){return new this(n).atanh()}function Sn(n,i){n=new this(n),i=new this(i);var r,o=this.precision,t=this.rounding,u=o+4;return!n.s||!i.s?r=new this(NaN):!n.d&&!i.d?(r=it(this,u,1).times(i.s>0?.25:.75),r.s=n.s):!i.d||n.isZero()?(r=i.s<0?it(this,o,t):new this(0),r.s=n.s):!n.d||i.isZero()?(r=it(this,u,1).times(.5),r.s=n.s):i.s<0?(this.precision=u,this.rounding=1,r=this.atan(ie(n,i,u,1)),i=it(this,u,1),this.precision=o,this.rounding=t,r=n.s<0?r.minus(i):r.plus(i)):r=this.atan(ie(n,i,u,1)),r}function Cn(n){return new this(n).cbrt()}function Pn(n){return K(n=new this(n),n.e+1,2)}function Ln(n,i,r){return new this(n).clamp(i,r)}function Tn(n){if(!n||typeof n!="object")throw Error(Et+"Object expected");var i,r,o,t=n.defaults===!0,u=["precision",1,dt,"rounding",0,8,"toExpNeg",-Mt,0,"toExpPos",0,Mt,"maxE",0,Mt,"minE",-Mt,0,"modulo",0,9];for(i=0;i<u.length;i+=3)if(r=u[i],t&&(this[r]=_r[r]),(o=n[r])!==void 0)if(ze(o)===o&&o>=u[i+1]&&o<=u[i+2])this[r]=o;else throw Error(ct+r+": "+o);if(r="crypto",t&&(this[r]=_r[r]),(o=n[r])!==void 0)if(o===!0||o===!1||o===0||o===1)if(o)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[r]=!0;else throw Error(ui);else this[r]=!1;else throw Error(ct+r+": "+o);return this}function xn(n){return new this(n).cos()}function Dn(n){return new this(n).cosh()}function vi(n){var i,r,o;function t(u){var p,c,g,M=this;if(!(M instanceof t))return new t(u);if(M.constructor=t,si(u)){M.s=u.s,V?!u.d||u.e>t.maxE?(M.e=NaN,M.d=null):u.e<t.minE?(M.e=0,M.d=[0]):(M.e=u.e,M.d=u.d.slice()):(M.e=u.e,M.d=u.d?u.d.slice():u.d);return}if(g=typeof u,g==="number"){if(u===0){M.s=1/u<0?-1:1,M.e=0,M.d=[0];return}if(u<0?(u=-u,M.s=-1):M.s=1,u===~~u&&u<1e7){for(p=0,c=u;c>=10;c/=10)p++;V?p>t.maxE?(M.e=NaN,M.d=null):p<t.minE?(M.e=0,M.d=[0]):(M.e=p,M.d=[u]):(M.e=p,M.d=[u]);return}if(u*0!==0){u||(M.s=NaN),M.e=NaN,M.d=null;return}return It(M,u.toString())}if(g==="string")return(c=u.charCodeAt(0))===45?(u=u.slice(1),M.s=-1):(c===43&&(u=u.slice(1)),M.s=1),ci.test(u)?It(M,u):gn(M,u);if(g==="bigint")return u<0?(u=-u,M.s=-1):M.s=1,It(M,u.toString());throw Error(ct+u)}if(t.prototype=N,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=Tn,t.clone=vi,t.isDecimal=si,t.abs=vn,t.acos=wn,t.acosh=Mn,t.add=bn,t.asin=yn,t.asinh=_n,t.atan=kn,t.atanh=An,t.atan2=Sn,t.cbrt=Cn,t.ceil=Pn,t.clamp=Ln,t.cos=xn,t.cosh=Dn,t.div=On,t.exp=In,t.floor=Rn,t.hypot=Bn,t.ln=Nn,t.log=En,t.log10=Un,t.log2=Fn,t.max=qn,t.min=jn,t.mod=$n,t.mul=Zn,t.pow=Wn,t.random=zn,t.round=Kn,t.sign=Gn,t.sin=Hn,t.sinh=Vn,t.sqrt=Qn,t.sub=Xn,t.sum=Jn,t.tan=Yn,t.tanh=eo,t.trunc=to,n===void 0&&(n={}),n&&n.defaults!==!0)for(o=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],i=0;i<o.length;)n.hasOwnProperty(r=o[i++])||(n[r]=this[r]);return t.config(n),t}function On(n,i){return new this(n).div(i)}function In(n){return new this(n).exp()}function Rn(n){return K(n=new this(n),n.e+1,3)}function Bn(){var n,i,r=new this(0);for(V=!1,n=0;n<arguments.length;)if(i=new this(arguments[n++]),i.d)r.d&&(r=r.plus(i.times(i)));else{if(i.s)return V=!0,new this(1/0);r=i}return V=!0,r.sqrt()}function si(n){return n instanceof St||n&&n.toStringTag===fi||!1}function Nn(n){return new this(n).ln()}function En(n,i){return new this(n).log(i)}function Fn(n){return new this(n).log(2)}function Un(n){return new this(n).log(10)}function qn(){return pi(this,arguments,-1)}function jn(){return pi(this,arguments,1)}function $n(n,i){return new this(n).mod(i)}function Zn(n,i){return new this(n).mul(i)}function Wn(n,i){return new this(n).pow(i)}function zn(n){var i,r,o,t,u=0,p=new this(1),c=[];if(n===void 0?n=this.precision:He(n,1,dt),o=Math.ceil(n/H),this.crypto)if(crypto.getRandomValues)for(i=crypto.getRandomValues(new Uint32Array(o));u<o;)t=i[u],t>=429e7?i[u]=crypto.getRandomValues(new Uint32Array(1))[0]:c[u++]=t%1e7;else if(crypto.randomBytes){for(i=crypto.randomBytes(o*=4);u<o;)t=i[u]+(i[u+1]<<8)+(i[u+2]<<16)+((i[u+3]&127)<<24),t>=214e7?crypto.randomBytes(4).copy(i,u):(c.push(t%1e7),u+=4);u=o/4}else throw Error(ui);else for(;u<o;)c[u++]=Math.random()*1e7|0;for(o=c[--u],n%=H,o&&n&&(t=Fe(10,H-n),c[u]=(o/t|0)*t);c[u]===0;u--)c.pop();if(u<0)r=0,c=[0];else{for(r=-1;c[0]===0;r-=H)c.shift();for(o=1,t=c[0];t>=10;t/=10)o++;o<H&&(r-=H-o)}return p.e=r,p.d=c,p}function Kn(n){return K(n=new this(n),n.e+1,this.rounding)}function Gn(n){return n=new this(n),n.d?n.d[0]?n.s:0*n.s:n.s||NaN}function Hn(n){return new this(n).sin()}function Vn(n){return new this(n).sinh()}function Qn(n){return new this(n).sqrt()}function Xn(n,i){return new this(n).sub(i)}function Jn(){var n=0,i=arguments,r=new this(i[n]);for(V=!1;r.s&&++n<i.length;)r=r.plus(i[n]);return V=!0,K(r,this.precision,this.rounding)}function Yn(n){return new this(n).tan()}function eo(n){return new this(n).tanh()}function to(n){return K(n=new this(n),n.e+1,1)}N[Symbol.for("nodejs.util.inspect.custom")]=N.toString;N[Symbol.toStringTag]="Decimal";var St=N.constructor=vi(_r);Rt=new St(Rt);Bt=new St(Bt);var Ue=St;var Dr=Ei(Mi());import{Transaction as rt}from"@mysten/sui/transactions";import{addHexPrefix as bi,d as ht,getObjectFields as yi}from"@cetusprotocol/common-sdk";import{parseObligation as ro}from"@suilend/sdk/parsers/obligation";import{getNetAprPercent as io,getTotalAprPercent as no,Side as oo,getFilteredRewards as so}from"@suilend/sdk";var Pr=n=>{let i=yi(n),{openPermissionsPause:r,closePermissionsPause:o,depositPermissionsPause:t,withdrawPermissionsPause:u,borrowPermissionsPause:p,repayPermissionsPause:c}=xr(i.permissions.toString(2));return{marketId:i.id.id,baseToken:bi(i.base_token.fields.name),quoteToken:bi(i.quote_token.fields.name),maxLongLeverage:ht(i.max_long_leverage).div(10**6).toString(),maxShortLeverage:ht(i.max_short_leverage).div(10**6).toString(),openFeeRate:ht(i.open_fee_rate).div(10**6).toString(),closeFeeRate:ht(i.close_fee_rate).div(10**6).toString(),openPermissionsPause:r,closePermissionsPause:o,depositPermissionsPause:t,withdrawPermissionsPause:u,borrowPermissionsPause:p,repayPermissionsPause:c}},Lr=(n,i)=>{let r=yi(n);return{createdAt:r.created_ts,positionId:r.id.id,initDepositAmount:r.init_deposit_amount,isLong:r.is_long,lendingMarketId:r.lending_market_id,obligationOwnerCap:r.obligation_owner_cap.fields.obligation_id,marketId:r.market_id,positionCapId:i}},Tr=(n,i,r,o,t)=>{let u=ro(i,r),p=io(u,t,o);if(!u.deposits||u.deposits.length===0)throw new Error("No deposits found in obligation data");let c=u.deposits[0],g=ht(c.reserve.config.closeLtvPct).div(100).toString(),M=ht(c.reserve.config.openLtvPct).div(100).toString(),b=u.depositedAmountUsd.gt(0)?u.borrowedAmountUsd.div(u.depositedAmountUsd).toString():"0",S=ht(1).div(g).toString(),C=ht(g).sub(b).toString(),T="0",D="0",O="0";if(u.borrows&&u.borrows.length>0){let B=u.borrows[0],R=t[B?.coinType];R&&(T=no(oo.BORROW,B?.reserve.borrowAprPercent,so(R.borrow)).toString()),D=B.borrowedAmountUsd.toString(),O=B.borrowedAmount.toString()}return{...n,...u,originObligation:i,netAprPercent:p.toString(),suppliedAPR:c.reserve.depositAprPercent.toString(),borrowedAPR:T,closeLTV:g,openLTV:M,cr:b,ltr:S,liquidationBuffer:C,depositedAmountUsd:c.depositedAmountUsd.toString(),borrowedAmountUsd:D,depositedAmount:c.depositedAmount.toString(),borrowedAmount:O}},xr=n=>{let i=n.length,r=n.substring(i-1,i)==="0",o=n.substring(i-2,i-1)==="0",t=n.substring(i-3,i-2)==="0",u=n.substring(i-4,i-3)==="0",p=n.substring(i-5,i-4)==="0",c=n.substring(i-6,i-5)==="0";return{openPermissionsPause:r,closePermissionsPause:o,depositPermissionsPause:t,withdrawPermissionsPause:u,borrowPermissionsPause:p,repayPermissionsPause:c}};import{SUI_SYSTEM_STATE_OBJECT_ID as Si}from"@mysten/sui/utils";import{compoundDebt as Ci,getFilteredRewards as qt,getStakingYieldAprPercent as Pi,getTotalAprPercent as jt,Side as _t}from"@suilend/sdk";var $t=class{constructor(i){this.getLeverageConfig=async()=>{let{package_id:i}=this._sdk.sdkOptions.leverage,r=ne(this._sdk.sdkOptions.leverage);try{let o=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::versioned::InitEvent`}),t=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::admin_cap::InitEvent`}),u=await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::config::InitEvent`}),c=(await this._sdk.FullClient.queryEventsByPage({MoveEventType:`${i}::market::InitEvent`})).data[0].parsedJson.markets_id,M=(await this._sdk.FullClient.getObject({id:c,options:{showContent:!0}})).data.content.fields.list.fields.id.id;return{versioned:o.data[0].parsedJson.versioned,admin_cap_id:t.data[0].parsedJson.admin_cap_id,global_config_id:u.data[0].parsedJson.global_config_id,markets:c,markets_table_id:M}}catch(o){return console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:78 ~ LeverageModules ~ error:",o),{versioned:"",admin_cap_id:"",global_config_id:"",markets_table_id:"",markets:""}}};this.getLeverageMarketsList=async(i=!1)=>{let o=this._sdk.getCache("leverage_markets_list",i);if(o)return o;let{package_id:t}=this._sdk.sdkOptions.leverage,u=[];try{let p=`${t}::market::CreateMarketEvent`,c=await this._sdk.FullClient.queryEventsByPage({MoveEventType:p});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:98 ~ LeverageModules ~ objects:",c);let g=c.data.map(M=>M.parsedJson.market_id);if(g.length>0)return(await this._sdk.FullClient.batchGetObjects(g,{showContent:!0,showType:!0})).forEach(b=>{let S=Pr(b),C=`leverage_market_info_${S.marketId}`;this._sdk.updateCache(C,S),u.push(S)}),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:106 ~ LeverageModules ~ marketList:",u),u}catch(p){return console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:108 ~ LeverageModules ~ error:",p),[]}};this.getLeverageMarketSuilendInfo=async i=>{let{baseToken:r,quoteToken:o}=await this.getLeverageMarketInfo(i),{lendingMarketId:t}=ne(this._sdk.sdkOptions.suilend),{allLendingMarketData:u,lstAprPercentMap:p}=await this._sdk.SuiLendModule.getLendingMarketData(!0),{baseReserveMapInfo:c,quoteReserveMapInfo:g}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(r,o,u);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:140 ~ LeverageModules ~ baseReserveMapInfo:",c);let M=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:u,lstAprPercentMap:p},!0);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:100 ~ LeverageModules ~ obligations:",M);let b=M[t].rewardMap[r],S=M[t].rewardMap[o],{depositedAmountUsd:C,borrowedAmountUsd:T,depositAprPercent:D,borrowAprPercent:O,depositedAmount:B,borrowedAmount:R}=c,{depositLimit:x,borrowLimit:q}=c.config,{depositedAmountUsd:j,borrowedAmountUsd:W,depositAprPercent:$,borrowAprPercent:z,depositedAmount:m,borrowedAmount:y}=g,{depositLimit:A,borrowLimit:P}=g.config,L=jt(_t.DEPOSIT,D,qt(b.deposit),Pi(_t.DEPOSIT,r,p)),h=jt(_t.BORROW,O,qt(b.borrow)),e=jt(_t.DEPOSIT,$,qt(S.deposit),Pi(_t.DEPOSIT,r,p)),s=jt(_t.BORROW,z,qt(S.borrow)),l=c.config.borrowLimitUsd,f=g.config.borrowLimitUsd,d=I(j.toString()).sub(W.toString()).toString(),v=I(C.toString()).sub(T.toString()).toString(),k=I(x.toString()).sub(I(B.toString())).toString(),w=I(q.toString()).sub(I(R.toString())).toString(),a=I(A.toString()).sub(I(m.toString())).toString(),_=I(P.toString()).sub(I(y.toString())).toString();return{longLiquidity:d,shortLiquidity:v,baseTotalDepositAprPercent:L.toString(),baseTotalBorrowAprPercent:h.toString(),quoteTotalDepositAprPercent:e.toString(),quoteTotalBorrowAprPercent:s.toString(),baseTokenAvailableDepositAmount:k,baseTokenAvailableBorrowAmount:w,quoteTokenAvailableDepositAmount:a,quoteTokenAvailableBorrowAmount:_}};this.getLeverageMarketInfo=async(i,r=!1)=>{let o=`leverage_market_info_${i}`,t=this._sdk.getCache(o,r);if(t)return t;let u=await this._sdk.FullClient.getObject({id:i,options:{showContent:!0}}),p=Pr(u);return this._sdk.updateCache(o,p,_i),p};this.getLeveragePositionList=async(i=this._sdk.getSenderAddress())=>{let r=await this._sdk.FullClient.getOwnedObjectsByPage(i,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{MatchAny:[{Package:this._sdk.sdkOptions.leverage.package_id}]}});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:126 ~ LeverageModules ~ ownerRes:",r);let{lendingMarketId:o,lendingMarketType:t}=ne(this._sdk.sdkOptions.suilend),u=await this._sdk.SuiLendModule.getSuilendClient(o,t),{allLendingMarketData:p,lstAprPercentMap:c}=await this._sdk.SuiLendModule.getLendingMarketData(),g=p[o].reserveMap,M=[],b=[],S=[];for(let D=0;D<r.data.length;D++){let O=ki(r.data[D]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:259 ~ LeverageModules ~ fields:",O),O.position_id&&(b.push(O.position_id),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:260 ~ LeverageModules ~ positionIdList:",b),M.push(O.id.id))}let C=await this._sdk.FullClient.batchGetObjects(b,{showContent:!0}),T=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:p,lstAprPercentMap:c},!0);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:269 ~ LeverageModules ~ obligations:",T);for(let D=0;D<C.length;D++){let O=Lr(C[D],M[D]),B=await u.getObligation(O.obligationOwnerCap);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:250 ~ LeverageModules ~ rawObligation:",B);let R=Tr(O,B,g||{},c||{},T[o].rewardMap);S.push({...R})}return S};this.getLeveragePosition=async(i,r=this._sdk.getSenderAddress(),o=!0)=>{console.log("\u{1F680} ~ LeverageModules ~ walletAddress:",r);let t=`leverage_position_${i}`,u=this._sdk.getCache(t,o);if(u)return u;let p=await this._sdk.FullClient.getOwnedObjectsByPage(r,{options:{showType:!0,showContent:!0,showOwner:!0},filter:{ObjectId:i}}),c=ki(p.data[0]),g=await this._sdk.FullClient.batchGetObjects([c.position_id],{showContent:!0}),M=Lr(g[0],i),{lendingMarketId:b,lendingMarketType:S}=ne(this._sdk.sdkOptions.suilend),C=await this._sdk.SuiLendModule.getSuilendClient(b,S),{allLendingMarketData:T,lstAprPercentMap:D}=await this._sdk.SuiLendModule.getLendingMarketData(),O=T[b].reserveMap,B=await this._sdk.SuiLendModule.getInitializeObligations({allLendingMarketData:T,lstAprPercentMap:D},!0),R=await C.getObligation(M.obligationOwnerCap),q={...Tr(M,R,O||{},D||{},B[b].rewardMap)};return this._sdk.updateCache(t,q,_i),q};this.getClmmPoolInfo=async i=>{let r=await this._sdk.ClmmSDK.Pool.getPool(i,!1),{coin_type_a:o,coin_type_b:t,fee_rate:u}=r;return{clmmFeeTier:u/1e6,clmmPoolCoinTypeA:o,clmmPoolCoinTypeB:t}};this.openLeveragePosition=async i=>{let{isQuote:r,isLong:o,amount:t,swapClmmPool:u="",slippage:p,leverage:c,marketId:g}=i,{lendingMarketId:M}=ne(this._sdk.sdkOptions.suilend),b=new rt,{openFeeRate:S,baseToken:C,quoteToken:T}=await this.getLeverageMarketInfo(g),{baseReserveArrayIndex:D,quoteReserveArrayIndex:O,reserve:B}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(C,T),R=B[0].mintDecimals,x=B[1].mintDecimals,{inputCoin:q,initDepositAmount:j}=await this.handleSwapLogic({isLong:o,isQuote:r,amount:t,baseToken:C,quoteToken:T,swapClmmPool:u,slippage:p,tx:b}),{flashAmount:W,flashLoanCoin:$,isFlashA:z,flashLoanFee:m,clmmPoolCoinTypeA:y,clmmPoolCoinTypeB:A,clmmPool:P}=await this.calculateFlashLoanParams({isLong:o,leverage:c,baseToken:C,quoteToken:T,initDepositAmount:j,reserve:B,baseTokenDecimal:R,quoteTokenDecimal:x,openFeeRate:S});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:489 ~ LeverageModules ~ flashAmount:",W);let{balanceA:L,balanceB:h,receipt:e}=this.flashLoan({amount:I(W),clmmPool:P,clmmPoolCoinTypeA:y,clmmPoolCoinTypeB:A,flashLoanCoin:$,tx:b}),{debtSwapAmountOut:s,debtSwapOutCoin:l}=await this.handleDebtSwapLogic({isLong:o,flashAmount:W,baseToken:C,quoteToken:T,swapClmmPool:u,isFlashA:z,balanceA:L,balanceB:h,slippage:p,clmmPoolCoinTypeA:y,clmmPoolCoinTypeB:A,tx:b});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:528 ~ LeverageModules ~ debtSwapAmountOut:",s.toString());let f=I(s).add(j).mul(1-S).toDP(0,Ue.ROUND_DOWN).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:550 ~ LeverageModules ~ totalDepositAmount:",f),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:528 ~ LeverageModules ~ debtSwapAmountOut:",s);let{positionCap:d,marginTradingContext:v}=this.createLeveragePosition({leverage:c,marketId:g,baseToken:C,quoteToken:T,isLong:o,initDepositAmount:I(t).toString(),initCoinType:r?T:C,tx:b});await this._sdk.SuiLendModule.refreshReservePrices(b,B);let k=o?D:O;await b.mergeCoins(l,[q]),await this.depositToLeveragePosition({isLong:o,marketId:g,positionCap:d,depositReserveArrayIndex:k,inputCoin:l,marginTradingContext:v},b);let w=I(W).add(m).toDP(0,Ue.ROUND_UP).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:407 ~ LeverageModules ~ borrowAmount:",{flashAmount:W,flashLoanFee:m,clmmPool:P});let a=this.borrowAsset({positionCap:d,reserveArrayIndex:o?O:D,borrowAmount:I(w),baseToken:C,quoteToken:T,isLong:o,lendingMarketId:M,marketId:g,marginTradingContext:v},b),_=Y.mintBalanceZero(z?A:y,b);return this.repayFlashLoan({tx:b,clmmPool:P,repayBase:z?Y.intoBalance(a,y,b):_,repayQuote:z?_:Y.intoBalance(a,A,b),receipt:e,clmmPoolCoinTypeA:y,clmmPoolCoinTypeB:A}),b.transferObjects([d],b.pure.address(this._sdk.getSenderAddress())),Y.destroyBalanceZero(z?h:L,z?A:y,b),await this.marginTradingContextConfirm(v,b),b};this.positionManageSizeDeposit=async i=>{let{marketId:r,positionCapId:o,swapClmmPool:t="",isQuote:u,amount:p,slippage:c,leverage:g,txb:M}=i,{lendingMarketId:b}=ne(this._sdk.sdkOptions.suilend),{baseToken:S,quoteToken:C}=await this.getLeverageMarketInfo(r),{isLong:T,deposits:D,borrows:O}=await this.getLeveragePosition(o),{reserve:B}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(S,C),R=M||new rt,x=this.createMarginTradingContext({marketId:r,positionCapId:o,action:"increase_size",coinType:u?C:S,amount:p.toString()},R),q=D[0].reserveArrayIndex.toString(),j=O[0].reserveArrayIndex.toString(),{inputCoin:W,initDepositAmount:$}=await this.handleSwapLogic({isLong:T,isQuote:u,amount:p,baseToken:S,quoteToken:C,swapClmmPool:t,slippage:c,tx:R}),{flashAmount:z,flashLoanFee:m,flashLoanCoin:y,isFlashA:A,borrowAmount:P}=await this.calculatePositionDeposit({marketId:r,isLong:T,isQuote:u,amount:I(p),swapClmmPool:t,leverage:g,slippage:c}),{clmmPoolCoinTypeA:L,clmmPoolCoinTypeB:h,clmmPool:e}=await this.getFlashLoanPool(y,z);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:492 ~ LeverageModules ~ flashLoanCoin:",y),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:492 ~ LeverageModules ~ borrowAmount:",P);let{balanceA:s,balanceB:l,receipt:f}=this.flashLoan({amount:I(z),clmmPool:e,clmmPoolCoinTypeA:L,clmmPoolCoinTypeB:h,flashLoanCoin:y,tx:R}),{debtSwapAmountOut:d,debtSwapOutCoin:v}=await this.handleDebtSwapLogic({isLong:T,flashAmount:z,baseToken:S,quoteToken:C,swapClmmPool:t,isFlashA:A,balanceA:s,balanceB:l,slippage:c,clmmPoolCoinTypeA:L,clmmPoolCoinTypeB:h,tx:R});R.mergeCoins(v,[W]),await this._sdk.SuiLendModule.refreshReservePrices(R,B),await this.depositToLeveragePosition({isLong:T,marketId:r,positionCapId:o,depositReserveArrayIndex:q,inputCoin:v,marginTradingContext:x},R),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:531 ~ LeverageModules ~ borrowAmount:",P);let k=this.borrowAsset({positionCapId:o,reserveArrayIndex:j,borrowAmount:I(P),baseToken:S,quoteToken:C,isLong:T,lendingMarketId:b,marketId:r,marginTradingContext:x},R),w=Y.mintBalanceZero(A?h:L,R);return this.repayFlashLoan({tx:R,clmmPool:e,repayBase:A?Y.intoBalance(k,L,R):w,repayQuote:A?w:Y.intoBalance(k,h,R),receipt:f,clmmPoolCoinTypeA:L,clmmPoolCoinTypeB:h}),Y.destroyBalanceZero(A?l:s,A?h:L,R),await this.marginTradingContextConfirm(x,R),R};this.positionManageSizeWithdraw=async i=>{let{marketId:r,amount:o,isQuote:t,txb:u,swapClmmPool:p="",slippage:c,positionCapId:g,isLong:M,leverage:b}=i,{lendingMarketType:S,lendingMarketId:C}=ne(this._sdk.sdkOptions.suilend),T=u||new rt,{withdrawCtokenAmount:D,repayAmount:O,isClose:B,deposits:R,borrows:x,reserve:q,baseToken:j,quoteToken:W,clmmPool:$,clmmPoolCoinTypeA:z,clmmPoolCoinTypeB:m,clmmFeeTier:y}=await this.calculatePositionWithdraw({marketId:r,positionCapId:g,isLong:M,isQuote:t,swapClmmPool:p,amount:o,leverage:b});if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:575 ~ LeverageModules ~ withdrawAmount:",D),B)return this.leveragePositionCloseWithCoin({marketId:r,positionCapId:g,isQuote:t,slippage:c,swapClmmPool:p});let A=this.createMarginTradingContext({marketId:r,positionCapId:g,action:"decrease_size",coinType:t?W:j,amount:o.toString()},T);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:669 ~ LeverageModules ~ repayAmount:",O),await this._sdk.SuiLendModule.refreshReservePrices(T,q);let{balanceA:P,balanceB:L,receipt:h,isLoanA:e,loanCoinType:s}=this.flashLoan({tx:T,amount:I(O),flashLoanCoin:x[0].reserve.coinType,clmmPool:$,clmmPoolCoinTypeA:z,clmmPoolCoinTypeB:m}),l=e?Y.fromBalance(P,z,T):Y.fromBalance(L,m,T);this.repay({txb:T,positionCapId:g,repayReserveArrayIndex:x[0].reserveArrayIndex.toString(),repayCoin:l,repayCoinType:M?W:j,repayAmount:I(0),marketId:r,marginTradingContext:A}),T.transferObjects([l],this._sdk.getSenderAddress());let f=await this._sdk.SuiLendModule.getSuilendClient(C,S),d=await this._sdk.PythConnection.getPriceFeedsUpdateData([R[0].reserve.priceIdentifier]),v=await f.pythClient.updatePriceFeeds(T,d,[R[0].reserve.priceIdentifier]),k=this.withdrawAsset({marketId:r,positionCapId:g,withdrawAmount:D.toString(),withdrawReserveArrayIndex:R[0].reserveArrayIndex.toString(),withdrawCoinType:M?j:W,baseToken:j,quoteToken:W,priceObjectId:v&&v[0],marginTradingContext:A},T),w=M?j:W,a,_;if(w!==s){_=I(O).add(I(O).mul(y)).toDP(0,Ue.ROUND_UP).toString();let G=await this.findRouters(w,s,I(_),!1,[p]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:748 ~ LeverageModules ~ routers:",{amountIn:G?.amount_in.toString(),amountOut:G?.amount_out.toString(),withdrawAmount:D}),_=G?.amount_in.toString();let te=T.splitCoins(k,[G?.amount_in.toString()]);a=await this.routerSwap({router:G?.route_obj,inputCoin:te,slippage:c,txb:T})}else a=T.splitCoins(k,[O]),_=O;let F=Y.mintBalanceZero(e?m:z,T);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:761 ~ LeverageModules ~ clmmPoolCoinTypeA:",z),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:761 ~ LeverageModules ~ clmmPoolCoinTypeB:",m),a&&this.repayFlashLoan({tx:T,clmmPool:$,repayBase:e?Y.intoBalance(a,z,T):F,repayQuote:e?F:Y.intoBalance(a,m,T),clmmPoolCoinTypeA:z,clmmPoolCoinTypeB:m,receipt:h});let E=t?W:j,U=I(D).sub(I(_)).toString(),Z;if(E!==w){let G=await this.findRouters(w,E,I(U),!0,[p]);Z=await this.routerSwap({router:G?.route_obj,inputCoin:k,slippage:c,txb:T})}else Z=k;return Z&&T.transferObjects([Z],this._sdk.getSenderAddress()),Y.destroyBalanceZero(e?L:P,e?m:z,T),await this.marginTradingContextConfirm(A,T),T};this.positionManageLeverageIncrease=async i=>{let{marketId:r,positionCapId:o,isLong:t,currentLeverage:u,targetLeverage:p,swapClmmPool:c="",slippage:g}=i,{lendingMarketId:M}=ne(this._sdk.sdkOptions.suilend),b=new rt,{flashLoanAmount:S,quoteToken:C,baseToken:T,reserve:D,deposits:O,borrows:B,borrowAmount:R,isFlashLoan:x}=await this.calculatePositionLeverage({marketId:r,positionCapId:o,currentLeverage:u,targetLeverage:p,swapClmmPool:c,slippage:g}),q=t?C:T,{clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W,clmmPool:$}=await this.getFlashLoanPool(q,S?.toString()||"");console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:896 ~ LeverageModules ~ clmmPoolCoinTypeA:",{clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W});let z=this.createMarginTradingContext({marketId:r,positionCapId:o,action:"increase_leverage",coinType:t?T:C,amount:"0"},b);if(D&&await this._sdk.SuiLendModule.refreshReservePrices(b,D),x){let{balanceA:m,balanceB:y,receipt:A,isLoanA:P}=this.flashLoan({tx:b,amount:I(S),clmmPool:$,clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W,flashLoanCoin:q}),L=t?C:T,h=t?T:C,e=await this.findRouters(L,h,I(S),!0,[c]),s=await this.routerSwap({router:e?.route_obj,inputCoin:P?Y.fromBalance(m,j,b):Y.fromBalance(y,W,b),slippage:g,txb:b});await this.depositToLeveragePosition({isLong:t,marketId:r,positionCapId:o,depositReserveArrayIndex:O[0].reserveArrayIndex.toString(),inputCoin:s,marginTradingContext:z},b);let l=await this.borrowAsset({positionCapId:o,reserveArrayIndex:t?D[1].arrayIndex.toString():D[0].arrayIndex.toString(),borrowAmount:I(R),baseToken:T,quoteToken:C,isLong:t,lendingMarketId:M,marketId:r,marginTradingContext:z},b),f=Y.mintBalanceZero(P?W:j,b);this.repayFlashLoan({tx:b,clmmPool:$,repayBase:P?Y.intoBalance(l,j,b):f,repayQuote:P?f:Y.intoBalance(l,W,b),receipt:A,clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W}),Y.destroyBalanceZero(P?y:m,P?W:j,b)}else{let m=await this.borrowAsset({positionCapId:o,reserveArrayIndex:t?D[1].arrayIndex.toString():D[0].arrayIndex.toString(),borrowAmount:I(R),baseToken:T,quoteToken:C,isLong:t,lendingMarketId:M,marketId:r,marginTradingContext:z},b),y=await this.findRouters(t?C:T,t?T:C,I(R),!0,[c]),A=await this.routerSwap({router:y?.route_obj,inputCoin:m,slippage:g,txb:b});await this.depositToLeveragePosition({isLong:t,marketId:r,positionCapId:o,depositReserveArrayIndex:O[0].reserveArrayIndex.toString(),inputCoin:A,marginTradingContext:z},b)}return await this.marginTradingContextConfirm(z,b),b};this.positionManageLeverageDecrease=async i=>{let{marketId:r,positionCapId:o,isLong:t,currentLeverage:u,targetLeverage:p,swapClmmPool:c="",slippage:g}=i,{baseToken:M,quoteToken:b,closeFeeRate:S}=await this.getLeverageMarketInfo(r),{deposits:C,borrows:T}=await this.getLeveragePosition(o),{reserve:D}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(M,b),{lendingMarketId:O,lendingMarketType:B}=ne(this._sdk.sdkOptions.suilend),R=new rt,{withdrawCtokenAmount:x,isFlashLoan:q}=await this.calculatePositionLeverage({marketId:r,positionCapId:o,currentLeverage:u,targetLeverage:p,swapClmmPool:c,slippage:g});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:654 ~ LeverageModules ~ withdrawCtokenAmount:",x,q);let j=this.createMarginTradingContext({marketId:r,positionCapId:o,action:"decrease_leverage",coinType:t?M:b,amount:"0"},R);await this._sdk.SuiLendModule.refreshReservePrices(R,D);let W=await this._sdk.SuiLendModule.getSuilendClient(O,B),$=await this._sdk.PythConnection.getPriceFeedsUpdateData([C[0].reserve.priceIdentifier]),z=await W.pythClient.updatePriceFeeds(R,$,[C[0].reserve.priceIdentifier]),{clmmPoolCoinTypeA:m,clmmPoolCoinTypeB:y,clmmPool:A,clmmFeeTier:P}=await this.getFlashLoanPool(C[0].reserve.coinType,x.toString());if(q){let{balanceA:L,balanceB:h,receipt:e,isLoanA:s}=this.flashLoan({tx:R,amount:I(x),clmmPool:A,clmmPoolCoinTypeA:m,clmmPoolCoinTypeB:y,flashLoanCoin:C[0].reserve.coinType});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1019 ~ LeverageModules ~ isLoanA:",s);let l=t?M:b,f=t?b:M,d=await this.findRouters(l,f,I(x),!0,[c]),v=await this.routerSwap({router:d?.route_obj,inputCoin:s?Y.fromBalance(L,m,R):Y.fromBalance(h,y,R),slippage:g,txb:R});await this.repay({txb:R,marketId:r,positionCapId:o,repayReserveArrayIndex:T[0].reserveArrayIndex.toString(),repayCoin:v,repayCoinType:t?b:M,repayAmount:I(0),marginTradingContext:j}),v&&R.transferObjects([v],this._sdk.getSenderAddress());let k=this.withdrawAsset({marketId:r,positionCapId:o,withdrawAmount:x.toString(),withdrawReserveArrayIndex:C[0].reserveArrayIndex.toString(),withdrawCoinType:t?M:b,baseToken:M,quoteToken:b,priceObjectId:z&&z[0],marginTradingContext:j},R),w=I(x).mul(I(1).add(P)).toDP(0,Ue.ROUND_UP),a=R.splitCoins(k,[w.toString()]);this.repayFlashLoan({tx:R,clmmPool:A,repayBase:s?Y.intoBalance(a,m,R):L,repayQuote:s?h:Y.intoBalance(a,y,R),receipt:e,clmmPoolCoinTypeA:m,clmmPoolCoinTypeB:y}),R.transferObjects([k],this._sdk.getSenderAddress())}else{let L=this.withdrawAsset({marketId:r,positionCapId:o,withdrawAmount:x.toString(),withdrawReserveArrayIndex:C[0].reserveArrayIndex.toString(),withdrawCoinType:t?M:b,baseToken:M,quoteToken:b,priceObjectId:z&&z[0],marginTradingContext:j},R),h=t?M:b,e=t?b:M,s=await this.findRouters(h,e,I(x),!0,[c]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:681 ~ LeverageModules ~ routers:",s,s?.route_obj.paths,h,e);let l=await this.routerSwap({router:s?.route_obj,inputCoin:L,slippage:g,txb:R});this.repay({txb:R,positionCapId:o,repayReserveArrayIndex:T[0].reserveArrayIndex.toString(),repayCoin:l,repayCoinType:t?b:M,repayAmount:I(0),marketId:r,marginTradingContext:j}),l&&R.transferObjects([l],this._sdk.getSenderAddress())}return await this.marginTradingContextConfirm(j,R),R};this.leveragePositionCloseWithCoin=async i=>{let{marketId:r,positionCapId:o,isQuote:t,slippage:u,swapClmmPool:p=""}=i,{baseToken:c,quoteToken:g,closeFeeRate:M}=await this.getLeverageMarketInfo(r),{deposits:b,borrows:S,isLong:C,originObligation:T}=await this.getLeveragePosition(o);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:847 ~ LeverageModules ~ borrows:",S);let D=new rt,O=t?g:c,B=C?c!==O:g!==O,{reserve:R}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(c,g),x=this.createMarginTradingContext({marketId:r,positionCapId:o,action:"close_position",coinType:t?g:c,amount:"0"},D);await this._sdk.SuiLendModule.refreshReservePrices(D,R);let q=T.borrows.length>0,j,W,$,z,m=I(0),y,A,P,L;if(q){let v=Ci(T.borrows[0],C?R[1]:R[0]),k=I(v.borrowedAmount.value.toString()).div(10**18).toDP(0,Ue.ROUND_UP).add(100),w=k,a=await this.getFlashLoanPool(S[0].reserve.coinType,w.toString());j=a.clmmPoolCoinTypeA,W=a.clmmPoolCoinTypeB,$=a.clmmPool,z=a.clmmFeeTier;let _=w.mul(z);m=w.add(_).toDP(0,Ue.ROUND_UP);let F=this.flashLoan({tx:D,amount:k,clmmPool:$,clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W,flashLoanCoin:S[0].reserve.coinType});A=F.balanceA,P=F.balanceB,L=F.receipt,y=F.isLoanA;let E=y?Y.fromBalance(A,y?j:W,D):Y.fromBalance(P,y?j:W,D);this.repay({txb:D,positionCapId:o,repayReserveArrayIndex:S[0].reserveArrayIndex.toString(),repayCoin:E,repayCoinType:y?j:W,repayAmount:I(0),marketId:r,marginTradingContext:x}),D.transferObjects([E],this._sdk.getSenderAddress())}let{lendingMarketType:h,lendingMarketId:e}=ne(this._sdk.sdkOptions.suilend),s=await this._sdk.SuiLendModule.getSuilendClient(e,h),l=[];for(let v=0;v<b.length;v++){let k=await this._sdk.PythConnection.getPriceFeedsUpdateData([b[v].reserve.priceIdentifier]);l=await s.pythClient.updatePriceFeeds(D,k,[b[v].reserve.priceIdentifier])}let f=this.withdrawAsset({marketId:r,positionCapId:o,withdrawAmount:Ai.toString(),withdrawReserveArrayIndex:b[0].reserveArrayIndex.toString(),withdrawCoinType:C?c:g,baseToken:c,quoteToken:g,priceObjectId:l&&l[0],marginTradingContext:x},D);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1217 ~ LeverageModules ~ deposits:",b);for(let v=1;v<b.length;v++){let k=this.withdrawAsset({marketId:r,positionCapId:o,withdrawAmount:Ai.toString(),withdrawReserveArrayIndex:b[v].reserveArrayIndex.toString(),withdrawCoinType:b[v].coinType,baseToken:c,quoteToken:g,priceObjectId:l&&l[v],marginTradingContext:x},D);D.transferObjects([k],this._sdk.getSenderAddress())}let d=b[0].depositedCtokenAmount.toString();if(q){let v=y?j!==b[0].reserve.coinType:W!==b[0].reserve.coinType,k;if(v){let a=await this.findRouters(C?c:g,y?j:W,m,!1,[p]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:902 ~ LeverageModules ~ isLong ? baseToken : quoteToken:",{from:C?c:g,to:y?j:W,amountIn:a?.amount_in.toString(),amountOut:m.toString()});let _=D.splitCoins(f,[a?.amount_in.toString()]);k=await this.routerSwap({router:a?.route_obj,inputCoin:_,slippage:u,txb:D}),d=I(b[0].depositedCtokenAmount.toString()).sub(a?.amount_in.toString()).toString()}else k=D.splitCoins(f,[m.toString()]),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:928 ~ LeverageModules ~ flashLoanAmountTotal.toString():",m.toString(),b[0].depositedCtokenAmount.toString()),d=I(b[0].depositedCtokenAmount.toString()).sub(m).toString();let w=y?P:A;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:921 ~ LeverageModules ~ isLoanA:",y),k&&this.repayFlashLoan({tx:D,clmmPool:$,repayBase:y?Y.intoBalance(k,j,D):w,repayQuote:y?w:Y.intoBalance(k,W,D),receipt:L,clmmPoolCoinTypeA:j,clmmPoolCoinTypeB:W})}if(B){let v=await this.findRouters(C?c:g,O,I(d),!0,[p]),k=await this.routerSwap({router:v?.route_obj,inputCoin:f,slippage:u,txb:D});k&&D.transferObjects([k],this._sdk.getSenderAddress())}else D.transferObjects([f],this._sdk.getSenderAddress());return this.leveragePositionClose({marketId:r,positionCapId:o,marginTradingContext:x},D),await this.marginTradingContextConfirm(x,D),D};this.leveragePositionClose=async(i,r)=>{let o=r||new rt,{marketId:t,positionCapId:u,marginTradingContext:p}=i,{leverage:c}=this._sdk.sdkOptions,{global_config_id:g,versioned:M}=ne(this._sdk.sdkOptions.leverage),{lendingMarketType:b,lendingMarketId:S}=ne(this._sdk.sdkOptions.suilend);return o.moveCall({target:`${c.published_at}::router::close_position`,typeArguments:[b],arguments:[p,o.object(g),o.object(S),o.object(t),o.object(u),o.object(pt),o.object(M)]}),o};this.leveragePositionRepay=async i=>{let{marketId:r,positionCapId:o,amount:t,isQuote:u,swapClmmPool:p="",slippage:c}=i,{baseToken:g,quoteToken:M}=await this.getLeverageMarketInfo(r),{borrows:b}=await this.getLeveragePosition(o),S=new rt,C=b[0].reserve.coinType,T=u?M!==C:g!==C,D,O=this.createMarginTradingContext({marketId:r,positionCapId:o,action:"repay_debt",coinType:u?M:g,amount:t.toString()},S);if(T){let B=await this.findRouters(u?M:g,C,t,!0,[p]),R=Y.buildCoinWithBalance(BigInt(t.toString()),u?M:g,S);D=await this.routerSwap({router:B?.route_obj,inputCoin:R,slippage:c,txb:S})}else D=Y.buildCoinWithBalance(BigInt(t.toString()),u?M:g,S);return this.repay({txb:S,positionCapId:o,repayReserveArrayIndex:b[0].reserveArrayIndex.toString(),repayCoin:D,repayCoinType:C,repayAmount:I(0),marketId:r,marginTradingContext:O}),S.transferObjects([D],this._sdk.getSenderAddress()),await this.marginTradingContextConfirm(O,S),S};this.positionTopUpCoken=async i=>{let{marketId:r,positionCapId:o,amount:t,isQuote:u,swapClmmPool:p="",slippage:c}=i,g=new rt,{baseToken:M,quoteToken:b}=await this.getLeverageMarketInfo(r),{isLong:S}=await this.getLeveragePosition(o),C=S&&u||!S&&!u,T=await this.createMarginTradingContext({marketId:r,positionCapId:o,action:"top_up_collateral",coinType:u?b:M,amount:t.toString()},g),D;if(C){let x=await this.findRouters(u?b:M,u?M:b,t,!0,[p]),q=Y.buildCoinWithBalance(BigInt(t.toString()),u?b:M,g);D=await this.routerSwap({router:x?.route_obj,inputCoin:q,slippage:c,txb:g})}else D=Y.buildCoinWithBalance(BigInt(t.toString()),u?b:M,g);let{baseReserveArrayIndex:O,quoteReserveArrayIndex:B,reserve:R}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(M,b);return await this.depositToLeveragePosition({isLong:S,marketId:r,positionCapId:o,depositReserveArrayIndex:S?O:B,inputCoin:D,marginTradingContext:T},g),await this.marginTradingContextConfirm(T,g),g};this.flashLoan=i=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:698 ~ LeverageModules ~ params:",i);let{clmm_pool:r}=this._sdk.ClmmSDK.sdkOptions,{global_config_id:o}=ne(this._sdk.ClmmSDK.sdkOptions.clmm_pool),{amount:t,clmmPool:u,clmmPoolCoinTypeA:p,clmmPoolCoinTypeB:c,flashLoanCoin:g,tx:M}=i,b=ao(p)===g;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:983 ~ LeverageModules ~ isLoanA:",b);let[S,C,T]=M.moveCall({target:`${r.published_at}::pool::flash_loan`,arguments:[M.object(o),M.object(u),M.pure.bool(b),M.pure.u64(t.toString())],typeArguments:[p,c]});return{balanceA:S,balanceB:C,receipt:T,isLoanA:b,loanCoinType:b?p:c}};this.repayFlashLoan=i=>{let{clmm_pool:r}=this._sdk.ClmmSDK.sdkOptions,{global_config_id:o}=ne(this._sdk.ClmmSDK.sdkOptions.clmm_pool),{tx:t,repayBase:u,repayQuote:p,receipt:c,clmmPool:g,clmmPoolCoinTypeA:M,clmmPoolCoinTypeB:b}=i;return console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:612 ~ LeverageModules ~ params:",i),t.moveCall({target:`${r.published_at}::pool::repay_flash_loan`,arguments:[t.object(o),t.object(g),u,p,c],typeArguments:[M,b]}),t};this.borrowAsset=(i,r)=>{let{reserveArrayIndex:o,borrowAmount:t,baseToken:u,quoteToken:p,lendingMarketId:c,isLong:g,marketId:M,positionCapId:b,positionCap:S,marginTradingContext:C}=i;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:677 ~ LeverageModules ~ params:",i);let{leverage:T}=this._sdk.sdkOptions,{lendingMarketType:D}=ne(this._sdk.sdkOptions.suilend),{global_config_id:O,versioned:B}=ne(this._sdk.sdkOptions.leverage);return Y.isSuiCoin(g?p:u)?r.moveCall({target:`${T.published_at}::router::borrow_sui`,typeArguments:[D],arguments:[C,r.object(O),r.object(c),r.object(M),b?r.object(b):S,r.pure.u64(o),r.pure.u64(t.toString()),r.object(Si),r.object(pt),r.object(B)]}):r.moveCall({target:`${T.published_at}::router::borrow_not_sui`,typeArguments:[D,g?p:u],arguments:[C,r.object(O),r.object(c),r.object(M),b?r.object(b):S,r.pure.u64(o),r.pure.u64(t.toString()),r.object(pt),r.object(B)]})};this.withdrawAsset=(i,r)=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:749 ~ LeverageModules ~ params:",i);let{leverage:o}=this._sdk.sdkOptions,{lendingMarketId:t,lendingMarketType:u}=ne(this._sdk.sdkOptions.suilend),{global_config_id:p,versioned:c}=ne(this._sdk.sdkOptions.leverage),{package_id:g}=this._sdk.sdkOptions.suilend,{marketId:M,withdrawAmount:b,withdrawReserveArrayIndex:S,withdrawCoinType:C,baseToken:T,quoteToken:D,priceObjectId:O,positionCapId:B,marginTradingContext:R}=i;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1596 ~ LeverageModules ~ withdrawAmount:",b);let[x]=r.moveCall({target:"0x1::option::none",typeArguments:[`${g}::lending_market::RateLimiterExemption<${u}, ${C}>`],arguments:[]});return Y.isSuiCoin(C)?r.moveCall({target:`${o.published_at}::router::withdraw_sui`,typeArguments:[u],arguments:[R,r.object(p),r.object(t),r.object(M),r.object(B),r.object(x),r.pure.u64(b),r.pure.u64(S),r.object(Si),r.object(pt),r.object(c)]}):r.moveCall({target:`${o.published_at}::router::withdraw_not_sui`,typeArguments:[u,C],arguments:[R,r.object(p),r.object(t),r.object(M),r.object(B),r.object(x),r.pure.u64(b),r.pure.u64(S),r.object(pt),r.object(c)]})};this.repay=i=>{console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1333 ~ LeverageModules ~ params:",i);let{leverage:r}=this._sdk.sdkOptions,{lendingMarketId:o,lendingMarketType:t}=ne(this._sdk.sdkOptions.suilend),{global_config_id:u,versioned:p}=ne(this._sdk.sdkOptions.leverage),{txb:c,repayAmount:g,repayCoinType:M,repayCoin:b,repayReserveArrayIndex:S,marketId:C,positionCapId:T,marginTradingContext:D}=i,O=c||new rt,B=b||Y.buildCoinWithBalance(BigInt(g.toString()),M,O);O.moveCall({target:`${r.published_at}::router::repay`,typeArguments:[t,M],arguments:[D,O.object(u),O.object(o),O.object(C),O.object(T),B,O.pure.u64(S.toString()),O.object(pt),O.object(p)]})};this.routerSwap=async i=>{let{slippage:r,txb:o,inputCoin:t,router:u}=i,p=o||new rt;if(u)return await this._sdk.AggregatorClient.routerSwap({router:u,inputCoin:t,slippage:r,txb:p})};this.calculatePositionDeposit=async i=>{let{isQuote:r,isLong:o,amount:t,swapClmmPool:u="",leverage:p,byAmountIn:c=!0,marketId:g}=i;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1514 ~ LeverageModules ~ amount:",t.toString());let{baseToken:M,quoteToken:b,openFeeRate:S}=await this.getLeverageMarketInfo(g),{reserve:C}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(M,b),T=C[0].mintDecimals,D=C[1].mintDecimals,O=t.toString();if(o&&r||!o&&!r){let s=o?b:M,l=o?M:b;O=(await this.findRouters(s,l,I(O),!0,[u]))?.amount_out.toString()}let{basePrice:R,quotePrice:x,flashAmount:q,flashLoanFee:j,flashLoanCoin:W,isFlashA:$,clmmPool:z,clmmFeeTier:m}=await this.calculateFlashLoanParams({isLong:o,leverage:p,baseToken:M,quoteToken:b,initDepositAmount:O,reserve:C,baseTokenDecimal:T,quoteTokenDecimal:D,openFeeRate:S});console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1533 ~ LeverageModules ~ flashAmount:",q.toString(),O);let y=await this.findRouters(o?b:M,o?M:b,I(q),!0,[u]),A=I(O).add(I(y?.amount_out.toString())).toDP(0,Ue.ROUND_DOWN).toString(),P=I(A).mul(o?R:x).toString(),L=I(q).add(j).toDP(0,Ue.ROUND_UP).toString(),h=I(L).mul(o?x:R).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1541 ~ LeverageModules ~ isQuote:",r);let e=r?I(t).div(10**(r?D:T)).mul(10**T).mul(p).div(R).toDP(0,Ue.ROUND_DOWN).toString():I(t).mul(p).toDP(0,Ue.ROUND_DOWN).toString();return{routers:y,leverageDepositAmount:e,quotePrice:x,basePrice:R,clmmFeeTier:m,flashLoanFee:j,flashLoanCoin:W,depositAmount:A,borrowAmount:L,depositAmountUSD:P,borrowAmountUSD:h,flashAmount:q,isFlashA:$,initDepositAmount:O}};this.calculatePositionWithdraw=async i=>{let{marketId:r,positionCapId:o,isLong:t,isQuote:u,swapClmmPool:p="",amount:c,leverage:g}=i,{baseToken:M,quoteToken:b}=await this.getLeverageMarketInfo(r),{deposits:S,borrows:C,originObligation:T}=await this.getLeveragePosition(o),{reserve:D,baseReserveMapInfo:O,quoteReserveMapInfo:B}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(M,b);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1951 ~ LeverageModules ~ reserve:",D);let R=D[0].mintDecimals,x=D[1].mintDecimals,q=t?O.cTokenExchangeRate:B.cTokenExchangeRate;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1955 ~ LeverageModules ~ ctokenExchangeRate:",q.toString());let j=await this._sdk.SuiLendModule.getLatestPriceFeeds(D,!0),W=j[yt(b)]?.price,$=j[yt(M)]?.price,z=I(0),m;t?(z=I(u?c.mul(W).toString():c.mul($).toString()).div(u?10**x:10**R),m=u?c.div($).div(10**x).mul(10**R).toString():c):(z=I(u?c.mul(W).toString():c.mul($).toString()).div(u?10**x:10**R),m=u?c:c.mul($).div(10**R).mul(10**x).toString()),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1973 ~ LeverageModules ~ withdrawAmount:",m);let y=I(S[0].depositedAmountUsd.toString()).sub(I(C[0].borrowedAmountUsd.toString())).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1887 ~ LeverageModules ~ availableWithdrawAmountUSD:",y);let A=I(y).div(t?$:W).mul(10**(t?R:x)).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1893 ~ LeverageModules ~ availableWithdrawAmount:",A);let P=z.div(I(y));console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1625 ~ LeverageModules ~ ratio:",P),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1899 ~ LeverageModules ~ borrows[0].borrowedAmount.toString():",C[0].borrowedAmount.toString()),m=I(m).mul(g).toDP(0,Ue.ROUND_DOWN).toString(),z=I(z).mul(g);let L=Ci(T.borrows[0],t?D[1]:D[0]),h=I(L.borrowedAmount.value.toString()).div(10**18).toDP(0,Ue.ROUND_UP),e=h.mul(P).toDP(0,Ue.ROUND_UP).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1901 ~ LeverageModules ~ repayAmount:",{repayAmount:e,ratio:P,withdrawAmount:m,borrowAmount:h,withdrawAmountUSD:z,quotePrice:W,basePrice:$});let{clmmPoolCoinTypeA:s,clmmPoolCoinTypeB:l,clmmFeeTier:f,clmmPool:d}=await this.getFlashLoanPool(C[0].reserve.coinType,e),v=await this.findRouters(S[0].reserve.coinType,C[0].reserve.coinType,I(e),!1,[p]),k=h.mul(I(1).sub(P)).toString(),w=I(k).mul(t?W:$).toString(),a=I(S[0].depositedAmount.toString()).mul(t?10**R:10**x).sub(I(m)).toString(),_=I(a).mul(t?$:W).toString(),F=I(e).mul(f).toString(),E=I(v?.amount_in.toString()).toString(),U=I(v?.amount_out.toString()).toString(),Z=I(P).gte(1);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1927 ~ LeverageModules ~ deposits[0].depositedAmount.toString():",{afterBorrowAmount:k,afterBorrowAmountUSD:w,afterDepositAmount:a,afterDepositAmountUSD:_});let G=I(m).div(q.toString()).toDP(0,Ue.ROUND_DOWN).toString();return console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1956 ~ LeverageModules ~ withdrawCtokenAmount:",G),{amountIn:E,amountOut:U,from:S[0].reserve.coinType,to:C[0].reserve.coinType,afterBorrowAmount:k,afterBorrowAmountUSD:w,afterDepositAmount:a,afterDepositAmountUSD:_,flashLoanFee:F,clmmFeeTier:f,withdrawAmount:m,withdrawCtokenAmount:G,repayAmount:e,ratio:P,isClose:Z,isLong:t,deposits:S,borrows:C,reserve:D,baseToken:M,quoteToken:b,clmmPool:d,clmmPoolCoinTypeA:s,clmmPoolCoinTypeB:l}};this.calculatePositionLeverage=async i=>{let{marketId:r,positionCapId:o,currentLeverage:t,targetLeverage:u,swapClmmPool:p=""}=i,c=I(u).sub(I(t)),g=c.gt(0),{baseToken:M,quoteToken:b}=await this.getLeverageMarketInfo(r),{borrows:S,deposits:C,isLong:T}=await this.getLeveragePosition(o);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1540 ~ LeverageModules ~ deposits:",C[0].reserve.config);let{reserve:D}=await this._sdk.SuiLendModule.getSuiLendReserveInfo(M,b),O=D[0].mintDecimals,B=D[1].mintDecimals,R=await this._sdk.SuiLendModule.getLatestPriceFeeds(D,!0),x=R[yt(b)]?.price,q=R[yt(M)]?.price,j=S.length>0,W=C[0].depositedAmountUsd.toString(),$=C[0].depositedAmount.toString(),z=j?I(S[0].borrowedAmount.toString()).mul(10**(T?B:O)):I(0),m=j?S[0].borrowedAmountUsd.toString():I(0),y=I(C[0].reserve.config.openLtvPct).div(100).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1552 ~ LeverageModules ~ openLTV:",y);let A=I(W).sub(I(m)).mul(y).toString();if(g){let P=I(W).div(t).mul(c).div(T?x:q).mul(10**(T?B:O)).toDP(0,Ue.ROUND_UP),L=P.gt(A),h=P;if(L){let e=T?b:M,s=T?M:b,l=await this.findRouters(e,s,h,!0,[p]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:2106 ~ LeverageModules ~ routers:",{routers:l,from:e,to:s});let f=I(l?.amount_out.toString()).toString(),d=I(f).mul(T?q:x).toString(),{clmmPoolCoinTypeA:v,clmmPoolCoinTypeB:k,clmmPool:w,clmmFeeTier:a}=await this.getFlashLoanPool(e,h.toString()),_=h.mul(a).toString(),F=T?v==b:k==M,U=I(h).add(_).toDP(0,Ue.ROUND_UP).toString(),Z=I(U).mul(T?x:q).toString(),G=I($).add(I(f).div(10**(T?O:B))).toString(),te=I(G).mul(T?q:x).toString(),Ze=I(z).add(I(U)).div(10**(T?B:O)).toString(),J=I(Ze).mul(T?x:q).toString();return{amountIn:l?.amount_in.toString(),amountOut:l?.amount_out.toString(),flashLoanFee:_,depositAmount:f,borrowAmount:U,depositAmountUSD:d,borrowAmountUSD:Z,flashLoanAmount:h,baseToken:M,quoteToken:b,reserve:D,deposits:C,borrows:S,clmmFeeTier:a,isFlashLoan:L,isLoanA:F,clmmPoolCoinTypeA:v,clmmPoolCoinTypeB:k,afterDepositAmount:G,afterBorrowAmount:Ze,afterDepositAmountUSD:te,afterBorrowAmountUSD:J}}else{let e=h.toString(),s=I(e).mul(T?x:q).toString(),l=await this.findRouters(T?b:M,T?M:b,I(e),!0,[p]),f=I(l?.amount_out.toString()).toString(),d=I(f).mul(T?q:x).toString(),v=$.toString(),k=I(v).mul(T?q:x).toString(),w=I(z).add(I(e)).div(10**(T?B:O)).toString(),a=I(w).mul(T?x:q).toString();return{amountIn:l?.amount_in.toString(),amountOut:l?.amount_out.toString(),depositAmount:f,borrowAmount:e,depositAmountUSD:d,borrowAmountUSD:s,flashLoanAmount:h,baseToken:M,quoteToken:b,reserve:D,deposits:C,borrows:S,isFlashLoan:L,afterDepositAmount:v,afterBorrowAmount:w,afterDepositAmountUSD:k,afterBorrowAmountUSD:a}}}else{let P=I(W).div(t);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1598 ~ LeverageModules ~ netValue:",P.toString());let L=P.mul(c.abs()).div(T?x:q).mul(10**(T?B:O)).toDP(0,Ue.ROUND_UP);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1596 ~ LeverageModules ~ repayAmount:",L.toString());let h=await this.findRouters(C[0].reserve.coinType,S[0].reserve.coinType,L,!1,[p]),e=h?.amount_in.toString(),s=I(W).mul(y).toString(),l=I(s).lt(I(e));if(console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:2223 ~ LeverageModules ~ isFlashLoan:",l),l){console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1606 ~ LeverageModules ~ withdrawCtokenAmount:",e);let f=L.toString(),d=I(f).mul(T?x:q).toString(),v=e,k=I(v).mul(T?q:x).toString(),{clmmPoolCoinTypeA:w,clmmPoolCoinTypeB:a,clmmPool:_,clmmFeeTier:F}=await this.getFlashLoanPool(S[0].reserve.coinType,f.toString()),E=I($).sub(I(v).div(10**(T?O:B))).toString(),U=I(E).mul(T?q:x).toString(),Z=I(z).sub(I(f)).div(10**(T?B:O)).toString(),G=I(Z).mul(T?x:q).toString();return{amountIn:h?.amount_in.toString(),amountOut:h?.amount_out.toString(),depositAmount:v,borrowAmount:f,depositAmountUSD:k,borrowAmountUSD:d,baseToken:M,quoteToken:b,reserve:D,deposits:C,borrows:S,clmmFeeTier:F,withdrawCtokenAmount:e,isFlashLoan:l,clmmPoolCoinTypeA:w,clmmPoolCoinTypeB:a,afterDepositAmount:E,afterBorrowAmount:Z,afterDepositAmountUSD:U,afterBorrowAmountUSD:G}}else{let d=(await this.findRouters(C[0].reserve.coinType,S[0].reserve.coinType,e,!0,[p]))?.amount_out.toString(),v=I(d).mul(T?x:q).toString(),k=e.toString(),w=I(k).mul(T?q:x).toString(),a=I($).sub(I(k).div(10**(T?O:B))).toString(),_=I(a).mul(T?q:x).toString(),F=I(z).sub(I(d)).div(10**(T?B:O)).toString(),E=I(F).mul(T?x:q).toString();return{amountIn:h?.amount_in.toString(),amountOut:h?.amount_out.toString(),depositAmount:k,borrowAmount:d,depositAmountUSD:w,borrowAmountUSD:v,baseToken:M,quoteToken:b,reserve:D,deposits:C,borrows:S,withdrawCtokenAmount:e,isFlashLoan:l,afterDepositAmount:a,afterBorrowAmount:F,afterDepositAmountUSD:_,afterBorrowAmountUSD:E}}}};this.getFlashLoanPool=async(i,r)=>{let o=await fetch(`https://api-sui.cetus.zone/v3/sui/margin_trading/pools?coin_type=${i}&min_amount=${r}`);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:2151 ~ LeverageModules ~ res:",o);let{data:t}=await o.json();return t&&t.length>0?{clmmPoolCoinTypeA:t[0].coin_type_a,clmmPoolCoinTypeB:t[0].coin_type_b,clmmPool:t[0].pool_id,clmmFeeTier:I(t[0].fee_rate).div(1e6).toString()}:{clmmPoolCoinTypeA:"",clmmPoolCoinTypeB:"",clmmPool:"",clmmFeeTier:""}};this.createMarginTradingContext=(i,r)=>{let{published_at:o}=this._sdk.sdkOptions.leverage,t=mt(),{marketId:u,positionCapId:p,action:c,coinType:g,amount:M}=i;return r.moveCall({target:`${o}::router::create_margin_trading_context`,arguments:[r.pure.string(t),r.object(u),r.object(p),r.pure.string(c),r.pure.string(g),r.pure.u64(M)],typeArguments:[]})};this.marginTradingContextConfirm=(i,r)=>{let{published_at:o}=this._sdk.sdkOptions.leverage;return r.moveCall({target:`${o}::router::confirm`,arguments:[i],typeArguments:[]}),r};this._sdk=i}async handleSwapLogic(i){let{isLong:r,isQuote:o,amount:t,baseToken:u,quoteToken:p,swapClmmPool:c="",slippage:g,tx:M}=i,b=r&&o||!r&&!o;console.log("\u{1F680}\u{1F680}\u{1F680} ~ LeverageModules ~ hasSwap:",b);let S,C,T=t.toString();if(b){let D=r&&o?p:u,O=r&&o?u:p,B=await this.findRouters(D,O,t,!0,[c]);if(T=B?.amount_out.toString(),B){let R=Y.buildCoinWithBalance(BigInt(t.toString()),D,M);C=await this.routerSwap({router:B.route_obj,slippage:g,inputCoin:R,txb:M}),S=C}}else S=Y.buildCoinWithBalance(BigInt(t.toString()),o?p:u,M);return{hasSwap:b,inputCoin:S,initDepositAmount:T}}async handleDebtSwapLogic(i){let{isLong:r,flashAmount:o,baseToken:t,quoteToken:u,swapClmmPool:p="",isFlashA:c,balanceA:g,balanceB:M,slippage:b,clmmPoolCoinTypeA:S,clmmPoolCoinTypeB:C,tx:T}=i,D=r?u:t,O=r?t:u,B=await this.findRouters(D,O,I(o),!0,[p]);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:311 ~ LeverageModules ~ debtRouters:",{debtRouters:B,debtFrom:D,debtTo:O,clmmPoolCoinTypeA:S,clmmPoolCoinTypeB:C});let R=I(B?.amount_out||0),x;return B&&(x=await this.routerSwap({router:B.route_obj,inputCoin:c?Y.fromBalance(g,S,T):Y.fromBalance(M,C,T),slippage:b,txb:T})),{debtFrom:D,debtTo:O,debtRouters:B,debtSwapAmountOut:R,debtSwapOutCoin:x}}async calculateFlashLoanParams(i){let{isLong:r,leverage:o,baseToken:t,quoteToken:u,initDepositAmount:p,reserve:c,baseTokenDecimal:g,quoteTokenDecimal:M,openFeeRate:b}=i;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1403 ~ LeverageModules ~ calculateFlashLoanParams ~ params:",i),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1079 ~ LeverageModules ~ calculateFlashLoanParams ~ initDepositAmount:",p);let S=await this._sdk.SuiLendModule.getLatestPriceFeeds(c);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:587 ~ LeverageModules ~ calculateFlashLoanParams ~ priceUpdateData:",S,u,t);let C=S[yt(u)]?.price,T=S[yt(t)]?.price,D=r?I(T).div(I(C)).toString():I(C).div(I(T)).toString();console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1057 ~ LeverageModules ~ calculateFlashLoanParams ~ rate:",{rate:D}),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1424 ~ LeverageModules ~ calculateFlashLoanParams ~ initDepositAmount:",p);let O=I(p).mul(1-b).mul(o-1).mul(D).div(10**(r?g:M)).mul(10**(r?M:g)).toDP(0,Ue.ROUND_UP).toString(),B=r?u:t;console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1457 ~ LeverageModules ~ flashLoanCoin:",B),console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1457 ~ LeverageModules ~ flashLoanCoin:",B);let{clmmPoolCoinTypeA:R,clmmFeeTier:x,clmmPool:q,clmmPoolCoinTypeB:j}=await this.getFlashLoanPool(B,O);console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1094 ~ LeverageModules ~ calculateFlashLoanParams ~ flashAmount:",O,x,B);let W=R===B,$=I(O).mul(x).toString();return{flashAmount:O,rate:D,quotePrice:C,basePrice:T,flashLoanCoin:B,isFlashA:W,flashLoanFee:$,clmmPool:q,clmmPoolCoinTypeA:R,clmmPoolCoinTypeB:j,clmmFeeTier:x}}createLeveragePosition(i){console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:1493 ~ LeverageModules ~ createLeveragePosition ~ params:",i);let{lendingMarketId:r,lendingMarketType:o}=ne(this._sdk.sdkOptions.suilend),{global_config_id:t,versioned:u}=ne(this._sdk.sdkOptions.leverage),p=this._sdk.sdkOptions.leverage,{leverage:c,marketId:g,baseToken:M,quoteToken:b,isLong:S,initDepositAmount:C,tx:T,initCoinType:D}=i,O=mt(),[B,R]=T.moveCall({target:`${p.published_at}::router::open_position`,arguments:[T.object(t),T.object(g),T.object(r),T.pure.u64(C),T.pure.string(D),T.pure.u64(I(c).mul(1e4).toString()),T.pure.string(O),T.object(pt),T.object(u)],typeArguments:[o,S?M:b]});return{positionCap:B,marginTradingContext:R}}async depositToLeveragePosition(i,r){console.log("\u{1F680}\u{1F680}\u{1F680} ~ leverageModules.ts:651 ~ LeverageModules ~ depositToLeveragePosition ~ params:",i);let{leverage:o}=this._sdk.sdkOptions,{lendingMarketType:t,lendingMarketId:u}=ne(this._sdk.sdkOptions.suilend),{isLong:p,marketId:c,positionCapId:g,positionCap:M,depositReserveArrayIndex:b,inputCoin:S,marginTradingContext:C}=i,{global_config_id:T,versioned:D}=ne(this._sdk.sdkOptions.leverage),{baseToken:O,quoteToken:B}=await this.getLeverageMarketInfo(c);return r.moveCall({target:`${o.published_at}::router::deposit`,typeArguments:[t,p?O:B],arguments:[C,r.object(T),r.object(u),r.object(c),g?r.object(g):M,S,r.pure.u64(b),r.object(pt),r.object(D)]}),r}async findRouters(i,r,o,t,u){let{providers:p}=this._sdk.sdkOptions;try{let c={from:i,target:r,amount:new Dr.default(o.toFixed(0).toString()),byAmountIn:t,depth:3,providers:p},g=await this._sdk.AggregatorClient.findRouters(c);if(g?.error?.code===10001)return{...g,is_exceed:g.insufficientLiquidity};if(g?.insufficientLiquidity)return{...g,is_exceed:g.insufficientLiquidity};if(!g?.paths||g?.paths?.length===0)throw Error("Aggregator no router");return{amount_in:g.amountIn.toString(),amount_out:g.amountOut.toString(),is_exceed:g.insufficientLiquidity,route_obj:g,byAmountIn:!0,originRes:g}}catch{try{if(u){let g=await this._sdk.AggregatorClient.swapInPools({from:i,target:r,amount:new Dr.default(o.toFixed(0).toString()),byAmountIn:t,pools:u});return g?{amount_in:g.routeData.amountIn.toString(),amount_out:g.routeData.amountOut.toString(),is_exceed:g.isExceed,route_obj:g.routeData,byAmountIn:!0,originRes:g}:null}return null}catch{return null}}}};import{AggregatorClient as uo,Env as Li}from"@cetusprotocol/aggregator-sdk";import{normalizeSuiAddress as fo}from"@mysten/sui/utils";import{SuiClient as co}from"@mysten/sui/client";import{CetusClmmSDK as ho}from"@cetusprotocol/sui-clmm-sdk";import{SuiPriceServiceConnection as po}from"@pythnetwork/pyth-sui-js";import{CLOCK_ADDRESS as kt,getPackagerConfigs as Qe}from"@cetusprotocol/common-sdk";import{Transaction as Ve}from"@mysten/sui/transactions";var Zt=class{constructor(i){this.queryGlobalPermissions=async()=>{let i=this._sdk.sdkOptions.leverage,{global_config_id:r}=Qe(i),o=await this._sdk.FullClient.getObject({id:r,options:{showContent:!0}});if(o){let t=o.data.content.fields.permissions.toString(2),{openPermissionsPause:u,closePermissionsPause:p,depositPermissionsPause:c,withdrawPermissionsPause:g,borrowPermissionsPause:M,repayPermissionsPause:b}=xr(t);return{openPermissionsPause:u,closePermissionsPause:p,depositPermissionsPause:c,withdrawPermissionsPause:g,borrowPermissionsPause:M,repayPermissionsPause:b}}};this.updateGlobalPermissions=async i=>{let r=new Ve;return this.updateGlobalOpenPositionPermissions(i,r),this.updateGlobalClosePositionPermissions(i,r),this.updateGlobalDepositPermissions(i,r),this.updateGlobalBorrowPermissions(i,r),this.updateGlobalWithdrawPermissions(i,r),this.updateGlobalRepayPermissions(i,r),r};this.updateGlobalOpenPositionPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_open_position_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateGlobalClosePositionPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_close_position_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateGlobalDepositPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_deposit_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateGlobalBorrowPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_borrow_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateGlobalWithdrawPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_withdraw_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateGlobalRepayPermissions=async(i,r)=>{let o=this._sdk.sdkOptions.leverage,{global_config_id:t,versioned:u,admin_cap_id:p}=Qe(o),c=r||new Ve;return c.moveCall({target:`${o.published_at}::config::update_global_repay_permissions`,arguments:[c.object(p),c.object(t),c.pure.bool(i),c.object(kt),c.object(u)],typeArguments:[]}),c};this.updateMarketCreatePositionPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_open_position_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketClosePositionPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_close_position_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketDepositPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_deposit_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketWithdrawPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_withdraw_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketBorrowPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_borrow_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketRepayPermissions=async(i,r,o)=>{let t=this._sdk.sdkOptions.leverage,{versioned:u,admin_cap_id:p}=Qe(t),c=o||new Ve;return c.moveCall({target:`${t.published_at}::market::update_market_repay_permission`,arguments:[c.object(p),c.object(i),c.pure.bool(r),c.object(u)],typeArguments:[]}),c};this.updateMarketPermissions=async(i,r,o)=>{let t=o||new Ve;return this.updateMarketCreatePositionPermissions(i,r,t),this.updateMarketClosePositionPermissions(i,r,t),this.updateMarketDepositPermissions(i,r,t),this.updateMarketWithdrawPermissions(i,r,t),this.updateMarketBorrowPermissions(i,r,t),this.updateMarketRepayPermissions(i,r,t),t};this._sdk=i}};import{getPackagerConfigs as Wt}from"@cetusprotocol/common-sdk";import{Transaction as Ct}from"@mysten/sui/transactions";var zt=class{constructor(i){this.createMarket=async i=>{let{package_id:r}=this._sdk.sdkOptions.leverage,{versioned:o,admin_cap_id:t,markets:u,global_config_id:p}=Wt(this._sdk.sdkOptions.leverage),c=new Ct;return c.moveCall({target:`${r}::market::create_market`,arguments:[c.object(t),c.object(p),c.object(u),c.pure.u64(i.maxLongLeverage),c.pure.u64(i.maxShortLeverage),c.pure.u64(i.openFeeRate),c.pure.u64(i.closeFeeRate),c.object(o)],typeArguments:[i.baseToken,i.quoteToken]}),c};this.updateMarketMaxLeverage=async i=>{let{package_id:r}=this._sdk.sdkOptions.leverage,{versioned:o,admin_cap_id:t}=Wt(this._sdk.sdkOptions.leverage),{marketId:u,maxLongLeverage:p,maxShortLeverage:c}=i,g=new Ct;return g.moveCall({target:`${r}::market::set_max_leverage`,arguments:[g.object(t),g.object(u),g.pure.u64(p),g.pure.u64(c),g.object(o)],typeArguments:[]}),g};this.updateMarketFeeRate=async i=>{let{package_id:r}=this._sdk.sdkOptions.leverage,{versioned:o,admin_cap_id:t}=Wt(this._sdk.sdkOptions.leverage),{marketId:u,openFeeRate:p,closeFeeRate:c}=i,g=new Ct;return g.moveCall({target:`${r}::market::set_fee_rate`,arguments:[g.object(t),g.object(u),g.pure.u64(p),g.pure.u64(c),g.object(o)],typeArguments:[]}),g};this.claimMarketFees=async(i,r)=>{let{package_id:o}=this._sdk.sdkOptions.leverage,{versioned:t,admin_cap_id:u}=Wt(this._sdk.sdkOptions.leverage),p=r||new Ct,{baseToken:c,quoteToken:g}=await this._sdk.LeverageModules.getLeverageMarketInfo(i);return p.moveCall({target:`${o}::market::claim_fees`,arguments:[p.object(u),p.object(i),p.object(t)],typeArguments:[c,g]}),p};this.claimAllMarketFees=async i=>{let r=new Ct;for(let o=0;o<i.length;o++){let t=i[o];this.claimMarketFees(t,r)}return r};this._sdk=i}};var Kt=class n extends lo{constructor(i,r){super(i),this._suilendModule=new Dt(this),this._leverageModules=new $t(this),this._permissionModules=new Zt(this),this._marketModules=new zt(this),this._aggregatorClient=new uo({signer:fo("0x0"),client:i.sui_client||new co({url:i.full_rpc_url}),env:i.env==="testnet"?Li.Testnet:Li.Mainnet,pythUrls:i.pyth_urls}),this._clmmSDK=r||ho.createSDK({env:i.env,full_rpc_url:i.full_rpc_url}),this._pythConnection=new po("https://hermes.pyth.network",{timeout:30*1e3})}get SuiLendModule(){return this._suilendModule}get LeverageModules(){return this._leverageModules}get AggregatorClient(){return this._aggregatorClient}get ClmmSDK(){return this._clmmSDK}get PythConnection(){return this._pythConnection}get PermissionModules(){return this._permissionModules}get MarketModules(){return this._marketModules}static createSDK(i){return n.createCustomSDK({...Vr,...i})}static createCustomSDK(i){return new n(i)}};var Ks=Kt;export{Kt as CetusLeverageSDK,qo as appendExtraRewards,wr as calculateBorrowAprPercent,ei as calculateDepositAprPercent,Yr as calculateUtilizationPercent,Ks as default,Wo as formatLtvPercentValue,ti as getPriceWithFormattedDecimals,$o as getRewardsAprMultiplier,jo as getUpdatedReserve,Vr as leverage_mainnet,Tr as mergePositionData,vr as oraclePriceMultiplierDecimal,Zo as updateRewardsWithMultiplier,Pr as wrapMarketInfo,xr as wrapMarketPermissions,Lr as wrapPosition};
|
|
2
2
|
/*! Bundled license information:
|
|
3
3
|
|
|
4
4
|
decimal.js/decimal.mjs:
|