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.
To swap the collateral, we need three actions:
  • redeem a part of exising WETH collateral for the aWETH position
  • route to swap the redeemed collateral (WETH) to a different position (WBTC)
  • deposit the swapped position (WBTC) as collateral for the existing debt
const fromAddress = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045" as Address;
const aWETH = "0x4d5F47FA6A74757f35C14fD3a6Ef8E3C9BC514E8";
const WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
const WBTC = "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599";
const aWBTC = "0x5Ee5bf7ae06D1Be5997A1A72006FE6C607eC6DE8";

// aave v3 position manager
const AAVE_V3 = "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2"; 

const bundle = await client.getBundleData(
  {
    chainId: 1,
    fromAddress,
    routingStrategy: "delegate",
  },
  [
    {
      protocol: "aave-v3",
      action: "redeem",
      args: {
        primaryAddress: AAVE_V3,
        tokenIn: aWETH, 
        tokenOut: WETH, 
        amountIn: "1000000000000000000",
      },
    },
    {
      protocol: "enso",
      action: "route",
      args: {
        tokenIn: WETH, 
        tokenOut: WBTC,
        amountIn: { useOutputOfCallAt: 0 },
      },
    },
    {
      protocol: "aave-v3",
      action: "deposit",
      args: {
        primaryAddress: AAVE_V3,
        tokenIn: WBTC, 
        tokenOut: aWBTC, 
        amountIn: { useOutputOfCallAt: 1 },
      },
    },
  ]
);
console.log(JSON.stringify(bundle, null, 2));