UNPKG

@diswap/v2-sdk

Version:

🛠 An SDK for building applications on top of Uniswap V2

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