A comprehensive Clarity smart contract for decentralized physical commodities trading on the Stacks blockchain. This contract enables secure peer-to-peer trading of physical commodities with built-in inventory management, dispute resolution, and delivery confirmation systems.
- Create Trade Listings: Sellers can list commodities with detailed specifications
- Accept Trades: Buyers can accept listings with automatic fund transfers
- Cancel Trades: Sellers can cancel active listings
- Delivery Confirmation: Buyers confirm receipt of goods
- Dispute Resolution: Authorized inspectors can resolve trade disputes
- User Balances: Deposit and withdraw funds securely
- Platform Fees: Configurable transaction fees (default 0.25%)
- Automatic Transfers: Secure fund transfers between parties
- Fee Collection: Platform fees automatically collected on trades
- Real-time Inventory: Track commodity quantities for each user
- Automatic Updates: Inventory updated on trade creation/completion
- Overselling Prevention: Balance checks prevent invalid trades
- Owner Controls: Admin functions for contract management
- Inspector Authorization: Qualified inspectors for dispute resolution
- Input Validation: Comprehensive validation of all user inputs
- Status Management: Contract can be paused/resumed by owner
trades: Store trade details and statususer-balances: Track user fund balancescommodity-inventory: Track commodity quantities per userauthorized-inspectors: Manage dispute resolution authorities
ACTIVE: Trade is available for acceptancePENDING_DELIVERY: Trade accepted, awaiting deliveryDELIVERED: Trade completed successfullyCANCELLED: Trade cancelled by sellerDISPUTED: Trade under dispute resolution
(contract-call? .commodities-trading create-trade
"WHEAT" ;; commodity
u1000 ;; quantity (1000 units)
u50 ;; price per unit (50 tokens)
u100000 ;; expires at block height
"GRADE_A" ;; quality grade
"Chicago, IL" ;; delivery location
)(contract-call? .commodities-trading accept-trade u1)(contract-call? .commodities-trading confirm-delivery u1)| Code | Constant | Description |
|---|---|---|
| u100 | err-owner-only |
Operation requires contract owner |
| u101 | err-not-found |
Trade or resource not found |
| u102 | err-unauthorized |
Unauthorized access attempt |
| u103 | err-invalid-amount |
Invalid amount or quantity |
| u104 | err-insufficient-balance |
Insufficient funds or inventory |
| u105 | err-trade-not-active |
Trade is not in active status |
| u106 | err-already-exists |
Resource already exists |
| u107 | err-invalid-status |
Invalid status transition |
| u108 | err-delivery-failed |
Delivery confirmation failed |
| u109 | err-invalid-input |
Input validation failed |
(contract-call? .commodities-trading set-platform-fee u30) ;; 0.30%(contract-call? .commodities-trading authorize-inspector 'SP1ABC...)(contract-call? .commodities-trading set-contract-status false) ;; pause
(contract-call? .commodities-trading set-contract-status true) ;; resume(contract-call? .commodities-trading get-trade u1)(contract-call? .commodities-trading get-user-balance 'SP1ABC...)(contract-call? .commodities-trading get-inventory 'SP1ABC... "WHEAT")(contract-call? .commodities-trading get-contract-stats)- Input Validation: All user inputs are thoroughly validated
- Access Control: Role-based permissions for sensitive operations
- Balance Checks: Prevents overdrafts and overselling
- Status Validation: Ensures proper trade state transitions
- Principal Verification: Prevents unauthorized access to trades
- Deploy the contract to Stacks testnet/mainnet
- The deploying principal becomes the contract owner
- Set initial platform fee if different from default (0.25%)
- Authorize inspectors for dispute resolution
- Contract starts in active state by default
The contract is optimized for gas efficiency:
- Minimal storage reads/writes
- Efficient data structures
- Consolidated operations where possible
- Under 300 lines for optimal deployment cost