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

# Zap Repay Debt

> Zap to repay debt from any position

<Tip>
  Note on Approvals: When using `routingStrategy`: `router`, you must first approve the Enso contract to spend your tokens.
  The code samples below include the necessary `client.getApprovalData()` step, which generates the required approval transaction
  to be signed before the main routing transaction.
</Tip>

<Warning>
  When using `repay` with `router` routing strategy, you must specify the debt holder via `onBehalfOf` argument.
</Warning>

Enabling zap to repay debt requires two steps:

* Suppose there's USDC debt
* `route` from arbitrary token (WETH) to the debt token (USDC)
* `repay` debt using the swap output by using the dynamic amount reference (`useOutputOfCallAt`)

```mermaid theme={null}
flowchart LR
    WETH((WETH)) --> SG1
    
    subgraph SG1["On-Behalf Repayment"]
        direction LR
        A1[enso.route] --> USDC((USDC))
        A2[aave-v3.repay<br/>onBehalfOf]
        USDC --> A2
    end
```

```ts lines highlight={16-17, 26-27, 31, 32, 46} theme={null}
const AAVE_v3 = "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2";
const fromAddress = "0x93621dca56fe26cdee86e4f6b18e116e9758ff11";
const USDC_ADDRESS = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
const WETH_ADDRESS: Address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";

const WETH_AMOUNT_IN = parseUnits("0.001", 18).toString();

const bundle = await client.getBundleData(
  {
    chainId: 1,
    fromAddress,
    routingStrategy: "router",
  },
  [
    {
      protocol: "enso",
      action: "route",
      args: {
        tokenIn: WETH_ADDRESS,
        tokenOut: USDC_ADDRESS,
        amountIn: WETH_AMOUNT_IN,
      },
    },
    {
      // FIX: could not simulate TX
      protocol: "aave-v3",
      action: "repay",
      args: {
        primaryAddress: AAVE_v3,
        tokenIn: USDC_ADDRESS,
        amountIn: { useOutputOfCallAt: 0 },
        onBehalfOf: fromAddress,
      },
    },
  ]
);
const approval = await client.getApprovalData({
  tokenAddress: WETH_ADDRESS,
  amount: WETH_AMOUNT_IN,
  chainId: 1,
  fromAddress,
});

console.log(JSON.stringify(bundle.bundle));

await sendEoa(approval.tx, approval.gas);
await sendEoa(bundle.tx, bundle.gas);
```
