Polytraders Dev Guide
internal
v3 spine Phase 1 · Shared contracts 9 demo-wired · 0 shadow-ready · 0 production-live · 100 pending · 109 total 15/33 infra tasks the plan status board
HomeBy LayerGovernance6.14 AttributionRevenueReporter

6.14 AttributionRevenueReporter

Governance Governance Service Explain PLANNED Spec started capital · Indirect P3 · Reporting & event store pending stub

AttributionRevenueReporter reconciles builder-code rebates with internal fill records and publishes a daily auditable SettlementReport. Retained 7 years for financial and regulatory compliance.

v3 readiness

Docs27/27
donehow scored
Impl0/15
pendinghow scored
Backtest0/4
pendinghow scored
Runtime0/8
pendinghow scored

A bot is done when all four scores are. What does done mean?

1. Bot Identity

LayerGovernance  Governance
Bot classGovernance Service
AuthorityExplain
StatusPLANNED
ReadinessSpec started
Runs beforeNothing — runs daily post-close
Runs afterBuilderAttribution reconciliation cycle
Applies toAll builder-code attributed fills within the reporting window
Default modeshadow_only
User-visiblesummary-only
Developer ownerPolytraders core

2. Purpose

AttributionRevenueReporter reconciles builder-code rebates with internal fill records and publishes a daily auditable SettlementReport. Retained 7 years for financial and regulatory compliance.

3. Why This Bot Matters

  • Rebate not reconciled

    Builder-code fee rebates may be under- or over-reported; financial records are inaccurate.

  • Per-strategy slice missing

    Revenue attribution to individual strategies is impossible; resource allocation decisions lack data.

No worked examples on this bot yet. Worked examples are optional but strongly recommended — they turn an abstract failure mode into something a developer can verify in a fixture.

4. Required Polymarket Inputs

InputSourceRequired?Use
Polymarket builder-code volume and rebate reportdataYesFetch official daily rebate figures for reconciliation.
Internal fill records from attribution loginternalYesCompare with official report to compute drift.

5. Required Internal Inputs

InputSourceRequired?Use
BuilderAttribution reconciliation outputgov.builderattributionYesSource of quarantine status and drift delta for each reconciliation window.
ExecutionReport streaminternal.report_busYesAggregate builder_fee_pusd per strategy and per user for the daily report.

6. Parameter Guide

ParameterDefaultWarningHardWhat it controls
report_cron0 6 * * *NoneNoneCron schedule for the daily attribution revenue report.
drift_alert_pct1.025Percentage drift between internal and official rebate figures that triggers an alert.

7. Detailed Parameter Instructions

report_cron

What it means

Cron schedule for the daily attribution revenue report.

Default

{ "report_cron": "0 6 * * *" }

Why this default matters

6 AM UTC runs after the exchange's daily close and BuilderAttribution reconciliation.

Threshold logic

ConditionAction
cron firesFetch official report; compute drift; emit SettlementReport

Developer check

scheduler.register(p.report_cron, generateReport)

User-facing English

Revenue attribution reports are published daily.

drift_alert_pct

What it means

Percentage drift between internal and official rebate figures that triggers an alert.

Default

{ "drift_alert_pct": 1.0 }

Why this default matters

1% drift is a reasonable threshold for fee reconciliation anomalies.

Threshold logic

ConditionAction
drift_pct > drift_alert_pctEmit REVENUE_DRIFT_DETECTED alert

Developer check

if abs(drift_pct) > p.drift_alert_pct: emit('REVENUE_DRIFT_DETECTED')

User-facing English

— not yet authored —

8. Default Configuration

{
  "bot_id": "gov.attributionrevenuereporter",
  "version": "0.1.0",
  "mode": "shadow_only",
  "defaults": {
    "report_cron": "0 6 * * *",
    "drift_alert_pct": 1.0,
    "publish_to": [
      "governance_audit",
      "finance_team"
    ],
    "retain_days": 2555
  }
}

