forked from Epondia/starked-education
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarketplace.rs
More file actions
348 lines (289 loc) · 10.2 KB
/
Copy pathmarketplace.rs
File metadata and controls
348 lines (289 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#![no_std]
use crate::utils::storage::{PackedTimestamps, StorageKey, StorageUtils};
use soroban_sdk::{
contract, contractimpl, contracttype, symbol_short, Address, Env, String, Vec, U256,
};
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum MarketplaceKey {
Listing(u64),
Rental(u64, Address),
Stake(u64, Address),
Dispute(u64),
MarketplaceCount,
ListingCount,
DisputeCount,
TradeCount(u64), // Trade count per credential for bonding curve
}
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Listing {
pub credential_id: u64,
pub seller: Address,
pub price: u64,
pub royalty_bps: u32, // Basis points (100 = 1%)
pub active: bool,
}
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Rental {
pub credential_id: u64,
pub tenant: Address,
pub expiry: u64,
pub price: u64,
}
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Stake {
pub credential_id: u64,
pub staker: Address,
pub amount: u64,
pub start_time: u64,
}
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Dispute {
pub id: u64,
pub listing_id: u64,
pub buyer: Address,
pub reason: String,
pub status: u32, // 0: Open, 1: Resolved, 2: Cancelled
}
#[contract]
pub struct MarketplaceContract;
#[contractimpl]
impl MarketplaceContract {
/// Initialize the marketplace
pub fn initialize(env: Env, admin: Address) {
if env.storage().instance().has(&StorageKey::Admin) {
panic!("Already initialized");
}
env.storage().instance().set(&StorageKey::Admin, &admin);
env.storage()
.instance()
.set(&MarketplaceKey::ListingCount, &0u64);
env.storage()
.instance()
.set(&MarketplaceKey::DisputeCount, &0u64);
}
/// List a credential for sale with royalties
pub fn list_credential(
env: Env,
seller: Address,
credential_id: u64,
price: u64,
royalty_bps: u32,
) -> u64 {
seller.require_auth();
// Ensure royalty is reasonable (max 30%)
if royalty_bps > 3000 {
panic!("Royalty too high");
}
let listing_id = env
.storage()
.instance()
.get(&MarketplaceKey::ListingCount)
.unwrap_or(0u64)
+ 1;
let listing = Listing {
credential_id,
seller: seller.clone(),
price,
royalty_bps,
active: true,
};
env.storage()
.instance()
.set(&MarketplaceKey::Listing(listing_id), &listing);
env.storage()
.instance()
.set(&MarketplaceKey::ListingCount, &listing_id);
env.events().publish(
(symbol_short!("marketplace"), symbol_short!("created")),
(listing_id, credential_id, seller, price),
);
listing_id
}
/// Purchase a listed credential
pub fn purchase_credential(env: Env, buyer: Address, listing_id: u64) {
buyer.require_auth();
let mut listing: Listing = env
.storage()
.instance()
.get(&MarketplaceKey::Listing(listing_id))
.unwrap_or_else(|| panic!("Listing not found"));
if !listing.active {
panic!("Listing is inactive");
}
// Logic for transferring tokens should go here (using a token contract)
// For this implementation, we focus on state changes and royalty math
let royalty_amount = (listing.price as u128 * listing.royalty_bps as u128 / 10000) as u64;
let seller_amount = listing.price - royalty_amount;
// Mark listing as sold
listing.active = false;
env.storage()
.instance()
.set(&MarketplaceKey::Listing(listing_id), &listing);
// Update trade count for bonding curve price discovery
let trade_count: u64 = env
.storage()
.instance()
.get(&MarketplaceKey::TradeCount(listing.credential_id))
.unwrap_or(0);
env.storage().instance().set(
&MarketplaceKey::TradeCount(listing.credential_id),
&(trade_count + 1),
);
env.events().publish(
(symbol_short!("marketplace"), symbol_short!("sale_completed")),
(listing_id, buyer, seller_amount, royalty_amount),
);
}
/// Licensing: Rent a credential for a specific duration
pub fn rent_credential(env: Env, tenant: Address, credential_id: u64, duration: u64) {
tenant.require_auth();
let price = Self::calculate_bonding_price(env.clone(), credential_id);
let expiry = env.ledger().timestamp() + duration;
let rental = Rental {
credential_id,
tenant: tenant.clone(),
expiry,
price,
};
env.storage().instance().set(
&MarketplaceKey::Rental(credential_id, tenant.clone()),
&rental,
);
env.events().publish(
(symbol_short!("marketplace"), symbol_short!("rented")),
(credential_id, tenant, expiry, price),
);
}
/// Bonding curve logic for price discovery
/// Price = BasePrice + (Slope * Trades^2)
pub fn calculate_bonding_price(env: Env, credential_id: u64) -> u64 {
let base_price = 100u64; // Minimum price
let slope = 10u64;
let trades: u64 = env
.storage()
.instance()
.get(&MarketplaceKey::TradeCount(credential_id))
.unwrap_or(0);
base_price + slope * trades * trades
}
/// Staking: Stake a credential for verification rewards
pub fn stake_credential(env: Env, staker: Address, credential_id: u64, amount: u64) {
staker.require_auth();
let stake = Stake {
credential_id,
staker: staker.clone(),
amount,
start_time: env.ledger().timestamp(),
};
env.storage().instance().set(
&MarketplaceKey::Stake(credential_id, staker.clone()),
&stake,
);
env.events().publish(
(symbol_short!("marketplace"), symbol_short!("staked")),
(credential_id, staker, amount),
);
}
/// Claim staking rewards based on reputation
pub fn claim_rewards(env: Env, staker: Address, credential_id: u64) -> u64 {
staker.require_auth();
let stake: Stake = env
.storage()
.instance()
.get(&MarketplaceKey::Stake(credential_id, staker.clone()))
.unwrap_or_else(|| panic!("No stake found"));
let now = env.ledger().timestamp();
let duration = now - stake.start_time;
// Reward = Amount * Duration * RewardRate
// Basic reward rate: 1% per day (86400 seconds)
let base_reward = (stake.amount as u128 * duration as u128 / 8640000) as u64;
// Reputation bonus (hypothetical integration)
// In a real system, we'd call the UserProfileContract
let reputation_bonus = 100; // placeholder for +10% bonus
let total_reward = base_reward + (base_reward * reputation_bonus / 1000);
// Reset stake time
let mut new_stake = stake;
new_stake.start_time = now;
env.storage().instance().set(
&MarketplaceKey::Stake(credential_id, staker.clone()),
&new_stake,
);
env.events().publish(
(symbol_short!("marketplace"), symbol_short!("rewards_claimed")),
(staker, total_reward),
);
total_reward
}
/// Automated Dispute Resolution: Open a dispute
pub fn open_dispute(env: Env, buyer: Address, listing_id: u64, reason: String) -> u64 {
buyer.require_auth();
let dispute_id = env
.storage()
.instance()
.get(&MarketplaceKey::DisputeCount)
.unwrap_or(0u64)
+ 1;
let dispute = Dispute {
id: dispute_id,
listing_id,
buyer: buyer.clone(),
reason,
status: 0, // Open
};
env.storage()
.instance()
.set(&MarketplaceKey::Dispute(dispute_id), &dispute);
env.storage()
.instance()
.set(&MarketplaceKey::DisputeCount, &dispute_id);
env.events().publish(
(symbol_short!("marketplace"), symbol_short!("dispute_opened")),
(dispute_id, listing_id, buyer),
);
dispute_id
}
/// Resolve a dispute (Admin only)
pub fn resolve_dispute(env: Env, admin: Address, dispute_id: u64, resolved: bool) {
admin.require_auth();
let stored_admin: Address = env
.storage()
.instance()
.get(&StorageKey::Admin)
.unwrap_or_else(|| panic!("Admin not set"));
if admin != stored_admin {
panic!("Unauthorized");
}
let mut dispute: Dispute = env
.storage()
.instance()
.get(&MarketplaceKey::Dispute(dispute_id))
.unwrap_or_else(|| panic!("Dispute not found"));
dispute.status = if resolved { 1 } else { 2 };
env.storage()
.instance()
.set(&MarketplaceKey::Dispute(dispute_id), &dispute);
env.events().publish(
(symbol_short!("marketplace"), symbol_short!("dispute_resolved")),
(dispute_id, dispute.status),
);
}
/// Escrow: Initiate a secure transaction with time-lock
pub fn initiate_escrow(env: Env, buyer: Address, listing_id: u64, timeout: u64) -> u64 {
buyer.require_auth();
// Logical escrow ID
let escrow_id = env.storage().instance().get::<_, u64>(&symbol_short!("esc_cnt")).unwrap_or(0) + 1;
env.storage().instance().set(&symbol_short!("esc_cnt"), &escrow_id);
let release_time = env.ledger().timestamp() + timeout;
env.storage().instance().set(&symbol_short!("escrow_t"), &release_time);
env.events().publish(
(symbol_short!("marketplace"), symbol_short!("escrow_initiated")),
(escrow_id, buyer, listing_id, release_time),
);
escrow_id
}
}