🚚

Fuel Logistics with GPS Tracking

Contract + GPS Sub-Machine Coordination

Step 1 of 15

📋

Create Fuel Contract

Fuel Contract draft

Create the main fuel contract state machine. This will track the entire delivery workflow from draft to settlement.

📋 Contract State

{
  "contractId": "FC-2025-001",
  "buyer": "DAG3oEy...BMUXe",
  "fuelType": "Diesel",
  "fuelQuantity": 5000,
  "pricePerLiter": 2,
  "status": "draft"
}

📍 GPS Tracker State

Not created yet

Key Concepts Demonstrated

🔗

Cross-Machine Dependencies

Steps 5, 7, 11: Contract reads GPS state using machines.{gpsTrackerCid}.state.status

🔄

Self-Transitions

Steps 8-10: GPS submits 3 separate ProcessFiberEvent messages with eventType="log_position", staying in "tracking" state while accumulating data (dataPointCount: 1→2→3→4)

📊

Data Aggregation

Step 12: Contract copies totalDistance (15.9 km) and gpsDataPoints (4) from GPS machine into its state

💰

Computed Fields

Step 13: Settlement calculates totalAmount = fuelQuantity * pricePerLiter using JSON Logic

❌ Error Scenario: Insufficient GPS Data

What happens if we try to confirm delivery without enough GPS data points?

Scenario:

  • Contract is in "in_transit" state
  • GPS is "stopped" ✓
  • But GPS only has 2 data points (requirement is 3) ✗

Guard Check:

GPS status === "stopped" AND dataPointCount >= 3
"stopped" === "stopped" ✓ AND 2 >= 3 ✗
Result: GUARD FAILS ✗

Result: The transition from "in_transit" to "delivered" is BLOCKED. The contract cannot proceed to delivery confirmation until GPS has logged at least 3 position updates. This ensures the delivery route is properly tracked before payment is authorized.