# Aave V3 Health Factor Preview

Preview how a supply, borrow, repay or withdraw changes a user’s health factor before sending it.

The health factor compares the liquidation-weighted collateral to the debt:

A health factor below 1 makes the position eligible for liquidation.

> [!NOTE]
> The health factor is null unless the user has at least one active borrow.

To preview how a user’s health factor will change before executing a market operation (supply, borrow, repay, withdraw), use the `useAaveHealthFactorPreview` hook in React, the `healthFactorPreview` client action in TypeScript, or the `healthFactorPreview` query in GraphQL.

<tabgroup>
<tab label="React">

```tsx title="Borrow"
import { useWalletClient } from "wagmi";
import { useAaveHealthFactorPreview, bigDecimal, evmAddress } from "@aave/react";

// …

const { data: walletClient } = useWalletClient();
const [preview, { loading, error }] = useAaveHealthFactorPreview();

const execute = async () => {
  const result = await preview({
    action: {
      borrow: {
        market: reserve.market.address,
        amount: {
          erc20: {
            currency: reserve.underlyingToken.address,
            value: bigDecimal(42), // 42 WETH
          },
        },
        sender: evmAddress(walletClient!.account.address),
        chainId: reserve.market.chain.chainId,
      },
    },
  });

  if (result.isErr()) {
    console.error(result.error);
  } else {
    console.log(result.value.after); // 1.5 or null
  }
};
```

```tsx title="Supply"
import { useWalletClient } from "wagmi";
import { useAaveHealthFactorPreview, bigDecimal, evmAddress } from "@aave/react";

// …

const { data: walletClient } = useWalletClient();
const [preview, { loading, error }] = useAaveHealthFactorPreview();

const execute = async () => {
  const result = await preview({
    action: {
      supply: {
        market: reserve.market.address,
        amount: {
          erc20: {
            currency: reserve.underlyingToken.address,
            value: bigDecimal(42), // 42 WETH
          },
        },
        sender: evmAddress(walletClient!.account.address),
        chainId: reserve.market.chain.chainId,
      },
    },
  });

  if (result.isErr()) {
    console.error(result.error);
  } else {
    console.log(result.value.after); // 1.5 or null
  }
};
```

```tsx title="Repay"
import { useWalletClient } from "wagmi";
import { useAaveHealthFactorPreview, bigDecimal, evmAddress } from "@aave/react";

// …

const { data: walletClient } = useWalletClient();
const [preview, { loading, error }] = useAaveHealthFactorPreview();

const execute = async () => {
  const result = await preview({
    action: {
      repay: {
        market: position.market.address,
        amount: {
          erc20: {
            currency: position.currency.address,
            value: {
              exact: bigDecimal(1), // 1 WETH
            },
          },
        },
        sender: evmAddress(walletClient!.account.address),
        chainId: position.market.chain.chainId,
      },
    },
  });

  if (result.isErr()) {
    console.error(result.error);
  } else {
    console.log(result.value.after); // 1.5 or null
  }
};
```

```tsx title="Withdraw"
import { useWalletClient } from "wagmi";
import { useAaveHealthFactorPreview, evmAddress } from "@aave/react";

// …

const { data: walletClient } = useWalletClient();
const [preview, { loading, error }] = useAaveHealthFactorPreview();

const execute = async () => {
  const result = await preview({
    action: {
      withdraw: {
        market: position.market.address,
        amount: {
          erc20: {
            currency: position.currency.address,
            value: {
              max: true,
            },
          },
        },
        sender: evmAddress(walletClient!.account.address),
        chainId: position.market.chain.chainId,
      },
    },
  });

  if (result.isErr()) {
    console.error(result.error);
  } else {
    console.log(result.value.after); // 1.5 or null
  }
};
```

</tab>
<tab label="TypeScript">

```ts title="Borrow"
import { bigDecimal, evmAddress } from "@aave/client";
import { healthFactorPreview } from "@aave/client/actions";

import { client } from "./client";

const result = await healthFactorPreview(client, {
  action: {
    borrow: {
      market: reserve.market.address,
      amount: {
        erc20: {
          currency: reserve.underlyingToken.address,
          value: bigDecimal(42), // 42 WETH
        },
      },
      sender: evmAddress(wallet.account.address),
      chainId: reserve.market.chain.chainId,
    },
  },
});

if (result.isErr()) {
  console.error(result.error);
} else {
  console.log(result.value.after); // 1.5 or null
}
```

```ts title="Supply"
import { bigDecimal, evmAddress } from "@aave/client";
import { healthFactorPreview } from "@aave/client/actions";

import { client } from "./client";

const result = await healthFactorPreview(client, {
  action: {
    supply: {
      market: reserve.market.address,
      amount: {
        erc20: {
          currency: reserve.underlyingToken.address,
          value: bigDecimal(42), // 42 WETH
        },
      },
      sender: evmAddress(wallet.account.address),
      chainId: reserve.market.chain.chainId,
    },
  },
});

if (result.isErr()) {
  console.error(result.error);
} else {
  console.log(result.value.after); // 1.5 or null
}
```

```ts title="Repay"
import { bigDecimal, evmAddress } from "@aave/client";
import { healthFactorPreview } from "@aave/client/actions";

import { client } from "./client";

const result = await healthFactorPreview(client, {
  action: {
    repay: {
      market: position.market.address,
      amount: {
        erc20: {
          currency: position.currency.address,
          value: {
            exact: bigDecimal(1), // 1 WETH
          },
        },
      },
      sender: evmAddress(wallet.account.address),
      chainId: position.market.chain.chainId,
    },
  },
});

if (result.isErr()) {
  console.error(result.error);
} else {
  console.log(result.value.after); // 1.5 or null
}
```

```ts title="Withdraw"
import { evmAddress } from "@aave/client";
import { healthFactorPreview } from "@aave/client/actions";

import { client } from "./client";

const result = await healthFactorPreview(client, {
  action: {
    withdraw: {
      market: position.market.address,
      amount: {
        erc20: {
          currency: position.currency.address,
          value: {
            max: true,
          },
        },
      },
      sender: evmAddress(wallet.account.address),
      chainId: position.market.chain.chainId,
    },
  },
});

if (result.isErr()) {
  console.error(result.error);
} else {
  console.log(result.value.after); // 1.5 or null
}
```

</tab>
<tab label="GraphQL">

```graphql title="Query"
query {
  healthFactorPreview(
    request: {
      action: {
        borrow: {
          market: "0x87870bca3f3fd6335c3f4ce8392d69350b4fa4e2"
          amount: {
            erc20: {
              currency: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
              value: "42"
            }
          }
          sender: "0x742d35cc6e5c4ce3b69a2a8c7c8e5f7e9a0b1234"
          chainId: 1
        }
        # OR
        # supply: {
        #   …
        # }

        # OR
        # repay: {
        #   …
        # }

        # OR
        # withdraw: {
        #   …
        # }
      }
    }
  ) {
    before
    after
  }
}
```

```json title="Response"
{
  "data": {
    "healthFactorPreview": {
      "after": "1.5",
      "before": null
    }
  }
}
```

</tab>
</tabgroup>
