@paraswap/dex-lib 3.0.5 → 3.0.6-balancer-direct-fn-fix.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.
package/src/config.ts CHANGED
@@ -46,7 +46,7 @@ const baseConfigs: { [network: number]: BaseConfig } = {
46
46
  nativeTokenSymbol: 'ETH',
47
47
  wrappedNativeTokenAddress: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
48
48
  hasEIP1559: true,
49
- augustusAddress: '0xE4e284Eb7a312d95A7949fCA8C634164358f4492',
49
+ augustusAddress: '0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57',
50
50
  augustusV6Address: '0x00000000FdAC7708D0D360BDDc1bc7d097F47439',
51
51
  augustusRFQAddress: '0xe92b586627ccA7a83dC919cc7127196d70f55a06',
52
52
  tokenTransferProxyAddress: '0x216b4b4ba9f3e719726886d34a177484278bfcae',
@@ -1006,8 +1006,6 @@ export class BalancerV2
1006
1006
  const params = this.getBalancerParam(
1007
1007
  srcToken,
1008
1008
  destToken,
1009
- srcAmount,
1010
- destAmount,
1011
1009
  data,
1012
1010
  side,
1013
1011
  this.dexHelper.config.data.augustusAddress!,
@@ -1068,58 +1066,20 @@ export class BalancerV2
1068
1066
  # note: we pass sender=null then the address of the executor is inferred contract side
1069
1067
  else (so buy)
1070
1068
  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
-
1092
1069
  */
1093
1070
  public getBalancerParam(
1094
1071
  srcToken: string,
1095
1072
  destToken: string,
1096
- srcAmount: string,
1097
- destAmount: string,
1098
1073
  data: OptimizedBalancerV2Data,
1099
1074
  side: SwapSide,
1100
1075
  recipient: string,
1101
1076
  sender: string,
1102
- shouldApplyHardLimits?: boolean,
1103
1077
  ): BalancerParam {
1104
1078
  let swapOffset = 0;
1105
1079
  let swaps: BalancerSwap[] = [];
1106
1080
  let assets: string[] = [];
1107
1081
  let limits: string[] = [];
1108
1082
 
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
-
1123
1083
  for (const swapData of data.swaps) {
1124
1084
  const pool = this.poolIdMap[swapData.poolId];
1125
1085
  const hasEth = [srcToken.toLowerCase(), destToken.toLowerCase()].includes(
@@ -1147,13 +1107,8 @@ export class BalancerV2
1147
1107
  }
1148
1108
 
1149
1109
  const _swaps = path.map((hop, index) => {
1150
- const assetInIndex = shouldWalkAssetsBackward
1151
- ? swapOffset + path.length - index
1152
- : swapOffset + index;
1153
-
1154
- const assetOutIndex = shouldWalkAssetsBackward
1155
- ? swapOffset + path.length - index - 1
1156
- : swapOffset + index + 1;
1110
+ const assetInIndex = swapOffset + index;
1111
+ const assetOutIndex = swapOffset + index + 1;
1157
1112
 
1158
1113
  const amount =
1159
1114
  (side === SwapSide.SELL && index === 0) ||
@@ -1190,18 +1145,6 @@ export class BalancerV2
1190
1145
  limits = limits.concat(_limits);
1191
1146
  }
1192
1147
 
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
-
1205
1148
  const funds = {
1206
1149
  sender,
1207
1150
  recipient,
@@ -1212,7 +1155,7 @@ export class BalancerV2
1212
1155
  const params: BalancerParam = [
1213
1156
  side === SwapSide.SELL ? SwapTypes.SwapExactIn : SwapTypes.SwapExactOut,
1214
1157
  side === SwapSide.SELL ? swaps : swaps.reverse(),
1215
- shouldWalkAssetsBackward ? assets.reverse() : assets,
1158
+ assets,
1216
1159
  funds,
1217
1160
  limits,
1218
1161
  MAX_UINT,
@@ -1311,8 +1254,6 @@ export class BalancerV2
1311
1254
  const [, swaps, assets, funds, limits, _deadline] = this.getBalancerParam(
1312
1255
  srcToken,
1313
1256
  destToken,
1314
- srcAmount,
1315
- destAmount,
1316
1257
  data,
1317
1258
  side,
1318
1259
  this.dexHelper.config.data.augustusAddress!,
@@ -1382,16 +1323,15 @@ export class BalancerV2
1382
1323
  const balancerParams = this.getBalancerParam(
1383
1324
  srcToken,
1384
1325
  destToken,
1385
- fromAmount,
1386
- toAmount,
1387
1326
  data,
1388
1327
  side,
1389
1328
  this.dexHelper.config.data.augustusV6Address!,
1390
1329
  this.dexHelper.config.data.augustusV6Address!,
1391
- true,
1392
1330
  );
1393
1331
 
1394
1332
  const swapParams: BalancerV2DirectParamV6 = [
1333
+ fromAmount,
1334
+ toAmount,
1395
1335
  quotedAmount,
1396
1336
  metadata,
1397
1337
  this.encodeBeneficiaryAndApproveFlag(beneficiary, !data.isApproved),
@@ -1455,8 +1395,6 @@ export class BalancerV2
1455
1395
  const params = this.getBalancerParam(
1456
1396
  srcToken,
1457
1397
  destToken,
1458
- srcAmount,
1459
- destAmount,
1460
1398
  data,
1461
1399
  side,
1462
1400
  this.dexHelper.config.data.augustusAddress!,
@@ -1491,8 +1429,6 @@ export class BalancerV2
1491
1429
  const params = this.getBalancerParam(
1492
1430
  srcToken,
1493
1431
  destToken,
1494
- srcAmount,
1495
- destAmount,
1496
1432
  data,
1497
1433
  side,
1498
1434
  recipient,
@@ -138,6 +138,8 @@ export type BalancerV2DirectParam = [
138
138
  ];
139
139
 
140
140
  export type BalancerV2DirectParamV6 = [
141
+ fromAmount: NumberAsString,
142
+ toAmount: NumberAsString,
141
143
  quotedAmount: NumberAsString,
142
144
  metadata: string,
143
145
  beneficiaryAndApproveFlag: NumberAsString,