@paraswap/dex-lib 3.0.6-balancer-direct-fn-fix.1 → 3.0.6

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/src/config.ts CHANGED
@@ -202,15 +202,15 @@ const baseConfigs: { [network: number]: BaseConfig } = {
202
202
  wrappedNativeTokenAddress: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270',
203
203
  hasEIP1559: true,
204
204
  augustusAddress: '0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57',
205
- augustusV6Address: '0x7d5B7A82f0047E83064332A88918d7067c00AA14',
205
+ augustusV6Address: '0x00000000FdAC7708D0D360BDDc1bc7d097F47439',
206
206
  augustusRFQAddress: '0xF3CD476C3C4D3Ac5cA2724767f269070CA09A043',
207
207
  tokenTransferProxyAddress: '0x216b4b4ba9f3e719726886d34a177484278bfcae',
208
208
  multicallV2Address: '0x275617327c958bD06b5D6b871E7f491D76113dd8',
209
209
  privateHttpProvider: process.env.HTTP_PROVIDER_137,
210
210
  executorsAddresses: {
211
- Executor01: '0xaF413566b437AC2e22AAb08A9eD10B5b93DC4eB8',
212
- Executor02: '0x307Bf21eb68d636A15EC4AbdAb9Dc466A2aa0282',
213
- Executor03: '0xe34f534159701B9019845b2551072b94403B3289',
211
+ Executor01: '0x000000006E85dF93Da91e65184708f020e58df2c',
212
+ Executor02: '0x000000C0ef6dBff0E689d393EDBb93702eEEf0d8',
213
+ Executor03: '0x000000856CFe8D0625D96BD87712A074d90a9F5b',
214
214
  },
215
215
  hashFlowAuthToken: process.env.API_KEY_HASHFLOW_AUTH_TOKEN || '',
216
216
  smardexSubgraphAuthToken: process.env.API_KEY_SMARDEX_SUBGRAPH || '',
@@ -1006,6 +1006,8 @@ export class BalancerV2
1006
1006
  const params = this.getBalancerParam(
1007
1007
  srcToken,
1008
1008
  destToken,
1009
+ srcAmount,
1010
+ destAmount,
1009
1011
  data,
1010
1012
  side,
1011
1013
  this.dexHelper.config.data.augustusAddress!,
@@ -1066,21 +1068,58 @@ export class BalancerV2
1066
1068
  # note: we pass sender=null then the address of the executor is inferred contract side
1067
1069
  else (so buy)
1068
1070
  sender = recipient = executor
1071
+
1072
+ ------------------------------------------------------------------------------------
1073
+
1074
+ Algorithm to determine balancer (limits) params:
1075
+
1076
+ if version = 5
1077
+ limits = [MAX_INT * assetsCount]
1078
+ else (so V6)
1079
+ if direct swap and horizontal sequences
1080
+ # note: v6 direct swap functions (swapExactAmountInOnBalancerV2 / swapExactAmountOutOnBalancerV2) do not allow parallel sequences of swap, read last note for details
1081
+ if sell
1082
+ limits = [srcAmount, ...[MAX_INT * assetsCount - 2]?, -destAmount]
1083
+ else (so buy)
1084
+ limits = [-destAmount, ...[MAX_INT * assetsCount - 2]?, srcAmount]
1085
+ # note: in this particular case we need to walk the assets backward since direct contract functions expect assets and limits to be reversed (note this is not natively enforced in balancer, only in our contract). It's not safe to do in a parallel buy sequence.
1086
+ else (so generic swaps)
1087
+ limits = [MAX_INT * assetsCount]
1088
+ # note:
1089
+ # for cases where we pass an array [MAX_INT * assetsCount] this assumes that we relax local slippage checks in balancer in favor of global check in augustus
1090
+ # otherwise, in direct v6 function, as we decode params off balancer and srcAmount and destAmount from limits array, this cannot work for parallel sequences where limits[-1] (sell) is a local slippage to 1 parallel branch only
1091
+
1069
1092
  */
1070
1093
  public getBalancerParam(
1071
1094
  srcToken: string,
1072
1095
  destToken: string,
1096
+ srcAmount: string,
1097
+ destAmount: string,
1073
1098
  data: OptimizedBalancerV2Data,
1074
1099
  side: SwapSide,
1075
1100
  recipient: string,
1076
1101
  sender: string,
1077
- shouldWalkAssetsBackward?: boolean, // should do for all buy but prefer keep it under control
1102
+ shouldApplyHardLimits?: boolean,
1078
1103
  ): BalancerParam {
1079
1104
  let swapOffset = 0;
1080
1105
  let swaps: BalancerSwap[] = [];
1081
1106
  let assets: string[] = [];
1082
1107
  let limits: string[] = [];
1083
1108
 
1109
+ if (shouldApplyHardLimits) {
1110
+ if (data.swaps.length > 1) {
1111
+ const error = new Error(
1112
+ `Not safe to apply hard limits for parallel sequences in balancerV2 in swapExactAmountInOnBalancerV2 / swapExactAmountOutOnBalancerV2`,
1113
+ );
1114
+ this.logger.error(error.message, error);
1115
+ throw error;
1116
+ }
1117
+ }
1118
+
1119
+ // read function comment up
1120
+ const shouldWalkAssetsBackward =
1121
+ side === SwapSide.BUY && shouldApplyHardLimits;
1122
+
1084
1123
  for (const swapData of data.swaps) {
1085
1124
  const pool = this.poolIdMap[swapData.poolId];
1086
1125
  const hasEth = [srcToken.toLowerCase(), destToken.toLowerCase()].includes(
@@ -1151,6 +1190,18 @@ export class BalancerV2
1151
1190
  limits = limits.concat(_limits);
1152
1191
  }
1153
1192
 
1193
+ if (shouldApplyHardLimits) {
1194
+ // re-accumulate amount to prevent leaving dust in payer contract
1195
+ const accumulatedAmount = swaps
1196
+ .reduce((acc, s) => acc + BigInt(s.amount), 0n)
1197
+ .toString();
1198
+
1199
+ [limits[0], limits[limits.length - 1]] =
1200
+ side == SwapSide.SELL
1201
+ ? [accumulatedAmount, (-BigInt(destAmount)).toString()]
1202
+ : [(-BigInt(destAmount)).toString(), srcAmount];
1203
+ }
1204
+
1154
1205
  const funds = {
1155
1206
  sender,
1156
1207
  recipient,
@@ -1164,7 +1215,7 @@ export class BalancerV2
1164
1215
  shouldWalkAssetsBackward ? assets.reverse() : assets,
1165
1216
  funds,
1166
1217
  limits,
1167
- getLocalDeadlineAsFriendlyPlaceholder(),
1218
+ MAX_UINT,
1168
1219
  ];
1169
1220
 
1170
1221
  return params;
@@ -1260,6 +1311,8 @@ export class BalancerV2
1260
1311
  const [, swaps, assets, funds, limits, _deadline] = this.getBalancerParam(
1261
1312
  srcToken,
1262
1313
  destToken,
1314
+ srcAmount,
1315
+ destAmount,
1263
1316
  data,
1264
1317
  side,
1265
1318
  this.dexHelper.config.data.augustusAddress!,
@@ -1329,16 +1382,16 @@ export class BalancerV2
1329
1382
  const balancerParams = this.getBalancerParam(
1330
1383
  srcToken,
1331
1384
  destToken,
1385
+ fromAmount,
1386
+ toAmount,
1332
1387
  data,
1333
1388
  side,
1334
1389
  this.dexHelper.config.data.augustusV6Address!,
1335
1390
  this.dexHelper.config.data.augustusV6Address!,
1336
- side === SwapSide.BUY,
1391
+ true,
1337
1392
  );
1338
1393
 
1339
1394
  const swapParams: BalancerV2DirectParamV6 = [
1340
- fromAmount,
1341
- toAmount,
1342
1395
  quotedAmount,
1343
1396
  metadata,
1344
1397
  this.encodeBeneficiaryAndApproveFlag(beneficiary, !data.isApproved),
@@ -1402,6 +1455,8 @@ export class BalancerV2
1402
1455
  const params = this.getBalancerParam(
1403
1456
  srcToken,
1404
1457
  destToken,
1458
+ srcAmount,
1459
+ destAmount,
1405
1460
  data,
1406
1461
  side,
1407
1462
  this.dexHelper.config.data.augustusAddress!,
@@ -1436,6 +1491,8 @@ export class BalancerV2
1436
1491
  const params = this.getBalancerParam(
1437
1492
  srcToken,
1438
1493
  destToken,
1494
+ srcAmount,
1495
+ destAmount,
1439
1496
  data,
1440
1497
  side,
1441
1498
  recipient,
@@ -138,8 +138,6 @@ export type BalancerV2DirectParam = [
138
138
  ];
139
139
 
140
140
  export type BalancerV2DirectParamV6 = [
141
- fromAmount: NumberAsString,
142
- toAmount: NumberAsString,
143
141
  quotedAmount: NumberAsString,
144
142
  metadata: string,
145
143
  beneficiaryAndApproveFlag: NumberAsString,
@@ -694,20 +694,6 @@ export class UniswapV3
694
694
 
695
695
  const zeroForOne = token0 === _srcAddress ? true : false;
696
696
 
697
- this.logger.log(
698
- 'UNI_POOLS',
699
- JSON.stringify(
700
- poolsToUse.poolWithState.map(el => ({
701
- address: el.poolAddress,
702
- token0: el.token0,
703
- token1: el.token1,
704
- fee: el.feeCode,
705
- })),
706
- null,
707
- 2,
708
- ),
709
- );
710
-
711
697
  const result = await Promise.all(
712
698
  poolsToUse.poolWithState.map(async (pool, i) => {
713
699
  const state = states[i];
@@ -391,7 +391,7 @@ export class GenericSwapTransactionBuilder {
391
391
  permit,
392
392
  deadline,
393
393
  uuid,
394
- beneficiary = NULL_ADDRESS,
394
+ beneficiary,
395
395
  onlyParams = false,
396
396
  }: {
397
397
  priceRoute: OptimalRate;
@@ -411,12 +411,8 @@ export class GenericSwapTransactionBuilder {
411
411
  beneficiary?: Address;
412
412
  onlyParams?: boolean;
413
413
  }): Promise<TxObject> {
414
- // if beneficiary is not defined, then in smart contract it will be replaced to msg.sender
415
414
  const _beneficiary =
416
- beneficiary !== NULL_ADDRESS &&
417
- beneficiary.toLowerCase() !== userAddress.toLowerCase()
418
- ? beneficiary
419
- : NULL_ADDRESS;
415
+ beneficiary && beneficiary !== NULL_ADDRESS ? beneficiary : userAddress;
420
416
 
421
417
  let encoder: (...params: any[]) => string;
422
418
  let params: (string | string[])[];