strade-stx 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/.activity_counter +1 -0
  2. package/.gitattributes +3 -0
  3. package/.vscode/settings.json +4 -0
  4. package/.vscode/tasks.json +19 -0
  5. package/CHANGELOG.md +1 -0
  6. package/Clarinet.toml +56 -0
  7. package/Clarinet.toml.backup +174 -0
  8. package/Clarinet.toml.old +146 -0
  9. package/DEPLOYMENT_RESULTS.md +160 -0
  10. package/README.md +344 -0
  11. package/TODO.md +34 -0
  12. package/contracts/CoreMarketPlace.clar +227 -0
  13. package/contracts/DisputeResolution_clar.clar +265 -0
  14. package/contracts/EscrowService.clar +171 -0
  15. package/contracts/UserProfile.clar +280 -0
  16. package/contracts/ft-trait.clar +24 -0
  17. package/contracts/token.clar +178 -0
  18. package/costs-reports.json +76026 -0
  19. package/deployments/default.mainnet-plan.yaml +67 -0
  20. package/deployments/default.simnet-plan.yaml +105 -0
  21. package/deployments/default.testnet-plan.yaml +67 -0
  22. package/deployments/new-contracts.testnet-plan.yaml +32 -0
  23. package/frontend/README.md +10 -0
  24. package/frontend/components.json +22 -0
  25. package/frontend/dist/assets/index-BacuuL66.css +1 -0
  26. package/frontend/dist/assets/index-jryypd5B.js +194 -0
  27. package/frontend/dist/favicon.png +0 -0
  28. package/frontend/dist/index.html +15 -0
  29. package/frontend/dist/manifest.json +15 -0
  30. package/frontend/dist/vite.svg +1 -0
  31. package/frontend/empty-mock.js +1 -0
  32. package/frontend/eslint.config.js +23 -0
  33. package/frontend/eslint.config.mjs +25 -0
  34. package/frontend/index.html +14 -0
  35. package/frontend/next.config.ts +17 -0
  36. package/frontend/package-lock.json +14740 -0
  37. package/frontend/package.json +56 -0
  38. package/frontend/postcss.config.js +5 -0
  39. package/frontend/postcss.config.mjs +5 -0
  40. package/frontend/public/favicon.png +0 -0
  41. package/frontend/public/file.svg +1 -0
  42. package/frontend/public/globe.svg +1 -0
  43. package/frontend/public/manifest.json +15 -0
  44. package/frontend/public/next.svg +1 -0
  45. package/frontend/public/vercel.svg +1 -0
  46. package/frontend/public/vite.svg +1 -0
  47. package/frontend/public/window.svg +1 -0
  48. package/frontend/src/App.css +42 -0
  49. package/frontend/src/App.tsx +177 -0
  50. package/frontend/src/app/about/page.tsx +208 -0
  51. package/frontend/src/app/favicon.ico +0 -0
  52. package/frontend/src/app/globals.css +129 -0
  53. package/frontend/src/app/help/page.tsx +167 -0
  54. package/frontend/src/app/how-it-works/page.tsx +274 -0
  55. package/frontend/src/app/layout.tsx +55 -0
  56. package/frontend/src/app/marketplace/page.tsx +324 -0
  57. package/frontend/src/app/my-listings/page.tsx +318 -0
  58. package/frontend/src/app/page.tsx +15 -0
  59. package/frontend/src/assets/react.svg +1 -0
  60. package/frontend/src/components/ConfirmDialog.tsx +54 -0
  61. package/frontend/src/components/CreateListingForm.tsx +231 -0
  62. package/frontend/src/components/ErrorBoundary.tsx +73 -0
  63. package/frontend/src/components/FilterPanel.tsx +10 -0
  64. package/frontend/src/components/Footer.tsx +100 -0
  65. package/frontend/src/components/Header.tsx +268 -0
  66. package/frontend/src/components/ImageUpload.tsx +147 -0
  67. package/frontend/src/components/LandingPage.tsx +322 -0
  68. package/frontend/src/components/ListingCard.tsx +154 -0
  69. package/frontend/src/components/LoadingSkeleton.tsx +44 -0
  70. package/frontend/src/components/MobileNav.tsx +89 -0
  71. package/frontend/src/components/NotificationBell.tsx +8 -0
  72. package/frontend/src/components/NotificationPanel.tsx +14 -0
  73. package/frontend/src/components/README.md +14 -0
  74. package/frontend/src/components/SearchBar.tsx +10 -0
  75. package/frontend/src/components/TestnetBanner.tsx +29 -0
  76. package/frontend/src/components/ThemeToggle.tsx +32 -0
  77. package/frontend/src/components/__tests__/Header.test.tsx +70 -0
  78. package/frontend/src/components/__tests__/ListingCard.test.tsx +86 -0
  79. package/frontend/src/components/providers/ThemeProvider.tsx +9 -0
  80. package/frontend/src/components/ui/alert-dialog.tsx +141 -0
  81. package/frontend/src/components/ui/avatar.tsx +53 -0
  82. package/frontend/src/components/ui/badge.tsx +46 -0
  83. package/frontend/src/components/ui/button.tsx +60 -0
  84. package/frontend/src/components/ui/card.tsx +92 -0
  85. package/frontend/src/components/ui/dialog.tsx +143 -0
  86. package/frontend/src/components/ui/dropdown-menu.tsx +257 -0
  87. package/frontend/src/components/ui/input.tsx +21 -0
  88. package/frontend/src/components/ui/label.tsx +24 -0
  89. package/frontend/src/components/ui/select.tsx +187 -0
  90. package/frontend/src/components/ui/sonner.tsx +40 -0
  91. package/frontend/src/components/ui/textarea.tsx +18 -0
  92. package/frontend/src/context/README.md +27 -0
  93. package/frontend/src/index.css +166 -0
  94. package/frontend/src/lib/notificationEvents.ts +10 -0
  95. package/frontend/src/lib/notificationStore.ts +13 -0
  96. package/frontend/src/lib/notifications.ts +13 -0
  97. package/frontend/src/lib/search.ts +28 -0
  98. package/frontend/src/lib/stacks.ts +189 -0
  99. package/frontend/src/lib/utils.ts +6 -0
  100. package/frontend/src/main.tsx +10 -0
  101. package/frontend/src/test/setup.ts +23 -0
  102. package/frontend/src/types.d.ts +9 -0
  103. package/frontend/tsconfig.app.json +28 -0
  104. package/frontend/tsconfig.json +41 -0
  105. package/frontend/tsconfig.node.json +26 -0
  106. package/frontend/vercel.json +4 -0
  107. package/frontend/vite.config.ts +6 -0
  108. package/frontend/vitest.config.ts +17 -0
  109. package/lcov.info +31338 -0
  110. package/mainnetcontracts.md +16 -0
  111. package/package.json +53 -0
  112. package/scripts/auto-activity.sh +9 -0
  113. package/scripts/cancel-pending.ts +67 -0
  114. package/scripts/check-balances.ts +23 -0
  115. package/scripts/distribute-evenly.ts +56 -0
  116. package/scripts/drain-accounts.ts +70 -0
  117. package/scripts/fund-accounts.ts +88 -0
  118. package/scripts/fund-active.ts +59 -0
  119. package/scripts/fund-unfunded.ts +88 -0
  120. package/scripts/generate-activity.ts +181 -0
  121. package/scripts/git-activity-generator.ts +154 -0
  122. package/scripts/mobile-server.ts +123 -0
  123. package/settings/Devnet.toml +155 -0
  124. package/settings/Mainnet.toml +7 -0
  125. package/settings/Testnet.toml +9 -0
  126. package/tests/CoreMarketPlace.fuzz.test.ts +435 -0
  127. package/tests/CoreMarketPlace.test.ts +564 -0
  128. package/tsconfig.json +26 -0
  129. package/vitest.config.js +49 -0
