A robust Clarity smart contract for tracking and monitoring temperature-sensitive pharmaceutical shipments on the Stacks blockchain. Ensures drug integrity through automated temperature monitoring, breach detection, and compliance enforcement.
This smart contract provides a complete cold chain management solution that automatically monitors temperature conditions, records breaches, and quarantines compromised shipments to protect pharmaceutical integrity.
- Create shipments with customizable temperature ranges
- Track shipment lifecycle (Created → In Transit → Delivered)
- Automatic status updates based on temperature compliance
- Owner and carrier authorization controls
- Real-time temperature and humidity recording
- Geolocation tracking for each reading
- Automatic breach detection when limits exceeded
- Severity classification (1-3) based on deviation magnitude
- Automatic breach logging with timestamps
- Classification: "below-min" or "above-max"
- Maintains breach history (up to 50 per shipment)
- Auto-quarantine after 3+ breaches
- Contract owner controls carrier/monitor authorization
- Role-based access control (Owner, Carrier, Monitor)
- Secure validation of all inputs
Shipments Map
- Drug details (name, origin, destination)
- Temperature thresholds (min/max)
- Status tracking and breach counts
- Ownership and carrier information
Temperature Readings Map
- Shipment association
- Temperature and humidity values
- Location and timestamp data
- Monitor identification
Temperature Breaches Map
- Breach details and classification
- Severity levels (1-3)
- Resolution status
- Timestamp tracking
status-created (u1)- Shipment initializedstatus-in-transit (u2)- Active monitoringstatus-delivered (u3)- Successfully completedstatus-breached (u4)- Temperature violation detectedstatus-quarantined (u5)- Multiple breaches, requires review
err-owner-only (u100)- Unauthorized owner operationerr-not-found (u101)- Resource doesn't existerr-unauthorized (u102)- Insufficient permissionserr-invalid-temp (u103)- Invalid temperature rangeerr-already-exists (u104)- Duplicate resourceerr-shipment-completed (u105)- Operation on completed shipmenterr-invalid-status (u106)- Invalid status transitionerr-breach-exists (u107)- Breach limit exceedederr-invalid-input (u108)- Invalid input parameters
add-authorized-carrier
(add-authorized-carrier (carrier principal))Authorizes a carrier to transport shipments. Owner only.
add-authorized-monitor
(add-authorized-monitor (monitor principal))Authorizes a monitor to record temperature readings. Owner only.
remove-authorized-carrier
(remove-authorized-carrier (carrier principal))Revokes carrier authorization. Owner only.
create-shipment
(create-shipment
(drug-name (string-ascii 100))
(origin (string-ascii 100))
(destination (string-ascii 100))
(carrier principal)
(min-temp int)
(max-temp int))Creates a new pharmaceutical shipment with specified parameters. Returns shipment ID.
start-shipment
(start-shipment (shipment-id uint))Transitions shipment to in-transit status. Owner or carrier only.
complete-shipment
(complete-shipment (shipment-id uint))Marks shipment as delivered. Carrier only.
quarantine-shipment
(quarantine-shipment (shipment-id uint))Manually quarantines a shipment. Owner or shipment owner only.
record-temperature
(record-temperature
(shipment-id uint)
(temperature int)
(humidity uint)
(location (string-ascii 100)))Records temperature reading. Automatically detects and logs breaches. Authorized monitors only.
get-shipment- Retrieve shipment detailsget-reading- Retrieve temperature readingget-breach- Retrieve breach informationget-shipment-breaches- Get all breach IDs for a shipmentis-carrier-authorized- Check carrier authorizationis-monitor-authorized- Check monitor authorizationget-shipment-status- Get current shipment statusget-total-shipments- Get total number of shipmentsget-total-readings- Get total number of readingsis-shipment-compliant- Check if shipment meets compliance
;; Owner authorizes carrier and monitor
(contract-call? .pharma-cold-chain add-authorized-carrier 'SP2CARRIER...)
(contract-call? .pharma-cold-chain add-authorized-monitor 'SP2MONITOR...);; Create vaccine shipment requiring 2-8°C
(contract-call? .pharma-cold-chain create-shipment
"COVID-19 Vaccine"
"Manufacturing Plant A"
"Hospital B"
'SP2CARRIER...
2 ;; min temp (°C)
8) ;; max temp (°C)
;; Returns: (ok u1)(contract-call? .pharma-cold-chain start-shipment u1);; Monitor records compliant temperature
(contract-call? .pharma-cold-chain record-temperature
u1
5 ;; temperature (°C)
u65 ;; humidity (%)
"Distribution Center")
;; Returns: (ok u1)
;; Recording breach (10°C exceeds max of 8°C)
(contract-call? .pharma-cold-chain record-temperature
u1
10 ;; temperature breach!
u70
"Warehouse")
;; Automatically logs breach and updates status(contract-call? .pharma-cold-chain is-shipment-compliant u1)
;; Returns: (ok true) if fewer than 3 breaches(contract-call? .pharma-cold-chain complete-shipment u1)The contract automatically:
- Detects temperature violations during recording
- Calculates severity based on deviation:
- Severity 1: 0-2°C deviation
- Severity 2: 2-5°C deviation
- Severity 3: >5°C deviation
- Records breach with type (below-min/above-max)
- Quarantines shipment after 3+ breaches
✅ Input Validation - All inputs validated before processing
✅ Role-Based Access - Function-level authorization checks
✅ Immutable Audit Trail - All readings timestamped on-chain
✅ Automatic Enforcement - Quarantine triggers on violations
✅ Principal Validation - Prevents invalid address operations
- Language: Clarity
- Blockchain: Stacks
- Code Lines: 299
- Max Breaches Per Shipment: 50
- String Limits: 100 characters
- Humidity Range: 0-100%
This contract supports pharmaceutical cold chain compliance including:
- WHO temperature monitoring guidelines
- GDP (Good Distribution Practice) requirements
- CFR Title 21 pharmaceutical storage standards
- Real-time monitoring and documentation
- Test authorization workflows
- Test shipment lifecycle transitions
- Test breach detection at boundaries
- Test quarantine triggering
- Test unauthorized access attempts
- Test input validation edge cases
- Deploy with contract owner as trusted entity
- Authorize carriers and monitors before operations
- Consider implementing notification system off-chain
- Monitor breach patterns for quality improvements
- Vaccine Distribution - COVID-19, flu vaccines requiring 2-8°C
- Biologics Transport - Temperature-sensitive medications
- Clinical Trial Materials - Strict temperature compliance
- Blood Products - Critical temperature maintenance
- Insulin Shipments - Refrigerated pharmaceutical transport
- Multi-signature approval for quarantine resolution
- Integration with IoT temperature sensors
- Automated alerts via oracles
- Carbon credits for compliant shipments
- Insurance claim automation on breaches