@uniswap/universal-router-sdk 1.5.6 → 1.5.7

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.
@@ -3,5 +3,6 @@ export declare const FORGE_ROUTER_ADDRESS = "0xe808c1cfeebb6cb36b537b82fa7c9eef3
3
3
  export declare const FORGE_PERMIT2_ADDRESS = "0x4a873bdd49f7f9cc0a5458416a12973fab208f8d";
4
4
  export declare const FORGE_SENDER_ADDRESS = "0xcf03dd0a894ef79cb5b601a43c4b25e3ae4c67ed";
5
5
  export declare const TEST_RECIPIENT_ADDRESS = "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
6
+ export declare const TEST_FEE_RECIPIENT_ADDRESS = "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
6
7
  export declare const PERMIT2_ADDRESS: string;
7
8
  export declare const ROUTER_ADDRESS: string;
@@ -8,12 +8,13 @@ var invariant = _interopDefault(require('tiny-invariant'));
8
8
  var UniversalRouter_json = require('@uniswap/universal-router/artifacts/contracts/UniversalRouter.sol/UniversalRouter.json');
9
9
  var abi$7 = require('@ethersproject/abi');
10
10
  var ethers = require('ethers');
11
- var JSBI = _interopDefault(require('jsbi'));
12
11
  var utils = require('ethers/lib/utils');
13
12
  var v2Sdk = require('@uniswap/v2-sdk');
14
13
  var v3Sdk = require('@uniswap/v3-sdk');
15
14
  var routerSdk = require('@uniswap/router-sdk');
16
15
  var sdkCore = require('@uniswap/sdk-core');
16
+ require('jsbi');
17
+ require('bignumber.js');
17
18
 
18
19
  function _extends() {
19
20
  _extends = Object.assign ? Object.assign.bind() : function (target) {
@@ -269,7 +270,11 @@ var ROUTER_AS_RECIPIENT = '0x0000000000000000000000000000000000000002';
269
270
  var OPENSEA_CONDUIT_SPENDER_ID = 0;
270
271
  var SUDOSWAP_SPENDER_ID = 1;
271
272
 
272
- var REFUND_ETH_PRICE_IMPACT_THRESHOLD = /*#__PURE__*/new sdkCore.Percent( /*#__PURE__*/JSBI.BigInt(50), /*#__PURE__*/JSBI.BigInt(100));
273
+ function encodeFeeBips(fee) {
274
+ return v3Sdk.toHex(fee.multiply(10000).quotient);
275
+ }
276
+
277
+ var REFUND_ETH_PRICE_IMPACT_THRESHOLD = /*#__PURE__*/new sdkCore.Percent(50, 100);
273
278
  // Wrapper for uniswap router-sdk trade entity to encode swaps for Universal Router
274
279
  // also translates trade objects from previous (v2, v3) SDKs
275
280
  var UniswapTrade = /*#__PURE__*/function () {
@@ -282,12 +287,14 @@ var UniswapTrade = /*#__PURE__*/function () {
282
287
  _proto.encode = function encode(planner, _config) {
283
288
  var _this$options$recipie;
284
289
  var payerIsUser = true;
290
+ // If the input currency is the native currency, we need to wrap it with the router as the recipient
285
291
  if (this.trade.inputAmount.currency.isNative) {
286
292
  // TODO: optimize if only one v2 pool we can directly send this to the pool
287
293
  planner.addCommand(CommandType.WRAP_ETH, [ROUTER_AS_RECIPIENT, this.trade.maximumAmountIn(this.options.slippageTolerance).quotient.toString()]);
288
294
  // since WETH is now owned by the router, the router pays for inputs
289
295
  payerIsUser = false;
290
296
  }
297
+ // The overall recipient at the end of the trade, SENDER_AS_RECIPIENT uses the msg.sender
291
298
  this.options.recipient = (_this$options$recipie = this.options.recipient) != null ? _this$options$recipie : SENDER_AS_RECIPIENT;
292
299
  // flag for whether we want to perform slippage check on aggregate output of multiple routes
293
300
  // 1. when there are >2 exact input trades. this is only a heuristic,
@@ -296,7 +303,7 @@ var UniswapTrade = /*#__PURE__*/function () {
296
303
  var performAggregatedSlippageCheck = this.trade.tradeType === sdkCore.TradeType.EXACT_INPUT && this.trade.routes.length > 2;
297
304
  var outputIsNative = this.trade.outputAmount.currency.isNative;
298
305
  var inputIsNative = this.trade.inputAmount.currency.isNative;
299
- var routerMustCustody = performAggregatedSlippageCheck || outputIsNative;
306
+ var routerMustCustody = performAggregatedSlippageCheck || outputIsNative || !!this.options.fee;
300
307
  for (var _iterator = _createForOfIteratorHelperLoose(this.trade.swaps), _step; !(_step = _iterator()).done;) {
301
308
  var swap = _step.value;
302
309
  switch (swap.route.protocol) {
@@ -313,11 +320,26 @@ var UniswapTrade = /*#__PURE__*/function () {
313
320
  throw new Error('UNSUPPORTED_TRADE_PROTOCOL');
314
321
  }
315
322
  }
323
+ var minimumAmountOut = ethers.BigNumber.from(this.trade.minimumAmountOut(this.options.slippageTolerance).quotient.toString());
324
+ // The router custodies for 3 reasons: to unwrap, to take a fee, and/or to do a slippage check
316
325
  if (routerMustCustody) {
326
+ // If there is a fee, that percentage is sent to the fee recipient
327
+ // In the case where ETH is the output currency, the fee is taken in WETH (for gas reasons)
328
+ if (!!this.options.fee) {
329
+ var feeBips = encodeFeeBips(this.options.fee.fee);
330
+ planner.addCommand(CommandType.PAY_PORTION, [this.trade.outputAmount.currency.wrapped.address, this.options.fee.recipient, feeBips]);
331
+ // If the trade is exact output, and a fee was taken, we must adjust the amount out to be the amount after the fee
332
+ // Otherwise we continue as expected with the trade's normal expected output
333
+ if (this.trade.tradeType === sdkCore.TradeType.EXACT_OUTPUT) {
334
+ minimumAmountOut = minimumAmountOut.sub(minimumAmountOut.mul(feeBips).div(10000));
335
+ }
336
+ }
337
+ // The remaining tokens that need to be sent to the user after the fee is taken will be caught
338
+ // by this if-else clause.
317
339
  if (outputIsNative) {
318
- planner.addCommand(CommandType.UNWRAP_WETH, [this.options.recipient, this.trade.minimumAmountOut(this.options.slippageTolerance).quotient.toString()]);
340
+ planner.addCommand(CommandType.UNWRAP_WETH, [this.options.recipient, minimumAmountOut]);
319
341
  } else {
320
- planner.addCommand(CommandType.SWEEP, [this.trade.outputAmount.currency.wrapped.address, this.options.recipient, this.trade.minimumAmountOut(this.options.slippageTolerance).quotient.toString()]);
342
+ planner.addCommand(CommandType.SWEEP, [this.trade.outputAmount.currency.wrapped.address, this.options.recipient, minimumAmountOut]);
321
343
  }
322
344
  }
323
345
  if (inputIsNative && (this.trade.tradeType === sdkCore.TradeType.EXACT_OUTPUT || riskOfPartialFill(this.trade))) {