# Repaying Debt

When you're ready to reduce or eliminate your debt, (using USDC as an example) you can repay it through the cUSDC contract. Repayment requires you to first approve the cUSDC contract to spend your USDC, then call the `repay()` function with the following function arguments:

<table><thead><tr><th width="99">Type</th><th width="100">Name</th><th>Description</th></tr></thead><tbody><tr><td>uint256</td><td>amount</td><td>The amount of the underlying asset to repay, or 0 for the full outstanding amount.</td></tr></tbody></table>

Implementation snippet:

```javascript
async function repayUSDCDebt(amount) {
  const USDCDecimals = await USDC.decimals();
  
  // Use 0 to repay the full outstanding amount
  const isFullRepay = amount === 'full';
  const amountToRepay = isFullRepay ? '0' : amount;
  const amountInSmallestUnit = ethers.utils.parseUnits(amountToRepay, USDCDecimals);
  
  // Approve USDC to be spent by eUSDC contract
  let approvalAmount = amountInSmallestUnit;
  const timestamp = block.timestamp;
  if (isFullRepay) {
    // Use the ProtocolReader contract to get a buffered amount that includes
    // any interest that needs to be accrued without making a separate transaction.
    const block = await provider.getBlock('latest');
    approvalAmount = protocolReader.debtBalanceAtTimestamp(
      userAddress,
      cUSDCAddress,
      timestamp);
  }
  
  await USDC.approve(cUSDCAddress, approvalAmount);
  
  // Repay the debt
  const repayTx = await cUSDC.repay(amountInSmallestUnit);
  const receipt = await repayTx.wait();
  
  console.log(`Repaid ${isFullRepay ? 'full debt' : amount + ' USDC'}`);
  return receipt;
}
```

Curvance simplifies full repayment by allowing you to input 0 as the amount, which automatically clears your entire outstanding debt, including any accrued interest. This removes the need for manual calculations of the total debt amount.

### Repaying Debt on Behalf of Others

A unique feature of Curvance is the ability to repay another users debt. This can be useful in various scenarios, such as:

* Helping a friend avoid liquidation.
* Managing multiple wallets in a DAO or organization.
* Implementing complex DeFi strategies.

The process is similar to regular repayment but uses the `repayFor` function using the following arguments:

<table><thead><tr><th width="112">Type</th><th width="144">Name</th><th>Description</th></tr></thead><tbody><tr><td>address</td><td>account</td><td>The account address to repay on behalf of.</td></tr><tr><td>uint256</td><td>amount</td><td>The amount to repay, or 0 for the full outstanding amount.</td></tr></tbody></table>

Implementation snippets:

```javascript
async function repayForAccount(account, amount) {
  const USDCDecimals = await USDC.decimals();
  const amountInSmallestUnit = ethers.utils.parseUnits(amount.toString(), USDCDecimals);
  
  // Approve USDC to be spent by cUSDC contract
  await USDC.approve(eUSDCAddress, amountInSmallestUnit);
  
  // Repay debt on behalf of another account
  const repayTx = await eUSDC.repayFor(account, amountInSmallestUnit);
  const receipt = await repayTx.wait();
  
  console.log(`Repaid ${amount} USDC on behalf of ${account}`);
  return receipt;
}
```

This function allows anyone to repay debt for any user without requiring permission from the borrower, creating interesting possibilities for social coordination in DeFi.


---

# 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.curvance.com/app/developer-docs/quick-start-guides/borrowing-and-repayment/repaying-debt.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.
