Skip to content

feat: add suspicious activity detection, automated flagging/auto-paus…#449

Open
barnabasolutayo-lgtm wants to merge 1 commit into
Savitura:mainfrom
barnabasolutayo-lgtm:suspicious-activity-detection
Open

feat: add suspicious activity detection, automated flagging/auto-paus…#449
barnabasolutayo-lgtm wants to merge 1 commit into
Savitura:mainfrom
barnabasolutayo-lgtm:suspicious-activity-detection

Conversation

@barnabasolutayo-lgtm

Copy link
Copy Markdown

##closes #409

Here is the Pull Request description in markdown format:

# PR: Suspicious Activity Detection and Auto-Freeze for Campaigns

## Summary of Changes
Introduced an automated fraud detection system to discover suspicious funding patterns (such as wash trading, coordinated fake contributions, or high velocity) and allowed platform administrators to review, approve, or freeze campaigns directly from the admin dashboard.

---

## Key Features & Implementations

### 1. Database Schema Extensions
* **IP Tracking:** Added `ip_address` column to `contributions` table.
* **Fraud Tracking:** Added `is_flagged_fraud` (boolean), `fraud_score` (integer), and `fraud_signals` (jsonb) columns to `campaigns` table.
* **Schema Alignment:** Fixed database schema drift by adding the missing `platform_fee_amount` column on the `contributions` table in `schema.sql`.

### 2. Backend Scorer (`fraudService.js`)
* Dynamically scores campaigns based on 4 fraud signals:
  1. **Coordinated Same IP Contributions:** Checks for multiple contributions from the same IP address in a 24-hour window (default weight: `20` per over-limit contribution).
  2. **Wallet Age:** Identifies contributions from user wallets created less than 1 hour ago (default weight: `30` per young wallet).
  3. **Abnormal Velocity:** Identifies funding velocity spikes (> 3x historical average in 1 hour) with absolute volume gates.
  4. **Single-Wallet Dominance:** Scores contributions from a single wallet that exceed 50% of the campaign's target amount.
* **Auto-Flagging & Freezing:** Flag campaigns when scores exceed `FRAUD_THRESHOLD` (50). If they exceed `FRAUD_AUTO_PAUSE_THRESHOLD` (80), campaign status is auto-suspended to block further contributions.
* **Tunable Parameters:** Weights, windows, and thresholds are fully tunable via environment variables.

### 3. Alerts & Admin Notifications
* **Immediate Alerts:** Sends immediate Slack/Sentry alerts via the alerting service when a campaign gets flagged.
* **Email System:** Added a new `campaignFraudFlagged` email template to immediately email admin users when a flag occurs.

### 4. Integration & Bug Fixes
* **Contributions Capture:** Integrated IP address capture (`req.ip`) in both Freighter (prepared) and custodial contribution pipelines.
* **Milestone Triggers:** Automated fraud re-evaluation on milestone submission, approval, and rejection.
* **Ledger Monitor Bug Fix:** Fixed a database placeholder count mismatch bug (15 placeholders vs 16 values) in `ledgerMonitor.js` to ensure Stellar transaction indexing runs smoothly.

### 5. Admin Dashboard
* **Fraud Detection Tab:** Added a dedicated admin view.
* **KPI Metrics:** Displays dynamic stats on overall flagged campaigns, true/false positive counters, and the false positive rate.
* **Flag Breakdown:** Lists flagged campaigns with visual severity colors and full signal-by-signal detail/score breakdown.
* **One-Click Actions:** Allows admins to Approve (Clear flag and restore active status) or Freeze (suspend) campaigns with a single click.

---

## Verification & Testing

### Automated Test Output
Wrote unit tests covering low-risk and high-risk fraud/suspension scenarios.
`node --test src/services/fraudService.test.js`

▶ FraudService
✔ evaluateCampaign with low risk campaign remains active and unflagged (68.3472ms)
✔ evaluateCampaign flags and suspends campaign with high score (44.1028ms)
✔ FraudService (120.1252ms)
ℹ tests 2
ℹ suites 1
ℹ pass 2
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 994.4879

@drips-wave

drips-wave Bot commented Jul 3, 2026

Copy link
Copy Markdown

@barnabasolutayo-lgtm 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

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.

enhancement: Add suspicious activity detection and auto-freeze for campaigns

1 participant