Skip to content

fix(xlm_wrapper): minimum reserve check before XLM deposit - #1042

Merged
Ceejaytech25 merged 1 commit into
ceejaylaboratory:mainfrom
boseshittu2323-design:fix/1002-xlm-minimum-reserve-check
Sep 2, 2026
Merged

Ceejaytech25 merged 1 commit into
ceejaylaboratory:mainfrom
boseshittu2323-design:fix/1002-xlm-minimum-reserve-check

Conversation

@boseshittu2323-design

Copy link
Copy Markdown
Contributor

Summary

Reject XLM wrapping requests that would leave an account's native balance below the Stellar network minimum reserve, preventing accounts from being stranded without enough XLM to pay transaction fees.

Problem

deposit() transferred XLM without checking whether the sender would retain the mandatory Stellar account reserve (0.5 XLM × number of ledger entries), which could render accounts non-functional.

Changes (contracts/xlm_wrapper/src/lib.rs)

  • DataKey::TrustlineCount(Address) — tracks per-account trustline count
  • BASE_RESERVE_STROOPS = 5_000_000 — 0.5 XLM in stroops (7 decimals)
  • MIN_ACCOUNT_ENTRIES = 2 — base account entry requirement
  • deposit() — before transferring, computes
    available = current_balance − (2 + trustlines) × 0.5 XLM
    and panics "InsufficientReserve" if amount > available
  • set_trustline_count(admin, account, count) — admin-only setter for per-account trustline count
  • get_min_reserve(account) — view function returning minimum reserve in stroops
  • Existing tests: fund_user helpers updated to mint amount + 10_000_000 so tests pass the new guard
  • New unit tests (7): reserve boundary pass/fail, default min reserve, trustline-adjusted reserve, combined trustline + deposit scenarios

Closes #1002

…oratory#1002)

Prevent wrapping requests that would leave an account below the Stellar
network minimum reserve (0.5 XLM × number of ledger entries).

Changes to contracts/xlm_wrapper/src/lib.rs:
- Add DataKey::TrustlineCount(Address) to track per-account trustline count
- Define BASE_RESERVE_STROOPS = 5_000_000 (0.5 XLM in stroops) and
  MIN_ACCOUNT_ENTRIES = 2 (base account entry requirement)
- deposit(): before transferring XLM, compute
    available = current_balance - (MIN_ACCOUNT_ENTRIES + trustlines) × 0.5 XLM
  and reject with 'InsufficientReserve' if amount > available
- Add set_trustline_count(admin, account, count) — admin-only setter so a
  trusted oracle can record the account's real trustline count on-chain
- Add get_min_reserve(account) — view function returning the minimum reserve
  in stroops for a given account
- Update existing test helpers (fund_user) to mint amount + 10_000_000
  (base reserve) so that pre-existing tests continue to pass
- New unit tests:
  - test_deposit_respects_base_reserve: deposit of exactly available succeeds
  - test_deposit_below_base_reserve_rejected: zero available → panic
  - test_deposit_exceeding_available_balance_rejected: over available → panic
  - test_get_min_reserve_default: 2 × 5_000_000 = 10_000_000
  - test_get_min_reserve_with_trustlines: 5 × 5_000_000 = 25_000_000
  - test_deposit_with_trustlines_enforces_higher_reserve: extra TLs → higher guard
  - test_deposit_with_trustlines_within_available: deposit within available passes

Closes ceejaylaboratory#1002
@drips-wave

drips-wave Bot commented Aug 25, 2026

Copy link
Copy Markdown

@boseshittu2323-design 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

@Ceejaytech25

Copy link
Copy Markdown
Contributor

Nice implementation, LGTM!

@Ceejaytech25
Ceejaytech25 merged commit e0b7777 into ceejaylaboratory:main Sep 2, 2026
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.

[Soroban] Add native XLM wrap/unwrap conversion safety checks in XLM Wrapper contract

2 participants