Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 add 'getListings' method #86

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions marketplace/marketplace.did
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ service : (principal, nat, principal) -> {
getFloor : (principal) -> (Result_1) query;
getProtocolFee : () -> (nat) query;
getTokenListing : (principal, nat) -> (Result_2) query;
getListings : (principal, bool, nat) -> (
variant { Ok : vec record { nat; Listing }; Err : MPApiError }
) query;
getTokenOffers : (principal, vec nat) -> (
vec record { nat; vec Offer },
) query;
Expand Down
55 changes: 55 additions & 0 deletions marketplace/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,61 @@ pub async fn get_token_listing(
.clone())
}

/// Get a tokens listings.
/// Parameters:
/// nft_canister_id: NFT canister (principal) id
/// own: Pass `true` for retrieve own listed tokens, otherwise pass `false` for retrieve other user's listed tokens
/// page: Page number (start from 0)
/// Returns listed tokens list based on given parameters.
#[query(name = "getListings")]
#[candid_method(query, rename = "getListings")]
pub async fn get_listings(nft_canister_id: Principal, own: bool, page: u128) -> Result<HashMap<Nat, Listing>, MPApiError> {
// verify collection is registered
let collection = collections()
.collections
.get(&nft_canister_id)
.ok_or(MPApiError::NonExistentCollection)?;

let listings = marketplace()
.listings
.entry(nft_canister_id)
.or_default();

// Filter tokens
let filteredTokens: Vec<Nat> = listings
.into_iter()
.filter(|(_, v)| if own == true {v.seller == ic::caller()} else {v.seller != ic::caller()})
.map(|(k, _)| k.clone())
.by_ref()
.collect();

// Calculate pagination
let start = (page * 10) as usize;
let mut len = 10;

// Make sure we already not reached the end in that case return empty hash map
if start > filteredTokens.len() { return Ok(HashMap::new()); }

// If there is still data then calculate next len
if start + len >= filteredTokens.len() {
len = filteredTokens.len() - start;
}

// Slice the filtered token for specified length and return the paginated listing
Ok(filteredTokens[start..start + len].to_vec()
.into_iter()
.map(|token_id| {
(
token_id.clone(),
listings
.entry(token_id)
.or_default()
.clone(),
)
})
.collect())
}

/// Get a tokens current offers. Can pass as many token ids as you want
#[query(name = "getTokenOffers")]
#[candid_method(query, rename = "getTokenOffers")]
Expand Down