@raydium-io/raydium-sdk-v2
Version:
An SDK for building applications on top of Raydium.
2 lines • 59 kB
JavaScript
var pn=Object.defineProperty,gn=Object.defineProperties;var hn=Object.getOwnPropertyDescriptors;var wt=Object.getOwnPropertySymbols;var wn=Object.prototype.hasOwnProperty,yn=Object.prototype.propertyIsEnumerable;var yt=(e,t,n)=>t in e?pn(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,O=(e,t)=>{for(var n in t||(t={}))wn.call(t,n)&&yt(e,n,t[n]);if(wt)for(var n of wt(t))yn.call(t,n)&&yt(e,n,t[n]);return e},G=(e,t)=>gn(e,hn(t));import j from"bn.js";import{PublicKey as xn}from"@solana/web3.js";import{get as bt,set as bn}from"lodash";var Ze=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}},Tt={},Tn={};function K(e){let t=bt(Tt,e);if(!t){let n=bt(Tn,e);t=new Ze({name:e,logLevel:n}),bn(Tt,e,t)}return t}import{MINT_SIZE as pi,TOKEN_PROGRAM_ID as gi,getTransferFeeConfig as hi,unpackMint as wi}from"@solana/spl-token";var Ye=K("Raydium_accountInfo_util");async function xt(e,t,n){let{batchRequest:r,commitment:i="confirmed",chunkCount:o=100}=O({batchRequest:!1},n),s=ze(t,o),a=new Array(s.length).fill([]);if(r){let c=s.map(d=>{let f=e._buildArgs([d.map(p=>p.toBase58())],i,"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:x,lamports:b,owner:R,rentEpoch:S}=f;return p.length!==2&&p[1]!=="base64"&&Ye.logWithError("info must be base64 encoded, RPC_ERROR"),{data:Buffer.from(p[0],"base64"),executable:x,lamports:b,owner:new xn(R),rentEpoch:S}}return null})))}else try{a=await Promise.all(s.map(c=>e.getMultipleAccountsInfo(c,i)))}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 xt(e,t.map(i=>i.pubkey),n);return t.map((i,o)=>G(O({},i),{accountInfo:r[o]}))}import te from"bn.js";var le=9e15,se=1e9,Xe="0123456789abcdef",Ne="2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058",ke="3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789",Qe={precision:20,rounding:4,modulo:1,toExpNeg:-7,toExpPos:21,minE:-le,maxE:le,crypto:!1},Rt,Q,T=!0,_e="[DecimalError] ",oe=_e+"Invalid argument: ",Bt=_e+"Precision limit exceeded",Nt=_e+"crypto unavailable",kt="[object Decimal]",q=Math.floor,D=Math.pow,Pn=/^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i,Sn=/^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i,Ln=/^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i,It=/^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,z=1e7,y=7,An=9007199254740991,Rn=Ne.length-1,$e=ke.length-1,m={toStringTag:kt};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,i=r.constructor;if(e=new i(e),t=new i(t),!e.s||!t.s)return new i(NaN);if(e.gt(t))throw Error(oe+t);return n=r.cmp(e),n<0?e:r.cmp(t)>0?t:new i(r)};m.comparedTo=m.cmp=function(e){var t,n,r,i,o=this,s=o.d,a=(e=new o.constructor(e)).d,c=o.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(o.e!==e.e)return o.e>e.e^c<0?1:-1;for(r=s.length,i=a.length,t=0,n=r<i?r:i;t<n;++t)if(s[t]!==a[t])return s[t]>a[t]^c<0?1:-1;return r===i?0:r>i^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())+y,r.rounding=1,n=Bn(r,vt(r,n)),r.precision=e,r.rounding=t,h(Q==2||Q==3?n.neg():n,e,t,!0)):new r(1):new r(NaN)};m.cubeRoot=m.cbrt=function(){var e,t,n,r,i,o,s,a,c,u,l=this,d=l.constructor;if(!l.isFinite()||l.isZero())return new d(l);for(T=!1,o=l.s*D(l.s*l,1/3),!o||Math.abs(o)==1/0?(n=U(l.d),e=l.e,(o=(e-n.length+1)%3)&&(n+=o==1||o==-2?"0":"00"),o=D(n,1/3),e=q((e+1)/3)-(e%3==(e<0?-1:2)),o==1/0?n="5e"+e:(n=o.toExponential(),n=n.slice(0,n.indexOf("e")+1)+e),r=new d(n),r.s=l.s):r=new d(o.toString()),s=(e=d.precision)+3;;)if(a=r,c=a.times(a).times(a),u=c.plus(l),r=k(u.plus(l).times(a),u.plus(c),s+2,1),U(a.d).slice(0,s)===(n=U(r.d)).slice(0,s))if(n=n.slice(s-3,s+1),n=="9999"||!i&&n=="4999"){if(!i&&(h(a,e+1,0),a.times(a).times(a).eq(l))){r=a;break}s+=4,i=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-q(this.e/y))*y,e=t[e],e)for(;e%10==0;e/=10)n--;n<0&&(n=0)}return n};m.dividedBy=m.div=function(e){return k(this,new this.constructor(e))};m.dividedToIntegerBy=m.divToInt=function(e){var t=this,n=t.constructor;return h(k(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,i,o=this,s=o.constructor,a=new s(1);if(!o.isFinite())return new s(o.s?1/0:NaN);if(o.isZero())return a;n=s.precision,r=s.rounding,s.precision=n+Math.max(o.e,o.sd())+4,s.rounding=1,i=o.d.length,i<32?(e=Math.ceil(i/3),t=(1/Fe(4,e)).toString()):(e=16,t="2.3283064365386962890625e-10"),o=de(s,1,o.times(t),new s(1),!0);for(var c,u=e,l=new s(8);u--;)c=o.times(o),o=a.minus(c.times(l.minus(c.times(l))));return h(o,s.precision=n,s.rounding=r,!0)};m.hyperbolicSine=m.sinh=function(){var e,t,n,r,i=this,o=i.constructor;if(!i.isFinite()||i.isZero())return new o(i);if(t=o.precision,n=o.rounding,o.precision=t+Math.max(i.e,i.sd())+4,o.rounding=1,r=i.d.length,r<3)i=de(o,2,i,i,!0);else{e=1.4*Math.sqrt(r),e=e>16?16:e|0,i=i.times(1/Fe(5,e)),i=de(o,2,i,i,!0);for(var s,a=new o(5),c=new o(16),u=new o(20);e--;)s=i.times(i),i=i.times(a.plus(s.times(c.times(s).plus(u))))}return o.precision=t,o.rounding=n,h(i,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,k(n.sinh(),n.cosh(),r.precision=e,r.rounding=t)):new r(n.s)};m.inverseCosine=m.acos=function(){var e,t=this,n=t.constructor,r=t.abs().cmp(1),i=n.precision,o=n.rounding;return r!==-1?r===0?t.isNeg()?Y(n,i,o):new n(0):new n(NaN):t.isZero()?Y(n,i+4,o).times(.5):(n.precision=i+6,n.rounding=1,t=t.asin(),e=Y(n,i+4,o).times(.5),n.precision=i,n.rounding=o,e.minus(t))};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,i=this,o=i.constructor;return i.isFinite()?i.e>=0?new o(i.abs().eq(1)?i.s/0:i.isZero()?i:NaN):(e=o.precision,t=o.rounding,r=i.sd(),Math.max(r,e)<2*-i.e-1?h(new o(i),e,t,!0):(o.precision=n=r-i.e,i=k(i.plus(1),new o(1).minus(i),n+e,1),o.precision=e+4,o.rounding=1,i=i.ln(),o.precision=e,o.rounding=t,i.times(.5))):new o(NaN)};m.inverseSine=m.asin=function(){var e,t,n,r,i=this,o=i.constructor;return i.isZero()?new o(i):(t=i.abs().cmp(1),n=o.precision,r=o.rounding,t!==-1?t===0?(e=Y(o,n+4,r).times(.5),e.s=i.s,e):new o(NaN):(o.precision=n+6,o.rounding=1,i=i.div(new o(1).minus(i.times(i)).sqrt().plus(1)).atan(),o.precision=n,o.rounding=r,i.times(2)))};m.inverseTangent=m.atan=function(){var e,t,n,r,i,o,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<=$e)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<=$e)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/y+2|0),e=n;e;--e)u=u.div(u.times(u).plus(1).sqrt().plus(1));for(T=!1,t=Math.ceil(a/y),r=1,c=u.times(u),s=new l(u),i=u;e!==-1;)if(i=i.times(c),o=s.minus(i.div(r+=2)),i=i.times(c),s=o.plus(i.div(r+=2)),s.d[t]!==void 0)for(e=t;s.d[e]===o.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&&q(this.e/y)>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,i,o,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)o=!0;else{for(i=n[0];i%10===0;)i/=10;o=i!==1}if(T=!1,a=d+p,s=ie(u,a),r=t?Ie(l,a+10):ie(e,a),c=k(s,r,a,1),ge(c.d,i=d,f))do if(a+=10,s=ie(u,a),r=t?Ie(l,a+10):ie(e,a),c=k(s,r,a,1),!o){+U(c.d).slice(i+1,i+15)+1==1e14&&(c=h(c,d+1,0));break}while(ge(c.d,i+=10,f));return T=!0,h(c,d,f)};m.minus=m.sub=function(e){var t,n,r,i,o,s,a,c,u,l,d,f,p=this,x=p.constructor;if(e=new x(e),!p.d||!e.d)return!p.s||!e.s?e=new x(NaN):p.d?e.s=-e.s:e=new x(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=x.precision,c=x.rounding,!u[0]||!f[0]){if(f[0])e.s=-e.s;else if(u[0])e=new x(p);else return new x(c===3?-0:0);return T?h(e,a,c):e}if(n=q(e.e/y),l=q(p.e/y),u=u.slice(),o=l-n,o){for(d=o<0,d?(t=u,o=-o,s=f.length):(t=f,n=l,s=u.length),r=Math.max(Math.ceil(a/y),s)+2,o>r&&(o=r,t.length=1),t.reverse(),r=o;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}o=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>o;){if(u[--r]<f[r]){for(i=r;i&&u[--i]===0;)u[i]=z-1;--u[i],u[r]+=z}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=Ce(u,n),T?h(e,a,c):e):new x(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=k(n,e.abs(),0,3,1),t.s*=e.s):t=k(n,e,0,r.modulo,1),t=t.times(e),T=!0,n.minus(t))};m.naturalExponential=m.exp=function(){return et(this)};m.naturalLogarithm=m.ln=function(){return ie(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,i,o,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(o=q(d.e/y),r=q(e.e/y),u=u.slice(),i=o-r,i){for(i<0?(n=u,i=-i,s=l.length):(n=l,r=o,s=u.length),o=Math.ceil(a/y),s=o>s?o+1:s+1,i>s&&(i=s,n.length=1),n.reverse();i--;)n.push(0);n.reverse()}for(s=u.length,i=l.length,s-i<0&&(i=s,n=l,l=u,u=n),t=0;i;)t=(u[--i]=u[i]+l[i]+t)/z|0,u[i]%=z;for(t&&(u.unshift(t),++r),s=u.length;u[--s]==0;)u.pop();return e.d=u,e.e=Ce(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=_t(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())+y,r.rounding=1,n=kn(r,vt(r,n)),r.precision=e,r.rounding=t,h(Q>2?n.neg():n,e,t,!0)):new r(NaN)};m.squareRoot=m.sqrt=function(){var e,t,n,r,i,o,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=U(a),(t.length+c)%2==0&&(t+="0"),u=Math.sqrt(t),c=q((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(o=r,r=o.plus(k(s,o,n+2,1)).times(.5),U(o.d).slice(0,n)===(t=U(r.d)).slice(0,n))if(t=t.slice(n-3,n+1),t=="9999"||!i&&t=="4999"){if(!i&&(h(o,c+1,0),o.times(o).eq(s))){r=o;break}n+=4,i=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=k(n,new r(1).minus(n.times(n)).sqrt(),e+10,0),r.precision=e,r.rounding=t,h(Q==2||Q==4?n.neg():n,e,t,!0)):new r(NaN)};m.times=m.mul=function(e){var t,n,r,i,o,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=q(l.e/y)+q(e.e/y),c=f.length,u=p.length,c<u&&(o=f,f=p,p=o,s=c,c=u,u=s),o=[],s=c+u,r=s;r--;)o.push(0);for(r=u;--r>=0;){for(t=0,i=c+r;i>r;)a=o[i]+p[r]*f[i-r-1]+t,o[i--]=a%z|0,t=a/z|0;o[i]=(o[i]+t)%z|0}for(;!o[--s];)o.pop();return t?++n:o.shift(),e.d=o,e.e=Ce(o,n),T?h(e,d.precision,d.rounding):e};m.toBinary=function(e,t){return nt(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,se),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,i=r.constructor;return e===void 0?n=J(r,!0):(H(e,0,se),t===void 0?t=i.rounding:H(t,0,8),r=h(new i(r),e+1,t),n=J(r,!0,e+1)),r.isNeg()&&!r.isZero()?"-"+n:n};m.toFixed=function(e,t){var n,r,i=this,o=i.constructor;return e===void 0?n=J(i):(H(e,0,se),t===void 0?t=o.rounding:H(t,0,8),r=h(new o(i),e+i.e+1,t),n=J(r,!1,e+r.e+1)),i.isNeg()&&!i.isZero()?"-"+n:n};m.toFraction=function(e){var t,n,r,i,o,s,a,c,u,l,d,f,p=this,x=p.d,b=p.constructor;if(!x)return new b(p);if(u=n=new b(1),r=c=new b(0),t=new b(r),o=t.e=_t(x)-p.e-1,s=o%y,t.d[0]=D(10,s<0?y+s:s),e==null)e=o>0?t:u;else{if(a=new b(e),!a.isInt()||a.lt(u))throw Error(oe+a);e=a.gt(t)?o>0?t:u:a}for(T=!1,a=new b(U(x)),l=b.precision,b.precision=o=x.length*y*2;d=k(a,t,0,1,1),i=n.plus(d.times(r)),i.cmp(e)!=1;)n=r,r=i,i=u,u=c.plus(d.times(i)),c=i,i=t,t=a.minus(d.times(i)),a=i;return i=k(e.minus(n),r,0,1,1),c=c.plus(i.times(u)),n=n.plus(i.times(r)),c.s=u.s=p.s,f=k(u,r,o,1).minus(p).abs().cmp(k(c,n,o,1).minus(p).abs())<1?[u,r]:[c,n],b.precision=l,T=!0,f};m.toHexadecimal=m.toHex=function(e,t){return nt(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=k(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 nt(this,8,e,t)};m.toPower=m.pow=function(e){var t,n,r,i,o,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(D(+a,u));if(a=new c(a),a.eq(1))return a;if(r=c.precision,o=c.rounding,e.eq(1))return h(a,r,o);if(t=q(e.e/y),t>=e.d.length-1&&(n=u<0?-u:u)<=An)return i=Ct(c,a,n,r),e.s<0?new c(1).div(i):h(i,r,o);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=D(+a,u),t=n==0||!isFinite(n)?q(u*(Math.log("0."+U(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),i=et(e.times(ie(a,r+n)),r),i.d&&(i=h(i,r+5,1),ge(i.d,r,o)&&(t=r+10,i=h(et(e.times(ie(a,t+n)),t),t+5,1),+U(i.d).slice(r+1,r+15)+1==1e14&&(i=h(i,r+1,0)))),i.s=s,T=!0,c.rounding=o,h(i,r,o))};m.toPrecision=function(e,t){var n,r=this,i=r.constructor;return e===void 0?n=J(r,r.e<=i.toExpNeg||r.e>=i.toExpPos):(H(e,1,se),t===void 0?t=i.rounding:H(t,0,8),r=h(new i(r),e,t),n=J(r,e<=r.e||r.e<=i.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,se),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=J(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=J(e,e.e<=t.toExpNeg||e.e>=t.toExpPos);return e.isNeg()?"-"+n:n};function U(e){var t,n,r,i=e.length-1,o="",s=e[0];if(i>0){for(o+=s,t=1;t<i;t++)r=e[t]+"",n=y-r.length,n&&(o+=re(n)),o+=r;s=e[t],r=s+"",n=y-r.length,n&&(o+=re(n))}else if(s===0)return"0";for(;s%10===0;)s/=10;return o+s}function H(e,t,n){if(e!==~~e||e<t||e>n)throw Error(oe+e)}function ge(e,t,n,r){var i,o,s,a;for(o=e[0];o>=10;o/=10)--t;return--t<0?(t+=y,i=0):(i=Math.ceil((t+1)/y),t%=y),o=D(10,y-t),a=e[i]%o|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==o||n>3&&a+1==o/2)&&(e[i+1]/o/100|0)==D(10,t-2)-1||(a==o/2||a==0)&&(e[i+1]/o/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==o||!r&&n>3&&a+1==o/2)&&(e[i+1]/o/1e3|0)==D(10,t-3)-1,s}function Be(e,t,n){for(var r,i=[0],o,s=0,a=e.length;s<a;){for(o=i.length;o--;)i[o]*=t;for(i[0]+=Xe.indexOf(e.charAt(s++)),r=0;r<i.length;r++)i[r]>n-1&&(i[r+1]===void 0&&(i[r+1]=0),i[r+1]+=i[r]/n|0,i[r]%=n)}return i.reverse()}function Bn(e,t){var n,r,i;if(t.isZero())return t;r=t.d.length,r<32?(n=Math.ceil(r/3),i=(1/Fe(4,n)).toString()):(n=16,i="2.3283064365386962890625e-10"),e.precision+=n,t=de(e,1,t.times(i),new e(1));for(var o=n;o--;){var s=t.times(t);t=s.times(s).minus(s).times(8).plus(1)}return e.precision-=n,t}var k=function(){function e(r,i,o){var s,a=0,c=r.length;for(r=r.slice();c--;)s=r[c]*i+a,r[c]=s%o|0,a=s/o|0;return a&&r.unshift(a),r}function t(r,i,o,s){var a,c;if(o!=s)c=o>s?1:-1;else for(a=c=0;a<o;a++)if(r[a]!=i[a]){c=r[a]>i[a]?1:-1;break}return c}function n(r,i,o,s){for(var a=0;o--;)r[o]-=a,a=r[o]<i[o]?1:0,r[o]=a*s+r[o]-i[o];for(;!r[0]&&r.length>1;)r.shift()}return function(r,i,o,s,a,c){var u,l,d,f,p,x,b,R,S,E,N,V,Se,X,Ge,Le,pe,He,Z,Ae,Re=r.constructor,je=r.s==i.s?1:-1,W=r.d,I=i.d;if(!W||!W[0]||!I||!I[0])return new Re(!r.s||!i.s||(W?I&&W[0]==I[0]:!I)?NaN:W&&W[0]==0||!I?je*0:je/0);for(c?(p=1,l=r.e-i.e):(c=z,p=y,l=q(r.e/p)-q(i.e/p)),Z=I.length,pe=W.length,S=new Re(je),E=S.d=[],d=0;I[d]==(W[d]||0);d++);if(I[d]>(W[d]||0)&&l--,o==null?(X=o=Re.precision,s=Re.rounding):a?X=o+(r.e-i.e)+1:X=o,X<0)E.push(1),x=!0;else{if(X=X/p+2|0,d=0,Z==1){for(f=0,I=I[0],X++;(d<pe||f)&&X--;d++)Ge=f*c+(W[d]||0),E[d]=Ge/I|0,f=Ge%I|0;x=f||d<pe}else{for(f=c/(I[0]+1)|0,f>1&&(I=e(I,f,c),W=e(W,f,c),Z=I.length,pe=W.length),Le=Z,N=W.slice(0,Z),V=N.length;V<Z;)N[V++]=0;Ae=I.slice(),Ae.unshift(0),He=I[0],I[1]>=c/2&&++He;do f=0,u=t(I,N,Z,V),u<0?(Se=N[0],Z!=V&&(Se=Se*c+(N[1]||0)),f=Se/He|0,f>1?(f>=c&&(f=c-1),b=e(I,f,c),R=b.length,V=N.length,u=t(b,N,R,V),u==1&&(f--,n(b,Z<R?Ae:I,R,c))):(f==0&&(u=f=1),b=I.slice()),R=b.length,R<V&&b.unshift(0),n(N,b,V,c),u==-1&&(V=N.length,u=t(I,N,Z,V),u<1&&(f++,n(N,Z<V?Ae:I,V,c))),V=N.length):u===0&&(f++,N=[0]),E[d++]=f,u&&N[0]?N[V++]=W[Le]||0:(N=[W[Le]],V=1);while((Le++<pe||N[0]!==void 0)&&X--);x=N[0]!==void 0}E[0]||E.shift()}if(p==1)S.e=l,Rt=x;else{for(d=1,f=E[0];f>=10;f/=10)d++;S.e=d+l*p-1,h(S,a?o+S.e+1:o,s,x)}return S}}();function h(e,t,n,r){var i,o,s,a,c,u,l,d,f,p=e.constructor;e:if(t!=null){if(d=e.d,!d)return e;for(i=1,a=d[0];a>=10;a/=10)i++;if(o=t-i,o<0)o+=y,s=t,l=d[f=0],c=l/D(10,i-s-1)%10|0;else if(f=Math.ceil((o+1)/y),a=d.length,f>=a)if(r){for(;a++<=f;)d.push(0);l=c=0,i=1,o%=y,s=o-y+1}else break e;else{for(l=a=d[f],i=1;a>=10;a/=10)i++;o%=y,s=o-y+i,c=s<0?0:l/D(10,i-s-1)%10|0}if(r=r||t<0||d[f+1]!==void 0||(s<0?l:l%D(10,i-s-1)),u=n<4?(c||r)&&(n==0||n==(e.s<0?3:2)):c>5||c==5&&(n==4||r||n==6&&(o>0?s>0?l/D(10,i-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]=D(10,(y-t%y)%y),e.e=-t||0):d[0]=e.e=0,e;if(o==0?(d.length=f,a=1,f--):(d.length=f+1,a=D(10,y-o),d[f]=s>0?(l/D(10,i-s)%D(10,s)|0)*a:0),u)for(;;)if(f==0){for(o=1,s=d[0];s>=10;s/=10)o++;for(s=d[0]+=a,a=1;s>=10;s/=10)a++;o!=a&&(e.e++,d[0]==z&&(d[0]=1));break}else{if(d[f]+=a,d[f]!=z)break;d[f--]=0,a=1}for(o=d.length;d[--o]===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 J(e,t,n){if(!e.isFinite())return Mt(e);var r,i=e.e,o=U(e.d),s=o.length;return t?(n&&(r=n-s)>0?o=o.charAt(0)+"."+o.slice(1)+re(r):s>1&&(o=o.charAt(0)+"."+o.slice(1)),o=o+(e.e<0?"e":"e+")+e.e):i<0?(o="0."+re(-i-1)+o,n&&(r=n-s)>0&&(o+=re(r))):i>=s?(o+=re(i+1-s),n&&(r=n-i-1)>0&&(o=o+"."+re(r))):((r=i+1)<s&&(o=o.slice(0,r)+"."+o.slice(r)),n&&(r=n-s)>0&&(i+1===s&&(o+="."),o+=re(r))),o}function Ce(e,t){var n=e[0];for(t*=y;n>=10;n/=10)t++;return t}function Ie(e,t,n){if(t>Rn)throw T=!0,n&&(e.precision=n),Error(Bt);return h(new e(Ne),t,1,!0)}function Y(e,t,n){if(t>$e)throw Error(Bt);return h(new e(ke),t,n,!0)}function _t(e){var t=e.length-1,n=t*y+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 re(e){for(var t="";e--;)t+="0";return t}function Ct(e,t,n,r){var i,o=new e(1),s=Math.ceil(r/y+4);for(T=!1;;){if(n%2&&(o=o.times(t),Lt(o.d,s)&&(i=!0)),n=q(n/2),n===0){n=o.d.length-1,i&&o.d[n]===0&&++o.d[n];break}t=t.times(t),Lt(t.d,s)}return T=!0,o}function St(e){return e.d[e.d.length-1]&1}function Ft(e,t,n){for(var r,i=new e(t[0]),o=0;++o<t.length;)if(r=new e(t[o]),r.s)i[n](r)&&(i=r);else{i=r;break}return i}function et(e,t){var n,r,i,o,s,a,c,u=0,l=0,d=0,f=e.constructor,p=f.rounding,x=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=x):c=t,a=new f(.03125);e.e>-2;)e=e.times(a),d+=5;for(r=Math.log(D(2,d))/Math.LN10*2+5|0,c+=r,n=o=s=new f(1),f.precision=c;;){if(o=h(o.times(e),c,1),n=n.times(++l),a=s.plus(k(o,n,c,1)),U(a.d).slice(0,c)===U(s.d).slice(0,c)){for(i=d;i--;)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=o=a=new f(1),l=0,u++;else return h(s,f.precision=x,p,T=!0);else return f.precision=x,s}s=a}}function ie(e,t){var n,r,i,o,s,a,c,u,l,d,f,p=1,x=10,b=e,R=b.d,S=b.constructor,E=S.rounding,N=S.precision;if(b.s<0||!R||!R[0]||!b.e&&R[0]==1&&R.length==1)return new S(R&&!R[0]?-1/0:b.s!=1?NaN:R?0:b);if(t==null?(T=!1,l=N):l=t,S.precision=l+=x,n=U(R),r=n.charAt(0),Math.abs(o=b.e)<15e14){for(;r<7&&r!=1||r==1&&n.charAt(1)>3;)b=b.times(e),n=U(b.d),r=n.charAt(0),p++;o=b.e,r>1?(b=new S("0."+n),o++):b=new S(r+"."+n.slice(1))}else return u=Ie(S,l+2,N).times(o+""),b=ie(new S(r+"."+n.slice(1)),l-x).plus(u),S.precision=N,t==null?h(b,N,E,T=!0):b;for(d=b,c=s=b=k(b.minus(1),b.plus(1),l,1),f=h(b.times(b),l,1),i=3;;){if(s=h(s.times(f),l,1),u=c.plus(k(s,new S(i),l,1)),U(u.d).slice(0,l)===U(c.d).slice(0,l))if(c=c.times(2),o!==0&&(c=c.plus(Ie(S,l+2,N).times(o+""))),c=k(c,new S(p),l,1),t==null)if(ge(c.d,l-x,E,a))S.precision=l+=x,u=s=b=k(d.minus(1),d.plus(1),l,1),f=h(b.times(b),l,1),i=a=1;else return h(c,S.precision=N,E,T=!0);else return S.precision=N,c;c=u,i+=2}}function Mt(e){return String(e.s*e.s/0)}function tt(e,t){var n,r,i;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(i=t.length;t.charCodeAt(i-1)===48;--i);if(t=t.slice(r,i),t){if(i-=r,e.e=n=n-r-1,e.d=[],r=(n+1)%y,n<0&&(r+=y),r<i){for(r&&e.d.push(+t.slice(0,r)),i-=y;r<i;)e.d.push(+t.slice(r,r+=y));t=t.slice(r),r=y-t.length}else r-=i;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 Nn(e,t){var n,r,i,o,s,a,c,u,l;if(t.indexOf("_")>-1){if(t=t.replace(/(\d)_(?=\d)/g,"$1"),It.test(t))return tt(e,t)}else if(t==="Infinity"||t==="NaN")return+t||(e.s=NaN),e.e=NaN,e.d=null,e;if(Sn.test(t))n=16,t=t.toLowerCase();else if(Pn.test(t))n=2;else if(Ln.test(t))n=8;else throw Error(oe+t);for(o=t.search(/p/i),o>0?(c=+t.slice(o+1),t=t.substring(2,o)):t=t.slice(2),o=t.indexOf("."),s=o>=0,r=e.constructor,s&&(t=t.replace(".",""),a=t.length,o=a-o,i=Ct(r,new r(n),o,o*2)),u=Be(t,n,z),l=u.length-1,o=l;u[o]===0;--o)u.pop();return o<0?new r(e.s*0):(e.e=Ce(u,l),e.d=u,T=!1,s&&(e=k(e,i,a*4)),c&&(e=e.times(Math.abs(c)<54?D(2,c):Me.pow(2,c))),T=!0,e)}function kn(e,t){var n,r=t.d.length;if(r<3)return t.isZero()?t:de(e,2,t,t);n=1.4*Math.sqrt(r),n=n>16?16:n|0,t=t.times(1/Fe(5,n)),t=de(e,2,t,t);for(var i,o=new e(5),s=new e(16),a=new e(20);n--;)i=t.times(t),t=t.times(o.plus(i.times(s.times(i).minus(a))));return t}function de(e,t,n,r,i){var o,s,a,c,u=1,l=e.precision,d=Math.ceil(l/y);for(T=!1,c=n.times(n),a=new e(r);;){if(s=k(a.times(c),new e(t++*t++),l,1),a=i?r.plus(s):r.minus(s),r=k(s.times(c),new e(t++*t++),l,1),s=a.plus(r),s.d[d]!==void 0){for(o=d;s.d[o]===a.d[o]&&o--;);if(o==-1)break}o=a,a=r,r=s,s=o,u++}return T=!0,s.d.length=d+1,s}function Fe(e,t){for(var n=e;--t;)n*=e;return n}function vt(e,t){var n,r=t.s<0,i=Y(e,e.precision,1),o=i.times(.5);if(t=t.abs(),t.lte(o))return Q=r?4:1,t;if(n=t.divToInt(i),n.isZero())Q=r?3:2;else{if(t=t.minus(n.times(i)),t.lte(o))return Q=St(n)?r?2:3:r?4:1,t;Q=St(n)?r?1:4:r?3:2}return t.minus(i).abs()}function nt(e,t,n,r){var i,o,s,a,c,u,l,d,f,p=e.constructor,x=n!==void 0;if(x?(H(n,1,se),r===void 0?r=p.rounding:H(r,0,8)):(n=p.precision,r=p.rounding),!e.isFinite())l=Mt(e);else{for(l=J(e),s=l.indexOf("."),x?(i=2,t==16?n=n*4-3:t==8&&(n=n*3-2)):i=t,s>=0&&(l=l.replace(".",""),f=new p(1),f.e=l.length-s,f.d=Be(J(f),10,i),f.e=f.d.length),d=Be(l,10,i),o=c=d.length;d[--c]==0;)d.pop();if(!d[0])l=x?"0p+0":"0";else{if(s<0?o--:(e=new p(e),e.d=d,e.e=o,e=k(e,f,n,r,0,i),d=e.d,o=e.e,u=Rt),s=d[n],a=i/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]>i-1;)d[n]=0,n||(++o,d.unshift(1));for(c=d.length;!d[c-1];--c);for(s=0,l="";s<c;s++)l+=Xe.charAt(d[s]);if(x){if(c>1)if(t==16||t==8){for(s=t==16?4:3,--c;c%s;c++)l+="0";for(d=Be(l,i,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+(o<0?"p":"p+")+o}else if(o<0){for(;++o;)l="0"+l;l="0."+l}else if(++o>c)for(o-=c;o--;)l+="0";else o<c&&(l=l.slice(0,o)+"."+l.slice(o))}l=(t==16?"0x":t==2?"0b":t==8?"0o":"")+l}return e.s<0?"-"+l:l}function Lt(e,t){if(e.length>t)return e.length=t,!0}function In(e){return new this(e).abs()}function _n(e){return new this(e).acos()}function Cn(e){return new this(e).acosh()}function Fn(e,t){return new this(e).plus(t)}function Mn(e){return new this(e).asin()}function vn(e){return new this(e).asinh()}function En(e){return new this(e).atan()}function Dn(e){return new this(e).atanh()}function Vn(e,t){e=new this(e),t=new this(t);var n,r=this.precision,i=this.rounding,o=r+4;return!e.s||!t.s?n=new this(NaN):!e.d&&!t.d?(n=Y(this,o,1).times(t.s>0?.25:.75),n.s=e.s):!t.d||e.isZero()?(n=t.s<0?Y(this,r,i):new this(0),n.s=e.s):!e.d||t.isZero()?(n=Y(this,o,1).times(.5),n.s=e.s):t.s<0?(this.precision=o,this.rounding=1,n=this.atan(k(e,t,o,1)),t=Y(this,o,1),this.precision=r,this.rounding=i,n=e.s<0?n.minus(t):n.plus(t)):n=this.atan(k(e,t,o,1)),n}function On(e){return new this(e).cbrt()}function Kn(e){return h(e=new this(e),e.e+1,2)}function Un(e,t,n){return new this(e).clamp(t,n)}function Wn(e){if(!e||typeof e!="object")throw Error(_e+"Object expected");var t,n,r,i=e.defaults===!0,o=["precision",1,se,"rounding",0,8,"toExpNeg",-le,0,"toExpPos",0,le,"maxE",0,le,"minE",-le,0,"modulo",0,9];for(t=0;t<o.length;t+=3)if(n=o[t],i&&(this[n]=Qe[n]),(r=e[n])!==void 0)if(q(r)===r&&r>=o[t+1]&&r<=o[t+2])this[n]=r;else throw Error(oe+n+": "+r);if(n="crypto",i&&(this[n]=Qe[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(Nt);else this[n]=!1;else throw Error(oe+n+": "+r);return this}function qn(e){return new this(e).cos()}function Gn(e){return new this(e).cosh()}function Et(e){var t,n,r;function i(o){var s,a,c,u=this;if(!(u instanceof i))return new i(o);if(u.constructor=i,At(o)){u.s=o.s,T?!o.d||o.e>i.maxE?(u.e=NaN,u.d=null):o.e<i.minE?(u.e=0,u.d=[0]):(u.e=o.e,u.d=o.d.slice()):(u.e=o.e,u.d=o.d?o.d.slice():o.d);return}if(c=typeof o,c==="number"){if(o===0){u.s=1/o<0?-1:1,u.e=0,u.d=[0];return}if(o<0?(o=-o,u.s=-1):u.s=1,o===~~o&&o<1e7){for(s=0,a=o;a>=10;a/=10)s++;T?s>i.maxE?(u.e=NaN,u.d=null):s<i.minE?(u.e=0,u.d=[0]):(u.e=s,u.d=[o]):(u.e=s,u.d=[o]);return}else if(o*0!==0){o||(u.s=NaN),u.e=NaN,u.d=null;return}return tt(u,o.toString())}else if(c!=="string")throw Error(oe+o);return(a=o.charCodeAt(0))===45?(o=o.slice(1),u.s=-1):(a===43&&(o=o.slice(1)),u.s=1),It.test(o)?tt(u,o):Nn(u,o)}if(i.prototype=m,i.ROUND_UP=0,i.ROUND_DOWN=1,i.ROUND_CEIL=2,i.ROUND_FLOOR=3,i.ROUND_HALF_UP=4,i.ROUND_HALF_DOWN=5,i.ROUND_HALF_EVEN=6,i.ROUND_HALF_CEIL=7,i.ROUND_HALF_FLOOR=8,i.EUCLID=9,i.config=i.set=Wn,i.clone=Et,i.isDecimal=At,i.abs=In,i.acos=_n,i.acosh=Cn,i.add=Fn,i.asin=Mn,i.asinh=vn,i.atan=En,i.atanh=Dn,i.atan2=Vn,i.cbrt=On,i.ceil=Kn,i.clamp=Un,i.cos=qn,i.cosh=Gn,i.div=Hn,i.exp=jn,i.floor=Zn,i.hypot=Yn,i.ln=zn,i.log=Jn,i.log10=Qn,i.log2=Xn,i.max=$n,i.min=er,i.mod=tr,i.mul=nr,i.pow=rr,i.random=ir,i.round=or,i.sign=sr,i.sin=ar,i.sinh=ur,i.sqrt=cr,i.sub=lr,i.sum=dr,i.tan=fr,i.tanh=mr,i.trunc=pr,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 i.config(e),i}function Hn(e,t){return new this(e).div(t)}function jn(e){return new this(e).exp()}function Zn(e){return h(e=new this(e),e.e+1,3)}function Yn(){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 At(e){return e instanceof Me||e&&e.toStringTag===kt||!1}function zn(e){return new this(e).ln()}function Jn(e,t){return new this(e).log(t)}function Xn(e){return new this(e).log(2)}function Qn(e){return new this(e).log(10)}function $n(){return Ft(this,arguments,"lt")}function er(){return Ft(this,arguments,"gt")}function tr(e,t){return new this(e).mod(t)}function nr(e,t){return new this(e).mul(t)}function rr(e,t){return new this(e).pow(t)}function ir(e){var t,n,r,i,o=0,s=new this(1),a=[];if(e===void 0?e=this.precision:H(e,1,se),r=Math.ceil(e/y),this.crypto)if(crypto.getRandomValues)for(t=crypto.getRandomValues(new Uint32Array(r));o<r;)i=t[o],i>=429e7?t[o]=crypto.getRandomValues(new Uint32Array(1))[0]:a[o++]=i%1e7;else if(crypto.randomBytes){for(t=crypto.randomBytes(r*=4);o<r;)i=t[o]+(t[o+1]<<8)+(t[o+2]<<16)+((t[o+3]&127)<<24),i>=214e7?crypto.randomBytes(4).copy(t,o):(a.push(i%1e7),o+=4);o=r/4}else throw Error(Nt);else for(;o<r;)a[o++]=Math.random()*1e7|0;for(r=a[--o],e%=y,r&&e&&(i=D(10,y-e),a[o]=(r/i|0)*i);a[o]===0;o--)a.pop();if(o<0)n=0,a=[0];else{for(n=-1;a[0]===0;n-=y)a.shift();for(r=1,i=a[0];i>=10;i/=10)r++;r<y&&(n-=y-r)}return s.e=n,s.d=a,s}function or(e){return h(e=new this(e),e.e+1,this.rounding)}function sr(e){return e=new this(e),e.d?e.d[0]?e.s:0*e.s:e.s||NaN}function ar(e){return new this(e).sin()}function ur(e){return new this(e).sinh()}function cr(e){return new this(e).sqrt()}function lr(e,t){return new this(e).sub(t)}function dr(){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 fr(e){return new this(e).tan()}function mr(e){return new this(e).tanh()}function pr(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 Me=m.constructor=Et(Qe);Ne=new Me(Ne);ke=new Me(ke);import Pr from"big.js";import De from"bn.js";import gr from"toformat";var hr=gr,he=hr;import Ee from"big.js";import yr from"bn.js";import br from"decimal.js-light";import we from"bn.js";var Dt=9007199254740991;function M(e){let t=K("Raydium_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>=Dt||e<=-Dt)&&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 ve=K("module/fraction"),rt=he(Ee),ye=he(br),Tr={[0]:ye.ROUND_DOWN,[1]:ye.ROUND_HALF_UP,[2]:ye.ROUND_UP},xr={[0]:Ee.roundDown,[1]:Ee.roundHalfUp,[2]:Ee.roundUp},B=class{constructor(t,n=new yr(1)){this.numerator=M(t),this.denominator=M(n)}get quotient(){return this.numerator.div(this.denominator)}invert(){return new B(this.denominator,this.numerator)}add(t){let n=t instanceof B?t:new B(M(t));return this.denominator.eq(n.denominator)?new B(this.numerator.add(n.numerator),this.denominator):new B(this.numerator.mul(n.denominator).add(n.numerator.mul(this.denominator)),this.denominator.mul(n.denominator))}sub(t){let n=t instanceof B?t:new B(M(t));return this.denominator.eq(n.denominator)?new B(this.numerator.sub(n.numerator),this.denominator):new B(this.numerator.mul(n.denominator).sub(n.numerator.mul(this.denominator)),this.denominator.mul(n.denominator))}mul(t){let n=t instanceof B?t:new B(M(t));return new B(this.numerator.mul(n.numerator),this.denominator.mul(n.denominator))}div(t){let n=t instanceof B?t:new B(M(t));return new B(this.numerator.mul(n.denominator),this.denominator.mul(n.numerator))}toSignificant(t,n={groupSeparator:""},r=1){Number.isInteger(t)||ve.logWithError(`${t} is not an integer.`),t<=0&&ve.logWithError(`${t} is not positive.`),ye.set({precision:t+1,rounding:Tr[r]});let i=new ye(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(t);return i.toFormat(i.decimalPlaces(),n)}toFixed(t,n={groupSeparator:""},r=1){return Number.isInteger(t)||ve.logWithError(`${t} is not an integer.`),t<0&&ve.logWithError(`${t} is negative.`),rt.DP=t,rt.RM=xr[r]||1,new rt(this.numerator.toString()).div(this.denominator.toString()).toFormat(t,n)}isZero(){return this.numerator.isZero()}};var Sr=K("Raydium_amount"),Vt=he(Pr);function Lr(e,t){let n="0",r="0";if(e.includes(".")){let i=e.split(".");i.length===2?([n,r]=i,r=r.padEnd(t,"0")):Sr.logWithError(`invalid number string, num: ${e}`)}else n=e;return[n,r.slice(0,t)||r]}var $=class extends B{constructor(n,r,i=!0,o){let s=new De(0),a=it.pow(new De(n.decimals));if(i)s=M(r);else{let c=new De(0),u=new De(0);if(typeof r=="string"||typeof r=="number"||typeof r=="bigint"){let[l,d]=Lr(r.toString(),n.decimals);c=M(l),u=M(d)}c=c.mul(a),s=c.add(u)}super(s,a);this.logger=K(o||"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 $(this.token,this.raw.add(n.raw))}subtract(n){return this.token.equals(n.token)||this.logger.logWithError("sub token not equals"),new $(this.token,this.raw.sub(n.raw))}toSignificant(n=this.token.decimals,r,i=0){return super.toSignificant(n,r,i)}toFixed(n=this.token.decimals,r,i=0){return n>this.token.decimals&&this.logger.logWithError("decimals overflow"),super.toFixed(n,r,i)}toExact(n={groupSeparator:""}){return Vt.DP=this.token.decimals,new Vt(this.numerator.toString()).div(this.denominator.toString()).toFormat(n)}};import{PublicKey as Ar}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Ot}from"@solana/spl-token";var Kt={chainId:101,address:Ar.default.toBase58(),programId:Ot.toBase58(),decimals:9,symbol:"SOL",name:"solana",logoURI:"https://img-v1.raydium.io/icon/So11111111111111111111111111111111111111112.png",tags:[],priority:2,type:"raydium",extensions:{coingeckoId:"solana"}},ae={chainId:101,address:"So11111111111111111111111111111111111111112",programId:Ot.toBase58(),decimals:9,symbol:"WSOL",name:"Wrapped SOL",logoURI:"https://img-v1.raydium.io/icon/So11111111111111111111111111111111111111112.png",tags:[],priority:2,type:"raydium",extensions:{coingeckoId:"solana"}};import{PublicKey as at}from"@solana/web3.js";import{PublicKey as _,SystemProgram as Ut,SYSVAR_RENT_PUBKEY as Rr}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Br}from"@solana/spl-token";function ot({pubkey:e,isSigner:t=!1,isWritable:n=!0}){return{pubkey:e,isWritable:n,isSigner:t}}var Xi=[ot({pubkey:Br,isWritable:!1}),ot({pubkey:Ut.programId,isWritable:!1}),ot({pubkey:Rr,isWritable:!1})];function Wt({publicKey:e,transformSol:t}){let n=st(e.toString());if(n instanceof _)return t&&n.equals(be)?Je:n;if(t&&n.toString()===be.toBase58())return Je;if(typeof n=="string"){if(n===_.default.toBase58())return _.default;try{return new _(n)}catch{throw new Error("invalid public key")}}throw new Error("invalid public key")}function st(e){try{return new _(e)}catch{return e}}var Qi=new _("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),$i=new _("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),eo=new _("SysvarRent111111111111111111111111111111111"),to=new _("SysvarC1ock11111111111111111111111111111111"),no=new _("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),ro=new _("Sysvar1nstructions1111111111111111111111111"),io=Ut.programId,oo=new _("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),so=new _("Ea5SjE2Y6yvCeW5dYTn7PYMuW5ikXkvbGdcmSnXeaLjS"),ao=new _("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"),uo=new _("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),co=new _("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),lo=new _("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),fo=new _("7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj"),mo=new _("USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX"),po=new _("NRVwhjBQiUPYtfDT5zRBVJajzFQHaBUNtC7SNVvqRFa"),go=new _("ANAxByE6G2WjFp7A4NqtWYXb3mgruyzZYg3spfxe6Lbo"),ho=new _("7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs"),Je=new _("So11111111111111111111111111111111111111112"),be=_.default;var ut=class{constructor({mint:t,decimals:n,symbol:r,name:i,skipMint:o=!1,isToken2022:s=!1}){if(t===be.toBase58()||t instanceof at&&be.equals(t)){this.decimals=ae.decimals,this.symbol=ae.symbol,this.name=ae.name,this.mint=new at(ae.address),this.isToken2022=!1;return}this.decimals=n,this.symbol=r||t.toString().substring(0,6),this.name=i||t.toString().substring(0,6),this.mint=o?at.default:Wt({publicKey:t}),this.isToken2022=s}equals(t){return this===t?!0:this.mint.equals(t.mint)}},ue=ut;ue.WSOL=new ut(G(O({},ae),{mint:ae.address}));var ct=class{constructor({decimals:t,symbol:n="UNKNOWN",name:r="UNKNOWN"}){this.decimals=t,this.symbol=n,this.name=r}equals(t){return this===t}},Ve=ct;Ve.SOL=new ct(Kt);import Nr from"bn.js";var qt=new B(new Nr(100)),fe=class extends B{toSignificant(t=5,n,r){return this.mul(qt).toSignificant(t,n,r)}toFixed(t=2,n,r){return this.mul(qt).toFixed(t,n,r)}};var kr=K("Raydium_price"),ee=class extends B{constructor(n){let{baseToken:r,quoteToken:i,numerator:o,denominator:s}=n;super(o,s);this.baseToken=r,this.quoteToken=i,this.scalar=new B(lt(r.decimals),lt(i.decimals))}get raw(){return new B(this.numerator,this.denominator)}get adjusted(){return super.mul(this.scalar)}invert(){return new ee({baseToken:this.quoteToken,quoteToken:this.baseToken,denominator:this.numerator,numerator:this.denominator})}mul(n){this.quoteToken!==n.baseToken&&kr.logWithError("mul token not equals");let r=super.mul(n);return new ee({baseToken:this.baseToken,quoteToken:n.quoteToken,denominator:r.denominator,numerator:r.numerator})}toSignificant(n=this.quoteToken.decimals,r,i){return this.adjusted.toSignificant(n,r,i)}toFixed(n=this.quoteToken.decimals,r,i){return this.adjusted.toFixed(n,r,i)}};var Ir=new te(0),ss=new te(1),as=new te(2),us=new te(3),cs=new te(5),it=new te(10),ls=new te(100),ds=new te(1e3),fs=new te(1e4);function lt(e){return it.pow(M(e))}var Gt=e=>typeof e=="number";function Ht(e,t,n){let r=Gt(t)?t*((n==null?void 0:n.unit)==="s"?1e3:1):t;return new Date(e).getTime()<=r}function jt(e,t,n){let r=Gt(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 Es}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Vs}from"@solana/spl-token";import{ComputeBudgetProgram as Ts,Keypair as Ss,PublicKey as Cr,Transaction as As,TransactionMessage as Bs,VersionedTransaction as Ns}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as Cs}from"@solana/spl-token";var Fs=K("Raydium_txUtil");function Te(e,t){let[n,r]=Cr.findProgramAddressSync(e,t);return{publicKey:n,nonce:r}}import{PublicKey as w}from"@solana/web3.js";var Zt=new w("EhhTKczWMGQt46ynNeRX1WfeagwwJd7ufHvCDjRxjo5Q"),Yt=new w("CBuCnLe26faBpcBP2fktp4rp8abpcAnTWft6ZrP5Q4T"),zt=new w("9KEPoZmtHUrBbhWN1v1KWLMkkvwY6WLtAVUCPRtRjP4z"),Jt=new w("FarmqiPv5eAj3j1GMdMCMUGXqPUvmquZtMy86QH6rzhG"),Ws=new w("CLaimxFqjHzgTJtAGHU47NPhg6qrc5sCnpC4tBLyABQS"),qs=new w("srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX"),Gs=new w("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin"),Hs=new w("675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8"),js=new w("5quBtoiQqxF9Jv6KYKctB59NT3gtJD2Y65kdnB1Uev3h"),Zs=new w("CDSr3ssLcRB6XYPJwAfFt18MZvEZp4LjHcvzBVZ45duo"),Ys=new w("CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK"),zs=new w("LockrWmn6K5twhz3y9w1dQERbmgSaRkfnTeTKbpofwE"),Js=new w("kN1kEznaF5Xbd8LYuqtEFcxzWSBk5Fv6ygX6SqEGJVy"),Xs=new w("CDSr3ssLcRB6XYPJwAfFt18MZvEZp4LjHcvzBVZ45duo"),Qs=new w("routeUGWgWzqBWFcrCfv8tritsqukccJPu3q5GPP3xS"),$s=new w("7YttLkHDoNj9wyDur5pM1ejNaAvT9X4eqaYcHQqtj2G5"),ea=new w("6FJon3QE27qgPVggARueB22hLvoh22VzJpXv4rBEoSLF"),ta=new w("CC12se5To1CdEuw7fDS27B7Geo5jJyL7t5UK2B44NgiH"),na=new w("9HzJyW1qZsEiSfMUf6L2jo3CcTKAyBmSyKdwQeYisHrC"),ra=new w("DropEU8AvevN3UrXWXTMuz3rqnMczQVNjq3kcSdW2SQi"),ia=new w("CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C"),oa=new w("GpMZbSM2GgvTKHJirzeGfMFoaZ8UR2X7F4v8vHTvxFbL"),sa=new w("DNXgeM9EiiaAbaWvwjHj9fQQLAX5ZsfHyvmYUNRAdNC8"),aa=new w("LockrWmn6K5twhz3y9w1dQERbmgSaRkfnTeTKbpofwE"),ua=new w("3f7GcQFG397GAaEnv51zR6tsTVihYRydnydDD1cXekxH"),ca=new w("LanMV9sAd7wArD4vJFi2qDdfnVhFxYSUg6eADduJ3uj"),la=new w("WLHv2UAZm6z4KyaaELi5pjdbJh6RESMva1Rnn8pJVVh"),da=new w("DRay6fNdQ5J82H7xV6uq2aV3mNrUZ1J4PgSKsWgptcm6"),fa=new w("5xqNaZXX5eUi4p5HU4oz9i5QnwRNT2y6oN7yyn4qENeq"),ma=new w("4Bu96XjU84XjPDSpveTVf6LYGCkfW5FK7SNkREWcEfV4"),pa=new w("6s1xP3hpbAfFoNtUNF8mfHsjr2Bd97JxFJRWLbL6aHuX");var xe={OPEN_BOOK_PROGRAM:new w("EoTcMgcDRTJVZDMZWBoU6rhYHZfkNTVEAfz3uUJRcYGj"),SERUM_PROGRAM_ID_V3:new w("Ray1111111111111111111111111111111111111111"),AMM_V4:new w("DRaya7Kj3aMWQSy19kSjvmuwq9docCHofyP9kanQGaav"),AMM_STABLE:new w("DRayDdXc1NZQ9C3hRWmoSf8zK4iapgMnjdNZWrfwsP8m"),CLMM_PROGRAM_ID:new w("DRayAUgENGQBKVaX8owNhgzkEDyoHTGVEGHVJT1E9pfH"),CLMM_LOCK_PROGRAM_ID:new w("DRay25Usp3YJAi7beckgpGUC7mGJ2cR1AVPxhYfwVCUX"),CLMM_LOCK_AUTH_ID:new w("6Aoh8h2Lw2m5UGxYR8AdAL87jTWYeKoxM52mJRzfYwN"),CREATE_CPMM_POOL_PROGRAM:new w("DRaycpLY18LhpbydsBWbVJtxpNv9oXPgjRSfpF2bWpYb"),CREATE_CPMM_POOL_AUTH:new w("CXniRufdq5xL8t8jZAPxsPZDpuudwuJSPWnbcD5Y5Nxq"),CREATE_CPMM_POOL_FEE_ACC:new w("3oE58BKVt8KuYkGxx8zBojugnymWmBiyafWgMrnb6eYy"),LOCK_CPMM_PROGRAM:new w("DRay25Usp3YJAi7beckgpGUC7mGJ2cR1AVPxhYfwVCUX"),LOCK_CPMM_AUTH:new w("7qWVV8UY2bRJfDLP4s37YzBPKUkVB46DStYJBpYbQzu3"),UTIL1216:w.default,Router:new w("DRaybByLpbUL57LJARs3j8BitTxVfzBg351EaMr5UTCd"),FARM_PROGRAM_ID_V3:new w("DRayWyrLmEW5KEeqs8kdTMMaBabapqagaBC7KWpGtJeZ"),FARM_PROGRAM_ID_V4:new w("Ray1111111111111111111111111111111111111111"),FARM_PROGRAM_ID_V5:new w("DRayiCGSZgku1GTK6rXD6mVDdingXy6APAH1R6R5L2LC"),FARM_PROGRAM_ID_V6:new w("DRayzbYakXs45ELHkzH6vC3fuhQqTAnv5A68gdFuvZyZ"),LAUNCHPAD_PROGRAM:new w("DRay6fNdQ5J82H7xV6uq2aV3mNrUZ1J4PgSKsWgptcm6"),LAUNCHPAD_AUTH:new w("5xqNaZXX5eUi4p5HU4oz9i5QnwRNT2y6oN7yyn4qENeq"),LAUNCHPAD_PLATFORM:new w("2Jx4KTDrVSdWNazuGpcA8n3ZLTRGGBDxAWhuKe2Xcj2a"),LAUNCHPAD_CONFIG:new w("7ZR4zD7PYfY2XxoG1Gxcy2EgEeGYrpxrwzPuwdUBssEt"),FEE_DESTINATION_ID:new w("9y8ENuuZ3b19quffx9hQvRVygG5ky6snHfRvGpuSfeJy"),MODEL_DATA_PUBKEY:new w("Ray1111111111111111111111111111111111111111")};import wa from"bn.js";import{PublicKey as xa,AddressLookupTableAccount as Pa}from"@solana/web3.js";import{PublicKey as Oa,sendAndConfirmTransaction as Ka,SystemProgram as qa,Transaction as Ga,TransactionMessage as ja,VersionedTransaction as Za}from"@solana/web3.js";import za from"axios";import{PublicKey as Fr}from"@solana/web3.js";import Mr from"bn.js";function vr(e){return typeof e=="object"&&e!==null&&![ue,$,Fr,B,Mr,ee,fe].some(t=>typeof t=="object"&&e instanceof t)}function Oe(e){return typeof e=="string"?st(e):Array.isArray(e)?e.map(t=>Oe(t)):vr(e)?Object.fromEntries(Object.entries(e).map(([t,n])=>[t,Oe(n)])):e}import Er from"bn.js";var au=new Er(1e6);import{PublicKey as qr}from"@solana/web3.js";import en,{isBN as tn}from"bn.js";import{bits as ku,BitStructure as Iu,blob as Dr,Blob as _u,cstr as Cu,f32 as Fu,f32be as Mu,f64 as vu,f64be as Eu,greedy as Du,Layout as Vr,ns64 as Vu,ns64be as Ou,nu64 as Ku,nu64be as Uu,offset as Wu,s16 as qu,s16be as Gu,s24 as Hu,s24be as ju,s32 as Zu,s32be as Yu,s40 as zu,s40be as Ju,s48 as Xu,s48be as Qu,s8 as $u,seq as Or,struct as ec,Structure as Kr,u16 as tc,u16be as nc,u24 as rc,u24be as ic,u32 as oc,u32be as sc,u40 as ac,u40be as uc,u48 as cc,u48be as lc,u8 as Ur,UInt as Wr,union as dc,Union as fc,unionLayoutDiscriminator as mc,utf8 as pc}from"@solana/buffer-layout";var dt=Vr,Xt=Kr;var ft=Wr;var Qt=Ur;var $t=Or;var ce=Dr;var me=class extends dt{constructor(n,r,i){super(n,i);this.blob=ce(n),this.signed=r}decode(n,r=0){let i=new en(this.blob.decode(n,r),10,"le");return this.signed?i.fromTwos(this.span*8).clone():i}encode(n,r,i=0){return typeof n=="number"&&(n=new en(n)),this.signed&&(n=n.toTwos(this.span*8)),this.blob.encode(n.toArrayLike(Buffer,"le",this.span),r,i)}};function v(e){return new ft(1,e)}function Ue(e){return new ft(4,e)}function g(e){return new me(8,!1,e)}function ne(e){return new me(16,!1,e)}function nn(e){return new me(1,!0,e)}function We(e){return new me(8,!0,e)}var Ke=class extends dt{constructor(n,r,i,o){super(n.span,o);this.layout=n,this.decoder=r,this.encoder=i}decode(n,r){return this.decoder(this.layout.decode(n,r))}encode(n,r,i){return this.layout.encode(this.encoder(n),r,i)}getSpan(n,r){return this.layout.getSpan(n,r)}};function P(e){return new Ke(ce(32),t=>new qr(t),t=>t.toBuffer(),e)}function pt(e){return new Ke(Qt(),Gr,Hr,e)}function Gr(e){if(e===0)return!1;if(e===1)return!0;throw new Error("Invalid bool: "+e)}function Hr(e){return e?1:0}var mt=class extends Xt{decode(t,n){return super.decode(t,n)}};function C(e,t,n){return new mt(e,t,n)}function F(e,t,n){let r,i=typeof t=="number"?t:tn(t)?t.toNumber():new Proxy(t,{get(o,s){if(!r){let a=Reflect.get(o,"count");r=tn(a)?a.toNumber():a,Reflect.set(o,"count",r)}return Reflect.get(o,s)},set(o,s,a){return s==="count"&&(r=a),Reflect.set(o,s,a)}});return $t(e,i,n)}var gt=C([P("mint"),P("owner"),g("amount"),Ue("delegateOption"),P("delegate"),v("state"),Ue("isNativeOption"),g("isNative"),g("delegatedAmount"),Ue("closeAuthorityOption"),P("closeAuthority")]);import{PublicKey as ht}from"@solana/web3.js";var Mc=C([v("instruction")]),vc=C([v("instruction")]),jr=C([g("rewardState"),g("rewardOpenTime"),g("rewardEndTime"),g("rewardLastUpdateTime"),g("totalReward"),g("totalRewardEmissioned"),g("rewardClaimed"),g("rewardPerSecond"),ne("accRewardPerShare"),P("rewardVault"),P("rewardMint"),P("rewardSender"),g("rewardType"),F(g(),15,"padding")]),Zr=C([g("state"),g("nonce"),P("lpVault"),P("rewardVault"),P(),P(),g(),g(),g("totalReward"),ne("perShareReward"),g("lastSlot"),g("perSlotReward")]),Yr=C([g("state"),g("nonce"),P("lpVault"),P("rewardVaultA"),g("totalRewardA"),ne("perShareRewardA"),g("perSlotRewardA"),v("option"),P("rewardVaultB"),ce(7),g("totalRewardB"),ne("perShareRewardB"),g("perSlotRewardB"),g("lastSlot"),P()]),zr=C([g(),g("state"),g("nonce"),g("validRewardTokenNum"),ne("rewardMultiplier"),g("rewardPeriodMax"),g("rewardPeriodMin"),g("rewardPeriodExtend"),P("lpMint"),P("lpVault"),F(jr,5,"rewardInfos"),P("creator"),P(),F(g(),32,"padding")]),rn=new Proxy(Zr,{get(e,t,n){return t==="decode"?(...r)=>{let i=e.decode(...r);return G(O({},i),{version:3,rewardInfos:[{rewardVault:i.rewardVault,totalReward:i.totalReward,perSlotReward:i.perSlotReward,perShareReward:i.perShareReward}]})}:Reflect.get(e,t,n)}}),on=new Proxy(Yr,{get(e,t,n){return t==="decode"?(...r)=>{let i=e.decode(...r);return G(O({},i),{version:5,rewardInfos:[{rewardVault:i.rewardVaultA,totalReward:i.totalRewardA,perSlotReward:i.perSlotRewardA,perShareReward:i.perShareRewardA},{rewardVault:i.rewardVaultB,totalReward:i.totalRewardB,perSlotReward:i.perSlotRewardB,perShareReward:i.perShareRewardB}]})}:Reflect.get(e,t,n)}}),sn=new Proxy(zr,{get(e,t,n){return t==="decode"?(...r)=>{let i=e.decode(...r);return G(O({},i),{version:6,rewardInfos:i.rewardInfos.map(o=>{var s;return G(O({},o),{rewardType:((s=Object.entries(qe).find(a=>String(a[1])===o.rewardType.toString()))!=null?s:["Standard SPL"])[0]})})})}:Reflect.get(e,t,n)}}),Jr=C([g("isSet"),g("rewardPerSecond"),g("rewardOpenTime"),g("rewardEndTime"),g("rewardType")]),Ec=C([v("instruction"),g("nonce"),F(Jr,5,"rewardTimeInfo")]),Dc=C([v("instruction"),g("rewardReopenTime"),g("rewardEndTime"),g("rewardPerSecond")]),Vc=C([v("instruction"),g("isSet"),g("rewardPerSecond"),g("rewardOpenTime"),g("rewardEndTime"),g("rewardType")]),Oc=C([g("state"),P("id"),P("owner"),g("deposited"),F(g(),1,"rewardDebts")]),an=C([g("state"),P("id"),P("owner"),g("deposited"),F(ne(),1,"rewardDebts"),g(""),g("voteLockedBalance"),F(g(),15)]),Kc=C([g("state"),P("id"),P("owner"),g("deposited"),F(g(),2,"rewardDebts")]),un=C([g("state"),P("id"),P("owner"),g("deposited"),F(ne(),2,"rewardDebts"),F(g(),17)]),cn=C([g(),g("state"),P("id"),P("owner"),g("deposited"),F(ne(),5,"rewardDebts"),F(g(),16)]),Uc=C([v("instruction"),g("amount")]),Xr=C([P("mint"),P("grantAuthority"),g("baselineVoteWeightScaledFactor"),g("maxExtraLockupVoteWeightScaledFactor"),g("lockupSaturationSecs"),nn("digitShift"),F(v(),7,"reserved1"),F(g(),7,"reserved2")]),ln=C([ce(8),P("governanceProgramId"),P("realm"),P("realmGoverningTokenMint"),P("realmAuthority"),F(v(),32,"reserved1"),F(Xr,4,"votingMints"),We("timeOffset"),v("bump"),F(v(),7,"reserved2"),F(g(),11,"reserved3")]),Qr=C([We("startTime"),We("endTime"),v("kind"),F(v(),15,"reserved")]),$r=C([F(Qr,1,"lockup"),g("amountDeposited_native"),g("amountInitiallyLockedNative"),pt("isUsed"),pt("allowClawback"),v("votingMintConfigIdx"),F(v(),29,"reserved")]),dn=C([ce(8),P("voterAuthority"),P("registrar"),F($r,32,"deposits"),v("voterBump"),v("voterWweightRecordBump"),F(v(),94,"reserved")]);import{NATIVE_MINT as Jc}from"@solana/spl-token";var Xc=K("Raydium_farm_config"),Qc=new ht("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),$c=new ht("FrspKwj8i3pNmKwXreTveC4fu7KL5ZbGeXdZBe2XViu1");var el=new ht("3TRTX4dXUpp2eqxi3tvQDFYUV7SdDJjcPE3Y4mbtftaX"),fn={3:rn,5:on,6:sn},mn={3:an,5:un,6:cn};var qe={"Standard SPL":0,"Option tokens":1},tl={[Zt.toString()]:3,[Yt.toString()]:4,[zt.toString()]:5,[Jt.toString()]:6,[xe.FARM_PROGRAM_ID_V3.toString()]:3,[xe.FARM_PROGRAM_ID_V4.toString()]:4,[xe.FARM_PROGRAM_ID_V5.toString()]:5,[xe.FARM_PROGRAM_ID_V6.toString()]:6};var Pe=K("Raydium.farm.util");function hl({programId:e,poolId:t,mint:n,type:r}){let{publicKey:i}=Te([t.toBuffer(),n.toBuffer(),Buffer.from(r==="lpVault"?"lp_vault_associated_seed":r==="rewardVault"?"reward_vault_associated_seed":"","utf-8")],e);return i}function ei({programId:e,poolId:t,owner:n,version:r}){let{publicKey:i}=Te([t.toBuffer(),n.toBuffer(),Buffer.from(r===6?"farmer_info_associated_seed":"staker_info_v2_associated_seed","utf-8")],e);return i}var wl=({programId:e,poolId:t})=>Te([t.toBuffer()],e);function yl(e){return{isSet:new j(1),rewardPerSecond:M(e.perSecond),rewardOpenTime:M(e.openTime),rewardEndTime:M(e.endTime),rewardType:M(qe[e.rewardType])}}function bl(e){return M(e.endTime).sub(M(e.openTime)).mul(M(e.perSecond))}function ti(e){let t=mn[e];return t||Pe.logWithError("invalid version",e),t}function ni(e){let t=fn[e];return t||Pe.logWithError("invalid version",e),t}function ri(e,t,n,r){if(e.version===3||e.version===5){if(e.lastSlot.gte(new j(n)))return e;let i=new j(n).sub(e.lastSlot);e.lastSlot=new j(n);for(let o of e.rewardInfos){if(t.amount.eq(new j(0)))continue;let s=o.perSlotReward.mul(i);o.perShareReward=o.perShareReward.add(s.mul(new j(10).pow(new j(e.version===3?9:15))).div(t.amount)),o.totalReward=o.totalReward.add(s)}}else if(e.version===6)for(let i of e.rewardInfos){if(i.rewardState.eq(new j(0)))continue;let o=j.min(new j(r),i.rewardEndTime);if(i.rewardOpenTime.gte(o))continue;let a=o.sub(i.rewardLastUpdateTime).mul(i.rewardPerSecond),c=i.totalReward.sub(i.totalRewardEmissioned);c.lt(a)?(a=c,i.rewardLastUpdateTime=i.rewardLastUpdateTime.add(c.div(i.rewardPerSecond))):i.rewardLastUpdateTime=o,!t.amount.eq(new j(0))&&(i.accRewardPerShare=i.accRewardPerShare.add(a.mul(e.rewardMultiplier).div(t.amount)),i.totalRewardEmissioned=i.totalRewardEmissioned.add(a))}return e}async function Tl({connection:e,farmPools:t,owner:n,config:r,chainTime:i}){let o=!1,s=!1,a=new j(10),c=[];for(let f of t){let p=Oe(f);p.version===6?s=!0:o=!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:ei({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:x,poolId:b,accountInfo:R}of l){let S=b.toBase58();if(u[S]=O({},u[S]),x==="state"){let E=ni(p);(!R||!R.data||R.data.length!==E.span)&&Pe.logWithError(`invalid farm state account info, pools.id, ${f}`),u[S].state=E.decode(R.data)}else if(x==="lpVault")(!R||!R.data||R.data.length!==gt.span)&&Pe.logWithError(`invalid farm lp vault account info, pools.lpVault, ${f}`),u[S].lpVault=gt.decode(R.data);else if(x==="ledger"){let E=ti(p);R&&R.data&&(R.data.length!==E.span&&Pe.logWithError(`invalid farm ledger account info, ledger, ${f}`),u[S].ledger=E.decode(R.data))}}let d=s||o?await e.getSlot():0;for(let f of Object.keys(u))u[f]!==void 0&&(u[f].state=ri(u[f].state,u[f].lpVault,d,i));for(let[f,{state:p,ledger:x}]of Object.entries(u))if(x){let b=p.version===6?p.rewardMultiplier:p.rewardInfos.length===1?a.pow(new j(9)):a.pow(new j(15)),R=p.rewardInfos.map((S,E)=>{let N=x.rewardDebts[E];return x.deposited.mul(p.version===6?S.accRewardPerShare:S.perShareReward).div(b).sub(N)});u[f].wrapped=G(O({},u[f].wrapped),{pendingRewards:R})}return u}function xl(e,t=Date.now()){if(e.version===6){let n=e.state.rewardInfos;if(n.every(({rewardOpenTime:r})=>Ht(t,r.toNumber(),{unit:"s"})))return"upcoming pool";if(n.every(({rewardEndTime:r})=>jt(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 Pl(e,t,n,r){let i=await e.getAccountInfo(t);if(i===null)throw Error("registrar info check error");let s=ln.decode(i.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=dn.decode(a.data).deposits.findIndex(l=>l.isUsed&&l.votingMintConfigIdx===s);return u===-1?{index:s,isInit:!1}:{index:u,isInit:!0}}export{bl as calFarmRewardAmount,yl as farmRewardInfoToConfig,Tl as fetchMultipleFarmInfoAndUpdate,wl as getAssociatedAuthority,ei as getAssociatedLedgerAccount,hl as getAssociatedLedgerPoolAccount,Pl as getDepositEntryIndex,ti as getFarmLedgerLayout,ni as getFarmStateLayout,xl as judgeFarmType,ri as updateFarmPoolInfo};
//# sourceMappingURL=util.mjs.map