mrls 0.1.8

Unofficial Rust client for the Moralis Web3 API - tokens, NFTs, wallets, DeFi, and market data
Documentation
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! NFT API client

use super::types::{
    GetMultipleCollectionsRequest, GetMultipleNftsRequest, HistoricalFloorPrice, Nft,
    NftCollection, NftCollectionStats, NftFloorPrice, NftOwner, NftResponse, NftSalePrice,
    NftSyncStatus, NftTrade, NftTrait, NftTransfer, NftsByTraitsRequest, TraitResyncStatus,
};
use crate::client::Client;
use crate::error::Result;
use serde::Serialize;

/// Query parameters for NFT endpoints
#[derive(Debug, Default, Serialize)]
pub struct NftQuery {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chain: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub normalise_metadata: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media_items: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exclude_spam: Option<bool>,
}

impl NftQuery {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn chain(mut self, chain: impl Into<String>) -> Self {
        self.chain = Some(chain.into());
        self
    }

    #[must_use]
    pub fn cursor(mut self, cursor: impl Into<String>) -> Self {
        self.cursor = Some(cursor.into());
        self
    }

    #[must_use]
    pub fn limit(mut self, limit: i32) -> Self {
        self.limit = Some(limit);
        self
    }

    #[must_use]
    pub fn exclude_spam(mut self, exclude: bool) -> Self {
        self.exclude_spam = Some(exclude);
        self
    }
}

/// API for NFT operations
pub struct NftApi<'a> {
    client: &'a Client,
}

impl<'a> NftApi<'a> {
    #[must_use]
    pub fn new(client: &'a Client) -> Self {
        Self { client }
    }

