@paraswap/dex-lib 2.15.5 → 2.15.7-friendly-local-deadline.0

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.
Files changed (69) hide show
  1. package/build/dex/curve-v1/curve-v1.js +6 -3
  2. package/build/dex/curve-v1/curve-v1.js.map +1 -1
  3. package/build/dex/curve-v1/pools/3pool.d.ts +2 -0
  4. package/build/dex/curve-v1/pools/3pool.js +21 -1
  5. package/build/dex/curve-v1/pools/3pool.js.map +1 -1
  6. package/build/dex/curve-v1/pools/curve-pool.js.map +1 -1
  7. package/build/dex/curve-v1/pools/sUSDpool.js +1 -1
  8. package/build/dex/curve-v1/pools/sUSDpool.js.map +1 -1
  9. package/build/dex/maverick-v1/maverick-v1.js +4 -4
  10. package/build/dex/maverick-v1/maverick-v1.js.map +1 -1
  11. package/build/dex/maverick-v1/types.d.ts +2 -3
  12. package/build/dex/maverick-v1/types.js.map +1 -1
  13. package/build/dex/platypus/platypus.js +3 -3
  14. package/build/dex/platypus/platypus.js.map +1 -1
  15. package/build/dex/shell.d.ts +1 -1
  16. package/build/dex/shell.js +1 -1
  17. package/build/dex/shell.js.map +1 -1
  18. package/build/dex/simple-exchange.d.ts +2 -1
  19. package/build/dex/simple-exchange.js +12 -4
  20. package/build/dex/simple-exchange.js.map +1 -1
  21. package/build/dex/trader-joe-v2.d.ts +1 -2
  22. package/build/dex/trader-joe-v2.js +3 -3
  23. package/build/dex/trader-joe-v2.js.map +1 -1
  24. package/build/dex/uniswap-v3/types.d.ts +2 -2
  25. package/build/dex/uniswap-v3/uniswap-v3.js +3 -3
  26. package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
  27. package/build/dex/uniswap-v3.d.ts +2 -3
  28. package/build/dex/uniswap-v3.js +5 -5
  29. package/build/dex/uniswap-v3.js.map +1 -1
  30. package/build/transaction-builder.js +7 -0
  31. package/build/transaction-builder.js.map +1 -1
  32. package/package.json +1 -1
  33. package/src/dex/curve-v1/curve-v1.ts +10 -3
  34. package/src/dex/curve-v1/pools/3pool.ts +40 -2
  35. package/src/dex/curve-v1/pools/curve-pool.ts +1 -1
  36. package/src/dex/curve-v1/pools/sUSDpool.ts +1 -1
  37. package/src/dex/maverick-v1/maverick-v1.ts +8 -5
  38. package/src/dex/maverick-v1/types.ts +2 -3
  39. package/src/dex/platypus/platypus.ts +7 -4
  40. package/src/dex/shell.ts +6 -4
  41. package/src/dex/simple-exchange.ts +12 -4
  42. package/src/dex/trader-joe-v2.ts +8 -6
  43. package/src/dex/uniswap-v3/types.ts +2 -2
  44. package/src/dex/uniswap-v3/uniswap-v3.ts +7 -4
  45. package/src/dex/uniswap-v3.ts +11 -9
  46. package/src/transaction-builder.ts +12 -0
  47. package/build/abi/maverick/pool.json +0 -913
  48. package/build/abi/maverick/router.json +0 -703
  49. package/build/dex/maverick/config.d.ts +0 -4
  50. package/build/dex/maverick/config.js +0 -20
  51. package/build/dex/maverick/config.js.map +0 -1
  52. package/build/dex/maverick/maverick-math/maverick-pool-math.d.ts +0 -67
  53. package/build/dex/maverick/maverick-math/maverick-pool-math.js +0 -406
  54. package/build/dex/maverick/maverick-math/maverick-pool-math.js.map +0 -1
  55. package/build/dex/maverick/maverick-math/maverick-sol-math.d.ts +0 -17
  56. package/build/dex/maverick/maverick-math/maverick-sol-math.js +0 -139
  57. package/build/dex/maverick/maverick-math/maverick-sol-math.js.map +0 -1
  58. package/build/dex/maverick/maverick-pool.d.ts +0 -41
  59. package/build/dex/maverick/maverick-pool.js +0 -130
  60. package/build/dex/maverick/maverick-pool.js.map +0 -1
  61. package/build/dex/maverick/maverick.d.ts +0 -42
  62. package/build/dex/maverick/maverick.js +0 -174
  63. package/build/dex/maverick/maverick.js.map +0 -1
  64. package/build/dex/maverick/subgraph-queries.d.ts +0 -4
  65. package/build/dex/maverick/subgraph-queries.js +0 -114
  66. package/build/dex/maverick/subgraph-queries.js.map +0 -1
  67. package/build/dex/maverick/types.d.ts +0 -46
  68. package/build/dex/maverick/types.js +0 -3
  69. package/build/dex/maverick/types.js.map +0 -1
