This guide covers test-only helpers used in contracts/credit/src/lib.rs for
draw/repay integration scenarios.
The main contract test module keeps liquidity setup lightweight with helper functions around the real Soroban token client rather than a separate fake token implementation.
Use these helpers in contracts/credit/src/lib.rs when a test needs to model
balance changes across multiple calls:
setup(...)to deploy the contract, configure the liquidity token, and seed the initial reserve;mint_liquidity(...)to top up the reserve or borrower between calls;liquidity_balance(...)to assert reserve depletion and repayment effects;approve(...)for repay-path allowance setup.
- Draw scenarios that need explicit reserve funding checks.
- Repay scenarios that need borrower balance/allowance fixtures.
- Any new integration-style test that currently duplicates token setup code.
Reserve-sensitive draw regressions should snapshot both state and events around the failing call:
- perform one successful draw to consume part of the reserve;
- record
utilized_amount,last_accrual_ts, and event counts; - attempt a second draw that exceeds the remaining reserve;
- assert the panic message, unchanged reserve balance, unchanged stored credit
line fields, and no additional
drawnoraccrueevents.
Cover both a single borrower issuing sequential draws and multiple borrowers sharing the same reserve so shared-liquidity regressions are caught.
Integration tests in contracts/credit/tests/token_failure_rollback.rs assert
that draw_credit / repay_credit clear the reentrancy guard after both
pre-transfer validation failures and mid-transfer CPI failures:
cargo test -p creditra-credit --test token_failure_rollback rollback- Pre-transfer failures use the real Stellar asset contract (insufficient
reserve / allowance) with
catch_unwindto continue the same test after panic. - Mid-transfer failures use the in-test
FailingTokenContractmock (internal balances, configurableset_fail_transfer/set_fail_transfer_from) for draw-fail-then-draw and repay-fail-then-repay sequencing.
MockLiquidityToken is test-only (#[cfg(test)]) and must not be imported
into contract runtime logic.