Skip to content

Commit

Permalink
Merge pull request #576 from CosmWasm/460-range-to-range_raw
Browse files Browse the repository at this point in the history
`range` to `range raw`
  • Loading branch information
maurolacy committed Dec 14, 2021
2 parents c56d397 + 4181f54 commit 90c49f4
Show file tree
Hide file tree
Showing 21 changed files with 512 additions and 543 deletions.
30 changes: 13 additions & 17 deletions contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ pub fn query_all_allowances(
// we use raw addresses here....
let start = start_after.map(Bound::exclusive);

let res: StdResult<Vec<AllowanceInfo>> = ALLOWANCES
let allowances = ALLOWANCES
.range(deps.storage, start, None, Order::Ascending)
.filter(|item| {
if let Ok((_, allow)) = item {
Expand All @@ -420,16 +420,14 @@ pub fn query_all_allowances(
})
.take(limit)
.map(|item| {
item.and_then(|(k, allow)| {
Ok(AllowanceInfo {
spender: String::from_utf8(k)?,
balance: allow.balance,
expires: allow.expires,
})
item.map(|(addr, allow)| AllowanceInfo {
spender: addr.into(),
balance: allow.balance,
expires: allow.expires,
})
})
.collect();
Ok(AllAllowancesResponse { allowances: res? })
.collect::<StdResult<Vec<_>>>()?;
Ok(AllAllowancesResponse { allowances })
}

// return a list of all permissions here
Expand All @@ -441,19 +439,17 @@ pub fn query_all_permissions(
let limit = calc_limit(limit);
let start = start_after.map(Bound::exclusive);

let res: StdResult<Vec<PermissionsInfo>> = PERMISSIONS
let permissions = PERMISSIONS
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
item.and_then(|(k, perm)| {
Ok(PermissionsInfo {
spender: String::from_utf8(k)?,
permissions: perm,
})
item.map(|(addr, perm)| PermissionsInfo {
spender: addr.into(),
permissions: perm,
})
})
.collect();
Ok(AllPermissionsResponse { permissions: res? })
.collect::<StdResult<Vec<_>>>()?;
Ok(AllPermissionsResponse { permissions })
}

// Migrate contract if version is lower than current version
Expand Down
20 changes: 9 additions & 11 deletions contracts/cw1155-base/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Order, Record, Response, StdResult,
SubMsg, Uint128,
to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Order, Response, StdResult, SubMsg,
Uint128,
};
use cw_storage_plus::Bound;

Expand Down Expand Up @@ -478,10 +478,10 @@ pub fn query(deps: Deps, env: Env, msg: Cw1155QueryMsg) -> StdResult<Binary> {
}
}

fn parse_approval(item: StdResult<Record<Expiration>>) -> StdResult<cw1155::Approval> {
item.and_then(|(k, expires)| {
let spender = String::from_utf8(k)?;
Ok(cw1155::Approval { spender, expires })
fn build_approval(item: StdResult<(Addr, Expiration)>) -> StdResult<cw1155::Approval> {
item.map(|(addr, expires)| cw1155::Approval {
spender: addr.into(),
expires,
})
}

Expand All @@ -501,7 +501,7 @@ fn query_all_approvals(
.range(deps.storage, start, None, Order::Ascending)
.filter(|r| include_expired || r.is_err() || !r.as_ref().unwrap().1.is_expired(&env.block))
.take(limit)
.map(parse_approval)
.map(build_approval)
.collect::<StdResult<_>>()?;
Ok(ApprovedForAllResponse { operators })
}
Expand All @@ -517,9 +517,8 @@ fn query_tokens(

let tokens = BALANCES
.prefix(&owner)
.range(deps.storage, start, None, Order::Ascending)
.keys(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| item.map(|(k, _)| String::from_utf8(k).unwrap()))
.collect::<StdResult<_>>()?;
Ok(TokensResponse { tokens })
}
Expand All @@ -532,9 +531,8 @@ fn query_all_tokens(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);
let tokens = TOKENS
.range(deps.storage, start, None, Order::Ascending)
.keys(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| item.map(|(k, _)| String::from_utf8(k).unwrap()))
.collect::<StdResult<_>>()?;
Ok(TokensResponse { tokens })
}
Expand Down
3 changes: 1 addition & 2 deletions contracts/cw20-atomic-swap/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{Addr, Binary, BlockInfo, Order, StdError, StdResult, Storage};
use cosmwasm_std::{Addr, Binary, BlockInfo, Order, StdResult, Storage};
use cw_storage_plus::{Bound, Map};

use cw20::{Balance, Expiration};
Expand Down Expand Up @@ -34,7 +34,6 @@ pub fn all_swap_ids(
SWAPS
.keys(storage, start, None, Order::Ascending)
.take(limit)
.map(|k| String::from_utf8(k).map_err(|_| StdError::invalid_utf8("Parsing swap id")))
.collect()
}

Expand Down
27 changes: 11 additions & 16 deletions contracts/cw20-base/src/enumerable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@ pub fn query_all_allowances(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);

let allowances: StdResult<Vec<AllowanceInfo>> = ALLOWANCES
let allowances = ALLOWANCES
.prefix(&owner_addr)
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
let (k, v) = item?;
Ok(AllowanceInfo {
spender: String::from_utf8(k)?,
allowance: v.allowance,
expires: v.expires,
item.map(|(addr, allow)| AllowanceInfo {
spender: addr.into(),
allowance: allow.allowance,
expires: allow.expires,
})
})
.collect();
Ok(AllAllowancesResponse {
allowances: allowances?,
})
.collect::<StdResult<_>>()?;
Ok(AllAllowancesResponse { allowances })
}

pub fn query_all_accounts(
Expand All @@ -44,15 +41,13 @@ pub fn query_all_accounts(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);

let accounts: Result<Vec<_>, _> = BALANCES
let accounts = BALANCES
.keys(deps.storage, start, None, Order::Ascending)
.map(String::from_utf8)
.take(limit)
.collect();
.map(|item| item.map(Into::into))
.collect::<StdResult<_>>()?;

Ok(AllAccountsResponse {
accounts: accounts?,
})
Ok(AllAccountsResponse { accounts })
}

#[cfg(test)]
Expand Down
3 changes: 1 addition & 2 deletions contracts/cw20-escrow/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{Addr, Coin, Env, Order, StdError, StdResult, Storage, Timestamp};
use cosmwasm_std::{Addr, Coin, Env, Order, StdResult, Storage, Timestamp};
use cw_storage_plus::Map;

use cw20::{Balance, Cw20CoinVerified};
Expand Down Expand Up @@ -96,7 +96,6 @@ pub const ESCROWS: Map<&str, Escrow> = Map::new("escrow");
pub fn all_escrow_ids(storage: &dyn Storage) -> StdResult<Vec<String>> {
ESCROWS
.keys(storage, None, None, Order::Ascending)
.map(|k| String::from_utf8(k).map_err(|_| StdError::invalid_utf8("parsing escrow key")))
.collect()
}

Expand Down
26 changes: 12 additions & 14 deletions contracts/cw20-ics20/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,30 @@ fn query_port(deps: Deps) -> StdResult<PortResponse> {
}

fn query_list(deps: Deps) -> StdResult<ListChannelsResponse> {
let channels: StdResult<Vec<_>> = CHANNEL_INFO
.range(deps.storage, None, None, Order::Ascending)
let channels = CHANNEL_INFO
.range_raw(deps.storage, None, None, Order::Ascending)
.map(|r| r.map(|(_, v)| v))
.collect();
Ok(ListChannelsResponse {
channels: channels?,
})
.collect::<StdResult<_>>()?;
Ok(ListChannelsResponse { channels })
}

// make public for ibc tests
pub fn query_channel(deps: Deps, id: String) -> StdResult<ChannelResponse> {
let info = CHANNEL_INFO.load(deps.storage, &id)?;
// this returns Vec<(outstanding, total)>
let state: StdResult<Vec<_>> = CHANNEL_STATE
let state = CHANNEL_STATE
.prefix(&id)
.range(deps.storage, None, None, Order::Ascending)
.map(|r| {
let (k, v) = r?;
let denom = String::from_utf8(k)?;
let outstanding = Amount::from_parts(denom.clone(), v.outstanding);
let total = Amount::from_parts(denom, v.total_sent);
Ok((outstanding, total))
r.map(|(denom, v)| {
let outstanding = Amount::from_parts(denom.clone(), v.outstanding);
let total = Amount::from_parts(denom, v.total_sent);
(outstanding, total)
})
})
.collect();
.collect::<StdResult<Vec<_>>>()?;
// we want (Vec<outstanding>, Vec<total>)
let (balances, total_sent) = state?.into_iter().unzip();
let (balances, total_sent) = state.into_iter().unzip();

Ok(ChannelResponse {
info,
Expand Down
61 changes: 29 additions & 32 deletions contracts/cw3-fixed-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use utils::Expiration;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{
next_id, parse_id, Ballot, Config, Proposal, BALLOTS, CONFIG, PROPOSALS, VOTERS,
};
use crate::state::{next_id, Ballot, Config, Proposal, BALLOTS, CONFIG, PROPOSALS, VOTERS};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw3-fixed-multisig";
Expand Down Expand Up @@ -315,13 +313,13 @@ fn list_proposals(

let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive_int);
let props: StdResult<Vec<_>> = PROPOSALS
let proposals = PROPOSALS
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|p| map_proposal(&env.block, &threshold, p))
.collect();
.collect::<StdResult<_>>()?;

Ok(ProposalListResponse { proposals: props? })
Ok(ProposalListResponse { proposals })
}

fn reverse_proposals(
Expand All @@ -338,30 +336,31 @@ fn reverse_proposals(

let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let end = start_before.map(Bound::exclusive_int);
let props: StdResult<Vec<_>> = PROPOSALS
let proposals = PROPOSALS
.range(deps.storage, None, end, Order::Descending)
.take(limit)
.map(|p| map_proposal(&env.block, &threshold, p))
.collect();
.collect::<StdResult<_>>()?;

Ok(ProposalListResponse { proposals: props? })
Ok(ProposalListResponse { proposals })
}

fn map_proposal(
block: &BlockInfo,
threshold: &ThresholdResponse,
item: StdResult<(Vec<u8>, Proposal)>,
item: StdResult<(u64, Proposal)>,
) -> StdResult<ProposalResponse> {
let (key, prop) = item?;
let status = prop.current_status(block);
Ok(ProposalResponse {
id: parse_id(&key)?,
title: prop.title,
description: prop.description,
msgs: prop.msgs,
status,
expires: prop.expires,
threshold: threshold.clone(),
item.map(|(id, prop)| {
let status = prop.current_status(block);
ProposalResponse {
id,
title: prop.title,
description: prop.description,
msgs: prop.msgs,
status,
expires: prop.expires,
threshold: threshold.clone(),
}
})
}

Expand All @@ -385,21 +384,20 @@ fn list_votes(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);

let votes: StdResult<Vec<_>> = BALLOTS
let votes = BALLOTS
.prefix(proposal_id)
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
let (key, ballot) = item?;
Ok(VoteInfo {
voter: String::from_utf8(key)?,
item.map(|(addr, ballot)| VoteInfo {
voter: addr.into(),
vote: ballot.vote,
weight: ballot.weight,
})
})
.collect();
.collect::<StdResult<_>>()?;

Ok(VoteListResponse { votes: votes? })
Ok(VoteListResponse { votes })
}

fn query_voter(deps: Deps, voter: String) -> StdResult<VoterResponse> {
Expand All @@ -416,19 +414,18 @@ fn list_voters(
let limit = limit.unwrap_or(DEFAULT_LIMIT).min(MAX_LIMIT) as usize;
let start = start_after.map(Bound::exclusive);

let voters: StdResult<Vec<_>> = VOTERS
let voters = VOTERS
.range(deps.storage, start, None, Order::Ascending)
.take(limit)
.map(|item| {
let (key, weight) = item?;
Ok(VoterDetail {
addr: String::from_utf8(key)?,
item.map(|(addr, weight)| VoterDetail {
addr: addr.into(),
weight,
})
})
.collect();
.collect::<StdResult<_>>()?;

Ok(VoterListResponse { voters: voters? })
Ok(VoterListResponse { voters })
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 90c49f4

Please sign in to comment.