UNPKG

goosefx-amm-sdk

Version:

SDK for the GooseFx AMM

2 lines 4.24 kB
import i from"bn.js";import{get as N,set as k}from"lodash";var b=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(n=>typeof n=="object"?JSON.stringify(n):n).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}},W={},x={};function s(o){let e=N(W,o);if(!e){let r=N(x,o);e=new b({name:o,logLevel:r}),k(W,o,e)}return e}var C=new i(0),j=new i(1),A=new i(2),$=new i(3),H=new i(5),y=new i(10),I=new i(100),M=new i(1e3),G=new i(1e4),D=9007199254740991;function m(o){let e=s("Gfx_parseBigNumberish");if(o instanceof i)return o;if(typeof o=="string"){if(o.match(/^-?[0-9]+$/))return new i(o);e.logWithError(`invalid BigNumberish string: ${o}`)}return typeof o=="number"?(o%1&&e.logWithError(`BigNumberish number underflow: ${o}`),(o>=D||o<=-D)&&e.logWithError(`BigNumberish number overflow: ${o}`),new i(String(o))):typeof o=="bigint"?new i(o.toString()):(e.error(`invalid BigNumberish value: ${o}`),new i(0))}function f(o){return y.pow(m(o))}import l from"big.js";import F from"bn.js";import O from"decimal.js-light";import S from"toformat";var T=S,h=T;var d=s("module/fraction"),B=h(l),u=h(O),U={[0]:u.ROUND_DOWN,[1]:u.ROUND_HALF_UP,[2]:u.ROUND_UP},v={[0]:l.roundDown,[1]:l.roundHalfUp,[2]:l.roundUp},t=class{constructor(e,r=new F(1)){this.numerator=m(e),this.denominator=m(r)}get quotient(){return this.numerator.div(this.denominator)}invert(){return new t(this.denominator,this.numerator)}add(e){let r=e instanceof t?e:new t(m(e));return this.denominator.eq(r.denominator)?new t(this.numerator.add(r.numerator),this.denominator):new t(this.numerator.mul(r.denominator).add(r.numerator.mul(this.denominator)),this.denominator.mul(r.denominator))}sub(e){let r=e instanceof t?e:new t(m(e));return this.denominator.eq(r.denominator)?new t(this.numerator.sub(r.numerator),this.denominator):new t(this.numerator.mul(r.denominator).sub(r.numerator.mul(this.denominator)),this.denominator.mul(r.denominator))}mul(e){let r=e instanceof t?e:new t(m(e));return new t(this.numerator.mul(r.numerator),this.denominator.mul(r.denominator))}div(e){let r=e instanceof t?e:new t(m(e));return new t(this.numerator.mul(r.denominator),this.denominator.mul(r.numerator))}toSignificant(e,r={groupSeparator:""},n=1){Number.isInteger(e)||d.logWithError(`${e} is not an integer.`),e<=0&&d.logWithError(`${e} is not positive.`),u.set({precision:e+1,rounding:U[n]});let a=new u(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(e);return a.toFormat(a.decimalPlaces(),r)}toFixed(e,r={groupSeparator:""},n=1){return Number.isInteger(e)||d.logWithError(`${e} is not an integer.`),e<0&&d.logWithError(`${e} is negative.`),B.DP=e,B.RM=v[n]||1,new B(this.numerator.toString()).div(this.denominator.toString()).toFormat(e,r)}isZero(){return this.numerator.isZero()}};var E=s("Ray_price"),p=class extends t{constructor(r){let{baseToken:n,quoteToken:a,numerator:w,denominator:L}=r;super(w,L);this.baseToken=n,this.quoteToken=a,this.scalar=new t(f(n.decimals),f(a.decimals))}get raw(){return new t(this.numerator,this.denominator)}get adjusted(){return super.mul(this.scalar)}invert(){return new p({baseToken:this.quoteToken,quoteToken:this.baseToken,denominator:this.numerator,numerator:this.denominator})}mul(r){this.quoteToken!==r.baseToken&&E.logWithError("mul token not equals");let n=super.mul(r);return new p({baseToken:this.baseToken,quoteToken:r.quoteToken,denominator:n.denominator,numerator:n.numerator})}toSignificant(r=this.quoteToken.decimals,n,a){return this.adjusted.toSignificant(r,n,a)}toFixed(r=this.quoteToken.decimals,n,a){return this.adjusted.toFixed(r,n,a)}};export{p as Price}; //# sourceMappingURL=price.mjs.map