goosefx-amm-sdk
Version:
SDK for the GooseFx AMM
2 lines • 6.21 kB
JavaScript
import H from"big.js";import g from"bn.js";import s from"bn.js";import{get as E,set as R}from"lodash";var S=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(t=>typeof t=="object"?JSON.stringify(t):t).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}},F={},q={};function m(i){let e=E(F,i);if(!e){let r=E(q,i);e=new S({name:i,logLevel:r}),R(F,i,e)}return e}var V=new s(0),J=new s(1),X=new s(2),K=new s(3),Q=new s(5),k=new s(10),Y=new s(100),rr=new s(1e3),er=new s(1e4),U=9007199254740991;function a(i){let e=m("Gfx_parseBigNumberish");if(i instanceof s)return i;if(typeof i=="string"){if(i.match(/^-?[0-9]+$/))return new s(i);e.logWithError(`invalid BigNumberish string: ${i}`)}return typeof i=="number"?(i%1&&e.logWithError(`BigNumberish number underflow: ${i}`),(i>=U||i<=-U)&&e.logWithError(`BigNumberish number overflow: ${i}`),new s(String(i))):typeof i=="bigint"?new s(i.toString()):(e.error(`invalid BigNumberish value: ${i}`),new s(0))}import A from"toformat";var C=A,d=C;import B from"big.js";import T from"bn.js";import P from"decimal.js-light";var w=m("module/fraction"),O=d(B),h=d(P),j={[0]:h.ROUND_DOWN,[1]:h.ROUND_HALF_UP,[2]:h.ROUND_UP},$={[0]:B.roundDown,[1]:B.roundHalfUp,[2]:B.roundUp},o=class{constructor(e,r=new T(1)){this.numerator=a(e),this.denominator=a(r)}get quotient(){return this.numerator.div(this.denominator)}invert(){return new o(this.denominator,this.numerator)}add(e){let r=e instanceof o?e:new o(a(e));return this.denominator.eq(r.denominator)?new o(this.numerator.add(r.numerator),this.denominator):new o(this.numerator.mul(r.denominator).add(r.numerator.mul(this.denominator)),this.denominator.mul(r.denominator))}sub(e){let r=e instanceof o?e:new o(a(e));return this.denominator.eq(r.denominator)?new o(this.numerator.sub(r.numerator),this.denominator):new o(this.numerator.mul(r.denominator).sub(r.numerator.mul(this.denominator)),this.denominator.mul(r.denominator))}mul(e){let r=e instanceof o?e:new o(a(e));return new o(this.numerator.mul(r.numerator),this.denominator.mul(r.denominator))}div(e){let r=e instanceof o?e:new o(a(e));return new o(this.numerator.mul(r.denominator),this.denominator.mul(r.numerator))}toSignificant(e,r={groupSeparator:""},t=1){Number.isInteger(e)||w.logWithError(`${e} is not an integer.`),e<=0&&w.logWithError(`${e} is not positive.`),h.set({precision:e+1,rounding:j[t]});let n=new h(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(e);return n.toFormat(n.decimalPlaces(),r)}toFixed(e,r={groupSeparator:""},t=1){return Number.isInteger(e)||w.logWithError(`${e} is not an integer.`),e<0&&w.logWithError(`${e} is negative.`),O.DP=e,O.RM=$[t]||1,new O(this.numerator.toString()).div(this.denominator.toString()).toFormat(e,r)}isZero(){return this.numerator.isZero()}};var I=m("Gfx_amount"),D=d(H);function v(i,e){let r="0",t="0";if(i.includes(".")){let n=i.split(".");n.length===2?([r,t]=n,t=t.padEnd(e,"0")):I.logWithError(`invalid number string, num: ${i}`)}else r=i;return[r,t.slice(0,e)||t]}var f=class extends o{constructor(r,t,n=!0,y){let p=new g(0),c=k.pow(new g(r.decimals));if(n)p=a(t);else{let u=new g(0),l=new g(0);if(typeof t=="string"||typeof t=="number"||typeof t=="bigint"){let[L,x]=v(t.toString(),r.decimals);u=a(L),l=a(x)}u=u.mul(c),p=u.add(l)}super(p,c);this.logger=m(y||"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 f(this.token,this.raw.add(r.raw))}subtract(r){return this.token.equals(r.token)||this.logger.logWithError("sub token not equals"),new f(this.token,this.raw.sub(r.raw))}toSignificant(r=this.token.decimals,t,n=0){return super.toSignificant(r,t,n)}toFixed(r=this.token.decimals,t,n=0){return r>this.token.decimals&&this.logger.logWithError("decimals overflow"),super.toFixed(r,t,n)}toExact(r={groupSeparator:""}){return D.DP=this.token.decimals,new D(this.numerator.toString()).div(this.denominator.toString()).toFormat(r)}},b=class extends o{constructor(r,t,n=!0,y){let p=new g(0),c=k.pow(new g(r.decimals));if(n)p=a(t);else{let u=new g(0),l=new g(0);if(typeof t=="string"||typeof t=="number"||typeof t=="bigint"){let[L,x]=v(t.toString(),r.decimals);u=a(L),l=a(x)}u=u.mul(c),p=u.add(l)}super(p,c);this.logger=m(y||"TokenAmount"),this.currency=r}get raw(){return this.numerator}isZero(){return this.raw.isZero()}gt(r){return this.currency.equals(r.currency)||this.logger.logWithError("gt currency not equals"),this.raw.gt(r.raw)}lt(r){return this.currency.equals(r.currency)||this.logger.logWithError("lt currency not equals"),this.raw.lt(r.raw)}add(r){return this.currency.equals(r.currency)||this.logger.logWithError("add currency not equals"),new b(this.currency,this.raw.add(r.raw))}sub(r){return this.currency.equals(r.currency)||this.logger.logWithError("sub currency not equals"),new b(this.currency,this.raw.sub(r.raw))}toSignificant(r=this.currency.decimals,t,n=0){return super.toSignificant(r,t,n)}toFixed(r=this.currency.decimals,t,n=0){return r>this.currency.decimals&&this.logger.logWithError("decimals overflow"),super.toFixed(r,t,n)}toExact(r={groupSeparator:""}){return D.DP=this.currency.decimals,new D(this.numerator.toString()).div(this.denominator.toString()).toFormat(r)}};export{b as CurrencyAmount,f as TokenAmount,v as splitNumber};
//# sourceMappingURL=amount.mjs.map