package/README.md ADDED
@@ -0,0 +1,344 @@
1
+ # Strade - Decentralized Marketplace on Stacks
2
+
3
+ [![Stacks](https://img.shields.io/badge/Stacks-Blockchain-5546FF?style=flat&logo=stacks)](https://www.stacks.co/)
4
+ [![Clarity](https://img.shields.io/badge/Clarity-4.0-blue?style=flat)](https://clarity-lang.org/)
5
+ [![Next.js](https://img.shields.io/badge/Next.js-15-black?style=flat&logo=next.js)](https://nextjs.org/)
6
+ [![License](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
7
+ [![Testnet](https://img.shields.io/badge/Testnet-Live-success)](https://explorer.hiro.so/address/STGEE2D7NV4RJC1MHK59AN83PEN0CBBEXNG4QQVF?chain=testnet)
8
+
9
+ A secure, decentralized marketplace built on the Stacks blockchain, featuring smart contract-powered escrow, dispute resolution, and user reputation systems.
10
+
11
+ ## Features
12
+
13
+ ### Core Marketplace
14
+ - **Create Listings**: List items for sale with customizable pricing and duration
15
+ - **Browse & Search**: Discover active listings with real-time updates
16
+ - **Direct Purchases**: Secure STX-based transactions with immediate confirmation
17
+ - **Listing Management**: Update, cancel, or modify your listings anytime
18
+
19
+ ### Escrow Service
20
+ - **Trustless Transactions**: Automated escrow holds funds until conditions are met
21
+ - **7-Day Protection**: Standard escrow duration with automatic expiry handling
22
+ - **Flexible Release**: Buyer-initiated or dispute-based fund releases
23
+ - **Refund Mechanism**: Built-in refund system for failed transactions
24
+
25
+ ### Dispute Resolution
26
+ - **Fair Arbitration**: Community-based voting system for dispute resolution
27
+ - **24-Hour Voting Period**: Efficient dispute resolution process
28
+ - **Multi-Arbitrator Support**: Minimum 3 votes required for decisions
29
+ - **Transparent Process**: All dispute actions recorded on-chain
30
+
31
+ ### User Profiles & Reputation
32
+ - **User Registration**: Create profiles with username, bio, and email
33
+ - **Rating System**: 5-star rating system for buyers and sellers
34
+ - **Reputation Score**: Calculated scores based on ratings and activity
35
+ - **Authorization System**: Contract-based user permissions
36
+
37
+ ### BST Token (Strade Token)
38
+ - **SIP-010 Compliant**: Standard fungible token implementation
39
+ - **1 Trillion Supply**: Initial supply minted to deployer
40
+ - **6 Decimals**: Precision for micro-transactions
41
+ - **Pausable Contract**: Emergency pause functionality for security
42
+
43
+ ## Architecture
44
+
45
+ ### Smart Contracts (Clarity 4)
46
+
47
+ ```
48
+ contracts/
49
+ ├── CoreMarketPlace.clar # Main marketplace logic
50
+ ├── EscrowService.clar # Escrow management
51
+ ├── DisputeResolution_clar.clar # Arbitration system
52
+ ├── UserProfile.clar # User management
53
+ └── token.clar # BST token (SIP-010)
54
+ ```
55
+
56
+ ### Frontend (Next.js 15)
57
+
58
+ ```
59
+ frontend/
60
+ ├── src/
61
+ │ ├── app/ # Next.js app router pages
62
+ │ ├── components/ # React components
63
+ │ ├── lib/ # Stacks integration & utilities
64
+ │ └── test/ # Unit tests
65
+ └── public/ # Static assets
66
+ ```
67
+
68
+ ## Getting Started
69
+
70
+ ### Prerequisites
71
+
72
+ - Node.js 18+ and npm
73
+ - [Clarinet](https://github.com/hirosystems/clarinet) for smart contract development
74
+ - [Leather Wallet](https://leather.io/) for testnet interactions
75
+
76
+ ### Installation
77
+
78
+ 1. **Clone the repository**
79
+ ```bash
80
+ git clone https://github.com/yourusername/strade.git
81
+ cd strade
82
+ ```
83
+
84
+ 2. **Install dependencies**
85
+ ```bash
86
+ # Install Clarinet (if not already installed)
87
+ curl -L https://github.com/hirosystems/clarinet/releases/download/latest/clarinet-linux-x64.tar.gz | tar xz
88
+
89
+ # Install frontend dependencies
90
+ cd frontend
91
+ npm install
92
+ ```
93
+
94
+ 3. **Configure environment**
95
+ ```bash
96
+ # Frontend configuration is in src/lib/stacks.ts
97
+ # Contract addresses are already configured for testnet
98
+ ```
99
+
100
+ ### Running Locally
101
+
102
+ #### Smart Contracts
103
+
104
+ ```bash
105
+ # Check contract syntax
106
+ clarinet check
107
+
108
+ # Run contract tests
109
+ clarinet test
110
+
111
+ # Start local console
112
+ clarinet console
113
+ ```
114
+
115
+ #### Frontend
116
+
117
+ ```bash
118
+ cd frontend
119
+
120
+ # Development server
121
+ npm run dev
122
+
123
+ # Build for production
124
+ npm run build
125
+
126
+ # Start production server
127
+ npm start
128
+
129
+ # Run tests
130
+ npm test
131
+ ```
132
+
133
+ Visit `http://localhost:3000` to see the application.
134
+
135
+ ## Testnet Deployment
136
+
137
+ All contracts are deployed and verified on Stacks Testnet:
138
+
139
+ | Contract | Address |
140
+ |----------|---------|
141
+ | CoreMarketPlace | `STGEE2D7NV4RJC1MHK59AN83PEN0CBBEXNG4QQVF.CoreMarketPlace` |
142
+ | EscrowService | `STGEE2D7NV4RJC1MHK59AN83PEN0CBBEXNG4QQVF.EscrowService` |
143
+ | UserProfile | `STGEE2D7NV4RJC1MHK59AN83PEN0CBBEXNG4QQVF.UserProfile` |
144
+ | DisputeResolution | `STGEE2D7NV4RJC1MHK59AN83PEN0CBBEXNG4QQVF.DisputeResolution_clar` |
145
+ | BST Token | `STGEE2D7NV4RJC1MHK59AN83PEN0CBBEXNG4QQVF.token` |
146
+
147
+ **Deployer Address**: [STGEE2D7NV4RJC1MHK59AN83PEN0CBBEXNG4QQVF](https://explorer.hiro.so/address/STGEE2D7NV4RJC1MHK59AN83PEN0CBBEXNG4QQVF?chain=testnet)
148
+
149
+ See [DEPLOYMENT_RESULTS.md](DEPLOYMENT_RESULTS.md) for detailed deployment information.
150
+
151
+ ## Testing
152
+
153
+ ### Smart Contract Tests
154
+
155
+ ```bash
156
+ # Run all contract tests
157
+ npm test
158
+
159
+ # Run with coverage
160
+ clarinet test --coverage
161
+
162
+ # Run specific test file
163
+ npm test -- tests/CoreMarketPlace.test.ts
164
+ ```
165
+
166
+ ### Frontend Tests
167
+
168
+ ```bash
169
+ cd frontend
170
+
171
+ # Run unit tests
172
+ npm test
173
+
174
+ # Run with coverage
175
+ npm test -- --coverage
176
+
177
+ # Run in watch mode
178
+ npm test -- --watch
179
+ ```
180
+
181
+ ### Test Coverage
182
+
183
+ - ✅ CoreMarketPlace: 20 tests
184
+ - ✅ Fuzz Testing: 13 tests
185
+ - ✅ All contracts verified on testnet
186
+
187
+ ## Security Features
188
+
189
+ - **Input Validation**: All user inputs are validated on-chain
190
+ - **Access Control**: Function-level permissions and ownership checks
191
+ - **Escrow Protection**: Automatic fund locking and secure release mechanisms
192
+ - **Dispute Resolution**: Multi-party arbitration prevents unilateral decisions
193
+ - **Emergency Pause**: Contract owner can pause operations if needed
194
+ - **Expiration Handling**: Automatic expiry for listings and escrow
195
+
196
+ ## Documentation
197
+
198
+ ### For Users
199
+ - [User Guide](docs/USER_GUIDE.md) - How to use Strade marketplace
200
+ - [FAQ](docs/FAQ.md) - Frequently asked questions
201
+
202
+ ### For Developers
203
+ - [Smart Contract API](docs/CONTRACT_API.md) - Contract functions and parameters
204
+ - [Frontend Integration](docs/INTEGRATION.md) - How to integrate with Strade
205
+ - [Development Guide](docs/DEVELOPMENT.md) - Contributing guidelines
206
+
207
+ ## Roadmap
208
+
209
+ ### Phase 1: Core Functionality
210
+ - [x] Smart contract development
211
+ - [x] Basic marketplace features
212
+ - [x] Escrow system
213
+ - [x] User profiles
214
+ - [x] Testnet deployment
215
+
216
+ ### Phase 2: Enhanced Features
217
+ - [ ] Advanced search and filters
218
+ - [ ] Image upload and IPFS integration
219
+ - [ ] Mobile-responsive design improvements
220
+ - [ ] Real-time notifications
221
+ - [ ] Multi-currency support
222
+
223
+ ### Phase 3: Scaling & Security
224
+ - [ ] Professional security audit
225
+ - [ ] Mainnet deployment
226
+ - [ ] Performance optimization
227
+ - [ ] Analytics dashboard
228
+ - [ ] Community governance
229
+
230
+ ### Phase 4: Ecosystem Growth
231
+ - [ ] Mobile app (iOS/Android)
232
+ - [ ] API for third-party integration
233
+ - [ ] Multi-chain support
234
+ - [ ] NFT marketplace integration
235
+ - [ ] Advanced reputation algorithms
236
+
237
+ ## Contributing
238
+
239
+ We welcome contributions! Please follow these steps:
240
+
241
+ 1. Fork the repository
242
+ 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
243
+ 3. Commit your changes (`git commit -m 'Add amazing feature'`)
244
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
245
+ 5. Open a Pull Request
246
+
247
+ Please ensure:
248
+ - All tests pass (`npm test`)
249
+ - Code follows existing style guidelines
250
+ - Commit messages are clear and descriptive
251
+ - Documentation is updated if needed
252
+
253
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed guidelines.
254
+
255
+ ## Smart Contract Details
256
+
257
+ ### CoreMarketPlace
258
+ - **Purpose**: Main marketplace logic for listing management
259
+ - **Key Functions**: `create-listing`, `update-listing`, `cancel-listing`, `purchase-listing`
260
+ - **Features**: Time-based expiration, status tracking, event logging
261
+
262
+ ### EscrowService
263
+ - **Purpose**: Secure fund holding during transactions
264
+ - **Key Functions**: `create-escrow`, `release-funds`, `refund-buyer`
265
+ - **Duration**: 1,008 blocks (~7 days)
266
+
267
+ ### DisputeResolution
268
+ - **Purpose**: Fair dispute resolution through arbitration
269
+ - **Key Functions**: `raise-dispute`, `vote-on-dispute`, `resolve-dispute`
270
+ - **Voting Period**: 144 blocks (~24 hours)
271
+
272
+ ### UserProfile
273
+ - **Purpose**: User identity and reputation management
274
+ - **Key Functions**: `register-user`, `update-profile`, `rate-user`, `calculate-reputation`
275
+ - **Features**: 5-star rating system, reputation scores
276
+
277
+ ### BST Token
278
+ - **Standard**: SIP-010 Fungible Token
279
+ - **Functions**: `transfer`, `mint`, `burn`, `get-balance`
280
+ - **Supply**: 1,000,000,000,000 BST (1 trillion)
281
+
282
+ ## Technology Stack
283
+
284
+ ### Blockchain
285
+ - **Stacks Blockchain**: Layer-1 blockchain for Bitcoin
286
+ - **Clarity 4**: Smart contract language with predictable behavior
287
+ - **PoX Consensus**: Proof of Transfer for Bitcoin finality
288
+
289
+ ### Frontend
290
+ - **Next.js 15**: React framework with App Router
291
+ - **TypeScript**: Type-safe development
292
+ - **Tailwind CSS**: Utility-first CSS framework
293
+ - **shadcn/ui**: Beautiful component library
294
+ - **Stacks.js**: Official Stacks JavaScript libraries
295
+
296
+ ### Development Tools
297
+ - **Clarinet**: Local development and testing environment
298
+ - **Vitest**: Fast unit testing framework
299
+ - **ESLint**: Code linting and quality
300
+ - **Prettier**: Code formatting
301
+
302
+ ## Project Statistics
303
+
304
+ - **Smart Contracts**: 5 deployed contracts
305
+ - **Total Lines of Code**: ~1,500 Clarity + ~2,000 TypeScript
306
+ - **Test Coverage**: 33 tests passing
307
+ - **Deployment Cost**: 0.427 STX
308
+ - **Clarity Version**: 4.0 (Epoch 3.0)
309
+
310
+ ## License
311
+
312
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
313
+
314
+ ## Acknowledgments
315
+
316
+ - **Stacks Foundation** - For the amazing blockchain infrastructure
317
+ - **Hiro Systems** - For development tools and APIs
318
+ - **Clarity Language** - For secure smart contract development
319
+ - **Open Source Community** - For inspiration and support
320
+
321
+ ## Support & Community
322
+
323
+ - **Website**: [strade.io](https://strade.io) (Coming Soon)
324
+ - **Documentation**: [docs.strade.io](https://docs.strade.io) (Coming Soon)
325
+ - **Discord**: [Join our community](https://discord.gg/strade) (Coming Soon)
326
+ - **Twitter**: [@StradeMarket](https://twitter.com/StradeMarket) (Coming Soon)
327
+ - **Email**: support@strade.io
328
+
329
+ ## Disclaimer
330
+
331
+ **TESTNET ONLY**: This project is currently deployed on Stacks Testnet. DO NOT use real assets or consider any transactions as having real-world value. The testnet can be reset at any time, and all data may be lost.
332
+
333
+ Before mainnet deployment, a comprehensive security audit will be conducted. Use at your own risk.
334
+
335
+ ## Star History
336
+
337
+ If you find this project useful, please consider giving it a star ⭐
338
+
339
+ ---
340
+
341
+ **Built on the Stacks Blockchain**
342
+
343
+ *Empowering decentralized commerce, one transaction at a time.*
344
+
package/TODO.md ADDED
@@ -0,0 +1,34 @@
1
+ # TODO.md - Git Activity Generator: Run 2000 More Commits
2
+
3
+ ## Plan Summary
4
+ **Information Gathered:**
5
+ - `scripts/git-activity-generator.ts`: Generates ~2000 git commits (7 PRs × 286 commits) via temp-commits/counter26XX.txt files (updates content, commits "chore: bump counter X"). Offset 2602, total files seen up to ~2753, .activity_counter=300 (likely tracks something else or reset).
6
+ - Existing temp-commits/ has many files; will clean to avoid conflicts.
7
+ - Uses `gh pr create` (assumes GitHub CLI auth'd).
8
+ - `generate-activity.ts`/`auto-activity.sh`: On-chain tx generator, not git-related.
9
+
10
+ **Plan:**
11
+ 1. Edit `scripts/git-activity-generator.ts`:
12
+ - Clean temp-commits/ fully on start.
13
+ - Set new offset = 2754 (next after visible files).
14
+ - Keep NUM_PRS=7, COMMITS_PER_PR=286 (~2000 total).
15
+ - Add progress logging, optional --count CLI arg for flexibility.
16
+ - Ensure safe git ops (stash if dirty, checkout main at end).
17
+ 2. No dependent files (self-contained).
18
+
19
+ **Dependent Files to be edited:** None.
20
+
21
+ **Followup steps:**
22
+ 1. Run dry-run: `npx tsx scripts/git-activity-generator.ts --dry-run`
23
+ 2. Ensure `gh auth status` (install/auth if needed).
24
+ 3. Execute: `npx tsx scripts/git-activity-generator.ts`
25
+ 4. Verify: `gh pr list`, `git log --oneline -10`
26
+ 5. Optional cleanup: rm -rf temp-commits/, git branch -D fake-activity-pr-*
27
+
28
+ ✅ Plan approved by user.
29
+
30
+ ## Progress
31
+ - [x] Create TODO.md
32
+ - [x] Step 1: Edit git-activity-generator.ts ✅ (offset 2754, --total CLI, progress, safety stashes, counter update)
33
+ - [ ] Step 2: Test dry-run
34
+ - [ ] Step 3: Run production
@@ -0,0 +1,227 @@
1
+ ;; CoreMarketPlace Contract
2
+ ;; This contract manages the creation, updating, and purchasing of listings in the Strade decentralized marketplace.
3
+ ;; It handles the core logic for marketplace interactions, including listing management and purchase fulfillment.
4
+
5
+ ;; --- Constants ---
6
+ ;; Defines immutable values used throughout the contract for error handling and configuration.
7
+
8
+ (define-constant CONTRACT_OWNER tx-sender) ;; Sets the contract deployer as the owner.
9
+ (define-constant ERR_NOT_AUTHORIZED (err u100)) ;; Error for unauthorized actions.
10
+ (define-constant ERR_LISTING_NOT_FOUND (err u101)) ;; Error when a listing cannot be found.
11
+ (define-constant ERR_INVALID_PRICE (err u102)) ;; Error for invalid listing prices (e.g., zero or negative).
12
+ (define-constant ERR_INVALID_SELLER (err u103)) ;; Error for invalid seller principals.
13
+ (define-constant ERR_INSUFFICIENT_BALANCE (err u104)) ;; Error when a buyer has insufficient funds.
14
+ (define-constant ERR_LISTING_EXPIRED (err u105)) ;; Error for expired listings.
15
+ (define-constant ERR_INVALID_STATUS (err u106)) ;; Error for invalid listing statuses.
16
+ (define-constant ERR_NOT_SELLER (err u107)) ;; Error when a user is not the seller of a listing.
17
+ (define-constant ERR_ALREADY_PURCHASED (err u108)) ;; Error for listings that have already been sold.
18
+ (define-constant ERR_INVALID_INPUT (err u109)) ;; Error for invalid input parameters.
19
+ (define-constant ERR_INVALID_DURATION (err u110)) ;; Error for invalid listing durations.
20
+ (define-constant ERR_INVALID_LISTING_ID (err u111)) ;; Error for invalid listing IDs.
21
+ (define-constant MAX_LISTING_DURATION u52560) ;; Maximum duration of a listing (approximately 1 year).
22
+
23
+ ;; --- Data Maps ---
24
+ ;; Defines the data structures used to store marketplace information.
25
+
26
+ (define-map Listings
27
+ { listing-id: uint }
28
+ {
29
+ seller: principal, ;; The principal of the seller.
30
+ name: (string-utf8 64), ;; The name of the listing.
31
+ description: (string-utf8 256), ;; A description of the item.
32
+ price: uint, ;; The price of the listing in micro-STX.
33
+ status: (string-ascii 20), ;; The current status of the listing (e.g., "active", "sold", "cancelled").
34
+ created-at: uint, ;; The block height at which the listing was created.
35
+ expires-at: uint ;; The block height at which the listing expires.
36
+ }
37
+ )
38
+
39
+ ;; --- Variables ---
40
+ ;; Defines mutable variables for tracking the contract's state.
41
+
42
+ (define-data-var last-listing-id uint u0) ;; Tracks the ID of the last created listing.
43
+
44
+ ;; --- Private Functions ---
45
+ ;; Helper functions intended for internal use by the contract.
46
+
47
+ ;; Checks if a given price is valid (greater than zero).
48
+ (define-private (is-valid-price (price uint))
49
+ (> price u0)
50
+ )
51
+
52
+ ;; Checks if a seller principal is valid (not the sender or the contract itself).
53
+ (define-private (is-valid-seller (seller principal))
54
+ (and
55
+ (not (is-eq seller tx-sender))
56
+ (not (is-eq seller (as-contract tx-sender)))
57
+ )
58
+ )
59
+
60
+ ;; Checks if a string is within the specified length constraints.
61
+ (define-private (is-valid-string (str (string-utf8 256)) (max-len uint))
62
+ (and (>= (len str) u1) (<= (len str) max-len))
63
+ )
64
+
65
+ ;; Checks if a listing duration is valid.
66
+ (define-private (is-valid-duration (duration uint))
67
+ (and (> duration u0) (<= duration MAX_LISTING_DURATION))
68
+ )
69
+
70
+ ;; Checks if a listing ID is valid.
71
+ (define-private (is-valid-listing-id (id uint))
72
+ (<= id (var-get last-listing-id))
73
+ )
74
+
75
+ ;; Increments and returns the next listing ID.
76
+ (define-private (increment-listing-id)
77
+ (let
78
+ (
79
+ (current-id (var-get last-listing-id))
80
+ )
81
+ (var-set last-listing-id (+ current-id u1))
82
+ (var-get last-listing-id)
83
+ )
84
+ )
85
+
86
+ ;; --- Public Functions ---
87
+ ;; Functions that can be called by any user.
88
+
89
+ ;; Creates a new listing in the marketplace.
90
+ ;; @param name: The name of the listing.
91
+ ;; @param description: A description of the item.
92
+ ;; @param price: The price of the listing in micro-STX.
93
+ ;; @param duration: The duration of the listing in blocks.
94
+ ;; @returns (ok uint): The ID of the newly created listing.
95
+ (define-public (create-listing (name (string-utf8 64)) (description (string-utf8 256)) (price uint) (duration uint))
96
+ (let
97
+ (
98
+ (listing-id (increment-listing-id))
99
+ (expires-at (+ stacks-block-height duration))
100
+ )
101
+ (asserts! (is-valid-price price) (err ERR_INVALID_PRICE))
102
+ (asserts! (is-valid-string name u64) (err ERR_INVALID_INPUT))
103
+ (asserts! (is-valid-string description u256) (err ERR_INVALID_INPUT))
104
+ (asserts! (is-valid-duration duration) (err ERR_INVALID_DURATION))
105
+ (map-set Listings
106
+ { listing-id: listing-id }
107
+ {
108
+ seller: tx-sender,
109
+ name: name,
110
+ description: description,
111
+ price: price,
112
+ status: "active",
113
+ created-at: stacks-block-height,
114
+ expires-at: expires-at
115
+ }
116
+ )
117
+ (print { event: "listing_created", listing-id: listing-id, seller: tx-sender })
118
+ (ok listing-id)
119
+ )
120
+ )
121
+
122
+ ;; Updates an existing listing.
123
+ ;; @param listing-id: The ID of the listing to update.
124
+ ;; @param new-price: The new price for the listing.
125
+ ;; @param new-description: The new description for the listing.
126
+ ;; @returns (ok bool): True if the update is successful.
127
+ (define-public (update-listing (listing-id uint) (new-price uint) (new-description (string-utf8 256)))
128
+ (begin
129
+ (asserts! (is-valid-listing-id listing-id) (err ERR_INVALID_LISTING_ID))
130
+ (let
131
+ (
132
+ (listing (unwrap! (map-get? Listings { listing-id: listing-id }) (err ERR_LISTING_NOT_FOUND)))
133
+ )
134
+ (asserts! (is-eq (get seller listing) tx-sender) (err ERR_NOT_SELLER))
135
+ (asserts! (is-eq (get status listing) "active") (err ERR_INVALID_STATUS))
136
+ (asserts! (is-valid-price new-price) (err ERR_INVALID_PRICE))
137
+ (asserts! (is-valid-string new-description u256) (err ERR_INVALID_INPUT))
138
+ (map-set Listings
139
+ { listing-id: listing-id }
140
+ (merge listing
141
+ {
142
+ price: new-price,
143
+ description: new-description
144
+ }
145
+ )
146
+ )
147
+ (print { event: "listing_updated", listing-id: listing-id, seller: tx-sender })
148
+ (ok true)
149
+ )
150
+ )
151
+ )
152
+
153
+ ;; Cancels a listing.
154
+ ;; @param listing-id: The ID of the listing to cancel.
155
+ ;; @returns (ok bool): True if the cancellation is successful.
156
+ (define-public (cancel-listing (listing-id uint))
157
+ (begin
158
+ (asserts! (is-valid-listing-id listing-id) (err ERR_INVALID_LISTING_ID))
159
+ (let
160
+ (
161
+ (listing (unwrap! (map-get? Listings { listing-id: listing-id }) (err ERR_LISTING_NOT_FOUND)))
162
+ )
163
+ (asserts! (is-eq (get seller listing) tx-sender) (err ERR_NOT_SELLER))
164
+ (asserts! (is-eq (get status listing) "active") (err ERR_INVALID_STATUS))
165
+ (map-set Listings
166
+ { listing-id: listing-id }
167
+ (merge listing { status: "cancelled" })
168
+ )
169
+ (print { event: "listing_cancelled", listing-id: listing-id, seller: tx-sender })
170
+ (ok true)
171
+ )
172
+ )
173
+ )
174
+
175
+ ;; Purchases a listing.
176
+ ;; @param listing-id: The ID of the listing to purchase.
177
+ ;; @returns (ok bool): True if the purchase is successful.
178
+ (define-public (purchase-listing (listing-id uint))
179
+ (begin
180
+ (asserts! (is-valid-listing-id listing-id) (err ERR_INVALID_LISTING_ID))
181
+ (let
182
+ (
183
+ (listing (unwrap! (map-get? Listings { listing-id: listing-id }) (err ERR_LISTING_NOT_FOUND)))
184
+ (price (get price listing))
185
+ (seller (get seller listing))
186
+ )
187
+ (asserts! (is-eq (get status listing) "active") (err ERR_INVALID_STATUS))
188
+ (asserts! (<= stacks-block-height (get expires-at listing)) (err ERR_LISTING_EXPIRED))
189
+ (asserts! (is-valid-seller seller) (err ERR_INVALID_SELLER))
190
+ (match (stx-transfer? price tx-sender seller)
191
+ success (begin
192
+ (map-set Listings
193
+ { listing-id: listing-id }
194
+ (merge listing { status: "sold" })
195
+ )
196
+ (print { event: "listing_purchased", listing-id: listing-id, buyer: tx-sender, seller: seller, price: price })
197
+ (ok true))
198
+ error (err ERR_INSUFFICIENT_BALANCE))
199
+ )
200
+ )
201
+ )
202
+
203
+ ;; --- Read-Only Functions ---
204
+ ;; Functions for retrieving data from the contract without making state changes.
205
+
206
+ ;; Retrieves a listing by its ID.
207
+ ;; @param listing-id: The ID of the listing to retrieve.
208
+ ;; @returns (optional {<listing-data>}): The listing data or none if not found.
209
+ (define-read-only (get-listing (listing-id uint))
210
+ (if (is-valid-listing-id listing-id)
211
+ (map-get? Listings { listing-id: listing-id })
212
+ none
213
+ )
214
+ )
215
+
216
+ ;; Retrieves the ID of the last created listing.
217
+ ;; @returns (ok uint): The last listing ID.
218
+ (define-read-only (get-last-listing-id)
219
+ (ok (var-get last-listing-id))
220
+ )
221
+
222
+ ;; --- Contract Initialization ---
223
+ ;; Initializes the contract upon deployment.
224
+ (begin
225
+ (print "CoreMarketPlace contract initialized")
226
+ (ok true)
227
+ )