Skip to content
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

Implement approval query; fix helper queries #32

Merged
merged 13 commits into from
Dec 3, 2021
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions contracts/cw721-base/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw721-base"
version = "0.10.0"
version = "0.10.1"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we release a minor version after merge @ethanfrey?

authors = ["Ethan Frey <ethanfrey@users.noreply.github.com>"]
edition = "2018"
description = "Basic implementation cw721 NFTs"
Expand All @@ -27,7 +27,7 @@ library = []
[dependencies]
cw0 = { version = "0.10.2" }
cw2 = { version = "0.10.2" }
cw721 = { path = "../../packages/cw721", version = "0.10.0" }
cw721 = { path = "../../packages/cw721", version = "0.10.1" }
cw-storage-plus = { version = "0.10.2" }
cosmwasm-std = { version = "1.0.0-beta2" }
schemars = "0.8.6"
Expand Down
25 changes: 22 additions & 3 deletions contracts/cw721-base/src/contract_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{from_binary, to_binary, CosmosMsg, DepsMut, Empty, Response, WasmMsg};

use cw721::{
ApprovedForAllResponse, ContractInfoResponse, Cw721Query, Cw721ReceiveMsg, Expiration,
NftInfoResponse, OwnerOfResponse,
Approval, ApprovalResponse, ApprovedForAllResponse, ContractInfoResponse, Cw721Query,
Cw721ReceiveMsg, Expiration, NftInfoResponse, OwnerOfResponse,
};

use crate::{
Expand Down Expand Up @@ -470,7 +470,7 @@ fn approving_all_revoking_all() {
};
let owner = mock_info("demeter", &[]);
let res = contract
.execute(deps.as_mut(), mock_env(), owner, approve_all_msg)
.execute(deps.as_mut(), mock_env(), owner.clone(), approve_all_msg)
.unwrap();
assert_eq!(
res,
Expand All @@ -480,6 +480,25 @@ fn approving_all_revoking_all() {
.add_attribute("operator", "random")
);

// test approval query
let res = contract
.approval(
deps.as_ref(),
mock_env(),
owner.sender.into_string(),
String::from("random"),
)
.unwrap();
assert_eq!(
res,
ApprovalResponse {
approval: Approval {
spender: String::from("random"),
expires: Expiration::Never {}
}
}
);

// random can now transfer
let random = mock_info("random", &[]);
let transfer_msg = ExecuteMsg::TransferNft {
Expand Down
8 changes: 8 additions & 0 deletions contracts/cw721-base/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ pub enum QueryMsg {
/// unset or false will filter out expired approvals, you must set to true to see them
include_expired: Option<bool>,
},

/// Return operator that can access all of the owner's tokens.
/// Return type: `ApprovedResponse`
Approved {
owner: String,
operator: String,
},

/// List all operators that can access all of the owner's tokens
/// Return type: `ApprovedForAllResponse`
ApprovedForAll {
Expand Down
30 changes: 28 additions & 2 deletions contracts/cw721-base/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use cosmwasm_std::{to_binary, Binary, BlockInfo, Deps, Env, Order, Record, StdEr

use cw0::maybe_addr;
use cw721::{
AllNftInfoResponse, ApprovedForAllResponse, ContractInfoResponse, CustomMsg, Cw721Query,
Expiration, NftInfoResponse, NumTokensResponse, OwnerOfResponse, TokensResponse,
AllNftInfoResponse, ApprovalResponse, ApprovedForAllResponse, ContractInfoResponse, CustomMsg,
Cw721Query, Expiration, NftInfoResponse, NumTokensResponse, OwnerOfResponse, TokensResponse,
};
use cw_storage_plus::Bound;

Expand Down Expand Up @@ -79,6 +79,29 @@ where
Ok(ApprovedForAllResponse { operators: res? })
}

fn approval(
&self,
deps: Deps,
_env: Env,
owner: String,
operator: String,
) -> StdResult<ApprovalResponse> {
let owner_addr = deps.api.addr_validate(&owner)?;
let operator_addr = deps.api.addr_validate(&operator)?;

let expires = self
.operators
.key((&owner_addr, &operator_addr))
.load(deps.storage)?;

Ok(ApprovalResponse {
approval: cw721::Approval {
spender: operator,
expires,
},
})
}

fn tokens(
&self,
deps: Deps,
Expand Down Expand Up @@ -197,6 +220,9 @@ where
QueryMsg::AllTokens { start_after, limit } => {
to_binary(&self.all_tokens(deps, start_after, limit)?)
}
QueryMsg::Approved { owner, operator } => {
to_binary(&self.approval(deps, env, owner, operator)?)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cw721/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw721"
version = "0.10.0"
version = "0.10.1"
authors = ["Ethan Frey <ethanfrey@users.noreply.github.com>"]
edition = "2018"
description = "Definition and types for the CosmWasm-721 NFT interface"
Expand Down
4 changes: 2 additions & 2 deletions packages/cw721/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub use cw0::Expiration;
pub use crate::helpers::Cw721Contract;
pub use crate::msg::Cw721ExecuteMsg;
pub use crate::query::{
AllNftInfoResponse, Approval, ApprovedForAllResponse, ContractInfoResponse, Cw721QueryMsg,
NftInfoResponse, NumTokensResponse, OwnerOfResponse, TokensResponse,
AllNftInfoResponse, Approval, ApprovalResponse, ApprovedForAllResponse, ContractInfoResponse,
Cw721QueryMsg, NftInfoResponse, NumTokensResponse, OwnerOfResponse, TokensResponse,
};
pub use crate::receiver::Cw721ReceiveMsg;
pub use crate::traits::{CustomMsg, Cw721, Cw721Execute, Cw721Query};
10 changes: 10 additions & 0 deletions packages/cw721/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ pub enum Cw721QueryMsg {
/// unset or false will filter out expired approvals, you must set to true to see them
include_expired: Option<bool>,
},

/// Return operator that can access all of the owner's tokens.
/// Return type: `ApprovedResponse`
Approved { owner: String, operator: String },

/// List all operators that can access all of the owner's tokens.
/// Return type: `ApprovedForAllResponse`
ApprovedForAll {
Expand Down Expand Up @@ -74,6 +79,11 @@ pub struct Approval {
pub expires: Expiration,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct ApprovalResponse {
pub approval: Approval,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct ApprovedForAllResponse {
pub operators: Vec<Approval>,
Expand Down
9 changes: 9 additions & 0 deletions packages/cw721/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::query::ApprovalResponse;
use crate::{
AllNftInfoResponse, ApprovedForAllResponse, ContractInfoResponse, NftInfoResponse,
NumTokensResponse, OwnerOfResponse, TokensResponse,
Expand Down Expand Up @@ -123,6 +124,14 @@ where
limit: Option<u32>,
) -> StdResult<ApprovedForAllResponse>;

fn approval(
&self,
deps: Deps,
env: Env,
owner: String,
spender: String,
) -> StdResult<ApprovalResponse>;

fn tokens(
&self,
deps: Deps,
Expand Down