UNPKG

goosefx-amm-sdk

Version:

SDK for the GooseFx AMM

2 lines 10.4 kB
import se from"big.js";import d from"bn.js";import u from"bn.js";import{get as _,set as V}from"lodash";var P=class{constructor(r){this.logLevel=r.logLevel!==void 0?r.logLevel:0,this.name=r.name}set level(r){this.logLevel=r}get time(){return Date.now().toString()}get moduleName(){return this.name}isLogLevel(r){return r<=this.logLevel}error(...r){return this.isLogLevel(0)?(console.error(this.time,this.name,"sdk logger error",...r),this):this}logWithError(...r){let e=r.map(t=>typeof t=="object"?JSON.stringify(t):t).join(", ");throw new Error(e)}warning(...r){return this.isLogLevel(1)?(console.warn(this.time,this.name,"sdk logger warning",...r),this):this}info(...r){return this.isLogLevel(2)?(console.info(this.time,this.name,"sdk logger info",...r),this):this}debug(...r){return this.isLogLevel(3)?(console.debug(this.time,this.name,"sdk logger debug",...r),this):this}},K={},J={};function l(o){let r=_(K,o);if(!r){let e=_(J,o);r=new P({name:o,logLevel:e}),V(K,o,r)}return r}var he=new u(0),ye=new u(1),we=new u(2),Ne=new u(3),We=new u(5),D=new u(10),xe=new u(100),ke=new u(1e3),Be=new u(1e4),I=9007199254740991;function a(o){let r=l("Gfx_parseBigNumberish");if(o instanceof u)return o;if(typeof o=="string"){if(o.match(/^-?[0-9]+$/))return new u(o);r.logWithError(`invalid BigNumberish string: ${o}`)}return typeof o=="number"?(o%1&&r.logWithError(`BigNumberish number underflow: ${o}`),(o>=I||o<=-I)&&r.logWithError(`BigNumberish number overflow: ${o}`),new u(String(o))):typeof o=="bigint"?new u(o.toString()):(r.error(`invalid BigNumberish value: ${o}`),new u(0))}function q(o){return D.pow(a(o))}import Q from"toformat";var ee=Q,N=ee;import T from"big.js";import te from"bn.js";import oe from"decimal.js-light";var L=l("module/fraction"),A=N(T),W=N(oe),ne={[0]:W.ROUND_DOWN,[1]:W.ROUND_HALF_UP,[2]:W.ROUND_UP},ie={[0]:T.roundDown,[1]:T.roundHalfUp,[2]:T.roundUp},n=class{constructor(r,e=new te(1)){this.numerator=a(r),this.denominator=a(e)}get quotient(){return this.numerator.div(this.denominator)}invert(){return new n(this.denominator,this.numerator)}add(r){let e=r instanceof n?r:new n(a(r));return this.denominator.eq(e.denominator)?new n(this.numerator.add(e.numerator),this.denominator):new n(this.numerator.mul(e.denominator).add(e.numerator.mul(this.denominator)),this.denominator.mul(e.denominator))}sub(r){let e=r instanceof n?r:new n(a(r));return this.denominator.eq(e.denominator)?new n(this.numerator.sub(e.numerator),this.denominator):new n(this.numerator.mul(e.denominator).sub(e.numerator.mul(this.denominator)),this.denominator.mul(e.denominator))}mul(r){let e=r instanceof n?r:new n(a(r));return new n(this.numerator.mul(e.numerator),this.denominator.mul(e.denominator))}div(r){let e=r instanceof n?r:new n(a(r));return new n(this.numerator.mul(e.denominator),this.denominator.mul(e.numerator))}toSignificant(r,e={groupSeparator:""},t=1){Number.isInteger(r)||L.logWithError(`${r} is not an integer.`),r<=0&&L.logWithError(`${r} is not positive.`),W.set({precision:r+1,rounding:ne[t]});let i=new W(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(r);return i.toFormat(i.decimalPlaces(),e)}toFixed(r,e={groupSeparator:""},t=1){return Number.isInteger(r)||L.logWithError(`${r} is not an integer.`),r<0&&L.logWithError(`${r} is negative.`),A.DP=r,A.RM=ie[t]||1,new A(this.numerator.toString()).div(this.denominator.toString()).toFormat(r,e)}isZero(){return this.numerator.isZero()}};var ae=l("Gfx_amount"),O=N(se);function j(o,r){let e="0",t="0";if(o.includes(".")){let i=o.split(".");i.length===2?([e,t]=i,t=t.padEnd(r,"0")):ae.logWithError(`invalid number string, num: ${o}`)}else e=o;return[e,t.slice(0,r)||t]}var x=class extends n{constructor(e,t,i=!0,f){let c=new d(0),y=D.pow(new d(e.decimals));if(i)c=a(t);else{let g=new d(0),w=new d(0);if(typeof t=="string"||typeof t=="number"||typeof t=="bigint"){let[R,E]=j(t.toString(),e.decimals);g=a(R),w=a(E)}g=g.mul(y),c=g.add(w)}super(c,y);this.logger=l(f||"TokenAmount"),this.token=e}get raw(){return this.numerator}isZero(){return this.raw.isZero()}gt(e){return this.token.equals(e.token)||this.logger.logWithError("gt token not equals"),this.raw.gt(e.raw)}lt(e){return this.token.equals(e.token)||this.logger.logWithError("lt token not equals"),this.raw.lt(e.raw)}add(e){return this.token.equals(e.token)||this.logger.logWithError("add token not equals"),new x(this.token,this.raw.add(e.raw))}subtract(e){return this.token.equals(e.token)||this.logger.logWithError("sub token not equals"),new x(this.token,this.raw.sub(e.raw))}toSignificant(e=this.token.decimals,t,i=0){return super.toSignificant(e,t,i)}toFixed(e=this.token.decimals,t,i=0){return e>this.token.decimals&&this.logger.logWithError("decimals overflow"),super.toFixed(e,t,i)}toExact(e={groupSeparator:""}){return O.DP=this.token.decimals,new O(this.numerator.toString()).div(this.denominator.toString()).toFormat(e)}},k=class extends n{constructor(e,t,i=!0,f){let c=new d(0),y=D.pow(new d(e.decimals));if(i)c=a(t);else{let g=new d(0),w=new d(0);if(typeof t=="string"||typeof t=="number"||typeof t=="bigint"){let[R,E]=j(t.toString(),e.decimals);g=a(R),w=a(E)}g=g.mul(y),c=g.add(w)}super(c,y);this.logger=l(f||"TokenAmount"),this.currency=e}get raw(){return this.numerator}isZero(){return this.raw.isZero()}gt(e){return this.currency.equals(e.currency)||this.logger.logWithError("gt currency not equals"),this.raw.gt(e.raw)}lt(e){return this.currency.equals(e.currency)||this.logger.logWithError("lt currency not equals"),this.raw.lt(e.raw)}add(e){return this.currency.equals(e.currency)||this.logger.logWithError("add currency not equals"),new k(this.currency,this.raw.add(e.raw))}sub(e){return this.currency.equals(e.currency)||this.logger.logWithError("sub currency not equals"),new k(this.currency,this.raw.sub(e.raw))}toSignificant(e=this.currency.decimals,t,i=0){return super.toSignificant(e,t,i)}toFixed(e=this.currency.decimals,t,i=0){return e>this.currency.decimals&&this.logger.logWithError("decimals overflow"),super.toFixed(e,t,i)}toExact(e={groupSeparator:""}){return O.DP=this.currency.decimals,new O(this.numerator.toString()).div(this.denominator.toString()).toFormat(e)}};import{TOKEN_PROGRAM_ID as G}from"@solana/spl-token";var H={address:"So11111111111111111111111111111111111111112",programId:G.toBase58(),decimals:9,symbol:"SOL",name:"solana",logoURI:"",tags:[],priority:2,type:"goose-fx",extensions:{coingeckoId:"solana"}},h={address:"So11111111111111111111111111111111111111112",programId:G.toBase58(),decimals:9,symbol:"WSOL",name:"Wrapped SOL",logoURI:"",tags:[],priority:2,type:"goose-fx",extensions:{coingeckoId:"solana"}};import{PublicKey as F}from"@solana/web3.js";import{TOKEN_PROGRAM_ID as ue}from"@solana/spl-token";import{PublicKey as s,SystemProgram as X,SYSVAR_RENT_PUBKEY as ce}from"@solana/web3.js";function M({pubkey:o,isSigner:r=!1,isWritable:e=!0}){return{pubkey:o,isWritable:e,isSigner:r}}var Je=[M({pubkey:ue,isWritable:!1}),M({pubkey:X.programId,isWritable:!1}),M({pubkey:ce,isWritable:!1})];function z({publicKey:o,transformSol:r}){let e=me(o.toString());if(e instanceof s)return r&&e.equals(B)?Y:e;if(r&&e.toString()===B.toBase58())return Y;if(typeof e=="string"){if(e===s.default.toBase58())return s.default;try{return new s(e)}catch{throw new Error("invalid public key")}}throw new Error("invalid public key")}function me(o){try{return new s(o)}catch{return o}}var Qe=new s("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),er=new s("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),rr=new s("SysvarRent111111111111111111111111111111111"),tr=new s("SysvarC1ock11111111111111111111111111111111"),or=new s("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),nr=new s("Sysvar1nstructions1111111111111111111111111"),ir=X.programId,sr=new s("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),ar=new s("Ea5SjE2Y6yvCeW5dYTn7PYMuW5ikXkvbGdcmSnXeaLjS"),ur=new s("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"),cr=new s("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),mr=new s("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),pr=new s("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),lr=new s("7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj"),gr=new s("USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX"),dr=new s("NRVwhjBQiUPYtfDT5zRBVJajzFQHaBUNtC7SNVvqRFa"),br=new s("ANAxByE6G2WjFp7A4NqtWYXb3mgruyzZYg3spfxe6Lbo"),fr=new s("7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs"),Y=new s("So11111111111111111111111111111111111111112"),B=s.default;var U=class{constructor({mint:r,decimals:e,symbol:t,name:i,skipMint:f=!1,isToken2022:c=!1}){if(r===B.toBase58()||r instanceof F&&B.equals(r)){this.decimals=h.decimals,this.symbol=h.symbol,this.name=h.name,this.mint=new F(h.address),this.isToken2022=!1;return}this.decimals=e,this.symbol=t||r.toString().substring(0,6),this.name=i||r.toString().substring(0,6),this.mint=f?F.default:z({publicKey:r}),this.isToken2022=c}equals(r){return this===r?!0:this.mint.equals(r.mint)}},b=U;b.WSOL=new U({...h,mint:h.address});var C=class{constructor({decimals:r,symbol:e="UNKNOWN",name:t="UNKNOWN"}){this.decimals=r,this.symbol=e,this.name=t}equals(r){return this===r}},v=C;v.SOL=new C(H);function Sr(o,r){return o instanceof b&&r instanceof b?o.equals(r):o instanceof b||r instanceof b?!1:o===r}import pe from"bn.js";var Z=new n(new pe(100)),$=class extends n{toSignificant(r=5,e,t){return this.mul(Z).toSignificant(r,e,t)}toFixed(r=2,e,t){return this.mul(Z).toFixed(r,e,t)}};var le=l("Ray_price"),S=class extends n{constructor(e){let{baseToken:t,quoteToken:i,numerator:f,denominator:c}=e;super(f,c);this.baseToken=t,this.quoteToken=i,this.scalar=new n(q(t.decimals),q(i.decimals))}get raw(){return new n(this.numerator,this.denominator)}get adjusted(){return super.mul(this.scalar)}invert(){return new S({baseToken:this.quoteToken,quoteToken:this.baseToken,denominator:this.numerator,numerator:this.denominator})}mul(e){this.quoteToken!==e.baseToken&&le.logWithError("mul token not equals");let t=super.mul(e);return new S({baseToken:this.baseToken,quoteToken:e.quoteToken,denominator:t.denominator,numerator:t.numerator})}toSignificant(e=this.quoteToken.decimals,t,i){return this.adjusted.toSignificant(e,t,i)}toFixed(e=this.quoteToken.decimals,t,i){return this.adjusted.toFixed(e,t,i)}};export{v as Currency,k as CurrencyAmount,n as Fraction,$ as Percent,S as Price,b as Token,x as TokenAmount,Z as _100_PERCENT,Sr as currencyEquals,j as splitNumber}; //# sourceMappingURL=index.mjs.map