package/src/dex/shell.ts CHANGED
@@ -2,9 +2,11 @@ import { Interface, JsonFragment } from '@ethersproject/abi';
2
2
  import { SwapSide } from '../constants';
3
3
  import { AdapterExchangeParam, Address, SimpleExchangeParam } from '../types';
4
4
  import { IDexTxBuilder } from './idex';
5
- import { SimpleExchange } from './simple-exchange';
5
+ import {
6
+ getLocalDeadlineAsFriendlyPlaceholder,
7
+ SimpleExchange,
8
+ } from './simple-exchange';
6
9
  import ShellABI from '../abi/Shell.json';
7
- import Web3 from 'web3';
8
10
  import { IDexHelper } from '../dex-helper';
9
11
 
10
12
  export type ShellData = {
@@ -16,7 +18,7 @@ type ShellParam = [
16
18
  _target: string,
17
19
  _originAmount: string,
18
20
  _minTargetAmount: string,
19
- _deadline?: number,
21
+ _deadline: string,
20
22
  ];
21
23
  enum ShellFunctions {
22
24
  originSwap = 'originSwap',
@@ -63,7 +65,7 @@ export class Shell
63
65
  destToken,
64
66
  srcAmount,
65
67
  destAmount,
66
- data.deadline || this.getDeadline(),
68
+ getLocalDeadlineAsFriendlyPlaceholder(),
67
69
  ];
68
70
  const swapData = this.exchangeRouterInterface.encodeFunctionData(
69
71
  swapFunction,
@@ -10,6 +10,18 @@ import { MAX_UINT } from '../constants';
10
10
  import Web3 from 'web3';
11
11
  import { IDexHelper } from '../dex-helper';
12
12
 
13
+ /*
14
+ * Context: Augustus routers have all a deadline protection logic implemented globally.
15
+ * But some integrations (router, pools,...) require passing a deadline generally as uint256.
16
+ * While this problem can be solved in adapters easily by passing block.timestamp (or block.timestamp +1 for some marginal cases),
17
+ * In the context of direct calls like simpleSwap we have to generate this value offchain.
18
+ * One can naively pick type(uint).max but that would impose a higher gas cost on the calldata.
19
+ * Here we decide to go with a high enough default so that the local deadline rarely supersedes the global router deadline.
20
+ */
21
+ export const FRIENDLY_LOCAL_DEADLINE = 7 * 24 * 60 * 60;
22
+ export const getLocalDeadlineAsFriendlyPlaceholder = () =>
23
+ String(Math.floor(new Date().getTime() / 1000) + FRIENDLY_LOCAL_DEADLINE);
24
+
13
25
  export class SimpleExchange {
14
26
  simpleSwapHelper: Interface;
15
27
  protected abiCoder: AbiCoder;
@@ -122,8 +134,4 @@ export class SimpleExchange {
122
134
  networkFee,
123
135
  };
124
136
  }
125
-
126
- getDeadline() {
127
- return Math.floor(new Date().getTime() / 1000) + 60 * 60;
128
- }
129
137
  }
@@ -2,7 +2,10 @@ import { Network, SwapSide } from '../constants';
2
2
  import { AdapterExchangeParam, Address, SimpleExchangeParam } from '../types';
3
3
  import { IDexTxBuilder } from './idex';
4
4
  import { IDexHelper } from '../dex-helper';
5
- import { SimpleExchange } from './simple-exchange';
5
+ import {
6
+ getLocalDeadlineAsFriendlyPlaceholder,
7
+ SimpleExchange,
8
+ } from './simple-exchange';
6
9
  import { NumberAsString } from '@paraswap/core';
7
10
  import { AsyncOrSync } from 'ts-essentials';
8
11
  import { Interface, JsonFragment } from '@ethersproject/abi';
@@ -29,7 +32,7 @@ type TraderJoeV2RouterBuyParams = [
29
32
  _pairBinSteps: NumberAsString[],
30
33
  _tokenPath: Address[],
31
34
  to: Address,
32
- _deadline: number,
35
+ _deadline: string,
33
36
  ];
34
37
 
35
38
  type TraderJoeV2RouterParam =
@@ -37,7 +40,6 @@ type TraderJoeV2RouterParam =
37
40
  | TraderJoeV2RouterBuyParams;
38
41
 
39
42
  export type TraderJoeV2Data = {
40
- deadline?: number;
41
43
  tokenIn: string; // redundant
42
44
  tokenOut: string; // redundant
43
45
  binStep: string;
@@ -87,7 +89,7 @@ export class TraderJoeV2
87
89
  {
88
90
  _pairBinSteps: [data.binStep],
89
91
  _tokenPath: [data.tokenIn, data.tokenOut], // FIXME: redundant, shoot & read from contract
90
- _deadline: data.deadline || this.getDeadline(),
92
+ _deadline: getLocalDeadlineAsFriendlyPlaceholder(), // FIXME: more gas efficient to pass block.timestamp in adapter
91
93
  },
92
94
  );
93
95
 
@@ -119,7 +121,7 @@ export class TraderJoeV2
119
121
  [data.binStep],
120
122
  [srcToken, destToken],
121
123
  this.augustusAddress,
122
- data.deadline || this.getDeadline(),
124
+ getLocalDeadlineAsFriendlyPlaceholder(),
123
125
  ]
124
126
  : [
125
127
  destAmount,
@@ -127,7 +129,7 @@ export class TraderJoeV2
127
129
  [data.binStep],
128
130
  [srcToken, destToken],
129
131
  this.augustusAddress,
130
- data.deadline || this.getDeadline(),
132
+ getLocalDeadlineAsFriendlyPlaceholder(),
131
133
  ];
132
134
  const swapData = this.exchangeRouterInterface.encodeFunctionData(
133
135
  swapFunction,
@@ -72,7 +72,7 @@ export type DexParams = {
72
72
  export type UniswapV3SellParam = {
73
73
  path: string;
74
74
  recipient: Address;
75
- deadline: number;
75
+ deadline: string;
76
76
  amountIn: NumberAsString;
77
77
  amountOutMinimum: NumberAsString;
78
78
  };
@@ -80,7 +80,7 @@ export type UniswapV3SellParam = {
80
80
  export type UniswapV3BuyParam = {
81
81
  path: string;
82
82
  recipient: Address;
83
- deadline: number;
83
+ deadline: string;
84
84
  amountOut: NumberAsString;
85
85
  amountInMaximum: NumberAsString;
86
86
  };
@@ -30,7 +30,10 @@ import {
30
30
  UniswapV3Functions,
31
31
  UniswapV3Param,
32
32
  } from './types';
33
- import { SimpleExchange } from '../simple-exchange';
33
+ import {
34
+ getLocalDeadlineAsFriendlyPlaceholder,
35
+ SimpleExchange,
36
+ } from '../simple-exchange';
34
37
  import { UniswapV3Config, Adapters, PoolsToPreload } from './config';
35
38
  import { UniswapV3EventPool } from './uniswap-v3-pool';
36
39
  import UniswapV3RouterABI from '../../abi/uniswap-v3/UniswapV3Router.abi.json';
@@ -686,7 +689,7 @@ export class UniswapV3
686
689
  },
687
690
  {
688
691
  path,
689
- deadline: this.getDeadline(),
692
+ deadline: getLocalDeadlineAsFriendlyPlaceholder(), // FIXME: more gas efficient to pass block.timestamp in adapter
690
693
  },
691
694
  );
692
695
 
@@ -740,14 +743,14 @@ export class UniswapV3
740
743
  side === SwapSide.SELL
741
744
  ? {
742
745
  recipient: this.augustusAddress,
743
- deadline: this.getDeadline(),
746
+ deadline: getLocalDeadlineAsFriendlyPlaceholder(),
744
747
  amountIn: srcAmount,
745
748
  amountOutMinimum: destAmount,
746
749
  path,
747
750
  }
748
751
  : {
749
752
  recipient: this.augustusAddress,
750
- deadline: this.getDeadline(),
753
+ deadline: getLocalDeadlineAsFriendlyPlaceholder(),
751
754
  amountOut: destAmount,
752
755
  amountInMaximum: srcAmount,
753
756
  path,
@@ -3,7 +3,10 @@ import { pack } from '@ethersproject/solidity';
3
3
  import { Network, SwapSide } from '../constants';
4
4
  import { AdapterExchangeParam, Address, SimpleExchangeParam } from '../types';
5
5
  import { IDexTxBuilder } from './idex';
6
- import { SimpleExchange } from './simple-exchange';
6
+ import {
7
+ getLocalDeadlineAsFriendlyPlaceholder,
8
+ SimpleExchange,
9
+ } from './simple-exchange';
7
10
  import UniswapV3RouterABI from '../abi/UniswapV3Router.json';
8
11
  import { NumberAsString } from '@paraswap/core';
9
12
  import { IDexHelper } from '../dex-helper';
@@ -17,7 +20,6 @@ const UNISWAP_V3_ROUTER_ADDRESSES: { [network: number]: Address } = {
17
20
 
18
21
  export type UniswapV3Data = {
19
22
  // ExactInputSingleParams
20
- deadline?: number;
21
23
  path: {
22
24
  tokenIn: Address;
23
25
  tokenOut: Address;
@@ -28,7 +30,7 @@ export type UniswapV3Data = {
28
30
  type UniswapV3SellParam = {
29
31
  path: string;
30
32
  recipient: Address;
31
- deadline: number;
33
+ deadline: string;
32
34
  amountIn: NumberAsString;
33
35
  amountOutMinimum: NumberAsString;
34
36
  };
@@ -36,7 +38,7 @@ type UniswapV3SellParam = {
36
38
  type UniswapV3BuyParam = {
37
39
  path: string;
38
40
  recipient: Address;
39
- deadline: number;
41
+ deadline: string;
40
42
  amountOut: NumberAsString;
41
43
  amountInMaximum: NumberAsString;
42
44
  };
@@ -112,7 +114,7 @@ export class UniswapV3
112
114
  data: UniswapV3Data,
113
115
  side: SwapSide,
114
116
  ): AdapterExchangeParam {
115
- const { deadline, path: rawPath } = data;
117
+ const { path: rawPath } = data;
116
118
  const path = this.encodePath(rawPath, side);
117
119
  const payload = this.abiCoder.encodeParameter(
118
120
  {
@@ -123,14 +125,14 @@ export class UniswapV3
123
125
  },
124
126
  {
125
127
  path,
126
- deadline: deadline || this.getDeadline(),
128
+ deadline: getLocalDeadlineAsFriendlyPlaceholder(), // FIXME: more gas efficient to pass block.timestamp in adapter
127
129
  },
128
130
  );
129
131
 
130
132
  return {
131
133
  targetExchange: this.routerAddress,
132
134
  payload,
133
- networkFee: '0', // warning
135
+ networkFee: '0',
134
136
  };
135
137
  }
136
138
 
@@ -151,14 +153,14 @@ export class UniswapV3
151
153
  side === SwapSide.SELL
152
154
  ? {
153
155
  recipient: this.augustusAddress,
154
- deadline: data.deadline || this.getDeadline(),
156
+ deadline: getLocalDeadlineAsFriendlyPlaceholder(),
155
157
  amountIn: srcAmount,
156
158
  amountOutMinimum: destAmount,
157
159
  path,
158
160
  }
159
161
  : {
160
162
  recipient: this.augustusAddress,
161
- deadline: data.deadline || this.getDeadline(),
163
+ deadline: getLocalDeadlineAsFriendlyPlaceholder(),
162
164
  amountOut: destAmount,
163
165
  amountInMaximum: srcAmount,
164
166
  path,
@@ -2,6 +2,10 @@ import { OptimalRate, Address, Adapters } from './types';
2
2
  import { ETHER_ADDRESS, SwapSide } from './constants';
3
3
  import { RouterService } from './router';
4
4
  import { DexAdapterService } from './dex';
5
+ import {
6
+ FRIENDLY_LOCAL_DEADLINE,
7
+ getLocalDeadlineAsFriendlyPlaceholder,
8
+ } from './dex/simple-exchange';
5
9
 
6
10
  export class TransactionBuilder {
7
11
  routerService: RouterService;
@@ -43,6 +47,14 @@ export class TransactionBuilder {
43
47
  beneficiary?: Address;
44
48
  onlyParams?: boolean;
45
49
  }) {
50
+ const globalDeadline = +deadline;
51
+ if (isNaN(globalDeadline)) throw new Error('deadline should be a number');
52
+ const localDeadline = +getLocalDeadlineAsFriendlyPlaceholder();
53
+ if (globalDeadline > localDeadline)
54
+ throw new Error(
55
+ `Deadline is too high. Maximum allowed is ${FRIENDLY_LOCAL_DEADLINE} seconds`,
56
+ );
57
+
46
58
  const _beneficiary = beneficiary || userAddress;
47
59
  const { encoder, params, networkFee } = await this.routerService
48
60
  .getRouterByContractMethod(priceRoute.contractMethod)