Skip to content

Fix/lp share price mechanism#39

Merged
EmeditWeb merged 6 commits into
StepFi-app:mainfrom
KingFRANKHOOD:fix/lp-share-price-mechanism
Jun 27, 2026
Merged

Fix/lp share price mechanism#39
EmeditWeb merged 6 commits into
StepFi-app:mainfrom
KingFRANKHOOD:fix/lp-share-price-mechanism

Conversation

@KingFRANKHOOD

Copy link
Copy Markdown
Contributor

Fix liquidity pool share price mechanism with deposit/withdraw and integration tests" --body "$(cat /tmp/pr_body Closes #16

Overhauls the liquidity pool's share price mechanism so that deposit and withdrawal are priced consistently through a single calculate_share_price_internal helper, and wires up the creditline contract integration tests to actually verify liquidity pool and reputation interactions.

Changes

Liquidity Pool Contract (contracts/liquidity-pool-contract/src/lib.rs)

  • deposit() - now issues shares at the current share price: shares = (amount * PRECISION) / share_price. Previously used a proportional formula that behaved differently on first vs subsequent deposits.
  • withdraw() - now returns tokens using the share price: amount = (shares * share_price) / PRECISION. Previously used shares * total_liquidity / total_shares.
  • calculate_withdrawal() - updated to use share price; returns 0 when shares == 0.
  • calculate_share_price_internal() - extracted shared logic into a single helper: total_liquidity * PRECISION / total_shares. Returns PRECISION (1.0) when pool is empty.
  • get_share_price() - new public query function.
  • accumulate_interest() - new public function calling distribute_interest_internal, making yield accrual explicit for LPs.
  • get_pool_stats() - delegates to the shared helper instead of duplicating the formula.

Types (contracts/liquidity-pool-contract/src/types.rs)

  • Added SHARE_PRICE_PRECISION = 10_000 constant, replacing inline uses of TOTAL_BPS for share price math.

Integration Tests (contracts/creditline-contract/src/tests.rs)

  • MockReputation - now stores and mutates scores per-user instead of always returning 100.
  • MockLiquidityPool - tracks calls to fund_loan, receive_repayment, receive_guarantee with assertion helpers.
  • MockLiquidityPoolEmpty - new mock that returns zero liquidity (insufficient-liquidity test).
  • TestCtx - exposes lp_id/rep_id (was _lp_id/_rep_id); adds 6 helper methods for mock state.
  • Enabled tests (previously #[ignore]):
    • test_loan_funding_debits_liquidity_pool
    • test_repayment_credited_to_liquidity_pool
    • test_guarantee_transferred_to_pool_on_default
    • test_insufficient_liquidity_rejects_loan_creation
    • test_multi_contract_integration_full_flow

…tline integration tests

- Make MockReputation stateful with storage-backed score tracking
- Make MockLiquidityPool stateful with call-tracking and query functions
- Add MockLiquidityPoolEmpty for insufficient-liquidity test scenario
- Add helper methods to TestCtx for querying mock state
- Un-ignore and implement assertions for 5 previously TODO-stubbed tests:
  test_loan_funding_debits_liquidity_pool
  test_repayment_credited_to_liquidity_pool
  test_guarantee_transferred_to_pool_on_default
  test_insufficient_liquidity_rejects_loan_creation
  test_multi_contract_integration_full_flow
@EmeditWeb

Copy link
Copy Markdown
Member

I'd run ci checks

@EmeditWeb

Copy link
Copy Markdown
Member

LGTM

@EmeditWeb EmeditWeb merged commit 3d3debe into StepFi-app:main Jun 27, 2026
1 check 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.

core: implement share price calculation in liquidity pool

2 participants