    /// Get NFTs owned by an address
    pub async fn get_wallet_nfts(
        &self,
        address: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<Nft>> {
        let path = format!("/{address}/nft");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get NFT transfers by wallet
    pub async fn get_wallet_nft_transfers(
        &self,
        address: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftTransfer>> {
        let path = format!("/{address}/nft/transfers");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get NFT collections owned by an address
    pub async fn get_wallet_collections(
        &self,
        address: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftCollection>> {
        let path = format!("/{address}/nft/collections");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get NFT transfers by contract
    pub async fn get_contract_transfers(
        &self,
        contract: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftTransfer>> {
        let path = format!("/nft/{contract}/transfers");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get NFT transfers for a specific token
    pub async fn get_token_transfers(
        &self,
        contract: &str,
        token_id: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftTransfer>> {
        let path = format!("/nft/{contract}/{token_id}/transfers");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get collection metadata
    pub async fn get_collection_metadata(
        &self,
        contract: &str,
        chain: Option<&str>,
    ) -> Result<NftCollection> {
        let path = format!("/nft/{contract}/metadata");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.get_with_query(&path, &query).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get owners of NFTs in a collection
    pub async fn get_collection_owners(
        &self,
        contract: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftOwner>> {
        let path = format!("/nft/{contract}/owners");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get owners of a specific NFT token
    pub async fn get_token_owners(
        &self,
        contract: &str,
        token_id: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftOwner>> {
        let path = format!("/nft/{contract}/{token_id}/owners");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get NFT metadata for a specific token
    pub async fn get_nft_metadata(
        &self,
        contract: &str,
        token_id: &str,
        chain: Option<&str>,
    ) -> Result<Nft> {
        let path = format!("/nft/{contract}/{token_id}");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.get_with_query(&path, &query).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get collection stats
    pub async fn get_collection_stats(
        &self,
        contract: &str,
        chain: Option<&str>,
    ) -> Result<NftCollectionStats> {
        let path = format!("/nft/{contract}/stats");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.get_with_query(&path, &query).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get floor price for a collection
    pub async fn get_floor_price(
        &self,
        contract: &str,
        chain: Option<&str>,
    ) -> Result<NftFloorPrice> {
        let path = format!("/nft/{contract}/floor-price");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.get_with_query(&path, &query).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get floor price for a specific token
    pub async fn get_token_floor_price(
        &self,
        contract: &str,
        token_id: &str,
        chain: Option<&str>,
    ) -> Result<NftFloorPrice> {
        let path = format!("/nft/{contract}/{token_id}/floor-price");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.get_with_query(&path, &query).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get trades for a collection
    pub async fn get_collection_trades(
        &self,
        contract: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftTrade>> {
        let path = format!("/nft/{contract}/trades");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get trades for a specific token
    pub async fn get_token_trades(
        &self,
        contract: &str,
        token_id: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftTrade>> {
        let path = format!("/nft/{contract}/{token_id}/trades");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get wallet NFT trades
    pub async fn get_wallet_trades(
        &self,
        address: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftTrade>> {
        let path = format!("/wallets/{address}/nfts/trades");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get collection traits
    pub async fn get_collection_traits(
        &self,
        contract: &str,
        chain: Option<&str>,
    ) -> Result<Vec<NftTrait>> {
        let path = format!("/nft/{contract}/traits");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.get_with_query(&path, &query).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get collection traits with pagination
    pub async fn get_collection_traits_paginated(
        &self,
        contract: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftTrait>> {
        let path = format!("/nft/{contract}/traits/paginate");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get unique owners count
    pub async fn get_unique_owners(
        &self,
        contract: &str,
        chain: Option<&str>,
    ) -> Result<serde_json::Value> {
        let path = format!("/nft/{contract}/unique-owners");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.get_with_query(&path, &query).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Resync NFT metadata
    pub async fn resync_metadata(
        &self,
        contract: &str,
        token_id: &str,
        chain: Option<&str>,
    ) -> Result<serde_json::Value> {
        let path = format!("/nft/{contract}/{token_id}/metadata/resync");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.get_with_query(&path, &query).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get multiple NFTs by token addresses and IDs (batch)
    pub async fn get_multiple_nfts(
        &self,
        request: &GetMultipleNftsRequest,
        chain: Option<&str>,
    ) -> Result<Vec<Nft>> {
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client
                .post_with_query("/nft/getMultipleNFTs", request, &query)
                .await
        } else {
            self.client.post("/nft/getMultipleNFTs", request).await
        }
    }

    /// Get NFTs by traits
    pub async fn get_nfts_by_traits(
        &self,
        contract: &str,
        request: &NftsByTraitsRequest,
        chain: Option<&str>,
    ) -> Result<NftResponse<Nft>> {
        let path = format!("/nft/{contract}/nfts-by-traits");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.post_with_query(&path, request, &query).await
        } else {
            self.client.post(&path, request).await
        }
    }

    /// Get historical floor price for a collection
    pub async fn get_floor_price_historical(
        &self,
        contract: &str,
        chain: Option<&str>,
    ) -> Result<Vec<HistoricalFloorPrice>> {
        let path = format!("/nft/{contract}/floor-price/historical");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.get_with_query(&path, &query).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Sync NFT collection metadata
    pub async fn sync_collection(
        &self,
        contract: &str,
        chain: Option<&str>,
    ) -> Result<NftSyncStatus> {
        let body = serde_json::json!({});
        let path = if let Some(c) = chain {
            format!("/nft/{contract}/sync?chain={c}")
        } else {
            format!("/nft/{contract}/sync")
        };
        self.client.put(&path, &body).await
    }

    /// Get NFTs by contract address
    pub async fn get_contract_nfts(
        &self,
        contract: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<Nft>> {
        let path = format!("/nft/{contract}");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get metadata for multiple NFT contracts (batch)
    pub async fn get_multiple_collections(
        &self,
        request: &GetMultipleCollectionsRequest,
        chain: Option<&str>,
    ) -> Result<Vec<NftCollection>> {
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client
                .post_with_query("/nft/metadata", request, &query)
                .await
        } else {
            self.client.post("/nft/metadata", request).await
        }
    }

    /// Resync NFT collection traits
    pub async fn resync_traits(
        &self,
        contract: &str,
        chain: Option<&str>,
    ) -> Result<TraitResyncStatus> {
        let path = format!("/nft/{contract}/traits/resync");
        if let Some(chain) = chain {
            let query = NftQuery::new().chain(chain);
            self.client.get_with_query(&path, &query).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get NFT sale prices by collection
    pub async fn get_collection_prices(
        &self,
        contract: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftSalePrice>> {
        let path = format!("/nft/{contract}/price");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }

    /// Get NFT sale prices by token
    pub async fn get_token_prices(
        &self,
        contract: &str,
        token_id: &str,
        query: Option<&NftQuery>,
    ) -> Result<NftResponse<NftSalePrice>> {
        let path = format!("/nft/{contract}/{token_id}/price");
        if let Some(q) = query {
            self.client.get_with_query(&path, q).await
        } else {
            self.client.get(&path).await
        }
    }
}