|
| 1 | +# Dataset Marketplace API - Buyer & Seller |
| 2 | + |
| 3 | +**Enclave Base URL**: `http://3.85.23.97:3000` |
| 4 | +**Sui RPC Endpoint:** `https://fullnode.testnet.sui.io:443` |
| 5 | + |
| 6 | +## Contract Info |
| 7 | + |
| 8 | +| Key | Value | |
| 9 | +| ------------------------- | -------------------------------------------------------------------- | |
| 10 | +| Package ID | `0x3ee8141400c1f4accbe52f3be4291e8faf6051dfbaaea794e052a5bbe974884c` | |
| 11 | +| DatasetRegistry Object ID | `0x6cf4a393c7543d9f7ccc97759bed957c88bd7c8790284a1fa6275f5aa3b51502` | |
| 12 | +| Network | Testnet | |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## On-Chain Queries (Sui RPC) |
| 17 | + |
| 18 | +### 1. Get All Listings from Registry |
| 19 | + |
| 20 | +Lấy danh sách tất cả dataset listing IDs từ Registry. |
| 21 | + |
| 22 | +**Request:** |
| 23 | + |
| 24 | +```http |
| 25 | +POST / |
| 26 | +Content-Type: application/json |
| 27 | +``` |
| 28 | + |
| 29 | +**Body:** |
| 30 | + |
| 31 | +```json |
| 32 | +{ |
| 33 | + "jsonrpc": "2.0", |
| 34 | + "id": 1, |
| 35 | + "method": "sui_getObject", |
| 36 | + "params": [ |
| 37 | + "0x6cf4a393c7543d9f7ccc97759bed957c88bd7c8790284a1fa6275f5aa3b51502", |
| 38 | + { |
| 39 | + "showContent": true |
| 40 | + } |
| 41 | + ] |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +**Response:** |
| 46 | + |
| 47 | +```json |
| 48 | +{ |
| 49 | + "jsonrpc": "2.0", |
| 50 | + "id": 1, |
| 51 | + "result": { |
| 52 | + "data": { |
| 53 | + "objectId": "0x...", |
| 54 | + "content": { |
| 55 | + "dataType": "moveObject", |
| 56 | + "type": "0x3ee8141400c1f4accbe52f3be4291e8faf6051dfbaaea794e052a5bbe974884c::marketplace::DatasetRegistry", |
| 57 | + "fields": { |
| 58 | + "id": { "id": "0x..." }, |
| 59 | + "listings": { |
| 60 | + "type": "0x2::vec_set::VecSet<0x2::object::ID>", |
| 61 | + "fields": { |
| 62 | + "contents": ["0xabc123...", "0xdef456...", "0x789ghi..."] |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +**Response Fields:** |
| 73 | +| Field | Description | |
| 74 | +|-------|-------------| |
| 75 | +| listings.fields.contents | Array of DatasetListing object IDs | |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +### 2. Get Dataset Listing Details |
| 80 | + |
| 81 | +Lấy chi tiết một DatasetListing từ object ID. |
| 82 | + |
| 83 | +**Body:** |
| 84 | + |
| 85 | +```json |
| 86 | +{ |
| 87 | + "jsonrpc": "2.0", |
| 88 | + "id": 1, |
| 89 | + "method": "sui_getObject", |
| 90 | + "params": [ |
| 91 | + "<LISTING_OBJECT_ID>", |
| 92 | + { |
| 93 | + "showContent": true |
| 94 | + } |
| 95 | + ] |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +**Response:** |
| 100 | + |
| 101 | +```json |
| 102 | +{ |
| 103 | + "jsonrpc": "2.0", |
| 104 | + "id": 1, |
| 105 | + "result": { |
| 106 | + "data": { |
| 107 | + "objectId": "0xabc123...", |
| 108 | + "content": { |
| 109 | + "dataType": "moveObject", |
| 110 | + "type": "0x3ee8141400c1f4accbe52f3be4291e8faf6051dfbaaea794e052a5bbe974884c::marketplace::DatasetListing", |
| 111 | + "fields": { |
| 112 | + "id": { "id": "0xabc123..." }, |
| 113 | + "seller": "0x1234567890abcdef...", |
| 114 | + "price": "1000000000", |
| 115 | + "blob_id": "XytJmscRNoZlvjHnpIQDEz_4N9Xqqu4LWwil09FbpJo", |
| 116 | + "encrypted_object": "a1b2c3d4e5f6...", |
| 117 | + "name": "Employee Dataset 2024", |
| 118 | + "description": "Contains employee records...", |
| 119 | + "preview_size": "1024", |
| 120 | + "total_size": "102400" |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +**Response Fields:** |
| 129 | +| Field | Type | Description | |
| 130 | +|-------|------|-------------| |
| 131 | +| seller | address | Địa chỉ người bán | |
| 132 | +| price | u64 | Giá (MIST, 1 SUI = 10^9 MIST) | |
| 133 | +| blob_id | string | Walrus blob ID | |
| 134 | +| encrypted_object | string | Hex-encoded encrypted data | |
| 135 | +| name | string | Tên dataset | |
| 136 | +| description | string | Mô tả dataset | |
| 137 | +| preview_size | u64 | Bytes cho phép preview | |
| 138 | +| total_size | u64 | Tổng kích thước dataset | |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +### 3. Get Multiple Listings (Batch) |
| 143 | + |
| 144 | +Lấy nhiều listings cùng lúc. |
| 145 | + |
| 146 | +**Body:** |
| 147 | + |
| 148 | +```json |
| 149 | +{ |
| 150 | + "jsonrpc": "2.0", |
| 151 | + "id": 1, |
| 152 | + "method": "sui_multiGetObjects", |
| 153 | + "params": [ |
| 154 | + ["0xabc123...", "0xdef456...", "0x789ghi..."], |
| 155 | + { |
| 156 | + "showContent": true |
| 157 | + } |
| 158 | + ] |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +**Response:** |
| 163 | + |
| 164 | +```json |
| 165 | +{ |
| 166 | + "jsonrpc": "2.0", |
| 167 | + "id": 1, |
| 168 | + "result": [ |
| 169 | + { |
| 170 | + "data": { |
| 171 | + "objectId": "0xabc123...", |
| 172 | + "content": { "fields": { "name": "Dataset 1", "...": "..." } } |
| 173 | + } |
| 174 | + }, |
| 175 | + { |
| 176 | + "data": { |
| 177 | + "objectId": "0xdef456...", |
| 178 | + "content": { "fields": { "name": "Dataset 2", "...": "..." } } |
| 179 | + } |
| 180 | + } |
| 181 | + ] |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +--- |
| 186 | + |
| 187 | +## Frontend Example (TypeScript) |
| 188 | + |
| 189 | +```typescript |
| 190 | +import { SuiClient } from "@mysten/sui/client"; |
| 191 | + |
| 192 | +const client = new SuiClient({ url: "https://fullnode.testnet.sui.io:443" }); |
| 193 | + |
| 194 | +const REGISTRY_ID = |
| 195 | + "0x6cf4a393c7543d9f7ccc97759bed957c88bd7c8790284a1fa6275f5aa3b51502"; |
| 196 | +const PACKAGE_ID = |
| 197 | + "0x3ee8141400c1f4accbe52f3be4291e8faf6051dfbaaea794e052a5bbe974884c"; |
| 198 | + |
| 199 | +// 1. Get all listing IDs from registry |
| 200 | +async function getAllListingIds(): Promise<string[]> { |
| 201 | + const registry = await client.getObject({ |
| 202 | + id: REGISTRY_ID, |
| 203 | + options: { showContent: true }, |
| 204 | + }); |
| 205 | + |
| 206 | + const content = registry.data?.content as any; |
| 207 | + return content?.fields?.listings?.fields?.contents || []; |
| 208 | +} |
| 209 | + |
| 210 | +// 2. Get listing details |
| 211 | +async function getListingDetails(listingId: string) { |
| 212 | + const listing = await client.getObject({ |
| 213 | + id: listingId, |
| 214 | + options: { showContent: true }, |
| 215 | + }); |
| 216 | + |
| 217 | + const fields = (listing.data?.content as any)?.fields; |
| 218 | + return { |
| 219 | + id: listingId, |
| 220 | + seller: fields.seller, |
| 221 | + price: BigInt(fields.price), |
| 222 | + blobId: fields.blob_id, |
| 223 | + name: fields.name, |
| 224 | + description: fields.description, |
| 225 | + previewSize: Number(fields.preview_size), |
| 226 | + totalSize: Number(fields.total_size), |
| 227 | + }; |
| 228 | +} |
| 229 | + |
| 230 | +// 3. Get all listings with details |
| 231 | +async function getAllListings() { |
| 232 | + const ids = await getAllListingIds(); |
| 233 | + |
| 234 | + const objects = await client.multiGetObjects({ |
| 235 | + ids, |
| 236 | + options: { showContent: true }, |
| 237 | + }); |
| 238 | + |
| 239 | + return objects.map((obj) => { |
| 240 | + const fields = (obj.data?.content as any)?.fields; |
| 241 | + return { |
| 242 | + id: obj.data?.objectId, |
| 243 | + seller: fields.seller, |
| 244 | + price: BigInt(fields.price), |
| 245 | + blobId: fields.blob_id, |
| 246 | + name: fields.name, |
| 247 | + description: fields.description, |
| 248 | + previewSize: Number(fields.preview_size), |
| 249 | + totalSize: Number(fields.total_size), |
| 250 | + }; |
| 251 | + }); |
| 252 | +} |
| 253 | + |
| 254 | +// 4. Preview dataset |
| 255 | +async function previewDataset( |
| 256 | + datasetId: string, |
| 257 | + blobId: string, |
| 258 | + address: string, |
| 259 | +) { |
| 260 | + const response = await fetch("http://3.85.23.97:3000/process_data", { |
| 261 | + method: "POST", |
| 262 | + headers: { "Content-Type": "application/json" }, |
| 263 | + body: JSON.stringify({ |
| 264 | + payload: { |
| 265 | + dataset_id: datasetId, |
| 266 | + blob_id: blobId, |
| 267 | + preview_bytes: 1024, |
| 268 | + requester_address: address, |
| 269 | + }, |
| 270 | + }), |
| 271 | + }); |
| 272 | + return response.json(); |
| 273 | +} |
| 274 | + |
| 275 | +// 5. Purchase dataset (after on-chain payment) |
| 276 | +async function purchaseDataset( |
| 277 | + datasetId: string, |
| 278 | + blobId: string, |
| 279 | + txDigest: string, |
| 280 | + buyerAddress: string, |
| 281 | +) { |
| 282 | + const response = await fetch("http://3.85.23.97:3000/process_data", { |
| 283 | + method: "POST", |
| 284 | + headers: { "Content-Type": "application/json" }, |
| 285 | + body: JSON.stringify({ |
| 286 | + payload: { |
| 287 | + dataset_id: datasetId, |
| 288 | + blob_id: blobId, |
| 289 | + payment_tx_digest: txDigest, |
| 290 | + buyer_address: buyerAddress, |
| 291 | + }, |
| 292 | + }), |
| 293 | + }); |
| 294 | + return response.json(); |
| 295 | +} |
| 296 | +``` |
0 commit comments