UNPKG

@kyberswap/ks-sdk-core

Version:

🛠 An CORE SDK for building DMM Classic SDK, Elastic SDK

627 lines (605 loc) • 28.1 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var JSBI = _interopDefault(require('jsbi')); var invariant = _interopDefault(require('tiny-invariant')); var _Decimal = _interopDefault(require('decimal.js-light')); var _Big = _interopDefault(require('big.js')); var toFormat = _interopDefault(require('toformat')); var address = require('@ethersproject/address'); (function (TradeType) { TradeType[TradeType["EXACT_INPUT"] = 0] = "EXACT_INPUT"; TradeType[TradeType["EXACT_OUTPUT"] = 1] = "EXACT_OUTPUT"; })(exports.TradeType || (exports.TradeType = {})); (function (Rounding) { Rounding[Rounding["ROUND_DOWN"] = 0] = "ROUND_DOWN"; Rounding[Rounding["ROUND_HALF_UP"] = 1] = "ROUND_HALF_UP"; Rounding[Rounding["ROUND_UP"] = 2] = "ROUND_UP"; })(exports.Rounding || (exports.Rounding = {})); var MaxUint256 = /*#__PURE__*/JSBI.BigInt('0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'); function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _inheritsLoose(t, o) { t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } var _toSignificantRoundin, _toFixedRounding; var Decimal = /*#__PURE__*/toFormat(_Decimal); var Big = /*#__PURE__*/toFormat(_Big); var toSignificantRounding = (_toSignificantRoundin = {}, _toSignificantRoundin[exports.Rounding.ROUND_DOWN] = Decimal.ROUND_DOWN, _toSignificantRoundin[exports.Rounding.ROUND_HALF_UP] = Decimal.ROUND_HALF_UP, _toSignificantRoundin[exports.Rounding.ROUND_UP] = Decimal.ROUND_UP, _toSignificantRoundin); var toFixedRounding = (_toFixedRounding = {}, _toFixedRounding[exports.Rounding.ROUND_DOWN] = 0, _toFixedRounding[exports.Rounding.ROUND_HALF_UP] = 1, _toFixedRounding[exports.Rounding.ROUND_UP] = 3, _toFixedRounding); var Fraction = /*#__PURE__*/function () { function Fraction(numerator, denominator) { if (denominator === void 0) { denominator = JSBI.BigInt(1); } this.numerator = JSBI.BigInt(numerator); this.denominator = JSBI.BigInt(denominator); } Fraction.tryParseFraction = function tryParseFraction(fractionish) { if (fractionish instanceof JSBI || typeof fractionish === 'number' || typeof fractionish === 'string') return new Fraction(fractionish); if ('numerator' in fractionish && 'denominator' in fractionish) return fractionish; throw new Error('Could not parse fraction'); } // performs floor division ; var _proto = Fraction.prototype; _proto.invert = function invert() { return new Fraction(this.denominator, this.numerator); }; _proto.add = function add(other) { var otherParsed = Fraction.tryParseFraction(other); if (JSBI.equal(this.denominator, otherParsed.denominator)) { return new Fraction(JSBI.add(this.numerator, otherParsed.numerator), this.denominator); } return new Fraction(JSBI.add(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator)), JSBI.multiply(this.denominator, otherParsed.denominator)); }; _proto.subtract = function subtract(other) { var otherParsed = Fraction.tryParseFraction(other); if (JSBI.equal(this.denominator, otherParsed.denominator)) { return new Fraction(JSBI.subtract(this.numerator, otherParsed.numerator), this.denominator); } return new Fraction(JSBI.subtract(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator)), JSBI.multiply(this.denominator, otherParsed.denominator)); }; _proto.lessThan = function lessThan(other) { var otherParsed = Fraction.tryParseFraction(other); return JSBI.lessThan(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator)); }; _proto.equalTo = function equalTo(other) { var otherParsed = Fraction.tryParseFraction(other); return JSBI.equal(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator)); }; _proto.greaterThan = function greaterThan(other) { var otherParsed = Fraction.tryParseFraction(other); return JSBI.greaterThan(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(otherParsed.numerator, this.denominator)); }; _proto.multiply = function multiply(other) { var otherParsed = Fraction.tryParseFraction(other); return new Fraction(JSBI.multiply(this.numerator, otherParsed.numerator), JSBI.multiply(this.denominator, otherParsed.denominator)); }; _proto.divide = function divide(other) { var otherParsed = Fraction.tryParseFraction(other); return new Fraction(JSBI.multiply(this.numerator, otherParsed.denominator), JSBI.multiply(this.denominator, otherParsed.numerator)); }; _proto.toSignificant = function toSignificant(significantDigits, format, rounding) { if (format === void 0) { format = { groupSeparator: '' }; } if (rounding === void 0) { rounding = exports.Rounding.ROUND_HALF_UP; } !Number.isInteger(significantDigits) ? invariant(false, significantDigits + " is not an integer.") : void 0; !(significantDigits > 0) ? invariant(false, significantDigits + " is not positive.") : void 0; Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] }); var quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits); return quotient.toFormat(quotient.decimalPlaces(), format); }; _proto.toFixed = function toFixed(decimalPlaces, format, rounding) { if (format === void 0) { format = { groupSeparator: '' }; } if (rounding === void 0) { rounding = exports.Rounding.ROUND_HALF_UP; } !Number.isInteger(decimalPlaces) ? invariant(false, decimalPlaces + " is not an integer.") : void 0; !(decimalPlaces >= 0) ? invariant(false, decimalPlaces + " is negative.") : void 0; Big.DP = decimalPlaces; Big.RM = toFixedRounding[rounding]; return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format); } /** * Helper method for converting any super class back to a fraction */; return _createClass(Fraction, [{ key: "quotient", get: function get() { return JSBI.divide(this.numerator, this.denominator); } // remainder after floor division }, { key: "remainder", get: function get() { return new Fraction(JSBI.remainder(this.numerator, this.denominator), this.denominator); } }, { key: "asFraction", get: function get() { return new Fraction(this.numerator, this.denominator); } }]); }(); var Big$1 = /*#__PURE__*/toFormat(_Big); var CurrencyAmount = /*#__PURE__*/function (_Fraction) { function CurrencyAmount(currency, numerator, denominator) { var _this; _this = _Fraction.call(this, numerator, denominator) || this; !JSBI.lessThanOrEqual(_this.quotient, MaxUint256) ? invariant(false, 'AMOUNT') : void 0; _this.currency = currency; _this.decimalScale = JSBI.exponentiate(JSBI.BigInt(10), JSBI.BigInt(currency.decimals)); return _this; } /** * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount * @param currency the currency in the amount * @param rawAmount the raw token or ether amount */ _inheritsLoose(CurrencyAmount, _Fraction); CurrencyAmount.fromRawAmount = function fromRawAmount(currency, rawAmount) { return new CurrencyAmount(currency, rawAmount); } /** * Construct a currency amount with a denominator that is not equal to 1 * @param currency the currency * @param numerator the numerator of the fractional token amount * @param denominator the denominator of the fractional token amount */; CurrencyAmount.fromFractionalAmount = function fromFractionalAmount(currency, numerator, denominator) { return new CurrencyAmount(currency, numerator, denominator); }; var _proto = CurrencyAmount.prototype; _proto.add = function add(other) { !this.currency.equals(other.currency) ? invariant(false, 'CURRENCY') : void 0; var added = _Fraction.prototype.add.call(this, other); return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator); }; _proto.subtract = function subtract(other) { !this.currency.equals(other.currency) ? invariant(false, 'CURRENCY') : void 0; var subtracted = _Fraction.prototype.subtract.call(this, other); return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator); }; _proto.multiply = function multiply(other) { var multiplied = _Fraction.prototype.multiply.call(this, other); return CurrencyAmount.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator); }; _proto.divide = function divide(other) { var divided = _Fraction.prototype.divide.call(this, other); return CurrencyAmount.fromFractionalAmount(this.currency, divided.numerator, divided.denominator); }; _proto.toSignificant = function toSignificant(significantDigits, format, rounding) { if (significantDigits === void 0) { significantDigits = 6; } if (rounding === void 0) { rounding = exports.Rounding.ROUND_DOWN; } return _Fraction.prototype.divide.call(this, this.decimalScale).toSignificant(significantDigits, format, rounding); }; _proto.toFixed = function toFixed(decimalPlaces, format, rounding) { if (decimalPlaces === void 0) { decimalPlaces = this.currency.decimals; } if (rounding === void 0) { rounding = exports.Rounding.ROUND_DOWN; } !(decimalPlaces <= this.currency.decimals) ? invariant(false, 'DECIMALS') : void 0; return _Fraction.prototype.divide.call(this, this.decimalScale).toFixed(decimalPlaces, format, rounding); }; _proto.toExact = function toExact(format) { if (format === void 0) { format = { groupSeparator: '' }; } Big$1.DP = this.currency.decimals; return new Big$1(this.quotient.toString()).div(this.decimalScale.toString()).toFormat(format); }; return _createClass(CurrencyAmount, [{ key: "wrapped", get: function get() { if (this.currency.isToken) return this; return CurrencyAmount.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator); } }]); }(Fraction); var TokenAmount = /*#__PURE__*/function (_CurrencyAmount) { function TokenAmount() { return _CurrencyAmount.apply(this, arguments) || this; } _inheritsLoose(TokenAmount, _CurrencyAmount); return TokenAmount; }(CurrencyAmount); var ONE_HUNDRED = /*#__PURE__*/new Fraction(/*#__PURE__*/JSBI.BigInt(100)); /** * Converts a fraction to a percent * @param fraction the fraction to convert */ function toPercent(fraction) { return new Percent(fraction.numerator, fraction.denominator); } var Percent = /*#__PURE__*/function (_Fraction) { function Percent() { var _this; _this = _Fraction.apply(this, arguments) || this; /** * This boolean prevents a fraction from being interpreted as a Percent */ _this.isPercent = true; return _this; } _inheritsLoose(Percent, _Fraction); var _proto = Percent.prototype; _proto.add = function add(other) { return toPercent(_Fraction.prototype.add.call(this, other)); }; _proto.subtract = function subtract(other) { return toPercent(_Fraction.prototype.subtract.call(this, other)); }; _proto.multiply = function multiply(other) { return toPercent(_Fraction.prototype.multiply.call(this, other)); }; _proto.divide = function divide(other) { return toPercent(_Fraction.prototype.divide.call(this, other)); }; _proto.toSignificant = function toSignificant(significantDigits, format, rounding) { if (significantDigits === void 0) { significantDigits = 5; } return _Fraction.prototype.multiply.call(this, ONE_HUNDRED).toSignificant(significantDigits, format, rounding); }; _proto.toFixed = function toFixed(decimalPlaces, format, rounding) { if (decimalPlaces === void 0) { decimalPlaces = 2; } return _Fraction.prototype.multiply.call(this, ONE_HUNDRED).toFixed(decimalPlaces, format, rounding); }; return Percent; }(Fraction); var Price = /*#__PURE__*/function (_Fraction) { /** * Construct a price, either with the base and quote currency amount, or the * @param args */ function Price() { var _this; var baseCurrency, quoteCurrency, denominator, numerator; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } if (args.length === 4) { baseCurrency = args[0]; quoteCurrency = args[1]; denominator = args[2]; numerator = args[3]; } else { var result = args[0].quoteAmount.divide(args[0].baseAmount); var _ref = [args[0].baseAmount.currency, args[0].quoteAmount.currency, result.denominator, result.numerator]; baseCurrency = _ref[0]; quoteCurrency = _ref[1]; denominator = _ref[2]; numerator = _ref[3]; } _this = _Fraction.call(this, numerator, denominator) || this; _this.baseCurrency = baseCurrency; _this.quoteCurrency = quoteCurrency; _this.scalar = new Fraction(JSBI.exponentiate(JSBI.BigInt(10), JSBI.BigInt(baseCurrency.decimals)), JSBI.exponentiate(JSBI.BigInt(10), JSBI.BigInt(quoteCurrency.decimals))); return _this; } /** * Flip the price, switching the base and quote currency */ _inheritsLoose(Price, _Fraction); var _proto = Price.prototype; _proto.invert = function invert() { return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator); } /** * Multiply the price by another price, returning a new price. The other price must have the same base currency as this price's quote currency * @param other the other price */; _proto.multiply = function multiply(other) { !this.quoteCurrency.equals(other.baseCurrency) ? invariant(false, 'TOKEN') : void 0; var fraction = _Fraction.prototype.multiply.call(this, other); return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator); } /** * Return the amount of quote currency corresponding to a given amount of the base currency * @param currencyAmount the amount of base currency to quote against the price */; _proto.quote = function quote(currencyAmount) { !currencyAmount.currency.equals(this.baseCurrency) ? invariant(false, 'TOKEN') : void 0; var result = _Fraction.prototype.multiply.call(this, currencyAmount); return CurrencyAmount.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator); } /** * Get the value scaled by decimals for formatting * @private */; _proto.toSignificant = function toSignificant(significantDigits, format, rounding) { if (significantDigits === void 0) { significantDigits = 6; } return this.adjustedForDecimals.toSignificant(significantDigits, format, rounding); }; _proto.toFixed = function toFixed(decimalPlaces, format, rounding) { if (decimalPlaces === void 0) { decimalPlaces = 4; } return this.adjustedForDecimals.toFixed(decimalPlaces, format, rounding); }; return _createClass(Price, [{ key: "adjustedForDecimals", get: function get() { return _Fraction.prototype.multiply.call(this, this.scalar); } }]); }(Fraction); (function (ChainId) { ChainId[ChainId["MAINNET"] = 1] = "MAINNET"; ChainId[ChainId["G\xD6RLI"] = 5] = "G\xD6RLI"; ChainId[ChainId["MATIC"] = 137] = "MATIC"; ChainId[ChainId["BSCMAINNET"] = 56] = "BSCMAINNET"; ChainId[ChainId["AVAXMAINNET"] = 43114] = "AVAXMAINNET"; ChainId[ChainId["FANTOM"] = 250] = "FANTOM"; ChainId[ChainId["ARBITRUM"] = 42161] = "ARBITRUM"; ChainId[ChainId["OPTIMISM"] = 10] = "OPTIMISM"; ChainId[ChainId["ZKSYNC"] = 324] = "ZKSYNC"; ChainId[ChainId["LINEA"] = 59144] = "LINEA"; ChainId[ChainId["BASE"] = 8453] = "BASE"; ChainId[ChainId["SCROLL"] = 534352] = "SCROLL"; ChainId[ChainId["BLAST"] = 81457] = "BLAST"; ChainId[ChainId["MANTLE"] = 5000] = "MANTLE"; ChainId[ChainId["SONIC"] = 146] = "SONIC"; ChainId[ChainId["BERA"] = 80094] = "BERA"; ChainId[ChainId["RONIN"] = 2020] = "RONIN"; ChainId[ChainId["UNICHAIN"] = 130] = "UNICHAIN"; ChainId[ChainId["HYPEREVM"] = 999] = "HYPEREVM"; })(exports.ChainId || (exports.ChainId = {})); /** * A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies */ var BaseCurrency = /** * Constructs an instance of the base class `BaseCurrency`. * @param chainId the chain ID on which this currency resides * @param decimals decimals of the currency * @param symbol symbol of the currency * @param name of the currency */ function BaseCurrency(chainId, decimals, symbol, name) { !Number.isSafeInteger(chainId) ? invariant(false, 'CHAIN_ID') : void 0; !(chainId in exports.ChainId) ? invariant(false, 'CHAIN_ID_NOT_SUPPORTED') : void 0; !(decimals >= 0 && decimals < 255 && Number.isInteger(decimals)) ? invariant(false, 'DECIMALS') : void 0; this.chainId = chainId; this.decimals = decimals; this.symbol = symbol; this.name = name; }; /** * Validates an address and returns the parsed (checksummed) version of that address * @param address the unchecksummed hex address */ function validateAndParseAddress(address$1) { try { return address.getAddress(address$1); } catch (error) { throw new Error(address$1 + " is not a valid address."); } } /** * Represents an ERC20 token with a unique address and some metadata. */ var Token = /*#__PURE__*/function (_BaseCurrency) { function Token(chainId, address, decimals, symbol, name) { var _this; _this = _BaseCurrency.call(this, chainId, decimals, symbol, name) || this; _this.isNative = false; _this.isToken = true; _this.address = validateAndParseAddress(address); return _this; } /** * Returns true if the two tokens are equivalent, i.e. have the same chainId and address. * @param other other token to compare */ _inheritsLoose(Token, _BaseCurrency); var _proto = Token.prototype; _proto.equals = function equals(other) { return other.isToken && this.chainId === other.chainId && this.address === other.address; } /** * Returns true if the address of this token sorts before the address of the other token * @param other other token to compare * @throws if the tokens have the same address * @throws if the tokens are on different chains */; _proto.sortsBefore = function sortsBefore(other) { !(this.chainId === other.chainId) ? invariant(false, 'CHAIN_IDS') : void 0; !(this.address !== other.address) ? invariant(false, 'ADDRESSES') : void 0; return this.address.toLowerCase() < other.address.toLowerCase(); } /** * Return this token, which does not need to be wrapped */; return _createClass(Token, [{ key: "wrapped", get: function get() { return this; } }]); }(BaseCurrency); var _WETH; var WETH = (_WETH = {}, _WETH[exports.ChainId.MAINNET] = /*#__PURE__*/new Token(exports.ChainId.MAINNET, '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', 18, 'WETH', 'Wrapped Ether'), _WETH[exports.ChainId.GÖRLI] = /*#__PURE__*/new Token(exports.ChainId.GÖRLI, '0x48f6D7dAE56623Dde5a0D56B283165cAE1753D70', 18, 'WETH', 'Wrapped Ether'), _WETH[exports.ChainId.MATIC] = /*#__PURE__*/new Token(exports.ChainId.MATIC, '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', 18, 'WMATIC', 'Wrapped Matic'), _WETH[exports.ChainId.BSCMAINNET] = /*#__PURE__*/new Token(exports.ChainId.BSCMAINNET, '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', 18, 'WBNB', 'Wrapped BNB'), _WETH[exports.ChainId.AVAXMAINNET] = /*#__PURE__*/new Token(exports.ChainId.AVAXMAINNET, '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7', 18, 'WAVAX', 'Wrapped AVAX'), _WETH[exports.ChainId.FANTOM] = /*#__PURE__*/new Token(exports.ChainId.FANTOM, '0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83', 18, 'WFTM', 'Wrapped Fantom'), _WETH[exports.ChainId.ARBITRUM] = /*#__PURE__*/new Token(exports.ChainId.ARBITRUM, '0x82aF49447D8a07e3bd95BD0d56f35241523fBab1', 18, 'WETH', 'Wrapped Ether'), _WETH[exports.ChainId.OPTIMISM] = /*#__PURE__*/new Token(exports.ChainId.OPTIMISM, '0x4200000000000000000000000000000000000006', 18, 'WETH', 'Wrapped Ether'), _WETH[exports.ChainId.ZKSYNC] = /*#__PURE__*/new Token(exports.ChainId.ZKSYNC, '0x5AEa5775959fBC2557Cc8789bC1bf90A239D9a91', 18, 'WETH', 'Wrapped Ether'), _WETH[exports.ChainId.LINEA] = /*#__PURE__*/new Token(exports.ChainId.LINEA, '0xe5D7C2a44FfDDf6b295A15c148167daaAf5Cf34f', 18, 'WETH', 'Wrapped Ether'), _WETH[exports.ChainId.BASE] = /*#__PURE__*/new Token(exports.ChainId.BASE, '0x4200000000000000000000000000000000000006', 18, 'WETH', 'Wrapped Ether'), _WETH[exports.ChainId.SCROLL] = /*#__PURE__*/new Token(exports.ChainId.SCROLL, '0x5300000000000000000000000000000000000004', 18, 'WETH', 'Wrapped Ether'), _WETH[exports.ChainId.BLAST] = /*#__PURE__*/new Token(exports.ChainId.BLAST, '0x4300000000000000000000000000000000000004', 18, 'WETH', 'Wrapped Ether'), _WETH[exports.ChainId.MANTLE] = /*#__PURE__*/new Token(exports.ChainId.MANTLE, '0x78c1b0C915c4FAA5FffA6CAbf0219DA63d7f4cb8', 18, 'WMNT', 'Wrapped Mantle'), _WETH[exports.ChainId.SONIC] = /*#__PURE__*/new Token(exports.ChainId.SONIC, '0x039e2fB66102314Ce7b64Ce5Ce3E5183bc94aD38', 18, 'wS', 'Wrapped S'), _WETH[exports.ChainId.BERA] = /*#__PURE__*/new Token(exports.ChainId.BERA, '0x6969696969696969696969696969696969696969', 18, 'wBera', 'Wrapped Bera'), _WETH[exports.ChainId.RONIN] = /*#__PURE__*/new Token(exports.ChainId.RONIN, '0xe514d9deb7966c8be0ca922de8a064264ea6bcd4', 18, 'WRON', 'Wrapped Ronin'), _WETH[exports.ChainId.UNICHAIN] = /*#__PURE__*/new Token(exports.ChainId.UNICHAIN, '0x4200000000000000000000000000000000000006', 18, 'WETH', 'Wrapped ETH'), _WETH[exports.ChainId.HYPEREVM] = /*#__PURE__*/new Token(exports.ChainId.HYPEREVM, '0x5555555555555555555555555555555555555555', 18, 'WHYPE', 'Wrapped HYPE'), _WETH); /** * Represents the native currency of the chain on which it resides, e.g. */ var NativeCurrency = /*#__PURE__*/function (_BaseCurrency) { function NativeCurrency(chainId, decimals, symbol, name) { var _this; _this = _BaseCurrency.call(this, chainId, decimals, symbol, name) || this; _this.isNative = true; _this.isToken = false; return _this; } _inheritsLoose(NativeCurrency, _BaseCurrency); var _proto = NativeCurrency.prototype; _proto.equals = function equals(other) { return other.isNative && other.chainId === this.chainId; }; return _createClass(NativeCurrency, [{ key: "wrapped", get: function get() { return WETH[this.chainId]; } }]); }(BaseCurrency); /** * Ether is the main usage of a 'native' currency, i.e. for Ethereum mainnet and all testnets */ var Ether = /*#__PURE__*/function (_NativeCurrency) { function Ether(chainId) { return _NativeCurrency.call(this, chainId, 18, 'ETH', 'Ether') || this; } _inheritsLoose(Ether, _NativeCurrency); Ether.onChain = function onChain(chainId) { var _this$_etherCache$cha; return (_this$_etherCache$cha = this._etherCache[chainId]) != null ? _this$_etherCache$cha : this._etherCache[chainId] = new Ether(chainId); }; var _proto = Ether.prototype; _proto.equals = function equals(other) { return other.isNative && other.chainId === this.chainId; }; return _createClass(Ether, [{ key: "wrapped", get: function get() { var weth9 = WETH[this.chainId]; !!!weth9 ? invariant(false, 'WRAPPED') : void 0; return weth9; } }]); }(NativeCurrency); Ether._etherCache = {}; /** * Returns the percent difference between the mid price and the execution price, i.e. price impact. * @param midPrice mid price before the trade * @param inputAmount the input amount of the trade * @param outputAmount the output amount of the trade */ function computePriceImpact(midPrice, inputAmount, outputAmount) { var quotedOutputAmount = midPrice.quote(inputAmount); // calculate price impact := (exactQuote - outputAmount) / exactQuote var priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount); return new Percent(priceImpact.numerator, priceImpact.denominator); } // given an array of items sorted by `comparator`, insert an item into its sort index and constrain the size to // `maxSize` by removing the last item function sortedInsert(items, add, maxSize, comparator) { !(maxSize > 0) ? invariant(false, 'MAX_SIZE_ZERO') : void 0; // this is an invariant because the interface cannot return multiple removed items if items.length exceeds maxSize !(items.length <= maxSize) ? invariant(false, 'ITEMS_SIZE') : void 0; // short circuit first item add if (items.length === 0) { items.push(add); return null; } else { var isFull = items.length === maxSize; // short circuit if full and the additional item does not come before the last item if (isFull && comparator(items[items.length - 1], add) <= 0) { return add; } var lo = 0, hi = items.length; while (lo < hi) { var mid = lo + hi >>> 1; if (comparator(items[mid], add) <= 0) { lo = mid + 1; } else { hi = mid; } } items.splice(lo, 0, add); return isFull ? items.pop() : null; } } var MAX_SAFE_INTEGER = /*#__PURE__*/JSBI.BigInt(Number.MAX_SAFE_INTEGER); var ZERO = /*#__PURE__*/JSBI.BigInt(0); var ONE = /*#__PURE__*/JSBI.BigInt(1); var TWO = /*#__PURE__*/JSBI.BigInt(2); /** * Computes floor(sqrt(value)) * @param value the value for which to compute the square root, rounded down */ function sqrt(value) { !JSBI.greaterThanOrEqual(value, ZERO) ? invariant(false, 'NEGATIVE') : void 0; // rely on built in sqrt if possible if (JSBI.lessThan(value, MAX_SAFE_INTEGER)) { return JSBI.BigInt(Math.floor(Math.sqrt(JSBI.toNumber(value)))); } var z; var x; z = value; x = JSBI.add(JSBI.divide(value, TWO), ONE); while (JSBI.lessThan(x, z)) { z = x; x = JSBI.divide(JSBI.add(JSBI.divide(value, x), x), TWO); } return z; } exports.CurrencyAmount = CurrencyAmount; exports.Ether = Ether; exports.Fraction = Fraction; exports.MaxUint256 = MaxUint256; exports.NativeCurrency = NativeCurrency; exports.Percent = Percent; exports.Price = Price; exports.Token = Token; exports.TokenAmount = TokenAmount; exports.WETH = WETH; exports.computePriceImpact = computePriceImpact; exports.sortedInsert = sortedInsert; exports.sqrt = sqrt; exports.validateAndParseAddress = validateAndParseAddress; //# sourceMappingURL=ks-sdk-core.cjs.development.js.map