@cetusprotocol/dlmm-zap-sdk 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- var Ni=Object.create;var Ai=Object.defineProperty;var Wi=Object.getOwnPropertyDescriptor;var Ui=Object.getOwnPropertyNames;var zi=Object.getPrototypeOf,Hi=Object.prototype.hasOwnProperty;var Vi=(b=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(b,{get:(m,d)=>(typeof require<"u"?require:m)[d]}):b)(function(b){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+b+'" is not supported')});var Ji=(b,m)=>()=>(m||b((m={exports:{}}).exports,m),m.exports);var Gi=(b,m,d,g)=>{if(m&&typeof m=="object"||typeof m=="function")for(let f of Ui(m))!Hi.call(b,f)&&f!==d&&Ai(b,f,{get:()=>m[f],enumerable:!(g=Wi(m,f))||g.enumerable});return b};var Di=(b,m,d)=>(d=b!=null?Ni(zi(b)):{},Gi(m||!b||!b.__esModule?Ai(d,"default",{value:b,enumerable:!0}):d,b));var ni=Ji((Oi,ei)=>{"use strict";(function(b,m){"use strict";function d(h,t){if(!h)throw new Error(t||"Assertion failed")}function g(h,t){h.super_=t;var r=function(){};r.prototype=t.prototype,h.prototype=new r,h.prototype.constructor=h}function f(h,t,r){if(f.isBN(h))return h;this.negative=0,this.words=null,this.length=0,this.red=null,h!==null&&((t==="le"||t==="be")&&(r=t,t=10),this._init(h||0,t||10,r||"be"))}typeof b=="object"?b.exports=f:m.BN=f,f.BN=f,f.wordSize=26;var S;try{typeof window<"u"&&typeof window.Buffer<"u"?S=window.Buffer:S=Vi("buffer").Buffer}catch{}f.isBN=function(t){return t instanceof f?!0:t!==null&&typeof t=="object"&&t.constructor.wordSize===f.wordSize&&Array.isArray(t.words)},f.max=function(t,r){return t.cmp(r)>0?t:r},f.min=function(t,r){return t.cmp(r)<0?t:r},f.prototype._init=function(t,r,e){if(typeof t=="number")return this._initNumber(t,r,e);if(typeof t=="object")return this._initArray(t,r,e);r==="hex"&&(r=16),d(r===(r|0)&&r>=2&&r<=36),t=t.toString().replace(/\s+/g,"");var n=0;t[0]==="-"&&(n++,this.negative=1),n<t.length&&(r===16?this._parseHex(t,n,e):(this._parseBase(t,r,n),e==="le"&&this._initArray(this.toArray(),r,e)))},f.prototype._initNumber=function(t,r,e){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[t&67108863],this.length=1):t<4503599627370496?(this.words=[t&67108863,t/67108864&67108863],this.length=2):(d(t<9007199254740992),this.words=[t&67108863,t/67108864&67108863,1],this.length=3),e==="le"&&this._initArray(this.toArray(),r,e)},f.prototype._initArray=function(t,r,e){if(d(typeof t.length=="number"),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var s,l,u=0;if(e==="be")for(n=t.length-1,s=0;n>=0;n-=3)l=t[n]|t[n-1]<<8|t[n-2]<<16,this.words[s]|=l<<u&67108863,this.words[s+1]=l>>>26-u&67108863,u+=24,u>=26&&(u-=26,s++);else if(e==="le")for(n=0,s=0;n<t.length;n+=3)l=t[n]|t[n+1]<<8|t[n+2]<<16,this.words[s]|=l<<u&67108863,this.words[s+1]=l>>>26-u&67108863,u+=24,u>=26&&(u-=26,s++);return this._strip()};function M(h,t){var r=h.charCodeAt(t);if(r>=48&&r<=57)return r-48;if(r>=65&&r<=70)return r-55;if(r>=97&&r<=102)return r-87;d(!1,"Invalid character in "+h)}function _(h,t,r){var e=M(h,r);return r-1>=t&&(e|=M(h,r-1)<<4),e}f.prototype._parseHex=function(t,r,e){this.length=Math.ceil((t.length-r)/6),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var s=0,l=0,u;if(e==="be")for(n=t.length-1;n>=r;n-=2)u=_(t,r,n)<<s,this.words[l]|=u&67108863,s>=18?(s-=18,l+=1,this.words[l]|=u>>>26):s+=8;else{var o=t.length-r;for(n=o%2===0?r+1:r;n<t.length;n+=2)u=_(t,r,n)<<s,this.words[l]|=u&67108863,s>=18?(s-=18,l+=1,this.words[l]|=u>>>26):s+=8}this._strip()};function w(h,t,r,e){for(var n=0,s=0,l=Math.min(h.length,r),u=t;u<l;u++){var o=h.charCodeAt(u)-48;n*=e,o>=49?s=o-49+10:o>=17?s=o-17+10:s=o,d(o>=0&&s<e,"Invalid character"),n+=s}return n}f.prototype._parseBase=function(t,r,e){this.words=[0],this.length=1;for(var n=0,s=1;s<=67108863;s*=r)n++;n--,s=s/r|0;for(var l=t.length-e,u=l%n,o=Math.min(l,l-u)+e,i=0,a=e;a<o;a+=n)i=w(t,a,a+n,r),this.imuln(s),this.words[0]+i<67108864?this.words[0]+=i:this._iaddn(i);if(u!==0){var p=1;for(i=w(t,a,t.length,r),a=0;a<u;a++)p*=r;this.imuln(p),this.words[0]+i<67108864?this.words[0]+=i:this._iaddn(i)}this._strip()},f.prototype.copy=function(t){t.words=new Array(this.length);for(var r=0;r<this.length;r++)t.words[r]=this.words[r];t.length=this.length,t.negative=this.negative,t.red=this.red};function A(h,t){h.words=t.words,h.length=t.length,h.negative=t.negative,h.red=t.red}if(f.prototype._move=function(t){A(t,this)},f.prototype.clone=function(){var t=new f(null);return this.copy(t),t},f.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},f.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},f.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{f.prototype[Symbol.for("nodejs.util.inspect.custom")]=L}catch{f.prototype.inspect=L}else f.prototype.inspect=L;function L(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var T=["","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],D=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];f.prototype.toString=function(t,r){t=t||10,r=r|0||1;var e;if(t===16||t==="hex"){e="";for(var n=0,s=0,l=0;l<this.length;l++){var u=this.words[l],o=((u<<n|s)&16777215).toString(16);s=u>>>24-n&16777215,n+=2,n>=26&&(n-=26,l--),s!==0||l!==this.length-1?e=T[6-o.length]+o+e:e=o+e}for(s!==0&&(e=s.toString(16)+e);e.length%r!==0;)e="0"+e;return this.negative!==0&&(e="-"+e),e}if(t===(t|0)&&t>=2&&t<=36){var i=C[t],a=D[t];e="";var p=this.clone();for(p.negative=0;!p.isZero();){var c=p.modrn(a).toString(t);p=p.idivn(a),p.isZero()?e=c+e:e=T[i-c.length]+c+e}for(this.isZero()&&(e="0"+e);e.length%r!==0;)e="0"+e;return this.negative!==0&&(e="-"+e),e}d(!1,"Base should be between 2 and 36")},f.prototype.toNumber=function(){var t=this.words[0];return this.length===2?t+=this.words[1]*67108864:this.length===3&&this.words[2]===1?t+=4503599627370496+this.words[1]*67108864:this.length>2&&d(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-t:t},f.prototype.toJSON=function(){return this.toString(16,2)},S&&(f.prototype.toBuffer=function(t,r){return this.toArrayLike(S,t,r)}),f.prototype.toArray=function(t,r){return this.toArrayLike(Array,t,r)};var q=function(t,r){return t.allocUnsafe?t.allocUnsafe(r):new t(r)};f.prototype.toArrayLike=function(t,r,e){this._strip();var n=this.byteLength(),s=e||Math.max(1,n);d(n<=s,"byte array longer than desired length"),d(s>0,"Requested array length <= 0");var l=q(t,s),u=r==="le"?"LE":"BE";return this["_toArrayLike"+u](l,n),l},f.prototype._toArrayLikeLE=function(t,r){for(var e=0,n=0,s=0,l=0;s<this.length;s++){var u=this.words[s]<<l|n;t[e++]=u&255,e<t.length&&(t[e++]=u>>8&255),e<t.length&&(t[e++]=u>>16&255),l===6?(e<t.length&&(t[e++]=u>>24&255),n=0,l=0):(n=u>>>24,l+=2)}if(e<t.length)for(t[e++]=n;e<t.length;)t[e++]=0},f.prototype._toArrayLikeBE=function(t,r){for(var e=t.length-1,n=0,s=0,l=0;s<this.length;s++){var u=this.words[s]<<l|n;t[e--]=u&255,e>=0&&(t[e--]=u>>8&255),e>=0&&(t[e--]=u>>16&255),l===6?(e>=0&&(t[e--]=u>>24&255),n=0,l=0):(n=u>>>24,l+=2)}if(e>=0)for(t[e--]=n;e>=0;)t[e--]=0},Math.clz32?f.prototype._countBits=function(t){return 32-Math.clz32(t)}:f.prototype._countBits=function(t){var r=t,e=0;return r>=4096&&(e+=13,r>>>=13),r>=64&&(e+=7,r>>>=7),r>=8&&(e+=4,r>>>=4),r>=2&&(e+=2,r>>>=2),e+r},f.prototype._zeroBits=function(t){if(t===0)return 26;var r=t,e=0;return(r&8191)===0&&(e+=13,r>>>=13),(r&127)===0&&(e+=7,r>>>=7),(r&15)===0&&(e+=4,r>>>=4),(r&3)===0&&(e+=2,r>>>=2),(r&1)===0&&e++,e},f.prototype.bitLength=function(){var t=this.words[this.length-1],r=this._countBits(t);return(this.length-1)*26+r};function W(h){for(var t=new Array(h.bitLength()),r=0;r<t.length;r++){var e=r/26|0,n=r%26;t[r]=h.words[e]>>>n&1}return t}f.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,r=0;r<this.length;r++){var e=this._zeroBits(this.words[r]);if(t+=e,e!==26)break}return t},f.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},f.prototype.toTwos=function(t){return this.negative!==0?this.abs().inotn(t).iaddn(1):this.clone()},f.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},f.prototype.isNeg=function(){return this.negative!==0},f.prototype.neg=function(){return this.clone().ineg()},f.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},f.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var r=0;r<t.length;r++)this.words[r]=this.words[r]|t.words[r];return this._strip()},f.prototype.ior=function(t){return d((this.negative|t.negative)===0),this.iuor(t)},f.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},f.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},f.prototype.iuand=function(t){var r;this.length>t.length?r=t:r=this;for(var e=0;e<r.length;e++)this.words[e]=this.words[e]&t.words[e];return this.length=r.length,this._strip()},f.prototype.iand=function(t){return d((this.negative|t.negative)===0),this.iuand(t)},f.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},f.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},f.prototype.iuxor=function(t){var r,e;this.length>t.length?(r=this,e=t):(r=t,e=this);for(var n=0;n<e.length;n++)this.words[n]=r.words[n]^e.words[n];if(this!==r)for(;n<r.length;n++)this.words[n]=r.words[n];return this.length=r.length,this._strip()},f.prototype.ixor=function(t){return d((this.negative|t.negative)===0),this.iuxor(t)},f.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},f.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},f.prototype.inotn=function(t){d(typeof t=="number"&&t>=0);var r=Math.ceil(t/26)|0,e=t%26;this._expand(r),e>0&&r--;for(var n=0;n<r;n++)this.words[n]=~this.words[n]&67108863;return e>0&&(this.words[n]=~this.words[n]&67108863>>26-e),this._strip()},f.prototype.notn=function(t){return this.clone().inotn(t)},f.prototype.setn=function(t,r){d(typeof t=="number"&&t>=0);var e=t/26|0,n=t%26;return this._expand(e+1),r?this.words[e]=this.words[e]|1<<n:this.words[e]=this.words[e]&~(1<<n),this._strip()},f.prototype.iadd=function(t){var r;if(this.negative!==0&&t.negative===0)return this.negative=0,r=this.isub(t),this.negative^=1,this._normSign();if(this.negative===0&&t.negative!==0)return t.negative=0,r=this.isub(t),t.negative=1,r._normSign();var e,n;this.length>t.length?(e=this,n=t):(e=t,n=this);for(var s=0,l=0;l<n.length;l++)r=(e.words[l]|0)+(n.words[l]|0)+s,this.words[l]=r&67108863,s=r>>>26;for(;s!==0&&l<e.length;l++)r=(e.words[l]|0)+s,this.words[l]=r&67108863,s=r>>>26;if(this.length=e.length,s!==0)this.words[this.length]=s,this.length++;else if(e!==this)for(;l<e.length;l++)this.words[l]=e.words[l];return this},f.prototype.add=function(t){var r;return t.negative!==0&&this.negative===0?(t.negative=0,r=this.sub(t),t.negative^=1,r):t.negative===0&&this.negative!==0?(this.negative=0,r=t.sub(this),this.negative=1,r):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},f.prototype.isub=function(t){if(t.negative!==0){t.negative=0;var r=this.iadd(t);return t.negative=1,r._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var e=this.cmp(t);if(e===0)return this.negative=0,this.length=1,this.words[0]=0,this;var n,s;e>0?(n=this,s=t):(n=t,s=this);for(var l=0,u=0;u<s.length;u++)r=(n.words[u]|0)-(s.words[u]|0)+l,l=r>>26,this.words[u]=r&67108863;for(;l!==0&&u<n.length;u++)r=(n.words[u]|0)+l,l=r>>26,this.words[u]=r&67108863;if(l===0&&u<n.length&&n!==this)for(;u<n.length;u++)this.words[u]=n.words[u];return this.length=Math.max(this.length,u),n!==this&&(this.negative=1),this._strip()},f.prototype.sub=function(t){return this.clone().isub(t)};function U(h,t,r){r.negative=t.negative^h.negative;var e=h.length+t.length|0;r.length=e,e=e-1|0;var n=h.words[0]|0,s=t.words[0]|0,l=n*s,u=l&67108863,o=l/67108864|0;r.words[0]=u;for(var i=1;i<e;i++){for(var a=o>>>26,p=o&67108863,c=Math.min(i,t.length-1),v=Math.max(0,i-h.length+1);v<=c;v++){var y=i-v|0;n=h.words[y]|0,s=t.words[v]|0,l=n*s+p,a+=l/67108864|0,p=l&67108863}r.words[i]=p|0,o=a|0}return o!==0?r.words[i]=o|0:r.length--,r._strip()}var Z=function(t,r,e){var n=t.words,s=r.words,l=e.words,u=0,o,i,a,p=n[0]|0,c=p&8191,v=p>>>13,y=n[1]|0,k=y&8191,F=y>>>13,kt=n[2]|0,K=kt&8191,N=kt>>>13,hi=n[3]|0,z=hi&8191,H=hi>>>13,fi=n[4]|0,V=fi&8191,J=fi>>>13,li=n[5]|0,G=li&8191,Q=li>>>13,ui=n[6]|0,X=ui&8191,Y=ui>>>13,di=n[7]|0,$=di&8191,j=di>>>13,mi=n[8]|0,tt=mi&8191,it=mi>>>13,pi=n[9]|0,rt=pi&8191,et=pi>>>13,ci=s[0]|0,nt=ci&8191,ot=ci>>>13,vi=s[1]|0,st=vi&8191,at=vi>>>13,gi=s[2]|0,ht=gi&8191,ft=gi>>>13,_i=s[3]|0,lt=_i&8191,ut=_i>>>13,Mi=s[4]|0,dt=Mi&8191,mt=Mi>>>13,wi=s[5]|0,pt=wi&8191,ct=wi>>>13,yi=s[6]|0,vt=yi&8191,gt=yi>>>13,bi=s[7]|0,_t=bi&8191,Mt=bi>>>13,xi=s[8]|0,wt=xi&8191,yt=xi>>>13,Si=s[9]|0,bt=Si&8191,xt=Si>>>13;e.negative=t.negative^r.negative,e.length=19,o=Math.imul(c,nt),i=Math.imul(c,ot),i=i+Math.imul(v,nt)|0,a=Math.imul(v,ot);var Ft=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Ft>>>26)|0,Ft&=67108863,o=Math.imul(k,nt),i=Math.imul(k,ot),i=i+Math.imul(F,nt)|0,a=Math.imul(F,ot),o=o+Math.imul(c,st)|0,i=i+Math.imul(c,at)|0,i=i+Math.imul(v,st)|0,a=a+Math.imul(v,at)|0;var Kt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Kt>>>26)|0,Kt&=67108863,o=Math.imul(K,nt),i=Math.imul(K,ot),i=i+Math.imul(N,nt)|0,a=Math.imul(N,ot),o=o+Math.imul(k,st)|0,i=i+Math.imul(k,at)|0,i=i+Math.imul(F,st)|0,a=a+Math.imul(F,at)|0,o=o+Math.imul(c,ht)|0,i=i+Math.imul(c,ft)|0,i=i+Math.imul(v,ht)|0,a=a+Math.imul(v,ft)|0;var Zt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Zt>>>26)|0,Zt&=67108863,o=Math.imul(z,nt),i=Math.imul(z,ot),i=i+Math.imul(H,nt)|0,a=Math.imul(H,ot),o=o+Math.imul(K,st)|0,i=i+Math.imul(K,at)|0,i=i+Math.imul(N,st)|0,a=a+Math.imul(N,at)|0,o=o+Math.imul(k,ht)|0,i=i+Math.imul(k,ft)|0,i=i+Math.imul(F,ht)|0,a=a+Math.imul(F,ft)|0,o=o+Math.imul(c,lt)|0,i=i+Math.imul(c,ut)|0,i=i+Math.imul(v,lt)|0,a=a+Math.imul(v,ut)|0;var Nt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Nt>>>26)|0,Nt&=67108863,o=Math.imul(V,nt),i=Math.imul(V,ot),i=i+Math.imul(J,nt)|0,a=Math.imul(J,ot),o=o+Math.imul(z,st)|0,i=i+Math.imul(z,at)|0,i=i+Math.imul(H,st)|0,a=a+Math.imul(H,at)|0,o=o+Math.imul(K,ht)|0,i=i+Math.imul(K,ft)|0,i=i+Math.imul(N,ht)|0,a=a+Math.imul(N,ft)|0,o=o+Math.imul(k,lt)|0,i=i+Math.imul(k,ut)|0,i=i+Math.imul(F,lt)|0,a=a+Math.imul(F,ut)|0,o=o+Math.imul(c,dt)|0,i=i+Math.imul(c,mt)|0,i=i+Math.imul(v,dt)|0,a=a+Math.imul(v,mt)|0;var Wt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Wt>>>26)|0,Wt&=67108863,o=Math.imul(G,nt),i=Math.imul(G,ot),i=i+Math.imul(Q,nt)|0,a=Math.imul(Q,ot),o=o+Math.imul(V,st)|0,i=i+Math.imul(V,at)|0,i=i+Math.imul(J,st)|0,a=a+Math.imul(J,at)|0,o=o+Math.imul(z,ht)|0,i=i+Math.imul(z,ft)|0,i=i+Math.imul(H,ht)|0,a=a+Math.imul(H,ft)|0,o=o+Math.imul(K,lt)|0,i=i+Math.imul(K,ut)|0,i=i+Math.imul(N,lt)|0,a=a+Math.imul(N,ut)|0,o=o+Math.imul(k,dt)|0,i=i+Math.imul(k,mt)|0,i=i+Math.imul(F,dt)|0,a=a+Math.imul(F,mt)|0,o=o+Math.imul(c,pt)|0,i=i+Math.imul(c,ct)|0,i=i+Math.imul(v,pt)|0,a=a+Math.imul(v,ct)|0;var Ut=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Ut>>>26)|0,Ut&=67108863,o=Math.imul(X,nt),i=Math.imul(X,ot),i=i+Math.imul(Y,nt)|0,a=Math.imul(Y,ot),o=o+Math.imul(G,st)|0,i=i+Math.imul(G,at)|0,i=i+Math.imul(Q,st)|0,a=a+Math.imul(Q,at)|0,o=o+Math.imul(V,ht)|0,i=i+Math.imul(V,ft)|0,i=i+Math.imul(J,ht)|0,a=a+Math.imul(J,ft)|0,o=o+Math.imul(z,lt)|0,i=i+Math.imul(z,ut)|0,i=i+Math.imul(H,lt)|0,a=a+Math.imul(H,ut)|0,o=o+Math.imul(K,dt)|0,i=i+Math.imul(K,mt)|0,i=i+Math.imul(N,dt)|0,a=a+Math.imul(N,mt)|0,o=o+Math.imul(k,pt)|0,i=i+Math.imul(k,ct)|0,i=i+Math.imul(F,pt)|0,a=a+Math.imul(F,ct)|0,o=o+Math.imul(c,vt)|0,i=i+Math.imul(c,gt)|0,i=i+Math.imul(v,vt)|0,a=a+Math.imul(v,gt)|0;var zt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(zt>>>26)|0,zt&=67108863,o=Math.imul($,nt),i=Math.imul($,ot),i=i+Math.imul(j,nt)|0,a=Math.imul(j,ot),o=o+Math.imul(X,st)|0,i=i+Math.imul(X,at)|0,i=i+Math.imul(Y,st)|0,a=a+Math.imul(Y,at)|0,o=o+Math.imul(G,ht)|0,i=i+Math.imul(G,ft)|0,i=i+Math.imul(Q,ht)|0,a=a+Math.imul(Q,ft)|0,o=o+Math.imul(V,lt)|0,i=i+Math.imul(V,ut)|0,i=i+Math.imul(J,lt)|0,a=a+Math.imul(J,ut)|0,o=o+Math.imul(z,dt)|0,i=i+Math.imul(z,mt)|0,i=i+Math.imul(H,dt)|0,a=a+Math.imul(H,mt)|0,o=o+Math.imul(K,pt)|0,i=i+Math.imul(K,ct)|0,i=i+Math.imul(N,pt)|0,a=a+Math.imul(N,ct)|0,o=o+Math.imul(k,vt)|0,i=i+Math.imul(k,gt)|0,i=i+Math.imul(F,vt)|0,a=a+Math.imul(F,gt)|0,o=o+Math.imul(c,_t)|0,i=i+Math.imul(c,Mt)|0,i=i+Math.imul(v,_t)|0,a=a+Math.imul(v,Mt)|0;var Ht=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Ht>>>26)|0,Ht&=67108863,o=Math.imul(tt,nt),i=Math.imul(tt,ot),i=i+Math.imul(it,nt)|0,a=Math.imul(it,ot),o=o+Math.imul($,st)|0,i=i+Math.imul($,at)|0,i=i+Math.imul(j,st)|0,a=a+Math.imul(j,at)|0,o=o+Math.imul(X,ht)|0,i=i+Math.imul(X,ft)|0,i=i+Math.imul(Y,ht)|0,a=a+Math.imul(Y,ft)|0,o=o+Math.imul(G,lt)|0,i=i+Math.imul(G,ut)|0,i=i+Math.imul(Q,lt)|0,a=a+Math.imul(Q,ut)|0,o=o+Math.imul(V,dt)|0,i=i+Math.imul(V,mt)|0,i=i+Math.imul(J,dt)|0,a=a+Math.imul(J,mt)|0,o=o+Math.imul(z,pt)|0,i=i+Math.imul(z,ct)|0,i=i+Math.imul(H,pt)|0,a=a+Math.imul(H,ct)|0,o=o+Math.imul(K,vt)|0,i=i+Math.imul(K,gt)|0,i=i+Math.imul(N,vt)|0,a=a+Math.imul(N,gt)|0,o=o+Math.imul(k,_t)|0,i=i+Math.imul(k,Mt)|0,i=i+Math.imul(F,_t)|0,a=a+Math.imul(F,Mt)|0,o=o+Math.imul(c,wt)|0,i=i+Math.imul(c,yt)|0,i=i+Math.imul(v,wt)|0,a=a+Math.imul(v,yt)|0;var Vt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Vt>>>26)|0,Vt&=67108863,o=Math.imul(rt,nt),i=Math.imul(rt,ot),i=i+Math.imul(et,nt)|0,a=Math.imul(et,ot),o=o+Math.imul(tt,st)|0,i=i+Math.imul(tt,at)|0,i=i+Math.imul(it,st)|0,a=a+Math.imul(it,at)|0,o=o+Math.imul($,ht)|0,i=i+Math.imul($,ft)|0,i=i+Math.imul(j,ht)|0,a=a+Math.imul(j,ft)|0,o=o+Math.imul(X,lt)|0,i=i+Math.imul(X,ut)|0,i=i+Math.imul(Y,lt)|0,a=a+Math.imul(Y,ut)|0,o=o+Math.imul(G,dt)|0,i=i+Math.imul(G,mt)|0,i=i+Math.imul(Q,dt)|0,a=a+Math.imul(Q,mt)|0,o=o+Math.imul(V,pt)|0,i=i+Math.imul(V,ct)|0,i=i+Math.imul(J,pt)|0,a=a+Math.imul(J,ct)|0,o=o+Math.imul(z,vt)|0,i=i+Math.imul(z,gt)|0,i=i+Math.imul(H,vt)|0,a=a+Math.imul(H,gt)|0,o=o+Math.imul(K,_t)|0,i=i+Math.imul(K,Mt)|0,i=i+Math.imul(N,_t)|0,a=a+Math.imul(N,Mt)|0,o=o+Math.imul(k,wt)|0,i=i+Math.imul(k,yt)|0,i=i+Math.imul(F,wt)|0,a=a+Math.imul(F,yt)|0,o=o+Math.imul(c,bt)|0,i=i+Math.imul(c,xt)|0,i=i+Math.imul(v,bt)|0,a=a+Math.imul(v,xt)|0;var Jt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Jt>>>26)|0,Jt&=67108863,o=Math.imul(rt,st),i=Math.imul(rt,at),i=i+Math.imul(et,st)|0,a=Math.imul(et,at),o=o+Math.imul(tt,ht)|0,i=i+Math.imul(tt,ft)|0,i=i+Math.imul(it,ht)|0,a=a+Math.imul(it,ft)|0,o=o+Math.imul($,lt)|0,i=i+Math.imul($,ut)|0,i=i+Math.imul(j,lt)|0,a=a+Math.imul(j,ut)|0,o=o+Math.imul(X,dt)|0,i=i+Math.imul(X,mt)|0,i=i+Math.imul(Y,dt)|0,a=a+Math.imul(Y,mt)|0,o=o+Math.imul(G,pt)|0,i=i+Math.imul(G,ct)|0,i=i+Math.imul(Q,pt)|0,a=a+Math.imul(Q,ct)|0,o=o+Math.imul(V,vt)|0,i=i+Math.imul(V,gt)|0,i=i+Math.imul(J,vt)|0,a=a+Math.imul(J,gt)|0,o=o+Math.imul(z,_t)|0,i=i+Math.imul(z,Mt)|0,i=i+Math.imul(H,_t)|0,a=a+Math.imul(H,Mt)|0,o=o+Math.imul(K,wt)|0,i=i+Math.imul(K,yt)|0,i=i+Math.imul(N,wt)|0,a=a+Math.imul(N,yt)|0,o=o+Math.imul(k,bt)|0,i=i+Math.imul(k,xt)|0,i=i+Math.imul(F,bt)|0,a=a+Math.imul(F,xt)|0;var Gt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Gt>>>26)|0,Gt&=67108863,o=Math.imul(rt,ht),i=Math.imul(rt,ft),i=i+Math.imul(et,ht)|0,a=Math.imul(et,ft),o=o+Math.imul(tt,lt)|0,i=i+Math.imul(tt,ut)|0,i=i+Math.imul(it,lt)|0,a=a+Math.imul(it,ut)|0,o=o+Math.imul($,dt)|0,i=i+Math.imul($,mt)|0,i=i+Math.imul(j,dt)|0,a=a+Math.imul(j,mt)|0,o=o+Math.imul(X,pt)|0,i=i+Math.imul(X,ct)|0,i=i+Math.imul(Y,pt)|0,a=a+Math.imul(Y,ct)|0,o=o+Math.imul(G,vt)|0,i=i+Math.imul(G,gt)|0,i=i+Math.imul(Q,vt)|0,a=a+Math.imul(Q,gt)|0,o=o+Math.imul(V,_t)|0,i=i+Math.imul(V,Mt)|0,i=i+Math.imul(J,_t)|0,a=a+Math.imul(J,Mt)|0,o=o+Math.imul(z,wt)|0,i=i+Math.imul(z,yt)|0,i=i+Math.imul(H,wt)|0,a=a+Math.imul(H,yt)|0,o=o+Math.imul(K,bt)|0,i=i+Math.imul(K,xt)|0,i=i+Math.imul(N,bt)|0,a=a+Math.imul(N,xt)|0;var Qt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Qt>>>26)|0,Qt&=67108863,o=Math.imul(rt,lt),i=Math.imul(rt,ut),i=i+Math.imul(et,lt)|0,a=Math.imul(et,ut),o=o+Math.imul(tt,dt)|0,i=i+Math.imul(tt,mt)|0,i=i+Math.imul(it,dt)|0,a=a+Math.imul(it,mt)|0,o=o+Math.imul($,pt)|0,i=i+Math.imul($,ct)|0,i=i+Math.imul(j,pt)|0,a=a+Math.imul(j,ct)|0,o=o+Math.imul(X,vt)|0,i=i+Math.imul(X,gt)|0,i=i+Math.imul(Y,vt)|0,a=a+Math.imul(Y,gt)|0,o=o+Math.imul(G,_t)|0,i=i+Math.imul(G,Mt)|0,i=i+Math.imul(Q,_t)|0,a=a+Math.imul(Q,Mt)|0,o=o+Math.imul(V,wt)|0,i=i+Math.imul(V,yt)|0,i=i+Math.imul(J,wt)|0,a=a+Math.imul(J,yt)|0,o=o+Math.imul(z,bt)|0,i=i+Math.imul(z,xt)|0,i=i+Math.imul(H,bt)|0,a=a+Math.imul(H,xt)|0;var Xt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Xt>>>26)|0,Xt&=67108863,o=Math.imul(rt,dt),i=Math.imul(rt,mt),i=i+Math.imul(et,dt)|0,a=Math.imul(et,mt),o=o+Math.imul(tt,pt)|0,i=i+Math.imul(tt,ct)|0,i=i+Math.imul(it,pt)|0,a=a+Math.imul(it,ct)|0,o=o+Math.imul($,vt)|0,i=i+Math.imul($,gt)|0,i=i+Math.imul(j,vt)|0,a=a+Math.imul(j,gt)|0,o=o+Math.imul(X,_t)|0,i=i+Math.imul(X,Mt)|0,i=i+Math.imul(Y,_t)|0,a=a+Math.imul(Y,Mt)|0,o=o+Math.imul(G,wt)|0,i=i+Math.imul(G,yt)|0,i=i+Math.imul(Q,wt)|0,a=a+Math.imul(Q,yt)|0,o=o+Math.imul(V,bt)|0,i=i+Math.imul(V,xt)|0,i=i+Math.imul(J,bt)|0,a=a+Math.imul(J,xt)|0;var Yt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(Yt>>>26)|0,Yt&=67108863,o=Math.imul(rt,pt),i=Math.imul(rt,ct),i=i+Math.imul(et,pt)|0,a=Math.imul(et,ct),o=o+Math.imul(tt,vt)|0,i=i+Math.imul(tt,gt)|0,i=i+Math.imul(it,vt)|0,a=a+Math.imul(it,gt)|0,o=o+Math.imul($,_t)|0,i=i+Math.imul($,Mt)|0,i=i+Math.imul(j,_t)|0,a=a+Math.imul(j,Mt)|0,o=o+Math.imul(X,wt)|0,i=i+Math.imul(X,yt)|0,i=i+Math.imul(Y,wt)|0,a=a+Math.imul(Y,yt)|0,o=o+Math.imul(G,bt)|0,i=i+Math.imul(G,xt)|0,i=i+Math.imul(Q,bt)|0,a=a+Math.imul(Q,xt)|0;var $t=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+($t>>>26)|0,$t&=67108863,o=Math.imul(rt,vt),i=Math.imul(rt,gt),i=i+Math.imul(et,vt)|0,a=Math.imul(et,gt),o=o+Math.imul(tt,_t)|0,i=i+Math.imul(tt,Mt)|0,i=i+Math.imul(it,_t)|0,a=a+Math.imul(it,Mt)|0,o=o+Math.imul($,wt)|0,i=i+Math.imul($,yt)|0,i=i+Math.imul(j,wt)|0,a=a+Math.imul(j,yt)|0,o=o+Math.imul(X,bt)|0,i=i+Math.imul(X,xt)|0,i=i+Math.imul(Y,bt)|0,a=a+Math.imul(Y,xt)|0;var jt=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(jt>>>26)|0,jt&=67108863,o=Math.imul(rt,_t),i=Math.imul(rt,Mt),i=i+Math.imul(et,_t)|0,a=Math.imul(et,Mt),o=o+Math.imul(tt,wt)|0,i=i+Math.imul(tt,yt)|0,i=i+Math.imul(it,wt)|0,a=a+Math.imul(it,yt)|0,o=o+Math.imul($,bt)|0,i=i+Math.imul($,xt)|0,i=i+Math.imul(j,bt)|0,a=a+Math.imul(j,xt)|0;var ti=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(ti>>>26)|0,ti&=67108863,o=Math.imul(rt,wt),i=Math.imul(rt,yt),i=i+Math.imul(et,wt)|0,a=Math.imul(et,yt),o=o+Math.imul(tt,bt)|0,i=i+Math.imul(tt,xt)|0,i=i+Math.imul(it,bt)|0,a=a+Math.imul(it,xt)|0;var ii=(u+o|0)+((i&8191)<<13)|0;u=(a+(i>>>13)|0)+(ii>>>26)|0,ii&=67108863,o=Math.imul(rt,bt),i=Math.imul(rt,xt),i=i+Math.imul(et,bt)|0,a=Math.imul(et,xt);var ri=(u+o|0)+((i&8191)<<13)|0;return u=(a+(i>>>13)|0)+(ri>>>26)|0,ri&=67108863,l[0]=Ft,l[1]=Kt,l[2]=Zt,l[3]=Nt,l[4]=Wt,l[5]=Ut,l[6]=zt,l[7]=Ht,l[8]=Vt,l[9]=Jt,l[10]=Gt,l[11]=Qt,l[12]=Xt,l[13]=Yt,l[14]=$t,l[15]=jt,l[16]=ti,l[17]=ii,l[18]=ri,u!==0&&(l[19]=u,e.length++),e};Math.imul||(Z=U);function E(h,t,r){r.negative=t.negative^h.negative,r.length=h.length+t.length;for(var e=0,n=0,s=0;s<r.length-1;s++){var l=n;n=0;for(var u=e&67108863,o=Math.min(s,t.length-1),i=Math.max(0,s-h.length+1);i<=o;i++){var a=s-i,p=h.words[a]|0,c=t.words[i]|0,v=p*c,y=v&67108863;l=l+(v/67108864|0)|0,y=y+u|0,u=y&67108863,l=l+(y>>>26)|0,n+=l>>>26,l&=67108863}r.words[s]=u,e=l,l=n}return e!==0?r.words[s]=e:r.length--,r._strip()}function R(h,t,r){return E(h,t,r)}f.prototype.mulTo=function(t,r){var e,n=this.length+t.length;return this.length===10&&t.length===10?e=Z(this,t,r):n<63?e=U(this,t,r):n<1024?e=E(this,t,r):e=R(this,t,r),e};function B(h,t){this.x=h,this.y=t}B.prototype.makeRBT=function(t){for(var r=new Array(t),e=f.prototype._countBits(t)-1,n=0;n<t;n++)r[n]=this.revBin(n,e,t);return r},B.prototype.revBin=function(t,r,e){if(t===0||t===e-1)return t;for(var n=0,s=0;s<r;s++)n|=(t&1)<<r-s-1,t>>=1;return n},B.prototype.permute=function(t,r,e,n,s,l){for(var u=0;u<l;u++)n[u]=r[t[u]],s[u]=e[t[u]]},B.prototype.transform=function(t,r,e,n,s,l){this.permute(l,t,r,e,n,s);for(var u=1;u<s;u<<=1)for(var o=u<<1,i=Math.cos(2*Math.PI/o),a=Math.sin(2*Math.PI/o),p=0;p<s;p+=o)for(var c=i,v=a,y=0;y<u;y++){var k=e[p+y],F=n[p+y],kt=e[p+y+u],K=n[p+y+u],N=c*kt-v*K;K=c*K+v*kt,kt=N,e[p+y]=k+kt,n[p+y]=F+K,e[p+y+u]=k-kt,n[p+y+u]=F-K,y!==o&&(N=i*c-a*v,v=i*v+a*c,c=N)}},B.prototype.guessLen13b=function(t,r){var e=Math.max(r,t)|1,n=e&1,s=0;for(e=e/2|0;e;e=e>>>1)s++;return 1<<s+1+n},B.prototype.conjugate=function(t,r,e){if(!(e<=1))for(var n=0;n<e/2;n++){var s=t[n];t[n]=t[e-n-1],t[e-n-1]=s,s=r[n],r[n]=-r[e-n-1],r[e-n-1]=-s}},B.prototype.normalize13b=function(t,r){for(var e=0,n=0;n<r/2;n++){var s=Math.round(t[2*n+1]/r)*8192+Math.round(t[2*n]/r)+e;t[n]=s&67108863,s<67108864?e=0:e=s/67108864|0}return t},B.prototype.convert13b=function(t,r,e,n){for(var s=0,l=0;l<r;l++)s=s+(t[l]|0),e[2*l]=s&8191,s=s>>>13,e[2*l+1]=s&8191,s=s>>>13;for(l=2*r;l<n;++l)e[l]=0;d(s===0),d((s&-8192)===0)},B.prototype.stub=function(t){for(var r=new Array(t),e=0;e<t;e++)r[e]=0;return r},B.prototype.mulp=function(t,r,e){var n=2*this.guessLen13b(t.length,r.length),s=this.makeRBT(n),l=this.stub(n),u=new Array(n),o=new Array(n),i=new Array(n),a=new Array(n),p=new Array(n),c=new Array(n),v=e.words;v.length=n,this.convert13b(t.words,t.length,u,n),this.convert13b(r.words,r.length,a,n),this.transform(u,l,o,i,n,s),this.transform(a,l,p,c,n,s);for(var y=0;y<n;y++){var k=o[y]*p[y]-i[y]*c[y];i[y]=o[y]*c[y]+i[y]*p[y],o[y]=k}return this.conjugate(o,i,n),this.transform(o,i,v,l,n,s),this.conjugate(v,l,n),this.normalize13b(v,n),e.negative=t.negative^r.negative,e.length=t.length+r.length,e._strip()},f.prototype.mul=function(t){var r=new f(null);return r.words=new Array(this.length+t.length),this.mulTo(t,r)},f.prototype.mulf=function(t){var r=new f(null);return r.words=new Array(this.length+t.length),R(this,t,r)},f.prototype.imul=function(t){return this.clone().mulTo(t,this)},f.prototype.imuln=function(t){var r=t<0;r&&(t=-t),d(typeof t=="number"),d(t<67108864);for(var e=0,n=0;n<this.length;n++){var s=(this.words[n]|0)*t,l=(s&67108863)+(e&67108863);e>>=26,e+=s/67108864|0,e+=l>>>26,this.words[n]=l&67108863}return e!==0&&(this.words[n]=e,this.length++),this.length=t===0?1:this.length,r?this.ineg():this},f.prototype.muln=function(t){return this.clone().imuln(t)},f.prototype.sqr=function(){return this.mul(this)},f.prototype.isqr=function(){return this.imul(this.clone())},f.prototype.pow=function(t){var r=W(t);if(r.length===0)return new f(1);for(var e=this,n=0;n<r.length&&r[n]===0;n++,e=e.sqr());if(++n<r.length)for(var s=e.sqr();n<r.length;n++,s=s.sqr())r[n]!==0&&(e=e.mul(s));return e},f.prototype.iushln=function(t){d(typeof t=="number"&&t>=0);var r=t%26,e=(t-r)/26,n=67108863>>>26-r<<26-r,s;if(r!==0){var l=0;for(s=0;s<this.length;s++){var u=this.words[s]&n,o=(this.words[s]|0)-u<<r;this.words[s]=o|l,l=u>>>26-r}l&&(this.words[s]=l,this.length++)}if(e!==0){for(s=this.length-1;s>=0;s--)this.words[s+e]=this.words[s];for(s=0;s<e;s++)this.words[s]=0;this.length+=e}return this._strip()},f.prototype.ishln=function(t){return d(this.negative===0),this.iushln(t)},f.prototype.iushrn=function(t,r,e){d(typeof t=="number"&&t>=0);var n;r?n=(r-r%26)/26:n=0;var s=t%26,l=Math.min((t-s)/26,this.length),u=67108863^67108863>>>s<<s,o=e;if(n-=l,n=Math.max(0,n),o){for(var i=0;i<l;i++)o.words[i]=this.words[i];o.length=l}if(l!==0)if(this.length>l)for(this.length-=l,i=0;i<this.length;i++)this.words[i]=this.words[i+l];else this.words[0]=0,this.length=1;var a=0;for(i=this.length-1;i>=0&&(a!==0||i>=n);i--){var p=this.words[i]|0;this.words[i]=a<<26-s|p>>>s,a=p&u}return o&&a!==0&&(o.words[o.length++]=a),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},f.prototype.ishrn=function(t,r,e){return d(this.negative===0),this.iushrn(t,r,e)},f.prototype.shln=function(t){return this.clone().ishln(t)},f.prototype.ushln=function(t){return this.clone().iushln(t)},f.prototype.shrn=function(t){return this.clone().ishrn(t)},f.prototype.ushrn=function(t){return this.clone().iushrn(t)},f.prototype.testn=function(t){d(typeof t=="number"&&t>=0);var r=t%26,e=(t-r)/26,n=1<<r;if(this.length<=e)return!1;var s=this.words[e];return!!(s&n)},f.prototype.imaskn=function(t){d(typeof t=="number"&&t>=0);var r=t%26,e=(t-r)/26;if(d(this.negative===0,"imaskn works only with positive numbers"),this.length<=e)return this;if(r!==0&&e++,this.length=Math.min(e,this.length),r!==0){var n=67108863^67108863>>>r<<r;this.words[this.length-1]&=n}return this.length===0&&(this.words[0]=0,this.length=1),this._strip()},f.prototype.maskn=function(t){return this.clone().imaskn(t)},f.prototype.iaddn=function(t){return d(typeof t=="number"),d(t<67108864),t<0?this.isubn(-t):this.negative!==0?this.length===1&&(this.words[0]|0)<=t?(this.words[0]=t-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},f.prototype._iaddn=function(t){this.words[0]+=t;for(var r=0;r<this.length&&this.words[r]>=67108864;r++)this.words[r]-=67108864,r===this.length-1?this.words[r+1]=1:this.words[r+1]++;return this.length=Math.max(this.length,r+1),this},f.prototype.isubn=function(t){if(d(typeof t=="number"),d(t<67108864),t<0)return this.iaddn(-t);if(this.negative!==0)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var r=0;r<this.length&&this.words[r]<0;r++)this.words[r]+=67108864,this.words[r+1]-=1;return this._strip()},f.prototype.addn=function(t){return this.clone().iaddn(t)},f.prototype.subn=function(t){return this.clone().isubn(t)},f.prototype.iabs=function(){return this.negative=0,this},f.prototype.abs=function(){return this.clone().iabs()},f.prototype._ishlnsubmul=function(t,r,e){var n=t.length+e,s;this._expand(n);var l,u=0;for(s=0;s<t.length;s++){l=(this.words[s+e]|0)+u;var o=(t.words[s]|0)*r;l-=o&67108863,u=(l>>26)-(o/67108864|0),this.words[s+e]=l&67108863}for(;s<this.length-e;s++)l=(this.words[s+e]|0)+u,u=l>>26,this.words[s+e]=l&67108863;if(u===0)return this._strip();for(d(u===-1),u=0,s=0;s<this.length;s++)l=-(this.words[s]|0)+u,u=l>>26,this.words[s]=l&67108863;return this.negative=1,this._strip()},f.prototype._wordDiv=function(t,r){var e=this.length-t.length,n=this.clone(),s=t,l=s.words[s.length-1]|0,u=this._countBits(l);e=26-u,e!==0&&(s=s.ushln(e),n.iushln(e),l=s.words[s.length-1]|0);var o=n.length-s.length,i;if(r!=="mod"){i=new f(null),i.length=o+1,i.words=new Array(i.length);for(var a=0;a<i.length;a++)i.words[a]=0}var p=n.clone()._ishlnsubmul(s,1,o);p.negative===0&&(n=p,i&&(i.words[o]=1));for(var c=o-1;c>=0;c--){var v=(n.words[s.length+c]|0)*67108864+(n.words[s.length+c-1]|0);for(v=Math.min(v/l|0,67108863),n._ishlnsubmul(s,v,c);n.negative!==0;)v--,n.negative=0,n._ishlnsubmul(s,1,c),n.isZero()||(n.negative^=1);i&&(i.words[c]=v)}return i&&i._strip(),n._strip(),r!=="div"&&e!==0&&n.iushrn(e),{div:i||null,mod:n}},f.prototype.divmod=function(t,r,e){if(d(!t.isZero()),this.isZero())return{div:new f(0),mod:new f(0)};var n,s,l;return this.negative!==0&&t.negative===0?(l=this.neg().divmod(t,r),r!=="mod"&&(n=l.div.neg()),r!=="div"&&(s=l.mod.neg(),e&&s.negative!==0&&s.iadd(t)),{div:n,mod:s}):this.negative===0&&t.negative!==0?(l=this.divmod(t.neg(),r),r!=="mod"&&(n=l.div.neg()),{div:n,mod:l.mod}):(this.negative&t.negative)!==0?(l=this.neg().divmod(t.neg(),r),r!=="div"&&(s=l.mod.neg(),e&&s.negative!==0&&s.isub(t)),{div:l.div,mod:s}):t.length>this.length||this.cmp(t)<0?{div:new f(0),mod:this}:t.length===1?r==="div"?{div:this.divn(t.words[0]),mod:null}:r==="mod"?{div:null,mod:new f(this.modrn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new f(this.modrn(t.words[0]))}:this._wordDiv(t,r)},f.prototype.div=function(t){return this.divmod(t,"div",!1).div},f.prototype.mod=function(t){return this.divmod(t,"mod",!1).mod},f.prototype.umod=function(t){return this.divmod(t,"mod",!0).mod},f.prototype.divRound=function(t){var r=this.divmod(t);if(r.mod.isZero())return r.div;var e=r.div.negative!==0?r.mod.isub(t):r.mod,n=t.ushrn(1),s=t.andln(1),l=e.cmp(n);return l<0||s===1&&l===0?r.div:r.div.negative!==0?r.div.isubn(1):r.div.iaddn(1)},f.prototype.modrn=function(t){var r=t<0;r&&(t=-t),d(t<=67108863);for(var e=(1<<26)%t,n=0,s=this.length-1;s>=0;s--)n=(e*n+(this.words[s]|0))%t;return r?-n:n},f.prototype.modn=function(t){return this.modrn(t)},f.prototype.idivn=function(t){var r=t<0;r&&(t=-t),d(t<=67108863);for(var e=0,n=this.length-1;n>=0;n--){var s=(this.words[n]|0)+e*67108864;this.words[n]=s/t|0,e=s%t}return this._strip(),r?this.ineg():this},f.prototype.divn=function(t){return this.clone().idivn(t)},f.prototype.egcd=function(t){d(t.negative===0),d(!t.isZero());var r=this,e=t.clone();r.negative!==0?r=r.umod(t):r=r.clone();for(var n=new f(1),s=new f(0),l=new f(0),u=new f(1),o=0;r.isEven()&&e.isEven();)r.iushrn(1),e.iushrn(1),++o;for(var i=e.clone(),a=r.clone();!r.isZero();){for(var p=0,c=1;(r.words[0]&c)===0&&p<26;++p,c<<=1);if(p>0)for(r.iushrn(p);p-- >0;)(n.isOdd()||s.isOdd())&&(n.iadd(i),s.isub(a)),n.iushrn(1),s.iushrn(1);for(var v=0,y=1;(e.words[0]&y)===0&&v<26;++v,y<<=1);if(v>0)for(e.iushrn(v);v-- >0;)(l.isOdd()||u.isOdd())&&(l.iadd(i),u.isub(a)),l.iushrn(1),u.iushrn(1);r.cmp(e)>=0?(r.isub(e),n.isub(l),s.isub(u)):(e.isub(r),l.isub(n),u.isub(s))}return{a:l,b:u,gcd:e.iushln(o)}},f.prototype._invmp=function(t){d(t.negative===0),d(!t.isZero());var r=this,e=t.clone();r.negative!==0?r=r.umod(t):r=r.clone();for(var n=new f(1),s=new f(0),l=e.clone();r.cmpn(1)>0&&e.cmpn(1)>0;){for(var u=0,o=1;(r.words[0]&o)===0&&u<26;++u,o<<=1);if(u>0)for(r.iushrn(u);u-- >0;)n.isOdd()&&n.iadd(l),n.iushrn(1);for(var i=0,a=1;(e.words[0]&a)===0&&i<26;++i,a<<=1);if(i>0)for(e.iushrn(i);i-- >0;)s.isOdd()&&s.iadd(l),s.iushrn(1);r.cmp(e)>=0?(r.isub(e),n.isub(s)):(e.isub(r),s.isub(n))}var p;return r.cmpn(1)===0?p=n:p=s,p.cmpn(0)<0&&p.iadd(t),p},f.prototype.gcd=function(t){if(this.isZero())return t.abs();if(t.isZero())return this.abs();var r=this.clone(),e=t.clone();r.negative=0,e.negative=0;for(var n=0;r.isEven()&&e.isEven();n++)r.iushrn(1),e.iushrn(1);do{for(;r.isEven();)r.iushrn(1);for(;e.isEven();)e.iushrn(1);var s=r.cmp(e);if(s<0){var l=r;r=e,e=l}else if(s===0||e.cmpn(1)===0)break;r.isub(e)}while(!0);return e.iushln(n)},f.prototype.invm=function(t){return this.egcd(t).a.umod(t)},f.prototype.isEven=function(){return(this.words[0]&1)===0},f.prototype.isOdd=function(){return(this.words[0]&1)===1},f.prototype.andln=function(t){return this.words[0]&t},f.prototype.bincn=function(t){d(typeof t=="number");var r=t%26,e=(t-r)/26,n=1<<r;if(this.length<=e)return this._expand(e+1),this.words[e]|=n,this;for(var s=n,l=e;s!==0&&l<this.length;l++){var u=this.words[l]|0;u+=s,s=u>>>26,u&=67108863,this.words[l]=u}return s!==0&&(this.words[l]=s,this.length++),this},f.prototype.isZero=function(){return this.length===1&&this.words[0]===0},f.prototype.cmpn=function(t){var r=t<0;if(this.negative!==0&&!r)return-1;if(this.negative===0&&r)return 1;this._strip();var e;if(this.length>1)e=1;else{r&&(t=-t),d(t<=67108863,"Number is too big");var n=this.words[0]|0;e=n===t?0:n<t?-1:1}return this.negative!==0?-e|0:e},f.prototype.cmp=function(t){if(this.negative!==0&&t.negative===0)return-1;if(this.negative===0&&t.negative!==0)return 1;var r=this.ucmp(t);return this.negative!==0?-r|0:r},f.prototype.ucmp=function(t){if(this.length>t.length)return 1;if(this.length<t.length)return-1;for(var r=0,e=this.length-1;e>=0;e--){var n=this.words[e]|0,s=t.words[e]|0;if(n!==s){n<s?r=-1:n>s&&(r=1);break}}return r},f.prototype.gtn=function(t){return this.cmpn(t)===1},f.prototype.gt=function(t){return this.cmp(t)===1},f.prototype.gten=function(t){return this.cmpn(t)>=0},f.prototype.gte=function(t){return this.cmp(t)>=0},f.prototype.ltn=function(t){return this.cmpn(t)===-1},f.prototype.lt=function(t){return this.cmp(t)===-1},f.prototype.lten=function(t){return this.cmpn(t)<=0},f.prototype.lte=function(t){return this.cmp(t)<=0},f.prototype.eqn=function(t){return this.cmpn(t)===0},f.prototype.eq=function(t){return this.cmp(t)===0},f.red=function(t){return new P(t)},f.prototype.toRed=function(t){return d(!this.red,"Already a number in reduction context"),d(this.negative===0,"red works only with positives"),t.convertTo(this)._forceRed(t)},f.prototype.fromRed=function(){return d(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},f.prototype._forceRed=function(t){return this.red=t,this},f.prototype.forceRed=function(t){return d(!this.red,"Already a number in reduction context"),this._forceRed(t)},f.prototype.redAdd=function(t){return d(this.red,"redAdd works only with red numbers"),this.red.add(this,t)},f.prototype.redIAdd=function(t){return d(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)},f.prototype.redSub=function(t){return d(this.red,"redSub works only with red numbers"),this.red.sub(this,t)},f.prototype.redISub=function(t){return d(this.red,"redISub works only with red numbers"),this.red.isub(this,t)},f.prototype.redShl=function(t){return d(this.red,"redShl works only with red numbers"),this.red.shl(this,t)},f.prototype.redMul=function(t){return d(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.mul(this,t)},f.prototype.redIMul=function(t){return d(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.imul(this,t)},f.prototype.redSqr=function(){return d(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},f.prototype.redISqr=function(){return d(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},f.prototype.redSqrt=function(){return d(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},f.prototype.redInvm=function(){return d(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},f.prototype.redNeg=function(){return d(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},f.prototype.redPow=function(t){return d(this.red&&!t.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,t)};var I={k256:null,p224:null,p192:null,p25519:null};function O(h,t){this.name=h,this.p=new f(t,16),this.n=this.p.bitLength(),this.k=new f(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}O.prototype._tmp=function(){var t=new f(null);return t.words=new Array(Math.ceil(this.n/13)),t},O.prototype.ireduce=function(t){var r=t,e;do this.split(r,this.tmp),r=this.imulK(r),r=r.iadd(this.tmp),e=r.bitLength();while(e>this.n);var n=e<this.n?-1:r.ucmp(this.p);return n===0?(r.words[0]=0,r.length=1):n>0?r.isub(this.p):r.strip!==void 0?r.strip():r._strip(),r},O.prototype.split=function(t,r){t.iushrn(this.n,0,r)},O.prototype.imulK=function(t){return t.imul(this.k)};function At(){O.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}g(At,O),At.prototype.split=function(t,r){for(var e=4194303,n=Math.min(t.length,9),s=0;s<n;s++)r.words[s]=t.words[s];if(r.length=n,t.length<=9){t.words[0]=0,t.length=1;return}var l=t.words[9];for(r.words[r.length++]=l&e,s=10;s<t.length;s++){var u=t.words[s]|0;t.words[s-10]=(u&e)<<4|l>>>22,l=u}l>>>=22,t.words[s-10]=l,l===0&&t.length>10?t.length-=10:t.length-=9},At.prototype.imulK=function(t){t.words[t.length]=0,t.words[t.length+1]=0,t.length+=2;for(var r=0,e=0;e<t.length;e++){var n=t.words[e]|0;r+=n*977,t.words[e]=r&67108863,r=n*64+(r/67108864|0)}return t.words[t.length-1]===0&&(t.length--,t.words[t.length-1]===0&&t.length--),t};function St(){O.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}g(St,O);function Dt(){O.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}g(Dt,O);function Ct(){O.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}g(Ct,O),Ct.prototype.imulK=function(t){for(var r=0,e=0;e<t.length;e++){var n=(t.words[e]|0)*19+r,s=n&67108863;n>>>=26,t.words[e]=s,r=n}return r!==0&&(t.words[t.length++]=r),t},f._prime=function(t){if(I[t])return I[t];var r;if(t==="k256")r=new At;else if(t==="p224")r=new St;else if(t==="p192")r=new Dt;else if(t==="p25519")r=new Ct;else throw new Error("Unknown prime "+t);return I[t]=r,r};function P(h){if(typeof h=="string"){var t=f._prime(h);this.m=t.p,this.prime=t}else d(h.gtn(1),"modulus must be greater than 1"),this.m=h,this.prime=null}P.prototype._verify1=function(t){d(t.negative===0,"red works only with positives"),d(t.red,"red works only with red numbers")},P.prototype._verify2=function(t,r){d((t.negative|r.negative)===0,"red works only with positives"),d(t.red&&t.red===r.red,"red works only with red numbers")},P.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):(A(t,t.umod(this.m)._forceRed(this)),t)},P.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},P.prototype.add=function(t,r){this._verify2(t,r);var e=t.add(r);return e.cmp(this.m)>=0&&e.isub(this.m),e._forceRed(this)},P.prototype.iadd=function(t,r){this._verify2(t,r);var e=t.iadd(r);return e.cmp(this.m)>=0&&e.isub(this.m),e},P.prototype.sub=function(t,r){this._verify2(t,r);var e=t.sub(r);return e.cmpn(0)<0&&e.iadd(this.m),e._forceRed(this)},P.prototype.isub=function(t,r){this._verify2(t,r);var e=t.isub(r);return e.cmpn(0)<0&&e.iadd(this.m),e},P.prototype.shl=function(t,r){return this._verify1(t),this.imod(t.ushln(r))},P.prototype.imul=function(t,r){return this._verify2(t,r),this.imod(t.imul(r))},P.prototype.mul=function(t,r){return this._verify2(t,r),this.imod(t.mul(r))},P.prototype.isqr=function(t){return this.imul(t,t.clone())},P.prototype.sqr=function(t){return this.mul(t,t)},P.prototype.sqrt=function(t){if(t.isZero())return t.clone();var r=this.m.andln(3);if(d(r%2===1),r===3){var e=this.m.add(new f(1)).iushrn(2);return this.pow(t,e)}for(var n=this.m.subn(1),s=0;!n.isZero()&&n.andln(1)===0;)s++,n.iushrn(1);d(!n.isZero());var l=new f(1).toRed(this),u=l.redNeg(),o=this.m.subn(1).iushrn(1),i=this.m.bitLength();for(i=new f(2*i*i).toRed(this);this.pow(i,o).cmp(u)!==0;)i.redIAdd(u);for(var a=this.pow(i,n),p=this.pow(t,n.addn(1).iushrn(1)),c=this.pow(t,n),v=s;c.cmp(l)!==0;){for(var y=c,k=0;y.cmp(l)!==0;k++)y=y.redSqr();d(k<v);var F=this.pow(a,new f(1).iushln(v-k-1));p=p.redMul(F),a=F.redSqr(),c=c.redMul(a),v=k}return p},P.prototype.invm=function(t){var r=t._invmp(this.m);return r.negative!==0?(r.negative=0,this.imod(r).redNeg()):this.imod(r)},P.prototype.pow=function(t,r){if(r.isZero())return new f(1).toRed(this);if(r.cmpn(1)===0)return t.clone();var e=4,n=new Array(1<<e);n[0]=new f(1).toRed(this),n[1]=t;for(var s=2;s<n.length;s++)n[s]=this.mul(n[s-1],t);var l=n[0],u=0,o=0,i=r.bitLength()%26;for(i===0&&(i=26),s=r.length-1;s>=0;s--){for(var a=r.words[s],p=i-1;p>=0;p--){var c=a>>p&1;if(l!==n[0]&&(l=this.sqr(l)),c===0&&u===0){o=0;continue}u<<=1,u|=c,o++,!(o!==e&&(s!==0||p!==0))&&(l=this.mul(l,n[u]),o=0,u=0)}i=26}return l},P.prototype.convertTo=function(t){var r=t.umod(this.m);return r===t?r.clone():r},P.prototype.convertFrom=function(t){var r=t.clone();return r.red=null,r},f.mont=function(t){return new Rt(t)};function Rt(h){P.call(this,h),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new f(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)}g(Rt,P),Rt.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},Rt.prototype.convertFrom=function(t){var r=this.imod(t.mul(this.rinv));return r.red=null,r},Rt.prototype.imul=function(t,r){if(t.isZero()||r.isZero())return t.words[0]=0,t.length=1,t;var e=t.imul(r),n=e.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=e.isub(n).iushrn(this.shift),l=s;return s.cmp(this.m)>=0?l=s.isub(this.m):s.cmpn(0)<0&&(l=s.iadd(this.m)),l._forceRed(this)},Rt.prototype.mul=function(t,r){if(t.isZero()||r.isZero())return new f(0)._forceRed(this);var e=t.mul(r),n=e.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=e.isub(n).iushrn(this.shift),l=s;return s.cmp(this.m)>=0?l=s.isub(this.m):s.cmpn(0)<0&&(l=s.iadd(this.m)),l._forceRed(this)},Rt.prototype.invm=function(t){var r=this.imod(t._invmp(this.m).mul(this.r2));return r._forceRed(this)}})(typeof ei>"u"||ei,Oi)});import{AggregatorClient as Fi,Env as Tt}from"@cetusprotocol/aggregator-sdk";import{normalizeSuiAddress as Ki}from"@mysten/sui/utils";import{CetusDlmmSDK as rr}from"@cetusprotocol/dlmm-sdk";import{SdkWrapper as er}from"@cetusprotocol/common-sdk";import{FullRpcUrlMainnet as Qi,GraphRpcUrlMainnet as Xi}from"@cetusprotocol/common-sdk";var Ri={env:"mainnet",full_rpc_url:Qi,graph_rpc_url:Xi,aggregator_url:"https://api-sui.cetus.zone/router_v3",providers:[]};import{FullRpcUrlTestnet as Yi,GraphRpcUrlTestnet as $i}from"@cetusprotocol/common-sdk";var Bi={env:"testnet",full_rpc_url:Yi,graph_rpc_url:$i,aggregator_url:"https://api-sui.devcetus.com/router_v3",providers:["CETUS","DEEPBOOK","KRIYA","KRIYAV3","FLOWX","FLOWXV3","AFTERMATH","TURBOS","HAEDAL","VOLO","AFSUI"]};var Ii=Di(ni(),1);import{Transaction as Li}from"@mysten/sui/transactions";import{CoinAssist as si,d as x,DETAILS_KEYS as Ot,fromDecimalsAmount as Pi,toDecimalsAmount as Ti}from"@cetusprotocol/common-sdk";import{BaseError as ji}from"@cetusprotocol/common-sdk";var ki=(_=>(_.UnsupportedDepositMode="UnsupportedDepositMode",_.PositionIdUndefined="PositionIdUndefined",_.ParameterError="ParameterError",_.ReachMaxIterations="ReachMaxIterations",_.BestLiquidityIsZero="BestLiquidityIsZero",_.SwapAmountError="SwapAmountError",_.AggregatorError="AggregatorError",_))(ki||{}),Lt=class extends ji{constructor(m,d,g){super(m,d||"UnknownError",g)}static isZapErrorCode(m,d){return this.isErrorCode(m,d)}},Ci=(b,m,d)=>{throw new Lt(m.message,b,d)},qt=(b,m,d)=>{throw new Lt(m,b,d)};var oi=.005;var Et=Di(ni(),1);import{ClmmPoolUtil as tr,d as Bt,TickMath as ir}from"@cetusprotocol/common-sdk";function qi(b,m,d,g,f,S,M){let _=M?new Et.default(b):new Et.default(m),w=tr.estLiquidityAndCoinAmountFromOneAmounts(g,f,_,M,!0,S,new Et.default(d)),A=M?_.toString():w.coin_amount_a.toString(),L=M?w.coin_amount_b.toString():_.toString(),T=Bt(b).sub(A),C=Bt(m).sub(L);return{is_enough_amount:M?C.gte(0):T.gte(0),use_amount_a:A,use_amount_b:L,liquidity:w.liquidity_amount,amount_limit_a:w.coin_amount_limit_a,amount_limit_b:w.coin_amount_limit_b,remain_amount:M?C:T}}function cr(b,m,d,g,f,S,M){let _=ir.sqrtPriceX64ToTickIndex(new Et.default(d)),w=M;_<g?w=!0:_>f&&(w=!1);let A=qi(b,m,d,g,f,S,w);return A.is_enough_amount||(w=!w,A=qi(b,m,d,g,f,S,w)),{fix_liquidity_amount_a:w,...A}}function Ei(b,m,d,g){let f=Bt(b),S=Bt(d),M=Bt(g),_=f,w=m?f:Bt(0),A=m?Bt(0):f,L=M,T=Bt(M).mul(.999),C=Bt(0),D=_,q=null;function W(E){if(m){let R=w.minus(E),B=A.plus(E.mul(S));return{A:R,B,R:B.div(R)}}else{let R=w.plus(E.mul(S)),B=A.minus(E);return{A:R,B,R:R.div(B)}}}for(let E=0;E<120;E++){let R=C.plus(D).div(2),{A:B,B:I,R:O}=W(R);if(B.lte(0)||I.lte(0)){D=R;continue}if(O.gt(L))D=R;else if(O.lt(T))C=R;else{if(q=R,Bt(T).eq(0))break;D=R}}if(!q)return{swap_amount:"0",final_amount_a:w.toFixed(0),final_amount_b:A.toFixed(0)};let{A:U,B:Z}=W(q);return{swap_amount:q.toFixed(0),final_amount_a:U.toFixed(0),final_amount_b:Z.toFixed(0)}}import{BinUtils as ai}from"@cetusprotocol/dlmm-sdk";var Pt=class{constructor(m){this._sdk=m}get sdk(){return this._sdk}async calculateBalanceSwapAmountWithoutActiveId(m,d){let{fix_amount_a:g,coin_amount:f}=d,{bin_step:S,pool_id:M,active_id:_,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:T}=m,C=await this._sdk.DlmmSDK.Pool.getPool(M,!1),D,q=g,W=f;return _>A?(q=!1,g?(D=await this.findRouters(C.coin_type_a,C.coin_type_b,f),W=x(D.swap_out_amount).mul(1-.001).toFixed(0)):W=f):_<w&&(q=!0,g?W=f:(D=await this.findRouters(C.coin_type_b,C.coin_type_a,f),W=x(D.swap_out_amount).mul(1-.001).toFixed(0))),{liquidity_info:await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:M,coin_amount:W,fix_amount_a:q,active_id:_,bin_step:S,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:T}),swap_result:D}}async calculateBalanceSwapAmount(m,d){let{fix_amount_a:g,coin_amount:f}=d,{bin_step:S,pool_id:M,active_id:_,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:T}=m;if(_>A||_<w)return this.calculateBalanceSwapAmountWithoutActiveId(m,d);let C=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:M,coin_amount:f,fix_amount_a:g,active_id:_,bin_step:S,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:T}),{amount_a:D,amount_b:q}=C,W=g?x(q).div(D):x(D).div(q),U=g?ai.getPricePerLamportFromBinId(_,S):x(1).div(ai.getPricePerLamportFromBinId(_,S)).toString(),Z=await this._sdk.DlmmSDK.Pool.getPool(M,!1);try{let t=await this.findRouters(g?Z.coin_type_a:Z.coin_type_b,g?Z.coin_type_b:Z.coin_type_a,f);t&&(U=x(t.swap_out_amount).div(t.swap_in_amount).toString())}catch{}let{swap_amount:E,final_amount_a:R,final_amount_b:B}=Ei(f,g,U,W.toString());console.log("\u{1F680} ~ ZapModule ~ swap_amount:",{swap_amount:E,final_amount_a:R,final_amount_b:B,target_rate:W,real_price:U,coin_amount:f,fix_amount_a:g});let I=E||"0",O,At,St,Dt=0,Ct=.02,P,Rt,h;do{let t=x(f).sub(I).toFixed(0);St=await this.findRouters(g?Z.coin_type_a:Z.coin_type_b,g?Z.coin_type_b:Z.coin_type_a,I);let r=x(St.swap_out_amount).toFixed(0),e=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:M,coin_amount:t,fix_amount_a:g,active_id:_,bin_step:S,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:T}),{amount_a:n,amount_b:s}=e,l=g?s:n,u=x(r).sub(l).div(r);if(u.gt(0)&&(h===void 0||u.lt(h))&&(P=St,Rt=e,h=u),x(r).gt(l))if(x(u).gt(Ct))console.log("calculateBalanceSwapAmount -: ",{swap_amount_in:I,real_remain_rate:u.toString(),cached_real_remain_rate:h?.toString(),temp_swap_result:St,deposit_amount_other:l,count:Dt}),I=x(I).mul(1-.01).toFixed(0),Dt++;else{O=St,At=e;break}else I=x(I).mul(1+.01).toFixed(0),I=Math.min(Number(x(f).mul(1-1e-4).toFixed(0)),Number(I)).toString(),console.log("calculateBalanceSwapAmount +: ",{swap_amount_in:I,real_remain_rate:u.toString(),cached_real_remain_rate:h?.toString(),temp_swap_result:St,deposit_amount_other:l,count:Dt}),Dt++;if(Dt>5)break}while(!O&&x(I).gt(0));if(O===void 0&&P&&(O=P,At=Rt),O===void 0&&St){let{swap_out_amount:t}=St,r=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:M,coin_amount:x(t).sub(1-.001).toFixed(0),fix_amount_a:!g,active_id:_,bin_step:S,lower_bin_id:w,upper_bin_id:A,active_bin_of_pool:L,strategy_type:T});O=St,At=r}return{swap_result:O,liquidity_info:At}}async preCalculateDepositAmount(m,d){let{fix_amount_a:g,coin_amount:f}=d;console.log("\u{1F680} ~ ZapModule ~ preCalculateDepositAmount ~ options:",{options:m,mode_options:d});let{liquidity_info:S,swap_result:M}=await this.calculateBalanceSwapAmount(m,d);return S===void 0?qt("SwapAmountError","Best liquidity info is undefined",{[Ot.METHOD_NAME]:"preCalculateDepositAmount"}):{bin_infos:S,swap_result:M,fix_amount_a:g,coin_amount:f}}async buildDepositPayload(m,d){let{swap_slippage:g=oi,bin_step:f,pool_id:S,active_id:M,lower_bin_id:_,upper_bin_id:w,strategy_type:A,deposit_obj:L,slippage:T,pos_obj:C}=m,{fix_amount_a:D,coin_amount:q,swap_result:W,bin_infos:U}=L;d=d||new Li;let Z=await this._sdk.DlmmSDK.Pool.getPool(S,!1),{coin_type_a:E,coin_type_b:R}=Z,B,I;if(W){let O=D?E:R,{swap_in_amount:At,route_obj:St}=W,Dt=si.buildCoinWithBalance(BigInt(At),O,d),Ct={router:St,slippage:g,txb:d,inputCoin:Dt},P=await this._sdk.AggregatorClient.fixableRouterSwapV3(Ct);D?(B=si.buildCoinWithBalance(BigInt(U.amount_a),E,d),I=P):(B=P,I=si.buildCoinWithBalance(BigInt(U.amount_b),R,d))}if(C){let O={pool_id:S,bin_infos:U,coin_type_a:E,coin_type_b:R,active_id:M,strategy_type:A,max_price_slippage:T,bin_step:f,use_bin_infos:!1,position_id:C.pos_id,collect_fee:C.collect_fee,reward_coins:C.collect_rewarder_types,coin_object_id_a:B,coin_object_id_b:I};this._sdk.DlmmSDK.Position.addLiquidityPayload(O,d)}else{let O={pool_id:S,bin_infos:U,coin_type_a:E,coin_type_b:R,lower_bin_id:_,upper_bin_id:w,active_id:M,strategy_type:A,use_bin_infos:!1,max_price_slippage:T,bin_step:f,coin_object_id_a:B,coin_object_id_b:I};this._sdk.DlmmSDK.Position.addLiquidityPayload(O,d)}return d}async findRouters(m,d,g){let{providers:f}=this._sdk.sdkOptions,S=this._sdk.AggregatorClient;if(x(g).lt(1))return qt("SwapAmountError","Swap amount is less than the minimum precision",{[Ot.METHOD_NAME]:"findRouters"});try{let M={from:m,target:d,amount:new Ii.default(g),byAmountIn:!0,depth:3,providers:f},_=await S.findRouters(M);if(_?.error)return qt("AggregatorError",`Aggregator findRouters error: ${_?.error}`,{[Ot.METHOD_NAME]:"findRouters",[Ot.REQUEST_PARAMS]:M});if(!_?.paths||_?.paths?.length===0)return qt("AggregatorError","Aggregator findRouters error: no router",{[Ot.METHOD_NAME]:"findRouters",[Ot.REQUEST_PARAMS]:M});let w=_.amountIn.toString(),A=_.amountOut.toString();return{swap_in_amount:w,swap_out_amount:A,route_obj:_}}catch(M){return console.log("\u{1F680} ~ ZapModule ~ error:",JSON.stringify(M,null,2)),Ci("AggregatorError",M,{[Ot.METHOD_NAME]:"swapInPools"})}}calculateZapOutAvailableAmount(m){let{is_receive_coin_a:d,mode:g,active_id:f,bin_step:S,remove_bin_range:M,prices:_,coin_decimal_a:w,coin_decimal_b:A}=m,L;if(_){let{coin_a_price:R,coin_b_price:B}=_;L=x(R).div(B).toString()}else L=ai.getPriceFromBinId(f,S,w,A);let T,C="0",D="0",q="0";M.forEach(R=>{R.bin_id===f&&(T=R),C=x(C).add(R.amount_a).toFixed(0),D=x(D).add(R.amount_b).toFixed(0)});let W=x(C).sub(T?.amount_a||"0").toFixed(0),U=x(D).sub(T?.amount_b||"0").toFixed(0),Z=x(Ti(x(Pi(C,w)).mul(L).toString(),A)).toFixed(0),E=x(Ti(x(Pi(D,A)).div(L).toString(),w)).toFixed(0);return g==="OnlyCoinA"?d?q=W:q=Z:g==="OnlyCoinB"?(D=U,d?q=E:q=D):g==="Both"&&(d?q=x(E).add(C).toFixed(0):q=x(Z).add(D).toFixed(0)),{available_amount:q,user_total_amount_a:C,user_total_amount_b:D,active_bin:T,is_receive_coin_a:d}}async preCalculateWithdrawAmount(m){let{is_receive_coin_a:d,remove_bin_range:g,coin_type_a:f,coin_type_b:S,mode:M,expected_receive_amount:_,active_id:w}=m,{available_amount:A,user_total_amount_a:L,user_total_amount_b:T,active_bin:C}=this.calculateZapOutAvailableAmount(m);if(x(A).lt(x(_)))return qt("SwapAmountError",`Available amount is less than the expected receive amount: ${A} < ${_}`,{[Ot.METHOD_NAME]:"preCalculateWithdrawAmount",[Ot.REQUEST_PARAMS]:m});let D=x(_).div(A),q;if(M==="Both"){let E=x(L).gt(0),R=E?x(L).mul(D).toFixed(0):x(T).mul(D).toFixed(0);q=this._sdk.DlmmSDK.Position.calculateRemoveLiquidityInfo({bins:g,active_id:w,fix_amount_a:E,coin_amount:R})}else{let E=M==="OnlyCoinA"?x(L).mul(D).toFixed(0):x(T).mul(D).toFixed(0);q=this._sdk.DlmmSDK.Position.calculateRemoveLiquidityInfo({bins:g,active_id:w,is_only_a:M==="OnlyCoinA",coin_amount:E})}let{amount_a:W,amount_b:U}=q,Z;return d&&x(U).gt(0)&&(Z=await this.findRouters(S,f,U)),!d&&x(W).gt(0)&&(Z=await this.findRouters(f,S,W)),{remove_liquidity_info:q,swap_result:Z,mode:M,is_receive_coin_a:d,expected_receive_amount:_,remove_percent:D.toString()}}async buildWithdrawPayload(m){let d=new Li,{withdraw_obj:g,swap_slippage:f=oi,bin_step:S,pool_id:M,active_id:_,collect_fee:w,reward_coins:A,coin_type_a:L,coin_type_b:T,position_id:C,slippage:D,remove_percent:q,is_close_position:W}=m,{remove_liquidity_info:U,swap_result:Z,is_receive_coin_a:E}=g,R={pool_id:M,bin_infos:U,coin_type_a:L,coin_type_b:T,position_id:C,slippage:D,active_id:_,reward_coins:A,collect_fee:w,bin_step:S,remove_percent:Number(q)},{coin_a_obj:B,coin_b_obj:I}=W?this._sdk.DlmmSDK.Position.closePositionNoTransferPayload(R,d):this._sdk.DlmmSDK.Position.removeLiquidityNoTransferPayload(R,d);if(Z){let{route_obj:O}=Z,At={router:O,slippage:f,txb:d,inputCoin:E?I:B},St=await this._sdk.AggregatorClient.fixableRouterSwapV3(At);E?d.transferObjects([B,St],this._sdk.getSenderAddress()):d.transferObjects([I,St],this._sdk.getSenderAddress())}else d.transferObjects([B,I],this._sdk.getSenderAddress());return d}};import{SuiJsonRpcClient as Zi}from"@mysten/sui/jsonRpc";var It=class b extends er{constructor(m,d){super(m),this._zapModule=new Pt(this),this._dlmmSDK=d||rr.createSDK({env:m.env,full_rpc_url:m.full_rpc_url}),this._aggregatorClient=this.createAggregatorClient()}createAggregatorClient(){return new Fi({signer:Ki("0x0"),client:this._sdkOptions.sui_client||new Zi({url:this._sdkOptions.full_rpc_url,network:this._sdkOptions.env==="testnet"?"testnet":"mainnet"}),env:this._sdkOptions.env==="testnet"?Tt.Testnet:Tt.Mainnet,pythUrls:this._sdkOptions.pyth_urls})}updatePythUrls(m){if(m.length===0)throw new Error("pythUrls is empty");this._sdkOptions.pyth_urls=m,this._aggregatorClient=this.createAggregatorClient()}setSenderAddress(m){this._dlmmSDK.setSenderAddress(m)}getSenderAddress(m=!0){return this._dlmmSDK.getSenderAddress(m)}updateProviders(m){if(m.length===0)throw new Error("providers is empty");this._sdkOptions.providers=m}updateFullRpcUrl(m){super.updateFullRpcUrl(m),this._dlmmSDK.updateFullRpcUrl(m),this._aggregatorClient=new Fi({signer:Ki("0x0"),client:new Zi({url:m,network:this._sdkOptions.env==="testnet"?"testnet":"mainnet"}),env:this._sdkOptions.env==="testnet"?Tt.Testnet:Tt.Mainnet,pythUrls:this._sdkOptions.pyth_urls})}get DlmmSDK(){return this._dlmmSDK}get AggregatorClient(){return this._aggregatorClient}get Zap(){return this._zapModule}static createSDK(m,d){let{env:g="mainnet",full_rpc_url:f}=m;return g==="mainnet"?b.createCustomSDK({...Ri,...m},d):b.createCustomSDK({...Bi,...m},d)}static createCustomSDK(m,d){return new b(m,d)}};var Me=It;export{It as CetusDlmmZapSDK,Lt as ZapError,ki as ZapErrorCode,Pt as ZapModule,Ei as calcExactSwapAmount,qi as calculateLiquidityAmountEnough,cr as calculateLiquidityAmountSide,Me as default,oi as defaultSwapSlippage,Ci as handleError,qt as handleMessageError,Ri as zapMainnet,Bi as zapTestnet};
1
+ var te=Object.create;var Fi=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var ee=Object.getOwnPropertyNames;var re=Object.getPrototypeOf,ne=Object.prototype.hasOwnProperty;var oe=(B=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(B,{get:(m,f)=>(typeof require<"u"?require:m)[f]}):B)(function(B){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+B+'" is not supported')});var se=(B,m)=>()=>(m||B((m={exports:{}}).exports,m),m.exports);var ae=(B,m,f,p)=>{if(m&&typeof m=="object"||typeof m=="function")for(let o of ee(m))!ne.call(B,o)&&o!==f&&Fi(B,o,{get:()=>m[o],enumerable:!(p=ie(m,o))||p.enumerable});return B};var di=(B,m,f)=>(f=B!=null?te(re(B)):{},ae(m||!B||!B.__esModule?Fi(f,"default",{value:B,enumerable:!0}):f,B));var Nt=se((Ii,mi)=>{"use strict";(function(B,m){"use strict";function f(u,t){if(!u)throw new Error(t||"Assertion failed")}function p(u,t){u.super_=t;var e=function(){};e.prototype=t.prototype,u.prototype=new e,u.prototype.constructor=u}function o(u,t,e){if(o.isBN(u))return u;this.negative=0,this.words=null,this.length=0,this.red=null,u!==null&&((t==="le"||t==="be")&&(e=t,t=10),this._init(u||0,t||10,e||"be"))}typeof B=="object"?B.exports=o:m.BN=o,o.BN=o,o.wordSize=26;var v;try{typeof window<"u"&&typeof window.Buffer<"u"?v=window.Buffer:v=oe("buffer").Buffer}catch{}o.isBN=function(t){return t instanceof o?!0:t!==null&&typeof t=="object"&&t.constructor.wordSize===o.wordSize&&Array.isArray(t.words)},o.max=function(t,e){return t.cmp(e)>0?t:e},o.min=function(t,e){return t.cmp(e)<0?t:e},o.prototype._init=function(t,e,r){if(typeof t=="number")return this._initNumber(t,e,r);if(typeof t=="object")return this._initArray(t,e,r);e==="hex"&&(e=16),f(e===(e|0)&&e>=2&&e<=36),t=t.toString().replace(/\s+/g,"");var n=0;t[0]==="-"&&(n++,this.negative=1),n<t.length&&(e===16?this._parseHex(t,n,r):(this._parseBase(t,e,n),r==="le"&&this._initArray(this.toArray(),e,r)))},o.prototype._initNumber=function(t,e,r){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[t&67108863],this.length=1):t<4503599627370496?(this.words=[t&67108863,t/67108864&67108863],this.length=2):(f(t<9007199254740992),this.words=[t&67108863,t/67108864&67108863,1],this.length=3),r==="le"&&this._initArray(this.toArray(),e,r)},o.prototype._initArray=function(t,e,r){if(f(typeof t.length=="number"),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var a,h,d=0;if(r==="be")for(n=t.length-1,a=0;n>=0;n-=3)h=t[n]|t[n-1]<<8|t[n-2]<<16,this.words[a]|=h<<d&67108863,this.words[a+1]=h>>>26-d&67108863,d+=24,d>=26&&(d-=26,a++);else if(r==="le")for(n=0,a=0;n<t.length;n+=3)h=t[n]|t[n+1]<<8|t[n+2]<<16,this.words[a]|=h<<d&67108863,this.words[a+1]=h>>>26-d&67108863,d+=24,d>=26&&(d-=26,a++);return this._strip()};function b(u,t){var e=u.charCodeAt(t);if(e>=48&&e<=57)return e-48;if(e>=65&&e<=70)return e-55;if(e>=97&&e<=102)return e-87;f(!1,"Invalid character in "+u)}function c(u,t,e){var r=b(u,e);return e-1>=t&&(r|=b(u,e-1)<<4),r}o.prototype._parseHex=function(t,e,r){this.length=Math.ceil((t.length-e)/6),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var a=0,h=0,d;if(r==="be")for(n=t.length-1;n>=e;n-=2)d=c(t,e,n)<<a,this.words[h]|=d&67108863,a>=18?(a-=18,h+=1,this.words[h]|=d>>>26):a+=8;else{var s=t.length-e;for(n=s%2===0?e+1:e;n<t.length;n+=2)d=c(t,e,n)<<a,this.words[h]|=d&67108863,a>=18?(a-=18,h+=1,this.words[h]|=d>>>26):a+=8}this._strip()};function _(u,t,e,r){for(var n=0,a=0,h=Math.min(u.length,e),d=t;d<h;d++){var s=u.charCodeAt(d)-48;n*=r,s>=49?a=s-49+10:s>=17?a=s-17+10:a=s,f(s>=0&&a<r,"Invalid character"),n+=a}return n}o.prototype._parseBase=function(t,e,r){this.words=[0],this.length=1;for(var n=0,a=1;a<=67108863;a*=e)n++;n--,a=a/e|0;for(var h=t.length-r,d=h%n,s=Math.min(h,h-d)+r,i=0,l=r;l<s;l+=n)i=_(t,l,l+n,e),this.imuln(a),this.words[0]+i<67108864?this.words[0]+=i:this._iaddn(i);if(d!==0){var C=1;for(i=_(t,l,t.length,e),l=0;l<d;l++)C*=e;this.imuln(C),this.words[0]+i<67108864?this.words[0]+=i:this._iaddn(i)}this._strip()},o.prototype.copy=function(t){t.words=new Array(this.length);for(var e=0;e<this.length;e++)t.words[e]=this.words[e];t.length=this.length,t.negative=this.negative,t.red=this.red};function M(u,t){u.words=t.words,u.length=t.length,u.negative=t.negative,u.red=t.red}if(o.prototype._move=function(t){M(t,this)},o.prototype.clone=function(){var t=new o(null);return this.copy(t),t},o.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},o.prototype._strip=function(){for(;this.length>1&&this.words[this.length-1]===0;)this.length--;return this._normSign()},o.prototype._normSign=function(){return this.length===1&&this.words[0]===0&&(this.negative=0),this},typeof Symbol<"u"&&typeof Symbol.for=="function")try{o.prototype[Symbol.for("nodejs.util.inspect.custom")]=x}catch{o.prototype.inspect=x}else o.prototype.inspect=x;function x(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var A=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],y=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],g=[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];o.prototype.toString=function(t,e){t=t||10,e=e|0||1;var r;if(t===16||t==="hex"){r="";for(var n=0,a=0,h=0;h<this.length;h++){var d=this.words[h],s=((d<<n|a)&16777215).toString(16);a=d>>>24-n&16777215,n+=2,n>=26&&(n-=26,h--),a!==0||h!==this.length-1?r=A[6-s.length]+s+r:r=s+r}for(a!==0&&(r=a.toString(16)+r);r.length%e!==0;)r="0"+r;return this.negative!==0&&(r="-"+r),r}if(t===(t|0)&&t>=2&&t<=36){var i=y[t],l=g[t];r="";var C=this.clone();for(C.negative=0;!C.isZero();){var P=C.modrn(l).toString(t);C=C.idivn(l),C.isZero()?r=P+r:r=A[i-P.length]+P+r}for(this.isZero()&&(r="0"+r);r.length%e!==0;)r="0"+r;return this.negative!==0&&(r="-"+r),r}f(!1,"Base should be between 2 and 36")},o.prototype.toNumber=function(){var t=this.words[0];return this.length===2?t+=this.words[1]*67108864:this.length===3&&this.words[2]===1?t+=4503599627370496+this.words[1]*67108864:this.length>2&&f(!1,"Number can only safely store up to 53 bits"),this.negative!==0?-t:t},o.prototype.toJSON=function(){return this.toString(16,2)},v&&(o.prototype.toBuffer=function(t,e){return this.toArrayLike(v,t,e)}),o.prototype.toArray=function(t,e){return this.toArrayLike(Array,t,e)};var w=function(t,e){return t.allocUnsafe?t.allocUnsafe(e):new t(e)};o.prototype.toArrayLike=function(t,e,r){this._strip();var n=this.byteLength(),a=r||Math.max(1,n);f(n<=a,"byte array longer than desired length"),f(a>0,"Requested array length <= 0");var h=w(t,a),d=e==="le"?"LE":"BE";return this["_toArrayLike"+d](h,n),h},o.prototype._toArrayLikeLE=function(t,e){for(var r=0,n=0,a=0,h=0;a<this.length;a++){var d=this.words[a]<<h|n;t[r++]=d&255,r<t.length&&(t[r++]=d>>8&255),r<t.length&&(t[r++]=d>>16&255),h===6?(r<t.length&&(t[r++]=d>>24&255),n=0,h=0):(n=d>>>24,h+=2)}if(r<t.length)for(t[r++]=n;r<t.length;)t[r++]=0},o.prototype._toArrayLikeBE=function(t,e){for(var r=t.length-1,n=0,a=0,h=0;a<this.length;a++){var d=this.words[a]<<h|n;t[r--]=d&255,r>=0&&(t[r--]=d>>8&255),r>=0&&(t[r--]=d>>16&255),h===6?(r>=0&&(t[r--]=d>>24&255),n=0,h=0):(n=d>>>24,h+=2)}if(r>=0)for(t[r--]=n;r>=0;)t[r--]=0},Math.clz32?o.prototype._countBits=function(t){return 32-Math.clz32(t)}:o.prototype._countBits=function(t){var e=t,r=0;return e>=4096&&(r+=13,e>>>=13),e>=64&&(r+=7,e>>>=7),e>=8&&(r+=4,e>>>=4),e>=2&&(r+=2,e>>>=2),r+e},o.prototype._zeroBits=function(t){if(t===0)return 26;var e=t,r=0;return(e&8191)===0&&(r+=13,e>>>=13),(e&127)===0&&(r+=7,e>>>=7),(e&15)===0&&(r+=4,e>>>=4),(e&3)===0&&(r+=2,e>>>=2),(e&1)===0&&r++,r},o.prototype.bitLength=function(){var t=this.words[this.length-1],e=this._countBits(t);return(this.length-1)*26+e};function S(u){for(var t=new Array(u.bitLength()),e=0;e<t.length;e++){var r=e/26|0,n=e%26;t[e]=u.words[r]>>>n&1}return t}o.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,e=0;e<this.length;e++){var r=this._zeroBits(this.words[e]);if(t+=r,r!==26)break}return t},o.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},o.prototype.toTwos=function(t){return this.negative!==0?this.abs().inotn(t).iaddn(1):this.clone()},o.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},o.prototype.isNeg=function(){return this.negative!==0},o.prototype.neg=function(){return this.clone().ineg()},o.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},o.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var e=0;e<t.length;e++)this.words[e]=this.words[e]|t.words[e];return this._strip()},o.prototype.ior=function(t){return f((this.negative|t.negative)===0),this.iuor(t)},o.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},o.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},o.prototype.iuand=function(t){var e;this.length>t.length?e=t:e=this;for(var r=0;r<e.length;r++)this.words[r]=this.words[r]&t.words[r];return this.length=e.length,this._strip()},o.prototype.iand=function(t){return f((this.negative|t.negative)===0),this.iuand(t)},o.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},o.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},o.prototype.iuxor=function(t){var e,r;this.length>t.length?(e=this,r=t):(e=t,r=this);for(var n=0;n<r.length;n++)this.words[n]=e.words[n]^r.words[n];if(this!==e)for(;n<e.length;n++)this.words[n]=e.words[n];return this.length=e.length,this._strip()},o.prototype.ixor=function(t){return f((this.negative|t.negative)===0),this.iuxor(t)},o.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},o.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},o.prototype.inotn=function(t){f(typeof t=="number"&&t>=0);var e=Math.ceil(t/26)|0,r=t%26;this._expand(e),r>0&&e--;for(var n=0;n<e;n++)this.words[n]=~this.words[n]&67108863;return r>0&&(this.words[n]=~this.words[n]&67108863>>26-r),this._strip()},o.prototype.notn=function(t){return this.clone().inotn(t)},o.prototype.setn=function(t,e){f(typeof t=="number"&&t>=0);var r=t/26|0,n=t%26;return this._expand(r+1),e?this.words[r]=this.words[r]|1<<n:this.words[r]=this.words[r]&~(1<<n),this._strip()},o.prototype.iadd=function(t){var e;if(this.negative!==0&&t.negative===0)return this.negative=0,e=this.isub(t),this.negative^=1,this._normSign();if(this.negative===0&&t.negative!==0)return t.negative=0,e=this.isub(t),t.negative=1,e._normSign();var r,n;this.length>t.length?(r=this,n=t):(r=t,n=this);for(var a=0,h=0;h<n.length;h++)e=(r.words[h]|0)+(n.words[h]|0)+a,this.words[h]=e&67108863,a=e>>>26;for(;a!==0&&h<r.length;h++)e=(r.words[h]|0)+a,this.words[h]=e&67108863,a=e>>>26;if(this.length=r.length,a!==0)this.words[this.length]=a,this.length++;else if(r!==this)for(;h<r.length;h++)this.words[h]=r.words[h];return this},o.prototype.add=function(t){var e;return t.negative!==0&&this.negative===0?(t.negative=0,e=this.sub(t),t.negative^=1,e):t.negative===0&&this.negative!==0?(this.negative=0,e=t.sub(this),this.negative=1,e):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},o.prototype.isub=function(t){if(t.negative!==0){t.negative=0;var e=this.iadd(t);return t.negative=1,e._normSign()}else if(this.negative!==0)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var r=this.cmp(t);if(r===0)return this.negative=0,this.length=1,this.words[0]=0,this;var n,a;r>0?(n=this,a=t):(n=t,a=this);for(var h=0,d=0;d<a.length;d++)e=(n.words[d]|0)-(a.words[d]|0)+h,h=e>>26,this.words[d]=e&67108863;for(;h!==0&&d<n.length;d++)e=(n.words[d]|0)+h,h=e>>26,this.words[d]=e&67108863;if(h===0&&d<n.length&&n!==this)for(;d<n.length;d++)this.words[d]=n.words[d];return this.length=Math.max(this.length,d),n!==this&&(this.negative=1),this._strip()},o.prototype.sub=function(t){return this.clone().isub(t)};function T(u,t,e){e.negative=t.negative^u.negative;var r=u.length+t.length|0;e.length=r,r=r-1|0;var n=u.words[0]|0,a=t.words[0]|0,h=n*a,d=h&67108863,s=h/67108864|0;e.words[0]=d;for(var i=1;i<r;i++){for(var l=s>>>26,C=s&67108863,P=Math.min(i,t.length-1),k=Math.max(0,i-u.length+1);k<=P;k++){var F=i-k|0;n=u.words[F]|0,a=t.words[k]|0,h=n*a+C,l+=h/67108864|0,C=h&67108863}e.words[i]=C|0,s=l|0}return s!==0?e.words[i]=s|0:e.length--,e._strip()}var R=function(t,e,r){var n=t.words,a=e.words,h=r.words,d=0,s,i,l,C=n[0]|0,P=C&8191,k=C>>>13,F=n[1]|0,N=F&8191,W=F>>>13,Tt=n[2]|0,U=Tt&8191,j=Tt>>>13,vi=n[3]|0,H=vi&8191,G=vi>>>13,wi=n[4]|0,J=wi&8191,X=wi>>>13,yi=n[5]|0,Q=yi&8191,Y=yi>>>13,Mi=n[6]|0,$=Mi&8191,tt=Mi>>>13,bi=n[7]|0,it=bi&8191,et=bi>>>13,xi=n[8]|0,rt=xi&8191,nt=xi>>>13,Ai=n[9]|0,ot=Ai&8191,st=Ai>>>13,Si=a[0]|0,at=Si&8191,lt=Si>>>13,Ci=a[1]|0,ut=Ci&8191,ft=Ci>>>13,Ri=a[2]|0,ht=Ri&8191,dt=Ri>>>13,Di=a[3]|0,mt=Di&8191,pt=Di>>>13,Pi=a[4]|0,ct=Pi&8191,_t=Pi>>>13,ki=a[5]|0,gt=ki&8191,vt=ki>>>13,Oi=a[6]|0,wt=Oi&8191,yt=Oi>>>13,Bi=a[7]|0,Mt=Bi&8191,bt=Bi>>>13,qi=a[8]|0,xt=qi&8191,At=qi>>>13,Ti=a[9]|0,St=Ti&8191,Ct=Ti>>>13;r.negative=t.negative^e.negative,r.length=19,s=Math.imul(P,at),i=Math.imul(P,lt),i=i+Math.imul(k,at)|0,l=Math.imul(k,lt);var Ht=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(Ht>>>26)|0,Ht&=67108863,s=Math.imul(N,at),i=Math.imul(N,lt),i=i+Math.imul(W,at)|0,l=Math.imul(W,lt),s=s+Math.imul(P,ut)|0,i=i+Math.imul(P,ft)|0,i=i+Math.imul(k,ut)|0,l=l+Math.imul(k,ft)|0;var Gt=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(Gt>>>26)|0,Gt&=67108863,s=Math.imul(U,at),i=Math.imul(U,lt),i=i+Math.imul(j,at)|0,l=Math.imul(j,lt),s=s+Math.imul(N,ut)|0,i=i+Math.imul(N,ft)|0,i=i+Math.imul(W,ut)|0,l=l+Math.imul(W,ft)|0,s=s+Math.imul(P,ht)|0,i=i+Math.imul(P,dt)|0,i=i+Math.imul(k,ht)|0,l=l+Math.imul(k,dt)|0;var Jt=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(Jt>>>26)|0,Jt&=67108863,s=Math.imul(H,at),i=Math.imul(H,lt),i=i+Math.imul(G,at)|0,l=Math.imul(G,lt),s=s+Math.imul(U,ut)|0,i=i+Math.imul(U,ft)|0,i=i+Math.imul(j,ut)|0,l=l+Math.imul(j,ft)|0,s=s+Math.imul(N,ht)|0,i=i+Math.imul(N,dt)|0,i=i+Math.imul(W,ht)|0,l=l+Math.imul(W,dt)|0,s=s+Math.imul(P,mt)|0,i=i+Math.imul(P,pt)|0,i=i+Math.imul(k,mt)|0,l=l+Math.imul(k,pt)|0;var Xt=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(Xt>>>26)|0,Xt&=67108863,s=Math.imul(J,at),i=Math.imul(J,lt),i=i+Math.imul(X,at)|0,l=Math.imul(X,lt),s=s+Math.imul(H,ut)|0,i=i+Math.imul(H,ft)|0,i=i+Math.imul(G,ut)|0,l=l+Math.imul(G,ft)|0,s=s+Math.imul(U,ht)|0,i=i+Math.imul(U,dt)|0,i=i+Math.imul(j,ht)|0,l=l+Math.imul(j,dt)|0,s=s+Math.imul(N,mt)|0,i=i+Math.imul(N,pt)|0,i=i+Math.imul(W,mt)|0,l=l+Math.imul(W,pt)|0,s=s+Math.imul(P,ct)|0,i=i+Math.imul(P,_t)|0,i=i+Math.imul(k,ct)|0,l=l+Math.imul(k,_t)|0;var Qt=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(Qt>>>26)|0,Qt&=67108863,s=Math.imul(Q,at),i=Math.imul(Q,lt),i=i+Math.imul(Y,at)|0,l=Math.imul(Y,lt),s=s+Math.imul(J,ut)|0,i=i+Math.imul(J,ft)|0,i=i+Math.imul(X,ut)|0,l=l+Math.imul(X,ft)|0,s=s+Math.imul(H,ht)|0,i=i+Math.imul(H,dt)|0,i=i+Math.imul(G,ht)|0,l=l+Math.imul(G,dt)|0,s=s+Math.imul(U,mt)|0,i=i+Math.imul(U,pt)|0,i=i+Math.imul(j,mt)|0,l=l+Math.imul(j,pt)|0,s=s+Math.imul(N,ct)|0,i=i+Math.imul(N,_t)|0,i=i+Math.imul(W,ct)|0,l=l+Math.imul(W,_t)|0,s=s+Math.imul(P,gt)|0,i=i+Math.imul(P,vt)|0,i=i+Math.imul(k,gt)|0,l=l+Math.imul(k,vt)|0;var Yt=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(Yt>>>26)|0,Yt&=67108863,s=Math.imul($,at),i=Math.imul($,lt),i=i+Math.imul(tt,at)|0,l=Math.imul(tt,lt),s=s+Math.imul(Q,ut)|0,i=i+Math.imul(Q,ft)|0,i=i+Math.imul(Y,ut)|0,l=l+Math.imul(Y,ft)|0,s=s+Math.imul(J,ht)|0,i=i+Math.imul(J,dt)|0,i=i+Math.imul(X,ht)|0,l=l+Math.imul(X,dt)|0,s=s+Math.imul(H,mt)|0,i=i+Math.imul(H,pt)|0,i=i+Math.imul(G,mt)|0,l=l+Math.imul(G,pt)|0,s=s+Math.imul(U,ct)|0,i=i+Math.imul(U,_t)|0,i=i+Math.imul(j,ct)|0,l=l+Math.imul(j,_t)|0,s=s+Math.imul(N,gt)|0,i=i+Math.imul(N,vt)|0,i=i+Math.imul(W,gt)|0,l=l+Math.imul(W,vt)|0,s=s+Math.imul(P,wt)|0,i=i+Math.imul(P,yt)|0,i=i+Math.imul(k,wt)|0,l=l+Math.imul(k,yt)|0;var $t=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+($t>>>26)|0,$t&=67108863,s=Math.imul(it,at),i=Math.imul(it,lt),i=i+Math.imul(et,at)|0,l=Math.imul(et,lt),s=s+Math.imul($,ut)|0,i=i+Math.imul($,ft)|0,i=i+Math.imul(tt,ut)|0,l=l+Math.imul(tt,ft)|0,s=s+Math.imul(Q,ht)|0,i=i+Math.imul(Q,dt)|0,i=i+Math.imul(Y,ht)|0,l=l+Math.imul(Y,dt)|0,s=s+Math.imul(J,mt)|0,i=i+Math.imul(J,pt)|0,i=i+Math.imul(X,mt)|0,l=l+Math.imul(X,pt)|0,s=s+Math.imul(H,ct)|0,i=i+Math.imul(H,_t)|0,i=i+Math.imul(G,ct)|0,l=l+Math.imul(G,_t)|0,s=s+Math.imul(U,gt)|0,i=i+Math.imul(U,vt)|0,i=i+Math.imul(j,gt)|0,l=l+Math.imul(j,vt)|0,s=s+Math.imul(N,wt)|0,i=i+Math.imul(N,yt)|0,i=i+Math.imul(W,wt)|0,l=l+Math.imul(W,yt)|0,s=s+Math.imul(P,Mt)|0,i=i+Math.imul(P,bt)|0,i=i+Math.imul(k,Mt)|0,l=l+Math.imul(k,bt)|0;var ti=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(ti>>>26)|0,ti&=67108863,s=Math.imul(rt,at),i=Math.imul(rt,lt),i=i+Math.imul(nt,at)|0,l=Math.imul(nt,lt),s=s+Math.imul(it,ut)|0,i=i+Math.imul(it,ft)|0,i=i+Math.imul(et,ut)|0,l=l+Math.imul(et,ft)|0,s=s+Math.imul($,ht)|0,i=i+Math.imul($,dt)|0,i=i+Math.imul(tt,ht)|0,l=l+Math.imul(tt,dt)|0,s=s+Math.imul(Q,mt)|0,i=i+Math.imul(Q,pt)|0,i=i+Math.imul(Y,mt)|0,l=l+Math.imul(Y,pt)|0,s=s+Math.imul(J,ct)|0,i=i+Math.imul(J,_t)|0,i=i+Math.imul(X,ct)|0,l=l+Math.imul(X,_t)|0,s=s+Math.imul(H,gt)|0,i=i+Math.imul(H,vt)|0,i=i+Math.imul(G,gt)|0,l=l+Math.imul(G,vt)|0,s=s+Math.imul(U,wt)|0,i=i+Math.imul(U,yt)|0,i=i+Math.imul(j,wt)|0,l=l+Math.imul(j,yt)|0,s=s+Math.imul(N,Mt)|0,i=i+Math.imul(N,bt)|0,i=i+Math.imul(W,Mt)|0,l=l+Math.imul(W,bt)|0,s=s+Math.imul(P,xt)|0,i=i+Math.imul(P,At)|0,i=i+Math.imul(k,xt)|0,l=l+Math.imul(k,At)|0;var ii=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(ii>>>26)|0,ii&=67108863,s=Math.imul(ot,at),i=Math.imul(ot,lt),i=i+Math.imul(st,at)|0,l=Math.imul(st,lt),s=s+Math.imul(rt,ut)|0,i=i+Math.imul(rt,ft)|0,i=i+Math.imul(nt,ut)|0,l=l+Math.imul(nt,ft)|0,s=s+Math.imul(it,ht)|0,i=i+Math.imul(it,dt)|0,i=i+Math.imul(et,ht)|0,l=l+Math.imul(et,dt)|0,s=s+Math.imul($,mt)|0,i=i+Math.imul($,pt)|0,i=i+Math.imul(tt,mt)|0,l=l+Math.imul(tt,pt)|0,s=s+Math.imul(Q,ct)|0,i=i+Math.imul(Q,_t)|0,i=i+Math.imul(Y,ct)|0,l=l+Math.imul(Y,_t)|0,s=s+Math.imul(J,gt)|0,i=i+Math.imul(J,vt)|0,i=i+Math.imul(X,gt)|0,l=l+Math.imul(X,vt)|0,s=s+Math.imul(H,wt)|0,i=i+Math.imul(H,yt)|0,i=i+Math.imul(G,wt)|0,l=l+Math.imul(G,yt)|0,s=s+Math.imul(U,Mt)|0,i=i+Math.imul(U,bt)|0,i=i+Math.imul(j,Mt)|0,l=l+Math.imul(j,bt)|0,s=s+Math.imul(N,xt)|0,i=i+Math.imul(N,At)|0,i=i+Math.imul(W,xt)|0,l=l+Math.imul(W,At)|0,s=s+Math.imul(P,St)|0,i=i+Math.imul(P,Ct)|0,i=i+Math.imul(k,St)|0,l=l+Math.imul(k,Ct)|0;var ei=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(ei>>>26)|0,ei&=67108863,s=Math.imul(ot,ut),i=Math.imul(ot,ft),i=i+Math.imul(st,ut)|0,l=Math.imul(st,ft),s=s+Math.imul(rt,ht)|0,i=i+Math.imul(rt,dt)|0,i=i+Math.imul(nt,ht)|0,l=l+Math.imul(nt,dt)|0,s=s+Math.imul(it,mt)|0,i=i+Math.imul(it,pt)|0,i=i+Math.imul(et,mt)|0,l=l+Math.imul(et,pt)|0,s=s+Math.imul($,ct)|0,i=i+Math.imul($,_t)|0,i=i+Math.imul(tt,ct)|0,l=l+Math.imul(tt,_t)|0,s=s+Math.imul(Q,gt)|0,i=i+Math.imul(Q,vt)|0,i=i+Math.imul(Y,gt)|0,l=l+Math.imul(Y,vt)|0,s=s+Math.imul(J,wt)|0,i=i+Math.imul(J,yt)|0,i=i+Math.imul(X,wt)|0,l=l+Math.imul(X,yt)|0,s=s+Math.imul(H,Mt)|0,i=i+Math.imul(H,bt)|0,i=i+Math.imul(G,Mt)|0,l=l+Math.imul(G,bt)|0,s=s+Math.imul(U,xt)|0,i=i+Math.imul(U,At)|0,i=i+Math.imul(j,xt)|0,l=l+Math.imul(j,At)|0,s=s+Math.imul(N,St)|0,i=i+Math.imul(N,Ct)|0,i=i+Math.imul(W,St)|0,l=l+Math.imul(W,Ct)|0;var ri=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(ri>>>26)|0,ri&=67108863,s=Math.imul(ot,ht),i=Math.imul(ot,dt),i=i+Math.imul(st,ht)|0,l=Math.imul(st,dt),s=s+Math.imul(rt,mt)|0,i=i+Math.imul(rt,pt)|0,i=i+Math.imul(nt,mt)|0,l=l+Math.imul(nt,pt)|0,s=s+Math.imul(it,ct)|0,i=i+Math.imul(it,_t)|0,i=i+Math.imul(et,ct)|0,l=l+Math.imul(et,_t)|0,s=s+Math.imul($,gt)|0,i=i+Math.imul($,vt)|0,i=i+Math.imul(tt,gt)|0,l=l+Math.imul(tt,vt)|0,s=s+Math.imul(Q,wt)|0,i=i+Math.imul(Q,yt)|0,i=i+Math.imul(Y,wt)|0,l=l+Math.imul(Y,yt)|0,s=s+Math.imul(J,Mt)|0,i=i+Math.imul(J,bt)|0,i=i+Math.imul(X,Mt)|0,l=l+Math.imul(X,bt)|0,s=s+Math.imul(H,xt)|0,i=i+Math.imul(H,At)|0,i=i+Math.imul(G,xt)|0,l=l+Math.imul(G,At)|0,s=s+Math.imul(U,St)|0,i=i+Math.imul(U,Ct)|0,i=i+Math.imul(j,St)|0,l=l+Math.imul(j,Ct)|0;var ni=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(ni>>>26)|0,ni&=67108863,s=Math.imul(ot,mt),i=Math.imul(ot,pt),i=i+Math.imul(st,mt)|0,l=Math.imul(st,pt),s=s+Math.imul(rt,ct)|0,i=i+Math.imul(rt,_t)|0,i=i+Math.imul(nt,ct)|0,l=l+Math.imul(nt,_t)|0,s=s+Math.imul(it,gt)|0,i=i+Math.imul(it,vt)|0,i=i+Math.imul(et,gt)|0,l=l+Math.imul(et,vt)|0,s=s+Math.imul($,wt)|0,i=i+Math.imul($,yt)|0,i=i+Math.imul(tt,wt)|0,l=l+Math.imul(tt,yt)|0,s=s+Math.imul(Q,Mt)|0,i=i+Math.imul(Q,bt)|0,i=i+Math.imul(Y,Mt)|0,l=l+Math.imul(Y,bt)|0,s=s+Math.imul(J,xt)|0,i=i+Math.imul(J,At)|0,i=i+Math.imul(X,xt)|0,l=l+Math.imul(X,At)|0,s=s+Math.imul(H,St)|0,i=i+Math.imul(H,Ct)|0,i=i+Math.imul(G,St)|0,l=l+Math.imul(G,Ct)|0;var oi=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(oi>>>26)|0,oi&=67108863,s=Math.imul(ot,ct),i=Math.imul(ot,_t),i=i+Math.imul(st,ct)|0,l=Math.imul(st,_t),s=s+Math.imul(rt,gt)|0,i=i+Math.imul(rt,vt)|0,i=i+Math.imul(nt,gt)|0,l=l+Math.imul(nt,vt)|0,s=s+Math.imul(it,wt)|0,i=i+Math.imul(it,yt)|0,i=i+Math.imul(et,wt)|0,l=l+Math.imul(et,yt)|0,s=s+Math.imul($,Mt)|0,i=i+Math.imul($,bt)|0,i=i+Math.imul(tt,Mt)|0,l=l+Math.imul(tt,bt)|0,s=s+Math.imul(Q,xt)|0,i=i+Math.imul(Q,At)|0,i=i+Math.imul(Y,xt)|0,l=l+Math.imul(Y,At)|0,s=s+Math.imul(J,St)|0,i=i+Math.imul(J,Ct)|0,i=i+Math.imul(X,St)|0,l=l+Math.imul(X,Ct)|0;var si=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(si>>>26)|0,si&=67108863,s=Math.imul(ot,gt),i=Math.imul(ot,vt),i=i+Math.imul(st,gt)|0,l=Math.imul(st,vt),s=s+Math.imul(rt,wt)|0,i=i+Math.imul(rt,yt)|0,i=i+Math.imul(nt,wt)|0,l=l+Math.imul(nt,yt)|0,s=s+Math.imul(it,Mt)|0,i=i+Math.imul(it,bt)|0,i=i+Math.imul(et,Mt)|0,l=l+Math.imul(et,bt)|0,s=s+Math.imul($,xt)|0,i=i+Math.imul($,At)|0,i=i+Math.imul(tt,xt)|0,l=l+Math.imul(tt,At)|0,s=s+Math.imul(Q,St)|0,i=i+Math.imul(Q,Ct)|0,i=i+Math.imul(Y,St)|0,l=l+Math.imul(Y,Ct)|0;var ai=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(ai>>>26)|0,ai&=67108863,s=Math.imul(ot,wt),i=Math.imul(ot,yt),i=i+Math.imul(st,wt)|0,l=Math.imul(st,yt),s=s+Math.imul(rt,Mt)|0,i=i+Math.imul(rt,bt)|0,i=i+Math.imul(nt,Mt)|0,l=l+Math.imul(nt,bt)|0,s=s+Math.imul(it,xt)|0,i=i+Math.imul(it,At)|0,i=i+Math.imul(et,xt)|0,l=l+Math.imul(et,At)|0,s=s+Math.imul($,St)|0,i=i+Math.imul($,Ct)|0,i=i+Math.imul(tt,St)|0,l=l+Math.imul(tt,Ct)|0;var li=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(li>>>26)|0,li&=67108863,s=Math.imul(ot,Mt),i=Math.imul(ot,bt),i=i+Math.imul(st,Mt)|0,l=Math.imul(st,bt),s=s+Math.imul(rt,xt)|0,i=i+Math.imul(rt,At)|0,i=i+Math.imul(nt,xt)|0,l=l+Math.imul(nt,At)|0,s=s+Math.imul(it,St)|0,i=i+Math.imul(it,Ct)|0,i=i+Math.imul(et,St)|0,l=l+Math.imul(et,Ct)|0;var ui=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(ui>>>26)|0,ui&=67108863,s=Math.imul(ot,xt),i=Math.imul(ot,At),i=i+Math.imul(st,xt)|0,l=Math.imul(st,At),s=s+Math.imul(rt,St)|0,i=i+Math.imul(rt,Ct)|0,i=i+Math.imul(nt,St)|0,l=l+Math.imul(nt,Ct)|0;var fi=(d+s|0)+((i&8191)<<13)|0;d=(l+(i>>>13)|0)+(fi>>>26)|0,fi&=67108863,s=Math.imul(ot,St),i=Math.imul(ot,Ct),i=i+Math.imul(st,St)|0,l=Math.imul(st,Ct);var hi=(d+s|0)+((i&8191)<<13)|0;return d=(l+(i>>>13)|0)+(hi>>>26)|0,hi&=67108863,h[0]=Ht,h[1]=Gt,h[2]=Jt,h[3]=Xt,h[4]=Qt,h[5]=Yt,h[6]=$t,h[7]=ti,h[8]=ii,h[9]=ei,h[10]=ri,h[11]=ni,h[12]=oi,h[13]=si,h[14]=ai,h[15]=li,h[16]=ui,h[17]=fi,h[18]=hi,d!==0&&(h[19]=d,r.length++),r};Math.imul||(R=T);function D(u,t,e){e.negative=t.negative^u.negative,e.length=u.length+t.length;for(var r=0,n=0,a=0;a<e.length-1;a++){var h=n;n=0;for(var d=r&67108863,s=Math.min(a,t.length-1),i=Math.max(0,a-u.length+1);i<=s;i++){var l=a-i,C=u.words[l]|0,P=t.words[i]|0,k=C*P,F=k&67108863;h=h+(k/67108864|0)|0,F=F+d|0,d=F&67108863,h=h+(F>>>26)|0,n+=h>>>26,h&=67108863}e.words[a]=d,r=h,h=n}return r!==0?e.words[a]=r:e.length--,e._strip()}function O(u,t,e){return D(u,t,e)}o.prototype.mulTo=function(t,e){var r,n=this.length+t.length;return this.length===10&&t.length===10?r=R(this,t,e):n<63?r=T(this,t,e):n<1024?r=D(this,t,e):r=O(this,t,e),r};function q(u,t){this.x=u,this.y=t}q.prototype.makeRBT=function(t){for(var e=new Array(t),r=o.prototype._countBits(t)-1,n=0;n<t;n++)e[n]=this.revBin(n,r,t);return e},q.prototype.revBin=function(t,e,r){if(t===0||t===r-1)return t;for(var n=0,a=0;a<e;a++)n|=(t&1)<<e-a-1,t>>=1;return n},q.prototype.permute=function(t,e,r,n,a,h){for(var d=0;d<h;d++)n[d]=e[t[d]],a[d]=r[t[d]]},q.prototype.transform=function(t,e,r,n,a,h){this.permute(h,t,e,r,n,a);for(var d=1;d<a;d<<=1)for(var s=d<<1,i=Math.cos(2*Math.PI/s),l=Math.sin(2*Math.PI/s),C=0;C<a;C+=s)for(var P=i,k=l,F=0;F<d;F++){var N=r[C+F],W=n[C+F],Tt=r[C+F+d],U=n[C+F+d],j=P*Tt-k*U;U=P*U+k*Tt,Tt=j,r[C+F]=N+Tt,n[C+F]=W+U,r[C+F+d]=N-Tt,n[C+F+d]=W-U,F!==s&&(j=i*P-l*k,k=i*k+l*P,P=j)}},q.prototype.guessLen13b=function(t,e){var r=Math.max(e,t)|1,n=r&1,a=0;for(r=r/2|0;r;r=r>>>1)a++;return 1<<a+1+n},q.prototype.conjugate=function(t,e,r){if(!(r<=1))for(var n=0;n<r/2;n++){var a=t[n];t[n]=t[r-n-1],t[r-n-1]=a,a=e[n],e[n]=-e[r-n-1],e[r-n-1]=-a}},q.prototype.normalize13b=function(t,e){for(var r=0,n=0;n<e/2;n++){var a=Math.round(t[2*n+1]/e)*8192+Math.round(t[2*n]/e)+r;t[n]=a&67108863,a<67108864?r=0:r=a/67108864|0}return t},q.prototype.convert13b=function(t,e,r,n){for(var a=0,h=0;h<e;h++)a=a+(t[h]|0),r[2*h]=a&8191,a=a>>>13,r[2*h+1]=a&8191,a=a>>>13;for(h=2*e;h<n;++h)r[h]=0;f(a===0),f((a&-8192)===0)},q.prototype.stub=function(t){for(var e=new Array(t),r=0;r<t;r++)e[r]=0;return e},q.prototype.mulp=function(t,e,r){var n=2*this.guessLen13b(t.length,e.length),a=this.makeRBT(n),h=this.stub(n),d=new Array(n),s=new Array(n),i=new Array(n),l=new Array(n),C=new Array(n),P=new Array(n),k=r.words;k.length=n,this.convert13b(t.words,t.length,d,n),this.convert13b(e.words,e.length,l,n),this.transform(d,h,s,i,n,a),this.transform(l,h,C,P,n,a);for(var F=0;F<n;F++){var N=s[F]*C[F]-i[F]*P[F];i[F]=s[F]*P[F]+i[F]*C[F],s[F]=N}return this.conjugate(s,i,n),this.transform(s,i,k,h,n,a),this.conjugate(k,h,n),this.normalize13b(k,n),r.negative=t.negative^e.negative,r.length=t.length+e.length,r._strip()},o.prototype.mul=function(t){var e=new o(null);return e.words=new Array(this.length+t.length),this.mulTo(t,e)},o.prototype.mulf=function(t){var e=new o(null);return e.words=new Array(this.length+t.length),O(this,t,e)},o.prototype.imul=function(t){return this.clone().mulTo(t,this)},o.prototype.imuln=function(t){var e=t<0;e&&(t=-t),f(typeof t=="number"),f(t<67108864);for(var r=0,n=0;n<this.length;n++){var a=(this.words[n]|0)*t,h=(a&67108863)+(r&67108863);r>>=26,r+=a/67108864|0,r+=h>>>26,this.words[n]=h&67108863}return r!==0&&(this.words[n]=r,this.length++),this.length=t===0?1:this.length,e?this.ineg():this},o.prototype.muln=function(t){return this.clone().imuln(t)},o.prototype.sqr=function(){return this.mul(this)},o.prototype.isqr=function(){return this.imul(this.clone())},o.prototype.pow=function(t){var e=S(t);if(e.length===0)return new o(1);for(var r=this,n=0;n<e.length&&e[n]===0;n++,r=r.sqr());if(++n<e.length)for(var a=r.sqr();n<e.length;n++,a=a.sqr())e[n]!==0&&(r=r.mul(a));return r},o.prototype.iushln=function(t){f(typeof t=="number"&&t>=0);var e=t%26,r=(t-e)/26,n=67108863>>>26-e<<26-e,a;if(e!==0){var h=0;for(a=0;a<this.length;a++){var d=this.words[a]&n,s=(this.words[a]|0)-d<<e;this.words[a]=s|h,h=d>>>26-e}h&&(this.words[a]=h,this.length++)}if(r!==0){for(a=this.length-1;a>=0;a--)this.words[a+r]=this.words[a];for(a=0;a<r;a++)this.words[a]=0;this.length+=r}return this._strip()},o.prototype.ishln=function(t){return f(this.negative===0),this.iushln(t)},o.prototype.iushrn=function(t,e,r){f(typeof t=="number"&&t>=0);var n;e?n=(e-e%26)/26:n=0;var a=t%26,h=Math.min((t-a)/26,this.length),d=67108863^67108863>>>a<<a,s=r;if(n-=h,n=Math.max(0,n),s){for(var i=0;i<h;i++)s.words[i]=this.words[i];s.length=h}if(h!==0)if(this.length>h)for(this.length-=h,i=0;i<this.length;i++)this.words[i]=this.words[i+h];else this.words[0]=0,this.length=1;var l=0;for(i=this.length-1;i>=0&&(l!==0||i>=n);i--){var C=this.words[i]|0;this.words[i]=l<<26-a|C>>>a,l=C&d}return s&&l!==0&&(s.words[s.length++]=l),this.length===0&&(this.words[0]=0,this.length=1),this._strip()},o.prototype.ishrn=function(t,e,r){return f(this.negative===0),this.iushrn(t,e,r)},o.prototype.shln=function(t){return this.clone().ishln(t)},o.prototype.ushln=function(t){return this.clone().iushln(t)},o.prototype.shrn=function(t){return this.clone().ishrn(t)},o.prototype.ushrn=function(t){return this.clone().iushrn(t)},o.prototype.testn=function(t){f(typeof t=="number"&&t>=0);var e=t%26,r=(t-e)/26,n=1<<e;if(this.length<=r)return!1;var a=this.words[r];return!!(a&n)},o.prototype.imaskn=function(t){f(typeof t=="number"&&t>=0);var e=t%26,r=(t-e)/26;if(f(this.negative===0,"imaskn works only with positive numbers"),this.length<=r)return this;if(e!==0&&r++,this.length=Math.min(r,this.length),e!==0){var n=67108863^67108863>>>e<<e;this.words[this.length-1]&=n}return this.length===0&&(this.words[0]=0,this.length=1),this._strip()},o.prototype.maskn=function(t){return this.clone().imaskn(t)},o.prototype.iaddn=function(t){return f(typeof t=="number"),f(t<67108864),t<0?this.isubn(-t):this.negative!==0?this.length===1&&(this.words[0]|0)<=t?(this.words[0]=t-(this.words[0]|0),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},o.prototype._iaddn=function(t){this.words[0]+=t;for(var e=0;e<this.length&&this.words[e]>=67108864;e++)this.words[e]-=67108864,e===this.length-1?this.words[e+1]=1:this.words[e+1]++;return this.length=Math.max(this.length,e+1),this},o.prototype.isubn=function(t){if(f(typeof t=="number"),f(t<67108864),t<0)return this.iaddn(-t);if(this.negative!==0)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,this.length===1&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var e=0;e<this.length&&this.words[e]<0;e++)this.words[e]+=67108864,this.words[e+1]-=1;return this._strip()},o.prototype.addn=function(t){return this.clone().iaddn(t)},o.prototype.subn=function(t){return this.clone().isubn(t)},o.prototype.iabs=function(){return this.negative=0,this},o.prototype.abs=function(){return this.clone().iabs()},o.prototype._ishlnsubmul=function(t,e,r){var n=t.length+r,a;this._expand(n);var h,d=0;for(a=0;a<t.length;a++){h=(this.words[a+r]|0)+d;var s=(t.words[a]|0)*e;h-=s&67108863,d=(h>>26)-(s/67108864|0),this.words[a+r]=h&67108863}for(;a<this.length-r;a++)h=(this.words[a+r]|0)+d,d=h>>26,this.words[a+r]=h&67108863;if(d===0)return this._strip();for(f(d===-1),d=0,a=0;a<this.length;a++)h=-(this.words[a]|0)+d,d=h>>26,this.words[a]=h&67108863;return this.negative=1,this._strip()},o.prototype._wordDiv=function(t,e){var r=this.length-t.length,n=this.clone(),a=t,h=a.words[a.length-1]|0,d=this._countBits(h);r=26-d,r!==0&&(a=a.ushln(r),n.iushln(r),h=a.words[a.length-1]|0);var s=n.length-a.length,i;if(e!=="mod"){i=new o(null),i.length=s+1,i.words=new Array(i.length);for(var l=0;l<i.length;l++)i.words[l]=0}var C=n.clone()._ishlnsubmul(a,1,s);C.negative===0&&(n=C,i&&(i.words[s]=1));for(var P=s-1;P>=0;P--){var k=(n.words[a.length+P]|0)*67108864+(n.words[a.length+P-1]|0);for(k=Math.min(k/h|0,67108863),n._ishlnsubmul(a,k,P);n.negative!==0;)k--,n.negative=0,n._ishlnsubmul(a,1,P),n.isZero()||(n.negative^=1);i&&(i.words[P]=k)}return i&&i._strip(),n._strip(),e!=="div"&&r!==0&&n.iushrn(r),{div:i||null,mod:n}},o.prototype.divmod=function(t,e,r){if(f(!t.isZero()),this.isZero())return{div:new o(0),mod:new o(0)};var n,a,h;return this.negative!==0&&t.negative===0?(h=this.neg().divmod(t,e),e!=="mod"&&(n=h.div.neg()),e!=="div"&&(a=h.mod.neg(),r&&a.negative!==0&&a.iadd(t)),{div:n,mod:a}):this.negative===0&&t.negative!==0?(h=this.divmod(t.neg(),e),e!=="mod"&&(n=h.div.neg()),{div:n,mod:h.mod}):(this.negative&t.negative)!==0?(h=this.neg().divmod(t.neg(),e),e!=="div"&&(a=h.mod.neg(),r&&a.negative!==0&&a.isub(t)),{div:h.div,mod:a}):t.length>this.length||this.cmp(t)<0?{div:new o(0),mod:this}:t.length===1?e==="div"?{div:this.divn(t.words[0]),mod:null}:e==="mod"?{div:null,mod:new o(this.modrn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new o(this.modrn(t.words[0]))}:this._wordDiv(t,e)},o.prototype.div=function(t){return this.divmod(t,"div",!1).div},o.prototype.mod=function(t){return this.divmod(t,"mod",!1).mod},o.prototype.umod=function(t){return this.divmod(t,"mod",!0).mod},o.prototype.divRound=function(t){var e=this.divmod(t);if(e.mod.isZero())return e.div;var r=e.div.negative!==0?e.mod.isub(t):e.mod,n=t.ushrn(1),a=t.andln(1),h=r.cmp(n);return h<0||a===1&&h===0?e.div:e.div.negative!==0?e.div.isubn(1):e.div.iaddn(1)},o.prototype.modrn=function(t){var e=t<0;e&&(t=-t),f(t<=67108863);for(var r=(1<<26)%t,n=0,a=this.length-1;a>=0;a--)n=(r*n+(this.words[a]|0))%t;return e?-n:n},o.prototype.modn=function(t){return this.modrn(t)},o.prototype.idivn=function(t){var e=t<0;e&&(t=-t),f(t<=67108863);for(var r=0,n=this.length-1;n>=0;n--){var a=(this.words[n]|0)+r*67108864;this.words[n]=a/t|0,r=a%t}return this._strip(),e?this.ineg():this},o.prototype.divn=function(t){return this.clone().idivn(t)},o.prototype.egcd=function(t){f(t.negative===0),f(!t.isZero());var e=this,r=t.clone();e.negative!==0?e=e.umod(t):e=e.clone();for(var n=new o(1),a=new o(0),h=new o(0),d=new o(1),s=0;e.isEven()&&r.isEven();)e.iushrn(1),r.iushrn(1),++s;for(var i=r.clone(),l=e.clone();!e.isZero();){for(var C=0,P=1;(e.words[0]&P)===0&&C<26;++C,P<<=1);if(C>0)for(e.iushrn(C);C-- >0;)(n.isOdd()||a.isOdd())&&(n.iadd(i),a.isub(l)),n.iushrn(1),a.iushrn(1);for(var k=0,F=1;(r.words[0]&F)===0&&k<26;++k,F<<=1);if(k>0)for(r.iushrn(k);k-- >0;)(h.isOdd()||d.isOdd())&&(h.iadd(i),d.isub(l)),h.iushrn(1),d.iushrn(1);e.cmp(r)>=0?(e.isub(r),n.isub(h),a.isub(d)):(r.isub(e),h.isub(n),d.isub(a))}return{a:h,b:d,gcd:r.iushln(s)}},o.prototype._invmp=function(t){f(t.negative===0),f(!t.isZero());var e=this,r=t.clone();e.negative!==0?e=e.umod(t):e=e.clone();for(var n=new o(1),a=new o(0),h=r.clone();e.cmpn(1)>0&&r.cmpn(1)>0;){for(var d=0,s=1;(e.words[0]&s)===0&&d<26;++d,s<<=1);if(d>0)for(e.iushrn(d);d-- >0;)n.isOdd()&&n.iadd(h),n.iushrn(1);for(var i=0,l=1;(r.words[0]&l)===0&&i<26;++i,l<<=1);if(i>0)for(r.iushrn(i);i-- >0;)a.isOdd()&&a.iadd(h),a.iushrn(1);e.cmp(r)>=0?(e.isub(r),n.isub(a)):(r.isub(e),a.isub(n))}var C;return e.cmpn(1)===0?C=n:C=a,C.cmpn(0)<0&&C.iadd(t),C},o.prototype.gcd=function(t){if(this.isZero())return t.abs();if(t.isZero())return this.abs();var e=this.clone(),r=t.clone();e.negative=0,r.negative=0;for(var n=0;e.isEven()&&r.isEven();n++)e.iushrn(1),r.iushrn(1);do{for(;e.isEven();)e.iushrn(1);for(;r.isEven();)r.iushrn(1);var a=e.cmp(r);if(a<0){var h=e;e=r,r=h}else if(a===0||r.cmpn(1)===0)break;e.isub(r)}while(!0);return r.iushln(n)},o.prototype.invm=function(t){return this.egcd(t).a.umod(t)},o.prototype.isEven=function(){return(this.words[0]&1)===0},o.prototype.isOdd=function(){return(this.words[0]&1)===1},o.prototype.andln=function(t){return this.words[0]&t},o.prototype.bincn=function(t){f(typeof t=="number");var e=t%26,r=(t-e)/26,n=1<<e;if(this.length<=r)return this._expand(r+1),this.words[r]|=n,this;for(var a=n,h=r;a!==0&&h<this.length;h++){var d=this.words[h]|0;d+=a,a=d>>>26,d&=67108863,this.words[h]=d}return a!==0&&(this.words[h]=a,this.length++),this},o.prototype.isZero=function(){return this.length===1&&this.words[0]===0},o.prototype.cmpn=function(t){var e=t<0;if(this.negative!==0&&!e)return-1;if(this.negative===0&&e)return 1;this._strip();var r;if(this.length>1)r=1;else{e&&(t=-t),f(t<=67108863,"Number is too big");var n=this.words[0]|0;r=n===t?0:n<t?-1:1}return this.negative!==0?-r|0:r},o.prototype.cmp=function(t){if(this.negative!==0&&t.negative===0)return-1;if(this.negative===0&&t.negative!==0)return 1;var e=this.ucmp(t);return this.negative!==0?-e|0:e},o.prototype.ucmp=function(t){if(this.length>t.length)return 1;if(this.length<t.length)return-1;for(var e=0,r=this.length-1;r>=0;r--){var n=this.words[r]|0,a=t.words[r]|0;if(n!==a){n<a?e=-1:n>a&&(e=1);break}}return e},o.prototype.gtn=function(t){return this.cmpn(t)===1},o.prototype.gt=function(t){return this.cmp(t)===1},o.prototype.gten=function(t){return this.cmpn(t)>=0},o.prototype.gte=function(t){return this.cmp(t)>=0},o.prototype.ltn=function(t){return this.cmpn(t)===-1},o.prototype.lt=function(t){return this.cmp(t)===-1},o.prototype.lten=function(t){return this.cmpn(t)<=0},o.prototype.lte=function(t){return this.cmp(t)<=0},o.prototype.eqn=function(t){return this.cmpn(t)===0},o.prototype.eq=function(t){return this.cmp(t)===0},o.red=function(t){return new K(t)},o.prototype.toRed=function(t){return f(!this.red,"Already a number in reduction context"),f(this.negative===0,"red works only with positives"),t.convertTo(this)._forceRed(t)},o.prototype.fromRed=function(){return f(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},o.prototype._forceRed=function(t){return this.red=t,this},o.prototype.forceRed=function(t){return f(!this.red,"Already a number in reduction context"),this._forceRed(t)},o.prototype.redAdd=function(t){return f(this.red,"redAdd works only with red numbers"),this.red.add(this,t)},o.prototype.redIAdd=function(t){return f(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)},o.prototype.redSub=function(t){return f(this.red,"redSub works only with red numbers"),this.red.sub(this,t)},o.prototype.redISub=function(t){return f(this.red,"redISub works only with red numbers"),this.red.isub(this,t)},o.prototype.redShl=function(t){return f(this.red,"redShl works only with red numbers"),this.red.shl(this,t)},o.prototype.redMul=function(t){return f(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.mul(this,t)},o.prototype.redIMul=function(t){return f(this.red,"redMul works only with red numbers"),this.red._verify2(this,t),this.red.imul(this,t)},o.prototype.redSqr=function(){return f(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},o.prototype.redISqr=function(){return f(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},o.prototype.redSqrt=function(){return f(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},o.prototype.redInvm=function(){return f(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},o.prototype.redNeg=function(){return f(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},o.prototype.redPow=function(t){return f(this.red&&!t.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,t)};var E={k256:null,p224:null,p192:null,p25519:null};function L(u,t){this.name=u,this.p=new o(t,16),this.n=this.p.bitLength(),this.k=new o(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}L.prototype._tmp=function(){var t=new o(null);return t.words=new Array(Math.ceil(this.n/13)),t},L.prototype.ireduce=function(t){var e=t,r;do this.split(e,this.tmp),e=this.imulK(e),e=e.iadd(this.tmp),r=e.bitLength();while(r>this.n);var n=r<this.n?-1:e.ucmp(this.p);return n===0?(e.words[0]=0,e.length=1):n>0?e.isub(this.p):e.strip!==void 0?e.strip():e._strip(),e},L.prototype.split=function(t,e){t.iushrn(this.n,0,e)},L.prototype.imulK=function(t){return t.imul(this.k)};function Dt(){L.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}p(Dt,L),Dt.prototype.split=function(t,e){for(var r=4194303,n=Math.min(t.length,9),a=0;a<n;a++)e.words[a]=t.words[a];if(e.length=n,t.length<=9){t.words[0]=0,t.length=1;return}var h=t.words[9];for(e.words[e.length++]=h&r,a=10;a<t.length;a++){var d=t.words[a]|0;t.words[a-10]=(d&r)<<4|h>>>22,h=d}h>>>=22,t.words[a-10]=h,h===0&&t.length>10?t.length-=10:t.length-=9},Dt.prototype.imulK=function(t){t.words[t.length]=0,t.words[t.length+1]=0,t.length+=2;for(var e=0,r=0;r<t.length;r++){var n=t.words[r]|0;e+=n*977,t.words[r]=e&67108863,e=n*64+(e/67108864|0)}return t.words[t.length-1]===0&&(t.length--,t.words[t.length-1]===0&&t.length--),t};function z(){L.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}p(z,L);function Pt(){L.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}p(Pt,L);function kt(){L.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}p(kt,L),kt.prototype.imulK=function(t){for(var e=0,r=0;r<t.length;r++){var n=(t.words[r]|0)*19+e,a=n&67108863;n>>>=26,t.words[r]=a,e=n}return e!==0&&(t.words[t.length++]=e),t},o._prime=function(t){if(E[t])return E[t];var e;if(t==="k256")e=new Dt;else if(t==="p224")e=new z;else if(t==="p192")e=new Pt;else if(t==="p25519")e=new kt;else throw new Error("Unknown prime "+t);return E[t]=e,e};function K(u){if(typeof u=="string"){var t=o._prime(u);this.m=t.p,this.prime=t}else f(u.gtn(1),"modulus must be greater than 1"),this.m=u,this.prime=null}K.prototype._verify1=function(t){f(t.negative===0,"red works only with positives"),f(t.red,"red works only with red numbers")},K.prototype._verify2=function(t,e){f((t.negative|e.negative)===0,"red works only with positives"),f(t.red&&t.red===e.red,"red works only with red numbers")},K.prototype.imod=function(t){return this.prime?this.prime.ireduce(t)._forceRed(this):(M(t,t.umod(this.m)._forceRed(this)),t)},K.prototype.neg=function(t){return t.isZero()?t.clone():this.m.sub(t)._forceRed(this)},K.prototype.add=function(t,e){this._verify2(t,e);var r=t.add(e);return r.cmp(this.m)>=0&&r.isub(this.m),r._forceRed(this)},K.prototype.iadd=function(t,e){this._verify2(t,e);var r=t.iadd(e);return r.cmp(this.m)>=0&&r.isub(this.m),r},K.prototype.sub=function(t,e){this._verify2(t,e);var r=t.sub(e);return r.cmpn(0)<0&&r.iadd(this.m),r._forceRed(this)},K.prototype.isub=function(t,e){this._verify2(t,e);var r=t.isub(e);return r.cmpn(0)<0&&r.iadd(this.m),r},K.prototype.shl=function(t,e){return this._verify1(t),this.imod(t.ushln(e))},K.prototype.imul=function(t,e){return this._verify2(t,e),this.imod(t.imul(e))},K.prototype.mul=function(t,e){return this._verify2(t,e),this.imod(t.mul(e))},K.prototype.isqr=function(t){return this.imul(t,t.clone())},K.prototype.sqr=function(t){return this.mul(t,t)},K.prototype.sqrt=function(t){if(t.isZero())return t.clone();var e=this.m.andln(3);if(f(e%2===1),e===3){var r=this.m.add(new o(1)).iushrn(2);return this.pow(t,r)}for(var n=this.m.subn(1),a=0;!n.isZero()&&n.andln(1)===0;)a++,n.iushrn(1);f(!n.isZero());var h=new o(1).toRed(this),d=h.redNeg(),s=this.m.subn(1).iushrn(1),i=this.m.bitLength();for(i=new o(2*i*i).toRed(this);this.pow(i,s).cmp(d)!==0;)i.redIAdd(d);for(var l=this.pow(i,n),C=this.pow(t,n.addn(1).iushrn(1)),P=this.pow(t,n),k=a;P.cmp(h)!==0;){for(var F=P,N=0;F.cmp(h)!==0;N++)F=F.redSqr();f(N<k);var W=this.pow(l,new o(1).iushln(k-N-1));C=C.redMul(W),l=W.redSqr(),P=P.redMul(l),k=N}return C},K.prototype.invm=function(t){var e=t._invmp(this.m);return e.negative!==0?(e.negative=0,this.imod(e).redNeg()):this.imod(e)},K.prototype.pow=function(t,e){if(e.isZero())return new o(1).toRed(this);if(e.cmpn(1)===0)return t.clone();var r=4,n=new Array(1<<r);n[0]=new o(1).toRed(this),n[1]=t;for(var a=2;a<n.length;a++)n[a]=this.mul(n[a-1],t);var h=n[0],d=0,s=0,i=e.bitLength()%26;for(i===0&&(i=26),a=e.length-1;a>=0;a--){for(var l=e.words[a],C=i-1;C>=0;C--){var P=l>>C&1;if(h!==n[0]&&(h=this.sqr(h)),P===0&&d===0){s=0;continue}d<<=1,d|=P,s++,!(s!==r&&(a!==0||C!==0))&&(h=this.mul(h,n[d]),s=0,d=0)}i=26}return h},K.prototype.convertTo=function(t){var e=t.umod(this.m);return e===t?e.clone():e},K.prototype.convertFrom=function(t){var e=t.clone();return e.red=null,e},o.mont=function(t){return new Ot(t)};function Ot(u){K.call(this,u),this.shift=this.m.bitLength(),this.shift%26!==0&&(this.shift+=26-this.shift%26),this.r=new o(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)}p(Ot,K),Ot.prototype.convertTo=function(t){return this.imod(t.ushln(this.shift))},Ot.prototype.convertFrom=function(t){var e=this.imod(t.mul(this.rinv));return e.red=null,e},Ot.prototype.imul=function(t,e){if(t.isZero()||e.isZero())return t.words[0]=0,t.length=1,t;var r=t.imul(e),n=r.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),a=r.isub(n).iushrn(this.shift),h=a;return a.cmp(this.m)>=0?h=a.isub(this.m):a.cmpn(0)<0&&(h=a.iadd(this.m)),h._forceRed(this)},Ot.prototype.mul=function(t,e){if(t.isZero()||e.isZero())return new o(0)._forceRed(this);var r=t.mul(e),n=r.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),a=r.isub(n).iushrn(this.shift),h=a;return a.cmp(this.m)>=0?h=a.isub(this.m):a.cmpn(0)<0&&(h=a.iadd(this.m)),h._forceRed(this)},Ot.prototype.invm=function(t){var e=this.imod(t._invmp(this.m).mul(this.r2));return e._forceRed(this)}})(typeof mi>"u"||mi,Ii)});import{AggregatorClient as Qi,Env as jt}from"@cetusprotocol/aggregator-sdk";import{normalizeSuiAddress as Yi}from"@mysten/sui/utils";import{CetusDlmmSDK as ve}from"@cetusprotocol/dlmm-sdk";import{SdkWrapper as we}from"@cetusprotocol/common-sdk";import{FullRpcUrlMainnet as le,GraphRpcUrlMainnet as ue}from"@cetusprotocol/common-sdk";var Li={env:"mainnet",full_rpc_url:le,graph_rpc_url:ue,aggregator_url:"https://api-sui.cetus.zone/router_v3",providers:[]};import{FullRpcUrlTestnet as fe,GraphRpcUrlTestnet as he}from"@cetusprotocol/common-sdk";var Ei={env:"testnet",full_rpc_url:fe,graph_rpc_url:he,aggregator_url:"https://api-sui.devcetus.com/router_v3",providers:["CETUS","DEEPBOOK","KRIYA","KRIYAV3","FLOWX","FLOWXV3","AFTERMATH","TURBOS","HAEDAL","VOLO","AFSUI"]};var Hi=di(Nt(),1);import{Transaction as zi}from"@mysten/sui/transactions";import{CoinAssist as ci,d as I,DETAILS_KEYS as qt,fromDecimalsAmount as ji,toDecimalsAmount as Vi}from"@cetusprotocol/common-sdk";import{BaseError as de}from"@cetusprotocol/common-sdk";var Ki=(c=>(c.UnsupportedDepositMode="UnsupportedDepositMode",c.PositionIdUndefined="PositionIdUndefined",c.ParameterError="ParameterError",c.ReachMaxIterations="ReachMaxIterations",c.BestLiquidityIsZero="BestLiquidityIsZero",c.SwapAmountError="SwapAmountError",c.AggregatorError="AggregatorError",c))(Ki||{}),Zt=class extends de{constructor(m,f,p){super(m,f||"UnknownError",p)}static isZapErrorCode(m,f){return this.isErrorCode(m,f)}},Ni=(B,m,f)=>{throw new Zt(m.message,B,f)},Lt=(B,m,f)=>{throw new Zt(m,B,f)};var pi=.005;var Et=di(Nt(),1);import{ClmmPoolUtil as me,d as Rt,TickMath as pe}from"@cetusprotocol/common-sdk";function Zi(B,m,f,p,o,v,b){let c=b?new Et.default(B):new Et.default(m),_=me.estLiquidityAndCoinAmountFromOneAmounts(p,o,c,b,!0,v,new Et.default(f)),M=b?c.toString():_.coin_amount_a.toString(),x=b?_.coin_amount_b.toString():c.toString(),A=Rt(B).sub(M),y=Rt(m).sub(x);return{is_enough_amount:b?y.gte(0):A.gte(0),use_amount_a:M,use_amount_b:x,liquidity:_.liquidity_amount,amount_limit_a:_.coin_amount_limit_a,amount_limit_b:_.coin_amount_limit_b,remain_amount:b?y:A}}function Oe(B,m,f,p,o,v,b){let c=pe.sqrtPriceX64ToTickIndex(new Et.default(f)),_=b;c<p?_=!0:c>o&&(_=!1);let M=Zi(B,m,f,p,o,v,_);return M.is_enough_amount||(_=!_,M=Zi(B,m,f,p,o,v,_)),{fix_liquidity_amount_a:_,...M}}function Wi(B,m,f,p){let o=Rt(B),v=Rt(m),b=Rt(f),c=Rt(p);if(o.eq(0)&&v.eq(0))return{swap_amount:"0",final_amount_a:"0",final_amount_b:"0",a_to_b:!0};let _;if(o.eq(0)?_=!1:v.eq(0)?_=!0:_=v.div(o).lt(c),c.eq(0)){if(!_){let D=o.plus(v.div(b));return{swap_amount:v.toFixed(0),final_amount_a:D.toFixed(0),final_amount_b:"0",a_to_b:_}}return{swap_amount:"0",final_amount_a:o.toFixed(0),final_amount_b:v.toFixed(0),a_to_b:_}}let M=_?o:v,x=c,A=c.mul(.999),y=Rt(0),g=M,w=null;function S(D){if(_){let E=o.minus(D),L=v.plus(D.mul(b));return{A:E,B:L,R:E.gt(0)?L.div(E):Rt(Number.MAX_SAFE_INTEGER)}}let O=o.plus(D.div(b)),q=v.minus(D);return{A:O,B:q,R:O.gt(0)?q.div(O):Rt(0)}}for(let D=0;D<120;D++){let O=y.plus(g).div(2),{A:q,B:E,R:L}=S(O);if(q.lte(0)||E.lte(0)){g=O;continue}if(L.gt(x))_?g=O:y=O;else if(L.lt(A))_?y=O:g=O;else{if(w=O,A.eq(0))break;g=O}}if(!w)return{swap_amount:"0",final_amount_a:o.toFixed(0),final_amount_b:v.toFixed(0),a_to_b:_};let{A:T,B:R}=S(w);return{swap_amount:w.toFixed(0),final_amount_a:T.toFixed(0),final_amount_b:R.toFixed(0),a_to_b:_}}function Ui(B,m,f,p){let o=Rt(B),v=Rt(f),b=Rt(p),c=o,_=m?o:Rt(0),M=m?Rt(0):o,x=b,A=Rt(b).mul(.999),y=Rt(0),g=c,w=null;function S(D){if(m){let O=_.minus(D),q=M.plus(D.mul(v));return{A:O,B:q,R:q.div(O)}}else{let O=_.plus(D.mul(v)),q=M.minus(D);return{A:O,B:q,R:O.div(q)}}}for(let D=0;D<120;D++){let O=y.plus(g).div(2),{A:q,B:E,R:L}=S(O);if(q.lte(0)||E.lte(0)){g=O;continue}if(L.gt(x))g=O;else if(L.lt(A))y=O;else{if(w=O,Rt(A).eq(0))break;g=O}}if(!w)return{swap_amount:"0",final_amount_a:_.toFixed(0),final_amount_b:M.toFixed(0)};let{A:T,B:R}=S(w);return{swap_amount:w.toFixed(0),final_amount_a:T.toFixed(0),final_amount_b:R.toFixed(0)}}import{BinUtils as _i}from"@cetusprotocol/dlmm-sdk";var Wt=class{constructor(m){this._sdk=m}get sdk(){return this._sdk}async calculateBalanceSwapAmountWithoutActiveId(m,f){let{fix_amount_a:p,coin_amount:o}=f,{bin_step:v,pool_id:b,active_id:c,lower_bin_id:_,upper_bin_id:M,active_bin_of_pool:x,strategy_type:A}=m,y=await this._sdk.DlmmSDK.Pool.getPool(b,!1),g,w=p,S=o;return c>M?(w=!1,p?(g=await this.findRouters(y.coin_type_a,y.coin_type_b,o),S=I(g.swap_out_amount).mul(1-.001).toFixed(0)):S=o):c<_&&(w=!0,p?S=o:(g=await this.findRouters(y.coin_type_b,y.coin_type_a,o),S=I(g.swap_out_amount).mul(1-.001).toFixed(0))),{liquidity_info:await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:b,coin_amount:S,fix_amount_a:w,active_id:c,bin_step:v,lower_bin_id:_,upper_bin_id:M,active_bin_of_pool:x,strategy_type:A}),swap_result:g}}async calculateBalanceSwapAmount(m,f){let{fix_amount_a:p,coin_amount:o}=f,{bin_step:v,pool_id:b,active_id:c,lower_bin_id:_,upper_bin_id:M,active_bin_of_pool:x,strategy_type:A}=m;if(c>M||c<_)return this.calculateBalanceSwapAmountWithoutActiveId(m,f);let y=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:b,coin_amount:o,fix_amount_a:p,active_id:c,bin_step:v,lower_bin_id:_,upper_bin_id:M,active_bin_of_pool:x,strategy_type:A}),{amount_a:g,amount_b:w}=y,S=p?I(w).div(g):I(g).div(w),T=p?_i.getPricePerLamportFromBinId(c,v):I(1).div(_i.getPricePerLamportFromBinId(c,v)).toString(),R=await this._sdk.DlmmSDK.Pool.getPool(b,!1);try{let t=await this.findRouters(p?R.coin_type_a:R.coin_type_b,p?R.coin_type_b:R.coin_type_a,o);t&&(T=I(t.swap_out_amount).div(t.swap_in_amount).toString())}catch{}let{swap_amount:D,final_amount_a:O,final_amount_b:q}=Ui(o,p,T,S.toString());console.log("\u{1F680} ~ ZapModule ~ swap_amount:",{swap_amount:D,final_amount_a:O,final_amount_b:q,target_rate:S,real_price:T,coin_amount:o,fix_amount_a:p});let E=D||"0",L,Dt,z,Pt=0,kt=.02,K,Ot,u;do{let t=I(o).sub(E).toFixed(0);z=await this.findRouters(p?R.coin_type_a:R.coin_type_b,p?R.coin_type_b:R.coin_type_a,E);let e=I(z.swap_out_amount).toFixed(0),r=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:b,coin_amount:t,fix_amount_a:p,active_id:c,bin_step:v,lower_bin_id:_,upper_bin_id:M,active_bin_of_pool:x,strategy_type:A}),{amount_a:n,amount_b:a}=r,h=p?a:n,d=I(e).sub(h).div(e);if(d.gt(0)&&(u===void 0||d.lt(u))&&(K=z,Ot=r,u=d),I(e).gt(h))if(I(d).gt(kt))console.log("calculateBalanceSwapAmount -: ",{swap_amount_in:E,real_remain_rate:d.toString(),cached_real_remain_rate:u?.toString(),temp_swap_result:z,deposit_amount_other:h,count:Pt}),E=I(E).mul(1-.01).toFixed(0),Pt++;else{L=z,Dt=r;break}else E=I(E).mul(1+.01).toFixed(0),E=Math.min(Number(I(o).mul(1-1e-4).toFixed(0)),Number(E)).toString(),console.log("calculateBalanceSwapAmount +: ",{swap_amount_in:E,real_remain_rate:d.toString(),cached_real_remain_rate:u?.toString(),temp_swap_result:z,deposit_amount_other:h,count:Pt}),Pt++;if(Pt>5)break}while(!L&&I(E).gt(0));if(L===void 0&&K&&(L=K,Dt=Ot),L===void 0&&z){let{swap_out_amount:t}=z,e=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:b,coin_amount:I(t).sub(1-.001).toFixed(0),fix_amount_a:!p,active_id:c,bin_step:v,lower_bin_id:_,upper_bin_id:M,active_bin_of_pool:x,strategy_type:A});L=z,Dt=e}return{swap_result:L,liquidity_info:Dt}}async preCalculateDepositAmount(m,f){let{fix_amount_a:p,coin_amount:o}=f;console.log("\u{1F680} ~ ZapModule ~ preCalculateDepositAmount ~ options:",{options:m,mode_options:f});let{liquidity_info:v,swap_result:b}=await this.calculateBalanceSwapAmount(m,f);return v===void 0?Lt("SwapAmountError","Best liquidity info is undefined",{[qt.METHOD_NAME]:"preCalculateDepositAmount"}):{bin_infos:v,swap_result:b,fix_amount_a:p,coin_amount:o}}async buildDepositPayload(m,f){let{swap_slippage:p=pi,bin_step:o,pool_id:v,active_id:b,lower_bin_id:c,upper_bin_id:_,strategy_type:M,deposit_obj:x,slippage:A,pos_obj:y}=m,{fix_amount_a:g,coin_amount:w,swap_result:S,bin_infos:T}=x;f=f||new zi;let R=await this._sdk.DlmmSDK.Pool.getPool(v,!1),{coin_type_a:D,coin_type_b:O}=R,q,E;if(S){let L=g?D:O,{swap_in_amount:Dt,route_obj:z}=S,Pt=ci.buildCoinWithBalance(BigInt(Dt),L,f),kt={router:z,slippage:p,txb:f,inputCoin:Pt},K=await this._sdk.AggregatorClient.fixableRouterSwapV3(kt);g?(q=ci.buildCoinWithBalance(BigInt(T.amount_a),D,f),E=K):(q=K,E=ci.buildCoinWithBalance(BigInt(T.amount_b),O,f))}if(y){let L={pool_id:v,bin_infos:T,coin_type_a:D,coin_type_b:O,active_id:b,strategy_type:M,max_price_slippage:A,bin_step:o,use_bin_infos:!1,position_id:y.pos_id,collect_fee:y.collect_fee,reward_coins:y.collect_rewarder_types,coin_object_id_a:q,coin_object_id_b:E};this._sdk.DlmmSDK.Position.addLiquidityPayload(L,f)}else{let L={pool_id:v,bin_infos:T,coin_type_a:D,coin_type_b:O,lower_bin_id:c,upper_bin_id:_,active_id:b,strategy_type:M,use_bin_infos:!1,max_price_slippage:A,bin_step:o,coin_object_id_a:q,coin_object_id_b:E};this._sdk.DlmmSDK.Position.addLiquidityPayload(L,f)}return f}async findRouters(m,f,p){let{providers:o}=this._sdk.sdkOptions,v=this._sdk.AggregatorClient;if(I(p).lt(1))return Lt("SwapAmountError","Swap amount is less than the minimum precision",{[qt.METHOD_NAME]:"findRouters"});try{let b={from:m,target:f,amount:new Hi.default(p),byAmountIn:!0,depth:3,providers:o},c=await v.findRouters(b);if(c?.error)return Lt("AggregatorError",`Aggregator findRouters error: ${c?.error}`,{[qt.METHOD_NAME]:"findRouters",[qt.REQUEST_PARAMS]:b});if(!c?.paths||c?.paths?.length===0)return Lt("AggregatorError","Aggregator findRouters error: no router",{[qt.METHOD_NAME]:"findRouters",[qt.REQUEST_PARAMS]:b});let _=c.amountIn.toString(),M=c.amountOut.toString();return{swap_in_amount:_,swap_out_amount:M,route_obj:c}}catch(b){return console.log("\u{1F680} ~ ZapModule ~ error:",JSON.stringify(b,null,2)),Ni("AggregatorError",b,{[qt.METHOD_NAME]:"swapInPools"})}}calculateZapOutAvailableAmount(m){let{is_receive_coin_a:f,mode:p,active_id:o,bin_step:v,remove_bin_range:b,prices:c,coin_decimal_a:_,coin_decimal_b:M}=m,x;if(c){let{coin_a_price:O,coin_b_price:q}=c;x=I(O).div(q).toString()}else x=_i.getPriceFromBinId(o,v,_,M);let A,y="0",g="0",w="0";b.forEach(O=>{O.bin_id===o&&(A=O),y=I(y).add(O.amount_a).toFixed(0),g=I(g).add(O.amount_b).toFixed(0)});let S=I(y).sub(A?.amount_a||"0").toFixed(0),T=I(g).sub(A?.amount_b||"0").toFixed(0),R=I(Vi(I(ji(y,_)).mul(x).toString(),M)).toFixed(0),D=I(Vi(I(ji(g,M)).div(x).toString(),_)).toFixed(0);return p==="OnlyCoinA"?f?w=S:w=R:p==="OnlyCoinB"?(g=T,f?w=D:w=g):p==="Both"&&(f?w=I(D).add(y).toFixed(0):w=I(R).add(g).toFixed(0)),{available_amount:w,user_total_amount_a:y,user_total_amount_b:g,active_bin:A,is_receive_coin_a:f}}async preCalculateWithdrawAmount(m){let{is_receive_coin_a:f,remove_bin_range:p,coin_type_a:o,coin_type_b:v,mode:b,expected_receive_amount:c,active_id:_}=m,{available_amount:M,user_total_amount_a:x,user_total_amount_b:A,active_bin:y}=this.calculateZapOutAvailableAmount(m);if(I(M).lt(I(c)))return Lt("SwapAmountError",`Available amount is less than the expected receive amount: ${M} < ${c}`,{[qt.METHOD_NAME]:"preCalculateWithdrawAmount",[qt.REQUEST_PARAMS]:m});let g=I(c).div(M),w;if(b==="Both"){let D=I(x).gt(0),O=D?I(x).mul(g).toFixed(0):I(A).mul(g).toFixed(0);w=this._sdk.DlmmSDK.Position.calculateRemoveLiquidityInfo({bins:p,active_id:_,fix_amount_a:D,coin_amount:O})}else{let D=b==="OnlyCoinA"?I(x).mul(g).toFixed(0):I(A).mul(g).toFixed(0);w=this._sdk.DlmmSDK.Position.calculateRemoveLiquidityInfo({bins:p,active_id:_,is_only_a:b==="OnlyCoinA",coin_amount:D})}let{amount_a:S,amount_b:T}=w,R;return f&&I(T).gt(0)&&(R=await this.findRouters(v,o,T)),!f&&I(S).gt(0)&&(R=await this.findRouters(o,v,S)),{remove_liquidity_info:w,swap_result:R,mode:b,is_receive_coin_a:f,expected_receive_amount:c,remove_percent:g.toString()}}async buildWithdrawPayload(m){let f=new zi,{withdraw_obj:p,swap_slippage:o=pi,bin_step:v,pool_id:b,active_id:c,collect_fee:_,reward_coins:M,coin_type_a:x,coin_type_b:A,position_id:y,slippage:g,remove_percent:w,is_close_position:S}=m,{remove_liquidity_info:T,swap_result:R,is_receive_coin_a:D}=p,O={pool_id:b,bin_infos:T,coin_type_a:x,coin_type_b:A,position_id:y,slippage:g,active_id:c,reward_coins:M,collect_fee:_,bin_step:v,remove_percent:Number(w)},{coin_a_obj:q,coin_b_obj:E}=S?this._sdk.DlmmSDK.Position.closePositionNoTransferPayload(O,f):this._sdk.DlmmSDK.Position.removeLiquidityNoTransferPayload(O,f);if(R){let{route_obj:L}=R,Dt={router:L,slippage:o,txb:f,inputCoin:D?E:q},z=await this._sdk.AggregatorClient.fixableRouterSwapV3(Dt);D?f.transferObjects([q,z],this._sdk.getSenderAddress()):f.transferObjects([E,z],this._sdk.getSenderAddress())}else f.transferObjects([q,E],this._sdk.getSenderAddress());return f}};import{Transaction as Kt}from"@mysten/sui/transactions";import{d as V,fixCoinType as Z,toDecimalsAmount as ce}from"@cetusprotocol/common-sdk";import{BinUtils as _e}from"@cetusprotocol/dlmm-sdk";var It=di(Nt(),1);import{ClmmPoolUtil as Gi,d as Ji,fixCoinType as Ut}from"@cetusprotocol/common-sdk";function mr({current_sqrt_price:B,tick_lower:m,tick_upper:f,amount_a:p,amount_b:o,slippage:v}){let b=new It.default(p),c=Gi.estLiquidityAndCoinAmountFromOneAmounts(m,f,b,!0,!0,v,new It.default(B)),_=b.toString(),M=c.coin_amount_b.toString(),x=Ji(o).sub(M);if(x.gte(0))return{liquidity:c.liquidity_amount,use_amount_a:_,use_amount_b:M,amount_limit_a:c.coin_amount_limit_a,amount_limit_b:c.coin_amount_limit_b,fix_amount_a:!0,remain_amount:x,is_enough_amount:!0};let y=new It.default(o);console.log("\u{1F680} ~ calculateLiquidityWithFallback ~ fixAmountB:",y.toString());let g=Gi.estLiquidityAndCoinAmountFromOneAmounts(m,f,y,!1,!0,v,new It.default(B));console.log("\u{1F680} ~ calculateLiquidityWithFallback ~ liquidityInputB:",JSON.stringify(g,null,2));let w=g.coin_amount_a.toString(),S=y.toString(),T=Ji(p).sub(w),R=T.gte(0);return{liquidity:g.liquidity_amount,use_amount_a:w,use_amount_b:S,amount_limit_a:g.coin_amount_limit_a,amount_limit_b:g.coin_amount_limit_b,fix_amount_a:!1,remain_amount:T,is_enough_amount:R}}function gi(B){return B===null||typeof B!="object"?!0:Object.keys(B).length===0}function Bt(B,m){return!B.map(p=>Ut(p)).includes(Ut(m))}function Ft(B,m){return Ut(B)===Ut(m)}var ge=.005,Xi=3,zt=class{constructor(m){this._sdk=m}get sdk(){return this._sdk}calculateAmountWithSlippage(m,f){return V(m).mul(V(1-f)).floor().toString()}async fetchFeeAndRewardSnapshot(m){let{feeData:f,rewardData:p}=await this._sdk.DlmmSDK.Position.fetchPositionFeeAndReward([{pool_id:m.pool_id,position_id:m.position_id,coin_type_a:m.coin_type_a,coin_type_b:m.coin_type_b,reward_coins:m.rewarder_types||[]}]);return{fee:f[m.position_id],reward:p[m.position_id]}}async getFeeAndReward(m){let{pool_id:f,position_id:p,coin_type_a:o,coin_type_b:v,rewarder_types:b,merge_swap_target_coin_type:c,not_merge_coins:_}=m,M="0",x="0",A={},y=await this.fetchFeeAndRewardSnapshot({pool_id:f,position_id:p,coin_type_a:o,coin_type_b:v,rewarder_types:b});y.fee&&(M=Bt(_,o)?y.fee.fee_owned_a:"0",x=Bt(_,v)?y.fee.fee_owned_b:"0"),y.reward?.rewards?.forEach(w=>{let S=Z(w.coin_type);S===Z(o)&&Bt(_,o)?M=V(M).add(w.reward_owned).toString():S===Z(v)&&Bt(_,v)?x=V(x).add(w.reward_owned).toString():S!==Z(c)&&Bt(_,S)&&(A[S]=w.reward_owned)});let g;if(!gi(A)&&(g=await this._sdk.AggregatorClient.findMergeSwapRouters({target:c,byAmountIn:!0,depth:Xi,froms:Object.entries(A).map(([w,S])=>({coinType:w,amount:S}))}),g?.totalAmountOut)){let w=this.calculateAmountWithSlippage(g.totalAmountOut.toString(),ge);Z(c)===Z(o)?M=V(M).add(w).toString():x=V(x).add(w).toString()}return{coin_amount_a:M,coin_amount_b:x,merge_routers:g}}async calculateClaimMerge(m){let{coin_type_a:f,coin_type_b:p,pool_id:o,position_id:v,rewarder_types:b,target_coin_type:c,not_merge_coins:_}=m,M={},x=new Set(_.map(w=>Z(w))),A=(w,S)=>{if(V(S).lte(0))return;let T=Z(w);if(!x.has(T)){if(!M[T]){M[T]={coinType:w,amount:S};return}M[T].amount=V(M[T].amount).add(S).toString()}},y=await this.fetchFeeAndRewardSnapshot({pool_id:o,position_id:v,coin_type_a:f,coin_type_b:p,rewarder_types:b});y.fee&&(A(f,y.fee.fee_owned_a),A(p,y.fee.fee_owned_b)),y.reward?.rewards?.forEach(w=>{A(w.coin_type,w.reward_owned)});let g=Object.values(M);return g.length===0?null:this._sdk.AggregatorClient.findMergeSwapRouters({target:c,byAmountIn:!0,depth:Xi,froms:g})}async calculateRebalance(m){let{pool_id:f,amount_a:p,amount_b:o,active_id:v,bin_step:b,lower_bin_id:c,upper_bin_id:_,strategy_type:M,active_bin_of_pool:x,slippage:A}=m,y=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:f,amount_a:p,amount_b:o,active_id:v,bin_step:b,lower_bin_id:c,upper_bin_id:_,active_bin_of_pool:x,strategy_type:M}),g=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:f,coin_amount:ce(100,9).toString(),fix_amount_a:v<_,active_id:v,bin_step:b,lower_bin_id:c,upper_bin_id:_,active_bin_of_pool:x,strategy_type:M}),w=V(g.amount_a),S=V(g.amount_b),T=_e.getPricePerLamportFromBinId(v,b),R;if(w.eq(0)&&S.eq(0))return{bin_infos:y,lower_bin_id:c,upper_bin_id:_,use_amount_a:y.amount_a,use_amount_b:y.amount_b,fix_amount_a:V(y.amount_a).gte(y.amount_b),remain_amount:"0"};if(w.eq(0))R={a_to_b:!0,swap_amount:p};else if(S.eq(0))R={a_to_b:!1,swap_amount:o};else{let K=S.div(w).toString();R=Wi(p,o,T,K)}if(V(R.swap_amount).lte(0))return{bin_infos:y,lower_bin_id:c,upper_bin_id:_,use_amount_a:y.amount_a,use_amount_b:y.amount_b,fix_amount_a:V(y.amount_a).gte(y.amount_b),remain_amount:"0"};let D=R.a_to_b?m.coin_type_a:m.coin_type_b,O=R.a_to_b?m.coin_type_b:m.coin_type_a,q=await this._sdk.Zap.findRouters(D,O,R.swap_amount),E=this.calculateAmountWithSlippage(q.swap_out_amount,A),L=R.a_to_b?V(p).sub(q.swap_in_amount).toFixed(0):V(p).add(E).toFixed(0),Dt=R.a_to_b?V(o).add(E).toFixed(0):V(o).sub(q.swap_in_amount).toFixed(0),z=await this._sdk.DlmmSDK.Position.calculateAddLiquidityInfo({pool_id:f,amount_a:L,amount_b:Dt,active_id:v,bin_step:b,lower_bin_id:c,upper_bin_id:_,active_bin_of_pool:x,strategy_type:M}),Pt=V(L).sub(z.amount_a).abs(),kt=V(Dt).sub(z.amount_b).abs();return{bin_infos:z,lower_bin_id:c,upper_bin_id:_,use_amount_a:z.amount_a,use_amount_b:z.amount_b,fix_amount_a:V(z.amount_a).gte(z.amount_b),remain_amount:Pt.lt(kt)?Pt.toFixed(0):kt.toFixed(0),swap_result:q}}async preSwap(m){let{from_coin_type:f,target_coin_type:p,amount:o,is_a2b:v,slippage:b}=m,c=await this._sdk.Zap.findRouters(f,p,o),_=this.calculateAmountWithSlippage(c.swap_out_amount,b),M=v?V(c.swap_out_amount).div(c.swap_in_amount).toString():V(c.swap_in_amount).div(c.swap_out_amount).toString();return{swapResult:c,routerPrice:M,swapAmountOutWithSlippage:_}}async collectFeeAndRewardsAndReturnCoins(m,f){let{pool_id:p,pos_id:o,coin_type_a:v,coin_type_b:b,rewarder_coin_types:c}=m;this._sdk.DlmmSDK.Position.updatePositionFeeAndRewards({pool_id:p,position_id:o,coin_type_a:v,coin_type_b:b},f);let _=this._sdk.DlmmSDK.Position.collectFeePayloadWithoutTransfer({pool_id:p,position_id:o,coin_type_a:v,coin_type_b:b},f),M=_.fee_a_obj,x=_.fee_b_obj,A=this._sdk.DlmmSDK.Position.collectRewardPayloadWithoutTransfer({pool_id:p,position_id:o,coin_type_a:v,coin_type_b:b,reward_coins:c||[]},f),y={};return A.reward_coin_objs.forEach((g,w)=>{let S=c[w];Ft(S,v)?f.mergeCoins(M,[g]):Ft(S,b)?f.mergeCoins(x,[g]):y[S]=g}),{fee_a:M,fee_b:x,other_rewarder:y}}async claimFeeAndRewardsAndMergeRewards(m,f,p=new Kt){let{coin_type_a:o,coin_type_b:v}=m,{fee_a:b,fee_b:c,other_rewarder:_}=await this.collectFeeAndRewardsAndReturnCoins(m,p),M=b,x=c,A={};for(let y in _){let g=_[y];f&&Bt(f.not_merge_coins,y)?Z(y)===Z(o)?p.mergeCoins(M,[g]):Z(y)===Z(v)?p.mergeCoins(x,[g]):A[Z(y)]={coinType:y,coin:g}:Z(o)===Z(y)?p.mergeCoins(M,[g]):Z(v)===Z(y)?p.mergeCoins(x,[g]):p.transferObjects([g],this.sdk.getSenderAddress())}if(!gi(A)&&f?.merge_routers){let y=f.merge_routers.allRoutes?.map(S=>A[Z(S.paths[0].from)]),g=await this._sdk.AggregatorClient.mergeSwap({router:f.merge_routers,inputCoins:y,slippage:f.slippage,txb:p}),w=f.merge_routers.allRoutes[0].paths[f.merge_routers.allRoutes[0].paths.length-1].target;Z(w)===Z(o)?p.mergeCoins(M,[g]):p.mergeCoins(x,[g])}return{coin_a:M,coin_b:x}}async closePosOnlyReturnAmountCoins(m,f){let{pool_id:p,pos_id:o,coin_type_a:v,coin_type_b:b,rewarder_coin_types:c,close_position:_=!0}=m,{fee_a:M,fee_b:x,other_rewarder:A}=await this.collectFeeAndRewardsAndReturnCoins(m,f);f.transferObjects([M,x],this.sdk.getSenderAddress());for(let w in A)f.transferObjects([A[w]],this.sdk.getSenderAddress());if(_){let{coin_a_obj:w,coin_b_obj:S}=this._sdk.DlmmSDK.Position.closePositionNoTransferPayload({pool_id:p,position_id:o,reward_coins:c,coin_type_a:v,coin_type_b:b},f);return{coin_a:w,coin_b:S}}let{coin_a_obj:y,coin_b_obj:g}=this._sdk.DlmmSDK.Position.removeLiquidityNoTransferPayload({pool_id:p,position_id:o,active_id:m.active_id,bin_step:m.bin_step,bin_infos:m.bin_infos,slippage:m.slippage,reward_coins:c,collect_fee:!1,remove_percent:m.remove_percent,coin_type_a:v,coin_type_b:b},f);return{coin_a:y,coin_b:g}}async closePosReturnCoinWithMerge(m,f,p=new Kt){let{coin_type_a:o,coin_type_b:v,pool_id:b,pos_id:c,close_position:_=!0}=m,{coin_a:M,coin_b:x}=await this.claimFeeAndRewardsAndMergeRewards(m,f,p),A=_?this._sdk.DlmmSDK.Position.closePositionNoTransferPayload({pool_id:b,position_id:c,reward_coins:m.rewarder_coin_types,coin_type_a:o,coin_type_b:v},p):this._sdk.DlmmSDK.Position.removeLiquidityNoTransferPayload({pool_id:b,position_id:c,active_id:m.active_id,bin_step:m.bin_step,bin_infos:m.bin_infos,slippage:m.slippage,reward_coins:m.rewarder_coin_types,collect_fee:!1,remove_percent:m.remove_percent,coin_type_a:o,coin_type_b:v},p);return p.mergeCoins(M,[A.coin_a_obj]),p.mergeCoins(x,[A.coin_b_obj]),{coin_a:M,coin_b:x}}async createClaimMergePayload(m){let{coin_type_a:f,coin_type_b:p,not_merge_coins:o,merge_routers:v,slippage:b,target_coin_type:c}=m,_=new Kt,{fee_a:M,fee_b:x,other_rewarder:A}=await this.collectFeeAndRewardsAndReturnCoins(m,_),y={},g,w;M&&Bt(o,f)&&!Ft(c,f)?g=M:M&&_.transferObjects([M],this.sdk.getSenderAddress()),x&&Bt(o,p)&&!Ft(c,p)?w=x:x&&_.transferObjects([x],this.sdk.getSenderAddress());for(let R in A){let D=A[R];Bt(o,R)&&!Ft(c,R)?Ft(R,f)?g?_.mergeCoins(g,[D]):g=D:Ft(R,p)?w?_.mergeCoins(w,[D]):w=D:y[Z(R)]={coinType:R,coin:D}:_.transferObjects([D],this.sdk.getSenderAddress())}g&&(y[Z(f)]={coinType:f,coin:g}),w&&(y[Z(p)]={coinType:p,coin:w});let S=v.allRoutes?.map(R=>y[Z(R.paths[0].from)]),T=await this._sdk.AggregatorClient.mergeSwap({router:v,inputCoins:S,slippage:b,txb:_});return _.transferObjects([T],this.sdk.getSenderAddress()),_}async createCompoundRebalanceAddPayload(m){let f=m.tx||new Kt,{baseParams:p,rebalancePre:o,rewarderMergeOption:v}=m,{pool_id:b,pos_id:c,coin_type_a:_,coin_type_b:M,active_id:x,bin_step:A,strategy_type:y}=p,{slippage:g}=v,{swap_result:w,bin_infos:S}=o,{coin_a:T,coin_b:R}=await this.claimFeeAndRewardsAndMergeRewards(p,v,f);if(w?.route_obj){let D=Z(w.route_obj.paths[0].from)===Z(_),O=D?f.splitCoins(T,[f.pure.u64(w.swap_in_amount)]):f.splitCoins(R,[f.pure.u64(w.swap_in_amount)]),q=await this._sdk.AggregatorClient.fixableRouterSwapV3({router:w.route_obj,slippage:g,txb:f,inputCoin:O});D?f.mergeCoins(R,[q]):f.mergeCoins(T,[q])}return this._sdk.DlmmSDK.Position.addLiquidityPayload({pool_id:b,position_id:c,coin_type_a:_,coin_type_b:M,bin_infos:S,active_id:x,bin_step:A,strategy_type:y,max_price_slippage:g,collect_fee:!1,reward_coins:[],use_bin_infos:!1,coin_object_id_a:T,coin_object_id_b:R},f),f}async createMovePositionPayload(m,f=new Kt){let{newPos:p,oldPos:o,rebalancePre:v,slippage:b,rewarderMergeOption:c,have_claim:_}=m,{pool_id:M,pos_id:x,coin_type_a:A,coin_type_b:y,rewarder_coin_types:g}=o,{swap_result:w,bin_infos:S}=v,T=_?await this.closePosOnlyReturnAmountCoins({pool_id:M,pos_id:x,coin_type_a:A,coin_type_b:y,rewarder_coin_types:g,active_id:o.active_id,bin_step:o.bin_step,slippage:o.slippage,bin_infos:o.bin_infos,remove_percent:o.remove_percent,close_position:o.close_position??!0},f):await this.closePosReturnCoinWithMerge({pool_id:M,pos_id:x,coin_type_a:A,coin_type_b:y,rewarder_coin_types:g,active_id:o.active_id,bin_step:o.bin_step,slippage:o.slippage,bin_infos:o.bin_infos,remove_percent:o.remove_percent,close_position:o.close_position??!0},c,f),R=T.coin_a,D=T.coin_b;if(w?.route_obj){let O=Z(w.route_obj.paths[0].from)===Z(A),q=O?f.splitCoins(R,[f.pure.u64(w.swap_in_amount)]):f.splitCoins(D,[f.pure.u64(w.swap_in_amount)]),E=await this._sdk.AggregatorClient.fixableRouterSwapV3({router:w.route_obj,slippage:b,txb:f,inputCoin:q});O?f.mergeCoins(D,[E]):f.mergeCoins(R,[E])}return this._sdk.DlmmSDK.Position.addLiquidityPayload({pool_id:M,coin_type_a:A,coin_type_b:y,lower_bin_id:p.lower_bin_id,upper_bin_id:p.upper_bin_id,active_id:p.active_id,bin_step:p.bin_step,strategy_type:p.strategy_type,active_bin_of_pool:p.active_bin_of_pool,max_price_slippage:b,use_bin_infos:!1,bin_infos:S,coin_object_id_a:f.splitCoins(R,[f.pure.u64(S.amount_a)]),coin_object_id_b:f.splitCoins(D,[f.pure.u64(S.amount_b)])},f),f.transferObjects([R,D],this.sdk.getSenderAddress()),f}};import{SuiJsonRpcClient as $i}from"@mysten/sui/jsonRpc";var Vt=class B extends we{constructor(m,f){super(m),this._zapModule=new Wt(this),this._compoundModule=new zt(this),this._dlmmSDK=f||ve.createSDK({env:m.env,full_rpc_url:m.full_rpc_url}),this._aggregatorClient=this.createAggregatorClient()}createAggregatorClient(){return new Qi({signer:Yi("0x0"),client:this._sdkOptions.sui_client||new $i({url:this._sdkOptions.full_rpc_url,network:this._sdkOptions.env==="testnet"?"testnet":"mainnet"}),env:this._sdkOptions.env==="testnet"?jt.Testnet:jt.Mainnet,pythUrls:this._sdkOptions.pyth_urls})}updatePythUrls(m){if(m.length===0)throw new Error("pythUrls is empty");this._sdkOptions.pyth_urls=m,this._aggregatorClient=this.createAggregatorClient()}setSenderAddress(m){this._dlmmSDK.setSenderAddress(m)}getSenderAddress(m=!0){return this._dlmmSDK.getSenderAddress(m)}updateProviders(m){if(m.length===0)throw new Error("providers is empty");this._sdkOptions.providers=m}updateFullRpcUrl(m){super.updateFullRpcUrl(m),this._dlmmSDK.updateFullRpcUrl(m),this._aggregatorClient=new Qi({signer:Yi("0x0"),client:new $i({url:m,network:this._sdkOptions.env==="testnet"?"testnet":"mainnet"}),env:this._sdkOptions.env==="testnet"?jt.Testnet:jt.Mainnet,pythUrls:this._sdkOptions.pyth_urls})}get DlmmSDK(){return this._dlmmSDK}get AggregatorClient(){return this._aggregatorClient}get Zap(){return this._zapModule}get Compound(){return this._compoundModule}static createSDK(m,f){let{env:p="mainnet",full_rpc_url:o}=m;return p==="mainnet"?B.createCustomSDK({...Li,...m},f):B.createCustomSDK({...Ei,...m},f)}static createCustomSDK(m,f){return new B(m,f)}};var Xr=Vt;export{Vt as CetusDlmmZapSDK,zt as CompoundModule,Zt as ZapError,Ki as ZapErrorCode,Wt as ZapModule,Ui as calcExactSwapAmount,Wi as calcSwapAmountForTargetRatio,Zi as calculateLiquidityAmountEnough,Oe as calculateLiquidityAmountSide,mr as calculateLiquidityWithFallback,Xr as default,pi as defaultSwapSlippage,Ni as handleError,Lt as handleMessageError,gi as isEmptyObj,Bt as isNotMergeCoin,Ft as isSameType,Li as zapMainnet,Ei as zapTestnet};
2
2
  //# sourceMappingURL=index.js.map