Skip to content

buyCollateral tests refactoring#369

Open
VladyslavPerederWoof wants to merge 2 commits intofeat/service-patchfrom
test/buyCollateral-refactoring
Open

buyCollateral tests refactoring#369
VladyslavPerederWoof wants to merge 2 commits intofeat/service-patchfrom
test/buyCollateral-refactoring

Conversation

@VladyslavPerederWoof
Copy link
Copy Markdown
Collaborator

@VladyslavPerederWoof VladyslavPerederWoof commented Mar 26, 2026

Summary

Rewrites the buyCollateral test suite to target CometWithExtendedAssetList (24 collateral assets) and align with the project's test writing conventions.

Key changes

  • Migrated from makeProtocol() to makeConfigurator() — the main describe now deploys a full configurator/proxy stack with 24 collateral assets (ASSET0..ASSET23), exercising CometWithExtendedAssetList + CometExtAssetList through the proxy.
  • Eliminated forbidden patterns — removed wait(), portfolio(), event(), and harness state-manipulation helpers (setTotalsBasic, setTotalsCollateral, setCollateralBalance). All state is now established through real protocol interactions (supply, withdraw, absorb, buyCollateral).
  • Adopted flow-based describe structure with atomic it blocks — each it checks exactly one value (a single balance, a single event, a single revert). Actions like time advances and contract calls are their own it blocks, making failures immediately identifiable.
  • Added takeSnapshot / snapshot.restore() isolation — independent describe blocks share a common starting state via snapshots instead of re-deploying the protocol.
  • Switched to revertedWithCustomError — replaces string-based revertedWith("custom error '...()'") assertions with typed custom error assertions (e.g., .to.be.revertedWithCustomError(comet, 'NotForSale')).

New test coverage

  • Revert cases (grouped under describe('revert cases')):
    • Paused — buying when isBuyPaused is true
    • NotForSale — reserves equal targetReserves exactly, and reserves exceed targetReserves
    • TooMuchSlippageminAmount exceeds quoted collateral
    • InsufficientReserves — single buy exceeds collateral reserves, and a second consecutive buy exceeds remaining reserves after a first successful buy
  • Successful purchase — buyer is the recipient — verifies events (BuyCollateral, base Transfer, collateral Transfer), alice/comet token balances, protocol reserves, and collateral reserves
  • Successful purchase — recipient differs from buyer — alice pays, bob receives collateral; verifies alice collateral balance unchanged, bob balance increased
  • Collateral for sale when reserves are negative — uses configurator to set targetReserves=0, creates bad debt via absorb (price drop + liquidation), then buys collateral with negative reserves
  • Collateral reserves vs user-owned collateral — verifies only excess (non-user-owned) collateral is buyable; alice's on-protocol position is unchanged after a buy from reserves
  • All 24 collateral assets — extended asset list coverage — sequential buy across all 24 packed AssetList slots, validating getAssetInfoByAddress inside quoteCollateral/buyCollateral for every slot
  • Fee-on-transfer tokens — split into two sub-describes: base token with 1% fee (event records net, not gross) and collateral token with 1% fee (event records gross, buyer receives net)
  • Reentrancy — retained and cleaned up: reentrant buyCollateral via EvilToken base is blocked (ReentrantCallBlocked), and a state-comparison proof shows reentrant supplyFrom during buyCollateral is silently blocked (evil totalSupplyBase = 0 vs normal totalSupplyBase = 1e6)

… cases

- Updated the buyCollateral test suite to support 24 collateral assets and added detailed setup for asset configurations.
- Implemented new revert cases for paused state and conditions where reserves equal or exceed target reserves.
- Improved test structure for clarity and maintainability.
…al market

- Introduced a new test suite for the buyCollateral function, specifically for the WBTC base (8 decimals) and tBTC collateral (18 decimals).
- Implemented scenarios for standard buys and price fluctuations, ensuring accurate quoting and balance changes.
- Enhanced test clarity with detailed comments and structured setup for asset configurations.
@VladyslavPerederWoof VladyslavPerederWoof added the Tests Tests refactoring label Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Tests Tests refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant