UNPKG

goosefx-amm-sdk

Version:

SDK for the GooseFx AMM

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