Skip to content

Commit

Permalink
docs: update docs at _swap and testSplitOutputCyclicSwap
Browse files Browse the repository at this point in the history
  • Loading branch information
royvardhan committed Mar 6, 2025
1 parent ecf425a commit ae99f80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
6 changes: 5 additions & 1 deletion foundry/src/TychoRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,15 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
* - The indices of the input and output tokens (via `tokenInIndex()` and `tokenOutIndex()`).
* - The portion of the available amount to be used for the swap, indicated by the `split` value.
*
* Two important notes:
* Three important notes:
* - The contract assumes that token indexes follow a specific order: the sell token is at index 0, followed by any
* intermediary tokens, and finally the buy token.
* - A `split` value of 0 is interpreted as 100% of the available amount (i.e., the entire remaining balance).
* This means that in scenarios without explicit splits the value should be 0, and when splits are present,
* the last swap should also have a split value of 0.
* - In case of cyclic swaps, the output token is the same as the input token.
* `cyclicSwapAmountOut` is used to track the amount of the output token, and is updated when
* the `tokenOutIndex` is 0.
*
* @param amountIn The initial amount of the sell token to be swapped.
* @param nTokens The total number of tokens involved in the swap path, used to initialize arrays for internal tracking.
Expand Down Expand Up @@ -346,6 +349,7 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
currentAmountOut = _callExecutor(
swapData.executor(), currentAmountIn, swapData.protocolData()
);
// Checks if the output token is the same as the input token
if (tokenOutIndex == 0) {
cyclicSwapAmountOut += currentAmountOut;
} else {
Expand Down
22 changes: 11 additions & 11 deletions foundry/test/TychoRouter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1232,11 +1232,11 @@ contract TychoRouterTest is TychoRouterTestSetup {
function testSplitInputCyclicSwap() public {
// This test has start and end tokens that are the same
// The flow is:
// ┌─── WETH (Pool 1) ───┐
// │ │
// USDC (60% split) ─┤ ├─> USDC
// │ │
// └─── WETH (Pool 2) ───┘
// ┌─── WETH (USV3 Pool 1) ───┐
// │
// USDC (60% split) ─┤ ├─> USDC
// │
// └─── WETH (USV3 Pool 2) ───┘
//
// 60% of USDC is swapped to WETH using Pool 1, then swapped back to USDC using Pool 2
// 40% of USDC is swapped to WETH using Pool 2, then swapped back to USDC using Pool 1
Expand Down Expand Up @@ -1300,12 +1300,12 @@ contract TychoRouterTest is TychoRouterTestSetup {
function testSplitOutputCyclicSwap() public {
// This test has start and end tokens that are the same
// The flow is:
// ┌─── WETH (Pool 2, 60% split) ───┐
// │ │
// USDC ----─┤ ├─> USDC
// │ │
// └─── WETH (Pool 2, the rest) ───┘
//
// ┌─── (USV3 Pool 1, 60% split) ───┐
// │ │
// USDC ──(USV2)── ┤WETH ├─> USDC
// │ │
// └─── (USV3 Pool 2, 40% split) ───┘

uint256 amountIn = 100 * 10 ** 6;
deal(USDC_ADDR, tychoRouterAddr, amountIn);

Expand Down

0 comments on commit ae99f80

Please sign in to comment.