Skip to content

test(lending): cover oracle-driven liquidation flow (#1522) - #1570

Merged
nanaf6203-bit merged 1 commit into
MettaChain:mainfrom
Moonwalker-rgb:test/lending-oracle-liquidation-1522
Nov 24, 2026
Merged

nanaf6203-bit merged 1 commit into
MettaChain:mainfrom
Moonwalker-rgb:test/lending-oracle-liquidation-1522

Conversation

@Moonwalker-rgb

@Moonwalker-rgb Moonwalker-rgb commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1522

Summary

Adds a standalone integration suite at
contract-extensions/tests/lending-oracle-liquidation-tests/ covering the
lending liquidation path with prices supplied by the compiled MockOracle.
It also repairs a merge artifact that currently prevents propchain-lending
from compiling.

Changes

  • contract-extensions/tests/lending-oracle-liquidation-tests/Cargo.toml
  • contract-extensions/tests/lending-oracle-liquidation-tests/src/lib.rs
  • contracts/lending/src/lib.rs — required compile repair only (see below)

Checklist

  • Extension changes are within contract-extensions/; the contract change
    is a compile repair explicitly required to build the tests
  • No modifications to contracts/ — not applicable: liquidate_loan did
    not compile, so contracts/lending/src/lib.rs had to be repaired
  • Rust code follows existing ink! patterns
  • Tests are included and passing
  • cargo clippy passes with no warnings
  • cargo test passes for the new suite
  • cargo build succeeds (the test run builds both compiled contracts)
  • PR references the issue number above

Testing

cargo test --manifest-path contract-extensions/tests/lending-oracle-liquidation-tests/Cargo.toml
# 11 passed; 0 failed

cargo clippy --manifest-path contract-extensions/tests/lending-oracle-liquidation-tests/Cargo.toml --all-targets -- -D warnings
# clean

Coverage against the acceptance criteria

  • Opens a collateralised, underwritten position (500k debt, 1M collateral).
  • Moves the oracle price down and checks the health-factor boundary
    (10_000 bps = 1.0; liquidation only strictly below it).
  • Verifies full liquidation closes the position: status becomes Liquidated,
    one LoanLiquidated event is emitted, and re-liquidation returns
    LoanNotActive.
  • Verifies a partial price decline leaves the position Active and healthy
    until the threshold is crossed (1.12, then 1.04, then below 1.0).
  • Verifies missing prices return PriceFeedMissing, stale prices return
    PriceFeedStale, and a price exactly at price_max_age is still accepted.
  • Verifies caller-supplied prices cannot force or prevent liquidation; the
    admin feed wins in both directions.
  • Uses multiple test accounts and only admin can publish a lending price.

Where the oracle boundary is

Lending does not call the oracle contract. Since #1088 liquidate_loan reads
only the admin-injected oracle_prices feed and ignores the caller-supplied
vector. The suite therefore pushes each price to MockOracle, reads it back
through the Oracle trait (so the compiled mock oracle really served it), and
then records the identical value on lending as admin. No price reaches lending
without first coming out of the oracle.

Liquidation semantics

liquidate_loan is an accounting finalisation in this contract: it re-prices
collateral from the oracle, compares aggregate LTV with the weighted
liquidation threshold, and marks the loan Liquidated. It does not transfer
collateral to the liquidator, repay debt from a liquidator balance, or support
partially liquidating a position. The suite pins those real semantics rather
than inventing a partial-liquidation path, which is called out in the crate
module docs.

Compile repair in contracts/lending

A merge left two copies of the liquidation valuation block: the stale copy
referenced the renamed _current_collateral_values parameter, and the
surviving copy never recomputed effective_threshold, which is used two lines
later. This PR consolidates them into the single oracle-only block, restoring
the #1088 valuation path and the weighted-threshold calculation. Without this
the test crate cannot build.

Note: cargo test -p propchain-lending --lib still reports pre-existing
failures in borrow-rate, deposit/escrow, marketplace and purge tests, and the
existing lending-liquidation-tests has three assertions written before the
#1088 oracle requirement. Those are outside this issue and are left untouched.

Add an integration suite under contract-extensions/tests that publishes
prices through the compiled mock oracle, records the same values on
lending's admin oracle feed, and exercises health-factor boundaries,
missing and stale prices, full liquidation, and oracle priority over
caller-supplied prices.

Also repair a merge artifact in liquidate_loan. A duplicated pre-MettaChain#1088
price block referenced the renamed _current_collateral_values parameter,
and the surviving block never recomputed effective_threshold, so
propchain-lending did not compile. Consolidating the block restores the
oracle-only valuation path and the weighted threshold calculation.
@drips-wave

drips-wave Bot commented Nov 23, 2026

Copy link
Copy Markdown

@Moonwalker-rgb Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@nanaf6203-bit nanaf6203-bit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice work. Thanks for the contribution!

@nanaf6203-bit
nanaf6203-bit merged commit abb3ccb into MettaChain:main Nov 24, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integration Test: Lending Liquidation with Oracle Price Feed

2 participants