Skip to main content

compound-patterns

Use when integrating with Compound V3 (Comet) for lending, borrowing, or building liquidation bots. Covers single-asset lending, collateral management, absorb mechanics, and interest rate models.

설치로 이동

소스 정보

저장소
ccashwell/evm-cortex
최근 소스 활동
2026년 4월 10일 16:31
감지된 SKILL.md 언어
영어
스타
131
포크
18

설치 방법

기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.

소스 파일 검토

설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.

SKILL.md 표시 중

SKILL.md
소스 지침 · 읽기 전용 미리보기
name
compound-patterns
description
Use when integrating with Compound V3 (Comet) for lending, borrowing, or building liquidation bots. Covers single-asset lending, collateral management, absorb mechanics, and interest rate models.
# Compound V3 (Comet) Integration ## Architecture Compound V3 (Comet) is a fundamentally different design from V2. Each Comet deployment is a single-asset market — one base asset (e.g., USDC) with multiple collateral assets. There are no cTokens; the Comet contract itself tracks balances. | Component | Description | |-----------|-------------| | `Comet` | Core market contract (one per base asset) | | `CometRewards` | Claims COMP rewards for suppliers/borrowers | | `Configurator` | Governance-controlled parameter updates | | `BulkerV2` | Batch operations including native ETH wrapping | ## Supply / Withdraw Base Asset ```solidity import {IComet} from "./interfaces/IComet.sol"; IComet comet = IComet(COMET_USDC); // Supply base asset (earn interest) IERC20(usdc).approve(address(comet), amount); comet.supply(usdc, amount); // Withdraw base asset comet.withdraw(usdc, amount); // Use type(uint256).max for full withdrawal ``` ## Collateral Management ```solidity // Supply collateral (does NOT earn interest) IERC20(weth).approve(address(comet), amount); comet.supply(weth, amount); // Withdraw collateral comet.withdraw(weth, amount); // Check collateral balance uint256 collateral = comet.collateralBalanceOf(account, weth); ``` ## Borrowing ```solidity // Borrow base asset against collateral // Simply withdraw more base than you supplied comet.withdraw(usdc, borrowAmount); // Repay borrow — supply base asset IERC20(usdc).approve(address(comet), repayAmount); comet.supply(usdc, repayAmount); ``` ## Account State ```solidity // Positive = supplying, negative = borrowing int256 balance = comet.balanceOf(account); // Check if account is liquidatable bool isLiquidatable = comet.isLiquidatable(account); // Get borrow balance (always positive) uint256 borrowBalance = comet.borrowBalanceOf(account); // Check if an action is allowed bool canBorrow = comet.isBorrowCollateralized(account); ``` ## Liquidation via Absorb ```solidity // Compound V3 uses "absorb" instead of traditional liquidation // Anyone can call absorb on an underwater account address[] memory accounts = new address[](1); accounts[0] = underwaterAccount; comet.absorb(msg.sender, accounts); // After absorb, protocol holds the collateral // Buy collateral at a discount through the Comet contract uint256 reserves = comet.getCollateralReserves(weth); comet.buyCollateral(weth, minAmount, baseAmount, recipient); ``` ## Interest Rate Model Compound V3 uses a kinked rate model: ``` if utilization <= kink: borrowRate = baseBorrowRate + (utilization * borrowRateSlopeLow) else: borrowRate = baseBorrowRate + (kink * borrowRateSlopeLow) + ((utilization - kink) * borrowRateSlopeHigh) ``` ```solidity uint256 utilization = comet.getUtilization(); uint256 supplyRate = comet.getSupplyRate(utilization); uint256 borrowRate = comet.getBorrowRate(utilization); // Rates are per-second, scaled by 1e18 // APR = rate * SECONDS_PER_YEAR ``` ## COMP Rewards ```solidity import {ICometRewards} from "./interfaces/ICometRewards.sol"; ICometRewards rewards = ICometRewards(COMET_REWARDS); // Claim COMP rewards rewards.claim(cometAddress, account, true); // true = accrue first // Check pending rewards ICometRewards.RewardOwed memory owed = rewards.getRewardOwed(cometAddress, account); // owed.token = COMP address, owed.owed = claimable amount ``` ## Safe Integration Pattern ```solidity contract CometIntegration { IComet public immutable comet; address public immutable baseToken; constructor(address _comet) { comet = IComet(_comet); baseToken = IComet(_comet).baseToken(); } function supplyBase(uint256 amount) external { IERC20(baseToken).transferFrom(msg.sender, address(this), amount); IERC20(baseToken).approve(address(comet), amount); comet.supply(baseToken, amount); } function supplyCollateral(address asset, uint256 amount) external { IERC20(asset).transferFrom(msg.sender, address(this), amount); IERC20(asset).approve(address(comet), amount); comet.supply(asset, amount); } function borrow(uint256 amount) external { comet.withdraw(baseToken, amount); IERC20(baseToken).transfer(msg.sender, amount); } function isHealthy() external view returns (bool) { return !comet.isLiquidatable(address(this)); } } ``` ## Checklist - [ ] Differentiate base asset operations (earn interest) from collateral (no interest) - [ ] Use `isLiquidatable()` to monitor health before withdrawals - [ ] Use `BulkerV2` for batching operations and native ETH support - [ ] Approve exact amounts to Comet before supply - [ ] Account for per-second interest accrual in balance checks - [ ] Test absorb + buyCollateral flow for liquidation bots - [ ] Check collateral factors and supply caps via `getAssetInfo()`
GitHub에서 보기