๐ŸฆCore Market Integration

This guide walks through integrating your smart contract with Moonwell Core lending markets. By the end, you'll know how to supply assets, borrow against collateral, query positions, claim rewards, and handle errors.

Prerequisites

Your contract needs to interact with two contracts:

  • mToken - The market contract for each asset (e.g. mUSDC, mWETH). Handles supply, borrow, repay, withdraw, liquidate.

  • Comptroller - The policy layer. Manages collateral, health checks, and rewards.

See mTokens and Comptroller for the full function reference.

Step 1: Supply Assets

Supplying means depositing underlying tokens and receiving mTokens in return. Your mToken balance grows in value over time as interest accrues.

MErc20 mToken = MErc20(0x...); // e.g. Moonwell USDC
EIP20Interface underlying = EIP20Interface(mToken.underlying());
uint supplyAmount = 1000e6; // 1,000 USDC

// Approve the mToken to pull underlying
underlying.approve(address(mToken), supplyAmount);

// Mint mTokens - returns 0 on success
uint err = mToken.mint(supplyAmount);
require(err == 0, "mint failed");

After minting, your contract holds mTokens. These are standard ERC-20 tokens - transferrable and composable.

Step 2: Enable as Collateral

Supplying alone does not enable borrowing. You must explicitly enter the market via the Comptroller to use your supply as collateral.

You can enter multiple markets in a single call. Once entered, your supply counts toward your borrowing capacity.

Step 3: Borrow

With collateral enabled, you can borrow from any market up to your account liquidity.

circle-info

Check your borrowing capacity first with comptroller.getAccountLiquidity(address(this)). The second return value (liquidity) is the USD value you can still borrow. The third (shortfall) is non-zero if you're underwater.

Step 4: Repay and Withdraw

Pass type(uint).max to repayBorrow to repay the full outstanding balance without calculating it yourself.

Step 5: Query Positions

Step 6: Claim Rewards

Moonwell distributes WELL token rewards to suppliers and borrowers. Claim them through the Comptroller.

Error Handling

mToken and Comptroller functions use two failure modes. Your integration must handle both.

Error codes - Business logic rejections (insufficient collateral, comptroller rejection) return a non-zero uint without reverting:

Reverts - Transfer failures, math overflows, and pause/cap enforcement revert the transaction. Use try/catch if you need to handle these gracefully:

See mToken Error Codes and Comptroller Error Codes for the full tables.

Full Example

A complete contract that accepts deposits from users, supplies to Moonwell, borrows against the collateral, and allows withdrawal:

Deployed Addresses

See mTokens - Deployed Contracts for all mToken market addresses on Base and OP Mainnet.

Last updated