Lend Assets
Depositing USDC for Lending
// Get the cUSDC contract with signer.
// USDC_CTOKEN is per isolated market, not a single global address; pull the
// address for the specific market you are depositing into from the deployment registry.
const cUSDC = new ethers.Contract(ADDRESSES.USDC_CTOKEN, CTOKEN_ABI, signer);
// Get the underlying USDC contract with signer.
const usdc = new ethers.Contract(ADDRESSES.USDC, ERC20_ABI, signer);
// Format amount (USDC has 6 decimals)
const depositAmount = ethers.parseUnits("1000", 6); // 1000 USDC
const userAddress = await signer.getAddress();
const maxAssets = await cUSDC.maxDeposit(userAddress);
if (maxAssets === 0n || depositAmount > maxAssets) {
throw new Error("Deposit is not currently available for this cToken");
}
// Preview the shares expected from this deposit.
// This is an estimate from the last-accrued exchange rate, not a guarantee.
const expectedShares = await cUSDC.previewDeposit(depositAmount);
if (expectedShares === 0n) throw new Error("Deposit amount would mint zero shares");
// Pre-flight: skip the approve tx if allowance is already sufficient
const current = await usdc.allowance(userAddress, ADDRESSES.USDC_CTOKEN);
if (current < depositAmount) {
const approveTx = await usdc.approve(ADDRESSES.USDC_CTOKEN, depositAmount);
await approveTx.wait();
}
// Deposit USDC for lending
const depositTx = await cUSDC.deposit(depositAmount, userAddress);
await depositTx.wait();Pre-flight checks before depositing
What happens when you deposit
Lending vs. collateralized deposits
Last updated
Was this helpful?