Skip to content

Conversation

piobab
Copy link
Collaborator

@piobab piobab commented Aug 29, 2023

Common way to do pagination in Cosmwasm is using limit, start_after and respond with vector of data. For example:

// settings for pagination
const MAX_LIMIT: u32 = 30;
const DEFAULT_LIMIT: u32 = 10;

pub fn query_list_members(
    deps: Deps,
    start_after: Option<String>,
    limit: Option<u32>,
) -> StdResult<MemberListResponse> {
    let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
    let addr = maybe_addr(deps.api, start_after)?;
    let start = addr.as_ref().map(Bound::exclusive);

    let members = MEMBERS
        .range(deps.storage, start, None, Order::Ascending)
        .take(limit)
        .map(|item| {
            item.map(|(addr, weight)| Member {
                addr: addr.into(),
                weight,
            })
        })
        .collect::<StdResult<_>>()?;

    Ok(MemberListResponse { members })
}

As an API user if I want to know if there is more data:

  • check if responded number of elements is less than sent limit parameter. This is problematic because if I send limit > MAX_LIMIT I get less elements than requested - thinking that there is no more data.
  • execute next query and see if there is more data. This is not efficient because I make extra query.
    Moreover, it is easy to forget about pagination as we noticed during contract configuration / validation. You could query something and you basically use default pagination params and don't see the rest of elements.

Solution
Add metadata data

#[cw_serde]
pub struct PaginationResponse<T> {
    pub data: Vec<T>,
    pub metadata: Metadata,
}

#[cw_serde]
pub struct Metadata {
    pub has_more: bool,
    pub next_start_after: Option<String>,
}

This way we see in the response if there is more data. Easy to operate on without depending on default params.

@piobab piobab changed the base branch from master to release/mars-v2 August 29, 2023 12:46
@piobab piobab marked this pull request as ready for review August 30, 2023 16:18
@piobab
Copy link
Collaborator Author

piobab commented Aug 30, 2023

This PR will be used to fix issue no. 5 from Oak.

@thec00n
Copy link

thec00n commented Aug 31, 2023

@piobab is there already a PR that fixes the audit issue on the rover side?

@piobab
Copy link
Collaborator Author

piobab commented Aug 31, 2023

@piobab is there already a PR that fixes the audit issue on the rover side?

mars-protocol/rover#191

@piobab piobab merged commit 757bf3c into release/mars-v2 Aug 31, 2023
@piobab piobab deleted the fix/MP-3296-pagination branch September 14, 2023 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants