@paraswap/dex-lib 2.19.1 → 2.20.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 (131) hide show
  1. package/build/abi/DirectSwap.json +1018 -0
  2. package/build/dex/balancer-v2/balancer-v2.d.ts +9 -3
  3. package/build/dex/balancer-v2/balancer-v2.js +83 -0
  4. package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
  5. package/build/dex/balancer-v2/constants.d.ts +4 -0
  6. package/build/dex/balancer-v2/constants.js +6 -1
  7. package/build/dex/balancer-v2/constants.js.map +1 -1
  8. package/build/dex/balancer-v2/types.d.ts +20 -1
  9. package/build/dex/balancer-v2/types.js.map +1 -1
  10. package/build/dex/curve-v1/constants.d.ts +1 -0
  11. package/build/dex/curve-v1/constants.js +2 -1
  12. package/build/dex/curve-v1/constants.js.map +1 -1
  13. package/build/dex/curve-v1/curve-v1.d.ts +9 -3
  14. package/build/dex/curve-v1/curve-v1.js +92 -4
  15. package/build/dex/curve-v1/curve-v1.js.map +1 -1
  16. package/build/dex/curve-v1/types.d.ts +24 -0
  17. package/build/dex/curve-v1/types.js +6 -1
  18. package/build/dex/curve-v1/types.js.map +1 -1
  19. package/build/dex/curve-v1-factory/constants.d.ts +1 -0
  20. package/build/dex/curve-v1-factory/constants.js +2 -1
  21. package/build/dex/curve-v1-factory/constants.js.map +1 -1
  22. package/build/dex/curve-v1-factory/curve-v1-factory.d.ts +11 -4
  23. package/build/dex/curve-v1-factory/curve-v1-factory.js +87 -0
  24. package/build/dex/curve-v1-factory/curve-v1-factory.js.map +1 -1
  25. package/build/dex/curve-v1-factory/types.d.ts +24 -0
  26. package/build/dex/curve-v1-factory/types.js +6 -1
  27. package/build/dex/curve-v1-factory/types.js.map +1 -1
  28. package/build/dex/curve-v2.d.ts +29 -7
  29. package/build/dex/curve-v2.js +91 -1
  30. package/build/dex/curve-v2.js.map +1 -1
  31. package/build/dex/idex.d.ts +1 -1
  32. package/build/dex/pancakeswap-v3/pancakeswap-v3.d.ts +2 -2
  33. package/build/dex/pancakeswap-v3/utils.js.map +1 -1
  34. package/build/dex/simple-exchange.d.ts +2 -0
  35. package/build/dex/simple-exchange.js +1 -0
  36. package/build/dex/simple-exchange.js.map +1 -1
  37. package/build/dex/swaap-v2/swaap-v2.js +5 -1
  38. package/build/dex/swaap-v2/swaap-v2.js.map +1 -1
  39. package/build/dex/swaap-v2/utils.js.map +1 -1
  40. package/build/dex/swaap-v2/validators.js +1 -2
  41. package/build/dex/swaap-v2/validators.js.map +1 -1
  42. package/build/dex/test-dex/config.d.ts +4 -0
  43. package/build/dex/test-dex/config.js +15 -0
  44. package/build/dex/test-dex/config.js.map +1 -0
  45. package/build/dex/test-dex/test-dex-pool.d.ts +39 -0
  46. package/build/dex/test-dex/test-dex-pool.js +63 -0
  47. package/build/dex/test-dex/test-dex-pool.js.map +1 -0
  48. package/build/dex/test-dex/test-dex.d.ts +37 -0
  49. package/build/dex/test-dex/test-dex.js +126 -0
  50. package/build/dex/test-dex/test-dex.js.map +1 -0
  51. package/build/dex/test-dex/types.d.ts +6 -0
  52. package/build/dex/{pancakeswap-v3 → test-dex}/types.js.map +1 -1
  53. package/build/dex/uniswap-v2/uniswap-v2.d.ts +1 -1
  54. package/build/dex/uniswap-v2/uniswap-v2.js +1 -1
  55. package/build/dex/uniswap-v2/uniswap-v2.js.map +1 -1
  56. package/build/dex/uniswap-v3/constants.d.ts +4 -0
  57. package/build/dex/uniswap-v3/constants.js +6 -1
  58. package/build/dex/uniswap-v3/constants.js.map +1 -1
  59. package/build/dex/uniswap-v3/types.d.ts +20 -3
  60. package/build/dex/uniswap-v3/types.js.map +1 -1
  61. package/build/dex/uniswap-v3/uniswap-v3.d.ts +7 -1
  62. package/build/dex/uniswap-v3/uniswap-v3.js +85 -1
  63. package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
  64. package/build/dex/woo-fi-v2/woo-fi-math.d.ts +16 -0
  65. package/build/dex/woo-fi-v2/woo-fi-math.js +137 -0
  66. package/build/dex/woo-fi-v2/woo-fi-math.js.map +1 -0
  67. package/build/dex/zerox/index.d.ts +1 -1
  68. package/build/dex/zerox/index.js +1 -1
  69. package/build/dex/zerox/index.js.map +1 -1
  70. package/build/implementations/local-paraswap-sdk.js +5 -1
  71. package/build/implementations/local-paraswap-sdk.js.map +1 -1
  72. package/build/router/directswap.js +7 -1
  73. package/build/router/directswap.js.map +1 -1
  74. package/build/router/index.js.map +1 -1
  75. package/build/router/payload-encoder.js +14 -3
  76. package/build/router/payload-encoder.js.map +1 -1
  77. package/build/types.d.ts +1 -0
  78. package/build/utils.js.map +1 -1
  79. package/package.json +5 -3
  80. package/src/abi/DirectSwap.json +1018 -0
  81. package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +79 -0
  82. package/src/dex/balancer-v2/balancer-v2.ts +153 -4
  83. package/src/dex/balancer-v2/constants.ts +5 -0
  84. package/src/dex/balancer-v2/types.ts +21 -1
  85. package/src/dex/curve-v1/constants.ts +2 -0
  86. package/src/dex/curve-v1/curve-v1-e2e.test.ts +57 -0
  87. package/src/dex/curve-v1/curve-v1.ts +144 -5
  88. package/src/dex/curve-v1/types.ts +26 -0
  89. package/src/dex/curve-v1-factory/constants.ts +2 -0
  90. package/src/dex/curve-v1-factory/curve-v1-factory-e2e.test.ts +1 -0
  91. package/src/dex/curve-v1-factory/curve-v1-factory.ts +140 -4
  92. package/src/dex/curve-v1-factory/types.ts +26 -0
  93. package/src/dex/curve-v2.ts +169 -8
  94. package/src/dex/idex.ts +6 -0
  95. package/src/dex/pancakeswap-v3/pancakeswap-v3-e2e.test.ts +3 -6
  96. package/src/dex/pancakeswap-v3/pancakeswap-v3-integration.test.ts +3 -14
  97. package/src/dex/pancakeswap-v3/pancakeswap-v3.ts +3 -3
  98. package/src/dex/pancakeswap-v3/utils.ts +1 -4
  99. package/src/dex/simple-exchange.ts +6 -0
  100. package/src/dex/solidly/solidly-e2e.test.ts +31 -32
  101. package/src/dex/swaap-v2/swaap-v2.ts +19 -11
  102. package/src/dex/swaap-v2/types.ts +4 -4
  103. package/src/dex/swaap-v2/utils.ts +4 -4
  104. package/src/dex/swaap-v2/validators.ts +4 -7
  105. package/src/dex/uniswap-v2/uniswap-v2.ts +6 -0
  106. package/src/dex/uniswap-v3/constants.ts +5 -0
  107. package/src/dex/uniswap-v3/types.ts +23 -3
  108. package/src/dex/uniswap-v3/uniswap-v3-e2e.test.ts +100 -2
  109. package/src/dex/uniswap-v3/uniswap-v3.ts +138 -3
  110. package/src/dex/zerox/index.ts +7 -0
  111. package/src/implementations/local-paraswap-sdk.ts +8 -1
  112. package/src/router/directswap.ts +23 -0
  113. package/src/router/index.ts +0 -2
  114. package/src/router/payload-encoder.ts +19 -3
  115. package/src/types.ts +1 -0
  116. package/src/utils.ts +1 -1
  117. package/tests/constants-e2e.ts +3 -3
  118. package/tests/utils-e2e.ts +136 -21
  119. package/tests/utils.ts +52 -0
  120. package/.idea/aws.xml +0 -17
  121. package/.idea/codeStyles/Project.xml +0 -19
  122. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  123. package/.idea/misc.xml +0 -6
  124. package/.idea/modules.xml +0 -8
  125. package/.idea/paraswap-dex-lib.iml +0 -9
  126. package/.idea/vcs.xml +0 -6
  127. package/build/dex/pancakeswap-v3/types.d.ts +0 -14
  128. package/build/dex/solidly/forks-override/chronos.d.ts +0 -20
  129. package/build/dex/solidly/forks-override/chronos.js +0 -44
  130. package/build/dex/solidly/forks-override/chronos.js.map +0 -1
  131. /package/build/dex/{pancakeswap-v3 → test-dex}/types.js +0 -0
