Skip to content

Commit

Permalink
Merge pull request #41 from public-awesome/fix-q
Browse files Browse the repository at this point in the history
Fixed query pagination
  • Loading branch information
shanev authored Oct 12, 2022
2 parents 66dfefc + 2da54e8 commit 46f386f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
9 changes: 8 additions & 1 deletion contracts/marketplace/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,20 @@ pub fn reverse_query_asks(
) -> StdResult<AsksResponse> {
let limit = limit.unwrap_or(DEFAULT_QUERY_LIMIT).min(MAX_QUERY_LIMIT) as usize;

let start = start_before.unwrap_or(
(asks()
.keys_raw(deps.storage, None, None, Order::Ascending)
.count()
+ 1) as u64,
);

let asks = asks()
.idx
.id
.range(
deps.storage,
None,
Some(Bound::exclusive(start_before.unwrap_or_default())),
Some(Bound::exclusive(start)),
Order::Descending,
)
.take(limit)
Expand Down
15 changes: 2 additions & 13 deletions contracts/marketplace/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ pub struct AskIndicies<'a> {
pub id: UniqueIndex<'a, u64, Ask, AskKey>,
/// Index by seller
pub seller: MultiIndex<'a, Addr, Ask, AskKey>,
/// Keeps track of whene renewal has to happen
pub height: MultiIndex<'a, u64, Ask, AskKey>,
}

impl<'a> IndexList<Ask> for AskIndicies<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Ask>> + '_> {
let v: Vec<&dyn Index<Ask>> = vec![&self.id, &self.seller, &self.height];
let v: Vec<&dyn Index<Ask>> = vec![&self.id, &self.seller];
Box::new(v.into_iter())
}
}
Expand All @@ -96,7 +94,6 @@ pub fn asks<'a>() -> IndexedMap<'a, AskKey, Ask, AskIndicies<'a>> {
"asks",
"asks__seller",
),
height: MultiIndex::new(|_pk: &[u8], d: &Ask| d.height, "asks", "asks__height"),
};
IndexedMap::new("asks", indexes)
}
Expand Down Expand Up @@ -130,26 +127,19 @@ pub fn bid_key(token_id: &str, bidder: &Addr) -> BidKey {

/// Defines incides for accessing bids
pub struct BidIndicies<'a> {
pub token_id: MultiIndex<'a, TokenId, Bid, BidKey>,
pub price: MultiIndex<'a, u128, Bid, BidKey>,
pub bidder: MultiIndex<'a, Addr, Bid, BidKey>,
pub height: MultiIndex<'a, u64, Bid, BidKey>,
}

impl<'a> IndexList<Bid> for BidIndicies<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<Bid>> + '_> {
let v: Vec<&dyn Index<Bid>> = vec![&self.token_id, &self.price, &self.bidder];
let v: Vec<&dyn Index<Bid>> = vec![&self.price, &self.bidder];
Box::new(v.into_iter())
}
}

pub fn bids<'a>() -> IndexedMap<'a, BidKey, Bid, BidIndicies<'a>> {
let indexes = BidIndicies {
token_id: MultiIndex::new(
|_pk: &[u8], d: &Bid| d.token_id.clone(),
"bids",
"bids__collection_token_id",
),
price: MultiIndex::new(
|_pk: &[u8], d: &Bid| d.amount.u128(),
"bids",
Expand All @@ -160,7 +150,6 @@ pub fn bids<'a>() -> IndexedMap<'a, BidKey, Bid, BidIndicies<'a>> {
"bids",
"bids__bidder",
),
height: MultiIndex::new(|_pk: &[u8], d: &Bid| d.height, "bids", "bids__height"),
};
IndexedMap::new("bids", indexes)
}
2 changes: 1 addition & 1 deletion contracts/name-minter/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ mod query {
mint_and_list(&mut app, "hack", USER);

let msg = MarketplaceQueryMsg::ReverseAsks {
start_before: Some(5),
start_before: None,
limit: None,
};
let res: AsksResponse = app.wrap().query_wasm_smart(MKT, &msg).unwrap();
Expand Down

0 comments on commit 46f386f

Please sign in to comment.