hyperspace-rs 0.2.0

A Rust library to interact with [Hyperspace](https://avax.hyperspace.xyz/) NFT marketplace on Avalanche
Documentation
use ethers::types::H160;
use serde::{Deserialize, Serialize};

use crate::{
    collection::types::GetBidsRequest,
    constants::{GET_COLLECTION_BIDS, GET_COLLECTION_STATS},
    types::{Bid, CollectionStats},
};

use super::{HyperspaceClient, HyperspaceClientError};

mod types;

use types::{GetBidsResponse, GetStatsRequest, GetStatsResponse};

pub struct Collection<'a>(pub(crate) &'a HyperspaceClient);

impl<'a> Collection<'a> {
    pub async fn get_stats(
        &self,
        project_id: impl AsRef<str>,
    ) -> Result<CollectionStats, HyperspaceClientError> {
        let body = GetStatsRequest::new(project_id.as_ref().to_string());
        let res = self
            .0
            .post::<GetStatsRequest, GetStatsResponse>(GET_COLLECTION_STATS, body)
            .await?;

        match res.project_stats.get(0) {
            Some(stats) => Ok(stats.to_owned()),
            None => Err(HyperspaceClientError::NoCollectionStats),
        }
    }

    pub async fn get_floor(
        &self,
        project_id: impl AsRef<str>,
    ) -> Result<f64, HyperspaceClientError> {
        Ok(self.get_stats(project_id).await?.floor_price)
    }

    pub async fn get_bids(
        &self,
        collection_address: H160,
    ) -> Result<Vec<Bid>, HyperspaceClientError> {
        let body = GetBidsRequest::new(collection_address);
        let res = self
            .0
            .post::<GetBidsRequest, GetBidsResponse>(GET_COLLECTION_BIDS, body)
            .await?;

        Ok(res.bids)
    }

    pub async fn get_highest_bid(
        &self,
        collection_address: H160,
    ) -> Result<Bid, HyperspaceClientError> {
        let bids = self.get_bids(collection_address).await?;

        match bids.get(0) {
            Some(bid) => Ok(bid.to_owned()),
            None => Err(HyperspaceClientError::NoHighestBid),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;

    async fn setup() -> HyperspaceClient {
        dotenv::dotenv().ok();

        let api_key = dotenv::var("API_KEY").unwrap();
        let private_key = dotenv::var("PRIVATE_KEY").unwrap();

        HyperspaceClient::new(api_key, private_key, None::<&str>).unwrap()
    }

    #[tokio::test]
    async fn test_get_stats() {
        let hs_client = setup().await;

        let stats = hs_client
            .collection()
            .get_stats("c43f2070-8d90-40f2-a81a-d564889263ee")
            .await
            .unwrap();

        dbg!(stats);
    }

    #[tokio::test]
    async fn test_get_floor() {
        let hs_client = setup().await;

        let floor = hs_client
            .collection()
            .get_floor("c43f2070-8d90-40f2-a81a-d564889263ee")
            .await
            .unwrap();

        dbg!(floor);
    }

    #[tokio::test]
    async fn test_get_bids() {
        let hs_client = setup().await;

        let bids = hs_client
            .collection()
            .get_bids(H160::from_str("0x54c800d2331e10467143911aabca092d68bf4166").unwrap())
            .await
            .unwrap();

        dbg!(bids);
    }

    #[tokio::test]
    async fn test_get_highest_bid() {
        let hs_client = setup().await;

        let bid = hs_client
            .collection()
            .get_highest_bid(H160::from_str("0x54c800d2331e10467143911aabca092d68bf4166").unwrap())
            .await
            .unwrap();

        dbg!(bid);
    }
}