Skip to content

Contract: Create On-Chain Analytics Tracker for Platform Metrics #514

@mftee

Description

@mftee

Description:
Build a smart contract that aggregates and stores key platform metrics on-chain, providing transparent, verifiable statistics about platform usage and performance.

Contract Purpose:
Track and aggregate important platform metrics on-chain to provide transparent analytics that anyone can verify, supporting data-driven decision making and platform transparency.

Core Functionality to Implement:

1. Data Structures

PlatformMetrics:

  • total_shipments
  • total_completed_shipments
  • total_active_shipments
  • total_cancelled_shipments
  • total_users
  • total_carriers
  • total_shippers
  • total_volume_usd (total value of all shipments)
  • total_distance_km (sum of all shipment distances)
  • total_payments_processed
  • last_updated (timestamp)

DailySnapshot:

  • date (UNIX timestamp truncated to day)
  • new_shipments
  • completed_shipments
  • new_users
  • active_carriers
  • volume_usd
  • average_delivery_time_hours
  • on_time_percentage

CarrierMetrics:

  • carrier_address
  • total_shipments_completed
  • total_shipments_active
  • total_revenue
  • average_rating
  • on_time_deliveries
  • late_deliveries
  • total_distance_km
  • last_updated

ShipperMetrics:

  • shipper_address
  • total_shipments_created
  • total_shipments_completed
  • total_spent
  • average_cost_per_shipment
  • preferred_carriers (array of top 3)
  • last_shipment_timestamp

RouteMetrics:

  • route_hash (hash of origin + destination)
  • origin
  • destination
  • shipment_count
  • average_cost
  • average_duration_hours
  • most_common_carrier

2. Storage

  • PlatformMetrics singleton (one global record)
  • Map of date to DailySnapshot
  • Map of carrier_address to CarrierMetrics
  • Map of shipper_address to ShipperMetrics
  • Map of route_hash to RouteMetrics
  • Array of last 30 daily snapshots (for quick access)

3. Contract Functions

Record Shipment Created:

  • Takes shipment_id, shipper_address, origin, destination, value
  • Increments total_shipments
  • Increments total_active_shipments
  • Updates shipper metrics
  • Updates route metrics
  • Updates today's snapshot
  • Only callable by Shipment contract
  • Emits ShipmentRecorded event

Record Shipment Completed:

  • Takes shipment_id, carrier_address, shipper_address, final_cost, duration_hours, distance_km, on_time
  • Increments total_completed_shipments
  • Decrements total_active_shipments
  • Updates carrier metrics (completed, revenue, on_time/late)
  • Updates shipper metrics (completed, total_spent)
  • Updates route metrics
  • Adds to total_distance_km
  • Updates today's snapshot
  • Only callable by Shipment contract
  • Emits ShipmentCompletedRecorded event

Record Shipment Cancelled:

  • Takes shipment_id
  • Increments total_cancelled_shipments
  • Decrements total_active_shipments
  • Only callable by Shipment contract
  • Emits ShipmentCancelledRecorded event

Record User Registered:

  • Takes user_address, user_type (Carrier or Shipper)
  • Increments total_users
  • If Carrier, increment total_carriers
  • If Shipper, increment total_shippers
  • Initializes CarrierMetrics or ShipperMetrics
  • Updates today's snapshot
  • Only callable by Access Control contract
  • Emits UserRegisteredRecorded event

Record Payment:

  • Takes payment_amount
  • Increments total_payments_processed
  • Adds to total_volume_usd
  • Only callable by Escrow contract
  • Emits PaymentRecorded event

Get Platform Metrics:

  • Returns complete PlatformMetrics struct
  • Public read function

Get Daily Snapshot:

  • Takes date (timestamp)
  • Returns DailySnapshot for that date
  • Public read function

Get Carrier Metrics:

  • Takes carrier_address
  • Returns CarrierMetrics
  • Public read function

Get Shipper Metrics:

  • Takes shipper_address
  • Returns ShipperMetrics
  • Public read function

Get Route Metrics:

  • Takes origin and destination
  • Computes route_hash
  • Returns RouteMetrics
  • Public read function

Get Top Routes:

  • Returns array of top 10 routes by shipment_count
  • Public read function

Get Top Carriers:

  • Returns array of top 10 carriers by shipments_completed
  • Public read function

Get Weekly Summary:

  • Aggregates last 7 daily snapshots
  • Returns totals and averages for the week
  • Public read function

Get Monthly Summary:

  • Aggregates last 30 daily snapshots
  • Returns totals and averages for the month
  • Public read function

Calculate Average Delivery Time:

  • Aggregates delivery times from completed shipments
  • Returns average in hours
  • Public read function

Calculate On-Time Percentage:

  • Calculates (on_time_deliveries / total_completed) * 100
  • Returns percentage
  • Public read function

4. Events

  • ShipmentRecorded(shipment_id, shipper, value, timestamp)
  • ShipmentCompletedRecorded(shipment_id, carrier, cost, duration, on_time, timestamp)
  • ShipmentCancelledRecorded(shipment_id, timestamp)
  • UserRegisteredRecorded(user_address, user_type, timestamp)
  • PaymentRecorded(amount, timestamp)
  • SnapshotCreated(date, new_shipments, completed_shipments, volume, timestamp)

5. Daily Snapshot Creation

  • Automatically create new snapshot at midnight UTC
  • Triggered by first transaction of new day
  • Carries forward cumulative metrics
  • Resets daily counters to zero

Aggregation Calculations:

Average Delivery Time:

Total Delivery Hours / Number of Completed Shipments

On-Time Percentage:

(On-Time Deliveries / Total Completed Deliveries) × 100

Average Cost per Route:

Total Cost for Route / Number of Shipments on Route

Carrier Efficiency Score:

(Completed Shipments × Average Rating × On-Time %) / 100

Integration Points:

  • Shipment contract calls record functions when events happen
  • Escrow contract calls record_payment on payment processing
  • Access Control contract calls record_user on registration
  • All calls are one-way (analytics doesn't affect other contracts)

Access Control:

  • Only authorized contracts can record metrics
  • All read functions are public
  • No manual metric updates (only through contract calls)
  • Ensures data integrity and accuracy

Acceptance Criteria:

  • Platform metrics initialize correctly
  • Shipment creation updates all relevant metrics
  • Shipment completion updates carrier and shipper metrics
  • Cancellation decrements active shipments
  • User registration increments counts correctly
  • Payment recording updates totals
  • Daily snapshots create automatically
  • Get functions return accurate data
  • Top routes calculation correct
  • Top carriers calculation correct
  • Weekly/monthly summaries aggregate correctly
  • Average calculations accurate
  • All recording restricted to authorized contracts
  • All events emit correctly
  • Unit tests cover all functions (>80% coverage)
  • Gas-optimized (efficient aggregations)

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions