Skip to content

feat: implement CreditLineData storage keyed by borrower#97

Open
iammrjude wants to merge 2 commits intoCreditra:mainfrom
iammrjude:feature/credit-line-storage
Open

feat: implement CreditLineData storage keyed by borrower#97
iammrjude wants to merge 2 commits intoCreditra:mainfrom
iammrjude:feature/credit-line-storage

Conversation

@iammrjude
Copy link

@iammrjude iammrjude commented Feb 25, 2026

Fixes #2 · Closes #2

Summary

Implements persistent storage for CreditLineData keyed by borrower Address. Each borrower now has a single credit line record that can be read and updated by all contract methods through dedicated storage helper functions.

Changes

contracts/credit/src/types.rs

  • Added last_update_timestamp: u64 field to CreditLineData for future interest accrual calculations

contracts/credit/src/lib.rs

  • Added get_credit_line_data storage helper — retrieves CreditLineData from persistent storage by borrower Address, returns None if not found
  • Added set_credit_line_data storage helper — writes CreditLineData to persistent storage keyed by borrower Address, overwrites any existing record
  • Refactored all contract methods to use the new helpers instead of raw storage calls
  • open_credit_line now sets last_update_timestamp on creation
  • draw_credit and repay_credit now update last_update_timestamp on every state change

Tests

Added mod test_credit_line_storage covering:

  • Missing key returns None
  • Storage write and read correctness across all fields
  • Overwrite replaces existing record correctly
  • Isolation between borrowers — modifying one does not affect the other
  • last_update_timestamp field is set and readable on open

Test Output

$ cargo test test_credit_line_storage
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.39s
     Running unittests src\lib.rs (target\debug\deps\creditra_credit-3834f56052e6a3bb.exe)

running 5 tests
test test_credit_line_storage::test_storage_returns_none_for_missing_borrower ... ok
test test_credit_line_storage::test_storage_last_update_timestamp_set_on_open ... ok
test test_credit_line_storage::test_storage_write_and_read_credit_line ... ok
test test_credit_line_storage::test_storage_overwrite_updates_record ... ok
test test_credit_line_storage::test_storage_isolation_between_borrowers ... ok

test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 65 filtered out; finished in 0.06s

cargo test

$ cargo test
   Compiling creditra-credit v0.1.0 (C:\Users\jude\stellar_projects\Creditra-Contracts\contracts\credit)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 6.27s
     Running unittests src\lib.rs (target\debug\deps\creditra_credit-3834f56052e6a3bb.exe)