9. Implementation Flow

  1. On cron schedule, fetch the Polymarket builder-code volume and rebate report for the previous day.
  2. Aggregate internal fill records from the ExecutionReport stream by strategy and by user for the same window.
  3. Compare total rebate_pusd from official report against sum of builder_fee_pusd from internal records.
  4. If drift > drift_alert_pct, emit REVENUE_DRIFT_DETECTED alert and quarantine the report pending review.
  5. Emit SettlementReport(event_type=REVENUE_REPORT) with per-strategy slices, drift delta, and reconciliation status.
  6. Publish report to governance_audit and finance_team recipients.

10. Reference Implementation

Pseudocode is language-agnostic. FETCH = read input. EMIT = produce output. IF/THEN/ELSE = decision. Translate directly to TypeScript, Python, Go, or Rust.

// ---- DAILY CRON JOB ----
FUNCTION generateReport(reportDate):
  window = {start: reportDate + T00:00Z, end: reportDate + T23:59Z}

  // Fetch official Polymarket rebate report
  official = FETCH data_api.GET(
    '/builder-code-report?code=polytraders&from=' + window.start + '&to=' + window.end
  )
  IF official IS NULL:
    scheduleRetry(generateReport, reportDate, delay=3600)
    EMIT SettlementReport(event_type='REPORT_DEFERRED', report_date=reportDate)
    RETURN

  // Aggregate internal fill records
  fills = postgres.select('attribution_log', WHERE fill_confirmed_at BETWEEN window)
  internalRebate = SUM(f.builder_fee_pusd FOR f IN fills)
  perStrategy = GROUP_BY(fills, 'strategy_slug', SUM('size_pusd'), SUM('builder_fee_pusd'))

  // Compute drift
  drift_pusd = abs(internalRebate - official.rebate_pusd)
  drift_pct = drift_pusd / official.rebate_pusd * 100 IF official.rebate_pusd > 0 ELSE 0

  IF drift_pct > config.drift_alert_pct:
    alerting.emit('REVENUE_DRIFT_DETECTED', {drift_pct, drift_pusd})

  // Emit SettlementReport
  EMIT SettlementReport(event_type='REVENUE_REPORT',
    report_date=reportDate,
    total_volume_pusd=SUM(f.size_pusd),
    total_rebate_pusd=internalRebate,
    official_rebate_pusd=official.rebate_pusd,
    drift_pusd=drift_pusd, drift_pct=drift_pct,
    drift_detected=drift_pct > config.drift_alert_pct,
    per_strategy=perStrategy,
    retained_until=now() + days(config.retain_days))

SDK calls used

  • data_api.GET('/builder-code-report?code=polytraders&from=...&to=...')
  • postgres.select('attribution_log', WHERE fill_confirmed_at BETWEEN ...)
  • alerting.emit('REVENUE_DRIFT_DETECTED', metadata)

Complexity: O(F) per report where F = fill count in window

11. Wire Examples

Input — what arrives on the wire

{
  "label": "Polymarket builder-code report",
  "source": "data_api",
  "payload": {
    "builder_code": "polytraders",
    "window_start": "2026-05-08T00:00:00Z",
    "window_end": "2026-05-08T23:59:59Z",
    "rebate_pusd": 120.8,
    "volume_pusd": 48320.5
  }
}

Output — what the bot emits

{
  "label": "SettlementReport — REVENUE_REPORT",
  "payload": {
    "report_id": "stl_revenue_01HX9Z",
    "event_type": "REVENUE_REPORT",
    "drift_detected": false,
    "report_kind": "SettlementReport",
    "topic": "polytraders.reports.settlement",
    "retained_until": "2033-05-08"
  }
}

12. Decision Logic

APPROVE

Not applicable — AttributionRevenueReporter is a reporting and reconciliation service.

RESHAPE_REQUIRED

Not applicable.

REJECT

Quarantines the report if drift > drift_alert_pct.

WARNING_ONLY

Emits REVENUE_DRIFT_DETECTED warn when drift is present but within manageable range.

13. Standard Decision Output

This bot returns a SettlementReport object. See SettlementReport schema.

{
  "report_id": "stl_attributionrevenuereporter_01HX9Z",
  "bot_id": "gov.attributionrevenuereporter",
  "event_type": "REVENUE_REPORT",
  "report_date": "2026-05-08",
  "total_volume_pusd": 48320.5,
  "total_rebate_pusd": 120.8,
  "official_rebate_pusd": 120.8,
  "drift_pusd": 0.0,
  "drift_pct": 0.0,
  "drift_detected": false,
  "per_strategy": [
    {
      "strategy": "sports-model",
      "volume_pusd": 30000.0,
      "rebate_pusd": 75.0
    }
  ],
  "report_kind": "SettlementReport",
  "topic": "polytraders.reports.settlement",
  "retained_until": "2033-05-08"
}

14. Reason Codes

CodeSeverityMeaningActionUser-facing message
REVENUE_REPORTINFODaily attribution revenue report generated and emitted.Log and store.
REVENUE_DRIFT_DETECTEDWARNDrift between internal and official rebate figures exceeds threshold.Emit alert; quarantine report pending review.A discrepancy was found in today's revenue report.
REPORT_DEFERREDWARNData API unavailable at report time; retry scheduled.Emit WARN; schedule retry.
RETENTION_BELOW_REGULATORY_MINIMUMWARNretain_days < 2555.Emit WARN; refuse config change.
KILL_SWITCH_ACTIVEWARNKillSwitch active; report notes trading was halted during window.Include kill-switch note in report.

15. Metrics & Logs

Metrics emitted

MetricTypeUnitLabelsMeaning
polytraders_gov_attributionrevenuereporter_reports_totalcountercountstatusTotal daily reports generated by status (complete/deferred/quarantined).
polytraders_gov_attributionrevenuereporter_drift_pusdgaugeusdRevenue drift in pUSD at last report.
polytraders_gov_attributionrevenuereporter_rebate_pusd_totalcounterusdCumulative rebate pUSD recorded.
polytraders_gov_attributionrevenuereporter_deferred_reportsgaugecountNumber of reports currently deferred pending Data API.

Alerts

AlertConditionSeverityRunbook
AttributionRevenueDriftpolytraders_gov_attributionrevenuereporter_drift_pusd > 0P1#runbook-attributionrevenue-drift
AttributionRevenueDeferredReportpolytraders_gov_attributionrevenuereporter_deferred_reports > 0P2#runbook-attributionrevenue-deferred

16. Developer Reporting

{
  "bot_id": "gov.attributionrevenuereporter",
  "event_type": "REPORT_GENERATED",
  "report_date": "2026-05-08",
  "fetch_latency_ms": 230,
  "strategies_included": 3
}

17. Plain-English Reporting

SituationUser-facing explanation
Daily revenue report publishedToday's builder attribution revenue report has been published. All records are available in the governance audit log.
Revenue drift detectedA discrepancy was found between internal records and the official rebate report. The affected records are under review.

18. Failure-Mode Block

main_failure_modeData API is unavailable at report time; official rebate figures cannot be fetched.
false_positive_riskA temporary Data API format change causes a spurious drift detection.
false_negative_riskInternal fill records are incomplete for the window, causing drift to appear smaller than it is.
safe_fallbackIf Data API is unavailable, defer the report and retry hourly. Emit REPORT_DEFERRED alert.
required_dependenciesPolymarket Data API (builder-code report), internal.report_bus (ExecutionReport), gov.builderattribution (reconciliation output)

19. Failure-Injection Recipes

ScenarioHow to injectExpected behaviourRecovery
DATA_API_UNAVAILABLEBlock TCP to data-api.polymarket.com at report timeAutomatic when Data API recovers.
REVENUE_DRIFTInsert synthetic fill with builder_fee_pusd=10 not in official reportManual review; remove synthetic record; re-run report.
DUPLICATE_REPORT_RUNTrigger two cron runs for the same report_dateIdempotent — no action needed.

20. State & Persistence

Cold-start recovery

On restart, check for any deferred reports and retry immediately.

21. Concurrency & Idempotency

AspectSpecification
Execution modelsingle scheduled job per day
Max in-flight2
Idempotency keyreport_date
Per-call timeout (ms)30000
Backpressure strategyskip if previous job still running
Locking / mutual exclusionPostgres unique constraint on report_date

22. Dependencies

Depends on (must run first)

BotWhyContract
gov.builderattributionBuilderAttribution provides reconciliation status and quarantine counts.Reconciliation must complete before daily report runs.

Emits to (downstream consumers)

