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.
When using repay with router routing strategy, you must specify the debt holder via onBehalfOf argument.
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)
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);