> ## Documentation Index
> Fetch the complete documentation index at: https://base-a060aa97-docs-devrel-859-eth-getproof-historical-proof.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate Borrowing

> Let users borrow USDC against WETH collateral with Morpho or Aave on Base.

Open a collateralized loan through a third-party protocol on Base. A safe integration shows collateral value, debt, liquidation parameters, and health before and after every user-signed action.

## Supply Collateral and Borrow

`clients.ts` creates Base mainnet public and wallet clients from a server-side `PRIVATE_KEY` that every example imports.

<Info>
  These snippets are illustrative. They show the shape of a borrowing integration on Base, not production-ready code. Follow each protocol's own documentation for current addresses, SDK details, and risk parameters: [Morpho](https://docs.morpho.org) and [Aave](https://aave.com/docs).
</Info>

<Tabs>
  <Tab title="Morpho">
    Prepare one Morpho bundle that supplies WETH collateral and borrows USDC from the WETH/USDC market.

    ```typescript borrow-morpho.ts lines wrap expandable highlight={5} theme={null}
    import { publicClient, walletClient } from './clients.js';
    import { type MarketId } from '@morpho-org/blue-sdk';
    import { fetchMarketParams } from '@morpho-org/blue-sdk-viem';
    import {
      isRequirementSignature,
      morphoViemExtension,
    } from '@morpho-org/morpho-sdk';
    import { parseUnits } from 'viem';
    import { base } from 'viem/chains';

    const marketId =
      '0x8793cf302b8ffd655ab97bd1c695dbd967807e8367a65cb2f4edaf1380ba1bda' as MarketId;
    const user = walletClient.account.address;
    const client = publicClient.extend(morphoViemExtension());
    const params = await fetchMarketParams(marketId, publicClient);
    const market = client.morpho.blue(params, base.id);
    const positionData = await market.getPositionData(user);
    const action = market.supplyCollateralBorrow({
      amount: parseUnits('2', 18),
      borrowAmount: parseUnits('2000', 6),
      userAddress: user,
      positionData,
    });

    const signatures = [];
    for (const requirement of await action.getRequirements()) {
      if (isRequirementSignature(requirement)) {
        signatures.push(await requirement.sign(walletClient, user));
      } else {
        const hash = await walletClient.sendTransaction(requirement);
        await publicClient.waitForTransactionReceipt({ hash });
      }
    }
    const request = action.buildTx(signatures);
    await publicClient.call({ account: user, ...request });
    const hash = await walletClient.sendTransaction(request);
    await publicClient.waitForTransactionReceipt({ hash });
    ```
  </Tab>

  <Tab title="Aave">
    Use Aave's official address book with the Base Aave V3 Pool. Supply WETH, explicitly enable it as collateral, then borrow USDC at the variable rate.

    ```typescript borrow-aave.ts lines wrap expandable highlight={16,21,24,40,46} theme={null}
    import { publicClient, walletClient } from './clients.js';
    import { AaveV3Base } from '@aave-dao/aave-address-book';
    import { parseAbi, parseUnits } from 'viem';

    const user = walletClient.account;
    const weth = AaveV3Base.ASSETS.WETH.UNDERLYING;
    const usdc = AaveV3Base.ASSETS.USDC.UNDERLYING;
    const collateral = parseUnits('2', 18);
    const erc20Abi = parseAbi(['function approve(address,uint256) returns (bool)']);
    const poolAbi = parseAbi([
      'function supply(address,uint256,address,uint16)',
      'function setUserUseReserveAsCollateral(address,bool)',
      'function borrow(address,uint256,uint256,uint16,address)',
    ]);

    const approval = await publicClient.simulateContract({
      account: user, address: weth, abi: erc20Abi, functionName: 'approve',
      args: [AaveV3Base.POOL, collateral],
    });
    await publicClient.waitForTransactionReceipt({
      hash: await walletClient.writeContract(approval.request),
    });

    const supplied = await publicClient.simulateContract({
      account: user, address: AaveV3Base.POOL, abi: poolAbi,
      functionName: 'supply', args: [weth, collateral, user.address, 0],
    });
    await publicClient.waitForTransactionReceipt({
      hash: await walletClient.writeContract(supplied.request),
    });

    const enabled = await publicClient.simulateContract({
      account: user, address: AaveV3Base.POOL, abi: poolAbi,
      functionName: 'setUserUseReserveAsCollateral', args: [weth, true],
    });
    await publicClient.waitForTransactionReceipt({
      hash: await walletClient.writeContract(enabled.request),
    });

    const loan = await publicClient.simulateContract({
      account: user, address: AaveV3Base.POOL, abi: poolAbi,
      functionName: 'borrow',
      args: [usdc, parseUnits('2000', 6), 2n, 0, user.address],
    });
    await publicClient.waitForTransactionReceipt({
      hash: await walletClient.writeContract(loan.request),
    });
    ```
  </Tab>
</Tabs>

<Warning>
  Collateral prices, oracle updates, interest, and protocol parameters can move a position toward liquidation. Re-fetch and display health immediately before signing, and warn clearly before a transaction creates unsafe debt.
</Warning>

## See Also

<CardGroup cols={2}>
  <Card title="Integrate Lending" icon="landmark" href="/build-on-base/integrate-defi/integrate-lending">
    Let users supply USDC to a money market.
  </Card>

  <Card title="Integrate an Earn Product" icon="vault" href="/build-on-base/integrate-defi/integrate-earn-product">
    Route deposits into yield-bearing vaults.
  </Card>
</CardGroup>
