@energiswap/v2-sdk
Version:
🛠An SDK for building applications on top of Energiswap V2
3 lines (2 loc) • 15.3 kB
JavaScript
"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@uniswap/sdk-core"),r=t(require("jsbi")),n=require("@ethersproject/address"),u=require("@ethersproject/bignumber"),o=require("@ethersproject/solidity"),i=t(require("tiny-invariant")),s=e.V2_FACTORY_ADDRESSES,a="0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f",c=r.BigInt(1e3),p=r.BigInt(0),l=r.BigInt(1),m=r.BigInt(5),f=r.BigInt(997),h=r.BigInt(1e3),d=r.BigInt(1e4),y=new e.Percent(p),v=new e.Percent(l);function A(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function T(t,e,r){if(I())return Reflect.construct.apply(null,arguments);var n=[null];n.push.apply(n,e);var u=new(t.bind.apply(t,n));return r&&O(u,r.prototype),u}function q(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,P(n.key),n)}}function k(t,e,r){return e&&q(t.prototype,e),r&&q(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function g(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return A(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?A(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function w(t){return(w=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function b(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,O(t,e)}function I(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){})))}catch(t){}return(I=function(){return!!t})()}function O(t,e){return(O=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t})(t,e)}function P(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,"string");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==typeof e?e:e+""}function x(t){var e="function"==typeof Map?new Map:void 0;return(x=function(t){if(null===t||!function(t){try{return-1!==Function.toString.call(t).indexOf("[native code]")}catch(e){return"function"==typeof t}}(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,r)}function r(){return T(t,arguments,w(this).constructor)}return r.prototype=Object.create(t.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),O(r,t)})(t)}var E="setPrototypeOf"in Object,C=function(t){function e(){var r;return(r=t.call(this)||this).isInsufficientReservesError=!0,r.name=r.constructor.name,E&&Object.setPrototypeOf(r,(this instanceof e?this.constructor:void 0).prototype),r}return b(e,t),e}(x(Error)),F=function(t){function e(){var r;return(r=t.call(this)||this).isInsufficientInputAmountError=!0,r.name=r.constructor.name,E&&Object.setPrototypeOf(r,(this instanceof e?this.constructor:void 0).prototype),r}return b(e,t),e}(x(Error)),B=function(t){var e=t.factoryAddress,r=t.tokenA,u=t.tokenB,i=r.sortsBefore(u)?[r,u]:[u,r];return n.getCreate2Address(e,o.keccak256(["bytes"],[o.pack(["address","address"],[i[0].address,i[1].address])]),a)},_=function(){function t(r,n){var u=r.currency.sortsBefore(n.currency)?[r,n]:[n,r];this.liquidityToken=new e.Token(u[0].currency.chainId,t.getAddress(u[0].currency,u[1].currency),18,"UNI-V2","Uniswap V2"),this.tokenAmounts=u}t.getAddress=function(t,e){var r,n=null!=(r=s[t.chainId])?r:"0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f";return B({factoryAddress:n,tokenA:t,tokenB:e})};var n=t.prototype;return n.involvesToken=function(t){return t.equals(this.token0)||t.equals(this.token1)},n.priceOf=function(t){return this.involvesToken(t)||i(!1),t.equals(this.token0)?this.token0Price:this.token1Price},n.reserveOf=function(t){return this.involvesToken(t)||i(!1),t.equals(this.token0)?this.reserve0:this.reserve1},n.getOutputAmount=function(n,u){if(void 0===u&&(u=!0),this.involvesToken(n.currency)||i(!1),r.equal(this.reserve0.quotient,p)||r.equal(this.reserve1.quotient,p))throw new C;var o=this.reserveOf(n.currency),s=this.reserveOf(n.currency.equals(this.token0)?this.token1:this.token0),a=u?this.derivePercentAfterSellFees(n):y,c=a.greaterThan(y)?e.CurrencyAmount.fromRawAmount(n.currency,a.multiply(n).quotient):n,l=r.multiply(c.quotient,f),m=r.multiply(l,s.quotient),d=r.add(r.multiply(o.quotient,h),l),v=e.CurrencyAmount.fromRawAmount(n.currency.equals(this.token0)?this.token1:this.token0,r.divide(m,d));if(r.equal(v.quotient,p))throw new F;var A=u?this.derivePercentAfterBuyFees(v):y,T=A.greaterThan(y)?e.CurrencyAmount.fromRawAmount(v.currency,v.multiply(A).quotient):v;if(r.equal(T.quotient,p))throw new F;return[T,new t(o.add(c),s.subtract(T))]},n.getInputAmount=function(n,u){void 0===u&&(u=!0),this.involvesToken(n.currency)||i(!1);var o=u?this.derivePercentAfterBuyFees(n):y,s=o.greaterThan(y)?e.CurrencyAmount.fromRawAmount(n.currency,r.add(n.divide(o).quotient,l)):n;if(r.equal(this.reserve0.quotient,p)||r.equal(this.reserve1.quotient,p)||r.greaterThanOrEqual(n.quotient,this.reserveOf(n.currency).quotient)||r.greaterThanOrEqual(s.quotient,this.reserveOf(n.currency).quotient))throw new C;var a=this.reserveOf(n.currency),c=this.reserveOf(n.currency.equals(this.token0)?this.token1:this.token0),m=r.multiply(r.multiply(c.quotient,s.quotient),h),d=r.multiply(r.subtract(a.quotient,s.quotient),f),v=e.CurrencyAmount.fromRawAmount(n.currency.equals(this.token0)?this.token1:this.token0,r.add(r.divide(m,d),l)),A=u?this.derivePercentAfterSellFees(v):y;return[A.greaterThan(y)?e.CurrencyAmount.fromRawAmount(v.currency,r.add(v.divide(A).quotient,l)):v,new t(c.add(v),a.subtract(n))]},n.getLiquidityMinted=function(t,n,u){t.currency.equals(this.liquidityToken)||i(!1);var o,s=n.currency.sortsBefore(u.currency)?[n,u]:[u,n];if(s[0].currency.equals(this.token0)&&s[1].currency.equals(this.token1)||i(!1),r.equal(t.quotient,p))o=r.subtract(e.sqrt(r.multiply(s[0].quotient,s[1].quotient)),c);else{var a=r.divide(r.multiply(s[0].quotient,t.quotient),this.reserve0.quotient),l=r.divide(r.multiply(s[1].quotient,t.quotient),this.reserve1.quotient);o=r.lessThanOrEqual(a,l)?a:l}if(!r.greaterThan(o,p))throw new F;return e.CurrencyAmount.fromRawAmount(this.liquidityToken,o)},n.getLiquidityValue=function(t,n,u,o,s){var a;if(void 0===o&&(o=!1),this.involvesToken(t)||i(!1),n.currency.equals(this.liquidityToken)||i(!1),u.currency.equals(this.liquidityToken)||i(!1),r.lessThanOrEqual(u.quotient,n.quotient)||i(!1),o){s||i(!1);var c=r.BigInt(s);if(r.equal(c,p))a=n;else{var l=e.sqrt(r.multiply(this.reserve0.quotient,this.reserve1.quotient)),f=e.sqrt(c);if(r.greaterThan(l,f)){var h=r.multiply(n.quotient,r.subtract(l,f)),d=r.add(r.multiply(l,m),f),y=r.divide(h,d);a=n.add(e.CurrencyAmount.fromRawAmount(this.liquidityToken,y))}else a=n}}else a=n;return e.CurrencyAmount.fromRawAmount(t,r.divide(r.multiply(u.quotient,this.reserveOf(t).quotient),a.quotient))},n.derivePercentAfterSellFees=function(t){var n=this.token0.wrapped.equals(t.wrapped.currency)?this.token0.wrapped.sellFeeBps:this.token1.wrapped.sellFeeBps;return null!=n&&n.gt(u.BigNumber.from(0))?v.subtract(new e.Percent(r.BigInt(n)).divide(d)):y},n.derivePercentAfterBuyFees=function(t){var n=this.token0.wrapped.equals(t.wrapped.currency)?this.token0.wrapped.buyFeeBps:this.token1.wrapped.buyFeeBps;return null!=n&&n.gt(u.BigNumber.from(0))?v.subtract(new e.Percent(r.BigInt(n)).divide(d)):y},k(t,[{key:"token0Price",get:function(){var t=this.tokenAmounts[1].divide(this.tokenAmounts[0]);return new e.Price(this.token0,this.token1,t.denominator,t.numerator)}},{key:"token1Price",get:function(){var t=this.tokenAmounts[0].divide(this.tokenAmounts[1]);return new e.Price(this.token1,this.token0,t.denominator,t.numerator)}},{key:"chainId",get:function(){return this.token0.chainId}},{key:"token0",get:function(){return this.tokenAmounts[0].currency}},{key:"token1",get:function(){return this.tokenAmounts[1].currency}},{key:"reserve0",get:function(){return this.tokenAmounts[0]}},{key:"reserve1",get:function(){return this.tokenAmounts[1]}}])}(),R=function(){return k((function(t,e,r){this._midPrice=null,t.length>0||i(!1);var n=t[0].chainId;t.every((function(t){return t.chainId===n}))||i(!1);var u=e.wrapped;t[0].involvesToken(u)||i(!1),void 0===r||t[t.length-1].involvesToken(r.wrapped)||i(!1);for(var o,s=[u],a=g(t.entries());!(o=a()).done;){var c=o.value,p=c[1],l=s[c[0]];l.equals(p.token0)||l.equals(p.token1)||i(!1);var m=l.equals(p.token0)?p.token1:p.token0;s.push(m)}this.pairs=t,this.path=s,this.input=e,this.output=r}),[{key:"midPrice",get:function(){if(null!==this._midPrice)return this._midPrice;for(var t,r=[],n=g(this.pairs.entries());!(t=n()).done;){var u=t.value,o=u[1];r.push(this.path[u[0]].equals(o.token0)?new e.Price(o.reserve0.currency,o.reserve1.currency,o.reserve0.quotient,o.reserve1.quotient):new e.Price(o.reserve1.currency,o.reserve0.currency,o.reserve1.quotient,o.reserve0.quotient))}var i=r.slice(1).reduce((function(t,e){return t.multiply(e)}),r[0]);return this._midPrice=new e.Price(this.input,this.output,i.denominator,i.numerator)}},{key:"chainId",get:function(){return this.pairs[0].chainId}}])}();function S(t,e){return t.inputAmount.currency.equals(e.inputAmount.currency)||i(!1),t.outputAmount.currency.equals(e.outputAmount.currency)||i(!1),t.outputAmount.equalTo(e.outputAmount)?t.inputAmount.equalTo(e.inputAmount)?0:t.inputAmount.lessThan(e.inputAmount)?-1:1:t.outputAmount.lessThan(e.outputAmount)?1:-1}function j(t,e){var r=S(t,e);return 0!==r?r:t.priceImpact.lessThan(e.priceImpact)?-1:t.priceImpact.greaterThan(e.priceImpact)?1:t.route.path.length-e.route.path.length}var U=function(){function t(t,r,n){this.route=t,this.tradeType=n;var u=new Array(t.path.length);if(n===e.TradeType.EXACT_INPUT){r.currency.equals(t.input)||i(!1),u[0]=r.wrapped;for(var o=0;o<t.path.length-1;o++){var s=t.pairs[o].getOutputAmount(u[o]);u[o+1]=s[0]}this.inputAmount=e.CurrencyAmount.fromFractionalAmount(t.input,r.numerator,r.denominator),this.outputAmount=e.CurrencyAmount.fromFractionalAmount(t.output,u[u.length-1].numerator,u[u.length-1].denominator)}else{r.currency.equals(t.output)||i(!1),u[u.length-1]=r.wrapped;for(var a=t.path.length-1;a>0;a--){var c=t.pairs[a-1].getInputAmount(u[a]);u[a-1]=c[0]}this.inputAmount=e.CurrencyAmount.fromFractionalAmount(t.input,u[0].numerator,u[0].denominator),this.outputAmount=e.CurrencyAmount.fromFractionalAmount(t.output,r.numerator,r.denominator)}this.executionPrice=new e.Price(this.inputAmount.currency,this.outputAmount.currency,this.inputAmount.quotient,this.outputAmount.quotient),this.priceImpact=e.computePriceImpact(t.midPrice,this.inputAmount,this.outputAmount)}t.exactIn=function(r,n){return new t(r,n,e.TradeType.EXACT_INPUT)},t.exactOut=function(r,n){return new t(r,n,e.TradeType.EXACT_OUTPUT)};var r=t.prototype;return r.minimumAmountOut=function(t){if(t.lessThan(p)&&i(!1),this.tradeType===e.TradeType.EXACT_OUTPUT)return this.outputAmount;var r=new e.Fraction(l).add(t).invert().multiply(this.outputAmount.quotient).quotient;return e.CurrencyAmount.fromRawAmount(this.outputAmount.currency,r)},r.maximumAmountIn=function(t){if(t.lessThan(p)&&i(!1),this.tradeType===e.TradeType.EXACT_INPUT)return this.inputAmount;var r=new e.Fraction(l).add(t).multiply(this.inputAmount.quotient).quotient;return e.CurrencyAmount.fromRawAmount(this.inputAmount.currency,r)},t.bestTradeExactIn=function(r,n,u,o,s,a,c){var l=void 0===o?{}:o,m=l.maxNumResults,f=void 0===m?3:m,h=l.maxHops,d=void 0===h?3:h;void 0===s&&(s=[]),void 0===a&&(a=n),void 0===c&&(c=[]),r.length>0||i(!1),d>0||i(!1),n===a||s.length>0||i(!1);for(var y=a.wrapped,v=u.wrapped,A=0;A<r.length;A++){var T=r[A];if((T.token0.equals(y.currency)||T.token1.equals(y.currency))&&!T.reserve0.equalTo(p)&&!T.reserve1.equalTo(p)){var q=void 0;try{q=T.getOutputAmount(y)[0]}catch(t){if(t.isInsufficientInputAmountError)continue;throw t}if(q.currency.equals(v))e.sortedInsert(c,new t(new R([].concat(s,[T]),n.currency,u),n,e.TradeType.EXACT_INPUT),f,j);else if(d>1&&r.length>1){var k=r.slice(0,A).concat(r.slice(A+1,r.length));t.bestTradeExactIn(k,n,u,{maxNumResults:f,maxHops:d-1},[].concat(s,[T]),q,c)}}}return c},r.worstExecutionPrice=function(t){return new e.Price(this.inputAmount.currency,this.outputAmount.currency,this.maximumAmountIn(t).quotient,this.minimumAmountOut(t).quotient)},t.bestTradeExactOut=function(r,n,u,o,s,a,c){var l=void 0===o?{}:o,m=l.maxNumResults,f=void 0===m?3:m,h=l.maxHops,d=void 0===h?3:h;void 0===s&&(s=[]),void 0===a&&(a=u),void 0===c&&(c=[]),r.length>0||i(!1),d>0||i(!1),u===a||s.length>0||i(!1);for(var y=a.wrapped,v=n.wrapped,A=0;A<r.length;A++){var T=r[A];if((T.token0.equals(y.currency)||T.token1.equals(y.currency))&&!T.reserve0.equalTo(p)&&!T.reserve1.equalTo(p)){var q=void 0;try{q=T.getInputAmount(y)[0]}catch(t){if(t.isInsufficientReservesError)continue;throw t}if(q.currency.equals(v))e.sortedInsert(c,new t(new R([T].concat(s),n,u.currency),u,e.TradeType.EXACT_OUTPUT),f,j);else if(d>1&&r.length>1){var k=r.slice(0,A).concat(r.slice(A+1,r.length));t.bestTradeExactOut(k,n,u,{maxNumResults:f,maxHops:d-1},[T].concat(s),q,c)}}}return c},t}();function N(t){return"0x"+t.quotient.toString(16)}var H=function(){function t(){}return t.swapCallParameters=function(t,r){var n=t.inputAmount.currency.isNative,u=t.outputAmount.currency.isNative;n&&u&&i(!1),!("ttl"in r)||r.ttl>0||i(!1);var o,s,a,c=e.validateAndParseAddress(r.recipient),p=N(t.maximumAmountIn(r.allowedSlippage)),l=N(t.minimumAmountOut(r.allowedSlippage)),m=t.route.path.map((function(t){return t.address})),f="ttl"in r?"0x"+(Math.floor((new Date).getTime()/1e3)+r.ttl).toString(16):"0x"+r.deadline.toString(16),h=Boolean(r.feeOnTransfer);switch(t.tradeType){case e.TradeType.EXACT_INPUT:n?(o=h?"swapExactETHForTokensSupportingFeeOnTransferTokens":"swapExactETHForTokens",s=[l,m,c,f],a=p):u?(o=h?"swapExactTokensForETHSupportingFeeOnTransferTokens":"swapExactTokensForETH",s=[p,l,m,c,f],a="0x0"):(o=h?"swapExactTokensForTokensSupportingFeeOnTransferTokens":"swapExactTokensForTokens",s=[p,l,m,c,f],a="0x0");break;case e.TradeType.EXACT_OUTPUT:h&&i(!1),n?(o="swapETHForExactTokens",s=[l,m,c,f],a=p):u?(o="swapTokensForExactETH",s=[l,p,m,c,f],a="0x0"):(o="swapTokensForExactTokens",s=[l,p,m,c,f],a="0x0")}return{methodName:o,args:s,value:a}},t}();exports.FACTORY_ADDRESS_MAP=s,exports.INIT_CODE_HASH=a,exports.InsufficientInputAmountError=F,exports.InsufficientReservesError=C,exports.MINIMUM_LIQUIDITY=c,exports.Pair=_,exports.Route=R,exports.Router=H,exports.Trade=U,exports.computePairAddress=B,exports.inputOutputComparator=S,exports.tradeComparator=j;
//# sourceMappingURL=v2-sdk.cjs.production.min.js.map