Skip to content

feat: swap distribution metrics (Issue #2) - #104

Open
Bigg770 wants to merge 2 commits into
OpenPeerX:mainfrom
Bigg770:feature/swap-distribution-metrics
Open

feat: swap distribution metrics (Issue #2)#104
Bigg770 wants to merge 2 commits into
OpenPeerX:mainfrom
Bigg770:feature/swap-distribution-metrics

Conversation

@Bigg770

@Bigg770 Bigg770 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Add on-chain swap-size histogram published as a SwapDistribution event.

events.rs:

  • SwapDistribution contracttype struct with four buckets: size_0_100 / size_100_1k / size_1k_10k / size_10k_plus + total_swaps.
  • SWAP_COUNT_KEY and SWAP_BUCKETS_KEY instance-storage constants.
  • Events::record_swap() increments counter and bucket, auto-emits the SwapDistribution event every SWAP_DISTRIBUTION_CADENCE (1 000) swaps.
  • Events::emit_swap_distribution() for admin-triggered flush + bucket reset.
  • Events::swap_count() helper for tests and analytics.

lib.rs:

  • CounterContract::emit_swap_distribution() entry point (admin-gated).
  • swap() now calls Events::record_swap() after every successful swap.

Also includes BatchShape optimisation (Issue #3) and its tests — both land in the same lib.rs file.

Tests (batch_tests.rs):

  • test_swap_count_increments: asserts counter is 1 after one swap, 2 after two.
  • test_admin_trigger_emit_swap_distribution: admin can trigger flush without panic.
  • test_batch_shape_ten_op_batch_correct_counts: 10-op batch executes correctly.
  • test_batch_shape_mixed_operations: mixed swap/mint batch executes correctly.

closes #91

Add on-chain swap-size histogram published as a SwapDistribution event.

events.rs:
- SwapDistribution contracttype struct with four buckets:
  size_0_100 / size_100_1k / size_1k_10k / size_10k_plus + total_swaps.
- SWAP_COUNT_KEY and SWAP_BUCKETS_KEY instance-storage constants.
- Events::record_swap() increments counter and bucket, auto-emits the
  SwapDistribution event every SWAP_DISTRIBUTION_CADENCE (1 000) swaps.
- Events::emit_swap_distribution() for admin-triggered flush + bucket reset.
- Events::swap_count() helper for tests and analytics.

lib.rs:
- CounterContract::emit_swap_distribution() entry point (admin-gated).
- swap() now calls Events::record_swap() after every successful swap.

Also includes BatchShape optimisation (Issue OpenPeerX#3) and its tests — both
land in the same lib.rs file.

Tests (batch_tests.rs):
- test_swap_count_increments: asserts counter is 1 after one swap, 2 after two.
- test_admin_trigger_emit_swap_distribution: admin can trigger flush without panic.
- test_batch_shape_ten_op_batch_correct_counts: 10-op batch executes correctly.
- test_batch_shape_mixed_operations: mixed swap/mint batch executes correctly.

Copy link
Copy Markdown
Contributor

Nice piece of work on the swap distribution metrics — clean telemetry surface and the wiring into the existing events module is tidy. CI is green across the board. Merging 🚀

Copy link
Copy Markdown
Contributor

Thanks again for the swap distribution metrics work — really nice surface 👋

Heads up: I went ahead and merged several earlier PRs (#101, #102, #103, and #110) before reaching yours. A few of those updated peerx-contracts/counter/src/lib.rs and the surrounding wiring, which now overlaps with your diff. CI was green when this PR was opened, but the merge into the current main now has conflicts.

Could you rebase this branch onto the latest main and push? Once CI re-runs green against the rebased tip, I'll merge it in. Happy to help if you hit anything gnarly during the rebase.

@ameeribro4-sudo

Copy link
Copy Markdown
Contributor

@Bigg770 resollve conflicts please

@Bigg770

Bigg770 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

@ameeribro4-sudo done sir can i be among the selected payers please

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.

[PeerX Backlog # 91] Structured metric events for swap distribution

2 participants