Skip to content

feat(backend): fix #812 implement trigger payout endpoint with grace …#892

Merged
ONEONUORA merged 2 commits into
Fracverse:masterfrom
fhayvy:feature/issue-812
Jun 29, 2026
Merged

feat(backend): fix #812 implement trigger payout endpoint with grace …#892
ONEONUORA merged 2 commits into
Fracverse:masterfrom
fhayvy:feature/issue-812

Conversation

@fhayvy

@fhayvy fhayvy commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Closes #812

PR Description

This Pull Request implements the POST /api/plans/payout endpoint in the Axum backend to resolve the issue.

How the Flow Works Now:

  1. Request Reception: The endpoint /api/plans/payout receives a PayoutRequest containing the plan owner's wallet address.
  2. Authentication: The request passes through the signature_auth_middleware, which verifies the cryptographic signature (Ed25519) of the request body to ensure authenticity.
  3. Database Transaction & Pessimistic Locking: A database transaction is started, and the active plan is retrieved using FOR UPDATE to lock the row and prevent concurrent trigger requests.
  4. Grace Period Verification: The handler checks if the grace period has elapsed (now >= last_ping + grace_period_seconds). If the owner is still active (within the grace period), the request is rejected with 400 Bad Request.
  5. Yield Projection: The handler calculates the final accrued yield up to the current time using compute_projected_accrued_yield and adds it to the principal to get the total payout amount.
  6. Pro-rata Beneficiary Distribution:
    • The handler loads all beneficiaries associated with the plan.
    • The total payout amount is split based on each beneficiary's allocation_bps (basis points).
    • Any remainder/dust from integer division is allocated to the last beneficiary to ensure 100% of the funds are distributed.
  7. Payout Record Insertion: An entry is inserted into the payouts table for each beneficiary, specifying the type (fiat or crypto) and status (processing).
  8. Distribution Initiation:
    • If the beneficiary has fiat_anchor_info set, the handler parses the details and triggers a fiat payout via the simulated Stellar Anchor off-ramp (state.anchor.create_payout).
    • If it is a crypto payout, the handler logs the transaction to simulate the on-chain transfer.
  9. Plan Deactivation: The plan is updated in the database, setting is_active = false and status = 'PAID_OUT'.
  10. Cache Invalidation: The Redis/memory cache is invalidated for both the owner and the beneficiaries.

Changed

  • backend/src/api.rs:
    • Implemented the trigger_payout handler to process payouts, calculate yields, insert payout records, and deactivate plans.
    • Implemented the parse_fiat_anchor_info helper to parse anchor details for the AnchorPayoutRequest.
  • backend/tests/api_tests.rs:
    • Added test_trigger_payout_invalid_signature to verify signature auth failure.
    • Added test_trigger_payout_valid_signature_not_found to verify successful signature auth and routing to the database handler.

Testing

The changes were verified by running the backend integration test suite.

cd backend
cargo test

All 25 tests compiled and passed successfully:

running 10 tests
test test_router_compiles ... ok
test test_create_plan_validation_negative_amount ... ok
test test_create_plan_validation_empty_owner ... ok
test test_create_plan_validation_invalid_bps ... ok
test test_get_plans_returns_cached_response_without_db_access ... ok
test test_ping_plan_invalid_signature ... ok
test test_trigger_payout_invalid_signature ... ok
test test_trigger_payout_valid_signature_not_found ... ok
test test_get_plans_is_public ... ok
test test_create_plan_with_valid_signature ... ok

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

@drips-wave

drips-wave Bot commented Jun 29, 2026

Copy link
Copy Markdown

@fhayvy 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

@ONEONUORA ONEONUORA left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job @fhayvy
Thank you for your contribution

@ONEONUORA ONEONUORA merged commit f659aa6 into Fracverse:master Jun 29, 2026
4 checks 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.

[Backend] REST API: Implement Trigger Payout Endpoint

2 participants