Skip to content

refactor(debit): extract shared check helper (#265) - #310

Merged
mikewheeleer merged 1 commit into
Agentpay-Org:mainfrom
ALIPHATICHYD:refactor/debit-01-helper
Jul 24, 2026
Merged

refactor(debit): extract shared check helper (#265)#310
mikewheeleer merged 1 commit into
Agentpay-Org:mainfrom
ALIPHATICHYD:refactor/debit-01-helper

Conversation

@ALIPHATICHYD

Copy link
Copy Markdown
Contributor

Summary

Closes #265

This PR extracts repeated inline debit precondition checks and debiting logic in contracts/escrow/src/lib.rs into private helper functions:

  • check_debit_precondition: Validates that an agent's prepaid credit balance is sufficient for projected usage bills, panicking with EscrowError::InsufficientCreditBalance (Document the schema-versioning and migration model for redeployments #28) if exceeded.
  • debit_agent_credit: Deducts min(billed, credit_balance) from the agent's prepaid balance in storage and publishes the cred_deb event.

Key Changes

  • contracts/escrow/src/lib.rs:
    • Route record_usage through check_debit_precondition
    • Route settle through debit_agent_credit
  • contracts/escrow/src/test.rs:
    • Added unit tests covering exact credit balance, zero credit balance, partial credit drawdown, and event emission edge cases.
    • Fixed pre-existing clippy warnings (duplicated-attributes and unnecessary-cast).

Verification & Test Output

$ cargo fmt --all -- --check
Passed.

$ cargo clippy --all-targets -- -D warnings
Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.56s

$ cargo test
running 293 tests
test test::test_accept_admin_transfer_clears_pending_slot ... ok
test test::test_accept_admin_transfer_requires_auth ... ok
test test::test_accept_admin_transfer_replaces_admin ... ok
test test::test_record_usage_allows_exact_credit_balance ... ok
test test::test_record_usage_allows_uncredited_agent ... ok
test test::test_record_usage_rejects_insufficient_credit_balance - should panic ... ok
test test::test_settle_partial_credit_balance_drawdown ... ok
...
test result: ok. 293 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 5.12s

@mikewheeleer

Copy link
Copy Markdown
Contributor

@ALIPHATICHYD solid and well-scoped, merging now 🚢

@mikewheeleer
mikewheeleer merged commit dd04f3a into Agentpay-Org:main Jul 24, 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.

Extract the repeated debit check into a shared helper

2 participants