BotWhyContract
internal.post_trade_archive

Sibling bots (same OrderIntent)

BotWhyContract
gov.posttradeexplainerPostTradeExplainer provides per-fill explanation context referenced in revenue reports.SettlementReport includes builder_fee_pusd.

External services

ServiceEndpointSLA assumedOn failure
Polymarket Data APIhttps://data-api.polymarket.com99.9% / 500ms p99Defer report; retry hourly; emit REPORT_DEFERRED.

23. Security Surfaces

Abuse vectors considered

  • Manipulating internal fill records to inflate reported rebate figures

Mitigations

  • Internal fill records are immutably written; any manipulation would show as drift against the official Polymarket report

24. Polymarket V2 Compatibility

AspectValue
CLOB versionv2
Collateral assetpUSD
EIP-712 Exchange domain version2
Aware of builderCode fieldyes
Aware of negative-risk marketsno
Multi-chain readyno
SDK usedpy-clob-client-v2
Settlement contractCTFExchangeV2
NotesAttributionRevenueReporter reconciles builder-code rebates in pUSD; reads builder_fee_pusd from fill records.

API surfaces declared

datainternal

Networks supported

polygon

25. Versioning & Migration

FieldValue
spec2.0.0
implementation0.1.0
schema2
releasedNone
planned_releaseQ4-2026

Migration history

DateFromToReasonAction taken
2026-04-28n/av2-specSpec drafted post-CLOB-V2 cutover; bot not yet implementedDesigned against V2 schema (pUSD, builder codes, V2 EIP-712 domain)

26. Acceptance Tests

Unit Tests

TestSetupExpected result
Drift detection fires when drift_pct exceeds thresholdinternal_rebate=120, official_rebate=122, drift_alert_pct=1.0REVENUE_DRIFT_DETECTED alert; report quarantined
Per-strategy slices sum to total volume3 strategies with volumes 30k, 12k, 6.3ktotal_volume_pusd = 48320 in SettlementReport

Integration Tests

TestExpected result
End-to-end: cron fires → fetch official report → reconcile → SettlementReport emittedSettlementReport on polytraders.reports.settlement with report_kind=SettlementReport

Property Tests

PropertyRequired behaviour
Every daily report is retained for >= 2555 daysAlways true

27. Operational Runbook

AttributionRevenueReporter incidents are usually revenue drift (P1) or deferred reports due to Data API unavailability.

On-call actions

AlertFirst stepDiagnosisMitigationEscalate to
AttributionRevenueDrift
AttributionRevenueDeferredReport

Manual overrides

Healthcheck

/internal/health/attributionrevenuereporter → green if Last report completed today; drift_pusd == 0; no deferred reports; red if Drift detected or report deferred for > 2h

28. Promotion Gates

A bot does not advance to the next readiness state until every gate below is green. Gates are observable from production data — no subjective sign-off.

Promote to Shadow

GateHow measuredThreshold
Drift detection unit test passesCIPass

Promote to Limited live

GateHow measuredThreshold
Daily report end-to-end test in staging produces correct per-strategy slicesIntegration testPass

Promote to General live

GateHow measuredThreshold
Finance team sign-off on revenue report format and 7-year retentionFinance team reviewPass

29. Developer Checklist

Ready-to-ship score: 27/27 sections complete · 100%

RequirementStatus
Purpose defined✓ done
Required inputs listed✓ done
Parameters defined✓ done
Defaults defined✓ done
Warning thresholds defined✓ done
Hard thresholds defined✓ done
Safe fallback defined✓ done
Structured output defined✓ done
Developer log defined✓ done
Plain-English explanation✓ done
Unit tests defined✓ done
Integration tests defined✓ done
Property tests defined✓ done
Failure-mode block complete✓ done
Reference implementation pseudocode✓ done
Wire examples (input + output)✓ done
Reason codes listed✓ done
Metrics & logs defined✓ done
State & persistence defined✓ done
Concurrency & idempotency defined✓ done
Dependencies declared✓ done
Security surfaces declared✓ done
Polymarket V2 compatibility declared✓ done
Version & migration history declared✓ done
Operational runbook defined✓ done
Promotion gates defined✓ done
Failure-injection recipes defined✓ done