forked from balancer/balancer-v3-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BatchRouter.sol
688 lines (623 loc) · 33.2 KB
/
BatchRouter.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IPermit2 } from "permit2/src/interfaces/IPermit2.sol";
import { IBatchRouter } from "@balancer-labs/v3-interfaces/contracts/vault/IBatchRouter.sol";
import { IWETH } from "@balancer-labs/v3-interfaces/contracts/solidity-utils/misc/IWETH.sol";
import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
import "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
import { EVMCallModeHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/EVMCallModeHelpers.sol";
import { CastingHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/CastingHelpers.sol";
import { InputHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/InputHelpers.sol";
import {
TransientEnumerableSet
} from "@balancer-labs/v3-solidity-utils/contracts/openzeppelin/TransientEnumerableSet.sol";
import {
TransientStorageHelpers
} from "@balancer-labs/v3-solidity-utils/contracts/helpers/TransientStorageHelpers.sol";
import { BatchRouterCommon } from "./BatchRouterCommon.sol";
struct SwapStepLocals {
bool isFirstStep;
bool isLastStep;
}
/**
* @notice Entrypoint for batch swaps, and batch swap queries.
* @dev The external API functions unlock the Vault, which calls back into the corresponding hook functions.
* These interpret the steps and paths in the input data, perform token accounting (in transient storage, to save gas),
* settle with the Vault, and handle wrapping and unwrapping ETH.
*/
contract BatchRouter is IBatchRouter, BatchRouterCommon {
using CastingHelpers for *;
using TransientEnumerableSet for TransientEnumerableSet.AddressSet;
using TransientStorageHelpers for *;
using SafeERC20 for IERC20;
using SafeCast for *;
constructor(
IVault vault,
IWETH weth,
IPermit2 permit2,
string memory routerVersion
) BatchRouterCommon(vault, weth, permit2, routerVersion) {
// solhint-disable-previous-line no-empty-blocks
}
/***************************************************************************
Swaps
***************************************************************************/
/// @inheritdoc IBatchRouter
function swapExactIn(
SwapPathExactAmountIn[] memory paths,
uint256 deadline,
bool wethIsEth,
bytes calldata userData
)
external
payable
saveSender(msg.sender)
returns (uint256[] memory pathAmountsOut, address[] memory tokensOut, uint256[] memory amountsOut)
{
return
abi.decode(
_vault.unlock(
abi.encodeCall(
BatchRouter.swapExactInHook,
SwapExactInHookParams({
sender: msg.sender,
paths: paths,
deadline: deadline,
wethIsEth: wethIsEth,
userData: userData
})
)
),
(uint256[], address[], uint256[])
);
}
/// @inheritdoc IBatchRouter
function swapExactOut(
SwapPathExactAmountOut[] memory paths,
uint256 deadline,
bool wethIsEth,
bytes calldata userData
)
external
payable
saveSender(msg.sender)
returns (uint256[] memory pathAmountsIn, address[] memory tokensIn, uint256[] memory amountsIn)
{
return
abi.decode(
_vault.unlock(
abi.encodeCall(
BatchRouter.swapExactOutHook,
SwapExactOutHookParams({
sender: msg.sender,
paths: paths,
deadline: deadline,
wethIsEth: wethIsEth,
userData: userData
})
)
),
(uint256[], address[], uint256[])
);
}
function swapExactInHook(
SwapExactInHookParams calldata params
)
external
nonReentrant
onlyVault
returns (uint256[] memory pathAmountsOut, address[] memory tokensOut, uint256[] memory amountsOut)
{
(pathAmountsOut, tokensOut, amountsOut) = _swapExactInHook(params);
_settlePaths(params.sender, params.wethIsEth);
}
function _swapExactInHook(
SwapExactInHookParams calldata params
) internal returns (uint256[] memory pathAmountsOut, address[] memory tokensOut, uint256[] memory amountsOut) {
// The deadline is timestamp-based: it should not be relied upon for sub-minute accuracy.
// solhint-disable-next-line not-rely-on-time
if (block.timestamp > params.deadline) {
revert SwapDeadline();
}
pathAmountsOut = _computePathAmountsOut(params);
// The hook writes current swap token and token amounts out.
// We copy that information to memory to return it before it is deleted during settlement.
tokensOut = _currentSwapTokensOut().values();
amountsOut = new uint256[](tokensOut.length);
for (uint256 i = 0; i < tokensOut.length; ++i) {
amountsOut[i] =
_currentSwapTokenOutAmounts().tGet(tokensOut[i]) +
_settledTokenAmounts().tGet(tokensOut[i]);
_settledTokenAmounts().tSet(tokensOut[i], 0);
}
}
function _computePathAmountsOut(
SwapExactInHookParams calldata params
) internal returns (uint256[] memory pathAmountsOut) {
pathAmountsOut = new uint256[](params.paths.length);
for (uint256 i = 0; i < params.paths.length; ++i) {
SwapPathExactAmountIn memory path = params.paths[i];
// These two variables shall be updated at the end of each step to be used as inputs of the next one.
// The initial values are the given token and amount in for the current path.
uint256 stepExactAmountIn = path.exactAmountIn;
IERC20 stepTokenIn = path.tokenIn;
if (path.steps[0].isBuffer && EVMCallModeHelpers.isStaticCall() == false) {
// If first step is a buffer, take the token in advance. We need this to wrap/unwrap.
_takeTokenIn(params.sender, stepTokenIn, stepExactAmountIn, params.wethIsEth);
} else {
// Paths may (or may not) share the same token in. To minimize token transfers, we store the addresses
// in a set with unique addresses that can be iterated later on.
// For example, if all paths share the same token in, the set will end up with only one entry.
_currentSwapTokensIn().add(address(stepTokenIn));
_currentSwapTokenInAmounts().tAdd(address(stepTokenIn), stepExactAmountIn);
}
for (uint256 j = 0; j < path.steps.length; ++j) {
SwapStepLocals memory stepLocals;
stepLocals.isLastStep = (j == path.steps.length - 1);
stepLocals.isFirstStep = (j == 0);
uint256 minAmountOut;
// minAmountOut only applies to the last step.
if (stepLocals.isLastStep) {
minAmountOut = path.minAmountOut;
} else {
minAmountOut = 0;
}
SwapPathStep memory step = path.steps[j];
if (step.isBuffer) {
(, , uint256 amountOut) = _vault.erc4626BufferWrapOrUnwrap(
BufferWrapOrUnwrapParams({
kind: SwapKind.EXACT_IN,
direction: step.pool == address(stepTokenIn)
? WrappingDirection.UNWRAP
: WrappingDirection.WRAP,
wrappedToken: IERC4626(step.pool),
amountGivenRaw: stepExactAmountIn,
limitRaw: minAmountOut
})
);
if (stepLocals.isLastStep) {
// The amount out for the last step of the path should be recorded for the return value, and the
// amount for the token should be sent back to the sender later on.
pathAmountsOut[i] = amountOut;
_currentSwapTokensOut().add(address(step.tokenOut));
_currentSwapTokenOutAmounts().tAdd(address(step.tokenOut), amountOut);
} else {
// Input for the next step is output of current step.
stepExactAmountIn = amountOut;
// The token in for the next step is the token out of the current step.
stepTokenIn = step.tokenOut;
}
} else if (address(stepTokenIn) == step.pool) {
// Token in is BPT: remove liquidity - Single token exact in
// Remove liquidity is not transient when it comes to BPT, meaning the caller needs to have the
// required amount when performing the operation. These tokens might be the output of a previous
// step, in which case the user will have a BPT credit.
if (stepLocals.isFirstStep) {
if (stepExactAmountIn > 0 && params.sender != address(this)) {
// If this is the first step, the sender must have the tokens. Therefore, we can transfer
// them to the Router, which acts as an intermediary. If the sender is the Router, we just
// skip this step (useful for queries).
//
// This saves one permit(1) approval for the BPT to the Router; if we burned tokens
// directly from the sender we would need their approval.
_permit2.transferFrom(
params.sender,
address(this),
stepExactAmountIn.toUint160(),
address(stepTokenIn)
);
}
// BPT is burned instantly, so we don't need to send it back later.
if (_currentSwapTokenInAmounts().tGet(address(stepTokenIn)) > 0) {
_currentSwapTokenInAmounts().tSub(address(stepTokenIn), stepExactAmountIn);
}
} else {
// If this is an intermediate step, we don't expect the sender to have BPT to burn.
// Then, we flashloan tokens here (which should in practice just use existing credit).
_vault.sendTo(IERC20(step.pool), address(this), stepExactAmountIn);
}
// minAmountOut cannot be 0 in this case, as that would send an array of 0s to the Vault, which
// wouldn't know which token to use.
(uint256[] memory amountsOut, uint256 tokenIndex) = _getSingleInputArrayAndTokenIndex(
step.pool,
step.tokenOut,
minAmountOut == 0 ? 1 : minAmountOut
);
// Router is always an intermediary in this case. The Vault will burn tokens from the Router, so
// Router is both owner and spender (which doesn't need approval).
// Reusing `amountsOut` as input argument and function output to prevent stack too deep error.
(, amountsOut, ) = _vault.removeLiquidity(
RemoveLiquidityParams({
pool: step.pool,
from: address(this),
maxBptAmountIn: stepExactAmountIn,
minAmountsOut: amountsOut,
kind: RemoveLiquidityKind.SINGLE_TOKEN_EXACT_IN,
userData: params.userData
})
);
if (stepLocals.isLastStep) {
// The amount out for the last step of the path should be recorded for the return value, and the
// amount for the token should be sent back to the sender later on.
pathAmountsOut[i] = amountsOut[tokenIndex];
_currentSwapTokensOut().add(address(step.tokenOut));
_currentSwapTokenOutAmounts().tAdd(address(step.tokenOut), amountsOut[tokenIndex]);
} else {
// Input for the next step is output of current step.
stepExactAmountIn = amountsOut[tokenIndex];
// The token in for the next step is the token out of the current step.
stepTokenIn = step.tokenOut;
}
} else if (address(step.tokenOut) == step.pool) {
// Token out is BPT: add liquidity - Single token exact in (unbalanced).
(uint256[] memory exactAmountsIn, ) = _getSingleInputArrayAndTokenIndex(
step.pool,
stepTokenIn,
stepExactAmountIn
);
(, uint256 bptAmountOut, ) = _vault.addLiquidity(
AddLiquidityParams({
pool: step.pool,
to: stepLocals.isLastStep ? params.sender : address(_vault),
maxAmountsIn: exactAmountsIn,
minBptAmountOut: minAmountOut,
kind: AddLiquidityKind.UNBALANCED,
userData: params.userData
})
);
if (stepLocals.isLastStep) {
// The amount out for the last step of the path should be recorded for the return value.
// We do not need to register the amount out in _currentSwapTokenOutAmounts since the BPT
// is minted directly to the sender, so this step can be considered settled at this point.
pathAmountsOut[i] = bptAmountOut;
_currentSwapTokensOut().add(address(step.tokenOut));
_settledTokenAmounts().tAdd(address(step.tokenOut), bptAmountOut);
} else {
// Input for the next step is output of current step.
stepExactAmountIn = bptAmountOut;
// The token in for the next step is the token out of the current step.
stepTokenIn = step.tokenOut;
// If this is an intermediate step, BPT is minted to the Vault so we just get the credit.
_vault.settle(IERC20(step.pool), bptAmountOut);
}
} else {
// No BPT involved in the operation: regular swap exact in.
(, , uint256 amountOut) = _vault.swap(
VaultSwapParams({
kind: SwapKind.EXACT_IN,
pool: step.pool,
tokenIn: stepTokenIn,
tokenOut: step.tokenOut,
amountGivenRaw: stepExactAmountIn,
limitRaw: minAmountOut,
userData: params.userData
})
);
if (stepLocals.isLastStep) {
// The amount out for the last step of the path should be recorded for the return value, and the
// amount for the token should be sent back to the sender later on.
pathAmountsOut[i] = amountOut;
_currentSwapTokensOut().add(address(step.tokenOut));
_currentSwapTokenOutAmounts().tAdd(address(step.tokenOut), amountOut);
} else {
// Input for the next step is output of current step.
stepExactAmountIn = amountOut;
// The token in for the next step is the token out of the current step.
stepTokenIn = step.tokenOut;
}
}
}
}
}
function swapExactOutHook(
SwapExactOutHookParams calldata params
)
external
nonReentrant
onlyVault
returns (uint256[] memory pathAmountsIn, address[] memory tokensIn, uint256[] memory amountsIn)
{
(pathAmountsIn, tokensIn, amountsIn) = _swapExactOutHook(params);
_settlePaths(params.sender, params.wethIsEth);
}
function _swapExactOutHook(
SwapExactOutHookParams calldata params
) internal returns (uint256[] memory pathAmountsIn, address[] memory tokensIn, uint256[] memory amountsIn) {
// The deadline is timestamp-based: it should not be relied upon for sub-minute accuracy.
// solhint-disable-next-line not-rely-on-time
if (block.timestamp > params.deadline) {
revert SwapDeadline();
}
pathAmountsIn = _computePathAmountsIn(params);
// The hook writes current swap token and token amounts in.
// We copy that information to memory to return it before it is deleted during settlement.
tokensIn = _currentSwapTokensIn().values(); // Copy transient storage to memory
amountsIn = new uint256[](tokensIn.length);
for (uint256 i = 0; i < tokensIn.length; ++i) {
amountsIn[i] = _currentSwapTokenInAmounts().tGet(tokensIn[i]) + _settledTokenAmounts().tGet(tokensIn[i]);
_settledTokenAmounts().tSet(tokensIn[i], 0);
}
}
/**
* @dev Executes every swap path in the given input parameters.
* Computes inputs for the path, and aggregates them by token and amounts as well in transient storage.
*/
function _computePathAmountsIn(
SwapExactOutHookParams calldata params
) internal returns (uint256[] memory pathAmountsIn) {
pathAmountsIn = new uint256[](params.paths.length);
for (uint256 i = 0; i < params.paths.length; ++i) {
SwapPathExactAmountOut memory path = params.paths[i];
// This variable shall be updated at the end of each step to be used as input of the next one.
// The first value corresponds to the given amount out for the current path.
uint256 stepExactAmountOut = path.exactAmountOut;
// Paths may (or may not) share the same token in. To minimize token transfers, we store the addresses in
// a set with unique addresses that can be iterated later on.
//
// For example, if all paths share the same token in, the set will end up with only one entry.
// Since the path is 'given out', the output of the operation specified by the last step in each path will
// be added to calculate the amounts in for each token.
_currentSwapTokensIn().add(address(path.tokenIn));
// Backwards iteration: the exact amount out applies to the last step, so we cannot iterate from first to
// last. The calculated input of step (j) is the exact amount out for step (j - 1).
for (int256 j = int256(path.steps.length - 1); j >= 0; --j) {
SwapPathStep memory step = path.steps[uint256(j)];
SwapStepLocals memory stepLocals;
stepLocals.isLastStep = (j == 0);
stepLocals.isFirstStep = (uint256(j) == path.steps.length - 1);
// These two variables are set at the beginning of the iteration and are used as inputs for
// the operation described by the step.
uint256 stepMaxAmountIn;
IERC20 stepTokenIn;
if (stepLocals.isFirstStep) {
// The first step in the iteration is the last one in the given array of steps, and it
// specifies the output token for the step as well as the exact amount out for that token.
// Output amounts are stored to send them later on.
_currentSwapTokensOut().add(address(step.tokenOut));
_currentSwapTokenOutAmounts().tAdd(address(step.tokenOut), stepExactAmountOut);
}
if (stepLocals.isLastStep) {
// In backwards order, the last step is the first one in the given path.
// The given token in and max amount in apply for this step.
stepMaxAmountIn = path.maxAmountIn;
stepTokenIn = path.tokenIn;
} else {
// For every other intermediate step, no maximum input applies.
// The input token for this step is the output token of the previous given step.
// We use uint128 to prevent Vault's internal scaling from overflowing.
stepMaxAmountIn = _MAX_AMOUNT;
stepTokenIn = path.steps[uint256(j - 1)].tokenOut;
}
if (step.isBuffer) {
if (stepLocals.isLastStep && EVMCallModeHelpers.isStaticCall() == false) {
// The buffer will need this token to wrap/unwrap, so take it from the user in advance.
_takeTokenIn(params.sender, path.tokenIn, path.maxAmountIn, params.wethIsEth);
}
(, uint256 amountIn, ) = _vault.erc4626BufferWrapOrUnwrap(
BufferWrapOrUnwrapParams({
kind: SwapKind.EXACT_OUT,
direction: step.pool == address(stepTokenIn)
? WrappingDirection.UNWRAP
: WrappingDirection.WRAP,
wrappedToken: IERC4626(step.pool),
amountGivenRaw: stepExactAmountOut,
limitRaw: stepMaxAmountIn
})
);
if (stepLocals.isLastStep) {
pathAmountsIn[i] = amountIn;
// Since the token was taken in advance, returns to the user what is left from the
// wrap/unwrap operation.
_currentSwapTokensOut().add(address(stepTokenIn));
_currentSwapTokenOutAmounts().tAdd(address(stepTokenIn), path.maxAmountIn - amountIn);
// `settledTokenAmounts` is used to return the `amountsIn` at the end of the operation, which
// is only amountIn. The difference between maxAmountIn and amountIn will be paid during
// settle.
_settledTokenAmounts().tAdd(address(path.tokenIn), amountIn);
} else {
stepExactAmountOut = amountIn;
}
} else if (address(stepTokenIn) == step.pool) {
// Token in is BPT: remove liquidity - Single token exact out
// Remove liquidity is not transient when it comes to BPT, meaning the caller needs to have the
// required amount when performing the operation. In this case, the BPT amount needed for the
// operation is not known in advance, so we take a flashloan for all the available reserves.
//
// The last step is the one that defines the inputs for this path. The caller should have enough
// BPT to burn already if that's the case, so we just skip this step if so.
if (stepLocals.isLastStep == false) {
stepMaxAmountIn = _vault.getReservesOf(stepTokenIn);
_vault.sendTo(IERC20(step.pool), address(this), stepMaxAmountIn);
} else if (params.sender != address(this)) {
// The last step being executed is the first step in the swap path, meaning that it's the one
// that defines the inputs of the path.
//
// In that case, the sender must have the tokens. Therefore, we can transfer them
// to the Router, which acts as an intermediary. If the sender is the Router, we just skip this
// step (useful for queries).
_permit2.transferFrom(
params.sender,
address(this),
stepMaxAmountIn.toUint160(),
address(stepTokenIn)
);
}
(uint256[] memory exactAmountsOut, ) = _getSingleInputArrayAndTokenIndex(
step.pool,
step.tokenOut,
stepExactAmountOut
);
// Router is always an intermediary in this case. The Vault will burn tokens from the Router, so
// Router is both owner and spender (which doesn't need approval).
(uint256 bptAmountIn, , ) = _vault.removeLiquidity(
RemoveLiquidityParams({
pool: step.pool,
from: address(this),
maxBptAmountIn: stepMaxAmountIn,
minAmountsOut: exactAmountsOut,
kind: RemoveLiquidityKind.SINGLE_TOKEN_EXACT_OUT,
userData: params.userData
})
);
if (stepLocals.isLastStep) {
// BPT is burned instantly, so we don't need to send it to the Vault during settlement.
pathAmountsIn[i] = bptAmountIn;
_settledTokenAmounts().tAdd(address(stepTokenIn), bptAmountIn);
// Refund unused portion of BPT to the user.alias
if (bptAmountIn < stepMaxAmountIn && params.sender != address(this)) {
stepTokenIn.safeTransfer(address(params.sender), stepMaxAmountIn - bptAmountIn);
}
} else {
// Output for the step (j - 1) is the input of step (j).
stepExactAmountOut = bptAmountIn;
// Refund unused portion of BPT flashloan to the Vault.
if (bptAmountIn < stepMaxAmountIn) {
uint256 refundAmount = stepMaxAmountIn - bptAmountIn;
stepTokenIn.safeTransfer(address(_vault), refundAmount);
_vault.settle(stepTokenIn, refundAmount);
}
}
} else if (address(step.tokenOut) == step.pool) {
// Token out is BPT: add liquidity - Single token exact out.
(uint256[] memory stepAmountsIn, uint256 tokenIndex) = _getSingleInputArrayAndTokenIndex(
step.pool,
stepTokenIn,
stepMaxAmountIn
);
// Reusing `amountsIn` as input argument and function output to prevent stack too deep error.
(stepAmountsIn, , ) = _vault.addLiquidity(
AddLiquidityParams({
pool: step.pool,
to: stepLocals.isFirstStep ? params.sender : address(_vault),
maxAmountsIn: stepAmountsIn,
minBptAmountOut: stepExactAmountOut,
kind: AddLiquidityKind.SINGLE_TOKEN_EXACT_OUT,
userData: params.userData
})
);
if (stepLocals.isLastStep) {
// The amount out for the last step of the path should be recorded for the return value.
pathAmountsIn[i] = stepAmountsIn[tokenIndex];
_currentSwapTokenInAmounts().tAdd(address(stepTokenIn), stepAmountsIn[tokenIndex]);
} else {
stepExactAmountOut = stepAmountsIn[tokenIndex];
}
// The first step executed determines the outputs for the path, since this is given out.
if (stepLocals.isFirstStep) {
// Instead of sending tokens back to the Vault, we can just discount it from whatever
// the Vault owes the sender to make one less transfer.
_currentSwapTokenOutAmounts().tSub(address(step.tokenOut), stepExactAmountOut);
} else {
// If it's not the first step, BPT is minted to the Vault so we just get the credit.
_vault.settle(IERC20(step.pool), stepExactAmountOut);
}
} else {
// No BPT involved in the operation: regular swap exact out.
(, uint256 amountIn, ) = _vault.swap(
VaultSwapParams({
kind: SwapKind.EXACT_OUT,
pool: step.pool,
tokenIn: stepTokenIn,
tokenOut: step.tokenOut,
amountGivenRaw: stepExactAmountOut,
limitRaw: stepMaxAmountIn,
userData: params.userData
})
);
if (stepLocals.isLastStep) {
pathAmountsIn[i] = amountIn;
_currentSwapTokenInAmounts().tAdd(address(stepTokenIn), amountIn);
} else {
stepExactAmountOut = amountIn;
}
}
}
}
}
/***************************************************************************
Queries
***************************************************************************/
/// @inheritdoc IBatchRouter
function querySwapExactIn(
SwapPathExactAmountIn[] memory paths,
address sender,
bytes calldata userData
)
external
saveSender(sender)
returns (uint256[] memory pathAmountsOut, address[] memory tokensOut, uint256[] memory amountsOut)
{
for (uint256 i = 0; i < paths.length; ++i) {
paths[i].minAmountOut = 0;
}
return
abi.decode(
_vault.quote(
abi.encodeCall(
BatchRouter.querySwapExactInHook,
SwapExactInHookParams({
sender: address(this),
paths: paths,
deadline: type(uint256).max,
wethIsEth: false,
userData: userData
})
)
),
(uint256[], address[], uint256[])
);
}
/// @inheritdoc IBatchRouter
function querySwapExactOut(
SwapPathExactAmountOut[] memory paths,
address sender,
bytes calldata userData
)
external
saveSender(sender)
returns (uint256[] memory pathAmountsIn, address[] memory tokensIn, uint256[] memory amountsIn)
{
for (uint256 i = 0; i < paths.length; ++i) {
paths[i].maxAmountIn = _MAX_AMOUNT;
}
return
abi.decode(
_vault.quote(
abi.encodeCall(
BatchRouter.querySwapExactOutHook,
SwapExactOutHookParams({
sender: address(this),
paths: paths,
deadline: type(uint256).max,
wethIsEth: false,
userData: userData
})
)
),
(uint256[], address[], uint256[])
);
}
function querySwapExactInHook(
SwapExactInHookParams calldata params
)
external
nonReentrant
onlyVault
returns (uint256[] memory pathAmountsOut, address[] memory tokensOut, uint256[] memory amountsOut)
{
(pathAmountsOut, tokensOut, amountsOut) = _swapExactInHook(params);
}
function querySwapExactOutHook(
SwapExactOutHookParams calldata params
)
external
nonReentrant
onlyVault
returns (uint256[] memory pathAmountsIn, address[] memory tokensIn, uint256[] memory amountsIn)
{
(pathAmountsIn, tokensIn, amountsIn) = _swapExactOutHook(params);
}
}