Skip to content

Withdrawal fee calculation (2%) + realtime balance.updated event - #1119

Merged
Akanimoh12 merged 2 commits into
Akanimoh12:test-implement-dripsfrom
Godbrand0:fix/withdrawal-fee-and-realtime-balance-941-951
Jul 25, 2026
Merged

Withdrawal fee calculation (2%) + realtime balance.updated event#1119
Akanimoh12 merged 2 commits into
Akanimoh12:test-implement-dripsfrom
Godbrand0:fix/withdrawal-fee-and-realtime-balance-941-951

Conversation

@Godbrand0

Copy link
Copy Markdown
Contributor

Summary

#941 — Withdrawal fee calculation

  • Adds a calculateWithdrawalFee pure function (backend/src/modules/withdrawals/withdrawals.service.ts) that splits a gross withdrawal amount into fee and netAmount, floored so the platform never over-charges.
  • Fee rate is configurable via WITHDRAWAL_FEE_BPS (env var, default 200 = 2%), wired through src/config/env.ts and src/config/index.ts, documented in .env.example.
  • prepareWithdrawal now sends netAmount (post-fee) on-chain and returns fee/netAmount in the response.
  • Unit tests for the fee formula (2% charge, flooring, zero-fee, invalid amount) plus an updated integration test asserting fee/netAmount on POST /withdrawals/prepare.

#951 — Realtime balance.updated event

  • Adds a typed balance.updated Socket.IO server event (backend/src/realtime/types.ts) and emitBalanceUpdated (backend/src/realtime/gateway.ts), exported from the realtime barrel.
  • Auth enforced the same way as notification.created: delivered only to the user:<id> room, which a socket may only join if its authenticated authUser.id matches.
  • Wired into PATCH /tips/:txHash/confirm — confirming a tip changes the recipient's withdrawable balance, so their sockets are notified (best-effort; failures are logged, not surfaced as a 500 for an already-successful confirmation).
  • New backend/src/realtime/gateway.test.ts covering: delivery to the balance owner after subscribing, rejection of subscribing to another user's room, and rejection of unauthenticated connections. Plus two new tests in tips.test.ts for the confirm → emit wiring.

Also fixed (blocking both issues)

The test-implement-drips branch had pre-existing merge-artifact duplication that broke npm run typecheck/test for essentially every module:

  • src/app.ts — duplicated/malformed import block (syntax error) and a duplicate router mount.
  • src/common/errors/AppError.tsServiceUnavailableError declared twice.
  • prisma/schema.prismaUser model had every relation field duplicated, and TipStatus enum was declared twice (schema failed prisma generate).

These were reconciled by keeping the more complete duplicate and removing the redundant one — no behavioral changes beyond making the branch buildable/testable again.

Test plan

  • npm run typecheck — no new errors (pre-existing unrelated errors in credit.service.ts / x.test.ts remain, not touched by this PR)
  • npm run lint — 0 errors (pre-existing warnings only)
  • npx vitest run src/modules/withdrawals/withdrawals.test.ts — 9/9 passing
  • npx vitest run src/realtime/gateway.test.ts — 3/3 passing
  • npx vitest run src/modules/tips/tips.test.ts -t confirm — 7/7 passing (includes 2 new balance.updated tests)
  • Full suite has pre-existing failures unrelated to this change (tests requiring a live Postgres/Redis via docker compose up, and unrelated bugs in credit, x, auth modules) — not introduced by this PR.

…altime event

Closes Akanimoh12#941, Akanimoh12#951

- withdrawals: compute a 2% (configurable via WITHDRAWAL_FEE_BPS) fee on
  prepareWithdrawal, send the net amount on-chain, and return fee/netAmount
  in the response.
- realtime: add a typed `balance.updated` Socket.IO event, auth-enforced via
  the existing user:<id> room, and emit it after a tip is confirmed.
- fix pre-existing merge-artifact duplication (app.ts imports/mounts,
  AppError.ts ServiceUnavailableError, prisma schema.prisma User model and
  TipStatus enum) that broke the build for every test on this branch.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@Akanimoh12

Copy link
Copy Markdown
Owner

Please resolve conflicts

@drips-wave

drips-wave Bot commented Jul 25, 2026

Copy link
Copy Markdown

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

@Akanimoh12
Akanimoh12 merged commit 27b152c into Akanimoh12:test-implement-drips Jul 25, 2026
4 of 5 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.

Realtime: Emit balance.updated events Withdrawals: Withdrawal fee calculation (2%)

2 participants