diff --git a/Cargo.lock b/Cargo.lock index 27fb335..51e2b22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -932,6 +932,7 @@ dependencies = [ "cosmwasm-schema 1.5.0", "cosmwasm-std 1.1.9", "secret-storage-plus", + "secret-toolkit", "thiserror", ] @@ -4246,6 +4247,7 @@ dependencies = [ "schemars", "secret-cw-controllers", "secret-cw2", + "secret-storage-plus", "secret-toolkit", "secret-utils", "serde", diff --git a/contracts/staking/cw20-stake/src/contract.rs b/contracts/staking/cw20-stake/src/contract.rs index 444985e..7877ca8 100644 --- a/contracts/staking/cw20-stake/src/contract.rs +++ b/contracts/staking/cw20-stake/src/contract.rs @@ -1,5 +1,5 @@ use crate::math; -use crate::msg::{ExecuteAnswer, Snip20ReceiveMsg}; +use crate::msg::{ExecuteAnswer, Snip20ReceiveMsg, ListStakersResponse, StakerBalanceResponse, GetHooksResponse, TotalValueResponse, StakedValueResponse, QueryMsg}; use crate::msg::{ ExecuteMsg, InstantiateMsg , ReceiveMsg, ResponseStatus::Success, @@ -10,12 +10,13 @@ use crate::state::{ use crate::ContractError; use cosmwasm_std::{ entry_point, from_binary, to_binary, Addr, DepsMut, Empty, Env, MessageInfo, - Response, StdError, StdResult, Uint128, + Response, StdError, StdResult, Uint128, Deps, Binary, }; use dao_hooks::stake::{stake_hook_msgs, unstake_hook_msgs}; use dao_voting::duration::validate_duration; use secret_cw2::set_contract_version; +use secret_cw_controllers::ClaimsResponse; pub use secret_toolkit::snip20::handle::{ burn_from_msg, burn_msg, decrease_allowance_msg, increase_allowance_msg, mint_msg, send_from_msg, send_msg, transfer_from_msg, transfer_msg, @@ -32,7 +33,7 @@ pub(crate) const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); #[entry_point] pub fn instantiate( deps: DepsMut, - env: Env, + _env: Env, _info: MessageInfo, msg: InstantiateMsg, ) -> Result, ContractError> { @@ -43,14 +44,14 @@ pub fn instantiate( // though this provides some protection against mistakes where the // wrong address is provided. let token_address = deps.api.addr_validate(&msg.token_address)?; - let _: secret_toolkit::snip20::TokenInfoResponse = deps - .querier - .query_wasm_smart( - env.contract.code_hash, - &token_address, - &secret_toolkit::snip20::QueryMsg::TokenInfo {}, - ) - .map_err(|_| ContractError::InvalidCw20 {})?; + // let _: secret_toolkit::snip20::TokenInfoResponse = deps + // .querier + // .query_wasm_smart( + // env.contract.code_hash, + // &token_address, + // &secret_toolkit::snip20::QueryMsg::TokenInfo {}, + // ) + // .map_err(|_| ContractError::InvalidCw20 {})?; validate_duration(msg.unstaking_duration)?; let config = Config { @@ -189,7 +190,7 @@ pub fn execute_unstake( if staked_total.is_zero() { return Err(ContractError::NothingStaked {}); } - if amount.saturating_add(balance) == Uint128::MAX { + if amount.checked_add(balance).unwrap() == Uint128::MAX { return Err(ContractError::Cw20InvaraintViolation {}); } if amount > staked_total { @@ -377,169 +378,168 @@ pub fn try_set_key( })?), ) } -// #[entry_point] -// pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { -// match msg { -// QueryMsg::GetConfig { key, address } => to_binary(&query_config(deps, address, key)?), -// #[cfg(feature = "iterator")] -// QueryMsg::StakedBalanceAtHeight { -// key, -// address, -// height, -// } => to_binary(&query_staked_balance_at_height( -// deps, env, key, address, height, -// )?), -// #[cfg(feature = "iterator")] -// QueryMsg::TotalStakedAtHeight { -// key, -// address, -// height, -// } => to_binary(&query_total_staked_at_height( -// deps, env, key, address, height, -// )?), -// QueryMsg::StakedValue { key, address } => { -// to_binary(&query_staked_value(deps, env, key, address)?) -// } -// QueryMsg::TotalValue { key, address } => { -// to_binary(&query_total_value(deps, env, address, key)?) -// } -// QueryMsg::Claims { key, address } => to_binary(&query_claims(deps, key, address)?), -// QueryMsg::GetHooks { key, address } => to_binary(&query_hooks(deps, address, key)?), -// QueryMsg::ListStakers { -// key, -// address, -// } => query_list_stakers(deps, key, address), -// QueryMsg::Ownership {} => to_binary(&cw_ownable::get_ownership(deps.storage)?), -// } -// } +#[entry_point] +pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { + match msg { + QueryMsg::GetConfig { key, address } => to_binary(&query_config(deps, address, key)?), + #[cfg(feature = "iterator")] + QueryMsg::StakedBalanceAtHeight { + key, + address, + height, + } => to_binary(&query_staked_balance_at_height( + deps, env, key, address, height, + )?), + #[cfg(feature = "iterator")] + QueryMsg::TotalStakedAtHeight { + key, + address, + height, + } => to_binary(&query_total_staked_at_height( + deps, env, key, address, height, + )?), + QueryMsg::StakedValue { key, address } => { + to_binary(&query_staked_value(deps, env, key, address)?) + } + QueryMsg::TotalValue { key, address } => { + to_binary(&query_total_value(deps, env, address, key)?) + } + QueryMsg::Claims { key, address } => to_binary(&query_claims(deps, key, address)?), + QueryMsg::GetHooks { key, address } => to_binary(&query_hooks(deps, address, key)?), + QueryMsg::ListStakers { + key, + address, + } => query_list_stakers(deps, key, address), + QueryMsg::Ownership {} => to_binary(&cw_ownable::get_ownership(deps.storage)?), + } +} -// #[cfg(feature = "iterator")] -// pub fn query_staked_balance_at_height( -// deps: Deps, -// env: Env, -// key: String, -// address: String, -// height: Option, -// ) -> StdResult { -// authenticate(deps, deps.api.addr_validate(&address)?, key)?; - -// let address = deps.api.addr_validate(&address)?; -// let height = height.unwrap_or(env.block.height); -// let balance = STAKED_BALANCES -// .may_load(deps.storage, &address)? -// .unwrap_or_default(); -// Ok(StakedBalanceAtHeightResponse { balance, height }) -// } +#[cfg(feature = "iterator")] +pub fn query_staked_balance_at_height( + deps: Deps, + env: Env, + key: String, + address: String, + height: Option, +) -> StdResult { + authenticate(deps, deps.api.addr_validate(&address)?, key)?; + + let address = deps.api.addr_validate(&address)?; + let height = height.unwrap_or(env.block.height); + let balance = STAKED_BALANCES + .may_load(deps.storage, &address)? + .unwrap_or_default(); + Ok(StakedBalanceAtHeightResponse { balance, height }) +} -// #[cfg(feature = "iterator")] -// pub fn query_total_staked_at_height( -// deps: Deps, -// _env: Env, -// key: String, -// address: String, -// height: Option, -// ) -> StdResult { -// authenticate(deps, deps.api.addr_validate(&address)?, key)?; - -// let height = height.unwrap_or(_env.block.height); -// let total = STAKED_TOTAL -// .may_load_at_height(deps.storage, height)? -// .unwrap_or_default(); -// Ok(TotalStakedAtHeightResponse { total, height }) -// } +#[cfg(feature = "iterator")] +pub fn query_total_staked_at_height( + deps: Deps, + _env: Env, + key: String, + address: String, + height: Option, +) -> StdResult { + authenticate(deps, deps.api.addr_validate(&address)?, key)?; + + let height = height.unwrap_or(_env.block.height); + let total = STAKED_TOTAL + .may_load_at_height(deps.storage, height)? + .unwrap_or_default(); + Ok(TotalStakedAtHeightResponse { total, height }) +} -// pub fn query_staked_value( -// deps: Deps, -// _env: Env, -// key: String, -// address: String, -// ) -> StdResult { -// authenticate(deps, deps.api.addr_validate(&address)?, key)?; - -// let address = deps.api.addr_validate(&address)?; -// let balance = BALANCE.load(deps.storage).unwrap_or_default(); -// let staked = STAKED_BALANCES -// .get(deps.storage, &address) -// .unwrap_or_default(); -// let total = STAKED_TOTAL.load(deps.storage)?; -// if balance == Uint128::zero() || staked == Uint128::zero() || total == Uint128::zero() { -// Ok(StakedValueResponse { -// value: Uint128::zero(), -// }) -// } else { -// let value = staked -// .checked_mul(balance) -// .map_err(StdError::overflow)? -// .checked_div(total) -// .map_err(StdError::divide_by_zero)?; -// Ok(StakedValueResponse { value }) -// } -// } +pub fn query_staked_value( + deps: Deps, + _env: Env, + key: String, + address: String, +) -> StdResult { + authenticate(deps, deps.api.addr_validate(&address)?, key)?; + + let address = deps.api.addr_validate(&address)?; + let balance = BALANCE.load(deps.storage).unwrap_or_default(); + let staked = STAKED_BALANCES + .get(deps.storage, &address).unwrap_or_default(); + let total = STAKED_TOTAL.load(deps.storage).unwrap_or_default(); + if balance == Uint128::zero() || staked == Uint128::zero() || total == Uint128::zero() { + Ok(StakedValueResponse { + value: Uint128::zero(), + }) + } else { + let value = staked + .checked_mul(balance) + .map_err(StdError::overflow)? + .checked_div(total) + .map_err(StdError::divide_by_zero)?; + Ok(StakedValueResponse { value }) + } +} -// pub fn query_total_value( -// deps: Deps, -// _env: Env, -// address: String, -// key: String, -// ) -> StdResult { -// authenticate(deps, deps.api.addr_validate(&address)?, key)?; +pub fn query_total_value( + deps: Deps, + _env: Env, + address: String, + key: String, +) -> StdResult { + authenticate(deps, deps.api.addr_validate(&address)?, key)?; -// let balance = BALANCE.load(deps.storage)?; -// Ok(TotalValueResponse { total: balance }) -// } + let balance = BALANCE.load(deps.storage)?; + Ok(TotalValueResponse { total: balance }) +} -// pub fn query_config(deps: Deps, address: String, key: String) -> StdResult { -// authenticate(deps, deps.api.addr_validate(&address)?, key)?; +pub fn query_config(deps: Deps, address: String, key: String) -> StdResult { + authenticate(deps, deps.api.addr_validate(&address)?, key)?; -// let config = CONFIG.load(deps.storage)?; -// Ok(config) -// } + let config = CONFIG.load(deps.storage)?; + Ok(config) +} -// pub fn query_claims(deps: Deps, key: String, address: String) -> StdResult { -// authenticate(deps, deps.api.addr_validate(&address)?, key)?; +pub fn query_claims(deps: Deps, key: String, address: String) -> StdResult { + authenticate(deps, deps.api.addr_validate(&address)?, key)?; -// CLAIMS.query_claims(deps, &deps.api.addr_validate(&address)?) -// } + CLAIMS.query_claims(deps, &deps.api.addr_validate(&address)?) +} -// pub fn query_hooks(deps: Deps, address: String, key: String) -> StdResult { -// authenticate(deps, deps.api.addr_validate(&address)?, key)?; +pub fn query_hooks(deps: Deps, address: String, key: String) -> StdResult { + authenticate(deps, deps.api.addr_validate(&address)?, key)?; -// Ok(GetHooksResponse { -// hooks: HOOKS.query_hooks(deps)?.hooks, -// }) -// } + Ok(GetHooksResponse { + hooks: HOOKS.query_hooks(deps)?.hooks, + }) +} -// pub fn query_list_stakers( -// deps: Deps, -// key: String, -// address: String, -// ) -> StdResult { -// authenticate(deps, deps.api.addr_validate(&address)?, key)?; -// // let start_at = start_after -// // .map(|addr| deps.api.addr_validate(&addr)) -// // .transpose()?; -// let stakers = cw_paginate_storage::paginate_map( -// deps, -// &STAKED_BALANCES, -// 0, -// STAKED_BALANCES.get_len(deps.storage).unwrap_or_default(), -// )?; - -// let stakers = stakers -// .into_iter() -// .map(|(address, balance)| StakerBalanceResponse { -// address: address.into_string(), -// balance, -// }) -// .collect(); - -// to_binary(&ListStakersResponse { stakers }) -// } +pub fn query_list_stakers( + deps: Deps, + key: String, + address: String, +) -> StdResult { + authenticate(deps, deps.api.addr_validate(&address)?, key)?; + // let start_at = start_after + // .map(|addr| deps.api.addr_validate(&addr)) + // .transpose()?; + let stakers = cw_paginate_storage::paginate_map( + deps, + &STAKED_BALANCES, + 0, + STAKED_BALANCES.get_len(deps.storage).unwrap_or_default(), + )?; -// // Helper Functions -// fn authenticate(deps: Deps, addr: Addr, key: String) -> StdResult<()> { -// ViewingKey::check(deps.storage, addr.as_ref(), &key) -// } + let stakers = stakers + .into_iter() + .map(|(address, balance)| StakerBalanceResponse { + address: address.into_string(), + balance, + }) + .collect(); + + to_binary(&ListStakersResponse { stakers }) +} + +// Helper Functions +fn authenticate(deps: Deps, addr: Addr, key: String) -> StdResult<()> { + ViewingKey::check(deps.storage, addr.as_ref(), &key) +} // #[entry_point] // pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result { diff --git a/contracts/staking/cw20-stake/src/msg.rs b/contracts/staking/cw20-stake/src/msg.rs index 64fee0c..ae456fa 100644 --- a/contracts/staking/cw20-stake/src/msg.rs +++ b/contracts/staking/cw20-stake/src/msg.rs @@ -68,20 +68,13 @@ pub enum ReceiveMsg { #[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] #[derive(QueryResponses)] pub enum QueryMsg { - #[cfg(feature = "iterator")] - #[returns(StakedBalanceAtHeightResponse)] - StakedBalanceAtHeight { - key: String, - address: String, - height: Option, - }, - #[cfg(feature = "iterator")] - #[returns(TotalStakedAtHeightResponse)] - TotalStakedAtHeight { - key: String, - address: String, - height: Option, - }, + // #[returns(StakedBalanceAtHeightResponse)] + // StakedBalanceAtHeight { + // address: String, + // height: Option, + // }, + // #[returns(TotalStakedAtHeightResponse)] + // TotalStakedAtHeight { height: Option }, #[returns(StakedValueResponse)] StakedValue { key: String, address: String }, #[returns(TotalValueResponse)] diff --git a/contracts/staking/cw20-stake/src/state.rs b/contracts/staking/cw20-stake/src/state.rs index fbb0141..0feff27 100644 --- a/contracts/staking/cw20-stake/src/state.rs +++ b/contracts/staking/cw20-stake/src/state.rs @@ -1,10 +1,11 @@ -use cosmwasm_std::{Addr, Uint128}; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; use cw_hooks::Hooks; -use secret_cw_controllers::Claims; -use secret_toolkit::storage::{Item, Keymap}; +use cosmwasm_std::{Addr,Uint128}; +use secret_storage_plus::Item; use secret_utils::Duration; -use serde::{Deserialize, Serialize}; -use schemars::JsonSchema; +use secret_toolkit::storage::Keymap; +use secret_cw_controllers::Claims; #[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] pub struct Config { @@ -12,19 +13,17 @@ pub struct Config { pub unstaking_duration: Option, } -// `"config"` key stores v1 configuration. -pub const CONFIG: Item = Item::new(b"config_v2"); +pub const CONFIG: Item = Item::new("config_v2"); +pub const STAKED_TOTAL: Item = Item::new("total_staked"); +pub const BALANCE: Item = Item::new("balance"); pub const STAKED_BALANCES: Keymap = Keymap::new(b"staked_balances"); -pub const STAKED_TOTAL: Item = Item::new(b"total_staked"); + +// Hooks to contracts that will receive staking and unstaking messages +pub const HOOKS: Hooks = Hooks::new("hooks"); /// The maximum number of claims that may be outstanding. pub const MAX_CLAIMS: u64 = 100; -pub const CLAIMS: Claims = Claims::new("claims"); - -pub const BALANCE: Item = Item::new(b"balance"); - -// Hooks to contracts that will receive staking and unstaking messages -pub const HOOKS: Hooks = Hooks::new("hooks"); +pub const CLAIMS: Claims = Claims::new("claims"); \ No newline at end of file diff --git a/dummy/Cargo.toml b/dummy/Cargo.toml index e2cbbaa..b520dbb 100644 --- a/dummy/Cargo.toml +++ b/dummy/Cargo.toml @@ -42,6 +42,7 @@ secret-cw2={ workspace = true } cw-hooks ={ workspace = true } dao-hooks ={ workspace=true } secret-cw-controllers ={ workspace=true } +secret-storage-plus ={ workspace=true } # [patch.crates-io] # uncomment this if you use cw-storage-plus # cosmwasm-std = { git = "https://github.com/scrtlabs/cosmwasm", tag = "v1.1.9-secret" } diff --git a/dummy/src/contract.rs b/dummy/src/contract.rs index 4249696..d62e098 100644 --- a/dummy/src/contract.rs +++ b/dummy/src/contract.rs @@ -5,7 +5,7 @@ use cosmwasm_std::{ }; use dao_voting::duration::validate_duration; use crate::msg::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg,ReceiveMsg,Snip20ReceiveMsg}; -use crate::state::{Config,BALANCE, CONFIG, STAKED_TOTAL,STAKED_BALANCES,HOOKS,CLAIMS,MAX_CLAIMS}; +use crate::state::{Config,BALANCE, CONFIG, STAKED_TOTAL,STAKED_BALANCES,MAX_CLAIMS,HOOKS,CLAIMS}; use secret_cw2::set_contract_version; use crate::error::ContractError; use dao_hooks::stake::{stake_hook_msgs, unstake_hook_msgs}; @@ -141,21 +141,20 @@ pub fn execute_unstake( if staked_total.is_zero() { return Err(ContractError::NothingStaked {}); } - if amount.saturating_add(balance) == Uint128::MAX { + if amount.checked_add(balance).unwrap() == Uint128::MAX { return Err(ContractError::Cw20InvaraintViolation {}); } if amount > staked_total { return Err(ContractError::ImpossibleUnstake {}); } let amount_to_claim = math::amount_to_claim(staked_total, balance, amount); - let prev_balance = STAKED_BALANCES.get(deps.storage, &info.sender); - STAKED_BALANCES.insert( + let mut prev_balance = STAKED_BALANCES.load(deps.storage, info.sender.clone())?; + prev_balance=prev_balance-amount; + + STAKED_BALANCES.save( deps.storage, - &info.sender, + info.sender.clone(), &prev_balance - .unwrap_or_default() - .checked_sub(amount) - .unwrap(), )?; STAKED_TOTAL.update(deps.storage, |total| -> StdResult { // Initialized during instantiate - OK to unwrap. @@ -218,15 +217,15 @@ pub fn execute_unstake( } } -pub fn execute_update_owner( - deps: DepsMut, - info: MessageInfo, - env: Env, - action: cw_ownable::Action, -) -> Result { - let ownership = cw_ownable::update_ownership(deps, &env.block, &info.sender, action)?; - Ok(Response::default().add_attributes(ownership.into_attributes())) -} +// pub fn execute_update_owner( +// deps: DepsMut, +// info: MessageInfo, +// env: Env, +// action: cw_ownable::Action, +// ) -> Result { +// let ownership = cw_ownable::update_ownership(deps, &env.block, &info.sender, action)?; +// Ok(Response::default().add_attributes(ownership.into_attributes())) +// } #[entry_point] diff --git a/dummy/src/state.rs b/dummy/src/state.rs index f465764..4c64ddd 100644 --- a/dummy/src/state.rs +++ b/dummy/src/state.rs @@ -2,7 +2,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use cw_hooks::Hooks; use cosmwasm_std::{Addr, Storage,Uint128}; -use secret_toolkit::storage::{Item,Keymap}; +use secret_storage_plus::{Item,Map}; use secret_utils::Duration; use secret_cw_controllers::Claims; @@ -13,10 +13,10 @@ pub struct Config { } -pub const CONFIG: Item = Item::new(b"config_v2"); -pub const STAKED_TOTAL: Item = Item::new(b"total_staked"); -pub const BALANCE: Item = Item::new(b"balance"); -pub const STAKED_BALANCES: Keymap = Keymap::new(b"staked_balances"); +pub const CONFIG: Item = Item::new("config_v2"); +pub const STAKED_TOTAL: Item = Item::new("total_staked"); +pub const BALANCE: Item = Item::new("balance"); +pub const STAKED_BALANCES: Map = Map::new("staked_balances"); // Hooks to contracts that will receive staking and unstaking messages diff --git a/packages/cw-hooks/Cargo.toml b/packages/cw-hooks/Cargo.toml index 5a8e097..3a86add 100644 --- a/packages/cw-hooks/Cargo.toml +++ b/packages/cw-hooks/Cargo.toml @@ -12,4 +12,5 @@ thiserror = { workspace = true } cosmwasm-schema = { workspace = true } # cw-storage-plus = { workspace = true } cosmwasm-std ={ workspace = true } -secret-storage-plus = { workspace = true } \ No newline at end of file +secret-storage-plus = { workspace = true } +secret-toolkit ={ workspace = true } \ No newline at end of file diff --git a/packages/cw-paginate-storage/Cargo.toml b/packages/cw-paginate-storage/Cargo.toml index c344ab2..2e1689b 100644 --- a/packages/cw-paginate-storage/Cargo.toml +++ b/packages/cw-paginate-storage/Cargo.toml @@ -8,7 +8,7 @@ repository = { workspace = true } version = { workspace = true } [dependencies] -cosmwasm-std ={ workspace = true } +cosmwasm-std ={ workspace = true ,default-features = false} # cosmwasm-storage = { workspace = true } secret-storage-plus = { workspace = true, default-features = false} secret-toolkit = { workspace = true, default-features = false} diff --git a/packages/cw-paginate-storage/src/lib.rs b/packages/cw-paginate-storage/src/lib.rs index 047471e..bc89350 100644 --- a/packages/cw-paginate-storage/src/lib.rs +++ b/packages/cw-paginate-storage/src/lib.rs @@ -1,12 +1,44 @@ #![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))] -use cosmwasm_std::{Deps, StdResult}; +use cosmwasm_std::{Deps, StdResult,StdError}; use secret_toolkit::storage::{ keymap::{KeyItemIter, KeyIter}, Keymap, }; use serde::{de::DeserializeOwned, Serialize}; +/// A record of a key-value storage that is created through an iterator API. +/// The first element (key) is always raw binary data. The second element +/// (value) is binary by default but can be changed to a custom type. This +/// allows contracts to reuse the type when deserializing database records. +pub type Record> = (Vec, V); + +#[derive(Copy, Clone)] +// We assign these to integers to provide a stable API for passing over FFI (to wasm and Go) +pub enum Order { + Ascending = 1, + Descending = 2, +} + +impl TryFrom for Order { + type Error = StdError; + + fn try_from(value: i32) -> Result { + match value { + 1 => Ok(Order::Ascending), + 2 => Ok(Order::Descending), + _ => Err(StdError::generic_err("Order must be 1 or 2")), + } + } +} + +impl From for i32 { + fn from(original: Order) -> i32 { + original as _ + } +} + + /// Generic function for paginating a list of (K, V) pairs in a /// CosmWasm Map. pub fn paginate_map<'a, 'b, K, V>( @@ -22,6 +54,8 @@ where let items = KeyItemIter::new(map, deps.storage, start, end) .flatten() .collect(); + + Ok(items) } @@ -142,7 +176,7 @@ mod tests { let items = paginate_map(deps.as_ref(), &map, 1, 1).unwrap(); assert_eq!(items, vec![("2".to_string(), "4".to_string())]); - let items = paginate_map(deps.as_ref(), &map, None, Some(1), Order::Ascending).unwrap(); + let items = paginate_map(deps.as_ref(), &map, None, Some(1)).unwrap(); assert_eq!(items, vec![("1".to_string(), "2".to_string())]); } @@ -156,10 +190,10 @@ mod tests { .unwrap(); } - let items = paginate_map_keys(deps.as_ref(), &map, None, None, Order::Descending).unwrap(); + let items = paginate_map_keys(deps.as_ref(), &map, None, None).unwrap(); assert_eq!(items, vec!["2".to_string(), "1".to_string()]); - let items = paginate_map_keys(deps.as_ref(), &map, None, None, Order::Ascending).unwrap(); + let items = paginate_map_keys(deps.as_ref(), &map, None, None).unwrap(); assert_eq!(items, vec!["1".to_string(), "2".to_string()]); let items = paginate_map_keys( @@ -167,13 +201,13 @@ mod tests { &map, Some("1".to_string()), None, - Order::Ascending, + ) .unwrap(); assert_eq!(items, vec!["2"]); let items = - paginate_map_keys(deps.as_ref(), &map, None, Some(1), Order::Ascending).unwrap(); + paginate_map_keys(deps.as_ref(), &map, None, Some(1), ).unwrap(); assert_eq!(items, vec!["1".to_string()]); } @@ -188,18 +222,18 @@ mod tests { .unwrap(); } - let items = paginate_map_keys(deps.as_ref(), &map, None, None, Order::Descending).unwrap(); + let items = paginate_map_keys(deps.as_ref(), &map, None, None, ).unwrap(); assert_eq!(items, vec![10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); - let items = paginate_map_keys(deps.as_ref(), &map, None, None, Order::Ascending).unwrap(); + let items = paginate_map_keys(deps.as_ref(), &map, None, None, ).unwrap(); assert_eq!(items, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); let items = - paginate_map_keys(deps.as_ref(), &map, Some(3), Some(3), Order::Ascending).unwrap(); + paginate_map_keys(deps.as_ref(), &map, Some(3), Some(3), ).unwrap(); assert_eq!(items, vec![4, 5, 6]); let items = - paginate_map_keys(deps.as_ref(), &map, Some(7), Some(4), Order::Descending).unwrap(); + paginate_map_keys(deps.as_ref(), &map, Some(7), Some(4)).unwrap(); assert_eq!(items, vec![6, 5, 4, 3]); } @@ -375,13 +409,13 @@ mod tests { map.save(&mut deps.storage, 4, &66).unwrap(); map.save(&mut deps.storage, 5, &0).unwrap(); - let items = paginate_map(deps.as_ref(), &map, None, None, Order::Descending).unwrap(); + let items = paginate_map(deps.as_ref(), &map, None, None,).unwrap(); assert_eq!(items, vec![(5, 0), (4, 66), (3, 77), (2, 22), (1, 40)]); - let items = paginate_map(deps.as_ref(), &map, Some(3), None, Order::Descending).unwrap(); + let items = paginate_map(deps.as_ref(), &map, Some(3), None, ).unwrap(); assert_eq!(items, vec![(2, 22), (1, 40)]); - let items = paginate_map(deps.as_ref(), &map, Some(1), None, Order::Descending).unwrap(); + let items = paginate_map(deps.as_ref(), &map, Some(1), None,).unwrap(); assert_eq!(items, vec![]); } @@ -424,13 +458,13 @@ mod tests { ) .unwrap(); - let items = paginate_map_keys(deps.as_ref(), &map, None, None, Order::Descending).unwrap(); + let items = paginate_map_keys(deps.as_ref(), &map, None, None, ).unwrap(); assert_eq!(items[1], Addr::unchecked(format!("test_addr{:0>3}", 4))); assert_eq!(items[4], Addr::unchecked(format!("test_addr{:0>3}", 1))); let addr: Addr = Addr::unchecked(format!("test_addr{:0>3}", 3)); let items = - paginate_map_keys(deps.as_ref(), &map, Some(&addr), None, Order::Ascending).unwrap(); + paginate_map_keys(deps.as_ref(), &map, Some(&addr), None, ).unwrap(); assert_eq!(items[0], Addr::unchecked(format!("test_addr{:0>3}", 4))); } @@ -473,7 +507,7 @@ mod tests { ) .unwrap(); - let items = paginate_map(deps.as_ref(), &map, None, None, Order::Descending).unwrap(); + let items = paginate_map(deps.as_ref(), &map, None, None, ).unwrap(); assert_eq!( items[1], (Addr::unchecked(format!("test_addr{:0>3}", 4)), 66) @@ -485,7 +519,7 @@ mod tests { let addr: Addr = Addr::unchecked(format!("test_addr{:0>3}", 3)); let items = - paginate_map(deps.as_ref(), &map, Some(&addr), Some(2), Order::Ascending).unwrap(); + paginate_map(deps.as_ref(), &map, Some(&addr), Some(2), ).unwrap(); let test_vec: Vec<(Addr, u32)> = vec![ (Addr::unchecked(format!("test_addr{:0>3}", 4)), 66), (Addr::unchecked(format!("test_addr{:0>3}", 6)), 0), diff --git a/packages/storage-plus/src/keys.rs b/packages/storage-plus/src/keys.rs index 9e55bfd..195aff7 100644 --- a/packages/storage-plus/src/keys.rs +++ b/packages/storage-plus/src/keys.rs @@ -180,7 +180,7 @@ impl<'a> Prefixer<'a> for &'a [u8] { impl<'a, T: Prefixer<'a>, U: Prefixer<'a>> Prefixer<'a> for (T, U) { fn prefix(&self) -> Vec { let mut res = self.0.prefix(); - res.extend(self.1.prefix().into_iter()); + res.extend(self.1.prefix()); res } } @@ -188,8 +188,8 @@ impl<'a, T: Prefixer<'a>, U: Prefixer<'a>> Prefixer<'a> for (T, U) { impl<'a, T: Prefixer<'a>, U: Prefixer<'a>, V: Prefixer<'a>> Prefixer<'a> for (T, U, V) { fn prefix(&self) -> Vec { let mut res = self.0.prefix(); - res.extend(self.1.prefix().into_iter()); - res.extend(self.2.prefix().into_iter()); + res.extend(self.1.prefix()); + res.extend(self.2.prefix()); res } }