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