-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_marketplace.move
More file actions
230 lines (194 loc) · 6.08 KB
/
data_marketplace.move
File metadata and controls
230 lines (194 loc) · 6.08 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
// Copyright (c), Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
/// Dataset Marketplace Module
/// Manages dataset listings, purchases, and access control
module dataset_marketplace::marketplace;
use enclave::enclave;
use std::string::String;
use sui::coin::{Self, Coin};
use sui::event;
use sui::sui::SUI;
use sui::vec_set::{Self, VecSet};
// ============ Error codes ============
const EInsufficientPayment: u64 = 0;
// const EDatasetNotFound: u64 = 1;
const ENotSeller: u64 = 2;
// const EAlreadyPurchased: u64 = 3;
const EListingInactive: u64 = 4;
// ============ One-Time Witness ============
public struct MARKETPLACE has drop {}
// ============ Init ============
fun init(otw: MARKETPLACE, ctx: &mut TxContext) {
let cap = enclave::new_cap(otw, ctx);
cap.create_enclave_config(
b"dataset marketplace".to_string(),
x"c44ce89e1c054687c4546e1d853e4821afb5ef8761484226c28687aa6f0dc7d2a0d3ec038a24828a62e68b1f3a3e72e6", // pcr0
x"c44ce89e1c054687c4546e1d853e4821afb5ef8761484226c28687aa6f0dc7d2a0d3ec038a24828a62e68b1f3a3e72e6", // pcr1
x"21b9efbc184807662e966d34f390821309eeac6802309798826296bf3e8bec7c10edb30948c90ba67310f7b964fc500a", // pcr2
ctx,
);
transfer::public_transfer(cap, ctx.sender());
// Create and share registry
let registry = DatasetRegistry {
id: object::new(ctx),
listings: vec_set::empty(),
};
transfer::share_object(registry);
}
// ============ Structs ============
/// Registry containing all DatasetListing IDs
public struct DatasetRegistry has key, store {
id: UID,
listings: VecSet<ID>,
}
/// Dataset listing created by seller
public struct DatasetListing has key, store {
id: UID,
seller: address,
price: u64,
blob_id: String, // Walrus blob ID
encrypted_object: String, // Seal encrypted object (hex)
name: String,
description: String,
preview_size: u64, // Bytes available for preview
total_size: u64,
image_url: String, // Preview image url
is_active: bool, // Listing is active or not
mime_type: String, // MIME type: "application/zip", "image/png", "text/csv", etc.
file_name: String, // Original file name for download
content_type: String, // For zip: type of files inside ("image/png", "image/jpeg", etc.)
file_count: u64, // Number of files (for zip archives)
}
/// Purchase receipt - proof that buyer paid for dataset
public struct PurchaseReceipt has key, store {
id: UID,
dataset_id: ID,
buyer: address,
seller: address,
price: u64,
timestamp: u64,
}
// ============ Events ============
public struct DatasetListed has copy, drop {
dataset_id: ID,
seller: address,
price: u64,
name: String,
}
public struct DatasetPurchased has copy, drop {
dataset_id: ID,
buyer: address,
seller: address,
price: u64,
}
public struct DatasetUnlisted has copy, drop {
dataset_id: ID,
seller: address,
}
// ============ Functions ============
/// Create a new dataset listing
public fun list_dataset(
registry: &mut DatasetRegistry,
blob_id: String,
encrypted_object: String,
name: String,
description: String,
price: u64,
preview_size: u64,
total_size: u64,
image_url: String,
mime_type: String,
file_name: String,
content_type: String,
file_count: u64,
ctx: &mut TxContext,
): DatasetListing {
let listing = DatasetListing {
id: object::new(ctx),
seller: ctx.sender(),
price,
blob_id,
encrypted_object,
name,
description,
preview_size,
total_size,
image_url,
is_active: true,
mime_type,
file_name,
content_type,
file_count,
};
// Register to registry
vec_set::insert(&mut registry.listings, object::id(&listing));
event::emit(DatasetListed {
dataset_id: object::id(&listing),
seller: ctx.sender(),
price,
name,
});
listing
}
/// Purchase a dataset - creates receipt and transfers payment to seller
public fun purchase_dataset(
listing: &DatasetListing,
payment: Coin<SUI>,
clock: &sui::clock::Clock,
ctx: &mut TxContext,
): PurchaseReceipt {
assert!(listing.is_active, EListingInactive);
assert!(coin::value(&payment) >= listing.price, EInsufficientPayment);
// Transfer payment to seller
transfer::public_transfer(payment, listing.seller);
let receipt = PurchaseReceipt {
id: object::new(ctx),
dataset_id: object::id(listing),
buyer: ctx.sender(),
seller: listing.seller,
price: listing.price,
timestamp: sui::clock::timestamp_ms(clock),
};
event::emit(DatasetPurchased {
dataset_id: object::id(listing),
buyer: ctx.sender(),
seller: listing.seller,
price: listing.price,
});
receipt
}
// ============ Registry Functions ============
/// Check if listing exists in registry
public fun is_registered(registry: &DatasetRegistry, listing_id: ID): bool {
vec_set::contains(®istry.listings, &listing_id)
}
/// Remove listing from registry
public fun unregister_listing(registry: &mut DatasetRegistry, listing: &mut DatasetListing, ctx: &mut TxContext) {
assert!(listing.seller == ctx.sender(), ENotSeller);
assert!(listing.is_active, EListingInactive);
vec_set::remove(&mut registry.listings, &object::id(listing));
listing.is_active = false;
event::emit(DatasetUnlisted {
dataset_id: object::id(listing),
seller: listing.seller,
});
}
/// Number of listings in registry
public fun listings_count(registry: &DatasetRegistry): u64 {
vec_set::length(®istry.listings)
}
/// Get all listing IDs
public fun get_all_listings(registry: &DatasetRegistry): vector<ID> {
*vec_set::keys(®istry.listings)
}
public fun is_active(listing: &DatasetListing): bool {
listing.is_active
}
// ============ Test Only Functions ============
#[test_only]
public fun new_registry_for_testing(ctx: &mut TxContext): DatasetRegistry {
DatasetRegistry {
id: object::new(ctx),
listings: vec_set::empty(),
}
}