8000 Albert/make open auction an option and add a query by cowboy0015 · Pull Request #364 · andromedaprotocol/andromeda-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Albert/make open auction an option and add a query #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
36 changes: 33 additions & 3 deletions contracts/non-fungible-tokens/andromeda-auction/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::state::{
auction_infos, read_auction_infos, read_bids, BIDS, NEXT_AUCTION_ID, TOKEN_AUCTION_STATE,
};
use andromeda_non_fungible_tokens::auction::{
AuctionIdsResponse, AuctionInfo, AuctionStateResponse, Bid, BidsResponse, Cw721HookMsg,
ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg, TokenAuctionState,
AuctionIdsResponse, AuctionInfo, AuctionStateResponse, AuthorizedAddressesResponse, Bid,
BidsResponse, Cw721HookMsg, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg,
TokenAuctionState,
};
use andromeda_std::{
ado_base::{
Expand Down Expand Up @@ -64,6 +65,10 @@ pub fn instantiate(
contract.register_modules(info.sender.as_str(), deps.storage, msg.modules)?;

if let Some(authorized_token_addresses) = msg.authorized_token_addresses {
if !authorized_token_addresses.is_empty() {
ADOContract::default().permission_action(SEND_NFT_ACTION, deps.storage)?;
}

for token_address in authorized_token_addresses {
let addr = token_address.get_raw_address(&deps.as_ref())?;
ADOContract::set_permission(
Expand Down Expand Up @@ -171,7 +176,7 @@ fn handle_receive_cw721(
ctx: ExecuteContext,
msg: Cw721ReceiveMsg,
) -> Result<Response, ContractError> {
ADOContract::default().is_permissioned_strict(
ADOContract::default().is_permissioned(
ctx.deps.storage,
ctx.env.clone(),
SEND_NFT_ACTION,
Expand Down Expand Up @@ -720,6 +725,16 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractErro
token_id,
token_address,
} => encode_binary(&query_is_closed(deps, env, token_id, token_address)?),
QueryMsg::AuthorizedAddresses {
start_after,
limit,
order_by,
} => encode_binary(&query_authorized_addresses(
deps,
start_after,
limit,
order_by,
)?),
_ => ADOContract::default().query(deps, env, msg),
}
}
10000 Expand Down Expand Up @@ -852,6 +867,21 @@ fn query_owner_of(
Ok(res)
}

fn query_authorized_addresses(
deps: Deps,
start_after: Option<String>,
limit: Option<u32>,
order_by: Option<OrderBy>,
) -> Result<AuthorizedAddressesResponse, ContractError> {
let addresses = ADOContract::default().query_permissioned_actors(
deps,
SEND_NFT_ACTION,
start_after,
limit,
order_by,
)?;
Ok(AuthorizedAddressesResponse { addresses })
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
// New version
Expand Down
4 changes: 1 addition & 3 deletions contracts/non-fungible-tokens/andromeda-auction/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use andromeda_non_fungible_tokens::auction::{AuctionInfo, Bid, TokenAuctionState};
use andromeda_std::{common::OrderBy, error::ContractError};
use cosmwasm_std::{Addr, Order, StdResult, Storage, Uint128};
use cosmwasm_std::{Order, StdResult, Storage, Uint128};

use cw_storage_plus::{Bound, Index, IndexList, IndexedMap, Item, Map, MultiIndex};

Expand All @@ -15,8 +15,6 @@ pub const BIDS: Map<u128, Vec<Bid>> = Map::new("bids"); // auction_id -> [bids]

pub const TOKEN_AUCTION_STATE: Map<u128, TokenAuctionState> = Map::new("auction_token_state");

pub const VALID_TOKEN_CONTRACTS: Map<Addr, bool> = Map::new("valid_token_contracts");

pub struct AuctionIdIndices<'a> {
/// PK: token_id + token_address
/// Secondary key: token_address
Expand Down
13 changes: 13 additions & 0 deletions packages/andromeda-non-fungible-tokens/src/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ pub enum QueryMsg {
start_after: Option<String>,
limit: Option<u64>,
},
/// Gets all of the authorized addresses for the auction
#[returns(AuthorizedAddressesResponse)]
AuthorizedAddresses {
start_after: Option<String> 10000 ;,
limit: Option<u32>,
order_by: Option<OrderBy>,
},

/// Gets the bids for the given auction id. Start_after starts indexing at 0.
#[returns(BidsResponse)]
Bids {
Expand Down Expand Up @@ -192,6 +200,11 @@ pub struct AuctionStateResponse {
pub is_cancelled: bool,
}

#[cw_serde]
pub struct AuthorizedAddressesResponse {
pub addresses: Vec<String>,
}

#[cw_serde]
pub struct AuctionIdsResponse {
pub auction_ids: Vec<Uint128>,
Expand Down
84 changes: 79 additions & 5 deletions packages/std/src/ado_contract/permissioning.rs
6D4E
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
ado_base::permissioning::{Permission, PermissionInfo, PermissioningMessage},
amp::{messages::AMPPkt, AndrAddr},
common::context::ExecuteContext,
common::{context::ExecuteContext, OrderBy},
error::ContractError,
};
use cosmwasm_std::{ensure, Deps, Env, MessageInfo, Order, Response, Storage};
Expand All @@ -16,12 +16,13 @@ pub struct PermissionsIndices<'a> {
/// PK: action + actor
///
/// Secondary key: actor
pub permissions: MultiIndex<'a, String, PermissionInfo, String>,
pub actor: MultiIndex<'a, String, PermissionInfo, String>,
pub action: MultiIndex<'a, String, PermissionInfo, String>,
}

impl<'a> IndexList<PermissionInfo> for PermissionsIndices<'a> {
fn get_indexes(&'_ self) -> Box<dyn Iterator<Item = &'_ dyn Index<PermissionInfo>> + '_> {
let v: Vec<&dyn Index<PermissionInfo>> = vec![&self.permissions];
let v: Vec<&dyn Index<PermissionInfo>> = vec![&self.action, &self.actor];
Box::new(v.into_iter())
}
}
Expand All @@ -31,7 +32,12 @@ impl<'a> IndexList<PermissionInfo> for PermissionsIndices<'a> {
/// Permissions are stored in a multi-indexed map with the primary key being the action and actor
pub fn permissions<'a>() -> IndexedMap<'a, &'a str, PermissionInfo, PermissionsIndices<'a>> {
let indexes = PermissionsIndices {
permissions: MultiIndex::new(|_pk: &[u8], r| r.actor.clone(), "andr_permissions", "actor"),
actor: MultiIndex::new(|_pk: &[u8], r| r.actor.clone(), "andr_permissions", "actor"),
action: MultiIndex::new(
|_pk: &[u8], r| r.action.clone(),
"andr_permissions",
"action",
),
};
IndexedMap::new("andr_permissions", indexes)
}
Expand Down Expand Up @@ -328,7 +334,7 @@ impl<'a> ADOContract<'a> {
let limit = limit.unwrap_or(DEFAULT_QUERY_LIMIT).min(MAX_QUERY_LIMIT) as usize;
let permissions = permissions()
.idx
.permissions
.actor
.prefix(actor)
.range(deps.storage, min, None, Order::Ascending)
.take(limit)
Expand All @@ -345,6 +351,42 @@ impl<'a> ADOContract<'a> {
.collect::<Vec<String>>();
Ok(actions)
}

pub fn query_permissioned_actors(
&self,
deps: Deps,
action: impl Into<String>,
start_after: Option<String>,
limit: Option<u32>,
order_by: Option<OrderBy>,
) -> Result<Vec<String>, ContractError> {
let action_string: String = action.into();
let order_by = match order_by {
Some(OrderBy::Desc) => Order::Descending,
_ => Order::Ascending,
};

let actors = permissions()
.idx
.action
.prefix(action_string.clone())
.keys(
deps.storage,
start_after.map(Bound::inclusive),
None,
order_by,
)
.take((limit).unwrap_or(DEFAULT_QUERY_LIMIT).min(MAX_QUERY_LIMIT) as usize)
.map(|p| {
p.unwrap()
.strip_prefix(action_string.as_str())
.unwrap()
.to_string()
})
.collect::<Vec<String>>();

Ok(actors)
}
}

/// Checks if the provided context is authorised to perform the provided action.
Expand Down Expand Up @@ -978,4 +1020,36 @@ mod tests {
assert_eq!(actions.len(), 1);
assert_eq!(actions[0], "action");
}

#[test]
fn test_query_permissioned_actors() {
let mut deps = mock_dependencies();
let env = mock_env();
let info = mock_info("owner", &[]);
let ctx = ExecuteContext {
deps: deps.as_mut(),
env,
info: info.clone(),
amp_ctx: None,
};

let contract = ADOContract::default();

contract.owner.save(ctx.deps.storage, &info.sender).unwrap();

let actor = "actor";
let action = "action";
ADOContract::default()
.execute_permission_action(ctx, action)
.unwrap();

ADOContract::set_permission(deps.as_mut().storage, action, actor, Permission::default())
.unwrap();
let actors = ADOContract::default()
.query_permissioned_actors(deps.as_ref(), action, None, None, None)
.unwrap();

assert_eq!(actors.len(), 1);
assert_eq!(actors[0], actor);
}
}
0