UNPKG

goosefx-amm-sdk

Version:

SDK for the GooseFx AMM

2 lines 7.14 kB
"use strict";var P=Object.create;var B=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var H=Object.getPrototypeOf,I=Object.prototype.hasOwnProperty;var M=(t,e)=>{for(var r in e)B(t,r,{get:e[r],enumerable:!0})},v=(t,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of $(e))!I.call(t,o)&&o!==r&&B(t,o,{get:()=>e[o],enumerable:!(i=j(e,o))||i.enumerable});return t};var c=(t,e,r)=>(r=t!=null?P(H(t)):{},v(e||!t||!t.__esModule?B(r,"default",{value:t,enumerable:!0}):r,t)),Z=t=>v(B({},"__esModule",{value:!0}),t);var Q={};M(Q,{CurrencyAmount:()=>d,TokenAmount:()=>l,splitNumber:()=>U});module.exports=Z(Q);var T=c(require("big.js")),m=c(require("bn.js"));var s=c(require("bn.js"));var b=require("lodash");var O=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(i=>typeof i=="object"?JSON.stringify(i):i).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}},R={},G={};function g(t){let e=(0,b.get)(R,t);if(!e){let r=(0,b.get)(G,t);e=new O({name:t,logLevel:r}),(0,b.set)(R,t,e)}return e}var tr=new s.default(0),ir=new s.default(1),or=new s.default(2),nr=new s.default(3),sr=new s.default(5),E=new s.default(10),ar=new s.default(100),ur=new s.default(1e3),mr=new s.default(1e4),q=9007199254740991;function a(t){let e=g("Gfx_parseBigNumberish");if(t instanceof s.default)return t;if(typeof t=="string"){if(t.match(/^-?[0-9]+$/))return new s.default(t);e.logWithError(`invalid BigNumberish string: ${t}`)}return typeof t=="number"?(t%1&&e.logWithError(`BigNumberish number underflow: ${t}`),(t>=q||t<=-q)&&e.logWithError(`BigNumberish number overflow: ${t}`),new s.default(String(t))):typeof t=="bigint"?new s.default(t.toString()):(e.error(`invalid BigNumberish value: ${t}`),new s.default(0))}var A=c(require("toformat")),z=A.default,N=z;var W=c(require("big.js")),C=c(require("bn.js")),_=c(require("decimal.js-light"));var y=g("module/fraction"),F=N(W.default),w=N(_.default),J={[0]:w.ROUND_DOWN,[1]:w.ROUND_HALF_UP,[2]:w.ROUND_UP},X={[0]:W.default.roundDown,[1]:W.default.roundHalfUp,[2]:W.default.roundUp},n=class{constructor(e,r=new C.default(1)){this.numerator=a(e),this.denominator=a(r)}get quotient(){return this.numerator.div(this.denominator)}invert(){return new n(this.denominator,this.numerator)}add(e){let r=e instanceof n?e:new n(a(e));return this.denominator.eq(r.denominator)?new n(this.numerator.add(r.numerator),this.denominator):new n(this.numerator.mul(r.denominator).add(r.numerator.mul(this.denominator)),this.denominator.mul(r.denominator))}sub(e){let r=e instanceof n?e:new n(a(e));return this.denominator.eq(r.denominator)?new n(this.numerator.sub(r.numerator),this.denominator):new n(this.numerator.mul(r.denominator).sub(r.numerator.mul(this.denominator)),this.denominator.mul(r.denominator))}mul(e){let r=e instanceof n?e:new n(a(e));return new n(this.numerator.mul(r.numerator),this.denominator.mul(r.denominator))}div(e){let r=e instanceof n?e:new n(a(e));return new n(this.numerator.mul(r.denominator),this.denominator.mul(r.numerator))}toSignificant(e,r={groupSeparator:""},i=1){Number.isInteger(e)||y.logWithError(`${e} is not an integer.`),e<=0&&y.logWithError(`${e} is not positive.`),w.set({precision:e+1,rounding:J[i]});let o=new w(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(e);return o.toFormat(o.decimalPlaces(),r)}toFixed(e,r={groupSeparator:""},i=1){return Number.isInteger(e)||y.logWithError(`${e} is not an integer.`),e<0&&y.logWithError(`${e} is negative.`),F.DP=e,F.RM=X[i]||1,new F(this.numerator.toString()).div(this.denominator.toString()).toFormat(e,r)}isZero(){return this.numerator.isZero()}};var K=g("Gfx_amount"),L=N(T.default);function U(t,e){let r="0",i="0";if(t.includes(".")){let o=t.split(".");o.length===2?([r,i]=o,i=i.padEnd(e,"0")):K.logWithError(`invalid number string, num: ${t}`)}else r=t;return[r,i.slice(0,e)||i]}var l=class extends n{constructor(r,i,o=!0,x){let p=new m.default(0),h=E.pow(new m.default(r.decimals));if(o)p=a(i);else{let u=new m.default(0),f=new m.default(0);if(typeof i=="string"||typeof i=="number"||typeof i=="bigint"){let[S,k]=U(i.toString(),r.decimals);u=a(S),f=a(k)}u=u.mul(h),p=u.add(f)}super(p,h);this.logger=g(x||"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 l(this.token,this.raw.add(r.raw))}subtract(r){return this.token.equals(r.token)||this.logger.logWithError("sub token not equals"),new l(this.token,this.raw.sub(r.raw))}toSignificant(r=this.token.decimals,i,o=0){return super.toSignificant(r,i,o)}toFixed(r=this.token.decimals,i,o=0){return r>this.token.decimals&&this.logger.logWithError("decimals overflow"),super.toFixed(r,i,o)}toExact(r={groupSeparator:""}){return L.DP=this.token.decimals,new L(this.numerator.toString()).div(this.denominator.toString()).toFormat(r)}},d=class extends n{constructor(r,i,o=!0,x){let p=new m.default(0),h=E.pow(new m.default(r.decimals));if(o)p=a(i);else{let u=new m.default(0),f=new m.default(0);if(typeof i=="string"||typeof i=="number"||typeof i=="bigint"){let[S,k]=U(i.toString(),r.decimals);u=a(S),f=a(k)}u=u.mul(h),p=u.add(f)}super(p,h);this.logger=g(x||"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 d(this.currency,this.raw.add(r.raw))}sub(r){return this.currency.equals(r.currency)||this.logger.logWithError("sub currency not equals"),new d(this.currency,this.raw.sub(r.raw))}toSignificant(r=this.currency.decimals,i,o=0){return super.toSignificant(r,i,o)}toFixed(r=this.currency.decimals,i,o=0){return r>this.currency.decimals&&this.logger.logWithError("decimals overflow"),super.toFixed(r,i,o)}toExact(r={groupSeparator:""}){return L.DP=this.currency.decimals,new L(this.numerator.toString()).div(this.denominator.toString()).toFormat(r)}};0&&(module.exports={CurrencyAmount,TokenAmount,splitNumber}); //# sourceMappingURL=amount.js.map