@uniswap/router-sdk 1.6.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,10 @@
1
+ import { Percent, validateAndParseAddress, Price, Fraction, CurrencyAmount, TradeType, sortedInsert, WETH9 } from '@uniswap/sdk-core';
1
2
  import JSBI from 'jsbi';
2
3
  import { Interface } from '@ethersproject/abi';
3
4
  import invariant from 'tiny-invariant';
4
5
  import { abi } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json';
5
6
  import { NonfungiblePositionManager, toHex, Multicall, Payments, Pool, Route as Route$1, Trade as Trade$1, encodeRouteToPath, SelfPermit, Position } from '@uniswap/v3-sdk';
6
7
  import { abi as abi$1 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json';
7
- import { validateAndParseAddress, Price, Fraction, CurrencyAmount, TradeType, Percent, sortedInsert, WETH9 } from '@uniswap/sdk-core';
8
8
  import { abi as abi$2 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json';
9
9
  import { abi as abi$3 } from '@uniswap/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json';
10
10
  import { Pair, Route, Trade as Trade$2 } from '@uniswap/v2-sdk';
@@ -16,6 +16,8 @@ var ZERO = /*#__PURE__*/JSBI.BigInt(0);
16
16
  var ONE = /*#__PURE__*/JSBI.BigInt(1); // = 1 << 23 or 100000000000000000000000
17
17
 
18
18
  var V2_FEE_PATH_PLACEHOLDER = 8388608;
19
+ var ZERO_PERCENT = /*#__PURE__*/new Percent(ZERO);
20
+ var ONE_HUNDRED_PERCENT = /*#__PURE__*/new Percent(100, 100);
19
21
 
20
22
  var ApprovalTypes;
21
23
 
@@ -1902,6 +1904,10 @@ var Trade = /*#__PURE__*/function () {
1902
1904
  }
1903
1905
  }
1904
1906
 
1907
+ if (this.swaps.length === 0) {
1908
+ throw new Error('No routes provided when calling Trade constructor');
1909
+ }
1910
+
1905
1911
  this.tradeType = tradeType; // each route must have the same input and output currency
1906
1912
 
1907
1913
  var inputCurrency = this.swaps[0].inputAmount.currency;
@@ -2237,7 +2243,31 @@ var Trade = /*#__PURE__*/function () {
2237
2243
  return (_this$_executionPrice = this._executionPrice) != null ? _this$_executionPrice : this._executionPrice = new Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
2238
2244
  }
2239
2245
  /**
2240
- * Returns the percent difference between the route's mid price and the price impact
2246
+ * Returns the sell tax of the input token
2247
+ */
2248
+
2249
+ }, {
2250
+ key: "inputTax",
2251
+ get: function get() {
2252
+ var inputCurrency = this.inputAmount.currency;
2253
+ if (inputCurrency.isNative || !inputCurrency.wrapped.sellFeeBps) return ZERO_PERCENT;
2254
+ return new Percent(inputCurrency.wrapped.sellFeeBps.toNumber(), 10000);
2255
+ }
2256
+ /**
2257
+ * Returns the buy tax of the output token
2258
+ */
2259
+
2260
+ }, {
2261
+ key: "outputTax",
2262
+ get: function get() {
2263
+ var outputCurrency = this.outputAmount.currency;
2264
+ if (outputCurrency.isNative || !outputCurrency.wrapped.buyFeeBps) return ZERO_PERCENT;
2265
+ return new Percent(outputCurrency.wrapped.buyFeeBps.toNumber(), 10000);
2266
+ }
2267
+ /**
2268
+ * Returns the percent difference between the route's mid price and the expected execution price
2269
+ * In order to exclude token taxes from the price impact calculation, the spot price is calculated
2270
+ * using a ratio of values that go into the pools, which are the post-tax input amount and pre-tax output amount.
2241
2271
  */
2242
2272
 
2243
2273
  }, {
@@ -2245,8 +2275,11 @@ var Trade = /*#__PURE__*/function () {
2245
2275
  get: function get() {
2246
2276
  if (this._priceImpact) {
2247
2277
  return this._priceImpact;
2248
- }
2278
+ } // returns 0% price impact even though this may be inaccurate as a swap may have occured.
2279
+ // because we're unable to derive the pre-buy-tax amount, use 0% as a placeholder.
2249
2280
 
2281
+
2282
+ if (this.outputTax.equalTo(ONE_HUNDRED_PERCENT)) return ZERO_PERCENT;
2250
2283
  var spotOutputAmount = CurrencyAmount.fromRawAmount(this.outputAmount.currency, 0);
2251
2284
 
2252
2285
  for (var _iterator9 = _createForOfIteratorHelperLoose(this.swaps), _step9; !(_step9 = _iterator9()).done;) {
@@ -2254,10 +2287,15 @@ var Trade = /*#__PURE__*/function () {
2254
2287
  route = _step9$value.route,
2255
2288
  inputAmount = _step9$value.inputAmount;
2256
2289
  var midPrice = route.midPrice;
2257
- spotOutputAmount = spotOutputAmount.add(midPrice.quote(inputAmount));
2258
- }
2290
+ var postTaxInputAmount = inputAmount.multiply(new Fraction(ONE).subtract(this.inputTax));
2291
+ spotOutputAmount = spotOutputAmount.add(midPrice.quote(postTaxInputAmount));
2292
+ } // if the total output of this trade is 0, then most likely the post-tax input was also 0, and therefore this swap
2293
+ // does not move the pools' market price
2259
2294
 
2260
- var priceImpact = spotOutputAmount.subtract(this.outputAmount).divide(spotOutputAmount);
2295
+
2296
+ if (spotOutputAmount.equalTo(ZERO)) return ZERO_PERCENT;
2297
+ var preTaxOutputAmount = this.outputAmount.divide(new Fraction(ONE).subtract(this.outputTax));
2298
+ var priceImpact = spotOutputAmount.subtract(preTaxOutputAmount).divide(spotOutputAmount);
2261
2299
  this._priceImpact = new Percent(priceImpact.numerator, priceImpact.denominator);
2262
2300
  return this._priceImpact;
2263
2301
  }
@@ -2837,5 +2875,5 @@ var SwapRouter = /*#__PURE__*/function () {
2837
2875
  }();
2838
2876
  SwapRouter.INTERFACE = /*#__PURE__*/new Interface(abi$3);
2839
2877
 
2840
- export { ADDRESS_THIS, ApprovalTypes, ApproveAndCall, MSG_SENDER, MixedRoute, MixedRouteSDK, MixedRouteTrade, MulticallExtended, ONE, PaymentsExtended, Protocol, RouteV2, RouteV3, SwapRouter, Trade, V2_FEE_PATH_PLACEHOLDER, ZERO, encodeMixedRouteToPath, getOutputOfPools, isMint, partitionMixedRouteByProtocol, tradeComparator };
2878
+ export { ADDRESS_THIS, ApprovalTypes, ApproveAndCall, MSG_SENDER, MixedRoute, MixedRouteSDK, MixedRouteTrade, MulticallExtended, ONE, ONE_HUNDRED_PERCENT, PaymentsExtended, Protocol, RouteV2, RouteV3, SwapRouter, Trade, V2_FEE_PATH_PLACEHOLDER, ZERO, ZERO_PERCENT, encodeMixedRouteToPath, getOutputOfPools, isMint, partitionMixedRouteByProtocol, tradeComparator };
2841
2879
  //# sourceMappingURL=router-sdk.esm.js.map