Skip to content

fix(lending): prevent protocol fee collection from draining depositor collateral (#457) - #463

Open
huangji6693-max wants to merge 1 commit into
thisisouvik:mainfrom
huangji6693-max:fix/prevent-fee-draining-depositor-collateral-457
Open

huangji6693-max wants to merge 1 commit into
thisisouvik:mainfrom
huangji6693-max:fix/prevent-fee-draining-depositor-collateral-457

Conversation

@huangji6693-max

Copy link
Copy Markdown

Summary

Prevents LendingContract::collect_fees from transferring depositor collateral to the treasury when the fee token is also a whitelisted collateral asset, and defers protocol fee accrual until loan repayment rather than booking unearned interest at loan request creation.

Changes

  • contracts/lending/src/lib.rs:
    • Added DataKey::TotalCollateral(Address) and get_total_collateral_for_asset(env, asset), updated in deposit_collateral and withdraw_collateral.
    • Updated collect_fees to compute available_fees = token_client.balance(&contract_address).saturating_sub(collateral_total) and panic with "Insufficient unencumbered fee token balance to collect fees" if available_fees < uncollected.
    • Removed upfront UncollectedFees crediting from create_loan_request and accrued loan.platform_fee in record_payment when the loan reaches LoanStatus::Repaid.
  • contracts/lending/src/test.rs:
    • Updated setup_fee_collection to approve, activate, and repay the loan before collecting fees.
    • Added unit tests test_collect_fees_cannot_drain_depositor_collateral and test_collect_fees_succeeds_when_unencumbered_fee_tokens_cover_accrual.

Verification

  • CARGO_BUILD_JOBS=2 cargo test --lib -- --test-threads=1 in contracts/lending: 137 passed

Closes #457

@vercel

vercel Bot commented Oct 26, 2026

Copy link
Copy Markdown

@huangji6693-max is attempting to deploy a commit to the thisisouvik's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Oct 26, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: aa56fb6d-e86c-43f8-846f-71d9cb11b38f
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

Prevent Protocol Fee Collection from Draining Depositor Collateral in LendingContract

1 participant