@uniswap/universal-router-sdk 1.5.5 → 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.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { SwapRouter } from './swapRouter';
2
2
  export * from './entities';
3
- export { UNIVERSAL_ROUTER_ADDRESS, PERMIT2_ADDRESS, ROUTER_AS_RECIPIENT, WETH_ADDRESS } from './utils/constants';
3
+ export { UNIVERSAL_ROUTER_ADDRESS, UNIVERSAL_ROUTER_CREATION_BLOCK, PERMIT2_ADDRESS, ROUTER_AS_RECIPIENT, WETH_ADDRESS, } from './utils/constants';
@@ -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) {
@@ -251,6 +252,10 @@ var UNIVERSAL_ROUTER_ADDRESS = function UNIVERSAL_ROUTER_ADDRESS(chainId) {
251
252
  if (!(chainId in CHAIN_CONFIGS)) throw new Error("Universal Router not deployed on chain " + chainId);
252
253
  return CHAIN_CONFIGS[chainId].router;
253
254
  };
255
+ var UNIVERSAL_ROUTER_CREATION_BLOCK = function UNIVERSAL_ROUTER_CREATION_BLOCK(chainId) {
256
+ if (!(chainId in CHAIN_CONFIGS)) throw new Error("Universal Router not deployed on chain " + chainId);
257
+ return CHAIN_CONFIGS[chainId].creationBlock;
258
+ };
254
259
  var WETH_ADDRESS = function WETH_ADDRESS(chainId) {
255
260
  if (!(chainId in CHAIN_CONFIGS)) throw new Error("Universal Router not deployed on chain " + chainId);
256
261
  if (CHAIN_CONFIGS[chainId].weth == WETH_NOT_SUPPORTED_ON_CHAIN) throw new Error("Chain " + chainId + " does not have WETH");
@@ -265,7 +270,11 @@ var ROUTER_AS_RECIPIENT = '0x0000000000000000000000000000000000000002';
265
270
  var OPENSEA_CONDUIT_SPENDER_ID = 0;
266
271
  var SUDOSWAP_SPENDER_ID = 1;
267
272
 
268
- 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);
269
278
  // Wrapper for uniswap router-sdk trade entity to encode swaps for Universal Router
270
279
  // also translates trade objects from previous (v2, v3) SDKs
271
280
  var UniswapTrade = /*#__PURE__*/function () {
@@ -278,12 +287,14 @@ var UniswapTrade = /*#__PURE__*/function () {
278
287
  _proto.encode = function encode(planner, _config) {
279
288
  var _this$options$recipie;
280
289
  var payerIsUser = true;
290
+ // If the input currency is the native currency, we need to wrap it with the router as the recipient
281
291
  if (this.trade.inputAmount.currency.isNative) {
282
292
  // TODO: optimize if only one v2 pool we can directly send this to the pool
283
293
  planner.addCommand(CommandType.WRAP_ETH, [ROUTER_AS_RECIPIENT, this.trade.maximumAmountIn(this.options.slippageTolerance).quotient.toString()]);
284
294
  // since WETH is now owned by the router, the router pays for inputs
285
295
  payerIsUser = false;
286
296
  }
297
+ // The overall recipient at the end of the trade, SENDER_AS_RECIPIENT uses the msg.sender
287
298
  this.options.recipient = (_this$options$recipie = this.options.recipient) != null ? _this$options$recipie : SENDER_AS_RECIPIENT;
288
299
  // flag for whether we want to perform slippage check on aggregate output of multiple routes
289
300
  // 1. when there are >2 exact input trades. this is only a heuristic,
@@ -292,7 +303,7 @@ var UniswapTrade = /*#__PURE__*/function () {
292
303
  var performAggregatedSlippageCheck = this.trade.tradeType === sdkCore.TradeType.EXACT_INPUT && this.trade.routes.length > 2;
293
304
  var outputIsNative = this.trade.outputAmount.currency.isNative;
294
305
  var inputIsNative = this.trade.inputAmount.currency.isNative;
295
- var routerMustCustody = performAggregatedSlippageCheck || outputIsNative;
306
+ var routerMustCustody = performAggregatedSlippageCheck || outputIsNative || !!this.options.fee;
296
307
  for (var _iterator = _createForOfIteratorHelperLoose(this.trade.swaps), _step; !(_step = _iterator()).done;) {
297
308
  var swap = _step.value;
298
309
  switch (swap.route.protocol) {
@@ -309,11 +320,26 @@ var UniswapTrade = /*#__PURE__*/function () {
309
320
  throw new Error('UNSUPPORTED_TRADE_PROTOCOL');
310
321
  }
311
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
312
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.
313
339
  if (outputIsNative) {
314
- 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]);
315
341
  } else {
316
- 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]);
317
343
  }
318
344
  }
319
345
  if (inputIsNative && (this.trade.tradeType === sdkCore.TradeType.EXACT_OUTPUT || riskOfPartialFill(this.trade))) {
@@ -10517,6 +10543,7 @@ exports.SeaportTrade = SeaportTrade;
10517
10543
  exports.SudoswapTrade = SudoswapTrade;
10518
10544
  exports.SwapRouter = SwapRouter;
10519
10545
  exports.UNIVERSAL_ROUTER_ADDRESS = UNIVERSAL_ROUTER_ADDRESS;
10546
+ exports.UNIVERSAL_ROUTER_CREATION_BLOCK = UNIVERSAL_ROUTER_CREATION_BLOCK;
10520
10547
  exports.UniswapTrade = UniswapTrade;
10521
10548
  exports.UnwrapWETH = UnwrapWETH;
10522
10549
  exports.WETH_ADDRESS = WETH_ADDRESS;