A comprehensive Clarity smart contract for the Stacks blockchain that enables transparent tracking and verification of conflict-free diamonds throughout their entire supply chain.
This smart contract provides a decentralized system for registering, verifying, and tracking the ownership chain of diamonds, ensuring transparency and authenticity in the diamond trade. It helps combat conflict diamonds by maintaining an immutable record of diamond provenance.
- Contract Owner: Full administrative control
- Authorized Verifiers: Certified entities that can verify diamond authenticity
- Multi-level Permission Controls: Role-based access for secure operations
- Register diamonds with unique identifiers
- Record essential details: origin, carat weight, certification number, mining date
- Automatic timestamp tracking
- Status management (pending/verified/rejected)
- Only authorized verifiers can approve/reject diamonds
- Prevents duplicate verification
- Immutable verification records
- Verifier attribution for accountability
- Complete transfer history for each diamond
- Transfer notes and timestamps
- Only verified diamonds can be transferred
- Prevents fraudulent ownership claims
- Comprehensive input validation on all user inputs
- 9 distinct error codes for precise error handling
- Prevention of self-transfers
- Immutable verification status once approved
- Owner-only update permissions before verification
{
owner: principal,
origin: string (max 100 chars),
carat-weight: uint,
certification-number: string (max 64 chars),
mining-date: uint (block height),
is-verified: bool,
verifier: optional principal,
status: string ("pending", "verified", "rejected"),
created-at: uint (block height)
}{
from-owner: principal,
to-owner: principal,
transfer-date: uint (block height),
notes: string (max 256 chars)
}(add-verifier (verifier principal))Adds an authorized verifier. Only callable by contract owner.
(remove-verifier (verifier principal))Removes an authorized verifier. Only callable by contract owner.
(register-diamond
(diamond-id (string-ascii 64))
(origin (string-ascii 100))
(carat-weight uint)
(cert-number (string-ascii 64))
(mining-date uint))Registers a new diamond in the system. The caller becomes the initial owner.
Validations:
- Diamond ID must be unique and non-empty
- Origin must be non-empty
- Carat weight must be between 1-100,000
- Mining date must be valid and not in the future
- Certification number must be non-empty
(verify-diamond (diamond-id (string-ascii 64)) (approved bool))Verifies or rejects a diamond. Only callable by authorized verifiers.
Requirements:
- Caller must be an authorized verifier
- Diamond must not already be verified
(transfer-diamond
(diamond-id (string-ascii 64))
(new-owner principal)
(notes (string-ascii 256)))Transfers diamond ownership to a new owner.
Requirements:
- Caller must be the current owner
- Diamond must be verified
- Cannot transfer to self
- Creates an immutable custody chain record
(update-diamond-origin
(diamond-id (string-ascii 64))
(new-origin (string-ascii 100)))Updates the origin information of a diamond.
Requirements:
- Caller must be the owner
- Diamond must not be verified yet
Returns complete diamond information including verification status and ownership.
Returns the current owner of a specific diamond.
Returns a specific transfer record from the custody chain.
Returns the total number of transfers for a diamond.
Checks if a diamond has been verified.
Returns the current status ("pending", "verified", or "rejected").
Returns comprehensive information including diamond data and transfer count.
Checks if a principal is an authorized verifier.
Returns the total number of authorized verifiers.
| Code | Constant | Description |
|---|---|---|
| u100 | err-owner-only |
Action requires contract owner privileges |
| u101 | err-not-found |
Diamond or record not found |
| u102 | err-already-exists |
Diamond ID or verifier already exists |
| u103 | err-unauthorized |
Caller not authorized for this action |
| u104 | err-invalid-status |
Invalid diamond status |
| u105 | err-not-authorized-verifier |
Caller is not an authorized verifier |
| u106 | err-already-verified |
Diamond already verified |
| u107 | err-invalid-transfer |
Transfer conditions not met |
| u108 | err-invalid-input |
Input validation failed |
Deploy the contract to Stacks blockchain. The deployer becomes the contract owner.
(contract-call? .diamond-verification add-verifier 'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7)(contract-call? .diamond-verification register-diamond
"DIA-001-2024-KIM"
"Kimberley, South Africa"
u150
"GIA-2157893456"
u800000)(contract-call? .diamond-verification verify-diamond "DIA-001-2024-KIM" true)(contract-call? .diamond-verification transfer-diamond
"DIA-001-2024-KIM"
'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7
"Sale to authorized dealer - Invoice #12345")(contract-call? .diamond-verification get-diamond-full-details "DIA-001-2024-KIM")- Input Validation: All user inputs are validated before processing
- Access Control: Role-based permissions prevent unauthorized actions
- Immutability: Verified diamonds cannot have their verification status changed
- Transparency: All transfers are recorded in an immutable custody chain
- Prevention of Fraud: Self-transfers and unverified diamond transfers are blocked
Language: Clarity
Blockchain: Stacks
Lines of Code: 287
Dependencies: None (uses only Clarity built-in functions)
- Deploy contract successfully
- Add and remove verifiers (owner only)
- Register diamonds with valid data
- Reject invalid diamond registrations
- Verify diamonds (verifier only)
- Transfer verified diamonds
- Prevent transfers of unverified diamonds
- Query custody chain history
- Update origin before verification
- Prevent updates after verification