# Loans & Collateral

This guide demonstrates how to integrate with Curvance Protocol using JavaScript and ethers.js v5.7, with specific examples using the shMON for collateral and USDC for lending.

{% hint style="info" %}
The MarketManager coordinates all cTokens, for a deep dive check out this article: [Market Manager](https://docs.curvance.com/cve/developer-docs/lending-protocol/market-manager)
{% endhint %}

#### Setting Up Your Integration

First, install ethers.js, for the examples in these docs, we use ethers 5.7.x.

```bash
npm install ethers@5.7.3
```

You'll need to define your contract addresses and ABIs. Here's a simplified example for the most important ones:

```javascript
const ADDRESSES = {
  SHMON: "0x21E27a5E5513D6e65C4f830167390997aA84843a",
  USDC: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
  SHMON_CTOKEN: "0x..." // Replace with actual cToken address
  USDC_CTOKEN: "0x..." // Replace with actual cToken address
};
```

## Important Considerations Before Depositing

Before sending any transactions, check these important protocol values by calling `mintPaused()` in the MarketManager contract.

1. **Protocol Status:** Verify if minting of the cToken is not paused:

Call [`actionsPaused()`](https://docs.curvance.com/cve/lending-protocol/market-manager#actionspaused) with the following arguments. Returning a tuple of booleans, indicating if minting, collateralization, and borrowing is paused.

<table><thead><tr><th width="109.20001220703125">Type</th><th width="469.98333740234375">Description</th></tr></thead><tbody><tr><td>address</td><td>Address of the cToken to check the minting status of.</td></tr></tbody></table>

Implementation:

```javascript
const marketManager = new ethers.Contract(MARKET_MANAGER_ADDRESS, MARKET_MANAGER_ABI, provider);
const actionsPaused = await marketManager.mintPaused(SHMON_CTOKEN);
```

2. **Collateral Caps:** If depositing as collateral, check if the asset has reached its collateral cap by calling `collateralCaps()`, and `marketCollateralPosted()` in the MarketManager contract:

Calling `collateralCaps()` using these function arguments, returning uint256 indicating the maximum amounts of cTokens that can be posted as collateral:

<table><thead><tr><th width="109.20001220703125">Type</th><th width="469.98333740234375">Description</th></tr></thead><tbody><tr><td>address</td><td>Address of the cToken to check the max amount of shares that can be posted as collateral.</td></tr></tbody></table>

Call `marketCollateralPosted()`  which returns a uint256 indicating the current amount of cTokens currently posted as collateral:

<table><thead><tr><th width="109.20001220703125">Type</th><th width="469.98333740234375">Description</th></tr></thead><tbody><tr><td>address</td><td>Address of the cToken to check the amount of collateral posted.</td></tr></tbody></table>

Implementation:

```javascript
const cap = await marketManager.collateralCaps(SHMON_CTOKEN);
const posted = await marketManager.marketCollateralPosted(SHMON_CTOKEN)
const capReached = posted.gte(cap) && !cap.isZero();
```

3. **Minimum Hold Period:** Be aware that Curvance implements a 20-minute minimum hold period for collateralized deposits to mitigate flash loan attacks.
4. **Non Rebasing:** Understand that your share value increases over time rather than the number of shares.

#### Best Practices for Production Implementations

1. **Error Handling:** Implement proper error handling for all transactions.
2. **Gas Estimation:** Use estimateGas before sending transactions to ensure proper gas limits.
3. **Approval Checking:** Check existing allowances before sending approval transactions.
4. **Transaction Monitoring:** Implement logic to track transaction status after submission.
5. **Read-Only Checks:** Use read-only calls to validate operations before sending transactions.

By following this guide, you'll be able to integrate with Curvance's deposit functionality for both yield-optimized collateral positions and lending positions using JavaScript and ethers.js v5.7.
