# Repay a loan

### Prerequisites

* A whitelisted borrower with current debt on an inventory pool

### Process Overview

1. Approve the inventory pool contract to spend your tokens
2. Call the `repay()` function directly on the inventory pool contract

### Step 1: Approve Token Spending

First, get the token address from the inventory pool and approve it to spend your tokens:

```javascript
// Using ethers.js
const inventoryPoolContract = new ethers.Contract(
  inventoryPoolAddress,
  [
    "function asset() view returns (address)"
  ],
  signer
);

// Get the token address from the inventory pool
const tokenAddress = await inventoryPoolContract.asset();

// Set up the token contract
const tokenContract = new ethers.Contract(
  tokenAddress,
  [
    "function approve(address spender, uint256 amount) returns (bool)",
    "function decimals() view returns (uint8)"
  ],
  signer
);

const decimals = await tokenContract.decimals();
const amount = ethers.parseUnits("1000.0", decimals);

// Approve the inventory pool to spend your tokens
const tx = await tokenContract.approve(inventoryPoolAddress, amount);
await tx.wait();
```

### Step 2: Repay the Loan

After approval, call the `repay()` function on the inventory pool contract. Note that the borrower parameter can be any address that has an active loan - it doesn't have to be the caller of the function:

```javascript
// Using ethers.js
const inventoryPoolContract = new ethers.Contract(
  inventoryPoolAddress,
  [
    "function repay(uint256 amount, address borrower)"
  ],
  signer
);

// Repay the loan
const tx = await inventoryPoolContract.repay(amount, borrowerAddress);
await tx.wait();
```

### Important Notes

* If repayment amount exceeds debt, the inventory pool will take the exact amount owed


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nomial.io/repay-a-loan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
