Skip to content

Commit ae42b7d

Browse files
committed
Update cw721-base with new index format
1 parent fad0402 commit ae42b7d

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

contracts/cw721-base/src/contract.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ use cw721::{
1313
use crate::error::ContractError;
1414
use crate::msg::{HandleMsg, InitMsg, MintMsg, MinterResponse, QueryMsg};
1515
use crate::state::{
16-
increment_tokens, num_tokens, tokens, Approval, TokenInfo, CONTRACT_INFO, IDX_OWNER, MINTER,
17-
OPERATORS,
16+
increment_tokens, num_tokens, tokens, Approval, TokenInfo, CONTRACT_INFO, MINTER, OPERATORS,
1817
};
19-
use cw_storage_plus::{Bound, OwnedBound};
18+
use cw_storage_plus::Bound;
2019

2120
// version info for migration info
2221
const CONTRACT_NAME: &str = "crates.io:cw721-base";
@@ -486,12 +485,12 @@ fn query_all_approvals<S: Storage, A: Api, Q: Querier>(
486485
) -> StdResult<ApprovedForAllResponse> {
487486
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
488487
let start_canon = maybe_canonical(deps.api, start_after)?;
489-
let start = OwnedBound::exclusive(start_canon);
488+
let start = start_canon.map(Bound::exclusive);
490489

491490
let owner_raw = deps.api.canonical_address(&owner)?;
492491
let res: StdResult<Vec<_>> = OPERATORS
493492
.prefix(&owner_raw)
494-
.range(&deps.storage, start.bound(), Bound::None, Order::Ascending)
493+
.range(&deps.storage, start, None, Order::Ascending)
495494
.filter(|r| include_expired || r.is_err() || !r.as_ref().unwrap().1.is_expired(&env.block))
496495
.take(limit)
497496
.map(|item| parse_approval(deps.api, item))
@@ -514,12 +513,14 @@ fn query_tokens<S: Storage, A: Api, Q: Querier>(
514513
) -> StdResult<TokensResponse> {
515514
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
516515
// TODO: get pagination working on second indexes
517-
let _start = OwnedBound::exclusive(start_after.map(Vec::from));
516+
let _start = start_after.map(Bound::exclusive);
518517

519518
let owner_raw = deps.api.canonical_address(&owner)?;
520519
let tokens: Result<Vec<String>, _> = tokens::<S>()
521-
.pks_by_index(&deps.storage, IDX_OWNER, &owner_raw)?
522-
// .range(&deps.storage, start.bound(), Bound::None, Order::Ascending)
520+
.idx
521+
.owner
522+
.pks(&deps.storage, &owner_raw)
523+
// .range(&deps.storage, start.bound(), None, Order::Ascending)
523524
.take(limit)
524525
.map(String::from_utf8)
525526
.collect();
@@ -534,10 +535,10 @@ fn query_all_tokens<S: Storage, A: Api, Q: Querier>(
534535
limit: Option<u32>,
535536
) -> StdResult<TokensResponse> {
536537
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
537-
let start = OwnedBound::exclusive(start_after.map(Vec::from));
538+
let start = start_after.map(Bound::exclusive);
538539

539540
let tokens: StdResult<Vec<String>> = tokens::<S>()
540-
.range(&deps.storage, start.bound(), Bound::None, Order::Ascending)
541+
.range(&deps.storage, start, None, Order::Ascending)
541542
.take(limit)
542543
.map(|item| item.map(|(k, _)| String::from_utf8_lossy(&k).to_string()))
543544
.collect();

contracts/cw721-base/src/state.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
33

44
use cosmwasm_std::{CanonicalAddr, StdResult, Storage};
55
use cw721::{ContractInfoResponse, Expiration};
6-
use cw_storage_plus::{IndexedMap, Item, Map};
6+
use cw_storage_plus::{Index, IndexList, IndexedMap, Item, Map, MultiIndex};
77

88
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
99
pub struct TokenInfo {
@@ -45,11 +45,20 @@ pub fn increment_tokens<S: Storage>(storage: &mut S) -> StdResult<u64> {
4545
Ok(val)
4646
}
4747

48-
pub const IDX_OWNER: &str = "owner";
48+
pub struct TokenIndexes<'a, S: Storage> {
49+
pub owner: MultiIndex<'a, S, TokenInfo>,
50+
}
51+
52+
impl<'a, S: Storage> IndexList<S, TokenInfo> for TokenIndexes<'a, S> {
53+
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<S, TokenInfo>> + '_> {
54+
let v: Vec<&dyn Index<S, TokenInfo>> = vec![&self.owner];
55+
Box::new(v.into_iter())
56+
}
57+
}
4958

50-
// indexed map needs function, not const (for now at least)
51-
pub fn tokens<'a, S: Storage + 'a>() -> IndexedMap<'a, 'a, &'a str, TokenInfo, S> {
52-
IndexedMap::<&str, TokenInfo, S>::new(b"tokens")
53-
.with_index(IDX_OWNER, b"tokens__owner", |d| d.owner.to_vec())
54-
.unwrap()
59+
pub fn tokens<'a, S: Storage>() -> IndexedMap<'a, &'a str, TokenInfo, S, TokenIndexes<'a, S>> {
60+
let indexes = TokenIndexes {
61+
owner: MultiIndex::new(|d| d.owner.to_vec(), b"tokens", b"tokens__owner"),
62+
};
63+
IndexedMap::new(b"tokens", indexes)
5564
}

packages/storage-plus/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod prefix;
1111

1212
pub use endian::Endian;
1313
#[cfg(feature = "iterator")]
14-
pub use indexed_map::IndexedMap;
14+
pub use indexed_map::{IndexList, IndexedMap};
1515
#[cfg(feature = "iterator")]
1616
pub use indexes::{index_int, index_string, Index, MultiIndex, UniqueIndex};
1717
pub use item::Item;

0 commit comments

Comments
 (0)