UNPKG

goosefx-amm-sdk

Version:

SDK for the GooseFx AMM

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