A robust Clarity smart contract for the Stacks blockchain that tracks ownership and condition history of secondhand goods. This contract provides immutable, auditable records of item transfers and status updates.
This smart contract enables users to:
- Register secondhand goods with metadata
- Transfer ownership with historical tracking
- Update item condition and status
- Retrieve complete ownership and status history
- Verify ownership at any point in time
All operations are recorded on the blockchain, creating an immutable audit trail for transparency and trust in the secondhand goods market.
- Robust Input Validation: All user inputs are validated before storage
- Complete History Tracking: Maintains full ownership and status change history
- Access Control: Only item owners can transfer or update their goods
- Comprehensive Error Handling: 7 distinct error codes for different failure scenarios
- Read-Only Queries: Safe functions to retrieve data without state changes
- Block Height Tracking: Timestamp tracking using Stacks block height
Stores primary information about each registered good: ``` { good-id: uint, title: string-ascii (100 chars max), description: string-ascii (256 chars max), current-owner: principal, current-status: string-ascii (20 chars max), condition: uint (1-10 scale), registration-block: uint } ```
Records every ownership transfer: ``` { good-id: uint, history-index: uint, previous-owner: principal, new-owner: principal, transfer-block: uint, transfer-reason: string-ascii (64 chars max) } ```
Records every condition and status update: ``` { good-id: uint, status-index: uint, old-status: string-ascii (20 chars max), new-status: string-ascii (20 chars max), condition-rating: uint (1-10 scale), update-block: uint } ```
Registers a new secondhand good in the system.
Parameters:
title: Item name (1-100 ASCII characters)description: Item details (1-256 ASCII characters)
Returns: (ok uint) - The assigned good-id
Caller: Any principal
Initial State: Status = "registered", Condition = 10/10
Transfers ownership of a good to a new owner.
Parameters:
good-id: ID of the good to transfernew-owner: Principal receiving the goodreason: Transfer reason (1-64 ASCII characters)
Returns: (ok true) on success
Caller: Current owner only
Side Effect: Creates ownership history entry
Updates the condition rating and status of a good.
Parameters:
good-id: ID of the good to updatenew-status: New status (1-20 ASCII characters)condition-rating: Condition on 1-10 scale
Returns: (ok true) on success
Caller: Current owner only
Validation: Condition must be between 1-10
Retrieves complete information about a good.
Retrieves a specific ownership transfer record.
Retrieves a specific status change record.
Returns total number of goods registered.
Returns the next available good-id.
Checks if a principal owns a specific good.
Returns the current condition rating of a good.
| Code | Name | Meaning |
|---|---|---|
| u401 | ERR-UNAUTHORIZED | Access denied |
| u402 | ERR-INVALID-GOOD | Good ID not found |
| u403 | ERR-INVALID-STATUS | Invalid status or condition value |
| u404 | ERR-ALREADY-EXISTS | Good already registered |
| u405 | ERR-NOT-OWNER | Caller is not the owner |
| u406 | ERR-INVALID-INPUT | Invalid input parameter |
;; Register a used laptop
(contract-call? .secondhand-goods register-good
"MacBook Pro 2020"
"15-inch, 16GB RAM, excellent condition")
;; Returns: (ok u1)
;; Update condition after inspection
(contract-call? .secondhand-goods update-condition
u1
"inspected"
u9)
;; Returns: (ok true)
;; Transfer to new owner
(contract-call? .secondhand-goods transfer-ownership
u1
'SP2C2YB838J624XPN3Q95GEQ3V77ZSC451DX4RZP7
"sold via marketplace")
;; Returns: (ok true)
;; Check ownership
(contract-call? .secondhand-goods verify-ownership
u1
'SP2C2YB838J624XPN3Q95GEQ3V77ZSC451DX4RZP7)
;; Returns: (ok true)