->|enso<br/>route| aUSDC((aUSDC))
aUSDC -->|aave-v3<br/>borrow| WETH((WETH))
```
```ts lines theme={null}
const fromAddress = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
const USDC_ADDRESS = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const AUSDC_ADDRESS = "0x98C23E9d8f34FEFb1B7BD6a91B7FF122F4e16F5c";
const WETH_ADDRESS = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
const AAVE_V3 = "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2";
const usdcAmountIn = parseUnits("10000", 6).toString();
const wethAmountOut = parseUnits("0.025", 18).toString();
const chainId = 1;
const bundle = await client.getBundleData(
{
chainId,
fromAddress,
routingStrategy: "delegate",
receiver: fromAddress,
},
[
{
protocol: "enso",
action: "route",
args: {
amountIn: usdcAmountIn,
tokenIn: USDC_ADDRESS,
tokenOut: AUSDC_ADDRESS,
},
},
{
protocol: "aave-v3",
action: "borrow",
args: {
primaryAddress: AAVE_V3,
collateral: USDC_ADDRESS,
tokenOut: WETH_ADDRESS,
amountOut: wethAmountOut,
},
},
]
);
console.log(JSON.stringify(bundle.bundle, null, 2));
await sendEoa(approvalData.tx, approvalData.gas);
// sending the actual transaction
await sendEoa(bundle.tx, bundle.gas);
```
# Collateral Swap
Source: https://docs.enso.build/pages/use-cases/lending-markets/collateral-swap
Switch the collateral backing your debt without closing the debt 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>
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
```mermaid theme={null}
flowchart LR
aWETH((aWETH)) -->|aave-v3<br/>redeem| WETH((WETH))
WETH -->|enso<br/>route| WBTC((WBTC))
WBTC -->|aave-v3<br/>deposit| aWBTC((aWBTC))
```
```ts theme={null}
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));
```
# Deposit with Leverage
Source: https://docs.enso.build/pages/use-cases/lending-markets/deposit-with-leverage
Depositing with leverage amplifies exposure by redepositing the same collateral:
* deposits initial collateral (WETH),
* borrows against it (USDC),
* swaps the borrowed funds for more collateral (USDC → WETH),
* and repeats this process.
```mermaid theme={null}
flowchart LR
WETH((WETH)) -->|aave-v3<br/>deposit| aWETH((aWETH))
aWETH -->|aave-v3<br/>borrow| USDC((USDC))
USDC -->|enso<br/>route| WETH_2((WETH))
WETH_2 -->|aave-v3<br/>deposit| aWETH_2((aWETH))
```
```ts lines highlight={32-33, 42-43, 52-53} theme={null}
const chainId = 1;
const fromAddress = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
// Token and Protocol Addresses
const WETH: Address = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
const aWETH: Address = "0x4d5F47FA6A74757f35C14fD3a6Ef8E3C9BC514E8";
const USDC: Address = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
const aaveV3Pool: Address = "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2";
// Define amounts
const initialDeposit = parseUnits("1", 18);
const borrowAmount = parseUnits("1000", 6);
const bundle = await client.getBundleData(
{
chainId,
fromAddress,
routingStrategy: "delegate",
},
[
{
protocol: "aave-v3",
action: "deposit",
args: {
tokenIn: WETH,
tokenOut: aWETH,
amountIn: initialDeposit.toString(),
primaryAddress: aaveV3Pool,
},
},
{
protocol: "aave-v3",
action: "borrow",
args: {
collateral: aWETH,
tokenOut: USDC,
amountOut: borrowAmount.toString(),
primaryAddress: aaveV3Pool,
},
},
{
protocol: "enso",
action: "route",
args: {
tokenIn: USDC,
tokenOut: WETH,
amountIn: borrowAmount.toString(),
slippage: "100",
},
},
{
protocol: "aave-v3",
action: "deposit",
args: {
tokenIn: WETH,
tokenOut: aWETH,
amountIn: { useOutputOfCallAt: 2 },
primaryAddress: aaveV3Pool,
},
},
]
);
console.log(JSON.stringify(bundle.bundle, null, 2));
await sendSmartWallet(bundle.tx, bundle.gas);
```
# Overview
Source: https://docs.enso.build/pages/use-cases/lending-markets/index
Shortcuts for Lending Markets Operations
<div>
<Card title="Withdraw and Swap" href="/pages/use-cases/lending-markets/withdraw-and-swap">
Redeem asssets and swap them directly to an arbitrary token in a single transaction
</Card>
<Card title="Borrowing" href="/pages/use-cases/lending-markets/borrowing">
Borrow from Lending Protocols
</Card>
<Card title="Position Migration" href="/pages/use-cases/lending-markets/migration">
Migrate positions between protocols
</Card>
<Card title="Zap & Deposit to Collateral" href="/pages/use-cases/lending-markets/zap-to-collateral">
Automatically zap any input token as collateral to arbitrary vault tokens
</Card>
<Card title="Collateral Swap" href="/pages/use-cases/lending-markets/collateral-swap">
Switch the collateral backing your debt without closing the debt position
</Card>
<Card title="Deposit with Leverage" href="/pages/use-cases/lending-markets/deposit-with-leverage">
Deposit using leveraged position
</Card>
<Card title="Zap Repay" href="/pages/use-cases/lending-markets/zap-repay-behalf-of">
Zap to repay debt from any position
</Card>
<Card title="Debt Swap" href="/pages/use-cases/lending-markets/">
TBD
</Card>
<Card title="Repay With Collateral" href="/pages/use-cases/lending-markets/index">
TBD
</Card>
<Card title="Deleverage" href="/pages/use-cases/lending-markets/index">
TBD
</Card>
</div>
# Migration
Source: https://docs.enso.build/pages/use-cases/lending-markets/migration
Migrate positions between protocols
## Migrating with same base token
Move your USDC lending position from Aave to Compound while keeping the same underlying asset.
**Route mechanics**
* `redeem` existing USDC collateral on AAVE
* `deposit` retreived USDC to Compound `CUSDC` position
```mermaid theme={null}
flowchart LR
aUSDC((aUSDC)) -->|aave-v3<br/>redeem| USDC((USDC))
USDC -->|compound-v3<br/>deposit| cUSDC((cUSDC))
```
```ts lines theme={null}
const fromAddress = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
const USDC_ADDRESS = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const AUSDC_ADDRESS = "0x98C23E9d8f34FEFb1B7BD6a91B7FF122F4e16F5c";
const CUSDC_ADDRESS = "0xc3d688B66703497DAA19211EEdff47f25384cdc3";
const AAVE_V3 = "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2";
const aUsdcAmountIn = parseUnits("5000", 6).toString(); // 5,000 aUSDC to withdraw
const chainId = 1;
const bundle = await client.getBundleData(
{
chainId,
fromAddress,
routingStrategy: "delegate",
},
// Migrate USDC lending position from Aave to Compound
[
{
protocol: "aave-v3",
action: "redeem",
args: {
tokenIn: AUSDC_ADDRESS,
tokenOut: USDC_ADDRESS,
amountIn: aUsdcAmountIn,
primaryAddress: AAVE_V3,
},
},
{
protocol: "compound-v3",
action: "deposit",
args: {
tokenIn: USDC_ADDRESS,
tokenOut: CUSDC_ADDRESS,
amountIn: aUsdcAmountIn,
primaryAddress: CUSDC_ADDRESS,
},
},
]
);
console.log(JSON.stringify(bundle.bundle, null, 2));
await sendSmartWallet(bundle.tx, bundle.tx);
```
## Migrating to different base position
Switch Aave lending position from USDC collateral to WETH collateral in a single transaction.
Bundle design:
* `redeem` existing USDC collateral on AAVE
* swap USDC to WETH using `route` action
* `deposit` resulting WETH to AAVE (using the `{useOutputOfCallAt: 1}` to reference the swap)
```mermaid theme={null}
flowchart LR
aUSDC((aUSDC)) -->|aave-v3<br/>redeem| USDC((USDC))
USDC -->|enso<br/>route| WETH((WETH))
WETH -->|aave-v3<br/>deposit| aWETH((aWETH))
```
```ts lines theme={null}
const fromAddress = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
const USDC_ADDRESS = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const AUSDC_ADDRESS = "0x98C23E9d8f34FEFb1B7BD6a91B7FF122F4e16F5c";
const WETH_ADDRESS = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
const AWETH_ADDRESS = "0x4d5F47FA6A74757f35C14fD3a6Ef8E3C9BC514E8";
const AAVE_V3 = "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2";
const aUsdcAmountIn = parseUnits("3000", 6).toString();
const chainId = 1;
const bundle = await client.getBundleData(
{
chainId,
fromAddress,
routingStrategy: "delegate",
},
// Switch collateral within Aave from USDC to WETH
[
{
protocol: "aave-v3",
action: "redeem",
args: {
tokenIn: AUSDC_ADDRESS,
tokenOut: USDC_ADDRESS,
amountIn: aUsdcAmountIn,
primaryAddress: AAVE_V3,
},
},
{
protocol: "enso",
action: "route",
args: {
tokenIn: USDC_ADDRESS,
tokenOut: WETH_ADDRESS,
amountIn: aUsdcAmountIn,
},
},
{
protocol: "aave-v3",
action: "deposit",
args: {
tokenIn: WETH_ADDRESS,
tokenOut: AWETH_ADDRESS,
amountIn: {useOutputOfCallAt: 1},
primaryAddress: AAVE_V3,
},
},
]
);
console.log(JSON.stringify(bundle.bundle, null, 2));
await sendSmartWallet(bundle.tx, bundle.tx);
```
# Withdraw & Swap
Source: https://docs.enso.build/pages/use-cases/lending-markets/withdraw-and-swap
Redeem asssets and swap them directly to an arbitrary token in a single transaction
Enso [GET `route`](/pages/build/get-started/route) API allows withdrawing (redeeming) from a curated vault position to an arbitrary DeFi or base asset.
## Withdraw PT and swap to USDC
This route redeems ezETH PT into USDC, performing any necessary withdrawals and swaps.
[Try this route →](https://happypath.enso.build/?tokenIn=0xeEE8aED1957ca1545a0508AfB51b53cCA7e3c0d1\&outChainId=1\&chainId=1\&tokenOut=0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48\&amountIn=10000000000000000000)
**Route mechanics:**
* Redeem from `PT_ezETH` to get `SY_ezETH`
* Redeem from `SY_ezETH` to obtain the deposited `ezETH`
* Swap `ezETH` to desired `USDC`
```mermaid theme={null}
flowchart LR
PT((PT_Token)) -->|pendle-pt<br/>redeem| SY((SY_Token))
SY -->|pendle-sy<br/>redeem| ezETH((ezETH))
ezETH -->|barter<br/>swap| USDC((USDC))
```
```ts lines theme={null}
const PT_ezETH_25APR2024 = "0xeEE8aED1957ca1545a0508AfB51b53cCA7e3c0d1";
const USDC: Address = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const amountIn = parseUnits("10", 18).toString();
const chainId = 1;
const fromAddress = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
const routeParams: RouteParams = {
chainId,
amountIn: [amountIn],
fromAddress,
tokenIn: [PT_ezETH_25APR2024],
tokenOut: [USDC],
routingStrategy: "router",
receiver: fromAddress,
slippage: "500",
};
const route = await client.getRouteData(routeParams);
console.log(JSON.stringify(route, null, 2));
const approvalData = await client.getApprovalData({
chainId,
fromAddress,
amount: amountIn,
tokenAddress: PT_ezETH_25APR2024,
});
await sendEoa(approvalData.tx, approvalData.gas);
// sending the actual transaction
await sendEoa(route.tx, route.gas);
```
## Withdraw ADAI to USDC
This sample withdraws `ADAI` and swaps it to USDC in a single transaction.
[Try this route →](https://happypath.enso.build/?tokenIn=0xeEE8aED1957ca1545a0508AfB51b53cCA7e3c0d1\&outChainId=1\&chainId=1\&tokenOut=0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48\&amountIn=10000000000000000000)
**Route mechanics:**
* Withdraw deposited `ADAI` to the underlying `DAI`
* Swaps `DAI` to `USDC`
* The `receiver` gets the resulting funds
```mermaid theme={null}
flowchart LR
aDAI((aDAI)) -->|aave-v3<br/>redeem| DAI((DAI))
DAI -->|swap| USDC((USDC))
```
```ts lines theme={null}
const ADAI_ADDRESS = "0x018008bfb33d285247A21d44E50697654f754e63";
const USDC: Address = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const amountIn = parseUnits("100", 18).toString();
const chainId = 1;
const fromAddress = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
const routeParams: RouteParams = {
chainId,
destinationChainId: chainId,
amountIn: [amountIn],
fromAddress,
tokenIn: [ADAI_ADDRESS],
tokenOut: [USDC],
routingStrategy: "router",
receiver: fromAddress,
slippage: "500",
};
const route = await client.getRouteData(routeParams);
console.log(JSON.stringify(route, null, 2));
const approvalData = await client.getApprovalData({
chainId,
fromAddress,
amount: amountIn,
tokenAddress: ADAI_ADDRESS,
});
await sendEoa(approvalData.tx, approvalData.gas);
// sending the actual transaction
await sendEoa(route.tx, route.gas);
```
# Zap Repay Debt
Source: https://docs.enso.build/pages/use-cases/lending-markets/zap-repay-behalf-of
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);
```
# Zap to Collateral
Source: https://docs.enso.build/pages/use-cases/lending-markets/zap-to-collateral
Automatically zap any input token as collateral to arbitrary vault tokens
Automatically convert any token into the required vault token and deposit as collateral in a single transaction.
In this example, we're zapping WETH to an AAVE `aUSDT` position.
```mermaid theme={null}
flowchart LR
WETH((WETH)) -->|barter<br/>swap| USDT((USDT))
USDT -->|aave-v3<br/>deposit| aUSDT((aUSDT))
```
```typescript lines theme={null}
const WETH: Address = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"; // WETH
const aUSDT: Address = "0x23878914EFE38d27C4D67Ab83ed1b93A74D4086a";
const fromAddress = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045" as Address;
const routeParams: RouteParams = {
tokenIn: [WETH],
tokenOut: [aUSDT],
amountIn: ["1000000000000000000"],
chainId: 1,
routingStrategy: "router",
fromAddress,
};
const approvalData = await client.getApprovalData({
amount: routeParams.amountIn[0],
chainId: 1,
tokenAddress: WETH,
fromAddress,
});
const route = await client.getRouteData(routeParams);
await sendEoa(approvalData.tx, approvalData.gas);
await sendEoa(route.tx, route.gas);
```
## Zap Deposits
### **Zap ETH to lend on Aave V3 on Arbitrum**
Deposit WETH into Aave on Arbitrum.
[Try this route →](https://happypath.enso.build/?tokenIn=0x82aF49447D8a07e3bd95BD0d56f35241523fBab1\&outChainId=42161\&chainId=42161\&tokenOut=0xe50fA9b3c56FfB159cB0FCA61F5c9D750e8128c8\&amountIn=1000000000000000000)
**Route Mechanics:**
* **Deposit into Aave V3**: The input `WETH` is deposited into the Aave V3 protocol, and in return, you receive `aWETH`, representing your lending position.
```mermaid theme={null}
flowchart LR
WETH((WETH)) -->|aave-v3<br/>deposit| aWETH((aWETH))
```
```javascript lines theme={null}
const WETH = "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1";
const aWETH = "0xe50fA9b3c56FfB159cB0FCA61F5c9D750e8128c8";
const routeParams = {
fromAddress: userAddress,
receiver: userAddress,
chainId: 42161, // Arbitrum
amountIn: ["1000000000000000000"], // 1 WETH
tokenIn: [WETH],
tokenOut: [aWETH],
routingStrategy: "router",
};
const route = await client.getRouteData(routeParams);
const approval = await client.getApprovalData({
chainId: 42161,
fromAddress: userAddress,
amount: "1000000000000000000",
tokenAddress: WETH,
});
// Approval transaction for `router` strategy
await sendEoa(approval.tx, approval.gas);
// Sending the actual transaction
await sendEoa(route.tx, route.gas);
```
***
### **Zap ETH to lend on Euler V2 on Mainnet**
Supply ETH to the Euler V2 lending market on the Ethereum Mainnet. This route provides a seamless way to start earning interest on your ETH.
[Try this route →](https://happypath.enso.build/?tokenIn=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE\&outChainId=1\&chainId=1\&tokenOut=0xb3b36220fA7d12f7055dab5c9FD18E860e9a6bF8\&amountIn=1000000000000000000)
**Route Mechanics:**
* **Wrap**: The input `ETH` is first wrapped into `WETH`.
* **Deposit**: The `WETH` is then deposited into the Euler V2 protocol, and you receive `eWETH` in return.
```mermaid theme={null}
flowchart LR
ETH((ETH)) -->|wrapped-native<br/>deposit| WETH((WETH))
WETH -->|euler-v2<br/>deposit| eWETH((eWETH))
```
```javascript lines theme={null}
const ETH = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
const eWETH = "0xb3b36220fA7d12f7055dab5c9FD18E860e9a6bF8";
const routeParams = {
fromAddress: userAddress,
receiver: userAddress,
chainId: 1, // Ethereum
amountIn: ["1000000000000000000"], // 1 ETH
tokenIn: [ETH],
tokenOut: [eWETH],
routingStrategy: "router",
};
const route = await client.getRouteData(routeParams);
// Approval transaction for `router` strategy
const approval = await client.getApprovalData({
chainId: 1,
fromAddress: userAddress,
amount: "1000000000000000000",
tokenAddress: ETH,
});
// Approval transaction for `router` strategy
await sendEoa(approval.tx, approval.gas);
// Sending the actual transaction
await sendEoa(route.tx, route.gas);
```
***
### **Lend USDC on Spark on Gnosis**
Supply USDC to Spark's money market on the Gnosis chain. This route provides a direct way to enter a lending position.
[Try this route →](https://happypath.enso.build/?tokenIn=0xDDAfbb505ad214D7b80b1f830fcCc89B60fb7A83\&outChainId=100\&chainId=100\&tokenOut=0x5850d127a04ed0b4f1fcdfb051b3409fb9fe6b90\&amountIn=100000000)
**Route Mechanics:**
* **Deposit into Spark**: Your `USDC` is deposited into the Spark-lend protocol, a프로젝트로 돌아가기