@@ -535,6 +535,85 @@ describe('BalancerV2 E2E', () => {
535
535
  */
536
536
  });
537
537
 
538
+ describe('BalancerV2 Polygon', () => {
539
+ const dexKey = 'BalancerV2';
540
+ const network = Network.POLYGON;
541
+ const tokens = Tokens[Network.POLYGON];
542
+ const holders = Holders[Network.POLYGON];
543
+ const provider = new StaticJsonRpcProvider(
544
+ generateConfig(network).privateHttpProvider,
545
+ network,
546
+ );
547
+
548
+ const sideToContractMethods = new Map([
549
+ [
550
+ SwapSide.SELL,
551
+ [
552
+ ContractMethod.simpleSwap,
553
+ ContractMethod.multiSwap,
554
+ ContractMethod.megaSwap,
555
+ ContractMethod.directBalancerV2GivenInSwap,
556
+ ],
557
+ ],
558
+ [
559
+ SwapSide.BUY,
560
+ [
561
+ ContractMethod.directBalancerV2GivenOutSwap,
562
+ ContractMethod.simpleBuy,
563
+ ContractMethod.buy,
564
+ ],
565
+ ],
566
+ ]);
567
+
568
+ const pairs: { name: string; sellAmount: string }[][] = [
569
+ [
570
+ { name: 'WMATIC', sellAmount: '1000000000000000000' },
571
+ { name: 'stMATIC', sellAmount: '1000000000000000000' },
572
+ ],
573
+ [
574
+ { name: 'MATIC', sellAmount: '1000000000000000000' },
575
+ { name: 'stMATIC', sellAmount: '1000000000000000000' },
576
+ ],
577
+ ];
578
+
579
+ sideToContractMethods.forEach((contractMethods, side) =>
580
+ describe(`${side}`, () => {
581
+ contractMethods.forEach((contractMethod: ContractMethod) => {
582
+ pairs.forEach(pair => {
583
+ describe(`${contractMethod}`, () => {
584
+ it(`${pair[0].name} -> ${pair[1].name}`, async () => {
585
+ await testE2E(
586
+ tokens[pair[0].name],
587
+ tokens[pair[1].name],
588
+ holders[pair[0].name],
589
+ pair[0].sellAmount,
590
+ side,
591
+ dexKey,
592
+ contractMethod,
593
+ network,
594
+ provider,
595
+ );
596
+ });
597
+ it(`${pair[1].name} -> ${pair[0].name}`, async () => {
598
+ await testE2E(
599
+ tokens[pair[1].name],
600
+ tokens[pair[0].name],
601
+ holders[pair[1].name],
602
+ pair[1].sellAmount,
603
+ side,
604
+ dexKey,
605
+ contractMethod,
606
+ network,
607
+ provider,
608
+ );
609
+ });
610
+ });
611
+ });
612
+ });
613
+ }),
614
+ );
615
+ });
616
+
538
617
  describe('BalancerV2 ARBITRUM', () => {
539
618
  const dexKey = 'BalancerV2';
540
619
  const network = Network.ARBITRUM;
@@ -7,6 +7,9 @@ import {
7
7
  ExchangePrices,
8
8
  Log,
9
9
  Logger,
10
+ TxInfo,
11
+ PreprocessTransactionOptions,
12
+ ExchangeTxInfo,
10
13
  PoolLiquidity,
11
14
  PoolPrices,
12
15
  SimpleExchangeParam,
@@ -27,8 +30,13 @@ import { WeightedPool } from './pools/weighted/WeightedPool';
27
30
  import { PhantomStablePool } from './pools/phantom-stable/PhantomStablePool';
28
31
  import { LinearPool } from './pools/linear/LinearPool';
29
32
  import VaultABI from '../../abi/balancer-v2/vault.json';
33
+ import DirectSwapABI from '../../abi/DirectSwap.json';
30
34
  import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
31
- import { getBigIntPow, getDexKeysWithNetwork } from '../../utils';
35
+ import {
36
+ getDexKeysWithNetwork,
37
+ getBigIntPow,
38
+ uuidToBytes16,
39
+ } from '../../utils';
32
40
  import { IDex } from '../../dex/idex';
33
41
  import { IDexHelper } from '../../dex-helper';
34
42
  import {
@@ -41,11 +49,15 @@ import {
41
49
  PoolStateCache,
42
50
  PoolStateMap,
43
51
  SubgraphPoolAddressDictionary,
52
+ BalancerV2DirectParam,
44
53
  SubgraphPoolBase,
45
54
  SwapTypes,
46
55
  } from './types';
47
- import { SimpleExchange } from '../simple-exchange';
48
- import { Adapters, BalancerConfig } from './config';
56
+ import {
57
+ getLocalDeadlineAsFriendlyPlaceholder,
58
+ SimpleExchange,
59
+ } from '../simple-exchange';
60
+ import { BalancerConfig, Adapters } from './config';
49
61
  import {
50
62
  getAllPoolsUsedInPaths,
51
63
  isSameAddress,
@@ -53,10 +65,12 @@ import {
53
65
  poolGetPathForTokenInOut,
54
66
  } from './utils';
55
67
  import {
68
+ DirectMethods,
56
69
  MIN_USD_LIQUIDITY_TO_FETCH,
57
70
  STABLE_GAS_COST,
58
71
  VARIABLE_GAS_COST_PER_CYCLE,
59
72
  } from './constants';
73
+ import { NumberAsString, OptimalSwapExchange } from '@paraswap/core';
60
74
 
61
75
  const fetchAllPools = `query ($count: Int) {
62
76
  pools: pools(
@@ -493,13 +507,16 @@ export class BalancerV2EventPool extends StatefulEventSubscriber<PoolStateMap> {
493
507
 
494
508
  export class BalancerV2
495
509
  extends SimpleExchange
496
- implements IDex<BalancerV2Data, BalancerParam, OptimizedBalancerV2Data>
510
+ implements
511
+ IDex<BalancerV2Data, BalancerV2DirectParam, OptimizedBalancerV2Data>
497
512
  {
498
513
  public eventPools: BalancerV2EventPool;
499
514
 
500
515
  readonly hasConstantPriceLargeAmounts = false;
501
516
  readonly isFeeOnTransferSupported = false;
502
517
 
518
+ readonly directSwapIface = new Interface(DirectSwapABI);
519
+
503
520
  public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
504
521
  getDexKeysWithNetwork(BalancerConfig);
505
522
 
@@ -1019,6 +1036,138 @@ export class BalancerV2
1019
1036
  return params;
1020
1037
  }
1021
1038
 
1039
+ static getDirectFunctionName(): string[] {
1040
+ return [DirectMethods.directSell, DirectMethods.directBuy];
1041
+ }
1042
+
1043
+ getTokenFromAddress(address: Address): Token {
1044
+ // In this Dex decimals are not used
1045
+ return { address, decimals: 0 };
1046
+ }
1047
+
1048
+ async preProcessTransaction(
1049
+ optimalSwapExchange: OptimalSwapExchange<OptimizedBalancerV2Data>,
1050
+ srcToken: Token,
1051
+ _0: Token,
1052
+ _1: SwapSide,
1053
+ options: PreprocessTransactionOptions,
1054
+ ): Promise<[OptimalSwapExchange<OptimizedBalancerV2Data>, ExchangeTxInfo]> {
1055
+ if (!options.isDirectMethod) {
1056
+ return [
1057
+ optimalSwapExchange,
1058
+ {
1059
+ deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
1060
+ },
1061
+ ];
1062
+ }
1063
+
1064
+ assert(
1065
+ optimalSwapExchange.data !== undefined,
1066
+ `preProcessTransaction: data field is missing`,
1067
+ );
1068
+
1069
+ let isApproved: boolean | undefined;
1070
+
1071
+ try {
1072
+ this.erc20Contract.options.address =
1073
+ this.dexHelper.config.wrapETH(srcToken).address;
1074
+ const allowance = await this.erc20Contract.methods
1075
+ .allowance(this.augustusAddress, this.vaultAddress)
1076
+ .call(undefined, 'latest');
1077
+ isApproved =
1078
+ BigInt(allowance.toString()) >= BigInt(optimalSwapExchange.srcAmount);
1079
+ } catch (e) {
1080
+ this.logger.error(
1081
+ `preProcessTransaction failed to retrieve allowance info: `,
1082
+ e,
1083
+ );
1084
+ }
1085
+
1086
+ return [
1087
+ {
1088
+ ...optimalSwapExchange,
1089
+ data: {
1090
+ ...optimalSwapExchange.data,
1091
+ isApproved,
1092
+ },
1093
+ },
1094
+ {
1095
+ deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
1096
+ },
1097
+ ];
1098
+ }
1099
+
1100
+ getDirectParam(
1101
+ srcToken: Address,
1102
+ destToken: Address,
1103
+ srcAmount: NumberAsString,
1104
+ destAmount: NumberAsString,
1105
+ expectedAmount: NumberAsString,
1106
+ data: OptimizedBalancerV2Data,
1107
+ side: SwapSide,
1108
+ permit: string,
1109
+ uuid: string,
1110
+ feePercent: NumberAsString,
1111
+ deadline: NumberAsString,
1112
+ partner: string,
1113
+ beneficiary: string,
1114
+ contractMethod?: string,
1115
+ ): TxInfo<BalancerV2DirectParam> {
1116
+ if (
1117
+ contractMethod !== DirectMethods.directSell &&
1118
+ contractMethod !== DirectMethods.directBuy
1119
+ ) {
1120
+ throw new Error(`Invalid contract method ${contractMethod}`);
1121
+ }
1122
+
1123
+ let isApproved: boolean = !!data.isApproved;
1124
+ if (data.isApproved === undefined) {
1125
+ this.logger.warn(`isApproved is undefined, defaulting to false`);
1126
+ }
1127
+
1128
+ const [, swaps, assets, funds, limits, _deadline] = this.getBalancerParam(
1129
+ srcToken,
1130
+ destToken,
1131
+ srcAmount,
1132
+ destAmount,
1133
+ data,
1134
+ side,
1135
+ );
1136
+
1137
+ const swapParams: BalancerV2DirectParam = [
1138
+ swaps,
1139
+ assets,
1140
+ funds,
1141
+ limits,
1142
+ srcAmount,
1143
+ destAmount,
1144
+ expectedAmount,
1145
+ _deadline,
1146
+ feePercent,
1147
+ this.vaultAddress,
1148
+ partner,
1149
+ isApproved,
1150
+ beneficiary,
1151
+ permit,
1152
+ uuidToBytes16(uuid),
1153
+ ];
1154
+
1155
+ const encoder = (...params: BalancerV2DirectParam) => {
1156
+ return this.directSwapIface.encodeFunctionData(
1157
+ side === SwapSide.SELL
1158
+ ? DirectMethods.directSell
1159
+ : DirectMethods.directBuy,
1160
+ [params],
1161
+ );
1162
+ };
1163
+
1164
+ return {
1165
+ params: swapParams,
1166
+ encoder,
1167
+ networkFee: '0',
1168
+ };
1169
+ }
1170
+
1022
1171
  async getSimpleParam(
1023
1172
  srcToken: string,
1024
1173
  destToken: string,
@@ -8,3 +8,8 @@ export const STABLE_GAS_COST = 84_000;
8
8
 
9
9
  // I see three pools used in trade: (57856 + 79098 + 51041) / 3 = 62665 ~ 63k
10
10
  export const VARIABLE_GAS_COST_PER_CYCLE = 63_000;
11
+
12
+ export enum DirectMethods {
13
+ directSell = 'directBalancerV2GivenInSwap',
14
+ directBuy = 'directBalancerV2GivenOutSwap',
15
+ }
@@ -1,3 +1,4 @@
1
+ import { NumberAsString } from '@paraswap/core';
1
2
  import { Address } from '../../types';
2
3
 
3
4
  // These should match the Balancer Pool types available on Subgraph
@@ -85,12 +86,13 @@ export type BalancerSwapV2 = {
85
86
 
86
87
  export type OptimizedBalancerV2Data = {
87
88
  swaps: BalancerSwapV2[];
89
+ isApproved?: boolean;
88
90
  };
89
91
 
90
92
  export type BalancerFunds = {
91
93
  sender: string;
92
- recipient: string;
93
94
  fromInternalBalance: boolean;
95
+ recipient: string;
94
96
  toInternalBalance: boolean;
95
97
  };
96
98
 
@@ -117,6 +119,24 @@ export type BalancerParam = [
117
119
  deadline: string,
118
120
  ];
119
121
 
122
+ export type BalancerV2DirectParam = [
123
+ swaps: BalancerSwap[],
124
+ assets: Address[],
125
+ funds: BalancerFunds,
126
+ limits: NumberAsString[],
127
+ fromAmount: NumberAsString,
128
+ toAmount: NumberAsString,
129
+ expectedAmount: NumberAsString,
130
+ deadline: NumberAsString,
131
+ feePercent: NumberAsString,
132
+ vault: Address,
133
+ partner: Address,
134
+ isApproved: boolean,
135
+ beneficiary: Address,
136
+ permit: string,
137
+ uuid: string,
138
+ ];
139
+
120
140
  export type BalancerV2Data = {
121
141
  poolId: string;
122
142
  };
@@ -2,6 +2,8 @@ import { CurveV1Config } from './config';
2
2
  import { SwerveConfig } from './forks/swerve/config';
3
3
  import { CurveForksConfig } from './forks/curve-forks/config';
4
4
 
5
+ export const DIRECT_METHOD_NAME = 'directCurveV1Swap';
6
+
5
7
  export const AllCurveForks = [
6
8
  ...Object.keys(CurveV1Config),
7
9
  ...Object.keys(CurveForksConfig),
@@ -133,6 +133,63 @@ describe('CurveV1 E2E', () => {
133
133
  });
134
134
  });
135
135
 
136
+ describe('CurveV1 POLYGON', () => {
137
+ const network = Network.POLYGON;
138
+ const tokens = Tokens[network];
139
+ const holders = Holders[network];
140
+ const provider = new StaticJsonRpcProvider(
141
+ generateConfig(network).privateHttpProvider,
142
+ network,
143
+ );
144
+
145
+ const tokensToTest = [
146
+ [
147
+ {
148
+ symbol: 'USDT',
149
+ amount: (10 ** 8).toString(),
150
+ },
151
+ {
152
+ symbol: 'DAI',
153
+ amount: (10 ** 8).toString(),
154
+ },
155
+ ],
156
+ ];
157
+
158
+ const sideToContractMethods = new Map([
159
+ [
160
+ SwapSide.SELL,
161
+ [
162
+ ContractMethod.simpleSwap,
163
+ ContractMethod.multiSwap,
164
+ ContractMethod.megaSwap,
165
+ ContractMethod.directCurveV1Swap,
166
+ ],
167
+ ],
168
+ ]);
169
+
170
+ sideToContractMethods.forEach((contractMethods, side) =>
171
+ contractMethods.forEach((contractMethod: ContractMethod) => {
172
+ tokensToTest.forEach(pair => {
173
+ describe(`${contractMethod}`, () => {
174
+ it(`${pair[0].symbol} -> ${pair[1].symbol}`, async () => {
175
+ await testE2E(
176
+ tokens[pair[0].symbol],
177
+ tokens[pair[1].symbol],
178
+ holders[pair[0].symbol],
179
+ side === SwapSide.SELL ? pair[0].amount : pair[1].amount,
180
+ side,
181
+ dexKey,
182
+ contractMethod,
183
+ network,
184
+ provider,
185
+ );
186
+ });
187
+ });
188
+ });
189
+ }),
190
+ );
191
+ });
192
+
136
193
  describe('FANTOM', () => {
137
194
  const network = Network.FANTOM;
138
195
  const tokens = Tokens[network];
@@ -1,17 +1,24 @@
1
1
  import { Interface, AbiCoder, JsonFragment } from '@ethersproject/abi';
2
+ import _ from 'lodash';
3
+ import { NumberAsString, OptimalSwapExchange } from '@paraswap/core';
4
+ import { assert } from 'ts-essentials';
2
5
  import BigNumber from 'bignumber.js';
3
6
  import CurveABI from '../../abi/Curve.json';
7
+ import DirectSwapABI from '../../abi/DirectSwap.json';
4
8
  import { Adapters, CurveV1Config } from './config';
5
9
 
6
10
  import {
7
11
  AdapterExchangeParam,
8
12
  Address,
9
13
  ExchangePrices,
14
+ ExchangeTxInfo,
10
15
  PoolLiquidity,
11
16
  PoolPrices,
17
+ PreprocessTransactionOptions,
12
18
  SimpleExchangeParam,
13
19
  Token,
14
20
  TransferFeeParams,
21
+ TxInfo,
15
22
  } from '../../types';
16
23
  import {
17
24
  Network,
@@ -48,8 +55,6 @@ import { TBTCPool, address as TBTCPoolAddress } from './pools/tBTCpool';
48
55
  import { USDKPool, address as USDKPoolAddress } from './pools/usdkpool';
49
56
  import { USTPool, address as USTPoolAddress } from './pools/ustpool';
50
57
  import { SLINKPool, address as SLINKPoolAddress } from './pools/sLinkpool';
51
-
52
- import * as _ from 'lodash';
53
58
  import {
54
59
  getBigIntPow,
55
60
  getDexKeysWithNetwork,
@@ -57,9 +62,13 @@ import {
57
62
  isDestTokenTransferFeeToBeExchanged,
58
63
  isSrcTokenTransferFeeToBeExchanged,
59
64
  Utils,
65
+ uuidToBytes16,
60
66
  } from '../../utils';
61
67
  import { BN_0 } from '../../bignumber-constants';
62
- import { SimpleExchange } from '../simple-exchange';
68
+ import {
69
+ getLocalDeadlineAsFriendlyPlaceholder,
70
+ SimpleExchange,
71
+ } from '../simple-exchange';
63
72
  import { IDex } from '../idex';
64
73
  import { IDexHelper } from '../../dex-helper';
65
74
  import { Logger } from 'log4js';
@@ -71,9 +80,12 @@ import {
71
80
  CurveV1Data,
72
81
  TokenWithReasonableVolume,
73
82
  CurveSwapFunctions,
83
+ CurveV1SwapType,
84
+ DirectCurveV1Param,
74
85
  } from './types';
75
86
  import { erc20Iface } from '../../lib/utils-interfaces';
76
87
  import { applyTransferFee } from '../../lib/token-transfer-fee';
88
+ import { DIRECT_METHOD_NAME } from './constants';
77
89
 
78
90
  const CURVE_DEFAULT_CHUNKS = 10;
79
91
 
@@ -82,7 +94,10 @@ const CURVE_POOL_GAS = 200 * 1000;
82
94
 
83
95
  const coder = new AbiCoder();
84
96
 
85
- export class CurveV1 extends SimpleExchange implements IDex<CurveV1Data> {
97
+ export class CurveV1
98
+ extends SimpleExchange
99
+ implements IDex<CurveV1Data, DirectCurveV1Param>
100
+ {
86
101
  exchangeRouterInterface: Interface;
87
102
  minConversionRate = '1';
88
103
 
@@ -102,6 +117,8 @@ export class CurveV1 extends SimpleExchange implements IDex<CurveV1Data> {
102
117
 
103
118
  protected disableFeeOnTransferTokenAddresses: Set<string>;
104
119
 
120
+ readonly directSwapIface = new Interface(DirectSwapABI);
121
+
105
122
  readonly SRC_TOKEN_DEX_TRANSFERS = 1;
106
123
  readonly DEST_TOKEN_DEX_TRANSFERS = 1;
107
124
 
@@ -840,6 +857,128 @@ export class CurveV1 extends SimpleExchange implements IDex<CurveV1Data> {
840
857
  return this.adapters[side];
841
858
  }
842
859
 
860
+ static getDirectFunctionName(): string[] {
861
+ return [DIRECT_METHOD_NAME];
862
+ }
863
+
864
+ getTokenFromAddress(address: Address): Token {
865
+ // In this Dex decimals are not used
866
+ return { address, decimals: 0 };
867
+ }
868
+
869
+ async preProcessTransaction(
870
+ optimalSwapExchange: OptimalSwapExchange<CurveV1Data>,
871
+ srcToken: Token,
872
+ _0: Token,
873
+ _1: SwapSide,
874
+ options: PreprocessTransactionOptions,
875
+ ): Promise<[OptimalSwapExchange<CurveV1Data>, ExchangeTxInfo]> {
876
+ if (!options.isDirectMethod) {
877
+ return [
878
+ optimalSwapExchange,
879
+ {
880
+ deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
881
+ },
882
+ ];
883
+ }
884
+
885
+ assert(
886
+ optimalSwapExchange.data !== undefined,
887
+ `preProcessTransaction: data field is missing`,
888
+ );
889
+
890
+ let isApproved: boolean | undefined;
891
+
892
+ try {
893
+ this.erc20Contract.options.address =
894
+ this.dexHelper.config.wrapETH(srcToken).address;
895
+ const allowance = await this.erc20Contract.methods
896
+ .allowance(this.augustusAddress, optimalSwapExchange.data.exchange)
897
+ .call(undefined, 'latest');
898
+ isApproved =
899
+ BigInt(allowance.toString()) >= BigInt(optimalSwapExchange.srcAmount);
900
+ } catch (e) {
901
+ this.logger.error(
902
+ `preProcessTransaction failed to retrieve allowance info: `,
903
+ e,
904
+ );
905
+ }
906
+
907
+ return [
908
+ {
909
+ ...optimalSwapExchange,
910
+ data: {
911
+ ...optimalSwapExchange.data,
912
+ isApproved,
913
+ },
914
+ },
915
+ {
916
+ deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
917
+ },
918
+ ];
919
+ }
920
+
921
+ getDirectParam(
922
+ srcToken: Address,
923
+ destToken: Address,
924
+ srcAmount: NumberAsString,
925
+ destAmount: NumberAsString,
926
+ expectedAmount: NumberAsString,
927
+ data: CurveV1Data,
928
+ side: SwapSide,
929
+ permit: string,
930
+ uuid: string,
931
+ feePercent: NumberAsString,
932
+ deadline: NumberAsString,
933
+ partner: string,
934
+ beneficiary: string,
935
+ contractMethod?: string,
936
+ ): TxInfo<DirectCurveV1Param> {
937
+ if (contractMethod !== DIRECT_METHOD_NAME) {
938
+ throw new Error(`Invalid contract method ${contractMethod}`);
939
+ }
940
+ assert(side === SwapSide.SELL, 'Buy not supported');
941
+
942
+ let isApproved: boolean = !!data.isApproved;
943
+ if (data.isApproved === undefined) {
944
+ this.logger.warn(`isApproved is undefined, defaulting to false`);
945
+ }
946
+
947
+ const swapParams: DirectCurveV1Param = [
948
+ srcToken,
949
+ destToken,
950
+ data.exchange,
951
+ srcAmount,
952
+ destAmount,
953
+ expectedAmount,
954
+ feePercent,
955
+ data.i.toString(),
956
+ data.j.toString(),
957
+ partner,
958
+ isApproved,
959
+ data.underlyingSwap
960
+ ? CurveV1SwapType.EXCHANGE_UNDERLYING
961
+ : CurveV1SwapType.EXCHANGE,
962
+ beneficiary,
963
+ // For CurveV1 we work as it is, without wrapping and unwrapping
964
+ false,
965
+ permit,
966
+ uuidToBytes16(uuid),
967
+ ];
968
+
969
+ const encoder = (...params: DirectCurveV1Param) => {
970
+ return this.directSwapIface.encodeFunctionData(DIRECT_METHOD_NAME, [
971
+ params,
972
+ ]);
973
+ };
974
+
975
+ return {
976
+ params: swapParams,
977
+ encoder,
978
+ networkFee: '0',
979
+ };
980
+ }
981
+
843
982
  async getSimpleParam(
844
983
  srcToken: string,
845
984
  destToken: string,
@@ -943,7 +1082,7 @@ export class CurveV1 extends SimpleExchange implements IDex<CurveV1Data> {
943
1082
  // the balances array in the pool as it was a mess to have each curve abi
944
1083
  // Some have balances[uint128] some have balances[uint256]
945
1084
  // One of the consequence for such a hack is below for ETH Pools
946
- // TODO: below can be highly optimised
1085
+ // TODO: below can be highly optimized
947
1086
  // * we don't need to query token decimals every iteration
948
1087
  // * we can use the decimals from the tokens list using the api
949
1088
 
@@ -1,3 +1,4 @@
1
+ import { NumberAsString } from '@paraswap/core';
1
2
  import { Address, Token } from '../../types';
2
3
 
3
4
  export type CurveV1Data = {
@@ -6,6 +7,7 @@ export type CurveV1Data = {
6
7
  j: number;
7
8
  underlyingSwap: boolean;
8
9
  deadline: number;
10
+ isApproved?: boolean;
9
11
  };
10
12
 
11
13
  export type PoolConfig = {
@@ -42,3 +44,27 @@ export enum CurveSwapFunctions {
42
44
  exchange = 'exchange',
43
45
  exchange_underlying = 'exchange_underlying',
44
46
  }
47
+
48
+ export enum CurveV1SwapType {
49
+ EXCHANGE,
50
+ EXCHANGE_UNDERLYING,
51
+ }
52
+
53
+ export type DirectCurveV1Param = [
54
+ fromToken: Address,
55
+ toToken: Address,
56
+ exchange: Address,
57
+ fromAmount: NumberAsString,
58
+ toAmount: NumberAsString,
59
+ expectedAmount: NumberAsString,
60
+ feePercent: NumberAsString,
61
+ i: NumberAsString,
62
+ j: NumberAsString,
63
+ partner: Address,
64
+ isApproved: boolean,
65
+ swapType: CurveV1SwapType,
66
+ beneficiary: Address,
67
+ needWrapNative: boolean,
68
+ permit: string,
69
+ uuid: string,
70
+ ];
@@ -34,3 +34,5 @@ export const NETWORK_ID_TO_NAME: Record<number, string> = {
34
34
  // revisited
35
35
  export const FACTORY_MAX_PLAIN_COINS = 4;
36
36
  export const FACTORY_MAX_PLAIN_IMPLEMENTATIONS_FOR_COIN = 10;
37
+
38
+ export const DIRECT_METHOD_NAME = 'directCurveV1Swap';
@@ -29,6 +29,7 @@ function testForNetwork(
29
29
  ContractMethod.simpleSwap,
30
30
  ContractMethod.multiSwap,
31
31
  ContractMethod.megaSwap,
32
+ ContractMethod.directCurveV1Swap,
32
33
  ],
33
34
  ],
34
35
  ]);