UNPKG

goosefx-amm-sdk

Version:

SDK for the GooseFx AMM

2 lines 10.6 kB
"use strict";var ae=Object.create;var S=Object.defineProperty;var ue=Object.getOwnPropertyDescriptor;var ce=Object.getOwnPropertyNames;var me=Object.getPrototypeOf,pe=Object.prototype.hasOwnProperty;var le=(t,e)=>{for(var r in e)S(t,r,{get:e[r],enumerable:!0})},j=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of ce(e))!pe.call(t,s)&&s!==r&&S(t,s,{get:()=>e[s],enumerable:!(o=ue(e,s))||o.enumerable});return t};var m=(t,e,r)=>(r=t!=null?ae(me(t)):{},j(e||!t||!t.__esModule?S(r,"default",{value:t,enumerable:!0}):r,t)),ge=t=>j(S({},"__esModule",{value:!0}),t);var Be={};le(Be,{getTimestamp:()=>ke,jsonInfo2PoolKeys:()=>C,notInnerObject:()=>ne,sleep:()=>xe});module.exports=ge(Be);var te=require("@solana/web3.js"),oe=m(require("bn.js"));var Z=m(require("big.js")),x=m(require("bn.js"));var a=m(require("bn.js"));var y=require("lodash");var R=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}},I={},de={};function p(t){let e=(0,y.get)(I,t);if(!e){let r=(0,y.get)(de,t);e=new R({name:t,logLevel:r}),(0,y.set)(I,t,e)}return e}var Le=new a.default(0),Re=new a.default(1),Oe=new a.default(2),Pe=new a.default(3),Ee=new a.default(5),O=new a.default(10),Ae=new a.default(100),qe=new a.default(1e3),Me=new a.default(1e4),G=9007199254740991;function c(t){let e=p("Gfx_parseBigNumberish");if(t instanceof a.default)return t;if(typeof t=="string"){if(t.match(/^-?[0-9]+$/))return new a.default(t);e.logWithError(`invalid BigNumberish string: ${t}`)}return typeof t=="number"?(t%1&&e.logWithError(`BigNumberish number underflow: ${t}`),(t>=G||t<=-G)&&e.logWithError(`BigNumberish number overflow: ${t}`),new a.default(String(t))):typeof t=="bigint"?new a.default(t.toString()):(e.error(`invalid BigNumberish value: ${t}`),new a.default(0))}function P(t){return O.pow(c(t))}var H=m(require("toformat")),be=H.default,w=be;var N=m(require("big.js")),Y=m(require("bn.js")),X=m(require("decimal.js-light"));var D=p("module/fraction"),E=w(N.default),W=w(X.default),he={[0]:W.ROUND_DOWN,[1]:W.ROUND_HALF_UP,[2]:W.ROUND_UP},ye={[0]:N.default.roundDown,[1]:N.default.roundHalfUp,[2]:N.default.roundUp},i=class{constructor(e,r=new Y.default(1)){this.numerator=c(e),this.denominator=c(r)}get quotient(){return this.numerator.div(this.denominator)}invert(){return new i(this.denominator,this.numerator)}add(e){let r=e instanceof i?e:new i(c(e));return this.denominator.eq(r.denominator)?new i(this.numerator.add(r.numerator),this.denominator):new i(this.numerator.mul(r.denominator).add(r.numerator.mul(this.denominator)),this.denominator.mul(r.denominator))}sub(e){let r=e instanceof i?e:new i(c(e));return this.denominator.eq(r.denominator)?new i(this.numerator.sub(r.numerator),this.denominator):new i(this.numerator.mul(r.denominator).sub(r.numerator.mul(this.denominator)),this.denominator.mul(r.denominator))}mul(e){let r=e instanceof i?e:new i(c(e));return new i(this.numerator.mul(r.numerator),this.denominator.mul(r.denominator))}div(e){let r=e instanceof i?e:new i(c(e));return new i(this.numerator.mul(r.denominator),this.denominator.mul(r.numerator))}toSignificant(e,r={groupSeparator:""},o=1){Number.isInteger(e)||D.logWithError(`${e} is not an integer.`),e<=0&&D.logWithError(`${e} is not positive.`),W.set({precision:e+1,rounding:he[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)||D.logWithError(`${e} is not an integer.`),e<0&&D.logWithError(`${e} is negative.`),E.DP=e,E.RM=ye[o]||1,new E(this.numerator.toString()).div(this.denominator.toString()).toFormat(e,r)}isZero(){return this.numerator.isZero()}};var we=p("Gfx_amount"),z=w(Z.default);function Ne(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")):we.logWithError(`invalid number string, num: ${t}`)}else r=t;return[r,o.slice(0,e)||o]}var g=class extends i{constructor(r,o,s=!0,h){let l=new x.default(0),_=O.pow(new x.default(r.decimals));if(s)l=c(o);else{let B=new x.default(0),K=new x.default(0);if(typeof o=="string"||typeof o=="number"||typeof o=="bigint"){let[ie,se]=Ne(o.toString(),r.decimals);B=c(ie),K=c(se)}B=B.mul(_),l=B.add(K)}super(l,_);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 z.DP=this.token.decimals,new z(this.numerator.toString()).div(this.denominator.toString()).toFormat(r)}};var A=require("@solana/spl-token"),$={address:"So11111111111111111111111111111111111111112",programId:A.TOKEN_PROGRAM_ID.toBase58(),decimals:9,symbol:"SOL",name:"solana",logoURI:"",tags:[],priority:2,type:"goose-fx",extensions:{coingeckoId:"solana"}},d={address:"So11111111111111111111111111111111111111112",programId:A.TOKEN_PROGRAM_ID.toBase58(),decimals:9,symbol:"WSOL",name:"Wrapped SOL",logoURI:"",tags:[],priority:2,type:"goose-fx",extensions:{coingeckoId:"solana"}};var T=require("@solana/web3.js");var J=require("@solana/spl-token"),n=require("@solana/web3.js");function q({pubkey:t,isSigner:e=!1,isWritable:r=!0}){return{pubkey:t,isWritable:r,isSigner:e}}var er=[q({pubkey:J.TOKEN_PROGRAM_ID,isWritable:!1}),q({pubkey:n.SystemProgram.programId,isWritable:!1}),q({pubkey:n.SYSVAR_RENT_PUBKEY,isWritable:!1})];function Q({publicKey:t,transformSol:e}){let r=M(t.toString());if(r instanceof n.PublicKey)return e&&r.equals(k)?V:r;if(e&&r.toString()===k.toBase58())return V;if(typeof r=="string"){if(r===n.PublicKey.default.toBase58())return n.PublicKey.default;try{return new n.PublicKey(r)}catch{throw new Error("invalid public key")}}throw new Error("invalid public key")}function M(t){try{return new n.PublicKey(t)}catch{return t}}var rr=new n.PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),tr=new n.PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),or=new n.PublicKey("SysvarRent111111111111111111111111111111111"),nr=new n.PublicKey("SysvarC1ock11111111111111111111111111111111"),ir=new n.PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),sr=new n.PublicKey("Sysvar1nstructions1111111111111111111111111"),ar=n.SystemProgram.programId,ur=new n.PublicKey("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"),cr=new n.PublicKey("Ea5SjE2Y6yvCeW5dYTn7PYMuW5ikXkvbGdcmSnXeaLjS"),mr=new n.PublicKey("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"),pr=new n.PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),lr=new n.PublicKey("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"),gr=new n.PublicKey("mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So"),dr=new n.PublicKey("7dHbWXmci3dT8UFYWYZweBLXgycu7Y3iL6trKn1Y7ARj"),br=new n.PublicKey("USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX"),fr=new n.PublicKey("NRVwhjBQiUPYtfDT5zRBVJajzFQHaBUNtC7SNVvqRFa"),hr=new n.PublicKey("ANAxByE6G2WjFp7A4NqtWYXb3mgruyzZYg3spfxe6Lbo"),yr=new n.PublicKey("7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs"),V=new n.PublicKey("So11111111111111111111111111111111111111112"),k=n.PublicKey.default;var F=class{constructor({mint:e,decimals:r,symbol:o,name:s,skipMint:h=!1,isToken2022:l=!1}){if(e===k.toBase58()||e instanceof T.PublicKey&&k.equals(e)){this.decimals=d.decimals,this.symbol=d.symbol,this.name=d.name,this.mint=new T.PublicKey(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?T.PublicKey.default:Q({publicKey:e}),this.isToken2022=l}equals(e){return this===e?!0:this.mint.equals(e.mint)}},f=F;f.WSOL=new F({...d,mint:d.address});var v=class{constructor({decimals:e,symbol:r="UNKNOWN",name:o="UNKNOWN"}){this.decimals=e,this.symbol=r,this.name=o}equals(e){return this===e}},U=v;U.SOL=new v($);var re=m(require("bn.js"));var ee=new i(new re.default(100)),L=class extends i{toSignificant(e=5,r,o){return this.mul(ee).toSignificant(e,r,o)}toFixed(e=2,r,o){return this.mul(ee).toFixed(e,r,o)}};var We=p("Ray_price"),b=class extends i{constructor(r){let{baseToken:o,quoteToken:s,numerator:h,denominator:l}=r;super(h,l);this.baseToken=o,this.quoteToken=s,this.scalar=new i(P(o.decimals),P(s.decimals))}get raw(){return new i(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&&We.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 xe(t){new Promise(e=>setTimeout(e,t))}function ke(){return new Date().getTime()}function ne(t){return typeof t=="object"&&t!==null&&![f,g,te.PublicKey,i,oe.default,b,L].some(e=>typeof e=="object"&&t instanceof e)}function C(t){return typeof t=="string"?M(t):Array.isArray(t)?t.map(e=>C(e)):ne(t)?Object.fromEntries(Object.entries(t).map(([e,r])=>[e,C(r)])):t}0&&(module.exports={getTimestamp,jsonInfo2PoolKeys,notInnerObject,sleep}); //# sourceMappingURL=utility.js.map