> ## Documentation Index
> Fetch the complete documentation index at: https://docs.eco.com/llms.txt
> Use this file to discover all available pages before exploring further.

# actions: Megas swap

> Megas swap: 4 exports of the actions module of @eco-incorp/sauce, including megasSwapToSauce, megasSwapToSauceSource, megasUsdcUsdtSwapToSauce, MegasSwapOpts.

`@eco-incorp/sauce/actions` lowers routing intents into Sauce programs and carries the AMM and bridge action primitives.

```typescript theme={null}
import { actionsToSauce } from "@eco-incorp/sauce/actions";
```

This page covers `actions/dist/megas-swap.d.ts`.

<Note>
  Generated from the type declarations shipped in `@eco-incorp/sauce` 0.99.4. Each entry shows the package authors' JSDoc and declaration for this SDK version. For runtime compatibility and deployed addresses, see [architecture and deployments](/programmable-transactions/sauce/architecture-and-deployments).
</Note>

### `megasSwapToSauce`

*Function* · `actions/dist/megas-swap.d.ts`

```typescript theme={null}
export declare function megasSwapToSauce(candidates: SwapAction[], amountIn: bigint, n: number, opts: MegasSwapOpts): Uint8Array[];
```

### `megasSwapToSauceSource`

*Function* · `actions/dist/megas-swap.d.ts`

Emit the SauceScript for a greedy split-routing across `candidates`.

Declared functions:
quote\_i(amount)        One per candidate; invokes the protocol quoter with a runtime amount.
quote\_any(idx, amount) if/else dispatch to the matching quote\_i.
swap\_any(idx, amount)  Same dispatch, but executing the swap.
main()                 The three passes below.

Main algorithm (all three passes are real `for` loops - the per-candidate state lives in
transient storage keyed by `i`):

1. Init:    for i in 0..N-1 \{ marg\[i] = quote\_any(i, bucket); }
   used\[i] and total\[i] default to 0 (TLOAD of untouched keys).
2. Greedy:  for iter in 0..n-1 \{
   for i in 0..N-1 argmax → (bestMarg, bestIdx);
   used\[bestIdx]  += 1;
   total\[bestIdx] += marg\[bestIdx];          // cached, no call
   marg\[bestIdx]   = quote\_any(bestIdx, (used\[bestIdx]+1)\*bucket)
   * total\[bestIdx];
     }
3. Execute: for i in 0..N-1 if (used\[i] > 0) swap\_any(i, used\[i]\*bucket);

SIZE, CHANGED: the retired compiler kept every main-level pass a fixed-size loop, so the program
was O(N). The Rust compiler's constant folder UNROLLS a loop whose bound it knows, and `N`/`n`
are compile-time constants here, so the emitted main is O(N + n·N) instructions. Behaviour is
identical; only the byte count grows. Reach for a runtime bound if that ever matters.

Requirements:

* n >= 1. n=1 reduces to "pick the single best pool at full size".
* Every candidate's quote must expose `toSauceDynamic` (fixed amount offset).
* Swap builders must accept a runtime amountIn expression; UniswapV4 does
  not and will embed a stale compile-time amount.

```typescript theme={null}
export declare function megasSwapToSauceSource(candidates: SwapAction[], amountIn: bigint, n: number, opts: MegasSwapOpts): string;
```

### `megasUsdcUsdtSwapToSauce`

*Function* · `actions/dist/megas-swap.d.ts`

Pre-configured greedy split router for USDC → USDT on Ethereum mainnet.

Candidates (all pre-populated, caller never picks addresses or pool IDs):

* UniswapV3 at 1 bps (pool 0x38…)
* UniswapV3 at 5 bps (pool 0x7858…)
* Curve stableswap 3pool (DAI/USDC/USDT, indices 1→2)
* Uniswap V2 router via \[USDC, USDT] pair

The returned bytecode is self-contained and can be fed straight into
`Sauce.cook(bytes[])`: it approves all 3 spenders for `amountIn`, runs the
split-swap loop for `n` buckets, and sends the USDT output to `recipient`.

Candidates whose pools are thin (UniV3 5 bps, shallow UniV2 pair) are still included - the
greedy algorithm handles them by never routing buckets that direction. That costs `N + n` extra
quoter CALLs worth of gas; drop them manually if you want to save.

Balancer V2 is NOT a candidate: its only quote is `queryBatchSwap`, which mutates state and
reverts to return, so it cannot be called on-chain at all.

```typescript theme={null}
export declare function megasUsdcUsdtSwapToSauce(amountIn: bigint, n: number, recipient: Addr, minTotalOut: string): Uint8Array[];
```

### `MegasSwapOpts`

*Interface* · `actions/dist/megas-swap.d.ts`

```typescript theme={null}
export interface MegasSwapOpts extends QuoteOpts {
    prepend?: ApproveAction[];
    minTotalOut: string;
    sweep?: {
        token: Addr;
        to: Addr;
    };
}
```
