UNPKG

@starkdefi/core-sdk

Version:

SDK for building StarkDefi applications

3 lines (2 loc) • 9.15 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,r=require("@starknet-react/chains"),n=(t=require("tiny-invariant"))&&"object"==typeof t&&"default"in t?t.default:t,e=require("starknet");function i(t){return[r.mainnet,r.sepolia].some((function(r){return r.id===t.id}))}var o=BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");function a(t,r){for(var n=0;n<r.length;n++){var e=r[n];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,c(e.key),e)}}function u(t,r,n){return r&&a(t.prototype,r),n&&a(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function s(t,r){t.prototype=Object.create(r.prototype),t.prototype.constructor=t,function(t,r){(Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,r){return t.__proto__=r,t})(t,r)}(t,r)}function c(t){var r=function(t,r){if("object"!=typeof t||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var e=n.call(t,"string");if("object"!=typeof e)return e;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==typeof r?r:r+""}var f=function(){function t(t,r){void 0===r&&(r=BigInt(1)),this.numerator=BigInt(t),this.denominator=BigInt(r)}t.tryParseFraction=function(r){if("bigint"==typeof r||"number"==typeof r||"string"==typeof r)return new t(r);if("numerator"in r&&"denominator"in r)return r;throw new Error("Could not parse fraction")};var r=t.prototype;return r.invert=function(){return new t(this.denominator,this.numerator)},r.add=function(r){var n=t.tryParseFraction(r);return this.denominator===n.denominator?new t(this.numerator+n.numerator,this.denominator):new t(this.numerator*n.denominator+n.numerator*this.denominator,this.denominator*n.denominator)},r.subtract=function(r){var n=t.tryParseFraction(r);return this.denominator===n.denominator?new t(this.numerator-n.numerator,this.denominator):new t(this.numerator*n.denominator-n.numerator*this.denominator,this.denominator*n.denominator)},r.lessThan=function(r){var n=t.tryParseFraction(r);return this.numerator*n.denominator<n.numerator*this.denominator},r.equalTo=function(r){var n=t.tryParseFraction(r);return this.numerator*n.denominator==n.numerator*this.denominator},r.greaterThan=function(r){var n=t.tryParseFraction(r);return this.numerator*n.denominator>n.numerator*this.denominator},r.multiply=function(r){var n=t.tryParseFraction(r);return new t(this.numerator*n.numerator,this.denominator*n.denominator)},r.divide=function(r){var n=t.tryParseFraction(r);return new t(this.numerator*n.denominator,this.denominator*n.numerator)},r.toSignificant=function(t){Number.isInteger(t)||n(!1),t>0||n(!1);var r=Number(this.numerator)/Number(this.denominator);if(0===r)return"0";var e=Math.floor(Math.log10(Math.abs(r)));if(r>=1){var i=Math.pow(10,t-e-1);return(Math.floor(r*i)/i).toString()}var o=Math.max(0,t-e-1),a=r.toFixed(o);if(a.includes(".")){var u=a.replace(/^0*\.?0*/,"").replace(".","");if(u.length>t){var s,c=u.substring(0,t),f=(null==(s=a.match(/^0*\.?(0*)/))||null==(s=s[1])?void 0:s.length)||0;a="0."+"0".repeat(f)+c}a=a.replace(/0+$/,"").replace(/\.$/,"")}return a},r.toFixed=function(t){return Number.isInteger(t)||n(!1),t>=0||n(!1),(Number(this.numerator)/Number(this.denominator)).toFixed(t)},u(t,[{key:"quotient",get:function(){return this.numerator/this.denominator}},{key:"remainder",get:function(){return new t(this.numerator%this.denominator,this.denominator)}},{key:"asFraction",get:function(){return new t(this.numerator,this.denominator)}}])}(),m=function(t){function r(r,e,i){var a;return(a=t.call(this,e,i)||this).quotient<=o||n(!1),a.token=r,a.decimalScale=BigInt(Math.pow(10,r.decimals)),a}s(r,t),r.fromRawAmount=function(t,n){return new r(t,n)},r.fromFractionalAmount=function(t,n,e){return new r(t,n,e)};var e=r.prototype;return e.add=function(e){this.token.equals(e.token)||n(!1);var i=t.prototype.add.call(this,e);return r.fromFractionalAmount(this.token,i.numerator,i.denominator)},e.subtract=function(e){this.token.equals(e.token)||n(!1);var i=t.prototype.subtract.call(this,e);return r.fromFractionalAmount(this.token,i.numerator,i.denominator)},e.multiply=function(n){var e=t.prototype.multiply.call(this,n);return r.fromFractionalAmount(this.token,e.numerator,e.denominator)},e.divide=function(n){var e=t.prototype.divide.call(this,n);return r.fromFractionalAmount(this.token,e.numerator,e.denominator)},e.toSignificant=function(r){return void 0===r&&(r=6),t.prototype.divide.call(this,this.decimalScale).toSignificant(r)},e.toFixed=function(r){return void 0===r&&(r=this.token.decimals),r<=this.token.decimals||n(!1),t.prototype.divide.call(this,this.decimalScale).toFixed(r)},e.toExact=function(){var t=(Number(this.quotient)/Number(this.decimalScale)).toFixed(this.token.decimals);return t.includes(".")&&(t=t.replace(/\.?0+$/,"")),t},u(r,[{key:"wrapped",get:function(){return this.token.isToken?this:r.fromFractionalAmount(this.token.wrapped,this.numerator,this.denominator)}}])}(f),h=function(t){function r(){for(var r,n,e,i,o,a=arguments.length,u=new Array(a),s=0;s<a;s++)u[s]=arguments[s];if(4===u.length)n=u[0],e=u[1],i=u[2],o=u[3];else{var c=u[0].quoteAmount.divide(u[0].baseAmount),m=[u[0].baseAmount.token,u[0].quoteAmount.token,c.denominator,c.numerator];n=m[0],e=m[1],i=m[2],o=m[3]}return(r=t.call(this,o,i)||this).baseCurrency=n,r.quoteCurrency=e,r.scalar=new f(BigInt(Math.pow(10,n.decimals)),BigInt(Math.pow(10,e.decimals))),r}s(r,t);var e=r.prototype;return e.invert=function(){return new r(this.quoteCurrency,this.baseCurrency,this.numerator,this.denominator)},e.multiply=function(e){this.quoteCurrency.equals(e.baseCurrency)||n(!1);var i=t.prototype.multiply.call(this,e);return new r(this.baseCurrency,e.quoteCurrency,i.denominator,i.numerator)},e.quote=function(r){r.token.equals(this.baseCurrency)||n(!1);var e=t.prototype.multiply.call(this,r);return m.fromFractionalAmount(this.quoteCurrency,e.numerator,e.denominator)},e.toSignificant=function(t){return void 0===t&&(t=6),this.adjustedForDecimals.toSignificant(t)},e.toFixed=function(t){return void 0===t&&(t=4),this.adjustedForDecimals.toFixed(t)},u(r,[{key:"adjustedForDecimals",get:function(){return t.prototype.multiply.call(this,this.scalar)}}])}(f),d=new f(BigInt(100));function l(t){return new p(t.numerator,t.denominator)}var p=function(t){function r(){var r;return(r=t.apply(this,arguments)||this).isPercent=!0,r}s(r,t);var n=r.prototype;return n.add=function(r){return l(t.prototype.add.call(this,r))},n.subtract=function(r){return l(t.prototype.subtract.call(this,r))},n.multiply=function(r){return l(t.prototype.multiply.call(this,r))},n.divide=function(r){return l(t.prototype.divide.call(this,r))},n.toSignificant=function(r){return void 0===r&&(r=5),t.prototype.multiply.call(this,d).toSignificant(r)},n.toFixed=function(r){return void 0===r&&(r=2),t.prototype.multiply.call(this,d).toFixed(r)},r}(f),y=function(t,r,e,o){i(t)||n(!1),r>=0&&r<255&&Number.isInteger(r)||n(!1),this.chain=t,this.decimals=r,this.symbol=e,this.name=o},v=function(t){function r(){var r;return(r=t.apply(this,arguments)||this).isNative=!0,r.isToken=!1,r}return s(r,t),r}(y);function g(t){if(/^0x[0-9a-fA-F]{63,64}$/.test(t))return t;throw new Error("Invalid Address Format")}var b=function(t){function r(r,n,i,o,a,u){var s;return(s=t.call(this,r,i,o,a)||this).isNative=!1,s.isToken=!0,s.address=u?g(n):e.validateAndParseAddress(n),s}s(r,t);var i=r.prototype;return i.equals=function(t){return t.isToken&&this.chain===t.chain&&this.address===t.address},i.sortsBefore=function(t){return this.chain!==t.chain&&n(!1),this.address===t.address&&n(!1),this.address.toLowerCase()<t.address.toLowerCase()},u(r,[{key:"wrapped",get:function(){return this}}])}(y),w=function(t){function r(r){var n=r.nativeCurrency;return t.call(this,r,n.decimals,n.symbol,n.name)||this}return s(r,t),r.onChain=function(t){var n;return null!=(n=this._etherCache[t.name])?n:this._etherCache[t.name]=new r(t)},r.prototype.equals=function(t){return t.isNative&&t.chain===this.chain},u(r,[{key:"wrapped",get:function(){var t=new b(this.chain,this.chain.nativeCurrency.address,this.chain.nativeCurrency.decimals,this.chain.nativeCurrency.symbol,this.chain.nativeCurrency.name);return t||n(!1),t}}])}(v);w._etherCache={};var k=BigInt(Number.MAX_SAFE_INTEGER),F=BigInt(0),x=BigInt(1),q=BigInt(2);exports.Ether=w,exports.Fraction=f,exports.MaxUint256=o,exports.NativeToken=v,exports.Percent=p,exports.Price=h,exports.Token=b,exports.TokenAmount=m,exports.checkValidAddress=g,exports.computePriceImpact=function(t,r,n){var e=t.quote(r),i=e.subtract(n).divide(e);return new p(i.numerator,i.denominator)},exports.isSupportedChainId=i,exports.sortedInsert=function(t,r,e,i){if(e>0||n(!1),t.length<=e||n(!1),0===t.length)return t.push(r),null;var o=t.length===e;if(o&&i(t[t.length-1],r)<=0)return r;for(var a=0,u=t.length;a<u;){var s=a+u>>>1;i(t[s],r)<=0?a=s+1:u=s}return t.splice(a,0,r),o?t.pop():null},exports.sqrt=function(t){if(t>=F||n(!1),t<k)return BigInt(Math.floor(Math.sqrt(Number(t))));var r,e;for(r=t,e=t/q+x;e<r;)r=e,e=(t/e+e)/q;return r}; //# sourceMappingURL=core-sdk.cjs.production.min.js.map