running 70 tests
test test::test_default_credit_line ... ok
test test::test_close_nonexistent_credit_line - should panic ... ok
test test::test_close_credit_line ... ok
test test::test_close_credit_line_idempotent_when_already_closed ... ok
test test::test_close_credit_line_borrower_when_utilized_zero ... ok
test test::test_close_credit_line_borrower_rejected_when_utilized_nonzero - should panic ... ok
test test::test_close_credit_line_unauthorized_closer - should panic ... ok
test test::test_close_credit_line_admin_force_close_with_utilization ... ok
test test::test_default_nonexistent_credit_line - should panic ... ok
test test::test_default_credit_line_unauthorized - should panic ... ok
test test::test_draw_cumulative_exceeds_limit - should panic ... ok
test test::test_draw_exceeds_credit_limit - should panic ... ok
test test::test_draw_negative_amount_fails - should panic ... ok
test test::test_draw_accumulates_across_multiple_draws ... ok
test test::test_draw_no_credit_line_fails - should panic ... ok
test test::test_draw_exact_credit_limit ... ok
test test::test_draw_on_defaulted_line_fails - should panic ... ok
test test::test_draw_on_suspended_line_fails - should panic ... ok
test test::test_draw_reduces_contract_reserve ... ok
test test::test_draw_on_closed_line_fails - should panic ... ok
test test::test_draw_updates_utilized_amount ... ok
test test::test_draw_transfers_correct_amount_to_borrower ... ok
test test::test_draw_zero_amount_fails - should panic ... ok
test test::test_draw_requires_borrower_auth ... ok
test test::test_event_default_credit_line ... ok
test test::test_event_close_credit_line ... ok
test test::test_event_open_credit_line ... ok
test test::test_init_and_open_credit_line ... ok
test test::test_event_suspend_credit_line ... ok
test test::test_event_lifecycle_sequence ... ok
test test::test_full_lifecycle ... ok
test test::test_open_credit_line_negative_limit_reverts - should panic ... ok
test test::test_open_credit_line_interest_rate_exceeds_max_reverts - should panic ... ok
test test::test_open_credit_line_risk_score_exceeds_max_reverts - should panic ... ok
test test::test_open_credit_line_duplicate_active_borrower_reverts - should panic ... ok
test test::test_open_credit_line_zero_limit_reverts - should panic ... ok
test test::test_repay_credit_nonexistent_line - should panic ... ok
test test::test_repay_credit_rejects_non_positive_amount - should panic ... ok
test test::test_multiple_borrowers_draw_independently ... ok
test test::test_repay_credit_rejected_when_closed - should panic ... ok
test test::test_reentrancy_guard_cleared_after_draw ... ok
test test::test_reentrancy_guard_cleared_after_repay ... ok
test test::test_suspend_credit_line ... ok
test test::test_suspend_nonexistent_credit_line - should panic ... ok
test test::test_repay_credit_reduces_utilized_amount ... ok
test test::test_suspend_credit_line_unauthorized - should panic ... ok
test test::test_repay_credit_saturates_at_zero ... ok
test test::test_update_risk_parameters_interest_rate_exceeds_max - should panic ... ok
test test::test_update_risk_parameters_nonexistent_line - should panic ... ok
test test::test_update_risk_parameters_at_boundaries ... ok
test test::test_update_risk_parameters_negative_credit_limit - should panic ... ok
test test::test_update_risk_parameters_unauthorized_caller - should panic ... ok
test test::test_update_risk_parameters_success ... ok
test test::test_update_risk_parameters_credit_limit_below_utilized - should panic ... ok
test test::test_update_risk_parameters_risk_score_exceeds_max - should panic ... ok
test test_close_utilized::test_close_utilized_admin_force_close_emits_closed_event ... ok
test test_close_utilized::test_close_utilized_admin_force_close_multiple_draws ... ok
test test_close_utilized::test_close_utilized_admin_force_close_preserves_utilized_amount ... ok
test test_close_utilized::test_close_utilized_borrower_rejected_at_full_utilization - should panic ... ok
test test_close_utilized::test_close_utilized_borrower_rejected_on_suspended_line - should panic ... ok
test test_close_utilized::test_close_utilized_borrower_rejected_after_partial_repayment - should panic ... ok
test test_close_utilized::test_close_utilized_borrower_rejected_at_minimum_utilization - should panic ... ok
test test_close_utilized::test_close_utilized_admin_force_close_suspended_line ... ok
test test_close_utilized::test_close_utilized_third_party_rejected_with_zero_utilization - should panic ... ok
test test_credit_line_storage::test_storage_returns_none_for_missing_borrower ... ok
test test_credit_line_storage::test_storage_last_update_timestamp_set_on_open ... ok
test test_credit_line_storage::test_storage_overwrite_updates_record ... ok
test test_credit_line_storage::test_storage_write_and_read_credit_line ... ok
test test_credit_line_storage::test_storage_isolation_between_borrowers ... ok
test test_close_utilized::test_close_utilized_borrower_succeeds_after_full_repayment ... ok

test result: ok. 70 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 1.40s

   Doc-tests creditra_credit

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

@iammrjude
Copy link
Author

@greatest0fallt1me Done Boss

image

@greatest0fallt1me
Copy link
Contributor

please resolve the conflicts

@greatest0fallt1me
Copy link
Contributor

@iammrjude Can you resolve the conflicts?

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.

Implement CreditLineData storage keyed by borrower

2 participants