> For the complete documentation index, see [llms.txt](https://city-protocol.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://city-protocol.gitbook.io/docs/developers/city-protocol-sdk/lending-and-borrowing.md).

# Lending and borrowing

Supply, borrow, repay and withdraw on Morpho Blue markets and Aave V3 pools.

`CityLendingClient` builds supply, borrow, repay and withdraw transactions on Morpho Blue markets and Aave V3 pools, and reads positions, market data and account health.

## How it works

`CityLendingClient` connects an application to established lending markets, starting with Morpho Blue and Aave V3, through one interface. Users supply assets to earn interest or borrow against collateral they have posted. The client handles transaction construction, prerequisite approvals and account-health reads: it builds supply, borrow and repay transactions on both protocols and withdraw transactions on Morpho Blue. It is built on Viem and works on any EVM chain where Morpho Blue markets or Aave V3 pools are deployed. Every transaction is built by the client and signed by the user's own wallet, so the flow stays non-custodial. Teams activate lending once identity, compliance and funding flows are stable, after the wallet and funding steps of the [suggested rollout order](/docs/developers/city-protocol-sdk.md#suggested-rollout-order).

## Build, then execute

The lending flow follows the same philosophy as the vault layer: build first, then execute. The client constructs every transaction an action needs, including token approvals and other prerequisite steps, and the caller signs and broadcasts them.

<figure><img src="https://3297077862-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJxNp2waFk6hg1G4lEQZu%2Fuploads%2Faa32eSRcbFuoOZO0Z201%2Ffig-sdk-lending.png?alt=media" alt="CityLendingClient builds for two lanes: Morpho Blue returns prerequisites and a bundler call sent in order, Aave V3 returns one transaction sent after the app&#x27;s own ERC-20 approval, and both end in account-health reads"><figcaption><p>Build, then execute. A Morpho Blue build returns its prerequisites and the bundler call, an Aave V3 build returns one transaction after your app's approval, and the user's wallet sends every transaction.</p></figcaption></figure>

{% stepper %}
{% step %}

### Initialize

Create `CityLendingClient` with a Viem `publicClient` for onchain reads, a `walletClient` for signing and the active `account`, passed explicitly. One instance serves both protocols.
{% endstep %}

{% step %}

### Build

Call the build method for the action. A Morpho Blue build returns `requirements`, the prerequisite transactions such as token approvals or authorizations, and `bundlerTx`, the protocol call itself. An Aave V3 build returns a single transaction.
{% endstep %}

{% step %}

### Execute

Send each transaction through the user's wallet. For Morpho Blue, send every transaction in `requirements` in order, then `bundlerTx`. For Aave V3, send a standard ERC-20 `approve` to the asset contract with the pool as spender before a supply or a repay, then the built transaction.
{% endstep %}

{% step %}

### Monitor

Read account health after execution: `getAaveUserAccountData` for an Aave V3 pool, and `getMorphoPosition` and `getMorphoMarket` for a Morpho Blue market.
{% endstep %}
{% endstepper %}

## Morpho Blue and Aave V3

| Feature             | Morpho Blue                                                                                                     | Aave V3                                                                                                                                        |
| ------------------- | --------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| Market structure    | Permissionless isolated markets, each defined by its collateral and loan asset pair, oracle and risk parameters | Shared pools with reserves supplied and borrowed under a unified risk model, plus efficiency mode and isolation mode for tighter capital usage |
| Build methods       | `buildSupply`, `buildBorrow`, `buildRepay` and `buildWithdraw`, by `marketId`                                   | `buildAaveSupplyTx`, `buildAaveBorrowTx` and `buildAaveRepayTx`, by `pool` and `asset`                                                         |
| Transaction pattern | Prerequisites, then the core transaction, batched through a bundler                                             | Single transaction                                                                                                                             |
| Approval handling   | Bundled in the `requirements` array                                                                             | Managed separately by the integrator                                                                                                           |
| Interest rates      | Set by market parameters                                                                                        | Dynamic, pool-governed                                                                                                                         |
| Health reads        | `getMorphoPosition` and `getMorphoMarket`                                                                       | `getAaveUserAccountData`                                                                                                                       |
| Best for            | Targeted exposure to specific pairs                                                                             | Broad lending and borrowing with established liquidity                                                                                         |

Both protocols run through the same `CityLendingClient` instance, so one application mixes Morpho Blue and Aave V3 with no separate client configuration.

## Monitoring account health

`getAaveUserAccountData` returns `totalCollateralBase`, `totalDebtBase`, `availableBorrowsBase`, `currentLiquidationThreshold`, `ltv` and `healthFactor`, with every base value in the pool's base currency. An Aave V3 borrow needs collateral supplied to the pool first, and the borrow amount must keep the health factor above 1. `getMorphoMarket` returns a market's total supply, total borrow and utilization, and `getMorphoPosition` returns a user's supplied assets, borrowed assets and collateral. A Morpho Blue borrow needs collateral posted in the market first, and a borrow that would take the position above the market's maximum LTV reverts onchain.

The borrow flow reads health on both sides of the transaction. Before the build, the interface shows the available borrow power and the health factor so the user chooses a safe amount; after execution, it reads account health again and updates. Pair these live reads with `CityPortfolioClient` subgraph queries for historical activity and indexed transaction logs.

{% content-ref url="/pages/gv9YkXS45v8mo8nSdpvE" %}
[Create a client](/docs/developers/city-protocol-sdk/lending-and-borrowing/create-a-client.md)
{% endcontent-ref %}

{% content-ref url="/pages/Q3xodd7JTn6vUWunXIIn" %}
[Morpho Blue](/docs/developers/city-protocol-sdk/lending-and-borrowing/morpho-blue.md)
{% endcontent-ref %}

{% content-ref url="/pages/gBHLTGvgjsREhevZTcN6" %}
[Aave V3](/docs/developers/city-protocol-sdk/lending-and-borrowing/aave-v3.md)
{% endcontent-ref %}

{% content-ref url="/pages/wVITJcvWXOUmtmcY3Hiw" %}
[Read methods](/docs/developers/city-protocol-sdk/lending-and-borrowing/read-methods.md)
{% endcontent-ref %}

{% content-ref url="/pages/mE6B5kheOelqH9UMph9q" %}
[Common frontend flows](/docs/developers/city-protocol-sdk/lending-and-borrowing/common-frontend-flows.md)
{% endcontent-ref %}
