UNPKG

@cobaltx/sdk-v2

Version:

An SDK for building applications on top of CobaltX.

9 lines 64.2 kB
import q from"bn.js";import{MINT_SIZE as Jr,TOKEN_PROGRAM_ID as $r,getTransferFeeConfig as eo,unpackMint as to}from"@solana/spl-token";import{PublicKey as cn}from"@solana/web3.js";import{get as dt,set as an}from"lodash";var je=class{constructor(t){this.logLevel=t.logLevel!==void 0?t.logLevel:0,this.name=t.name}set level(t){this.logLevel=t}get time(){return Date.now().toString()}get moduleName(){return this.name}isLogLevel(t){return t<=this.logLevel}error(...t){return this.isLogLevel(0)?(console.error(this.time,this.name,"sdk logger error",...t),this):this}logWithError(...t){let n=t.map(r=>typeof r=="object"?JSON.stringify(r):r).join(", ");throw new Error(n)}warning(...t){return this.isLogLevel(1)?(console.warn(this.time,this.name,"sdk logger warning",...t),this):this}info(...t){return this.isLogLevel(2)?(console.info(this.time,this.name,"sdk logger info",...t),this):this}debug(...t){return this.isLogLevel(3)?(console.debug(this.time,this.name,"sdk logger debug",...t),this):this}},ft={},un={};function F(e){let t=dt(ft,e);if(!t){let n=dt(un,e);t=new je({name:e,logLevel:n}),an(ft,e,t)}return t}var Ye=F("CobaltX_accountInfo_util");async function mt(e,t,n){let{batchRequest:r,commitment:o="confirmed",chunkCount:i=100}={batchRequest:!1,...n},s=ze(t,i),a=new Array(s.length).fill([]);if(r){let c=s.map(d=>{let f=e._buildArgs([d.map(p=>p.toBase58())],o,"base64");return{methodName:"getMultipleAccounts",args:f}}),u=ze(c,10);a=(await(await Promise.all(u.map(async d=>await e._rpcBatchRequest(d)))).flat()).map(d=>(d.error&&Ye.logWithError(`failed to get info for multiple accounts, RPC_ERROR, ${d.error.message}`),d.result.value.map(f=>{if(f){let{data:p,executable:y,lamports:b,owner:_,rentEpoch:x}=f;return p.length!==2&&p[1]!=="base64"&&Ye.logWithError("info must be base64 encoded, RPC_ERROR"),{data:Buffer.from(p[0],"base64"),executable:y,lamports:b,owner:new cn(_),rentEpoch:x}}return null})))}else try{a=await Promise.all(s.map(c=>e.getMultipleAccountsInfo(c,o)))}catch(c){c instanceof Error&&Ye.logWithError(`failed to get info for multiple accounts, RPC_ERROR, ${c.message}`)}return a.flat()}async function pt(e,t,n){let r=await mt(e,t.map(o=>o.pubkey),n);return t.map((o,i)=>({...o,accountInfo:r[i]}))}import ee from"bn.js";var ce=9e15,ie=1e9,Xe="0123456789abcdef",Ee="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",Be="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Ze={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-ce,maxE:ce,crypto:!1},Tt,Z,T=!0,ke="[DecimalError] ",oe=ke+"Invalid argument: ",yt=ke+"Precision limit exceeded",St=ke+"crypto unavailable",Pt="[object Decimal]",W=Math.floor,V=Math.pow,ln=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,dn=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,fn=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,xt=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,j=1e7,w=7,mn=9007199254740991,pn=Ee.length-1,Qe=Be.length-1,m={toStringTag:Pt};m.absoluteValue=m.abs=function(){var e=new this.constructor(this);return e.s<0&&(e.s=1),h(e)};m.ceil=function(){return h(new this.constructor(this),this.e+1,2)};m.clampedTo=m.clamp=function(e,t){var n,r=this,o=r.constructor;if(e=new o(e),t=new o(t),!e.s||!t.s)return new o(NaN);if(e.gt(t))throw Error(oe+t);return n=r.cmp(e),n<0?e:r.cmp(t)>0?t:new o(r)};m.comparedTo=m.cmp=function(e){var t,n,r,o,i=this,s=i.d,a=(e=new i.constructor(e)).d,c=i.s,u=e.s;if(!s||!a)return!c||!u?NaN:c!==u?c:s===a?0:!s^c<0?1:-1;if(!s[0]||!a[0])return s[0]?c:a[0]?-u:0;if(c!==u)return c;if(i.e!==e.e)return i.e>e.e^c<0?1:-1;for(r=s.length,o=a.length,t=0,n=r<o?r:o;t<n;++t)if(s[t]!==a[t])return s[t]>a[t]^c<0?1:-1;return r===o?0:r>o^c<0?1:-1};m.cosine=m.cos=function(){var e,t,n=this,r=n.constructor;return n.d?n.d[0]?(e=r.precision,t=r.rounding,r.precision=e+Math.max(n.e,n.sd())+w,r.rounding=1,n=gn(r,Et(r,n)),r.precision=e,r.rounding=t,h(Z==2||Z==3?n.neg():n,e,t,!0)):new r(1):new r(NaN)};m.cubeRoot=m.cbrt=function(){var e,t,n,r,o,i,s,a,c,u,l=this,d=l.constructor;if(!l.isFinite()||l.isZero())return new d(l);for(T=!1,i=l.s*V(l.s*l,1/3),!i||Math.abs(i)==1/0?(n=K(l.d),e=l.e,(i=(e-n.length+1)%3)&&(n+=i==1||i==-2?"0":"00"),i=V(n,1/3),e=W((e+1)/3)-(e%3==(e<0?-1:2)),i==1/0?n="5e"+e:(n=i.toExponential(),n=n.slice(0,n.indexOf("e")+1)+e),r=new d(n),r.s=l.s):r=new d(i.toString()),s=(e=d.precision)+3;;)if(a=r,c=a.times(a).times(a),u=c.plus(l),r=B(u.plus(l).times(a),u.plus(c),s+2,1),K(a.d).slice(0,s)===(n=K(r.d)).slice(0,s))if(n=n.slice(s-3,s+1),n=="9999"||!o&&n=="4999"){if(!o&&(h(a,e+1,0),a.times(a).times(a).eq(l))){r=a;break}s+=4,o=1}else{(!+n||!+n.slice(1)&&n.charAt(0)=="5")&&(h(r,e+1,1),t=!r.times(r).times(r).eq(l));break}return T=!0,h(r,e,d.rounding,t)};m.decimalPlaces=m.dp=function(){var e,t=this.d,n=NaN;if(t){if(e=t.length-1,n=(e-W(this.e/w))*w,e=t[e],e)for(;e%10==0;e/=10)n--;n<0&&(n=0)}return n};m.dividedBy=m.div=function(e){return B(this,new this.constructor(e))};m.dividedToIntegerBy=m.divToInt=function(e){var t=this,n=t.constructor;return h(B(t,new n(e),0,1,1),n.precision,n.rounding)};m.equals=m.eq=function(e){return this.cmp(e)===0};m.floor=function(){return h(new this.constructor(this),this.e+1,3)};m.greaterThan=m.gt=function(e){return this.cmp(e)>0};m.greaterThanOrEqualTo=m.gte=function(e){var t=this.cmp(e);return t==1||t===0};m.hyperbolicCosine=m.cosh=function(){var e,t,n,r,o,i=this,s=i.constructor,a=new s(1);if(!i.isFinite())return new s(i.s?1/0:NaN);if(i.isZero())return a;n=s.precision,r=s.rounding,s.precision=n+Math.max(i.e,i.sd())+4,s.rounding=1,o=i.d.length,o<32?(e=Math.ceil(o/3),t=(1/Ce(4,e)).toString()):(e=16,t="2.3283064365386962890625e-10"),i=le(s,1,i.times(t),new s(1),!0);for(var c,u=e,l=new s(8);u--;)c=i.times(i),i=a.minus(c.times(l.minus(c.times(l))));return h(i,s.precision=n,s.rounding=r,!0)};m.hyperbolicSine=m.sinh=function(){var e,t,n,r,o=this,i=o.constructor;if(!o.isFinite()||o.isZero())return new i(o);if(t=i.precision,n=i.rounding,i.precision=t+Math.max(o.e,o.sd())+4,i.rounding=1,r=o.d.length,r<3)o=le(i,2,o,o,!0);else{e=1.4*Math.sqrt(r),e=e>16?16:e|0,o=o.times(1/Ce(5,e)),o=le(i,2,o,o,!0);for(var s,a=new i(5),c=new i(16),u=new i(20);e--;)s=o.times(o),o=o.times(a.plus(s.times(c.times(s).plus(u))))}return i.precision=t,i.rounding=n,h(o,t,n,!0)};m.hyperbolicTangent=m.tanh=function(){var e,t,n=this,r=n.constructor;return n.isFinite()?n.isZero()?new r(n):(e=r.precision,t=r.rounding,r.precision=e+7,r.rounding=1,B(n.sinh(),n.cosh(),r.precision=e,r.rounding=t)):new r(n.s)};m.inverseCosine=m.acos=function(){var e=this,t=e.constructor,n=e.abs().cmp(1),r=t.precision,o=t.rounding;return n!==-1?n===0?e.isNeg()?Y(t,r,o):new t(0):new t(NaN):e.isZero()?Y(t,r+4,o).times(.5):(t.precision=r+6,t.rounding=1,e=new t(1).minus(e).div(e.plus(1)).sqrt().atan(),t.precision=r,t.rounding=o,e.times(2))};m.inverseHyperbolicCosine=m.acosh=function(){var e,t,n=this,r=n.constructor;return n.lte(1)?new r(n.eq(1)?0:NaN):n.isFinite()?(e=r.precision,t=r.rounding,r.precision=e+Math.max(Math.abs(n.e),n.sd())+4,r.rounding=1,T=!1,n=n.times(n).minus(1).sqrt().plus(n),T=!0,r.precision=e,r.rounding=t,n.ln()):new r(n)};m.inverseHyperbolicSine=m.asinh=function(){var e,t,n=this,r=n.constructor;return!n.isFinite()||n.isZero()?new r(n):(e=r.precision,t=r.rounding,r.precision=e+2*Math.max(Math.abs(n.e),n.sd())+6,r.rounding=1,T=!1,n=n.times(n).plus(1).sqrt().plus(n),T=!0,r.precision=e,r.rounding=t,n.ln())};m.inverseHyperbolicTangent=m.atanh=function(){var e,t,n,r,o=this,i=o.constructor;return o.isFinite()?o.e>=0?new i(o.abs().eq(1)?o.s/0:o.isZero()?o:NaN):(e=i.precision,t=i.rounding,r=o.sd(),Math.max(r,e)<2*-o.e-1?h(new i(o),e,t,!0):(i.precision=n=r-o.e,o=B(o.plus(1),new i(1).minus(o),n+e,1),i.precision=e+4,i.rounding=1,o=o.ln(),i.precision=e,i.rounding=t,o.times(.5))):new i(NaN)};m.inverseSine=m.asin=function(){var e,t,n,r,o=this,i=o.constructor;return o.isZero()?new i(o):(t=o.abs().cmp(1),n=i.precision,r=i.rounding,t!==-1?t===0?(e=Y(i,n+4,r).times(.5),e.s=o.s,e):new i(NaN):(i.precision=n+6,i.rounding=1,o=o.div(new i(1).minus(o.times(o)).sqrt().plus(1)).atan(),i.precision=n,i.rounding=r,o.times(2)))};m.inverseTangent=m.atan=function(){var e,t,n,r,o,i,s,a,c,u=this,l=u.constructor,d=l.precision,f=l.rounding;if(u.isFinite()){if(u.isZero())return new l(u);if(u.abs().eq(1)&&d+4<=Qe)return s=Y(l,d+4,f).times(.25),s.s=u.s,s}else{if(!u.s)return new l(NaN);if(d+4<=Qe)return s=Y(l,d+4,f).times(.5),s.s=u.s,s}for(l.precision=a=d+10,l.rounding=1,n=Math.min(28,a/w+2|0),e=n;e;--e)u=u.div(u.times(u).plus(1).sqrt().plus(1));for(T=!1,t=Math.ceil(a/w),r=1,c=u.times(u),s=new l(u),o=u;e!==-1;)if(o=o.times(c),i=s.minus(o.div(r+=2)),o=o.times(c),s=i.plus(o.div(r+=2)),s.d[t]!==void 0)for(e=t;s.d[e]===i.d[e]&&e--;);return n&&(s=s.times(2<<n-1)),T=!0,h(s,l.precision=d,l.rounding=f,!0)};m.isFinite=function(){return!!this.d};m.isInteger=m.isInt=function(){return!!this.d&&W(this.e/w)>this.d.length-2};m.isNaN=function(){return!this.s};m.isNegative=m.isNeg=function(){return this.s<0};m.isPositive=m.isPos=function(){return this.s>0};m.isZero=function(){return!!this.d&&this.d[0]===0};m.lessThan=m.lt=function(e){return this.cmp(e)<0};m.lessThanOrEqualTo=m.lte=function(e){return this.cmp(e)<1};m.logarithm=m.log=function(e){var t,n,r,o,i,s,a,c,u=this,l=u.constructor,d=l.precision,f=l.rounding,p=5;if(e==null)e=new l(10),t=!0;else{if(e=new l(e),n=e.d,e.s<0||!n||!n[0]||e.eq(1))return new l(NaN);t=e.eq(10)}if(n=u.d,u.s<0||!n||!n[0]||u.eq(1))return new l(n&&!n[0]?-1/0:u.s!=1?NaN:n?0:1/0);if(t)if(n.length>1)i=!0;else{for(o=n[0];o%10===0;)o/=10;i=o!==1}if(T=!1,a=d+p,s=re(u,a),r=t?Re(l,a+10):re(e,a),c=B(s,r,a,1),ge(c.d,o=d,f))do if(a+=10,s=re(u,a),r=t?Re(l,a+10):re(e,a),c=B(s,r,a,1),!i){+K(c.d).slice(o+1,o+15)+1==1e14&&(c=h(c,d+1,0));break}while(ge(c.d,o+=10,f));return T=!0,h(c,d,f)};m.minus=m.sub=function(e){var t,n,r,o,i,s,a,c,u,l,d,f,p=this,y=p.constructor;if(e=new y(e),!p.d||!e.d)return!p.s||!e.s?e=new y(NaN):p.d?e.s=-e.s:e=new y(e.d||p.s!==e.s?p:NaN),e;if(p.s!=e.s)return e.s=-e.s,p.plus(e);if(u=p.d,f=e.d,a=y.precision,c=y.rounding,!u[0]||!f[0]){if(f[0])e.s=-e.s;else if(u[0])e=new y(p);else return new y(c===3?-0:0);return T?h(e,a,c):e}if(n=W(e.e/w),l=W(p.e/w),u=u.slice(),i=l-n,i){for(d=i<0,d?(t=u,i=-i,s=f.length):(t=f,n=l,s=u.length),r=Math.max(Math.ceil(a/w),s)+2,i>r&&(i=r,t.length=1),t.reverse(),r=i;r--;)t.push(0);t.reverse()}else{for(r=u.length,s=f.length,d=r<s,d&&(s=r),r=0;r<s;r++)if(u[r]!=f[r]){d=u[r]<f[r];break}i=0}for(d&&(t=u,u=f,f=t,e.s=-e.s),s=u.length,r=f.length-s;r>0;--r)u[s++]=0;for(r=f.length;r>i;){if(u[--r]<f[r]){for(o=r;o&&u[--o]===0;)u[o]=j-1;--u[o],u[r]+=j}u[r]-=f[r]}for(;u[--s]===0;)u.pop();for(;u[0]===0;u.shift())--n;return u[0]?(e.d=u,e.e=Ie(u,n),T?h(e,a,c):e):new y(c===3?-0:0)};m.modulo=m.mod=function(e){var t,n=this,r=n.constructor;return e=new r(e),!n.d||!e.s||e.d&&!e.d[0]?new r(NaN):!e.d||n.d&&!n.d[0]?h(new r(n),r.precision,r.rounding):(T=!1,r.modulo==9?(t=B(n,e.abs(),0,3,1),t.s*=e.s):t=B(n,e,0,r.modulo,1),t=t.times(e),T=!0,n.minus(t))};m.naturalExponential=m.exp=function(){return Je(this)};m.naturalLogarithm=m.ln=function(){return re(this)};m.negated=m.neg=function(){var e=new this.constructor(this);return e.s=-e.s,h(e)};m.plus=m.add=function(e){var t,n,r,o,i,s,a,c,u,l,d=this,f=d.constructor;if(e=new f(e),!d.d||!e.d)return!d.s||!e.s?e=new f(NaN):d.d||(e=new f(e.d||d.s===e.s?d:NaN)),e;if(d.s!=e.s)return e.s=-e.s,d.minus(e);if(u=d.d,l=e.d,a=f.precision,c=f.rounding,!u[0]||!l[0])return l[0]||(e=new f(d)),T?h(e,a,c):e;if(i=W(d.e/w),r=W(e.e/w),u=u.slice(),o=i-r,o){for(o<0?(n=u,o=-o,s=l.length):(n=l,r=i,s=u.length),i=Math.ceil(a/w),s=i>s?i+1:s+1,o>s&&(o=s,n.length=1),n.reverse();o--;)n.push(0);n.reverse()}for(s=u.length,o=l.length,s-o<0&&(o=s,n=l,l=u,u=n),t=0;o;)t=(u[--o]=u[o]+l[o]+t)/j|0,u[o]%=j;for(t&&(u.unshift(t),++r),s=u.length;u[--s]==0;)u.pop();return e.d=u,e.e=Ie(u,r),T?h(e,a,c):e};m.precision=m.sd=function(e){var t,n=this;if(e!==void 0&&e!==!!e&&e!==1&&e!==0)throw Error(oe+e);return n.d?(t=At(n.d),e&&n.e+1>t&&(t=n.e+1)):t=NaN,t};m.round=function(){var e=this,t=e.constructor;return h(new t(e),e.e+1,t.rounding)};m.sine=m.sin=function(){var e,t,n=this,r=n.constructor;return n.isFinite()?n.isZero()?new r(n):(e=r.precision,t=r.rounding,r.precision=e+Math.max(n.e,n.sd())+w,r.rounding=1,n=wn(r,Et(r,n)),r.precision=e,r.rounding=t,h(Z>2?n.neg():n,e,t,!0)):new r(NaN)};m.squareRoot=m.sqrt=function(){var e,t,n,r,o,i,s=this,a=s.d,c=s.e,u=s.s,l=s.constructor;if(u!==1||!a||!a[0])return new l(!u||u<0&&(!a||a[0])?NaN:a?s:1/0);for(T=!1,u=Math.sqrt(+s),u==0||u==1/0?(t=K(a),(t.length+c)%2==0&&(t+="0"),u=Math.sqrt(t),c=W((c+1)/2)-(c<0||c%2),u==1/0?t="5e"+c:(t=u.toExponential(),t=t.slice(0,t.indexOf("e")+1)+c),r=new l(t)):r=new l(u.toString()),n=(c=l.precision)+3;;)if(i=r,r=i.plus(B(s,i,n+2,1)).times(.5),K(i.d).slice(0,n)===(t=K(r.d)).slice(0,n))if(t=t.slice(n-3,n+1),t=="9999"||!o&&t=="4999"){if(!o&&(h(i,c+1,0),i.times(i).eq(s))){r=i;break}n+=4,o=1}else{(!+t||!+t.slice(1)&&t.charAt(0)=="5")&&(h(r,c+1,1),e=!r.times(r).eq(s));break}return T=!0,h(r,c,l.rounding,e)};m.tangent=m.tan=function(){var e,t,n=this,r=n.constructor;return n.isFinite()?n.isZero()?new r(n):(e=r.precision,t=r.rounding,r.precision=e+10,r.rounding=1,n=n.sin(),n.s=1,n=B(n,new r(1).minus(n.times(n)).sqrt(),e+10,0),r.precision=e,r.rounding=t,h(Z==2||Z==4?n.neg():n,e,t,!0)):new r(NaN)};m.times=m.mul=function(e){var t,n,r,o,i,s,a,c,u,l=this,d=l.constructor,f=l.d,p=(e=new d(e)).d;if(e.s*=l.s,!f||!f[0]||!p||!p[0])return new d(!e.s||f&&!f[0]&&!p||p&&!p[0]&&!f?NaN:!f||!p?e.s/0:e.s*0);for(n=W(l.e/w)+W(e.e/w),c=f.length,u=p.length,c<u&&(i=f,f=p,p=i,s=c,c=u,u=s),i=[],s=c+u,r=s;r--;)i.push(0);for(r=u;--r>=0;){for(t=0,o=c+r;o>r;)a=i[o]+p[r]*f[o-r-1]+t,i[o--]=a%j|0,t=a/j|0;i[o]=(i[o]+t)%j|0}for(;!i[--s];)i.pop();return t?++n:i.shift(),e.d=i,e.e=Ie(i,n),T?h(e,d.precision,d.rounding):e};m.toBinary=function(e,t){return $e(this,2,e,t)};m.toDecimalPlaces=m.toDP=function(e,t){var n=this,r=n.constructor;return n=new r(n),e===void 0?n:(H(e,0,ie),t===void 0?t=r.rounding:H(t,0,8),h(n,e+n.e+1,t))};m.toExponential=function(e,t){var n,r=this,o=r.constructor;return e===void 0?n=z(r,!0):(H(e,0,ie),t===void 0?t=o.rounding:H(t,0,8),r=h(new o(r),e+1,t),n=z(r,!0,e+1)),r.isNeg()&&!r.isZero()?"-"+n:n};m.toFixed=function(e,t){var n,r,o=this,i=o.constructor;return e===void 0?n=z(o):(H(e,0,ie),t===void 0?t=i.rounding:H(t,0,8),r=h(new i(o),e+o.e+1,t),n=z(r,!1,e+r.e+1)),o.isNeg()&&!o.isZero()?"-"+n:n};m.toFraction=function(e){var t,n,r,o,i,s,a,c,u,l,d,f,p=this,y=p.d,b=p.constructor;if(!y)return new b(p);if(u=n=new b(1),r=c=new b(0),t=new b(r),i=t.e=At(y)-p.e-1,s=i%w,t.d[0]=V(10,s<0?w+s:s),e==null)e=i>0?t:u;else{if(a=new b(e),!a.isInt()||a.lt(u))throw Error(oe+a);e=a.gt(t)?i>0?t:u:a}for(T=!1,a=new b(K(y)),l=b.precision,b.precision=i=y.length*w*2;d=B(a,t,0,1,1),o=n.plus(d.times(r)),o.cmp(e)!=1;)n=r,r=o,o=u,u=c.plus(d.times(o)),c=o,o=t,t=a.minus(d.times(o)),a=o;return o=B(e.minus(n),r,0,1,1),c=c.plus(o.times(u)),n=n.plus(o.times(r)),c.s=u.s=p.s,f=B(u,r,i,1).minus(p).abs().cmp(B(c,n,i,1).minus(p).abs())<1?[u,r]:[c,n],b.precision=l,T=!0,f};m.toHexadecimal=m.toHex=function(e,t){return $e(this,16,e,t)};m.toNearest=function(e,t){var n=this,r=n.constructor;if(n=new r(n),e==null){if(!n.d)return n;e=new r(1),t=r.rounding}else{if(e=new r(e),t===void 0?t=r.rounding:H(t,0,8),!n.d)return e.s?n:e;if(!e.d)return e.s&&(e.s=n.s),e}return e.d[0]?(T=!1,n=B(n,e,0,t,1).times(e),T=!0,h(n)):(e.s=n.s,n=e),n};m.toNumber=function(){return+this};m.toOctal=function(e,t){return $e(this,8,e,t)};m.toPower=m.pow=function(e){var t,n,r,o,i,s,a=this,c=a.constructor,u=+(e=new c(e));if(!a.d||!e.d||!a.d[0]||!e.d[0])return new c(V(+a,u));if(a=new c(a),a.eq(1))return a;if(r=c.precision,i=c.rounding,e.eq(1))return h(a,r,i);if(t=W(e.e/w),t>=e.d.length-1&&(n=u<0?-u:u)<=mn)return o=Lt(c,a,n,r),e.s<0?new c(1).div(o):h(o,r,i);if(s=a.s,s<0){if(t<e.d.length-1)return new c(NaN);if((e.d[t]&1)==0&&(s=1),a.e==0&&a.d[0]==1&&a.d.length==1)return a.s=s,a}return n=V(+a,u),t=n==0||!isFinite(n)?W(u*(Math.log("0."+K(a.d))/Math.LN10+a.e+1)):new c(n+"").e,t>c.maxE+1||t<c.minE-1?new c(t>0?s/0:0):(T=!1,c.rounding=a.s=1,n=Math.min(12,(t+"").length),o=Je(e.times(re(a,r+n)),r),o.d&&(o=h(o,r+5,1),ge(o.d,r,i)&&(t=r+10,o=h(Je(e.times(re(a,t+n)),t),t+5,1),+K(o.d).slice(r+1,r+15)+1==1e14&&(o=h(o,r+1,0)))),o.s=s,T=!0,c.rounding=i,h(o,r,i))};m.toPrecision=function(e,t){var n,r=this,o=r.constructor;return e===void 0?n=z(r,r.e<=o.toExpNeg||r.e>=o.toExpPos):(H(e,1,ie),t===void 0?t=o.rounding:H(t,0,8),r=h(new o(r),e,t),n=z(r,e<=r.e||r.e<=o.toExpNeg,e)),r.isNeg()&&!r.isZero()?"-"+n:n};m.toSignificantDigits=m.toSD=function(e,t){var n=this,r=n.constructor;return e===void 0?(e=r.precision,t=r.rounding):(H(e,1,ie),t===void 0?t=r.rounding:H(t,0,8)),h(new r(n),e,t)};m.toString=function(){var e=this,t=e.constructor,n=z(e,e.e<=t.toExpNeg||e.e>=t.toExpPos);return e.isNeg()&&!e.isZero()?"-"+n:n};m.truncated=m.trunc=function(){return h(new this.constructor(this),this.e+1,1)};m.valueOf=m.toJSON=function(){var e=this,t=e.constructor,n=z(e,e.e<=t.toExpNeg||e.e>=t.toExpPos);return e.isNeg()?"-"+n:n};function K(e){var t,n,r,o=e.length-1,i="",s=e[0];if(o>0){for(i+=s,t=1;t<o;t++)r=e[t]+"",n=w-r.length,n&&(i+=ne(n)),i+=r;s=e[t],r=s+"",n=w-r.length,n&&(i+=ne(n))}else if(s===0)return"0";for(;s%10===0;)s/=10;return i+s}function H(e,t,n){if(e!==~~e||e<t||e>n)throw Error(oe+e)}function ge(e,t,n,r){var o,i,s,a;for(i=e[0];i>=10;i/=10)--t;return--t<0?(t+=w,o=0):(o=Math.ceil((t+1)/w),t%=w),i=V(10,w-t),a=e[o]%i|0,r==null?t<3?(t==0?a=a/100|0:t==1&&(a=a/10|0),s=n<4&&a==99999||n>3&&a==49999||a==5e4||a==0):s=(n<4&&a+1==i||n>3&&a+1==i/2)&&(e[o+1]/i/100|0)==V(10,t-2)-1||(a==i/2||a==0)&&(e[o+1]/i/100|0)==0:t<4?(t==0?a=a/1e3|0:t==1?a=a/100|0:t==2&&(a=a/10|0),s=(r||n<4)&&a==9999||!r&&n>3&&a==4999):s=((r||n<4)&&a+1==i||!r&&n>3&&a+1==i/2)&&(e[o+1]/i/1e3|0)==V(10,t-3)-1,s}function _e(e,t,n){for(var r,o=[0],i,s=0,a=e.length;s<a;){for(i=o.length;i--;)o[i]*=t;for(o[0]+=Xe.indexOf(e.charAt(s++)),r=0;r<o.length;r++)o[r]>n-1&&(o[r+1]===void 0&&(o[r+1]=0),o[r+1]+=o[r]/n|0,o[r]%=n)}return o.reverse()}function gn(e,t){var n,r,o;if(t.isZero())return t;r=t.d.length,r<32?(n=Math.ceil(r/3),o=(1/Ce(4,n)).toString()):(n=16,o="2.3283064365386962890625e-10"),e.precision+=n,t=le(e,1,t.times(o),new e(1));for(var i=n;i--;){var s=t.times(t);t=s.times(s).minus(s).times(8).plus(1)}return e.precision-=n,t}var B=function(){function e(r,o,i){var s,a=0,c=r.length;for(r=r.slice();c--;)s=r[c]*o+a,r[c]=s%i|0,a=s/i|0;return a&&r.unshift(a),r}function t(r,o,i,s){var a,c;if(i!=s)c=i>s?1:-1;else for(a=c=0;a<i;a++)if(r[a]!=o[a]){c=r[a]>o[a]?1:-1;break}return c}function n(r,o,i,s){for(var a=0;i--;)r[i]-=a,a=r[i]<o[i]?1:0,r[i]=a*s+r[i]-o[i];for(;!r[0]&&r.length>1;)r.shift()}return function(r,o,i,s,a,c){var u,l,d,f,p,y,b,_,x,O,E,v,Pe,X,He,xe,pe,qe,G,Ae,Le=r.constructor,Ge=r.s==o.s?1:-1,U=r.d,R=o.d;if(!U||!U[0]||!R||!R[0])return new Le(!r.s||!o.s||(U?R&&U[0]==R[0]:!R)?NaN:U&&U[0]==0||!R?Ge*0:Ge/0);for(c?(p=1,l=r.e-o.e):(c=j,p=w,l=W(r.e/p)-W(o.e/p)),G=R.length,pe=U.length,x=new Le(Ge),O=x.d=[],d=0;R[d]==(U[d]||0);d++);if(R[d]>(U[d]||0)&&l--,i==null?(X=i=Le.precision,s=Le.rounding):a?X=i+(r.e-o.e)+1:X=i,X<0)O.push(1),y=!0;else{if(X=X/p+2|0,d=0,G==1){for(f=0,R=R[0],X++;(d<pe||f)&&X--;d++)He=f*c+(U[d]||0),O[d]=He/R|0,f=He%R|0;y=f||d<pe}else{for(f=c/(R[0]+1)|0,f>1&&(R=e(R,f,c),U=e(U,f,c),G=R.length,pe=U.length),xe=G,E=U.slice(0,G),v=E.length;v<G;)E[v++]=0;Ae=R.slice(),Ae.unshift(0),qe=R[0],R[1]>=c/2&&++qe;do f=0,u=t(R,E,G,v),u<0?(Pe=E[0],G!=v&&(Pe=Pe*c+(E[1]||0)),f=Pe/qe|0,f>1?(f>=c&&(f=c-1),b=e(R,f,c),_=b.length,v=E.length,u=t(b,E,_,v),u==1&&(f--,n(b,G<_?Ae:R,_,c))):(f==0&&(u=f=1),b=R.slice()),_=b.length,_<v&&b.unshift(0),n(E,b,v,c),u==-1&&(v=E.length,u=t(R,E,G,v),u<1&&(f++,n(E,G<v?Ae:R,v,c))),v=E.length):u===0&&(f++,E=[0]),O[d++]=f,u&&E[0]?E[v++]=U[xe]||0:(E=[U[xe]],v=1);while((xe++<pe||E[0]!==void 0)&&X--);y=E[0]!==void 0}O[0]||O.shift()}if(p==1)x.e=l,Tt=y;else{for(d=1,f=O[0];f>=10;f/=10)d++;x.e=d+l*p-1,h(x,a?i+x.e+1:i,s,y)}return x}}();function h(e,t,n,r){var o,i,s,a,c,u,l,d,f,p=e.constructor;e:if(t!=null){if(d=e.d,!d)return e;for(o=1,a=d[0];a>=10;a/=10)o++;if(i=t-o,i<0)i+=w,s=t,l=d[f=0],c=l/V(10,o-s-1)%10|0;else if(f=Math.ceil((i+1)/w),a=d.length,f>=a)if(r){for(;a++<=f;)d.push(0);l=c=0,o=1,i%=w,s=i-w+1}else break e;else{for(l=a=d[f],o=1;a>=10;a/=10)o++;i%=w,s=i-w+o,c=s<0?0:l/V(10,o-s-1)%10|0}if(r=r||t<0||d[f+1]!==void 0||(s<0?l:l%V(10,o-s-1)),u=n<4?(c||r)&&(n==0||n==(e.s<0?3:2)):c>5||c==5&&(n==4||r||n==6&&(i>0?s>0?l/V(10,o-s):0:d[f-1])%10&1||n==(e.s<0?8:7)),t<1||!d[0])return d.length=0,u?(t-=e.e+1,d[0]=V(10,(w-t%w)%w),e.e=-t||0):d[0]=e.e=0,e;if(i==0?(d.length=f,a=1,f--):(d.length=f+1,a=V(10,w-i),d[f]=s>0?(l/V(10,o-s)%V(10,s)|0)*a:0),u)for(;;)if(f==0){for(i=1,s=d[0];s>=10;s/=10)i++;for(s=d[0]+=a,a=1;s>=10;s/=10)a++;i!=a&&(e.e++,d[0]==j&&(d[0]=1));break}else{if(d[f]+=a,d[f]!=j)break;d[f--]=0,a=1}for(i=d.length;d[--i]===0;)d.pop()}return T&&(e.e>p.maxE?(e.d=null,e.e=NaN):e.e<p.minE&&(e.e=0,e.d=[0])),e}function z(e,t,n){if(!e.isFinite())return Nt(e);var r,o=e.e,i=K(e.d),s=i.length;return t?(n&&(r=n-s)>0?i=i.charAt(0)+"."+i.slice(1)+ne(r):s>1&&(i=i.charAt(0)+"."+i.slice(1)),i=i+(e.e<0?"e":"e+")+e.e):o<0?(i="0."+ne(-o-1)+i,n&&(r=n-s)>0&&(i+=ne(r))):o>=s?(i+=ne(o+1-s),n&&(r=n-o-1)>0&&(i=i+"."+ne(r))):((r=o+1)<s&&(i=i.slice(0,r)+"."+i.slice(r)),n&&(r=n-s)>0&&(o+1===s&&(i+="."),i+=ne(r))),i}function Ie(e,t){var n=e[0];for(t*=w;n>=10;n/=10)t++;return t}function Re(e,t,n){if(t>pn)throw T=!0,n&&(e.precision=n),Error(yt);return h(new e(Ee),t,1,!0)}function Y(e,t,n){if(t>Qe)throw Error(yt);return h(new e(Be),t,n,!0)}function At(e){var t=e.length-1,n=t*w+1;if(t=e[t],t){for(;t%10==0;t/=10)n--;for(t=e[0];t>=10;t/=10)n++}return n}function ne(e){for(var t="";e--;)t+="0";return t}function Lt(e,t,n,r){var o,i=new e(1),s=Math.ceil(r/w+4);for(T=!1;;){if(n%2&&(i=i.times(t),wt(i.d,s)&&(o=!0)),n=W(n/2),n===0){n=i.d.length-1,o&&i.d[n]===0&&++i.d[n];break}t=t.times(t),wt(t.d,s)}return T=!0,i}function ht(e){return e.d[e.d.length-1]&1}function _t(e,t,n){for(var r,o,i=new e(t[0]),s=0;++s<t.length;){if(o=new e(t[s]),!o.s){i=o;break}r=i.cmp(o),(r===n||r===0&&i.s===n)&&(i=o)}return i}function Je(e,t){var n,r,o,i,s,a,c,u=0,l=0,d=0,f=e.constructor,p=f.rounding,y=f.precision;if(!e.d||!e.d[0]||e.e>17)return new f(e.d?e.d[0]?e.s<0?0:1/0:1:e.s?e.s<0?0:e:0/0);for(t==null?(T=!1,c=y):c=t,a=new f(.03125);e.e>-2;)e=e.times(a),d+=5;for(r=Math.log(V(2,d))/Math.LN10*2+5|0,c+=r,n=i=s=new f(1),f.precision=c;;){if(i=h(i.times(e),c,1),n=n.times(++l),a=s.plus(B(i,n,c,1)),K(a.d).slice(0,c)===K(s.d).slice(0,c)){for(o=d;o--;)s=h(s.times(s),c,1);if(t==null)if(u<3&&ge(s.d,c-r,p,u))f.precision=c+=10,n=i=a=new f(1),l=0,u++;else return h(s,f.precision=y,p,T=!0);else return f.precision=y,s}s=a}}function re(e,t){var n,r,o,i,s,a,c,u,l,d,f,p=1,y=10,b=e,_=b.d,x=b.constructor,O=x.rounding,E=x.precision;if(b.s<0||!_||!_[0]||!b.e&&_[0]==1&&_.length==1)return new x(_&&!_[0]?-1/0:b.s!=1?NaN:_?0:b);if(t==null?(T=!1,l=E):l=t,x.precision=l+=y,n=K(_),r=n.charAt(0),Math.abs(i=b.e)<15e14){for(;r<7&&r!=1||r==1&&n.charAt(1)>3;)b=b.times(e),n=K(b.d),r=n.charAt(0),p++;i=b.e,r>1?(b=new x("0."+n),i++):b=new x(r+"."+n.slice(1))}else return u=Re(x,l+2,E).times(i+""),b=re(new x(r+"."+n.slice(1)),l-y).plus(u),x.precision=E,t==null?h(b,E,O,T=!0):b;for(d=b,c=s=b=B(b.minus(1),b.plus(1),l,1),f=h(b.times(b),l,1),o=3;;){if(s=h(s.times(f),l,1),u=c.plus(B(s,new x(o),l,1)),K(u.d).slice(0,l)===K(c.d).slice(0,l))if(c=c.times(2),i!==0&&(c=c.plus(Re(x,l+2,E).times(i+""))),c=B(c,new x(p),l,1),t==null)if(ge(c.d,l-y,O,a))x.precision=l+=y,u=s=b=B(d.minus(1),d.plus(1),l,1),f=h(b.times(b),l,1),o=a=1;else return h(c,x.precision=E,O,T=!0);else return x.precision=E,c;c=u,o+=2}}function Nt(e){return String(e.s*e.s/0)}function Ne(e,t){var n,r,o;for((n=t.indexOf("."))>-1&&(t=t.replace(".","")),(r=t.search(/e/i))>0?(n<0&&(n=r),n+=+t.slice(r+1),t=t.substring(0,r)):n<0&&(n=t.length),r=0;t.charCodeAt(r)===48;r++);for(o=t.length;t.charCodeAt(o-1)===48;--o);if(t=t.slice(r,o),t){if(o-=r,e.e=n=n-r-1,e.d=[],r=(n+1)%w,n<0&&(r+=w),r<o){for(r&&e.d.push(+t.slice(0,r)),o-=w;r<o;)e.d.push(+t.slice(r,r+=w));t=t.slice(r),r=w-t.length}else r-=o;for(;r--;)t+="0";e.d.push(+t),T&&(e.e>e.constructor.maxE?(e.d=null,e.e=NaN):e.e<e.constructor.minE&&(e.e=0,e.d=[0]))}else e.e=0,e.d=[0];return e}function hn(e,t){var n,r,o,i,s,a,c,u,l;if(t.indexOf("_")>-1){if(t=t.replace(/(\d)_(?=\d)/g,"$1"),xt.test(t))return Ne(e,t)}else if(t==="Infinity"||t==="NaN")return+t||(e.s=NaN),e.e=NaN,e.d=null,e;if(dn.test(t))n=16,t=t.toLowerCase();else if(ln.test(t))n=2;else if(fn.test(t))n=8;else throw Error(oe+t);for(i=t.search(/p/i),i>0?(c=+t.slice(i+1),t=t.substring(2,i)):t=t.slice(2),i=t.indexOf("."),s=i>=0,r=e.constructor,s&&(t=t.replace(".",""),a=t.length,i=a-i,o=Lt(r,new r(n),i,i*2)),u=_e(t,n,j),l=u.length-1,i=l;u[i]===0;--i)u.pop();return i<0?new r(e.s*0):(e.e=Ie(u,l),e.d=u,T=!1,s&&(e=B(e,o,a*4)),c&&(e=e.times(Math.abs(c)<54?V(2,c):De.pow(2,c))),T=!0,e)}function wn(e,t){var n,r=t.d.length;if(r<3)return t.isZero()?t:le(e,2,t,t);n=1.4*Math.sqrt(r),n=n>16?16:n|0,t=t.times(1/Ce(5,n)),t=le(e,2,t,t);for(var o,i=new e(5),s=new e(16),a=new e(20);n--;)o=t.times(t),t=t.times(i.plus(o.times(s.times(o).minus(a))));return t}function le(e,t,n,r,o){var i,s,a,c,u=1,l=e.precision,d=Math.ceil(l/w);for(T=!1,c=n.times(n),a=new e(r);;){if(s=B(a.times(c),new e(t++*t++),l,1),a=o?r.plus(s):r.minus(s),r=B(s.times(c),new e(t++*t++),l,1),s=a.plus(r),s.d[d]!==void 0){for(i=d;s.d[i]===a.d[i]&&i--;);if(i==-1)break}i=a,a=r,r=s,s=i,u++}return T=!0,s.d.length=d+1,s}function Ce(e,t){for(var n=e;--t;)n*=e;return n}function Et(e,t){var n,r=t.s<0,o=Y(e,e.precision,1),i=o.times(.5);if(t=t.abs(),t.lte(i))return Z=r?4:1,t;if(n=t.divToInt(o),n.isZero())Z=r?3:2;else{if(t=t.minus(n.times(o)),t.lte(i))return Z=ht(n)?r?2:3:r?4:1,t;Z=ht(n)?r?1:4:r?3:2}return t.minus(o).abs()}function $e(e,t,n,r){var o,i,s,a,c,u,l,d,f,p=e.constructor,y=n!==void 0;if(y?(H(n,1,ie),r===void 0?r=p.rounding:H(r,0,8)):(n=p.precision,r=p.rounding),!e.isFinite())l=Nt(e);else{for(l=z(e),s=l.indexOf("."),y?(o=2,t==16?n=n*4-3:t==8&&(n=n*3-2)):o=t,s>=0&&(l=l.replace(".",""),f=new p(1),f.e=l.length-s,f.d=_e(z(f),10,o),f.e=f.d.length),d=_e(l,10,o),i=c=d.length;d[--c]==0;)d.pop();if(!d[0])l=y?"0p+0":"0";else{if(s<0?i--:(e=new p(e),e.d=d,e.e=i,e=B(e,f,n,r,0,o),d=e.d,i=e.e,u=Tt),s=d[n],a=o/2,u=u||d[n+1]!==void 0,u=r<4?(s!==void 0||u)&&(r===0||r===(e.s<0?3:2)):s>a||s===a&&(r===4||u||r===6&&d[n-1]&1||r===(e.s<0?8:7)),d.length=n,u)for(;++d[--n]>o-1;)d[n]=0,n||(++i,d.unshift(1));for(c=d.length;!d[c-1];--c);for(s=0,l="";s<c;s++)l+=Xe.charAt(d[s]);if(y){if(c>1)if(t==16||t==8){for(s=t==16?4:3,--c;c%s;c++)l+="0";for(d=_e(l,o,t),c=d.length;!d[c-1];--c);for(s=1,l="1.";s<c;s++)l+=Xe.charAt(d[s])}else l=l.charAt(0)+"."+l.slice(1);l=l+(i<0?"p":"p+")+i}else if(i<0){for(;++i;)l="0"+l;l="0."+l}else if(++i>c)for(i-=c;i--;)l+="0";else i<c&&(l=l.slice(0,i)+"."+l.slice(i))}l=(t==16?"0x":t==2?"0b":t==8?"0o":"")+l}return e.s<0?"-"+l:l}function wt(e,t){if(e.length>t)return e.length=t,!0}function bn(e){return new this(e).abs()}function Tn(e){return new this(e).acos()}function yn(e){return new this(e).acosh()}function Sn(e,t){return new this(e).plus(t)}function Pn(e){return new this(e).asin()}function xn(e){return new this(e).asinh()}function An(e){return new this(e).atan()}function Ln(e){return new this(e).atanh()}function _n(e,t){e=new this(e),t=new this(t);var n,r=this.precision,o=this.rounding,i=r+4;return!e.s||!t.s?n=new this(NaN):!e.d&&!t.d?(n=Y(this,i,1).times(t.s>0?.25:.75),n.s=e.s):!t.d||e.isZero()?(n=t.s<0?Y(this,r,o):new this(0),n.s=e.s):!e.d||t.isZero()?(n=Y(this,i,1).times(.5),n.s=e.s):t.s<0?(this.precision=i,this.rounding=1,n=this.atan(B(e,t,i,1)),t=Y(this,i,1),this.precision=r,this.rounding=o,n=e.s<0?n.minus(t):n.plus(t)):n=this.atan(B(e,t,i,1)),n}function Nn(e){return new this(e).cbrt()}function En(e){return h(e=new this(e),e.e+1,2)}function Bn(e,t,n){return new this(e).clamp(t,n)}function Rn(e){if(!e||typeof e!="object")throw Error(ke+"Object expected");var t,n,r,o=e.defaults===!0,i=["precision",1,ie,"rounding",0,8,"toExpNeg",-ce,0,"toExpPos",0,ce,"maxE",0,ce,"minE",-ce,0,"modulo",0,9];for(t=0;t<i.length;t+=3)if(n=i[t],o&&(this[n]=Ze[n]),(r=e[n])!==void 0)if(W(r)===r&&r>=i[t+1]&&r<=i[t+2])this[n]=r;else throw Error(oe+n+": "+r);if(n="crypto",o&&(this[n]=Ze[n]),(r=e[n])!==void 0)if(r===!0||r===!1||r===0||r===1)if(r)if(typeof crypto<"u"&&crypto&&(crypto.getRandomValues||crypto.randomBytes))this[n]=!0;else throw Error(St);else this[n]=!1;else throw Error(oe+n+": "+r);return this}function kn(e){return new this(e).cos()}function In(e){return new this(e).cosh()}function Bt(e){var t,n,r;function o(i){var s,a,c,u=this;if(!(u instanceof o))return new o(i);if(u.constructor=o,bt(i)){u.s=i.s,T?!i.d||i.e>o.maxE?(u.e=NaN,u.d=null):i.e<o.minE?(u.e=0,u.d=[0]):(u.e=i.e,u.d=i.d.slice()):(u.e=i.e,u.d=i.d?i.d.slice():i.d);return}if(c=typeof i,c==="number"){if(i===0){u.s=1/i<0?-1:1,u.e=0,u.d=[0];return}if(i<0?(i=-i,u.s=-1):u.s=1,i===~~i&&i<1e7){for(s=0,a=i;a>=10;a/=10)s++;T?s>o.maxE?(u.e=NaN,u.d=null):s<o.minE?(u.e=0,u.d=[0]):(u.e=s,u.d=[i]):(u.e=s,u.d=[i]);return}if(i*0!==0){i||(u.s=NaN),u.e=NaN,u.d=null;return}return Ne(u,i.toString())}if(c==="string")return(a=i.charCodeAt(0))===45?(i=i.slice(1),u.s=-1):(a===43&&(i=i.slice(1)),u.s=1),xt.test(i)?Ne(u,i):hn(u,i);if(c==="bigint")return i<0?(i=-i,u.s=-1):u.s=1,Ne(u,i.toString());throw Error(oe+i)}if(o.prototype=m,o.ROUND_UP=0,o.ROUND_DOWN=1,o.ROUND_CEIL=2,o.ROUND_FLOOR=3,o.ROUND_HALF_UP=4,o.ROUND_HALF_DOWN=5,o.ROUND_HALF_EVEN=6,o.ROUND_HALF_CEIL=7,o.ROUND_HALF_FLOOR=8,o.EUCLID=9,o.config=o.set=Rn,o.clone=Bt,o.isDecimal=bt,o.abs=bn,o.acos=Tn,o.acosh=yn,o.add=Sn,o.asin=Pn,o.asinh=xn,o.atan=An,o.atanh=Ln,o.atan2=_n,o.cbrt=Nn,o.ceil=En,o.clamp=Bn,o.cos=kn,o.cosh=In,o.div=Cn,o.exp=Dn,o.floor=Mn,o.hypot=On,o.ln=Vn,o.log=vn,o.log10=Kn,o.log2=Fn,o.max=Un,o.min=Wn,o.mod=Hn,o.mul=qn,o.pow=Gn,o.random=jn,o.round=Yn,o.sign=zn,o.sin=Xn,o.sinh=Zn,o.sqrt=Qn,o.sub=Jn,o.sum=$n,o.tan=er,o.tanh=tr,o.trunc=nr,e===void 0&&(e={}),e&&e.defaults!==!0)for(r=["precision","rounding","toExpNeg","toExpPos","maxE","minE","modulo","crypto"],t=0;t<r.length;)e.hasOwnProperty(n=r[t++])||(e[n]=this[n]);return o.config(e),o}function Cn(e,t){return new this(e).div(t)}function Dn(e){return new this(e).exp()}function Mn(e){return h(e=new this(e),e.e+1,3)}function On(){var e,t,n=new this(0);for(T=!1,e=0;e<arguments.length;)if(t=new this(arguments[e++]),t.d)n.d&&(n=n.plus(t.times(t)));else{if(t.s)return T=!0,new this(1/0);n=t}return T=!0,n.sqrt()}function bt(e){return e instanceof De||e&&e.toStringTag===Pt||!1}function Vn(e){return new this(e).ln()}function vn(e,t){return new this(e).log(t)}function Fn(e){return new this(e).log(2)}function Kn(e){return new this(e).log(10)}function Un(){return _t(this,arguments,-1)}function Wn(){return _t(this,arguments,1)}function Hn(e,t){return new this(e).mod(t)}function qn(e,t){return new this(e).mul(t)}function Gn(e,t){return new this(e).pow(t)}function jn(e){var t,n,r,o,i=0,s=new this(1),a=[];if(e===void 0?e=this.precision:H(e,1,ie),r=Math.ceil(e/w),this.crypto)if(crypto.getRandomValues)for(t=crypto.getRandomValues(new Uint32Array(r));i<r;)o=t[i],o>=429e7?t[i]=crypto.getRandomValues(new Uint32Array(1))[0]:a[i++]=o%1e7;else if(crypto.randomBytes){for(t=crypto.randomBytes(r*=4);i<r;)o=t[i]+(t[i+1]<<8)+(t[i+2]<<16)+((t[i+3]&127)<<24),o>=214e7?crypto.randomBytes(4).copy(t,i):(a.push(o%1e7),i+=4);i=r/4}else throw Error(St);else for(;i<r;)a[i++]=Math.random()*1e7|0;for(r=a[--i],e%=w,r&&e&&(o=V(10,w-e),a[i]=(r/o|0)*o);a[i]===0;i--)a.pop();if(i<0)n=0,a=[0];else{for(n=-1;a[0]===0;n-=w)a.shift();for(r=1,o=a[0];o>=10;o/=10)r++;r<w&&(n-=w-r)}return s.e=n,s.d=a,s}function Yn(e){return h(e=new this(e),e.e+1,this.rounding)}function zn(e){return e=new this(e),e.d?e.d[0]?e.s:0*e.s:e.s||NaN}function Xn(e){return new this(e).sin()}function Zn(e){return new this(e).sinh()}function Qn(e){return new this(e).sqrt()}function Jn(e,t){return new this(e).sub(t)}function $n(){var e=0,t=arguments,n=new this(t[e]);for(T=!1;n.s&&++e<t.length;)n=n.plus(t[e]);return T=!0,h(n,this.precision,this.rounding)}function er(e){return new this(e).tan()}function tr(e){return new this(e).tanh()}function nr(e){return h(e=new this(e),e.e+1,1)}m[Symbol.for("nodejs.util.inspect.custom")]=m.toString;m[Symbol.toStringTag]="Decimal";var De=m.constructor=Bt(Ze);Ee=new De(Ee);Be=new De(Be);import lr from"big.js";import Ve from"bn.js";import rr from"toformat";var or=rr,he=or;import Oe from"big.js";import sr from"bn.js";import ar from"decimal.js-light";import we from"bn.js";var Rt=9007199254740991;function D(e){let t=F("CobaltX_parseBigNumberish");if(e instanceof we)return e;if(typeof e=="string"){if(e.match(/^-?[0-9]+$/))return new we(e);t.logWithError(`invalid BigNumberish string: ${e}`)}return typeof e=="number"?(e%1&&t.logWithError(`BigNumberish number underflow: ${e}`),(e>=Rt||e<=-Rt)&&t.logWithError(`BigNumberish number overflow: ${e}`),new we(String(e))):typeof e=="bigint"?new we(e.toString()):(t.error(`invalid BigNumberish value: ${e}`),new we(0))}var Me=F("module/fraction"),et=he(Oe),be=he(ar),ur={[0]:be.ROUND_DOWN,[1]:be.ROUND_HALF_UP,[2]:be.ROUND_UP},cr={[0]:Oe.roundDown,[1]:Oe.roundHalfUp,[2]:Oe.roundUp},N=class{constructor(t,n=new sr(1)){this.numerator=D(t),this.denominator=D(n)}get quotient(){return this.numerator.div(this.denominator)}invert(){return new N(this.denominator,this.numerator)}add(t){let n=t instanceof N?t:new N(D(t));return this.denominator.eq(n.denominator)?new N(this.numerator.add(n.numerator),this.denominator):new N(this.numerator.mul(n.denominator).add(n.numerator.mul(this.denominator)),this.denominator.mul(n.denominator))}sub(t){let n=t instanceof N?t:new N(D(t));return this.denominator.eq(n.denominator)?new N(this.numerator.sub(n.numerator),this.denominator):new N(this.numerator.mul(n.denominator).sub(n.numerator.mul(this.denominator)),this.denominator.mul(n.denominator))}mul(t){let n=t instanceof N?t:new N(D(t));return new N(this.numerator.mul(n.numerator),this.denominator.mul(n.denominator))}div(t){let n=t instanceof N?t:new N(D(t));return new N(this.numerator.mul(n.denominator),this.denominator.mul(n.numerator))}toSignificant(t,n={groupSeparator:""},r=1){Number.isInteger(t)||Me.logWithError(`${t} is not an integer.`),t<=0&&Me.logWithError(`${t} is not positive.`),be.set({precision:t+1,rounding:ur[r]});let o=new be(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(t);return o.toFormat(o.decimalPlaces(),n)}toFixed(t,n={groupSeparator:""},r=1){return Number.isInteger(t)||Me.logWithError(`${t} is not an integer.`),t<0&&Me.logWithError(`${t} is negative.`),et.DP=t,et.RM=cr[r]||1,new et(this.numerator.toString()).div(this.denominator.toString()).toFormat(t,n)}isZero(){return this.numerator.isZero()}};var dr=F("CobaltX_amount"),kt=he(lr);function fr(e,t){let n="0",r="0";if(e.includes(".")){let o=e.split(".");o.length===2?([n,r]=o,r=r.padEnd(t,"0")):dr.logWithError(`invalid number string, num: ${e}`)}else n=e;return[n,r.slice(0,t)||r]}var Q=class extends N{constructor(n,r,o=!0,i){let s=new Ve(0),a=tt.pow(new Ve(n.decimals));if(o)s=D(r);else{let c=new Ve(0),u=new Ve(0);if(typeof r=="string"||typeof r=="number"||typeof r=="bigint"){let[l,d]=fr(r.toString(),n.decimals);c=D(l),u=D(d)}c=c.mul(a),s=c.add(u)}super(s,a);this.logger=F(i||"TokenAmount"),this.token=n}get raw(){return this.numerator}isZero(){return this.raw.isZero()}gt(n){return this.token.equals(n.token)||this.logger.logWithError("gt token not equals"),this.raw.gt(n.raw)}lt(n){return this.token.equals(n.token)||this.logger.logWithError("lt token not equals"),this.raw.lt(n.raw)}add(n){return this.token.equals(n.token)||this.logger.logWithError("add token not equals"),new Q(this.token,this.raw.add(n.raw))}subtract(n){return this.token.equals(n.token)||this.logger.logWithError("sub token not equals"),new Q(this.token,this.raw.sub(n.raw))}toSignificant(n=this.token.decimals,r,o=0){return super.toSignificant(n,r,o)}toFixed(n=this.token.decimals,r,o=0){return n>this.token.decimals&&this.logger.logWithError("decimals overflow"),super.toFixed(n,r,o)}toExact(n={groupSeparator:""}){return kt.DP=this.token.decimals,new kt(this.numerator.toString()).div(this.denominator.toString()).toFormat(n)}};import{TOKEN_PROGRAM_ID as pr}from"@solana/spl-token";import{PublicKey as Ko}from"@solana/web3.js";import{PublicKey as J}from"@solana/web3.js";var mr={["sooneth"]:{NATIVE_TOKEN_SYMBOL:"ETH",SWAP_HOST:"https://trade-api.cobaltx.io",BASE_HOST:"https://api.cobaltx.io",OWNER_BASE_HOST:"https://api.cobaltx.io",SERVICE_BASE_HOST:"https://api.cobaltx.io",MONITOR_BASE_HOST:"https://api.cobaltx.io",SERVICE_1_BASE_HOST:"https://api.cobaltx.io",ANALYTICS_BASE_HOST:"https://api.cobaltx.io",GITHUB_TOKEN_LIST_URL:"https://raw.githubusercontent.com/cobaltx-io/tokens/main/tokens.json",WSOL_MINT_ADDRESS:"So11111111111111111111111111111111111111112",GITHUB_LOGO_URL:"https://raw.githubusercontent.com/cobaltx-io/tokens/main/logo/So11111111111111111111111111111111111111112.png",LOOKUP_TABLE_CACHE:"6fa5xTGwoYJPMLMXaQ4tQFKMoYvXuvsJFLigMJ9a5QeB",CLMM_PROGRAMM_ID:"2TnjBuwqyBB9to5jURagDT7jLmBPefGRiKL2yh1zPZ4V",BONK_MINT_ADDRESS:"71kRXzJMvSeArtXYNEWa8KAjpRJosdMQ7Dpgy5Jt5zfd",SOL_MINT_ADDRESS:"ERFzpDteGNo8LTDKW1WwVGrkRMmA2y9WZHXNHxMA6BSV",USDC_MINT_ADDRESS:"ExYxyorY2x3h8gLcuypnHFQV3F36rmMkQVn7HHV8mUy9",USDT_MINT_ADDRESS:"742wcXVzkhNuEePAot7L3GvPseh93pvYFPgyHLX8mUy9",FULL_TOKEN_NAME:"Wrapped Ethereum",ETH_MINT_ADDRESS:"71kRXzJMvSeArtXYNEWa8KAjpRJosdMQ7Dpgy5Jt5zfd",WRAPPED_TOKEN_SYMBOL:"WETH",INPUT_MINT_ADDRESS:J.default.toString(),OUTPUT_MINT_ADDRESS:"742wcXVzkhNuEePAot7L3GvPseh93pvYFPgyHLX8mUy9",POPULAR_TOKENS:{SOL:J.default.toString(),ETH:"71kRXzJMvSeArtXYNEWa8KAjpRJosdMQ7Dpgy5Jt5zfd",USDC:"ExYxyorY2x3h8gLcuypnHFQV3F36rmMkQVn7HHV8KjtA",USDT:"742wcXVzkhNuEePAot7L3GvPseh93pvYFPgyHLX8mUy9"},WSOL_DECIMALS:9},["svmbnb"]:{NATIVE_TOKEN_SYMBOL:"BNB",SWAP_HOST:"https://trade-api.svmbnb.cobaltx.io",BASE_HOST:"https://api.svmbnb.cobaltx.io",OWNER_BASE_HOST:"https://api.svmbnb.cobaltx.io",SERVICE_BASE_HOST:"https://api.svmbnb.cobaltx.io",MONITOR_BASE_HOST:"https://api.svmbnb.cobaltx.io",SERVICE_1_BASE_HOST:"https://api.svmbnb.cobaltx.io",ANALYTICS_BASE_HOST:"https://api.svmbnb.cobaltx.io",GITHUB_TOKEN_LIST_URL:"https://raw.githubusercontent.com/cobaltx-io/svmbnb-tokens/main/tokens.json",WSOL_MINT_ADDRESS:"So11111111111111111111111111111111111111112",GITHUB_LOGO_URL:"https://raw.githubusercontent.com/cobaltx-io/svmbnb-tokens/main/logo/So11111111111111111111111111111111111111112.svg",LOOKUP_TABLE_CACHE:"GSR5XQGWhpzV9TTduBJR6qPNjYsepMQSGkpaqfrbrW9w",CLMM_PROGRAMM_ID:"6f1b3xyVZbnWMHXBjgW1KPwxmPAgXcRdrvKNn4Nmf1Cn",ETH_MINT_ADDRESS:"H3xpFH1yN5E1zFdAoBrC9Y7m7RfJUeQNLvkHi8kNf9Qu",USDC_MINT_ADDRESS:"5odzAHDpDETYhupMkc6kjiYen3K2nrf8oPmvqmsoNW4L",USDT_MINT_ADDRESS:"s8cRDxnp3VxjB6khRnGDdxv6QbCUPyY9Vn1bgSQQsnG",SOL_MINT_ADDRESS:"So11111111111111111111111111111111111111112",FULL_TOKEN_NAME:"Wrapped BNB",WRAPPED_TOKEN_SYMBOL:"WBNB",INPUT_MINT_ADDRESS:J.default.toBase58(),OUTPUT_MINT_ADDRESS:"ABoT7umUXnx8RtJTeRqKB6myebVjP2wdJGQuV23jVVH3",POPULAR_TOKENS:{SOL:J.default.toString(),ETH:"H3xpFH1yN5E1zFdAoBrC9Y7m7RfJUeQNLvkHi8kNf9Qu",USDC:"5odzAHDpDETYhupMkc6kjiYen3K2nrf8oPmvqmsoNW4L",USDT:"s8cRDxnp3VxjB6khRnGDdxv6QbCUPyY9Vn1bgSQQsnG"},WSOL_DECIMALS:9},["svmbnb_testnet"]:{NATIVE_TOKEN_SYMBOL:"BNB",SWAP_HOST:"https://trade-api.svmbnb.cobaltx.io",BASE_HOST:"https://api.testnet.svmbnb.cobaltx.io",OWNER_BASE_HOST:"https://api.testnet.svmbnb.cobaltx.io",SERVICE_BASE_HOST:"https://api.testnet.svmbnb.cobaltx.io",MONITOR_BASE_HOST:"https://api.testnet.svmbnb.cobaltx.io",SERVICE_1_BASE_HOST:"https://api.testnet.svmbnb.cobaltx.io",ANALYTICS_BASE_HOST:"https://api.testnet.svmbnb.cobaltx.io",GITHUB_TOKEN_LIST_URL:"https://raw.githubusercontent.com/cobaltx-io/svmbnb-testnet-tokens/main/tokens.json",WSOL_MINT_ADDRESS:"So11111111111111111111111111111111111111112",GITHUB_LOGO_URL:"https://raw.githubusercontent.com/cobaltx-io/svmbnb-testnet-tokens/main/logo/So11111111111111111111111111111111111111112.svg",LOOKUP_TABLE_CACHE:"D79gSiTNV9pyLPJyscW5yZYwdPVDrtKsamL3ZfQi6JiM",CLMM_PROGRAMM_ID:"FgycD57kcCZsjpoTAi2u67GTPxm59bPEhycL2Lw1icm1",ETH_MINT_ADDRESS:"5aLr3sV4UNVcMB6CiA3PZTWbgVBRNyMdEp1s5LNfhR95",USDC_MINT_ADDRESS:"jrhUNRxEDpFmGV3sqaWyveS7bVtRQgtHsfBTCdjfMBw",USDT_MINT_ADDRESS:"Ex86C4gmdjvPRNrfyvsyejbjkP6RstNaBC5g3itZw2iv",FULL_TOKEN_NAME:"Wrapped BNB",WRAPPED_TOKEN_SYMBOL:"WBNB",INPUT_MINT_ADDRESS:"bnb",OUTPUT_MINT_ADDRESS:"jrhUNRxEDpFmGV3sqaWyveS7bVtRQgtHsfBTCdjfMBw",POPULAR_TOKENS:{SOL:J.default.toString(),ETH:"5aLr3sV4UNVcMB6CiA3PZTWbgVBRNyMdEp1s5LNfhR95",USDC:"jrhUNRxEDpFmGV3sqaWyveS7bVtRQgtHsfBTCdjfMBw",USDT:"Ex86C4gmdjvPRNrfyvsyejbjkP6RstNaBC5g3itZw2iv"},WSOL_DECIMALS:9},["svmbase_testnet"]:{NATIVE_TOKEN_SYMBOL:"ETH",SWAP_HOST:"https://trade-api.testnet.svmbase.cobaltx.io",BASE_HOST:"https://api.testnet.svmbase.cobaltx.io",OWNER_BASE_HOST:"https://api.testnet.svmbase.cobaltx.io",SERVICE_BASE_HOST:"https://api.testnet.svmbase.cobaltx.io",MONITOR_BASE_HOST:"https://api.testnet.svmbase.cobaltx.io",SERVICE_1_BASE_HOST:"https://api.testnet.svmbase.cobaltx.io",ANALYTICS_BASE_HOST:"https://api.testnet.svmbase.cobaltx.io",GITHUB_TOKEN_LIST_URL:"https://raw.githubusercontent.com/cobaltx-io/svmbase-testnet-tokens/main/tokens.json",WSOL_MINT_ADDRESS:"So11111111111111111111111111111111111111112",GITHUB_LOGO_URL:"https://raw.githubusercontent.com/cobaltx-io/svmbase-testnet-tokens/main/logo/So11111111111111111111111111111111111111112.png",LOOKUP_TABLE_CACHE:"AyPMvNmUUY2jwZ75NxhkLYPraQGNk5DssVfP4TnMcPJF",CLMM_PROGRAMM_ID:"DnZnBqJKiGSYHUHYvkXAbQmkMbwrjfgo4orR7jWyMsik",ETH_MINT_ADDRESS:"71kRXzJMvSeArtXYNEWa8KAjpRJosdMQ7Dpgy5Jt5zfd",USDC_MINT_ADDRESS:"63tPXWPnYeZ9hA3dJsAY5RcQ8YbwEz8uZ5nKu3uFD1aK",USDT_MINT_ADDRESS:"4fyX7hSmPetQC2wRS3YVAhWeFzif7WStarKwTvCXrBtD",FULL_TOKEN_NAME:"Wrapped Ethereum",WRAPPED_TOKEN_SYMBOL:"WETH",INPUT_MINT_ADDRESS:J.default.toString(),OUTPUT_MINT_ADDRESS:"63tPXWPnYeZ9hA3dJsAY5RcQ8YbwEz8uZ5nKu3uFD1aK",POPULAR_TOKENS:{SOL:J.default.toString(),ETH:"So11111111111111111111111111111111111111112",USDC:"63tPXWPnYeZ9hA3dJsAY5RcQ8YbwEz8uZ5nKu3uFD1aK",USDT:"4fyX7hSmPetQC2wRS3YVAhWeFzif7WStarKwTvCXrBtD"},WSOL_DECIMALS:9},["soonbase_mainnet"]:{NATIVE_TOKEN_SYMBOL:"ETH",SWAP_HOST:"https://trade-api.soonbase.cobaltx.io",BASE_HOST:"https://api.soonbase.cobaltx.io",OWNER_BASE_HOST:"https://api.soonbase.cobaltx.io",SERVICE_BASE_HOST:"https://api.soonbase.cobaltx.io",MONITOR_BASE_HOST:"https://api.soonbase.cobaltx.io",SERVICE_1_BASE_HOST:"https://api.soonbase.cobaltx.io",ANALYTICS_BASE_HOST:"https://api.soonbase.cobaltx.io",GITHUB_TOKEN_LIST_URL:"https://raw.githubusercontent.com/cobaltx-io/soonbase-tokens/main/tokens.json",WSOL_MINT_ADDRESS:"So11111111111111111111111111111111111111112",GITHUB_LOGO_URL:"https://raw.githubusercontent.com/cobaltx-io/soonbase-tokens/main/logo/So11111111111111111111111111111111111111112.png",LOOKUP_TABLE_CACHE:"FGRusuLycV6nhSPQc9VX55YjT6DCzJFc59mvTTarSTjK",CLMM_PROGRAMM_ID:"6f1b3xyVZbnWMHXBjgW1KPwxmPAgXcRdrvKNn4Nmf1Cn",ETH_MINT_ADDRESS:"71kRXzJMvSeArtXYNEWa8KAjpRJosdMQ7Dpgy5Jt5zfd",USDC_MINT_ADDRESS:"BoYN2f2gLQS4jpvMSV9dAZwfzKJiKGk4NPck5BjXfujo",USDT_MINT_ADDRESS:"J87bysZjwfS3n6KR21cwbdPZVJWNSFcXpWF5kiChAwQH",FULL_TOKEN_NAME:"Wrapped Ethereum",WRAPPED_TOKEN_SYMBOL:"WETH",INPUT_MINT_ADDRESS:J.default.toString(),OUTPUT_MINT_ADDRESS:"BoYN2f2gLQS4jpvMSV9dAZwfzKJiKGk4NPck5BjXfujo",POPULAR_TOKENS:{SOL:J.default.toString(),ETH:"FkvCG5DcuYCQ1BnPH4ZzTkCdk3VzutgyDAFBec4qAjhh",USDC:"BoYN2f2gLQS4jpvMSV9dAZwfzKJiKGk4NPck5BjXfujo",USDT:"J87bysZjwfS3n6KR21cwbdPZVJWNSFcXpWF5kiChAwQH"},WSOL_DECIMALS:9}},de=e=>mr[e];function ae(e){let t=de(e);return{chainId:101,address:t.WSOL_MINT_ADDRESS,programId:pr.toBase58(),decimals:9,symbol:t.WRAPPED_TOKEN_SYMBOL||"",name:t.FULL_TOKEN_NAME,logoURI:t.GITHUB_LOGO_URL,tags:[],priority:2,type:"cobaltx",extensions:{coingeckoId:"solana"}}}import{PublicKey as ot}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as gr}from"@solana/spl-token";import{PublicKey as C,SystemProgram as It,SYSVAR_RENT_PUBKEY as hr}from"@solana/web3.js";function nt({pubkey:e,isSigner:t=!1,isWritable:n=!0}){return{pubkey:e,isWritable:n,isSigner:t}}var zo=[nt({pubkey:gr,isWritable:!1}),nt({pubkey:It.programId,isWritable:!1}),nt({pubkey:hr,isWritable:!1})];function Ct({publicKey:e,transformSol:t,network:n}){let r=rt(e.toString());if(t&&n){let{WSOLMint:o}=gt(n);if(r instanceof C)return t&&r.equals(Te)?o:r;if(t&&r.toString()===Te.toBase58())return o}else if(r instanceof C)return r;if(typeof r=="string"){if(r===C.default.toBase58())return C.default;try{return new C(r)}catch{throw new Error("invalid public key")}}throw new Error("invalid public key")}function rt(e){try{return new C(e)}catch{return e}}var Xo=new C("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Zo=new C("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),Qo=new C("SysvarRent111111111111111111111111111111111"),Jo=new C("SysvarC1ock11111111111111111111111111111111"),$o=new C("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),ei=new C("Sysvar1nstructions1111111111111111111111111"),ti=It.programId,Te=C.default;function gt(e){let t=de(e);return{USDCMint:new C(t.USDC_MINT_ADDRESS),USDTMint:new C(t.USDT_MINT_ADDRESS),WSOLMint:new C(t.WSOL_MINT_ADDRESS),ETHMint:new C(t.ETH_MINT_ADDRESS),BONK_Mint:t.BONK_MINT_ADDRESS?new C(t.BONK_MINT_ADDRESS):C.default,SOL_Mint:t.SOL_MINT_ADDRESS?new C(t.SOL_MINT_ADDRESS):C.default,INPUT_MINT:new C(t.INPUT_MINT_ADDRESS),OUTPUT_MINT:new C(t.OUTPUT_MINT_ADDRESS)}}var se=class{constructor({mint:t,decimals:n,symbol:r,name:o,skipMint:i=!1,isToken2022:s=!1,network:a}){if(t===Te.toBase58()||t instanceof ot&&Te.equals(t)&&a){this.decimals=ae(a).decimals,this.symbol=ae(a).symbol,this.name=ae(a).name,this.mint=new ot(ae(a).address),this.isToken2022=!1;return}this.decimals=n,this.symbol=r||t.toString().substring(0,6),this.name=o||t.toString().substring(0,6),this.mint=i?ot.default:Ct({publicKey:t}),this.isToken2022=s}static getWSol(t){return new se({...ae(t),mint:ae(t).address})}equals(t){return this===t?!0:this.mint.equals(t.mint)}};import wr from"bn.js";var Dt=new N(new wr(100)),fe=class extends N{toSignificant(t=5,n,r){return this.mul(Dt).toSignificant(t,n,r)}toFixed(t=2,n,r){return this.mul(Dt).toFixed(t,n,r)}};var br=F("CobaltX_price"),$=class extends N{constructor(n){let{baseToken:r,quoteToken:o,numerator:i,denominator:s}=n;super(i,s);this.baseToken=r,this.quoteToken=o,this.scalar=new N(it(r.decimals),it(o.decimals))}get raw(){return new N(this.numerator,this.denominator)}get adjusted(){return super.mul(this.scalar)}invert(){return new $({baseToken:this.quoteToken,quoteToken:this.baseToken,denominator:this.numerator,numerator:this.denominator})}mul(n){this.quoteToken!==n.baseToken&&br.logWithError("mul token not equals");let r=super.mul(n);return new $({baseToken:this.baseToken,quoteToken:n.quoteToken,denominator:r.denominator,numerator:r.numerator})}toSignificant(n=this.quoteToken.decimals,r,o){return this.adjusted.toSignificant(n,r,o)}toFixed(n=this.quoteToken.decimals,r,o){return this.adjusted.toFixed(n,r,o)}};import{PublicKey as yr}from"@solana/web3.js";import Sr from"bn.js";function Mt(e){return typeof e=="object"&&e!==null&&![se,Q,yr,N,Sr,$,fe].some(t=>typeof t=="object"&&e instanceof t)}function ve(e){return typeof e=="string"?rt(e):Array.isArray(e)?e.map(t=>ve(t)):Mt(e)?Object.fromEntries(Object.entries(e).map(([t,n])=>[t,ve(n)])):e}var Tr=new ee(0),$i=new ee(1),es=new ee(2),ts=new ee(3),ns=new ee(5),tt=new ee(10),rs=new ee(100),os=new ee(1e3),is=new ee(1e4);function it(e){return tt.pow(D(e))}var Ot=e=>typeof e=="number";function Vt(e,t,n){let r=Ot(t)?t*((n==null?void 0:n.unit)==="s"?1e3:1):t;return new Date(e).getTime()<=r}function vt(e,t,n){let r=Ot(t)?t*((n==null?void 0:n.unit)==="s"?1e3:1):t;return new Date(e).getTime()>r}function ze(e,t=1,n=[]){let r=[...e];if(t<=0)return n;for(;r.length;)n.push(r.splice(0,t));return n}import{PublicKey as Rs}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Is}from"@solana/spl-token";import{ComputeBudgetProgram as ps,Keypair as ws,PublicKey as Pr,Transaction as Ts,TransactionMessage as Ss,VersionedTransaction as Ps}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as _s}from"@solana/spl-token";var Ns=F("CobaltX_txUtil");function ye(e,t){let[n,r]=Pr.findProgramAddressSync(e,t);return{publicKey:n,nonce:r}}import{PublicKey as S}from"@solana/web3.js";var Ft=new S("EhhTKczWMGQt46ynNeRX1WfeagwwJd7ufHvCDjRxjo5Q"),Kt=new S("CBuCnLe26faBpcBP2fktp4rp8abpcAnTWft6ZrP5Q4T"),Ut=new S("9KEPoZmtHUrBbhWN1v1KWLMkkvwY6WLtAVUCPRtRjP4z"),Wt=new S("FarmqiPv5eAj3j1GMdMCMUGXqPUvmquZtMy86QH6rzhG"),Vs=new S("CLaimxFqjHzgTJtAGHU47NPhg6qrc5sCnpC4tBLyABQS"),vs=new S("srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX"),Fs=new S("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin"),Ks=new S("675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8"),Us=new S("5quBtoiQqxF9Jv6KYKctB59NT3gtJD2Y65kdnB1Uev3h"),Ws=new S("CDSr3ssLcRB6XYPJwAfFt18MZvEZp4LjHcvzBVZ45duo");var Hs=new S("LockrWmn6K5twhz3y9w1dQERbmgSaRkfnTeTKbpofwE"),qs=new S("kN1kEznaF5Xbd8LYuqtEFcxzWSBk5Fv6ygX6SqEGJVy"),Gs=new S("routeUGWgWzqBWFcrCfv8tritsqukccJPu3q5GPP3xS"),js=new S("7YttLkHDoNj9wyDur5pM1ejNaAvT9X4eqaYcHQqtj2G5"),Ys=new S("6FJon3QE27qgPVggARueB22hLvoh22VzJpXv4rBEoSLF"),zs=new S("CC12se5To1CdEuw7fDS27B7Geo5jJyL7t5UK2B44NgiH"),Xs=new S("9HzJyW1qZsEiSfMUf6L2jo3CcTKAyBmSyKdwQeYisHrC"),Zs=new S("DropEU8AvevN3UrXWXTMuz3rqnMczQVNjq3kcSdW2SQi"),Qs=new S("CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C"),Js=new S("GpMZbSM2GgvTKHJirzeGfMFoaZ8UR2X7F4v8vHTvxFbL"),$s=new S("DNXgeM9EiiaAbaWvwjHj9fQQLAX5ZsfHyvmYUNRAdNC8"),xr=new S("CPMDWBwJDtYax9qW7AyRuVC19Cc4L4Vcy4n2BHAbHkCW"),Ar=new S("7rQ1QFNosMkUCuh7Z7fPbTHvh73b68sQYdirycEzJVuw"),Lr=new S("G11FKBRaAkHAKuLCgLM6K6NUc9rTjPAznRCjZifrTQe2"),ea=new S("LockrWmn6K5twhz3y9w1dQERbmgSaRkfnTeTKbpofwE"),_r=new S("DLockwT7X7sxtLmGH9g5kmfcjaBtncdbUmi738m5bvQC"),ta=new S("3f7GcQFG397GAaEnv51zR6tsTVihYRydnydDD1cXekxH"),Nr=new S("7AFUeLVRjBfzqK3tTGw8hN48KLQWSk6DTE8xprWdPqix");var na={SERUM_MARKET:S.default,OPENBOOK_MARKET:new S("EoTcMgcDRTJVZDMZWBoU6rhYHZfkNTVEAfz3uUJRcYGj"),UTIL1216:S.default,FarmV3:new S("85BFyr98MbCUU9MVTEgzx1nbhWACbJqLzho6zd6DZcWL"),FarmV5:new S("EcLzTrNg9V7qhcdyXDe2qjtPkiGzDM2UbdRaeaadU5r2"),FarmV6:new S("Farm2hJLcqPtPg8M4rR6DMrsRNc5TPm5Cs4bVQrMe2T7"),AmmV4:new S("HWy1jotHpo6UqeQxx49dpYYdQB8wj9Qk9MdxwjLvDHB8"),AmmStable:new S("DDg4VmQaJV9ogWce7LpcjBA9bv22wRp5uaTPa5pGjijF"),CLMM:new S("devi51mZmdwUJGU9hjN27vEz64Gps7uUefqxg27EAtH"),CLMM_LOCK_PROGRAM_ID:new S("DLockwT7X7sxtLmGH9g5kmfcjaBtncdbUmi738m5bvQC"),CLMM_LOCK_AUTH_ID:new S("8qmHNvu2Kr2C7U8mJL4Vz1vTDxMhVuXKREwU7TNoaVEo"),Router:new S("BVChZ3XFEwTMUk1o9i3HAf91H6mFxSwa5X2wFAWhYPhU"),CREATE_CPMM_POOL_PROGRAM:xr,CREATE_CPMM_POOL_AUTH:Ar,CREATE_CPMM_POOL_FEE_ACC:Lr,FEE_DESTINATION_ID:new S("3XMrhbv989VxAMi3DErLV9eJht1pHppW5LbKxe9fkEFR"),LOCK_CPMM_PROGRAM:_r,LCOK_CPMM_AUTH:Nr};import ia from"bn.js";import{PublicKey as ca,AddressLookupTableAccount as la}from"@solana/web3.js";import{PublicKey as Na,sendAndConfirmTransaction as Ea,Transaction as Ra,TransactionMessage as Ia,VersionedTransaction as Ca}from"@solana/web3.js";import Ma from"axios";import{PublicKey as Dr}from"@solana/web3.js";import jt,{isBN as Yt}from"bn.js";import{bits as su,BitStructure as au,blob as Er,Blob as uu,cstr as cu,f32 as lu,f32be as du,f64 as fu,f64be as mu,greedy as pu,Layout as Br,ns64 as gu,ns64be as hu,nu64 as wu,nu64be as bu,offset as Tu,s16 as yu,s16be as Su,s24 as Pu,s24be as xu,s32 as Au,s32be as Lu,s40 as _u,s40be as Nu,s48 as Eu,s48be as Bu,s8 as Ru,seq as Rr,struct as ku,Structure as kr,u16 as Iu,u16be as Cu,u24 as Du,u24be as Mu,u32 as Ou,u32be as Vu,u40 as vu,u40be as Fu,u48 as Ku,u48be as Uu,u8 as Ir,UInt as Cr,union as Wu,Union as Hu,unionLayoutDiscriminator as qu,utf8 as Gu}from"@solana/buffer-layout";var st=Br,Ht=kr;var at=Cr;var qt=Ir;var Gt=Rr;var ue=Er;var me=class extends st{constructor(n,r,o){super(n,o);this.blob=ue(n),this.signed=r}decode(n,r=0){let o=new jt(this.blob.decode(n,r),10,"le");return this.signed?o.fromTwos(this.span*8).clone():o}encode(n,r,o=0){return typeof n=="number"&&(n=new jt(n)),this.signed&&(n=n.toTwos(this.span*8)),this.blob.encode(n.toArrayLike(Buffer,"le",this.span),r,o)}};function M(e){return new at(1,e)}function Ke(e){return new at(4,e)}function g(e){return new me(8,!1,e)}function te(e){return new me(16,!1,e)}function zt(e){return new me(1,!0,e)}function Ue(e){return new me(8,!0,e)}var Fe=class extends st{constructor(n,r,o,i){super(n.span,i);this.layout=n,this.decoder=r,this.encoder=o}decode(n,r){return this.decoder(this.layout.decode(n,r))}encode(n,r,o){return this.layout.encode(this.encoder(n),r,o)}getSpan(n,r){return this.layout.getSpan(n,r)}};function P(e){return new Fe(ue(32),t=>new Dr(t),t=>t.toBuffer(),e)}function ct(e){return new Fe(qt(),Mr,Or,e)}function Mr(e){if(e===0)return!1;if(e===1)return!0;throw new Error("Invalid bool: "+e)}function Or(e){return e?1:0}var ut=class extends Ht{decode(t,n){return super.decode(t,n)}};function k(e,t,n){return new ut(e,t,n)}function I(e,t,n){let r,o=typeof t=="number"?t:Yt(t)?t.toNumber():new Proxy(t,{get(i,s){if(!r){let a=Reflect.get(i,"count");r=Yt(a)?a.toNumber():a,Reflect.set(i,"count",r)}return Reflect.get(i,s)},set(i,s,a){return s==="count"&&(r=a),Reflect.set(i,s,a)}});return Gt(e,o,n)}var lt=k([P("mint"),P("owner"),g("amount"),Ke("delegateOption"),P("delegate"),M("state"),Ke("isNativeOption"),g("isNative"),g("delegatedAmount"),Ke("closeAuthorityOption"),P("closeAuthority")]);import{PublicKey as rn}from"@solana/web3.js";var dc=k([M("instruction")]),fc=k([M("instruction")]),Vr=k([g("rewardState"),g("rewardOpenTime"),g("rewardEndTime"),g("rewardLastUpdateTime"),g("totalReward"),g("totalRewardEmissioned"),g("rewardClaimed"),g("rewardPerSecond"),te("accRewardPerShare"),P("rewardVault"),P("rewardMint"),P("rewardSender"),g("rewardType"),I(g(),15,"padding")]),vr=k([g("state"),g("nonce"),P("lpVault"),P("rewardVault"),P(),P(),g(),g(),g("totalReward"),te("perShareReward"),g("lastSlot"),g("perSlotReward")]),Fr=k([g("state"),g("nonce"),P("lpVault"),P("rewardVaultA"),g("totalRewardA"),te("perShareRewardA"),g("perSlotRewardA"),M("option"),P("rewardVaultB"),ue(7),g("totalRewardB"),te("perShareRewardB"),g("perSlotRewardB"),g("lastSlot"),P()]),Kr=k([g(),g("state"),g("nonce"),g("validRewardTokenNum"),te("rewardMultiplier"),g("rewardPeriodMax"),g("rewardPeriodMin"),g("rewardPeriodExtend"),P("lpMint"),P("lpVault"),I(Vr,5,"rewardInfos"),P("creator"),P(),I(g(),32,"padding")]),Xt=new Proxy(vr,{get(e,t,n){return t==="decode"?(...r)=>{let o=e.decode(...r);return{...o,version:3,rewardInfos:[{rewardVault:o.rewardVault,totalReward:o.totalReward,perSlotReward:o.perSlotReward,perShareReward:o.perShareReward}]}}:Reflect.get(e,t,n)}}),Zt=new Proxy(Fr,{get(e,t,n){return t==="decode"?(...r)=>{let o=e.decode(...r);return{...o,version:5,rewardInfos:[{rewardVault:o.rewardVaultA,totalReward:o.totalRewardA,perSlotReward:o.perSlotRewardA,perShareReward:o.perShareRewardA},{rewardVault:o.rewardVaultB,totalReward:o.totalRewardB,perSlotReward:o.perSlotRewardB,perShareReward:o.perShareRewardB}]}}:Reflect.get(e,t,n)}}),Qt=new Proxy(Kr,{get(e,t,n){return t==="decode"?(...r)=>{let o=e.decode(...r);return{...o,version:6,rewardInfos:o.rewardInfos.map(i=>{var s;return{...i,rewardType:((s=Object.entries(We).find(a=>String(a[1])===i.rewardType.toString()))!=null?s:["Standard SPL"])[0]}})}}:Reflect.get(e,t,n)}}),Ur=k([g("isSet"),g("rewardPerSecond"),g("rewardOpenTime"),g("rewardEndTime"),g("rewardType")]),mc=k([M("instruction"),g("nonce"),I(Ur,5,"rewardTimeInfo")]),pc=k([M("instruction"),g("rewardReopenTime"),g("rewardEndTime"),g("rewardPerSecond")]),gc=k([M("instruction"),g("isSet"),g("rewardPerSecond"),g("rewardOpenTime"),g("rewardEndTime"),g("rewardType")]),hc=k([g("state"),P("id"),P("owner"),g("deposited"),I(g(),1,"rewardDebts")]),Jt=k([g("state"),P("id"),P("owner"),g("deposited"),I(te(),1,"rewardDebts"),g(""),g("voteLockedBalance"),I(g(),15)]),wc=k([g("state"),P("id"),P("owner"),g("deposited"),I(g(),2,"rewardDebts")]),$t=k([g("state"),P("id"),P("owner"),g("deposited"),I(te(),2,"rewardDebts"),I(g(),17)]),en=k([g(),g("state"),P("id"),P("owner"),g("deposited"),I(te(),5,"rewardDebts"),I(g(),16)]),bc=k([M("instruction"),g("amount")]),Wr=k([P("mint"),P("grantAuthority"),g("baselineVoteWeightScaledFactor"),g("maxExtraLockupVoteWeightScaledFactor"),g("lockupSaturationSecs"),zt("digitShift"),I(M(),7,"reserved1"),I(g(),7,"reserved2")]),tn=k([ue(8),P("governanceProgramId"),P("realm"),P("realmGoverningTokenMint"),P("realmAuthority"),I(M(),32,"reserved1"),I(Wr,4,"votingMints"),Ue("timeOffset"),M("bump"),I(M(),7,"reserved2"),I(g(),11,"reserved3")]),Hr=k([Ue("startTime"),Ue("endTime"),M("kind"),I(M(),15,"reserved")]),qr=k([I(Hr,1,"lockup"),g("amountDeposited_native"),g("amountInitiallyLockedNative"),ct("isUsed"),ct("allowClawback"),M("votingMintConfigIdx"),I(M(),29,"reserved")]),nn=k([ue(8),P("voterAuthority"),P("registrar"),I(qr,32,"deposits"),M("voterBump"),M("voterWweightRecordBump"),I(M(),94,"reserved")]);var _c=F("CobaltX_farm_config"),Nc=new rn("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),Ec=new rn("FrspKwj8i3pNmKwXreTveC4fu7KL5ZbGeXdZBe2XViu1"),on={3:Xt,5:Zt,6:Qt},sn={3:Jt,5:$t,6:en};var We={"Standard SPL":0,"Option tokens":1},Bc={[Ft.toString()]:3,[Kt.toString()]:4,[Ut.toString()]:5,[Wt.toString()]:6};var Se=F("CobaltX.farm.util");function qc({programId:e,poolId:t,mint:n,type:r}){let{publicKey:o}=ye([t.toBuffer(),n.toBuffer(),Buffer.from(r==="lpVault"?"lp_vault_associated_seed":r==="rewardVault"?"reward_vault_associated_seed":"","utf-8")],e);return o}function Gr({programId:e,poolId:t,owner:n,version:r}){let{publicKey:o}=ye([t.toBuffer(),n.toBuffer(),Buffer.from(r===6?"farmer_info_associated_seed":"staker_info_v2_associated_seed","utf-8")],e);return o}var Gc=({programId:e,poolId:t})=>ye([t.toBuffer()],e);function jc(e){return{isSet:new q(1),rewardPerSecond:D(e.perSecond),rewardOpenTime:D(e.openTime),rewardEndTime:D(e.endTime),rewardType:D(We[e.rewardType])}}function Yc(e){return D(e.endTime).sub(D(e.openTime)).mul(D(e.perSecond))}function jr(e){let t=sn[e];return t||Se.logWithError("invalid version",e),t}function Yr(e){let t=on[e];return t||Se.logWithError("invalid version",e),t}function zr(e,t,n,r){if(e.version===3||e.version===5){if(e.lastSlot.gte(new q(n)))return e;let o=new q(n).sub(e.lastSlot);e.lastSlot=new q(n);for(let i of e.rewardInfos){if(t.amount.eq(new q(0)))continue;let s=i.perSlotReward.mul(o);i.perShareReward=i.perShareReward.add(s.mul(new q(10).pow(new q(e.version===3?9:15))).div(t.amount)),i.totalReward=i.totalReward.add(s)}}else if(e.version===6)for(let o of e.rewardInfos){if(o.rewardState.eq(new q(0)))continue;let i=q.min(new q(r),o.rewardEndTime);if(o.rewardOpenTime.gte(i))continue;let a=i.sub(o.rewardLastUpdateTime).mul(o.rewardPerSecond),c=o.totalReward.sub(o.totalRewardEmissioned);c.lt(a)?(a=c,o.rewardLastUpdateTime=o.rewardLastUpdateTime.add(c.div(o.rewardPerSecond))):o.rewardLastUpdateTime=i,!t.amount.eq(new q(0))&&(o.accRewardPerShare=o.accRewardPerShare.add(a.mul(e.rewardMultiplier).div(t.amount)),o.totalRewardEmissioned=o.totalRewardEmissioned.add(a))}return e}async function zc({connection:e,farmPools:t,owner:n,config:r,chainTime:o}){let i=!1,s=!1,a=new q(10),c=[];for(let f of t){let p=ve(f);p.version===6?s=!0:i=!0,c.push({pubkey:p.id,version:p.version,key:"state",poolId:p.id},{pubkey:p.lpVault,version:p.version,key:"lpVault",poolId:p.id}),n&&c.push({pubkey:Gr({programId:p.programId,poolId:p.id,owner:n,version:f.version}),version:p.version,key:"ledger",poolId:p.id})}let u={},l=await pt(e,c,r);for(let{pubkey:f,version:p,key:y,poolId:b,accountInfo:_}of l){let x=b.toBase58();if(u[x]={...u[x]},y==="state"){let O=Yr(p);(!_||!_.data||_.data.length!==O.span)&&Se.logWithError(`invalid farm state account info, pools.id, ${f}`),u[x].state=O.decode(_.data)}else if(y==="lpVault")(!_||!_.data||_.data.length!==lt.span)&&Se.logWithError(`invalid farm lp vault account info, pools.lpVault, ${f}`),u[x].lpVault=lt.decode(_.data);else if(y==="ledger"){let O=jr(p);_&&_.data&&(_.data.length!==O.span&&Se.logWithError(`invalid farm ledger account info, ledger, ${f}`),u[x].ledger=O.decode(_.data))}}let d=s||i?await e.getSlot():0;for(let f of Object.keys(u))u[f]!==void 0&&(u[f].state=zr(u[f].state,u[f].lpVault,d,o));for(let[f,{state:p,ledger:y}]of Object.entries(u))if(y){let b=p.version===6?p.rewardMultiplier:p.rewardInfos.length===1?a.pow(new q(9)):a.pow(new q(15)),_=p.rewardInfos.map((x,O)=>{let E=y.rewardDebts[O];return y.deposited.mul(p.version===6?x.accRewardPerShare:x.perShareReward).div(b).sub(E)});u[f].wrapped={...u[f].wrapped,pendingRewards:_}}return u}function Xc(e,t=Date.now()){if(e.version===6){let n=e.state.rewardInfos;if(n.every(({rewardOpenTime:r})=>Vt(t,r.toNumber(),{unit:"s"})))return"upcoming pool";if(n.every(({rewardEndTime:r})=>vt(t,r.toNumber(),{unit:"s"})))return"closed pool"}else{let n=e.state.rewardInfos.map(({perSlotReward:r})=>r);if(n.length===2){if(String(n[0])==="0"&&String(n[1])!=="0")return"normal fusion pool";if(String(n[0])!=="0"&&String(n[1])!=="0")return"dual fusion pool";if(String(n[0])==="0"&&String(n[1])==="0")return"closed pool"}else if(n.length===1&&String(n[0])==="0")return"closed pool"}}async function Zc(e,t,n,r){let o=await e.getAccountInfo(t);if(o===null)throw Error("registrar info check error");let s=tn.decode(o.data).votingMints.findIndex(l=>l.mint.equals(r));if(s===-1)throw Error("find voter mint error");let a=await e.getAccountInfo(n);if(a===null)return{index:s,isInit:!1};let u=nn.decode(a.data).deposits.findIndex(l=>l.isUsed&&l.votingMintConfigIdx===s);return u===-1?{index:s,isInit:!1}:{index:u,isInit:!0}}export{Yc as calFarmRewardAmount,jc as farmRewardInfoToConfig,zc as fetchMultipleFarmInfoAndUpdate,Gc as getAssociatedAuthority,Gr as getAssociatedLedgerAccount,qc as getAssociatedLedgerPoolAccount,Zc as getDepositEntryIndex,jr as getFarmLedgerLayout,Yr as getFarmStateLayout,Xc as judgeFarmType,zr as updateFarmPoolInfo};
/*!
 *  decimal.js v10.5.0
 *  An arbitrary-precision Decimal type for JavaScript.
 *  https://github.com/MikeMcl/decimal.js
 *  Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
 *  MIT Licence
 */
//# sourceMappingURL=util.mjs.map